Update $message_linecount for maildir_tag.
[exim.git] / src / src / spam.c
CommitLineData
8544e77a 1/* $Cambridge: exim/src/src/spam.c,v 1.18 2010/06/05 11:13:30 pdp Exp $ */
8523533c
TK
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
7/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
8/* License: GPL */
9
10/* Code for calling spamassassin's spamd. Called from acl.c. */
11
12#include "exim.h"
13#ifdef WITH_CONTENT_SCAN
14#include "spam.h"
15
16uschar spam_score_buffer[16];
17uschar spam_score_int_buffer[16];
18uschar spam_bar_buffer[128];
19uschar spam_report_buffer[32600];
20uschar prev_user_name[128] = "";
21int spam_ok = 0;
22int spam_rc = 0;
f7274286 23uschar *prev_spamd_address_work = NULL;
8523533c
TK
24
25int spam(uschar **listptr) {
26 int sep = 0;
27 uschar *list = *listptr;
28 uschar *user_name;
29 uschar user_name_buffer[128];
f7b63901 30 unsigned long mbox_size;
8523533c
TK
31 FILE *mbox_file;
32 int spamd_sock;
33 uschar spamd_buffer[32600];
cfe75fc3 34 int i, j, offset, result;
8523533c
TK
35 uschar spamd_version[8];
36 uschar spamd_score_char;
37 double spamd_threshold, spamd_score;
38 int spamd_report_offset;
39 uschar *p,*q;
40 int override = 0;
cfe75fc3
PH
41 time_t start;
42 size_t read, wrote;
8523533c 43 struct sockaddr_un server;
f452e07e 44#ifndef NO_POLL_H
cfe75fc3 45 struct pollfd pollfd;
25257489
PH
46#else /* Patch posted by Erik ? for OS X */
47 struct timeval select_tv; /* and applied by PH */
48 fd_set select_fd;
f452e07e 49#endif
b6e6e716 50 uschar *spamd_address_work;
8523533c 51
5614ee86 52 /* stop compiler warning */
91ecef39 53 result = 0;
5614ee86 54
8523533c
TK
55 /* find the username from the option list */
56 if ((user_name = string_nextinlist(&list, &sep,
57 user_name_buffer,
58 sizeof(user_name_buffer))) == NULL) {
59 /* no username given, this means no scanning should be done */
60 return FAIL;
61 };
62
63 /* if username is "0" or "false", do not scan */
64 if ( (Ustrcmp(user_name,"0") == 0) ||
65 (strcmpic(user_name,US"false") == 0) ) {
66 return FAIL;
67 };
68
69 /* if there is an additional option, check if it is "true" */
70 if (strcmpic(list,US"true") == 0) {
71 /* in that case, always return true later */
72 override = 1;
73 };
74
f7274286
PP
75 /* expand spamd_address if needed */
76 if (*spamd_address == '$') {
77 spamd_address_work = expand_string(spamd_address);
78 if (spamd_address_work == NULL) {
79 log_write(0, LOG_MAIN|LOG_PANIC,
80 "spamassassin acl condition: spamd_address starts with $, but expansion failed: %s", expand_string_message);
81 return DEFER;
82 }
83 }
84 else
85 spamd_address_work = spamd_address;
86
87 /* check if previous spamd_address was expanded and has changed. dump cached results if so */
88 if ( spam_ok && ( prev_spamd_address_work != NULL) && (Ustrcmp(prev_spamd_address_work, spamd_address_work) != 0)) {
89 spam_ok = 0;
90 }
91
8e669ac1 92 /* if we scanned for this username last time, just return */
8523533c
TK
93 if ( spam_ok && ( Ustrcmp(prev_user_name, user_name) == 0 ) ) {
94 if (override)
95 return OK;
96 else
97 return spam_rc;
98 };
8e669ac1 99
8523533c 100 /* make sure the eml mbox file is spooled up */
8544e77a 101 mbox_file = spool_mbox(&mbox_size, NULL);
8e669ac1 102
8523533c
TK
103 if (mbox_file == NULL) {
104 /* error while spooling */
105 log_write(0, LOG_MAIN|LOG_PANIC,
106 "spam acl condition: error while creating mbox spool file");
107 return DEFER;
108 };
109
cfe75fc3 110 start = time(NULL);
b6e6e716 111
8523533c 112 /* socket does not start with '/' -> network socket */
b6e6e716 113 if (*spamd_address_work != '/') {
8523533c
TK
114 time_t now = time(NULL);
115 int num_servers = 0;
116 int current_server = 0;
117 int start_server = 0;
118 uschar *address = NULL;
e1e7cfcb 119 uschar *spamd_address_list_ptr = spamd_address_work;
8523533c
TK
120 uschar address_buffer[256];
121 spamd_address_container * spamd_address_vector[32];
122
123 /* Check how many spamd servers we have
124 and register their addresses */
125 while ((address = string_nextinlist(&spamd_address_list_ptr, &sep,
126 address_buffer,
127 sizeof(address_buffer))) != NULL) {
8e669ac1 128
8523533c
TK
129 spamd_address_container *this_spamd =
130 (spamd_address_container *)store_get(sizeof(spamd_address_container));
8e669ac1 131
8523533c
TK
132 /* grok spamd address and port */
133 if( sscanf(CS address, "%s %u", this_spamd->tcp_addr, &(this_spamd->tcp_port)) != 2 ) {
134 log_write(0, LOG_MAIN,
135 "spam acl condition: warning - invalid spamd address: '%s'", address);
136 continue;
137 };
8e669ac1 138
8523533c
TK
139 spamd_address_vector[num_servers] = this_spamd;
140 num_servers++;
141 if (num_servers > 31)
142 break;
143 };
8e669ac1 144
8523533c
TK
145 /* check if we have at least one server */
146 if (!num_servers) {
147 log_write(0, LOG_MAIN|LOG_PANIC,
148 "spam acl condition: no useable spamd server addresses in spamd_address configuration option.");
f1e894f3 149 (void)fclose(mbox_file);
8523533c
TK
150 return DEFER;
151 };
152
153 current_server = start_server = (int)now % num_servers;
154
155 while (1) {
8e669ac1 156
8523533c
TK
157 debug_printf("trying server %s, port %u\n",
158 spamd_address_vector[current_server]->tcp_addr,
159 spamd_address_vector[current_server]->tcp_port);
8e669ac1 160
8523533c
TK
161 /* contact a spamd */
162 if ( (spamd_sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
163 log_write(0, LOG_MAIN|LOG_PANIC,
164 "spam acl condition: error creating IP socket for spamd");
f1e894f3 165 (void)fclose(mbox_file);
8e669ac1 166 return DEFER;
8523533c 167 };
8e669ac1 168
8523533c
TK
169 if (ip_connect( spamd_sock,
170 AF_INET,
171 spamd_address_vector[current_server]->tcp_addr,
172 spamd_address_vector[current_server]->tcp_port,
173 5 ) > -1) {
174 /* connection OK */
175 break;
176 };
8e669ac1 177
8523533c
TK
178 log_write(0, LOG_MAIN|LOG_PANIC,
179 "spam acl condition: warning - spamd connection to %s, port %u failed: %s",
180 spamd_address_vector[current_server]->tcp_addr,
181 spamd_address_vector[current_server]->tcp_port,
182 strerror(errno));
183 current_server++;
184 if (current_server >= num_servers)
185 current_server = 0;
186 if (current_server == start_server) {
187 log_write(0, LOG_MAIN|LOG_PANIC, "spam acl condition: all spamd servers failed");
f1e894f3
PH
188 (void)fclose(mbox_file);
189 (void)close(spamd_sock);
8523533c
TK
190 return DEFER;
191 };
192 };
193
194 }
195 else {
196 /* open the local socket */
197
198 if ((spamd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
199 log_write(0, LOG_MAIN|LOG_PANIC,
200 "malware acl condition: spamd: unable to acquire socket (%s)",
201 strerror(errno));
f1e894f3 202 (void)fclose(mbox_file);
8523533c
TK
203 return DEFER;
204 }
205
206 server.sun_family = AF_UNIX;
b6e6e716 207 Ustrcpy(server.sun_path, spamd_address_work);
8523533c
TK
208
209 if (connect(spamd_sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
210 log_write(0, LOG_MAIN|LOG_PANIC,
211 "malware acl condition: spamd: unable to connect to UNIX socket %s (%s)",
b6e6e716 212 spamd_address_work, strerror(errno) );
f1e894f3
PH
213 (void)fclose(mbox_file);
214 (void)close(spamd_sock);
8523533c
TK
215 return DEFER;
216 }
217
218 }
219
220 /* now we are connected to spamd on spamd_sock */
b07e6aa3 221 (void)string_format(spamd_buffer,
8523533c 222 sizeof(spamd_buffer),
f7b63901 223 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
8523533c
TK
224 user_name,
225 mbox_size);
226
227 /* send our request */
228 if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0) {
f1e894f3 229 (void)close(spamd_sock);
8523533c
TK
230 log_write(0, LOG_MAIN|LOG_PANIC,
231 "spam acl condition: spamd send failed: %s", strerror(errno));
f1e894f3
PH
232 (void)fclose(mbox_file);
233 (void)close(spamd_sock);
8523533c
TK
234 return DEFER;
235 };
236
237 /* now send the file */
cfe75fc3
PH
238 /* spamd sometimes accepts conections but doesn't read data off
239 * the connection. We make the file descriptor non-blocking so
240 * that the write will only write sufficient data without blocking
241 * and we poll the desciptor to make sure that we can write without
242 * blocking. Short writes are gracefully handled and if the whole
243 * trasaction takes too long it is aborted.
25257489
PH
244 * Note: poll() is not supported in OSX 10.2 and is reported to be
245 * broken in more recent versions (up to 10.4).
cfe75fc3 246 */
f452e07e 247#ifndef NO_POLL_H
cfe75fc3
PH
248 pollfd.fd = spamd_sock;
249 pollfd.events = POLLOUT;
f452e07e 250#endif
ff790e47 251 (void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK);
8523533c 252 do {
cfe75fc3
PH
253 read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
254 if (read > 0) {
255 offset = 0;
256again:
f452e07e 257#ifndef NO_POLL_H
cfe75fc3 258 result = poll(&pollfd, 1, 1000);
25257489
PH
259
260/* Patch posted by Erik ? for OS X and applied by PH */
261#else
262 select_tv.tv_sec = 1;
263 select_tv.tv_usec = 0;
264 FD_ZERO(&select_fd);
265 FD_SET(spamd_sock, &select_fd);
266 result = select(spamd_sock+1, NULL, &select_fd, NULL, &select_tv);
267#endif
268/* End Erik's patch */
269
cfe75fc3 270 if (result == -1 && errno == EINTR)
25257489 271 goto again;
cfe75fc3
PH
272 else if (result < 1) {
273 if (result == -1)
274 log_write(0, LOG_MAIN|LOG_PANIC,
275 "spam acl condition: %s on spamd socket", strerror(errno));
276 else {
277 if (time(NULL) - start < SPAMD_TIMEOUT)
278 goto again;
279 log_write(0, LOG_MAIN|LOG_PANIC,
280 "spam acl condition: timed out writing spamd socket");
281 }
f1e894f3
PH
282 (void)close(spamd_sock);
283 (void)fclose(mbox_file);
8523533c 284 return DEFER;
cfe75fc3 285 }
25257489 286
cfe75fc3 287 wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0);
8d7d227d
TK
288 if (wrote == -1)
289 {
290 log_write(0, LOG_MAIN|LOG_PANIC,
291 "spam acl condition: %s on spamd socket", strerror(errno));
f1e894f3
PH
292 (void)close(spamd_sock);
293 (void)fclose(mbox_file);
8d7d227d
TK
294 return DEFER;
295 }
cfe75fc3
PH
296 if (offset + wrote != read) {
297 offset += wrote;
298 goto again;
299 }
300 }
301 }
302 while (!feof(mbox_file) && !ferror(mbox_file));
303 if (ferror(mbox_file)) {
304 log_write(0, LOG_MAIN|LOG_PANIC,
305 "spam acl condition: error reading spool file: %s", strerror(errno));
f1e894f3
PH
306 (void)close(spamd_sock);
307 (void)fclose(mbox_file);
cfe75fc3 308 return DEFER;
8523533c 309 }
8523533c 310
f1e894f3 311 (void)fclose(mbox_file);
8523533c
TK
312
313 /* we're done sending, close socket for writing */
314 shutdown(spamd_sock,SHUT_WR);
8e669ac1 315
cfe75fc3
PH
316 /* read spamd response using what's left of the timeout.
317 */
8523533c
TK
318 memset(spamd_buffer, 0, sizeof(spamd_buffer));
319 offset = 0;
320 while((i = ip_recv(spamd_sock,
321 spamd_buffer + offset,
322 sizeof(spamd_buffer) - offset - 1,
cfe75fc3 323 SPAMD_TIMEOUT - time(NULL) + start)) > 0 ) {
8523533c
TK
324 offset += i;
325 }
326
327 /* error handling */
328 if((i <= 0) && (errno != 0)) {
329 log_write(0, LOG_MAIN|LOG_PANIC,
330 "spam acl condition: error reading from spamd socket: %s", strerror(errno));
f1e894f3 331 (void)close(spamd_sock);
8523533c
TK
332 return DEFER;
333 }
334
335 /* reading done */
f1e894f3 336 (void)close(spamd_sock);
8523533c
TK
337
338 /* dig in the spamd output and put the report in a multiline header, if requested */
0806a9c5 339 if( sscanf(CS spamd_buffer,"SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
8523533c 340 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
8e669ac1 341
8523533c 342 /* try to fall back to pre-2.50 spamd output */
0806a9c5 343 if( sscanf(CS spamd_buffer,"SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
8523533c
TK
344 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
345 log_write(0, LOG_MAIN|LOG_PANIC,
346 "spam acl condition: cannot parse spamd output");
347 return DEFER;
348 };
349 };
350
351 /* Create report. Since this is a multiline string,
352 we must hack it into shape first */
353 p = &spamd_buffer[spamd_report_offset];
354 q = spam_report_buffer;
355 while (*p != '\0') {
356 /* skip \r */
357 if (*p == '\r') {
358 p++;
359 continue;
360 };
361 *q = *p;
362 q++;
363 if (*p == '\n') {
89dec7b6
TF
364 /* add an extra space after the newline to ensure
365 that it is treated as a header continuation line */
366 *q = ' ';
8523533c 367 q++;
8523533c
TK
368 };
369 p++;
370 };
371 /* NULL-terminate */
372 *q = '\0';
373 q--;
374 /* cut off trailing leftovers */
375 while (*q <= ' ') {
376 *q = '\0';
377 q--;
378 };
379 spam_report = spam_report_buffer;
380
381 /* create spam bar */
382 spamd_score_char = spamd_score > 0 ? '+' : '-';
383 j = abs((int)(spamd_score));
384 i = 0;
385 if( j != 0 ) {
386 while((i < j) && (i <= MAX_SPAM_BAR_CHARS))
387 spam_bar_buffer[i++] = spamd_score_char;
388 }
389 else{
390 spam_bar_buffer[0] = '/';
391 i = 1;
392 }
393 spam_bar_buffer[i] = '\0';
394 spam_bar = spam_bar_buffer;
395
396 /* create "float" spam score */
b07e6aa3 397 (void)string_format(spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score);
8523533c
TK
398 spam_score = spam_score_buffer;
399
400 /* create "int" spam score */
401 j = (int)((spamd_score + 0.001)*10);
b07e6aa3 402 (void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j);
8523533c
TK
403 spam_score_int = spam_score_int_buffer;
404
405 /* compare threshold against score */
406 if (spamd_score >= spamd_threshold) {
407 /* spam as determined by user's threshold */
408 spam_rc = OK;
409 }
410 else {
411 /* not spam */
412 spam_rc = FAIL;
413 };
8e669ac1 414
f7274286
PP
415 /* remember expanded spamd_address if needed */
416 if (spamd_address_work != spamd_address) {
417 prev_spamd_address_work = string_copy(spamd_address_work);
b6e6e716 418 }
f7274286
PP
419 /* remember user name and "been here" for it */
420 Ustrcpy(prev_user_name, user_name);
421 spam_ok = 1;
8e669ac1 422
8523533c
TK
423 if (override) {
424 /* always return OK, no matter what the score */
425 return OK;
426 }
427 else {
428 return spam_rc;
429 };
430}
431
432#endif