2 * uvscan local_scan() function for Exim (requires at least Exim v4.14)
3 * known to work with VirusScan for Linux v4.16.0 (Scan engine v4.2.40)
4 * but should be OK with other platforms
6 * this file is free software (license=GNU GPLv2) and comes with no
7 * guarantees--if it breaks, you get to keep the pieces (maybe not the mail)!
9 * by (ie patches / flames to): mb/local_scan@dcs.qmul.ac.uk, 2003-05-02
10 * (original version on 2002-05-25)
15 #include <sys/types.h>
19 #include "local_scan.h"
22 * remember to set LOCAL_SCAN_HAS_OPTIONS=yes in Local/Makefile
23 * otherwise you get stuck with the compile-time defaults
26 static uschar *uvscan_binary = US"/usr/local/uvscan/uvscan";
27 static uschar *data_directory = US"/usr/local/uvscan";
29 optionlist local_scan_options[] = { /* alphabetical order */
30 { "data_directory", opt_stringptr, &data_directory },
31 { "uvscan_binary", opt_stringptr, &uvscan_binary }
34 int local_scan_options_count = sizeof(local_scan_options)/sizeof(optionlist);
36 /* log headers in rejectlog or not? */
38 //#define VIRUS_IN_MAIL LOCAL_SCAN_REJECT
39 #define VIRUS_IN_MAIL LOCAL_SCAN_REJECT_NOLOGHDR
42 * buffer is used both for file copying and catching uvscan's output
43 * BUFSIZE = 1024 should always be fine
48 /* some number which uvscan doesn't return */
52 * some macros to make the main function more obvious
53 * NB bailing out might leave tempfiles hanging around
54 * (and open fds, but no need to be worried about that)
57 #define BAIL(btext) { log_write(0, LOG_MAIN, "UVSCAN ERROR: "btext); \
58 *return_text = "local scanning problem: please try again later"; \
59 return LOCAL_SCAN_TEMPREJECT; }
61 #define DEBUG(dtext) if ((debug_selector & D_local_scan) != 0) \
62 { debug_printf(dtext); sleep(1); }
63 /* sleep useful for running exim -d */
65 #define RESULT(rtext) header_add(32, \
66 "X-uvscan-result: "rtext" (%s)\n", message_id);
68 #define FREEZE(ftext) { header_add(32, \
69 "X-uvscan-warning: frozen for manual attention (%s)\n", message_id); \
70 RESULT(ftext); *return_text = ftext; return LOCAL_SCAN_ACCEPT_FREEZE; }
72 /* OK, enough waffle. On with the show! */
74 int local_scan(int fd, uschar **return_text)
76 char tf[] = "/tmp/local_scan.XXXXXX"; /* should this be tunable? */
77 int tmpfd, bytesin, bytesout, pid, status, pipe_fd[2];
79 static uschar buffer[BUFSIZE];
81 DEBUG("entered uvscan local_scan() function");
84 * I set majordomo to resend using -oMr lsmtp
85 * (and yes, I know majordomo isn't actually using SMTP..)
86 * no point in scanning these beasties twice
89 if(!strcmp(received_protocol, "lsmtp"))
90 return LOCAL_SCAN_ACCEPT;
92 /* create a file to copy the data into */
94 if ((tmpfd = mkstemp(tf)) == -1)
95 BAIL("mkstemp failed");
97 DEBUG("made tmp file");
99 /* copy said file BUFSIZE at a time */
101 while ((bytesin = read(fd, buffer, BUFSIZE)) > 0) {
102 bytesout = write(tmpfd, buffer, bytesin);
104 BAIL("writing to tmp file");
107 BAIL("reading from spool file");
111 if(pipe(pipe_fd) == -1)
116 if((pid = fork()) == -1)
117 BAIL("couldn't fork");
120 close(1); /* close stdout */
121 if(dup2(pipe_fd[1],1) == -1) /* duplicate write as stdout */
122 BAIL("dup2 (stdout) failed");
123 if(fcntl(1,F_SETFD,0) == -1) /* fd to NOT close on exec() */
124 BAIL("fcntl (stdout) failed");
126 execl(uvscan_binary, uvscan_binary, "--mime", "--secure",
127 "-d", data_directory, tf, NULL);
128 DEBUG("execl failed");
132 if(waitpid(pid, &status, 0) < 1)
133 BAIL("couldn't wait for child");
135 DEBUG("about to unlink");
138 FREEZE("couldn't unlink tmp file");
140 DEBUG("unlinked :)");
143 * choose what to do based on the return code of uvscan
144 * RESULT() or FREEZE() according to personal taste
147 if(WIFEXITED(status) != 0)
148 switch(WEXITSTATUS(status)) {
149 case 0: RESULT("clean"); break;
150 case 2: RESULT("driver integrity check failed"); break;
151 case 6: FREEZE("general problem occurred"); break;
152 case 8: RESULT("could not find a driver"); break;
153 case 12: FREEZE("failed to clean file"); break;
155 // RESULT("virus detected"); /* were we to accept */
156 DEBUG("about to read from uvscan process");
158 FD_SET(pipe_fd[0], &fds);
159 if(select(pipe_fd[0]+1, &fds, NULL, NULL, NULL)) {
160 /* last NULL above means wait forever! */
161 DEBUG("select returned non-zero");
162 if((bytesin = read(pipe_fd[0], buffer,
164 buffer[bytesin] = (uschar)0;
165 *return_text = buffer + 22;
166 /* 22 was empirically found ;) */
167 return VIRUS_IN_MAIL;
169 BAIL("reading from uvscan process");
172 case 15: FREEZE("self-check failed"); break;
173 case 19: FREEZE("virus detected and cleaned"); break;
174 case MAGIC: RESULT("couldn't run uvscan"); break;
176 RESULT("unknown error code");
177 header_add(32, "X-uvscan-status: %d\n",
178 WEXITSTATUS(status));
182 BAIL("child exited abnormally");
184 return LOCAL_SCAN_ACCEPT;