1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
8 /* Code for unpacking MIME containers. Called from acl.c. */
11 #ifdef WITH_OLD_DEMIME
15 uschar demime_reason_buffer
[1024];
16 struct file_extension
*file_extensions
= NULL
;
18 int demime(uschar
**listptr
) {
20 uschar
*list
= *listptr
;
22 uschar option_buffer
[64];
23 unsigned long mbox_size
;
25 uschar defer_error_buffer
[1024];
28 /* reset found_extension variable */
29 found_extension
= NULL
;
31 /* try to find 1st option */
32 if ((option
= string_nextinlist(&list
, &sep
,
34 sizeof(option_buffer
))) != NULL
) {
36 /* parse 1st option */
37 if ( (Ustrcmp(option
,"false") == 0) || (Ustrcmp(option
,"0") == 0) ) {
38 /* explicitly no demimeing */
43 /* no options -> no demimeing */
47 /* make sure the eml mbox file is spooled up */
48 mbox_file
= spool_mbox(&mbox_size
, NULL
);
50 if (mbox_file
== NULL
) {
51 /* error while spooling */
52 log_write(0, LOG_MAIN
|LOG_PANIC
,
53 "demime acl condition: error while creating mbox spool file");
57 /* call demimer if not already done earlier */
59 demime_rc
= mime_demux(mbox_file
, defer_error_buffer
);
61 (void)fclose(mbox_file
);
63 if (demime_rc
== DEFER
) {
64 /* temporary failure (DEFER => DEFER) */
65 log_write(0, LOG_MAIN
,
66 "demime acl condition: %s", defer_error_buffer
);
70 /* set demime_ok to avoid unpacking again */
73 /* check for file extensions, if there */
74 while (option
!= NULL
) {
75 struct file_extension
*this_extension
= file_extensions
;
77 /* Look for the wildcard. If it is found, we always return true.
78 The user must then use a custom condition to evaluate demime_errorlevel */
79 if (Ustrcmp(option
,"*") == 0) {
80 found_extension
= NULL
;
84 /* loop thru extension list */
85 while (this_extension
!= NULL
) {
86 if (strcmpic(option
, this_extension
->file_extension_string
) == 0) {
88 found_extension
= this_extension
->file_extension_string
;
91 this_extension
= this_extension
->next
;
94 /* grab next extension from option list */
95 option
= string_nextinlist(&list
, &sep
,
97 sizeof(option_buffer
));
105 /*************************************************
106 * small hex_str -> integer conversion function *
107 *************************************************/
109 /* needed for quoted-printable
112 unsigned int mime_hstr_i(uschar
*cptr
) {
113 unsigned int i
, j
= 0;
115 while (cptr
&& *cptr
&& isxdigit(*cptr
)) {
126 /*************************************************
127 * decode quoted-printable chars *
128 *************************************************/
130 /* gets called when we hit a =
131 returns: new pointer position
134 -1 - soft line break, no char
135 0-255 - char to write
138 uschar
*mime_decode_qp(uschar
*qp_p
,int *c
) {
139 uschar hex
[] = {0,0,0};
141 uschar
*initial_pos
= qp_p
;
143 /* advance one char */
147 if ( (*qp_p
== '\t') || (*qp_p
== ' ') || (*qp_p
== '\r') ) {
148 /* tab or whitespace may follow
149 just ignore it, but remember
150 that this is not a valid hex
156 else if ( (('0' <= *qp_p
) && (*qp_p
<= '9')) || (('A' <= *qp_p
) && (*qp_p
<= 'F')) || (('a' <= *qp_p
) && (*qp_p
<= 'f')) ) {
157 /* this is a valid hex char, if nan is unset */
159 /* this is illegal */
168 else if (*qp_p
== '\n') {
169 /* hit soft line break already, continue */
174 /* illegal char here */
179 if ( (('0' <= *qp_p
) && (*qp_p
<= '9')) || (('A' <= *qp_p
) && (*qp_p
<= 'F')) || (('a' <= *qp_p
) && (*qp_p
<= 'f')) ) {
182 /* do hex conversion */
183 *c
= mime_hstr_i(hex
);
202 /*************************************************
203 * open new dump file *
204 *************************************************/
206 /* open new dump file
207 returns: -2 soft error
208 or file #, FILE * in f
211 int mime_get_dump_file(uschar
*extension
, FILE **f
, uschar
*info
) {
212 uschar file_name
[1024];
214 unsigned int file_nr
;
215 uschar default_extension
[] = ".com";
218 if (extension
== NULL
)
219 extension
= default_extension
;
221 /* scan the proposed extension.
222 if it is longer than 4 chars, or
223 contains exotic chars, use the default extension */
225 /* if (Ustrlen(extension) > 4) {
226 extension = default_extension;
233 *p
= (uschar
)tolower((uschar
)*p
);
234 if ( (*p
< 97) || (*p
> 122) ) {
235 extension
= default_extension
;
241 /* find a new file to write to */
246 (void)string_format(file_name
,1024,"%s/scan/%s/%s-%05u%s",spool_directory
,message_id
,message_id
,file_nr
,extension
);
248 if (file_nr
>= MIME_SANITY_MAX_DUMP_FILES
) {
249 /* max parts reached */
250 mime_trigger_error(MIME_ERRORLEVEL_TOO_MANY_PARTS
);
253 result
= stat(CS file_name
,&mystat
);
257 *f
= modefopen(file_name
,"wb+",SPOOL_MODE
);
259 /* cannot open new dump file, disk full ? -> soft error */
260 (void)string_format(info
, 1024,"unable to open dump file");
268 /*************************************************
269 * Find a string in a mime header *
270 *************************************************/
272 /* Find a string in a mime header, and optionally fill in
273 the value associated with it into *value
275 returns: 0 - nothing found
277 2 - found param + value
280 int mime_header_find(uschar
*header
, uschar
*param
, uschar
**value
) {
283 needle
= strstric(header
,param
,FALSE
);
284 if (needle
!= NULL
) {
286 needle
+= Ustrlen(param
);
287 if (*needle
== '=') {
291 value_start
= needle
+ 1;
292 value_end
= strstric(value_start
,US
";",FALSE
);
293 if (value_end
!= NULL
) {
294 /* allocate mem for value */
295 *value
= (uschar
*)malloc((value_end
- value_start
)+1);
299 Ustrncpy(*value
,value_start
,(value_end
- value_start
));
300 (*value
)[(value_end
- value_start
)] = '\0';
311 /*************************************************
312 * Read a line of MIME input *
313 *************************************************/
314 /* returns status code, one of
317 MIME_READ_LINE_OVERFLOW 2
319 In header mode, the line will be "cooked".
322 int mime_read_line(FILE *f
, int mime_demux_mode
, uschar
*buffer
, long *num_copied
) {
325 int header_value_mode
= 0;
326 int header_open_brackets
= 0;
335 /* --------- header mode -------------- */
336 if (mime_demux_mode
== MIME_DEMUX_MODE_MIME_HEADERS
) {
338 /* always skip CRs */
339 if (c
== '\r') continue;
342 if ((*num_copied
) > 0) {
343 /* look if next char is '\t' or ' ' */
346 if ( (c
== '\t') || (c
== ' ') ) continue;
349 /* end of the header, terminate with ';' */
354 /* skip control characters */
355 if (c
< 32) continue;
357 /* skip whitespace + tabs */
358 if ( (c
== ' ') || (c
== '\t') )
361 if (header_value_mode
) {
362 /* --------- value mode ----------- */
364 if (c
== '"') continue;
366 /* leave value mode on ';' */
368 header_value_mode
= 0;
370 /* -------------------------------- */
373 /* -------- non-value mode -------- */
375 /* quote next char. can be used
376 to escape brackets. */
381 header_open_brackets
++;
384 else if ((c
== ')') && header_open_brackets
) {
385 header_open_brackets
--;
388 else if ( (c
== '=') && !header_open_brackets
) {
389 /* enter value mode */
390 header_value_mode
= 1;
393 /* skip chars while we are in a comment */
394 if (header_open_brackets
> 0)
396 /* -------------------------------- */
399 /* ------------------------------------ */
401 /* ----------- non-header mode -------- */
405 /* ------------------------------------ */
408 /* copy the char to the buffer */
409 buffer
[*num_copied
] = (uschar
)c
;
413 /* break if buffer is full */
414 if (*num_copied
> MIME_SANITY_MAX_LINE_LENGTH
-1) {
420 buffer
[*num_copied
] = '\0';
422 if (*num_copied
> MIME_SANITY_MAX_LINE_LENGTH
-1)
423 return MIME_READ_LINE_OVERFLOW
;
426 return MIME_READ_LINE_EOF
;
428 return MIME_READ_LINE_OK
;
432 /*************************************************
433 * Check for a MIME boundary *
434 *************************************************/
436 /* returns: 0 - no boundary found
437 1 - start boundary found
438 2 - end boundary found
441 int mime_check_boundary(uschar
*line
, struct boundary
*boundaries
) {
442 struct boundary
*thisboundary
= boundaries
;
443 uschar workbuf
[MIME_SANITY_MAX_LINE_LENGTH
+1];
446 /* check for '--' first */
447 if (Ustrncmp(line
,"--",2) == 0) {
449 /* strip tab and space */
450 for (i
= 2; i
< Ustrlen(line
); i
++) {
451 if ((line
[i
] != ' ') && (line
[i
] != '\t')) {
452 workbuf
[j
] = line
[i
];
458 while(thisboundary
!= NULL
) {
459 if (Ustrncmp(workbuf
,thisboundary
->boundary_string
,Ustrlen(thisboundary
->boundary_string
)) == 0) {
460 if (Ustrncmp(&workbuf
[Ustrlen(thisboundary
->boundary_string
)],"--",2) == 0) {
461 /* final boundary found */
466 thisboundary
= thisboundary
->next
;
474 /*************************************************
475 * Check for start of a UUENCODE block *
476 *************************************************/
478 /* returns 0 for no hit,
482 int mime_check_uu_start(uschar
*line
, uschar
*uu_file_extension
, int *has_tnef
) {
484 if ( (strncmpic(line
,US
"begin ",6) == 0)) {
485 uschar
*uu_filename
= &line
[6];
487 /* skip perms, if present */
488 Ustrtoul(&line
[6],&uu_filename
,10);
490 /* advance one char */
493 /* This should be the filename.
494 Check if winmail.dat is present,
495 which indicates TNEF. */
496 if (strncmpic(uu_filename
,US
"winmail.dat",11) == 0) {
500 /* reverse to dot if present,
501 copy up to 4 chars for the extension */
502 if (Ustrrchr(uu_filename
,'.') != NULL
)
503 uu_filename
= Ustrrchr(uu_filename
,'.');
505 return sscanf(CS uu_filename
, "%4[.0-9A-Za-z]",CS uu_file_extension
);
514 /*************************************************
516 *************************************************/
518 /* returns number of decoded bytes
522 int warned_about_uudec_line_sanity_1
= 0;
523 int warned_about_uudec_line_sanity_2
= 0;
524 long uu_decode_line(uschar
*line
, uschar
**data
, long line_len
, uschar
*info
) {
526 long num_decoded
= 0;
529 int uu_decoded_line_len
, uu_encoded_line_len
;
531 /* allocate memory for data and work buffer */
532 *data
= (uschar
*)malloc(line_len
);
534 (void)string_format(info
, 1024,"unable to allocate %lu bytes",line_len
);
538 work
= (uschar
*)malloc(line_len
);
540 (void)string_format(info
, 1024,"unable to allocate %lu bytes",line_len
);
544 memcpy(work
,line
,line_len
);
546 /* First char is line length
547 This is microsofts way of getting it. Scary. */
549 /* ignore this line */
553 uu_decoded_line_len
= uudec
[work
[0]];
563 uu_encoded_line_len
= (p
- &work
[1]);
566 /* check that resulting line length is a multiple of 4 */
567 if ( ( uu_encoded_line_len
% 4 ) != 0) {
568 if (!warned_about_uudec_line_sanity_1
) {
569 mime_trigger_error(MIME_ERRORLEVEL_UU_MISALIGNED
);
570 warned_about_uudec_line_sanity_1
= 1;
575 /* check that the line length matches */
576 if ( ( (((uu_encoded_line_len
/4)*3)-2) > uu_decoded_line_len
) || (((uu_encoded_line_len
/4)*3) < uu_decoded_line_len
) ) {
577 if (!warned_about_uudec_line_sanity_2
) {
578 mime_trigger_error(MIME_ERRORLEVEL_UU_LINE_LENGTH
);
579 warned_about_uudec_line_sanity_2
= 1;
584 while ( ((p
- &work
[1]) < uu_encoded_line_len
) && (num_decoded
< uu_decoded_line_len
)) {
586 /* byte 0 ---------------------- */
587 if ((p
- &work
[1] + 1) >= uu_encoded_line_len
) {
591 (*data
)[num_decoded
] = *p
;
592 (*data
)[num_decoded
] <<= 2;
596 (*data
)[num_decoded
] |= tmp_c
;
601 /* byte 1 ---------------------- */
602 if ((p
- &work
[1] + 1) >= uu_encoded_line_len
) {
606 (*data
)[num_decoded
] = *p
;
607 (*data
)[num_decoded
] <<= 4;
611 (*data
)[num_decoded
] |= tmp_c
;
616 /* byte 2 ---------------------- */
617 if ((p
- &work
[1] + 1) >= uu_encoded_line_len
) {
621 (*data
)[num_decoded
] = *p
;
622 (*data
)[num_decoded
] <<= 6;
624 (*data
)[num_decoded
] |= *(p
+1);
631 return uu_decoded_line_len
;
635 /*************************************************
636 * Decode a b64 or qp line *
637 *************************************************/
639 /* returns number of decoded bytes
644 int warned_about_b64_line_length
= 0;
645 int warned_about_b64_line_sanity
= 0;
646 int warned_about_b64_illegal_char
= 0;
647 int warned_about_qp_line_sanity
= 0;
648 long mime_decode_line(int mime_demux_mode
,uschar
*line
, uschar
**data
, long max_data_len
, uschar
*info
) {
650 long num_decoded
= 0;
654 /* allocate memory for data */
655 *data
= (uschar
*)malloc(max_data_len
);
657 (void)string_format(info
, 1024,"unable to allocate %lu bytes",max_data_len
);
661 if (mime_demux_mode
== MIME_DEMUX_MODE_BASE64
) {
662 /* ---------------------------------------------- */
664 /* NULL out trailing '\r' and '\n' chars */
665 while (Ustrrchr(line
,'\r') != NULL
) {
666 *(Ustrrchr(line
,'\r')) = '\0';
668 while (Ustrrchr(line
,'\n') != NULL
) {
669 *(Ustrrchr(line
,'\n')) = '\0';
672 /* check maximum base 64 line length */
673 if (Ustrlen(line
) > MIME_SANITY_MAX_B64_LINE_LENGTH
) {
674 if (!warned_about_b64_line_length
) {
675 mime_trigger_error(MIME_ERRORLEVEL_B64_LINE_LENGTH
);
676 warned_about_b64_line_length
= 1;
682 while (*(p
+offset
) != '\0') {
683 /* hit illegal char ? */
684 if (b64
[*(p
+offset
)] == 128) {
685 if (!warned_about_b64_illegal_char
) {
686 mime_trigger_error(MIME_ERRORLEVEL_B64_ILLEGAL_CHAR
);
687 warned_about_b64_illegal_char
= 1;
692 *p
= b64
[*(p
+offset
)];
698 /* check that resulting line length is a multiple of 4 */
699 if ( ( (p
- &line
[0]) % 4 ) != 0) {
700 if (!warned_about_b64_line_sanity
) {
701 mime_trigger_error(MIME_ERRORLEVEL_B64_MISALIGNED
);
702 warned_about_b64_line_sanity
= 1;
706 /* line is translated, start bit shifting */
712 /* byte 0 ---------------------- */
717 (*data
)[num_decoded
] = *p
;
718 (*data
)[num_decoded
] <<= 2;
722 (*data
)[num_decoded
] |= tmp_c
;
727 /* byte 1 ---------------------- */
732 (*data
)[num_decoded
] = *p
;
733 (*data
)[num_decoded
] <<= 4;
737 (*data
)[num_decoded
] |= tmp_c
;
742 /* byte 2 ---------------------- */
747 (*data
)[num_decoded
] = *p
;
748 (*data
)[num_decoded
] <<= 6;
750 (*data
)[num_decoded
] |= *(p
+1);
757 /* ---------------------------------------------- */
759 else if (mime_demux_mode
== MIME_DEMUX_MODE_QP
) {
760 /* ---------------------------------------------- */
765 int decode_qp_result
;
767 p
= mime_decode_qp(p
,&decode_qp_result
);
769 if (decode_qp_result
== -2) {
770 /* Error from decoder. p is unchanged. */
771 if (!warned_about_qp_line_sanity
) {
772 mime_trigger_error(MIME_ERRORLEVEL_QP_ILLEGAL_CHAR
);
773 warned_about_qp_line_sanity
= 1;
775 (*data
)[num_decoded
] = '=';
779 else if (decode_qp_result
== -1) {
780 /* End of the line with soft line break.
784 else if (decode_qp_result
>= 0) {
785 (*data
)[num_decoded
] = decode_qp_result
;
790 (*data
)[num_decoded
] = *p
;
797 /* ---------------------------------------------- */
805 /*************************************************
806 * Log demime errors and set mime error level *
807 *************************************************/
809 /* This sets the global demime_reason expansion
810 variable and the demime_errorlevel gauge. */
812 void mime_trigger_error(int level
, uschar
*format
, ...) {
816 if( (f
= malloc(16384+23)) != NULL
) {
817 /* first log the incident */
818 sprintf(f
,"demime acl condition: ");
820 va_start(ap
, format
);
821 (void)string_vformat(US f
, 16383,(char *)format
, ap
);
824 log_write(0, LOG_MAIN
, "%s", f
);
825 /* then copy to demime_reason_buffer if new
826 level is greater than old level */
827 if (level
> demime_errorlevel
) {
828 demime_errorlevel
= level
;
829 Ustrcpy(demime_reason_buffer
, US f
);
830 demime_reason
= demime_reason_buffer
;
836 /*************************************************
837 * Demultiplex MIME stream. *
838 *************************************************/
840 /* We can handle BASE64, QUOTED-PRINTABLE, and UUENCODE.
841 UUENCODE does not need to have a proper
842 transfer-encoding header, we detect it with "begin"
844 This function will report human parsable errors in
847 returns DEFER -> soft error (see *info)
848 OK -> EOF hit, all ok
851 int mime_demux(FILE *f
, uschar
*info
) {
852 int mime_demux_mode
= MIME_DEMUX_MODE_MIME_HEADERS
;
853 int uu_mode
= MIME_UU_MODE_OFF
;
854 FILE *mime_dump_file
= NULL
;
855 FILE *uu_dump_file
= NULL
;
857 int mime_read_line_status
= MIME_READ_LINE_OK
;
859 struct boundary
*boundaries
= NULL
;
860 struct mime_part mime_part_p
;
864 /* allocate room for our linebuffer */
865 line
= (uschar
*)malloc(MIME_SANITY_MAX_LINE_LENGTH
);
867 (void)string_format(info
, 1024,"unable to allocate %u bytes",MIME_SANITY_MAX_LINE_LENGTH
);
871 /* clear MIME header structure */
872 memset(&mime_part_p
,0,sizeof(mime_part
));
874 /* ----------------------- start demux loop --------------------- */
875 while (mime_read_line_status
== MIME_READ_LINE_OK
) {
877 /* read a line of input. Depending on the mode we are in,
878 the returned format will differ. */
879 mime_read_line_status
= mime_read_line(f
,mime_demux_mode
,line
,&line_len
);
881 if (mime_read_line_status
== MIME_READ_LINE_OVERFLOW
) {
882 mime_trigger_error(MIME_ERRORLEVEL_LONG_LINE
);
883 /* despite the error, continue .. */
884 mime_read_line_status
= MIME_READ_LINE_OK
;
887 else if (mime_read_line_status
== MIME_READ_LINE_EOF
) {
891 if (mime_demux_mode
== MIME_DEMUX_MODE_MIME_HEADERS
) {
892 /* -------------- header mode --------------------- */
894 /* Check for an empty line, which is the end of the headers.
895 In HEADER mode, the line is returned "cooked", with the
896 final '\n' replaced by a ';' */
900 /* We have reached the end of the headers. Start decoding
901 with the collected settings. */
902 if (mime_part_p
.seen_content_transfer_encoding
> 1) {
903 mime_demux_mode
= mime_part_p
.seen_content_transfer_encoding
;
906 /* default to plain mode if no specific encoding type found */
907 mime_demux_mode
= MIME_DEMUX_MODE_PLAIN
;
910 /* open new dump file */
911 tmp
= mime_get_dump_file(mime_part_p
.extension
, &mime_dump_file
, info
);
916 /* clear out mime_part */
917 memset(&mime_part_p
,0,sizeof(mime_part
));
920 /* Another header to check for file extensions,
921 encoding type and boundaries */
922 if (strncmpic(US
"content-type:",line
,Ustrlen("content-type:")) == 0) {
923 /* ---------------------------- Content-Type header ------------------------------- */
924 uschar
*value
= line
;
926 /* check for message/partial MIME type and reject it */
927 if (mime_header_find(line
,US
"message/partial",NULL
) > 0)
928 mime_trigger_error(MIME_ERRORLEVEL_MESSAGE_PARTIAL
);
930 /* check for TNEF content type, remember to unpack TNEF later. */
931 if (mime_header_find(line
,US
"application/ms-tnef",NULL
) > 0)
934 /* check for message/rfcxxx attachments */
935 if (mime_header_find(line
,US
"message/rfc822",NULL
) > 0)
938 /* find the file extension, but do not fill it in
939 it is already set, since content-disposition has
941 if (mime_part_p
.extension
== NULL
) {
942 if (mime_header_find(line
,US
"name",&value
) == 2) {
943 if (Ustrlen(value
) > MIME_SANITY_MAX_FILENAME
)
944 mime_trigger_error(MIME_ERRORLEVEL_FILENAME_LENGTH
);
945 mime_part_p
.extension
= value
;
946 mime_part_p
.extension
= Ustrrchr(value
,'.');
947 if (mime_part_p
.extension
== NULL
) {
948 /* file without extension, setting
949 NULL will use the default extension later */
950 mime_part_p
.extension
= NULL
;
953 struct file_extension
*this_extension
=
954 (struct file_extension
*)malloc(sizeof(file_extension
));
956 this_extension
->file_extension_string
=
957 (uschar
*)malloc(Ustrlen(mime_part_p
.extension
)+1);
958 Ustrcpy(this_extension
->file_extension_string
,
959 mime_part_p
.extension
+1);
960 this_extension
->next
= file_extensions
;
961 file_extensions
= this_extension
;
966 /* find a boundary and add it to the list, if present */
968 if (mime_header_find(line
,US
"boundary",&value
) == 2) {
969 struct boundary
*thisboundary
;
971 if (Ustrlen(value
) > MIME_SANITY_MAX_BOUNDARY_LENGTH
) {
972 mime_trigger_error(MIME_ERRORLEVEL_BOUNDARY_LENGTH
);
975 thisboundary
= (struct boundary
*)malloc(sizeof(boundary
));
976 thisboundary
->next
= boundaries
;
977 thisboundary
->boundary_string
= value
;
978 boundaries
= thisboundary
;
982 if (mime_part_p
.seen_content_type
== 0) {
983 mime_part_p
.seen_content_type
= 1;
986 mime_trigger_error(MIME_ERRORLEVEL_DOUBLE_HEADERS
);
988 /* ---------------------------------------------------------------------------- */
990 else if (strncmpic(US
"content-transfer-encoding:",line
,Ustrlen("content-transfer-encoding:")) == 0) {
991 /* ---------------------------- Content-Transfer-Encoding header -------------- */
993 if (mime_part_p
.seen_content_transfer_encoding
== 0) {
994 if (mime_header_find(line
,US
"base64",NULL
) > 0) {
995 mime_part_p
.seen_content_transfer_encoding
= MIME_DEMUX_MODE_BASE64
;
997 else if (mime_header_find(line
,US
"quoted-printable",NULL
) > 0) {
998 mime_part_p
.seen_content_transfer_encoding
= MIME_DEMUX_MODE_QP
;
1001 mime_part_p
.seen_content_transfer_encoding
= MIME_DEMUX_MODE_PLAIN
;
1005 mime_trigger_error(MIME_ERRORLEVEL_DOUBLE_HEADERS
);
1007 /* ---------------------------------------------------------------------------- */
1009 else if (strncmpic(US
"content-disposition:",line
,Ustrlen("content-disposition:")) == 0) {
1010 /* ---------------------------- Content-Disposition header -------------------- */
1011 uschar
*value
= line
;
1013 if (mime_part_p
.seen_content_disposition
== 0) {
1014 mime_part_p
.seen_content_disposition
= 1;
1016 if (mime_header_find(line
,US
"filename",&value
) == 2) {
1017 if (Ustrlen(value
) > MIME_SANITY_MAX_FILENAME
)
1018 mime_trigger_error(MIME_ERRORLEVEL_FILENAME_LENGTH
);
1019 mime_part_p
.extension
= value
;
1020 mime_part_p
.extension
= Ustrrchr(value
,'.');
1021 if (mime_part_p
.extension
== NULL
) {
1022 /* file without extension, setting
1023 NULL will use the default extension later */
1024 mime_part_p
.extension
= NULL
;
1027 struct file_extension
*this_extension
=
1028 (struct file_extension
*)malloc(sizeof(file_extension
));
1030 this_extension
->file_extension_string
=
1031 (uschar
*)malloc(Ustrlen(mime_part_p
.extension
)+1);
1032 Ustrcpy(this_extension
->file_extension_string
,
1033 mime_part_p
.extension
+1);
1034 this_extension
->next
= file_extensions
;
1035 file_extensions
= this_extension
;
1040 mime_trigger_error(MIME_ERRORLEVEL_DOUBLE_HEADERS
);
1042 /* ---------------------------------------------------------------------------- */
1044 }; /* End of header checks */
1045 /* ------------------------------------------------ */
1048 /* -------------- non-header mode ----------------- */
1051 if (uu_mode
== MIME_UU_MODE_OFF
) {
1052 uschar uu_file_extension
[5];
1053 /* We are not currently decoding UUENCODE
1054 Check for possible UUENCODE start tag. */
1055 if (mime_check_uu_start(line
,uu_file_extension
,&has_tnef
)) {
1056 /* possible UUENCODING start detected.
1057 Set unconfirmed mode first. */
1058 uu_mode
= MIME_UU_MODE_UNCONFIRMED
;
1059 /* open new uu dump file */
1060 tmp
= mime_get_dump_file(uu_file_extension
, &uu_dump_file
, info
);
1071 if (uu_mode
== MIME_UU_MODE_UNCONFIRMED
) {
1072 /* We are in unconfirmed UUENCODE mode. */
1074 data_len
= uu_decode_line(line
,&data
,line_len
,info
);
1076 if (data_len
== -2) {
1077 /* temp error, turn off uudecode mode */
1078 if (uu_dump_file
!= NULL
) {
1079 (void)fclose(uu_dump_file
); uu_dump_file
= NULL
;
1081 uu_mode
= MIME_UU_MODE_OFF
;
1084 else if (data_len
== -1) {
1085 if (uu_dump_file
!= NULL
) {
1086 (void)fclose(uu_dump_file
); uu_dump_file
= NULL
;
1088 uu_mode
= MIME_UU_MODE_OFF
;
1091 else if (data_len
> 0) {
1092 /* we have at least decoded a valid byte
1093 turn on confirmed mode */
1094 uu_mode
= MIME_UU_MODE_CONFIRMED
;
1097 else if (uu_mode
== MIME_UU_MODE_CONFIRMED
) {
1098 /* If we are in confirmed UU mode,
1099 check for single "end" tag on line */
1100 if ((strncmpic(line
,US
"end",3) == 0) && (line
[3] < 32)) {
1101 if (uu_dump_file
!= NULL
) {
1102 (void)fclose(uu_dump_file
); uu_dump_file
= NULL
;
1104 uu_mode
= MIME_UU_MODE_OFF
;
1107 data_len
= uu_decode_line(line
,&data
,line_len
,info
);
1108 if (data_len
== -2) {
1109 /* temp error, turn off uudecode mode */
1110 if (uu_dump_file
!= NULL
) {
1111 (void)fclose(uu_dump_file
); uu_dump_file
= NULL
;
1113 uu_mode
= MIME_UU_MODE_OFF
;
1116 else if (data_len
== -1) {
1117 /* skip this line */
1123 /* write data to dump file, if available */
1125 if (fwrite(data
,1,data_len
,uu_dump_file
) < data_len
) {
1127 (void)string_format(info
, 1024,"short write on uudecode dump file");
1134 if (mime_demux_mode
!= MIME_DEMUX_MODE_SCANNING
) {
1135 /* Non-scanning and Non-header mode. That means
1136 we are currently decoding data to the dump
1139 /* Check for a known boundary. */
1140 tmp
= mime_check_boundary(line
,boundaries
);
1142 /* We have hit a known start boundary.
1143 That will put us back in header mode. */
1144 mime_demux_mode
= MIME_DEMUX_MODE_MIME_HEADERS
;
1145 if (mime_dump_file
!= NULL
) {
1146 /* if the attachment was a RFC822 message, recurse into it */
1149 rewind(mime_dump_file
);
1150 mime_demux(mime_dump_file
,info
);
1153 (void)fclose(mime_dump_file
); mime_dump_file
= NULL
;
1156 else if (tmp
== 2) {
1157 /* We have hit a known end boundary.
1158 That puts us into scanning mode, which will end when we hit another known start boundary */
1159 mime_demux_mode
= MIME_DEMUX_MODE_SCANNING
;
1160 if (mime_dump_file
!= NULL
) {
1161 /* if the attachment was a RFC822 message, recurse into it */
1164 rewind(mime_dump_file
);
1165 mime_demux(mime_dump_file
,info
);
1168 (void)fclose(mime_dump_file
); mime_dump_file
= NULL
;
1175 /* decode the line with the appropriate method */
1176 if (mime_demux_mode
== MIME_DEMUX_MODE_PLAIN
) {
1177 /* in plain mode, just dump the line */
1179 data_len
= line_len
;
1181 else if ( (mime_demux_mode
== MIME_DEMUX_MODE_QP
) || (mime_demux_mode
== MIME_DEMUX_MODE_BASE64
) ) {
1182 data_len
= mime_decode_line(mime_demux_mode
,line
,&data
,line_len
,info
);
1184 /* Error reported from the line decoder. */
1189 /* write data to dump file */
1191 if (fwrite(data
,1,data_len
,mime_dump_file
) < data_len
) {
1193 (void)string_format(info
, 1024,"short write on dump file");
1202 /* Scanning mode. We end up here after a end boundary.
1203 This will usually be at the end of a message or at
1204 the end of a MIME container.
1205 We need to look for another start boundary to get
1206 back into header mode. */
1207 if (mime_check_boundary(line
,boundaries
) == 1) {
1208 mime_demux_mode
= MIME_DEMUX_MODE_MIME_HEADERS
;
1212 /* ------------------------------------------------ */
1215 /* ----------------------- end demux loop ----------------------- */
1217 /* close files, they could still be open */
1218 if (mime_dump_file
!= NULL
)
1219 (void)fclose(mime_dump_file
);
1220 if (uu_dump_file
!= NULL
)
1221 (void)fclose(uu_dump_file
);
1223 /* release line buffer */
1226 /* FIXME: release boundary buffers.
1227 Not too much of a problem since
1228 this instance of exim is not resident. */
1231 uschar file_name
[1024];
1232 /* at least one file could be TNEF encoded.
1233 attempt to send all decoded files thru the TNEF decoder */
1235 (void)string_format(file_name
,1024,"%s/scan/%s",spool_directory
,message_id
);
1236 /* Removed FTTB. We need to decide on TNEF inclusion */
1237 /* mime_unpack_tnef(file_name); */