4 * This is the MIME parser for Citadel.
6 * Copyright (c) 1998-2006 by Art Cancro
7 * This code is distributed under the GNU General Public License v2.
15 #include <sys/types.h>
23 #include "serv_extensions.h"
24 #include "sysdep_decls.h"
27 #include "mime_parser.h"
30 void extract_key(char *target, char *source, char *key)
34 strcpy(target, source);
35 for (a = 0; a < strlen(target); ++a) {
36 if ((!strncasecmp(&target[a], key, strlen(key)))
37 && (target[a + strlen(key)] == '=')) {
38 strcpy(target, &target[a + strlen(key) + 1]);
40 strcpy(target, &target[1]);
41 for (b = 0; b < strlen(target); ++b)
52 * For non-multipart messages, we need to generate a quickie partnum of "1"
53 * to return to callback functions. Some callbacks demand it.
55 char *fixed_partnum(char *supplied_partnum) {
56 if (supplied_partnum == NULL) return "1";
57 if (strlen(supplied_partnum)==0) return "1";
58 return supplied_partnum;
64 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
65 * according to RFC2045 section 6.7
67 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
69 int decoded_length = 0;
72 while (pos < sourcelen)
74 if (!strncmp(&encoded[pos], "=\r\n", 3))
78 else if (!strncmp(&encoded[pos], "=\n", 2))
82 else if (encoded[pos] == '=')
85 sscanf(&encoded[pos+1], "%02x", &ch);
87 decoded[decoded_length++] = ch;
91 decoded[decoded_length++] = encoded[pos];
95 decoded[decoded_length] = 0;
96 return(decoded_length);
101 * Given a message or message-part body and a length, handle any necessary
102 * decoding and pass the request up the stack.
104 void mime_decode(char *partnum,
105 char *part_start, size_t length,
106 char *content_type, char *charset, char *encoding,
108 char *name, char *filename,
120 void (*PreMultiPartCallBack)
131 void (*PostMultiPartCallBack)
148 size_t bytes_decoded = 0;
150 /* Some encodings aren't really encodings */
151 if (!strcasecmp(encoding, "7bit"))
152 strcpy(encoding, "");
153 if (!strcasecmp(encoding, "8bit"))
154 strcpy(encoding, "");
155 if (!strcasecmp(encoding, "binary"))
156 strcpy(encoding, "");
158 /* If this part is not encoded, send as-is */
159 if ( (strlen(encoding) == 0) || (dont_decode)) {
160 if (CallBack != NULL) {
161 CallBack(name, filename, fixed_partnum(partnum),
162 disposition, part_start,
163 content_type, charset, length, encoding, userdata);
168 /* Fail silently if we hit an unknown encoding. */
169 if ((strcasecmp(encoding, "base64"))
170 && (strcasecmp(encoding, "quoted-printable"))) {
175 * Allocate a buffer for the decoded data. The output buffer is slightly
176 * larger than the input buffer; this assumes that the decoded data
177 * will never be significantly larger than the encoded data. This is a
178 * safe assumption with base64, uuencode, and quoted-printable.
180 decoded = malloc(length + 32768);
181 if (decoded == NULL) {
185 if (!strcasecmp(encoding, "base64")) {
186 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
188 else if (!strcasecmp(encoding, "quoted-printable")) {
189 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
192 if (bytes_decoded > 0) if (CallBack != NULL) {
193 CallBack(name, filename, fixed_partnum(partnum),
194 disposition, decoded,
195 content_type, charset, bytes_decoded, "binary", userdata);
202 * Break out the components of a multipart message
203 * (This function expects to be fed HEADERS + CONTENT)
204 * Note: NULL can be supplied as content_end; in this case, the message is
205 * considered to have ended when the parser encounters a 0x00 byte.
207 void the_mime_parser(char *partnum,
208 char *content_start, char *content_end,
220 void (*PreMultiPartCallBack)
231 void (*PostMultiPartCallBack)
249 char *part_start, *part_end = NULL;
254 size_t startary_len = 0;
259 size_t content_length;
263 char *content_type_name;
264 char *content_disposition_name;
270 char nested_partnum[256];
272 char *evaluate_crlf_ptr = NULL;
277 boundary = malloc(SIZ);
278 memset(boundary, 0, SIZ);
280 startary = malloc(SIZ);
281 memset(startary, 0, SIZ);
283 endary = malloc(SIZ);
284 memset(endary, 0, SIZ);
286 header = malloc(SIZ);
287 memset(header, 0, SIZ);
289 content_type = malloc(SIZ);
290 memset(content_type, 0, SIZ);
292 charset = malloc(SIZ);
293 memset(charset, 0, SIZ);
295 encoding = malloc(SIZ);
296 memset(encoding, 0, SIZ);
298 content_type_name = malloc(SIZ);
299 memset(content_type_name, 0, SIZ);
301 content_disposition_name = malloc(SIZ);
302 memset(content_disposition_name, 0, SIZ);
304 filename = malloc(SIZ);
305 memset(filename, 0, SIZ);
307 disposition = malloc(SIZ);
308 memset(disposition, 0, SIZ);
310 /* If the caller didn't supply an endpointer, generate one by measure */
311 if (content_end == NULL) {
312 content_end = &content_start[strlen(content_start)];
315 /* Learn interesting things from the headers */
318 ptr = memreadline(ptr, buf, SIZ);
319 if (ptr >= content_end) {
323 for (i = 0; i < strlen(buf); ++i) {
324 if (isspace(buf[i])) {
329 if (!isspace(buf[0])) {
330 if (!strncasecmp(header, "Content-type:", 13)) {
331 strcpy(content_type, &header[13]);
332 striplt(content_type);
333 extract_key(content_type_name, content_type, "name");
334 extract_key(charset, content_type, "charset");
335 /* Deal with weird headers */
336 if (strchr(content_type, ' '))
337 *(strchr(content_type, ' ')) = '\0';
338 if (strchr(content_type, ';'))
339 *(strchr(content_type, ';')) = '\0';
341 if (!strncasecmp(header, "Content-Disposition:", 20)) {
342 strcpy(disposition, &header[20]);
343 striplt(disposition);
344 extract_key(content_disposition_name, disposition, "name");
345 extract_key(filename, disposition, "filename");
347 if (!strncasecmp(header, "Content-length: ", 15)) {
349 safestrncpy(clbuf, &header[15], sizeof clbuf);
351 content_length = (size_t) atol(clbuf);
353 if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
354 strcpy(encoding, &header[26]);
357 if (strlen(boundary) == 0)
358 extract_key(boundary, header, "boundary");
361 if ((strlen(header) + strlen(buf) + 2) < SIZ) {
364 } while ((strlen(buf) > 0) && (*ptr != 0));
366 if (strchr(disposition, ';'))
367 *(strchr(disposition, ';')) = '\0';
368 striplt(disposition);
369 if (strchr(content_type, ';'))
370 *(strchr(content_type, ';')) = '\0';
371 striplt(content_type);
373 if (strlen(boundary) > 0) {
379 /* If this is a multipart message, then recursively process it */
383 /* Tell the client about this message's multipartedness */
384 if (PreMultiPartCallBack != NULL) {
385 PreMultiPartCallBack("", "", partnum, "",
386 NULL, content_type, charset,
387 0, encoding, userdata);
390 /* Figure out where the boundaries are */
391 snprintf(startary, SIZ, "--%s", boundary);
392 snprintf(endary, SIZ, "--%s--", boundary);
393 startary_len = strlen(startary);
397 next_boundary = NULL;
398 for (srch=ptr; srch<content_end; ++srch) {
399 if (!memcmp(srch, startary, startary_len)) {
400 next_boundary = srch;
405 if ( (part_start != NULL) && (next_boundary != NULL) ) {
406 part_end = next_boundary;
407 --part_end; /* omit the trailing LF */
409 --part_end; /* omit the trailing CR */
412 if (strlen(partnum) > 0) {
413 snprintf(nested_partnum,
414 sizeof nested_partnum,
419 snprintf(nested_partnum,
420 sizeof nested_partnum,
423 the_mime_parser(nested_partnum,
424 part_start, part_end,
426 PreMultiPartCallBack,
427 PostMultiPartCallBack,
432 if (next_boundary != NULL) {
433 /* If we pass out of scope, don't attempt to
434 * read past the end boundary. */
435 if (!strcmp(next_boundary, endary)) {
439 /* Set up for the next part. */
440 part_start = strstr(next_boundary, "\n");
442 /* Determine whether newlines are LF or CRLF */
443 evaluate_crlf_ptr = part_start;
445 if (!memcmp(evaluate_crlf_ptr, "\r\n", 2)) {
452 /* Advance past the LF ... now we're in the next part */
458 /* Invalid end of multipart. Bail out! */
461 } while ( (ptr < content_end) && (next_boundary != NULL) );
463 if (PostMultiPartCallBack != NULL) {
464 PostMultiPartCallBack("", "", partnum, "", NULL,
465 content_type, charset, 0, encoding, userdata);
470 /* If it's not a multipart message, then do something with it */
474 while (ptr < content_end) {
478 part_end = content_end;
481 * I thought there was an off-by-one error here, but there isn't.
482 * This probably means that there's an off-by-one error somewhere
483 * else ... or maybe only in certain messages?
488 /* Truncate if the header told us to */
489 if ( (content_length > 0) && (length > content_length) ) {
490 length = content_length;
493 /* Sometimes the "name" field is tacked on to Content-type,
494 * and sometimes it's tacked on to Content-disposition. Use
495 * whichever one we have.
497 if (strlen(content_disposition_name) > strlen(content_type_name)) {
498 name = content_disposition_name;
501 name = content_type_name;
504 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
505 partnum, length, content_type, charset, encoding); */
507 /* Ok, we've got a non-multipart part here, so do something with it.
511 content_type, charset, encoding, disposition,
513 CallBack, NULL, NULL,
514 userdata, dont_decode
518 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
520 if (!strcasecmp(content_type, "message/rfc822")) {
522 if (PreMultiPartCallBack != NULL) {
523 PreMultiPartCallBack("", "", partnum, "",
524 NULL, content_type, charset,
525 0, encoding, userdata);
527 if (CallBack != NULL) {
528 if (strlen(partnum) > 0) {
529 snprintf(nested_partnum,
530 sizeof nested_partnum,
535 snprintf(nested_partnum,
536 sizeof nested_partnum,
539 the_mime_parser(nested_partnum,
540 part_start, part_end,
542 PreMultiPartCallBack,
543 PostMultiPartCallBack,
548 if (PostMultiPartCallBack != NULL) {
549 PostMultiPartCallBack("", "", partnum, "", NULL,
550 content_type, charset, 0, encoding, userdata);
558 end_parser: /* free the buffers! end the oppression!! */
566 free(content_type_name);
567 free(content_disposition_name);
575 * Entry point for the MIME parser.
576 * (This function expects to be fed HEADERS + CONTENT)
577 * Note: NULL can be supplied as content_end; in this case, the message is
578 * considered to have ended when the parser encounters a 0x00 byte.
580 void mime_parser(char *content_start,
595 void (*PreMultiPartCallBack)
607 void (*PostMultiPartCallBack)
624 the_mime_parser("", content_start, content_end,
626 PreMultiPartCallBack,
627 PostMultiPartCallBack,
628 userdata, dont_decode);