Browse code

clamdscan: add support for --reload (bb#1098)

git-svn: trunk@4185

Tomasz Kojm authored on 2008/09/18 17:17:47
Showing 6 changed files
... ...
@@ -1,3 +1,8 @@
1
+Thu Sep 18 10:25:07 CEST 2008 (tk)
2
+----------------------------------
3
+  * clamdscan: add support for --reload (bb#1098)
4
+	       Thanks to Jeffrey Thomas Peckham
5
+
1 6
 Wed Sep 17 10:59:48 CEST 2008 (tk)
2 7
 ----------------------------------
3 8
   * libclamav/mbox.c: mail-follow-urls was not RFC compliant (bb#1192)
... ...
@@ -51,6 +51,15 @@ static void print_server_version(const struct optstruct *opt)
51 51
     }
52 52
 }
53 53
 
54
+static int reload_server_database(const struct optstruct *opt)
55
+{
56
+    if(reload_clamd_database(opt)) {
57
+	logg("!Clamd did not reload the database\n");
58
+	return 2;
59
+    }
60
+    return 0;
61
+}
62
+
54 63
 int main(int argc, char **argv)
55 64
 {
56 65
 	int ds, dms, ret, infected;
... ...
@@ -61,7 +70,8 @@ int main(int argc, char **argv)
61 61
 	const char *clamdscan_accepted[] = { "help", "version", "verbose", "quiet",
62 62
 				  "stdout", "log", "move", "copy", "remove",
63 63
 				  "config-file", "no-summary",  "fdpass",
64
-				  "disable-summary", "multiscan", NULL };
64
+				  "disable-summary", "multiscan", "reload",
65
+				  NULL };
65 66
 
66 67
 
67 68
     opt = opt_parse(argc, argv, clamscan_shortopt, clamscan_longopt, clamdscan_accepted);
... ...
@@ -108,6 +118,13 @@ int main(int argc, char **argv)
108 108
 	logg_file = NULL;
109 109
 
110 110
 
111
+    if(opt_check(opt, "reload")) {
112
+	ret = reload_server_database(opt);
113
+	opt_free(opt);
114
+	logg_close();
115
+	exit(ret);
116
+    }
117
+
111 118
     time(&starttime);
112 119
     /* ctime() does \n, but I need it once more */
113 120
 
... ...
@@ -133,6 +150,7 @@ int main(int argc, char **argv)
133 133
 	logg("Time: %d.%3.3d sec (%d m %d s)\n", ds, dms/1000, ds/60, ds%60);
134 134
     }
135 135
 
136
+    logg_close();
136 137
     opt_free(opt);
137 138
     exit(ret);
138 139
 }
... ...
@@ -160,6 +178,7 @@ void help(void)
160 160
     mprintf("    --multiscan           -m           Force MULTISCAN mode\n");
161 161
     mprintf("    --infected            -i           Only print infected files\n");
162 162
     mprintf("    --no-summary                       Disable summary at end of scanning\n");
163
+    mprintf("    --reload                           Request clamd to reload virus database\n");
163 164
     mprintf("    --fdpass                           pass filedescriptor to clamd (useful if clamd is running as a different user)\n");
164 165
     mprintf("\n");
165 166
 
... ...
@@ -360,6 +360,7 @@ int get_clamd_version(const struct optstruct *opt)
360 360
 
361 361
     if(write(sockd, "VERSION", 7) <= 0) {
362 362
 	logg("^Can't write to the socket.\n");
363
+	close(sockd);
363 364
 	return 2;
364 365
     }
365 366
 
... ...
@@ -372,6 +373,33 @@ int get_clamd_version(const struct optstruct *opt)
372 372
     return 0;
373 373
 }
374 374
 
375
+int reload_clamd_database(const struct optstruct *opt)
376
+{
377
+	char buff[64];
378
+	int bread, sockd;
379
+
380
+
381
+    if((sockd = dconnect(opt, NULL)) < 0)
382
+	return 2;
383
+
384
+    if(write(sockd, "RELOAD", 6) <= 0) {
385
+	logg("!Can't write to the socket.\n");
386
+	close(sockd);
387
+	return 2;
388
+    }
389
+
390
+    bread = read(sockd, buff, sizeof(buff) - 1);
391
+    buff[bread] = '\0';
392
+    if(strncmp(buff, "RELOADING", 9)) {
393
+	logg("!Incorrect reply from clamd\n");
394
+	close(sockd);
395
+	return 2;
396
+    }
397
+
398
+    close(sockd);
399
+    return 0;
400
+}
401
+
375 402
 int client(const struct optstruct *opt, int *infected)
376 403
 {
377 404
 	char cwd[PATH_MAX+1], *fullpath;
... ...
@@ -23,5 +23,6 @@
23 23
 
24 24
 int client(const struct optstruct *opt, int *infected);
25 25
 int get_clamd_version(const struct optstruct *opt);
26
+int reload_clamd_database(const struct optstruct *opt);
26 27
 
27 28
 #endif
... ...
@@ -37,6 +37,7 @@ static struct option clamscan_longopt[] = {
37 37
     {"tempdir", 1, 0, 0},
38 38
     {"leave-temps", 0, 0, 0},
39 39
     {"config-file", 1, 0, 0},	    /* clamdscan */
40
+    {"reload", 0, 0, 0},           /* clamdscan */
40 41
     {"multiscan", 0, 0, 'm'},
41 42
     {"database", 1, 0, 'd'},
42 43
     {"force", 0, 0, 0},
... ...
@@ -45,6 +45,9 @@ Move infected files into DIRECTORY.
45 45
 \fB\-\-no\-summary\fR
46 46
 Do not display summary at the end of scanning.
47 47
 .TP
48
+\fB\-\-reload\fR
49
+Request clamd to reload virus database.
50
+.TP
48 51
 \fB\-\-fdpass\fR
49 52
 Pass the file descriptor permissions clamd. This is useful if clamd is running as a different user as it is faster than streaming the file to clamd.
50 53
 Only available if connected to clamd through local(unix) sockets and scanning stdin.