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) {
76 decoded[0] = encoded[0];
77 while (sourcepos <= sourcelen){
78 check = &decoded[destpos];
79 decoded[destpos] = encoded[sourcepos];
80 if ((ignore_last == 0) && (decoded[destpos-1] == '='))
82 if ((*check == '\0') ||
86 decoded[destpos - 1] = '\0';
89 else if (sourcelen - sourcepos > 2)
91 sscanf(&encoded[sourcepos], "%02x", &ch);
92 decoded[destpos - 1] = ch;
104 decoded[destpos] = 0;
110 * Given a message or message-part body and a length, handle any necessary
111 * decoding and pass the request up the stack.
113 void mime_decode(char *partnum,
114 char *part_start, size_t length,
115 char *content_type, char *charset, char *encoding,
117 char *name, char *filename,
129 void (*PreMultiPartCallBack)
140 void (*PostMultiPartCallBack)
157 size_t bytes_decoded = 0;
159 /* Some encodings aren't really encodings */
160 if (!strcasecmp(encoding, "7bit"))
161 strcpy(encoding, "");
162 if (!strcasecmp(encoding, "8bit"))
163 strcpy(encoding, "");
164 if (!strcasecmp(encoding, "binary"))
165 strcpy(encoding, "");
167 /* If this part is not encoded, send as-is */
168 if ( (strlen(encoding) == 0) || (dont_decode)) {
169 if (CallBack != NULL) {
170 CallBack(name, filename, fixed_partnum(partnum),
171 disposition, part_start,
172 content_type, charset, length, encoding, userdata);
177 /* Fail silently if we hit an unknown encoding. */
178 if ((strcasecmp(encoding, "base64"))
179 && (strcasecmp(encoding, "quoted-printable"))) {
184 * Allocate a buffer for the decoded data. The output buffer is slightly
185 * larger than the input buffer; this assumes that the decoded data
186 * will never be significantly larger than the encoded data. This is a
187 * safe assumption with base64, uuencode, and quoted-printable.
189 decoded = malloc(length + 32768);
190 if (decoded == NULL) {
194 if (!strcasecmp(encoding, "base64")) {
195 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
197 else if (!strcasecmp(encoding, "quoted-printable")) {
198 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
201 if (bytes_decoded > 0) if (CallBack != NULL) {
202 CallBack(name, filename, fixed_partnum(partnum),
203 disposition, decoded,
204 content_type, charset, bytes_decoded, "binary", userdata);
211 * Break out the components of a multipart message
212 * (This function expects to be fed HEADERS + CONTENT)
213 * Note: NULL can be supplied as content_end; in this case, the message is
214 * considered to have ended when the parser encounters a 0x00 byte.
216 void the_mime_parser(char *partnum,
217 char *content_start, char *content_end,
229 void (*PreMultiPartCallBack)
240 void (*PostMultiPartCallBack)
258 char *part_start, *part_end = NULL;
263 size_t startary_len = 0;
268 size_t content_length;
272 char *content_type_name;
273 char *content_disposition_name;
279 char nested_partnum[SIZ];
284 boundary = malloc(SIZ);
285 memset(boundary, 0, SIZ);
287 startary = malloc(SIZ);
288 memset(startary, 0, SIZ);
290 endary = malloc(SIZ);
291 memset(endary, 0, SIZ);
293 header = malloc(SIZ);
294 memset(header, 0, SIZ);
296 content_type = malloc(SIZ);
297 memset(content_type, 0, SIZ);
299 charset = malloc(SIZ);
300 memset(charset, 0, SIZ);
302 encoding = malloc(SIZ);
303 memset(encoding, 0, SIZ);
305 content_type_name = malloc(SIZ);
306 memset(content_type_name, 0, SIZ);
308 content_disposition_name = malloc(SIZ);
309 memset(content_disposition_name, 0, SIZ);
311 filename = malloc(SIZ);
312 memset(filename, 0, SIZ);
314 disposition = malloc(SIZ);
315 memset(disposition, 0, SIZ);
317 /* If the caller didn't supply an endpointer, generate one by measure */
318 if (content_end == NULL) {
319 content_end = &content_start[strlen(content_start)];
322 /* Learn interesting things from the headers */
325 ptr = memreadline(ptr, buf, SIZ);
326 if (ptr >= content_end) {
330 for (i = 0; i < strlen(buf); ++i) {
331 if (isspace(buf[i])) {
336 if (!isspace(buf[0])) {
337 if (!strncasecmp(header, "Content-type: ", 14)) {
338 strcpy(content_type, &header[14]);
339 extract_key(content_type_name, content_type, "name");
340 extract_key(charset, content_type, "charset");
341 /* Deal with weird headers */
342 if (strchr(content_type, ' '))
343 *(strchr(content_type, ' ')) = '\0';
344 if (strchr(content_type, ';'))
345 *(strchr(content_type, ';')) = '\0';
347 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
348 strcpy(disposition, &header[21]);
349 extract_key(content_disposition_name, disposition, "name");
350 extract_key(filename, disposition, "filename");
352 if (!strncasecmp(header, "Content-length: ", 16)) {
353 content_length = (size_t) atol(&header[16]);
355 if (!strncasecmp(header,
356 "Content-transfer-encoding: ", 27))
357 strcpy(encoding, &header[27]);
358 if (strlen(boundary) == 0)
359 extract_key(boundary, header, "boundary");
362 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;
409 if (strlen(partnum) > 0) {
410 snprintf(nested_partnum,
411 sizeof nested_partnum,
416 snprintf(nested_partnum,
417 sizeof nested_partnum,
420 the_mime_parser(nested_partnum,
421 part_start, part_end,
423 PreMultiPartCallBack,
424 PostMultiPartCallBack,
429 if (next_boundary != NULL) {
430 /* If we pass out of scope, don't attempt to
431 * read past the end boundary. */
432 if (!strcmp(next_boundary, endary)) {
436 /* Set up for the next part. */
437 part_start = strstr(next_boundary, "\n");
443 /* Invalid end of multipart. Bail out! */
446 } while ( (ptr < content_end) && (next_boundary != NULL) );
448 if (PostMultiPartCallBack != NULL) {
449 PostMultiPartCallBack("", "", partnum, "", NULL,
450 content_type, charset, 0, encoding, userdata);
455 /* If it's not a multipart message, then do something with it */
459 while (ptr < content_end) {
463 part_end = content_end;
464 /* fix an off-by-one error */
468 /* Truncate if the header told us to */
469 if ( (content_length > 0) && (length > content_length) ) {
470 length = content_length;
473 /* Sometimes the "name" field is tacked on to Content-type,
474 * and sometimes it's tacked on to Content-disposition. Use
475 * whichever one we have.
477 if (strlen(content_disposition_name) > strlen(content_type_name)) {
478 name = content_disposition_name;
481 name = content_type_name;
484 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
485 partnum, length, content_type, charset, encoding); */
487 /* Ok, we've got a non-multipart part here, so do something with it.
491 content_type, charset, encoding, disposition,
493 CallBack, NULL, NULL,
494 userdata, dont_decode
498 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
500 if (!strcasecmp(content_type, "message/rfc822")) {
502 if (PreMultiPartCallBack != NULL) {
503 PreMultiPartCallBack("", "", partnum, "",
504 NULL, content_type, charset,
505 0, encoding, userdata);
507 if (CallBack != NULL) {
508 if (strlen(partnum) > 0) {
509 snprintf(nested_partnum,
510 sizeof nested_partnum,
515 snprintf(nested_partnum,
516 sizeof nested_partnum,
519 the_mime_parser(nested_partnum,
520 part_start, part_end,
522 PreMultiPartCallBack,
523 PostMultiPartCallBack,
528 if (PostMultiPartCallBack != NULL) {
529 PostMultiPartCallBack("", "", partnum, "", NULL,
530 content_type, charset, 0, encoding, userdata);
538 end_parser: /* free the buffers! end the oppression!! */
546 free(content_type_name);
547 free(content_disposition_name);
555 * Entry point for the MIME parser.
556 * (This function expects to be fed HEADERS + CONTENT)
557 * Note: NULL can be supplied as content_end; in this case, the message is
558 * considered to have ended when the parser encounters a 0x00 byte.
560 void mime_parser(char *content_start,
575 void (*PreMultiPartCallBack)
587 void (*PostMultiPartCallBack)
604 the_mime_parser("", content_start, content_end,
606 PreMultiPartCallBack,
607 PostMultiPartCallBack,
608 userdata, dont_decode);