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;
52 /* stop compiler warning */
55 /* find the username from the option list */
56 if ((user_name = string_nextinlist(&list, &sep,
58 sizeof(user_name_buffer))) == NULL)
60 /* no username given, this means no scanning should be done */
64 /* if username is "0" or "false", do not scan */
65 if ( (Ustrcmp(user_name,"0") == 0) ||
66 (strcmpic(user_name,US"false") == 0) )
69 /* if there is an additional option, check if it is "true" */
70 if (strcmpic(list,US"true") == 0)
71 /* in that case, always return true later */
74 /* expand spamd_address if needed */
75 if (*spamd_address == '$')
77 spamd_address_work = expand_string(spamd_address);
78 if (spamd_address_work == NULL)
80 log_write(0, LOG_MAIN|LOG_PANIC,
81 "spamassassin acl condition: spamd_address starts with $, but expansion failed: %s", expand_string_message);
86 spamd_address_work = spamd_address;
88 /* check if previous spamd_address was expanded and has changed. dump cached results if so */
90 && prev_spamd_address_work != NULL
91 && Ustrcmp(prev_spamd_address_work, spamd_address_work) != 0
95 /* if we scanned for this username last time, just return */
96 if ( spam_ok && Ustrcmp(prev_user_name, user_name) == 0)
102 /* make sure the eml mbox file is spooled up */
103 mbox_file = spool_mbox(&mbox_size, NULL);
105 if (mbox_file == NULL)
107 /* error while spooling */
108 log_write(0, LOG_MAIN|LOG_PANIC,
109 "spam acl condition: error while creating mbox spool file");
115 /* socket does not start with '/' -> network socket */
116 if (*spamd_address_work != '/')
120 uschar *address = NULL;
121 uschar *spamd_address_list_ptr = spamd_address_work;
122 uschar address_buffer[256];
123 spamd_address_container * spamd_address_vector[32];
125 /* Check how many spamd servers we have
126 and register their addresses */
127 while ((address = string_nextinlist(&spamd_address_list_ptr, &sep,
129 sizeof(address_buffer))) != NULL)
132 /* Potential memory leak as we never free the store. */
133 spamd_address_container *this_spamd =
134 (spamd_address_container *)store_get(sizeof(spamd_address_container));
136 /* grok spamd address and port */
137 if (sscanf(CS address, "%s %u", this_spamd->tcp_addr, &(this_spamd->tcp_port)) != 2)
139 log_write(0, LOG_MAIN,
140 "spam acl condition: warning - invalid spamd address: '%s'", address);
144 spamd_address_vector[num_servers] = this_spamd;
145 if (++num_servers > 31)
149 /* check if we have at least one server */
152 log_write(0, LOG_MAIN|LOG_PANIC,
153 "spam acl condition: no useable spamd server addresses in spamd_address configuration option.");
154 (void)fclose(mbox_file);
158 while ( num_servers > 0 )
162 /* Randomly pick a server to try */
163 current_server = random_number( num_servers );
165 debug_printf("trying server %s, port %u\n",
166 spamd_address_vector[current_server]->tcp_addr,
167 spamd_address_vector[current_server]->tcp_port);
169 /* contact a spamd */
170 if ( (spamd_sock = ip_socket(SOCK_STREAM, AF_INET)) < 0)
172 log_write(0, LOG_MAIN|LOG_PANIC,
173 "spam acl condition: error creating IP socket for spamd");
174 (void)fclose(mbox_file);
178 if (ip_connect( spamd_sock,
180 spamd_address_vector[current_server]->tcp_addr,
181 spamd_address_vector[current_server]->tcp_port,
186 log_write(0, LOG_MAIN|LOG_PANIC,
187 "spam acl condition: warning - spamd connection to %s, port %u failed: %s",
188 spamd_address_vector[current_server]->tcp_addr,
189 spamd_address_vector[current_server]->tcp_port,
192 (void)close(spamd_sock);
194 /* Remove the server from the list. XXX We should free the memory */
196 for ( i = current_server; i < num_servers; i++ )
197 spamd_address_vector[i] = spamd_address_vector[i+1];
200 if ( num_servers == 0 )
202 log_write(0, LOG_MAIN|LOG_PANIC, "spam acl condition: all spamd servers failed");
203 (void)fclose(mbox_file);
209 /* open the local socket */
211 if ((spamd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
213 log_write(0, LOG_MAIN|LOG_PANIC,
214 "malware acl condition: spamd: unable to acquire socket (%s)",
216 (void)fclose(mbox_file);
220 server.sun_family = AF_UNIX;
221 Ustrcpy(server.sun_path, spamd_address_work);
223 if (connect(spamd_sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0)
225 log_write(0, LOG_MAIN|LOG_PANIC,
226 "malware acl condition: spamd: unable to connect to UNIX socket %s (%s)",
227 spamd_address_work, strerror(errno) );
228 (void)fclose(mbox_file);
229 (void)close(spamd_sock);
234 if (spamd_sock == -1)
236 log_write(0, LOG_MAIN|LOG_PANIC,
237 "programming fault, spamd_sock unexpectedly unset");
238 (void)fclose(mbox_file);
239 (void)close(spamd_sock);
243 /* now we are connected to spamd on spamd_sock */
244 (void)string_format(spamd_buffer,
245 sizeof(spamd_buffer),
246 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
250 /* send our request */
251 if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0)
253 (void)close(spamd_sock);
254 log_write(0, LOG_MAIN|LOG_PANIC,
255 "spam acl condition: spamd send failed: %s", strerror(errno));
256 (void)fclose(mbox_file);
257 (void)close(spamd_sock);
261 /* now send the file */
262 /* spamd sometimes accepts conections but doesn't read data off
263 * the connection. We make the file descriptor non-blocking so
264 * that the write will only write sufficient data without blocking
265 * and we poll the desciptor to make sure that we can write without
266 * blocking. Short writes are gracefully handled and if the whole
267 * trasaction takes too long it is aborted.
268 * Note: poll() is not supported in OSX 10.2 and is reported to be
269 * broken in more recent versions (up to 10.4).
272 pollfd.fd = spamd_sock;
273 pollfd.events = POLLOUT;
275 (void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK);
278 read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
284 result = poll(&pollfd, 1, 1000);
286 /* Patch posted by Erik ? for OS X and applied by PH */
288 select_tv.tv_sec = 1;
289 select_tv.tv_usec = 0;
291 FD_SET(spamd_sock, &select_fd);
292 result = select(spamd_sock+1, NULL, &select_fd, NULL, &select_tv);
294 /* End Erik's patch */
296 if (result == -1 && errno == EINTR)
301 log_write(0, LOG_MAIN|LOG_PANIC,
302 "spam acl condition: %s on spamd socket", strerror(errno));
305 if (time(NULL) - start < SPAMD_TIMEOUT)
307 log_write(0, LOG_MAIN|LOG_PANIC,
308 "spam acl condition: timed out writing spamd socket");
310 (void)close(spamd_sock);
311 (void)fclose(mbox_file);
315 wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0);
318 log_write(0, LOG_MAIN|LOG_PANIC,
319 "spam acl condition: %s on spamd socket", strerror(errno));
320 (void)close(spamd_sock);
321 (void)fclose(mbox_file);
324 if (offset + wrote != read)
331 while (!feof(mbox_file) && !ferror(mbox_file));
333 if (ferror(mbox_file))
335 log_write(0, LOG_MAIN|LOG_PANIC,
336 "spam acl condition: error reading spool file: %s", strerror(errno));
337 (void)close(spamd_sock);
338 (void)fclose(mbox_file);
342 (void)fclose(mbox_file);
344 /* we're done sending, close socket for writing */
345 shutdown(spamd_sock,SHUT_WR);
347 /* read spamd response using what's left of the timeout.
349 memset(spamd_buffer, 0, sizeof(spamd_buffer));
351 while ((i = ip_recv(spamd_sock,
352 spamd_buffer + offset,
353 sizeof(spamd_buffer) - offset - 1,
354 SPAMD_TIMEOUT - time(NULL) + start)) > 0 )
358 if (i <= 0 && errno != 0)
360 log_write(0, LOG_MAIN|LOG_PANIC,
361 "spam acl condition: error reading from spamd socket: %s", strerror(errno));
362 (void)close(spamd_sock);
367 (void)close(spamd_sock);
369 /* dig in the spamd output and put the report in a multiline header, if requested */
370 if (sscanf(CS spamd_buffer,
371 "SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
372 spamd_version, &spamd_score, &spamd_threshold,
373 &spamd_report_offset) != 3)
376 /* try to fall back to pre-2.50 spamd output */
377 if (sscanf(CS spamd_buffer,
378 "SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
379 spamd_version, &spamd_score, &spamd_threshold,
380 &spamd_report_offset) != 3 )
382 log_write(0, LOG_MAIN|LOG_PANIC,
383 "spam acl condition: cannot parse spamd output");
388 /* Create report. Since this is a multiline string,
389 we must hack it into shape first */
390 p = &spamd_buffer[spamd_report_offset];
391 q = spam_report_buffer;
403 /* add an extra space after the newline to ensure
404 that it is treated as a header continuation line */
410 /* cut off trailing leftovers */
414 spam_report = spam_report_buffer;
416 /* create spam bar */
417 spamd_score_char = spamd_score > 0 ? '+' : '-';
418 j = abs((int)(spamd_score));
421 while ((i < j) && (i <= MAX_SPAM_BAR_CHARS))
422 spam_bar_buffer[i++] = spamd_score_char;
425 spam_bar_buffer[0] = '/';
428 spam_bar_buffer[i] = '\0';
429 spam_bar = spam_bar_buffer;
431 /* create "float" spam score */
432 (void)string_format(spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score);
433 spam_score = spam_score_buffer;
435 /* create "int" spam score */
436 j = (int)((spamd_score + 0.001)*10);
437 (void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j);
438 spam_score_int = spam_score_int_buffer;
440 /* compare threshold against score */
441 if (spamd_score >= spamd_threshold)
443 /* spam as determined by user's threshold */
452 /* remember expanded spamd_address if needed */
453 if (spamd_address_work != spamd_address)
454 prev_spamd_address_work = string_copy(spamd_address_work);
456 /* remember user name and "been here" for it */
457 Ustrcpy(prev_user_name, user_name);
460 if (override) /* always return OK, no matter what the score */