1 /* $Cambridge: exim/src/src/spam.c,v 1.11 2005/08/01 14:41:25 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 /* stop compiler warning */
50 /* find the username from the option list */
51 if ((user_name
= string_nextinlist(&list
, &sep
,
53 sizeof(user_name_buffer
))) == NULL
) {
54 /* no username given, this means no scanning should be done */
58 /* if username is "0" or "false", do not scan */
59 if ( (Ustrcmp(user_name
,"0") == 0) ||
60 (strcmpic(user_name
,US
"false") == 0) ) {
64 /* if there is an additional option, check if it is "true" */
65 if (strcmpic(list
,US
"true") == 0) {
66 /* in that case, always return true later */
70 /* if we scanned for this username last time, just return */
71 if ( spam_ok
&& ( Ustrcmp(prev_user_name
, user_name
) == 0 ) ) {
78 /* make sure the eml mbox file is spooled up */
79 mbox_file
= spool_mbox(&mbox_size
);
81 if (mbox_file
== NULL
) {
82 /* error while spooling */
83 log_write(0, LOG_MAIN
|LOG_PANIC
,
84 "spam acl condition: error while creating mbox spool file");
89 /* socket does not start with '/' -> network socket */
90 if (*spamd_address
!= '/') {
91 time_t now
= time(NULL
);
93 int current_server
= 0;
95 uschar
*address
= NULL
;
96 uschar
*spamd_address_list_ptr
= spamd_address
;
97 uschar address_buffer
[256];
98 spamd_address_container
* spamd_address_vector
[32];
100 /* Check how many spamd servers we have
101 and register their addresses */
102 while ((address
= string_nextinlist(&spamd_address_list_ptr
, &sep
,
104 sizeof(address_buffer
))) != NULL
) {
106 spamd_address_container
*this_spamd
=
107 (spamd_address_container
*)store_get(sizeof(spamd_address_container
));
109 /* grok spamd address and port */
110 if( sscanf(CS address
, "%s %u", this_spamd
->tcp_addr
, &(this_spamd
->tcp_port
)) != 2 ) {
111 log_write(0, LOG_MAIN
,
112 "spam acl condition: warning - invalid spamd address: '%s'", address
);
116 spamd_address_vector
[num_servers
] = this_spamd
;
118 if (num_servers
> 31)
122 /* check if we have at least one server */
124 log_write(0, LOG_MAIN
|LOG_PANIC
,
125 "spam acl condition: no useable spamd server addresses in spamd_address configuration option.");
126 (void)fclose(mbox_file
);
130 current_server
= start_server
= (int)now
% num_servers
;
134 debug_printf("trying server %s, port %u\n",
135 spamd_address_vector
[current_server
]->tcp_addr
,
136 spamd_address_vector
[current_server
]->tcp_port
);
138 /* contact a spamd */
139 if ( (spamd_sock
= ip_socket(SOCK_STREAM
, AF_INET
)) < 0) {
140 log_write(0, LOG_MAIN
|LOG_PANIC
,
141 "spam acl condition: error creating IP socket for spamd");
142 (void)fclose(mbox_file
);
146 if (ip_connect( spamd_sock
,
148 spamd_address_vector
[current_server
]->tcp_addr
,
149 spamd_address_vector
[current_server
]->tcp_port
,
155 log_write(0, LOG_MAIN
|LOG_PANIC
,
156 "spam acl condition: warning - spamd connection to %s, port %u failed: %s",
157 spamd_address_vector
[current_server
]->tcp_addr
,
158 spamd_address_vector
[current_server
]->tcp_port
,
161 if (current_server
>= num_servers
)
163 if (current_server
== start_server
) {
164 log_write(0, LOG_MAIN
|LOG_PANIC
, "spam acl condition: all spamd servers failed");
165 (void)fclose(mbox_file
);
166 (void)close(spamd_sock
);
173 /* open the local socket */
175 if ((spamd_sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
176 log_write(0, LOG_MAIN
|LOG_PANIC
,
177 "malware acl condition: spamd: unable to acquire socket (%s)",
179 (void)fclose(mbox_file
);
183 server
.sun_family
= AF_UNIX
;
184 Ustrcpy(server
.sun_path
, spamd_address
);
186 if (connect(spamd_sock
, (struct sockaddr
*) &server
, sizeof(struct sockaddr_un
)) < 0) {
187 log_write(0, LOG_MAIN
|LOG_PANIC
,
188 "malware acl condition: spamd: unable to connect to UNIX socket %s (%s)",
189 spamd_address
, strerror(errno
) );
190 (void)fclose(mbox_file
);
191 (void)close(spamd_sock
);
197 /* now we are connected to spamd on spamd_sock */
198 (void)string_format(spamd_buffer
,
199 sizeof(spamd_buffer
),
200 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
204 /* send our request */
205 if (send(spamd_sock
, spamd_buffer
, Ustrlen(spamd_buffer
), 0) < 0) {
206 (void)close(spamd_sock
);
207 log_write(0, LOG_MAIN
|LOG_PANIC
,
208 "spam acl condition: spamd send failed: %s", strerror(errno
));
209 (void)fclose(mbox_file
);
210 (void)close(spamd_sock
);
214 /* now send the file */
215 /* spamd sometimes accepts conections but doesn't read data off
216 * the connection. We make the file descriptor non-blocking so
217 * that the write will only write sufficient data without blocking
218 * and we poll the desciptor to make sure that we can write without
219 * blocking. Short writes are gracefully handled and if the whole
220 * trasaction takes too long it is aborted.
221 * Note: poll() is not supported in OSX 10.2.
224 pollfd
.fd
= spamd_sock
;
225 pollfd
.events
= POLLOUT
;
227 (void)fcntl(spamd_sock
, F_SETFL
, O_NONBLOCK
);
229 read
= fread(spamd_buffer
,1,sizeof(spamd_buffer
),mbox_file
);
234 result
= poll(&pollfd
, 1, 1000);
235 if (result
== -1 && errno
== EINTR
)
237 else if (result
< 1) {
239 log_write(0, LOG_MAIN
|LOG_PANIC
,
240 "spam acl condition: %s on spamd socket", strerror(errno
));
242 if (time(NULL
) - start
< SPAMD_TIMEOUT
)
244 log_write(0, LOG_MAIN
|LOG_PANIC
,
245 "spam acl condition: timed out writing spamd socket");
247 (void)close(spamd_sock
);
248 (void)fclose(mbox_file
);
252 wrote
= send(spamd_sock
,spamd_buffer
+ offset
,read
- offset
,0);
255 log_write(0, LOG_MAIN
|LOG_PANIC
,
256 "spam acl condition: %s on spamd socket", strerror(errno
));
257 (void)close(spamd_sock
);
258 (void)fclose(mbox_file
);
261 if (offset
+ wrote
!= read
) {
267 while (!feof(mbox_file
) && !ferror(mbox_file
));
268 if (ferror(mbox_file
)) {
269 log_write(0, LOG_MAIN
|LOG_PANIC
,
270 "spam acl condition: error reading spool file: %s", strerror(errno
));
271 (void)close(spamd_sock
);
272 (void)fclose(mbox_file
);
276 (void)fclose(mbox_file
);
278 /* we're done sending, close socket for writing */
279 shutdown(spamd_sock
,SHUT_WR
);
281 /* read spamd response using what's left of the timeout.
283 memset(spamd_buffer
, 0, sizeof(spamd_buffer
));
285 while((i
= ip_recv(spamd_sock
,
286 spamd_buffer
+ offset
,
287 sizeof(spamd_buffer
) - offset
- 1,
288 SPAMD_TIMEOUT
- time(NULL
) + start
)) > 0 ) {
293 if((i
<= 0) && (errno
!= 0)) {
294 log_write(0, LOG_MAIN
|LOG_PANIC
,
295 "spam acl condition: error reading from spamd socket: %s", strerror(errno
));
296 (void)close(spamd_sock
);
301 (void)close(spamd_sock
);
303 /* dig in the spamd output and put the report in a multiline header, if requested */
304 if( sscanf(CS spamd_buffer
,"SPAMD/%s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
305 spamd_version
,&spamd_score
,&spamd_threshold
,&spamd_report_offset
) != 3 ) {
307 /* try to fall back to pre-2.50 spamd output */
308 if( sscanf(CS spamd_buffer
,"SPAMD/%s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
309 spamd_version
,&spamd_score
,&spamd_threshold
,&spamd_report_offset
) != 3 ) {
310 log_write(0, LOG_MAIN
|LOG_PANIC
,
311 "spam acl condition: cannot parse spamd output");
316 /* Create report. Since this is a multiline string,
317 we must hack it into shape first */
318 p
= &spamd_buffer
[spamd_report_offset
];
319 q
= spam_report_buffer
;
332 while( (*p
<= ' ') && (*p
!= '\0') ) {
342 /* cut off trailing leftovers */
347 spam_report
= spam_report_buffer
;
349 /* create spam bar */
350 spamd_score_char
= spamd_score
> 0 ? '+' : '-';
351 j
= abs((int)(spamd_score
));
354 while((i
< j
) && (i
<= MAX_SPAM_BAR_CHARS
))
355 spam_bar_buffer
[i
++] = spamd_score_char
;
358 spam_bar_buffer
[0] = '/';
361 spam_bar_buffer
[i
] = '\0';
362 spam_bar
= spam_bar_buffer
;
364 /* create "float" spam score */
365 (void)string_format(spam_score_buffer
, sizeof(spam_score_buffer
),"%.1f", spamd_score
);
366 spam_score
= spam_score_buffer
;
368 /* create "int" spam score */
369 j
= (int)((spamd_score
+ 0.001)*10);
370 (void)string_format(spam_score_int_buffer
, sizeof(spam_score_int_buffer
), "%d", j
);
371 spam_score_int
= spam_score_int_buffer
;
373 /* compare threshold against score */
374 if (spamd_score
>= spamd_threshold
) {
375 /* spam as determined by user's threshold */
383 /* remember user name and "been here" for it */
384 Ustrcpy(prev_user_name
, user_name
);
388 /* always return OK, no matter what the score */