| 1 | /* $Cambridge: exim/src/src/spam.c,v 1.6 2005/05/10 22:39:20 tom Exp $ */ |
| 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]; |
| 29 | unsigned long mbox_size; |
| 30 | FILE *mbox_file; |
| 31 | int spamd_sock; |
| 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; |
| 38 | uschar *p,*q; |
| 39 | int override = 0; |
| 40 | time_t start; |
| 41 | size_t read, wrote; |
| 42 | struct sockaddr_un server; |
| 43 | #ifndef NO_POLL_H |
| 44 | struct pollfd pollfd; |
| 45 | #endif |
| 46 | |
| 47 | /* find the username from the option list */ |
| 48 | if ((user_name = string_nextinlist(&list, &sep, |
| 49 | user_name_buffer, |
| 50 | sizeof(user_name_buffer))) == NULL) { |
| 51 | /* no username given, this means no scanning should be done */ |
| 52 | return FAIL; |
| 53 | }; |
| 54 | |
| 55 | /* if username is "0" or "false", do not scan */ |
| 56 | if ( (Ustrcmp(user_name,"0") == 0) || |
| 57 | (strcmpic(user_name,US"false") == 0) ) { |
| 58 | return FAIL; |
| 59 | }; |
| 60 | |
| 61 | /* if there is an additional option, check if it is "true" */ |
| 62 | if (strcmpic(list,US"true") == 0) { |
| 63 | /* in that case, always return true later */ |
| 64 | override = 1; |
| 65 | }; |
| 66 | |
| 67 | /* if we scanned for this username last time, just return */ |
| 68 | if ( spam_ok && ( Ustrcmp(prev_user_name, user_name) == 0 ) ) { |
| 69 | if (override) |
| 70 | return OK; |
| 71 | else |
| 72 | return spam_rc; |
| 73 | }; |
| 74 | |
| 75 | /* make sure the eml mbox file is spooled up */ |
| 76 | mbox_file = spool_mbox(&mbox_size); |
| 77 | |
| 78 | if (mbox_file == NULL) { |
| 79 | /* error while spooling */ |
| 80 | log_write(0, LOG_MAIN|LOG_PANIC, |
| 81 | "spam acl condition: error while creating mbox spool file"); |
| 82 | return DEFER; |
| 83 | }; |
| 84 | |
| 85 | start = time(NULL); |
| 86 | /* socket does not start with '/' -> network socket */ |
| 87 | if (*spamd_address != '/') { |
| 88 | time_t now = time(NULL); |
| 89 | int num_servers = 0; |
| 90 | int current_server = 0; |
| 91 | int start_server = 0; |
| 92 | uschar *address = NULL; |
| 93 | uschar *spamd_address_list_ptr = spamd_address; |
| 94 | uschar address_buffer[256]; |
| 95 | spamd_address_container * spamd_address_vector[32]; |
| 96 | |
| 97 | /* Check how many spamd servers we have |
| 98 | and register their addresses */ |
| 99 | while ((address = string_nextinlist(&spamd_address_list_ptr, &sep, |
| 100 | address_buffer, |
| 101 | sizeof(address_buffer))) != NULL) { |
| 102 | |
| 103 | spamd_address_container *this_spamd = |
| 104 | (spamd_address_container *)store_get(sizeof(spamd_address_container)); |
| 105 | |
| 106 | /* grok spamd address and port */ |
| 107 | if( sscanf(CS address, "%s %u", this_spamd->tcp_addr, &(this_spamd->tcp_port)) != 2 ) { |
| 108 | log_write(0, LOG_MAIN, |
| 109 | "spam acl condition: warning - invalid spamd address: '%s'", address); |
| 110 | continue; |
| 111 | }; |
| 112 | |
| 113 | spamd_address_vector[num_servers] = this_spamd; |
| 114 | num_servers++; |
| 115 | if (num_servers > 31) |
| 116 | break; |
| 117 | }; |
| 118 | |
| 119 | /* check if we have at least one server */ |
| 120 | if (!num_servers) { |
| 121 | log_write(0, LOG_MAIN|LOG_PANIC, |
| 122 | "spam acl condition: no useable spamd server addresses in spamd_address configuration option."); |
| 123 | fclose(mbox_file); |
| 124 | return DEFER; |
| 125 | }; |
| 126 | |
| 127 | current_server = start_server = (int)now % num_servers; |
| 128 | |
| 129 | while (1) { |
| 130 | |
| 131 | debug_printf("trying server %s, port %u\n", |
| 132 | spamd_address_vector[current_server]->tcp_addr, |
| 133 | spamd_address_vector[current_server]->tcp_port); |
| 134 | |
| 135 | /* contact a spamd */ |
| 136 | if ( (spamd_sock = ip_socket(SOCK_STREAM, AF_INET)) < 0) { |
| 137 | log_write(0, LOG_MAIN|LOG_PANIC, |
| 138 | "spam acl condition: error creating IP socket for spamd"); |
| 139 | fclose(mbox_file); |
| 140 | return DEFER; |
| 141 | }; |
| 142 | |
| 143 | if (ip_connect( spamd_sock, |
| 144 | AF_INET, |
| 145 | spamd_address_vector[current_server]->tcp_addr, |
| 146 | spamd_address_vector[current_server]->tcp_port, |
| 147 | 5 ) > -1) { |
| 148 | /* connection OK */ |
| 149 | break; |
| 150 | }; |
| 151 | |
| 152 | log_write(0, LOG_MAIN|LOG_PANIC, |
| 153 | "spam acl condition: warning - spamd connection to %s, port %u failed: %s", |
| 154 | spamd_address_vector[current_server]->tcp_addr, |
| 155 | spamd_address_vector[current_server]->tcp_port, |
| 156 | strerror(errno)); |
| 157 | current_server++; |
| 158 | if (current_server >= num_servers) |
| 159 | current_server = 0; |
| 160 | if (current_server == start_server) { |
| 161 | log_write(0, LOG_MAIN|LOG_PANIC, "spam acl condition: all spamd servers failed"); |
| 162 | fclose(mbox_file); |
| 163 | close(spamd_sock); |
| 164 | return DEFER; |
| 165 | }; |
| 166 | }; |
| 167 | |
| 168 | } |
| 169 | else { |
| 170 | /* open the local socket */ |
| 171 | |
| 172 | if ((spamd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { |
| 173 | log_write(0, LOG_MAIN|LOG_PANIC, |
| 174 | "malware acl condition: spamd: unable to acquire socket (%s)", |
| 175 | strerror(errno)); |
| 176 | fclose(mbox_file); |
| 177 | return DEFER; |
| 178 | } |
| 179 | |
| 180 | server.sun_family = AF_UNIX; |
| 181 | Ustrcpy(server.sun_path, spamd_address); |
| 182 | |
| 183 | if (connect(spamd_sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) { |
| 184 | log_write(0, LOG_MAIN|LOG_PANIC, |
| 185 | "malware acl condition: spamd: unable to connect to UNIX socket %s (%s)", |
| 186 | spamd_address, strerror(errno) ); |
| 187 | fclose(mbox_file); |
| 188 | close(spamd_sock); |
| 189 | return DEFER; |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | |
| 194 | /* now we are connected to spamd on spamd_sock */ |
| 195 | snprintf(CS spamd_buffer, |
| 196 | sizeof(spamd_buffer), |
| 197 | "REPORT SPAMC/1.2\r\nUser: %s\r\nContent-length: %ld\r\n\r\n", |
| 198 | user_name, |
| 199 | mbox_size); |
| 200 | |
| 201 | /* send our request */ |
| 202 | if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0) { |
| 203 | close(spamd_sock); |
| 204 | log_write(0, LOG_MAIN|LOG_PANIC, |
| 205 | "spam acl condition: spamd send failed: %s", strerror(errno)); |
| 206 | fclose(mbox_file); |
| 207 | close(spamd_sock); |
| 208 | return DEFER; |
| 209 | }; |
| 210 | |
| 211 | /* now send the file */ |
| 212 | /* spamd sometimes accepts conections but doesn't read data off |
| 213 | * the connection. We make the file descriptor non-blocking so |
| 214 | * that the write will only write sufficient data without blocking |
| 215 | * and we poll the desciptor to make sure that we can write without |
| 216 | * blocking. Short writes are gracefully handled and if the whole |
| 217 | * trasaction takes too long it is aborted. |
| 218 | * Note: poll() is not supported in OSX 10.2. |
| 219 | */ |
| 220 | #ifndef NO_POLL_H |
| 221 | pollfd.fd = spamd_sock; |
| 222 | pollfd.events = POLLOUT; |
| 223 | #endif |
| 224 | fcntl(spamd_sock, F_SETFL, O_NONBLOCK); |
| 225 | do { |
| 226 | read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file); |
| 227 | if (read > 0) { |
| 228 | offset = 0; |
| 229 | again: |
| 230 | #ifndef NO_POLL_H |
| 231 | result = poll(&pollfd, 1, 1000); |
| 232 | if (result == -1 && errno == EINTR) |
| 233 | continue; |
| 234 | else if (result < 1) { |
| 235 | if (result == -1) |
| 236 | log_write(0, LOG_MAIN|LOG_PANIC, |
| 237 | "spam acl condition: %s on spamd socket", strerror(errno)); |
| 238 | else { |
| 239 | if (time(NULL) - start < SPAMD_TIMEOUT) |
| 240 | goto again; |
| 241 | log_write(0, LOG_MAIN|LOG_PANIC, |
| 242 | "spam acl condition: timed out writing spamd socket"); |
| 243 | } |
| 244 | close(spamd_sock); |
| 245 | fclose(mbox_file); |
| 246 | return DEFER; |
| 247 | } |
| 248 | #endif |
| 249 | wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0); |
| 250 | if (offset + wrote != read) { |
| 251 | offset += wrote; |
| 252 | goto again; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | while (!feof(mbox_file) && !ferror(mbox_file)); |
| 257 | if (ferror(mbox_file)) { |
| 258 | log_write(0, LOG_MAIN|LOG_PANIC, |
| 259 | "spam acl condition: error reading spool file: %s", strerror(errno)); |
| 260 | close(spamd_sock); |
| 261 | fclose(mbox_file); |
| 262 | return DEFER; |
| 263 | } |
| 264 | |
| 265 | fclose(mbox_file); |
| 266 | |
| 267 | /* we're done sending, close socket for writing */ |
| 268 | shutdown(spamd_sock,SHUT_WR); |
| 269 | |
| 270 | /* read spamd response using what's left of the timeout. |
| 271 | */ |
| 272 | memset(spamd_buffer, 0, sizeof(spamd_buffer)); |
| 273 | offset = 0; |
| 274 | while((i = ip_recv(spamd_sock, |
| 275 | spamd_buffer + offset, |
| 276 | sizeof(spamd_buffer) - offset - 1, |
| 277 | SPAMD_TIMEOUT - time(NULL) + start)) > 0 ) { |
| 278 | offset += i; |
| 279 | } |
| 280 | |
| 281 | /* error handling */ |
| 282 | if((i <= 0) && (errno != 0)) { |
| 283 | log_write(0, LOG_MAIN|LOG_PANIC, |
| 284 | "spam acl condition: error reading from spamd socket: %s", strerror(errno)); |
| 285 | close(spamd_sock); |
| 286 | return DEFER; |
| 287 | } |
| 288 | |
| 289 | /* reading done */ |
| 290 | close(spamd_sock); |
| 291 | |
| 292 | /* dig in the spamd output and put the report in a multiline header, if requested */ |
| 293 | if( sscanf(CS spamd_buffer,"SPAMD/%s 0 EX_OK\r\nContent-length: %*u\r\n\r\n%lf/%lf\r\n%n", |
| 294 | spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) { |
| 295 | |
| 296 | /* try to fall back to pre-2.50 spamd output */ |
| 297 | if( sscanf(CS spamd_buffer,"SPAMD/%s 0 EX_OK\r\nSpam: %*s ; %lf / %lf\r\n\r\n%n", |
| 298 | spamd_version,&spamd_score,&spamd_threshold,&spamd_report_offset) != 3 ) { |
| 299 | log_write(0, LOG_MAIN|LOG_PANIC, |
| 300 | "spam acl condition: cannot parse spamd output"); |
| 301 | return DEFER; |
| 302 | }; |
| 303 | }; |
| 304 | |
| 305 | /* Create report. Since this is a multiline string, |
| 306 | we must hack it into shape first */ |
| 307 | p = &spamd_buffer[spamd_report_offset]; |
| 308 | q = spam_report_buffer; |
| 309 | while (*p != '\0') { |
| 310 | /* skip \r */ |
| 311 | if (*p == '\r') { |
| 312 | p++; |
| 313 | continue; |
| 314 | }; |
| 315 | *q = *p; |
| 316 | q++; |
| 317 | if (*p == '\n') { |
| 318 | *q = '\t'; |
| 319 | q++; |
| 320 | /* eat whitespace */ |
| 321 | while( (*p <= ' ') && (*p != '\0') ) { |
| 322 | p++; |
| 323 | }; |
| 324 | p--; |
| 325 | }; |
| 326 | p++; |
| 327 | }; |
| 328 | /* NULL-terminate */ |
| 329 | *q = '\0'; |
| 330 | q--; |
| 331 | /* cut off trailing leftovers */ |
| 332 | while (*q <= ' ') { |
| 333 | *q = '\0'; |
| 334 | q--; |
| 335 | }; |
| 336 | spam_report = spam_report_buffer; |
| 337 | |
| 338 | /* create spam bar */ |
| 339 | spamd_score_char = spamd_score > 0 ? '+' : '-'; |
| 340 | j = abs((int)(spamd_score)); |
| 341 | i = 0; |
| 342 | if( j != 0 ) { |
| 343 | while((i < j) && (i <= MAX_SPAM_BAR_CHARS)) |
| 344 | spam_bar_buffer[i++] = spamd_score_char; |
| 345 | } |
| 346 | else{ |
| 347 | spam_bar_buffer[0] = '/'; |
| 348 | i = 1; |
| 349 | } |
| 350 | spam_bar_buffer[i] = '\0'; |
| 351 | spam_bar = spam_bar_buffer; |
| 352 | |
| 353 | /* create "float" spam score */ |
| 354 | snprintf(CS spam_score_buffer, sizeof(spam_score_buffer),"%.1f", spamd_score); |
| 355 | spam_score = spam_score_buffer; |
| 356 | |
| 357 | /* create "int" spam score */ |
| 358 | j = (int)((spamd_score + 0.001)*10); |
| 359 | snprintf(CS spam_score_int_buffer, sizeof(spam_score_int_buffer), "%d", j); |
| 360 | spam_score_int = spam_score_int_buffer; |
| 361 | |
| 362 | /* compare threshold against score */ |
| 363 | if (spamd_score >= spamd_threshold) { |
| 364 | /* spam as determined by user's threshold */ |
| 365 | spam_rc = OK; |
| 366 | } |
| 367 | else { |
| 368 | /* not spam */ |
| 369 | spam_rc = FAIL; |
| 370 | }; |
| 371 | |
| 372 | /* remember user name and "been here" for it */ |
| 373 | Ustrcpy(prev_user_name, user_name); |
| 374 | spam_ok = 1; |
| 375 | |
| 376 | if (override) { |
| 377 | /* always return OK, no matter what the score */ |
| 378 | return OK; |
| 379 | } |
| 380 | else { |
| 381 | return spam_rc; |
| 382 | }; |
| 383 | } |
| 384 | |
| 385 | #endif |