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