sendmail: update by Vladimir

This commit is contained in:
Denis Vlasenko 2008-09-28 13:50:57 +00:00
parent 6826441a02
commit 2b2d977057
4 changed files with 254 additions and 411 deletions

View File

@ -149,7 +149,7 @@ USE_FBSPLASH(APPLET(fbsplash, _BB_DIR_SBIN, _BB_SUID_NEVER))
USE_FDFLUSH(APPLET_ODDNAME(fdflush, freeramdisk, _BB_DIR_BIN, _BB_SUID_NEVER, fdflush)) USE_FDFLUSH(APPLET_ODDNAME(fdflush, freeramdisk, _BB_DIR_BIN, _BB_SUID_NEVER, fdflush))
USE_FDFORMAT(APPLET(fdformat, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) USE_FDFORMAT(APPLET(fdformat, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
USE_FDISK(APPLET(fdisk, _BB_DIR_SBIN, _BB_SUID_NEVER)) USE_FDISK(APPLET(fdisk, _BB_DIR_SBIN, _BB_SUID_NEVER))
USE_FETCHMAIL(APPLET_ODDNAME(fetchmail, sendgetmail, _BB_DIR_USR_BIN, _BB_SUID_NEVER, fetchmail)) //USE_FETCHMAIL(APPLET(fetchmail, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
USE_FEATURE_GREP_FGREP_ALIAS(APPLET_ODDNAME(fgrep, grep, _BB_DIR_BIN, _BB_SUID_NEVER, fgrep)) USE_FEATURE_GREP_FGREP_ALIAS(APPLET_ODDNAME(fgrep, grep, _BB_DIR_BIN, _BB_SUID_NEVER, fgrep))
USE_FIND(APPLET_NOEXEC(find, find, _BB_DIR_USR_BIN, _BB_SUID_NEVER, find)) USE_FIND(APPLET_NOEXEC(find, find, _BB_DIR_USR_BIN, _BB_SUID_NEVER, find))
USE_FINDFS(APPLET(findfs, _BB_DIR_SBIN, _BB_SUID_MAYBE)) USE_FINDFS(APPLET(findfs, _BB_DIR_SBIN, _BB_SUID_MAYBE))
@ -235,6 +235,7 @@ USE_LSATTR(APPLET(lsattr, _BB_DIR_BIN, _BB_SUID_NEVER))
USE_LSMOD(APPLET(lsmod, _BB_DIR_SBIN, _BB_SUID_NEVER)) USE_LSMOD(APPLET(lsmod, _BB_DIR_SBIN, _BB_SUID_NEVER))
USE_MODPROBE_SMALL(APPLET_ODDNAME(lsmod, modprobe, _BB_DIR_SBIN, _BB_SUID_NEVER, modprobe)) USE_MODPROBE_SMALL(APPLET_ODDNAME(lsmod, modprobe, _BB_DIR_SBIN, _BB_SUID_NEVER, modprobe))
USE_UNLZMA(APPLET_ODDNAME(lzmacat, unlzma, _BB_DIR_USR_BIN, _BB_SUID_NEVER, lzmacat)) USE_UNLZMA(APPLET_ODDNAME(lzmacat, unlzma, _BB_DIR_USR_BIN, _BB_SUID_NEVER, lzmacat))
USE_FEATURE_SENDMAIL_MAILX(APPLET_ODDNAME(mail, sendmail, _BB_DIR_USR_BIN, _BB_SUID_NEVER, sendmail))
USE_MAKEDEVS(APPLET(makedevs, _BB_DIR_SBIN, _BB_SUID_NEVER)) USE_MAKEDEVS(APPLET(makedevs, _BB_DIR_SBIN, _BB_SUID_NEVER))
USE_MAN(APPLET(man, _BB_DIR_SBIN, _BB_SUID_NEVER)) USE_MAN(APPLET(man, _BB_DIR_SBIN, _BB_SUID_NEVER))
USE_MATCHPATHCON(APPLET(matchpathcon, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)) USE_MATCHPATHCON(APPLET(matchpathcon, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
@ -313,7 +314,7 @@ USE_RX(APPLET(rx, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
USE_SCRIPT(APPLET(script, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) USE_SCRIPT(APPLET(script, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
USE_SED(APPLET(sed, _BB_DIR_BIN, _BB_SUID_NEVER)) USE_SED(APPLET(sed, _BB_DIR_BIN, _BB_SUID_NEVER))
USE_SELINUXENABLED(APPLET(selinuxenabled, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)) USE_SELINUXENABLED(APPLET(selinuxenabled, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
USE_SENDMAIL(APPLET_ODDNAME(sendmail, sendgetmail, _BB_DIR_USR_SBIN, _BB_SUID_NEVER, sendmail)) USE_SENDMAIL(APPLET(sendmail, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
USE_SEQ(APPLET_NOFORK(seq, seq, _BB_DIR_USR_BIN, _BB_SUID_NEVER, seq)) USE_SEQ(APPLET_NOFORK(seq, seq, _BB_DIR_USR_BIN, _BB_SUID_NEVER, seq))
USE_SESTATUS(APPLET(sestatus, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)) USE_SESTATUS(APPLET(sestatus, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
USE_SETARCH(APPLET(setarch, _BB_DIR_BIN, _BB_SUID_NEVER)) USE_SETARCH(APPLET(setarch, _BB_DIR_BIN, _BB_SUID_NEVER))

View File

@ -3468,7 +3468,7 @@
#define sendmail_trivial_usage \ #define sendmail_trivial_usage \
"[-w timeout] [-H [user:pass@]server[:port]] [-S]\n" \ "[-w timeout] [-H [user:pass@]server[:port]] [-S]\n" \
"[-N type] [-f sender] [-F fullname] " \ "[-N type] [-f sender] [-F fullname] " \
USE_FEATURE_SENDMAIL_MAILX("[-s subject] [-c charset] [-a attach]... ") "[-t] [rcpt]..." USE_FEATURE_SENDMAIL_MAILX("[-s subject] [-c cc-rcpt]... [-j charset] [-a attach]... [-e err-rcpt] ") "[-t] [rcpt]..."
#define sendmail_full_usage "\n\n" \ #define sendmail_full_usage "\n\n" \
"Send an email\n" \ "Send an email\n" \
"\nOptions:" \ "\nOptions:" \
@ -3480,9 +3480,11 @@
"\n -F fullname Sender full name. Overrides $NAME" \ "\n -F fullname Sender full name. Overrides $NAME" \
USE_FEATURE_SENDMAIL_MAILX( \ USE_FEATURE_SENDMAIL_MAILX( \
"\n -s subject Subject" \ "\n -s subject Subject" \
"\n -c charset Assume charset for body and subject (" CONFIG_FEATURE_SENDMAIL_CHARSET ")" \ "\n -c rcpt Cc: recipient. May be multiple" \
"\n -j charset Assume charset for body and subject (" CONFIG_FEATURE_SENDMAIL_CHARSET ")" \
"\n -a file File to attach. May be multiple" \ "\n -a file File to attach. May be multiple" \
) "\n -e rcpt Errors-To: recipient" \
)
"\n -t Read recipients and subject from body" \ "\n -t Read recipients and subject from body" \
"\n" \ "\n" \
"\nOther options are silently ignored; -oi is implied" \ "\nOther options are silently ignored; -oi is implied" \

View File

@ -694,6 +694,13 @@ config FEATURE_SENDMAIL_MAILX
help help
Allow to specify subject, attachments and their charset. Allow to specify subject, attachments and their charset.
config FEATURE_SENDMAIL_MAILXX
bool "Allow to specify Cc: addresses and some additional headers"
default n
depends on FEATURE_SENDMAIL_MAILX
help
Allow to specify Cc: addresses and some additional headers: Errors-To:.
config FEATURE_SENDMAIL_SSL config FEATURE_SENDMAIL_SSL
bool "Allow to communicate via SSL/TLS" bool "Allow to communicate via SSL/TLS"
default y default y

View File

@ -1,6 +1,6 @@
/* vi: set sw=4 ts=4: */ /* vi: set sw=4 ts=4: */
/* /*
* bare bones sendmail/fetchmail * bare bones sendmail
* *
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
* *
@ -14,15 +14,12 @@ struct globals {
FILE *fp0; // initial stdin FILE *fp0; // initial stdin
// arguments for SSL connection helper // arguments for SSL connection helper
const char *xargs[9]; const char *xargs[9];
// arguments for postprocess helper
const char *fargs[3];
}; };
#define G (*ptr_to_globals) #define G (*ptr_to_globals)
#define helper_pid (G.helper_pid) #define helper_pid (G.helper_pid)
#define timeout (G.timeout ) #define timeout (G.timeout )
#define fp0 (G.fp0 ) #define fp0 (G.fp0 )
#define xargs (G.xargs ) #define xargs (G.xargs )
#define fargs (G.fargs )
#define INIT_G() do { \ #define INIT_G() do { \
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
xargs[0] = "openssl"; \ xargs[0] = "openssl"; \
@ -33,13 +30,9 @@ struct globals {
xargs[5] = "-tls1"; \ xargs[5] = "-tls1"; \
xargs[6] = "-starttls"; \ xargs[6] = "-starttls"; \
xargs[7] = "smtp"; \ xargs[7] = "smtp"; \
fargs[0] = CONFIG_FEATURE_SENDMAIL_CHARSET; \
} while (0) } while (0)
#define opt_connect (xargs[4]) #define opt_connect (xargs[4])
#define opt_after_connect (xargs[5])
#define opt_charset (fargs[0])
#define opt_subject (fargs[1])
static void uuencode(char *fname, const char *text) static void uuencode(char *fname, const char *text)
{ {
@ -112,8 +105,12 @@ static void signal_handler(int signo)
// SIGCHLD. reap zombies // SIGCHLD. reap zombies
if (wait_any_nohang(&err) > 0) if (wait_any_nohang(&err) > 0)
if (WIFEXITED(err) && WEXITSTATUS(err)) if (WIFEXITED(err)) {
bb_error_msg_and_die("child exited (%d)", WEXITSTATUS(err)); // if (WEXITSTATUS(err))
bb_error_msg_and_die("child exited (%d)", WEXITSTATUS(err));
// else
// kill(0, SIGCONT);
}
#undef err #undef err
} }
@ -178,10 +175,8 @@ static int smtp_checkp(const char *fmt, const char *param, int code)
while ((answer = xmalloc_fgetline(stdin)) != NULL) while ((answer = xmalloc_fgetline(stdin)) != NULL)
if (strlen(answer) <= 3 || '-' != answer[3]) if (strlen(answer) <= 3 || '-' != answer[3])
break; break;
//bb_error_msg("FMT[%s]ANS[%s]", fmt, answer);
if (answer) { if (answer) {
int n = atoi(answer); int n = atoi(answer);
//bb_error_msg("FMT[%s]COD[%d][%d]", fmt, n, code);
alarm(0); alarm(0);
free(answer); free(answer);
if (-1 == code || n == code) if (-1 == code || n == code)
@ -211,50 +206,6 @@ static char *sane(char *str)
return str; return str;
} }
#if ENABLE_FETCHMAIL
static void pop3_checkr(const char *fmt, const char *param, char **ret)
{
const char *msg = command(fmt, param);
char *answer = xmalloc_fgetline(stdin);
if (answer && '+' == *answer) {
alarm(0);
if (ret)
*ret = answer+4; // skip "+OK "
else if (ENABLE_FEATURE_CLEAN_UP)
free(answer);
return;
}
kill_helper();
bb_error_msg_and_die("%s failed", msg);
}
static inline void pop3_check(const char *fmt, const char *param)
{
pop3_checkr(fmt, param, NULL);
}
static void pop3_message(const char *filename)
{
int fd;
char *answer;
// create and open file filename
// read stdin, copy to created file
fd = xopen(filename, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL);
while ((answer = xmalloc_fgets_str(stdin, "\r\n")) != NULL) {
char *s = answer;
if ('.' == *answer) {
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);
}
#endif
// NB: parse_url can modify url[] (despite const), but only if '@' is there // 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) static const char *parse_url(const char *url, const char **user, const char **pass)
{ {
@ -280,64 +231,61 @@ static void rcptto(const char *s)
smtp_checkp("RCPT TO:<%s>", s, 250); smtp_checkp("RCPT TO:<%s>", s, 250);
} }
int sendgetmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int sendgetmail_main(int argc UNUSED_PARAM, char **argv) int sendmail_main(int argc UNUSED_PARAM, char **argv)
{ {
#if ENABLE_FEATURE_SENDMAIL_MAILX #if ENABLE_FEATURE_SENDMAIL_MAILX
llist_t *opt_attachments = NULL; llist_t *opt_attachments = NULL;
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
#endif #endif
char *opt_from, *opt_fullname; char *opt_from, *opt_fullname;
const char *opt_user; const char *opt_user;
const char *opt_pass; const char *opt_pass;
int code;
char *boundary;
llist_t *l;
llist_t *headers = NULL;
char *domain = sane(safe_getdomainname());
unsigned opts;
enum { enum {
OPT_w = 1 << 0, // network timeout OPT_w = 1 << 0, // network timeout
OPT_H = 1 << 1, // [user:password@]server[:port] OPT_H = 1 << 1, // [user:password@]server[:port]
OPT_S = 1 << 2, // connect using openssl s_client helper OPT_S = 1 << 2, // connect using openssl s_client helper
OPT_t = 1 << 3, // read message for recipients
OPTS_t = 1 << 3, // sendmail: read message for recipients OPT_N = 1 << 4, // request notification
OPTF_t = 1 << 3, // fetchmail: use "TOP" not "RETR" OPT_f = 1 << 5, // sender address
OPT_F = 1 << 6, // sender name, overrides $NAME
OPTS_N = 1 << 4, // sendmail: request notification OPT_s = 1 << 7, // subject
OPTF_z = 1 << 4, // fetchmail: delete from server OPT_j = 1 << 8, // assumed charset
OPT_a = 1 << 9, // attachment(s)
OPTS_f = 1 << 5, // sendmail: sender address OPT_c = 1 << 10, // carbon copy
OPTS_F = 1 << 6, // sendmail: sender name, overrides $NAME OPT_e = 1 << 11, // errors-to address
OPTS_s = 1 << 7, // sendmail: subject
OPTS_c = 1 << 8, // sendmail: assumed charset
OPTS_a = 1 << 9, // sendmail: attachment(s)
}; };
const char *options;
unsigned opts;
// init global variables // init global variables
INIT_G(); INIT_G();
// parse options, different option sets for sendmail and fetchmail // save initial stdin since body is piped!
// N.B. opt_after_connect hereafter is NULL if we are called as fetchmail xdup2(STDIN_FILENO, 3);
// and is NOT NULL if we are called as sendmail fp0 = fdopen(3, "r");
if (!ENABLE_FETCHMAIL || 's' == applet_name[0]) {
// SENDMAIL // parse options
// save initial stdin since body is piped! opt_complementary = "w+:a::" USE_FEATURE_SENDMAIL_MAILXX("c::");
xdup2(STDIN_FILENO, 3); opts = getopt32(argv,
fp0 = fdopen(3, "r"); "w:H:St" "N:f:F:" USE_FEATURE_SENDMAIL_MAILX("s:j:a:") USE_FEATURE_SENDMAIL_MAILXX("c:e:")
opt_complementary = "w+:a::"; "X:V:vq:R:O:o:nmL:Iih:GC:B:b:A:" // postfix compat only, ignored
options = "w:H:St" "N:f:F:" USE_FEATURE_SENDMAIL_MAILX("s:c:a:") // r:Q:p:M:Dd are candidates from another man page. TODO?
"X:V:vq:R:O:o:nmL:Iih:GC:B:b:A:"; // postfix compat only, ignored "46E", // ssmtp introduces another quirks. TODO?: -a[upm] (user, pass, method) to be supported
} else {
// FETCHMAIL
opt_after_connect = NULL;
opt_complementary = "-1:w+";
options = "w:H:St" "z";
}
opts = getopt32(argv, options,
&timeout /* -w */, &opt_connect /* -H */, &timeout /* -w */, &opt_connect /* -H */,
NULL, &opt_from, &opt_fullname, NULL, &opt_from, &opt_fullname,
#if ENABLE_FEATURE_SENDMAIL_MAILX USE_FEATURE_SENDMAIL_MAILX(&opt_subject, &opt_charset, &opt_attachments,)
&opt_subject, &opt_charset, &opt_attachments, USE_FEATURE_SENDMAIL_MAILXX(&opt_carboncopies, &opt_errors_to,)
#endif
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
); );
//argc -= optind; //argc -= optind;
@ -354,7 +302,6 @@ int sendgetmail_main(int argc UNUSED_PARAM, char **argv)
// NB: parse_url modifies opt_connect[] ONLY if '@' is there. // 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. // Thus "127.0.0.1" won't be modified, an is ok that it is RO.
opt_connect = parse_url(opt_connect, &opt_user, &opt_pass); opt_connect = parse_url(opt_connect, &opt_user, &opt_pass);
// bb_error_msg("H[%s] U[%s] P[%s]", opt_connect, opt_user, opt_pass);
// username must be defined! // username must be defined!
if (!opt_user) { if (!opt_user) {
@ -376,347 +323,233 @@ int sendgetmail_main(int argc UNUSED_PARAM, char **argv)
xdup2(STDIN_FILENO, STDOUT_FILENO); xdup2(STDIN_FILENO, STDOUT_FILENO);
} }
// are we sendmail? // got no sender address? -> use username as a resort
if (!ENABLE_FETCHMAIL || opt_after_connect) if (!(opts & OPT_f)) {
/*************************************************** opt_from = xasprintf("%s@%s", opt_user, domain);
* SENDMAIL }
***************************************************/
{
int code;
char *boundary;
llist_t *l;
llist_t *headers = NULL;
char *domain = sane(safe_getdomainname());
// got no sender address? -> use username as a resort // introduce to server
if (!(opts & OPTS_f)) {
opt_from = xasprintf("%s@%s", opt_user, domain); // we didn't use SSL helper? ->
if (!(opts & OPT_S)) {
// ... wait for initial server OK
smtp_check(NULL, 220);
}
// 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);
} }
// authenticated OK? -> retry to set sender
// but this time die on failure!
code = 250;
}
// introduce to server // recipients specified as arguments
while (*argv) {
// we didn't use SSL helper? -> char *s = sane(*argv);
if (!(opts & OPT_S)) { // loose test on email address validity
// ... wait for initial server OK if (strchr(s, '@')) {
smtp_check(NULL, 220); rcptto(s);
llist_add_to_end(&headers, xasprintf("To: %s", s));
} }
argv++;
}
// we should start with modern EHLO #if ENABLE_FEATURE_SENDMAIL_MAILXX
if (250 != smtp_checkp("EHLO %s", domain, -1)) { // carbon copies recipients specified as -c options
smtp_checkp("HELO %s", domain, 250); 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));
} }
if (ENABLE_FEATURE_CLEAN_UP) }
free(domain); #endif
// set sender // if -t specified or no recipients specified -> read recipients from message
// NOTE: if password has not been specified // i.e. scan stdin for To:, Cc:, Bcc: lines ...
// then no authentication is possible // ... and then use the rest of stdin as message body
code = (opt_pass ? -1 : 250); // N.B. subject read from body can be further overrided with one specified on command line.
// first try softly without authentication // recipients are merged. Bcc: lines are deleted
while (250 != smtp_checkp("MAIL FROM:<%s>", opt_from, code)) { // N.B. other headers are collected and will be dumped verbatim
// MAIL FROM failed -> authentication needed if (opts & OPT_t || !headers) {
if (334 == smtp_check("AUTH LOGIN", -1)) { // fetch recipients and (optionally) subject
uuencode(NULL, opt_user); // opt_user != NULL char *s;
smtp_check("", 334); while ((s = xmalloc_fgetline(fp0)) != NULL) {
uuencode(NULL, opt_pass); if (0 == strncasecmp("To: ", s, 4) || 0 == strncasecmp("Cc: ", s, 4)) {
smtp_check("", 235); rcptto(sane(s+4));
} llist_add_to_end(&headers, s);
// authenticated OK? -> retry to set sender } else if (0 == strncasecmp("Bcc: ", s, 5)) {
// but this time die on failure! rcptto(sane(s+5));
code = 250; free(s);
} // N.B. Bcc vanishes from headers!
} else if (0 == strncmp("Subject: ", s, 9)) {
// recipients specified as arguments // we read subject -> use it verbatim unless it is specified
while (*argv) { // on command line
// loose test on email address validity if (!(opts & OPT_s))
if (strchr(sane(*argv), '@')) {
rcptto(sane(*argv));
llist_add_to_end(&headers, xasprintf("To: %s", *argv));
}
argv++;
}
// 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 & OPTS_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); llist_add_to_end(&headers, s);
} else if (0 == strncasecmp("Bcc: ", s, 5)) { else
rcptto(sane(s+5));
free(s); free(s);
// N.B. Bcc vanishes from headers! } else if (s[0]) {
} else if (0 == strncmp("Subject: ", s, 9)) { // misc header
// we read subject -> use it verbatim unless it is specified llist_add_to_end(&headers, s);
// on command line
if (!(opts & OPTS_s))
llist_add_to_end(&headers, s);
else
free(s);
} else if (s[0]) {
// misc header
llist_add_to_end(&headers, s);
} else {
free(s);
break; // stop on the first empty line
}
}
}
// enter "put message" mode
smtp_check("DATA", 354);
// 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);
}
// put (possibly encoded) subject
if (opts & OPTS_c)
sane((char *)opt_charset);
if (opts & OPTS_s) {
printf("Subject: ");
if (opts & OPTS_c) {
printf("=?%s?B?", opt_charset);
uuencode(NULL, opt_subject);
printf("?=");
} else { } else {
printf("%s", opt_subject); free(s);
break; // stop on the first empty line
} }
printf("\r\n");
} }
}
// put sender name, $NAME is the default // enter "put message" mode
if (!(opts & OPTS_F)) smtp_check("DATA", 354);
opt_fullname = getenv("NAME");
if (opt_fullname)
printf("From: \"%s\" <%s>\r\n", opt_fullname, opt_from);
// put notification // put headers we could have preread with -t
if (opts & OPTS_N) for (l = headers; l; l = l->link) {
printf("Disposition-Notification-To: %s\r\n", opt_from); printf("%s\r\n", l->data);
if (ENABLE_FEATURE_CLEAN_UP)
free(l->data);
}
// make a random string -- it will delimit message parts // put (possibly encoded) subject
srand(monotonic_us()); if (opts & OPT_j)
boundary = xasprintf("%d-%d-%d", rand(), rand(), rand()); 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);
}
printf("\r\n");
}
// put common headers // put sender name, $NAME is the default
// TODO: do we really need this? if (!(opts & OPT_F))
// printf("Message-ID: <%s>\r\n", boundary); opt_fullname = getenv("NAME");
if (opt_fullname)
printf("From: \"%s\" <%s>\r\n", opt_fullname, opt_from);
// put notification
if (opts & OPT_N)
printf("Disposition-Notification-To: %s\r\n", opt_from);
#if ENABLE_FEATURE_SENDMAIL_MAILXX
// put errors recipient
if (opts & OPT_e)
printf("Errors-To: %s\r\n", opt_errors_to);
#endif
// make a random string -- it will delimit message parts
srand(monotonic_us());
boundary = xasprintf("%d-%d-%d", rand(), rand(), rand());
// put common headers
// TODO: do we really need this?
// printf("Message-ID: <%s>\r\n", boundary);
#if ENABLE_FEATURE_SENDMAIL_MAILX #if ENABLE_FEATURE_SENDMAIL_MAILX
// have attachments? -> compose multipart MIME // have attachments? -> compose multipart MIME
if (opt_attachments) { if (opt_attachments) {
const char *fmt; const char *fmt;
const char *p; const char *p;
char *q; 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) {
printf( printf(
"Mime-Version: 1.0\r\n" fmt
"%smultipart/mixed; boundary=\"%s\"\r\n"
, "Content-Type: "
, boundary , boundary
, "Content-Type: "
, p
, "Content-Disposition: inline"
, q
, "Content-Transfer-Encoding: base64\r\n"
); );
p = "";
// 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 = fmt =
"\r\n--%s\r\n" "\r\n--%s\r\n"
"%stext/plain; charset=%s\r\n" "%sapplication/octet-stream%s\r\n"
"%s%s\r\n" "%s; filename=\"%s\"\r\n"
"%s" "%s"
; ;
p = opt_charset; uuencode(l->data, NULL);
q = (char *)""; l = l->link;
l = opt_attachments; if (l)
while (l) { q = bb_get_last_path_component_strip(l->data);
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"
;
uuencode(l->data, NULL);
l = l->link;
if (l)
q = bb_get_last_path_component_strip(l->data);
}
// put message terminator // put message terminator
printf("\r\n--%s--\r\n" "\r\n", boundary); printf("\r\n--%s--\r\n" "\r\n", boundary);
// no attachments? -> just dump message // no attachments? -> just dump message
} else } else
#endif #endif
{ {
char *s; char *s;
// terminate headers // terminate headers
printf("\r\n"); printf("\r\n");
// put plain text respecting leading dots // put plain text respecting leading dots
while ((s = xmalloc_fgetline(fp0)) != NULL) { while ((s = xmalloc_fgetline(fp0)) != NULL) {
// escape leading dots // escape leading dots
// N.B. this feature is implied even if no -i switch given // N.B. this feature is implied even if no -i (-oi) switch given
// N.B. we need to escape the leading dot regardless of // N.B. we need to escape the leading dot regardless of
// whether it is single or not character on the line // whether it is single or not character on the line
if (/*(opts & OPTS_i) && */ '.' == s[0] /*&& '\0' == s[1] */) if ('.' == s[0] /*&& '\0' == s[1] */)
printf("."); printf(".");
// dump read line // dump read line
printf("%s\r\n", s); printf("%s\r\n", s);
}
} }
// leave "put message" mode
smtp_check(".", 250);
// ... and say goodbye
smtp_check("QUIT", 221);
// cleanup
if (ENABLE_FEATURE_CLEAN_UP)
fclose(fp0);
} }
#if ENABLE_FETCHMAIL
/***************************************************
* FETCHMAIL
***************************************************/
else {
char *buf;
unsigned nmsg;
char *hostname;
pid_t pid;
// cache fetch command: // leave "put message" mode
// TOP will return only the headers smtp_check(".", 250);
// RETR will dump the whole message // ... and say goodbye
const char *retr = (opts & OPTF_t) ? "TOP %u 0" : "RETR %u"; smtp_check("QUIT", 221);
// cleanup
// goto maildir if (ENABLE_FEATURE_CLEAN_UP)
xchdir(*argv++); fclose(fp0);
// cache postprocess program
*fargs = *argv;
// authenticate
// password is mandatory
if (!opt_pass) {
bb_error_msg_and_die("no password");
}
// get server greeting
pop3_checkr(NULL, NULL, &buf);
// server supports APOP?
if ('<' == *buf) {
md5_ctx_t md5;
// yes! compose <stamp><password>
char *s = strchr(buf, '>');
if (s)
strcpy(s+1, opt_pass);
s = buf;
// get md5 sum of <stamp><password>
md5_begin(&md5);
md5_hash(s, strlen(s), &md5);
md5_end(s, &md5);
// 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;
*bin2hex((char *)md5_hex, s, 16) = '\0';
// APOP
s = xasprintf("%s %s", opt_user, md5_hex);
#undef md5_hex
pop3_check("APOP %s", s);
if (ENABLE_FEATURE_CLEAN_UP) {
free(s);
free(buf-4); // buf is "+OK " away from malloc'ed string
}
// server ignores APOP -> use simple text authentication
} else {
// USER
pop3_check("USER %s", opt_user);
// PASS
pop3_check("PASS %s", opt_pass);
}
// get mailbox statistics
pop3_checkr("STAT", NULL, &buf);
// prepare message filename suffix
hostname = safe_gethostname();
pid = getpid();
// get messages counter
// 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
// in this case the following loop simply will not be executed
nmsg = atoi(buf);
if (ENABLE_FEATURE_CLEAN_UP)
free(buf-4); // buf is "+OK " away from malloc'ed string
// loop through messages
for (; nmsg; nmsg--) {
// generate unique filename
char *filename = xasprintf("tmp/%llu.%u.%s",
monotonic_us(), (unsigned)pid, hostname);
char *target;
int rc;
// retrieve message in ./tmp/
pop3_check(retr, (const char *)(ptrdiff_t)nmsg);
pop3_message(filename);
// delete message from server
if (opts & OPTF_z)
pop3_check("DELE %u", (const char*)(ptrdiff_t)nmsg);
// run postprocessing program
if (*fargs) {
fargs[1] = filename;
rc = wait4pid(spawn((char **)fargs));
if (99 == rc)
break;
if (1 == rc)
goto skip;
}
// atomically move message to ./new/
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);
}
// Bye
pop3_check("QUIT", NULL);
}
#endif // ENABLE_FETCHMAIL
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }