2 Unix SMB/CIFS implementation.
4 interface functions for the sam database
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Volker Lendecke 2004
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
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/>.
25 #include "librpc/gen_ndr/ndr_netlogon.h"
26 #include "librpc/gen_ndr/ndr_security.h"
27 #include "lib/events/events.h"
28 #include "lib/ldb-samba/ldb_wrap.h"
30 #include <ldb_errors.h>
31 #include "libcli/security/security.h"
32 #include "libcli/security/claims-conversions.h"
33 #include "libcli/auth/libcli_auth.h"
34 #include "libcli/ldap/ldap_ndr.h"
35 #include "system/time.h"
36 #include "system/filesys.h"
38 #include "../lib/util/util_ldb.h"
39 #include "dsdb/samdb/samdb.h"
40 #include "../libds/common/flags.h"
41 #include "param/param.h"
42 #include "lib/events/events.h"
43 #include "auth/credentials/credentials.h"
44 #include "param/secrets.h"
45 #include "auth/auth.h"
46 #include "lib/tsocket/tsocket.h"
47 #include "lib/param/loadparm.h"
50 connect to the SAM database specified by URL
51 return an opaque context pointer on success, or NULL on failure
53 int samdb_connect_url(TALLOC_CTX
*mem_ctx
,
54 struct tevent_context
*ev_ctx
,
55 struct loadparm_context
*lp_ctx
,
56 struct auth_session_info
*session_info
,
59 const struct tsocket_address
*remote_address
,
60 struct ldb_context
**ldb_ret
,
63 struct ldb_context
*ldb
= NULL
;
68 /* We create sam.ldb in provision, and never anywhere else */
69 flags
|= LDB_FLG_DONT_CREATE_DB
;
71 if (remote_address
== NULL
) {
72 ldb
= ldb_wrap_find(url
, ev_ctx
, lp_ctx
,
73 session_info
, NULL
, flags
);
75 *ldb_ret
= talloc_reference(mem_ctx
, ldb
);
76 if (*ldb_ret
== NULL
) {
77 return LDB_ERR_OPERATIONS_ERROR
;
83 ldb
= samba_ldb_init(mem_ctx
, ev_ctx
, lp_ctx
, session_info
, NULL
);
86 *errstring
= talloc_asprintf(mem_ctx
,
87 "Failed to set up Samba ldb "
88 "wrappers with samba_ldb_init() "
91 return LDB_ERR_OPERATIONS_ERROR
;
94 dsdb_set_global_schema(ldb
);
96 ret
= samba_ldb_connect(ldb
, lp_ctx
, url
, flags
);
97 if (ret
!= LDB_SUCCESS
) {
98 *errstring
= talloc_asprintf(mem_ctx
,
99 "Failed to connect to %s: %s",
103 return LDB_ERR_OPERATIONS_ERROR
;
107 * If a remote_address was specified, then set it on the DB
108 * and do not add to the wrap list (as we need to keep the LDB
109 * pointer unique for the address).
111 * We use this for audit logging and for the "netlogon" attribute
113 if (remote_address
!= NULL
) {
114 ldb_set_opaque(ldb
, "remoteAddress",
115 discard_const(remote_address
));
120 if (!ldb_wrap_add(url
, ev_ctx
, lp_ctx
, session_info
, NULL
, flags
, ldb
)) {
121 *errstring
= talloc_asprintf(mem_ctx
,
122 "Failed to add cached DB reference"
126 return LDB_ERR_OPERATIONS_ERROR
;
135 connect to the SAM database
136 return an opaque context pointer on success, or NULL on failure
138 struct ldb_context
*samdb_connect(TALLOC_CTX
*mem_ctx
,
139 struct tevent_context
*ev_ctx
,
140 struct loadparm_context
*lp_ctx
,
141 struct auth_session_info
*session_info
,
142 const struct tsocket_address
*remote_address
,
146 struct ldb_context
*ldb
;
147 int ret
= samdb_connect_url(mem_ctx
,
156 if (ret
== LDB_SUCCESS
) {
162 /****************************************************************************
163 Create the SID list for this user.
164 ****************************************************************************/
165 NTSTATUS
security_token_create(TALLOC_CTX
*mem_ctx
,
166 struct loadparm_context
*lp_ctx
,
168 const struct auth_SidAttr
*sids
,
169 uint32_t num_device_sids
,
170 const struct auth_SidAttr
*device_sids
,
171 struct auth_claims auth_claims
,
172 uint32_t session_info_flags
,
173 struct security_token
**token
)
175 struct security_token
*ptoken
;
178 enum claims_evaluation_control evaluate_claims
;
179 bool sids_are_valid
= false;
180 bool device_sids_are_valid
= false;
181 bool authentication_was_compounded
= session_info_flags
& AUTH_SESSION_INFO_FORCE_COMPOUNDED_AUTHENTICATION
;
184 * Some special-case callers can't supply the lp_ctx, but do
185 * not interact with claims or conditional ACEs
187 if (lp_ctx
== NULL
) {
188 evaluate_claims
= CLAIMS_EVALUATION_INVALID_STATE
;
190 enum acl_claims_evaluation claims_evaultion_setting
191 = lpcfg_acl_claims_evaluation(lp_ctx
);
194 * We are well inside the AD DC, so we do not need to check
195 * the server role etc
197 switch (claims_evaultion_setting
) {
198 case ACL_CLAIMS_EVALUATION_AD_DC_ONLY
:
199 evaluate_claims
= CLAIMS_EVALUATION_ALWAYS
;
202 evaluate_claims
= CLAIMS_EVALUATION_NEVER
;
206 ptoken
= security_token_initialise(mem_ctx
, evaluate_claims
);
207 NT_STATUS_HAVE_NO_MEMORY(ptoken
);
209 if (num_sids
> UINT32_MAX
- 6) {
211 return NT_STATUS_INVALID_PARAMETER
;
213 ptoken
->sids
= talloc_array(ptoken
, struct dom_sid
, num_sids
+ 6 /* over-allocate */);
214 if (ptoken
->sids
== NULL
) {
216 return NT_STATUS_NO_MEMORY
;
219 ptoken
->num_sids
= 0;
221 for (i
= 0; i
< num_sids
; i
++) {
222 uint32_t check_sid_idx
;
223 for (check_sid_idx
= 0;
224 check_sid_idx
< ptoken
->num_sids
;
226 if (dom_sid_equal(&ptoken
->sids
[check_sid_idx
], &sids
[i
].sid
)) {
231 if (check_sid_idx
== ptoken
->num_sids
) {
232 const struct dom_sid
*sid
= &sids
[i
].sid
;
234 sids_are_valid
= sids_are_valid
|| dom_sid_equal(
235 sid
, &global_sid_Claims_Valid
);
236 authentication_was_compounded
= authentication_was_compounded
|| dom_sid_equal(
237 sid
, &global_sid_Compounded_Authentication
);
239 ptoken
->sids
= talloc_realloc(ptoken
, ptoken
->sids
, struct dom_sid
, ptoken
->num_sids
+ 1);
240 if (ptoken
->sids
== NULL
) {
242 return NT_STATUS_NO_MEMORY
;
245 ptoken
->sids
[ptoken
->num_sids
] = *sid
;
250 if (authentication_was_compounded
&& num_device_sids
) {
251 ptoken
->device_sids
= talloc_array(ptoken
, struct dom_sid
, num_device_sids
);
252 if (ptoken
->device_sids
== NULL
) {
254 return NT_STATUS_NO_MEMORY
;
256 for (i
= 0; i
< num_device_sids
; i
++) {
257 uint32_t check_sid_idx
;
258 for (check_sid_idx
= 0;
259 check_sid_idx
< ptoken
->num_device_sids
;
261 if (dom_sid_equal(&ptoken
->device_sids
[check_sid_idx
], &device_sids
[i
].sid
)) {
266 if (check_sid_idx
== ptoken
->num_device_sids
) {
267 const struct dom_sid
*device_sid
= &device_sids
[i
].sid
;
269 device_sids_are_valid
= device_sids_are_valid
|| dom_sid_equal(
270 device_sid
, &global_sid_Claims_Valid
);
272 ptoken
->device_sids
= talloc_realloc(ptoken
,
275 ptoken
->num_device_sids
+ 1);
276 if (ptoken
->device_sids
== NULL
) {
278 return NT_STATUS_NO_MEMORY
;
281 ptoken
->device_sids
[ptoken
->num_device_sids
] = *device_sid
;
282 ptoken
->num_device_sids
++;
287 /* The caller may have requested simple privileges, for example if there isn't a local DB */
288 if (session_info_flags
& AUTH_SESSION_INFO_SIMPLE_PRIVILEGES
) {
289 /* Shortcuts to prevent recursion and avoid lookups */
290 if (ptoken
->sids
== NULL
) {
291 ptoken
->privilege_mask
= 0;
292 } else if (security_token_is_system(ptoken
)) {
293 ptoken
->privilege_mask
= ~0;
294 } else if (security_token_is_anonymous(ptoken
)) {
295 ptoken
->privilege_mask
= 0;
296 } else if (security_token_has_builtin_administrators(ptoken
)) {
297 ptoken
->privilege_mask
= ~0;
299 /* All other 'users' get a empty priv set so far */
300 ptoken
->privilege_mask
= 0;
303 /* setup the privilege mask for this token */
304 status
= samdb_privilege_setup(lp_ctx
, ptoken
);
305 if (!NT_STATUS_IS_OK(status
)) {
307 DEBUG(1,("Unable to access privileges database\n"));
313 * TODO: we might want to regard ‘session_info_flags’ for the device
314 * SIDs as well as for the client SIDs.
317 if (sids_are_valid
) {
318 status
= claims_data_security_claims(ptoken
,
319 auth_claims
.user_claims
,
320 &ptoken
->user_claims
,
321 &ptoken
->num_user_claims
);
322 if (!NT_STATUS_IS_OK(status
)) {
328 if (device_sids_are_valid
&& authentication_was_compounded
) {
329 status
= claims_data_security_claims(ptoken
,
330 auth_claims
.device_claims
,
331 &ptoken
->device_claims
,
332 &ptoken
->num_device_claims
);
333 if (!NT_STATUS_IS_OK(status
)) {
339 security_token_debug(0, 10, ptoken
);