1 /* $Cambridge: exim/src/src/malware.c,v 1.2 2004/12/16 15:11:47 tom 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 /* Routine to check whether a system is big- or litte-endian.
28 Ripped from http://www.faqs.org/faqs/graphics/fileformats-faq/part4/section-7.html
29 Needed for proper kavdaemon implementation. Sigh. */
30 #define BIG_MY_ENDIAN 0
31 #define LITTLE_MY_ENDIAN 1
32 int test_byte_order(void);
33 int test_byte_order() {
34 short int word = 0x0001;
35 char *byte = (char *) &word;
36 return(byte[0] ? LITTLE_MY_ENDIAN : BIG_MY_ENDIAN);
39 uschar malware_name_buffer[256];
42 int malware(uschar **listptr) {
44 uschar *list = *listptr;
45 uschar *av_scanner_work = av_scanner;
47 uschar scanner_name_buffer[16];
48 uschar *malware_regex;
49 uschar malware_regex_buffer[64];
50 uschar malware_regex_default[] = ".+";
51 unsigned long long mbox_size;
57 /* make sure the eml mbox file is spooled up */
58 mbox_file = spool_mbox(&mbox_size);
59 if (mbox_file == NULL) {
60 /* error while spooling */
61 log_write(0, LOG_MAIN|LOG_PANIC,
62 "malware acl condition: error while creating mbox spool file");
65 /* none of our current scanners need the mbox
66 file as a stream, so we can close it right away */
69 /* extract the malware regex to match against from the option list */
70 if ((malware_regex = string_nextinlist(&list, &sep,
72 sizeof(malware_regex_buffer))) != NULL) {
74 /* parse 1st option */
75 if ( (strcmpic(malware_regex,US"false") == 0) ||
76 (Ustrcmp(malware_regex,"0") == 0) ) {
77 /* explicitly no matching */
81 /* special cases (match anything except empty) */
82 if ( (strcmpic(malware_regex,US"true") == 0) ||
83 (Ustrcmp(malware_regex,"*") == 0) ||
84 (Ustrcmp(malware_regex,"1") == 0) ) {
85 malware_regex = malware_regex_default;
89 /* empty means "don't match anything" */
93 /* compile the regex, see if it works */
94 re = pcre_compile(CS malware_regex, PCRE_COPT, (const char **)&rerror, &roffset, NULL);
96 log_write(0, LOG_MAIN|LOG_PANIC,
97 "malware acl condition: regular expression error in '%s': %s at offset %d", malware_regex, rerror, roffset);
101 /* if av_scanner starts with a dollar, expand it first */
102 if (*av_scanner == '$') {
103 av_scanner_work = expand_string(av_scanner);
104 if (av_scanner_work == NULL) {
105 log_write(0, LOG_MAIN|LOG_PANIC,
106 "malware acl condition: av_scanner starts with $, but expansion failed: %s", expand_string_message);
110 debug_printf("Expanded av_scanner global: %s\n", av_scanner_work);
111 /* disable result caching in this case */
117 /* Do not scan twice. */
118 if (malware_ok == 0) {
120 /* find the scanner type from the av_scanner option */
121 if ((scanner_name = string_nextinlist(&av_scanner_work, &sep,
123 sizeof(scanner_name_buffer))) == NULL) {
124 /* no scanner given */
125 log_write(0, LOG_MAIN|LOG_PANIC,
126 "malware acl condition: av_scanner configuration variable is empty");
130 /* "drweb" scanner type ----------------------------------------------- */
131 /* v0.1 - added support for tcp sockets */
132 /* v0.0 - initial release -- support for unix sockets */
133 if (strcmpic(scanner_name,US"drweb") == 0) {
134 uschar *drweb_options;
135 uschar drweb_options_buffer[1024];
136 uschar drweb_options_default[] = "/usr/local/drweb/run/drwebd.sock";
137 struct sockaddr_un server;
138 int sock, port, result, ovector[30];
140 uschar tmpbuf[1024], *drweb_fbuf;
141 uschar scanrequest[1024];
142 uschar drweb_match_string[128];
143 int drweb_rc, drweb_cmd, drweb_flags = 0x0000, drweb_fd,
144 drweb_vnum, drweb_slen, drweb_fin = 0x0000;
146 uschar hostname[256];
151 if ((drweb_options = string_nextinlist(&av_scanner_work, &sep,
152 drweb_options_buffer, sizeof(drweb_options_buffer))) == NULL) {
153 /* no options supplied, use default options */
154 drweb_options = drweb_options_default;
157 if (*drweb_options != '/') {
159 /* extract host and port part */
160 if( sscanf(CS drweb_options, "%s %u", hostname, &port) != 2 ) {
161 log_write(0, LOG_MAIN|LOG_PANIC,
162 "malware acl condition: drweb: invalid socket '%s'", drweb_options);
166 /* Lookup the host */
167 if((he = gethostbyname(CS hostname)) == 0) {
168 log_write(0, LOG_MAIN|LOG_PANIC,
169 "malware acl condition: drweb: failed to lookup host '%s'", hostname);
173 in = *(struct in_addr *) he->h_addr_list[0];
175 /* Open the drwebd TCP socket */
176 if ( (sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
177 log_write(0, LOG_MAIN|LOG_PANIC,
178 "malware acl condition: drweb: unable to acquire socket (%s)",
183 if (ip_connect(sock, AF_INET, (uschar*)inet_ntoa(in), port, 5) < 0) {
185 log_write(0, LOG_MAIN|LOG_PANIC,
186 "malware acl condition: drweb: connection to %s, port %u failed (%s)",
187 inet_ntoa(in), port, strerror(errno));
191 /* prepare variables */
192 drweb_cmd = htonl(DRWEBD_SCAN_CMD);
193 drweb_flags = htonl(DRWEBD_RETURN_VIRUSES | DRWEBD_IS_MAIL);
194 snprintf(CS scanrequest, 1024,CS"%s/scan/%s/%s.eml",
195 spool_directory, message_id, message_id);
198 drweb_fd = open(CS scanrequest, O_RDONLY);
199 if (drweb_fd == -1) {
200 log_write(0, LOG_MAIN|LOG_PANIC,
201 "malware acl condition: drweb: can't open spool file %s: %s",
202 scanrequest, strerror(errno));
205 fsize = lseek(drweb_fd, 0, SEEK_END);
207 log_write(0, LOG_MAIN|LOG_PANIC,
208 "malware acl condition: drweb: can't seek spool file %s: %s",
209 scanrequest, strerror(errno));
212 drweb_slen = htonl(fsize);
213 lseek(drweb_fd, 0, SEEK_SET);
215 /* send scan request */
216 if ((send(sock, &drweb_cmd, sizeof(drweb_cmd), 0) < 0) ||
217 (send(sock, &drweb_flags, sizeof(drweb_flags), 0) < 0) ||
218 (send(sock, &drweb_fin, sizeof(drweb_fin), 0) < 0) ||
219 (send(sock, &drweb_slen, sizeof(drweb_slen), 0) < 0)) {
222 log_write(0, LOG_MAIN|LOG_PANIC,
223 "malware acl condition: drweb: unable to send commands to socket (%s)", drweb_options);
227 drweb_fbuf = (uschar *) malloc (fsize);
231 log_write(0, LOG_MAIN|LOG_PANIC,
232 "malware acl condition: drweb: unable to allocate memory %u for file (%s)",
237 result = read (drweb_fd, drweb_fbuf, fsize);
241 log_write(0, LOG_MAIN|LOG_PANIC,
242 "malware acl condition: drweb: can't read spool file %s: %s",
243 scanrequest, strerror(errno));
247 /* send file body to socket */
248 if (send(sock, drweb_fbuf, fsize, 0) < 0) {
250 log_write(0, LOG_MAIN|LOG_PANIC,
251 "malware acl condition: drweb: unable to send file body to socket (%s)", drweb_options);
258 /* open the drwebd UNIX socket */
259 sock = socket(AF_UNIX, SOCK_STREAM, 0);
261 log_write(0, LOG_MAIN|LOG_PANIC,
262 "malware acl condition: drweb: can't open UNIX socket");
265 server.sun_family = AF_UNIX;
266 Ustrcpy(server.sun_path, drweb_options);
267 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
269 log_write(0, LOG_MAIN|LOG_PANIC,
270 "malware acl condition: drweb: unable to connect to socket (%s). errno=%d", drweb_options, errno);
274 /* prepare variables */
275 drweb_cmd = htonl(DRWEBD_SCAN_CMD);
276 drweb_flags = htonl(DRWEBD_RETURN_VIRUSES | DRWEBD_IS_MAIL);
277 snprintf(CS scanrequest, 1024,CS"%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
278 drweb_slen = htonl(Ustrlen(scanrequest));
280 /* send scan request */
281 if ((send(sock, &drweb_cmd, sizeof(drweb_cmd), 0) < 0) ||
282 (send(sock, &drweb_flags, sizeof(drweb_flags), 0) < 0) ||
283 (send(sock, &drweb_slen, sizeof(drweb_slen), 0) < 0) ||
284 (send(sock, scanrequest, Ustrlen(scanrequest), 0) < 0) ||
285 (send(sock, &drweb_fin, sizeof(drweb_fin), 0) < 0)) {
287 log_write(0, LOG_MAIN|LOG_PANIC,
288 "malware acl condition: drweb: unable to send commands to socket (%s)", drweb_options);
293 /* wait for result */
294 if ((bread = recv(sock, &drweb_rc, sizeof(drweb_rc), 0) != sizeof(drweb_rc))) {
296 log_write(0, LOG_MAIN|LOG_PANIC,
297 "malware acl condition: drweb: unable to read return code");
300 drweb_rc = ntohl(drweb_rc);
302 if ((bread = recv(sock, &drweb_vnum, sizeof(drweb_vnum), 0) != sizeof(drweb_vnum))) {
304 log_write(0, LOG_MAIN|LOG_PANIC,
305 "malware acl condition: drweb: unable to read the number of viruses");
308 drweb_vnum = ntohl(drweb_vnum);
310 /* "virus(es) found" if virus number is > 0 */
314 uschar pre_malware_nb[256];
316 malware_name = malware_name_buffer;
318 /* setup default virus name */
319 Ustrcpy(malware_name_buffer,"unknown");
321 /* read and concatenate virus names into one string */
322 for (i=0;i<drweb_vnum;i++)
324 /* read the size of report */
325 if ((bread = recv(sock, &drweb_slen, sizeof(drweb_slen), 0) != sizeof(drweb_slen))) {
327 log_write(0, LOG_MAIN|LOG_PANIC,
328 "malware acl condition: drweb: cannot read report size");
331 drweb_slen = ntohl(drweb_slen);
333 /* read report body */
334 if ((bread = recv(sock, tmpbuf, drweb_slen, 0)) != drweb_slen) {
336 log_write(0, LOG_MAIN|LOG_PANIC,
337 "malware acl condition: drweb: cannot read report string");
340 tmpbuf[drweb_slen] = '\0';
342 /* set up match regex, depends on retcode */
343 Ustrcpy(drweb_match_string, "infected\\swith\\s*(.+?)$");
345 drweb_re = pcre_compile( CS drweb_match_string,
347 (const char **)&rerror,
351 /* try matcher on the line, grab substring */
352 result = pcre_exec(drweb_re, NULL, CS tmpbuf, Ustrlen(tmpbuf), 0, 0, ovector, 30);
354 pcre_copy_substring(CS tmpbuf, ovector, result, 1, CS pre_malware_nb, 255);
356 /* the first name we just copy to malware_name */
358 Ustrcpy(CS malware_name_buffer, CS pre_malware_nb);
360 /* concatenate each new virus name to previous */
361 int slen = Ustrlen(malware_name_buffer);
362 if (slen < (slen+Ustrlen(pre_malware_nb))) {
363 Ustrcat(malware_name_buffer, "/");
364 Ustrcat(malware_name_buffer, pre_malware_nb);
375 /* ----------------------------------------------------------------------- */
376 else if (strcmpic(scanner_name,US"aveserver") == 0) {
378 uschar kav_options_buffer[1024];
379 uschar kav_options_default[] = "/var/run/aveserver";
382 struct sockaddr_un server;
385 if ((kav_options = string_nextinlist(&av_scanner_work, &sep,
387 sizeof(kav_options_buffer))) == NULL) {
388 /* no options supplied, use default options */
389 kav_options = kav_options_default;
392 /* open the aveserver socket */
393 sock = socket(AF_UNIX, SOCK_STREAM, 0);
395 log_write(0, LOG_MAIN|LOG_PANIC,
396 "malware acl condition: can't open UNIX socket.");
399 server.sun_family = AF_UNIX;
400 Ustrcpy(server.sun_path, kav_options);
401 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
403 log_write(0, LOG_MAIN|LOG_PANIC,
404 "malware acl condition: unable to connect to aveserver UNIX socket (%s). errno=%d", kav_options, errno);
408 /* read aveserver's greeting and see if it is ready (2xx greeting) */
409 recv_line(sock, buf, 32768);
412 /* aveserver is having problems */
414 log_write(0, LOG_MAIN|LOG_PANIC,
415 "malware acl condition: aveserver is unavailable (Responded: %s).", ((buf[0] != 0) ? buf : (uschar *)"nothing") );
419 /* prepare our command */
420 snprintf(CS buf, 32768, "SCAN bPQRSTUW %s/scan/%s/%s.eml\r\n", spool_directory, message_id, message_id);
423 if (send(sock, buf, Ustrlen(buf), 0) < 0) {
425 log_write(0, LOG_MAIN|LOG_PANIC,
426 "malware acl condition: unable to write to aveserver UNIX socket (%s)", kav_options);
431 /* read response lines, find malware name and final response */
432 while (recv_line(sock, buf, 32768) > 0) {
433 debug_printf("aveserver: %s\n", buf);
434 if (buf[0] == '2') break;
435 if (Ustrncmp(buf,"322",3) == 0) {
436 uschar *p = Ustrchr(&buf[4],' ');
438 Ustrcpy(malware_name_buffer,&buf[4]);
439 malware_name = malware_name_buffer;
445 /* "fsecure" scanner type ------------------------------------------------- */
446 else if (strcmpic(scanner_name,US"fsecure") == 0) {
447 uschar *fsecure_options;
448 uschar fsecure_options_buffer[1024];
449 uschar fsecure_options_default[] = "/var/run/.fsav";
450 struct sockaddr_un server;
451 int sock, i, j, bread = 0;
452 uschar file_name[1024];
453 uschar av_buffer[1024];
455 static uschar *cmdoptions[] = { "CONFIGURE\tARCHIVE\t1\n","CONFIGURE\tTIMEOUT\t0\n","CONFIGURE\tMAXARCH\t5\n","CONFIGURE\tMIME\t1\n" };
458 if ((fsecure_options = string_nextinlist(&av_scanner_work, &sep,
459 fsecure_options_buffer,
460 sizeof(fsecure_options_buffer))) == NULL) {
461 /* no options supplied, use default options */
462 fsecure_options = fsecure_options_default;
465 /* open the fsecure socket */
466 sock = socket(AF_UNIX, SOCK_STREAM, 0);
468 log_write(0, LOG_MAIN|LOG_PANIC,
469 "malware acl condition: unable to open fsecure socket %s (%s)",
470 fsecure_options, strerror(errno));
473 server.sun_family = AF_UNIX;
474 Ustrcpy(server.sun_path, fsecure_options);
475 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
477 log_write(0, LOG_MAIN|LOG_PANIC,
478 "malware acl condition: unable to connect to fsecure socket %s (%s)",
479 fsecure_options, strerror(errno));
484 memset(av_buffer, 0, sizeof(av_buffer));
485 for (i=0; i != 4; i++) {
486 /* debug_printf("send option \"%s\"",cmdoptions[i]); */
487 if (write(sock, cmdoptions[i], Ustrlen(cmdoptions[i])) < 0) {
489 log_write(0, LOG_MAIN|LOG_PANIC,
490 "malware acl condition: unable to write fsecure option %d to %s (%s)",
491 i, fsecure_options, strerror(errno));
495 bread = read(sock, av_buffer, sizeof(av_buffer));
496 if (bread >0) av_buffer[bread]='\0';
499 log_write(0, LOG_MAIN|LOG_PANIC,
500 "malware acl condition: unable to read fsecure answer %d (%s)", i, strerror(errno));
503 for (j=0;j<bread;j++) if((av_buffer[j]=='\r')||(av_buffer[j]=='\n')) av_buffer[j] ='@';
504 /* debug_printf("read answer %d read=%d \"%s\"\n", i, bread, av_buffer ); */
505 /* while (Ustrstr(av_buffer, "OK\tServer configured.@") == NULL); */
508 /* pass the mailfile to fsecure */
509 snprintf(CS file_name,1024,"SCAN\t%s/scan/%s/%s.eml\n", spool_directory, message_id, message_id);
510 /* debug_printf("send scan %s",file_name); */
511 if (write(sock, file_name, Ustrlen(file_name)) < 0) {
513 log_write(0, LOG_MAIN|LOG_PANIC,
514 "malware acl condition: unable to write fsecure scan to %s (%s)",
515 fsecure_options, strerror(errno));
520 /* todo also SUSPICION\t */
521 fs_inf = pcre_compile("\\S{0,5}INFECTED\\t[^\\t]*\\t([^\\t]+)\\t\\S*$", PCRE_COPT, (const char **)&rerror, &roffset, NULL);
523 /* read report, linewise */
527 memset(av_buffer, 0, sizeof(av_buffer));
529 bread=read(sock, &av_buffer[i], 1);
532 log_write(0, LOG_MAIN|LOG_PANIC,
533 "malware acl condition: unable to read fsecure result (%s)", strerror(errno));
538 while ((i < sizeof(av_buffer)-1 ) && (av_buffer[i-1] != '\n'));
539 av_buffer[i-1] = '\0';
540 /* debug_printf("got line \"%s\"\n",av_buffer); */
542 /* Really search for virus again? */
543 if (malware_name == NULL) {
544 /* try matcher on the line, grab substring */
545 i = pcre_exec(fs_inf, NULL, CS av_buffer, Ustrlen(av_buffer), 0, 0, ovector, 30);
548 pcre_copy_substring(CS av_buffer, ovector, i, 1, CS malware_name_buffer, 255);
549 malware_name = malware_name_buffer;
553 while (Ustrstr(av_buffer, "OK\tScan ok.") == NULL);
556 /* ----------------------------------------------------------------------- */
558 /* "kavdaemon" scanner type ------------------------------------------------ */
559 else if (strcmpic(scanner_name,US"kavdaemon") == 0) {
561 uschar kav_options_buffer[1024];
562 uschar kav_options_default[] = "/var/run/AvpCtl";
563 struct sockaddr_un server;
567 uschar scanrequest[1024];
568 uschar kav_match_string[128];
570 unsigned long kav_reportlen, bread;
573 if ((kav_options = string_nextinlist(&av_scanner_work, &sep,
575 sizeof(kav_options_buffer))) == NULL) {
576 /* no options supplied, use default options */
577 kav_options = kav_options_default;
580 /* open the kavdaemon socket */
581 sock = socket(AF_UNIX, SOCK_STREAM, 0);
583 log_write(0, LOG_MAIN|LOG_PANIC,
584 "malware acl condition: can't open UNIX socket.");
587 server.sun_family = AF_UNIX;
588 Ustrcpy(server.sun_path, kav_options);
589 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
591 log_write(0, LOG_MAIN|LOG_PANIC,
592 "malware acl condition: unable to connect to kavdaemon UNIX socket (%s). errno=%d", kav_options, errno);
596 /* get current date and time, build scan request */
598 strftime(CS tmpbuf, sizeof(tmpbuf), "<0>%d %b %H:%M:%S:%%s/scan/%%s", localtime(&t));
599 snprintf(CS scanrequest, 1024,CS tmpbuf, spool_directory, message_id);
601 /* send scan request */
602 if (send(sock, scanrequest, Ustrlen(scanrequest)+1, 0) < 0) {
604 log_write(0, LOG_MAIN|LOG_PANIC,
605 "malware acl condition: unable to write to kavdaemon UNIX socket (%s)", kav_options);
609 /* wait for result */
610 if ((bread = recv(sock, tmpbuf, 2, 0) != 2)) {
612 log_write(0, LOG_MAIN|LOG_PANIC,
613 "malware acl condition: unable to read 2 bytes from kavdaemon socket.");
617 /* get errorcode from one nibble */
618 if (test_byte_order() == LITTLE_MY_ENDIAN) {
619 kav_rc = tmpbuf[0] & 0x0F;
622 kav_rc = tmpbuf[1] & 0x0F;
625 /* improper kavdaemon configuration */
626 if ( (kav_rc == 5) || (kav_rc == 6) ) {
628 log_write(0, LOG_MAIN|LOG_PANIC,
629 "malware acl condition: please reconfigure kavdaemon to NOT disinfect or remove infected files.");
635 log_write(0, LOG_MAIN|LOG_PANIC,
636 "malware acl condition: kavdaemon reported 'scanning not completed' (code 1).");
642 log_write(0, LOG_MAIN|LOG_PANIC,
643 "malware acl condition: kavdaemon reported 'kavdaemon damaged' (code 7).");
647 /* code 8 is not handled, since it is ambigous. It appears mostly on
648 bounces where part of a file has been cut off */
650 /* "virus found" return codes (2-4) */
651 if ((kav_rc > 1) && (kav_rc < 5)) {
654 /* setup default virus name */
655 Ustrcpy(malware_name_buffer,"unknown");
656 malware_name = malware_name_buffer;
658 if (test_byte_order() == LITTLE_MY_ENDIAN) {
659 report_flag = tmpbuf[1];
662 report_flag = tmpbuf[0];
665 /* read the report, if available */
666 if( report_flag == 1 ) {
667 /* read report size */
668 if ((bread = recv(sock, &kav_reportlen, 4, 0)) != 4) {
670 log_write(0, LOG_MAIN|LOG_PANIC,
671 "malware acl condition: cannot read report size from kavdaemon");
675 /* it's possible that avp returns av_buffer[1] == 1 but the
676 reportsize is 0 (!?) */
677 if (kav_reportlen > 0) {
678 /* set up match regex, depends on retcode */
680 Ustrcpy(kav_match_string, "suspicion:\\s*(.+?)\\s*$");
682 Ustrcpy(kav_match_string, "infected:\\s*(.+?)\\s*$");
684 kav_re = pcre_compile( CS kav_match_string,
686 (const char **)&rerror,
690 /* read report, linewise */
691 while (kav_reportlen > 0) {
696 while ( recv(sock, &tmpbuf[bread], 1, 0) == 1 ) {
698 if ( (tmpbuf[bread] == '\n') || (bread > 1021) ) break;
702 tmpbuf[bread] = '\0';
704 /* try matcher on the line, grab substring */
705 result = pcre_exec(kav_re, NULL, CS tmpbuf, Ustrlen(tmpbuf), 0, 0, ovector, 30);
707 pcre_copy_substring(CS tmpbuf, ovector, result, 1, CS malware_name_buffer, 255);
721 /* ----------------------------------------------------------------------- */
724 /* "cmdline" scanner type ------------------------------------------------ */
725 else if (strcmpic(scanner_name,US"cmdline") == 0) {
726 uschar *cmdline_scanner;
727 uschar cmdline_scanner_buffer[1024];
728 uschar *cmdline_trigger;
729 uschar cmdline_trigger_buffer[1024];
730 const pcre *cmdline_trigger_re;
731 uschar *cmdline_regex;
732 uschar cmdline_regex_buffer[1024];
733 const pcre *cmdline_regex_re;
734 uschar file_name[1024];
735 uschar commandline[1024];
736 void (*eximsigchld)(int);
737 void (*eximsigpipe)(int);
738 FILE *scanner_out = NULL;
739 FILE *scanner_record = NULL;
740 uschar linebuffer[32767];
745 /* find scanner command line */
746 if ((cmdline_scanner = string_nextinlist(&av_scanner_work, &sep,
747 cmdline_scanner_buffer,
748 sizeof(cmdline_scanner_buffer))) == NULL) {
749 /* no command line supplied */
750 log_write(0, LOG_MAIN|LOG_PANIC,
751 "malware acl condition: missing commandline specification for cmdline scanner type.");
755 /* find scanner output trigger */
756 if ((cmdline_trigger = string_nextinlist(&av_scanner_work, &sep,
757 cmdline_trigger_buffer,
758 sizeof(cmdline_trigger_buffer))) == NULL) {
759 /* no trigger regex supplied */
760 log_write(0, LOG_MAIN|LOG_PANIC,
761 "malware acl condition: missing trigger specification for cmdline scanner type.");
765 /* precompile trigger regex */
766 cmdline_trigger_re = pcre_compile(CS cmdline_trigger, PCRE_COPT, (const char **)&rerror, &roffset, NULL);
767 if (cmdline_trigger_re == NULL) {
768 log_write(0, LOG_MAIN|LOG_PANIC,
769 "malware acl condition: regular expression error in '%s': %s at offset %d", cmdline_trigger_re, rerror, roffset);
773 /* find scanner name regex */
774 if ((cmdline_regex = string_nextinlist(&av_scanner_work, &sep,
775 cmdline_regex_buffer,
776 sizeof(cmdline_regex_buffer))) == NULL) {
777 /* no name regex supplied */
778 log_write(0, LOG_MAIN|LOG_PANIC,
779 "malware acl condition: missing virus name regex specification for cmdline scanner type.");
783 /* precompile name regex */
784 cmdline_regex_re = pcre_compile(CS cmdline_regex, PCRE_COPT, (const char **)&rerror, &roffset, NULL);
785 if (cmdline_regex_re == NULL) {
786 log_write(0, LOG_MAIN|LOG_PANIC,
787 "malware acl condition: regular expression error in '%s': %s at offset %d", cmdline_regex_re, rerror, roffset);
791 /* prepare scanner call */
792 snprintf(CS file_name,1024,"%s/scan/%s", spool_directory, message_id);
793 snprintf(CS commandline,1024, CS cmdline_scanner,file_name);
794 /* redirect STDERR too */
795 Ustrcat(commandline," 2>&1");
797 /* store exims signal handlers */
798 eximsigchld = signal(SIGCHLD,SIG_DFL);
799 eximsigpipe = signal(SIGPIPE,SIG_DFL);
801 scanner_out = popen(CS commandline,"r");
802 if (scanner_out == NULL) {
803 log_write(0, LOG_MAIN|LOG_PANIC,
804 "malware acl condition: calling cmdline scanner (%s) failed: %s.", commandline, strerror(errno));
805 signal(SIGCHLD,eximsigchld);
806 signal(SIGPIPE,eximsigpipe);
810 snprintf(CS file_name,1024,"%s/scan/%s/%s_scanner_output", spool_directory, message_id, message_id);
811 scanner_record = fopen(CS file_name,"w");
813 if (scanner_record == NULL) {
814 log_write(0, LOG_MAIN|LOG_PANIC,
815 "malware acl condition: opening scanner output file (%s) failed: %s.", file_name, strerror(errno));
817 signal(SIGCHLD,eximsigchld);
818 signal(SIGPIPE,eximsigpipe);
822 /* look for trigger while recording output */
823 while(fgets(CS linebuffer,32767,scanner_out) != NULL) {
824 if ( Ustrlen(linebuffer) > fwrite(linebuffer, 1, Ustrlen(linebuffer), scanner_record) ) {
826 log_write(0, LOG_MAIN|LOG_PANIC,
827 "malware acl condition: short write on scanner output file (%s).", file_name);
829 signal(SIGCHLD,eximsigchld);
830 signal(SIGPIPE,eximsigpipe);
833 /* try trigger match */
834 if (!trigger && regex_match_and_setup(cmdline_trigger_re, linebuffer, 0, -1))
838 fclose(scanner_record);
840 signal(SIGCHLD,eximsigchld);
841 signal(SIGPIPE,eximsigpipe);
844 /* setup default virus name */
845 Ustrcpy(malware_name_buffer,"unknown");
846 malware_name = malware_name_buffer;
848 /* re-open the scanner output file, look for name match */
849 scanner_record = fopen(CS file_name,"r");
850 while(fgets(CS linebuffer,32767,scanner_record) != NULL) {
852 result = pcre_exec(cmdline_regex_re, NULL, CS linebuffer, Ustrlen(linebuffer), 0, 0, ovector, 30);
854 pcre_copy_substring(CS linebuffer, ovector, result, 1, CS malware_name_buffer, 255);
857 fclose(scanner_record);
864 /* ----------------------------------------------------------------------- */
867 /* "sophie" scanner type ------------------------------------------------- */
868 else if (strcmpic(scanner_name,US"sophie") == 0) {
869 uschar *sophie_options;
870 uschar sophie_options_buffer[1024];
871 uschar sophie_options_default[] = "/var/run/sophie";
873 struct sockaddr_un server;
875 uschar file_name[1024];
876 uschar av_buffer[1024];
878 if ((sophie_options = string_nextinlist(&av_scanner_work, &sep,
879 sophie_options_buffer,
880 sizeof(sophie_options_buffer))) == NULL) {
881 /* no options supplied, use default options */
882 sophie_options = sophie_options_default;
885 /* open the sophie socket */
886 sock = socket(AF_UNIX, SOCK_STREAM, 0);
888 log_write(0, LOG_MAIN|LOG_PANIC,
889 "malware acl condition: can't open UNIX socket.");
892 server.sun_family = AF_UNIX;
893 Ustrcpy(server.sun_path, sophie_options);
894 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
896 log_write(0, LOG_MAIN|LOG_PANIC,
897 "malware acl condition: unable to connect to sophie UNIX socket (%s). errno=%d", sophie_options, errno);
901 /* pass the scan directory to sophie */
902 snprintf(CS file_name,1024,"%s/scan/%s", spool_directory, message_id);
903 if (write(sock, file_name, Ustrlen(file_name)) < 0) {
905 log_write(0, LOG_MAIN|LOG_PANIC,
906 "malware acl condition: unable to write to sophie UNIX socket (%s)", sophie_options);
910 write(sock, "\n", 1);
912 /* wait for result */
913 memset(av_buffer, 0, sizeof(av_buffer));
914 if ((!(bread = read(sock, av_buffer, sizeof(av_buffer))) > 0)) {
916 log_write(0, LOG_MAIN|LOG_PANIC,
917 "malware acl condition: unable to read from sophie UNIX socket (%s)", sophie_options);
924 if (av_buffer[0] == '1') {
925 if (Ustrchr(av_buffer, '\n')) *Ustrchr(av_buffer, '\n') = '\0';
926 Ustrcpy(malware_name_buffer,&av_buffer[2]);
927 malware_name = malware_name_buffer;
929 else if (!strncmp(CS av_buffer, "-1", 2)) {
930 log_write(0, LOG_MAIN|LOG_PANIC,
931 "malware acl condition: malware acl condition: sophie reported error");
935 /* all ok, no virus */
939 /* ----------------------------------------------------------------------- */
942 /* "clamd" scanner type ------------------------------------------------- */
943 /* This code was contributed by David Saez <david@ols.es> */
944 else if (strcmpic(scanner_name,US"clamd") == 0) {
945 uschar *clamd_options;
946 uschar clamd_options_buffer[1024];
947 uschar clamd_options_default[] = "/tmp/clamd";
949 struct sockaddr_un server;
950 int sock,port,bread=0;
951 uschar file_name[1024];
952 uschar av_buffer[1024];
953 uschar hostname[256];
957 if ((clamd_options = string_nextinlist(&av_scanner_work, &sep,
958 clamd_options_buffer,
959 sizeof(clamd_options_buffer))) == NULL) {
960 /* no options supplied, use default options */
961 clamd_options = clamd_options_default;
964 /* socket does not start with '/' -> network socket */
965 if (*clamd_options != '/') {
967 /* extract host and port part */
968 if( sscanf(CS clamd_options, "%s %u", hostname, &port) != 2 ) {
969 log_write(0, LOG_MAIN|LOG_PANIC,
970 "malware acl condition: clamd: invalid socket '%s'", clamd_options);
974 /* Lookup the host */
975 if((he = gethostbyname(CS hostname)) == 0) {
976 log_write(0, LOG_MAIN|LOG_PANIC,
977 "malware acl condition: clamd: failed to lookup host '%s'", hostname);
981 in = *(struct in_addr *) he->h_addr_list[0];
983 /* Open the ClamAV Socket */
984 if ( (sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
985 log_write(0, LOG_MAIN|LOG_PANIC,
986 "malware acl condition: clamd: unable to acquire socket (%s)",
991 if (ip_connect(sock, AF_INET, (uschar*)inet_ntoa(in), port, 5) < 0) {
993 log_write(0, LOG_MAIN|LOG_PANIC,
994 "malware acl condition: clamd: connection to %s, port %u failed (%s)",
995 inet_ntoa(in), port, strerror(errno));
1000 /* open the local socket */
1001 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
1002 log_write(0, LOG_MAIN|LOG_PANIC,
1003 "malware acl condition: clamd: unable to acquire socket (%s)",
1008 server.sun_family = AF_UNIX;
1009 Ustrcpy(server.sun_path, clamd_options);
1011 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
1013 log_write(0, LOG_MAIN|LOG_PANIC,
1014 "malware acl condition: clamd: unable to connect to UNIX socket %s (%s)",
1015 clamd_options, strerror(errno) );
1020 /* Pass the string to ClamAV (7 = "SCAN \n" + \0) */
1022 snprintf(CS file_name,1024,"SCAN %s/scan/%s\n", spool_directory, message_id);
1024 if (send(sock, file_name, Ustrlen(file_name), 0) < 0) {
1026 log_write(0, LOG_MAIN|LOG_PANIC,"malware acl condition: clamd: unable to write to socket (%s)",
1032 We're done sending, close socket for writing.
1034 One user reported that clamd 0.70 does not like this any more ...
1038 /* shutdown(sock, SHUT_WR); */
1040 /* Read the result */
1041 memset(av_buffer, 0, sizeof(av_buffer));
1042 bread = read(sock, av_buffer, sizeof(av_buffer));
1046 log_write(0, LOG_MAIN|LOG_PANIC,
1047 "malware acl condition: clamd: unable to read from socket (%s)",
1052 if (bread == sizeof(av_buffer)) {
1053 log_write(0, LOG_MAIN|LOG_PANIC,
1054 "malware acl condition: clamd: buffer too small");
1058 /* Check the result. ClamAV Returns
1059 infected: -> "<filename>: <virusname> FOUND"
1060 not-infected: -> "<filename>: OK"
1061 error: -> "<filename>: <errcode> ERROR */
1063 if (!(*av_buffer)) {
1064 log_write(0, LOG_MAIN|LOG_PANIC,
1065 "malware acl condition: clamd: ClamAV returned null");
1069 /* colon in returned output? */
1070 if((p = Ustrrchr(av_buffer,':')) == NULL) {
1071 log_write(0, LOG_MAIN|LOG_PANIC,
1072 "malware acl condition: clamd: ClamAV returned malformed result: %s",
1077 /* strip filename strip CR at the end */
1079 while (*p == ' ') ++p;
1081 p = vname + Ustrlen(vname) - 1;
1082 if( *p == '\n' ) *p = '\0';
1084 if ((p = Ustrstr(vname, "FOUND"))!=NULL) {
1086 for (--p;p>vname && *p<=32;p--) *p=0;
1087 for (;*vname==32;vname++);
1088 Ustrcpy(malware_name_buffer,vname);
1089 malware_name = malware_name_buffer;
1092 if (Ustrstr(vname, "ERROR")!=NULL) {
1093 /* ClamAV reports ERROR
1095 for (;*vname!='\n' && vname>av_buffer; vname--);
1096 if (*vname=='\n') vname++;
1098 log_write(0, LOG_MAIN|LOG_PANIC,
1099 "malware acl condition: clamd: ClamAV returned %s",vname);
1103 /* Everything should be OK */
1104 malware_name = NULL;
1108 /* ----------------------------------------------------------------------- */
1111 /* "mksd" scanner type --------------------------------------------------- */
1112 else if (strcmpic(scanner_name,US"mksd") == 0) {
1113 uschar *mksd_options;
1114 char *mksd_options_end;
1115 uschar mksd_options_buffer[32];
1116 int mksd_maxproc = 1; /* default, if no option supplied */
1117 struct sockaddr_un server;
1121 if ((mksd_options = string_nextinlist(&av_scanner_work, &sep,
1122 mksd_options_buffer,
1123 sizeof(mksd_options_buffer))) != NULL) {
1124 mksd_maxproc = (int) strtol(CS mksd_options, &mksd_options_end, 10);
1125 if ((*mksd_options == '\0') || (*mksd_options_end != '\0') ||
1126 (mksd_maxproc < 1) || (mksd_maxproc > 32)) {
1127 log_write(0, LOG_MAIN|LOG_PANIC,
1128 "malware acl condition: mksd: invalid option '%s'", mksd_options);
1133 /* open the mksd socket */
1134 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1136 log_write(0, LOG_MAIN|LOG_PANIC,
1137 "malware acl condition: can't open UNIX socket.");
1140 server.sun_family = AF_UNIX;
1141 Ustrcpy(server.sun_path, "/var/run/mksd/socket");
1142 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
1144 log_write(0, LOG_MAIN|LOG_PANIC,
1145 "malware acl condition: unable to connect to mksd UNIX socket (/var/run/mksd/socket). errno=%d", errno);
1149 malware_name = NULL;
1151 retval = mksd_scan_packed(sock);
1156 /* ----------------------------------------------------------------------- */
1158 /* "unknown" scanner type ------------------------------------------------- */
1160 log_write(0, LOG_MAIN|LOG_PANIC,
1161 "malware condition: unknown scanner type '%s'", scanner_name);
1164 /* ----------------------------------------------------------------------- */
1166 /* set "been here, done that" marker */
1170 /* match virus name against pattern (caseless ------->----------v) */
1171 if ( (malware_name != NULL) &&
1172 (regex_match_and_setup(re, malware_name, 0, -1)) ) {
1181 /* simple wrapper for reading lines from sockets */
1182 int recv_line(int sock, uschar *buffer, int size) {
1185 memset(buffer,0,size);
1187 while(recv(sock,p,1,0) > -1) {
1188 if ((p-buffer) > (size-2)) break;
1189 if (*p == '\n') break;
1190 if (*p != '\r') p++;
1198 /* ============= private routines for the "mksd" scanner type ============== */
1200 #include <sys/uio.h>
1202 int mksd_writev (int sock, struct iovec *iov, int iovcnt)
1208 i = writev (sock, iov, iovcnt);
1209 while ((i < 0) && (errno == EINTR));
1212 log_write(0, LOG_MAIN|LOG_PANIC,
1213 "malware acl condition: unable to write to mksd UNIX socket (/var/run/mksd/socket)");
1218 if (i >= iov->iov_len) {
1225 iov->iov_base = CS iov->iov_base + i;
1231 int mksd_read_lines (int sock, uschar *av_buffer, int av_buffer_size)
1237 if ((i = recv (sock, av_buffer+offset, av_buffer_size-offset, 0)) <= 0) {
1239 log_write(0, LOG_MAIN|LOG_PANIC,
1240 "malware acl condition: unable to read from mksd UNIX socket (/var/run/mksd/socket)");
1245 /* offset == av_buffer_size -> buffer full */
1246 if (offset == av_buffer_size) {
1248 log_write(0, LOG_MAIN|LOG_PANIC,
1249 "malware acl condition: malformed reply received from mksd");
1252 } while (av_buffer[offset-1] != '\n');
1254 av_buffer[offset] = '\0';
1258 int mksd_parse_line (char *line)
1269 if ((p = strchr (line, '\n')) != NULL)
1271 log_write(0, LOG_MAIN|LOG_PANIC,
1272 "malware acl condition: mksd scanner failed: %s", line);
1276 if ((p = strchr (line, '\n')) != NULL) {
1278 if (((p-line) > 5) && ((p-line) < sizeof (malware_name_buffer)) && (line[3] == ' '))
1279 if (((p = strchr (line+4, ' ')) != NULL) && ((p-line) > 4)) {
1281 Ustrcpy (malware_name_buffer, line+4);
1282 malware_name = malware_name_buffer;
1286 log_write(0, LOG_MAIN|LOG_PANIC,
1287 "malware acl condition: malformed reply received from mksd: %s", line);
1292 int mksd_scan_packed (int sock)
1294 struct iovec iov[7];
1295 char *cmd = "MSQ/scan/.eml\n";
1296 uschar av_buffer[1024];
1298 iov[0].iov_base = cmd;
1300 iov[1].iov_base = CS spool_directory;
1301 iov[1].iov_len = Ustrlen (spool_directory);
1302 iov[2].iov_base = cmd + 3;
1304 iov[3].iov_base = iov[5].iov_base = CS message_id;
1305 iov[3].iov_len = iov[5].iov_len = Ustrlen (message_id);
1306 iov[4].iov_base = cmd + 3;
1308 iov[6].iov_base = cmd + 9;
1311 if (mksd_writev (sock, iov, 7) < 0)
1314 if (mksd_read_lines (sock, av_buffer, sizeof (av_buffer)) < 0)
1319 return mksd_parse_line (CS av_buffer);