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>
18 #include <lib-lib/debug.h>
29 #include <sasl/sasl.h>
30 #include <sasl/saslutil.h>
31 #include "mutt_sasl.h"
35 /* SASL authenticator */
36 static pop_auth_res_t pop_auth_sasl (POP_DATA * pop_data, const char *method)
38 sasl_conn_t *saslconn;
39 sasl_interact_t *interaction = NULL;
41 char buf[LONG_STRING];
42 char inbuf[LONG_STRING];
46 const char *pc = NULL;
48 unsigned int len, olen;
49 unsigned char client_start;
51 if (mutt_sasl_client_new (pop_data->conn, &saslconn) < 0) {
52 debug_print (1, ("Error allocating SASL connection.\n"));
57 method = pop_data->auth_list;
62 sasl_client_start (saslconn, method, &interaction, &pc, &olen, &mech);
64 if (rc != SASL_INTERACT)
66 mutt_sasl_interact (interaction);
69 if (rc != SASL_OK && rc != SASL_CONTINUE) {
70 debug_print (1, ("Failure starting authentication exchange. No shared mechanisms?\n"));
72 /* SASL doesn't support suggested mechanisms, so fall back */
76 client_start = (olen > 0);
78 mutt_message _("Authenticating (SASL)...");
80 snprintf (buf, sizeof (buf), "AUTH %s", mech);
83 /* looping protocol */
85 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
86 mutt_socket_write (pop_data->conn, buf);
87 if (mutt_socket_readln (inbuf, sizeof (inbuf), pop_data->conn) < 0) {
88 sasl_dispose (&saslconn);
89 pop_data->status = POP_DISCONNECTED;
93 if (rc != SASL_CONTINUE)
97 if (!m_strncmp(inbuf, "+ ", 2)
98 && sasl_decode64 (inbuf, strlen (inbuf), buf, LONG_STRING - 1,
102 debug_print (1, ("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);
116 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
119 /* send out response, or line break if none needed */
121 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
122 debug_print (1, ("error base64-encoding client response.\n"));
126 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
137 if (!m_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 (!m_strncmp(inbuf, "+ ", 2)) {
147 snprintf (buf, sizeof (buf), "*\r\n");
148 if (pop_query (pop_data, buf, sizeof (buf)) == PQ_NOT_CONNECTED)
152 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 p_delete(&pop_data->timestamp);
167 if ((p1 = strchr (buf, '<')) && (p2 = strchr (p1, '>'))) {
169 pop_data->timestamp = m_strdup(p1);
173 /* APOP authenticator */
174 static pop_auth_res_t pop_auth_apop (POP_DATA * pop_data,
175 const char *method __attribute__ ((unused)))
178 unsigned char digest[16];
180 char buf[LONG_STRING];
183 if (!pop_data->timestamp)
184 return POP_A_UNAVAIL;
186 mutt_message _("Authenticating (APOP)...");
188 /* Compute the authentication hash to send to the server */
189 MD5Init (&mdContext);
190 MD5Update (&mdContext, (unsigned char *) pop_data->timestamp,
191 strlen (pop_data->timestamp));
192 MD5Update (&mdContext, (unsigned char *) pop_data->conn->account.pass,
193 strlen (pop_data->conn->account.pass));
194 MD5Final (digest, &mdContext);
196 for (i = 0; i < ssizeof(digest); i++)
197 sprintf (hash + 2 * i, "%02x", digest[i]);
199 /* Send APOP command to server */
200 snprintf(buf, sizeof(buf), "APOP %s %s\r\n", pop_data->conn->account.user,
203 switch (pop_query (pop_data, buf, sizeof (buf))) {
205 return POP_A_SUCCESS;
206 case PQ_NOT_CONNECTED:
208 case PFD_FUNCT_ERROR:
214 mutt_error ("%s %s", _("APOP authentication failed."), pop_data->err_msg);
217 return POP_A_FAILURE;
220 /* USER authenticator */
221 static pop_auth_res_t pop_auth_user (POP_DATA * pop_data,
222 const char *method __attribute__ ((unused)))
224 char buf[LONG_STRING];
225 pop_query_status ret;
227 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
228 return POP_A_UNAVAIL;
230 mutt_message _("Logging in...");
232 snprintf (buf, sizeof (buf), "USER %s\r\n", pop_data->conn->account.user);
233 ret = pop_query (pop_data, buf, sizeof (buf));
235 if (pop_data->cmd_user == CMD_UNKNOWN) {
237 pop_data->cmd_user = CMD_AVAILABLE;
239 debug_print (1, ("set USER capability\n"));
243 pop_data->cmd_user = CMD_NOT_AVAILABLE;
245 debug_print (1, ("unset USER capability\n"));
246 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
247 _("Command USER is not supported by server."));
252 snprintf (buf, sizeof (buf), "PASS %s\r\n", pop_data->conn->account.pass);
253 ret = pop_query_d (pop_data, buf, sizeof (buf),
255 /* don't print the password unless we're at the ungodly debugging level */
256 DebugLevel < M_SOCK_LOG_FULL ? "PASS *\r\n" :
263 return POP_A_SUCCESS;
264 case PQ_NOT_CONNECTED:
266 case PFD_FUNCT_ERROR:
272 mutt_error ("%s %s", _("Login failed."), pop_data->err_msg);
275 return POP_A_FAILURE;
278 static pop_auth_t pop_authenticators[] = {
280 {pop_auth_sasl, NULL},
282 {pop_auth_apop, "apop"},
283 {pop_auth_user, "user"},
290 * -1 - conection lost,
292 * -3 - authentication canceled.
294 pop_query_status pop_authenticate (POP_DATA * pop_data)
296 ACCOUNT *act = &pop_data->conn->account;
297 pop_auth_t *authenticator;
302 int ret = POP_A_UNAVAIL;
304 if (mutt_account_getuser (act) || !act->user[0] ||
305 mutt_account_getpass (act) || !act->pass[0])
306 return PFD_FUNCT_ERROR;
308 if (PopAuthenticators && *PopAuthenticators) {
309 /* Try user-specified list of authentication methods */
310 methods = m_strdup(PopAuthenticators);
314 comma = strchr (method, ':');
317 debug_print (2, ("Trying method %s\n", method));
318 authenticator = pop_authenticators;
320 while (authenticator->authenticate) {
321 if (!authenticator->method ||
322 !ascii_strcasecmp (authenticator->method, method)) {
323 ret = authenticator->authenticate (pop_data, method);
324 if (ret == POP_A_SOCKET)
325 switch (pop_connect (pop_data)) {
328 ret = authenticator->authenticate (pop_data, method);
335 if (ret != POP_A_UNAVAIL)
337 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
338 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL))) {
352 /* Fall back to default: any authenticator */
353 debug_print (2, ("Using any available method.\n"));
354 authenticator = pop_authenticators;
356 while (authenticator->authenticate) {
357 ret = authenticator->authenticate (pop_data, authenticator->method);
358 if (ret == POP_A_SOCKET)
359 switch (pop_connect (pop_data)) {
363 authenticator->authenticate (pop_data, authenticator->method);
370 if (ret != POP_A_UNAVAIL)
372 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
373 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL)))
384 return PQ_NOT_CONNECTED;
387 mutt_error (_("No authenticators available"));