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