]> code.citadel.org Git - citadel.git/blob - citadel/mime_parser.c
* Updated the MIME parser API to include the "charset" portion of
[citadel.git] / citadel / mime_parser.c
1 /*
2  * $Id$
3  *
4  * This is the MIME parser for Citadel.  Sometimes it actually works.
5  *
6  * Copyright (c) 1998-2005 by Art Cancro
7  * This code is distributed under the terms of the GNU General Public License.
8  *
9  */
10
11 #ifdef DLL_EXPORT
12 #define IN_LIBCIT
13 #endif
14
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <signal.h>
19 #include <sys/types.h>
20 #include <ctype.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <errno.h>
24
25 #include "citadel.h"
26 #include "server.h"
27 #include "serv_extensions.h"
28 #include "sysdep_decls.h"
29 #include "tools.h"
30
31 #include "mime_parser.h"
32
33
34 void extract_key(char *target, char *source, char *key)
35 {
36         int a, b;
37
38         strcpy(target, source);
39         for (a = 0; a < strlen(target); ++a) {
40                 if ((!strncasecmp(&target[a], key, strlen(key)))
41                     && (target[a + strlen(key)] == '=')) {
42                         strcpy(target, &target[a + strlen(key) + 1]);
43                         if (target[0] == 34)
44                                 strcpy(target, &target[1]);
45                         for (b = 0; b < strlen(target); ++b)
46                                 if (target[b] == 34)
47                                         target[b] = 0;
48                         return;
49                 }
50         }
51         strcpy(target, "");
52 }
53
54
55 /*
56  * For non-multipart messages, we need to generate a quickie partnum of "1"
57  * to return to callback functions.  Some callbacks demand it.
58  */
59 char *fixed_partnum(char *supplied_partnum) {
60         if (supplied_partnum == NULL) return "1";
61         if (strlen(supplied_partnum)==0) return "1";
62         return supplied_partnum;
63 }
64
65
66
67 /*
68  * Convert "quoted-printable" to binary.  Returns number of bytes decoded.
69  */
70 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
71         char buf[SIZ];
72         int buf_length = 0;
73         int soft_line_break = 0;
74         unsigned int ch;
75         int decoded_length = 0;
76         int i;
77
78         decoded[0] = 0;
79         decoded_length = 0;
80         buf[0] = 0;
81         buf_length = 0;
82
83         for (i = 0; i < sourcelen; ++i) {
84
85                 buf[buf_length++] = encoded[i];
86
87                 if ( (encoded[i] == '\n')
88                    || (encoded[i] == 0)
89                    || (i == (sourcelen-1)) ) {
90                         buf[buf_length++] = 0;
91
92                         /*** begin -- process one line ***/
93
94                         if (buf[strlen(buf)-1] == '\n') {
95                                 buf[strlen(buf)-1] = 0;
96                         }
97                         if (buf[strlen(buf)-1] == '\r') {
98                                 buf[strlen(buf)-1] = 0;
99                         }
100                         while (isspace(buf[strlen(buf)-1])) {
101                                 buf[strlen(buf)-1] = 0;
102                         }
103                         soft_line_break = 0;
104
105                         while (strlen(buf) > 0) {
106                                 if (!strcmp(buf, "=")) {
107                                         soft_line_break = 1;
108                                         strcpy(buf, "");
109                                 } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
110                                         sscanf(&buf[1], "%02x", &ch);
111                                         decoded[decoded_length++] = ch;
112                                         strcpy(buf, &buf[3]);
113                                 } else {
114                                         decoded[decoded_length++] = buf[0];
115                                         strcpy(buf, &buf[1]);
116                                 }
117                         }
118                         if (soft_line_break == 0) {
119                                 decoded[decoded_length++] = '\r';
120                                 decoded[decoded_length++] = '\n';
121                         }
122                         buf_length = 0;
123                         /*** end -- process one line ***/
124                 }
125         }
126
127         decoded[decoded_length++] = 0;
128         return(decoded_length);
129 }
130
131 /*
132  * Given a message or message-part body and a length, handle any necessary
133  * decoding and pass the request up the stack.
134  */
135 void mime_decode(char *partnum,
136                  char *part_start, size_t length,
137                  char *content_type, char *charset, char *encoding,
138                  char *disposition,
139                  char *name, char *filename,
140                  void (*CallBack)
141                   (char *cbname,
142                    char *cbfilename,
143                    char *cbpartnum,
144                    char *cbdisp,
145                    void *cbcontent,
146                    char *cbtype,
147                    char *cbcharset,
148                    size_t cblength,
149                    char *cbencoding,
150                    void *cbuserdata),
151                  void (*PreMultiPartCallBack)
152                   (char *cbname,
153                    char *cbfilename,
154                    char *cbpartnum,
155                    char *cbdisp,
156                    void *cbcontent,
157                    char *cbtype,
158                    char *cbcharset,
159                    size_t cblength,
160                    char *cbencoding,
161                    void *cbuserdata),
162                  void (*PostMultiPartCallBack)
163                   (char *cbname,
164                    char *cbfilename,
165                    char *cbpartnum,
166                    char *cbdisp,
167                    void *cbcontent,
168                    char *cbtype,
169                    char *cbcharset,
170                    size_t cblength,
171                    char *cbencoding,
172                    void *cbuserdata),
173                   void *userdata,
174                   int dont_decode
175 )
176 {
177
178         char *decoded;
179         size_t bytes_decoded = 0;
180
181         /* Some encodings aren't really encodings */
182         if (!strcasecmp(encoding, "7bit"))
183                 strcpy(encoding, "");
184         if (!strcasecmp(encoding, "8bit"))
185                 strcpy(encoding, "");
186         if (!strcasecmp(encoding, "binary"))
187                 strcpy(encoding, "");
188
189         /* If this part is not encoded, send as-is */
190         if ( (strlen(encoding) == 0) || (dont_decode)) {
191                 if (CallBack != NULL) {
192                         CallBack(name, filename, fixed_partnum(partnum),
193                                 disposition, part_start,
194                                 content_type, charset, length, encoding, userdata);
195                         }
196                 return;
197         }
198         
199         if ((strcasecmp(encoding, "base64"))
200             && (strcasecmp(encoding, "quoted-printable"))) {
201                 return;
202         }
203         /*
204          * Allocate a buffer for the decoded data.  The output buffer is the
205          * same size as the input buffer; this assumes that the decoded data
206          * will never be larger than the encoded data.  This is a safe
207          * assumption with base64, uuencode, and quoted-printable.
208          */
209         decoded = malloc(length+2048);
210         if (decoded == NULL) {
211                 return;
212         }
213
214         if (!strcasecmp(encoding, "base64")) {
215                 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
216         }
217         else if (!strcasecmp(encoding, "quoted-printable")) {
218                 bytes_decoded = CtdlDecodeQuotedPrintable(decoded,
219                                                         part_start, length);
220         }
221
222         if (bytes_decoded > 0) if (CallBack != NULL) {
223                 CallBack(name, filename, fixed_partnum(partnum),
224                         disposition, decoded,
225                         content_type, charset, bytes_decoded, "binary", userdata);
226         }
227
228         free(decoded);
229 }
230
231 /*
232  * Break out the components of a multipart message
233  * (This function expects to be fed HEADERS + CONTENT)
234  * Note: NULL can be supplied as content_end; in this case, the message is
235  * considered to have ended when the parser encounters a 0x00 byte.
236  */
237 void the_mime_parser(char *partnum,
238                      char *content_start, char *content_end,
239                      void (*CallBack)
240                       (char *cbname,
241                        char *cbfilename,
242                        char *cbpartnum,
243                        char *cbdisp,
244                        void *cbcontent,
245                        char *cbtype,
246                        char *cbcharset,
247                        size_t cblength,
248                        char *cbencoding,
249                        void *cbuserdata),
250                      void (*PreMultiPartCallBack)
251                       (char *cbname,
252                        char *cbfilename,
253                        char *cbpartnum,
254                        char *cbdisp,
255                        void *cbcontent,
256                        char *cbtype,
257                        char *cbcharset,
258                        size_t cblength,
259                        char *cbencoding,
260                        void *cbuserdata),
261                      void (*PostMultiPartCallBack)
262                       (char *cbname,
263                        char *cbfilename,
264                        char *cbpartnum,
265                        char *cbdisp,
266                        void *cbcontent,
267                        char *cbtype,
268                        char *cbcharset,
269                        size_t cblength,
270                        char *cbencoding,
271                        void *cbuserdata),
272                       void *userdata,
273                       int dont_decode
274 )
275 {
276
277         char *ptr;
278         char *part_start, *part_end = NULL;
279         char buf[SIZ];
280         char *header;
281         char *boundary;
282         char *startary;
283         char *endary;
284         char *next_boundary;
285         char *content_type;
286         char *charset;
287         size_t content_length;
288         char *encoding;
289         char *disposition;
290         char *name = NULL;
291         char *content_type_name;
292         char *content_disposition_name;
293         char *filename;
294         int is_multipart;
295         int part_seq = 0;
296         int i;
297         size_t length;
298         char nested_partnum[SIZ];
299
300         ptr = content_start;
301         content_length = 0;
302
303         boundary = malloc(SIZ);
304         memset(boundary, 0, SIZ);
305
306         startary = malloc(SIZ);
307         memset(startary, 0, SIZ);
308
309         endary = malloc(SIZ);
310         memset(endary, 0, SIZ);
311
312         header = malloc(SIZ);
313         memset(header, 0, SIZ);
314
315         content_type = malloc(SIZ);
316         memset(content_type, 0, SIZ);
317
318         charset = malloc(SIZ);
319         memset(charset, 0, SIZ);
320
321         encoding = malloc(SIZ);
322         memset(encoding, 0, SIZ);
323
324         content_type_name = malloc(SIZ);
325         memset(content_type_name, 0, SIZ);
326
327         content_disposition_name = malloc(SIZ);
328         memset(content_disposition_name, 0, SIZ);
329
330         filename = malloc(SIZ);
331         memset(filename, 0, SIZ);
332
333         disposition = malloc(SIZ);
334         memset(disposition, 0, SIZ);
335
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)];
339         }
340
341         /* Learn interesting things from the headers */
342         strcpy(header, "");
343         do {
344                 ptr = memreadline(ptr, buf, SIZ);
345                 if (ptr >= content_end) {
346                         goto end_parser;
347                 }
348
349                 for (i = 0; i < strlen(buf); ++i) {
350                         if (isspace(buf[i])) {
351                                 buf[i] = ' ';
352                         }
353                 }
354
355                 if (!isspace(buf[0])) {
356                         if (!strncasecmp(header, "Content-type: ", 14)) {
357                                 strcpy(content_type, &header[14]);
358                                 extract_key(content_type_name, content_type, "name");
359                                 extract_key(charset, content_type, "charset");
360                                 /* Deal with weird headers */
361                                 if (strchr(content_type, ' '))
362                                         *(strchr(content_type, ' ')) = '\0';
363                                 if (strchr(content_type, ';'))
364                                         *(strchr(content_type, ';')) = '\0';
365                         }
366                         if (!strncasecmp(header, "Content-Disposition: ", 21)) {
367                                 strcpy(disposition, &header[21]);
368                                 extract_key(content_disposition_name, disposition, "name");
369                                 extract_key(filename, disposition, "filename");
370                         }
371                         if (!strncasecmp(header, "Content-length: ", 16)) {
372                                 content_length = (size_t) atol(&header[16]);
373                         }
374                         if (!strncasecmp(header,
375                                       "Content-transfer-encoding: ", 27))
376                                 strcpy(encoding, &header[27]);
377                         if (strlen(boundary) == 0)
378                                 extract_key(boundary, header, "boundary");
379                         strcpy(header, "");
380                 }
381                 if ((strlen(header) + strlen(buf) + 2) < SIZ)
382                         strcat(header, buf);
383         } while ((strlen(buf) > 0) && (*ptr != 0));
384
385         if (strchr(disposition, ';'))
386                 *(strchr(disposition, ';')) = '\0';
387         striplt(disposition);
388         if (strchr(content_type, ';'))
389                 *(strchr(content_type, ';')) = '\0';
390         striplt(content_type);
391
392         if (!strlen(content_type)) {
393                 strcpy(content_type, "text/plain");
394         }
395         if (!strlen(charset)) {
396                 strcpy(charset, "us-ascii");
397         }
398
399         if (strlen(boundary) > 0) {
400                 is_multipart = 1;
401         } else {
402                 is_multipart = 0;
403         }
404
405         /* If this is a multipart message, then recursively process it */
406         part_start = NULL;
407         if (is_multipart) {
408
409                 /* Tell the client about this message's multipartedness */
410                 if (PreMultiPartCallBack != NULL) {
411                         PreMultiPartCallBack("", "", partnum, "",
412                                 NULL, content_type, charset,
413                                 0, encoding, userdata);
414                 }
415
416                 /* Figure out where the boundaries are */
417                 snprintf(startary, SIZ, "--%s", boundary);
418                 snprintf(endary, SIZ, "--%s--", boundary);
419
420                 part_start = NULL;
421                 do {
422                         next_boundary = bmstrstr(ptr, startary, strncmp);
423                         if ( (part_start != NULL) && (next_boundary != NULL) ) {
424                                 part_end = next_boundary;
425                                 --part_end;
426
427                                 if (strlen(partnum) > 0) {
428                                         snprintf(nested_partnum,
429                                                  sizeof nested_partnum,
430                                                  "%s.%d", partnum,
431                                                  ++part_seq);
432                                 }
433                                 else {
434                                         snprintf(nested_partnum,
435                                                  sizeof nested_partnum,
436                                                  "%d", ++part_seq);
437                                 }
438                                 the_mime_parser(nested_partnum,
439                                             part_start, part_end,
440                                                 CallBack,
441                                                 PreMultiPartCallBack,
442                                                 PostMultiPartCallBack,
443                                                 userdata,
444                                                 dont_decode);
445                         }
446
447                         if (next_boundary != NULL) {
448                                 /* If we pass out of scope, don't attempt to read
449                                  * past the end boundary. */
450                                 if (!strcmp(next_boundary, endary)) {
451                                         ptr = content_end;
452                                 }
453                                 else {
454                                         /* Set up for the next part. */
455                                         part_start = strstr(next_boundary, "\n");
456                                         ++part_start;
457                                         ptr = part_start;
458                                 }
459                         }
460                         else {
461                                 /* Invalid end of multipart.  Bail out! */
462                                 ptr = content_end;
463                         }
464                 } while ( (ptr < content_end) && (next_boundary != NULL) );
465
466                 if (PostMultiPartCallBack != NULL) {
467                         PostMultiPartCallBack("", "", partnum, "", NULL,
468                                 content_type, charset, 0, encoding, userdata);
469                 }
470                 goto end_parser;
471         }
472
473         /* If it's not a multipart message, then do something with it */
474         if (!is_multipart) {
475                 part_start = ptr;
476                 length = 0;
477                 while (ptr < content_end) {
478                         ++ptr;
479                         ++length;
480                 }
481                 part_end = content_end;
482                 /* fix an off-by-one error */
483                 --part_end;
484                 --length;
485                 
486                 /* Truncate if the header told us to */
487                 if ( (content_length > 0) && (length > content_length) ) {
488                         length = content_length;
489                 }
490
491                 /* Sometimes the "name" field is tacked on to Content-type,
492                  * and sometimes it's tacked on to Content-disposition.  Use
493                  * whichever one we have.
494                  */
495                 if (strlen(content_disposition_name) > strlen(content_type_name)) {
496                         name = content_disposition_name;
497                 }
498                 else {
499                         name = content_type_name;
500                 }
501                 
502                 mime_decode(partnum,
503                             part_start, length,
504                             content_type, charset, encoding, disposition,
505                             name, filename,
506                             CallBack, NULL, NULL,
507                             userdata, dont_decode);
508         }
509
510 end_parser:     /* free the buffers!  end the oppression!! */
511         free(boundary);
512         free(startary);
513         free(endary);   
514         free(header);
515         free(content_type);
516         free(charset);
517         free(encoding);
518         free(content_type_name);
519         free(content_disposition_name);
520         free(filename);
521         free(disposition);
522 }
523
524
525
526 /*
527  * Entry point for the MIME parser.
528  * (This function expects to be fed HEADERS + CONTENT)
529  * Note: NULL can be supplied as content_end; in this case, the message is
530  * considered to have ended when the parser encounters a 0x00 byte.
531  */
532 void mime_parser(char *content_start,
533                 char *content_end,
534
535                  void (*CallBack)
536                   (char *cbname,
537                    char *cbfilename,
538                    char *cbpartnum,
539                    char *cbdisp,
540                    void *cbcontent,
541                    char *cbtype,
542                    char *cbcharset,
543                    size_t cblength,
544                    char *cbencoding,
545                    void *cbuserdata),
546
547                  void (*PreMultiPartCallBack)
548                   (char *cbname,
549                    char *cbfilename,
550                    char *cbpartnum,
551                    char *cbdisp,
552                    void *cbcontent,
553                    char *cbtype,
554                    char *cbcharset,
555                    size_t cblength,
556                    char *cbencoding,
557                    void *cbuserdata),
558
559                  void (*PostMultiPartCallBack)
560                   (char *cbname,
561                    char *cbfilename,
562                    char *cbpartnum,
563                    char *cbdisp,
564                    void *cbcontent,
565                    char *cbtype,
566                    char *cbcharset,
567                    size_t cblength,
568                    char *cbencoding,
569                    void *cbuserdata),
570
571                   void *userdata,
572                   int dont_decode
573 )
574 {
575
576         the_mime_parser("", content_start, content_end,
577                         CallBack,
578                         PreMultiPartCallBack,
579                         PostMultiPartCallBack,
580                         userdata, dont_decode);
581 }