blob: 653f2e60a755156922f2a4eb5b9ac65a4ff3590e [file] [log] [blame]
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +00003 * bare bones sendmail/fetchmail
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +00004 *
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 Vlasenko6d52c1e2008-02-08 18:24:54 +000011#define INITIAL_STDIN_FILENO 3
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +000012
Denis Vlasenkoa2980c62008-02-02 17:54:35 +000013static void uuencode(char *fname, const char *text)
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +000014{
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +000015 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 Vlasenkoa2980c62008-02-02 17:54:35 +000020#define src_buf text
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +000021 int fd;
Denis Vlasenkoa2980c62008-02-02 17:54:35 +000022#define len fd
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +000023 char dst_buf[DST_BUF_SIZE + 1];
24
Denis Vlasenkoa2980c62008-02-02 17:54:35 +000025 if (fname) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +000026 fd = INITIAL_STDIN_FILENO;
27 if (NOT_LONE_DASH(fname))
28 fd = xopen(fname, O_RDONLY);
Denis Vlasenkoa2980c62008-02-02 17:54:35 +000029 src_buf = bb_common_bufsiz1;
Denis Vlasenko17db1a92008-02-26 21:13:17 +000030 // 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 Vlasenkoa2980c62008-02-02 17:54:35 +000034 len = strlen(text);
Denis Vlasenko17db1a92008-02-26 21:13:17 +000035 } else
36 return;
Denis Vlasenkoa2980c62008-02-02 17:54:35 +000037
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 Vlasenko6d52c1e2008-02-08 18:24:54 +000052 // encode the buffer we just read in
Denis Vlasenkoa2980c62008-02-02 17:54:35 +000053 bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
54 if (fname) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +000055 xwrite(STDOUT_FILENO, "\r\n", 2);
Denis Vlasenkoa2980c62008-02-02 17:54:35 +000056 } else {
57 src_buf += size;
58 len -= size;
59 }
60 xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
61 }
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +000062 if (fname)
Denis Vlasenkoa2980c62008-02-02 17:54:35 +000063 close(fd);
Denis Vlasenko3dee8e22008-06-27 21:24:08 +000064#undef src_buf
65#undef len
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +000066}
67
Denis Vlasenko17db1a92008-02-26 21:13:17 +000068struct globals {
69 pid_t helper_pid;
70 unsigned timeout;
71 // arguments for SSL connection helper
72 const char *xargs[9];
Denis Vlasenko17db1a92008-02-26 21:13:17 +000073 // arguments for postprocess helper
74 const char *fargs[3];
Denis Vlasenko17db1a92008-02-26 21:13:17 +000075};
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 Vlasenko574f2f42008-02-27 18:41:59 +000082 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenko8195d202008-02-27 09:39:04 +000083 xargs[0] = "openssl"; \
84 xargs[1] = "s_client"; \
85 xargs[2] = "-quiet"; \
86 xargs[3] = "-connect"; \
Denis Vlasenko3dee8e22008-06-27 21:24:08 +000087 /*xargs[4] = "localhost";*/ \
Denis Vlasenko8195d202008-02-27 09:39:04 +000088 xargs[5] = "-tls1"; \
89 xargs[6] = "-starttls"; \
90 xargs[7] = "smtp"; \
Denis Vlasenko17db1a92008-02-26 21:13:17 +000091 fargs[0] = "utf-8"; \
92} while (0)
93
94#define opt_connect (xargs[4])
95#define opt_after_connect (xargs[5])
Denis Vlasenko17db1a92008-02-26 21:13:17 +000096#define opt_charset (fargs[0])
97#define opt_subject (fargs[1])
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +000098
99static 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 Vlasenkoddd42cb2008-01-29 00:59:15 +0000106// generic signal handler
107static void signal_handler(int signo)
108{
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000109#define err signo
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000110
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000111 if (SIGALRM == signo) {
112 kill_helper();
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000113 bb_error_msg_and_die("timed out");
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000114 }
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000115
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 Vlasenko3dee8e22008-06-27 21:24:08 +0000120#undef err
121}
122
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000123static void launch_helper(const char **argv)
124{
125 // setup vanilla unidirectional pipes interchange
126 int idx;
127 int pipes[4];
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000128
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000129 xpipe(pipes);
130 xpipe(pipes+2);
Denis Vlasenkofa0b56d2008-07-01 16:09:07 +0000131 helper_pid = vfork();
132 if (helper_pid < 0)
133 bb_perror_msg_and_die("vfork");
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000134 idx = (!helper_pid) * 2;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000135 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 Vlasenko8195d202008-02-27 09:39:04 +0000142 // child: try to execute connection helper
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000143 BB_EXECVP(*argv, (char **)argv);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000144 _exit(127);
145 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000146 // parent: check whether child is alive
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000147 bb_signals(0
148 + (1 << SIGCHLD)
149 + (1 << SIGALRM)
150 , signal_handler);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000151 signal_handler(SIGCHLD);
152 // child seems OK -> parent goes on
153}
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000154
Denis Vlasenko8195d202008-02-27 09:39:04 +0000155static const char *command(const char *fmt, const char *param)
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000156{
Denis Vlasenko8195d202008-02-27 09:39:04 +0000157 const char *msg = fmt;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000158 alarm(timeout);
159 if (msg) {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000160 msg = xasprintf(fmt, param);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000161 printf("%s\r\n", msg);
162 }
163 fflush(stdout);
164 return msg;
165}
166
167static int smtp_checkp(const char *fmt, const char *param, int code)
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000168{
169 char *answer;
Denis Vlasenko8195d202008-02-27 09:39:04 +0000170 const char *msg = command(fmt, param);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000171 // 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 Vlasenko8ee649a2008-03-26 20:04:27 +0000177 while ((answer = xmalloc_fgetline(stdin)) != NULL)
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000178 if (strlen(answer) <= 3 || '-' != answer[3])
179 break;
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000180 if (answer) {
181 int n = atoi(answer);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000182 alarm(0);
183 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000184 free(answer);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000185 }
186 if (-1 == code || n == code) {
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000187 return n;
188 }
189 }
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000190 kill_helper();
191 bb_error_msg_and_die("%s failed", msg);
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000192}
193
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000194static inline int smtp_check(const char *fmt, int code)
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000195{
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000196 return smtp_checkp(fmt, NULL, code);
197}
198
Denis Vlasenkoa2980c62008-02-02 17:54:35 +0000199// strip argument of bad chars
200static 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 Vlasenkoddd42cb2008-01-29 00:59:15 +0000213
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000214#if ENABLE_FETCHMAIL
215static void pop3_checkr(const char *fmt, const char *param, char **ret)
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000216{
Denis Vlasenko8195d202008-02-27 09:39:04 +0000217 const char *msg = command(fmt, param);
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000218 char *answer = xmalloc_fgetline(stdin);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000219 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 Vlasenko6b06cb82008-05-15 21:30:45 +0000231static inline void pop3_check(const char *fmt, const char *param)
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000232{
233 pop3_checkr(fmt, param, NULL);
234}
235
236static void pop3_message(const char *filename)
237{
238 int fd;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000239 char *answer;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000240 // 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 Vlasenko8195d202008-02-27 09:39:04 +0000243 while ((answer = xmalloc_fgets_str(stdin, "\r\n")) != NULL) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000244 char *s = answer;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000245 if ('.' == *answer) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000246 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 Vlasenko17db1a92008-02-26 21:13:17 +0000256#endif
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000257
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000258static char *parse_url(char *url, char **user, char **pass)
259{
260 // parse [user[:pass]@]host
261 // return host
262 char *s = strchr(url, '@');
263 *user = *pass = NULL;
264 if (s) {
265 *s++ = '\0';
266 *user = url;
267 url = s;
268 s = strchr(*user, ':');
269 if (s) {
270 *s++ = '\0';
271 *pass = s;
272 }
273 }
274 return url;
275}
276
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000277int sendgetmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000278int sendgetmail_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000279{
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000280 llist_t *opt_recipients = NULL;
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000281 llist_t *opt_attachments = NULL;
282 char *opt_from;
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000283 char *opt_user;
284 char *opt_pass;
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000285 enum {
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000286 OPT_w = 1 << 0, // network timeout
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000287 OPT_H = 1 << 1, // [user:password@]server[:port]
288 OPT_S = 1 << 2, // connect using openssl s_client helper
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000289
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000290 OPTS_t = 1 << 3, // sendmail: read addresses from body
291 OPTF_t = 1 << 3, // fetchmail: use "TOP" not "RETR"
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000292
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000293 OPTS_s = 1 << 4, // sendmail: subject
294 OPTF_z = 1 << 4, // fetchmail: delete from server
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000295
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000296 OPTS_c = 1 << 5, // sendmail: assumed charset
297 OPTS_a = 1 << 6, // sendmail: attachment(s)
298 OPTS_i = 1 << 7, // sendmail: ignore lone dots in message body (implied)
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000299
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000300 OPTS_N = 1 << 8, // sendmail: request notification
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000301 OPTS_f = 1 << 9, // sendmail: sender address
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000302 };
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000303 const char *options;
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000304 int opts;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000305
Denis Vlasenko8195d202008-02-27 09:39:04 +0000306 // init global variables
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000307 INIT_G();
308
Denis Vlasenko8195d202008-02-27 09:39:04 +0000309 // parse options, different option sets for sendmail and fetchmail
310 // N.B. opt_after_connect hereafter is NULL if we are called as fetchmail
311 // and is NOT NULL if we are called as sendmail
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000312 if (!ENABLE_FETCHMAIL || 's' == applet_name[0]) {
313 // SENDMAIL
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000314 // save initial stdin since body is piped!
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000315 xdup2(STDIN_FILENO, INITIAL_STDIN_FILENO);
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000316 opt_complementary = "w+:a::";
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000317 options = "w:H:St" "s:c:a:iN:f:";
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000318 // body is pseudo attachment read from stdin
319 llist_add_to_end(&opt_attachments, (char *)"-");
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000320 } else {
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000321 // FETCHMAIL
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000322 opt_after_connect = NULL;
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000323 opt_complementary = "-1:w+";
324 options = "w:H:St" "z";
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000325 }
326 opts = getopt32(argv, options,
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000327 &timeout /* -w */, &opt_connect /* -H */,
328 &opt_subject, &opt_charset, &opt_attachments, NULL, &opt_from
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000329 );
330 //argc -= optind;
331 argv += optind;
332
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000333 // connect to server
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000334 // host[:port] not specified ? -> use $HOSTNAME. no $HOSTNAME ? -> use localhost
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000335 if (!(opts & OPT_H)) {
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000336 opt_connect = getenv("HOSTNAME");
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000337 if (!opt_connect)
338 opt_connect = "127.0.0.1";
339 }
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000340 // fetch username and password, if any
341 // NB: parse_url modifies opt_connect[] ONLY if '@' is there.
342 // Thus "127.0.0.1" won't be modified, an is ok that it is RO.
343 opt_connect = parse_url((char*)opt_connect, &opt_user, &opt_pass);
344// bb_error_msg("H[%s] U[%s] P[%s]", opt_connect, opt_user, opt_pass);
345
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000346 // username must be defined!
347 if (!opt_user) {
348 // N.B. IMHO getenv("USER") can be way easily spoofed!
349 opt_user = bb_getpwuid(NULL, -1, getuid());
350 }
351
Denis Vlasenko8195d202008-02-27 09:39:04 +0000352 // SSL ordered? ->
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000353 if (opts & OPT_S) {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000354 // ... use openssl helper
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000355 launch_helper(xargs);
Denis Vlasenko8195d202008-02-27 09:39:04 +0000356 // no SSL ordered? ->
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000357 } else {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000358 // ... make plain connect
359 int fd = create_and_connect_stream_or_die(opt_connect, 25);
360 // make ourselves a simple IO filter
361 // from now we know nothing about network :)
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000362 xmove_fd(fd, STDIN_FILENO);
363 xdup2(STDIN_FILENO, STDOUT_FILENO);
364 }
365
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000366 // are we sendmail?
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000367 if (!ENABLE_FETCHMAIL || opt_after_connect)
Denis Vlasenko8195d202008-02-27 09:39:04 +0000368/***************************************************
369 * SENDMAIL
370 ***************************************************/
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000371 {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000372 int code;
373 char *boundary;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000374 const char *fmt;
375 const char *p;
376 char *q;
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000377 llist_t *l;
Denis Vlasenko38e54f12008-07-16 21:55:03 +0000378 llist_t *headers = NULL;
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000379
380 // recipients specified as arguments
381 while (*argv) {
382 // loose test on email address validity
383 if (strchr(sane(*argv), '@'))
384 llist_add_to_end(&opt_recipients, *argv);
385 argv++;
386 }
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000387
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000388 // if -t specified or no recipients specified -> enter all-included mode
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000389 // i.e. scan stdin for To: and Subject: lines ...
390 // ... and then use the rest of stdin as message body
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000391 // N.B. subject read from body has priority
392 // over that specified on command line.
393 // recipients are merged
Denis Vlasenko38e54f12008-07-16 21:55:03 +0000394 // N.B. other headers are collected and will be dumped verbatim
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000395 if (opts & OPTS_t || !opt_recipients) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000396 // fetch recipients and (optionally) subject
397 char *s;
398 while ((s = xmalloc_reads(INITIAL_STDIN_FILENO, NULL, NULL)) != NULL) {
Denis Vlasenko0ed090e2008-07-15 20:56:23 +0000399 if (0 == strncasecmp("To: ", s, 4) || 0 == strncasecmp("Cc: ", s, 4)) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000400 llist_add_to_end(&opt_recipients, s+4);
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000401/* } else if (0 == strncmp("From: ", s, 6)) {
402 opt_from = s+6;
403 opts |= OPTS_f;
404*/ } else if (0 == strncmp("Subject: ", s, 9)) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000405 opt_subject = s+9;
406 opts |= OPTS_s;
Denis Vlasenko38e54f12008-07-16 21:55:03 +0000407 } else if (s[0]) {
408 // misc header
409 llist_add_to_end(&headers, s);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000410 } else {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000411 free(s);
Denis Vlasenko38e54f12008-07-16 21:55:03 +0000412 break; // empty line
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000413 }
414 }
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000415 }
416
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000417 // got no sender address? -> use username as a resort
418 if (!(opts & OPTS_f)) {
419 char *domain = safe_getdomainname();
420 opt_from = xasprintf("%s@%s", opt_user, domain);
421 free(domain);
422 }
423
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000424 // introduce to server
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000425
426 // we didn't use SSL helper? ->
427 if (!(opts & OPT_S)) {
428 // ... wait for initial server OK
429 smtp_check(NULL, 220);
430 }
431
Denis Vlasenko8195d202008-02-27 09:39:04 +0000432 // we should start with modern EHLO
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000433 if (250 != smtp_checkp("EHLO %s", sane(opt_from), -1)) {
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000434 smtp_checkp("HELO %s", opt_from, 250);
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000435 }
436
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000437 // set sender
Denis Vlasenko8195d202008-02-27 09:39:04 +0000438 // NOTE: if password has not been specified
439 // then no authentication is possible
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000440 code = (opt_pass) ? -1 : 250;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000441 // first try softly without authentication
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000442 while (250 != smtp_checkp("MAIL FROM:<%s>", opt_from, code)) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000443 // MAIL FROM failed -> authentication needed
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000444 if (334 == smtp_check("AUTH LOGIN", -1)) {
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000445 uuencode(NULL, opt_user); // opt_user != NULL
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000446 smtp_check("", 334);
447 uuencode(NULL, opt_pass);
448 smtp_check("", 235);
449 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000450 // authenticated OK? -> retry to set sender
451 // but this time die on failure!
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000452 code = 250;
453 }
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000454
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000455 // set recipients
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000456 for (l = opt_recipients; l; l = l->link) {
457 smtp_checkp("RCPT TO:<%s>", sane(l->data), 250);
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000458 }
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000459
Denis Vlasenko8195d202008-02-27 09:39:04 +0000460 // enter "put message" mode
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000461 smtp_check("DATA", 354);
Denis Vlasenko8195d202008-02-27 09:39:04 +0000462
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000463 // put address headers
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000464 printf("From: %s\r\n", opt_from);
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000465 for (l = opt_recipients; l; l = l->link) {
466 printf("To: %s\r\n", l->data);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000467 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000468
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000469 // put encoded subject
470 if (opts & OPTS_c)
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000471 sane((char *)opt_charset);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000472 if (opts & OPTS_s) {
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000473 printf("Subject: =?%s?B?", opt_charset);
474 uuencode(NULL, opt_subject);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000475 printf("?=\r\n");
476 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000477
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000478 // put notification
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000479 if (opts & OPTS_N)
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000480 printf("Disposition-Notification-To: %s\r\n", opt_from);
Denis Vlasenko8195d202008-02-27 09:39:04 +0000481
Denis Vlasenko38e54f12008-07-16 21:55:03 +0000482 // put headers we could have preread with -t
483 for (l = headers; l; l = l->link) {
484 printf("%s\r\n", l->data);
485 }
486
Denis Vlasenko8195d202008-02-27 09:39:04 +0000487 // make a random string -- it will delimit message parts
488 srand(monotonic_us());
Denis Vlasenkoc6938402008-03-24 02:18:03 +0000489 boundary = xasprintf("%d-%d-%d", rand(), rand(), rand());
Denis Vlasenko8195d202008-02-27 09:39:04 +0000490
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000491 // put common headers and body start
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000492 printf(
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000493 "Message-ID: <%s>\r\n"
494 "Mime-Version: 1.0\r\n"
495 "%smultipart/mixed; boundary=\"%s\"\r\n"
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000496 , boundary
Denis Vlasenkoa2980c62008-02-02 17:54:35 +0000497 , "Content-Type: "
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000498 , boundary
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000499 );
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000500
Denis Vlasenko8195d202008-02-27 09:39:04 +0000501 // put body + attachment(s)
502 // N.B. all these weird things just to be tiny
503 // by reusing string patterns!
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000504 fmt =
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000505 "\r\n--%s\r\n"
506 "%stext/plain; charset=%s\r\n"
507 "%s%s\r\n"
508 "%s"
509 ;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000510 p = opt_charset;
511 q = (char *)"";
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000512 l = opt_attachments;
513 while (l) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000514 printf(
515 fmt
516 , boundary
517 , "Content-Type: "
518 , p
519 , "Content-Disposition: inline"
520 , q
521 , "Content-Transfer-Encoding: base64\r\n"
522 );
523 p = "";
524 fmt =
525 "\r\n--%s\r\n"
526 "%sapplication/octet-stream%s\r\n"
527 "%s; filename=\"%s\"\r\n"
528 "%s"
529 ;
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000530 uuencode(l->data, NULL);
531 l = l->link;
532 if (l)
533 q = bb_get_last_path_component_strip(l->data);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000534 }
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000535
Denis Vlasenko8195d202008-02-27 09:39:04 +0000536 // put message terminator
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000537 printf("\r\n--%s--\r\n" "\r\n", boundary);
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000538
Denis Vlasenko8195d202008-02-27 09:39:04 +0000539 // leave "put message" mode
Denis Vlasenkoc6938402008-03-24 02:18:03 +0000540 smtp_check(".", 250);
Denis Vlasenko8195d202008-02-27 09:39:04 +0000541 // ... and say goodbye
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000542 smtp_check("QUIT", 221);
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000543 }
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000544#if ENABLE_FETCHMAIL
Denis Vlasenko8195d202008-02-27 09:39:04 +0000545/***************************************************
546 * FETCHMAIL
547 ***************************************************/
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000548 else {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000549 char *buf;
550 unsigned nmsg;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000551 char *hostname;
552 pid_t pid;
Denis Vlasenko8195d202008-02-27 09:39:04 +0000553
554 // cache fetch command:
555 // TOP will return only the headers
556 // RETR will dump the whole message
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000557 const char *retr = (opts & OPTF_t) ? "TOP %u 0" : "RETR %u";
558
559 // goto maildir
560 xchdir(*argv++);
561
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000562 // cache postprocess program
563 *fargs = *argv;
Denis Vlasenkoc6938402008-03-24 02:18:03 +0000564
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000565 // authenticate
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000566
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000567 // password is mandatory
568 if (!opt_pass) {
569 bb_error_msg_and_die("no password");
570 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000571
572 // get server greeting
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000573 pop3_checkr(NULL, NULL, &buf);
Denis Vlasenko8195d202008-02-27 09:39:04 +0000574
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000575 // server supports APOP?
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000576 if ('<' == *buf) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000577 md5_ctx_t md5;
Denis Vlasenko8195d202008-02-27 09:39:04 +0000578 // yes! compose <stamp><password>
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000579 char *s = strchr(buf, '>');
580 if (s)
581 strcpy(s+1, opt_pass);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000582 s = buf;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000583 // get md5 sum of <stamp><password>
584 md5_begin(&md5);
585 md5_hash(s, strlen(s), &md5);
586 md5_end(s, &md5);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000587 // NOTE: md5 struct contains enough space
588 // so we reuse md5 space instead of xzalloc(16*2+1)
589#define md5_hex ((uint8_t *)&md5)
590// uint8_t *md5_hex = (uint8_t *)&md5;
Denis Vlasenko023dc672008-05-09 18:07:15 +0000591 *bin2hex((char *)md5_hex, s, 16) = '\0';
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000592 // APOP
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000593 s = xasprintf("%s %s", opt_user, md5_hex);
594#undef md5_hex
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000595 pop3_check("APOP %s", s);
596 if (ENABLE_FEATURE_CLEAN_UP) {
597 free(s);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000598 free(buf-4); // buf is "+OK " away from malloc'ed string
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000599 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000600 // server ignores APOP -> use simple text authentication
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000601 } else {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000602 // USER
603 pop3_check("USER %s", opt_user);
604 // PASS
605 pop3_check("PASS %s", opt_pass);
606 }
607
Denis Vlasenko8195d202008-02-27 09:39:04 +0000608 // get mailbox statistics
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000609 pop3_checkr("STAT", NULL, &buf);
610
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000611 // prepare message filename suffix
Denis Vlasenko8195d202008-02-27 09:39:04 +0000612 hostname = safe_gethostname();
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000613 pid = getpid();
614
Denis Vlasenko8195d202008-02-27 09:39:04 +0000615 // get messages counter
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000616 // NOTE: we don't use xatou(buf) since buf is "nmsg nbytes"
617 // we only need nmsg and atoi is just exactly what we need
618 // if atoi fails to convert buf into number it returns 0
Denis Vlasenko42cc3042008-03-24 02:05:58 +0000619 // in this case the following loop simply will not be executed
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000620 nmsg = atoi(buf);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000621 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000622 free(buf-4); // buf is "+OK " away from malloc'ed string
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000623
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +0000624 // loop through messages
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000625 for (; nmsg; nmsg--) {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000626
627 // generate unique filename
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000628 char *filename = xasprintf("tmp/%llu.%u.%s",
629 monotonic_us(), (unsigned)pid, hostname);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000630 char *target;
Denis Vlasenko8195d202008-02-27 09:39:04 +0000631 int rc;
632
633 // retrieve message in ./tmp/
Denis Vlasenko85c24712008-03-17 09:04:04 +0000634 pop3_check(retr, (const char *)(ptrdiff_t)nmsg);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000635 pop3_message(filename);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000636 // delete message from server
637 if (opts & OPTF_z)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000638 pop3_check("DELE %u", (const char*)(ptrdiff_t)nmsg);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000639
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000640 // run postprocessing program
641 if (*fargs) {
642 fargs[1] = filename;
643 rc = wait4pid(spawn((char **)fargs));
644 if (99 == rc)
645 break;
646 if (1 == rc)
647 goto skip;
648 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000649
650 // atomically move message to ./new/
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000651 target = xstrdup(filename);
652 strncpy(target, "new", 3);
653 // ... or just stop receiving on error
654 if (rename_or_warn(filename, target))
655 break;
656 free(target);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000657 skip:
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000658 free(filename);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000659 }
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000660
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000661 // Bye
662 pop3_check("QUIT", NULL);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000663 }
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000664#endif // ENABLE_FETCHMAIL
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000665
666 return 0;
667}