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.
22 #include "lib/debug.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) {
51 debug_print (1, ("Error allocating SASL connection.\n"));
56 method = pop_data->auth_list;
61 sasl_client_start (saslconn, method, &interaction, &pc, &olen, &mech);
63 if (rc != SASL_INTERACT)
65 mutt_sasl_interact (interaction);
68 if (rc != SASL_OK && rc != SASL_CONTINUE) {
69 debug_print (1, ("Failure starting authentication exchange. No shared mechanisms?\n"));
71 /* SASL doesn't support suggested mechanisms, so fall back */
75 client_start = (olen > 0);
77 mutt_message _("Authenticating (SASL)...");
79 snprintf (buf, sizeof (buf), "AUTH %s", mech);
82 /* looping protocol */
84 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
85 mutt_socket_write (pop_data->conn, buf);
86 if (mutt_socket_readln (inbuf, sizeof (inbuf), pop_data->conn) < 0) {
87 sasl_dispose (&saslconn);
88 pop_data->status = POP_DISCONNECTED;
92 if (rc != SASL_CONTINUE)
96 if (!str_ncmp (inbuf, "+ ", 2)
97 && sasl_decode64 (inbuf, strlen (inbuf), buf, LONG_STRING - 1,
101 debug_print (1, ("error base64-decoding server response.\n"));
107 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
108 if (rc != SASL_INTERACT)
110 mutt_sasl_interact (interaction);
115 if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
118 /* send out response, or line break if none needed */
120 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
121 debug_print (1, ("error base64-encoding client response.\n"));
125 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
136 if (!str_ncmp (inbuf, "+OK", 3)) {
137 mutt_sasl_setup_conn (pop_data->conn, saslconn);
138 return POP_A_SUCCESS;
142 sasl_dispose (&saslconn);
144 /* terminate SASL sessoin if the last responce is not +OK nor -ERR */
145 if (!str_ncmp (inbuf, "+ ", 2)) {
146 snprintf (buf, sizeof (buf), "*\r\n");
147 if (pop_query (pop_data, buf, sizeof (buf)) == PQ_NOT_CONNECTED)
151 mutt_error _("SASL authentication failed.");
155 return POP_A_FAILURE;
159 /* Get the server timestamp for APOP authentication */
160 void pop_apop_timestamp (POP_DATA * pop_data, char *buf)
164 mem_free (&pop_data->timestamp);
166 if ((p1 = strchr (buf, '<')) && (p2 = strchr (p1, '>'))) {
168 pop_data->timestamp = str_dup (p1);
172 /* APOP authenticator */
173 static pop_auth_res_t pop_auth_apop (POP_DATA * pop_data, const char *method)
176 unsigned char digest[16];
178 char buf[LONG_STRING];
181 if (!pop_data->timestamp)
182 return POP_A_UNAVAIL;
184 mutt_message _("Authenticating (APOP)...");
186 /* Compute the authentication hash to send to the server */
187 MD5Init (&mdContext);
188 MD5Update (&mdContext, (unsigned char *) pop_data->timestamp,
189 strlen (pop_data->timestamp));
190 MD5Update (&mdContext, (unsigned char *) pop_data->conn->account.pass,
191 strlen (pop_data->conn->account.pass));
192 MD5Final (digest, &mdContext);
194 for (i = 0; i < sizeof (digest); i++)
195 sprintf (hash + 2 * i, "%02x", digest[i]);
197 /* Send APOP command to server */
198 snprintf (buf, sizeof (buf), "APOP %s %s\r\n", pop_data->conn->account.user,
201 switch (pop_query (pop_data, buf, sizeof (buf))) {
203 return POP_A_SUCCESS;
204 case PQ_NOT_CONNECTED:
206 case PFD_FUNCT_ERROR:
212 mutt_error ("%s %s", _("APOP authentication failed."), pop_data->err_msg);
215 return POP_A_FAILURE;
218 /* USER authenticator */
219 static pop_auth_res_t pop_auth_user (POP_DATA * pop_data, const char *method)
221 char buf[LONG_STRING];
222 pop_query_status ret;
224 if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
225 return POP_A_UNAVAIL;
227 mutt_message _("Logging in...");
229 snprintf (buf, sizeof (buf), "USER %s\r\n", pop_data->conn->account.user);
230 ret = pop_query (pop_data, buf, sizeof (buf));
232 if (pop_data->cmd_user == CMD_UNKNOWN) {
234 pop_data->cmd_user = CMD_AVAILABLE;
236 debug_print (1, ("set USER capability\n"));
240 pop_data->cmd_user = CMD_NOT_AVAILABLE;
242 debug_print (1, ("unset USER capability\n"));
243 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
244 _("Command USER is not supported by server."));
249 snprintf (buf, sizeof (buf), "PASS %s\r\n", pop_data->conn->account.pass);
250 ret = pop_query_d (pop_data, buf, sizeof (buf),
252 /* don't print the password unless we're at the ungodly debugging level */
253 DebugLevel < M_SOCK_LOG_FULL ? "PASS *\r\n" :
260 return POP_A_SUCCESS;
261 case PQ_NOT_CONNECTED:
263 case PFD_FUNCT_ERROR:
269 mutt_error ("%s %s", _("Login failed."), pop_data->err_msg);
272 return POP_A_FAILURE;
275 static pop_auth_t pop_authenticators[] = {
277 {pop_auth_sasl, NULL},
279 {pop_auth_apop, "apop"},
280 {pop_auth_user, "user"},
287 * -1 - conection lost,
289 * -3 - authentication canceled.
291 pop_query_status pop_authenticate (POP_DATA * pop_data)
293 ACCOUNT *acct = &pop_data->conn->account;
294 pop_auth_t *authenticator;
299 int ret = POP_A_UNAVAIL;
301 if (mutt_account_getuser (acct) || !acct->user[0] ||
302 mutt_account_getpass (acct) || !acct->pass[0])
303 return PFD_FUNCT_ERROR;
305 if (PopAuthenticators && *PopAuthenticators) {
306 /* Try user-specified list of authentication methods */
307 methods = str_dup (PopAuthenticators);
311 comma = strchr (method, ':');
314 debug_print (2, ("Trying method %s\n", method));
315 authenticator = pop_authenticators;
317 while (authenticator->authenticate) {
318 if (!authenticator->method ||
319 !ascii_strcasecmp (authenticator->method, method)) {
320 ret = authenticator->authenticate (pop_data, method);
321 if (ret == POP_A_SOCKET)
322 switch (pop_connect (pop_data)) {
325 ret = authenticator->authenticate (pop_data, method);
332 if (ret != POP_A_UNAVAIL)
334 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
335 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL))) {
349 /* Fall back to default: any authenticator */
350 debug_print (2, ("Using any available method.\n"));
351 authenticator = pop_authenticators;
353 while (authenticator->authenticate) {
354 ret = authenticator->authenticate (pop_data, authenticator->method);
355 if (ret == POP_A_SOCKET)
356 switch (pop_connect (pop_data)) {
360 authenticator->authenticate (pop_data, authenticator->method);
367 if (ret != POP_A_UNAVAIL)
369 if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
370 (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL)))
381 return PQ_NOT_CONNECTED;
384 mutt_error (_("No authenticators available"));