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