Tweak previous, it added a bad memory access
[s-mailx.git] / mime-parse.c
blob50ae6a65da29518b428e189ffa0b5f5d4ea4381d
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 - 2017 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 = ok_vlook(charset_7bit);
103 else
104 ip->m_charset = n_charsetalias_expand(ip->m_charset);
106 if ((ip->m_ct_enc = hfield1("content-transfer-encoding",
107 (struct message*)ip)) == NULL)
108 ip->m_ct_enc = mime_enc_from_conversion(CONV_7BIT);
109 ip->m_mime_enc = mime_enc_from_ctehead(ip->m_ct_enc);
111 if (((cp = hfield1("content-disposition", (struct message*)ip)) == NULL ||
112 (ip->m_filename = mime_param_get("filename", cp)) == NULL) &&
113 ip->m_ct_type != NULL)
114 ip->m_filename = mime_param_get("name", ip->m_ct_type);
116 if ((cp = hfield1("content-description", (struct message*)ip)) != NULL)
117 ip->m_content_description = cp;
119 if ((ip->m_mimecontent = mime_type_classify_part(ip)) == MIME_822) {
120 /* TODO (v15) HACK: message/rfc822 is treated special, that this one is
121 * TODO too stupid to apply content-decoding when (falsely) applied */
122 if (ip->m_mime_enc != MIMEE_8B && ip->m_mime_enc != MIMEE_7B) {
123 n_err(_("Pre-v15 %s cannot handle (falsely) encoded message/rfc822\n"
124 " (not 7bit or 8bit)! Interpreting as text/plain!\n"),
125 n_uagent);
126 ip->m_mimecontent = MIME_TEXT_PLAIN;
130 if (mpf & MIME_PARSE_PARTS) {
131 if (level > 9999) { /* TODO MAGIC */
132 n_err(_("MIME content too deeply nested\n"));
133 goto jleave;
135 switch (ip->m_mimecontent) {
136 case MIME_PKCS7:
137 if (mpf & MIME_PARSE_DECRYPT) {
138 #ifdef HAVE_SSL
139 _mime_parse_pkcs7(zmp, ip, mpf, level);
140 if (ip->m_content_info & CI_ENCRYPTED_OK)
141 ip->m_content_info |= CI_EXPANDED;
142 break;
143 #else
144 n_err(_("No SSL / S/MIME support compiled in\n"));
145 goto jleave;
146 #endif
148 break;
149 default:
150 break;
151 case MIME_ALTERNATIVE:
152 case MIME_RELATED: /* TODO /related yet handled like /alternative */
153 case MIME_DIGEST:
154 case MIME_SIGNED:
155 case MIME_ENCRYPTED:
156 case MIME_MULTI:
157 if (!_mime_parse_multipart(zmp, ip, mpf, level))
158 goto jleave;
159 break;
160 case MIME_822:
161 _mime_parse_rfc822(zmp, ip, mpf, level);
162 break;
166 rv = TRU1;
167 jleave:
168 NYD_LEAVE;
169 return rv;
172 static void
173 _mime_parse_rfc822(struct message *zmp, struct mimepart *ip,
174 enum mime_parse_flags mpf, int level)
176 int c, lastc = '\n';
177 size_t cnt;
178 FILE *ibuf;
179 off_t offs;
180 struct mimepart *np;
181 long lines;
182 NYD_ENTER;
184 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL)
185 goto jleave;
187 cnt = ip->m_size;
188 lines = ip->m_lines;
189 while (cnt && ((c = getc(ibuf)) != EOF)) {
190 --cnt;
191 if (c == '\n') {
192 --lines;
193 if (lastc == '\n')
194 break;
196 lastc = c;
198 offs = ftell(ibuf);
200 np = csalloc(1, sizeof *np);
201 np->m_flag = MNOFROM;
202 np->m_content_info = CI_HAVE_HEADER | CI_HAVE_BODY;
203 np->m_block = mailx_blockof(offs);
204 np->m_offset = mailx_offsetof(offs);
205 np->m_size = np->m_xsize = cnt;
206 np->m_lines = np->m_xlines = lines;
207 np->m_partstring = ip->m_partstring;
208 np->m_parent = ip;
209 ip->m_multipart = np;
211 if (!(mpf & MIME_PARSE_SHALLOW) && ok_blook(rfc822_body_from_)) {
212 substdate((struct message*)np);
213 np->m_from = fakefrom((struct message*)np);/* TODO strip MNOFROM flag? */
216 _mime_parse_part(zmp, np, mpf, level + 1);
217 jleave:
218 NYD_LEAVE;
221 #ifdef HAVE_SSL
222 static void
223 _mime_parse_pkcs7(struct message *zmp, struct mimepart *ip,
224 enum mime_parse_flags mpf, int level)
226 struct message m, *xmp;
227 struct mimepart *np;
228 char *to, *cc;
229 NYD_ENTER;
231 memcpy(&m, ip, sizeof m);
232 to = hfield1("to", zmp);
233 cc = hfield1("cc", zmp);
235 if ((xmp = smime_decrypt(&m, to, cc, 0)) != NULL) {
236 np = csalloc(1, sizeof *np);
237 np->m_flag = xmp->m_flag;
238 np->m_content_info = xmp->m_content_info | CI_ENCRYPTED | CI_ENCRYPTED_OK;
239 np->m_block = xmp->m_block;
240 np->m_offset = xmp->m_offset;
241 np->m_size = xmp->m_size;
242 np->m_xsize = xmp->m_xsize;
243 np->m_lines = xmp->m_lines;
244 np->m_xlines = xmp->m_xlines;
246 /* TODO using part "1" for decrypted content is a hack */
247 if ((np->m_partstring = ip->m_partstring) == NULL)
248 ip->m_partstring = np->m_partstring = n_UNCONST(n_1);
250 if (_mime_parse_part(zmp, np, mpf, level + 1) == OKAY) {
251 ip->m_content_info |= CI_ENCRYPTED | CI_ENCRYPTED_OK;
252 np->m_parent = ip;
253 ip->m_multipart = np;
255 } else
256 ip->m_content_info |= CI_ENCRYPTED | CI_ENCRYPTED_BAD;
257 NYD_LEAVE;
259 #endif /* HAVE_SSL */
261 static bool_t
262 _mime_parse_multipart(struct message *zmp, struct mimepart *ip,
263 enum mime_parse_flags mpf, int level)
265 struct mimepart *np = NULL;
266 char *boundary, *line = NULL;
267 size_t linesize = 0, linelen, cnt, boundlen;
268 FILE *ibuf;
269 off_t offs;
270 int part = 0;
271 long lines = 0;
272 NYD_ENTER;
274 if ((boundary = mime_param_boundary_get(ip->m_ct_type, &linelen)) == NULL)
275 goto jleave;
277 boundlen = linelen;
278 if ((ibuf = setinput(&mb, (struct message*)ip, NEED_BODY)) == NULL)
279 goto jleave;
281 cnt = ip->m_size;
282 while (fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0))
283 if (line[0] == '\n')
284 break;
285 offs = ftell(ibuf);
287 /* TODO using part "1" for decrypted content is a hack */
288 if (ip->m_partstring == NULL)
289 ip->m_partstring = n_UNCONST("1");
290 __mime_parse_new(ip, &np, offs, NULL);
292 while (fgetline(&line, &linesize, &cnt, &linelen, ibuf, 0)) {
293 /* XXX linelen includes LF */
294 if (!((lines > 0 || part == 0) && linelen > boundlen &&
295 !strncmp(line, boundary, boundlen))) {
296 ++lines;
297 continue;
300 /* Subpart boundary? */
301 if (line[boundlen] == '\n') {
302 offs = ftell(ibuf);
303 if (part > 0) {
304 __mime_parse_end(&np, offs - boundlen - 2, lines);
305 __mime_parse_new(ip, &np, offs - boundlen - 2, NULL);
307 __mime_parse_end(&np, offs, 2);
308 __mime_parse_new(ip, &np, offs, &part);
309 lines = 0;
310 continue;
313 /* Final boundary? Be aware of cases where there is no separating
314 * newline in between boundaries, as has been seen in a message with
315 * "Content-Type: multipart/appledouble;" */
316 if (linelen < boundlen + 2)
317 continue;
318 linelen -= boundlen + 2;
319 if (line[boundlen] != '-' || line[boundlen + 1] != '-' ||
320 (linelen > 0 && line[boundlen + 2] != '\n'))
321 continue;
322 offs = ftell(ibuf);
323 if (part != 0) {
324 __mime_parse_end(&np, offs - boundlen - 4, lines);
325 __mime_parse_new(ip, &np, offs - boundlen - 4, NULL);
327 __mime_parse_end(&np, offs + cnt, 2);
328 break;
330 if (np) {
331 offs = ftell(ibuf);
332 __mime_parse_end(&np, offs, lines);
335 for (np = ip->m_multipart; np != NULL; np = np->m_nextpart)
336 if (np->m_mimecontent != MIME_DISCARD)
337 _mime_parse_part(zmp, np, mpf, level + 1);
339 jleave:
340 if (line != NULL)
341 free(line);
342 NYD_LEAVE;
343 return (ip != NULL);
346 static void
347 __mime_parse_new(struct mimepart *ip, struct mimepart **np, off_t offs,
348 int *part)
350 struct mimepart *pp;
351 size_t sz;
352 NYD_ENTER;
354 *np = csalloc(1, sizeof **np);
355 (*np)->m_flag = MNOFROM;
356 (*np)->m_content_info = CI_HAVE_HEADER | CI_HAVE_BODY;
357 (*np)->m_block = mailx_blockof(offs);
358 (*np)->m_offset = mailx_offsetof(offs);
360 if (part) {
361 ++(*part);
362 sz = (ip->m_partstring != NULL) ? strlen(ip->m_partstring) : 0;
363 sz += 20;
364 (*np)->m_partstring = salloc(sz);
365 if (ip->m_partstring)
366 snprintf((*np)->m_partstring, sz, "%s.%u", ip->m_partstring, *part);
367 else
368 snprintf((*np)->m_partstring, sz, "%u", *part);
369 } else
370 (*np)->m_mimecontent = MIME_DISCARD;
371 (*np)->m_parent = ip;
373 if (ip->m_multipart) {
374 for (pp = ip->m_multipart; pp->m_nextpart != NULL; pp = pp->m_nextpart)
376 pp->m_nextpart = *np;
377 } else
378 ip->m_multipart = *np;
379 NYD_LEAVE;
382 static void
383 __mime_parse_end(struct mimepart **np, off_t xoffs, long lines)
385 off_t offs;
386 NYD_ENTER;
388 offs = mailx_positionof((*np)->m_block, (*np)->m_offset);
389 (*np)->m_size = (*np)->m_xsize = xoffs - offs;
390 (*np)->m_lines = (*np)->m_xlines = lines;
391 *np = NULL;
392 NYD_LEAVE;
395 FL struct mimepart *
396 mime_parse_msg(struct message *mp, enum mime_parse_flags mpf)
398 struct mimepart *ip;
399 NYD_ENTER;
401 ip = csalloc(1, sizeof *ip);
402 ip->m_flag = mp->m_flag;
403 ip->m_content_info = mp->m_content_info;
404 ip->m_block = mp->m_block;
405 ip->m_offset = mp->m_offset;
406 ip->m_size = mp->m_size;
407 ip->m_xsize = mp->m_xsize;
408 ip->m_lines = mp->m_lines;
409 ip->m_xlines = mp->m_lines;
410 if (!_mime_parse_part(mp, ip, mpf, 0))
411 ip = NULL;
412 NYD_LEAVE;
413 return ip;
416 /* s-it-mode */