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