]> code.citadel.org Git - citadel.git/blob - citadel/mime_parser.c
Rewrote the quoted-printable decoder again. The previous
[citadel.git] / citadel / mime_parser.c
1 /*
2  * $Id$
3  *
4  * This is the MIME parser for Citadel.
5  *
6  * Copyright (c) 1998-2006 by Art Cancro
7  * This code is distributed under the GNU General Public License v2.
8  *
9  */
10
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <sys/stat.h>
19 #include <errno.h>
20
21 #include "citadel.h"
22 #include "server.h"
23 #include "serv_extensions.h"
24 #include "sysdep_decls.h"
25 #include "tools.h"
26
27 #include "mime_parser.h"
28
29
30 void extract_key(char *target, char *source, char *key)
31 {
32         int a, b;
33
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]);
39                         if (target[0] == 34)
40                                 strcpy(target, &target[1]);
41                         for (b = 0; b < strlen(target); ++b)
42                                 if (target[b] == 34)
43                                         target[b] = 0;
44                         return;
45                 }
46         }
47         strcpy(target, "");
48 }
49
50
51 /*
52  * For non-multipart messages, we need to generate a quickie partnum of "1"
53  * to return to callback functions.  Some callbacks demand it.
54  */
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;
59 }
60
61
62
63 /*
64  * Convert "quoted-printable" to binary.  Returns number of bytes decoded.
65  * according to RFC2045 section 6.7
66  */
67 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
68         unsigned int ch;
69         int decoded_length = 0;
70         int pos = 0;
71
72         while (pos < sourcelen)
73         {
74                 if (!strncmp(&encoded[pos], "=\r\n", 3))
75                 {
76                         pos += 3;
77                 }
78                 else if (!strncmp(&encoded[pos], "=\n", 2))
79                 {
80                         pos += 2;
81                 }
82                 else if (encoded[pos] == '=')
83                 {
84                         ch = 0;
85                         sscanf(&encoded[pos+1], "%02x", &ch);
86                         pos += 3;
87                         decoded[decoded_length++] = ch;
88                 }
89                 else
90                 {
91                         decoded[decoded_length++] = encoded[pos];
92                         pos += 1;
93                 }
94         }
95         decoded[decoded_length] = 0;
96         return(decoded_length);
97 }
98
99
100 /*
101  * Given a message or message-part body and a length, handle any necessary
102  * decoding and pass the request up the stack.
103  */
104 void mime_decode(char *partnum,
105                  char *part_start, size_t length,
106                  char *content_type, char *charset, char *encoding,
107                  char *disposition,
108                  char *name, char *filename,
109                  void (*CallBack)
110                   (char *cbname,
111                    char *cbfilename,
112                    char *cbpartnum,
113                    char *cbdisp,
114                    void *cbcontent,
115                    char *cbtype,
116                    char *cbcharset,
117                    size_t cblength,
118                    char *cbencoding,
119                    void *cbuserdata),
120                  void (*PreMultiPartCallBack)
121                   (char *cbname,
122                    char *cbfilename,
123                    char *cbpartnum,
124                    char *cbdisp,
125                    void *cbcontent,
126                    char *cbtype,
127                    char *cbcharset,
128                    size_t cblength,
129                    char *cbencoding,
130                    void *cbuserdata),
131                  void (*PostMultiPartCallBack)
132                   (char *cbname,
133                    char *cbfilename,
134                    char *cbpartnum,
135                    char *cbdisp,
136                    void *cbcontent,
137                    char *cbtype,
138                    char *cbcharset,
139                    size_t cblength,
140                    char *cbencoding,
141                    void *cbuserdata),
142                   void *userdata,
143                   int dont_decode
144 )
145 {
146
147         char *decoded;
148         size_t bytes_decoded = 0;
149
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, "");
157
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);
164                         }
165                 return;
166         }
167         
168         /* Fail silently if we hit an unknown encoding. */
169         if ((strcasecmp(encoding, "base64"))
170             && (strcasecmp(encoding, "quoted-printable"))) {
171                 return;
172         }
173
174         /*
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.
179          */
180         decoded = malloc(length + 32768);
181         if (decoded == NULL) {
182                 return;
183         }
184
185         if (!strcasecmp(encoding, "base64")) {
186                 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
187         }
188         else if (!strcasecmp(encoding, "quoted-printable")) {
189                 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
190         }
191
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);
196         }
197
198         free(decoded);
199 }
200
201 /*
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.
206  */
207 void the_mime_parser(char *partnum,
208                      char *content_start, char *content_end,
209                      void (*CallBack)
210                       (char *cbname,
211                        char *cbfilename,
212                        char *cbpartnum,
213                        char *cbdisp,
214                        void *cbcontent,
215                        char *cbtype,
216                        char *cbcharset,
217                        size_t cblength,
218                        char *cbencoding,
219                        void *cbuserdata),
220                      void (*PreMultiPartCallBack)
221                       (char *cbname,
222                        char *cbfilename,
223                        char *cbpartnum,
224                        char *cbdisp,
225                        void *cbcontent,
226                        char *cbtype,
227                        char *cbcharset,
228                        size_t cblength,
229                        char *cbencoding,
230                        void *cbuserdata),
231                      void (*PostMultiPartCallBack)
232                       (char *cbname,
233                        char *cbfilename,
234                        char *cbpartnum,
235                        char *cbdisp,
236                        void *cbcontent,
237                        char *cbtype,
238                        char *cbcharset,
239                        size_t cblength,
240                        char *cbencoding,
241                        void *cbuserdata),
242                       void *userdata,
243                       int dont_decode
244 )
245 {
246
247         char *ptr;
248         char *srch = NULL;
249         char *part_start, *part_end = NULL;
250         char buf[SIZ];
251         char *header;
252         char *boundary;
253         char *startary;
254         size_t startary_len = 0;
255         char *endary;
256         char *next_boundary;
257         char *content_type;
258         char *charset;
259         size_t content_length;
260         char *encoding;
261         char *disposition;
262         char *name = NULL;
263         char *content_type_name;
264         char *content_disposition_name;
265         char *filename;
266         int is_multipart;
267         int part_seq = 0;
268         int i;
269         size_t length;
270         char nested_partnum[SIZ];
271
272         ptr = content_start;
273         content_length = 0;
274
275         boundary = malloc(SIZ);
276         memset(boundary, 0, SIZ);
277
278         startary = malloc(SIZ);
279         memset(startary, 0, SIZ);
280
281         endary = malloc(SIZ);
282         memset(endary, 0, SIZ);
283
284         header = malloc(SIZ);
285         memset(header, 0, SIZ);
286
287         content_type = malloc(SIZ);
288         memset(content_type, 0, SIZ);
289
290         charset = malloc(SIZ);
291         memset(charset, 0, SIZ);
292
293         encoding = malloc(SIZ);
294         memset(encoding, 0, SIZ);
295
296         content_type_name = malloc(SIZ);
297         memset(content_type_name, 0, SIZ);
298
299         content_disposition_name = malloc(SIZ);
300         memset(content_disposition_name, 0, SIZ);
301
302         filename = malloc(SIZ);
303         memset(filename, 0, SIZ);
304
305         disposition = malloc(SIZ);
306         memset(disposition, 0, SIZ);
307
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)];
311         }
312
313         /* Learn interesting things from the headers */
314         strcpy(header, "");
315         do {
316                 ptr = memreadline(ptr, buf, SIZ);
317                 if (ptr >= content_end) {
318                         goto end_parser;
319                 }
320
321                 for (i = 0; i < strlen(buf); ++i) {
322                         if (isspace(buf[i])) {
323                                 buf[i] = ' ';
324                         }
325                 }
326
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';
337                         }
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");
342                         }
343                         if (!strncasecmp(header, "Content-length: ", 16)) {
344                                 content_length = (size_t) atol(&header[16]);
345                         }
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");
351                         strcpy(header, "");
352                 }
353                 if ((strlen(header) + strlen(buf) + 2) < SIZ)
354                         strcat(header, buf);
355         } while ((strlen(buf) > 0) && (*ptr != 0));
356
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);
363
364         if (strlen(boundary) > 0) {
365                 is_multipart = 1;
366         } else {
367                 is_multipart = 0;
368         }
369
370         /* If this is a multipart message, then recursively process it */
371         part_start = NULL;
372         if (is_multipart) {
373
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);
379                 }
380
381                 /* Figure out where the boundaries are */
382                 snprintf(startary, SIZ, "--%s", boundary);
383                 snprintf(endary, SIZ, "--%s--", boundary);
384                 startary_len = strlen(startary);
385
386                 part_start = NULL;
387                 do {
388                         next_boundary = NULL;
389                         for (srch=ptr; srch<content_end; ++srch) {
390                                 if (!memcmp(srch, startary, startary_len)) {
391                                         next_boundary = srch;
392                                         srch = content_end;
393                                 }
394                         }
395
396                         if ( (part_start != NULL) && (next_boundary != NULL) ) {
397                                 part_end = next_boundary;
398                                 --part_end;
399
400                                 if (strlen(partnum) > 0) {
401                                         snprintf(nested_partnum,
402                                                  sizeof nested_partnum,
403                                                  "%s.%d", partnum,
404                                                  ++part_seq);
405                                 }
406                                 else {
407                                         snprintf(nested_partnum,
408                                                  sizeof nested_partnum,
409                                                  "%d", ++part_seq);
410                                 }
411                                 the_mime_parser(nested_partnum,
412                                             part_start, part_end,
413                                                 CallBack,
414                                                 PreMultiPartCallBack,
415                                                 PostMultiPartCallBack,
416                                                 userdata,
417                                                 dont_decode);
418                         }
419
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)) {
424                                         ptr = content_end;
425                                 }
426                                 else {
427                                         /* Set up for the next part. */
428                                         part_start = strstr(next_boundary, "\n");
429                                         ++part_start;
430                                         ptr = part_start;
431                                 }
432                         }
433                         else {
434                                 /* Invalid end of multipart.  Bail out! */
435                                 ptr = content_end;
436                         }
437                 } while ( (ptr < content_end) && (next_boundary != NULL) );
438
439                 if (PostMultiPartCallBack != NULL) {
440                         PostMultiPartCallBack("", "", partnum, "", NULL,
441                                 content_type, charset, 0, encoding, userdata);
442                 }
443                 goto end_parser;
444         }
445
446         /* If it's not a multipart message, then do something with it */
447         if (!is_multipart) {
448                 part_start = ptr;
449                 length = 0;
450                 while (ptr < content_end) {
451                         ++ptr;
452                         ++length;
453                 }
454                 part_end = content_end;
455                 /* fix an off-by-one error */
456                 --part_end;
457                 --length;
458                 
459                 /* Truncate if the header told us to */
460                 if ( (content_length > 0) && (length > content_length) ) {
461                         length = content_length;
462                 }
463
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.
467                  */
468                 if (strlen(content_disposition_name) > strlen(content_type_name)) {
469                         name = content_disposition_name;
470                 }
471                 else {
472                         name = content_type_name;
473                 }
474         
475                 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
476                         partnum, length, content_type, charset, encoding); */
477
478                 /* Ok, we've got a non-multipart part here, so do something with it.
479                  */
480                 mime_decode(partnum,
481                         part_start, length,
482                         content_type, charset, encoding, disposition,
483                         name, filename,
484                         CallBack, NULL, NULL,
485                         userdata, dont_decode
486                 );
487
488                 /*
489                  * Now if it's an encapsulated message/rfc822 then we have to recurse into it
490                  */
491                 if (!strcasecmp(content_type, "message/rfc822")) {
492
493                         if (PreMultiPartCallBack != NULL) {
494                                 PreMultiPartCallBack("", "", partnum, "",
495                                         NULL, content_type, charset,
496                                         0, encoding, userdata);
497                         }
498                         if (CallBack != NULL) {
499                                 if (strlen(partnum) > 0) {
500                                         snprintf(nested_partnum,
501                                                  sizeof nested_partnum,
502                                                  "%s.%d", partnum,
503                                                  ++part_seq);
504                                 }
505                                 else {
506                                         snprintf(nested_partnum,
507                                                  sizeof nested_partnum,
508                                                  "%d", ++part_seq);
509                                 }
510                                 the_mime_parser(nested_partnum,
511                                         part_start, part_end,
512                                         CallBack,
513                                         PreMultiPartCallBack,
514                                         PostMultiPartCallBack,
515                                         userdata,
516                                         dont_decode
517                                 );
518                         }
519                         if (PostMultiPartCallBack != NULL) {
520                                 PostMultiPartCallBack("", "", partnum, "", NULL,
521                                         content_type, charset, 0, encoding, userdata);
522                         }
523
524
525                 }
526
527         }
528
529 end_parser:     /* free the buffers!  end the oppression!! */
530         free(boundary);
531         free(startary);
532         free(endary);   
533         free(header);
534         free(content_type);
535         free(charset);
536         free(encoding);
537         free(content_type_name);
538         free(content_disposition_name);
539         free(filename);
540         free(disposition);
541 }
542
543
544
545 /*
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.
550  */
551 void mime_parser(char *content_start,
552                 char *content_end,
553
554                  void (*CallBack)
555                   (char *cbname,
556                    char *cbfilename,
557                    char *cbpartnum,
558                    char *cbdisp,
559                    void *cbcontent,
560                    char *cbtype,
561                    char *cbcharset,
562                    size_t cblength,
563                    char *cbencoding,
564                    void *cbuserdata),
565
566                  void (*PreMultiPartCallBack)
567                   (char *cbname,
568                    char *cbfilename,
569                    char *cbpartnum,
570                    char *cbdisp,
571                    void *cbcontent,
572                    char *cbtype,
573                    char *cbcharset,
574                    size_t cblength,
575                    char *cbencoding,
576                    void *cbuserdata),
577
578                  void (*PostMultiPartCallBack)
579                   (char *cbname,
580                    char *cbfilename,
581                    char *cbpartnum,
582                    char *cbdisp,
583                    void *cbcontent,
584                    char *cbtype,
585                    char *cbcharset,
586                    size_t cblength,
587                    char *cbencoding,
588                    void *cbuserdata),
589
590                   void *userdata,
591                   int dont_decode
592 )
593 {
594
595         the_mime_parser("", content_start, content_end,
596                         CallBack,
597                         PreMultiPartCallBack,
598                         PostMultiPartCallBack,
599                         userdata, dont_decode);
600 }