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 char * 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)
104 /* make sure the eml mbox file is spooled up */
105 mbox_file
= spool_mbox(&mbox_size
, NULL
);
107 if (mbox_file
== NULL
)
109 /* error while spooling */
110 log_write(0, LOG_MAIN
|LOG_PANIC
,
111 "%s error while creating mbox spool file", loglabel
);
117 /* socket does not start with '/' -> network socket */
118 if (*spamd_address_work
!= '/')
122 uschar
*address
= NULL
;
123 uschar
*spamd_address_list_ptr
= spamd_address_work
;
124 uschar address_buffer
[256];
125 spamd_address_container
* spamd_address_vector
[32];
127 /* Check how many spamd servers we have
128 and register their addresses */
129 while ((address
= string_nextinlist(&spamd_address_list_ptr
, &sep
,
131 sizeof(address_buffer
))) != NULL
)
134 /* Potential memory leak as we never free the store. */
135 spamd_address_container
*this_spamd
=
136 (spamd_address_container
*)store_get(sizeof(spamd_address_container
));
138 /* grok spamd address and port */
139 if (sscanf(CS address
, "%s %u", this_spamd
->tcp_addr
, &(this_spamd
->tcp_port
)) != 2)
141 log_write(0, LOG_MAIN
,
142 "%s warning - invalid spamd address: '%s'", loglabel
, address
);
146 spamd_address_vector
[num_servers
] = this_spamd
;
148 >= sizeof(spamd_address_vector
)/sizeof(spamd_address_vector
[0]))
152 /* check if we have at least one server */
155 log_write(0, LOG_MAIN
|LOG_PANIC
,
156 "%s no useable spamd server addresses in spamd_address configuration option.",
158 (void)fclose(mbox_file
);
162 while (num_servers
> 0)
166 /* Randomly pick a server to try */
167 current_server
= random_number(num_servers
);
169 debug_printf("trying server %s, port %u\n",
170 spamd_address_vector
[current_server
]->tcp_addr
,
171 spamd_address_vector
[current_server
]->tcp_port
);
173 /* contact a spamd */
174 if ((spamd_sock
= ip_socket(SOCK_STREAM
, AF_INET
)) < 0)
176 log_write(0, LOG_MAIN
|LOG_PANIC
,
177 "%s error creating IP socket for spamd", loglabel
);
178 (void)fclose(mbox_file
);
182 if (ip_connect(spamd_sock
,
184 spamd_address_vector
[current_server
]->tcp_addr
,
185 spamd_address_vector
[current_server
]->tcp_port
,
190 log_write(0, LOG_MAIN
|LOG_PANIC
,
191 "%s warning - spamd connection to %s, port %u failed: %s",
193 spamd_address_vector
[current_server
]->tcp_addr
,
194 spamd_address_vector
[current_server
]->tcp_port
,
197 (void)close(spamd_sock
);
199 /* Remove the server from the list. XXX We should free the memory */
201 for (i
= current_server
; i
< num_servers
; i
++)
202 spamd_address_vector
[i
] = spamd_address_vector
[i
+1];
205 if (num_servers
== 0)
207 log_write(0, LOG_MAIN
|LOG_PANIC
, "%s all spamd servers failed", loglabel
);
208 (void)fclose(mbox_file
);
214 /* open the local socket */
216 if ((spamd_sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
218 log_write(0, LOG_MAIN
|LOG_PANIC
,
219 "%s spamd: unable to acquire socket (%s)",
222 (void)fclose(mbox_file
);
226 server
.sun_family
= AF_UNIX
;
227 Ustrcpy(server
.sun_path
, spamd_address_work
);
229 if (connect(spamd_sock
, (struct sockaddr
*) &server
, sizeof(struct sockaddr_un
)) < 0)
231 log_write(0, LOG_MAIN
|LOG_PANIC
,
232 "%s spamd: unable to connect to UNIX socket %s (%s)",
234 spamd_address_work
, strerror(errno
) );
235 (void)fclose(mbox_file
);
236 (void)close(spamd_sock
);
241 if (spamd_sock
== -1)
243 log_write(0, LOG_MAIN
|LOG_PANIC
,
244 "programming fault, spamd_sock unexpectedly unset");
245 (void)fclose(mbox_file
);
246 (void)close(spamd_sock
);
250 /* now we are connected to spamd on spamd_sock */
251 (void)string_format(spamd_buffer
,
252 sizeof(spamd_buffer
),
253 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
257 /* send our request */
258 if (send(spamd_sock
, spamd_buffer
, Ustrlen(spamd_buffer
), 0) < 0)
260 (void)close(spamd_sock
);
261 log_write(0, LOG_MAIN
|LOG_PANIC
,
262 "%s spamd send failed: %s", loglabel
, strerror(errno
));
263 (void)fclose(mbox_file
);
264 (void)close(spamd_sock
);
268 /* now send the file */
269 /* spamd sometimes accepts conections but doesn't read data off
270 * the connection. We make the file descriptor non-blocking so
271 * that the write will only write sufficient data without blocking
272 * and we poll the desciptor to make sure that we can write without
273 * blocking. Short writes are gracefully handled and if the whole
274 * trasaction takes too long it is aborted.
275 * Note: poll() is not supported in OSX 10.2 and is reported to be
276 * broken in more recent versions (up to 10.4).
279 pollfd
.fd
= spamd_sock
;
280 pollfd
.events
= POLLOUT
;
282 (void)fcntl(spamd_sock
, F_SETFL
, O_NONBLOCK
);
285 read
= fread(spamd_buffer
,1,sizeof(spamd_buffer
),mbox_file
);
291 result
= poll(&pollfd
, 1, 1000);
293 /* Patch posted by Erik ? for OS X and applied by PH */
295 select_tv
.tv_sec
= 1;
296 select_tv
.tv_usec
= 0;
298 FD_SET(spamd_sock
, &select_fd
);
299 result
= select(spamd_sock
+1, NULL
, &select_fd
, NULL
, &select_tv
);
301 /* End Erik's patch */
303 if (result
== -1 && errno
== EINTR
)
308 log_write(0, LOG_MAIN
|LOG_PANIC
,
309 "%s %s on spamd socket", loglabel
, strerror(errno
));
312 if (time(NULL
) - start
< SPAMD_TIMEOUT
)
314 log_write(0, LOG_MAIN
|LOG_PANIC
,
315 "%s timed out writing spamd socket", loglabel
);
317 (void)close(spamd_sock
);
318 (void)fclose(mbox_file
);
322 wrote
= send(spamd_sock
,spamd_buffer
+ offset
,read
- offset
,0);
325 log_write(0, LOG_MAIN
|LOG_PANIC
,
326 "%s %s on spamd socket", loglabel
, strerror(errno
));
327 (void)close(spamd_sock
);
328 (void)fclose(mbox_file
);
331 if (offset
+ wrote
!= read
)
338 while (!feof(mbox_file
) && !ferror(mbox_file
));
340 if (ferror(mbox_file
))
342 log_write(0, LOG_MAIN
|LOG_PANIC
,
343 "%s error reading spool file: %s", loglabel
, strerror(errno
));
344 (void)close(spamd_sock
);
345 (void)fclose(mbox_file
);
349 (void)fclose(mbox_file
);
351 /* we're done sending, close socket for writing */
352 shutdown(spamd_sock
,SHUT_WR
);
354 /* read spamd response using what's left of the timeout.
356 memset(spamd_buffer
, 0, sizeof(spamd_buffer
));
358 while ((i
= ip_recv(spamd_sock
,
359 spamd_buffer
+ offset
,
360 sizeof(spamd_buffer
) - offset
- 1,
361 SPAMD_TIMEOUT
- time(NULL
) + start
)) > 0 )
365 if (i
<= 0 && errno
!= 0)
367 log_write(0, LOG_MAIN
|LOG_PANIC
,
368 "%s error reading from spamd socket: %s", loglabel
, strerror(errno
));
369 (void)close(spamd_sock
);
374 (void)close(spamd_sock
);
376 /* dig in the spamd output and put the report in a multiline header, if requested */
377 if (sscanf(CS spamd_buffer
,
378 "SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
379 spamd_version
, &spamd_score
, &spamd_threshold
,
380 &spamd_report_offset
) != 3)
383 /* try to fall back to pre-2.50 spamd output */
384 if (sscanf(CS spamd_buffer
,
385 "SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
386 spamd_version
, &spamd_score
, &spamd_threshold
,
387 &spamd_report_offset
) != 3 )
389 log_write(0, LOG_MAIN
|LOG_PANIC
,
390 "%s cannot parse spamd output", loglabel
);
395 /* Create report. Since this is a multiline string,
396 we must hack it into shape first */
397 p
= &spamd_buffer
[spamd_report_offset
];
398 q
= spam_report_buffer
;
410 /* add an extra space after the newline to ensure
411 that it is treated as a header continuation line */
417 /* cut off trailing leftovers */
421 spam_report
= spam_report_buffer
;
423 /* create spam bar */
424 spamd_score_char
= spamd_score
> 0 ? '+' : '-';
425 j
= abs((int)(spamd_score
));
428 while ((i
< j
) && (i
<= MAX_SPAM_BAR_CHARS
))
429 spam_bar_buffer
[i
++] = spamd_score_char
;
432 spam_bar_buffer
[0] = '/';
435 spam_bar_buffer
[i
] = '\0';
436 spam_bar
= spam_bar_buffer
;
438 /* create "float" spam score */
439 (void)string_format(spam_score_buffer
, sizeof(spam_score_buffer
),"%.1f", spamd_score
);
440 spam_score
= spam_score_buffer
;
442 /* create "int" spam score */
443 j
= (int)((spamd_score
+ 0.001)*10);
444 (void)string_format(spam_score_int_buffer
, sizeof(spam_score_int_buffer
), "%d", j
);
445 spam_score_int
= spam_score_int_buffer
;
447 /* compare threshold against score */
448 if (spamd_score
>= spamd_threshold
)
450 /* spam as determined by user's threshold */
459 /* remember expanded spamd_address if needed */
460 if (spamd_address_work
!= spamd_address
)
461 prev_spamd_address_work
= string_copy(spamd_address_work
);
463 /* remember user name and "been here" for it */
464 Ustrcpy(prev_user_name
, user_name
);
467 if (override
) /* always return OK, no matter what the score */