Obsolete -E and -B (-> STDOUT always LBF, STDIN with -#)
[s-mailx.git] / mime_parse.c
blob0a402137ad0c4cbf3c99f255958299002cd384e5
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Parse a message into a tree of struct mimepart objects.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE mime_parse
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 /* Fetch plain */
43 static char * _mime_parse_ct_plain_from_ct(char const *cth);
45 static bool_t _mime_parse_part(struct message *zmp, struct mimepart *ip,
46 enum mime_parse_flags mpf, int level);
48 static void _mime_parse_rfc822(struct message *zmp, struct mimepart *ip,
49 enum mime_parse_flags mpf, int level);
51 #ifdef HAVE_SSL
52 static void _mime_parse_pkcs7(struct message *zmp, struct mimepart *ip,
53 enum mime_parse_flags mpf, int level);
54 #endif
56 static bool_t _mime_parse_multipart(struct message *zmp,
57 struct mimepart *ip, enum mime_parse_flags mpf, int level);
58 static void __mime_parse_new(struct mimepart *ip, struct mimepart **np,
59 off_t offs, int *part);
60 static void __mime_parse_end(struct mimepart **np, off_t xoffs,
61 long lines);
63 static char *
64 _mime_parse_ct_plain_from_ct(char const *cth)
66 char *rv_b, *rv;
67 NYD2_ENTER;
69 rv_b = savestr(cth);
71 if ((rv = strchr(rv_b, ';')) != NULL)
72 *rv = '\0';
74 rv = rv_b + strlen(rv_b);
75 while (rv > rv_b && blankchar(rv[-1]))
76 --rv;
77 *rv = '\0';
78 NYD2_LEAVE;
79 return rv_b;
82 static bool_t
83 _mime_parse_part(struct message *zmp, struct mimepart *ip,
84 enum mime_parse_flags mpf, int level)
86 char *cp;
87 bool_t rv = FAL0;
88 NYD_ENTER;
90 ip->m_ct_type = hfield1("content-type", (struct message*)ip);
91 if (ip->m_ct_type != NULL)
92 ip->m_ct_type_plain = _mime_parse_ct_plain_from_ct(ip->m_ct_type);
93 else if (ip->m_parent != NULL && ip->m_parent->m_mimecontent == MIME_DIGEST)
94 ip->m_ct_type_plain = "message/rfc822";
95 else
96 ip->m_ct_type_plain = "text/plain";
97 ip->m_ct_type_usr_ovwr = NULL;
99 if (ip->m_ct_type != NULL)
100 ip->m_charset = mime_param_get("charset", ip->m_ct_type);
101 if (ip->m_charset == NULL)
102 ip->m_charset = charset_get_7bit();
104 if ((ip->m_ct_enc = hfield1("content-transfer-encoding",
105 (struct message*)ip)) == NULL)
106 ip->m_ct_enc = mime_enc_from_conversion(CONV_7BIT);
107 ip->m_mime_enc = mime_enc_from_ctehead(ip->m_ct_enc);
109 if (((cp = hfield1("content-disposition", (struct message*)ip)) == NULL ||
110 (ip->m_filename = mime_param_get("filename", cp)) == NULL) &&
111 ip->m_ct_type != NULL)
112 ip->m_filename = mime_param_get("name", ip->m_ct_type);
114 if ((ip->m_mimecontent = mime_type_classify_part(ip)) == MIME_822) {
115 /* TODO (v15) HACK: message/rfc822 is treated special, that this one is
116 * TODO too stupid to apply content-decoding when (falsely) applied */
117 if (ip->m_mime_enc != MIMEE_8B && ip->m_mime_enc != MIMEE_7B) {
118 n_err(_("Pre-v15 %s cannot handle (falsely) encoded message/rfc822\n"
119 " (not 7bit or 8bit)! Interpreting as text/plain!\n"),
120 uagent);
121 ip->m_mimecontent = MIME_TEXT_PLAIN;
125 if (mpf & MIME_PARSE_PARTS) {
126 if (level > 9999) { /* TODO MAGIC */
127 n_err(_("MIME content too deeply nested\n"));
128 goto jleave;
130 switch (ip->m_mimecontent) {
131 case MIME_PKCS7:
132 if (mpf & MIME_PARSE_DECRYPT) {
133 #ifdef HAVE_SSL
134 _mime_parse_pkcs7(zmp, ip, mpf, level);
135 if (ip->m_content_info & CI_ENCRYPTED_OK)
136 ip->m_content_info |= CI_EXPANDED;
137 break;
138 #else
139 n_err(_("No SSL / S/MIME support compiled in\n"));
140 goto jleave;
141 #endif
143 break;
144 default:
145 break;
146 case MIME_ALTERNATIVE:
147 case MIME_RELATED: /* TODO /related yet handled like /alternative */
148 case MIME_DIGEST:
149 case MIME_SIGNED:
150 case MIME_ENCRYPTED:
151 case MIME_MULTI:
152 if (!_mime_parse_multipart(zmp, ip, mpf, level))
153 goto jleave;
154 break;
155 case MIME_822:
156 _mime_parse_rfc822(zmp, ip, mpf, level);
157 break;
161 rv = TRU1;
162 jleave:
163 NYD_LEAVE;
164 return rv;
167 static void
168 _mime_parse_rfc822(struct message *zmp, struct mimepart *ip,
169 enum mime_parse_flags mpf, int level)
171 int c, lastc = '\n';
172 size_t cnt;
173 FILE *ibuf;
174 off_t offs;
175 struct mimepart *np;
176 long lines;
177 NYD_ENTER;
179 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL)
180 goto jleave;
182 cnt = ip->m_size;
183 lines = ip->m_lines;
184 while (cnt && ((c = getc(ibuf)) != EOF)) {
185 --cnt;
186 if (c == '\n') {
187 --lines;
188 if (lastc == '\n')
189 break;
191 lastc = c;
193 offs = ftell(ibuf);
195 np = csalloc(1, sizeof *np);
196 np->m_flag = MNOFROM;
197 np->m_content_info = CI_HAVE_HEADER | CI_HAVE_BODY;
198 np->m_block = mailx_blockof(offs);
199 np->m_offset = mailx_offsetof(offs);
200 np->m_size = np->m_xsize = cnt;
201 np->m_lines = np->m_xlines = lines;
202 np->m_partstring = ip->m_partstring;
203 np->m_parent = ip;
204 ip->m_multipart = np;
206 if (!(mpf & MIME_PARSE_SHALLOW) && ok_blook(rfc822_body_from_)) {
207 substdate((struct message*)np);
208 np->m_from = fakefrom((struct message*)np);/* TODO strip MNOFROM flag? */
211 _mime_parse_part(zmp, np, mpf, level + 1);
212 jleave:
213 NYD_LEAVE;
216 #ifdef HAVE_SSL
217 static void
218 _mime_parse_pkcs7(struct message *zmp, struct mimepart *ip,
219 enum mime_parse_flags mpf, int level)
221 struct message m, *xmp;
222 struct mimepart *np;
223 char *to, *cc;
224 NYD_ENTER;
226 memcpy(&m, ip, sizeof m);
227 to = hfield1("to", zmp);
228 cc = hfield1("cc", zmp);
230 if ((xmp = smime_decrypt(&m, to, cc, 0)) != NULL) {
231 np = csalloc(1, sizeof *np);
232 np->m_flag = xmp->m_flag;
233 np->m_content_info = xmp->m_content_info | CI_ENCRYPTED | CI_ENCRYPTED_OK;
234 np->m_block = xmp->m_block;
235 np->m_offset = xmp->m_offset;
236 np->m_size = xmp->m_size;
237 np->m_xsize = xmp->m_xsize;
238 np->m_lines = xmp->m_lines;
239 np->m_xlines = xmp->m_xlines;
241 /* TODO using part "1" for decrypted content is a hack */
242 if ((np->m_partstring = ip->m_partstring) == NULL)
243 ip->m_partstring = np->m_partstring = n_UNCONST("1");
245 if (_mime_parse_part(zmp, np, mpf, level + 1) == OKAY) {
246 ip->m_content_info |= CI_ENCRYPTED | CI_ENCRYPTED_OK;
247 np->m_parent = ip;
248 ip->m_multipart = np;
250 } else
251 ip->m_content_info |= CI_ENCRYPTED | CI_ENCRYPTED_BAD;
252 NYD_LEAVE;
254 #endif /* HAVE_SSL */
256 static bool_t
257 _mime_parse_multipart(struct message *zmp, struct mimepart *ip,
258 enum mime_parse_flags mpf, int level)
260 struct mimepart *np = NULL;
261 char *boundary, *line = NULL;
262 size_t linesize = 0, linelen, cnt, boundlen;
263 FILE *ibuf;
264 off_t offs;
265 int part = 0;
266 long lines = 0;
267 NYD_ENTER;
269 if ((boundary = mime_param_boundary_get(ip->m_ct_type, &linelen)) == NULL)
270 goto jleave;
272 boundlen = linelen;
273 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL)
274 goto jleave;
276 cnt = ip->m_size;
277 while (fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0))
278 if (line[0] == '\n')
279 break;
280 offs = ftell(ibuf);
282 /* TODO using part "1" for decrypted content is a hack */
283 if (ip->m_partstring == NULL)
284 ip->m_partstring = n_UNCONST("1");
285 __mime_parse_new(ip, &np, offs, NULL);
287 while (fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
288 /* XXX linelen includes LF */
289 if (!((lines > 0 || part == 0) && linelen > boundlen &&
290 !strncmp(line, boundary, boundlen))) {
291 ++lines;
292 continue;
295 /* Subpart boundary? */
296 if (line[boundlen] == '\n') {
297 offs = ftell(ibuf);
298 if (part > 0) {
299 __mime_parse_end(&np, offs - boundlen - 2, lines);
300 __mime_parse_new(ip, &np, offs - boundlen - 2, NULL);
302 __mime_parse_end(&np, offs, 2);
303 __mime_parse_new(ip, &np, offs, &part);
304 lines = 0;
305 continue;
308 /* Final boundary? Be aware of cases where there is no separating
309 * newline in between boundaries, as has been seen in a message with
310 * "Content-Type: multipart/appledouble;" */
311 if (linelen < boundlen + 2)
312 continue;
313 linelen -= boundlen + 2;
314 if (line[boundlen] != '-' || line[boundlen + 1] != '-' ||
315 (linelen > 0 && line[boundlen + 2] != '\n'))
316 continue;
317 offs = ftell(ibuf);
318 if (part != 0) {
319 __mime_parse_end(&np, offs - boundlen - 4, lines);
320 __mime_parse_new(ip, &np, offs - boundlen - 4, NULL);
322 __mime_parse_end(&np, offs + cnt, 2);
323 break;
325 if (np) {
326 offs = ftell(ibuf);
327 __mime_parse_end(&np, offs, lines);
330 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart)
331 if (np->m_mimecontent != MIME_DISCARD)
332 _mime_parse_part(zmp, np, mpf, level + 1);
334 jleave:
335 if (line != NULL)
336 free(line);
337 NYD_LEAVE;
338 return (ip != NULL);
341 static void
342 __mime_parse_new(struct mimepart *ip, struct mimepart **np, off_t offs,
343 int *part)
345 struct mimepart *pp;
346 size_t sz;
347 NYD_ENTER;
349 *np = csalloc(1, sizeof **np);
350 (*np)->m_flag = MNOFROM;
351 (*np)->m_content_info = CI_HAVE_HEADER | CI_HAVE_BODY;
352 (*np)->m_block = mailx_blockof(offs);
353 (*np)->m_offset = mailx_offsetof(offs);
355 if (part) {
356 ++(*part);
357 sz = (ip->m_partstring != NULL) ? strlen(ip->m_partstring) : 0;
358 sz += 20;
359 (*np)->m_partstring = salloc(sz);
360 if (ip->m_partstring)
361 snprintf((*np)->m_partstring, sz, "%s.%u", ip->m_partstring, *part);
362 else
363 snprintf((*np)->m_partstring, sz, "%u", *part);
364 } else
365 (*np)->m_mimecontent = MIME_DISCARD;
366 (*np)->m_parent = ip;
368 if (ip->m_multipart) {
369 for (pp = ip->m_multipart; pp->m_nextpart != NULL; pp = pp->m_nextpart)
371 pp->m_nextpart = *np;
372 } else
373 ip->m_multipart = *np;
374 NYD_LEAVE;
377 static void
378 __mime_parse_end(struct mimepart **np, off_t xoffs, long lines)
380 off_t offs;
381 NYD_ENTER;
383 offs = mailx_positionof((*np)->m_block, (*np)->m_offset);
384 (*np)->m_size = (*np)->m_xsize = xoffs - offs;
385 (*np)->m_lines = (*np)->m_xlines = lines;
386 *np = NULL;
387 NYD_LEAVE;
390 FL struct mimepart *
391 mime_parse_msg(struct message *mp, enum mime_parse_flags mpf)
393 struct mimepart *ip;
394 NYD_ENTER;
396 ip = csalloc(1, sizeof *ip);
397 ip->m_flag = mp->m_flag;
398 ip->m_content_info = mp->m_content_info;
399 ip->m_block = mp->m_block;
400 ip->m_offset = mp->m_offset;
401 ip->m_size = mp->m_size;
402 ip->m_xsize = mp->m_xsize;
403 ip->m_lines = mp->m_lines;
404 ip->m_xlines = mp->m_lines;
405 if (!_mime_parse_part(mp, ip, mpf, 0))
406 ip = NULL;
407 NYD_LEAVE;
408 return ip;
411 /* s-it-mode */