r4494: Allow gensec_gssapi to use the SPNEGO mech provided by Heimdal (off by
[samba.git] / source4 / libcli / auth / gensec_gssapi.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Kerberos backend for GENSEC
5    
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "system/kerberos.h"
26 #include "system/time.h"
27 #include "libcli/auth/kerberos.h"
28 #include "auth/auth.h"
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_AUTH
32
33 struct gensec_gssapi_state {
34         gss_ctx_id_t gssapi_context;
35         struct gss_channel_bindings_struct *input_chan_bindings;
36         gss_name_t server_name;
37         gss_name_t client_name;
38         int want_flags, got_flags;
39         const gss_OID_desc *gss_oid;
40 };
41 static int gensec_gssapi_destory(void *ptr) 
42 {
43         struct gensec_gssapi_state *gensec_gssapi_state = ptr;
44         OM_uint32 maj_stat, min_stat;
45
46         if (gensec_gssapi_state->gssapi_context != GSS_C_NO_CONTEXT) {
47                 maj_stat = gss_delete_sec_context (&min_stat,
48                                                    &gensec_gssapi_state->gssapi_context,
49                                                    GSS_C_NO_BUFFER);
50         }
51
52         if (gensec_gssapi_state->server_name != GSS_C_NO_NAME) {
53                 maj_stat = gss_release_name(&min_stat, &gensec_gssapi_state->server_name);
54         }
55         if (gensec_gssapi_state->client_name != GSS_C_NO_NAME) {
56                 maj_stat = gss_release_name(&min_stat, &gensec_gssapi_state->client_name);
57         }
58         return 0;
59 }
60
61 static NTSTATUS gensec_gssapi_start(struct gensec_security *gensec_security)
62 {
63         struct gensec_gssapi_state *gensec_gssapi_state;
64
65         gensec_gssapi_state = talloc_p(gensec_security, struct gensec_gssapi_state);
66         if (!gensec_gssapi_state) {
67                 return NT_STATUS_NO_MEMORY;
68         }
69
70         gensec_security->private_data = gensec_gssapi_state;
71
72         gensec_gssapi_state->gssapi_context = GSS_C_NO_CONTEXT;
73         gensec_gssapi_state->server_name = GSS_C_NO_NAME;
74         gensec_gssapi_state->client_name = GSS_C_NO_NAME;
75
76         talloc_set_destructor(gensec_gssapi_state, gensec_gssapi_destory); 
77
78         /* TODO: Fill in channel bindings */
79         gensec_gssapi_state->input_chan_bindings = GSS_C_NO_CHANNEL_BINDINGS;
80         
81         gensec_gssapi_state->want_flags = 0;
82         gensec_gssapi_state->got_flags = 0;
83
84         if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
85                 /* GSSAPI won't give us the session keys */
86                 return NT_STATUS_INVALID_PARAMETER;
87         }
88         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
89                 gensec_gssapi_state->want_flags |= GSS_C_INTEG_FLAG;
90         }
91         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
92                 gensec_gssapi_state->want_flags |= GSS_C_CONF_FLAG;
93         }
94
95         if (strcmp(gensec_security->ops->oid, GENSEC_OID_KERBEROS5) == 0) {
96                 static const gss_OID_desc gensec_gss_krb5_mechanism_oid_desc =
97                         {9, (void *)discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02")};
98
99                 gensec_gssapi_state->gss_oid = &gensec_gss_krb5_mechanism_oid_desc;
100         } else if (strcmp(gensec_security->ops->oid, GENSEC_OID_SPNEGO) == 0) {
101                 static const gss_OID_desc gensec_gss_spnego_mechanism_oid_desc =
102                         {6, (void *)discard_const_p(char, "\x2b\x06\x01\x05\x05\x02")};
103                 gensec_gssapi_state->gss_oid = &gensec_gss_spnego_mechanism_oid_desc;
104         } else {
105                 return NT_STATUS_INVALID_PARAMETER;
106         }
107
108         return NT_STATUS_OK;
109 }
110
111 static NTSTATUS gensec_gssapi_server_start(struct gensec_security *gensec_security)
112 {
113         NTSTATUS nt_status;
114         struct gensec_gssapi_state *gensec_gssapi_state;
115
116         nt_status = gensec_gssapi_start(gensec_security);
117         if (!NT_STATUS_IS_OK(nt_status)) {
118                 return nt_status;
119         }
120
121         gensec_gssapi_state = gensec_security->private_data;
122
123         return NT_STATUS_OK;
124 }
125
126 static NTSTATUS gensec_gssapi_client_start(struct gensec_security *gensec_security)
127 {
128         struct gensec_gssapi_state *gensec_gssapi_state;
129         NTSTATUS nt_status;
130         gss_buffer_desc name_token;
131         OM_uint32 maj_stat, min_stat;
132
133         nt_status = gensec_gssapi_start(gensec_security);
134         if (!NT_STATUS_IS_OK(nt_status)) {
135                 return nt_status;
136         }
137
138         gensec_gssapi_state = gensec_security->private_data;
139
140         name_token.value = talloc_asprintf(gensec_gssapi_state, "%s@%s", gensec_get_target_service(gensec_security), 
141                                            gensec_get_target_hostname(gensec_security));
142         DEBUG(0, ("name: %s\n", (char *)name_token.value));
143         name_token.length = strlen(name_token.value);
144
145         maj_stat = gss_import_name (&min_stat,
146                                     &name_token,
147                                     GSS_C_NT_HOSTBASED_SERVICE,
148                                     &gensec_gssapi_state->server_name);
149
150
151         if (maj_stat) {
152                 return NT_STATUS_UNSUCCESSFUL;
153         }
154         return NT_STATUS_OK;
155 }
156
157
158
159 /**
160  * Next state function for the GSSAPI GENSEC mechanism
161  * 
162  * @param gensec_gssapi_state GSSAPI State
163  * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
164  * @param in The request, as a DATA_BLOB
165  * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
166  * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent, 
167  *                or NT_STATUS_OK if the user is authenticated. 
168  */
169
170 static NTSTATUS gensec_gssapi_update(struct gensec_security *gensec_security, 
171                                    TALLOC_CTX *out_mem_ctx, 
172                                    const DATA_BLOB in, DATA_BLOB *out) 
173 {
174         struct gensec_gssapi_state *gensec_gssapi_state = gensec_security->private_data;
175         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
176         OM_uint32 maj_stat, min_stat;
177         OM_uint32 min_stat2;
178         gss_buffer_desc input_token, output_token;
179         gss_OID gss_oid_p;
180         input_token.length = in.length;
181         input_token.value = in.data;
182
183         switch (gensec_security->gensec_role) {
184         case GENSEC_CLIENT:
185         {
186                 maj_stat = gss_init_sec_context(&min_stat, 
187                                                 GSS_C_NO_CREDENTIAL, 
188                                                 &gensec_gssapi_state->gssapi_context, 
189                                                 gensec_gssapi_state->server_name, 
190                                                 discard_const_p(gss_OID_desc, gensec_gssapi_state->gss_oid),
191                                                 gensec_gssapi_state->want_flags, 
192                                                 0, 
193                                                 gensec_gssapi_state->input_chan_bindings,
194                                                 &input_token, 
195                                                 NULL, 
196                                                 &output_token, 
197                                                 &gensec_gssapi_state->got_flags, /* ret flags */
198                                                 NULL);
199                 break;
200         }
201         case GENSEC_SERVER:
202         {
203                 maj_stat = gss_accept_sec_context(&min_stat, 
204                                                   gensec_gssapi_state->gssapi_context, 
205                                                   GSS_C_NO_CREDENTIAL, 
206                                                   &input_token, 
207                                                   gensec_gssapi_state->input_chan_bindings,
208                                                   &gensec_gssapi_state->client_name, 
209                                                   &gss_oid_p,
210                                                   &output_token, 
211                                                   &gensec_gssapi_state->got_flags, 
212                                                   NULL, 
213                                                   NULL);
214                 gensec_gssapi_state->gss_oid = gss_oid_p;
215                 break;
216         }
217         default:
218                 return NT_STATUS_INVALID_PARAMETER;
219                 
220         }
221
222         *out = data_blob_talloc(out_mem_ctx, output_token.value, output_token.length);
223         gss_release_buffer(&min_stat2, &output_token);
224
225         if (maj_stat == GSS_S_COMPLETE) {
226                 return NT_STATUS_OK;
227         } else if (maj_stat == GSS_S_CONTINUE_NEEDED) {
228                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
229         } else {
230                 gss_buffer_desc msg1, msg2;
231                 OM_uint32 msg_ctx = 0;
232                 
233                 msg1.value = NULL;
234                 msg2.value = NULL;
235                 gss_display_status(&min_stat2, maj_stat, GSS_C_GSS_CODE,
236                                    GSS_C_NULL_OID, &msg_ctx, &msg1);
237                 gss_display_status(&min_stat2, min_stat, GSS_C_MECH_CODE,
238                                    GSS_C_NULL_OID, &msg_ctx, &msg2);
239                 DEBUG(1, ("gensec_gssapi_update: %s : %s\n", (char *)msg1.value, (char *)msg2.value));
240                 gss_release_buffer(&min_stat2, &msg1);
241                 gss_release_buffer(&min_stat2, &msg2);
242
243                 return nt_status;
244         }
245         
246 }
247
248 static NTSTATUS gensec_gssapi_wrap(struct gensec_security *gensec_security, 
249                                    TALLOC_CTX *mem_ctx, 
250                                    const DATA_BLOB *in, 
251                                    DATA_BLOB *out)
252 {
253         struct gensec_gssapi_state *gensec_gssapi_state = gensec_security->private_data;
254         OM_uint32 maj_stat, min_stat;
255         gss_buffer_desc input_token, output_token;
256         int conf_state;
257         input_token.length = in->length;
258         input_token.value = in->data;
259         
260         maj_stat = gss_wrap(&min_stat, 
261                             gensec_gssapi_state->gssapi_context, 
262                             gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL),
263                             GSS_C_QOP_DEFAULT,
264                             &input_token,
265                             &conf_state,
266                             &output_token);
267         if (GSS_ERROR(maj_stat)) {
268                 return NT_STATUS_ACCESS_DENIED;
269         }
270         *out = data_blob_talloc(mem_ctx, output_token.value, output_token.length);
271
272         gss_release_buffer(&min_stat, &output_token);
273
274         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)
275             && !conf_state) {
276                 return NT_STATUS_ACCESS_DENIED;
277         }
278         return NT_STATUS_OK;
279 }
280
281 static NTSTATUS gensec_gssapi_unwrap(struct gensec_security *gensec_security, 
282                                      TALLOC_CTX *mem_ctx, 
283                                      const DATA_BLOB *in, 
284                                      DATA_BLOB *out)
285 {
286         struct gensec_gssapi_state *gensec_gssapi_state = gensec_security->private_data;
287         OM_uint32 maj_stat, min_stat;
288         gss_buffer_desc input_token, output_token;
289         int conf_state;
290         gss_qop_t qop_state;
291         input_token.length = in->length;
292         input_token.value = in->data;
293         
294         maj_stat = gss_unwrap(&min_stat, 
295                               gensec_gssapi_state->gssapi_context, 
296                               &input_token,
297                               &output_token, 
298                               &conf_state,
299                               &qop_state);
300         if (GSS_ERROR(maj_stat)) {
301                 return NT_STATUS_ACCESS_DENIED;
302         }
303         *out = data_blob_talloc(mem_ctx, output_token.value, output_token.length);
304
305         gss_release_buffer(&min_stat, &output_token);
306         
307         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)
308             && !conf_state) {
309                 return NT_STATUS_ACCESS_DENIED;
310         }
311         return NT_STATUS_OK;
312 }
313
314 static BOOL gensec_gssapi_have_feature(struct gensec_security *gensec_security, 
315                                        uint32 feature) 
316 {
317         struct gensec_gssapi_state *gensec_gssapi_state = gensec_security->private_data;
318         if (feature & GENSEC_FEATURE_SIGN) {
319                 return gensec_gssapi_state->got_flags & GSS_C_INTEG_FLAG;
320         }
321         if (feature & GENSEC_FEATURE_SEAL) {
322                 return gensec_gssapi_state->got_flags & GSS_C_CONF_FLAG;
323         }
324         return False;
325 }
326
327 /* As a server, this could in theory accept any GSSAPI mech */
328 static const struct gensec_security_ops gensec_gssapi_krb5_security_ops = {
329         .name           = "gssapi_krb5",
330         .sasl_name      = "GSSAPI",
331         .oid            = GENSEC_OID_KERBEROS5,
332         .client_start   = gensec_gssapi_client_start,
333         .server_start   = gensec_gssapi_server_start,
334         .update         = gensec_gssapi_update,
335         .wrap           = gensec_gssapi_wrap,
336         .unwrap         = gensec_gssapi_unwrap,
337         .have_feature   = gensec_gssapi_have_feature
338
339 };
340
341 static const struct gensec_security_ops gensec_gssapi_spnego_security_ops = {
342         .name           = "gssapi_spnego",
343         .sasl_name      = "GSS-SPNEGO",
344         .oid            = GENSEC_OID_SPNEGO,
345         .client_start   = gensec_gssapi_client_start,
346         .server_start   = gensec_gssapi_server_start,
347         .update         = gensec_gssapi_update,
348         .wrap           = gensec_gssapi_wrap,
349         .unwrap         = gensec_gssapi_unwrap,
350         .have_feature   = gensec_gssapi_have_feature
351
352 };
353
354 NTSTATUS gensec_gssapi_init(void)
355 {
356         NTSTATUS ret;
357
358         ret = gensec_register(&gensec_gssapi_krb5_security_ops);
359         if (!NT_STATUS_IS_OK(ret)) {
360                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
361                         gensec_gssapi_krb5_security_ops.name));
362                 return ret;
363         }
364
365         ret = gensec_register(&gensec_gssapi_spnego_security_ops);
366         if (!NT_STATUS_IS_OK(ret)) {
367                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
368                         gensec_gssapi_spnego_security_ops.name));
369                 return ret;
370         }
371
372         return ret;
373 }