1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
8 /* Code for calling spamassassin's spamd. Called from acl.c. */
11 #ifdef WITH_CONTENT_SCAN
14 uschar spam_score_buffer[16];
15 uschar spam_score_int_buffer[16];
16 uschar spam_bar_buffer[128];
17 uschar spam_report_buffer[32600];
18 uschar prev_user_name[128] = "";
21 uschar *prev_spamd_address_work = NULL;
24 spam(uschar **listptr)
27 uschar *list = *listptr;
29 uschar user_name_buffer[128];
30 unsigned long mbox_size;
33 uschar spamd_buffer[32600];
34 int i, j, offset, result;
35 uschar spamd_version[8];
36 uschar spamd_score_char;
37 double spamd_threshold, spamd_score;
38 int spamd_report_offset;
43 struct sockaddr_un server;
46 #else /* Patch posted by Erik ? for OS X */
47 struct timeval select_tv; /* and applied by PH */
50 uschar *spamd_address_work;
51 static const uschar * loglabel = US"spam acl condition:";
53 /* stop compiler warning */
56 /* find the username from the option list */
57 if ((user_name = string_nextinlist(&list, &sep,
59 sizeof(user_name_buffer))) == NULL)
61 /* no username given, this means no scanning should be done */
65 /* if username is "0" or "false", do not scan */
66 if ( (Ustrcmp(user_name,"0") == 0) ||
67 (strcmpic(user_name,US"false") == 0) )
70 /* if there is an additional option, check if it is "true" */
71 if (strcmpic(list,US"true") == 0)
72 /* in that case, always return true later */
75 /* expand spamd_address if needed */
76 if (*spamd_address == '$')
78 spamd_address_work = expand_string(spamd_address);
79 if (spamd_address_work == NULL)
81 log_write(0, LOG_MAIN|LOG_PANIC,
82 "%s spamd_address starts with $, but expansion failed: %s",
83 loglabel, expand_string_message);
88 spamd_address_work = spamd_address;
90 /* check if previous spamd_address was expanded and has changed. dump cached results if so */
92 && prev_spamd_address_work != NULL
93 && Ustrcmp(prev_spamd_address_work, spamd_address_work) != 0
97 /* if we scanned for this username last time, just return */
98 if (spam_ok && Ustrcmp(prev_user_name, user_name) == 0)
99 return override ? OK : spam_rc;
101 /* make sure the eml mbox file is spooled up */
102 mbox_file = spool_mbox(&mbox_size, NULL);
104 if (mbox_file == NULL)
106 /* error while spooling */
107 log_write(0, LOG_MAIN|LOG_PANIC,
108 "%s error while creating mbox spool file", loglabel);
114 /* socket does not start with '/' -> network socket */
115 if (*spamd_address_work != '/')
119 uschar *address = NULL;
120 uschar *spamd_address_list_ptr = spamd_address_work;
121 uschar address_buffer[256];
122 spamd_address_container * spamd_address_vector[32];
124 /* Check how many spamd servers we have
125 and register their addresses */
126 while ((address = string_nextinlist(&spamd_address_list_ptr, &sep,
128 sizeof(address_buffer))) != NULL)
131 /* Potential memory leak as we never free the store. */
132 spamd_address_container *this_spamd =
133 (spamd_address_container *)store_get(sizeof(spamd_address_container));
135 /* grok spamd address and port */
136 if (sscanf(CS address, "%23s %u", this_spamd->tcp_addr, &this_spamd->tcp_port) != 2)
138 log_write(0, LOG_MAIN,
139 "%s warning - invalid spamd address: '%s'", loglabel, address);
143 spamd_address_vector[num_servers] = this_spamd;
145 >= sizeof(spamd_address_vector)/sizeof(spamd_address_vector[0]))
149 /* check if we have at least one server */
152 log_write(0, LOG_MAIN|LOG_PANIC,
153 "%s no useable spamd server addresses in spamd_address configuration option.",
155 (void)fclose(mbox_file);
159 while (num_servers > 0)
163 /* Randomly pick a server to try */
164 current_server = random_number(num_servers);
166 debug_printf("trying server %s, port %u\n",
167 spamd_address_vector[current_server]->tcp_addr,
168 spamd_address_vector[current_server]->tcp_port);
170 /* contact a spamd */
171 if ((spamd_sock = ip_socket(SOCK_STREAM, AF_INET)) < 0)
173 log_write(0, LOG_MAIN|LOG_PANIC,
174 "%s error creating IP socket for spamd", loglabel);
175 (void)fclose(mbox_file);
179 if (ip_connect(spamd_sock,
181 spamd_address_vector[current_server]->tcp_addr,
182 spamd_address_vector[current_server]->tcp_port,
187 log_write(0, LOG_MAIN|LOG_PANIC,
188 "%s warning - spamd connection to %s, port %u failed: %s",
190 spamd_address_vector[current_server]->tcp_addr,
191 spamd_address_vector[current_server]->tcp_port,
194 (void)close(spamd_sock);
196 /* Remove the server from the list. XXX We should free the memory */
198 for (i = current_server; i < num_servers; i++)
199 spamd_address_vector[i] = spamd_address_vector[i+1];
202 if (num_servers == 0)
204 log_write(0, LOG_MAIN|LOG_PANIC, "%s all spamd servers failed", loglabel);
205 (void)fclose(mbox_file);
211 /* open the local socket */
213 if ((spamd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
215 log_write(0, LOG_MAIN|LOG_PANIC,
216 "%s spamd: unable to acquire socket (%s)",
219 (void)fclose(mbox_file);
223 server.sun_family = AF_UNIX;
224 Ustrcpy(server.sun_path, spamd_address_work);
226 if (connect(spamd_sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0)
228 log_write(0, LOG_MAIN|LOG_PANIC,
229 "%s spamd: unable to connect to UNIX socket %s (%s)",
231 spamd_address_work, strerror(errno) );
232 (void)fclose(mbox_file);
233 (void)close(spamd_sock);
238 if (spamd_sock == -1)
240 log_write(0, LOG_MAIN|LOG_PANIC,
241 "programming fault, spamd_sock unexpectedly unset");
242 (void)fclose(mbox_file);
243 (void)close(spamd_sock);
247 /* now we are connected to spamd on spamd_sock */
248 (void)string_format(spamd_buffer,
249 sizeof(spamd_buffer),
250 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
254 /* send our request */
255 if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0)
257 (void)close(spamd_sock);
258 log_write(0, LOG_MAIN|LOG_PANIC,
259 "%s spamd send failed: %s", loglabel, strerror(errno));
260 (void)fclose(mbox_file);
261 (void)close(spamd_sock);
265 /* now send the file */
266 /* spamd sometimes accepts conections but doesn't read data off
267 * the connection. We make the file descriptor non-blocking so
268 * that the write will only write sufficient data without blocking
269 * and we poll the desciptor to make sure that we can write without
270 * blocking. Short writes are gracefully handled and if the whole
271 * trasaction takes too long it is aborted.
272 * Note: poll() is not supported in OSX 10.2 and is reported to be
273 * broken in more recent versions (up to 10.4).
276 pollfd.fd = spamd_sock;
277 pollfd.events = POLLOUT;
279 (void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK);
282 read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
288 result = poll(&pollfd, 1, 1000);
290 /* Patch posted by Erik ? for OS X and applied by PH */
292 select_tv.tv_sec = 1;
293 select_tv.tv_usec = 0;
295 FD_SET(spamd_sock, &select_fd);
296 result = select(spamd_sock+1, NULL, &select_fd, NULL, &select_tv);
298 /* End Erik's patch */
300 if (result == -1 && errno == EINTR)
305 log_write(0, LOG_MAIN|LOG_PANIC,
306 "%s %s on spamd socket", loglabel, strerror(errno));
309 if (time(NULL) - start < SPAMD_TIMEOUT)
311 log_write(0, LOG_MAIN|LOG_PANIC,
312 "%s timed out writing spamd socket", loglabel);
314 (void)close(spamd_sock);
315 (void)fclose(mbox_file);
319 wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0);
322 log_write(0, LOG_MAIN|LOG_PANIC,
323 "%s %s on spamd socket", loglabel, strerror(errno));
324 (void)close(spamd_sock);
325 (void)fclose(mbox_file);
328 if (offset + wrote != read)
335 while (!feof(mbox_file) && !ferror(mbox_file));
337 if (ferror(mbox_file))
339 log_write(0, LOG_MAIN|LOG_PANIC,
340 "%s error reading spool file: %s", loglabel, strerror(errno));
341 (void)close(spamd_sock);
342 (void)fclose(mbox_file);
346 (void)fclose(mbox_file);
348 /* we're done sending, close socket for writing */
349 shutdown(spamd_sock,SHUT_WR);
351 /* read spamd response using what's left of the timeout.
353 memset(spamd_buffer, 0, sizeof(spamd_buffer));
355 while ((i = ip_recv(spamd_sock,
356 spamd_buffer + offset,
357 sizeof(spamd_buffer) - offset - 1,
358 SPAMD_TIMEOUT - time(NULL) + start)) > 0 )
362 if (i <= 0 && errno != 0)
364 log_write(0, LOG_MAIN|LOG_PANIC,
365 "%s error reading from spamd socket: %s", loglabel, strerror(errno));
366 (void)close(spamd_sock);
371 (void)close(spamd_sock);
373 /* dig in the spamd output and put the report in a multiline header, if requested */
374 if (sscanf(CS spamd_buffer,
375 "SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
376 spamd_version, &spamd_score, &spamd_threshold,
377 &spamd_report_offset) != 3)
380 /* try to fall back to pre-2.50 spamd output */
381 if (sscanf(CS spamd_buffer,
382 "SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
383 spamd_version, &spamd_score, &spamd_threshold,
384 &spamd_report_offset) != 3 )
386 log_write(0, LOG_MAIN|LOG_PANIC,
387 "%s cannot parse spamd output", loglabel);
392 /* Create report. Since this is a multiline string,
393 we must hack it into shape first */
394 p = &spamd_buffer[spamd_report_offset];
395 q = spam_report_buffer;
407 /* add an extra space after the newline to ensure
408 that it is treated as a header continuation line */
414 /* cut off trailing leftovers */
418 spam_report = spam_report_buffer;
420 /* create spam bar */
421 spamd_score_char = spamd_score > 0 ? '+' : '-';
422 j = abs((int)(spamd_score));
425 while ((i < j) && (i <= MAX_SPAM_BAR_CHARS))
426 spam_bar_buffer[i++] = spamd_score_char;
429 spam_bar_buffer[0] = '/';
432 spam_bar_buffer[i] = '\0';
433 spam_bar = spam_bar_buffer;
435 /* create "float" spam score */
436 (void)string_format(spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score);
437 spam_score = spam_score_buffer;
439 /* create "int" spam score */
440 j = (int)((spamd_score + 0.001)*10);
441 (void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j);
442 spam_score_int = spam_score_int_buffer;
444 /* compare threshold against score */
445 if (spamd_score >= spamd_threshold)
447 /* spam as determined by user's threshold */
456 /* remember expanded spamd_address if needed */
457 if (spamd_address_work != spamd_address)
458 prev_spamd_address_work = string_copy(spamd_address_work);
460 /* remember user name and "been here" for it */
461 Ustrcpy(prev_user_name, user_name);
464 if (override) /* always return OK, no matter what the score */