1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003 - 2015
7 * Copyright (c) The Exim Maintainers 2016 - 2018
10 /* Code for calling spamassassin's spamd. Called from acl.c. */
13 #ifdef WITH_CONTENT_SCAN
16 uschar spam_score_buffer[16];
17 uschar spam_score_int_buffer[16];
18 uschar spam_bar_buffer[128];
19 uschar * spam_action_buffer;
20 uschar spam_report_buffer[32600];
21 uschar prev_user_name[128] = "";
24 uschar *prev_spamd_address_work = NULL;
26 static const uschar * loglabel = US"spam acl condition:";
30 spamd_param_init(spamd_address_container *spamd)
32 /* default spamd server weight, time and priority value */
33 spamd->is_failed = FALSE;
34 spamd->weight = SPAMD_WEIGHT;
35 spamd->timeout = SPAMD_TIMEOUT;
43 spamd_param(const uschar * param, spamd_address_container * spamd)
45 static int timesinceday = -1;
49 /*XXX more clever parsing could discard embedded spaces? */
51 if (sscanf(CCS param, "pri=%u", &spamd->priority))
54 if (sscanf(CCS param, "weight=%u", &spamd->weight))
56 if (spamd->weight == 0) /* this server disabled: skip it */
61 if (Ustrncmp(param, "time=", 5) == 0)
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 const uschar * end_string;
70 if ((end_string = Ustrchr(s, '-')))
73 if ( sscanf(CS end_string, "%u.%u.%u", &end_h, &end_m, &end_s) == 0
74 || sscanf(CS s, "%u.%u.%u", &start_h, &start_m, &start_s) == 0
83 time_t now = time(NULL);
84 struct tm *tmp = localtime(&now);
85 timesinceday = tmp->tm_hour*3600 + tmp->tm_min*60 + tmp->tm_sec;
88 time_start = start_h*3600 + start_m*60 + start_s;
89 time_end = end_h*3600 + end_m*60 + end_s;
91 if (timesinceday < time_start || timesinceday >= time_end)
92 return 1; /* skip spamd server */
97 if (Ustrncmp(param, "tmo=", 4) == 0)
99 int sec = readconf_readtime((s = param+4), '\0', FALSE);
103 spamd->timeout = sec;
107 if (Ustrncmp(param, "retry=", 6) == 0)
109 int sec = readconf_readtime((s = param+6), '\0', FALSE);
117 log_write(0, LOG_MAIN, "%s warning - invalid spamd parameter: '%s'",
119 return -1; /* syntax error */
122 log_write(0, LOG_MAIN,
123 "%s warning - invalid spamd %s value: '%s'", loglabel, name, s);
124 return -1; /* syntax error */
129 spamd_get_server(spamd_address_container ** spamds, int num_servers)
132 spamd_address_container * sd;
135 static BOOL srandomed = FALSE;
137 /* speedup, if we have only 1 server */
138 if (num_servers == 1)
139 return (spamds[0]->is_failed ? -1 : 0);
145 gettimeofday(&tv, NULL);
146 srandom((unsigned int)(tv.tv_usec/1000));
150 /* scan for highest pri */
151 for (pri = 0, i = 0; i < num_servers; i++)
154 if (!sd->is_failed && sd->priority > pri) pri = sd->priority;
157 /* get sum of weights */
158 for (weights = 0, i = 0; i < num_servers; i++)
161 if (!sd->is_failed && sd->priority == pri) weights += sd->weight;
163 if (weights == 0) /* all servers failed */
166 for (rnd = random() % weights, i = 0; i < num_servers; i++)
169 if (!sd->is_failed && sd->priority == pri)
170 if ((rnd -= sd->weight) <= 0)
174 log_write(0, LOG_MAIN|LOG_PANIC,
175 "%s unknown error (memory/cpu corruption?)", loglabel);
181 spam(const uschar **listptr)
184 const uschar *list = *listptr;
186 uschar user_name_buffer[128];
187 unsigned long mbox_size;
189 client_conn_ctx spamd_cctx = {.sock = -1};
190 uschar spamd_buffer[32600];
191 int i, j, offset, result;
192 uschar spamd_version[8];
193 uschar spamd_short_result[8];
194 uschar spamd_score_char;
195 double spamd_threshold, spamd_score, spamd_reject_score;
196 int spamd_report_offset;
202 struct pollfd pollfd;
203 #else /* Patch posted by Erik ? for OS X */
204 struct timeval select_tv; /* and applied by PH */
207 uschar *spamd_address_work;
208 spamd_address_container * sd;
210 /* stop compiler warning */
213 /* find the username from the option list */
214 if ((user_name = string_nextinlist(&list, &sep,
216 sizeof(user_name_buffer))) == NULL)
218 /* no username given, this means no scanning should be done */
222 /* if username is "0" or "false", do not scan */
223 if ( (Ustrcmp(user_name,"0") == 0) ||
224 (strcmpic(user_name,US"false") == 0) )
227 /* if there is an additional option, check if it is "true" */
228 if (strcmpic(list,US"true") == 0)
229 /* in that case, always return true later */
232 /* expand spamd_address if needed */
233 if (*spamd_address == '$')
235 spamd_address_work = expand_string(spamd_address);
236 if (spamd_address_work == NULL)
238 log_write(0, LOG_MAIN|LOG_PANIC,
239 "%s spamd_address starts with $, but expansion failed: %s",
240 loglabel, expand_string_message);
245 spamd_address_work = spamd_address;
247 DEBUG(D_acl) debug_printf_indent("spamd: addrlist '%s'\n", spamd_address_work);
249 /* check if previous spamd_address was expanded and has changed. dump cached results if so */
251 && prev_spamd_address_work != NULL
252 && Ustrcmp(prev_spamd_address_work, spamd_address_work) != 0
256 /* if we scanned for this username last time, just return */
257 if (spam_ok && Ustrcmp(prev_user_name, user_name) == 0)
258 return override ? OK : spam_rc;
260 /* make sure the eml mbox file is spooled up */
262 if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
263 { /* error while spooling */
264 log_write(0, LOG_MAIN|LOG_PANIC,
265 "%s error while creating mbox spool file", loglabel);
275 const uschar * spamd_address_list_ptr = spamd_address_work;
276 spamd_address_container * spamd_address_vector[32];
278 /* Check how many spamd servers we have
279 and register their addresses */
280 sep = 0; /* default colon-sep */
281 while ((address = string_nextinlist(&spamd_address_list_ptr, &sep, NULL, 0)))
283 const uschar * sublist;
284 int sublist_sep = -(int)' '; /* default space-sep */
288 DEBUG(D_acl) debug_printf_indent("spamd: addr entry '%s'\n", address);
289 sd = (spamd_address_container *)store_get(sizeof(spamd_address_container));
291 for (sublist = address, args = 0, spamd_param_init(sd);
292 (s = string_nextinlist(&sublist, &sublist_sep, NULL, 0));
296 DEBUG(D_acl) debug_printf_indent("spamd: addr parm '%s'\n", s);
299 case 0: sd->hostspec = s;
300 if (*s == '/') args++; /* local; no port */
302 case 1: sd->hostspec = string_sprintf("%s %s", sd->hostspec, s);
304 default: spamd_param(s, sd);
310 log_write(0, LOG_MAIN,
311 "%s warning - invalid spamd address: '%s'", loglabel, address);
315 spamd_address_vector[num_servers] = sd;
316 if (++num_servers > 31)
320 /* check if we have at least one server */
323 log_write(0, LOG_MAIN|LOG_PANIC,
324 "%s no useable spamd server addresses in spamd_address configuration option.",
329 current_server = spamd_get_server(spamd_address_vector, num_servers);
330 sd = spamd_address_vector[current_server];
335 DEBUG(D_acl) debug_printf_indent("spamd: trying server %s\n", sd->hostspec);
339 /*XXX could potentially use TFO early-data here */
340 if ( (spamd_cctx.sock = ip_streamsocket(sd->hostspec, &errstr, 5)) >= 0
344 DEBUG(D_acl) debug_printf_indent("spamd: server %s: retry conn\n", sd->hostspec);
345 while (sd->retry > 0) sd->retry = sleep(sd->retry);
347 if (spamd_cctx.sock >= 0)
350 log_write(0, LOG_MAIN, "%s spamd: %s", loglabel, errstr);
351 sd->is_failed = TRUE;
353 current_server = spamd_get_server(spamd_address_vector, num_servers);
354 if (current_server < 0)
356 log_write(0, LOG_MAIN|LOG_PANIC, "%s all spamd servers failed", loglabel);
359 sd = spamd_address_vector[current_server];
363 (void)fcntl(spamd_cctx.sock, F_SETFL, O_NONBLOCK);
364 /* now we are connected to spamd on spamd_sock */
366 (void)string_format(spamd_buffer,
367 sizeof(spamd_buffer),
368 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
371 /* send our request */
372 wrote = send(spamd_cctx.sock, spamd_buffer, Ustrlen(spamd_buffer), 0);
376 (void)close(spamd_cctx.sock);
377 log_write(0, LOG_MAIN|LOG_PANIC,
378 "%s spamd %s send failed: %s", loglabel, callout_address, strerror(errno));
382 /* now send the file */
383 /* spamd sometimes accepts connections but doesn't read data off
384 * the connection. We make the file descriptor non-blocking so
385 * that the write will only write sufficient data without blocking
386 * and we poll the descriptor to make sure that we can write without
387 * blocking. Short writes are gracefully handled and if the whole
388 * transaction takes too long it is aborted.
389 * Note: poll() is not supported in OSX 10.2 and is reported to be
390 * broken in more recent versions (up to 10.4).
393 pollfd.fd = spamd_cctx.sock;
394 pollfd.events = POLLOUT;
398 read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
404 result = poll(&pollfd, 1, 1000);
406 /* Patch posted by Erik ? for OS X and applied by PH */
408 select_tv.tv_sec = 1;
409 select_tv.tv_usec = 0;
411 FD_SET(spamd_cctx.sock, &select_fd);
412 result = select(spamd_cctx.sock+1, NULL, &select_fd, NULL, &select_tv);
414 /* End Erik's patch */
416 if (result == -1 && errno == EINTR)
421 log_write(0, LOG_MAIN|LOG_PANIC,
422 "%s %s on spamd %s socket", loglabel, callout_address, strerror(errno));
425 if (time(NULL) - start < sd->timeout)
427 log_write(0, LOG_MAIN|LOG_PANIC,
428 "%s timed out writing spamd %s, socket", loglabel, callout_address);
430 (void)close(spamd_cctx.sock);
434 wrote = send(spamd_cctx.sock,spamd_buffer + offset,read - offset,0);
437 log_write(0, LOG_MAIN|LOG_PANIC,
438 "%s %s on spamd %s socket", loglabel, callout_address, strerror(errno));
439 (void)close(spamd_cctx.sock);
442 if (offset + wrote != read)
449 while (!feof(mbox_file) && !ferror(mbox_file));
451 if (ferror(mbox_file))
453 log_write(0, LOG_MAIN|LOG_PANIC,
454 "%s error reading spool file: %s", loglabel, strerror(errno));
455 (void)close(spamd_cctx.sock);
459 (void)fclose(mbox_file);
461 /* we're done sending, close socket for writing */
462 shutdown(spamd_cctx.sock, SHUT_WR);
464 /* read spamd response using what's left of the timeout. */
465 memset(spamd_buffer, 0, sizeof(spamd_buffer));
467 while ((i = ip_recv(&spamd_cctx,
468 spamd_buffer + offset,
469 sizeof(spamd_buffer) - offset - 1,
470 sd->timeout - time(NULL) + start)) > 0)
472 spamd_buffer[offset] = '\0'; /* guard byte */
475 if (i <= 0 && errno != 0)
477 log_write(0, LOG_MAIN|LOG_PANIC,
478 "%s error reading from spamd %s, socket: %s", loglabel, callout_address, strerror(errno));
479 (void)close(spamd_cctx.sock);
484 (void)close(spamd_cctx.sock);
487 /* dig in the spamd output and put the report in a multiline header,
489 if (sscanf(CS spamd_buffer,
490 "SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
491 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3)
493 /* try to fall back to pre-2.50 spamd output */
494 if (sscanf(CS spamd_buffer,
495 "SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
496 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3)
498 log_write(0, LOG_MAIN|LOG_PANIC,
499 "%s cannot parse spamd %s output", loglabel, callout_address);
504 spam_action_buffer = spamd_score >= spamd_threshold ? US"reject" : US"no action";
507 /* Create report. Since this is a multiline string,
508 we must hack it into shape first */
509 p = &spamd_buffer[spamd_report_offset];
510 q = spam_report_buffer;
522 /* add an extra space after the newline to ensure
523 that it is treated as a header continuation line */
529 /* cut off trailing leftovers */
533 spam_report = spam_report_buffer;
534 spam_action = spam_action_buffer;
536 /* create spam bar */
537 spamd_score_char = spamd_score > 0 ? '+' : '-';
538 j = abs((int)(spamd_score));
541 while ((i < j) && (i <= MAX_SPAM_BAR_CHARS))
542 spam_bar_buffer[i++] = spamd_score_char;
545 spam_bar_buffer[0] = '/';
548 spam_bar_buffer[i] = '\0';
549 spam_bar = spam_bar_buffer;
551 /* create "float" spam score */
552 (void)string_format(spam_score_buffer, sizeof(spam_score_buffer),
553 "%.1f", spamd_score);
554 spam_score = spam_score_buffer;
556 /* create "int" spam score */
557 j = (int)((spamd_score + 0.001)*10);
558 (void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer),
560 spam_score_int = spam_score_int_buffer;
562 /* compare threshold against score */
563 spam_rc = spamd_score >= spamd_threshold
564 ? OK /* spam as determined by user's threshold */
565 : FAIL; /* not spam */
567 /* remember expanded spamd_address if needed */
568 if (spamd_address_work != spamd_address)
569 prev_spamd_address_work = string_copy(spamd_address_work);
571 /* remember user name and "been here" for it */
572 Ustrcpy(prev_user_name, user_name);
576 ? OK /* always return OK, no matter what the score */
580 (void)fclose(mbox_file);