7936a1
From 0b196043f08ea4c025f19c4519175a3a73e1d185 Mon Sep 17 00:00:00 2001
7936a1
From: Isaac Boukris <iboukris@gmail.com>
7936a1
Date: Fri, 27 Sep 2019 18:25:03 +0300
7936a1
Subject: [PATCH 1/3] mit-kdc: add basic loacl realm S4U support
7936a1
7936a1
Signed-off-by: Isaac Boukris <iboukris@gmail.com>
7936a1
Pair-Programmed-With: Andreas Schneider <asn@samba.org>
7936a1
---
7936a1
 source4/kdc/mit-kdb/kdb_samba_policies.c | 124 +++++++++++------------
7936a1
 source4/kdc/mit_samba.c                  |  47 ++-------
7936a1
 source4/kdc/mit_samba.h                  |   6 +-
7936a1
 3 files changed, 71 insertions(+), 106 deletions(-)
7936a1
7936a1
diff --git a/source4/kdc/mit-kdb/kdb_samba_policies.c b/source4/kdc/mit-kdb/kdb_samba_policies.c
7936a1
index f35210669c2..b1c7c5dcc5e 100644
7936a1
--- a/source4/kdc/mit-kdb/kdb_samba_policies.c
7936a1
+++ b/source4/kdc/mit-kdb/kdb_samba_policies.c
7936a1
@@ -195,13 +195,17 @@ static krb5_error_code ks_verify_pac(krb5_context context,
7936a1
 				     krb5_keyblock *krbtgt_key,
7936a1
 				     krb5_timestamp authtime,
7936a1
 				     krb5_authdata **tgt_auth_data,
7936a1
-				     krb5_pac *pac)
7936a1
+				     krb5_pac *out_pac)
7936a1
 {
7936a1
 	struct mit_samba_context *mit_ctx;
7936a1
 	krb5_authdata **authdata = NULL;
7936a1
-	krb5_pac ipac = NULL;
7936a1
-	DATA_BLOB logon_data = { NULL, 0 };
7936a1
+	krb5_keyblock *header_server_key = NULL;
7936a1
+	krb5_key_data *impersonator_kd = NULL;
7936a1
+	krb5_keyblock impersonator_key = {0};
7936a1
 	krb5_error_code code;
7936a1
+	krb5_pac pac;
7936a1
+
7936a1
+	*out_pac = NULL;
7936a1
 
7936a1
 	mit_ctx = ks_get_context(context);
7936a1
 	if (mit_ctx == NULL) {
7936a1
@@ -233,41 +237,43 @@ static krb5_error_code ks_verify_pac(krb5_context context,
7936a1
 	code = krb5_pac_parse(context,
7936a1
 			      authdata[0]->contents,
7936a1
 			      authdata[0]->length,
7936a1
-			      &ipac);
7936a1
+			      &pac;;
7936a1
 	if (code != 0) {
7936a1
 		goto done;
7936a1
 	}
7936a1
 
7936a1
-	/* TODO: verify this is correct
7936a1
-	 *
7936a1
-	 * In the constrained delegation case, the PAC is from a service
7936a1
-	 * ticket rather than a TGT; we must verify the server and KDC
7936a1
-	 * signatures to assert that the server did not forge the PAC.
7936a1
+	/*
7936a1
+	 * For constrained delegation in MIT version < 1.18 we aren't provided
7936a1
+	 * with the 2nd ticket server key to verify the PAC.
7936a1
+	 * We can workaround that by fetching the key from the client db entry,
7936a1
+	 * which is the impersonator account in that version.
7936a1
+	 * TODO: use the provided entry in the new 1.18 version.
7936a1
 	 */
7936a1
 	if (flags & KRB5_KDB_FLAG_CONSTRAINED_DELEGATION) {
7936a1
-		code = krb5_pac_verify(context,
7936a1
-				       ipac,
7936a1
-				       authtime,
7936a1
-				       client_princ,
7936a1
-				       server_key,
7936a1
-				       krbtgt_key);
7936a1
+		/* The impersonator must be local. */
7936a1
+		if (client == NULL) {
7936a1
+			code = KRB5KDC_ERR_BADOPTION;
7936a1
+			goto done;
7936a1
+		}
7936a1
+		/* Fetch and decrypt 2nd ticket server's current key. */
7936a1
+		code = krb5_dbe_find_enctype(context, client, -1, -1, 0,
7936a1
+					     &impersonator_kd);
7936a1
+		if (code != 0) {
7936a1
+			goto done;
7936a1
+		}
7936a1
+		code = krb5_dbe_decrypt_key_data(context, NULL,
7936a1
+						 impersonator_kd,
7936a1
+						 &impersonator_key, NULL);
7936a1
+		if (code != 0) {
7936a1
+			goto done;
7936a1
+		}
7936a1
+		header_server_key = &impersonator_key;
7936a1
 	} else {
7936a1
-		code = krb5_pac_verify(context,
7936a1
-				       ipac,
7936a1
-				       authtime,
7936a1
-				       client_princ,
7936a1
-				       krbtgt_key,
7936a1
-				       NULL);
7936a1
-	}
7936a1
-	if (code != 0) {
7936a1
-		goto done;
7936a1
+		header_server_key = krbtgt_key;
7936a1
 	}
7936a1
 
7936a1
-	/* check and update PAC */
7936a1
-	code = krb5_pac_parse(context,
7936a1
-			      authdata[0]->contents,
7936a1
-			      authdata[0]->length,
7936a1
-			      pac);
7936a1
+	code = krb5_pac_verify(context, pac, authtime, client_princ,
7936a1
+			       header_server_key, NULL);
7936a1
 	if (code != 0) {
7936a1
 		goto done;
7936a1
 	}
7936a1
@@ -275,17 +281,22 @@ static krb5_error_code ks_verify_pac(krb5_context context,
7936a1
 	code = mit_samba_reget_pac(mit_ctx,
7936a1
 				   context,
7936a1
 				   flags,
7936a1
-				   client_princ,
7936a1
 				   client,
7936a1
 				   server,
7936a1
 				   krbtgt,
7936a1
 				   krbtgt_key,
7936a1
-				   pac);
7936a1
+				   &pac;;
7936a1
+	if (code != 0) {
7936a1
+		goto done;
7936a1
+	}
7936a1
+
7936a1
+	*out_pac = pac;
7936a1
+	pac = NULL;
7936a1
 
7936a1
 done:
7936a1
+	krb5_free_keyblock_contents(context, &impersonator_key);
7936a1
 	krb5_free_authdata(context, authdata);
7936a1
-	krb5_pac_free(context, ipac);
7936a1
-	free(logon_data.data);
7936a1
+	krb5_pac_free(context, pac);
7936a1
 
7936a1
 	return code;
7936a1
 }
7936a1
@@ -314,6 +325,7 @@ krb5_error_code kdb_samba_db_sign_auth_data(krb5_context context,
7936a1
 	krb5_authdata **pac_auth_data = NULL;
7936a1
 	krb5_authdata **authdata = NULL;
7936a1
 	krb5_boolean is_as_req;
7936a1
+	krb5_const_principal pac_client;
7936a1
 	krb5_error_code code;
7936a1
 	krb5_pac pac = NULL;
7936a1
 	krb5_data pac_data;
7936a1
@@ -325,11 +337,6 @@ krb5_error_code kdb_samba_db_sign_auth_data(krb5_context context,
7936a1
 	krbtgt = krbtgt == NULL ? local_krbtgt : krbtgt;
7936a1
 	krbtgt_key = krbtgt_key == NULL ? local_krbtgt_key : krbtgt_key;
7936a1
 
7936a1
-	/* FIXME: We don't support S4U yet */
7936a1
-	if (flags & KRB5_KDB_FLAGS_S4U) {
7936a1
-		return KRB5_KDB_DBTYPE_NOSUP;
7936a1
-	}
7936a1
-
7936a1
 	is_as_req = ((flags & KRB5_KDB_FLAG_CLIENT_REFERRALS_ONLY) != 0);
7936a1
 
7936a1
 	/*
7936a1
@@ -390,6 +397,16 @@ krb5_error_code kdb_samba_db_sign_auth_data(krb5_context context,
7936a1
 		ks_client_princ = client->princ;
7936a1
 	}
7936a1
 
7936a1
+	/* In protocol transition, we are currently not provided with the tgt
7936a1
+	 * client name to verify the PAC, we could probably skip the name
7936a1
+	 * verification and just verify the signatures, but since we don't
7936a1
+	 * support cross-realm nor aliases, we can just use server->princ */
7936a1
+	if (flags & KRB5_KDB_FLAG_PROTOCOL_TRANSITION) {
7936a1
+		pac_client = server->princ;
7936a1
+	} else {
7936a1
+		pac_client = ks_client_princ;
7936a1
+	}
7936a1
+
7936a1
 	if (client_entry == NULL) {
7936a1
 		client_entry = client;
7936a1
 	}
7936a1
@@ -454,7 +471,7 @@ krb5_error_code kdb_samba_db_sign_auth_data(krb5_context context,
7936a1
 
7936a1
 			code = ks_verify_pac(context,
7936a1
 					     flags,
7936a1
-					     ks_client_princ,
7936a1
+					     pac_client,
7936a1
 					     client_entry,
7936a1
 					     server,
7936a1
 					     krbtgt,
7936a1
@@ -494,7 +511,7 @@ krb5_error_code kdb_samba_db_sign_auth_data(krb5_context context,
7936a1
 		  is_as_req ? "AS-REQ" : "TGS-REQ",
7936a1
 		  client_name);
7936a1
 	code = krb5_pac_sign(context, pac, authtime, ks_client_princ,
7936a1
-			server_key, krbtgt_key, &pac_data);
7936a1
+			     server_key, krbtgt_key, &pac_data);
7936a1
 	if (code != 0) {
7936a1
 		DBG_ERR("krb5_pac_sign failed: %d\n", code);
7936a1
 		goto done;
7936a1
@@ -520,12 +537,6 @@ krb5_error_code kdb_samba_db_sign_auth_data(krb5_context context,
7936a1
 					      KRB5_AUTHDATA_IF_RELEVANT,
7936a1
 					      authdata,
7936a1
 					      signed_auth_data);
7936a1
-	if (code != 0) {
7936a1
-		goto done;
7936a1
-	}
7936a1
-
7936a1
-	code = 0;
7936a1
-
7936a1
 done:
7936a1
 	if (client_entry != NULL && client_entry != client) {
7936a1
 		ks_free_principal(context, client_entry);
7936a1
@@ -551,32 +562,13 @@ krb5_error_code kdb_samba_db_check_allowed_to_delegate(krb5_context context,
7936a1
 	 * server; -> delegating service
7936a1
 	 * proxy; -> target principal
7936a1
 	 */
7936a1
-	krb5_db_entry *delegating_service = discard_const_p(krb5_db_entry, server);
7936a1
-
7936a1
-	char *target_name = NULL;
7936a1
-	bool is_enterprise;
7936a1
-	krb5_error_code code;
7936a1
 
7936a1
 	mit_ctx = ks_get_context(context);
7936a1
 	if (mit_ctx == NULL) {
7936a1
 		return KRB5_KDB_DBNOTINITED;
7936a1
 	}
7936a1
 
7936a1
-	code = krb5_unparse_name(context, proxy, &target_name);
7936a1
-	if (code) {
7936a1
-		goto done;
7936a1
-	}
7936a1
-
7936a1
-	is_enterprise = (proxy->type == KRB5_NT_ENTERPRISE_PRINCIPAL);
7936a1
-
7936a1
-	code = mit_samba_check_s4u2proxy(mit_ctx,
7936a1
-					 delegating_service,
7936a1
-					 target_name,
7936a1
-					 is_enterprise);
7936a1
-
7936a1
-done:
7936a1
-	free(target_name);
7936a1
-	return code;
7936a1
+	return mit_samba_check_s4u2proxy(mit_ctx, server, proxy);
7936a1
 }
7936a1
 
7936a1
 
7936a1
diff --git a/source4/kdc/mit_samba.c b/source4/kdc/mit_samba.c
7936a1
index 4239332f0d9..acc3cba6254 100644
7936a1
--- a/source4/kdc/mit_samba.c
7936a1
+++ b/source4/kdc/mit_samba.c
7936a1
@@ -501,7 +501,6 @@ int mit_samba_get_pac(struct mit_samba_context *smb_ctx,
7936a1
 krb5_error_code mit_samba_reget_pac(struct mit_samba_context *ctx,
7936a1
 				    krb5_context context,
7936a1
 				    int flags,
7936a1
-				    krb5_const_principal client_principal,
7936a1
 				    krb5_db_entry *client,
7936a1
 				    krb5_db_entry *server,
7936a1
 				    krb5_db_entry *krbtgt,
7936a1
@@ -665,7 +664,7 @@ krb5_error_code mit_samba_reget_pac(struct mit_samba_context *ctx,
7936a1
 								  context,
7936a1
 								  *pac,
7936a1
 								  server->princ,
7936a1
-								  discard_const(client_principal),
7936a1
+								  client->princ,
7936a1
 								  deleg_blob);
7936a1
 		if (!NT_STATUS_IS_OK(nt_status)) {
7936a1
 			DEBUG(0, ("Update delegation info failed: %s\n",
7936a1
@@ -987,41 +986,17 @@ int mit_samba_check_client_access(struct mit_samba_context *ctx,
7936a1
 }
7936a1
 
7936a1
 int mit_samba_check_s4u2proxy(struct mit_samba_context *ctx,
7936a1
-			      krb5_db_entry *kentry,
7936a1
-			      const char *target_name,
7936a1
-			      bool is_nt_enterprise_name)
7936a1
+			      const krb5_db_entry *server,
7936a1
+			      krb5_const_principal target_principal)
7936a1
 {
7936a1
-#if 1
7936a1
-	/*
7936a1
-	 * This is disabled because mit_samba_update_pac_data() does not handle
7936a1
-	 * S4U_DELEGATION_INFO
7936a1
-	 */
7936a1
-
7936a1
-	return KRB5KDC_ERR_BADOPTION;
7936a1
-#else
7936a1
-	krb5_principal target_principal;
7936a1
-	int flags = 0;
7936a1
-	int ret;
7936a1
-
7936a1
-	if (is_nt_enterprise_name) {
7936a1
-		flags = KRB5_PRINCIPAL_PARSE_ENTERPRISE;
7936a1
-	}
7936a1
-
7936a1
-	ret = krb5_parse_name_flags(ctx->context, target_name,
7936a1
-				    flags, &target_principal);
7936a1
-	if (ret) {
7936a1
-		return ret;
7936a1
-	}
7936a1
-
7936a1
-	ret = samba_kdc_check_s4u2proxy(ctx->context,
7936a1
-					ctx->db_ctx,
7936a1
-					skdc_entry,
7936a1
-					target_principal);
7936a1
-
7936a1
-	krb5_free_principal(ctx->context, target_principal);
7936a1
-
7936a1
-	return ret;
7936a1
-#endif
7936a1
+	struct samba_kdc_entry *server_skdc_entry =
7936a1
+		 talloc_get_type_abort(server->e_data,
7936a1
+				       struct samba_kdc_entry);
7936a1
+
7936a1
+	return samba_kdc_check_s4u2proxy(ctx->context,
7936a1
+					 ctx->db_ctx,
7936a1
+					 server_skdc_entry,
7936a1
+					 target_principal);
7936a1
 }
7936a1
 
7936a1
 static krb5_error_code mit_samba_change_pwd_error(krb5_context context,
7936a1
diff --git a/source4/kdc/mit_samba.h b/source4/kdc/mit_samba.h
7936a1
index 636c77ec97c..9cb00c9610e 100644
7936a1
--- a/source4/kdc/mit_samba.h
7936a1
+++ b/source4/kdc/mit_samba.h
7936a1
@@ -56,7 +56,6 @@ int mit_samba_get_pac(struct mit_samba_context *smb_ctx,
7936a1
 krb5_error_code mit_samba_reget_pac(struct mit_samba_context *ctx,
7936a1
 				    krb5_context context,
7936a1
 				    int flags,
7936a1
-				    krb5_const_principal client_principal,
7936a1
 				    krb5_db_entry *client,
7936a1
 				    krb5_db_entry *server,
7936a1
 				    krb5_db_entry *krbtgt,
7936a1
@@ -73,9 +72,8 @@ int mit_samba_check_client_access(struct mit_samba_context *ctx,
7936a1
 				  DATA_BLOB *e_data);
7936a1
 
7936a1
 int mit_samba_check_s4u2proxy(struct mit_samba_context *ctx,
7936a1
-			      krb5_db_entry *kentry,
7936a1
-			      const char *target_name,
7936a1
-			      bool is_nt_enterprise_name);
7936a1
+			      const krb5_db_entry *server,
7936a1
+			      krb5_const_principal target_principal);
7936a1
 
7936a1
 int mit_samba_kpasswd_change_password(struct mit_samba_context *ctx,
7936a1
 				      char *pwd,
7936a1
-- 
7936a1
2.33.1
7936a1
7936a1
7936a1
From 992d38fa35c01f2f0bdb39d387fa29e8eb8d3d37 Mon Sep 17 00:00:00 2001
7936a1
From: Isaac Boukris <iboukris@gmail.com>
7936a1
Date: Fri, 27 Sep 2019 18:35:30 +0300
7936a1
Subject: [PATCH 2/3] krb5-mit: enable S4U client support for MIT build
7936a1
7936a1
Signed-off-by: Isaac Boukris <iboukris@gmail.com>
7936a1
Pair-Programmed-With: Andreas Schneider <asn@samba.org>
7936a1
---
7936a1
 lib/krb5_wrap/krb5_samba.c            | 185 ++++++++++++++++++++++++++
7936a1
 lib/krb5_wrap/krb5_samba.h            |   2 -
7936a1
 source4/auth/kerberos/kerberos_util.c |  11 --
7936a1
 3 files changed, 185 insertions(+), 13 deletions(-)
7936a1
7936a1
diff --git a/lib/krb5_wrap/krb5_samba.c b/lib/krb5_wrap/krb5_samba.c
7936a1
index fff5b4e2a22..791b417d5ba 100644
7936a1
--- a/lib/krb5_wrap/krb5_samba.c
7936a1
+++ b/lib/krb5_wrap/krb5_samba.c
7936a1
@@ -2694,6 +2694,191 @@ krb5_error_code smb_krb5_kinit_s4u2_ccache(krb5_context ctx,
7936a1
 
7936a1
 	return 0;
7936a1
 }
7936a1
+
7936a1
+#else /* MIT */
7936a1
+
7936a1
+static bool princ_compare_no_dollar(krb5_context ctx,
7936a1
+				    krb5_principal a,
7936a1
+				    krb5_principal b)
7936a1
+{
7936a1
+	bool cmp;
7936a1
+	krb5_principal mod = NULL;
7936a1
+
7936a1
+	if (a->length == 1 && b->length == 1 &&
7936a1
+	    a->data[0].length != 0 && b->data[0].length != 0 &&
7936a1
+	    a->data[0].data[a->data[0].length -1] !=
7936a1
+	    b->data[0].data[b->data[0].length -1]) {
7936a1
+		if (a->data[0].data[a->data[0].length -1] == '$') {
7936a1
+			mod = a;
7936a1
+			mod->data[0].length--;
7936a1
+		} else if (b->data[0].data[b->data[0].length -1] == '$') {
7936a1
+			mod = b;
7936a1
+			mod->data[0].length--;
7936a1
+		}
7936a1
+	}
7936a1
+
7936a1
+	cmp = krb5_principal_compare_flags(ctx, a, b,
7936a1
+					   KRB5_PRINCIPAL_COMPARE_CASEFOLD);
7936a1
+
7936a1
+	if (mod != NULL) {
7936a1
+		mod->data[0].length++;
7936a1
+	}
7936a1
+
7936a1
+	return cmp;
7936a1
+}
7936a1
+
7936a1
+krb5_error_code smb_krb5_kinit_s4u2_ccache(krb5_context ctx,
7936a1
+					   krb5_ccache store_cc,
7936a1
+					   krb5_principal init_principal,
7936a1
+					   const char *init_password,
7936a1
+					   krb5_principal impersonate_principal,
7936a1
+					   const char *self_service,
7936a1
+					   const char *target_service,
7936a1
+					   krb5_get_init_creds_opt *krb_options,
7936a1
+					   time_t *expire_time,
7936a1
+					   time_t *kdc_time)
7936a1
+{
7936a1
+	krb5_error_code code;
7936a1
+	krb5_principal self_princ = NULL;
7936a1
+	krb5_principal target_princ = NULL;
7936a1
+	krb5_creds *store_creds;
7936a1
+	krb5_creds *s4u2self_creds = NULL;
7936a1
+	krb5_creds *s4u2proxy_creds = NULL;
7936a1
+	krb5_creds init_creds = {0};
7936a1
+	krb5_creds mcreds = {0};
7936a1
+	krb5_flags options = KRB5_GC_NO_STORE;
7936a1
+	krb5_ccache tmp_cc;
7936a1
+	bool s4u2proxy;
7936a1
+
7936a1
+	code = krb5_cc_new_unique(ctx, "MEMORY", NULL, &tmp_cc);
7936a1
+	if (code != 0) {
7936a1
+		return code;
7936a1
+	}
7936a1
+
7936a1
+	code = krb5_get_init_creds_password(ctx, &init_creds,
7936a1
+					    init_principal,
7936a1
+					    init_password,
7936a1
+					    NULL, NULL,
7936a1
+					    0,
7936a1
+					    NULL,
7936a1
+					    krb_options);
7936a1
+	if (code != 0) {
7936a1
+		goto done;
7936a1
+	}
7936a1
+
7936a1
+	code = krb5_cc_initialize(ctx, tmp_cc, init_creds.client);
7936a1
+	if (code != 0) {
7936a1
+		goto done;
7936a1
+	}
7936a1
+
7936a1
+	code = krb5_cc_store_cred(ctx, tmp_cc, &init_creds);
7936a1
+	if (code != 0) {
7936a1
+		goto done;
7936a1
+	}
7936a1
+
7936a1
+	/*
7936a1
+	 * Check if we also need S4U2Proxy or if S4U2Self is
7936a1
+	 * enough in order to get a ticket for the target.
7936a1
+	 */
7936a1
+	if (target_service == NULL) {
7936a1
+		s4u2proxy = false;
7936a1
+	} else if (strcmp(target_service, self_service) == 0) {
7936a1
+		s4u2proxy = false;
7936a1
+	} else {
7936a1
+		s4u2proxy = true;
7936a1
+	}
7936a1
+
7936a1
+	code = krb5_parse_name(ctx, self_service, &self_princ);
7936a1
+	if (code != 0) {
7936a1
+		goto done;
7936a1
+	}
7936a1
+
7936a1
+	/* MIT lacks aliases support in S4U, for S4U2Self we require the tgt
7936a1
+	 * client and the request server to be the same principal name. */
7936a1
+	if (!princ_compare_no_dollar(ctx, init_creds.client, self_princ)) {
7936a1
+		code = KRB5KDC_ERR_PADATA_TYPE_NOSUPP;
7936a1
+		goto done;
7936a1
+	}
7936a1
+
7936a1
+	mcreds.client = impersonate_principal;
7936a1
+	mcreds.server = init_creds.client;
7936a1
+
7936a1
+	code = krb5_get_credentials_for_user(ctx, options, tmp_cc, &mcreds,
7936a1
+					     NULL, &s4u2self_creds);
7936a1
+	if (code != 0) {
7936a1
+		goto done;
7936a1
+	}
7936a1
+
7936a1
+	if (s4u2proxy) {
7936a1
+		code = krb5_parse_name(ctx, target_service, &target_princ);
7936a1
+		if (code != 0) {
7936a1
+			goto done;
7936a1
+		}
7936a1
+
7936a1
+		mcreds.client = init_creds.client;
7936a1
+		mcreds.server = target_princ;
7936a1
+		mcreds.second_ticket = s4u2self_creds->ticket;
7936a1
+
7936a1
+		code = krb5_get_credentials(ctx, options |
7936a1
+					    KRB5_GC_CONSTRAINED_DELEGATION,
7936a1
+					    tmp_cc, &mcreds, &s4u2proxy_creds);
7936a1
+		if (code != 0) {
7936a1
+			goto done;
7936a1
+		}
7936a1
+
7936a1
+		/* Check KDC support of S4U2Proxy extension */
7936a1
+		if (!krb5_principal_compare(ctx, s4u2self_creds->client,
7936a1
+					    s4u2proxy_creds->client)) {
7936a1
+			code = KRB5KDC_ERR_PADATA_TYPE_NOSUPP;
7936a1
+			goto done;
7936a1
+		}
7936a1
+
7936a1
+		store_creds = s4u2proxy_creds;
7936a1
+	} else {
7936a1
+		store_creds = s4u2self_creds;;
7936a1
+
7936a1
+		/* We need to save the ticket with the requested server name
7936a1
+		 * or the caller won't be able to find it in cache. */
7936a1
+		if (!krb5_principal_compare(ctx, self_princ,
7936a1
+			store_creds->server)) {
7936a1
+			krb5_free_principal(ctx, store_creds->server);
7936a1
+			store_creds->server = NULL;
7936a1
+			code = krb5_copy_principal(ctx, self_princ,
7936a1
+						   &store_creds->server);
7936a1
+			if (code != 0) {
7936a1
+				goto done;
7936a1
+			}
7936a1
+		}
7936a1
+	}
7936a1
+
7936a1
+	code = krb5_cc_initialize(ctx, store_cc, store_creds->client);
7936a1
+	if (code != 0) {
7936a1
+		goto done;
7936a1
+	}
7936a1
+
7936a1
+	code = krb5_cc_store_cred(ctx, store_cc, store_creds);
7936a1
+	if (code != 0) {
7936a1
+		goto done;
7936a1
+	}
7936a1
+
7936a1
+	if (expire_time) {
7936a1
+		*expire_time = (time_t) store_creds->times.endtime;
7936a1
+	}
7936a1
+
7936a1
+	if (kdc_time) {
7936a1
+		*kdc_time = (time_t) store_creds->times.starttime;
7936a1
+	}
7936a1
+
7936a1
+done:
7936a1
+	krb5_cc_destroy(ctx, tmp_cc);
7936a1
+	krb5_free_cred_contents(ctx, &init_creds);
7936a1
+	krb5_free_creds(ctx, s4u2self_creds);
7936a1
+	krb5_free_creds(ctx, s4u2proxy_creds);
7936a1
+	krb5_free_principal(ctx, self_princ);
7936a1
+	krb5_free_principal(ctx, target_princ);
7936a1
+
7936a1
+	return code;
7936a1
+}
7936a1
 #endif
7936a1
 
7936a1
 #if !defined(HAVE_KRB5_MAKE_PRINCIPAL) && defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
7936a1
diff --git a/lib/krb5_wrap/krb5_samba.h b/lib/krb5_wrap/krb5_samba.h
7936a1
index eab67f6d969..b5385c69a33 100644
7936a1
--- a/lib/krb5_wrap/krb5_samba.h
7936a1
+++ b/lib/krb5_wrap/krb5_samba.h
7936a1
@@ -252,7 +252,6 @@ krb5_error_code smb_krb5_kinit_password_ccache(krb5_context ctx,
7936a1
 					       krb5_get_init_creds_opt *krb_options,
7936a1
 					       time_t *expire_time,
7936a1
 					       time_t *kdc_time);
7936a1
-#ifdef SAMBA4_USES_HEIMDAL
7936a1
 krb5_error_code smb_krb5_kinit_s4u2_ccache(krb5_context ctx,
7936a1
 					   krb5_ccache store_cc,
7936a1
 					   krb5_principal init_principal,
7936a1
@@ -263,7 +262,6 @@ krb5_error_code smb_krb5_kinit_s4u2_ccache(krb5_context ctx,
7936a1
 					   krb5_get_init_creds_opt *krb_options,
7936a1
 					   time_t *expire_time,
7936a1
 					   time_t *kdc_time);
7936a1
-#endif
7936a1
 
7936a1
 #if defined(HAVE_KRB5_MAKE_PRINCIPAL)
7936a1
 #define smb_krb5_make_principal krb5_make_principal
7936a1
diff --git a/source4/auth/kerberos/kerberos_util.c b/source4/auth/kerberos/kerberos_util.c
7936a1
index 544d9d853cc..c14d8c72d8c 100644
7936a1
--- a/source4/auth/kerberos/kerberos_util.c
7936a1
+++ b/source4/auth/kerberos/kerberos_util.c
7936a1
@@ -234,9 +234,7 @@ done:
7936a1
 {
7936a1
 	krb5_error_code ret;
7936a1
 	const char *password;
7936a1
-#ifdef SAMBA4_USES_HEIMDAL
7936a1
 	const char *self_service;
7936a1
-#endif
7936a1
 	const char *target_service;
7936a1
 	time_t kdc_time = 0;
7936a1
 	krb5_principal princ;
7936a1
@@ -268,9 +266,7 @@ done:
7936a1
 		return ret;
7936a1
 	}
7936a1
 
7936a1
-#ifdef SAMBA4_USES_HEIMDAL
7936a1
 	self_service = cli_credentials_get_self_service(credentials);
7936a1
-#endif
7936a1
 	target_service = cli_credentials_get_target_service(credentials);
7936a1
 
7936a1
 	password = cli_credentials_get_password(credentials);
7936a1
@@ -331,7 +327,6 @@ done:
7936a1
 #endif
7936a1
 		if (password) {
7936a1
 			if (impersonate_principal) {
7936a1
-#ifdef SAMBA4_USES_HEIMDAL
7936a1
 				ret = smb_krb5_kinit_s4u2_ccache(smb_krb5_context->krb5_context,
7936a1
 								 ccache,
7936a1
 								 princ,
7936a1
@@ -342,12 +337,6 @@ done:
7936a1
 								 krb_options,
7936a1
 								 NULL,
7936a1
 								 &kdc_time);
7936a1
-#else
7936a1
-				talloc_free(mem_ctx);
7936a1
-				(*error_string) = "INTERNAL error: s4u2 ops "
7936a1
-					"are not supported with MIT build yet";
7936a1
-				return EINVAL;
7936a1
-#endif
7936a1
 			} else {
7936a1
 				ret = smb_krb5_kinit_password_ccache(smb_krb5_context->krb5_context,
7936a1
 								     ccache,
7936a1
-- 
7936a1
2.33.1
7936a1
7936a1
7936a1
From f1951b501ca0fb3e613f04437c99dc1bbf204609 Mon Sep 17 00:00:00 2001
7936a1
From: Isaac Boukris <iboukris@gmail.com>
7936a1
Date: Sat, 19 Sep 2020 14:16:20 +0200
7936a1
Subject: [PATCH 3/3] wip: for canonicalization with new MIT kdc code
7936a1
7936a1
---
7936a1
 source4/heimdal/lib/hdb/hdb.h | 1 +
7936a1
 source4/kdc/db-glue.c         | 8 ++++++--
7936a1
 source4/kdc/mit_samba.c       | 3 +++
7936a1
 source4/kdc/sdb.h             | 1 +
7936a1
 4 files changed, 11 insertions(+), 2 deletions(-)
7936a1
7936a1
diff --git a/source4/heimdal/lib/hdb/hdb.h b/source4/heimdal/lib/hdb/hdb.h
7936a1
index 5ef9d9565f3..dafaffc6c2d 100644
7936a1
--- a/source4/heimdal/lib/hdb/hdb.h
7936a1
+++ b/source4/heimdal/lib/hdb/hdb.h
7936a1
@@ -63,6 +63,7 @@ enum hdb_lockop{ HDB_RLOCK, HDB_WLOCK };
7936a1
 #define HDB_F_ALL_KVNOS		2048	/* we want all the keys, live or not */
7936a1
 #define HDB_F_FOR_AS_REQ	4096	/* fetch is for a AS REQ */
7936a1
 #define HDB_F_FOR_TGS_REQ	8192	/* fetch is for a TGS REQ */
7936a1
+#define HDB_F_FORCE_CANON	16384	/* force canonicalition */
7936a1
 
7936a1
 /* hdb_capability_flags */
7936a1
 #define HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL 1
7936a1
diff --git a/source4/kdc/db-glue.c b/source4/kdc/db-glue.c
7936a1
index aff74f2ee71..d16b4c3329a 100644
7936a1
--- a/source4/kdc/db-glue.c
7936a1
+++ b/source4/kdc/db-glue.c
7936a1
@@ -916,17 +916,21 @@ static krb5_error_code samba_kdc_message2entry(krb5_context context,
7936a1
 			}
7936a1
 		}
7936a1
 
7936a1
-	} else if (ent_type == SAMBA_KDC_ENT_TYPE_ANY && principal == NULL) {
7936a1
+	} else if (ent_type == SAMBA_KDC_ENT_TYPE_ANY && principal == NULL) { // was this supposed to be || ?
7936a1
 		ret = smb_krb5_make_principal(context, &entry_ex->entry.principal, lpcfg_realm(lp_ctx), samAccountName, NULL);
7936a1
 		if (ret) {
7936a1
 			krb5_clear_error_message(context);
7936a1
 			goto out;
7936a1
 		}
7936a1
-	} else if ((flags & SDB_F_CANON) && (flags & SDB_F_FOR_AS_REQ)) {
7936a1
+	} else if (((flags & SDB_F_CANON) && (flags & SDB_F_FOR_AS_REQ)) || (flags & SDB_F_FORCE_CANON)){
7936a1
 		/*
7936a1
 		 * SDB_F_CANON maps from the canonicalize flag in the
7936a1
 		 * packet, and has a different meaning between AS-REQ
7936a1
 		 * and TGS-REQ.  We only change the principal in the AS-REQ case
7936a1
+		 *
7936a1
+		 * The SDB_F_FORCE_CANON if for the new MIT kdc code that wants
7936a1
+		 * the canonical name in all lookups, and takes care to canonicalize
7936a1
+		 * only when appropriate.
7936a1
 		 */
7936a1
 		ret = smb_krb5_make_principal(context, &entry_ex->entry.principal, lpcfg_realm(lp_ctx), samAccountName, NULL);
7936a1
 		if (ret) {
7936a1
diff --git a/source4/kdc/mit_samba.c b/source4/kdc/mit_samba.c
7936a1
index acc3cba6254..f0b9df8b613 100644
7936a1
--- a/source4/kdc/mit_samba.c
7936a1
+++ b/source4/kdc/mit_samba.c
7936a1
@@ -224,6 +224,9 @@ int mit_samba_get_principal(struct mit_samba_context *ctx,
7936a1
 	if (kflags & KRB5_KDB_FLAG_CANONICALIZE) {
7936a1
 		sflags |= SDB_F_CANON;
7936a1
 	}
7936a1
+#if KRB5_KDB_API_VERSION >= 10
7936a1
+	sflags |= SDB_F_FORCE_CANON;
7936a1
+#endif
7936a1
 	if (kflags & (KRB5_KDB_FLAG_CLIENT_REFERRALS_ONLY |
7936a1
 		      KRB5_KDB_FLAG_INCLUDE_PAC)) {
7936a1
 		/*
7936a1
diff --git a/source4/kdc/sdb.h b/source4/kdc/sdb.h
7936a1
index c929acccce6..a9115ec23d7 100644
7936a1
--- a/source4/kdc/sdb.h
7936a1
+++ b/source4/kdc/sdb.h
7936a1
@@ -116,6 +116,7 @@ struct sdb_entry_ex {
7936a1
 #define SDB_F_KVNO_SPECIFIED	128	/* we want a particular KVNO */
7936a1
 #define SDB_F_FOR_AS_REQ	4096	/* fetch is for a AS REQ */
7936a1
 #define SDB_F_FOR_TGS_REQ	8192	/* fetch is for a TGS REQ */
7936a1
+#define SDB_F_FORCE_CANON	16384	/* force canonicalition */
7936a1
 
7936a1
 void sdb_free_entry(struct sdb_entry_ex *e);
7936a1
 void free_sdb_entry(struct sdb_entry *s);
7936a1
-- 
7936a1
2.33.1
7936a1