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/mem.h>
15 #include <lib-lib/ascii.h>
16 #include <lib-lib/macros.h>
17 #include <lib-hash/hash.h>
28 #include <sasl/sasl.h>
29 #include <sasl/saslutil.h>
30 #include "mutt_sasl.h"
34 /* SASL authenticator */
35 static pop_auth_res_t pop_auth_sasl (POP_DATA * pop_data, const char *method)
37 sasl_conn_t *saslconn;
38 sasl_interact_t *interaction = NULL;
40 char buf[LONG_STRING];
41 char inbuf[LONG_STRING];
45 const char *pc = NULL;
47 unsigned int len, olen;
48 unsigned char client_start;
50 if (mutt_sasl_client_new (pop_data->conn, &saslconn) < 0) {
55 method = pop_data->auth_list;
60 sasl_client_start (saslconn, method, &interaction, &pc, &olen, &mech);
62 if (rc != SASL_INTERACT)
64 mutt_sasl_interact (interaction);
67 if (rc != SASL_OK && rc != SASL_CONTINUE) {
68 /* SASL doesn't support suggested mechanisms, so fall back */
72 client_start = (olen > 0);
74 mutt_message _("Authenticating (SASL)...");
76 snprintf (buf, sizeof (buf), "AUTH %s", mech);
79 /* looping protocol */
81 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
82 mutt_socket_write (pop_data->conn, buf);
83 if (mutt_socket_readln (inbuf, sizeof (inbuf), pop_data->conn) < 0) {
84 sasl_dispose (&saslconn);
85 pop_data->status = POP_DISCONNECTED;
89 if (rc != SASL_CONTINUE)
93 if (!m_strncmp(inbuf, "+ ", 2)
94 && sasl_decode64 (inbuf, strlen (inbuf), buf, LONG_STRING - 1,
103 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
104 if (rc != SASL_INTERACT)
106 mutt_sasl_interact (interaction);
111 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
114 /* send out response, or line break if none needed */
116 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
120 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
131 if (!m_strncmp(inbuf, "+OK", 3)) {
132 mutt_sasl_setup_conn (pop_data->conn, saslconn);
133 return POP_A_SUCCESS;
137 sasl_dispose (&saslconn);
139 /* terminate SASL sessoin if the last responce is not +OK nor -ERR */
140 if (!m_strncmp(inbuf, "+ ", 2)) {
141 snprintf (buf, sizeof (buf), "*\r\n");
142 if (pop_query (pop_data, buf, sizeof (buf)) == PQ_NOT_CONNECTED)
146 mutt_error _("SASL authentication failed.");
150 return POP_A_FAILURE;
154 /* Get the server timestamp for APOP authentication */
155 void pop_apop_timestamp (POP_DATA * pop_data, char *buf)
159 p_delete(&pop_data->timestamp);
161 if ((p1 = strchr (buf, '<')) && (p2 = strchr (p1, '>'))) {
163 pop_data->timestamp = m_strdup(p1);
167 /* APOP authenticator */
168 static pop_auth_res_t pop_auth_apop (POP_DATA * pop_data,
169 const char *method __attribute__ ((unused)))
172 unsigned char digest[16];
174 char buf[LONG_STRING];
177 if (!pop_data->timestamp)
178 return POP_A_UNAVAIL;
180 mutt_message _("Authenticating (APOP)...");
182 /* Compute the authentication hash to send to the server */
183 MD5Init (&mdContext);
184 MD5Update (&mdContext, (unsigned char *) pop_data->timestamp,
185 strlen (pop_data->timestamp));
186 MD5Update (&mdContext, (unsigned char *) pop_data->conn->account.pass,
187 strlen (pop_data->conn->account.pass));
188 MD5Final (digest, &mdContext);
190 for (i = 0; i < ssizeof(digest); i++)
191 sprintf (hash + 2 * i, "%02x", digest[i]);
193 /* Send APOP command to server */
194 snprintf(buf, sizeof(buf), "APOP %s %s\r\n", pop_data->conn->account.user,
197 switch (pop_query (pop_data, buf, sizeof (buf))) {
199 return POP_A_SUCCESS;
200 case PQ_NOT_CONNECTED:
202 case PFD_FUNCT_ERROR:
208 mutt_error ("%s %s", _("APOP authentication failed."), pop_data->err_msg);
211 return POP_A_FAILURE;
214 /* USER authenticator */
215 static pop_auth_res_t pop_auth_user (POP_DATA * pop_data,
216 const char *method __attribute__ ((unused)))
218 char buf[LONG_STRING];
219 pop_query_status ret;
221 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
222 return POP_A_UNAVAIL;
224 mutt_message _("Logging in...");
226 snprintf (buf, sizeof (buf), "USER %s\r\n", pop_data->conn->account.user);
227 ret = pop_query (pop_data, buf, sizeof (buf));
229 if (pop_data->cmd_user == CMD_UNKNOWN) {
231 pop_data->cmd_user = CMD_AVAILABLE;
235 pop_data->cmd_user = CMD_NOT_AVAILABLE;
237 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
238 _("Command USER is not supported by server."));
243 snprintf (buf, sizeof (buf), "PASS %s\r\n", pop_data->conn->account.pass);
244 ret = pop_query_d (pop_data, buf, sizeof (buf), NULL);
249 return POP_A_SUCCESS;
250 case PQ_NOT_CONNECTED:
252 case PFD_FUNCT_ERROR:
258 mutt_error ("%s %s", _("Login failed."), pop_data->err_msg);
261 return POP_A_FAILURE;
264 static pop_auth_t pop_authenticators[] = {
266 {pop_auth_sasl, NULL},
268 {pop_auth_apop, "apop"},
269 {pop_auth_user, "user"},
276 * -1 - conection lost,
278 * -3 - authentication canceled.
280 pop_query_status pop_authenticate (POP_DATA * pop_data)
282 ACCOUNT *act = &pop_data->conn->account;
283 pop_auth_t *authenticator;
288 int ret = POP_A_UNAVAIL;
290 if (mutt_account_getuser (act) || !act->user[0] ||
291 mutt_account_getpass (act) || !act->pass[0])
292 return PFD_FUNCT_ERROR;
294 if (PopAuthenticators && *PopAuthenticators) {
295 /* Try user-specified list of authentication methods */
296 methods = m_strdup(PopAuthenticators);
300 comma = strchr (method, ':');
303 authenticator = pop_authenticators;
305 while (authenticator->authenticate) {
306 if (!authenticator->method ||
307 !ascii_strcasecmp (authenticator->method, method)) {
308 ret = authenticator->authenticate (pop_data, method);
309 if (ret == POP_A_SOCKET)
310 switch (pop_connect (pop_data)) {
313 ret = authenticator->authenticate (pop_data, method);
320 if (ret != POP_A_UNAVAIL)
322 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
323 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL))) {
337 /* Fall back to default: any authenticator */
338 authenticator = pop_authenticators;
340 while (authenticator->authenticate) {
341 ret = authenticator->authenticate (pop_data, authenticator->method);
342 if (ret == POP_A_SOCKET)
343 switch (pop_connect (pop_data)) {
347 authenticator->authenticate (pop_data, authenticator->method);
354 if (ret != POP_A_UNAVAIL)
356 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
357 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL)))
368 return PQ_NOT_CONNECTED;
371 mutt_error (_("No authenticators available"));