remove most of the debug code: often makes the code unreadable, for little
[apps/madmutt.git] / imap / auth_sasl.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2000-3 Brendan Cully <brendan@kublai.com>
4  *
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.
8  */
9
10 /* SASL login/authentication code */
11
12 #if HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #include <lib-lib/mem.h>
17 #include <lib-lib/ascii.h>
18
19 #include "mutt.h"
20 #include "mutt_sasl.h"
21 #include "imap_private.h"
22 #include "auth.h"
23
24 #include <lib-lib/macros.h>
25
26 #include <sasl/sasl.h>
27 #include <sasl/saslutil.h>
28
29 /* imap_auth_sasl: Default authenticator if available. */
30 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
31 {
32   sasl_conn_t *saslconn;
33   sasl_interact_t *interaction = NULL;
34   int rc, irc;
35   char buf[HUGE_STRING];
36   const char *mech;
37
38   const char *pc = NULL;
39   unsigned int len, olen;
40   unsigned char client_start;
41
42   if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
43     return IMAP_AUTH_FAILURE;
44   }
45
46   rc = SASL_FAIL;
47
48   /* If the user hasn't specified a method, use any available */
49   if (!method) {
50     method = idata->capstr;
51
52     /* hack for SASL ANONYMOUS support:
53      * 1. Fetch username. If it's "" or "anonymous" then
54      * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
55      * 3. if sasl_client_start fails, fall through... */
56
57     if (mutt_account_getuser (&idata->conn->account))
58       return IMAP_AUTH_FAILURE;
59
60     if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
61         (!idata->conn->account.user[0] ||
62          !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
63       rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
64                               &mech);
65   }
66
67   if (rc != SASL_OK && rc != SASL_CONTINUE)
68     do {
69       rc = sasl_client_start (saslconn, method, &interaction,
70                               &pc, &olen, &mech);
71       if (rc == SASL_INTERACT)
72         mutt_sasl_interact (interaction);
73     }
74     while (rc == SASL_INTERACT);
75
76   client_start = (olen > 0);
77
78   if (rc != SASL_OK && rc != SASL_CONTINUE) {
79     /* SASL doesn't support LOGIN, so fall back */
80     return IMAP_AUTH_UNAVAIL;
81   }
82
83   mutt_message (_("Authenticating (%s)..."), mech);
84
85   snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
86   imap_cmd_start (idata, buf);
87   irc = IMAP_CMD_CONTINUE;
88
89   /* looping protocol */
90   while (rc == SASL_CONTINUE || olen > 0) {
91     do
92       irc = imap_cmd_step (idata);
93     while (irc == IMAP_CMD_CONTINUE);
94
95     if (method && irc == IMAP_CMD_NO) {
96       sasl_dispose (&saslconn);
97       return IMAP_AUTH_UNAVAIL;
98     }
99
100     if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
101       goto bail;
102
103     if (irc == IMAP_CMD_RESPOND) {
104       if (sasl_decode64(idata->cmd.buf + 2, m_strlen(idata->cmd.buf + 2), buf,
105                         LONG_STRING - 1, &len) != SASL_OK) {
106         goto bail;
107       }
108     }
109
110     /* client-start is only available with the SASL-IR extension, but
111      * SASL 2.1 seems to want to use it regardless, at least for DIGEST
112      * fast reauth. Override if the server sent an initial continuation */
113     if (!client_start || buf[0]) {
114       do {
115         rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
116         if (rc == SASL_INTERACT)
117           mutt_sasl_interact (interaction);
118       }
119       while (rc == SASL_INTERACT);
120     }
121     else
122       client_start = 0;
123
124     /* send out response, or line break if none needed */
125     if (olen) {
126       if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
127         goto bail;
128       }
129     }
130
131     if (irc == IMAP_CMD_RESPOND) {
132       m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
133       mutt_socket_write (idata->conn, buf);
134     }
135
136     /* If SASL has errored out, send an abort string to the server */
137     if (rc < 0) {
138       mutt_socket_write (idata->conn, "*\r\n");
139     }
140
141     olen = 0;
142   }
143
144   while (irc != IMAP_CMD_OK)
145     if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
146       break;
147
148   if (rc != SASL_OK)
149     goto bail;
150
151   if (imap_code (idata->cmd.buf)) {
152     mutt_sasl_setup_conn (idata->conn, saslconn);
153     return IMAP_AUTH_SUCCESS;
154   }
155
156 bail:
157   mutt_error _("SASL authentication failed.");
158   mutt_sleep (2);
159   sasl_dispose (&saslconn);
160
161   return IMAP_AUTH_FAILURE;
162 }