Jelajahi Sumber

* Recoded getopt function to work as intended.
* In updatebin() check if the new binary will work first.


svn: 425

Bryan Drewery 22 tahun lalu
induk
melakukan
0fc1c12397
2 mengubah file dengan 44 tambahan dan 61 penghapusan
  1. 30 56
      src/main.c
  2. 14 5
      src/misc.c

+ 30 - 56
src/main.c

@@ -592,52 +592,37 @@ void show_help()
 }
 
 
-#ifdef LEAF
-#define PARSE_FLAGS "cedghnstvDEGLP"
-#endif /* LEAF */
-#ifdef HUB
-#define PARSE_FLAGS "edghnstvDEG"
-#endif /* HUB */
-#define FLAGS_CHECKPASS "edhgntDEGv"
 char *confdir();
 static void dtx_arg(int argc, char *argv[])
 {
+#ifdef LEAF
+#define PARSE_FLAGS "2c:d:De:E::g:G:L:P:hnstv"
+#else /* !LEAF */
+#define PARSE_FLAGS "2d:De:E::g:G:hnstv"
+#endif /* HUB */
+#define FLAGS_CHECKPASS "dDeEgGhntv"
   int i, localhub_pid = 0;
   char *p = NULL, *p2 = NULL;
   while ((i = getopt(argc, argv, PARSE_FLAGS)) != EOF) {
     if (strchr(FLAGS_CHECKPASS, i))
       checkpass();
     switch (i) {
+      case '2':
+        exit(2);
 #ifdef LEAF
       case 'c':
         localhub = 0;
-        p = argv[optind];
         if (!localhub)
-          gotspawn(p);
+          gotspawn(optarg);
         break;
 #endif /* LEAF */
       case 'h':
         show_help();
         break; /* never reached */
       case 'g':
-        p = argv[optind];
-        if (p)
-          gen_conf(p, 1);
-        else
-          fatal(STR("You must specify a filename after -g"), 0);
-        break; /* never reached */
+        gen_conf(optarg, 1);
       case 'G':
-        p = argv[optind];
-        if (p)
-          gen_conf(p, 2);
-        else
-          fatal(STR("You must specify a filename after -G"), 0);
-        break; /* never reached */
-      case 'k':
-        p = argv[optind];
-/*        kill_bot(p); */
-        exit(0);
-        break; /* never reached */
+        gen_conf(optarg, 2);
       case 'n':
 	backgrd = 0;
 	break;
@@ -652,35 +637,28 @@ static void dtx_arg(int argc, char *argv[])
         sdprintf(STR("debug enabled"));
         break;
       case 'E':
-        p = argv[optind];
-        if (p) {
-          if (!strcmp(p, "all")) {
+        if (optarg && optarg[0]) {
+          if (!strcmp(optarg, "all")) {
             int n;
             putlog(LOG_MISC, "*", STR("Listing all errors"));
             for (n = 1; n < ERR_MAX; n++)
-              putlog(LOG_MISC, "*", STR("Error #%d: %s"), n, werr_tostr(n));
+            putlog(LOG_MISC, "*", STR("Error #%d: %s"), n, werr_tostr(n));
           } else {
-            putlog(LOG_MISC, "*", STR("Error #%d: %s"), atoi(p), werr_tostr(atoi(p)));
+            putlog(LOG_MISC, "*", STR("Error #%d: %s"), atoi(optarg), werr_tostr(atoi(optarg)));
           }
           exit(0);
         } else {
           fatal(STR("You must specify error number after -E (or 'all')"), 0);
         }
-         break;
+        break;
       case 'e':
         if (argv[optind])
           p = argv[optind];
-        if (argv[optind+1])
-          p2 = argv[optind+1];
-        got_ed("e", p, p2);
-        break; /* this should never be reached */
+        got_ed("e", optarg, p);
       case 'd':
         if (argv[optind])
           p = argv[optind];
-        if (argv[optind+1])
-          p2 = argv[optind+1];
-        got_ed("d", p, p2);
-        break; /* this should never be reached */
+        got_ed("d", optarg, p);
       case 'v':
       {
         char date[50];
@@ -694,24 +672,21 @@ static void dtx_arg(int argc, char *argv[])
 #ifdef LEAF
       case 'L':
       {
-        p = argv[optind];
-        if (p && p[0]) {
-          FILE *fp;
-          char buf2[DIRMAX], s[11];
-
-          egg_snprintf(buf2, sizeof buf2, "%s/.../.pid.%s", confdir(), p);
-          if ((fp = fopen(buf2, "r"))) {
-            fgets(s, 10, fp);
-            localhub_pid = atoi(s);
-            fclose(fp);
-            /* printf("LOCALHUB PID: %d\n", localhub_pid); */
-          }
+        FILE *fp;
+        char buf2[DIRMAX], s[11];
+
+        egg_snprintf(buf2, sizeof buf2, "%s/.../.pid.%s", confdir(), optarg);
+        if ((fp = fopen(buf2, "r"))) {
+          fgets(s, 10, fp);
+          localhub_pid = atoi(s);
+          fclose(fp);
+          /* printf("LOCALHUB PID: %d\n", localhub_pid); */
         }
         break;
       }
-      case 'P':	
-        if (atoi(argv[optind]) != localhub_pid)
-          exit(1);
+      case 'P':
+        if (atoi(optarg) && (atoi(optarg) != localhub_pid))
+          exit(2);
         else
           sdprintf(STR("Updating..."));
         localhub = 1;
@@ -719,7 +694,6 @@ static void dtx_arg(int argc, char *argv[])
         break;
 #endif
       default:
-//        exit(1);
         break;
     }
   }

+ 14 - 5
src/misc.c

@@ -1326,14 +1326,14 @@ int updatebin (int idx, char *par, int autoi)
 {
   char *path = NULL,
    *newbin;
-  char buf[2048], old[1024];
+  char buf[DIRMAX], old[DIRMAX], testbuf[DIRMAX];
   struct stat sb;
   int i;
 #ifdef LEAF
   module_entry *me;
 #endif /* LEAF */
 
-  buf[0] = 0;
+  buf[0] = testbuf[0] = old[0] = 0;
   path = newsplit(&par);
   par = path;
   if (!par[0]) {
@@ -1383,6 +1383,16 @@ int updatebin (int idx, char *par, int autoi)
   egg_snprintf(old, sizeof old, "%s.bin.old", tempdir);
   copyfile(binname, old);
 
+  /* The binary should return '2' when ran with -2, if not it's probably corrupt. */
+  snprintf(testbuf, sizeof testbuf, "%s -2", path);
+  i = system(testbuf);
+  if (i == -1 || WEXITSTATUS(i) != 2) {
+    if (idx)
+      dprintf(idx, STR("Couldn't restart new binary (error %d)\n"), i);
+    putlog(LOG_MISC, "*", STR("Couldn't restart new binary (error %d)\n"), i);
+    return i;
+  }
+
   if (movefile(path, binname)) {
     if (idx)
       dprintf(idx, STR("Can't rename %s to %s\n"), path, binname);
@@ -1391,12 +1401,10 @@ int updatebin (int idx, char *par, int autoi)
   }
 
   sprintf(buf, "%s", binname);
-  
 #ifdef LEAF
   if (localhub) {
     /* if localhub = 1, this is the spawn bot and controls
      * the spawning of new bots. */
-
      sprintf(buf, "%s -L %s -P %d", buf, botnetnick, getpid());
   } 
 #endif /* LEAF */
@@ -1411,7 +1419,8 @@ int updatebin (int idx, char *par, int autoi)
   usleep(5000);
 #endif /* HUB */
   putlog(LOG_DEBUG, "*", "Running for update: %s", buf);
-  if ((i = system(buf))) {
+  i = system(buf);
+  if (i == -1 || WEXITSTATUS(i)) {
     if (idx)
       dprintf(idx, STR("Couldn't restart new binary (error %d)\n"), i);
     putlog(LOG_MISC, "*", STR("Couldn't restart new binary (error %d)\n"), i);