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