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[SIZ];
275 boundary = malloc(SIZ);
276 memset(boundary, 0, SIZ);
278 startary = malloc(SIZ);
279 memset(startary, 0, SIZ);
281 endary = malloc(SIZ);
282 memset(endary, 0, SIZ);
284 header = malloc(SIZ);
285 memset(header, 0, SIZ);
287 content_type = malloc(SIZ);
288 memset(content_type, 0, SIZ);
290 charset = malloc(SIZ);
291 memset(charset, 0, SIZ);
293 encoding = malloc(SIZ);
294 memset(encoding, 0, SIZ);
296 content_type_name = malloc(SIZ);
297 memset(content_type_name, 0, SIZ);
299 content_disposition_name = malloc(SIZ);
300 memset(content_disposition_name, 0, SIZ);
302 filename = malloc(SIZ);
303 memset(filename, 0, SIZ);
305 disposition = malloc(SIZ);
306 memset(disposition, 0, SIZ);
308 /* If the caller didn't supply an endpointer, generate one by measure */
309 if (content_end == NULL) {
310 content_end = &content_start[strlen(content_start)];
313 /* Learn interesting things from the headers */
316 ptr = memreadline(ptr, buf, SIZ);
317 if (ptr >= content_end) {
321 for (i = 0; i < strlen(buf); ++i) {
322 if (isspace(buf[i])) {
327 if (!isspace(buf[0])) {
328 if (!strncasecmp(header, "Content-type: ", 14)) {
329 strcpy(content_type, &header[14]);
330 extract_key(content_type_name, content_type, "name");
331 extract_key(charset, content_type, "charset");
332 /* Deal with weird headers */
333 if (strchr(content_type, ' '))
334 *(strchr(content_type, ' ')) = '\0';
335 if (strchr(content_type, ';'))
336 *(strchr(content_type, ';')) = '\0';
338 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
339 strcpy(disposition, &header[21]);
340 extract_key(content_disposition_name, disposition, "name");
341 extract_key(filename, disposition, "filename");
343 if (!strncasecmp(header, "Content-length: ", 16)) {
344 content_length = (size_t) atol(&header[16]);
346 if (!strncasecmp(header,
347 "Content-transfer-encoding: ", 27))
348 strcpy(encoding, &header[27]);
349 if (strlen(boundary) == 0)
350 extract_key(boundary, header, "boundary");
353 if ((strlen(header) + strlen(buf) + 2) < SIZ)
355 } while ((strlen(buf) > 0) && (*ptr != 0));
357 if (strchr(disposition, ';'))
358 *(strchr(disposition, ';')) = '\0';
359 striplt(disposition);
360 if (strchr(content_type, ';'))
361 *(strchr(content_type, ';')) = '\0';
362 striplt(content_type);
364 if (strlen(boundary) > 0) {
370 /* If this is a multipart message, then recursively process it */
374 /* Tell the client about this message's multipartedness */
375 if (PreMultiPartCallBack != NULL) {
376 PreMultiPartCallBack("", "", partnum, "",
377 NULL, content_type, charset,
378 0, encoding, userdata);
381 /* Figure out where the boundaries are */
382 snprintf(startary, SIZ, "--%s", boundary);
383 snprintf(endary, SIZ, "--%s--", boundary);
384 startary_len = strlen(startary);
388 next_boundary = NULL;
389 for (srch=ptr; srch<content_end; ++srch) {
390 if (!memcmp(srch, startary, startary_len)) {
391 next_boundary = srch;
396 if ( (part_start != NULL) && (next_boundary != NULL) ) {
397 part_end = next_boundary;
400 if (strlen(partnum) > 0) {
401 snprintf(nested_partnum,
402 sizeof nested_partnum,
407 snprintf(nested_partnum,
408 sizeof nested_partnum,
411 the_mime_parser(nested_partnum,
412 part_start, part_end,
414 PreMultiPartCallBack,
415 PostMultiPartCallBack,
420 if (next_boundary != NULL) {
421 /* If we pass out of scope, don't attempt to
422 * read past the end boundary. */
423 if (!strcmp(next_boundary, endary)) {
427 /* Set up for the next part. */
428 part_start = strstr(next_boundary, "\n");
434 /* Invalid end of multipart. Bail out! */
437 } while ( (ptr < content_end) && (next_boundary != NULL) );
439 if (PostMultiPartCallBack != NULL) {
440 PostMultiPartCallBack("", "", partnum, "", NULL,
441 content_type, charset, 0, encoding, userdata);
446 /* If it's not a multipart message, then do something with it */
450 while (ptr < content_end) {
454 part_end = content_end;
455 /* fix an off-by-one error */
459 /* Truncate if the header told us to */
460 if ( (content_length > 0) && (length > content_length) ) {
461 length = content_length;
464 /* Sometimes the "name" field is tacked on to Content-type,
465 * and sometimes it's tacked on to Content-disposition. Use
466 * whichever one we have.
468 if (strlen(content_disposition_name) > strlen(content_type_name)) {
469 name = content_disposition_name;
472 name = content_type_name;
475 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
476 partnum, length, content_type, charset, encoding); */
478 /* Ok, we've got a non-multipart part here, so do something with it.
482 content_type, charset, encoding, disposition,
484 CallBack, NULL, NULL,
485 userdata, dont_decode
489 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
491 if (!strcasecmp(content_type, "message/rfc822")) {
493 if (PreMultiPartCallBack != NULL) {
494 PreMultiPartCallBack("", "", partnum, "",
495 NULL, content_type, charset,
496 0, encoding, userdata);
498 if (CallBack != NULL) {
499 if (strlen(partnum) > 0) {
500 snprintf(nested_partnum,
501 sizeof nested_partnum,
506 snprintf(nested_partnum,
507 sizeof nested_partnum,
510 the_mime_parser(nested_partnum,
511 part_start, part_end,
513 PreMultiPartCallBack,
514 PostMultiPartCallBack,
519 if (PostMultiPartCallBack != NULL) {
520 PostMultiPartCallBack("", "", partnum, "", NULL,
521 content_type, charset, 0, encoding, userdata);
529 end_parser: /* free the buffers! end the oppression!! */
537 free(content_type_name);
538 free(content_disposition_name);
546 * Entry point for the MIME parser.
547 * (This function expects to be fed HEADERS + CONTENT)
548 * Note: NULL can be supplied as content_end; in this case, the message is
549 * considered to have ended when the parser encounters a 0x00 byte.
551 void mime_parser(char *content_start,
566 void (*PreMultiPartCallBack)
578 void (*PostMultiPartCallBack)
595 the_mime_parser("", content_start, content_end,
597 PreMultiPartCallBack,
598 PostMultiPartCallBack,
599 userdata, dont_decode);