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