1 /* vi: set sw=4 ts=4: */
3 * makemime: create MIME-encoded message
4 * reformime: parse MIME-encoded message
6 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
8 * Licensed under GPLv2, see file LICENSE in this tarball for details.
14 makemime -c type [-o file] [-e encoding] [-C charset] [-N name] \
15 [-a "Header: Contents"] file
16 -m [ type ] [-o file] [-e encoding] [-a "Header: Contents"] file
17 -j [-o file] file1 file2
20 file: filename - read or write from filename
21 - - read or write from stdin or stdout
22 &n - read or write from file descriptor n
23 \( opts \) - read from child process, that generates [ opts ]
27 -c type - create a new MIME section from "file" with this
28 Content-Type: (default is application/octet-stream).
29 -C charset - MIME charset of a new text/plain section.
30 -N name - MIME content name of the new mime section.
31 -m [ type ] - create a multipart mime section from "file" of this
32 Content-Type: (default is multipart/mixed).
33 -e encoding - use the given encoding (7bit, 8bit, quoted-printable,
34 or base64), instead of guessing. Omit "-e" and use
35 -c auto to set Content-Type: to text/plain or
36 application/octet-stream based on picked encoding.
37 -j file1 file2 - join mime section file2 to multipart section file1.
38 -o file - write the result to file, instead of stdout (not
39 allowed in child processes).
40 -a header - prepend an additional header to the output.
42 @file - read all of the above options from file, one option or
44 {which version of makemime is this? What do we support?}
48 /* In busybox 1.15.0.svn, makemime generates output like this
49 * (empty lines are shown exactly!):
50 {headers added with -a HDR}
52 Content-Type: multipart/mixed; boundary="24269534-2145583448-1655890676"
54 --24269534-2145583448-1655890676
55 Content-Type: {set by -c, e.g. text/plain}; charset={set by -C, e.g. us-ascii}
56 Content-Disposition: inline; filename="A"
57 Content-Transfer-Encoding: base64
60 --24269534-2145583448-1655890676
61 Content-Type: {set by -c, e.g. text/plain}; charset={set by -C, e.g. us-ascii}
62 Content-Disposition: inline; filename="B"
63 Content-Transfer-Encoding: base64
66 --24269534-2145583448-1655890676--
71 /* For reference: here is an example email to LKML which has
72 * 1st unnamed part (so it serves as an email body)
73 * and one attached file:
75 Content-Type: multipart/mixed; boundary="=-tOfTf3byOS0vZgxEWcX+"
81 --=-tOfTf3byOS0vZgxEWcX+
82 Content-Type: text/plain
83 Content-Transfer-Encoding: 7bit
89 --=-tOfTf3byOS0vZgxEWcX+
90 Content-Disposition: attachment; filename="xyz"
91 Content-Type: text/plain; name="xyz"; charset="UTF-8"
92 Content-Transfer-Encoding: 7bit
97 --=-tOfTf3byOS0vZgxEWcX+--
99 ...random junk added by mailing list robots and such...
102 int makemime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
103 int makemime_main(int argc UNUSED_PARAM, char **argv)
105 llist_t *opt_headers = NULL, *l;
106 const char *opt_output;
107 #define boundary opt_output
110 OPT_c = 1 << 0, // Content-Type:
111 OPT_e = 1 << 1, // Content-Transfer-Encoding. Ignored. Assumed base64
112 OPT_o = 1 << 2, // output to
113 OPT_C = 1 << 3, // charset
114 OPT_N = 1 << 4, // COMPAT
115 OPT_a = 1 << 5, // additional headers
116 OPT_m = 1 << 6, // COMPAT
117 OPT_j = 1 << 7, // COMPAT
123 opt_complementary = "a::";
124 opts = getopt32(argv,
126 &G.content_type, NULL, &opt_output, &G.opt_charset, NULL, &opt_headers, NULL, NULL
133 freopen(opt_output, "w", stdout);
135 // no files given on command line? -> use stdin
137 *--argv = (char *)"-";
139 // put additional headers
140 for (l = opt_headers; l; l = l->link)
143 // make a random string -- it will delimit message parts
144 srand(monotonic_us());
145 boundary = xasprintf("%u-%u-%u",
146 (unsigned)rand(), (unsigned)rand(), (unsigned)rand());
148 // put multipart header
150 "Mime-Version: 1.0\n"
151 "Content-Type: multipart/mixed; boundary=\"%s\"\n"
159 "Content-Type: %s; charset=%s\n"
160 "Content-Disposition: inline; filename=\"%s\"\n"
161 "Content-Transfer-Encoding: base64\n"
165 , bb_get_last_path_component_strip(*argv)
167 encode_base64(*argv++, (const char *)stdin, "");
170 // put multipart footer
171 printf("\n--%s--\n" "\n", boundary);
177 static const char *find_token(const char *const string_array[], const char *key, const char *defvalue)
179 const char *r = NULL;
180 for (int i = 0; string_array[i] != 0; i++) {
181 if (strcasecmp(string_array[i], key) == 0) {
182 r = (char *)string_array[i+1];
186 return (r) ? r : defvalue;
189 static const char *xfind_token(const char *const string_array[], const char *key)
191 const char *r = find_token(string_array, key, NULL);
194 bb_error_msg_and_die("header: %s", key);
200 #if ENABLE_FEATURE_REFORMIME_COMPAT
214 static int parse(const char *boundary, char **argv)
218 int boundary_len = strlen(boundary);
219 const char *delims = " ;\"\t\r\n";
222 const char *tokens[32]; // 32 is enough
224 // prepare unique string pattern
225 uniq = xasprintf("%%llu.%u.%s", (unsigned)getpid(), safe_gethostname());
227 //bb_info_msg("PARSE[%s]", terminator);
229 while ((line = xmalloc_fgets_str(stdin, "\r\n\r\n")) != NULL) {
231 // seek to start of MIME section
232 // N.B. to avoid false positives let us seek to the _last_ occurance
235 while ((s = strcasestr(s, "Content-Type:")) != NULL)
239 //bb_info_msg("L[%s]", p);
242 // TODO: strip of comments which are of form: (comment-text)
244 tokens[ntokens] = NULL;
245 for (s = strtok(p, delims); s; s = strtok(NULL, delims)) {
247 if (ntokens < ARRAY_SIZE(tokens) - 1)
249 //bb_info_msg("L[%d][%s]", ntokens, s);
251 tokens[ntokens] = NULL;
252 //bb_info_msg("N[%d]", ntokens);
255 type = find_token(tokens, "Content-Type:", "text/plain");
256 //bb_info_msg("T[%s]", type);
257 if (0 == strncasecmp(type, "multipart/", 10)) {
258 if (0 == strcasecmp(type+10, "mixed")) {
259 parse(xfind_token(tokens, "boundary="), argv);
261 bb_error_msg_and_die("no support of content type '%s'", type);
267 const char *charset = find_token(tokens, "charset=", CONFIG_FEATURE_MIME_CHARSET);
269 const char *encoding = find_token(tokens, "Content-Transfer-Encoding:", "7bit");
270 // compose target filename
271 char *filename = (char *)find_token(tokens, "filename=", NULL);
273 filename = xasprintf(uniq, monotonic_us());
275 filename = bb_get_last_path_component_strip(xstrdup(filename));
277 // start external helper, if any
283 // child reads from fd[0]
285 xmove_fd(fd[0], STDIN_FILENO);
286 xsetenv("CONTENT_TYPE", type);
287 xsetenv("CHARSET", charset);
288 xsetenv("ENCODING", encoding);
289 xsetenv("FILENAME", filename);
290 BB_EXECVP(*argv, argv);
293 // parent dumps to fd[1]
295 fp = fdopen(fd[1], "w");
296 signal(SIGPIPE, SIG_IGN); // ignore EPIPE
297 // or create a file for dump
299 char *fname = xasprintf("%s%s", *argv, filename);
300 fp = xfopen_for_write(fname);
308 if (0 == strcasecmp(encoding, "base64")) {
309 decode_base64(stdin, fp);
310 } else if (0 != strcasecmp(encoding, "7bit")
311 && 0 != strcasecmp(encoding, "8bit")
313 // quoted-printable, binary, user-defined are unsupported so far
314 bb_error_msg_and_die("no support of encoding '%s'", encoding);
316 // N.B. we have written redundant \n. so truncate the file
317 // The following weird 2-tacts reading technique is due to
318 // we have to not write extra \n at the end of the file
319 // In case of -x option we could truncate the resulting file as
320 // fseek(fp, -1, SEEK_END);
321 // if (ftruncate(fileno(fp), ftell(fp)))
322 // bb_perror_msg("ftruncate");
323 // But in case of -X we have to be much more careful. There is
324 // no means to truncate what we already have sent to the helper.
325 p = xmalloc_fgets_str(stdin, "\r\n");
327 s = xmalloc_fgets_str(stdin, "\r\n");
332 && 0 == strncmp(s+2, boundary, boundary_len)
341 while ((s = xmalloc_fgetline_str(stdin, "\r\n")) != NULL) {
342 if ('-' == s[0] && '-' == s[1]
343 && 0 == strncmp(s+2, boundary, boundary_len))
345 fprintf(fp, "%s\n", s);
347 // N.B. we have written redundant \n. so truncate the file
348 fseek(fp, -1, SEEK_END);
349 if (ftruncate(fileno(fp), ftell(fp)))
350 bb_perror_msg("ftruncate");
357 signal(SIGPIPE, SIG_DFL);
358 // exit if helper exited >0
364 // check multipart finalized
365 if (s && '-' == s[2+boundary_len] && '-' == s[2+boundary_len+1]) {
374 //bb_info_msg("ENDPARSE[%s]", boundary);
380 Usage: reformime [options]
381 -d - parse a delivery status notification.
382 -e - extract contents of MIME section.
383 -x - extract MIME section to a file.
384 -X - pipe MIME section to a program.
386 -s n.n.n.n - specify MIME section.
387 -r - rewrite message, filling in missing MIME headers.
388 -r7 - also convert 8bit/raw encoding to quoted-printable, if possible.
389 -r8 - also convert quoted-printable encoding to 8bit, if possible.
390 -c charset - default charset for rewriting, -o, and -O.
391 -m [file] [file]... - create a MIME message digest.
392 -h "header" - decode RFC 2047-encoded header.
393 -o "header" - encode unstructured header using RFC 2047.
394 -O "header" - encode address list header using RFC 2047.
397 int reformime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
398 int reformime_main(int argc UNUSED_PARAM, char **argv)
400 const char *opt_prefix = "";
405 // N.B. only -x and -X are supported so far
406 opt_complementary = "x--X:X--x" IF_FEATURE_REFORMIME_COMPAT(":m::");
407 opts = getopt32(argv,
408 "x:X" IF_FEATURE_REFORMIME_COMPAT("deis:r:c:m:h:o:O:"),
410 IF_FEATURE_REFORMIME_COMPAT(, NULL, NULL, &G.opt_charset, NULL, NULL, NULL, NULL)
415 return parse("", (opts & OPT_X) ? argv : (char **)&opt_prefix);