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