mime.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* 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. *
  8. * Licensed under GPLv2, see file LICENSE in this source tree.
  9. */
  10. #include "libbb.h"
  11. #include "mail.h"
  12. /*
  13. makemime -c type [-o file] [-e encoding] [-C charset] [-N name] \
  14. [-a "Header: Contents"] file
  15. -m [ type ] [-o file] [-e encoding] [-a "Header: Contents"] file
  16. -j [-o file] file1 file2
  17. @file
  18. file: filename - read or write from filename
  19. - - read or write from stdin or stdout
  20. &n - read or write from file descriptor n
  21. \( opts \) - read from child process, that generates [ opts ]
  22. Options:
  23. -c type - create a new MIME section from "file" with this
  24. Content-Type: (default is application/octet-stream).
  25. -C charset - MIME charset of a new text/plain section.
  26. -N name - MIME content name of the new mime section.
  27. -m [ type ] - create a multipart mime section from "file" of this
  28. Content-Type: (default is multipart/mixed).
  29. -e encoding - use the given encoding (7bit, 8bit, quoted-printable,
  30. or base64), instead of guessing. Omit "-e" and use
  31. -c auto to set Content-Type: to text/plain or
  32. application/octet-stream based on picked encoding.
  33. -j file1 file2 - join mime section file2 to multipart section file1.
  34. -o file - write the result to file, instead of stdout (not
  35. allowed in child processes).
  36. -a header - prepend an additional header to the output.
  37. @file - read all of the above options from file, one option or
  38. value on each line.
  39. {which version of makemime is this? What do we support?}
  40. */
  41. /* In busybox 1.15.0.svn, makemime generates output like this
  42. * (empty lines are shown exactly!):
  43. {headers added with -a HDR}
  44. Mime-Version: 1.0
  45. Content-Type: multipart/mixed; boundary="24269534-2145583448-1655890676"
  46. --24269534-2145583448-1655890676
  47. Content-Type: {set by -c, e.g. text/plain}; charset={set by -C, e.g. us-ascii}
  48. Content-Disposition: inline; filename="A"
  49. Content-Transfer-Encoding: base64
  50. ...file A contents...
  51. --24269534-2145583448-1655890676
  52. Content-Type: {set by -c, e.g. text/plain}; charset={set by -C, e.g. us-ascii}
  53. Content-Disposition: inline; filename="B"
  54. Content-Transfer-Encoding: base64
  55. ...file B contents...
  56. --24269534-2145583448-1655890676--
  57. */
  58. /* For reference: here is an example email to LKML which has
  59. * 1st unnamed part (so it serves as an email body)
  60. * and one attached file:
  61. ...other headers...
  62. Content-Type: multipart/mixed; boundary="=-tOfTf3byOS0vZgxEWcX+"
  63. ...other headers...
  64. Mime-Version: 1.0
  65. ...other headers...
  66. --=-tOfTf3byOS0vZgxEWcX+
  67. Content-Type: text/plain
  68. Content-Transfer-Encoding: 7bit
  69. ...email text...
  70. ...email text...
  71. --=-tOfTf3byOS0vZgxEWcX+
  72. Content-Disposition: attachment; filename="xyz"
  73. Content-Type: text/plain; name="xyz"; charset="UTF-8"
  74. Content-Transfer-Encoding: 7bit
  75. ...file contents...
  76. ...file contents...
  77. --=-tOfTf3byOS0vZgxEWcX+--
  78. ...random junk added by mailing list robots and such...
  79. */
  80. int makemime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  81. int makemime_main(int argc UNUSED_PARAM, char **argv)
  82. {
  83. llist_t *opt_headers = NULL, *l;
  84. const char *opt_output;
  85. #define boundary opt_output
  86. enum {
  87. OPT_c = 1 << 0, // Content-Type:
  88. OPT_e = 1 << 1, // Content-Transfer-Encoding. Ignored. Assumed base64
  89. OPT_o = 1 << 2, // output to
  90. OPT_C = 1 << 3, // charset
  91. OPT_N = 1 << 4, // COMPAT
  92. OPT_a = 1 << 5, // additional headers
  93. OPT_m = 1 << 6, // COMPAT
  94. OPT_j = 1 << 7, // COMPAT
  95. };
  96. INIT_G();
  97. // parse options
  98. opt_complementary = "a::";
  99. opts = getopt32(argv,
  100. "c:e:o:C:N:a:m:j:",
  101. &G.content_type, NULL, &opt_output, &G.opt_charset, NULL, &opt_headers, NULL, NULL
  102. );
  103. //argc -= optind;
  104. argv += optind;
  105. // respect -o output
  106. if (opts & OPT_o)
  107. freopen(opt_output, "w", stdout);
  108. // no files given on command line? -> use stdin
  109. if (!*argv)
  110. *--argv = (char *)"-";
  111. // put additional headers
  112. for (l = opt_headers; l; l = l->link)
  113. puts(l->data);
  114. // make a random string -- it will delimit message parts
  115. srand(monotonic_us());
  116. boundary = xasprintf("%u-%u-%u",
  117. (unsigned)rand(), (unsigned)rand(), (unsigned)rand());
  118. // put multipart header
  119. printf(
  120. "Mime-Version: 1.0\n"
  121. "Content-Type: multipart/mixed; boundary=\"%s\"\n"
  122. , boundary
  123. );
  124. // put attachments
  125. while (*argv) {
  126. printf(
  127. "\n--%s\n"
  128. "Content-Type: %s; charset=%s\n"
  129. "Content-Disposition: inline; filename=\"%s\"\n"
  130. "Content-Transfer-Encoding: base64\n"
  131. , boundary
  132. , G.content_type
  133. , G.opt_charset
  134. , bb_get_last_path_component_strip(*argv)
  135. );
  136. encode_base64(*argv++, (const char *)stdin, "");
  137. }
  138. // put multipart footer
  139. printf("\n--%s--\n" "\n", boundary);
  140. return EXIT_SUCCESS;
  141. #undef boundary
  142. }
  143. static const char *find_token(const char *const string_array[], const char *key, const char *defvalue)
  144. {
  145. const char *r = NULL;
  146. int i;
  147. for (i = 0; string_array[i] != NULL; i++) {
  148. if (strcasecmp(string_array[i], key) == 0) {
  149. r = (char *)string_array[i+1];
  150. break;
  151. }
  152. }
  153. return (r) ? r : defvalue;
  154. }
  155. static const char *xfind_token(const char *const string_array[], const char *key)
  156. {
  157. const char *r = find_token(string_array, key, NULL);
  158. if (r)
  159. return r;
  160. bb_error_msg_and_die("header: %s", key);
  161. }
  162. enum {
  163. OPT_x = 1 << 0,
  164. OPT_X = 1 << 1,
  165. #if ENABLE_FEATURE_REFORMIME_COMPAT
  166. OPT_d = 1 << 2,
  167. OPT_e = 1 << 3,
  168. OPT_i = 1 << 4,
  169. OPT_s = 1 << 5,
  170. OPT_r = 1 << 6,
  171. OPT_c = 1 << 7,
  172. OPT_m = 1 << 8,
  173. OPT_h = 1 << 9,
  174. OPT_o = 1 << 10,
  175. OPT_O = 1 << 11,
  176. #endif
  177. };
  178. static int parse(const char *boundary, char **argv)
  179. {
  180. char *line, *s, *p;
  181. const char *type;
  182. int boundary_len = strlen(boundary);
  183. const char *delims = " ;\"\t\r\n";
  184. const char *uniq;
  185. int ntokens;
  186. const char *tokens[32]; // 32 is enough
  187. // prepare unique string pattern
  188. uniq = xasprintf("%%llu.%u.%s", (unsigned)getpid(), safe_gethostname());
  189. //bb_info_msg("PARSE[%s]", uniq);
  190. while ((line = xmalloc_fgets_str(stdin, "\r\n\r\n")) != NULL) {
  191. // seek to start of MIME section
  192. // N.B. to avoid false positives let us seek to the _last_ occurance
  193. p = NULL;
  194. s = line;
  195. while ((s = strcasestr(s, "Content-Type:")) != NULL)
  196. p = s++;
  197. if (!p)
  198. goto next;
  199. //bb_info_msg("L[%s]", p);
  200. // split to tokens
  201. // TODO: strip of comments which are of form: (comment-text)
  202. ntokens = 0;
  203. tokens[ntokens] = NULL;
  204. for (s = strtok(p, delims); s; s = strtok(NULL, delims)) {
  205. tokens[ntokens] = s;
  206. if (ntokens < ARRAY_SIZE(tokens) - 1)
  207. ntokens++;
  208. //bb_info_msg("L[%d][%s]", ntokens, s);
  209. }
  210. tokens[ntokens] = NULL;
  211. //bb_info_msg("N[%d]", ntokens);
  212. // analyse tokens
  213. type = find_token(tokens, "Content-Type:", "text/plain");
  214. //bb_info_msg("T[%s]", type);
  215. if (0 == strncasecmp(type, "multipart/", 10)) {
  216. if (0 == strcasecmp(type+10, "mixed")) {
  217. parse(xfind_token(tokens, "boundary="), argv);
  218. } else
  219. bb_error_msg_and_die("no support of content type '%s'", type);
  220. } else {
  221. pid_t pid = pid;
  222. int rc;
  223. FILE *fp;
  224. // fetch charset
  225. const char *charset = find_token(tokens, "charset=", CONFIG_FEATURE_MIME_CHARSET);
  226. // fetch encoding
  227. const char *encoding = find_token(tokens, "Content-Transfer-Encoding:", "7bit");
  228. // compose target filename
  229. char *filename = (char *)find_token(tokens, "filename=", NULL);
  230. if (!filename)
  231. filename = xasprintf(uniq, monotonic_us());
  232. else
  233. filename = bb_get_last_path_component_strip(xstrdup(filename));
  234. // start external helper, if any
  235. if (opts & OPT_X) {
  236. int fd[2];
  237. xpipe(fd);
  238. pid = vfork();
  239. if (0 == pid) {
  240. // child reads from fd[0]
  241. close(fd[1]);
  242. xmove_fd(fd[0], STDIN_FILENO);
  243. xsetenv("CONTENT_TYPE", type);
  244. xsetenv("CHARSET", charset);
  245. xsetenv("ENCODING", encoding);
  246. xsetenv("FILENAME", filename);
  247. BB_EXECVP_or_die(argv);
  248. }
  249. // parent dumps to fd[1]
  250. close(fd[0]);
  251. fp = xfdopen_for_write(fd[1]);
  252. signal(SIGPIPE, SIG_IGN); // ignore EPIPE
  253. // or create a file for dump
  254. } else {
  255. char *fname = xasprintf("%s%s", *argv, filename);
  256. fp = xfopen_for_write(fname);
  257. free(fname);
  258. }
  259. // housekeeping
  260. free(filename);
  261. // dump to fp
  262. if (0 == strcasecmp(encoding, "base64")) {
  263. read_base64(stdin, fp, '-');
  264. } else if (0 != strcasecmp(encoding, "7bit")
  265. && 0 != strcasecmp(encoding, "8bit")
  266. ) {
  267. // quoted-printable, binary, user-defined are unsupported so far
  268. bb_error_msg_and_die("no support of encoding '%s'", encoding);
  269. } else {
  270. // N.B. we have written redundant \n. so truncate the file
  271. // The following weird 2-tacts reading technique is due to
  272. // we have to not write extra \n at the end of the file
  273. // In case of -x option we could truncate the resulting file as
  274. // fseek(fp, -1, SEEK_END);
  275. // if (ftruncate(fileno(fp), ftell(fp)))
  276. // bb_perror_msg("ftruncate");
  277. // But in case of -X we have to be much more careful. There is
  278. // no means to truncate what we already have sent to the helper.
  279. p = xmalloc_fgets_str(stdin, "\r\n");
  280. while (p) {
  281. s = xmalloc_fgets_str(stdin, "\r\n");
  282. if (s == NULL)
  283. break;
  284. if ('-' == s[0]
  285. && '-' == s[1]
  286. && 0 == strncmp(s+2, boundary, boundary_len)
  287. ) {
  288. break;
  289. }
  290. fputs(p, fp);
  291. p = s;
  292. }
  293. /*
  294. while ((s = xmalloc_fgetline_str(stdin, "\r\n")) != NULL) {
  295. if ('-' == s[0] && '-' == s[1]
  296. && 0 == strncmp(s+2, boundary, boundary_len))
  297. break;
  298. fprintf(fp, "%s\n", s);
  299. }
  300. // N.B. we have written redundant \n. so truncate the file
  301. fseek(fp, -1, SEEK_END);
  302. if (ftruncate(fileno(fp), ftell(fp)))
  303. bb_perror_msg("ftruncate");
  304. */
  305. }
  306. fclose(fp);
  307. // finalize helper
  308. if (opts & OPT_X) {
  309. signal(SIGPIPE, SIG_DFL);
  310. // exit if helper exited >0
  311. rc = (wait4pid(pid) & 0xff);
  312. if (rc)
  313. return rc+20;
  314. }
  315. // check multipart finalized
  316. if (s && '-' == s[2+boundary_len] && '-' == s[2+boundary_len+1]) {
  317. free(line);
  318. break;
  319. }
  320. }
  321. next:
  322. free(line);
  323. }
  324. //bb_info_msg("ENDPARSE[%s]", boundary);
  325. return EXIT_SUCCESS;
  326. }
  327. /*
  328. Usage: reformime [options]
  329. -d - parse a delivery status notification.
  330. -e - extract contents of MIME section.
  331. -x - extract MIME section to a file.
  332. -X - pipe MIME section to a program.
  333. -i - show MIME info.
  334. -s n.n.n.n - specify MIME section.
  335. -r - rewrite message, filling in missing MIME headers.
  336. -r7 - also convert 8bit/raw encoding to quoted-printable, if possible.
  337. -r8 - also convert quoted-printable encoding to 8bit, if possible.
  338. -c charset - default charset for rewriting, -o, and -O.
  339. -m [file] [file]... - create a MIME message digest.
  340. -h "header" - decode RFC 2047-encoded header.
  341. -o "header" - encode unstructured header using RFC 2047.
  342. -O "header" - encode address list header using RFC 2047.
  343. */
  344. int reformime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  345. int reformime_main(int argc UNUSED_PARAM, char **argv)
  346. {
  347. const char *opt_prefix = "";
  348. INIT_G();
  349. // parse options
  350. // N.B. only -x and -X are supported so far
  351. opt_complementary = "x--X:X--x" IF_FEATURE_REFORMIME_COMPAT(":m::");
  352. opts = getopt32(argv,
  353. "x:X" IF_FEATURE_REFORMIME_COMPAT("deis:r:c:m:h:o:O:"),
  354. &opt_prefix
  355. IF_FEATURE_REFORMIME_COMPAT(, NULL, NULL, &G.opt_charset, NULL, NULL, NULL, NULL)
  356. );
  357. //argc -= optind;
  358. argv += optind;
  359. return parse("", (opts & OPT_X) ? argv : (char **)&opt_prefix);
  360. }