Revert "Spamd: add missing initialiser. Rspamd mode was incorrectly sometimes seen."
[exim.git] / src / src / spam.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003 - 2015
6  * License: GPL
7  * Copyright (c) The Exim Maintainers 2016 - 2018
8  */
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
16 uschar spam_score_buffer[16];
17 uschar spam_score_int_buffer[16];
18 uschar spam_bar_buffer[128];
19 uschar spam_action_buffer[32];
20 uschar spam_report_buffer[32600];
21 uschar prev_user_name[128] = "";
22 int spam_ok = 0;
23 int spam_rc = 0;
24 uschar *prev_spamd_address_work = NULL;
25
26 static const uschar * loglabel = US"spam acl condition:";
27
28
29 static int
30 spamd_param_init(spamd_address_container *spamd)
31 {
32 /* default spamd server weight, time and priority value */
33 spamd->is_failed = FALSE;
34 spamd->weight = SPAMD_WEIGHT;
35 spamd->timeout = SPAMD_TIMEOUT;
36 spamd->retry = 0;
37 spamd->priority = 1;
38 return 0;
39 }
40
41
42 static int
43 spamd_param(const uschar * param, spamd_address_container * spamd)
44 {
45 static int timesinceday = -1;
46 const uschar * s;
47 const uschar * name;
48
49 /*XXX more clever parsing could discard embedded spaces? */
50
51 if (sscanf(CCS param, "pri=%u", &spamd->priority))
52   return 0; /* OK */
53
54 if (sscanf(CCS param, "weight=%u", &spamd->weight))
55   {
56   if (spamd->weight == 0) /* this server disabled: skip it */
57     return 1;
58   return 0; /* OK */
59   }
60
61 if (Ustrncmp(param, "time=", 5) == 0)
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   const uschar * end_string;
67
68   name = US"time";
69   s = param+5;
70   if ((end_string = Ustrchr(s, '-')))
71     {
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        )
76       goto badval;
77     }
78   else
79     goto badval;
80
81   if (timesinceday < 0)
82     {
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     }
87
88   time_start = start_h*3600 + start_m*60 + start_s;
89   time_end = end_h*3600 + end_m*60 + end_s;
90
91   if (timesinceday < time_start || timesinceday >= time_end)
92     return 1; /* skip spamd server */
93
94   return 0; /* OK */
95   }
96
97 if (Ustrcmp(param, "variant=rspamd") == 0)
98   {
99   spamd->is_rspamd = TRUE;
100   return 0;
101   }
102
103 if (Ustrncmp(param, "tmo=", 4) == 0)
104   {
105   int sec = readconf_readtime((s = param+4), '\0', FALSE);
106   name = US"timeout";
107   if (sec < 0)
108     goto badval;
109   spamd->timeout = sec;
110   return 0;
111   }
112
113 if (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;
120   return 0;
121   }
122
123 log_write(0, LOG_MAIN, "%s warning - invalid spamd parameter: '%s'",
124   loglabel, param);
125 return -1; /* syntax error */
126
127 badval:
128   log_write(0, LOG_MAIN,
129     "%s warning - invalid spamd %s value: '%s'", loglabel, name, s);
130   return -1; /* syntax error */
131 }
132
133
134 static int
135 spamd_get_server(spamd_address_container ** spamds, int num_servers)
136 {
137 unsigned int i;
138 spamd_address_container * sd;
139 long rnd, weights;
140 unsigned pri;
141 static BOOL srandomed = FALSE;
142
143 /* speedup, if we have only 1 server */
144 if (num_servers == 1)
145   return (spamds[0]->is_failed ? -1 : 0);
146
147 /* init ranmod */
148 if (!srandomed)
149   {
150   struct timeval tv;
151   gettimeofday(&tv, NULL);
152   srandom((unsigned int)(tv.tv_usec/1000));
153   srandomed = TRUE;
154   }
155
156 /* scan for highest pri */
157 for (pri = 0, i = 0; i < num_servers; i++)
158   {
159   sd = spamds[i];
160   if (!sd->is_failed && sd->priority > pri) pri = sd->priority;
161   }
162
163 /* get sum of weights */
164 for (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   }
169 if (weights == 0)       /* all servers failed */
170   return -1;
171
172 for (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)
177       return i;
178   }
179
180 log_write(0, LOG_MAIN|LOG_PANIC,
181   "%s unknown error (memory/cpu corruption?)", loglabel);
182 return -1;
183 }
184
185
186 int
187 spam(const uschar **listptr)
188 {
189 int sep = 0;
190 const uschar *list = *listptr;
191 uschar *user_name;
192 uschar user_name_buffer[128];
193 unsigned long mbox_size;
194 FILE *mbox_file;
195 client_conn_ctx spamd_cctx = {.sock = -1};
196 uschar spamd_buffer[32600];
197 int i, j, offset, result;
198 uschar spamd_version[8];
199 uschar spamd_short_result[8];
200 uschar spamd_score_char;
201 double spamd_threshold, spamd_score, spamd_reject_score;
202 int spamd_report_offset;
203 uschar *p,*q;
204 int override = 0;
205 time_t start;
206 size_t read, wrote;
207 #ifndef NO_POLL_H
208 struct pollfd pollfd;
209 #else                               /* Patch posted by Erik ? for OS X */
210 struct timeval select_tv;         /* and applied by PH */
211 fd_set select_fd;
212 #endif
213 uschar *spamd_address_work;
214 spamd_address_container * sd;
215
216 /* stop compiler warning */
217 result = 0;
218
219 /* find the username from the option list */
220 if ((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 */
229 if ( (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" */
234 if (strcmpic(list,US"true") == 0)
235   /* in that case, always return true later */
236   override = 1;
237
238 /* expand spamd_address if needed */
239 if (*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;
248     }
249   }
250 else
251   spamd_address_work = spamd_address;
252
253 DEBUG(D_acl) debug_printf_indent("spamd: addrlist '%s'\n", spamd_address_work);
254
255 /* check if previous spamd_address was expanded and has changed. dump cached results if so */
256 if (  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 */
263 if (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 */
267
268 if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
269   {                                                             /* error while spooling */
270   log_write(0, LOG_MAIN|LOG_PANIC,
271          "%s error while creating mbox spool file", loglabel);
272   return DEFER;
273   }
274
275 start = time(NULL);
276
277   {
278   int num_servers = 0;
279   int current_server;
280   uschar * address;
281   const uschar * spamd_address_list_ptr = spamd_address_work;
282   spamd_address_container * spamd_address_vector[32];
283
284   /* Check how many spamd servers we have
285      and register their addresses */
286   sep = 0;                              /* default colon-sep */
287   while ((address = string_nextinlist(&spamd_address_list_ptr, &sep, NULL, 0)))
288     {
289     const uschar * sublist;
290     int sublist_sep = -(int)' ';        /* default space-sep */
291     unsigned args;
292     uschar * s;
293
294     DEBUG(D_acl) debug_printf_indent("spamd: addr entry '%s'\n", address);
295     sd = (spamd_address_container *)store_get(sizeof(spamd_address_container));
296
297     for (sublist = address, args = 0, spamd_param_init(sd);
298          (s = string_nextinlist(&sublist, &sublist_sep, NULL, 0));
299          args++
300          )
301       {
302         DEBUG(D_acl) debug_printf_indent("spamd:  addr parm '%s'\n", s);
303         switch (args)
304         {
305         case 0:   sd->hostspec = s;
306                   if (*s == '/') args++;        /* local; no port */
307                   break;
308         case 1:   sd->hostspec = string_sprintf("%s %s", sd->hostspec, s);
309                   break;
310         default:  spamd_param(s, sd);
311                   break;
312         }
313       }
314     if (args < 2)
315       {
316       log_write(0, LOG_MAIN,
317         "%s warning - invalid spamd address: '%s'", loglabel, address);
318       continue;
319       }
320
321     spamd_address_vector[num_servers] = sd;
322     if (++num_servers > 31)
323       break;
324     }
325
326   /* check if we have at least one server */
327   if (!num_servers)
328     {
329     log_write(0, LOG_MAIN|LOG_PANIC,
330        "%s no useable spamd server addresses in spamd_address configuration option.",
331        loglabel);
332     goto defer;
333     }
334
335   current_server = spamd_get_server(spamd_address_vector, num_servers);
336   sd = spamd_address_vector[current_server];
337   for(;;)
338     {
339     uschar * errstr;
340
341     DEBUG(D_acl) debug_printf_indent("spamd: trying server %s\n", sd->hostspec);
342
343     for (;;)
344       {
345       /*XXX could potentially use TFO early-data here */
346       if (  (spamd_cctx.sock = ip_streamsocket(sd->hostspec, &errstr, 5)) >= 0
347          || sd->retry <= 0
348          )
349         break;
350       DEBUG(D_acl) debug_printf_indent("spamd: server %s: retry conn\n", sd->hostspec);
351       while (sd->retry > 0) sd->retry = sleep(sd->retry);
352       }
353     if (spamd_cctx.sock >= 0)
354       break;
355
356     log_write(0, LOG_MAIN, "%s spamd: %s", loglabel, errstr);
357     sd->is_failed = TRUE;
358
359     current_server = spamd_get_server(spamd_address_vector, num_servers);
360     if (current_server < 0)
361       {
362       log_write(0, LOG_MAIN|LOG_PANIC, "%s all spamd servers failed", loglabel);
363       goto defer;
364       }
365     sd = spamd_address_vector[current_server];
366     }
367   }
368
369 (void)fcntl(spamd_cctx.sock, F_SETFL, O_NONBLOCK);
370 /* now we are connected to spamd on spamd_cctx.sock */
371 if (sd->is_rspamd)
372   {                             /* rspamd variant */
373   uschar *req_str;
374   const uschar * helo;
375   const uschar * fcrdns;
376   const uschar * authid;
377
378   req_str = string_sprintf("CHECK RSPAMC/1.3\r\nContent-length: %lu\r\n"
379     "Queue-Id: %s\r\nFrom: <%s>\r\nRecipient-Number: %d\r\n",
380     mbox_size, message_id, sender_address, recipients_count);
381   for (i = 0; i < recipients_count; i ++)
382     req_str = string_sprintf("%sRcpt: <%s>\r\n", req_str, recipients_list[i].address);
383   if ((helo = expand_string(US"$sender_helo_name")) != NULL && *helo != '\0')
384     req_str = string_sprintf("%sHelo: %s\r\n", req_str, helo);
385   if ((fcrdns = expand_string(US"$sender_host_name")) != NULL && *fcrdns != '\0')
386     req_str = string_sprintf("%sHostname: %s\r\n", req_str, fcrdns);
387   if (sender_host_address != NULL)
388     req_str = string_sprintf("%sIP: %s\r\n", req_str, sender_host_address);
389   if ((authid = expand_string(US"$authenticated_id")) != NULL && *authid != '\0')
390     req_str = string_sprintf("%sUser: %s\r\n", req_str, authid);
391   req_str = string_sprintf("%s\r\n", req_str);
392   wrote = send(spamd_cctx.sock, req_str->s, req_str->ptr, 0);
393   }
394 else
395   {                             /* spamassassin variant */
396   (void)string_format(spamd_buffer,
397           sizeof(spamd_buffer),
398           "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
399           user_name,
400           mbox_size);
401   /* send our request */
402   wrote = send(spamd_cctx.sock, spamd_buffer, Ustrlen(spamd_buffer), 0);
403   }
404
405 if (wrote == -1)
406   {
407   (void)close(spamd_cctx.sock);
408   log_write(0, LOG_MAIN|LOG_PANIC,
409        "%s spamd %s send failed: %s", loglabel, callout_address, strerror(errno));
410   goto defer;
411   }
412
413 /* now send the file */
414 /* spamd sometimes accepts connections but doesn't read data off
415  * the connection.  We make the file descriptor non-blocking so
416  * that the write will only write sufficient data without blocking
417  * and we poll the descriptor to make sure that we can write without
418  * blocking.  Short writes are gracefully handled and if the whole
419  * transaction takes too long it is aborted.
420  * Note: poll() is not supported in OSX 10.2 and is reported to be
421  *       broken in more recent versions (up to 10.4).
422  */
423 #ifndef NO_POLL_H
424 pollfd.fd = spamd_cctx.sock;
425 pollfd.events = POLLOUT;
426 #endif
427 (void)fcntl(spamd_cctx.sock, F_SETFL, O_NONBLOCK);
428 do
429   {
430   read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
431   if (read > 0)
432     {
433     offset = 0;
434 again:
435 #ifndef NO_POLL_H
436     result = poll(&pollfd, 1, 1000);
437
438 /* Patch posted by Erik ? for OS X and applied by PH */
439 #else
440     select_tv.tv_sec = 1;
441     select_tv.tv_usec = 0;
442     FD_ZERO(&select_fd);
443     FD_SET(spamd_cctx.sock, &select_fd);
444     result = select(spamd_cctx.sock+1, NULL, &select_fd, NULL, &select_tv);
445 #endif
446 /* End Erik's patch */
447
448     if (result == -1 && errno == EINTR)
449       goto again;
450     else if (result < 1)
451       {
452       if (result == -1)
453         log_write(0, LOG_MAIN|LOG_PANIC,
454           "%s %s on spamd %s socket", loglabel, callout_address, strerror(errno));
455       else
456         {
457         if (time(NULL) - start < sd->timeout)
458           goto again;
459         log_write(0, LOG_MAIN|LOG_PANIC,
460           "%s timed out writing spamd %s, socket", loglabel, callout_address);
461         }
462       (void)close(spamd_cctx.sock);
463       goto defer;
464       }
465
466     wrote = send(spamd_cctx.sock,spamd_buffer + offset,read - offset,0);
467     if (wrote == -1)
468       {
469       log_write(0, LOG_MAIN|LOG_PANIC,
470           "%s %s on spamd %s socket", loglabel, callout_address, strerror(errno));
471       (void)close(spamd_cctx.sock);
472       goto defer;
473       }
474     if (offset + wrote != read)
475       {
476       offset += wrote;
477       goto again;
478       }
479     }
480   }
481 while (!feof(mbox_file) && !ferror(mbox_file));
482
483 if (ferror(mbox_file))
484   {
485   log_write(0, LOG_MAIN|LOG_PANIC,
486     "%s error reading spool file: %s", loglabel, strerror(errno));
487   (void)close(spamd_cctx.sock);
488   goto defer;
489   }
490
491 (void)fclose(mbox_file);
492
493 /* we're done sending, close socket for writing */
494 if (!sd->is_rspamd)
495   shutdown(spamd_cctx.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_cctx,
501                    spamd_buffer + offset,
502                    sizeof(spamd_buffer) - offset - 1,
503                    sd->timeout - time(NULL) + start)) > 0)
504   offset += i;
505 spamd_buffer[offset] = '\0';    /* guard byte */
506
507 /* error handling */
508 if (i <= 0 && errno != 0)
509   {
510   log_write(0, LOG_MAIN|LOG_PANIC,
511        "%s error reading from spamd %s, socket: %s", loglabel, callout_address, strerror(errno));
512   (void)close(spamd_cctx.sock);
513   return DEFER;
514   }
515
516 /* reading done */
517 (void)close(spamd_cctx.sock);
518
519 if (sd->is_rspamd)
520   {                             /* rspamd variant of reply */
521   int r;
522   if (  (r = sscanf(CS spamd_buffer,
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,
525           &spamd_reject_score, &spamd_report_offset)) != 5
526      || spamd_report_offset >= offset           /* verify within buffer */
527      )
528     {
529     log_write(0, LOG_MAIN|LOG_PANIC,
530               "%s cannot parse spamd %s, output: %d", loglabel, callout_address, r);
531     return DEFER;
532     }
533   /* now parse action */
534   p = &spamd_buffer[spamd_report_offset];
535
536   if (Ustrncmp(p, "Action: ", sizeof("Action: ") - 1) == 0)
537     {
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';
543     }
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)
552     {
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,
559                   "%s cannot parse spamd %s output", loglabel, callout_address);
560         return DEFER;
561         }
562     }
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;
639
640 defer:
641   (void)fclose(mbox_file);
642   return DEFER;
643 }
644
645 #endif
646 /* vi: aw ai sw=2
647 */