2 * Copyright notice from original mutt:
3 * Copyright (C) 2000-2001 Vsevolod Volkov <vvv@mutt.org.ua>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
27 #include <sasl/sasl.h>
28 #include <sasl/saslutil.h>
34 #include "mutt_sasl.h"
38 /* SASL authenticator */
39 static pop_auth_res_t pop_auth_sasl (POP_DATA * pop_data, const char *method)
41 sasl_conn_t *saslconn;
42 sasl_interact_t *interaction = NULL;
44 char buf[LONG_STRING];
45 char inbuf[LONG_STRING];
49 const char *pc = NULL;
53 unsigned int len, olen;
54 unsigned char client_start;
56 if (mutt_sasl_client_new (pop_data->conn, &saslconn) < 0) {
59 "pop_auth_sasl: Error allocating SASL connection.\n"));
64 method = pop_data->auth_list;
69 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) {
82 "pop_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
84 /* SASL doesn't support suggested mechanisms, so fall back */
88 client_start = (olen > 0);
90 mutt_message _("Authenticating (SASL)...");
92 snprintf (buf, sizeof (buf), "AUTH %s", mech);
95 /* 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) {
100 sasl_dispose (&saslconn);
101 pop_data->status = POP_DISCONNECTED;
105 if (rc != SASL_CONTINUE)
109 if (!mutt_strncmp (inbuf, "+ ", 2)
110 && sasl_decode64 (inbuf, strlen (inbuf), buf, LONG_STRING - 1,
113 if (!mutt_strncmp (inbuf, "+ ", 2)
114 && sasl_decode64 (inbuf, strlen (inbuf), buf, &len) != SASL_OK)
119 "pop_auth_sasl: error base64-decoding server response.\n"));
125 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
126 if (rc != SASL_INTERACT)
128 mutt_sasl_interact (interaction);
133 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
136 /* send out response, or line break if none needed */
138 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
141 "pop_auth_sasl: error base64-encoding client response.\n"));
145 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
156 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)) {
166 snprintf (buf, sizeof (buf), "*\r\n");
167 if (pop_query (pop_data, buf, sizeof (buf)) == PQ_NOT_CONNECTED)
171 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, '>'))) {
188 pop_data->timestamp = safe_strdup (p1);
192 /* APOP authenticator */
193 static pop_auth_res_t pop_auth_apop (POP_DATA * pop_data, const char *method)
196 unsigned char digest[16];
198 char buf[LONG_STRING];
201 if (!pop_data->timestamp)
202 return POP_A_UNAVAIL;
204 mutt_message _("Authenticating (APOP)...");
206 /* Compute the authentication hash to send to the server */
207 MD5Init (&mdContext);
208 MD5Update (&mdContext, (unsigned char *) pop_data->timestamp,
209 strlen (pop_data->timestamp));
210 MD5Update (&mdContext, (unsigned char *) pop_data->conn->account.pass,
211 strlen (pop_data->conn->account.pass));
212 MD5Final (digest, &mdContext);
214 for (i = 0; i < sizeof (digest); i++)
215 sprintf (hash + 2 * i, "%02x", digest[i]);
217 /* Send APOP command to server */
218 snprintf (buf, sizeof (buf), "APOP %s %s\r\n", pop_data->conn->account.user,
221 switch (pop_query (pop_data, buf, sizeof (buf))) {
223 return POP_A_SUCCESS;
224 case PQ_NOT_CONNECTED:
228 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];
239 pop_query_status ret;
241 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
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 == CMD_UNKNOWN) {
251 pop_data->cmd_user = CMD_AVAILABLE;
253 dprint (1, (debugfile, "pop_auth_user: set USER capability\n"));
257 pop_data->cmd_user = CMD_NOT_AVAILABLE;
259 dprint (1, (debugfile, "pop_auth_user: unset USER capability\n"));
260 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
261 _("Command USER is not supported by server."));
266 snprintf (buf, sizeof (buf), "PASS %s\r\n", pop_data->conn->account.pass);
267 ret = pop_query_d (pop_data, buf, sizeof (buf),
269 /* don't print the password unless we're at the ungodly debugging level */
270 debuglevel < M_SOCK_LOG_FULL ? "PASS *\r\n" :
277 return POP_A_SUCCESS;
278 case PQ_NOT_CONNECTED:
282 mutt_error ("%s %s", _("Login failed."), pop_data->err_msg);
285 return POP_A_FAILURE;
288 static pop_auth_t pop_authenticators[] = {
290 {pop_auth_sasl, NULL},
292 {pop_auth_apop, "apop"},
293 {pop_auth_user, "user"},
300 * -1 - conection lost,
302 * -3 - authentication canceled.
304 pop_query_status pop_authenticate (POP_DATA * pop_data)
306 ACCOUNT *acct = &pop_data->conn->account;
307 pop_auth_t *authenticator;
312 int ret = POP_A_UNAVAIL;
314 if (mutt_account_getuser (acct) || !acct->user[0] ||
315 mutt_account_getpass (acct) || !acct->pass[0])
316 return PFD_FUNCT_ERROR;
318 if (PopAuthenticators && *PopAuthenticators) {
319 /* Try user-specified list of authentication methods */
320 methods = safe_strdup (PopAuthenticators);
324 comma = strchr (method, ':');
327 dprint (2, (debugfile, "pop_authenticate: Trying method %s\n", method));
328 authenticator = pop_authenticators;
330 while (authenticator->authenticate) {
331 if (!authenticator->method ||
332 !ascii_strcasecmp (authenticator->method, method)) {
333 ret = authenticator->authenticate (pop_data, method);
334 if (ret == POP_A_SOCKET)
335 switch (pop_connect (pop_data)) {
338 ret = authenticator->authenticate (pop_data, method);
345 if (ret != POP_A_UNAVAIL)
347 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
348 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL))) {
362 /* Fall back to default: any authenticator */
364 (debugfile, "pop_authenticate: Using any available method.\n"));
365 authenticator = pop_authenticators;
367 while (authenticator->authenticate) {
368 ret = authenticator->authenticate (pop_data, authenticator->method);
369 if (ret == POP_A_SOCKET)
370 switch (pop_connect (pop_data)) {
374 authenticator->authenticate (pop_data, authenticator->method);
381 if (ret != POP_A_UNAVAIL)
383 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
384 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL)))
395 return PQ_NOT_CONNECTED;
398 mutt_error (_("No authenticators available"));