3
0

popmaildir.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * popmaildir: a simple yet powerful POP3 client
  4. * Delivers contents of remote mailboxes to local Maildir
  5. *
  6. * Inspired by original utility by Nikola Vladov
  7. *
  8. * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
  9. *
  10. * Licensed under GPLv2, see file LICENSE in this source tree.
  11. */
  12. //kbuild:lib-$(CONFIG_POPMAILDIR) += popmaildir.o mail.o
  13. //usage:#define popmaildir_trivial_usage
  14. //usage: "[OPTIONS] MAILDIR [CONN_HELPER ARGS]"
  15. //usage:#define popmaildir_full_usage "\n\n"
  16. //usage: "Fetch content of remote mailbox to local maildir\n"
  17. /* //usage: "\n -b Binary mode. Ignored" */
  18. /* //usage: "\n -d Debug. Ignored" */
  19. /* //usage: "\n -m Show used memory. Ignored" */
  20. /* //usage: "\n -V Show version. Ignored" */
  21. /* //usage: "\n -c Use tcpclient. Ignored" */
  22. /* //usage: "\n -a Use APOP protocol. Implied. If server supports APOP -> use it" */
  23. //usage: "\n -s Skip authorization"
  24. //usage: "\n -T Get messages with TOP instead of RETR"
  25. //usage: "\n -k Keep retrieved messages on the server"
  26. //usage: "\n -t SEC Network timeout"
  27. //usage: IF_FEATURE_POPMAILDIR_DELIVERY(
  28. //usage: "\n -F \"PROG ARGS\" Filter program (may be repeated)"
  29. //usage: "\n -M \"PROG ARGS\" Delivery program"
  30. //usage: )
  31. //usage: "\n"
  32. //usage: "\nFetch from plain POP3 server:"
  33. //usage: "\npopmaildir -k DIR nc pop3.server.com 110 <user_and_pass.txt"
  34. //usage: "\nFetch from SSLed POP3 server and delete fetched emails:"
  35. //usage: "\npopmaildir DIR -- openssl s_client -quiet -connect pop3.server.com:995 <user_and_pass.txt"
  36. /* //usage: "\n -R BYTES Remove old messages on the server >= BYTES. Ignored" */
  37. /* //usage: "\n -Z N1-N2 Remove messages from N1 to N2 (dangerous). Ignored" */
  38. /* //usage: "\n -L BYTES Don't retrieve new messages >= BYTES. Ignored" */
  39. /* //usage: "\n -H LINES Type first LINES of a message. Ignored" */
  40. //usage:
  41. //usage:#define popmaildir_example_usage
  42. //usage: "$ popmaildir -k ~/Maildir -- nc pop.drvv.ru 110 [<password_file]\n"
  43. //usage: "$ popmaildir ~/Maildir -- openssl s_client -quiet -connect pop.gmail.com:995 [<password_file]\n"
  44. #include "libbb.h"
  45. #include "mail.h"
  46. static void pop3_checkr(const char *fmt, const char *param, char **ret)
  47. {
  48. char *msg = send_mail_command(fmt, param);
  49. char *answer = xmalloc_fgetline(stdin);
  50. if (answer && '+' == answer[0]) {
  51. free(msg);
  52. if (timeout)
  53. alarm(0);
  54. if (ret) {
  55. // skip "+OK "
  56. memmove(answer, answer + 4, strlen(answer) - 4);
  57. *ret = answer;
  58. } else
  59. free(answer);
  60. return;
  61. }
  62. bb_error_msg_and_die("%s failed, reply was: %s", msg, answer);
  63. }
  64. static void pop3_check(const char *fmt, const char *param)
  65. {
  66. pop3_checkr(fmt, param, NULL);
  67. }
  68. int popmaildir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  69. int popmaildir_main(int argc UNUSED_PARAM, char **argv)
  70. {
  71. char *buf;
  72. unsigned nmsg;
  73. char *hostname;
  74. pid_t pid;
  75. const char *retr;
  76. #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
  77. const char *delivery;
  78. #endif
  79. unsigned opt_nlines = 0;
  80. enum {
  81. OPT_b = 1 << 0, // -b binary mode. Ignored
  82. OPT_d = 1 << 1, // -d,-dd,-ddd debug. Ignored
  83. OPT_m = 1 << 2, // -m show used memory. Ignored
  84. OPT_V = 1 << 3, // -V version. Ignored
  85. OPT_c = 1 << 4, // -c use tcpclient. Ignored
  86. OPT_a = 1 << 5, // -a use APOP protocol
  87. OPT_s = 1 << 6, // -s skip authorization
  88. OPT_T = 1 << 7, // -T get messages with TOP instead with RETR
  89. OPT_k = 1 << 8, // -k keep retrieved messages on the server
  90. OPT_t = 1 << 9, // -t90 set timeout to 90 sec
  91. OPT_R = 1 << 10, // -R20000 remove old messages on the server >= 20000 bytes (requires -k). Ignored
  92. OPT_Z = 1 << 11, // -Z11-23 remove messages from 11 to 23 (dangerous). Ignored
  93. OPT_L = 1 << 12, // -L50000 not retrieve new messages >= 50000 bytes. Ignored
  94. OPT_H = 1 << 13, // -H30 type first 30 lines of a message; (-L12000 -H30). Ignored
  95. OPT_M = 1 << 14, // -M\"program arg1 arg2 ...\"; deliver by program. Treated like -F
  96. OPT_F = 1 << 15, // -F\"program arg1 arg2 ...\"; filter by program. Treated like -M
  97. };
  98. // init global variables
  99. INIT_G();
  100. // parse options
  101. opt_complementary = "-1:dd:t+:R+:L+:H+";
  102. opts = getopt32(argv,
  103. "bdmVcasTkt:" "R:Z:L:H:" IF_FEATURE_POPMAILDIR_DELIVERY("M:F:"),
  104. &timeout, NULL, NULL, NULL, &opt_nlines
  105. IF_FEATURE_POPMAILDIR_DELIVERY(, &delivery, &delivery) // we treat -M and -F the same
  106. );
  107. //argc -= optind;
  108. argv += optind;
  109. // get auth info
  110. if (!(opts & OPT_s))
  111. get_cred_or_die(STDIN_FILENO);
  112. // goto maildir
  113. xchdir(*argv++);
  114. // launch connect helper, if any
  115. if (*argv)
  116. launch_helper((const char **)argv);
  117. // get server greeting
  118. pop3_checkr(NULL, NULL, &buf);
  119. // authenticate (if no -s given)
  120. if (!(opts & OPT_s)) {
  121. // server supports APOP and we want it?
  122. if ('<' == buf[0] && (opts & OPT_a)) {
  123. union { // save a bit of stack
  124. md5_ctx_t ctx;
  125. char hex[16 * 2 + 1];
  126. } md5;
  127. uint32_t res[16 / 4];
  128. char *s = strchr(buf, '>');
  129. if (s)
  130. s[1] = '\0';
  131. // get md5 sum of "<stamp>password" string
  132. md5_begin(&md5.ctx);
  133. md5_hash(&md5.ctx, buf, strlen(buf));
  134. md5_hash(&md5.ctx, G.pass, strlen(G.pass));
  135. md5_end(&md5.ctx, res);
  136. *bin2hex(md5.hex, (char*)res, 16) = '\0';
  137. // APOP
  138. s = xasprintf("%s %s", G.user, md5.hex);
  139. pop3_check("APOP %s", s);
  140. free(s);
  141. free(buf);
  142. // server ignores APOP -> use simple text authentication
  143. } else {
  144. // USER
  145. pop3_check("USER %s", G.user);
  146. // PASS
  147. pop3_check("PASS %s", G.pass);
  148. }
  149. }
  150. // get mailbox statistics
  151. pop3_checkr("STAT", NULL, &buf);
  152. // prepare message filename suffix
  153. hostname = safe_gethostname();
  154. pid = getpid();
  155. // get messages counter
  156. // NOTE: we don't use xatou(buf) since buf is "nmsg nbytes"
  157. // we only need nmsg and atoi is just exactly what we need
  158. // if atoi fails to convert buf into number it returns 0
  159. // in this case the following loop simply will not be executed
  160. nmsg = atoi(buf);
  161. free(buf);
  162. // loop through messages
  163. retr = (opts & OPT_T) ? xasprintf("TOP %%u %u", opt_nlines) : "RETR %u";
  164. for (; nmsg; nmsg--) {
  165. char *filename;
  166. char *target;
  167. char *answer;
  168. FILE *fp;
  169. #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
  170. int rc;
  171. #endif
  172. // generate unique filename
  173. filename = xasprintf("tmp/%llu.%u.%s",
  174. monotonic_us(), (unsigned)pid, hostname);
  175. // retrieve message in ./tmp/ unless filter is specified
  176. pop3_check(retr, (const char *)(ptrdiff_t)nmsg);
  177. #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
  178. // delivery helper ordered? -> setup pipe
  179. if (opts & (OPT_F|OPT_M)) {
  180. // helper will have $FILENAME set to filename
  181. xsetenv("FILENAME", filename);
  182. fp = popen(delivery, "w");
  183. unsetenv("FILENAME");
  184. if (!fp) {
  185. bb_perror_msg("delivery helper");
  186. break;
  187. }
  188. } else
  189. #endif
  190. // create and open file filename
  191. fp = xfopen_for_write(filename);
  192. // copy stdin to fp (either filename or delivery helper)
  193. while ((answer = xmalloc_fgets_str(stdin, "\r\n")) != NULL) {
  194. char *s = answer;
  195. if ('.' == answer[0]) {
  196. if ('.' == answer[1])
  197. s++;
  198. else if ('\r' == answer[1] && '\n' == answer[2] && '\0' == answer[3])
  199. break;
  200. }
  201. //*strchrnul(s, '\r') = '\n';
  202. fputs(s, fp);
  203. free(answer);
  204. }
  205. #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
  206. // analyse delivery status
  207. if (opts & (OPT_F|OPT_M)) {
  208. rc = pclose(fp);
  209. if (99 == rc) // 99 means bail out
  210. break;
  211. // if (rc) // !0 means skip to the next message
  212. goto skip;
  213. // // 0 means continue
  214. } else {
  215. // close filename
  216. fclose(fp);
  217. }
  218. #endif
  219. // delete message from server
  220. if (!(opts & OPT_k))
  221. pop3_check("DELE %u", (const char*)(ptrdiff_t)nmsg);
  222. // atomically move message to ./new/
  223. target = xstrdup(filename);
  224. strncpy(target, "new", 3);
  225. // ... or just stop receiving on failure
  226. if (rename_or_warn(filename, target))
  227. break;
  228. free(target);
  229. #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
  230. skip:
  231. #endif
  232. free(filename);
  233. }
  234. // Bye
  235. pop3_check("QUIT", NULL);
  236. if (ENABLE_FEATURE_CLEAN_UP) {
  237. free(G.user);
  238. free(G.pass);
  239. }
  240. return EXIT_SUCCESS;
  241. }