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