1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
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_failed
= FALSE
;
32 spamd
->weight
= SPAMD_WEIGHT
;
33 spamd
->timeout
= SPAMD_TIMEOUT
;
41 spamd_param(const uschar
* param
, spamd_address_container
* spamd
)
43 static int timesinceday
= -1;
47 /*XXX more clever parsing could discard embedded spaces? */
49 if (sscanf(CCS param
, "pri=%u", &spamd
->priority
))
52 if (sscanf(CCS param
, "weight=%u", &spamd
->weight
))
54 if (spamd
->weight
== 0) /* this server disabled: skip it */
59 if (Ustrncmp(param
, "time=", 5) == 0)
61 unsigned int start_h
= 0, start_m
= 0, start_s
= 0;
62 unsigned int end_h
= 24, end_m
= 0, end_s
= 0;
63 unsigned int time_start
, time_end
;
64 const uschar
* end_string
;
68 if ((end_string
= Ustrchr(s
, '-')))
71 if ( sscanf(CS end_string
, "%u.%u.%u", &end_h
, &end_m
, &end_s
) == 0
72 || sscanf(CS s
, "%u.%u.%u", &start_h
, &start_m
, &start_s
) == 0
81 time_t now
= time(NULL
);
82 struct tm
*tmp
= localtime(&now
);
83 timesinceday
= tmp
->tm_hour
*3600 + tmp
->tm_min
*60 + tmp
->tm_sec
;
86 time_start
= start_h
*3600 + start_m
*60 + start_s
;
87 time_end
= end_h
*3600 + end_m
*60 + end_s
;
89 if (timesinceday
< time_start
|| timesinceday
>= time_end
)
90 return 1; /* skip spamd server */
95 if (Ustrcmp(param
, "variant=rspamd") == 0)
97 spamd
->is_rspamd
= TRUE
;
101 if (Ustrncmp(param
, "tmo=", 4) == 0)
103 int sec
= readconf_readtime((s
= param
+4), '\0', FALSE
);
107 spamd
->timeout
= sec
;
111 if (Ustrncmp(param
, "retry=", 6) == 0)
113 int sec
= readconf_readtime((s
= param
+6), '\0', FALSE
);
121 log_write(0, LOG_MAIN
, "%s warning - invalid spamd parameter: '%s'",
123 return -1; /* syntax error */
126 log_write(0, LOG_MAIN
,
127 "%s warning - invalid spamd %s value: '%s'", loglabel
, name
, s
);
128 return -1; /* syntax error */
133 spamd_get_server(spamd_address_container
** spamds
, int num_servers
)
136 spamd_address_container
* sd
;
139 static BOOL srandomed
= FALSE
;
141 /* seedup, if we have only 1 server */
142 if (num_servers
== 1)
143 return (spamds
[0]->is_failed
? -1 : 0);
149 gettimeofday(&tv
, NULL
);
150 srandom((unsigned int)(tv
.tv_usec
/1000));
154 /* scan for highest pri */
155 for (pri
= 0, i
= 0; i
< num_servers
; i
++)
158 if (!sd
->is_failed
&& sd
->priority
> pri
) pri
= sd
->priority
;
161 /* get sum of weights */
162 for (weights
= 0, i
= 0; i
< num_servers
; i
++)
165 if (!sd
->is_failed
&& sd
->priority
== pri
) weights
+= sd
->weight
;
167 if (weights
== 0) /* all servers failed */
170 for (rnd
= random() % weights
, i
= 0; i
< num_servers
; i
++)
173 if (!sd
->is_failed
&& sd
->priority
== pri
)
174 if ((rnd
-= sd
->weight
) <= 0)
178 log_write(0, LOG_MAIN
|LOG_PANIC
,
179 "%s unknown error (memory/cpu corruption?)", loglabel
);
185 spam(const uschar
**listptr
)
188 const uschar
*list
= *listptr
;
190 uschar user_name_buffer
[128];
191 unsigned long mbox_size
;
194 uschar spamd_buffer
[32600];
195 int i
, j
, offset
, result
;
196 uschar spamd_version
[8];
197 uschar spamd_short_result
[8];
198 uschar spamd_score_char
;
199 double spamd_threshold
, spamd_score
, spamd_reject_score
;
200 int spamd_report_offset
;
206 struct pollfd pollfd
;
207 #else /* Patch posted by Erik ? for OS X */
208 struct timeval select_tv
; /* and applied by PH */
211 uschar
*spamd_address_work
;
212 spamd_address_container
* sd
;
214 /* stop compiler warning */
217 /* find the username from the option list */
218 if ((user_name
= string_nextinlist(&list
, &sep
,
220 sizeof(user_name_buffer
))) == NULL
)
222 /* no username given, this means no scanning should be done */
226 /* if username is "0" or "false", do not scan */
227 if ( (Ustrcmp(user_name
,"0") == 0) ||
228 (strcmpic(user_name
,US
"false") == 0) )
231 /* if there is an additional option, check if it is "true" */
232 if (strcmpic(list
,US
"true") == 0)
233 /* in that case, always return true later */
236 /* expand spamd_address if needed */
237 if (*spamd_address
== '$')
239 spamd_address_work
= expand_string(spamd_address
);
240 if (spamd_address_work
== NULL
)
242 log_write(0, LOG_MAIN
|LOG_PANIC
,
243 "%s spamd_address starts with $, but expansion failed: %s",
244 loglabel
, expand_string_message
);
249 spamd_address_work
= spamd_address
;
251 DEBUG(D_acl
) debug_printf("spamd: addrlist '%s'\n", spamd_address_work
);
253 /* check if previous spamd_address was expanded and has changed. dump cached results if so */
255 && prev_spamd_address_work
!= NULL
256 && Ustrcmp(prev_spamd_address_work
, spamd_address_work
) != 0
260 /* if we scanned for this username last time, just return */
261 if (spam_ok
&& Ustrcmp(prev_user_name
, user_name
) == 0)
262 return override
? OK
: spam_rc
;
264 /* make sure the eml mbox file is spooled up */
265 mbox_file
= spool_mbox(&mbox_size
, NULL
);
267 if (mbox_file
== NULL
)
269 /* error while spooling */
270 log_write(0, LOG_MAIN
|LOG_PANIC
,
271 "%s error while creating mbox spool file", loglabel
);
281 const uschar
* spamd_address_list_ptr
= spamd_address_work
;
282 spamd_address_container
* spamd_address_vector
[32];
284 /* Check how many spamd servers we have
285 and register their addresses */
286 sep
= 0; /* default colon-sep */
287 while ((address
= string_nextinlist(&spamd_address_list_ptr
, &sep
,
290 const uschar
* sublist
;
291 int sublist_sep
= -(int)' '; /* default space-sep */
295 DEBUG(D_acl
) debug_printf("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("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("spamd: trying server %s\n", sd
->hostspec
);
346 if ( (spamd_sock
= ip_streamsocket(sd
->hostspec
, &errstr
, 5)) >= 0
350 DEBUG(D_acl
) debug_printf("sspamd: erver %s: retry conn\n", sd
->hostspec
);
351 while (sd
->retry
> 0) sd
->retry
= sleep(sd
->retry
);
356 log_write(0, LOG_MAIN
, "%s spamd: %s", loglabel
, errstr
);
357 sd
->is_failed
= TRUE
;
359 current_server
= spamd_get_server(spamd_address_vector
, num_servers
);
360 if (current_server
< 0)
362 log_write(0, LOG_MAIN
|LOG_PANIC
, "%s all spamd servers failed", loglabel
);
365 sd
= spamd_address_vector
[current_server
];
369 if (spamd_sock
== -1)
371 log_write(0, LOG_MAIN
|LOG_PANIC
,
372 "programming fault, spamd_sock unexpectedly unset");
376 (void)fcntl(spamd_sock
, F_SETFL
, O_NONBLOCK
);
377 /* now we are connected to spamd on spamd_sock */
379 { /* rspamd variant */
382 const uschar
* fcrdns
;
383 const uschar
* authid
;
385 req_str
= string_sprintf("CHECK RSPAMC/1.3\r\nContent-length: %lu\r\n"
386 "Queue-Id: %s\r\nFrom: <%s>\r\nRecipient-Number: %d\r\n",
387 mbox_size
, message_id
, sender_address
, recipients_count
);
388 for (i
= 0; i
< recipients_count
; i
++)
389 req_str
= string_sprintf("%sRcpt: <%s>\r\n", req_str
, recipients_list
[i
].address
);
390 if ((helo
= expand_string(US
"$sender_helo_name")) != NULL
&& *helo
!= '\0')
391 req_str
= string_sprintf("%sHelo: %s\r\n", req_str
, helo
);
392 if ((fcrdns
= expand_string(US
"$sender_host_name")) != NULL
&& *fcrdns
!= '\0')
393 req_str
= string_sprintf("%sHostname: %s\r\n", req_str
, fcrdns
);
394 if (sender_host_address
!= NULL
)
395 req_str
= string_sprintf("%sIP: %s\r\n", req_str
, sender_host_address
);
396 if ((authid
= expand_string(US
"$authenticated_id")) != NULL
&& *authid
!= '\0')
397 req_str
= string_sprintf("%sUser: %s\r\n", req_str
, authid
);
398 req_str
= string_sprintf("%s\r\n", req_str
);
399 wrote
= send(spamd_sock
, req_str
, Ustrlen(req_str
), 0);
402 { /* spamassassin variant */
403 (void)string_format(spamd_buffer
,
404 sizeof(spamd_buffer
),
405 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
408 /* send our request */
409 wrote
= send(spamd_sock
, spamd_buffer
, Ustrlen(spamd_buffer
), 0);
414 (void)close(spamd_sock
);
415 log_write(0, LOG_MAIN
|LOG_PANIC
,
416 "%s spamd send failed: %s", loglabel
, strerror(errno
));
420 /* now send the file */
421 /* spamd sometimes accepts conections but doesn't read data off
422 * the connection. We make the file descriptor non-blocking so
423 * that the write will only write sufficient data without blocking
424 * and we poll the desciptor to make sure that we can write without
425 * blocking. Short writes are gracefully handled and if the whole
426 * trasaction takes too long it is aborted.
427 * Note: poll() is not supported in OSX 10.2 and is reported to be
428 * broken in more recent versions (up to 10.4).
431 pollfd
.fd
= spamd_sock
;
432 pollfd
.events
= POLLOUT
;
434 (void)fcntl(spamd_sock
, F_SETFL
, O_NONBLOCK
);
437 read
= fread(spamd_buffer
,1,sizeof(spamd_buffer
),mbox_file
);
443 result
= poll(&pollfd
, 1, 1000);
445 /* Patch posted by Erik ? for OS X and applied by PH */
447 select_tv
.tv_sec
= 1;
448 select_tv
.tv_usec
= 0;
450 FD_SET(spamd_sock
, &select_fd
);
451 result
= select(spamd_sock
+1, NULL
, &select_fd
, NULL
, &select_tv
);
453 /* End Erik's patch */
455 if (result
== -1 && errno
== EINTR
)
460 log_write(0, LOG_MAIN
|LOG_PANIC
,
461 "%s %s on spamd socket", loglabel
, strerror(errno
));
464 if (time(NULL
) - start
< sd
->timeout
)
466 log_write(0, LOG_MAIN
|LOG_PANIC
,
467 "%s timed out writing spamd socket", loglabel
);
469 (void)close(spamd_sock
);
473 wrote
= send(spamd_sock
,spamd_buffer
+ offset
,read
- offset
,0);
476 log_write(0, LOG_MAIN
|LOG_PANIC
,
477 "%s %s on spamd socket", loglabel
, strerror(errno
));
478 (void)close(spamd_sock
);
481 if (offset
+ wrote
!= read
)
488 while (!feof(mbox_file
) && !ferror(mbox_file
));
490 if (ferror(mbox_file
))
492 log_write(0, LOG_MAIN
|LOG_PANIC
,
493 "%s error reading spool file: %s", loglabel
, strerror(errno
));
494 (void)close(spamd_sock
);
498 (void)fclose(mbox_file
);
500 /* we're done sending, close socket for writing */
501 shutdown(spamd_sock
,SHUT_WR
);
503 /* read spamd response using what's left of the timeout. */
504 memset(spamd_buffer
, 0, sizeof(spamd_buffer
));
506 while ((i
= ip_recv(spamd_sock
,
507 spamd_buffer
+ offset
,
508 sizeof(spamd_buffer
) - offset
- 1,
509 sd
->timeout
- time(NULL
) + start
)) > 0 )
513 if (i
<= 0 && errno
!= 0)
515 log_write(0, LOG_MAIN
|LOG_PANIC
,
516 "%s error reading from spamd socket: %s", loglabel
, strerror(errno
));
517 (void)close(spamd_sock
);
522 (void)close(spamd_sock
);
525 { /* rspamd variant of reply */
527 if ((r
= sscanf(CS spamd_buffer
,
528 "RSPAMD/%7s 0 EX_OK\r\nMetric: default; %7s %lf / %lf / %lf\r\n%n",
529 spamd_version
, spamd_short_result
, &spamd_score
, &spamd_threshold
,
530 &spamd_reject_score
, &spamd_report_offset
)) != 5)
532 log_write(0, LOG_MAIN
|LOG_PANIC
,
533 "%s cannot parse spamd output: %d", loglabel
, r
);
536 /* now parse action */
537 p
= &spamd_buffer
[spamd_report_offset
];
539 if (Ustrncmp(p
, "Action: ", sizeof("Action: ") - 1) == 0)
541 p
+= sizeof("Action: ") - 1;
542 q
= &spam_action_buffer
[0];
543 while (*p
&& *p
!= '\r' && (q
- spam_action_buffer
) < sizeof(spam_action_buffer
) - 1)
550 /* dig in the spamd output and put the report in a multiline header,
552 if (sscanf(CS spamd_buffer
,
553 "SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
554 spamd_version
,&spamd_score
,&spamd_threshold
,&spamd_report_offset
) != 3)
556 /* try to fall back to pre-2.50 spamd output */
557 if (sscanf(CS spamd_buffer
,
558 "SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
559 spamd_version
,&spamd_score
,&spamd_threshold
,&spamd_report_offset
) != 3)
561 log_write(0, LOG_MAIN
|LOG_PANIC
,
562 "%s cannot parse spamd output", loglabel
);
567 Ustrcpy(spam_action_buffer
,
568 spamd_score
>= spamd_threshold
? "reject" : "no action");
571 /* Create report. Since this is a multiline string,
572 we must hack it into shape first */
573 p
= &spamd_buffer
[spamd_report_offset
];
574 q
= spam_report_buffer
;
586 /* add an extra space after the newline to ensure
587 that it is treated as a header continuation line */
593 /* cut off trailing leftovers */
597 spam_report
= spam_report_buffer
;
598 spam_action
= spam_action_buffer
;
600 /* create spam bar */
601 spamd_score_char
= spamd_score
> 0 ? '+' : '-';
602 j
= abs((int)(spamd_score
));
605 while ((i
< j
) && (i
<= MAX_SPAM_BAR_CHARS
))
606 spam_bar_buffer
[i
++] = spamd_score_char
;
609 spam_bar_buffer
[0] = '/';
612 spam_bar_buffer
[i
] = '\0';
613 spam_bar
= spam_bar_buffer
;
615 /* create "float" spam score */
616 (void)string_format(spam_score_buffer
, sizeof(spam_score_buffer
),
617 "%.1f", spamd_score
);
618 spam_score
= spam_score_buffer
;
620 /* create "int" spam score */
621 j
= (int)((spamd_score
+ 0.001)*10);
622 (void)string_format(spam_score_int_buffer
, sizeof(spam_score_int_buffer
),
624 spam_score_int
= spam_score_int_buffer
;
626 /* compare threshold against score */
627 spam_rc
= spamd_score
>= spamd_threshold
628 ? OK
/* spam as determined by user's threshold */
629 : FAIL
; /* not spam */
631 /* remember expanded spamd_address if needed */
632 if (spamd_address_work
!= spamd_address
)
633 prev_spamd_address_work
= string_copy(spamd_address_work
);
635 /* remember user name and "been here" for it */
636 Ustrcpy(prev_user_name
, user_name
);
640 ? OK
/* always return OK, no matter what the score */
644 (void)fclose(mbox_file
);