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