Rocco Rutte:
[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 "mutt.h"
17 #include "mutt_sasl.h"
18 #include "imap_private.h"
19 #include "auth.h"
20
21 #include "lib/intl.h"
22
23 #ifdef USE_SASL2
24 #include <sasl/sasl.h>
25 #include <sasl/saslutil.h>
26 #else
27 #include <sasl.h>
28 #include <saslutil.h>
29 #endif
30
31 /* imap_auth_sasl: Default authenticator if available. */
32 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
33 {
34   sasl_conn_t *saslconn;
35   sasl_interact_t *interaction = NULL;
36   int rc, irc;
37   char buf[HUGE_STRING];
38   const char *mech;
39
40 #ifdef USE_SASL2
41   const char *pc = NULL;
42 #else
43   char *pc = NULL;
44 #endif
45   unsigned int len, olen;
46   unsigned char client_start;
47
48   if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
49     dprint (1, (debugfile,
50                 "imap_auth_sasl: Error allocating SASL connection.\n"));
51     return IMAP_AUTH_FAILURE;
52   }
53
54   rc = SASL_FAIL;
55
56   /* If the user hasn't specified a method, use any available */
57   if (!method) {
58     method = idata->capstr;
59
60     /* hack for SASL ANONYMOUS support:
61      * 1. Fetch username. If it's "" or "anonymous" then
62      * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
63      * 3. if sasl_client_start fails, fall through... */
64
65     if (mutt_account_getuser (&idata->conn->account))
66       return IMAP_AUTH_FAILURE;
67
68     if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
69         (!idata->conn->account.user[0] ||
70          !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
71 #ifdef USE_SASL2
72       rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
73                               &mech);
74 #else
75       rc =
76         sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, NULL, &pc, &olen,
77                            &mech);
78 #endif
79   }
80
81   if (rc != SASL_OK && rc != SASL_CONTINUE)
82     do {
83 #ifdef USE_SASL2
84       rc = sasl_client_start (saslconn, method, &interaction,
85                               &pc, &olen, &mech);
86 #else
87       rc = sasl_client_start (saslconn, method, NULL, &interaction,
88                               &pc, &olen, &mech);
89 #endif
90       if (rc == SASL_INTERACT)
91         mutt_sasl_interact (interaction);
92     }
93     while (rc == SASL_INTERACT);
94
95   client_start = (olen > 0);
96
97   if (rc != SASL_OK && rc != SASL_CONTINUE) {
98     if (method)
99       dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method));
100     else
101       dprint (1,
102               (debugfile,
103                "imap_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
104     /* SASL doesn't support LOGIN, so fall back */
105
106     return IMAP_AUTH_UNAVAIL;
107   }
108
109   mutt_message (_("Authenticating (%s)..."), mech);
110
111   snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
112   imap_cmd_start (idata, buf);
113   irc = IMAP_CMD_CONTINUE;
114
115   /* looping protocol */
116   while (rc == SASL_CONTINUE || olen > 0) {
117     do
118       irc = imap_cmd_step (idata);
119     while (irc == IMAP_CMD_CONTINUE);
120
121     if (method && irc == IMAP_CMD_NO) {
122       dprint (2, (debugfile, "imap_auth_sasl: %s failed\n", method));
123       sasl_dispose (&saslconn);
124       return IMAP_AUTH_UNAVAIL;
125     }
126
127     if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
128       goto bail;
129
130     if (irc == IMAP_CMD_RESPOND) {
131 #ifdef USE_SASL2
132       if (sasl_decode64
133           (idata->cmd.buf + 2, mutt_strlen (idata->cmd.buf + 2), buf,
134            LONG_STRING - 1,
135 #else
136       if (sasl_decode64 (idata->cmd.buf + 2, mutt_strlen (idata->cmd.buf + 2), buf,
137 #endif
138                          &len) != SASL_OK) {
139         dprint (1,
140                 (debugfile,
141                  "imap_auth_sasl: error base64-decoding server response.\n"));
142         goto bail;
143       }
144     }
145
146     if (!client_start) {
147       do {
148         rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
149         if (rc == SASL_INTERACT)
150           mutt_sasl_interact (interaction);
151       }
152       while (rc == SASL_INTERACT);
153     }
154     else
155       client_start = 0;
156
157     /* send out response, or line break if none needed */
158     if (olen) {
159       if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
160         dprint (1,
161                 (debugfile,
162                  "imap_auth_sasl: error base64-encoding client response.\n"));
163         goto bail;
164       }
165
166       /* sasl_client_st(art|ep) allocate pc with malloc, expect me to 
167        * free it */
168 #ifndef USE_SASL2
169       FREE (&pc);
170 #endif
171     }
172
173     if (irc == IMAP_CMD_RESPOND) {
174       strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
175       mutt_socket_write (idata->conn, buf);
176     }
177
178     /* If SASL has errored out, send an abort string to the server */
179     if (rc < 0) {
180       mutt_socket_write (idata->conn, "*\r\n");
181       dprint (1,
182               (debugfile, "imap_auth_sasl: sasl_client_step error %d\n", rc));
183     }
184
185     olen = 0;
186   }
187
188   while (irc != IMAP_CMD_OK)
189     if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
190       break;
191
192   if (rc != SASL_OK)
193     goto bail;
194
195   if (imap_code (idata->cmd.buf)) {
196     mutt_sasl_setup_conn (idata->conn, saslconn);
197     return IMAP_AUTH_SUCCESS;
198   }
199
200 bail:
201   mutt_error _("SASL authentication failed.");
202   mutt_sleep (2);
203   sasl_dispose (&saslconn);
204
205   return IMAP_AUTH_FAILURE;
206 }