2008-01-29 06:29:15 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2008-09-28 19:20:57 +05:30
|
|
|
* bare bones sendmail
|
2008-01-29 06:29:15 +05:30
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-09-27 19:31:22 +05:30
|
|
|
struct globals {
|
|
|
|
pid_t helper_pid;
|
|
|
|
unsigned timeout;
|
|
|
|
FILE *fp0; // initial stdin
|
|
|
|
// arguments for SSL connection helper
|
|
|
|
const char *xargs[9];
|
|
|
|
};
|
|
|
|
#define G (*ptr_to_globals)
|
|
|
|
#define helper_pid (G.helper_pid)
|
|
|
|
#define timeout (G.timeout )
|
|
|
|
#define fp0 (G.fp0 )
|
|
|
|
#define xargs (G.xargs )
|
|
|
|
#define INIT_G() do { \
|
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
|
|
|
xargs[0] = "openssl"; \
|
|
|
|
xargs[1] = "s_client"; \
|
|
|
|
xargs[2] = "-quiet"; \
|
|
|
|
xargs[3] = "-connect"; \
|
|
|
|
/*xargs[4] = "localhost";*/ \
|
|
|
|
xargs[5] = "-tls1"; \
|
|
|
|
xargs[6] = "-starttls"; \
|
|
|
|
xargs[7] = "smtp"; \
|
|
|
|
} while (0)
|
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
#define opt_connect (xargs[4])
|
2008-01-29 06:29:15 +05:30
|
|
|
|
2008-02-02 23:24:35 +05:30
|
|
|
static void uuencode(char *fname, const char *text)
|
2008-01-29 06:29:15 +05:30
|
|
|
{
|
2008-02-08 23:54:54 +05:30
|
|
|
enum {
|
|
|
|
SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */
|
|
|
|
DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
|
|
|
|
};
|
|
|
|
|
2008-02-02 23:24:35 +05:30
|
|
|
#define src_buf text
|
2008-09-27 19:31:22 +05:30
|
|
|
FILE *fp = fp;
|
|
|
|
ssize_t len = len;
|
2008-02-02 23:24:35 +05:30
|
|
|
char dst_buf[DST_BUF_SIZE + 1];
|
2008-01-29 06:29:15 +05:30
|
|
|
|
2008-02-02 23:24:35 +05:30
|
|
|
if (fname) {
|
2008-09-27 19:31:22 +05:30
|
|
|
fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : fp0;
|
2008-02-02 23:24:35 +05:30
|
|
|
src_buf = bb_common_bufsiz1;
|
2008-02-27 02:43:17 +05:30
|
|
|
// 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 23:24:35 +05:30
|
|
|
len = strlen(text);
|
2008-02-27 02:43:17 +05:30
|
|
|
} else
|
|
|
|
return;
|
2008-02-02 23:24:35 +05:30
|
|
|
|
2008-01-29 06:29:15 +05:30
|
|
|
while (1) {
|
2008-02-02 23:24:35 +05:30
|
|
|
size_t size;
|
|
|
|
if (fname) {
|
2008-09-27 19:31:22 +05:30
|
|
|
size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
|
2008-02-02 23:24:35 +05:30
|
|
|
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 06:29:15 +05:30
|
|
|
if (!size)
|
|
|
|
break;
|
2008-02-08 23:54:54 +05:30
|
|
|
// encode the buffer we just read in
|
2008-02-02 23:24:35 +05:30
|
|
|
bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
|
|
|
|
if (fname) {
|
2008-09-27 19:31:22 +05:30
|
|
|
printf("\r\n");
|
2008-02-02 23:24:35 +05:30
|
|
|
} else {
|
|
|
|
src_buf += size;
|
|
|
|
len -= size;
|
|
|
|
}
|
2008-09-27 19:31:22 +05:30
|
|
|
fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout);
|
2008-01-29 06:29:15 +05:30
|
|
|
}
|
2008-02-08 23:54:54 +05:30
|
|
|
if (fname)
|
2008-09-27 19:31:22 +05:30
|
|
|
fclose(fp);
|
2008-06-28 02:54:08 +05:30
|
|
|
#undef src_buf
|
2008-01-29 06:29:15 +05:30
|
|
|
}
|
|
|
|
|
2008-02-08 23:54:54 +05:30
|
|
|
|
2008-09-27 19:31:22 +05:30
|
|
|
#if ENABLE_FEATURE_SENDMAIL_SSL
|
2008-02-08 23:54:54 +05:30
|
|
|
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 06:29:15 +05:30
|
|
|
// generic signal handler
|
|
|
|
static void signal_handler(int signo)
|
|
|
|
{
|
2008-02-27 02:43:17 +05:30
|
|
|
#define err signo
|
2008-02-08 23:54:54 +05:30
|
|
|
if (SIGALRM == signo) {
|
|
|
|
kill_helper();
|
2008-01-29 06:29:15 +05:30
|
|
|
bb_error_msg_and_die("timed out");
|
2008-02-08 23:54:54 +05:30
|
|
|
}
|
2008-01-29 06:29:15 +05:30
|
|
|
|
|
|
|
// SIGCHLD. reap zombies
|
|
|
|
if (wait_any_nohang(&err) > 0)
|
2008-09-28 19:20:57 +05:30
|
|
|
if (WIFEXITED(err)) {
|
|
|
|
// if (WEXITSTATUS(err))
|
|
|
|
bb_error_msg_and_die("child exited (%d)", WEXITSTATUS(err));
|
|
|
|
// else
|
|
|
|
// kill(0, SIGCONT);
|
|
|
|
}
|
2008-06-28 02:54:08 +05:30
|
|
|
#undef err
|
|
|
|
}
|
|
|
|
|
2008-02-08 23:54:54 +05:30
|
|
|
static void launch_helper(const char **argv)
|
2008-01-29 06:29:15 +05:30
|
|
|
{
|
2008-02-08 23:54:54 +05:30
|
|
|
// setup vanilla unidirectional pipes interchange
|
|
|
|
int idx;
|
|
|
|
int pipes[4];
|
2008-06-28 02:54:08 +05:30
|
|
|
|
2008-02-08 23:54:54 +05:30
|
|
|
xpipe(pipes);
|
|
|
|
xpipe(pipes+2);
|
2008-07-01 21:39:07 +05:30
|
|
|
helper_pid = vfork();
|
|
|
|
if (helper_pid < 0)
|
|
|
|
bb_perror_msg_and_die("vfork");
|
2008-06-28 02:54:08 +05:30
|
|
|
idx = (!helper_pid) * 2;
|
2008-02-08 23:54:54 +05:30
|
|
|
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 15:09:04 +05:30
|
|
|
// child: try to execute connection helper
|
2008-02-27 02:43:17 +05:30
|
|
|
BB_EXECVP(*argv, (char **)argv);
|
2008-02-08 23:54:54 +05:30
|
|
|
_exit(127);
|
|
|
|
}
|
2008-02-27 15:09:04 +05:30
|
|
|
// parent: check whether child is alive
|
2008-02-27 02:43:17 +05:30
|
|
|
bb_signals(0
|
|
|
|
+ (1 << SIGCHLD)
|
|
|
|
+ (1 << SIGALRM)
|
|
|
|
, signal_handler);
|
2008-02-08 23:54:54 +05:30
|
|
|
signal_handler(SIGCHLD);
|
|
|
|
// child seems OK -> parent goes on
|
|
|
|
}
|
2008-09-27 19:31:22 +05:30
|
|
|
#else
|
|
|
|
#define kill_helper() ((void)0)
|
|
|
|
#define launch_helper(x) bb_error_msg_and_die("no SSL support")
|
|
|
|
#endif
|
2008-02-08 23:54:54 +05:30
|
|
|
|
2008-02-27 15:09:04 +05:30
|
|
|
static const char *command(const char *fmt, const char *param)
|
2008-02-08 23:54:54 +05:30
|
|
|
{
|
2008-02-27 15:09:04 +05:30
|
|
|
const char *msg = fmt;
|
2008-02-08 23:54:54 +05:30
|
|
|
alarm(timeout);
|
|
|
|
if (msg) {
|
2008-02-27 15:09:04 +05:30
|
|
|
msg = xasprintf(fmt, param);
|
2008-02-08 23:54:54 +05:30
|
|
|
printf("%s\r\n", msg);
|
|
|
|
}
|
2008-01-29 06:29:15 +05:30
|
|
|
fflush(stdout);
|
2008-02-08 23:54:54 +05:30
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smtp_checkp(const char *fmt, const char *param, int code)
|
|
|
|
{
|
|
|
|
char *answer;
|
2008-02-27 15:09:04 +05:30
|
|
|
const char *msg = command(fmt, param);
|
2008-02-08 23:54:54 +05:30
|
|
|
// 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-27 01:34:27 +05:30
|
|
|
while ((answer = xmalloc_fgetline(stdin)) != NULL)
|
2008-02-27 02:43:17 +05:30
|
|
|
if (strlen(answer) <= 3 || '-' != answer[3])
|
|
|
|
break;
|
2008-01-29 06:29:15 +05:30
|
|
|
if (answer) {
|
|
|
|
int n = atoi(answer);
|
2008-02-08 23:54:54 +05:30
|
|
|
alarm(0);
|
2008-09-27 19:31:22 +05:30
|
|
|
free(answer);
|
|
|
|
if (-1 == code || n == code)
|
2008-01-29 06:29:15 +05:30
|
|
|
return n;
|
|
|
|
}
|
2008-02-08 23:54:54 +05:30
|
|
|
kill_helper();
|
|
|
|
bb_error_msg_and_die("%s failed", msg);
|
2008-01-29 06:29:15 +05:30
|
|
|
}
|
|
|
|
|
2008-05-16 03:00:45 +05:30
|
|
|
static inline int smtp_check(const char *fmt, int code)
|
2008-01-29 06:29:15 +05:30
|
|
|
{
|
2008-02-08 23:54:54 +05:30
|
|
|
return smtp_checkp(fmt, NULL, code);
|
|
|
|
}
|
|
|
|
|
2008-02-27 02:43:17 +05:30
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2008-08-11 02:16:39 +05:30
|
|
|
// NB: parse_url can modify url[] (despite const), but only if '@' is there
|
|
|
|
static const char *parse_url(const char *url, const char **user, const char **pass)
|
2008-06-29 03:16:41 +05:30
|
|
|
{
|
|
|
|
// 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-07-18 01:07:09 +05:30
|
|
|
static void rcptto(const char *s)
|
|
|
|
{
|
|
|
|
smtp_checkp("RCPT TO:<%s>", s, 250);
|
|
|
|
}
|
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int sendmail_main(int argc UNUSED_PARAM, char **argv)
|
2008-01-29 06:29:15 +05:30
|
|
|
{
|
2008-09-27 19:31:22 +05:30
|
|
|
#if ENABLE_FEATURE_SENDMAIL_MAILX
|
2008-06-28 02:54:08 +05:30
|
|
|
llist_t *opt_attachments = NULL;
|
2008-09-28 19:20:57 +05:30
|
|
|
const char *opt_subject;
|
|
|
|
const char *opt_charset = CONFIG_FEATURE_SENDMAIL_CHARSET;
|
|
|
|
#if ENABLE_FEATURE_SENDMAIL_MAILXX
|
|
|
|
llist_t *opt_carboncopies = NULL;
|
|
|
|
char *opt_errors_to;
|
|
|
|
#endif
|
2008-09-27 19:31:22 +05:30
|
|
|
#endif
|
|
|
|
char *opt_from, *opt_fullname;
|
2008-08-11 02:16:39 +05:30
|
|
|
const char *opt_user;
|
|
|
|
const char *opt_pass;
|
2008-09-28 19:20:57 +05:30
|
|
|
int code;
|
|
|
|
char *boundary;
|
|
|
|
llist_t *l;
|
|
|
|
llist_t *headers = NULL;
|
|
|
|
char *domain = sane(safe_getdomainname());
|
|
|
|
unsigned opts;
|
2008-09-27 19:31:22 +05:30
|
|
|
|
2008-01-29 06:29:15 +05:30
|
|
|
enum {
|
2008-02-27 02:43:17 +05:30
|
|
|
OPT_w = 1 << 0, // network timeout
|
2008-06-29 03:16:41 +05:30
|
|
|
OPT_H = 1 << 1, // [user:password@]server[:port]
|
|
|
|
OPT_S = 1 << 2, // connect using openssl s_client helper
|
2008-09-28 19:20:57 +05:30
|
|
|
OPT_t = 1 << 3, // read message for recipients
|
|
|
|
OPT_N = 1 << 4, // request notification
|
|
|
|
OPT_f = 1 << 5, // sender address
|
|
|
|
OPT_F = 1 << 6, // sender name, overrides $NAME
|
2008-10-29 18:46:28 +05:30
|
|
|
#if ENABLE_FEATURE_SENDMAIL_MAILX
|
2008-09-28 19:20:57 +05:30
|
|
|
OPT_s = 1 << 7, // subject
|
|
|
|
OPT_j = 1 << 8, // assumed charset
|
|
|
|
OPT_a = 1 << 9, // attachment(s)
|
2008-10-29 18:46:28 +05:30
|
|
|
#if ENABLE_FEATURE_SENDMAIL_MAILXX
|
2008-09-28 19:20:57 +05:30
|
|
|
OPT_c = 1 << 10, // carbon copy
|
|
|
|
OPT_e = 1 << 11, // errors-to address
|
2008-10-29 18:46:28 +05:30
|
|
|
#endif
|
|
|
|
#endif
|
2008-06-28 02:54:08 +05:30
|
|
|
};
|
2008-02-08 23:54:54 +05:30
|
|
|
|
2008-02-27 15:09:04 +05:30
|
|
|
// init global variables
|
2008-02-27 02:43:17 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// save initial stdin since body is piped!
|
|
|
|
xdup2(STDIN_FILENO, 3);
|
|
|
|
fp0 = fdopen(3, "r");
|
|
|
|
|
|
|
|
// parse options
|
|
|
|
opt_complementary = "w+:a::" USE_FEATURE_SENDMAIL_MAILXX("c::");
|
|
|
|
opts = getopt32(argv,
|
|
|
|
"w:H:St" "N:f:F:" USE_FEATURE_SENDMAIL_MAILX("s:j:a:") USE_FEATURE_SENDMAIL_MAILXX("c:e:")
|
|
|
|
"X:V:vq:R:O:o:nmL:Iih:GC:B:b:A:" // postfix compat only, ignored
|
|
|
|
// r:Q:p:M:Dd are candidates from another man page. TODO?
|
|
|
|
"46E", // ssmtp introduces another quirks. TODO?: -a[upm] (user, pass, method) to be supported
|
2008-06-29 03:16:41 +05:30
|
|
|
&timeout /* -w */, &opt_connect /* -H */,
|
2008-09-27 19:31:22 +05:30
|
|
|
NULL, &opt_from, &opt_fullname,
|
2008-09-28 19:20:57 +05:30
|
|
|
USE_FEATURE_SENDMAIL_MAILX(&opt_subject, &opt_charset, &opt_attachments,)
|
|
|
|
USE_FEATURE_SENDMAIL_MAILXX(&opt_carboncopies, &opt_errors_to,)
|
2008-09-27 19:31:22 +05:30
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
2008-01-29 06:29:15 +05:30
|
|
|
);
|
|
|
|
//argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2008-02-08 23:54:54 +05:30
|
|
|
// connect to server
|
2008-09-27 19:31:22 +05:30
|
|
|
// host[:port] not specified ? -> use $SMTPHOST. no $SMTPHOST ? -> use localhost
|
2008-06-28 02:54:08 +05:30
|
|
|
if (!(opts & OPT_H)) {
|
2008-09-27 19:31:22 +05:30
|
|
|
opt_connect = getenv("SMTPHOST");
|
2008-06-28 02:54:08 +05:30
|
|
|
if (!opt_connect)
|
|
|
|
opt_connect = "127.0.0.1";
|
|
|
|
}
|
2008-06-29 03:16:41 +05:30
|
|
|
// 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.
|
2008-08-11 02:16:39 +05:30
|
|
|
opt_connect = parse_url(opt_connect, &opt_user, &opt_pass);
|
2008-06-29 03:16:41 +05:30
|
|
|
|
2008-06-30 19:00:21 +05:30
|
|
|
// 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 15:09:04 +05:30
|
|
|
// SSL ordered? ->
|
2008-06-29 03:16:41 +05:30
|
|
|
if (opts & OPT_S) {
|
2008-02-27 15:09:04 +05:30
|
|
|
// ... use openssl helper
|
2008-02-27 02:43:17 +05:30
|
|
|
launch_helper(xargs);
|
2008-02-27 15:09:04 +05:30
|
|
|
// no SSL ordered? ->
|
2008-02-08 23:54:54 +05:30
|
|
|
} else {
|
2008-02-27 15:09:04 +05:30
|
|
|
// ... 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 23:54:54 +05:30
|
|
|
xmove_fd(fd, STDIN_FILENO);
|
|
|
|
xdup2(STDIN_FILENO, STDOUT_FILENO);
|
|
|
|
}
|
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// got no sender address? -> use username as a resort
|
|
|
|
if (!(opts & OPT_f)) {
|
|
|
|
opt_from = xasprintf("%s@%s", opt_user, domain);
|
|
|
|
}
|
2008-06-30 19:00:21 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// introduce to server
|
2008-06-30 19:00:21 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// we didn't use SSL helper? ->
|
|
|
|
if (!(opts & OPT_S)) {
|
|
|
|
// ... wait for initial server OK
|
|
|
|
smtp_check(NULL, 220);
|
|
|
|
}
|
2008-06-30 19:00:21 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// we should start with modern EHLO
|
|
|
|
if (250 != smtp_checkp("EHLO %s", domain, -1)) {
|
|
|
|
smtp_checkp("HELO %s", domain, 250);
|
|
|
|
}
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
free(domain);
|
|
|
|
|
|
|
|
// set sender
|
|
|
|
// NOTE: if password has not been specified
|
|
|
|
// then no authentication is possible
|
|
|
|
code = (opt_pass ? -1 : 250);
|
|
|
|
// first try softly without authentication
|
|
|
|
while (250 != smtp_checkp("MAIL FROM:<%s>", opt_from, code)) {
|
|
|
|
// MAIL FROM failed -> authentication needed
|
|
|
|
if (334 == smtp_check("AUTH LOGIN", -1)) {
|
|
|
|
uuencode(NULL, opt_user); // opt_user != NULL
|
|
|
|
smtp_check("", 334);
|
|
|
|
uuencode(NULL, opt_pass);
|
|
|
|
smtp_check("", 235);
|
2008-01-29 06:29:15 +05:30
|
|
|
}
|
2008-09-28 19:20:57 +05:30
|
|
|
// authenticated OK? -> retry to set sender
|
|
|
|
// but this time die on failure!
|
|
|
|
code = 250;
|
|
|
|
}
|
|
|
|
|
|
|
|
// recipients specified as arguments
|
|
|
|
while (*argv) {
|
|
|
|
char *s = sane(*argv);
|
|
|
|
// loose test on email address validity
|
|
|
|
if (strchr(s, '@')) {
|
|
|
|
rcptto(s);
|
|
|
|
llist_add_to_end(&headers, xasprintf("To: %s", s));
|
2008-01-29 06:29:15 +05:30
|
|
|
}
|
2008-09-28 19:20:57 +05:30
|
|
|
argv++;
|
|
|
|
}
|
2008-02-27 02:43:17 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
#if ENABLE_FEATURE_SENDMAIL_MAILXX
|
|
|
|
// carbon copies recipients specified as -c options
|
|
|
|
for (l = opt_carboncopies; l; l = l->link) {
|
|
|
|
char *s = sane(l->data);
|
|
|
|
// loose test on email address validity
|
|
|
|
if (strchr(s, '@')) {
|
|
|
|
rcptto(s);
|
|
|
|
// TODO: do we ever need to mangle the message?
|
|
|
|
//llist_add_to_end(&headers, xasprintf("Cc: %s", s));
|
2008-07-18 01:07:09 +05:30
|
|
|
}
|
2008-09-28 19:20:57 +05:30
|
|
|
}
|
|
|
|
#endif
|
2008-07-18 01:07:09 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// if -t specified or no recipients specified -> read recipients from message
|
|
|
|
// i.e. scan stdin for To:, Cc:, Bcc: lines ...
|
|
|
|
// ... and then use the rest of stdin as message body
|
|
|
|
// N.B. subject read from body can be further overrided with one specified on command line.
|
|
|
|
// recipients are merged. Bcc: lines are deleted
|
|
|
|
// N.B. other headers are collected and will be dumped verbatim
|
|
|
|
if (opts & OPT_t || !headers) {
|
|
|
|
// fetch recipients and (optionally) subject
|
|
|
|
char *s;
|
|
|
|
while ((s = xmalloc_fgetline(fp0)) != NULL) {
|
|
|
|
if (0 == strncasecmp("To: ", s, 4) || 0 == strncasecmp("Cc: ", s, 4)) {
|
|
|
|
rcptto(sane(s+4));
|
|
|
|
llist_add_to_end(&headers, s);
|
|
|
|
} else if (0 == strncasecmp("Bcc: ", s, 5)) {
|
|
|
|
rcptto(sane(s+5));
|
|
|
|
free(s);
|
|
|
|
// N.B. Bcc vanishes from headers!
|
|
|
|
} else if (0 == strncmp("Subject: ", s, 9)) {
|
|
|
|
// we read subject -> use it verbatim unless it is specified
|
|
|
|
// on command line
|
2008-10-29 18:46:28 +05:30
|
|
|
#if ENABLE_FEATURE_SENDMAIL_MAILX
|
|
|
|
if (opts & OPT_s)
|
2008-07-18 01:07:09 +05:30
|
|
|
free(s);
|
2008-10-29 18:46:28 +05:30
|
|
|
else
|
|
|
|
#endif
|
|
|
|
llist_add_to_end(&headers, s);
|
2008-09-28 19:20:57 +05:30
|
|
|
} else if (s[0]) {
|
|
|
|
// misc header
|
|
|
|
llist_add_to_end(&headers, s);
|
|
|
|
} else {
|
|
|
|
free(s);
|
|
|
|
break; // stop on the first empty line
|
2008-07-18 01:07:09 +05:30
|
|
|
}
|
2008-01-29 06:29:15 +05:30
|
|
|
}
|
2008-09-28 19:20:57 +05:30
|
|
|
}
|
2008-01-29 06:29:15 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// enter "put message" mode
|
|
|
|
smtp_check("DATA", 354);
|
2008-02-27 15:09:04 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// put headers we could have preread with -t
|
|
|
|
for (l = headers; l; l = l->link) {
|
|
|
|
printf("%s\r\n", l->data);
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
free(l->data);
|
|
|
|
}
|
2008-02-27 15:09:04 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// put (possibly encoded) subject
|
2008-10-29 18:46:28 +05:30
|
|
|
#if ENABLE_FEATURE_SENDMAIL_MAILX
|
2008-09-28 19:20:57 +05:30
|
|
|
if (opts & OPT_j)
|
|
|
|
sane((char *)opt_charset);
|
|
|
|
if (opts & OPT_s) {
|
|
|
|
printf("Subject: ");
|
|
|
|
if (opts & OPT_j) {
|
|
|
|
printf("=?%s?B?", opt_charset);
|
|
|
|
uuencode(NULL, opt_subject);
|
|
|
|
printf("?=");
|
|
|
|
} else {
|
|
|
|
printf("%s", opt_subject);
|
2008-02-08 23:54:54 +05:30
|
|
|
}
|
2008-09-28 19:20:57 +05:30
|
|
|
printf("\r\n");
|
|
|
|
}
|
2008-10-29 18:46:28 +05:30
|
|
|
#endif
|
2008-02-27 15:09:04 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// put sender name, $NAME is the default
|
|
|
|
if (!(opts & OPT_F))
|
|
|
|
opt_fullname = getenv("NAME");
|
|
|
|
if (opt_fullname)
|
|
|
|
printf("From: \"%s\" <%s>\r\n", opt_fullname, opt_from);
|
2008-09-27 19:31:22 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// put notification
|
|
|
|
if (opts & OPT_N)
|
|
|
|
printf("Disposition-Notification-To: %s\r\n", opt_from);
|
2008-02-27 15:09:04 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
#if ENABLE_FEATURE_SENDMAIL_MAILXX
|
|
|
|
// put errors recipient
|
|
|
|
if (opts & OPT_e)
|
|
|
|
printf("Errors-To: %s\r\n", opt_errors_to);
|
|
|
|
#endif
|
2008-02-27 15:09:04 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// make a random string -- it will delimit message parts
|
|
|
|
srand(monotonic_us());
|
|
|
|
boundary = xasprintf("%d-%d-%d", rand(), rand(), rand());
|
2008-09-27 19:31:22 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// put common headers
|
|
|
|
// TODO: do we really need this?
|
|
|
|
// printf("Message-ID: <%s>\r\n", boundary);
|
2008-09-27 19:31:22 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
#if ENABLE_FEATURE_SENDMAIL_MAILX
|
|
|
|
// have attachments? -> compose multipart MIME
|
|
|
|
if (opt_attachments) {
|
|
|
|
const char *fmt;
|
|
|
|
const char *p;
|
|
|
|
char *q;
|
|
|
|
|
|
|
|
printf(
|
|
|
|
"Mime-Version: 1.0\r\n"
|
|
|
|
"%smultipart/mixed; boundary=\"%s\"\r\n"
|
|
|
|
, "Content-Type: "
|
|
|
|
, boundary
|
|
|
|
);
|
|
|
|
|
|
|
|
// body is pseudo attachment read from stdin in first turn
|
|
|
|
llist_add_to(&opt_attachments, (char *)"-");
|
|
|
|
|
|
|
|
// put body + attachment(s)
|
|
|
|
// N.B. all these weird things just to be tiny
|
|
|
|
// by reusing string patterns!
|
|
|
|
fmt =
|
|
|
|
"\r\n--%s\r\n"
|
|
|
|
"%stext/plain; charset=%s\r\n"
|
|
|
|
"%s%s\r\n"
|
|
|
|
"%s"
|
|
|
|
;
|
|
|
|
p = opt_charset;
|
|
|
|
q = (char *)"";
|
|
|
|
l = opt_attachments;
|
|
|
|
while (l) {
|
2008-02-08 23:54:54 +05:30
|
|
|
printf(
|
2008-09-28 19:20:57 +05:30
|
|
|
fmt
|
2008-09-27 19:31:22 +05:30
|
|
|
, boundary
|
2008-09-28 19:20:57 +05:30
|
|
|
, "Content-Type: "
|
|
|
|
, p
|
|
|
|
, "Content-Disposition: inline"
|
|
|
|
, q
|
|
|
|
, "Content-Transfer-Encoding: base64\r\n"
|
2008-02-08 23:54:54 +05:30
|
|
|
);
|
2008-09-28 19:20:57 +05:30
|
|
|
p = "";
|
2008-02-08 23:54:54 +05:30
|
|
|
fmt =
|
|
|
|
"\r\n--%s\r\n"
|
2008-09-28 19:20:57 +05:30
|
|
|
"%sapplication/octet-stream%s\r\n"
|
|
|
|
"%s; filename=\"%s\"\r\n"
|
2008-02-08 23:54:54 +05:30
|
|
|
"%s"
|
|
|
|
;
|
2008-09-28 19:20:57 +05:30
|
|
|
uuencode(l->data, NULL);
|
|
|
|
l = l->link;
|
|
|
|
if (l)
|
|
|
|
q = bb_get_last_path_component_strip(l->data);
|
|
|
|
}
|
2008-09-27 19:31:22 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// put message terminator
|
|
|
|
printf("\r\n--%s--\r\n" "\r\n", boundary);
|
2008-02-27 02:43:17 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// no attachments? -> just dump message
|
|
|
|
} else
|
2008-09-27 19:31:22 +05:30
|
|
|
#endif
|
2008-09-28 19:20:57 +05:30
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
// terminate headers
|
|
|
|
printf("\r\n");
|
|
|
|
// put plain text respecting leading dots
|
|
|
|
while ((s = xmalloc_fgetline(fp0)) != NULL) {
|
|
|
|
// escape leading dots
|
|
|
|
// N.B. this feature is implied even if no -i (-oi) switch given
|
|
|
|
// N.B. we need to escape the leading dot regardless of
|
|
|
|
// whether it is single or not character on the line
|
|
|
|
if ('.' == s[0] /*&& '\0' == s[1] */)
|
|
|
|
printf(".");
|
|
|
|
// dump read line
|
|
|
|
printf("%s\r\n", s);
|
2008-09-27 19:31:22 +05:30
|
|
|
}
|
2008-06-28 02:54:08 +05:30
|
|
|
}
|
2008-02-27 15:09:04 +05:30
|
|
|
|
2008-09-28 19:20:57 +05:30
|
|
|
// leave "put message" mode
|
|
|
|
smtp_check(".", 250);
|
|
|
|
// ... and say goodbye
|
|
|
|
smtp_check("QUIT", 221);
|
|
|
|
// cleanup
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
fclose(fp0);
|
2008-01-29 06:29:15 +05:30
|
|
|
|
2008-09-27 19:31:22 +05:30
|
|
|
return EXIT_SUCCESS;
|
2008-01-29 06:29:15 +05:30
|
|
|
}
|