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