Fix parsing of quoted parameter values in MIME headers. Bug 1513
[exim.git] / src / src / mime.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004 */
6 /* License: GPL */
7
8 #include "exim.h"
9 #ifdef WITH_CONTENT_SCAN        /* entire file */
10 #include "mime.h"
11 #include <sys/stat.h>
12
13 FILE *mime_stream = NULL;
14 uschar *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
24 void
25 mime_set_anomaly(int level, const char *text)
26 {
27   mime_anomaly_level = level;
28   mime_anomaly_text = CUS text;
29 }
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
44 uschar *
45 mime_decode_qp_char(uschar *qp_p, int *c)
46 {
47 uschar *initial_pos = qp_p;
48
49 /* advance one char */
50 qp_p++;
51
52 /* Check for two hex digits and decode them */
53 if (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   }
61
62 /* tab or whitespace may follow just ignore it if it precedes \n */
63 while (*qp_p == '\t' || *qp_p == ' ' || *qp_p == '\r')
64   qp_p++;
65
66 if (*qp_p == '\n')      /* hit soft line break */
67   {
68   *c = -1;
69   return qp_p;
70   }
71
72 /* illegal char here */
73 *c = -2;
74 return initial_pos;
75 }
76
77
78 /* just dump MIME part without any decoding */
79 static ssize_t
80 mime_decode_asis(FILE* in, FILE* out, uschar* boundary)
81 {
82   ssize_t len, size = 0;
83   uschar buffer[MIME_MAX_LINE_LENGTH];
84
85   while(fgets(CS buffer, MIME_MAX_LINE_LENGTH, mime_stream) != NULL)
86     {
87     if (boundary != NULL
88        && Ustrncmp(buffer, "--", 2) == 0
89        && Ustrncmp((buffer+2), boundary, Ustrlen(boundary)) == 0
90        )
91       break;
92
93     len = Ustrlen(buffer);
94     if (fwrite(buffer, 1, (size_t)len, out) < len)
95       return -1;
96     size += len;
97     } /* while */
98   return size;
99 }
100
101
102 /* decode base64 MIME part */
103 static ssize_t
104 mime_decode_base64(FILE* in, FILE* out, uschar* boundary)
105 {
106   uschar ibuf[MIME_MAX_LINE_LENGTH], obuf[MIME_MAX_LINE_LENGTH];
107   uschar *ipos, *opos;
108   ssize_t len, size = 0;
109   int bytestate = 0;
110
111   opos = obuf;
112
113   while (Ufgets(ibuf, MIME_MAX_LINE_LENGTH, in) != NULL)
114     {
115     if (boundary != NULL
116        && Ustrncmp(ibuf, "--", 2) == 0
117        && Ustrncmp((ibuf+2), boundary, Ustrlen(boundary)) == 0
118        )
119       break;
120
121     for (ipos = ibuf ; *ipos != '\r' && *ipos != '\n' && *ipos != 0; ++ipos)
122       {
123       if (*ipos == '=')                 /* skip padding */
124         {
125         ++bytestate;
126         continue;
127         }
128       if (mime_b64[*ipos] == 128)       /* skip bad characters */
129         {
130         mime_set_anomaly(MIME_ANOMALY_BROKEN_BASE64);
131         continue;
132         }
133
134       /* simple state-machine */
135       switch((bytestate++) & 3)
136         {
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;
154         } /* switch */
155       } /* for */
156
157     /* something to write? */
158     len = opos - obuf;
159     if (len > 0)
160       {
161       if (fwrite(obuf, 1, len, out) != len) return -1; /* error */
162       size += len;
163       /* copy incomplete last byte to start of obuf, where we continue */
164       if ((bytestate & 3) != 0)
165         *obuf = *opos;
166       opos = obuf;
167       }
168     } /* while */
169
170   /* write out last byte if it was incomplete */
171   if (bytestate & 3)
172     {
173     if (fwrite(obuf, 1, 1, out) != 1) return -1;
174     ++size;
175     }
176
177   return size;
178 }
179
180
181 /* decode quoted-printable MIME part */
182 static ssize_t
183 mime_decode_qp(FILE* in, FILE* out, uschar* boundary)
184 {
185 uschar ibuf[MIME_MAX_LINE_LENGTH], obuf[MIME_MAX_LINE_LENGTH];
186 uschar *ipos, *opos;
187 ssize_t len, size = 0;
188
189 while (fgets(CS ibuf, MIME_MAX_LINE_LENGTH, in) != NULL)
190   {
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 */
196
197   ipos = ibuf;
198   opos = obuf;
199
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         }
223       }
224     else
225       {
226       *opos = *ipos;
227       ++opos;
228       ++ipos;
229       }
230     }
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;
237     }
238   }
239 return size;
240 }
241
242
243 FILE *
244 mime_get_decode_file(uschar *pname, uschar *fname)
245 {
246 FILE *f = NULL;
247 uschar *filename;
248
249 filename = (uschar *)malloc(2048);
250
251 if (pname && fname)
252   {
253   (void)string_format(filename, 2048, "%s/%s", pname, fname);
254   f = modefopen(filename,"wb+",SPOOL_MODE);
255   }
256 else if (!pname)
257   f = modefopen(fname,"wb+",SPOOL_MODE);
258 else 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);
274
275   f = modefopen(filename, "wb+", SPOOL_MODE);
276   }
277
278 /* set expansion variable */
279 mime_decoded_filename = filename;
280
281 return f;
282 }
283
284
285 int
286 mime_decode(uschar **listptr)
287 {
288 int sep = 0;
289 uschar *list = *listptr;
290 uschar *option;
291 uschar option_buffer[1024];
292 uschar decode_path[1024];
293 FILE *decode_file = NULL;
294 long f_pos = 0;
295 ssize_t size_counter = 0;
296 ssize_t (*decode_function)(FILE*, FILE*, uschar*);
297
298 if (mime_stream == NULL)
299   return FAIL;
300
301 f_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 */
307 if ((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 */
314     return FAIL;
315
316   if (Ustrcmp(option,"default") == 0)
317     /* explicit default path + file names */
318     goto DEFAULT_PATH;
319
320   if (option[0] == '/')
321     {
322     struct stat statbuf;
323
324     memset(&statbuf,0,sizeof(statbuf));
325
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);
330     else
331       /* does not exist or is a file, use as full file name */
332       decode_file = mime_get_decode_file(NULL, option);
333     }
334   else
335     /* assume file name only, use default path */
336     decode_file = mime_get_decode_file(decode_path, option);
337   }
338 else
339   {
340   /* no option? patch default path */
341 DEFAULT_PATH:
342   decode_file = mime_get_decode_file(decode_path, NULL);
343   }
344
345 if (!decode_file)
346   return DEFER;
347
348 /* decode according to mime type */
349 decode_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 */
357
358 size_counter = decode_function(mime_stream, decode_file, mime_current_boundary);
359
360 clearerr(mime_stream);
361 fseek(mime_stream, f_pos, SEEK_SET);
362
363 if (fclose(decode_file) != 0 || size_counter < 0)
364   return DEFER;
365
366 /* round up to the next KiB */
367 mime_content_size = (size_counter + 1023) / 1024;
368
369 return OK;
370 }
371
372 int
373 mime_get_header(FILE *f, uschar *header)
374 {
375 int c = EOF;
376 int done = 0;
377 int header_value_mode = 0;
378 int header_open_brackets = 0;
379 int num_copied = 0;
380
381 while(!done)
382   {
383   if ((c = fgetc(f)) == EOF) break;
384
385   /* always skip CRs */
386   if (c == '\r') continue;
387
388   if (c == '\n')
389     {
390     if (num_copied > 0)
391       {
392       /* look if next char is '\t' or ' ' */
393       if ((c = fgetc(f)) == EOF) break;
394       if ( (c == '\t') || (c == ' ') ) continue;
395       (void)ungetc(c,f);
396       }
397     /* end of the header, terminate with ';' */
398     c = ';';
399     done = 1;
400     }
401
402   /* skip control characters */
403   if (c < 32) continue;
404
405   if (header_value_mode)
406     {
407     /* --------- value mode ----------- */
408     /* skip leading whitespace */
409     if ( ((c == '\t') || (c == ' ')) && (header_value_mode == 1) )
410       continue;
411
412       /* we have hit a non-whitespace char, start copying value data */
413       header_value_mode = 2;
414
415       if (c == '"')       /* flip "quoted" mode */
416         header_value_mode = header_value_mode==2 ? 3 : 2;
417
418       /* leave value mode on unquoted ';' */
419       if (header_value_mode == 2 && c == ';') {
420         header_value_mode = 0;
421       };
422       /* -------------------------------- */
423     }
424   else
425     {
426     /* -------- non-value mode -------- */
427     /* skip whitespace + tabs */
428     if ( (c == ' ') || (c == '\t') )
429       continue;
430     if (c == '\\')
431       {
432       /* quote next char. can be used
433       to escape brackets. */
434       if ((c = fgetc(f)) == EOF) break;
435       }
436     else if (c == '(')
437       {
438       header_open_brackets++;
439       continue;
440       }
441     else if ((c == ')') && header_open_brackets)
442       {
443       header_open_brackets--;
444       continue;
445       }
446     else if ( (c == '=') && !header_open_brackets ) /* enter value mode */
447       header_value_mode = 1;
448
449     /* skip chars while we are in a comment */
450     if (header_open_brackets > 0)
451       continue;
452     /* -------------------------------- */
453     }
454
455   /* copy the char to the buffer */
456   header[num_copied++] = (uschar)c;
457
458   /* break if header buffer is full */
459   if (num_copied > MIME_MAX_HEADER_SIZE-1)
460     done = 1;
461   }
462
463 if ((num_copied > 0) && (header[num_copied-1] != ';'))
464   header[num_copied-1] = ';';
465
466 /* 0-terminate */
467 header[num_copied] = '\0';
468
469 /* return 0 for EOF or empty line */
470 if ((c == EOF) || (num_copied == 1))
471   return 0;
472 else
473   return 1;
474 }
475
476
477 int
478 mime_acl_check(uschar *acl, FILE *f, struct mime_boundary_context *context,
479                    uschar **user_msgptr, uschar **log_msgptr)
480 {
481 int rc = OK;
482 uschar *header = NULL;
483 struct mime_boundary_context nested_context;
484
485 /* reserve a line buffer to work in */
486 if (!(header = (uschar *)malloc(MIME_MAX_HEADER_SIZE+1)))
487   {
488   log_write(0, LOG_PANIC,
489        "MIME ACL: can't allocate %d bytes of memory.", MIME_MAX_HEADER_SIZE+1);
490   return DEFER;
491   }
492
493 /* Not actually used at the moment, but will be vital to fixing
494  * some RFC 2046 nonconformance later... */
495 nested_context.parent = context;
496
497 /* loop through parts */
498 while(1)
499   {
500   /* reset all per-part mime variables */
501   mime_anomaly_level     = 0;
502   mime_anomaly_text      = NULL;
503   mime_boundary          = NULL;
504   mime_charset           = NULL;
505   mime_decoded_filename  = NULL;
506   mime_filename          = NULL;
507   mime_content_description = NULL;
508   mime_content_disposition = NULL;
509   mime_content_id        = NULL;
510   mime_content_transfer_encoding = NULL;
511   mime_content_type      = NULL;
512   mime_is_multipart      = 0;
513   mime_content_size      = 0;
514
515   /*
516   If boundary is null, we assume that *f is positioned on the start of headers (for example,
517   at the very beginning of a message.
518   If a boundary is given, we must first advance to it to reach the start of the next header
519   block.
520   */
521
522   /* NOTE -- there's an error here -- RFC2046 specifically says to
523    * check for outer boundaries.  This code doesn't do that, and
524    * I haven't fixed this.
525    *
526    * (I have moved partway towards adding support, however, by adding
527    * a "parent" field to my new boundary-context structure.)
528    */
529   if (context != NULL)
530     {
531     while(fgets(CS header, MIME_MAX_HEADER_SIZE, f) != NULL)
532       {
533       /* boundary line must start with 2 dashes */
534       if (Ustrncmp(header,"--",2) == 0)
535         {
536         if (Ustrncmp((header+2),context->boundary,Ustrlen(context->boundary)) == 0)
537           {
538           /* found boundary */
539           if (Ustrncmp((header+2+Ustrlen(context->boundary)),"--",2) == 0)
540             {
541             /* END boundary found */
542             debug_printf("End boundary found %s\n", context->boundary);
543             return rc;
544             }
545           else
546             debug_printf("Next part with boundary %s\n", context->boundary);
547
548           /* can't use break here */
549           goto DECODE_HEADERS;
550           }
551         }
552       }
553     /* Hit EOF or read error. Ugh. */
554     debug_printf("Hit EOF ...\n");
555     return rc;
556     }
557
558 DECODE_HEADERS:
559   /* parse headers, set up expansion variables */
560   while (mime_get_header(f,header))
561     {
562     int i;
563     /* loop through header list */
564     for (i = 0; i < mime_header_list_size; i++)
565       {
566       uschar *header_value = NULL;
567       int header_value_len = 0;
568
569       /* found an interesting header? */
570       if (strncmpic(mime_header_list[i].name,header,mime_header_list[i].namelen) == 0)
571         {
572         uschar *p = header + mime_header_list[i].namelen;
573         /* yes, grab the value (normalize to lower case)
574            and copy to its corresponding expansion variable */
575         while(*p != ';')
576           {
577           *p = tolower(*p);
578           p++;
579           }
580         header_value_len = (p - (header + mime_header_list[i].namelen));
581         header_value = (uschar *)malloc(header_value_len+1);
582         memset(header_value,0,header_value_len+1);
583         p = header + mime_header_list[i].namelen;
584         Ustrncpy(header_value, p, header_value_len);
585         debug_printf("Found %s MIME header, value is '%s'\n", mime_header_list[i].name, header_value);
586         *((uschar **)(mime_header_list[i].value)) = header_value;
587
588         /* make p point to the next character after the closing ';' */
589         p += (header_value_len+1);
590
591         /* grab all param=value tags on the remaining line, check if they are interesting */
592 NEXT_PARAM_SEARCH:
593         while (*p != 0)
594           {
595           mime_parameter * mp;
596           for (mp = mime_parameter_list;
597                mp < &mime_parameter_list[mime_parameter_list_size];
598                mp++)
599             {
600             uschar *param_value = NULL;
601             int param_value_len = 0;
602
603             /* found an interesting parameter? */
604             if (strncmpic(mp->name, p, mp->namelen) == 0)
605               {
606               uschar *q = p + mp->namelen;
607               int size = 0;
608               int ptr = 0;
609
610               /* yes, grab the value and copy to its corresponding expansion variable */
611               while(*q && *q != ';')            /* ; terminates */
612                 {
613                 if (*q == '"')
614                   {
615                   q++;                          /* skip leading " */
616                   while(*q && *q != '"')        /* which protects ; */
617                     param_value = string_cat(param_value, &size, &ptr, q++, 1);
618                   if (*q) q++;                  /* skip trailing " */
619                   }
620                 else
621                   param_value = string_cat(param_value, &size, &ptr, q++, 1);
622                 }
623               param_value[ptr++] = '\0';
624               param_value_len = ptr;
625
626               param_value = rfc2047_decode(param_value, check_rfc2047_length, NULL, 32, &param_value_len, &q);
627               debug_printf("Found %s MIME parameter in %s header, value is '%s'\n", mp->name, mime_header_list[i].name, param_value);
628               *((uschar **)(mp->value)) = param_value;
629               p += (mp->namelen + param_value_len + 1);
630               goto NEXT_PARAM_SEARCH;
631             }
632           }
633           /* There is something, but not one of our interesting parameters.
634              Advance to the next semicolon */
635           while(*p != ';') p++;
636           p++;
637         }
638       }
639     }
640   }
641
642   /* set additional flag variables (easier access) */
643   if ( (mime_content_type != NULL) &&
644        (Ustrncmp(mime_content_type,"multipart",9) == 0) )
645     mime_is_multipart = 1;
646
647   /* Make a copy of the boundary pointer.
648      Required since mime_boundary is global
649      and can be overwritten further down in recursion */
650   nested_context.boundary = mime_boundary;
651
652   /* raise global counter */
653   mime_part_count++;
654
655   /* copy current file handle to global variable */
656   mime_stream = f;
657   mime_current_boundary = context ? context->boundary : 0;
658
659   /* Note the context */
660   mime_is_coverletter = !(context && context->context == MBC_ATTACHMENT);
661
662   /* call ACL handling function */
663   rc = acl_check(ACL_WHERE_MIME, NULL, acl, user_msgptr, log_msgptr);
664
665   mime_stream = NULL;
666   mime_current_boundary = NULL;
667
668   if (rc != OK) break;
669
670   /* If we have a multipart entity and a boundary, go recursive */
671   if ( (mime_content_type != NULL) &&
672        (nested_context.boundary != NULL) &&
673        (Ustrncmp(mime_content_type,"multipart",9) == 0) )
674     {
675     debug_printf("Entering multipart recursion, boundary '%s'\n", nested_context.boundary);
676
677     nested_context.context =
678       context && context->context == MBC_ATTACHMENT
679       ? MBC_ATTACHMENT
680       :    Ustrcmp(mime_content_type,"multipart/alternative") == 0
681         || Ustrcmp(mime_content_type,"multipart/related") == 0
682       ? MBC_COVERLETTER_ALL
683       : MBC_COVERLETTER_ONESHOT;
684
685     rc = mime_acl_check(acl, f, &nested_context, user_msgptr, log_msgptr);
686     if (rc != OK) break;
687     }
688   else if ( (mime_content_type != NULL) &&
689           (Ustrncmp(mime_content_type,"message/rfc822",14) == 0) )
690     {
691     uschar *rfc822name = NULL;
692     uschar filename[2048];
693     int file_nr = 0;
694     int result = 0;
695
696     /* must find first free sequential filename */
697     do
698       {
699       struct stat mystat;
700       (void)string_format(filename, 2048,
701         "%s/scan/%s/__rfc822_%05u", spool_directory, message_id, file_nr++);
702       /* security break */
703       if (file_nr >= 128)
704         goto NO_RFC822;
705       result = stat(CS filename,&mystat);
706       } while (result != -1);
707
708     rfc822name = filename;
709
710     /* decode RFC822 attachment */
711     mime_decoded_filename = NULL;
712     mime_stream = f;
713     mime_current_boundary = context ? context->boundary : NULL;
714     mime_decode(&rfc822name);
715     mime_stream = NULL;
716     mime_current_boundary = NULL;
717     if (!mime_decoded_filename)         /* decoding failed */
718       {
719       log_write(0, LOG_MAIN,
720            "mime_regex acl condition warning - could not decode RFC822 MIME part to file.");
721       return DEFER;
722       }
723     mime_decoded_filename = NULL;
724     }
725
726 NO_RFC822:
727   /* If the boundary of this instance is NULL, we are finished here */
728   if (context == NULL) break;
729
730   if (context->context == MBC_COVERLETTER_ONESHOT)
731     context->context = MBC_ATTACHMENT;
732   }
733
734 return rc;
735 }
736
737 #endif  /*WITH_CONTENT_SCAN*/
738
739 /* vi: sw ai sw=2
740 */