81d632
From 5604f16d805a73dd35a69c162966d081a1ebdb84 Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Thu, 15 Mar 2018 17:40:07 +0100
81d632
Subject: [PATCH 01/21] s3:torture: add SMB2-ANONYMOUS which asserts no GUEST
81d632
 bit for anonymous
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit 82d8aa3b9cb15512d29a97b5a7e55ea1a052734f)
81d632
(cherry picked from commit 23d1850c1c632984052ac923ab365501dd1c0195)
81d632
---
81d632
 source3/torture/proto.h     |  1 +
81d632
 source3/torture/test_smb2.c | 42 +++++++++++++++++++++++++++++++++++++
81d632
 source3/torture/torture.c   |  1 +
81d632
 3 files changed, 44 insertions(+)
81d632
81d632
diff --git a/source3/torture/proto.h b/source3/torture/proto.h
81d632
index 4c3e5401ce0..6f12ff7c2b9 100644
81d632
--- a/source3/torture/proto.h
81d632
+++ b/source3/torture/proto.h
81d632
@@ -95,6 +95,7 @@ bool run_nttrans_create(int dummy);
81d632
 bool run_nttrans_fsctl(int dummy);
81d632
 bool run_smb2_basic(int dummy);
81d632
 bool run_smb2_negprot(int dummy);
81d632
+bool run_smb2_anonymous(int dummy);
81d632
 bool run_smb2_session_reconnect(int dummy);
81d632
 bool run_smb2_tcon_dependence(int dummy);
81d632
 bool run_smb2_multi_channel(int dummy);
81d632
diff --git a/source3/torture/test_smb2.c b/source3/torture/test_smb2.c
81d632
index 297c3abca9f..897d034f6a9 100644
81d632
--- a/source3/torture/test_smb2.c
81d632
+++ b/source3/torture/test_smb2.c
81d632
@@ -24,6 +24,7 @@
81d632
 #include "../libcli/smb/smbXcli_base.h"
81d632
 #include "libcli/security/security.h"
81d632
 #include "libsmb/proto.h"
81d632
+#include "auth/credentials/credentials.h"
81d632
 #include "auth/gensec/gensec.h"
81d632
 #include "auth_generic.h"
81d632
 #include "../librpc/ndr/libndr.h"
81d632
@@ -274,6 +275,47 @@ bool run_smb2_negprot(int dummy)
81d632
 	return true;
81d632
 }
81d632
 
81d632
+bool run_smb2_anonymous(int dummy)
81d632
+{
81d632
+	struct cli_state *cli = NULL;
81d632
+	NTSTATUS status;
81d632
+	struct cli_credentials *anon_creds = NULL;
81d632
+	bool guest = false;
81d632
+
81d632
+	printf("Starting SMB2-ANONYMOUS\n");
81d632
+
81d632
+	if (!torture_init_connection(&cli)) {
81d632
+		return false;
81d632
+	}
81d632
+
81d632
+	status = smbXcli_negprot(cli->conn, cli->timeout,
81d632
+				 PROTOCOL_SMB2_02, PROTOCOL_LATEST);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		printf("smbXcli_negprot returned %s\n", nt_errstr(status));
81d632
+		return false;
81d632
+	}
81d632
+
81d632
+	anon_creds = cli_credentials_init_anon(talloc_tos());
81d632
+	if (anon_creds == NULL) {
81d632
+		printf("cli_credentials_init_anon failed\n");
81d632
+		return false;
81d632
+	}
81d632
+
81d632
+	status = cli_session_setup_creds(cli, anon_creds);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		printf("cli_session_setup returned %s\n", nt_errstr(status));
81d632
+		return false;
81d632
+	}
81d632
+
81d632
+	guest = smbXcli_session_is_guest(cli->smb2.session);
81d632
+	if (guest) {
81d632
+		printf("anonymous session should not have guest authentication\n");
81d632
+		return false;
81d632
+	}
81d632
+
81d632
+	return true;
81d632
+}
81d632
+
81d632
 bool run_smb2_session_reconnect(int dummy)
81d632
 {
81d632
 	struct cli_state *cli1;
81d632
diff --git a/source3/torture/torture.c b/source3/torture/torture.c
81d632
index 31e2bcc3497..e3834432ccb 100644
81d632
--- a/source3/torture/torture.c
81d632
+++ b/source3/torture/torture.c
81d632
@@ -11644,6 +11644,7 @@ static struct {
81d632
 	{ "NOTIFY-ONLINE", run_notify_online },
81d632
 	{ "SMB2-BASIC", run_smb2_basic },
81d632
 	{ "SMB2-NEGPROT", run_smb2_negprot },
81d632
+	{ "SMB2-ANONYMOUS", run_smb2_anonymous },
81d632
 	{ "SMB2-SESSION-RECONNECT", run_smb2_session_reconnect },
81d632
 	{ "SMB2-TCON-DEPENDENCE", run_smb2_tcon_dependence },
81d632
 	{ "SMB2-MULTI-CHANNEL", run_smb2_multi_channel },
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 6dfd59a8a8862b0954f8bd87b3816062f00fea0f Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Thu, 15 Mar 2018 18:04:21 +0100
81d632
Subject: [PATCH 02/21] s3:selftest: run SMB2-ANONYMOUS
81d632
81d632
This fails against a non AD DC smbd.
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit bf707a1eba39e996bb19457b63ddb658cc4183c2)
81d632
(cherry picked from commit e39a5bd12e1704926c9d8141d8ef75a093670892)
81d632
---
81d632
 selftest/knownfail.d/anonymous-guest | 1 +
81d632
 source3/selftest/tests.py            | 1 +
81d632
 2 files changed, 2 insertions(+)
81d632
 create mode 100644 selftest/knownfail.d/anonymous-guest
81d632
81d632
diff --git a/selftest/knownfail.d/anonymous-guest b/selftest/knownfail.d/anonymous-guest
81d632
new file mode 100644
81d632
index 00000000000..a134cece3d5
81d632
--- /dev/null
81d632
+++ b/selftest/knownfail.d/anonymous-guest
81d632
@@ -0,0 +1 @@
81d632
+^samba3.smbtorture_s3.*nt4_dc.*.SMB2-ANONYMOUS.smbtorture
81d632
diff --git a/source3/selftest/tests.py b/source3/selftest/tests.py
81d632
index 56b94c436ce..c0522b3ed6f 100755
81d632
--- a/source3/selftest/tests.py
81d632
+++ b/source3/selftest/tests.py
81d632
@@ -75,6 +75,7 @@ tests = ["FDPASS", "LOCK1", "LOCK2", "LOCK3", "LOCK4", "LOCK5", "LOCK6", "LOCK7"
81d632
         "GETADDRINFO", "UID-REGRESSION-TEST", "SHORTNAME-TEST",
81d632
         "CASE-INSENSITIVE-CREATE", "SMB2-BASIC", "NTTRANS-FSCTL", "SMB2-NEGPROT",
81d632
         "SMB2-SESSION-REAUTH", "SMB2-SESSION-RECONNECT", "SMB2-FTRUNCATE",
81d632
+        "SMB2-ANONYMOUS",
81d632
         "CLEANUP1",
81d632
         "CLEANUP2",
81d632
         "CLEANUP4",
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 40b619182e63df1cbc8e47c79a0ac0f83debce69 Mon Sep 17 00:00:00 2001
81d632
From: Ralph Boehme <slow@samba.org>
81d632
Date: Wed, 14 Mar 2018 11:44:49 +0100
81d632
Subject: [PATCH 03/21] libcli/security: only announce a session as GUEST if
81d632
 'Builtin\Guests' is there without 'Authenticated User'
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit f564847c8e9d31fe07dd3cbf435986b36f097fa3)
81d632
(cherry picked from commit ff7a8e416b53e073a6d16fb122cdeba8b53c6e53)
81d632
---
81d632
 libcli/security/session.c | 18 +++++++++++-------
81d632
 1 file changed, 11 insertions(+), 7 deletions(-)
81d632
81d632
diff --git a/libcli/security/session.c b/libcli/security/session.c
81d632
index 0fbb87d584e..f17e884c847 100644
81d632
--- a/libcli/security/session.c
81d632
+++ b/libcli/security/session.c
81d632
@@ -26,6 +26,9 @@
81d632
 enum security_user_level security_session_user_level(struct auth_session_info *session_info,
81d632
 						     const struct dom_sid *domain_sid)
81d632
 {
81d632
+	bool authenticated = false;
81d632
+	bool guest = false;
81d632
+
81d632
 	if (!session_info) {
81d632
 		return SECURITY_ANONYMOUS;
81d632
 	}
81d632
@@ -38,8 +41,13 @@ enum security_user_level security_session_user_level(struct auth_session_info *s
81d632
 		return SECURITY_ANONYMOUS;
81d632
 	}
81d632
 
81d632
-	if (security_token_has_builtin_guests(session_info->security_token)) {
81d632
-		return SECURITY_GUEST;
81d632
+	authenticated = security_token_has_nt_authenticated_users(session_info->security_token);
81d632
+	guest = security_token_has_builtin_guests(session_info->security_token);
81d632
+	if (!authenticated) {
81d632
+		if (guest) {
81d632
+			return SECURITY_GUEST;
81d632
+		}
81d632
+		return SECURITY_ANONYMOUS;
81d632
 	}
81d632
 
81d632
 	if (security_token_has_builtin_administrators(session_info->security_token)) {
81d632
@@ -60,9 +68,5 @@ enum security_user_level security_session_user_level(struct auth_session_info *s
81d632
 		return SECURITY_DOMAIN_CONTROLLER;
81d632
 	}
81d632
 
81d632
-	if (security_token_has_nt_authenticated_users(session_info->security_token)) {
81d632
-		return SECURITY_USER;
81d632
-	}
81d632
-
81d632
-	return SECURITY_ANONYMOUS;
81d632
+	return SECURITY_USER;
81d632
 }
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From b2e7990934503c86c17751a8c4f7d5f40b32aed7 Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Thu, 1 Mar 2018 18:05:28 +0100
81d632
Subject: [PATCH 04/21] s3:auth: remove unused auth_serversupplied_info->system
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit 28ad1306b880a44824ee956a19656ac29581a1b9)
81d632
(cherry picked from commit b991dca37a425cc252752e5a306df80077814aaf)
81d632
---
81d632
 source3/auth/auth_util.c | 1 -
81d632
 source3/include/auth.h   | 1 -
81d632
 2 files changed, 2 deletions(-)
81d632
81d632
diff --git a/source3/auth/auth_util.c b/source3/auth/auth_util.c
81d632
index 1021f2a6fef..4ae9dad2dd6 100644
81d632
--- a/source3/auth/auth_util.c
81d632
+++ b/source3/auth/auth_util.c
81d632
@@ -1045,7 +1045,6 @@ static struct auth_serversupplied_info *copy_session_info_serverinfo_guest(TALLO
81d632
 	SMB_ASSERT(src->unix_info);
81d632
 
81d632
 	dst->guest = true;
81d632
-	dst->system = false;
81d632
 
81d632
 	/* This element must be provided to convert back to an
81d632
 	 * auth_serversupplied_info.  This needs to be from the
81d632
diff --git a/source3/include/auth.h b/source3/include/auth.h
81d632
index b7223c15036..d3055373964 100644
81d632
--- a/source3/include/auth.h
81d632
+++ b/source3/include/auth.h
81d632
@@ -30,7 +30,6 @@ struct extra_auth_info {
81d632
 
81d632
 struct auth_serversupplied_info {
81d632
 	bool guest;
81d632
-	bool system;
81d632
 
81d632
 	struct security_unix_token utok;
81d632
 
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 092a1ddebdcd399676820edafb33afe535522ee4 Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Fri, 2 Mar 2018 16:37:58 +0100
81d632
Subject: [PATCH 05/21] s3:auth: add the "Unix Groups" sid for the primary gid
81d632
81d632
The primary gid might not be in the gid array.
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit f3ca3e71cc35876df47e31ec9c3643308add2405)
81d632
(cherry picked from commit 1258f287420642698c456f6bb17bf4547a921964)
81d632
---
81d632
 source3/auth/auth_util.c | 4 ++++
81d632
 1 file changed, 4 insertions(+)
81d632
81d632
diff --git a/source3/auth/auth_util.c b/source3/auth/auth_util.c
81d632
index 4ae9dad2dd6..2aa40388d14 100644
81d632
--- a/source3/auth/auth_util.c
81d632
+++ b/source3/auth/auth_util.c
81d632
@@ -660,7 +660,11 @@ NTSTATUS create_local_token(TALLOC_CTX *mem_ctx,
81d632
 	 */
81d632
 
81d632
 	uid_to_unix_users_sid(session_info->unix_token->uid, &tmp_sid);
81d632
+	add_sid_to_array_unique(session_info->security_token, &tmp_sid,
81d632
+				&session_info->security_token->sids,
81d632
+				&session_info->security_token->num_sids);
81d632
 
81d632
+	gid_to_unix_groups_sid(session_info->unix_token->gid, &tmp_sid);
81d632
 	add_sid_to_array_unique(session_info->security_token, &tmp_sid,
81d632
 				&session_info->security_token->sids,
81d632
 				&session_info->security_token->num_sids);
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From c7b23189a548a0d684e04ef78e0fa7c3e3456316 Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Tue, 6 Mar 2018 17:14:34 +0100
81d632
Subject: [PATCH 06/21] s3:auth: move add_local_groups() out of
81d632
 finalize_local_nt_token()
81d632
81d632
finalize_local_nt_token() will be used in another place,
81d632
were we don't want to add local groups in a following commit.
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit df3d278853ec097df27c221369dfb3ed0297d6c8)
81d632
(cherry picked from commit 85097b155447257d9c4a66cd43ac432a27b52529)
81d632
---
81d632
 source3/auth/token_util.c | 22 +++++++++++++++-------
81d632
 1 file changed, 15 insertions(+), 7 deletions(-)
81d632
81d632
diff --git a/source3/auth/token_util.c b/source3/auth/token_util.c
81d632
index 03c4b646007..e5a12db1ba3 100644
81d632
--- a/source3/auth/token_util.c
81d632
+++ b/source3/auth/token_util.c
81d632
@@ -208,6 +208,8 @@ static NTSTATUS add_builtin_administrators(struct security_token *token,
81d632
 	return NT_STATUS_OK;
81d632
 }
81d632
 
81d632
+static NTSTATUS add_local_groups(struct security_token *result,
81d632
+				 bool is_guest);
81d632
 static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 					bool is_guest);
81d632
 
81d632
@@ -323,6 +325,13 @@ NTSTATUS create_local_nt_token_from_info3(TALLOC_CTX *mem_ctx,
81d632
 		}
81d632
 	}
81d632
 
81d632
+	status = add_local_groups(usrtok, is_guest);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		DEBUG(3, ("Failed to add local groups\n"));
81d632
+		TALLOC_FREE(usrtok);
81d632
+		return status;
81d632
+	}
81d632
+
81d632
 	status = finalize_local_nt_token(usrtok, is_guest);
81d632
 	if (!NT_STATUS_IS_OK(status)) {
81d632
 		DEBUG(3, ("Failed to finalize nt token\n"));
81d632
@@ -392,6 +401,12 @@ struct security_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
81d632
 		}
81d632
 	}
81d632
 
81d632
+	status = add_local_groups(result, is_guest);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		TALLOC_FREE(result);
81d632
+		return NULL;
81d632
+	}
81d632
+
81d632
 	status = finalize_local_nt_token(result, is_guest);
81d632
 	if (!NT_STATUS_IS_OK(status)) {
81d632
 		TALLOC_FREE(result);
81d632
@@ -502,13 +517,6 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 	NTSTATUS status;
81d632
 	struct acct_info *info;
81d632
 
81d632
-	/* Add any local groups. */
81d632
-
81d632
-	status = add_local_groups(result, is_guest);
81d632
-	if (!NT_STATUS_IS_OK(status)) {
81d632
-		return status;
81d632
-	}
81d632
-
81d632
 	/* Add in BUILTIN sids */
81d632
 
81d632
 	status = add_sid_to_array(result, &global_sid_World,
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From b914f0e37eb05eb656d37cb317f1b3d556325edd Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Tue, 13 Mar 2018 21:35:48 +0100
81d632
Subject: [PATCH 07/21] s3:passdb: handle dom_sid=NULL in
81d632
 create_builtin_{users,administrators}()
81d632
81d632
We should not crash if we're called with NULL.
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit efdc617c76d9043286e33b961f45ad4564232102)
81d632
(cherry picked from commit c1f61c0816441be2061b3fd23db04dc60dcc64f7)
81d632
---
81d632
 source3/passdb/pdb_util.c | 10 ++++++----
81d632
 1 file changed, 6 insertions(+), 4 deletions(-)
81d632
81d632
diff --git a/source3/passdb/pdb_util.c b/source3/passdb/pdb_util.c
81d632
index bf7b2b8abd1..309eb893f8a 100644
81d632
--- a/source3/passdb/pdb_util.c
81d632
+++ b/source3/passdb/pdb_util.c
81d632
@@ -130,8 +130,9 @@ NTSTATUS create_builtin_users(const struct dom_sid *dom_sid)
81d632
 	}
81d632
 
81d632
 	/* add domain users */
81d632
-	if ((IS_DC || (lp_server_role() == ROLE_DOMAIN_MEMBER))
81d632
-		&& sid_compose(&dom_users, dom_sid, DOMAIN_RID_USERS))
81d632
+	if ((IS_DC || (lp_server_role() == ROLE_DOMAIN_MEMBER)) &&
81d632
+	    (dom_sid != NULL) &&
81d632
+	    sid_compose(&dom_users, dom_sid, DOMAIN_RID_USERS))
81d632
 	{
81d632
 		status = add_sid_to_builtin(&global_sid_Builtin_Users,
81d632
 					    &dom_users);
81d632
@@ -159,8 +160,9 @@ NTSTATUS create_builtin_administrators(const struct dom_sid *dom_sid)
81d632
 	}
81d632
 
81d632
 	/* add domain admins */
81d632
-	if ((IS_DC || (lp_server_role() == ROLE_DOMAIN_MEMBER))
81d632
-		&& sid_compose(&dom_admins, dom_sid, DOMAIN_RID_ADMINS))
81d632
+	if ((IS_DC || (lp_server_role() == ROLE_DOMAIN_MEMBER)) &&
81d632
+	    (dom_sid != NULL) &&
81d632
+	    sid_compose(&dom_admins, dom_sid, DOMAIN_RID_ADMINS))
81d632
 	{
81d632
 		status = add_sid_to_builtin(&global_sid_Builtin_Administrators,
81d632
 					    &dom_admins);
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From db7aa26880d37b0966cbf99100457ba31d3a0e9b Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Tue, 13 Mar 2018 21:38:27 +0100
81d632
Subject: [PATCH 08/21] s3:auth: only call secrets_fetch_domain_sid() once in
81d632
 finalize_local_nt_token()
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit c2ffbf9f764a94ef1dc1280741884cf63a017308)
81d632
(cherry picked from commit e0e4aa1ac539d2811bd801e9e3b8f69d7e306f3b)
81d632
---
81d632
 source3/auth/token_util.c | 35 +++++++++++++++++++----------------
81d632
 1 file changed, 19 insertions(+), 16 deletions(-)
81d632
81d632
diff --git a/source3/auth/token_util.c b/source3/auth/token_util.c
81d632
index e5a12db1ba3..f3d24cdac2f 100644
81d632
--- a/source3/auth/token_util.c
81d632
+++ b/source3/auth/token_util.c
81d632
@@ -190,6 +190,9 @@ static NTSTATUS add_builtin_administrators(struct security_token *token,
81d632
 	if ( IS_DC ) {
81d632
 		sid_copy( &domadm, get_global_sam_sid() );
81d632
 	} else {
81d632
+		if (dom_sid == NULL) {
81d632
+			return NT_STATUS_INVALID_PARAMETER_MIX;
81d632
+		}
81d632
 		sid_copy(&domadm, dom_sid);
81d632
 	}
81d632
 	sid_append_rid( &domadm, DOMAIN_RID_ADMINS );
81d632
@@ -513,9 +516,11 @@ static NTSTATUS add_local_groups(struct security_token *result,
81d632
 static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 					bool is_guest)
81d632
 {
81d632
-	struct dom_sid dom_sid;
81d632
+	struct dom_sid _dom_sid = { 0, };
81d632
+	struct dom_sid *domain_sid = NULL;
81d632
 	NTSTATUS status;
81d632
 	struct acct_info *info;
81d632
+	bool ok;
81d632
 
81d632
 	/* Add in BUILTIN sids */
81d632
 
81d632
@@ -547,6 +552,16 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 		}
81d632
 	}
81d632
 
81d632
+	become_root();
81d632
+	ok = secrets_fetch_domain_sid(lp_workgroup(), &_dom_sid);
81d632
+	if (ok) {
81d632
+		domain_sid = &_dom_sid;
81d632
+	} else {
81d632
+		DEBUG(3, ("Failed to fetch domain sid for %s\n",
81d632
+			  lp_workgroup()));
81d632
+	}
81d632
+	unbecome_root();
81d632
+
81d632
 	info = talloc_zero(talloc_tos(), struct acct_info);
81d632
 	if (info == NULL) {
81d632
 		DEBUG(0, ("talloc failed!\n"));
81d632
@@ -561,18 +576,12 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 	if (!NT_STATUS_IS_OK(status)) {
81d632
 
81d632
 		become_root();
81d632
-		if (!secrets_fetch_domain_sid(lp_workgroup(), &dom_sid)) {
81d632
-			status = NT_STATUS_OK;
81d632
-			DEBUG(3, ("Failed to fetch domain sid for %s\n",
81d632
-				  lp_workgroup()));
81d632
-		} else {
81d632
-			status = create_builtin_administrators(&dom_sid);
81d632
-		}
81d632
+		status = create_builtin_administrators(domain_sid);
81d632
 		unbecome_root();
81d632
 
81d632
 		if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
81d632
 			/* Add BUILTIN\Administrators directly to token. */
81d632
-			status = add_builtin_administrators(result, &dom_sid);
81d632
+			status = add_builtin_administrators(result, domain_sid);
81d632
 			if ( !NT_STATUS_IS_OK(status) ) {
81d632
 				DEBUG(3, ("Failed to check for local "
81d632
 					  "Administrators membership (%s)\n",
81d632
@@ -593,13 +602,7 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 	if (!NT_STATUS_IS_OK(status)) {
81d632
 
81d632
 		become_root();
81d632
-		if (!secrets_fetch_domain_sid(lp_workgroup(), &dom_sid)) {
81d632
-			status = NT_STATUS_OK;
81d632
-			DEBUG(3, ("Failed to fetch domain sid for %s\n",
81d632
-				  lp_workgroup()));
81d632
-		} else {
81d632
-			status = create_builtin_users(&dom_sid);
81d632
-		}
81d632
+		status = create_builtin_users(domain_sid);
81d632
 		unbecome_root();
81d632
 
81d632
 		if (!NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE) &&
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 9c86a3d2a0783fae2ec2883907ec877f9edd1dac Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Tue, 6 Mar 2018 23:26:28 +0100
81d632
Subject: [PATCH 09/21] s3:auth: add add_builtin_guests() handling to
81d632
 finalize_local_nt_token()
81d632
81d632
We should add Builtin_Guests depending on the current token
81d632
not based on 'is_guest'. Even authenticated users can be member
81d632
a guest related group and therefore get Builtin_Guests.
81d632
81d632
Sadly we still need to use 'is_guest' within create_local_nt_token()
81d632
as we only have S-1-22-* SIDs there and still need to
81d632
add Builtin_Guests.
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit e8dc55d2b969b670322a913799d1af459a1000e7)
81d632
(cherry picked from commit 7687d26f8bb6aa57672c70f95bee3f67b9957107)
81d632
---
81d632
 source3/auth/token_util.c | 122 +++++++++++++++++++++++++++++++++++---
81d632
 1 file changed, 114 insertions(+), 8 deletions(-)
81d632
81d632
diff --git a/source3/auth/token_util.c b/source3/auth/token_util.c
81d632
index f3d24cdac2f..30f2f8d346b 100644
81d632
--- a/source3/auth/token_util.c
81d632
+++ b/source3/auth/token_util.c
81d632
@@ -211,6 +211,74 @@ static NTSTATUS add_builtin_administrators(struct security_token *token,
81d632
 	return NT_STATUS_OK;
81d632
 }
81d632
 
81d632
+static NTSTATUS add_builtin_guests(struct security_token *token,
81d632
+				   const struct dom_sid *dom_sid)
81d632
+{
81d632
+	struct dom_sid tmp_sid;
81d632
+	NTSTATUS status;
81d632
+
81d632
+	/*
81d632
+	 * First check the local GUEST account.
81d632
+	 */
81d632
+	sid_copy(&tmp_sid, get_global_sam_sid());
81d632
+	sid_append_rid(&tmp_sid, DOMAIN_RID_GUEST);
81d632
+
81d632
+	if (nt_token_check_sid(&tmp_sid, token)) {
81d632
+		status = add_sid_to_array_unique(token,
81d632
+					&global_sid_Builtin_Guests,
81d632
+					&token->sids, &token->num_sids);
81d632
+		if (!NT_STATUS_IS_OK(status)) {
81d632
+			return status;
81d632
+		}
81d632
+
81d632
+		return NT_STATUS_OK;
81d632
+	}
81d632
+
81d632
+	/*
81d632
+	 * First check the local GUESTS group.
81d632
+	 */
81d632
+	sid_copy(&tmp_sid, get_global_sam_sid());
81d632
+	sid_append_rid(&tmp_sid, DOMAIN_RID_GUESTS);
81d632
+
81d632
+	if (nt_token_check_sid(&tmp_sid, token)) {
81d632
+		status = add_sid_to_array_unique(token,
81d632
+					&global_sid_Builtin_Guests,
81d632
+					&token->sids, &token->num_sids);
81d632
+		if (!NT_STATUS_IS_OK(status)) {
81d632
+			return status;
81d632
+		}
81d632
+
81d632
+		return NT_STATUS_OK;
81d632
+	}
81d632
+
81d632
+	if (lp_server_role() != ROLE_DOMAIN_MEMBER) {
81d632
+		return NT_STATUS_OK;
81d632
+	}
81d632
+
81d632
+	if (dom_sid == NULL) {
81d632
+		return NT_STATUS_INVALID_PARAMETER_MIX;
81d632
+	}
81d632
+
81d632
+	/*
81d632
+	 * First check the domain GUESTS group.
81d632
+	 */
81d632
+	sid_copy(&tmp_sid, dom_sid);
81d632
+	sid_append_rid(&tmp_sid, DOMAIN_RID_GUESTS);
81d632
+
81d632
+	if (nt_token_check_sid(&tmp_sid, token)) {
81d632
+		status = add_sid_to_array_unique(token,
81d632
+					&global_sid_Builtin_Guests,
81d632
+					&token->sids, &token->num_sids);
81d632
+		if (!NT_STATUS_IS_OK(status)) {
81d632
+			return status;
81d632
+		}
81d632
+
81d632
+		return NT_STATUS_OK;
81d632
+	}
81d632
+
81d632
+	return NT_STATUS_OK;
81d632
+}
81d632
+
81d632
 static NTSTATUS add_local_groups(struct security_token *result,
81d632
 				 bool is_guest);
81d632
 static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
@@ -416,6 +484,29 @@ struct security_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
81d632
 		return NULL;
81d632
 	}
81d632
 
81d632
+	if (is_guest) {
81d632
+		/*
81d632
+		 * It's ugly, but for now it's
81d632
+		 * needed to add Builtin_Guests
81d632
+		 * here, the "local" token only
81d632
+		 * consist of S-1-22-* SIDs
81d632
+		 * and finalize_local_nt_token()
81d632
+		 * doesn't have the chance to
81d632
+		 * to detect it need to
81d632
+		 * add Builtin_Guests via
81d632
+		 * add_builtin_guests().
81d632
+		 */
81d632
+		status = add_sid_to_array_unique(result,
81d632
+						 &global_sid_Builtin_Guests,
81d632
+						 &result->sids,
81d632
+						 &result->num_sids);
81d632
+		if (!NT_STATUS_IS_OK(status)) {
81d632
+			DEBUG(3, ("Failed to add SID to nt token\n"));
81d632
+			TALLOC_FREE(result);
81d632
+			return NULL;
81d632
+		}
81d632
+	}
81d632
+
81d632
 	return result;
81d632
 }
81d632
 
81d632
@@ -535,14 +626,7 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 		return status;
81d632
 	}
81d632
 
81d632
-	if (is_guest) {
81d632
-		status = add_sid_to_array(result, &global_sid_Builtin_Guests,
81d632
-					  &result->sids,
81d632
-					  &result->num_sids);
81d632
-		if (!NT_STATUS_IS_OK(status)) {
81d632
-			return status;
81d632
-		}
81d632
-	} else {
81d632
+	if (!is_guest) {
81d632
 		status = add_sid_to_array(result,
81d632
 					  &global_sid_Authenticated_Users,
81d632
 					  &result->sids,
81d632
@@ -613,6 +697,28 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 		}
81d632
 	}
81d632
 
81d632
+	/*
81d632
+	 * Add BUILTIN\Guests directly to token.
81d632
+	 * But only if the token already indicates
81d632
+	 * real guest access by:
81d632
+	 * - local GUEST account
81d632
+	 * - local GUESTS group
81d632
+	 * - domain GUESTS group
81d632
+	 *
81d632
+	 * Even if a user was authenticated, it
81d632
+	 * can be member of a guest related group.
81d632
+	 */
81d632
+	status = add_builtin_guests(result, domain_sid);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		DEBUG(3, ("Failed to check for local "
81d632
+			  "Guests membership (%s)\n",
81d632
+			  nt_errstr(status)));
81d632
+		/*
81d632
+		 * This is a hard error.
81d632
+		 */
81d632
+		return status;
81d632
+	}
81d632
+
81d632
 	TALLOC_FREE(info);
81d632
 
81d632
 	/* Deal with local groups */
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 02ec86b90cc7c293d3086d59a0d349a967375665 Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Tue, 6 Mar 2018 23:36:03 +0100
81d632
Subject: [PATCH 10/21] s3:auth: don't try to expand system or anonymous tokens
81d632
 in finalize_local_nt_token()
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit 4f81ef9353ad76390aa910c8c17456fec21916c6)
81d632
(cherry picked from commit ecee9453a6ef611763d11e88e2ecf212f065a86c)
81d632
---
81d632
 source3/auth/token_util.c | 24 ++++++++++++++++++++++++
81d632
 1 file changed, 24 insertions(+)
81d632
81d632
diff --git a/source3/auth/token_util.c b/source3/auth/token_util.c
81d632
index 30f2f8d346b..6ebfa54126b 100644
81d632
--- a/source3/auth/token_util.c
81d632
+++ b/source3/auth/token_util.c
81d632
@@ -613,6 +613,13 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 	struct acct_info *info;
81d632
 	bool ok;
81d632
 
81d632
+	result->privilege_mask = 0;
81d632
+	result->rights_mask = 0;
81d632
+
81d632
+	if (result->num_sids == 0) {
81d632
+		return NT_STATUS_INVALID_TOKEN;
81d632
+	}
81d632
+
81d632
 	/* Add in BUILTIN sids */
81d632
 
81d632
 	status = add_sid_to_array(result, &global_sid_World,
81d632
@@ -626,6 +633,23 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 		return status;
81d632
 	}
81d632
 
81d632
+	/*
81d632
+	 * Don't expand nested groups of system, anonymous etc
81d632
+	 *
81d632
+	 * Note that they still get SID_WORLD and SID_NETWORK
81d632
+	 * for now in order let existing tests pass.
81d632
+	 *
81d632
+	 * But SYSTEM doesn't get AUTHENTICATED_USERS
81d632
+	 * and ANONYMOUS doesn't get BUILTIN GUESTS anymore.
81d632
+	 */
81d632
+	if (security_token_is_anonymous(result)) {
81d632
+		return NT_STATUS_OK;
81d632
+	}
81d632
+	if (security_token_is_system(result)) {
81d632
+		result->privilege_mask = ~0;
81d632
+		return NT_STATUS_OK;
81d632
+	}
81d632
+
81d632
 	if (!is_guest) {
81d632
 		status = add_sid_to_array(result,
81d632
 					  &global_sid_Authenticated_Users,
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From e243c00682b4e3b82f5cdddf7079d6dadb5f2e68 Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Tue, 6 Mar 2018 23:40:10 +0100
81d632
Subject: [PATCH 11/21] s3:auth: pass AUTH_SESSION_INFO_* flags to
81d632
 finalize_local_nt_token()
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit d3aae5ba65c7ed0d5e9f8389101cf1c8c1f0a25b)
81d632
(cherry picked from commit 627a86bf2d516e256701f50473d0cdfd15d7eecc)
81d632
---
81d632
 source3/auth/token_util.c | 58 ++++++++++++++++++++++++++-------------
81d632
 1 file changed, 39 insertions(+), 19 deletions(-)
81d632
81d632
diff --git a/source3/auth/token_util.c b/source3/auth/token_util.c
81d632
index 6ebfa54126b..acb916ab55c 100644
81d632
--- a/source3/auth/token_util.c
81d632
+++ b/source3/auth/token_util.c
81d632
@@ -282,7 +282,7 @@ static NTSTATUS add_builtin_guests(struct security_token *token,
81d632
 static NTSTATUS add_local_groups(struct security_token *result,
81d632
 				 bool is_guest);
81d632
 static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
-					bool is_guest);
81d632
+					uint32_t session_info_flags);
81d632
 
81d632
 NTSTATUS get_user_sid_info3_and_extra(const struct netr_SamInfo3 *info3,
81d632
 				      const struct extra_auth_info *extra,
81d632
@@ -313,6 +313,7 @@ NTSTATUS create_local_nt_token_from_info3(TALLOC_CTX *mem_ctx,
81d632
 					  struct security_token **ntok)
81d632
 {
81d632
 	struct security_token *usrtok = NULL;
81d632
+	uint32_t session_info_flags = 0;
81d632
 	NTSTATUS status;
81d632
 	int i;
81d632
 
81d632
@@ -403,7 +404,12 @@ NTSTATUS create_local_nt_token_from_info3(TALLOC_CTX *mem_ctx,
81d632
 		return status;
81d632
 	}
81d632
 
81d632
-	status = finalize_local_nt_token(usrtok, is_guest);
81d632
+	session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
81d632
+	if (!is_guest) {
81d632
+		session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
81d632
+	}
81d632
+
81d632
+	status = finalize_local_nt_token(usrtok, session_info_flags);
81d632
 	if (!NT_STATUS_IS_OK(status)) {
81d632
 		DEBUG(3, ("Failed to finalize nt token\n"));
81d632
 		TALLOC_FREE(usrtok);
81d632
@@ -427,6 +433,7 @@ struct security_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
81d632
 	struct security_token *result = NULL;
81d632
 	int i;
81d632
 	NTSTATUS status;
81d632
+	uint32_t session_info_flags = 0;
81d632
 
81d632
 	DEBUG(10, ("Create local NT token for %s\n",
81d632
 		   sid_string_dbg(user_sid)));
81d632
@@ -478,7 +485,12 @@ struct security_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
81d632
 		return NULL;
81d632
 	}
81d632
 
81d632
-	status = finalize_local_nt_token(result, is_guest);
81d632
+	session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
81d632
+	if (!is_guest) {
81d632
+		session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
81d632
+	}
81d632
+
81d632
+	status = finalize_local_nt_token(result, session_info_flags);
81d632
 	if (!NT_STATUS_IS_OK(status)) {
81d632
 		TALLOC_FREE(result);
81d632
 		return NULL;
81d632
@@ -605,7 +617,7 @@ static NTSTATUS add_local_groups(struct security_token *result,
81d632
 }
81d632
 
81d632
 static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
-					bool is_guest)
81d632
+					uint32_t session_info_flags)
81d632
 {
81d632
 	struct dom_sid _dom_sid = { 0, };
81d632
 	struct dom_sid *domain_sid = NULL;
81d632
@@ -620,17 +632,17 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 		return NT_STATUS_INVALID_TOKEN;
81d632
 	}
81d632
 
81d632
-	/* Add in BUILTIN sids */
81d632
-
81d632
-	status = add_sid_to_array(result, &global_sid_World,
81d632
-				  &result->sids, &result->num_sids);
81d632
-	if (!NT_STATUS_IS_OK(status)) {
81d632
-		return status;
81d632
-	}
81d632
-	status = add_sid_to_array(result, &global_sid_Network,
81d632
-				  &result->sids, &result->num_sids);
81d632
-	if (!NT_STATUS_IS_OK(status)) {
81d632
-		return status;
81d632
+	if (session_info_flags & AUTH_SESSION_INFO_DEFAULT_GROUPS) {
81d632
+		status = add_sid_to_array(result, &global_sid_World,
81d632
+					  &result->sids, &result->num_sids);
81d632
+		if (!NT_STATUS_IS_OK(status)) {
81d632
+			return status;
81d632
+		}
81d632
+		status = add_sid_to_array(result, &global_sid_Network,
81d632
+					  &result->sids, &result->num_sids);
81d632
+		if (!NT_STATUS_IS_OK(status)) {
81d632
+			return status;
81d632
+		}
81d632
 	}
81d632
 
81d632
 	/*
81d632
@@ -650,7 +662,7 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 		return NT_STATUS_OK;
81d632
 	}
81d632
 
81d632
-	if (!is_guest) {
81d632
+	if (session_info_flags & AUTH_SESSION_INFO_AUTHENTICATED) {
81d632
 		status = add_sid_to_array(result,
81d632
 					  &global_sid_Authenticated_Users,
81d632
 					  &result->sids,
81d632
@@ -660,6 +672,8 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 		}
81d632
 	}
81d632
 
81d632
+	/* Add in BUILTIN sids */
81d632
+
81d632
 	become_root();
81d632
 	ok = secrets_fetch_domain_sid(lp_workgroup(), &_dom_sid);
81d632
 	if (ok) {
81d632
@@ -772,10 +786,16 @@ static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
 		unbecome_root();
81d632
 	}
81d632
 
81d632
-	/* Add privileges based on current user sids */
81d632
 
81d632
-	get_privileges_for_sids(&result->privilege_mask, result->sids,
81d632
-				result->num_sids);
81d632
+	if (session_info_flags & AUTH_SESSION_INFO_SIMPLE_PRIVILEGES) {
81d632
+		if (security_token_has_builtin_administrators(result)) {
81d632
+			result->privilege_mask = ~0;
81d632
+		}
81d632
+	} else {
81d632
+		/* Add privileges based on current user sids */
81d632
+		get_privileges_for_sids(&result->privilege_mask, result->sids,
81d632
+					result->num_sids);
81d632
+	}
81d632
 
81d632
 	return NT_STATUS_OK;
81d632
 }
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From d97bfd5d7ecc48f6781161397928d9094d95dae1 Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Tue, 6 Mar 2018 23:45:30 +0100
81d632
Subject: [PATCH 12/21] s3:auth: remove static from finalize_local_nt_token()
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit 7f47f9e1f220d2dd547cf77bbc292357a2173870)
81d632
(cherry picked from commit 8b5253e5d4c79265a9c35955f83407a0c11a76d1)
81d632
---
81d632
 source3/auth/proto.h      | 2 ++
81d632
 source3/auth/token_util.c | 6 ++----
81d632
 2 files changed, 4 insertions(+), 4 deletions(-)
81d632
81d632
diff --git a/source3/auth/proto.h b/source3/auth/proto.h
81d632
index 3942815e467..d3403f1a929 100644
81d632
--- a/source3/auth/proto.h
81d632
+++ b/source3/auth/proto.h
81d632
@@ -359,6 +359,8 @@ struct security_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
81d632
 					    bool is_guest,
81d632
 					    int num_groupsids,
81d632
 					    const struct dom_sid *groupsids);
81d632
+NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
+				 uint32_t session_info_flags);
81d632
 NTSTATUS get_user_sid_info3_and_extra(const struct netr_SamInfo3 *info3,
81d632
 				      const struct extra_auth_info *extra,
81d632
 				      struct dom_sid *sid);
81d632
diff --git a/source3/auth/token_util.c b/source3/auth/token_util.c
81d632
index acb916ab55c..f015f8d2cd5 100644
81d632
--- a/source3/auth/token_util.c
81d632
+++ b/source3/auth/token_util.c
81d632
@@ -281,8 +281,6 @@ static NTSTATUS add_builtin_guests(struct security_token *token,
81d632
 
81d632
 static NTSTATUS add_local_groups(struct security_token *result,
81d632
 				 bool is_guest);
81d632
-static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
-					uint32_t session_info_flags);
81d632
 
81d632
 NTSTATUS get_user_sid_info3_and_extra(const struct netr_SamInfo3 *info3,
81d632
 				      const struct extra_auth_info *extra,
81d632
@@ -616,8 +614,8 @@ static NTSTATUS add_local_groups(struct security_token *result,
81d632
 	return NT_STATUS_OK;
81d632
 }
81d632
 
81d632
-static NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
-					uint32_t session_info_flags)
81d632
+NTSTATUS finalize_local_nt_token(struct security_token *result,
81d632
+				 uint32_t session_info_flags)
81d632
 {
81d632
 	struct dom_sid _dom_sid = { 0, };
81d632
 	struct dom_sid *domain_sid = NULL;
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 424de089a89f226854e159c1ce0bab3dc2eb8eaf Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Tue, 6 Mar 2018 16:38:10 +0100
81d632
Subject: [PATCH 13/21] auth: add auth_user_info_copy() function
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit 6ff891195855403bc485725aef8d43d4e3cabacb)
81d632
(cherry picked from commit 104de61756e6b098985c3a599a3ccf62cbbe7299)
81d632
---
81d632
 auth/auth_sam_reply.c | 35 +++++++++++++++++++++++++++++++++++
81d632
 auth/auth_sam_reply.h |  3 +++
81d632
 2 files changed, 38 insertions(+)
81d632
81d632
diff --git a/auth/auth_sam_reply.c b/auth/auth_sam_reply.c
81d632
index 15d17b0745e..bd695151dc0 100644
81d632
--- a/auth/auth_sam_reply.c
81d632
+++ b/auth/auth_sam_reply.c
81d632
@@ -333,6 +333,41 @@ NTSTATUS make_user_info_SamBaseInfo(TALLOC_CTX *mem_ctx,
81d632
 	return NT_STATUS_OK;
81d632
 }
81d632
 
81d632
+struct auth_user_info *auth_user_info_copy(TALLOC_CTX *mem_ctx,
81d632
+					   const struct auth_user_info *src)
81d632
+{
81d632
+	struct auth_user_info *dst = NULL;
81d632
+
81d632
+	dst = talloc_zero(mem_ctx, struct auth_user_info);
81d632
+	if (dst == NULL) {
81d632
+		return NULL;
81d632
+	}
81d632
+
81d632
+	*dst = *src;
81d632
+#define _COPY_STRING(_mem, _str) do { \
81d632
+	if ((_str) != NULL) { \
81d632
+		(_str) = talloc_strdup((_mem), (_str)); \
81d632
+		if ((_str) == NULL) { \
81d632
+			TALLOC_FREE(dst); \
81d632
+			return NULL; \
81d632
+		} \
81d632
+	} \
81d632
+} while(0)
81d632
+	_COPY_STRING(dst, dst->account_name);
81d632
+	_COPY_STRING(dst, dst->user_principal_name);
81d632
+	_COPY_STRING(dst, dst->domain_name);
81d632
+	_COPY_STRING(dst, dst->dns_domain_name);
81d632
+	_COPY_STRING(dst, dst->full_name);
81d632
+	_COPY_STRING(dst, dst->logon_script);
81d632
+	_COPY_STRING(dst, dst->profile_path);
81d632
+	_COPY_STRING(dst, dst->home_directory);
81d632
+	_COPY_STRING(dst, dst->home_drive);
81d632
+	_COPY_STRING(dst, dst->logon_server);
81d632
+#undef _COPY_STRING
81d632
+
81d632
+	return dst;
81d632
+}
81d632
+
81d632
 /**
81d632
  * Make a user_info_dc struct from the info3 returned by a domain logon
81d632
  */
81d632
diff --git a/auth/auth_sam_reply.h b/auth/auth_sam_reply.h
81d632
index 4aa3096c889..e4b26e961d7 100644
81d632
--- a/auth/auth_sam_reply.h
81d632
+++ b/auth/auth_sam_reply.h
81d632
@@ -38,6 +38,9 @@ NTSTATUS make_user_info_SamBaseInfo(TALLOC_CTX *mem_ctx,
81d632
 				    bool authenticated,
81d632
 				    struct auth_user_info **_user_info);
81d632
 
81d632
+struct auth_user_info *auth_user_info_copy(TALLOC_CTX *mem_ctx,
81d632
+					   const struct auth_user_info *src);
81d632
+
81d632
 NTSTATUS auth_convert_user_info_dc_saminfo6(TALLOC_CTX *mem_ctx,
81d632
 					   const struct auth_user_info_dc *user_info_dc,
81d632
 					   struct netr_SamInfo6 **_sam6);
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 417e52e67a662903ee0585371bcb9507fe6f8d87 Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Wed, 7 Mar 2018 00:21:13 +0100
81d632
Subject: [PATCH 14/21] s3:auth: add auth3_user_info_dc_add_hints() and
81d632
 auth3_session_info_create()
81d632
81d632
These functions make it possible to construct a full auth_session_info
81d632
from the information available from an auth_user_info_dc structure.
81d632
81d632
This has all the logic from create_local_token() that is used
81d632
to transform a auth_serversupplied_info to a full auth_session_info.
81d632
81d632
In order to workarround the restriction that auth_user_info_dc
81d632
doesn't contain hints for the unix token/name, we use
81d632
the special S-1-5-88 (Unix_NFS) sids:
81d632
81d632
 - S-1-5-88-1-Y gives the uid=Y
81d632
 - S-1-5-88-2-Y gives the gid=Y
81d632
 - S-1-5-88-3-Y gives flags=Y AUTH3_UNIX_HINT_*
81d632
81d632
The currently implemented flags are:
81d632
81d632
- AUTH3_UNIX_HINT_QUALIFIED_NAME
81d632
  unix_name = DOMAIN+ACCOUNT
81d632
81d632
- AUTH3_UNIX_HINT_ISLOLATED_NAME
81d632
  unix_name = ACCOUNT
81d632
81d632
- AUTH3_UNIX_HINT_DONT_TRANSLATE_FROM_SIDS
81d632
  Don't translate the nt token SIDS into uid/gids
81d632
  using sid mapping.
81d632
81d632
- AUTH3_UNIX_HINT_DONT_TRANSLATE_TO_SIDS
81d632
  Don't translate the unix token uid/gids to S-1-22-X-Y SIDS
81d632
81d632
- AUTH3_UNIX_HINT_DONT_EXPAND_UNIX_GROUPS
81d632
  The unix token won't get expanded gid values
81d632
  from getgroups_unix_user()
81d632
81d632
By using the hints it is possible to keep the current logic
81d632
where an authentication backend provides uid/gid values and
81d632
the unix name.
81d632
81d632
Note the S-1-5-88-* SIDS never appear in the final security_token.
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit af4bc135e486e17164da0ea918281fbf689892c3)
81d632
(cherry picked from commit b8c518d57fc32f8daffb0d4798dc8f5de17c0150)
81d632
---
81d632
 source3/auth/auth_util.c | 552 +++++++++++++++++++++++++++++++++++++++
81d632
 source3/auth/proto.h     |  32 +++
81d632
 2 files changed, 584 insertions(+)
81d632
81d632
diff --git a/source3/auth/auth_util.c b/source3/auth/auth_util.c
81d632
index 2aa40388d14..9d6e8020d77 100644
81d632
--- a/source3/auth/auth_util.c
81d632
+++ b/source3/auth/auth_util.c
81d632
@@ -692,6 +692,558 @@ NTSTATUS create_local_token(TALLOC_CTX *mem_ctx,
81d632
 	return NT_STATUS_OK;
81d632
 }
81d632
 
81d632
+NTSTATUS auth3_user_info_dc_add_hints(struct auth_user_info_dc *user_info_dc,
81d632
+				      uid_t uid,
81d632
+				      gid_t gid,
81d632
+				      uint32_t flags)
81d632
+{
81d632
+	uint32_t orig_num_sids = user_info_dc->num_sids;
81d632
+	struct dom_sid tmp_sid = { 0, };
81d632
+	NTSTATUS status;
81d632
+
81d632
+	/*
81d632
+	 * We add S-5-88-1-X in order to pass the uid
81d632
+	 * for the unix token.
81d632
+	 */
81d632
+	sid_compose(&tmp_sid,
81d632
+		    &global_sid_Unix_NFS_Users,
81d632
+		    (uint32_t)uid);
81d632
+	status = add_sid_to_array_unique(user_info_dc->sids,
81d632
+					 &tmp_sid,
81d632
+					 &user_info_dc->sids,
81d632
+					 &user_info_dc->num_sids);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		DEBUG(0, ("add_sid_to_array_unique failed: %s\n",
81d632
+			  nt_errstr(status)));
81d632
+		goto fail;
81d632
+	}
81d632
+
81d632
+	/*
81d632
+	 * We add S-5-88-2-X in order to pass the gid
81d632
+	 * for the unix token.
81d632
+	 */
81d632
+	sid_compose(&tmp_sid,
81d632
+		    &global_sid_Unix_NFS_Groups,
81d632
+		    (uint32_t)gid);
81d632
+	status = add_sid_to_array_unique(user_info_dc->sids,
81d632
+					 &tmp_sid,
81d632
+					 &user_info_dc->sids,
81d632
+					 &user_info_dc->num_sids);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		DEBUG(0, ("add_sid_to_array_unique failed: %s\n",
81d632
+			  nt_errstr(status)));
81d632
+		goto fail;
81d632
+	}
81d632
+
81d632
+	/*
81d632
+	 * We add S-5-88-3-X in order to pass some flags
81d632
+	 * (AUTH3_UNIX_HINT_*) to auth3_create_session_info().
81d632
+	 */
81d632
+	sid_compose(&tmp_sid,
81d632
+		    &global_sid_Unix_NFS_Mode,
81d632
+		    flags);
81d632
+	status = add_sid_to_array_unique(user_info_dc->sids,
81d632
+					 &tmp_sid,
81d632
+					 &user_info_dc->sids,
81d632
+					 &user_info_dc->num_sids);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		DEBUG(0, ("add_sid_to_array_unique failed: %s\n",
81d632
+			  nt_errstr(status)));
81d632
+		goto fail;
81d632
+	}
81d632
+
81d632
+	return NT_STATUS_OK;
81d632
+
81d632
+fail:
81d632
+	user_info_dc->num_sids = orig_num_sids;
81d632
+	return status;
81d632
+}
81d632
+
81d632
+NTSTATUS auth3_session_info_create(TALLOC_CTX *mem_ctx,
81d632
+				   const struct auth_user_info_dc *user_info_dc,
81d632
+				   const char *original_user_name,
81d632
+				   uint32_t session_info_flags,
81d632
+				   struct auth_session_info **session_info_out)
81d632
+{
81d632
+	TALLOC_CTX *frame = talloc_stackframe();
81d632
+	struct auth_session_info *session_info = NULL;
81d632
+	uid_t hint_uid = -1;
81d632
+	bool found_hint_uid = false;
81d632
+	uid_t hint_gid = -1;
81d632
+	bool found_hint_gid = false;
81d632
+	uint32_t hint_flags = 0;
81d632
+	bool found_hint_flags = false;
81d632
+	bool need_getpwuid = false;
81d632
+	struct unixid *ids = NULL;
81d632
+	uint32_t num_gids = 0;
81d632
+	gid_t *gids = NULL;
81d632
+	struct dom_sid tmp_sid = { 0, };
81d632
+	fstring tmp = { 0, };
81d632
+	NTSTATUS status;
81d632
+	size_t i;
81d632
+	bool ok;
81d632
+
81d632
+	*session_info_out = NULL;
81d632
+
81d632
+	if (user_info_dc->num_sids == 0) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_INVALID_TOKEN;
81d632
+	}
81d632
+
81d632
+	if (user_info_dc->info == NULL) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_INVALID_TOKEN;
81d632
+	}
81d632
+
81d632
+	if (user_info_dc->info->account_name == NULL) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_INVALID_TOKEN;
81d632
+	}
81d632
+
81d632
+	session_info = talloc_zero(mem_ctx, struct auth_session_info);
81d632
+	if (session_info == NULL) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_NO_MEMORY;
81d632
+	}
81d632
+	/* keep this under frame for easier cleanup */
81d632
+	talloc_reparent(mem_ctx, frame, session_info);
81d632
+
81d632
+	session_info->info = auth_user_info_copy(session_info,
81d632
+						 user_info_dc->info);
81d632
+	if (session_info->info == NULL) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_NO_MEMORY;
81d632
+	}
81d632
+
81d632
+	session_info->security_token = talloc_zero(session_info,
81d632
+						   struct security_token);
81d632
+	if (session_info->security_token == NULL) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_NO_MEMORY;
81d632
+	}
81d632
+
81d632
+	/*
81d632
+	 * Avoid a lot of reallocations and allocate what we'll
81d632
+	 * use in most cases.
81d632
+	 */
81d632
+	session_info->security_token->sids = talloc_zero_array(
81d632
+						session_info->security_token,
81d632
+						struct dom_sid,
81d632
+						user_info_dc->num_sids);
81d632
+	if (session_info->security_token->sids == NULL) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_NO_MEMORY;
81d632
+	}
81d632
+
81d632
+	for (i = PRIMARY_USER_SID_INDEX; i < user_info_dc->num_sids; i++) {
81d632
+		struct security_token *nt_token = session_info->security_token;
81d632
+		int cmp;
81d632
+
81d632
+		/*
81d632
+		 * S-1-5-88-X-Y sids are only used to give hints
81d632
+		 * to the unix token construction.
81d632
+		 *
81d632
+		 * S-1-5-88-1-Y gives the uid=Y
81d632
+		 * S-1-5-88-2-Y gives the gid=Y
81d632
+		 * S-1-5-88-3-Y gives flags=Y: AUTH3_UNIX_HINT_*
81d632
+		 */
81d632
+		cmp = dom_sid_compare_domain(&global_sid_Unix_NFS,
81d632
+					     &user_info_dc->sids[i]);
81d632
+		if (cmp == 0) {
81d632
+			bool match;
81d632
+			uint32_t hint = 0;
81d632
+
81d632
+			match = sid_peek_rid(&user_info_dc->sids[i], &hint);
81d632
+			if (!match) {
81d632
+				continue;
81d632
+			}
81d632
+
81d632
+			match = dom_sid_in_domain(&global_sid_Unix_NFS_Users,
81d632
+						  &user_info_dc->sids[i]);
81d632
+			if (match) {
81d632
+				if (found_hint_uid) {
81d632
+					TALLOC_FREE(frame);
81d632
+					return NT_STATUS_INVALID_TOKEN;
81d632
+				}
81d632
+				found_hint_uid = true;
81d632
+				hint_uid = (uid_t)hint;
81d632
+				continue;
81d632
+			}
81d632
+
81d632
+			match = dom_sid_in_domain(&global_sid_Unix_NFS_Groups,
81d632
+						  &user_info_dc->sids[i]);
81d632
+			if (match) {
81d632
+				if (found_hint_gid) {
81d632
+					TALLOC_FREE(frame);
81d632
+					return NT_STATUS_INVALID_TOKEN;
81d632
+				}
81d632
+				found_hint_gid = true;
81d632
+				hint_gid = (gid_t)hint;
81d632
+				continue;
81d632
+			}
81d632
+
81d632
+			match = dom_sid_in_domain(&global_sid_Unix_NFS_Mode,
81d632
+						  &user_info_dc->sids[i]);
81d632
+			if (match) {
81d632
+				if (found_hint_flags) {
81d632
+					TALLOC_FREE(frame);
81d632
+					return NT_STATUS_INVALID_TOKEN;
81d632
+				}
81d632
+				found_hint_flags = true;
81d632
+				hint_flags = hint;
81d632
+				continue;
81d632
+			}
81d632
+
81d632
+			continue;
81d632
+		}
81d632
+
81d632
+		status = add_sid_to_array_unique(nt_token->sids,
81d632
+						 &user_info_dc->sids[i],
81d632
+						 &nt_token->sids,
81d632
+						 &nt_token->num_sids);
81d632
+		if (!NT_STATUS_IS_OK(status)) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return status;
81d632
+		}
81d632
+	}
81d632
+
81d632
+	/*
81d632
+	 * We need at least one usable SID
81d632
+	 */
81d632
+	if (session_info->security_token->num_sids == 0) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_INVALID_TOKEN;
81d632
+	}
81d632
+
81d632
+	/*
81d632
+	 * We need all tree hints: uid, gid, flags
81d632
+	 * or none of them.
81d632
+	 */
81d632
+	if (found_hint_uid || found_hint_gid || found_hint_flags) {
81d632
+		if (!found_hint_uid) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_INVALID_TOKEN;
81d632
+		}
81d632
+
81d632
+		if (!found_hint_gid) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_INVALID_TOKEN;
81d632
+		}
81d632
+
81d632
+		if (!found_hint_flags) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_INVALID_TOKEN;
81d632
+		}
81d632
+	}
81d632
+
81d632
+	if (session_info->info->authenticated) {
81d632
+		session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
81d632
+	}
81d632
+
81d632
+	status = finalize_local_nt_token(session_info->security_token,
81d632
+					 session_info_flags);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return status;
81d632
+	}
81d632
+
81d632
+	/*
81d632
+	 * unless set otherwise, the session key is the user session
81d632
+	 * key from the auth subsystem
81d632
+	 */
81d632
+	if (user_info_dc->user_session_key.length != 0) {
81d632
+		session_info->session_key = data_blob_dup_talloc(session_info,
81d632
+						user_info_dc->user_session_key);
81d632
+		if (session_info->session_key.data == NULL) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_NO_MEMORY;
81d632
+		}
81d632
+	}
81d632
+
81d632
+	if (!(session_info_flags & AUTH_SESSION_INFO_UNIX_TOKEN)) {
81d632
+		goto done;
81d632
+	}
81d632
+
81d632
+	session_info->unix_token = talloc_zero(session_info, struct security_unix_token);
81d632
+	if (session_info->unix_token == NULL) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_NO_MEMORY;
81d632
+	}
81d632
+	session_info->unix_token->uid = -1;
81d632
+	session_info->unix_token->gid = -1;
81d632
+
81d632
+	session_info->unix_info = talloc_zero(session_info, struct auth_user_info_unix);
81d632
+	if (session_info->unix_info == NULL) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_NO_MEMORY;
81d632
+	}
81d632
+
81d632
+	/* Convert the SIDs to uid/gids. */
81d632
+
81d632
+	ids = talloc_zero_array(frame, struct unixid,
81d632
+				session_info->security_token->num_sids);
81d632
+	if (ids == NULL) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_NO_MEMORY;
81d632
+	}
81d632
+
81d632
+	if (!(hint_flags & AUTH3_UNIX_HINT_DONT_TRANSLATE_FROM_SIDS)) {
81d632
+		ok = sids_to_unixids(session_info->security_token->sids,
81d632
+				     session_info->security_token->num_sids,
81d632
+				     ids);
81d632
+		if (!ok) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_NO_MEMORY;
81d632
+		}
81d632
+	}
81d632
+
81d632
+	if (found_hint_uid) {
81d632
+		session_info->unix_token->uid = hint_uid;
81d632
+	} else if (ids[0].type == ID_TYPE_UID) {
81d632
+		/*
81d632
+		 * The primary SID resolves to a UID only.
81d632
+		 */
81d632
+		session_info->unix_token->uid = ids[0].id;
81d632
+	} else if (ids[0].type == ID_TYPE_BOTH) {
81d632
+		/*
81d632
+		 * The primary SID resolves to a UID and GID,
81d632
+		 * use it as uid and add it as first element
81d632
+		 * to the groups array.
81d632
+		 */
81d632
+		session_info->unix_token->uid = ids[0].id;
81d632
+
81d632
+		ok = add_gid_to_array_unique(session_info->unix_token,
81d632
+					     session_info->unix_token->uid,
81d632
+					     &session_info->unix_token->groups,
81d632
+					     &session_info->unix_token->ngroups);
81d632
+		if (!ok) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_NO_MEMORY;
81d632
+		}
81d632
+	} else {
81d632
+		/*
81d632
+		 * It we can't get a uid, we can't imporsonate
81d632
+		 * the user.
81d632
+		 */
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_INVALID_TOKEN;
81d632
+	}
81d632
+
81d632
+	if (found_hint_gid) {
81d632
+		session_info->unix_token->gid = hint_gid;
81d632
+	} else {
81d632
+		need_getpwuid = true;
81d632
+	}
81d632
+
81d632
+	if (hint_flags & AUTH3_UNIX_HINT_QUALIFIED_NAME) {
81d632
+		session_info->unix_info->unix_name =
81d632
+			talloc_asprintf(session_info->unix_info,
81d632
+					"%s%c%s",
81d632
+					session_info->info->domain_name,
81d632
+					*lp_winbind_separator(),
81d632
+					session_info->info->account_name);
81d632
+		if (session_info->unix_info->unix_name == NULL) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_NO_MEMORY;
81d632
+		}
81d632
+	} else if (hint_flags & AUTH3_UNIX_HINT_ISLOLATED_NAME) {
81d632
+		session_info->unix_info->unix_name =
81d632
+			talloc_strdup(session_info->unix_info,
81d632
+				      session_info->info->account_name);
81d632
+		if (session_info->unix_info->unix_name == NULL) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_NO_MEMORY;
81d632
+		}
81d632
+	} else {
81d632
+		need_getpwuid = true;
81d632
+	}
81d632
+
81d632
+	if (need_getpwuid) {
81d632
+		struct passwd *pwd = NULL;
81d632
+
81d632
+		/*
81d632
+		 * Ask the system for the primary gid
81d632
+		 * and the real unix name.
81d632
+		 */
81d632
+		pwd = getpwuid_alloc(frame, session_info->unix_token->uid);
81d632
+		if (pwd == NULL) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_INVALID_TOKEN;
81d632
+		}
81d632
+		if (!found_hint_gid) {
81d632
+			session_info->unix_token->gid = pwd->pw_gid;
81d632
+		}
81d632
+
81d632
+		session_info->unix_info->unix_name =
81d632
+			talloc_strdup(session_info->unix_info, pwd->pw_name);
81d632
+		if (session_info->unix_info->unix_name == NULL) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_NO_MEMORY;
81d632
+		}
81d632
+
81d632
+		TALLOC_FREE(pwd);
81d632
+	}
81d632
+
81d632
+	ok = add_gid_to_array_unique(session_info->unix_token,
81d632
+				     session_info->unix_token->gid,
81d632
+				     &session_info->unix_token->groups,
81d632
+				     &session_info->unix_token->ngroups);
81d632
+	if (!ok) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_NO_MEMORY;
81d632
+	}
81d632
+
81d632
+	/* This is a potentially untrusted username for use in %U */
81d632
+	alpha_strcpy(tmp, original_user_name, ". _-$", sizeof(tmp));
81d632
+	session_info->unix_info->sanitized_username =
81d632
+				talloc_strdup(session_info->unix_info, tmp);
81d632
+	if (session_info->unix_info->sanitized_username == NULL) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return NT_STATUS_NO_MEMORY;
81d632
+	}
81d632
+
81d632
+	for (i=0; i < session_info->security_token->num_sids; i++) {
81d632
+
81d632
+		if (ids[i].type != ID_TYPE_GID &&
81d632
+		    ids[i].type != ID_TYPE_BOTH) {
81d632
+			struct security_token *nt_token =
81d632
+				session_info->security_token;
81d632
+
81d632
+			DEBUG(10, ("Could not convert SID %s to gid, "
81d632
+				   "ignoring it\n",
81d632
+				   sid_string_dbg(&nt_token->sids[i])));
81d632
+			continue;
81d632
+		}
81d632
+
81d632
+		ok = add_gid_to_array_unique(session_info->unix_token,
81d632
+					     ids[i].id,
81d632
+					     &session_info->unix_token->groups,
81d632
+					     &session_info->unix_token->ngroups);
81d632
+		if (!ok) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_NO_MEMORY;
81d632
+		}
81d632
+	}
81d632
+	TALLOC_FREE(ids);
81d632
+
81d632
+	/*
81d632
+	 * Now we must get any groups this user has been
81d632
+	 * added to in /etc/group and merge them in.
81d632
+	 * This has to be done in every code path
81d632
+	 * that creates an NT token, as remote users
81d632
+	 * may have been added to the local /etc/group
81d632
+	 * database. Tokens created merely from the
81d632
+	 * info3 structs (via the DC or via the krb5 PAC)
81d632
+	 * won't have these local groups. Note the
81d632
+	 * groups added here will only be UNIX groups
81d632
+	 * (S-1-22-2-XXXX groups) as getgroups_unix_user()
81d632
+	 * turns off winbindd before calling getgroups().
81d632
+	 *
81d632
+	 * NB. This is duplicating work already
81d632
+	 * done in the 'unix_user:' case of
81d632
+	 * create_token_from_sid() but won't
81d632
+	 * do anything other than be inefficient
81d632
+	 * in that case.
81d632
+	 */
81d632
+	if (!(hint_flags & AUTH3_UNIX_HINT_DONT_EXPAND_UNIX_GROUPS)) {
81d632
+		ok = getgroups_unix_user(frame,
81d632
+					 session_info->unix_info->unix_name,
81d632
+					 session_info->unix_token->gid,
81d632
+					 &gids, &num_gids);
81d632
+		if (!ok) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_INVALID_TOKEN;
81d632
+		}
81d632
+	}
81d632
+
81d632
+	for (i=0; i < num_gids; i++) {
81d632
+
81d632
+		ok = add_gid_to_array_unique(session_info->unix_token,
81d632
+					     gids[i],
81d632
+					     &session_info->unix_token->groups,
81d632
+					     &session_info->unix_token->ngroups);
81d632
+		if (!ok) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return NT_STATUS_NO_MEMORY;
81d632
+		}
81d632
+	}
81d632
+	TALLOC_FREE(gids);
81d632
+
81d632
+	if (hint_flags & AUTH3_UNIX_HINT_DONT_TRANSLATE_TO_SIDS) {
81d632
+		/*
81d632
+		 * We should not translate the unix token uid/gids
81d632
+		 * to S-1-22-X-Y SIDs.
81d632
+		 */
81d632
+		goto done;
81d632
+	}
81d632
+
81d632
+	/*
81d632
+	 * Add the "Unix Group" SID for each gid to catch mapped groups
81d632
+	 * and their Unix equivalent.  This is to solve the backwards
81d632
+	 * compatibility problem of 'valid users = +ntadmin' where
81d632
+	 * ntadmin has been paired with "Domain Admins" in the group
81d632
+	 * mapping table.  Otherwise smb.conf would need to be changed
81d632
+	 * to 'valid user = "Domain Admins"'.  --jerry
81d632
+	 *
81d632
+	 * For consistency we also add the "Unix User" SID,
81d632
+	 * so that the complete unix token is represented within
81d632
+	 * the nt token.
81d632
+	 */
81d632
+
81d632
+	uid_to_unix_users_sid(session_info->unix_token->uid, &tmp_sid);
81d632
+	status = add_sid_to_array_unique(session_info->security_token, &tmp_sid,
81d632
+					 &session_info->security_token->sids,
81d632
+					 &session_info->security_token->num_sids);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return status;
81d632
+	}
81d632
+
81d632
+	gid_to_unix_groups_sid(session_info->unix_token->gid, &tmp_sid);
81d632
+	status = add_sid_to_array_unique(session_info->security_token, &tmp_sid,
81d632
+					 &session_info->security_token->sids,
81d632
+					 &session_info->security_token->num_sids);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return status;
81d632
+	}
81d632
+
81d632
+	for (i=0; i < session_info->unix_token->ngroups; i++ ) {
81d632
+		struct security_token *nt_token = session_info->security_token;
81d632
+
81d632
+		gid_to_unix_groups_sid(session_info->unix_token->groups[i],
81d632
+				       &tmp_sid);
81d632
+		status = add_sid_to_array_unique(nt_token->sids,
81d632
+						 &tmp_sid,
81d632
+						 &nt_token->sids,
81d632
+						 &nt_token->num_sids);
81d632
+		if (!NT_STATUS_IS_OK(status)) {
81d632
+			TALLOC_FREE(frame);
81d632
+			return status;
81d632
+		}
81d632
+	}
81d632
+
81d632
+done:
81d632
+	security_token_debug(DBGC_AUTH, 10, session_info->security_token);
81d632
+	if (session_info->unix_token != NULL) {
81d632
+		debug_unix_user_token(DBGC_AUTH, 10,
81d632
+				      session_info->unix_token->uid,
81d632
+				      session_info->unix_token->gid,
81d632
+				      session_info->unix_token->ngroups,
81d632
+				      session_info->unix_token->groups);
81d632
+	}
81d632
+
81d632
+	status = log_nt_token(session_info->security_token);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		TALLOC_FREE(frame);
81d632
+		return status;
81d632
+	}
81d632
+
81d632
+	*session_info_out = talloc_move(mem_ctx, &session_info);
81d632
+	TALLOC_FREE(frame);
81d632
+	return NT_STATUS_OK;
81d632
+}
81d632
+
81d632
 /***************************************************************************
81d632
  Make (and fill) a server_info struct from a 'struct passwd' by conversion
81d632
  to a struct samu
81d632
diff --git a/source3/auth/proto.h b/source3/auth/proto.h
81d632
index d3403f1a929..84e20093218 100644
81d632
--- a/source3/auth/proto.h
81d632
+++ b/source3/auth/proto.h
81d632
@@ -225,6 +225,38 @@ NTSTATUS create_local_token(TALLOC_CTX *mem_ctx,
81d632
 			    DATA_BLOB *session_key,
81d632
 			    const char *smb_name,
81d632
 			    struct auth_session_info **session_info_out);
81d632
+
81d632
+/*
81d632
+ * The unix name should be constructed as DOMAIN+ACCOUNT,
81d632
+ * while '+' will be the "winbind separator" character.
81d632
+ */
81d632
+#define AUTH3_UNIX_HINT_QUALIFIED_NAME             0x00000001
81d632
+/*
81d632
+ * The unix name will be just ACCOUNT
81d632
+ */
81d632
+#define AUTH3_UNIX_HINT_ISLOLATED_NAME             0x00000002
81d632
+/*
81d632
+ * Don't translate the nt token SIDS into uid/gids
81d632
+ */
81d632
+#define AUTH3_UNIX_HINT_DONT_TRANSLATE_FROM_SIDS   0x00000004
81d632
+/*
81d632
+ * Don't translate the unix token uid/gids to S-1-22-X-Y SIDS
81d632
+ */
81d632
+#define AUTH3_UNIX_HINT_DONT_TRANSLATE_TO_SIDS     0x00000008
81d632
+/*
81d632
+ * The unix token won't get expanded gid values
81d632
+ * from getgroups_unix_user()
81d632
+ */
81d632
+#define AUTH3_UNIX_HINT_DONT_EXPAND_UNIX_GROUPS    0x00000010
81d632
+NTSTATUS auth3_user_info_dc_add_hints(struct auth_user_info_dc *user_info_dc,
81d632
+				      uid_t uid,
81d632
+				      gid_t gid,
81d632
+				      uint32_t flags);
81d632
+NTSTATUS auth3_session_info_create(TALLOC_CTX *mem_ctx,
81d632
+				   const struct auth_user_info_dc *user_info_dc,
81d632
+				   const char *original_user_name,
81d632
+				   uint32_t session_info_flags,
81d632
+				   struct auth_session_info **session_info_out);
81d632
 NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username,
81d632
 				    bool is_guest,
81d632
 				    uid_t *uid, gid_t *gid,
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 92c6d4d81f801cced97adce4e5a054d226876607 Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Wed, 7 Mar 2018 00:51:51 +0100
81d632
Subject: [PATCH 15/21] s3:auth: base make_new_session_info_system() on
81d632
 auth_system_user_info_dc() and auth3_create_session_info()
81d632
81d632
The changes in the resulting token look like this:
81d632
81d632
           unix_token               : *
81d632
               unix_token: struct security_unix_token
81d632
                   uid                      : 0x0000000000000000 (0)
81d632
                   gid                      : 0x0000000000000000 (0)
81d632
-                  ngroups                  : 0x00000000 (0)
81d632
-                  groups: ARRAY(0)
81d632
+                  ngroups                  : 0x00000001 (1)
81d632
+                  groups: ARRAY(1)
81d632
+                      groups                   : 0x0000000000000000 (0)
81d632
81d632
...
81d632
81d632
                   domain_name              : *
81d632
                       domain_name              : 'NT AUTHORITY'
81d632
                   dns_domain_name          : NULL
81d632
-                  full_name                : NULL
81d632
-                  logon_script             : NULL
81d632
-                  profile_path             : NULL
81d632
-                  home_directory           : NULL
81d632
-                  home_drive               : NULL
81d632
-                  logon_server             : NULL
81d632
+                  full_name                : *
81d632
+                      full_name                : 'System'
81d632
+                  logon_script             : *
81d632
+                      logon_script             : ''
81d632
+                  profile_path             : *
81d632
+                      profile_path             : ''
81d632
+                  home_directory           : *
81d632
+                      home_directory           : ''
81d632
+                  home_drive               : *
81d632
+                      home_drive               : ''
81d632
+                  logon_server             : *
81d632
+                      logon_server             : 'SLOWSERVER'
81d632
                   last_logon               : NTTIME(0)
81d632
                   last_logoff              : NTTIME(0)
81d632
                   acct_expiry              : NTTIME(0)
81d632
                   last_password_change     : NTTIME(0)
81d632
                   allow_password_change    : NTTIME(0)
81d632
                   force_password_change    : NTTIME(0)
81d632
                   logon_count              : 0x0000 (0)
81d632
                   bad_password_count       : 0x0000 (0)
81d632
-                  acct_flags               : 0x00000000 (0)
81d632
+                  acct_flags               : 0x00000010 (16)
81d632
                   authenticated            : 0x01 (1)
81d632
           unix_info                : *
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(similar to commit e8402ec0486ced6ac2adb640c61a9e5abc77d4e4)
81d632
(cherry picked from commit 19026525a2b649f282bb11d55ae1eb5807fc4a3a)
81d632
---
81d632
 source3/auth/auth_util.c | 123 ++++++++++++++-------------------------
81d632
 1 file changed, 43 insertions(+), 80 deletions(-)
81d632
81d632
diff --git a/source3/auth/auth_util.c b/source3/auth/auth_util.c
81d632
index 9d6e8020d77..7fc3da22317 100644
81d632
--- a/source3/auth/auth_util.c
81d632
+++ b/source3/auth/auth_util.c
81d632
@@ -36,6 +36,7 @@
81d632
 #include "../librpc/gen_ndr/idmap.h"
81d632
 #include "lib/param/loadparm.h"
81d632
 #include "../lib/tsocket/tsocket.h"
81d632
+#include "source4/auth/auth.h"
81d632
 
81d632
 #undef DBGC_CLASS
81d632
 #define DBGC_CLASS DBGC_AUTH
81d632
@@ -1295,31 +1296,6 @@ done:
81d632
 	return status;
81d632
 }
81d632
 
81d632
-static NTSTATUS get_system_info3(TALLOC_CTX *mem_ctx,
81d632
-				 struct netr_SamInfo3 *info3)
81d632
-{
81d632
-	NTSTATUS status;
81d632
-
81d632
-	/* Set account name */
81d632
-	init_lsa_String(&info3->base.account_name, "SYSTEM");
81d632
-
81d632
-	/* Set domain name */
81d632
-	init_lsa_StringLarge(&info3->base.logon_domain, "NT AUTHORITY");
81d632
-
81d632
-
81d632
-	status = dom_sid_split_rid(mem_ctx, &global_sid_System,
81d632
-				   &info3->base.domain_sid,
81d632
-				   &info3->base.rid);
81d632
-	if (!NT_STATUS_IS_OK(status)) {
81d632
-		return status;
81d632
-	}
81d632
-
81d632
-	/* Primary gid is the same */
81d632
-	info3->base.primary_gid = info3->base.rid;
81d632
-
81d632
-	return NT_STATUS_OK;
81d632
-}
81d632
-
81d632
 static NTSTATUS get_guest_info3(TALLOC_CTX *mem_ctx,
81d632
 				struct netr_SamInfo3 *info3)
81d632
 {
81d632
@@ -1448,80 +1424,67 @@ done:
81d632
 static NTSTATUS make_new_session_info_system(TALLOC_CTX *mem_ctx,
81d632
 					    struct auth_session_info **session_info)
81d632
 {
81d632
+	TALLOC_CTX *frame = talloc_stackframe();
81d632
+	struct auth_user_info_dc *user_info_dc = NULL;
81d632
+	uid_t uid = -1;
81d632
+	gid_t gid = -1;
81d632
+	uint32_t hint_flags = 0;
81d632
+	uint32_t session_info_flags = 0;
81d632
 	NTSTATUS status;
81d632
-	struct auth_serversupplied_info *server_info;
81d632
-	TALLOC_CTX *tmp_ctx;
81d632
-
81d632
-	tmp_ctx = talloc_stackframe();
81d632
-	if (tmp_ctx == NULL) {
81d632
-		return NT_STATUS_NO_MEMORY;
81d632
-	}
81d632
-
81d632
-	server_info = make_server_info(tmp_ctx);
81d632
-	if (!server_info) {
81d632
-		status = NT_STATUS_NO_MEMORY;
81d632
-		DEBUG(0, ("failed making server_info\n"));
81d632
-		goto done;
81d632
-	}
81d632
 
81d632
-	server_info->info3 = talloc_zero(server_info, struct netr_SamInfo3);
81d632
-	if (!server_info->info3) {
81d632
-		status = NT_STATUS_NO_MEMORY;
81d632
-		DEBUG(0, ("talloc failed setting info3\n"));
81d632
-		goto done;
81d632
-	}
81d632
-
81d632
-	status = get_system_info3(server_info, server_info->info3);
81d632
+	status = auth_system_user_info_dc(frame, lp_netbios_name(),
81d632
+					  &user_info_dc);
81d632
 	if (!NT_STATUS_IS_OK(status)) {
81d632
-		DEBUG(0, ("Failed creating system info3 with %s\n",
81d632
+		DEBUG(0, ("auth_system_user_info_dc failed: %s\n",
81d632
 			  nt_errstr(status)));
81d632
 		goto done;
81d632
 	}
81d632
 
81d632
-	server_info->utok.uid = sec_initial_uid();
81d632
-	server_info->utok.gid = sec_initial_gid();
81d632
-	server_info->unix_name = talloc_asprintf(server_info,
81d632
-						 "NT AUTHORITY%cSYSTEM",
81d632
-						 *lp_winbind_separator());
81d632
-
81d632
-	if (!server_info->unix_name) {
81d632
-		status = NT_STATUS_NO_MEMORY;
81d632
-		DEBUG(0, ("talloc_asprintf failed setting unix_name\n"));
81d632
-		goto done;
81d632
-	}
81d632
+	/*
81d632
+	 * Just get the initial uid/gid
81d632
+	 * and don't expand the unix groups.
81d632
+	 */
81d632
+	uid = sec_initial_uid();
81d632
+	gid = sec_initial_gid();
81d632
+	hint_flags |= AUTH3_UNIX_HINT_DONT_EXPAND_UNIX_GROUPS;
81d632
 
81d632
-	server_info->security_token = talloc_zero(server_info, struct security_token);
81d632
-	if (!server_info->security_token) {
81d632
-		status = NT_STATUS_NO_MEMORY;
81d632
-		DEBUG(0, ("talloc failed setting security token\n"));
81d632
-		goto done;
81d632
-	}
81d632
+	/*
81d632
+	 * Also avoid sid mapping to gids,
81d632
+	 * as well as adding the unix_token uid/gids as
81d632
+	 * S-1-22-X-Y SIDs to the nt token.
81d632
+	 */
81d632
+	hint_flags |= AUTH3_UNIX_HINT_DONT_TRANSLATE_FROM_SIDS;
81d632
+	hint_flags |= AUTH3_UNIX_HINT_DONT_TRANSLATE_TO_SIDS;
81d632
 
81d632
-	status = add_sid_to_array_unique(server_info->security_token->sids,
81d632
-					 &global_sid_System,
81d632
-					 &server_info->security_token->sids,
81d632
-					 &server_info->security_token->num_sids);
81d632
+	/*
81d632
+	 * The unix name will be "NT AUTHORITY+SYSTEM",
81d632
+	 * where '+' is the "winbind separator" character.
81d632
+	 */
81d632
+	hint_flags |= AUTH3_UNIX_HINT_QUALIFIED_NAME;
81d632
+	status = auth3_user_info_dc_add_hints(user_info_dc,
81d632
+					      uid,
81d632
+					      gid,
81d632
+					      hint_flags);
81d632
 	if (!NT_STATUS_IS_OK(status)) {
81d632
+		DEBUG(0, ("auth3_user_info_dc_add_hints failed: %s\n",
81d632
+			  nt_errstr(status)));
81d632
 		goto done;
81d632
 	}
81d632
 
81d632
-	/* SYSTEM has all privilages */
81d632
-	server_info->security_token->privilege_mask = ~0;
81d632
-
81d632
-	/* Now turn the server_info into a session_info with the full token etc */
81d632
-	status = create_local_token(mem_ctx, server_info, NULL, "SYSTEM", session_info);
81d632
-	talloc_free(server_info);
81d632
-
81d632
+	session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
81d632
+	session_info_flags |= AUTH_SESSION_INFO_UNIX_TOKEN;
81d632
+	status = auth3_session_info_create(mem_ctx, user_info_dc,
81d632
+					   user_info_dc->info->account_name,
81d632
+					   session_info_flags,
81d632
+					   session_info);
81d632
 	if (!NT_STATUS_IS_OK(status)) {
81d632
-		DEBUG(0, ("create_local_token failed: %s\n",
81d632
+		DEBUG(0, ("auth3_session_info_create failed: %s\n",
81d632
 			  nt_errstr(status)));
81d632
 		goto done;
81d632
 	}
81d632
 
81d632
-	talloc_steal(mem_ctx, *session_info);
81d632
-
81d632
 done:
81d632
-	TALLOC_FREE(tmp_ctx);
81d632
+	TALLOC_FREE(frame);
81d632
 	return status;
81d632
 }
81d632
 
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From c8e19cd979f18eba054b51664d2206493ed8d5e2 Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Fri, 2 Mar 2018 17:07:11 +0100
81d632
Subject: [PATCH 16/21] s3:auth: pass the whole auth_session_info from
81d632
 copy_session_info_serverinfo_guest() to create_local_token()
81d632
81d632
We only need to adjust sanitized_username in order to keep the same behaviour.
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit a2a289d0446fedb4ea40834b5b5b190fdca30906)
81d632
(cherry picked from commit c3fdc6157377e71cf354fae5b59b823a4ebaa0eb)
81d632
---
81d632
 source3/auth/auth_util.c | 51 +++++++++++++++++-----------------------
81d632
 source3/include/auth.h   |  5 ++--
81d632
 2 files changed, 23 insertions(+), 33 deletions(-)
81d632
81d632
diff --git a/source3/auth/auth_util.c b/source3/auth/auth_util.c
81d632
index 7fc3da22317..a151ac13724 100644
81d632
--- a/source3/auth/auth_util.c
81d632
+++ b/source3/auth/auth_util.c
81d632
@@ -500,6 +500,26 @@ NTSTATUS create_local_token(TALLOC_CTX *mem_ctx,
81d632
 		return NT_STATUS_LOGON_FAILURE;
81d632
 	}
81d632
 
81d632
+	if (server_info->cached_session_info != NULL) {
81d632
+		session_info = copy_session_info(mem_ctx,
81d632
+				server_info->cached_session_info);
81d632
+		if (session_info == NULL) {
81d632
+			return NT_STATUS_NO_MEMORY;
81d632
+		}
81d632
+
81d632
+		/* This is a potentially untrusted username for use in %U */
81d632
+		alpha_strcpy(tmp, smb_username, ". _-$", sizeof(tmp));
81d632
+		session_info->unix_info->sanitized_username =
81d632
+				talloc_strdup(session_info->unix_info, tmp);
81d632
+		if (session_info->unix_info->sanitized_username == NULL) {
81d632
+			TALLOC_FREE(session_info);
81d632
+			return NT_STATUS_NO_MEMORY;
81d632
+		}
81d632
+
81d632
+		*session_info_out = session_info;
81d632
+		return NT_STATUS_OK;
81d632
+	}
81d632
+
81d632
 	session_info = talloc_zero(mem_ctx, struct auth_session_info);
81d632
 	if (!session_info) {
81d632
 		return NT_STATUS_NO_MEMORY;
81d632
@@ -554,30 +574,6 @@ NTSTATUS create_local_token(TALLOC_CTX *mem_ctx,
81d632
 		return status;
81d632
 	}
81d632
 
81d632
-	if (server_info->security_token) {
81d632
-		/* Just copy the token, it has already been finalised
81d632
-		 * (nasty hack to support a cached guest/system session_info
81d632
-		 */
81d632
-
81d632
-		session_info->security_token = dup_nt_token(session_info, server_info->security_token);
81d632
-		if (!session_info->security_token) {
81d632
-			TALLOC_FREE(session_info);
81d632
-			return NT_STATUS_NO_MEMORY;
81d632
-		}
81d632
-
81d632
-		session_info->unix_token->ngroups = server_info->utok.ngroups;
81d632
-		if (server_info->utok.ngroups != 0) {
81d632
-			session_info->unix_token->groups = (gid_t *)talloc_memdup(
81d632
-				session_info->unix_token, server_info->utok.groups,
81d632
-				sizeof(gid_t)*session_info->unix_token->ngroups);
81d632
-		} else {
81d632
-			session_info->unix_token->groups = NULL;
81d632
-		}
81d632
-
81d632
-		*session_info_out = session_info;
81d632
-		return NT_STATUS_OK;
81d632
-	}
81d632
-
81d632
 	/*
81d632
 	 * If winbind is not around, we can not make much use of the SIDs the
81d632
 	 * domain controller provided us with. Likewise if the user name was
81d632
@@ -1586,12 +1582,6 @@ static struct auth_serversupplied_info *copy_session_info_serverinfo_guest(TALLO
81d632
 	 * to take the wrong path */
81d632
 	SMB_ASSERT(src->security_token);
81d632
 
81d632
-	dst->security_token = dup_nt_token(dst, src->security_token);
81d632
-	if (!dst->security_token) {
81d632
-		TALLOC_FREE(dst);
81d632
-		return NULL;
81d632
-	}
81d632
-
81d632
 	dst->session_key = data_blob_talloc( dst, src->session_key.data,
81d632
 						src->session_key.length);
81d632
 
81d632
@@ -1612,6 +1602,7 @@ static struct auth_serversupplied_info *copy_session_info_serverinfo_guest(TALLO
81d632
 		return NULL;
81d632
 	}
81d632
 
81d632
+	dst->cached_session_info = src;
81d632
 	return dst;
81d632
 }
81d632
 
81d632
diff --git a/source3/include/auth.h b/source3/include/auth.h
81d632
index d3055373964..31a1f201835 100644
81d632
--- a/source3/include/auth.h
81d632
+++ b/source3/include/auth.h
81d632
@@ -34,15 +34,14 @@ struct auth_serversupplied_info {
81d632
 	struct security_unix_token utok;
81d632
 
81d632
 	/*
81d632
-	 * NT group information taken from the info3 structure
81d632
+	 * A complete auth_session_info
81d632
 	 *
81d632
 	 * This is not normally filled in, during the typical
81d632
 	 * authentication process.  If filled in, it has already been
81d632
 	 * finalised by a nasty hack to support a cached guest/system
81d632
 	 * session_info
81d632
 	 */
81d632
-
81d632
-	struct security_token *security_token;
81d632
+	const struct auth_session_info *cached_session_info;
81d632
 
81d632
 	/* These are the intermediate session keys, as provided by a
81d632
 	 * NETLOGON server and used by NTLMSSP to negotiate key
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 86475067dbe32ea21081d67115035a62b9802e1c Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Fri, 2 Mar 2018 14:39:44 +0100
81d632
Subject: [PATCH 17/21] s3:auth: add make_{server,session}_info_anonymous()
81d632
81d632
It's important to have them separated from make_{server,session}_info_guest(),
81d632
because there's a fundamental difference between anonymous (the client requested
81d632
no authentication) and guest (the server lies about the authentication failure).
81d632
81d632
The following is the difference between guest and anonymous token:
81d632
81d632
             security_token: struct security_token
81d632
-                num_sids                 : 0x0000000a (10)
81d632
-                sids: ARRAY(10)
81d632
-                    sids                     : S-1-5-21-3793881525-3372187982-3724979742-501
81d632
-                    sids                     : S-1-5-21-3793881525-3372187982-3724979742-514
81d632
-                    sids                     : S-1-22-2-65534
81d632
-                    sids                     : S-1-22-2-65533
81d632
+                num_sids                 : 0x00000009 (9)
81d632
+                sids: ARRAY(9)
81d632
+                    sids                     : S-1-5-7
81d632
                     sids                     : S-1-1-0
81d632
                     sids                     : S-1-5-2
81d632
-                    sids                     : S-1-5-32-546
81d632
                     sids                     : S-1-22-1-65533
81d632
+                    sids                     : S-1-22-2-65534
81d632
+                    sids                     : S-1-22-2-100004
81d632
                     sids                     : S-1-22-2-100002
81d632
                     sids                     : S-1-22-2-100003
81d632
+                    sids                     : S-1-22-2-65533
81d632
                 privilege_mask           : 0x0000000000000000 (0)
81d632
81d632
...
81d632
81d632
         unix_token               : *
81d632
             unix_token: struct security_unix_token
81d632
                 uid                      : 0x000000000000fffd (65533)
81d632
                 gid                      : 0x000000000000fffe (65534)
81d632
-                ngroups                  : 0x00000004 (4)
81d632
-                groups: ARRAY(4)
81d632
+                ngroups                  : 0x00000005 (5)
81d632
+                groups: ARRAY(5)
81d632
                     groups                   : 0x000000000000fffe (65534)
81d632
-                    groups                   : 0x000000000000fffd (65533)
81d632
+                    groups                   : 0x00000000000186a4 (100004)
81d632
                     groups                   : 0x00000000000186a2 (100002)
81d632
                     groups                   : 0x00000000000186a3 (100003)
81d632
+                    groups                   : 0x000000000000fffd (65533)
81d632
81d632
             info: struct auth_user_info
81d632
                 account_name             : *
81d632
-                    account_name             : 'nobody'
81d632
+                    account_name             : 'ANONYMOUS LOGON'
81d632
                 user_principal_name      : NULL
81d632
                 user_principal_constructed: 0x00 (0)
81d632
                 domain_name              : *
81d632
-                    domain_name              : 'SAMBA-TEST'
81d632
+                    domain_name              : 'NT AUTHORITY'
81d632
                 dns_domain_name          : NULL
81d632
-                full_name                : NULL
81d632
-                logon_script             : NULL
81d632
-                profile_path             : NULL
81d632
-                home_directory           : NULL
81d632
-                home_drive               : NULL
81d632
-                logon_server             : NULL
81d632
+                full_name                : *
81d632
+                    full_name                : 'Anonymous Logon'
81d632
+                logon_script             : *
81d632
+                    logon_script             : ''
81d632
+                profile_path             : *
81d632
+                    profile_path             : ''
81d632
+                home_directory           : *
81d632
+                    home_directory           : ''
81d632
+                home_drive               : *
81d632
+                    home_drive               : ''
81d632
+                logon_server             : *
81d632
+                    logon_server             : 'LOCALNT4DC2'
81d632
                 last_logon               : NTTIME(0)
81d632
                 last_logoff              : NTTIME(0)
81d632
                 acct_expiry              : NTTIME(0)
81d632
                 last_password_change     : NTTIME(0)
81d632
                 allow_password_change    : NTTIME(0)
81d632
                 force_password_change    : NTTIME(0)
81d632
                 logon_count              : 0x0000 (0)
81d632
                 bad_password_count       : 0x0000 (0)
81d632
-                acct_flags               : 0x00000000 (0)
81d632
+                acct_flags               : 0x00000010 (16)
81d632
                 authenticated            : 0x00 (0)
81d632
             security_token: struct security_token
81d632
                 num_sids                 : 0x00000006 (6)
81d632
                 sids: ARRAY(6)
81d632
+                    sids                     : S-1-5-7
81d632
+                    sids                     : S-1-1-0
81d632
+                    sids                     : S-1-5-2
81d632
                     sids                     : S-1-22-1-65533
81d632
                     sids                     : S-1-22-2-65534
81d632
                     sids                     : S-1-22-2-65533
81d632
-                    sids                     : S-1-1-0
81d632
-                    sids                     : S-1-5-2
81d632
-                    sids                     : S-1-5-32-546
81d632
                 privilege_mask           : 0x0000000000000000 (0)
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
81d632
(similar to commit 6afb6b67a198c88ab8fa3fee931729c43605716d)
81d632
81d632
(cherry picked from commit 8f69498ab6fa85dc3d23a1453224a654a9bedead)
81d632
---
81d632
 source3/auth/auth_util.c | 143 ++++++++++++++++++++++++++++++++++++++-
81d632
 source3/auth/proto.h     |   4 ++
81d632
 2 files changed, 146 insertions(+), 1 deletion(-)
81d632
81d632
diff --git a/source3/auth/auth_util.c b/source3/auth/auth_util.c
81d632
index a151ac13724..a1dde2cc7be 100644
81d632
--- a/source3/auth/auth_util.c
81d632
+++ b/source3/auth/auth_util.c
81d632
@@ -1484,6 +1484,87 @@ done:
81d632
 	return status;
81d632
 }
81d632
 
81d632
+static NTSTATUS make_new_session_info_anonymous(TALLOC_CTX *mem_ctx,
81d632
+					struct auth_session_info **session_info)
81d632
+{
81d632
+	TALLOC_CTX *frame = talloc_stackframe();
81d632
+	const char *guest_account = lp_guest_account();
81d632
+	struct auth_user_info_dc *user_info_dc = NULL;
81d632
+	struct passwd *pwd = NULL;
81d632
+	uint32_t hint_flags = 0;
81d632
+	uint32_t session_info_flags = 0;
81d632
+	NTSTATUS status;
81d632
+
81d632
+	/*
81d632
+	 * We use the guest account for the unix token
81d632
+	 * while we use a true anonymous nt token.
81d632
+	 *
81d632
+	 * It's very important to have a separate
81d632
+	 * nt token for anonymous.
81d632
+	 */
81d632
+
81d632
+	pwd = Get_Pwnam_alloc(frame, guest_account);
81d632
+	if (pwd == NULL) {
81d632
+		DBG_ERR("Unable to locate guest account [%s]!\n",
81d632
+			guest_account);
81d632
+		status = NT_STATUS_NO_SUCH_USER;
81d632
+		goto done;
81d632
+	}
81d632
+
81d632
+	status = auth_anonymous_user_info_dc(frame, lp_netbios_name(),
81d632
+					     &user_info_dc);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		DEBUG(0, ("auth_anonymous_user_info_dc failed: %s\n",
81d632
+			  nt_errstr(status)));
81d632
+		goto done;
81d632
+	}
81d632
+
81d632
+	/*
81d632
+	 * Note we don't pass AUTH3_UNIX_HINT_QUALIFIED_NAME
81d632
+	 * nor AUTH3_UNIX_HINT_ISOLATED_NAME here
81d632
+	 * as we want the unix name be found by getpwuid_alloc().
81d632
+	 */
81d632
+
81d632
+	status = auth3_user_info_dc_add_hints(user_info_dc,
81d632
+					      pwd->pw_uid,
81d632
+					      pwd->pw_gid,
81d632
+					      hint_flags);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		DEBUG(0, ("auth3_user_info_dc_add_hints failed: %s\n",
81d632
+			  nt_errstr(status)));
81d632
+		goto done;
81d632
+	}
81d632
+
81d632
+	/*
81d632
+	 * In future we may want to remove
81d632
+	 * AUTH_SESSION_INFO_DEFAULT_GROUPS.
81d632
+	 *
81d632
+	 * Similar to Windows with EveryoneIncludesAnonymous
81d632
+	 * and RestrictAnonymous.
81d632
+	 *
81d632
+	 * We may introduce AUTH_SESSION_INFO_ANON_WORLD...
81d632
+	 *
81d632
+	 * But for this is required to keep the existing tests
81d632
+	 * working.
81d632
+	 */
81d632
+	session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
81d632
+	session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
81d632
+	session_info_flags |= AUTH_SESSION_INFO_UNIX_TOKEN;
81d632
+	status = auth3_session_info_create(mem_ctx, user_info_dc,
81d632
+					   "",
81d632
+					   session_info_flags,
81d632
+					   session_info);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		DEBUG(0, ("auth3_session_info_create failed: %s\n",
81d632
+			  nt_errstr(status)));
81d632
+		goto done;
81d632
+	}
81d632
+
81d632
+done:
81d632
+	TALLOC_FREE(frame);
81d632
+	return status;
81d632
+}
81d632
+
81d632
 /****************************************************************************
81d632
   Fake a auth_session_info just from a username (as a
81d632
   session_info structure, with create_local_token() already called on
81d632
@@ -1661,15 +1742,30 @@ bool session_info_set_session_key(struct auth_session_info *info,
81d632
 }
81d632
 
81d632
 static struct auth_session_info *guest_info = NULL;
81d632
+static struct auth_session_info *anonymous_info = NULL;
81d632
 
81d632
 static struct auth_serversupplied_info *guest_server_info = NULL;
81d632
 
81d632
 bool init_guest_info(void)
81d632
 {
81d632
+	NTSTATUS status;
81d632
+
81d632
 	if (guest_info != NULL)
81d632
 		return true;
81d632
 
81d632
-	return NT_STATUS_IS_OK(make_new_session_info_guest(&guest_info, &guest_server_info));
81d632
+	status = make_new_session_info_guest(&guest_info,
81d632
+					     &guest_server_info);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		return false;
81d632
+	}
81d632
+
81d632
+	status = make_new_session_info_anonymous(NULL,
81d632
+						 &anonymous_info);
81d632
+	if (!NT_STATUS_IS_OK(status)) {
81d632
+		return false;
81d632
+	}
81d632
+
81d632
+	return true;
81d632
 }
81d632
 
81d632
 NTSTATUS make_server_info_guest(TALLOC_CTX *mem_ctx,
81d632
@@ -1690,6 +1786,51 @@ NTSTATUS make_session_info_guest(TALLOC_CTX *mem_ctx,
81d632
 	return (*session_info != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
81d632
 }
81d632
 
81d632
+NTSTATUS make_server_info_anonymous(TALLOC_CTX *mem_ctx,
81d632
+				    struct auth_serversupplied_info **server_info)
81d632
+{
81d632
+	if (anonymous_info == NULL) {
81d632
+		return NT_STATUS_UNSUCCESSFUL;
81d632
+	}
81d632
+
81d632
+	/*
81d632
+	 * This is trickier than it would appear to need to be because
81d632
+	 * we are trying to avoid certain costly operations when the
81d632
+	 * structure is converted to a 'auth_session_info' again in
81d632
+	 * create_local_token()
81d632
+	 *
81d632
+	 * We use a guest server_info, but with the anonymous session info,
81d632
+	 * which means create_local_token() will return a copy
81d632
+	 * of the anonymous token.
81d632
+	 *
81d632
+	 * The server info is just used as legacy in order to
81d632
+	 * keep existing code working. Maybe some debug messages
81d632
+	 * will still refer to guest instead of anonymous.
81d632
+	 */
81d632
+	*server_info = copy_session_info_serverinfo_guest(mem_ctx, anonymous_info,
81d632
+							  guest_server_info);
81d632
+	if (*server_info == NULL) {
81d632
+		return NT_STATUS_NO_MEMORY;
81d632
+	}
81d632
+
81d632
+	return NT_STATUS_OK;
81d632
+}
81d632
+
81d632
+NTSTATUS make_session_info_anonymous(TALLOC_CTX *mem_ctx,
81d632
+				     struct auth_session_info **session_info)
81d632
+{
81d632
+	if (anonymous_info == NULL) {
81d632
+		return NT_STATUS_UNSUCCESSFUL;
81d632
+	}
81d632
+
81d632
+	*session_info = copy_session_info(mem_ctx, anonymous_info);
81d632
+	if (*session_info == NULL) {
81d632
+		return NT_STATUS_NO_MEMORY;
81d632
+	}
81d632
+
81d632
+	return NT_STATUS_OK;
81d632
+}
81d632
+
81d632
 static struct auth_session_info *system_info = NULL;
81d632
 
81d632
 NTSTATUS init_system_session_info(void)
81d632
diff --git a/source3/auth/proto.h b/source3/auth/proto.h
81d632
index 84e20093218..0ce34742ab6 100644
81d632
--- a/source3/auth/proto.h
81d632
+++ b/source3/auth/proto.h
81d632
@@ -284,6 +284,10 @@ NTSTATUS make_server_info_guest(TALLOC_CTX *mem_ctx,
81d632
 				struct auth_serversupplied_info **server_info);
81d632
 NTSTATUS make_session_info_guest(TALLOC_CTX *mem_ctx,
81d632
 				struct auth_session_info **server_info);
81d632
+NTSTATUS make_server_info_anonymous(TALLOC_CTX *mem_ctx,
81d632
+				    struct auth_serversupplied_info **server_info);
81d632
+NTSTATUS make_session_info_anonymous(TALLOC_CTX *mem_ctx,
81d632
+				     struct auth_session_info **psession_info);
81d632
 NTSTATUS make_session_info_system(TALLOC_CTX *mem_ctx,
81d632
 				 struct auth_session_info **session_info);
81d632
 const struct auth_session_info *get_session_info_system(void);
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 001dcfa09cbe00feaed7be6355e63cd44d4d7cfd Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Fri, 2 Mar 2018 14:40:19 +0100
81d632
Subject: [PATCH 18/21] s3:rpc_server: make use of
81d632
 make_session_info_anonymous()
81d632
81d632
For unauthenticated connections we should default to a
81d632
session info with an anonymous nt token.
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
(cherry picked from commit 0ee9a550944034718ea188b277cca4b6fc5fbc5c)
81d632
(cherry picked from commit 47b13364bed551fb9480ff8ac500d6251fae7b72)
81d632
---
81d632
 source3/rpc_server/rpc_server.c | 9 +++------
81d632
 1 file changed, 3 insertions(+), 6 deletions(-)
81d632
81d632
diff --git a/source3/rpc_server/rpc_server.c b/source3/rpc_server/rpc_server.c
81d632
index e15cd205cdc..4f196dec76e 100644
81d632
--- a/source3/rpc_server/rpc_server.c
81d632
+++ b/source3/rpc_server/rpc_server.c
81d632
@@ -1104,14 +1104,11 @@ void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
81d632
 	}
81d632
 
81d632
 	if (ncacn_conn->session_info == NULL) {
81d632
-		/*
81d632
-		 * TODO: use auth_anonymous_session_info() here?
81d632
-		 */
81d632
-		status = make_session_info_guest(ncacn_conn,
81d632
-						 &ncacn_conn->session_info);
81d632
+		status = make_session_info_anonymous(ncacn_conn,
81d632
+						     &ncacn_conn->session_info);
81d632
 		if (!NT_STATUS_IS_OK(status)) {
81d632
 			DEBUG(2, ("Failed to create "
81d632
-				  "make_session_info_guest - %s\n",
81d632
+				  "make_session_info_anonymous - %s\n",
81d632
 				  nt_errstr(status)));
81d632
 			talloc_free(ncacn_conn);
81d632
 			return;
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 825ec4ad86285315a5ff3285c33ca7c876dc18a8 Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Fri, 2 Mar 2018 14:40:19 +0100
81d632
Subject: [PATCH 19/21] s3:auth: make use of
81d632
 make_{server,session}_info_anonymous()
81d632
MIME-Version: 1.0
81d632
Content-Type: text/plain; charset=UTF-8
81d632
Content-Transfer-Encoding: 8bit
81d632
81d632
It's important to have them separated from make_{server,session}_info_guest(),
81d632
because there's a fundamental difference between anonymous (the client requested
81d632
no authentication) and guest (the server lies about the authentication failure).
81d632
81d632
When it's really an anonymous connection, we should reflect that in the
81d632
resulting session info.
81d632
81d632
This should fix a problem where Windows 10 tries to join
81d632
a Samba hosted NT4 domain and has SMB2/3 enabled.
81d632
81d632
We no longer return SMB_SETUP_GUEST or SMB2_SESSION_FLAG_IS_GUEST
81d632
for true anonymous connections.
81d632
81d632
The commit message from a few commit before shows the resulting
81d632
auth_session_info change.
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13328
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Ralph Boehme <slow@samba.org>
81d632
81d632
Autobuild-User(master): Ralph Böhme <slow@samba.org>
81d632
Autobuild-Date(master): Fri Mar 16 03:03:31 CET 2018 on sn-devel-144
81d632
81d632
(cherry picked from commit 1957bf11f127fc08c6622999cadc7dd580ac7d3b)
81d632
(cherry picked from commit 6c1dde631da2f5b41682210eca40f9d363168696)
81d632
---
81d632
 selftest/knownfail.d/anonymous-guest | 1 -
81d632
 source3/auth/auth_builtin.c          | 2 +-
81d632
 source3/auth/auth_ntlmssp.c          | 5 +----
81d632
 3 files changed, 2 insertions(+), 6 deletions(-)
81d632
 delete mode 100644 selftest/knownfail.d/anonymous-guest
81d632
81d632
diff --git a/selftest/knownfail.d/anonymous-guest b/selftest/knownfail.d/anonymous-guest
81d632
deleted file mode 100644
81d632
index a134cece3d5..00000000000
81d632
--- a/selftest/knownfail.d/anonymous-guest
81d632
+++ /dev/null
81d632
@@ -1 +0,0 @@
81d632
-^samba3.smbtorture_s3.*nt4_dc.*.SMB2-ANONYMOUS.smbtorture
81d632
diff --git a/source3/auth/auth_builtin.c b/source3/auth/auth_builtin.c
81d632
index 0fa95d9f16d..a2d95a77330 100644
81d632
--- a/source3/auth/auth_builtin.c
81d632
+++ b/source3/auth/auth_builtin.c
81d632
@@ -81,7 +81,7 @@ static NTSTATUS check_guest_security(const struct auth_context *auth_context,
81d632
 		break;
81d632
 	}
81d632
 
81d632
-	return make_server_info_guest(NULL, server_info);
81d632
+	return make_server_info_anonymous(NULL, server_info);
81d632
 }
81d632
 
81d632
 /* Guest modules initialisation */
81d632
diff --git a/source3/auth/auth_ntlmssp.c b/source3/auth/auth_ntlmssp.c
81d632
index fd629fd9a03..2e345e17571 100644
81d632
--- a/source3/auth/auth_ntlmssp.c
81d632
+++ b/source3/auth/auth_ntlmssp.c
81d632
@@ -65,10 +65,7 @@ NTSTATUS auth3_generate_session_info(struct auth4_context *auth_context,
81d632
 
81d632
 		cmp = dom_sid_compare(sid, &global_sid_Anonymous);
81d632
 		if (cmp == 0) {
81d632
-			/*
81d632
-			 * TODO: use auth_anonymous_session_info() here?
81d632
-			 */
81d632
-			return make_session_info_guest(mem_ctx, session_info);
81d632
+			return make_session_info_anonymous(mem_ctx, session_info);
81d632
 		}
81d632
 
81d632
 		return NT_STATUS_INTERNAL_ERROR;
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 48646ffe1c60854d832c80f42c1236e43d5b1fb9 Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Tue, 9 Jan 2018 08:55:48 +0100
81d632
Subject: [PATCH 20/21] s3:libsmb: allow -U"\\administrator" to work
81d632
81d632
cli_credentials_get_principal() returns NULL in that case.
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13206
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
81d632
(cherry picked from commit 0786a65cabb92a812cf1c692d0d26914f74a6f87)
81d632
(cherry picked from commit 4c087a0e9e8ffd797e810f7dc21d630fd6833eed)
81d632
---
81d632
 source3/libsmb/cliconnect.c | 7 ++++++-
81d632
 1 file changed, 6 insertions(+), 1 deletion(-)
81d632
81d632
diff --git a/source3/libsmb/cliconnect.c b/source3/libsmb/cliconnect.c
81d632
index 70bcead445e..d819e4c62f2 100644
81d632
--- a/source3/libsmb/cliconnect.c
81d632
+++ b/source3/libsmb/cliconnect.c
81d632
@@ -283,8 +283,9 @@ NTSTATUS cli_session_creds_prepare_krb5(struct cli_state *cli,
81d632
 
81d632
 	auth_requested = cli_credentials_authentication_requested(creds);
81d632
 	if (auth_requested) {
81d632
+		errno = 0;
81d632
 		user_principal = cli_credentials_get_principal(creds, frame);
81d632
-		if (user_principal == NULL) {
81d632
+		if (errno != 0) {
81d632
 			TALLOC_FREE(frame);
81d632
 			return NT_STATUS_NO_MEMORY;
81d632
 		}
81d632
@@ -299,6 +300,10 @@ NTSTATUS cli_session_creds_prepare_krb5(struct cli_state *cli,
81d632
 		try_kerberos = true;
81d632
 	}
81d632
 
81d632
+	if (user_principal == NULL) {
81d632
+		try_kerberos = false;
81d632
+	}
81d632
+
81d632
 	if (target_hostname == NULL) {
81d632
 		try_kerberos = false;
81d632
 	} else if (is_ipaddress(target_hostname)) {
81d632
-- 
81d632
2.17.0
81d632
81d632
81d632
From 38c3a25e80d7dfdef3edf330117a43a1acded21d Mon Sep 17 00:00:00 2001
81d632
From: Stefan Metzmacher <metze@samba.org>
81d632
Date: Tue, 9 Jan 2018 08:57:05 +0100
81d632
Subject: [PATCH 21/21] s3:cliconnect.c: remove useless ';'
81d632
81d632
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13206
81d632
81d632
Signed-off-by: Stefan Metzmacher <metze@samba.org>
81d632
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
81d632
(cherry picked from commit e039e9b0d2a16b21ace019b028e5c8244486b8a3)
81d632
(cherry picked from commit 04cc8936c3f90bf3bbb05bce25c55212c8f0823b)
81d632
---
81d632
 source3/libsmb/cliconnect.c | 2 +-
81d632
 1 file changed, 1 insertion(+), 1 deletion(-)
81d632
81d632
diff --git a/source3/libsmb/cliconnect.c b/source3/libsmb/cliconnect.c
81d632
index d819e4c62f2..8c815659c80 100644
81d632
--- a/source3/libsmb/cliconnect.c
81d632
+++ b/source3/libsmb/cliconnect.c
81d632
@@ -1289,7 +1289,7 @@ static struct tevent_req *cli_session_setup_spnego_send(
81d632
 
81d632
 	status = cli_session_creds_prepare_krb5(cli, creds);
81d632
 	if (tevent_req_nterror(req, status)) {
81d632
-		return tevent_req_post(req, ev);;
81d632
+		return tevent_req_post(req, ev);
81d632
 	}
81d632
 
81d632
 	subreq = cli_session_setup_gensec_send(state, ev, cli, creds,
81d632
-- 
81d632
2.17.0
81d632