xref: /netbsd-src/usr.bin/mail/mime_decode.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: mime_decode.c,v 1.11 2007/10/29 23:20:38 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.11 2007/10/29 23:20:38 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: %s\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(0);	/* get last registered file or 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 && is_WSP(cp2[-1]))
192 		cp2--;
193 	*cp2 = '\0';
194 	if (*cp == ';')
195 		cp++;
196 	cp = skip_WSP(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_WSP(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), blkoffsetof(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' directory.
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 		if ((obuf = last_registered_file(0)) == NULL)
741 			obuf = stdout;
742 		detachdir = prefix;
743 		prefix = NULL;
744 		igntab = ignoreall;	/* always ignore the headers */
745 	}
746 	/*
747 	 * Set this early so pipe_end() will work!
748 	 */
749 	mip->mi_fo = obuf;
750 
751 	(void)fflush(obuf);  /* Be safe and flush!  XXX - necessary? */
752 
753 	/*
754 	 * Handle the prefix as a pipe stage so it doesn't get seen by
755 	 * any decoding or hooks.
756 	 */
757 	if (prefix != NULL) {
758 		static struct prefix_line_args_s prefix_line_args;
759 		const char *dp, *dp2 = NULL;
760 		for (dp = prefix; *dp; dp++)
761 			if (!is_WSP(*dp))
762 				dp2 = dp;
763 		prefix_line_args.prefixlen = dp2 == 0 ? 0 : dp2 - prefix + 1;
764 		prefix_line_args.prefix = prefix;
765 		mime_run_function(prefix_line, pipe_end(mip), (void*)&prefix_line_args);
766 	}
767 
768 	end_of_prefix = last_registered_file(0);
769 	error = 0;
770 	for (/*EMPTY*/; mip; mip = mip->mi_flink) {
771 		mip->mi_fo = obuf;
772 		mip->mi_head_end = obuf;
773 		mip->mi_detachdir = detachdir;
774 		mip->mi_detachall = detachall_flag;
775 		error |= sendmessage(mip->mp, pipe_end(mip), igntab, NULL, mip);
776 		close_top_files(end_of_prefix);	/* don't close the prefixer! */
777 	}
778 	return error;
779 }
780 
781 
782 #ifdef CHARSET_SUPPORT
783 /**********************************************
784  * higher level interface to run mime_ficonv().
785  */
786 static void
787 run_mime_ficonv(struct mime_info *mip, const char *charset)
788 {
789 	FILE *fo;
790 	iconv_t cd;
791 
792 	fo = pipe_end(mip);
793 
794 	if (charset == NULL ||
795 	    mip->mi_charset == NULL ||
796 	    strcasecmp(mip->mi_charset, charset) == 0 ||
797 	    strcasecmp(mip->mi_charset, "unknown") == 0)
798 		return;
799 
800 	cd = iconv_open(charset, mip->mi_charset);
801 	if (cd == (iconv_t)-1) {
802 		(void)fprintf(fo, "\t [ iconv_open failed: %s ]\n\n",
803 		    strerror(errno));
804 		(void)fflush(fo);	/* flush here or see double! */
805 		return;
806 	}
807 
808 	if (mip->mi_detachdir == NULL && /* don't contaminate the detach! */
809 	    value(ENAME_MIME_CHARSET_VERBOSE))
810 		(void)fprintf(fo, "\t[ converting %s -> %s ]\n\n",
811 		    mip->mi_charset, charset);
812 
813 	mime_run_function(mime_ficonv, fo, cd);
814 
815 	(void)iconv_close(cd);
816 }
817 #endif /* CHARSET_SUPPORT */
818 
819 
820 PUBLIC void
821 run_decoder(struct mime_info *mip, void(*fn)(FILE*, FILE*, void *))
822 {
823 #ifdef CHARSET_SUPPORT
824 	char *charset;
825 
826 	charset = value(ENAME_MIME_CHARSET);
827 	if (charset && mip->mi_type && strcasecmp(mip->mi_type, "text") == 0)
828 		run_mime_ficonv(mip, charset);
829 #endif /* CHARSET_SUPPORT */
830 
831 	if (mip->mi_detachdir == NULL &&
832 	    fn == mime_fio_copy)/* XXX - avoid an extra unnecessary pipe stage */
833 		return;
834 
835 	mime_run_function(fn, pipe_end(mip),
836 	    mip->mi_detachdir ? NULL : __UNCONST("add_lf"));
837 }
838 
839 
840 /*
841  * Determine how to handle the display based on the type and subtype
842  * fields.
843  */
844 enum dispmode_e {
845 	DM_IGNORE	= 0x00,	/* silently ignore part - must be zero! */
846 	DM_DISPLAY,		/* decode and display the part */
847 	DM_UNKNOWN,		/* unknown display */
848 	DM_BINARY,		/* indicate binary data */
849 	DM_PGPSIGN,		/* OpenPGP signed part */
850 	DM_PGPENCR,		/* OpenPGP encrypted part */
851 	DM_PGPKEYS		/* OpenPGP keys part */
852 };
853 #define APPLICATION_OCTET_STREAM	DM_BINARY
854 
855 static enum dispmode_e
856 get_display_mode(struct mime_info *mip, mime_codec_t dec)
857 {
858 	struct mime_subtype_s {
859 		const char *st_name;
860 		enum dispmode_e st_dispmode;
861 	};
862 	struct mime_type_s {
863 		const char *mt_type;
864 		const struct mime_subtype_s *mt_subtype;
865 		enum dispmode_e mt_dispmode;	/* default if NULL subtype */
866 	};
867 	static const struct mime_subtype_s text_subtype_tbl[] = {
868 		{ "plain",		DM_DISPLAY },
869 		{ "html", 		DM_DISPLAY },	/* rfc2854 */
870 		{ "rfc822-headers",	DM_DISPLAY },
871 		{ "css",		DM_DISPLAY },	/* rfc2318 */
872 		{ "enriched",		DM_DISPLAY },	/* rfc1523/rfc1563/rfc1896 */
873 		{ "graphics",		DM_DISPLAY },	/* rfc0553 */
874 		{ "nroff",		DM_DISPLAY },	/* rfc4263 */
875 		{ "red",		DM_DISPLAY },	/* rfc4102 */
876 		{ NULL,			DM_DISPLAY }	/* default */
877 	};
878 	static const struct mime_subtype_s image_subtype_tbl[] = {
879 		{ "tiff",		DM_BINARY },	/* rfc2302/rfc3302 */
880 		{ "tiff-fx",		DM_BINARY },	/* rfc3250/rfc3950 */
881 		{ "t38",		DM_BINARY },	/* rfc3362 */
882 		{ NULL,			DM_BINARY }	/* default */
883 	};
884 	static const struct mime_subtype_s audio_subtype_tbl[] = {
885 		{ "mpeg",		DM_BINARY },	/* rfc3003 */
886 		{ "t38",		DM_BINARY },	/* rfc4612 */
887 		{ NULL,			DM_BINARY }	/* default */
888 	};
889 	static const struct mime_subtype_s video_subtype_tbl[] = {
890 		{ NULL,			DM_BINARY }	/* default */
891 	};
892 	static const struct mime_subtype_s application_subtype_tbl[] = {
893 		{ "octet-stream",	APPLICATION_OCTET_STREAM },
894 		{ "pgp-encrypted",      DM_PGPENCR },   /* rfc3156 */
895 		{ "pgp-keys",           DM_PGPKEYS },   /* rfc3156 */
896 		{ "pgp-signature",      DM_PGPSIGN },   /* rfc3156 */
897 		{ "pdf",		DM_BINARY },	/* rfc3778 */
898 		{ "whoispp-query",	DM_UNKNOWN },	/* rfc2957 */
899 		{ "whoispp-response",	DM_UNKNOWN },	/* rfc2958 */
900 		{ "font-tdpfr",		DM_UNKNOWN },	/* rfc3073 */
901 		{ "xhtml+xml",		DM_UNKNOWN },	/* rfc3236 */
902 		{ "ogg",		DM_UNKNOWN },	/* rfc3534 */
903 		{ "rdf+xml",		DM_UNKNOWN },	/* rfc3870 */
904 		{ "soap+xml",		DM_UNKNOWN },	/* rfc3902 */
905 		{ "mbox",		DM_UNKNOWN },	/* rfc4155 */
906 		{ "xv+xml",		DM_UNKNOWN },	/* rfc4374 */
907 		{ "smil",		DM_UNKNOWN },	/* rfc4536 */
908 		{ "smil+xml",		DM_UNKNOWN },	/* rfc4536 */
909 		{ "json",		DM_UNKNOWN },	/* rfc4627 */
910 		{ "voicexml+xml",	DM_UNKNOWN },	/* rfc4267 */
911 		{ "ssml+xml",		DM_UNKNOWN },	/* rfc4267 */
912 		{ "srgs",		DM_UNKNOWN },	/* rfc4267 */
913 		{ "srgs+xml",		DM_UNKNOWN },	/* rfc4267 */
914 		{ "ccxml+xml",		DM_UNKNOWN },	/* rfc4267 */
915 		{ "pls+xml.",		DM_UNKNOWN },	/* rfc4267 */
916 		{ NULL,			APPLICATION_OCTET_STREAM } /* default */
917 	};
918 	static const struct mime_type_s mime_type_tbl[] = {
919 		{ "text",	 text_subtype_tbl,		DM_DISPLAY },
920 		{ "image",	 image_subtype_tbl,		DM_IGNORE },
921 		{ "audio",	 audio_subtype_tbl,		DM_IGNORE },
922 		{ "video",	 video_subtype_tbl,		DM_IGNORE },
923 		{ "application", application_subtype_tbl,	APPLICATION_OCTET_STREAM },
924 		{ NULL,		 NULL,				DM_UNKNOWN }, /* default */
925 	};
926 	const struct mime_type_s *mtp;
927 	const struct mime_subtype_s *stp;
928 	const char *mi_type;
929 	const char *mi_subtype;
930 
931 	/*
932 	 * Silently ignore all multipart bodies.
933 	 * 1) In the case of "multipart" types, this typically
934 	 *    contains a message for non-mime enabled mail readers.
935 	 * 2) In the case of "message" type, there should be no body.
936 	 */
937 	if (mip->mi_ignore_body)	/*is_multipart(mip) || is_message(mip))*/
938 		return DM_IGNORE;
939 
940 	/*
941 	 * If the encoding type given but not recognized, treat block
942 	 * as "application/octet-stream".  rfc 2049 sec 2 part 2.
943 	 */
944 	if (mip->mi_encoding && dec == NULL)
945 		return APPLICATION_OCTET_STREAM;
946 
947 	mi_type    = mip->mi_type;
948 	mi_subtype = mip->mi_type ? mip->mi_subtype : NULL;
949 
950 	/*
951 	 * If there was no type specified, display anyway so we don't
952 	 * miss anything.  (The encoding type is known.)
953 	 */
954 	if (mi_type == NULL)
955 		return DM_DISPLAY;	/* XXX - default to something safe! */
956 
957 	for (mtp = mime_type_tbl; mtp->mt_type; mtp++) {
958 		if (strcasecmp(mtp->mt_type, mi_type) == 0) {
959 			if (mi_subtype == NULL)
960 				return mtp->mt_dispmode;
961 			for (stp = mtp->mt_subtype; stp->st_name; stp++) {
962 				if (strcasecmp(stp->st_name, mi_subtype) == 0)
963 					return stp->st_dispmode;
964 			}
965 			return stp->st_dispmode;
966 		}
967 	}
968 	return mtp->mt_dispmode;
969 }
970 
971 
972 PUBLIC FILE *
973 mime_decode_body(struct mime_info *mip)
974 {
975 	static enum dispmode_e dispmode;
976 	mime_codec_t dec;
977 	const char *cmd;
978 
979 	/* close anything left over from mime_decode_head() */
980 	close_top_files(mip->mi_head_end);
981 
982 	/*
983 	 * Make sure we flush everything down the pipe so children
984 	 * don't see it.
985 	 */
986 	(void)fflush(pipe_end(mip));
987 
988 	if (mip->mi_detachdir)	/* We are detaching!  Ignore the hooks. */
989 		return mime_detach_parts(mip);
990 
991 	cmd = NULL;
992 	if (mip->mi_command_hook == NULL)
993 		cmd = get_command_hook(mip, "-body");
994 
995 	dec = mime_fio_decoder(mip->mi_encoding);
996 
997 	/*
998 	 * If there is a filter running, we need to send the message
999 	 * to it.  Otherwise, get the default display mode for this body.
1000 	 */
1001 	dispmode = cmd || mip->mi_command_hook ? DM_DISPLAY : get_display_mode(mip, dec);
1002 
1003 	if (dec == NULL)	/* make sure we have a usable decoder */
1004 		dec = mime_fio_decoder(MIME_TRANSFER_7BIT);
1005 
1006 	if (dispmode == DM_DISPLAY) {
1007 		int flags;
1008 		if (cmd == NULL)
1009 			/* just get the flags */
1010 			flags = mime_run_command(mip->mi_command_hook, NULL);
1011 		else
1012 			flags = mime_run_command(cmd, pipe_end(mip));
1013 		if ((flags & CMD_FLAG_NO_DECODE) == 0)
1014 			run_decoder(mip, dec);
1015 		return pipe_end(mip);
1016 	}
1017 	else {
1018 		static const struct msg_tbl_s {
1019 			enum dispmode_e dm;
1020 			const char *msg;
1021 		} msg_tbl[] = {
1022 			{ DM_BINARY,	"binary content"	},
1023 			{ DM_PGPSIGN,	"OpenPGP signature"	},
1024 			{ DM_PGPENCR,	"OpenPGP encrypted"	},
1025 			{ DM_PGPKEYS,	"OpenPGP keys"		},
1026 			{ DM_UNKNOWN,	"unknown data"		},
1027 			{ DM_IGNORE,	NULL			},
1028 			{ -1,		NULL			},
1029 		};
1030 		const struct msg_tbl_s *mp;
1031 
1032 		for (mp = msg_tbl; mp->dm != -1; mp++)
1033 			if (mp->dm == dispmode)
1034 				break;
1035 
1036 		assert(mp->dm != -1);	/* msg_tbl is short if this happens! */
1037 
1038 		if (mp->msg)
1039 			(void)fprintf(pipe_end(mip), "  [%s]\n\n", mp->msg);
1040 
1041 		return NULL;
1042 	}
1043 }
1044 
1045 
1046 
1047 /************************************************************************
1048  * Higher level header decoding interface.
1049  *
1050  * The core routines are in mime_header.c.
1051  */
1052 
1053 PUBLIC char *
1054 mime_decode_hfield(char *linebuf, size_t bufsize, char *hdrstr)
1055 {
1056 	hfield_decoder_t decode;
1057 	decode = mime_hfield_decoder(hdrstr);
1058 	if (decode) {
1059 		decode(linebuf, bufsize, hdrstr);
1060 		return linebuf;
1061 	}
1062 	return hdrstr;
1063 }
1064 
1065 /*
1066  * Return the next header field found in the given message.
1067  * Return >= 0 if something found, < 0 elsewise.
1068  * "colon" is set to point to the colon in the header.
1069  */
1070 static int
1071 get_folded_hfield(FILE *f, char *linebuf, size_t bufsize, int rem, char **colon)
1072 {
1073 	char *cp, *cp2;
1074 	char *line;
1075 	size_t len;
1076 
1077 	for (;;) {
1078 		if (--rem <= 0)
1079 			return -1;
1080 		if ((cp = fgetln(f, &len)) == NULL)
1081 			return -1;
1082 		for (cp2 = cp;
1083 		     isprint((unsigned char)*cp2) &&
1084 			 !is_WSP(*cp2) && *cp2 != ':';
1085 		     cp2++)
1086 			continue;
1087 		len = MIN(bufsize - 1, len);
1088 		bufsize -= len;
1089 		(void)memcpy(linebuf, cp, len);
1090 		*colon = *cp2 == ':' ? linebuf + (cp2 - cp) : NULL;
1091 		line = linebuf + len;
1092 		for (/*EMPTY*/; rem > 0; rem--) {
1093 			int c;
1094 			(void)ungetc(c = getc(f), f);
1095 			if (!is_WSP(c))
1096 				break;
1097 
1098 			if ((cp = fgetln(f, &len)) == NULL)
1099 				break;
1100 			len = MIN(bufsize - 1, len);
1101 			bufsize -= len;
1102 			if (len == 0)
1103 			    break;
1104 			(void)memcpy(line, cp, len);
1105 			line += len;
1106 		}
1107 		*line = 0;
1108 		return rem;
1109 		/* NOTREACHED */
1110 	}
1111 }
1112 
1113 static void
1114 decode_header(FILE *fi, FILE *fo, void *cookie __unused)
1115 {
1116 	char linebuf[LINESIZE];
1117 	char *colon;
1118 #ifdef __lint__
1119 	cookie = cookie;
1120 #endif
1121 	while(get_folded_hfield(fi, linebuf, sizeof(linebuf), INT_MAX, &colon) >= 0) {
1122 		char decbuf[LINESIZE];
1123 		char *hdrstr;
1124 		hdrstr = linebuf;
1125 		if (colon)
1126 			hdrstr = mime_decode_hfield(decbuf, sizeof(decbuf), hdrstr);
1127 		(void)fprintf(fo, hdrstr);
1128 	}
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