123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- #include "libbb.h"
- #include "mail.h"
- static void pop3_checkr(const char *fmt, const char *param, char **ret)
- {
- char *msg = send_mail_command(fmt, param);
- char *answer = xmalloc_fgetline(stdin);
- if (answer && '+' == answer[0]) {
- free(msg);
- if (G.timeout)
- alarm(0);
- if (ret) {
-
- memmove(answer, answer + 4, strlen(answer) - 4);
- *ret = answer;
- } else
- free(answer);
- return;
- }
- bb_error_msg_and_die("%s failed, reply was: %s", msg, answer);
- }
- static void pop3_check(const char *fmt, const char *param)
- {
- pop3_checkr(fmt, param, NULL);
- }
- int popmaildir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int popmaildir_main(int argc UNUSED_PARAM, char **argv)
- {
- unsigned opts;
- char *buf;
- unsigned nmsg;
- char *hostname;
- pid_t pid;
- const char *retr;
- #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
- const char *delivery;
- #endif
- unsigned opt_nlines = 0;
- enum {
- OPT_b = 1 << 0,
- OPT_d = 1 << 1,
- OPT_m = 1 << 2,
- OPT_V = 1 << 3,
- OPT_c = 1 << 4,
- OPT_a = 1 << 5,
- OPT_s = 1 << 6,
- OPT_T = 1 << 7,
- OPT_k = 1 << 8,
- OPT_t = 1 << 9,
- OPT_R = 1 << 10,
- OPT_Z = 1 << 11,
- OPT_L = 1 << 12,
- OPT_H = 1 << 13,
- OPT_M = 1 << 14,
- OPT_F = 1 << 15,
- };
-
- INIT_G();
-
- opts = getopt32(argv, "^"
- "bdmVcasTkt:+" "R:+Z:L:+H:+" IF_FEATURE_POPMAILDIR_DELIVERY("M:F:")
- "\0" "-1:dd",
- &G.timeout, NULL, NULL, NULL, &opt_nlines
- IF_FEATURE_POPMAILDIR_DELIVERY(, &delivery, &delivery)
- );
-
- argv += optind;
-
- if (!(opts & OPT_s))
- get_cred_or_die(STDIN_FILENO);
-
- xchdir(*argv++);
-
- if (*argv)
- launch_helper((const char **)argv);
-
- pop3_checkr(NULL, NULL, &buf);
-
- if (!(opts & OPT_s)) {
-
- if ('<' == buf[0] && (opts & OPT_a)) {
- union {
- md5_ctx_t ctx;
- char hex[16 * 2 + 1];
- } md5;
- uint32_t res[MD5_OUTSIZE / 4];
- char *s = strchr(buf, '>');
- if (s)
- s[1] = '\0';
-
- md5_begin(&md5.ctx);
- md5_hash(&md5.ctx, buf, strlen(buf));
- md5_hash(&md5.ctx, G.pass, strlen(G.pass));
- md5_end(&md5.ctx, res);
- *bin2hex(md5.hex, (char*)res, 16) = '\0';
-
- s = xasprintf("%s %s", G.user, md5.hex);
- pop3_check("APOP %s", s);
- free(s);
- free(buf);
-
- } else {
-
- pop3_check("USER %s", G.user);
-
- pop3_check("PASS %s", G.pass);
- }
- }
-
- pop3_checkr("STAT", NULL, &buf);
-
- hostname = safe_gethostname();
- pid = getpid();
-
-
-
-
-
- nmsg = atoi(buf);
- free(buf);
-
- retr = (opts & OPT_T) ? xasprintf("TOP %%u %u", opt_nlines) : "RETR %u";
- for (; nmsg; nmsg--) {
- char *filename;
- char *target;
- char *answer;
- FILE *fp;
- #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
- int rc;
- #endif
-
- filename = xasprintf("tmp/%llu.%u.%s",
- monotonic_us(), (unsigned)pid, hostname);
-
- pop3_check(retr, (const char *)(ptrdiff_t)nmsg);
- #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
-
- if (opts & (OPT_F|OPT_M)) {
-
- xsetenv("FILENAME", filename);
- fp = popen(delivery, "w");
- unsetenv("FILENAME");
- if (!fp) {
- bb_simple_perror_msg("delivery helper");
- break;
- }
- } else
- #endif
-
- fp = xfopen_for_write(filename);
-
- while ((answer = xmalloc_fgets_str(stdin, "\r\n")) != NULL) {
- char *s = answer;
- if ('.' == answer[0]) {
- if ('.' == answer[1])
- s++;
- else if ('\r' == answer[1] && '\n' == answer[2] && '\0' == answer[3])
- break;
- }
-
- fputs(s, fp);
- free(answer);
- }
- #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
-
- if (opts & (OPT_F|OPT_M)) {
- rc = pclose(fp);
- if (99 == rc)
- break;
- goto skip;
- } else {
-
- fclose(fp);
- }
- #endif
-
- if (!(opts & OPT_k))
- pop3_check("DELE %u", (const char*)(ptrdiff_t)nmsg);
-
- target = xstrdup(filename);
- memcpy(target, "new", 3);
-
- if (rename_or_warn(filename, target))
- break;
- free(target);
- #if ENABLE_FEATURE_POPMAILDIR_DELIVERY
- skip:
- #endif
- free(filename);
- }
-
- pop3_check("QUIT", NULL);
- if (ENABLE_FEATURE_CLEAN_UP) {
- free(G.user);
- free(G.pass);
- }
- return EXIT_SUCCESS;
- }
|