4 * This is the MIME parser for Citadel. Sometimes it actually works.
6 * Copyright (c) 1998-2003 by Art Cancro
7 * This code is distributed under the terms of the GNU General Public License.
21 #include <sys/types.h>
23 #include <sys/socket.h>
24 #ifdef HAVE_SYS_TIME_H
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
40 #include "webserver.h"
42 #include "mime_parser.h"
45 void extract_key(char *target, char *source, char *key)
49 strcpy(target, source);
50 for (a = 0; a < strlen(target); ++a) {
51 if ((!strncasecmp(&target[a], key, strlen(key)))
52 && (target[a + strlen(key)] == '=')) {
53 strcpy(target, &target[a + strlen(key) + 1]);
55 strcpy(target, &target[1]);
56 for (b = 0; b < strlen(target); ++b)
67 * For non-multipart messages, we need to generate a quickie partnum of "1"
68 * to return to callback functions. Some callbacks demand it.
70 char *fixed_partnum(char *supplied_partnum) {
71 if (supplied_partnum == NULL) return "1";
72 if (strlen(supplied_partnum)==0) return "1";
73 return supplied_partnum;
79 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
81 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
84 int soft_line_break = 0;
86 int decoded_length = 0;
94 for (i = 0; i < sourcelen; ++i) {
96 buf[buf_length++] = encoded[i];
98 if ( (encoded[i] == '\n')
100 || (i == (sourcelen-1)) ) {
101 buf[buf_length++] = 0;
103 /*** begin -- process one line ***/
105 if (buf[strlen(buf)-1] == '\n') {
106 buf[strlen(buf)-1] = 0;
108 if (buf[strlen(buf)-1] == '\r') {
109 buf[strlen(buf)-1] = 0;
111 while (isspace(buf[strlen(buf)-1])) {
112 buf[strlen(buf)-1] = 0;
116 while (strlen(buf) > 0) {
117 if (!strcmp(buf, "=")) {
120 } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
121 sscanf(&buf[1], "%02x", &ch);
122 decoded[decoded_length++] = ch;
123 strcpy(buf, &buf[3]);
125 decoded[decoded_length++] = buf[0];
126 strcpy(buf, &buf[1]);
129 if (soft_line_break == 0) {
130 decoded[decoded_length++] = '\r';
131 decoded[decoded_length++] = '\n';
134 /*** end -- process one line ***/
138 decoded[decoded_length++] = 0;
139 return(decoded_length);
143 * Given a message or message-part body and a length, handle any necessary
144 * decoding and pass the request up the stack.
146 void mime_decode(char *partnum,
147 char *part_start, size_t length,
148 char *content_type, char *encoding,
150 char *name, char *filename,
161 void (*PreMultiPartCallBack)
171 void (*PostMultiPartCallBack)
187 size_t bytes_decoded = 0;
189 /* Some encodings aren't really encodings */
190 if (!strcasecmp(encoding, "7bit"))
191 strcpy(encoding, "");
192 if (!strcasecmp(encoding, "8bit"))
193 strcpy(encoding, "");
194 if (!strcasecmp(encoding, "binary"))
195 strcpy(encoding, "");
197 /* If this part is not encoded, send as-is */
198 if ( (strlen(encoding) == 0) || (dont_decode)) {
199 if (CallBack != NULL) {
200 CallBack(name, filename, fixed_partnum(partnum),
201 disposition, part_start,
202 content_type, length, encoding, userdata);
207 if ((strcasecmp(encoding, "base64"))
208 && (strcasecmp(encoding, "quoted-printable"))) {
212 * Allocate a buffer for the decoded data. The output buffer is the
213 * same size as the input buffer; this assumes that the decoded data
214 * will never be larger than the encoded data. This is a safe
215 * assumption with base64, uuencode, and quoted-printable.
217 decoded = mallok(length+2048);
218 if (decoded == NULL) {
222 if (!strcasecmp(encoding, "base64")) {
223 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
225 else if (!strcasecmp(encoding, "quoted-printable")) {
226 bytes_decoded = CtdlDecodeQuotedPrintable(decoded,
230 if (bytes_decoded > 0) if (CallBack != NULL) {
231 CallBack(name, filename, fixed_partnum(partnum),
232 disposition, decoded,
233 content_type, bytes_decoded, "binary", userdata);
240 * Break out the components of a multipart message
241 * (This function expects to be fed HEADERS + CONTENT)
242 * Note: NULL can be supplied as content_end; in this case, the message is
243 * considered to have ended when the parser encounters a 0x00 byte.
245 void the_mime_parser(char *partnum,
246 char *content_start, char *content_end,
257 void (*PreMultiPartCallBack)
267 void (*PostMultiPartCallBack)
283 char *part_start, *part_end = NULL;
290 size_t content_length;
294 char *content_type_name;
295 char *content_disposition_name;
301 char nested_partnum[SIZ];
306 boundary = mallok(SIZ);
307 memset(boundary, 0, SIZ);
309 startary = mallok(SIZ);
310 memset(startary, 0, SIZ);
312 endary = mallok(SIZ);
313 memset(endary, 0, SIZ);
315 header = mallok(SIZ);
316 memset(header, 0, SIZ);
318 content_type = mallok(SIZ);
319 memset(content_type, 0, SIZ);
321 encoding = mallok(SIZ);
322 memset(encoding, 0, SIZ);
324 content_type_name = mallok(SIZ);
325 memset(content_type_name, 0, SIZ);
327 content_disposition_name = mallok(SIZ);
328 memset(content_disposition_name, 0, SIZ);
330 filename = mallok(SIZ);
331 memset(filename, 0, SIZ);
333 disposition = mallok(SIZ);
334 memset(disposition, 0, SIZ);
336 /* If the caller didn't supply an endpointer, generate one by measure */
337 if (content_end == NULL) {
338 content_end = &content_start[strlen(content_start)];
341 /* Learn interesting things from the headers */
344 ptr = memreadline(ptr, buf, SIZ);
345 if (ptr >= content_end) {
349 for (i = 0; i < strlen(buf); ++i)
352 if (!isspace(buf[0])) {
353 if (!strncasecmp(header, "Content-type: ", 14)) {
354 strcpy(content_type, &header[14]);
355 extract_key(content_type_name, content_type, "name");
356 /* Deal with weird headers */
357 if (strchr(content_type, ' '))
358 *(strchr(content_type, ' ')) = '\0';
359 if (strchr(content_type, ';'))
360 *(strchr(content_type, ';')) = '\0';
362 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
363 strcpy(disposition, &header[21]);
364 extract_key(content_disposition_name, disposition, "name");
365 extract_key(filename, disposition, "filename");
367 if (!strncasecmp(header, "Content-length: ", 16)) {
368 content_length = (size_t) atol(&header[16]);
370 if (!strncasecmp(header,
371 "Content-transfer-encoding: ", 27))
372 strcpy(encoding, &header[27]);
373 if (strlen(boundary) == 0)
374 extract_key(boundary, header, "boundary");
377 if ((strlen(header) + strlen(buf) + 2) < SIZ)
379 } while ((strlen(buf) > 0) && (*ptr != 0));
381 if (strchr(disposition, ';'))
382 *(strchr(disposition, ';')) = '\0';
383 striplt(disposition);
384 if (strchr(content_type, ';'))
385 *(strchr(content_type, ';')) = '\0';
386 striplt(content_type);
388 if (strlen(boundary) > 0) {
394 /* If this is a multipart message, then recursively process it */
398 /* Tell the client about this message's multipartedness */
399 if (PreMultiPartCallBack != NULL) {
400 PreMultiPartCallBack("", "", partnum, "",
402 0, encoding, userdata);
405 /* Figure out where the boundaries are */
406 snprintf(startary, SIZ, "--%s", boundary);
407 snprintf(endary, SIZ, "--%s--", boundary);
409 if ( (!strncasecmp(ptr, startary, strlen(startary)))
410 || (!strncasecmp(ptr, endary, strlen(endary))) ) {
411 if (part_start != NULL) {
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,
431 ptr = memreadline(ptr, buf, SIZ);
438 /* If we pass out of scope in the MIME multipart (by
439 * hitting the end boundary), force the pointer out
440 * of scope so this loop ends.
442 if (ptr < content_end) {
443 if (!strcasecmp(ptr, endary)) {
447 } while (ptr <= content_end);
448 if (PostMultiPartCallBack != NULL) {
449 PostMultiPartCallBack("", "", partnum, "", NULL,
450 content_type, 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;
486 content_type, encoding, disposition,
488 CallBack, NULL, NULL,
489 userdata, dont_decode);
492 end_parser: /* free the buffers! end the oppression!! */
499 phree(content_type_name);
500 phree(content_disposition_name);
508 * Entry point for the MIME parser.
509 * (This function expects to be fed HEADERS + CONTENT)
510 * Note: NULL can be supplied as content_end; in this case, the message is
511 * considered to have ended when the parser encounters a 0x00 byte.
513 void mime_parser(char *content_start,
527 void (*PreMultiPartCallBack)
538 void (*PostMultiPartCallBack)
554 the_mime_parser("", content_start, content_end,
556 PreMultiPartCallBack,
557 PostMultiPartCallBack,
558 userdata, dont_decode);