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