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