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.
14 #include <lib-lib/lib-lib.h>
15 #include <lib-hash/hash.h>
26 #include <sasl/sasl.h>
27 #include <sasl/saslutil.h>
28 #include "mutt_sasl.h"
32 /* SASL authenticator */
33 static pop_auth_res_t pop_auth_sasl (POP_DATA * pop_data, const char *method)
35 sasl_conn_t *saslconn;
36 sasl_interact_t *interaction = NULL;
38 char buf[LONG_STRING];
39 char inbuf[LONG_STRING];
43 const char *pc = NULL;
45 unsigned int len, olen;
46 unsigned char client_start;
48 if (mutt_sasl_client_new (pop_data->conn, &saslconn) < 0) {
53 method = pop_data->auth_list;
58 sasl_client_start (saslconn, method, &interaction, &pc, &olen, &mech);
60 if (rc != SASL_INTERACT)
62 mutt_sasl_interact (interaction);
65 if (rc != SASL_OK && rc != SASL_CONTINUE) {
66 /* SASL doesn't support suggested mechanisms, so fall back */
70 client_start = (olen > 0);
72 mutt_message _("Authenticating (SASL)...");
74 snprintf (buf, sizeof (buf), "AUTH %s", mech);
77 /* looping protocol */
79 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
80 mutt_socket_write (pop_data->conn, buf);
81 if (mutt_socket_readln (inbuf, sizeof (inbuf), pop_data->conn) < 0) {
82 sasl_dispose (&saslconn);
83 pop_data->status = POP_DISCONNECTED;
87 if (rc != SASL_CONTINUE)
91 if (!m_strncmp(inbuf, "+ ", 2)
92 && sasl_decode64 (inbuf, strlen (inbuf), buf, LONG_STRING - 1,
101 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
102 if (rc != SASL_INTERACT)
104 mutt_sasl_interact (interaction);
109 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
112 /* send out response, or line break if none needed */
114 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
118 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
129 if (!m_strncmp(inbuf, "+OK", 3)) {
130 mutt_sasl_setup_conn (pop_data->conn, saslconn);
131 return POP_A_SUCCESS;
135 sasl_dispose (&saslconn);
137 /* terminate SASL sessoin if the last responce is not +OK nor -ERR */
138 if (!m_strncmp(inbuf, "+ ", 2)) {
139 snprintf (buf, sizeof (buf), "*\r\n");
140 if (pop_query (pop_data, buf, sizeof (buf)) == PQ_NOT_CONNECTED)
144 mutt_error _("SASL authentication failed.");
148 return POP_A_FAILURE;
152 /* Get the server timestamp for APOP authentication */
153 void pop_apop_timestamp (POP_DATA * pop_data, char *buf)
157 p_delete(&pop_data->timestamp);
159 if ((p1 = strchr (buf, '<')) && (p2 = strchr (p1, '>'))) {
161 pop_data->timestamp = m_strdup(p1);
165 /* APOP authenticator */
166 static pop_auth_res_t pop_auth_apop (POP_DATA * pop_data,
167 const char *method __attribute__ ((unused)))
170 unsigned char digest[16];
172 char buf[LONG_STRING];
175 if (!pop_data->timestamp)
176 return POP_A_UNAVAIL;
178 mutt_message _("Authenticating (APOP)...");
180 /* Compute the authentication hash to send to the server */
181 MD5Init (&mdContext);
182 MD5Update (&mdContext, (unsigned char *) pop_data->timestamp,
183 strlen (pop_data->timestamp));
184 MD5Update (&mdContext, (unsigned char *) pop_data->conn->account.pass,
185 strlen (pop_data->conn->account.pass));
186 MD5Final (digest, &mdContext);
188 for (i = 0; i < ssizeof(digest); i++)
189 sprintf (hash + 2 * i, "%02x", digest[i]);
191 /* Send APOP command to server */
192 snprintf(buf, sizeof(buf), "APOP %s %s\r\n", pop_data->conn->account.user,
195 switch (pop_query (pop_data, buf, sizeof (buf))) {
197 return POP_A_SUCCESS;
198 case PQ_NOT_CONNECTED:
200 case PFD_FUNCT_ERROR:
206 mutt_error ("%s %s", _("APOP authentication failed."), pop_data->err_msg);
209 return POP_A_FAILURE;
212 /* USER authenticator */
213 static pop_auth_res_t pop_auth_user (POP_DATA * pop_data,
214 const char *method __attribute__ ((unused)))
216 char buf[LONG_STRING];
217 pop_query_status ret;
219 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
220 return POP_A_UNAVAIL;
222 mutt_message _("Logging in...");
224 snprintf (buf, sizeof (buf), "USER %s\r\n", pop_data->conn->account.user);
225 ret = pop_query (pop_data, buf, sizeof (buf));
227 if (pop_data->cmd_user == CMD_UNKNOWN) {
229 pop_data->cmd_user = CMD_AVAILABLE;
233 pop_data->cmd_user = CMD_NOT_AVAILABLE;
235 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
236 _("Command USER is not supported by server."));
241 snprintf (buf, sizeof (buf), "PASS %s\r\n", pop_data->conn->account.pass);
242 ret = pop_query_d (pop_data, buf, sizeof (buf), NULL);
247 return POP_A_SUCCESS;
248 case PQ_NOT_CONNECTED:
250 case PFD_FUNCT_ERROR:
256 mutt_error ("%s %s", _("Login failed."), pop_data->err_msg);
259 return POP_A_FAILURE;
262 static pop_auth_t pop_authenticators[] = {
264 {pop_auth_sasl, NULL},
266 {pop_auth_apop, "apop"},
267 {pop_auth_user, "user"},
274 * -1 - conection lost,
276 * -3 - authentication canceled.
278 pop_query_status pop_authenticate (POP_DATA * pop_data)
280 ACCOUNT *act = &pop_data->conn->account;
281 pop_auth_t *authenticator;
286 int ret = POP_A_UNAVAIL;
288 if (mutt_account_getuser (act) || !act->user[0] ||
289 mutt_account_getpass (act) || !act->pass[0])
290 return PFD_FUNCT_ERROR;
292 if (PopAuthenticators && *PopAuthenticators) {
293 /* Try user-specified list of authentication methods */
294 methods = m_strdup(PopAuthenticators);
298 comma = strchr (method, ':');
301 authenticator = pop_authenticators;
303 while (authenticator->authenticate) {
304 if (!authenticator->method ||
305 !ascii_strcasecmp (authenticator->method, method)) {
306 ret = authenticator->authenticate (pop_data, method);
307 if (ret == POP_A_SOCKET)
308 switch (pop_connect (pop_data)) {
311 ret = authenticator->authenticate (pop_data, method);
318 if (ret != POP_A_UNAVAIL)
320 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
321 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL))) {
335 /* Fall back to default: any authenticator */
336 authenticator = pop_authenticators;
338 while (authenticator->authenticate) {
339 ret = authenticator->authenticate (pop_data, authenticator->method);
340 if (ret == POP_A_SOCKET)
341 switch (pop_connect (pop_data)) {
345 authenticator->authenticate (pop_data, authenticator->method);
352 if (ret != POP_A_UNAVAIL)
354 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
355 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL)))
366 return PQ_NOT_CONNECTED;
369 mutt_error (_("No authenticators available"));