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