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