2 Unix SMB/CIFS implementation.
3 Copyright (C) Luke Kenneth Casson Leighton 1996-2000.
4 Copyright (C) Tim Potter 2000.
5 Copyright (C) Re-written by Jeremy Allison 2000.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 extern NT_USER_TOKEN anonymous_token;
25 /*********************************************************************************
26 Check an ACE against a SID. We return the remaining needed permission
27 bits not yet granted. Zero means permission allowed (no more needed bits).
28 **********************************************************************************/
30 static uint32 check_ace(SEC_ACE *ace, const NT_USER_TOKEN *token, uint32 acc_desired,
33 uint32 mask = ace->access_mask;
36 * Inherit only is ignored.
39 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
44 * If this ACE has no SID in common with the token,
45 * ignore it as it cannot be used to make an access
49 if (!token_sid_in_ace( token, ace))
53 case SEC_ACE_TYPE_ACCESS_ALLOWED:
55 * This is explicitly allowed.
56 * Remove the bits from the remaining
57 * access required. Return the remaining
62 case SEC_ACE_TYPE_ACCESS_DENIED:
64 * This is explicitly denied.
65 * If any bits match terminate here,
68 if (acc_desired & mask) {
69 *status = NT_STATUS_ACCESS_DENIED;
73 case SEC_ACE_TYPE_SYSTEM_ALARM:
74 case SEC_ACE_TYPE_SYSTEM_AUDIT:
75 *status = NT_STATUS_NOT_IMPLEMENTED;
78 *status = NT_STATUS_INVALID_PARAMETER;
85 /*********************************************************************************
86 Maximum access was requested. Calculate the max possible. Fail if it doesn't
87 include other bits requested.
88 **********************************************************************************/
90 static bool get_max_access( SEC_ACL *the_acl, const NT_USER_TOKEN *token, uint32 *granted,
94 uint32 acc_denied = 0;
95 uint32 acc_granted = 0;
98 for ( i = 0 ; i < the_acl->num_aces; i++) {
99 SEC_ACE *ace = &the_acl->aces[i];
100 uint32 mask = ace->access_mask;
102 if (!token_sid_in_ace( token, ace))
106 case SEC_ACE_TYPE_ACCESS_ALLOWED:
107 acc_granted |= (mask & ~acc_denied);
109 case SEC_ACE_TYPE_ACCESS_DENIED:
110 acc_denied |= (mask & ~acc_granted);
112 case SEC_ACE_TYPE_SYSTEM_ALARM:
113 case SEC_ACE_TYPE_SYSTEM_AUDIT:
114 *status = NT_STATUS_NOT_IMPLEMENTED;
118 *status = NT_STATUS_INVALID_PARAMETER;
125 * If we were granted no access, or we desired bits that we
126 * didn't get, then deny.
129 if ((acc_granted == 0) || ((acc_granted & desired) != desired)) {
130 *status = NT_STATUS_ACCESS_DENIED;
136 * Return the access we did get.
139 *granted = acc_granted;
140 *status = NT_STATUS_OK;
144 /* Map generic access rights to object specific rights. This technique is
145 used to give meaning to assigning read, write, execute and all access to
146 objects. Each type of object has its own mapping of generic to object
147 specific access rights. */
149 void se_map_generic(uint32 *access_mask, const struct generic_mapping *mapping)
151 uint32 old_mask = *access_mask;
153 if (*access_mask & GENERIC_READ_ACCESS) {
154 *access_mask &= ~GENERIC_READ_ACCESS;
155 *access_mask |= mapping->generic_read;
158 if (*access_mask & GENERIC_WRITE_ACCESS) {
159 *access_mask &= ~GENERIC_WRITE_ACCESS;
160 *access_mask |= mapping->generic_write;
163 if (*access_mask & GENERIC_EXECUTE_ACCESS) {
164 *access_mask &= ~GENERIC_EXECUTE_ACCESS;
165 *access_mask |= mapping->generic_execute;
168 if (*access_mask & GENERIC_ALL_ACCESS) {
169 *access_mask &= ~GENERIC_ALL_ACCESS;
170 *access_mask |= mapping->generic_all;
173 if (old_mask != *access_mask) {
174 DEBUG(10, ("se_map_generic(): mapped mask 0x%08x to 0x%08x\n",
175 old_mask, *access_mask));
179 /* Map generic access rights to object specific rights for all the ACE's
183 void security_acl_map_generic(struct security_acl *sa,
184 const struct generic_mapping *mapping)
192 for (i = 0; i < sa->num_aces; i++) {
193 se_map_generic(&sa->aces[i].access_mask, mapping);
197 /* Map standard access rights to object specific rights. This technique is
198 used to give meaning to assigning read, write, execute and all access to
199 objects. Each type of object has its own mapping of standard to object
200 specific access rights. */
202 void se_map_standard(uint32 *access_mask, struct standard_mapping *mapping)
204 uint32 old_mask = *access_mask;
206 if (*access_mask & READ_CONTROL_ACCESS) {
207 *access_mask &= ~READ_CONTROL_ACCESS;
208 *access_mask |= mapping->std_read;
211 if (*access_mask & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS)) {
212 *access_mask &= ~(DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS);
213 *access_mask |= mapping->std_all;
216 if (old_mask != *access_mask) {
217 DEBUG(10, ("se_map_standard(): mapped mask 0x%08x to 0x%08x\n",
218 old_mask, *access_mask));
222 /*****************************************************************************
223 Check access rights of a user against a security descriptor. Look at
224 each ACE in the security descriptor until an access denied ACE denies
225 any of the desired rights to the user or any of the users groups, or one
226 or more ACEs explicitly grant all requested access rights. See
227 "Access-Checking" document in MSDN.
228 *****************************************************************************/
230 bool se_access_check(const SEC_DESC *sd, const NT_USER_TOKEN *token,
231 uint32 acc_desired, uint32 *acc_granted,
236 uint32 tmp_acc_desired = acc_desired;
238 if (!status || !acc_granted)
242 token = &anonymous_token;
244 *status = NT_STATUS_OK;
247 DEBUG(10,("se_access_check: requested access 0x%08x, for NT token "
248 "with %u entries and first sid %s.\n",
249 (unsigned int)acc_desired, (unsigned int)token->num_sids,
250 sid_string_dbg(&token->user_sids[0])));
253 * No security descriptor or security descriptor with no DACL
254 * present allows all access.
257 /* ACL must have something in it */
259 if (!sd || (sd && (!(sd->type & SEC_DESC_DACL_PRESENT) || sd->dacl == NULL))) {
260 *status = NT_STATUS_OK;
261 *acc_granted = acc_desired;
262 DEBUG(5, ("se_access_check: no sd or blank DACL, access allowed\n"));
266 /* The user sid is the first in the token */
268 DEBUG(3, ("se_access_check: user sid is %s\n",
270 &token->user_sids[PRIMARY_USER_SID_INDEX])));
272 for (i = 1; i < token->num_sids; i++) {
273 DEBUGADD(3, ("se_access_check: also %s\n",
274 sid_string_dbg(&token->user_sids[i])));
278 /* Is the token the owner of the SID ? */
281 for (i = 0; i < token->num_sids; i++) {
282 if (sid_equal(&token->user_sids[i], sd->owner_sid)) {
284 * The owner always has SEC_RIGHTS_WRITE_DAC & READ_CONTROL.
286 if (tmp_acc_desired & WRITE_DAC_ACCESS)
287 tmp_acc_desired &= ~WRITE_DAC_ACCESS;
288 if (tmp_acc_desired & READ_CONTROL_ACCESS)
289 tmp_acc_desired &= ~READ_CONTROL_ACCESS;
296 if (tmp_acc_desired & MAXIMUM_ALLOWED_ACCESS) {
297 tmp_acc_desired &= ~MAXIMUM_ALLOWED_ACCESS;
298 return get_max_access( the_acl, token, acc_granted, tmp_acc_desired,
302 for ( i = 0 ; i < the_acl->num_aces && tmp_acc_desired != 0; i++) {
303 SEC_ACE *ace = &the_acl->aces[i];
305 DEBUGADD(10,("se_access_check: ACE %u: type %d, flags = "
306 "0x%02x, SID = %s mask = %x, current desired "
307 "= %x\n", (unsigned int)i, ace->type, ace->flags,
308 sid_string_dbg(&ace->trustee),
309 (unsigned int) ace->access_mask,
310 (unsigned int)tmp_acc_desired ));
312 tmp_acc_desired = check_ace( ace, token, tmp_acc_desired, status);
313 if (NT_STATUS_V(*status)) {
315 DEBUG(5,("se_access_check: ACE %u denied with status %s.\n", (unsigned int)i, nt_errstr(*status)));
321 * If there are no more desired permissions left then
322 * access was allowed.
325 if (tmp_acc_desired == 0) {
326 *acc_granted = acc_desired;
327 *status = NT_STATUS_OK;
328 DEBUG(5,("se_access_check: access (%x) granted.\n", (unsigned int)acc_desired ));
333 *status = NT_STATUS_ACCESS_DENIED;
334 DEBUG(5,("se_access_check: access (%x) denied.\n", (unsigned int)acc_desired ));
339 /*******************************************************************
341 ********************************************************************/
343 NTSTATUS samr_make_sam_obj_sd(TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size)
353 sid_copy(&adm_sid, &global_sid_Builtin);
354 sid_append_rid(&adm_sid, BUILTIN_ALIAS_RID_ADMINS);
356 sid_copy(&act_sid, &global_sid_Builtin);
357 sid_append_rid(&act_sid, BUILTIN_ALIAS_RID_ACCOUNT_OPS);
359 /*basic access for every one*/
360 init_sec_access(&mask, GENERIC_RIGHTS_SAM_EXECUTE | GENERIC_RIGHTS_SAM_READ);
361 init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
363 /*full access for builtin aliases Administrators and Account Operators*/
364 init_sec_access(&mask, GENERIC_RIGHTS_SAM_ALL_ACCESS);
365 init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
366 init_sec_ace(&ace[2], &act_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
368 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
369 return NT_STATUS_NO_MEMORY;
371 if ((*psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
372 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
373 psa, sd_size)) == NULL)
374 return NT_STATUS_NO_MEMORY;