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