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