r4531: Include the OID locally, as it seems to be hard to get the includes
[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         gss_OID_desc hostbased = {10, 
134                                   (void *)discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12"
135                                                           "\x01\x02\x01\x04")};
136
137         nt_status = gensec_gssapi_start(gensec_security);
138         if (!NT_STATUS_IS_OK(nt_status)) {
139                 return nt_status;
140         }
141
142         gensec_gssapi_state = gensec_security->private_data;
143
144         name_token.value = talloc_asprintf(gensec_gssapi_state, "%s@%s", gensec_get_target_service(gensec_security), 
145                                            gensec_get_target_hostname(gensec_security));
146         DEBUG(0, ("name: %s\n", (char *)name_token.value));
147         name_token.length = strlen(name_token.value);
148
149         maj_stat = gss_import_name (&min_stat,
150                                     &name_token,
151                                     &hostbased,
152                                     &gensec_gssapi_state->server_name);
153
154
155         if (maj_stat) {
156                 return NT_STATUS_UNSUCCESSFUL;
157         }
158         return NT_STATUS_OK;
159 }
160
161
162
163 /**
164  * Next state function for the GSSAPI GENSEC mechanism
165  * 
166  * @param gensec_gssapi_state GSSAPI State
167  * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
168  * @param in The request, as a DATA_BLOB
169  * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
170  * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent, 
171  *                or NT_STATUS_OK if the user is authenticated. 
172  */
173
174 static NTSTATUS gensec_gssapi_update(struct gensec_security *gensec_security, 
175                                    TALLOC_CTX *out_mem_ctx, 
176                                    const DATA_BLOB in, DATA_BLOB *out) 
177 {
178         struct gensec_gssapi_state *gensec_gssapi_state = gensec_security->private_data;
179         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
180         OM_uint32 maj_stat, min_stat;
181         OM_uint32 min_stat2;
182         gss_buffer_desc input_token, output_token;
183         gss_OID gss_oid_p;
184         input_token.length = in.length;
185         input_token.value = in.data;
186
187         switch (gensec_security->gensec_role) {
188         case GENSEC_CLIENT:
189         {
190                 maj_stat = gss_init_sec_context(&min_stat, 
191                                                 GSS_C_NO_CREDENTIAL, 
192                                                 &gensec_gssapi_state->gssapi_context, 
193                                                 gensec_gssapi_state->server_name, 
194                                                 discard_const_p(gss_OID_desc, gensec_gssapi_state->gss_oid),
195                                                 gensec_gssapi_state->want_flags, 
196                                                 0, 
197                                                 gensec_gssapi_state->input_chan_bindings,
198                                                 &input_token, 
199                                                 NULL, 
200                                                 &output_token, 
201                                                 &gensec_gssapi_state->got_flags, /* ret flags */
202                                                 NULL);
203                 break;
204         }
205         case GENSEC_SERVER:
206         {
207                 maj_stat = gss_accept_sec_context(&min_stat, 
208                                                   gensec_gssapi_state->gssapi_context, 
209                                                   GSS_C_NO_CREDENTIAL, 
210                                                   &input_token, 
211                                                   gensec_gssapi_state->input_chan_bindings,
212                                                   &gensec_gssapi_state->client_name, 
213                                                   &gss_oid_p,
214                                                   &output_token, 
215                                                   &gensec_gssapi_state->got_flags, 
216                                                   NULL, 
217                                                   NULL);
218                 gensec_gssapi_state->gss_oid = gss_oid_p;
219                 break;
220         }
221         default:
222                 return NT_STATUS_INVALID_PARAMETER;
223                 
224         }
225
226         *out = data_blob_talloc(out_mem_ctx, output_token.value, output_token.length);
227         gss_release_buffer(&min_stat2, &output_token);
228
229         if (maj_stat == GSS_S_COMPLETE) {
230                 return NT_STATUS_OK;
231         } else if (maj_stat == GSS_S_CONTINUE_NEEDED) {
232                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
233         } else {
234                 gss_buffer_desc msg1, msg2;
235                 OM_uint32 msg_ctx = 0;
236                 
237                 msg1.value = NULL;
238                 msg2.value = NULL;
239                 gss_display_status(&min_stat2, maj_stat, GSS_C_GSS_CODE,
240                                    GSS_C_NULL_OID, &msg_ctx, &msg1);
241                 gss_display_status(&min_stat2, min_stat, GSS_C_MECH_CODE,
242                                    GSS_C_NULL_OID, &msg_ctx, &msg2);
243                 DEBUG(1, ("gensec_gssapi_update: %s : %s\n", (char *)msg1.value, (char *)msg2.value));
244                 gss_release_buffer(&min_stat2, &msg1);
245                 gss_release_buffer(&min_stat2, &msg2);
246
247                 return nt_status;
248         }
249         
250 }
251
252 static NTSTATUS gensec_gssapi_wrap(struct gensec_security *gensec_security, 
253                                    TALLOC_CTX *mem_ctx, 
254                                    const DATA_BLOB *in, 
255                                    DATA_BLOB *out)
256 {
257         struct gensec_gssapi_state *gensec_gssapi_state = gensec_security->private_data;
258         OM_uint32 maj_stat, min_stat;
259         gss_buffer_desc input_token, output_token;
260         int conf_state;
261         input_token.length = in->length;
262         input_token.value = in->data;
263         
264         maj_stat = gss_wrap(&min_stat, 
265                             gensec_gssapi_state->gssapi_context, 
266                             gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL),
267                             GSS_C_QOP_DEFAULT,
268                             &input_token,
269                             &conf_state,
270                             &output_token);
271         if (GSS_ERROR(maj_stat)) {
272                 return NT_STATUS_ACCESS_DENIED;
273         }
274         *out = data_blob_talloc(mem_ctx, output_token.value, output_token.length);
275
276         gss_release_buffer(&min_stat, &output_token);
277
278         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)
279             && !conf_state) {
280                 return NT_STATUS_ACCESS_DENIED;
281         }
282         return NT_STATUS_OK;
283 }
284
285 static NTSTATUS gensec_gssapi_unwrap(struct gensec_security *gensec_security, 
286                                      TALLOC_CTX *mem_ctx, 
287                                      const DATA_BLOB *in, 
288                                      DATA_BLOB *out)
289 {
290         struct gensec_gssapi_state *gensec_gssapi_state = gensec_security->private_data;
291         OM_uint32 maj_stat, min_stat;
292         gss_buffer_desc input_token, output_token;
293         int conf_state;
294         gss_qop_t qop_state;
295         input_token.length = in->length;
296         input_token.value = in->data;
297         
298         maj_stat = gss_unwrap(&min_stat, 
299                               gensec_gssapi_state->gssapi_context, 
300                               &input_token,
301                               &output_token, 
302                               &conf_state,
303                               &qop_state);
304         if (GSS_ERROR(maj_stat)) {
305                 return NT_STATUS_ACCESS_DENIED;
306         }
307         *out = data_blob_talloc(mem_ctx, output_token.value, output_token.length);
308
309         gss_release_buffer(&min_stat, &output_token);
310         
311         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)
312             && !conf_state) {
313                 return NT_STATUS_ACCESS_DENIED;
314         }
315         return NT_STATUS_OK;
316 }
317
318 static BOOL gensec_gssapi_have_feature(struct gensec_security *gensec_security, 
319                                        uint32 feature) 
320 {
321         struct gensec_gssapi_state *gensec_gssapi_state = gensec_security->private_data;
322         if (feature & GENSEC_FEATURE_SIGN) {
323                 return gensec_gssapi_state->got_flags & GSS_C_INTEG_FLAG;
324         }
325         if (feature & GENSEC_FEATURE_SEAL) {
326                 return gensec_gssapi_state->got_flags & GSS_C_CONF_FLAG;
327         }
328         return False;
329 }
330
331 /* As a server, this could in theory accept any GSSAPI mech */
332 static const struct gensec_security_ops gensec_gssapi_krb5_security_ops = {
333         .name           = "gssapi_krb5",
334         .sasl_name      = "GSSAPI",
335         .oid            = GENSEC_OID_KERBEROS5,
336         .client_start   = gensec_gssapi_client_start,
337         .server_start   = gensec_gssapi_server_start,
338         .update         = gensec_gssapi_update,
339         .wrap           = gensec_gssapi_wrap,
340         .unwrap         = gensec_gssapi_unwrap,
341         .have_feature   = gensec_gssapi_have_feature,
342         .enabled        = False
343
344 };
345
346 static const struct gensec_security_ops gensec_gssapi_spnego_security_ops = {
347         .name           = "gssapi_spnego",
348         .sasl_name      = "GSS-SPNEGO",
349         .oid            = GENSEC_OID_SPNEGO,
350         .client_start   = gensec_gssapi_client_start,
351         .server_start   = gensec_gssapi_server_start,
352         .update         = gensec_gssapi_update,
353         .wrap           = gensec_gssapi_wrap,
354         .unwrap         = gensec_gssapi_unwrap,
355         .have_feature   = gensec_gssapi_have_feature,
356         .enabled        = False
357 };
358
359 NTSTATUS gensec_gssapi_init(void)
360 {
361         NTSTATUS ret;
362
363         ret = gensec_register(&gensec_gssapi_krb5_security_ops);
364         if (!NT_STATUS_IS_OK(ret)) {
365                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
366                         gensec_gssapi_krb5_security_ops.name));
367                 return ret;
368         }
369
370         ret = gensec_register(&gensec_gssapi_spnego_security_ops);
371         if (!NT_STATUS_IS_OK(ret)) {
372                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
373                         gensec_gssapi_spnego_security_ops.name));
374                 return ret;
375         }
376
377         return ret;
378 }