| 1 | /************************************************* |
| 2 | * Exim - an Internet mail transport agent * |
| 3 | *************************************************/ |
| 4 | |
| 5 | /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004 */ |
| 6 | /* License: GPL */ |
| 7 | |
| 8 | #include "exim.h" |
| 9 | #ifdef WITH_CONTENT_SCAN /* entire file */ |
| 10 | #include "mime.h" |
| 11 | #include <sys/stat.h> |
| 12 | |
| 13 | FILE *mime_stream = NULL; |
| 14 | uschar *mime_current_boundary = NULL; |
| 15 | |
| 16 | /************************************************* |
| 17 | * set MIME anomaly level + text * |
| 18 | *************************************************/ |
| 19 | |
| 20 | /* Small wrapper to set the two expandables which |
| 21 | give info on detected "problems" in MIME |
| 22 | encodings. Those are defined in mime.h. */ |
| 23 | |
| 24 | static void |
| 25 | mime_set_anomaly(int level, const char *text) |
| 26 | { |
| 27 | mime_anomaly_level = level; |
| 28 | mime_anomaly_text = CUS text; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | /************************************************* |
| 33 | * decode quoted-printable chars * |
| 34 | *************************************************/ |
| 35 | |
| 36 | /* gets called when we hit a = |
| 37 | returns: new pointer position |
| 38 | result code in c: |
| 39 | -2 - decode error |
| 40 | -1 - soft line break, no char |
| 41 | 0-255 - char to write |
| 42 | */ |
| 43 | |
| 44 | static uschar * |
| 45 | mime_decode_qp_char(uschar *qp_p, int *c) |
| 46 | { |
| 47 | uschar *initial_pos = qp_p; |
| 48 | |
| 49 | /* advance one char */ |
| 50 | qp_p++; |
| 51 | |
| 52 | /* Check for two hex digits and decode them */ |
| 53 | if (isxdigit(*qp_p) && isxdigit(qp_p[1])) |
| 54 | { |
| 55 | /* Do hex conversion */ |
| 56 | *c = (isdigit(*qp_p) ? *qp_p - '0' : toupper(*qp_p) - 'A' + 10) <<4; |
| 57 | qp_p++; |
| 58 | *c |= isdigit(*qp_p) ? *qp_p - '0' : toupper(*qp_p) - 'A' + 10; |
| 59 | return qp_p + 1; |
| 60 | } |
| 61 | |
| 62 | /* tab or whitespace may follow just ignore it if it precedes \n */ |
| 63 | while (*qp_p == '\t' || *qp_p == ' ' || *qp_p == '\r') |
| 64 | qp_p++; |
| 65 | |
| 66 | if (*qp_p == '\n') /* hit soft line break */ |
| 67 | { |
| 68 | *c = -1; |
| 69 | return qp_p; |
| 70 | } |
| 71 | |
| 72 | /* illegal char here */ |
| 73 | *c = -2; |
| 74 | return initial_pos; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /* just dump MIME part without any decoding */ |
| 79 | static ssize_t |
| 80 | mime_decode_asis(FILE* in, FILE* out, uschar* boundary) |
| 81 | { |
| 82 | ssize_t len, size = 0; |
| 83 | uschar buffer[MIME_MAX_LINE_LENGTH]; |
| 84 | |
| 85 | while(fgets(CS buffer, MIME_MAX_LINE_LENGTH, mime_stream) != NULL) |
| 86 | { |
| 87 | if (boundary != NULL |
| 88 | && Ustrncmp(buffer, "--", 2) == 0 |
| 89 | && Ustrncmp((buffer+2), boundary, Ustrlen(boundary)) == 0 |
| 90 | ) |
| 91 | break; |
| 92 | |
| 93 | len = Ustrlen(buffer); |
| 94 | if (fwrite(buffer, 1, (size_t)len, out) < len) |
| 95 | return -1; |
| 96 | size += len; |
| 97 | } /* while */ |
| 98 | return size; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /* decode base64 MIME part */ |
| 103 | static ssize_t |
| 104 | mime_decode_base64(FILE* in, FILE* out, uschar* boundary) |
| 105 | { |
| 106 | uschar ibuf[MIME_MAX_LINE_LENGTH], obuf[MIME_MAX_LINE_LENGTH]; |
| 107 | uschar *ipos, *opos; |
| 108 | ssize_t len, size = 0; |
| 109 | int bytestate = 0; |
| 110 | |
| 111 | opos = obuf; |
| 112 | |
| 113 | while (Ufgets(ibuf, MIME_MAX_LINE_LENGTH, in) != NULL) |
| 114 | { |
| 115 | if (boundary != NULL |
| 116 | && Ustrncmp(ibuf, "--", 2) == 0 |
| 117 | && Ustrncmp((ibuf+2), boundary, Ustrlen(boundary)) == 0 |
| 118 | ) |
| 119 | break; |
| 120 | |
| 121 | for (ipos = ibuf ; *ipos != '\r' && *ipos != '\n' && *ipos != 0; ++ipos) |
| 122 | { |
| 123 | if (*ipos == '=') /* skip padding */ |
| 124 | { |
| 125 | ++bytestate; |
| 126 | continue; |
| 127 | } |
| 128 | if (mime_b64[*ipos] == 128) /* skip bad characters */ |
| 129 | { |
| 130 | mime_set_anomaly(MIME_ANOMALY_BROKEN_BASE64); |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | /* simple state-machine */ |
| 135 | switch((bytestate++) & 3) |
| 136 | { |
| 137 | case 0: |
| 138 | *opos = mime_b64[*ipos] << 2; |
| 139 | break; |
| 140 | case 1: |
| 141 | *opos |= mime_b64[*ipos] >> 4; |
| 142 | ++opos; |
| 143 | *opos = mime_b64[*ipos] << 4; |
| 144 | break; |
| 145 | case 2: |
| 146 | *opos |= mime_b64[*ipos] >> 2; |
| 147 | ++opos; |
| 148 | *opos = mime_b64[*ipos] << 6; |
| 149 | break; |
| 150 | case 3: |
| 151 | *opos |= mime_b64[*ipos]; |
| 152 | ++opos; |
| 153 | break; |
| 154 | } /* switch */ |
| 155 | } /* for */ |
| 156 | |
| 157 | /* something to write? */ |
| 158 | len = opos - obuf; |
| 159 | if (len > 0) |
| 160 | { |
| 161 | if (fwrite(obuf, 1, len, out) != len) return -1; /* error */ |
| 162 | size += len; |
| 163 | /* copy incomplete last byte to start of obuf, where we continue */ |
| 164 | if ((bytestate & 3) != 0) |
| 165 | *obuf = *opos; |
| 166 | opos = obuf; |
| 167 | } |
| 168 | } /* while */ |
| 169 | |
| 170 | /* write out last byte if it was incomplete */ |
| 171 | if (bytestate & 3) |
| 172 | { |
| 173 | if (fwrite(obuf, 1, 1, out) != 1) return -1; |
| 174 | ++size; |
| 175 | } |
| 176 | |
| 177 | return size; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | /* decode quoted-printable MIME part */ |
| 182 | static ssize_t |
| 183 | mime_decode_qp(FILE* in, FILE* out, uschar* boundary) |
| 184 | { |
| 185 | uschar ibuf[MIME_MAX_LINE_LENGTH], obuf[MIME_MAX_LINE_LENGTH]; |
| 186 | uschar *ipos, *opos; |
| 187 | ssize_t len, size = 0; |
| 188 | |
| 189 | while (fgets(CS ibuf, MIME_MAX_LINE_LENGTH, in) != NULL) |
| 190 | { |
| 191 | if (boundary != NULL |
| 192 | && Ustrncmp(ibuf, "--", 2) == 0 |
| 193 | && Ustrncmp((ibuf+2), boundary, Ustrlen(boundary)) == 0 |
| 194 | ) |
| 195 | break; /* todo: check for missing boundary */ |
| 196 | |
| 197 | ipos = ibuf; |
| 198 | opos = obuf; |
| 199 | |
| 200 | while (*ipos != 0) |
| 201 | { |
| 202 | if (*ipos == '=') |
| 203 | { |
| 204 | int decode_qp_result; |
| 205 | |
| 206 | ipos = mime_decode_qp_char(ipos, &decode_qp_result); |
| 207 | |
| 208 | if (decode_qp_result == -2) |
| 209 | { |
| 210 | /* Error from decoder. ipos is unchanged. */ |
| 211 | mime_set_anomaly(MIME_ANOMALY_BROKEN_QP); |
| 212 | *opos = '='; |
| 213 | ++opos; |
| 214 | ++ipos; |
| 215 | } |
| 216 | else if (decode_qp_result == -1) |
| 217 | break; |
| 218 | else if (decode_qp_result >= 0) |
| 219 | { |
| 220 | *opos = decode_qp_result; |
| 221 | ++opos; |
| 222 | } |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | *opos = *ipos; |
| 227 | ++opos; |
| 228 | ++ipos; |
| 229 | } |
| 230 | } |
| 231 | /* something to write? */ |
| 232 | len = opos - obuf; |
| 233 | if (len > 0) |
| 234 | { |
| 235 | if (fwrite(obuf, 1, len, out) != len) return -1; /* error */ |
| 236 | size += len; |
| 237 | } |
| 238 | } |
| 239 | return size; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | static FILE * |
| 244 | mime_get_decode_file(uschar *pname, uschar *fname) |
| 245 | { |
| 246 | FILE *f = NULL; |
| 247 | uschar *filename; |
| 248 | |
| 249 | filename = (uschar *)malloc(2048); |
| 250 | |
| 251 | if (pname && fname) |
| 252 | { |
| 253 | (void)string_format(filename, 2048, "%s/%s", pname, fname); |
| 254 | f = modefopen(filename,"wb+",SPOOL_MODE); |
| 255 | } |
| 256 | else if (!pname) |
| 257 | f = modefopen(fname,"wb+",SPOOL_MODE); |
| 258 | else if (!fname) |
| 259 | { |
| 260 | int file_nr = 0; |
| 261 | int result = 0; |
| 262 | |
| 263 | /* must find first free sequential filename */ |
| 264 | do |
| 265 | { |
| 266 | struct stat mystat; |
| 267 | (void)string_format(filename, 2048, |
| 268 | "%s/%s-%05u", pname, message_id, file_nr++); |
| 269 | /* security break */ |
| 270 | if (file_nr >= 1024) |
| 271 | break; |
| 272 | result = stat(CS filename, &mystat); |
| 273 | } while(result != -1); |
| 274 | |
| 275 | f = modefopen(filename, "wb+", SPOOL_MODE); |
| 276 | } |
| 277 | |
| 278 | /* set expansion variable */ |
| 279 | mime_decoded_filename = filename; |
| 280 | |
| 281 | return f; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | int |
| 286 | mime_decode(const uschar **listptr) |
| 287 | { |
| 288 | int sep = 0; |
| 289 | const uschar *list = *listptr; |
| 290 | uschar *option; |
| 291 | uschar option_buffer[1024]; |
| 292 | uschar decode_path[1024]; |
| 293 | FILE *decode_file = NULL; |
| 294 | long f_pos = 0; |
| 295 | ssize_t size_counter = 0; |
| 296 | ssize_t (*decode_function)(FILE*, FILE*, uschar*); |
| 297 | |
| 298 | if (mime_stream == NULL) |
| 299 | return FAIL; |
| 300 | |
| 301 | f_pos = ftell(mime_stream); |
| 302 | |
| 303 | /* build default decode path (will exist since MBOX must be spooled up) */ |
| 304 | (void)string_format(decode_path,1024,"%s/scan/%s",spool_directory,message_id); |
| 305 | |
| 306 | /* try to find 1st option */ |
| 307 | if ((option = string_nextinlist(&list, &sep, |
| 308 | option_buffer, |
| 309 | sizeof(option_buffer))) != NULL) |
| 310 | { |
| 311 | /* parse 1st option */ |
| 312 | if ( (Ustrcmp(option,"false") == 0) || (Ustrcmp(option,"0") == 0) ) |
| 313 | /* explicitly no decoding */ |
| 314 | return FAIL; |
| 315 | |
| 316 | if (Ustrcmp(option,"default") == 0) |
| 317 | /* explicit default path + file names */ |
| 318 | goto DEFAULT_PATH; |
| 319 | |
| 320 | if (option[0] == '/') |
| 321 | { |
| 322 | struct stat statbuf; |
| 323 | |
| 324 | memset(&statbuf,0,sizeof(statbuf)); |
| 325 | |
| 326 | /* assume either path or path+file name */ |
| 327 | if ( (stat(CS option, &statbuf) == 0) && S_ISDIR(statbuf.st_mode) ) |
| 328 | /* is directory, use it as decode_path */ |
| 329 | decode_file = mime_get_decode_file(option, NULL); |
| 330 | else |
| 331 | /* does not exist or is a file, use as full file name */ |
| 332 | decode_file = mime_get_decode_file(NULL, option); |
| 333 | } |
| 334 | else |
| 335 | /* assume file name only, use default path */ |
| 336 | decode_file = mime_get_decode_file(decode_path, option); |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | /* no option? patch default path */ |
| 341 | DEFAULT_PATH: |
| 342 | decode_file = mime_get_decode_file(decode_path, NULL); |
| 343 | } |
| 344 | |
| 345 | if (!decode_file) |
| 346 | return DEFER; |
| 347 | |
| 348 | /* decode according to mime type */ |
| 349 | decode_function = |
| 350 | !mime_content_transfer_encoding |
| 351 | ? mime_decode_asis /* no encoding, dump as-is */ |
| 352 | : Ustrcmp(mime_content_transfer_encoding, "base64") == 0 |
| 353 | ? mime_decode_base64 |
| 354 | : Ustrcmp(mime_content_transfer_encoding, "quoted-printable") == 0 |
| 355 | ? mime_decode_qp |
| 356 | : mime_decode_asis; /* unknown encoding type, just dump as-is */ |
| 357 | |
| 358 | size_counter = decode_function(mime_stream, decode_file, mime_current_boundary); |
| 359 | |
| 360 | clearerr(mime_stream); |
| 361 | fseek(mime_stream, f_pos, SEEK_SET); |
| 362 | |
| 363 | if (fclose(decode_file) != 0 || size_counter < 0) |
| 364 | return DEFER; |
| 365 | |
| 366 | /* round up to the next KiB */ |
| 367 | mime_content_size = (size_counter + 1023) / 1024; |
| 368 | |
| 369 | return OK; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | static int |
| 374 | mime_get_header(FILE *f, uschar *header) |
| 375 | { |
| 376 | int c = EOF; |
| 377 | int done = 0; |
| 378 | int header_value_mode = 0; |
| 379 | int header_open_brackets = 0; |
| 380 | int num_copied = 0; |
| 381 | |
| 382 | while(!done) |
| 383 | { |
| 384 | if ((c = fgetc(f)) == EOF) break; |
| 385 | |
| 386 | /* always skip CRs */ |
| 387 | if (c == '\r') continue; |
| 388 | |
| 389 | if (c == '\n') |
| 390 | { |
| 391 | if (num_copied > 0) |
| 392 | { |
| 393 | /* look if next char is '\t' or ' ' */ |
| 394 | if ((c = fgetc(f)) == EOF) break; |
| 395 | if ( (c == '\t') || (c == ' ') ) continue; |
| 396 | (void)ungetc(c,f); |
| 397 | } |
| 398 | /* end of the header, terminate with ';' */ |
| 399 | c = ';'; |
| 400 | done = 1; |
| 401 | } |
| 402 | |
| 403 | /* skip control characters */ |
| 404 | if (c < 32) continue; |
| 405 | |
| 406 | if (header_value_mode) |
| 407 | { |
| 408 | /* --------- value mode ----------- */ |
| 409 | /* skip leading whitespace */ |
| 410 | if ( ((c == '\t') || (c == ' ')) && (header_value_mode == 1) ) |
| 411 | continue; |
| 412 | |
| 413 | /* we have hit a non-whitespace char, start copying value data */ |
| 414 | header_value_mode = 2; |
| 415 | |
| 416 | if (c == '"') /* flip "quoted" mode */ |
| 417 | header_value_mode = header_value_mode==2 ? 3 : 2; |
| 418 | |
| 419 | /* leave value mode on unquoted ';' */ |
| 420 | if (header_value_mode == 2 && c == ';') { |
| 421 | header_value_mode = 0; |
| 422 | }; |
| 423 | /* -------------------------------- */ |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | /* -------- non-value mode -------- */ |
| 428 | /* skip whitespace + tabs */ |
| 429 | if ( (c == ' ') || (c == '\t') ) |
| 430 | continue; |
| 431 | if (c == '\\') |
| 432 | { |
| 433 | /* quote next char. can be used |
| 434 | to escape brackets. */ |
| 435 | if ((c = fgetc(f)) == EOF) break; |
| 436 | } |
| 437 | else if (c == '(') |
| 438 | { |
| 439 | header_open_brackets++; |
| 440 | continue; |
| 441 | } |
| 442 | else if ((c == ')') && header_open_brackets) |
| 443 | { |
| 444 | header_open_brackets--; |
| 445 | continue; |
| 446 | } |
| 447 | else if ( (c == '=') && !header_open_brackets ) /* enter value mode */ |
| 448 | header_value_mode = 1; |
| 449 | |
| 450 | /* skip chars while we are in a comment */ |
| 451 | if (header_open_brackets > 0) |
| 452 | continue; |
| 453 | /* -------------------------------- */ |
| 454 | } |
| 455 | |
| 456 | /* copy the char to the buffer */ |
| 457 | header[num_copied++] = (uschar)c; |
| 458 | |
| 459 | /* break if header buffer is full */ |
| 460 | if (num_copied > MIME_MAX_HEADER_SIZE-1) |
| 461 | done = 1; |
| 462 | } |
| 463 | |
| 464 | if ((num_copied > 0) && (header[num_copied-1] != ';')) |
| 465 | header[num_copied-1] = ';'; |
| 466 | |
| 467 | /* 0-terminate */ |
| 468 | header[num_copied] = '\0'; |
| 469 | |
| 470 | /* return 0 for EOF or empty line */ |
| 471 | if ((c == EOF) || (num_copied == 1)) |
| 472 | return 0; |
| 473 | else |
| 474 | return 1; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | static void |
| 479 | mime_vars_reset(void) |
| 480 | { |
| 481 | mime_anomaly_level = 0; |
| 482 | mime_anomaly_text = NULL; |
| 483 | mime_boundary = NULL; |
| 484 | mime_charset = NULL; |
| 485 | mime_decoded_filename = NULL; |
| 486 | mime_filename = NULL; |
| 487 | mime_content_description = NULL; |
| 488 | mime_content_disposition = NULL; |
| 489 | mime_content_id = NULL; |
| 490 | mime_content_transfer_encoding = NULL; |
| 491 | mime_content_type = NULL; |
| 492 | mime_is_multipart = 0; |
| 493 | mime_content_size = 0; |
| 494 | } |
| 495 | |
| 496 | |
| 497 | /* Grab a parameter value, dealing with quoting. |
| 498 | |
| 499 | Arguments: |
| 500 | str Input string. Updated on return to point to terminating ; or NUL |
| 501 | |
| 502 | Return: |
| 503 | Allocated string with parameter value |
| 504 | */ |
| 505 | static uschar * |
| 506 | mime_param_val(uschar ** sp) |
| 507 | { |
| 508 | uschar * s = *sp; |
| 509 | uschar * val = NULL; |
| 510 | int size = 0, ptr = 0; |
| 511 | |
| 512 | /* debug_printf(" considering paramval '%s'\n", s); */ |
| 513 | |
| 514 | while (*s && *s != ';') /* ; terminates */ |
| 515 | if (*s == '"') |
| 516 | { |
| 517 | s++; /* skip opening " */ |
| 518 | while (*s && *s != '"') /* " protects ; */ |
| 519 | val = string_cat(val, &size, &ptr, s++, 1); |
| 520 | if (*s) s++; /* skip closing " */ |
| 521 | } |
| 522 | else |
| 523 | val = string_cat(val, &size, &ptr, s++, 1); |
| 524 | if (val) val[ptr] = '\0'; |
| 525 | *sp = s; |
| 526 | return val; |
| 527 | } |
| 528 | |
| 529 | static uschar * |
| 530 | mime_next_semicolon(uschar * s) |
| 531 | { |
| 532 | while (*s && *s != ';') /* ; terminates */ |
| 533 | if (*s == '"') |
| 534 | { |
| 535 | s++; /* skip opening " */ |
| 536 | while (*s && *s != '"') /* " protects ; */ |
| 537 | s++; |
| 538 | if (*s) s++; /* skip closing " */ |
| 539 | } |
| 540 | else |
| 541 | s++; |
| 542 | return s; |
| 543 | } |
| 544 | |
| 545 | |
| 546 | int |
| 547 | mime_acl_check(uschar *acl, FILE *f, struct mime_boundary_context *context, |
| 548 | uschar **user_msgptr, uschar **log_msgptr) |
| 549 | { |
| 550 | int rc = OK; |
| 551 | uschar * header = NULL; |
| 552 | struct mime_boundary_context nested_context; |
| 553 | |
| 554 | /* reserve a line buffer to work in */ |
| 555 | header = store_get(MIME_MAX_HEADER_SIZE+1); |
| 556 | |
| 557 | /* Not actually used at the moment, but will be vital to fixing |
| 558 | * some RFC 2046 nonconformance later... */ |
| 559 | nested_context.parent = context; |
| 560 | |
| 561 | /* loop through parts */ |
| 562 | while(1) |
| 563 | { |
| 564 | /* reset all per-part mime variables */ |
| 565 | mime_vars_reset(); |
| 566 | |
| 567 | /* If boundary is null, we assume that *f is positioned on the start of |
| 568 | headers (for example, at the very beginning of a message. If a boundary is |
| 569 | given, we must first advance to it to reach the start of the next header |
| 570 | block. */ |
| 571 | |
| 572 | /* NOTE -- there's an error here -- RFC2046 specifically says to |
| 573 | * check for outer boundaries. This code doesn't do that, and |
| 574 | * I haven't fixed this. |
| 575 | * |
| 576 | * (I have moved partway towards adding support, however, by adding |
| 577 | * a "parent" field to my new boundary-context structure.) |
| 578 | */ |
| 579 | if (context) for (;;) |
| 580 | { |
| 581 | if (!fgets(CS header, MIME_MAX_HEADER_SIZE, f)) |
| 582 | { |
| 583 | /* Hit EOF or read error. Ugh. */ |
| 584 | DEBUG(D_acl) debug_printf("Hit EOF ...\n"); |
| 585 | return rc; |
| 586 | } |
| 587 | |
| 588 | /* boundary line must start with 2 dashes */ |
| 589 | if ( Ustrncmp(header, "--", 2) == 0 |
| 590 | && Ustrncmp(header+2, context->boundary, Ustrlen(context->boundary)) == 0 |
| 591 | ) |
| 592 | { /* found boundary */ |
| 593 | if (Ustrncmp((header+2+Ustrlen(context->boundary)), "--", 2) == 0) |
| 594 | { |
| 595 | /* END boundary found */ |
| 596 | DEBUG(D_acl) debug_printf("End boundary found %s\n", |
| 597 | context->boundary); |
| 598 | return rc; |
| 599 | } |
| 600 | |
| 601 | DEBUG(D_acl) debug_printf("Next part with boundary %s\n", |
| 602 | context->boundary); |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /* parse headers, set up expansion variables */ |
| 608 | while (mime_get_header(f, header)) |
| 609 | { |
| 610 | struct mime_header * mh; |
| 611 | |
| 612 | /* look for interesting headers */ |
| 613 | for (mh = mime_header_list; |
| 614 | mh < mime_header_list + mime_header_list_size; |
| 615 | mh++) if (strncmpic(mh->name, header, mh->namelen) == 0) |
| 616 | { |
| 617 | uschar * header_value = NULL; |
| 618 | int header_value_len = 0; |
| 619 | uschar * p = header + mh->namelen; |
| 620 | uschar * q; |
| 621 | |
| 622 | /* grab the value (normalize to lower case) |
| 623 | and copy to its corresponding expansion variable */ |
| 624 | |
| 625 | for (q = p; *q != ';' && *q; q++) ; |
| 626 | *mh->value = string_copynlc(p, q-p); |
| 627 | DEBUG(D_acl) debug_printf("found %s MIME header, value is '%s'\n", |
| 628 | mh->name, *mh->value); |
| 629 | |
| 630 | if (*(p = q)) p++; /* jump past the ; */ |
| 631 | |
| 632 | { |
| 633 | uschar * mime_fname = NULL; |
| 634 | uschar * mime_fname_rfc2231 = NULL; |
| 635 | uschar * mime_filename_charset = NULL; |
| 636 | BOOL decoding_failed = FALSE; |
| 637 | |
| 638 | /* grab all param=value tags on the remaining line, |
| 639 | check if they are interesting */ |
| 640 | |
| 641 | while (*p) |
| 642 | { |
| 643 | mime_parameter * mp; |
| 644 | |
| 645 | DEBUG(D_acl) debug_printf(" considering paramlist '%s'\n", p); |
| 646 | |
| 647 | if ( !mime_filename |
| 648 | && strncmpic("content-disposition:", header, 20) == 0 |
| 649 | && strncmpic("filename*", p, 9) == 0 |
| 650 | ) |
| 651 | { /* RFC 2231 filename */ |
| 652 | uschar * q; |
| 653 | |
| 654 | /* find value of the filename */ |
| 655 | p += 9; |
| 656 | while(*p != '=' && *p) p++; |
| 657 | if (*p) p++; /* p is filename or NUL */ |
| 658 | q = mime_param_val(&p); /* p now trailing ; or NUL */ |
| 659 | |
| 660 | if (q && *q) |
| 661 | { |
| 662 | uschar * temp_string, * err_msg; |
| 663 | int slen; |
| 664 | |
| 665 | /* build up an un-decoded filename over successive |
| 666 | filename*= parameters (for use when 2047 decode fails) */ |
| 667 | |
| 668 | mime_fname_rfc2231 = string_sprintf("%#s%s", |
| 669 | mime_fname_rfc2231, q); |
| 670 | |
| 671 | if (!decoding_failed) |
| 672 | { |
| 673 | int size; |
| 674 | if (!mime_filename_charset) |
| 675 | { |
| 676 | uschar * s = q; |
| 677 | |
| 678 | /* look for a ' in the "filename" */ |
| 679 | while(*s != '\'' && *s) s++; /* s is ' or NUL */ |
| 680 | |
| 681 | if ((size = s-q) > 0) |
| 682 | { |
| 683 | mime_filename_charset = string_copyn(q, size); |
| 684 | p = s; |
| 685 | |
| 686 | while(*p == '\'' && *p) p++; /* p is after ' */ |
| 687 | } |
| 688 | } |
| 689 | else |
| 690 | p = q; |
| 691 | |
| 692 | temp_string = expand_string(string_sprintf( |
| 693 | "=?%s?Q?${sg{%s}{\\N%%([\\dA-Fa-f]{2})\\N}{=\\$1}}?=", |
| 694 | mime_filename_charset, p)); |
| 695 | slen = Ustrlen(temp_string); |
| 696 | |
| 697 | temp_string = rfc2047_decode(temp_string, FALSE, NULL, 32, |
| 698 | NULL, &err_msg); |
| 699 | size = Ustrlen(temp_string); |
| 700 | |
| 701 | if (size == slen) |
| 702 | decoding_failed = TRUE; |
| 703 | else |
| 704 | /* build up a decoded filename over successive |
| 705 | filename*= parameters */ |
| 706 | |
| 707 | mime_filename = mime_fname = mime_fname |
| 708 | ? string_sprintf("%s%s", mime_fname, temp_string) |
| 709 | : temp_string; |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | else |
| 715 | /* look for interesting parameters */ |
| 716 | for (mp = mime_parameter_list; |
| 717 | mp < mime_parameter_list + nelem(mime_parameter_list); |
| 718 | mp++ |
| 719 | ) if (strncmpic(mp->name, p, mp->namelen) == 0) |
| 720 | { |
| 721 | uschar * q; |
| 722 | uschar * dummy_errstr; |
| 723 | |
| 724 | /* grab the value and copy to its expansion variable */ |
| 725 | p += mp->namelen; |
| 726 | q = mime_param_val(&p); /* p now trailing ; or NUL */ |
| 727 | |
| 728 | *mp->value = q && *q |
| 729 | ? rfc2047_decode(q, check_rfc2047_length, NULL, 32, NULL, |
| 730 | &dummy_errstr) |
| 731 | : NULL; |
| 732 | DEBUG(D_acl) debug_printf( |
| 733 | " found %s MIME parameter in %s header, value '%s'\n", |
| 734 | mp->name, mh->name, *mp->value); |
| 735 | |
| 736 | break; /* done matching param names */ |
| 737 | } |
| 738 | |
| 739 | |
| 740 | /* There is something, but not one of our interesting parameters. |
| 741 | Advance past the next semicolon */ |
| 742 | p = mime_next_semicolon(p); |
| 743 | if (*p) p++; |
| 744 | } /* param scan on line */ |
| 745 | |
| 746 | if (strncmpic("content-disposition:", header, 20) == 0) |
| 747 | { |
| 748 | if (decoding_failed) mime_filename = mime_fname_rfc2231; |
| 749 | |
| 750 | DEBUG(D_acl) debug_printf( |
| 751 | " found %s MIME parameter in %s header, value is '%s'\n", |
| 752 | "filename", mh->name, mime_filename); |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | /* set additional flag variables (easier access) */ |
| 759 | if ( mime_content_type |
| 760 | && Ustrncmp(mime_content_type,"multipart",9) == 0 |
| 761 | ) |
| 762 | mime_is_multipart = 1; |
| 763 | |
| 764 | /* Make a copy of the boundary pointer. |
| 765 | Required since mime_boundary is global |
| 766 | and can be overwritten further down in recursion */ |
| 767 | nested_context.boundary = mime_boundary; |
| 768 | |
| 769 | /* raise global counter */ |
| 770 | mime_part_count++; |
| 771 | |
| 772 | /* copy current file handle to global variable */ |
| 773 | mime_stream = f; |
| 774 | mime_current_boundary = context ? context->boundary : 0; |
| 775 | |
| 776 | /* Note the context */ |
| 777 | mime_is_coverletter = !(context && context->context == MBC_ATTACHMENT); |
| 778 | |
| 779 | /* call ACL handling function */ |
| 780 | rc = acl_check(ACL_WHERE_MIME, NULL, acl, user_msgptr, log_msgptr); |
| 781 | |
| 782 | mime_stream = NULL; |
| 783 | mime_current_boundary = NULL; |
| 784 | |
| 785 | if (rc != OK) break; |
| 786 | |
| 787 | /* If we have a multipart entity and a boundary, go recursive */ |
| 788 | if ( (mime_content_type != NULL) && |
| 789 | (nested_context.boundary != NULL) && |
| 790 | (Ustrncmp(mime_content_type,"multipart",9) == 0) ) |
| 791 | { |
| 792 | DEBUG(D_acl) debug_printf("Entering multipart recursion, boundary '%s'\n", |
| 793 | nested_context.boundary); |
| 794 | |
| 795 | nested_context.context = |
| 796 | context && context->context == MBC_ATTACHMENT |
| 797 | ? MBC_ATTACHMENT |
| 798 | : Ustrcmp(mime_content_type,"multipart/alternative") == 0 |
| 799 | || Ustrcmp(mime_content_type,"multipart/related") == 0 |
| 800 | ? MBC_COVERLETTER_ALL |
| 801 | : MBC_COVERLETTER_ONESHOT; |
| 802 | |
| 803 | rc = mime_acl_check(acl, f, &nested_context, user_msgptr, log_msgptr); |
| 804 | if (rc != OK) break; |
| 805 | } |
| 806 | else if ( (mime_content_type != NULL) && |
| 807 | (Ustrncmp(mime_content_type,"message/rfc822",14) == 0) ) |
| 808 | { |
| 809 | const uschar *rfc822name = NULL; |
| 810 | uschar filename[2048]; |
| 811 | int file_nr = 0; |
| 812 | int result = 0; |
| 813 | |
| 814 | /* must find first free sequential filename */ |
| 815 | do |
| 816 | { |
| 817 | struct stat mystat; |
| 818 | (void)string_format(filename, 2048, |
| 819 | "%s/scan/%s/__rfc822_%05u", spool_directory, message_id, file_nr++); |
| 820 | /* security break */ |
| 821 | if (file_nr >= 128) |
| 822 | goto NO_RFC822; |
| 823 | result = stat(CS filename,&mystat); |
| 824 | } while (result != -1); |
| 825 | |
| 826 | rfc822name = filename; |
| 827 | |
| 828 | /* decode RFC822 attachment */ |
| 829 | mime_decoded_filename = NULL; |
| 830 | mime_stream = f; |
| 831 | mime_current_boundary = context ? context->boundary : NULL; |
| 832 | mime_decode(&rfc822name); |
| 833 | mime_stream = NULL; |
| 834 | mime_current_boundary = NULL; |
| 835 | if (!mime_decoded_filename) /* decoding failed */ |
| 836 | { |
| 837 | log_write(0, LOG_MAIN, |
| 838 | "mime_regex acl condition warning - could not decode RFC822 MIME part to file."); |
| 839 | rc = DEFER; |
| 840 | goto out; |
| 841 | } |
| 842 | mime_decoded_filename = NULL; |
| 843 | } |
| 844 | |
| 845 | NO_RFC822: |
| 846 | /* If the boundary of this instance is NULL, we are finished here */ |
| 847 | if (!context) break; |
| 848 | |
| 849 | if (context->context == MBC_COVERLETTER_ONESHOT) |
| 850 | context->context = MBC_ATTACHMENT; |
| 851 | } |
| 852 | |
| 853 | out: |
| 854 | mime_vars_reset(); |
| 855 | return rc; |
| 856 | } |
| 857 | |
| 858 | #endif /*WITH_CONTENT_SCAN*/ |
| 859 | |
| 860 | /* vi: sw ai sw=2 |
| 861 | */ |