mime_decode.c revision 1.5 1 /* $NetBSD: mime_decode.c,v 1.5 2006/11/28 18:46:04 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.5 2006/11/28 18:46:04 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 #include "mime_detach.h"
70 #endif
71 #include "glob.h"
72 #include "thread.h"
73
74 #if 0
75 #ifndef __lint__
76 /*
77 * XXX - This block for debugging only and eventually should go away.
78 */
79 static void
80 show_one_mime_info(FILE *fp, struct mime_info *mip)
81 {
82 #define XX(a) (a) ? (a) : "<null>"
83
84 (void)fprintf(fp, ">> --------\n");
85 (void)fprintf(fp, "mip %d:\n", mip->mi_partnum);
86 (void)fprintf(fp, "** Version: %s\n", XX(mip->mi_version));
87 (void)fprintf(fp, "** type: %s\n", XX(mip->mi_type));
88 (void)fprintf(fp, "** subtype: %s\n", XX(mip->mi_subtype));
89 (void)fprintf(fp, "** boundary: %s\n", XX(mip->mi_boundary));
90 (void)fprintf(fp, "** charset: %s\n", XX(mip->mi_charset));
91 (void)fprintf(fp, "** encoding: %s\n", XX(mip->mi_encoding));
92 (void)fprintf(fp, "** disposition: %s\n", XX(mip->mi_disposition));
93 (void)fprintf(fp, "** filename: %s\n", XX(mip->mi_filename));
94 (void)fprintf(fp, "** %p: flag: 0x%x, block: %ld, offset: %d, size: %lld, lines: %ld:%ld\n",
95 mip->mp,
96 mip->mp->m_flag,
97 mip->mp->m_block, mip->mp->m_offset, mip->mp->m_size,
98 mip->mp->m_lines, mip->mp->m_blines);
99 (void)fprintf(fp, "** mip: %p\n", mip);
100 (void)fprintf(fp, "** mi_flink: %p\n", mip->mi_flink);
101 (void)fprintf(fp, "** mi_blink: %p\n", mip->mi_blink);
102 (void)fprintf(fp, "** mip %p, mp %p, parent_mip %p, parent_mp %p\n",
103 mip, mip->mp, mip->mi_parent.mip, mip->mi_parent.mp);
104
105 (void)fprintf(fp, "** mi_fo %p, mi_head_end %p, mi_pipe_end %p\n",
106 mip->mi_fo, mip->mi_head_end, mip->mi_pipe_end);
107
108 (void)fprintf(fp, "** mi_ignore_body: %d\n", mip->mi_ignore_body);
109 (void)fprintf(fp, "** mi_partnum: %d\n", mip->mi_partnum);
110 (void)fprintf(fp, "** mi_partstr: %s\n", mip->mi_partstr);
111 (void)fprintf(fp, "** mi_msgstr: %d\n", mip->mi_msgstr);
112
113 (void)fflush(fp);
114
115 #undef XX
116 }
117
118 __unused
119 static void
120 show_mime_info(FILE *fp, struct mime_info *mip, struct mime_info *end_mip)
121 {
122 for (/* EMTPY */; mip != end_mip; mip = mip->mi_flink)
123 show_one_mime_info(fp, mip);
124
125 (void)fprintf(fp, "++ =========\n");
126 (void)fflush(fp);
127 }
128 #endif /* __lint__ */
129 #endif /* #if */
130
131
132 /*
133 * Our interface to the file registry in popen.c
134 */
135 PUBLIC FILE *
136 pipe_end(struct mime_info *mip)
137 {
138 FILE *fp;
139 fp = last_registered_file(mip->mi_detachdir ? 0 : 1); /* get last registered pipe */
140 if (fp == NULL)
141 fp = mip->mi_fo;
142 return fp;
143 }
144
145 /*
146 * Copy the first ';' delimited substring from 'src' (null terminated)
147 * into 'dst', expanding quotes and removing comments (as per RFC
148 * 822). Returns a pointer in src to the next non-white character
149 * following ';'. The caller is responsible for ensuring 'dst' is
150 * sufficiently large to hold the result.
151 */
152 static char *
153 get_param(char *dst, char *src)
154 {
155 char *lastq;
156 char *cp;
157 char *cp2;
158 int nesting;
159
160 cp2 = dst;
161 lastq = dst;
162 for (cp = src; *cp && *cp != ';'; cp++) {
163 switch (*cp) {
164 case '"': /* start of quoted string */
165 for (cp++; *cp; cp++) {
166 if (*cp == '"')
167 break;
168 if (*cp == '\\' && cp[1] != '\0')
169 ++cp;
170 *cp2++ = *cp;
171 }
172 lastq = cp2-1;
173 break;
174 case '(': /* start of comment */
175 nesting = 1;
176 while (nesting > 0 && *++cp) {
177 if (*cp == '\\' && cp[1] != '\0')
178 cp++;
179 if (*cp == '(')
180 nesting++;
181 if (*cp == ')')
182 nesting--;
183 }
184 break;
185 default:
186 *cp2++ = *cp;
187 break;
188 }
189 }
190 /* remove trailing white space */
191 while (cp2 > lastq && isblank((unsigned char)cp2[-1]))
192 cp2--;
193 *cp2 = '\0';
194 if (*cp == ';')
195 cp++;
196 cp = skip_blank(cp);
197 return cp;
198 }
199
200 /*
201 * Content parameter
202 * if field is NULL, return the content "specifier".
203 */
204 static char*
205 cparam(const char field[], char *src, int downcase)
206 {
207 char *cp;
208 char *dst;
209
210 if (src == NULL)
211 return NULL;
212
213 dst = salloc(strlen(src) + 1); /* large enough for any param in src */
214 cp = skip_blank(src);
215 cp = get_param(dst, cp);
216
217 if (field == NULL)
218 return dst;
219
220 while (*cp != '\0') {
221 size_t len = strlen(field);
222 cp = get_param(dst, cp);
223 if (strncasecmp(dst, field, len) == 0 && dst[len] == '=') {
224 char *cp2;
225 cp2 = dst + len + 1;
226 if (downcase)
227 istrcpy(cp2, cp2);
228 return cp2;
229 }
230 }
231 return NULL;
232 }
233
234
235 static void
236 get_content(struct mime_info *mip)
237 {
238 char *mime_disposition_field;
239 char *mime_type_field;
240 char *filename;
241 struct message *mp;
242 char *cp;
243
244 mp = mip->mp;
245 mip->mi_version = cparam(NULL, hfield(MIME_HDR_VERSION, mp), 0);
246 mip->mi_encoding = cparam(NULL, hfield(MIME_HDR_ENCODING, mp), 1);
247
248 mime_type_field = hfield(MIME_HDR_TYPE, mp);
249 mip->mi_type = cparam(NULL, mime_type_field, 1);
250 if (mip->mi_type) {
251 cp = strchr(mip->mi_type, '/');
252 if (cp)
253 *cp++ = '\0';
254 mip->mi_subtype = cp;
255 }
256 mip->mi_boundary = cparam("boundary", mime_type_field, 0);
257 mip->mi_charset = cparam("charset", mime_type_field, 1);
258
259 mime_disposition_field = hfield(MIME_HDR_DISPOSITION, mp);
260 mip->mi_disposition = cparam(NULL, mime_disposition_field, 1);
261 /*
262 * The type field typically has a "name" parameter for "image"
263 * and "video" types, and I assume for other types as well.
264 * We grab it, but override it if the disposition field has a
265 * filename parameter as it often does for "attachments".
266 * More careful analysis could be done, but this seems to work
267 * pretty well.
268 */
269 filename = cparam("name", mime_type_field, 0);
270 if ((cp = cparam("filename", mime_disposition_field, 0)) != NULL)
271 filename = cp;
272 if (filename) {
273 filename = basename(filename); /* avoid absolute pathnames */
274 filename = savestr(filename); /* save it! */
275 }
276 mip->mi_filename = filename;
277 }
278
279
280 static struct message *
281 salloc_message(int flag, long block, short offset)
282 {
283 struct message *mp;
284 /* use csalloc in case someone adds a field someday! */
285 mp = csalloc(1, sizeof(*mp));
286 mp->m_flag = flag;
287 mp->m_block = block;
288 mp->m_offset = offset;
289 #if 0
290 mp->m_lines = 0;
291 mp->m_size = 0;
292 mp->m_blines = 0;
293 #endif
294 return mp;
295 }
296
297 static struct mime_info *
298 insert_new_mip(struct mime_info *this_mip, struct mime_info *top_mip,
299 struct message *top_mp, off_t end_pos, int partnum)
300 {
301 struct mime_info *new_mip;
302
303 new_mip = csalloc(1, sizeof(*new_mip));
304 new_mip->mi_blink = this_mip;
305 new_mip->mi_flink = this_mip->mi_flink;
306 this_mip->mi_flink = new_mip;
307
308 new_mip->mp = salloc_message(this_mip->mp->m_flag,
309 (long)blockof(end_pos), offsetof(end_pos));
310
311 new_mip->mi_parent.mip = top_mip;
312 new_mip->mi_parent.mp = top_mp;
313 new_mip->mi_partnum = partnum;
314
315 return new_mip;
316 }
317
318 static void
319 split_multipart(struct mime_info *top_mip)
320 {
321 FILE *fp;
322 struct message *top_mp;
323 struct message *this_mp;
324 struct mime_info *this_mip;
325 off_t beg_pos;
326 const char *boundary;
327 size_t boundary_len;
328 long lines_left; /* must be signed and same size as m_lines */
329 int partnum;
330 int in_header;
331
332 top_mp = top_mip->mp;
333 this_mp = salloc_message(top_mp->m_flag, top_mp->m_block, top_mp->m_offset);
334 this_mip = top_mip;
335 this_mip->mp = this_mp;
336
337 partnum = 1;
338 /* top_mip->mi_partnum = partnum++; */ /* Keep the number set by the caller */
339 in_header = 1;
340 boundary = top_mip->mi_boundary;
341 boundary_len = boundary ? strlen(boundary) : 0;
342
343 fp = setinput(top_mp);
344 beg_pos = ftello(fp);
345 #if 0
346 warnx("beg_pos: %lld, m_lines: %ld, m_blines: %ld",
347 beg_pos, top_mp->m_lines, top_mp->m_blines);
348 #endif
349 for (lines_left = top_mp->m_lines - 1; lines_left >= 0; lines_left--) {
350 char *line;
351 size_t line_len;
352
353 line = fgetln(fp, &line_len);
354
355 this_mp->m_lines++; /* count the message lines */
356
357 if (!in_header)
358 this_mp->m_blines++; /* count the body lines */
359
360 if (lines_left == 0 || (
361 !in_header &&
362 line_len >= boundary_len + 2 &&
363 line[0] == '-' && line[1] == '-' &&
364 strncmp(line + 2, boundary, boundary_len) == 0)) {
365 off_t cur_pos;
366 off_t end_pos;
367
368 cur_pos = ftello(fp);
369
370 /* the boundary belongs to the next part */
371 end_pos = cur_pos - line_len;
372 this_mp->m_lines -= 1;
373 this_mp->m_blines -= 1;
374
375 this_mp->m_size = end_pos - beg_pos;
376 #if 0
377 warnx("end_pos: %lld, m_lines: %ld, m_blines: %ld",
378 end_pos, this_mp->m_lines, this_mp->m_blines);
379 #endif
380 if (line[boundary_len + 2] == '-' &&
381 line[boundary_len + 3] == '-') {/* end of multipart */
382 /* do a sanity check on the EOM */
383 if (lines_left != 1) {
384 /*
385 * XXX - this can happen!
386 * Should we display the
387 * trailing garbage or check
388 * that it is blank or just
389 * ignore it?
390 */
391 #if 0
392 (void)printf("EOM: lines left: %ld\n", lines_left);
393 #endif
394 }
395 break; /* XXX - stop at this point or grab the rest? */
396 }
397 this_mip = insert_new_mip(this_mip, top_mip, top_mp, end_pos, partnum++);
398 this_mp = this_mip->mp;
399 this_mp->m_lines = 1; /* already read the first line in the header! */
400 beg_pos = end_pos;
401 in_header = 1;
402 }
403
404 if (line_len == 1)
405 in_header = 0;
406 }
407 }
408
409 static void
410 split_message(struct mime_info *top_mip)
411 {
412 struct mime_info *this_mip;
413 struct message *top_mp;
414 struct message *this_mp;
415 FILE *fp;
416 off_t beg_pos;
417 long lines_left; /* must be same size as m_lines */
418 int in_header;
419
420 top_mp = top_mip->mp;
421 this_mp = salloc_message(top_mp->m_flag, top_mp->m_block, top_mp->m_offset);
422 this_mip = top_mip;
423 this_mip->mp = this_mp;
424
425 in_header = 1;
426
427 fp = setinput(top_mp);
428 beg_pos = ftello(fp);
429
430 for (lines_left = top_mp->m_lines; lines_left > 0; lines_left--) {
431 size_t line_len;
432
433 (void)fgetln(fp, &line_len);
434
435 this_mp->m_lines++; /* count the message lines */
436 if (!in_header)
437 this_mp->m_blines++; /* count the body lines */
438
439 if (in_header && line_len == 1) { /* end of header */
440 off_t end_pos;
441 end_pos = ftello(fp);
442 this_mp->m_size = end_pos - beg_pos;
443 this_mip = insert_new_mip(this_mip, top_mip,top_mp, end_pos, 0);
444 this_mp = this_mip->mp;
445 this_mp->m_lines = 1; /* we already counted one line in the header! */
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 if (top_mip->mi_partnum == 0) {
569 if (top_mip->mi_blink)
570 top_mip->mi_partstr = top_mip->mi_blink->mi_partstr;
571 }
572 else if (top_mip->mi_parent.mip) {
573 const char *prefix;
574 char *cp;
575 prefix = top_mip->mi_parent.mip->mi_partstr;
576 (void)sasprintf(&cp, "%s%s%d", prefix,
577 *prefix ? "." : "", top_mip->mi_partnum);
578 top_mip->mi_partstr = cp;
579 }
580
581 next_mip = top_mip->mi_flink;
582
583 if (is_multipart(top_mip)) {
584 top_mip->mi_ignore_body = 1; /* the first body is ignored */
585 split_multipart(top_mip);
586
587 for (this_mip = top_mip->mi_flink;
588 this_mip != next_mip;
589 this_mip = this_mip->mi_flink) {
590 get_content(this_mip);
591 }
592 if (is_alternative(top_mip)) {
593 this_mip = select_alternative(top_mip, next_mip);
594 this_mip->mi_partnum = 0; /* suppress partnum display */
595 this_mip->mi_flink = next_mip;
596 this_mip->mi_blink = top_mip;
597 top_mip->mi_flink = this_mip;
598 }
599 /*
600 * Recurse into each part.
601 */
602 for (this_mip = top_mip->mi_flink;
603 this_mip != next_mip;
604 this_mip = expand_mip(this_mip))
605 continue;
606 }
607 else if (is_message(top_mip)) {
608 top_mip->mi_ignore_body = 1; /* the first body is ignored */
609 split_message(top_mip);
610
611 this_mip = top_mip->mi_flink;
612 if (this_mip) {
613 get_content(this_mip);
614 /*
615 * If the one part is MIME encoded, recurse into it.
616 * XXX - Should this be conditional on subtype "rcs822"?
617 */
618 if (this_mip->mi_type &&
619 this_mip->mi_version &&
620 equal(this_mip->mi_version, MIME_VERSION)) {
621 this_mip->mi_partnum = 0;
622 (void)expand_mip(this_mip);
623 }
624 }
625 }
626 return next_mip;
627 }
628
629
630 #if 0
631 static int
632 show_partnum(FILE *fp, struct mime_info *mip)
633 {
634 int need_dot;
635 need_dot = 0;
636 if (mip->mi_parent.mip && mip->mi_parent.mip->mi_parent.mip)
637 need_dot = show_partnum(fp, mip->mi_parent.mip);
638
639 if (mip->mi_partnum) {
640 (void)fprintf(fp, "%s%d", need_dot ? "." : "", mip->mi_partnum);
641 need_dot = 1;
642 }
643 return need_dot;
644 }
645 #endif
646
647
648 PUBLIC struct mime_info *
649 mime_decode_open(struct message *mp)
650 {
651 struct mime_info *mip;
652 struct mime_info *p;
653
654 mip = csalloc(1, sizeof(*mip));
655 mip->mp = salloc(sizeof(*mip->mp));
656 *mip->mp = *mp; /* copy this so we don't trash the master mp */
657
658 get_content(mip);
659
660 /* RFC 2049 - sec 2 item 1 */
661 if (mip->mi_version == NULL ||
662 !equal(mip->mi_version, MIME_VERSION))
663 return NULL;
664
665 mip->mi_partstr = "";
666 if (mip->mi_type)
667 (void)expand_mip(mip);
668
669 /*
670 * Get the pipe_end and propagate it down the chain.
671 */
672 mip->mi_pipe_end = last_registered_file(0); /* for mime_decode_close() */
673 for (p = mip->mi_flink; p; p = p->mi_flink)
674 p->mi_pipe_end = mip->mi_pipe_end;
675
676 /* show_mime_info(stderr, mip, NULL); */
677
678 return mip;
679 }
680
681
682 PUBLIC void
683 mime_decode_close(struct mime_info *mip)
684 {
685 if (mip)
686 close_top_files(mip->mi_pipe_end);
687 }
688
689
690 struct prefix_line_args_s {
691 const char *prefix;
692 size_t prefixlen;
693 };
694
695 static void
696 prefix_line(FILE *fi, FILE *fo, void *cookie)
697 {
698 struct prefix_line_args_s *args;
699 const char *line;
700 const char *prefix;
701 size_t prefixlen;
702 size_t length;
703
704 args = cookie;
705 prefix = args->prefix;
706 prefixlen = args->prefixlen;
707
708 while ((line = fgetln(fi, &length)) != NULL) {
709 if (length > 1)
710 (void)fputs(prefix, fo);
711 else
712 (void)fwrite(prefix, sizeof *prefix,
713 prefixlen, fo);
714 (void)fwrite(line, sizeof(*line), length, fo);
715 }
716 (void)fflush(fo);
717 }
718
719 PUBLIC int
720 mime_sendmessage(struct message *mp, FILE *obuf, struct ignoretab *igntab,
721 const char *prefix, struct mime_info *mip)
722 {
723 int error;
724 int detachall_flag;
725 const char *detachdir;
726 FILE *end_of_prefix;
727
728 if (mip == NULL)
729 return obuf ? /* were we trying to detach? */
730 sendmessage(mp, obuf, igntab, prefix, NULL) : 0;
731 /*
732 * The prefix has two meanigs which we handle here:
733 * 1) If obuf == NULL, then we are detaching to the 'prefix' director.
734 * 2) If obuf != NULL, then the prefix is prepended to each line.
735 */
736 detachdir = NULL;
737 detachall_flag = igntab == detachall;
738 if (obuf == NULL) {
739 assert(prefix != NULL); /* coding error! */
740 obuf = stdout;
741 detachdir = prefix;
742 prefix = NULL;
743 igntab = ignoreall; /* always ignore the headers */
744 }
745 /*
746 * Set this early so pipe_end() will work!
747 */
748 mip->mi_fo = obuf;
749
750 (void)fflush(obuf); /* Be safe and flush! XXX - necessary? */
751
752 /*
753 * Handle the prefix as a pipe stage so it doesn't get seen by
754 * any decoding or hooks.
755 */
756 if (prefix != NULL) {
757 static struct prefix_line_args_s prefix_line_args;
758 const char *dp, *dp2 = NULL;
759 for (dp = prefix; *dp; dp++)
760 if (*dp != ' ' && *dp != '\t')
761 dp2 = dp;
762 prefix_line_args.prefixlen = dp2 == 0 ? 0 : dp2 - prefix + 1;
763 prefix_line_args.prefix = prefix;
764 mime_run_function(prefix_line, pipe_end(mip), (void*)&prefix_line_args);
765 }
766
767 end_of_prefix = last_registered_file(0);
768 error = 0;
769 for (/* EMPTY */; mip; mip = mip->mi_flink) {
770 mip->mi_fo = obuf;
771 mip->mi_detachdir = detachdir;
772 mip->mi_detachall = detachall_flag;
773 error |= sendmessage(mip->mp, pipe_end(mip), igntab, NULL, mip);
774 close_top_files(end_of_prefix); /* don't close the prefixer! */
775 }
776 return error;
777 }
778
779
780 #ifdef CHARSET_SUPPORT
781 /**********************************************
782 * higher level interface to run mime_ficonv().
783 */
784 static void
785 run_mime_ficonv(struct mime_info *mip, const char *charset)
786 {
787 FILE *fo;
788 iconv_t cd;
789
790 fo = pipe_end(mip);
791
792 if (charset == NULL ||
793 mip->mi_charset == NULL ||
794 strcasecmp(mip->mi_charset, charset) == 0 ||
795 strcasecmp(mip->mi_charset, "unknown") == 0)
796 return;
797
798 cd = iconv_open(charset, mip->mi_charset);
799 if (cd == (iconv_t)-1) {
800 (void)fprintf(fo, "\t [ iconv_open failed: %s ]\n\n",
801 strerror(errno));
802 (void)fflush(fo); /* flush here or see double! */
803 return;
804 }
805
806 if (mip->mi_detachdir == NULL && /* don't contaminate the detach! */
807 value(ENAME_MIME_CHARSET_VERBOSE))
808 (void)fprintf(fo, "\t[ converting %s -> %s ]\n\n",
809 mip->mi_charset, charset);
810
811 mime_run_function(mime_ficonv, fo, cd);
812
813 (void)iconv_close(cd);
814 }
815 #endif /* CHARSET_SUPPORT */
816
817
818 PUBLIC void
819 run_decoder(struct mime_info *mip, void(*fn)(FILE*, FILE*, void *))
820 {
821 #ifdef CHARSET_SUPPORT
822 char *charset;
823
824 charset = value(ENAME_MIME_CHARSET);
825 if (charset && mip->mi_type && strcasecmp(mip->mi_type, "text") == 0)
826 run_mime_ficonv(mip, charset);
827 #endif /* CHARSET_SUPPORT */
828
829 if (mip->mi_detachdir == NULL &&
830 fn == mime_fio_copy)/* XXX - avoid an extra unnecessary pipe stage */
831 return;
832
833 mime_run_function(fn, pipe_end(mip),
834 mip->mi_detachdir ? NULL : __UNCONST("add_lf"));
835 }
836
837
838 /*
839 * Determine how to handle the display based on the type and subtype
840 * fields.
841 */
842 enum dispmode_e {
843 DM_IGNORE = 0x00, /* silently ignore part - must be zero! */
844 DM_DISPLAY, /* decode and display the part */
845 DM_UNKNOWN, /* unknown display */
846 DM_BINARY, /* indicate binary data */
847 DM_PGPSIGN, /* OpenPGP signed part */
848 DM_PGPENCR, /* OpenPGP encrypted part */
849 DM_PGPKEYS /* OpenPGP keys part */
850 };
851 #define APPLICATION_OCTET_STREAM DM_BINARY
852
853 static enum dispmode_e
854 get_display_mode(struct mime_info *mip, mime_codec_t dec)
855 {
856 struct mime_subtype_s {
857 const char *st_name;
858 enum dispmode_e st_dispmode;
859 };
860 struct mime_type_s {
861 const char *mt_type;
862 const struct mime_subtype_s *mt_subtype;
863 enum dispmode_e mt_dispmode; /* default if NULL subtype */
864 };
865 static const struct mime_subtype_s text_subtype_tbl[] = {
866 { "plain", DM_DISPLAY },
867 { "html", DM_DISPLAY }, /* rfc2854 */
868 { "rfc822-headers", DM_DISPLAY },
869 { "css", DM_DISPLAY }, /* rfc2318 */
870 { "enriched", DM_DISPLAY }, /* rfc1523/rfc1563/rfc1896 */
871 { "graphics", DM_DISPLAY }, /* rfc0553 */
872 { "nroff", DM_DISPLAY }, /* rfc4263 */
873 { "red", DM_DISPLAY }, /* rfc4102 */
874 { NULL, DM_DISPLAY } /* default */
875 };
876 static const struct mime_subtype_s image_subtype_tbl[] = {
877 { "tiff", DM_BINARY }, /* rfc2302/rfc3302 */
878 { "tiff-fx", DM_BINARY }, /* rfc3250/rfc3950 */
879 { "t38", DM_BINARY }, /* rfc3362 */
880 { NULL, DM_BINARY } /* default */
881 };
882 static const struct mime_subtype_s audio_subtype_tbl[] = {
883 { "mpeg", DM_BINARY }, /* rfc3003 */
884 { "t38", DM_BINARY }, /* rfc4612 */
885 { NULL, DM_BINARY } /* default */
886 };
887 static const struct mime_subtype_s video_subtype_tbl[] = {
888 { NULL, DM_BINARY } /* default */
889 };
890 static const struct mime_subtype_s application_subtype_tbl[] = {
891 { "octet-stream", APPLICATION_OCTET_STREAM },
892 { "pgp-encrypted", DM_PGPENCR }, /* rfc3156 */
893 { "pgp-keys", DM_PGPKEYS }, /* rfc3156 */
894 { "pgp-signature", DM_PGPSIGN }, /* rfc3156 */
895 { "pdf", DM_BINARY }, /* rfc3778 */
896 { "whoispp-query", DM_UNKNOWN }, /* rfc2957 */
897 { "whoispp-response", DM_UNKNOWN }, /* rfc2958 */
898 { "font-tdpfr", DM_UNKNOWN }, /* rfc3073 */
899 { "xhtml+xml", DM_UNKNOWN }, /* rfc3236 */
900 { "ogg", DM_UNKNOWN }, /* rfc3534 */
901 { "rdf+xml", DM_UNKNOWN }, /* rfc3870 */
902 { "soap+xml", DM_UNKNOWN }, /* rfc3902 */
903 { "mbox", DM_UNKNOWN }, /* rfc4155 */
904 { "xv+xml", DM_UNKNOWN }, /* rfc4374 */
905 { "smil", DM_UNKNOWN }, /* rfc4536 */
906 { "smil+xml", DM_UNKNOWN }, /* rfc4536 */
907 { "json", DM_UNKNOWN }, /* rfc4627 */
908 { "voicexml+xml", DM_UNKNOWN }, /* rfc4267 */
909 { "ssml+xml", DM_UNKNOWN }, /* rfc4267 */
910 { "srgs", DM_UNKNOWN }, /* rfc4267 */
911 { "srgs+xml", DM_UNKNOWN }, /* rfc4267 */
912 { "ccxml+xml", DM_UNKNOWN }, /* rfc4267 */
913 { "pls+xml.", DM_UNKNOWN }, /* rfc4267 */
914 { NULL, APPLICATION_OCTET_STREAM } /* default */
915 };
916 static const struct mime_type_s mime_type_tbl[] = {
917 { "text", text_subtype_tbl, DM_DISPLAY },
918 { "image", image_subtype_tbl, DM_IGNORE },
919 { "audio", audio_subtype_tbl, DM_IGNORE },
920 { "video", video_subtype_tbl, DM_IGNORE },
921 { "application", application_subtype_tbl, APPLICATION_OCTET_STREAM },
922 { "NULL", NULL, DM_UNKNOWN }, /* default */
923 };
924 const struct mime_type_s *mtp;
925 const struct mime_subtype_s *stp;
926 const char *mi_type;
927 const char *mi_subtype;
928
929 /*
930 * Silently ignore all multipart bodies.
931 * 1) In the case of "multipart" types, this typically
932 * contains a message for non-mime enabled mail readers.
933 * 2) In the case of "message" type, there should be no body.
934 */
935 if (mip->mi_ignore_body) /*is_multipart(mip) || is_message(mip))*/
936 return DM_IGNORE;
937
938 /*
939 * If the encoding type given but not recognized, treat block
940 * as "application/octet-stream". rfc 2049 sec 2 part 2.
941 */
942 if (mip->mi_encoding && dec == NULL)
943 return APPLICATION_OCTET_STREAM;
944
945 mi_type = mip->mi_type;
946 mi_subtype = mip->mi_type ? mip->mi_subtype : NULL;
947
948 /*
949 * If there was no type specified, display anyway so we don't
950 * miss anything. (The encoding type is known.)
951 */
952 if (mi_type == NULL)
953 return DM_DISPLAY; /* XXX - default to something safe! */
954
955 for (mtp = mime_type_tbl; mtp->mt_type; mtp++) {
956 if (strcasecmp(mtp->mt_type, mi_type) == 0) {
957 if (mi_subtype == NULL)
958 return mtp->mt_dispmode;
959 for (stp = mtp->mt_subtype; stp->st_name; stp++) {
960 if (strcasecmp(stp->st_name, mi_subtype) == 0)
961 return stp->st_dispmode;
962 }
963 return stp->st_dispmode;
964 }
965 }
966 return mtp->mt_dispmode;
967 }
968
969
970 PUBLIC FILE *
971 mime_decode_body(struct mime_info *mip)
972 {
973 static enum dispmode_e dispmode;
974 mime_codec_t dec;
975 const char *cmd;
976
977 /* close anything left over from mime_decode_head() */
978 close_top_files(mip->mi_head_end);
979
980 /*
981 * Make sure we flush everything down the pipe so children
982 * don't see it.
983 */
984 (void)fflush(pipe_end(mip));
985
986 if (mip->mi_detachdir) /* We are detaching! Ignore the hooks. */
987 return mime_detach_parts(mip);
988
989 cmd = NULL;
990 if (mip->mi_command_hook == NULL)
991 cmd = get_command_hook(mip, "-body");
992
993 dec = mime_fio_decoder(mip->mi_encoding);
994
995 /*
996 * If there is a filter running, we need to send the message
997 * to it. Otherwise, get the default display mode for this body.
998 */
999 dispmode = cmd || mip->mi_command_hook ? DM_DISPLAY : get_display_mode(mip, dec);
1000
1001 if (dec == NULL) /* make sure we have a usable decoder */
1002 dec = mime_fio_decoder(MIME_TRANSFER_7BIT);
1003
1004 if (dispmode == DM_DISPLAY) {
1005 int flags;
1006 if (cmd == NULL)
1007 /* just get the flags */
1008 flags = mime_run_command(mip->mi_command_hook, NULL);
1009 else
1010 flags = mime_run_command(cmd, pipe_end(mip));
1011 if ((flags & CMD_FLAG_NO_DECODE) == 0)
1012 run_decoder(mip, dec);
1013 return pipe_end(mip);
1014 }
1015 else {
1016 static const struct msg_tbl_s {
1017 enum dispmode_e dm;
1018 const char *msg;
1019 } msg_tbl[] = {
1020 { DM_BINARY, "binary content" },
1021 { DM_PGPSIGN, "OpenPGP signature" },
1022 { DM_PGPENCR, "OpenPGP encrypted" },
1023 { DM_PGPKEYS, "OpenPGP keys" },
1024 { DM_UNKNOWN, "unknown data" },
1025 { DM_IGNORE, NULL },
1026 { -1, NULL },
1027 };
1028 const struct msg_tbl_s *mp;
1029
1030 for (mp = msg_tbl; mp->dm != -1; mp++)
1031 if (mp->dm == dispmode)
1032 break;
1033
1034 assert(mp->dm != -1); /* msg_tbl is short if this happens! */
1035
1036 if (mp->msg)
1037 (void)fprintf(pipe_end(mip), " [%s]\n\n", mp->msg);
1038
1039 return NULL;
1040 }
1041 }
1042
1043
1044
1045 /************************************************************************
1046 * Higher level header decoding interface.
1047 *
1048 * The core routines are in mime_header.c.
1049 */
1050
1051 PUBLIC char *
1052 mime_decode_hfield(char *linebuf, size_t bufsize, char *hdrstr)
1053 {
1054 hfield_decoder_t decode;
1055 decode = mime_hfield_decoder(hdrstr);
1056 if (decode) {
1057 decode(linebuf, bufsize, hdrstr);
1058 return linebuf;
1059 }
1060 return hdrstr;
1061 }
1062
1063 /*
1064 * Return the next header field found in the given message.
1065 * Return >= 0 if something found, < 0 elsewise.
1066 * "colon" is set to point to the colon in the header.
1067 */
1068 static int
1069 get_folded_hfield(FILE *f, char *linebuf, size_t bufsize, int rem, char **colon)
1070 {
1071 char *cp, *cp2;
1072 char *line;
1073 size_t len;
1074
1075 for (;;) {
1076 if (--rem <= 0)
1077 return -1;
1078 if ((cp = fgetln(f, &len)) == NULL)
1079 return -1;
1080 for (cp2 = cp; isprint((unsigned char)*cp2) &&
1081 !isblank((unsigned char)*cp2) && *cp2 != ':'; cp2++)
1082 continue;
1083 len = MIN(bufsize - 1, len);
1084 bufsize -= len;
1085 (void)memcpy(linebuf, cp, len);
1086 *colon = *cp2 == ':' ? linebuf + (cp2 - cp) : NULL;
1087 line = linebuf + len;
1088 for (/*EMPTY*/; rem > 0; rem--) {
1089 int c;
1090 (void)ungetc(c = getc(f), f);
1091 if (c == EOF || !isblank((unsigned char)c))
1092 break;
1093
1094 if ((cp = fgetln(f, &len)) == NULL)
1095 break;
1096 len = MIN(bufsize - 1, len);
1097 bufsize -= len;
1098 if (len == 0)
1099 break;
1100 (void)memcpy(line, cp, len);
1101 line += len;
1102 }
1103 *line = 0;
1104 return rem;
1105 /* NOTREACHED */
1106 }
1107 }
1108
1109 static void
1110 decode_header(FILE *fi, FILE *fo, void *cookie __unused)
1111 {
1112 char linebuf[LINESIZE];
1113 char *colon;
1114 #ifdef __lint__
1115 cookie = cookie;
1116 #endif
1117 while(get_folded_hfield(fi, linebuf, sizeof(linebuf), INT_MAX, &colon) >= 0) {
1118 char decbuf[LINESIZE];
1119 char *hdrstr;
1120 hdrstr = linebuf;
1121 if (colon)
1122 hdrstr = mime_decode_hfield(decbuf, sizeof(decbuf), hdrstr);
1123 (void)fprintf(fo, hdrstr);
1124 }
1125 }
1126
1127
1128 PUBLIC FILE *
1129 mime_decode_header(struct mime_info *mip)
1130 {
1131 int flags;
1132 const char *cmd;
1133 FILE *fo;
1134
1135 fo = pipe_end(mip);
1136
1137 if (mip->mi_detachdir) { /* We are detaching. Don't run anything! */
1138 (void)fflush(fo);
1139 return pipe_end(mip);
1140 }
1141
1142 if (mip->mi_partnum)
1143 (void)fprintf(fo, "----- Part %s -----\n", mip->mi_partstr);
1144
1145 (void)fflush(fo); /* Flush so the childern don't see it. */
1146
1147 /*
1148 * install the message hook before the head hook.
1149 */
1150 cmd = get_command_hook(mip, "-hook");
1151 mip->mi_command_hook = cmd;
1152 if (cmd) {
1153 flags = mime_run_command(cmd, pipe_end(mip));
1154 mip->mi_head_end = last_registered_file(0);
1155 }
1156 else {
1157 cmd = get_command_hook(mip, "-head");
1158 mip->mi_head_end = last_registered_file(0);
1159 flags = mime_run_command(cmd, pipe_end(mip));
1160 }
1161
1162 if (value(ENAME_MIME_DECODE_HDR) && (flags & CMD_FLAG_NO_DECODE) == 0)
1163 mime_run_function(decode_header, pipe_end(mip), NULL);
1164
1165 return pipe_end(mip);
1166 }
1167
1168 #endif /* MIME_SUPPORT */
1169