Commit | Line | Data |
---|---|---|
a0d6ba8a | 1 | /* $Cambridge: exim/src/src/mime.c,v 1.13 2005/11/15 10:08:25 ph10 Exp $ */ |
8523533c TK |
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; | |
f7b63901 PH |
28 | mime_anomaly_text = US text; |
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 | ||
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; | |
8e669ac1 | 59 | |
8523533c TK |
60 | /* advance one char */ |
61 | qp_p++; | |
8e669ac1 | 62 | |
8523533c TK |
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 | } | |
8e669ac1 | 85 | else if (*qp_p == '\n') { |
8523533c TK |
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 | }; | |
8e669ac1 | 95 | |
8523533c TK |
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; | |
8e669ac1 | 107 | return initial_pos; |
8523533c TK |
108 | }; |
109 | } | |
110 | else { | |
111 | /* illegal char */ | |
112 | *c = -2; | |
8e669ac1 | 113 | return initial_pos; |
8523533c TK |
114 | }; |
115 | } | |
116 | ||
117 | ||
fb2274d4 | 118 | uschar *mime_parse_line(uschar *buffer, uschar *data, uschar *encoding, int *num_decoded) { |
84330b7b | 119 | |
8523533c TK |
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; | |
8e669ac1 | 131 | |
8523533c TK |
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; | |
8e669ac1 | 153 | |
8523533c TK |
154 | /* line is translated, start bit shifting */ |
155 | p = buffer; | |
8e669ac1 | 156 | *num_decoded = 0; |
8523533c TK |
157 | while(*p != 255) { |
158 | uschar tmp_c; | |
8e669ac1 | 159 | |
8523533c TK |
160 | /* byte 0 ---------------------- */ |
161 | if (*(p+1) == 255) { | |
8523533c TK |
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) { | |
8523533c TK |
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) { | |
8523533c TK |
184 | break; |
185 | } | |
186 | data[(*num_decoded)] = *p; | |
187 | data[(*num_decoded)] <<= 6; | |
8e669ac1 | 188 | data[(*num_decoded)] |= *(p+1); |
8523533c TK |
189 | (*num_decoded)++; |
190 | p+=2; | |
8e669ac1 | 191 | |
8523533c TK |
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; | |
8e669ac1 | 204 | |
8523533c | 205 | p = mime_decode_qp_char(p,&decode_qp_result); |
8e669ac1 | 206 | |
8523533c TK |
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) { | |
f7b63901 | 237 | FILE *f = NULL; |
8523533c | 238 | uschar *filename; |
8e669ac1 | 239 | |
8523533c | 240 | filename = (uschar *)malloc(2048); |
8e669ac1 | 241 | |
8523533c | 242 | if ((pname != NULL) && (fname != NULL)) { |
b07e6aa3 | 243 | (void)string_format(filename, 2048, "%s/%s", pname, fname); |
c58b88df | 244 | f = fopen(CS filename,"wb+"); |
8523533c TK |
245 | } |
246 | else if (pname == NULL) { | |
c58b88df | 247 | f = fopen(CS fname,"wb+"); |
8523533c TK |
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; | |
b07e6aa3 | 256 | (void)string_format(filename,2048,"%s/%s-%05u", pname, message_id, file_nr); |
8523533c TK |
257 | file_nr++; |
258 | /* security break */ | |
259 | if (file_nr >= 1024) | |
260 | break; | |
261 | result = stat(CS filename,&mystat); | |
262 | } | |
263 | while(result != -1); | |
c58b88df | 264 | f = fopen(CS filename,"wb+"); |
8523533c | 265 | }; |
8e669ac1 | 266 | |
8523533c TK |
267 | /* set expansion variable */ |
268 | mime_decoded_filename = filename; | |
8e669ac1 | 269 | |
8523533c TK |
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; | |
fb2274d4 | 282 | uschar *decode_buffer = NULL; |
8523533c TK |
283 | long f_pos = 0; |
284 | unsigned int size_counter = 0; | |
285 | ||
286 | if (mime_stream == NULL) | |
287 | return FAIL; | |
8e669ac1 | 288 | |
8523533c | 289 | f_pos = ftell(mime_stream); |
8e669ac1 | 290 | |
8523533c | 291 | /* build default decode path (will exist since MBOX must be spooled up) */ |
b07e6aa3 | 292 | (void)string_format(decode_path,1024,"%s/scan/%s",spool_directory,message_id); |
8e669ac1 | 293 | |
fb2274d4 | 294 | /* reserve a line and decoder buffer to work in */ |
8523533c TK |
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 | }; | |
8e669ac1 | 301 | |
fb2274d4 TK |
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 | ||
8523533c TK |
309 | /* try to find 1st option */ |
310 | if ((option = string_nextinlist(&list, &sep, | |
311 | option_buffer, | |
312 | sizeof(option_buffer))) != NULL) { | |
8e669ac1 | 313 | |
8523533c TK |
314 | /* parse 1st option */ |
315 | if ( (Ustrcmp(option,"false") == 0) || (Ustrcmp(option,"0") == 0) ) { | |
316 | /* explicitly no decoding */ | |
317 | return FAIL; | |
318 | }; | |
8e669ac1 | 319 | |
8523533c TK |
320 | if (Ustrcmp(option,"default") == 0) { |
321 | /* explicit default path + file names */ | |
322 | goto DEFAULT_PATH; | |
323 | }; | |
8e669ac1 | 324 | |
8523533c TK |
325 | if (option[0] == '/') { |
326 | struct stat statbuf; | |
327 | ||
328 | memset(&statbuf,0,sizeof(statbuf)); | |
8e669ac1 | 329 | |
8523533c TK |
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); | |
8e669ac1 | 345 | |
8523533c TK |
346 | if (decode_file == NULL) |
347 | return DEFER; | |
8e669ac1 | 348 | |
8523533c TK |
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; | |
8e669ac1 | 354 | |
8523533c TK |
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 | }; | |
8e669ac1 | 362 | |
fb2274d4 TK |
363 | decoded_line = mime_parse_line(buffer, decode_buffer, mime_content_transfer_encoding, &decoded_line_length); |
364 | ||
8523533c TK |
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; | |
8e669ac1 PH |
373 | |
374 | if (size_counter > 1023) { | |
8523533c TK |
375 | if ((mime_content_size + (size_counter / 1024)) < 65535) |
376 | mime_content_size += (size_counter / 1024); | |
8e669ac1 | 377 | else |
8523533c TK |
378 | mime_content_size = 65535; |
379 | size_counter = (size_counter % 1024); | |
380 | }; | |
8e669ac1 | 381 | |
8523533c | 382 | } |
8e669ac1 | 383 | |
f1e894f3 | 384 | (void)fclose(decode_file); |
8e669ac1 | 385 | |
8523533c TK |
386 | clearerr(mime_stream); |
387 | fseek(mime_stream,f_pos,SEEK_SET); | |
8e669ac1 | 388 | |
8523533c TK |
389 | /* round up remaining size bytes to one k */ |
390 | if (size_counter) { | |
391 | mime_content_size++; | |
392 | }; | |
8e669ac1 | 393 | |
8523533c TK |
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; | |
8e669ac1 | 403 | |
8523533c | 404 | while(!done) { |
8e669ac1 | 405 | |
8523533c TK |
406 | c = fgetc(f); |
407 | if (c == EOF) break; | |
8e669ac1 | 408 | |
8523533c TK |
409 | /* always skip CRs */ |
410 | if (c == '\r') continue; | |
8e669ac1 | 411 | |
8523533c TK |
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; | |
f1e894f3 | 418 | (void)ungetc(c,f); |
8523533c TK |
419 | }; |
420 | /* end of the header, terminate with ';' */ | |
421 | c = ';'; | |
422 | done = 1; | |
423 | }; | |
8e669ac1 | 424 | |
8523533c TK |
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; | |
8e669ac1 | 433 | |
8523533c TK |
434 | /* we have hit a non-whitespace char, start copying value data */ |
435 | header_value_mode = 2; | |
8e669ac1 | 436 | |
8523533c TK |
437 | /* skip quotes */ |
438 | if (c == '"') continue; | |
8e669ac1 | 439 | |
8523533c TK |
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 | }; | |
8e669ac1 | 469 | |
8523533c TK |
470 | /* skip chars while we are in a comment */ |
471 | if (header_open_brackets > 0) | |
472 | continue; | |
473 | /* -------------------------------- */ | |
474 | }; | |
8e669ac1 | 475 | |
8523533c TK |
476 | /* copy the char to the buffer */ |
477 | header[num_copied] = (uschar)c; | |
478 | /* raise counter */ | |
479 | num_copied++; | |
8e669ac1 | 480 | |
8523533c TK |
481 | /* break if header buffer is full */ |
482 | if (num_copied > MIME_MAX_HEADER_SIZE-1) { | |
483 | done = 1; | |
484 | }; | |
485 | }; | |
486 | ||
9f093f3a | 487 | if ((num_copied > 0) && (header[num_copied-1] != ';')) { |
8523533c TK |
488 | header[num_copied-1] = ';'; |
489 | }; | |
490 | ||
491 | /* 0-terminate */ | |
492 | header[num_copied] = '\0'; | |
8e669ac1 | 493 | |
8523533c TK |
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 | ||
54cdb463 PH |
502 | int mime_acl_check(uschar *acl, FILE *f, struct mime_boundary_context *context, |
503 | uschar **user_msgptr, uschar **log_msgptr) { | |
8523533c TK |
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, | |
54cdb463 | 512 | "MIME ACL: can't allocate %d bytes of memory.", MIME_MAX_HEADER_SIZE+1); |
8523533c TK |
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) { | |
8e669ac1 | 522 | |
8523533c | 523 | /* reset all per-part mime variables */ |
f7b63901 | 524 | mime_anomaly_level = 0; |
8523533c TK |
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; | |
8e669ac1 | 537 | |
8523533c TK |
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 | */ | |
8e669ac1 | 544 | |
8523533c TK |
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 | * | |
8e669ac1 | 549 | * (I have moved partway towards adding support, however, by adding |
8523533c TK |
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 | }; | |
8e669ac1 | 575 | |
8523533c TK |
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; | |
8e669ac1 | 584 | |
8523533c TK |
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; | |
8e669ac1 | 601 | |
8523533c TK |
602 | /* make p point to the next character after the closing ';' */ |
603 | p += (header_value_len+1); | |
8e669ac1 | 604 | |
8523533c TK |
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; | |
8e669ac1 | 611 | |
8523533c TK |
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); | |
a0d6ba8a | 622 | param_value = rfc2047_decode(param_value, check_rfc2047_length, NULL, 32, ¶m_value_len, &q); |
8523533c TK |
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 | }; | |
8e669ac1 | 637 | |
8523533c TK |
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; | |
8e669ac1 | 642 | |
8523533c TK |
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; | |
8e669ac1 | 647 | |
8523533c TK |
648 | /* raise global counter */ |
649 | mime_part_count++; | |
8e669ac1 | 650 | |
8523533c TK |
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); | |
8e669ac1 | 657 | |
8523533c | 658 | /* call ACL handling function */ |
54cdb463 | 659 | rc = acl_check(ACL_WHERE_MIME, NULL, acl, user_msgptr, log_msgptr); |
8e669ac1 | 660 | |
8523533c TK |
661 | mime_stream = NULL; |
662 | mime_current_boundary = NULL; | |
8e669ac1 | 663 | |
8523533c | 664 | if (rc != OK) break; |
8e669ac1 | 665 | |
8523533c TK |
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 | ||
54cdb463 | 680 | rc = mime_acl_check(acl, f, &nested_context, user_msgptr, log_msgptr); |
8523533c TK |
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; | |
8e669ac1 | 689 | |
8523533c TK |
690 | /* must find first free sequential filename */ |
691 | do { | |
692 | struct stat mystat; | |
b07e6aa3 | 693 | (void)string_format(filename,2048,"%s/scan/%s/__rfc822_%05u", spool_directory, message_id, file_nr); |
8523533c TK |
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); | |
8e669ac1 | 701 | |
8523533c | 702 | rfc822name = filename; |
8e669ac1 | 703 | |
8523533c TK |
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 | }; | |
8e669ac1 | 719 | |
8523533c TK |
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; | |
8e669ac1 | 726 | |
8523533c TK |
727 | }; |
728 | ||
729 | return rc; | |
730 | } | |
731 | ||
732 | #endif |