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