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