From: Richard Russon Date: Mon, 3 Sep 2018 15:27:21 +0000 (+0100) Subject: reorganise smtp auth_plain X-Git-Tag: 2019-10-25~666^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=85c1902f251c8069f48348c650ce2e716c1acf1c;p=neomutt reorganise smtp auth_plain --- diff --git a/smtp.c b/smtp.c index 1b7b71589..8bbaed01e 100644 --- a/smtp.c +++ b/smtp.c @@ -533,6 +533,47 @@ static int smtp_auth_oauth(struct Connection *conn) return SMTP_AUTH_SUCCESS; } +/** + * smtp_auth_plain - Authenticate using plain text + * @param conn SMTP Connection + * @retval 0 Success + * @retval <0 Error, e.g. #SMTP_AUTH_FAIL + */ +static int smtp_auth_plain(struct Connection *conn) +{ + char buf[LONG_STRING]; + + /* Get username and password. Bail out of any cannot be retrieved. */ + if ((mutt_account_getuser(&conn->account) < 0) || + (mutt_account_getpass(&conn->account) < 0)) + { + goto error; + } + + /* Build the initial client response. */ + size_t len = mutt_sasl_plain_msg(buf, sizeof(buf), "AUTH PLAIN", conn->account.user, + conn->account.user, conn->account.pass); + + /* Terminate as per SMTP protocol. Bail out if there's no room left. */ + if (snprintf(buf + len, sizeof(buf) - len, "\r\n") != 2) + { + goto error; + } + + /* Send request, receive response (with a check for OK code). */ + if ((mutt_socket_send(conn, buf) < 0) || smtp_get_resp(conn)) + { + goto error; + } + + /* If we got here, auth was successful. */ + return 0; + +error: + mutt_error(_("SASL authentication failed")); + return -1; +} + /** * smtp_auth - Authenticate to an SMTP server * @param conn SMTP Connection @@ -563,6 +604,10 @@ static int smtp_auth(struct Connection *conn) { r = smtp_auth_oauth(conn); } + else if (strcmp(method, "plain") == 0) + { + r = smtp_auth_plain(conn); + } else { #ifdef USE_SASL @@ -608,68 +653,6 @@ static int smtp_auth(struct Connection *conn) return (r == SMTP_AUTH_SUCCESS) ? 0 : -1; } -#ifdef USE_SASL -/** - * smtp_auth_plain - Authenticate using plain text - * @param conn SMTP Connection - * @retval 0 Success - * @retval <0 Error, e.g. #SMTP_AUTH_FAIL - */ -static int smtp_auth_plain(struct Connection *conn) -{ - char buf[LONG_STRING]; - size_t len; - const char *method = NULL; - const char *delim = NULL; - const char *error = _("SASL authentication failed"); - - if (!SmtpAuthenticators || !*SmtpAuthenticators) - { - goto error; - } - - /* Check if any elements in SmtpAuthenticators is "plain" */ - for (method = SmtpAuthenticators, delim = SmtpAuthenticators; - *delim && (delim = mutt_str_strchrnul(method, ':')); method = delim + 1) - { - if (mutt_str_strncasecmp(method, "plain", 5) == 0) - { - /* Get username and password. Bail out of any cannot be retrieved. */ - if ((mutt_account_getuser(&conn->account) < 0) || - (mutt_account_getpass(&conn->account) < 0)) - { - goto error; - } - - /* Build the initial client response. */ - len = mutt_sasl_plain_msg(buf, sizeof(buf), "AUTH PLAIN", conn->account.user, - conn->account.user, conn->account.pass); - - /* Terminate as per SMTP protocol. Bail out if there's no room left. */ - if (snprintf(buf + len, sizeof(buf) - len, "\r\n") != 2) - { - goto error; - } - - /* Send request, receive response (with a check for OK code). */ - if ((mutt_socket_send(conn, buf) < 0) || smtp_get_resp(conn)) - { - goto error; - } - - /* If we got here, auth was successful. */ - return 0; - } - } - - error = _("No authenticators available"); - -error: - mutt_error(error); - return -1; -} -#endif /* USE_SASL */ - /** * smtp_open - Open an SMTP Connection * @param conn SMTP Connection @@ -735,9 +718,6 @@ static int smtp_open(struct Connection *conn, bool esmtp) } return smtp_auth(conn); -#ifdef USE_SASL - return smtp_auth_plain(conn); -#endif } return 0;