Browse Source

* Fixed a startup segfault. (fixes #195)


svn: 2713
Bryan Drewery 20 years ago
parent
commit
4fc1fac61d
10 changed files with 80 additions and 47 deletions
  1. 1 0
      doc/UPDATES
  2. 1 1
      misc/grep
  3. 6 4
      src/binary.c
  4. 48 32
      src/conf.c
  5. 1 1
      src/conf.h
  6. 2 1
      src/main.c
  7. 7 3
      src/misc.c
  8. 1 1
      src/misc_file.c
  9. 10 3
      src/shell.c
  10. 3 1
      src/shell.h

+ 1 - 0
doc/UPDATES

@@ -59,6 +59,7 @@ Lines prefixed with '-' were disabled before release and are not finished, or ar
 * Fixed some startup segfaults resulting from bugs in glibc.
 * Fixed bot going floodless when it wasn't granted it.
 * Fixed bots requesting op in chans they don't have access to. (fixes #196)
+* Fixed a startup segfault. (fixes #195)
 
 1.2.8
 * Fixed [bot]* cmds depending on case of botnicks.

+ 1 - 1
misc/grep

@@ -1,3 +1,3 @@
 #!/bin/sh
 grep=`which grep`
-grep "$@" | grep -v "^configure" | grep -v "^ChangeLog" | grep -v "^Binary" | grep -v "^src/_" | grep -v "^tmp/" | grep -v "help\..*:" | grep -v "\.svn" | grep -v ".deps.*\.Po" | grep -v "\.c~:" | grep -v "tags:"
+grep "$@" | grep -v "^configure" | grep -v "^ChangeLog" | grep -v "^Binary" | grep -v "^src/_" | grep -v "^tmp/" | grep -v "help\..*:" | grep -v "\.svn" | grep -v ".deps.*\.Po" | grep -v "\.c~:" | grep -v "tags:" | grep -v "\.orig:"

+ 6 - 4
src/binary.c

@@ -504,11 +504,13 @@ void conf_to_bin(conf_t *in, bool move, int die)
   simple_sprintf(settings.pscloak, "%d", in->pscloak);
 
   strlcpy(settings.binname, in->binname, sizeof(settings.binname));
-  strlcpy(settings.username, in->username, sizeof(settings.username));
-
-  strlcpy(settings.uname, in->uname, sizeof(settings.uname));
+  if (in->username)
+    strlcpy(settings.username, in->username, sizeof(settings.username));
+  if (in->uname)
+    strlcpy(settings.uname, in->uname, sizeof(settings.uname));
   strlcpy(settings.datadir, in->datadir, sizeof(settings.datadir));
-  strlcpy(settings.homedir, in->homedir, sizeof(settings.homedir));
+  if (in->homedir)
+    strlcpy(settings.homedir, in->homedir, sizeof(settings.homedir));
   strlcpy(settings.binpath, in->binpath, sizeof(settings.binpath));
   for (bot = in->bots; bot && bot->nick; bot = bot->next) {
     simple_snprintf(settings.bots, sizeof(settings.bots), "%s%s%s %s %s%s %s,", 

+ 48 - 32
src/conf.c

@@ -41,11 +41,11 @@ tellconf()
 {
   conf_bot *bot = NULL;
   int i = 0;
-
   sdprintf("tempdir: %s\n", replace(tempdir, conf.homedir, "~"));
   sdprintf("uid: %d\n", conf.uid);
   sdprintf("uname: %s\n", conf.uname);
   sdprintf("homedir: %s\n", conf.homedir);
+  sdprintf("username: %s\n", conf.username);
   sdprintf("binpath: %s\n", replace(conf.binpath, conf.homedir, "~"));
   sdprintf("binname: %s\n", conf.binname);
   sdprintf("datadir: %s\n", replace(conf.datadir, conf.homedir, "~"));
@@ -314,6 +314,7 @@ confedit()
     localhub_pid = conf.bots->pid;
 
   tmpconf.my_close();
+  free_conf();
   readconf((const char *) tmpconf.file, 0);               /* read cleartext conf tmp into &settings */
   expand_tilde(&conf.binpath);
   expand_tilde(&conf.datadir);
@@ -346,7 +347,8 @@ init_conf()
 #endif /* !CYGWIN_HACKS */
   conf.autouname = 0;
 #ifdef CYGWIN_HACKS
-  conf.binpath = strdup(homedir());
+  if (homedir())
+    conf.binpath = strdup(homedir());
 #else /* !CYGWIN_HACKS */
   conf.binpath = strdup(dirname(binname));
 #endif /* CYGWIN_HACKS */
@@ -601,14 +603,21 @@ free_conf()
   free_conf_bots(conf.bots);
   free_bot(conf.bot);
   conf.bot = NULL;
-  free(conf.localhub);
-  free(conf.uname);
-  free(conf.username);
+  if (conf.localhub)
+    free(conf.localhub);
+  if (conf.uname)
+    free(conf.uname);
+  if (conf.username)
+    free(conf.username);
   if (conf.datadir)
     free(conf.datadir);
-  free(conf.homedir);
-  free(conf.binname);
-  free(conf.binpath);
+  if (conf.homedir)
+    free(conf.homedir);
+  if (conf.binname)
+   free(conf.binname);
+  if (conf.binpath)
+    free(conf.binpath);
+  init_conf();
 }
 
 void
@@ -625,14 +634,23 @@ free_conf_bots(conf_bot *list)
   }
 }
 
+void prep_homedir(bool error)
+{
+  if (!conf.homedir)
+    str_redup(&conf.homedir, homedir());
+
+  if (error && (!conf.homedir || !conf.homedir[0]))
+    werr(ERR_NOHOMEDIR);
+}
+
 int
 parseconf(bool error)
 {
-  if (conf.username) {
+  if (!conf.username)
     str_redup(&conf.username, my_username());
-  } else {
-    conf.username = strdup(my_username());
-  }
+
+  if (error && (!conf.username || !conf.username[0]))
+    werr(ERR_NOUSERNAME);
 
 #ifndef CYGWIN_HACKS
   if (error && conf.uid != (signed) myuid) {
@@ -651,12 +669,6 @@ parseconf(bool error)
   } else if (!conf.uname) { /* if not set, then just set it, wont happen again next time... */
     conf.uname = strdup(my_uname());
   }
-
-  if (conf.homedir) {
-    str_redup(&conf.homedir, homedir());
-  } else {
-    conf.homedir = strdup(homedir());
-  }
   
 #endif /* !CYGWIN_HACKS */
   return 0;
@@ -721,7 +733,7 @@ readconf(const char *fname, int bits)
         if (line[0])
           option = newsplit(&line);
 
-        if (!option)
+        if (!option || !line[0])
           continue;
 
         if (!strcmp(option, "autocron")) {      /* automatically check/create crontab? */
@@ -875,23 +887,23 @@ writeconf(char *filename, FILE * stream, int bits)
 
   comment("");
 
-  if (conf.username && strcmp(conf.username, my_username())) {
+  if (conf.username && my_username() && strcmp(conf.username, my_username())) {
     conf_com();
     my_write(f, "%s! username %s\n", do_confedit == CONF_AUTO ? "" : "#", my_username());
     my_write(f, "%s! username %s\n", do_confedit == CONF_STATIC ? "" : "#", conf.username);
   } else
-    my_write(f, "! username %s\n", conf.username ? conf.username : my_username());
+    my_write(f, "! username %s\n", conf.username ? conf.username : my_username() ? my_username() : "");
 
-  if (conf.homedir && strcmp(conf.homedir, homedir(0))) {
+  if (conf.homedir && homedir(0) && strcmp(conf.homedir, homedir(0))) {
     conf_com();
     my_write(f, "%s! homedir %s\n", do_confedit == CONF_AUTO ? "" : "#", homedir(0));
     my_write(f, "%s! homedir %s\n", do_confedit == CONF_STATIC ? "" : "#", conf.homedir);
   } else 
-    my_write(f, "! homedir %s\n", conf.homedir ? conf.homedir : homedir(0));
+    my_write(f, "! homedir %s\n", conf.homedir ? conf.homedir : homedir(0) ? homedir(0) : "");
 
   comment("\n# binpath needs to be full path unless it begins with '~', which uses 'homedir', ie, '~/'");
 
-  if (strstr(conf.binpath, homedir())) {
+  if (homedir() && strstr(conf.binpath, homedir())) {
     p = replace(conf.binpath, homedir(), "~");
     my_write(f, "! binpath %s\n", p);
   } else
@@ -903,7 +915,7 @@ writeconf(char *filename, FILE * stream, int bits)
   comment("");
 
   comment("# datadir should be set to a static directory that is writable");
-  if (strstr(conf.datadir, homedir())) {
+  if (homedir() && strstr(conf.datadir, homedir())) {
     p = replace(conf.datadir, homedir(), "~");
     my_write(f, "! datadir %s\n", p);
   } else
@@ -1059,17 +1071,17 @@ fill_conf_bot()
 }
 
 void
-bin_to_conf(void)
+bin_to_conf(bool error)
 {
 /* printf("Converting binary data to conf struct\n"); */
   conf.uid = atol(settings.uid);
-  str_redup(&conf.username, settings.username);
+  if (settings.username[0])
+    str_redup(&conf.username, settings.username);
   str_redup(&conf.uname, settings.uname); 
   str_redup(&conf.datadir, settings.datadir);
-  expand_tilde(&conf.datadir);
-  str_redup(&conf.homedir, settings.homedir);
+  if (settings.homedir[0])
+    str_redup(&conf.homedir, settings.homedir);
   str_redup(&conf.binpath, settings.binpath);
-  expand_tilde(&conf.binpath);
   str_redup(&conf.binname, settings.binname);
   conf.portmin = atol(settings.portmin);
   conf.portmax = atol(settings.portmax);
@@ -1078,7 +1090,12 @@ bin_to_conf(void)
   conf.watcher = atoi(settings.watcher);
   conf.pscloak = atoi(settings.pscloak);
 
-  /* BOTS */
+
+  prep_homedir(error);
+  expand_tilde(&conf.datadir);
+  expand_tilde(&conf.binpath);
+
+  /* PARSE/ADD BOTS */
   {
     char *p = NULL, *tmp = NULL, *tmpp = NULL;
  
@@ -1112,7 +1129,6 @@ bin_to_conf(void)
 
   if (clear_tmpdir)
     clear_tmp();	/* clear out the tmp dir, no matter if we are localhub or not */
-
   conf_compat_pids();
   conf_checkpids();
 

+ 1 - 1
src/conf.h

@@ -68,7 +68,7 @@ int readconf(const char *, int);
 int parseconf(bool);
 int writeconf(char *, FILE *, int);
 void fill_conf_bot();
-void bin_to_conf(void);
+void bin_to_conf(bool error = 0);
 void conf_checkpids();
 void conf_add_userlist_bots();
 conf_bot *conf_bots_dup(conf_bot *);

+ 2 - 1
src/main.c

@@ -569,8 +569,9 @@ static void startup_checks(int hack) {
 #endif /* CYGWIN_HACKS */
 
 #ifndef CYGWIN_HACKS
+  /* Only error out with missing homedir when we aren't editing the binary */
   if (settings.uname[0])
-    bin_to_conf();		/* read our memory from settings[] into conf[] */
+    bin_to_conf(do_confedit ? 0 : 1);		/* read our memory from settings[] into conf[] */
 
   if (do_confedit)
     confedit();		/* this will exit() */

+ 7 - 3
src/misc.c

@@ -962,15 +962,19 @@ int goodpass(char *pass, int idx, char *nick)
 
 char *replace(const char *string, const char *oldie, const char *newbie)
 {
-  if (string == NULL) 
-    return "";
+  if (string == NULL || !string[0])
+    return (char *) string;
+
+  static char newstring[1024] = "";
+
+  if (oldie == NULL || !oldie[0])
+    return (char *) string;
 
   char *c = NULL;
 
   if ((c = (char *) strstr(string, oldie)) == NULL) 
     return (char *) string;
 
-  static char newstring[1024] = "";
   const size_t new_len = strlen(newbie), old_len = strlen(oldie), end = (strlen(string) - old_len);
   size_t str_index = 0, newstr_index = 0, oldie_index = c - string, cpy_len;
 

+ 1 - 1
src/misc_file.c

@@ -251,7 +251,7 @@ void Tempfile::FindDir()
   /* If this is a hub, use, "./tmp/" */
   if (conf.bots && conf.bots->nick && conf.bots->hub)
     simple_snprintf(tempdir, DIRMAX, "%s/tmp/", conf.binpath);
-  else {
+  else if (conf.homedir && conf.homedir[0]) {
     //need to create ~/.ssh/  
     simple_snprintf(tempdir, DIRMAX, "%s/.ssh/", conf.homedir);
     clear_tmpdir = 0;

+ 10 - 3
src/shell.c

@@ -664,6 +664,10 @@ char *werr_tostr(int errnum)
     return STR("Bot is disabled, remove '/' in config");
   case ERR_NOBOTS:
     return STR("There are no bots in the binary! Please use ./binary -C to edit");
+  case ERR_NOHOMEDIR:
+    return STR("There is no homedir set. Please set one in the binary config with ./binary -C");
+  case ERR_NOUSERNAME:
+    return STR("There is no username set. Please set one in the binary config with ./binary -C");
   case ERR_NOBOT:
     return STR("I have no bot record but received -B???");
   default:
@@ -821,7 +825,7 @@ char *homedir(bool useconf)
       realpath(tmp, homedir_buf); /* this will convert lame home dirs of /home/blah->/usr/home/blah */
     ContextNote("realpath(): Success");
   }
-  return homedir_buf;
+  return homedir_buf[0] ? homedir_buf : NULL;
 }
 
 char *my_username()
@@ -841,7 +845,7 @@ char *my_username()
       simple_snprintf(username, sizeof username, "%s", pw->pw_name);
 #endif /* CYGWIN_HACKS */
   }
-  return username;
+  return username[0] ? username : NULL;
 }
 
 void mkdir_p(const char *dir) {
@@ -869,6 +873,9 @@ void mkdir_p(const char *dir) {
 
 void expand_tilde(char **ptr)
 {
+  if (!conf.homedir || !conf.homedir[0])
+    return;
+
   char *str = ptr ? *ptr : NULL;
 
   if (str && strchr(str, '~')) {
@@ -878,7 +885,7 @@ void expand_tilde(char **ptr)
     if (str[siz - 1] == '/')
       str[siz - 1] = 0;
 
-    if ((p = replace(str, "~", homedir())))
+    if ((p = replace(str, "~", conf.homedir)))
       str_redup(ptr, p);
     else
       fatal("Unforseen error expanding '~'", 0);

+ 3 - 1
src/shell.h

@@ -19,7 +19,9 @@
 #define ERR_BOTDISABLED	17
 #define ERR_NOBOTS	18
 #define ERR_NOBOT	19
-#define ERR_MAX         20
+#define ERR_NOUSERNAME	20
+#define ERR_NOHOMEDIR	21
+#define ERR_MAX         22
 
 #define EMAIL_OWNERS    BIT0
 #define EMAIL_TEAM      BIT1