blob: 2eb01dc94e6b3a41feeaccb77ac53aaf4379f2c6 [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 Vlasenkoddd42cb2008-01-29 00:59:15 +000064}
65
Denis Vlasenko17db1a92008-02-26 21:13:17 +000066struct globals {
67 pid_t helper_pid;
68 unsigned timeout;
69 // arguments for SSL connection helper
70 const char *xargs[9];
Denis Vlasenko17db1a92008-02-26 21:13:17 +000071 // arguments for postprocess helper
72 const char *fargs[3];
Denis Vlasenko17db1a92008-02-26 21:13:17 +000073};
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 Vlasenko574f2f42008-02-27 18:41:59 +000080 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenko8195d202008-02-27 09:39:04 +000081 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 Vlasenko17db1a92008-02-26 21:13:17 +000089 fargs[0] = "utf-8"; \
90} while (0)
91
92#define opt_connect (xargs[4])
93#define opt_after_connect (xargs[5])
Denis Vlasenko17db1a92008-02-26 21:13:17 +000094#define opt_charset (fargs[0])
95#define opt_subject (fargs[1])
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +000096
97static 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 Vlasenkoddd42cb2008-01-29 00:59:15 +0000104// generic signal handler
105static void signal_handler(int signo)
106{
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000107#define err signo
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000108
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000109 if (SIGALRM == signo) {
110 kill_helper();
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000111 bb_error_msg_and_die("timed out");
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000112 }
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000113
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 Vlasenko6d52c1e2008-02-08 18:24:54 +0000120static 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 Vlasenko8195d202008-02-27 09:39:04 +0000138 // child: try to execute connection helper
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000139 BB_EXECVP(*argv, (char **)argv);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000140 _exit(127);
141 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000142 // parent: check whether child is alive
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000143 bb_signals(0
144 + (1 << SIGCHLD)
145 + (1 << SIGALRM)
146 , signal_handler);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000147 signal_handler(SIGCHLD);
148 // child seems OK -> parent goes on
149}
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000150
Denis Vlasenko8195d202008-02-27 09:39:04 +0000151static const char *command(const char *fmt, const char *param)
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000152{
Denis Vlasenko8195d202008-02-27 09:39:04 +0000153 const char *msg = fmt;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000154 alarm(timeout);
155 if (msg) {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000156 msg = xasprintf(fmt, param);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000157 printf("%s\r\n", msg);
158 }
159 fflush(stdout);
160 return msg;
161}
162
163static int smtp_checkp(const char *fmt, const char *param, int code)
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000164{
165 char *answer;
Denis Vlasenko8195d202008-02-27 09:39:04 +0000166 const char *msg = command(fmt, param);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000167 // 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 Vlasenko17db1a92008-02-26 21:13:17 +0000173 while ((answer = xmalloc_getline(stdin)) != NULL)
174 if (strlen(answer) <= 3 || '-' != answer[3])
175 break;
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000176 if (answer) {
177 int n = atoi(answer);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000178 alarm(0);
179 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000180 free(answer);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000181 }
182 if (-1 == code || n == code) {
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000183 return n;
184 }
185 }
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000186 kill_helper();
187 bb_error_msg_and_die("%s failed", msg);
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000188}
189
Denis Vlasenko8195d202008-02-27 09:39:04 +0000190static int inline smtp_check(const char *fmt, int code)
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000191{
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000192 return smtp_checkp(fmt, NULL, code);
193}
194
Denis Vlasenkoa2980c62008-02-02 17:54:35 +0000195// strip argument of bad chars
196static 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 Vlasenkoddd42cb2008-01-29 00:59:15 +0000209
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000210#if ENABLE_FETCHMAIL
211static void pop3_checkr(const char *fmt, const char *param, char **ret)
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000212{
Denis Vlasenko8195d202008-02-27 09:39:04 +0000213 const char *msg = command(fmt, param);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000214 char *answer = xmalloc_getline(stdin);
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 Vlasenko8195d202008-02-27 09:39:04 +0000227static void inline pop3_check(const char *fmt, const char *param)
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000228{
229 pop3_checkr(fmt, param, NULL);
230}
231
232static void pop3_message(const char *filename)
233{
234 int fd;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000235 char *answer;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000236 // 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 Vlasenko8195d202008-02-27 09:39:04 +0000239 while ((answer = xmalloc_fgets_str(stdin, "\r\n")) != NULL) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000240 char *s = answer;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000241 if ('.' == *answer) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000242 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 Vlasenko17db1a92008-02-26 21:13:17 +0000252#endif
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000253
254int sendgetmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +0000255int sendgetmail_main(int argc ATTRIBUTE_UNUSED, char **argv)
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000256{
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000257 llist_t *opt_recipients = NULL;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000258
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000259 const char *opt_user;
260 const char *opt_pass;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000261
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000262 enum {
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000263 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 Vlasenko6d52c1e2008-02-08 18:24:54 +0000267
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000268 OPTS_n = 1 << 4, // sendmail: request notification
269 OPTF_t = 1 << 4, // fetchmail: use "TOP" not "RETR"
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000270
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000271 OPTS_s = 1 << 5, // sendmail: subject
272 OPTF_z = 1 << 5, // fetchmail: delete from server
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000273
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000274 OPTS_c = 1 << 6, // sendmail: assumed charset
275 OPTS_t = 1 << 7, // sendmail: recipient(s)
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000276 };
277
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000278 const char *options;
279 unsigned opts;
280
Denis Vlasenko8195d202008-02-27 09:39:04 +0000281 // init global variables
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000282 INIT_G();
283
Denis Vlasenko8195d202008-02-27 09:39:04 +0000284 // parse options, different option sets for sendmail and fetchmail
285 // N.B. opt_after_connect hereafter is NULL if we are called as fetchmail
286 // and is NOT NULL if we are called as sendmail
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000287 if (!ENABLE_FETCHMAIL || 's' == applet_name[0]) {
288 // SENDMAIL
289 // save initial stdin (body or attachements can be piped!)
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000290 xdup2(STDIN_FILENO, INITIAL_STDIN_FILENO);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000291 opt_complementary = "-2:w+:t:t::"; // count(-t) > 0
292 options = "w:U:P:X" "ns:c:t:";
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000293 } else {
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000294 // FETCHMAIL
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000295 opt_after_connect = NULL;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000296 opt_complementary = "-2:w+:P";
297 options = "w:U:P:X" "tz";
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000298 }
299 opts = getopt32(argv, options,
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000300 &timeout, &opt_user, &opt_pass,
301 &opt_subject, &opt_charset, &opt_recipients
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000302 );
303 //argc -= optind;
304 argv += optind;
305
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000306 // first argument is remote server[:port]
307 opt_connect = *argv++;
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000308
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000309 // connect to server
Denis Vlasenko8195d202008-02-27 09:39:04 +0000310 // SSL ordered? ->
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000311 if (opts & OPT_X) {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000312 // ... use openssl helper
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000313 launch_helper(xargs);
Denis Vlasenko8195d202008-02-27 09:39:04 +0000314 // no SSL ordered? ->
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000315 } else {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000316 // ... make plain connect
317 int fd = create_and_connect_stream_or_die(opt_connect, 25);
318 // make ourselves a simple IO filter
319 // from now we know nothing about network :)
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000320 xmove_fd(fd, STDIN_FILENO);
321 xdup2(STDIN_FILENO, STDOUT_FILENO);
322 }
323
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000324#if ENABLE_FETCHMAIL
Denis Vlasenko8195d202008-02-27 09:39:04 +0000325 // we are sendmail?
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000326 if (opt_after_connect)
327#endif
328 {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000329/***************************************************
330 * SENDMAIL
331 ***************************************************/
332
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000333 char *opt_from;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000334 int code;
335 char *boundary;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000336 const char *fmt;
337 const char *p;
338 char *q;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000339
Denis Vlasenko8195d202008-02-27 09:39:04 +0000340 // we didn't use SSL helper? ->
341 if (!(opts & OPT_X)) {
342 // ... wait for initial server OK
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000343 smtp_check(NULL, 220);
Denis Vlasenko8195d202008-02-27 09:39:04 +0000344 }
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000345
Denis Vlasenko8195d202008-02-27 09:39:04 +0000346 // get the sender
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000347 opt_from = sane(*argv++);
348
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000349 // introduce to server
Denis Vlasenko8195d202008-02-27 09:39:04 +0000350 // we should start with modern EHLO
351 if (250 != smtp_checkp("EHLO %s", opt_from, -1)) {
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000352 smtp_checkp("HELO %s", opt_from, 250);
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000353 }
354
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000355 // set sender
Denis Vlasenko8195d202008-02-27 09:39:04 +0000356 // NOTE: if password has not been specified
357 // then no authentication is possible
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000358 code = (opts & OPT_P) ? -1 : 250;
359 // first try softly without authentication
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000360 while (250 != smtp_checkp("MAIL FROM:<%s>", opt_from, code)) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000361 // MAIL FROM failed -> authentication needed
Denis Vlasenko8195d202008-02-27 09:39:04 +0000362 // have we got username?
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000363 if (!(opts & OPT_U)) {
364 // no! fetch it from "from" option
365 //opts |= OPT_U;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000366 opt_user = xstrdup(opt_from);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000367 *strchrnul(opt_user, '@') = '\0';
368 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000369 // now we've got username
370 // so try to authenticate
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000371 if (334 == smtp_check("AUTH LOGIN", -1)) {
372 uuencode(NULL, opt_user);
373 smtp_check("", 334);
374 uuencode(NULL, opt_pass);
375 smtp_check("", 235);
376 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000377 // authenticated OK? -> retry to set sender
378 // but this time die on failure!
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000379 code = 250;
380 }
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000381
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000382 // set recipients
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000383 for (llist_t *to = opt_recipients; to; to = to->link) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000384 smtp_checkp("RCPT TO:<%s>", sane(to->data), 250);
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000385 }
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000386
Denis Vlasenko8195d202008-02-27 09:39:04 +0000387 // enter "put message" mode
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000388 smtp_check("DATA", 354);
Denis Vlasenko8195d202008-02-27 09:39:04 +0000389
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000390 // put address headers
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000391 printf("From: %s\r\n", opt_from);
392 for (llist_t *to = opt_recipients; to; to = to->link) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000393 printf("To: %s\r\n", to->data);
394 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000395
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000396 // put encoded subject
397 if (opts & OPTS_c)
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000398 sane((char *)opt_charset);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000399 if (opts & OPTS_s) {
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000400 printf("Subject: =?%s?B?", opt_charset);
401 uuencode(NULL, opt_subject);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000402 printf("?=\r\n");
403 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000404
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000405 // put notification
406 if (opts & OPTS_n)
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000407 printf("Disposition-Notification-To: %s\r\n", opt_from);
Denis Vlasenko8195d202008-02-27 09:39:04 +0000408
409 // make a random string -- it will delimit message parts
410 srand(monotonic_us());
411 boundary = xasprintf("%d-%d-%d", rand(), rand(), rand());
412
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000413 // put common headers and body start
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000414 printf(
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000415 "Message-ID: <%s>\r\n"
416 "Mime-Version: 1.0\r\n"
417 "%smultipart/mixed; boundary=\"%s\"\r\n"
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000418 , boundary
Denis Vlasenkoa2980c62008-02-02 17:54:35 +0000419 , "Content-Type: "
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000420 , boundary
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000421 );
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000422
Denis Vlasenko8195d202008-02-27 09:39:04 +0000423 // put body + attachment(s)
424 // N.B. all these weird things just to be tiny
425 // by reusing string patterns!
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000426 fmt =
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000427 "\r\n--%s\r\n"
428 "%stext/plain; charset=%s\r\n"
429 "%s%s\r\n"
430 "%s"
431 ;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000432 p = opt_charset;
433 q = (char *)"";
434 while (*argv) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000435 printf(
436 fmt
437 , boundary
438 , "Content-Type: "
439 , p
440 , "Content-Disposition: inline"
441 , q
442 , "Content-Transfer-Encoding: base64\r\n"
443 );
444 p = "";
445 fmt =
446 "\r\n--%s\r\n"
447 "%sapplication/octet-stream%s\r\n"
448 "%s; filename=\"%s\"\r\n"
449 "%s"
450 ;
451 uuencode(*argv, NULL);
452 if (*(++argv))
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000453 q = bb_get_last_path_component_strip(*argv);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000454 }
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000455
Denis Vlasenko8195d202008-02-27 09:39:04 +0000456 // put message terminator
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000457 printf("\r\n--%s--\r\n" "\r\n", boundary);
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000458
Denis Vlasenko8195d202008-02-27 09:39:04 +0000459 // leave "put message" mode
460 smtp_check(".", 250);
461 // ... and say goodbye
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000462 smtp_check("QUIT", 221);
463
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000464#if ENABLE_FETCHMAIL
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000465 } else {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000466/***************************************************
467 * FETCHMAIL
468 ***************************************************/
469
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000470 char *buf;
471 unsigned nmsg;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000472 char *hostname;
473 pid_t pid;
Denis Vlasenko8195d202008-02-27 09:39:04 +0000474
475 // cache fetch command:
476 // TOP will return only the headers
477 // RETR will dump the whole message
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000478 const char *retr = (opts & OPTF_t) ? "TOP %u 0" : "RETR %u";
479
480 // goto maildir
481 xchdir(*argv++);
482
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000483 // cache postprocess program
484 *fargs = *argv;
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000485
486 // authenticate
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000487 if (!(opts & OPT_U)) {
488 //opts |= OPT_U;
Denis Vlasenko8195d202008-02-27 09:39:04 +0000489 // N.B. IMHO getenv("USER") can be way easily spoofed!
490 opt_user = bb_getpwuid(NULL, -1, getuid());
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000491 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000492
493 // get server greeting
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000494 pop3_checkr(NULL, NULL, &buf);
Denis Vlasenko8195d202008-02-27 09:39:04 +0000495
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000496 // server supports APOP?
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000497 if ('<' == *buf) {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000498 md5_ctx_t md5;
Denis Vlasenko8195d202008-02-27 09:39:04 +0000499 // yes! compose <stamp><password>
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000500 char *s = strchr(buf, '>');
501 if (s)
502 strcpy(s+1, opt_pass);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000503 s = buf;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000504 // get md5 sum of <stamp><password>
505 md5_begin(&md5);
506 md5_hash(s, strlen(s), &md5);
507 md5_end(s, &md5);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000508 // NOTE: md5 struct contains enough space
509 // so we reuse md5 space instead of xzalloc(16*2+1)
510#define md5_hex ((uint8_t *)&md5)
511// uint8_t *md5_hex = (uint8_t *)&md5;
512 *bin2hex(md5_hex, s, 16) = '\0';
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000513 // APOP
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000514 s = xasprintf("%s %s", opt_user, md5_hex);
515#undef md5_hex
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000516 pop3_check("APOP %s", s);
517 if (ENABLE_FEATURE_CLEAN_UP) {
518 free(s);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000519 free(buf-4); // buf is "+OK " away from malloc'ed string
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000520 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000521 // server ignores APOP -> use simple text authentication
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000522 } else {
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000523 // USER
524 pop3_check("USER %s", opt_user);
525 // PASS
526 pop3_check("PASS %s", opt_pass);
527 }
528
Denis Vlasenko8195d202008-02-27 09:39:04 +0000529 // get mailbox statistics
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000530 pop3_checkr("STAT", NULL, &buf);
531
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000532 // prepare message filename suffix
Denis Vlasenko8195d202008-02-27 09:39:04 +0000533 hostname = safe_gethostname();
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000534 pid = getpid();
535
Denis Vlasenko8195d202008-02-27 09:39:04 +0000536 // get messages counter
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000537 // NOTE: we don't use xatou(buf) since buf is "nmsg nbytes"
538 // we only need nmsg and atoi is just exactly what we need
539 // if atoi fails to convert buf into number it returns 0
540 // in this case the following loop simply will not be executed
541 nmsg = atoi(buf);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000542 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000543 free(buf-4); // buf is "+OK " away from malloc'ed string
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000544
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +0000545 // loop through messages
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000546 for (; nmsg; nmsg--) {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000547
548 // generate unique filename
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000549 char *filename = xasprintf("tmp/%llu.%u.%s", monotonic_us(), pid, hostname);
550 char *target;
Denis Vlasenko8195d202008-02-27 09:39:04 +0000551 int rc;
552
553 // retrieve message in ./tmp/
Denis Vlasenko85c24712008-03-17 09:04:04 +0000554 pop3_check(retr, (const char *)(ptrdiff_t)nmsg);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000555 pop3_message(filename);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000556 // delete message from server
557 if (opts & OPTF_z)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000558 pop3_check("DELE %u", (const char*)(ptrdiff_t)nmsg);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000559
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000560 // run postprocessing program
561 if (*fargs) {
562 fargs[1] = filename;
563 rc = wait4pid(spawn((char **)fargs));
564 if (99 == rc)
565 break;
566 if (1 == rc)
567 goto skip;
568 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000569
570 // atomically move message to ./new/
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000571 target = xstrdup(filename);
572 strncpy(target, "new", 3);
573 // ... or just stop receiving on error
574 if (rename_or_warn(filename, target))
575 break;
576 free(target);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000577 skip:
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000578 free(filename);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000579 }
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000580
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000581 // Bye
582 pop3_check("QUIT", NULL);
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000583#endif // ENABLE_FETCHMAIL
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000584 }
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000585
586 return 0;
587}