blob: 0aff8b1d7107e5fa9ae4d9761d2bc6d160229538 [file] [log] [blame]
Denis Vlasenko239d06b2008-11-06 23:42:42 +00001/* vi: set sw=4 ts=4: */
2/*
3 * makemime: create MIME-encoded message
4 * reformime: parse MIME-encoded message
5 *
6 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko239d06b2008-11-06 23:42:42 +00009 */
Pere Orga6a3e01d2011-04-01 22:56:30 +020010
11//usage:#define makemime_trivial_usage
12//usage: "[OPTIONS] [FILE]..."
13//usage:#define makemime_full_usage "\n\n"
14//usage: "Create multipart MIME-encoded message from FILEs\n"
15/* //usage: "Transfer encoding is base64, disposition is inline (not attachment)\n" */
16//usage: "\nOptions:"
17//usage: "\n -o FILE Output. Default: stdout"
18//usage: "\n -a HDR Add header. Examples:"
19//usage: "\n \"From: user@host.org\", \"Date: `date -R`\""
20//usage: "\n -c CT Content type. Default: text/plain"
21//usage: "\n -C CS Charset. Default: " CONFIG_FEATURE_MIME_CHARSET
22/* //usage: "\n -e ENC Transfer encoding. Ignored. base64 is assumed" */
23//usage: "\n"
24//usage: "\nOther options are silently ignored"
25
26//usage:#define reformime_trivial_usage
27//usage: "[OPTIONS] [FILE]..."
28//usage:#define reformime_full_usage "\n\n"
29//usage: "Parse MIME-encoded message\n"
30//usage: "\nOptions:"
31//usage: "\n -x PREFIX Extract content of MIME sections to files"
32//usage: "\n -X PROG ARGS Filter content of MIME sections through PROG"
33//usage: "\n Must be the last option"
34//usage: "\n"
35//usage: "\nOther options are silently ignored"
36
Denis Vlasenko239d06b2008-11-06 23:42:42 +000037#include "libbb.h"
38#include "mail.h"
39
40/*
41 makemime -c type [-o file] [-e encoding] [-C charset] [-N name] \
42 [-a "Header: Contents"] file
43 -m [ type ] [-o file] [-e encoding] [-a "Header: Contents"] file
44 -j [-o file] file1 file2
45 @file
46
47 file: filename - read or write from filename
48 - - read or write from stdin or stdout
49 &n - read or write from file descriptor n
50 \( opts \) - read from child process, that generates [ opts ]
51
52Options:
53
54 -c type - create a new MIME section from "file" with this
55 Content-Type: (default is application/octet-stream).
56 -C charset - MIME charset of a new text/plain section.
57 -N name - MIME content name of the new mime section.
58 -m [ type ] - create a multipart mime section from "file" of this
59 Content-Type: (default is multipart/mixed).
60 -e encoding - use the given encoding (7bit, 8bit, quoted-printable,
61 or base64), instead of guessing. Omit "-e" and use
62 -c auto to set Content-Type: to text/plain or
63 application/octet-stream based on picked encoding.
64 -j file1 file2 - join mime section file2 to multipart section file1.
Denys Vlasenko666e1d32009-07-05 21:46:37 +020065 -o file - write the result to file, instead of stdout (not
Denis Vlasenko239d06b2008-11-06 23:42:42 +000066 allowed in child processes).
67 -a header - prepend an additional header to the output.
68
69 @file - read all of the above options from file, one option or
70 value on each line.
Denys Vlasenko666e1d32009-07-05 21:46:37 +020071 {which version of makemime is this? What do we support?}
72*/
73
74
75/* In busybox 1.15.0.svn, makemime generates output like this
76 * (empty lines are shown exactly!):
77{headers added with -a HDR}
78Mime-Version: 1.0
79Content-Type: multipart/mixed; boundary="24269534-2145583448-1655890676"
80
81--24269534-2145583448-1655890676
82Content-Type: {set by -c, e.g. text/plain}; charset={set by -C, e.g. us-ascii}
83Content-Disposition: inline; filename="A"
84Content-Transfer-Encoding: base64
85
86...file A contents...
87--24269534-2145583448-1655890676
88Content-Type: {set by -c, e.g. text/plain}; charset={set by -C, e.g. us-ascii}
89Content-Disposition: inline; filename="B"
90Content-Transfer-Encoding: base64
91
92...file B contents...
93--24269534-2145583448-1655890676--
94
95*/
96
97
98/* For reference: here is an example email to LKML which has
99 * 1st unnamed part (so it serves as an email body)
100 * and one attached file:
101...other headers...
102Content-Type: multipart/mixed; boundary="=-tOfTf3byOS0vZgxEWcX+"
103...other headers...
104Mime-Version: 1.0
105...other headers...
106
107
108--=-tOfTf3byOS0vZgxEWcX+
109Content-Type: text/plain
110Content-Transfer-Encoding: 7bit
111
112...email text...
113...email text...
114
115
116--=-tOfTf3byOS0vZgxEWcX+
117Content-Disposition: attachment; filename="xyz"
118Content-Type: text/plain; name="xyz"; charset="UTF-8"
119Content-Transfer-Encoding: 7bit
120
121...file contents...
122...file contents...
123
124--=-tOfTf3byOS0vZgxEWcX+--
125
126...random junk added by mailing list robots and such...
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000127*/
128
Denys Vlasenko5707b522010-12-20 05:12:39 +0100129/* man makemime:
130
131 * -c TYPE: create a (non-multipart) MIME section with Content-Type: TYPE
132 * makemime -c TYPE [-e ENCODING] [-o OUTFILE] [-C CHARSET] [-N NAME] [-a HEADER...] FILE
133 * The -C option sets the MIME charset attribute for text/plain content.
134 * The -N option sets the name attribute for Content-Type:
135 * Encoding must be one of the following: 7bit, 8bit, quoted-printable, or base64.
136
137 * -m multipart/TYPE: create a multipart MIME collection with Content-Type: multipart/TYPE
138 * makemime -m multipart/TYPE [-e ENCODING] [-o OUTFILE] [-a HEADER...] FILE
139 * Type must be either "multipart/mixed", "multipart/alternative", or some other MIME multipart content type.
140 * Additionally, encoding can only be "7bit" or "8bit", and will default to "8bit" if not specified.
141 * Finally, filename must be a MIME-formatted section, NOT a regular file.
142 * The -m option creates an initial multipart MIME collection, that contains only one MIME section, taken from filename.
143 * The collection is written to standard output, or the pipe or to outputfile.
144
145 * -j FILE1: add a section to a multipart MIME collection
146 * makemime -j FILE1 [-o OUTFILE] FILE2
147 * FILE1 must be a MIME collection that was previously created by the -m option.
148 * FILE2 must be a MIME section that was previously created by the -c option.
149 * The -j options adds the MIME section in FILE2 to the MIME collection in FILE1.
150 */
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000151int makemime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
152int makemime_main(int argc UNUSED_PARAM, char **argv)
153{
154 llist_t *opt_headers = NULL, *l;
155 const char *opt_output;
156#define boundary opt_output
157
158 enum {
Denys Vlasenko5707b522010-12-20 05:12:39 +0100159 OPT_c = 1 << 0, // create (non-multipart) section
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000160 OPT_e = 1 << 1, // Content-Transfer-Encoding. Ignored. Assumed base64
161 OPT_o = 1 << 2, // output to
162 OPT_C = 1 << 3, // charset
163 OPT_N = 1 << 4, // COMPAT
164 OPT_a = 1 << 5, // additional headers
Denys Vlasenko5707b522010-12-20 05:12:39 +0100165 //OPT_m = 1 << 6, // create mutipart section
166 //OPT_j = 1 << 7, // join section to multipart section
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000167 };
168
169 INIT_G();
170
171 // parse options
172 opt_complementary = "a::";
173 opts = getopt32(argv,
Denys Vlasenko5707b522010-12-20 05:12:39 +0100174 "c:e:o:C:N:a", //:m:j:",
175 &G.content_type, NULL, &opt_output, &G.opt_charset, NULL, &opt_headers //, NULL, NULL
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000176 );
177 //argc -= optind;
178 argv += optind;
179
180 // respect -o output
181 if (opts & OPT_o)
182 freopen(opt_output, "w", stdout);
183
184 // no files given on command line? -> use stdin
185 if (!*argv)
186 *--argv = (char *)"-";
187
188 // put additional headers
189 for (l = opt_headers; l; l = l->link)
190 puts(l->data);
191
192 // make a random string -- it will delimit message parts
193 srand(monotonic_us());
Denys Vlasenko666e1d32009-07-05 21:46:37 +0200194 boundary = xasprintf("%u-%u-%u",
195 (unsigned)rand(), (unsigned)rand(), (unsigned)rand());
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000196
197 // put multipart header
198 printf(
199 "Mime-Version: 1.0\n"
200 "Content-Type: multipart/mixed; boundary=\"%s\"\n"
201 , boundary
202 );
203
204 // put attachments
205 while (*argv) {
206 printf(
207 "\n--%s\n"
208 "Content-Type: %s; charset=%s\n"
209 "Content-Disposition: inline; filename=\"%s\"\n"
210 "Content-Transfer-Encoding: base64\n"
211 , boundary
212 , G.content_type
213 , G.opt_charset
214 , bb_get_last_path_component_strip(*argv)
215 );
216 encode_base64(*argv++, (const char *)stdin, "");
217 }
218
219 // put multipart footer
220 printf("\n--%s--\n" "\n", boundary);
221
222 return EXIT_SUCCESS;
223#undef boundary
224}
225
226static const char *find_token(const char *const string_array[], const char *key, const char *defvalue)
227{
228 const char *r = NULL;
Denys Vlasenko90a99042009-09-06 02:36:23 +0200229 int i;
230 for (i = 0; string_array[i] != NULL; i++) {
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000231 if (strcasecmp(string_array[i], key) == 0) {
232 r = (char *)string_array[i+1];
233 break;
234 }
235 }
236 return (r) ? r : defvalue;
237}
238
239static const char *xfind_token(const char *const string_array[], const char *key)
240{
241 const char *r = find_token(string_array, key, NULL);
242 if (r)
243 return r;
244 bb_error_msg_and_die("header: %s", key);
245}
246
247enum {
248 OPT_x = 1 << 0,
249 OPT_X = 1 << 1,
250#if ENABLE_FEATURE_REFORMIME_COMPAT
251 OPT_d = 1 << 2,
252 OPT_e = 1 << 3,
253 OPT_i = 1 << 4,
254 OPT_s = 1 << 5,
255 OPT_r = 1 << 6,
256 OPT_c = 1 << 7,
257 OPT_m = 1 << 8,
258 OPT_h = 1 << 9,
259 OPT_o = 1 << 10,
260 OPT_O = 1 << 11,
261#endif
262};
263
264static int parse(const char *boundary, char **argv)
265{
266 char *line, *s, *p;
267 const char *type;
268 int boundary_len = strlen(boundary);
269 const char *delims = " ;\"\t\r\n";
270 const char *uniq;
271 int ntokens;
272 const char *tokens[32]; // 32 is enough
273
274 // prepare unique string pattern
275 uniq = xasprintf("%%llu.%u.%s", (unsigned)getpid(), safe_gethostname());
276
Denys Vlasenko9fe98f72010-09-16 17:51:13 +0200277//bb_info_msg("PARSE[%s]", uniq);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000278
279 while ((line = xmalloc_fgets_str(stdin, "\r\n\r\n")) != NULL) {
280
281 // seek to start of MIME section
282 // N.B. to avoid false positives let us seek to the _last_ occurance
283 p = NULL;
284 s = line;
Denys Vlasenkoa51543a2009-07-07 07:52:34 +0200285 while ((s = strcasestr(s, "Content-Type:")) != NULL)
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000286 p = s++;
287 if (!p)
288 goto next;
289//bb_info_msg("L[%s]", p);
290
291 // split to tokens
292 // TODO: strip of comments which are of form: (comment-text)
293 ntokens = 0;
294 tokens[ntokens] = NULL;
295 for (s = strtok(p, delims); s; s = strtok(NULL, delims)) {
296 tokens[ntokens] = s;
297 if (ntokens < ARRAY_SIZE(tokens) - 1)
298 ntokens++;
299//bb_info_msg("L[%d][%s]", ntokens, s);
300 }
301 tokens[ntokens] = NULL;
302//bb_info_msg("N[%d]", ntokens);
303
304 // analyse tokens
305 type = find_token(tokens, "Content-Type:", "text/plain");
306//bb_info_msg("T[%s]", type);
307 if (0 == strncasecmp(type, "multipart/", 10)) {
308 if (0 == strcasecmp(type+10, "mixed")) {
309 parse(xfind_token(tokens, "boundary="), argv);
310 } else
311 bb_error_msg_and_die("no support of content type '%s'", type);
312 } else {
313 pid_t pid = pid;
314 int rc;
315 FILE *fp;
316 // fetch charset
317 const char *charset = find_token(tokens, "charset=", CONFIG_FEATURE_MIME_CHARSET);
318 // fetch encoding
319 const char *encoding = find_token(tokens, "Content-Transfer-Encoding:", "7bit");
320 // compose target filename
321 char *filename = (char *)find_token(tokens, "filename=", NULL);
322 if (!filename)
323 filename = xasprintf(uniq, monotonic_us());
324 else
325 filename = bb_get_last_path_component_strip(xstrdup(filename));
326
327 // start external helper, if any
328 if (opts & OPT_X) {
329 int fd[2];
330 xpipe(fd);
Denis Vlasenko44f96d32008-11-09 02:23:42 +0000331 pid = vfork();
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000332 if (0 == pid) {
333 // child reads from fd[0]
Denys Vlasenkoa51543a2009-07-07 07:52:34 +0200334 close(fd[1]);
335 xmove_fd(fd[0], STDIN_FILENO);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000336 xsetenv("CONTENT_TYPE", type);
337 xsetenv("CHARSET", charset);
338 xsetenv("ENCODING", encoding);
339 xsetenv("FILENAME", filename);
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200340 BB_EXECVP_or_die(argv);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000341 }
342 // parent dumps to fd[1]
343 close(fd[0]);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100344 fp = xfdopen_for_write(fd[1]);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000345 signal(SIGPIPE, SIG_IGN); // ignore EPIPE
346 // or create a file for dump
347 } else {
348 char *fname = xasprintf("%s%s", *argv, filename);
349 fp = xfopen_for_write(fname);
350 free(fname);
351 }
352
353 // housekeeping
354 free(filename);
355
356 // dump to fp
357 if (0 == strcasecmp(encoding, "base64")) {
Denys Vlasenko9fe98f72010-09-16 17:51:13 +0200358 read_base64(stdin, fp, '-');
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000359 } else if (0 != strcasecmp(encoding, "7bit")
Denys Vlasenkoa51543a2009-07-07 07:52:34 +0200360 && 0 != strcasecmp(encoding, "8bit")
361 ) {
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000362 // quoted-printable, binary, user-defined are unsupported so far
363 bb_error_msg_and_die("no support of encoding '%s'", encoding);
364 } else {
365 // N.B. we have written redundant \n. so truncate the file
366 // The following weird 2-tacts reading technique is due to
367 // we have to not write extra \n at the end of the file
368 // In case of -x option we could truncate the resulting file as
369 // fseek(fp, -1, SEEK_END);
370 // if (ftruncate(fileno(fp), ftell(fp)))
371 // bb_perror_msg("ftruncate");
372 // But in case of -X we have to be much more careful. There is
373 // no means to truncate what we already have sent to the helper.
374 p = xmalloc_fgets_str(stdin, "\r\n");
375 while (p) {
Denys Vlasenkoa51543a2009-07-07 07:52:34 +0200376 s = xmalloc_fgets_str(stdin, "\r\n");
377 if (s == NULL)
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000378 break;
Denys Vlasenkoa51543a2009-07-07 07:52:34 +0200379 if ('-' == s[0]
380 && '-' == s[1]
381 && 0 == strncmp(s+2, boundary, boundary_len)
382 ) {
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000383 break;
Denys Vlasenkoa51543a2009-07-07 07:52:34 +0200384 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000385 fputs(p, fp);
386 p = s;
387 }
388
389/*
390 while ((s = xmalloc_fgetline_str(stdin, "\r\n")) != NULL) {
391 if ('-' == s[0] && '-' == s[1]
392 && 0 == strncmp(s+2, boundary, boundary_len))
393 break;
394 fprintf(fp, "%s\n", s);
395 }
396 // N.B. we have written redundant \n. so truncate the file
397 fseek(fp, -1, SEEK_END);
398 if (ftruncate(fileno(fp), ftell(fp)))
399 bb_perror_msg("ftruncate");
400*/
401 }
402 fclose(fp);
403
404 // finalize helper
405 if (opts & OPT_X) {
406 signal(SIGPIPE, SIG_DFL);
407 // exit if helper exited >0
Denys Vlasenko8531d762010-03-18 22:44:00 +0100408 rc = (wait4pid(pid) & 0xff);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000409 if (rc)
410 return rc+20;
411 }
412
413 // check multipart finalized
414 if (s && '-' == s[2+boundary_len] && '-' == s[2+boundary_len+1]) {
415 free(line);
416 break;
417 }
418 }
419 next:
Denys Vlasenko3581c622010-01-25 13:39:24 +0100420 free(line);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000421 }
422
423//bb_info_msg("ENDPARSE[%s]", boundary);
424
425 return EXIT_SUCCESS;
426}
427
428/*
429Usage: reformime [options]
430 -d - parse a delivery status notification.
431 -e - extract contents of MIME section.
432 -x - extract MIME section to a file.
433 -X - pipe MIME section to a program.
434 -i - show MIME info.
435 -s n.n.n.n - specify MIME section.
436 -r - rewrite message, filling in missing MIME headers.
437 -r7 - also convert 8bit/raw encoding to quoted-printable, if possible.
438 -r8 - also convert quoted-printable encoding to 8bit, if possible.
439 -c charset - default charset for rewriting, -o, and -O.
440 -m [file] [file]... - create a MIME message digest.
441 -h "header" - decode RFC 2047-encoded header.
442 -o "header" - encode unstructured header using RFC 2047.
443 -O "header" - encode address list header using RFC 2047.
444*/
445
446int reformime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
447int reformime_main(int argc UNUSED_PARAM, char **argv)
448{
449 const char *opt_prefix = "";
450
451 INIT_G();
452
453 // parse options
454 // N.B. only -x and -X are supported so far
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000455 opt_complementary = "x--X:X--x" IF_FEATURE_REFORMIME_COMPAT(":m::");
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000456 opts = getopt32(argv,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000457 "x:X" IF_FEATURE_REFORMIME_COMPAT("deis:r:c:m:h:o:O:"),
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000458 &opt_prefix
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000459 IF_FEATURE_REFORMIME_COMPAT(, NULL, NULL, &G.opt_charset, NULL, NULL, NULL, NULL)
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000460 );
461 //argc -= optind;
462 argv += optind;
463
464 return parse("", (opts & OPT_X) ? argv : (char **)&opt_prefix);
465}