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.
66 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
69 int soft_line_break = 0;
71 int decoded_length = 0;
79 for (i = 0; i < sourcelen; ++i) {
81 buf[buf_length++] = encoded[i];
83 if ( (encoded[i] == '\n')
85 || (i == (sourcelen-1)) ) {
86 buf[buf_length++] = 0;
88 /*** begin -- process one line ***/
90 if (buf[strlen(buf)-1] == '\n') {
91 buf[strlen(buf)-1] = 0;
93 if (buf[strlen(buf)-1] == '\r') {
94 buf[strlen(buf)-1] = 0;
96 while (isspace(buf[strlen(buf)-1])) {
97 buf[strlen(buf)-1] = 0;
101 while (strlen(buf) > 0) {
102 if (!strcmp(buf, "=")) {
105 } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
106 sscanf(&buf[1], "%02x", &ch);
107 decoded[decoded_length++] = ch;
108 strcpy(buf, &buf[3]);
110 decoded[decoded_length++] = buf[0];
111 strcpy(buf, &buf[1]);
114 if (soft_line_break == 0) {
115 decoded[decoded_length++] = '\r';
116 decoded[decoded_length++] = '\n';
119 /*** end -- process one line ***/
123 decoded[decoded_length++] = 0;
124 return(decoded_length);
128 * Given a message or message-part body and a length, handle any necessary
129 * decoding and pass the request up the stack.
131 void mime_decode(char *partnum,
132 char *part_start, size_t length,
133 char *content_type, char *charset, char *encoding,
135 char *name, char *filename,
147 void (*PreMultiPartCallBack)
158 void (*PostMultiPartCallBack)
175 size_t bytes_decoded = 0;
177 /* Some encodings aren't really encodings */
178 if (!strcasecmp(encoding, "7bit"))
179 strcpy(encoding, "");
180 if (!strcasecmp(encoding, "8bit"))
181 strcpy(encoding, "");
182 if (!strcasecmp(encoding, "binary"))
183 strcpy(encoding, "");
185 /* If this part is not encoded, send as-is */
186 if ( (strlen(encoding) == 0) || (dont_decode)) {
187 if (CallBack != NULL) {
188 CallBack(name, filename, fixed_partnum(partnum),
189 disposition, part_start,
190 content_type, charset, length, encoding, userdata);
195 /* Fail silently if we hit an unknown encoding. */
196 if ((strcasecmp(encoding, "base64"))
197 && (strcasecmp(encoding, "quoted-printable"))) {
202 * Allocate a buffer for the decoded data. The output buffer is slightly
203 * larger than the input buffer; this assumes that the decoded data
204 * will never be significantly larger than the encoded data. This is a
205 * safe assumption with base64, uuencode, and quoted-printable.
207 decoded = malloc(length + 32768);
208 if (decoded == NULL) {
212 if (!strcasecmp(encoding, "base64")) {
213 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
215 else if (!strcasecmp(encoding, "quoted-printable")) {
216 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
219 if (bytes_decoded > 0) if (CallBack != NULL) {
220 CallBack(name, filename, fixed_partnum(partnum),
221 disposition, decoded,
222 content_type, charset, bytes_decoded, "binary", userdata);
229 * Break out the components of a multipart message
230 * (This function expects to be fed HEADERS + CONTENT)
231 * Note: NULL can be supplied as content_end; in this case, the message is
232 * considered to have ended when the parser encounters a 0x00 byte.
234 void the_mime_parser(char *partnum,
235 char *content_start, char *content_end,
247 void (*PreMultiPartCallBack)
258 void (*PostMultiPartCallBack)
276 char *part_start, *part_end = NULL;
281 size_t startary_len = 0;
286 size_t content_length;
290 char *content_type_name;
291 char *content_disposition_name;
297 char nested_partnum[SIZ];
302 boundary = malloc(SIZ);
303 memset(boundary, 0, SIZ);
305 startary = malloc(SIZ);
306 memset(startary, 0, SIZ);
308 endary = malloc(SIZ);
309 memset(endary, 0, SIZ);
311 header = malloc(SIZ);
312 memset(header, 0, SIZ);
314 content_type = malloc(SIZ);
315 memset(content_type, 0, SIZ);
317 charset = malloc(SIZ);
318 memset(charset, 0, SIZ);
320 encoding = malloc(SIZ);
321 memset(encoding, 0, SIZ);
323 content_type_name = malloc(SIZ);
324 memset(content_type_name, 0, SIZ);
326 content_disposition_name = malloc(SIZ);
327 memset(content_disposition_name, 0, SIZ);
329 filename = malloc(SIZ);
330 memset(filename, 0, SIZ);
332 disposition = malloc(SIZ);
333 memset(disposition, 0, SIZ);
335 /* If the caller didn't supply an endpointer, generate one by measure */
336 if (content_end == NULL) {
337 content_end = &content_start[strlen(content_start)];
340 /* Learn interesting things from the headers */
343 ptr = memreadline(ptr, buf, SIZ);
344 if (ptr >= content_end) {
348 for (i = 0; i < strlen(buf); ++i) {
349 if (isspace(buf[i])) {
354 if (!isspace(buf[0])) {
355 if (!strncasecmp(header, "Content-type: ", 14)) {
356 strcpy(content_type, &header[14]);
357 extract_key(content_type_name, content_type, "name");
358 extract_key(charset, content_type, "charset");
359 /* Deal with weird headers */
360 if (strchr(content_type, ' '))
361 *(strchr(content_type, ' ')) = '\0';
362 if (strchr(content_type, ';'))
363 *(strchr(content_type, ';')) = '\0';
365 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
366 strcpy(disposition, &header[21]);
367 extract_key(content_disposition_name, disposition, "name");
368 extract_key(filename, disposition, "filename");
370 if (!strncasecmp(header, "Content-length: ", 16)) {
371 content_length = (size_t) atol(&header[16]);
373 if (!strncasecmp(header,
374 "Content-transfer-encoding: ", 27))
375 strcpy(encoding, &header[27]);
376 if (strlen(boundary) == 0)
377 extract_key(boundary, header, "boundary");
380 if ((strlen(header) + strlen(buf) + 2) < SIZ)
382 } while ((strlen(buf) > 0) && (*ptr != 0));
384 if (strchr(disposition, ';'))
385 *(strchr(disposition, ';')) = '\0';
386 striplt(disposition);
387 if (strchr(content_type, ';'))
388 *(strchr(content_type, ';')) = '\0';
389 striplt(content_type);
391 if (strlen(boundary) > 0) {
397 /* If this is a multipart message, then recursively process it */
401 /* Tell the client about this message's multipartedness */
402 if (PreMultiPartCallBack != NULL) {
403 PreMultiPartCallBack("", "", partnum, "",
404 NULL, content_type, charset,
405 0, encoding, userdata);
408 /* Figure out where the boundaries are */
409 snprintf(startary, SIZ, "--%s", boundary);
410 snprintf(endary, SIZ, "--%s--", boundary);
411 startary_len = strlen(startary);
415 next_boundary = NULL;
416 for (srch=ptr; srch<content_end; ++srch) {
417 if (!memcmp(srch, startary, startary_len)) {
418 next_boundary = srch;
423 if ( (part_start != NULL) && (next_boundary != NULL) ) {
424 part_end = next_boundary;
427 if (strlen(partnum) > 0) {
428 snprintf(nested_partnum,
429 sizeof nested_partnum,
434 snprintf(nested_partnum,
435 sizeof nested_partnum,
438 the_mime_parser(nested_partnum,
439 part_start, part_end,
441 PreMultiPartCallBack,
442 PostMultiPartCallBack,
447 if (next_boundary != NULL) {
448 /* If we pass out of scope, don't attempt to
449 * read past the end boundary. */
450 if (!strcmp(next_boundary, endary)) {
454 /* Set up for the next part. */
455 part_start = strstr(next_boundary, "\n");
461 /* Invalid end of multipart. Bail out! */
464 } while ( (ptr < content_end) && (next_boundary != NULL) );
466 if (PostMultiPartCallBack != NULL) {
467 PostMultiPartCallBack("", "", partnum, "", NULL,
468 content_type, charset, 0, encoding, userdata);
473 /* If it's not a multipart message, then do something with it */
477 while (ptr < content_end) {
481 part_end = content_end;
482 /* fix an off-by-one error */
486 /* Truncate if the header told us to */
487 if ( (content_length > 0) && (length > content_length) ) {
488 length = content_length;
491 /* Sometimes the "name" field is tacked on to Content-type,
492 * and sometimes it's tacked on to Content-disposition. Use
493 * whichever one we have.
495 if (strlen(content_disposition_name) > strlen(content_type_name)) {
496 name = content_disposition_name;
499 name = content_type_name;
502 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
503 partnum, length, content_type, charset, encoding); */
505 /* Ok, we've got a non-multipart part here, so do something with it.
509 content_type, charset, encoding, disposition,
511 CallBack, NULL, NULL,
512 userdata, dont_decode
516 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
518 if (!strcasecmp(content_type, "message/rfc822")) {
520 if (PreMultiPartCallBack != NULL) {
521 PreMultiPartCallBack("", "", partnum, "",
522 NULL, content_type, charset,
523 0, encoding, userdata);
525 if (CallBack != NULL) {
526 if (strlen(partnum) > 0) {
527 snprintf(nested_partnum,
528 sizeof nested_partnum,
533 snprintf(nested_partnum,
534 sizeof nested_partnum,
537 the_mime_parser(nested_partnum,
538 part_start, part_end,
540 PreMultiPartCallBack,
541 PostMultiPartCallBack,
546 if (PostMultiPartCallBack != NULL) {
547 PostMultiPartCallBack("", "", partnum, "", NULL,
548 content_type, charset, 0, encoding, userdata);
556 end_parser: /* free the buffers! end the oppression!! */
564 free(content_type_name);
565 free(content_disposition_name);
573 * Entry point for the MIME parser.
574 * (This function expects to be fed HEADERS + CONTENT)
575 * Note: NULL can be supplied as content_end; in this case, the message is
576 * considered to have ended when the parser encounters a 0x00 byte.
578 void mime_parser(char *content_start,
593 void (*PreMultiPartCallBack)
605 void (*PostMultiPartCallBack)
622 the_mime_parser("", content_start, content_end,
624 PreMultiPartCallBack,
625 PostMultiPartCallBack,
626 userdata, dont_decode);