aboutsummaryrefslogtreecommitdiff
path: root/mailutils/sendmail.c
diff options
context:
space:
mode:
authorRaffaello D. Di Napoli <rafdev@dinapo.li>2018-06-26 19:17:45 -0400
committerDenys Vlasenko <vda.linux@googlemail.com>2018-07-31 17:17:40 +0200
commitf28b8857a9fa7b2b137a19ce7069077da5706d78 (patch)
treea699ba6077e00505ebb041944eb005d17bd53055 /mailutils/sendmail.c
parentc16ae469ef132d7250a509ef5ccf3420e2fc567f (diff)
downloadbusybox-f28b8857a9fa7b2b137a19ce7069077da5706d78.tar.gz
sendmail: support AUTH PLAIN in addition to AUTH LOGIN
Implement the -am argument to allow choosing an AUTH method. For now only PLAIN and LOGIN are supported, but others can be added easily in the future. AUTH PLAIN required adding a new variant of encode_base64() capable of handling NUL characters in the input string; the old function is now a wrapper for the newer one. function old new delta encode_n_base64 - 236 +236 sendmail_main 1199 1380 +181 packed_usage 32873 32877 +4 encode_base64 242 36 -206 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 2/1 up/down: 421/-206) Total: 215 bytes Signed-off-by: Raffaello D. Di Napoli <rafdev@dinapo.li> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'mailutils/sendmail.c')
-rw-r--r--mailutils/sendmail.c40
1 files changed, 32 insertions, 8 deletions
diff --git a/mailutils/sendmail.c b/mailutils/sendmail.c
index 0170f2870..1dbaf595c 100644
--- a/mailutils/sendmail.c
+++ b/mailutils/sendmail.c
@@ -36,7 +36,9 @@
//usage: "\n openssl s_client -quiet -tls1 -connect smtp.gmail.com:465"
//usage: "\n $SMTP_ANTISPAM_DELAY: seconds to wait after helper connect"
//usage: "\n -S HOST[:PORT] Server (default $SMTPHOST or 127.0.0.1)"
-//usage: "\n -amLOGIN Log in using AUTH LOGIN (-amCRAM-MD5 not supported)"
+//usage: "\n -amLOGIN Log in using AUTH LOGIN"
+//usage: "\n -amPLAIN or AUTH PLAIN"
+//usage: "\n (-amCRAM-MD5 not supported)"
//usage: "\n -auUSER Username for AUTH"
//usage: "\n -apPASS Password for AUTH"
//usage: "\n"
@@ -248,6 +250,10 @@ int sendmail_main(int argc UNUSED_PARAM, char **argv)
OPT_S = 1 << 6, // specify connection string
OPT_a = 1 << 7, // authentication tokens
OPT_v = 1 << 8, // verbosity
+ //--- from -am
+ OPT_am_mask = 3 << 14, // AUTH method
+ OPT_am_login = 0 << 14, // AUTH LOGIN (default)
+ OPT_am_plain = 1 << 14, // AUTH PLAIN
};
// init global variables
@@ -286,9 +292,12 @@ int sendmail_main(int argc UNUSED_PARAM, char **argv)
G.user = xstrdup(a+1);
if ('p' == a[0])
G.pass = xstrdup(a+1);
- // N.B. we support only AUTH LOGIN so far
- //if ('m' == a[0])
- // G.method = xstrdup(a+1);
+ if ('m' == a[0]) {
+ if (strcasecmp("plain", a+1) == 0)
+ opts |= OPT_am_plain;
+ else if (strcasecmp("login", a+1) != 0)
+ bb_error_msg_and_die("unsupported AUTH method %s", a+1);
+ }
}
// N.B. list == NULL here
//bb_error_msg("OPT[%x] AU[%s], AP[%s], AM[%s], ARGV[%s]", opts, au, ap, am, *argv);
@@ -348,13 +357,28 @@ int sendmail_main(int argc UNUSED_PARAM, char **argv)
// perform authentication
if (opts & OPT_a) {
- smtp_check("AUTH LOGIN", 334);
// we must read credentials unless they are given via -a[up] options
if (!G.user || !G.pass)
get_cred_or_die(4);
- encode_base64(NULL, G.user, NULL);
- smtp_check("", 334);
- encode_base64(NULL, G.pass, NULL);
+ if ((opts & OPT_am_mask) == OPT_am_plain) {
+ char *plain_auth;
+ size_t user_len, pass_len;
+ user_len = strlen(G.user);
+ pass_len = strlen(G.pass);
+ smtp_check("AUTH PLAIN", 334);
+ // use \1 as placeholders for \0 (format string is NUL-terminated)
+ plain_auth = xasprintf("\1%s\1%s", G.user, G.pass);
+ // substitute placeholders
+ plain_auth[0] = '\0';
+ plain_auth[1 + user_len] = '\0';
+ encode_n_base64(NULL, plain_auth, 1 + user_len + 1 + pass_len, NULL);
+ free(plain_auth);
+ } else if ((opts & OPT_am_mask) == OPT_am_login) {
+ smtp_check("AUTH LOGIN", 334);
+ encode_base64(NULL, G.user, NULL);
+ smtp_check("", 334);
+ encode_base64(NULL, G.pass, NULL);
+ }
smtp_check("", 235);
}