xref: /netbsd/usr.bin/mail/mime_decode.c (revision 02bc8589)
1 /*	$NetBSD: mime_decode.c,v 1.17 2010/06/21 19:49:31 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.17 2010/06/21 19:49:31 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 *
pipe_end(struct mime_info * mip)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 *
get_param(char * dst,char * src)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*
cparam(const char field[],char * src,int downcase)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
get_content(struct mime_info * mip)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 	 * XXX: If we have a "Content-Type" in the header, then assume
272 	 * we also have a "MIME-Version: 1.0".  This fixes some broken
273 	 * MIME headers that I have seen occasionally.
274 	 */
275 	if (mip->mi_version == NULL && mip->mi_type != NULL)
276 		mip->mi_version = MIME_VERSION;
277 }
278 
279 static struct message *
salloc_message(int flag,long block,short offset)280 salloc_message(int flag, long block, short offset)
281 {
282 	struct message *mp;
283 	/* use csalloc in case someone adds a field someday! */
284 	mp = csalloc(1, sizeof(*mp));
285 	mp->m_flag   = flag;
286 	mp->m_block  = block;
287 	mp->m_offset = offset;
288 #if 0
289 	mp->m_lines  = 0;
290 	mp->m_size   = 0;
291 	mp->m_blines = 0;
292 #endif
293 	return mp;
294 }
295 
296 static struct mime_info *
insert_new_mip(struct mime_info * this_mip,struct mime_info * top_mip,struct message * top_mp,off_t end_pos,int partnum)297 insert_new_mip(struct mime_info *this_mip, struct mime_info *top_mip,
298     struct message *top_mp, off_t end_pos, int partnum)
299 {
300 	struct mime_info *new_mip;
301 
302 	new_mip = csalloc(1, sizeof(*new_mip));
303 	new_mip->mi_blink = this_mip;
304 	new_mip->mi_flink = this_mip->mi_flink;
305 	this_mip->mi_flink = new_mip;
306 
307 	new_mip->mp = salloc_message(this_mip->mp->m_flag,
308 	    (long)blockof(end_pos), blkoffsetof(end_pos));
309 
310 	new_mip->mi_parent.mip = top_mip;
311 	new_mip->mi_parent.mp = top_mp;
312 	new_mip->mi_partnum = partnum;
313 
314 	return new_mip;
315 }
316 
317 static void
split_multipart(struct mime_info * top_mip)318 split_multipart(struct mime_info *top_mip)
319 {
320 	FILE *fp;
321 	struct message *top_mp;
322 	struct message *this_mp;
323 	struct mime_info *this_mip;
324 	off_t beg_pos;
325 	const char *boundary;
326 	size_t boundary_len;
327 	long lines_left;	/* must be signed and same size as m_lines */
328 	int partnum;
329 	int in_header;
330 
331 	top_mp = top_mip->mp;
332 	this_mp = salloc_message(top_mp->m_flag, top_mp->m_block, top_mp->m_offset);
333 	this_mip = top_mip;
334 	this_mip->mp = this_mp;
335 
336 	partnum = 1;
337 /*	top_mip->mi_partnum = partnum++;  */ /* Keep the number set by the caller */
338 	in_header = 1;
339 	boundary = top_mip->mi_boundary;
340 	boundary_len = boundary ? strlen(boundary) : 0;
341 
342 	fp = setinput(top_mp);
343 	beg_pos = ftello(fp);
344 #if 0
345 	warnx("beg_pos: %lld,  m_lines: %ld,  m_blines: %ld",
346 	    beg_pos, top_mp->m_lines, top_mp->m_blines);
347 #endif
348 	for (lines_left = top_mp->m_lines - 1; lines_left >= 0; lines_left--) {
349 		char *line;
350 		size_t line_len;
351 
352 		line = fgetln(fp, &line_len);
353 
354 		this_mp->m_lines++;		/* count the message lines */
355 
356 		if (!in_header)
357 			this_mp->m_blines++;	/* count the body lines */
358 
359 		if (lines_left == 0 || (
360 			    !in_header &&
361 			    line_len >= boundary_len + 2 &&
362 			    line[0] == '-' && line[1] == '-' &&
363 			    strncmp(line + 2, boundary, boundary_len) == 0)) {
364 			off_t cur_pos;
365 			off_t end_pos;
366 
367 			cur_pos = ftello(fp);
368 
369 			/* the boundary belongs to the next part */
370 			end_pos = cur_pos - line_len;
371 			this_mp->m_lines  -= 1;
372 			this_mp->m_blines -= 1;
373 
374 			this_mp->m_size = end_pos - beg_pos;
375 #if 0
376 			warnx("end_pos: %lld,  m_lines: %ld,  m_blines: %ld",
377 			    end_pos, this_mp->m_lines, this_mp->m_blines);
378 #endif
379 			if (line[boundary_len + 2] == '-' &&
380 			    line[boundary_len + 3] == '-') {/* end of multipart */
381 				/* do a sanity check on the EOM */
382 				if (lines_left != 1) {
383 					/*
384 					 * XXX - this can happen!
385 					 * Should we display the
386 					 * trailing garbage or check
387 					 * that it is blank or just
388 					 * ignore it?
389 					 */
390 #if 0
391 					(void)printf("EOM: lines left: %ld\n", lines_left);
392 #endif
393 				}
394 				break;	/* XXX - stop at this point or grab the rest? */
395 			}
396 			this_mip = insert_new_mip(this_mip, top_mip, top_mp, end_pos, partnum++);
397 			this_mp = this_mip->mp;
398 			this_mp->m_lines = 1; /* already read the first line in the header! */
399 			beg_pos = end_pos;
400 			in_header = 1;
401 		}
402 
403 		if (line_len == 1)
404 			in_header = 0;
405 	}
406 }
407 
408 static void
split_message(struct mime_info * top_mip)409 split_message(struct mime_info *top_mip)
410 {
411 	struct mime_info *this_mip;
412 	struct message *top_mp;
413 	struct message *this_mp;
414 	FILE *fp;
415 	off_t beg_pos;
416 	long lines_left;	/* must be same size as m_lines */
417 	int in_header;
418 
419 	top_mp = top_mip->mp;
420 	this_mp = salloc_message(top_mp->m_flag, top_mp->m_block, top_mp->m_offset);
421 	this_mip = top_mip;
422 	this_mip->mp = this_mp;
423 
424 	in_header = 1;
425 
426 	fp = setinput(top_mp);
427 	beg_pos = ftello(fp);
428 
429 	for (lines_left = top_mp->m_lines; lines_left > 0; lines_left--) {
430 		size_t line_len;
431 
432 		(void)fgetln(fp, &line_len);
433 
434 		this_mp->m_lines++;		/* count the message lines */
435 		if (!in_header)
436 			this_mp->m_blines++;	/* count the body lines */
437 
438 		if (in_header && line_len == 1) { /* end of header */
439 			off_t end_pos;
440 			end_pos = ftello(fp);
441 			this_mp->m_size = end_pos - beg_pos;
442 			this_mip = insert_new_mip(this_mip, top_mip,top_mp, end_pos, 0);
443 			this_mp = this_mip->mp;
444 			this_mp->m_lines = 1; /* we already counted one line in the header! */
445 			beg_pos = end_pos;
446 			in_header = 0;	/* never in header again */
447 		}
448 	}
449 
450 	/* close the last message */
451 	this_mp->m_size = ftello(fp) - beg_pos;
452 }
453 
454 
455 static const char *
get_command_hook(struct mime_info * mip,const char * domain)456 get_command_hook(struct mime_info *mip, const char *domain)
457 {
458 	char *key;
459 	char *cmd;
460 
461 	if (mip->mi_type == NULL)
462 		return NULL;
463 
464 	/* XXX - should we use easprintf() here?  We are probably
465 	 * hosed elsewhere if this fails anyway. */
466 
467 	cmd = NULL;
468 	if (mip->mi_subtype) {
469 		if (asprintf(&key, "mime%s-%s-%s",
470 			domain,	mip->mi_type, mip->mi_subtype) == -1) {
471 			warn("get_command_hook: subtupe: asprintf");
472 			return NULL;
473 		}
474 		cmd = value(key);
475 		free(key);
476 	}
477 	if (cmd == NULL) {
478 		if (asprintf(&key, "mime%s-%s", domain, mip->mi_type) == -1) {
479 			warn("get_command_hook: type: asprintf");
480 			return NULL;
481 		}
482 		cmd = value(key);
483 		free(key);
484 	}
485 	return cmd;
486 }
487 
488 
489 static int
is_basic_alternative(struct mime_info * mip)490 is_basic_alternative(struct mime_info *mip)
491 {
492 	return
493 	    strcasecmp(mip->mi_type, "text") == 0 &&
494 	    strcasecmp(mip->mi_subtype, "plain") == 0;
495 }
496 
497 static struct mime_info *
select_alternative(struct mime_info * top_mip,struct mime_info * end_mip)498 select_alternative(struct mime_info *top_mip, struct mime_info *end_mip)
499 {
500 	struct mime_info *the_mip;	/* the chosen alternate */
501 	struct mime_info *this_mip;
502 	/*
503 	 * The alternates are supposed to occur in order of
504 	 * increasing "complexity".  So: if there is at least
505 	 * one alternate of type "text/plain", use the last
506 	 * one, otherwise default to the first alternate.
507 	 */
508 	the_mip = top_mip->mi_flink;
509 	for (this_mip = top_mip->mi_flink;
510 	     this_mip != end_mip;
511 	     this_mip = this_mip->mi_flink) {
512 		const char *cmd;
513 
514 		if (this_mip->mi_type == NULL ||
515 		    this_mip->mi_subtype == NULL)
516 			continue;
517 
518 		if (is_basic_alternative(this_mip))
519 			the_mip = this_mip;
520 		else if (
521 			(cmd = get_command_hook(this_mip, "-hook")) ||
522 			(cmd = get_command_hook(this_mip, "-head")) ||
523 			(cmd = get_command_hook(this_mip, "-body"))) {
524 			int flags;
525 			/* just get the flags. */
526 			flags = mime_run_command(cmd, NULL);
527 			if ((flags & CMD_FLAG_ALTERNATIVE) != 0)
528 				the_mip = this_mip;
529 		}
530 	}
531 	return the_mip;
532 }
533 
534 
535 static inline int
is_multipart(struct mime_info * mip)536 is_multipart(struct mime_info *mip)
537 {
538 	return mip->mi_type &&
539 	    strcasecmp("multipart", mip->mi_type) == 0;
540 }
541 static inline int
is_message(struct mime_info * mip)542 is_message(struct mime_info *mip)
543 {
544 	return mip->mi_type &&
545 	    strcasecmp("message", mip->mi_type) == 0;
546 }
547 
548 static inline int
is_alternative(struct mime_info * mip)549 is_alternative(struct mime_info *mip)
550 {
551 	return mip->mi_subtype &&
552 	    strcasecmp("alternative", mip->mi_subtype) == 0;
553 }
554 
555 
556 /*
557  * Take a mime_info pointer and expand it recursively into all its
558  * mime parts.  Only "multipart" and "message" types recursed into;
559  * they are handled separately.
560  */
561 static struct mime_info *
expand_mip(struct mime_info * top_mip)562 expand_mip(struct mime_info *top_mip)
563 {
564 	struct mime_info *this_mip;
565 	struct mime_info *next_mip;
566 
567 	if (top_mip->mi_partnum == 0) {
568 		if (top_mip->mi_blink)
569 			top_mip->mi_partstr = top_mip->mi_blink->mi_partstr;
570 	}
571 	else if (top_mip->mi_parent.mip) {
572 		const char *prefix;
573 		char *cp;
574 		prefix = top_mip->mi_parent.mip->mi_partstr;
575 		(void)sasprintf(&cp, "%s%s%d", prefix,
576 		    *prefix ? "." : "", top_mip->mi_partnum);
577 		top_mip->mi_partstr = cp;
578 	}
579 
580 	next_mip = top_mip->mi_flink;
581 
582 	if (is_multipart(top_mip)) {
583 		top_mip->mi_ignore_body = 1; /* the first body is ignored */
584 		split_multipart(top_mip);
585 
586 		for (this_mip = top_mip->mi_flink;
587 		     this_mip != next_mip;
588 		     this_mip = this_mip->mi_flink) {
589 			get_content(this_mip);
590 		}
591 		if (is_alternative(top_mip)) {
592 			this_mip = select_alternative(top_mip, next_mip);
593 			this_mip->mi_partnum = 0; /* suppress partnum display */
594 			this_mip->mi_flink = next_mip;
595 			this_mip->mi_blink = top_mip;
596 			top_mip->mi_flink  = this_mip;
597 		}
598 		/*
599 		 * Recurse into each part.
600 		 */
601 		for (this_mip = top_mip->mi_flink;
602 		     this_mip != next_mip;
603 		     this_mip = expand_mip(this_mip))
604 			continue;
605 	}
606 	else if (is_message(top_mip)) {
607 		top_mip->mi_ignore_body = 1; /* the first body is ignored */
608 		split_message(top_mip);
609 
610 		this_mip = top_mip->mi_flink;
611 		if (this_mip) {
612 			get_content(this_mip);
613 			/*
614 			 * If the one part is MIME encoded, recurse into it.
615 			 * XXX - Should this be conditional on subtype "rcs822"?
616 			 */
617 			if (this_mip->mi_type &&
618 			    this_mip->mi_version &&
619 			    equal(this_mip->mi_version, MIME_VERSION)) {
620 				this_mip->mi_partnum = 0;
621 				(void)expand_mip(this_mip);
622 			}
623 		}
624 	}
625 	return next_mip;
626 }
627 
628 
629 #if 0
630 static int
631 show_partnum(FILE *fp, struct mime_info *mip)
632 {
633 	int need_dot;
634 	need_dot = 0;
635 	if (mip->mi_parent.mip && mip->mi_parent.mip->mi_parent.mip)
636 		need_dot = show_partnum(fp, mip->mi_parent.mip);
637 
638 	if (mip->mi_partnum) {
639 		(void)fprintf(fp, "%s%d", need_dot ? "." : "",  mip->mi_partnum);
640 		need_dot = 1;
641 	}
642 	return need_dot;
643 }
644 #endif
645 
646 
647 PUBLIC struct mime_info *
mime_decode_open(struct message * mp)648 mime_decode_open(struct message *mp)
649 {
650 	struct mime_info *mip;
651 	struct mime_info *p;
652 
653 	mip = csalloc(1, sizeof(*mip));
654 	mip->mp = salloc(sizeof(*mip->mp));
655 	*mip->mp = *mp;		/* copy this so we don't trash the master mp */
656 
657 	get_content(mip);
658 
659 	/* RFC 2049 - sec 2 item 1 */
660 	if (mip->mi_version == NULL ||
661 	    !equal(mip->mi_version, MIME_VERSION))
662 		return NULL;
663 
664 	mip->mi_partstr = "";
665 	if (mip->mi_type)
666 		(void)expand_mip(mip);
667 
668 	/*
669 	 * Get the pipe_end and propagate it down the chain.
670 	 */
671 	mip->mi_pipe_end = last_registered_file(0); /* for mime_decode_close() */
672 	for (p = mip->mi_flink; p; p = p->mi_flink)
673 		p->mi_pipe_end = mip->mi_pipe_end;
674 
675 /*	show_mime_info(stderr, mip, NULL); */
676 
677 	return mip;
678 }
679 
680 
681 PUBLIC void
mime_decode_close(struct mime_info * mip)682 mime_decode_close(struct mime_info *mip)
683 {
684 	if (mip)
685 		close_top_files(mip->mi_pipe_end);
686 }
687 
688 
689 struct prefix_line_args_s {
690 	const char *prefix;
691 	size_t prefixlen;
692 };
693 
694 static void
prefix_line(FILE * fi,FILE * fo,void * cookie)695 prefix_line(FILE *fi, FILE *fo, void *cookie)
696 {
697 	struct prefix_line_args_s *args;
698 	const char *line;
699 	const char *prefix;
700 	size_t prefixlen;
701 	size_t length;
702 
703 	args = cookie;
704 	prefix    = args->prefix;
705 	prefixlen = args->prefixlen;
706 
707 	while ((line = fgetln(fi, &length)) != NULL) {
708 		if (length > 1)
709 			(void)fputs(prefix, fo);
710 		else
711 			(void)fwrite(prefix, sizeof(*prefix),
712 			    prefixlen, fo);
713 		(void)fwrite(line, sizeof(*line), length, fo);
714 	}
715 	(void)fflush(fo);
716 }
717 
718 PUBLIC int
mime_sendmessage(struct message * mp,FILE * obuf,struct ignoretab * igntab,const char * prefix,struct mime_info * mip)719 mime_sendmessage(struct message *mp, FILE *obuf, struct ignoretab *igntab,
720     const char *prefix, struct mime_info *mip)
721 {
722 	int error;
723 	int detachall_flag;
724 	const char *detachdir;
725 	FILE *end_of_prefix;
726 
727 	if (mip == NULL)
728 		return obuf ?	/* were we trying to detach? */
729 		    sendmessage(mp, obuf, igntab, prefix, NULL) : 0;
730 	/*
731 	 * The prefix has two meanigs which we handle here:
732 	 * 1) If obuf == NULL, then we are detaching to the 'prefix' directory.
733 	 * 2) If obuf != NULL, then the prefix is prepended to each line.
734 	 */
735 	detachdir = NULL;
736 	detachall_flag = igntab == detachall;
737 	if (obuf == NULL) {
738 		assert(prefix != NULL);		/* coding error! */
739 		if ((obuf = last_registered_file(0)) == NULL)
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 (!is_WSP(*dp))
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_head_end = obuf;
772 		mip->mi_detachdir = detachdir;
773 		mip->mi_detachall = detachall_flag;
774 		error |= sendmessage(mip->mp, pipe_end(mip), igntab, NULL, mip);
775 		close_top_files(end_of_prefix);	/* don't close the prefixer! */
776 	}
777 	return error;
778 }
779 
780 
781 #ifdef CHARSET_SUPPORT
782 /**********************************************
783  * higher level interface to run mime_ficonv().
784  */
785 static void
run_mime_ficonv(struct mime_info * mip,const char * charset)786 run_mime_ficonv(struct mime_info *mip, const char *charset)
787 {
788 	FILE *fo;
789 	iconv_t cd;
790 
791 	fo = pipe_end(mip);
792 
793 	if (charset == NULL ||
794 	    mip->mi_charset == NULL ||
795 	    strcasecmp(mip->mi_charset, charset) == 0 ||
796 	    strcasecmp(mip->mi_charset, "unknown") == 0)
797 		return;
798 
799 	cd = iconv_open(charset, mip->mi_charset);
800 	if (cd == (iconv_t)-1) {
801 		(void)fprintf(fo, "\t [ iconv_open failed: %s ]\n\n",
802 		    strerror(errno));
803 		(void)fflush(fo);	/* flush here or see double! */
804 		return;
805 	}
806 
807 	if (mip->mi_detachdir == NULL && /* don't contaminate the detach! */
808 	    value(ENAME_MIME_CHARSET_VERBOSE))
809 		(void)fprintf(fo, "\t[ converting %s -> %s ]\n\n",
810 		    mip->mi_charset, charset);
811 
812 	mime_run_function(mime_ficonv, fo, cd);
813 
814 	(void)iconv_close(cd);
815 }
816 #endif /* CHARSET_SUPPORT */
817 
818 
819 PUBLIC void
run_decoder(struct mime_info * mip,void (* fn)(FILE *,FILE *,void *))820 run_decoder(struct mime_info *mip, void(*fn)(FILE*, FILE*, void *))
821 {
822 #ifdef CHARSET_SUPPORT
823 	char *charset;
824 
825 	charset = value(ENAME_MIME_CHARSET);
826 	if (charset && mip->mi_type && strcasecmp(mip->mi_type, "text") == 0)
827 		run_mime_ficonv(mip, charset);
828 #endif /* CHARSET_SUPPORT */
829 
830 	if (mip->mi_detachdir == NULL &&
831 	    fn == mime_fio_copy)/* XXX - avoid an extra unnecessary pipe stage */
832 		return;
833 
834 	mime_run_function(fn, pipe_end(mip),
835 	    mip->mi_detachdir ? NULL : __UNCONST("add_lf"));
836 }
837 
838 
839 /*
840  * Determine how to handle the display based on the type and subtype
841  * fields.
842  */
843 enum dispmode_e {
844 	DM_IGNORE	= 0x00,	/* silently ignore part - must be zero! */
845 	DM_DISPLAY,		/* decode and display the part */
846 	DM_UNKNOWN,		/* unknown display */
847 	DM_BINARY,		/* indicate binary data */
848 	DM_PGPSIGN,		/* OpenPGP signed part */
849 	DM_PGPENCR,		/* OpenPGP encrypted part */
850 	DM_PGPKEYS,		/* OpenPGP keys part */
851 	DM_SENTINEL		/* end marker; shouldn't be used */
852 };
853 #define APPLICATION_OCTET_STREAM	DM_BINARY
854 
855 static enum dispmode_e
get_display_mode(struct mime_info * mip,mime_codec_t dec)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 *
mime_decode_body(struct mime_info * mip)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 			{ DM_SENTINEL,	NULL			},
1029 		};
1030 		const struct msg_tbl_s *mp;
1031 
1032 		for (mp = msg_tbl; mp->dm != DM_SENTINEL; mp++)
1033 			if (mp->dm == dispmode)
1034 				break;
1035 
1036 		assert(mp->dm != DM_SENTINEL);	/* 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  * Higher level header decoding interface.
1048  *
1049  * The core routines are in mime_header.c.
1050  */
1051 
1052 /*
1053  * Decode a portion of the header field.
1054  *
1055  * linebuf	buffer to decode into.
1056  * bufsize	size of linebuf.
1057  * hdrline	full header line including header name.
1058  * srcstr	pointer to string to decode
1059  */
1060 PUBLIC char *
mime_decode_hfield(char * linebuf,size_t bufsize,const char * hdrline,char * srcstr)1061 mime_decode_hfield(char *linebuf, size_t bufsize, const char *hdrline, char *srcstr)
1062 {
1063 	hfield_decoder_t decode;
1064 	decode = mime_hfield_decoder(hdrline);
1065 	if (decode) {
1066 		decode(linebuf, bufsize, srcstr);
1067 		return linebuf;
1068 	}
1069 	return srcstr;
1070 }
1071 
1072 /*
1073  * Return the next header field found in the input stream.
1074  * Return 0 if something found, -1 otherwise.
1075  * For a proper header, "*colon" is set to point to the colon
1076  * terminating the header name.  Otherwise it is NULL.
1077  *
1078  * NOTE: unlike gethfield() in support.c this:
1079  * 1) preserves folding (newlines),
1080  * 2) reads until fgetln() gets an EOF,
1081  * 3) only sets *colon if there is a "proper" one.
1082  */
1083 static int
get_folded_hfield(FILE * f,char * linebuf,size_t bufsize,char ** colon)1084 get_folded_hfield(FILE *f, char *linebuf, size_t bufsize, char **colon)
1085 {
1086 	char *cp, *cp2;
1087 	char *line;
1088 	size_t len;
1089 
1090 	if ((cp = fgetln(f, &len)) == NULL)
1091 		return -1;
1092 	for (cp2 = cp;
1093 	     cp2 < cp + len && isprint((unsigned char)*cp2) &&
1094 		 !is_WSP(*cp2) && *cp2 != ':';
1095 	     cp2++)
1096 		continue;
1097 	len = MIN(bufsize - 1, len);
1098 	bufsize -= len;
1099 	(void)memcpy(linebuf, cp, len);
1100 	*colon = *cp2 == ':' ? linebuf + (cp2 - cp) : NULL;
1101 	line = linebuf + len;
1102 	for (;;) {
1103 		int c;
1104 		(void)ungetc(c = getc(f), f);
1105 		if (!is_WSP(c))
1106 			break;
1107 
1108 		if ((cp = fgetln(f, &len)) == NULL)
1109 			break;
1110 		len = MIN(bufsize - 1, len);
1111 		bufsize -= len;
1112 		if (len == 0)
1113 			break;
1114 		(void)memcpy(line, cp, len);
1115 		line += len;
1116 	}
1117 	*line = 0;
1118 	return 0;
1119 }
1120 
1121 static void
decode_header(FILE * fi,FILE * fo,void * cookie __unused)1122 decode_header(FILE *fi, FILE *fo, void *cookie __unused)
1123 {
1124 	char linebuf[LINESIZE];
1125 	char *colon;
1126 #ifdef __lint__
1127 	cookie = cookie;
1128 #endif
1129 	while (get_folded_hfield(fi, linebuf, sizeof(linebuf), &colon) >= 0) {
1130 		char decbuf[LINESIZE];
1131 		char *hdrstr;
1132 		hdrstr = linebuf;
1133 		if (colon)
1134 			hdrstr = mime_decode_hfield(decbuf, sizeof(decbuf), hdrstr, hdrstr);
1135 		(void)fputs(hdrstr, fo);
1136 	}
1137 }
1138 
1139 PUBLIC FILE *
mime_decode_header(struct mime_info * mip)1140 mime_decode_header(struct mime_info *mip)
1141 {
1142 	int flags;
1143 	const char *cmd;
1144 	FILE *fo;
1145 
1146 	fo = pipe_end(mip);
1147 
1148 	if (mip->mi_detachdir) { /* We are detaching.  Don't run anything! */
1149 		(void)fflush(fo);
1150 		return pipe_end(mip);
1151 	}
1152 
1153 	if (mip->mi_partnum)
1154 		(void)fprintf(fo, "----- Part %s -----\n", mip->mi_partstr);
1155 
1156 	(void)fflush(fo);	/* Flush so the childern don't see it. */
1157 
1158 	/*
1159 	 * install the message hook before the head hook.
1160 	 */
1161 	cmd = get_command_hook(mip, "-hook");
1162 	mip->mi_command_hook = cmd;
1163 	if (cmd) {
1164 		flags = mime_run_command(cmd, pipe_end(mip));
1165 		mip->mi_head_end = last_registered_file(0);
1166 	}
1167 	else {
1168 		cmd = get_command_hook(mip, "-head");
1169 		mip->mi_head_end = last_registered_file(0);
1170 		flags = mime_run_command(cmd, pipe_end(mip));
1171 	}
1172 
1173 	if (value(ENAME_MIME_DECODE_HDR) && (flags & CMD_FLAG_NO_DECODE) == 0)
1174 		mime_run_function(decode_header, pipe_end(mip), NULL);
1175 
1176 	return pipe_end(mip);
1177 }
1178 
1179 #endif /* MIME_SUPPORT */
1180