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;
23 int spam(uschar **listptr) {
25 uschar *list = *listptr;
27 uschar user_name_buffer[128];
28 unsigned long mbox_size;
31 uschar spamd_buffer[32600];
32 int i, j, offset, result;
33 uschar spamd_version[8];
34 uschar spamd_score_char;
35 double spamd_threshold, spamd_score;
36 int spamd_report_offset;
41 struct sockaddr_un server;
44 #else /* Patch posted by Erik ? for OS X */
45 struct timeval select_tv; /* and applied by PH */
48 uschar *spamd_address_work;
50 /* stop compiler warning */
53 /* find the username from the option list */
54 if ((user_name = string_nextinlist(&list, &sep,
56 sizeof(user_name_buffer))) == NULL) {
57 /* no username given, this means no scanning should be done */
61 /* if username is "0" or "false", do not scan */
62 if ( (Ustrcmp(user_name,"0") == 0) ||
63 (strcmpic(user_name,US"false") == 0) ) {
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 */
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);
83 spamd_address_work = spamd_address;
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)) {
90 /* if we scanned for this username last time, just return */
91 if ( spam_ok && ( Ustrcmp(prev_user_name, user_name) == 0 ) ) {
98 /* make sure the eml mbox file is spooled up */
99 mbox_file = spool_mbox(&mbox_size, NULL);
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");
110 /* socket does not start with '/' -> network socket */
111 if (*spamd_address_work != '/') {
114 uschar *address = NULL;
115 uschar *spamd_address_list_ptr = spamd_address_work;
116 uschar address_buffer[256];
117 spamd_address_container * spamd_address_vector[32];
119 /* Check how many spamd servers we have
120 and register their addresses */
121 while ((address = string_nextinlist(&spamd_address_list_ptr, &sep,
123 sizeof(address_buffer))) != NULL) {
125 /* Potential memory leak as we never free the store. */
126 spamd_address_container *this_spamd =
127 (spamd_address_container *)store_get(sizeof(spamd_address_container));
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);
136 spamd_address_vector[num_servers] = this_spamd;
138 if (num_servers > 31)
142 /* check if we have at least one server */
144 log_write(0, LOG_MAIN|LOG_PANIC,
145 "spam acl condition: no useable spamd server addresses in spamd_address configuration option.");
146 (void)fclose(mbox_file);
150 while ( num_servers > 0 ) {
153 /* Randomly pick a server to try */
154 current_server = random_number( num_servers );
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);
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");
164 (void)fclose(mbox_file);
168 if (ip_connect( spamd_sock,
170 spamd_address_vector[current_server]->tcp_addr,
171 spamd_address_vector[current_server]->tcp_port,
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,
183 (void)close(spamd_sock);
185 /* Remove the server from the list. XXX We should free the memory */
187 for( i = current_server; i < num_servers; i++ )
188 spamd_address_vector[i] = spamd_address_vector[i+1];
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);
199 /* open the local socket */
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)",
205 (void)fclose(mbox_file);
209 server.sun_family = AF_UNIX;
210 Ustrcpy(server.sun_path, spamd_address_work);
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)",
215 spamd_address_work, strerror(errno) );
216 (void)fclose(mbox_file);
217 (void)close(spamd_sock);
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);
231 /* now we are connected to spamd on spamd_sock */
232 (void)string_format(spamd_buffer,
233 sizeof(spamd_buffer),
234 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
238 /* send our request */
239 if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0) {
240 (void)close(spamd_sock);
241 log_write(0, LOG_MAIN|LOG_PANIC,
242 "spam acl condition: spamd send failed: %s", strerror(errno));
243 (void)fclose(mbox_file);
244 (void)close(spamd_sock);
248 /* now send the file */
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.
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).
259 pollfd.fd = spamd_sock;
260 pollfd.events = POLLOUT;
262 (void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK);
264 read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
269 result = poll(&pollfd, 1, 1000);
271 /* Patch posted by Erik ? for OS X and applied by PH */
273 select_tv.tv_sec = 1;
274 select_tv.tv_usec = 0;
276 FD_SET(spamd_sock, &select_fd);
277 result = select(spamd_sock+1, NULL, &select_fd, NULL, &select_tv);
279 /* End Erik's patch */
281 if (result == -1 && errno == EINTR)
283 else if (result < 1) {
285 log_write(0, LOG_MAIN|LOG_PANIC,
286 "spam acl condition: %s on spamd socket", strerror(errno));
288 if (time(NULL) - start < SPAMD_TIMEOUT)
290 log_write(0, LOG_MAIN|LOG_PANIC,
291 "spam acl condition: timed out writing spamd socket");
293 (void)close(spamd_sock);
294 (void)fclose(mbox_file);
298 wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0);
301 log_write(0, LOG_MAIN|LOG_PANIC,
302 "spam acl condition: %s on spamd socket", strerror(errno));
303 (void)close(spamd_sock);
304 (void)fclose(mbox_file);
307 if (offset + wrote != read) {
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));
317 (void)close(spamd_sock);
318 (void)fclose(mbox_file);
322 (void)fclose(mbox_file);
324 /* we're done sending, close socket for writing */
325 shutdown(spamd_sock,SHUT_WR);
327 /* read spamd response using what's left of the timeout.
329 memset(spamd_buffer, 0, sizeof(spamd_buffer));
331 while((i = ip_recv(spamd_sock,
332 spamd_buffer + offset,
333 sizeof(spamd_buffer) - offset - 1,
334 SPAMD_TIMEOUT - time(NULL) + start)) > 0 ) {
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));
342 (void)close(spamd_sock);
347 (void)close(spamd_sock);
349 /* dig in the spamd output and put the report in a multiline header, if requested */
350 if( sscanf(CS spamd_buffer,"SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
351 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
353 /* try to fall back to pre-2.50 spamd output */
354 if( sscanf(CS spamd_buffer,"SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
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");
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;
375 /* add an extra space after the newline to ensure
376 that it is treated as a header continuation line */
385 /* cut off trailing leftovers */
390 spam_report = spam_report_buffer;
392 /* create spam bar */
393 spamd_score_char = spamd_score > 0 ? '+' : '-';
394 j = abs((int)(spamd_score));
397 while((i < j) && (i <= MAX_SPAM_BAR_CHARS))
398 spam_bar_buffer[i++] = spamd_score_char;
401 spam_bar_buffer[0] = '/';
404 spam_bar_buffer[i] = '\0';
405 spam_bar = spam_bar_buffer;
407 /* create "float" spam score */
408 (void)string_format(spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score);
409 spam_score = spam_score_buffer;
411 /* create "int" spam score */
412 j = (int)((spamd_score + 0.001)*10);
413 (void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j);
414 spam_score_int = spam_score_int_buffer;
416 /* compare threshold against score */
417 if (spamd_score >= spamd_threshold) {
418 /* spam as determined by user's threshold */
426 /* remember expanded spamd_address if needed */
427 if (spamd_address_work != spamd_address) {
428 prev_spamd_address_work = string_copy(spamd_address_work);
430 /* remember user name and "been here" for it */
431 Ustrcpy(prev_user_name, user_name);
435 /* always return OK, no matter what the score */