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