1 /* $Cambridge: exim/src/src/spam.c,v 1.8 2005/06/22 15:44:38 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;
47 /* find the username from the option list */
48 if ((user_name = string_nextinlist(&list, &sep,
50 sizeof(user_name_buffer))) == NULL) {
51 /* no username given, this means no scanning should be done */
55 /* if username is "0" or "false", do not scan */
56 if ( (Ustrcmp(user_name,"0") == 0) ||
57 (strcmpic(user_name,US"false") == 0) ) {
61 /* if there is an additional option, check if it is "true" */
62 if (strcmpic(list,US"true") == 0) {
63 /* in that case, always return true later */
67 /* if we scanned for this username last time, just return */
68 if ( spam_ok && ( Ustrcmp(prev_user_name, user_name) == 0 ) ) {
75 /* make sure the eml mbox file is spooled up */
76 mbox_file = spool_mbox(&mbox_size);
78 if (mbox_file == NULL) {
79 /* error while spooling */
80 log_write(0, LOG_MAIN|LOG_PANIC,
81 "spam acl condition: error while creating mbox spool file");
86 /* socket does not start with '/' -> network socket */
87 if (*spamd_address != '/') {
88 time_t now = time(NULL);
90 int current_server = 0;
92 uschar *address = NULL;
93 uschar *spamd_address_list_ptr = spamd_address;
94 uschar address_buffer[256];
95 spamd_address_container * spamd_address_vector[32];
97 /* Check how many spamd servers we have
98 and register their addresses */
99 while ((address = string_nextinlist(&spamd_address_list_ptr, &sep,
101 sizeof(address_buffer))) != NULL) {
103 spamd_address_container *this_spamd =
104 (spamd_address_container *)store_get(sizeof(spamd_address_container));
106 /* grok spamd address and port */
107 if( sscanf(CS address, "%s %u", this_spamd->tcp_addr, &(this_spamd->tcp_port)) != 2 ) {
108 log_write(0, LOG_MAIN,
109 "spam acl condition: warning - invalid spamd address: '%s'", address);
113 spamd_address_vector[num_servers] = this_spamd;
115 if (num_servers > 31)
119 /* check if we have at least one server */
121 log_write(0, LOG_MAIN|LOG_PANIC,
122 "spam acl condition: no useable spamd server addresses in spamd_address configuration option.");
127 current_server = start_server = (int)now % num_servers;
131 debug_printf("trying server %s, port %u\n",
132 spamd_address_vector[current_server]->tcp_addr,
133 spamd_address_vector[current_server]->tcp_port);
135 /* contact a spamd */
136 if ( (spamd_sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
137 log_write(0, LOG_MAIN|LOG_PANIC,
138 "spam acl condition: error creating IP socket for spamd");
143 if (ip_connect( spamd_sock,
145 spamd_address_vector[current_server]->tcp_addr,
146 spamd_address_vector[current_server]->tcp_port,
152 log_write(0, LOG_MAIN|LOG_PANIC,
153 "spam acl condition: warning - spamd connection to %s, port %u failed: %s",
154 spamd_address_vector[current_server]->tcp_addr,
155 spamd_address_vector[current_server]->tcp_port,
158 if (current_server >= num_servers)
160 if (current_server == start_server) {
161 log_write(0, LOG_MAIN|LOG_PANIC, "spam acl condition: all spamd servers failed");
170 /* open the local socket */
172 if ((spamd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
173 log_write(0, LOG_MAIN|LOG_PANIC,
174 "malware acl condition: spamd: unable to acquire socket (%s)",
180 server.sun_family = AF_UNIX;
181 Ustrcpy(server.sun_path, spamd_address);
183 if (connect(spamd_sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
184 log_write(0, LOG_MAIN|LOG_PANIC,
185 "malware acl condition: spamd: unable to connect to UNIX socket %s (%s)",
186 spamd_address, strerror(errno) );
194 /* now we are connected to spamd on spamd_sock */
195 snprintf(CS spamd_buffer,
196 sizeof(spamd_buffer),
197 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
201 /* send our request */
202 if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0) {
204 log_write(0, LOG_MAIN|LOG_PANIC,
205 "spam acl condition: spamd send failed: %s", strerror(errno));
211 /* now send the file */
212 /* spamd sometimes accepts conections but doesn't read data off
213 * the connection. We make the file descriptor non-blocking so
214 * that the write will only write sufficient data without blocking
215 * and we poll the desciptor to make sure that we can write without
216 * blocking. Short writes are gracefully handled and if the whole
217 * trasaction takes too long it is aborted.
218 * Note: poll() is not supported in OSX 10.2.
221 pollfd.fd = spamd_sock;
222 pollfd.events = POLLOUT;
224 (void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK);
226 read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
231 result = poll(&pollfd, 1, 1000);
232 if (result == -1 && errno == EINTR)
234 else if (result < 1) {
236 log_write(0, LOG_MAIN|LOG_PANIC,
237 "spam acl condition: %s on spamd socket", strerror(errno));
239 if (time(NULL) - start < SPAMD_TIMEOUT)
241 log_write(0, LOG_MAIN|LOG_PANIC,
242 "spam acl condition: timed out writing spamd socket");
249 wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0);
252 log_write(0, LOG_MAIN|LOG_PANIC,
253 "spam acl condition: %s on spamd socket", strerror(errno));
258 if (offset + wrote != read) {
264 while (!feof(mbox_file) && !ferror(mbox_file));
265 if (ferror(mbox_file)) {
266 log_write(0, LOG_MAIN|LOG_PANIC,
267 "spam acl condition: error reading spool file: %s", strerror(errno));
275 /* we're done sending, close socket for writing */
276 shutdown(spamd_sock,SHUT_WR);
278 /* read spamd response using what's left of the timeout.
280 memset(spamd_buffer, 0, sizeof(spamd_buffer));
282 while((i = ip_recv(spamd_sock,
283 spamd_buffer + offset,
284 sizeof(spamd_buffer) - offset - 1,
285 SPAMD_TIMEOUT - time(NULL) + start)) > 0 ) {
290 if((i <= 0) && (errno != 0)) {
291 log_write(0, LOG_MAIN|LOG_PANIC,
292 "spam acl condition: error reading from spamd socket: %s", strerror(errno));
300 /* dig in the spamd output and put the report in a multiline header, if requested */
301 if( sscanf(CS spamd_buffer,"SPAMD/%s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
302 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
304 /* try to fall back to pre-2.50 spamd output */
305 if( sscanf(CS spamd_buffer,"SPAMD/%s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
306 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
307 log_write(0, LOG_MAIN|LOG_PANIC,
308 "spam acl condition: cannot parse spamd output");
313 /* Create report. Since this is a multiline string,
314 we must hack it into shape first */
315 p = &spamd_buffer[spamd_report_offset];
316 q = spam_report_buffer;
329 while( (*p <= ' ') && (*p != '\0') ) {
339 /* cut off trailing leftovers */
344 spam_report = spam_report_buffer;
346 /* create spam bar */
347 spamd_score_char = spamd_score > 0 ? '+' : '-';
348 j = abs((int)(spamd_score));
351 while((i < j) && (i <= MAX_SPAM_BAR_CHARS))
352 spam_bar_buffer[i++] = spamd_score_char;
355 spam_bar_buffer[0] = '/';
358 spam_bar_buffer[i] = '\0';
359 spam_bar = spam_bar_buffer;
361 /* create "float" spam score */
362 snprintf(CS spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score);
363 spam_score = spam_score_buffer;
365 /* create "int" spam score */
366 j = (int)((spamd_score + 0.001)*10);
367 snprintf(CS spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j);
368 spam_score_int = spam_score_int_buffer;
370 /* compare threshold against score */
371 if (spamd_score >= spamd_threshold) {
372 /* spam as determined by user's threshold */
380 /* remember user name and "been here" for it */
381 Ustrcpy(prev_user_name, user_name);
385 /* always return OK, no matter what the score */