1 /* $Cambridge: exim/src/src/spam.c,v 1.5 2005/04/27 10:00:18 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
10 /* Code for calling spamassassin's spamd. Called from acl.c. */
13 #ifdef WITH_CONTENT_SCAN
16 uschar spam_score_buffer[16];
17 uschar spam_score_int_buffer[16];
18 uschar spam_bar_buffer[128];
19 uschar spam_report_buffer[32600];
20 uschar prev_user_name[128] = "";
24 int spam(uschar **listptr) {
26 uschar *list = *listptr;
28 uschar user_name_buffer[128];
29 unsigned long mbox_size;
32 uschar spamd_buffer[32600];
33 int i, j, offset, result;
34 uschar spamd_version[8];
35 uschar spamd_score_char;
36 double spamd_threshold, spamd_score;
37 int spamd_report_offset;
42 struct sockaddr_un server;
45 /* find the username from the option list */
46 if ((user_name = string_nextinlist(&list, &sep,
48 sizeof(user_name_buffer))) == NULL) {
49 /* no username given, this means no scanning should be done */
53 /* if username is "0" or "false", do not scan */
54 if ( (Ustrcmp(user_name,"0") == 0) ||
55 (strcmpic(user_name,US"false") == 0) ) {
59 /* if there is an additional option, check if it is "true" */
60 if (strcmpic(list,US"true") == 0) {
61 /* in that case, always return true later */
65 /* if we scanned for this username last time, just return */
66 if ( spam_ok && ( Ustrcmp(prev_user_name, user_name) == 0 ) ) {
73 /* make sure the eml mbox file is spooled up */
74 mbox_file = spool_mbox(&mbox_size);
76 if (mbox_file == NULL) {
77 /* error while spooling */
78 log_write(0, LOG_MAIN|LOG_PANIC,
79 "spam acl condition: error while creating mbox spool file");
84 /* socket does not start with '/' -> network socket */
85 if (*spamd_address != '/') {
86 time_t now = time(NULL);
88 int current_server = 0;
90 uschar *address = NULL;
91 uschar *spamd_address_list_ptr = spamd_address;
92 uschar address_buffer[256];
93 spamd_address_container * spamd_address_vector[32];
95 /* Check how many spamd servers we have
96 and register their addresses */
97 while ((address = string_nextinlist(&spamd_address_list_ptr, &sep,
99 sizeof(address_buffer))) != NULL) {
101 spamd_address_container *this_spamd =
102 (spamd_address_container *)store_get(sizeof(spamd_address_container));
104 /* grok spamd address and port */
105 if( sscanf(CS address, "%s %u", this_spamd->tcp_addr, &(this_spamd->tcp_port)) != 2 ) {
106 log_write(0, LOG_MAIN,
107 "spam acl condition: warning - invalid spamd address: '%s'", address);
111 spamd_address_vector[num_servers] = this_spamd;
113 if (num_servers > 31)
117 /* check if we have at least one server */
119 log_write(0, LOG_MAIN|LOG_PANIC,
120 "spam acl condition: no useable spamd server addresses in spamd_address configuration option.");
125 current_server = start_server = (int)now % num_servers;
129 debug_printf("trying server %s, port %u\n",
130 spamd_address_vector[current_server]->tcp_addr,
131 spamd_address_vector[current_server]->tcp_port);
133 /* contact a spamd */
134 if ( (spamd_sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
135 log_write(0, LOG_MAIN|LOG_PANIC,
136 "spam acl condition: error creating IP socket for spamd");
141 if (ip_connect( spamd_sock,
143 spamd_address_vector[current_server]->tcp_addr,
144 spamd_address_vector[current_server]->tcp_port,
150 log_write(0, LOG_MAIN|LOG_PANIC,
151 "spam acl condition: warning - spamd connection to %s, port %u failed: %s",
152 spamd_address_vector[current_server]->tcp_addr,
153 spamd_address_vector[current_server]->tcp_port,
156 if (current_server >= num_servers)
158 if (current_server == start_server) {
159 log_write(0, LOG_MAIN|LOG_PANIC, "spam acl condition: all spamd servers failed");
168 /* open the local socket */
170 if ((spamd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
171 log_write(0, LOG_MAIN|LOG_PANIC,
172 "malware acl condition: spamd: unable to acquire socket (%s)",
178 server.sun_family = AF_UNIX;
179 Ustrcpy(server.sun_path, spamd_address);
181 if (connect(spamd_sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
182 log_write(0, LOG_MAIN|LOG_PANIC,
183 "malware acl condition: spamd: unable to connect to UNIX socket %s (%s)",
184 spamd_address, strerror(errno) );
192 /* now we are connected to spamd on spamd_sock */
193 snprintf(CS spamd_buffer,
194 sizeof(spamd_buffer),
195 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
199 /* send our request */
200 if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0) {
202 log_write(0, LOG_MAIN|LOG_PANIC,
203 "spam acl condition: spamd send failed: %s", strerror(errno));
209 /* now send the file */
210 /* spamd sometimes accepts conections but doesn't read data off
211 * the connection. We make the file descriptor non-blocking so
212 * that the write will only write sufficient data without blocking
213 * and we poll the desciptor to make sure that we can write without
214 * blocking. Short writes are gracefully handled and if the whole
215 * trasaction takes too long it is aborted.
217 pollfd.fd = spamd_sock;
218 pollfd.events = POLLOUT;
219 fcntl(spamd_sock, F_SETFL, O_NONBLOCK);
221 read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
225 result = poll(&pollfd, 1, 1000);
226 if (result == -1 && errno == EINTR)
228 else if (result < 1) {
230 log_write(0, LOG_MAIN|LOG_PANIC,
231 "spam acl condition: %s on spamd socket", strerror(errno));
233 if (time(NULL) - start < SPAMD_TIMEOUT)
235 log_write(0, LOG_MAIN|LOG_PANIC,
236 "spam acl condition: timed out writing spamd socket");
242 wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0);
243 if (offset + wrote != read) {
249 while (!feof(mbox_file) && !ferror(mbox_file));
250 if (ferror(mbox_file)) {
251 log_write(0, LOG_MAIN|LOG_PANIC,
252 "spam acl condition: error reading spool file: %s", strerror(errno));
260 /* we're done sending, close socket for writing */
261 shutdown(spamd_sock,SHUT_WR);
263 /* read spamd response using what's left of the timeout.
265 memset(spamd_buffer, 0, sizeof(spamd_buffer));
267 while((i = ip_recv(spamd_sock,
268 spamd_buffer + offset,
269 sizeof(spamd_buffer) - offset - 1,
270 SPAMD_TIMEOUT - time(NULL) + start)) > 0 ) {
275 if((i <= 0) && (errno != 0)) {
276 log_write(0, LOG_MAIN|LOG_PANIC,
277 "spam acl condition: error reading from spamd socket: %s", strerror(errno));
285 /* dig in the spamd output and put the report in a multiline header, if requested */
286 if( sscanf(CS spamd_buffer,"SPAMD/%s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
287 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
289 /* try to fall back to pre-2.50 spamd output */
290 if( sscanf(CS spamd_buffer,"SPAMD/%s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
291 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
292 log_write(0, LOG_MAIN|LOG_PANIC,
293 "spam acl condition: cannot parse spamd output");
298 /* Create report. Since this is a multiline string,
299 we must hack it into shape first */
300 p = &spamd_buffer[spamd_report_offset];
301 q = spam_report_buffer;
314 while( (*p <= ' ') && (*p != '\0') ) {
324 /* cut off trailing leftovers */
329 spam_report = spam_report_buffer;
331 /* create spam bar */
332 spamd_score_char = spamd_score > 0 ? '+' : '-';
333 j = abs((int)(spamd_score));
336 while((i < j) && (i <= MAX_SPAM_BAR_CHARS))
337 spam_bar_buffer[i++] = spamd_score_char;
340 spam_bar_buffer[0] = '/';
343 spam_bar_buffer[i] = '\0';
344 spam_bar = spam_bar_buffer;
346 /* create "float" spam score */
347 snprintf(CS spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score);
348 spam_score = spam_score_buffer;
350 /* create "int" spam score */
351 j = (int)((spamd_score + 0.001)*10);
352 snprintf(CS spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j);
353 spam_score_int = spam_score_int_buffer;
355 /* compare threshold against score */
356 if (spamd_score >= spamd_threshold) {
357 /* spam as determined by user's threshold */
365 /* remember user name and "been here" for it */
366 Ustrcpy(prev_user_name, user_name);
370 /* always return OK, no matter what the score */