popmaildir.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 tarball for details.
  11. */
  12. #include "libbb.h"
  13. #include "mail.h"
  14. static void pop3_checkr(const char *fmt, const char *param, char **ret)
  15. {
  16. const char *msg = command(fmt, param);
  17. char *answer = xmalloc_fgetline(stdin);
  18. if (answer && '+' == answer[0]) {
  19. if (timeout)
  20. alarm(0);
  21. if (ret) {
  22. // skip "+OK "
  23. memmove(answer, answer + 4, strlen(answer) - 4);
  24. *ret = answer;
  25. } else
  26. free(answer);
  27. return;
  28. }
  29. bb_error_msg_and_die("%s failed: %s", msg, answer);
  30. }
  31. static void pop3_check(const char *fmt, const char *param)
  32. {
  33. pop3_checkr(fmt, param, NULL);
  34. }
  35. int popmaildir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  36. int popmaildir_main(int argc UNUSED_PARAM, char **argv)
  37. {
  38. char *buf;
  39. unsigned nmsg;
  40. char *hostname;
  41. pid_t pid;
  42. const char *retr;
  43. #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
  44. const char *delivery;
  45. #endif
  46. unsigned opt_nlines = 0;
  47. enum {
  48. OPT_b = 1 << 0, // -b binary mode. Ignored
  49. OPT_d = 1 << 1, // -d,-dd,-ddd debug. Ignored
  50. OPT_m = 1 << 2, // -m show used memory. Ignored
  51. OPT_V = 1 << 3, // -V version. Ignored
  52. OPT_c = 1 << 4, // -c use tcpclient. Ignored
  53. OPT_a = 1 << 5, // -a use APOP protocol
  54. OPT_s = 1 << 6, // -s skip authorization
  55. OPT_T = 1 << 7, // -T get messages with TOP instead with RETR
  56. OPT_k = 1 << 8, // -k keep retrieved messages on the server
  57. OPT_t = 1 << 9, // -t90 set timeout to 90 sec
  58. OPT_R = 1 << 10, // -R20000 remove old messages on the server >= 20000 bytes (requires -k). Ignored
  59. OPT_Z = 1 << 11, // -Z11-23 remove messages from 11 to 23 (dangerous). Ignored
  60. OPT_L = 1 << 12, // -L50000 not retrieve new messages >= 50000 bytes. Ignored
  61. OPT_H = 1 << 13, // -H30 type first 30 lines of a message; (-L12000 -H30). Ignored
  62. OPT_M = 1 << 14, // -M\"program arg1 arg2 ...\"; deliver by program. Treated like -F
  63. OPT_F = 1 << 15, // -F\"program arg1 arg2 ...\"; filter by program. Treated like -M
  64. };
  65. // init global variables
  66. INIT_G();
  67. // parse options
  68. opt_complementary = "-1:dd:t+:R+:L+:H+";
  69. opts = getopt32(argv,
  70. "bdmVcasTkt:" "R:Z:L:H:" USE_FEATURE_POPMAILDIR_DELIVERY("M:F:"),
  71. &timeout, NULL, NULL, NULL, &opt_nlines
  72. USE_FEATURE_POPMAILDIR_DELIVERY(, &delivery, &delivery) // we treat -M and -F the same
  73. );
  74. //argc -= optind;
  75. argv += optind;
  76. // get auth info
  77. if (!(opts & OPT_s))
  78. get_cred_or_die(STDIN_FILENO);
  79. // goto maildir
  80. xchdir(*argv++);
  81. // launch connect helper, if any
  82. if (*argv)
  83. launch_helper((const char **)argv);
  84. // get server greeting
  85. pop3_checkr(NULL, NULL, &buf);
  86. // authenticate (if no -s given)
  87. if (!(opts & OPT_s)) {
  88. // server supports APOP and we want it?
  89. if ('<' == buf[0] && (opts & OPT_a)) {
  90. union { // save a bit of stack
  91. md5_ctx_t ctx;
  92. char hex[16 * 2 + 1];
  93. } md5;
  94. uint32_t res[16 / 4];
  95. char *s = strchr(buf, '>');
  96. if (s)
  97. s[1] = '\0';
  98. // get md5 sum of "<stamp>password" string
  99. md5_begin(&md5.ctx);
  100. md5_hash(buf, strlen(buf), &md5.ctx);
  101. md5_hash(G.pass, strlen(G.pass), &md5.ctx);
  102. md5_end(res, &md5.ctx);
  103. *bin2hex(md5.hex, (char*)res, 16) = '\0';
  104. // APOP
  105. s = xasprintf("%s %s", G.user, md5.hex);
  106. pop3_check("APOP %s", s);
  107. free(s);
  108. free(buf);
  109. // server ignores APOP -> use simple text authentication
  110. } else {
  111. // USER
  112. pop3_check("USER %s", G.user);
  113. // PASS
  114. pop3_check("PASS %s", G.pass);
  115. }
  116. }
  117. // get mailbox statistics
  118. pop3_checkr("STAT", NULL, &buf);
  119. // prepare message filename suffix
  120. hostname = safe_gethostname();
  121. pid = getpid();
  122. // get messages counter
  123. // NOTE: we don't use xatou(buf) since buf is "nmsg nbytes"
  124. // we only need nmsg and atoi is just exactly what we need
  125. // if atoi fails to convert buf into number it returns 0
  126. // in this case the following loop simply will not be executed
  127. nmsg = atoi(buf);
  128. free(buf);
  129. // loop through messages
  130. retr = (opts & OPT_T) ? xasprintf("TOP %%u %u", opt_nlines) : "RETR %u";
  131. for (; nmsg; nmsg--) {
  132. char *filename;
  133. char *target;
  134. char *answer;
  135. FILE *fp;
  136. #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
  137. int rc;
  138. #endif
  139. // generate unique filename
  140. filename = xasprintf("tmp/%llu.%u.%s",
  141. monotonic_us(), (unsigned)pid, hostname);
  142. // retrieve message in ./tmp/ unless filter is specified
  143. pop3_check(retr, (const char *)(ptrdiff_t)nmsg);
  144. #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
  145. // delivery helper ordered? -> setup pipe
  146. if (opts & (OPT_F|OPT_M)) {
  147. // helper will have $FILENAME set to filename
  148. xsetenv("FILENAME", filename);
  149. fp = popen(delivery, "w");
  150. unsetenv("FILENAME");
  151. if (!fp) {
  152. bb_perror_msg("delivery helper");
  153. break;
  154. }
  155. } else
  156. #endif
  157. // create and open file filename
  158. fp = xfopen_for_write(filename);
  159. // copy stdin to fp (either filename or delivery helper)
  160. while ((answer = xmalloc_fgets_str(stdin, "\r\n")) != NULL) {
  161. char *s = answer;
  162. if ('.' == answer[0]) {
  163. if ('.' == answer[1])
  164. s++;
  165. else if ('\r' == answer[1] && '\n' == answer[2] && '\0' == answer[3])
  166. break;
  167. }
  168. //*strchrnul(s, '\r') = '\n';
  169. fputs(s, fp);
  170. free(answer);
  171. }
  172. #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
  173. // analyse delivery status
  174. if (opts & (OPT_F|OPT_M)) {
  175. rc = pclose(fp);
  176. if (99 == rc) // 99 means bail out
  177. break;
  178. // if (rc) // !0 means skip to the next message
  179. goto skip;
  180. // // 0 means continue
  181. } else {
  182. // close filename
  183. fclose(fp);
  184. }
  185. #endif
  186. // delete message from server
  187. if (!(opts & OPT_k))
  188. pop3_check("DELE %u", (const char*)(ptrdiff_t)nmsg);
  189. // atomically move message to ./new/
  190. target = xstrdup(filename);
  191. strncpy(target, "new", 3);
  192. // ... or just stop receiving on failure
  193. if (rename_or_warn(filename, target))
  194. break;
  195. free(target);
  196. #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
  197. skip:
  198. #endif
  199. free(filename);
  200. }
  201. // Bye
  202. pop3_check("QUIT", NULL);
  203. if (ENABLE_FEATURE_CLEAN_UP) {
  204. free(G.user);
  205. free(G.pass);
  206. }
  207. return EXIT_SUCCESS;
  208. }