2008-11-07 05:12:42 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* bare bones sendmail
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2008-11-07 05:12:42 +05:30
|
|
|
*/
|
2016-11-23 14:28:03 +05:30
|
|
|
//config:config SENDMAIL
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "sendmail (14 kb)"
|
2016-11-23 14:28:03 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Barebones sendmail.
|
2016-11-23 14:28:03 +05:30
|
|
|
|
|
|
|
//applet:IF_SENDMAIL(APPLET(sendmail, BB_DIR_USR_SBIN, BB_SUID_DROP))
|
2010-12-20 09:42:39 +05:30
|
|
|
|
2011-05-22 07:16:33 +05:30
|
|
|
//kbuild:lib-$(CONFIG_SENDMAIL) += sendmail.o mail.o
|
|
|
|
|
2010-12-20 09:42:39 +05:30
|
|
|
//usage:#define sendmail_trivial_usage
|
2016-07-14 22:44:54 +05:30
|
|
|
//usage: "[-tv] [-f SENDER] [-amLOGIN 4<user_pass.txt | -auUSER -apPASS]"
|
2016-07-14 23:36:44 +05:30
|
|
|
//usage: "\n [-w SECS] [-H 'PROG ARGS' | -S HOST] [RECIPIENT_EMAIL]..."
|
2010-12-20 09:42:39 +05:30
|
|
|
//usage:#define sendmail_full_usage "\n\n"
|
|
|
|
//usage: "Read email from stdin and send it\n"
|
|
|
|
//usage: "\nStandard options:"
|
|
|
|
//usage: "\n -t Read additional recipients from message body"
|
2014-02-05 19:31:39 +05:30
|
|
|
//usage: "\n -f SENDER For use in MAIL FROM:<sender>. Can be empty string"
|
|
|
|
//usage: "\n Default: -auUSER, or username of current UID"
|
2010-12-20 09:42:39 +05:30
|
|
|
//usage: "\n -o OPTIONS Various options. -oi implied, others are ignored"
|
2016-07-14 22:44:54 +05:30
|
|
|
//usage: "\n -i -oi synonym, implied and ignored"
|
2010-12-20 09:42:39 +05:30
|
|
|
//usage: "\n"
|
|
|
|
//usage: "\nBusybox specific options:"
|
|
|
|
//usage: "\n -v Verbose"
|
|
|
|
//usage: "\n -w SECS Network timeout"
|
2016-07-14 22:44:54 +05:30
|
|
|
//usage: "\n -H 'PROG ARGS' Run connection helper. Examples:"
|
|
|
|
//usage: "\n openssl s_client -quiet -tls1 -starttls smtp -connect smtp.gmail.com:25"
|
|
|
|
//usage: "\n openssl s_client -quiet -tls1 -connect smtp.gmail.com:465"
|
2016-09-07 16:46:33 +05:30
|
|
|
//usage: "\n $SMTP_ANTISPAM_DELAY: seconds to wait after helper connect"
|
2016-07-14 22:44:54 +05:30
|
|
|
//usage: "\n -S HOST[:PORT] Server (default $SMTPHOST or 127.0.0.1)"
|
2018-06-27 04:47:45 +05:30
|
|
|
//usage: "\n -amLOGIN Log in using AUTH LOGIN"
|
|
|
|
//usage: "\n -amPLAIN or AUTH PLAIN"
|
|
|
|
//usage: "\n (-amCRAM-MD5 not supported)"
|
2016-07-14 22:44:54 +05:30
|
|
|
//usage: "\n -auUSER Username for AUTH"
|
|
|
|
//usage: "\n -apPASS Password for AUTH"
|
2010-12-20 09:42:39 +05:30
|
|
|
//usage: "\n"
|
2016-07-14 22:44:54 +05:30
|
|
|
//usage: "\nIf no -a options are given, authentication is not done."
|
|
|
|
//usage: "\nIf -amLOGIN is given but no -au/-ap, user/password is read from fd #4."
|
|
|
|
//usage: "\nOther options are silently ignored; -oi is implied."
|
2010-12-20 09:42:39 +05:30
|
|
|
//usage: IF_MAKEMIME(
|
2016-07-14 22:44:54 +05:30
|
|
|
//usage: "\nUse makemime to create emails with attachments."
|
2010-12-20 09:42:39 +05:30
|
|
|
//usage: )
|
|
|
|
|
2014-02-05 19:31:39 +05:30
|
|
|
/* Currently we don't sanitize or escape user-supplied SENDER and RECIPIENT_EMAILs.
|
|
|
|
* We may need to do so. For one, '.' in usernames seems to require escaping!
|
|
|
|
*
|
|
|
|
* From http://cr.yp.to/smtp/address.html:
|
|
|
|
*
|
|
|
|
* SMTP offers three ways to encode a character inside an address:
|
|
|
|
*
|
|
|
|
* "safe": the character, if it is not <>()[].,;:@, backslash,
|
|
|
|
* double-quote, space, or an ASCII control character;
|
|
|
|
* "quoted": the character, if it is not \012, \015, backslash,
|
|
|
|
* or double-quote; or
|
|
|
|
* "slashed": backslash followed by the character.
|
|
|
|
*
|
|
|
|
* An encoded box part is either (1) a sequence of one or more slashed
|
|
|
|
* or safe characters or (2) a double quote, a sequence of zero or more
|
|
|
|
* slashed or quoted characters, and a double quote. It represents
|
|
|
|
* the concatenation of the characters encoded inside it.
|
|
|
|
*
|
|
|
|
* For example, the encoded box parts
|
|
|
|
* angels
|
|
|
|
* \a\n\g\e\l\s
|
|
|
|
* "\a\n\g\e\l\s"
|
|
|
|
* "angels"
|
|
|
|
* "ang\els"
|
|
|
|
* all represent the 6-byte string "angels", and the encoded box parts
|
|
|
|
* a\,comma
|
|
|
|
* \a\,\c\o\m\m\a
|
|
|
|
* "a,comma"
|
|
|
|
* all represent the 7-byte string "a,comma".
|
|
|
|
*
|
|
|
|
* An encoded address contains
|
|
|
|
* the byte <;
|
|
|
|
* optionally, a route followed by a colon;
|
|
|
|
* an encoded box part, the byte @, and a domain; and
|
|
|
|
* the byte >.
|
|
|
|
*
|
|
|
|
* It represents an Internet mail address, given by concatenating
|
|
|
|
* the string represented by the encoded box part, the byte @,
|
|
|
|
* and the domain. For example, the encoded addresses
|
|
|
|
* <God@heaven.af.mil>
|
|
|
|
* <\God@heaven.af.mil>
|
|
|
|
* <"God"@heaven.af.mil>
|
|
|
|
* <@gateway.af.mil,@uucp.local:"\G\o\d"@heaven.af.mil>
|
|
|
|
* all represent the Internet mail address "God@heaven.af.mil".
|
|
|
|
*/
|
|
|
|
|
2008-11-07 05:12:42 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
#include "mail.h"
|
|
|
|
|
2009-10-16 03:20:48 +05:30
|
|
|
// limit maximum allowed number of headers to prevent overflows.
|
|
|
|
// set to 0 to not limit
|
|
|
|
#define MAX_HEADERS 256
|
|
|
|
|
2010-12-20 09:42:39 +05:30
|
|
|
static void send_r_n(const char *s)
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
bb_error_msg("send:'%s'", s);
|
|
|
|
printf("%s\r\n", s);
|
|
|
|
}
|
|
|
|
|
2008-11-07 05:12:42 +05:30
|
|
|
static int smtp_checkp(const char *fmt, const char *param, int code)
|
|
|
|
{
|
|
|
|
char *answer;
|
2010-12-20 09:42:39 +05:30
|
|
|
char *msg = send_mail_command(fmt, param);
|
2008-11-07 05:12:42 +05:30
|
|
|
// read stdin
|
2010-12-20 09:42:39 +05:30
|
|
|
// if the string has a form NNN- -- read next string. E.g. EHLO response
|
2008-11-07 05:12:42 +05:30
|
|
|
// 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
|
2010-12-20 09:42:39 +05:30
|
|
|
while ((answer = xmalloc_fgetline(stdin)) != NULL) {
|
2010-12-20 16:49:46 +05:30
|
|
|
if (verbose)
|
2011-09-18 06:31:49 +05:30
|
|
|
bb_error_msg("recv:'%.*s'", (int)(strchrnul(answer, '\r') - answer), answer);
|
2008-11-07 05:12:42 +05:30
|
|
|
if (strlen(answer) <= 3 || '-' != answer[3])
|
|
|
|
break;
|
2010-12-20 09:42:39 +05:30
|
|
|
free(answer);
|
|
|
|
}
|
2008-11-07 05:12:42 +05:30
|
|
|
if (answer) {
|
|
|
|
int n = atoi(answer);
|
|
|
|
if (timeout)
|
|
|
|
alarm(0);
|
|
|
|
free(answer);
|
2011-09-18 06:31:49 +05:30
|
|
|
if (-1 == code || n == code) {
|
|
|
|
free(msg);
|
2008-11-07 05:12:42 +05:30
|
|
|
return n;
|
2011-09-18 06:31:49 +05:30
|
|
|
}
|
2008-11-07 05:12:42 +05:30
|
|
|
}
|
|
|
|
bb_error_msg_and_die("%s failed", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smtp_check(const char *fmt, int code)
|
|
|
|
{
|
|
|
|
return smtp_checkp(fmt, NULL, code);
|
|
|
|
}
|
|
|
|
|
|
|
|
// strip argument of bad chars
|
|
|
|
static char *sane_address(char *str)
|
|
|
|
{
|
2013-03-18 23:17:16 +05:30
|
|
|
char *s;
|
2013-02-25 04:15:06 +05:30
|
|
|
|
2013-03-18 23:17:16 +05:30
|
|
|
trim(str);
|
|
|
|
s = str;
|
2008-11-07 05:12:42 +05:30
|
|
|
while (*s) {
|
2017-10-05 20:48:23 +05:30
|
|
|
/* Standard allows these chars in username without quoting:
|
|
|
|
* /!#$%&'*+-=?^_`{|}~
|
|
|
|
* and allows dot (.) with some restrictions.
|
|
|
|
* I chose to only allow a saner subset.
|
|
|
|
* I propose to expand it only on user's request.
|
|
|
|
*/
|
|
|
|
if (!isalnum(*s) && !strchr("=+_-.@", *s)) {
|
2013-03-18 23:17:16 +05:30
|
|
|
bb_error_msg("bad address '%s'", str);
|
|
|
|
/* returning "": */
|
|
|
|
str[0] = '\0';
|
|
|
|
return str;
|
2008-11-07 05:12:42 +05:30
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2013-02-25 04:15:08 +05:30
|
|
|
// check for an address inside angle brackets, if not found fall back to normal
|
|
|
|
static char *angle_address(char *str)
|
|
|
|
{
|
2013-03-18 23:17:16 +05:30
|
|
|
char *s, *e;
|
|
|
|
|
2017-08-05 21:20:35 +05:30
|
|
|
e = trim(str);
|
2018-06-27 04:48:02 +05:30
|
|
|
if (e != str && *--e == '>') {
|
2013-03-18 23:17:16 +05:30
|
|
|
s = strrchr(str, '<');
|
|
|
|
if (s) {
|
|
|
|
*e = '\0';
|
|
|
|
str = s + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sane_address(str);
|
2013-02-25 04:15:08 +05:30
|
|
|
}
|
|
|
|
|
2008-11-07 05:12:42 +05:30
|
|
|
static void rcptto(const char *s)
|
|
|
|
{
|
2013-02-25 04:15:06 +05:30
|
|
|
if (!*s)
|
|
|
|
return;
|
2009-10-16 03:20:48 +05:30
|
|
|
// N.B. we don't die if recipient is rejected, for the other recipients may be accepted
|
|
|
|
if (250 != smtp_checkp("RCPT TO:<%s>", s, -1))
|
|
|
|
bb_error_msg("Bad recipient: <%s>", s);
|
2008-11-07 05:12:42 +05:30
|
|
|
}
|
|
|
|
|
2013-02-25 04:15:09 +05:30
|
|
|
// send to a list of comma separated addresses
|
2013-03-18 23:17:16 +05:30
|
|
|
static void rcptto_list(const char *list)
|
2013-02-25 04:15:09 +05:30
|
|
|
{
|
2017-08-22 19:23:16 +05:30
|
|
|
char *free_me = xstrdup(list);
|
|
|
|
char *str = free_me;
|
|
|
|
char *s = free_me;
|
2013-02-25 04:15:09 +05:30
|
|
|
char prev = 0;
|
2013-03-18 23:17:16 +05:30
|
|
|
int in_quote = 0;
|
2013-02-25 04:15:09 +05:30
|
|
|
|
2013-03-18 23:17:16 +05:30
|
|
|
while (*s) {
|
|
|
|
char ch = *s++;
|
2013-02-25 04:15:09 +05:30
|
|
|
|
|
|
|
if (ch == '"' && prev != '\\') {
|
|
|
|
in_quote = !in_quote;
|
|
|
|
} else if (!in_quote && ch == ',') {
|
2013-03-18 23:17:16 +05:30
|
|
|
s[-1] = '\0';
|
|
|
|
rcptto(angle_address(str));
|
|
|
|
str = s;
|
2013-02-25 04:15:09 +05:30
|
|
|
}
|
|
|
|
prev = ch;
|
|
|
|
}
|
|
|
|
if (prev != ',')
|
2013-03-18 23:17:16 +05:30
|
|
|
rcptto(angle_address(str));
|
2017-08-22 19:23:16 +05:30
|
|
|
free(free_me);
|
2013-02-25 04:15:09 +05:30
|
|
|
}
|
|
|
|
|
2008-11-07 05:12:42 +05:30
|
|
|
int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int sendmail_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
2016-09-07 16:46:33 +05:30
|
|
|
char *opt_connect;
|
2014-02-05 19:31:39 +05:30
|
|
|
char *opt_from = NULL;
|
2009-04-01 05:11:53 +05:30
|
|
|
char *s;
|
|
|
|
llist_t *list = NULL;
|
2012-04-28 20:34:19 +05:30
|
|
|
char *host = sane_address(safe_gethostname());
|
2009-10-16 03:20:48 +05:30
|
|
|
unsigned nheaders = 0;
|
2008-11-07 05:12:42 +05:30
|
|
|
int code;
|
2013-02-25 04:15:10 +05:30
|
|
|
enum {
|
|
|
|
HDR_OTHER = 0,
|
|
|
|
HDR_TOCC,
|
|
|
|
HDR_BCC,
|
|
|
|
} last_hdr = 0;
|
|
|
|
int check_hdr;
|
2013-02-25 04:15:11 +05:30
|
|
|
int has_to = 0;
|
2008-11-07 05:12:42 +05:30
|
|
|
|
|
|
|
enum {
|
2009-04-01 05:11:53 +05:30
|
|
|
//--- standard options
|
|
|
|
OPT_t = 1 << 0, // read message for recipients, append them to those on cmdline
|
|
|
|
OPT_f = 1 << 1, // sender address
|
|
|
|
OPT_o = 1 << 2, // various options. -oi IMPLIED! others are IGNORED!
|
2009-10-04 05:04:54 +05:30
|
|
|
OPT_i = 1 << 3, // IMPLIED!
|
2009-04-01 05:11:53 +05:30
|
|
|
//--- BB specific options
|
2009-10-04 05:04:54 +05:30
|
|
|
OPT_w = 1 << 4, // network timeout
|
|
|
|
OPT_H = 1 << 5, // use external connection helper
|
|
|
|
OPT_S = 1 << 6, // specify connection string
|
|
|
|
OPT_a = 1 << 7, // authentication tokens
|
2010-12-20 09:42:39 +05:30
|
|
|
OPT_v = 1 << 8, // verbosity
|
2018-07-31 21:06:31 +05:30
|
|
|
//--- for -amMETHOD
|
|
|
|
OPT_am_plain = 1 << 9, // AUTH PLAIN
|
2008-11-07 05:12:42 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
// init global variables
|
|
|
|
INIT_G();
|
|
|
|
|
2016-09-07 16:46:33 +05:30
|
|
|
// default HOST[:PORT] is $SMTPHOST, or localhost
|
|
|
|
opt_connect = getenv("SMTPHOST");
|
|
|
|
if (!opt_connect)
|
|
|
|
opt_connect = (char *)"127.0.0.1";
|
|
|
|
|
2008-11-07 05:12:42 +05:30
|
|
|
// save initial stdin since body is piped!
|
|
|
|
xdup2(STDIN_FILENO, 3);
|
2009-11-16 03:58:11 +05:30
|
|
|
G.fp0 = xfdopen_for_read(3);
|
2008-11-07 05:12:42 +05:30
|
|
|
|
|
|
|
// parse options
|
2009-04-01 05:11:53 +05:30
|
|
|
// N.B. since -H and -S are mutually exclusive they do not interfere in opt_connect
|
|
|
|
// -a is for ssmtp (http://downloads.openwrt.org/people/nico/man/man8/ssmtp.8.html) compatibility,
|
|
|
|
// it is still under development.
|
2017-08-09 01:25:02 +05:30
|
|
|
opts = getopt32(argv, "^"
|
|
|
|
"tf:o:iw:+H:S:a:*:v"
|
|
|
|
"\0"
|
|
|
|
// -v is a counter, -H and -S are mutually exclusive, -a is a list
|
|
|
|
"vv:H--S:S--H",
|
|
|
|
&opt_from, NULL,
|
|
|
|
&timeout, &opt_connect, &opt_connect, &list, &verbose
|
|
|
|
);
|
2008-11-07 05:12:42 +05:30
|
|
|
//argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2009-04-01 05:11:53 +05:30
|
|
|
// process -a[upm]<token> options
|
|
|
|
if ((opts & OPT_a) && !list)
|
|
|
|
bb_show_usage();
|
|
|
|
while (list) {
|
|
|
|
char *a = (char *) llist_pop(&list);
|
|
|
|
if ('u' == a[0])
|
|
|
|
G.user = xstrdup(a+1);
|
|
|
|
if ('p' == a[0])
|
|
|
|
G.pass = xstrdup(a+1);
|
2018-06-27 04:47:45 +05:30
|
|
|
if ('m' == a[0]) {
|
2018-07-31 21:06:31 +05:30
|
|
|
if ((a[1] | 0x20) == 'p') // PLAIN
|
2018-06-27 04:47:45 +05:30
|
|
|
opts |= OPT_am_plain;
|
2018-07-31 21:06:31 +05:30
|
|
|
else if ((a[1] | 0x20) == 'l') // LOGIN
|
|
|
|
; /* do nothing (this is the default) */
|
|
|
|
else
|
2018-06-27 04:47:45 +05:30
|
|
|
bb_error_msg_and_die("unsupported AUTH method %s", a+1);
|
|
|
|
}
|
2009-04-01 05:11:53 +05:30
|
|
|
}
|
|
|
|
// N.B. list == NULL here
|
2016-03-30 19:34:37 +05:30
|
|
|
//bb_error_msg("OPT[%x] AU[%s], AP[%s], AM[%s], ARGV[%s]", opts, au, ap, am, *argv);
|
2009-04-01 05:11:53 +05:30
|
|
|
|
2008-11-07 05:12:42 +05:30
|
|
|
// connect to server
|
|
|
|
|
|
|
|
// connection helper ordered? ->
|
|
|
|
if (opts & OPT_H) {
|
2016-09-07 16:46:33 +05:30
|
|
|
const char *delay;
|
2008-11-07 05:12:42 +05:30
|
|
|
const char *args[] = { "sh", "-c", opt_connect, NULL };
|
|
|
|
// plug it in
|
|
|
|
launch_helper(args);
|
2011-09-18 06:31:49 +05:30
|
|
|
// Now:
|
|
|
|
// our stdout will go to helper's stdin,
|
|
|
|
// helper's stdout will be available on our stdin.
|
|
|
|
|
|
|
|
// Wait for initial server message.
|
|
|
|
// If helper (such as openssl) invokes STARTTLS, the initial 220
|
|
|
|
// is swallowed by helper (and not repeated after TLS is initiated).
|
|
|
|
// We will send NOOP cmd to server and check the response.
|
|
|
|
// We should get 220+250 on plain connection, 250 on STARTTLSed session.
|
|
|
|
//
|
|
|
|
// The problem here is some servers delay initial 220 message,
|
|
|
|
// and consider client to be a spammer if it starts sending cmds
|
|
|
|
// before 220 reached it. The code below is unsafe in this regard:
|
|
|
|
// in non-STARTTLSed case, we potentially send NOOP before 220
|
|
|
|
// is sent by server.
|
2016-09-07 16:46:33 +05:30
|
|
|
//
|
|
|
|
// If $SMTP_ANTISPAM_DELAY is set, we pause before sending NOOP.
|
|
|
|
//
|
|
|
|
delay = getenv("SMTP_ANTISPAM_DELAY");
|
|
|
|
if (delay)
|
|
|
|
sleep(atoi(delay));
|
2011-09-18 06:31:49 +05:30
|
|
|
code = smtp_check("NOOP", -1);
|
|
|
|
if (code == 220)
|
|
|
|
// we got 220 - this is not STARTTLSed connection,
|
|
|
|
// eat 250 response to our NOOP
|
|
|
|
smtp_check(NULL, 250);
|
|
|
|
else
|
|
|
|
if (code != 250)
|
|
|
|
bb_error_msg_and_die("SMTP init failed");
|
2009-04-01 05:11:53 +05:30
|
|
|
} else {
|
2011-09-18 06:31:49 +05:30
|
|
|
// vanilla connection
|
2008-11-07 05:12:42 +05:30
|
|
|
int fd;
|
|
|
|
fd = create_and_connect_stream_or_die(opt_connect, 25);
|
|
|
|
// and make ourselves a simple IO filter
|
|
|
|
xmove_fd(fd, STDIN_FILENO);
|
|
|
|
xdup2(STDIN_FILENO, STDOUT_FILENO);
|
|
|
|
|
2011-09-18 06:31:49 +05:30
|
|
|
// Wait for initial server 220 message
|
|
|
|
smtp_check(NULL, 220);
|
|
|
|
}
|
2008-11-07 05:12:42 +05:30
|
|
|
|
|
|
|
// we should start with modern EHLO
|
2012-04-28 20:34:19 +05:30
|
|
|
if (250 != smtp_checkp("EHLO %s", host, -1))
|
|
|
|
smtp_checkp("HELO %s", host, 250);
|
2009-04-01 05:11:53 +05:30
|
|
|
|
|
|
|
// perform authentication
|
|
|
|
if (opts & OPT_a) {
|
2018-07-31 21:06:31 +05:30
|
|
|
// read credentials unless they are given via -a[up] options
|
2009-04-01 05:11:53 +05:30
|
|
|
if (!G.user || !G.pass)
|
|
|
|
get_cred_or_die(4);
|
2018-07-31 21:06:31 +05:30
|
|
|
if (opts & OPT_am_plain) {
|
2018-07-31 21:37:20 +05:30
|
|
|
// C: AUTH PLAIN
|
|
|
|
// S: 334
|
|
|
|
// C: base64encoded(auth<NUL>user<NUL>pass)
|
|
|
|
// S: 235 2.7.0 Authentication successful
|
|
|
|
//Note: a shorter format is allowed:
|
|
|
|
// C: AUTH PLAIN base64encoded(auth<NUL>user<NUL>pass)
|
|
|
|
// S: 235 2.7.0 Authentication successful
|
2018-06-27 04:47:45 +05:30
|
|
|
smtp_check("AUTH PLAIN", 334);
|
2018-07-31 21:37:20 +05:30
|
|
|
{
|
|
|
|
unsigned user_len = strlen(G.user);
|
|
|
|
unsigned pass_len = strlen(G.pass);
|
|
|
|
unsigned sz = 1 + user_len + 1 + pass_len;
|
|
|
|
char plain_auth[sz + 1];
|
|
|
|
// the format is:
|
|
|
|
// "authorization identity<NUL>username<NUL>password"
|
|
|
|
// authorization identity is empty.
|
|
|
|
plain_auth[0] = '\0';
|
|
|
|
strcpy(stpcpy(plain_auth + 1, G.user) + 1, G.pass);
|
|
|
|
printbuf_base64(plain_auth, sz);
|
|
|
|
}
|
2018-07-31 21:06:31 +05:30
|
|
|
} else {
|
2018-07-31 21:37:20 +05:30
|
|
|
// C: AUTH LOGIN
|
|
|
|
// S: 334 VXNlcm5hbWU6
|
|
|
|
// ^^^^^^^^^^^^ server says "Username:"
|
|
|
|
// C: base64encoded(user)
|
|
|
|
// S: 334 UGFzc3dvcmQ6
|
|
|
|
// ^^^^^^^^^^^^ server says "Password:"
|
|
|
|
// C: base64encoded(pass)
|
|
|
|
// S: 235 2.7.0 Authentication successful
|
2018-06-27 04:47:45 +05:30
|
|
|
smtp_check("AUTH LOGIN", 334);
|
2018-07-31 21:00:08 +05:30
|
|
|
printstr_base64(G.user);
|
2018-06-27 04:47:45 +05:30
|
|
|
smtp_check("", 334);
|
2018-07-31 21:00:08 +05:30
|
|
|
printstr_base64(G.pass);
|
2018-06-27 04:47:45 +05:30
|
|
|
}
|
2009-04-01 05:11:53 +05:30
|
|
|
smtp_check("", 235);
|
|
|
|
}
|
2008-11-07 05:12:42 +05:30
|
|
|
|
|
|
|
// set sender
|
2011-09-18 06:31:49 +05:30
|
|
|
// N.B. we have here a very loosely defined algorythm
|
2008-11-07 05:12:42 +05:30
|
|
|
// since sendmail historically offers no means to specify secrets on cmdline.
|
|
|
|
// 1) server can require no authentication ->
|
|
|
|
// we must just provide a (possibly fake) reply address.
|
|
|
|
// 2) server can require AUTH ->
|
|
|
|
// we must provide valid username and password along with a (possibly fake) reply address.
|
|
|
|
// For the sake of security username and password are to be read either from console or from a secured file.
|
|
|
|
// Since reading from console may defeat usability, the solution is either to read from a predefined
|
|
|
|
// file descriptor (e.g. 4), or again from a secured file.
|
|
|
|
|
2014-02-05 19:31:39 +05:30
|
|
|
// got no sender address? use auth name, then UID username as a last resort
|
|
|
|
if (!opt_from) {
|
2014-02-09 14:19:36 +05:30
|
|
|
opt_from = xasprintf("%s@%s",
|
|
|
|
G.user ? G.user : xuid2uname(getuid()),
|
|
|
|
xgethostbyname(host)->h_name);
|
2014-02-05 19:31:39 +05:30
|
|
|
}
|
2014-02-09 14:19:36 +05:30
|
|
|
free(host);
|
2014-02-05 19:31:39 +05:30
|
|
|
|
2009-04-01 05:11:53 +05:30
|
|
|
smtp_checkp("MAIL FROM:<%s>", opt_from, 250);
|
|
|
|
|
|
|
|
// process message
|
|
|
|
|
|
|
|
// read recipients from message and add them to those given on cmdline.
|
|
|
|
// this means we scan stdin for To:, Cc:, Bcc: lines until an empty line
|
|
|
|
// and then use the rest of stdin as message body
|
|
|
|
code = 0; // set "analyze headers" mode
|
|
|
|
while ((s = xmalloc_fgetline(G.fp0)) != NULL) {
|
2009-10-16 03:20:48 +05:30
|
|
|
dump:
|
2009-04-01 05:11:53 +05:30
|
|
|
// put message lines doubling leading dots
|
|
|
|
if (code) {
|
2008-11-07 05:12:42 +05:30
|
|
|
// 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] */)
|
2015-10-08 02:12:45 +05:30
|
|
|
bb_putchar('.');
|
2008-11-07 05:12:42 +05:30
|
|
|
// dump read line
|
2010-12-20 09:42:39 +05:30
|
|
|
send_r_n(s);
|
2009-04-01 05:11:53 +05:30
|
|
|
free(s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// analyze headers
|
|
|
|
// To: or Cc: headers add recipients
|
2013-03-18 23:17:16 +05:30
|
|
|
check_hdr = (0 == strncasecmp("To:", s, 3));
|
2013-02-25 04:15:11 +05:30
|
|
|
has_to |= check_hdr;
|
2011-11-19 02:55:35 +05:30
|
|
|
if (opts & OPT_t) {
|
2013-02-25 04:15:11 +05:30
|
|
|
if (check_hdr || 0 == strncasecmp("Bcc:" + 1, s, 3)) {
|
2013-02-25 04:15:09 +05:30
|
|
|
rcptto_list(s+3);
|
2013-02-25 04:15:10 +05:30
|
|
|
last_hdr = HDR_TOCC;
|
2011-11-19 02:55:35 +05:30
|
|
|
goto addheader;
|
|
|
|
}
|
|
|
|
// Bcc: header adds blind copy (hidden) recipient
|
|
|
|
if (0 == strncasecmp("Bcc:", s, 4)) {
|
2013-02-25 04:15:09 +05:30
|
|
|
rcptto_list(s+4);
|
2011-11-19 02:55:35 +05:30
|
|
|
free(s);
|
2013-02-25 04:15:10 +05:30
|
|
|
last_hdr = HDR_BCC;
|
2011-11-19 02:55:35 +05:30
|
|
|
continue; // N.B. Bcc: vanishes from headers!
|
|
|
|
}
|
2011-09-18 06:31:49 +05:30
|
|
|
}
|
2013-02-25 04:15:12 +05:30
|
|
|
check_hdr = (list && isspace(s[0]));
|
2013-02-25 04:15:10 +05:30
|
|
|
if (strchr(s, ':') || check_hdr) {
|
2011-09-18 06:31:49 +05:30
|
|
|
// other headers go verbatim
|
|
|
|
// N.B. RFC2822 2.2.3 "Long Header Fields" allows for headers to occupy several lines.
|
|
|
|
// Continuation is denoted by prefixing additional lines with whitespace(s).
|
|
|
|
// Thanks (stefan.seyfried at googlemail.com) for pointing this out.
|
2013-02-25 04:15:10 +05:30
|
|
|
if (check_hdr && last_hdr != HDR_OTHER) {
|
|
|
|
rcptto_list(s+1);
|
|
|
|
if (last_hdr == HDR_BCC)
|
|
|
|
continue;
|
|
|
|
// N.B. Bcc: vanishes from headers!
|
|
|
|
} else {
|
|
|
|
last_hdr = HDR_OTHER;
|
|
|
|
}
|
2009-10-16 03:20:48 +05:30
|
|
|
addheader:
|
2009-10-17 07:05:10 +05:30
|
|
|
// N.B. we allow MAX_HEADERS generic headers at most to prevent attacks
|
2009-10-16 03:20:48 +05:30
|
|
|
if (MAX_HEADERS && ++nheaders >= MAX_HEADERS)
|
|
|
|
goto bail;
|
2009-04-01 05:11:53 +05:30
|
|
|
llist_add_to_end(&list, s);
|
|
|
|
} else {
|
2011-09-18 06:31:49 +05:30
|
|
|
// a line without ":" (an empty line too, by definition) doesn't look like a valid header
|
|
|
|
// so stop "analyze headers" mode
|
2009-10-16 03:20:48 +05:30
|
|
|
reenter:
|
2009-04-01 05:11:53 +05:30
|
|
|
// put recipients specified on cmdline
|
2013-02-25 04:15:12 +05:30
|
|
|
check_hdr = 1;
|
2009-04-01 05:11:53 +05:30
|
|
|
while (*argv) {
|
2009-10-16 03:20:48 +05:30
|
|
|
char *t = sane_address(*argv);
|
|
|
|
rcptto(t);
|
|
|
|
//if (MAX_HEADERS && ++nheaders >= MAX_HEADERS)
|
|
|
|
// goto bail;
|
2013-02-25 04:15:12 +05:30
|
|
|
if (!has_to) {
|
|
|
|
const char *hdr;
|
|
|
|
|
|
|
|
if (check_hdr && argv[1])
|
|
|
|
hdr = "To: %s,";
|
|
|
|
else if (check_hdr)
|
|
|
|
hdr = "To: %s";
|
|
|
|
else if (argv[1])
|
|
|
|
hdr = "To: %s," + 3;
|
|
|
|
else
|
|
|
|
hdr = "To: %s" + 3;
|
2013-02-25 04:15:11 +05:30
|
|
|
llist_add_to_end(&list,
|
2013-02-25 04:15:12 +05:30
|
|
|
xasprintf(hdr, t));
|
|
|
|
check_hdr = 0;
|
|
|
|
}
|
2009-04-01 05:11:53 +05:30
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
// enter "put message" mode
|
2009-10-16 03:20:48 +05:30
|
|
|
// N.B. DATA fails iff no recipients were accepted (or even provided)
|
|
|
|
// in this case just bail out gracefully
|
|
|
|
if (354 != smtp_check("DATA", -1))
|
|
|
|
goto bail;
|
2009-04-01 05:11:53 +05:30
|
|
|
// dump the headers
|
|
|
|
while (list) {
|
2010-12-20 09:42:39 +05:30
|
|
|
send_r_n((char *) llist_pop(&list));
|
2009-04-01 05:11:53 +05:30
|
|
|
}
|
|
|
|
// stop analyzing headers
|
|
|
|
code++;
|
2009-10-16 03:20:48 +05:30
|
|
|
// N.B. !s means: we read nothing, and nothing to be read in the future.
|
|
|
|
// just dump empty line and break the loop
|
|
|
|
if (!s) {
|
2010-12-20 09:42:39 +05:30
|
|
|
send_r_n("");
|
2009-10-16 03:20:48 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
// go dump message body
|
|
|
|
// N.B. "s" already contains the first non-header line, so pretend we read it from input
|
|
|
|
goto dump;
|
2008-11-07 05:12:42 +05:30
|
|
|
}
|
|
|
|
}
|
2009-10-16 03:20:48 +05:30
|
|
|
// odd case: we didn't stop "analyze headers" mode -> message body is empty. Reenter the loop
|
|
|
|
// N.B. after reenter code will be > 0
|
|
|
|
if (!code)
|
|
|
|
goto reenter;
|
2008-11-07 05:12:42 +05:30
|
|
|
|
2009-04-01 05:11:53 +05:30
|
|
|
// finalize the message
|
2008-11-07 05:12:42 +05:30
|
|
|
smtp_check(".", 250);
|
2009-10-16 03:20:48 +05:30
|
|
|
bail:
|
2008-11-07 05:12:42 +05:30
|
|
|
// ... and say goodbye
|
|
|
|
smtp_check("QUIT", 221);
|
|
|
|
// cleanup
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
fclose(G.fp0);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|