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