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