1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003 - 2015 */
8 /* Code for calling spamassassin's spamd. Called from acl.c. */
11 #ifdef WITH_CONTENT_SCAN
14 uschar spam_score_buffer
[16];
15 uschar spam_score_int_buffer
[16];
16 uschar spam_bar_buffer
[128];
17 uschar spam_action_buffer
[32];
18 uschar spam_report_buffer
[32600];
19 uschar prev_user_name
[128] = "";
22 uschar
*prev_spamd_address_work
= NULL
;
24 static const uschar
* loglabel
= US
"spam acl condition:";
28 spamd_param_init(spamd_address_container
*spamd
)
30 /* default spamd server weight, time and priority value */
31 spamd
->is_rspamd
= FALSE
;
32 spamd
->is_failed
= FALSE
;
33 spamd
->weight
= SPAMD_WEIGHT
;
34 spamd
->timeout
= SPAMD_TIMEOUT
;
42 spamd_param(const uschar
* param
, spamd_address_container
* spamd
)
44 static int timesinceday
= -1;
48 /*XXX more clever parsing could discard embedded spaces? */
50 if (sscanf(CCS param
, "pri=%u", &spamd
->priority
))
53 if (sscanf(CCS param
, "weight=%u", &spamd
->weight
))
55 if (spamd
->weight
== 0) /* this server disabled: skip it */
60 if (Ustrncmp(param
, "time=", 5) == 0)
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
;
69 if ((end_string
= Ustrchr(s
, '-')))
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
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
;
87 time_start
= start_h
*3600 + start_m
*60 + start_s
;
88 time_end
= end_h
*3600 + end_m
*60 + end_s
;
90 if (timesinceday
< time_start
|| timesinceday
>= time_end
)
91 return 1; /* skip spamd server */
96 if (Ustrcmp(param
, "variant=rspamd") == 0)
98 spamd
->is_rspamd
= TRUE
;
102 if (Ustrncmp(param
, "tmo=", 4) == 0)
104 int sec
= readconf_readtime((s
= param
+4), '\0', FALSE
);
108 spamd
->timeout
= sec
;
112 if (Ustrncmp(param
, "retry=", 6) == 0)
114 int sec
= readconf_readtime((s
= param
+6), '\0', FALSE
);
122 log_write(0, LOG_MAIN
, "%s warning - invalid spamd parameter: '%s'",
124 return -1; /* syntax error */
127 log_write(0, LOG_MAIN
,
128 "%s warning - invalid spamd %s value: '%s'", loglabel
, name
, s
);
129 return -1; /* syntax error */
134 spamd_get_server(spamd_address_container
** spamds
, int num_servers
)
137 spamd_address_container
* sd
;
140 static BOOL srandomed
= FALSE
;
142 /* seedup, if we have only 1 server */
143 if (num_servers
== 1)
144 return (spamds
[0]->is_failed
? -1 : 0);
150 gettimeofday(&tv
, NULL
);
151 srandom((unsigned int)(tv
.tv_usec
/1000));
155 /* scan for highest pri */
156 for (pri
= 0, i
= 0; i
< num_servers
; i
++)
159 if (!sd
->is_failed
&& sd
->priority
> pri
) pri
= sd
->priority
;
162 /* get sum of weights */
163 for (weights
= 0, i
= 0; i
< num_servers
; i
++)
166 if (!sd
->is_failed
&& sd
->priority
== pri
) weights
+= sd
->weight
;
168 if (weights
== 0) /* all servers failed */
171 for (rnd
= random() % weights
, i
= 0; i
< num_servers
; i
++)
174 if (!sd
->is_failed
&& sd
->priority
== pri
)
175 if ((rnd
-= sd
->weight
) <= 0)
179 log_write(0, LOG_MAIN
|LOG_PANIC
,
180 "%s unknown error (memory/cpu corruption?)", loglabel
);
186 spam(const uschar
**listptr
)
189 const uschar
*list
= *listptr
;
191 uschar user_name_buffer
[128];
192 unsigned long mbox_size
;
195 uschar spamd_buffer
[32600];
196 int i
, j
, offset
, result
;
197 uschar spamd_version
[8];
198 uschar spamd_short_result
[8];
199 uschar spamd_score_char
;
200 double spamd_threshold
, spamd_score
, spamd_reject_score
;
201 int spamd_report_offset
;
207 struct pollfd pollfd
;
208 #else /* Patch posted by Erik ? for OS X */
209 struct timeval select_tv
; /* and applied by PH */
212 uschar
*spamd_address_work
;
213 spamd_address_container
* sd
;
215 /* stop compiler warning */
218 /* find the username from the option list */
219 if ((user_name
= string_nextinlist(&list
, &sep
,
221 sizeof(user_name_buffer
))) == NULL
)
223 /* no username given, this means no scanning should be done */
227 /* if username is "0" or "false", do not scan */
228 if ( (Ustrcmp(user_name
,"0") == 0) ||
229 (strcmpic(user_name
,US
"false") == 0) )
232 /* if there is an additional option, check if it is "true" */
233 if (strcmpic(list
,US
"true") == 0)
234 /* in that case, always return true later */
237 /* expand spamd_address if needed */
238 if (*spamd_address
== '$')
240 spamd_address_work
= expand_string(spamd_address
);
241 if (spamd_address_work
== NULL
)
243 log_write(0, LOG_MAIN
|LOG_PANIC
,
244 "%s spamd_address starts with $, but expansion failed: %s",
245 loglabel
, expand_string_message
);
250 spamd_address_work
= spamd_address
;
252 DEBUG(D_acl
) debug_printf("spamd: addrlist '%s'\n", spamd_address_work
);
254 /* check if previous spamd_address was expanded and has changed. dump cached results if so */
256 && prev_spamd_address_work
!= NULL
257 && Ustrcmp(prev_spamd_address_work
, spamd_address_work
) != 0
261 /* if we scanned for this username last time, just return */
262 if (spam_ok
&& Ustrcmp(prev_user_name
, user_name
) == 0)
263 return override
? OK
: spam_rc
;
265 /* make sure the eml mbox file is spooled up */
266 mbox_file
= spool_mbox(&mbox_size
, NULL
);
268 if (mbox_file
== 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
,
291 const uschar
* sublist
;
292 int sublist_sep
= -(int)' '; /* default space-sep */
296 DEBUG(D_acl
) debug_printf("spamd: addr entry '%s'\n", address
);
297 sd
= (spamd_address_container
*)store_get(sizeof(spamd_address_container
));
299 for (sublist
= address
, args
= 0, spamd_param_init(sd
);
300 (s
= string_nextinlist(&sublist
, &sublist_sep
, NULL
, 0));
304 DEBUG(D_acl
) debug_printf("spamd: addr parm '%s'\n", s
);
307 case 0: sd
->hostspec
= s
;
308 if (*s
== '/') args
++; /* local; no port */
310 case 1: sd
->hostspec
= string_sprintf("%s %s", sd
->hostspec
, s
);
312 default: spamd_param(s
, sd
);
318 log_write(0, LOG_MAIN
,
319 "%s warning - invalid spamd address: '%s'", loglabel
, address
);
323 spamd_address_vector
[num_servers
] = sd
;
324 if (++num_servers
> 31)
328 /* check if we have at least one server */
331 log_write(0, LOG_MAIN
|LOG_PANIC
,
332 "%s no useable spamd server addresses in spamd_address configuration option.",
337 current_server
= spamd_get_server(spamd_address_vector
, num_servers
);
338 sd
= spamd_address_vector
[current_server
];
343 DEBUG(D_acl
) debug_printf("spamd: trying server %s\n", sd
->hostspec
);
347 if ( (spamd_sock
= ip_streamsocket(sd
->hostspec
, &errstr
, 5)) >= 0
351 DEBUG(D_acl
) debug_printf("spamd: server %s: retry conn\n", sd
->hostspec
);
352 while (sd
->retry
> 0) sd
->retry
= sleep(sd
->retry
);
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 if (spamd_sock
== -1)
372 log_write(0, LOG_MAIN
|LOG_PANIC
,
373 "programming fault, spamd_sock unexpectedly unset");
377 (void)fcntl(spamd_sock
, F_SETFL
, O_NONBLOCK
);
378 /* now we are connected to spamd on spamd_sock */
380 { /* rspamd variant */
383 const uschar
* fcrdns
;
384 const uschar
* authid
;
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);
403 { /* spamassassin variant */
404 (void)string_format(spamd_buffer
,
405 sizeof(spamd_buffer
),
406 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
409 /* send our request */
410 wrote
= send(spamd_sock
, spamd_buffer
, Ustrlen(spamd_buffer
), 0);
415 (void)close(spamd_sock
);
416 log_write(0, LOG_MAIN
|LOG_PANIC
,
417 "%s spamd send failed: %s", loglabel
, strerror(errno
));
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).
432 pollfd
.fd
= spamd_sock
;
433 pollfd
.events
= POLLOUT
;
435 (void)fcntl(spamd_sock
, F_SETFL
, O_NONBLOCK
);
438 read
= fread(spamd_buffer
,1,sizeof(spamd_buffer
),mbox_file
);
444 result
= poll(&pollfd
, 1, 1000);
446 /* Patch posted by Erik ? for OS X and applied by PH */
448 select_tv
.tv_sec
= 1;
449 select_tv
.tv_usec
= 0;
451 FD_SET(spamd_sock
, &select_fd
);
452 result
= select(spamd_sock
+1, NULL
, &select_fd
, NULL
, &select_tv
);
454 /* End Erik's patch */
456 if (result
== -1 && errno
== EINTR
)
461 log_write(0, LOG_MAIN
|LOG_PANIC
,
462 "%s %s on spamd socket", loglabel
, strerror(errno
));
465 if (time(NULL
) - start
< sd
->timeout
)
467 log_write(0, LOG_MAIN
|LOG_PANIC
,
468 "%s timed out writing spamd socket", loglabel
);
470 (void)close(spamd_sock
);
474 wrote
= send(spamd_sock
,spamd_buffer
+ offset
,read
- offset
,0);
477 log_write(0, LOG_MAIN
|LOG_PANIC
,
478 "%s %s on spamd socket", loglabel
, strerror(errno
));
479 (void)close(spamd_sock
);
482 if (offset
+ wrote
!= read
)
489 while (!feof(mbox_file
) && !ferror(mbox_file
));
491 if (ferror(mbox_file
))
493 log_write(0, LOG_MAIN
|LOG_PANIC
,
494 "%s error reading spool file: %s", loglabel
, strerror(errno
));
495 (void)close(spamd_sock
);
499 (void)fclose(mbox_file
);
501 /* we're done sending, close socket for writing */
502 shutdown(spamd_sock
,SHUT_WR
);
504 /* read spamd response using what's left of the timeout. */
505 memset(spamd_buffer
, 0, sizeof(spamd_buffer
));
507 while ((i
= ip_recv(spamd_sock
,
508 spamd_buffer
+ offset
,
509 sizeof(spamd_buffer
) - offset
- 1,
510 sd
->timeout
- time(NULL
) + start
)) > 0 )
514 if (i
<= 0 && errno
!= 0)
516 log_write(0, LOG_MAIN
|LOG_PANIC
,
517 "%s error reading from spamd socket: %s", loglabel
, strerror(errno
));
518 (void)close(spamd_sock
);
523 (void)close(spamd_sock
);
526 { /* rspamd variant of reply */
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)
533 log_write(0, LOG_MAIN
|LOG_PANIC
,
534 "%s cannot parse spamd output: %d", loglabel
, r
);
537 /* now parse action */
538 p
= &spamd_buffer
[spamd_report_offset
];
540 if (Ustrncmp(p
, "Action: ", sizeof("Action: ") - 1) == 0)
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)
551 /* dig in the spamd output and put the report in a multiline header,
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)
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)
562 log_write(0, LOG_MAIN
|LOG_PANIC
,
563 "%s cannot parse spamd output", loglabel
);
568 Ustrcpy(spam_action_buffer
,
569 spamd_score
>= spamd_threshold
? "reject" : "no action");
572 /* Create report. Since this is a multiline string,
573 we must hack it into shape first */
574 p
= &spamd_buffer
[spamd_report_offset
];
575 q
= spam_report_buffer
;
587 /* add an extra space after the newline to ensure
588 that it is treated as a header continuation line */
594 /* cut off trailing leftovers */
598 spam_report
= spam_report_buffer
;
599 spam_action
= spam_action_buffer
;
601 /* create spam bar */
602 spamd_score_char
= spamd_score
> 0 ? '+' : '-';
603 j
= abs((int)(spamd_score
));
606 while ((i
< j
) && (i
<= MAX_SPAM_BAR_CHARS
))
607 spam_bar_buffer
[i
++] = spamd_score_char
;
610 spam_bar_buffer
[0] = '/';
613 spam_bar_buffer
[i
] = '\0';
614 spam_bar
= spam_bar_buffer
;
616 /* create "float" spam score */
617 (void)string_format(spam_score_buffer
, sizeof(spam_score_buffer
),
618 "%.1f", spamd_score
);
619 spam_score
= spam_score_buffer
;
621 /* create "int" spam score */
622 j
= (int)((spamd_score
+ 0.001)*10);
623 (void)string_format(spam_score_int_buffer
, sizeof(spam_score_int_buffer
),
625 spam_score_int
= spam_score_int_buffer
;
627 /* compare threshold against score */
628 spam_rc
= spamd_score
>= spamd_threshold
629 ? OK
/* spam as determined by user's threshold */
630 : FAIL
; /* not spam */
632 /* remember expanded spamd_address if needed */
633 if (spamd_address_work
!= spamd_address
)
634 prev_spamd_address_work
= string_copy(spamd_address_work
);
636 /* remember user name and "been here" for it */
637 Ustrcpy(prev_user_name
, user_name
);
641 ? OK
/* always return OK, no matter what the score */
645 (void)fclose(mbox_file
);