Move the event loop to evtloop.c, and wake up sleeping jobs every 10 seconds.
[apps/madmutt.git] / imap / auth.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-8 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
5  * Copyright (C) 1999-2001 Brendan Cully <brendan@kublai.com>
6  *
7  * This file is part of mutt-ng, see http://www.muttng.org/.
8  * It's licensed under the GNU General Public License,
9  * please see the file GPL in the top level source directory.
10  */
11
12 /* IMAP login/authentication code */
13
14 #include <lib-lib/lib-lib.h>
15
16 #include <sasl/sasl.h>
17 #include <sasl/saslutil.h>
18
19 #include "mutt.h"
20 #include "mutt_sasl.h"
21 #include "imap_private.h"
22
23 enum {
24     IMAP_AUTH_SUCCESS = 0,
25     IMAP_AUTH_FAILURE,
26     IMAP_AUTH_UNAVAIL
27 };
28
29 /* imap_auth_sasl: Default authenticator if available. */
30 static int 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          !m_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.data + 2, idata->cmd.buf.len - 2, buf,
105                         LONG_STRING - 1, &len) != SASL_OK)
106       {
107         goto bail;
108       }
109     }
110
111     /* client-start is only available with the SASL-IR extension, but
112      * SASL 2.1 seems to want to use it regardless, at least for DIGEST
113      * fast reauth. Override if the server sent an initial continuation */
114     if (!client_start || buf[0]) {
115       do {
116         rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
117         if (rc == SASL_INTERACT)
118           mutt_sasl_interact (interaction);
119       }
120       while (rc == SASL_INTERACT);
121     }
122     else
123       client_start = 0;
124
125     /* send out response, or line break if none needed */
126     if (olen) {
127       if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
128         goto bail;
129       }
130     }
131
132     if (irc == IMAP_CMD_RESPOND) {
133       m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
134       mutt_socket_write (idata->conn, buf);
135     }
136
137     /* If SASL has errored out, send an abort string to the server */
138     if (rc < 0) {
139       mutt_socket_write (idata->conn, "*\r\n");
140     }
141
142     olen = 0;
143   }
144
145   while (irc != IMAP_CMD_OK)
146     if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
147       break;
148
149   if (rc != SASL_OK)
150     goto bail;
151
152   if (imap_code(idata->cmd.buf.data)) {
153     mutt_sasl_setup_conn (idata->conn, saslconn);
154     return IMAP_AUTH_SUCCESS;
155   }
156
157 bail:
158   mutt_error _("SASL authentication failed.");
159   mutt_sleep (2);
160   sasl_dispose (&saslconn);
161
162   return IMAP_AUTH_FAILURE;
163 }
164
165 int imap_authenticate (IMAP_DATA * idata)
166 {
167     int r = -1;
168
169     if (!m_strisempty(ImapAuthenticators)) {
170         const char *p, *q;
171         char buf[STRING];
172
173         for (p = ImapAuthenticators;; p = q) {
174             while (*p == ':')
175                 p++;
176             if (!*p)
177                 break;
178
179             q = strchrnul(p, ':');
180             m_strncpy(buf, sizeof(buf), p, q - p);
181
182             if ((r = imap_auth_sasl(idata, buf)) != IMAP_AUTH_UNAVAIL) {
183                 return r;
184             }
185         }
186     } else {
187         if ((r = imap_auth_sasl(idata, NULL)) != IMAP_AUTH_UNAVAIL) {
188             return r;
189         }
190     }
191
192     mutt_error (_("No authenticators available"));
193     mutt_sleep (1);
194     return r;
195 }