1 /* $Cambridge: exim/src/src/spam.c,v 1.13 2006/09/05 14:05:43 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 #else /* Patch posted by Erik ? for OS X */
46 struct timeval select_tv; /* and applied by PH */
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 /* if we scanned for this username last time, just return */
74 if ( spam_ok && ( Ustrcmp(prev_user_name, user_name) == 0 ) ) {
81 /* make sure the eml mbox file is spooled up */
82 mbox_file = spool_mbox(&mbox_size);
84 if (mbox_file == NULL) {
85 /* error while spooling */
86 log_write(0, LOG_MAIN|LOG_PANIC,
87 "spam acl condition: error while creating mbox spool file");
92 /* socket does not start with '/' -> network socket */
93 if (*spamd_address != '/') {
94 time_t now = time(NULL);
96 int current_server = 0;
98 uschar *address = NULL;
99 uschar *spamd_address_list_ptr = spamd_address;
100 uschar address_buffer[256];
101 spamd_address_container * spamd_address_vector[32];
103 /* Check how many spamd servers we have
104 and register their addresses */
105 while ((address = string_nextinlist(&spamd_address_list_ptr, &sep,
107 sizeof(address_buffer))) != NULL) {
109 spamd_address_container *this_spamd =
110 (spamd_address_container *)store_get(sizeof(spamd_address_container));
112 /* grok spamd address and port */
113 if( sscanf(CS address, "%s %u", this_spamd->tcp_addr, &(this_spamd->tcp_port)) != 2 ) {
114 log_write(0, LOG_MAIN,
115 "spam acl condition: warning - invalid spamd address: '%s'", address);
119 spamd_address_vector[num_servers] = this_spamd;
121 if (num_servers > 31)
125 /* check if we have at least one server */
127 log_write(0, LOG_MAIN|LOG_PANIC,
128 "spam acl condition: no useable spamd server addresses in spamd_address configuration option.");
129 (void)fclose(mbox_file);
133 current_server = start_server = (int)now % num_servers;
137 debug_printf("trying server %s, port %u\n",
138 spamd_address_vector[current_server]->tcp_addr,
139 spamd_address_vector[current_server]->tcp_port);
141 /* contact a spamd */
142 if ( (spamd_sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
143 log_write(0, LOG_MAIN|LOG_PANIC,
144 "spam acl condition: error creating IP socket for spamd");
145 (void)fclose(mbox_file);
149 if (ip_connect( spamd_sock,
151 spamd_address_vector[current_server]->tcp_addr,
152 spamd_address_vector[current_server]->tcp_port,
158 log_write(0, LOG_MAIN|LOG_PANIC,
159 "spam acl condition: warning - spamd connection to %s, port %u failed: %s",
160 spamd_address_vector[current_server]->tcp_addr,
161 spamd_address_vector[current_server]->tcp_port,
164 if (current_server >= num_servers)
166 if (current_server == start_server) {
167 log_write(0, LOG_MAIN|LOG_PANIC, "spam acl condition: all spamd servers failed");
168 (void)fclose(mbox_file);
169 (void)close(spamd_sock);
176 /* open the local socket */
178 if ((spamd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
179 log_write(0, LOG_MAIN|LOG_PANIC,
180 "malware acl condition: spamd: unable to acquire socket (%s)",
182 (void)fclose(mbox_file);
186 server.sun_family = AF_UNIX;
187 Ustrcpy(server.sun_path, spamd_address);
189 if (connect(spamd_sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
190 log_write(0, LOG_MAIN|LOG_PANIC,
191 "malware acl condition: spamd: unable to connect to UNIX socket %s (%s)",
192 spamd_address, strerror(errno) );
193 (void)fclose(mbox_file);
194 (void)close(spamd_sock);
200 /* now we are connected to spamd on spamd_sock */
201 (void)string_format(spamd_buffer,
202 sizeof(spamd_buffer),
203 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
207 /* send our request */
208 if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0) {
209 (void)close(spamd_sock);
210 log_write(0, LOG_MAIN|LOG_PANIC,
211 "spam acl condition: spamd send failed: %s", strerror(errno));
212 (void)fclose(mbox_file);
213 (void)close(spamd_sock);
217 /* now send the file */
218 /* spamd sometimes accepts conections but doesn't read data off
219 * the connection. We make the file descriptor non-blocking so
220 * that the write will only write sufficient data without blocking
221 * and we poll the desciptor to make sure that we can write without
222 * blocking. Short writes are gracefully handled and if the whole
223 * trasaction takes too long it is aborted.
224 * Note: poll() is not supported in OSX 10.2 and is reported to be
225 * broken in more recent versions (up to 10.4).
228 pollfd.fd = spamd_sock;
229 pollfd.events = POLLOUT;
231 (void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK);
233 read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
238 result = poll(&pollfd, 1, 1000);
240 /* Patch posted by Erik ? for OS X and applied by PH */
242 select_tv.tv_sec = 1;
243 select_tv.tv_usec = 0;
245 FD_SET(spamd_sock, &select_fd);
246 result = select(spamd_sock+1, NULL, &select_fd, NULL, &select_tv);
248 /* End Erik's patch */
250 if (result == -1 && errno == EINTR)
252 else if (result < 1) {
254 log_write(0, LOG_MAIN|LOG_PANIC,
255 "spam acl condition: %s on spamd socket", strerror(errno));
257 if (time(NULL) - start < SPAMD_TIMEOUT)
259 log_write(0, LOG_MAIN|LOG_PANIC,
260 "spam acl condition: timed out writing spamd socket");
262 (void)close(spamd_sock);
263 (void)fclose(mbox_file);
267 wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0);
270 log_write(0, LOG_MAIN|LOG_PANIC,
271 "spam acl condition: %s on spamd socket", strerror(errno));
272 (void)close(spamd_sock);
273 (void)fclose(mbox_file);
276 if (offset + wrote != read) {
282 while (!feof(mbox_file) && !ferror(mbox_file));
283 if (ferror(mbox_file)) {
284 log_write(0, LOG_MAIN|LOG_PANIC,
285 "spam acl condition: error reading spool file: %s", strerror(errno));
286 (void)close(spamd_sock);
287 (void)fclose(mbox_file);
291 (void)fclose(mbox_file);
293 /* we're done sending, close socket for writing */
294 shutdown(spamd_sock,SHUT_WR);
296 /* read spamd response using what's left of the timeout.
298 memset(spamd_buffer, 0, sizeof(spamd_buffer));
300 while((i = ip_recv(spamd_sock,
301 spamd_buffer + offset,
302 sizeof(spamd_buffer) - offset - 1,
303 SPAMD_TIMEOUT - time(NULL) + start)) > 0 ) {
308 if((i <= 0) && (errno != 0)) {
309 log_write(0, LOG_MAIN|LOG_PANIC,
310 "spam acl condition: error reading from spamd socket: %s", strerror(errno));
311 (void)close(spamd_sock);
316 (void)close(spamd_sock);
318 /* dig in the spamd output and put the report in a multiline header, if requested */
319 if( sscanf(CS spamd_buffer,"SPAMD/%s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
320 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
322 /* try to fall back to pre-2.50 spamd output */
323 if( sscanf(CS spamd_buffer,"SPAMD/%s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
324 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
325 log_write(0, LOG_MAIN|LOG_PANIC,
326 "spam acl condition: cannot parse spamd output");
331 /* Create report. Since this is a multiline string,
332 we must hack it into shape first */
333 p = &spamd_buffer[spamd_report_offset];
334 q = spam_report_buffer;
347 while( (*p <= ' ') && (*p != '\0') ) {
357 /* cut off trailing leftovers */
362 spam_report = spam_report_buffer;
364 /* create spam bar */
365 spamd_score_char = spamd_score > 0 ? '+' : '-';
366 j = abs((int)(spamd_score));
369 while((i < j) && (i <= MAX_SPAM_BAR_CHARS))
370 spam_bar_buffer[i++] = spamd_score_char;
373 spam_bar_buffer[0] = '/';
376 spam_bar_buffer[i] = '\0';
377 spam_bar = spam_bar_buffer;
379 /* create "float" spam score */
380 (void)string_format(spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score);
381 spam_score = spam_score_buffer;
383 /* create "int" spam score */
384 j = (int)((spamd_score + 0.001)*10);
385 (void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j);
386 spam_score_int = spam_score_int_buffer;
388 /* compare threshold against score */
389 if (spamd_score >= spamd_threshold) {
390 /* spam as determined by user's threshold */
398 /* remember user name and "been here" for it */
399 Ustrcpy(prev_user_name, user_name);
403 /* always return OK, no matter what the score */