Merge branch 'debian'
[hcoop/debian/exim4.git] / src / mime.c
CommitLineData
420a0d19
CE
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
2ea97746
CE
5/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004 - 2015
6 * License: GPL
7 * Copyright (c) The Exim Maintainers 2015 - 2018
8 */
420a0d19
CE
9
10#include "exim.h"
11#ifdef WITH_CONTENT_SCAN /* entire file */
12#include "mime.h"
13#include <sys/stat.h>
14
15FILE *mime_stream = NULL;
16uschar *mime_current_boundary = NULL;
17
2ea97746
CE
18static mime_header mime_header_list[] = {
19 /* name namelen value */
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);
28
29static mime_parameter mime_parameter_list[] = {
30 /* name namelen value */
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
37
420a0d19
CE
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
2ea97746 44 encodings. Indexes are defined in mime.h. */
420a0d19
CE
45
46void
2ea97746 47mime_set_anomaly(int idx)
420a0d19 48{
2ea97746
CE
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;
420a0d19
CE
57}
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
2ea97746 72static uschar *
420a0d19
CE
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 }
89
90/* tab or whitespace may follow just ignore it if it precedes \n */
91while (*qp_p == '\t' || *qp_p == ' ' || *qp_p == '\r')
92 qp_p++;
93
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;
103}
104
105
106/* just dump MIME part without any decoding */
107static ssize_t
108mime_decode_asis(FILE* in, FILE* out, uschar* boundary)
109{
110 ssize_t len, size = 0;
111 uschar buffer[MIME_MAX_LINE_LENGTH];
112
113 while(fgets(CS buffer, MIME_MAX_LINE_LENGTH, mime_stream) != NULL)
114 {
115 if (boundary != NULL
116 && Ustrncmp(buffer, "--", 2) == 0
117 && Ustrncmp((buffer+2), boundary, Ustrlen(boundary)) == 0
118 )
119 break;
120
121 len = Ustrlen(buffer);
122 if (fwrite(buffer, 1, (size_t)len, out) < len)
123 return -1;
124 size += len;
125 } /* while */
126 return size;
127}
128
129
420a0d19
CE
130
131/* decode quoted-printable MIME part */
132static ssize_t
133mime_decode_qp(FILE* in, FILE* out, uschar* boundary)
134{
135uschar ibuf[MIME_MAX_LINE_LENGTH], obuf[MIME_MAX_LINE_LENGTH];
136uschar *ipos, *opos;
137ssize_t len, size = 0;
138
139while (fgets(CS ibuf, MIME_MAX_LINE_LENGTH, in) != NULL)
140 {
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 */
146
147 ipos = ibuf;
148 opos = obuf;
149
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);
2ea97746 162 *opos++ = '=';
420a0d19
CE
163 ++ipos;
164 }
165 else if (decode_qp_result == -1)
166 break;
167 else if (decode_qp_result >= 0)
2ea97746 168 *opos++ = decode_qp_result;
420a0d19
CE
169 }
170 else
2ea97746 171 *opos++ = *ipos++;
420a0d19
CE
172 }
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;
179 }
180 }
181return size;
182}
183
184
2ea97746
CE
185/*
186 * Return open filehandle for combo of path and file.
187 * Side-effect: set mime_decoded_filename, to copy in allocated mem
188 */
189static FILE *
420a0d19
CE
190mime_get_decode_file(uschar *pname, uschar *fname)
191{
420a0d19 192if (pname && fname)
2ea97746 193 mime_decoded_filename = string_sprintf("%s/%s", pname, fname);
420a0d19 194else if (!pname)
2ea97746 195 mime_decoded_filename = string_copy(fname);
420a0d19
CE
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;
2ea97746 205 mime_decoded_filename = string_sprintf("%s/%s-%05u", pname, message_id, file_nr++);
420a0d19
CE
206 /* security break */
207 if (file_nr >= 1024)
208 break;
2ea97746 209 result = stat(CS mime_decoded_filename, &mystat);
420a0d19 210 } while(result != -1);
420a0d19
CE
211 }
212
2ea97746 213return modefopen(mime_decoded_filename, "wb+", SPOOL_MODE);
420a0d19
CE
214}
215
216
217int
2ea97746 218mime_decode(const uschar **listptr)
420a0d19
CE
219{
220int sep = 0;
2ea97746
CE
221const uschar *list = *listptr;
222uschar * option;
223uschar * decode_path;
420a0d19
CE
224FILE *decode_file = NULL;
225long f_pos = 0;
226ssize_t size_counter = 0;
227ssize_t (*decode_function)(FILE*, FILE*, uschar*);
228
2ea97746 229if (!mime_stream || (f_pos = ftell(mime_stream)) < 0)
420a0d19
CE
230 return FAIL;
231
420a0d19 232/* build default decode path (will exist since MBOX must be spooled up) */
2ea97746 233decode_path = string_sprintf("%s/scan/%s", spool_directory, message_id);
420a0d19
CE
234
235/* try to find 1st option */
2ea97746 236if ((option = string_nextinlist(&list, &sep, NULL, 0)))
420a0d19
CE
237 {
238 /* parse 1st option */
2ea97746 239 if ((Ustrcmp(option,"false") == 0) || (Ustrcmp(option,"0") == 0))
420a0d19
CE
240 /* explicitly no decoding */
241 return FAIL;
242
243 if (Ustrcmp(option,"default") == 0)
244 /* explicit default path + file names */
245 goto DEFAULT_PATH;
246
247 if (option[0] == '/')
248 {
249 struct stat statbuf;
250
251 memset(&statbuf,0,sizeof(statbuf));
252
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);
257 else
258 /* does not exist or is a file, use as full file name */
259 decode_file = mime_get_decode_file(NULL, option);
260 }
261 else
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 }
271
272if (!decode_file)
273 return DEFER;
274
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 */
284
285size_counter = decode_function(mime_stream, decode_file, mime_current_boundary);
286
287clearerr(mime_stream);
2ea97746
CE
288if (fseek(mime_stream, f_pos, SEEK_SET))
289 return DEFER;
420a0d19
CE
290
291if (fclose(decode_file) != 0 || size_counter < 0)
292 return DEFER;
293
294/* round up to the next KiB */
295mime_content_size = (size_counter + 1023) / 1024;
296
297return OK;
298}
299
2ea97746
CE
300
301static int
420a0d19
CE
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;
309
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 }
330
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;
340
2ea97746
CE
341 /* we have hit a non-whitespace char, start copying value data */
342 header_value_mode = 2;
420a0d19 343
2ea97746
CE
344 if (c == '"') /* flip "quoted" mode */
345 header_value_mode = header_value_mode==2 ? 3 : 2;
420a0d19 346
2ea97746
CE
347 /* leave value mode on unquoted ';' */
348 if (header_value_mode == 2 && c == ';')
349 header_value_mode = 0;
350 /* -------------------------------- */
420a0d19
CE
351 }
352 else
353 {
354 /* -------- non-value mode -------- */
355 /* skip whitespace + tabs */
356 if ( (c == ' ') || (c == '\t') )
357 continue;
358 if (c == '\\')
359 {
360 /* quote next char. can be used
361 to escape brackets. */
362 if ((c = fgetc(f)) == EOF) break;
363 }
364 else if (c == '(')
365 {
366 header_open_brackets++;
367 continue;
368 }
369 else if ((c == ')') && header_open_brackets)
370 {
371 header_open_brackets--;
372 continue;
373 }
374 else if ( (c == '=') && !header_open_brackets ) /* enter value mode */
375 header_value_mode = 1;
376
377 /* skip chars while we are in a comment */
378 if (header_open_brackets > 0)
379 continue;
380 /* -------------------------------- */
381 }
382
383 /* copy the char to the buffer */
384 header[num_copied++] = (uschar)c;
385
386 /* break if header buffer is full */
387 if (num_copied > MIME_MAX_HEADER_SIZE-1)
388 done = 1;
389 }
390
391if ((num_copied > 0) && (header[num_copied-1] != ';'))
392 header[num_copied-1] = ';';
393
394/* 0-terminate */
395header[num_copied] = '\0';
396
397/* return 0 for EOF or empty line */
398if ((c == EOF) || (num_copied == 1))
399 return 0;
400else
401 return 1;
402}
403
404
2ea97746
CE
405static void
406mime_vars_reset(void)
407{
408mime_anomaly_level = 0;
409mime_anomaly_text = NULL;
410mime_boundary = NULL;
411mime_charset = NULL;
412mime_decoded_filename = NULL;
413mime_filename = NULL;
414mime_content_description = NULL;
415mime_content_disposition = NULL;
416mime_content_id = NULL;
417mime_content_transfer_encoding = NULL;
418mime_content_type = NULL;
419mime_is_multipart = 0;
420mime_content_size = 0;
421}
422
423
424/* Grab a parameter value, dealing with quoting.
425
426Arguments:
427 str Input string. Updated on return to point to terminating ; or NUL
428
429Return:
430 Allocated string with parameter value
431*/
432static uschar *
433mime_param_val(uschar ** sp)
434{
435uschar * s = *sp;
436gstring * val = NULL;
437
438/* debug_printf_indent(" considering paramval '%s'\n", s); */
439
440while (*s && *s != ';') /* ; terminates */
441 if (*s == '"')
442 {
443 s++; /* skip opening " */
444 while (*s && *s != '"') /* " protects ; */
445 val = string_catn(val, s++, 1);
446 if (*s) s++; /* skip closing " */
447 }
448 else
449 val = string_catn(val, s++, 1);
450*sp = s;
451return string_from_gstring(val);
452}
453
454static uschar *
455mime_next_semicolon(uschar * s)
456{
457while (*s && *s != ';') /* ; terminates */
458 if (*s == '"')
459 {
460 s++; /* skip opening " */
461 while (*s && *s != '"') /* " protects ; */
462 s++;
463 if (*s) s++; /* skip closing " */
464 }
465 else
466 s++;
467return s;
468}
469
470
471static uschar *
472rfc2231_to_2047(const uschar * fname, const uschar * charset, int * len)
473{
474gstring * val = string_catn(NULL, US"=?", 2);
475uschar c;
476
477if (charset)
478 val = string_cat(val, charset);
479val = string_catn(val, US"?Q?", 3);
480
481while ((c = *fname))
482 if (c == '%' && isxdigit(fname[1]) && isxdigit(fname[2]))
483 {
484 val = string_catn(val, US"=", 1);
485 val = string_catn(val, ++fname, 2);
486 fname += 2;
487 }
488 else
489 val = string_catn(val, fname++, 1);
490
491val = string_catn(val, US"?=", 2);
492*len = val->ptr;
493return string_from_gstring(val);
494}
495
496
420a0d19
CE
497int
498mime_acl_check(uschar *acl, FILE *f, struct mime_boundary_context *context,
2ea97746 499 uschar **user_msgptr, uschar **log_msgptr)
420a0d19
CE
500{
501int rc = OK;
2ea97746 502uschar * header = NULL;
420a0d19
CE
503struct mime_boundary_context nested_context;
504
505/* reserve a line buffer to work in */
2ea97746 506header = store_get(MIME_MAX_HEADER_SIZE+1);
420a0d19
CE
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 */
2ea97746
CE
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. */
420a0d19
CE
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 */
2ea97746 530 if (context) for (;;)
420a0d19 531 {
2ea97746 532 if (!fgets(CS header, MIME_MAX_HEADER_SIZE, f))
420a0d19 533 {
2ea97746
CE
534 /* Hit EOF or read error. Ugh. */
535 DEBUG(D_acl) debug_printf_indent("MIME: Hit EOF ...\n");
536 return rc;
537 }
420a0d19 538
2ea97746
CE
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 */
547 DEBUG(D_acl) debug_printf_indent("MIME: End boundary found %s\n",
548 context->boundary);
549 return rc;
420a0d19 550 }
2ea97746
CE
551
552 DEBUG(D_acl) debug_printf_indent("MIME: Next part with boundary %s\n",
553 context->boundary);
554 break;
420a0d19 555 }
420a0d19
CE
556 }
557
420a0d19 558 /* parse headers, set up expansion variables */
2ea97746 559 while (mime_get_header(f, header))
420a0d19 560 {
2ea97746
CE
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)
420a0d19 567 {
2ea97746
CE
568 uschar * p = header + mh->namelen;
569 uschar * q;
570
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);
576 DEBUG(D_acl) debug_printf_indent("MIME: found %s header, value is '%s'\n",
577 mh->name, *mh->value);
578
579 if (*(p = q)) p++; /* jump past the ; */
420a0d19 580
420a0d19 581 {
2ea97746
CE
582 uschar * mime_fname = NULL;
583 uschar * mime_fname_rfc2231 = NULL;
584 uschar * mime_filename_charset = NULL;
585 BOOL decoding_failed = FALSE;
586
587 /* grab all param=value tags on the remaining line,
588 check if they are interesting */
589
590 while (*p)
420a0d19
CE
591 {
592 mime_parameter * mp;
2ea97746
CE
593
594 DEBUG(D_acl) debug_printf_indent("MIME: considering paramlist '%s'\n", p);
595
596 if ( !mime_filename
597 && strncmpic(CUS"content-disposition:", header, 20) == 0
598 && strncmpic(CUS"filename*", p, 9) == 0
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)
420a0d19 610 {
2ea97746
CE
611 uschar * temp_string, * err_msg;
612 int slen;
420a0d19 613
2ea97746
CE
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)
420a0d19 621 {
2ea97746
CE
622 int size;
623 if (!mime_filename_charset)
420a0d19 624 {
2ea97746
CE
625 uschar * s = q;
626
627 /* look for a ' in the "filename" */
628 while(*s != '\'' && *s) s++; /* s is 1st ' or NUL */
629
630 if ((size = s-q) > 0)
631 mime_filename_charset = string_copyn(q, size);
632
633 if (*(p = s)) p++;
634 while(*p == '\'') p++; /* p is after 2nd ' */
420a0d19
CE
635 }
636 else
2ea97746
CE
637 p = q;
638
639 DEBUG(D_acl) debug_printf_indent("MIME: charset %s fname '%s'\n",
640 mime_filename_charset ? mime_filename_charset : US"<NULL>", p);
641
642 temp_string = rfc2231_to_2047(p, mime_filename_charset, &slen);
643 DEBUG(D_acl) debug_printf_indent("MIME: 2047-name %s\n", temp_string);
644
645 temp_string = rfc2047_decode(temp_string, FALSE, NULL, ' ',
646 NULL, &err_msg);
647 DEBUG(D_acl) debug_printf_indent("MIME: plain-name %s\n", temp_string);
648
649 if (!temp_string || (size = Ustrlen(temp_string)) == slen)
650 decoding_failed = TRUE;
651 else
652 /* build up a decoded filename over successive
653 filename*= parameters */
654
655 mime_filename = mime_fname = mime_fname
656 ? string_sprintf("%s%s", mime_fname, temp_string)
657 : temp_string;
420a0d19 658 }
2ea97746 659 }
420a0d19 660 }
2ea97746
CE
661
662 else
663 /* look for interesting parameters */
664 for (mp = mime_parameter_list;
665 mp < mime_parameter_list + nelem(mime_parameter_list);
666 mp++
667 ) if (strncmpic(mp->name, p, mp->namelen) == 0)
668 {
669 uschar * q;
670 uschar * dummy_errstr;
671
672 /* grab the value and copy to its expansion variable */
673 p += mp->namelen;
674 q = mime_param_val(&p); /* p now trailing ; or NUL */
675
676 *mp->value = q && *q
677 ? rfc2047_decode(q, check_rfc2047_length, NULL, 32, NULL,
678 &dummy_errstr)
679 : NULL;
680 DEBUG(D_acl) debug_printf_indent(
681 "MIME: found %s parameter in %s header, value '%s'\n",
682 mp->name, mh->name, *mp->value);
683
684 break; /* done matching param names */
685 }
686
687
420a0d19 688 /* There is something, but not one of our interesting parameters.
2ea97746
CE
689 Advance past the next semicolon */
690 p = mime_next_semicolon(p);
691 if (*p) p++;
692 } /* param scan on line */
693
694 if (strncmpic(CUS"content-disposition:", header, 20) == 0)
695 {
696 if (decoding_failed) mime_filename = mime_fname_rfc2231;
697
698 DEBUG(D_acl) debug_printf_indent(
699 "MIME: found %s parameter in %s header, value is '%s'\n",
700 "filename", mh->name, mime_filename);
701 }
420a0d19
CE
702 }
703 }
704 }
420a0d19
CE
705
706 /* set additional flag variables (easier access) */
2ea97746
CE
707 if ( mime_content_type
708 && Ustrncmp(mime_content_type,"multipart",9) == 0
709 )
420a0d19
CE
710 mime_is_multipart = 1;
711
712 /* Make a copy of the boundary pointer.
713 Required since mime_boundary is global
714 and can be overwritten further down in recursion */
715 nested_context.boundary = mime_boundary;
716
717 /* raise global counter */
718 mime_part_count++;
719
720 /* copy current file handle to global variable */
721 mime_stream = f;
722 mime_current_boundary = context ? context->boundary : 0;
723
724 /* Note the context */
725 mime_is_coverletter = !(context && context->context == MBC_ATTACHMENT);
726
727 /* call ACL handling function */
728 rc = acl_check(ACL_WHERE_MIME, NULL, acl, user_msgptr, log_msgptr);
729
730 mime_stream = NULL;
731 mime_current_boundary = NULL;
732
733 if (rc != OK) break;
734
735 /* If we have a multipart entity and a boundary, go recursive */
736 if ( (mime_content_type != NULL) &&
737 (nested_context.boundary != NULL) &&
738 (Ustrncmp(mime_content_type,"multipart",9) == 0) )
739 {
2ea97746
CE
740 DEBUG(D_acl)
741 debug_printf_indent("MIME: Entering multipart recursion, boundary '%s'\n",
742 nested_context.boundary);
420a0d19
CE
743
744 nested_context.context =
745 context && context->context == MBC_ATTACHMENT
746 ? MBC_ATTACHMENT
747 : Ustrcmp(mime_content_type,"multipart/alternative") == 0
748 || Ustrcmp(mime_content_type,"multipart/related") == 0
749 ? MBC_COVERLETTER_ALL
750 : MBC_COVERLETTER_ONESHOT;
751
752 rc = mime_acl_check(acl, f, &nested_context, user_msgptr, log_msgptr);
753 if (rc != OK) break;
754 }
755 else if ( (mime_content_type != NULL) &&
756 (Ustrncmp(mime_content_type,"message/rfc822",14) == 0) )
757 {
2ea97746 758 const uschar *rfc822name = NULL;
420a0d19
CE
759 uschar filename[2048];
760 int file_nr = 0;
761 int result = 0;
762
763 /* must find first free sequential filename */
764 do
765 {
766 struct stat mystat;
767 (void)string_format(filename, 2048,
768 "%s/scan/%s/__rfc822_%05u", spool_directory, message_id, file_nr++);
769 /* security break */
770 if (file_nr >= 128)
771 goto NO_RFC822;
772 result = stat(CS filename,&mystat);
773 } while (result != -1);
774
775 rfc822name = filename;
776
777 /* decode RFC822 attachment */
778 mime_decoded_filename = NULL;
779 mime_stream = f;
780 mime_current_boundary = context ? context->boundary : NULL;
781 mime_decode(&rfc822name);
782 mime_stream = NULL;
783 mime_current_boundary = NULL;
784 if (!mime_decoded_filename) /* decoding failed */
785 {
786 log_write(0, LOG_MAIN,
2ea97746
CE
787 "MIME acl condition warning - could not decode RFC822 MIME part to file.");
788 rc = DEFER;
789 goto out;
420a0d19
CE
790 }
791 mime_decoded_filename = NULL;
792 }
793
794NO_RFC822:
795 /* If the boundary of this instance is NULL, we are finished here */
2ea97746 796 if (!context) break;
420a0d19
CE
797
798 if (context->context == MBC_COVERLETTER_ONESHOT)
799 context->context = MBC_ATTACHMENT;
800 }
801
2ea97746
CE
802out:
803mime_vars_reset();
420a0d19
CE
804return rc;
805}
806
807#endif /*WITH_CONTENT_SCAN*/
808
809/* vi: sw ai sw=2
810*/