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