11c8e3
From a32bef9d1193e2bc253b7af8f4d0adb6476937f5 Mon Sep 17 00:00:00 2001
11c8e3
From: Samuel Cabrero <scabrero@suse.de>
11c8e3
Date: Tue, 22 Feb 2022 12:59:44 +0100
11c8e3
Subject: [PATCH 1/6] s3:libads: Fix memory leak in kerberos_return_pac() error
11c8e3
 path
11c8e3
11c8e3
Signed-off-by: Samuel Cabrero <scabrero@samba.org>
11c8e3
Reviewed-by: Stefan Metzmacher <metze@samba.org>
11c8e3
Reviewed-by: Andreas Schneider <asn@samba.org>
11c8e3
(cherry picked from commit 3dbcd20de98cd28683a9c248368e5082b6388111)
11c8e3
---
11c8e3
 source3/libads/authdata.c | 11 ++++++++---
11c8e3
 1 file changed, 8 insertions(+), 3 deletions(-)
11c8e3
11c8e3
diff --git a/source3/libads/authdata.c b/source3/libads/authdata.c
11c8e3
index dd21d895fc2..c048510d480 100644
11c8e3
--- a/source3/libads/authdata.c
11c8e3
+++ b/source3/libads/authdata.c
11c8e3
@@ -61,7 +61,10 @@ NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
11c8e3
 {
11c8e3
 	krb5_error_code ret;
11c8e3
 	NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
11c8e3
-	DATA_BLOB tkt, tkt_wrapped, ap_rep, sesskey1;
11c8e3
+	DATA_BLOB tkt = data_blob_null;
11c8e3
+	DATA_BLOB tkt_wrapped = data_blob_null;
11c8e3
+	DATA_BLOB ap_rep = data_blob_null;
11c8e3
+	DATA_BLOB sesskey1 = data_blob_null;
11c8e3
 	const char *auth_princ = NULL;
11c8e3
 	const char *cc = "MEMORY:kerberos_return_pac";
11c8e3
 	struct auth_session_info *session_info;
11c8e3
@@ -81,7 +84,8 @@ NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
11c8e3
 	ZERO_STRUCT(sesskey1);
11c8e3
 
11c8e3
 	if (!name || !pass) {
11c8e3
-		return NT_STATUS_INVALID_PARAMETER;
11c8e3
+		status = NT_STATUS_INVALID_PARAMETER;
11c8e3
+		goto out;
11c8e3
 	}
11c8e3
 
11c8e3
 	if (cache_name) {
11c8e3
@@ -131,7 +135,8 @@ NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
11c8e3
 
11c8e3
 	if (expire_time && renew_till_time &&
11c8e3
 	    (*expire_time == 0) && (*renew_till_time == 0)) {
11c8e3
-		return NT_STATUS_INVALID_LOGON_TYPE;
11c8e3
+		status = NT_STATUS_INVALID_LOGON_TYPE;
11c8e3
+		goto out;
11c8e3
 	}
11c8e3
 
11c8e3
 	ret = ads_krb5_cli_get_ticket(mem_ctx,
11c8e3
-- 
11c8e3
2.35.1
11c8e3
11c8e3
11c8e3
From d5a800beb60ee0b9310fa073c2e06a7dcbe65d5e Mon Sep 17 00:00:00 2001
11c8e3
From: Samuel Cabrero <scabrero@suse.de>
11c8e3
Date: Tue, 22 Feb 2022 13:00:05 +0100
11c8e3
Subject: [PATCH 2/6] lib:krb5_wrap: Improve debug message and use newer debug
11c8e3
 macro
11c8e3
11c8e3
Signed-off-by: Samuel Cabrero <scabrero@samba.org>
11c8e3
Reviewed-by: Stefan Metzmacher <metze@samba.org>
11c8e3
Reviewed-by: Andreas Schneider <asn@samba.org>
11c8e3
(cherry picked from commit ed14513be055cc56eb39785323df2c538a813865)
11c8e3
---
11c8e3
 lib/krb5_wrap/krb5_samba.c | 2 +-
11c8e3
 1 file changed, 1 insertion(+), 1 deletion(-)
11c8e3
11c8e3
diff --git a/lib/krb5_wrap/krb5_samba.c b/lib/krb5_wrap/krb5_samba.c
11c8e3
index fff5b4e2a22..42d4b950f80 100644
11c8e3
--- a/lib/krb5_wrap/krb5_samba.c
11c8e3
+++ b/lib/krb5_wrap/krb5_samba.c
11c8e3
@@ -1079,7 +1079,7 @@ krb5_error_code smb_krb5_renew_ticket(const char *ccache_string,
11c8e3
 		goto done;
11c8e3
 	}
11c8e3
 
11c8e3
-	DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
11c8e3
+	DBG_DEBUG("Using %s as ccache for '%s'\n", ccache_string, client_string);
11c8e3
 
11c8e3
 	/* FIXME: we should not fall back to defaults */
11c8e3
 	ret = krb5_cc_resolve(context, discard_const_p(char, ccache_string), &ccache);
11c8e3
-- 
11c8e3
2.35.1
11c8e3
11c8e3
11c8e3
From 79d08465f66df67b69fdafed8eec48290acf24b9 Mon Sep 17 00:00:00 2001
11c8e3
From: Samuel Cabrero <scabrero@suse.de>
11c8e3
Date: Tue, 22 Feb 2022 14:28:28 +0100
11c8e3
Subject: [PATCH 3/6] lib:krb5_wrap: Fix wrong debug message and use newer
11c8e3
 debug macro
11c8e3
11c8e3
Signed-off-by: Samuel Cabrero <scabrero@samba.org>
11c8e3
Reviewed-by: Stefan Metzmacher <metze@samba.org>
11c8e3
Reviewed-by: Andreas Schneider <asn@samba.org>
11c8e3
(cherry picked from commit 1b5b4107a5081f15ba215f3025056d509fcfcf2a)
11c8e3
---
11c8e3
 lib/krb5_wrap/krb5_samba.c | 5 ++++-
11c8e3
 1 file changed, 4 insertions(+), 1 deletion(-)
11c8e3
11c8e3
diff --git a/lib/krb5_wrap/krb5_samba.c b/lib/krb5_wrap/krb5_samba.c
11c8e3
index 42d4b950f80..76c2dcd2126 100644
11c8e3
--- a/lib/krb5_wrap/krb5_samba.c
11c8e3
+++ b/lib/krb5_wrap/krb5_samba.c
11c8e3
@@ -1101,7 +1101,10 @@ krb5_error_code smb_krb5_renew_ticket(const char *ccache_string,
11c8e3
 
11c8e3
 	ret = krb5_get_renewed_creds(context, &creds, client, ccache, discard_const_p(char, service_string));
11c8e3
 	if (ret) {
11c8e3
-		DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
11c8e3
+		DBG_DEBUG("krb5_get_renewed_creds using ccache '%s' "
11c8e3
+			  "for client '%s' and service '%s' failed: %s\n",
11c8e3
+			  ccache_string, client_string, service_string,
11c8e3
+			  error_message(ret));
11c8e3
 		goto done;
11c8e3
 	}
11c8e3
 
11c8e3
-- 
11c8e3
2.35.1
11c8e3
11c8e3
11c8e3
From 00418e5b78fa4361c0386c13374154d310426f77 Mon Sep 17 00:00:00 2001
11c8e3
From: Samuel Cabrero <scabrero@suse.de>
11c8e3
Date: Tue, 22 Feb 2022 13:08:56 +0100
11c8e3
Subject: [PATCH 4/6] s3:libads: Return canonical principal and realm from
11c8e3
 kerberos_return_pac()
11c8e3
11c8e3
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14979
11c8e3
11c8e3
Signed-off-by: Samuel Cabrero <scabrero@samba.org>
11c8e3
Reviewed-by: Stefan Metzmacher <metze@samba.org>
11c8e3
Reviewed-by: Andreas Schneider <asn@samba.org>
11c8e3
(cherry picked from commit 00b1f44a7e8f66976757535bcbc6bea97fb1c29f)
11c8e3
---
11c8e3
 source3/libads/authdata.c       | 22 +++++++++++++++++++++-
11c8e3
 source3/libads/kerberos_proto.h |  2 ++
11c8e3
 source3/utils/net_ads.c         |  2 ++
11c8e3
 source3/winbindd/winbindd_pam.c |  2 ++
11c8e3
 4 files changed, 27 insertions(+), 1 deletion(-)
11c8e3
11c8e3
diff --git a/source3/libads/authdata.c b/source3/libads/authdata.c
11c8e3
index c048510d480..bf9a2335445 100644
11c8e3
--- a/source3/libads/authdata.c
11c8e3
+++ b/source3/libads/authdata.c
11c8e3
@@ -57,6 +57,8 @@ NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
11c8e3
 			     time_t renewable_time,
11c8e3
 			     const char *impersonate_princ_s,
11c8e3
 			     const char *local_service,
11c8e3
+			     char **_canon_principal,
11c8e3
+			     char **_canon_realm,
11c8e3
 			     struct PAC_DATA_CTR **_pac_data_ctr)
11c8e3
 {
11c8e3
 	krb5_error_code ret;
11c8e3
@@ -75,6 +77,8 @@ NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
11c8e3
 	struct auth4_context *auth_context;
11c8e3
 	struct loadparm_context *lp_ctx;
11c8e3
 	struct PAC_DATA_CTR *pac_data_ctr = NULL;
11c8e3
+	char *canon_principal = NULL;
11c8e3
+	char *canon_realm = NULL;
11c8e3
 
11c8e3
 	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
11c8e3
 	NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
11c8e3
@@ -88,6 +92,14 @@ NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
11c8e3
 		goto out;
11c8e3
 	}
11c8e3
 
11c8e3
+	if (_canon_principal != NULL) {
11c8e3
+		*_canon_principal = NULL;
11c8e3
+	}
11c8e3
+
11c8e3
+	if (_canon_realm != NULL) {
11c8e3
+		*_canon_realm = NULL;
11c8e3
+	}
11c8e3
+
11c8e3
 	if (cache_name) {
11c8e3
 		cc = cache_name;
11c8e3
 	}
11c8e3
@@ -109,7 +121,9 @@ NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
11c8e3
 					  request_pac,
11c8e3
 					  add_netbios_addr,
11c8e3
 					  renewable_time,
11c8e3
-					  NULL, NULL, NULL,
11c8e3
+					  tmp_ctx,
11c8e3
+					  &canon_principal,
11c8e3
+					  &canon_realm,
11c8e3
 					  &status);
11c8e3
 	if (ret) {
11c8e3
 		DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
11c8e3
@@ -243,6 +257,12 @@ NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
11c8e3
 	}
11c8e3
 
11c8e3
 	*_pac_data_ctr = talloc_move(mem_ctx, &pac_data_ctr);
11c8e3
+	if (_canon_principal != NULL) {
11c8e3
+		*_canon_principal = talloc_move(mem_ctx, &canon_principal);
11c8e3
+	}
11c8e3
+	if (_canon_realm != NULL) {
11c8e3
+		*_canon_realm = talloc_move(mem_ctx, &canon_realm);
11c8e3
+	}
11c8e3
 
11c8e3
 out:
11c8e3
 	talloc_free(tmp_ctx);
11c8e3
diff --git a/source3/libads/kerberos_proto.h b/source3/libads/kerberos_proto.h
11c8e3
index 3d7b5bc074b..807381248c8 100644
11c8e3
--- a/source3/libads/kerberos_proto.h
11c8e3
+++ b/source3/libads/kerberos_proto.h
11c8e3
@@ -78,6 +78,8 @@ NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
11c8e3
 			     time_t renewable_time,
11c8e3
 			     const char *impersonate_princ_s,
11c8e3
 			     const char *local_service,
11c8e3
+			     char **_canon_principal,
11c8e3
+			     char **_canon_realm,
11c8e3
 			     struct PAC_DATA_CTR **pac_data_ctr);
11c8e3
 
11c8e3
 /* The following definitions come from libads/krb5_setpw.c  */
11c8e3
diff --git a/source3/utils/net_ads.c b/source3/utils/net_ads.c
11c8e3
index 8f993f9ba4c..c41fb0afe9c 100644
11c8e3
--- a/source3/utils/net_ads.c
11c8e3
+++ b/source3/utils/net_ads.c
11c8e3
@@ -3273,6 +3273,8 @@ static int net_ads_kerberos_pac_common(struct net_context *c, int argc, const ch
11c8e3
 				     2592000, /* one month */
11c8e3
 				     impersonate_princ_s,
11c8e3
 				     local_service,
11c8e3
+				     NULL,
11c8e3
+				     NULL,
11c8e3
 				     pac_data_ctr);
11c8e3
 	if (!NT_STATUS_IS_OK(status)) {
11c8e3
 		d_printf(_("failed to query kerberos PAC: %s\n"),
11c8e3
diff --git a/source3/winbindd/winbindd_pam.c b/source3/winbindd/winbindd_pam.c
11c8e3
index 7606bfb4ecd..025a5cbc111 100644
11c8e3
--- a/source3/winbindd/winbindd_pam.c
11c8e3
+++ b/source3/winbindd/winbindd_pam.c
11c8e3
@@ -789,6 +789,8 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
11c8e3
 				     WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
11c8e3
 				     NULL,
11c8e3
 				     local_service,
11c8e3
+				     NULL,
11c8e3
+				     NULL,
11c8e3
 				     &pac_data_ctr);
11c8e3
 	if (user_ccache_file != NULL) {
11c8e3
 		gain_root_privilege();
11c8e3
-- 
11c8e3
2.35.1
11c8e3
11c8e3
11c8e3
From d754753ab8edf6dde241d91442fe6afba8993de5 Mon Sep 17 00:00:00 2001
11c8e3
From: Samuel Cabrero <scabrero@suse.de>
11c8e3
Date: Tue, 22 Feb 2022 13:19:02 +0100
11c8e3
Subject: [PATCH 5/6] s3:winbind: Store canonical principal and realm in ccache
11c8e3
 entry
11c8e3
11c8e3
They will be used later to refresh the tickets.
11c8e3
11c8e3
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14979
11c8e3
11c8e3
Signed-off-by: Samuel Cabrero <scabrero@samba.org>
11c8e3
Reviewed-by: Stefan Metzmacher <metze@samba.org>
11c8e3
Reviewed-by: Andreas Schneider <asn@samba.org>
11c8e3
(cherry picked from commit 0f4f330773d272b4d28ff3ba5a41bdd4ba569c8b)
11c8e3
---
11c8e3
 source3/winbindd/winbindd.h            |  2 ++
11c8e3
 source3/winbindd/winbindd_cred_cache.c | 16 +++++++++++++++-
11c8e3
 source3/winbindd/winbindd_pam.c        | 14 ++++++++++----
11c8e3
 source3/winbindd/winbindd_proto.h      |  4 +++-
11c8e3
 4 files changed, 30 insertions(+), 6 deletions(-)
11c8e3
11c8e3
diff --git a/source3/winbindd/winbindd.h b/source3/winbindd/winbindd.h
11c8e3
index a6b2238cec1..dac4a1fa927 100644
11c8e3
--- a/source3/winbindd/winbindd.h
11c8e3
+++ b/source3/winbindd/winbindd.h
11c8e3
@@ -344,6 +344,8 @@ struct WINBINDD_CCACHE_ENTRY {
11c8e3
 	const char *service;
11c8e3
 	const char *username;
11c8e3
 	const char *realm;
11c8e3
+	const char *canon_principal;
11c8e3
+	const char *canon_realm;
11c8e3
 	struct WINBINDD_MEMORY_CREDS *cred_ptr;
11c8e3
 	int ref_count;
11c8e3
 	uid_t uid;
11c8e3
diff --git a/source3/winbindd/winbindd_cred_cache.c b/source3/winbindd/winbindd_cred_cache.c
11c8e3
index c3077e21989..88847b1ab97 100644
11c8e3
--- a/source3/winbindd/winbindd_cred_cache.c
11c8e3
+++ b/source3/winbindd/winbindd_cred_cache.c
11c8e3
@@ -501,7 +501,9 @@ NTSTATUS add_ccache_to_list(const char *princ_name,
11c8e3
 			    time_t create_time,
11c8e3
 			    time_t ticket_end,
11c8e3
 			    time_t renew_until,
11c8e3
-			    bool postponed_request)
11c8e3
+			    bool postponed_request,
11c8e3
+			    const char *canon_principal,
11c8e3
+			    const char *canon_realm)
11c8e3
 {
11c8e3
 	struct WINBINDD_CCACHE_ENTRY *entry = NULL;
11c8e3
 	struct timeval t;
11c8e3
@@ -617,6 +619,18 @@ NTSTATUS add_ccache_to_list(const char *princ_name,
11c8e3
 			goto no_mem;
11c8e3
 		}
11c8e3
 	}
11c8e3
+	if (canon_principal != NULL) {
11c8e3
+		entry->canon_principal = talloc_strdup(entry, canon_principal);
11c8e3
+		if (entry->canon_principal == NULL) {
11c8e3
+			goto no_mem;
11c8e3
+		}
11c8e3
+	}
11c8e3
+	if (canon_realm != NULL) {
11c8e3
+		entry->canon_realm = talloc_strdup(entry, canon_realm);
11c8e3
+		if (entry->canon_realm == NULL) {
11c8e3
+			goto no_mem;
11c8e3
+		}
11c8e3
+	}
11c8e3
 
11c8e3
 	entry->ccname = talloc_strdup(entry, ccname);
11c8e3
 	if (!entry->ccname) {
11c8e3
diff --git a/source3/winbindd/winbindd_pam.c b/source3/winbindd/winbindd_pam.c
11c8e3
index 025a5cbc111..a24cef78440 100644
11c8e3
--- a/source3/winbindd/winbindd_pam.c
11c8e3
+++ b/source3/winbindd/winbindd_pam.c
11c8e3
@@ -687,6 +687,8 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
11c8e3
 	const char *local_service;
11c8e3
 	uint32_t i;
11c8e3
 	struct netr_SamInfo6 *info6_copy = NULL;
11c8e3
+	char *canon_principal = NULL;
11c8e3
+	char *canon_realm = NULL;
11c8e3
 	bool ok;
11c8e3
 
11c8e3
 	*info6 = NULL;
11c8e3
@@ -789,8 +791,8 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
11c8e3
 				     WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
11c8e3
 				     NULL,
11c8e3
 				     local_service,
11c8e3
-				     NULL,
11c8e3
-				     NULL,
11c8e3
+				     &canon_principal,
11c8e3
+				     &canon_realm,
11c8e3
 				     &pac_data_ctr);
11c8e3
 	if (user_ccache_file != NULL) {
11c8e3
 		gain_root_privilege();
11c8e3
@@ -856,7 +858,9 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
11c8e3
 					    time(NULL),
11c8e3
 					    ticket_lifetime,
11c8e3
 					    renewal_until,
11c8e3
-					    false);
11c8e3
+					    false,
11c8e3
+					    canon_principal,
11c8e3
+					    canon_realm);
11c8e3
 
11c8e3
 		if (!NT_STATUS_IS_OK(result)) {
11c8e3
 			DEBUG(10,("winbindd_raw_kerberos_login: failed to add ccache to list: %s\n",
11c8e3
@@ -1233,7 +1237,9 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
11c8e3
 							    time(NULL),
11c8e3
 							    time(NULL) + lp_winbind_cache_time(),
11c8e3
 							    time(NULL) + WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
11c8e3
-							    true);
11c8e3
+							    true,
11c8e3
+							    principal_s,
11c8e3
+							    realm);
11c8e3
 
11c8e3
 				if (!NT_STATUS_IS_OK(result)) {
11c8e3
 					DEBUG(10,("winbindd_dual_pam_auth_cached: failed "
11c8e3
diff --git a/source3/winbindd/winbindd_proto.h b/source3/winbindd/winbindd_proto.h
11c8e3
index c0d653a6d77..16c23f3de40 100644
11c8e3
--- a/source3/winbindd/winbindd_proto.h
11c8e3
+++ b/source3/winbindd/winbindd_proto.h
11c8e3
@@ -236,7 +236,9 @@ NTSTATUS add_ccache_to_list(const char *princ_name,
11c8e3
 			    time_t create_time,
11c8e3
 			    time_t ticket_end,
11c8e3
 			    time_t renew_until,
11c8e3
-			    bool postponed_request);
11c8e3
+			    bool postponed_request,
11c8e3
+			    const char *canon_principal,
11c8e3
+			    const char *canon_realm);
11c8e3
 NTSTATUS remove_ccache(const char *username);
11c8e3
 struct WINBINDD_MEMORY_CREDS *find_memory_creds_by_name(const char *username);
11c8e3
 NTSTATUS winbindd_add_memory_creds(const char *username,
11c8e3
-- 
11c8e3
2.35.1
11c8e3
11c8e3
11c8e3
From 82452eb54758de50700776fb92b7e7af892fdaea Mon Sep 17 00:00:00 2001
11c8e3
From: Samuel Cabrero <scabrero@suse.de>
11c8e3
Date: Tue, 22 Feb 2022 14:28:44 +0100
11c8e3
Subject: [PATCH 6/6] s3:winbind: Use the canonical principal name to renew the
11c8e3
 credentials
11c8e3
11c8e3
The principal name stored in the winbindd ccache entry might be an
11c8e3
enterprise principal name if enterprise principals are enabled. Use
11c8e3
the canonical name to renew the credentials.
11c8e3
11c8e3
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14979
11c8e3
11c8e3
Signed-off-by: Samuel Cabrero <scabrero@samba.org>
11c8e3
Reviewed-by: Stefan Metzmacher <metze@samba.org>
11c8e3
Reviewed-by: Andreas Schneider <asn@samba.org>
11c8e3
(cherry picked from commit 8246ccc23d064147412bb3475e6431a9fffc0d27)
11c8e3
---
11c8e3
 source3/winbindd/winbindd_cred_cache.c | 2 +-
11c8e3
 1 file changed, 1 insertion(+), 1 deletion(-)
11c8e3
11c8e3
diff --git a/source3/winbindd/winbindd_cred_cache.c b/source3/winbindd/winbindd_cred_cache.c
11c8e3
index 88847b1ab97..6c65db6a73f 100644
11c8e3
--- a/source3/winbindd/winbindd_cred_cache.c
11c8e3
+++ b/source3/winbindd/winbindd_cred_cache.c
11c8e3
@@ -209,7 +209,7 @@ rekinit:
11c8e3
 	set_effective_uid(entry->uid);
11c8e3
 
11c8e3
 	ret = smb_krb5_renew_ticket(entry->ccname,
11c8e3
-				    entry->principal_name,
11c8e3
+				    entry->canon_principal,
11c8e3
 				    entry->service,
11c8e3
 				    &new_start);
11c8e3
 #if defined(DEBUG_KRB5_TKT_RENEWAL)
11c8e3
-- 
11c8e3
2.35.1
11c8e3