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