xref: /netbsd-src/usr.bin/mail/mime_decode.c (revision 441cf27377d1a434f8583f9c24bf38c33b05f45c)
1*441cf273Smsaitoh /*	$NetBSD: mime_decode.c,v 1.18 2024/05/13 00:25:23 msaitoh Exp $	*/
28207b28aSchristos 
38207b28aSchristos /*-
48207b28aSchristos  * Copyright (c) 2006 The NetBSD Foundation, Inc.
58207b28aSchristos  * All rights reserved.
68207b28aSchristos  *
78207b28aSchristos  * This code is derived from software contributed to The NetBSD Foundation
88207b28aSchristos  * by Anon Ymous.
98207b28aSchristos  *
108207b28aSchristos  * Redistribution and use in source and binary forms, with or without
118207b28aSchristos  * modification, are permitted provided that the following conditions
128207b28aSchristos  * are met:
138207b28aSchristos  * 1. Redistributions of source code must retain the above copyright
148207b28aSchristos  *    notice, this list of conditions and the following disclaimer.
158207b28aSchristos  * 2. Redistributions in binary form must reproduce the above copyright
168207b28aSchristos  *    notice, this list of conditions and the following disclaimer in the
178207b28aSchristos  *    documentation and/or other materials provided with the distribution.
188207b28aSchristos  *
198207b28aSchristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
208207b28aSchristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
218207b28aSchristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
228207b28aSchristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
238207b28aSchristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
248207b28aSchristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
258207b28aSchristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
268207b28aSchristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
278207b28aSchristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
288207b28aSchristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
298207b28aSchristos  * POSSIBILITY OF SUCH DAMAGE.
308207b28aSchristos  */
318207b28aSchristos 
328207b28aSchristos 
338207b28aSchristos #ifdef MIME_SUPPORT
348207b28aSchristos 
358207b28aSchristos #include <sys/cdefs.h>
368207b28aSchristos #ifndef __lint__
37*441cf273Smsaitoh __RCSID("$NetBSD: mime_decode.c,v 1.18 2024/05/13 00:25:23 msaitoh Exp $");
388207b28aSchristos #endif /* not __lint__ */
398207b28aSchristos 
408207b28aSchristos #include <assert.h>
418207b28aSchristos #include <err.h>
428207b28aSchristos #include <fcntl.h>
438207b28aSchristos #include <libgen.h>
448207b28aSchristos #include <signal.h>
458207b28aSchristos #include <stdio.h>
468207b28aSchristos #include <stdlib.h>
478207b28aSchristos #include <string.h>
488207b28aSchristos #include <unistd.h>
498207b28aSchristos #include <iconv.h>
508207b28aSchristos 
518207b28aSchristos #include "def.h"
528207b28aSchristos #include "extern.h"
538207b28aSchristos #ifdef USE_EDITLINE
548207b28aSchristos #include "complete.h"
558207b28aSchristos #endif
568207b28aSchristos #ifdef MIME_SUPPORT
578207b28aSchristos #include "mime.h"
588207b28aSchristos #include "mime_child.h"
598207b28aSchristos #include "mime_codecs.h"
608207b28aSchristos #include "mime_header.h"
61f3098750Schristos #include "mime_detach.h"
628207b28aSchristos #endif
638207b28aSchristos #include "glob.h"
64f3098750Schristos #include "thread.h"
658207b28aSchristos 
668207b28aSchristos #if 0
678207b28aSchristos #ifndef __lint__
688207b28aSchristos /*
698207b28aSchristos  * XXX - This block for debugging only and eventually should go away.
708207b28aSchristos  */
718207b28aSchristos static void
728207b28aSchristos show_one_mime_info(FILE *fp, struct mime_info *mip)
738207b28aSchristos {
748207b28aSchristos #define XX(a) (a) ? (a) : "<null>"
758207b28aSchristos 
768207b28aSchristos 	(void)fprintf(fp, ">> --------\n");
778207b28aSchristos 	(void)fprintf(fp, "mip %d:\n", mip->mi_partnum);
788207b28aSchristos 	(void)fprintf(fp, "** Version: %s\n",  XX(mip->mi_version));
798207b28aSchristos 	(void)fprintf(fp, "** type: %s\n",     XX(mip->mi_type));
808207b28aSchristos 	(void)fprintf(fp, "** subtype: %s\n",  XX(mip->mi_subtype));
81f3098750Schristos 	(void)fprintf(fp, "** boundary: %s\n", XX(mip->mi_boundary));
828207b28aSchristos 	(void)fprintf(fp, "** charset: %s\n",  XX(mip->mi_charset));
838207b28aSchristos 	(void)fprintf(fp, "** encoding: %s\n", XX(mip->mi_encoding));
84f3098750Schristos 	(void)fprintf(fp, "** disposition: %s\n", XX(mip->mi_disposition));
85f3098750Schristos 	(void)fprintf(fp, "** filename: %s\n", XX(mip->mi_filename));
868207b28aSchristos 	(void)fprintf(fp, "** %p: flag: 0x%x, block: %ld, offset: %d, size: %lld, lines: %ld:%ld\n",
878207b28aSchristos 	    mip->mp,
888207b28aSchristos 	    mip->mp->m_flag,
898207b28aSchristos 	    mip->mp->m_block, mip->mp->m_offset, mip->mp->m_size,
908207b28aSchristos 	    mip->mp->m_lines, mip->mp->m_blines);
918207b28aSchristos 	(void)fprintf(fp, "** mip: %p\n", mip);
928207b28aSchristos 	(void)fprintf(fp, "** mi_flink: %p\n", mip->mi_flink);
938207b28aSchristos 	(void)fprintf(fp, "** mi_blink: %p\n", mip->mi_blink);
948207b28aSchristos 	(void)fprintf(fp, "** mip %p, mp %p,  parent_mip %p, parent_mp %p\n",
958207b28aSchristos 	    mip, mip->mp, mip->mi_parent.mip, mip->mi_parent.mp);
968207b28aSchristos 
978207b28aSchristos 	(void)fprintf(fp, "** mi_fo %p, mi_head_end %p, mi_pipe_end %p\n",
988207b28aSchristos 	    mip->mi_fo, mip->mi_head_end, mip->mi_pipe_end);
998207b28aSchristos 
100f3098750Schristos 	(void)fprintf(fp, "** mi_ignore_body: %d\n", mip->mi_ignore_body);
1018207b28aSchristos 	(void)fprintf(fp, "** mi_partnum: %d\n", mip->mi_partnum);
102f3098750Schristos 	(void)fprintf(fp, "** mi_partstr: %s\n", mip->mi_partstr);
10307a95a1dSchristos 	(void)fprintf(fp, "** mi_msgstr: %s\n", mip->mi_msgstr);
1048207b28aSchristos 
1058207b28aSchristos 	(void)fflush(fp);
1068207b28aSchristos 
1078207b28aSchristos #undef XX
1088207b28aSchristos }
1098207b28aSchristos 
1108207b28aSchristos __unused
1118207b28aSchristos static void
1128207b28aSchristos show_mime_info(FILE *fp, struct mime_info *mip, struct mime_info *end_mip)
1138207b28aSchristos {
114*441cf273Smsaitoh 	for (/* EMPTY */; mip != end_mip; mip = mip->mi_flink)
1158207b28aSchristos 		show_one_mime_info(fp, mip);
1168207b28aSchristos 
1178207b28aSchristos 	(void)fprintf(fp, "++ =========\n");
1188207b28aSchristos 	(void)fflush(fp);
1198207b28aSchristos }
1208207b28aSchristos #endif /* __lint__ */
1218207b28aSchristos #endif /* #if */
1228207b28aSchristos 
1238207b28aSchristos 
1248207b28aSchristos /*
1258207b28aSchristos  * Our interface to the file registry in popen.c
1268207b28aSchristos  */
127f3098750Schristos PUBLIC FILE *
pipe_end(struct mime_info * mip)1288207b28aSchristos pipe_end(struct mime_info *mip)
1298207b28aSchristos {
1308207b28aSchristos 	FILE *fp;
131b01c8fbcSchristos 	fp = last_registered_file(0);	/* get last registered file or pipe */
1328207b28aSchristos 	if (fp == NULL)
1338207b28aSchristos 		fp = mip->mi_fo;
1348207b28aSchristos 	return fp;
1358207b28aSchristos }
1368207b28aSchristos 
1378207b28aSchristos /*
1388207b28aSchristos  * Copy the first ';' delimited substring from 'src' (null terminated)
1398207b28aSchristos  * into 'dst', expanding quotes and removing comments (as per RFC
1408207b28aSchristos  * 822).  Returns a pointer in src to the next non-white character
1418207b28aSchristos  * following ';'.  The caller is responsible for ensuring 'dst' is
1428207b28aSchristos  * sufficiently large to hold the result.
1438207b28aSchristos  */
1448207b28aSchristos static char *
get_param(char * dst,char * src)1458207b28aSchristos get_param(char *dst, char *src)
1468207b28aSchristos {
1478207b28aSchristos 	char *lastq;
1488207b28aSchristos 	char *cp;
1498207b28aSchristos 	char *cp2;
1508207b28aSchristos 	int nesting;
1518207b28aSchristos 
1528207b28aSchristos 	cp2 = dst;
1538207b28aSchristos 	lastq = dst;
1548207b28aSchristos 	for (cp = src; *cp && *cp != ';'; cp++) {
1558207b28aSchristos 		switch (*cp) {
1568207b28aSchristos 		case '"':	/* start of quoted string */
1578207b28aSchristos 			for (cp++; *cp; cp++) {
1588207b28aSchristos 				if (*cp == '"')
1598207b28aSchristos 					break;
1608207b28aSchristos 				if (*cp == '\\' && cp[1] != '\0')
1618207b28aSchristos 					++cp;
1628207b28aSchristos 				*cp2++ = *cp;
1638207b28aSchristos 			}
1648207b28aSchristos 			lastq = cp2-1;
1658207b28aSchristos 			break;
1668207b28aSchristos 		case '(':	/* start of comment */
1678207b28aSchristos 			nesting = 1;
1688207b28aSchristos 			while (nesting > 0 && *++cp) {
1698207b28aSchristos 				if (*cp == '\\' && cp[1] != '\0')
1708207b28aSchristos 					cp++;
1718207b28aSchristos 				if (*cp == '(')
1728207b28aSchristos 					nesting++;
1738207b28aSchristos 				if (*cp == ')')
1748207b28aSchristos 					nesting--;
1758207b28aSchristos 			}
1768207b28aSchristos 			break;
1778207b28aSchristos 		default:
1788207b28aSchristos 			*cp2++ = *cp;
1798207b28aSchristos 			break;
1808207b28aSchristos 		}
1818207b28aSchristos 	}
1828207b28aSchristos 	/* remove trailing white space */
183d727506fSchristos 	while (cp2 > lastq && is_WSP(cp2[-1]))
1848207b28aSchristos 		cp2--;
1858207b28aSchristos 	*cp2 = '\0';
1868207b28aSchristos 	if (*cp == ';')
1878207b28aSchristos 		cp++;
188d727506fSchristos 	cp = skip_WSP(cp);
1898207b28aSchristos 	return cp;
1908207b28aSchristos }
1918207b28aSchristos 
1928207b28aSchristos /*
1938207b28aSchristos  * Content parameter
1948207b28aSchristos  *    if field is NULL, return the content "specifier".
1958207b28aSchristos  */
1968207b28aSchristos static char*
cparam(const char field[],char * src,int downcase)1978207b28aSchristos cparam(const char field[], char *src, int downcase)
1988207b28aSchristos {
1998207b28aSchristos 	char *cp;
2008207b28aSchristos 	char *dst;
2018207b28aSchristos 
2028207b28aSchristos 	if (src == NULL)
2038207b28aSchristos 		return NULL;
2048207b28aSchristos 
2058207b28aSchristos 	dst = salloc(strlen(src) + 1); /* large enough for any param in src */
206d727506fSchristos 	cp = skip_WSP(src);
2078207b28aSchristos 	cp = get_param(dst, cp);
2088207b28aSchristos 
2098207b28aSchristos 	if (field == NULL)
2108207b28aSchristos 		return dst;
2118207b28aSchristos 
2128207b28aSchristos 	while (*cp != '\0') {
2138207b28aSchristos 		size_t len = strlen(field);
2148207b28aSchristos 		cp = get_param(dst, cp);
2158207b28aSchristos 		if (strncasecmp(dst, field, len) == 0 && dst[len] == '=') {
2168207b28aSchristos 			char *cp2;
2178207b28aSchristos 			cp2 = dst + len + 1;
2188207b28aSchristos 			if (downcase)
2198207b28aSchristos 				istrcpy(cp2, cp2);
2208207b28aSchristos 			return cp2;
2218207b28aSchristos 		}
2228207b28aSchristos 	}
2238207b28aSchristos 	return NULL;
2248207b28aSchristos }
2258207b28aSchristos 
2268207b28aSchristos 
2278207b28aSchristos static void
get_content(struct mime_info * mip)2288207b28aSchristos get_content(struct mime_info *mip)
2298207b28aSchristos {
230f3098750Schristos 	char *mime_disposition_field;
2318207b28aSchristos 	char *mime_type_field;
232f3098750Schristos 	char *filename;
2338207b28aSchristos 	struct message *mp;
2348207b28aSchristos 	char *cp;
2358207b28aSchristos 
2368207b28aSchristos 	mp = mip->mp;
2378207b28aSchristos 	mip->mi_version  = cparam(NULL, hfield(MIME_HDR_VERSION,  mp), 0);
2388207b28aSchristos 	mip->mi_encoding = cparam(NULL, hfield(MIME_HDR_ENCODING, mp), 1);
2398207b28aSchristos 
2408207b28aSchristos 	mime_type_field = hfield(MIME_HDR_TYPE, mp);
2418207b28aSchristos 	mip->mi_type = cparam(NULL, mime_type_field, 1);
2428207b28aSchristos 	if (mip->mi_type) {
2438207b28aSchristos 		cp = strchr(mip->mi_type, '/');
2448207b28aSchristos 		if (cp)
2458207b28aSchristos 			*cp++ = '\0';
2468207b28aSchristos 		mip->mi_subtype = cp;
2478207b28aSchristos 	}
2488207b28aSchristos 	mip->mi_boundary = cparam("boundary", mime_type_field, 0);
249f3098750Schristos 	mip->mi_charset  = cparam("charset",  mime_type_field, 1);
250f3098750Schristos 
251f3098750Schristos 	mime_disposition_field = hfield(MIME_HDR_DISPOSITION, mp);
252f3098750Schristos 	mip->mi_disposition = cparam(NULL, mime_disposition_field, 1);
253f3098750Schristos 	/*
254f3098750Schristos 	 * The type field typically has a "name" parameter for "image"
255f3098750Schristos 	 * and "video" types, and I assume for other types as well.
256f3098750Schristos 	 * We grab it, but override it if the disposition field has a
257f3098750Schristos 	 * filename parameter as it often does for "attachments".
258f3098750Schristos 	 * More careful analysis could be done, but this seems to work
259f3098750Schristos 	 * pretty well.
260f3098750Schristos 	 */
261f3098750Schristos 	filename = cparam("name", mime_type_field, 0);
262f3098750Schristos 	if ((cp = cparam("filename", mime_disposition_field, 0)) != NULL)
263f3098750Schristos 		filename = cp;
264f3098750Schristos 	if (filename) {
265f3098750Schristos 		filename = basename(filename);	/* avoid absolute pathnames */
266f3098750Schristos 		filename = savestr(filename);	/* save it! */
267f3098750Schristos 	}
268f3098750Schristos 	mip->mi_filename = filename;
2698207b28aSchristos 
2701ed23800Schristos 	/*
2711ed23800Schristos 	 * XXX: If we have a "Content-Type" in the header, then assume
2721ed23800Schristos 	 * we also have a "MIME-Version: 1.0".  This fixes some broken
2731ed23800Schristos 	 * MIME headers that I have seen occasionally.
2741ed23800Schristos 	 */
2751ed23800Schristos 	if (mip->mi_version == NULL && mip->mi_type != NULL)
2761ed23800Schristos 		mip->mi_version = MIME_VERSION;
2771ed23800Schristos }
2788207b28aSchristos 
2798207b28aSchristos static struct message *
salloc_message(int flag,long block,short offset)2808207b28aSchristos salloc_message(int flag, long block, short offset)
2818207b28aSchristos {
2828207b28aSchristos 	struct message *mp;
2838207b28aSchristos 	/* use csalloc in case someone adds a field someday! */
2848207b28aSchristos 	mp = csalloc(1, sizeof(*mp));
2858207b28aSchristos 	mp->m_flag   = flag;
2868207b28aSchristos 	mp->m_block  = block;
2878207b28aSchristos 	mp->m_offset = offset;
2888207b28aSchristos #if 0
2898207b28aSchristos 	mp->m_lines  = 0;
2908207b28aSchristos 	mp->m_size   = 0;
2918207b28aSchristos 	mp->m_blines = 0;
2928207b28aSchristos #endif
2938207b28aSchristos 	return mp;
2948207b28aSchristos }
2958207b28aSchristos 
2968207b28aSchristos 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)297f3098750Schristos insert_new_mip(struct mime_info *this_mip, struct mime_info *top_mip,
298f3098750Schristos     struct message *top_mp, off_t end_pos, int partnum)
2998207b28aSchristos {
3008207b28aSchristos 	struct mime_info *new_mip;
301f3098750Schristos 
3028207b28aSchristos 	new_mip = csalloc(1, sizeof(*new_mip));
3038207b28aSchristos 	new_mip->mi_blink = this_mip;
3048207b28aSchristos 	new_mip->mi_flink = this_mip->mi_flink;
3058207b28aSchristos 	this_mip->mi_flink = new_mip;
306f3098750Schristos 
307f3098750Schristos 	new_mip->mp = salloc_message(this_mip->mp->m_flag,
308fc687570Sdogcow 	    (long)blockof(end_pos), blkoffsetof(end_pos));
309f3098750Schristos 
310f3098750Schristos 	new_mip->mi_parent.mip = top_mip;
311f3098750Schristos 	new_mip->mi_parent.mp = top_mp;
312f3098750Schristos 	new_mip->mi_partnum = partnum;
313f3098750Schristos 
3148207b28aSchristos 	return new_mip;
3158207b28aSchristos }
3168207b28aSchristos 
3178207b28aSchristos static void
split_multipart(struct mime_info * top_mip)3188207b28aSchristos split_multipart(struct mime_info *top_mip)
3198207b28aSchristos {
3208207b28aSchristos 	FILE *fp;
3218207b28aSchristos 	struct message *top_mp;
3228207b28aSchristos 	struct message *this_mp;
3238207b28aSchristos 	struct mime_info *this_mip;
3248207b28aSchristos 	off_t beg_pos;
3258207b28aSchristos 	const char *boundary;
3268207b28aSchristos 	size_t boundary_len;
3278207b28aSchristos 	long lines_left;	/* must be signed and same size as m_lines */
3288207b28aSchristos 	int partnum;
3298207b28aSchristos 	int in_header;
3308207b28aSchristos 
3318207b28aSchristos 	top_mp = top_mip->mp;
3328207b28aSchristos 	this_mp = salloc_message(top_mp->m_flag, top_mp->m_block, top_mp->m_offset);
3338207b28aSchristos 	this_mip = top_mip;
3348207b28aSchristos 	this_mip->mp = this_mp;
3358207b28aSchristos 
3368207b28aSchristos 	partnum = 1;
3378207b28aSchristos /*	top_mip->mi_partnum = partnum++;  */ /* Keep the number set by the caller */
3388207b28aSchristos 	in_header = 1;
3398207b28aSchristos 	boundary = top_mip->mi_boundary;
3408207b28aSchristos 	boundary_len = boundary ? strlen(boundary) : 0;
3418207b28aSchristos 
3428207b28aSchristos 	fp = setinput(top_mp);
3438207b28aSchristos 	beg_pos = ftello(fp);
344f3098750Schristos #if 0
345f3098750Schristos 	warnx("beg_pos: %lld,  m_lines: %ld,  m_blines: %ld",
346f3098750Schristos 	    beg_pos, top_mp->m_lines, top_mp->m_blines);
347f3098750Schristos #endif
3488207b28aSchristos 	for (lines_left = top_mp->m_lines - 1; lines_left >= 0; lines_left--) {
3498207b28aSchristos 		char *line;
3508207b28aSchristos 		size_t line_len;
3518207b28aSchristos 
3528207b28aSchristos 		line = fgetln(fp, &line_len);
3538207b28aSchristos 
3548207b28aSchristos 		this_mp->m_lines++;		/* count the message lines */
3558207b28aSchristos 
3568207b28aSchristos 		if (!in_header)
3578207b28aSchristos 			this_mp->m_blines++;	/* count the body lines */
3588207b28aSchristos 
3598207b28aSchristos 		if (lines_left == 0 || (
3608207b28aSchristos 			    !in_header &&
3618207b28aSchristos 			    line_len >= boundary_len + 2 &&
3628207b28aSchristos 			    line[0] == '-' && line[1] == '-' &&
3638207b28aSchristos 			    strncmp(line + 2, boundary, boundary_len) == 0)) {
3648207b28aSchristos 			off_t cur_pos;
3658207b28aSchristos 			off_t end_pos;
3668207b28aSchristos 
3678207b28aSchristos 			cur_pos = ftello(fp);
3688207b28aSchristos 
3698207b28aSchristos 			/* the boundary belongs to the next part */
3708207b28aSchristos 			end_pos = cur_pos - line_len;
3718207b28aSchristos 			this_mp->m_lines  -= 1;
3728207b28aSchristos 			this_mp->m_blines -= 1;
3738207b28aSchristos 
3748207b28aSchristos 			this_mp->m_size = end_pos - beg_pos;
375f3098750Schristos #if 0
376f3098750Schristos 			warnx("end_pos: %lld,  m_lines: %ld,  m_blines: %ld",
377f3098750Schristos 			    end_pos, this_mp->m_lines, this_mp->m_blines);
378f3098750Schristos #endif
3798207b28aSchristos 			if (line[boundary_len + 2] == '-' &&
3808207b28aSchristos 			    line[boundary_len + 3] == '-') {/* end of multipart */
3818207b28aSchristos 				/* do a sanity check on the EOM */
382f3098750Schristos 				if (lines_left != 1) {
3838207b28aSchristos 					/*
3848207b28aSchristos 					 * XXX - this can happen!
3858207b28aSchristos 					 * Should we display the
3868207b28aSchristos 					 * trailing garbage or check
3878207b28aSchristos 					 * that it is blank or just
3888207b28aSchristos 					 * ignore it?
3898207b28aSchristos 					 */
390f3098750Schristos #if 0
391f3098750Schristos 					(void)printf("EOM: lines left: %ld\n", lines_left);
392f3098750Schristos #endif
3938207b28aSchristos 				}
3948207b28aSchristos 				break;	/* XXX - stop at this point or grab the rest? */
3958207b28aSchristos 			}
396f3098750Schristos 			this_mip = insert_new_mip(this_mip, top_mip, top_mp, end_pos, partnum++);
397f3098750Schristos 			this_mp = this_mip->mp;
398f3098750Schristos 			this_mp->m_lines = 1; /* already read the first line in the header! */
3998207b28aSchristos 			beg_pos = end_pos;
4008207b28aSchristos 			in_header = 1;
4018207b28aSchristos 		}
4028207b28aSchristos 
4038207b28aSchristos 		if (line_len == 1)
4048207b28aSchristos 			in_header = 0;
4058207b28aSchristos 	}
4068207b28aSchristos }
4078207b28aSchristos 
4088207b28aSchristos static void
split_message(struct mime_info * top_mip)4098207b28aSchristos split_message(struct mime_info *top_mip)
4108207b28aSchristos {
4118207b28aSchristos 	struct mime_info *this_mip;
4128207b28aSchristos 	struct message *top_mp;
4138207b28aSchristos 	struct message *this_mp;
4148207b28aSchristos 	FILE *fp;
4158207b28aSchristos 	off_t beg_pos;
4168207b28aSchristos 	long lines_left;	/* must be same size as m_lines */
4178207b28aSchristos 	int in_header;
4188207b28aSchristos 
4198207b28aSchristos 	top_mp = top_mip->mp;
4208207b28aSchristos 	this_mp = salloc_message(top_mp->m_flag, top_mp->m_block, top_mp->m_offset);
4218207b28aSchristos 	this_mip = top_mip;
4228207b28aSchristos 	this_mip->mp = this_mp;
4238207b28aSchristos 
4248207b28aSchristos 	in_header = 1;
4258207b28aSchristos 
4268207b28aSchristos 	fp = setinput(top_mp);
4278207b28aSchristos 	beg_pos = ftello(fp);
4288207b28aSchristos 
4298207b28aSchristos 	for (lines_left = top_mp->m_lines; lines_left > 0; lines_left--) {
4308207b28aSchristos 		size_t line_len;
4318207b28aSchristos 
4328207b28aSchristos 		(void)fgetln(fp, &line_len);
4338207b28aSchristos 
4348207b28aSchristos 		this_mp->m_lines++;		/* count the message lines */
4358207b28aSchristos 		if (!in_header)
4368207b28aSchristos 			this_mp->m_blines++;	/* count the body lines */
4378207b28aSchristos 
4388207b28aSchristos 		if (in_header && line_len == 1) { /* end of header */
4398207b28aSchristos 			off_t end_pos;
4408207b28aSchristos 			end_pos = ftello(fp);
4418207b28aSchristos 			this_mp->m_size = end_pos - beg_pos;
442f3098750Schristos 			this_mip = insert_new_mip(this_mip, top_mip,top_mp, end_pos, 0);
443f3098750Schristos 			this_mp = this_mip->mp;
444f3098750Schristos 			this_mp->m_lines = 1; /* we already counted one line in the header! */
4458207b28aSchristos 			beg_pos = end_pos;
4468207b28aSchristos 			in_header = 0;	/* never in header again */
4478207b28aSchristos 		}
4488207b28aSchristos 	}
4498207b28aSchristos 
4508207b28aSchristos 	/* close the last message */
4518207b28aSchristos 	this_mp->m_size = ftello(fp) - beg_pos;
4528207b28aSchristos }
4538207b28aSchristos 
4548207b28aSchristos 
4558207b28aSchristos static const char *
get_command_hook(struct mime_info * mip,const char * domain)4568207b28aSchristos get_command_hook(struct mime_info *mip, const char *domain)
4578207b28aSchristos {
4588207b28aSchristos 	char *key;
4598207b28aSchristos 	char *cmd;
4608207b28aSchristos 
4618207b28aSchristos 	if (mip->mi_type == NULL)
4628207b28aSchristos 		return NULL;
4638207b28aSchristos 
4648207b28aSchristos 	/* XXX - should we use easprintf() here?  We are probably
4658207b28aSchristos 	 * hosed elsewhere if this fails anyway. */
4668207b28aSchristos 
4678207b28aSchristos 	cmd = NULL;
4688207b28aSchristos 	if (mip->mi_subtype) {
4698207b28aSchristos 		if (asprintf(&key, "mime%s-%s-%s",
4708207b28aSchristos 			domain,	mip->mi_type, mip->mi_subtype) == -1) {
4718207b28aSchristos 			warn("get_command_hook: subtupe: asprintf");
4728207b28aSchristos 			return NULL;
4738207b28aSchristos 		}
4748207b28aSchristos 		cmd = value(key);
4758207b28aSchristos 		free(key);
4768207b28aSchristos 	}
4778207b28aSchristos 	if (cmd == NULL) {
4788207b28aSchristos 		if (asprintf(&key, "mime%s-%s", domain, mip->mi_type) == -1) {
4798207b28aSchristos 			warn("get_command_hook: type: asprintf");
4808207b28aSchristos 			return NULL;
4818207b28aSchristos 		}
4828207b28aSchristos 		cmd = value(key);
4838207b28aSchristos 		free(key);
4848207b28aSchristos 	}
4858207b28aSchristos 	return cmd;
4868207b28aSchristos }
4878207b28aSchristos 
4888207b28aSchristos 
4898207b28aSchristos static int
is_basic_alternative(struct mime_info * mip)4908207b28aSchristos is_basic_alternative(struct mime_info *mip)
4918207b28aSchristos {
4928207b28aSchristos 	return
4938207b28aSchristos 	    strcasecmp(mip->mi_type, "text") == 0 &&
4948207b28aSchristos 	    strcasecmp(mip->mi_subtype, "plain") == 0;
4958207b28aSchristos }
4968207b28aSchristos 
4978207b28aSchristos static struct mime_info *
select_alternative(struct mime_info * top_mip,struct mime_info * end_mip)4988207b28aSchristos select_alternative(struct mime_info *top_mip, struct mime_info *end_mip)
4998207b28aSchristos {
5008207b28aSchristos 	struct mime_info *the_mip;	/* the chosen alternate */
5018207b28aSchristos 	struct mime_info *this_mip;
5028207b28aSchristos 	/*
5038207b28aSchristos 	 * The alternates are supposed to occur in order of
5048207b28aSchristos 	 * increasing "complexity".  So: if there is at least
5058207b28aSchristos 	 * one alternate of type "text/plain", use the last
5068207b28aSchristos 	 * one, otherwise default to the first alternate.
5078207b28aSchristos 	 */
5088207b28aSchristos 	the_mip = top_mip->mi_flink;
5098207b28aSchristos 	for (this_mip = top_mip->mi_flink;
5108207b28aSchristos 	     this_mip != end_mip;
5118207b28aSchristos 	     this_mip = this_mip->mi_flink) {
5128207b28aSchristos 		const char *cmd;
5138207b28aSchristos 
5148207b28aSchristos 		if (this_mip->mi_type == NULL ||
5158207b28aSchristos 		    this_mip->mi_subtype == NULL)
5168207b28aSchristos 			continue;
5178207b28aSchristos 
5188207b28aSchristos 		if (is_basic_alternative(this_mip))
5198207b28aSchristos 			the_mip = this_mip;
5208207b28aSchristos 		else if (
5218207b28aSchristos 			(cmd = get_command_hook(this_mip, "-hook")) ||
5228207b28aSchristos 			(cmd = get_command_hook(this_mip, "-head")) ||
5238207b28aSchristos 			(cmd = get_command_hook(this_mip, "-body"))) {
5248207b28aSchristos 			int flags;
5258207b28aSchristos 			/* just get the flags. */
5268207b28aSchristos 			flags = mime_run_command(cmd, NULL);
5278207b28aSchristos 			if ((flags & CMD_FLAG_ALTERNATIVE) != 0)
5288207b28aSchristos 				the_mip = this_mip;
5298207b28aSchristos 		}
5308207b28aSchristos 	}
5318207b28aSchristos 	return the_mip;
5328207b28aSchristos }
5338207b28aSchristos 
5348207b28aSchristos 
5358207b28aSchristos static inline int
is_multipart(struct mime_info * mip)5368207b28aSchristos is_multipart(struct mime_info *mip)
5378207b28aSchristos {
5388207b28aSchristos 	return mip->mi_type &&
5398207b28aSchristos 	    strcasecmp("multipart", mip->mi_type) == 0;
5408207b28aSchristos }
5418207b28aSchristos static inline int
is_message(struct mime_info * mip)5428207b28aSchristos is_message(struct mime_info *mip)
5438207b28aSchristos {
5448207b28aSchristos 	return mip->mi_type &&
5458207b28aSchristos 	    strcasecmp("message", mip->mi_type) == 0;
5468207b28aSchristos }
5478207b28aSchristos 
5488207b28aSchristos static inline int
is_alternative(struct mime_info * mip)5498207b28aSchristos is_alternative(struct mime_info *mip)
5508207b28aSchristos {
5518207b28aSchristos 	return mip->mi_subtype &&
5528207b28aSchristos 	    strcasecmp("alternative", mip->mi_subtype) == 0;
5538207b28aSchristos }
5548207b28aSchristos 
5558207b28aSchristos 
5568207b28aSchristos /*
5578207b28aSchristos  * Take a mime_info pointer and expand it recursively into all its
5588207b28aSchristos  * mime parts.  Only "multipart" and "message" types recursed into;
5598207b28aSchristos  * they are handled separately.
5608207b28aSchristos  */
5618207b28aSchristos static struct mime_info *
expand_mip(struct mime_info * top_mip)5628207b28aSchristos expand_mip(struct mime_info *top_mip)
5638207b28aSchristos {
5648207b28aSchristos 	struct mime_info *this_mip;
5658207b28aSchristos 	struct mime_info *next_mip;
5668207b28aSchristos 
567f3098750Schristos 	if (top_mip->mi_partnum == 0) {
568f3098750Schristos 		if (top_mip->mi_blink)
569f3098750Schristos 			top_mip->mi_partstr = top_mip->mi_blink->mi_partstr;
570f3098750Schristos 	}
571f3098750Schristos 	else if (top_mip->mi_parent.mip) {
572f3098750Schristos 		const char *prefix;
573f3098750Schristos 		char *cp;
574f3098750Schristos 		prefix = top_mip->mi_parent.mip->mi_partstr;
575f3098750Schristos 		(void)sasprintf(&cp, "%s%s%d", prefix,
576f3098750Schristos 		    *prefix ? "." : "", top_mip->mi_partnum);
577f3098750Schristos 		top_mip->mi_partstr = cp;
578f3098750Schristos 	}
579f3098750Schristos 
5808207b28aSchristos 	next_mip = top_mip->mi_flink;
5818207b28aSchristos 
5828207b28aSchristos 	if (is_multipart(top_mip)) {
583f3098750Schristos 		top_mip->mi_ignore_body = 1; /* the first body is ignored */
5848207b28aSchristos 		split_multipart(top_mip);
5858207b28aSchristos 
5868207b28aSchristos 		for (this_mip = top_mip->mi_flink;
5878207b28aSchristos 		     this_mip != next_mip;
5888207b28aSchristos 		     this_mip = this_mip->mi_flink) {
5898207b28aSchristos 			get_content(this_mip);
5908207b28aSchristos 		}
5918207b28aSchristos 		if (is_alternative(top_mip)) {
5928207b28aSchristos 			this_mip = select_alternative(top_mip, next_mip);
5938207b28aSchristos 			this_mip->mi_partnum = 0; /* suppress partnum display */
5948207b28aSchristos 			this_mip->mi_flink = next_mip;
5958207b28aSchristos 			this_mip->mi_blink = top_mip;
5968207b28aSchristos 			top_mip->mi_flink  = this_mip;
5978207b28aSchristos 		}
5988207b28aSchristos 		/*
5998207b28aSchristos 		 * Recurse into each part.
6008207b28aSchristos 		 */
6018207b28aSchristos 		for (this_mip = top_mip->mi_flink;
6028207b28aSchristos 		     this_mip != next_mip;
6038207b28aSchristos 		     this_mip = expand_mip(this_mip))
6048207b28aSchristos 			continue;
6058207b28aSchristos 	}
6068207b28aSchristos 	else if (is_message(top_mip)) {
607f3098750Schristos 		top_mip->mi_ignore_body = 1; /* the first body is ignored */
6088207b28aSchristos 		split_message(top_mip);
6098207b28aSchristos 
6108207b28aSchristos 		this_mip = top_mip->mi_flink;
6118207b28aSchristos 		if (this_mip) {
6128207b28aSchristos 			get_content(this_mip);
6138207b28aSchristos 			/*
6148207b28aSchristos 			 * If the one part is MIME encoded, recurse into it.
6158207b28aSchristos 			 * XXX - Should this be conditional on subtype "rcs822"?
6168207b28aSchristos 			 */
6178207b28aSchristos 			if (this_mip->mi_type &&
6188207b28aSchristos 			    this_mip->mi_version &&
6198207b28aSchristos 			    equal(this_mip->mi_version, MIME_VERSION)) {
6208207b28aSchristos 				this_mip->mi_partnum = 0;
6218207b28aSchristos 				(void)expand_mip(this_mip);
6228207b28aSchristos 			}
6238207b28aSchristos 		}
6248207b28aSchristos 	}
6258207b28aSchristos 	return next_mip;
6268207b28aSchristos }
6278207b28aSchristos 
6288207b28aSchristos 
629f3098750Schristos #if 0
6308207b28aSchristos static int
6318207b28aSchristos show_partnum(FILE *fp, struct mime_info *mip)
6328207b28aSchristos {
6338207b28aSchristos 	int need_dot;
6348207b28aSchristos 	need_dot = 0;
6358207b28aSchristos 	if (mip->mi_parent.mip && mip->mi_parent.mip->mi_parent.mip)
6368207b28aSchristos 		need_dot = show_partnum(fp, mip->mi_parent.mip);
6378207b28aSchristos 
6388207b28aSchristos 	if (mip->mi_partnum) {
6398207b28aSchristos 		(void)fprintf(fp, "%s%d", need_dot ? "." : "",  mip->mi_partnum);
6408207b28aSchristos 		need_dot = 1;
6418207b28aSchristos 	}
6428207b28aSchristos 	return need_dot;
6438207b28aSchristos }
644f3098750Schristos #endif
6458207b28aSchristos 
6468207b28aSchristos 
6478207b28aSchristos PUBLIC struct mime_info *
mime_decode_open(struct message * mp)6488207b28aSchristos mime_decode_open(struct message *mp)
6498207b28aSchristos {
6508207b28aSchristos 	struct mime_info *mip;
651f3098750Schristos 	struct mime_info *p;
6528207b28aSchristos 
6538207b28aSchristos 	mip = csalloc(1, sizeof(*mip));
6548207b28aSchristos 	mip->mp = salloc(sizeof(*mip->mp));
655f3098750Schristos 	*mip->mp = *mp;		/* copy this so we don't trash the master mp */
6568207b28aSchristos 
6578207b28aSchristos 	get_content(mip);
6588207b28aSchristos 
6598207b28aSchristos 	/* RFC 2049 - sec 2 item 1 */
6608207b28aSchristos 	if (mip->mi_version == NULL ||
6618207b28aSchristos 	    !equal(mip->mi_version, MIME_VERSION))
6628207b28aSchristos 		return NULL;
6638207b28aSchristos 
664f3098750Schristos 	mip->mi_partstr = "";
6658207b28aSchristos 	if (mip->mi_type)
6668207b28aSchristos 		(void)expand_mip(mip);
6678207b28aSchristos 
668f3098750Schristos 	/*
669f3098750Schristos 	 * Get the pipe_end and propagate it down the chain.
670f3098750Schristos 	 */
671f3098750Schristos 	mip->mi_pipe_end = last_registered_file(0); /* for mime_decode_close() */
672f3098750Schristos 	for (p = mip->mi_flink; p; p = p->mi_flink)
673f3098750Schristos 		p->mi_pipe_end = mip->mi_pipe_end;
674f3098750Schristos 
6758207b28aSchristos /*	show_mime_info(stderr, mip, NULL); */
6768207b28aSchristos 
6778207b28aSchristos 	return mip;
6788207b28aSchristos }
6798207b28aSchristos 
6808207b28aSchristos 
6818207b28aSchristos PUBLIC void
mime_decode_close(struct mime_info * mip)6828207b28aSchristos mime_decode_close(struct mime_info *mip)
6838207b28aSchristos {
6848207b28aSchristos 	if (mip)
6858207b28aSchristos 		close_top_files(mip->mi_pipe_end);
6868207b28aSchristos }
6878207b28aSchristos 
6888207b28aSchristos 
6898207b28aSchristos struct prefix_line_args_s {
6908207b28aSchristos 	const char *prefix;
6918207b28aSchristos 	size_t prefixlen;
6928207b28aSchristos };
6938207b28aSchristos 
6948207b28aSchristos static void
prefix_line(FILE * fi,FILE * fo,void * cookie)6958207b28aSchristos prefix_line(FILE *fi, FILE *fo, void *cookie)
6968207b28aSchristos {
6978207b28aSchristos 	struct prefix_line_args_s *args;
6988207b28aSchristos 	const char *line;
6998207b28aSchristos 	const char *prefix;
7008207b28aSchristos 	size_t prefixlen;
7018207b28aSchristos 	size_t length;
7028207b28aSchristos 
7038207b28aSchristos 	args = cookie;
7048207b28aSchristos 	prefix    = args->prefix;
7058207b28aSchristos 	prefixlen = args->prefixlen;
7068207b28aSchristos 
7078207b28aSchristos 	while ((line = fgetln(fi, &length)) != NULL) {
7088207b28aSchristos 		if (length > 1)
7098207b28aSchristos 			(void)fputs(prefix, fo);
7108207b28aSchristos 		else
7112283d346Schristos 			(void)fwrite(prefix, sizeof(*prefix),
7128207b28aSchristos 			    prefixlen, fo);
7138207b28aSchristos 		(void)fwrite(line, sizeof(*line), length, fo);
7148207b28aSchristos 	}
7158207b28aSchristos 	(void)fflush(fo);
7168207b28aSchristos }
7178207b28aSchristos 
7188207b28aSchristos PUBLIC int
mime_sendmessage(struct message * mp,FILE * obuf,struct ignoretab * igntab,const char * prefix,struct mime_info * mip)719f3098750Schristos mime_sendmessage(struct message *mp, FILE *obuf, struct ignoretab *igntab,
7208207b28aSchristos     const char *prefix, struct mime_info *mip)
7218207b28aSchristos {
7228207b28aSchristos 	int error;
723f3098750Schristos 	int detachall_flag;
724f3098750Schristos 	const char *detachdir;
7258207b28aSchristos 	FILE *end_of_prefix;
7268207b28aSchristos 
7278207b28aSchristos 	if (mip == NULL)
728f3098750Schristos 		return obuf ?	/* were we trying to detach? */
729f3098750Schristos 		    sendmessage(mp, obuf, igntab, prefix, NULL) : 0;
7308207b28aSchristos 	/*
731f3098750Schristos 	 * The prefix has two meanigs which we handle here:
732b01c8fbcSchristos 	 * 1) If obuf == NULL, then we are detaching to the 'prefix' directory.
733f3098750Schristos 	 * 2) If obuf != NULL, then the prefix is prepended to each line.
734f3098750Schristos 	 */
735f3098750Schristos 	detachdir = NULL;
736f3098750Schristos 	detachall_flag = igntab == detachall;
737f3098750Schristos 	if (obuf == NULL) {
738f3098750Schristos 		assert(prefix != NULL);		/* coding error! */
739581e519fSchristos 		if ((obuf = last_registered_file(0)) == NULL)
740f3098750Schristos 			obuf = stdout;
741f3098750Schristos 		detachdir = prefix;
742f3098750Schristos 		prefix = NULL;
743f3098750Schristos 		igntab = ignoreall;	/* always ignore the headers */
744f3098750Schristos 	}
745f3098750Schristos 	/*
746f3098750Schristos 	 * Set this early so pipe_end() will work!
7478207b28aSchristos 	 */
7488207b28aSchristos 	mip->mi_fo = obuf;
7498207b28aSchristos 
7503bf60b13Schristos 	(void)fflush(obuf);  /* Be safe and flush!  XXX - necessary? */
7513bf60b13Schristos 
7528207b28aSchristos 	/*
7538207b28aSchristos 	 * Handle the prefix as a pipe stage so it doesn't get seen by
7548207b28aSchristos 	 * any decoding or hooks.
7558207b28aSchristos 	 */
7568207b28aSchristos 	if (prefix != NULL) {
7578207b28aSchristos 		static struct prefix_line_args_s prefix_line_args;
7588207b28aSchristos 		const char *dp, *dp2 = NULL;
7598207b28aSchristos 		for (dp = prefix; *dp; dp++)
760d727506fSchristos 			if (!is_WSP(*dp))
7618207b28aSchristos 				dp2 = dp;
7628207b28aSchristos 		prefix_line_args.prefixlen = dp2 == 0 ? 0 : dp2 - prefix + 1;
7638207b28aSchristos 		prefix_line_args.prefix = prefix;
7648207b28aSchristos 		mime_run_function(prefix_line, pipe_end(mip), (void*)&prefix_line_args);
7658207b28aSchristos 	}
7668207b28aSchristos 
7678207b28aSchristos 	end_of_prefix = last_registered_file(0);
7688207b28aSchristos 	error = 0;
7698207b28aSchristos 	for (/*EMPTY*/; mip; mip = mip->mi_flink) {
7708207b28aSchristos 		mip->mi_fo = obuf;
771581e519fSchristos 		mip->mi_head_end = obuf;
772f3098750Schristos 		mip->mi_detachdir = detachdir;
773f3098750Schristos 		mip->mi_detachall = detachall_flag;
774f3098750Schristos 		error |= sendmessage(mip->mp, pipe_end(mip), igntab, NULL, mip);
7758207b28aSchristos 		close_top_files(end_of_prefix);	/* don't close the prefixer! */
7768207b28aSchristos 	}
7778207b28aSchristos 	return error;
7788207b28aSchristos }
7798207b28aSchristos 
7808207b28aSchristos 
7818207b28aSchristos #ifdef CHARSET_SUPPORT
7828207b28aSchristos /**********************************************
7838207b28aSchristos  * higher level interface to run mime_ficonv().
7848207b28aSchristos  */
7858207b28aSchristos static void
run_mime_ficonv(struct mime_info * mip,const char * charset)7868207b28aSchristos run_mime_ficonv(struct mime_info *mip, const char *charset)
7878207b28aSchristos {
7888207b28aSchristos 	FILE *fo;
7898207b28aSchristos 	iconv_t cd;
7908207b28aSchristos 
7918207b28aSchristos 	fo = pipe_end(mip);
7928207b28aSchristos 
7938207b28aSchristos 	if (charset == NULL ||
7948207b28aSchristos 	    mip->mi_charset == NULL ||
7958207b28aSchristos 	    strcasecmp(mip->mi_charset, charset) == 0 ||
7968207b28aSchristos 	    strcasecmp(mip->mi_charset, "unknown") == 0)
7978207b28aSchristos 		return;
7988207b28aSchristos 
7998207b28aSchristos 	cd = iconv_open(charset, mip->mi_charset);
8008207b28aSchristos 	if (cd == (iconv_t)-1) {
8018207b28aSchristos 		(void)fprintf(fo, "\t [ iconv_open failed: %s ]\n\n",
8028207b28aSchristos 		    strerror(errno));
8038207b28aSchristos 		(void)fflush(fo);	/* flush here or see double! */
8048207b28aSchristos 		return;
8058207b28aSchristos 	}
8068207b28aSchristos 
807f3098750Schristos 	if (mip->mi_detachdir == NULL && /* don't contaminate the detach! */
808f3098750Schristos 	    value(ENAME_MIME_CHARSET_VERBOSE))
809f3098750Schristos 		(void)fprintf(fo, "\t[ converting %s -> %s ]\n\n",
810f3098750Schristos 		    mip->mi_charset, charset);
8118207b28aSchristos 
8128207b28aSchristos 	mime_run_function(mime_ficonv, fo, cd);
8138207b28aSchristos 
814f20e38f6Schristos 	(void)iconv_close(cd);
8158207b28aSchristos }
8168207b28aSchristos #endif /* CHARSET_SUPPORT */
8178207b28aSchristos 
8188207b28aSchristos 
819f3098750Schristos PUBLIC void
run_decoder(struct mime_info * mip,void (* fn)(FILE *,FILE *,void *))8208207b28aSchristos run_decoder(struct mime_info *mip, void(*fn)(FILE*, FILE*, void *))
8218207b28aSchristos {
8228207b28aSchristos #ifdef CHARSET_SUPPORT
8238207b28aSchristos 	char *charset;
8248207b28aSchristos 
8258207b28aSchristos 	charset = value(ENAME_MIME_CHARSET);
8268207b28aSchristos 	if (charset && mip->mi_type && strcasecmp(mip->mi_type, "text") == 0)
8278207b28aSchristos 		run_mime_ficonv(mip, charset);
8288207b28aSchristos #endif /* CHARSET_SUPPORT */
8298207b28aSchristos 
830f3098750Schristos 	if (mip->mi_detachdir == NULL &&
831f3098750Schristos 	    fn == mime_fio_copy)/* XXX - avoid an extra unnecessary pipe stage */
8328207b28aSchristos 		return;
8338207b28aSchristos 
834f3098750Schristos 	mime_run_function(fn, pipe_end(mip),
835f3098750Schristos 	    mip->mi_detachdir ? NULL : __UNCONST("add_lf"));
8368207b28aSchristos }
8378207b28aSchristos 
8388207b28aSchristos 
8398207b28aSchristos /*
8408207b28aSchristos  * Determine how to handle the display based on the type and subtype
8418207b28aSchristos  * fields.
8428207b28aSchristos  */
8438207b28aSchristos enum dispmode_e {
8448207b28aSchristos 	DM_IGNORE	= 0x00,	/* silently ignore part - must be zero! */
8458207b28aSchristos 	DM_DISPLAY,		/* decode and display the part */
8468207b28aSchristos 	DM_UNKNOWN,		/* unknown display */
8478207b28aSchristos 	DM_BINARY,		/* indicate binary data */
8488207b28aSchristos 	DM_PGPSIGN,		/* OpenPGP signed part */
8498207b28aSchristos 	DM_PGPENCR,		/* OpenPGP encrypted part */
850c172e3b9Slukem 	DM_PGPKEYS,		/* OpenPGP keys part */
851c172e3b9Slukem 	DM_SENTINEL		/* end marker; shouldn't be used */
8528207b28aSchristos };
8538207b28aSchristos #define APPLICATION_OCTET_STREAM	DM_BINARY
8548207b28aSchristos 
8558207b28aSchristos static enum dispmode_e
get_display_mode(struct mime_info * mip,mime_codec_t dec)8568207b28aSchristos get_display_mode(struct mime_info *mip, mime_codec_t dec)
8578207b28aSchristos {
8588207b28aSchristos 	struct mime_subtype_s {
8598207b28aSchristos 		const char *st_name;
8608207b28aSchristos 		enum dispmode_e st_dispmode;
8618207b28aSchristos 	};
8628207b28aSchristos 	struct mime_type_s {
8638207b28aSchristos 		const char *mt_type;
8648207b28aSchristos 		const struct mime_subtype_s *mt_subtype;
8658207b28aSchristos 		enum dispmode_e mt_dispmode;	/* default if NULL subtype */
8668207b28aSchristos 	};
8678207b28aSchristos 	static const struct mime_subtype_s text_subtype_tbl[] = {
8688207b28aSchristos 		{ "plain",		DM_DISPLAY },
8698207b28aSchristos 		{ "html", 		DM_DISPLAY },	/* rfc2854 */
8708207b28aSchristos 		{ "rfc822-headers",	DM_DISPLAY },
8718207b28aSchristos 		{ "css",		DM_DISPLAY },	/* rfc2318 */
8728207b28aSchristos 		{ "enriched",		DM_DISPLAY },	/* rfc1523/rfc1563/rfc1896 */
8738207b28aSchristos 		{ "graphics",		DM_DISPLAY },	/* rfc0553 */
8748207b28aSchristos 		{ "nroff",		DM_DISPLAY },	/* rfc4263 */
8758207b28aSchristos 		{ "red",		DM_DISPLAY },	/* rfc4102 */
8768207b28aSchristos 		{ NULL,			DM_DISPLAY }	/* default */
8778207b28aSchristos 	};
8788207b28aSchristos 	static const struct mime_subtype_s image_subtype_tbl[] = {
8798207b28aSchristos 		{ "tiff",		DM_BINARY },	/* rfc2302/rfc3302 */
8808207b28aSchristos 		{ "tiff-fx",		DM_BINARY },	/* rfc3250/rfc3950 */
8818207b28aSchristos 		{ "t38",		DM_BINARY },	/* rfc3362 */
8828207b28aSchristos 		{ NULL,			DM_BINARY }	/* default */
8838207b28aSchristos 	};
8848207b28aSchristos 	static const struct mime_subtype_s audio_subtype_tbl[] = {
8858207b28aSchristos 		{ "mpeg",		DM_BINARY },	/* rfc3003 */
8868207b28aSchristos 		{ "t38",		DM_BINARY },	/* rfc4612 */
8878207b28aSchristos 		{ NULL,			DM_BINARY }	/* default */
8888207b28aSchristos 	};
8898207b28aSchristos 	static const struct mime_subtype_s video_subtype_tbl[] = {
8908207b28aSchristos 		{ NULL,			DM_BINARY }	/* default */
8918207b28aSchristos 	};
8928207b28aSchristos 	static const struct mime_subtype_s application_subtype_tbl[] = {
8938207b28aSchristos 		{ "octet-stream",	APPLICATION_OCTET_STREAM },
8948207b28aSchristos 		{ "pgp-encrypted",      DM_PGPENCR },   /* rfc3156 */
8958207b28aSchristos 		{ "pgp-keys",           DM_PGPKEYS },   /* rfc3156 */
8968207b28aSchristos 		{ "pgp-signature",      DM_PGPSIGN },   /* rfc3156 */
8978207b28aSchristos 		{ "pdf",		DM_BINARY },	/* rfc3778 */
8988207b28aSchristos 		{ "whoispp-query",	DM_UNKNOWN },	/* rfc2957 */
8998207b28aSchristos 		{ "whoispp-response",	DM_UNKNOWN },	/* rfc2958 */
9008207b28aSchristos 		{ "font-tdpfr",		DM_UNKNOWN },	/* rfc3073 */
9018207b28aSchristos 		{ "xhtml+xml",		DM_UNKNOWN },	/* rfc3236 */
9028207b28aSchristos 		{ "ogg",		DM_UNKNOWN },	/* rfc3534 */
9038207b28aSchristos 		{ "rdf+xml",		DM_UNKNOWN },	/* rfc3870 */
9048207b28aSchristos 		{ "soap+xml",		DM_UNKNOWN },	/* rfc3902 */
9058207b28aSchristos 		{ "mbox",		DM_UNKNOWN },	/* rfc4155 */
9068207b28aSchristos 		{ "xv+xml",		DM_UNKNOWN },	/* rfc4374 */
9078207b28aSchristos 		{ "smil",		DM_UNKNOWN },	/* rfc4536 */
9088207b28aSchristos 		{ "smil+xml",		DM_UNKNOWN },	/* rfc4536 */
9098207b28aSchristos 		{ "json",		DM_UNKNOWN },	/* rfc4627 */
9108207b28aSchristos 		{ "voicexml+xml",	DM_UNKNOWN },	/* rfc4267 */
9118207b28aSchristos 		{ "ssml+xml",		DM_UNKNOWN },	/* rfc4267 */
9128207b28aSchristos 		{ "srgs",		DM_UNKNOWN },	/* rfc4267 */
9138207b28aSchristos 		{ "srgs+xml",		DM_UNKNOWN },	/* rfc4267 */
9148207b28aSchristos 		{ "ccxml+xml",		DM_UNKNOWN },	/* rfc4267 */
9158207b28aSchristos 		{ "pls+xml.",		DM_UNKNOWN },	/* rfc4267 */
9168207b28aSchristos 		{ NULL,			APPLICATION_OCTET_STREAM } /* default */
9178207b28aSchristos 	};
9188207b28aSchristos 	static const struct mime_type_s mime_type_tbl[] = {
9198207b28aSchristos 		{ "text",	 text_subtype_tbl,		DM_DISPLAY },
9208207b28aSchristos 		{ "image",	 image_subtype_tbl,		DM_IGNORE },
9218207b28aSchristos 		{ "audio",	 audio_subtype_tbl,		DM_IGNORE },
9228207b28aSchristos 		{ "video",	 video_subtype_tbl,		DM_IGNORE },
9238207b28aSchristos 		{ "application", application_subtype_tbl,	APPLICATION_OCTET_STREAM },
92407a95a1dSchristos 		{ NULL,		 NULL,				DM_UNKNOWN }, /* default */
9258207b28aSchristos 	};
9268207b28aSchristos 	const struct mime_type_s *mtp;
9278207b28aSchristos 	const struct mime_subtype_s *stp;
9288207b28aSchristos 	const char *mi_type;
9298207b28aSchristos 	const char *mi_subtype;
9308207b28aSchristos 
9318207b28aSchristos 	/*
9328207b28aSchristos 	 * Silently ignore all multipart bodies.
9338207b28aSchristos 	 * 1) In the case of "multipart" types, this typically
9348207b28aSchristos 	 *    contains a message for non-mime enabled mail readers.
9358207b28aSchristos 	 * 2) In the case of "message" type, there should be no body.
9368207b28aSchristos 	 */
937f3098750Schristos 	if (mip->mi_ignore_body)	/*is_multipart(mip) || is_message(mip))*/
9388207b28aSchristos 		return DM_IGNORE;
9398207b28aSchristos 
9408207b28aSchristos 	/*
9418207b28aSchristos 	 * If the encoding type given but not recognized, treat block
9428207b28aSchristos 	 * as "application/octet-stream".  rfc 2049 sec 2 part 2.
9438207b28aSchristos 	 */
9448207b28aSchristos 	if (mip->mi_encoding && dec == NULL)
9458207b28aSchristos 		return APPLICATION_OCTET_STREAM;
9468207b28aSchristos 
9478207b28aSchristos 	mi_type    = mip->mi_type;
9488207b28aSchristos 	mi_subtype = mip->mi_type ? mip->mi_subtype : NULL;
9498207b28aSchristos 
9508207b28aSchristos 	/*
9518207b28aSchristos 	 * If there was no type specified, display anyway so we don't
9528207b28aSchristos 	 * miss anything.  (The encoding type is known.)
9538207b28aSchristos 	 */
9548207b28aSchristos 	if (mi_type == NULL)
9558207b28aSchristos 		return DM_DISPLAY;	/* XXX - default to something safe! */
9568207b28aSchristos 
9578207b28aSchristos 	for (mtp = mime_type_tbl; mtp->mt_type; mtp++) {
9588207b28aSchristos 		if (strcasecmp(mtp->mt_type, mi_type) == 0) {
9598207b28aSchristos 			if (mi_subtype == NULL)
9608207b28aSchristos 				return mtp->mt_dispmode;
9618207b28aSchristos 			for (stp = mtp->mt_subtype; stp->st_name; stp++) {
9628207b28aSchristos 				if (strcasecmp(stp->st_name, mi_subtype) == 0)
9638207b28aSchristos 					return stp->st_dispmode;
9648207b28aSchristos 			}
9658207b28aSchristos 			return stp->st_dispmode;
9668207b28aSchristos 		}
9678207b28aSchristos 	}
9688207b28aSchristos 	return mtp->mt_dispmode;
9698207b28aSchristos }
9708207b28aSchristos 
9718207b28aSchristos 
9728207b28aSchristos PUBLIC FILE *
mime_decode_body(struct mime_info * mip)9738207b28aSchristos mime_decode_body(struct mime_info *mip)
9748207b28aSchristos {
9758207b28aSchristos 	static enum dispmode_e dispmode;
9768207b28aSchristos 	mime_codec_t dec;
9778207b28aSchristos 	const char *cmd;
9788207b28aSchristos 
9798207b28aSchristos 	/* close anything left over from mime_decode_head() */
9808207b28aSchristos 	close_top_files(mip->mi_head_end);
9818207b28aSchristos 
9828207b28aSchristos 	/*
9838207b28aSchristos 	 * Make sure we flush everything down the pipe so children
9848207b28aSchristos 	 * don't see it.
9858207b28aSchristos 	 */
9868207b28aSchristos 	(void)fflush(pipe_end(mip));
9878207b28aSchristos 
988f3098750Schristos 	if (mip->mi_detachdir)	/* We are detaching!  Ignore the hooks. */
989f3098750Schristos 		return mime_detach_parts(mip);
990f3098750Schristos 
9918207b28aSchristos 	cmd = NULL;
9928207b28aSchristos 	if (mip->mi_command_hook == NULL)
9938207b28aSchristos 		cmd = get_command_hook(mip, "-body");
9948207b28aSchristos 
9958207b28aSchristos 	dec = mime_fio_decoder(mip->mi_encoding);
996f3098750Schristos 
9978207b28aSchristos 	/*
9988207b28aSchristos 	 * If there is a filter running, we need to send the message
9998207b28aSchristos 	 * to it.  Otherwise, get the default display mode for this body.
10008207b28aSchristos 	 */
10018207b28aSchristos 	dispmode = cmd || mip->mi_command_hook ? DM_DISPLAY : get_display_mode(mip, dec);
10028207b28aSchristos 
10038207b28aSchristos 	if (dec == NULL)	/* make sure we have a usable decoder */
10048207b28aSchristos 		dec = mime_fio_decoder(MIME_TRANSFER_7BIT);
10058207b28aSchristos 
10068207b28aSchristos 	if (dispmode == DM_DISPLAY) {
10078207b28aSchristos 		int flags;
10088207b28aSchristos 		if (cmd == NULL)
10098207b28aSchristos 			/* just get the flags */
10108207b28aSchristos 			flags = mime_run_command(mip->mi_command_hook, NULL);
10118207b28aSchristos 		else
10128207b28aSchristos 			flags = mime_run_command(cmd, pipe_end(mip));
10138207b28aSchristos 		if ((flags & CMD_FLAG_NO_DECODE) == 0)
10148207b28aSchristos 			run_decoder(mip, dec);
10158207b28aSchristos 		return pipe_end(mip);
10168207b28aSchristos 	}
10178207b28aSchristos 	else {
10188207b28aSchristos 		static const struct msg_tbl_s {
10198207b28aSchristos 			enum dispmode_e dm;
10208207b28aSchristos 			const char *msg;
10218207b28aSchristos 		} msg_tbl[] = {
10228207b28aSchristos 			{ DM_BINARY,	"binary content"	},
10238207b28aSchristos 			{ DM_PGPSIGN,	"OpenPGP signature"	},
10248207b28aSchristos 			{ DM_PGPENCR,	"OpenPGP encrypted"	},
10258207b28aSchristos 			{ DM_PGPKEYS,	"OpenPGP keys"		},
10268207b28aSchristos 			{ DM_UNKNOWN,	"unknown data"		},
10278207b28aSchristos 			{ DM_IGNORE,	NULL			},
1028c172e3b9Slukem 			{ DM_SENTINEL,	NULL			},
10298207b28aSchristos 		};
10308207b28aSchristos 		const struct msg_tbl_s *mp;
10318207b28aSchristos 
1032c172e3b9Slukem 		for (mp = msg_tbl; mp->dm != DM_SENTINEL; mp++)
10338207b28aSchristos 			if (mp->dm == dispmode)
10348207b28aSchristos 				break;
10358207b28aSchristos 
1036c172e3b9Slukem 		assert(mp->dm != DM_SENTINEL);	/* msg_tbl is short if this happens! */
10378207b28aSchristos 
10388207b28aSchristos 		if (mp->msg)
10398207b28aSchristos 			(void)fprintf(pipe_end(mip), "  [%s]\n\n", mp->msg);
10408207b28aSchristos 
10418207b28aSchristos 		return NULL;
10428207b28aSchristos 	}
10438207b28aSchristos }
10448207b28aSchristos 
10458207b28aSchristos 
10468207b28aSchristos /************************************************************************
10478207b28aSchristos  * Higher level header decoding interface.
10488207b28aSchristos  *
10498207b28aSchristos  * The core routines are in mime_header.c.
10508207b28aSchristos  */
10518207b28aSchristos 
1052a2fe0ba0Schristos /*
1053a2fe0ba0Schristos  * Decode a portion of the header field.
1054a2fe0ba0Schristos  *
1055a2fe0ba0Schristos  * linebuf	buffer to decode into.
1056a2fe0ba0Schristos  * bufsize	size of linebuf.
1057a2fe0ba0Schristos  * hdrline	full header line including header name.
1058a2fe0ba0Schristos  * srcstr	pointer to string to decode
1059a2fe0ba0Schristos  */
10608207b28aSchristos PUBLIC char *
mime_decode_hfield(char * linebuf,size_t bufsize,const char * hdrline,char * srcstr)1061a2fe0ba0Schristos mime_decode_hfield(char *linebuf, size_t bufsize, const char *hdrline, char *srcstr)
10628207b28aSchristos {
10638207b28aSchristos 	hfield_decoder_t decode;
1064a2fe0ba0Schristos 	decode = mime_hfield_decoder(hdrline);
10658207b28aSchristos 	if (decode) {
1066a2fe0ba0Schristos 		decode(linebuf, bufsize, srcstr);
10678207b28aSchristos 		return linebuf;
10688207b28aSchristos 	}
1069a2fe0ba0Schristos 	return srcstr;
10708207b28aSchristos }
10718207b28aSchristos 
10728207b28aSchristos /*
1073a2fe0ba0Schristos  * Return the next header field found in the input stream.
1074a2fe0ba0Schristos  * Return 0 if something found, -1 otherwise.
1075a2fe0ba0Schristos  * For a proper header, "*colon" is set to point to the colon
1076a2fe0ba0Schristos  * terminating the header name.  Otherwise it is NULL.
1077a2fe0ba0Schristos  *
1078a2fe0ba0Schristos  * NOTE: unlike gethfield() in support.c this:
1079a2fe0ba0Schristos  * 1) preserves folding (newlines),
1080a2fe0ba0Schristos  * 2) reads until fgetln() gets an EOF,
1081a2fe0ba0Schristos  * 3) only sets *colon if there is a "proper" one.
10828207b28aSchristos  */
10838207b28aSchristos static int
get_folded_hfield(FILE * f,char * linebuf,size_t bufsize,char ** colon)1084a2fe0ba0Schristos get_folded_hfield(FILE *f, char *linebuf, size_t bufsize, char **colon)
10858207b28aSchristos {
10868207b28aSchristos 	char *cp, *cp2;
10878207b28aSchristos 	char *line;
10888207b28aSchristos 	size_t len;
10898207b28aSchristos 
10908207b28aSchristos 	if ((cp = fgetln(f, &len)) == NULL)
10918207b28aSchristos 		return -1;
1092d727506fSchristos 	for (cp2 = cp;
1093a2fe0ba0Schristos 	     cp2 < cp + len && isprint((unsigned char)*cp2) &&
1094d727506fSchristos 		 !is_WSP(*cp2) && *cp2 != ':';
1095d727506fSchristos 	     cp2++)
10968207b28aSchristos 		continue;
10978207b28aSchristos 	len = MIN(bufsize - 1, len);
10988207b28aSchristos 	bufsize -= len;
10998207b28aSchristos 	(void)memcpy(linebuf, cp, len);
11008207b28aSchristos 	*colon = *cp2 == ':' ? linebuf + (cp2 - cp) : NULL;
11018207b28aSchristos 	line = linebuf + len;
1102a2fe0ba0Schristos 	for (;;) {
11038207b28aSchristos 		int c;
11048207b28aSchristos 		(void)ungetc(c = getc(f), f);
1105d727506fSchristos 		if (!is_WSP(c))
11068207b28aSchristos 			break;
11078207b28aSchristos 
11088207b28aSchristos 		if ((cp = fgetln(f, &len)) == NULL)
11098207b28aSchristos 			break;
11108207b28aSchristos 		len = MIN(bufsize - 1, len);
11118207b28aSchristos 		bufsize -= len;
11128207b28aSchristos 		if (len == 0)
11138207b28aSchristos 			break;
11148207b28aSchristos 		(void)memcpy(line, cp, len);
11158207b28aSchristos 		line += len;
11168207b28aSchristos 	}
11178207b28aSchristos 	*line = 0;
1118a2fe0ba0Schristos 	return 0;
11198207b28aSchristos }
11208207b28aSchristos 
11218207b28aSchristos static void
decode_header(FILE * fi,FILE * fo,void * cookie __unused)11228207b28aSchristos decode_header(FILE *fi, FILE *fo, void *cookie __unused)
11238207b28aSchristos {
11248207b28aSchristos 	char linebuf[LINESIZE];
11258207b28aSchristos 	char *colon;
11268207b28aSchristos #ifdef __lint__
11278207b28aSchristos 	cookie = cookie;
11288207b28aSchristos #endif
1129a2fe0ba0Schristos 	while (get_folded_hfield(fi, linebuf, sizeof(linebuf), &colon) >= 0) {
11308207b28aSchristos 		char decbuf[LINESIZE];
11318207b28aSchristos 		char *hdrstr;
11328207b28aSchristos 		hdrstr = linebuf;
11338207b28aSchristos 		if (colon)
1134a2fe0ba0Schristos 			hdrstr = mime_decode_hfield(decbuf, sizeof(decbuf), hdrstr, hdrstr);
113502bc8589Schristos 		(void)fputs(hdrstr, fo);
11368207b28aSchristos 	}
11378207b28aSchristos }
11388207b28aSchristos 
11398207b28aSchristos PUBLIC FILE *
mime_decode_header(struct mime_info * mip)11408207b28aSchristos mime_decode_header(struct mime_info *mip)
11418207b28aSchristos {
11428207b28aSchristos 	int flags;
11438207b28aSchristos 	const char *cmd;
11448207b28aSchristos 	FILE *fo;
11458207b28aSchristos 
11468207b28aSchristos 	fo = pipe_end(mip);
11478207b28aSchristos 
1148f3098750Schristos 	if (mip->mi_detachdir) { /* We are detaching.  Don't run anything! */
11498207b28aSchristos 		(void)fflush(fo);
1150f3098750Schristos 		return pipe_end(mip);
1151f3098750Schristos 	}
1152f3098750Schristos 
1153f3098750Schristos 	if (mip->mi_partnum)
1154f3098750Schristos 		(void)fprintf(fo, "----- Part %s -----\n", mip->mi_partstr);
1155f3098750Schristos 
1156f3098750Schristos 	(void)fflush(fo);	/* Flush so the childern don't see it. */
11578207b28aSchristos 
11588207b28aSchristos 	/*
11598207b28aSchristos 	 * install the message hook before the head hook.
11608207b28aSchristos 	 */
11618207b28aSchristos 	cmd = get_command_hook(mip, "-hook");
11628207b28aSchristos 	mip->mi_command_hook = cmd;
11638207b28aSchristos 	if (cmd) {
11648207b28aSchristos 		flags = mime_run_command(cmd, pipe_end(mip));
11658207b28aSchristos 		mip->mi_head_end = last_registered_file(0);
11668207b28aSchristos 	}
11678207b28aSchristos 	else {
11688207b28aSchristos 		cmd = get_command_hook(mip, "-head");
11698207b28aSchristos 		mip->mi_head_end = last_registered_file(0);
11708207b28aSchristos 		flags = mime_run_command(cmd, pipe_end(mip));
11718207b28aSchristos 	}
11728207b28aSchristos 
11738207b28aSchristos 	if (value(ENAME_MIME_DECODE_HDR) && (flags & CMD_FLAG_NO_DECODE) == 0)
11748207b28aSchristos 		mime_run_function(decode_header, pipe_end(mip), NULL);
11758207b28aSchristos 
11768207b28aSchristos 	return pipe_end(mip);
11778207b28aSchristos }
11788207b28aSchristos 
11798207b28aSchristos #endif /* MIME_SUPPORT */
1180