2008-01-29 00:59:15 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2008-02-08 18:24:54 +00:00
|
|
|
* bare bones sendmail/fetchmail
|
2008-01-29 00:59:15 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
#define INITIAL_STDIN_FILENO 3
|
2008-01-29 00:59:15 +00:00
|
|
|
|
2008-02-02 17:54:35 +00:00
|
|
|
static void uuencode(char *fname, const char *text)
|
2008-01-29 00:59:15 +00:00
|
|
|
{
|
2008-02-08 18:24:54 +00:00
|
|
|
enum {
|
|
|
|
SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */
|
|
|
|
DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
|
|
|
|
};
|
|
|
|
|
2008-02-02 17:54:35 +00:00
|
|
|
#define src_buf text
|
2008-01-29 00:59:15 +00:00
|
|
|
int fd;
|
2008-02-02 17:54:35 +00:00
|
|
|
#define len fd
|
|
|
|
char dst_buf[DST_BUF_SIZE + 1];
|
2008-01-29 00:59:15 +00:00
|
|
|
|
2008-02-02 17:54:35 +00:00
|
|
|
if (fname) {
|
2008-02-08 18:24:54 +00:00
|
|
|
fd = INITIAL_STDIN_FILENO;
|
|
|
|
if (NOT_LONE_DASH(fname))
|
|
|
|
fd = xopen(fname, O_RDONLY);
|
2008-02-02 17:54:35 +00:00
|
|
|
src_buf = bb_common_bufsiz1;
|
2008-02-26 21:13:17 +00:00
|
|
|
// N.B. strlen(NULL) segfaults!
|
|
|
|
} else if (text) {
|
|
|
|
// though we do not call uuencode(NULL, NULL) explicitly
|
|
|
|
// still we do not want to break things suddenly
|
2008-02-02 17:54:35 +00:00
|
|
|
len = strlen(text);
|
2008-02-26 21:13:17 +00:00
|
|
|
} else
|
|
|
|
return;
|
2008-02-02 17:54:35 +00:00
|
|
|
|
|
|
|
fflush(stdout); // sync stdio and unistd output
|
2008-01-29 00:59:15 +00:00
|
|
|
while (1) {
|
2008-02-02 17:54:35 +00:00
|
|
|
size_t size;
|
|
|
|
if (fname) {
|
|
|
|
size = full_read(fd, (char *)src_buf, SRC_BUF_SIZE);
|
|
|
|
if ((ssize_t)size < 0)
|
|
|
|
bb_perror_msg_and_die(bb_msg_read_error);
|
|
|
|
} else {
|
|
|
|
size = len;
|
|
|
|
if (len > SRC_BUF_SIZE)
|
|
|
|
size = SRC_BUF_SIZE;
|
|
|
|
}
|
2008-01-29 00:59:15 +00:00
|
|
|
if (!size)
|
|
|
|
break;
|
2008-02-08 18:24:54 +00:00
|
|
|
// encode the buffer we just read in
|
2008-02-02 17:54:35 +00:00
|
|
|
bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
|
|
|
|
if (fname) {
|
2008-02-08 18:24:54 +00:00
|
|
|
xwrite(STDOUT_FILENO, "\r\n", 2);
|
2008-02-02 17:54:35 +00:00
|
|
|
} else {
|
|
|
|
src_buf += size;
|
|
|
|
len -= size;
|
|
|
|
}
|
|
|
|
xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
|
2008-01-29 00:59:15 +00:00
|
|
|
}
|
2008-02-08 18:24:54 +00:00
|
|
|
if (fname)
|
2008-02-02 17:54:35 +00:00
|
|
|
close(fd);
|
2008-06-27 21:24:08 +00:00
|
|
|
#undef src_buf
|
|
|
|
#undef len
|
2008-01-29 00:59:15 +00:00
|
|
|
}
|
|
|
|
|
2008-02-26 21:13:17 +00:00
|
|
|
struct globals {
|
|
|
|
pid_t helper_pid;
|
|
|
|
unsigned timeout;
|
|
|
|
// arguments for SSL connection helper
|
|
|
|
const char *xargs[9];
|
|
|
|
// arguments for postprocess helper
|
|
|
|
const char *fargs[3];
|
|
|
|
};
|
|
|
|
#define G (*ptr_to_globals)
|
|
|
|
#define helper_pid (G.helper_pid)
|
|
|
|
#define timeout (G.timeout )
|
|
|
|
#define xargs (G.xargs )
|
|
|
|
#define fargs (G.fargs )
|
|
|
|
#define INIT_G() do { \
|
2008-02-27 18:41:59 +00:00
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
2008-02-27 09:39:04 +00:00
|
|
|
xargs[0] = "openssl"; \
|
|
|
|
xargs[1] = "s_client"; \
|
|
|
|
xargs[2] = "-quiet"; \
|
|
|
|
xargs[3] = "-connect"; \
|
2008-06-27 21:24:08 +00:00
|
|
|
/*xargs[4] = "localhost";*/ \
|
2008-02-27 09:39:04 +00:00
|
|
|
xargs[5] = "-tls1"; \
|
|
|
|
xargs[6] = "-starttls"; \
|
|
|
|
xargs[7] = "smtp"; \
|
2008-02-26 21:13:17 +00:00
|
|
|
fargs[0] = "utf-8"; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define opt_connect (xargs[4])
|
|
|
|
#define opt_after_connect (xargs[5])
|
|
|
|
#define opt_charset (fargs[0])
|
|
|
|
#define opt_subject (fargs[1])
|
2008-02-08 18:24:54 +00:00
|
|
|
|
|
|
|
static void kill_helper(void)
|
|
|
|
{
|
|
|
|
// TODO!!!: is there more elegant way to terminate child on program failure?
|
|
|
|
if (helper_pid > 0)
|
|
|
|
kill(helper_pid, SIGTERM);
|
|
|
|
}
|
|
|
|
|
2008-01-29 00:59:15 +00:00
|
|
|
// generic signal handler
|
|
|
|
static void signal_handler(int signo)
|
|
|
|
{
|
2008-02-26 21:13:17 +00:00
|
|
|
#define err signo
|
2008-01-29 00:59:15 +00:00
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
if (SIGALRM == signo) {
|
|
|
|
kill_helper();
|
2008-01-29 00:59:15 +00:00
|
|
|
bb_error_msg_and_die("timed out");
|
2008-02-08 18:24:54 +00:00
|
|
|
}
|
2008-01-29 00:59:15 +00:00
|
|
|
|
|
|
|
// SIGCHLD. reap zombies
|
|
|
|
if (wait_any_nohang(&err) > 0)
|
|
|
|
if (WIFEXITED(err) && WEXITSTATUS(err))
|
|
|
|
bb_error_msg_and_die("child exited (%d)", WEXITSTATUS(err));
|
2008-06-27 21:24:08 +00:00
|
|
|
#undef err
|
|
|
|
}
|
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
static void launch_helper(const char **argv)
|
2008-01-29 00:59:15 +00:00
|
|
|
{
|
2008-02-08 18:24:54 +00:00
|
|
|
// setup vanilla unidirectional pipes interchange
|
|
|
|
int idx;
|
|
|
|
int pipes[4];
|
2008-06-27 21:24:08 +00:00
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
xpipe(pipes);
|
|
|
|
xpipe(pipes+2);
|
2008-07-01 16:09:07 +00:00
|
|
|
helper_pid = vfork();
|
|
|
|
if (helper_pid < 0)
|
|
|
|
bb_perror_msg_and_die("vfork");
|
2008-06-27 21:24:08 +00:00
|
|
|
idx = (!helper_pid) * 2;
|
2008-02-08 18:24:54 +00:00
|
|
|
xdup2(pipes[idx], STDIN_FILENO);
|
|
|
|
xdup2(pipes[3-idx], STDOUT_FILENO);
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
for (int i = 4; --i >= 0; )
|
|
|
|
if (pipes[i] > STDOUT_FILENO)
|
|
|
|
close(pipes[i]);
|
|
|
|
if (!helper_pid) {
|
2008-02-27 09:39:04 +00:00
|
|
|
// child: try to execute connection helper
|
2008-02-26 21:13:17 +00:00
|
|
|
BB_EXECVP(*argv, (char **)argv);
|
2008-02-08 18:24:54 +00:00
|
|
|
_exit(127);
|
|
|
|
}
|
2008-02-27 09:39:04 +00:00
|
|
|
// parent: check whether child is alive
|
2008-02-26 21:13:17 +00:00
|
|
|
bb_signals(0
|
|
|
|
+ (1 << SIGCHLD)
|
|
|
|
+ (1 << SIGALRM)
|
|
|
|
, signal_handler);
|
2008-02-08 18:24:54 +00:00
|
|
|
signal_handler(SIGCHLD);
|
|
|
|
// child seems OK -> parent goes on
|
|
|
|
}
|
|
|
|
|
2008-02-27 09:39:04 +00:00
|
|
|
static const char *command(const char *fmt, const char *param)
|
2008-02-08 18:24:54 +00:00
|
|
|
{
|
2008-02-27 09:39:04 +00:00
|
|
|
const char *msg = fmt;
|
2008-02-08 18:24:54 +00:00
|
|
|
alarm(timeout);
|
|
|
|
if (msg) {
|
2008-02-27 09:39:04 +00:00
|
|
|
msg = xasprintf(fmt, param);
|
2008-02-08 18:24:54 +00:00
|
|
|
printf("%s\r\n", msg);
|
|
|
|
}
|
2008-01-29 00:59:15 +00:00
|
|
|
fflush(stdout);
|
2008-02-08 18:24:54 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smtp_checkp(const char *fmt, const char *param, int code)
|
|
|
|
{
|
|
|
|
char *answer;
|
2008-02-27 09:39:04 +00:00
|
|
|
const char *msg = command(fmt, param);
|
2008-02-08 18:24:54 +00:00
|
|
|
// read stdin
|
|
|
|
// if the string has a form \d\d\d- -- read next string. E.g. EHLO response
|
|
|
|
// parse first bytes to a number
|
|
|
|
// if code = -1 then just return this number
|
|
|
|
// if code != -1 then checks whether the number equals the code
|
|
|
|
// if not equal -> die saying msg
|
2008-03-26 20:04:27 +00:00
|
|
|
while ((answer = xmalloc_fgetline(stdin)) != NULL)
|
2008-02-26 21:13:17 +00:00
|
|
|
if (strlen(answer) <= 3 || '-' != answer[3])
|
|
|
|
break;
|
2008-01-29 00:59:15 +00:00
|
|
|
if (answer) {
|
|
|
|
int n = atoi(answer);
|
2008-02-08 18:24:54 +00:00
|
|
|
alarm(0);
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
2008-01-29 00:59:15 +00:00
|
|
|
free(answer);
|
2008-02-08 18:24:54 +00:00
|
|
|
}
|
|
|
|
if (-1 == code || n == code) {
|
2008-01-29 00:59:15 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
2008-02-08 18:24:54 +00:00
|
|
|
kill_helper();
|
|
|
|
bb_error_msg_and_die("%s failed", msg);
|
2008-01-29 00:59:15 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 21:30:45 +00:00
|
|
|
static inline int smtp_check(const char *fmt, int code)
|
2008-01-29 00:59:15 +00:00
|
|
|
{
|
2008-02-08 18:24:54 +00:00
|
|
|
return smtp_checkp(fmt, NULL, code);
|
|
|
|
}
|
|
|
|
|
2008-02-26 21:13:17 +00:00
|
|
|
// strip argument of bad chars
|
|
|
|
static char *sane(char *str)
|
|
|
|
{
|
|
|
|
char *s = str;
|
|
|
|
char *p = s;
|
|
|
|
while (*s) {
|
|
|
|
if (isalnum(*s) || '_' == *s || '-' == *s || '.' == *s || '@' == *s) {
|
|
|
|
*p++ = *s;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLE_FETCHMAIL
|
2008-02-08 18:24:54 +00:00
|
|
|
static void pop3_checkr(const char *fmt, const char *param, char **ret)
|
|
|
|
{
|
2008-02-27 09:39:04 +00:00
|
|
|
const char *msg = command(fmt, param);
|
2008-03-26 20:04:27 +00:00
|
|
|
char *answer = xmalloc_fgetline(stdin);
|
2008-02-26 21:13:17 +00:00
|
|
|
if (answer && '+' == *answer) {
|
2008-02-08 18:24:54 +00:00
|
|
|
alarm(0);
|
|
|
|
if (ret)
|
2008-02-26 21:13:17 +00:00
|
|
|
*ret = answer+4; // skip "+OK "
|
|
|
|
else if (ENABLE_FEATURE_CLEAN_UP)
|
2008-02-08 18:24:54 +00:00
|
|
|
free(answer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kill_helper();
|
|
|
|
bb_error_msg_and_die("%s failed", msg);
|
|
|
|
}
|
|
|
|
|
2008-05-15 21:30:45 +00:00
|
|
|
static inline void pop3_check(const char *fmt, const char *param)
|
2008-02-08 18:24:54 +00:00
|
|
|
{
|
|
|
|
pop3_checkr(fmt, param, NULL);
|
2008-01-29 00:59:15 +00:00
|
|
|
}
|
2008-02-02 17:54:35 +00:00
|
|
|
|
2008-02-26 21:13:17 +00:00
|
|
|
static void pop3_message(const char *filename)
|
2008-02-08 18:24:54 +00:00
|
|
|
{
|
2008-02-26 21:13:17 +00:00
|
|
|
int fd;
|
2008-02-08 18:24:54 +00:00
|
|
|
char *answer;
|
2008-02-26 21:13:17 +00:00
|
|
|
// create and open file filename
|
|
|
|
// read stdin, copy to created file
|
|
|
|
fd = xopen(filename, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL);
|
2008-02-27 09:39:04 +00:00
|
|
|
while ((answer = xmalloc_fgets_str(stdin, "\r\n")) != NULL) {
|
2008-02-08 18:24:54 +00:00
|
|
|
char *s = answer;
|
2008-02-26 21:13:17 +00:00
|
|
|
if ('.' == *answer) {
|
2008-02-08 18:24:54 +00:00
|
|
|
if ('.' == answer[1])
|
|
|
|
s++;
|
|
|
|
else if ('\r' == answer[1] && '\n' == answer[2] && '\0' == answer[3])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
xwrite(fd, s, strlen(s));
|
|
|
|
free(answer);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2008-02-26 21:13:17 +00:00
|
|
|
#endif
|
2008-02-08 18:24:54 +00:00
|
|
|
|
2008-06-28 21:46:41 +00:00
|
|
|
static char *parse_url(char *url, char **user, char **pass)
|
|
|
|
{
|
|
|
|
// parse [user[:pass]@]host
|
|
|
|
// return host
|
|
|
|
char *s = strchr(url, '@');
|
|
|
|
*user = *pass = NULL;
|
|
|
|
if (s) {
|
|
|
|
*s++ = '\0';
|
|
|
|
*user = url;
|
|
|
|
url = s;
|
|
|
|
s = strchr(*user, ':');
|
|
|
|
if (s) {
|
|
|
|
*s++ = '\0';
|
|
|
|
*pass = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
int sendgetmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int sendgetmail_main(int argc UNUSED_PARAM, char **argv)
|
2008-01-29 00:59:15 +00:00
|
|
|
{
|
2008-02-26 21:13:17 +00:00
|
|
|
llist_t *opt_recipients = NULL;
|
2008-06-27 21:24:08 +00:00
|
|
|
llist_t *opt_attachments = NULL;
|
|
|
|
char *opt_from;
|
2008-06-28 21:46:41 +00:00
|
|
|
char *opt_user;
|
|
|
|
char *opt_pass;
|
2008-01-29 00:59:15 +00:00
|
|
|
enum {
|
2008-02-26 21:13:17 +00:00
|
|
|
OPT_w = 1 << 0, // network timeout
|
2008-06-28 21:46:41 +00:00
|
|
|
OPT_H = 1 << 1, // [user:password@]server[:port]
|
|
|
|
OPT_S = 1 << 2, // connect using openssl s_client helper
|
2008-02-08 18:24:54 +00:00
|
|
|
|
2008-06-28 21:46:41 +00:00
|
|
|
OPTS_t = 1 << 3, // sendmail: read addresses from body
|
|
|
|
OPTF_t = 1 << 3, // fetchmail: use "TOP" not "RETR"
|
2008-02-08 18:24:54 +00:00
|
|
|
|
2008-06-28 21:46:41 +00:00
|
|
|
OPTS_s = 1 << 4, // sendmail: subject
|
|
|
|
OPTF_z = 1 << 4, // fetchmail: delete from server
|
2008-02-08 18:24:54 +00:00
|
|
|
|
2008-06-28 21:46:41 +00:00
|
|
|
OPTS_c = 1 << 5, // sendmail: assumed charset
|
|
|
|
OPTS_a = 1 << 6, // sendmail: attachment(s)
|
|
|
|
OPTS_i = 1 << 7, // sendmail: ignore lone dots in message body (implied)
|
2008-01-29 00:59:15 +00:00
|
|
|
|
2008-06-28 21:46:41 +00:00
|
|
|
OPTS_N = 1 << 8, // sendmail: request notification
|
2008-06-30 13:30:21 +00:00
|
|
|
OPTS_f = 1 << 9, // sendmail: sender address
|
2008-06-27 21:24:08 +00:00
|
|
|
};
|
2008-02-08 18:24:54 +00:00
|
|
|
const char *options;
|
2008-06-27 21:24:08 +00:00
|
|
|
int opts;
|
2008-02-08 18:24:54 +00:00
|
|
|
|
2008-02-27 09:39:04 +00:00
|
|
|
// init global variables
|
2008-02-26 21:13:17 +00:00
|
|
|
INIT_G();
|
|
|
|
|
2008-02-27 09:39:04 +00:00
|
|
|
// parse options, different option sets for sendmail and fetchmail
|
|
|
|
// N.B. opt_after_connect hereafter is NULL if we are called as fetchmail
|
|
|
|
// and is NOT NULL if we are called as sendmail
|
2008-02-26 21:13:17 +00:00
|
|
|
if (!ENABLE_FETCHMAIL || 's' == applet_name[0]) {
|
|
|
|
// SENDMAIL
|
2008-06-30 13:30:21 +00:00
|
|
|
// save initial stdin since body is piped!
|
2008-02-08 18:24:54 +00:00
|
|
|
xdup2(STDIN_FILENO, INITIAL_STDIN_FILENO);
|
2008-06-27 21:24:08 +00:00
|
|
|
opt_complementary = "w+:a::";
|
2008-06-28 21:46:41 +00:00
|
|
|
options = "w:H:St" "s:c:a:iN:f:";
|
2008-06-27 21:24:08 +00:00
|
|
|
// body is pseudo attachment read from stdin
|
|
|
|
llist_add_to_end(&opt_attachments, (char *)"-");
|
2008-02-08 18:24:54 +00:00
|
|
|
} else {
|
2008-02-26 21:13:17 +00:00
|
|
|
// FETCHMAIL
|
2008-02-08 18:24:54 +00:00
|
|
|
opt_after_connect = NULL;
|
2008-06-28 21:46:41 +00:00
|
|
|
opt_complementary = "-1:w+";
|
|
|
|
options = "w:H:St" "z";
|
2008-02-08 18:24:54 +00:00
|
|
|
}
|
|
|
|
opts = getopt32(argv, options,
|
2008-06-28 21:46:41 +00:00
|
|
|
&timeout /* -w */, &opt_connect /* -H */,
|
|
|
|
&opt_subject, &opt_charset, &opt_attachments, NULL, &opt_from
|
2008-01-29 00:59:15 +00:00
|
|
|
);
|
|
|
|
//argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
// connect to server
|
2008-06-30 13:30:21 +00:00
|
|
|
// host[:port] not specified ? -> use $HOSTNAME. no $HOSTNAME ? -> use localhost
|
2008-06-27 21:24:08 +00:00
|
|
|
if (!(opts & OPT_H)) {
|
2008-06-30 13:30:21 +00:00
|
|
|
opt_connect = getenv("HOSTNAME");
|
2008-06-27 21:24:08 +00:00
|
|
|
if (!opt_connect)
|
|
|
|
opt_connect = "127.0.0.1";
|
|
|
|
}
|
2008-06-28 21:46:41 +00:00
|
|
|
// fetch username and password, if any
|
|
|
|
// NB: parse_url modifies opt_connect[] ONLY if '@' is there.
|
|
|
|
// Thus "127.0.0.1" won't be modified, an is ok that it is RO.
|
|
|
|
opt_connect = parse_url((char*)opt_connect, &opt_user, &opt_pass);
|
|
|
|
// bb_error_msg("H[%s] U[%s] P[%s]", opt_connect, opt_user, opt_pass);
|
|
|
|
|
2008-06-30 13:30:21 +00:00
|
|
|
// username must be defined!
|
|
|
|
if (!opt_user) {
|
|
|
|
// N.B. IMHO getenv("USER") can be way easily spoofed!
|
|
|
|
opt_user = bb_getpwuid(NULL, -1, getuid());
|
|
|
|
}
|
|
|
|
|
2008-02-27 09:39:04 +00:00
|
|
|
// SSL ordered? ->
|
2008-06-28 21:46:41 +00:00
|
|
|
if (opts & OPT_S) {
|
2008-02-27 09:39:04 +00:00
|
|
|
// ... use openssl helper
|
2008-02-26 21:13:17 +00:00
|
|
|
launch_helper(xargs);
|
2008-02-27 09:39:04 +00:00
|
|
|
// no SSL ordered? ->
|
2008-02-08 18:24:54 +00:00
|
|
|
} else {
|
2008-02-27 09:39:04 +00:00
|
|
|
// ... make plain connect
|
|
|
|
int fd = create_and_connect_stream_or_die(opt_connect, 25);
|
|
|
|
// make ourselves a simple IO filter
|
|
|
|
// from now we know nothing about network :)
|
2008-02-08 18:24:54 +00:00
|
|
|
xmove_fd(fd, STDIN_FILENO);
|
|
|
|
xdup2(STDIN_FILENO, STDOUT_FILENO);
|
|
|
|
}
|
|
|
|
|
2008-06-27 21:24:08 +00:00
|
|
|
// are we sendmail?
|
2008-06-28 21:46:41 +00:00
|
|
|
if (!ENABLE_FETCHMAIL || opt_after_connect)
|
2008-02-27 09:39:04 +00:00
|
|
|
/***************************************************
|
|
|
|
* SENDMAIL
|
|
|
|
***************************************************/
|
2008-06-27 21:24:08 +00:00
|
|
|
{
|
2008-02-08 18:24:54 +00:00
|
|
|
int code;
|
|
|
|
char *boundary;
|
2008-02-26 21:13:17 +00:00
|
|
|
const char *fmt;
|
|
|
|
const char *p;
|
|
|
|
char *q;
|
2008-06-27 21:24:08 +00:00
|
|
|
llist_t *l;
|
2008-07-16 21:55:03 +00:00
|
|
|
llist_t *headers = NULL;
|
2008-06-27 21:24:08 +00:00
|
|
|
|
|
|
|
// recipients specified as arguments
|
|
|
|
while (*argv) {
|
|
|
|
// loose test on email address validity
|
|
|
|
if (strchr(sane(*argv), '@'))
|
|
|
|
llist_add_to_end(&opt_recipients, *argv);
|
|
|
|
argv++;
|
|
|
|
}
|
2008-02-08 18:24:54 +00:00
|
|
|
|
2008-06-27 21:24:08 +00:00
|
|
|
// if -t specified or no recipients specified -> enter all-included mode
|
2008-04-07 21:02:35 +00:00
|
|
|
// i.e. scan stdin for To: and Subject: lines ...
|
|
|
|
// ... and then use the rest of stdin as message body
|
2008-06-27 21:24:08 +00:00
|
|
|
// N.B. subject read from body has priority
|
|
|
|
// over that specified on command line.
|
|
|
|
// recipients are merged
|
2008-07-16 21:55:03 +00:00
|
|
|
// N.B. other headers are collected and will be dumped verbatim
|
2008-06-27 21:24:08 +00:00
|
|
|
if (opts & OPTS_t || !opt_recipients) {
|
2008-04-07 21:02:35 +00:00
|
|
|
// fetch recipients and (optionally) subject
|
|
|
|
char *s;
|
|
|
|
while ((s = xmalloc_reads(INITIAL_STDIN_FILENO, NULL, NULL)) != NULL) {
|
2008-07-15 20:56:23 +00:00
|
|
|
if (0 == strncasecmp("To: ", s, 4) || 0 == strncasecmp("Cc: ", s, 4)) {
|
2008-04-07 21:02:35 +00:00
|
|
|
llist_add_to_end(&opt_recipients, s+4);
|
2008-06-30 13:30:21 +00:00
|
|
|
/* } else if (0 == strncmp("From: ", s, 6)) {
|
|
|
|
opt_from = s+6;
|
|
|
|
opts |= OPTS_f;
|
|
|
|
*/ } else if (0 == strncmp("Subject: ", s, 9)) {
|
2008-04-07 21:02:35 +00:00
|
|
|
opt_subject = s+9;
|
|
|
|
opts |= OPTS_s;
|
2008-07-16 21:55:03 +00:00
|
|
|
} else if (s[0]) {
|
|
|
|
// misc header
|
|
|
|
llist_add_to_end(&headers, s);
|
2008-04-07 21:02:35 +00:00
|
|
|
} else {
|
|
|
|
free(s);
|
2008-07-16 21:55:03 +00:00
|
|
|
break; // empty line
|
2008-04-07 21:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-30 13:30:21 +00:00
|
|
|
// got no sender address? -> use username as a resort
|
|
|
|
if (!(opts & OPTS_f)) {
|
|
|
|
char *domain = safe_getdomainname();
|
|
|
|
opt_from = xasprintf("%s@%s", opt_user, domain);
|
|
|
|
free(domain);
|
|
|
|
}
|
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
// introduce to server
|
2008-06-30 13:30:21 +00:00
|
|
|
|
|
|
|
// we didn't use SSL helper? ->
|
|
|
|
if (!(opts & OPT_S)) {
|
|
|
|
// ... wait for initial server OK
|
|
|
|
smtp_check(NULL, 220);
|
|
|
|
}
|
|
|
|
|
2008-02-27 09:39:04 +00:00
|
|
|
// we should start with modern EHLO
|
2008-06-27 21:24:08 +00:00
|
|
|
if (250 != smtp_checkp("EHLO %s", sane(opt_from), -1)) {
|
2008-02-26 21:13:17 +00:00
|
|
|
smtp_checkp("HELO %s", opt_from, 250);
|
2008-01-29 00:59:15 +00:00
|
|
|
}
|
2008-02-08 18:24:54 +00:00
|
|
|
|
|
|
|
// set sender
|
2008-02-27 09:39:04 +00:00
|
|
|
// NOTE: if password has not been specified
|
|
|
|
// then no authentication is possible
|
2008-06-28 21:46:41 +00:00
|
|
|
code = (opt_pass) ? -1 : 250;
|
2008-02-08 18:24:54 +00:00
|
|
|
// first try softly without authentication
|
2008-02-26 21:13:17 +00:00
|
|
|
while (250 != smtp_checkp("MAIL FROM:<%s>", opt_from, code)) {
|
2008-02-08 18:24:54 +00:00
|
|
|
// MAIL FROM failed -> authentication needed
|
|
|
|
if (334 == smtp_check("AUTH LOGIN", -1)) {
|
2008-06-30 13:30:21 +00:00
|
|
|
uuencode(NULL, opt_user); // opt_user != NULL
|
2008-02-08 18:24:54 +00:00
|
|
|
smtp_check("", 334);
|
|
|
|
uuencode(NULL, opt_pass);
|
|
|
|
smtp_check("", 235);
|
|
|
|
}
|
2008-02-27 09:39:04 +00:00
|
|
|
// authenticated OK? -> retry to set sender
|
|
|
|
// but this time die on failure!
|
2008-02-08 18:24:54 +00:00
|
|
|
code = 250;
|
2008-01-29 00:59:15 +00:00
|
|
|
}
|
2008-02-26 21:13:17 +00:00
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
// set recipients
|
2008-06-27 21:24:08 +00:00
|
|
|
for (l = opt_recipients; l; l = l->link) {
|
|
|
|
smtp_checkp("RCPT TO:<%s>", sane(l->data), 250);
|
2008-01-29 00:59:15 +00:00
|
|
|
}
|
|
|
|
|
2008-02-27 09:39:04 +00:00
|
|
|
// enter "put message" mode
|
2008-02-08 18:24:54 +00:00
|
|
|
smtp_check("DATA", 354);
|
2008-02-27 09:39:04 +00:00
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
// put address headers
|
2008-02-26 21:13:17 +00:00
|
|
|
printf("From: %s\r\n", opt_from);
|
2008-06-27 21:24:08 +00:00
|
|
|
for (l = opt_recipients; l; l = l->link) {
|
|
|
|
printf("To: %s\r\n", l->data);
|
2008-01-29 00:59:15 +00:00
|
|
|
}
|
2008-02-27 09:39:04 +00:00
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
// put encoded subject
|
|
|
|
if (opts & OPTS_c)
|
2008-02-26 21:13:17 +00:00
|
|
|
sane((char *)opt_charset);
|
2008-02-08 18:24:54 +00:00
|
|
|
if (opts & OPTS_s) {
|
2008-02-26 21:13:17 +00:00
|
|
|
printf("Subject: =?%s?B?", opt_charset);
|
|
|
|
uuencode(NULL, opt_subject);
|
2008-02-08 18:24:54 +00:00
|
|
|
printf("?=\r\n");
|
|
|
|
}
|
2008-02-27 09:39:04 +00:00
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
// put notification
|
2008-06-28 21:46:41 +00:00
|
|
|
if (opts & OPTS_N)
|
2008-02-26 21:13:17 +00:00
|
|
|
printf("Disposition-Notification-To: %s\r\n", opt_from);
|
2008-02-27 09:39:04 +00:00
|
|
|
|
2008-07-16 21:55:03 +00:00
|
|
|
// put headers we could have preread with -t
|
|
|
|
for (l = headers; l; l = l->link) {
|
|
|
|
printf("%s\r\n", l->data);
|
|
|
|
}
|
|
|
|
|
2008-02-27 09:39:04 +00:00
|
|
|
// make a random string -- it will delimit message parts
|
|
|
|
srand(monotonic_us());
|
2008-03-24 02:18:03 +00:00
|
|
|
boundary = xasprintf("%d-%d-%d", rand(), rand(), rand());
|
2008-02-27 09:39:04 +00:00
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
// put common headers and body start
|
2008-01-29 00:59:15 +00:00
|
|
|
printf(
|
2008-02-08 18:24:54 +00:00
|
|
|
"Message-ID: <%s>\r\n"
|
|
|
|
"Mime-Version: 1.0\r\n"
|
|
|
|
"%smultipart/mixed; boundary=\"%s\"\r\n"
|
2008-01-29 00:59:15 +00:00
|
|
|
, boundary
|
2008-02-02 17:54:35 +00:00
|
|
|
, "Content-Type: "
|
2008-02-08 18:24:54 +00:00
|
|
|
, boundary
|
2008-01-29 00:59:15 +00:00
|
|
|
);
|
2008-02-26 21:13:17 +00:00
|
|
|
|
2008-02-27 09:39:04 +00:00
|
|
|
// put body + attachment(s)
|
|
|
|
// N.B. all these weird things just to be tiny
|
|
|
|
// by reusing string patterns!
|
2008-02-26 21:13:17 +00:00
|
|
|
fmt =
|
2008-02-08 18:24:54 +00:00
|
|
|
"\r\n--%s\r\n"
|
|
|
|
"%stext/plain; charset=%s\r\n"
|
|
|
|
"%s%s\r\n"
|
|
|
|
"%s"
|
|
|
|
;
|
2008-02-26 21:13:17 +00:00
|
|
|
p = opt_charset;
|
|
|
|
q = (char *)"";
|
2008-06-27 21:24:08 +00:00
|
|
|
l = opt_attachments;
|
|
|
|
while (l) {
|
2008-02-08 18:24:54 +00:00
|
|
|
printf(
|
|
|
|
fmt
|
|
|
|
, boundary
|
|
|
|
, "Content-Type: "
|
|
|
|
, p
|
|
|
|
, "Content-Disposition: inline"
|
|
|
|
, q
|
|
|
|
, "Content-Transfer-Encoding: base64\r\n"
|
|
|
|
);
|
|
|
|
p = "";
|
|
|
|
fmt =
|
|
|
|
"\r\n--%s\r\n"
|
|
|
|
"%sapplication/octet-stream%s\r\n"
|
|
|
|
"%s; filename=\"%s\"\r\n"
|
|
|
|
"%s"
|
|
|
|
;
|
2008-06-27 21:24:08 +00:00
|
|
|
uuencode(l->data, NULL);
|
|
|
|
l = l->link;
|
|
|
|
if (l)
|
|
|
|
q = bb_get_last_path_component_strip(l->data);
|
2008-02-08 18:24:54 +00:00
|
|
|
}
|
2008-02-26 21:13:17 +00:00
|
|
|
|
2008-02-27 09:39:04 +00:00
|
|
|
// put message terminator
|
2008-02-08 18:24:54 +00:00
|
|
|
printf("\r\n--%s--\r\n" "\r\n", boundary);
|
|
|
|
|
2008-02-27 09:39:04 +00:00
|
|
|
// leave "put message" mode
|
2008-03-24 02:18:03 +00:00
|
|
|
smtp_check(".", 250);
|
2008-02-27 09:39:04 +00:00
|
|
|
// ... and say goodbye
|
2008-02-08 18:24:54 +00:00
|
|
|
smtp_check("QUIT", 221);
|
2008-06-27 21:24:08 +00:00
|
|
|
}
|
2008-02-26 21:13:17 +00:00
|
|
|
#if ENABLE_FETCHMAIL
|
2008-02-27 09:39:04 +00:00
|
|
|
/***************************************************
|
|
|
|
* FETCHMAIL
|
|
|
|
***************************************************/
|
2008-06-27 21:24:08 +00:00
|
|
|
else {
|
2008-02-08 18:24:54 +00:00
|
|
|
char *buf;
|
|
|
|
unsigned nmsg;
|
2008-02-26 21:13:17 +00:00
|
|
|
char *hostname;
|
|
|
|
pid_t pid;
|
2008-02-27 09:39:04 +00:00
|
|
|
|
|
|
|
// cache fetch command:
|
|
|
|
// TOP will return only the headers
|
|
|
|
// RETR will dump the whole message
|
2008-02-26 21:13:17 +00:00
|
|
|
const char *retr = (opts & OPTF_t) ? "TOP %u 0" : "RETR %u";
|
|
|
|
|
|
|
|
// goto maildir
|
|
|
|
xchdir(*argv++);
|
|
|
|
|
|
|
|
// cache postprocess program
|
|
|
|
*fargs = *argv;
|
2008-03-24 02:18:03 +00:00
|
|
|
|
2008-02-26 21:13:17 +00:00
|
|
|
// authenticate
|
2008-06-30 13:30:21 +00:00
|
|
|
|
2008-06-28 21:46:41 +00:00
|
|
|
// password is mandatory
|
|
|
|
if (!opt_pass) {
|
|
|
|
bb_error_msg_and_die("no password");
|
|
|
|
}
|
2008-02-27 09:39:04 +00:00
|
|
|
|
|
|
|
// get server greeting
|
2008-02-08 18:24:54 +00:00
|
|
|
pop3_checkr(NULL, NULL, &buf);
|
2008-02-27 09:39:04 +00:00
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
// server supports APOP?
|
2008-02-26 21:13:17 +00:00
|
|
|
if ('<' == *buf) {
|
2008-02-08 18:24:54 +00:00
|
|
|
md5_ctx_t md5;
|
2008-02-27 09:39:04 +00:00
|
|
|
// yes! compose <stamp><password>
|
2008-02-08 18:24:54 +00:00
|
|
|
char *s = strchr(buf, '>');
|
|
|
|
if (s)
|
|
|
|
strcpy(s+1, opt_pass);
|
2008-02-26 21:13:17 +00:00
|
|
|
s = buf;
|
2008-02-08 18:24:54 +00:00
|
|
|
// get md5 sum of <stamp><password>
|
|
|
|
md5_begin(&md5);
|
|
|
|
md5_hash(s, strlen(s), &md5);
|
|
|
|
md5_end(s, &md5);
|
2008-02-26 21:13:17 +00:00
|
|
|
// NOTE: md5 struct contains enough space
|
|
|
|
// so we reuse md5 space instead of xzalloc(16*2+1)
|
|
|
|
#define md5_hex ((uint8_t *)&md5)
|
|
|
|
// uint8_t *md5_hex = (uint8_t *)&md5;
|
2008-05-09 18:07:15 +00:00
|
|
|
*bin2hex((char *)md5_hex, s, 16) = '\0';
|
2008-02-08 18:24:54 +00:00
|
|
|
// APOP
|
2008-02-26 21:13:17 +00:00
|
|
|
s = xasprintf("%s %s", opt_user, md5_hex);
|
|
|
|
#undef md5_hex
|
2008-02-08 18:24:54 +00:00
|
|
|
pop3_check("APOP %s", s);
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
|
|
|
free(s);
|
2008-02-26 21:13:17 +00:00
|
|
|
free(buf-4); // buf is "+OK " away from malloc'ed string
|
2008-02-08 18:24:54 +00:00
|
|
|
}
|
2008-02-27 09:39:04 +00:00
|
|
|
// server ignores APOP -> use simple text authentication
|
2008-02-08 18:24:54 +00:00
|
|
|
} else {
|
|
|
|
// USER
|
|
|
|
pop3_check("USER %s", opt_user);
|
|
|
|
// PASS
|
|
|
|
pop3_check("PASS %s", opt_pass);
|
|
|
|
}
|
|
|
|
|
2008-02-27 09:39:04 +00:00
|
|
|
// get mailbox statistics
|
2008-02-08 18:24:54 +00:00
|
|
|
pop3_checkr("STAT", NULL, &buf);
|
|
|
|
|
2008-02-26 21:13:17 +00:00
|
|
|
// prepare message filename suffix
|
2008-02-27 09:39:04 +00:00
|
|
|
hostname = safe_gethostname();
|
2008-02-26 21:13:17 +00:00
|
|
|
pid = getpid();
|
|
|
|
|
2008-02-27 09:39:04 +00:00
|
|
|
// get messages counter
|
2008-02-26 21:13:17 +00:00
|
|
|
// NOTE: we don't use xatou(buf) since buf is "nmsg nbytes"
|
|
|
|
// we only need nmsg and atoi is just exactly what we need
|
|
|
|
// if atoi fails to convert buf into number it returns 0
|
2008-03-24 02:05:58 +00:00
|
|
|
// in this case the following loop simply will not be executed
|
2008-02-26 21:13:17 +00:00
|
|
|
nmsg = atoi(buf);
|
2008-02-08 18:24:54 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
2008-02-26 21:13:17 +00:00
|
|
|
free(buf-4); // buf is "+OK " away from malloc'ed string
|
2008-02-08 18:24:54 +00:00
|
|
|
|
2008-02-11 11:44:38 +00:00
|
|
|
// loop through messages
|
2008-02-08 18:24:54 +00:00
|
|
|
for (; nmsg; nmsg--) {
|
2008-02-27 09:39:04 +00:00
|
|
|
|
|
|
|
// generate unique filename
|
2008-06-27 21:24:08 +00:00
|
|
|
char *filename = xasprintf("tmp/%llu.%u.%s",
|
|
|
|
monotonic_us(), (unsigned)pid, hostname);
|
2008-02-26 21:13:17 +00:00
|
|
|
char *target;
|
2008-02-27 09:39:04 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
// retrieve message in ./tmp/
|
2008-03-17 09:04:04 +00:00
|
|
|
pop3_check(retr, (const char *)(ptrdiff_t)nmsg);
|
2008-02-26 21:13:17 +00:00
|
|
|
pop3_message(filename);
|
2008-02-08 18:24:54 +00:00
|
|
|
// delete message from server
|
|
|
|
if (opts & OPTF_z)
|
2008-03-17 09:04:04 +00:00
|
|
|
pop3_check("DELE %u", (const char*)(ptrdiff_t)nmsg);
|
2008-02-26 21:13:17 +00:00
|
|
|
|
|
|
|
// run postprocessing program
|
|
|
|
if (*fargs) {
|
|
|
|
fargs[1] = filename;
|
|
|
|
rc = wait4pid(spawn((char **)fargs));
|
|
|
|
if (99 == rc)
|
|
|
|
break;
|
|
|
|
if (1 == rc)
|
|
|
|
goto skip;
|
|
|
|
}
|
2008-02-27 09:39:04 +00:00
|
|
|
|
|
|
|
// atomically move message to ./new/
|
2008-02-26 21:13:17 +00:00
|
|
|
target = xstrdup(filename);
|
|
|
|
strncpy(target, "new", 3);
|
|
|
|
// ... or just stop receiving on error
|
|
|
|
if (rename_or_warn(filename, target))
|
|
|
|
break;
|
|
|
|
free(target);
|
|
|
|
skip:
|
|
|
|
free(filename);
|
2008-02-08 18:24:54 +00:00
|
|
|
}
|
2008-02-26 21:13:17 +00:00
|
|
|
|
2008-02-08 18:24:54 +00:00
|
|
|
// Bye
|
|
|
|
pop3_check("QUIT", NULL);
|
|
|
|
}
|
2008-06-27 21:24:08 +00:00
|
|
|
#endif // ENABLE_FETCHMAIL
|
2008-01-29 00:59:15 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|