Przeglądaj źródła

* Port [3218] to 1.2.14
* All bots are now rehashed after editing binary - nick casing, ip changes are taken into effect.
* Bots now use the shell username instead of botnick when ident does not work.
* Move a lot of the rehash logic into confedit()
* Add origbotnick to handle the original -B nick, as origbotname is the preferred nick on irc and is changed with .botset 'nick'


svn: 3219

Bryan Drewery 19 lat temu
rodzic
commit
129139a1dd
13 zmienionych plików z 105 dodań i 91 usunięć
  1. 2 0
      doc/UPDATES
  2. 29 33
      src/binary.c
  3. 11 3
      src/chanprog.c
  4. 1 1
      src/chanprog.h
  5. 5 5
      src/cmds.c
  6. 33 30
      src/conf.c
  7. 4 4
      src/conf.h
  8. 1 1
      src/dcc.c
  9. 5 4
      src/main.c
  10. 8 4
      src/misc.c
  11. 1 1
      src/net.h
  12. 4 4
      src/shell.c
  13. 1 1
      src/users.c

+ 2 - 0
doc/UPDATES

@@ -15,6 +15,8 @@ Lines prefixed with '-' were disabled before release and are not finished, or ar
 * Delay 'op-requests'.time seconds before re-requesting op on opeless channels.
 * Fix some issues with case with pid files, nicks, conf loading. (This will break some bots upgrading to 1.2.14)
 * Fix bot not reconnecting with new ip/host when -HUPd. (fixes #340)
+* All bots are now rehashed after editing binary - nick casing, ip changes are taken into effect.
+* Bots now use the shell username instead of botnick when ident does not work.
 
 1.2.13 - http://wraith.shatow.net/milestone/1.2.13
 * Fix cmd_chanset accepting invalid flags

+ 29 - 33
src/binary.c

@@ -569,7 +569,8 @@ void reload_bin_data() {
     bool was_localhub = conf.bot->localhub ? 1 : 0;
     
     /* save the old bots list */
-    oldbots = conf_bots_dup(conf.bots);
+    if (conf.bots)
+      oldbots = conf_bots_dup(conf.bots);
 
     /* Save the old conf.bot */
     oldbot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
@@ -577,27 +578,25 @@ void reload_bin_data() {
 
     /* free up our current conf struct */
     free_conf();
+
     /* Fill conf[] with binary data from settings[] */
     bin_to_conf();
 
     /* fill up conf.bot using origbotname */
     fill_conf_bot(0); /* 0 to avoid exiting if conf.bot cannot be filled */
 
-    /* add any new bots not in userfile */
-    conf_add_userlist_bots();
-
-    /* kill and remove bots removed from conf */
-    if (oldbots) {
-      kill_removed_bots(oldbots, conf.bots);
-
-      /* If we don't have conf.bot, then all bots were removed or just our own record */
-      if ((!conf.bot && was_localhub) || 
-           (conf.bot && !conf.bot->localhub && was_localhub)
-          ) {
-
-        /* no longer the localhub (or removed), need to alert the new one to rehash (or start it) */
+    if (was_localhub) {
+      /* add any new bots not in userfile */
+      conf_add_userlist_bots();
 
+       /* deluser removed bots from conf */
+      if (oldbots)
+        deluser_removed_bots(oldbots, conf.bots);
+#ifdef this_is_handled_by_confedit_now
+      /* no longer the localhub (or removed), need to alert the new one to rehash (or start it) */
+      if (!conf.bot || !conf.bot->localhub) {
         conf_bot *localhub = conf_getlocalhub(conf.bots);
+
         /* then SIGHUP new localhub or spawn new localhub */
         if (localhub) {
           /* Check for pid again - may be using fork-interval */
@@ -608,14 +607,12 @@ void reload_bin_data() {
                start new localhub - done below in spawnbots() */
         }
       }
-    }
-
-    /* start/disable new bots as necesary */
-    conf_checkpids();
-    spawnbots(1);		//1 signifies to not start me!
 
-    if (!conf.bot || !conf.bot->localhub)
-      free_conf_bots(conf.bots);
+      /* start/disable new bots as necesary */
+      conf_checkpids(conf.bots);
+      spawnbots(1);		//1 signifies to not start me!
+#endif
+    }
 
     if (conf.bot && conf.bot->disabled) {
       if (tands > 0) {
@@ -627,9 +624,7 @@ void reload_bin_data() {
         nuke_server("Bot disabled in binary.");
 
       werr(ERR_BOTDISABLED);
-    }
-
-    if (!conf.bot) {
+    } else if (!conf.bot) {
       conf.bot = oldbot;
 
       if (tands > 0) {
@@ -643,19 +638,20 @@ void reload_bin_data() {
       werr(ERR_BADBOT);
     }
 
-    if (oldbot) {
-      if (strcmp(conf.bot->nick, oldbot->nick)) {
-        change_handle(conf.bot->u, conf.bot->nick);
-//        var_set_by_name(conf.bot->nick, "nick", conf.bot->nick);
-//        var_set_userentry(conf.bot->nick, "nick", conf.bot->nick);
-      }
-
-
-      free_bot(oldbot);
+    /* The bot nick changed! (case) */
+    if (strcmp(conf.bot->nick, oldbot->nick)) {
+      change_handle(conf.bot->u, conf.bot->nick);
+//      var_set_by_name(conf.bot->nick, "nick", conf.bot->nick);
+//      var_set_userentry(conf.bot->nick, "nick", conf.bot->nick);
     }
 
+    free_bot(oldbot);
+
     if (oldbots)
       free_conf_bots(oldbots);
+
+    if (!conf.bot->localhub)
+      free_conf_bots(conf.bots);
   }
 }
 

+ 11 - 3
src/chanprog.c

@@ -20,6 +20,7 @@
 #include "net.h"
 #include "misc.h"
 #include "users.h"
+#include "botnet.h"
 #include "userrec.h"
 #include "main.h"
 #include "debug.h"
@@ -35,8 +36,9 @@
 
 struct chanset_t 	*chanset = NULL;	/* Channel list			*/
 char 			admin[121] = "";	/* Admin info			*/
-char 			origbotname[NICKLEN + 1] = "";
-char 			botname[NICKLEN + 1] = "";	/* Primary botname		*/
+char			origbotnick[NICKLEN + 1] = "";	/* from -B (placed into conf.bot->nick .. for backup when conf is cleared */
+char 			origbotname[NICKLEN + 1] = "";	/* Nick to regain */
+char 			botname[NICKLEN + 1] = "";	/* IRC nickname */
 port_t     		my_port = 0;
 bool			reset_chans = 0;
 
@@ -556,8 +558,14 @@ void chanprog()
 
   /* Check if our ip changed during a rehash */
   if (ip4) {
-    if (strcmp(ip4, myipstr(4)) || strcmp(ip6, myipstr(6)))
+    if (strcmp(ip4, myipstr(4)) || strcmp(ip6, myipstr(6))) {
+      if (tands > 0) {
+        botnet_send_chat(-1, conf.bot->nick, "IP changed.");
+        botnet_send_bye("IP changed.");
+      }
       fatal("IP changed.", 1);
+    }
+
     free(ip4);
     free(ip6);
   }

+ 1 - 1
src/chanprog.h

@@ -29,7 +29,7 @@ void add_myself_to_userlist();
 void load_internal_users();
 
 extern struct chanset_t		*chanset;
-extern char			admin[], origbotname[NICKLEN + 1], botname[];
+extern char			admin[], origbotnick[NICKLEN + 1], origbotname[NICKLEN + 1], botname[];
 extern port_t			my_port;
 extern bool			reset_chans;
 

+ 5 - 5
src/cmds.c

@@ -1750,7 +1750,7 @@ static void cmd_conf(int idx, char *par)
 #endif /* !CYGWIN_HACKS */
 
   if (listbot || !egg_strcasecmp(cmd, "list")) {
-    conf_checkpids();
+    conf_checkpids(conf.bots);
     conf_bot *bot = NULL;
     unsigned int i = 0;
 
@@ -1775,11 +1775,11 @@ static void cmd_conf(int idx, char *par)
     /* rewrite our binary */
     conf_to_bin(&conf, 0, -1);
 
-    kill_removed_bots(oldlist, conf.bots);
+//    kill_removed_bots(oldlist, conf.bots);
     conf_add_userlist_bots();
-    conf_checkpids();
-    spawnbots(1);
-    conf_checkpids(0);
+    conf_checkpids(conf.bots);
+    spawnbots(conf.bots, 1);
+    conf_checkpids(conf.bots, 0);
   }
 
   free_conf_bots(oldlist);

+ 33 - 30
src/conf.c

@@ -100,11 +100,11 @@ void spawnbot(const char *nick)
  * bots prefixxed with '/' will be killed auto if running.
  * if (updating) then we were called with -U or -u */
 void
-spawnbots(bool rehashed)
+spawnbots(conf_bot *bots, bool rehashed)
 {
   conf_bot *bot = NULL;
 
-  for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
+  for (bot = bots; bot && bot->nick; bot = bot->next) {
     sdprintf("checking bot: %s", bot->nick);
 
     if (bot->disabled) {
@@ -118,7 +118,8 @@ spawnbots(bool rehashed)
       -if updating and we find our nick, skip
       -if pid exists and not updating, bot is running and we have nothing more to do, skip.
      */
-    } else if ((!egg_strcasecmp(bot->nick, conf.bot->nick) && (updating == UPDATE_AUTO || rehashed)) || (bot->pid && !updating)) {
+    } else if ((conf.bot && !egg_strcasecmp(bot->nick, conf.bot->nick) && 
+               (updating == UPDATE_AUTO || rehashed)) || (bot->pid && !updating)) {
       sdprintf(" ... skipping. Updating: %d, pid: %d", updating, bot->pid);
       continue;
     } else {
@@ -134,7 +135,7 @@ spawnbots(bool rehashed)
 }
 
 int
-conf_killbot(const char *botnick, conf_bot *bot, int signal)
+conf_killbot(conf_bot *bots, const char *botnick, conf_bot *bot, int signal)
 {
   int ret = -1;
 
@@ -142,9 +143,11 @@ conf_killbot(const char *botnick, conf_bot *bot, int signal)
     if (bot->pid)
       ret = kill(bot->pid, signal);
   } else {
-    for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
+    for (bot = bots; bot && bot->nick; bot = bot->next) {
       /* kill all bots but myself if botnick==NULL, otherwise just kill botnick */
-      if ((!botnick && egg_strcasecmp(conf.bot->nick, bot->nick)) || (botnick && !egg_strcasecmp(botnick, bot->nick))) {
+      if ((!conf.bot) || 
+          (!botnick && (conf.bot->nick && egg_strcasecmp(conf.bot->nick, bot->nick))) || 
+          (botnick && !egg_strcasecmp(botnick, bot->nick))) {
         if (bot->pid)
           ret = kill(bot->pid, signal);
 
@@ -200,7 +203,7 @@ confedit()
   struct stat st, sn;
   struct timespec ts1, ts2;           /* time before and after edit */
   bool autowrote = 0;
-  conf_bot *localhub = NULL, *localhub_old = NULL;
+  conf_bot *oldbots = NULL;
 
   um = umask(077);
 
@@ -314,26 +317,25 @@ confedit()
     }
   }
 
-  localhub_old = conf_getlocalhub(conf.bots);
-  
-  if (localhub_old) {
-    localhub = (conf_bot *) my_calloc(1, sizeof(conf_bot));
-    conf_bot_dup(localhub, localhub_old);
-  }
-
   tmpconf.my_close();
+
+  oldbots = conf_bots_dup(conf.bots);
   free_conf();
+
   readconf((const char *) tmpconf.file, 0);               /* read cleartext conf tmp into &settings */
   expand_tilde(&conf.binpath);
   expand_tilde(&conf.datadir);
   unlink(tmpconf.file);
   conf_to_bin(&conf, 0, -1);
 
-  /* Lookup the pid now in case it changed while in the editor */
-  if (localhub) {
-    localhub->pid = checkpid(localhub->nick, localhub, NULL);
-    kill(localhub->pid, SIGUSR1);
-  }
+  /* Now signal all of the old bots with SIGUSR1
+   * They will auto die and determine new localhub, etc..
+   */
+  conf_checkpids(oldbots);
+  conf_killbot(oldbots, NULL, NULL, SIGUSR1);
+
+  /* Now spawn new bots */
+  spawnbots(conf.bots);
 
   exit(0);
 
@@ -426,11 +428,11 @@ static void conf_compat_pids()
   }  
 }
 
-void conf_checkpids(bool all)
+void conf_checkpids(conf_bot *bots, bool all)
 {
   conf_bot *bot = NULL;
 
-  for (bot = conf.bots; bot && bot->nick; bot = bot->next)
+  for (bot = bots; bot && bot->nick; bot = bot->next)
     if (all || (!all && bot->pid == 0))
       bot->pid = checkpid(bot->nick, bot, NULL);
 }
@@ -494,7 +496,7 @@ checkpid(const char *nick, conf_bot *bot, const char *usedir)
     }
 
     if (bufp[0] && pid && can_stat(bufp) && (getpid() == pid) &&
-        !egg_strncasecmp(nick, origbotname, HANDLEN)) {
+        !egg_strncasecmp(nick, origbotnick, HANDLEN)) {
       socksfile = strdup(bufp);
       return 0;
     }
@@ -614,7 +616,7 @@ conf_delbot(char *botn)
   for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
     if (!egg_strcasecmp(bot->nick, botn)) {     /* found it! */
       bot->pid = checkpid(bot->nick, bot, NULL);
-      conf_killbot(NULL, bot, SIGKILL);
+      conf_killbot(conf.bots, NULL, bot, SIGKILL);
       free_bot(bot);
       return 0;
     }
@@ -1035,7 +1037,7 @@ conf_bot *conf_bots_dup(conf_bot *src)
   return ret;
 }
 
-void kill_removed_bots(conf_bot *oldlist, conf_bot *newlist)
+void deluser_removed_bots(conf_bot *oldlist, conf_bot *newlist)
 {
   if (oldlist) {
     conf_bot *botold = NULL, *botnew = NULL;
@@ -1050,9 +1052,10 @@ void kill_removed_bots(conf_bot *oldlist, conf_bot *newlist)
           break;
         }
       }
-      if (!found && egg_strcasecmp(botold->nick, origbotname)) {	/* Never kill ME.. will handle it elsewhere */
-        botold->pid = checkpid(botold->nick, botold, NULL);
-        conf_killbot(NULL, botold, SIGKILL);
+      if (!found && egg_strcasecmp(botold->nick, origbotnick)) {	/* Never kill ME.. will handle it elsewhere */
+	/* No need to kill -- they are signalled and they will die on their own now */
+        //botold->pid = checkpid(botold->nick, botold, NULL);
+        //conf_killbot(conf.bots, NULL, botold, SIGKILL);
         if ((u = get_user_by_handle(userlist, botold->nick))) {
           putlog(LOG_MISC, "*", "Removing '%s' as it has been removed from the binary config.", botold->nick);
           if (server_online)
@@ -1081,9 +1084,9 @@ fill_conf_bot(bool fatal)
   /* This first clause should actually be obsolete */
   if (!used_B && conf.bots && conf.bots->nick) {
     mynick = strdup(conf.bots->nick);
-    strlcpy(origbotname, conf.bots->nick, HANDLEN + 1);
+    strlcpy(origbotnick, conf.bots->nick, HANDLEN + 1);
   } else
-    mynick = strldup(origbotname, HANDLEN);
+    mynick = strldup(origbotnick, HANDLEN);
 
   sdprintf("mynick: %s", mynick);
 
@@ -1172,7 +1175,7 @@ bin_to_conf(bool error)
   if (clear_tmpdir)
     clear_tmp();	/* clear out the tmp dir, no matter if we are localhub or not */
   conf_compat_pids();
-  conf_checkpids();
+  conf_checkpids(conf.bots);
 
   tellconf();
 }

+ 4 - 4
src/conf.h

@@ -56,8 +56,8 @@ enum {
 
 
 void spawnbot(const char *);
-void spawnbots(bool rehashed = 0);
-int conf_killbot(const char *, conf_bot *, int);
+void spawnbots(conf_bot *bots, bool rehashed = 0);
+int conf_killbot(conf_bot *, const char *, conf_bot *, int);
 void confedit() __attribute__((noreturn));
 void conf_addbot(char *, char *, char *, char *);
 int conf_delbot(char *);
@@ -71,10 +71,10 @@ int parseconf(bool);
 int writeconf(char *, FILE *, int);
 void fill_conf_bot(bool fatal = 1);
 void bin_to_conf(bool error = 0);
-void conf_checkpids(bool all = 1);
+void conf_checkpids(conf_bot *bots, bool all = 1);
 void conf_add_userlist_bots();
 conf_bot *conf_bots_dup(conf_bot *);
-void kill_removed_bots(conf_bot *, conf_bot *);
+void deluser_removed_bots(conf_bot *, conf_bot *);
 conf_bot *conf_getlocalhub(conf_bot *);
 void conf_setmypid(pid_t);
 void conf_bot_dup(conf_bot *dest, conf_bot *src);

+ 1 - 1
src/dcc.c

@@ -1290,7 +1290,7 @@ detect_telnet_flood(char *floodhost)
     lasttelnettime = 0;
     lasttelnethost[0] = 0;
     putlog(LOG_MISC, "*", "Telnet connection flood from %s!  Placing on ignore!", floodhost);
-    addignore(floodhost, origbotname, "Telnet connection flood", now + (60 * ignore_time));
+    addignore(floodhost, conf.bot->nick, "Telnet connection flood", now + (60 * ignore_time));
     return 1;
   }
   return 0;

+ 5 - 4
src/main.c

@@ -311,6 +311,7 @@ static void dtx_arg(int argc, char *argv[])
       case 'B':
         used_B = 1;
         strlcpy(origbotname, optarg, HANDLEN + 1);
+        strlcpy(origbotnick, optarg, HANDLEN + 1);
         break;
       case 'H':
         printf("SHA1 (%s): %s\n", optarg, SHA1(optarg));
@@ -570,7 +571,7 @@ static void startup_checks(int hack) {
 
   if (can_stat(cfile))
     readconf(cfile, 0);	/* will read into &conf struct */
-  conf_checkpids();
+  conf_checkpids(conf.bots);
 #endif /* CYGWIN_HACKS */
 
 #ifndef CYGWIN_HACKS
@@ -600,7 +601,7 @@ static void startup_checks(int hack) {
     if (do_killbot[0]) {
       const char *what = (kill_sig == SIGKILL ? "kill" : "restart");
 
-      if (conf_killbot(do_killbot, NULL, kill_sig) == 0)
+      if (conf_killbot(conf.bots, do_killbot, NULL, kill_sig) == 0)
         printf("'%s' successfully %sed.\n", do_killbot, what);
       else {
         printf("Error %sing '%s'\n", what, do_killbot);
@@ -624,7 +625,7 @@ static void startup_checks(int hack) {
       if (!conf.bots || !conf.bots->nick)     /* no bots ! */
         werr(ERR_NOBOTS);
 
-      spawnbots();
+      spawnbots(conf.bots);
       exit(0); /* our job is done! */
     }
   }
@@ -773,7 +774,7 @@ printf("out: %s\n", out);
   console_init();
   chanprog();
 
-  strcpy(botuser, origbotname);
+  strcpy(botuser, conf.username ? conf.username : origbotname);
 
   if (!conf.bot->hub && conf.bot->localhub)
     sdprintf("I am localhub (%s)", conf.bot->nick);

+ 8 - 4
src/misc.c

@@ -633,6 +633,10 @@ readsocks(const char *fname)
     char nserv[50] = "";
 
     if ((ip4 && ip6) && (strcmp(ip4, myipstr(4)) || strcmp(ip6, myipstr(6)))) {
+      if (tands > 0) {		/* We're not linked yet.. but for future */
+        botnet_send_chat(-1, conf.bot->nick, "IP changed.");
+        botnet_send_bye("IP changed.");
+      }
       fatal("IP changed.", 1);
     } else {
       simple_snprintf(nserv, sizeof(nserv), "%s:%d", dcc[servidx].host, dcc[servidx].port);
@@ -875,8 +879,8 @@ int updatebin(int idx, char *par, int secs)
   }
   if (updating == UPDATE_AUTO) {
     /* Make all other bots do a soft restart */
-    conf_checkpids();
-    conf_killbot(NULL, NULL, SIGHUP);
+    conf_checkpids(conf.bots);
+    conf_killbot(conf.bots, NULL, NULL, SIGHUP);
 
     if (conf.bot->pid)
       kill(conf.bot->pid, SIGHUP);
@@ -885,8 +889,8 @@ int updatebin(int idx, char *par, int secs)
 
   if (!conf.bot->hub && secs > 0) {
     /* Make all other bots do a soft restart */
-    conf_checkpids();
-    conf_killbot(NULL, NULL, SIGHUP);
+    conf_checkpids(conf.bots);
+    conf_killbot(conf.bots, NULL, NULL, SIGHUP);
     
     /* invoked with -u */
     if (updating == UPDATE_AUTO) {

+ 1 - 1
src/net.h

@@ -156,7 +156,7 @@ extern union sockaddr_union 		cached_myip6_so;
 extern unsigned long			notalloc;
 #endif /* USE_IPV6 */
 
-extern char				firewall[], botuser[], natip[];
+extern char				firewall[], botuser[];
 extern int				resolve_timeout, MAXSOCKS, socks_total;
 extern bool				identd_hack, cached_ip;
 extern port_t				firewallport;

+ 4 - 4
src/shell.c

@@ -583,8 +583,8 @@ void suicide(const char *msg)
   unlink(binname);
 
   if (conf.bot->localhub) {
-    conf_checkpids();
-    conf_killbot(NULL, NULL, SIGKILL);
+    conf_checkpids(conf.bots);
+    conf_killbot(conf.bots, NULL, NULL, SIGKILL);
   }
   unlink(conf.bot->pid_file);
   //Not recursively clearing these dirs as they may be ~USER/ ..
@@ -646,8 +646,8 @@ void detected(int code, char *msg)
     break;
   }
   if (killbots && conf.bot->localhub) {
-    conf_checkpids();
-    conf_killbot(NULL, NULL, SIGKILL);
+    conf_checkpids(conf.bots);
+    conf_killbot(conf.bots, NULL, NULL, SIGKILL);
   }
   if (do_fatal)
     fatal(msg, 0);

+ 1 - 1
src/users.c

@@ -594,7 +594,7 @@ int readuserfile(const char *file, struct userrec **ret)
   simple_snprintf(s, 180, "%s", temps);
   free(temps);
   if (s[1] < '4') {
-    fatal("boring....", 0);
+    fatal("Empty or malformed userfile.", 0);
   }
   if (s[1] > '4')
     fatal("Invalid userfile format.", 0);