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