Commit | Line | Data |
---|---|---|
8523533c TK |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
f9ba5e22 | 5 | /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004 - 2015 |
80fea873 | 6 | * License: GPL |
f9ba5e22 | 7 | * Copyright (c) The Exim Maintainers 2015 - 2018 |
80fea873 | 8 | */ |
8523533c TK |
9 | |
10 | #include "exim.h" | |
c007c974 | 11 | #ifdef WITH_CONTENT_SCAN /* entire file */ |
8523533c TK |
12 | #include "mime.h" |
13 | #include <sys/stat.h> | |
14 | ||
15 | FILE *mime_stream = NULL; | |
16 | uschar *mime_current_boundary = NULL; | |
f2cb6292 JH |
17 | |
18 | static mime_header mime_header_list[] = { | |
f2ed27cf | 19 | /* name namelen value */ |
f2cb6292 JH |
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); | |
3c51463e JH |
28 | |
29 | static mime_parameter mime_parameter_list[] = { | |
f2ed27cf | 30 | /* name namelen value */ |
3c51463e JH |
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 | ||
8523533c TK |
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 | |
f4d091fb | 44 | encodings. Indexes are defined in mime.h. */ |
8523533c | 45 | |
f4d091fb JH |
46 | void |
47 | mime_set_anomaly(int idx) | |
c007c974 | 48 | { |
f4d091fb JH |
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; | |
f7b63901 | 57 | } |
8523533c TK |
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 | ||
f846c8f5 | 72 | static uschar * |
c007c974 JH |
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 | } | |
8e669ac1 | 89 | |
c007c974 JH |
90 | /* tab or whitespace may follow just ignore it if it precedes \n */ |
91 | while (*qp_p == '\t' || *qp_p == ' ' || *qp_p == '\r') | |
8523533c | 92 | qp_p++; |
8e669ac1 | 93 | |
c007c974 JH |
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; | |
8523533c TK |
103 | } |
104 | ||
105 | ||
baee9eee | 106 | /* just dump MIME part without any decoding */ |
eb02738d PP |
107 | static ssize_t |
108 | mime_decode_asis(FILE* in, FILE* out, uschar* boundary) | |
baee9eee | 109 | { |
eb02738d | 110 | ssize_t len, size = 0; |
baee9eee | 111 | uschar buffer[MIME_MAX_LINE_LENGTH]; |
d203e649 | 112 | |
c007c974 JH |
113 | while(fgets(CS buffer, MIME_MAX_LINE_LENGTH, mime_stream) != NULL) |
114 | { | |
baee9eee | 115 | if (boundary != NULL |
c007c974 JH |
116 | && Ustrncmp(buffer, "--", 2) == 0 |
117 | && Ustrncmp((buffer+2), boundary, Ustrlen(boundary)) == 0 | |
118 | ) | |
baee9eee NM |
119 | break; |
120 | ||
121 | len = Ustrlen(buffer); | |
122 | if (fwrite(buffer, 1, (size_t)len, out) < len) | |
123 | return -1; | |
124 | size += len; | |
c007c974 | 125 | } /* while */ |
baee9eee NM |
126 | return size; |
127 | } | |
84330b7b | 128 | |
8523533c | 129 | |
8e669ac1 | 130 | |
baee9eee | 131 | /* decode quoted-printable MIME part */ |
eb02738d PP |
132 | static ssize_t |
133 | mime_decode_qp(FILE* in, FILE* out, uschar* boundary) | |
baee9eee | 134 | { |
c007c974 JH |
135 | uschar ibuf[MIME_MAX_LINE_LENGTH], obuf[MIME_MAX_LINE_LENGTH]; |
136 | uschar *ipos, *opos; | |
137 | ssize_t len, size = 0; | |
baee9eee | 138 | |
c007c974 | 139 | while (fgets(CS ibuf, MIME_MAX_LINE_LENGTH, in) != NULL) |
baee9eee | 140 | { |
c007c974 JH |
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 */ | |
8523533c | 146 | |
c007c974 JH |
147 | ipos = ibuf; |
148 | opos = obuf; | |
8e669ac1 | 149 | |
c007c974 JH |
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); | |
f4bb363f | 162 | *opos++ = '='; |
c007c974 JH |
163 | ++ipos; |
164 | } | |
165 | else if (decode_qp_result == -1) | |
166 | break; | |
167 | else if (decode_qp_result >= 0) | |
f4bb363f | 168 | *opos++ = decode_qp_result; |
8523533c | 169 | } |
c007c974 | 170 | else |
f4bb363f | 171 | *opos++ = *ipos++; |
baee9eee | 172 | } |
c007c974 JH |
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; | |
baee9eee | 179 | } |
8523533c | 180 | } |
c007c974 | 181 | return size; |
8523533c TK |
182 | } |
183 | ||
184 | ||
f1d38a56 JH |
185 | /* |
186 | * Return open filehandle for combo of path and file. | |
187 | * Side-effect: set mime_decoded_filename, to copy in allocated mem | |
188 | */ | |
f846c8f5 | 189 | static FILE * |
c007c974 JH |
190 | mime_get_decode_file(uschar *pname, uschar *fname) |
191 | { | |
c007c974 | 192 | if (pname && fname) |
f1d38a56 | 193 | mime_decoded_filename = string_sprintf("%s/%s", pname, fname); |
c007c974 | 194 | else if (!pname) |
f1d38a56 | 195 | mime_decoded_filename = string_copy(fname); |
c007c974 JH |
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; | |
f1d38a56 | 205 | mime_decoded_filename = string_sprintf("%s/%s-%05u", pname, message_id, file_nr++); |
c007c974 JH |
206 | /* security break */ |
207 | if (file_nr >= 1024) | |
208 | break; | |
f1d38a56 | 209 | result = stat(CS mime_decoded_filename, &mystat); |
c007c974 | 210 | } while(result != -1); |
c007c974 | 211 | } |
8e669ac1 | 212 | |
f1d38a56 | 213 | return modefopen(mime_decoded_filename, "wb+", SPOOL_MODE); |
8523533c TK |
214 | } |
215 | ||
216 | ||
c007c974 | 217 | int |
55414b25 | 218 | mime_decode(const uschar **listptr) |
c007c974 JH |
219 | { |
220 | int sep = 0; | |
55414b25 | 221 | const uschar *list = *listptr; |
040721f2 JH |
222 | uschar * option; |
223 | uschar * decode_path; | |
c007c974 JH |
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 | ||
dc5d51dc | 229 | if (!mime_stream || (f_pos = ftell(mime_stream)) < 0) |
c007c974 JH |
230 | return FAIL; |
231 | ||
c007c974 | 232 | /* build default decode path (will exist since MBOX must be spooled up) */ |
040721f2 | 233 | decode_path = string_sprintf("%s/scan/%s", spool_directory, message_id); |
c007c974 JH |
234 | |
235 | /* try to find 1st option */ | |
040721f2 | 236 | if ((option = string_nextinlist(&list, &sep, NULL, 0))) |
c007c974 JH |
237 | { |
238 | /* parse 1st option */ | |
dc5d51dc | 239 | if ((Ustrcmp(option,"false") == 0) || (Ustrcmp(option,"0") == 0)) |
c007c974 | 240 | /* explicitly no decoding */ |
8523533c | 241 | return FAIL; |
8e669ac1 | 242 | |
c007c974 JH |
243 | if (Ustrcmp(option,"default") == 0) |
244 | /* explicit default path + file names */ | |
245 | goto DEFAULT_PATH; | |
8e669ac1 | 246 | |
c007c974 JH |
247 | if (option[0] == '/') |
248 | { | |
249 | struct stat statbuf; | |
8e669ac1 | 250 | |
c007c974 | 251 | memset(&statbuf,0,sizeof(statbuf)); |
8e669ac1 | 252 | |
c007c974 JH |
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); | |
8523533c | 257 | else |
c007c974 JH |
258 | /* does not exist or is a file, use as full file name */ |
259 | decode_file = mime_get_decode_file(NULL, option); | |
260 | } | |
baee9eee | 261 | else |
c007c974 JH |
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 | } | |
8e669ac1 | 271 | |
c007c974 JH |
272 | if (!decode_file) |
273 | return DEFER; | |
8e669ac1 | 274 | |
c007c974 JH |
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 */ | |
8e669ac1 | 284 | |
c007c974 | 285 | size_counter = decode_function(mime_stream, decode_file, mime_current_boundary); |
baee9eee | 286 | |
c007c974 | 287 | clearerr(mime_stream); |
d4ff61d1 JH |
288 | if (fseek(mime_stream, f_pos, SEEK_SET)) |
289 | return DEFER; | |
8e669ac1 | 290 | |
c007c974 JH |
291 | if (fclose(decode_file) != 0 || size_counter < 0) |
292 | return DEFER; | |
8523533c | 293 | |
c007c974 JH |
294 | /* round up to the next KiB */ |
295 | mime_content_size = (size_counter + 1023) / 1024; | |
8e669ac1 | 296 | |
c007c974 JH |
297 | return OK; |
298 | } | |
8e669ac1 | 299 | |
f846c8f5 JH |
300 | |
301 | static int | |
c007c974 JH |
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; | |
8e669ac1 | 309 | |
c007c974 JH |
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 | } | |
8e669ac1 | 330 | |
c007c974 JH |
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; | |
8e669ac1 | 340 | |
13c7874e JH |
341 | /* we have hit a non-whitespace char, start copying value data */ |
342 | header_value_mode = 2; | |
8e669ac1 | 343 | |
13c7874e JH |
344 | if (c == '"') /* flip "quoted" mode */ |
345 | header_value_mode = header_value_mode==2 ? 3 : 2; | |
8e669ac1 | 346 | |
13c7874e JH |
347 | /* leave value mode on unquoted ';' */ |
348 | if (header_value_mode == 2 && c == ';') | |
349 | header_value_mode = 0; | |
350 | /* -------------------------------- */ | |
8523533c | 351 | } |
c007c974 JH |
352 | else |
353 | { | |
354 | /* -------- non-value mode -------- */ | |
355 | /* skip whitespace + tabs */ | |
356 | if ( (c == ' ') || (c == '\t') ) | |
357 | continue; | |
358 | if (c == '\\') | |
359 | { | |
360 | /* quote next char. can be used | |
361 | to escape brackets. */ | |
362 | if ((c = fgetc(f)) == EOF) break; | |
8523533c | 363 | } |
c007c974 JH |
364 | else if (c == '(') |
365 | { | |
366 | header_open_brackets++; | |
367 | continue; | |
8523533c | 368 | } |
c007c974 JH |
369 | else if ((c == ')') && header_open_brackets) |
370 | { | |
371 | header_open_brackets--; | |
372 | continue; | |
8523533c | 373 | } |
c007c974 JH |
374 | else if ( (c == '=') && !header_open_brackets ) /* enter value mode */ |
375 | header_value_mode = 1; | |
8e669ac1 | 376 | |
c007c974 JH |
377 | /* skip chars while we are in a comment */ |
378 | if (header_open_brackets > 0) | |
379 | continue; | |
380 | /* -------------------------------- */ | |
381 | } | |
8e669ac1 | 382 | |
c007c974 JH |
383 | /* copy the char to the buffer */ |
384 | header[num_copied++] = (uschar)c; | |
8e669ac1 | 385 | |
c007c974 JH |
386 | /* break if header buffer is full */ |
387 | if (num_copied > MIME_MAX_HEADER_SIZE-1) | |
388 | done = 1; | |
389 | } | |
8523533c | 390 | |
c007c974 JH |
391 | if ((num_copied > 0) && (header[num_copied-1] != ';')) |
392 | header[num_copied-1] = ';'; | |
8523533c | 393 | |
c007c974 JH |
394 | /* 0-terminate */ |
395 | header[num_copied] = '\0'; | |
8e669ac1 | 396 | |
c007c974 | 397 | /* return 0 for EOF or empty line */ |
d7978c0f | 398 | return c == EOF || num_copied == 1 ? 0 : 1; |
8523533c TK |
399 | } |
400 | ||
401 | ||
f846c8f5 JH |
402 | static void |
403 | mime_vars_reset(void) | |
404 | { | |
405 | mime_anomaly_level = 0; | |
406 | mime_anomaly_text = NULL; | |
407 | mime_boundary = NULL; | |
408 | mime_charset = NULL; | |
409 | mime_decoded_filename = NULL; | |
410 | mime_filename = NULL; | |
411 | mime_content_description = NULL; | |
412 | mime_content_disposition = NULL; | |
413 | mime_content_id = NULL; | |
414 | mime_content_transfer_encoding = NULL; | |
415 | mime_content_type = NULL; | |
416 | mime_is_multipart = 0; | |
417 | mime_content_size = 0; | |
418 | } | |
419 | ||
420 | ||
421 | /* Grab a parameter value, dealing with quoting. | |
422 | ||
423 | Arguments: | |
424 | str Input string. Updated on return to point to terminating ; or NUL | |
425 | ||
426 | Return: | |
427 | Allocated string with parameter value | |
428 | */ | |
429 | static uschar * | |
430 | mime_param_val(uschar ** sp) | |
431 | { | |
432 | uschar * s = *sp; | |
acec9514 | 433 | gstring * val = NULL; |
f846c8f5 | 434 | |
e1d04f48 | 435 | /* debug_printf_indent(" considering paramval '%s'\n", s); */ |
f846c8f5 JH |
436 | |
437 | while (*s && *s != ';') /* ; terminates */ | |
438 | if (*s == '"') | |
439 | { | |
440 | s++; /* skip opening " */ | |
441 | while (*s && *s != '"') /* " protects ; */ | |
acec9514 | 442 | val = string_catn(val, s++, 1); |
f846c8f5 JH |
443 | if (*s) s++; /* skip closing " */ |
444 | } | |
445 | else | |
acec9514 | 446 | val = string_catn(val, s++, 1); |
f846c8f5 | 447 | *sp = s; |
acec9514 | 448 | return string_from_gstring(val); |
f846c8f5 JH |
449 | } |
450 | ||
451 | static uschar * | |
452 | mime_next_semicolon(uschar * s) | |
453 | { | |
454 | while (*s && *s != ';') /* ; terminates */ | |
455 | if (*s == '"') | |
456 | { | |
457 | s++; /* skip opening " */ | |
458 | while (*s && *s != '"') /* " protects ; */ | |
459 | s++; | |
460 | if (*s) s++; /* skip closing " */ | |
461 | } | |
462 | else | |
463 | s++; | |
464 | return s; | |
465 | } | |
466 | ||
467 | ||
627d1a1b JH |
468 | static uschar * |
469 | rfc2231_to_2047(const uschar * fname, const uschar * charset, int * len) | |
470 | { | |
acec9514 | 471 | gstring * val = string_catn(NULL, US"=?", 2); |
627d1a1b JH |
472 | uschar c; |
473 | ||
622dbd6a | 474 | if (charset) |
acec9514 JH |
475 | val = string_cat(val, charset); |
476 | val = string_catn(val, US"?Q?", 3); | |
627d1a1b JH |
477 | |
478 | while ((c = *fname)) | |
479 | if (c == '%' && isxdigit(fname[1]) && isxdigit(fname[2])) | |
480 | { | |
acec9514 JH |
481 | val = string_catn(val, US"=", 1); |
482 | val = string_catn(val, ++fname, 2); | |
627d1a1b JH |
483 | fname += 2; |
484 | } | |
485 | else | |
acec9514 | 486 | val = string_catn(val, fname++, 1); |
627d1a1b | 487 | |
acec9514 | 488 | val = string_catn(val, US"?=", 2); |
b378c1f3 | 489 | *len = val->ptr; |
acec9514 | 490 | return string_from_gstring(val); |
627d1a1b JH |
491 | } |
492 | ||
493 | ||
c007c974 JH |
494 | int |
495 | mime_acl_check(uschar *acl, FILE *f, struct mime_boundary_context *context, | |
f846c8f5 | 496 | uschar **user_msgptr, uschar **log_msgptr) |
c007c974 JH |
497 | { |
498 | int rc = OK; | |
f846c8f5 | 499 | uschar * header = NULL; |
c007c974 JH |
500 | struct mime_boundary_context nested_context; |
501 | ||
f3ebb786 JH |
502 | /* reserve a line buffer to work in. Assume tainted data. */ |
503 | header = store_get(MIME_MAX_HEADER_SIZE+1, TRUE); | |
c007c974 JH |
504 | |
505 | /* Not actually used at the moment, but will be vital to fixing | |
506 | * some RFC 2046 nonconformance later... */ | |
507 | nested_context.parent = context; | |
508 | ||
509 | /* loop through parts */ | |
510 | while(1) | |
511 | { | |
512 | /* reset all per-part mime variables */ | |
f846c8f5 JH |
513 | mime_vars_reset(); |
514 | ||
515 | /* If boundary is null, we assume that *f is positioned on the start of | |
516 | headers (for example, at the very beginning of a message. If a boundary is | |
517 | given, we must first advance to it to reach the start of the next header | |
518 | block. */ | |
c007c974 JH |
519 | |
520 | /* NOTE -- there's an error here -- RFC2046 specifically says to | |
521 | * check for outer boundaries. This code doesn't do that, and | |
522 | * I haven't fixed this. | |
523 | * | |
524 | * (I have moved partway towards adding support, however, by adding | |
525 | * a "parent" field to my new boundary-context structure.) | |
526 | */ | |
f846c8f5 | 527 | if (context) for (;;) |
c007c974 | 528 | { |
f846c8f5 | 529 | if (!fgets(CS header, MIME_MAX_HEADER_SIZE, f)) |
c007c974 | 530 | { |
f846c8f5 | 531 | /* Hit EOF or read error. Ugh. */ |
e1d04f48 | 532 | DEBUG(D_acl) debug_printf_indent("MIME: Hit EOF ...\n"); |
f846c8f5 JH |
533 | return rc; |
534 | } | |
5c6cf6a0 | 535 | |
f846c8f5 JH |
536 | /* boundary line must start with 2 dashes */ |
537 | if ( Ustrncmp(header, "--", 2) == 0 | |
538 | && Ustrncmp(header+2, context->boundary, Ustrlen(context->boundary)) == 0 | |
539 | ) | |
540 | { /* found boundary */ | |
541 | if (Ustrncmp((header+2+Ustrlen(context->boundary)), "--", 2) == 0) | |
542 | { | |
543 | /* END boundary found */ | |
e1d04f48 | 544 | DEBUG(D_acl) debug_printf_indent("MIME: End boundary found %s\n", |
f846c8f5 JH |
545 | context->boundary); |
546 | return rc; | |
c007c974 | 547 | } |
f846c8f5 | 548 | |
e1d04f48 | 549 | DEBUG(D_acl) debug_printf_indent("MIME: Next part with boundary %s\n", |
f846c8f5 JH |
550 | context->boundary); |
551 | break; | |
8523533c | 552 | } |
c007c974 | 553 | } |
8e669ac1 | 554 | |
c007c974 | 555 | /* parse headers, set up expansion variables */ |
5c6cf6a0 | 556 | while (mime_get_header(f, header)) |
f846c8f5 JH |
557 | |
558 | /* look for interesting headers */ | |
d7978c0f | 559 | for (struct mime_header * mh = mime_header_list; |
f846c8f5 JH |
560 | mh < mime_header_list + mime_header_list_size; |
561 | mh++) if (strncmpic(mh->name, header, mh->namelen) == 0) | |
562 | { | |
f846c8f5 JH |
563 | uschar * p = header + mh->namelen; |
564 | uschar * q; | |
c007c974 | 565 | |
f846c8f5 JH |
566 | /* grab the value (normalize to lower case) |
567 | and copy to its corresponding expansion variable */ | |
568 | ||
569 | for (q = p; *q != ';' && *q; q++) ; | |
570 | *mh->value = string_copynlc(p, q-p); | |
e1d04f48 | 571 | DEBUG(D_acl) debug_printf_indent("MIME: found %s header, value is '%s'\n", |
f846c8f5 JH |
572 | mh->name, *mh->value); |
573 | ||
574 | if (*(p = q)) p++; /* jump past the ; */ | |
575 | ||
576 | { | |
577 | uschar * mime_fname = NULL; | |
578 | uschar * mime_fname_rfc2231 = NULL; | |
579 | uschar * mime_filename_charset = NULL; | |
580 | BOOL decoding_failed = FALSE; | |
c007c974 | 581 | |
5c6cf6a0 JH |
582 | /* grab all param=value tags on the remaining line, |
583 | check if they are interesting */ | |
f846c8f5 | 584 | |
5c6cf6a0 | 585 | while (*p) |
c007c974 | 586 | { |
e1d04f48 | 587 | DEBUG(D_acl) debug_printf_indent("MIME: considering paramlist '%s'\n", p); |
f846c8f5 JH |
588 | |
589 | if ( !mime_filename | |
fc362fc5 JH |
590 | && strncmpic(CUS"content-disposition:", header, 20) == 0 |
591 | && strncmpic(CUS"filename*", p, 9) == 0 | |
f846c8f5 JH |
592 | ) |
593 | { /* RFC 2231 filename */ | |
594 | uschar * q; | |
595 | ||
596 | /* find value of the filename */ | |
597 | p += 9; | |
598 | while(*p != '=' && *p) p++; | |
599 | if (*p) p++; /* p is filename or NUL */ | |
600 | q = mime_param_val(&p); /* p now trailing ; or NUL */ | |
601 | ||
602 | if (q && *q) | |
c007c974 | 603 | { |
f846c8f5 JH |
604 | uschar * temp_string, * err_msg; |
605 | int slen; | |
4fd5d2bf | 606 | |
f846c8f5 JH |
607 | /* build up an un-decoded filename over successive |
608 | filename*= parameters (for use when 2047 decode fails) */ | |
609 | ||
610 | mime_fname_rfc2231 = string_sprintf("%#s%s", | |
611 | mime_fname_rfc2231, q); | |
612 | ||
613 | if (!decoding_failed) | |
614 | { | |
615 | int size; | |
616 | if (!mime_filename_charset) | |
4fd5d2bf | 617 | { |
f846c8f5 JH |
618 | uschar * s = q; |
619 | ||
620 | /* look for a ' in the "filename" */ | |
622dbd6a | 621 | while(*s != '\'' && *s) s++; /* s is 1st ' or NUL */ |
f846c8f5 JH |
622 | |
623 | if ((size = s-q) > 0) | |
f846c8f5 | 624 | mime_filename_charset = string_copyn(q, size); |
f846c8f5 | 625 | |
622dbd6a JH |
626 | if (*(p = s)) p++; |
627 | while(*p == '\'') p++; /* p is after 2nd ' */ | |
4fd5d2bf JH |
628 | } |
629 | else | |
f846c8f5 | 630 | p = q; |
5c6cf6a0 | 631 | |
e1d04f48 | 632 | DEBUG(D_acl) debug_printf_indent("MIME: charset %s fname '%s'\n", |
622dbd6a JH |
633 | mime_filename_charset ? mime_filename_charset : US"<NULL>", p); |
634 | ||
627d1a1b | 635 | temp_string = rfc2231_to_2047(p, mime_filename_charset, &slen); |
e1d04f48 | 636 | DEBUG(D_acl) debug_printf_indent("MIME: 2047-name %s\n", temp_string); |
622dbd6a JH |
637 | |
638 | temp_string = rfc2047_decode(temp_string, FALSE, NULL, ' ', | |
f846c8f5 | 639 | NULL, &err_msg); |
e1d04f48 | 640 | DEBUG(D_acl) debug_printf_indent("MIME: plain-name %s\n", temp_string); |
622dbd6a | 641 | |
b378c1f3 | 642 | if (!temp_string || (size = Ustrlen(temp_string)) == slen) |
f846c8f5 JH |
643 | decoding_failed = TRUE; |
644 | else | |
645 | /* build up a decoded filename over successive | |
646 | filename*= parameters */ | |
647 | ||
648 | mime_filename = mime_fname = mime_fname | |
649 | ? string_sprintf("%s%s", mime_fname, temp_string) | |
650 | : temp_string; | |
93cad488 | 651 | } |
f846c8f5 | 652 | } |
5c6cf6a0 | 653 | } |
f846c8f5 | 654 | |
e7c25d5b | 655 | else |
f846c8f5 | 656 | /* look for interesting parameters */ |
d7978c0f | 657 | for (mime_parameter * mp = mime_parameter_list; |
f846c8f5 JH |
658 | mp < mime_parameter_list + nelem(mime_parameter_list); |
659 | mp++ | |
660 | ) if (strncmpic(mp->name, p, mp->namelen) == 0) | |
661 | { | |
662 | uschar * q; | |
663 | uschar * dummy_errstr; | |
664 | ||
665 | /* grab the value and copy to its expansion variable */ | |
666 | p += mp->namelen; | |
667 | q = mime_param_val(&p); /* p now trailing ; or NUL */ | |
668 | ||
669 | *mp->value = q && *q | |
670 | ? rfc2047_decode(q, check_rfc2047_length, NULL, 32, NULL, | |
671 | &dummy_errstr) | |
672 | : NULL; | |
e1d04f48 | 673 | DEBUG(D_acl) debug_printf_indent( |
622dbd6a | 674 | "MIME: found %s parameter in %s header, value '%s'\n", |
f846c8f5 JH |
675 | mp->name, mh->name, *mp->value); |
676 | ||
677 | break; /* done matching param names */ | |
678 | } | |
679 | ||
680 | ||
681 | /* There is something, but not one of our interesting parameters. | |
682 | Advance past the next semicolon */ | |
683 | p = mime_next_semicolon(p); | |
684 | if (*p) p++; | |
685 | } /* param scan on line */ | |
686 | ||
fc362fc5 | 687 | if (strncmpic(CUS"content-disposition:", header, 20) == 0) |
f846c8f5 JH |
688 | { |
689 | if (decoding_failed) mime_filename = mime_fname_rfc2231; | |
690 | ||
e1d04f48 | 691 | DEBUG(D_acl) debug_printf_indent( |
622dbd6a | 692 | "MIME: found %s parameter in %s header, value is '%s'\n", |
f846c8f5 JH |
693 | "filename", mh->name, mime_filename); |
694 | } | |
c007c974 JH |
695 | } |
696 | } | |
8e669ac1 | 697 | |
c007c974 | 698 | /* set additional flag variables (easier access) */ |
f846c8f5 JH |
699 | if ( mime_content_type |
700 | && Ustrncmp(mime_content_type,"multipart",9) == 0 | |
701 | ) | |
c007c974 | 702 | mime_is_multipart = 1; |
8e669ac1 | 703 | |
c007c974 JH |
704 | /* Make a copy of the boundary pointer. |
705 | Required since mime_boundary is global | |
706 | and can be overwritten further down in recursion */ | |
707 | nested_context.boundary = mime_boundary; | |
8e669ac1 | 708 | |
c007c974 JH |
709 | /* raise global counter */ |
710 | mime_part_count++; | |
8523533c | 711 | |
c007c974 JH |
712 | /* copy current file handle to global variable */ |
713 | mime_stream = f; | |
714 | mime_current_boundary = context ? context->boundary : 0; | |
8e669ac1 | 715 | |
c007c974 JH |
716 | /* Note the context */ |
717 | mime_is_coverletter = !(context && context->context == MBC_ATTACHMENT); | |
8e669ac1 | 718 | |
c007c974 JH |
719 | /* call ACL handling function */ |
720 | rc = acl_check(ACL_WHERE_MIME, NULL, acl, user_msgptr, log_msgptr); | |
8e669ac1 | 721 | |
c007c974 JH |
722 | mime_stream = NULL; |
723 | mime_current_boundary = NULL; | |
724 | ||
725 | if (rc != OK) break; | |
8e669ac1 | 726 | |
c007c974 JH |
727 | /* If we have a multipart entity and a boundary, go recursive */ |
728 | if ( (mime_content_type != NULL) && | |
729 | (nested_context.boundary != NULL) && | |
730 | (Ustrncmp(mime_content_type,"multipart",9) == 0) ) | |
731 | { | |
622dbd6a | 732 | DEBUG(D_acl) |
e1d04f48 | 733 | debug_printf_indent("MIME: Entering multipart recursion, boundary '%s'\n", |
622dbd6a | 734 | nested_context.boundary); |
c007c974 JH |
735 | |
736 | nested_context.context = | |
737 | context && context->context == MBC_ATTACHMENT | |
738 | ? MBC_ATTACHMENT | |
739 | : Ustrcmp(mime_content_type,"multipart/alternative") == 0 | |
740 | || Ustrcmp(mime_content_type,"multipart/related") == 0 | |
741 | ? MBC_COVERLETTER_ALL | |
742 | : MBC_COVERLETTER_ONESHOT; | |
743 | ||
744 | rc = mime_acl_check(acl, f, &nested_context, user_msgptr, log_msgptr); | |
745 | if (rc != OK) break; | |
8523533c | 746 | } |
c007c974 JH |
747 | else if ( (mime_content_type != NULL) && |
748 | (Ustrncmp(mime_content_type,"message/rfc822",14) == 0) ) | |
749 | { | |
55414b25 | 750 | const uschar *rfc822name = NULL; |
c007c974 JH |
751 | uschar filename[2048]; |
752 | int file_nr = 0; | |
753 | int result = 0; | |
8e669ac1 | 754 | |
c007c974 JH |
755 | /* must find first free sequential filename */ |
756 | do | |
757 | { | |
758 | struct stat mystat; | |
759 | (void)string_format(filename, 2048, | |
760 | "%s/scan/%s/__rfc822_%05u", spool_directory, message_id, file_nr++); | |
761 | /* security break */ | |
762 | if (file_nr >= 128) | |
763 | goto NO_RFC822; | |
764 | result = stat(CS filename,&mystat); | |
765 | } while (result != -1); | |
766 | ||
767 | rfc822name = filename; | |
768 | ||
769 | /* decode RFC822 attachment */ | |
770 | mime_decoded_filename = NULL; | |
771 | mime_stream = f; | |
772 | mime_current_boundary = context ? context->boundary : NULL; | |
773 | mime_decode(&rfc822name); | |
774 | mime_stream = NULL; | |
775 | mime_current_boundary = NULL; | |
776 | if (!mime_decoded_filename) /* decoding failed */ | |
777 | { | |
778 | log_write(0, LOG_MAIN, | |
f1d38a56 | 779 | "MIME acl condition warning - could not decode RFC822 MIME part to file."); |
f846c8f5 JH |
780 | rc = DEFER; |
781 | goto out; | |
c007c974 JH |
782 | } |
783 | mime_decoded_filename = NULL; | |
784 | } | |
8523533c | 785 | |
c007c974 JH |
786 | NO_RFC822: |
787 | /* If the boundary of this instance is NULL, we are finished here */ | |
f846c8f5 | 788 | if (!context) break; |
8e669ac1 | 789 | |
c007c974 JH |
790 | if (context->context == MBC_COVERLETTER_ONESHOT) |
791 | context->context = MBC_ATTACHMENT; | |
792 | } | |
8523533c | 793 | |
f846c8f5 JH |
794 | out: |
795 | mime_vars_reset(); | |
c007c974 | 796 | return rc; |
8523533c TK |
797 | } |
798 | ||
c007c974 JH |
799 | #endif /*WITH_CONTENT_SCAN*/ |
800 | ||
801 | /* vi: sw ai sw=2 | |
802 | */ |