1 /* $Cambridge: exim/src/src/malware.c,v 1.7 2005/02/17 11:58:26 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
10 /* Code for calling virus (malware) scanners. Called from acl.c. */
13 #ifdef WITH_CONTENT_SCAN
15 /* declaration of private routines */
16 int mksd_scan_packed(int sock);
18 /* SHUT_WR seems to be undefined on Unixware? */
23 #define DRWEBD_SCAN_CMD (1) /* scan file, buffer or diskfile */
24 #define DRWEBD_RETURN_VIRUSES (1<<0) /* ask daemon return to us viruses names from report */
25 #define DRWEBD_IS_MAIL (1<<19) /* say to daemon that format is "archive MAIL" */
27 #define DERR_READ_ERR (1<<0) /* read error */
28 #define DERR_NOMEMORY (1<<2) /* no memory */
29 #define DERR_TIMEOUT (1<<9) /* scan timeout has run out */
30 #define DERR_BAD_CALL (1<<15) /* wrong command */
32 /* Routine to check whether a system is big- or litte-endian.
33 Ripped from http://www.faqs.org/faqs/graphics/fileformats-faq/part4/section-7.html
34 Needed for proper kavdaemon implementation. Sigh. */
35 #define BIG_MY_ENDIAN 0
36 #define LITTLE_MY_ENDIAN 1
37 int test_byte_order(void);
38 int test_byte_order() {
39 short int word = 0x0001;
40 char *byte = (char *) &word;
41 return(byte[0] ? LITTLE_MY_ENDIAN : BIG_MY_ENDIAN);
44 uschar malware_name_buffer[256];
47 int malware(uschar **listptr) {
49 uschar *list = *listptr;
50 uschar *av_scanner_work = av_scanner;
52 uschar scanner_name_buffer[16];
53 uschar *malware_regex;
54 uschar malware_regex_buffer[64];
55 uschar malware_regex_default[] = ".+";
56 unsigned long mbox_size;
62 /* make sure the eml mbox file is spooled up */
63 mbox_file = spool_mbox(&mbox_size);
64 if (mbox_file == NULL) {
65 /* error while spooling */
66 log_write(0, LOG_MAIN|LOG_PANIC,
67 "malware acl condition: error while creating mbox spool file");
70 /* none of our current scanners need the mbox
71 file as a stream, so we can close it right away */
74 /* extract the malware regex to match against from the option list */
75 if ((malware_regex = string_nextinlist(&list, &sep,
77 sizeof(malware_regex_buffer))) != NULL) {
79 /* parse 1st option */
80 if ( (strcmpic(malware_regex,US"false") == 0) ||
81 (Ustrcmp(malware_regex,"0") == 0) ) {
82 /* explicitly no matching */
86 /* special cases (match anything except empty) */
87 if ( (strcmpic(malware_regex,US"true") == 0) ||
88 (Ustrcmp(malware_regex,"*") == 0) ||
89 (Ustrcmp(malware_regex,"1") == 0) ) {
90 malware_regex = malware_regex_default;
94 /* empty means "don't match anything" */
98 /* Reset sep that is set by previous string_nextinlist() call */
101 /* compile the regex, see if it works */
102 re = pcre_compile(CS malware_regex, PCRE_COPT, (const char **)&rerror, &roffset, NULL);
104 log_write(0, LOG_MAIN|LOG_PANIC,
105 "malware acl condition: regular expression error in '%s': %s at offset %d", malware_regex, rerror, roffset);
109 /* if av_scanner starts with a dollar, expand it first */
110 if (*av_scanner == '$') {
111 av_scanner_work = expand_string(av_scanner);
112 if (av_scanner_work == NULL) {
113 log_write(0, LOG_MAIN|LOG_PANIC,
114 "malware acl condition: av_scanner starts with $, but expansion failed: %s", expand_string_message);
118 debug_printf("Expanded av_scanner global: %s\n", av_scanner_work);
119 /* disable result caching in this case */
125 /* Do not scan twice. */
126 if (malware_ok == 0) {
128 /* find the scanner type from the av_scanner option */
129 if ((scanner_name = string_nextinlist(&av_scanner_work, &sep,
131 sizeof(scanner_name_buffer))) == NULL) {
132 /* no scanner given */
133 log_write(0, LOG_MAIN|LOG_PANIC,
134 "malware acl condition: av_scanner configuration variable is empty");
138 /* "drweb" scanner type ----------------------------------------------- */
139 /* v0.1 - added support for tcp sockets */
140 /* v0.0 - initial release -- support for unix sockets */
141 if (strcmpic(scanner_name,US"drweb") == 0) {
142 uschar *drweb_options;
143 uschar drweb_options_buffer[1024];
144 uschar drweb_options_default[] = "/usr/local/drweb/run/drwebd.sock";
145 struct sockaddr_un server;
146 int sock, result, ovector[30];
147 unsigned int port, fsize;
148 uschar tmpbuf[1024], *drweb_fbuf;
149 uschar scanrequest[1024];
150 uschar drweb_match_string[128];
151 int drweb_rc, drweb_cmd, drweb_flags = 0x0000, drweb_fd,
152 drweb_vnum, drweb_slen, drweb_fin = 0x0000;
154 uschar hostname[256];
159 if ((drweb_options = string_nextinlist(&av_scanner_work, &sep,
160 drweb_options_buffer, sizeof(drweb_options_buffer))) == NULL) {
161 /* no options supplied, use default options */
162 drweb_options = drweb_options_default;
165 if (*drweb_options != '/') {
167 /* extract host and port part */
168 if( sscanf(CS drweb_options, "%s %u", hostname, &port) != 2 ) {
169 log_write(0, LOG_MAIN|LOG_PANIC,
170 "malware acl condition: drweb: invalid socket '%s'", drweb_options);
174 /* Lookup the host */
175 if((he = gethostbyname(CS hostname)) == 0) {
176 log_write(0, LOG_MAIN|LOG_PANIC,
177 "malware acl condition: drweb: failed to lookup host '%s'", hostname);
181 in = *(struct in_addr *) he->h_addr_list[0];
183 /* Open the drwebd TCP socket */
184 if ( (sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
185 log_write(0, LOG_MAIN|LOG_PANIC,
186 "malware acl condition: drweb: unable to acquire socket (%s)",
191 if (ip_connect(sock, AF_INET, (uschar*)inet_ntoa(in), port, 5) < 0) {
193 log_write(0, LOG_MAIN|LOG_PANIC,
194 "malware acl condition: drweb: connection to %s, port %u failed (%s)",
195 inet_ntoa(in), port, strerror(errno));
199 /* prepare variables */
200 drweb_cmd = htonl(DRWEBD_SCAN_CMD);
201 drweb_flags = htonl(DRWEBD_RETURN_VIRUSES | DRWEBD_IS_MAIL);
202 snprintf(CS scanrequest, 1024,CS"%s/scan/%s/%s.eml",
203 spool_directory, message_id, message_id);
206 drweb_fd = open(CS scanrequest, O_RDONLY);
207 if (drweb_fd == -1) {
209 log_write(0, LOG_MAIN|LOG_PANIC,
210 "malware acl condition: drweb: can't open spool file %s: %s",
211 scanrequest, strerror(errno));
214 fsize = lseek(drweb_fd, 0, SEEK_END);
218 log_write(0, LOG_MAIN|LOG_PANIC,
219 "malware acl condition: drweb: can't seek spool file %s: %s",
220 scanrequest, strerror(errno));
223 drweb_slen = htonl(fsize);
224 lseek(drweb_fd, 0, SEEK_SET);
226 /* send scan request */
227 if ((send(sock, &drweb_cmd, sizeof(drweb_cmd), 0) < 0) ||
228 (send(sock, &drweb_flags, sizeof(drweb_flags), 0) < 0) ||
229 (send(sock, &drweb_fin, sizeof(drweb_fin), 0) < 0) ||
230 (send(sock, &drweb_slen, sizeof(drweb_slen), 0) < 0)) {
233 log_write(0, LOG_MAIN|LOG_PANIC,
234 "malware acl condition: drweb: unable to send commands to socket (%s)", drweb_options);
238 drweb_fbuf = (uschar *) malloc (fsize);
242 log_write(0, LOG_MAIN|LOG_PANIC,
243 "malware acl condition: drweb: unable to allocate memory %u for file (%s)",
248 result = read (drweb_fd, drweb_fbuf, fsize);
253 log_write(0, LOG_MAIN|LOG_PANIC,
254 "malware acl condition: drweb: can't read spool file %s: %s",
255 scanrequest, strerror(errno));
260 /* send file body to socket */
261 if (send(sock, drweb_fbuf, fsize, 0) < 0) {
264 log_write(0, LOG_MAIN|LOG_PANIC,
265 "malware acl condition: drweb: unable to send file body to socket (%s)", drweb_options);
271 /* open the drwebd UNIX socket */
272 sock = socket(AF_UNIX, SOCK_STREAM, 0);
274 log_write(0, LOG_MAIN|LOG_PANIC,
275 "malware acl condition: drweb: can't open UNIX socket");
278 server.sun_family = AF_UNIX;
279 Ustrcpy(server.sun_path, drweb_options);
280 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
282 log_write(0, LOG_MAIN|LOG_PANIC,
283 "malware acl condition: drweb: unable to connect to socket (%s). errno=%d", drweb_options, errno);
287 /* prepare variables */
288 drweb_cmd = htonl(DRWEBD_SCAN_CMD);
289 drweb_flags = htonl(DRWEBD_RETURN_VIRUSES | DRWEBD_IS_MAIL);
290 snprintf(CS scanrequest, 1024,CS"%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
291 drweb_slen = htonl(Ustrlen(scanrequest));
293 /* send scan request */
294 if ((send(sock, &drweb_cmd, sizeof(drweb_cmd), 0) < 0) ||
295 (send(sock, &drweb_flags, sizeof(drweb_flags), 0) < 0) ||
296 (send(sock, &drweb_slen, sizeof(drweb_slen), 0) < 0) ||
297 (send(sock, scanrequest, Ustrlen(scanrequest), 0) < 0) ||
298 (send(sock, &drweb_fin, sizeof(drweb_fin), 0) < 0)) {
300 log_write(0, LOG_MAIN|LOG_PANIC,
301 "malware acl condition: drweb: unable to send commands to socket (%s)", drweb_options);
306 /* wait for result */
307 if ((bread = recv(sock, &drweb_rc, sizeof(drweb_rc), 0) != sizeof(drweb_rc))) {
309 log_write(0, LOG_MAIN|LOG_PANIC,
310 "malware acl condition: drweb: unable to read return code");
313 drweb_rc = ntohl(drweb_rc);
315 if ((bread = recv(sock, &drweb_vnum, sizeof(drweb_vnum), 0) != sizeof(drweb_vnum))) {
317 log_write(0, LOG_MAIN|LOG_PANIC,
318 "malware acl condition: drweb: unable to read the number of viruses");
321 drweb_vnum = ntohl(drweb_vnum);
323 /* "virus(es) found" if virus number is > 0 */
327 uschar pre_malware_nb[256];
329 malware_name = malware_name_buffer;
331 /* setup default virus name */
332 Ustrcpy(malware_name_buffer,"unknown");
334 /* read and concatenate virus names into one string */
335 for (i=0;i<drweb_vnum;i++)
337 /* read the size of report */
338 if ((bread = recv(sock, &drweb_slen, sizeof(drweb_slen), 0) != sizeof(drweb_slen))) {
340 log_write(0, LOG_MAIN|LOG_PANIC,
341 "malware acl condition: drweb: cannot read report size");
344 drweb_slen = ntohl(drweb_slen);
346 /* read report body */
347 if ((bread = recv(sock, tmpbuf, drweb_slen, 0)) != drweb_slen) {
349 log_write(0, LOG_MAIN|LOG_PANIC,
350 "malware acl condition: drweb: cannot read report string");
353 tmpbuf[drweb_slen] = '\0';
355 /* set up match regex, depends on retcode */
356 Ustrcpy(drweb_match_string, "infected\\swith\\s*(.+?)$");
358 drweb_re = pcre_compile( CS drweb_match_string,
360 (const char **)&rerror,
364 /* try matcher on the line, grab substring */
365 result = pcre_exec(drweb_re, NULL, CS tmpbuf, Ustrlen(tmpbuf), 0, 0, ovector, 30);
367 pcre_copy_substring(CS tmpbuf, ovector, result, 1, CS pre_malware_nb, 255);
369 /* the first name we just copy to malware_name */
371 Ustrcpy(CS malware_name_buffer, CS pre_malware_nb);
373 /* concatenate each new virus name to previous */
374 int slen = Ustrlen(malware_name_buffer);
375 if (slen < (slen+Ustrlen(pre_malware_nb))) {
376 Ustrcat(malware_name_buffer, "/");
377 Ustrcat(malware_name_buffer, pre_malware_nb);
383 char *drweb_s = NULL;
385 if (drweb_rc & DERR_READ_ERR) drweb_s = "read error";
386 if (drweb_rc & DERR_NOMEMORY) drweb_s = "no memory";
387 if (drweb_rc & DERR_TIMEOUT) drweb_s = "timeout";
388 if (drweb_rc & DERR_BAD_CALL) drweb_s = "wrong command";
389 /* retcodes DERR_SYMLINK, DERR_NO_REGFILE, DERR_SKIPPED.
390 * DERR_TOO_BIG, DERR_TOO_COMPRESSED, DERR_SPAM,
391 * DERR_CRC_ERROR, DERR_READSOCKET, DERR_WRITE_ERR
392 * and others are ignored */
394 log_write(0, LOG_MAIN|LOG_PANIC,
395 "malware acl condition: drweb: drweb daemon retcode 0x%x (%s)", drweb_rc, drweb_s);
404 /* ----------------------------------------------------------------------- */
405 else if (strcmpic(scanner_name,US"aveserver") == 0) {
407 uschar kav_options_buffer[1024];
408 uschar kav_options_default[] = "/var/run/aveserver";
410 struct sockaddr_un server;
413 if ((kav_options = string_nextinlist(&av_scanner_work, &sep,
415 sizeof(kav_options_buffer))) == NULL) {
416 /* no options supplied, use default options */
417 kav_options = kav_options_default;
420 /* open the aveserver socket */
421 sock = socket(AF_UNIX, SOCK_STREAM, 0);
423 log_write(0, LOG_MAIN|LOG_PANIC,
424 "malware acl condition: can't open UNIX socket.");
427 server.sun_family = AF_UNIX;
428 Ustrcpy(server.sun_path, kav_options);
429 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
431 log_write(0, LOG_MAIN|LOG_PANIC,
432 "malware acl condition: unable to connect to aveserver UNIX socket (%s). errno=%d", kav_options, errno);
436 /* read aveserver's greeting and see if it is ready (2xx greeting) */
437 recv_line(sock, buf, 32768);
440 /* aveserver is having problems */
442 log_write(0, LOG_MAIN|LOG_PANIC,
443 "malware acl condition: aveserver is unavailable (Responded: %s).", ((buf[0] != 0) ? buf : (uschar *)"nothing") );
447 /* prepare our command */
448 snprintf(CS buf, 32768, "SCAN bPQRSTUW %s/scan/%s/%s.eml\r\n", spool_directory, message_id, message_id);
451 if (send(sock, buf, Ustrlen(buf), 0) < 0) {
453 log_write(0, LOG_MAIN|LOG_PANIC,
454 "malware acl condition: unable to write to aveserver UNIX socket (%s)", kav_options);
459 /* read response lines, find malware name and final response */
460 while (recv_line(sock, buf, 32768) > 0) {
461 debug_printf("aveserver: %s\n", buf);
462 if (buf[0] == '2') break;
463 if (Ustrncmp(buf,"322",3) == 0) {
464 uschar *p = Ustrchr(&buf[4],' ');
466 Ustrcpy(malware_name_buffer,&buf[4]);
467 malware_name = malware_name_buffer;
473 /* "fsecure" scanner type ------------------------------------------------- */
474 else if (strcmpic(scanner_name,US"fsecure") == 0) {
475 uschar *fsecure_options;
476 uschar fsecure_options_buffer[1024];
477 uschar fsecure_options_default[] = "/var/run/.fsav";
478 struct sockaddr_un server;
479 int sock, i, j, bread = 0;
480 uschar file_name[1024];
481 uschar av_buffer[1024];
483 static uschar *cmdoptions[] = { US"CONFIGURE\tARCHIVE\t1\n",
484 US"CONFIGURE\tTIMEOUT\t0\n",
485 US"CONFIGURE\tMAXARCH\t5\n",
486 US"CONFIGURE\tMIME\t1\n" };
489 if ((fsecure_options = string_nextinlist(&av_scanner_work, &sep,
490 fsecure_options_buffer,
491 sizeof(fsecure_options_buffer))) == NULL) {
492 /* no options supplied, use default options */
493 fsecure_options = fsecure_options_default;
496 /* open the fsecure socket */
497 sock = socket(AF_UNIX, SOCK_STREAM, 0);
499 log_write(0, LOG_MAIN|LOG_PANIC,
500 "malware acl condition: unable to open fsecure socket %s (%s)",
501 fsecure_options, strerror(errno));
504 server.sun_family = AF_UNIX;
505 Ustrcpy(server.sun_path, fsecure_options);
506 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
508 log_write(0, LOG_MAIN|LOG_PANIC,
509 "malware acl condition: unable to connect to fsecure socket %s (%s)",
510 fsecure_options, strerror(errno));
515 memset(av_buffer, 0, sizeof(av_buffer));
516 for (i=0; i != 4; i++) {
517 /* debug_printf("send option \"%s\"",cmdoptions[i]); */
518 if (write(sock, cmdoptions[i], Ustrlen(cmdoptions[i])) < 0) {
520 log_write(0, LOG_MAIN|LOG_PANIC,
521 "malware acl condition: unable to write fsecure option %d to %s (%s)",
522 i, fsecure_options, strerror(errno));
526 bread = read(sock, av_buffer, sizeof(av_buffer));
527 if (bread >0) av_buffer[bread]='\0';
530 log_write(0, LOG_MAIN|LOG_PANIC,
531 "malware acl condition: unable to read fsecure answer %d (%s)", i, strerror(errno));
534 for (j=0;j<bread;j++) if((av_buffer[j]=='\r')||(av_buffer[j]=='\n')) av_buffer[j] ='@';
535 /* debug_printf("read answer %d read=%d \"%s\"\n", i, bread, av_buffer ); */
536 /* while (Ustrstr(av_buffer, "OK\tServer configured.@") == NULL); */
539 /* pass the mailfile to fsecure */
540 snprintf(CS file_name,1024,"SCAN\t%s/scan/%s/%s.eml\n", spool_directory, message_id, message_id);
541 /* debug_printf("send scan %s",file_name); */
542 if (write(sock, file_name, Ustrlen(file_name)) < 0) {
544 log_write(0, LOG_MAIN|LOG_PANIC,
545 "malware acl condition: unable to write fsecure scan to %s (%s)",
546 fsecure_options, strerror(errno));
551 /* todo also SUSPICION\t */
552 fs_inf = pcre_compile("\\S{0,5}INFECTED\\t[^\\t]*\\t([^\\t]+)\\t\\S*$", PCRE_COPT, (const char **)&rerror, &roffset, NULL);
554 /* read report, linewise */
558 memset(av_buffer, 0, sizeof(av_buffer));
560 bread=read(sock, &av_buffer[i], 1);
563 log_write(0, LOG_MAIN|LOG_PANIC,
564 "malware acl condition: unable to read fsecure result (%s)", strerror(errno));
569 while ((i < sizeof(av_buffer)-1 ) && (av_buffer[i-1] != '\n'));
570 av_buffer[i-1] = '\0';
571 /* debug_printf("got line \"%s\"\n",av_buffer); */
573 /* Really search for virus again? */
574 if (malware_name == NULL) {
575 /* try matcher on the line, grab substring */
576 i = pcre_exec(fs_inf, NULL, CS av_buffer, Ustrlen(av_buffer), 0, 0, ovector, 30);
579 pcre_copy_substring(CS av_buffer, ovector, i, 1, CS malware_name_buffer, 255);
580 malware_name = malware_name_buffer;
584 while (Ustrstr(av_buffer, "OK\tScan ok.") == NULL);
587 /* ----------------------------------------------------------------------- */
589 /* "kavdaemon" scanner type ------------------------------------------------ */
590 else if (strcmpic(scanner_name,US"kavdaemon") == 0) {
592 uschar kav_options_buffer[1024];
593 uschar kav_options_default[] = "/var/run/AvpCtl";
594 struct sockaddr_un server;
598 uschar scanrequest[1024];
599 uschar kav_match_string[128];
601 unsigned long kav_reportlen, bread;
604 if ((kav_options = string_nextinlist(&av_scanner_work, &sep,
606 sizeof(kav_options_buffer))) == NULL) {
607 /* no options supplied, use default options */
608 kav_options = kav_options_default;
611 /* open the kavdaemon socket */
612 sock = socket(AF_UNIX, SOCK_STREAM, 0);
614 log_write(0, LOG_MAIN|LOG_PANIC,
615 "malware acl condition: can't open UNIX socket.");
618 server.sun_family = AF_UNIX;
619 Ustrcpy(server.sun_path, kav_options);
620 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
622 log_write(0, LOG_MAIN|LOG_PANIC,
623 "malware acl condition: unable to connect to kavdaemon UNIX socket (%s). errno=%d", kav_options, errno);
627 /* get current date and time, build scan request */
629 strftime(CS tmpbuf, sizeof(tmpbuf), "<0>%d %b %H:%M:%S:%%s/scan/%%s", localtime(&t));
630 snprintf(CS scanrequest, 1024,CS tmpbuf, spool_directory, message_id);
632 /* send scan request */
633 if (send(sock, scanrequest, Ustrlen(scanrequest)+1, 0) < 0) {
635 log_write(0, LOG_MAIN|LOG_PANIC,
636 "malware acl condition: unable to write to kavdaemon UNIX socket (%s)", kav_options);
640 /* wait for result */
641 if ((bread = recv(sock, tmpbuf, 2, 0) != 2)) {
643 log_write(0, LOG_MAIN|LOG_PANIC,
644 "malware acl condition: unable to read 2 bytes from kavdaemon socket.");
648 /* get errorcode from one nibble */
649 if (test_byte_order() == LITTLE_MY_ENDIAN) {
650 kav_rc = tmpbuf[0] & 0x0F;
653 kav_rc = tmpbuf[1] & 0x0F;
656 /* improper kavdaemon configuration */
657 if ( (kav_rc == 5) || (kav_rc == 6) ) {
659 log_write(0, LOG_MAIN|LOG_PANIC,
660 "malware acl condition: please reconfigure kavdaemon to NOT disinfect or remove infected files.");
666 log_write(0, LOG_MAIN|LOG_PANIC,
667 "malware acl condition: kavdaemon reported 'scanning not completed' (code 1).");
673 log_write(0, LOG_MAIN|LOG_PANIC,
674 "malware acl condition: kavdaemon reported 'kavdaemon damaged' (code 7).");
678 /* code 8 is not handled, since it is ambigous. It appears mostly on
679 bounces where part of a file has been cut off */
681 /* "virus found" return codes (2-4) */
682 if ((kav_rc > 1) && (kav_rc < 5)) {
685 /* setup default virus name */
686 Ustrcpy(malware_name_buffer,"unknown");
687 malware_name = malware_name_buffer;
689 if (test_byte_order() == LITTLE_MY_ENDIAN) {
690 report_flag = tmpbuf[1];
693 report_flag = tmpbuf[0];
696 /* read the report, if available */
697 if( report_flag == 1 ) {
698 /* read report size */
699 if ((bread = recv(sock, &kav_reportlen, 4, 0)) != 4) {
701 log_write(0, LOG_MAIN|LOG_PANIC,
702 "malware acl condition: cannot read report size from kavdaemon");
706 /* it's possible that avp returns av_buffer[1] == 1 but the
707 reportsize is 0 (!?) */
708 if (kav_reportlen > 0) {
709 /* set up match regex, depends on retcode */
711 Ustrcpy(kav_match_string, "suspicion:\\s*(.+?)\\s*$");
713 Ustrcpy(kav_match_string, "infected:\\s*(.+?)\\s*$");
715 kav_re = pcre_compile( CS kav_match_string,
717 (const char **)&rerror,
721 /* read report, linewise */
722 while (kav_reportlen > 0) {
727 while ( recv(sock, &tmpbuf[bread], 1, 0) == 1 ) {
729 if ( (tmpbuf[bread] == '\n') || (bread > 1021) ) break;
733 tmpbuf[bread] = '\0';
735 /* try matcher on the line, grab substring */
736 result = pcre_exec(kav_re, NULL, CS tmpbuf, Ustrlen(tmpbuf), 0, 0, ovector, 30);
738 pcre_copy_substring(CS tmpbuf, ovector, result, 1, CS malware_name_buffer, 255);
752 /* ----------------------------------------------------------------------- */
755 /* "cmdline" scanner type ------------------------------------------------ */
756 else if (strcmpic(scanner_name,US"cmdline") == 0) {
757 uschar *cmdline_scanner;
758 uschar cmdline_scanner_buffer[1024];
759 uschar *cmdline_trigger;
760 uschar cmdline_trigger_buffer[1024];
761 const pcre *cmdline_trigger_re;
762 uschar *cmdline_regex;
763 uschar cmdline_regex_buffer[1024];
764 const pcre *cmdline_regex_re;
765 uschar file_name[1024];
766 uschar commandline[1024];
767 void (*eximsigchld)(int);
768 void (*eximsigpipe)(int);
769 FILE *scanner_out = NULL;
770 FILE *scanner_record = NULL;
771 uschar linebuffer[32767];
776 /* find scanner command line */
777 if ((cmdline_scanner = string_nextinlist(&av_scanner_work, &sep,
778 cmdline_scanner_buffer,
779 sizeof(cmdline_scanner_buffer))) == NULL) {
780 /* no command line supplied */
781 log_write(0, LOG_MAIN|LOG_PANIC,
782 "malware acl condition: missing commandline specification for cmdline scanner type.");
786 /* find scanner output trigger */
787 if ((cmdline_trigger = string_nextinlist(&av_scanner_work, &sep,
788 cmdline_trigger_buffer,
789 sizeof(cmdline_trigger_buffer))) == NULL) {
790 /* no trigger regex supplied */
791 log_write(0, LOG_MAIN|LOG_PANIC,
792 "malware acl condition: missing trigger specification for cmdline scanner type.");
796 /* precompile trigger regex */
797 cmdline_trigger_re = pcre_compile(CS cmdline_trigger, PCRE_COPT, (const char **)&rerror, &roffset, NULL);
798 if (cmdline_trigger_re == NULL) {
799 log_write(0, LOG_MAIN|LOG_PANIC,
800 "malware acl condition: regular expression error in '%s': %s at offset %d", cmdline_trigger_re, rerror, roffset);
804 /* find scanner name regex */
805 if ((cmdline_regex = string_nextinlist(&av_scanner_work, &sep,
806 cmdline_regex_buffer,
807 sizeof(cmdline_regex_buffer))) == NULL) {
808 /* no name regex supplied */
809 log_write(0, LOG_MAIN|LOG_PANIC,
810 "malware acl condition: missing virus name regex specification for cmdline scanner type.");
814 /* precompile name regex */
815 cmdline_regex_re = pcre_compile(CS cmdline_regex, PCRE_COPT, (const char **)&rerror, &roffset, NULL);
816 if (cmdline_regex_re == NULL) {
817 log_write(0, LOG_MAIN|LOG_PANIC,
818 "malware acl condition: regular expression error in '%s': %s at offset %d", cmdline_regex_re, rerror, roffset);
822 /* prepare scanner call */
823 snprintf(CS file_name,1024,"%s/scan/%s", spool_directory, message_id);
824 snprintf(CS commandline,1024, CS cmdline_scanner,file_name);
825 /* redirect STDERR too */
826 Ustrcat(commandline," 2>&1");
828 /* store exims signal handlers */
829 eximsigchld = signal(SIGCHLD,SIG_DFL);
830 eximsigpipe = signal(SIGPIPE,SIG_DFL);
832 scanner_out = popen(CS commandline,"r");
833 if (scanner_out == NULL) {
834 log_write(0, LOG_MAIN|LOG_PANIC,
835 "malware acl condition: calling cmdline scanner (%s) failed: %s.", commandline, strerror(errno));
836 signal(SIGCHLD,eximsigchld);
837 signal(SIGPIPE,eximsigpipe);
841 snprintf(CS file_name,1024,"%s/scan/%s/%s_scanner_output", spool_directory, message_id, message_id);
842 scanner_record = fopen(CS file_name,"w");
844 if (scanner_record == NULL) {
845 log_write(0, LOG_MAIN|LOG_PANIC,
846 "malware acl condition: opening scanner output file (%s) failed: %s.", file_name, strerror(errno));
848 signal(SIGCHLD,eximsigchld);
849 signal(SIGPIPE,eximsigpipe);
853 /* look for trigger while recording output */
854 while(fgets(CS linebuffer,32767,scanner_out) != NULL) {
855 if ( Ustrlen(linebuffer) > fwrite(linebuffer, 1, Ustrlen(linebuffer), scanner_record) ) {
857 log_write(0, LOG_MAIN|LOG_PANIC,
858 "malware acl condition: short write on scanner output file (%s).", file_name);
860 signal(SIGCHLD,eximsigchld);
861 signal(SIGPIPE,eximsigpipe);
864 /* try trigger match */
865 if (!trigger && regex_match_and_setup(cmdline_trigger_re, linebuffer, 0, -1))
869 fclose(scanner_record);
871 signal(SIGCHLD,eximsigchld);
872 signal(SIGPIPE,eximsigpipe);
875 /* setup default virus name */
876 Ustrcpy(malware_name_buffer,"unknown");
877 malware_name = malware_name_buffer;
879 /* re-open the scanner output file, look for name match */
880 scanner_record = fopen(CS file_name,"r");
881 while(fgets(CS linebuffer,32767,scanner_record) != NULL) {
883 result = pcre_exec(cmdline_regex_re, NULL, CS linebuffer, Ustrlen(linebuffer), 0, 0, ovector, 30);
885 pcre_copy_substring(CS linebuffer, ovector, result, 1, CS malware_name_buffer, 255);
888 fclose(scanner_record);
895 /* ----------------------------------------------------------------------- */
898 /* "sophie" scanner type ------------------------------------------------- */
899 else if (strcmpic(scanner_name,US"sophie") == 0) {
900 uschar *sophie_options;
901 uschar sophie_options_buffer[1024];
902 uschar sophie_options_default[] = "/var/run/sophie";
904 struct sockaddr_un server;
906 uschar file_name[1024];
907 uschar av_buffer[1024];
909 if ((sophie_options = string_nextinlist(&av_scanner_work, &sep,
910 sophie_options_buffer,
911 sizeof(sophie_options_buffer))) == NULL) {
912 /* no options supplied, use default options */
913 sophie_options = sophie_options_default;
916 /* open the sophie socket */
917 sock = socket(AF_UNIX, SOCK_STREAM, 0);
919 log_write(0, LOG_MAIN|LOG_PANIC,
920 "malware acl condition: can't open UNIX socket.");
923 server.sun_family = AF_UNIX;
924 Ustrcpy(server.sun_path, sophie_options);
925 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
927 log_write(0, LOG_MAIN|LOG_PANIC,
928 "malware acl condition: unable to connect to sophie UNIX socket (%s). errno=%d", sophie_options, errno);
932 /* pass the scan directory to sophie */
933 snprintf(CS file_name,1024,"%s/scan/%s", spool_directory, message_id);
934 if (write(sock, file_name, Ustrlen(file_name)) < 0) {
936 log_write(0, LOG_MAIN|LOG_PANIC,
937 "malware acl condition: unable to write to sophie UNIX socket (%s)", sophie_options);
941 write(sock, "\n", 1);
943 /* wait for result */
944 memset(av_buffer, 0, sizeof(av_buffer));
945 if ((!(bread = read(sock, av_buffer, sizeof(av_buffer))) > 0)) {
947 log_write(0, LOG_MAIN|LOG_PANIC,
948 "malware acl condition: unable to read from sophie UNIX socket (%s)", sophie_options);
955 if (av_buffer[0] == '1') {
956 if (Ustrchr(av_buffer, '\n')) *Ustrchr(av_buffer, '\n') = '\0';
957 Ustrcpy(malware_name_buffer,&av_buffer[2]);
958 malware_name = malware_name_buffer;
960 else if (!strncmp(CS av_buffer, "-1", 2)) {
961 log_write(0, LOG_MAIN|LOG_PANIC,
962 "malware acl condition: malware acl condition: sophie reported error");
966 /* all ok, no virus */
970 /* ----------------------------------------------------------------------- */
973 /* "clamd" scanner type ------------------------------------------------- */
974 /* This code was contributed by David Saez */
975 else if (strcmpic(scanner_name,US"clamd") == 0) {
976 uschar *clamd_options;
977 uschar clamd_options_buffer[1024];
978 uschar clamd_options_default[] = "/tmp/clamd";
980 struct sockaddr_un server;
983 uschar file_name[1024];
984 uschar av_buffer[1024];
985 uschar hostname[256];
988 uschar *clamd_options2;
989 uschar clamd_options2_buffer[1024];
990 uschar clamd_options2_default[] = "";
991 uschar av_buffer2[1024];
993 uschar scanrequest[1024];
994 int sockData, clam_fd, result;
997 if ((clamd_options = string_nextinlist(&av_scanner_work, &sep,
998 clamd_options_buffer,
999 sizeof(clamd_options_buffer))) == NULL) {
1000 /* no options supplied, use default options */
1001 clamd_options = clamd_options_default;
1003 if ((clamd_options2 = string_nextinlist(&av_scanner_work, &sep,
1004 clamd_options2_buffer,
1005 sizeof(clamd_options2_buffer))) == NULL) {
1006 clamd_options2 = clamd_options2_default;
1009 /* socket does not start with '/' -> network socket */
1010 if (*clamd_options != '/') {
1012 /* extract host and port part */
1013 if( sscanf(CS clamd_options, "%s %u", hostname, &port) != 2 ) {
1014 log_write(0, LOG_MAIN|LOG_PANIC,
1015 "malware acl condition: clamd: invalid socket '%s'", clamd_options);
1019 /* Lookup the host */
1020 if((he = gethostbyname(CS hostname)) == 0) {
1021 log_write(0, LOG_MAIN|LOG_PANIC,
1022 "malware acl condition: clamd: failed to lookup host '%s'", hostname);
1026 in = *(struct in_addr *) he->h_addr_list[0];
1028 /* Open the ClamAV Socket */
1029 if ( (sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
1030 log_write(0, LOG_MAIN|LOG_PANIC,
1031 "malware acl condition: clamd: unable to acquire socket (%s)",
1036 if (ip_connect(sock, AF_INET, (uschar*)inet_ntoa(in), port, 5) < 0) {
1038 log_write(0, LOG_MAIN|LOG_PANIC,
1039 "malware acl condition: clamd: connection to %s, port %u failed (%s)",
1040 inet_ntoa(in), port, strerror(errno));
1044 if (strcmpic(clamd_options2,US"local") == 0) {
1046 /* Pass the string to ClamAV (7 = "SCAN \n" + \0) */
1048 snprintf(CS file_name,1024,"SCAN %s/scan/%s\n", spool_directory, message_id);
1050 if (send(sock, file_name, Ustrlen(file_name), 0) < 0) {
1052 log_write(0, LOG_MAIN|LOG_PANIC,"malware acl condition: clamd: unable to write to socket (%s)",
1058 /* Pass the string to ClamAV (7 = "STREAM\n") */
1060 if (send(sock, "STREAM\n", 7, 0) < 0) {
1062 log_write(0, LOG_MAIN|LOG_PANIC,"malware acl condition: clamd: unable to write to socket (%s)",
1066 memset(av_buffer2, 0, sizeof(av_buffer2));
1067 bread = read(sock, av_buffer2, sizeof(av_buffer2));
1070 log_write(0, LOG_MAIN|LOG_PANIC,
1071 "malware acl condition: clamd: unable to read PORT from socket (%s)",
1076 if (bread == sizeof(av_buffer)) {
1077 log_write(0, LOG_MAIN|LOG_PANIC,
1078 "malware acl condition: clamd: buffer too small");
1082 if (!(*av_buffer2)) {
1083 log_write(0, LOG_MAIN|LOG_PANIC,
1084 "malware acl condition: clamd: ClamAV returned null");
1088 av_buffer2[bread] = '\0';
1089 if( sscanf(CS av_buffer2, "PORT %u\n", &port) != 1 ) {
1090 log_write(0, LOG_MAIN|LOG_PANIC,
1091 "malware acl condition: clamd: Expected port information from clamd, got '%s'", av_buffer2);
1095 if ( (sockData = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
1096 log_write(0, LOG_MAIN|LOG_PANIC,
1097 "malware acl condition: clamd: unable to acquire socket (%s)",
1102 if (ip_connect(sockData, AF_INET, (uschar*)inet_ntoa(in), port, 5) < 0) {
1104 log_write(0, LOG_MAIN|LOG_PANIC,
1105 "malware acl condition: clamd: connection to %s, port %u failed (%s)",
1106 inet_ntoa(in), port, strerror(errno));
1110 snprintf(CS scanrequest, 1024,CS"%s/scan/%s/%s.eml",
1111 spool_directory, message_id, message_id);
1113 /* calc file size */
1114 clam_fd = open(CS scanrequest, O_RDONLY);
1115 if (clam_fd == -1) {
1116 log_write(0, LOG_MAIN|LOG_PANIC,
1117 "malware acl condition: clamd: can't open spool file %s: %s",
1118 scanrequest, strerror(errno));
1121 fsize = lseek(clam_fd, 0, SEEK_END);
1123 log_write(0, LOG_MAIN|LOG_PANIC,
1124 "malware acl condition: clamd: can't seek spool file %s: %s",
1125 scanrequest, strerror(errno));
1128 lseek(clam_fd, 0, SEEK_SET);
1130 clamav_fbuf = (uschar *) malloc (fsize);
1134 log_write(0, LOG_MAIN|LOG_PANIC,
1135 "malware acl condition: clamd: unable to allocate memory %u for file (%s)",
1136 fsize, scanrequest);
1140 result = read (clam_fd, clamav_fbuf, fsize);
1145 log_write(0, LOG_MAIN|LOG_PANIC,
1146 "malware acl condition: clamd: can't read spool file %s: %s",
1147 scanrequest, strerror(errno));
1152 /* send file body to socket */
1153 if (send(sockData, clamav_fbuf, fsize, 0) < 0) {
1156 log_write(0, LOG_MAIN|LOG_PANIC,
1157 "malware acl condition: clamd: unable to send file body to socket (%s:%u)", hostname, port);
1165 /* open the local socket */
1166 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
1167 log_write(0, LOG_MAIN|LOG_PANIC,
1168 "malware acl condition: clamd: unable to acquire socket (%s)",
1173 server.sun_family = AF_UNIX;
1174 Ustrcpy(server.sun_path, clamd_options);
1176 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
1178 log_write(0, LOG_MAIN|LOG_PANIC,
1179 "malware acl condition: clamd: unable to connect to UNIX socket %s (%s)",
1180 clamd_options, strerror(errno) );
1185 /* Pass the string to ClamAV (7 = "SCAN \n" + \0) */
1187 snprintf(CS file_name,1024,"SCAN %s/scan/%s\n", spool_directory, message_id);
1189 if (send(sock, file_name, Ustrlen(file_name), 0) < 0) {
1191 log_write(0, LOG_MAIN|LOG_PANIC,"malware acl condition: clamd: unable to write to socket (%s)",
1197 We're done sending, close socket for writing.
1199 One user reported that clamd 0.70 does not like this any more ...
1203 /* shutdown(sock, SHUT_WR); */
1205 /* Read the result */
1206 memset(av_buffer, 0, sizeof(av_buffer));
1207 bread = read(sock, av_buffer, sizeof(av_buffer));
1211 log_write(0, LOG_MAIN|LOG_PANIC,
1212 "malware acl condition: clamd: unable to read from socket (%s)",
1217 if (bread == sizeof(av_buffer)) {
1218 log_write(0, LOG_MAIN|LOG_PANIC,
1219 "malware acl condition: clamd: buffer too small");
1223 /* Check the result. ClamAV Returns
1224 infected: -> "<filename>: <virusname> FOUND"
1225 not-infected: -> "<filename>: OK"
1226 error: -> "<filename>: <errcode> ERROR */
1228 if (!(*av_buffer)) {
1229 log_write(0, LOG_MAIN|LOG_PANIC,
1230 "malware acl condition: clamd: ClamAV returned null");
1234 /* colon in returned output? */
1235 if((p = Ustrrchr(av_buffer,':')) == NULL) {
1236 log_write(0, LOG_MAIN|LOG_PANIC,
1237 "malware acl condition: clamd: ClamAV returned malformed result: %s",
1242 /* strip filename strip CR at the end */
1244 while (*p == ' ') ++p;
1246 p = vname + Ustrlen(vname) - 1;
1247 if( *p == '\n' ) *p = '\0';
1249 if ((p = Ustrstr(vname, "FOUND"))!=NULL) {
1251 for (--p;p>vname && *p<=32;p--) *p=0;
1252 for (;*vname==32;vname++);
1253 Ustrcpy(malware_name_buffer,vname);
1254 malware_name = malware_name_buffer;
1257 if (Ustrstr(vname, "ERROR")!=NULL) {
1258 /* ClamAV reports ERROR
1260 for (;*vname!='\n' && vname>av_buffer; vname--);
1261 if (*vname=='\n') vname++;
1263 log_write(0, LOG_MAIN|LOG_PANIC,
1264 "malware acl condition: clamd: ClamAV returned %s",vname);
1268 /* Everything should be OK */
1269 malware_name = NULL;
1273 /* ----------------------------------------------------------------------- */
1276 /* "mksd" scanner type --------------------------------------------------- */
1277 else if (strcmpic(scanner_name,US"mksd") == 0) {
1278 uschar *mksd_options;
1279 char *mksd_options_end;
1280 uschar mksd_options_buffer[32];
1281 int mksd_maxproc = 1; /* default, if no option supplied */
1282 struct sockaddr_un server;
1286 if ((mksd_options = string_nextinlist(&av_scanner_work, &sep,
1287 mksd_options_buffer,
1288 sizeof(mksd_options_buffer))) != NULL) {
1289 mksd_maxproc = (int) strtol(CS mksd_options, &mksd_options_end, 10);
1290 if ((*mksd_options == '\0') || (*mksd_options_end != '\0') ||
1291 (mksd_maxproc < 1) || (mksd_maxproc > 32)) {
1292 log_write(0, LOG_MAIN|LOG_PANIC,
1293 "malware acl condition: mksd: invalid option '%s'", mksd_options);
1298 /* open the mksd socket */
1299 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1301 log_write(0, LOG_MAIN|LOG_PANIC,
1302 "malware acl condition: can't open UNIX socket.");
1305 server.sun_family = AF_UNIX;
1306 Ustrcpy(server.sun_path, "/var/run/mksd/socket");
1307 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
1309 log_write(0, LOG_MAIN|LOG_PANIC,
1310 "malware acl condition: unable to connect to mksd UNIX socket (/var/run/mksd/socket). errno=%d", errno);
1314 malware_name = NULL;
1316 retval = mksd_scan_packed(sock);
1321 /* ----------------------------------------------------------------------- */
1323 /* "unknown" scanner type ------------------------------------------------- */
1325 log_write(0, LOG_MAIN|LOG_PANIC,
1326 "malware condition: unknown scanner type '%s'", scanner_name);
1329 /* ----------------------------------------------------------------------- */
1331 /* set "been here, done that" marker */
1335 /* match virus name against pattern (caseless ------->----------v) */
1336 if ( (malware_name != NULL) &&
1337 (regex_match_and_setup(re, malware_name, 0, -1)) ) {
1346 /* simple wrapper for reading lines from sockets */
1347 int recv_line(int sock, uschar *buffer, int size) {
1350 memset(buffer,0,size);
1352 while(recv(sock,p,1,0) > -1) {
1353 if ((p-buffer) > (size-2)) break;
1354 if (*p == '\n') break;
1355 if (*p != '\r') p++;
1363 /* ============= private routines for the "mksd" scanner type ============== */
1365 #include <sys/uio.h>
1367 int mksd_writev (int sock, struct iovec *iov, int iovcnt)
1373 i = writev (sock, iov, iovcnt);
1374 while ((i < 0) && (errno == EINTR));
1377 log_write(0, LOG_MAIN|LOG_PANIC,
1378 "malware acl condition: unable to write to mksd UNIX socket (/var/run/mksd/socket)");
1383 if (i >= iov->iov_len) {
1390 iov->iov_base = CS iov->iov_base + i;
1396 int mksd_read_lines (int sock, uschar *av_buffer, int av_buffer_size)
1402 if ((i = recv (sock, av_buffer+offset, av_buffer_size-offset, 0)) <= 0) {
1404 log_write(0, LOG_MAIN|LOG_PANIC,
1405 "malware acl condition: unable to read from mksd UNIX socket (/var/run/mksd/socket)");
1410 /* offset == av_buffer_size -> buffer full */
1411 if (offset == av_buffer_size) {
1413 log_write(0, LOG_MAIN|LOG_PANIC,
1414 "malware acl condition: malformed reply received from mksd");
1417 } while (av_buffer[offset-1] != '\n');
1419 av_buffer[offset] = '\0';
1423 int mksd_parse_line (char *line)
1434 if ((p = strchr (line, '\n')) != NULL)
1436 log_write(0, LOG_MAIN|LOG_PANIC,
1437 "malware acl condition: mksd scanner failed: %s", line);
1441 if ((p = strchr (line, '\n')) != NULL) {
1443 if (((p-line) > 5) && ((p-line) < sizeof (malware_name_buffer)) && (line[3] == ' '))
1444 if (((p = strchr (line+4, ' ')) != NULL) && ((p-line) > 4)) {
1446 Ustrcpy (malware_name_buffer, line+4);
1447 malware_name = malware_name_buffer;
1451 log_write(0, LOG_MAIN|LOG_PANIC,
1452 "malware acl condition: malformed reply received from mksd: %s", line);
1457 int mksd_scan_packed (int sock)
1459 struct iovec iov[7];
1460 char *cmd = "MSQ/scan/.eml\n";
1461 uschar av_buffer[1024];
1463 iov[0].iov_base = cmd;
1465 iov[1].iov_base = CS spool_directory;
1466 iov[1].iov_len = Ustrlen (spool_directory);
1467 iov[2].iov_base = cmd + 3;
1469 iov[3].iov_base = iov[5].iov_base = CS message_id;
1470 iov[3].iov_len = iov[5].iov_len = Ustrlen (message_id);
1471 iov[4].iov_base = cmd + 3;
1473 iov[6].iov_base = cmd + 9;
1476 if (mksd_writev (sock, iov, 7) < 0)
1479 if (mksd_read_lines (sock, av_buffer, sizeof (av_buffer)) < 0)
1484 return mksd_parse_line (CS av_buffer);