lib/replace: split out GSSAPI from lib/replace/system/kerberos.h into lib/replace...
[sfrench/samba-autobuild/.git] / source4 / auth / gensec / 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-2005
7    Copyright (C) Stefan Metzmacher <metze@samba.org> 2004-2005
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "lib/events/events.h"
26 #include "system/kerberos.h"
27 #include "system/gssapi.h"
28 #include "auth/kerberos/kerberos.h"
29 #include "librpc/gen_ndr/krb5pac.h"
30 #include "auth/auth.h"
31 #include <ldb.h>
32 #include "auth/auth_sam.h"
33 #include "librpc/rpc/dcerpc.h"
34 #include "auth/credentials/credentials.h"
35 #include "auth/credentials/credentials_krb5.h"
36 #include "auth/gensec/gensec.h"
37 #include "auth/gensec/gensec_proto.h"
38 #include "auth/gensec/gensec_toplevel_proto.h"
39 #include "param/param.h"
40 #include "auth/session_proto.h"
41 #include "gensec_gssapi.h"
42 #include "lib/util/util_net.h"
43 #include "auth/kerberos/pac_utils.h"
44
45 _PUBLIC_ NTSTATUS gensec_gssapi_init(void);
46
47 static size_t gensec_gssapi_max_input_size(struct gensec_security *gensec_security);
48 static size_t gensec_gssapi_max_wrapped_size(struct gensec_security *gensec_security);
49
50 static int gensec_gssapi_destructor(struct gensec_gssapi_state *gensec_gssapi_state)
51 {
52         OM_uint32 maj_stat, min_stat;
53         
54         if (gensec_gssapi_state->delegated_cred_handle != GSS_C_NO_CREDENTIAL) {
55                 maj_stat = gss_release_cred(&min_stat, 
56                                             &gensec_gssapi_state->delegated_cred_handle);
57         }
58
59         if (gensec_gssapi_state->gssapi_context != GSS_C_NO_CONTEXT) {
60                 maj_stat = gss_delete_sec_context (&min_stat,
61                                                    &gensec_gssapi_state->gssapi_context,
62                                                    GSS_C_NO_BUFFER);
63         }
64
65         if (gensec_gssapi_state->server_name != GSS_C_NO_NAME) {
66                 maj_stat = gss_release_name(&min_stat, &gensec_gssapi_state->server_name);
67         }
68         if (gensec_gssapi_state->client_name != GSS_C_NO_NAME) {
69                 maj_stat = gss_release_name(&min_stat, &gensec_gssapi_state->client_name);
70         }
71
72         if (gensec_gssapi_state->lucid) {
73                 gss_krb5_free_lucid_sec_context(&min_stat, gensec_gssapi_state->lucid);
74         }
75
76         return 0;
77 }
78
79 static NTSTATUS gensec_gssapi_init_lucid(struct gensec_gssapi_state *gensec_gssapi_state)
80 {
81         OM_uint32 maj_stat, min_stat;
82
83         if (gensec_gssapi_state->lucid) {
84                 return NT_STATUS_OK;
85         }
86
87         maj_stat = gss_krb5_export_lucid_sec_context(&min_stat,
88                                                      &gensec_gssapi_state->gssapi_context,
89                                                      1,
90                                                      (void **)&gensec_gssapi_state->lucid);
91         if (maj_stat != GSS_S_COMPLETE) {
92                 DEBUG(0,("gensec_gssapi_init_lucid: %s\n",
93                         gssapi_error_string(gensec_gssapi_state,
94                                             maj_stat, min_stat,
95                                             gensec_gssapi_state->gss_oid)));
96                 return NT_STATUS_INTERNAL_ERROR;
97         }
98
99         if (gensec_gssapi_state->lucid->version != 1) {
100                 DEBUG(0,("gensec_gssapi_init_lucid: lucid version[%d] != 1\n",
101                         gensec_gssapi_state->lucid->version));
102                 gss_krb5_free_lucid_sec_context(&min_stat, gensec_gssapi_state->lucid);
103                 gensec_gssapi_state->lucid = NULL;
104                 return NT_STATUS_INTERNAL_ERROR;
105         }
106
107         return NT_STATUS_OK;
108 }
109
110 static NTSTATUS gensec_gssapi_start(struct gensec_security *gensec_security)
111 {
112         struct gensec_gssapi_state *gensec_gssapi_state;
113         krb5_error_code ret;
114         const char *realm;
115
116         gensec_gssapi_state = talloc_zero(gensec_security, struct gensec_gssapi_state);
117         if (!gensec_gssapi_state) {
118                 return NT_STATUS_NO_MEMORY;
119         }
120
121         gensec_security->private_data = gensec_gssapi_state;
122
123         gensec_gssapi_state->gssapi_context = GSS_C_NO_CONTEXT;
124
125         /* TODO: Fill in channel bindings */
126         gensec_gssapi_state->input_chan_bindings = GSS_C_NO_CHANNEL_BINDINGS;
127
128         gensec_gssapi_state->server_name = GSS_C_NO_NAME;
129         gensec_gssapi_state->client_name = GSS_C_NO_NAME;
130         
131         gensec_gssapi_state->gss_want_flags = 0;
132
133         if (gensec_setting_bool(gensec_security->settings, "gensec_gssapi", "delegation_by_kdc_policy", true)) {
134                 gensec_gssapi_state->gss_want_flags |= GSS_C_DELEG_POLICY_FLAG;
135         }
136         if (gensec_setting_bool(gensec_security->settings, "gensec_gssapi", "mutual", true)) {
137                 gensec_gssapi_state->gss_want_flags |= GSS_C_MUTUAL_FLAG;
138         }
139         if (gensec_setting_bool(gensec_security->settings, "gensec_gssapi", "delegation", true)) {
140                 gensec_gssapi_state->gss_want_flags |= GSS_C_DELEG_FLAG;
141         }
142         if (gensec_setting_bool(gensec_security->settings, "gensec_gssapi", "replay", true)) {
143                 gensec_gssapi_state->gss_want_flags |= GSS_C_REPLAY_FLAG;
144         }
145         if (gensec_setting_bool(gensec_security->settings, "gensec_gssapi", "sequence", true)) {
146                 gensec_gssapi_state->gss_want_flags |= GSS_C_SEQUENCE_FLAG;
147         }
148
149         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
150                 gensec_gssapi_state->gss_want_flags |= GSS_C_INTEG_FLAG;
151         }
152         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
153                 gensec_gssapi_state->gss_want_flags |= GSS_C_INTEG_FLAG;
154                 gensec_gssapi_state->gss_want_flags |= GSS_C_CONF_FLAG;
155         }
156         if (gensec_security->want_features & GENSEC_FEATURE_DCE_STYLE) {
157                 gensec_gssapi_state->gss_want_flags |= GSS_C_DCE_STYLE;
158         }
159
160         gensec_gssapi_state->gss_got_flags = 0;
161
162         switch (gensec_security->ops->auth_type) {
163         case DCERPC_AUTH_TYPE_SPNEGO:
164                 gensec_gssapi_state->gss_oid = gss_mech_spnego;
165                 break;
166         case DCERPC_AUTH_TYPE_KRB5:
167         default:
168                 gensec_gssapi_state->gss_oid = gss_mech_krb5;
169                 break;
170         }
171
172         ret = smb_krb5_init_context(gensec_gssapi_state,
173                                     NULL,
174                                     gensec_security->settings->lp_ctx,
175                                     &gensec_gssapi_state->smb_krb5_context);
176         if (ret) {
177                 DEBUG(1,("gensec_krb5_start: krb5_init_context failed (%s)\n",
178                          error_message(ret)));
179                 talloc_free(gensec_gssapi_state);
180                 return NT_STATUS_INTERNAL_ERROR;
181         }
182
183         gensec_gssapi_state->client_cred = NULL;
184         gensec_gssapi_state->server_cred = NULL;
185
186         gensec_gssapi_state->lucid = NULL;
187
188         gensec_gssapi_state->delegated_cred_handle = GSS_C_NO_CREDENTIAL;
189
190         gensec_gssapi_state->sasl = false;
191         gensec_gssapi_state->sasl_state = STAGE_GSS_NEG;
192         gensec_gssapi_state->sasl_protection = 0;
193
194         gensec_gssapi_state->max_wrap_buf_size
195                 = gensec_setting_int(gensec_security->settings, "gensec_gssapi", "max wrap buf size", 65536);
196         gensec_gssapi_state->gss_exchange_count = 0;
197         gensec_gssapi_state->sig_size = 0;
198
199         talloc_set_destructor(gensec_gssapi_state, gensec_gssapi_destructor);
200
201         realm = lpcfg_realm(gensec_security->settings->lp_ctx);
202         if (realm != NULL) {
203                 ret = gsskrb5_set_default_realm(realm);
204                 if (ret) {
205                         DEBUG(1,("gensec_krb5_start: gsskrb5_set_default_realm failed\n"));
206                         talloc_free(gensec_gssapi_state);
207                         return NT_STATUS_INTERNAL_ERROR;
208                 }
209         }
210
211         /* don't do DNS lookups of any kind, it might/will fail for a netbios name */
212         ret = gsskrb5_set_dns_canonicalize(gensec_setting_bool(gensec_security->settings, "krb5", "set_dns_canonicalize", false));
213         if (ret) {
214                 DEBUG(1,("gensec_krb5_start: gsskrb5_set_dns_canonicalize failed\n"));
215                 talloc_free(gensec_gssapi_state);
216                 return NT_STATUS_INTERNAL_ERROR;
217         }
218
219         return NT_STATUS_OK;
220 }
221
222 static NTSTATUS gensec_gssapi_server_start(struct gensec_security *gensec_security)
223 {
224         NTSTATUS nt_status;
225         int ret;
226         struct gensec_gssapi_state *gensec_gssapi_state;
227         struct cli_credentials *machine_account;
228         struct gssapi_creds_container *gcc;
229
230         nt_status = gensec_gssapi_start(gensec_security);
231         if (!NT_STATUS_IS_OK(nt_status)) {
232                 return nt_status;
233         }
234
235         gensec_gssapi_state = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
236
237         machine_account = gensec_get_credentials(gensec_security);
238         
239         if (!machine_account) {
240                 DEBUG(3, ("No machine account credentials specified\n"));
241                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
242         } else {
243                 ret = cli_credentials_get_server_gss_creds(machine_account, 
244                                                            gensec_security->settings->lp_ctx, &gcc);
245                 if (ret) {
246                         DEBUG(1, ("Aquiring acceptor credentials failed: %s\n", 
247                                   error_message(ret)));
248                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
249                 }
250         }
251
252         gensec_gssapi_state->server_cred = gcc;
253         return NT_STATUS_OK;
254
255 }
256
257 static NTSTATUS gensec_gssapi_sasl_server_start(struct gensec_security *gensec_security)
258 {
259         NTSTATUS nt_status;
260         struct gensec_gssapi_state *gensec_gssapi_state;
261         nt_status = gensec_gssapi_server_start(gensec_security);
262
263         if (NT_STATUS_IS_OK(nt_status)) {
264                 gensec_gssapi_state = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
265                 gensec_gssapi_state->sasl = true;
266         }
267         return nt_status;
268 }
269
270 static NTSTATUS gensec_gssapi_client_creds(struct gensec_security *gensec_security,
271                                            struct tevent_context *ev)
272 {
273         struct gensec_gssapi_state *gensec_gssapi_state;
274         struct gssapi_creds_container *gcc;
275         struct cli_credentials *creds = gensec_get_credentials(gensec_security);
276         const char *error_string;
277         int ret;
278
279         gensec_gssapi_state = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
280
281         /* Only run this the first time the update() call is made */
282         if (gensec_gssapi_state->client_cred) {
283                 return NT_STATUS_OK;
284         }
285
286         ret = cli_credentials_get_client_gss_creds(creds,
287                                                    ev,
288                                                    gensec_security->settings->lp_ctx, &gcc, &error_string);
289         switch (ret) {
290         case 0:
291                 break;
292         case EINVAL:
293                 DEBUG(3, ("Cannot obtain client GSS credentials we need to contact %s : %s\n", gensec_gssapi_state->target_principal, error_string));
294                 return NT_STATUS_INVALID_PARAMETER;
295         case KRB5KDC_ERR_PREAUTH_FAILED:
296         case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN:
297                 DEBUG(1, ("Wrong username or password: %s\n", error_string));
298                 return NT_STATUS_LOGON_FAILURE;
299         case KRB5_KDC_UNREACH:
300                 DEBUG(3, ("Cannot reach a KDC we require to contact %s : %s\n", gensec_gssapi_state->target_principal, error_string));
301                 return NT_STATUS_NO_LOGON_SERVERS;
302         case KRB5_CC_NOTFOUND:
303         case KRB5_CC_END:
304                 DEBUG(2, ("Error obtaining ticket we require to contact %s: (possibly due to clock skew between us and the KDC) %s\n", gensec_gssapi_state->target_principal, error_string));
305                 return NT_STATUS_TIME_DIFFERENCE_AT_DC;
306         default:
307                 DEBUG(1, ("Aquiring initiator credentials failed: %s\n", error_string));
308                 return NT_STATUS_UNSUCCESSFUL;
309         }
310
311         gensec_gssapi_state->client_cred = gcc;
312         if (!talloc_reference(gensec_gssapi_state, gcc)) {
313                 return NT_STATUS_NO_MEMORY;
314         }
315
316         return NT_STATUS_OK;
317 }
318
319 static NTSTATUS gensec_gssapi_client_start(struct gensec_security *gensec_security)
320 {
321         struct gensec_gssapi_state *gensec_gssapi_state;
322         struct cli_credentials *creds = gensec_get_credentials(gensec_security);
323         NTSTATUS nt_status;
324         gss_buffer_desc name_token;
325         gss_OID name_type;
326         OM_uint32 maj_stat, min_stat;
327         const char *hostname = gensec_get_target_hostname(gensec_security);
328
329         if (!hostname) {
330                 DEBUG(1, ("Could not determine hostname for target computer, cannot use kerberos\n"));
331                 return NT_STATUS_INVALID_PARAMETER;
332         }
333         if (is_ipaddress(hostname)) {
334                 DEBUG(2, ("Cannot do GSSAPI to an IP address\n"));
335                 return NT_STATUS_INVALID_PARAMETER;
336         }
337         if (strcmp(hostname, "localhost") == 0) {
338                 DEBUG(2, ("GSSAPI to 'localhost' does not make sense\n"));
339                 return NT_STATUS_INVALID_PARAMETER;
340         }
341
342         nt_status = gensec_gssapi_start(gensec_security);
343         if (!NT_STATUS_IS_OK(nt_status)) {
344                 return nt_status;
345         }
346
347         gensec_gssapi_state = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
348
349         if (cli_credentials_get_impersonate_principal(creds)) {
350                 gensec_gssapi_state->gss_want_flags &= ~(GSS_C_DELEG_FLAG|GSS_C_DELEG_POLICY_FLAG);
351         }
352
353         gensec_gssapi_state->target_principal = gensec_get_target_principal(gensec_security);
354         if (gensec_gssapi_state->target_principal) {
355                 name_type = GSS_C_NULL_OID;
356         } else {
357                 gensec_gssapi_state->target_principal = talloc_asprintf(gensec_gssapi_state, "%s/%s@%s",
358                                             gensec_get_target_service(gensec_security), 
359                                             hostname, lpcfg_realm(gensec_security->settings->lp_ctx));
360
361                 name_type = GSS_C_NT_USER_NAME;
362         }
363         name_token.value  = discard_const_p(uint8_t, gensec_gssapi_state->target_principal);
364         name_token.length = strlen(gensec_gssapi_state->target_principal);
365
366
367         maj_stat = gss_import_name (&min_stat,
368                                     &name_token,
369                                     name_type,
370                                     &gensec_gssapi_state->server_name);
371         if (maj_stat) {
372                 DEBUG(2, ("GSS Import name of %s failed: %s\n",
373                           (char *)name_token.value,
374                           gssapi_error_string(gensec_gssapi_state, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
375                 return NT_STATUS_INVALID_PARAMETER;
376         }
377
378         return NT_STATUS_OK;
379 }
380
381 static NTSTATUS gensec_gssapi_sasl_client_start(struct gensec_security *gensec_security)
382 {
383         NTSTATUS nt_status;
384         struct gensec_gssapi_state *gensec_gssapi_state;
385         nt_status = gensec_gssapi_client_start(gensec_security);
386
387         if (NT_STATUS_IS_OK(nt_status)) {
388                 gensec_gssapi_state = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
389                 gensec_gssapi_state->sasl = true;
390         }
391         return nt_status;
392 }
393
394
395 /**
396  * Next state function for the GSSAPI GENSEC mechanism
397  * 
398  * @param gensec_gssapi_state GSSAPI State
399  * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
400  * @param in The request, as a DATA_BLOB
401  * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
402  * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent, 
403  *                or NT_STATUS_OK if the user is authenticated. 
404  */
405
406 static NTSTATUS gensec_gssapi_update(struct gensec_security *gensec_security, 
407                                      TALLOC_CTX *out_mem_ctx,
408                                      struct tevent_context *ev,
409                                      const DATA_BLOB in, DATA_BLOB *out)
410 {
411         struct gensec_gssapi_state *gensec_gssapi_state
412                 = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
413         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
414         OM_uint32 maj_stat, min_stat;
415         OM_uint32 min_stat2;
416         gss_buffer_desc input_token, output_token;
417         gss_OID gss_oid_p = NULL;
418         input_token.length = in.length;
419         input_token.value = in.data;
420
421         switch (gensec_gssapi_state->sasl_state) {
422         case STAGE_GSS_NEG:
423         {
424                 switch (gensec_security->gensec_role) {
425                 case GENSEC_CLIENT:
426                 {
427                         struct gsskrb5_send_to_kdc send_to_kdc;
428                         krb5_error_code ret;
429
430                         nt_status = gensec_gssapi_client_creds(gensec_security, ev);
431                         if (!NT_STATUS_IS_OK(nt_status)) {
432                                 return nt_status;
433                         }
434
435 #ifdef SAMBA4_USES_HEIMDAL
436                         send_to_kdc.func = smb_krb5_send_and_recv_func;
437                         send_to_kdc.ptr = ev;
438 #endif
439
440                         min_stat = gsskrb5_set_send_to_kdc(&send_to_kdc);
441                         if (min_stat) {
442                                 DEBUG(1,("gensec_krb5_start: gsskrb5_set_send_to_kdc failed\n"));
443                                 return NT_STATUS_INTERNAL_ERROR;
444                         }
445
446                         maj_stat = gss_init_sec_context(&min_stat, 
447                                                         gensec_gssapi_state->client_cred->creds,
448                                                         &gensec_gssapi_state->gssapi_context, 
449                                                         gensec_gssapi_state->server_name, 
450                                                         gensec_gssapi_state->gss_oid,
451                                                         gensec_gssapi_state->gss_want_flags, 
452                                                         0, 
453                                                         gensec_gssapi_state->input_chan_bindings,
454                                                         &input_token, 
455                                                         &gss_oid_p,
456                                                         &output_token, 
457                                                         &gensec_gssapi_state->gss_got_flags, /* ret flags */
458                                                         NULL);
459                         if (gss_oid_p) {
460                                 gensec_gssapi_state->gss_oid = gss_oid_p;
461                         }
462
463 #ifdef SAMBA4_USES_HEIMDAL
464                         send_to_kdc.func = smb_krb5_send_and_recv_func;
465                         send_to_kdc.ptr = NULL;
466 #endif
467
468                         ret = gsskrb5_set_send_to_kdc(&send_to_kdc);
469                         if (ret) {
470                                 DEBUG(1,("gensec_krb5_start: gsskrb5_set_send_to_kdc failed\n"));
471                                 return NT_STATUS_INTERNAL_ERROR;
472                         }
473
474                         break;
475                 }
476                 case GENSEC_SERVER:
477                 {
478                         maj_stat = gss_accept_sec_context(&min_stat, 
479                                                           &gensec_gssapi_state->gssapi_context, 
480                                                           gensec_gssapi_state->server_cred->creds,
481                                                           &input_token, 
482                                                           gensec_gssapi_state->input_chan_bindings,
483                                                           &gensec_gssapi_state->client_name, 
484                                                           &gss_oid_p,
485                                                           &output_token, 
486                                                           &gensec_gssapi_state->gss_got_flags, 
487                                                           NULL, 
488                                                           &gensec_gssapi_state->delegated_cred_handle);
489                         if (gss_oid_p) {
490                                 gensec_gssapi_state->gss_oid = gss_oid_p;
491                         }
492                         break;
493                 }
494                 default:
495                         return NT_STATUS_INVALID_PARAMETER;
496                         
497                 }
498
499                 gensec_gssapi_state->gss_exchange_count++;
500
501                 if (maj_stat == GSS_S_COMPLETE) {
502                         *out = data_blob_talloc(out_mem_ctx, output_token.value, output_token.length);
503                         gss_release_buffer(&min_stat2, &output_token);
504                         
505                         if (gensec_gssapi_state->gss_got_flags & GSS_C_DELEG_FLAG) {
506                                 DEBUG(5, ("gensec_gssapi: credentials were delegated\n"));
507                         } else {
508                                 DEBUG(5, ("gensec_gssapi: NO credentials were delegated\n"));
509                         }
510
511                         /* We may have been invoked as SASL, so there
512                          * is more work to do */
513                         if (gensec_gssapi_state->sasl) {
514                                 gensec_gssapi_state->sasl_state = STAGE_SASL_SSF_NEG;
515                                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
516                         } else {
517                                 gensec_gssapi_state->sasl_state = STAGE_DONE;
518
519                                 if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
520                                         DEBUG(5, ("GSSAPI Connection will be cryptographically sealed\n"));
521                                 } else if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
522                                         DEBUG(5, ("GSSAPI Connection will be cryptographically signed\n"));
523                                 } else {
524                                         DEBUG(5, ("GSSAPI Connection will have no cryptographic protection\n"));
525                                 }
526
527                                 return NT_STATUS_OK;
528                         }
529                 } else if (maj_stat == GSS_S_CONTINUE_NEEDED) {
530                         *out = data_blob_talloc(out_mem_ctx, output_token.value, output_token.length);
531                         gss_release_buffer(&min_stat2, &output_token);
532                         
533                         return NT_STATUS_MORE_PROCESSING_REQUIRED;
534                 } else if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
535                         gss_cred_id_t creds;
536                         gss_name_t name;
537                         gss_buffer_desc buffer;
538                         OM_uint32 lifetime = 0;
539                         gss_cred_usage_t usage;
540                         const char *role = NULL;
541                         DEBUG(0, ("GSS %s Update(krb5)(%d) Update failed, credentials expired during GSSAPI handshake!\n",
542                                   role,
543                                   gensec_gssapi_state->gss_exchange_count));
544
545                         
546                         switch (gensec_security->gensec_role) {
547                         case GENSEC_CLIENT:
548                                 creds = gensec_gssapi_state->client_cred->creds;
549                                 role = "client";
550                         case GENSEC_SERVER:
551                                 creds = gensec_gssapi_state->server_cred->creds;
552                                 role = "server";
553                         }
554
555                         maj_stat = gss_inquire_cred(&min_stat, 
556                                                     creds,
557                                                     &name, &lifetime, &usage, NULL);
558
559                         if (maj_stat == GSS_S_COMPLETE) {
560                                 const char *usage_string;
561                                 switch (usage) {
562                                 case GSS_C_BOTH:
563                                         usage_string = "GSS_C_BOTH";
564                                         break;
565                                 case GSS_C_ACCEPT:
566                                         usage_string = "GSS_C_ACCEPT";
567                                         break;
568                                 case GSS_C_INITIATE:
569                                         usage_string = "GSS_C_INITIATE";
570                                         break;
571                                 }
572                                 maj_stat = gss_display_name(&min_stat, name, &buffer, NULL);
573                                 if (maj_stat) {
574                                         buffer.value = NULL;
575                                         buffer.length = 0;
576                                 }
577                                 if (lifetime > 0) {
578                                         DEBUG(0, ("GSSAPI gss_inquire_cred indicates expiry of %*.*s in %u sec for %s\n", 
579                                                   (int)buffer.length, (int)buffer.length, (char *)buffer.value, 
580                                                   lifetime, usage_string));
581                                 } else {
582                                         DEBUG(0, ("GSSAPI gss_inquire_cred indicates %*.*s has already expired for %s\n", 
583                                                   (int)buffer.length, (int)buffer.length, (char *)buffer.value, 
584                                                   usage_string));
585                                 }
586                                 gss_release_buffer(&min_stat, &buffer);
587                                 gss_release_name(&min_stat, &name);
588                         } else if (maj_stat != GSS_S_COMPLETE) {
589                                 DEBUG(0, ("inquiry of credential lifefime via GSSAPI gss_inquire_cred failed: %s\n",
590                                           gssapi_error_string(out_mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
591                         }
592                         return NT_STATUS_INVALID_PARAMETER;
593                 } else if (gss_oid_equal(gensec_gssapi_state->gss_oid, gss_mech_krb5)) {
594                         switch (min_stat) {
595                         case KRB5KRB_AP_ERR_TKT_NYV:
596                                 DEBUG(1, ("Error with ticket to contact %s: possible clock skew between us and the KDC or target server: %s\n",
597                                           gensec_gssapi_state->target_principal,
598                                           gssapi_error_string(out_mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
599                                 return NT_STATUS_TIME_DIFFERENCE_AT_DC; /* Make SPNEGO ignore us, we can't go any further here */
600                         case KRB5KRB_AP_ERR_TKT_EXPIRED:
601                                 DEBUG(1, ("Error with ticket to contact %s: ticket is expired, possible clock skew between us and the KDC or target server: %s\n",
602                                           gensec_gssapi_state->target_principal,
603                                           gssapi_error_string(out_mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
604                                 return NT_STATUS_INVALID_PARAMETER; /* Make SPNEGO ignore us, we can't go any further here */
605                         case KRB5_KDC_UNREACH:
606                                 DEBUG(3, ("Cannot reach a KDC we require in order to obtain a ticetk to %s: %s\n",
607                                           gensec_gssapi_state->target_principal,
608                                           gssapi_error_string(out_mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
609                                 return NT_STATUS_NO_LOGON_SERVERS; /* Make SPNEGO ignore us, we can't go any further here */
610                         case KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN:
611                                 DEBUG(3, ("Server %s is not registered with our KDC: %s\n",
612                                           gensec_gssapi_state->target_principal,
613                                           gssapi_error_string(out_mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
614                                 return NT_STATUS_INVALID_PARAMETER; /* Make SPNEGO ignore us, we can't go any further here */
615                         case KRB5KRB_AP_ERR_MSG_TYPE:
616                                 /* garbage input, possibly from the auto-mech detection */
617                                 return NT_STATUS_INVALID_PARAMETER;
618                         default:
619                                 DEBUG(1, ("GSS %s Update(krb5)(%d) Update failed: %s\n",
620                                           gensec_security->gensec_role == GENSEC_CLIENT ? "client" : "server",
621                                           gensec_gssapi_state->gss_exchange_count,
622                                           gssapi_error_string(out_mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
623                                 return nt_status;
624                         }
625                 } else {
626                         DEBUG(1, ("GSS %s Update(%d) failed: %s\n",
627                                   gensec_security->gensec_role == GENSEC_CLIENT ? "client" : "server",
628                                   gensec_gssapi_state->gss_exchange_count,
629                                   gssapi_error_string(out_mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
630                         return nt_status;
631                 }
632                 break;
633         }
634
635         /* These last two stages are only done if we were invoked as SASL */
636         case STAGE_SASL_SSF_NEG:
637         {
638                 switch (gensec_security->gensec_role) {
639                 case GENSEC_CLIENT:
640                 {
641                         uint8_t maxlength_proposed[4]; 
642                         uint8_t maxlength_accepted[4]; 
643                         uint8_t security_supported;
644                         int conf_state;
645                         gss_qop_t qop_state;
646                         input_token.length = in.length;
647                         input_token.value = in.data;
648
649                         /* As a client, we have just send a
650                          * zero-length blob to the server (after the
651                          * normal GSSAPI exchange), and it has replied
652                          * with it's SASL negotiation */
653                         
654                         maj_stat = gss_unwrap(&min_stat, 
655                                               gensec_gssapi_state->gssapi_context, 
656                                               &input_token,
657                                               &output_token, 
658                                               &conf_state,
659                                               &qop_state);
660                         if (GSS_ERROR(maj_stat)) {
661                                 DEBUG(1, ("gensec_gssapi_update: GSS UnWrap of SASL protection negotiation failed: %s\n", 
662                                           gssapi_error_string(out_mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
663                                 return NT_STATUS_ACCESS_DENIED;
664                         }
665                         
666                         if (output_token.length < 4) {
667                                 return NT_STATUS_INVALID_PARAMETER;
668                         }
669
670                         memcpy(maxlength_proposed, output_token.value, 4);
671                         gss_release_buffer(&min_stat, &output_token);
672
673                         /* first byte is the proposed security */
674                         security_supported = maxlength_proposed[0];
675                         maxlength_proposed[0] = '\0';
676                         
677                         /* Rest is the proposed max wrap length */
678                         gensec_gssapi_state->max_wrap_buf_size = MIN(RIVAL(maxlength_proposed, 0), 
679                                                                      gensec_gssapi_state->max_wrap_buf_size);
680                         gensec_gssapi_state->sasl_protection = 0;
681                         if (security_supported & NEG_SEAL) {
682                                 if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
683                                         gensec_gssapi_state->sasl_protection |= NEG_SEAL;
684                                 }
685                         }
686                         if (security_supported & NEG_SIGN) {
687                                 if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
688                                         gensec_gssapi_state->sasl_protection |= NEG_SIGN;
689                                 }
690                         }
691                         if (security_supported & NEG_NONE) {
692                                 gensec_gssapi_state->sasl_protection |= NEG_NONE;
693                         }
694                         if (gensec_gssapi_state->sasl_protection == 0) {
695                                 DEBUG(1, ("Remote server does not support unprotected connections\n"));
696                                 return NT_STATUS_ACCESS_DENIED;
697                         }
698
699                         /* Send back the negotiated max length */
700
701                         RSIVAL(maxlength_accepted, 0, gensec_gssapi_state->max_wrap_buf_size);
702
703                         maxlength_accepted[0] = gensec_gssapi_state->sasl_protection;
704                         
705                         input_token.value = maxlength_accepted;
706                         input_token.length = sizeof(maxlength_accepted);
707
708                         maj_stat = gss_wrap(&min_stat, 
709                                             gensec_gssapi_state->gssapi_context, 
710                                             false,
711                                             GSS_C_QOP_DEFAULT,
712                                             &input_token,
713                                             &conf_state,
714                                             &output_token);
715                         if (GSS_ERROR(maj_stat)) {
716                                 DEBUG(1, ("GSS Update(SSF_NEG): GSS Wrap failed: %s\n", 
717                                           gssapi_error_string(out_mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
718                                 return NT_STATUS_ACCESS_DENIED;
719                         }
720                         
721                         *out = data_blob_talloc(out_mem_ctx, output_token.value, output_token.length);
722                         gss_release_buffer(&min_stat, &output_token);
723
724                         /* quirk:  This changes the value that gensec_have_feature returns, to be that after SASL negotiation */
725                         gensec_gssapi_state->sasl_state = STAGE_DONE;
726
727                         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
728                                 DEBUG(3, ("SASL/GSSAPI Connection to server will be cryptographically sealed\n"));
729                         } else if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
730                                 DEBUG(3, ("SASL/GSSAPI Connection to server will be cryptographically signed\n"));
731                         } else {
732                                 DEBUG(3, ("SASL/GSSAPI Connection to server will have no cryptographically protection\n"));
733                         }
734
735                         return NT_STATUS_OK;
736                 }
737                 case GENSEC_SERVER:
738                 {
739                         uint8_t maxlength_proposed[4]; 
740                         uint8_t security_supported = 0x0;
741                         int conf_state;
742
743                         /* As a server, we have just been sent a zero-length blob (note this, but it isn't fatal) */
744                         if (in.length != 0) {
745                                 DEBUG(1, ("SASL/GSSAPI: client sent non-zero length starting SASL negotiation!\n"));
746                         }
747                         
748                         /* Give the client some idea what we will support */
749                           
750                         RSIVAL(maxlength_proposed, 0, gensec_gssapi_state->max_wrap_buf_size);
751                         /* first byte is the proposed security */
752                         maxlength_proposed[0] = '\0';
753                         
754                         gensec_gssapi_state->sasl_protection = 0;
755                         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
756                                 security_supported |= NEG_SEAL;
757                         } 
758                         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
759                                 security_supported |= NEG_SIGN;
760                         }
761                         if (security_supported == 0) {
762                                 /* If we don't support anything, this must be 0 */
763                                 RSIVAL(maxlength_proposed, 0, 0x0);
764                         }
765
766                         /* TODO:  We may not wish to support this */
767                         security_supported |= NEG_NONE;
768                         maxlength_proposed[0] = security_supported;
769                         
770                         input_token.value = maxlength_proposed;
771                         input_token.length = sizeof(maxlength_proposed);
772
773                         maj_stat = gss_wrap(&min_stat, 
774                                             gensec_gssapi_state->gssapi_context, 
775                                             false,
776                                             GSS_C_QOP_DEFAULT,
777                                             &input_token,
778                                             &conf_state,
779                                             &output_token);
780                         if (GSS_ERROR(maj_stat)) {
781                                 DEBUG(1, ("GSS Update(SSF_NEG): GSS Wrap failed: %s\n", 
782                                           gssapi_error_string(out_mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
783                                 return NT_STATUS_ACCESS_DENIED;
784                         }
785                         
786                         *out = data_blob_talloc(out_mem_ctx, output_token.value, output_token.length);
787                         gss_release_buffer(&min_stat, &output_token);
788
789                         gensec_gssapi_state->sasl_state = STAGE_SASL_SSF_ACCEPT;
790                         return NT_STATUS_MORE_PROCESSING_REQUIRED;
791                 }
792                 default:
793                         return NT_STATUS_INVALID_PARAMETER;
794                         
795                 }
796         }
797         /* This is s server-only stage */
798         case STAGE_SASL_SSF_ACCEPT:
799         {
800                 uint8_t maxlength_accepted[4]; 
801                 uint8_t security_accepted;
802                 int conf_state;
803                 gss_qop_t qop_state;
804                 input_token.length = in.length;
805                 input_token.value = in.data;
806                         
807                 maj_stat = gss_unwrap(&min_stat, 
808                                       gensec_gssapi_state->gssapi_context, 
809                                       &input_token,
810                                       &output_token, 
811                                       &conf_state,
812                                       &qop_state);
813                 if (GSS_ERROR(maj_stat)) {
814                         DEBUG(1, ("gensec_gssapi_update: GSS UnWrap of SASL protection negotiation failed: %s\n", 
815                                   gssapi_error_string(out_mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
816                         return NT_STATUS_ACCESS_DENIED;
817                 }
818                         
819                 if (output_token.length < 4) {
820                         return NT_STATUS_INVALID_PARAMETER;
821                 }
822
823                 memcpy(maxlength_accepted, output_token.value, 4);
824                 gss_release_buffer(&min_stat, &output_token);
825                 
826                 /* first byte is the proposed security */
827                 security_accepted = maxlength_accepted[0];
828                 maxlength_accepted[0] = '\0';
829
830                 /* Rest is the proposed max wrap length */
831                 gensec_gssapi_state->max_wrap_buf_size = MIN(RIVAL(maxlength_accepted, 0), 
832                                                              gensec_gssapi_state->max_wrap_buf_size);
833
834                 gensec_gssapi_state->sasl_protection = 0;
835                 if (security_accepted & NEG_SEAL) {
836                         if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
837                                 DEBUG(1, ("Remote client wanted seal, but gensec refused\n"));
838                                 return NT_STATUS_ACCESS_DENIED;
839                         }
840                         gensec_gssapi_state->sasl_protection |= NEG_SEAL;
841                 }
842                 if (security_accepted & NEG_SIGN) {
843                         if (!gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
844                                 DEBUG(1, ("Remote client wanted sign, but gensec refused\n"));
845                                 return NT_STATUS_ACCESS_DENIED;
846                         }
847                         gensec_gssapi_state->sasl_protection |= NEG_SIGN;
848                 }
849                 if (security_accepted & NEG_NONE) {
850                         gensec_gssapi_state->sasl_protection |= NEG_NONE;
851                 }
852
853                 /* quirk:  This changes the value that gensec_have_feature returns, to be that after SASL negotiation */
854                 gensec_gssapi_state->sasl_state = STAGE_DONE;
855                 if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
856                         DEBUG(5, ("SASL/GSSAPI Connection from client will be cryptographically sealed\n"));
857                 } else if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
858                         DEBUG(5, ("SASL/GSSAPI Connection from client will be cryptographically signed\n"));
859                 } else {
860                         DEBUG(5, ("SASL/GSSAPI Connection from client will have no cryptographic protection\n"));
861                 }
862
863                 *out = data_blob(NULL, 0);
864                 return NT_STATUS_OK;    
865         }
866         default:
867                 return NT_STATUS_INVALID_PARAMETER;
868         }
869 }
870
871 static NTSTATUS gensec_gssapi_wrap(struct gensec_security *gensec_security, 
872                                    TALLOC_CTX *mem_ctx, 
873                                    const DATA_BLOB *in, 
874                                    DATA_BLOB *out)
875 {
876         struct gensec_gssapi_state *gensec_gssapi_state
877                 = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
878         OM_uint32 maj_stat, min_stat;
879         gss_buffer_desc input_token, output_token;
880         int conf_state;
881         input_token.length = in->length;
882         input_token.value = in->data;
883
884         maj_stat = gss_wrap(&min_stat, 
885                             gensec_gssapi_state->gssapi_context, 
886                             gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL),
887                             GSS_C_QOP_DEFAULT,
888                             &input_token,
889                             &conf_state,
890                             &output_token);
891         if (GSS_ERROR(maj_stat)) {
892                 DEBUG(1, ("gensec_gssapi_wrap: GSS Wrap failed: %s\n", 
893                           gssapi_error_string(mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
894                 return NT_STATUS_ACCESS_DENIED;
895         }
896
897         *out = data_blob_talloc(mem_ctx, output_token.value, output_token.length);
898         gss_release_buffer(&min_stat, &output_token);
899
900         if (gensec_gssapi_state->sasl) {
901                 size_t max_wrapped_size = gensec_gssapi_max_wrapped_size(gensec_security);
902                 if (max_wrapped_size < out->length) {
903                         DEBUG(1, ("gensec_gssapi_wrap: when wrapped, INPUT data (%u) is grew to be larger than SASL negotiated maximum output size (%u > %u)\n",
904                                   (unsigned)in->length, 
905                                   (unsigned)out->length, 
906                                   (unsigned int)max_wrapped_size));
907                         return NT_STATUS_INVALID_PARAMETER;
908                 }
909         }
910         
911         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)
912             && !conf_state) {
913                 return NT_STATUS_ACCESS_DENIED;
914         }
915         return NT_STATUS_OK;
916 }
917
918 static NTSTATUS gensec_gssapi_unwrap(struct gensec_security *gensec_security, 
919                                      TALLOC_CTX *mem_ctx, 
920                                      const DATA_BLOB *in, 
921                                      DATA_BLOB *out)
922 {
923         struct gensec_gssapi_state *gensec_gssapi_state
924                 = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
925         OM_uint32 maj_stat, min_stat;
926         gss_buffer_desc input_token, output_token;
927         int conf_state;
928         gss_qop_t qop_state;
929         input_token.length = in->length;
930         input_token.value = in->data;
931         
932         if (gensec_gssapi_state->sasl) {
933                 size_t max_wrapped_size = gensec_gssapi_max_wrapped_size(gensec_security);
934                 if (max_wrapped_size < in->length) {
935                         DEBUG(1, ("gensec_gssapi_unwrap: WRAPPED data is larger than SASL negotiated maximum size\n"));
936                         return NT_STATUS_INVALID_PARAMETER;
937                 }
938         }
939         
940         maj_stat = gss_unwrap(&min_stat, 
941                               gensec_gssapi_state->gssapi_context, 
942                               &input_token,
943                               &output_token, 
944                               &conf_state,
945                               &qop_state);
946         if (GSS_ERROR(maj_stat)) {
947                 DEBUG(1, ("gensec_gssapi_unwrap: GSS UnWrap failed: %s\n", 
948                           gssapi_error_string(mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
949                 return NT_STATUS_ACCESS_DENIED;
950         }
951
952         *out = data_blob_talloc(mem_ctx, output_token.value, output_token.length);
953         gss_release_buffer(&min_stat, &output_token);
954         
955         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)
956             && !conf_state) {
957                 return NT_STATUS_ACCESS_DENIED;
958         }
959         return NT_STATUS_OK;
960 }
961
962 /* Find out the maximum input size negotiated on this connection */
963
964 static size_t gensec_gssapi_max_input_size(struct gensec_security *gensec_security) 
965 {
966         struct gensec_gssapi_state *gensec_gssapi_state
967                 = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
968         OM_uint32 maj_stat, min_stat;
969         OM_uint32 max_input_size;
970
971         maj_stat = gss_wrap_size_limit(&min_stat, 
972                                        gensec_gssapi_state->gssapi_context,
973                                        gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL),
974                                        GSS_C_QOP_DEFAULT,
975                                        gensec_gssapi_state->max_wrap_buf_size,
976                                        &max_input_size);
977         if (GSS_ERROR(maj_stat)) {
978                 TALLOC_CTX *mem_ctx = talloc_new(NULL); 
979                 DEBUG(1, ("gensec_gssapi_max_input_size: determinaing signature size with gss_wrap_size_limit failed: %s\n", 
980                           gssapi_error_string(mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
981                 talloc_free(mem_ctx);
982                 return 0;
983         }
984
985         return max_input_size;
986 }
987
988 /* Find out the maximum output size negotiated on this connection */
989 static size_t gensec_gssapi_max_wrapped_size(struct gensec_security *gensec_security) 
990 {
991         struct gensec_gssapi_state *gensec_gssapi_state = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);;
992         return gensec_gssapi_state->max_wrap_buf_size;
993 }
994
995 static NTSTATUS gensec_gssapi_seal_packet(struct gensec_security *gensec_security, 
996                                           TALLOC_CTX *mem_ctx, 
997                                           uint8_t *data, size_t length, 
998                                           const uint8_t *whole_pdu, size_t pdu_length, 
999                                           DATA_BLOB *sig)
1000 {
1001         struct gensec_gssapi_state *gensec_gssapi_state
1002                 = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
1003         OM_uint32 maj_stat, min_stat;
1004         gss_buffer_desc input_token, output_token;
1005         int conf_state;
1006         ssize_t sig_length;
1007
1008         input_token.length = length;
1009         input_token.value = data;
1010         
1011         maj_stat = gss_wrap(&min_stat, 
1012                             gensec_gssapi_state->gssapi_context,
1013                             gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL),
1014                             GSS_C_QOP_DEFAULT,
1015                             &input_token,
1016                             &conf_state,
1017                             &output_token);
1018         if (GSS_ERROR(maj_stat)) {
1019                 DEBUG(1, ("gensec_gssapi_seal_packet: GSS Wrap failed: %s\n", 
1020                           gssapi_error_string(mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
1021                 return NT_STATUS_ACCESS_DENIED;
1022         }
1023
1024         if (output_token.length < input_token.length) {
1025                 DEBUG(1, ("gensec_gssapi_seal_packet: GSS Wrap length [%ld] *less* than caller length [%ld]\n", 
1026                           (long)output_token.length, (long)length));
1027                 return NT_STATUS_INTERNAL_ERROR;
1028         }
1029         sig_length = output_token.length - input_token.length;
1030
1031         memcpy(data, ((uint8_t *)output_token.value) + sig_length, length);
1032         *sig = data_blob_talloc(mem_ctx, (uint8_t *)output_token.value, sig_length);
1033
1034         dump_data_pw("gensec_gssapi_seal_packet: sig\n", sig->data, sig->length);
1035         dump_data_pw("gensec_gssapi_seal_packet: clear\n", data, length);
1036         dump_data_pw("gensec_gssapi_seal_packet: sealed\n", ((uint8_t *)output_token.value) + sig_length, output_token.length - sig_length);
1037
1038         gss_release_buffer(&min_stat, &output_token);
1039
1040         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)
1041             && !conf_state) {
1042                 return NT_STATUS_ACCESS_DENIED;
1043         }
1044         return NT_STATUS_OK;
1045 }
1046
1047 static NTSTATUS gensec_gssapi_unseal_packet(struct gensec_security *gensec_security, 
1048                                             uint8_t *data, size_t length, 
1049                                             const uint8_t *whole_pdu, size_t pdu_length,
1050                                             const DATA_BLOB *sig)
1051 {
1052         struct gensec_gssapi_state *gensec_gssapi_state
1053                 = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
1054         OM_uint32 maj_stat, min_stat;
1055         gss_buffer_desc input_token, output_token;
1056         int conf_state;
1057         gss_qop_t qop_state;
1058         DATA_BLOB in;
1059
1060         dump_data_pw("gensec_gssapi_unseal_packet: sig\n", sig->data, sig->length);
1061
1062         in = data_blob_talloc(gensec_security, NULL, sig->length + length);
1063
1064         memcpy(in.data, sig->data, sig->length);
1065         memcpy(in.data + sig->length, data, length);
1066
1067         input_token.length = in.length;
1068         input_token.value = in.data;
1069         
1070         maj_stat = gss_unwrap(&min_stat, 
1071                               gensec_gssapi_state->gssapi_context, 
1072                               &input_token,
1073                               &output_token, 
1074                               &conf_state,
1075                               &qop_state);
1076         talloc_free(in.data);
1077         if (GSS_ERROR(maj_stat)) {
1078                 char *error_string = gssapi_error_string(NULL, maj_stat, min_stat, gensec_gssapi_state->gss_oid);
1079                 DEBUG(1, ("gensec_gssapi_unseal_packet: GSS UnWrap failed: %s\n", 
1080                           error_string));
1081                 talloc_free(error_string);
1082                 return NT_STATUS_ACCESS_DENIED;
1083         }
1084
1085         if (output_token.length != length) {
1086                 return NT_STATUS_INTERNAL_ERROR;
1087         }
1088
1089         memcpy(data, output_token.value, length);
1090
1091         gss_release_buffer(&min_stat, &output_token);
1092         
1093         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)
1094             && !conf_state) {
1095                 return NT_STATUS_ACCESS_DENIED;
1096         }
1097         return NT_STATUS_OK;
1098 }
1099
1100 static NTSTATUS gensec_gssapi_sign_packet(struct gensec_security *gensec_security, 
1101                                           TALLOC_CTX *mem_ctx, 
1102                                           const uint8_t *data, size_t length, 
1103                                           const uint8_t *whole_pdu, size_t pdu_length, 
1104                                           DATA_BLOB *sig)
1105 {
1106         struct gensec_gssapi_state *gensec_gssapi_state
1107                 = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
1108         OM_uint32 maj_stat, min_stat;
1109         gss_buffer_desc input_token, output_token;
1110
1111         if (gensec_security->want_features & GENSEC_FEATURE_SIGN_PKT_HEADER) {
1112                 input_token.length = pdu_length;
1113                 input_token.value = discard_const_p(uint8_t *, whole_pdu);
1114         } else {
1115                 input_token.length = length;
1116                 input_token.value = discard_const_p(uint8_t *, data);
1117         }
1118
1119         maj_stat = gss_get_mic(&min_stat,
1120                             gensec_gssapi_state->gssapi_context,
1121                             GSS_C_QOP_DEFAULT,
1122                             &input_token,
1123                             &output_token);
1124         if (GSS_ERROR(maj_stat)) {
1125                 DEBUG(1, ("GSS GetMic failed: %s\n",
1126                           gssapi_error_string(mem_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
1127                 return NT_STATUS_ACCESS_DENIED;
1128         }
1129
1130         *sig = data_blob_talloc(mem_ctx, (uint8_t *)output_token.value, output_token.length);
1131
1132         dump_data_pw("gensec_gssapi_seal_packet: sig\n", sig->data, sig->length);
1133
1134         gss_release_buffer(&min_stat, &output_token);
1135
1136         return NT_STATUS_OK;
1137 }
1138
1139 static NTSTATUS gensec_gssapi_check_packet(struct gensec_security *gensec_security, 
1140                                            const uint8_t *data, size_t length, 
1141                                            const uint8_t *whole_pdu, size_t pdu_length, 
1142                                            const DATA_BLOB *sig)
1143 {
1144         struct gensec_gssapi_state *gensec_gssapi_state
1145                 = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
1146         OM_uint32 maj_stat, min_stat;
1147         gss_buffer_desc input_token;
1148         gss_buffer_desc input_message;
1149         gss_qop_t qop_state;
1150
1151         dump_data_pw("gensec_gssapi_seal_packet: sig\n", sig->data, sig->length);
1152
1153         if (gensec_security->want_features & GENSEC_FEATURE_SIGN_PKT_HEADER) {
1154                 input_message.length = pdu_length;
1155                 input_message.value = discard_const(whole_pdu);
1156         } else {
1157                 input_message.length = length;
1158                 input_message.value = discard_const(data);
1159         }
1160
1161         input_token.length = sig->length;
1162         input_token.value = sig->data;
1163
1164         maj_stat = gss_verify_mic(&min_stat,
1165                               gensec_gssapi_state->gssapi_context, 
1166                               &input_message,
1167                               &input_token,
1168                               &qop_state);
1169         if (GSS_ERROR(maj_stat)) {
1170                 char *error_string = gssapi_error_string(NULL, maj_stat, min_stat, gensec_gssapi_state->gss_oid);
1171                 DEBUG(1, ("GSS VerifyMic failed: %s\n", error_string));
1172                 talloc_free(error_string);
1173
1174                 return NT_STATUS_ACCESS_DENIED;
1175         }
1176
1177         return NT_STATUS_OK;
1178 }
1179
1180 /* Try to figure out what features we actually got on the connection */
1181 static bool gensec_gssapi_have_feature(struct gensec_security *gensec_security, 
1182                                        uint32_t feature) 
1183 {
1184         struct gensec_gssapi_state *gensec_gssapi_state
1185                 = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
1186         if (feature & GENSEC_FEATURE_SIGN) {
1187                 /* If we are going GSSAPI SASL, then we honour the second negotiation */
1188                 if (gensec_gssapi_state->sasl 
1189                     && gensec_gssapi_state->sasl_state == STAGE_DONE) {
1190                         return ((gensec_gssapi_state->sasl_protection & NEG_SIGN) 
1191                                 && (gensec_gssapi_state->gss_got_flags & GSS_C_INTEG_FLAG));
1192                 }
1193                 return gensec_gssapi_state->gss_got_flags & GSS_C_INTEG_FLAG;
1194         }
1195         if (feature & GENSEC_FEATURE_SEAL) {
1196                 /* If we are going GSSAPI SASL, then we honour the second negotiation */
1197                 if (gensec_gssapi_state->sasl 
1198                     && gensec_gssapi_state->sasl_state == STAGE_DONE) {
1199                         return ((gensec_gssapi_state->sasl_protection & NEG_SEAL) 
1200                                  && (gensec_gssapi_state->gss_got_flags & GSS_C_CONF_FLAG));
1201                 }
1202                 return gensec_gssapi_state->gss_got_flags & GSS_C_CONF_FLAG;
1203         }
1204         if (feature & GENSEC_FEATURE_SESSION_KEY) {
1205                 /* Only for GSSAPI/Krb5 */
1206                 if (gss_oid_equal(gensec_gssapi_state->gss_oid, gss_mech_krb5)) {
1207                         return true;
1208                 }
1209         }
1210         if (feature & GENSEC_FEATURE_DCE_STYLE) {
1211                 return gensec_gssapi_state->gss_got_flags & GSS_C_DCE_STYLE;
1212         }
1213         if (feature & GENSEC_FEATURE_NEW_SPNEGO) {
1214                 NTSTATUS status;
1215                 uint32_t keytype;
1216
1217                 if (!(gensec_gssapi_state->gss_got_flags & GSS_C_INTEG_FLAG)) {
1218                         return false;
1219                 }
1220
1221                 if (gensec_setting_bool(gensec_security->settings, "gensec_gssapi", "force_new_spnego", false)) {
1222                         return true;
1223                 }
1224                 if (gensec_setting_bool(gensec_security->settings, "gensec_gssapi", "disable_new_spnego", false)) {
1225                         return false;
1226                 }
1227
1228                 status = gssapi_get_session_key(gensec_gssapi_state,
1229                                                 gensec_gssapi_state->gssapi_context, NULL, &keytype);
1230                 /* 
1231                  * We should do a proper sig on the mechListMic unless
1232                  * we know we have to be backwards compatible with
1233                  * earlier windows versions.  
1234                  * 
1235                  * Negotiating a non-krb5
1236                  * mech for example should be regarded as having
1237                  * NEW_SPNEGO
1238                  */
1239                 if (NT_STATUS_IS_OK(status)) {
1240                         switch (keytype) {
1241                         case ENCTYPE_DES_CBC_CRC:
1242                         case ENCTYPE_DES_CBC_MD5:
1243                         case ENCTYPE_ARCFOUR_HMAC:
1244                         case ENCTYPE_DES3_CBC_SHA1:
1245                                 return false;
1246                         }
1247                 }
1248                 return true;
1249         }
1250         /* We can always do async (rather than strict request/reply) packets.  */
1251         if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
1252                 return true;
1253         }
1254         return false;
1255 }
1256
1257 /*
1258  * Extract the 'sesssion key' needed by SMB signing and ncacn_np 
1259  * (for encrypting some passwords).
1260  * 
1261  * This breaks all the abstractions, but what do you expect...
1262  */
1263 static NTSTATUS gensec_gssapi_session_key(struct gensec_security *gensec_security, 
1264                                           TALLOC_CTX *mem_ctx,
1265                                           DATA_BLOB *session_key) 
1266 {
1267         struct gensec_gssapi_state *gensec_gssapi_state
1268                 = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
1269         return gssapi_get_session_key(mem_ctx, gensec_gssapi_state->gssapi_context, session_key, NULL);
1270 }
1271
1272 /* Get some basic (and authorization) information about the user on
1273  * this session.  This uses either the PAC (if present) or a local
1274  * database lookup */
1275 static NTSTATUS gensec_gssapi_session_info(struct gensec_security *gensec_security,
1276                                            TALLOC_CTX *mem_ctx,
1277                                            struct auth_session_info **_session_info) 
1278 {
1279         NTSTATUS nt_status;
1280         TALLOC_CTX *tmp_ctx;
1281         struct gensec_gssapi_state *gensec_gssapi_state
1282                 = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
1283         struct auth_session_info *session_info = NULL;
1284         OM_uint32 maj_stat, min_stat;
1285         DATA_BLOB pac_blob, *pac_blob_ptr = NULL;
1286
1287         gss_buffer_desc name_token;
1288         char *principal_string;
1289         
1290         tmp_ctx = talloc_named(mem_ctx, 0, "gensec_gssapi_session_info context");
1291         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
1292
1293         maj_stat = gss_display_name (&min_stat,
1294                                      gensec_gssapi_state->client_name,
1295                                      &name_token,
1296                                      NULL);
1297         if (GSS_ERROR(maj_stat)) {
1298                 DEBUG(1, ("GSS display_name failed: %s\n",
1299                           gssapi_error_string(tmp_ctx, maj_stat, min_stat, gensec_gssapi_state->gss_oid)));
1300                 talloc_free(tmp_ctx);
1301                 return NT_STATUS_FOOBAR;
1302         }
1303
1304         principal_string = talloc_strndup(tmp_ctx,
1305                                           (const char *)name_token.value,
1306                                           name_token.length);
1307
1308         gss_release_buffer(&min_stat, &name_token);
1309
1310         if (!principal_string) {
1311                 talloc_free(tmp_ctx);
1312                 return NT_STATUS_NO_MEMORY;
1313         }
1314
1315         nt_status = gssapi_obtain_pac_blob(tmp_ctx,  gensec_gssapi_state->gssapi_context,
1316                                            gensec_gssapi_state->client_name,
1317                                            &pac_blob);
1318         
1319         /* IF we have the PAC - otherwise we need to get this
1320          * data from elsewere - local ldb, or (TODO) lookup of some
1321          * kind... 
1322          */
1323         if (NT_STATUS_IS_OK(nt_status)) {
1324                 pac_blob_ptr = &pac_blob;
1325         }
1326         nt_status = gensec_generate_session_info_pac(tmp_ctx,
1327                                                      gensec_security,
1328                                                      gensec_gssapi_state->smb_krb5_context,
1329                                                      pac_blob_ptr, principal_string,
1330                                                      gensec_get_remote_address(gensec_security),
1331                                                      &session_info);
1332         if (!NT_STATUS_IS_OK(nt_status)) {
1333                 talloc_free(tmp_ctx);
1334                 return nt_status;
1335         }
1336
1337         nt_status = gensec_gssapi_session_key(gensec_security, session_info, &session_info->session_key);
1338         if (!NT_STATUS_IS_OK(nt_status)) {
1339                 talloc_free(tmp_ctx);
1340                 return nt_status;
1341         }
1342
1343         if (!(gensec_gssapi_state->gss_got_flags & GSS_C_DELEG_FLAG)) {
1344                 DEBUG(10, ("gensec_gssapi: NO delegated credentials supplied by client\n"));
1345         } else {
1346                 krb5_error_code ret;
1347                 const char *error_string;
1348
1349                 DEBUG(10, ("gensec_gssapi: delegated credentials supplied by client\n"));
1350                 session_info->credentials = cli_credentials_init(session_info);
1351                 if (!session_info->credentials) {
1352                         talloc_free(tmp_ctx);
1353                         return NT_STATUS_NO_MEMORY;
1354                 }
1355
1356                 cli_credentials_set_conf(session_info->credentials, gensec_security->settings->lp_ctx);
1357                 /* Just so we don't segfault trying to get at a username */
1358                 cli_credentials_set_anonymous(session_info->credentials);
1359                 
1360                 ret = cli_credentials_set_client_gss_creds(session_info->credentials, 
1361                                                            gensec_security->settings->lp_ctx,
1362                                                            gensec_gssapi_state->delegated_cred_handle,
1363                                                            CRED_SPECIFIED, &error_string);
1364                 if (ret) {
1365                         talloc_free(tmp_ctx);
1366                         DEBUG(2,("Failed to get gss creds: %s\n", error_string));
1367                         return NT_STATUS_NO_MEMORY;
1368                 }
1369                 
1370                 /* This credential handle isn't useful for password authentication, so ensure nobody tries to do that */
1371                 cli_credentials_set_kerberos_state(session_info->credentials, CRED_MUST_USE_KERBEROS);
1372
1373                 /* It has been taken from this place... */
1374                 gensec_gssapi_state->delegated_cred_handle = GSS_C_NO_CREDENTIAL;
1375         }
1376         *_session_info = talloc_steal(mem_ctx, session_info);
1377         talloc_free(tmp_ctx);
1378
1379         return NT_STATUS_OK;
1380 }
1381
1382 static size_t gensec_gssapi_sig_size(struct gensec_security *gensec_security, size_t data_size)
1383 {
1384         struct gensec_gssapi_state *gensec_gssapi_state
1385                 = talloc_get_type(gensec_security->private_data, struct gensec_gssapi_state);
1386         NTSTATUS status;
1387
1388         if (gensec_gssapi_state->sig_size) {
1389                 return gensec_gssapi_state->sig_size;
1390         }
1391
1392         if (gensec_gssapi_state->gss_got_flags & GSS_C_CONF_FLAG) {
1393                 gensec_gssapi_state->sig_size = 45;
1394         } else {
1395                 gensec_gssapi_state->sig_size = 37;
1396         }
1397
1398         status = gensec_gssapi_init_lucid(gensec_gssapi_state);
1399         if (!NT_STATUS_IS_OK(status)) {
1400                 return gensec_gssapi_state->sig_size;
1401         }
1402
1403         if (gensec_gssapi_state->lucid->protocol == 1) {
1404                 if (gensec_gssapi_state->gss_got_flags & GSS_C_CONF_FLAG) {
1405                         /*
1406                          * TODO: windows uses 76 here, but we don't know
1407                          *       gss_wrap works with aes keys yet
1408                          */
1409                         gensec_gssapi_state->sig_size = 76;
1410                 } else {
1411                         gensec_gssapi_state->sig_size = 28;
1412                 }
1413         } else if (gensec_gssapi_state->lucid->protocol == 0) {
1414                 switch (gensec_gssapi_state->lucid->rfc1964_kd.ctx_key.type) {
1415                 case KEYTYPE_DES:
1416                 case KEYTYPE_ARCFOUR:
1417                 case KEYTYPE_ARCFOUR_56:
1418                         if (gensec_gssapi_state->gss_got_flags & GSS_C_CONF_FLAG) {
1419                                 gensec_gssapi_state->sig_size = 45;
1420                         } else {
1421                                 gensec_gssapi_state->sig_size = 37;
1422                         }
1423                         break;
1424                 case KEYTYPE_DES3:
1425                         if (gensec_gssapi_state->gss_got_flags & GSS_C_CONF_FLAG) {
1426                                 gensec_gssapi_state->sig_size = 57;
1427                         } else {
1428                                 gensec_gssapi_state->sig_size = 49;
1429                         }
1430                         break;
1431                 }
1432         }
1433
1434         return gensec_gssapi_state->sig_size;
1435 }
1436
1437 static const char *gensec_gssapi_krb5_oids[] = { 
1438         GENSEC_OID_KERBEROS5_OLD,
1439         GENSEC_OID_KERBEROS5,
1440         NULL 
1441 };
1442
1443 static const char *gensec_gssapi_spnego_oids[] = { 
1444         GENSEC_OID_SPNEGO,
1445         NULL 
1446 };
1447
1448 /* As a server, this could in theory accept any GSSAPI mech */
1449 static const struct gensec_security_ops gensec_gssapi_spnego_security_ops = {
1450         .name           = "gssapi_spnego",
1451         .sasl_name      = "GSS-SPNEGO",
1452         .auth_type      = DCERPC_AUTH_TYPE_SPNEGO,
1453         .oid            = gensec_gssapi_spnego_oids,
1454         .client_start   = gensec_gssapi_client_start,
1455         .server_start   = gensec_gssapi_server_start,
1456         .magic          = gensec_magic_check_krb5_oid,
1457         .update         = gensec_gssapi_update,
1458         .session_key    = gensec_gssapi_session_key,
1459         .session_info   = gensec_gssapi_session_info,
1460         .sign_packet    = gensec_gssapi_sign_packet,
1461         .check_packet   = gensec_gssapi_check_packet,
1462         .seal_packet    = gensec_gssapi_seal_packet,
1463         .unseal_packet  = gensec_gssapi_unseal_packet,
1464         .wrap           = gensec_gssapi_wrap,
1465         .unwrap         = gensec_gssapi_unwrap,
1466         .have_feature   = gensec_gssapi_have_feature,
1467         .enabled        = false,
1468         .kerberos       = true,
1469         .priority       = GENSEC_GSSAPI
1470 };
1471
1472 /* As a server, this could in theory accept any GSSAPI mech */
1473 static const struct gensec_security_ops gensec_gssapi_krb5_security_ops = {
1474         .name           = "gssapi_krb5",
1475         .auth_type      = DCERPC_AUTH_TYPE_KRB5,
1476         .oid            = gensec_gssapi_krb5_oids,
1477         .client_start   = gensec_gssapi_client_start,
1478         .server_start   = gensec_gssapi_server_start,
1479         .magic          = gensec_magic_check_krb5_oid,
1480         .update         = gensec_gssapi_update,
1481         .session_key    = gensec_gssapi_session_key,
1482         .session_info   = gensec_gssapi_session_info,
1483         .sig_size       = gensec_gssapi_sig_size,
1484         .sign_packet    = gensec_gssapi_sign_packet,
1485         .check_packet   = gensec_gssapi_check_packet,
1486         .seal_packet    = gensec_gssapi_seal_packet,
1487         .unseal_packet  = gensec_gssapi_unseal_packet,
1488         .wrap           = gensec_gssapi_wrap,
1489         .unwrap         = gensec_gssapi_unwrap,
1490         .have_feature   = gensec_gssapi_have_feature,
1491         .enabled        = true,
1492         .kerberos       = true,
1493         .priority       = GENSEC_GSSAPI
1494 };
1495
1496 /* As a server, this could in theory accept any GSSAPI mech */
1497 static const struct gensec_security_ops gensec_gssapi_sasl_krb5_security_ops = {
1498         .name             = "gssapi_krb5_sasl",
1499         .sasl_name        = "GSSAPI",
1500         .client_start     = gensec_gssapi_sasl_client_start,
1501         .server_start     = gensec_gssapi_sasl_server_start,
1502         .update           = gensec_gssapi_update,
1503         .session_key      = gensec_gssapi_session_key,
1504         .session_info     = gensec_gssapi_session_info,
1505         .max_input_size   = gensec_gssapi_max_input_size,
1506         .max_wrapped_size = gensec_gssapi_max_wrapped_size,
1507         .wrap             = gensec_gssapi_wrap,
1508         .unwrap           = gensec_gssapi_unwrap,
1509         .have_feature     = gensec_gssapi_have_feature,
1510         .enabled          = true,
1511         .kerberos         = true,
1512         .priority         = GENSEC_GSSAPI
1513 };
1514
1515 _PUBLIC_ NTSTATUS gensec_gssapi_init(void)
1516 {
1517         NTSTATUS ret;
1518
1519         ret = gensec_register(&gensec_gssapi_spnego_security_ops);
1520         if (!NT_STATUS_IS_OK(ret)) {
1521                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1522                         gensec_gssapi_spnego_security_ops.name));
1523                 return ret;
1524         }
1525
1526         ret = gensec_register(&gensec_gssapi_krb5_security_ops);
1527         if (!NT_STATUS_IS_OK(ret)) {
1528                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1529                         gensec_gssapi_krb5_security_ops.name));
1530                 return ret;
1531         }
1532
1533         ret = gensec_register(&gensec_gssapi_sasl_krb5_security_ops);
1534         if (!NT_STATUS_IS_OK(ret)) {
1535                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1536                         gensec_gssapi_sasl_krb5_security_ops.name));
1537                 return ret;
1538         }
1539
1540         return ret;
1541 }