diff options
author | Raffaello D. Di Napoli <rafdev@dinapo.li> | 2018-06-26 19:17:45 -0400 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-07-31 17:17:40 +0200 |
commit | f28b8857a9fa7b2b137a19ce7069077da5706d78 (patch) | |
tree | a699ba6077e00505ebb041944eb005d17bd53055 | |
parent | c16ae469ef132d7250a509ef5ccf3420e2fc567f (diff) | |
download | busybox-w32-f28b8857a9fa7b2b137a19ce7069077da5706d78.tar.gz busybox-w32-f28b8857a9fa7b2b137a19ce7069077da5706d78.tar.bz2 busybox-w32-f28b8857a9fa7b2b137a19ce7069077da5706d78.zip |
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>
-rw-r--r-- | mailutils/mail.c | 18 | ||||
-rw-r--r-- | mailutils/mail.h | 1 | ||||
-rw-r--r-- | mailutils/sendmail.c | 40 |
3 files changed, 45 insertions, 14 deletions
diff --git a/mailutils/mail.c b/mailutils/mail.c index 7af7edd6c..2ad959f7d 100644 --- a/mailutils/mail.c +++ b/mailutils/mail.c | |||
@@ -109,6 +109,17 @@ static char* FAST_FUNC parse_url(char *url, char **user, char **pass) | |||
109 | 109 | ||
110 | void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol) | 110 | void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol) |
111 | { | 111 | { |
112 | size_t len = len; | ||
113 | if (text) { | ||
114 | // though we do not call uuencode(NULL, NULL) explicitly | ||
115 | // still we do not want to break things suddenly | ||
116 | len = strlen(text); | ||
117 | } | ||
118 | encode_n_base64(fname, text, len, eol); | ||
119 | } | ||
120 | |||
121 | void FAST_FUNC encode_n_base64(char *fname, const char *text, size_t len, const char *eol) | ||
122 | { | ||
112 | enum { | 123 | enum { |
113 | SRC_BUF_SIZE = 57, /* This *MUST* be a multiple of 3 */ | 124 | SRC_BUF_SIZE = 57, /* This *MUST* be a multiple of 3 */ |
114 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), | 125 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), |
@@ -116,17 +127,12 @@ void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol) | |||
116 | #define src_buf text | 127 | #define src_buf text |
117 | char src[SRC_BUF_SIZE]; | 128 | char src[SRC_BUF_SIZE]; |
118 | FILE *fp = fp; | 129 | FILE *fp = fp; |
119 | ssize_t len = len; | ||
120 | char dst_buf[DST_BUF_SIZE + 1]; | 130 | char dst_buf[DST_BUF_SIZE + 1]; |
121 | 131 | ||
122 | if (fname) { | 132 | if (fname) { |
123 | fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text; | 133 | fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text; |
124 | src_buf = src; | 134 | src_buf = src; |
125 | } else if (text) { | 135 | } else if (!text) |
126 | // though we do not call uuencode(NULL, NULL) explicitly | ||
127 | // still we do not want to break things suddenly | ||
128 | len = strlen(text); | ||
129 | } else | ||
130 | return; | 136 | return; |
131 | 137 | ||
132 | while (1) { | 138 | while (1) { |
diff --git a/mailutils/mail.h b/mailutils/mail.h index fa0c5b378..4eb2bc2c0 100644 --- a/mailutils/mail.h +++ b/mailutils/mail.h | |||
@@ -35,3 +35,4 @@ void get_cred_or_die(int fd) FAST_FUNC; | |||
35 | char *send_mail_command(const char *fmt, const char *param) FAST_FUNC; | 35 | char *send_mail_command(const char *fmt, const char *param) FAST_FUNC; |
36 | 36 | ||
37 | void encode_base64(char *fname, const char *text, const char *eol) FAST_FUNC; | 37 | void encode_base64(char *fname, const char *text, const char *eol) FAST_FUNC; |
38 | 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 0170f2870..1dbaf595c 100644 --- a/mailutils/sendmail.c +++ b/mailutils/sendmail.c | |||
@@ -36,7 +36,9 @@ | |||
36 | //usage: "\n openssl s_client -quiet -tls1 -connect smtp.gmail.com:465" | 36 | //usage: "\n openssl s_client -quiet -tls1 -connect smtp.gmail.com:465" |
37 | //usage: "\n $SMTP_ANTISPAM_DELAY: seconds to wait after helper connect" | 37 | //usage: "\n $SMTP_ANTISPAM_DELAY: seconds to wait after helper connect" |
38 | //usage: "\n -S HOST[:PORT] Server (default $SMTPHOST or 127.0.0.1)" | 38 | //usage: "\n -S HOST[:PORT] Server (default $SMTPHOST or 127.0.0.1)" |
39 | //usage: "\n -amLOGIN Log in using AUTH LOGIN (-amCRAM-MD5 not supported)" | 39 | //usage: "\n -amLOGIN Log in using AUTH LOGIN" |
40 | //usage: "\n -amPLAIN or AUTH PLAIN" | ||
41 | //usage: "\n (-amCRAM-MD5 not supported)" | ||
40 | //usage: "\n -auUSER Username for AUTH" | 42 | //usage: "\n -auUSER Username for AUTH" |
41 | //usage: "\n -apPASS Password for AUTH" | 43 | //usage: "\n -apPASS Password for AUTH" |
42 | //usage: "\n" | 44 | //usage: "\n" |
@@ -248,6 +250,10 @@ int sendmail_main(int argc UNUSED_PARAM, char **argv) | |||
248 | OPT_S = 1 << 6, // specify connection string | 250 | OPT_S = 1 << 6, // specify connection string |
249 | OPT_a = 1 << 7, // authentication tokens | 251 | OPT_a = 1 << 7, // authentication tokens |
250 | OPT_v = 1 << 8, // verbosity | 252 | OPT_v = 1 << 8, // verbosity |
253 | //--- from -am | ||
254 | OPT_am_mask = 3 << 14, // AUTH method | ||
255 | OPT_am_login = 0 << 14, // AUTH LOGIN (default) | ||
256 | OPT_am_plain = 1 << 14, // AUTH PLAIN | ||
251 | }; | 257 | }; |
252 | 258 | ||
253 | // init global variables | 259 | // init global variables |
@@ -286,9 +292,12 @@ int sendmail_main(int argc UNUSED_PARAM, char **argv) | |||
286 | G.user = xstrdup(a+1); | 292 | G.user = xstrdup(a+1); |
287 | if ('p' == a[0]) | 293 | if ('p' == a[0]) |
288 | G.pass = xstrdup(a+1); | 294 | G.pass = xstrdup(a+1); |
289 | // N.B. we support only AUTH LOGIN so far | 295 | if ('m' == a[0]) { |
290 | //if ('m' == a[0]) | 296 | if (strcasecmp("plain", a+1) == 0) |
291 | // G.method = xstrdup(a+1); | 297 | opts |= OPT_am_plain; |
298 | else if (strcasecmp("login", a+1) != 0) | ||
299 | bb_error_msg_and_die("unsupported AUTH method %s", a+1); | ||
300 | } | ||
292 | } | 301 | } |
293 | // N.B. list == NULL here | 302 | // N.B. list == NULL here |
294 | //bb_error_msg("OPT[%x] AU[%s], AP[%s], AM[%s], ARGV[%s]", opts, au, ap, am, *argv); | 303 | //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) | |||
348 | 357 | ||
349 | // perform authentication | 358 | // perform authentication |
350 | if (opts & OPT_a) { | 359 | if (opts & OPT_a) { |
351 | smtp_check("AUTH LOGIN", 334); | ||
352 | // we must read credentials unless they are given via -a[up] options | 360 | // we must read credentials unless they are given via -a[up] options |
353 | if (!G.user || !G.pass) | 361 | if (!G.user || !G.pass) |
354 | get_cred_or_die(4); | 362 | get_cred_or_die(4); |
355 | encode_base64(NULL, G.user, NULL); | 363 | if ((opts & OPT_am_mask) == OPT_am_plain) { |
356 | smtp_check("", 334); | 364 | char *plain_auth; |
357 | encode_base64(NULL, G.pass, NULL); | 365 | size_t user_len, pass_len; |
366 | user_len = strlen(G.user); | ||
367 | pass_len = strlen(G.pass); | ||
368 | smtp_check("AUTH PLAIN", 334); | ||
369 | // use \1 as placeholders for \0 (format string is NUL-terminated) | ||
370 | plain_auth = xasprintf("\1%s\1%s", G.user, G.pass); | ||
371 | // substitute placeholders | ||
372 | plain_auth[0] = '\0'; | ||
373 | plain_auth[1 + user_len] = '\0'; | ||
374 | encode_n_base64(NULL, plain_auth, 1 + user_len + 1 + pass_len, NULL); | ||
375 | free(plain_auth); | ||
376 | } else if ((opts & OPT_am_mask) == OPT_am_login) { | ||
377 | smtp_check("AUTH LOGIN", 334); | ||
378 | encode_base64(NULL, G.user, NULL); | ||
379 | smtp_check("", 334); | ||
380 | encode_base64(NULL, G.pass, NULL); | ||
381 | } | ||
358 | smtp_check("", 235); | 382 | smtp_check("", 235); |
359 | } | 383 | } |
360 | 384 | ||