2 * Copyright (C) 2000-2001 Vsevolod Volkov <vvv@mutt.org.ua>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
29 #include <sasl/sasl.h>
30 #include <sasl/saslutil.h>
36 #include "mutt_sasl.h"
40 /* SASL authenticator */
41 static pop_auth_res_t pop_auth_sasl (POP_DATA *pop_data, const char *method)
43 sasl_conn_t *saslconn;
44 sasl_interact_t *interaction = NULL;
46 char buf[LONG_STRING];
47 char inbuf[LONG_STRING];
50 const char *pc = NULL;
54 unsigned int len, olen;
55 unsigned char client_start;
57 if (mutt_sasl_client_new (pop_data->conn, &saslconn) < 0)
59 dprint (1, (debugfile, "pop_auth_sasl: Error allocating SASL connection.\n"));
64 method = pop_data->auth_list;
69 rc = sasl_client_start(saslconn, method, &interaction, &pc, &olen, &mech);
71 rc = sasl_client_start (saslconn, method, NULL,
72 &interaction, &pc, &olen, &mech);
74 if (rc != SASL_INTERACT)
76 mutt_sasl_interact (interaction);
79 if (rc != SASL_OK && rc != SASL_CONTINUE)
81 dprint (1, (debugfile, "pop_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
83 /* SASL doesn't support suggested mechanisms, so fall back */
87 client_start = (olen > 0);
89 mutt_message _("Authenticating (SASL)...");
91 snprintf (buf, sizeof (buf), "AUTH %s", mech);
94 /* looping protocol */
97 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
98 mutt_socket_write (pop_data->conn, buf);
99 if (mutt_socket_readln (inbuf, sizeof (inbuf), pop_data->conn) < 0)
101 sasl_dispose (&saslconn);
102 pop_data->status = POP_DISCONNECTED;
106 if (rc != SASL_CONTINUE)
110 if (!mutt_strncmp (inbuf, "+ ", 2)
111 && sasl_decode64 (inbuf, strlen (inbuf), buf, LONG_STRING-1, &len) != SASL_OK)
113 if (!mutt_strncmp (inbuf, "+ ", 2)
114 && sasl_decode64 (inbuf, strlen (inbuf), buf, &len) != SASL_OK)
117 dprint (1, (debugfile, "pop_auth_sasl: error base64-decoding server response.\n"));
124 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
125 if (rc != SASL_INTERACT)
127 mutt_sasl_interact (interaction);
132 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
135 /* send out response, or line break if none needed */
138 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK)
140 dprint (1, (debugfile, "pop_auth_sasl: error base64-encoding client response.\n"));
144 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
155 if (!mutt_strncmp (inbuf, "+OK", 3))
157 mutt_sasl_setup_conn (pop_data->conn, saslconn);
158 return POP_A_SUCCESS;
162 sasl_dispose (&saslconn);
164 /* terminate SASL sessoin if the last responce is not +OK nor -ERR */
165 if (!mutt_strncmp (inbuf, "+ ", 2))
167 snprintf (buf, sizeof (buf), "*\r\n");
168 if (pop_query (pop_data, buf, sizeof (buf)) == -1)
172 mutt_error _("SASL authentication failed.");
175 return POP_A_FAILURE;
179 /* Get the server timestamp for APOP authentication */
180 void pop_apop_timestamp (POP_DATA *pop_data, char *buf)
184 FREE (&pop_data->timestamp);
186 if ((p1 = strchr (buf, '<')) && (p2 = strchr (p1, '>')))
189 pop_data->timestamp = safe_strdup (p1);
193 /* APOP authenticator */
194 static pop_auth_res_t pop_auth_apop (POP_DATA *pop_data, const char *method)
197 unsigned char digest[16];
199 char buf[LONG_STRING];
202 if (!pop_data->timestamp)
203 return POP_A_UNAVAIL;
205 mutt_message _("Authenticating (APOP)...");
207 /* Compute the authentication hash to send to the server */
208 MD5Init (&mdContext);
209 MD5Update (&mdContext, (unsigned char *)pop_data->timestamp,
210 strlen (pop_data->timestamp));
211 MD5Update (&mdContext, (unsigned char *)pop_data->conn->account.pass,
212 strlen (pop_data->conn->account.pass));
213 MD5Final (digest, &mdContext);
215 for (i = 0; i < sizeof (digest); i++)
216 sprintf (hash + 2 * i, "%02x", digest[i]);
218 /* Send APOP command to server */
219 snprintf (buf, sizeof (buf), "APOP %s %s\r\n", pop_data->conn->account.user, hash);
221 switch (pop_query (pop_data, buf, sizeof (buf)))
224 return POP_A_SUCCESS;
229 mutt_error _("APOP authentication failed.");
232 return POP_A_FAILURE;
235 /* USER authenticator */
236 static pop_auth_res_t pop_auth_user (POP_DATA *pop_data, const char *method)
238 char buf[LONG_STRING];
241 if (!pop_data->cmd_user)
242 return POP_A_UNAVAIL;
244 mutt_message _("Logging in...");
246 snprintf (buf, sizeof (buf), "USER %s\r\n", pop_data->conn->account.user);
247 ret = pop_query (pop_data, buf, sizeof (buf));
249 if (pop_data->cmd_user == 2)
253 pop_data->cmd_user = 1;
255 dprint (1, (debugfile, "pop_auth_user: set USER capability\n"));
260 pop_data->cmd_user = 0;
262 dprint (1, (debugfile, "pop_auth_user: unset USER capability\n"));
263 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
264 _("Command USER is not supported by server."));
270 snprintf (buf, sizeof (buf), "PASS %s\r\n", pop_data->conn->account.pass);
271 ret = pop_query_d (pop_data, buf, sizeof (buf),
273 /* don't print the password unless we're at the ungodly debugging level */
274 debuglevel < M_SOCK_LOG_FULL ? "PASS *\r\n" :
282 return POP_A_SUCCESS;
287 mutt_error ("%s %s", _("Login failed."), pop_data->err_msg);
290 return POP_A_FAILURE;
293 static pop_auth_t pop_authenticators[] = {
295 { pop_auth_sasl, NULL },
297 { pop_auth_apop, "apop" },
298 { pop_auth_user, "user" },
305 * -1 - conection lost,
307 * -3 - authentication canceled.
309 int pop_authenticate (POP_DATA* pop_data)
311 ACCOUNT *acct = &pop_data->conn->account;
312 pop_auth_t* authenticator;
317 int ret = POP_A_UNAVAIL;
319 if (mutt_account_getuser (acct) || !acct->user[0] ||
320 mutt_account_getpass (acct) || !acct->pass[0])
323 if (PopAuthenticators && *PopAuthenticators)
325 /* Try user-specified list of authentication methods */
326 methods = safe_strdup (PopAuthenticators);
331 comma = strchr (method, ':');
334 dprint (2, (debugfile, "pop_authenticate: Trying method %s\n", method));
335 authenticator = pop_authenticators;
337 while (authenticator->authenticate)
339 if (!authenticator->method ||
340 !ascii_strcasecmp (authenticator->method, method))
342 ret = authenticator->authenticate (pop_data, method);
343 if (ret == POP_A_SOCKET)
344 switch (pop_connect (pop_data))
348 ret = authenticator->authenticate (pop_data, method);
355 if (ret != POP_A_UNAVAIL)
357 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
358 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL)))
374 /* Fall back to default: any authenticator */
375 dprint (2, (debugfile, "pop_authenticate: Using any available method.\n"));
376 authenticator = pop_authenticators;
378 while (authenticator->authenticate)
380 ret = authenticator->authenticate (pop_data, authenticator->method);
381 if (ret == POP_A_SOCKET)
382 switch (pop_connect (pop_data))
386 ret = authenticator->authenticate (pop_data, authenticator->method);
393 if (ret != POP_A_UNAVAIL)
395 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
396 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL)))
411 mutt_error (_("No authenticators available"));