mime_decode.c revision 1.1 1 /* $NetBSD: mime_decode.c,v 1.1 2006/10/21 21:37:21 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Anon Ymous.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39
40 #ifdef MIME_SUPPORT
41
42 #include <sys/cdefs.h>
43 #ifndef __lint__
44 __RCSID("$NetBSD: mime_decode.c,v 1.1 2006/10/21 21:37:21 christos Exp $");
45 #endif /* not __lint__ */
46
47 #include <assert.h>
48 #include <err.h>
49 #include <fcntl.h>
50 #include <libgen.h>
51 #include <setjmp.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <iconv.h>
58
59 #include "def.h"
60 #include "extern.h"
61 #ifdef USE_EDITLINE
62 #include "complete.h"
63 #endif
64 #ifdef MIME_SUPPORT
65 #include "mime.h"
66 #include "mime_child.h"
67 #include "mime_codecs.h"
68 #include "mime_header.h"
69 #endif
70 #include "glob.h"
71
72
73 /************************************************
74 * The fundametal data structure for this module!
75 */
76 struct mime_info {
77 struct mime_info *mi_blink;
78 struct mime_info *mi_flink;
79
80 /* sendmessage -> decoder -> filter -> pager */
81
82 FILE *mi_fo; /* output file handle pointing to PAGER */
83 FILE *mi_pipe_end; /* initial end of pipe */
84 FILE *mi_head_end; /* close to here at start of body */
85
86 int mi_partnum; /* part number displayed (if nonzero) */
87
88 const char *mi_version;
89 const char *mi_type;
90 const char *mi_subtype;
91 const char *mi_boundary;
92 const char *mi_charset;
93 const char *mi_encoding;
94
95 struct message *mp; /* MP for this message regarded as a part. */
96 struct {
97 struct mime_info *mip; /* parent of part of multipart message */
98 struct message *mp;
99 } mi_parent;
100
101 const char *mi_command_hook; /* alternate command used to process this message */
102 };
103
104
105 #if 0
106 #ifndef __lint__
107 /*
108 * XXX - This block for debugging only and eventually should go away.
109 */
110 static void
111 show_one_mime_info(FILE *fp, struct mime_info *mip)
112 {
113 #define XX(a) (a) ? (a) : "<null>"
114
115 (void)fprintf(fp, ">> --------\n");
116 (void)fprintf(fp, "mip %d:\n", mip->mi_partnum);
117 (void)fprintf(fp, "** Version: %s\n", XX(mip->mi_version));
118 (void)fprintf(fp, "** type: %s\n", XX(mip->mi_type));
119 (void)fprintf(fp, "** subtype: %s\n", XX(mip->mi_subtype));
120 (void)fprintf(fp, "** charset: %s\n", XX(mip->mi_charset));
121 (void)fprintf(fp, "** encoding: %s\n", XX(mip->mi_encoding));
122 (void)fprintf(fp, "** boundary: %s\n", XX(mip->mi_boundary));
123 (void)fprintf(fp, "** %p: flag: 0x%x, block: %ld, offset: %d, size: %lld, lines: %ld:%ld\n",
124 mip->mp,
125 mip->mp->m_flag,
126 mip->mp->m_block, mip->mp->m_offset, mip->mp->m_size,
127 mip->mp->m_lines, mip->mp->m_blines);
128 (void)fprintf(fp, "** mip: %p\n", mip);
129 (void)fprintf(fp, "** mi_flink: %p\n", mip->mi_flink);
130 (void)fprintf(fp, "** mi_blink: %p\n", mip->mi_blink);
131 (void)fprintf(fp, "** mip %p, mp %p, parent_mip %p, parent_mp %p\n",
132 mip, mip->mp, mip->mi_parent.mip, mip->mi_parent.mp);
133
134 (void)fprintf(fp, "** mi_fo %p, mi_head_end %p, mi_pipe_end %p\n",
135 mip->mi_fo, mip->mi_head_end, mip->mi_pipe_end);
136
137 (void)fprintf(fp, "** mi_partnum: %d\n", mip->mi_partnum);
138
139 (void)fflush(fp);
140
141 #undef XX
142 }
143
144 __unused
145 static void
146 show_mime_info(FILE *fp, struct mime_info *mip, struct mime_info *end_mip)
147 {
148 for (/* EMTPY */; mip != end_mip; mip = mip->mi_flink)
149 show_one_mime_info(fp, mip);
150
151 (void)fprintf(fp, "++ =========\n");
152 (void)fflush(fp);
153 }
154 #endif /* __lint__ */
155 #endif /* #if */
156
157
158 /*
159 * Our interface to the file registry in popen.c
160 */
161 static FILE *
162 pipe_end(struct mime_info *mip)
163 {
164 FILE *fp;
165 fp = last_registered_file(1); /* get last registered pipe */
166 if (fp == NULL)
167 fp = mip->mi_fo;
168 return fp;
169 }
170
171 /*
172 * Copy the first ';' delimited substring from 'src' (null terminated)
173 * into 'dst', expanding quotes and removing comments (as per RFC
174 * 822). Returns a pointer in src to the next non-white character
175 * following ';'. The caller is responsible for ensuring 'dst' is
176 * sufficiently large to hold the result.
177 */
178 static char *
179 get_param(char *dst, char *src)
180 {
181 char *lastq;
182 char *cp;
183 char *cp2;
184 int nesting;
185
186 cp2 = dst;
187 lastq = dst;
188 for (cp = src; *cp && *cp != ';'; cp++) {
189 switch (*cp) {
190 case '"': /* start of quoted string */
191 for (cp++; *cp; cp++) {
192 if (*cp == '"')
193 break;
194 if (*cp == '\\' && cp[1] != '\0')
195 ++cp;
196 *cp2++ = *cp;
197 }
198 lastq = cp2-1;
199 break;
200 case '(': /* start of comment */
201 nesting = 1;
202 while (nesting > 0 && *++cp) {
203 if (*cp == '\\' && cp[1] != '\0')
204 cp++;
205 if (*cp == '(')
206 nesting++;
207 if (*cp == ')')
208 nesting--;
209 }
210 break;
211 default:
212 *cp2++ = *cp;
213 break;
214 }
215 }
216 /* remove trailing white space */
217 while (cp2 > lastq && isblank((unsigned char)cp2[-1]))
218 cp2--;
219 *cp2 = '\0';
220 if (*cp == ';')
221 cp++;
222 cp = skip_white(cp);
223 return cp;
224 }
225
226 /*
227 * Content parameter
228 * if field is NULL, return the content "specifier".
229 */
230 static char*
231 cparam(const char field[], char *src, int downcase)
232 {
233 char *cp;
234 char *dst;
235
236 if (src == NULL)
237 return NULL;
238
239 dst = salloc(strlen(src) + 1); /* large enough for any param in src */
240 cp = skip_white(src);
241 cp = get_param(dst, cp);
242
243 if (field == NULL)
244 return dst;
245
246 while (*cp != '\0') {
247 size_t len = strlen(field);
248 cp = get_param(dst, cp);
249 if (strncasecmp(dst, field, len) == 0 && dst[len] == '=') {
250 char *cp2;
251 cp2 = dst + len + 1;
252 if (downcase)
253 istrcpy(cp2, cp2);
254 return cp2;
255 }
256 }
257 return NULL;
258 }
259
260
261 static void
262 get_content(struct mime_info *mip)
263 {
264 char *mime_type_field;
265 struct message *mp;
266 char *cp;
267
268 mp = mip->mp;
269 mip->mi_version = cparam(NULL, hfield(MIME_HDR_VERSION, mp), 0);
270 mip->mi_encoding = cparam(NULL, hfield(MIME_HDR_ENCODING, mp), 1);
271
272 mime_type_field = hfield(MIME_HDR_TYPE, mp);
273 mip->mi_type = cparam(NULL, mime_type_field, 1);
274 if (mip->mi_type) {
275 cp = strchr(mip->mi_type, '/');
276 if (cp)
277 *cp++ = '\0';
278 mip->mi_subtype = cp;
279 }
280 mip->mi_charset = cparam("charset", mime_type_field, 1);
281 mip->mi_boundary = cparam("boundary", mime_type_field, 0);
282 }
283
284
285 static struct message *
286 salloc_message(int flag, long block, short offset)
287 {
288 struct message *mp;
289 /* use csalloc in case someone adds a field someday! */
290 mp = csalloc(1, sizeof(*mp));
291 mp->m_flag = flag;
292 mp->m_block = block;
293 mp->m_offset = offset;
294 #if 0
295 mp->m_lines = 0;
296 mp->m_size = 0;
297 mp->m_blines = 0;
298 #endif
299 return mp;
300 }
301
302 static struct mime_info *
303 insert_new_mip(struct mime_info *this_mip)
304 {
305 struct mime_info *new_mip;
306 new_mip = csalloc(1, sizeof(*new_mip));
307 new_mip->mi_blink = this_mip;
308 new_mip->mi_flink = this_mip->mi_flink;
309 this_mip->mi_flink = new_mip;
310 this_mip = new_mip;
311 return new_mip;
312 }
313
314 static void
315 split_multipart(struct mime_info *top_mip)
316 {
317 FILE *fp;
318 struct message *top_mp;
319 struct message *this_mp;
320 struct mime_info *this_mip;
321 off_t beg_pos;
322 const char *boundary;
323 size_t boundary_len;
324 long lines_left; /* must be signed and same size as m_lines */
325 int partnum;
326 int in_header;
327
328 top_mp = top_mip->mp;
329 this_mp = salloc_message(top_mp->m_flag, top_mp->m_block, top_mp->m_offset);
330 this_mip = top_mip;
331 this_mip->mp = this_mp;
332
333 partnum = 1;
334 /* top_mip->mi_partnum = partnum++; */ /* Keep the number set by the caller */
335 in_header = 1;
336 boundary = top_mip->mi_boundary;
337 boundary_len = boundary ? strlen(boundary) : 0;
338
339 fp = setinput(top_mp);
340 beg_pos = ftello(fp);
341
342 for (lines_left = top_mp->m_lines - 1; lines_left >= 0; lines_left--) {
343 char *line;
344 size_t line_len;
345
346 line = fgetln(fp, &line_len);
347
348 this_mp->m_lines++; /* count the message lines */
349
350 if (!in_header)
351 this_mp->m_blines++; /* count the body lines */
352
353 if (lines_left == 0 || (
354 !in_header &&
355 line_len >= boundary_len + 2 &&
356 line[0] == '-' && line[1] == '-' &&
357 strncmp(line + 2, boundary, boundary_len) == 0)) {
358 off_t cur_pos;
359 off_t end_pos;
360
361 cur_pos = ftello(fp);
362
363 /* the boundary belongs to the next part */
364 end_pos = cur_pos - line_len;
365 this_mp->m_lines -= 1;
366 this_mp->m_blines -= 1;
367
368 this_mp->m_size = end_pos - beg_pos;
369
370 if (line[boundary_len + 2] == '-' &&
371 line[boundary_len + 3] == '-') {/* end of multipart */
372 /* do a sanity check on the EOM */
373 if (lines_left) {
374 /*
375 * XXX - this can happen!
376 * Should we display the
377 * trailing garbage or check
378 * that it is blank or just
379 * ignore it?
380 */
381 /* (void)printf("EOM: lines left: %ld\n", lines_left); */
382 }
383 break; /* XXX - stop at this point or grab the rest? */
384 }
385
386 this_mip = insert_new_mip(this_mip);
387 this_mp = salloc_message(top_mp->m_flag,
388 (long)blockof(end_pos), offsetof(end_pos));
389 this_mip->mp = this_mp;
390 this_mip->mi_parent.mip = top_mip;
391 this_mip->mi_parent.mp = top_mp;
392 this_mip->mi_partnum = partnum++;
393
394 beg_pos = end_pos;
395 in_header = 1;
396 }
397
398 if (line_len == 1)
399 in_header = 0;
400 }
401 }
402
403 static void
404 split_message(struct mime_info *top_mip)
405 {
406 struct mime_info *this_mip;
407 struct message *top_mp;
408 struct message *this_mp;
409 FILE *fp;
410 off_t beg_pos;
411 long lines_left; /* must be same size as m_lines */
412 int in_header;
413
414 top_mp = top_mip->mp;
415 this_mp = salloc_message(top_mp->m_flag, top_mp->m_block, top_mp->m_offset);
416 this_mip = top_mip;
417 this_mip->mp = this_mp;
418
419 in_header = 1;
420
421 fp = setinput(top_mp);
422 beg_pos = ftello(fp);
423
424 for (lines_left = top_mp->m_lines; lines_left > 0; lines_left--) {
425 size_t line_len;
426
427 (void)fgetln(fp, &line_len);
428
429 this_mp->m_lines++; /* count the message lines */
430 if (!in_header)
431 this_mp->m_blines++; /* count the body lines */
432
433 if (in_header && line_len == 1) { /* end of header */
434 off_t end_pos;
435 end_pos = ftello(fp);
436 this_mp->m_size = end_pos - beg_pos;
437
438 this_mip = insert_new_mip(this_mip);
439 this_mp = salloc_message(top_mp->m_flag,
440 (long)blockof(end_pos), offsetof(end_pos));
441 this_mip->mp = this_mp;
442 this_mip->mi_parent.mip = top_mip;
443 this_mip->mi_parent.mp = top_mp;
444 this_mip->mi_partnum = 0; /* no partnum displayed */
445
446 beg_pos = end_pos;
447 in_header = 0; /* never in header again */
448 }
449 }
450
451 /* close the last message */
452 this_mp->m_size = ftello(fp) - beg_pos;
453 }
454
455
456 static const char *
457 get_command_hook(struct mime_info *mip, const char *domain)
458 {
459 char *key;
460 char *cmd;
461
462 if (mip->mi_type == NULL)
463 return NULL;
464
465 /* XXX - should we use easprintf() here? We are probably
466 * hosed elsewhere if this fails anyway. */
467
468 cmd = NULL;
469 if (mip->mi_subtype) {
470 if (asprintf(&key, "mime%s-%s-%s",
471 domain, mip->mi_type, mip->mi_subtype) == -1) {
472 warn("get_command_hook: subtupe: asprintf");
473 return NULL;
474 }
475 cmd = value(key);
476 free(key);
477 }
478 if (cmd == NULL) {
479 if (asprintf(&key, "mime%s-%s", domain, mip->mi_type) == -1) {
480 warn("get_command_hook: type: asprintf");
481 return NULL;
482 }
483 cmd = value(key);
484 free(key);
485 }
486 return cmd;
487 }
488
489
490 static int
491 is_basic_alternative(struct mime_info *mip)
492 {
493 return
494 strcasecmp(mip->mi_type, "text") == 0 &&
495 strcasecmp(mip->mi_subtype, "plain") == 0;
496 }
497
498 static struct mime_info *
499 select_alternative(struct mime_info *top_mip, struct mime_info *end_mip)
500 {
501 struct mime_info *the_mip; /* the chosen alternate */
502 struct mime_info *this_mip;
503 /*
504 * The alternates are supposed to occur in order of
505 * increasing "complexity". So: if there is at least
506 * one alternate of type "text/plain", use the last
507 * one, otherwise default to the first alternate.
508 */
509 the_mip = top_mip->mi_flink;
510 for (this_mip = top_mip->mi_flink;
511 this_mip != end_mip;
512 this_mip = this_mip->mi_flink) {
513 const char *cmd;
514
515 if (this_mip->mi_type == NULL ||
516 this_mip->mi_subtype == NULL)
517 continue;
518
519 if (is_basic_alternative(this_mip))
520 the_mip = this_mip;
521 else if (
522 (cmd = get_command_hook(this_mip, "-hook")) ||
523 (cmd = get_command_hook(this_mip, "-head")) ||
524 (cmd = get_command_hook(this_mip, "-body"))) {
525 int flags;
526 /* just get the flags. */
527 flags = mime_run_command(cmd, NULL);
528 if ((flags & CMD_FLAG_ALTERNATIVE) != 0)
529 the_mip = this_mip;
530 }
531 }
532 return the_mip;
533 }
534
535
536 static inline int
537 is_multipart(struct mime_info *mip)
538 {
539 return mip->mi_type &&
540 strcasecmp("multipart", mip->mi_type) == 0;
541 }
542 static inline int
543 is_message(struct mime_info *mip)
544 {
545 return mip->mi_type &&
546 strcasecmp("message", mip->mi_type) == 0;
547 }
548
549 static inline int
550 is_alternative(struct mime_info *mip)
551 {
552 return mip->mi_subtype &&
553 strcasecmp("alternative", mip->mi_subtype) == 0;
554 }
555
556
557 /*
558 * Take a mime_info pointer and expand it recursively into all its
559 * mime parts. Only "multipart" and "message" types recursed into;
560 * they are handled separately.
561 */
562 static struct mime_info *
563 expand_mip(struct mime_info *top_mip)
564 {
565 struct mime_info *this_mip;
566 struct mime_info *next_mip;
567
568 next_mip = top_mip->mi_flink;
569
570 if (is_multipart(top_mip)) {
571 split_multipart(top_mip);
572
573 for (this_mip = top_mip->mi_flink;
574 this_mip != next_mip;
575 this_mip = this_mip->mi_flink) {
576 get_content(this_mip);
577 }
578 if (is_alternative(top_mip)) {
579 this_mip = select_alternative(top_mip, next_mip);
580 this_mip->mi_partnum = 0; /* suppress partnum display */
581 this_mip->mi_flink = next_mip;
582 this_mip->mi_blink = top_mip;
583 top_mip->mi_flink = this_mip;
584 }
585 /*
586 * Recurse into each part.
587 */
588 for (this_mip = top_mip->mi_flink;
589 this_mip != next_mip;
590 this_mip = expand_mip(this_mip))
591 continue;
592 }
593 else if (is_message(top_mip)) {
594 split_message(top_mip);
595
596 this_mip = top_mip->mi_flink;
597 if (this_mip) {
598 get_content(this_mip);
599 /*
600 * If the one part is MIME encoded, recurse into it.
601 * XXX - Should this be conditional on subtype "rcs822"?
602 */
603 if (this_mip->mi_type &&
604 this_mip->mi_version &&
605 equal(this_mip->mi_version, MIME_VERSION)) {
606 this_mip->mi_partnum = 0;
607 (void)expand_mip(this_mip);
608 }
609 }
610 }
611
612 return next_mip;
613 }
614
615
616 static int
617 show_partnum(FILE *fp, struct mime_info *mip)
618 {
619 int need_dot;
620 need_dot = 0;
621 if (mip->mi_parent.mip && mip->mi_parent.mip->mi_parent.mip)
622 need_dot = show_partnum(fp, mip->mi_parent.mip);
623
624 if (mip->mi_partnum) {
625 (void)fprintf(fp, "%s%d", need_dot ? "." : "", mip->mi_partnum);
626 need_dot = 1;
627 }
628 return need_dot;
629 }
630
631
632 PUBLIC struct mime_info *
633 mime_decode_open(struct message *mp)
634 {
635 struct mime_info *mip;
636
637 mip = csalloc(1, sizeof(*mip));
638 mip->mp = salloc(sizeof(*mip->mp));
639 *mip->mp = *mp; /* copy this so we can change its m_lines */
640
641 get_content(mip);
642
643 /* RFC 2049 - sec 2 item 1 */
644 if (mip->mi_version == NULL ||
645 !equal(mip->mi_version, MIME_VERSION))
646 return NULL;
647
648 if (mip->mi_type)
649 (void)expand_mip(mip);
650
651 /* show_mime_info(stderr, mip, NULL); */
652
653 return mip;
654 }
655
656
657 PUBLIC void
658 mime_decode_close(struct mime_info *mip)
659 {
660 if (mip)
661 close_top_files(mip->mi_pipe_end);
662 }
663
664
665 struct prefix_line_args_s {
666 const char *prefix;
667 size_t prefixlen;
668 };
669
670 static void
671 prefix_line(FILE *fi, FILE *fo, void *cookie)
672 {
673 struct prefix_line_args_s *args;
674 const char *line;
675 const char *prefix;
676 size_t prefixlen;
677 size_t length;
678
679 args = cookie;
680 prefix = args->prefix;
681 prefixlen = args->prefixlen;
682
683 while ((line = fgetln(fi, &length)) != NULL) {
684 if (length > 1)
685 (void)fputs(prefix, fo);
686 else
687 (void)fwrite(prefix, sizeof *prefix,
688 prefixlen, fo);
689 (void)fwrite(line, sizeof(*line), length, fo);
690 }
691 (void)fflush(fo);
692 }
693
694 PUBLIC int
695 mime_sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
696 const char *prefix, struct mime_info *mip)
697 {
698 int error;
699 FILE *end_of_pipe;
700 FILE *end_of_prefix;
701
702 if (mip == NULL)
703 return sendmessage(mp, obuf, doign ? ignore : 0, prefix, NULL);
704
705 (void)fflush(obuf); /* Be safe and flush! XXX - necessary? */
706
707 /*
708 * Set these early so pipe_end() and mime_decode_close() work!
709 */
710 mip->mi_fo = obuf;
711 mip->mi_pipe_end = last_registered_file(0);
712
713 /*
714 * Handle the prefix as a pipe stage so it doesn't get seen by
715 * any decoding or hooks.
716 */
717 if (prefix != NULL) {
718 static struct prefix_line_args_s prefix_line_args;
719 const char *dp, *dp2 = NULL;
720 for (dp = prefix; *dp; dp++)
721 if (*dp != ' ' && *dp != '\t')
722 dp2 = dp;
723 prefix_line_args.prefixlen = dp2 == 0 ? 0 : dp2 - prefix + 1;
724 prefix_line_args.prefix = prefix;
725 mime_run_function(prefix_line, pipe_end(mip), (void*)&prefix_line_args);
726 }
727
728 end_of_pipe = mip->mi_pipe_end;
729 end_of_prefix = last_registered_file(0);
730 error = 0;
731 for (/* EMPTY */; mip; mip = mip->mi_flink) {
732 mip->mi_fo = obuf;
733 mip->mi_pipe_end = end_of_pipe;
734 error |= sendmessage(mip->mp, pipe_end(mip), doign ? ignore : 0, NULL, mip);
735 close_top_files(end_of_prefix); /* don't close the prefixer! */
736 }
737 return error;
738 }
739
740
741 #ifdef CHARSET_SUPPORT
742 /**********************************************
743 * higher level interface to run mime_ficonv().
744 *
745 */
746 static void
747 run_mime_ficonv(struct mime_info *mip, const char *charset)
748 {
749 FILE *fo;
750 iconv_t cd;
751
752 fo = pipe_end(mip);
753
754 if (charset == NULL ||
755 mip->mi_charset == NULL ||
756 strcasecmp(mip->mi_charset, charset) == 0 ||
757 strcasecmp(mip->mi_charset, "unknown") == 0)
758 return;
759
760 cd = iconv_open(charset, mip->mi_charset);
761 if (cd == (iconv_t)-1) {
762 (void)fprintf(fo, "\t [ iconv_open failed: %s ]\n\n",
763 strerror(errno));
764 (void)fflush(fo); /* flush here or see double! */
765 return;
766 }
767
768 if (value(ENAME_MIME_CHARSET_VERBOSE))
769 (void)fprintf(fo, "\t[ converting %s -> %s ]\n\n", mip->mi_charset, charset);
770
771 mime_run_function(mime_ficonv, fo, cd);
772
773 iconv_close(cd);
774 }
775 #endif /* CHARSET_SUPPORT */
776
777
778 static void
779 run_decoder(struct mime_info *mip, void(*fn)(FILE*, FILE*, void *))
780 {
781 #ifdef CHARSET_SUPPORT
782 char *charset;
783
784 charset = value(ENAME_MIME_CHARSET);
785 if (charset && mip->mi_type && strcasecmp(mip->mi_type, "text") == 0)
786 run_mime_ficonv(mip, charset);
787 #endif /* CHARSET_SUPPORT */
788
789 if (fn == mime_fio_copy)/* XXX - avoid an extra unnecessary pipe stage */
790 return;
791
792 mime_run_function(fn, pipe_end(mip), (void*)1);
793 }
794
795
796 /*
797 * Determine how to handle the display based on the type and subtype
798 * fields.
799 */
800 enum dispmode_e {
801 DM_IGNORE = 0x00, /* silently ignore part - must be zero! */
802 DM_DISPLAY, /* decode and display the part */
803 DM_UNKNOWN, /* unknown display */
804 DM_BINARY, /* indicate binary data */
805 DM_PGPSIGN, /* OpenPGP signed part */
806 DM_PGPENCR, /* OpenPGP encrypted part */
807 DM_PGPKEYS /* OpenPGP keys part */
808 };
809 #define APPLICATION_OCTET_STREAM DM_BINARY
810
811 static enum dispmode_e
812 get_display_mode(struct mime_info *mip, mime_codec_t dec)
813 {
814 struct mime_subtype_s {
815 const char *st_name;
816 enum dispmode_e st_dispmode;
817 };
818 struct mime_type_s {
819 const char *mt_type;
820 const struct mime_subtype_s *mt_subtype;
821 enum dispmode_e mt_dispmode; /* default if NULL subtype */
822 };
823 static const struct mime_subtype_s text_subtype_tbl[] = {
824 { "plain", DM_DISPLAY },
825 { "html", DM_DISPLAY }, /* rfc2854 */
826 { "rfc822-headers", DM_DISPLAY },
827 { "css", DM_DISPLAY }, /* rfc2318 */
828 { "enriched", DM_DISPLAY }, /* rfc1523/rfc1563/rfc1896 */
829 { "graphics", DM_DISPLAY }, /* rfc0553 */
830 { "nroff", DM_DISPLAY }, /* rfc4263 */
831 { "red", DM_DISPLAY }, /* rfc4102 */
832 { NULL, DM_DISPLAY } /* default */
833 };
834 static const struct mime_subtype_s image_subtype_tbl[] = {
835 { "tiff", DM_BINARY }, /* rfc2302/rfc3302 */
836 { "tiff-fx", DM_BINARY }, /* rfc3250/rfc3950 */
837 { "t38", DM_BINARY }, /* rfc3362 */
838 { NULL, DM_BINARY } /* default */
839 };
840 static const struct mime_subtype_s audio_subtype_tbl[] = {
841 { "mpeg", DM_BINARY }, /* rfc3003 */
842 { "t38", DM_BINARY }, /* rfc4612 */
843 { NULL, DM_BINARY } /* default */
844 };
845 static const struct mime_subtype_s video_subtype_tbl[] = {
846 { NULL, DM_BINARY } /* default */
847 };
848 static const struct mime_subtype_s application_subtype_tbl[] = {
849 { "octet-stream", APPLICATION_OCTET_STREAM },
850 { "pgp-encrypted", DM_PGPENCR }, /* rfc3156 */
851 { "pgp-keys", DM_PGPKEYS }, /* rfc3156 */
852 { "pgp-signature", DM_PGPSIGN }, /* rfc3156 */
853 { "pdf", DM_BINARY }, /* rfc3778 */
854 { "whoispp-query", DM_UNKNOWN }, /* rfc2957 */
855 { "whoispp-response", DM_UNKNOWN }, /* rfc2958 */
856 { "font-tdpfr", DM_UNKNOWN }, /* rfc3073 */
857 { "xhtml+xml", DM_UNKNOWN }, /* rfc3236 */
858 { "ogg", DM_UNKNOWN }, /* rfc3534 */
859 { "rdf+xml", DM_UNKNOWN }, /* rfc3870 */
860 { "soap+xml", DM_UNKNOWN }, /* rfc3902 */
861 { "mbox", DM_UNKNOWN }, /* rfc4155 */
862 { "xv+xml", DM_UNKNOWN }, /* rfc4374 */
863 { "smil", DM_UNKNOWN }, /* rfc4536 */
864 { "smil+xml", DM_UNKNOWN }, /* rfc4536 */
865 { "json", DM_UNKNOWN }, /* rfc4627 */
866 { "voicexml+xml", DM_UNKNOWN }, /* rfc4267 */
867 { "ssml+xml", DM_UNKNOWN }, /* rfc4267 */
868 { "srgs", DM_UNKNOWN }, /* rfc4267 */
869 { "srgs+xml", DM_UNKNOWN }, /* rfc4267 */
870 { "ccxml+xml", DM_UNKNOWN }, /* rfc4267 */
871 { "pls+xml.", DM_UNKNOWN }, /* rfc4267 */
872 { NULL, APPLICATION_OCTET_STREAM } /* default */
873 };
874 static const struct mime_type_s mime_type_tbl[] = {
875 { "text", text_subtype_tbl, DM_DISPLAY },
876 { "image", image_subtype_tbl, DM_IGNORE },
877 { "audio", audio_subtype_tbl, DM_IGNORE },
878 { "video", video_subtype_tbl, DM_IGNORE },
879 { "application", application_subtype_tbl, APPLICATION_OCTET_STREAM },
880 { "NULL", NULL, DM_UNKNOWN }, /* default */
881 };
882 const struct mime_type_s *mtp;
883 const struct mime_subtype_s *stp;
884 const char *mi_type;
885 const char *mi_subtype;
886
887 /*
888 * Silently ignore all multipart bodies.
889 * 1) In the case of "multipart" types, this typically
890 * contains a message for non-mime enabled mail readers.
891 * 2) In the case of "message" type, there should be no body.
892 */
893 if (is_multipart(mip) || is_message(mip))
894 return DM_IGNORE;
895
896 /*
897 * If the encoding type given but not recognized, treat block
898 * as "application/octet-stream". rfc 2049 sec 2 part 2.
899 */
900 if (mip->mi_encoding && dec == NULL)
901 return APPLICATION_OCTET_STREAM;
902
903 mi_type = mip->mi_type;
904 mi_subtype = mip->mi_type ? mip->mi_subtype : NULL;
905
906 /*
907 * If there was no type specified, display anyway so we don't
908 * miss anything. (The encoding type is known.)
909 */
910 if (mi_type == NULL)
911 return DM_DISPLAY; /* XXX - default to something safe! */
912
913 for (mtp = mime_type_tbl; mtp->mt_type; mtp++) {
914 if (strcasecmp(mtp->mt_type, mi_type) == 0) {
915 if (mi_subtype == NULL)
916 return mtp->mt_dispmode;
917 for (stp = mtp->mt_subtype; stp->st_name; stp++) {
918 if (strcasecmp(stp->st_name, mi_subtype) == 0)
919 return stp->st_dispmode;
920 }
921 return stp->st_dispmode;
922 }
923 }
924 return mtp->mt_dispmode;
925 }
926
927
928 PUBLIC FILE *
929 mime_decode_body(struct mime_info *mip)
930 {
931 static enum dispmode_e dispmode;
932 mime_codec_t dec;
933 const char *cmd;
934
935 /* close anything left over from mime_decode_head() */
936 close_top_files(mip->mi_head_end);
937
938 /*
939 * Make sure we flush everything down the pipe so children
940 * don't see it.
941 */
942 (void)fflush(pipe_end(mip));
943
944 cmd = NULL;
945 if (mip->mi_command_hook == NULL)
946 cmd = get_command_hook(mip, "-body");
947
948 dec = mime_fio_decoder(mip->mi_encoding);
949 /*
950 * If there is a filter running, we need to send the message
951 * to it. Otherwise, get the default display mode for this body.
952 */
953 dispmode = cmd || mip->mi_command_hook ? DM_DISPLAY : get_display_mode(mip, dec);
954
955 if (dec == NULL) /* make sure we have a usable decoder */
956 dec = mime_fio_decoder(MIME_TRANSFER_7BIT);
957
958 if (dispmode == DM_DISPLAY) {
959 int flags;
960 if (cmd == NULL)
961 /* just get the flags */
962 flags = mime_run_command(mip->mi_command_hook, NULL);
963 else
964 flags = mime_run_command(cmd, pipe_end(mip));
965 if ((flags & CMD_FLAG_NO_DECODE) == 0)
966 run_decoder(mip, dec);
967 return pipe_end(mip);
968 }
969 else {
970 static const struct msg_tbl_s {
971 enum dispmode_e dm;
972 const char *msg;
973 } msg_tbl[] = {
974 { DM_BINARY, "binary content" },
975 { DM_PGPSIGN, "OpenPGP signature" },
976 { DM_PGPENCR, "OpenPGP encrypted" },
977 { DM_PGPKEYS, "OpenPGP keys" },
978 { DM_UNKNOWN, "unknown data" },
979 { DM_IGNORE, NULL },
980 { -1, NULL },
981 };
982 const struct msg_tbl_s *mp;
983
984 for (mp = msg_tbl; mp->dm != -1; mp++)
985 if (mp->dm == dispmode)
986 break;
987
988 assert(mp->dm != -1); /* msg_tbl is short if this happens! */
989
990 if (mp->msg)
991 (void)fprintf(pipe_end(mip), " [%s]\n\n", mp->msg);
992
993 return NULL;
994 }
995 }
996
997
998
999
1000 /************************************************************************
1001 * Higher level header decoding interface.
1002 *
1003 * The core routines are in mime_header.c.
1004 */
1005
1006 PUBLIC char *
1007 mime_decode_hfield(char *linebuf, size_t bufsize, char *hdrstr)
1008 {
1009 hfield_decoder_t decode;
1010 decode = mime_hfield_decoder(hdrstr);
1011 if (decode) {
1012 decode(linebuf, bufsize, hdrstr);
1013 return linebuf;
1014 }
1015 return hdrstr;
1016 }
1017
1018 /*
1019 * Return the next header field found in the given message.
1020 * Return >= 0 if something found, < 0 elsewise.
1021 * "colon" is set to point to the colon in the header.
1022 */
1023 static int
1024 get_folded_hfield(FILE *f, char *linebuf, size_t bufsize, int rem, char **colon)
1025 {
1026 char *cp, *cp2;
1027 char *line;
1028 size_t len;
1029
1030 for (;;) {
1031 if (--rem <= 0)
1032 return -1;
1033 if ((cp = fgetln(f, &len)) == NULL)
1034 return -1;
1035 for (cp2 = cp; isprint((unsigned char)*cp2) &&
1036 !isblank((unsigned char)*cp2) && *cp2 != ':'; cp2++)
1037 continue;
1038 len = MIN(bufsize - 1, len);
1039 bufsize -= len;
1040 (void)memcpy(linebuf, cp, len);
1041 *colon = *cp2 == ':' ? linebuf + (cp2 - cp) : NULL;
1042 line = linebuf + len;
1043 for (/*EMPTY*/; rem > 0; rem--) {
1044 int c;
1045 (void)ungetc(c = getc(f), f);
1046 if (c == EOF || !isblank((unsigned char)c))
1047 break;
1048
1049 if ((cp = fgetln(f, &len)) == NULL)
1050 break;
1051 len = MIN(bufsize - 1, len);
1052 bufsize -= len;
1053 if (len == 0)
1054 break;
1055 (void)memcpy(line, cp, len);
1056 line += len;
1057 }
1058 *line = 0;
1059 return rem;
1060 /* NOTREACHED */
1061 }
1062 }
1063
1064 static void
1065 decode_header(FILE *fi, FILE *fo, void *cookie __unused)
1066 {
1067 char linebuf[LINESIZE];
1068 char *colon;
1069 #ifdef __lint__
1070 cookie = cookie;
1071 #endif
1072 while(get_folded_hfield(fi, linebuf, sizeof(linebuf), INT_MAX, &colon) >= 0) {
1073 char decbuf[LINESIZE];
1074 char *hdrstr;
1075 hdrstr = linebuf;
1076 if (colon)
1077 hdrstr = mime_decode_hfield(decbuf, sizeof(decbuf), hdrstr);
1078 (void)fprintf(fo, hdrstr);
1079 }
1080 }
1081
1082
1083 PUBLIC FILE *
1084 mime_decode_header(struct mime_info *mip)
1085 {
1086 int flags;
1087 const char *cmd;
1088 FILE *fo;
1089
1090 fo = pipe_end(mip);
1091 /*
1092 * Make sure we flush everything down the pipe so children
1093 * don't see it.
1094 */
1095
1096 if (mip->mi_partnum) {
1097 (void)fprintf(fo, "----- Part ");
1098 (void)show_partnum(fo, mip);
1099 (void)fprintf(fo, " -----\n");
1100 }
1101 (void)fflush(fo);
1102
1103 /*
1104 * install the message hook before the head hook.
1105 */
1106 cmd = get_command_hook(mip, "-hook");
1107 mip->mi_command_hook = cmd;
1108 if (cmd) {
1109 flags = mime_run_command(cmd, pipe_end(mip));
1110 mip->mi_head_end = last_registered_file(0);
1111 }
1112 else {
1113 cmd = get_command_hook(mip, "-head");
1114 mip->mi_head_end = last_registered_file(0);
1115 flags = mime_run_command(cmd, pipe_end(mip));
1116 }
1117
1118 if (value(ENAME_MIME_DECODE_HDR) && (flags & CMD_FLAG_NO_DECODE) == 0)
1119 mime_run_function(decode_header, pipe_end(mip), NULL);
1120
1121 return pipe_end(mip);
1122 }
1123
1124 #endif /* MIME_SUPPORT */
1125