aboutsummaryrefslogtreecommitdiff
path: root/degesch.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2015-05-16 12:39:30 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2015-05-16 12:39:30 +0200
commitc2c82d20dd0f508c0fd928e2b192fe821e84b990 (patch)
tree48b74ee92d2426eb9c377ed4453255ff290f88f1 /degesch.c
parentca8540e21703e002d66a5c280753d4d3ee4e4924 (diff)
downloadxK-c2c82d20dd0f508c0fd928e2b192fe821e84b990.tar.gz
xK-c2c82d20dd0f508c0fd928e2b192fe821e84b990.tar.xz
xK-c2c82d20dd0f508c0fd928e2b192fe821e84b990.zip
degesch: avoid crashing on null user info
Diffstat (limited to 'degesch.c')
-rw-r--r--degesch.c81
1 files changed, 41 insertions, 40 deletions
diff --git a/degesch.c b/degesch.c
index de093c6..b336938 100644
--- a/degesch.c
+++ b/degesch.c
@@ -3269,14 +3269,52 @@ end:
// --- Connection establishment ------------------------------------------------
+static bool
+irc_autofill_user_info (struct server *s, struct error **e)
+{
+ const char *nickname = get_config_string (s->config, "nickname");
+ const char *username = get_config_string (s->config, "username");
+ const char *realname = get_config_string (s->config, "realname");
+
+ if (nickname && username && realname)
+ return true;
+
+ // Read POSIX user info and fill the configuration if needed
+ struct passwd *pwd = getpwuid (geteuid ());
+ if (!pwd)
+ FAIL ("cannot retrieve user information: %s", strerror (errno));
+
+ // FIXME: set_config_strings() writes errors on its own
+ if (!nickname)
+ set_config_string (s->config, "nickname", pwd->pw_name);
+ if (!username)
+ set_config_string (s->config, "username", pwd->pw_name);
+
+ // Not all systems have the GECOS field but the vast majority does
+ if (!realname)
+ {
+ char *gecos = pwd->pw_gecos;
+
+ // The first comma, if any, ends the user's real name
+ char *comma = strchr (gecos, ',');
+ if (comma)
+ *comma = '\0';
+
+ set_config_string (s->config, "realname", gecos);
+ }
+
+ return true;
+}
+
static void
irc_register (struct server *s)
{
+ // Fill in user information automatically if needed
+ irc_autofill_user_info (s, NULL);
+
const char *nickname = get_config_string (s->config, "nickname");
const char *username = get_config_string (s->config, "username");
const char *realname = get_config_string (s->config, "realname");
-
- // These are filled automatically if needed
hard_assert (nickname && username && realname);
irc_send (s, "NICK %s", nickname);
@@ -6319,43 +6357,6 @@ app_editline_init (struct input *self)
// --- Configuration loading ---------------------------------------------------
static bool
-autofill_user_info (struct server *s, struct error **e)
-{
- const char *nickname = get_config_string (s->config, "nickname");
- const char *username = get_config_string (s->config, "username");
- const char *realname = get_config_string (s->config, "realname");
-
- if (nickname && username && realname)
- return true;
-
- // Read POSIX user info and fill the configuration if needed
- struct passwd *pwd = getpwuid (geteuid ());
- if (!pwd)
- FAIL ("cannot retrieve user information: %s", strerror (errno));
-
- // FIXME: set_config_strings() writes errors on its own
- if (!nickname)
- set_config_string (s->config, "nickname", pwd->pw_name);
- if (!username)
- set_config_string (s->config, "username", pwd->pw_name);
-
- // Not all systems have the GECOS field but the vast majority does
- if (!realname)
- {
- char *gecos = pwd->pw_gecos;
-
- // The first comma, if any, ends the user's real name
- char *comma = strchr (gecos, ',');
- if (comma)
- *comma = '\0';
-
- set_config_string (s->config, "realname", gecos);
- }
-
- return true;
-}
-
-static bool
read_file (const char *filename, struct str *output, struct error **e)
{
FILE *fp = fopen (filename, "rb");
@@ -6664,7 +6665,7 @@ create_server (struct app_context *ctx)
s->reconnect_delay = get_config_integer (s->config, "reconnect_delay");
struct error *e = NULL;
- if (!autofill_user_info (s, &e))
+ if (!irc_autofill_user_info (s, &e))
{
print_error ("%s: %s", "failed to fill in user details", e->message);
error_free (e);