[PATCH] Mail: Support SASL EXTERNAL (RFC 4422)
Rob N ★
robn at fastmail.com
Sat Oct 8 07:31:02 UTC 2016
# HG changeset patch
# User Rob N ★ <robn at fastmail.com>
# Date 1475910300 -39600
# Sat Oct 08 18:05:00 2016 +1100
# Node ID 205f2148260460379f9b0889cdb8015994028c73
# Parent 1606a817c1d48ed351f1dd7d9cb9c996e2c77b9a
Mail: Support SASL EXTERNAL (RFC 4422)
This is needed to allow TLS client certificate auth to work. With
ssl_verify_client configured, the auth daemon can choose to allow the
connection to proceed based on the certificate data.
This has been tested with Thunderbird for IMAP only. I've not yet found a
client that will do client certificate auth for POP3 or SMTP, and the method is
not really documented anywhere that I can find. That said, its simple enough
that the way I've done is probably right.
diff -r 1606a817c1d4 -r 205f21482604 src/mail/ngx_mail.h
--- a/src/mail/ngx_mail.h Fri Oct 07 16:59:14 2016 +0300
+++ b/src/mail/ngx_mail.h Sat Oct 08 18:05:00 2016 +1100
@@ -136,7 +136,8 @@
ngx_pop3_auth_login_username,
ngx_pop3_auth_login_password,
ngx_pop3_auth_plain,
- ngx_pop3_auth_cram_md5
+ ngx_pop3_auth_cram_md5,
+ ngx_pop3_auth_external
} ngx_pop3_state_e;
@@ -146,6 +147,7 @@
ngx_imap_auth_login_password,
ngx_imap_auth_plain,
ngx_imap_auth_cram_md5,
+ ngx_imap_auth_external,
ngx_imap_login,
ngx_imap_user,
ngx_imap_passwd
@@ -158,6 +160,7 @@
ngx_smtp_auth_login_password,
ngx_smtp_auth_plain,
ngx_smtp_auth_cram_md5,
+ ngx_smtp_auth_external,
ngx_smtp_helo,
ngx_smtp_helo_xclient,
ngx_smtp_helo_from,
@@ -289,14 +292,16 @@
#define NGX_MAIL_AUTH_LOGIN_USERNAME 2
#define NGX_MAIL_AUTH_APOP 3
#define NGX_MAIL_AUTH_CRAM_MD5 4
-#define NGX_MAIL_AUTH_NONE 5
+#define NGX_MAIL_AUTH_EXTERNAL 5
+#define NGX_MAIL_AUTH_NONE 6
#define NGX_MAIL_AUTH_PLAIN_ENABLED 0x0002
#define NGX_MAIL_AUTH_LOGIN_ENABLED 0x0004
#define NGX_MAIL_AUTH_APOP_ENABLED 0x0008
#define NGX_MAIL_AUTH_CRAM_MD5_ENABLED 0x0010
-#define NGX_MAIL_AUTH_NONE_ENABLED 0x0020
+#define NGX_MAIL_AUTH_EXTERNAL_ENABLED 0x0020
+#define NGX_MAIL_AUTH_NONE_ENABLED 0x0040
#define NGX_MAIL_PARSE_INVALID_COMMAND 20
@@ -381,6 +386,8 @@
ngx_int_t ngx_mail_auth_cram_md5_salt(ngx_mail_session_t *s,
ngx_connection_t *c, char *prefix, size_t len);
ngx_int_t ngx_mail_auth_cram_md5(ngx_mail_session_t *s, ngx_connection_t *c);
+ngx_int_t ngx_mail_auth_external(ngx_mail_session_t *s, ngx_connection_t *c,
+ ngx_uint_t n);
ngx_int_t ngx_mail_auth_parse(ngx_mail_session_t *s, ngx_connection_t *c);
void ngx_mail_send(ngx_event_t *wev);
diff -r 1606a817c1d4 -r 205f21482604 src/mail/ngx_mail_auth_http_module.c
--- a/src/mail/ngx_mail_auth_http_module.c Fri Oct 07 16:59:14 2016 +0300
+++ b/src/mail/ngx_mail_auth_http_module.c Sat Oct 08 18:05:00 2016 +1100
@@ -151,6 +151,7 @@
ngx_string("plain"),
ngx_string("apop"),
ngx_string("cram-md5"),
+ ngx_string("external"),
ngx_string("none")
};
diff -r 1606a817c1d4 -r 205f21482604 src/mail/ngx_mail_handler.c
--- a/src/mail/ngx_mail_handler.c Fri Oct 07 16:59:14 2016 +0300
+++ b/src/mail/ngx_mail_handler.c Sat Oct 08 18:05:00 2016 +1100
@@ -612,6 +612,40 @@
}
+ngx_int_t
+ngx_mail_auth_external(ngx_mail_session_t *s, ngx_connection_t *c,
+ ngx_uint_t n)
+{
+ ngx_str_t *arg, external;
+
+ arg = s->args.elts;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
+ "mail auth external: \"%V\"", &arg[n]);
+
+ external.data = ngx_pnalloc(c->pool, ngx_base64_decoded_length(arg[n].len));
+ if (external.data == NULL) {
+ return NGX_ERROR;
+ }
+
+ if (ngx_decode_base64(&external, &arg[n]) != NGX_OK) {
+ ngx_log_error(NGX_LOG_INFO, c->log, 0,
+ "client sent invalid base64 encoding in AUTH EXTERNAL command");
+ return NGX_MAIL_PARSE_INVALID_COMMAND;
+ }
+
+ s->login.len = external.len;
+ s->login.data = external.data;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
+ "mail auth external: \"%V\"", &s->login);
+
+ s->auth_method = NGX_MAIL_AUTH_EXTERNAL;
+
+ return NGX_DONE;
+}
+
+
void
ngx_mail_send(ngx_event_t *wev)
{
diff -r 1606a817c1d4 -r 205f21482604 src/mail/ngx_mail_imap_handler.c
--- a/src/mail/ngx_mail_imap_handler.c Fri Oct 07 16:59:14 2016 +0300
+++ b/src/mail/ngx_mail_imap_handler.c Sat Oct 08 18:05:00 2016 +1100
@@ -222,6 +222,10 @@
case ngx_imap_auth_cram_md5:
rc = ngx_mail_auth_cram_md5(s, c);
break;
+
+ case ngx_imap_auth_external:
+ rc = ngx_mail_auth_external(s, c, 0);
+ break;
}
} else if (rc == NGX_IMAP_NEXT) {
@@ -399,6 +403,13 @@
}
return NGX_ERROR;
+
+ case NGX_MAIL_AUTH_EXTERNAL:
+
+ ngx_str_set(&s->out, imap_username);
+ s->mail_state = ngx_imap_auth_external;
+
+ return NGX_OK;
}
return rc;
diff -r 1606a817c1d4 -r 205f21482604 src/mail/ngx_mail_imap_module.c
--- a/src/mail/ngx_mail_imap_module.c Fri Oct 07 16:59:14 2016 +0300
+++ b/src/mail/ngx_mail_imap_module.c Sat Oct 08 18:05:00 2016 +1100
@@ -29,6 +29,7 @@
{ ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
{ ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED },
{ ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
+ { ngx_string("external"), NGX_MAIL_AUTH_EXTERNAL_ENABLED },
{ ngx_null_string, 0 }
};
@@ -38,6 +39,7 @@
ngx_string("AUTH=LOGIN"),
ngx_null_string, /* APOP */
ngx_string("AUTH=CRAM-MD5"),
+ ngx_string("AUTH=EXTERNAL"),
ngx_null_string /* NONE */
};
@@ -179,7 +181,7 @@
}
for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
- m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
+ m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
m <<= 1, i++)
{
if (m & conf->auth_methods) {
@@ -205,7 +207,7 @@
auth = p;
for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
- m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
+ m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
m <<= 1, i++)
{
if (m & conf->auth_methods) {
diff -r 1606a817c1d4 -r 205f21482604 src/mail/ngx_mail_parse.c
--- a/src/mail/ngx_mail_parse.c Fri Oct 07 16:59:14 2016 +0300
+++ b/src/mail/ngx_mail_parse.c Sat Oct 08 18:05:00 2016 +1100
@@ -905,13 +905,27 @@
if (arg[0].len == 8) {
- if (s->args.nelts != 1) {
- return NGX_MAIL_PARSE_INVALID_COMMAND;
+ if (ngx_strncasecmp(arg[0].data, (u_char *) "CRAM-MD5", 8) == 0) {
+
+ if (s->args.nelts != 1) {
+ return NGX_MAIL_PARSE_INVALID_COMMAND;
+ }
+
+ return NGX_MAIL_AUTH_CRAM_MD5;
}
- if (ngx_strncasecmp(arg[0].data, (u_char *) "CRAM-MD5", 8) == 0) {
- return NGX_MAIL_AUTH_CRAM_MD5;
+ if (ngx_strncasecmp(arg[0].data, (u_char *) "EXTERNAL", 8) == 0) {
+
+ if (s->args.nelts == 1) {
+ return NGX_MAIL_AUTH_EXTERNAL;
+ }
+
+ if (s->args.nelts == 2) {
+ return ngx_mail_auth_external(s, c, 1);
+ }
}
+
+ return NGX_MAIL_PARSE_INVALID_COMMAND;
}
return NGX_MAIL_PARSE_INVALID_COMMAND;
diff -r 1606a817c1d4 -r 205f21482604 src/mail/ngx_mail_pop3_handler.c
--- a/src/mail/ngx_mail_pop3_handler.c Fri Oct 07 16:59:14 2016 +0300
+++ b/src/mail/ngx_mail_pop3_handler.c Sat Oct 08 18:05:00 2016 +1100
@@ -240,6 +240,10 @@
case ngx_pop3_auth_cram_md5:
rc = ngx_mail_auth_cram_md5(s, c);
break;
+
+ case ngx_pop3_auth_external:
+ rc = ngx_mail_auth_external(s, c, 0);
+ break;
}
}
@@ -494,6 +498,13 @@
}
return NGX_ERROR;
+
+ case NGX_MAIL_AUTH_EXTERNAL:
+
+ ngx_str_set(&s->out, pop3_username);
+ s->mail_state = ngx_pop3_auth_external;
+
+ return NGX_OK;
}
return rc;
diff -r 1606a817c1d4 -r 205f21482604 src/mail/ngx_mail_pop3_module.c
--- a/src/mail/ngx_mail_pop3_module.c Fri Oct 07 16:59:14 2016 +0300
+++ b/src/mail/ngx_mail_pop3_module.c Sat Oct 08 18:05:00 2016 +1100
@@ -29,23 +29,33 @@
{ ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
{ ngx_string("apop"), NGX_MAIL_AUTH_APOP_ENABLED },
{ ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
+ { ngx_string("external"), NGX_MAIL_AUTH_EXTERNAL_ENABLED },
{ ngx_null_string, 0 }
};
-static ngx_str_t ngx_mail_pop3_auth_plain_capability =
- ngx_string("+OK methods supported:" CRLF
- "LOGIN" CRLF
- "PLAIN" CRLF
- "." CRLF);
+static ngx_str_t ngx_mail_pop3_auth_methods_capabilities[] = {
+ ngx_string("LOGIN PLAIN"),
+ ngx_null_string, /* LOGIN */
+ ngx_null_string, /* APOP */
+ ngx_string("CRAM-MD5"),
+ ngx_string("EXTERNAL"),
+ ngx_null_string /* NONE */
+};
-static ngx_str_t ngx_mail_pop3_auth_cram_md5_capability =
- ngx_string("+OK methods supported:" CRLF
- "LOGIN" CRLF
- "PLAIN" CRLF
- "CRAM-MD5" CRLF
- "." CRLF);
+static ngx_str_t ngx_mail_pop3_auth_methods_authnames[] = {
+ ngx_string("LOGIN" CRLF "PLAIN" CRLF),
+ ngx_null_string, /* LOGIN */
+ ngx_null_string, /* APOP */
+ ngx_string("CRAM-MD5" CRLF),
+ ngx_string("EXTERNAL" CRLF),
+ ngx_null_string /* NONE */
+};
+
+
+static ngx_str_t ngx_mail_pop3_auth_capability =
+ ngx_string("+OK methods supported:" CRLF);
static ngx_mail_protocol_t ngx_mail_pop3_protocol = {
@@ -140,7 +150,7 @@
u_char *p;
size_t size, stls_only_size;
ngx_str_t *c, *d;
- ngx_uint_t i;
+ ngx_uint_t i, m;
ngx_conf_merge_bitmask_value(conf->auth_methods,
prev->auth_methods,
@@ -179,11 +189,15 @@
stls_only_size += c[i].len + sizeof(CRLF) - 1;
}
- if (conf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED) {
- size += sizeof("SASL LOGIN PLAIN CRAM-MD5" CRLF) - 1;
+ size += sizeof("SASL") + 1;
- } else {
- size += sizeof("SASL LOGIN PLAIN" CRLF) - 1;
+ for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
+ m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
+ m <<= 1, i++)
+ {
+ if (m & conf->auth_methods) {
+ size += 1 + ngx_mail_pop3_auth_methods_capabilities[i].len;
+ }
}
p = ngx_pnalloc(cf->pool, size);
@@ -202,15 +216,22 @@
*p++ = CR; *p++ = LF;
}
- if (conf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED) {
- p = ngx_cpymem(p, "SASL LOGIN PLAIN CRAM-MD5" CRLF,
- sizeof("SASL LOGIN PLAIN CRAM-MD5" CRLF) - 1);
+ p = ngx_cpymem(p, "SASL", sizeof("SASL") - 1);
- } else {
- p = ngx_cpymem(p, "SASL LOGIN PLAIN" CRLF,
- sizeof("SASL LOGIN PLAIN" CRLF) - 1);
+ for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
+ m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
+ m <<= 1, i++)
+ {
+ if (m & conf->auth_methods) {
+ *p++ = ' ';
+ p = ngx_cpymem(p, ngx_mail_pop3_auth_methods_capabilities[i].data,
+ ngx_mail_pop3_auth_methods_capabilities[i].len);
+ }
}
+ *p++ = CR; *p++ = LF;
+
+
*p++ = '.'; *p++ = CR; *p = LF;
@@ -231,13 +252,39 @@
*p++ = '.'; *p++ = CR; *p = LF;
- if (conf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED) {
- conf->auth_capability = ngx_mail_pop3_auth_cram_md5_capability;
+ size = ngx_mail_pop3_auth_capability.len + 3;
- } else {
- conf->auth_capability = ngx_mail_pop3_auth_plain_capability;
+ for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
+ m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
+ m <<= 1, i++)
+ {
+ if (m & conf->auth_methods) {
+ size += ngx_mail_pop3_auth_methods_authnames[i].len;
+ }
}
+ p = ngx_pnalloc(cf->pool, size);
+ if (p == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ conf->auth_capability.data = p;
+ conf->auth_capability.len = size;
+
+ p = ngx_cpymem(p, ngx_mail_pop3_auth_capability.data,
+ ngx_mail_pop3_auth_capability.len);
+
+ for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
+ m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
+ m <<= 1, i++)
+ {
+ if (m & conf->auth_methods) {
+ p = ngx_cpymem(p, ngx_mail_pop3_auth_methods_authnames[i].data,
+ ngx_mail_pop3_auth_methods_authnames[i].len);
+ }
+ }
+ *p++ = '.'; *p++ = CR; *p = LF;
+
p = ngx_pnalloc(cf->pool, stls_only_size);
if (p == NULL) {
diff -r 1606a817c1d4 -r 205f21482604 src/mail/ngx_mail_smtp_handler.c
--- a/src/mail/ngx_mail_smtp_handler.c Fri Oct 07 16:59:14 2016 +0300
+++ b/src/mail/ngx_mail_smtp_handler.c Sat Oct 08 18:05:00 2016 +1100
@@ -485,6 +485,10 @@
case ngx_smtp_auth_cram_md5:
rc = ngx_mail_auth_cram_md5(s, c);
break;
+
+ case ngx_smtp_auth_external:
+ rc = ngx_mail_auth_external(s, c, 0);
+ break;
}
}
@@ -652,6 +656,13 @@
}
return NGX_ERROR;
+
+ case NGX_MAIL_AUTH_EXTERNAL:
+
+ ngx_str_set(&s->out, smtp_username);
+ s->mail_state = ngx_smtp_auth_external;
+
+ return NGX_OK;
}
return rc;
diff -r 1606a817c1d4 -r 205f21482604 src/mail/ngx_mail_smtp_module.c
--- a/src/mail/ngx_mail_smtp_module.c Fri Oct 07 16:59:14 2016 +0300
+++ b/src/mail/ngx_mail_smtp_module.c Sat Oct 08 18:05:00 2016 +1100
@@ -21,6 +21,7 @@
{ ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
{ ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED },
{ ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
+ { ngx_string("external"), NGX_MAIL_AUTH_EXTERNAL_ENABLED },
{ ngx_string("none"), NGX_MAIL_AUTH_NONE_ENABLED },
{ ngx_null_string, 0 }
};
@@ -31,6 +32,7 @@
ngx_string("LOGIN"),
ngx_null_string, /* APOP */
ngx_string("CRAM-MD5"),
+ ngx_string("EXTERNAL"),
ngx_null_string /* NONE */
};
@@ -207,7 +209,7 @@
auth_enabled = 0;
for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
- m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
+ m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
m <<= 1, i++)
{
if (m & conf->auth_methods) {
@@ -250,7 +252,7 @@
*p++ = 'A'; *p++ = 'U'; *p++ = 'T'; *p++ = 'H';
for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
- m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
+ m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
m <<= 1, i++)
{
if (m & conf->auth_methods) {
More information about the nginx-devel
mailing list