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