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