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