瀏覽代碼

* Ported [2399] to 1.2.9 (fixes #158)

svn: 2573
Bryan Drewery 20 年之前
父節點
當前提交
80f9f93951
共有 8 個文件被更改,包括 64 次插入26 次删除
  1. 1 0
      doc/UPDATES
  2. 2 2
      src/binary.c
  3. 6 6
      src/common.h
  4. 3 3
      src/conf.c
  5. 2 0
      src/eggdrop.h
  6. 6 5
      src/misc.c
  7. 43 10
      src/shell.c
  8. 1 0
      src/shell.h

+ 1 - 0
doc/UPDATES

@@ -13,6 +13,7 @@ Lines prefixed with '-' were disabled before release and are not finished, or ar
 
 1.2.9
 * Fixed cmd_[un]stick not properly using numbers for channel masks. (#160)
+* Fixed binary spawning/updating to escape needed chars for shell. (#158)
 
 1.2.8
 * Fixed [bot]* cmds depending on case of botnicks.

+ 2 - 2
src/binary.c

@@ -416,10 +416,10 @@ check_sum(const char *fname, const char *cfgfile)
 static bool check_bin_initialized(const char *fname)
 {
   int i = 0;
-  size_t len = strlen(fname) + 3 + 1;
+  size_t len = strlen(shell_escape(fname)) + 3 + 1;
   char *path = (char *) my_calloc(1, len);
 
-  simple_snprintf(path, len, "%s -q", fname);
+  simple_snprintf(path, len, "%s -q", shell_escape(fname));
 
   i = system(path);
   free(path);

+ 6 - 6
src/common.h

@@ -42,14 +42,14 @@
 #ifdef WIN32
 # undef exit
 # define exit(x) ExitProcess(x)
-
-/*
-# undef system
-  int my_system(const char *);		
-# define system(_run) 	my_system(_run)
-*/
 #endif /* WIN32 */
 
+
+//# undef system
+int system(const char *);		
+//# define system(_run) 	my_system(_run)
+
+
 #define BIT0    (uint32_t) 0x000000001
 #define BIT1    (uint32_t) 0x000000002
 #define BIT2    (uint32_t) 0x000000004

+ 3 - 3
src/conf.c

@@ -82,11 +82,11 @@ tellconf()
 
 void spawnbot(const char *nick)
 {
-  size_t size = strlen(nick) + strlen(binname) + 20;
+  size_t size = strlen(shell_escape(nick)) + strlen(shell_escape(binname)) + 20;
   char *run = (char *) my_calloc(1, size);
   int status = 0;
 
-  simple_snprintf(run, size, "%s -B %s", binname, replace(nick, "`", "\\`"));
+  simple_snprintf(run, size, "%s -B %s", shell_escape(binname), shell_escape(nick));
   sdprintf("Spawning '%s': %s", nick, run);
   status = system(run);
   if (status == -1 || WEXITSTATUS(status))
@@ -863,7 +863,7 @@ writeconf(char *filename, FILE * stream, int bits)
 
   comment("");
 
-  comment("# Automatically add the bot to crontab?");
+  comment("# Automatically add the bot to crontab? (Disable if binname has funky chars that need escaping)");
   my_write(f, "! autocron %d\n", conf.autocron);
 
   comment("");

+ 2 - 0
src/eggdrop.h

@@ -60,6 +60,8 @@
 #define RANDSPECIAL     "#*+,-./;<=>?[]^_{}|"
 #define RANDSPECIALLEN  19
 
+#define ESCAPESHELL	"`\\|#*()[]& "
+
 #if (NICKMAX < 9) || (NICKMAX > 32)
 #  include "invalid NICKMAX value"
 #endif

+ 6 - 5
src/misc.c

@@ -18,6 +18,7 @@
 #include "egg_timer.h"
 #include "dcc.h"
 #include "users.h"
+#include "shell.h"
 #include "main.h"
 #include "debug.h"
 #include "dccutil.h"
@@ -687,9 +688,9 @@ restart(int idx)
 
   simple_sprintf(shit, "-%s%s%sB", !backgrd ? "n" : "", term_z ? "t" : "", sdebug ? "D" : "");
 
-  argv[0] = strdup(binname);
+  argv[0] = strdup(shell_escape(binname));
   argv[1] = strdup(shit);
-  argv[2] = strdup(replace(conf.bot->nick, "`", "\\`"));
+  argv[2] = strdup(shell_escape(conf.bot->nick));
   argv[3] = NULL;
 
   unlink(conf.bot->pid_file);
@@ -719,7 +720,7 @@ hard_restart(int idx)
   }
   fatal(idx <= 0x7FF0 ? reason : NULL, 1);
   usleep(2000 * 500);
-  simple_sprintf(buf, "%s -B %s\n", binname, conf.bot->nick);
+  simple_sprintf(buf, "%s -B %s\n", shell_escape(binname), shell_escape(conf.bot->nick));
   unlink(conf.bot->pid_file); /* if this fails it is ok, cron will restart the bot, *hopefully* */
   system(buf); /* start new bot. */
   exit(0);
@@ -793,7 +794,7 @@ int updatebin(int idx, char *par, int secs)
 
   /* The binary should return '2' when ran with -2, if not it's probably corrupt. */
 #ifndef CYGWIN_HACKS
-  simple_snprintf(buf, sizeof(buf), STR("%s -2"), path);
+  simple_snprintf(buf, sizeof(buf), STR("%s -2"), shell_escape(path));
   putlog(LOG_DEBUG, "*", "Running for update binary test: %s", buf);
   i = system(buf);
   if (i == -1 || WEXITSTATUS(i) != 2) {
@@ -806,7 +807,7 @@ int updatebin(int idx, char *par, int secs)
 
   /* now to send our config to the new binary */
 #ifndef CYGWIN_HACKS
-  simple_snprintf(buf, sizeof(buf), STR("%s -4 %s"), path, conffile->file);
+  simple_snprintf(buf, sizeof(buf), STR("%s -4 %s"), shell_escape(path), conffile->file);
   putlog(LOG_DEBUG, "*", "Running for update conf: %s", buf);
   i = system(buf);
   delete conffile;

+ 43 - 10
src/shell.c

@@ -49,11 +49,11 @@
 
 bool clear_tmpdir = 0;
 
-#ifdef WIN32
-int
-my_system(const char *run)
+int my_system(const char *run)
 {
   int ret = -1;
+#ifdef WIN32
+
   PROCESS_INFORMATION pinfo;
   STARTUPINFO sinfo;
 
@@ -74,8 +74,10 @@ my_system(const char *run)
     return -1;
   else
     return 0;
-}
+#else
+  return system(run);
 #endif /* WIN32 */
+}
 
 void clear_tmp()
 {
@@ -216,7 +218,7 @@ void check_processes()
     return;
 
   /* Get this binary's filename */
-  strlcpy(buf, binname, sizeof(buf));
+  strlcpy(buf, shell_escape(binname), sizeof(buf));
   p = strrchr(buf, '/');
   if (p) {
     p++;
@@ -248,7 +250,8 @@ void check_processes()
       mytime = newsplit(&curp);
       strlcpy(cmd, curp, sizeof(cmd));
       /* skip any <defunct> procs "/bin/sh -c" crontab stuff and binname crontab stuff */
-      if (!strstr(cmd, "<defunct>") && !strncmp(cmd, "/bin/sh -c", 10) && !strncmp(cmd, binname, strlen(binname))) {
+      if (!strstr(cmd, "<defunct>") && !strncmp(cmd, "/bin/sh -c", 10) && 
+          !strncmp(cmd, shell_escape(binname), strlen(shell_escape(binname)))) {
         /* get rid of any args */
         if ((p = strchr(cmd, ' ')))
           *p = 0;
@@ -911,6 +914,8 @@ char *move_bin(const char *ipath, const char *file, bool run)
       sdprintf("Binary successfully moved to: %s", newbin);
       unlink(binname);
       if (run) {
+        simple_snprintf(newbin, sizeof newbin, "%s%s%s", 
+                        path, path[strlen(path) - 1] == '/' ? "" : "/", shell_escape(file));
         system(newbin);
         sdprintf("exiting to let new binary run...");
         exit(0);
@@ -943,12 +948,12 @@ void crontab_del() {
 
   tmpFile = (char *) my_calloc(1, strlen(binname) + 100);
 
-  strcpy(tmpFile, binname);
+  strcpy(tmpFile, shell_escape(binname));
   if (!(p = strrchr(tmpFile, '/')))
     return;
   p++;
   strcpy(p, ".ctb");
-  simple_sprintf(buf, "crontab -l | grep -v \"%s\" | grep -v \"^#\" | grep -v \"^\\$\" > %s", binname, tmpFile);
+  simple_sprintf(buf, "crontab -l | grep -v '%s' | grep -v \"^#\" | grep -v \"^\\$\" > %s", binname, tmpFile);
   if (shell_exec(buf, NULL, NULL, NULL)) {
     simple_sprintf(buf, "crontab %s", tmpFile);
     shell_exec(buf, NULL, NULL, NULL);
@@ -959,7 +964,7 @@ void crontab_del() {
 int crontab_exists() {
   char buf[2048] = "", *out = NULL;
 
-  simple_snprintf(buf, sizeof buf, "crontab -l | grep \"%s\" | grep -v \"^#\"", binname);
+  simple_snprintf(buf, sizeof buf, "crontab -l | grep '%s' | grep -v \"^#\"", binname);
   if (shell_exec(buf, NULL, &out, NULL)) {
     if (out && strstr(out, binname)) {
       free(out);
@@ -986,7 +991,8 @@ void crontab_create(int interval) {
 
   char buf[256] = "";
 
-  simple_snprintf(buf, sizeof buf, "crontab -l | grep -v \"%s\" | grep -v \"^#\" | grep -v \"^\\$\"> %s", binname, tmpFile);
+  simple_snprintf(buf, sizeof buf, "crontab -l | grep -v \"%s\" | grep -v \"^#\" | grep -v \"^\\$\"> %s", 
+                  binname, tmpFile);
   if (shell_exec(buf, NULL, NULL, NULL) && (f = fdopen(fd, "a")) != NULL) {
     buf[0] = 0;
     if (interval == 1)
@@ -1080,3 +1086,30 @@ const char *det_translate_num(int num)
   }
   return "ignore";
 }
+
+char *shell_escape(const char *path)
+{
+  static char ret1[DIRMAX] = "", ret2[DIRMAX] = "", *ret = NULL;
+  static bool alt = 0;
+  char *c = NULL;
+
+  if (alt) {
+    alt = 0;
+    ret = ret1;
+  } else {
+    alt = 1;
+    ret = ret2;
+  }
+
+  ret[0] = 0;
+
+  for (c = (char *) path; c && *c; ++c) {
+    if (strchr(ESCAPESHELL, *c))
+      simple_snprintf(ret, sizeof(ret1), "%s\\%c", ret[0] ? ret : "", *c);
+    else
+      simple_snprintf(ret, sizeof(ret1), "%s%c", ret[0] ? ret : "", *c);
+  }
+
+  return ret;
+}
+

+ 1 - 0
src/shell.h

@@ -67,6 +67,7 @@ void werr(int) __attribute__((noreturn));
 char *werr_tostr(int);
 int det_translate(const char *);
 const char *det_translate_num(int);
+char *shell_escape(const char *);
 
 extern bool		clear_tmpdir;