Merge from master into 4.next
[exim.git] / src / src / spam.c
CommitLineData
8523533c
TK
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
4fab92fb
HSHR
5/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003 - 2015
6 * License: GPL
7 * Copyright (c) The Exim Maintainers 2016
8 */
8523533c
TK
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];
bfe645c1 19uschar spam_action_buffer[32];
8523533c
TK
20uschar spam_report_buffer[32600];
21uschar prev_user_name[128] = "";
22int spam_ok = 0;
23int spam_rc = 0;
f7274286 24uschar *prev_spamd_address_work = NULL;
8523533c 25
bfe645c1
JH
26static const uschar * loglabel = US"spam acl condition:";
27
28
29static int
30spamd_param_init(spamd_address_container *spamd)
c007c974 31{
bfe645c1
JH
32/* default spamd server weight, time and priority value */
33spamd->is_rspamd = FALSE;
34spamd->is_failed = FALSE;
35spamd->weight = SPAMD_WEIGHT;
36spamd->timeout = SPAMD_TIMEOUT;
37spamd->retry = 0;
38spamd->priority = 1;
39return 0;
40}
8523533c 41
5614ee86 42
bfe645c1
JH
43static int
44spamd_param(const uschar * param, spamd_address_container * spamd)
45{
46static int timesinceday = -1;
47const uschar * s;
48const uschar * name;
49
50/*XXX more clever parsing could discard embedded spaces? */
51
52if (sscanf(CCS param, "pri=%u", &spamd->priority))
53 return 0; /* OK */
54
55if (sscanf(CCS param, "weight=%u", &spamd->weight))
56 {
57 if (spamd->weight == 0) /* this server disabled: skip it */
58 return 1;
59 return 0; /* OK */
60 }
61
62if (Ustrncmp(param, "time=", 5) == 0)
63 {
64 unsigned int start_h = 0, start_m = 0, start_s = 0;
65 unsigned int end_h = 24, end_m = 0, end_s = 0;
66 unsigned int time_start, time_end;
67 const uschar * end_string;
68
69 name = US"time";
70 s = param+5;
71 if ((end_string = Ustrchr(s, '-')))
80832b14 72 {
bfe645c1
JH
73 end_string++;
74 if ( sscanf(CS end_string, "%u.%u.%u", &end_h, &end_m, &end_s) == 0
75 || sscanf(CS s, "%u.%u.%u", &start_h, &start_m, &start_s) == 0
76 )
77 goto badval;
80832b14 78 }
bfe645c1
JH
79 else
80 goto badval;
8523533c 81
bfe645c1 82 if (timesinceday < 0)
80832b14 83 {
bfe645c1
JH
84 time_t now = time(NULL);
85 struct tm *tmp = localtime(&now);
86 timesinceday = tmp->tm_hour*3600 + tmp->tm_min*60 + tmp->tm_sec;
f7274286 87 }
f7274286 88
bfe645c1
JH
89 time_start = start_h*3600 + start_m*60 + start_s;
90 time_end = end_h*3600 + end_m*60 + end_s;
91
92 if (timesinceday < time_start || timesinceday >= time_end)
93 return 1; /* skip spamd server */
94
95 return 0; /* OK */
96 }
97
98if (Ustrcmp(param, "variant=rspamd") == 0)
99 {
100 spamd->is_rspamd = TRUE;
101 return 0;
102 }
103
104if (Ustrncmp(param, "tmo=", 4) == 0)
105 {
106 int sec = readconf_readtime((s = param+4), '\0', FALSE);
107 name = US"timeout";
108 if (sec < 0)
109 goto badval;
110 spamd->timeout = sec;
111 return 0;
112 }
113
114if (Ustrncmp(param, "retry=", 6) == 0)
115 {
116 int sec = readconf_readtime((s = param+6), '\0', FALSE);
117 name = US"retry";
118 if (sec < 0)
119 goto badval;
120 spamd->retry = sec;
121 return 0;
122 }
123
124log_write(0, LOG_MAIN, "%s warning - invalid spamd parameter: '%s'",
125 loglabel, param);
126return -1; /* syntax error */
127
128badval:
129 log_write(0, LOG_MAIN,
130 "%s warning - invalid spamd %s value: '%s'", loglabel, name, s);
131 return -1; /* syntax error */
132}
133
f7274286 134
bfe645c1
JH
135static int
136spamd_get_server(spamd_address_container ** spamds, int num_servers)
137{
138unsigned int i;
139spamd_address_container * sd;
140long rnd, weights;
141unsigned pri;
142static BOOL srandomed = FALSE;
143
4fab92fb 144/* speedup, if we have only 1 server */
bfe645c1
JH
145if (num_servers == 1)
146 return (spamds[0]->is_failed ? -1 : 0);
147
148/* init ranmod */
149if (!srandomed)
150 {
151 struct timeval tv;
152 gettimeofday(&tv, NULL);
153 srandom((unsigned int)(tv.tv_usec/1000));
154 srandomed = TRUE;
155 }
156
157/* scan for highest pri */
158for (pri = 0, i = 0; i < num_servers; i++)
159 {
160 sd = spamds[i];
161 if (!sd->is_failed && sd->priority > pri) pri = sd->priority;
162 }
163
164/* get sum of weights */
165for (weights = 0, i = 0; i < num_servers; i++)
166 {
167 sd = spamds[i];
168 if (!sd->is_failed && sd->priority == pri) weights += sd->weight;
169 }
170if (weights == 0) /* all servers failed */
171 return -1;
172
173for (rnd = random() % weights, i = 0; i < num_servers; i++)
174 {
175 sd = spamds[i];
176 if (!sd->is_failed && sd->priority == pri)
177 if ((rnd -= sd->weight) <= 0)
178 return i;
179 }
180
181log_write(0, LOG_MAIN|LOG_PANIC,
182 "%s unknown error (memory/cpu corruption?)", loglabel);
183return -1;
184}
8e669ac1 185
8e669ac1 186
bfe645c1
JH
187int
188spam(const uschar **listptr)
189{
190int sep = 0;
191const uschar *list = *listptr;
192uschar *user_name;
193uschar user_name_buffer[128];
194unsigned long mbox_size;
195FILE *mbox_file;
196int spamd_sock = -1;
197uschar spamd_buffer[32600];
198int i, j, offset, result;
199uschar spamd_version[8];
200uschar spamd_short_result[8];
201uschar spamd_score_char;
202double spamd_threshold, spamd_score, spamd_reject_score;
203int spamd_report_offset;
204uschar *p,*q;
205int override = 0;
206time_t start;
207size_t read, wrote;
208#ifndef NO_POLL_H
209struct pollfd pollfd;
210#else /* Patch posted by Erik ? for OS X */
211struct timeval select_tv; /* and applied by PH */
212fd_set select_fd;
213#endif
214uschar *spamd_address_work;
215spamd_address_container * sd;
216
217/* stop compiler warning */
218result = 0;
219
220/* find the username from the option list */
221if ((user_name = string_nextinlist(&list, &sep,
222 user_name_buffer,
223 sizeof(user_name_buffer))) == NULL)
224 {
225 /* no username given, this means no scanning should be done */
226 return FAIL;
227 }
228
229/* if username is "0" or "false", do not scan */
230if ( (Ustrcmp(user_name,"0") == 0) ||
231 (strcmpic(user_name,US"false") == 0) )
232 return FAIL;
233
234/* if there is an additional option, check if it is "true" */
235if (strcmpic(list,US"true") == 0)
236 /* in that case, always return true later */
237 override = 1;
238
239/* expand spamd_address if needed */
240if (*spamd_address == '$')
241 {
242 spamd_address_work = expand_string(spamd_address);
243 if (spamd_address_work == NULL)
80832b14 244 {
8523533c 245 log_write(0, LOG_MAIN|LOG_PANIC,
bfe645c1
JH
246 "%s spamd_address starts with $, but expansion failed: %s",
247 loglabel, expand_string_message);
8523533c 248 return DEFER;
80832b14 249 }
bfe645c1
JH
250 }
251else
252 spamd_address_work = spamd_address;
253
254DEBUG(D_acl) debug_printf("spamd: addrlist '%s'\n", spamd_address_work);
255
256/* check if previous spamd_address was expanded and has changed. dump cached results if so */
257if ( spam_ok
258 && prev_spamd_address_work != NULL
259 && Ustrcmp(prev_spamd_address_work, spamd_address_work) != 0
260 )
261 spam_ok = 0;
262
263/* if we scanned for this username last time, just return */
264if (spam_ok && Ustrcmp(prev_user_name, user_name) == 0)
265 return override ? OK : spam_rc;
266
267/* make sure the eml mbox file is spooled up */
268mbox_file = spool_mbox(&mbox_size, NULL);
269
270if (mbox_file == NULL)
271 {
272 /* error while spooling */
273 log_write(0, LOG_MAIN|LOG_PANIC,
274 "%s error while creating mbox spool file", loglabel);
275 return DEFER;
276 }
277
278start = time(NULL);
279
280 {
281 int num_servers = 0;
282 int current_server;
283 uschar * address;
284 const uschar * spamd_address_list_ptr = spamd_address_work;
285 spamd_address_container * spamd_address_vector[32];
286
287 /* Check how many spamd servers we have
288 and register their addresses */
289 sep = 0; /* default colon-sep */
290 while ((address = string_nextinlist(&spamd_address_list_ptr, &sep,
291 NULL, 0)) != NULL)
80832b14 292 {
bfe645c1
JH
293 const uschar * sublist;
294 int sublist_sep = -(int)' '; /* default space-sep */
295 unsigned args;
296 uschar * s;
297
298 DEBUG(D_acl) debug_printf("spamd: addr entry '%s'\n", address);
299 sd = (spamd_address_container *)store_get(sizeof(spamd_address_container));
300
301 for (sublist = address, args = 0, spamd_param_init(sd);
302 (s = string_nextinlist(&sublist, &sublist_sep, NULL, 0));
303 args++
304 )
80832b14 305 {
bfe645c1
JH
306 DEBUG(D_acl) debug_printf("spamd: addr parm '%s'\n", s);
307 switch (args)
308 {
309 case 0: sd->hostspec = s;
310 if (*s == '/') args++; /* local; no port */
311 break;
312 case 1: sd->hostspec = string_sprintf("%s %s", sd->hostspec, s);
313 break;
314 default: spamd_param(s, sd);
315 break;
80832b14 316 }
80832b14 317 }
bfe645c1 318 if (args < 2)
80832b14 319 {
bfe645c1
JH
320 log_write(0, LOG_MAIN,
321 "%s warning - invalid spamd address: '%s'", loglabel, address);
322 continue;
80832b14 323 }
8523533c 324
bfe645c1
JH
325 spamd_address_vector[num_servers] = sd;
326 if (++num_servers > 31)
327 break;
328 }
29cfeb94 329
bfe645c1
JH
330 /* check if we have at least one server */
331 if (!num_servers)
332 {
333 log_write(0, LOG_MAIN|LOG_PANIC,
334 "%s no useable spamd server addresses in spamd_address configuration option.",
335 loglabel);
336 goto defer;
29cfeb94 337 }
bfe645c1
JH
338
339 current_server = spamd_get_server(spamd_address_vector, num_servers);
340 sd = spamd_address_vector[current_server];
341 for(;;)
80832b14 342 {
bfe645c1 343 uschar * errstr;
8523533c 344
bfe645c1
JH
345 DEBUG(D_acl) debug_printf("spamd: trying server %s\n", sd->hostspec);
346
347 for (;;)
80832b14 348 {
bfe645c1
JH
349 if ( (spamd_sock = ip_streamsocket(sd->hostspec, &errstr, 5)) >= 0
350 || sd->retry <= 0
351 )
352 break;
353 DEBUG(D_acl) debug_printf("spamd: server %s: retry conn\n", sd->hostspec);
354 while (sd->retry > 0) sd->retry = sleep(sd->retry);
80832b14 355 }
bfe645c1
JH
356 if (spamd_sock >= 0)
357 break;
8523533c 358
bfe645c1
JH
359 log_write(0, LOG_MAIN, "%s spamd: %s", loglabel, errstr);
360 sd->is_failed = TRUE;
8523533c 361
bfe645c1
JH
362 current_server = spamd_get_server(spamd_address_vector, num_servers);
363 if (current_server < 0)
80832b14 364 {
bfe645c1
JH
365 log_write(0, LOG_MAIN|LOG_PANIC, "%s all spamd servers failed", loglabel);
366 goto defer;
80832b14 367 }
bfe645c1 368 sd = spamd_address_vector[current_server];
8523533c 369 }
bfe645c1
JH
370 }
371
bfe645c1
JH
372(void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK);
373/* now we are connected to spamd on spamd_sock */
374if (sd->is_rspamd)
375 { /* rspamd variant */
376 uschar *req_str;
377 const uschar * helo;
378 const uschar * fcrdns;
379 const uschar * authid;
380
381 req_str = string_sprintf("CHECK RSPAMC/1.3\r\nContent-length: %lu\r\n"
382 "Queue-Id: %s\r\nFrom: <%s>\r\nRecipient-Number: %d\r\n",
383 mbox_size, message_id, sender_address, recipients_count);
384 for (i = 0; i < recipients_count; i ++)
385 req_str = string_sprintf("%sRcpt: <%s>\r\n", req_str, recipients_list[i].address);
386 if ((helo = expand_string(US"$sender_helo_name")) != NULL && *helo != '\0')
387 req_str = string_sprintf("%sHelo: %s\r\n", req_str, helo);
388 if ((fcrdns = expand_string(US"$sender_host_name")) != NULL && *fcrdns != '\0')
389 req_str = string_sprintf("%sHostname: %s\r\n", req_str, fcrdns);
390 if (sender_host_address != NULL)
391 req_str = string_sprintf("%sIP: %s\r\n", req_str, sender_host_address);
392 if ((authid = expand_string(US"$authenticated_id")) != NULL && *authid != '\0')
393 req_str = string_sprintf("%sUser: %s\r\n", req_str, authid);
394 req_str = string_sprintf("%s\r\n", req_str);
395 wrote = send(spamd_sock, req_str, Ustrlen(req_str), 0);
396 }
397else
398 { /* spamassassin variant */
b07e6aa3 399 (void)string_format(spamd_buffer,
bfe645c1
JH
400 sizeof(spamd_buffer),
401 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
402 user_name,
403 mbox_size);
8523533c 404 /* send our request */
bfe645c1
JH
405 wrote = send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0);
406 }
8523533c 407
bfe645c1
JH
408if (wrote == -1)
409 {
410 (void)close(spamd_sock);
411 log_write(0, LOG_MAIN|LOG_PANIC,
412 "%s spamd %s send failed: %s", loglabel, callout_address, strerror(errno));
413 goto defer;
414 }
415
416/* now send the file */
417/* spamd sometimes accepts conections but doesn't read data off
418 * the connection. We make the file descriptor non-blocking so
419 * that the write will only write sufficient data without blocking
420 * and we poll the desciptor to make sure that we can write without
421 * blocking. Short writes are gracefully handled and if the whole
422 * trasaction takes too long it is aborted.
423 * Note: poll() is not supported in OSX 10.2 and is reported to be
424 * broken in more recent versions (up to 10.4).
425 */
f452e07e 426#ifndef NO_POLL_H
bfe645c1
JH
427pollfd.fd = spamd_sock;
428pollfd.events = POLLOUT;
f452e07e 429#endif
bfe645c1
JH
430(void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK);
431do
432 {
433 read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
434 if (read > 0)
80832b14 435 {
bfe645c1 436 offset = 0;
cfe75fc3 437again:
f452e07e 438#ifndef NO_POLL_H
bfe645c1 439 result = poll(&pollfd, 1, 1000);
25257489
PH
440
441/* Patch posted by Erik ? for OS X and applied by PH */
442#else
bfe645c1
JH
443 select_tv.tv_sec = 1;
444 select_tv.tv_usec = 0;
445 FD_ZERO(&select_fd);
446 FD_SET(spamd_sock, &select_fd);
447 result = select(spamd_sock+1, NULL, &select_fd, NULL, &select_tv);
25257489
PH
448#endif
449/* End Erik's patch */
450
bfe645c1
JH
451 if (result == -1 && errno == EINTR)
452 goto again;
453 else if (result < 1)
454 {
455 if (result == -1)
80832b14 456 log_write(0, LOG_MAIN|LOG_PANIC,
bfe645c1
JH
457 "%s %s on spamd %s socket", loglabel, callout_address, strerror(errno));
458 else
80832b14 459 {
bfe645c1
JH
460 if (time(NULL) - start < sd->timeout)
461 goto again;
462 log_write(0, LOG_MAIN|LOG_PANIC,
463 "%s timed out writing spamd %s, socket", loglabel, callout_address);
80832b14 464 }
bfe645c1
JH
465 (void)close(spamd_sock);
466 goto defer;
cfe75fc3 467 }
80832b14 468
bfe645c1
JH
469 wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0);
470 if (wrote == -1)
471 {
472 log_write(0, LOG_MAIN|LOG_PANIC,
473 "%s %s on spamd %s socket", loglabel, callout_address, strerror(errno));
474 (void)close(spamd_sock);
475 goto defer;
476 }
477 if (offset + wrote != read)
478 {
479 offset += wrote;
480 goto again;
481 }
80832b14 482 }
bfe645c1
JH
483 }
484while (!feof(mbox_file) && !ferror(mbox_file));
8523533c 485
bfe645c1
JH
486if (ferror(mbox_file))
487 {
488 log_write(0, LOG_MAIN|LOG_PANIC,
489 "%s error reading spool file: %s", loglabel, strerror(errno));
490 (void)close(spamd_sock);
491 goto defer;
492 }
493
494(void)fclose(mbox_file);
495
496/* we're done sending, close socket for writing */
4fab92fb
HSHR
497if (!sd->is_rspamd)
498 shutdown(spamd_sock,SHUT_WR);
bfe645c1
JH
499
500/* read spamd response using what's left of the timeout. */
501memset(spamd_buffer, 0, sizeof(spamd_buffer));
502offset = 0;
503while ((i = ip_recv(spamd_sock,
504 spamd_buffer + offset,
505 sizeof(spamd_buffer) - offset - 1,
4fab92fb 506 sd->timeout - time(NULL) + start)) > 0)
bfe645c1 507 offset += i;
4fab92fb 508spamd_buffer[offset] = '\0'; /* guard byte */
bfe645c1
JH
509
510/* error handling */
511if (i <= 0 && errno != 0)
512 {
513 log_write(0, LOG_MAIN|LOG_PANIC,
514 "%s error reading from spamd %s, socket: %s", loglabel, callout_address, strerror(errno));
515 (void)close(spamd_sock);
516 return DEFER;
517 }
518
519/* reading done */
520(void)close(spamd_sock);
521
522if (sd->is_rspamd)
523 { /* rspamd variant of reply */
524 int r;
4fab92fb 525 if ( (r = sscanf(CS spamd_buffer,
bfe645c1
JH
526 "RSPAMD/%7s 0 EX_OK\r\nMetric: default; %7s %lf / %lf / %lf\r\n%n",
527 spamd_version, spamd_short_result, &spamd_score, &spamd_threshold,
4fab92fb
HSHR
528 &spamd_reject_score, &spamd_report_offset)) != 5
529 || spamd_report_offset >= offset /* verify within buffer */
530 )
80832b14 531 {
8523533c 532 log_write(0, LOG_MAIN|LOG_PANIC,
bfe645c1 533 "%s cannot parse spamd %s, output: %d", loglabel, callout_address, r);
8523533c 534 return DEFER;
80832b14 535 }
bfe645c1
JH
536 /* now parse action */
537 p = &spamd_buffer[spamd_report_offset];
8523533c 538
bfe645c1 539 if (Ustrncmp(p, "Action: ", sizeof("Action: ") - 1) == 0)
80832b14 540 {
bfe645c1
JH
541 p += sizeof("Action: ") - 1;
542 q = &spam_action_buffer[0];
543 while (*p && *p != '\r' && (q - spam_action_buffer) < sizeof(spam_action_buffer) - 1)
544 *q++ = *p++;
545 *q = '\0';
80832b14 546 }
bfe645c1
JH
547 }
548else
549 { /* spamassassin */
550 /* dig in the spamd output and put the report in a multiline header,
551 if requested */
552 if (sscanf(CS spamd_buffer,
553 "SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
554 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3)
80832b14 555 {
bfe645c1
JH
556 /* try to fall back to pre-2.50 spamd output */
557 if (sscanf(CS spamd_buffer,
558 "SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
559 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3)
560 {
561 log_write(0, LOG_MAIN|LOG_PANIC,
562 "%s cannot parse spamd %s output", loglabel, callout_address);
563 return DEFER;
564 }
80832b14 565 }
8523533c 566
bfe645c1
JH
567 Ustrcpy(spam_action_buffer,
568 spamd_score >= spamd_threshold ? "reject" : "no action");
569 }
570
571/* Create report. Since this is a multiline string,
572we must hack it into shape first */
573p = &spamd_buffer[spamd_report_offset];
574q = spam_report_buffer;
575while (*p != '\0')
576 {
577 /* skip \r */
578 if (*p == '\r')
80832b14 579 {
bfe645c1
JH
580 p++;
581 continue;
80832b14 582 }
bfe645c1
JH
583 *q++ = *p;
584 if (*p++ == '\n')
80832b14 585 {
bfe645c1
JH
586 /* add an extra space after the newline to ensure
587 that it is treated as a header continuation line */
588 *q++ = ' ';
80832b14 589 }
bfe645c1
JH
590 }
591/* NULL-terminate */
592*q-- = '\0';
593/* cut off trailing leftovers */
594while (*q <= ' ')
595 *q-- = '\0';
8e669ac1 596
bfe645c1
JH
597spam_report = spam_report_buffer;
598spam_action = spam_action_buffer;
599
600/* create spam bar */
601spamd_score_char = spamd_score > 0 ? '+' : '-';
602j = abs((int)(spamd_score));
603i = 0;
604if (j != 0)
605 while ((i < j) && (i <= MAX_SPAM_BAR_CHARS))
606 spam_bar_buffer[i++] = spamd_score_char;
607else
608 {
609 spam_bar_buffer[0] = '/';
610 i = 1;
611 }
612spam_bar_buffer[i] = '\0';
613spam_bar = spam_bar_buffer;
614
615/* create "float" spam score */
616(void)string_format(spam_score_buffer, sizeof(spam_score_buffer),
617 "%.1f", spamd_score);
618spam_score = spam_score_buffer;
619
620/* create "int" spam score */
621j = (int)((spamd_score + 0.001)*10);
622(void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer),
623 "%d", j);
624spam_score_int = spam_score_int_buffer;
625
626/* compare threshold against score */
627spam_rc = spamd_score >= spamd_threshold
628 ? OK /* spam as determined by user's threshold */
629 : FAIL; /* not spam */
630
631/* remember expanded spamd_address if needed */
632if (spamd_address_work != spamd_address)
633 prev_spamd_address_work = string_copy(spamd_address_work);
634
635/* remember user name and "been here" for it */
636Ustrcpy(prev_user_name, user_name);
637spam_ok = 1;
638
639return override
640 ? OK /* always return OK, no matter what the score */
641 : spam_rc;
642
643defer:
644 (void)fclose(mbox_file);
645 return DEFER;
8523533c
TK
646}
647
648#endif
bfe645c1
JH
649/* vi: aw ai sw=2
650*/