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