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