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 ther 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
46 int makemime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
47 int makemime_main(int argc UNUSED_PARAM, char **argv)
49 llist_t *opt_headers = NULL, *l;
50 const char *opt_output;
51 #define boundary opt_output
54 OPT_c = 1 << 0, // Content-Type:
55 OPT_e = 1 << 1, // Content-Transfer-Encoding. Ignored. Assumed base64
56 OPT_o = 1 << 2, // output to
57 OPT_C = 1 << 3, // charset
58 OPT_N = 1 << 4, // COMPAT
59 OPT_a = 1 << 5, // additional headers
60 OPT_m = 1 << 6, // COMPAT
61 OPT_j = 1 << 7, // COMPAT
67 opt_complementary = "a::";
70 &G.content_type, NULL, &opt_output, &G.opt_charset, NULL, &opt_headers, NULL, NULL
77 freopen(opt_output, "w", stdout);
79 // no files given on command line? -> use stdin
81 *--argv = (char *)"-";
83 // put additional headers
84 for (l = opt_headers; l; l = l->link)
87 // make a random string -- it will delimit message parts
88 srand(monotonic_us());
89 boundary = xasprintf("%d-%d-%d", rand(), rand(), rand());
91 // put multipart header
94 "Content-Type: multipart/mixed; boundary=\"%s\"\n"
102 "Content-Type: %s; charset=%s\n"
103 "Content-Disposition: inline; filename=\"%s\"\n"
104 "Content-Transfer-Encoding: base64\n"
108 , bb_get_last_path_component_strip(*argv)
110 encode_base64(*argv++, (const char *)stdin, "");
113 // put multipart footer
114 printf("\n--%s--\n" "\n", boundary);
120 static const char *find_token(const char *const string_array[], const char *key, const char *defvalue)
122 const char *r = NULL;
123 for (int i = 0; string_array[i] != 0; i++) {
124 if (strcasecmp(string_array[i], key) == 0) {
125 r = (char *)string_array[i+1];
129 return (r) ? r : defvalue;
132 static const char *xfind_token(const char *const string_array[], const char *key)
134 const char *r = find_token(string_array, key, NULL);
137 bb_error_msg_and_die("header: %s", key);
143 #if ENABLE_FEATURE_REFORMIME_COMPAT
157 static int parse(const char *boundary, char **argv)
161 int boundary_len = strlen(boundary);
162 const char *delims = " ;\"\t\r\n";
165 const char *tokens[32]; // 32 is enough
167 // prepare unique string pattern
168 uniq = xasprintf("%%llu.%u.%s", (unsigned)getpid(), safe_gethostname());
170 //bb_info_msg("PARSE[%s]", terminator);
172 while ((line = xmalloc_fgets_str(stdin, "\r\n\r\n")) != NULL) {
174 // seek to start of MIME section
175 // N.B. to avoid false positives let us seek to the _last_ occurance
178 while ((s=strcasestr(s, "Content-Type:")) != NULL)
182 //bb_info_msg("L[%s]", p);
185 // TODO: strip of comments which are of form: (comment-text)
187 tokens[ntokens] = NULL;
188 for (s = strtok(p, delims); s; s = strtok(NULL, delims)) {
190 if (ntokens < ARRAY_SIZE(tokens) - 1)
192 //bb_info_msg("L[%d][%s]", ntokens, s);
194 tokens[ntokens] = NULL;
195 //bb_info_msg("N[%d]", ntokens);
198 type = find_token(tokens, "Content-Type:", "text/plain");
199 //bb_info_msg("T[%s]", type);
200 if (0 == strncasecmp(type, "multipart/", 10)) {
201 if (0 == strcasecmp(type+10, "mixed")) {
202 parse(xfind_token(tokens, "boundary="), argv);
204 bb_error_msg_and_die("no support of content type '%s'", type);
210 const char *charset = find_token(tokens, "charset=", CONFIG_FEATURE_MIME_CHARSET);
212 const char *encoding = find_token(tokens, "Content-Transfer-Encoding:", "7bit");
213 // compose target filename
214 char *filename = (char *)find_token(tokens, "filename=", NULL);
216 filename = xasprintf(uniq, monotonic_us());
218 filename = bb_get_last_path_component_strip(xstrdup(filename));
220 // start external helper, if any
226 // child reads from fd[0]
227 xdup2(fd[0], STDIN_FILENO);
228 close(fd[0]); close(fd[1]);
229 xsetenv("CONTENT_TYPE", type);
230 xsetenv("CHARSET", charset);
231 xsetenv("ENCODING", encoding);
232 xsetenv("FILENAME", filename);
233 BB_EXECVP(*argv, argv);
236 // parent dumps to fd[1]
238 fp = fdopen(fd[1], "w");
239 signal(SIGPIPE, SIG_IGN); // ignore EPIPE
240 // or create a file for dump
242 char *fname = xasprintf("%s%s", *argv, filename);
243 fp = xfopen_for_write(fname);
251 if (0 == strcasecmp(encoding, "base64")) {
252 decode_base64(stdin, fp);
253 } else if (0 != strcasecmp(encoding, "7bit")
254 && 0 != strcasecmp(encoding, "8bit")) {
255 // quoted-printable, binary, user-defined are unsupported so far
256 bb_error_msg_and_die("no support of encoding '%s'", encoding);
258 // N.B. we have written redundant \n. so truncate the file
259 // The following weird 2-tacts reading technique is due to
260 // we have to not write extra \n at the end of the file
261 // In case of -x option we could truncate the resulting file as
262 // fseek(fp, -1, SEEK_END);
263 // if (ftruncate(fileno(fp), ftell(fp)))
264 // bb_perror_msg("ftruncate");
265 // But in case of -X we have to be much more careful. There is
266 // no means to truncate what we already have sent to the helper.
267 p = xmalloc_fgets_str(stdin, "\r\n");
269 if ((s = xmalloc_fgets_str(stdin, "\r\n")) == NULL)
271 if ('-' == s[0] && '-' == s[1]
272 && 0 == strncmp(s+2, boundary, boundary_len))
279 while ((s = xmalloc_fgetline_str(stdin, "\r\n")) != NULL) {
280 if ('-' == s[0] && '-' == s[1]
281 && 0 == strncmp(s+2, boundary, boundary_len))
283 fprintf(fp, "%s\n", s);
285 // N.B. we have written redundant \n. so truncate the file
286 fseek(fp, -1, SEEK_END);
287 if (ftruncate(fileno(fp), ftell(fp)))
288 bb_perror_msg("ftruncate");
295 signal(SIGPIPE, SIG_DFL);
296 // exit if helper exited >0
302 // check multipart finalized
303 if (s && '-' == s[2+boundary_len] && '-' == s[2+boundary_len+1]) {
312 //bb_info_msg("ENDPARSE[%s]", boundary);
318 Usage: reformime [options]
319 -d - parse a delivery status notification.
320 -e - extract contents of MIME section.
321 -x - extract MIME section to a file.
322 -X - pipe MIME section to a program.
324 -s n.n.n.n - specify MIME section.
325 -r - rewrite message, filling in missing MIME headers.
326 -r7 - also convert 8bit/raw encoding to quoted-printable, if possible.
327 -r8 - also convert quoted-printable encoding to 8bit, if possible.
328 -c charset - default charset for rewriting, -o, and -O.
329 -m [file] [file]... - create a MIME message digest.
330 -h "header" - decode RFC 2047-encoded header.
331 -o "header" - encode unstructured header using RFC 2047.
332 -O "header" - encode address list header using RFC 2047.
335 int reformime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
336 int reformime_main(int argc UNUSED_PARAM, char **argv)
338 const char *opt_prefix = "";
343 // N.B. only -x and -X are supported so far
344 opt_complementary = "x--X:X--x" IF_FEATURE_REFORMIME_COMPAT(":m::");
345 opts = getopt32(argv,
346 "x:X" IF_FEATURE_REFORMIME_COMPAT("deis:r:c:m:h:o:O:"),
348 IF_FEATURE_REFORMIME_COMPAT(, NULL, NULL, &G.opt_charset, NULL, NULL, NULL, NULL)
353 return parse("", (opts & OPT_X) ? argv : (char **)&opt_prefix);