Bump to version 1.22.1
[platform/upstream/busybox.git] / mailutils / reformime.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * reformime: parse MIME-encoded message
4  *
5  * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9
10 //kbuild:lib-$(CONFIG_REFORMIME) += reformime.o mail.o
11
12 #include "libbb.h"
13 #include "mail.h"
14
15 #if 0
16 # define dbg_error_msg(...) bb_error_msg(__VA_ARGS__)
17 #else
18 # define dbg_error_msg(...) ((void)0)
19 #endif
20
21 static const char *find_token(const char *const string_array[], const char *key, const char *defvalue)
22 {
23         const char *r = NULL;
24         int i;
25         for (i = 0; string_array[i] != NULL; i++) {
26                 if (strcasecmp(string_array[i], key) == 0) {
27                         r = (char *)string_array[i+1];
28                         break;
29                 }
30         }
31         return (r) ? r : defvalue;
32 }
33
34 static const char *xfind_token(const char *const string_array[], const char *key)
35 {
36         const char *r = find_token(string_array, key, NULL);
37         if (r)
38                 return r;
39         bb_error_msg_and_die("not found: '%s'", key);
40 }
41
42 enum {
43         OPT_x = 1 << 0,
44         OPT_X = 1 << 1,
45 #if ENABLE_FEATURE_REFORMIME_COMPAT
46         OPT_d = 1 << 2,
47         OPT_e = 1 << 3,
48         OPT_i = 1 << 4,
49         OPT_s = 1 << 5,
50         OPT_r = 1 << 6,
51         OPT_c = 1 << 7,
52         OPT_m = 1 << 8,
53         OPT_h = 1 << 9,
54         OPT_o = 1 << 10,
55         OPT_O = 1 << 11,
56 #endif
57 };
58
59 static int parse(const char *boundary, char **argv)
60 {
61         int boundary_len = strlen(boundary);
62         char uniq[sizeof("%%llu.%u") + sizeof(int)*3];
63
64         dbg_error_msg("BOUNDARY[%s]", boundary);
65
66         // prepare unique string pattern
67         sprintf(uniq, "%%llu.%u", (unsigned)getpid());
68         dbg_error_msg("UNIQ[%s]", uniq);
69
70         while (1) {
71                 char *header;
72                 const char *tokens[32]; /* 32 is enough */
73                 const char *type;
74
75                 /* Read the header (everything up to two \n) */
76                 {
77                         unsigned header_idx = 0;
78                         int last_ch = 0;
79                         header = NULL;
80                         while (1) {
81                                 int ch = fgetc(stdin);
82                                 if (ch == '\r') /* Support both line endings */
83                                         continue;
84                                 if (ch == EOF)
85                                         break;
86                                 if (ch == '\n' && last_ch == ch)
87                                         break;
88                                 if (!(header_idx & 0xff))
89                                         header = xrealloc(header, header_idx + 0x101);
90                                 header[header_idx++] = last_ch = ch;
91                         }
92                         if (!header) {
93                                 dbg_error_msg("EOF");
94                                 break;
95                         }
96                         header[header_idx] = '\0';
97                         dbg_error_msg("H:'%s'", p);
98                 }
99
100                 /* Split to tokens */
101                 {
102                         char *s, *p;
103                         unsigned ntokens;
104                         const char *delims = ";=\" \t\n";
105
106                         /* Skip to last Content-Type: */
107                         s = p = header;
108                         while ((p = strchr(p, '\n')) != NULL) {
109                                 p++;
110                                 if (strncasecmp(p, "Content-Type:", sizeof("Content-Type:")-1) == 0)
111                                         s = p;
112                         }
113                         dbg_error_msg("L:'%s'", p);
114                         ntokens = 0;
115                         s = strtok(s, delims);
116                         while (s) {
117                                 tokens[ntokens] = s;
118                                 if (ntokens < ARRAY_SIZE(tokens) - 1)
119                                         ntokens++;
120                                 dbg_error_msg("L[%d]='%s'", ntokens, s);
121                                 s = strtok(NULL, delims);
122                         }
123                         tokens[ntokens] = NULL;
124                         dbg_error_msg("EMPTYLINE, ntokens:%d", ntokens);
125                         if (ntokens == 0)
126                                 break;
127                 }
128
129                 /* Is it multipart? */
130                 type = find_token(tokens, "Content-Type:", "text/plain");
131                 dbg_error_msg("TYPE:'%s'", type);
132                 if (0 == strncasecmp(type, "multipart/", 10)) {
133                         /* Yes, recurse */
134                         if (strcasecmp(type + 10, "mixed") != 0)
135                                 bb_error_msg_and_die("no support of content type '%s'", type);
136                         parse(xfind_token(tokens, "boundary"), argv);
137
138                 } else {
139                         /* No, process one non-multipart section */
140                         char *end;
141                         pid_t pid = pid;
142                         FILE *fp;
143
144                         const char *charset = find_token(tokens, "charset", CONFIG_FEATURE_MIME_CHARSET);
145                         const char *encoding = find_token(tokens, "Content-Transfer-Encoding:", "7bit");
146
147                         /* Compose target filename */
148                         char *filename = (char *)find_token(tokens, "filename", NULL);
149                         if (!filename)
150                                 filename = xasprintf(uniq, monotonic_us());
151                         else
152                                 filename = bb_get_last_path_component_strip(xstrdup(filename));
153
154                         if (opts & OPT_X) {
155                                 int fd[2];
156
157                                 /* start external helper */
158                                 xpipe(fd);
159                                 pid = vfork();
160                                 if (0 == pid) {
161                                         /* child reads from fd[0] */
162                                         close(fd[1]);
163                                         xmove_fd(fd[0], STDIN_FILENO);
164                                         xsetenv("CONTENT_TYPE", type);
165                                         xsetenv("CHARSET", charset);
166                                         xsetenv("ENCODING", encoding);
167                                         xsetenv("FILENAME", filename);
168                                         BB_EXECVP_or_die(argv);
169                                 }
170                                 /* parent will write to fd[1] */
171                                 close(fd[0]);
172                                 fp = xfdopen_for_write(fd[1]);
173                                 signal(SIGPIPE, SIG_IGN);
174                         } else {
175                                 /* write to file */
176                                 char *fname = xasprintf("%s%s", *argv, filename);
177                                 fp = xfopen_for_write(fname);
178                                 free(fname);
179                         }
180                         free(filename);
181
182                         /* write to fp */
183                         end = NULL;
184                         if (0 == strcasecmp(encoding, "base64")) {
185                                 read_base64(stdin, fp, '-');
186                         } else
187                         if (0 != strcasecmp(encoding, "7bit")
188                          && 0 != strcasecmp(encoding, "8bit")
189                         ) {
190                                 /* quoted-printable, binary, user-defined are unsupported so far */
191                                 bb_error_msg_and_die("encoding '%s' not supported", encoding);
192                         } else {
193                                 /* plain 7bit or 8bit */
194                                 while ((end = xmalloc_fgets(stdin)) != NULL) {
195                                         if ('-' == end[0]
196                                          && '-' == end[1]
197                                          && strncmp(end + 2, boundary, boundary_len) == 0
198                                         ) {
199                                                 break;
200                                         }
201                                         fputs(end, fp);
202                                 }
203                         }
204                         fclose(fp);
205
206                         /* Wait for child */
207                         if (opts & OPT_X) {
208                                 int rc;
209                                 signal(SIGPIPE, SIG_DFL);
210                                 rc = (wait4pid(pid) & 0xff);
211                                 if (rc != 0)
212                                         return rc + 20;
213                         }
214
215                         /* Multipart ended? */
216                         if (end && '-' == end[2 + boundary_len] && '-' == end[2 + boundary_len + 1]) {
217                                 dbg_error_msg("FINISHED MPART:'%s'", end);
218                                 break;
219                         }
220                         dbg_error_msg("FINISHED:'%s'", end);
221                         free(end);
222                 } /* end of "handle one non-multipart block" */
223
224                 free(header);
225         } /* while (1) */
226
227         dbg_error_msg("ENDPARSE[%s]", boundary);
228
229         return EXIT_SUCCESS;
230 }
231
232 //usage:#define reformime_trivial_usage
233 //usage:       "[OPTIONS]"
234 //usage:#define reformime_full_usage "\n\n"
235 //usage:       "Parse MIME-encoded message on stdin\n"
236 //usage:     "\n        -x PREFIX       Extract content of MIME sections to files"
237 //usage:     "\n        -X PROG ARGS    Filter content of MIME sections through PROG"
238 //usage:     "\n                        Must be the last option"
239 //usage:     "\n"
240 //usage:     "\nOther options are silently ignored"
241
242 /*
243 Usage: reformime [options]
244     -d - parse a delivery status notification.
245     -e - extract contents of MIME section.
246     -x - extract MIME section to a file.
247     -X - pipe MIME section to a program.
248     -i - show MIME info.
249     -s n.n.n.n - specify MIME section.
250     -r - rewrite message, filling in missing MIME headers.
251     -r7 - also convert 8bit/raw encoding to quoted-printable, if possible.
252     -r8 - also convert quoted-printable encoding to 8bit, if possible.
253     -c charset - default charset for rewriting, -o, and -O.
254     -m [file] [file]... - create a MIME message digest.
255     -h "header" - decode RFC 2047-encoded header.
256     -o "header" - encode unstructured header using RFC 2047.
257     -O "header" - encode address list header using RFC 2047.
258 */
259
260 int reformime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
261 int reformime_main(int argc UNUSED_PARAM, char **argv)
262 {
263         const char *opt_prefix = "";
264
265         INIT_G();
266
267         // parse options
268         // N.B. only -x and -X are supported so far
269         opt_complementary = "x--X:X--x" IF_FEATURE_REFORMIME_COMPAT(":m::");
270         opts = getopt32(argv,
271                 "x:X" IF_FEATURE_REFORMIME_COMPAT("deis:r:c:m:h:o:O:"),
272                 &opt_prefix
273                 IF_FEATURE_REFORMIME_COMPAT(, NULL, NULL, &G.opt_charset, NULL, NULL, NULL, NULL)
274         );
275         argv += optind;
276
277         return parse("", (opts & OPT_X) ? argv : (char **)&opt_prefix);
278 }