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