4 * This is the MIME parser for Citadel. Sometimes it actually works.
6 * Copyright (c) 1998-2001 by Art Cancro
7 * This code is distributed under the terms of the GNU General Public License.
19 #include <sys/types.h>
26 #include "dynloader.h"
27 #include "sysdep_decls.h"
28 #include "mime_parser.h"
32 void extract_key(char *target, char *source, char *key)
36 strcpy(target, source);
37 for (a = 0; a < strlen(target); ++a) {
38 if ((!strncasecmp(&target[a], key, strlen(key)))
39 && (target[a + strlen(key)] == '=')) {
40 strcpy(target, &target[a + strlen(key) + 1]);
42 strcpy(target, &target[1]);
43 for (b = 0; b < strlen(target); ++b)
54 * For non-multipart messages, we need to generate a quickie partnum of "1"
55 * to return to callback functions. Some callbacks demand it.
57 char *fixed_partnum(char *supplied_partnum) {
58 if (supplied_partnum == NULL) return "1";
59 if (strlen(supplied_partnum)==0) return "1";
60 return supplied_partnum;
65 * Given a message or message-part body and a length, handle any necessary
66 * decoding and pass the request up the stack.
68 void mime_decode(char *partnum,
69 char *part_start, size_t length,
70 char *content_type, char *encoding,
72 char *name, char *filename,
83 void (*PreMultiPartCallBack)
93 void (*PostMultiPartCallBack)
113 size_t bytes_sent = 0;
114 size_t bytes_recv = 0;
118 lprintf(9, "mime_decode() called\n");
120 /* Some encodings aren't really encodings */
121 if (!strcasecmp(encoding, "7bit"))
122 strcpy(encoding, "");
123 if (!strcasecmp(encoding, "8bit"))
124 strcpy(encoding, "");
125 if (!strcasecmp(encoding, "binary"))
126 strcpy(encoding, "");
128 /* If this part is not encoded, send as-is */
129 if ( (strlen(encoding) == 0) || (dont_decode)) {
130 if (CallBack != NULL) {
131 CallBack(name, filename, fixed_partnum(partnum),
132 disposition, part_start,
133 content_type, length, encoding, userdata);
137 if ((strcasecmp(encoding, "base64"))
138 && (strcasecmp(encoding, "quoted-printable"))) {
139 lprintf(9, "ERROR: unknown MIME encoding '%s'\n", encoding);
143 * Allocate a buffer for the decoded data. The output buffer is the
144 * same size as the input buffer; this assumes that the decoded data
145 * will never be larger than the encoded data. This is a safe
146 * assumption with base64, uuencode, and quoted-printable. Just to
147 * be safe, we still pad the buffer a bit.
149 decoded = malloc(length + 1024);
150 if (decoded == NULL) {
151 lprintf(9, "ERROR: cannot allocate memory.\n");
154 if (pipe(sendpipe) != 0)
156 if (pipe(recvpipe) != 0)
166 /* send stdio to the pipes */
167 if (dup2(sendpipe[0], 0) < 0)
168 lprintf(9, "ERROR dup2()\n");
169 if (dup2(recvpipe[1], 1) < 0)
170 lprintf(9, "ERROR dup2()\n");
171 close(sendpipe[1]); /* Close the ends we're not using */
173 if (!strcasecmp(encoding, "base64"))
174 execlp("./base64", "base64", "-d", NULL);
175 else if (!strcasecmp(encoding, "quoted-printable"))
176 execlp("./qpdecode", "qpdecode", NULL);
177 lprintf(9, "ERROR: cannot exec decoder for %s\n", encoding);
180 close(sendpipe[0]); /* Close the ends we're not using */
183 while ((bytes_sent < length) && (write_error == 0)) {
184 /* Empty the input pipe FIRST */
185 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
186 blocksize = read(recvpipe[0], &decoded[bytes_recv],
189 lprintf(9, "ERROR: cannot read from pipe\n");
191 bytes_recv = bytes_recv + blocksize;
193 /* Then put some data into the output pipe */
194 blocksize = length - bytes_sent;
195 if (blocksize > 2048)
197 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
198 lprintf(9, "ERROR: cannot write to pipe: %s\n",
202 bytes_sent = bytes_sent + blocksize;
205 /* Empty the input pipe */
206 while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
208 bytes_recv = bytes_recv + blocksize;
211 if (bytes_recv > 0) if (CallBack != NULL) {
212 CallBack(name, filename, fixed_partnum(partnum),
213 disposition, decoded,
214 content_type, bytes_recv, "binary", userdata);
221 * Break out the components of a multipart message
222 * (This function expects to be fed HEADERS + CONTENT)
223 * Note: NULL can be supplied as content_end; in this case, the message is
224 * considered to have ended when the parser encounters a 0x00 byte.
226 void the_mime_parser(char *partnum,
227 char *content_start, char *content_end,
238 void (*PreMultiPartCallBack)
248 void (*PostMultiPartCallBack)
264 char *part_start, *part_end = NULL;
270 char content_type[SIZ];
271 size_t content_length;
273 char disposition[SIZ];
280 char nested_partnum[SIZ];
282 lprintf(9, "the_mime_parser() called\n");
284 memset(boundary, 0, sizeof boundary);
285 memset(content_type, 0, sizeof content_type);
286 memset(encoding, 0, sizeof encoding);
287 memset(name, 0, sizeof name);
288 memset(filename, 0, sizeof filename);
289 memset(disposition, 0, sizeof disposition);
292 /* If the caller didn't supply an endpointer, generate one by measure */
293 if (content_end == NULL) {
294 content_end = &content_start[strlen(content_start)];
297 /* Learn interesting things from the headers */
300 ptr = memreadline(ptr, buf, sizeof buf);
301 if (ptr >= content_end)
304 for (i = 0; i < strlen(buf); ++i)
307 if (!isspace(buf[0])) {
308 if (!strncasecmp(header, "Content-type: ", 14)) {
309 strcpy(content_type, &header[14]);
310 extract_key(name, content_type, "name");
311 lprintf(9, "Extracted content-type <%s>\n",
314 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
315 strcpy(disposition, &header[21]);
316 extract_key(filename, disposition, "filename");
318 if (!strncasecmp(header, "Content-length: ", 16)) {
319 content_length = (size_t) atol(&header[16]);
321 if (!strncasecmp(header,
322 "Content-transfer-encoding: ", 27))
323 strcpy(encoding, &header[27]);
324 if (strlen(boundary) == 0)
325 extract_key(boundary, header, "boundary");
328 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
330 } while ((strlen(buf) > 0) && (*ptr != 0));
332 for (i = 0; i < strlen(disposition); ++i)
333 if (disposition[i] == ';')
335 while (isspace(disposition[0]))
336 strcpy(disposition, &disposition[1]);
337 for (i = 0; i < strlen(content_type); ++i)
338 if (content_type[i] == ';')
340 while (isspace(content_type[0]))
341 strcpy(content_type, &content_type[1]);
343 if (strlen(boundary) > 0) {
349 lprintf(9, "is_multipart=%d, boundary=<%s>\n",
350 is_multipart, boundary);
352 /* If this is a multipart message, then recursively process it */
356 /* Tell the client about this message's multipartedness */
357 if (PreMultiPartCallBack != NULL) {
358 PreMultiPartCallBack("", "", partnum, "",
360 0, encoding, userdata);
363 /* Figure out where the boundaries are */
364 sprintf(startary, "--%s", boundary);
365 sprintf(endary, "--%s--", boundary);
367 if ( (!strncasecmp(ptr, startary, strlen(startary)))
368 || (!strncasecmp(ptr, endary, strlen(endary))) ) {
369 lprintf(9, "hit boundary!\n");
370 if (part_start != NULL) {
371 if (strlen(partnum) > 0) {
372 sprintf(nested_partnum, "%s.%d",
373 partnum, ++part_seq);
376 sprintf(nested_partnum, "%d",
379 the_mime_parser(nested_partnum,
380 part_start, part_end,
382 PreMultiPartCallBack,
383 PostMultiPartCallBack,
387 ptr = memreadline(ptr, buf, sizeof(buf));
394 } while ( (strcasecmp(ptr, endary)) && (ptr <= content_end) );
395 if (PostMultiPartCallBack != NULL) {
396 PostMultiPartCallBack("", "", partnum, "", NULL,
397 content_type, 0, encoding, userdata);
402 /* If it's not a multipart message, then do something with it */
404 lprintf(9, "doing non-multipart thing\n");
407 while (ptr < content_end) {
411 part_end = content_end;
413 /* Truncate if the header told us to */
414 if ( (content_length > 0) && (length > content_length) ) {
415 length = content_length;
416 lprintf(9, "truncated to %d\n", content_length);
421 content_type, encoding, disposition,
423 CallBack, NULL, NULL,
424 userdata, dont_decode);
431 * Entry point for the MIME parser.
432 * (This function expects to be fed HEADERS + CONTENT)
433 * Note: NULL can be supplied as content_end; in this case, the message is
434 * considered to have ended when the parser encounters a 0x00 byte.
436 void mime_parser(char *content_start,
450 void (*PreMultiPartCallBack)
461 void (*PostMultiPartCallBack)
477 lprintf(9, "mime_parser() called\n");
478 the_mime_parser("", content_start, content_end,
480 PreMultiPartCallBack,
481 PostMultiPartCallBack,
482 userdata, dont_decode);