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.
15 #include <sys/types.h>
21 #include "sysdep_decls.h"
22 #include "mime_parser.h"
26 void extract_key(char *target, char *source, char *key)
30 strcpy(target, source);
31 for (a = 0; a < strlen(target); ++a) {
32 if ((!strncasecmp(&target[a], key, strlen(key)))
33 && (target[a + strlen(key)] == '=')) {
34 strcpy(target, &target[a + strlen(key) + 1]);
36 strcpy(target, &target[1]);
37 for (b = 0; b < strlen(target); ++b)
48 * For non-multipart messages, we need to generate a quickie partnum of "1"
49 * to return to callback functions. Some callbacks demand it.
51 char *fixed_partnum(char *supplied_partnum) {
52 if (supplied_partnum == NULL) return "1";
53 if (strlen(supplied_partnum)==0) return "1";
54 return supplied_partnum;
59 * Given a message or message-part body and a length, handle any necessary
60 * decoding and pass the request up the stack.
62 void mime_decode(char *partnum,
63 char *part_start, size_t length,
64 char *content_type, char *encoding,
66 char *name, char *filename,
77 void (*PreMultiPartCallBack)
87 void (*PostMultiPartCallBack)
107 size_t bytes_sent = 0;
108 size_t bytes_recv = 0;
112 lprintf(9, "mime_decode() called\n");
114 /* Some encodings aren't really encodings */
115 if (!strcasecmp(encoding, "7bit"))
116 strcpy(encoding, "");
117 if (!strcasecmp(encoding, "8bit"))
118 strcpy(encoding, "");
119 if (!strcasecmp(encoding, "binary"))
120 strcpy(encoding, "");
122 /* If this part is not encoded, send as-is */
123 if ( (strlen(encoding) == 0) || (dont_decode)) {
124 if (CallBack != NULL) {
125 CallBack(name, filename, fixed_partnum(partnum),
126 disposition, part_start,
127 content_type, length, encoding, userdata);
131 if ((strcasecmp(encoding, "base64"))
132 && (strcasecmp(encoding, "quoted-printable"))) {
133 lprintf(9, "ERROR: unknown MIME encoding '%s'\n", encoding);
137 * Allocate a buffer for the decoded data. The output buffer is the
138 * same size as the input buffer; this assumes that the decoded data
139 * will never be larger than the encoded data. This is a safe
140 * assumption with base64, uuencode, and quoted-printable. Just to
141 * be safe, we still pad the buffer a bit.
143 decoded = malloc(length + 1024);
144 if (decoded == NULL) {
145 lprintf(9, "ERROR: cannot allocate memory.\n");
148 if (pipe(sendpipe) != 0)
150 if (pipe(recvpipe) != 0)
160 /* send stdio to the pipes */
161 if (dup2(sendpipe[0], 0) < 0)
162 lprintf(9, "ERROR dup2()\n");
163 if (dup2(recvpipe[1], 1) < 0)
164 lprintf(9, "ERROR dup2()\n");
165 close(sendpipe[1]); /* Close the ends we're not using */
167 if (!strcasecmp(encoding, "base64"))
168 execlp("./base64", "base64", "-d", NULL);
169 else if (!strcasecmp(encoding, "quoted-printable"))
170 execlp("./qpdecode", "qpdecode", NULL);
171 lprintf(9, "ERROR: cannot exec decoder for %s\n", encoding);
174 close(sendpipe[0]); /* Close the ends we're not using */
177 while ((bytes_sent < length) && (write_error == 0)) {
178 /* Empty the input pipe FIRST */
179 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
180 blocksize = read(recvpipe[0], &decoded[bytes_recv],
183 lprintf(9, "ERROR: cannot read from pipe\n");
185 bytes_recv = bytes_recv + blocksize;
187 /* Then put some data into the output pipe */
188 blocksize = length - bytes_sent;
189 if (blocksize > 2048)
191 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
192 lprintf(9, "ERROR: cannot write to pipe: %s\n",
196 bytes_sent = bytes_sent + blocksize;
199 /* Empty the input pipe */
200 while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
202 bytes_recv = bytes_recv + blocksize;
205 if (bytes_recv > 0) if (CallBack != NULL) {
206 CallBack(name, filename, fixed_partnum(partnum),
207 disposition, decoded,
208 content_type, bytes_recv, "binary", userdata);
215 * Break out the components of a multipart message
216 * (This function expects to be fed HEADERS + CONTENT)
217 * Note: NULL can be supplied as content_end; in this case, the message is
218 * considered to have ended when the parser encounters a 0x00 byte.
220 void the_mime_parser(char *partnum,
221 char *content_start, char *content_end,
232 void (*PreMultiPartCallBack)
242 void (*PostMultiPartCallBack)
258 char *part_start, *part_end = NULL;
264 char content_type[SIZ];
265 size_t content_length;
267 char disposition[SIZ];
274 char nested_partnum[SIZ];
276 lprintf(9, "the_mime_parser() called\n");
278 memset(boundary, 0, sizeof boundary);
279 memset(content_type, 0, sizeof content_type);
280 memset(encoding, 0, sizeof encoding);
281 memset(name, 0, sizeof name);
282 memset(filename, 0, sizeof filename);
283 memset(disposition, 0, sizeof disposition);
286 /* If the caller didn't supply an endpointer, generate one by measure */
287 if (content_end == NULL) {
288 content_end = &content_start[strlen(content_start)];
291 /* Learn interesting things from the headers */
294 ptr = memreadline(ptr, buf, sizeof buf);
295 if (ptr >= content_end)
298 for (i = 0; i < strlen(buf); ++i)
301 if (!isspace(buf[0])) {
302 if (!strncasecmp(header, "Content-type: ", 14)) {
303 strcpy(content_type, &header[14]);
304 extract_key(name, content_type, "name");
306 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
307 strcpy(disposition, &header[21]);
308 extract_key(filename, disposition, "filename");
310 if (!strncasecmp(header, "Content-length: ", 16)) {
311 content_length = (size_t) atol(&header[16]);
313 if (!strncasecmp(header,
314 "Content-transfer-encoding: ", 27))
315 strcpy(encoding, &header[27]);
316 if (strlen(boundary) == 0)
317 extract_key(boundary, header, "boundary");
320 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
322 } while ((strlen(buf) > 0) && (*ptr != 0));
324 for (i = 0; i < strlen(disposition); ++i)
325 if (disposition[i] == ';')
327 while (isspace(disposition[0]))
328 strcpy(disposition, &disposition[1]);
329 for (i = 0; i < strlen(content_type); ++i)
330 if (content_type[i] == ';')
332 while (isspace(content_type[0]))
333 strcpy(content_type, &content_type[1]);
335 if (strlen(boundary) > 0) {
341 lprintf(9, "is_multipart=%d, boundary=<%s>\n",
342 is_multipart, boundary);
344 /* If this is a multipart message, then recursively process it */
348 /* Tell the client about this message's multipartedness */
349 if (PreMultiPartCallBack != NULL) {
350 PreMultiPartCallBack("", "", partnum, "",
352 0, encoding, userdata);
355 /* Figure out where the boundaries are */
356 sprintf(startary, "--%s", boundary);
357 sprintf(endary, "--%s--", boundary);
359 if ( (!strncasecmp(ptr, startary, strlen(startary)))
360 || (!strncasecmp(ptr, endary, strlen(endary))) ) {
361 lprintf(9, "hit boundary!\n");
362 if (part_start != NULL) {
363 if (strlen(partnum) > 0) {
364 sprintf(nested_partnum, "%s.%d",
365 partnum, ++part_seq);
368 sprintf(nested_partnum, "%d",
371 the_mime_parser(nested_partnum,
372 part_start, part_end,
374 PreMultiPartCallBack,
375 PostMultiPartCallBack,
379 ptr = memreadline(ptr, buf, sizeof(buf));
386 } while ( (strcasecmp(ptr, endary)) && (ptr <= content_end) );
387 if (PostMultiPartCallBack != NULL) {
388 PostMultiPartCallBack("", "", partnum, "", NULL,
389 content_type, 0, encoding, userdata);
394 /* If it's not a multipart message, then do something with it */
396 lprintf(9, "doing non-multipart thing\n");
399 while (ptr < content_end) {
403 part_end = content_end;
405 /* Truncate if the header told us to */
406 if ( (content_length > 0) && (length > content_length) ) {
407 length = content_length;
408 lprintf(9, "truncated to %d\n", content_length);
413 content_type, encoding, disposition,
415 CallBack, NULL, NULL,
416 userdata, dont_decode);
423 * Entry point for the MIME parser.
424 * (This function expects to be fed HEADERS + CONTENT)
425 * Note: NULL can be supplied as content_end; in this case, the message is
426 * considered to have ended when the parser encounters a 0x00 byte.
428 void mime_parser(char *content_start,
442 void (*PreMultiPartCallBack)
453 void (*PostMultiPartCallBack)
469 lprintf(9, "mime_parser() called\n");
470 the_mime_parser("", content_start, content_end,
472 PreMultiPartCallBack,
473 PostMultiPartCallBack,
474 userdata, dont_decode);