]> code.citadel.org Git - citadel.git/blob - webcit/mime_parser.c
* Brought over an update of the MIME parser from Citadel.
[citadel.git] / webcit / mime_parser.c
1 /*
2  * $Id$
3  *
4  * This is the MIME parser for Citadel.  Sometimes it actually works.
5  *
6  * Copyright (c) 1998-2001 by Art Cancro
7  * This code is distributed under the terms of the GNU General Public License.
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 #include <pthread.h>
21 #include "webcit.h"
22 #include "mime_parser.h"
23
24
25 void extract_key(char *target, char *source, char *key)
26 {
27         int a, b;
28
29         strcpy(target, source);
30         for (a = 0; a < strlen(target); ++a) {
31                 if ((!strncasecmp(&target[a], key, strlen(key)))
32                     && (target[a + strlen(key)] == '=')) {
33                         strcpy(target, &target[a + strlen(key) + 1]);
34                         if (target[0] == 34)
35                                 strcpy(target, &target[1]);
36                         for (b = 0; b < strlen(target); ++b)
37                                 if (target[b] == 34)
38                                         target[b] = 0;
39                         return;
40                 }
41         }
42         strcpy(target, "");
43 }
44
45
46 /*
47  * For non-multipart messages, we need to generate a quickie partnum of "1"
48  * to return to callback functions.  Some callbacks demand it.
49  */
50 char *fixed_partnum(char *supplied_partnum) {
51         if (supplied_partnum == NULL) return "1";
52         if (strlen(supplied_partnum)==0) return "1";
53         return supplied_partnum;
54 }
55
56
57
58 /*
59  * Convert "quoted-printable" to binary.  Returns number of bytes decoded.
60  */
61 int decode_quoted_printable(char *decoded, char *encoded, int sourcelen) {
62         char buf[SIZ];
63         int buf_length = 0;
64         int soft_line_break = 0;
65         int ch;
66         int decoded_length = 0;
67         int i;
68
69         decoded[0] = 0;
70         decoded_length = 0;
71         buf[0] = 0;
72         buf_length = 0;
73
74         for (i = 0; i < sourcelen; ++i) {
75
76                 buf[buf_length++] = encoded[i];
77
78                 if ( (encoded[i] == '\n')
79                    || (encoded[i] == 0)
80                    || (i == (sourcelen-1)) ) {
81                         buf[buf_length++] = 0;
82
83                         /*** begin -- process one line ***/
84
85                         if (buf[strlen(buf)-1] == '\n') {
86                                 buf[strlen(buf)-1] = 0;
87                         }
88                         if (buf[strlen(buf)-1] == '\r') {
89                                 buf[strlen(buf)-1] = 0;
90                         }
91                         while (isspace(buf[strlen(buf)-1])) {
92                                 buf[strlen(buf)-1] = 0;
93                         }
94                         soft_line_break = 0;
95
96                         while (strlen(buf) > 0) {
97                                 if (!strcmp(buf, "=")) {
98                                         soft_line_break = 1;
99                                         strcpy(buf, "");
100                                 } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
101                                         sscanf(&buf[1], "%02x", &ch);
102                                         decoded[decoded_length++] = ch;
103                                         strcpy(buf, &buf[3]);
104                                 } else {
105                                         decoded[decoded_length++] = buf[0];
106                                         strcpy(buf, &buf[1]);
107                                 }
108                         }
109                         if (soft_line_break == 0) {
110                                 decoded[decoded_length++] = '\r';
111                                 decoded[decoded_length++] = '\n';
112                         }
113                         buf_length = 0;
114                         /*** end -- process one line ***/
115                 }
116         }
117
118         decoded[decoded_length++] = 0;
119         return(decoded_length);
120 }
121
122 /*
123  * Given a message or message-part body and a length, handle any necessary
124  * decoding and pass the request up the stack.
125  */
126 void mime_decode(char *partnum,
127                  char *part_start, size_t length,
128                  char *content_type, char *encoding,
129                  char *disposition,
130                  char *name, char *filename,
131                  void (*CallBack)
132                   (char *cbname,
133                    char *cbfilename,
134                    char *cbpartnum,
135                    char *cbdisp,
136                    void *cbcontent,
137                    char *cbtype,
138                    size_t cblength,
139                    char *cbencoding,
140                    void *cbuserdata),
141                  void (*PreMultiPartCallBack)
142                   (char *cbname,
143                    char *cbfilename,
144                    char *cbpartnum,
145                    char *cbdisp,
146                    void *cbcontent,
147                    char *cbtype,
148                    size_t cblength,
149                    char *cbencoding,
150                    void *cbuserdata),
151                  void (*PostMultiPartCallBack)
152                   (char *cbname,
153                    char *cbfilename,
154                    char *cbpartnum,
155                    char *cbdisp,
156                    void *cbcontent,
157                    char *cbtype,
158                    size_t cblength,
159                    char *cbencoding,
160                    void *cbuserdata),
161                   void *userdata,
162                   int dont_decode
163 )
164 {
165
166         char *decoded;
167         size_t bytes_decoded = 0;
168
169         /* Some encodings aren't really encodings */
170         if (!strcasecmp(encoding, "7bit"))
171                 strcpy(encoding, "");
172         if (!strcasecmp(encoding, "8bit"))
173                 strcpy(encoding, "");
174         if (!strcasecmp(encoding, "binary"))
175                 strcpy(encoding, "");
176
177         /* If this part is not encoded, send as-is */
178         if ( (strlen(encoding) == 0) || (dont_decode)) {
179                 if (CallBack != NULL) {
180                         CallBack(name, filename, fixed_partnum(partnum),
181                                 disposition, part_start,
182                                 content_type, length, encoding, userdata);
183                         }
184                 return;
185         }
186         if ((strcasecmp(encoding, "base64"))
187             && (strcasecmp(encoding, "quoted-printable"))) {
188                 fprintf(stderr, "ERROR: unknown MIME encoding '%s'\n", encoding);
189                 return;
190         }
191         /*
192          * Allocate a buffer for the decoded data.  The output buffer is the
193          * same size as the input buffer; this assumes that the decoded data
194          * will never be larger than the encoded data.  This is a safe
195          * assumption with base64, uuencode, and quoted-printable.  Just to
196          * be safe, we still pad the buffer a bit.
197          */
198         decoded = malloc(length + 1024);
199         if (decoded == NULL) {
200                 fprintf(stderr, "ERROR: cannot allocate memory.\n");
201                 return;
202         }
203
204         if (!strcasecmp(encoding, "base64")) {
205                 bytes_decoded = decode_base64(decoded, part_start);
206         }
207         else if (!strcasecmp(encoding, "quoted-printable")) {
208                 bytes_decoded = decode_quoted_printable(decoded,
209                                                         part_start, length);
210         }
211
212         if (bytes_decoded > 0) if (CallBack != NULL) {
213                 CallBack(name, filename, fixed_partnum(partnum),
214                         disposition, decoded,
215                         content_type, bytes_decoded, "binary", userdata);
216         }
217
218         free(decoded);
219 }
220
221
222
223
224
225 /*
226  * Break out the components of a multipart message
227  * (This function expects to be fed HEADERS + CONTENT)
228  * Note: NULL can be supplied as content_end; in this case, the message is
229  * considered to have ended when the parser encounters a 0x00 byte.
230  */
231 void the_mime_parser(char *partnum,
232                      char *content_start, char *content_end,
233                      void (*CallBack)
234                       (char *cbname,
235                        char *cbfilename,
236                        char *cbpartnum,
237                        char *cbdisp,
238                        void *cbcontent,
239                        char *cbtype,
240                        size_t cblength,
241                        char *cbencoding,
242                        void *cbuserdata),
243                      void (*PreMultiPartCallBack)
244                       (char *cbname,
245                        char *cbfilename,
246                        char *cbpartnum,
247                        char *cbdisp,
248                        void *cbcontent,
249                        char *cbtype,
250                        size_t cblength,
251                        char *cbencoding,
252                        void *cbuserdata),
253                      void (*PostMultiPartCallBack)
254                       (char *cbname,
255                        char *cbfilename,
256                        char *cbpartnum,
257                        char *cbdisp,
258                        void *cbcontent,
259                        char *cbtype,
260                        size_t cblength,
261                        char *cbencoding,
262                        void *cbuserdata),
263                       void *userdata,
264                       int dont_decode
265 )
266 {
267
268         char *ptr;
269         char *part_start, *part_end = NULL;
270         char buf[SIZ];
271         char header[SIZ];
272         char boundary[SIZ];
273         char startary[SIZ];
274         char endary[SIZ];
275         char content_type[SIZ];
276         size_t content_length;
277         char encoding[SIZ];
278         char disposition[SIZ];
279         char name[SIZ];
280         char filename[SIZ];
281         int is_multipart;
282         int part_seq = 0;
283         int i;
284         size_t length;
285         char nested_partnum[SIZ];
286
287         fprintf(stderr, "the_mime_parser() called\n");
288         ptr = content_start;
289         memset(boundary, 0, sizeof boundary);
290         memset(content_type, 0, sizeof content_type);
291         memset(encoding, 0, sizeof encoding);
292         memset(name, 0, sizeof name);
293         memset(filename, 0, sizeof filename);
294         memset(disposition, 0, sizeof disposition);
295         content_length = 0;
296
297         /* If the caller didn't supply an endpointer, generate one by measure */
298         if (content_end == NULL) {
299                 content_end = &content_start[strlen(content_start)];
300         }
301
302         /* Learn interesting things from the headers */
303         strcpy(header, "");
304         do {
305                 ptr = memreadline(ptr, buf, sizeof buf);
306                 if (ptr >= content_end)
307                         return;
308
309                 for (i = 0; i < strlen(buf); ++i)
310                         if (isspace(buf[i]))
311                                 buf[i] = ' ';
312                 if (!isspace(buf[0])) {
313                         if (!strncasecmp(header, "Content-type: ", 14)) {
314                                 strcpy(content_type, &header[14]);
315                                 extract_key(name, content_type, "name");
316                         }
317                         if (!strncasecmp(header, "Content-Disposition: ", 21)) {
318                                 strcpy(disposition, &header[21]);
319                                 extract_key(filename, disposition, "filename");
320                         }
321                         if (!strncasecmp(header, "Content-length: ", 16)) {
322                                 content_length = (size_t) atol(&header[16]);
323                         }
324                         if (!strncasecmp(header,
325                                       "Content-transfer-encoding: ", 27))
326                                 strcpy(encoding, &header[27]);
327                         if (strlen(boundary) == 0)
328                                 extract_key(boundary, header, "boundary");
329                         strcpy(header, "");
330                 }
331                 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
332                         strcat(header, buf);
333         } while ((strlen(buf) > 0) && (*ptr != 0));
334
335         for (i = 0; i < strlen(disposition); ++i)
336                 if (disposition[i] == ';')
337                         disposition[i] = 0;
338         while (isspace(disposition[0]))
339                 strcpy(disposition, &disposition[1]);
340         for (i = 0; i < strlen(content_type); ++i)
341                 if (content_type[i] == ';')
342                         content_type[i] = 0;
343         while (isspace(content_type[0]))
344                 strcpy(content_type, &content_type[1]);
345
346         if (strlen(boundary) > 0) {
347                 is_multipart = 1;
348         } else {
349                 is_multipart = 0;
350         }
351
352         fprintf(stderr, "is_multipart=%d, boundary=<%s>\n",
353                 is_multipart, boundary);
354
355         /* If this is a multipart message, then recursively process it */
356         part_start = NULL;
357         if (is_multipart) {
358
359                 /* Tell the client about this message's multipartedness */
360                 if (PreMultiPartCallBack != NULL) {
361                         PreMultiPartCallBack("", "", partnum, "",
362                                 NULL, content_type,
363                                 0, encoding, userdata);
364                 }
365
366                 /* Figure out where the boundaries are */
367                 sprintf(startary, "--%s", boundary);
368                 sprintf(endary, "--%s--", boundary);
369                 do {
370                         /* if (ptr >= content_end) goto END_MULTI; */
371
372                         if ( (!strncasecmp(ptr, startary, strlen(startary)))
373                            || (!strncasecmp(ptr, endary, strlen(endary))) ) {
374                                 fprintf(stderr, "hit boundary!\n");
375                                 if (part_start != NULL) {
376                                         if (strlen(partnum) > 0) {
377                                                 sprintf(nested_partnum, "%s.%d",
378                                                         partnum, ++part_seq);
379                                         }
380                                         else {
381                                                 sprintf(nested_partnum, "%d",
382                                                         ++part_seq);
383                                         }
384                                         the_mime_parser(nested_partnum,
385                                                     part_start, part_end,
386                                                         CallBack,
387                                                         PreMultiPartCallBack,
388                                                         PostMultiPartCallBack,
389                                                         userdata,
390                                                         dont_decode);
391                                 }
392                                 ptr = memreadline(ptr, buf, sizeof(buf));
393                                 part_start = ptr;
394                         }
395                         else {
396                                 part_end = ptr;
397                                 ++ptr;
398                         }
399                 } while ( (strcasecmp(ptr, endary)) && (ptr <= content_end) );
400                 if (PostMultiPartCallBack != NULL) {
401                         PostMultiPartCallBack("", "", partnum, "", NULL,
402                                 content_type, 0, encoding, userdata);
403                 }
404                 return;
405         }
406
407         /* If it's not a multipart message, then do something with it */
408         if (!is_multipart) {
409                 fprintf(stderr, "doing non-multipart thing\n");
410                 part_start = ptr;
411                 length = 0;
412                 while (ptr < content_end) {
413                         ++ptr;
414                         ++length;
415                 }
416                 part_end = content_end;
417                 
418                 /* Truncate if the header told us to */
419                 if ( (content_length > 0) && (length > content_length) ) {
420                         length = content_length;
421                         fprintf(stderr, "truncated to %d\n", content_length);
422                 }
423                 
424                 mime_decode(partnum,
425                             part_start, length,
426                             content_type, encoding, disposition,
427                             name, filename,
428                             CallBack, NULL, NULL,
429                             userdata, dont_decode);
430         }
431 }
432
433
434
435 /*
436  * Entry point for the MIME parser.
437  * (This function expects to be fed HEADERS + CONTENT)
438  * Note: NULL can be supplied as content_end; in this case, the message is
439  * considered to have ended when the parser encounters a 0x00 byte.
440  */
441 void mime_parser(char *content_start,
442                 char *content_end,
443
444                  void (*CallBack)
445                   (char *cbname,
446                    char *cbfilename,
447                    char *cbpartnum,
448                    char *cbdisp,
449                    void *cbcontent,
450                    char *cbtype,
451                    size_t cblength,
452                    char *cbencoding,
453                    void *cbuserdata),
454
455                  void (*PreMultiPartCallBack)
456                   (char *cbname,
457                    char *cbfilename,
458                    char *cbpartnum,
459                    char *cbdisp,
460                    void *cbcontent,
461                    char *cbtype,
462                    size_t cblength,
463                    char *cbencoding,
464                    void *cbuserdata),
465
466                  void (*PostMultiPartCallBack)
467                   (char *cbname,
468                    char *cbfilename,
469                    char *cbpartnum,
470                    char *cbdisp,
471                    void *cbcontent,
472                    char *cbtype,
473                    size_t cblength,
474                    char *cbencoding,
475                    void *cbuserdata),
476
477                   void *userdata,
478                   int dont_decode
479 )
480 {
481
482         fprintf(stderr, "mime_parser() called\n");
483         the_mime_parser("", content_start, content_end,
484                         CallBack,
485                         PreMultiPartCallBack,
486                         PostMultiPartCallBack,
487                         userdata, dont_decode);
488 }