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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 #include <sasl/sasl.h>
33 #include <sasl/saslutil.h>
35 #include "mutt_sasl.h"
39 /* SASL authenticator */
40 static pop_auth_res_t pop_auth_sasl (POP_DATA *pop_data, const char *method)
42 sasl_conn_t *saslconn;
43 sasl_interact_t *interaction = NULL;
45 char buf[LONG_STRING];
46 char inbuf[LONG_STRING];
48 const char *pc = NULL;
49 unsigned int len, olen, client_start;
51 if (mutt_sasl_client_new (pop_data->conn, &saslconn) < 0)
53 dprint (1, (debugfile, "pop_auth_sasl: Error allocating SASL connection.\n"));
58 method = pop_data->auth_list;
62 rc = sasl_client_start(saslconn, method, &interaction, &pc, &olen, &mech);
63 if (rc != SASL_INTERACT)
65 mutt_sasl_interact (interaction);
68 if (rc != SASL_OK && rc != SASL_CONTINUE)
70 dprint (1, (debugfile, "pop_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
72 /* SASL doesn't support suggested mechanisms, so fall back */
78 mutt_message _("Authenticating (SASL)...");
80 snprintf (buf, sizeof (buf), "AUTH %s", mech);
83 /* looping protocol */
86 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
87 mutt_socket_write (pop_data->conn, buf);
88 if (mutt_socket_readln (inbuf, sizeof (inbuf), pop_data->conn) < 0)
90 sasl_dispose (&saslconn);
91 pop_data->status = POP_DISCONNECTED;
95 if (!client_start && rc != SASL_CONTINUE)
98 if (!mutt_strncmp (inbuf, "+ ", 2)
99 && sasl_decode64 (inbuf+2, strlen (inbuf+2), buf, LONG_STRING-1, &len) != SASL_OK)
101 dprint (1, (debugfile, "pop_auth_sasl: error base64-decoding server response.\n"));
108 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
109 if (rc != SASL_INTERACT)
111 mutt_sasl_interact (interaction);
119 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
122 /* send out response, or line break if none needed */
125 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK)
127 dprint (1, (debugfile, "pop_auth_sasl: error base64-encoding client response.\n"));
136 if (!mutt_strncmp (inbuf, "+OK", 3))
138 mutt_sasl_setup_conn (pop_data->conn, saslconn);
139 return POP_A_SUCCESS;
143 sasl_dispose (&saslconn);
145 /* terminate SASL sessoin if the last responce is not +OK nor -ERR */
146 if (!mutt_strncmp (inbuf, "+ ", 2))
148 snprintf (buf, sizeof (buf), "*\r\n");
149 if (pop_query (pop_data, buf, sizeof (buf)) == -1)
153 mutt_error _("SASL authentication failed.");
156 return POP_A_FAILURE;
160 /* Get the server timestamp for APOP authentication */
161 void pop_apop_timestamp (POP_DATA *pop_data, char *buf)
165 FREE (&pop_data->timestamp);
167 if ((p1 = strchr (buf, '<')) && (p2 = strchr (p1, '>')))
170 pop_data->timestamp = safe_strdup (p1);
174 /* APOP authenticator */
175 static pop_auth_res_t pop_auth_apop (POP_DATA *pop_data, const char *method)
178 unsigned char digest[16];
180 char buf[LONG_STRING];
183 if (!pop_data->timestamp)
184 return POP_A_UNAVAIL;
186 if (rfc822_valid_msgid (pop_data->timestamp) < 0)
188 mutt_error _("POP timestamp is invalid!");
190 return POP_A_UNAVAIL;
193 mutt_message _("Authenticating (APOP)...");
195 /* Compute the authentication hash to send to the server */
197 md5_process_bytes (pop_data->timestamp, strlen (pop_data->timestamp), &ctx);
198 md5_process_bytes (pop_data->conn->account.pass,
199 strlen (pop_data->conn->account.pass), &ctx);
200 md5_finish_ctx (&ctx, digest);
202 for (i = 0; i < sizeof (digest); i++)
203 sprintf (hash + 2 * i, "%02x", digest[i]);
205 /* Send APOP command to server */
206 snprintf (buf, sizeof (buf), "APOP %s %s\r\n", pop_data->conn->account.user, hash);
208 switch (pop_query (pop_data, buf, sizeof (buf)))
211 return POP_A_SUCCESS;
216 mutt_error _("APOP authentication failed.");
219 return POP_A_FAILURE;
222 /* USER authenticator */
223 static pop_auth_res_t pop_auth_user (POP_DATA *pop_data, const char *method)
225 char buf[LONG_STRING];
228 if (!pop_data->cmd_user)
229 return POP_A_UNAVAIL;
231 mutt_message _("Logging in...");
233 snprintf (buf, sizeof (buf), "USER %s\r\n", pop_data->conn->account.user);
234 ret = pop_query (pop_data, buf, sizeof (buf));
236 if (pop_data->cmd_user == 2)
240 pop_data->cmd_user = 1;
242 dprint (1, (debugfile, "pop_auth_user: set USER capability\n"));
247 pop_data->cmd_user = 0;
249 dprint (1, (debugfile, "pop_auth_user: unset USER capability\n"));
250 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
251 _("Command USER is not supported by server."));
257 snprintf (buf, sizeof (buf), "PASS %s\r\n", pop_data->conn->account.pass);
258 ret = pop_query_d (pop_data, buf, sizeof (buf),
260 /* don't print the password unless we're at the ungodly debugging level */
261 debuglevel < M_SOCK_LOG_FULL ? "PASS *\r\n" :
269 return POP_A_SUCCESS;
274 mutt_error ("%s %s", _("Login failed."), pop_data->err_msg);
277 return POP_A_FAILURE;
280 static pop_auth_t pop_authenticators[] = {
282 { pop_auth_sasl, NULL },
284 { pop_auth_apop, "apop" },
285 { pop_auth_user, "user" },
292 * -1 - conection lost,
294 * -3 - authentication canceled.
296 int pop_authenticate (POP_DATA* pop_data)
298 ACCOUNT *acct = &pop_data->conn->account;
299 pop_auth_t* authenticator;
304 int ret = POP_A_UNAVAIL;
306 if (mutt_account_getuser (acct) || !acct->user[0] ||
307 mutt_account_getpass (acct) || !acct->pass[0])
310 if (PopAuthenticators && *PopAuthenticators)
312 /* Try user-specified list of authentication methods */
313 methods = safe_strdup (PopAuthenticators);
318 comma = strchr (method, ':');
321 dprint (2, (debugfile, "pop_authenticate: Trying method %s\n", method));
322 authenticator = pop_authenticators;
324 while (authenticator->authenticate)
326 if (!authenticator->method ||
327 !ascii_strcasecmp (authenticator->method, method))
329 ret = authenticator->authenticate (pop_data, method);
330 if (ret == POP_A_SOCKET)
331 switch (pop_connect (pop_data))
335 ret = authenticator->authenticate (pop_data, method);
342 if (ret != POP_A_UNAVAIL)
344 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
345 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL)))
361 /* Fall back to default: any authenticator */
362 dprint (2, (debugfile, "pop_authenticate: Using any available method.\n"));
363 authenticator = pop_authenticators;
365 while (authenticator->authenticate)
367 ret = authenticator->authenticate (pop_data, authenticator->method);
368 if (ret == POP_A_SOCKET)
369 switch (pop_connect (pop_data))
373 ret = authenticator->authenticate (pop_data, authenticator->method);
380 if (ret != POP_A_UNAVAIL)
382 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
383 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL)))
398 mutt_error (_("No authenticators available"));