Commit | Line | Data |
---|---|---|
8523533c TK |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
5 | /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */ | |
6 | /* License: GPL */ | |
7 | ||
8 | /* Code for calling spamassassin's spamd. Called from acl.c. */ | |
9 | ||
10 | #include "exim.h" | |
11 | #ifdef WITH_CONTENT_SCAN | |
12 | #include "spam.h" | |
13 | ||
14 | uschar spam_score_buffer[16]; | |
15 | uschar spam_score_int_buffer[16]; | |
16 | uschar spam_bar_buffer[128]; | |
c5f280e2 | 17 | uschar spam_action_buffer[32]; |
8523533c TK |
18 | uschar spam_report_buffer[32600]; |
19 | uschar prev_user_name[128] = ""; | |
20 | int spam_ok = 0; | |
21 | int spam_rc = 0; | |
f7274286 | 22 | uschar *prev_spamd_address_work = NULL; |
8523533c | 23 | |
fd4d8871 | 24 | static const uschar * loglabel = US"spam acl condition:"; |
8523533c | 25 | |
fd4d8871 R |
26 | static int |
27 | spamd_param_init(spamd_address_container *spamd) | |
28 | { | |
29 | /* default spamd server weight, time and backup value */ | |
30 | spamd->weight = SPAMD_WEIGHT; | |
31 | spamd->is_failed = FALSE; | |
32 | spamd->is_backup = FALSE; | |
33 | return 0; | |
34 | } | |
8523533c | 35 | |
8523533c | 36 | |
fd4d8871 R |
37 | static int |
38 | spamd_param(const uschar *param, spamd_address_container *spamd) | |
39 | { | |
40 | static int timesinceday = -1; | |
41 | uschar buffer[256]; | |
42 | ||
43 | /* check backup parameter */ | |
44 | if (Ustrcmp(param, "backup") == 0) | |
45 | { | |
46 | spamd->is_backup = TRUE; | |
47 | return 0; /* OK */ | |
48 | } | |
49 | ||
50 | /*XXX more clever parsing could discard embedded spaces? */ | |
51 | ||
52 | /* check weight parameter */ | |
53 | if (sscanf(param, "weight=%u", &spamd->weight)) | |
54 | { | |
55 | if (spamd->weight == 0) /* this server disabled: skip it */ | |
56 | return 1; | |
57 | return 0; /* OK */ | |
58 | } | |
59 | ||
60 | /* check time parameter */ | |
61 | if (sscanf(param, "time=%s", buffer)) | |
62 | { | |
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; | |
66 | uschar *start_string; | |
67 | uschar *end_string; | |
68 | uschar *delimiter; | |
69 | ||
70 | if ((delimiter = US strchr(CS buffer, '-'))) | |
ddcf2b5f | 71 | { |
fd4d8871 R |
72 | *delimiter = '\0'; |
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) | |
ddcf2b5f | 77 | { |
fd4d8871 R |
78 | log_write(0, LOG_MAIN, |
79 | "%s warning - invalid spamd time value: '%s'", loglabel, buffer); | |
80 | return -1; /* syntax error */ | |
ddcf2b5f | 81 | } |
f7274286 | 82 | } |
f7274286 | 83 | else |
ddcf2b5f | 84 | { |
fd4d8871 R |
85 | log_write(0, LOG_MAIN, |
86 | "%s warning - invalid spamd time value: '%s'", loglabel, buffer); | |
87 | return -1; /* syntax error */ | |
ddcf2b5f | 88 | } |
8523533c | 89 | |
fd4d8871 | 90 | if (timesinceday < 0) |
ddcf2b5f | 91 | { |
fd4d8871 R |
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; | |
95 | } | |
8e669ac1 | 96 | |
fd4d8871 R |
97 | time_start = start_h*3600 + start_m*60 + start_s; |
98 | time_end = end_h*3600 + end_m*60 + end_s; | |
8e669ac1 | 99 | |
fd4d8871 R |
100 | if (timesinceday < time_start || timesinceday >= time_end) |
101 | return 1; /* skip spamd server */ | |
c5f280e2 | 102 | |
fd4d8871 R |
103 | return 0; /* OK */ |
104 | } | |
8e669ac1 | 105 | |
fd4d8871 R |
106 | if (Ustrcmp(param, "variant=rspamd") == 0) |
107 | { | |
108 | spamd->is_rspamd = TRUE; | |
109 | return 0; | |
110 | } | |
8e669ac1 | 111 | |
fd4d8871 R |
112 | log_write(0, LOG_MAIN, "%s warning - invalid spamd parameter: '%s'", |
113 | loglabel, param); | |
114 | return -1; /* syntax error */ | |
115 | } | |
8523533c | 116 | |
8523533c | 117 | |
fd4d8871 R |
118 | static int |
119 | spamd_get_server(spamd_address_container **spamds, int num_servers) | |
120 | { | |
121 | unsigned int i; | |
122 | long rnd, weights = 0; | |
123 | static BOOL srandomed = 0; | |
124 | BOOL usebackup = FALSE; | |
125 | ||
126 | for (;;) | |
127 | { | |
128 | /* seedup, if we have only 1 server */ | |
129 | if (num_servers == 1) | |
130 | return (spamds[0]->is_failed ? -1 : 0); | |
131 | ||
132 | /* init ranmod */ | |
133 | if (!srandomed) | |
134 | { | |
135 | struct timeval tv; | |
136 | gettimeofday(&tv, NULL); | |
137 | srandom((unsigned int)(tv.tv_usec/1000)); | |
138 | srandomed = TRUE; | |
139 | } | |
8e669ac1 | 140 | |
fd4d8871 R |
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; | |
8e669ac1 | 145 | |
fd4d8871 R |
146 | if (weights != 0) |
147 | break; | |
148 | if (usebackup) /* all servers failed (backups too) */ | |
149 | return -1; | |
150 | usebackup = TRUE; | |
151 | } | |
8e669ac1 | 152 | |
fd4d8871 | 153 | rnd = random() % weights; |
8e669ac1 | 154 | |
fd4d8871 R |
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) | |
158 | return i; | |
29cfeb94 | 159 | |
fd4d8871 R |
160 | log_write(0, LOG_MAIN|LOG_PANIC, |
161 | "%s unknown error (memory/cpu corruption?)", loglabel); | |
162 | return -1; | |
163 | } | |
29cfeb94 | 164 | |
29cfeb94 | 165 | |
fd4d8871 R |
166 | int |
167 | spam(uschar **listptr) | |
168 | { | |
169 | int sep = 0; | |
170 | uschar *list = *listptr; | |
171 | uschar *user_name; | |
172 | uschar user_name_buffer[128]; | |
173 | unsigned long mbox_size; | |
174 | FILE *mbox_file; | |
175 | int spamd_sock = -1; | |
176 | uschar spamd_buffer[32600]; | |
177 | int i, j, offset, result; | |
178 | BOOL is_rspamd; | |
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; | |
184 | uschar *p,*q; | |
185 | int override = 0; | |
186 | time_t start; | |
187 | size_t read, wrote; | |
188 | struct sockaddr_un server; | |
189 | #ifndef NO_POLL_H | |
190 | struct pollfd pollfd; | |
191 | #else /* Patch posted by Erik ? for OS X */ | |
192 | struct timeval select_tv; /* and applied by PH */ | |
193 | fd_set select_fd; | |
194 | #endif | |
195 | uschar *spamd_address_work; | |
196 | ||
197 | /* stop compiler warning */ | |
198 | result = 0; | |
199 | ||
200 | /* find the username from the option list */ | |
201 | if ((user_name = string_nextinlist(&list, &sep, | |
202 | user_name_buffer, | |
203 | sizeof(user_name_buffer))) == NULL) | |
204 | { | |
205 | /* no username given, this means no scanning should be done */ | |
206 | return FAIL; | |
207 | } | |
208 | ||
209 | /* if username is "0" or "false", do not scan */ | |
210 | if ( (Ustrcmp(user_name,"0") == 0) || | |
211 | (strcmpic(user_name,US"false") == 0) ) | |
212 | return FAIL; | |
213 | ||
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 */ | |
217 | override = 1; | |
218 | ||
219 | /* expand spamd_address if needed */ | |
220 | if (*spamd_address == '$') | |
221 | { | |
222 | spamd_address_work = expand_string(spamd_address); | |
223 | if (spamd_address_work == NULL) | |
224 | { | |
225 | log_write(0, LOG_MAIN|LOG_PANIC, | |
226 | "%s spamd_address starts with $, but expansion failed: %s", | |
227 | loglabel, expand_string_message); | |
228 | return DEFER; | |
29cfeb94 | 229 | } |
fd4d8871 R |
230 | } |
231 | else | |
232 | spamd_address_work = spamd_address; | |
233 | ||
234 | HDEBUG(D_acl) debug_printf("spamd: addrlist '%s'\n", spamd_address_work); | |
235 | ||
236 | /* check if previous spamd_address was expanded and has changed. dump cached results if so */ | |
237 | if ( spam_ok | |
238 | && prev_spamd_address_work != NULL | |
239 | && Ustrcmp(prev_spamd_address_work, spamd_address_work) != 0 | |
240 | ) | |
241 | spam_ok = 0; | |
242 | ||
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; | |
246 | ||
247 | /* make sure the eml mbox file is spooled up */ | |
248 | mbox_file = spool_mbox(&mbox_size, NULL); | |
249 | ||
250 | if (mbox_file == NULL) | |
251 | { | |
252 | /* error while spooling */ | |
253 | log_write(0, LOG_MAIN|LOG_PANIC, | |
254 | "%s error while creating mbox spool file", loglabel); | |
255 | return DEFER; | |
256 | } | |
257 | ||
258 | start = time(NULL); | |
259 | ||
260 | { | |
261 | int num_servers = 0; | |
262 | int current_server; | |
263 | uschar *address; | |
264 | uschar *spamd_address_list_ptr = spamd_address_work; | |
265 | spamd_address_container * spamd_address_vector[32]; | |
266 | ||
267 | ||
268 | /* Check how many spamd servers we have | |
269 | and register their addresses */ | |
270 | while ((address = string_nextinlist(&spamd_address_list_ptr, &sep, | |
271 | NULL, 0)) != NULL) | |
ddcf2b5f | 272 | { |
fd4d8871 R |
273 | uschar * sublist; |
274 | int sublist_sep = -(int)' '; /* default space-sep */ | |
275 | unsigned args; | |
276 | uschar * s; | |
277 | spamd_address_container * this_spamd = | |
278 | (spamd_address_container *)store_get(sizeof(spamd_address_container)); | |
279 | ||
280 | HDEBUG(D_acl) debug_printf("spamd: addr entry '%s'\n", address); | |
281 | ||
282 | for (sublist = address, args = 0, spamd_param_init(this_spamd); | |
283 | s = string_nextinlist(&sublist, &sublist_sep, NULL, 0); | |
284 | args++ | |
285 | ) | |
ddcf2b5f | 286 | { |
fd4d8871 R |
287 | HDEBUG(D_acl) debug_printf("spamd: addr parm '%s'\n", s); |
288 | switch (args) | |
289 | { | |
290 | case 0: this_spamd->hostname = s; | |
291 | if (*s == '/') args++; /* local; no port */ | |
292 | break; | |
293 | case 1: this_spamd->tcp_port = atoi(s); break; | |
294 | default: spamd_param(s, this_spamd); break; | |
295 | } | |
ddcf2b5f | 296 | } |
fd4d8871 | 297 | if (args < 2) |
c5f280e2 | 298 | { |
fd4d8871 R |
299 | log_write(0, LOG_MAIN, |
300 | "%s warning - invalid spamd address: '%s'", loglabel, address); | |
301 | continue; | |
c5f280e2 | 302 | } |
8523533c | 303 | |
fd4d8871 R |
304 | spamd_address_vector[num_servers] = this_spamd; |
305 | if (++num_servers > 31) | |
306 | break; | |
8523533c TK |
307 | } |
308 | ||
fd4d8871 R |
309 | /* check if we have at least one server */ |
310 | if (!num_servers) | |
ddcf2b5f | 311 | { |
0f501486 | 312 | log_write(0, LOG_MAIN|LOG_PANIC, |
fd4d8871 R |
313 | "%s no useable spamd server addresses in spamd_address configuration option.", |
314 | loglabel); | |
0f501486 | 315 | (void)fclose(mbox_file); |
0f501486 | 316 | return DEFER; |
ddcf2b5f | 317 | } |
0f501486 | 318 | |
fd4d8871 | 319 | while (1) |
ddcf2b5f | 320 | { |
fd4d8871 R |
321 | struct hostent *he; |
322 | int i; | |
323 | spamd_address_container * this_spamd; | |
324 | BOOL is_local; | |
8523533c | 325 | |
fd4d8871 R |
326 | current_server = spamd_get_server(spamd_address_vector, num_servers); |
327 | this_spamd = spamd_address_vector[current_server]; | |
328 | ||
329 | is_local = *this_spamd->hostname == '/'; | |
330 | ||
331 | debug_printf(is_local | |
332 | ? "trying server %s\n" : "trying server %s, port %u\n", | |
333 | this_spamd->hostname, this_spamd->tcp_port); | |
334 | ||
335 | /* contact a spamd */ | |
336 | if (is_local) | |
ddcf2b5f | 337 | { |
fd4d8871 R |
338 | if ((spamd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) |
339 | { | |
340 | log_write(0, LOG_MAIN|LOG_PANIC, | |
341 | "%s spamd: unable to acquire socket (%s)", | |
342 | loglabel, | |
343 | strerror(errno)); | |
344 | (void)fclose(mbox_file); | |
345 | return DEFER; | |
346 | } | |
25257489 | 347 | |
fd4d8871 R |
348 | server.sun_family = AF_UNIX; |
349 | Ustrcpy(server.sun_path, this_spamd->hostname); | |
25257489 | 350 | |
fd4d8871 | 351 | if (connect(spamd_sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) >= 0) |
ddcf2b5f | 352 | { |
fd4d8871 R |
353 | is_rspamd = this_spamd->is_rspamd; |
354 | break; /* connection OK */ | |
ddcf2b5f | 355 | } |
25257489 | 356 | |
fd4d8871 R |
357 | log_write(0, LOG_MAIN|LOG_PANIC, |
358 | "%s spamd: unable to connect to UNIX socket %s (%s)", | |
359 | loglabel, server.sun_path, strerror(errno) ); | |
360 | } | |
361 | else | |
362 | { | |
363 | if ((spamd_sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) | |
ddcf2b5f JH |
364 | { |
365 | log_write(0, LOG_MAIN|LOG_PANIC, | |
fd4d8871 R |
366 | "%s error creating IP socket for spamd", loglabel); |
367 | (void)fclose(mbox_file); | |
368 | return DEFER; | |
ddcf2b5f | 369 | } |
fd4d8871 R |
370 | |
371 | /*XXX should we use getaddrinfo? */ | |
372 | if (!(he = gethostbyname(CS this_spamd->hostname))) | |
373 | log_write(0, LOG_MAIN|LOG_PANIC, | |
374 | "%s failed to lookup host '%s'", loglabel, this_spamd->hostname); | |
375 | ||
376 | else | |
ddcf2b5f | 377 | { |
fd4d8871 R |
378 | struct in_addr in = *(struct in_addr *) he->h_addr_list[0]; |
379 | ||
380 | if (ip_connect(spamd_sock, AF_INET, US inet_ntoa(in), | |
381 | this_spamd->tcp_port, 5) > -1) | |
382 | { | |
383 | is_rspamd = this_spamd->is_rspamd; | |
384 | break; /* connection OK */ | |
385 | } | |
386 | ||
387 | log_write(0, LOG_MAIN|LOG_PANIC, | |
388 | "%s warning - spamd connection to '%s', port %u failed: %s", | |
389 | loglabel, | |
390 | this_spamd->hostname, this_spamd->tcp_port, strerror(errno)); | |
ddcf2b5f | 391 | } |
ddcf2b5f | 392 | |
fd4d8871 R |
393 | (void)close(spamd_sock); |
394 | ||
395 | this_spamd->is_failed = TRUE; | |
396 | current_server = spamd_get_server(spamd_address_vector, num_servers); | |
397 | if (current_server < 0) | |
398 | { | |
399 | log_write(0, LOG_MAIN|LOG_PANIC, "%s all spamd servers failed", | |
400 | loglabel); | |
401 | (void)fclose(mbox_file); | |
402 | return DEFER; | |
403 | } | |
404 | } | |
ddcf2b5f | 405 | } |
fd4d8871 | 406 | } |
8523533c | 407 | |
fd4d8871 R |
408 | if (spamd_sock == -1) |
409 | { | |
410 | log_write(0, LOG_MAIN|LOG_PANIC, | |
411 | "programming fault, spamd_sock unexpectedly unset"); | |
f1e894f3 | 412 | (void)fclose(mbox_file); |
fd4d8871 R |
413 | return DEFER; |
414 | } | |
415 | ||
416 | (void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK); | |
417 | /* now we are connected to spamd on spamd_sock */ | |
418 | if (is_rspamd) | |
419 | { /* rspamd variant */ | |
420 | uschar *req_str; | |
421 | const char *helo; | |
422 | const char *fcrdns; | |
423 | ||
424 | req_str = string_sprintf("CHECK RSPAMC/1.3\r\nContent-length: %lu\r\n" | |
425 | "Queue-Id: %s\r\nFrom: <%s>\r\nRecipient-Number: %d\r\n", mbox_size, | |
426 | message_id, sender_address, recipients_count); | |
427 | for (i = 0; i < recipients_count; i ++) | |
428 | req_str = string_sprintf("%sRcpt: <%s>\r\n", req_str, recipients_list[i].address); | |
429 | if ((helo = expand_string(US"$sender_helo_name")) != NULL && *helo != '\0') | |
430 | req_str = string_sprintf("%sHelo: %s\r\n", req_str, helo); | |
431 | if ((fcrdns = expand_string(US"$sender_host_name")) != NULL && *fcrdns != '\0') | |
432 | req_str = string_sprintf("%sHostname: %s\r\n", req_str, fcrdns); | |
433 | if (sender_host_address != NULL) | |
434 | req_str = string_sprintf("%sIP: %s\r\n", req_str, sender_host_address); | |
435 | req_str = string_sprintf("%s\r\n", req_str); | |
436 | wrote = send(spamd_sock, req_str, Ustrlen(req_str), 0); | |
437 | } | |
438 | else | |
439 | { /* spamassassin variant */ | |
440 | (void)string_format(spamd_buffer, | |
441 | sizeof(spamd_buffer), | |
442 | "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n", | |
443 | user_name, | |
444 | mbox_size); | |
445 | /* send our request */ | |
446 | wrote = send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0); | |
447 | } | |
448 | if (wrote == -1) | |
449 | { | |
450 | (void)close(spamd_sock); | |
451 | log_write(0, LOG_MAIN|LOG_PANIC, | |
452 | "%s spamd send failed: %s", loglabel, strerror(errno)); | |
453 | (void)fclose(mbox_file); | |
454 | return DEFER; | |
455 | } | |
456 | ||
457 | /* now send the file */ | |
458 | /* spamd sometimes accepts conections but doesn't read data off | |
459 | * the connection. We make the file descriptor non-blocking so | |
460 | * that the write will only write sufficient data without blocking | |
461 | * and we poll the desciptor to make sure that we can write without | |
462 | * blocking. Short writes are gracefully handled and if the whole | |
463 | * trasaction takes too long it is aborted. | |
464 | * Note: poll() is not supported in OSX 10.2 and is reported to be | |
465 | * broken in more recent versions (up to 10.4). | |
466 | */ | |
467 | #ifndef NO_POLL_H | |
468 | pollfd.fd = spamd_sock; | |
469 | pollfd.events = POLLOUT; | |
470 | #endif | |
471 | (void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK); | |
472 | do | |
473 | { | |
474 | read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file); | |
475 | if (read > 0) | |
ddcf2b5f | 476 | { |
fd4d8871 R |
477 | offset = 0; |
478 | again: | |
479 | #ifndef NO_POLL_H | |
480 | result = poll(&pollfd, 1, 1000); | |
8523533c | 481 | |
fd4d8871 R |
482 | /* Patch posted by Erik ? for OS X and applied by PH */ |
483 | #else | |
484 | select_tv.tv_sec = 1; | |
485 | select_tv.tv_usec = 0; | |
486 | FD_ZERO(&select_fd); | |
487 | FD_SET(spamd_sock, &select_fd); | |
488 | result = select(spamd_sock+1, NULL, &select_fd, NULL, &select_tv); | |
489 | #endif | |
490 | /* End Erik's patch */ | |
8523533c | 491 | |
fd4d8871 R |
492 | if (result == -1 && errno == EINTR) |
493 | goto again; | |
494 | else if (result < 1) | |
c5f280e2 | 495 | { |
fd4d8871 R |
496 | if (result == -1) |
497 | log_write(0, LOG_MAIN|LOG_PANIC, | |
498 | "%s %s on spamd socket", loglabel, strerror(errno)); | |
499 | else | |
500 | { | |
501 | if (time(NULL) - start < SPAMD_TIMEOUT) | |
502 | goto again; | |
503 | log_write(0, LOG_MAIN|LOG_PANIC, | |
504 | "%s timed out writing spamd socket", loglabel); | |
505 | } | |
506 | (void)close(spamd_sock); | |
507 | (void)fclose(mbox_file); | |
508 | return DEFER; | |
c5f280e2 | 509 | } |
8e669ac1 | 510 | |
fd4d8871 R |
511 | wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0); |
512 | if (wrote == -1) | |
c5f280e2 | 513 | { |
fd4d8871 R |
514 | log_write(0, LOG_MAIN|LOG_PANIC, |
515 | "%s %s on spamd socket", loglabel, strerror(errno)); | |
516 | (void)close(spamd_sock); | |
517 | (void)fclose(mbox_file); | |
518 | return DEFER; | |
c5f280e2 | 519 | } |
fd4d8871 | 520 | if (offset + wrote != read) |
ddcf2b5f | 521 | { |
fd4d8871 R |
522 | offset += wrote; |
523 | goto again; | |
ddcf2b5f JH |
524 | } |
525 | } | |
fd4d8871 R |
526 | } |
527 | while (!feof(mbox_file) && !ferror(mbox_file)); | |
8523533c | 528 | |
fd4d8871 R |
529 | if (ferror(mbox_file)) |
530 | { | |
531 | log_write(0, LOG_MAIN|LOG_PANIC, | |
532 | "%s error reading spool file: %s", loglabel, strerror(errno)); | |
533 | (void)close(spamd_sock); | |
534 | (void)fclose(mbox_file); | |
535 | return DEFER; | |
536 | } | |
537 | ||
538 | (void)fclose(mbox_file); | |
539 | ||
540 | /* we're done sending, close socket for writing */ | |
541 | shutdown(spamd_sock,SHUT_WR); | |
542 | ||
543 | /* read spamd response using what's left of the timeout. */ | |
544 | memset(spamd_buffer, 0, sizeof(spamd_buffer)); | |
545 | offset = 0; | |
546 | while ((i = ip_recv(spamd_sock, | |
547 | spamd_buffer + offset, | |
548 | sizeof(spamd_buffer) - offset - 1, | |
549 | SPAMD_TIMEOUT - time(NULL) + start)) > 0 ) | |
550 | offset += i; | |
551 | ||
552 | /* error handling */ | |
553 | if (i <= 0 && errno != 0) | |
554 | { | |
555 | log_write(0, LOG_MAIN|LOG_PANIC, | |
556 | "%s error reading from spamd socket: %s", loglabel, strerror(errno)); | |
557 | (void)close(spamd_sock); | |
558 | return DEFER; | |
559 | } | |
560 | ||
561 | /* reading done */ | |
562 | (void)close(spamd_sock); | |
563 | ||
564 | if (is_rspamd) | |
565 | { /* rspamd variant of reply */ | |
566 | int r; | |
567 | if ((r = sscanf(CS spamd_buffer, | |
568 | "RSPAMD/%7s 0 EX_OK\r\nMetric: default; %7s %lf / %lf / %lf\r\n%n", | |
569 | spamd_version, spamd_short_result, &spamd_score, &spamd_threshold, | |
570 | &spamd_reject_score, &spamd_report_offset)) != 5) | |
571 | { | |
572 | log_write(0, LOG_MAIN|LOG_PANIC, | |
573 | "%s cannot parse spamd output: %d", loglabel, r); | |
574 | return DEFER; | |
575 | } | |
576 | /* now parse action */ | |
8523533c | 577 | p = &spamd_buffer[spamd_report_offset]; |
fd4d8871 R |
578 | |
579 | if (Ustrncmp(p, "Action: ", sizeof("Action: ") - 1) == 0) | |
ddcf2b5f | 580 | { |
fd4d8871 R |
581 | p += sizeof("Action: ") - 1; |
582 | q = &spam_action_buffer[0]; | |
583 | while (*p && *p != '\r' && (q - spam_action_buffer) < sizeof(spam_action_buffer) - 1) | |
584 | *q++ = *p++; | |
585 | *q = '\0'; | |
ddcf2b5f | 586 | } |
fd4d8871 R |
587 | } |
588 | else | |
589 | { /* spamassassin */ | |
590 | /* dig in the spamd output and put the report in a multiline header, | |
591 | if requested */ | |
592 | if (sscanf(CS spamd_buffer, | |
593 | "SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n", | |
594 | spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3) | |
ddcf2b5f | 595 | { |
fd4d8871 R |
596 | /* try to fall back to pre-2.50 spamd output */ |
597 | if (sscanf(CS spamd_buffer, | |
598 | "SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n", | |
599 | spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3) | |
600 | { | |
601 | log_write(0, LOG_MAIN|LOG_PANIC, | |
602 | "%s cannot parse spamd output", loglabel); | |
603 | return DEFER; | |
604 | } | |
ddcf2b5f | 605 | } |
fd4d8871 R |
606 | |
607 | Ustrcpy(spam_action_buffer, | |
608 | spamd_score >= spamd_threshold ? "reject" : "no action"); | |
609 | } | |
610 | ||
611 | /* Create report. Since this is a multiline string, | |
612 | we must hack it into shape first */ | |
613 | p = &spamd_buffer[spamd_report_offset]; | |
614 | q = spam_report_buffer; | |
615 | while (*p != '\0') | |
616 | { | |
617 | /* skip \r */ | |
618 | if (*p == '\r') | |
619 | { | |
620 | p++; | |
621 | continue; | |
622 | } | |
623 | *q++ = *p; | |
624 | if (*p++ == '\n') | |
625 | { | |
626 | /* add an extra space after the newline to ensure | |
627 | that it is treated as a header continuation line */ | |
628 | *q++ = ' '; | |
629 | } | |
630 | } | |
631 | /* NULL-terminate */ | |
632 | *q-- = '\0'; | |
633 | /* cut off trailing leftovers */ | |
634 | while (*q <= ' ') | |
635 | *q-- = '\0'; | |
636 | ||
637 | spam_report = spam_report_buffer; | |
638 | spam_action = spam_action_buffer; | |
639 | ||
640 | /* create spam bar */ | |
641 | spamd_score_char = spamd_score > 0 ? '+' : '-'; | |
642 | j = abs((int)(spamd_score)); | |
643 | i = 0; | |
644 | if (j != 0) | |
645 | while ((i < j) && (i <= MAX_SPAM_BAR_CHARS)) | |
646 | spam_bar_buffer[i++] = spamd_score_char; | |
647 | else | |
648 | { | |
649 | spam_bar_buffer[0] = '/'; | |
650 | i = 1; | |
651 | } | |
652 | spam_bar_buffer[i] = '\0'; | |
653 | spam_bar = spam_bar_buffer; | |
654 | ||
655 | /* create "float" spam score */ | |
656 | (void)string_format(spam_score_buffer, sizeof(spam_score_buffer), | |
657 | "%.1f", spamd_score); | |
658 | spam_score = spam_score_buffer; | |
659 | ||
660 | /* create "int" spam score */ | |
661 | j = (int)((spamd_score + 0.001)*10); | |
662 | (void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer), | |
663 | "%d", j); | |
664 | spam_score_int = spam_score_int_buffer; | |
665 | ||
666 | /* compare threshold against score */ | |
667 | spam_rc = spamd_score >= spamd_threshold | |
668 | ? OK /* spam as determined by user's threshold */ | |
669 | : FAIL; /* not spam */ | |
670 | ||
671 | /* remember expanded spamd_address if needed */ | |
672 | if (spamd_address_work != spamd_address) | |
673 | prev_spamd_address_work = string_copy(spamd_address_work); | |
674 | ||
675 | /* remember user name and "been here" for it */ | |
676 | Ustrcpy(prev_user_name, user_name); | |
677 | spam_ok = 1; | |
678 | ||
679 | return override | |
680 | ? OK /* always return OK, no matter what the score */ | |
681 | : spam_rc; | |
8523533c TK |
682 | } |
683 | ||
684 | #endif |