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:";
27 spamd_param_init(spamd_address_container
*spamd
)
29 /* default spamd server weight, time and backup value */
30 spamd
->weight
= SPAMD_WEIGHT
;
31 spamd
->is_failed
= FALSE
;
32 spamd
->is_backup
= FALSE
;
38 spamd_param(const uschar
*param
, spamd_address_container
*spamd
)
40 static int timesinceday
= -1;
43 /* check backup parameter */
44 if (Ustrcmp(param
, "backup") == 0)
46 spamd
->is_backup
= TRUE
;
50 /*XXX more clever parsing could discard embedded spaces? */
52 /* check weight parameter */
53 if (sscanf(param
, "weight=%u", &spamd
->weight
))
55 if (spamd
->weight
== 0) /* this server disabled: skip it */
60 /* check time parameter */
61 if (sscanf(param
, "time=%s", buffer
))
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
;
70 if ((delimiter
= US
strchr(CS buffer
, '-')))
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)
78 log_write(0, LOG_MAIN
,
79 "%s warning - invalid spamd time value: '%s'", loglabel
, buffer
);
80 return -1; /* syntax error */
85 log_write(0, LOG_MAIN
,
86 "%s warning - invalid spamd time value: '%s'", loglabel
, buffer
);
87 return -1; /* syntax error */
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
;
97 time_start
= start_h
*3600 + start_m
*60 + start_s
;
98 time_end
= end_h
*3600 + end_m
*60 + end_s
;
100 if (timesinceday
< time_start
|| timesinceday
>= time_end
)
101 return 1; /* skip spamd server */
106 if (Ustrcmp(param
, "variant=rspamd") == 0)
108 spamd
->is_rspamd
= TRUE
;
112 log_write(0, LOG_MAIN
, "%s warning - invalid spamd parameter: '%s'",
114 return -1; /* syntax error */
119 spamd_get_server(spamd_address_container
**spamds
, int num_servers
)
122 long rnd
, weights
= 0;
123 static BOOL srandomed
= 0;
124 BOOL usebackup
= FALSE
;
128 /* seedup, if we have only 1 server */
129 if (num_servers
== 1)
130 return (spamds
[0]->is_failed
? -1 : 0);
136 gettimeofday(&tv
, NULL
);
137 srandom((unsigned int)(tv
.tv_usec
/1000));
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
;
148 if (usebackup
) /* all servers failed (backups too) */
153 rnd
= random() % weights
;
155 for (i
= 0; i
< num_servers
; i
++)
156 if (!spamds
[i
]->is_failed
&& spamds
[i
]->is_backup
== usebackup
)
157 if ((rnd
-= spamds
[i
]->weight
) < 0)
160 log_write(0, LOG_MAIN
|LOG_PANIC
,
161 "%s unknown error (memory/cpu corruption?)", loglabel
);
167 spam(uschar
**listptr
)
170 uschar
*list
= *listptr
;
172 uschar user_name_buffer
[128];
173 unsigned long mbox_size
;
176 uschar spamd_buffer
[32600];
177 int i
, j
, offset
, result
;
179 uschar spamd_version
[8];
180 uschar spamd_short_result
[8];
181 uschar spamd_score_char
;
182 double spamd_threshold
, spamd_score
, spamd_reject_score
;
183 int spamd_report_offset
;
188 struct sockaddr_un server
;
190 struct pollfd pollfd
;
191 #else /* Patch posted by Erik ? for OS X */
192 struct timeval select_tv
; /* and applied by PH */
195 uschar
*spamd_address_work
;
197 /* stop compiler warning */
200 /* find the username from the option list */
201 if ((user_name
= string_nextinlist(&list
, &sep
,
203 sizeof(user_name_buffer
))) == NULL
)
205 /* no username given, this means no scanning should be done */
209 /* if username is "0" or "false", do not scan */
210 if ( (Ustrcmp(user_name
,"0") == 0) ||
211 (strcmpic(user_name
,US
"false") == 0) )
214 /* if there is an additional option, check if it is "true" */
215 if (strcmpic(list
,US
"true") == 0)
216 /* in that case, always return true later */
219 /* expand spamd_address if needed */
220 if (*spamd_address
== '$')
222 spamd_address_work
= expand_string(spamd_address
);
223 if (spamd_address_work
== NULL
)
225 log_write(0, LOG_MAIN
|LOG_PANIC
,
226 "%s spamd_address starts with $, but expansion failed: %s",
227 loglabel
, expand_string_message
);
232 spamd_address_work
= spamd_address
;
234 HDEBUG(D_acl
) debug_printf("spamd: addrlist '%s'\n", spamd_address_work
);
236 /* check if previous spamd_address was expanded and has changed. dump cached results if so */
238 && prev_spamd_address_work
!= NULL
239 && Ustrcmp(prev_spamd_address_work
, spamd_address_work
) != 0
243 /* if we scanned for this username last time, just return */
244 if (spam_ok
&& Ustrcmp(prev_user_name
, user_name
) == 0)
245 return override
? OK
: spam_rc
;
247 /* make sure the eml mbox file is spooled up */
248 mbox_file
= spool_mbox(&mbox_size
, NULL
);
250 if (mbox_file
== NULL
)
252 /* error while spooling */
253 log_write(0, LOG_MAIN
|LOG_PANIC
,
254 "%s error while creating mbox spool file", loglabel
);
264 uschar
*spamd_address_list_ptr
= spamd_address_work
;
265 spamd_address_container
* spamd_address_vector
[32];
266 spamd_address_container
* sd
;
268 /* Check how many spamd servers we have
269 and register their addresses */
270 while ((address
= string_nextinlist(&spamd_address_list_ptr
, &sep
,
274 int sublist_sep
= -(int)' '; /* default space-sep */
278 HDEBUG(D_acl
) debug_printf("spamd: addr entry '%s'\n", address
);
279 sd
= (spamd_address_container
*)store_get(sizeof(spamd_address_container
));
281 for (sublist
= address
, args
= 0, spamd_param_init(sd
);
282 s
= string_nextinlist(&sublist
, &sublist_sep
, NULL
, 0);
286 HDEBUG(D_acl
) debug_printf("spamd: addr parm '%s'\n", s
);
289 case 0: sd
->hostspec
= s
;
290 if (*s
== '/') args
++; /* local; no port */
292 case 1: sd
->hostspec
= string_sprintf("%s %s", sd
->hostspec
, s
);
294 default: spamd_param(s
, sd
);
300 log_write(0, LOG_MAIN
,
301 "%s warning - invalid spamd address: '%s'", loglabel
, address
);
305 spamd_address_vector
[num_servers
] = sd
;
306 if (++num_servers
> 31)
310 /* check if we have at least one server */
313 log_write(0, LOG_MAIN
|LOG_PANIC
,
314 "%s no useable spamd server addresses in spamd_address configuration option.",
323 current_server
= spamd_get_server(spamd_address_vector
, num_servers
);
324 sd
= spamd_address_vector
[current_server
];
326 debug_printf("trying server %s\n", sd
->hostspec
);
328 /* contact a spamd */
329 if ((spamd_sock
= ip_streamsocket(sd
->hostspec
, &errstr
, 5)) >= 0)
332 log_write(0, LOG_MAIN
, "%s spamd: %s", loglabel
, errstr
);
333 sd
->is_failed
= TRUE
;
335 current_server
= spamd_get_server(spamd_address_vector
, num_servers
);
336 if (current_server
< 0)
338 log_write(0, LOG_MAIN
|LOG_PANIC
, "%s all spamd servers failed",
343 is_rspamd
= sd
->is_rspamd
;
346 if (spamd_sock
== -1)
348 log_write(0, LOG_MAIN
|LOG_PANIC
,
349 "programming fault, spamd_sock unexpectedly unset");
353 (void)fcntl(spamd_sock
, F_SETFL
, O_NONBLOCK
);
354 /* now we are connected to spamd on spamd_sock */
356 { /* rspamd variant */
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);
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",
382 /* send our request */
383 wrote
= send(spamd_sock
, spamd_buffer
, Ustrlen(spamd_buffer
), 0);
387 (void)close(spamd_sock
);
388 log_write(0, LOG_MAIN
|LOG_PANIC
,
389 "%s spamd send failed: %s", loglabel
, strerror(errno
));
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).
404 pollfd
.fd
= spamd_sock
;
405 pollfd
.events
= POLLOUT
;
407 (void)fcntl(spamd_sock
, F_SETFL
, O_NONBLOCK
);
410 read
= fread(spamd_buffer
,1,sizeof(spamd_buffer
),mbox_file
);
416 result
= poll(&pollfd
, 1, 1000);
418 /* Patch posted by Erik ? for OS X and applied by PH */
420 select_tv
.tv_sec
= 1;
421 select_tv
.tv_usec
= 0;
423 FD_SET(spamd_sock
, &select_fd
);
424 result
= select(spamd_sock
+1, NULL
, &select_fd
, NULL
, &select_tv
);
426 /* End Erik's patch */
428 if (result
== -1 && errno
== EINTR
)
433 log_write(0, LOG_MAIN
|LOG_PANIC
,
434 "%s %s on spamd socket", loglabel
, strerror(errno
));
437 if (time(NULL
) - start
< SPAMD_TIMEOUT
)
439 log_write(0, LOG_MAIN
|LOG_PANIC
,
440 "%s timed out writing spamd socket", loglabel
);
442 (void)close(spamd_sock
);
446 wrote
= send(spamd_sock
,spamd_buffer
+ offset
,read
- offset
,0);
449 log_write(0, LOG_MAIN
|LOG_PANIC
,
450 "%s %s on spamd socket", loglabel
, strerror(errno
));
451 (void)close(spamd_sock
);
454 if (offset
+ wrote
!= read
)
461 while (!feof(mbox_file
) && !ferror(mbox_file
));
463 if (ferror(mbox_file
))
465 log_write(0, LOG_MAIN
|LOG_PANIC
,
466 "%s error reading spool file: %s", loglabel
, strerror(errno
));
467 (void)close(spamd_sock
);
471 (void)fclose(mbox_file
);
473 /* we're done sending, close socket for writing */
474 shutdown(spamd_sock
,SHUT_WR
);
476 /* read spamd response using what's left of the timeout. */
477 memset(spamd_buffer
, 0, sizeof(spamd_buffer
));
479 while ((i
= ip_recv(spamd_sock
,
480 spamd_buffer
+ offset
,
481 sizeof(spamd_buffer
) - offset
- 1,
482 SPAMD_TIMEOUT
- time(NULL
) + start
)) > 0 )
486 if (i
<= 0 && errno
!= 0)
488 log_write(0, LOG_MAIN
|LOG_PANIC
,
489 "%s error reading from spamd socket: %s", loglabel
, strerror(errno
));
490 (void)close(spamd_sock
);
495 (void)close(spamd_sock
);
498 { /* rspamd variant of reply */
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)
505 log_write(0, LOG_MAIN
|LOG_PANIC
,
506 "%s cannot parse spamd output: %d", loglabel
, r
);
509 /* now parse action */
510 p
= &spamd_buffer
[spamd_report_offset
];
512 if (Ustrncmp(p
, "Action: ", sizeof("Action: ") - 1) == 0)
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)
523 /* dig in the spamd output and put the report in a multiline header,
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)
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)
534 log_write(0, LOG_MAIN
|LOG_PANIC
,
535 "%s cannot parse spamd output", loglabel
);
540 Ustrcpy(spam_action_buffer
,
541 spamd_score
>= spamd_threshold
? "reject" : "no action");
544 /* Create report. Since this is a multiline string,
545 we must hack it into shape first */
546 p
= &spamd_buffer
[spamd_report_offset
];
547 q
= spam_report_buffer
;
559 /* add an extra space after the newline to ensure
560 that it is treated as a header continuation line */
566 /* cut off trailing leftovers */
570 spam_report
= spam_report_buffer
;
571 spam_action
= spam_action_buffer
;
573 /* create spam bar */
574 spamd_score_char
= spamd_score
> 0 ? '+' : '-';
575 j
= abs((int)(spamd_score
));
578 while ((i
< j
) && (i
<= MAX_SPAM_BAR_CHARS
))
579 spam_bar_buffer
[i
++] = spamd_score_char
;
582 spam_bar_buffer
[0] = '/';
585 spam_bar_buffer
[i
] = '\0';
586 spam_bar
= spam_bar_buffer
;
588 /* create "float" spam score */
589 (void)string_format(spam_score_buffer
, sizeof(spam_score_buffer
),
590 "%.1f", spamd_score
);
591 spam_score
= spam_score_buffer
;
593 /* create "int" spam score */
594 j
= (int)((spamd_score
+ 0.001)*10);
595 (void)string_format(spam_score_int_buffer
, sizeof(spam_score_int_buffer
),
597 spam_score_int
= spam_score_int_buffer
;
599 /* compare threshold against score */
600 spam_rc
= spamd_score
>= spamd_threshold
601 ? OK
/* spam as determined by user's threshold */
602 : FAIL
; /* not spam */
604 /* remember expanded spamd_address if needed */
605 if (spamd_address_work
!= spamd_address
)
606 prev_spamd_address_work
= string_copy(spamd_address_work
);
608 /* remember user name and "been here" for it */
609 Ustrcpy(prev_user_name
, user_name
);
613 ? OK
/* always return OK, no matter what the score */
617 (void)fclose(mbox_file
);