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 int timeout_sec
;
25 static const uschar
* loglabel
= US
"spam acl condition:";
29 spamd_param_init(spamd_address_container
*spamd
)
31 /* default spamd server weight, time and backup value */
32 spamd
->weight
= SPAMD_WEIGHT
;
33 spamd
->is_failed
= FALSE
;
34 spamd
->is_backup
= FALSE
;
40 spamd_param(const uschar
*param
, spamd_address_container
*spamd
)
42 static int timesinceday
= -1;
45 /* check backup parameter */
46 if (Ustrcmp(param
, "backup") == 0)
48 spamd
->is_backup
= TRUE
;
52 /*XXX more clever parsing could discard embedded spaces? */
54 /* check weight parameter */
55 if (sscanf(param
, "weight=%u", &spamd
->weight
))
57 if (spamd
->weight
== 0) /* this server disabled: skip it */
62 /* check time parameter */
63 if (Ustrncmp(param
, "time=", 5) == 0)
65 unsigned int start_h
= 0, start_m
= 0, start_s
= 0;
66 unsigned int end_h
= 24, end_m
= 0, end_s
= 0;
67 unsigned int time_start
, time_end
;
68 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
78 log_write(0, LOG_MAIN
,
79 "%s warning - invalid spamd time value: '%s'", loglabel
, s
);
80 return -1; /* syntax error */
85 log_write(0, LOG_MAIN
,
86 "%s warning - invalid spamd time value: '%s'", loglabel
, s
);
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 if (Ustrncmp(param
, "tmo=", 4) == 0)
114 int sec
= readconf_readtime((s
= param
+4), '\0', FALSE
);
117 log_write(0, LOG_MAIN
,
118 "%s warning - invalid spamd timeout value: '%s'", loglabel
, s
);
119 return -1; /* syntax error */
125 log_write(0, LOG_MAIN
, "%s warning - invalid spamd parameter: '%s'",
127 return -1; /* syntax error */
132 spamd_get_server(spamd_address_container
**spamds
, int num_servers
)
135 long rnd
, weights
= 0;
136 static BOOL srandomed
= 0;
137 BOOL usebackup
= 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 /* get sum of all weights */
155 for (i
= 0; i
< num_servers
; i
++)
156 if (!spamds
[i
]->is_failed
&& spamds
[i
]->is_backup
== usebackup
)
157 weights
+= spamds
[i
]->weight
;
161 if (usebackup
) /* all servers failed (backups too) */
166 rnd
= random() % weights
;
168 for (i
= 0; i
< num_servers
; i
++)
169 if (!spamds
[i
]->is_failed
&& spamds
[i
]->is_backup
== usebackup
)
170 if ((rnd
-= spamds
[i
]->weight
) < 0)
173 log_write(0, LOG_MAIN
|LOG_PANIC
,
174 "%s unknown error (memory/cpu corruption?)", loglabel
);
180 spam(const uschar
**listptr
)
183 const uschar
*list
= *listptr
;
185 uschar user_name_buffer
[128];
186 unsigned long mbox_size
;
189 uschar spamd_buffer
[32600];
190 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
;
201 struct sockaddr_un server
;
203 struct pollfd pollfd
;
204 #else /* Patch posted by Erik ? for OS X */
205 struct timeval select_tv
; /* and applied by PH */
208 uschar
*spamd_address_work
;
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 timeout_sec
= SPAMD_TIMEOUT
;
229 /* if there is an additional option, check if it is "true" */
230 if (strcmpic(list
,US
"true") == 0)
231 /* in that case, always return true later */
234 /* expand spamd_address if needed */
235 if (*spamd_address
== '$')
237 spamd_address_work
= expand_string(spamd_address
);
238 if (spamd_address_work
== NULL
)
240 log_write(0, LOG_MAIN
|LOG_PANIC
,
241 "%s spamd_address starts with $, but expansion failed: %s",
242 loglabel
, expand_string_message
);
247 spamd_address_work
= spamd_address
;
249 HDEBUG(D_acl
) debug_printf("spamd: addrlist '%s'\n", spamd_address_work
);
251 /* check if previous spamd_address was expanded and has changed. dump cached results if so */
253 && prev_spamd_address_work
!= NULL
254 && Ustrcmp(prev_spamd_address_work
, spamd_address_work
) != 0
258 /* if we scanned for this username last time, just return */
259 if (spam_ok
&& Ustrcmp(prev_user_name
, user_name
) == 0)
260 return override
? OK
: spam_rc
;
262 /* make sure the eml mbox file is spooled up */
263 mbox_file
= spool_mbox(&mbox_size
, NULL
);
265 if (mbox_file
== NULL
)
267 /* error while spooling */
268 log_write(0, LOG_MAIN
|LOG_PANIC
,
269 "%s error while creating mbox spool file", loglabel
);
279 const uschar
*spamd_address_list_ptr
= spamd_address_work
;
280 spamd_address_container
* spamd_address_vector
[32];
281 spamd_address_container
* sd
;
283 /* Check how many spamd servers we have
284 and register their addresses */
285 while ((address
= string_nextinlist(&spamd_address_list_ptr
, &sep
,
288 const uschar
* sublist
;
289 int sublist_sep
= -(int)' '; /* default space-sep */
293 HDEBUG(D_acl
) debug_printf("spamd: addr entry '%s'\n", address
);
294 sd
= (spamd_address_container
*)store_get(sizeof(spamd_address_container
));
296 for (sublist
= address
, args
= 0, spamd_param_init(sd
);
297 s
= string_nextinlist(&sublist
, &sublist_sep
, NULL
, 0);
301 HDEBUG(D_acl
) debug_printf("spamd: addr parm '%s'\n", s
);
304 case 0: sd
->hostspec
= s
;
305 if (*s
== '/') args
++; /* local; no port */
307 case 1: sd
->hostspec
= string_sprintf("%s %s", sd
->hostspec
, s
);
309 default: spamd_param(s
, sd
);
315 log_write(0, LOG_MAIN
,
316 "%s warning - invalid spamd address: '%s'", loglabel
, address
);
320 spamd_address_vector
[num_servers
] = sd
;
321 if (++num_servers
> 31)
325 /* check if we have at least one server */
328 log_write(0, LOG_MAIN
|LOG_PANIC
,
329 "%s no useable spamd server addresses in spamd_address configuration option.",
338 current_server
= spamd_get_server(spamd_address_vector
, num_servers
);
339 sd
= spamd_address_vector
[current_server
];
341 debug_printf("trying server %s\n", sd
->hostspec
);
343 /* contact a spamd */
344 if ((spamd_sock
= ip_streamsocket(sd
->hostspec
, &errstr
, 5)) >= 0)
347 log_write(0, LOG_MAIN
, "%s spamd: %s", loglabel
, errstr
);
348 sd
->is_failed
= TRUE
;
350 current_server
= spamd_get_server(spamd_address_vector
, num_servers
);
351 if (current_server
< 0)
353 log_write(0, LOG_MAIN
|LOG_PANIC
, "%s all spamd servers failed",
358 is_rspamd
= sd
->is_rspamd
;
361 if (spamd_sock
== -1)
363 log_write(0, LOG_MAIN
|LOG_PANIC
,
364 "programming fault, spamd_sock unexpectedly unset");
368 (void)fcntl(spamd_sock
, F_SETFL
, O_NONBLOCK
);
369 /* now we are connected to spamd on spamd_sock */
371 { /* rspamd variant */
376 req_str
= string_sprintf("CHECK RSPAMC/1.3\r\nContent-length: %lu\r\n"
377 "Queue-Id: %s\r\nFrom: <%s>\r\nRecipient-Number: %d\r\n", mbox_size
,
378 message_id
, sender_address
, recipients_count
);
379 for (i
= 0; i
< recipients_count
; i
++)
380 req_str
= string_sprintf("%sRcpt: <%s>\r\n", req_str
, recipients_list
[i
].address
);
381 if ((helo
= expand_string(US
"$sender_helo_name")) != NULL
&& *helo
!= '\0')
382 req_str
= string_sprintf("%sHelo: %s\r\n", req_str
, helo
);
383 if ((fcrdns
= expand_string(US
"$sender_host_name")) != NULL
&& *fcrdns
!= '\0')
384 req_str
= string_sprintf("%sHostname: %s\r\n", req_str
, fcrdns
);
385 if (sender_host_address
!= NULL
)
386 req_str
= string_sprintf("%sIP: %s\r\n", req_str
, sender_host_address
);
387 req_str
= string_sprintf("%s\r\n", req_str
);
388 wrote
= send(spamd_sock
, req_str
, Ustrlen(req_str
), 0);
391 { /* spamassassin variant */
392 (void)string_format(spamd_buffer
,
393 sizeof(spamd_buffer
),
394 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
397 /* send our request */
398 wrote
= send(spamd_sock
, spamd_buffer
, Ustrlen(spamd_buffer
), 0);
402 (void)close(spamd_sock
);
403 log_write(0, LOG_MAIN
|LOG_PANIC
,
404 "%s spamd send failed: %s", loglabel
, strerror(errno
));
408 /* now send the file */
409 /* spamd sometimes accepts conections but doesn't read data off
410 * the connection. We make the file descriptor non-blocking so
411 * that the write will only write sufficient data without blocking
412 * and we poll the desciptor to make sure that we can write without
413 * blocking. Short writes are gracefully handled and if the whole
414 * trasaction takes too long it is aborted.
415 * Note: poll() is not supported in OSX 10.2 and is reported to be
416 * broken in more recent versions (up to 10.4).
419 pollfd
.fd
= spamd_sock
;
420 pollfd
.events
= POLLOUT
;
422 (void)fcntl(spamd_sock
, F_SETFL
, O_NONBLOCK
);
425 read
= fread(spamd_buffer
,1,sizeof(spamd_buffer
),mbox_file
);
431 result
= poll(&pollfd
, 1, 1000);
433 /* Patch posted by Erik ? for OS X and applied by PH */
435 select_tv
.tv_sec
= 1;
436 select_tv
.tv_usec
= 0;
438 FD_SET(spamd_sock
, &select_fd
);
439 result
= select(spamd_sock
+1, NULL
, &select_fd
, NULL
, &select_tv
);
441 /* End Erik's patch */
443 if (result
== -1 && errno
== EINTR
)
448 log_write(0, LOG_MAIN
|LOG_PANIC
,
449 "%s %s on spamd socket", loglabel
, strerror(errno
));
452 if (time(NULL
) - start
< timeout_sec
)
454 log_write(0, LOG_MAIN
|LOG_PANIC
,
455 "%s timed out writing spamd socket", loglabel
);
457 (void)close(spamd_sock
);
461 wrote
= send(spamd_sock
,spamd_buffer
+ offset
,read
- offset
,0);
464 log_write(0, LOG_MAIN
|LOG_PANIC
,
465 "%s %s on spamd socket", loglabel
, strerror(errno
));
466 (void)close(spamd_sock
);
469 if (offset
+ wrote
!= read
)
476 while (!feof(mbox_file
) && !ferror(mbox_file
));
478 if (ferror(mbox_file
))
480 log_write(0, LOG_MAIN
|LOG_PANIC
,
481 "%s error reading spool file: %s", loglabel
, strerror(errno
));
482 (void)close(spamd_sock
);
486 (void)fclose(mbox_file
);
488 /* we're done sending, close socket for writing */
489 shutdown(spamd_sock
,SHUT_WR
);
491 /* read spamd response using what's left of the timeout. */
492 memset(spamd_buffer
, 0, sizeof(spamd_buffer
));
494 while ((i
= ip_recv(spamd_sock
,
495 spamd_buffer
+ offset
,
496 sizeof(spamd_buffer
) - offset
- 1,
497 timeout_sec
- time(NULL
) + start
)) > 0 )
501 if (i
<= 0 && errno
!= 0)
503 log_write(0, LOG_MAIN
|LOG_PANIC
,
504 "%s error reading from spamd socket: %s", loglabel
, strerror(errno
));
505 (void)close(spamd_sock
);
510 (void)close(spamd_sock
);
513 { /* rspamd variant of reply */
515 if ((r
= sscanf(CS spamd_buffer
,
516 "RSPAMD/%7s 0 EX_OK\r\nMetric: default; %7s %lf / %lf / %lf\r\n%n",
517 spamd_version
, spamd_short_result
, &spamd_score
, &spamd_threshold
,
518 &spamd_reject_score
, &spamd_report_offset
)) != 5)
520 log_write(0, LOG_MAIN
|LOG_PANIC
,
521 "%s cannot parse spamd output: %d", loglabel
, r
);
524 /* now parse action */
525 p
= &spamd_buffer
[spamd_report_offset
];
527 if (Ustrncmp(p
, "Action: ", sizeof("Action: ") - 1) == 0)
529 p
+= sizeof("Action: ") - 1;
530 q
= &spam_action_buffer
[0];
531 while (*p
&& *p
!= '\r' && (q
- spam_action_buffer
) < sizeof(spam_action_buffer
) - 1)
538 /* dig in the spamd output and put the report in a multiline header,
540 if (sscanf(CS spamd_buffer
,
541 "SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
542 spamd_version
,&spamd_score
,&spamd_threshold
,&spamd_report_offset
) != 3)
544 /* try to fall back to pre-2.50 spamd output */
545 if (sscanf(CS spamd_buffer
,
546 "SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
547 spamd_version
,&spamd_score
,&spamd_threshold
,&spamd_report_offset
) != 3)
549 log_write(0, LOG_MAIN
|LOG_PANIC
,
550 "%s cannot parse spamd output", loglabel
);
555 Ustrcpy(spam_action_buffer
,
556 spamd_score
>= spamd_threshold
? "reject" : "no action");
559 /* Create report. Since this is a multiline string,
560 we must hack it into shape first */
561 p
= &spamd_buffer
[spamd_report_offset
];
562 q
= spam_report_buffer
;
574 /* add an extra space after the newline to ensure
575 that it is treated as a header continuation line */
581 /* cut off trailing leftovers */
585 spam_report
= spam_report_buffer
;
586 spam_action
= spam_action_buffer
;
588 /* create spam bar */
589 spamd_score_char
= spamd_score
> 0 ? '+' : '-';
590 j
= abs((int)(spamd_score
));
593 while ((i
< j
) && (i
<= MAX_SPAM_BAR_CHARS
))
594 spam_bar_buffer
[i
++] = spamd_score_char
;
597 spam_bar_buffer
[0] = '/';
600 spam_bar_buffer
[i
] = '\0';
601 spam_bar
= spam_bar_buffer
;
603 /* create "float" spam score */
604 (void)string_format(spam_score_buffer
, sizeof(spam_score_buffer
),
605 "%.1f", spamd_score
);
606 spam_score
= spam_score_buffer
;
608 /* create "int" spam score */
609 j
= (int)((spamd_score
+ 0.001)*10);
610 (void)string_format(spam_score_int_buffer
, sizeof(spam_score_int_buffer
),
612 spam_score_int
= spam_score_int_buffer
;
614 /* compare threshold against score */
615 spam_rc
= spamd_score
>= spamd_threshold
616 ? OK
/* spam as determined by user's threshold */
617 : FAIL
; /* not spam */
619 /* remember expanded spamd_address if needed */
620 if (spamd_address_work
!= spamd_address
)
621 prev_spamd_address_work
= string_copy(spamd_address_work
);
623 /* remember user name and "been here" for it */
624 Ustrcpy(prev_user_name
, user_name
);
628 ? OK
/* always return OK, no matter what the score */
632 (void)fclose(mbox_file
);