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