X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=imap%2Fauth_sasl.c;h=b8b1e3ce64128e0d0f9d3237cc5187d0ebcf6048;hp=a76677ff750125d7a71c1c3642e95f5f9c5b7fac;hb=108f3c7ab59844591f7540347914ea57be5245e2;hpb=f404a0ca916be07049af51a3022baaaaab94def6 diff --git a/imap/auth_sasl.c b/imap/auth_sasl.c index a76677f..b8b1e3c 100644 --- a/imap/auth_sasl.c +++ b/imap/auth_sasl.c @@ -1,20 +1,11 @@ /* + * Copyright notice from original mutt: * Copyright (C) 2000-3 Brendan Cully - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. - */ + * + * This file is part of mutt-ng, see http://www.muttng.org/. + * It's licensed under the GNU General Public License, + * please see the file GPL in the top level source directory. + */ /* SASL login/authentication code */ @@ -22,47 +13,42 @@ # include "config.h" #endif +#include +#include + #include "mutt.h" #include "mutt_sasl.h" #include "imap_private.h" #include "auth.h" -#ifdef USE_SASL2 +#include +#include + #include #include -#else -#include -#include -#endif /* imap_auth_sasl: Default authenticator if available. */ -imap_auth_res_t imap_auth_sasl (IMAP_DATA* idata, const char* method) +imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method) { - sasl_conn_t* saslconn; - sasl_interact_t* interaction = NULL; + sasl_conn_t *saslconn; + sasl_interact_t *interaction = NULL; int rc, irc; char buf[HUGE_STRING]; - const char* mech; -#ifdef USE_SASL2 + const char *mech; + const char *pc = NULL; -#else - char* pc = NULL; -#endif unsigned int len, olen; unsigned char client_start; - if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) - { - dprint (1, (debugfile, - "imap_auth_sasl: Error allocating SASL connection.\n")); + if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) { + debug_print (1, ("Error allocating SASL connection.\n")); return IMAP_AUTH_FAILURE; } rc = SASL_FAIL; /* If the user hasn't specified a method, use any available */ - if (!method) - { + if (!method) { method = idata->capstr; /* hack for SASL ANONYMOUS support: @@ -74,40 +60,28 @@ imap_auth_res_t imap_auth_sasl (IMAP_DATA* idata, const char* method) return IMAP_AUTH_FAILURE; if (mutt_bit_isset (idata->capabilities, AUTH_ANON) && - (!idata->conn->account.user[0] || - !ascii_strncmp (idata->conn->account.user, "anonymous", 9))) -#ifdef USE_SASL2 - rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen, + (!idata->conn->account.user[0] || + !ascii_strncmp (idata->conn->account.user, "anonymous", 9))) + rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen, &mech); -#else - rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, NULL, &pc, &olen, - &mech); -#endif } - + if (rc != SASL_OK && rc != SASL_CONTINUE) - do - { -#ifdef USE_SASL2 + do { rc = sasl_client_start (saslconn, method, &interaction, - &pc, &olen, &mech); -#else - rc = sasl_client_start (saslconn, method, NULL, &interaction, - &pc, &olen, &mech); -#endif + &pc, &olen, &mech); if (rc == SASL_INTERACT) - mutt_sasl_interact (interaction); + mutt_sasl_interact (interaction); } while (rc == SASL_INTERACT); client_start = (olen > 0); - if (rc != SASL_OK && rc != SASL_CONTINUE) - { + if (rc != SASL_OK && rc != SASL_CONTINUE) { if (method) - dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method)); + debug_print (2, ("%s unavailable\n", method)); else - dprint (1, (debugfile, "imap_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n")); + debug_print (1, ("Failure starting authentication exchange. No shared mechanisms?\n")); /* SASL doesn't support LOGIN, so fall back */ return IMAP_AUTH_UNAVAIL; @@ -120,15 +94,13 @@ imap_auth_res_t imap_auth_sasl (IMAP_DATA* idata, const char* method) irc = IMAP_CMD_CONTINUE; /* looping protocol */ - while (rc == SASL_CONTINUE || olen > 0) - { + while (rc == SASL_CONTINUE || olen > 0) { do irc = imap_cmd_step (idata); while (irc == IMAP_CMD_CONTINUE); - if (method && irc == IMAP_CMD_NO) - { - dprint (2, (debugfile, "imap_auth_sasl: %s failed\n", method)); + if (method && irc == IMAP_CMD_NO) { + debug_print (2, ("%s failed\n", method)); sasl_dispose (&saslconn); return IMAP_AUTH_UNAVAIL; } @@ -136,27 +108,24 @@ imap_auth_res_t imap_auth_sasl (IMAP_DATA* idata, const char* method) if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO) goto bail; - if (irc == IMAP_CMD_RESPOND) - { -#ifdef USE_SASL2 - if (sasl_decode64 (idata->cmd.buf+2, strlen (idata->cmd.buf+2), buf, LONG_STRING-1, -#else - if (sasl_decode64 (idata->cmd.buf+2, strlen (idata->cmd.buf+2), buf, -#endif - &len) != SASL_OK) - { - dprint (1, (debugfile, "imap_auth_sasl: error base64-decoding server response.\n")); - goto bail; + if (irc == IMAP_CMD_RESPOND) { + if (sasl_decode64 + (idata->cmd.buf + 2, m_strlen(idata->cmd.buf + 2), buf, + LONG_STRING - 1, + &len) != SASL_OK) { + debug_print (1, ("error base64-decoding server response.\n")); + goto bail; } } - if (!client_start) - { - do - { - rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen); - if (rc == SASL_INTERACT) - mutt_sasl_interact (interaction); + /* client-start is only available with the SASL-IR extension, but + * SASL 2.1 seems to want to use it regardless, at least for DIGEST + * fast reauth. Override if the server sent an initial continuation */ + if (!client_start || buf[0]) { + do { + rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen); + if (rc == SASL_INTERACT) + mutt_sasl_interact (interaction); } while (rc == SASL_INTERACT); } @@ -164,34 +133,24 @@ imap_auth_res_t imap_auth_sasl (IMAP_DATA* idata, const char* method) client_start = 0; /* send out response, or line break if none needed */ - if (olen) - { - if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) - { - dprint (1, (debugfile, "imap_auth_sasl: error base64-encoding client response.\n")); - goto bail; + if (olen) { + if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) { + debug_print (1, ("error base64-encoding client response.\n")); + goto bail; } - - /* sasl_client_st(art|ep) allocate pc with malloc, expect me to - * free it */ -#ifndef USE_SASL2 - FREE (&pc); -#endif } - - if (irc == IMAP_CMD_RESPOND) - { - strfcpy (buf + olen, "\r\n", sizeof (buf) - olen); + + if (irc == IMAP_CMD_RESPOND) { + m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n"); mutt_socket_write (idata->conn, buf); } /* If SASL has errored out, send an abort string to the server */ - if (rc < 0) - { + if (rc < 0) { mutt_socket_write (idata->conn, "*\r\n"); - dprint (1, (debugfile, "imap_auth_sasl: sasl_client_step error %d\n",rc)); + debug_print (1, ("sasl_client_step error %d\n", rc)); } - + olen = 0; } @@ -202,15 +161,14 @@ imap_auth_res_t imap_auth_sasl (IMAP_DATA* idata, const char* method) if (rc != SASL_OK) goto bail; - if (imap_code (idata->cmd.buf)) - { + if (imap_code (idata->cmd.buf)) { mutt_sasl_setup_conn (idata->conn, saslconn); return IMAP_AUTH_SUCCESS; } - bail: +bail: mutt_error _("SASL authentication failed."); - mutt_sleep(2); + mutt_sleep (2); sasl_dispose (&saslconn); return IMAP_AUTH_FAILURE;