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