Patch to use select() when poll() not available in spam.c.
[exim.git] / src / src / spam.c
1 /* $Cambridge: exim/src/src/spam.c,v 1.12 2006/07/03 15:19:44 ph10 Exp $ */
2
3 /*************************************************
4 *     Exim - an Internet mail transport agent    *
5 *************************************************/
6
7 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
8 /* License: GPL */
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_report_buffer[32600];
20 uschar prev_user_name[128] = "";
21 int spam_ok = 0;
22 int spam_rc = 0;
23
24 int spam(uschar **listptr) {
25   int sep = 0;
26   uschar *list = *listptr;
27   uschar *user_name;
28   uschar user_name_buffer[128];
29   unsigned long mbox_size;
30   FILE *mbox_file;
31   int spamd_sock;
32   uschar spamd_buffer[32600];
33   int i, j, offset, result;
34   uschar spamd_version[8];
35   uschar spamd_score_char;
36   double spamd_threshold, spamd_score;
37   int spamd_report_offset;
38   uschar *p,*q;
39   int override = 0;
40   time_t start;
41   size_t read, wrote;
42   struct sockaddr_un server;
43 #ifndef NO_POLL_H
44   struct pollfd pollfd;
45 #else                               /* Patch posted by Erik ? for OS X */
46   struct timeval select_tv;         /* and applied by PH */
47   fd_set select_fd;
48 #endif
49
50   /* stop compiler warning */
51   result = result;
52
53   /* find the username from the option list */
54   if ((user_name = string_nextinlist(&list, &sep,
55                                      user_name_buffer,
56                                      sizeof(user_name_buffer))) == NULL) {
57     /* no username given, this means no scanning should be done */
58     return FAIL;
59   };
60
61   /* if username is "0" or "false", do not scan */
62   if ( (Ustrcmp(user_name,"0") == 0) ||
63        (strcmpic(user_name,US"false") == 0) ) {
64     return FAIL;
65   };
66
67   /* if there is an additional option, check if it is "true" */
68   if (strcmpic(list,US"true") == 0) {
69     /* in that case, always return true later */
70     override = 1;
71   };
72
73   /* if we scanned for this username last time, just return */
74   if ( spam_ok && ( Ustrcmp(prev_user_name, user_name) == 0 ) ) {
75     if (override)
76       return OK;
77     else
78       return spam_rc;
79   };
80
81   /* make sure the eml mbox file is spooled up */
82   mbox_file = spool_mbox(&mbox_size);
83
84   if (mbox_file == NULL) {
85     /* error while spooling */
86     log_write(0, LOG_MAIN|LOG_PANIC,
87            "spam acl condition: error while creating mbox spool file");
88     return DEFER;
89   };
90
91   start = time(NULL);
92   /* socket does not start with '/' -> network socket */
93   if (*spamd_address != '/') {
94     time_t now = time(NULL);
95     int num_servers = 0;
96     int current_server = 0;
97     int start_server = 0;
98     uschar *address = NULL;
99     uschar *spamd_address_list_ptr = spamd_address;
100     uschar address_buffer[256];
101     spamd_address_container * spamd_address_vector[32];
102
103     /* Check how many spamd servers we have
104        and register their addresses */
105     while ((address = string_nextinlist(&spamd_address_list_ptr, &sep,
106                                         address_buffer,
107                                         sizeof(address_buffer))) != NULL) {
108
109       spamd_address_container *this_spamd =
110         (spamd_address_container *)store_get(sizeof(spamd_address_container));
111
112       /* grok spamd address and port */
113       if( sscanf(CS address, "%s %u", this_spamd->tcp_addr, &(this_spamd->tcp_port)) != 2 ) {
114         log_write(0, LOG_MAIN,
115           "spam acl condition: warning - invalid spamd address: '%s'", address);
116         continue;
117       };
118
119       spamd_address_vector[num_servers] = this_spamd;
120       num_servers++;
121       if (num_servers > 31)
122         break;
123     };
124
125     /* check if we have at least one server */
126     if (!num_servers) {
127       log_write(0, LOG_MAIN|LOG_PANIC,
128          "spam acl condition: no useable spamd server addresses in spamd_address configuration option.");
129       (void)fclose(mbox_file);
130       return DEFER;
131     };
132
133     current_server = start_server = (int)now % num_servers;
134
135     while (1) {
136
137       debug_printf("trying server %s, port %u\n",
138                    spamd_address_vector[current_server]->tcp_addr,
139                    spamd_address_vector[current_server]->tcp_port);
140
141       /* contact a spamd */
142       if ( (spamd_sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
143         log_write(0, LOG_MAIN|LOG_PANIC,
144            "spam acl condition: error creating IP socket for spamd");
145         (void)fclose(mbox_file);
146         return DEFER;
147       };
148
149       if (ip_connect( spamd_sock,
150                       AF_INET,
151                       spamd_address_vector[current_server]->tcp_addr,
152                       spamd_address_vector[current_server]->tcp_port,
153                       5 ) > -1) {
154         /* connection OK */
155         break;
156       };
157
158       log_write(0, LOG_MAIN|LOG_PANIC,
159          "spam acl condition: warning - spamd connection to %s, port %u failed: %s",
160          spamd_address_vector[current_server]->tcp_addr,
161          spamd_address_vector[current_server]->tcp_port,
162          strerror(errno));
163       current_server++;
164       if (current_server >= num_servers)
165         current_server = 0;
166       if (current_server == start_server) {
167         log_write(0, LOG_MAIN|LOG_PANIC, "spam acl condition: all spamd servers failed");
168         (void)fclose(mbox_file);
169         (void)close(spamd_sock);
170         return DEFER;
171       };
172     };
173
174   }
175   else {
176     /* open the local socket */
177
178     if ((spamd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
179       log_write(0, LOG_MAIN|LOG_PANIC,
180                 "malware acl condition: spamd: unable to acquire socket (%s)",
181                 strerror(errno));
182       (void)fclose(mbox_file);
183       return DEFER;
184     }
185
186     server.sun_family = AF_UNIX;
187     Ustrcpy(server.sun_path, spamd_address);
188
189     if (connect(spamd_sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
190       log_write(0, LOG_MAIN|LOG_PANIC,
191                 "malware acl condition: spamd: unable to connect to UNIX socket %s (%s)",
192                 spamd_address, strerror(errno) );
193       (void)fclose(mbox_file);
194       (void)close(spamd_sock);
195       return DEFER;
196     }
197
198   }
199
200   /* now we are connected to spamd on spamd_sock */
201   (void)string_format(spamd_buffer,
202            sizeof(spamd_buffer),
203            "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
204            user_name,
205            mbox_size);
206
207   /* send our request */
208   if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0) {
209     (void)close(spamd_sock);
210     log_write(0, LOG_MAIN|LOG_PANIC,
211          "spam acl condition: spamd send failed: %s", strerror(errno));
212     (void)fclose(mbox_file);
213     (void)close(spamd_sock);
214     return DEFER;
215   };
216
217   /* now send the file */
218   /* spamd sometimes accepts conections but doesn't read data off
219    * the connection.  We make the file descriptor non-blocking so
220    * that the write will only write sufficient data without blocking
221    * and we poll the desciptor to make sure that we can write without
222    * blocking.  Short writes are gracefully handled and if the whole
223    * trasaction takes too long it is aborted.
224    * Note: poll() is not supported in OSX 10.2 and is reported to be
225    *       broken in more recent versions (up to 10.4).
226    */
227 #ifndef NO_POLL_H
228   pollfd.fd = spamd_sock;
229   pollfd.events = POLLOUT;
230 #endif
231   (void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK);
232   do {
233     read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
234     if (read > 0) {
235       offset = 0;
236 again:
237 #ifndef NO_POLL_H
238       result = poll(&pollfd, 1, 1000);
239
240 /* Patch posted by Erik ? for OS X and applied by PH */
241 #else
242       select_tv.tv_sec = 1;
243       select_tv.tv_usec = 0;
244       FD_ZERO(&select_fd);
245       FD_SET(spamd_sock, &select_fd);
246       result = select(spamd_sock+1, NULL, &select_fd, NULL, &select_tv);
247 #endif
248 /* End Erik's patch */
249
250       if (result == -1 && errno == EINTR)
251         goto again;
252       else if (result < 1) {
253         if (result == -1)
254           log_write(0, LOG_MAIN|LOG_PANIC,
255             "spam acl condition: %s on spamd socket", strerror(errno));
256         else {
257           if (time(NULL) - start < SPAMD_TIMEOUT)
258           goto again;
259           log_write(0, LOG_MAIN|LOG_PANIC,
260             "spam acl condition: timed out writing spamd socket");
261         }
262         (void)close(spamd_sock);
263         (void)fclose(mbox_file);
264         return DEFER;
265       }
266
267       wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0);
268       if (wrote == -1)
269       {
270           log_write(0, LOG_MAIN|LOG_PANIC,
271             "spam acl condition: %s on spamd socket", strerror(errno));
272         (void)close(spamd_sock);
273         (void)fclose(mbox_file);
274         return DEFER;
275       }
276       if (offset + wrote != read) {
277         offset += wrote;
278         goto again;
279       }
280     }
281   }
282   while (!feof(mbox_file) && !ferror(mbox_file));
283   if (ferror(mbox_file)) {
284     log_write(0, LOG_MAIN|LOG_PANIC,
285       "spam acl condition: error reading spool file: %s", strerror(errno));
286     (void)close(spamd_sock);
287     (void)fclose(mbox_file);
288     return DEFER;
289   }
290
291   (void)fclose(mbox_file);
292
293   /* we're done sending, close socket for writing */
294   shutdown(spamd_sock,SHUT_WR);
295
296   /* read spamd response using what's left of the timeout.
297    */
298   memset(spamd_buffer, 0, sizeof(spamd_buffer));
299   offset = 0;
300   while((i = ip_recv(spamd_sock,
301                      spamd_buffer + offset,
302                      sizeof(spamd_buffer) - offset - 1,
303                      SPAMD_TIMEOUT - time(NULL) + start)) > 0 ) {
304     offset += i;
305   }
306
307   /* error handling */
308   if((i <= 0) && (errno != 0)) {
309     log_write(0, LOG_MAIN|LOG_PANIC,
310          "spam acl condition: error reading from spamd socket: %s", strerror(errno));
311     (void)close(spamd_sock);
312     return DEFER;
313   }
314
315   /* reading done */
316   (void)close(spamd_sock);
317
318   /* dig in the spamd output and put the report in a multiline header, if requested */
319   if( sscanf(CS spamd_buffer,"SPAMD/%s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
320              spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
321
322     /* try to fall back to pre-2.50 spamd output */
323     if( sscanf(CS spamd_buffer,"SPAMD/%s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
324                spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
325       log_write(0, LOG_MAIN|LOG_PANIC,
326          "spam acl condition: cannot parse spamd output");
327       return DEFER;
328     };
329   };
330
331   /* Create report. Since this is a multiline string,
332   we must hack it into shape first */
333   p = &spamd_buffer[spamd_report_offset];
334   q = spam_report_buffer;
335   while (*p != '\0') {
336     /* skip \r */
337     if (*p == '\r') {
338       p++;
339       continue;
340     };
341     *q = *p;
342     q++;
343     if (*p == '\n') {
344       *q = '\t';
345       q++;
346       /* eat whitespace */
347       while( (*p <= ' ') && (*p != '\0') ) {
348         p++;
349       };
350       p--;
351     };
352     p++;
353   };
354   /* NULL-terminate */
355   *q = '\0';
356   q--;
357   /* cut off trailing leftovers */
358   while (*q <= ' ') {
359     *q = '\0';
360     q--;
361   };
362   spam_report = spam_report_buffer;
363
364   /* create spam bar */
365   spamd_score_char = spamd_score > 0 ? '+' : '-';
366   j = abs((int)(spamd_score));
367   i = 0;
368   if( j != 0 ) {
369     while((i < j) && (i <= MAX_SPAM_BAR_CHARS))
370        spam_bar_buffer[i++] = spamd_score_char;
371   }
372   else{
373     spam_bar_buffer[0] = '/';
374     i = 1;
375   }
376   spam_bar_buffer[i] = '\0';
377   spam_bar = spam_bar_buffer;
378
379   /* create "float" spam score */
380   (void)string_format(spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score);
381   spam_score = spam_score_buffer;
382
383   /* create "int" spam score */
384   j = (int)((spamd_score + 0.001)*10);
385   (void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j);
386   spam_score_int = spam_score_int_buffer;
387
388   /* compare threshold against score */
389   if (spamd_score >= spamd_threshold) {
390     /* spam as determined by user's threshold */
391     spam_rc = OK;
392   }
393   else {
394     /* not spam */
395     spam_rc = FAIL;
396   };
397
398   /* remember user name and "been here" for it */
399   Ustrcpy(prev_user_name, user_name);
400   spam_ok = 1;
401
402   if (override) {
403     /* always return OK, no matter what the score */
404     return OK;
405   }
406   else {
407     return spam_rc;
408   };
409 }
410
411 #endif