1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
6 * Copyright (c) The Exim Maintainers 2015 - 2022
7 * Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003 - 2015
11 /* Code for calling virus (malware) scanners. Called from acl.c. */
14 #ifdef WITH_CONTENT_SCAN /* entire file */
17 #ifndef DISABLE_MAL_FFROTD
20 #ifndef DISABLE_MAL_FFROT6D
23 #ifndef DISABLE_MAL_DRWEB
26 #ifndef DISABLE_MAL_AVE
29 #ifndef DISABLE_MAL_FSECURE
32 #ifndef DISABLE_MAL_KAV
35 #ifndef DISABLE_MAL_SOPHIE
38 #ifndef DISABLE_MAL_CLAM
41 #ifndef DISABLE_MAL_MKS
44 #ifndef DISABLE_MAL_AVAST
47 #ifndef DISABLE_MAL_SOCK
50 #ifndef DISABLE_MAL_CMDLINE
55 typedef enum {MC_NONE, MC_TCP, MC_UNIX, MC_STRM} contype_t;
60 const uschar * options_default;
64 #ifndef DISABLE_MAL_FFROTD
65 { M_FPROTD, US"f-protd", US"localhost 10200-10204", MC_TCP },
67 #ifndef DISABLE_MAL_FFROT6D
68 { M_FPROT6D, US"f-prot6d", US"localhost 10200", MC_TCP },
70 #ifndef DISABLE_MAL_DRWEB
71 { M_DRWEB, US"drweb", US"/usr/local/drweb/run/drwebd.sock", MC_STRM },
73 #ifndef DISABLE_MAL_AVE
74 { M_AVES, US"aveserver", US"/var/run/aveserver", MC_UNIX },
76 #ifndef DISABLE_MAL_FSECURE
77 { M_FSEC, US"fsecure", US"/var/run/.fsav", MC_UNIX },
79 #ifndef DISABLE_MAL_KAV
80 { M_KAVD, US"kavdaemon", US"/var/run/AvpCtl", MC_UNIX },
82 #ifndef DISABLE_MAL_SOPHIE
83 { M_SOPHIE, US"sophie", US"/var/run/sophie", MC_UNIX },
85 #ifndef DISABLE_MAL_CLAM
86 { M_CLAMD, US"clamd", US"/tmp/clamd", MC_NONE },
88 #ifndef DISABLE_MAL_MKS
89 { M_MKSD, US"mksd", NULL, MC_NONE },
91 #ifndef DISABLE_MAL_AVAST
92 { M_AVAST, US"avast", US"/var/run/avast/scan.sock", MC_STRM },
94 #ifndef DISABLE_MAL_SOCK
95 { M_SOCK, US"sock", US"/tmp/malware.sock", MC_STRM },
97 #ifndef DISABLE_MAL_CMDLINE
98 { M_CMDL, US"cmdline", NULL, MC_NONE },
100 { -1, NULL, NULL, MC_NONE } /* end-marker */
103 /******************************************************************************/
104 # ifdef MACRO_PREDEF /* build solely to predefine macros */
106 # include "macro_predef.h"
109 features_malware(void)
113 uschar buf[EXIM_DRIVERNAME_MAX];
115 spf(buf, sizeof(buf), US"_HAVE_MALWARE_");
117 for (const struct scan * sc = m_scans; sc->scancode != -1; sc++)
119 for (s = sc->name, t = buf+14; *s; s++) if (*s != '-')
122 builtin_macro_create(buf);
126 /******************************************************************************/
127 # else /*!MACRO_PREDEF, main build*/
130 #define MALWARE_TIMEOUT 120 /* default timeout, seconds */
132 static const uschar * malware_regex_default = US ".+";
133 static const pcre2_code * malware_default_re = NULL;
136 #ifndef DISABLE_MAL_CLAM
137 /* The maximum number of clamd servers that are supported in the configuration */
138 # define MAX_CLAMD_SERVERS 32
139 # define MAX_CLAMD_SERVERS_S "32"
141 typedef struct clamd_address {
149 #ifndef DISABLE_MAL_DRWEB
150 # define DRWEBD_SCAN_CMD (1) /* scan file, buffer or diskfile */
151 # define DRWEBD_RETURN_VIRUSES (1<<0) /* ask daemon return to us viruses names from report */
152 # define DRWEBD_IS_MAIL (1<<19) /* say to daemon that format is "archive MAIL" */
154 # define DERR_READ_ERR (1<<0) /* read error */
155 # define DERR_NOMEMORY (1<<2) /* no memory */
156 # define DERR_TIMEOUT (1<<9) /* scan timeout has run out */
157 # define DERR_BAD_CALL (1<<15) /* wrong command */
159 static const uschar * drweb_re_str = US "infected\\swith\\s*(.+?)$";
160 static const pcre2_code * drweb_re = NULL;
163 #ifndef DISABLE_MAL_FSECURE
164 static const uschar * fsec_re_str = US "\\S{0,5}INFECTED\\t[^\\t]*\\t([^\\t]+)\\t\\S*$";
165 static const pcre2_code * fsec_re = NULL;
168 #ifndef DISABLE_MAL_KAV
169 static const uschar * kav_re_sus_str = US "suspicion:\\s*(.+?)\\s*$";
170 static const uschar * kav_re_inf_str = US "infected:\\s*(.+?)\\s*$";
171 static const pcre2_code * kav_re_sus = NULL;
172 static const pcre2_code * kav_re_inf = NULL;
175 #ifndef DISABLE_MAL_AVAST
176 static const uschar * ava_re_clean_str = US "(?!\\\\)\\t\\[\\+\\]";
177 static const uschar * ava_re_virus_str = US "(?!\\\\)\\t\\[L\\]\\d+\\.0\\t0\\s(.*)";
178 static const uschar * ava_re_error_str = US "(?!\\\\)\\t\\[E\\]\\d+\\.0\\tError\\s\\d+\\s(.*)";
179 static const pcre2_code * ava_re_clean = NULL;
180 static const pcre2_code * ava_re_virus = NULL;
181 static const pcre2_code * ava_re_error = NULL;
184 #ifndef DISABLE_MAL_FFROT6D
185 static const uschar * fprot6d_re_error_str = US "^\\d+\\s<(.+?)>$";
186 static const uschar * fprot6d_re_virus_str = US "^\\d+\\s<infected:\\s+(.+?)>\\s+.+$";
187 static const pcre2_code * fprot6d_re_error = NULL;
188 static const pcre2_code * fprot6d_re_virus = NULL;
193 /******************************************************************************/
195 #ifndef DISABLE_MAL_KAV
196 /* Routine to check whether a system is big- or little-endian.
197 Ripped from http://www.faqs.org/faqs/graphics/fileformats-faq/part4/section-7.html
198 Needed for proper kavdaemon implementation. Sigh. */
199 # define BIG_MY_ENDIAN 0
200 # define LITTLE_MY_ENDIAN 1
201 static int test_byte_order(void);
205 short int word = 0x0001;
206 char *byte = CS &word;
207 return(byte[0] ? LITTLE_MY_ENDIAN : BIG_MY_ENDIAN);
211 BOOL malware_ok = FALSE;
213 /* Gross hacks for the -bmalware option; perhaps we should just create
214 the scan directory normally for that case, but look into rigging up the
215 needed header variables if not already set on the command-line? */
216 extern int spool_mbox_ok;
217 extern uschar spooled_message_id[MESSAGE_ID_LENGTH+1];
220 /* Some (currently avast only) use backslash escaped whitespace,
221 this function undoes these escapes */
223 #ifndef DISABLE_MAL_AVAST
229 if (*p == '\\' && (isspace(p[1]) || p[1] == '\\'))
230 for (p0 = p; *p0; ++p0) *p0 = p0[1];
234 /* --- malware_*_defer --- */
236 malware_panic_defer(const uschar * str)
238 log_write(0, LOG_MAIN|LOG_PANIC, "malware acl condition: %s", str);
242 malware_log_defer(const uschar * str)
244 log_write(0, LOG_MAIN, "malware acl condition: %s", str);
247 /* --- m_*_defer --- */
249 m_panic_defer(struct scan * scanent, const uschar * hostport,
252 return malware_panic_defer(string_sprintf("%s %s : %s",
253 scanent->name, hostport ? hostport : CUS"", str));
255 /* --- m_*_defer_3 */
257 m_panic_defer_3(struct scan * scanent, const uschar * hostport,
258 const uschar * str, int fd_to_close)
260 DEBUG(D_acl) debug_print_socket(fd_to_close);
261 (void) close(fd_to_close);
262 return m_panic_defer(scanent, hostport, str);
265 /*************************************************/
267 #ifndef DISABLE_MAL_CLAM
268 /* Only used by the Clamav code, which is working from a list of servers and
269 uses the returned in_addr to get a second connection to the same system.
272 m_tcpsocket(const uschar * hostname, unsigned int port,
273 host_item * host, uschar ** errstr, const blob * fastopen_blob)
275 int fd = ip_connectedsocket(SOCK_STREAM, hostname, port, port, 5,
276 host, errstr, fastopen_blob);
277 #ifdef EXIM_TFO_FREEBSD
278 /* Under some fault conditions, FreeBSD 12.2 seen to send a (non-TFO) SYN
279 and, getting no response, wait for a long time. Impose a 5s max. */
281 (void) poll_one_fd(fd, POLLOUT, 5 * 1000);
288 m_sock_send(int sock, uschar * buf, int cnt, uschar ** errstr)
290 if (send(sock, buf, cnt, 0) < 0)
294 *errstr = string_sprintf("unable to send to socket (%s): %s",
301 static const pcre2_code *
302 m_pcre_compile(const uschar * re, BOOL cacheable, uschar ** errstr)
304 return regex_compile(re, cacheable ? MCS_CACHEABLE : MCS_NOFLAGS, errstr,
309 m_pcre_exec(const pcre2_code * cre, uschar * text)
311 pcre2_match_data * md = pcre2_match_data_create(2, pcre_gen_ctx);
312 int i = pcre2_match(cre, text, PCRE2_ZERO_TERMINATED, 0, 0, md, pcre_gen_mtc_ctx);
313 PCRE2_UCHAR * substr = NULL;
316 if (i >= 2) /* Got it */
318 pcre2_substring_get_bynumber(md, 1, &substr, &slen); /* uses same ctx as md */
319 if (!substr) substr = US"";
321 /* pcre2_match_data_free(md); gen ctx needs no free */
325 static const pcre2_code *
326 m_pcre_nextinlist(const uschar ** list, int * sep,
327 BOOL cacheable, char * listerr, uschar ** errstr)
329 const uschar * list_ele;
330 const pcre2_code * cre = NULL;
332 if (!(list_ele = string_nextinlist(list, sep, NULL, 0)))
333 *errstr = US listerr;
336 DEBUG(D_acl) debug_printf_indent("%15s%10s'%s'\n", "", "RE: ",
337 string_printing(list_ele));
338 cre = m_pcre_compile(CUS list_ele, cacheable, errstr);
345 Simple though inefficient wrapper for reading a line. Drop CRs and the
346 trailing newline. Can return early on buffer full. Null-terminate.
347 Apply initial timeout if no data ready.
349 Return: number of chars - zero for an empty line
351 -2 on timeout or error
354 recv_line(int fd, uschar * buffer, int bsize, time_t tmo)
360 if (!fd_ready(fd, tmo))
363 /*XXX tmo handling assumes we always get a whole line */
366 while ((rcv = read(fd, p, 1)) > 0)
369 if (p-buffer > bsize-2) break;
370 if (*p == '\n') break;
377 debug_printf_indent("Malware scan: read %s (%s)\n",
378 rcv==0 ? "EOF" : "error", strerror(errno));
379 debug_print_socket(fd);
381 return rcv==0 ? -1 : -2;
385 DEBUG(D_acl) debug_printf_indent("Malware scan: read '%s'\n", buffer);
389 /* return TRUE iff size as requested */
390 #ifndef DISABLE_MAL_DRWEB
392 recv_len(int sock, void * buf, int size, time_t tmo)
394 return fd_ready(sock, tmo)
395 ? recv(sock, buf, size, 0) == size
402 #ifndef DISABLE_MAL_MKS
403 /* ============= private routines for the "mksd" scanner type ============== */
405 # include <sys/uio.h>
408 mksd_writev (int sock, struct iovec * iov, int iovcnt)
415 i = writev (sock, iov, iovcnt);
416 while (i < 0 && errno == EINTR);
419 (void) malware_panic_defer(
420 US"unable to write to mksd UNIX socket (/var/run/mksd/socket)");
423 for (;;) /* check for short write */
424 if (i >= iov->iov_len)
434 iov->iov_base = CS iov->iov_base + i;
441 mksd_read_lines (int sock, uschar *av_buffer, int av_buffer_size, time_t tmo)
443 client_conn_ctx cctx = {.sock = sock};
449 i = ip_recv(&cctx, av_buffer+offset, av_buffer_size-offset, tmo);
452 (void) malware_panic_defer(US"unable to read from mksd UNIX socket (/var/run/mksd/socket)");
457 /* offset == av_buffer_size -> buffer full */
458 if (offset == av_buffer_size)
460 (void) malware_panic_defer(US"malformed reply received from mksd");
463 } while (av_buffer[offset-1] != '\n');
465 av_buffer[offset] = '\0';
470 mksd_parse_line(struct scan * scanent, char * line)
481 if ((p = strchr (line, '\n')) != NULL)
483 return m_panic_defer(scanent, NULL,
484 string_sprintf("scanner failed: %s", line));
487 if ((p = strchr (line, '\n')) != NULL)
492 && (p = strchr(line+4, ' ')) != NULL
497 malware_name = string_copy(US line+4);
501 return m_panic_defer(scanent, NULL,
502 string_sprintf("malformed reply received: %s", line));
507 mksd_scan_packed(struct scan * scanent, int sock, const uschar * scan_filename,
511 const char *cmd = "MSQ\n";
512 uschar av_buffer[1024];
514 iov[0].iov_base = (void *) cmd;
516 iov[1].iov_base = (void *) scan_filename;
517 iov[1].iov_len = Ustrlen(scan_filename);
518 iov[2].iov_base = (void *) (cmd + 3);
521 if (mksd_writev (sock, iov, 3) < 0)
524 if (mksd_read_lines (sock, av_buffer, sizeof (av_buffer), tmo) < 0)
527 return mksd_parse_line (scanent, CS av_buffer);
532 #ifndef DISABLE_MAL_CLAM
534 clamd_option(clamd_address * cd, const uschar * optstr, int * subsep)
539 while ((s = string_nextinlist(&optstr, subsep, NULL, 0)))
540 if (Ustrncmp(s, "retry=", 6) == 0)
542 int sec = readconf_readtime((s += 6), '\0', FALSE);
555 /*************************************************
556 * Scan content for malware *
557 *************************************************/
559 /* This is an internal interface for scanning an email; the normal interface
560 is via malware(), or there's malware_in_file() used for testing/debugging.
563 malware_re match condition for "malware="
564 cacheable the RE did not use any dynamic elements during expansion
565 scan_filename the file holding the email to be scanned, if we're faking
566 this up for the -bmalware test, else NULL
567 timeout if nonzero, non-default timeoutl
569 Returns: Exim message processing code (OK, FAIL, DEFER, ...)
570 where true means malware was found (condition applies)
573 malware_internal(const uschar * malware_re, BOOL cacheable,
574 const uschar * scan_filename, int timeout)
577 const uschar *av_scanner_work = av_scanner;
578 BOOL av_scanner_textonly;
579 uschar *scanner_name;
580 unsigned long mbox_size;
582 const pcre2_code *re;
584 struct scan * scanent;
585 const uschar * scanner_options;
586 client_conn_ctx malware_daemon_ctx = {.sock = -1};
588 uschar * eml_filename, * eml_dir;
591 return FAIL; /* empty means "don't match anything" */
593 /* Ensure the eml mbox file is spooled up */
595 if (!(mbox_file = spool_mbox(&mbox_size, scan_filename, &eml_filename)))
596 return malware_panic_defer(US"error while creating mbox spool file");
598 /* None of our current scanners need the mbox file as a stream (they use
599 the name), so we can close it right away. Get the directory too. */
601 (void) fclose(mbox_file);
602 eml_dir = string_copyn(eml_filename, Ustrrchr(eml_filename, '/') - eml_filename);
604 /* parse 1st option */
605 if (strcmpic(malware_re, US"false") == 0 || Ustrcmp(malware_re, "0") == 0)
606 return FAIL; /* explicitly no matching */
608 /* special cases (match anything except empty) */
609 if ( strcmpic(malware_re, US"true") == 0
610 || Ustrcmp(malware_re, "*") == 0
611 || Ustrcmp(malware_re, "1") == 0
614 if ( !malware_default_re
615 && !(malware_default_re = m_pcre_compile(malware_regex_default, FALSE, &errstr)))
616 return malware_panic_defer(errstr);
617 malware_re = malware_regex_default;
618 re = malware_default_re;
621 /* compile the regex, see if it works */
622 else if (!(re = m_pcre_compile(malware_re, cacheable, &errstr)))
623 return malware_panic_defer(errstr);
625 /* if av_scanner starts with a dollar, expand it first */
626 if (*av_scanner == '$')
628 if (!(av_scanner_work = expand_string_2(av_scanner, &av_scanner_textonly)))
629 return malware_panic_defer(
630 string_sprintf("av_scanner starts with $, but expansion failed: %s",
631 expand_string_message));
634 debug_printf_indent("Expanded av_scanner global: %s\n", av_scanner_work);
635 /* disable result caching in this case */
640 av_scanner_textonly = TRUE;
642 /* Do not scan twice (unless av_scanner is dynamic). */
645 /* find the scanner type from the av_scanner option */
646 if (!(scanner_name = string_nextinlist(&av_scanner_work, &sep, NULL, 0)))
647 return malware_panic_defer(US"av_scanner configuration variable is empty");
648 if (!timeout) timeout = MALWARE_TIMEOUT;
649 tmo = time(NULL) + timeout;
651 for (scanent = m_scans; ; scanent++)
654 return malware_panic_defer(string_sprintf("unknown scanner type '%s'",
656 if (strcmpic(scanner_name, US scanent->name) != 0)
658 DEBUG(D_acl) debug_printf_indent("Malware scan: %s tmo=%s\n",
659 scanner_name, readconf_printtime(timeout));
661 if (!(scanner_options = string_nextinlist(&av_scanner_work, &sep, NULL, 0)))
662 scanner_options = scanent->options_default;
663 if (scanent->conn == MC_NONE)
666 DEBUG(D_acl) debug_printf_indent("%15s%10s%s\n", "", "socket: ", scanner_options);
667 switch(scanent->conn)
670 malware_daemon_ctx.sock = ip_tcpsocket(scanner_options, &errstr, 5, NULL); break;
672 malware_daemon_ctx.sock = ip_unixsocket(scanner_options, &errstr); break;
674 malware_daemon_ctx.sock = ip_streamsocket(scanner_options, &errstr, 5, NULL); break;
676 /* compiler quietening */ break;
678 if (malware_daemon_ctx.sock < 0)
679 return m_panic_defer(scanent, CUS callout_address, errstr);
683 switch (scanent->scancode)
685 #ifndef DISABLE_MAL_FFROTD
686 case M_FPROTD: /* "f-protd" scanner type -------------------------------- */
688 uschar *fp_scan_option;
689 unsigned int detected=0, par_count=0;
690 uschar * scanrequest;
691 uschar buf[32768], *strhelper, *strhelper2;
692 uschar * malware_name_internal = NULL;
695 scanrequest = string_sprintf("GET %s", eml_filename);
697 while ((fp_scan_option = string_nextinlist(&av_scanner_work, &sep,
700 scanrequest = string_sprintf("%s%s%s", scanrequest,
701 par_count ? "%20" : "?", fp_scan_option);
704 scanrequest = string_sprintf("%s HTTP/1.0\r\n\r\n", scanrequest);
705 DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s: %s\n",
706 scanner_name, scanrequest);
708 /* send scan request */
709 if (m_sock_send(malware_daemon_ctx.sock, scanrequest, Ustrlen(scanrequest)+1, &errstr) < 0)
710 return m_panic_defer(scanent, CUS callout_address, errstr);
712 while ((len = recv_line(malware_daemon_ctx.sock, buf, sizeof(buf), tmo)) >= 0)
715 if (Ustrstr(buf, US"<detected type=\"") != NULL)
717 else if (detected && (strhelper = Ustrstr(buf, US"<name>")))
719 if ((strhelper2 = Ustrstr(buf, US"</name>")) != NULL)
722 malware_name_internal = string_copy(strhelper+6);
725 else if (Ustrstr(buf, US"<summary code=\""))
727 malware_name = Ustrstr(buf, US"<summary code=\"11\">")
728 ? malware_name_internal : NULL;
734 (void)close(malware_daemon_ctx.sock);
741 #ifndef DISABLE_MAL_FFROT6D
742 case M_FPROT6D: /* "f-prot6d" scanner type ----------------------------------- */
745 uschar * e, * linebuffer, * scanrequest;
746 uschar av_buffer[1024];
748 if ((!fprot6d_re_virus && !(fprot6d_re_virus = m_pcre_compile(fprot6d_re_virus_str, FALSE, &errstr)))
749 || (!fprot6d_re_error && !(fprot6d_re_error = m_pcre_compile(fprot6d_re_error_str, FALSE, &errstr))))
750 return malware_panic_defer(errstr);
752 scanrequest = string_sprintf("SCAN FILE %s\n", eml_filename);
753 DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s: %s\n",
754 scanner_name, scanrequest);
756 if (m_sock_send(malware_daemon_ctx.sock, scanrequest, Ustrlen(scanrequest), &errstr) < 0)
757 return m_panic_defer(scanent, CUS callout_address, errstr);
759 bread = ip_recv(&malware_daemon_ctx, av_buffer, sizeof(av_buffer), tmo);
762 return m_panic_defer_3(scanent, CUS callout_address,
763 string_sprintf("unable to read from socket (%s)", strerror(errno)),
764 malware_daemon_ctx.sock);
766 if (bread == sizeof(av_buffer))
767 return m_panic_defer_3(scanent, CUS callout_address,
768 US"buffer too small", malware_daemon_ctx.sock);
770 av_buffer[bread] = '\0';
771 linebuffer = string_copy(av_buffer);
773 m_sock_send(malware_daemon_ctx.sock, US"QUIT\n", 5, 0);
775 if ((e = m_pcre_exec(fprot6d_re_error, linebuffer)))
776 return m_panic_defer_3(scanent, CUS callout_address,
777 string_sprintf("scanner reported error (%s)", e), malware_daemon_ctx.sock);
779 if (!(malware_name = m_pcre_exec(fprot6d_re_virus, linebuffer)))
786 #ifndef DISABLE_MAL_DRWEB
787 case M_DRWEB: /* "drweb" scanner type ----------------------------------- */
788 /* v0.1 - added support for tcp sockets */
789 /* v0.0 - initial release -- support for unix sockets */
793 unsigned int fsize_uint;
794 uschar * tmpbuf, *drweb_fbuf;
795 int drweb_rc, drweb_cmd, drweb_flags = 0x0000, drweb_fd,
796 drweb_vnum, drweb_slen, drweb_fin = 0x0000;
798 /* prepare variables */
799 drweb_cmd = htonl(DRWEBD_SCAN_CMD);
800 drweb_flags = htonl(DRWEBD_RETURN_VIRUSES | DRWEBD_IS_MAIL);
802 if (*scanner_options != '/')
805 if ((drweb_fd = exim_open2(CCS eml_filename, O_RDONLY)) == -1)
806 return m_panic_defer_3(scanent, NULL,
807 string_sprintf("can't open spool file %s: %s",
808 eml_filename, strerror(errno)),
809 malware_daemon_ctx.sock);
811 if ((fsize = lseek(drweb_fd, 0, SEEK_END)) == -1)
814 badseek: err = errno;
815 (void)close(drweb_fd);
816 return m_panic_defer_3(scanent, NULL,
817 string_sprintf("can't seek spool file %s: %s",
818 eml_filename, strerror(err)),
819 malware_daemon_ctx.sock);
821 fsize_uint = (unsigned int) fsize;
822 if ((off_t)fsize_uint != fsize)
824 (void)close(drweb_fd);
825 return m_panic_defer_3(scanent, NULL,
826 string_sprintf("seeking spool file %s, size overflow",
828 malware_daemon_ctx.sock);
830 drweb_slen = htonl(fsize);
831 if (lseek(drweb_fd, 0, SEEK_SET) < 0)
834 DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s remote scan [%s]\n",
835 scanner_name, scanner_options);
837 /* send scan request */
838 if ((send(malware_daemon_ctx.sock, &drweb_cmd, sizeof(drweb_cmd), 0) < 0) ||
839 (send(malware_daemon_ctx.sock, &drweb_flags, sizeof(drweb_flags), 0) < 0) ||
840 (send(malware_daemon_ctx.sock, &drweb_fin, sizeof(drweb_fin), 0) < 0) ||
841 (send(malware_daemon_ctx.sock, &drweb_slen, sizeof(drweb_slen), 0) < 0))
843 (void)close(drweb_fd);
844 return m_panic_defer_3(scanent, CUS callout_address, string_sprintf(
845 "unable to send commands to socket (%s)", scanner_options),
846 malware_daemon_ctx.sock);
849 if (!(drweb_fbuf = store_malloc(fsize_uint)))
851 (void)close(drweb_fd);
852 return m_panic_defer_3(scanent, NULL,
853 string_sprintf("unable to allocate memory %u for file (%s)",
854 fsize_uint, eml_filename),
855 malware_daemon_ctx.sock);
858 if ((result = read (drweb_fd, drweb_fbuf, fsize)) == -1)
861 (void)close(drweb_fd);
862 store_free(drweb_fbuf);
863 return m_panic_defer_3(scanent, NULL,
864 string_sprintf("can't read spool file %s: %s",
865 eml_filename, strerror(err)),
866 malware_daemon_ctx.sock);
868 (void)close(drweb_fd);
870 /* send file body to socket */
871 if (send(malware_daemon_ctx.sock, drweb_fbuf, fsize, 0) < 0)
873 store_free(drweb_fbuf);
874 return m_panic_defer_3(scanent, CUS callout_address, string_sprintf(
875 "unable to send file body to socket (%s)", scanner_options),
876 malware_daemon_ctx.sock);
878 store_free(drweb_fbuf);
882 drweb_slen = htonl(Ustrlen(eml_filename));
884 DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s local scan [%s]\n",
885 scanner_name, scanner_options);
887 /* send scan request */
888 if ((send(malware_daemon_ctx.sock, &drweb_cmd, sizeof(drweb_cmd), 0) < 0) ||
889 (send(malware_daemon_ctx.sock, &drweb_flags, sizeof(drweb_flags), 0) < 0) ||
890 (send(malware_daemon_ctx.sock, &drweb_slen, sizeof(drweb_slen), 0) < 0) ||
891 (send(malware_daemon_ctx.sock, eml_filename, Ustrlen(eml_filename), 0) < 0) ||
892 (send(malware_daemon_ctx.sock, &drweb_fin, sizeof(drweb_fin), 0) < 0))
893 return m_panic_defer_3(scanent, CUS callout_address, string_sprintf(
894 "unable to send commands to socket (%s)", scanner_options),
895 malware_daemon_ctx.sock);
898 /* wait for result */
899 if (!recv_len(malware_daemon_ctx.sock, &drweb_rc, sizeof(drweb_rc), tmo))
900 return m_panic_defer_3(scanent, CUS callout_address,
901 US"unable to read return code", malware_daemon_ctx.sock);
902 drweb_rc = ntohl(drweb_rc);
904 if (!recv_len(malware_daemon_ctx.sock, &drweb_vnum, sizeof(drweb_vnum), tmo))
905 return m_panic_defer_3(scanent, CUS callout_address,
906 US"unable to read the number of viruses", malware_daemon_ctx.sock);
907 drweb_vnum = ntohl(drweb_vnum);
909 /* "virus(es) found" if virus number is > 0 */
914 /* setup default virus name */
915 malware_name = US"unknown";
917 /* set up match regex */
919 drweb_re = m_pcre_compile(drweb_re_str, FALSE, &errstr);
921 /* read and concatenate virus names into one string */
922 for (int i = 0; i < drweb_vnum; i++)
924 pcre2_match_data * md = pcre2_match_data_create(2, pcre_gen_ctx);
926 /* read the size of report */
927 if (!recv_len(malware_daemon_ctx.sock, &drweb_slen, sizeof(drweb_slen), tmo))
928 return m_panic_defer_3(scanent, CUS callout_address,
929 US"cannot read report size", malware_daemon_ctx.sock);
930 drweb_slen = ntohl(drweb_slen);
932 /* assume tainted, since it is external input */
933 tmpbuf = store_get(drweb_slen, GET_TAINTED);
935 /* read report body */
936 if (!recv_len(malware_daemon_ctx.sock, tmpbuf, drweb_slen, tmo))
937 return m_panic_defer_3(scanent, CUS callout_address,
938 US"cannot read report string", malware_daemon_ctx.sock);
939 tmpbuf[drweb_slen] = '\0';
941 /* try matcher on the line, grab substring */
942 result = pcre2_match(drweb_re, (PCRE2_SPTR)tmpbuf, PCRE2_ZERO_TERMINATED,
943 0, 0, md, pcre_gen_mtc_ctx);
946 PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
948 if (i==0) /* the first name we just copy to malware_name */
949 g = string_catn(NULL, US ovec[2], ovec[3] - ovec[2]);
951 else /* concatenate each new virus name to previous */
953 g = string_catn(g, US"/", 1);
954 g = string_catn(g, US ovec[2], ovec[3] - ovec[2]);
957 /* pcre2_match_data_free(md); gen ctx needs no free */
959 malware_name = string_from_gstring(g);
963 const char *drweb_s = NULL;
965 if (drweb_rc & DERR_READ_ERR) drweb_s = "read error";
966 if (drweb_rc & DERR_NOMEMORY) drweb_s = "no memory";
967 if (drweb_rc & DERR_TIMEOUT) drweb_s = "timeout";
968 if (drweb_rc & DERR_BAD_CALL) drweb_s = "wrong command";
969 /* retcodes DERR_SYMLINK, DERR_NO_REGFILE, DERR_SKIPPED.
970 * DERR_TOO_BIG, DERR_TOO_COMPRESSED, DERR_SPAM,
971 * DERR_CRC_ERROR, DERR_READSOCKET, DERR_WRITE_ERR
972 * and others are ignored */
974 return m_panic_defer_3(scanent, CUS callout_address,
975 string_sprintf("drweb daemon retcode 0x%x (%s)", drweb_rc, drweb_s),
976 malware_daemon_ctx.sock);
985 #ifndef DISABLE_MAL_AVE
986 case M_AVES: /* "aveserver" scanner type -------------------------------- */
991 /* read aveserver's greeting and see if it is ready (2xx greeting) */
993 recv_line(malware_daemon_ctx.sock, buf, sizeof(buf), tmo);
995 if (buf[0] != '2') /* aveserver is having problems */
996 return m_panic_defer_3(scanent, CUS callout_address,
997 string_sprintf("unavailable (Responded: %s).",
998 ((buf[0] != 0) ? buf : US "nothing") ),
999 malware_daemon_ctx.sock);
1001 /* prepare our command */
1002 (void)string_format(buf, sizeof(buf), "SCAN bPQRSTUW %s\r\n",
1006 DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s %s\n",
1008 if (m_sock_send(malware_daemon_ctx.sock, buf, Ustrlen(buf), &errstr) < 0)
1009 return m_panic_defer(scanent, CUS callout_address, errstr);
1011 malware_name = NULL;
1013 /* read response lines, find malware name and final response */
1014 while (recv_line(malware_daemon_ctx.sock, buf, sizeof(buf), tmo) > 0)
1018 if (buf[0] == '5') /* aveserver is having problems */
1020 result = m_panic_defer(scanent, CUS callout_address,
1021 string_sprintf("unable to scan file %s (Responded: %s).",
1022 eml_filename, buf));
1025 if (Ustrncmp(buf,"322",3) == 0)
1027 uschar *p = Ustrchr(&buf[4], ' ');
1029 malware_name = string_copy(&buf[4]);
1033 if (m_sock_send(malware_daemon_ctx.sock, US"quit\r\n", 6, &errstr) < 0)
1034 return m_panic_defer(scanent, CUS callout_address, errstr);
1036 /* read aveserver's greeting and see if it is ready (2xx greeting) */
1038 recv_line(malware_daemon_ctx.sock, buf, sizeof(buf), tmo);
1040 if (buf[0] != '2') /* aveserver is having problems */
1041 return m_panic_defer_3(scanent, CUS callout_address,
1042 string_sprintf("unable to quit dialogue (Responded: %s).",
1043 ((buf[0] != 0) ? buf : US "nothing") ),
1044 malware_daemon_ctx.sock);
1046 if (result == DEFER)
1048 (void)close(malware_daemon_ctx.sock);
1055 #ifndef DISABLE_MAL_FSECURE
1056 case M_FSEC: /* "fsecure" scanner type ---------------------------------- */
1060 uschar av_buffer[1024];
1061 static uschar *cmdopt[] = { US"CONFIGURE\tARCHIVE\t1\n",
1062 US"CONFIGURE\tTIMEOUT\t0\n",
1063 US"CONFIGURE\tMAXARCH\t5\n",
1064 US"CONFIGURE\tMIME\t1\n" };
1066 malware_name = NULL;
1068 DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s scan [%s]\n",
1069 scanner_name, scanner_options);
1071 memset(av_buffer, 0, sizeof(av_buffer));
1072 for (i = 0; i != nelem(cmdopt); i++)
1075 if (m_sock_send(malware_daemon_ctx.sock, cmdopt[i], Ustrlen(cmdopt[i]), &errstr) < 0)
1076 return m_panic_defer(scanent, CUS callout_address, errstr);
1078 bread = ip_recv(&malware_daemon_ctx, av_buffer, sizeof(av_buffer), tmo);
1079 if (bread > 0) av_buffer[bread]='\0';
1081 return m_panic_defer_3(scanent, CUS callout_address,
1082 string_sprintf("unable to read answer %d (%s)", i, strerror(errno)),
1083 malware_daemon_ctx.sock);
1084 for (int j = 0; j < bread; j++)
1085 if (av_buffer[j] == '\r' || av_buffer[j] == '\n')
1089 /* pass the mailfile to fsecure */
1090 file_name = string_sprintf("SCAN\t%s\n", eml_filename);
1092 if (m_sock_send(malware_daemon_ctx.sock, file_name, Ustrlen(file_name), &errstr) < 0)
1093 return m_panic_defer(scanent, CUS callout_address, errstr);
1096 /* todo also SUSPICION\t */
1098 fsec_re = m_pcre_compile(fsec_re_str, FALSE, &errstr);
1100 /* read report, linewise. Apply a timeout as the Fsecure daemon
1101 sometimes wants an answer to "PING" but they won't tell us what */
1103 uschar * p = av_buffer;
1109 i = av_buffer+sizeof(av_buffer)-p;
1110 if ((bread= ip_recv(&malware_daemon_ctx, p, i-1, tmo)) < 0)
1111 return m_panic_defer_3(scanent, CUS callout_address,
1112 string_sprintf("unable to read result (%s)", strerror(errno)),
1113 malware_daemon_ctx.sock);
1115 for (p[bread] = '\0'; (q = Ustrchr(p, '\n')); p = q+1)
1119 /* Really search for virus again? */
1121 /* try matcher on the line, grab substring */
1122 malware_name = m_pcre_exec(fsec_re, p);
1124 if (Ustrstr(p, "OK\tScan ok."))
1128 /* copy down the trailing partial line then read another chunk */
1129 i = av_buffer+sizeof(av_buffer)-p;
1130 memmove(av_buffer, p, i);
1140 #ifndef DISABLE_MAL_KAV
1141 case M_KAVD: /* "kavdaemon" scanner type -------------------------------- */
1144 uschar tmpbuf[1024];
1145 uschar * scanrequest;
1147 unsigned long kav_reportlen;
1149 const pcre2_code *kav_re;
1152 /* get current date and time, build scan request */
1154 /* pdp note: before the eml_filename parameter, this scanned the
1155 directory; not finding documentation, so we'll strip off the directory.
1156 The side-effect is that the test framework scanning may end up in
1157 scanning more than was requested, but for the normal interface, this is
1160 strftime(CS tmpbuf, sizeof(tmpbuf), "%d %b %H:%M:%S", localtime(&t));
1161 scanrequest = string_sprintf("<0>%s:%s", CS tmpbuf, eml_filename);
1162 p = Ustrrchr(scanrequest, '/');
1166 DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s scan [%s]\n",
1167 scanner_name, scanner_options);
1169 /* send scan request */
1170 if (m_sock_send(malware_daemon_ctx.sock, scanrequest, Ustrlen(scanrequest)+1, &errstr) < 0)
1171 return m_panic_defer(scanent, CUS callout_address, errstr);
1173 /* wait for result */
1174 if (!recv_len(malware_daemon_ctx.sock, tmpbuf, 2, tmo))
1175 return m_panic_defer_3(scanent, CUS callout_address,
1176 US"unable to read 2 bytes from socket.", malware_daemon_ctx.sock);
1178 /* get errorcode from one nibble */
1179 kav_rc = tmpbuf[ test_byte_order()==LITTLE_MY_ENDIAN ? 0 : 1 ] & 0x0F;
1182 case 5: case 6: /* improper kavdaemon configuration */
1183 return m_panic_defer_3(scanent, CUS callout_address,
1184 US"please reconfigure kavdaemon to NOT disinfect or remove infected files.",
1185 malware_daemon_ctx.sock);
1187 return m_panic_defer_3(scanent, CUS callout_address,
1188 US"reported 'scanning not completed' (code 1).", malware_daemon_ctx.sock);
1190 return m_panic_defer_3(scanent, CUS callout_address,
1191 US"reported 'kavdaemon damaged' (code 7).", malware_daemon_ctx.sock);
1194 /* code 8 is not handled, since it is ambiguous. It appears mostly on
1195 bounces where part of a file has been cut off */
1197 /* "virus found" return codes (2-4) */
1198 if (kav_rc > 1 && kav_rc < 5)
1200 int report_flag = 0;
1202 /* setup default virus name */
1203 malware_name = US"unknown";
1205 report_flag = tmpbuf[ test_byte_order() == LITTLE_MY_ENDIAN ? 1 : 0 ];
1207 /* read the report, if available */
1208 if (report_flag == 1)
1210 /* read report size */
1211 if (!recv_len(malware_daemon_ctx.sock, &kav_reportlen, 4, tmo))
1212 return m_panic_defer_3(scanent, CUS callout_address,
1213 US"cannot read report size", malware_daemon_ctx.sock);
1215 /* it's possible that avp returns av_buffer[1] == 1 but the
1216 reportsize is 0 (!?) */
1217 if (kav_reportlen > 0)
1219 /* set up match regex, depends on retcode */
1222 if (!kav_re_sus) kav_re_sus = m_pcre_compile(kav_re_sus_str, FALSE, &errstr);
1223 kav_re = kav_re_sus;
1227 if (!kav_re_inf) kav_re_inf = m_pcre_compile(kav_re_inf_str, FALSE, &errstr);
1228 kav_re = kav_re_inf;
1231 /* read report, linewise. Using size from stream to read amount of data
1232 from same stream is safe enough. */
1233 /* coverity[tainted_data] */
1234 while (kav_reportlen > 0)
1236 if ((bread = recv_line(malware_daemon_ctx.sock, tmpbuf, sizeof(tmpbuf), tmo)) < 0)
1238 kav_reportlen -= bread+1;
1240 /* try matcher on the line, grab substring */
1241 if ((malware_name = m_pcre_exec(kav_re, tmpbuf)))
1247 else /* no virus found */
1248 malware_name = NULL;
1254 #ifndef DISABLE_MAL_CMDLINE
1255 case M_CMDL: /* "cmdline" scanner type ---------------------------------- */
1257 const uschar *cmdline_scanner = scanner_options;
1258 const pcre2_code *cmdline_trigger_re;
1259 const pcre2_code *cmdline_regex_re;
1261 uschar * commandline;
1262 void (*eximsigchld)(int);
1263 void (*eximsigpipe)(int);
1264 FILE *scanner_out = NULL;
1266 FILE *scanner_record = NULL;
1267 uschar linebuffer[32767];
1272 if (!cmdline_scanner)
1273 return m_panic_defer(scanent, NULL, errstr);
1275 /* find scanner output trigger */
1276 cmdline_trigger_re = m_pcre_nextinlist(&av_scanner_work, &sep, av_scanner_textonly,
1277 "missing trigger specification", &errstr);
1278 if (!cmdline_trigger_re)
1279 return m_panic_defer(scanent, NULL, errstr);
1281 /* find scanner name regex */
1282 cmdline_regex_re = m_pcre_nextinlist(&av_scanner_work, &sep, av_scanner_textonly,
1283 "missing virus name regex specification", &errstr);
1284 if (!cmdline_regex_re)
1285 return m_panic_defer(scanent, NULL, errstr);
1287 /* prepare scanner call; despite the naming, file_name holds a directory
1288 name which is documented as the value given to %s. */
1290 file_name = string_copy(eml_filename);
1291 p = Ustrrchr(file_name, '/');
1294 commandline = string_sprintf(CS cmdline_scanner, file_name);
1296 /* redirect STDERR too */
1297 commandline = string_sprintf("%s 2>&1", commandline);
1299 DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s scan [%s]\n",
1300 scanner_name, commandline);
1302 /* store exims signal handlers */
1303 eximsigchld = signal(SIGCHLD,SIG_DFL);
1304 eximsigpipe = signal(SIGPIPE,SIG_DFL);
1306 if (!(scanner_out = popen(CS commandline,"r")))
1309 signal(SIGCHLD,eximsigchld); signal(SIGPIPE,eximsigpipe);
1310 return m_panic_defer(scanent, NULL,
1311 string_sprintf("call (%s) failed: %s.", commandline, strerror(err)));
1313 scanner_fd = fileno(scanner_out);
1315 file_name = string_sprintf("%s/%s_scanner_output", eml_dir, message_id);
1317 if (!(scanner_record = modefopen(file_name, "wb", SPOOL_MODE)))
1320 (void) pclose(scanner_out);
1321 signal(SIGCHLD,eximsigchld); signal(SIGPIPE,eximsigpipe);
1322 return m_panic_defer(scanent, NULL, string_sprintf(
1323 "opening scanner output file (%s) failed: %s.",
1324 file_name, strerror(err)));
1327 /* look for trigger while recording output */
1328 while ((rcnt = recv_line(scanner_fd, linebuffer,
1329 sizeof(linebuffer), tmo)))
1336 (void) pclose(scanner_out);
1337 signal(SIGCHLD,eximsigchld); signal(SIGPIPE,eximsigpipe);
1338 return m_panic_defer(scanent, NULL, string_sprintf(
1339 "unable to read from scanner (%s): %s",
1340 commandline, strerror(err)));
1343 if (Ustrlen(linebuffer) > fwrite(linebuffer, 1, Ustrlen(linebuffer), scanner_record))
1346 (void) pclose(scanner_out);
1347 signal(SIGCHLD,eximsigchld); signal(SIGPIPE,eximsigpipe);
1348 return m_panic_defer(scanent, NULL, string_sprintf(
1349 "short write on scanner output file (%s).", file_name));
1351 putc('\n', scanner_record);
1352 /* try trigger match */
1354 && regex_match_and_setup(cmdline_trigger_re, linebuffer, 0, -1)
1359 (void)fclose(scanner_record);
1360 sep = pclose(scanner_out);
1361 signal(SIGCHLD,eximsigchld); signal(SIGPIPE,eximsigpipe);
1363 return m_panic_defer(scanent, NULL,
1365 ? string_sprintf("running scanner failed: %s", strerror(sep))
1366 : string_sprintf("scanner returned error code: %d", sep));
1371 /* setup default virus name */
1372 malware_name = US"unknown";
1374 /* re-open the scanner output file, look for name match */
1375 scanner_record = Ufopen(file_name, "rb");
1376 while (Ufgets(linebuffer, sizeof(linebuffer), scanner_record))
1377 if ((s = m_pcre_exec(cmdline_regex_re, linebuffer))) /* try match */
1379 (void)fclose(scanner_record);
1381 else /* no virus found */
1382 malware_name = NULL;
1387 #ifndef DISABLE_MAL_SOPHIE
1388 case M_SOPHIE: /* "sophie" scanner type --------------------------------- */
1393 uschar av_buffer[1024];
1395 /* pass the scan directory to sophie */
1396 file_name = string_copy(eml_filename);
1397 if ((p = Ustrrchr(file_name, '/')))
1400 DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s scan [%s]\n",
1401 scanner_name, scanner_options);
1403 if ( write(malware_daemon_ctx.sock, file_name, Ustrlen(file_name)) < 0
1404 || write(malware_daemon_ctx.sock, "\n", 1) != 1
1406 return m_panic_defer_3(scanent, CUS callout_address,
1407 string_sprintf("unable to write to UNIX socket (%s)", scanner_options),
1408 malware_daemon_ctx.sock);
1410 /* wait for result */
1411 memset(av_buffer, 0, sizeof(av_buffer));
1412 if ((bread = ip_recv(&malware_daemon_ctx, av_buffer, sizeof(av_buffer), tmo)) <= 0)
1413 return m_panic_defer_3(scanent, CUS callout_address,
1414 string_sprintf("unable to read from UNIX socket (%s)", scanner_options),
1415 malware_daemon_ctx.sock);
1418 if (av_buffer[0] == '1') {
1419 uschar * s = Ustrchr(av_buffer, '\n');
1422 malware_name = string_copy(&av_buffer[2]);
1424 else if (!strncmp(CS av_buffer, "-1", 2))
1425 return m_panic_defer_3(scanent, CUS callout_address,
1426 US"scanner reported error", malware_daemon_ctx.sock);
1427 else /* all ok, no virus */
1428 malware_name = NULL;
1434 #ifndef DISABLE_MAL_CLAM
1435 case M_CLAMD: /* "clamd" scanner type ----------------------------------- */
1437 /* This code was originally contributed by David Saez */
1438 /* There are three scanning methods available to us:
1439 * (1) Use the SCAN command, pointing to a file in the filesystem
1440 * (2) Use the STREAM command, send the data on a separate port
1441 * (3) Use the zINSTREAM command, send the data inline
1442 * The zINSTREAM command was introduced with ClamAV 0.95, which marked
1443 * STREAM deprecated; see: http://wiki.clamav.net/bin/view/Main/UpgradeNotes095
1444 * In Exim, we use SCAN if using a Unix-domain socket or explicitly told that
1445 * the TCP-connected daemon is actually local; otherwise we use zINSTREAM
1446 * See Exim bug 926 for details. */
1448 uschar *p, *vname, *result_tag;
1450 uschar av_buffer[1024];
1451 uschar *hostname = US"";
1454 unsigned int fsize_uint;
1455 BOOL use_scan_command = FALSE;
1456 clamd_address * cv[MAX_CLAMD_SERVERS];
1457 int num_servers = 0;
1458 uint32_t send_size, send_final_zeroblock;
1461 /*XXX if unixdomain socket, only one server supported. Needs fixing;
1462 there's no reason we should not mix local and remote servers */
1464 if (*scanner_options == '/')
1467 const uschar * sublist;
1470 /* Local file; so we def want to use_scan_command and don't want to try
1471 passing IP/port combinations */
1472 use_scan_command = TRUE;
1473 cd = (clamd_address *) store_get(sizeof(clamd_address), GET_UNTAINTED);
1475 /* extract socket-path part */
1476 sublist = scanner_options;
1477 cd->hostspec = string_nextinlist(&sublist, &subsep, NULL, 0);
1480 if (clamd_option(cd, sublist, &subsep) != OK)
1481 return m_panic_defer(scanent, NULL,
1482 string_sprintf("bad option '%s'", scanner_options));
1487 /* Go through the rest of the list of host/port and construct an array
1488 * of servers to try. The first one is the bit we just passed from
1489 * scanner_options so process that first and then scan the remainder of
1490 * the address buffer */
1494 const uschar * sublist;
1498 /* The 'local' option means use the SCAN command over the network
1499 * socket (ie common file storage in use) */
1500 /*XXX we could accept this also as a local option? */
1501 if (strcmpic(scanner_options, US"local") == 0)
1503 use_scan_command = TRUE;
1507 cd = (clamd_address *) store_get(sizeof(clamd_address), GET_UNTAINTED);
1509 /* extract host and port part */
1510 sublist = scanner_options;
1511 if (!(cd->hostspec = string_nextinlist(&sublist, &subsep, NULL, 0)))
1513 (void) m_panic_defer(scanent, NULL,
1514 string_sprintf("missing address: '%s'", scanner_options));
1517 if (!(s = string_nextinlist(&sublist, &subsep, NULL, 0)))
1519 (void) m_panic_defer(scanent, NULL,
1520 string_sprintf("missing port: '%s'", scanner_options));
1523 cd->tcp_port = atoi(CS s);
1526 /*XXX should these options be common over scanner types? */
1527 if (clamd_option(cd, sublist, &subsep) != OK)
1528 return m_panic_defer(scanent, NULL,
1529 string_sprintf("bad option '%s'", scanner_options));
1531 cv[num_servers++] = cd;
1532 if (num_servers >= MAX_CLAMD_SERVERS)
1534 (void) m_panic_defer(scanent, NULL,
1535 US"More than " MAX_CLAMD_SERVERS_S " clamd servers "
1536 "specified; only using the first " MAX_CLAMD_SERVERS_S );
1539 } while ((scanner_options = string_nextinlist(&av_scanner_work, &sep,
1542 /* check if we have at least one server */
1544 return m_panic_defer(scanent, NULL,
1545 US"no useable server addresses in malware configuration option.");
1548 /* See the discussion of response formats below to see why we really
1549 don't like colons in filenames when passing filenames to ClamAV. */
1550 if (use_scan_command && Ustrchr(eml_filename, ':'))
1551 return m_panic_defer(scanent, NULL,
1552 string_sprintf("local/SCAN mode incompatible with" \
1553 " : in path to email filename [%s]", eml_filename));
1555 /* Set up the very first data we will be sending */
1556 if (!use_scan_command)
1557 { cmd_str.data = US"zINSTREAM"; cmd_str.len = 10; }
1561 cmd_str.data = string_sprintf("SCAN %s\n%n", eml_filename, &n);
1562 cmd_str.len = n; /* .len is a size_t */
1565 /* We have some network servers specified */
1568 /* Confirmed in ClamAV source (0.95.3) that the TCPAddr option of clamd
1569 only supports AF_INET, but we should probably be looking to the
1570 future and rewriting this to be protocol-independent anyway. */
1572 while (num_servers > 0)
1574 int i = random_number(num_servers);
1575 clamd_address * cd = cv[i];
1577 DEBUG(D_acl) debug_printf_indent("trying server name %s, port %u\n",
1578 cd->hostspec, cd->tcp_port);
1580 /* Lookup the host. This is to ensure that we connect to the same IP
1581 on both connections (as one host could resolve to multiple ips) */
1584 /*XXX we trust that the cmd_str is idempotent */
1585 if ((malware_daemon_ctx.sock = m_tcpsocket(cd->hostspec, cd->tcp_port,
1587 use_scan_command ? &cmd_str : NULL)) >= 0)
1589 /* Connection successfully established with a server */
1590 hostname = cd->hostspec;
1591 if (use_scan_command) cmd_str.len = 0;
1594 if (cd->retry <= 0) break;
1595 while (cd->retry > 0) cd->retry = sleep(cd->retry);
1597 if (malware_daemon_ctx.sock >= 0)
1600 (void) m_panic_defer(scanent, CUS callout_address, errstr);
1602 /* Remove the server from the list. XXX We should free the memory */
1604 for (; i < num_servers; i++)
1608 if (num_servers == 0)
1609 return m_panic_defer(scanent, NULL, US"all servers failed");
1614 if ((malware_daemon_ctx.sock = ip_unixsocket(cv[0]->hostspec, &errstr)) >= 0)
1616 hostname = cv[0]->hostspec;
1619 if (cv[0]->retry <= 0)
1620 return m_panic_defer(scanent, CUS callout_address, errstr);
1621 while (cv[0]->retry > 0) cv[0]->retry = sleep(cv[0]->retry);
1624 /* have socket in variable "sock"; command to use is semi-independent of
1625 the socket protocol. We use SCAN if is local (either Unix/local
1626 domain socket, or explicitly told local) else we stream the data.
1627 How we stream the data depends upon how we were built. */
1629 if (!use_scan_command)
1632 #if defined(EXIM_TCP_CORK) && !defined(OS_SENDFILE)
1635 /* New protocol: "zINSTREAM\n" followed by a sequence of <length><data>
1636 chunks, <n> a 4-byte number (network order), terminated by a zero-length
1637 chunk. We only send one chunk. */
1639 DEBUG(D_acl) debug_printf_indent(
1640 "Malware scan: issuing %s new-style remote scan (zINSTREAM)\n",
1643 #if defined(EXIM_TCP_CORK)
1644 (void) setsockopt(malware_daemon_ctx.sock, IPPROTO_TCP, EXIM_TCP_CORK,
1645 US &on, sizeof(on));
1647 /* Pass the string to ClamAV (10 = "zINSTREAM\0"), if not already sent */
1649 if (send(malware_daemon_ctx.sock, cmd_str.data, cmd_str.len, 0) < 0)
1650 return m_panic_defer_3(scanent, CUS hostname,
1651 string_sprintf("unable to send zINSTREAM to socket (%s)",
1653 malware_daemon_ctx.sock);
1655 if ((clam_fd = exim_open2(CS eml_filename, O_RDONLY)) < 0)
1658 return m_panic_defer_3(scanent, NULL,
1659 string_sprintf("can't open spool file %s: %s",
1660 eml_filename, strerror(err)),
1661 malware_daemon_ctx.sock);
1663 if (fstat(clam_fd, &st) < 0)
1666 (void)close(clam_fd);
1667 return m_panic_defer_3(scanent, NULL,
1668 string_sprintf("can't stat spool file %s: %s",
1669 eml_filename, strerror(err)),
1670 malware_daemon_ctx.sock);
1672 fsize_uint = (unsigned int) st.st_size;
1673 if ((off_t)fsize_uint != st.st_size)
1675 (void)close(clam_fd);
1676 return m_panic_defer_3(scanent, NULL,
1677 string_sprintf("stat spool file %s, size overflow", eml_filename),
1678 malware_daemon_ctx.sock);
1681 /* send file size */
1682 send_size = htonl(fsize_uint);
1683 if (send(malware_daemon_ctx.sock, &send_size, sizeof(send_size), 0) < 0)
1684 return m_panic_defer_3(scanent, NULL,
1685 string_sprintf("unable to send file size to socket (%s)", hostname),
1686 malware_daemon_ctx.sock);
1688 /* send file body */
1692 int n = os_sendfile(malware_daemon_ctx.sock, clam_fd, NULL, (size_t)fsize_uint);
1694 return m_panic_defer_3(scanent, NULL,
1695 string_sprintf("unable to send file body to socket (%s): %s", hostname, strerror(errno)),
1696 malware_daemon_ctx.sock);
1699 int n = MIN(fsize_uint, big_buffer_size);
1700 if ((n = read(clam_fd, big_buffer, n)) < 0)
1701 return m_panic_defer_3(scanent, NULL,
1702 string_sprintf("can't read spool file %s: %s",
1703 eml_filename, strerror(errno)),
1704 malware_daemon_ctx.sock);
1705 if (send(malware_daemon_ctx.sock, big_buffer, (size_t)n, 0) < 0)
1706 return m_panic_defer_3(scanent, NULL,
1707 string_sprintf("unable to send file body to socket (%s): %s", hostname, strerror(errno)),
1708 malware_daemon_ctx.sock);
1710 # ifdef EXIM_TCP_CORK
1714 (void) setsockopt(malware_daemon_ctx.sock, IPPROTO_TCP, EXIM_TCP_CORK,
1715 US &off, sizeof(off));
1718 #endif /*!OS_SENDFILE*/
1722 send_final_zeroblock = 0;
1723 if (send(malware_daemon_ctx.sock, &send_final_zeroblock, sizeof(send_final_zeroblock), 0) < 0)
1724 return m_panic_defer_3(scanent, NULL,
1725 string_sprintf("unable to send file terminator to socket (%s)", hostname),
1726 malware_daemon_ctx.sock);
1728 (void) setsockopt(malware_daemon_ctx.sock, IPPROTO_TCP, EXIM_TCP_CORK,
1729 US &off, sizeof(off));
1733 { /* use scan command */
1734 /* Send a SCAN command pointing to a filename; then in the then in the
1735 scan-method-neutral part, read the response back */
1737 /* ================================================================= */
1739 /* Prior to the reworking post-Exim-4.72, this scanned a directory,
1740 which dates to when ClamAV needed us to break apart the email into the
1741 MIME parts (eg, with the now deprecated demime condition coming first).
1742 Some time back, ClamAV gained the ability to deconstruct the emails, so
1743 doing this would actually have resulted in the mail attachments being
1744 scanned twice, in the broken out files and from the original .eml.
1745 Since ClamAV now handles emails (and has for quite some time) we can
1746 just use the email file itself. */
1747 /* Pass the string to ClamAV (7 = "SCAN \n" + \0), if not already sent */
1749 DEBUG(D_acl) debug_printf_indent(
1750 "Malware scan: issuing %s local-path scan [%s]\n",
1751 scanner_name, scanner_options);
1754 if (send(malware_daemon_ctx.sock, cmd_str.data, cmd_str.len, 0) < 0)
1755 return m_panic_defer_3(scanent, CUS callout_address,
1756 string_sprintf("unable to write to socket (%s)", strerror(errno)),
1757 malware_daemon_ctx.sock);
1759 /* Do not shut down the socket for writing; a user report noted that
1760 clamd 0.70 does not react well to this. */
1762 /* Commands have been sent, no matter which scan method or connection
1763 type we're using; now just read the result, independent of method. */
1765 /* Read the result */
1766 memset(av_buffer, 0, sizeof(av_buffer));
1767 bread = ip_recv(&malware_daemon_ctx, av_buffer, sizeof(av_buffer), tmo);
1768 (void)close(malware_daemon_ctx.sock);
1769 malware_daemon_ctx.sock = -1;
1770 malware_daemon_ctx.tls_ctx = NULL;
1773 return m_panic_defer(scanent, CUS callout_address,
1774 string_sprintf("unable to read from socket (%s)",
1775 errno == 0 ? "EOF" : strerror(errno)));
1777 if (bread == sizeof(av_buffer))
1778 return m_panic_defer(scanent, CUS callout_address,
1779 US"buffer too small");
1780 /* We're now assured of a NULL at the end of av_buffer */
1782 /* Check the result. ClamAV returns one of two result formats.
1783 In the basic mode, the response is of the form:
1784 infected: -> "<filename>: <virusname> FOUND"
1785 not-infected: -> "<filename>: OK"
1786 error: -> "<filename>: <errcode> ERROR
1787 If the ExtendedDetectionInfo option has been turned on, then we get:
1788 "<filename>: <virusname>(<virushash>:<virussize>) FOUND"
1789 for the infected case. Compare:
1790 /tmp/eicar.com: Eicar-Test-Signature FOUND
1791 /tmp/eicar.com: Eicar-Test-Signature(44d88612fea8a8f36de82e1278abb02f:68) FOUND
1793 In the streaming case, clamd uses the filename "stream" which you should
1794 be able to verify with { ktrace clamdscan --stream /tmp/eicar.com }. (The
1795 client app will replace "stream" with the original filename before returning
1796 results to stdout, but the trace shows the data).
1798 We will assume that the pathname passed to clamd from Exim does not contain
1799 a colon. We will have whined loudly above if the eml_filename does (and we're
1800 passing a filename to clamd). */
1803 return m_panic_defer(scanent, CUS callout_address,
1804 US"ClamAV returned null");
1806 /* strip newline at the end (won't be present for zINSTREAM)
1807 (also any trailing whitespace, which shouldn't exist, but we depend upon
1808 this below, so double-check) */
1810 p = av_buffer + Ustrlen(av_buffer) - 1;
1811 if (*p == '\n') *p = '\0';
1813 DEBUG(D_acl) debug_printf_indent("Malware response: %s\n", av_buffer);
1815 while (isspace(*--p) && (p > av_buffer))
1819 /* colon in returned output? */
1820 if (!(p = Ustrchr(av_buffer,':')))
1821 return m_panic_defer(scanent, CUS callout_address, string_sprintf(
1822 "ClamAV returned malformed result (missing colon): %s",
1825 /* strip filename */
1826 while (*p && isspace(*++p)) /**/;
1829 /* It would be bad to encounter a virus with "FOUND" in part of the name,
1830 but we should at least be resistant to it. */
1831 p = Ustrrchr(vname, ' ');
1832 result_tag = p ? p+1 : vname;
1834 if (Ustrcmp(result_tag, "FOUND") == 0)
1836 /* p should still be the whitespace before the result_tag */
1837 while (isspace(*p)) --p;
1839 /* Strip off the extended information too, which will be in parens
1840 after the virus name, with no intervening whitespace. */
1843 /* "(hash:size)", so previous '(' will do; if not found, we have
1844 a curious virus name, but not an error. */
1845 p = Ustrrchr(vname, '(');
1849 malware_name = string_copy(vname);
1850 DEBUG(D_acl) debug_printf_indent("Malware found, name \"%s\"\n", malware_name);
1853 else if (Ustrcmp(result_tag, "ERROR") == 0)
1854 return m_panic_defer(scanent, CUS callout_address,
1855 string_sprintf("ClamAV returned: %s", av_buffer));
1857 else if (Ustrcmp(result_tag, "OK") == 0)
1859 /* Everything should be OK */
1860 malware_name = NULL;
1861 DEBUG(D_acl) debug_printf_indent("Malware not found\n");
1865 return m_panic_defer(scanent, CUS callout_address,
1866 string_sprintf("unparseable response from ClamAV: {%s}", av_buffer));
1872 #ifndef DISABLE_MAL_SOCK
1873 case M_SOCK: /* "sock" scanner type ------------------------------------- */
1874 /* This code was derived by Martin Poole from the clamd code contributed
1875 by David Saez and the cmdline code
1879 uschar * commandline;
1880 uschar av_buffer[1024];
1881 uschar * linebuffer;
1882 uschar * sockline_scanner;
1883 uschar sockline_scanner_default[] = "%s\n";
1884 const pcre2_code *sockline_trig_re;
1885 const pcre2_code *sockline_name_re;
1887 /* find scanner command line */
1888 if ( (sockline_scanner = string_nextinlist(&av_scanner_work, &sep,
1890 && *sockline_scanner
1892 { /* check for no expansions apart from one %s */
1893 uschar * s = Ustrchr(sockline_scanner, '%');
1895 if ((*s != 's' && *s != '%') || Ustrchr(s+1, '%'))
1896 return m_panic_defer_3(scanent, NULL,
1897 US"unsafe sock scanner call spec", malware_daemon_ctx.sock);
1900 sockline_scanner = sockline_scanner_default;
1901 DEBUG(D_acl) debug_printf_indent("%15s%10s'%s'\n", "", "cmdline: ",
1902 string_printing(sockline_scanner));
1904 /* find scanner output trigger */
1905 sockline_trig_re = m_pcre_nextinlist(&av_scanner_work, &sep, av_scanner_textonly,
1906 "missing trigger specification", &errstr);
1907 if (!sockline_trig_re)
1908 return m_panic_defer_3(scanent, NULL, errstr, malware_daemon_ctx.sock);
1910 /* find virus name regex */
1911 sockline_name_re = m_pcre_nextinlist(&av_scanner_work, &sep, av_scanner_textonly,
1912 "missing virus name regex specification", &errstr);
1913 if (!sockline_name_re)
1914 return m_panic_defer_3(scanent, NULL, errstr, malware_daemon_ctx.sock);
1916 /* prepare scanner call - security depends on expansions check above */
1917 commandline = string_sprintf( CS sockline_scanner, CS eml_filename);
1918 DEBUG(D_acl) debug_printf_indent("%15s%10s'%s'\n", "", "expanded: ",
1919 string_printing(commandline));
1921 /* Pass the command string to the socket */
1922 if (m_sock_send(malware_daemon_ctx.sock, commandline, Ustrlen(commandline), &errstr) < 0)
1923 return m_panic_defer(scanent, CUS callout_address, errstr);
1925 /* Read the result */
1926 bread = ip_recv(&malware_daemon_ctx, av_buffer, sizeof(av_buffer), tmo);
1929 return m_panic_defer_3(scanent, CUS callout_address,
1930 string_sprintf("unable to read from socket (%s)", strerror(errno)),
1931 malware_daemon_ctx.sock);
1933 if (bread == sizeof(av_buffer))
1934 return m_panic_defer_3(scanent, CUS callout_address,
1935 US"buffer too small", malware_daemon_ctx.sock);
1936 av_buffer[bread] = '\0';
1937 linebuffer = string_copy(av_buffer);
1938 DEBUG(D_acl) debug_printf_indent("%15s%10s'%s'\n", "", "answer: ",
1939 string_printing(linebuffer));
1941 /* try trigger match */
1942 if (regex_match_and_setup(sockline_trig_re, linebuffer, 0, -1))
1944 if (!(malware_name = m_pcre_exec(sockline_name_re, av_buffer)))
1945 malware_name = US "unknown";
1946 DEBUG(D_acl) debug_printf_indent("%15s%10s'%s'\n", "", "name: ",
1947 string_printing(malware_name));
1949 else /* no virus found */
1950 malware_name = NULL;
1955 #ifndef DISABLE_MAL_MKS
1956 case M_MKSD: /* "mksd" scanner type ------------------------------------- */
1958 char *mksd_options_end;
1959 int mksd_maxproc = 1; /* default, if no option supplied */
1962 if (scanner_options)
1964 mksd_maxproc = (int)strtol(CS scanner_options, &mksd_options_end, 10);
1965 if ( *scanner_options == '\0'
1966 || *mksd_options_end != '\0'
1968 || mksd_maxproc > 32
1970 return m_panic_defer(scanent, CUS callout_address,
1971 string_sprintf("invalid option '%s'", scanner_options));
1974 if((malware_daemon_ctx.sock = ip_unixsocket(US "/var/run/mksd/socket", &errstr)) < 0)
1975 return m_panic_defer(scanent, CUS callout_address, errstr);
1977 malware_name = NULL;
1979 DEBUG(D_acl) debug_printf_indent("Malware scan: issuing %s scan\n", scanner_name);
1981 if ((retval = mksd_scan_packed(scanent, malware_daemon_ctx.sock, eml_filename, tmo)) != OK)
1983 close (malware_daemon_ctx.sock);
1990 #ifndef DISABLE_MAL_AVAST
1991 case M_AVAST: /* "avast" scanner type ----------------------------------- */
1994 uschar * scanrequest;
1995 enum {AVA_HELO, AVA_OPT, AVA_RSP, AVA_DONE} avast_stage;
1997 uschar * error_message = NULL;
1998 BOOL more_data = FALSE;
2001 /* According to Martin Tuma @avast the protocol uses "escaped
2002 whitespace", that is, every embedded whitespace is backslash
2003 escaped, as well as backslash is protected by backslash.
2004 The returned lines contain the name of the scanned file, a tab
2008 [E] - some error occurred
2009 Such marker follows the first non-escaped TAB. For more information
2010 see avast-protocol(5)
2012 We observed two cases:
2014 <- /file [E]0.0 Error 13 Permission denied
2015 <- 451 SCAN Engine error 13 permission denied
2018 <- /file… [E]3.0 Error 41120 The file is a decompression bomb
2020 <- /file… [+]2.0 0 Eicar Test Virus!!!
2023 If the scanner returns 4xx, DEFER is a good decision, combined
2024 with a panic log entry, to get the admin's attention.
2026 If the scanner returns 200, we reject it as malware, if found any,
2027 or, in case of an error, we set the malware message to the error
2030 Some of the >= 42000 errors are message related - usually some
2031 broken archives etc, but some of them are e.g. license related.
2032 Once the license expires the engine starts returning errors for
2033 every scanning attempt. I¹ have the full list of the error codes
2034 but it is not a public API and is subject to change. It is hard
2035 for me to say what you should do in case of an engine error. You
2036 can have a “Treat * unscanned file as infection” policy or “Treat
2037 unscanned file as clean” policy. ¹) Jakub Bednar
2041 if ( ( !ava_re_clean
2042 && !(ava_re_clean = m_pcre_compile(ava_re_clean_str, FALSE, &errstr)))
2044 && !(ava_re_virus = m_pcre_compile(ava_re_virus_str, FALSE, &errstr)))
2046 && !(ava_re_error = m_pcre_compile(ava_re_error_str, FALSE, &errstr)))
2048 return malware_panic_defer(errstr);
2050 /* wait for result */
2051 for (avast_stage = AVA_HELO;
2052 (nread = recv_line(malware_daemon_ctx.sock, buf, sizeof(buf), tmo)) > 0;
2055 int slen = Ustrlen(buf);
2059 /* Multi line responses are bracketed between 210 … and nnn … */
2060 if (Ustrncmp(buf, "210", 3) == 0)
2065 else if (more_data && isdigit(buf[0])) more_data = 0;
2067 switch (avast_stage)
2070 if (more_data) continue;
2071 if (Ustrncmp(buf, "220", 3) != 0)
2072 goto endloop; /* require a 220 */
2076 if (more_data) continue;
2077 if (Ustrncmp(buf, "200", 3) != 0)
2078 goto endloop; /* require a 200 */
2083 /* Check for another option to send. Newline-terminate it. */
2084 if ((scanrequest = string_nextinlist(&av_scanner_work, &sep,
2087 if (Ustrcmp(scanrequest, "pass_unscanned") == 0)
2089 DEBUG(D_acl) debug_printf_indent("pass unscanned files as clean\n");
2093 scanrequest = string_sprintf("%s\n", scanrequest);
2094 avast_stage = AVA_OPT; /* just sent option */
2095 DEBUG(D_acl) debug_printf_indent("send to avast OPTION: %s", scanrequest);
2099 scanrequest = string_sprintf("SCAN %s\n", eml_dir);
2100 avast_stage = AVA_RSP; /* just sent command */
2101 DEBUG(D_acl) debug_printf_indent("send to avast REQUEST: SCAN %s\n", eml_dir);
2104 /* send config-cmd or scan-request to socket */
2105 len = Ustrlen(scanrequest);
2106 if (send(malware_daemon_ctx.sock, scanrequest, len, 0) == -1)
2108 scanrequest[len-1] = '\0';
2109 return m_panic_defer_3(scanent, CUS callout_address, string_sprintf(
2110 "unable to send request '%s' to socket (%s): %s",
2111 scanrequest, scanner_options, strerror(errno)), malware_daemon_ctx.sock);
2118 if (isdigit(buf[0])) /* We're done */
2121 if (malware_name) /* Nothing else matters, just read on */
2124 if (regex_match(ava_re_clean, buf, slen, NULL))
2127 if ((malware_name = m_pcre_exec(ava_re_virus, buf)))
2129 unescape(malware_name);
2131 debug_printf_indent("unescaped malware name: '%s'\n", malware_name);
2135 if (strict) /* treat scanner errors as malware */
2137 if ((malware_name = m_pcre_exec(ava_re_error, buf)))
2139 unescape(malware_name);
2141 debug_printf_indent("unescaped error message: '%s'\n", malware_name);
2145 else if (regex_match(ava_re_error, buf, slen, NULL))
2147 log_write(0, LOG_MAIN, "internal scanner error (ignored): %s", buf);
2151 /* here also for any unexpected response from the scanner */
2152 DEBUG(D_acl) debug_printf("avast response not handled: '%s'\n", buf);
2156 default: log_write(0, LOG_PANIC, "%s:%d:%s: should not happen",
2157 __FILE__, __LINE__, __FUNCTION__);
2164 if (nread == -1) error_message = US"EOF from scanner";
2165 else if (nread < 0) error_message = US"timeout from scanner";
2166 else if (nread == 0) error_message = US"got nothing from scanner";
2167 else if (buf[0] != '2') error_message = buf;
2169 DEBUG(D_acl) debug_printf_indent("sent to avast QUIT\n");
2170 if (send(malware_daemon_ctx.sock, "QUIT\n", 5, 0) == -1)
2171 return m_panic_defer_3(scanent, CUS callout_address,
2172 string_sprintf("unable to send quit request to socket (%s): %s",
2173 scanner_options, strerror(errno)), malware_daemon_ctx.sock);
2176 return m_panic_defer_3(scanent, CUS callout_address, error_message, malware_daemon_ctx.sock);
2180 } /* scanner type switch */
2182 if (malware_daemon_ctx.sock >= 0)
2183 (void) close (malware_daemon_ctx.sock);
2184 malware_ok = TRUE; /* set "been here, done that" marker */
2187 /* match virus name against pattern (caseless ------->----------v) */
2188 if (malware_name && regex_match_and_setup(re, malware_name, 0, -1))
2190 DEBUG(D_acl) debug_printf_indent(
2191 "Matched regex to malware [%s] [%s]\n", malware_re, malware_name);
2199 /*************************************************
2200 * Scan an email for malware *
2201 *************************************************/
2203 /* This is the normal interface for scanning an email, which doesn't need a
2204 filename; it's a wrapper around the malware_file function.
2207 malware_re match condition for "malware="
2208 cacheable the RE did not use any dynamic elements during expansion
2209 timeout if nonzero, timeout in seconds
2211 Returns: Exim message processing code (OK, FAIL, DEFER, ...)
2212 where true means malware was found (condition applies)
2215 malware(const uschar * malware_re, BOOL cacheable, int timeout)
2217 int ret = malware_internal(malware_re, cacheable, NULL, timeout);
2219 if (ret == DEFER) av_failed = TRUE;
2224 /*************************************************
2225 * Scan a file for malware *
2226 *************************************************/
2228 /* This is a test wrapper for scanning an email, which is not used in
2229 normal processing. Scan any file, using the Exim scanning interface.
2230 This function tampers with various global variables so is unsafe to use
2231 in any other context.
2234 eml_filename a file holding the message to be scanned
2236 Returns: Exim message processing code (OK, FAIL, DEFER, ...)
2237 where true means malware was found (condition applies)
2240 malware_in_file(uschar *eml_filename)
2242 uschar message_id_buf[64];
2245 /* spool_mbox() assumes various parameters exist, when creating
2246 the relevant directory and the email within */
2248 (void) string_format(message_id_buf, sizeof(message_id_buf),
2249 "dummy-%d", vaguely_random_number(INT_MAX));
2250 message_id = message_id_buf;
2251 sender_address = US"malware-sender@example.net";
2253 recipients_list = NULL;
2254 receive_add_recipient(US"malware-victim@example.net", -1);
2255 f.enable_dollar_recipients = TRUE;
2257 ret = malware_internal(US"*", TRUE, eml_filename, 0);
2259 Ustrncpy(spooled_message_id, message_id, sizeof(spooled_message_id));
2262 /* don't set no_mbox_unspool; at present, there's no way for it to become
2263 set, but if that changes, then it should apply to these tests too */
2267 /* silence static analysis tools */
2277 if (!malware_default_re)
2278 malware_default_re = regex_must_compile(malware_regex_default, MCS_NOFLAGS, TRUE);
2280 #ifndef DISABLE_MAL_DRWEB
2282 drweb_re = regex_must_compile(drweb_re_str, MCS_NOFLAGS, TRUE);
2284 #ifndef DISABLE_MAL_FSECURE
2286 fsec_re = regex_must_compile(fsec_re_str, MCS_NOFLAGS, TRUE);
2288 #ifndef DISABLE_MAL_KAV
2290 kav_re_sus = regex_must_compile(kav_re_sus_str, MCS_NOFLAGS, TRUE);
2292 kav_re_inf = regex_must_compile(kav_re_inf_str, MCS_NOFLAGS, TRUE);
2294 #ifndef DISABLE_MAL_AVAST
2296 ava_re_clean = regex_must_compile(ava_re_clean_str, MCS_NOFLAGS, TRUE);
2298 ava_re_virus = regex_must_compile(ava_re_virus_str, MCS_NOFLAGS, TRUE);
2300 ava_re_error = regex_must_compile(ava_re_error_str, MCS_NOFLAGS, TRUE);
2302 #ifndef DISABLE_MAL_FFROT6D
2303 if (!fprot6d_re_error)
2304 fprot6d_re_error = regex_must_compile(fprot6d_re_error_str, MCS_NOFLAGS, TRUE);
2305 if (!fprot6d_re_virus)
2306 fprot6d_re_virus = regex_must_compile(fprot6d_re_virus_str, MCS_NOFLAGS, TRUE);
2312 malware_show_supported(gstring * g)
2314 g = string_cat(g, US"Malware:");
2315 for (struct scan * sc = m_scans; sc->scancode != (scanner_t)-1; sc++)
2316 g = string_fmt_append(g, " %s", sc->name);
2317 return string_cat(g, US"\n");
2321 # endif /*!MACRO_PREDEF*/
2322 #endif /*WITH_CONTENT_SCAN*/