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