]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Fix #7449 About incorrect JSON output with TlsAllowedCn directive
authorEric Bollengier <eric@baculasystems.com>
Tue, 9 Mar 2021 16:06:03 +0000 (17:06 +0100)
committerEric Bollengier <eric@baculasystems.com>
Fri, 26 Mar 2021 13:57:58 +0000 (14:57 +0100)
bacula/src/console/bbconsjson.c
bacula/src/dird/bdirjson.c
bacula/src/filed/bfdjson.c
bacula/src/lib/Makefile.in
bacula/src/lib/bjson.c
bacula/src/lib/bjson.h
bacula/src/lib/bsnprintf.c
bacula/src/lib/protos.h
bacula/src/stored/bsdjson.c

index 239f758123358b3696eb43d6d83f517af2b7b673..63d31bb0fa885dc9cb24b2bd7f2c7d1ef466d043 100644 (file)
@@ -72,7 +72,6 @@ static void dump_json(display_filter *filter);
 /* Static variables */
 static char *configfile = NULL;
 static FILE *output = stdout;
-static bool teeout = false;               /* output to output and stdout */
 static int numdir;
 static POOLMEM *args;
 static CONFIG *config;
@@ -559,7 +558,6 @@ static void dump_directives()
    }
    printf("\n]\n");
 }
-#endif
 
 /*
  * Send a line to the output file and or the terminal
@@ -575,6 +573,8 @@ void senditf(const char *fmt,...)
    sendit(buf);
 }
 
+static bool teeout = false;               /* output to output and stdout */
+
 void sendit(const char *buf)
 {
 #ifdef CONIO_FIX
@@ -613,3 +613,4 @@ void sendit(const char *buf)
    }
 #endif
 }
+#endif
index 91ffe8ce8e64b4261d50c81da9fdd024d020a793..0b6bf2e681cac24af694d31775adf8e059075687 100644 (file)
@@ -296,7 +296,7 @@ static void display_jobtype(HPKT &hpkt)
    int i;
    for (i=0; jobtypes[i].type_name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == jobtypes[i].job_type) {
-         sendit(NULL, "\n    \"%s\": %s", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": %s", hpkt.ritem->name,
             quote_string(hpkt.edbuf, jobtypes[i].type_name));
          return;
       }
@@ -308,7 +308,7 @@ static void display_label(HPKT &hpkt)
    int i;
    for (i=0; tapelabels[i].name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == tapelabels[i].token) {
-         sendit(NULL, "\n    \"%s\": %s", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": %s", hpkt.ritem->name,
             quote_string(hpkt.edbuf, tapelabels[i].name));
          return;
       }
@@ -320,7 +320,7 @@ static void display_joblevel(HPKT &hpkt)
    int i;
    for (i=0; joblevels[i].level_name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == joblevels[i].level) {
-         sendit(NULL, "\n    \"%s\": %s", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": %s", hpkt.ritem->name,
             quote_string(hpkt.edbuf, joblevels[i].level_name));
          return;
       }
@@ -332,7 +332,7 @@ static void display_replace(HPKT &hpkt)
    int i;
    for (i=0; ReplaceOptions[i].name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == ReplaceOptions[i].token) {
-         sendit(NULL, "\n    \"%s\": %s", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": %s", hpkt.ritem->name,
             quote_string(hpkt.edbuf, ReplaceOptions[i].name));
          return;
       }
@@ -344,7 +344,7 @@ static void display_migtype(HPKT &hpkt)
    int i;
    for (i=0; migtypes[i].type_name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == migtypes[i].job_type) {
-         sendit(NULL, "\n    \"%s\": %s", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": %s", hpkt.ritem->name,
             quote_string(hpkt.edbuf, migtypes[i].type_name));
          return;
       }
@@ -353,17 +353,17 @@ static void display_migtype(HPKT &hpkt)
 
 static void display_actiononpurge(HPKT &hpkt)
 {
-   sendit(NULL, "\n    \"%s\":", hpkt.ritem->name);
+   hpkt.sendit(hpkt, "\n    \"%s\":", hpkt.ritem->name);
    if (*(uint32_t *)(hpkt.ritem->value) | ON_PURGE_TRUNCATE) {
-      sendit(NULL, "\"Truncate\"");
+      hpkt.sendit(hpkt, "\"Truncate\"");
    } else {
-      sendit(NULL, "null");
+      hpkt.sendit(hpkt, "null");
    }
 }
 
 static void display_acl(HPKT &hpkt)
 {
-   sendit(NULL, "\n    \"%s\":", hpkt.ritem->name);
+   hpkt.sendit(hpkt, "\n    \"%s\":", hpkt.ritem->name);
    hpkt.list = ((alist **)hpkt.ritem->value)[hpkt.ritem->code];
    display_alist(hpkt);
 }
@@ -377,11 +377,11 @@ static void display_options(HPKT &hpkt, INCEXE *ie)
    int i, j, k;
    alist *list;
 
-   sendit(NULL, "      \"Options\": [ \n       {\n");
+   hpkt.sendit(hpkt, "      \"Options\": [ \n       {\n");
    for (i=0; i<ie->num_opts; i++) {
       FOPTS *fo = ie->opts_list[i];
       if (!first_opt) {
-         sendit(NULL, ",\n       {\n");
+         hpkt.sendit(hpkt, ",\n       {\n");
       }
       first_dir = true;
       for (j=0; options_items[j].name; j++) {
@@ -399,9 +399,9 @@ static void display_options(HPKT &hpkt, INCEXE *ie)
             }
             if (list->size() > 0) {
                if (!first_dir) {
-                  sendit(NULL, ",\n");
+                  hpkt.sendit(hpkt, ",\n");
                }
-               sendit(NULL, "         \"%s\":", options_items[j].name);
+               hpkt.sendit(hpkt, "         \"%s\":", options_items[j].name);
                hpkt.list = list;
                display_alist(hpkt);
                first_dir = false;
@@ -438,9 +438,9 @@ static void display_options(HPKT &hpkt, INCEXE *ie)
             }
             if (list->size() > 0) {
                if (!first_dir) {
-                  sendit(NULL, ",\n");
+                  hpkt.sendit(hpkt, ",\n");
                }
-               sendit(NULL, "         \"%s\":", options_items[j].name);
+               hpkt.sendit(hpkt, "         \"%s\":", options_items[j].name);
                hpkt.list = list;
                display_alist(hpkt);
                first_dir = false;
@@ -450,9 +450,9 @@ static void display_options(HPKT &hpkt, INCEXE *ie)
             list = &fo->base;
             if (list->size() > 0) {
                if (!first_dir) {
-                  sendit(NULL, ",\n");
+                  hpkt.sendit(hpkt, ",\n");
                }
-               sendit(NULL, "         \"%s\":", options_items[j].name);
+               hpkt.sendit(hpkt, "         \"%s\":", options_items[j].name);
                hpkt.list = list;
                display_alist(hpkt);
                first_dir = false;
@@ -467,9 +467,9 @@ static void display_options(HPKT &hpkt, INCEXE *ie)
                      strip_long_opts(lopts, fo->opts);
                      if (strstr(lopts, FS_options[k].option)) {
                         if (!first_dir) {
-                           sendit(NULL, ",\n");
+                           hpkt.sendit(hpkt, ",\n");
                         }
-                        sendit(NULL, "         \"%s\": %s", options_items[j].name,
+                        hpkt.sendit(hpkt, "         \"%s\": %s", options_items[j].name,
                            quote_string(hpkt.edbuf, FS_options[k].name));
                         found = true;
                         break;
@@ -498,9 +498,9 @@ static void display_options(HPKT &hpkt, INCEXE *ie)
                      *end = 0;       /* terminate this string */
                   }
                   if (!first_dir) {
-                     sendit(NULL, ",\n");
+                     hpkt.sendit(hpkt, ",\n");
                   }
-                  sendit(NULL, "         \"%s\": %s", options_items[j].name,
+                  hpkt.sendit(hpkt, "         \"%s\": %s", options_items[j].name,
                      quote_string(hpkt.edbuf, pos));
                   found = true;
                   if (end) {    /* Still have other options to parse */
@@ -515,9 +515,9 @@ static void display_options(HPKT &hpkt, INCEXE *ie)
          } else if (options_items[j].handler == store_plugin) {
             if (fo->plugin) {
                if (!first_dir) {
-                  sendit(NULL, ",\n");
+                  hpkt.sendit(hpkt, ",\n");
                }
-               sendit(NULL, "         \"%s\": %s", options_items[j].name,
+               hpkt.sendit(hpkt, "         \"%s\": %s", options_items[j].name,
                   quote_string(hpkt.edbuf, fo->plugin));
                first_dir = false;
                first_opt = false;
@@ -526,9 +526,9 @@ static void display_options(HPKT &hpkt, INCEXE *ie)
             list = &fo->fstype;
             if (list->size() > 0) {
                if (!first_dir) {
-                  sendit(NULL, ",\n");
+                  hpkt.sendit(hpkt, ",\n");
                }
-               sendit(NULL, "         \"%s\":", options_items[j].name);
+               hpkt.sendit(hpkt, "         \"%s\":", options_items[j].name);
                hpkt.list = list;
                display_alist(hpkt);
                first_dir = false;
@@ -538,9 +538,9 @@ static void display_options(HPKT &hpkt, INCEXE *ie)
             list = &fo->drivetype;
             if (list->size() > 0) {
                if (!first_dir) {
-                  sendit(NULL, ",\n");
+                  hpkt.sendit(hpkt, ",\n");
                }
-               sendit(NULL, "         \"%s\":", options_items[j].name);
+               hpkt.sendit(hpkt, "         \"%s\":", options_items[j].name);
                hpkt.list = list;
                display_alist(hpkt);
                first_dir = false;
@@ -548,9 +548,9 @@ static void display_options(HPKT &hpkt, INCEXE *ie)
             }
          }
       }
-      sendit(NULL, "\n       }");
+      hpkt.sendit(hpkt, "\n       }");
    }
-   sendit(NULL, "\n      ]");
+   hpkt.sendit(hpkt, "\n      ]");
 }
 
 /*
@@ -573,68 +573,68 @@ static void display_include_exclude(HPKT &hpkt)
 
    if (hpkt.ritem->code == 0) { /* Include */
       INCEXE *ie;
-      sendit(NULL, "\n    \"%s\": [{\n", hpkt.ritem->name);
+      hpkt.sendit(hpkt, "\n    \"%s\": [{\n", hpkt.ritem->name);
       for (j=0; j<fs->num_includes; j++) {
          if (j > 0) {
-            sendit(NULL, ",\n    {\n");
+            hpkt.sendit(hpkt, ",\n    {\n");
          }
          first_dir = true;
          ie = fs->include_items[j];
          for (i=0; newinc_items[i].name; i++) {
             if (strcasecmp(newinc_items[i].name, "File") == 0) {
                if (!first_dir) {
-                  sendit(NULL, ",\n");
+                  hpkt.sendit(hpkt, ",\n");
                }
-               sendit(NULL, "      \"%s\":", newinc_items[i].name);
+               hpkt.sendit(hpkt, "      \"%s\":", newinc_items[i].name);
                first_dir = false;
                hpkt.list = &ie->name_list;
                display_alist(hpkt);
             } if (strcasecmp(newinc_items[i].name, "Plugin") == 0 &&
                   ie->plugin_list.size() > 0) {
                if (!first_dir) {
-                  sendit(NULL, ",\n");
+                  hpkt.sendit(hpkt, ",\n");
                }
-               sendit(NULL, "      \"%s\":", newinc_items[i].name);
+               hpkt.sendit(hpkt, "      \"%s\":", newinc_items[i].name);
                first_dir = false;
                hpkt.list = &ie->plugin_list;
                display_alist(hpkt);
             } if (strcasecmp(newinc_items[i].name, "Options") == 0 &&
                   ie->num_opts > 0) {
                if (!first_dir) {
-                  sendit(NULL, ",\n");
+                  hpkt.sendit(hpkt, ",\n");
                }
                display_options(hpkt, ie);
             } if (strcasecmp(newinc_items[i].name, "ExcludeDirContaining") == 0 &&
                   ie->ignoredir) {
                if (!first_dir) {
-                  sendit(NULL, ",\n");
+                  hpkt.sendit(hpkt, ",\n");
                }
-               sendit(NULL, "      \"%s\": %s ", newinc_items[i].name,
+               hpkt.sendit(hpkt, "      \"%s\": %s ", newinc_items[i].name,
                   quote_string(hpkt.edbuf, ie->ignoredir));
                first_dir = false;
             }
          }
-         sendit(NULL, "\n    }");
+         hpkt.sendit(hpkt, "\n    }");
       }
-      sendit(NULL, "]");
+      hpkt.sendit(hpkt, "]");
    } else {
       /* Exclude */
-      sendit(NULL, "\n    \"%s\": {\n", hpkt.ritem->name);
+      hpkt.sendit(hpkt, "\n    \"%s\": {\n", hpkt.ritem->name);
       first_dir = true;
       for (int i=0; newinc_items[i].name; i++) {
          INCEXE *ie;
          if (strcasecmp(newinc_items[i].name, "File") == 0) {
             if (!first_dir) {
-               sendit(NULL, ",\n");
+               hpkt.sendit(hpkt, ",\n");
             }
-            sendit(NULL, "      \"%s\": ", newinc_items[i].name);
+            hpkt.sendit(hpkt, "      \"%s\": ", newinc_items[i].name);
             first_dir = false;
             ie = fs->exclude_items[0];
             hpkt.list = &ie->name_list;
             display_alist(hpkt);
          }
       }
-      sendit(NULL, "\n    }");
+      hpkt.sendit(hpkt, "\n    }");
    }
 }
 
@@ -649,53 +649,53 @@ static bool display_runscript(HPKT &hpkt)
       return false;
    }
 
-   sendit(NULL, "\n    \"Runscript\": [\n");
+   hpkt.sendit(hpkt, "\n    \"Runscript\": [\n");
 
    foreach_alist(script, *runscripts) {
       if (first) {
-         sendit(NULL, "      {\n");
+         hpkt.sendit(hpkt, "      {\n");
       } else {
-         sendit(NULL, ",\n      {\n");
+         hpkt.sendit(hpkt, ",\n      {\n");
       }
       if (script->when == SCRIPT_Any) {
-         sendit(NULL, "        \"RunsWhen\": \"Always\",\n");
+         hpkt.sendit(hpkt, "        \"RunsWhen\": \"Always\",\n");
 
       } else if (script->when == SCRIPT_After) {
-         sendit(NULL, "        \"RunsWhen\": \"After\",\n");
+         hpkt.sendit(hpkt, "        \"RunsWhen\": \"After\",\n");
 
       } else if (script->when == SCRIPT_Before) {
-         sendit(NULL, "        \"RunsWhen\": \"Before\",\n");
+         hpkt.sendit(hpkt, "        \"RunsWhen\": \"Before\",\n");
 
       } else if (script->when == SCRIPT_AfterVSS) {
-         sendit(NULL, "        \"RunsWhen\": \"AfterVSS\",\n");
+         hpkt.sendit(hpkt, "        \"RunsWhen\": \"AfterVSS\",\n");
       }
 
       if (script->fail_on_error != def->fail_on_error) {
-         sendit(NULL, "        \"FailJobOnError\": %s,\n", script->fail_on_error?"true":"false");
+         hpkt.sendit(hpkt, "        \"FailJobOnError\": %s,\n", script->fail_on_error?"true":"false");
       }
 
       if (script->on_success != def->on_success) {
-         sendit(NULL, "        \"RunsOnSuccess\": %s,\n", script->on_success?"true":"false");
+         hpkt.sendit(hpkt, "        \"RunsOnSuccess\": %s,\n", script->on_success?"true":"false");
       }
 
       if (script->on_failure != def->on_failure) {
-         sendit(NULL, "        \"RunsOnFailure\": %s,\n", script->on_failure?"true":"false");
+         hpkt.sendit(hpkt, "        \"RunsOnFailure\": %s,\n", script->on_failure?"true":"false");
       }
 
       if (script->is_local()) {
-         sendit(NULL, "        \"RunsOnClient\": false,\n");
+         hpkt.sendit(hpkt, "        \"RunsOnClient\": false,\n");
       }
 
       if (script->command) {
-         sendit(NULL, "        \"%s\": %s\n",
+         hpkt.sendit(hpkt, "        \"%s\": %s\n",
                 (script->cmd_type == SHELL_CMD)?"Command":"Console",
                 quote_string(hpkt.edbuf, script->command));
       }
-      sendit(NULL, "      }");
+      hpkt.sendit(hpkt, "      }");
       first = false;
    }
 
-   sendit(NULL, "\n    ]\n");
+   hpkt.sendit(hpkt, "\n    ]\n");
    free_runscript(def);
    return true;
 }
@@ -709,20 +709,20 @@ static void display_run(HPKT &hpkt)
    bool first_run = true;
    RES *res;
 
-   sendit(NULL, "\n    \"%s\": [\n", hpkt.ritem->name);
+   hpkt.sendit(hpkt, "\n    \"%s\": [\n", hpkt.ritem->name);
    for ( ; run; run=run->next) {
-      if (!first_run) sendit(NULL, ",\n");
+      if (!first_run) hpkt.sendit(hpkt, ",\n");
       first_run = false;
       first = true;
-      sendit(NULL, "     {\n");
+      hpkt.sendit(hpkt, "     {\n");
       /* First do override fields */
       for (i=0; RunFields[i].name; i++) {
          switch (RunFields[i].token) {
          case 'f':  /* FullPool */
             if (run->full_pool) {
                res = (RES *)run->full_pool;
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %s", RunFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %s", RunFields[i].name,
                      quote_string(hpkt.edbuf, res->name));
                first = false;
             }
@@ -730,8 +730,8 @@ static void display_run(HPKT &hpkt)
          case 'i':  /* IncrementalPool */
             if (run->inc_pool) {
                res = (RES *)run->inc_pool;
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %s", RunFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %s", RunFields[i].name,
                      quote_string(hpkt.edbuf, res->name));
                first = false;
             }
@@ -739,8 +739,8 @@ static void display_run(HPKT &hpkt)
          case 'd':  /* Differential Pool */
             if (run->diff_pool) {
                res = (RES *)run->diff_pool;
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %s", RunFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %s", RunFields[i].name,
                      quote_string(hpkt.edbuf, res->name));
                first = false;
             }
@@ -748,8 +748,8 @@ static void display_run(HPKT &hpkt)
          case 'N':  /* Next Pool */
             if (run->next_pool) {
                res = (RES *)run->next_pool;
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %s", RunFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %s", RunFields[i].name,
                      quote_string(hpkt.edbuf, res->name));
                first = false;
             }
@@ -759,8 +759,8 @@ static void display_run(HPKT &hpkt)
             //if (run->level_set) {
                for (j=0; joblevels[j].level_name; j++) {
                   if ((int)run->level == joblevels[j].level) {
-                     if (!first) sendit(NULL, ",\n");
-                     sendit(NULL, "      \"%s\": \"%s\"", RunFields[i].name,
+                     if (!first) hpkt.sendit(hpkt, ",\n");
+                     hpkt.sendit(hpkt, "      \"%s\": \"%s\"", RunFields[i].name,
                         joblevels[j].level_name);
                      first = false;
                   }
@@ -770,8 +770,8 @@ static void display_run(HPKT &hpkt)
          case 'P':  /* Pool */
             if (run->pool) {
                res = (RES *)run->pool;
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %s", RunFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %s", RunFields[i].name,
                      quote_string(hpkt.edbuf, res->name));
                first = false;
             }
@@ -779,8 +779,8 @@ static void display_run(HPKT &hpkt)
          case 'S':  /* Storage */
             if (run->storage) {
                res = (RES *)run->storage;
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %s", RunFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %s", RunFields[i].name,
                      quote_string(hpkt.edbuf, res->name));
                first = false;
             }
@@ -788,48 +788,48 @@ static void display_run(HPKT &hpkt)
          case 'M':  /* Messages */
             if (run->msgs) {
                res = (RES *)run->msgs;
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %s", RunFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %s", RunFields[i].name,
                      quote_string(hpkt.edbuf, res->name));
                first = false;
             }
             break;
          case 'p':  /* priority */
             if (run->priority_set) {
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %d", RunFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %d", RunFields[i].name,
                      run->Priority);
                first = false;
             }
             break;
          case 's':  /* Spool Data */
             if (run->spool_data_set) {
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %s", RunFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %s", RunFields[i].name,
                      run->spool_data?"true":"false");
                first = false;
             }
             break;
          case 'W':  /* Write Part After Job */
             if (run->write_part_after_job_set) {
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %s", RunFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %s", RunFields[i].name,
                      run->write_part_after_job?"true":"false");
                first = false;
             }
             break;
          case 'm':  /* MaxRunScheduledTime */
             if (run->MaxRunSchedTime_set) {
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %lld", RunFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %lld", RunFields[i].name,
                      run->MaxRunSchedTime);
                first = false;
             }
             break;
          case 'a':  /* Accurate */
             if (run->accurate_set) {
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %s", RunFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %s", RunFields[i].name,
                      run->accurate?"true":"false");
                first = false;
             }
@@ -840,52 +840,52 @@ static void display_run(HPKT &hpkt)
       } /* End all RunFields (overrides) */
       /* Now handle timing */
       if (byte_is_set(run->hour, sizeof(run->hour))) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"Hour\":");
-         display_bit_array(run->hour, 24);
-         sendit(NULL, ",\n      \"Minute\": %d", run->minute);
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"Hour\":");
+         display_bit_array(hpkt, run->hour, 24);
+         hpkt.sendit(hpkt, ",\n      \"Minute\": %d", run->minute);
          first = false;
       }
       /* bit 32 is used to store the keyword LastDay, so we look up to 0-31 */
       if (byte_is_set(run->mday, sizeof(run->mday))) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"Day\":");
-         display_bit_array(run->mday, 31);
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"Day\":");
+         display_bit_array(hpkt, run->mday, 31);
          first = false;
       }
       if (run->last_day_set) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"LastDay\": 1");
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"LastDay\": 1");
          first = false;
       }
       if (byte_is_set(run->month, sizeof(run->month))) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"Month\":");
-         display_bit_array(run->month, 12);
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"Month\":");
+         display_bit_array(hpkt, run->month, 12);
          first = false;
       }
       if (byte_is_set(run->wday, sizeof(run->wday))) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"DayOfWeek\":");
-         display_bit_array(run->wday, 7);
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"DayOfWeek\":");
+         display_bit_array(hpkt, run->wday, 7);
          first = false;
       }
       if (byte_is_set(run->wom, sizeof(run->wom))) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"WeekOfMonth\":");
-         display_bit_array(run->wom, 6);
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"WeekOfMonth\":");
+         display_bit_array(hpkt, run->wom, 6);
          first = false;
       }
       if (byte_is_set(run->woy, sizeof(run->woy))) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"WeekOfYear\":");
-         display_bit_array(run->woy, 54);
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"WeekOfYear\":");
+         display_bit_array(hpkt, run->woy, 54);
          first = false;
       }
-      sendit(NULL, "\n     }");
+      hpkt.sendit(hpkt, "\n     }");
 
    } /* End this Run directive */
-   sendit(NULL, "\n    ]");
+   hpkt.sendit(hpkt, "\n    ]");
 }
 
 /*
@@ -908,18 +908,18 @@ static void dump_json(display_filter *filter)
    /* List resources and directives */
    if (filter->do_only_data) {
       /* Skip the Name */
-      sendit(NULL, "[");
+      hpkt.sendit(hpkt, "[");
 
    /*
     * { "aa": { "Name": "aa",.. }, "bb": { "Name": "bb", ... }
     * or print a single item
     */
    } else if (filter->do_one || filter->do_list) {
-      sendit(NULL, "{");
+      hpkt.sendit(hpkt, "{");
 
    } else {
    /* [ { "Client": { "Name": "aa",.. } }, { "Director": { "Name": "bb", ... } } ]*/
-      sendit(NULL, "[");
+      hpkt.sendit(hpkt, "[");
    }
 
    first_res = true;
@@ -967,9 +967,9 @@ static void dump_json(display_filter *filter)
          }
 
          if (first_res) {
-            sendit(NULL, "\n");
+            hpkt.sendit(hpkt, "\n");
          } else {
-            sendit(NULL, ",\n");
+            hpkt.sendit(hpkt, ",\n");
          }
 
          /* Find where the Name is defined, should always be 0 */
@@ -981,7 +981,7 @@ static void dump_json(display_filter *filter)
          }
 
          if (filter->do_only_data) {
-            sendit(NULL, " {");
+            hpkt.sendit(hpkt, " {");
 
          } else if (filter->do_one) {
             /* Nothing to print */
@@ -993,13 +993,13 @@ static void dump_json(display_filter *filter)
             /* Search and display Name, should be the first item */
             for (item=0; items[item].name; item++) {
                if (strcmp(items[item].name, "Name") == 0) {
-                  sendit(NULL, "%s: {\n", quote_string(hpkt.edbuf2, *items[item].value));
+                  hpkt.sendit(hpkt, "%s: {\n", quote_string(hpkt.edbuf2, *items[item].value));
                   break;
                }
             }
          } else {
             /* Begin new resource */
-            sendit(NULL, "{\n  \"%s\": {", resources[resinx].name);
+            hpkt.sendit(hpkt, "{\n  \"%s\": {", resources[resinx].name);
          }
 
          first_res = false;
@@ -1036,7 +1036,7 @@ static void dump_json(display_filter *filter)
                   continue;
                }
                if (first_directive++ > 0) {
-                  sendit(NULL, ",");
+                  hpkt.sendit(hpkt, ",");
                }
 
                /* 1: found, 0: not found, -1 found but empty */
@@ -1078,7 +1078,7 @@ static void dump_json(display_filter *filter)
                } else if (items[item].handler == store_coll_type) {
                   display_collector_types(hpkt);
                } else {
-                  sendit(NULL, "\n    \"%s\": null", items[item].name);
+                  hpkt.sendit(hpkt, "\n    \"%s\": null", items[item].name);
                }
             } else { /* end if is present */
                /* For some directive, the bitmap is not set (like addresses) */
@@ -1088,12 +1088,12 @@ static void dump_json(display_filter *filter)
                       && items[item].handler == store_bool /* yes or no */
                       && *(bool *)(items[item].value) == true)
                   {
-                     if (first_directive++ > 0) sendit(NULL, ",");
+                     if (first_directive++ > 0) hpkt.sendit(hpkt, ",");
                      if (*(items[item-1].value) == NULL) {
-                        sendit(NULL, "\n    \"Autochanger\": %s", quote_string(hpkt.edbuf2, *items[name_pos].value));
+                        hpkt.sendit(hpkt, "\n    \"Autochanger\": %s", quote_string(hpkt.edbuf2, *items[name_pos].value));
                      } else {
                         STORE *r = (STORE *)*(items[item-1].value);
-                        sendit(NULL, "\n    \"Autochanger\": %s", quote_string(hpkt.edbuf2, r->name()));
+                        hpkt.sendit(hpkt, "\n    \"Autochanger\": %s", quote_string(hpkt.edbuf2, r->name()));
                      }
                   }
                }
@@ -1101,8 +1101,8 @@ static void dump_json(display_filter *filter)
                if (strcmp(resources[resinx].name, "Director") == 0) {
                   if (strcmp(items[item].name, "DirPort") == 0) {
                      if (get_first_port_host_order(director->DIRaddrs) != items[item].default_value) {
-                        if (first_directive++ > 0) sendit(NULL, ",");
-                        sendit(NULL, "\n    \"DirPort\": %d",
+                        if (first_directive++ > 0) hpkt.sendit(hpkt, ",");
+                        hpkt.sendit(hpkt, "\n    \"DirPort\": %d",
                            get_first_port_host_order(director->DIRaddrs));
                      }
 
@@ -1110,16 +1110,16 @@ static void dump_json(display_filter *filter)
                      char buf[500];
                      get_first_address(director->DIRaddrs, buf, sizeof(buf));
                      if (strcmp(buf, "0.0.0.0") != 0) {
-                        if (first_directive++ > 0) sendit(NULL, ",");
-                        sendit(NULL, "\n    \"DirAddress\": \"%s\"", buf);
+                        if (first_directive++ > 0) hpkt.sendit(hpkt, ",");
+                        hpkt.sendit(hpkt, "\n    \"DirAddress\": \"%s\"", buf);
                      }
 
                   } else if (strcmp(items[item].name, "DirSourceAddress") == 0 && director->DIRsrc_addr) {
                      char buf[500];
                      get_first_address(director->DIRsrc_addr, buf, sizeof(buf));
                      if (strcmp(buf, "0.0.0.0") != 0) {
-                        if (first_directive++ > 0) sendit(NULL, ",");
-                        sendit(NULL, "\n    \"DirSourceAddress\": \"%s\"", buf);
+                        if (first_directive++ > 0) hpkt.sendit(hpkt, ",");
+                        hpkt.sendit(hpkt, "\n    \"DirSourceAddress\": \"%s\"", buf);
                      }
                   }
                }
@@ -1131,31 +1131,31 @@ static void dump_json(display_filter *filter)
 
          /* { "aa": { "Name": "aa",.. }, "bb": { "Name": "bb", ... } */
          if (filter->do_only_data || filter->do_list) {
-            sendit(NULL, "\n }"); /* Finish the Resource with a single } */
+            hpkt.sendit(hpkt, "\n }"); /* Finish the Resource with a single } */
 
          } else {
             if (filter->do_one) {
                /* don't print anything */
 
             } else if (first_directive > 0) {
-               sendit(NULL, "\n  }\n}");  /* end of resource */
+               hpkt.sendit(hpkt, "\n  }\n}");  /* end of resource */
 
             } else {
-               sendit(NULL, "}\n}");
+               hpkt.sendit(hpkt, "}\n}");
             }
          }
       } /* End loop over all resources of this type */
    } /* End loop all resource types */
 
    if (filter->do_only_data) {
-      sendit(NULL, "\n]\n");
+      hpkt.sendit(hpkt, "\n]\n");
 
    /* In list context, we are dealing with a hash */
    } else if (filter->do_one || filter->do_list) {
-      sendit(NULL, "\n}\n");
+      hpkt.sendit(hpkt, "\n}\n");
 
    } else {
-      sendit(NULL, "\n]\n");
+      hpkt.sendit(hpkt, "\n]\n");
    }
    term_hpkt(hpkt);
 }
index 47f665506775afeca980c71b928c02cf26de7669..936fc9f2efc5ed4c0b653d70359fe85d145ec4ad 100644 (file)
@@ -92,7 +92,7 @@ static void display_cipher(HPKT &hpkt)
    int i;
    for (i=0; ciphertypes[i].type_name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == ciphertypes[i].type_value) {
-         sendit(NULL, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
                 ciphertypes[i].type_name);
          return;
       }
@@ -104,7 +104,7 @@ static void display_digest(HPKT &hpkt)
    int i;
    for (i=0; digesttypes[i].type_name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == digesttypes[i].type_value) {
-         sendit(NULL, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
                 digesttypes[i].type_name);
          return;
       }
@@ -285,19 +285,19 @@ static void display_runres(HPKT &hpkt)
    bool first = true;
    bool first_run = true;
 
-   sendit(NULL, "\n    \"%s\": [\n", hpkt.ritem->name);
+   hpkt.sendit(hpkt, "\n    \"%s\": [\n", hpkt.ritem->name);
    for ( ; run; run=run->next) {
-      if (!first_run) sendit(NULL, ",\n");
+      if (!first_run) hpkt.sendit(hpkt, ",\n");
       first_run = false;
       first = true;
-      sendit(NULL, "     {\n");
+      hpkt.sendit(hpkt, "     {\n");
       /* First do override fields */
       for (i=0; ConnectFields[i].name; i++) {
          switch (ConnectFields[i].token) {
          case 'm':  /* MaxConnectTime */
             if (run->MaxConnectTime_set) {
-               if (!first) sendit(NULL, ",\n");
-               sendit(NULL, "      \"%s\": %lld", ConnectFields[i].name,
+               if (!first) hpkt.sendit(hpkt, ",\n");
+               hpkt.sendit(hpkt, "      \"%s\": %lld", ConnectFields[i].name,
                      run->MaxConnectTime);
                first = false;
             }
@@ -308,52 +308,52 @@ static void display_runres(HPKT &hpkt)
       } /* End all ConnectFields (overrides) */
       /* Now handle timing */
       if (byte_is_set(run->hour, sizeof(run->hour))) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"Hour\":");
-         display_bit_array(run->hour, 24);
-         sendit(NULL, ",\n      \"Minute\": %d", run->minute);
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"Hour\":");
+         display_bit_array(hpkt, run->hour, 24);
+         hpkt.sendit(hpkt, ",\n      \"Minute\": %d", run->minute);
          first = false;
       }
       /* bit 32 is used to store the keyword LastDay, so we look up to 0-31 */
       if (byte_is_set(run->mday, sizeof(run->mday))) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"Day\":");
-         display_bit_array(run->mday, 31);
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"Day\":");
+         display_bit_array(hpkt, run->mday, 31);
          first = false;
       }
       if (run->last_day_set) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"LastDay\": 1");
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"LastDay\": 1");
          first = false;
       }
       if (byte_is_set(run->month, sizeof(run->month))) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"Month\":");
-         display_bit_array(run->month, 12);
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"Month\":");
+         display_bit_array(hpkt, run->month, 12);
          first = false;
       }
       if (byte_is_set(run->wday, sizeof(run->wday))) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"DayOfWeek\":");
-         display_bit_array(run->wday, 7);
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"DayOfWeek\":");
+         display_bit_array(hpkt, run->wday, 7);
          first = false;
       }
       if (byte_is_set(run->wom, sizeof(run->wom))) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"WeekOfMonth\":");
-         display_bit_array(run->wom, 6);
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"WeekOfMonth\":");
+         display_bit_array(hpkt, run->wom, 6);
          first = false;
       }
       if (byte_is_set(run->woy, sizeof(run->woy))) {
-         if (!first) sendit(NULL, ",\n");
-         sendit(NULL, "      \"WeekOfYear\":");
-         display_bit_array(run->woy, 54);
+         if (!first) hpkt.sendit(hpkt, ",\n");
+         hpkt.sendit(hpkt, "      \"WeekOfYear\":");
+         display_bit_array(hpkt, run->woy, 54);
          first = false;
       }
-      sendit(NULL, "\n     }");
+      hpkt.sendit(hpkt, "\n     }");
 
    } /* End this Run directive */
-   sendit(NULL, "\n    ]");
+   hpkt.sendit(hpkt, "\n    ]");
 }
 
 /*
@@ -376,18 +376,18 @@ static void dump_json(display_filter *filter)
    me = (CLIENT *)GetNextRes(R_CLIENT, NULL);
 
    if (filter->do_only_data) {
-      sendit(NULL, "[");
+      hpkt.sendit(hpkt, "[");
 
    /* List resources and directives */
    /* { "aa": { "Name": "aa",.. }, "bb": { "Name": "bb", ... }
     * or print a single item
     */
    } else if (filter->do_one || filter->do_list) {
-      sendit(NULL, "{");
+      hpkt.sendit(hpkt, "{");
 
    } else {
    /* [ { "Client": { "Name": "aa",.. } }, { "Director": { "Name": "bb", ... } } ]*/
-      sendit(NULL, "[");
+      hpkt.sendit(hpkt, "[");
    }
 
    first_res = true;
@@ -446,7 +446,7 @@ static void dump_json(display_filter *filter)
          first_directive = 0;
 
          if (filter->do_only_data) {
-            sendit(NULL, " {");
+            hpkt.sendit(hpkt, " {");
 
          } else if (filter->do_one) {
             /* Nothing to print */
@@ -458,13 +458,13 @@ static void dump_json(display_filter *filter)
             /* Search and display Name, should be the first item */
             for (item=0; items[item].name; item++) {
                if (strcmp(items[item].name, "Name") == 0) {
-                  sendit(NULL, "%s: {\n", quote_string(hpkt.edbuf2, *items[item].value));
+                  hpkt.sendit(hpkt, "%s: {\n", quote_string(hpkt.edbuf2, *items[item].value));
                   break;
                }
             }
          } else {
             /* Begin new resource */
-            sendit(NULL, "{\n  \"%s\": {", resources[resinx].name);
+            hpkt.sendit(hpkt, "{\n  \"%s\": {", resources[resinx].name);
          }
 
          /* dirtry trick for a deprecated directive */
@@ -549,15 +549,15 @@ static void dump_json(display_filter *filter)
 
          /* { "aa": { "Name": "aa",.. }, "bb": { "Name": "bb", ... } */
          if (filter->do_only_data || filter->do_list) {
-            sendit(NULL, "\n }"); /* Finish the Resource with a single } */
+            hpkt.sendit(hpkt, "\n }"); /* Finish the Resource with a single } */
 
          } else {
             if (filter->do_one) {
                /* don't print anything */
             } else if (first_directive > 0) {
-               sendit(NULL, "\n  }\n}");  /* end of resource */
+               hpkt.sendit(hpkt, "\n  }\n}");  /* end of resource */
             } else {
-               sendit(NULL, "}\n}");
+               hpkt.sendit(hpkt, "}\n}");
             }
          }
          first_res = false;
@@ -565,14 +565,14 @@ static void dump_json(display_filter *filter)
    } /* End loop all resource types */
 
    if (filter->do_only_data) {
-      sendit(NULL, "\n]\n");
+      hpkt.sendit(hpkt, "\n]\n");
 
    /* In list context, we are dealing with a hash */
    } else if (filter->do_one || filter->do_list) {
-      sendit(NULL, "\n}\n");
+      hpkt.sendit(hpkt, "\n}\n");
 
    } else {
-      sendit(NULL, "\n]\n");
+      hpkt.sendit(hpkt, "\n]\n");
    }
    term_hpkt(hpkt);
 }
index 5ee28d8958305081525b4378b3bf3f78a8c2ccfe..e1786593121fe77f26ee4f89ebb0cdceb127f4b1 100644 (file)
@@ -304,7 +304,7 @@ sha1_test: Makefile libbac.la sha1.c unittests.o
 
 bsnprintf_test: Makefile libbac.la bsnprintf.c unittests.o
        $(RMF) bsnprintf.o
-       $(CXX) -DTEST_PROGRAM $(DEFS) $(DEBUG) -c $(CPPFLAGS) -I$(srcdir) -I$(basedir) $(DINCLUDE)  $(CFLAGS) bsnprintf.c
+       $(CXX) -DTEST_PROGRAM $(DEFS) $(DEBUG) -c $(CPPFLAGS) -I$(srcdir) -I$(basedir) $(DINCLUDE)  $(CFLAGS) -Wno-format-truncation bsnprintf.c 
        $(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L. -o $@ bsnprintf.o unittests.o $(DLIB) -lbac -lm $(LIBS) $(OPENSSL_LIBS)
        $(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) $@ $(DESTDIR)$(sbindir)/
        $(RMF) bsnprintf.o
index 20bbcf940e1b7d5a739d2eca1dddb17e9c41d380..5163c90cdb1dce36dbe730deac44e873af24fcc2 100644 (file)
@@ -55,15 +55,29 @@ struct display_filter
    regex_t directive_reg;
 };
 
-static void sendit(void *sock, const char *fmt, ...)
+void bjson_sendit(HPKT &hpkt, const char *fmt, ...)
 {
-   char buf[3000];
    va_list arg_ptr;
+   bool done=false;
+
+   while (!done) {
+      uint32_t len = sizeof_pool_memory(hpkt.out);
+      va_start(arg_ptr, fmt);
+      // The return value of our bvsnprintf() doesn't represent
+      // the full length of the string, we need to check it afterward
+      bvsnprintf(hpkt.out, len, (char *)fmt, arg_ptr);
+      va_end(arg_ptr);
+
+      if (strlen(hpkt.out) >= (len - 1)) {
+         // We got an overflow, we need more room to display the output
+         hpkt.out = check_pool_memory_size(hpkt.out, len*2);
+
+      } else {
+         done = true;
+      }
+   }
 
-   va_start(arg_ptr, fmt);
-   bvsnprintf(buf, sizeof(buf), (char *)fmt, arg_ptr);
-   va_end(arg_ptr);
-   fputs(buf, stdout);
+   fputs(hpkt.out, stdout);
    fflush(stdout);
 }
 
@@ -72,15 +86,17 @@ void init_hpkt(HPKT &hpkt)
    memset(&hpkt, 0, sizeof(hpkt));
    hpkt.edbuf = get_pool_memory(PM_EMSG);
    hpkt.edbuf2 = get_pool_memory(PM_EMSG);
+   hpkt.out = get_pool_memory(PM_EMSG);
    hpkt.json = true;
    hpkt.hfunc = HF_DISPLAY;
-   hpkt.sendit = sendit;
+   hpkt.sendit = bjson_sendit;
 }
 
 void term_hpkt(HPKT &hpkt)
 {
    free_pool_memory(hpkt.edbuf);
    free_pool_memory(hpkt.edbuf2);
+   free_pool_memory(hpkt.out);
    memset(&hpkt, 0, sizeof(hpkt));
 }
 
@@ -150,7 +166,7 @@ void edit_msg_types(HPKT &hpkt, DEST *dest)
             }
          }
          if (!found) {
-            sendit(NULL, "No find for type=%d\n", i);
+            bjson_sendit(hpkt, "No find for type=%d\n", i);
          }
          count++;
       }
@@ -182,7 +198,7 @@ void edit_msg_types(HPKT &hpkt, DEST *dest)
                }
             }
             if (!found) {
-               sendit(NULL, "No find for type=%d in second loop\n", i);
+               bjson_sendit(hpkt, "No find for type=%d in second loop\n", i);
             }
          } else if (i == M_SAVED) {
             /* Saved is not set by default, users must explicitly use it
@@ -257,11 +273,11 @@ bool display_msgs(HPKT &hpkt)
 
    if (!hpkt.in_store_msg) {
       hpkt.in_store_msg = true;
-      sendit(NULL, "\n    \"Destinations\": [");
+      bjson_sendit(hpkt, "\n    \"Destinations\": [");
    }
    for (dest=msgs->dest_chain; dest; dest=dest->next) {
       if (dest->dest_code == hpkt.ritem->code) {
-         if (!first) sendit(NULL, ",");
+         if (!first) bjson_sendit(hpkt, ",");
          first = false;
          edit_msg_types(hpkt, dest);
          switch (hpkt.ritem->code) {
@@ -271,7 +287,7 @@ bool display_msgs(HPKT &hpkt)
          case MD_SYSLOG:
          case MD_CONSOLE:
          case MD_CATALOG:
-            sendit(NULL, "\n      {\n        \"Type\": \"%s\","
+            bjson_sendit(hpkt, "\n      {\n        \"Type\": \"%s\","
                          "\n        \"MsgTypes\": %s\n      }",
                hpkt.ritem->name, hpkt.edbuf);
             break;
@@ -279,10 +295,10 @@ bool display_msgs(HPKT &hpkt)
          case MD_DIRECTOR:
          case MD_FILE:
          case MD_APPEND:
-            sendit(NULL, "\n      {\n        \"Type\": \"%s\","
+            bjson_sendit(hpkt, "\n      {\n        \"Type\": \"%s\","
                          "\n        \"MsgTypes\": %s,\n",
                hpkt.ritem->name, hpkt.edbuf);
-            sendit(NULL, "        \"Where\": [%s]\n      }",
+            bjson_sendit(hpkt, "        \"Where\": [%s]\n      }",
                quote_where(hpkt.edbuf, dest->where));
             break;
          /* Now we edit MsgTypes, Where, and Command */
@@ -290,12 +306,12 @@ bool display_msgs(HPKT &hpkt)
          case MD_OPERATOR:
          case MD_MAIL_ON_ERROR:
          case MD_MAIL_ON_SUCCESS:
-            sendit(NULL, "\n      {\n        \"Type\": \"%s\","
+            bjson_sendit(hpkt, "\n      {\n        \"Type\": \"%s\","
                          "\n        \"MsgTypes\": %s,\n",
                hpkt.ritem->name, hpkt.edbuf);
-            sendit(NULL, "        \"Where\": [%s],\n",
+            bjson_sendit(hpkt, "        \"Where\": [%s],\n",
                quote_where(hpkt.edbuf, dest->where));
-            sendit(NULL, "        \"Command\": %s\n      }",
+            bjson_sendit(hpkt, "        \"Command\": %s\n      }",
                quote_string(hpkt.edbuf, dest->mail_cmd));
             break;
          default:
@@ -316,14 +332,14 @@ void display_last(HPKT &hpkt)
 {
    if (hpkt.in_store_msg) {
       hpkt.in_store_msg = false;
-      sendit(NULL, "\n    ]");
+      bjson_sendit(hpkt, "\n    ]");
    }
 }
 
 void display_alist(HPKT &hpkt)
 {
    edit_alist(hpkt);
-   sendit(NULL, "%s", hpkt.edbuf);
+   bjson_sendit(hpkt, "%s", hpkt.edbuf);
 }
 
 bool display_alist_str(HPKT &hpkt)
@@ -332,7 +348,7 @@ bool display_alist_str(HPKT &hpkt)
    if (!hpkt.list) {
       return false;
    }
-   sendit(NULL, "\n    \"%s\":", hpkt.ritem->name);
+   bjson_sendit(hpkt, "\n    \"%s\":", hpkt.ritem->name);
    display_alist(hpkt);
    return true;
 }
@@ -347,16 +363,16 @@ bool display_alist_res(HPKT &hpkt)
    if (!list) {
       return false;
    }
-   sendit(NULL, "\n    \"%s\":", hpkt.ritem->name);
-   sendit(NULL, " [");
+   bjson_sendit(hpkt, "\n    \"%s\":", hpkt.ritem->name);
+   bjson_sendit(hpkt, " [");
    foreach_alist(res, list) {
       if (!f) {
-         sendit(NULL, ", ");
+         bjson_sendit(hpkt, ", ");
       }
-      sendit(NULL, "%s", quote_string(hpkt.edbuf, res->name));
+      bjson_sendit(hpkt, "%s", quote_string(hpkt.edbuf, res->name));
       f = false;
    }
-   sendit(NULL, "]");
+   bjson_sendit(hpkt, "]");
    return true;
 }
 
@@ -365,39 +381,39 @@ void display_res(HPKT &hpkt)
    RES *res;
 
    res = (RES *)*hpkt.ritem->value;
-   sendit(NULL, "\n    \"%s\": %s", hpkt.ritem->name,
+   bjson_sendit(hpkt, "\n    \"%s\": %s", hpkt.ritem->name,
       quote_string(hpkt.edbuf, res->name));
 }
 
 void display_string_pair(HPKT &hpkt)
 {
-   sendit(NULL, "\n    \"%s\": %s", hpkt.ritem->name,
+   bjson_sendit(hpkt, "\n    \"%s\": %s", hpkt.ritem->name,
       quote_string(hpkt.edbuf, *hpkt.ritem->value));
 }
 
 void display_int32_pair(HPKT &hpkt)
 {
    char ed1[50];
-   sendit(NULL, "\n    \"%s\": %s", hpkt.ritem->name,
+   bjson_sendit(hpkt, "\n    \"%s\": %s", hpkt.ritem->name,
       edit_int64(*(int32_t *)hpkt.ritem->value, ed1));
 }
 
 void display_int64_pair(HPKT &hpkt)
 {
    char ed1[50];
-   sendit(NULL, "\n    \"%s\": %s", hpkt.ritem->name,
+   bjson_sendit(hpkt, "\n    \"%s\": %s", hpkt.ritem->name,
       edit_int64(*(int64_t *)hpkt.ritem->value, ed1));
 }
 
 void display_bool_pair(HPKT &hpkt)
 {
-   sendit(NULL, "\n    \"%s\": %s", hpkt.ritem->name,
+   bjson_sendit(hpkt, "\n    \"%s\": %s", hpkt.ritem->name,
       ((*(bool *)(hpkt.ritem->value)) == 0)?"false":"true");
 }
 
 void display_bit_pair(HPKT &hpkt)
 {
-   sendit(NULL, "\n    \"%s\": %s", hpkt.ritem->name,
+   bjson_sendit(hpkt, "\n    \"%s\": %s", hpkt.ritem->name,
       ((*(uint32_t *)(hpkt.ritem->value) & hpkt.ritem->code)
          == 0)?"false":"true");
 }
@@ -415,19 +431,19 @@ bool byte_is_set(char *byte, int num)
    return found;
 }
 
-void display_bit_array(char *array, int num)
+void display_bit_array(HPKT &hpkt, char *array, int num)
 {
    int i;
    bool first = true;
-   sendit(NULL, " [");
+   bjson_sendit(hpkt, " [");
    for (i=0; i<num; i++) {
       if (bit_is_set(i, array)) {
-         if (!first) sendit(NULL, ", ");
+         if (!first) bjson_sendit(hpkt, ", ");
          first = false;
-         sendit(NULL, "%d", i);
+         bjson_sendit(hpkt, "%d", i);
       }
    }
-   sendit(NULL, "]");
+   bjson_sendit(hpkt, "]");
 }
 
 void display_collector_types(HPKT &hpkt)
@@ -435,7 +451,7 @@ void display_collector_types(HPKT &hpkt)
    int i;
    for (i=0; collectortypes[i].type_name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == collectortypes[i].coll_type) {
-         sendit(NULL, "\n    \"%s\": %s", hpkt.ritem->name,
+         bjson_sendit(hpkt, "\n    \"%s\": %s", hpkt.ritem->name,
             quote_string(hpkt.edbuf, collectortypes[i].type_name));
          return;
       }
index 66b7a7d2b24e8f1e9e7166d1d6d4407f73851da5..67a0ebf40ca07e17ba125348b5cb2bb4f5335924 100644 (file)
@@ -42,17 +42,20 @@ enum HFUNC {
 struct HPKT {
    POOLMEM *edbuf;                    /* editing buffer */
    POOLMEM *edbuf2;                   /* editing buffer */
+   POOLMEM *out;                      /* output buffer */
    RES_ITEM *ritem;                   /* RES_ITEM for call */
    RES *res;                          /* Pointer to resource header */
    HFUNC hfunc;                       /* Handler function to do */
    bool json;                         /* set to display Json */
    bool in_store_msg;                 /* set when doing store_msg */
    bool exclude;                      /* Include/Exclude flage */
-   void (*sendit)(void *sock, const char *fmt, ...); /* print routine */
+   void (*sendit)(HPKT &hpkt, const char *fmt, ...); /* print routine */
    LEX *lc;                           /* Lex packet */
    int index;                         /* Index item ITEM table */
    int pass;                          /* Store pass number */
    alist *list;                       /* alist to edit */
 };
 
+void bjson_sendit(HPKT &hpkt, const char *fmt, ...);
+
 #endif /* __BJSON_H__ */
index 961530bee5e861a02c2698b6c449022f1ddf60f8..0360220ae6674ca5aabf516402452127f07b1445 100644 (file)
@@ -961,9 +961,9 @@ int main(int argc, char *argv[])
 
    int x, y;
 
-   printf("\n\tTesting bsnprintf against system sprintf...\n\n");
+   log("Testing bsnprintf against system sprintf...");
 #ifdef FP_OUTPUT
-   printf("Testing bsnprintf float format codes\n");
+   log("Testing bsnprintf float format codes");
    for (x = 0; fp_fmt[x] != NULL; x++){
       check_cont = true;
       check_nr = true;
@@ -984,7 +984,7 @@ int main(int argc, char *argv[])
    }
 #endif
 
-   printf("Testing bsnprintf int format codes\n");
+   log("Testing bsnprintf int format codes");
    for (x = 0; int_fmt[x] != NULL; x++){
       check_cont = true;
       check_nr = true;
@@ -1004,7 +1004,7 @@ int main(int argc, char *argv[])
       ok(check_cont, msg);
    }
 
-   printf("Testing bsnprintf long format codes\n");
+   log("Testing bsnprintf long format codes");
    for (x = 0; ll_fmt[x] != NULL; x++) {
       check_cont = true;
       check_nr = true;
@@ -1024,7 +1024,7 @@ int main(int argc, char *argv[])
       ok(check_cont, msg);
    }
 
-   printf("Testing bsnprintf str format codes\n");
+   log("Testing bsnprintf str format codes");
    for (x = 0; s_fmt[x] != NULL; x++) {
       check_cont = true;
       check_nr = true;
@@ -1044,7 +1044,7 @@ int main(int argc, char *argv[])
       ok(check_cont, msg);
    }
 
-   printf("Testing bsnprintf long str format codes\n");
+   log("Testing bsnprintf long str format codes");
    for (x = 0; ls_fmt[x] != NULL; x++) {
       check_cont = true;
       check_nr = true;
@@ -1064,6 +1064,17 @@ int main(int argc, char *argv[])
       ok(check_cont, msg);
    }
 
+   log("Testing bsnprintf return bytes");
+   bcount = bsnprintf(buf1, sizeof(buf1), "This is a format with a number %d", 1);
+   pcount = sprintf(buf1, "This is a format with a number %d", 1);
+   is (strlen(buf1), bcount, "Checking the return code against strlen()");
+   is (bcount, pcount, "Checking the return code of sprintf()");
+
+   bcount = bsnprintf(buf1, 10, "This is a format with a number %d", 1);
+   pcount = snprintf(buf1, 10, "This is a format with a number %d", 1);
+   is (strlen(buf1), 9, "Checking the return code against strlen()");
+   is (bcount, 10, "Checking the return code of sprintf()");
+   is (bcount, pcount, "Checking difference between bsnprintf() and snprintf()");
    return report();
 }
 #endif /* TEST_PROGRAM */
index 1acae333162d0c2d18ed244c836d11c626914d96..0b9b5143b02795636829a790952238b5590972f8 100644 (file)
@@ -59,7 +59,7 @@ void display_int64_pair(HPKT &hpkt);
 void display_bool_pair(HPKT &hpkt);
 void display_bit_pair(HPKT &hpkt);
 bool byte_is_set(char *byte, int num);
-void display_bit_array(char *array, int num);
+void display_bit_array(HPKT &hpkt, char *array, int num);
 void display_last(HPKT &hpkt);
 void init_hpkt(HPKT &hpkt);
 void term_hpkt(HPKT &hpkt);
index 94cf5a1a56e5d2572478cb04811bed7008e643f6..3170d01d27287fe3be5715ef1cbde2c4085dd901 100644 (file)
@@ -63,7 +63,6 @@ typedef struct
 /* Forward referenced functions */
 void terminate_stored(int sig);
 static int check_resources();
-static void sendit(void *sock, const char *fmt, ...);
 static void dump_json(display_filter *filter);
 
 #define CONFIG_FILE "bacula-sd.conf"  /* Default config file */
@@ -75,7 +74,7 @@ char *configfile = NULL;
 
 /* Global static variables */
 static CONFIG *config;
-
+static void sendit(void *sock, const char *fmt, ...);
 
 static void usage()
 {
@@ -239,7 +238,7 @@ static void display_devtype(HPKT &hpkt)
    int i;
    for (i=0; dev_types[i].name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == dev_types[i].token) {
-         sendit(NULL, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
                 dev_types[i].name);
          return;
       }
@@ -251,7 +250,7 @@ static void display_label(HPKT &hpkt)
    int i;
    for (i=0; tapelabels[i].name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == tapelabels[i].token) {
-         sendit(NULL, "\n      \"%s\": \"%s\"", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n      \"%s\": \"%s\"", hpkt.ritem->name,
                 tapelabels[i].name);
          return;
       }
@@ -263,7 +262,7 @@ static void display_cloud_driver(HPKT &hpkt)
    int i;
    for (i=0; cloud_drivers[i].name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == cloud_drivers[i].token) {
-         sendit(NULL, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
                 cloud_drivers[i].name);
          return;
       }
@@ -276,7 +275,7 @@ static void display_dedup_driver(HPKT &hpkt)
    int i;
    for (i=0; dedup_drivers[i].name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == dedup_drivers[i].token) {
-         sendit(NULL, "\n      \"%s\": \"%s\"", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n      \"%s\": \"%s\"", hpkt.ritem->name,
                 dedup_drivers[i].name);
          return;
       }
@@ -289,7 +288,7 @@ static void display_protocol(HPKT &hpkt)
    int i;
    for (i=0; proto_opts[i].name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == proto_opts[i].token) {
-         sendit(NULL, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
                 proto_opts[i].name);
          return;
       }
@@ -301,7 +300,7 @@ static void display_truncate_cache(HPKT &hpkt)
    int i;
    for (i=0; trunc_opts[i].name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == trunc_opts[i].token) {
-         sendit(NULL, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
                 trunc_opts[i].name);
          return;
       }
@@ -313,7 +312,7 @@ static void display_uri_style(HPKT &hpkt)
    int i;
    for (i=0; uri_opts[i].name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == uri_opts[i].token) {
-         sendit(NULL, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
                 uri_opts[i].name);
          return;
       }
@@ -325,7 +324,7 @@ static void display_upload(HPKT &hpkt)
    int i;
    for (i=0; upload_opts[i].name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == upload_opts[i].token) {
-         sendit(NULL, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
                 upload_opts[i].name);
          return;
       }
@@ -338,7 +337,7 @@ static void display_transfer_priority(HPKT &hpkt)
    int i;
    for (i=0; restore_prio_opts[i].name; i++) {
       if (*(int32_t *)(hpkt.ritem->value) == restore_prio_opts[i].token) {
-         sendit(NULL, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
+         hpkt.sendit(hpkt, "\n    \"%s\": \"%s\"", hpkt.ritem->name,
                 restore_prio_opts[i].name);
          return;
       }
@@ -367,18 +366,18 @@ static void dump_json(display_filter *filter)
    init_hpkt(hpkt);
 
    if (filter->do_only_data) {
-      sendit(NULL, "[");
+      hpkt.sendit(hpkt, "[");
 
    /* List resources and directives */
    /* { "aa": { "Name": "aa",.. }, "bb": { "Name": "bb", ... }
     * or print a single item
     */
    } else if (filter->do_one || filter->do_list) {
-      sendit(NULL, "{");
+      hpkt.sendit(hpkt, "{");
 
    } else {
    /* [ { "Device": { "Name": "aa",.. } }, { "Director": { "Name": "bb", ... } } ]*/
-      sendit(NULL, "[");
+      hpkt.sendit(hpkt, "[");
    }
 
    first_res = true;
@@ -423,13 +422,13 @@ static void dump_json(display_filter *filter)
          }
 
          if (first_res) {
-            sendit(NULL, "\n");
+            hpkt.sendit(hpkt, "\n");
          } else {
-            sendit(NULL, ",\n");
+            hpkt.sendit(hpkt, ",\n");
          }
 
          if (filter->do_only_data) {
-            sendit(NULL, " {");
+            hpkt.sendit(hpkt, " {");
 
          } else if (filter->do_one) {
             /* Nothing to print */
@@ -441,13 +440,13 @@ static void dump_json(display_filter *filter)
             /* Search and display Name, should be the first item */
             for (item=0; items[item].name; item++) {
                if (strcmp(items[item].name, "Name") == 0) {
-                  sendit(NULL, "%s: {\n", quote_string(hpkt.edbuf2, *items[item].value));
+                  hpkt.sendit(hpkt, "%s: {\n", quote_string(hpkt.edbuf2, *items[item].value));
                   break;
                }
             }
          } else {
             /* Begin new resource */
-            sendit(NULL, "{\n  \"%s\": {", resources[resinx].name);
+            hpkt.sendit(hpkt, "{\n  \"%s\": {", resources[resinx].name);
          }
 
          first_res = false;
@@ -507,16 +506,16 @@ static void dump_json(display_filter *filter)
                if (me && strcmp(resources[resinx].name, "Storage") == 0) {
                   if (strcmp(items[item].name, "SdPort") == 0) {
                      if (get_first_port_host_order(me->sdaddrs) != items[item].default_value) {
-                        if (first_directive++ > 0) sendit(NULL, ",");
-                        sendit(NULL, "\n    \"SdPort\": %d",
+                        if (first_directive++ > 0) hpkt.sendit(hpkt, ",");
+                        hpkt.sendit(hpkt, "\n    \"SdPort\": %d",
                            get_first_port_host_order(me->sdaddrs));
                      }
                   } else if (me && strcmp(items[item].name, "SdAddress") == 0) {
                      char buf[500];
                      get_first_address(me->sdaddrs, buf, sizeof(buf));
                      if (strcmp(buf, "0.0.0.0") != 0) {
-                        if (first_directive++ > 0) sendit(NULL, ",");
-                        sendit(NULL, "\n    \"SdAddress\": \"%s\"", buf);
+                        if (first_directive++ > 0) hpkt.sendit(hpkt, ",");
+                        hpkt.sendit(hpkt, "\n    \"SdAddress\": \"%s\"", buf);
                      }
                   }
                }
@@ -528,17 +527,17 @@ static void dump_json(display_filter *filter)
 
          /* { "aa": { "Name": "aa",.. }, "bb": { "Name": "bb", ... } */
          if (filter->do_only_data || filter->do_list) {
-            sendit(NULL, "\n }"); /* Finish the Resource with a single } */
+            hpkt.sendit(hpkt, "\n }"); /* Finish the Resource with a single } */
 
          } else {
             if (filter->do_one) {
                /* don't print anything */
 
             } else if (first_directive > 0) {
-               sendit(NULL, "\n  }\n}");  /* end of resource */
+               hpkt.sendit(hpkt, "\n  }\n}");  /* end of resource */
 
             } else {
-               sendit(NULL, "}\n }");
+               hpkt.sendit(hpkt, "}\n }");
             }
          }
 
@@ -546,14 +545,14 @@ static void dump_json(display_filter *filter)
    } /* End loop all resource types */
 
    if (filter->do_only_data) {
-      sendit(NULL, "\n]\n");
+      hpkt.sendit(hpkt, "\n]\n");
 
    /* In list context, we are dealing with a hash */
    } else if (filter->do_one || filter->do_list) {
-      sendit(NULL, "\n}\n");
+      hpkt.sendit(hpkt, "\n}\n");
 
    } else {
-      sendit(NULL, "\n]\n");
+      hpkt.sendit(hpkt, "\n]\n");
    }
    term_hpkt(hpkt);
 }