8b3f01
From 25465d0bc77dd712b3d94e488f2cf0583fd7ac04 Mon Sep 17 00:00:00 2001
8b3f01
From: Andreas Schneider <asn@cryptomilk.org>
8b3f01
Date: Tue, 26 Apr 2022 07:10:56 +0200
8b3f01
Subject: [PATCH 1/5] s3:passdb: Remove trailing spaces in lookup_sid.c
8b3f01
8b3f01
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15054
8b3f01
8b3f01
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
8b3f01
Reviewed-by: Jeremy Allison <jra@samba.org>
8b3f01
(cherry picked from commit 756cd0eed30322ae6dbd5402ec11441387475884)
8b3f01
---
8b3f01
 source3/passdb/lookup_sid.c | 18 +++++++++---------
8b3f01
 1 file changed, 9 insertions(+), 9 deletions(-)
8b3f01
8b3f01
diff --git a/source3/passdb/lookup_sid.c b/source3/passdb/lookup_sid.c
8b3f01
index a551bcfd24a..3a28cdc68a6 100644
8b3f01
--- a/source3/passdb/lookup_sid.c
8b3f01
+++ b/source3/passdb/lookup_sid.c
8b3f01
@@ -1,4 +1,4 @@
8b3f01
-/* 
8b3f01
+/*
8b3f01
    Unix SMB/CIFS implementation.
8b3f01
    uid/user handling
8b3f01
    Copyright (C) Andrew Tridgell         1992-1998
8b3f01
@@ -72,7 +72,7 @@ static bool lookup_unix_group_name(const char *name, struct dom_sid *sid)
8b3f01
  If an explicit domain name was given in the form domain\user, it
8b3f01
  has to try that. If no explicit domain name was given, we have
8b3f01
  to do guesswork.
8b3f01
-*****************************************************************/  
8b3f01
+*****************************************************************/
8b3f01
 
8b3f01
 bool lookup_name(TALLOC_CTX *mem_ctx,
8b3f01
 		 const char *full_name, int flags,
8b3f01
@@ -300,7 +300,7 @@ bool lookup_name(TALLOC_CTX *mem_ctx,
8b3f01
 		goto ok;
8b3f01
 	}
8b3f01
 
8b3f01
-	/* 6. Builtin aliases */	
8b3f01
+	/* 6. Builtin aliases */
8b3f01
 
8b3f01
 	if ((flags & LOOKUP_NAME_BUILTIN) &&
8b3f01
 	    lookup_builtin_name(name, &rid))
8b3f01
@@ -882,7 +882,7 @@ NTSTATUS lookup_sids(TALLOC_CTX *mem_ctx, int num_sids,
8b3f01
 	}
8b3f01
 
8b3f01
 	/* First build up the data structures:
8b3f01
-	 * 
8b3f01
+	 *
8b3f01
 	 * dom_infos is a list of domains referenced in the list of
8b3f01
 	 * SIDs. Later we will walk the list of domains and look up the RIDs
8b3f01
 	 * in bulk.
8b3f01
@@ -1070,7 +1070,7 @@ NTSTATUS lookup_sids(TALLOC_CTX *mem_ctx, int num_sids,
8b3f01
 
8b3f01
 /*****************************************************************
8b3f01
  *THE CANONICAL* convert SID to name function.
8b3f01
-*****************************************************************/  
8b3f01
+*****************************************************************/
8b3f01
 
8b3f01
 bool lookup_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
8b3f01
 		const char **ret_domain, const char **ret_name,
8b3f01
@@ -1104,7 +1104,7 @@ bool lookup_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
8b3f01
 		goto done;
8b3f01
 	}
8b3f01
 
8b3f01
-	if ((ret_name != NULL) && 
8b3f01
+	if ((ret_name != NULL) &&
8b3f01
 	    !(*ret_name = talloc_strdup(mem_ctx, name->name))) {
8b3f01
 		goto done;
8b3f01
 	}
8b3f01
@@ -1130,7 +1130,7 @@ bool lookup_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
8b3f01
 
8b3f01
 /*****************************************************************
8b3f01
  *THE LEGACY* convert SID to id function.
8b3f01
-*****************************************************************/  
8b3f01
+*****************************************************************/
8b3f01
 
8b3f01
 static bool legacy_sid_to_unixid(const struct dom_sid *psid, struct unixid *id)
8b3f01
 {
8b3f01
@@ -1465,7 +1465,7 @@ fail:
8b3f01
 
8b3f01
 /*****************************************************************
8b3f01
  *THE CANONICAL* convert SID to uid function.
8b3f01
-*****************************************************************/  
8b3f01
+*****************************************************************/
8b3f01
 
8b3f01
 bool sid_to_uid(const struct dom_sid *psid, uid_t *puid)
8b3f01
 {
8b3f01
@@ -1527,7 +1527,7 @@ bool sid_to_uid(const struct dom_sid *psid, uid_t *puid)
8b3f01
 /*****************************************************************
8b3f01
  *THE CANONICAL* convert SID to gid function.
8b3f01
  Group mapping is used for gids that maps to Wellknown SIDs
8b3f01
-*****************************************************************/  
8b3f01
+*****************************************************************/
8b3f01
 
8b3f01
 bool sid_to_gid(const struct dom_sid *psid, gid_t *pgid)
8b3f01
 {
8b3f01
-- 
8b3f01
2.36.0
8b3f01
8b3f01
8b3f01
From e884efce61290ad6f4125ab4e3adb08bcc1a800d Mon Sep 17 00:00:00 2001
8b3f01
From: Andreas Schneider <asn@cryptomilk.org>
8b3f01
Date: Tue, 26 Apr 2022 07:12:02 +0200
8b3f01
Subject: [PATCH 2/5] s3:passdb: Add support to handle UPNs in lookup_name()
8b3f01
8b3f01
This address an issue if sssd is running and handling nsswitch. If we look up
8b3f01
a user with getpwnam("DOMAIN\user") it will return user@REALM in the passwd
8b3f01
structure. We need to be able to deal with that.
8b3f01
8b3f01
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15054
8b3f01
8b3f01
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
8b3f01
Reviewed-by: Jeremy Allison <jra@samba.org>
8b3f01
(cherry picked from commit 2a03fb91c1120718ada9d4b8421044cb7eae7b83)
8b3f01
---
8b3f01
 source3/passdb/lookup_sid.c | 14 ++++++++++++--
8b3f01
 1 file changed, 12 insertions(+), 2 deletions(-)
8b3f01
8b3f01
diff --git a/source3/passdb/lookup_sid.c b/source3/passdb/lookup_sid.c
8b3f01
index 3a28cdc68a6..c14d7a7b123 100644
8b3f01
--- a/source3/passdb/lookup_sid.c
8b3f01
+++ b/source3/passdb/lookup_sid.c
8b3f01
@@ -100,8 +100,18 @@ bool lookup_name(TALLOC_CTX *mem_ctx,
8b3f01
 					PTR_DIFF(p, full_name));
8b3f01
 		name = talloc_strdup(tmp_ctx, p+1);
8b3f01
 	} else {
8b3f01
-		domain = talloc_strdup(tmp_ctx, "");
8b3f01
-		name = talloc_strdup(tmp_ctx, full_name);
8b3f01
+		char *q = strchr_m(full_name, '@');
8b3f01
+
8b3f01
+		/* Set the domain for UPNs */
8b3f01
+		if (q != NULL) {
8b3f01
+			name = talloc_strndup(tmp_ctx,
8b3f01
+					      full_name,
8b3f01
+					      PTR_DIFF(q, full_name));
8b3f01
+			domain = talloc_strdup(tmp_ctx, q + 1);
8b3f01
+		} else {
8b3f01
+			domain = talloc_strdup(tmp_ctx, "");
8b3f01
+			name = talloc_strdup(tmp_ctx, full_name);
8b3f01
+		}
8b3f01
 	}
8b3f01
 
8b3f01
 	if ((domain == NULL) || (name == NULL)) {
8b3f01
-- 
8b3f01
2.36.0
8b3f01
8b3f01
8b3f01
From cc548efd5fa1783e8412e7ac695c8d6be3323d67 Mon Sep 17 00:00:00 2001
8b3f01
From: Andreas Schneider <asn@cryptomilk.org>
8b3f01
Date: Tue, 26 Apr 2022 12:26:25 +0200
8b3f01
Subject: [PATCH 3/5] s3:passdb: Use already defined pointer in
8b3f01
 lookup_name_smbconf()
8b3f01
8b3f01
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
8b3f01
Reviewed-by: Jeremy Allison <jra@samba.org>
8b3f01
(cherry picked from commit ed8e466854d6d8d6120388716a7b604df7a4db27)
8b3f01
---
8b3f01
 source3/passdb/lookup_sid.c | 12 +++++-------
8b3f01
 1 file changed, 5 insertions(+), 7 deletions(-)
8b3f01
8b3f01
diff --git a/source3/passdb/lookup_sid.c b/source3/passdb/lookup_sid.c
8b3f01
index c14d7a7b123..dbea5578f92 100644
8b3f01
--- a/source3/passdb/lookup_sid.c
8b3f01
+++ b/source3/passdb/lookup_sid.c
8b3f01
@@ -464,7 +464,7 @@ bool lookup_name_smbconf(TALLOC_CTX *mem_ctx,
8b3f01
 		 const char **ret_domain, const char **ret_name,
8b3f01
 		 struct dom_sid *ret_sid, enum lsa_SidType *ret_type)
8b3f01
 {
8b3f01
-	char *qualified_name;
8b3f01
+	char *qualified_name = NULL;
8b3f01
 	const char *p;
8b3f01
 
8b3f01
 	if ((p = strchr_m(full_name, *lp_winbind_separator())) != NULL) {
8b3f01
@@ -472,16 +472,14 @@ bool lookup_name_smbconf(TALLOC_CTX *mem_ctx,
8b3f01
 		/* The name is already qualified with a domain. */
8b3f01
 
8b3f01
 		if (*lp_winbind_separator() != '\\') {
8b3f01
-			char *tmp;
8b3f01
-
8b3f01
 			/* lookup_name() needs '\\' as a separator */
8b3f01
 
8b3f01
-			tmp = talloc_strdup(mem_ctx, full_name);
8b3f01
-			if (!tmp) {
8b3f01
+			qualified_name = talloc_strdup(mem_ctx, full_name);
8b3f01
+			if (qualified_name == NULL) {
8b3f01
 				return false;
8b3f01
 			}
8b3f01
-			tmp[p - full_name] = '\\';
8b3f01
-			full_name = tmp;
8b3f01
+			qualified_name[p - full_name] = '\\';
8b3f01
+			full_name = qualified_name;
8b3f01
 		}
8b3f01
 
8b3f01
 		return lookup_name(mem_ctx, full_name, flags,
8b3f01
-- 
8b3f01
2.36.0
8b3f01
8b3f01
8b3f01
From 3ee3336f4a3fbb80ccabe6c1494a68286af55437 Mon Sep 17 00:00:00 2001
8b3f01
From: Andreas Schneider <asn@cryptomilk.org>
8b3f01
Date: Tue, 26 Apr 2022 07:24:10 +0200
8b3f01
Subject: [PATCH 4/5] s3:passdb: Refactor lookup_name_smbconf()
8b3f01
8b3f01
This will be changed to support UPNs too in the next patch.
8b3f01
8b3f01
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15054
8b3f01
8b3f01
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
8b3f01
Reviewed-by: Jeremy Allison <jra@samba.org>
8b3f01
(cherry picked from commit 2690310743920dfe20ac235c1e3617e0f421eddc)
8b3f01
---
8b3f01
 source3/passdb/lookup_sid.c | 7 ++++---
8b3f01
 1 file changed, 4 insertions(+), 3 deletions(-)
8b3f01
8b3f01
diff --git a/source3/passdb/lookup_sid.c b/source3/passdb/lookup_sid.c
8b3f01
index dbea5578f92..de9dd123239 100644
8b3f01
--- a/source3/passdb/lookup_sid.c
8b3f01
+++ b/source3/passdb/lookup_sid.c
8b3f01
@@ -465,13 +465,14 @@ bool lookup_name_smbconf(TALLOC_CTX *mem_ctx,
8b3f01
 		 struct dom_sid *ret_sid, enum lsa_SidType *ret_type)
8b3f01
 {
8b3f01
 	char *qualified_name = NULL;
8b3f01
-	const char *p;
8b3f01
+	const char *p = strchr_m(full_name, *lp_winbind_separator());
8b3f01
+	bool is_qualified = p != NULL;
8b3f01
 
8b3f01
-	if ((p = strchr_m(full_name, *lp_winbind_separator())) != NULL) {
8b3f01
+	if (is_qualified) {
8b3f01
 
8b3f01
 		/* The name is already qualified with a domain. */
8b3f01
 
8b3f01
-		if (*lp_winbind_separator() != '\\') {
8b3f01
+		if (p != NULL && *lp_winbind_separator() != '\\') {
8b3f01
 			/* lookup_name() needs '\\' as a separator */
8b3f01
 
8b3f01
 			qualified_name = talloc_strdup(mem_ctx, full_name);
8b3f01
-- 
8b3f01
2.36.0
8b3f01
8b3f01
8b3f01
From 1baa5b170c36854eaa0a5f2c9aba29d50194f750 Mon Sep 17 00:00:00 2001
8b3f01
From: Andreas Schneider <asn@cryptomilk.org>
8b3f01
Date: Tue, 26 Apr 2022 07:39:12 +0200
8b3f01
Subject: [PATCH 5/5] s3:passdb: Also allow to handle UPNs in
8b3f01
 lookup_name_smbconf()
8b3f01
8b3f01
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15054
8b3f01
8b3f01
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
8b3f01
Reviewed-by: Jeremy Allison <jra@samba.org>
8b3f01
(cherry picked from commit 28fc44f2852046d03cada161ed1001d04d9e1554)
8b3f01
---
8b3f01
 source3/passdb/lookup_sid.c | 3 ++-
8b3f01
 1 file changed, 2 insertions(+), 1 deletion(-)
8b3f01
8b3f01
diff --git a/source3/passdb/lookup_sid.c b/source3/passdb/lookup_sid.c
8b3f01
index de9dd123239..426ea3f81bd 100644
8b3f01
--- a/source3/passdb/lookup_sid.c
8b3f01
+++ b/source3/passdb/lookup_sid.c
8b3f01
@@ -466,8 +466,9 @@ bool lookup_name_smbconf(TALLOC_CTX *mem_ctx,
8b3f01
 {
8b3f01
 	char *qualified_name = NULL;
8b3f01
 	const char *p = strchr_m(full_name, *lp_winbind_separator());
8b3f01
-	bool is_qualified = p != NULL;
8b3f01
+	bool is_qualified = p != NULL || strchr_m(full_name, '@') != NULL;
8b3f01
 
8b3f01
+	/* For DOMAIN\user or user@REALM directly call lookup_name(). */
8b3f01
 	if (is_qualified) {
8b3f01
 
8b3f01
 		/* The name is already qualified with a domain. */
8b3f01
-- 
8b3f01
2.36.0
8b3f01