* The size constant "256" which shows up everywhere as a buffer size has now
[citadel.git] / citadel / mime_parser.c
1 /*
2  * $Id$
3  *
4  * This is a really bad attempt at writing a parser to handle MIME-encoded
5  * messages.
6  *
7  * Copyright (c) 1998-1999 by Art Cancro
8  * This code is distributed under the terms of the GNU General Public License.
9  *
10  */
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <signal.h>
17 #include <sys/types.h>
18 #include <ctype.h>
19 #include <string.h>
20 #include <sys/stat.h>
21 #include <errno.h>
22 #include "citadel.h"
23 #include "mime_parser.h"
24 #include "sysdep_decls.h"
25 #include "server.h"
26
27
28
29 void extract_key(char *target, char *source, char *key)
30 {
31         int a, b;
32
33         strcpy(target, source);
34         for (a = 0; a < strlen(target); ++a) {
35                 if ((!strncasecmp(&target[a], key, strlen(key)))
36                     && (target[a + strlen(key)] == '=')) {
37                         strcpy(target, &target[a + strlen(key) + 1]);
38                         if (target[0] == 34)
39                                 strcpy(target, &target[1]);
40                         for (b = 0; b < strlen(target); ++b)
41                                 if (target[b] == 34)
42                                         target[b] = 0;
43                         return;
44                 }
45         }
46         strcpy(target, "");
47 }
48
49
50
51 /* 
52  * Utility function to "readline" from memory
53  * (returns new pointer)
54  */
55 char *memreadline(char *start, char *buf, int maxlen)
56 {
57         char ch;
58         char *ptr;
59
60         ptr = start;
61         memset(buf, 0, maxlen);
62
63         while (1) {
64                 ch = *ptr++;
65                 if ((ch == 10) || (ch == 0)) {
66                         if (strlen(buf) > 0)
67                                 if (buf[strlen(buf) - 1] == 13)
68                                         buf[strlen(buf) - 1] = 0;
69                         return ptr;
70                 }
71                 if (strlen(buf) < (maxlen - 1)) {
72                         buf[strlen(buf) + 1] = 0;
73                         buf[strlen(buf)] = ch;
74                 }
75         }
76 }
77
78 /*
79  * Given a message or message-part body and a length, handle any necessary
80  * decoding and pass the request up the stack.
81  */
82 void mime_decode(char *partnum,
83                  char *part_start, size_t length,
84                  char *content_type, char *encoding,
85                  char *disposition,
86                  char *name, char *filename,
87                  void (*CallBack)
88                   (char *cbname,
89                    char *cbfilename,
90                    char *cbpartnum,
91                    char *cbdisp,
92                    void *cbcontent,
93                    char *cbtype,
94                    size_t cblength,
95                    char *cbencoding,
96                    void *cbuserdata),
97                   void *userdata,
98                   int dont_decode
99 )
100 {
101
102         char *decoded;
103         struct stat statbuf;
104         int sendpipe[2];
105         int recvpipe[2];
106         int childpid;
107         size_t bytes_sent = 0;
108         size_t bytes_recv = 0;
109         size_t blocksize;
110         int write_error = 0;
111
112         lprintf(9, "mime_decode() called\n");
113
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, "");
121
122         /* If this part is not encoded, send as-is */
123         if ( (strlen(encoding) == 0) || (dont_decode)) {
124                 CallBack(name, filename, partnum, disposition, part_start,
125                          content_type, length, encoding, userdata);
126                 return;
127         }
128         if ((strcasecmp(encoding, "base64"))
129             && (strcasecmp(encoding, "quoted-printable"))) {
130                 lprintf(5, "ERROR: unknown MIME encoding '%s'\n", encoding);
131                 return;
132         }
133         /*
134          * Allocate a buffer for the decoded data.  The output buffer is the
135          * same size as the input buffer; this assumes that the decoded data
136          * will never be larger than the encoded data.  This is a safe
137          * assumption with base64, uuencode, and quoted-printable.  Just to
138          * be safe, we still pad the buffer a bit.
139          */
140         decoded = mallok(length + 1024);
141         if (decoded == NULL) {
142                 lprintf(5, "ERROR: cannot allocate memory.\n");
143                 return;
144         }
145         if (pipe(sendpipe) != 0)
146                 return;
147         if (pipe(recvpipe) != 0)
148                 return;
149
150         childpid = fork();
151         if (childpid < 0) {
152                 phree(decoded);
153                 return;
154         }
155         if (childpid == 0) {
156                 close(2);
157                 /* send stdio to the pipes */
158                 if (dup2(sendpipe[0], 0) < 0)
159                         lprintf(5, "ERROR dup2()\n");
160                 if (dup2(recvpipe[1], 1) < 0)
161                         lprintf(5, "ERROR dup2()\n");
162                 close(sendpipe[1]);     /* Close the ends we're not using */
163                 close(recvpipe[0]);
164                 if (!strcasecmp(encoding, "base64"))
165                         execlp("./base64", "base64", "-d", NULL);
166                 else if (!strcasecmp(encoding, "quoted-printable"))
167                         execlp("./qpdecode", "qpdecode", NULL);
168                 lprintf(5, "ERROR: cannot exec decoder for %s\n", encoding);
169                 exit(1);
170         }
171         close(sendpipe[0]);     /* Close the ends we're not using  */
172         close(recvpipe[1]);
173
174         while ((bytes_sent < length) && (write_error == 0)) {
175                 /* Empty the input pipe FIRST */
176                 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
177                         blocksize = read(recvpipe[0], &decoded[bytes_recv],
178                                          statbuf.st_size);
179                         if (blocksize < 0)
180                                 lprintf(5, "ERROR: cannot read from pipe\n");
181                         else
182                                 bytes_recv = bytes_recv + blocksize;
183                 }
184                 /* Then put some data into the output pipe */
185                 blocksize = length - bytes_sent;
186                 if (blocksize > 2048)
187                         blocksize = 2048;
188                 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
189                         lprintf(5, "ERROR: cannot write to pipe: %s\n",
190                                 strerror(errno));
191                         write_error = 1;
192                 }
193                 bytes_sent = bytes_sent + blocksize;
194         }
195         close(sendpipe[1]);
196         /* Empty the input pipe */
197         while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
198                (blocksize > 0)) {
199                 bytes_recv = bytes_recv + blocksize;
200         }
201
202         if (bytes_recv > 0)
203                 CallBack(name, filename, partnum, disposition, decoded,
204                          content_type, bytes_recv, "binary", userdata);
205
206         phree(decoded);
207 }
208
209 /*
210  * Break out the components of a multipart message
211  * (This function expects to be fed HEADERS + CONTENT)
212  * Note: NULL can be supplied as content_end; in this case, the message is
213  * considered to have ended when the parser encounters a 0x00 byte.
214  */
215 void the_mime_parser(char *partnum,
216                      char *content_start, char *content_end,
217                      void (*CallBack)
218                       (char *cbname,
219                        char *cbfilename,
220                        char *cbpartnum,
221                        char *cbdisp,
222                        void *cbcontent,
223                        char *cbtype,
224                        size_t cblength,
225                        char *cbencoding,
226                        void *cbuserdata),
227                       void *userdata,
228                       int dont_decode
229 )
230 {
231
232         char *ptr;
233         char *part_start, *part_end;
234         char buf[SIZ];
235         char header[SIZ];
236         char boundary[SIZ];
237         char startary[SIZ];
238         char endary[SIZ];
239         char content_type[SIZ];
240         char encoding[SIZ];
241         char disposition[SIZ];
242         char name[SIZ];
243         char filename[SIZ];
244         int is_multipart;
245         int part_seq = 0;
246         int i;
247         size_t length;
248         char nested_partnum[SIZ];
249
250         lprintf(9, "the_mime_parser() called\n");
251         ptr = content_start;
252         memset(boundary, 0, sizeof boundary);
253         memset(content_type, 0, sizeof content_type);
254         memset(encoding, 0, sizeof encoding);
255         memset(name, 0, sizeof name);
256         memset(filename, 0, sizeof filename);
257         memset(disposition, 0, sizeof disposition);
258
259         /* Learn interesting things from the headers */
260         strcpy(header, "");
261         do {
262                 ptr = memreadline(ptr, buf, sizeof buf);
263                 if (*ptr == 0)
264                         return; /* premature end of message */
265                 if (content_end != NULL)
266                         if (ptr >= content_end)
267                                 return;
268
269                 for (i = 0; i < strlen(buf); ++i)
270                         if (isspace(buf[i]))
271                                 buf[i] = ' ';
272                 if (!isspace(buf[0])) {
273                         if (!strncasecmp(header, "Content-type: ", 14)) {
274                                 strcpy(content_type, &header[14]);
275                                 extract_key(name, content_type, "name");
276                         }
277                         if (!strncasecmp(header, "Content-Disposition: ", 21)) {
278                                 strcpy(disposition, &header[21]);
279                                 extract_key(filename, disposition, "filename");
280                         }
281                         if (!strncasecmp(header,
282                                       "Content-transfer-encoding: ", 27))
283                                 strcpy(encoding, &header[27]);
284                         if (strlen(boundary) == 0)
285                                 extract_key(boundary, header, "boundary");
286                         strcpy(header, "");
287                 }
288                 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
289                         strcat(header, buf);
290         } while ((strlen(buf) > 0) && (*ptr != 0));
291
292         for (i = 0; i < strlen(disposition); ++i)
293                 if (disposition[i] == ';')
294                         disposition[i] = 0;
295         while (isspace(disposition[0]))
296                 strcpy(disposition, &disposition[1]);
297         for (i = 0; i < strlen(content_type); ++i)
298                 if (content_type[i] == ';')
299                         content_type[i] = 0;
300         while (isspace(content_type[0]))
301                 strcpy(content_type, &content_type[1]);
302
303         if (strlen(boundary) > 0) {
304                 is_multipart = 1;
305         } else {
306                 is_multipart = 0;
307         }
308
309         /* If this is a multipart message, then recursively process it */
310         part_start = NULL;
311         if (is_multipart) {
312
313                 /* Tell the client about this message's multipartedness */
314                 CallBack("", "", partnum, "", NULL, content_type, 0,
315                                 encoding, userdata);
316
317                 /* Figure out where the boundaries are */
318                 sprintf(startary, "--%s", boundary);
319                 sprintf(endary, "--%s--", boundary);
320                 do {
321                         part_end = ptr;
322                         ptr = memreadline(ptr, buf, sizeof buf);
323                         if (*ptr == 0)
324                                 return;         /* premature end of message */
325                         if (content_end != NULL)
326                                 if (ptr >= content_end)
327                                         return;
328                         if ((!strcasecmp(buf, startary))
329                             || (!strcasecmp(buf, endary))) {
330                                 if (part_start != NULL) {
331                                         if (strlen(partnum) > 0) {
332                                                 sprintf(nested_partnum, "%s.%d",
333                                                         partnum, ++part_seq);
334                                         }
335                                         else {
336                                                 sprintf(nested_partnum, "%d",
337                                                         ++part_seq);
338                                         }
339                                         the_mime_parser(nested_partnum,
340                                                     part_start, part_end,
341                                                         CallBack, userdata,
342                                                         dont_decode);
343                                 }
344                                 part_start = ptr;
345                         }
346                 } while (strcasecmp(buf, endary));
347         }
348
349         /* If it's not a multipart message, then do something with it */
350         if (!is_multipart) {
351                 part_start = ptr;
352                 length = 0;
353                 while ((*ptr != 0)
354                       && ((content_end == NULL) || (ptr < content_end))) {
355                         ++length;
356                         part_end = ptr++;
357                 }
358                 mime_decode(partnum,
359                             part_start, length,
360                             content_type, encoding, disposition,
361                             name, filename, CallBack, userdata, dont_decode);
362         }
363
364
365 }
366
367
368
369 /*
370  * Entry point for the MIME parser.
371  * (This function expects to be fed HEADERS + CONTENT)
372  * Note: NULL can be supplied as content_end; in this case, the message is
373  * considered to have ended when the parser encounters a 0x00 byte.
374  */
375 void mime_parser(char *content_start, char *content_end,
376                  void (*CallBack)
377                   (char *cbname,
378                    char *cbfilename,
379                    char *cbpartnum,
380                    char *cbdisp,
381                    void *cbcontent,
382                    char *cbtype,
383                    size_t cblength,
384                    char *cbencoding,
385                    void *cbuserdata),
386                   void *userdata,
387                   int dont_decode
388 )
389 {
390
391         lprintf(9, "mime_parser() called\n");
392         the_mime_parser("", content_start, content_end, CallBack,
393                         userdata, dont_decode);
394 }