[PATCH] sendmail: support AUTH PLAIN in addition to AUTH LOGIN

Raffaello D. Di Napoli rafdev at dinapo.li
Tue Jun 26 23:17:45 UTC 2018


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.

Signed-off-by: Raffaello D. Di Napoli <rafdev at dinapo.li>
---
 mailutils/mail.c     | 25 ++++++++++++++-----------
 mailutils/mail.h     |  3 +++
 mailutils/sendmail.c | 38 ++++++++++++++++++++++++++++++--------
 3 files changed, 47 insertions(+), 19 deletions(-)

diff --git a/mailutils/mail.c b/mailutils/mail.c
index 7af7edd6c..14f90857f 100644
--- a/mailutils/mail.c
+++ b/mailutils/mail.c
@@ -108,6 +108,17 @@ static char* FAST_FUNC parse_url(char *url, char **user, char **pass)
 */
 
 void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol)
+{
+	size_t len = len;
+	if (text) {
+		// though we do not call uuencode(NULL, NULL) explicitly
+		// still we do not want to break things suddenly
+		len = strlen(text);
+	}
+	encode_n_base64(fname, text, len, eol);
+}
+
+void FAST_FUNC encode_n_base64(char *fname, const char *text, size_t len, const char *eol)
 {
 	enum {
 		SRC_BUF_SIZE = 57,  /* This *MUST* be a multiple of 3 */
@@ -116,17 +127,12 @@ void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol)
 #define src_buf text
 	char src[SRC_BUF_SIZE];
 	FILE *fp = fp;
-	ssize_t len = len;
 	char dst_buf[DST_BUF_SIZE + 1];
 
 	if (fname) {
 		fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text;
 		src_buf = src;
-	} else if (text) {
-		// though we do not call uuencode(NULL, NULL) explicitly
-		// still we do not want to break things suddenly
-		len = strlen(text);
-	} else
+	} else if (!text)
 		return;
 
 	while (1) {
@@ -135,11 +141,8 @@ void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol)
 			size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
 			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;
-		}
+		} else
+			size = min(SRC_BUF_SIZE, len);
 		if (!size)
 			break;
 		// encode the buffer we just read in
diff --git a/mailutils/mail.h b/mailutils/mail.h
index fa0c5b378..be0cff4c2 100644
--- a/mailutils/mail.h
+++ b/mailutils/mail.h
@@ -7,6 +7,8 @@
  * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
+#include <stddef.h>
+
 struct globals {
 	pid_t helper_pid;
 	unsigned timeout;
@@ -35,3 +37,4 @@ void get_cred_or_die(int fd) FAST_FUNC;
 char *send_mail_command(const char *fmt, const char *param) FAST_FUNC;
 
 void encode_base64(char *fname, const char *text, const char *eol) FAST_FUNC;
+void encode_n_base64(char *fname, const char *text, size_t size, const char *eol) FAST_FUNC;
diff --git a/mailutils/sendmail.c b/mailutils/sendmail.c
index 4ca91fad8..804e53a33 100644
--- a/mailutils/sendmail.c
+++ b/mailutils/sendmail.c
@@ -248,6 +248,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
@@ -284,11 +288,14 @@ int sendmail_main(int argc UNUSED_PARAM, char **argv)
 		char *a = (char *) llist_pop(&list);
 		if ('u' == a[0])
 			G.user = xstrdup(a+1);
-		if ('p' == a[0])
+		else 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);
+		else 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 +355,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);
 	}
 
-- 
2.16.4



More information about the busybox mailing list