Commit | Line | Data |
---|---|---|
0806a9c5 | 1 | /* $Cambridge: exim/src/src/spam.c,v 1.14 2007/05/14 18:56:25 magnus Exp $ */ |
8523533c TK |
2 | |
3 | /************************************************* | |
4 | * Exim - an Internet mail transport agent * | |
5 | *************************************************/ | |
6 | ||
7 | /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */ | |
8 | /* License: GPL */ | |
9 | ||
10 | /* Code for calling spamassassin's spamd. Called from acl.c. */ | |
11 | ||
12 | #include "exim.h" | |
13 | #ifdef WITH_CONTENT_SCAN | |
14 | #include "spam.h" | |
15 | ||
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] = ""; | |
21 | int spam_ok = 0; | |
22 | int spam_rc = 0; | |
23 | ||
24 | int spam(uschar **listptr) { | |
25 | int sep = 0; | |
26 | uschar *list = *listptr; | |
27 | uschar *user_name; | |
28 | uschar user_name_buffer[128]; | |
f7b63901 | 29 | unsigned long mbox_size; |
8523533c TK |
30 | FILE *mbox_file; |
31 | int spamd_sock; | |
32 | uschar spamd_buffer[32600]; | |
cfe75fc3 | 33 | int i, j, offset, result; |
8523533c TK |
34 | uschar spamd_version[8]; |
35 | uschar spamd_score_char; | |
36 | double spamd_threshold, spamd_score; | |
37 | int spamd_report_offset; | |
38 | uschar *p,*q; | |
39 | int override = 0; | |
cfe75fc3 PH |
40 | time_t start; |
41 | size_t read, wrote; | |
8523533c | 42 | struct sockaddr_un server; |
f452e07e | 43 | #ifndef NO_POLL_H |
cfe75fc3 | 44 | struct pollfd pollfd; |
25257489 PH |
45 | #else /* Patch posted by Erik ? for OS X */ |
46 | struct timeval select_tv; /* and applied by PH */ | |
47 | fd_set select_fd; | |
f452e07e | 48 | #endif |
8523533c | 49 | |
5614ee86 | 50 | /* stop compiler warning */ |
91ecef39 | 51 | result = 0; |
5614ee86 | 52 | |
8523533c TK |
53 | /* find the username from the option list */ |
54 | if ((user_name = string_nextinlist(&list, &sep, | |
55 | user_name_buffer, | |
56 | sizeof(user_name_buffer))) == NULL) { | |
57 | /* no username given, this means no scanning should be done */ | |
58 | return FAIL; | |
59 | }; | |
60 | ||
61 | /* if username is "0" or "false", do not scan */ | |
62 | if ( (Ustrcmp(user_name,"0") == 0) || | |
63 | (strcmpic(user_name,US"false") == 0) ) { | |
64 | return FAIL; | |
65 | }; | |
66 | ||
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 */ | |
70 | override = 1; | |
71 | }; | |
72 | ||
8e669ac1 | 73 | /* if we scanned for this username last time, just return */ |
8523533c TK |
74 | if ( spam_ok && ( Ustrcmp(prev_user_name, user_name) == 0 ) ) { |
75 | if (override) | |
76 | return OK; | |
77 | else | |
78 | return spam_rc; | |
79 | }; | |
8e669ac1 | 80 | |
8523533c TK |
81 | /* make sure the eml mbox file is spooled up */ |
82 | mbox_file = spool_mbox(&mbox_size); | |
8e669ac1 | 83 | |
8523533c TK |
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"); | |
88 | return DEFER; | |
89 | }; | |
90 | ||
cfe75fc3 | 91 | start = time(NULL); |
8523533c TK |
92 | /* socket does not start with '/' -> network socket */ |
93 | if (*spamd_address != '/') { | |
94 | time_t now = time(NULL); | |
95 | int num_servers = 0; | |
96 | int current_server = 0; | |
97 | int start_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]; | |
102 | ||
103 | /* Check how many spamd servers we have | |
104 | and register their addresses */ | |
105 | while ((address = string_nextinlist(&spamd_address_list_ptr, &sep, | |
106 | address_buffer, | |
107 | sizeof(address_buffer))) != NULL) { | |
8e669ac1 | 108 | |
8523533c TK |
109 | spamd_address_container *this_spamd = |
110 | (spamd_address_container *)store_get(sizeof(spamd_address_container)); | |
8e669ac1 | 111 | |
8523533c TK |
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); | |
116 | continue; | |
117 | }; | |
8e669ac1 | 118 | |
8523533c TK |
119 | spamd_address_vector[num_servers] = this_spamd; |
120 | num_servers++; | |
121 | if (num_servers > 31) | |
122 | break; | |
123 | }; | |
8e669ac1 | 124 | |
8523533c TK |
125 | /* check if we have at least one server */ |
126 | if (!num_servers) { | |
127 | log_write(0, LOG_MAIN|LOG_PANIC, | |
128 | "spam acl condition: no useable spamd server addresses in spamd_address configuration option."); | |
f1e894f3 | 129 | (void)fclose(mbox_file); |
8523533c TK |
130 | return DEFER; |
131 | }; | |
132 | ||
133 | current_server = start_server = (int)now % num_servers; | |
134 | ||
135 | while (1) { | |
8e669ac1 | 136 | |
8523533c TK |
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); | |
8e669ac1 | 140 | |
8523533c TK |
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"); | |
f1e894f3 | 145 | (void)fclose(mbox_file); |
8e669ac1 | 146 | return DEFER; |
8523533c | 147 | }; |
8e669ac1 | 148 | |
8523533c TK |
149 | if (ip_connect( spamd_sock, |
150 | AF_INET, | |
151 | spamd_address_vector[current_server]->tcp_addr, | |
152 | spamd_address_vector[current_server]->tcp_port, | |
153 | 5 ) > -1) { | |
154 | /* connection OK */ | |
155 | break; | |
156 | }; | |
8e669ac1 | 157 | |
8523533c TK |
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, | |
162 | strerror(errno)); | |
163 | current_server++; | |
164 | if (current_server >= num_servers) | |
165 | current_server = 0; | |
166 | if (current_server == start_server) { | |
167 | log_write(0, LOG_MAIN|LOG_PANIC, "spam acl condition: all spamd servers failed"); | |
f1e894f3 PH |
168 | (void)fclose(mbox_file); |
169 | (void)close(spamd_sock); | |
8523533c TK |
170 | return DEFER; |
171 | }; | |
172 | }; | |
173 | ||
174 | } | |
175 | else { | |
176 | /* open the local socket */ | |
177 | ||
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)", | |
181 | strerror(errno)); | |
f1e894f3 | 182 | (void)fclose(mbox_file); |
8523533c TK |
183 | return DEFER; |
184 | } | |
185 | ||
186 | server.sun_family = AF_UNIX; | |
187 | Ustrcpy(server.sun_path, spamd_address); | |
188 | ||
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) ); | |
f1e894f3 PH |
193 | (void)fclose(mbox_file); |
194 | (void)close(spamd_sock); | |
8523533c TK |
195 | return DEFER; |
196 | } | |
197 | ||
198 | } | |
199 | ||
200 | /* now we are connected to spamd on spamd_sock */ | |
b07e6aa3 | 201 | (void)string_format(spamd_buffer, |
8523533c | 202 | sizeof(spamd_buffer), |
f7b63901 | 203 | "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n", |
8523533c TK |
204 | user_name, |
205 | mbox_size); | |
206 | ||
207 | /* send our request */ | |
208 | if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0) { | |
f1e894f3 | 209 | (void)close(spamd_sock); |
8523533c TK |
210 | log_write(0, LOG_MAIN|LOG_PANIC, |
211 | "spam acl condition: spamd send failed: %s", strerror(errno)); | |
f1e894f3 PH |
212 | (void)fclose(mbox_file); |
213 | (void)close(spamd_sock); | |
8523533c TK |
214 | return DEFER; |
215 | }; | |
216 | ||
217 | /* now send the file */ | |
cfe75fc3 PH |
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. | |
25257489 PH |
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). | |
cfe75fc3 | 226 | */ |
f452e07e | 227 | #ifndef NO_POLL_H |
cfe75fc3 PH |
228 | pollfd.fd = spamd_sock; |
229 | pollfd.events = POLLOUT; | |
f452e07e | 230 | #endif |
ff790e47 | 231 | (void)fcntl(spamd_sock, F_SETFL, O_NONBLOCK); |
8523533c | 232 | do { |
cfe75fc3 PH |
233 | read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file); |
234 | if (read > 0) { | |
235 | offset = 0; | |
236 | again: | |
f452e07e | 237 | #ifndef NO_POLL_H |
cfe75fc3 | 238 | result = poll(&pollfd, 1, 1000); |
25257489 PH |
239 | |
240 | /* Patch posted by Erik ? for OS X and applied by PH */ | |
241 | #else | |
242 | select_tv.tv_sec = 1; | |
243 | select_tv.tv_usec = 0; | |
244 | FD_ZERO(&select_fd); | |
245 | FD_SET(spamd_sock, &select_fd); | |
246 | result = select(spamd_sock+1, NULL, &select_fd, NULL, &select_tv); | |
247 | #endif | |
248 | /* End Erik's patch */ | |
249 | ||
cfe75fc3 | 250 | if (result == -1 && errno == EINTR) |
25257489 | 251 | goto again; |
cfe75fc3 PH |
252 | else if (result < 1) { |
253 | if (result == -1) | |
254 | log_write(0, LOG_MAIN|LOG_PANIC, | |
255 | "spam acl condition: %s on spamd socket", strerror(errno)); | |
256 | else { | |
257 | if (time(NULL) - start < SPAMD_TIMEOUT) | |
258 | goto again; | |
259 | log_write(0, LOG_MAIN|LOG_PANIC, | |
260 | "spam acl condition: timed out writing spamd socket"); | |
261 | } | |
f1e894f3 PH |
262 | (void)close(spamd_sock); |
263 | (void)fclose(mbox_file); | |
8523533c | 264 | return DEFER; |
cfe75fc3 | 265 | } |
25257489 | 266 | |
cfe75fc3 | 267 | wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0); |
8d7d227d TK |
268 | if (wrote == -1) |
269 | { | |
270 | log_write(0, LOG_MAIN|LOG_PANIC, | |
271 | "spam acl condition: %s on spamd socket", strerror(errno)); | |
f1e894f3 PH |
272 | (void)close(spamd_sock); |
273 | (void)fclose(mbox_file); | |
8d7d227d TK |
274 | return DEFER; |
275 | } | |
cfe75fc3 PH |
276 | if (offset + wrote != read) { |
277 | offset += wrote; | |
278 | goto again; | |
279 | } | |
280 | } | |
281 | } | |
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)); | |
f1e894f3 PH |
286 | (void)close(spamd_sock); |
287 | (void)fclose(mbox_file); | |
cfe75fc3 | 288 | return DEFER; |
8523533c | 289 | } |
8523533c | 290 | |
f1e894f3 | 291 | (void)fclose(mbox_file); |
8523533c TK |
292 | |
293 | /* we're done sending, close socket for writing */ | |
294 | shutdown(spamd_sock,SHUT_WR); | |
8e669ac1 | 295 | |
cfe75fc3 PH |
296 | /* read spamd response using what's left of the timeout. |
297 | */ | |
8523533c TK |
298 | memset(spamd_buffer, 0, sizeof(spamd_buffer)); |
299 | offset = 0; | |
300 | while((i = ip_recv(spamd_sock, | |
301 | spamd_buffer + offset, | |
302 | sizeof(spamd_buffer) - offset - 1, | |
cfe75fc3 | 303 | SPAMD_TIMEOUT - time(NULL) + start)) > 0 ) { |
8523533c TK |
304 | offset += i; |
305 | } | |
306 | ||
307 | /* error handling */ | |
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)); | |
f1e894f3 | 311 | (void)close(spamd_sock); |
8523533c TK |
312 | return DEFER; |
313 | } | |
314 | ||
315 | /* reading done */ | |
f1e894f3 | 316 | (void)close(spamd_sock); |
8523533c TK |
317 | |
318 | /* dig in the spamd output and put the report in a multiline header, if requested */ | |
0806a9c5 | 319 | if( sscanf(CS spamd_buffer,"SPAMD/%7s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n", |
8523533c | 320 | spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) { |
8e669ac1 | 321 | |
8523533c | 322 | /* try to fall back to pre-2.50 spamd output */ |
0806a9c5 | 323 | if( sscanf(CS spamd_buffer,"SPAMD/%7s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n", |
8523533c TK |
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"); | |
327 | return DEFER; | |
328 | }; | |
329 | }; | |
330 | ||
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; | |
335 | while (*p != '\0') { | |
336 | /* skip \r */ | |
337 | if (*p == '\r') { | |
338 | p++; | |
339 | continue; | |
340 | }; | |
341 | *q = *p; | |
342 | q++; | |
343 | if (*p == '\n') { | |
344 | *q = '\t'; | |
345 | q++; | |
346 | /* eat whitespace */ | |
347 | while( (*p <= ' ') && (*p != '\0') ) { | |
348 | p++; | |
349 | }; | |
350 | p--; | |
351 | }; | |
352 | p++; | |
353 | }; | |
354 | /* NULL-terminate */ | |
355 | *q = '\0'; | |
356 | q--; | |
357 | /* cut off trailing leftovers */ | |
358 | while (*q <= ' ') { | |
359 | *q = '\0'; | |
360 | q--; | |
361 | }; | |
362 | spam_report = spam_report_buffer; | |
363 | ||
364 | /* create spam bar */ | |
365 | spamd_score_char = spamd_score > 0 ? '+' : '-'; | |
366 | j = abs((int)(spamd_score)); | |
367 | i = 0; | |
368 | if( j != 0 ) { | |
369 | while((i < j) && (i <= MAX_SPAM_BAR_CHARS)) | |
370 | spam_bar_buffer[i++] = spamd_score_char; | |
371 | } | |
372 | else{ | |
373 | spam_bar_buffer[0] = '/'; | |
374 | i = 1; | |
375 | } | |
376 | spam_bar_buffer[i] = '\0'; | |
377 | spam_bar = spam_bar_buffer; | |
378 | ||
379 | /* create "float" spam score */ | |
b07e6aa3 | 380 | (void)string_format(spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score); |
8523533c TK |
381 | spam_score = spam_score_buffer; |
382 | ||
383 | /* create "int" spam score */ | |
384 | j = (int)((spamd_score + 0.001)*10); | |
b07e6aa3 | 385 | (void)string_format(spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j); |
8523533c TK |
386 | spam_score_int = spam_score_int_buffer; |
387 | ||
388 | /* compare threshold against score */ | |
389 | if (spamd_score >= spamd_threshold) { | |
390 | /* spam as determined by user's threshold */ | |
391 | spam_rc = OK; | |
392 | } | |
393 | else { | |
394 | /* not spam */ | |
395 | spam_rc = FAIL; | |
396 | }; | |
8e669ac1 | 397 | |
8523533c TK |
398 | /* remember user name and "been here" for it */ |
399 | Ustrcpy(prev_user_name, user_name); | |
400 | spam_ok = 1; | |
8e669ac1 | 401 | |
8523533c TK |
402 | if (override) { |
403 | /* always return OK, no matter what the score */ | |
404 | return OK; | |
405 | } | |
406 | else { | |
407 | return spam_rc; | |
408 | }; | |
409 | } | |
410 | ||
411 | #endif |