Kaynağa Gözat

* Changed the update code around to write_settings() now
* Updated leafs now restart after 120 secs, not 300


svn: 1304

Bryan Drewery 22 yıl önce
ebeveyn
işleme
8830f7402c
11 değiştirilmiş dosya ile 105 ekleme ve 109 silme
  1. 18 13
      src/binary.c
  2. 1 1
      src/binary.h
  3. 5 3
      src/cmds.c
  4. 6 3
      src/conf.c
  5. 0 1
      src/conf.h
  6. 26 6
      src/main.c
  7. 44 68
      src/misc.c
  8. 1 1
      src/mod/update.mod/update.c
  9. 4 2
      src/net.c
  10. 0 2
      src/settings.h
  11. 0 9
      src/shell.c

+ 18 - 13
src/binary.c

@@ -272,12 +272,12 @@ static void edpack(settings_t *incfg, const char *hash, int what)
 #undef dofield
 }
 
-
-void
+/* 
+static void
 tellconfig(settings_t *incfg)
 {
 #define dofield(_field)		printf("%s: %s\n", #_field, _field);
-  /* -- STATIC -- */
+  // -- STATIC --
   dofield(incfg->hash);
   dofield(incfg->packname);
   dofield(incfg->shellhash);
@@ -286,7 +286,7 @@ tellconfig(settings_t *incfg)
   dofield(incfg->owners);
   dofield(incfg->owneremail);
   dofield(incfg->hubs);
-  /* -- DYNAMIC -- */
+  // -- DYNAMIC --
   dofield(incfg->bots);
   dofield(incfg->uid);
   dofield(incfg->autouname);
@@ -302,6 +302,7 @@ tellconfig(settings_t *incfg)
   dofield(incfg->portmax);
 #undef dofield
 }
+*/
 
 void
 check_sum(const char *fname, const char *cfgfile)
@@ -316,7 +317,7 @@ check_sum(const char *fname, const char *cfgfile)
       fatal("Binary not initialized.", 0);
 
     readcfg(cfgfile);
-tellconfig(&settings);
+/* tellconfig(&settings); */
     if (bin_md5(fname, WRITE_MD5, &ctx))
       printf("* Wrote settings to binary.\n"); 
     exit(0);
@@ -325,10 +326,9 @@ tellconfig(&settings);
 
 
     hash = bin_md5(fname, GET_MD5, &ctx);
-tellconfig(&settings);
+/* tellconfig(&settings); */
     edpack(&settings, hash, PACK_DEC);
-printf("\n\n");
-tellconfig(&settings);
+/* tellconfig(&settings); */
 
     if (strcmp(settings.hash, hash)) {
       unlink(fname);
@@ -337,14 +337,19 @@ tellconfig(&settings);
   }
 }
 
-void write_settings(const char *fname)
+void write_settings(const char *fname, int die)
 {
   MD5_CTX ctx;
+  char *hash = NULL;
 
   MD5_Init(&ctx);
-  if (bin_md5(fname, WRITE_MD5, &ctx))
+  if ((hash = bin_md5(fname, WRITE_MD5, &ctx))) {
     printf("* Wrote settings to %s.\n", fname);
-  exit(0);
+    edpack(&settings, hash, PACK_DEC);
+  }
+
+  if (die)
+    exit(0);
 }
 
 void conf_to_bin(conf_t *in)
@@ -374,6 +379,6 @@ void conf_to_bin(conf_t *in)
                            bot->ip6 ? bot->ip6 : "");
     }
 
-  tellconfig(&settings);
-  write_settings(binname);
+  /* tellconfig(&settings); */
+  write_settings(binname, 1);
 }

+ 1 - 1
src/binary.h

@@ -9,6 +9,6 @@ extern int checked_bin_buf;
 #  define GET_MD5		2
 
 void check_sum(const char *, const char *);
-void write_settings(const char *);
+void write_settings(const char *, int);
 void conf_to_bin(conf_t *);
 #endif /* !_BINARY_H */

+ 5 - 3
src/cmds.c

@@ -933,12 +933,14 @@ static void cmd_update(int idx, char *par)
 {
   putlog(LOG_CMDS, "*", "#%s# update %s", dcc[idx].nick, par);
 #ifdef LEAF
-  if (!localhub)
-    dprintf(idx, "Please use this command on the first listed in the conf for this user@box\n");
+  if (!localhub) {
+    dprintf(idx, "Please use '%s%s%s' for this login/shell.\n", RED(idx), conf.localhub, COLOR_END(idx));
+    return;
+  }
 #endif /* LEAF */
   if (!par[0])
     dprintf(idx, "Usage: update <binname>\n");
-  updatebin(idx, par, 0);
+  updatebin(idx, par, 20);
 }
 
 static void cmd_uptime(int idx, char *par)

+ 6 - 3
src/conf.c

@@ -770,7 +770,9 @@ fillconf(conf_t * inconf)
   inconf->uid = conffile.uid;
 }
 
-void tellconf(conf_t *inconf)
+/*
+static void 
+tellconf(conf_t *inconf)
 {
 conf_bot *bot;
 int i = 0;
@@ -797,10 +799,11 @@ printf("watcher: %d\n", inconf->watcher);
     }
 printf("\n\n\n\n");
 }
+*/
 
 void bin_to_conf(settings_t *in)
 {
-printf("Converting binary data to conf struct\n");
+/* printf("Converting binary data to conf struct\n"); */
   conffile.uid = atol(settings.uid);
   conffile.username = strdup(settings.username);
   conffile.uname = strdup(settings.uname);
@@ -841,5 +844,5 @@ printf("Converting binary data to conf struct\n");
     }
     free(tmpp);
   }
-  tellconf(&conffile);
+  /* tellconf(&conffile); */
 }

+ 0 - 1
src/conf.h

@@ -66,7 +66,6 @@ int parseconf();
 int writeconf(char *, FILE *, int);
 void fillconf(conf_t *);
 void bin_to_conf();
-void tellconf(conf_t *);
 
 extern char		cfile[DIRMAX];
 #endif /* !_CONF_H */

+ 26 - 6
src/main.c

@@ -9,9 +9,9 @@
 #include "common.h"
 #include "main.h"
 #include "color.h"
-#include "binary.h"
 #include "dcc.h"
 #include "misc.h"
+#include "binary.h"
 #include "response.h"
 #include "thread.h"
 #include "settings.h"
@@ -92,6 +92,7 @@ static int	do_confedit = 0;		/* show conf menu if -C */
 #ifdef LEAF
 static char    do_killbot[21] = "";
 #endif /* LEAF */
+static char *update_bin = NULL;
 static int 	checktrace = 1;		/* Check for trace when starting up? */
 
 
@@ -150,7 +151,7 @@ void fatal(const char *s, int recoverable)
   nuke_server((char *) s);
 #endif /* LEAF */
 
-  if (s[0])
+  if (s && s[0])
     putlog(LOG_MISC, "*", "!*! %s", s);
 
 /*  flushlogs(); */
@@ -158,6 +159,10 @@ void fatal(const char *s, int recoverable)
     ssl_cleanup();
 #endif /* HAVE_SSL */
 
+#ifdef HUB
+  listen_all(my_port, 1); /* close the listening port... */
+#endif /* HUB */
+
   for (i = 0; i < dcc_total; i++)
     if (dcc[i].sock >= 0)
       killsock(dcc[i].sock);
@@ -267,11 +272,11 @@ static void show_help()
 
 
 #ifdef LEAF
-# define PARSE_FLAGS "02B:Cd:De:Eg:G:k:L:P:hnstv"
+# define PARSE_FLAGS "02B:Cd:De:Eg:G:k:L:P:hnstu:v"
 #else /* !LEAF */
-# define PARSE_FLAGS "02Cd:De:Eg:G:hnstv"
+# define PARSE_FLAGS "02Cd:De:Eg:G:hnstu:v"
 #endif /* HUB */
-#define FLAGS_CHECKPASS "CdDeEgGhkntv"
+#define FLAGS_CHECKPASS "CdDeEgGhkntuv"
 static void dtx_arg(int argc, char *argv[])
 {
   int i = 0;
@@ -341,6 +346,13 @@ static void dtx_arg(int argc, char *argv[])
         if (argv[optind])
           p = argv[optind];
         got_ed("d", optarg, p);
+      case 'u':
+        if (optarg) {
+          update_bin = strdup(optarg);
+          updating++;
+          break;
+        } else
+          exit(0);
       case 'v':
       {
         char date[50] = "";
@@ -678,8 +690,14 @@ static void startup_checks() {
         printf("Error killing '%s'\n", do_killbot);
       exit(0);
     } else {
+      if (update_bin)	{			/* invoked with -u bin */
+        kill(conf.bot->pid, SIGKILL);
+        unlink(conf.bot->pid_file);
+        writepid(conf.bot->pid_file, getpid());
+        updatebin(DP_STDOUT, update_bin, 1);	/* will call restart all bots */
+      }
       spawnbots();
-      if (updating) 
+      if (updating)
         exit(0); /* just let the timer restart us (our parent) */
     }
   }
@@ -717,6 +735,8 @@ int tracecheck_breakpoint() {
 
 int main(int argc, char **argv)
 {
+//printf("YAY!\n\n\n\n\n\n\n\n");
+//exit(0);
   egg_timeval_t egg_timeval_now;
 
   Context;

+ 44 - 68
src/misc.c

@@ -10,7 +10,9 @@
 #include "common.h"
 #include "misc.h"
 #include "settings.h"
+#include "binary.h"
 #include "rfc1459.h"
+#include "botnet.h"
 #include "misc_file.h"
 #include "egg_timer.h"
 #include "dcc.h"
@@ -646,34 +648,40 @@ void kill_bot(char *s1, char *s2)
 
 /* Update system code
  */
-#ifdef LEAF
-static void updatelocal() __attribute__((noreturn));
 
-static void updatelocal(void)
+void 
+restart(int idx)
 {
-  /* let's drop the server connection ASAP */
-  nuke_server("Updating...");
-
-  botnet_send_chat(-1, conf.bot->nick, "Updating...");
-  botnet_send_bye();
-
-  fatal("Updating...", 1);
+#ifdef HUB
+  write_userfile(idx);
+#endif /* HUB */
+#ifdef LEAF
+  nuke_server("Updating...");		/* let's drop the server connection ASAP */
+#endif /* LEAF */
+  if (tands > 0) {
+    botnet_send_chat(-1, conf.bot->nick, "Updating...");
+    botnet_send_bye();
+  }
+  fatal(idx <= 0x7FF0 ? "Updating..." : NULL, 1);
   usleep(2000 * 500);
   unlink(conf.bot->pid_file); /* if this fails it is ok, cron will restart the bot, *hopefully* */
   system(binname); /* start new bot. */
   exit(0);
 }
-#endif /* LEAF */
 
-int updatebin(int idx, char *par, int autoi)
+static void restart_data(int *idx)
+{
+  restart(*idx);
+}
+
+int updatebin(int idx, char *par, int secs)
 {
   char *path = NULL, *newbin = NULL, buf[DIRMAX] = "", old[DIRMAX] = "", testbuf[DIRMAX] = "";
-  struct stat sb;
   int i;
 
-  path = newsplit(&par);
-  par = path;
-  if (!par[0]) {
+  path = par;
+
+  if (!par || !par[0]) {
     logidx(idx, "Not enough parameters.");
     return 1;
   }
@@ -706,7 +714,7 @@ int updatebin(int idx, char *par, int autoi)
     logidx(idx, "Can't update with the current binary");
     return 1;
   }
-  if (stat(path, &sb)) {
+  if (!can_stat(path)) {
     logidx(idx, "%s can't be accessed", path);
     free(path);
     return 1;
@@ -722,6 +730,8 @@ int updatebin(int idx, char *par, int autoi)
   egg_snprintf(old, sizeof old, "%s.bin.old", tempdir);
   copyfile(binname, old);
 
+  write_settings(path, 0);	/* re-write the binary with our data */
+
   /* The binary should return '2' when ran with -2, if not it's probably corrupt. */
   egg_snprintf(testbuf, sizeof testbuf, "%s -2", path);
 #ifndef CYGWIN_HACKS
@@ -751,44 +761,29 @@ int updatebin(int idx, char *par, int autoi)
     return 1;
   }
 
-  egg_snprintf(buf, sizeof buf, "%s", binname);
-
   /* safe to run new binary.. */
 
-#ifdef HUB
-  listen_all(my_port, 1); /* close the listening port... */
-  usleep(5000);
-#endif /* HUB */
 #ifdef LEAF
-  if (!autoi && localhub) {
-    /* let's drop the server connection ASAP */
-    nuke_server("Updating...");
-#endif /* LEAF */
-    putlog(LOG_DEBUG, "*", "Running for update: %s", buf);
-    logidx(idx, "Updating...bye");
-    putlog(LOG_MISC, "*", "Updating...");
-    botnet_send_chat(-1, conf.bot->nick, "Updating...");
-    botnet_send_bye();
-    fatal("Updating...", 1);
-    usleep(2000 * 500);
-    unlink(conf.bot->pid_file); /* delete pid so new binary doesnt exit. */
-    system(buf);		/* run the binary, it SHOULD work from earlier tests.. */
-    exit(0);
-#ifdef LEAF
-  } else if (localhub && autoi) {
-    egg_timeval_t howlong;
-
-    egg_snprintf(buf, sizeof buf, "%s -L %s -P %d", binname, conf.bot->nick, getpid());	
-    putlog(LOG_DEBUG, "*", "Running for update: %s", buf);
+  if (secs > 0) {
     /* will exit after run, cron will restart us later */
-    system(buf);
-
-    howlong.sec = 300;
-    howlong.usec = 0;
-    timer_create(&howlong, "updatelocal()", (Function) updatelocal);
+    egg_snprintf(buf, sizeof buf, "%s -L %s -P %d", binname, conf.bot->nick, getpid());
+    putlog(LOG_DEBUG, "*", "Running for update: %s", buf);
+    system(buf);	/* restarts other bots running, removes pid files */
+    
+    /* this odd statement makes it so specifying 1 sec will restart other bots running
+     * and then just restart with no delay */
+    if (secs > 1) {
+      egg_timeval_t howlong;
+      howlong.sec = secs;
+      howlong.usec = 0;
+      timer_create_complex(&howlong, "restarting for update", (Function) restart_data, (void *) &idx, 0);
+    } else
+      restart(idx);
     return 0;
-  }
+  } else
 #endif /* LEAF */
+    restart(idx);	/* no timer */
+
  /* this should never be reached */
   return 2;
 }
@@ -1082,25 +1077,6 @@ char *color(int idx, int type, int which)
   return "";
 }
 
-void
-restart(int idx)
-{
-#ifdef HUB
-  write_userfile(idx);
-#endif /* HUB */
-#ifdef LEAF
-  nuke_server("Restarting...");
-#endif /* LEAF */
-  botnet_send_chat(-1, conf.bot->nick, "Restarting...");
-  botnet_send_bye();
-
-  fatal("Restarting...", 1);
-  sleep(1);
-  unlink(conf.bot->pid_file); /* if this fails it is ok, cron will restart the bot, *hopefully* */
-  system(binname); /* start new bot. */
-  exit(0);
-}
-
 char *
 strtolower(char *s)
 {

+ 1 - 1
src/mod/update.mod/update.c

@@ -273,7 +273,7 @@ void finish_update(int idx)
 
   putlog(LOG_MISC, "*", "Updating with binary: %s", buf2);
   
-  if (updatebin(0, buf2, 1))
+  if (updatebin(0, buf2, 120))
     putlog(LOG_MISC, "*", "Failed to update to new binary..");
 #ifdef LEAF
   else

+ 4 - 2
src/net.c

@@ -1391,9 +1391,11 @@ int sockgets(char *s, int *len)
     p = strchr(xx, '\r');
   if (p != NULL) {
     *p = 0;
+/* FIXME: overlapping here */
+
     strcpy(s, xx);
-/*  overlap  strcpy(xx, p + 1); */
-    sprintf(xx, "%s", p + 1);
+    strcpy(xx, p + 1);
+
 /*    if (s[0] && strlen(s) && (s[strlen(s) - 1] == '\r')) */
     if (s[strlen(s) - 1] == '\r')
       s[strlen(s) - 1] = 0;

+ 0 - 2
src/settings.h

@@ -36,6 +36,4 @@ typedef struct settings_struct {
 
 extern settings_t       settings;
 
-void tellconfig(settings_t *);
-
 #endif /* !_SETTINGS_H */

+ 0 - 9
src/shell.c

@@ -984,12 +984,3 @@ void crazy_trace()
 #endif /* CRAZY_TRACE */
 
 
-/* need to account for this after reading in conf/spawning bots. 
-ifdef LEAF
-      if (updating)
-        exit(0);  let cron restart us. 
-    }  localhub 
-endif LEAF 
-*/
-
-