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.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-hash/hash.h>
13 #include <lib-mx/mx.h>
19 #include <sasl/sasl.h>
20 #include <sasl/saslutil.h>
21 #include "mutt_sasl.h"
25 /* SASL authenticator */
26 static pop_auth_res_t pop_auth_sasl (POP_DATA * pop_data, const char *method)
28 sasl_conn_t *saslconn;
29 sasl_interact_t *interaction = NULL;
31 char buf[LONG_STRING];
32 char inbuf[LONG_STRING];
36 const char *pc = NULL;
38 unsigned int len, olen;
39 unsigned char client_start;
41 if (mutt_sasl_client_new (pop_data->conn, &saslconn) < 0) {
46 method = pop_data->auth_list;
51 sasl_client_start (saslconn, method, &interaction, &pc, &olen, &mech);
53 if (rc != SASL_INTERACT)
55 mutt_sasl_interact (interaction);
58 if (rc != SASL_OK && rc != SASL_CONTINUE) {
59 /* SASL doesn't support suggested mechanisms, so fall back */
63 client_start = (olen > 0);
65 mutt_message _("Authenticating (SASL)...");
67 snprintf (buf, sizeof (buf), "AUTH %s", mech);
70 /* looping protocol */
72 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
73 mutt_socket_write (pop_data->conn, buf);
74 if (mutt_socket_readln (inbuf, sizeof (inbuf), pop_data->conn) < 0) {
75 sasl_dispose (&saslconn);
76 pop_data->status = POP_DISCONNECTED;
80 if (rc != SASL_CONTINUE)
84 if (!m_strncmp(inbuf, "+ ", 2)
85 && sasl_decode64 (inbuf, strlen (inbuf), buf, LONG_STRING - 1,
94 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
95 if (rc != SASL_INTERACT)
97 mutt_sasl_interact (interaction);
102 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
105 /* send out response, or line break if none needed */
107 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
111 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
122 if (!m_strncmp(inbuf, "+OK", 3)) {
123 mutt_sasl_setup_conn (pop_data->conn, saslconn);
124 return POP_A_SUCCESS;
128 sasl_dispose (&saslconn);
130 /* terminate SASL sessoin if the last responce is not +OK nor -ERR */
131 if (!m_strncmp(inbuf, "+ ", 2)) {
132 snprintf (buf, sizeof (buf), "*\r\n");
133 if (pop_query (pop_data, buf, sizeof (buf)) == PQ_NOT_CONNECTED)
137 mutt_error _("SASL authentication failed.");
141 return POP_A_FAILURE;
145 /* Get the server timestamp for APOP authentication */
146 void pop_apop_timestamp (POP_DATA * pop_data, char *buf)
150 p_delete(&pop_data->timestamp);
152 if ((p1 = strchr (buf, '<')) && (p2 = strchr (p1, '>'))) {
154 pop_data->timestamp = m_strdup(p1);
158 /* APOP authenticator */
159 static pop_auth_res_t pop_auth_apop (POP_DATA * pop_data,
160 const char *method __attribute__ ((unused)))
163 unsigned char digest[16];
165 char buf[LONG_STRING];
168 if (!pop_data->timestamp)
169 return POP_A_UNAVAIL;
171 mutt_message _("Authenticating (APOP)...");
173 /* Compute the authentication hash to send to the server */
174 MD5Init (&mdContext);
175 MD5Update (&mdContext, (unsigned char *) pop_data->timestamp,
176 strlen (pop_data->timestamp));
177 MD5Update (&mdContext, (unsigned char *) pop_data->conn->account.pass,
178 strlen (pop_data->conn->account.pass));
179 MD5Final (digest, &mdContext);
181 for (i = 0; i < ssizeof(digest); i++)
182 sprintf (hash + 2 * i, "%02x", digest[i]);
184 /* Send APOP command to server */
185 snprintf(buf, sizeof(buf), "APOP %s %s\r\n", pop_data->conn->account.user,
188 switch (pop_query (pop_data, buf, sizeof (buf))) {
190 return POP_A_SUCCESS;
191 case PQ_NOT_CONNECTED:
193 case PFD_FUNCT_ERROR:
199 mutt_error ("%s %s", _("APOP authentication failed."), pop_data->err_msg);
202 return POP_A_FAILURE;
205 /* USER authenticator */
206 static pop_auth_res_t pop_auth_user (POP_DATA * pop_data,
207 const char *method __attribute__ ((unused)))
209 char buf[LONG_STRING];
210 pop_query_status ret;
212 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
213 return POP_A_UNAVAIL;
215 mutt_message _("Logging in...");
217 snprintf (buf, sizeof (buf), "USER %s\r\n", pop_data->conn->account.user);
218 ret = pop_query (pop_data, buf, sizeof (buf));
220 if (pop_data->cmd_user == CMD_UNKNOWN) {
222 pop_data->cmd_user = CMD_AVAILABLE;
226 pop_data->cmd_user = CMD_NOT_AVAILABLE;
228 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
229 _("Command USER is not supported by server."));
234 snprintf (buf, sizeof (buf), "PASS %s\r\n", pop_data->conn->account.pass);
235 ret = pop_query (pop_data, buf, sizeof (buf));
240 return POP_A_SUCCESS;
241 case PQ_NOT_CONNECTED:
243 case PFD_FUNCT_ERROR:
249 mutt_error ("%s %s", _("Login failed."), pop_data->err_msg);
252 return POP_A_FAILURE;
255 static pop_auth_t pop_authenticators[] = {
257 {pop_auth_sasl, NULL},
259 {pop_auth_apop, "apop"},
260 {pop_auth_user, "user"},
267 * -1 - conection lost,
269 * -3 - authentication canceled.
271 pop_query_status pop_authenticate (POP_DATA * pop_data)
273 ACCOUNT *act = &pop_data->conn->account;
274 pop_auth_t *authenticator;
279 int ret = POP_A_UNAVAIL;
281 if (mutt_account_getuser (act) || !act->user[0] ||
282 mutt_account_getpass (act) || !act->pass[0])
283 return PFD_FUNCT_ERROR;
285 if (PopAuthenticators && *PopAuthenticators) {
286 /* Try user-specified list of authentication methods */
287 methods = m_strdup(PopAuthenticators);
291 comma = strchr (method, ':');
294 authenticator = pop_authenticators;
296 while (authenticator->authenticate) {
297 if (!authenticator->method ||
298 !ascii_strcasecmp (authenticator->method, method)) {
299 ret = authenticator->authenticate (pop_data, method);
300 if (ret == POP_A_SOCKET)
301 switch (pop_connect (pop_data)) {
304 ret = authenticator->authenticate (pop_data, method);
311 if (ret != POP_A_UNAVAIL)
313 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
314 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL))) {
328 /* Fall back to default: any authenticator */
329 authenticator = pop_authenticators;
331 while (authenticator->authenticate) {
332 ret = authenticator->authenticate (pop_data, authenticator->method);
333 if (ret == POP_A_SOCKET)
334 switch (pop_connect (pop_data)) {
338 authenticator->authenticate (pop_data, authenticator->method);
345 if (ret != POP_A_UNAVAIL)
347 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
348 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL)))
359 return PQ_NOT_CONNECTED;
362 mutt_error (_("No authenticators available"));