Browse code

Add support for setting the HostID in freshclam

Shawn Webb authored on 2014/01/16 02:27:17
Showing 3 changed files
... ...
@@ -68,8 +68,10 @@ static short foreground = 1;
68 68
 char updtmpdir[512], dbdir[512];
69 69
 int sigchld_wait = 1;
70 70
 const char *pidfile = NULL;
71
+char hostid[37];
71 72
 
72 73
 void submit_host_info(struct optstruct *opts);
74
+char *get_hostid(void *cbdata);
73 75
 
74 76
 static void
75 77
 sighandler (int sig)
... ...
@@ -333,6 +335,22 @@ main (int argc, char **argv)
333 333
         return 0;
334 334
     }
335 335
 
336
+    /* Stats/intelligence gathering  */
337
+    if (optget(opts, "stats-host-id")->enabled) {
338
+        char *p = optget(opts, "stats-host-id")->strarg;
339
+
340
+        if (strcmp(p, "default")) {
341
+            if (strlen(p) > 36) {
342
+                logg("!Invalid HostID\n");
343
+                optfree(opts);
344
+                return FCE_INIT;
345
+            }
346
+
347
+            strcpy(hostid, p);
348
+        } else {
349
+            strcpy(hostid, "default");
350
+        }
351
+    }
336 352
     submit_host_info(opts);
337 353
 
338 354
     if (optget (opts, "HTTPProxyPassword")->enabled)
... ...
@@ -757,5 +775,44 @@ void submit_host_info(struct optstruct *opts)
757 757
 
758 758
     sprintf(intel->host_info, "%s %s", TARGET_OS_TYPE, TARGET_ARCH_TYPE);
759 759
 
760
+    if (!strcmp(hostid, "none"))
761
+        cl_engine_set_clcb_stats_get_hostid(engine, NULL);
762
+    else if (strcmp(hostid, "default"))
763
+        cl_engine_set_clcb_stats_get_hostid(engine, get_hostid);
764
+
760 765
     cl_engine_free(engine);
761 766
 }
767
+
768
+int is_valid_hostid(void)
769
+{
770
+    int count, i;
771
+
772
+    if (strlen(hostid) != 36)
773
+        return 0;
774
+
775
+    count=0;
776
+    for (i=0; i < 36; i++)
777
+        if (hostid[i] == '-')
778
+            count++;
779
+
780
+    if (count != 4)
781
+        return 0;
782
+
783
+    if (hostid[8] != '-' || hostid[13] != '-' || hostid[18] != '-' || hostid[23] != '-')
784
+        return 0;
785
+
786
+    return 1;
787
+}
788
+
789
+char *get_hostid(void *cbdata)
790
+{
791
+    if (!strcmp(hostid, "none"))
792
+        return NULL;
793
+
794
+    if (!is_valid_hostid())
795
+        return strdup(STATS_ANON_UUID);
796
+
797
+    logg("HostID is valid: %s\n", hostid);
798
+
799
+    return strdup(hostid);
800
+}
... ...
@@ -214,6 +214,14 @@ void clamav_stats_submit(struct cl_engine *engine, void *cbdata)
214 214
     if (!(intel) || !(engine))
215 215
         return;
216 216
 
217
+    if (!(engine->cb_stats_get_hostid)) {
218
+        /* Submitting stats is disabled due to HostID being turned off */
219
+        if ((engine->cb_stats_flush))
220
+            engine->cb_stats_flush(engine, cbdata);
221
+
222
+        return;
223
+    }
224
+
217 225
 #ifdef CL_THREAD_SAFE
218 226
     err = pthread_mutex_lock(&(intel->mutex));
219 227
     if (err) {
... ...
@@ -179,6 +179,7 @@ const struct clam_option __clam_options[] = {
179 179
 
180 180
     /* config file/cmdline options */
181 181
     { "LogFile", "log", 'l', TYPE_STRING, NULL, -1, NULL, 0, OPT_CLAMD | OPT_MILTER | OPT_CLAMSCAN | OPT_CLAMDSCAN, "Save all reports to a log file.", "/tmp/clamav.log" },
182
+    { "StatsHostID", "stats-host-id", 0, TYPE_STRING, NULL, -1, NULL, 0, OPT_FRESHCLAM | OPT_CLAMD | OPT_CLAMSCAN, "HostID in the form of an UUID to use when submitting statistical information. See the clamscan manpage for more information.", "default" },
182 183
 
183 184
     { "LogFileUnlock", NULL, 0, TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMD | OPT_MILTER, "By default the log file is locked for writing and only a single\ndaemon process can write to it. This option disables the lock.", "yes" },
184 185