* Renamed "dynloader" to "serv_extensions" globally. We don't want people
[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-2002 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         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 *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                    size_t cblength,
148                    char *cbencoding,
149                    void *cbuserdata),
150                  void (*PreMultiPartCallBack)
151                   (char *cbname,
152                    char *cbfilename,
153                    char *cbpartnum,
154                    char *cbdisp,
155                    void *cbcontent,
156                    char *cbtype,
157                    size_t cblength,
158                    char *cbencoding,
159                    void *cbuserdata),
160                  void (*PostMultiPartCallBack)
161                   (char *cbname,
162                    char *cbfilename,
163                    char *cbpartnum,
164                    char *cbdisp,
165                    void *cbcontent,
166                    char *cbtype,
167                    size_t cblength,
168                    char *cbencoding,
169                    void *cbuserdata),
170                   void *userdata,
171                   int dont_decode
172 )
173 {
174
175         char *decoded;
176         size_t bytes_decoded = 0;
177
178         /* Some encodings aren't really encodings */
179         if (!strcasecmp(encoding, "7bit"))
180                 strcpy(encoding, "");
181         if (!strcasecmp(encoding, "8bit"))
182                 strcpy(encoding, "");
183         if (!strcasecmp(encoding, "binary"))
184                 strcpy(encoding, "");
185
186         /* If this part is not encoded, send as-is */
187         if ( (strlen(encoding) == 0) || (dont_decode)) {
188                 if (CallBack != NULL) {
189                         CallBack(name, filename, fixed_partnum(partnum),
190                                 disposition, part_start,
191                                 content_type, length, encoding, userdata);
192                         }
193                 return;
194         }
195         
196         if ((strcasecmp(encoding, "base64"))
197             && (strcasecmp(encoding, "quoted-printable"))) {
198                 return;
199         }
200         /*
201          * Allocate a buffer for the decoded data.  The output buffer is the
202          * same size as the input buffer; this assumes that the decoded data
203          * will never be larger than the encoded data.  This is a safe
204          * assumption with base64, uuencode, and quoted-printable.
205          */
206         decoded = mallok(length+2048);
207         if (decoded == NULL) {
208                 return;
209         }
210
211         if (!strcasecmp(encoding, "base64")) {
212                 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
213         }
214         else if (!strcasecmp(encoding, "quoted-printable")) {
215                 bytes_decoded = CtdlDecodeQuotedPrintable(decoded,
216                                                         part_start, length);
217         }
218
219         if (bytes_decoded > 0) if (CallBack != NULL) {
220                 CallBack(name, filename, fixed_partnum(partnum),
221                         disposition, decoded,
222                         content_type, bytes_decoded, "binary", userdata);
223         }
224
225         phree(decoded);
226 }
227
228 /*
229  * Break out the components of a multipart message
230  * (This function expects to be fed HEADERS + CONTENT)
231  * Note: NULL can be supplied as content_end; in this case, the message is
232  * considered to have ended when the parser encounters a 0x00 byte.
233  */
234 void the_mime_parser(char *partnum,
235                      char *content_start, char *content_end,
236                      void (*CallBack)
237                       (char *cbname,
238                        char *cbfilename,
239                        char *cbpartnum,
240                        char *cbdisp,
241                        void *cbcontent,
242                        char *cbtype,
243                        size_t cblength,
244                        char *cbencoding,
245                        void *cbuserdata),
246                      void (*PreMultiPartCallBack)
247                       (char *cbname,
248                        char *cbfilename,
249                        char *cbpartnum,
250                        char *cbdisp,
251                        void *cbcontent,
252                        char *cbtype,
253                        size_t cblength,
254                        char *cbencoding,
255                        void *cbuserdata),
256                      void (*PostMultiPartCallBack)
257                       (char *cbname,
258                        char *cbfilename,
259                        char *cbpartnum,
260                        char *cbdisp,
261                        void *cbcontent,
262                        char *cbtype,
263                        size_t cblength,
264                        char *cbencoding,
265                        void *cbuserdata),
266                       void *userdata,
267                       int dont_decode
268 )
269 {
270
271         char *ptr;
272         char *part_start, *part_end = NULL;
273         char buf[SIZ];
274         char *header;
275         char *boundary;
276         char *startary;
277         char *endary;
278         char *content_type;
279         size_t content_length;
280         char *encoding;
281         char *disposition;
282         char *name;
283         char *filename;
284         int is_multipart;
285         int part_seq = 0;
286         int i;
287         size_t length;
288         char nested_partnum[SIZ];
289
290         ptr = content_start;
291         content_length = 0;
292
293         boundary = mallok(SIZ);
294         memset(boundary, 0, SIZ);
295
296         startary = mallok(SIZ);
297         memset(startary, 0, SIZ);
298
299         endary = mallok(SIZ);
300         memset(endary, 0, SIZ);
301
302         header = mallok(SIZ);
303         memset(header, 0, SIZ);
304
305         content_type = mallok(SIZ);
306         memset(content_type, 0, SIZ);
307
308         encoding = mallok(SIZ);
309         memset(encoding, 0, SIZ);
310
311         name = mallok(SIZ);
312         memset(name, 0, SIZ);
313
314         filename = mallok(SIZ);
315         memset(filename, 0, SIZ);
316
317         disposition = mallok(SIZ);
318         memset(disposition, 0, SIZ);
319
320         /* If the caller didn't supply an endpointer, generate one by measure */
321         if (content_end == NULL) {
322                 content_end = &content_start[strlen(content_start)];
323         }
324
325         /* Learn interesting things from the headers */
326         strcpy(header, "");
327         do {
328                 ptr = memreadline(ptr, buf, SIZ);
329                 if (ptr >= content_end) {
330                         goto end_parser;
331                 }
332
333                 for (i = 0; i < strlen(buf); ++i)
334                         if (isspace(buf[i]))
335                                 buf[i] = ' ';
336                 if (!isspace(buf[0])) {
337                         if (!strncasecmp(header, "Content-type: ", 14)) {
338                                 strcpy(content_type, &header[14]);
339                                 extract_key(name, content_type, "name");
340                                 /* Deal with weird headers */
341                                 if (strchr(content_type, ' '))
342                                         *(strchr(content_type, ' ')) = '\0';
343                                 if (strchr(content_type, ';'))
344                                         *(strchr(content_type, ';')) = '\0';
345                         }
346                         if (!strncasecmp(header, "Content-Disposition: ", 21)) {
347                                 strcpy(disposition, &header[21]);
348                                 extract_key(filename, disposition, "filename");
349                         }
350                         if (!strncasecmp(header, "Content-length: ", 16)) {
351                                 content_length = (size_t) atol(&header[16]);
352                         }
353                         if (!strncasecmp(header,
354                                       "Content-transfer-encoding: ", 27))
355                                 strcpy(encoding, &header[27]);
356                         if (strlen(boundary) == 0)
357                                 extract_key(boundary, header, "boundary");
358                         strcpy(header, "");
359                 }
360                 if ((strlen(header) + strlen(buf) + 2) < SIZ)
361                         strcat(header, buf);
362         } while ((strlen(buf) > 0) && (*ptr != 0));
363
364         if (strchr(disposition, ';'))
365                 *(strchr(disposition, ';')) = '\0';
366         striplt(disposition);
367         if (strchr(content_type, ';'))
368                 *(strchr(content_type, ';')) = '\0';
369         striplt(content_type);
370
371         if (strlen(boundary) > 0) {
372                 is_multipart = 1;
373         } else {
374                 is_multipart = 0;
375         }
376
377         /* If this is a multipart message, then recursively process it */
378         part_start = NULL;
379         if (is_multipart) {
380
381                 /* Tell the client about this message's multipartedness */
382                 if (PreMultiPartCallBack != NULL) {
383                         PreMultiPartCallBack("", "", partnum, "",
384                                 NULL, content_type,
385                                 0, encoding, userdata);
386                 }
387
388                 /* Figure out where the boundaries are */
389                 snprintf(startary, SIZ, "--%s", boundary);
390                 snprintf(endary, SIZ, "--%s--", boundary);
391                 do {
392                         if ( (!strncasecmp(ptr, startary, strlen(startary)))
393                            || (!strncasecmp(ptr, endary, strlen(endary))) ) {
394                                 if (part_start != NULL) {
395                                         if (strlen(partnum) > 0) {
396                                                 snprintf(nested_partnum,
397                                                          sizeof nested_partnum,
398                                                          "%s.%d", partnum,
399                                                          ++part_seq);
400                                         }
401                                         else {
402                                                 snprintf(nested_partnum,
403                                                          sizeof nested_partnum,
404                                                          "%d", ++part_seq);
405                                         }
406                                         the_mime_parser(nested_partnum,
407                                                     part_start, part_end,
408                                                         CallBack,
409                                                         PreMultiPartCallBack,
410                                                         PostMultiPartCallBack,
411                                                         userdata,
412                                                         dont_decode);
413                                 }
414                                 ptr = memreadline(ptr, buf, SIZ);
415                                 part_start = ptr;
416                         }
417                         else {
418                                 part_end = ptr;
419                                 ++ptr;
420                         }
421                         /* If we pass out of scope in the MIME multipart (by
422                          * hitting the end boundary), force the pointer out
423                          * of scope so this loop ends.
424                          */
425                         if (ptr < content_end) {
426                                 if (!strcasecmp(ptr, endary)) {
427                                         ptr = content_end++;
428                                 }
429                         }
430                 } while (ptr <= content_end);
431                 if (PostMultiPartCallBack != NULL) {
432                         PostMultiPartCallBack("", "", partnum, "", NULL,
433                                 content_type, 0, encoding, userdata);
434                 }
435                 goto end_parser;
436         }
437
438         /* If it's not a multipart message, then do something with it */
439         if (!is_multipart) {
440                 part_start = ptr;
441                 length = 0;
442                 while (ptr < content_end) {
443                         ++ptr;
444                         ++length;
445                 }
446                 part_end = content_end;
447                 
448                 /* Truncate if the header told us to */
449                 if ( (content_length > 0) && (length > content_length) ) {
450                         length = content_length;
451                 }
452                 
453                 mime_decode(partnum,
454                             part_start, length,
455                             content_type, encoding, disposition,
456                             name, filename,
457                             CallBack, NULL, NULL,
458                             userdata, dont_decode);
459         }
460
461 end_parser:     /* free the buffers!  end the oppression!! */
462         phree(boundary);
463         phree(startary);
464         phree(endary);  
465         phree(header);
466         phree(content_type);
467         phree(encoding);
468         phree(name);
469         phree(filename);
470         phree(disposition);
471 }
472
473
474
475 /*
476  * Entry point for the MIME parser.
477  * (This function expects to be fed HEADERS + CONTENT)
478  * Note: NULL can be supplied as content_end; in this case, the message is
479  * considered to have ended when the parser encounters a 0x00 byte.
480  */
481 void mime_parser(char *content_start,
482                 char *content_end,
483
484                  void (*CallBack)
485                   (char *cbname,
486                    char *cbfilename,
487                    char *cbpartnum,
488                    char *cbdisp,
489                    void *cbcontent,
490                    char *cbtype,
491                    size_t cblength,
492                    char *cbencoding,
493                    void *cbuserdata),
494
495                  void (*PreMultiPartCallBack)
496                   (char *cbname,
497                    char *cbfilename,
498                    char *cbpartnum,
499                    char *cbdisp,
500                    void *cbcontent,
501                    char *cbtype,
502                    size_t cblength,
503                    char *cbencoding,
504                    void *cbuserdata),
505
506                  void (*PostMultiPartCallBack)
507                   (char *cbname,
508                    char *cbfilename,
509                    char *cbpartnum,
510                    char *cbdisp,
511                    void *cbcontent,
512                    char *cbtype,
513                    size_t cblength,
514                    char *cbencoding,
515                    void *cbuserdata),
516
517                   void *userdata,
518                   int dont_decode
519 )
520 {
521
522         the_mime_parser("", content_start, content_end,
523                         CallBack,
524                         PreMultiPartCallBack,
525                         PostMultiPartCallBack,
526                         userdata, dont_decode);
527 }