Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 209 lines (176 sloc) 5.78 kB
14c2920 Imported Upstream version 1.5.18
Antonio Radici authored
1 /*
19304f7 Imported Upstream version 1.5.19
Antonio Radici authored
2 * Copyright (C) 2000-6 Brendan Cully <brendan@kublai.com>
14c2920 Imported Upstream version 1.5.18
Antonio Radici authored
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 /* SASL login/authentication code */
20
21 #if HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "mutt.h"
26 #include "mutt_sasl.h"
27 #include "imap_private.h"
28 #include "auth.h"
29
30 #include <sasl/sasl.h>
31 #include <sasl/saslutil.h>
32
33 /* imap_auth_sasl: Default authenticator if available. */
34 imap_auth_res_t imap_auth_sasl (IMAP_DATA* idata, const char* method)
35 {
36 sasl_conn_t* saslconn;
37 sasl_interact_t* interaction = NULL;
38 int rc, irc;
39 char buf[HUGE_STRING];
40 const char* mech;
41 const char *pc = NULL;
42 unsigned int len, olen;
43 unsigned char client_start;
44
45 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0)
46 {
47 dprint (1, (debugfile,
48 "imap_auth_sasl: Error allocating SASL connection.\n"));
49 return IMAP_AUTH_FAILURE;
50 }
51
52 rc = SASL_FAIL;
53
54 /* If the user hasn't specified a method, use any available */
55 if (!method)
56 {
57 method = idata->capstr;
58
59 /* hack for SASL ANONYMOUS support:
60 * 1. Fetch username. If it's "" or "anonymous" then
61 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
62 * 3. if sasl_client_start fails, fall through... */
63
64 if (mutt_account_getuser (&idata->conn->account))
65 return IMAP_AUTH_FAILURE;
66
67 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
68 (!idata->conn->account.user[0] ||
69 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
70 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
71 &mech);
72 }
73
74 if (rc != SASL_OK && rc != SASL_CONTINUE)
75 do
76 {
77 rc = sasl_client_start (saslconn, method, &interaction,
78 &pc, &olen, &mech);
79 if (rc == SASL_INTERACT)
80 mutt_sasl_interact (interaction);
81 }
82 while (rc == SASL_INTERACT);
83
84 client_start = (olen > 0);
85
86 if (rc != SASL_OK && rc != SASL_CONTINUE)
87 {
88 if (method)
89 dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method));
90 else
91 dprint (1, (debugfile, "imap_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
92 /* SASL doesn't support LOGIN, so fall back */
93
94 return IMAP_AUTH_UNAVAIL;
95 }
96
97 mutt_message (_("Authenticating (%s)..."), mech);
98
99 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
100 if (mutt_bit_isset (idata->capabilities, SASL_IR) && client_start)
101 {
102 len = mutt_strlen (buf);
103 buf[len++] = ' ';
104 if (sasl_encode64 (pc, olen, buf + len, sizeof (buf) - len, &olen) != SASL_OK)
105 {
106 dprint (1, (debugfile, "imap_auth_sasl: error base64-encoding client response.\n"));
107 goto bail;
108 }
109 client_start = olen = 0;
110 }
111 imap_cmd_start (idata, buf);
112 irc = IMAP_CMD_CONTINUE;
113
114 /* looping protocol */
115 while (rc == SASL_CONTINUE || olen > 0)
116 {
117 do
118 irc = imap_cmd_step (idata);
119 while (irc == IMAP_CMD_CONTINUE);
120
121 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
122 goto bail;
123
124 if (irc == IMAP_CMD_RESPOND)
125 {
126 /* Exchange incorrectly returns +\r\n instead of + \r\n */
127 if (idata->buf[1] == '\0')
128 {
129 buf[0] = '\0';
130 len = 0;
131 }
132 else if (sasl_decode64 (idata->buf+2, strlen (idata->buf+2), buf,
133 LONG_STRING-1, &len) != SASL_OK)
134 {
135 dprint (1, (debugfile, "imap_auth_sasl: error base64-decoding server response.\n"));
136 goto bail;
137 }
138 }
139
140 /* client-start is only available with the SASL-IR extension, but
141 * SASL 2.1 seems to want to use it regardless, at least for DIGEST
142 * fast reauth. Override if the server sent an initial continuation */
143 if (!client_start || buf[0])
144 {
145 do
146 {
147 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
148 if (rc == SASL_INTERACT)
149 mutt_sasl_interact (interaction);
150 }
151 while (rc == SASL_INTERACT);
152 }
153 else
154 client_start = 0;
155
156 /* send out response, or line break if none needed */
157 if (olen)
158 {
159 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK)
160 {
161 dprint (1, (debugfile, "imap_auth_sasl: error base64-encoding client response.\n"));
162 goto bail;
163 }
164 }
165
166 if (irc == IMAP_CMD_RESPOND)
167 {
168 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
169 mutt_socket_write (idata->conn, buf);
170 }
171
172 /* If SASL has errored out, send an abort string to the server */
173 if (rc < 0)
174 {
175 mutt_socket_write (idata->conn, "*\r\n");
176 dprint (1, (debugfile, "imap_auth_sasl: sasl_client_step error %d\n",rc));
177 }
178
179 olen = 0;
180 }
181
182 while (irc != IMAP_CMD_OK)
183 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
184 break;
185
186 if (rc != SASL_OK)
187 goto bail;
188
189 if (imap_code (idata->buf))
190 {
191 mutt_sasl_setup_conn (idata->conn, saslconn);
192 return IMAP_AUTH_SUCCESS;
193 }
194
195 bail:
196 sasl_dispose (&saslconn);
197
198 if (method)
199 {
200 dprint (2, (debugfile, "imap_auth_sasl: %s failed\n", method));
201 return IMAP_AUTH_UNAVAIL;
202 }
203
204 mutt_error _("SASL authentication failed.");
205 mutt_sleep(2);
206
207 return IMAP_AUTH_FAILURE;
208 }
Something went wrong with that request. Please try again.