Rocco Rutte:
[apps/madmutt.git] / imap / auth_gss.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1999-2000 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 /* GSS login/authentication code */
11
12 #if HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #include "mutt.h"
17 #include "imap_private.h"
18 #include "auth.h"
19
20 #include "lib/intl.h"
21
22 #include <netinet/in.h>
23
24 #ifdef HAVE_HEIMDAL
25 #  include <gssapi/gssapi.h>
26 #  define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
27 #else
28 #  include <gssapi/gssapi.h>
29 #  include <gssapi/gssapi_generic.h>
30 #endif
31
32 #define GSS_BUFSIZE 8192
33
34 #define GSS_AUTH_P_NONE      1
35 #define GSS_AUTH_P_INTEGRITY 2
36 #define GSS_AUTH_P_PRIVACY   4
37
38 /* imap_auth_gss: AUTH=GSSAPI support. */
39 imap_auth_res_t imap_auth_gss (IMAP_DATA * idata, const char *method)
40 {
41   gss_buffer_desc request_buf, send_token;
42   gss_buffer_t sec_token;
43   gss_name_t target_name;
44   gss_ctx_id_t context;
45   gss_OID mech_name;
46   gss_qop_t quality;
47   int cflags;
48   OM_uint32 maj_stat, min_stat;
49   char buf1[GSS_BUFSIZE], buf2[GSS_BUFSIZE], server_conf_flags;
50   unsigned long buf_size;
51   int rc;
52
53   if (!mutt_bit_isset (idata->capabilities, AGSSAPI))
54     return IMAP_AUTH_UNAVAIL;
55
56   if (mutt_account_getuser (&idata->conn->account))
57     return IMAP_AUTH_FAILURE;
58
59   /* get an IMAP service ticket for the server */
60   snprintf (buf1, sizeof (buf1), "imap@%s", idata->conn->account.host);
61   request_buf.value = buf1;
62   request_buf.length = safe_strlen (buf1) + 1;
63   maj_stat = gss_import_name (&min_stat, &request_buf, gss_nt_service_name,
64                               &target_name);
65   if (maj_stat != GSS_S_COMPLETE) {
66     dprint (2, (debugfile, "Couldn't get service name for [%s]\n", buf1));
67     return IMAP_AUTH_UNAVAIL;
68   }
69 #ifdef DEBUG
70   else if (debuglevel >= 2) {
71     maj_stat = gss_display_name (&min_stat, target_name, &request_buf,
72                                  &mech_name);
73     dprint (2, (debugfile, "Using service name [%s]\n",
74                 (char *) request_buf.value));
75     maj_stat = gss_release_buffer (&min_stat, &request_buf);
76   }
77 #endif
78   /* Acquire initial credentials - without a TGT GSSAPI is UNAVAIL */
79   sec_token = GSS_C_NO_BUFFER;
80   context = GSS_C_NO_CONTEXT;
81
82   /* build token */
83   maj_stat = gss_init_sec_context (&min_stat, GSS_C_NO_CREDENTIAL, &context,
84                                    target_name, GSS_C_NO_OID,
85                                    GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG, 0,
86                                    GSS_C_NO_CHANNEL_BINDINGS, sec_token, NULL,
87                                    &send_token, (unsigned int *) &cflags,
88                                    NULL);
89   if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) {
90     dprint (1, (debugfile, "Error acquiring credentials - no TGT?\n"));
91     gss_release_name (&min_stat, &target_name);
92
93     return IMAP_AUTH_UNAVAIL;
94   }
95
96   /* now begin login */
97   mutt_message _("Authenticating (GSSAPI)...");
98
99   imap_cmd_start (idata, "AUTHENTICATE GSSAPI");
100
101   /* expect a null continuation response ("+") */
102   do
103     rc = imap_cmd_step (idata);
104   while (rc == IMAP_CMD_CONTINUE);
105
106   if (rc != IMAP_CMD_RESPOND) {
107     dprint (2, (debugfile, "Invalid response from server: %s\n", buf1));
108     gss_release_name (&min_stat, &target_name);
109     goto bail;
110   }
111
112   /* now start the security context initialisation loop... */
113   dprint (2, (debugfile, "Sending credentials\n"));
114   mutt_to_base64 ((unsigned char *) buf1, send_token.value, send_token.length,
115                   sizeof (buf1) - 2);
116   gss_release_buffer (&min_stat, &send_token);
117   safe_strcat (buf1, sizeof (buf1), "\r\n");
118   mutt_socket_write (idata->conn, buf1);
119
120   while (maj_stat == GSS_S_CONTINUE_NEEDED) {
121     /* Read server data */
122     do
123       rc = imap_cmd_step (idata);
124     while (rc == IMAP_CMD_CONTINUE);
125
126     if (rc != IMAP_CMD_RESPOND) {
127       dprint (1, (debugfile, "Error receiving server response.\n"));
128       gss_release_name (&min_stat, &target_name);
129       goto bail;
130     }
131
132     request_buf.length = mutt_from_base64 (buf2, idata->cmd.buf + 2);
133     request_buf.value = buf2;
134     sec_token = &request_buf;
135
136     /* Write client data */
137     maj_stat = gss_init_sec_context (&min_stat, GSS_C_NO_CREDENTIAL, &context,
138                                      target_name, GSS_C_NO_OID,
139                                      GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG,
140                                      0, GSS_C_NO_CHANNEL_BINDINGS, sec_token,
141                                      NULL, &send_token,
142                                      (unsigned int *) &cflags, NULL);
143     if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) {
144       dprint (1, (debugfile, "Error exchanging credentials\n"));
145       gss_release_name (&min_stat, &target_name);
146
147       goto err_abort_cmd;
148     }
149     mutt_to_base64 ((unsigned char *) buf1, send_token.value,
150                     send_token.length, sizeof (buf1) - 2);
151     gss_release_buffer (&min_stat, &send_token);
152     safe_strcat (buf1, sizeof (buf1), "\r\n");
153     mutt_socket_write (idata->conn, buf1);
154   }
155
156   gss_release_name (&min_stat, &target_name);
157
158   /* get security flags and buffer size */
159   do
160     rc = imap_cmd_step (idata);
161   while (rc == IMAP_CMD_CONTINUE);
162
163   if (rc != IMAP_CMD_RESPOND) {
164     dprint (1, (debugfile, "Error receiving server response.\n"));
165     goto bail;
166   }
167   request_buf.length = mutt_from_base64 (buf2, idata->cmd.buf + 2);
168   request_buf.value = buf2;
169
170   maj_stat = gss_unwrap (&min_stat, context, &request_buf, &send_token,
171                          &cflags, &quality);
172   if (maj_stat != GSS_S_COMPLETE) {
173     dprint (2, (debugfile, "Couldn't unwrap security level data\n"));
174     gss_release_buffer (&min_stat, &send_token);
175     goto err_abort_cmd;
176   }
177   dprint (2, (debugfile, "Credential exchange complete\n"));
178
179   /* first octet is security levels supported. We want NONE */
180   server_conf_flags = ((char *) send_token.value)[0];
181   if (!(((char *) send_token.value)[0] & GSS_AUTH_P_NONE)) {
182     dprint (2, (debugfile, "Server requires integrity or privacy\n"));
183     gss_release_buffer (&min_stat, &send_token);
184     goto err_abort_cmd;
185   }
186
187   /* we don't care about buffer size if we don't wrap content. But here it is */
188   ((char *) send_token.value)[0] = 0;
189   buf_size = ntohl (*((long *) send_token.value));
190   gss_release_buffer (&min_stat, &send_token);
191   dprint (2, (debugfile, "Unwrapped security level flags: %c%c%c\n",
192               server_conf_flags & GSS_AUTH_P_NONE ? 'N' : '-',
193               server_conf_flags & GSS_AUTH_P_INTEGRITY ? 'I' : '-',
194               server_conf_flags & GSS_AUTH_P_PRIVACY ? 'P' : '-'));
195   dprint (2, (debugfile, "Maximum GSS token size is %ld\n", buf_size));
196
197   /* agree to terms (hack!) */
198   buf_size = htonl (buf_size);  /* not relevant without integrity/privacy */
199   memcpy (buf1, &buf_size, 4);
200   buf1[0] = GSS_AUTH_P_NONE;
201   /* server decides if principal can log in as user */
202   strncpy (buf1 + 4, idata->conn->account.user, sizeof (buf1) - 4);
203   request_buf.value = buf1;
204   request_buf.length = 4 + safe_strlen (idata->conn->account.user) + 1;
205   maj_stat = gss_wrap (&min_stat, context, 0, GSS_C_QOP_DEFAULT, &request_buf,
206                        &cflags, &send_token);
207   if (maj_stat != GSS_S_COMPLETE) {
208     dprint (2, (debugfile, "Error creating login request\n"));
209     goto err_abort_cmd;
210   }
211
212   mutt_to_base64 ((unsigned char *) buf1, send_token.value, send_token.length,
213                   sizeof (buf1) - 2);
214   dprint (2, (debugfile, "Requesting authorisation as %s\n",
215               idata->conn->account.user));
216   safe_strcat (buf1, sizeof (buf1), "\r\n");
217   mutt_socket_write (idata->conn, buf1);
218
219   /* Joy of victory or agony of defeat? */
220   do
221     rc = imap_cmd_step (idata);
222   while (rc == IMAP_CMD_CONTINUE);
223   if (rc == IMAP_CMD_RESPOND) {
224     dprint (1, (debugfile, "Unexpected server continuation request.\n"));
225     goto err_abort_cmd;
226   }
227   if (imap_code (idata->cmd.buf)) {
228     /* flush the security context */
229     dprint (2, (debugfile, "Releasing GSS credentials\n"));
230     maj_stat = gss_delete_sec_context (&min_stat, &context, &send_token);
231     if (maj_stat != GSS_S_COMPLETE)
232       dprint (1, (debugfile, "Error releasing credentials\n"));
233
234     /* send_token may contain a notification to the server to flush
235      * credentials. RFC 1731 doesn't specify what to do, and since this
236      * support is only for authentication, we'll assume the server knows
237      * enough to flush its own credentials */
238     gss_release_buffer (&min_stat, &send_token);
239
240     return IMAP_AUTH_SUCCESS;
241   }
242   else
243     goto bail;
244
245 err_abort_cmd:
246   mutt_socket_write (idata->conn, "*\r\n");
247   do
248     rc = imap_cmd_step (idata);
249   while (rc == IMAP_CMD_CONTINUE);
250
251 bail:
252   mutt_error _("GSSAPI authentication failed.");
253   mutt_sleep (2);
254   return IMAP_AUTH_FAILURE;
255 }