Commit | Line | Data |
---|---|---|
8523533c TK |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
5 | /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */ | |
6 | /* License: GPL */ | |
7 | ||
8 | /* Code for calling spamassassin's spamd. Called from acl.c. */ | |
9 | ||
10 | #include "exim.h" | |
11 | #ifdef WITH_CONTENT_SCAN | |
12 | #include "spam.h" | |
13 | ||
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] = ""; | |
19 | int spam_ok = 0; | |
20 | int spam_rc = 0; | |
f7274286 | 21 | uschar *prev_spamd_address_work = NULL; |
8523533c | 22 | |
c007c974 JH |
23 | int |
24 | spam(uschar **listptr) | |
25 | { | |
8523533c TK |
26 | int sep = 0; |
27 | uschar *list = *listptr; | |
28 | uschar *user_name; | |
29 | uschar user_name_buffer[128]; | |
f7b63901 | 30 | unsigned long mbox_size; |
8523533c | 31 | FILE *mbox_file; |
0f501486 | 32 | int spamd_sock = -1; |
8523533c | 33 | uschar spamd_buffer[32600]; |
cfe75fc3 | 34 | int i, j, offset, result; |
8523533c TK |
35 | uschar spamd_version[8]; |
36 | uschar spamd_score_char; | |
37 | double spamd_threshold, spamd_score; | |
38 | int spamd_report_offset; | |
39 | uschar *p,*q; | |
40 | int override = 0; | |
cfe75fc3 PH |
41 | time_t start; |
42 | size_t read, wrote; | |
8523533c | 43 | struct sockaddr_un server; |
f452e07e | 44 | #ifndef NO_POLL_H |
cfe75fc3 | 45 | struct pollfd pollfd; |
25257489 PH |
46 | #else /* Patch posted by Erik ? for OS X */ |
47 | struct timeval select_tv; /* and applied by PH */ | |
48 | fd_set select_fd; | |
f452e07e | 49 | #endif |
b6e6e716 | 50 | uschar *spamd_address_work; |
8523533c | 51 | |
5614ee86 | 52 | /* stop compiler warning */ |
91ecef39 | 53 | result = 0; |
5614ee86 | 54 | |
8523533c TK |
55 | /* find the username from the option list */ |
56 | if ((user_name = string_nextinlist(&list, &sep, | |
57 | user_name_buffer, | |
58 | sizeof(user_name_buffer))) == NULL) { | |
59 | /* no username given, this means no scanning should be done */ | |
60 | return FAIL; | |
61 | }; | |
62 | ||
63 | /* if username is "0" or "false", do not scan */ | |
64 | if ( (Ustrcmp(user_name,"0") == 0) || | |
65 | (strcmpic(user_name,US"false") == 0) ) { | |
66 | return FAIL; | |
67 | }; | |
68 | ||
69 | /* if there is an additional option, check if it is "true" */ | |
70 | if (strcmpic(list,US"true") == 0) { | |
71 | /* in that case, always return true later */ | |
72 | override = 1; | |
73 | }; | |
74 | ||
f7274286 PP |
75 | /* expand spamd_address if needed */ |
76 | if (*spamd_address == '$') { | |
77 | spamd_address_work = expand_string(spamd_address); | |
78 | if (spamd_address_work == NULL) { | |
79 | log_write(0, LOG_MAIN|LOG_PANIC, | |
80 | "spamassassin acl condition: spamd_address starts with $, but expansion failed: %s", expand_string_message); | |
81 | return DEFER; | |
82 | } | |
83 | } | |
84 | else | |
85 | spamd_address_work = spamd_address; | |
86 | ||
87 | /* check if previous spamd_address was expanded and has changed. dump cached results if so */ | |
88 | if ( spam_ok && ( prev_spamd_address_work != NULL) && (Ustrcmp(prev_spamd_address_work, spamd_address_work) != 0)) { | |
89 | spam_ok = 0; | |
90 | } | |
91 | ||
8e669ac1 | 92 | /* if we scanned for this username last time, just return */ |
8523533c TK |
93 | if ( spam_ok && ( Ustrcmp(prev_user_name, user_name) == 0 ) ) { |
94 | if (override) | |
95 | return OK; | |
96 | else | |
97 | return spam_rc; | |
98 | }; | |
8e669ac1 | 99 | |
8523533c | 100 | /* make sure the eml mbox file is spooled up */ |
8544e77a | 101 | mbox_file = spool_mbox(&mbox_size, NULL); |
8e669ac1 | 102 | |
8523533c TK |
103 | if (mbox_file == NULL) { |
104 | /* error while spooling */ | |
105 | log_write(0, LOG_MAIN|LOG_PANIC, | |
106 | "spam acl condition: error while creating mbox spool file"); | |
107 | return DEFER; | |
108 | }; | |
109 | ||
cfe75fc3 | 110 | start = time(NULL); |
b6e6e716 | 111 | |
8523533c | 112 | /* socket does not start with '/' -> network socket */ |
b6e6e716 | 113 | if (*spamd_address_work != '/') { |
8523533c | 114 | int num_servers = 0; |
29cfeb94 | 115 | int current_server; |
8523533c | 116 | uschar *address = NULL; |
e1e7cfcb | 117 | uschar *spamd_address_list_ptr = spamd_address_work; |
8523533c TK |
118 | uschar address_buffer[256]; |
119 | spamd_address_container * spamd_address_vector[32]; | |
120 | ||
121 | /* Check how many spamd servers we have | |
122 | and register their addresses */ | |
123 | while ((address = string_nextinlist(&spamd_address_list_ptr, &sep, | |
124 | address_buffer, | |
125 | sizeof(address_buffer))) != NULL) { | |
8e669ac1 | 126 | |
29cfeb94 | 127 | /* Potential memory leak as we never free the store. */ |
8523533c TK |
128 | spamd_address_container *this_spamd = |
129 | (spamd_address_container *)store_get(sizeof(spamd_address_container)); | |
8e669ac1 | 130 | |
8523533c | 131 | /* grok spamd address and port */ |
e252eb8c JH |
132 | if (sscanf(CS address, "%23s %u", this_spamd->tcp_addr, &(this_spamd->tcp_port)) != 2) |
133 | { | |
8523533c TK |
134 | log_write(0, LOG_MAIN, |
135 | "spam acl condition: warning - invalid spamd address: '%s'", address); | |
136 | continue; | |
137 | }; | |
8e669ac1 | 138 | |
8523533c TK |
139 | spamd_address_vector[num_servers] = this_spamd; |
140 | num_servers++; | |
141 | if (num_servers > 31) | |
142 | break; | |
143 | }; | |
8e669ac1 | 144 | |
8523533c TK |
145 | /* check if we have at least one server */ |
146 | if (!num_servers) { | |
147 | log_write(0, LOG_MAIN|LOG_PANIC, | |
148 | "spam acl condition: no useable spamd server addresses in spamd_address configuration option."); | |
f1e894f3 | 149 | (void)fclose(mbox_file); |
8523533c TK |
150 | return DEFER; |
151 | }; | |
152 | ||
29cfeb94 | 153 | while ( num_servers > 0 ) { |
2e64baa9 | 154 | int i; |
8523533c | 155 | |
29cfeb94 PP |
156 | /* Randomly pick a server to try */ |
157 | current_server = random_number( num_servers ); | |
8e669ac1 | 158 | |
8523533c TK |
159 | debug_printf("trying server %s, port %u\n", |
160 | spamd_address_vector[current_server]->tcp_addr, | |
161 | spamd_address_vector[current_server]->tcp_port); | |
8e669ac1 | 162 | |
8523533c TK |
163 | /* contact a spamd */ |
164 | if ( (spamd_sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) { | |
165 | log_write(0, LOG_MAIN|LOG_PANIC, | |
166 | "spam acl condition: error creating IP socket for spamd"); | |
f1e894f3 | 167 | (void)fclose(mbox_file); |
8e669ac1 | 168 | return DEFER; |
8523533c | 169 | }; |
8e669ac1 | 170 | |
8523533c TK |
171 | if (ip_connect( spamd_sock, |
172 | AF_INET, | |
173 | spamd_address_vector[current_server]->tcp_addr, | |
174 | spamd_address_vector[current_server]->tcp_port, | |
175 | 5 ) > -1) { | |
176 | /* connection OK */ | |
177 | break; | |
178 | }; | |
8e669ac1 | 179 | |
8523533c TK |
180 | log_write(0, LOG_MAIN|LOG_PANIC, |
181 | "spam acl condition: warning - spamd connection to %s, port %u failed: %s", | |
182 | spamd_address_vector[current_server]->tcp_addr, | |
183 | spamd_address_vector[current_server]->tcp_port, | |
184 | strerror(errno)); | |
29cfeb94 PP |
185 | |
186 | (void)close(spamd_sock); | |
187 | ||
188 | /* Remove the server from the list. XXX We should free the memory */ | |
189 | num_servers--; | |
29cfeb94 PP |
190 | for( i = current_server; i < num_servers; i++ ) |
191 | spamd_address_vector[i] = spamd_address_vector[i+1]; | |
192 | } | |
193 | ||
194 | if ( num_servers == 0 ) { | |
195 | log_write(0, LOG_MAIN|LOG_PANIC, "spam acl condition: all spamd servers failed"); | |
196 | (void)fclose(mbox_file); | |
197 | return DEFER; | |
198 | } | |
8523533c TK |
199 | |
200 | } | |
201 | else { | |
202 | /* open the local socket */ | |
203 | ||
204 | if ((spamd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { | |
205 | log_write(0, LOG_MAIN|LOG_PANIC, | |
206 | "malware acl condition: spamd: unable to acquire socket (%s)", | |
207 | strerror(errno)); | |
f1e894f3 | 208 | (void)fclose(mbox_file); |
8523533c TK |
209 | return DEFER; |
210 | } | |
211 | ||
212 | server.sun_family = AF_UNIX; | |
b6e6e716 | 213 | Ustrcpy(server.sun_path, spamd_address_work); |
8523533c TK |
214 | |
215 | if (connect(spamd_sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) { | |
216 | log_write(0, LOG_MAIN|LOG_PANIC, | |
217 | "malware acl condition: spamd: unable to connect to UNIX socket %s (%s)", | |
b6e6e716 | 218 | spamd_address_work, strerror(errno) ); |
f1e894f3 PH |
219 | (void)fclose(mbox_file); |
220 | (void)close(spamd_sock); | |
8523533c TK |
221 | return DEFER; |
222 | } | |
223 | ||
224 | } | |
225 | ||
0f501486 PP |
226 | if (spamd_sock == -1) { |
227 | log_write(0, LOG_MAIN|LOG_PANIC, | |
228 | "programming fault, spamd_sock unexpectedly unset"); | |
229 | (void)fclose(mbox_file); | |
230 | (void)close(spamd_sock); | |
231 | return DEFER; | |
232 | } | |
233 | ||
8523533c | 234 | /* now we are connected to spamd on spamd_sock */ |
b07e6aa3 | 235 | (void)string_format(spamd_buffer, |
8523533c | 236 | sizeof(spamd_buffer), |
f7b63901 | 237 | "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n", |
8523533c TK |
238 | user_name, |
239 | mbox_size); | |
240 | ||
241 | /* send our request */ | |
242 | if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0) { | |
f1e894f3 | 243 | (void)close(spamd_sock); |
8523533c TK |
244 | log_write(0, LOG_MAIN|LOG_PANIC, |
245 | "spam acl condition: spamd send failed: %s", strerror(errno)); | |
f1e894f3 PH |
246 | (void)fclose(mbox_file); |
247 | (void)close(spamd_sock); | |
8523533c TK |
248 | return DEFER; |
249 | }; | |
250 | ||
251 | /* now send the file */ | |
cfe75fc3 PH |
252 | /* spamd sometimes accepts conections but doesn't read data off |
253 | * the connection. We make the file descriptor non-blocking so | |
254 | * that the write will only write sufficient data without blocking | |
255 | * and we poll the desciptor to make sure that we can write without | |
256 | * blocking. Short writes are gracefully handled and if the whole | |
257 | * trasaction takes too long it is aborted. | |
25257489 PH |
258 | * Note: poll() is not supported in OSX 10.2 and is reported to be |
259 | * broken in more recent versions (up to 10.4). | |
cfe75fc3 | 260 | */ |
f452e07e | 261 | #ifndef NO_POLL_H |
cfe75fc3 PH |
262 | pollfd.fd = spamd_sock; |
263 | pollfd.events = POLLOUT; | |
f452e07e | 264 | #endif |
ff790e47 | 265 | (void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK); |
8523533c | 266 | do { |
cfe75fc3 PH |
267 | read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file); |
268 | if (read > 0) { | |
269 | offset = 0; | |
270 | again: | |
f452e07e | 271 | #ifndef NO_POLL_H |
cfe75fc3 | 272 | result = poll(&pollfd, 1, 1000); |
25257489 PH |
273 | |
274 | /* Patch posted by Erik ? for OS X and applied by PH */ | |
275 | #else | |
276 | select_tv.tv_sec = 1; | |
277 | select_tv.tv_usec = 0; | |
278 | FD_ZERO(&select_fd); | |
279 | FD_SET(spamd_sock, &select_fd); | |
280 | result = select(spamd_sock+1, NULL, &select_fd, NULL, &select_tv); | |
281 | #endif | |
282 | /* End Erik's patch */ | |
283 | ||
cfe75fc3 | 284 | if (result == -1 && errno == EINTR) |
25257489 | 285 | goto again; |
cfe75fc3 PH |
286 | else if (result < 1) { |
287 | if (result == -1) | |
288 | log_write(0, LOG_MAIN|LOG_PANIC, | |
289 | "spam acl condition: %s on spamd socket", strerror(errno)); | |
290 | else { | |
291 | if (time(NULL) - start < SPAMD_TIMEOUT) | |
c007c974 | 292 | goto again; |
cfe75fc3 PH |
293 | log_write(0, LOG_MAIN|LOG_PANIC, |
294 | "spam acl condition: timed out writing spamd socket"); | |
295 | } | |
f1e894f3 PH |
296 | (void)close(spamd_sock); |
297 | (void)fclose(mbox_file); | |
8523533c | 298 | return DEFER; |
cfe75fc3 | 299 | } |
25257489 | 300 | |
cfe75fc3 | 301 | wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0); |
8d7d227d TK |
302 | if (wrote == -1) |
303 | { | |
304 | log_write(0, LOG_MAIN|LOG_PANIC, | |
305 | "spam acl condition: %s on spamd socket", strerror(errno)); | |
f1e894f3 PH |
306 | (void)close(spamd_sock); |
307 | (void)fclose(mbox_file); | |
8d7d227d TK |
308 | return DEFER; |
309 | } | |
cfe75fc3 PH |
310 | if (offset + wrote != read) { |
311 | offset += wrote; | |
312 | goto again; | |
313 | } | |
314 | } | |
315 | } | |
316 | while (!feof(mbox_file) && !ferror(mbox_file)); | |
317 | if (ferror(mbox_file)) { | |
318 | log_write(0, LOG_MAIN|LOG_PANIC, | |
319 | "spam acl condition: error reading spool file: %s", strerror(errno)); | |
f1e894f3 PH |
320 | (void)close(spamd_sock); |
321 | (void)fclose(mbox_file); | |
cfe75fc3 | 322 | return DEFER; |
8523533c | 323 | } |
8523533c | 324 | |
f1e894f3 | 325 | (void)fclose(mbox_file); |
8523533c TK |
326 | |
327 | /* we're done sending, close socket for writing */ | |
328 | shutdown(spamd_sock,SHUT_WR); | |
8e669ac1 | 329 | |
cfe75fc3 PH |
330 | /* read spamd response using what's left of the timeout. |
331 | */ | |
8523533c TK |
332 | memset(spamd_buffer, 0, sizeof(spamd_buffer)); |
333 | offset = 0; | |
334 | while((i = ip_recv(spamd_sock, | |
335 | spamd_buffer + offset, | |
336 | sizeof(spamd_buffer) - offset - 1, | |
cfe75fc3 | 337 | SPAMD_TIMEOUT - time(NULL) + start)) > 0 ) { |
8523533c TK |
338 | offset += i; |
339 | } | |
340 | ||
341 | /* error handling */ | |
342 | if((i <= 0) && (errno != 0)) { | |
343 | log_write(0, LOG_MAIN|LOG_PANIC, | |
344 | "spam acl condition: error reading from spamd socket: %s", strerror(errno)); | |
f1e894f3 | 345 | (void)close(spamd_sock); |
8523533c TK |
346 | return DEFER; |
347 | } | |
348 | ||
349 | /* reading done */ | |
f1e894f3 | 350 | (void)close(spamd_sock); |
8523533c TK |
351 | |
352 | /* dig in the spamd output and put the report in a multiline header, if requested */ | |
0806a9c5 | 353 | if( sscanf(CS spamd_buffer,"SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n", |
8523533c | 354 | spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) { |
8e669ac1 | 355 | |
8523533c | 356 | /* try to fall back to pre-2.50 spamd output */ |
0806a9c5 | 357 | if( sscanf(CS spamd_buffer,"SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n", |
8523533c TK |
358 | spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) { |
359 | log_write(0, LOG_MAIN|LOG_PANIC, | |
360 | "spam acl condition: cannot parse spamd output"); | |
361 | return DEFER; | |
362 | }; | |
363 | }; | |
364 | ||
365 | /* Create report. Since this is a multiline string, | |
366 | we must hack it into shape first */ | |
367 | p = &spamd_buffer[spamd_report_offset]; | |
368 | q = spam_report_buffer; | |
369 | while (*p != '\0') { | |
370 | /* skip \r */ | |
371 | if (*p == '\r') { | |
372 | p++; | |
373 | continue; | |
374 | }; | |
375 | *q = *p; | |
376 | q++; | |
377 | if (*p == '\n') { | |
89dec7b6 TF |
378 | /* add an extra space after the newline to ensure |
379 | that it is treated as a header continuation line */ | |
380 | *q = ' '; | |
8523533c | 381 | q++; |
8523533c TK |
382 | }; |
383 | p++; | |
384 | }; | |
385 | /* NULL-terminate */ | |
386 | *q = '\0'; | |
387 | q--; | |
388 | /* cut off trailing leftovers */ | |
389 | while (*q <= ' ') { | |
390 | *q = '\0'; | |
391 | q--; | |
392 | }; | |
393 | spam_report = spam_report_buffer; | |
394 | ||
395 | /* create spam bar */ | |
396 | spamd_score_char = spamd_score > 0 ? '+' : '-'; | |
397 | j = abs((int)(spamd_score)); | |
398 | i = 0; | |
399 | if( j != 0 ) { | |
400 | while((i < j) && (i <= MAX_SPAM_BAR_CHARS)) | |
401 | spam_bar_buffer[i++] = spamd_score_char; | |
402 | } | |
403 | else{ | |
404 | spam_bar_buffer[0] = '/'; | |
405 | i = 1; | |
406 | } | |
407 | spam_bar_buffer[i] = '\0'; | |
408 | spam_bar = spam_bar_buffer; | |
409 | ||
410 | /* create "float" spam score */ | |
b07e6aa3 | 411 | (void)string_format(spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score); |
8523533c TK |
412 | spam_score = spam_score_buffer; |
413 | ||
414 | /* create "int" spam score */ | |
415 | j = (int)((spamd_score + 0.001)*10); | |
b07e6aa3 | 416 | (void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j); |
8523533c TK |
417 | spam_score_int = spam_score_int_buffer; |
418 | ||
419 | /* compare threshold against score */ | |
420 | if (spamd_score >= spamd_threshold) { | |
421 | /* spam as determined by user's threshold */ | |
422 | spam_rc = OK; | |
423 | } | |
424 | else { | |
425 | /* not spam */ | |
426 | spam_rc = FAIL; | |
427 | }; | |
8e669ac1 | 428 | |
f7274286 PP |
429 | /* remember expanded spamd_address if needed */ |
430 | if (spamd_address_work != spamd_address) { | |
431 | prev_spamd_address_work = string_copy(spamd_address_work); | |
b6e6e716 | 432 | } |
f7274286 PP |
433 | /* remember user name and "been here" for it */ |
434 | Ustrcpy(prev_user_name, user_name); | |
435 | spam_ok = 1; | |
8e669ac1 | 436 | |
8523533c TK |
437 | if (override) { |
438 | /* always return OK, no matter what the score */ | |
439 | return OK; | |
440 | } | |
441 | else { | |
442 | return spam_rc; | |
443 | }; | |
444 | } | |
445 | ||
446 | #endif |