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