String handling: refactor the expanding-string routines and users to use a descriptor...
[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[] = {
f2ed27cf 19 /* name namelen value */
f2cb6292
JH
20 { US"content-type:", 13, &mime_content_type },
21 { US"content-disposition:", 20, &mime_content_disposition },
22 { US"content-transfer-encoding:", 26, &mime_content_transfer_encoding },
23 { US"content-id:", 11, &mime_content_id },
24 { US"content-description:", 20, &mime_content_description }
25};
26
27static int mime_header_list_size = nelem(mime_header_list);
3c51463e
JH
28
29static mime_parameter mime_parameter_list[] = {
f2ed27cf 30 /* name namelen value */
3c51463e
JH
31 { US"name=", 5, &mime_filename },
32 { US"filename=", 9, &mime_filename },
33 { US"charset=", 8, &mime_charset },
34 { US"boundary=", 9, &mime_boundary }
35};
36
8523533c
TK
37
38/*************************************************
39* set MIME anomaly level + text *
40*************************************************/
41
42/* Small wrapper to set the two expandables which
43 give info on detected "problems" in MIME
f4d091fb 44 encodings. Indexes are defined in mime.h. */
8523533c 45
f4d091fb
JH
46void
47mime_set_anomaly(int idx)
c007c974 48{
f4d091fb
JH
49struct anom {
50 int level;
51 const uschar * text;
52} anom[] = { {1, CUS"Broken Quoted-Printable encoding detected"},
53 {2, CUS"Broken BASE64 encoding detected"} };
54
55mime_anomaly_level = anom[idx].level;
56mime_anomaly_text = anom[idx].text;
f7b63901 57}
8523533c
TK
58
59
60/*************************************************
61* decode quoted-printable chars *
62*************************************************/
63
64/* gets called when we hit a =
65 returns: new pointer position
66 result code in c:
67 -2 - decode error
68 -1 - soft line break, no char
69 0-255 - char to write
70*/
71
f846c8f5 72static uschar *
c007c974
JH
73mime_decode_qp_char(uschar *qp_p, int *c)
74{
75uschar *initial_pos = qp_p;
76
77/* advance one char */
78qp_p++;
79
80/* Check for two hex digits and decode them */
81if (isxdigit(*qp_p) && isxdigit(qp_p[1]))
82 {
83 /* Do hex conversion */
84 *c = (isdigit(*qp_p) ? *qp_p - '0' : toupper(*qp_p) - 'A' + 10) <<4;
85 qp_p++;
86 *c |= isdigit(*qp_p) ? *qp_p - '0' : toupper(*qp_p) - 'A' + 10;
87 return qp_p + 1;
88 }
8e669ac1 89
c007c974
JH
90/* tab or whitespace may follow just ignore it if it precedes \n */
91while (*qp_p == '\t' || *qp_p == ' ' || *qp_p == '\r')
8523533c 92 qp_p++;
8e669ac1 93
c007c974
JH
94if (*qp_p == '\n') /* hit soft line break */
95 {
96 *c = -1;
97 return qp_p;
98 }
99
100/* illegal char here */
101*c = -2;
102return initial_pos;
8523533c
TK
103}
104
105
baee9eee 106/* just dump MIME part without any decoding */
eb02738d
PP
107static ssize_t
108mime_decode_asis(FILE* in, FILE* out, uschar* boundary)
baee9eee 109{
eb02738d 110 ssize_t len, size = 0;
baee9eee 111 uschar buffer[MIME_MAX_LINE_LENGTH];
d203e649 112
c007c974
JH
113 while(fgets(CS buffer, MIME_MAX_LINE_LENGTH, mime_stream) != NULL)
114 {
baee9eee 115 if (boundary != NULL
c007c974
JH
116 && Ustrncmp(buffer, "--", 2) == 0
117 && Ustrncmp((buffer+2), boundary, Ustrlen(boundary)) == 0
118 )
baee9eee
NM
119 break;
120
121 len = Ustrlen(buffer);
122 if (fwrite(buffer, 1, (size_t)len, out) < len)
123 return -1;
124 size += len;
c007c974 125 } /* while */
baee9eee
NM
126 return size;
127}
84330b7b 128
8523533c 129
8e669ac1 130
baee9eee 131/* decode quoted-printable MIME part */
eb02738d
PP
132static ssize_t
133mime_decode_qp(FILE* in, FILE* out, uschar* boundary)
baee9eee 134{
c007c974
JH
135uschar ibuf[MIME_MAX_LINE_LENGTH], obuf[MIME_MAX_LINE_LENGTH];
136uschar *ipos, *opos;
137ssize_t len, size = 0;
baee9eee 138
c007c974 139while (fgets(CS ibuf, MIME_MAX_LINE_LENGTH, in) != NULL)
baee9eee 140 {
c007c974
JH
141 if (boundary != NULL
142 && Ustrncmp(ibuf, "--", 2) == 0
143 && Ustrncmp((ibuf+2), boundary, Ustrlen(boundary)) == 0
144 )
145 break; /* todo: check for missing boundary */
8523533c 146
c007c974
JH
147 ipos = ibuf;
148 opos = obuf;
8e669ac1 149
c007c974
JH
150 while (*ipos != 0)
151 {
152 if (*ipos == '=')
153 {
154 int decode_qp_result;
155
156 ipos = mime_decode_qp_char(ipos, &decode_qp_result);
157
158 if (decode_qp_result == -2)
159 {
160 /* Error from decoder. ipos is unchanged. */
161 mime_set_anomaly(MIME_ANOMALY_BROKEN_QP);
f4bb363f 162 *opos++ = '=';
c007c974
JH
163 ++ipos;
164 }
165 else if (decode_qp_result == -1)
166 break;
167 else if (decode_qp_result >= 0)
f4bb363f 168 *opos++ = decode_qp_result;
8523533c 169 }
c007c974 170 else
f4bb363f 171 *opos++ = *ipos++;
baee9eee 172 }
c007c974
JH
173 /* something to write? */
174 len = opos - obuf;
175 if (len > 0)
176 {
177 if (fwrite(obuf, 1, len, out) != len) return -1; /* error */
178 size += len;
baee9eee 179 }
8523533c 180 }
c007c974 181return size;
8523533c
TK
182}
183
184
f1d38a56
JH
185/*
186 * Return open filehandle for combo of path and file.
187 * Side-effect: set mime_decoded_filename, to copy in allocated mem
188 */
f846c8f5 189static FILE *
c007c974
JH
190mime_get_decode_file(uschar *pname, uschar *fname)
191{
c007c974 192if (pname && fname)
f1d38a56 193 mime_decoded_filename = string_sprintf("%s/%s", pname, fname);
c007c974 194else if (!pname)
f1d38a56 195 mime_decoded_filename = string_copy(fname);
c007c974
JH
196else if (!fname)
197 {
198 int file_nr = 0;
199 int result = 0;
200
201 /* must find first free sequential filename */
202 do
203 {
204 struct stat mystat;
f1d38a56 205 mime_decoded_filename = string_sprintf("%s/%s-%05u", pname, message_id, file_nr++);
c007c974
JH
206 /* security break */
207 if (file_nr >= 1024)
208 break;
f1d38a56 209 result = stat(CS mime_decoded_filename, &mystat);
c007c974 210 } while(result != -1);
c007c974 211 }
8e669ac1 212
f1d38a56 213return modefopen(mime_decoded_filename, "wb+", SPOOL_MODE);
8523533c
TK
214}
215
216
c007c974 217int
55414b25 218mime_decode(const uschar **listptr)
c007c974
JH
219{
220int sep = 0;
55414b25 221const uschar *list = *listptr;
040721f2
JH
222uschar * option;
223uschar * decode_path;
c007c974
JH
224FILE *decode_file = NULL;
225long f_pos = 0;
226ssize_t size_counter = 0;
227ssize_t (*decode_function)(FILE*, FILE*, uschar*);
228
dc5d51dc 229if (!mime_stream || (f_pos = ftell(mime_stream)) < 0)
c007c974
JH
230 return FAIL;
231
c007c974 232/* build default decode path (will exist since MBOX must be spooled up) */
040721f2 233decode_path = string_sprintf("%s/scan/%s", spool_directory, message_id);
c007c974
JH
234
235/* try to find 1st option */
040721f2 236if ((option = string_nextinlist(&list, &sep, NULL, 0)))
c007c974
JH
237 {
238 /* parse 1st option */
dc5d51dc 239 if ((Ustrcmp(option,"false") == 0) || (Ustrcmp(option,"0") == 0))
c007c974 240 /* explicitly no decoding */
8523533c 241 return FAIL;
8e669ac1 242
c007c974
JH
243 if (Ustrcmp(option,"default") == 0)
244 /* explicit default path + file names */
245 goto DEFAULT_PATH;
8e669ac1 246
c007c974
JH
247 if (option[0] == '/')
248 {
249 struct stat statbuf;
8e669ac1 250
c007c974 251 memset(&statbuf,0,sizeof(statbuf));
8e669ac1 252
c007c974
JH
253 /* assume either path or path+file name */
254 if ( (stat(CS option, &statbuf) == 0) && S_ISDIR(statbuf.st_mode) )
255 /* is directory, use it as decode_path */
256 decode_file = mime_get_decode_file(option, NULL);
8523533c 257 else
c007c974
JH
258 /* does not exist or is a file, use as full file name */
259 decode_file = mime_get_decode_file(NULL, option);
260 }
baee9eee 261 else
c007c974
JH
262 /* assume file name only, use default path */
263 decode_file = mime_get_decode_file(decode_path, option);
264 }
265else
266 {
267 /* no option? patch default path */
268DEFAULT_PATH:
269 decode_file = mime_get_decode_file(decode_path, NULL);
270 }
8e669ac1 271
c007c974
JH
272if (!decode_file)
273 return DEFER;
8e669ac1 274
c007c974
JH
275/* decode according to mime type */
276decode_function =
277 !mime_content_transfer_encoding
278 ? mime_decode_asis /* no encoding, dump as-is */
279 : Ustrcmp(mime_content_transfer_encoding, "base64") == 0
280 ? mime_decode_base64
281 : Ustrcmp(mime_content_transfer_encoding, "quoted-printable") == 0
282 ? mime_decode_qp
283 : mime_decode_asis; /* unknown encoding type, just dump as-is */
8e669ac1 284
c007c974 285size_counter = decode_function(mime_stream, decode_file, mime_current_boundary);
baee9eee 286
c007c974 287clearerr(mime_stream);
d4ff61d1
JH
288if (fseek(mime_stream, f_pos, SEEK_SET))
289 return DEFER;
8e669ac1 290
c007c974
JH
291if (fclose(decode_file) != 0 || size_counter < 0)
292 return DEFER;
8523533c 293
c007c974
JH
294/* round up to the next KiB */
295mime_content_size = (size_counter + 1023) / 1024;
8e669ac1 296
c007c974
JH
297return OK;
298}
8e669ac1 299
f846c8f5
JH
300
301static int
c007c974
JH
302mime_get_header(FILE *f, uschar *header)
303{
304int c = EOF;
305int done = 0;
306int header_value_mode = 0;
307int header_open_brackets = 0;
308int num_copied = 0;
8e669ac1 309
c007c974
JH
310while(!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
392if ((num_copied > 0) && (header[num_copied-1] != ';'))
393 header[num_copied-1] = ';';
8523533c 394
c007c974
JH
395/* 0-terminate */
396header[num_copied] = '\0';
8e669ac1 397
c007c974
JH
398/* return 0 for EOF or empty line */
399if ((c == EOF) || (num_copied == 1))
400 return 0;
401else
402 return 1;
8523533c
TK
403}
404
405
f846c8f5
JH
406static void
407mime_vars_reset(void)
408{
409mime_anomaly_level = 0;
410mime_anomaly_text = NULL;
411mime_boundary = NULL;
412mime_charset = NULL;
413mime_decoded_filename = NULL;
414mime_filename = NULL;
415mime_content_description = NULL;
416mime_content_disposition = NULL;
417mime_content_id = NULL;
418mime_content_transfer_encoding = NULL;
419mime_content_type = NULL;
420mime_is_multipart = 0;
421mime_content_size = 0;
422}
423
424
425/* Grab a parameter value, dealing with quoting.
426
427Arguments:
428 str Input string. Updated on return to point to terminating ; or NUL
429
430Return:
431 Allocated string with parameter value
432*/
433static uschar *
434mime_param_val(uschar ** sp)
435{
436uschar * s = *sp;
acec9514 437gstring * val = NULL;
f846c8f5 438
e1d04f48 439/* debug_printf_indent(" considering paramval '%s'\n", s); */
f846c8f5
JH
440
441while (*s && *s != ';') /* ; terminates */
442 if (*s == '"')
443 {
444 s++; /* skip opening " */
445 while (*s && *s != '"') /* " protects ; */
acec9514 446 val = string_catn(val, s++, 1);
f846c8f5
JH
447 if (*s) s++; /* skip closing " */
448 }
449 else
acec9514 450 val = string_catn(val, s++, 1);
f846c8f5 451*sp = s;
acec9514 452return string_from_gstring(val);
f846c8f5
JH
453}
454
455static uschar *
456mime_next_semicolon(uschar * s)
457{
458while (*s && *s != ';') /* ; terminates */
459 if (*s == '"')
460 {
461 s++; /* skip opening " */
462 while (*s && *s != '"') /* " protects ; */
463 s++;
464 if (*s) s++; /* skip closing " */
465 }
466 else
467 s++;
468return s;
469}
470
471
627d1a1b
JH
472static uschar *
473rfc2231_to_2047(const uschar * fname, const uschar * charset, int * len)
474{
acec9514 475gstring * val = string_catn(NULL, US"=?", 2);
627d1a1b
JH
476uschar c;
477
622dbd6a 478if (charset)
acec9514
JH
479 val = string_cat(val, charset);
480val = string_catn(val, US"?Q?", 3);
627d1a1b
JH
481
482while ((c = *fname))
483 if (c == '%' && isxdigit(fname[1]) && isxdigit(fname[2]))
484 {
acec9514
JH
485 val = string_catn(val, US"=", 1);
486 val = string_catn(val, ++fname, 2);
627d1a1b
JH
487 fname += 2;
488 }
489 else
acec9514 490 val = string_catn(val, fname++, 1);
627d1a1b 491
acec9514
JH
492val = string_catn(val, US"?=", 2);
493return string_from_gstring(val);
627d1a1b
JH
494}
495
496
c007c974
JH
497int
498mime_acl_check(uschar *acl, FILE *f, struct mime_boundary_context *context,
f846c8f5 499 uschar **user_msgptr, uschar **log_msgptr)
c007c974
JH
500{
501int rc = OK;
f846c8f5 502uschar * header = NULL;
c007c974
JH
503struct mime_boundary_context nested_context;
504
505/* reserve a line buffer to work in */
f846c8f5 506header = store_get(MIME_MAX_HEADER_SIZE+1);
c007c974
JH
507
508/* Not actually used at the moment, but will be vital to fixing
509 * some RFC 2046 nonconformance later... */
510nested_context.parent = context;
511
512/* loop through parts */
513while(1)
514 {
515 /* reset all per-part mime variables */
f846c8f5
JH
516 mime_vars_reset();
517
518 /* If boundary is null, we assume that *f is positioned on the start of
519 headers (for example, at the very beginning of a message. If a boundary is
520 given, we must first advance to it to reach the start of the next header
521 block. */
c007c974
JH
522
523 /* NOTE -- there's an error here -- RFC2046 specifically says to
524 * check for outer boundaries. This code doesn't do that, and
525 * I haven't fixed this.
526 *
527 * (I have moved partway towards adding support, however, by adding
528 * a "parent" field to my new boundary-context structure.)
529 */
f846c8f5 530 if (context) for (;;)
c007c974 531 {
f846c8f5 532 if (!fgets(CS header, MIME_MAX_HEADER_SIZE, f))
c007c974 533 {
f846c8f5 534 /* Hit EOF or read error. Ugh. */
e1d04f48 535 DEBUG(D_acl) debug_printf_indent("MIME: Hit EOF ...\n");
f846c8f5
JH
536 return rc;
537 }
5c6cf6a0 538
f846c8f5
JH
539 /* boundary line must start with 2 dashes */
540 if ( Ustrncmp(header, "--", 2) == 0
541 && Ustrncmp(header+2, context->boundary, Ustrlen(context->boundary)) == 0
542 )
543 { /* found boundary */
544 if (Ustrncmp((header+2+Ustrlen(context->boundary)), "--", 2) == 0)
545 {
546 /* END boundary found */
e1d04f48 547 DEBUG(D_acl) debug_printf_indent("MIME: End boundary found %s\n",
f846c8f5
JH
548 context->boundary);
549 return rc;
c007c974 550 }
f846c8f5 551
e1d04f48 552 DEBUG(D_acl) debug_printf_indent("MIME: Next part with boundary %s\n",
f846c8f5
JH
553 context->boundary);
554 break;
8523533c 555 }
c007c974 556 }
8e669ac1 557
c007c974 558 /* parse headers, set up expansion variables */
5c6cf6a0 559 while (mime_get_header(f, header))
c007c974 560 {
f846c8f5
JH
561 struct mime_header * mh;
562
563 /* look for interesting headers */
564 for (mh = mime_header_list;
565 mh < mime_header_list + mime_header_list_size;
566 mh++) if (strncmpic(mh->name, header, mh->namelen) == 0)
567 {
f846c8f5
JH
568 uschar * p = header + mh->namelen;
569 uschar * q;
c007c974 570
f846c8f5
JH
571 /* grab the value (normalize to lower case)
572 and copy to its corresponding expansion variable */
573
574 for (q = p; *q != ';' && *q; q++) ;
575 *mh->value = string_copynlc(p, q-p);
e1d04f48 576 DEBUG(D_acl) debug_printf_indent("MIME: found %s header, value is '%s'\n",
f846c8f5
JH
577 mh->name, *mh->value);
578
579 if (*(p = q)) p++; /* jump past the ; */
580
581 {
582 uschar * mime_fname = NULL;
583 uschar * mime_fname_rfc2231 = NULL;
584 uschar * mime_filename_charset = NULL;
585 BOOL decoding_failed = FALSE;
c007c974 586
5c6cf6a0
JH
587 /* grab all param=value tags on the remaining line,
588 check if they are interesting */
f846c8f5 589
5c6cf6a0 590 while (*p)
c007c974
JH
591 {
592 mime_parameter * mp;
f846c8f5 593
e1d04f48 594 DEBUG(D_acl) debug_printf_indent("MIME: considering paramlist '%s'\n", p);
f846c8f5
JH
595
596 if ( !mime_filename
fc362fc5
JH
597 && strncmpic(CUS"content-disposition:", header, 20) == 0
598 && strncmpic(CUS"filename*", p, 9) == 0
f846c8f5
JH
599 )
600 { /* RFC 2231 filename */
601 uschar * q;
602
603 /* find value of the filename */
604 p += 9;
605 while(*p != '=' && *p) p++;
606 if (*p) p++; /* p is filename or NUL */
607 q = mime_param_val(&p); /* p now trailing ; or NUL */
608
609 if (q && *q)
c007c974 610 {
f846c8f5
JH
611 uschar * temp_string, * err_msg;
612 int slen;
4fd5d2bf 613
f846c8f5
JH
614 /* build up an un-decoded filename over successive
615 filename*= parameters (for use when 2047 decode fails) */
616
617 mime_fname_rfc2231 = string_sprintf("%#s%s",
618 mime_fname_rfc2231, q);
619
620 if (!decoding_failed)
621 {
622 int size;
623 if (!mime_filename_charset)
4fd5d2bf 624 {
f846c8f5
JH
625 uschar * s = q;
626
627 /* look for a ' in the "filename" */
622dbd6a 628 while(*s != '\'' && *s) s++; /* s is 1st ' or NUL */
f846c8f5
JH
629
630 if ((size = s-q) > 0)
f846c8f5 631 mime_filename_charset = string_copyn(q, size);
f846c8f5 632
622dbd6a
JH
633 if (*(p = s)) p++;
634 while(*p == '\'') p++; /* p is after 2nd ' */
4fd5d2bf
JH
635 }
636 else
f846c8f5 637 p = q;
5c6cf6a0 638
e1d04f48 639 DEBUG(D_acl) debug_printf_indent("MIME: charset %s fname '%s'\n",
622dbd6a
JH
640 mime_filename_charset ? mime_filename_charset : US"<NULL>", p);
641
627d1a1b 642 temp_string = rfc2231_to_2047(p, mime_filename_charset, &slen);
e1d04f48 643 DEBUG(D_acl) debug_printf_indent("MIME: 2047-name %s\n", temp_string);
622dbd6a
JH
644
645 temp_string = rfc2047_decode(temp_string, FALSE, NULL, ' ',
f846c8f5 646 NULL, &err_msg);
e1d04f48 647 DEBUG(D_acl) debug_printf_indent("MIME: plain-name %s\n", temp_string);
622dbd6a 648
f846c8f5
JH
649 size = Ustrlen(temp_string);
650
651 if (size == slen)
652 decoding_failed = TRUE;
653 else
654 /* build up a decoded filename over successive
655 filename*= parameters */
656
657 mime_filename = mime_fname = mime_fname
658 ? string_sprintf("%s%s", mime_fname, temp_string)
659 : temp_string;
93cad488 660 }
f846c8f5 661 }
5c6cf6a0 662 }
f846c8f5 663
e7c25d5b 664 else
f846c8f5
JH
665 /* look for interesting parameters */
666 for (mp = mime_parameter_list;
667 mp < mime_parameter_list + nelem(mime_parameter_list);
668 mp++
669 ) if (strncmpic(mp->name, p, mp->namelen) == 0)
670 {
671 uschar * q;
672 uschar * dummy_errstr;
673
674 /* grab the value and copy to its expansion variable */
675 p += mp->namelen;
676 q = mime_param_val(&p); /* p now trailing ; or NUL */
677
678 *mp->value = q && *q
679 ? rfc2047_decode(q, check_rfc2047_length, NULL, 32, NULL,
680 &dummy_errstr)
681 : NULL;
e1d04f48 682 DEBUG(D_acl) debug_printf_indent(
622dbd6a 683 "MIME: found %s parameter in %s header, value '%s'\n",
f846c8f5
JH
684 mp->name, mh->name, *mp->value);
685
686 break; /* done matching param names */
687 }
688
689
690 /* There is something, but not one of our interesting parameters.
691 Advance past the next semicolon */
692 p = mime_next_semicolon(p);
693 if (*p) p++;
694 } /* param scan on line */
695
fc362fc5 696 if (strncmpic(CUS"content-disposition:", header, 20) == 0)
f846c8f5
JH
697 {
698 if (decoding_failed) mime_filename = mime_fname_rfc2231;
699
e1d04f48 700 DEBUG(D_acl) debug_printf_indent(
622dbd6a 701 "MIME: found %s parameter in %s header, value is '%s'\n",
f846c8f5
JH
702 "filename", mh->name, mime_filename);
703 }
c007c974
JH
704 }
705 }
f846c8f5 706 }
8e669ac1 707
c007c974 708 /* set additional flag variables (easier access) */
f846c8f5
JH
709 if ( mime_content_type
710 && Ustrncmp(mime_content_type,"multipart",9) == 0
711 )
c007c974 712 mime_is_multipart = 1;
8e669ac1 713
c007c974
JH
714 /* Make a copy of the boundary pointer.
715 Required since mime_boundary is global
716 and can be overwritten further down in recursion */
717 nested_context.boundary = mime_boundary;
8e669ac1 718
c007c974
JH
719 /* raise global counter */
720 mime_part_count++;
8523533c 721
c007c974
JH
722 /* copy current file handle to global variable */
723 mime_stream = f;
724 mime_current_boundary = context ? context->boundary : 0;
8e669ac1 725
c007c974
JH
726 /* Note the context */
727 mime_is_coverletter = !(context && context->context == MBC_ATTACHMENT);
8e669ac1 728
c007c974
JH
729 /* call ACL handling function */
730 rc = acl_check(ACL_WHERE_MIME, NULL, acl, user_msgptr, log_msgptr);
8e669ac1 731
c007c974
JH
732 mime_stream = NULL;
733 mime_current_boundary = NULL;
734
735 if (rc != OK) break;
8e669ac1 736
c007c974
JH
737 /* If we have a multipart entity and a boundary, go recursive */
738 if ( (mime_content_type != NULL) &&
739 (nested_context.boundary != NULL) &&
740 (Ustrncmp(mime_content_type,"multipart",9) == 0) )
741 {
622dbd6a 742 DEBUG(D_acl)
e1d04f48 743 debug_printf_indent("MIME: Entering multipart recursion, boundary '%s'\n",
622dbd6a 744 nested_context.boundary);
c007c974
JH
745
746 nested_context.context =
747 context && context->context == MBC_ATTACHMENT
748 ? MBC_ATTACHMENT
749 : Ustrcmp(mime_content_type,"multipart/alternative") == 0
750 || Ustrcmp(mime_content_type,"multipart/related") == 0
751 ? MBC_COVERLETTER_ALL
752 : MBC_COVERLETTER_ONESHOT;
753
754 rc = mime_acl_check(acl, f, &nested_context, user_msgptr, log_msgptr);
755 if (rc != OK) break;
8523533c 756 }
c007c974
JH
757 else if ( (mime_content_type != NULL) &&
758 (Ustrncmp(mime_content_type,"message/rfc822",14) == 0) )
759 {
55414b25 760 const uschar *rfc822name = NULL;
c007c974
JH
761 uschar filename[2048];
762 int file_nr = 0;
763 int result = 0;
8e669ac1 764
c007c974
JH
765 /* must find first free sequential filename */
766 do
767 {
768 struct stat mystat;
769 (void)string_format(filename, 2048,
770 "%s/scan/%s/__rfc822_%05u", spool_directory, message_id, file_nr++);
771 /* security break */
772 if (file_nr >= 128)
773 goto NO_RFC822;
774 result = stat(CS filename,&mystat);
775 } while (result != -1);
776
777 rfc822name = filename;
778
779 /* decode RFC822 attachment */
780 mime_decoded_filename = NULL;
781 mime_stream = f;
782 mime_current_boundary = context ? context->boundary : NULL;
783 mime_decode(&rfc822name);
784 mime_stream = NULL;
785 mime_current_boundary = NULL;
786 if (!mime_decoded_filename) /* decoding failed */
787 {
788 log_write(0, LOG_MAIN,
f1d38a56 789 "MIME acl condition warning - could not decode RFC822 MIME part to file.");
f846c8f5
JH
790 rc = DEFER;
791 goto out;
c007c974
JH
792 }
793 mime_decoded_filename = NULL;
794 }
8523533c 795
c007c974
JH
796NO_RFC822:
797 /* If the boundary of this instance is NULL, we are finished here */
f846c8f5 798 if (!context) break;
8e669ac1 799
c007c974
JH
800 if (context->context == MBC_COVERLETTER_ONESHOT)
801 context->context = MBC_ATTACHMENT;
802 }
8523533c 803
f846c8f5
JH
804out:
805mime_vars_reset();
c007c974 806return rc;
8523533c
TK
807}
808
c007c974
JH
809#endif /*WITH_CONTENT_SCAN*/
810
811/* vi: sw ai sw=2
812*/