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