Переглянути джерело

Add validations when reading pack config

Bryan Drewery 14 роки тому
батько
коміт
21081a4da2
2 змінених файлів з 72 додано та 28 видалено
  1. 2 0
      doc/UPDATES
  2. 70 28
      src/binary.c

+ 2 - 0
doc/UPDATES

@@ -1,3 +1,5 @@
+* Reading in pack.cfg will now do some sanity checks
+
 1.4.0 - http://wraith.botpack.net/milestone/1.4.0
   * Updated server list, 'set -yes servers -' and 'set -yes servers6 -' to get new list.
   * Change +bitch reaction to use normal queue for deopping opper/opped clients

+ 70 - 28
src/binary.c

@@ -343,17 +343,25 @@ readcfg(const char *cfgfile, bool read_stdin)
   char *buffer = NULL, *p = NULL;
   int skip = 0, line = 0, feature = 0;
   bool error = 0;
-
-#define ADD_ERROR \
-  error_line = (line_list_t *) my_calloc(1, sizeof(line_list_t)); \
-  error_line->line = line; \
-  error_line->next = NULL; \
-  list_append((struct list_type **) &(error_list), (struct list_type *) error_line); \
-  error = 1;
+  bd::Array<bd::String> words;
+  bd::String line_str;
+
+#define ADD_ERROR(str) \
+  if (line != -1) { \
+    fprintf(stderr, STR("\n[Line %2d]: " str "\n"), line); \
+    error_line = (line_list_t *) my_calloc(1, sizeof(line_list_t)); \
+    error_line->line = line; \
+    error_line->next = NULL; \
+    list_append((struct list_type **) &(error_list), (struct list_type *) error_line); \
+  } else \
+    fprintf(stderr, STR("\n" str "\n")); \
+  error = 1
 
   settings.salt1[0] = settings.salt2[0] = 0;
-  if (!read_stdin)
+  if (!read_stdin) {
     printf(STR("Reading '%s' "), cfgfile);
+    fflush(stdout);
+  }
   while ((!feof(f)) && ((buffer = step_thru_file(f)) != NULL)) {
     if (tty_changed) {
       printf(".");
@@ -366,33 +374,52 @@ readcfg(const char *cfgfile, bool read_stdin)
       if ((skipline(buffer, &skip)))
         continue;
       if ((strchr(buffer, '<') || strchr(buffer, '>')) && !strstr(buffer, "SALT")) {
-        ADD_ERROR
+        ADD_ERROR("Invalid <>");
       }
       p = strchr(buffer, ' ');
       while (p && (strchr(LISTSEPERATORS, p[0])))
         *p++ = 0;
       if (p) {
-        size_t p_len = strlen(trim(p));
+        line_str = bd::String(trim(p));
+        words = line_str.split(" ");
+
         if (!strcasecmp(buffer, STR("packname"))) {
-          strlcpy(settings.packname, trim(p), sizeof settings.packname);
+          if (words.length() == 0) {
+            ADD_ERROR("PACKNAME requires argument");
+          }
+          strlcpy(settings.packname, line_str.c_str(), sizeof settings.packname);
         } else if (!strcasecmp(buffer, STR("shellhash")) || !strcasecmp(buffer, STR("binarypass"))) {
-          if (p_len != 40 && p_len != 47) {
-            fprintf(stderr, STR("\nBINARYPASS should be a SHA1 hash or salted-SHA1 hash.\n"));
-            ADD_ERROR
+          if (line_str.length() != 40 && line_str.length() != 47) {
+            ADD_ERROR("BINARYPASS should be a SHA1 hash or salted-SHA1 hash.");
           }
-          strlcpy(settings.shellhash, trim(p), sizeof settings.shellhash);
+          strlcpy(settings.shellhash, line_str.c_str(), sizeof settings.shellhash);
         } else if (!strcasecmp(buffer, STR("dccprefix"))) {
-          strlcpy(settings.dcc_prefix, trim(p), 2);
+          if (words.length() == 0) {
+            ADD_ERROR("DCCPREFIX requires argument");
+          }
+          strlcpy(settings.dcc_prefix, line_str.c_str(), 2);
         } else if (!strcasecmp(buffer, STR("owner"))) {
-          strlcat(settings.owners, trim(p), sizeof(settings.owners));
+          if (words.length() < 2) {
+            ADD_ERROR("OWNER requires 2 arguments: nick password");
+          }
+          strlcat(settings.owners, line_str.c_str(), sizeof(settings.owners));
           strlcat(settings.owners, ",", sizeof(settings.owners));
         } else if (!strcasecmp(buffer, STR("hub"))) {
-          strlcat(settings.hubs, trim(p), sizeof(settings.hubs));
+          if (words.length() < 3) {
+            ADD_ERROR("HUB requires 3 arguments: nick host port");
+          }
+          strlcat(settings.hubs, line_str.c_str(), sizeof(settings.hubs));
           strlcat(settings.hubs, ",", sizeof(settings.hubs));
         } else if (!strcasecmp(buffer, STR("salt1"))) {
-          strlcat(settings.salt1, trim(p), sizeof(settings.salt1));
+          if (words.length() == 0) {
+            ADD_ERROR("SALT1 requires argument");
+          }
+          strlcat(settings.salt1, line_str.c_str(), sizeof(settings.salt1));
         } else if (!strcasecmp(buffer, STR("salt2"))) {
-          strlcat(settings.salt2, trim(p), sizeof(settings.salt2));
+          if (words.length() == 0) {
+            ADD_ERROR("SALT2 requires argument");
+          }
+          strlcat(settings.salt2, line_str.c_str(), sizeof(settings.salt2));
           if (read_stdin) break;
         }
       } else { /* SINGLE DIRECTIVES */
@@ -413,21 +440,36 @@ readcfg(const char *cfgfile, bool read_stdin)
     tcsetattr (fileno (stdin), TCSAFLUSH | TCSASOFT, &s);
   }
 
+  line = -1;
+  /* Was the entire pack read in? */
+  if (!settings.packname[0]) {
+    ADD_ERROR("Missing PACKNAME");
+  }
+  if (!settings.dcc_prefix[0]) {
+    ADD_ERROR("Missing DCCPREFIX");
+  }
+  if (!settings.shellhash[0]) {
+    ADD_ERROR("Missing BINARYPASS");
+  }
+  if (!settings.owners[0]) {
+    ADD_ERROR("Missing OWNER");
+  }
+  if (!settings.hubs[0]) {
+    ADD_ERROR("Missing HUBS");
+  }
+  if (!settings.salt1[0] || !settings.salt2[0]) {
+    ADD_ERROR("Missing SALTS");
+  }
+
+
   if (error) {
     printf("\n");
-    fprintf(stderr, STR("Error: Look at your configuration again and remove any <>\n"));
+    fprintf(stderr, STR("Error: Look at your configuration again and fix any errors.\n"));
     for (error_line = error_list; error_line; error_line = error_line->next)
         fprintf(stderr, STR("Line %d\n"), error_line->line);
     exit(1);
   }
 
-  /* Was the entire pack read in? */
-  if (!settings.salt1[0] || !settings.salt2[0]) {
-    printf("\n");
-    fprintf(stderr, STR("Missing SALTS\n"));
-    exit(1);
-  }
-
   if (!read_stdin) printf(STR(" Success\n"));
   else printf("\n");
   return 1;