s4:auth Avoid doing database lookups for NT AUTHORITY users
[Samba.git] / source4 / auth / session.c
blobbd1be8eebbb95ec1650600fe1f75fe46700f86f2
1 /*
2 Unix SMB/CIFS implementation.
3 Authentication utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Andrew Bartlett 2001-2010
6 Copyright (C) Jeremy Allison 2000-2001
7 Copyright (C) Rafal Szczesniak 2002
8 Copyright (C) Stefan Metzmacher 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "auth/auth.h"
26 #include "libcli/security/security.h"
27 #include "libcli/auth/libcli_auth.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "auth/session_proto.h"
31 _PUBLIC_ struct auth_session_info *anonymous_session(TALLOC_CTX *mem_ctx,
32 struct loadparm_context *lp_ctx)
34 NTSTATUS nt_status;
35 struct auth_session_info *session_info = NULL;
36 nt_status = auth_anonymous_session_info(mem_ctx, lp_ctx, &session_info);
37 if (!NT_STATUS_IS_OK(nt_status)) {
38 return NULL;
40 return session_info;
43 _PUBLIC_ NTSTATUS auth_generate_session_info(TALLOC_CTX *mem_ctx,
44 struct auth_context *auth_context, /* Optional if the domain SID is in the NT AUTHORITY domain */
45 struct auth_serversupplied_info *server_info,
46 uint32_t session_info_flags,
47 struct auth_session_info **_session_info)
49 struct auth_session_info *session_info;
50 NTSTATUS nt_status;
51 unsigned int i, num_groupSIDs = 0;
52 const char *account_sid_string;
53 const char *account_sid_dn;
54 DATA_BLOB account_sid_blob;
55 const char *primary_group_string;
56 const char *primary_group_dn;
57 DATA_BLOB primary_group_blob;
59 const char *filter;
61 struct dom_sid **groupSIDs = NULL;
62 const struct dom_sid *dom_sid, *anonymous_sid, *system_sid;
64 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
65 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
67 /* For now, we don't have trusted domains, so we do a very
68 * simple check to see that the user's SID is in *this*
69 * domain, and then trust the user account control. When we
70 * get trusted domains, we should check it's a trusted domain
71 * in this forest. This elaborate check is to try and avoid a
72 * nasty security bug if we forget about this later... */
74 session_info = talloc(tmp_ctx, struct auth_session_info);
75 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(session_info, tmp_ctx);
77 session_info->server_info = talloc_reference(session_info, server_info);
79 /* unless set otherwise, the session key is the user session
80 * key from the auth subsystem */
81 session_info->session_key = server_info->user_session_key;
83 anonymous_sid = dom_sid_parse_talloc(tmp_ctx, SID_NT_ANONYMOUS);
84 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(anonymous_sid, tmp_ctx);
86 system_sid = dom_sid_parse_talloc(tmp_ctx, SID_NT_SYSTEM);
87 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(system_sid, tmp_ctx);
89 if (dom_sid_equal(anonymous_sid, server_info->account_sid)) {
90 /* Don't expand nested groups of system, anonymous etc*/
91 } else if (dom_sid_equal(system_sid, server_info->account_sid)) {
92 /* Don't expand nested groups of system, anonymous etc*/
93 } else if (auth_context) {
94 if (server_info->acct_flags & ACB_SVRTRUST) {
95 dom_sid = samdb_domain_sid(auth_context->sam_ctx);
96 if (dom_sid) {
97 if (dom_sid_in_domain(dom_sid, server_info->account_sid)) {
98 session_info_flags |= AUTH_SESSION_INFO_ENTERPRISE_DC;
99 } else {
100 DEBUG(2, ("DC %s is not in our domain. "
101 "It will not have Enterprise Domain Controllers membership on this server",
102 server_info->account_name));
104 } else {
105 DEBUG(2, ("Could not obtain local domain SID, "
106 "so can not determine if DC %s is a DC of this domain. "
107 "It will not have Enterprise Domain Controllers membership",
108 server_info->account_name));
112 groupSIDs = talloc_array(tmp_ctx, struct dom_sid *, server_info->n_domain_groups);
113 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs, tmp_ctx);
114 if (!groupSIDs) {
115 talloc_free(tmp_ctx);
116 return NT_STATUS_NO_MEMORY;
119 num_groupSIDs = server_info->n_domain_groups;
121 for (i=0; i < server_info->n_domain_groups; i++) {
122 groupSIDs[i] = server_info->domain_groups[i];
125 filter = talloc_asprintf(tmp_ctx, "(&(objectClass=group)(groupType:1.2.840.113556.1.4.803:=%u))",
126 GROUP_TYPE_BUILTIN_LOCAL_GROUP);
128 /* Search for each group in the token */
130 /* Expands the account SID - this function takes in
131 * memberOf-like values, so we fake one up with the
132 * <SID=S-...> format of DN and then let it expand
133 * them, as long as they meet the filter - so only
134 * builtin groups
136 * We already have the primary group in the token, so set
137 * 'only childs' flag to true
139 account_sid_string = dom_sid_string(tmp_ctx, server_info->account_sid);
140 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(account_sid_string, server_info);
142 account_sid_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", account_sid_string);
143 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(account_sid_dn, server_info);
145 account_sid_blob = data_blob_string_const(account_sid_dn);
147 nt_status = authsam_expand_nested_groups(auth_context->sam_ctx, &account_sid_blob, true, filter,
148 tmp_ctx, &groupSIDs, &num_groupSIDs);
149 if (!NT_STATUS_IS_OK(nt_status)) {
150 talloc_free(tmp_ctx);
151 return nt_status;
154 /* Expands the primary group - this function takes in
155 * memberOf-like values, so we fake one up with the
156 * <SID=S-...> format of DN and then let it expand
157 * them, as long as they meet the filter - so only
158 * builtin groups
160 * We already have the primary group in the token, so set
161 * 'only childs' flag to true
163 primary_group_string = dom_sid_string(tmp_ctx, server_info->primary_group_sid);
164 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_string, server_info);
166 primary_group_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", primary_group_string);
167 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_dn, server_info);
169 primary_group_blob = data_blob_string_const(primary_group_dn);
171 nt_status = authsam_expand_nested_groups(auth_context->sam_ctx, &primary_group_blob, true, filter,
172 tmp_ctx, &groupSIDs, &num_groupSIDs);
173 if (!NT_STATUS_IS_OK(nt_status)) {
174 talloc_free(tmp_ctx);
175 return nt_status;
178 for (i = 0; i < server_info->n_domain_groups; i++) {
179 char *group_string;
180 const char *group_dn;
181 DATA_BLOB group_blob;
183 group_string = dom_sid_string(tmp_ctx,
184 server_info->domain_groups[i]);
185 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(group_string, server_info);
187 group_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", group_string);
188 talloc_free(group_string);
189 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(group_dn, server_info);
190 group_blob = data_blob_string_const(group_dn);
192 /* This function takes in memberOf values and expands
193 * them, as long as they meet the filter - so only
194 * builtin groups */
195 nt_status = authsam_expand_nested_groups(auth_context->sam_ctx, &group_blob, true, filter,
196 tmp_ctx, &groupSIDs, &num_groupSIDs);
197 if (!NT_STATUS_IS_OK(nt_status)) {
198 talloc_free(tmp_ctx);
199 return nt_status;
204 nt_status = security_token_create(session_info,
205 auth_context ? auth_context->event_ctx : NULL,
206 auth_context ? auth_context->lp_ctx : NULL,
207 server_info->account_sid,
208 server_info->primary_group_sid,
209 num_groupSIDs,
210 groupSIDs,
211 session_info_flags,
212 &session_info->security_token);
213 NT_STATUS_NOT_OK_RETURN_AND_FREE(nt_status, tmp_ctx);
215 session_info->credentials = NULL;
217 talloc_steal(mem_ctx, session_info);
218 *_session_info = session_info;
219 talloc_free(tmp_ctx);
220 return NT_STATUS_OK;
224 * prints a struct auth_session_info security token to debug output.
226 void auth_session_info_debug(int dbg_lev,
227 const struct auth_session_info *session_info)
229 if (!session_info) {
230 DEBUG(dbg_lev, ("Session Info: (NULL)\n"));
231 return;
234 security_token_debug(dbg_lev, session_info->security_token);