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