1 /* $Cambridge: exim/src/src/malware.c,v 1.1.2.2 2004/11/26 16:04:26 tom Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 #ifdef WITH_CONTENT_SCAN
9 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
12 /* Code for calling virus (malware) scanners. Called from acl.c. */
16 /* declaration of private routines */
17 int mksd_scan_packed(int sock);
18 int mksd_scan_unpacked(int sock, int maxproc);
20 /* SHUT_WR seems to be undefined on Unixware? */
25 #define DRWEBD_SCAN_CMD (1) /* scan file, buffer or diskfile */
26 #define DRWEBD_RETURN_VIRUSES (1<<0) /* ask daemon return to us viruses names from report */
27 #define DRWEBD_IS_MAIL (1<<19) /* say to daemon that format is "archive MAIL" */
29 /* Routine to check whether a system is big- or litte-endian.
30 Ripped from http://www.faqs.org/faqs/graphics/fileformats-faq/part4/section-7.html
31 Needed for proper kavdaemon implementation. Sigh. */
32 #define BIG_MY_ENDIAN 0
33 #define LITTLE_MY_ENDIAN 1
34 int test_byte_order(void);
35 int test_byte_order() {
36 short int word = 0x0001;
37 char *byte = (char *) &word;
38 return(byte[0] ? LITTLE_MY_ENDIAN : BIG_MY_ENDIAN);
41 uschar malware_name_buffer[256];
44 int malware(uschar **listptr) {
46 uschar *list = *listptr;
47 uschar *av_scanner_work = av_scanner;
49 uschar scanner_name_buffer[16];
50 uschar *malware_regex;
51 uschar malware_regex_buffer[64];
52 uschar malware_regex_default[] = ".+";
53 unsigned long long mbox_size;
59 /* make sure the eml mbox file is spooled up */
60 mbox_file = spool_mbox(&mbox_size);
61 if (mbox_file == NULL) {
62 /* error while spooling */
63 log_write(0, LOG_MAIN|LOG_PANIC,
64 "malware acl condition: error while creating mbox spool file");
67 /* none of our current scanners need the mbox
68 file as a stream, so we can close it right away */
71 /* extract the malware regex to match against from the option list */
72 if ((malware_regex = string_nextinlist(&list, &sep,
74 sizeof(malware_regex_buffer))) != NULL) {
76 /* parse 1st option */
77 if ( (strcmpic(malware_regex,US"false") == 0) ||
78 (Ustrcmp(malware_regex,"0") == 0) ) {
79 /* explicitly no matching */
83 /* special cases (match anything except empty) */
84 if ( (strcmpic(malware_regex,US"true") == 0) ||
85 (Ustrcmp(malware_regex,"*") == 0) ||
86 (Ustrcmp(malware_regex,"1") == 0) ) {
87 malware_regex = malware_regex_default;
91 /* empty means "don't match anything" */
95 /* compile the regex, see if it works */
96 re = pcre_compile(CS malware_regex, PCRE_COPT, (const char **)&rerror, &roffset, NULL);
98 log_write(0, LOG_MAIN|LOG_PANIC,
99 "malware acl condition: regular expression error in '%s': %s at offset %d", malware_regex, rerror, roffset);
103 /* if av_scanner starts with a dollar, expand it first */
104 if (*av_scanner == '$') {
105 av_scanner_work = expand_string(av_scanner);
106 if (av_scanner_work == NULL) {
107 log_write(0, LOG_MAIN|LOG_PANIC,
108 "malware acl condition: av_scanner starts with $, but expansion failed: %s", expand_string_message);
112 debug_printf("Expanded av_scanner global: %s\n", av_scanner_work);
113 /* disable result caching in this case */
119 /* Do not scan twice. */
120 if (malware_ok == 0) {
122 /* find the scanner type from the av_scanner option */
123 if ((scanner_name = string_nextinlist(&av_scanner_work, &sep,
125 sizeof(scanner_name_buffer))) == NULL) {
126 /* no scanner given */
127 log_write(0, LOG_MAIN|LOG_PANIC,
128 "malware acl condition: av_scanner configuration variable is empty");
132 /* "drweb" scanner type ----------------------------------------------- */
133 /* v0.1 - added support for tcp sockets */
134 /* v0.0 - initial release -- support for unix sockets */
135 if (strcmpic(scanner_name,US"drweb") == 0) {
136 uschar *drweb_options;
137 uschar drweb_options_buffer[1024];
138 uschar drweb_options_default[] = "/usr/local/drweb/run/drwebd.sock";
139 struct sockaddr_un server;
140 int sock, port, result, ovector[30];
142 uschar tmpbuf[1024], *drweb_fbuf;
143 uschar scanrequest[1024];
144 uschar drweb_match_string[128];
145 int drweb_rc, drweb_cmd, drweb_flags = 0x0000, drweb_fd,
146 drweb_vnum, drweb_slen, drweb_fin = 0x0000;
148 uschar hostname[256];
153 if ((drweb_options = string_nextinlist(&av_scanner_work, &sep,
154 drweb_options_buffer, sizeof(drweb_options_buffer))) == NULL) {
155 /* no options supplied, use default options */
156 drweb_options = drweb_options_default;
159 if (*drweb_options != '/') {
161 /* extract host and port part */
162 if( sscanf(CS drweb_options, "%s %u", hostname, &port) != 2 ) {
163 log_write(0, LOG_MAIN|LOG_PANIC,
164 "malware acl condition: drweb: invalid socket '%s'", drweb_options);
168 /* Lookup the host */
169 if((he = gethostbyname(CS hostname)) == 0) {
170 log_write(0, LOG_MAIN|LOG_PANIC,
171 "malware acl condition: drweb: failed to lookup host '%s'", hostname);
175 in = *(struct in_addr *) he->h_addr_list[0];
177 /* Open the drwebd TCP socket */
178 if ( (sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
179 log_write(0, LOG_MAIN|LOG_PANIC,
180 "malware acl condition: drweb: unable to acquire socket (%s)",
185 if (ip_connect(sock, AF_INET, (uschar*)inet_ntoa(in), port, 5) < 0) {
187 log_write(0, LOG_MAIN|LOG_PANIC,
188 "malware acl condition: drweb: connection to %s, port %u failed (%s)",
189 inet_ntoa(in), port, strerror(errno));
193 /* prepare variables */
194 drweb_cmd = htonl(DRWEBD_SCAN_CMD);
195 drweb_flags = htonl(DRWEBD_RETURN_VIRUSES | DRWEBD_IS_MAIL);
196 snprintf(CS scanrequest, 1024,CS"%s/scan/%s/%s.eml",
197 spool_directory, message_id, message_id);
200 drweb_fd = open(CS scanrequest, O_RDONLY);
201 if (drweb_fd == -1) {
202 log_write(0, LOG_MAIN|LOG_PANIC,
203 "malware acl condition: drweb: can't open spool file %s: %s",
204 scanrequest, strerror(errno));
207 fsize = lseek(drweb_fd, 0, SEEK_END);
209 log_write(0, LOG_MAIN|LOG_PANIC,
210 "malware acl condition: drweb: can't seek spool file %s: %s",
211 scanrequest, strerror(errno));
214 drweb_slen = htonl(fsize);
215 lseek(drweb_fd, 0, SEEK_SET);
217 /* send scan request */
218 if ((send(sock, &drweb_cmd, sizeof(drweb_cmd), 0) < 0) ||
219 (send(sock, &drweb_flags, sizeof(drweb_flags), 0) < 0) ||
220 (send(sock, &drweb_fin, sizeof(drweb_fin), 0) < 0) ||
221 (send(sock, &drweb_slen, sizeof(drweb_slen), 0) < 0)) {
224 log_write(0, LOG_MAIN|LOG_PANIC,
225 "malware acl condition: drweb: unable to send commands to socket (%s)", drweb_options);
229 drweb_fbuf = (uschar *) malloc (fsize);
233 log_write(0, LOG_MAIN|LOG_PANIC,
234 "malware acl condition: drweb: unable to allocate memory %u for file (%s)",
239 result = read (drweb_fd, drweb_fbuf, fsize);
243 log_write(0, LOG_MAIN|LOG_PANIC,
244 "malware acl condition: drweb: can't read spool file %s: %s",
245 scanrequest, strerror(errno));
249 /* send file body to socket */
250 if (send(sock, drweb_fbuf, fsize, 0) < 0) {
252 log_write(0, LOG_MAIN|LOG_PANIC,
253 "malware acl condition: drweb: unable to send file body to socket (%s)", drweb_options);
260 /* open the drwebd UNIX socket */
261 sock = socket(AF_UNIX, SOCK_STREAM, 0);
263 log_write(0, LOG_MAIN|LOG_PANIC,
264 "malware acl condition: drweb: can't open UNIX socket");
267 server.sun_family = AF_UNIX;
268 Ustrcpy(server.sun_path, drweb_options);
269 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
271 log_write(0, LOG_MAIN|LOG_PANIC,
272 "malware acl condition: drweb: unable to connect to socket (%s). errno=%d", drweb_options, errno);
276 /* prepare variables */
277 drweb_cmd = htonl(DRWEBD_SCAN_CMD);
278 drweb_flags = htonl(DRWEBD_RETURN_VIRUSES | DRWEBD_IS_MAIL);
279 snprintf(CS scanrequest, 1024,CS"%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
280 drweb_slen = htonl(Ustrlen(scanrequest));
282 /* send scan request */
283 if ((send(sock, &drweb_cmd, sizeof(drweb_cmd), 0) < 0) ||
284 (send(sock, &drweb_flags, sizeof(drweb_flags), 0) < 0) ||
285 (send(sock, &drweb_slen, sizeof(drweb_slen), 0) < 0) ||
286 (send(sock, scanrequest, Ustrlen(scanrequest), 0) < 0) ||
287 (send(sock, &drweb_fin, sizeof(drweb_fin), 0) < 0)) {
289 log_write(0, LOG_MAIN|LOG_PANIC,
290 "malware acl condition: drweb: unable to send commands to socket (%s)", drweb_options);
295 /* wait for result */
296 if ((bread = recv(sock, &drweb_rc, sizeof(drweb_rc), 0) != sizeof(drweb_rc))) {
298 log_write(0, LOG_MAIN|LOG_PANIC,
299 "malware acl condition: drweb: unable to read return code");
302 drweb_rc = ntohl(drweb_rc);
304 if ((bread = recv(sock, &drweb_vnum, sizeof(drweb_vnum), 0) != sizeof(drweb_vnum))) {
306 log_write(0, LOG_MAIN|LOG_PANIC,
307 "malware acl condition: drweb: unable to read the number of viruses");
310 drweb_vnum = ntohl(drweb_vnum);
312 /* "virus(es) found" if virus number is > 0 */
316 uschar pre_malware_nb[256];
318 malware_name = malware_name_buffer;
320 /* setup default virus name */
321 Ustrcpy(malware_name_buffer,"unknown");
323 /* read and concatenate virus names into one string */
324 for (i=0;i<drweb_vnum;i++)
326 /* read the size of report */
327 if ((bread = recv(sock, &drweb_slen, sizeof(drweb_slen), 0) != sizeof(drweb_slen))) {
329 log_write(0, LOG_MAIN|LOG_PANIC,
330 "malware acl condition: drweb: cannot read report size");
333 drweb_slen = ntohl(drweb_slen);
335 /* read report body */
336 if ((bread = recv(sock, tmpbuf, drweb_slen, 0)) != drweb_slen) {
338 log_write(0, LOG_MAIN|LOG_PANIC,
339 "malware acl condition: drweb: cannot read report string");
342 tmpbuf[drweb_slen] = '\0';
344 /* set up match regex, depends on retcode */
345 Ustrcpy(drweb_match_string, "infected\\swith\\s*(.+?)$");
347 drweb_re = pcre_compile( CS drweb_match_string,
349 (const char **)&rerror,
353 /* try matcher on the line, grab substring */
354 result = pcre_exec(drweb_re, NULL, CS tmpbuf, Ustrlen(tmpbuf), 0, 0, ovector, 30);
356 pcre_copy_substring(CS tmpbuf, ovector, result, 1, CS pre_malware_nb, 255);
358 /* the first name we just copy to malware_name */
360 Ustrcpy(CS malware_name_buffer, CS pre_malware_nb);
362 /* concatenate each new virus name to previous */
363 int slen = Ustrlen(malware_name_buffer);
364 if (slen < (slen+Ustrlen(pre_malware_nb))) {
365 Ustrcat(malware_name_buffer, "/");
366 Ustrcat(malware_name_buffer, pre_malware_nb);
377 /* ----------------------------------------------------------------------- */
378 else if (strcmpic(scanner_name,US"aveserver") == 0) {
380 uschar kav_options_buffer[1024];
381 uschar kav_options_default[] = "/var/run/aveserver";
384 struct sockaddr_un server;
387 if ((kav_options = string_nextinlist(&av_scanner_work, &sep,
389 sizeof(kav_options_buffer))) == NULL) {
390 /* no options supplied, use default options */
391 kav_options = kav_options_default;
394 /* open the aveserver socket */
395 sock = socket(AF_UNIX, SOCK_STREAM, 0);
397 log_write(0, LOG_MAIN|LOG_PANIC,
398 "malware acl condition: can't open UNIX socket.");
401 server.sun_family = AF_UNIX;
402 Ustrcpy(server.sun_path, kav_options);
403 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
405 log_write(0, LOG_MAIN|LOG_PANIC,
406 "malware acl condition: unable to connect to aveserver UNIX socket (%s). errno=%d", kav_options, errno);
410 /* read aveserver's greeting and see if it is ready (2xx greeting) */
411 recv_line(sock, buf, 32768);
414 /* aveserver is having problems */
416 log_write(0, LOG_MAIN|LOG_PANIC,
417 "malware acl condition: aveserver is unavailable (Responded: %s).", ((buf[0] != 0) ? buf : (uschar *)"nothing") );
421 /* prepare our command */
422 snprintf(CS buf, 32768, "SCAN bPQRSTUW %s/scan/%s/%s.eml\r\n", spool_directory, message_id, message_id);
425 if (send(sock, buf, Ustrlen(buf), 0) < 0) {
427 log_write(0, LOG_MAIN|LOG_PANIC,
428 "malware acl condition: unable to write to aveserver UNIX socket (%s)", kav_options);
433 /* read response lines, find malware name and final response */
434 while (recv_line(sock, buf, 32768) > 0) {
435 debug_printf("aveserver: %s\n", buf);
436 if (buf[0] == '2') break;
437 if (Ustrncmp(buf,"322",3) == 0) {
438 uschar *p = Ustrchr(&buf[4],' ');
440 Ustrcpy(malware_name_buffer,&buf[4]);
441 malware_name = malware_name_buffer;
447 /* "fsecure" scanner type ------------------------------------------------- */
448 else if (strcmpic(scanner_name,US"fsecure") == 0) {
449 uschar *fsecure_options;
450 uschar fsecure_options_buffer[1024];
451 uschar fsecure_options_default[] = "/var/run/.fsav";
452 struct sockaddr_un server;
453 int sock, i, j, bread = 0;
454 uschar file_name[1024];
455 uschar av_buffer[1024];
457 static uschar *cmdoptions[] = { "CONFIGURE\tARCHIVE\t1\n","CONFIGURE\tTIMEOUT\t0\n","CONFIGURE\tMAXARCH\t5\n","CONFIGURE\tMIME\t1\n" };
460 if ((fsecure_options = string_nextinlist(&av_scanner_work, &sep,
461 fsecure_options_buffer,
462 sizeof(fsecure_options_buffer))) == NULL) {
463 /* no options supplied, use default options */
464 fsecure_options = fsecure_options_default;
467 /* open the fsecure socket */
468 sock = socket(AF_UNIX, SOCK_STREAM, 0);
470 log_write(0, LOG_MAIN|LOG_PANIC,
471 "malware acl condition: unable to open fsecure socket %s (%s)",
472 fsecure_options, strerror(errno));
475 server.sun_family = AF_UNIX;
476 Ustrcpy(server.sun_path, fsecure_options);
477 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
479 log_write(0, LOG_MAIN|LOG_PANIC,
480 "malware acl condition: unable to connect to fsecure socket %s (%s)",
481 fsecure_options, strerror(errno));
486 memset(av_buffer, 0, sizeof(av_buffer));
487 for (i=0; i != 4; i++) {
488 /* debug_printf("send option \"%s\"",cmdoptions[i]); */
489 if (write(sock, cmdoptions[i], Ustrlen(cmdoptions[i])) < 0) {
491 log_write(0, LOG_MAIN|LOG_PANIC,
492 "malware acl condition: unable to write fsecure option %d to %s (%s)",
493 i, fsecure_options, strerror(errno));
497 bread = read(sock, av_buffer, sizeof(av_buffer));
498 if (bread >0) av_buffer[bread]='\0';
501 log_write(0, LOG_MAIN|LOG_PANIC,
502 "malware acl condition: unable to read fsecure answer %d (%s)", i, strerror(errno));
505 for (j=0;j<bread;j++) if((av_buffer[j]=='\r')||(av_buffer[j]=='\n')) av_buffer[j] ='@';
506 /* debug_printf("read answer %d read=%d \"%s\"\n", i, bread, av_buffer ); */
507 /* while (Ustrstr(av_buffer, "OK\tServer configured.@") == NULL); */
510 /* pass the mailfile to fsecure */
511 snprintf(CS file_name,1024,"SCAN\t%s/scan/%s/%s.eml\n", spool_directory, message_id, message_id);
512 /* debug_printf("send scan %s",file_name); */
513 if (write(sock, file_name, Ustrlen(file_name)) < 0) {
515 log_write(0, LOG_MAIN|LOG_PANIC,
516 "malware acl condition: unable to write fsecure scan to %s (%s)",
517 fsecure_options, strerror(errno));
522 /* todo also SUSPICION\t */
523 fs_inf = pcre_compile("\\S{0,5}INFECTED\\t[^\\t]*\\t([^\\t]+)\\t\\S*$", PCRE_COPT, (const char **)&rerror, &roffset, NULL);
525 /* read report, linewise */
529 memset(av_buffer, 0, sizeof(av_buffer));
531 bread=read(sock, &av_buffer[i], 1);
534 log_write(0, LOG_MAIN|LOG_PANIC,
535 "malware acl condition: unable to read fsecure result (%s)", strerror(errno));
540 while ((i < sizeof(av_buffer)-1 ) && (av_buffer[i-1] != '\n'));
541 av_buffer[i-1] = '\0';
542 /* debug_printf("got line \"%s\"\n",av_buffer); */
544 /* Really search for virus again? */
545 if (malware_name == NULL) {
546 /* try matcher on the line, grab substring */
547 i = pcre_exec(fs_inf, NULL, CS av_buffer, Ustrlen(av_buffer), 0, 0, ovector, 30);
550 pcre_copy_substring(CS av_buffer, ovector, i, 1, CS malware_name_buffer, 255);
551 malware_name = malware_name_buffer;
555 while (Ustrstr(av_buffer, "OK\tScan ok.") == NULL);
558 /* ----------------------------------------------------------------------- */
560 /* "kavdaemon" scanner type ------------------------------------------------ */
561 else if (strcmpic(scanner_name,US"kavdaemon") == 0) {
563 uschar kav_options_buffer[1024];
564 uschar kav_options_default[] = "/var/run/AvpCtl";
565 struct sockaddr_un server;
569 uschar scanrequest[1024];
570 uschar kav_match_string[128];
572 unsigned long kav_reportlen, bread;
575 if ((kav_options = string_nextinlist(&av_scanner_work, &sep,
577 sizeof(kav_options_buffer))) == NULL) {
578 /* no options supplied, use default options */
579 kav_options = kav_options_default;
582 /* open the kavdaemon socket */
583 sock = socket(AF_UNIX, SOCK_STREAM, 0);
585 log_write(0, LOG_MAIN|LOG_PANIC,
586 "malware acl condition: can't open UNIX socket.");
589 server.sun_family = AF_UNIX;
590 Ustrcpy(server.sun_path, kav_options);
591 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
593 log_write(0, LOG_MAIN|LOG_PANIC,
594 "malware acl condition: unable to connect to kavdaemon UNIX socket (%s). errno=%d", kav_options, errno);
598 /* get current date and time, build scan request */
600 strftime(CS tmpbuf, sizeof(tmpbuf), "<0>%d %b %H:%M:%S:%%s/scan/%%s", localtime(&t));
601 snprintf(CS scanrequest, 1024,CS tmpbuf, spool_directory, message_id);
603 /* send scan request */
604 if (send(sock, scanrequest, Ustrlen(scanrequest)+1, 0) < 0) {
606 log_write(0, LOG_MAIN|LOG_PANIC,
607 "malware acl condition: unable to write to kavdaemon UNIX socket (%s)", kav_options);
611 /* wait for result */
612 if ((bread = recv(sock, tmpbuf, 2, 0) != 2)) {
614 log_write(0, LOG_MAIN|LOG_PANIC,
615 "malware acl condition: unable to read 2 bytes from kavdaemon socket.");
619 /* get errorcode from one nibble */
620 if (test_byte_order() == LITTLE_MY_ENDIAN) {
621 kav_rc = tmpbuf[0] & 0x0F;
624 kav_rc = tmpbuf[1] & 0x0F;
627 /* improper kavdaemon configuration */
628 if ( (kav_rc == 5) || (kav_rc == 6) ) {
630 log_write(0, LOG_MAIN|LOG_PANIC,
631 "malware acl condition: please reconfigure kavdaemon to NOT disinfect or remove infected files.");
637 log_write(0, LOG_MAIN|LOG_PANIC,
638 "malware acl condition: kavdaemon reported 'scanning not completed' (code 1).");
644 log_write(0, LOG_MAIN|LOG_PANIC,
645 "malware acl condition: kavdaemon reported 'kavdaemon damaged' (code 7).");
649 /* code 8 is not handled, since it is ambigous. It appears mostly on
650 bounces where part of a file has been cut off */
652 /* "virus found" return codes (2-4) */
653 if ((kav_rc > 1) && (kav_rc < 5)) {
656 /* setup default virus name */
657 Ustrcpy(malware_name_buffer,"unknown");
658 malware_name = malware_name_buffer;
660 if (test_byte_order() == LITTLE_MY_ENDIAN) {
661 report_flag = tmpbuf[1];
664 report_flag = tmpbuf[0];
667 /* read the report, if available */
668 if( report_flag == 1 ) {
669 /* read report size */
670 if ((bread = recv(sock, &kav_reportlen, 4, 0)) != 4) {
672 log_write(0, LOG_MAIN|LOG_PANIC,
673 "malware acl condition: cannot read report size from kavdaemon");
677 /* it's possible that avp returns av_buffer[1] == 1 but the
678 reportsize is 0 (!?) */
679 if (kav_reportlen > 0) {
680 /* set up match regex, depends on retcode */
682 Ustrcpy(kav_match_string, "suspicion:\\s*(.+?)\\s*$");
684 Ustrcpy(kav_match_string, "infected:\\s*(.+?)\\s*$");
686 kav_re = pcre_compile( CS kav_match_string,
688 (const char **)&rerror,
692 /* read report, linewise */
693 while (kav_reportlen > 0) {
698 while ( recv(sock, &tmpbuf[bread], 1, 0) == 1 ) {
700 if ( (tmpbuf[bread] == '\n') || (bread > 1021) ) break;
704 tmpbuf[bread] = '\0';
706 /* try matcher on the line, grab substring */
707 result = pcre_exec(kav_re, NULL, CS tmpbuf, Ustrlen(tmpbuf), 0, 0, ovector, 30);
709 pcre_copy_substring(CS tmpbuf, ovector, result, 1, CS malware_name_buffer, 255);
723 /* ----------------------------------------------------------------------- */
726 /* "cmdline" scanner type ------------------------------------------------ */
727 else if (strcmpic(scanner_name,US"cmdline") == 0) {
728 uschar *cmdline_scanner;
729 uschar cmdline_scanner_buffer[1024];
730 uschar *cmdline_trigger;
731 uschar cmdline_trigger_buffer[1024];
732 const pcre *cmdline_trigger_re;
733 uschar *cmdline_regex;
734 uschar cmdline_regex_buffer[1024];
735 const pcre *cmdline_regex_re;
736 uschar file_name[1024];
737 uschar commandline[1024];
738 void (*eximsigchld)(int);
739 void (*eximsigpipe)(int);
740 FILE *scanner_out = NULL;
741 FILE *scanner_record = NULL;
742 uschar linebuffer[32767];
747 /* find scanner command line */
748 if ((cmdline_scanner = string_nextinlist(&av_scanner_work, &sep,
749 cmdline_scanner_buffer,
750 sizeof(cmdline_scanner_buffer))) == NULL) {
751 /* no command line supplied */
752 log_write(0, LOG_MAIN|LOG_PANIC,
753 "malware acl condition: missing commandline specification for cmdline scanner type.");
757 /* find scanner output trigger */
758 if ((cmdline_trigger = string_nextinlist(&av_scanner_work, &sep,
759 cmdline_trigger_buffer,
760 sizeof(cmdline_trigger_buffer))) == NULL) {
761 /* no trigger regex supplied */
762 log_write(0, LOG_MAIN|LOG_PANIC,
763 "malware acl condition: missing trigger specification for cmdline scanner type.");
767 /* precompile trigger regex */
768 cmdline_trigger_re = pcre_compile(CS cmdline_trigger, PCRE_COPT, (const char **)&rerror, &roffset, NULL);
769 if (cmdline_trigger_re == NULL) {
770 log_write(0, LOG_MAIN|LOG_PANIC,
771 "malware acl condition: regular expression error in '%s': %s at offset %d", cmdline_trigger_re, rerror, roffset);
775 /* find scanner name regex */
776 if ((cmdline_regex = string_nextinlist(&av_scanner_work, &sep,
777 cmdline_regex_buffer,
778 sizeof(cmdline_regex_buffer))) == NULL) {
779 /* no name regex supplied */
780 log_write(0, LOG_MAIN|LOG_PANIC,
781 "malware acl condition: missing virus name regex specification for cmdline scanner type.");
785 /* precompile name regex */
786 cmdline_regex_re = pcre_compile(CS cmdline_regex, PCRE_COPT, (const char **)&rerror, &roffset, NULL);
787 if (cmdline_regex_re == NULL) {
788 log_write(0, LOG_MAIN|LOG_PANIC,
789 "malware acl condition: regular expression error in '%s': %s at offset %d", cmdline_regex_re, rerror, roffset);
793 /* prepare scanner call */
794 snprintf(CS file_name,1024,"%s/scan/%s", spool_directory, message_id);
795 snprintf(CS commandline,1024, CS cmdline_scanner,file_name);
796 /* redirect STDERR too */
797 Ustrcat(commandline," 2>&1");
799 /* store exims signal handlers */
800 eximsigchld = signal(SIGCHLD,SIG_DFL);
801 eximsigpipe = signal(SIGPIPE,SIG_DFL);
803 scanner_out = popen(CS commandline,"r");
804 if (scanner_out == NULL) {
805 log_write(0, LOG_MAIN|LOG_PANIC,
806 "malware acl condition: calling cmdline scanner (%s) failed: %s.", commandline, strerror(errno));
807 signal(SIGCHLD,eximsigchld);
808 signal(SIGPIPE,eximsigpipe);
812 snprintf(CS file_name,1024,"%s/scan/%s/%s_scanner_output", spool_directory, message_id, message_id);
813 scanner_record = fopen(CS file_name,"w");
815 if (scanner_record == NULL) {
816 log_write(0, LOG_MAIN|LOG_PANIC,
817 "malware acl condition: opening scanner output file (%s) failed: %s.", file_name, strerror(errno));
819 signal(SIGCHLD,eximsigchld);
820 signal(SIGPIPE,eximsigpipe);
824 /* look for trigger while recording output */
825 while(fgets(CS linebuffer,32767,scanner_out) != NULL) {
826 if ( Ustrlen(linebuffer) > fwrite(linebuffer, 1, Ustrlen(linebuffer), scanner_record) ) {
828 log_write(0, LOG_MAIN|LOG_PANIC,
829 "malware acl condition: short write on scanner output file (%s).", file_name);
831 signal(SIGCHLD,eximsigchld);
832 signal(SIGPIPE,eximsigpipe);
835 /* try trigger match */
836 if (!trigger && regex_match_and_setup(cmdline_trigger_re, linebuffer, 0, -1))
840 fclose(scanner_record);
842 signal(SIGCHLD,eximsigchld);
843 signal(SIGPIPE,eximsigpipe);
846 /* setup default virus name */
847 Ustrcpy(malware_name_buffer,"unknown");
848 malware_name = malware_name_buffer;
850 /* re-open the scanner output file, look for name match */
851 scanner_record = fopen(CS file_name,"r");
852 while(fgets(CS linebuffer,32767,scanner_record) != NULL) {
854 result = pcre_exec(cmdline_regex_re, NULL, CS linebuffer, Ustrlen(linebuffer), 0, 0, ovector, 30);
856 pcre_copy_substring(CS linebuffer, ovector, result, 1, CS malware_name_buffer, 255);
859 fclose(scanner_record);
866 /* ----------------------------------------------------------------------- */
869 /* "sophie" scanner type ------------------------------------------------- */
870 else if (strcmpic(scanner_name,US"sophie") == 0) {
871 uschar *sophie_options;
872 uschar sophie_options_buffer[1024];
873 uschar sophie_options_default[] = "/var/run/sophie";
875 struct sockaddr_un server;
877 uschar file_name[1024];
878 uschar av_buffer[1024];
880 if ((sophie_options = string_nextinlist(&av_scanner_work, &sep,
881 sophie_options_buffer,
882 sizeof(sophie_options_buffer))) == NULL) {
883 /* no options supplied, use default options */
884 sophie_options = sophie_options_default;
887 /* open the sophie socket */
888 sock = socket(AF_UNIX, SOCK_STREAM, 0);
890 log_write(0, LOG_MAIN|LOG_PANIC,
891 "malware acl condition: can't open UNIX socket.");
894 server.sun_family = AF_UNIX;
895 Ustrcpy(server.sun_path, sophie_options);
896 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
898 log_write(0, LOG_MAIN|LOG_PANIC,
899 "malware acl condition: unable to connect to sophie UNIX socket (%s). errno=%d", sophie_options, errno);
903 /* pass the scan directory to sophie */
904 snprintf(CS file_name,1024,"%s/scan/%s", spool_directory, message_id);
905 if (write(sock, file_name, Ustrlen(file_name)) < 0) {
907 log_write(0, LOG_MAIN|LOG_PANIC,
908 "malware acl condition: unable to write to sophie UNIX socket (%s)", sophie_options);
912 write(sock, "\n", 1);
914 /* wait for result */
915 memset(av_buffer, 0, sizeof(av_buffer));
916 if ((!(bread = read(sock, av_buffer, sizeof(av_buffer))) > 0)) {
918 log_write(0, LOG_MAIN|LOG_PANIC,
919 "malware acl condition: unable to read from sophie UNIX socket (%s)", sophie_options);
926 if (av_buffer[0] == '1') {
927 if (Ustrchr(av_buffer, '\n')) *Ustrchr(av_buffer, '\n') = '\0';
928 Ustrcpy(malware_name_buffer,&av_buffer[2]);
929 malware_name = malware_name_buffer;
931 else if (!strncmp(CS av_buffer, "-1", 2)) {
932 log_write(0, LOG_MAIN|LOG_PANIC,
933 "malware acl condition: malware acl condition: sophie reported error");
937 /* all ok, no virus */
941 /* ----------------------------------------------------------------------- */
944 /* "clamd" scanner type ------------------------------------------------- */
945 /* This code was contributed by David Saez <david@ols.es> */
946 else if (strcmpic(scanner_name,US"clamd") == 0) {
947 uschar *clamd_options;
948 uschar clamd_options_buffer[1024];
949 uschar clamd_options_default[] = "/tmp/clamd";
951 struct sockaddr_un server;
952 int sock,port,bread=0;
953 uschar file_name[1024];
954 uschar av_buffer[1024];
955 uschar hostname[256];
959 if ((clamd_options = string_nextinlist(&av_scanner_work, &sep,
960 clamd_options_buffer,
961 sizeof(clamd_options_buffer))) == NULL) {
962 /* no options supplied, use default options */
963 clamd_options = clamd_options_default;
966 /* socket does not start with '/' -> network socket */
967 if (*clamd_options != '/') {
969 /* extract host and port part */
970 if( sscanf(CS clamd_options, "%s %u", hostname, &port) != 2 ) {
971 log_write(0, LOG_MAIN|LOG_PANIC,
972 "malware acl condition: clamd: invalid socket '%s'", clamd_options);
976 /* Lookup the host */
977 if((he = gethostbyname(CS hostname)) == 0) {
978 log_write(0, LOG_MAIN|LOG_PANIC,
979 "malware acl condition: clamd: failed to lookup host '%s'", hostname);
983 in = *(struct in_addr *) he->h_addr_list[0];
985 /* Open the ClamAV Socket */
986 if ( (sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
987 log_write(0, LOG_MAIN|LOG_PANIC,
988 "malware acl condition: clamd: unable to acquire socket (%s)",
993 if (ip_connect(sock, AF_INET, (uschar*)inet_ntoa(in), port, 5) < 0) {
995 log_write(0, LOG_MAIN|LOG_PANIC,
996 "malware acl condition: clamd: connection to %s, port %u failed (%s)",
997 inet_ntoa(in), port, strerror(errno));
1002 /* open the local socket */
1003 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
1004 log_write(0, LOG_MAIN|LOG_PANIC,
1005 "malware acl condition: clamd: unable to acquire socket (%s)",
1010 server.sun_family = AF_UNIX;
1011 Ustrcpy(server.sun_path, clamd_options);
1013 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
1015 log_write(0, LOG_MAIN|LOG_PANIC,
1016 "malware acl condition: clamd: unable to connect to UNIX socket %s (%s)",
1017 clamd_options, strerror(errno) );
1022 /* Pass the string to ClamAV (7 = "SCAN \n" + \0) */
1024 snprintf(CS file_name,1024,"SCAN %s/scan/%s\n", spool_directory, message_id);
1026 if (send(sock, file_name, Ustrlen(file_name), 0) < 0) {
1028 log_write(0, LOG_MAIN|LOG_PANIC,"malware acl condition: clamd: unable to write to socket (%s)",
1034 We're done sending, close socket for writing.
1036 One user reported that clamd 0.70 does not like this any more ...
1040 /* shutdown(sock, SHUT_WR); */
1042 /* Read the result */
1043 memset(av_buffer, 0, sizeof(av_buffer));
1044 bread = read(sock, av_buffer, sizeof(av_buffer));
1048 log_write(0, LOG_MAIN|LOG_PANIC,
1049 "malware acl condition: clamd: unable to read from socket (%s)",
1054 if (bread == sizeof(av_buffer)) {
1055 log_write(0, LOG_MAIN|LOG_PANIC,
1056 "malware acl condition: clamd: buffer too small");
1060 /* Check the result. ClamAV Returns
1061 infected: -> "<filename>: <virusname> FOUND"
1062 not-infected: -> "<filename>: OK"
1063 error: -> "<filename>: <errcode> ERROR */
1065 if (!(*av_buffer)) {
1066 log_write(0, LOG_MAIN|LOG_PANIC,
1067 "malware acl condition: clamd: ClamAV returned null");
1071 /* colon in returned output? */
1072 if((p = Ustrrchr(av_buffer,':')) == NULL) {
1073 log_write(0, LOG_MAIN|LOG_PANIC,
1074 "malware acl condition: clamd: ClamAV returned malformed result: %s",
1079 /* strip filename strip CR at the end */
1081 while (*p == ' ') ++p;
1083 p = vname + Ustrlen(vname) - 1;
1084 if( *p == '\n' ) *p = '\0';
1086 if ((p = Ustrstr(vname, "FOUND"))!=NULL) {
1088 for (--p;p>vname && *p<=32;p--) *p=0;
1089 for (;*vname==32;vname++);
1090 Ustrcpy(malware_name_buffer,vname);
1091 malware_name = malware_name_buffer;
1094 if (Ustrstr(vname, "ERROR")!=NULL) {
1095 /* ClamAV reports ERROR
1097 for (;*vname!='\n' && vname>av_buffer; vname--);
1098 if (*vname=='\n') vname++;
1100 log_write(0, LOG_MAIN|LOG_PANIC,
1101 "malware acl condition: clamd: ClamAV returned %s",vname);
1105 /* Everything should be OK */
1106 malware_name = NULL;
1110 /* ----------------------------------------------------------------------- */
1113 /* "mksd" scanner type --------------------------------------------------- */
1114 else if (strcmpic(scanner_name,US"mksd") == 0) {
1115 uschar *mksd_options;
1116 char *mksd_options_end;
1117 uschar mksd_options_buffer[32];
1118 int mksd_maxproc = 1; /* default, if no option supplied */
1119 struct sockaddr_un server;
1123 if ((mksd_options = string_nextinlist(&av_scanner_work, &sep,
1124 mksd_options_buffer,
1125 sizeof(mksd_options_buffer))) != NULL) {
1126 mksd_maxproc = (int) strtol(CS mksd_options, &mksd_options_end, 10);
1127 if ((*mksd_options == '\0') || (*mksd_options_end != '\0') ||
1128 (mksd_maxproc < 1) || (mksd_maxproc > 32)) {
1129 log_write(0, LOG_MAIN|LOG_PANIC,
1130 "malware acl condition: mksd: invalid option '%s'", mksd_options);
1135 /* open the mksd socket */
1136 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1138 log_write(0, LOG_MAIN|LOG_PANIC,
1139 "malware acl condition: can't open UNIX socket.");
1142 server.sun_family = AF_UNIX;
1143 Ustrcpy(server.sun_path, "/var/run/mksd/socket");
1144 if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
1146 log_write(0, LOG_MAIN|LOG_PANIC,
1147 "malware acl condition: unable to connect to mksd UNIX socket (/var/run/mksd/socket). errno=%d", errno);
1151 malware_name = NULL;
1153 /* choose the appropriate scan routine */
1154 retval = demime_ok ?
1155 mksd_scan_unpacked(sock, mksd_maxproc) :
1156 mksd_scan_packed(sock);
1161 /* ----------------------------------------------------------------------- */
1163 /* "unknown" scanner type ------------------------------------------------- */
1165 log_write(0, LOG_MAIN|LOG_PANIC,
1166 "malware condition: unknown scanner type '%s'", scanner_name);
1169 /* ----------------------------------------------------------------------- */
1171 /* set "been here, done that" marker */
1175 /* match virus name against pattern (caseless ------->----------v) */
1176 if ( (malware_name != NULL) &&
1177 (regex_match_and_setup(re, malware_name, 0, -1)) ) {
1186 /* simple wrapper for reading lines from sockets */
1187 int recv_line(int sock, uschar *buffer, int size) {
1190 memset(buffer,0,size);
1192 while(recv(sock,p,1,0) > -1) {
1193 if ((p-buffer) > (size-2)) break;
1194 if (*p == '\n') break;
1195 if (*p != '\r') p++;
1203 /* ============= private routines for the "mksd" scanner type ============== */
1205 #include <sys/uio.h>
1207 int mksd_writev (int sock, struct iovec *iov, int iovcnt)
1213 i = writev (sock, iov, iovcnt);
1214 while ((i < 0) && (errno == EINTR));
1217 log_write(0, LOG_MAIN|LOG_PANIC,
1218 "malware acl condition: unable to write to mksd UNIX socket (/var/run/mksd/socket)");
1223 if (i >= iov->iov_len) {
1230 iov->iov_base = CS iov->iov_base + i;
1236 int mksd_read_lines (int sock, uschar *av_buffer, int av_buffer_size)
1242 if ((i = recv (sock, av_buffer+offset, av_buffer_size-offset, 0)) <= 0) {
1244 log_write(0, LOG_MAIN|LOG_PANIC,
1245 "malware acl condition: unable to read from mksd UNIX socket (/var/run/mksd/socket)");
1250 /* offset == av_buffer_size -> buffer full */
1251 if (offset == av_buffer_size) {
1253 log_write(0, LOG_MAIN|LOG_PANIC,
1254 "malware acl condition: malformed reply received from mksd");
1257 } while (av_buffer[offset-1] != '\n');
1259 av_buffer[offset] = '\0';
1263 int mksd_parse_line (char *line)
1274 if ((p = strchr (line, '\n')) != NULL)
1276 log_write(0, LOG_MAIN|LOG_PANIC,
1277 "malware acl condition: mksd scanner failed: %s", line);
1281 if ((p = strchr (line, '\n')) != NULL) {
1283 if (((p-line) > 5) && ((p-line) < sizeof (malware_name_buffer)) && (line[3] == ' '))
1284 if (((p = strchr (line+4, ' ')) != NULL) && ((p-line) > 4)) {
1286 Ustrcpy (malware_name_buffer, line+4);
1287 malware_name = malware_name_buffer;
1291 log_write(0, LOG_MAIN|LOG_PANIC,
1292 "malware acl condition: malformed reply received from mksd: %s", line);
1297 int mksd_scan_packed (int sock)
1299 struct iovec iov[7];
1300 char *cmd = "MSQ/scan/.eml\n";
1301 uschar av_buffer[1024];
1303 iov[0].iov_base = cmd;
1305 iov[1].iov_base = CS spool_directory;
1306 iov[1].iov_len = Ustrlen (spool_directory);
1307 iov[2].iov_base = cmd + 3;
1309 iov[3].iov_base = iov[5].iov_base = CS message_id;
1310 iov[3].iov_len = iov[5].iov_len = Ustrlen (message_id);
1311 iov[4].iov_base = cmd + 3;
1313 iov[6].iov_base = cmd + 9;
1316 if (mksd_writev (sock, iov, 7) < 0)
1319 if (mksd_read_lines (sock, av_buffer, sizeof (av_buffer)) < 0)
1324 return mksd_parse_line (CS av_buffer);
1327 int mksd_scan_unpacked (int sock, int maxproc)
1329 struct iovec iov[5];
1330 char *cmd = "\nSQ/";
1332 struct dirent *entry;
1336 uschar mbox_name[1024];
1337 uschar unpackdir[1024];
1338 uschar av_buffer[16384];
1340 snprintf (CS mbox_name, sizeof (mbox_name), "%s.eml", CS message_id);
1341 snprintf (CS unpackdir, sizeof (unpackdir), "%s/scan/%s", CS spool_directory, CS message_id);
1343 if ((unpdir = opendir (CS unpackdir)) == NULL) {
1345 log_write(0, LOG_MAIN|LOG_PANIC,
1346 "malware acl condition: unable to scan spool directory");
1350 iov[0].iov_base = cmd;
1352 iov[1].iov_base = CS unpackdir;
1353 iov[1].iov_len = Ustrlen (unpackdir);
1354 iov[2].iov_base = cmd + 3;
1356 iov[4].iov_base = cmd;
1360 while ((unpdir != NULL) || (pending > 0)) {
1363 while ((pending < maxproc) && (unpdir != NULL)) {
1364 if ((entry = readdir (unpdir)) != NULL) {
1365 if ((Ustrcmp (entry->d_name, ".") != 0) &&
1366 (Ustrcmp (entry->d_name, "..") != 0) &&
1367 (Ustrcmp (entry->d_name, mbox_name) != 0)) {
1368 iov[3].iov_base = entry->d_name;
1369 iov[3].iov_len = strlen (entry->d_name);
1370 if (mksd_writev (sock, iov, 5) < 0) {
1374 iov[0].iov_base = cmd + 1;
1384 /* read and parse */
1386 if ((offset = mksd_read_lines (sock, av_buffer, sizeof (av_buffer))) < 0) {
1393 if (((i = mksd_parse_line (CS line)) != OK) || (malware_name != NULL)) {
1400 if ((line = Ustrchr (line, '\n')) == NULL) {
1404 log_write(0, LOG_MAIN|LOG_PANIC,
1405 "malware acl condition: unterminated line received from mksd");
1408 } while (++line != (av_buffer + offset));