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>
24 #include "lib/debug.h"
30 #include <sasl/sasl.h>
31 #include <sasl/saslutil.h>
32 #include "mutt_sasl.h"
36 /* SASL authenticator */
37 static pop_auth_res_t pop_auth_sasl (POP_DATA * pop_data, const char *method)
39 sasl_conn_t *saslconn;
40 sasl_interact_t *interaction = NULL;
42 char buf[LONG_STRING];
43 char inbuf[LONG_STRING];
47 const char *pc = NULL;
49 unsigned int len, olen;
50 unsigned char client_start;
52 if (mutt_sasl_client_new (pop_data->conn, &saslconn) < 0) {
53 debug_print (1, ("Error allocating SASL connection.\n"));
58 method = pop_data->auth_list;
63 sasl_client_start (saslconn, method, &interaction, &pc, &olen, &mech);
65 if (rc != SASL_INTERACT)
67 mutt_sasl_interact (interaction);
70 if (rc != SASL_OK && rc != SASL_CONTINUE) {
71 debug_print (1, ("Failure starting authentication exchange. No shared mechanisms?\n"));
73 /* SASL doesn't support suggested mechanisms, so fall back */
77 client_start = (olen > 0);
79 mutt_message _("Authenticating (SASL)...");
81 snprintf (buf, sizeof (buf), "AUTH %s", mech);
84 /* looping protocol */
86 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
87 mutt_socket_write (pop_data->conn, buf);
88 if (mutt_socket_readln (inbuf, sizeof (inbuf), pop_data->conn) < 0) {
89 sasl_dispose (&saslconn);
90 pop_data->status = POP_DISCONNECTED;
94 if (rc != SASL_CONTINUE)
98 if (!str_ncmp (inbuf, "+ ", 2)
99 && sasl_decode64 (inbuf, strlen (inbuf), buf, LONG_STRING - 1,
103 debug_print (1, ("error base64-decoding server response.\n"));
109 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
110 if (rc != SASL_INTERACT)
112 mutt_sasl_interact (interaction);
117 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
120 /* send out response, or line break if none needed */
122 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
123 debug_print (1, ("error base64-encoding client response.\n"));
127 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
138 if (!str_ncmp (inbuf, "+OK", 3)) {
139 mutt_sasl_setup_conn (pop_data->conn, saslconn);
140 return POP_A_SUCCESS;
144 sasl_dispose (&saslconn);
146 /* terminate SASL sessoin if the last responce is not +OK nor -ERR */
147 if (!str_ncmp (inbuf, "+ ", 2)) {
148 snprintf (buf, sizeof (buf), "*\r\n");
149 if (pop_query (pop_data, buf, sizeof (buf)) == PQ_NOT_CONNECTED)
153 mutt_error _("SASL authentication failed.");
157 return POP_A_FAILURE;
161 /* Get the server timestamp for APOP authentication */
162 void pop_apop_timestamp (POP_DATA * pop_data, char *buf)
166 p_delete(&pop_data->timestamp);
168 if ((p1 = strchr (buf, '<')) && (p2 = strchr (p1, '>'))) {
170 pop_data->timestamp = str_dup (p1);
174 /* APOP authenticator */
175 static pop_auth_res_t pop_auth_apop (POP_DATA * pop_data, const char *method)
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 < sizeof (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, const char *method)
223 char buf[LONG_STRING];
224 pop_query_status ret;
226 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
227 return POP_A_UNAVAIL;
229 mutt_message _("Logging in...");
231 snprintf (buf, sizeof (buf), "USER %s\r\n", pop_data->conn->account.user);
232 ret = pop_query (pop_data, buf, sizeof (buf));
234 if (pop_data->cmd_user == CMD_UNKNOWN) {
236 pop_data->cmd_user = CMD_AVAILABLE;
238 debug_print (1, ("set USER capability\n"));
242 pop_data->cmd_user = CMD_NOT_AVAILABLE;
244 debug_print (1, ("unset USER capability\n"));
245 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
246 _("Command USER is not supported by server."));
251 snprintf (buf, sizeof (buf), "PASS %s\r\n", pop_data->conn->account.pass);
252 ret = pop_query_d (pop_data, buf, sizeof (buf),
254 /* don't print the password unless we're at the ungodly debugging level */
255 DebugLevel < M_SOCK_LOG_FULL ? "PASS *\r\n" :
262 return POP_A_SUCCESS;
263 case PQ_NOT_CONNECTED:
265 case PFD_FUNCT_ERROR:
271 mutt_error ("%s %s", _("Login failed."), pop_data->err_msg);
274 return POP_A_FAILURE;
277 static pop_auth_t pop_authenticators[] = {
279 {pop_auth_sasl, NULL},
281 {pop_auth_apop, "apop"},
282 {pop_auth_user, "user"},
289 * -1 - conection lost,
291 * -3 - authentication canceled.
293 pop_query_status pop_authenticate (POP_DATA * pop_data)
295 ACCOUNT *acct = &pop_data->conn->account;
296 pop_auth_t *authenticator;
301 int ret = POP_A_UNAVAIL;
303 if (mutt_account_getuser (acct) || !acct->user[0] ||
304 mutt_account_getpass (acct) || !acct->pass[0])
305 return PFD_FUNCT_ERROR;
307 if (PopAuthenticators && *PopAuthenticators) {
308 /* Try user-specified list of authentication methods */
309 methods = str_dup (PopAuthenticators);
313 comma = strchr (method, ':');
316 debug_print (2, ("Trying method %s\n", method));
317 authenticator = pop_authenticators;
319 while (authenticator->authenticate) {
320 if (!authenticator->method ||
321 !ascii_strcasecmp (authenticator->method, method)) {
322 ret = authenticator->authenticate (pop_data, method);
323 if (ret == POP_A_SOCKET)
324 switch (pop_connect (pop_data)) {
327 ret = authenticator->authenticate (pop_data, method);
334 if (ret != POP_A_UNAVAIL)
336 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
337 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL))) {
351 /* Fall back to default: any authenticator */
352 debug_print (2, ("Using any available method.\n"));
353 authenticator = pop_authenticators;
355 while (authenticator->authenticate) {
356 ret = authenticator->authenticate (pop_data, authenticator->method);
357 if (ret == POP_A_SOCKET)
358 switch (pop_connect (pop_data)) {
362 authenticator->authenticate (pop_data, authenticator->method);
369 if (ret != POP_A_UNAVAIL)
371 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
372 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL)))
383 return PQ_NOT_CONNECTED;
386 mutt_error (_("No authenticators available"));