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[32];
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_rspamd = FALSE;
34 spamd->is_failed = FALSE;
35 spamd->weight = SPAMD_WEIGHT;
36 spamd->timeout = SPAMD_TIMEOUT;
44 spamd_param(const uschar * param, spamd_address_container * spamd)
46 static int timesinceday = -1;
50 /*XXX more clever parsing could discard embedded spaces? */
52 if (sscanf(CCS param, "pri=%u", &spamd->priority))
55 if (sscanf(CCS param, "weight=%u", &spamd->weight))
57 if (spamd->weight == 0) /* this server disabled: skip it */
62 if (Ustrncmp(param, "time=", 5) == 0)
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;
71 if ((end_string = Ustrchr(s, '-')))
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
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;
89 time_start = start_h*3600 + start_m*60 + start_s;
90 time_end = end_h*3600 + end_m*60 + end_s;
92 if (timesinceday < time_start || timesinceday >= time_end)
93 return 1; /* skip spamd server */
98 if (Ustrcmp(param, "variant=rspamd") == 0)
100 spamd->is_rspamd = TRUE;
104 if (Ustrncmp(param, "tmo=", 4) == 0)
106 int sec = readconf_readtime((s = param+4), '\0', FALSE);
110 spamd->timeout = sec;
114 if (Ustrncmp(param, "retry=", 6) == 0)
116 int sec = readconf_readtime((s = param+6), '\0', FALSE);
124 log_write(0, LOG_MAIN, "%s warning - invalid spamd parameter: '%s'",
126 return -1; /* syntax error */
129 log_write(0, LOG_MAIN,
130 "%s warning - invalid spamd %s value: '%s'", loglabel, name, s);
131 return -1; /* syntax error */
136 spamd_get_server(spamd_address_container ** spamds, int num_servers)
139 spamd_address_container * sd;
142 static BOOL srandomed = FALSE;
144 /* speedup, if we have only 1 server */
145 if (num_servers == 1)
146 return (spamds[0]->is_failed ? -1 : 0);
152 gettimeofday(&tv, NULL);
153 srandom((unsigned int)(tv.tv_usec/1000));
157 /* scan for highest pri */
158 for (pri = 0, i = 0; i < num_servers; i++)
161 if (!sd->is_failed && sd->priority > pri) pri = sd->priority;
164 /* get sum of weights */
165 for (weights = 0, i = 0; i < num_servers; i++)
168 if (!sd->is_failed && sd->priority == pri) weights += sd->weight;
170 if (weights == 0) /* all servers failed */
173 for (rnd = random() % weights, i = 0; i < num_servers; i++)
176 if (!sd->is_failed && sd->priority == pri)
177 if ((rnd -= sd->weight) <= 0)
181 log_write(0, LOG_MAIN|LOG_PANIC,
182 "%s unknown error (memory/cpu corruption?)", loglabel);
188 spam(const uschar **listptr)
191 const uschar *list = *listptr;
193 uschar user_name_buffer[128];
194 unsigned long mbox_size;
196 client_conn_ctx spamd_cctx = {.sock = -1};
197 uschar spamd_buffer[32600];
198 int i, j, offset, result;
199 uschar spamd_version[8];
200 uschar spamd_short_result[8];
201 uschar spamd_score_char;
202 double spamd_threshold, spamd_score, spamd_reject_score;
203 int spamd_report_offset;
209 struct pollfd pollfd;
210 #else /* Patch posted by Erik ? for OS X */
211 struct timeval select_tv; /* and applied by PH */
214 uschar *spamd_address_work;
215 spamd_address_container * sd;
217 /* stop compiler warning */
220 /* find the username from the option list */
221 if ((user_name = string_nextinlist(&list, &sep,
223 sizeof(user_name_buffer))) == NULL)
225 /* no username given, this means no scanning should be done */
229 /* if username is "0" or "false", do not scan */
230 if ( (Ustrcmp(user_name,"0") == 0) ||
231 (strcmpic(user_name,US"false") == 0) )
234 /* if there is an additional option, check if it is "true" */
235 if (strcmpic(list,US"true") == 0)
236 /* in that case, always return true later */
239 /* expand spamd_address if needed */
240 if (*spamd_address == '$')
242 spamd_address_work = expand_string(spamd_address);
243 if (spamd_address_work == NULL)
245 log_write(0, LOG_MAIN|LOG_PANIC,
246 "%s spamd_address starts with $, but expansion failed: %s",
247 loglabel, expand_string_message);
252 spamd_address_work = spamd_address;
254 DEBUG(D_acl) debug_printf_indent("spamd: addrlist '%s'\n", spamd_address_work);
256 /* check if previous spamd_address was expanded and has changed. dump cached results if so */
258 && prev_spamd_address_work != NULL
259 && Ustrcmp(prev_spamd_address_work, spamd_address_work) != 0
263 /* if we scanned for this username last time, just return */
264 if (spam_ok && Ustrcmp(prev_user_name, user_name) == 0)
265 return override ? OK : spam_rc;
267 /* make sure the eml mbox file is spooled up */
269 if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
270 { /* error while spooling */
271 log_write(0, LOG_MAIN|LOG_PANIC,
272 "%s error while creating mbox spool file", loglabel);
282 const uschar * spamd_address_list_ptr = spamd_address_work;
283 spamd_address_container * spamd_address_vector[32];
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, NULL, 0)))
290 const uschar * sublist;
291 int sublist_sep = -(int)' '; /* default space-sep */
295 DEBUG(D_acl) debug_printf_indent("spamd: addr entry '%s'\n", address);
296 sd = (spamd_address_container *)store_get(sizeof(spamd_address_container));
298 for (sublist = address, args = 0, spamd_param_init(sd);
299 (s = string_nextinlist(&sublist, &sublist_sep, NULL, 0));
303 DEBUG(D_acl) debug_printf_indent("spamd: addr parm '%s'\n", s);
306 case 0: sd->hostspec = s;
307 if (*s == '/') args++; /* local; no port */
309 case 1: sd->hostspec = string_sprintf("%s %s", sd->hostspec, s);
311 default: spamd_param(s, sd);
317 log_write(0, LOG_MAIN,
318 "%s warning - invalid spamd address: '%s'", loglabel, address);
322 spamd_address_vector[num_servers] = sd;
323 if (++num_servers > 31)
327 /* check if we have at least one server */
330 log_write(0, LOG_MAIN|LOG_PANIC,
331 "%s no useable spamd server addresses in spamd_address configuration option.",
336 current_server = spamd_get_server(spamd_address_vector, num_servers);
337 sd = spamd_address_vector[current_server];
342 DEBUG(D_acl) debug_printf_indent("spamd: trying server %s\n", sd->hostspec);
346 /*XXX could potentially use TFO early-data here */
347 if ( (spamd_cctx.sock = ip_streamsocket(sd->hostspec, &errstr, 5)) >= 0
351 DEBUG(D_acl) debug_printf_indent("spamd: server %s: retry conn\n", sd->hostspec);
352 while (sd->retry > 0) sd->retry = sleep(sd->retry);
354 if (spamd_cctx.sock >= 0)
357 log_write(0, LOG_MAIN, "%s spamd: %s", loglabel, errstr);
358 sd->is_failed = TRUE;
360 current_server = spamd_get_server(spamd_address_vector, num_servers);
361 if (current_server < 0)
363 log_write(0, LOG_MAIN|LOG_PANIC, "%s all spamd servers failed", loglabel);
366 sd = spamd_address_vector[current_server];
370 (void)fcntl(spamd_cctx.sock, F_SETFL, O_NONBLOCK);
371 /* now we are connected to spamd on spamd_cctx.sock */
373 { /* rspamd variant */
376 const uschar * fcrdns;
377 const uschar * authid;
379 req_str = string_sprintf("CHECK RSPAMC/1.3\r\nContent-length: %lu\r\n"
380 "Queue-Id: %s\r\nFrom: <%s>\r\nRecipient-Number: %d\r\n",
381 mbox_size, message_id, sender_address, recipients_count);
382 for (i = 0; i < recipients_count; i ++)
383 req_str = string_sprintf("%sRcpt: <%s>\r\n", req_str, recipients_list[i].address);
384 if ((helo = expand_string(US"$sender_helo_name")) != NULL && *helo != '\0')
385 req_str = string_sprintf("%sHelo: %s\r\n", req_str, helo);
386 if ((fcrdns = expand_string(US"$sender_host_name")) != NULL && *fcrdns != '\0')
387 req_str = string_sprintf("%sHostname: %s\r\n", req_str, fcrdns);
388 if (sender_host_address != NULL)
389 req_str = string_sprintf("%sIP: %s\r\n", req_str, sender_host_address);
390 if ((authid = expand_string(US"$authenticated_id")) != NULL && *authid != '\0')
391 req_str = string_sprintf("%sUser: %s\r\n", req_str, authid);
392 req_str = string_sprintf("%s\r\n", req_str);
393 wrote = send(spamd_cctx.sock, req_str->s, req_str->ptr, 0);
396 { /* spamassassin variant */
397 (void)string_format(spamd_buffer,
398 sizeof(spamd_buffer),
399 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
402 /* send our request */
403 wrote = send(spamd_cctx.sock, spamd_buffer, Ustrlen(spamd_buffer), 0);
408 (void)close(spamd_cctx.sock);
409 log_write(0, LOG_MAIN|LOG_PANIC,
410 "%s spamd %s send failed: %s", loglabel, callout_address, strerror(errno));
414 /* now send the file */
415 /* spamd sometimes accepts connections but doesn't read data off
416 * the connection. We make the file descriptor non-blocking so
417 * that the write will only write sufficient data without blocking
418 * and we poll the descriptor to make sure that we can write without
419 * blocking. Short writes are gracefully handled and if the whole
420 * transaction takes too long it is aborted.
421 * Note: poll() is not supported in OSX 10.2 and is reported to be
422 * broken in more recent versions (up to 10.4).
425 pollfd.fd = spamd_cctx.sock;
426 pollfd.events = POLLOUT;
428 (void)fcntl(spamd_cctx.sock, F_SETFL, O_NONBLOCK);
431 read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
437 result = poll(&pollfd, 1, 1000);
439 /* Patch posted by Erik ? for OS X and applied by PH */
441 select_tv.tv_sec = 1;
442 select_tv.tv_usec = 0;
444 FD_SET(spamd_cctx.sock, &select_fd);
445 result = select(spamd_cctx.sock+1, NULL, &select_fd, NULL, &select_tv);
447 /* End Erik's patch */
449 if (result == -1 && errno == EINTR)
454 log_write(0, LOG_MAIN|LOG_PANIC,
455 "%s %s on spamd %s socket", loglabel, callout_address, strerror(errno));
458 if (time(NULL) - start < sd->timeout)
460 log_write(0, LOG_MAIN|LOG_PANIC,
461 "%s timed out writing spamd %s, socket", loglabel, callout_address);
463 (void)close(spamd_cctx.sock);
467 wrote = send(spamd_cctx.sock,spamd_buffer + offset,read - offset,0);
470 log_write(0, LOG_MAIN|LOG_PANIC,
471 "%s %s on spamd %s socket", loglabel, callout_address, strerror(errno));
472 (void)close(spamd_cctx.sock);
475 if (offset + wrote != read)
482 while (!feof(mbox_file) && !ferror(mbox_file));
484 if (ferror(mbox_file))
486 log_write(0, LOG_MAIN|LOG_PANIC,
487 "%s error reading spool file: %s", loglabel, strerror(errno));
488 (void)close(spamd_cctx.sock);
492 (void)fclose(mbox_file);
494 /* we're done sending, close socket for writing */
496 shutdown(spamd_cctx.sock,SHUT_WR);
498 /* read spamd response using what's left of the timeout. */
499 memset(spamd_buffer, 0, sizeof(spamd_buffer));
501 while ((i = ip_recv(&spamd_cctx,
502 spamd_buffer + offset,
503 sizeof(spamd_buffer) - offset - 1,
504 sd->timeout - time(NULL) + start)) > 0)
506 spamd_buffer[offset] = '\0'; /* guard byte */
509 if (i <= 0 && errno != 0)
511 log_write(0, LOG_MAIN|LOG_PANIC,
512 "%s error reading from spamd %s, socket: %s", loglabel, callout_address, strerror(errno));
513 (void)close(spamd_cctx.sock);
518 (void)close(spamd_cctx.sock);
521 { /* rspamd variant of reply */
523 if ( (r = sscanf(CS spamd_buffer,
524 "RSPAMD/%7s 0 EX_OK\r\nMetric: default; %7s %lf / %lf / %lf\r\n%n",
525 spamd_version, spamd_short_result, &spamd_score, &spamd_threshold,
526 &spamd_reject_score, &spamd_report_offset)) != 5
527 || spamd_report_offset >= offset /* verify within buffer */
530 log_write(0, LOG_MAIN|LOG_PANIC,
531 "%s cannot parse spamd %s, output: %d", loglabel, callout_address, r);
534 /* now parse action */
535 p = &spamd_buffer[spamd_report_offset];
537 if (Ustrncmp(p, "Action: ", sizeof("Action: ") - 1) == 0)
539 p += sizeof("Action: ") - 1;
540 q = &spam_action_buffer[0];
541 while (*p && *p != '\r' && (q - spam_action_buffer) < sizeof(spam_action_buffer) - 1)
548 /* dig in the spamd output and put the report in a multiline header,
550 if (sscanf(CS spamd_buffer,
551 "SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
552 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3)
554 /* try to fall back to pre-2.50 spamd output */
555 if (sscanf(CS spamd_buffer,
556 "SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
557 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3)
559 log_write(0, LOG_MAIN|LOG_PANIC,
560 "%s cannot parse spamd %s output", loglabel, callout_address);
565 Ustrcpy(spam_action_buffer,
566 spamd_score >= spamd_threshold ? "reject" : "no action");
569 /* Create report. Since this is a multiline string,
570 we must hack it into shape first */
571 p = &spamd_buffer[spamd_report_offset];
572 q = spam_report_buffer;
584 /* add an extra space after the newline to ensure
585 that it is treated as a header continuation line */
591 /* cut off trailing leftovers */
595 spam_report = spam_report_buffer;
596 spam_action = spam_action_buffer;
598 /* create spam bar */
599 spamd_score_char = spamd_score > 0 ? '+' : '-';
600 j = abs((int)(spamd_score));
603 while ((i < j) && (i <= MAX_SPAM_BAR_CHARS))
604 spam_bar_buffer[i++] = spamd_score_char;
607 spam_bar_buffer[0] = '/';
610 spam_bar_buffer[i] = '\0';
611 spam_bar = spam_bar_buffer;
613 /* create "float" spam score */
614 (void)string_format(spam_score_buffer, sizeof(spam_score_buffer),
615 "%.1f", spamd_score);
616 spam_score = spam_score_buffer;
618 /* create "int" spam score */
619 j = (int)((spamd_score + 0.001)*10);
620 (void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer),
622 spam_score_int = spam_score_int_buffer;
624 /* compare threshold against score */
625 spam_rc = spamd_score >= spamd_threshold
626 ? OK /* spam as determined by user's threshold */
627 : FAIL; /* not spam */
629 /* remember expanded spamd_address if needed */
630 if (spamd_address_work != spamd_address)
631 prev_spamd_address_work = string_copy(spamd_address_work);
633 /* remember user name and "been here" for it */
634 Ustrcpy(prev_user_name, user_name);
638 ? OK /* always return OK, no matter what the score */
642 (void)fclose(mbox_file);