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