Browse code

Corrections to freshclam logging initialization. Added notation to --help output for --stdout option to indicate that debug messages will not be redirected. Changing direct calls to cli_dbgmsg_internal to use cli_dbgmsg, as cli_dbgmsg_internal always prints, even when --debug is not enabled.

Micah Snyder authored on 2019/05/22 03:49:42
Showing 7 changed files
... ...
@@ -191,7 +191,7 @@ void help(void)
191 191
     mprintf("    --version           -V             Print version number and exit\n");
192 192
     mprintf("    --verbose           -v             Be verbose\n");
193 193
     mprintf("    --quiet                            Be quiet, only output error messages\n");
194
-    mprintf("    --stdout                           Write to stdout instead of stderr\n");
194
+    mprintf("    --stdout                           Write to stdout instead of stderr. Does not affect 'debug' messages.\n");
195 195
     mprintf("                                       (this help is always written to stdout)\n");
196 196
     mprintf("    --log=FILE          -l FILE        Save scan report in FILE\n");
197 197
     mprintf("    --file-list=FILE    -f FILE        Scan files from FILE\n");
... ...
@@ -208,7 +208,7 @@ void help(void)
208 208
     mprintf("    --archive-verbose     -a             Show filenames inside scanned archives\n");
209 209
     mprintf("    --debug                              Enable libclamav's debug messages\n");
210 210
     mprintf("    --quiet                              Only output error messages\n");
211
-    mprintf("    --stdout                             Write to stdout instead of stderr\n");
211
+    mprintf("    --stdout                             Write to stdout instead of stderr. Does not affect 'debug' messages.\n");
212 212
     mprintf("    --no-summary                         Disable summary at end of scanning\n");
213 213
     mprintf("    --infected            -i             Only print infected files\n");
214 214
     mprintf("    --suppress-ok-results -o             Skip printing OK files\n");
... ...
@@ -46,6 +46,10 @@
46 46
 #include <grp.h>
47 47
 #endif
48 48
 
49
+#if defined(USE_SYSLOG) && !defined(C_AIX)
50
+#include <syslog.h>
51
+#endif
52
+
49 53
 #include "target.h"
50 54
 #include "clamav.h"
51 55
 #include "libfreshclam/libfreshclam.h"
... ...
@@ -147,7 +151,7 @@ static void help(void)
147 147
     mprintf("    --debug                              Enable debug messages\n");
148 148
     mprintf("    --quiet                              Only output error messages\n");
149 149
     mprintf("    --no-warnings                        Don't print and log warnings\n");
150
-    mprintf("    --stdout                             Write to stdout instead of stderr\n");
150
+    mprintf("    --stdout                             Write to stdout instead of stderr. Does not affect 'debug' messages.\n");
151 151
     mprintf("    --show-progress                      Show download progress percentage\n");
152 152
     mprintf("\n");
153 153
     mprintf("    --config-file=FILE                   Read configuration from FILE.\n");
... ...
@@ -172,7 +176,7 @@ static void help(void)
172 172
     mprintf("\n");
173 173
 }
174 174
 
175
-static void msg_callback(enum cl_msg severity, const char *fullmsg, const char *msg, void *ctx)
175
+static void libclamav_msg_callback(enum cl_msg severity, const char *fullmsg, const char *msg, void *ctx)
176 176
 {
177 177
     UNUSEDPARAM(fullmsg);
178 178
     UNUSEDPARAM(ctx);
... ...
@@ -190,6 +194,20 @@ static void msg_callback(enum cl_msg severity, const char *fullmsg, const char *
190 190
     }
191 191
 }
192 192
 
193
+static void libclamav_msg_callback_quiet(enum cl_msg severity, const char *fullmsg, const char *msg, void *ctx)
194
+{
195
+    UNUSEDPARAM(fullmsg);
196
+    UNUSEDPARAM(ctx);
197
+
198
+    switch (severity) {
199
+        case CL_MSG_ERROR:
200
+            logg("^[LibClamAV] %s", msg);
201
+            break;
202
+        default:
203
+            break;
204
+    }
205
+}
206
+
193 207
 fc_error_t download_complete_callback(const char *dbFilename, void *context)
194 208
 {
195 209
     fc_error_t status = FC_EARG;
... ...
@@ -749,46 +767,91 @@ static fc_error_t initialize(struct optstruct *opts)
749 749
         goto done;
750 750
     }
751 751
 
752
-    /* Set libclamav message callback. */
753
-    cl_set_clcb_msg(msg_callback);
754
-
755 752
     /*
756 753
      * Identify libfreshclam config options.
757 754
      */
758
-    /* Set libclamav Message option flags. */
755
+    /* Set libclamav Message and [file-based] Logging option flags.
756
+       mprintf and logg options are also directly set, as they are also
757
+       used in freshclam (not only used in libfreshclam) */
759 758
     if (optget(opts, "Debug")->enabled || optget(opts, "debug")->enabled)
760 759
         fcConfig.msgFlags |= FC_CONFIG_MSG_DEBUG;
761
-    if (optget(opts, "verbose")->enabled)
760
+
761
+    if ((optget(opts, "verbose")->enabled) ||
762
+        (optget(opts, "LogVerbose")->enabled)) {
762 763
         fcConfig.msgFlags |= FC_CONFIG_MSG_VERBOSE;
763
-    if (optget(opts, "quiet")->enabled)
764
+        fcConfig.logFlags |= FC_CONFIG_LOG_VERBOSE;
765
+        mprintf_verbose = 1;
766
+        logg_verbose    = 1;
767
+    }
768
+
769
+    if (optget(opts, "quiet")->enabled) {
764 770
         fcConfig.msgFlags |= FC_CONFIG_MSG_QUIET;
765
-    if (optget(opts, "no-warnings")->enabled)
771
+        mprintf_quiet = 1;
772
+        /* Silence libclamav messages. */
773
+        cl_set_clcb_msg(libclamav_msg_callback_quiet);
774
+    } else {
775
+        /* Enable libclamav messages, with [LibClamAV] message prefix. */
776
+        cl_set_clcb_msg(libclamav_msg_callback);
777
+    }
778
+
779
+    if (optget(opts, "no-warnings")->enabled) {
766 780
         fcConfig.msgFlags |= FC_CONFIG_MSG_NOWARN;
767
-    if (optget(opts, "stdout")->enabled)
781
+        fcConfig.logFlags |= FC_CONFIG_LOG_NOWARN;
782
+        mprintf_nowarn = 1;
783
+        logg_nowarn    = 1;
784
+    }
785
+
786
+    if (optget(opts, "stdout")->enabled) {
768 787
         fcConfig.msgFlags |= FC_CONFIG_MSG_STDOUT;
769
-    if (optget(opts, "show-progress")->enabled)
788
+        mprintf_stdout = 1;
789
+    }
790
+
791
+    if (optget(opts, "show-progress")->enabled) {
770 792
         fcConfig.msgFlags |= FC_CONFIG_MSG_SHOWPROGRESS;
793
+        mprintf_progress = 1;
794
+    }
771 795
 
772
-    /* Set libclamav [file-based] Logging option flags. */
773
-    if (mprintf_verbose ? 1 : optget(opts, "LogVerbose")->enabled)
774
-        fcConfig.logFlags |= FC_CONFIG_LOG_VERBOSE;
775
-    if (optget(opts, "LogTime")->enabled)
796
+    if (optget(opts, "LogTime")->enabled) {
776 797
         fcConfig.logFlags |= FC_CONFIG_LOG_TIME;
777
-    if (optget(opts, "LogFileMaxSize")->numarg && optget(opts, "LogRotate")->enabled)
798
+        logg_time = 1;
799
+    }
800
+    if (optget(opts, "LogFileMaxSize")->numarg && optget(opts, "LogRotate")->enabled) {
778 801
         fcConfig.logFlags |= FC_CONFIG_LOG_ROTATE;
802
+        logg_rotate = 1;
803
+    }
779 804
     if (optget(opts, "LogSyslog")->enabled)
780 805
         fcConfig.logFlags |= FC_CONFIG_LOG_SYSLOG;
781 806
 
782 807
     logFileOpt = optget(opts, "UpdateLogFile");
783
-    if (logFileOpt->enabled)
808
+    if (logFileOpt->enabled) {
784 809
         fcConfig.logFile = logFileOpt->strarg;
785
-    if (optget(opts, "LogFileMaxSize")->numarg)
810
+        logg_file        = cli_strdup(fcConfig.logFile);
811
+        if (0 != logg("#--------------------------------------\n")) {
812
+            mprintf("!Problem with internal logger (UpdateLogFile = %s).\n", logg_file);
813
+            status = FC_ELOGGING;
814
+            goto done;
815
+        }
816
+    }
817
+    if (optget(opts, "LogFileMaxSize")->numarg) {
786 818
         fcConfig.maxLogSize = optget(opts, "LogFileMaxSize")->numarg;
819
+        logg_size           = 1;
820
+    }
787 821
 
788 822
 #if defined(USE_SYSLOG) && !defined(C_AIX)
789 823
     if (optget(opts, "LogSyslog")->enabled) {
790
-        if (optget(opts, "LogFacility")->enabled)
824
+        if (optget(opts, "LogFacility")->enabled) {
825
+            int logFacility = LOG_LOCAL6;
826
+
791 827
             fcConfig.logFacility = optget(opts, "LogFacility")->strarg;
828
+            if ((NULL != fcConfig.logFacility) && (-1 == (logFacility = logg_facility(fcConfig.logFacility)))) {
829
+                mprintf("!LogFacility: %s: No such facility.\n", fcConfig.logFacility);
830
+                status = FC_ELOGGING;
831
+                goto done;
832
+            }
833
+
834
+            openlog("freshclam", LOG_PID, logFacility);
835
+            logg_syslog = 1;
836
+        }
792 837
     }
793 838
 #endif
794 839
 
... ...
@@ -896,7 +959,7 @@ static fc_error_t initialize(struct optstruct *opts)
896 896
      */
897 897
     ret = switch_user(optget(opts, "DatabaseOwner")->strarg);
898 898
     if (FC_SUCCESS != ret) {
899
-        logg("!Failedd to switch to %s user.\n", optget(opts, "DatabaseOwner")->strarg);
899
+        logg("!Failed to switch to %s user.\n", optget(opts, "DatabaseOwner")->strarg);
900 900
         status = ret;
901 901
         goto done;
902 902
     }
... ...
@@ -1560,15 +1560,15 @@ static cl_error_t asn1_parse_mscat(struct cl_engine *engine, fmap_t *map, size_t
1560 1560
                         // Change this so that raw is only populated when the
1561 1561
                         // debug flag is set, and then copy/display the full
1562 1562
                         // contents.
1563
-                        cli_dbgmsg_internal("cert:\n");
1564
-                        cli_dbgmsg_internal("  subject: %s\n", subject);
1565
-                        cli_dbgmsg_internal("  serial: %s\n", serial);
1566
-                        cli_dbgmsg_internal("  pubkey: %s\n", mod);
1567
-                        cli_dbgmsg_internal("  i: %s %lu->%lu %s%s%s\n", issuer, (unsigned long)x509->not_before, (unsigned long)x509->not_after, x509->codeSign ? "code " : "", x509->timeSign ? "time " : "", x509->certSign ? "cert " : "");
1568
-                        cli_dbgmsg_internal("  ==============RAW==============\n");
1569
-                        cli_dbgmsg_internal("  raw_subject: %s\n", raw_subject);
1570
-                        cli_dbgmsg_internal("  raw_serial: %s\n", raw_serial);
1571
-                        cli_dbgmsg_internal("  raw_issuer: %s\n", raw_issuer);
1563
+                        cli_dbgmsg("cert:\n");
1564
+                        cli_dbgmsg("  subject: %s\n", subject);
1565
+                        cli_dbgmsg("  serial: %s\n", serial);
1566
+                        cli_dbgmsg("  pubkey: %s\n", mod);
1567
+                        cli_dbgmsg("  i: %s %lu->%lu %s%s%s\n", issuer, (unsigned long)x509->not_before, (unsigned long)x509->not_after, x509->codeSign ? "code " : "", x509->timeSign ? "time " : "", x509->certSign ? "cert " : "");
1568
+                        cli_dbgmsg("  ==============RAW==============\n");
1569
+                        cli_dbgmsg("  raw_subject: %s\n", raw_subject);
1570
+                        cli_dbgmsg("  raw_serial: %s\n", raw_serial);
1571
+                        cli_dbgmsg("  raw_issuer: %s\n", raw_issuer);
1572 1572
 
1573 1573
                         x509 = x509->next;
1574 1574
                     }
... ...
@@ -141,7 +141,8 @@ fc_error_t fc_initialize(fc_config *fcConfig)
141 141
     logg_time    = (fcConfig->logFlags & FC_CONFIG_LOG_TIME) ? 1 : 0;
142 142
     logg_rotate  = (fcConfig->logFlags & FC_CONFIG_LOG_ROTATE) ? 1 : 0;
143 143
     logg_size    = fcConfig->maxLogSize;
144
-    if (NULL != fcConfig->logFile) {
144
+    /* Set a log file if requested, and is not already set */
145
+    if ((NULL == logg_file) && (NULL != fcConfig->logFile)) {
145 146
         logg_file = cli_strdup(fcConfig->logFile);
146 147
         if (0 != logg("#--------------------------------------\n")) {
147 148
             mprintf("!Problem with internal logger (UpdateLogFile = %s).\n", logg_file);
... ...
@@ -151,10 +152,10 @@ fc_error_t fc_initialize(fc_config *fcConfig)
151 151
     }
152 152
 
153 153
 #if defined(USE_SYSLOG) && !defined(C_AIX)
154
-    /* Initialize syslog if available and requested */
154
+    /* Initialize syslog if available and requested, and is not already set */
155 155
     if (fcConfig->logFlags & FC_CONFIG_LOG_SYSLOG) {
156 156
         int logFacility = LOG_LOCAL6;
157
-        if ((NULL != fcConfig->logFacility) && (-1 == (logFacility = logg_facility(fcConfig->logFacility)))) {
157
+        if ((0 == logg_syslog) && (NULL != fcConfig->logFacility) && (-1 == (logFacility = logg_facility(fcConfig->logFacility)))) {
158 158
             mprintf("!LogFacility: %s: No such facility.\n", fcConfig->logFacility);
159 159
             status = FC_ELOGGING;
160 160
             goto done;
... ...
@@ -232,7 +232,7 @@ static int rename_logg(STATBUF *sb)
232 232
     }
233 233
 
234 234
     t = time(NULL);
235
-    
235
+
236 236
 #ifdef _WIN32
237 237
     if (0 != localtime_s(&t, &tmp)) {
238 238
 #else
... ...
@@ -477,19 +477,19 @@ void mprintf(const char *str, ...)
477 477
     fd = stdout;
478 478
 
479 479
     /* legend:
480
- * ! - error
481
- * @ - error with logging
482
- * ...
483
- */
480
+     * ! - error
481
+     * @ - error with logging
482
+     * ...
483
+     */
484 484
 
485 485
     /*
486
- *             ERROR    WARNING    STANDARD
487
- * normal      stderr   stderr     stdout
488
- * 
489
- * verbose     stderr   stderr     stdout
490
- * 
491
- * quiet       stderr     no         no
492
- */
486
+     *             ERROR    WARNING    STANDARD
487
+     * normal      stderr   stderr     stdout
488
+     *
489
+     * verbose     stderr   stderr     stdout
490
+     *
491
+     * quiet       stderr     no         no
492
+     */
493 493
 
494 494
     ARGLEN(args, str, len);
495 495
     if (len <= sizeof(buffer)) {
... ...
@@ -3508,7 +3508,7 @@ static void help(void)
3508 3508
     mprintf("    --version              -V              Print version number and exit\n");
3509 3509
     mprintf("    --quiet                                Be quiet, output only error messages\n");
3510 3510
     mprintf("    --debug                                Enable debug messages\n");
3511
-    mprintf("    --stdout                               Write to stdout instead of stderr\n");
3511
+    mprintf("    --stdout                               Write to stdout instead of stderr. Does not affect 'debug' messages.\n");
3512 3512
     mprintf("    --hex-dump                             Convert data from stdin to a hex\n");
3513 3513
     mprintf("                                           string and print it on stdout\n");
3514 3514
     mprintf("    --md5 [FILES]                          Generate MD5 checksum from stdin\n");