r10392: Merges from SAMBA_3_0 for 3.0.20a
[Samba.git] / source / lib / util_seaccess.c
blob73fc45c844d432cb7af43bf5ef439c468de3b8e2
1 /*
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 extern NT_USER_TOKEN anonymous_token;
26 /*********************************************************************************
27 Check an ACE against a SID. We return the remaining needed permission
28 bits not yet granted. Zero means permission allowed (no more needed bits).
29 **********************************************************************************/
31 static uint32 check_ace(SEC_ACE *ace, const NT_USER_TOKEN *token, uint32 acc_desired,
32 NTSTATUS *status)
34 uint32 mask = ace->info.mask;
37 * Inherit only is ignored.
40 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
41 return acc_desired;
45 * If this ACE has no SID in common with the token,
46 * ignore it as it cannot be used to make an access
47 * determination.
50 if (!token_sid_in_ace( token, ace))
51 return acc_desired;
53 switch (ace->type) {
54 case SEC_ACE_TYPE_ACCESS_ALLOWED:
56 * This is explicitly allowed.
57 * Remove the bits from the remaining
58 * access required. Return the remaining
59 * bits needed.
61 acc_desired &= ~mask;
62 break;
63 case SEC_ACE_TYPE_ACCESS_DENIED:
65 * This is explicitly denied.
66 * If any bits match terminate here,
67 * we are denied.
69 if (acc_desired & mask) {
70 *status = NT_STATUS_ACCESS_DENIED;
71 return 0xFFFFFFFF;
73 break;
74 case SEC_ACE_TYPE_SYSTEM_ALARM:
75 case SEC_ACE_TYPE_SYSTEM_AUDIT:
76 *status = NT_STATUS_NOT_IMPLEMENTED;
77 return 0xFFFFFFFF;
78 default:
79 *status = NT_STATUS_INVALID_PARAMETER;
80 return 0xFFFFFFFF;
83 return acc_desired;
86 /*********************************************************************************
87 Maximum access was requested. Calculate the max possible. Fail if it doesn't
88 include other bits requested.
89 **********************************************************************************/
91 static BOOL get_max_access( SEC_ACL *the_acl, const NT_USER_TOKEN *token, uint32 *granted,
92 uint32 desired,
93 NTSTATUS *status)
95 uint32 acc_denied = 0;
96 uint32 acc_granted = 0;
97 size_t i;
99 for ( i = 0 ; i < the_acl->num_aces; i++) {
100 SEC_ACE *ace = &the_acl->ace[i];
101 uint32 mask = ace->info.mask;
103 if (!token_sid_in_ace( token, ace))
104 continue;
106 switch (ace->type) {
107 case SEC_ACE_TYPE_ACCESS_ALLOWED:
108 acc_granted |= (mask & ~acc_denied);
109 break;
110 case SEC_ACE_TYPE_ACCESS_DENIED:
111 acc_denied |= (mask & ~acc_granted);
112 break;
113 case SEC_ACE_TYPE_SYSTEM_ALARM:
114 case SEC_ACE_TYPE_SYSTEM_AUDIT:
115 *status = NT_STATUS_NOT_IMPLEMENTED;
116 *granted = 0;
117 return False;
118 default:
119 *status = NT_STATUS_INVALID_PARAMETER;
120 *granted = 0;
121 return False;
126 * If we were granted no access, or we desired bits that we
127 * didn't get, then deny.
130 if ((acc_granted == 0) || ((acc_granted & desired) != desired)) {
131 *status = NT_STATUS_ACCESS_DENIED;
132 *granted = 0;
133 return False;
137 * Return the access we did get.
140 *granted = acc_granted;
141 *status = NT_STATUS_OK;
142 return True;
145 /* Map generic access rights to object specific rights. This technique is
146 used to give meaning to assigning read, write, execute and all access to
147 objects. Each type of object has its own mapping of generic to object
148 specific access rights. */
150 void se_map_generic(uint32 *access_mask, struct generic_mapping *mapping)
152 uint32 old_mask = *access_mask;
154 if (*access_mask & GENERIC_READ_ACCESS) {
155 *access_mask &= ~GENERIC_READ_ACCESS;
156 *access_mask |= mapping->generic_read;
159 if (*access_mask & GENERIC_WRITE_ACCESS) {
160 *access_mask &= ~GENERIC_WRITE_ACCESS;
161 *access_mask |= mapping->generic_write;
164 if (*access_mask & GENERIC_EXECUTE_ACCESS) {
165 *access_mask &= ~GENERIC_EXECUTE_ACCESS;
166 *access_mask |= mapping->generic_execute;
169 if (*access_mask & GENERIC_ALL_ACCESS) {
170 *access_mask &= ~GENERIC_ALL_ACCESS;
171 *access_mask |= mapping->generic_all;
174 if (old_mask != *access_mask) {
175 DEBUG(10, ("se_map_generic(): mapped mask 0x%08x to 0x%08x\n",
176 old_mask, *access_mask));
180 /* Map standard access rights to object specific rights. This technique is
181 used to give meaning to assigning read, write, execute and all access to
182 objects. Each type of object has its own mapping of standard to object
183 specific access rights. */
185 void se_map_standard(uint32 *access_mask, struct standard_mapping *mapping)
187 uint32 old_mask = *access_mask;
189 if (*access_mask & READ_CONTROL_ACCESS) {
190 *access_mask &= ~READ_CONTROL_ACCESS;
191 *access_mask |= mapping->std_read;
194 if (*access_mask & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS)) {
195 *access_mask &= ~(DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS);
196 *access_mask |= mapping->std_all;
199 if (old_mask != *access_mask) {
200 DEBUG(10, ("se_map_standard(): mapped mask 0x%08x to 0x%08x\n",
201 old_mask, *access_mask));
205 /*****************************************************************************
206 Check access rights of a user against a security descriptor. Look at
207 each ACE in the security descriptor until an access denied ACE denies
208 any of the desired rights to the user or any of the users groups, or one
209 or more ACEs explicitly grant all requested access rights. See
210 "Access-Checking" document in MSDN.
211 *****************************************************************************/
213 BOOL se_access_check(const SEC_DESC *sd, const NT_USER_TOKEN *token,
214 uint32 acc_desired, uint32 *acc_granted,
215 NTSTATUS *status)
217 size_t i;
218 SEC_ACL *the_acl;
219 fstring sid_str;
220 uint32 tmp_acc_desired = acc_desired;
222 if (!status || !acc_granted)
223 return False;
225 if (!token)
226 token = &anonymous_token;
228 *status = NT_STATUS_OK;
229 *acc_granted = 0;
231 DEBUG(10,("se_access_check: requested access 0x%08x, for NT token with %u entries and first sid %s.\n",
232 (unsigned int)acc_desired, (unsigned int)token->num_sids,
233 sid_to_string(sid_str, &token->user_sids[0])));
236 * No security descriptor or security descriptor with no DACL
237 * present allows all access.
240 /* ACL must have something in it */
242 if (!sd || (sd && (!(sd->type & SEC_DESC_DACL_PRESENT) || sd->dacl == NULL))) {
243 *status = NT_STATUS_OK;
244 *acc_granted = acc_desired;
245 DEBUG(5, ("se_access_check: no sd or blank DACL, access allowed\n"));
246 return True;
249 /* The user sid is the first in the token */
250 if (DEBUGLVL(3)) {
251 DEBUG(3, ("se_access_check: user sid is %s\n", sid_to_string(sid_str, &token->user_sids[PRIMARY_USER_SID_INDEX]) ));
253 for (i = 1; i < token->num_sids; i++) {
254 DEBUGADD(3, ("se_access_check: also %s\n",
255 sid_to_string(sid_str, &token->user_sids[i])));
259 /* Is the token the owner of the SID ? */
261 if (sd->owner_sid) {
262 for (i = 0; i < token->num_sids; i++) {
263 if (sid_equal(&token->user_sids[i], sd->owner_sid)) {
265 * The owner always has SEC_RIGHTS_WRITE_DAC & READ_CONTROL.
267 if (tmp_acc_desired & WRITE_DAC_ACCESS)
268 tmp_acc_desired &= ~WRITE_DAC_ACCESS;
269 if (tmp_acc_desired & READ_CONTROL_ACCESS)
270 tmp_acc_desired &= ~READ_CONTROL_ACCESS;
275 the_acl = sd->dacl;
277 if (tmp_acc_desired & MAXIMUM_ALLOWED_ACCESS) {
278 tmp_acc_desired &= ~MAXIMUM_ALLOWED_ACCESS;
279 return get_max_access( the_acl, token, acc_granted, tmp_acc_desired,
280 status);
283 for ( i = 0 ; i < the_acl->num_aces && tmp_acc_desired != 0; i++) {
284 SEC_ACE *ace = &the_acl->ace[i];
286 DEBUGADD(10,("se_access_check: ACE %u: type %d, flags = 0x%02x, SID = %s mask = %x, current desired = %x\n",
287 (unsigned int)i, ace->type, ace->flags,
288 sid_to_string(sid_str, &ace->trustee),
289 (unsigned int) ace->info.mask,
290 (unsigned int)tmp_acc_desired ));
292 tmp_acc_desired = check_ace( ace, token, tmp_acc_desired, status);
293 if (NT_STATUS_V(*status)) {
294 *acc_granted = 0;
295 DEBUG(5,("se_access_check: ACE %u denied with status %s.\n", (unsigned int)i, nt_errstr(*status)));
296 return False;
301 * If there are no more desired permissions left then
302 * access was allowed.
305 if (tmp_acc_desired == 0) {
306 *acc_granted = acc_desired;
307 *status = NT_STATUS_OK;
308 DEBUG(5,("se_access_check: access (%x) granted.\n", (unsigned int)acc_desired ));
309 return True;
312 *acc_granted = 0;
313 *status = NT_STATUS_ACCESS_DENIED;
314 DEBUG(5,("se_access_check: access (%x) denied.\n", (unsigned int)acc_desired ));
315 return False;
319 /*******************************************************************
320 samr_make_sam_obj_sd
321 ********************************************************************/
323 NTSTATUS samr_make_sam_obj_sd(TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size)
325 DOM_SID adm_sid;
326 DOM_SID act_sid;
328 SEC_ACE ace[3];
329 SEC_ACCESS mask;
331 SEC_ACL *psa = NULL;
333 sid_copy(&adm_sid, &global_sid_Builtin);
334 sid_append_rid(&adm_sid, BUILTIN_ALIAS_RID_ADMINS);
336 sid_copy(&act_sid, &global_sid_Builtin);
337 sid_append_rid(&act_sid, BUILTIN_ALIAS_RID_ACCOUNT_OPS);
339 /*basic access for every one*/
340 init_sec_access(&mask, GENERIC_RIGHTS_SAM_EXECUTE | GENERIC_RIGHTS_SAM_READ);
341 init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
343 /*full access for builtin aliases Administrators and Account Operators*/
344 init_sec_access(&mask, GENERIC_RIGHTS_SAM_ALL_ACCESS);
345 init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
346 init_sec_ace(&ace[2], &act_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
348 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
349 return NT_STATUS_NO_MEMORY;
351 if ((*psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, sd_size)) == NULL)
352 return NT_STATUS_NO_MEMORY;
354 return NT_STATUS_OK;