1 /* $Cambridge: exim/src/src/spam.c,v 1.4 2005/02/17 11:58:26 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];
34 uschar spamd_version[8];
35 uschar spamd_score_char;
36 double spamd_threshold, spamd_score;
37 int spamd_report_offset;
40 struct sockaddr_un server;
42 /* find the username from the option list */
43 if ((user_name = string_nextinlist(&list, &sep,
45 sizeof(user_name_buffer))) == NULL) {
46 /* no username given, this means no scanning should be done */
50 /* if username is "0" or "false", do not scan */
51 if ( (Ustrcmp(user_name,"0") == 0) ||
52 (strcmpic(user_name,US"false") == 0) ) {
56 /* if there is an additional option, check if it is "true" */
57 if (strcmpic(list,US"true") == 0) {
58 /* in that case, always return true later */
62 /* if we scanned for this username last time, just return */
63 if ( spam_ok && ( Ustrcmp(prev_user_name, user_name) == 0 ) ) {
70 /* make sure the eml mbox file is spooled up */
71 mbox_file = spool_mbox(&mbox_size);
73 if (mbox_file == NULL) {
74 /* error while spooling */
75 log_write(0, LOG_MAIN|LOG_PANIC,
76 "spam acl condition: error while creating mbox spool file");
80 /* socket does not start with '/' -> network socket */
81 if (*spamd_address != '/') {
82 time_t now = time(NULL);
84 int current_server = 0;
86 uschar *address = NULL;
87 uschar *spamd_address_list_ptr = spamd_address;
88 uschar address_buffer[256];
89 spamd_address_container * spamd_address_vector[32];
91 /* Check how many spamd servers we have
92 and register their addresses */
93 while ((address = string_nextinlist(&spamd_address_list_ptr, &sep,
95 sizeof(address_buffer))) != NULL) {
97 spamd_address_container *this_spamd =
98 (spamd_address_container *)store_get(sizeof(spamd_address_container));
100 /* grok spamd address and port */
101 if( sscanf(CS address, "%s %u", this_spamd->tcp_addr, &(this_spamd->tcp_port)) != 2 ) {
102 log_write(0, LOG_MAIN,
103 "spam acl condition: warning - invalid spamd address: '%s'", address);
107 spamd_address_vector[num_servers] = this_spamd;
109 if (num_servers > 31)
113 /* check if we have at least one server */
115 log_write(0, LOG_MAIN|LOG_PANIC,
116 "spam acl condition: no useable spamd server addresses in spamd_address configuration option.");
121 current_server = start_server = (int)now % num_servers;
125 debug_printf("trying server %s, port %u\n",
126 spamd_address_vector[current_server]->tcp_addr,
127 spamd_address_vector[current_server]->tcp_port);
129 /* contact a spamd */
130 if ( (spamd_sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
131 log_write(0, LOG_MAIN|LOG_PANIC,
132 "spam acl condition: error creating IP socket for spamd");
137 if (ip_connect( spamd_sock,
139 spamd_address_vector[current_server]->tcp_addr,
140 spamd_address_vector[current_server]->tcp_port,
146 log_write(0, LOG_MAIN|LOG_PANIC,
147 "spam acl condition: warning - spamd connection to %s, port %u failed: %s",
148 spamd_address_vector[current_server]->tcp_addr,
149 spamd_address_vector[current_server]->tcp_port,
152 if (current_server >= num_servers)
154 if (current_server == start_server) {
155 log_write(0, LOG_MAIN|LOG_PANIC, "spam acl condition: all spamd servers failed");
164 /* open the local socket */
166 if ((spamd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
167 log_write(0, LOG_MAIN|LOG_PANIC,
168 "malware acl condition: spamd: unable to acquire socket (%s)",
174 server.sun_family = AF_UNIX;
175 Ustrcpy(server.sun_path, spamd_address);
177 if (connect(spamd_sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
178 log_write(0, LOG_MAIN|LOG_PANIC,
179 "malware acl condition: spamd: unable to connect to UNIX socket %s (%s)",
180 spamd_address, strerror(errno) );
188 /* now we are connected to spamd on spamd_sock */
189 snprintf(CS spamd_buffer,
190 sizeof(spamd_buffer),
191 "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n",
195 /* send our request */
196 if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0) {
198 log_write(0, LOG_MAIN|LOG_PANIC,
199 "spam acl condition: spamd send failed: %s", strerror(errno));
205 /* now send the file */
207 j = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
209 i = send(spamd_sock,spamd_buffer,j,0);
211 log_write(0, LOG_MAIN|LOG_PANIC,
212 "spam acl condition: error/short send to spamd");
223 /* we're done sending, close socket for writing */
224 shutdown(spamd_sock,SHUT_WR);
226 /* read spamd response */
227 memset(spamd_buffer, 0, sizeof(spamd_buffer));
229 while((i = ip_recv(spamd_sock,
230 spamd_buffer + offset,
231 sizeof(spamd_buffer) - offset - 1,
232 SPAMD_READ_TIMEOUT)) > 0 ) {
237 if((i <= 0) && (errno != 0)) {
238 log_write(0, LOG_MAIN|LOG_PANIC,
239 "spam acl condition: error reading from spamd socket: %s", strerror(errno));
247 /* dig in the spamd output and put the report in a multiline header, if requested */
248 if( sscanf(CS spamd_buffer,"SPAMD/%s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n",
249 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
251 /* try to fall back to pre-2.50 spamd output */
252 if( sscanf(CS spamd_buffer,"SPAMD/%s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n",
253 spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) {
254 log_write(0, LOG_MAIN|LOG_PANIC,
255 "spam acl condition: cannot parse spamd output");
260 /* Create report. Since this is a multiline string,
261 we must hack it into shape first */
262 p = &spamd_buffer[spamd_report_offset];
263 q = spam_report_buffer;
276 while( (*p <= ' ') && (*p != '\0') ) {
286 /* cut off trailing leftovers */
291 spam_report = spam_report_buffer;
293 /* create spam bar */
294 spamd_score_char = spamd_score > 0 ? '+' : '-';
295 j = abs((int)(spamd_score));
298 while((i < j) && (i <= MAX_SPAM_BAR_CHARS))
299 spam_bar_buffer[i++] = spamd_score_char;
302 spam_bar_buffer[0] = '/';
305 spam_bar_buffer[i] = '\0';
306 spam_bar = spam_bar_buffer;
308 /* create "float" spam score */
309 snprintf(CS spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score);
310 spam_score = spam_score_buffer;
312 /* create "int" spam score */
313 j = (int)((spamd_score + 0.001)*10);
314 snprintf(CS spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j);
315 spam_score_int = spam_score_int_buffer;
317 /* compare threshold against score */
318 if (spamd_score >= spamd_threshold) {
319 /* spam as determined by user's threshold */
327 /* remember user name and "been here" for it */
328 Ustrcpy(prev_user_name, user_name);
332 /* always return OK, no matter what the score */