mime_header.c revision 1.1 1 /* $NetBSD: mime_header.c,v 1.1 2006/10/21 21:37:21 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Anon Ymous.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39
40 /*
41 * This module contains the core MIME header decoding routines.
42 * Please refer to RFC 2047 and RFC 2822.
43 */
44
45 #ifdef MIME_SUPPORT
46
47 #include <sys/cdefs.h>
48 #ifndef __lint__
49 __RCSID("$NetBSD: mime_header.c,v 1.1 2006/10/21 21:37:21 christos Exp $");
50 #endif /* not __lint__ */
51
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 #include "def.h"
57 #include "extern.h"
58 #include "mime.h"
59 #include "mime_header.h"
60 #include "mime_codecs.h"
61
62 /*
63 * Our interface to mime_b64tobin()
64 *
65 * XXX - This should move to mime_codecs.c.
66 */
67 static ssize_t
68 mime_B64_decode(char *outbuf, size_t outlen, const char *inbuf, size_t inlen)
69 {
70 if (outlen < 3 * roundup(inlen, 4) / 4)
71 return -1;
72
73 return mime_b64tobin(outbuf, inbuf, inlen);
74 }
75
76
77 /*
78 * Header specific "quoted-printable" decode!
79 * Differences with body QP decoding (see rfc 2047, sec 4.2):
80 * 1) '=' occurs _only_ when followed by two hex digits (FWS is not allowed).
81 * 2) Spaces can be encoded as '_' in headers for readability.
82 *
83 * XXX - This should move to mime_codecs.c.
84 */
85 static ssize_t
86 mime_QPh_decode(char *outbuf, size_t outlen, const char *inbuf, size_t inlen)
87 {
88 const char *p, *inend;
89 char *outend;
90 char *q;
91
92 outend = outbuf + outlen;
93 inend = inbuf + inlen;
94 q = outbuf;
95 for (p = inbuf; p < inend; p++) {
96 if (q >= outend)
97 return -1;
98 if (*p == '=') {
99 p++;
100 if (p + 1 < inend) {
101 int c;
102 char *bufend;
103 char buf[3];
104 buf[0] = *p++;
105 buf[1] = *p;
106 buf[2] = '\0';
107 c = strtol(buf, &bufend, 16);
108 if (bufend != &buf[2])
109 return -1;
110 *q++ = c;
111 }
112 else
113 return -1;
114 }
115 else if (*p == '_') /* header's may encode ' ' as '_' */
116 *q++ = ' ';
117 else
118 *q++ = *p;
119 }
120 return q - outbuf;
121 }
122
123 static const char *
124 grab_charset(char *from_cs, size_t from_cs_len, const char *p)
125 {
126 char *q;
127 q = from_cs;
128 for (/*EMPTY*/; *p != '?'; p++) {
129 if (*p == '\0' || q >= from_cs + from_cs_len - 1)
130 return NULL;
131 *q++ = *p;
132 }
133 *q = '\0';
134 return ++p; /* if here, then we got the '?' */
135 }
136
137 /*
138 * An encoded word is a string of at most 75 non-white space
139 * characters of the following form:
140 *
141 * =?charset?X?encoding?=
142 *
143 * where:
144 * 'charset' is the original character set of the unencoded string.
145 *
146 * 'X' is the encoding type 'B' or 'Q' for "base64" or
147 * "quoted-printable", respectively,
148 * 'encoding' is the encoded string.
149 *
150 * Both 'charset' and 'X' are case independent and 'encoding' cannot
151 * contain any whitespace or '?' characters. The 'encoding' must also
152 * be fully contained within the encoded words, i.e., it cannot be
153 * split between encoded words.
154 *
155 * Note: the 'B' encoding is a slightly modified "quoted-printable"
156 * encoding. In particular, spaces (' ') may be encoded as '_' to
157 * improve undecoded readability.
158 */
159 static int
160 decode_word(const char **ibuf, char **obuf, char *oend, const char *to_cs)
161 {
162 ssize_t declen;
163 size_t enclen, dstlen;
164 char decword[LINESIZE];
165 char from_cs[LINESIZE];
166 const char *encword, *iend, *p;
167 char *dstend;
168 char enctype;
169
170 p = *ibuf;
171 if (p[0] != '=' && p[1] != '?')
172 return -1;
173 if (strlen(p) < 2 + 1 + 3 + 1 + 2)
174 return -1;
175 p = grab_charset(from_cs, sizeof(from_cs), p + 2);
176 if (p == NULL)
177 return -1;
178 enctype = *p++;
179 if (*p++ != '?')
180 return -1;
181 encword = p;
182 p = strchr(p, '?');
183 if (p == NULL || p[1] != '=')
184 return -1;
185 enclen = p - encword; /* length of encoded substring */
186 iend = p + 2;
187 /* encoded words are at most 75 characters (RFC 2047, sec 2) */
188 if (iend > *ibuf + 75)
189 return -1;
190
191 dstend = to_cs ? decword : *obuf;
192 dstlen = (to_cs ? sizeof(decword): oend - *obuf) - 1;
193
194 if (enctype == 'B' || enctype == 'b')
195 declen = mime_B64_decode(dstend, dstlen, encword, enclen);
196 else if (enctype == 'Q' || enctype == 'q')
197 declen = mime_QPh_decode(dstend, dstlen, encword, enclen);
198 else
199 return -1;
200
201 if (declen == -1)
202 return -1;
203
204 dstend += declen;
205 #ifdef CHARSET_SUPPORT
206 if (to_cs != NULL) {
207 iconv_t cd;
208 const char *src;
209 size_t srclen;
210 size_t cnt;
211
212 cd = iconv_open(to_cs, from_cs);
213 if (cd == (iconv_t)-1)
214 return -1;
215
216 src = decword;
217 srclen = declen;
218 dstend = *obuf;
219 dstlen = oend - *obuf - 1;
220 cnt = mime_iconv(cd, &src, &srclen, &dstend, &dstlen);
221
222 (void)iconv_close(cd);
223 if (cnt == (size_t)-1)
224 return -1;
225 }
226 #endif /* CHARSET_SUPPORT */
227 *dstend = '\0';
228 *ibuf = iend;
229 *obuf = dstend;
230 return 0;
231 }
232
233
234 /*
235 * Folding White Space. See RFC 2822.
236 */
237 static inline int
238 is_FWS(int c)
239 {
240 return isblank(c) || c == '\n';
241 }
242
243 static inline const char *
244 skip_FWS(const char *p)
245 {
246 while (is_FWS((unsigned char)*p))
247 p++;
248 return p;
249 }
250
251 static inline void
252 copy_skipped_FWS(char **dst, char *dstend, const char **src, const char *srcend)
253 {
254 const char *p, *pend;
255 char *q, *qend;
256
257 p = *src;
258 q = *dst;
259 pend = srcend;
260 qend = dstend;
261
262 if (p) { /* copy any skipped linear-white-space */
263 while (p < pend && q < qend)
264 *q++ = *p++;
265 *dst = q;
266 *src = NULL;
267 }
268 }
269
270 /*
271 * Decode an unstructured field.
272 *
273 * See RFC 2822 Sec 2.2.1 and 3.6.5.
274 * Encoded words may occur anywhere in unstructured fields provided
275 * they are separated from any other text or encoded words by at least
276 * one linear-white-space character. (See RFC 2047 sec 5.1.) If two
277 * encoded words occur sequentially (separated by only FWS) then the
278 * separating FWS is removed.
279 *
280 * NOTE: unstructured fields cannot contain 'quoted-pairs' (see
281 * RFC2822 sec 3.2.6 and RFC 2047), but that is no problem as a '\\'
282 * (or any non-whitespace character) immediately before an
283 * encoded-word will prevent it from being decoded.
284 *
285 * hstring should be a NULL terminated string.
286 * outbuf should be sufficiently large to hold the result.
287 */
288 static void
289 mime_decode_usfield(char *outbuf, size_t outsize, const char *hstring)
290 {
291 const char *p, *p0;
292 char *q, *qend;
293 int lastc;
294 const char *charset;
295
296 charset = value(ENAME_MIME_CHARSET);
297 qend = outbuf + outsize - 1; /* Make sure there is room for the trailing NULL! */
298 q = outbuf;
299 p = hstring;
300 p0 = NULL;
301 lastc = (unsigned char)' ';
302 while (*p && q < qend) {
303 const char *p1;
304 char *q1;
305 if (is_FWS(lastc) && p[0] == '=' && p[1] == '?' &&
306 decode_word((p1 = p, &p1), (q1 = q, &q1), qend, charset) == 0 &&
307 (*p1 == '\0' || is_FWS((unsigned char)*p1))) {
308 p0 = p1; /* pointer to first character after encoded word */
309 q = q1;
310 p = skip_FWS(p1);
311 lastc = (unsigned char)*p0;
312 }
313 else {
314 copy_skipped_FWS(&q, qend, &p0, p);
315 lastc = (unsigned char)*p;
316 if (q < qend)
317 *q++ = *p++;
318 }
319 }
320 copy_skipped_FWS(&q, qend, &p0, p);
321 *q = '\0';
322 }
323
324 /*
325 * Decode a field comment.
326 *
327 * Comments only occur in structured fields, can be nested (rfc 2822,
328 * sec 3.2.3), and can contain 'encoded-words' and 'quoted-pairs'.
329 * Otherwise, they can be regarded as unstructured fields that are
330 * bounded by '(' and ')' characters.
331 */
332 static int
333 decode_comment(char **obuf, char *oend, const char **ibuf, const char *iend, const char *charset)
334 {
335 const char *p, *pend, *p0;
336 char *q, *qend;
337 int lastc;
338
339 p = *ibuf;
340 q = *obuf;
341 pend = iend;
342 qend = oend;
343 lastc = (unsigned char)' ';
344 p0 = NULL;
345 while (p < pend && q < qend) {
346 const char *p1;
347 char *q1;
348
349 if (is_FWS(lastc) && p[0] == '=' && p[1] == '?' &&
350 decode_word((p1 = p, &p1), (q1 = q, &q1), qend, charset) == 0 &&
351 (*p1 == ')' || is_FWS((unsigned char)*p1))) {
352 lastc = (unsigned char)*p1;
353 p0 = p1;
354 q = q1;
355 p = skip_FWS(p1);
356 /*
357 * XXX - this check should be unnecessary as *pend should
358 * be '\0' which will stop skip_FWS()
359 */
360 if (p > pend)
361 p = pend;
362 }
363 else {
364 copy_skipped_FWS(&q, qend, &p0, p);
365 if (q >= qend) /* XXX - q > qend cannot happen */
366 break;
367
368 if (*p == ')') {
369 *q++ = *p++; /* copy the closing ')' */
370 break; /* and get out of here! */
371 }
372
373 if (*p == '(') {
374 *q++ = *p++; /* copy the opening '(' */
375 if (decode_comment(&q, qend, &p, pend, charset) == -1)
376 return -1; /* is this right or should we update? */
377 lastc = ')';
378 }
379 else if (*p == '\\' && p + 1 < pend) { /* quoted-pair */
380 if (p[1] == '(' || p[1] == ')' || p[1] == '\\') /* need quoted-pair*/
381 *q++ = *p;
382 p++;
383 lastc = (unsigned char)*p;
384 if (q < qend)
385 *q++ = *p++;
386 }
387 else {
388 lastc = (unsigned char)*p;
389 *q++ = *p++;
390 }
391 }
392 }
393 *ibuf = p;
394 *obuf = q;
395 return 0;
396 }
397
398 /*
399 * Decode a quoted-string or no-fold-quote.
400 *
401 * These cannot contain encoded words. They can contain quoted-pairs,
402 * making '\\' special. They have no other structure. See RFC 2822
403 * sec 3.2.5 and 3.6.4.
404 */
405 static void
406 decode_quoted_string(char **obuf, char *oend, const char **ibuf, const char *iend)
407 {
408 const char *p, *pend;
409 char *q, *qend;
410
411 qend = oend;
412 pend = iend;
413 p = *ibuf;
414 q = *obuf;
415 while (p < pend && q < qend) {
416 if (*p == '"') {
417 *q++ = *p++; /* copy the closing '"' */
418 break;
419 }
420 if (*p == '\\' && p + 1 < pend) { /* quoted-pair */
421 if (p[1] == '"' || p[1] == '\\') {
422 *q++ = *p;
423 if (q >= qend)
424 break;
425 }
426 p++;
427 }
428 *q++ = *p++;
429 }
430 *ibuf = p;
431 *obuf = q;
432 }
433
434 /*
435 * Decode a domain-literal or no-fold-literal.
436 *
437 * These cannot contain encoded words. They can have quoted pairs and
438 * are delimited by '[' and ']' making '\\', '[', and ']' special.
439 * They have no other structure. See RFC 2822 sec 3.4.1 and 3.6.4.
440 */
441 static void
442 decode_domain_literal(char **obuf, char *oend, const char **ibuf, const char *iend)
443 {
444 const char *p, *pend;
445 char *q, *qend;
446
447 qend = oend;
448 pend = iend;
449 p = *ibuf;
450 q = *obuf;
451 while (p < pend && q < qend) {
452 if (*p == ']') {
453 *q++ = *p++; /* copy the closing ']' */
454 break;
455 }
456 if (*p == '\\' && p + 1 < pend) { /* quoted-pair */
457 if (p[1] == '[' || p[1] == ']' || p[1] == '\\') {
458 *q++ = *p;
459 if (q >= qend)
460 break;
461 }
462 p++;
463 }
464 *q++ = *p++;
465 }
466 *ibuf = p;
467 *obuf = q;
468 }
469
470 /*
471 * Specials: see RFC 2822 sec 3.2.1.
472 */
473 static inline int
474 is_specials(int c)
475 {
476 static const char specialtab[] = {
477 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
478 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
479 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0,
480 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0,
481
482 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
483 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,
484 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
485 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
486 };
487 return specialtab[(c & 0x7f)];
488 }
489
490 /*
491 * Decode a structured field.
492 *
493 * At the top level, structured fields can only contain encoded-words
494 * via 'phrases' and 'comments'. See RFC 2047 sec 5.
495 */
496 static void
497 mime_decode_sfield(char *linebuf, size_t bufsize, const char *hstring)
498 {
499 const char *p, *pend, *p0;
500 char *q, *qend;
501 const char *charset;
502 int lastc;
503
504 charset = value(ENAME_MIME_CHARSET);
505
506 p = hstring;
507 q = linebuf;
508 pend = hstring + strlen(hstring);
509 qend = linebuf + bufsize - 1; /* save room for the NULL terminator */
510 lastc = (unsigned char)' ';
511 p0 = NULL;
512 while (p < pend && q < qend) {
513 const char *p1;
514 char *q1;
515
516 if (*p != '=') {
517 copy_skipped_FWS(&q, qend, &p0, p);
518 if (q >= qend)
519 break;
520 }
521
522 switch (*p) {
523 case '(': /* start of comment */
524 *q++ = *p++; /* copy the opening '(' */
525 (void)decode_comment(&q, qend, &p, pend, charset);
526 lastc = (unsigned char)p[-1];
527 break;
528
529 case '"': /* start of quoted-string or no-fold-quote */
530 *q++ = *p++; /* copy the opening '"' */
531 decode_quoted_string(&q, qend, &p, pend);
532 lastc = (unsigned char)p[-1];
533 break;
534
535 case '[': /* start of domain-literal or no-fold-literal */
536 *q++ = *p++; /* copy the opening '[' */
537 decode_domain_literal(&q, qend, &p, pend);
538 lastc = (unsigned char)p[-1];
539 break;
540
541 case '\\': /* start of quoted-pair */
542 if (p + 1 < pend) { /* quoted pair */
543 if (is_specials(p[1])) {
544 *q++ = *p;
545 if (q >= qend)
546 break;
547 }
548 p++; /* skip the '\\' */
549 }
550 goto copy_char;
551
552 case '=':
553 /*
554 * At this level encoded words can appear via
555 * 'phrases' (possibly delimited by ',' as in
556 * 'keywords'). Thus we handle them as such.
557 * Hopefully this is sufficient.
558 */
559 if ((lastc == ',' || is_FWS(lastc)) && p[1] == '?' &&
560 decode_word((p1 = p, &p1), (q1 = q, &q1), qend, charset) == 0 &&
561 (*p1 == '\0' || *p1 == ',' || is_FWS((unsigned char)*p1))) {
562 lastc = (unsigned char)*p1;
563 p0 = p1;
564 q = q1;
565 p = skip_FWS(p1);
566 /*
567 * XXX - this check should be
568 * unnecessary as *pend should be '\0'
569 * which will stop skip_FWS()
570 */
571 if (p > pend)
572 p = pend;
573 break;
574 }
575 else {
576 copy_skipped_FWS(&q, qend, &p0, p);
577 if (q >= qend)
578 break;
579 goto copy_char;
580 }
581
582 case '<': /* start of angle-addr, msg-id, or path. */
583 /*
584 * A msg-id cannot contain encoded-pairs or
585 * encoded-words, but angle-addr and path can.
586 * Distinguishing between them seems to be
587 * unnecessary, so let's be loose and just
588 * decode them as if they were all the same.
589 */
590 default:
591 copy_char:
592 lastc = (unsigned char)*p;
593 *q++ = *p++;
594 break;
595 }
596 }
597 copy_skipped_FWS(&q, qend, &p0, p);
598 *q = '\0'; /* null terminate the result! */
599 }
600
601
602 /*
603 * Returns the correct hfield decoder, or NULL if none.
604 * Info extracted from RFC 2822.
605 */
606 PUBLIC hfield_decoder_t
607 mime_hfield_decoder(char *name)
608 {
609 static const struct field_decoder_tbl_s {
610 const char *field_name;
611 hfield_decoder_t decoder;
612 } field_decoder_tbl[] = {
613 { "Received:", NULL },
614 { "Content-Type:", NULL },
615 { "Content-Disposition:", NULL },
616 { "Content-Transfer-Encoding:", NULL },
617 { "Content-Description:", mime_decode_sfield },
618 { "Content-ID:", mime_decode_sfield },
619 { "MIME-Version:", mime_decode_sfield },
620 { "Bcc:", mime_decode_sfield },
621 { "Cc:", mime_decode_sfield },
622 { "Date:", mime_decode_sfield },
623 { "From:", mime_decode_sfield },
624 { "In-Reply-To:", mime_decode_sfield },
625 { "Keywords:", mime_decode_sfield },
626 { "Message-ID:", mime_decode_sfield },
627 { "References:", mime_decode_sfield },
628 { "Reply-To:", mime_decode_sfield },
629 { "Return-Path:", mime_decode_sfield },
630 { "Sender:", mime_decode_sfield },
631 { "To:", mime_decode_sfield },
632 { "Subject:", mime_decode_usfield },
633 { "Comments:", mime_decode_usfield },
634 { "X-", mime_decode_usfield },
635 { NULL, mime_decode_usfield }, /* optional-fields */
636 };
637 const struct field_decoder_tbl_s *fp;
638
639 /* XXX - this begs for a hash table! */
640 for (fp = field_decoder_tbl; fp->field_name; fp++)
641 if (strncasecmp(name, fp->field_name, strlen(fp->field_name)) == 0)
642 return fp->decoder;
643 return fp->decoder;
644 }
645
646 #endif /* MIME_SUPPORT */
647