4 * This is the MIME parser for Citadel. Sometimes it actually works.
6 * Copyright (c) 1998-2002 by Art Cancro
7 * This code is distributed under the terms of the GNU General Public License.
15 #include <sys/types.h>
21 #include "webserver.h"
24 void extract_key(char *target, char *source, char *key)
28 strcpy(target, source);
29 for (a = 0; a < strlen(target); ++a) {
30 if ((!strncasecmp(&target[a], key, strlen(key)))
31 && (target[a + strlen(key)] == '=')) {
32 strcpy(target, &target[a + strlen(key) + 1]);
34 strcpy(target, &target[1]);
35 for (b = 0; b < strlen(target); ++b)
46 * For non-multipart messages, we need to generate a quickie partnum of "1"
47 * to return to callback functions. Some callbacks demand it.
49 char *fixed_partnum(char *supplied_partnum) {
50 if (supplied_partnum == NULL) return "1";
51 if (strlen(supplied_partnum)==0) return "1";
52 return supplied_partnum;
58 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
60 int decode_quoted_printable(char *decoded, char *encoded, int sourcelen) {
63 int soft_line_break = 0;
65 int decoded_length = 0;
73 for (i = 0; i < sourcelen; ++i) {
75 buf[buf_length++] = encoded[i];
77 if ( (encoded[i] == '\n')
79 || (i == (sourcelen-1)) ) {
80 buf[buf_length++] = 0;
82 /*** begin -- process one line ***/
84 if (buf[strlen(buf)-1] == '\n') {
85 buf[strlen(buf)-1] = 0;
87 if (buf[strlen(buf)-1] == '\r') {
88 buf[strlen(buf)-1] = 0;
90 while (isspace(buf[strlen(buf)-1])) {
91 buf[strlen(buf)-1] = 0;
95 while (strlen(buf) > 0) {
96 if (!strcmp(buf, "=")) {
99 } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
100 sscanf(&buf[1], "%02x", &ch);
101 decoded[decoded_length++] = ch;
102 strcpy(buf, &buf[3]);
104 decoded[decoded_length++] = buf[0];
105 strcpy(buf, &buf[1]);
108 if (soft_line_break == 0) {
109 decoded[decoded_length++] = '\r';
110 decoded[decoded_length++] = '\n';
113 /*** end -- process one line ***/
117 decoded[decoded_length++] = 0;
118 return(decoded_length);
122 * Given a message or message-part body and a length, handle any necessary
123 * decoding and pass the request up the stack.
125 void mime_decode(char *partnum,
126 char *part_start, size_t length,
127 char *content_type, char *encoding,
129 char *name, char *filename,
140 void (*PreMultiPartCallBack)
150 void (*PostMultiPartCallBack)
166 size_t bytes_decoded = 0;
168 lprintf(9, "mime_decode() called\n");
170 /* Some encodings aren't really encodings */
171 if (!strcasecmp(encoding, "7bit"))
172 strcpy(encoding, "");
173 if (!strcasecmp(encoding, "8bit"))
174 strcpy(encoding, "");
175 if (!strcasecmp(encoding, "binary"))
176 strcpy(encoding, "");
178 /* If this part is not encoded, send as-is */
179 if ( (strlen(encoding) == 0) || (dont_decode)) {
180 if (CallBack != NULL) {
181 CallBack(name, filename, fixed_partnum(partnum),
182 disposition, part_start,
183 content_type, length, encoding, userdata);
188 if ((strcasecmp(encoding, "base64"))
189 && (strcasecmp(encoding, "quoted-printable"))) {
190 lprintf(1, "ERROR: unknown MIME encoding '%s'\n", encoding);
194 * Allocate a buffer for the decoded data. The output buffer is the
195 * same size as the input buffer; this assumes that the decoded data
196 * will never be larger than the encoded data. This is a safe
197 * assumption with base64, uuencode, and quoted-printable.
199 lprintf(9, "About to allocate %d bytes for decoded part\n",
201 decoded = malloc(length+2048);
202 if (decoded == NULL) {
203 lprintf(1, "ERROR: cannot allocate memory.\n");
206 lprintf(9, "Got it!\n");
208 lprintf(9, "Decoding %s\n", encoding);
209 if (!strcasecmp(encoding, "base64")) {
210 bytes_decoded = decode_base64(decoded, part_start);
212 else if (!strcasecmp(encoding, "quoted-printable")) {
213 bytes_decoded = decode_quoted_printable(decoded,
216 lprintf(9, "Bytes decoded: %d\n", bytes_decoded);
218 if (bytes_decoded > 0) if (CallBack != NULL) {
219 CallBack(name, filename, fixed_partnum(partnum),
220 disposition, decoded,
221 content_type, bytes_decoded, "binary", userdata);
228 * Break out the components of a multipart message
229 * (This function expects to be fed HEADERS + CONTENT)
230 * Note: NULL can be supplied as content_end; in this case, the message is
231 * considered to have ended when the parser encounters a 0x00 byte.
233 void the_mime_parser(char *partnum,
234 char *content_start, char *content_end,
245 void (*PreMultiPartCallBack)
255 void (*PostMultiPartCallBack)
271 char *part_start, *part_end = NULL;
278 size_t content_length;
287 char nested_partnum[SIZ];
289 lprintf(9, "the_mime_parser() called\n");
293 boundary = malloc(SIZ);
294 memset(boundary, 0, SIZ);
296 startary = malloc(SIZ);
297 memset(startary, 0, SIZ);
299 endary = malloc(SIZ);
300 memset(endary, 0, SIZ);
302 header = malloc(SIZ);
303 memset(header, 0, SIZ);
305 content_type = malloc(SIZ);
306 memset(content_type, 0, SIZ);
308 encoding = malloc(SIZ);
309 memset(encoding, 0, SIZ);
312 memset(name, 0, SIZ);
314 filename = malloc(SIZ);
315 memset(filename, 0, SIZ);
317 disposition = malloc(SIZ);
318 memset(disposition, 0, SIZ);
320 /* If the caller didn't supply an endpointer, generate one by measure */
321 if (content_end == NULL) {
322 content_end = &content_start[strlen(content_start)];
325 /* Learn interesting things from the headers */
328 ptr = memreadline(ptr, buf, SIZ);
329 if (ptr >= content_end) {
333 for (i = 0; i < strlen(buf); ++i)
336 if (!isspace(buf[0])) {
337 if (!strncasecmp(header, "Content-type: ", 14)) {
338 strcpy(content_type, &header[14]);
339 extract_key(name, content_type, "name");
340 lprintf(9, "Extracted content-type <%s>\n",
343 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
344 strcpy(disposition, &header[21]);
345 extract_key(filename, disposition, "filename");
347 if (!strncasecmp(header, "Content-length: ", 16)) {
348 content_length = (size_t) atol(&header[16]);
350 if (!strncasecmp(header,
351 "Content-transfer-encoding: ", 27))
352 strcpy(encoding, &header[27]);
353 if (strlen(boundary) == 0)
354 extract_key(boundary, header, "boundary");
357 if ((strlen(header) + strlen(buf) + 2) < SIZ)
359 } while ((strlen(buf) > 0) && (*ptr != 0));
361 for (i = 0; i < strlen(disposition); ++i)
362 if (disposition[i] == ';')
364 while (isspace(disposition[0]))
365 strcpy(disposition, &disposition[1]);
366 for (i = 0; i < strlen(content_type); ++i)
367 if (content_type[i] == ';')
369 while (isspace(content_type[0]))
370 strcpy(content_type, &content_type[1]);
372 if (strlen(boundary) > 0) {
378 lprintf(9, "is_multipart=%d, boundary=<%s>\n",
379 is_multipart, boundary);
381 /* If this is a multipart message, then recursively process it */
385 /* Tell the client about this message's multipartedness */
386 if (PreMultiPartCallBack != NULL) {
387 PreMultiPartCallBack("", "", partnum, "",
389 0, encoding, userdata);
392 /* Figure out where the boundaries are */
393 sprintf(startary, "--%s", boundary);
394 sprintf(endary, "--%s--", boundary);
396 if ( (!strncasecmp(ptr, startary, strlen(startary)))
397 || (!strncasecmp(ptr, endary, strlen(endary))) ) {
398 lprintf(9, "hit boundary!\n");
399 if (part_start != NULL) {
400 if (strlen(partnum) > 0) {
401 sprintf(nested_partnum, "%s.%d",
402 partnum, ++part_seq);
405 sprintf(nested_partnum, "%d",
408 the_mime_parser(nested_partnum,
409 part_start, part_end,
411 PreMultiPartCallBack,
412 PostMultiPartCallBack,
416 ptr = memreadline(ptr, buf, SIZ);
423 } while ( (strcasecmp(ptr, endary)) && (ptr <= content_end) );
424 if (PostMultiPartCallBack != NULL) {
425 PostMultiPartCallBack("", "", partnum, "", NULL,
426 content_type, 0, encoding, userdata);
431 /* If it's not a multipart message, then do something with it */
433 lprintf(9, "doing non-multipart thing\n");
436 while (ptr < content_end) {
440 part_end = content_end;
442 /* Truncate if the header told us to */
443 if ( (content_length > 0) && (length > content_length) ) {
444 length = content_length;
445 lprintf(9, "truncated to %ld\n", (long)content_length);
450 content_type, encoding, disposition,
452 CallBack, NULL, NULL,
453 userdata, dont_decode);
456 end_parser: /* free the buffers! end the oppression!! */
471 * Entry point for the MIME parser.
472 * (This function expects to be fed HEADERS + CONTENT)
473 * Note: NULL can be supplied as content_end; in this case, the message is
474 * considered to have ended when the parser encounters a 0x00 byte.
476 void mime_parser(char *content_start,
490 void (*PreMultiPartCallBack)
501 void (*PostMultiPartCallBack)
517 lprintf(9, "mime_parser() called\n");
518 the_mime_parser("", content_start, content_end,
520 PreMultiPartCallBack,
521 PostMultiPartCallBack,
522 userdata, dont_decode);