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