a3a04f
From 334a4870cbbfefcd09c10f432a320ceaac29a14a Mon Sep 17 00:00:00 2001
a3a04f
From: Alexander Bokovoy <ab@samba.org>
a3a04f
Date: Fri, 3 Mar 2017 17:08:09 +0200
a3a04f
Subject: [PATCH 1/6] gssapi: check for gss_acquire_cred_from
a3a04f
a3a04f
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12611
a3a04f
a3a04f
Signed-off-by: Alexander Bokovoy <ab@samba.org>
a3a04f
Reviewed-by: Stefan Metzmacher <metze@samba.org>
a3a04f
(cherry picked from commit d630a364f9d74443e482934f76cd7107c331e108)
a3a04f
---
a3a04f
 wscript_configure_system_mitkrb5 | 1 +
a3a04f
 1 file changed, 1 insertion(+)
a3a04f
a3a04f
diff --git a/wscript_configure_system_mitkrb5 b/wscript_configure_system_mitkrb5
a3a04f
index 06a9821..d3e8ebf 100644
a3a04f
--- a/wscript_configure_system_mitkrb5
a3a04f
+++ b/wscript_configure_system_mitkrb5
a3a04f
@@ -92,6 +92,7 @@ conf.CHECK_FUNCS_IN('''
a3a04f
        gsskrb5_extract_authz_data_from_sec_context
a3a04f
        gss_krb5_export_lucid_sec_context
a3a04f
        gss_import_cred gss_export_cred
a3a04f
+       gss_acquire_cred_from
a3a04f
        ''', 'gssapi gssapi_krb5')
a3a04f
 conf.CHECK_VARIABLE('GSS_KRB5_CRED_NO_CI_FLAGS_X', headers=possible_gssapi_headers)
a3a04f
 conf.CHECK_FUNCS_IN('krb5_mk_req_extended krb5_kt_compare', 'krb5')
a3a04f
-- 
a3a04f
2.9.3
a3a04f
a3a04f
a3a04f
From 4b4a95436a56ee91e6bef8e905656c387ce2f62c Mon Sep 17 00:00:00 2001
a3a04f
From: Alexander Bokovoy <ab@samba.org>
a3a04f
Date: Fri, 3 Mar 2017 16:14:57 +0200
a3a04f
Subject: [PATCH 2/6] lib/krb5_wrap: add smb_gss_krb5_import_cred wrapper
a3a04f
a3a04f
Wrap gss_krb5_import_cred() to allow re-implementing it with
a3a04f
gss_acquire_cred_from() for newer MIT versions. gss_acquire_cred_from()
a3a04f
works fine with GSSAPI interposer (GSS-proxy) while
a3a04f
gss_krb5_import_cred() is not interposed yet.
a3a04f
a3a04f
The wrapper has additional parameter, krb5_context handle, to facilitate
a3a04f
with credentials cache name discovery. All our callers to
a3a04f
gss_krb5_import_cred() already have krb5 context handy.
a3a04f
a3a04f
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12611
a3a04f
a3a04f
Signed-off-by: Alexander Bokovoy <ab@samba.org>
a3a04f
Reviewed-by: Stefan Metzmacher <metze@samba.org>
a3a04f
(cherry picked from commit 0e6e8dd2600c699a7a02e3d11fed21b5bc49858d)
a3a04f
---
a3a04f
 lib/krb5_wrap/gss_samba.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++
a3a04f
 lib/krb5_wrap/gss_samba.h |  13 +++++
a3a04f
 2 files changed, 134 insertions(+)
a3a04f
a3a04f
diff --git a/lib/krb5_wrap/gss_samba.c b/lib/krb5_wrap/gss_samba.c
a3a04f
index b444633..757ffc5 100644
a3a04f
--- a/lib/krb5_wrap/gss_samba.c
a3a04f
+++ b/lib/krb5_wrap/gss_samba.c
a3a04f
@@ -48,4 +48,125 @@ int smb_gss_oid_equal(const gss_OID first_oid, const gss_OID second_oid)
a3a04f
 }
a3a04f
 #endif /* !HAVE_GSS_OID_EQUAL */
a3a04f
 
a3a04f
+
a3a04f
+/* wrapper around gss_krb5_import_cred() that prefers to use gss_acquire_cred_from()
a3a04f
+ * if this GSSAPI extension is available. gss_acquire_cred_from() is properly
a3a04f
+ * interposed by GSSPROXY while gss_krb5_import_cred() is not.
a3a04f
+ *
a3a04f
+ * This wrapper requires a proper krb5_context to resolve ccache name.
a3a04f
+ * All gss_krb5_import_cred() callers in Samba already have krb5_context available. */
a3a04f
+uint32_t smb_gss_krb5_import_cred(uint32_t *minor_status, krb5_context ctx,
a3a04f
+				  krb5_ccache id, krb5_principal keytab_principal,
a3a04f
+				  krb5_keytab keytab, gss_cred_id_t *cred)
a3a04f
+{
a3a04f
+	uint32_t major_status = 0;
a3a04f
+
a3a04f
+#if HAVE_GSS_ACQUIRE_CRED_FROM
a3a04f
+	uint32_t minor = 0;
a3a04f
+	gss_key_value_element_desc ccache_element = {
a3a04f
+		.key = "ccache",
a3a04f
+		.value = NULL,
a3a04f
+	};
a3a04f
+
a3a04f
+	gss_key_value_element_desc keytab_element = {
a3a04f
+		.key = "keytab",
a3a04f
+		.value = NULL,
a3a04f
+	};
a3a04f
+
a3a04f
+	gss_key_value_element_desc elements[2];
a3a04f
+
a3a04f
+	gss_key_value_set_desc cred_store = {
a3a04f
+		.elements = &ccache_element,
a3a04f
+		.count = 1,
a3a04f
+	};
a3a04f
+
a3a04f
+	gss_OID_set mech_set = GSS_C_NO_OID_SET;
a3a04f
+	gss_cred_usage_t cred_usage = GSS_C_INITIATE;
a3a04f
+	gss_name_t name = NULL;
a3a04f
+	gss_buffer_desc pr_name = {
a3a04f
+		.value = NULL,
a3a04f
+		.length = 0,
a3a04f
+	};
a3a04f
+
a3a04f
+	if (id != NULL) {
a3a04f
+		major_status = krb5_cc_get_full_name(ctx,
a3a04f
+						     id,
a3a04f
+						     discard_const(&ccache_element.value));
a3a04f
+		if (major_status != 0) {
a3a04f
+			return major_status;
a3a04f
+		}
a3a04f
+	}
a3a04f
+
a3a04f
+	if (keytab != NULL) {
a3a04f
+		keytab_element.value = malloc(4096);
a3a04f
+		if (!keytab_element.value) {
a3a04f
+			return ENOMEM;
a3a04f
+		}
a3a04f
+		major_status = krb5_kt_get_name(ctx,
a3a04f
+						keytab,
a3a04f
+						discard_const(keytab_element.value), 4096);
a3a04f
+		if (major_status != 0) {
a3a04f
+			free(discard_const(keytab_element.value));
a3a04f
+			return major_status;
a3a04f
+		}
a3a04f
+		cred_usage = GSS_C_ACCEPT;
a3a04f
+		cred_store.elements = &keytab_element;
a3a04f
+
a3a04f
+		if (keytab_principal != NULL) {
a3a04f
+			major_status = krb5_unparse_name(ctx, keytab_principal, (char**)&pr_name.value);
a3a04f
+			if (major_status != 0) {
a3a04f
+				free(discard_const(keytab_element.value));
a3a04f
+				return major_status;
a3a04f
+			}
a3a04f
+			pr_name.length = strlen(pr_name.value);
a3a04f
+
a3a04f
+			major_status = gss_import_name(minor_status,
a3a04f
+						       &pr_name,
a3a04f
+						       discard_const(GSS_KRB5_NT_PRINCIPAL_NAME),
a3a04f
+						       &name);
a3a04f
+			if (major_status != 0) {
a3a04f
+				krb5_free_unparsed_name(ctx, pr_name.value);
a3a04f
+				free(discard_const(keytab_element.value));
a3a04f
+				return major_status;
a3a04f
+			}
a3a04f
+		}
a3a04f
+	}
a3a04f
+
a3a04f
+	if (id != NULL && keytab != NULL) {
a3a04f
+		elements[0] = ccache_element;
a3a04f
+		elements[1] = keytab_element;
a3a04f
+
a3a04f
+		cred_store.elements = elements;
a3a04f
+		cred_store.count = 2;
a3a04f
+		cred_usage = GSS_C_BOTH;
a3a04f
+	}
a3a04f
+
a3a04f
+	major_status = gss_acquire_cred_from(minor_status,
a3a04f
+					     name,
a3a04f
+					     0,
a3a04f
+					     mech_set,
a3a04f
+					     cred_usage,
a3a04f
+					     &cred_store,
a3a04f
+					     cred,
a3a04f
+					     NULL,
a3a04f
+					     NULL);
a3a04f
+
a3a04f
+	if (pr_name.value != NULL) {
a3a04f
+		(void)gss_release_name(&minor, &name);
a3a04f
+		krb5_free_unparsed_name(ctx, pr_name.value);
a3a04f
+	}
a3a04f
+	if (keytab_element.value != NULL) {
a3a04f
+		free(discard_const(keytab_element.value));
a3a04f
+	}
a3a04f
+	krb5_free_string(ctx, discard_const(ccache_element.value));
a3a04f
+#else
a3a04f
+	major_status = gss_krb5_import_cred(minor_status,
a3a04f
+					id,
a3a04f
+					keytab_principal,
a3a04f
+					keytab, cred);
a3a04f
+#endif
a3a04f
+	return major_status;
a3a04f
+}
a3a04f
+
a3a04f
+
a3a04f
 #endif /* HAVE_GSSAPI */
a3a04f
diff --git a/lib/krb5_wrap/gss_samba.h b/lib/krb5_wrap/gss_samba.h
a3a04f
index 5319932..89aee34 100644
a3a04f
--- a/lib/krb5_wrap/gss_samba.h
a3a04f
+++ b/lib/krb5_wrap/gss_samba.h
a3a04f
@@ -25,6 +25,7 @@
a3a04f
 #ifdef HAVE_GSSAPI
a3a04f
 
a3a04f
 #include "system/gssapi.h"
a3a04f
+#include "krb5_samba.h"
a3a04f
 
a3a04f
 #if defined(HAVE_GSS_OID_EQUAL)
a3a04f
 #define smb_gss_oid_equal gss_oid_equal
a3a04f
@@ -32,5 +33,17 @@
a3a04f
 int smb_gss_oid_equal(const gss_OID first_oid, const gss_OID second_oid);
a3a04f
 #endif /* HAVE_GSS_OID_EQUAL */
a3a04f
 
a3a04f
+/* wrapper around gss_krb5_import_cred() that prefers to use gss_acquire_cred_from()
a3a04f
+ * if this GSSAPI extension is available. gss_acquire_cred_from() is properly
a3a04f
+ * interposed by GSS-proxy while gss_krb5_import_cred() is not.
a3a04f
+ *
a3a04f
+ * This wrapper requires a proper krb5_context to resolve the ccache name for
a3a04f
+ * gss_acquire_cred_from().
a3a04f
+ *
a3a04f
+ * All gss_krb5_import_cred() callers in Samba already have krb5_context available. */
a3a04f
+uint32_t smb_gss_krb5_import_cred(OM_uint32 *minor_status, krb5_context ctx,
a3a04f
+				  krb5_ccache id, krb5_principal keytab_principal,
a3a04f
+				  krb5_keytab keytab, gss_cred_id_t *cred);
a3a04f
+
a3a04f
 #endif /* HAVE_GSSAPI */
a3a04f
 #endif /* _GSS_SAMBA_H */
a3a04f
-- 
a3a04f
2.9.3
a3a04f
a3a04f
a3a04f
From f06fafce32a27acf4028ab573297c64189b62e30 Mon Sep 17 00:00:00 2001
a3a04f
From: Alexander Bokovoy <ab@samba.org>
a3a04f
Date: Fri, 3 Mar 2017 16:57:13 +0200
a3a04f
Subject: [PATCH 3/6] credentials_krb5: convert to use smb_gss_krb5_import_cred
a3a04f
a3a04f
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12611
a3a04f
a3a04f
Signed-off-by: Alexander Bokovoy <ab@samba.org>
a3a04f
Reviewed-by: Stefan Metzmacher <metze@samba.org>
a3a04f
(cherry picked from commit ca8fd793930173b4e625d3f286739de214155bc1)
a3a04f
---
a3a04f
 auth/credentials/credentials_krb5.c | 22 +++++++++++++---------
a3a04f
 1 file changed, 13 insertions(+), 9 deletions(-)
a3a04f
a3a04f
diff --git a/auth/credentials/credentials_krb5.c b/auth/credentials/credentials_krb5.c
a3a04f
index e974df9..0e68012 100644
a3a04f
--- a/auth/credentials/credentials_krb5.c
a3a04f
+++ b/auth/credentials/credentials_krb5.c
a3a04f
@@ -579,8 +579,9 @@ _PUBLIC_ int cli_credentials_get_client_gss_creds(struct cli_credentials *cred,
a3a04f
 		return ENOMEM;
a3a04f
 	}
a3a04f
 
a3a04f
-	maj_stat = gss_krb5_import_cred(&min_stat, ccache->ccache, NULL, NULL, 
a3a04f
-					&gcc->creds);
a3a04f
+	maj_stat = smb_gss_krb5_import_cred(&min_stat, ccache->smb_krb5_context->krb5_context,
a3a04f
+					    ccache->ccache, NULL, NULL,
a3a04f
+					    &gcc->creds);
a3a04f
 	if ((maj_stat == GSS_S_FAILURE) &&
a3a04f
 	    (min_stat == (OM_uint32)KRB5_CC_END ||
a3a04f
 	     min_stat == (OM_uint32)KRB5_CC_NOTFOUND ||
a3a04f
@@ -597,8 +598,9 @@ _PUBLIC_ int cli_credentials_get_client_gss_creds(struct cli_credentials *cred,
a3a04f
 			return ret;
a3a04f
 		}
a3a04f
 
a3a04f
-		maj_stat = gss_krb5_import_cred(&min_stat, ccache->ccache, NULL, NULL,
a3a04f
-						&gcc->creds);
a3a04f
+		maj_stat = smb_gss_krb5_import_cred(&min_stat, ccache->smb_krb5_context->krb5_context,
a3a04f
+						    ccache->ccache, NULL, NULL,
a3a04f
+						    &gcc->creds);
a3a04f
 
a3a04f
 	}
a3a04f
 
a3a04f
@@ -609,7 +611,7 @@ _PUBLIC_ int cli_credentials_get_client_gss_creds(struct cli_credentials *cred,
a3a04f
 		} else {
a3a04f
 			ret = EINVAL;
a3a04f
 		}
a3a04f
-		(*error_string) = talloc_asprintf(cred, "gss_krb5_import_cred failed: %s", error_message(ret));
a3a04f
+		(*error_string) = talloc_asprintf(cred, "smb_gss_krb5_import_cred failed: %s", error_message(ret));
a3a04f
 		return ret;
a3a04f
 	}
a3a04f
 
a3a04f
@@ -1076,12 +1078,14 @@ _PUBLIC_ int cli_credentials_get_server_gss_creds(struct cli_credentials *cred,
a3a04f
 
a3a04f
 	if (ktc->password_based || obtained < CRED_SPECIFIED) {
a3a04f
 		/* This creates a GSSAPI cred_id_t for match-by-key with only the keytab set */
a3a04f
-		maj_stat = gss_krb5_import_cred(&min_stat, NULL, NULL, ktc->keytab,
a3a04f
-						&gcc->creds);
a3a04f
+		maj_stat = smb_gss_krb5_import_cred(&min_stat, smb_krb5_context->krb5_context,
a3a04f
+						    NULL, NULL, ktc->keytab,
a3a04f
+						    &gcc->creds);
a3a04f
 	} else {
a3a04f
 		/* This creates a GSSAPI cred_id_t with the principal and keytab set, matching by name */
a3a04f
-		maj_stat = gss_krb5_import_cred(&min_stat, NULL, princ, ktc->keytab,
a3a04f
-						&gcc->creds);
a3a04f
+		maj_stat = smb_gss_krb5_import_cred(&min_stat, smb_krb5_context->krb5_context,
a3a04f
+						    NULL, princ, ktc->keytab,
a3a04f
+						    &gcc->creds);
a3a04f
 	}
a3a04f
 	if (maj_stat) {
a3a04f
 		if (min_stat) {
a3a04f
-- 
a3a04f
2.9.3
a3a04f
a3a04f
a3a04f
From 5305bffd4c72a85cc6c3148222ef7e346cbe3d87 Mon Sep 17 00:00:00 2001
a3a04f
From: Alexander Bokovoy <ab@samba.org>
a3a04f
Date: Fri, 3 Mar 2017 16:57:50 +0200
a3a04f
Subject: [PATCH 4/6] libads: convert to use smb_gss_krb5_import_cred
a3a04f
a3a04f
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12611
a3a04f
a3a04f
Signed-off-by: Alexander Bokovoy <ab@samba.org>
a3a04f
Reviewed-by: Stefan Metzmacher <metze@samba.org>
a3a04f
(cherry picked from commit 520167992bd2477bc11920d2dc9ec87f2cb339c9)
a3a04f
---
a3a04f
 source3/libads/sasl.c | 2 +-
a3a04f
 1 file changed, 1 insertion(+), 1 deletion(-)
a3a04f
a3a04f
diff --git a/source3/libads/sasl.c b/source3/libads/sasl.c
a3a04f
index 8570788..30127fa 100644
a3a04f
--- a/source3/libads/sasl.c
a3a04f
+++ b/source3/libads/sasl.c
a3a04f
@@ -372,7 +372,7 @@ static ADS_STATUS ads_init_gssapi_cred(ADS_STRUCT *ads, gss_cred_id_t *cred)
a3a04f
 		goto done;
a3a04f
 	}
a3a04f
 
a3a04f
-	maj = gss_krb5_import_cred(&min, kccache, NULL, NULL, cred);
a3a04f
+	maj = smb_gss_krb5_import_cred(&min, kctx, kccache, NULL, NULL, cred);
a3a04f
 	if (maj != GSS_S_COMPLETE) {
a3a04f
 		status = ADS_ERROR_GSS(maj, min);
a3a04f
 		goto done;
a3a04f
-- 
a3a04f
2.9.3
a3a04f
a3a04f
a3a04f
From 1dbc68f9bee19a9c26825cc5be7d81951dcac710 Mon Sep 17 00:00:00 2001
a3a04f
From: Alexander Bokovoy <ab@samba.org>
a3a04f
Date: Fri, 3 Mar 2017 16:58:14 +0200
a3a04f
Subject: [PATCH 5/6] s3-gse: convert to use smb_gss_krb5_import_cred
a3a04f
a3a04f
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12611
a3a04f
a3a04f
Signed-off-by: Alexander Bokovoy <ab@samba.org>
a3a04f
Reviewed-by: Stefan Metzmacher <metze@samba.org>
a3a04f
(cherry picked from commit 3d733d5791a6d82edda13ac39790bd8ba893f3d7)
a3a04f
---
a3a04f
 source3/librpc/crypto/gse.c | 20 +++++++++++---------
a3a04f
 1 file changed, 11 insertions(+), 9 deletions(-)
a3a04f
a3a04f
diff --git a/source3/librpc/crypto/gse.c b/source3/librpc/crypto/gse.c
a3a04f
index abf20bc..f4238f3 100644
a3a04f
--- a/source3/librpc/crypto/gse.c
a3a04f
+++ b/source3/librpc/crypto/gse.c
a3a04f
@@ -252,11 +252,12 @@ static NTSTATUS gse_init_client(TALLOC_CTX *mem_ctx,
a3a04f
 	/* TODO: get krb5 ticket using username/password, if no valid
a3a04f
 	 * one already available in ccache */
a3a04f
 
a3a04f
-	gss_maj = gss_krb5_import_cred(&gss_min,
a3a04f
-				       gse_ctx->ccache,
a3a04f
-				       NULL, /* keytab_principal */
a3a04f
-				       NULL, /* keytab */
a3a04f
-				       &gse_ctx->creds);
a3a04f
+	gss_maj = smb_gss_krb5_import_cred(&gss_min,
a3a04f
+					   gse_ctx->k5ctx,
a3a04f
+					   gse_ctx->ccache,
a3a04f
+					   NULL, /* keytab_principal */
a3a04f
+					   NULL, /* keytab */
a3a04f
+					   &gse_ctx->creds);
a3a04f
 	if (gss_maj) {
a3a04f
 		char *ccache = NULL;
a3a04f
 		int kret;
a3a04f
@@ -268,7 +269,7 @@ static NTSTATUS gse_init_client(TALLOC_CTX *mem_ctx,
a3a04f
 			ccache = NULL;
a3a04f
 		}
a3a04f
 
a3a04f
-		DEBUG(5, ("gss_krb5_import_cred ccache[%s] failed with [%s] -"
a3a04f
+		DEBUG(5, ("smb_gss_krb5_import_cred ccache[%s] failed with [%s] -"
a3a04f
 			  "the caller may retry after a kinit.\n",
a3a04f
 			  ccache, gse_errstr(gse_ctx, gss_maj, gss_min)));
a3a04f
 		SAFE_FREE(ccache);
a3a04f
@@ -430,12 +431,13 @@ static NTSTATUS gse_init_server(TALLOC_CTX *mem_ctx,
a3a04f
 	}
a3a04f
 
a3a04f
 	/* This creates a GSSAPI cred_id_t with the keytab set */
a3a04f
-	gss_maj = gss_krb5_import_cred(&gss_min, NULL, NULL, gse_ctx->keytab, 
a3a04f
-				       &gse_ctx->creds);
a3a04f
+	gss_maj = smb_gss_krb5_import_cred(&gss_min, gse_ctx->k5ctx,
a3a04f
+					   NULL, NULL, gse_ctx->keytab,
a3a04f
+					   &gse_ctx->creds);
a3a04f
 
a3a04f
 	if (gss_maj != 0
a3a04f
 	    && gss_maj != (GSS_S_CALL_BAD_STRUCTURE|GSS_S_BAD_NAME)) {
a3a04f
-		DEBUG(0, ("gss_krb5_import_cred failed with [%s]\n",
a3a04f
+		DEBUG(0, ("smb_gss_krb5_import_cred failed with [%s]\n",
a3a04f
 			  gse_errstr(gse_ctx, gss_maj, gss_min)));
a3a04f
 		status = NT_STATUS_INTERNAL_ERROR;
a3a04f
 		goto done;
a3a04f
-- 
a3a04f
2.9.3
a3a04f
a3a04f
a3a04f
From 3c9390d26cf12e483d98f005b43da7b10348753d Mon Sep 17 00:00:00 2001
a3a04f
From: Alexander Bokovoy <ab@samba.org>
a3a04f
Date: Wed, 8 Mar 2017 12:38:49 +0200
a3a04f
Subject: [PATCH 6/6] s3-gse: move krb5 fallback to smb_gss_krb5_import_cred
a3a04f
 wrapper
a3a04f
a3a04f
MIT krb5 1.9 version of gss_krb5_import_cred() may fail when importing
a3a04f
credentials from a keytab without specifying actual principal.
a3a04f
This was fixed in MIT krb5 1.9.2 (see commit
a3a04f
71c3be093db577aa52f6b9a9a3a9f442ca0d8f20 in MIT krb5-1.9 branch, git
a3a04f
master's version is bd18687a705a8a6cdcb7c140764d1a7c6a3381b5).
a3a04f
a3a04f
Move fallback code to the smb_gss_krb5_import_cred wrapper. We only
a3a04f
expect this fallback to happen with krb5 GSSAPI mechanism, thus hard
a3a04f
code use of krb5 mech when calling to gss_acquire_cred.
a3a04f
a3a04f
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12611
a3a04f
a3a04f
Signed-off-by: Alexander Bokovoy <ab@samba.org>
a3a04f
Reviewed-by: Stefan Metzmacher <metze@samba.org>
a3a04f
a3a04f
Autobuild-User(master): Alexander Bokovoy <ab@samba.org>
a3a04f
Autobuild-Date(master): Wed Mar  8 22:00:24 CET 2017 on sn-devel-144
a3a04f
a3a04f
(cherry picked from commit 57286d57732d49fdb8b8e21f584787cdbc917c32)
a3a04f
---
a3a04f
 lib/krb5_wrap/gss_samba.c   | 46 +++++++++++++++++++++++++++++++++++++++---
a3a04f
 source3/librpc/crypto/gse.c | 49 +--------------------------------------------
a3a04f
 2 files changed, 44 insertions(+), 51 deletions(-)
a3a04f
a3a04f
diff --git a/lib/krb5_wrap/gss_samba.c b/lib/krb5_wrap/gss_samba.c
a3a04f
index 757ffc5..9e5ad4a 100644
a3a04f
--- a/lib/krb5_wrap/gss_samba.c
a3a04f
+++ b/lib/krb5_wrap/gss_samba.c
a3a04f
@@ -161,9 +161,49 @@ uint32_t smb_gss_krb5_import_cred(uint32_t *minor_status, krb5_context ctx,
a3a04f
 	krb5_free_string(ctx, discard_const(ccache_element.value));
a3a04f
 #else
a3a04f
 	major_status = gss_krb5_import_cred(minor_status,
a3a04f
-					id,
a3a04f
-					keytab_principal,
a3a04f
-					keytab, cred);
a3a04f
+					    id,
a3a04f
+					    keytab_principal,
a3a04f
+					    keytab, cred);
a3a04f
+
a3a04f
+	if (major_status == (GSS_S_CALL_BAD_STRUCTURE|GSS_S_BAD_NAME)) {
a3a04f
+		if ((keytab_principal == NULL) && (keytab != NULL)) {
a3a04f
+			/* No principal was specified and MIT krb5 1.9 version failed.
a3a04f
+			 * We have to fall back to set global acceptor identity */
a3a04f
+			gss_OID_set_desc mech_set;
a3a04f
+			char *kt_name = NULL;
a3a04f
+
a3a04f
+			kt_name = malloc(4096);
a3a04f
+			if (!kt_name) {
a3a04f
+				return ENOMEM;
a3a04f
+			}
a3a04f
+
a3a04f
+			major_status = krb5_kt_get_name(ctx,
a3a04f
+							keytab,
a3a04f
+							kt_name, 4096);
a3a04f
+			if (major_status != 0) {
a3a04f
+				free(kt_name);
a3a04f
+				return major_status;
a3a04f
+			}
a3a04f
+
a3a04f
+			major_status = gsskrb5_register_acceptor_identity(kt_name);
a3a04f
+			if (major_status) {
a3a04f
+				free(kt_name);
a3a04f
+				return major_status;
a3a04f
+			}
a3a04f
+
a3a04f
+			/* We are dealing with krb5 GSSAPI mech in this fallback */
a3a04f
+			mech_set.count = 1;
a3a04f
+			mech_set.elements = gss_mech_krb5;
a3a04f
+			major_status = gss_acquire_cred(minor_status,
a3a04f
+							GSS_C_NO_NAME,
a3a04f
+							GSS_C_INDEFINITE,
a3a04f
+							&mech_set,
a3a04f
+							GSS_C_ACCEPT,
a3a04f
+							cred,
a3a04f
+							NULL, NULL);
a3a04f
+			free(kt_name);
a3a04f
+		}
a3a04f
+	}
a3a04f
 #endif
a3a04f
 	return major_status;
a3a04f
 }
a3a04f
diff --git a/source3/librpc/crypto/gse.c b/source3/librpc/crypto/gse.c
a3a04f
index f4238f3..a111320 100644
a3a04f
--- a/source3/librpc/crypto/gse.c
a3a04f
+++ b/source3/librpc/crypto/gse.c
a3a04f
@@ -435,58 +435,11 @@ static NTSTATUS gse_init_server(TALLOC_CTX *mem_ctx,
a3a04f
 					   NULL, NULL, gse_ctx->keytab,
a3a04f
 					   &gse_ctx->creds);
a3a04f
 
a3a04f
-	if (gss_maj != 0
a3a04f
-	    && gss_maj != (GSS_S_CALL_BAD_STRUCTURE|GSS_S_BAD_NAME)) {
a3a04f
+	if (gss_maj != 0) {
a3a04f
 		DEBUG(0, ("smb_gss_krb5_import_cred failed with [%s]\n",
a3a04f
 			  gse_errstr(gse_ctx, gss_maj, gss_min)));
a3a04f
 		status = NT_STATUS_INTERNAL_ERROR;
a3a04f
 		goto done;
a3a04f
-
a3a04f
-		/* This is the error the MIT krb5 1.9 gives when it
a3a04f
-		 * implements the function, but we do not specify the
a3a04f
-		 * principal.  However, when we specify the principal
a3a04f
-		 * as host$@REALM the GSS acceptor fails with 'wrong
a3a04f
-		 * principal in request'.  Work around the issue by
a3a04f
-		 * falling back to the alternate approach below. */
a3a04f
-	} else if (gss_maj == (GSS_S_CALL_BAD_STRUCTURE|GSS_S_BAD_NAME))
a3a04f
-	/* FIXME!!!
a3a04f
-	 * This call sets the default keytab for the whole server, not
a3a04f
-	 * just for this context. Need to find a way that does not alter
a3a04f
-	 * the state of the whole server ... */
a3a04f
-	{
a3a04f
-		const char *ktname;
a3a04f
-		gss_OID_set_desc mech_set;
a3a04f
-
a3a04f
-		ret = smb_krb5_kt_get_name(gse_ctx, gse_ctx->k5ctx,
a3a04f
-				   gse_ctx->keytab, &ktname);
a3a04f
-		if (ret) {
a3a04f
-			status = NT_STATUS_INTERNAL_ERROR;
a3a04f
-			goto done;
a3a04f
-		}
a3a04f
-
a3a04f
-		ret = gsskrb5_register_acceptor_identity(ktname);
a3a04f
-		if (ret) {
a3a04f
-			status = NT_STATUS_INTERNAL_ERROR;
a3a04f
-			goto done;
a3a04f
-		}
a3a04f
-
a3a04f
-		mech_set.count = 1;
a3a04f
-		mech_set.elements = &gse_ctx->gss_mech;
a3a04f
-
a3a04f
-		gss_maj = gss_acquire_cred(&gss_min,
a3a04f
-				   GSS_C_NO_NAME,
a3a04f
-				   GSS_C_INDEFINITE,
a3a04f
-				   &mech_set,
a3a04f
-				   GSS_C_ACCEPT,
a3a04f
-				   &gse_ctx->creds,
a3a04f
-				   NULL, NULL);
a3a04f
-
a3a04f
-		if (gss_maj) {
a3a04f
-			DEBUG(0, ("gss_acquire_creds failed with [%s]\n",
a3a04f
-				  gse_errstr(gse_ctx, gss_maj, gss_min)));
a3a04f
-			status = NT_STATUS_INTERNAL_ERROR;
a3a04f
-			goto done;
a3a04f
-		}
a3a04f
 	}
a3a04f
 
a3a04f
 	status = NT_STATUS_OK;
a3a04f
-- 
a3a04f
2.9.3
a3a04f