Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 3 | * bare bones sendmail/fetchmail |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> |
| 6 | * |
| 7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | #include "libbb.h" |
| 10 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 11 | #define INITIAL_STDIN_FILENO 3 |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 13 | static void uuencode(char *fname, const char *text) |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 14 | { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 15 | enum { |
| 16 | SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */ |
| 17 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), |
| 18 | }; |
| 19 | |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 20 | #define src_buf text |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 21 | int fd; |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 22 | #define len fd |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 23 | char dst_buf[DST_BUF_SIZE + 1]; |
| 24 | |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 25 | if (fname) { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 26 | fd = INITIAL_STDIN_FILENO; |
| 27 | if (NOT_LONE_DASH(fname)) |
| 28 | fd = xopen(fname, O_RDONLY); |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 29 | src_buf = bb_common_bufsiz1; |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 30 | // N.B. strlen(NULL) segfaults! |
| 31 | } else if (text) { |
| 32 | // though we do not call uuencode(NULL, NULL) explicitly |
| 33 | // still we do not want to break things suddenly |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 34 | len = strlen(text); |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 35 | } else |
| 36 | return; |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 37 | |
| 38 | fflush(stdout); // sync stdio and unistd output |
| 39 | while (1) { |
| 40 | size_t size; |
| 41 | if (fname) { |
| 42 | size = full_read(fd, (char *)src_buf, SRC_BUF_SIZE); |
| 43 | if ((ssize_t)size < 0) |
| 44 | bb_perror_msg_and_die(bb_msg_read_error); |
| 45 | } else { |
| 46 | size = len; |
| 47 | if (len > SRC_BUF_SIZE) |
| 48 | size = SRC_BUF_SIZE; |
| 49 | } |
| 50 | if (!size) |
| 51 | break; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 52 | // encode the buffer we just read in |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 53 | bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); |
| 54 | if (fname) { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 55 | xwrite(STDOUT_FILENO, "\r\n", 2); |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 56 | } else { |
| 57 | src_buf += size; |
| 58 | len -= size; |
| 59 | } |
| 60 | xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3)); |
| 61 | } |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 62 | if (fname) |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 63 | close(fd); |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 66 | struct globals { |
| 67 | pid_t helper_pid; |
| 68 | unsigned timeout; |
| 69 | // arguments for SSL connection helper |
| 70 | const char *xargs[9]; |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 71 | // arguments for postprocess helper |
| 72 | const char *fargs[3]; |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 73 | }; |
| 74 | #define G (*ptr_to_globals) |
| 75 | #define helper_pid (G.helper_pid) |
| 76 | #define timeout (G.timeout ) |
| 77 | #define xargs (G.xargs ) |
| 78 | #define fargs (G.fargs ) |
| 79 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 80 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 81 | xargs[0] = "openssl"; \ |
| 82 | xargs[1] = "s_client"; \ |
| 83 | xargs[2] = "-quiet"; \ |
| 84 | xargs[3] = "-connect"; \ |
| 85 | /*xargs[4] = "server[:port]";*/ \ |
| 86 | xargs[5] = "-tls1"; \ |
| 87 | xargs[6] = "-starttls"; \ |
| 88 | xargs[7] = "smtp"; \ |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 89 | fargs[0] = "utf-8"; \ |
| 90 | } while (0) |
| 91 | |
| 92 | #define opt_connect (xargs[4]) |
| 93 | #define opt_after_connect (xargs[5]) |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 94 | #define opt_charset (fargs[0]) |
| 95 | #define opt_subject (fargs[1]) |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 96 | |
| 97 | static void kill_helper(void) |
| 98 | { |
| 99 | // TODO!!!: is there more elegant way to terminate child on program failure? |
| 100 | if (helper_pid > 0) |
| 101 | kill(helper_pid, SIGTERM); |
| 102 | } |
| 103 | |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 104 | // generic signal handler |
| 105 | static void signal_handler(int signo) |
| 106 | { |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 107 | #define err signo |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 108 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 109 | if (SIGALRM == signo) { |
| 110 | kill_helper(); |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 111 | bb_error_msg_and_die("timed out"); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 112 | } |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 113 | |
| 114 | // SIGCHLD. reap zombies |
| 115 | if (wait_any_nohang(&err) > 0) |
| 116 | if (WIFEXITED(err) && WEXITSTATUS(err)) |
| 117 | bb_error_msg_and_die("child exited (%d)", WEXITSTATUS(err)); |
| 118 | } |
| 119 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 120 | static void launch_helper(const char **argv) |
| 121 | { |
| 122 | // setup vanilla unidirectional pipes interchange |
| 123 | int idx; |
| 124 | int pipes[4]; |
| 125 | xpipe(pipes); |
| 126 | xpipe(pipes+2); |
| 127 | helper_pid = vfork(); |
| 128 | if (helper_pid < 0) |
| 129 | bb_perror_msg_and_die("vfork"); |
| 130 | idx = (!helper_pid)*2; |
| 131 | xdup2(pipes[idx], STDIN_FILENO); |
| 132 | xdup2(pipes[3-idx], STDOUT_FILENO); |
| 133 | if (ENABLE_FEATURE_CLEAN_UP) |
| 134 | for (int i = 4; --i >= 0; ) |
| 135 | if (pipes[i] > STDOUT_FILENO) |
| 136 | close(pipes[i]); |
| 137 | if (!helper_pid) { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 138 | // child: try to execute connection helper |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 139 | BB_EXECVP(*argv, (char **)argv); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 140 | _exit(127); |
| 141 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 142 | // parent: check whether child is alive |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 143 | bb_signals(0 |
| 144 | + (1 << SIGCHLD) |
| 145 | + (1 << SIGALRM) |
| 146 | , signal_handler); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 147 | signal_handler(SIGCHLD); |
| 148 | // child seems OK -> parent goes on |
| 149 | } |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 150 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 151 | static const char *command(const char *fmt, const char *param) |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 152 | { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 153 | const char *msg = fmt; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 154 | alarm(timeout); |
| 155 | if (msg) { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 156 | msg = xasprintf(fmt, param); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 157 | printf("%s\r\n", msg); |
| 158 | } |
| 159 | fflush(stdout); |
| 160 | return msg; |
| 161 | } |
| 162 | |
| 163 | static int smtp_checkp(const char *fmt, const char *param, int code) |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 164 | { |
| 165 | char *answer; |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 166 | const char *msg = command(fmt, param); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 167 | // read stdin |
| 168 | // if the string has a form \d\d\d- -- read next string. E.g. EHLO response |
| 169 | // parse first bytes to a number |
| 170 | // if code = -1 then just return this number |
| 171 | // if code != -1 then checks whether the number equals the code |
| 172 | // if not equal -> die saying msg |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 173 | while ((answer = xmalloc_fgetline(stdin)) != NULL) |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 174 | if (strlen(answer) <= 3 || '-' != answer[3]) |
| 175 | break; |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 176 | if (answer) { |
| 177 | int n = atoi(answer); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 178 | alarm(0); |
| 179 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 180 | free(answer); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 181 | } |
| 182 | if (-1 == code || n == code) { |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 183 | return n; |
| 184 | } |
| 185 | } |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 186 | kill_helper(); |
| 187 | bb_error_msg_and_die("%s failed", msg); |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 190 | static inline int smtp_check(const char *fmt, int code) |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 191 | { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 192 | return smtp_checkp(fmt, NULL, code); |
| 193 | } |
| 194 | |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 195 | // strip argument of bad chars |
| 196 | static char *sane(char *str) |
| 197 | { |
| 198 | char *s = str; |
| 199 | char *p = s; |
| 200 | while (*s) { |
| 201 | if (isalnum(*s) || '_' == *s || '-' == *s || '.' == *s || '@' == *s) { |
| 202 | *p++ = *s; |
| 203 | } |
| 204 | s++; |
| 205 | } |
| 206 | *p = '\0'; |
| 207 | return str; |
| 208 | } |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 209 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 210 | #if ENABLE_FETCHMAIL |
| 211 | static void pop3_checkr(const char *fmt, const char *param, char **ret) |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 212 | { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 213 | const char *msg = command(fmt, param); |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 214 | char *answer = xmalloc_fgetline(stdin); |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 215 | if (answer && '+' == *answer) { |
| 216 | alarm(0); |
| 217 | if (ret) |
| 218 | *ret = answer+4; // skip "+OK " |
| 219 | else if (ENABLE_FEATURE_CLEAN_UP) |
| 220 | free(answer); |
| 221 | return; |
| 222 | } |
| 223 | kill_helper(); |
| 224 | bb_error_msg_and_die("%s failed", msg); |
| 225 | } |
| 226 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 227 | static inline void pop3_check(const char *fmt, const char *param) |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 228 | { |
| 229 | pop3_checkr(fmt, param, NULL); |
| 230 | } |
| 231 | |
| 232 | static void pop3_message(const char *filename) |
| 233 | { |
| 234 | int fd; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 235 | char *answer; |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 236 | // create and open file filename |
| 237 | // read stdin, copy to created file |
| 238 | fd = xopen(filename, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL); |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 239 | while ((answer = xmalloc_fgets_str(stdin, "\r\n")) != NULL) { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 240 | char *s = answer; |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 241 | if ('.' == *answer) { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 242 | if ('.' == answer[1]) |
| 243 | s++; |
| 244 | else if ('\r' == answer[1] && '\n' == answer[2] && '\0' == answer[3]) |
| 245 | break; |
| 246 | } |
| 247 | xwrite(fd, s, strlen(s)); |
| 248 | free(answer); |
| 249 | } |
| 250 | close(fd); |
| 251 | } |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 252 | #endif |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 253 | |
| 254 | int sendgetmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 255 | int sendgetmail_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 256 | { |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 257 | llist_t *opt_recipients = NULL; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 258 | |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 259 | const char *opt_user; |
| 260 | const char *opt_pass; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 261 | |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 262 | enum { |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 263 | OPT_w = 1 << 0, // network timeout |
| 264 | OPT_U = 1 << 1, // user |
| 265 | OPT_P = 1 << 2, // password |
| 266 | OPT_X = 1 << 3, // connect using openssl s_client helper |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 267 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 268 | OPTS_n = 1 << 4, // sendmail: request notification |
| 269 | OPTF_t = 1 << 4, // fetchmail: use "TOP" not "RETR" |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 270 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 271 | OPTS_s = 1 << 5, // sendmail: subject |
| 272 | OPTF_z = 1 << 5, // fetchmail: delete from server |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 273 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 274 | OPTS_c = 1 << 6, // sendmail: assumed charset |
| 275 | OPTS_t = 1 << 7, // sendmail: recipient(s) |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 276 | OPTS_i = 1 << 8, // sendmail: ignore lone dots in message body (implied) |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 279 | const char *options; |
| 280 | unsigned opts; |
| 281 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 282 | // init global variables |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 283 | INIT_G(); |
| 284 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 285 | // parse options, different option sets for sendmail and fetchmail |
| 286 | // N.B. opt_after_connect hereafter is NULL if we are called as fetchmail |
| 287 | // and is NOT NULL if we are called as sendmail |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 288 | if (!ENABLE_FETCHMAIL || 's' == applet_name[0]) { |
| 289 | // SENDMAIL |
| 290 | // save initial stdin (body or attachements can be piped!) |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 291 | xdup2(STDIN_FILENO, INITIAL_STDIN_FILENO); |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 292 | opt_complementary = "-2:w+:t::"; |
| 293 | options = "w:U:P:X" "ns:c:t:i"; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 294 | } else { |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 295 | // FETCHMAIL |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 296 | opt_after_connect = NULL; |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 297 | opt_complementary = "-2:w+:P"; |
| 298 | options = "w:U:P:X" "tz"; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 299 | } |
| 300 | opts = getopt32(argv, options, |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 301 | &timeout, &opt_user, &opt_pass, |
| 302 | &opt_subject, &opt_charset, &opt_recipients |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 303 | ); |
| 304 | //argc -= optind; |
| 305 | argv += optind; |
| 306 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 307 | // first argument is remote server[:port] |
| 308 | opt_connect = *argv++; |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 309 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 310 | // connect to server |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 311 | // SSL ordered? -> |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 312 | if (opts & OPT_X) { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 313 | // ... use openssl helper |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 314 | launch_helper(xargs); |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 315 | // no SSL ordered? -> |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 316 | } else { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 317 | // ... make plain connect |
| 318 | int fd = create_and_connect_stream_or_die(opt_connect, 25); |
| 319 | // make ourselves a simple IO filter |
| 320 | // from now we know nothing about network :) |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 321 | xmove_fd(fd, STDIN_FILENO); |
| 322 | xdup2(STDIN_FILENO, STDOUT_FILENO); |
| 323 | } |
| 324 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 325 | #if ENABLE_FETCHMAIL |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 326 | // we are sendmail? |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 327 | if (opt_after_connect) |
| 328 | #endif |
| 329 | { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 330 | /*************************************************** |
| 331 | * SENDMAIL |
| 332 | ***************************************************/ |
| 333 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 334 | char *opt_from; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 335 | int code; |
| 336 | char *boundary; |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 337 | const char *fmt; |
| 338 | const char *p; |
| 339 | char *q; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 340 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 341 | // we didn't use SSL helper? -> |
| 342 | if (!(opts & OPT_X)) { |
| 343 | // ... wait for initial server OK |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 344 | smtp_check(NULL, 220); |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 345 | } |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 346 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 347 | // get the sender |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 348 | opt_from = sane(*argv++); |
| 349 | |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 350 | // if no recipients _and_ no body files specified -> enter all-included mode |
| 351 | // i.e. scan stdin for To: and Subject: lines ... |
| 352 | // ... and then use the rest of stdin as message body |
| 353 | if (!opt_recipients && !*argv) { |
| 354 | // fetch recipients and (optionally) subject |
| 355 | char *s; |
| 356 | while ((s = xmalloc_reads(INITIAL_STDIN_FILENO, NULL, NULL)) != NULL) { |
| 357 | if (0 == strncmp("To: ", s, 4)) { |
| 358 | llist_add_to_end(&opt_recipients, s+4); |
| 359 | } else if (0 == strncmp("Subject: ", s, 9)) { |
| 360 | opt_subject = s+9; |
| 361 | opts |= OPTS_s; |
| 362 | } else { |
| 363 | char first = s[0]; |
| 364 | free(s); |
| 365 | if (!first) |
| 366 | break; // empty line |
| 367 | } |
| 368 | } |
| 369 | // order to read body from stdin |
| 370 | *--argv = (char *)"-"; |
| 371 | } |
| 372 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 373 | // introduce to server |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 374 | // we should start with modern EHLO |
| 375 | if (250 != smtp_checkp("EHLO %s", opt_from, -1)) { |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 376 | smtp_checkp("HELO %s", opt_from, 250); |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 379 | // set sender |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 380 | // NOTE: if password has not been specified |
| 381 | // then no authentication is possible |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 382 | code = (opts & OPT_P) ? -1 : 250; |
| 383 | // first try softly without authentication |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 384 | while (250 != smtp_checkp("MAIL FROM:<%s>", opt_from, code)) { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 385 | // MAIL FROM failed -> authentication needed |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 386 | // have we got username? |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 387 | if (!(opts & OPT_U)) { |
| 388 | // no! fetch it from "from" option |
| 389 | //opts |= OPT_U; |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 390 | opt_user = xstrdup(opt_from); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 391 | *strchrnul(opt_user, '@') = '\0'; |
| 392 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 393 | // now we've got username |
| 394 | // so try to authenticate |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 395 | if (334 == smtp_check("AUTH LOGIN", -1)) { |
| 396 | uuencode(NULL, opt_user); |
| 397 | smtp_check("", 334); |
| 398 | uuencode(NULL, opt_pass); |
| 399 | smtp_check("", 235); |
| 400 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 401 | // authenticated OK? -> retry to set sender |
| 402 | // but this time die on failure! |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 403 | code = 250; |
| 404 | } |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 405 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 406 | // set recipients |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 407 | for (llist_t *to = opt_recipients; to; to = to->link) { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 408 | smtp_checkp("RCPT TO:<%s>", sane(to->data), 250); |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 409 | } |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 410 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 411 | // enter "put message" mode |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 412 | smtp_check("DATA", 354); |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 413 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 414 | // put address headers |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 415 | printf("From: %s\r\n", opt_from); |
| 416 | for (llist_t *to = opt_recipients; to; to = to->link) { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 417 | printf("To: %s\r\n", to->data); |
| 418 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 419 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 420 | // put encoded subject |
| 421 | if (opts & OPTS_c) |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 422 | sane((char *)opt_charset); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 423 | if (opts & OPTS_s) { |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 424 | printf("Subject: =?%s?B?", opt_charset); |
| 425 | uuencode(NULL, opt_subject); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 426 | printf("?=\r\n"); |
| 427 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 428 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 429 | // put notification |
| 430 | if (opts & OPTS_n) |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 431 | printf("Disposition-Notification-To: %s\r\n", opt_from); |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 432 | |
| 433 | // make a random string -- it will delimit message parts |
| 434 | srand(monotonic_us()); |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 435 | boundary = xasprintf("%d-%d-%d", rand(), rand(), rand()); |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 436 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 437 | // put common headers and body start |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 438 | printf( |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 439 | "Message-ID: <%s>\r\n" |
| 440 | "Mime-Version: 1.0\r\n" |
| 441 | "%smultipart/mixed; boundary=\"%s\"\r\n" |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 442 | , boundary |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 443 | , "Content-Type: " |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 444 | , boundary |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 445 | ); |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 446 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 447 | // put body + attachment(s) |
| 448 | // N.B. all these weird things just to be tiny |
| 449 | // by reusing string patterns! |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 450 | fmt = |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 451 | "\r\n--%s\r\n" |
| 452 | "%stext/plain; charset=%s\r\n" |
| 453 | "%s%s\r\n" |
| 454 | "%s" |
| 455 | ; |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 456 | p = opt_charset; |
| 457 | q = (char *)""; |
| 458 | while (*argv) { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 459 | printf( |
| 460 | fmt |
| 461 | , boundary |
| 462 | , "Content-Type: " |
| 463 | , p |
| 464 | , "Content-Disposition: inline" |
| 465 | , q |
| 466 | , "Content-Transfer-Encoding: base64\r\n" |
| 467 | ); |
| 468 | p = ""; |
| 469 | fmt = |
| 470 | "\r\n--%s\r\n" |
| 471 | "%sapplication/octet-stream%s\r\n" |
| 472 | "%s; filename=\"%s\"\r\n" |
| 473 | "%s" |
| 474 | ; |
| 475 | uuencode(*argv, NULL); |
| 476 | if (*(++argv)) |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 477 | q = bb_get_last_path_component_strip(*argv); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 478 | } |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 479 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 480 | // put message terminator |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 481 | printf("\r\n--%s--\r\n" "\r\n", boundary); |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 482 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 483 | // leave "put message" mode |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 484 | smtp_check(".", 250); |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 485 | // ... and say goodbye |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 486 | smtp_check("QUIT", 221); |
| 487 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 488 | #if ENABLE_FETCHMAIL |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 489 | } else { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 490 | /*************************************************** |
| 491 | * FETCHMAIL |
| 492 | ***************************************************/ |
| 493 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 494 | char *buf; |
| 495 | unsigned nmsg; |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 496 | char *hostname; |
| 497 | pid_t pid; |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 498 | |
| 499 | // cache fetch command: |
| 500 | // TOP will return only the headers |
| 501 | // RETR will dump the whole message |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 502 | const char *retr = (opts & OPTF_t) ? "TOP %u 0" : "RETR %u"; |
| 503 | |
| 504 | // goto maildir |
| 505 | xchdir(*argv++); |
| 506 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 507 | // cache postprocess program |
| 508 | *fargs = *argv; |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 509 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 510 | // authenticate |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 511 | if (!(opts & OPT_U)) { |
| 512 | //opts |= OPT_U; |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 513 | // N.B. IMHO getenv("USER") can be way easily spoofed! |
| 514 | opt_user = bb_getpwuid(NULL, -1, getuid()); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 515 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 516 | |
| 517 | // get server greeting |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 518 | pop3_checkr(NULL, NULL, &buf); |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 519 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 520 | // server supports APOP? |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 521 | if ('<' == *buf) { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 522 | md5_ctx_t md5; |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 523 | // yes! compose <stamp><password> |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 524 | char *s = strchr(buf, '>'); |
| 525 | if (s) |
| 526 | strcpy(s+1, opt_pass); |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 527 | s = buf; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 528 | // get md5 sum of <stamp><password> |
| 529 | md5_begin(&md5); |
| 530 | md5_hash(s, strlen(s), &md5); |
| 531 | md5_end(s, &md5); |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 532 | // NOTE: md5 struct contains enough space |
| 533 | // so we reuse md5 space instead of xzalloc(16*2+1) |
| 534 | #define md5_hex ((uint8_t *)&md5) |
| 535 | // uint8_t *md5_hex = (uint8_t *)&md5; |
Denis Vlasenko | 023dc67 | 2008-05-09 18:07:15 +0000 | [diff] [blame] | 536 | *bin2hex((char *)md5_hex, s, 16) = '\0'; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 537 | // APOP |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 538 | s = xasprintf("%s %s", opt_user, md5_hex); |
| 539 | #undef md5_hex |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 540 | pop3_check("APOP %s", s); |
| 541 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 542 | free(s); |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 543 | free(buf-4); // buf is "+OK " away from malloc'ed string |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 544 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 545 | // server ignores APOP -> use simple text authentication |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 546 | } else { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 547 | // USER |
| 548 | pop3_check("USER %s", opt_user); |
| 549 | // PASS |
| 550 | pop3_check("PASS %s", opt_pass); |
| 551 | } |
| 552 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 553 | // get mailbox statistics |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 554 | pop3_checkr("STAT", NULL, &buf); |
| 555 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 556 | // prepare message filename suffix |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 557 | hostname = safe_gethostname(); |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 558 | pid = getpid(); |
| 559 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 560 | // get messages counter |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 561 | // NOTE: we don't use xatou(buf) since buf is "nmsg nbytes" |
| 562 | // we only need nmsg and atoi is just exactly what we need |
| 563 | // if atoi fails to convert buf into number it returns 0 |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 564 | // in this case the following loop simply will not be executed |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 565 | nmsg = atoi(buf); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 566 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 567 | free(buf-4); // buf is "+OK " away from malloc'ed string |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 568 | |
Bernhard Reutner-Fischer | a985d30 | 2008-02-11 11:44:38 +0000 | [diff] [blame] | 569 | // loop through messages |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 570 | for (; nmsg; nmsg--) { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 571 | |
| 572 | // generate unique filename |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 573 | char *filename = xasprintf("tmp/%llu.%u.%s", monotonic_us(), pid, hostname); |
| 574 | char *target; |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 575 | int rc; |
| 576 | |
| 577 | // retrieve message in ./tmp/ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 578 | pop3_check(retr, (const char *)(ptrdiff_t)nmsg); |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 579 | pop3_message(filename); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 580 | // delete message from server |
| 581 | if (opts & OPTF_z) |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 582 | pop3_check("DELE %u", (const char*)(ptrdiff_t)nmsg); |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 583 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 584 | // run postprocessing program |
| 585 | if (*fargs) { |
| 586 | fargs[1] = filename; |
| 587 | rc = wait4pid(spawn((char **)fargs)); |
| 588 | if (99 == rc) |
| 589 | break; |
| 590 | if (1 == rc) |
| 591 | goto skip; |
| 592 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 593 | |
| 594 | // atomically move message to ./new/ |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 595 | target = xstrdup(filename); |
| 596 | strncpy(target, "new", 3); |
| 597 | // ... or just stop receiving on error |
| 598 | if (rename_or_warn(filename, target)) |
| 599 | break; |
| 600 | free(target); |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 601 | skip: |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 602 | free(filename); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 603 | } |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 604 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 605 | // Bye |
| 606 | pop3_check("QUIT", NULL); |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 607 | #endif // ENABLE_FETCHMAIL |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 608 | } |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 609 | |
| 610 | return 0; |
| 611 | } |