split some security related functions in their own files.
[Samba/bb.git] / source3 / lib / util_seaccess.c
blobcb0f46e2f9d7a847c6369579f62f4d52794c932d
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 DOM_SID global_sid_Builtin;
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 extern NT_USER_TOKEN anonymous_token;
218 size_t i;
219 SEC_ACL *the_acl;
220 fstring sid_str;
221 uint32 tmp_acc_desired = acc_desired;
223 if (!status || !acc_granted)
224 return False;
226 if (!token)
227 token = &anonymous_token;
229 *status = NT_STATUS_OK;
230 *acc_granted = 0;
232 DEBUG(10,("se_access_check: requested access 0x%08x, for NT token with %u entries and first sid %s.\n",
233 (unsigned int)acc_desired, (unsigned int)token->num_sids,
234 sid_to_string(sid_str, &token->user_sids[0])));
237 * No security descriptor or security descriptor with no DACL
238 * present allows all access.
241 /* ACL must have something in it */
243 if (!sd || (sd && (!(sd->type & SEC_DESC_DACL_PRESENT) || sd->dacl == NULL))) {
244 *status = NT_STATUS_OK;
245 *acc_granted = acc_desired;
246 DEBUG(5, ("se_access_check: no sd or blank DACL, access allowed\n"));
247 return True;
250 /* The user sid is the first in the token */
251 if (DEBUGLVL(3)) {
252 DEBUG(3, ("se_access_check: user sid is %s\n", sid_to_string(sid_str, &token->user_sids[PRIMARY_USER_SID_INDEX]) ));
254 for (i = 1; i < token->num_sids; i++) {
255 DEBUGADD(3, ("se_access_check: also %s\n",
256 sid_to_string(sid_str, &token->user_sids[i])));
260 /* Is the token the owner of the SID ? */
262 if (sd->owner_sid) {
263 for (i = 0; i < token->num_sids; i++) {
264 if (sid_equal(&token->user_sids[i], sd->owner_sid)) {
266 * The owner always has SEC_RIGHTS_WRITE_DAC & READ_CONTROL.
268 if (tmp_acc_desired & WRITE_DAC_ACCESS)
269 tmp_acc_desired &= ~WRITE_DAC_ACCESS;
270 if (tmp_acc_desired & READ_CONTROL_ACCESS)
271 tmp_acc_desired &= ~READ_CONTROL_ACCESS;
276 the_acl = sd->dacl;
278 if (tmp_acc_desired & MAXIMUM_ALLOWED_ACCESS) {
279 tmp_acc_desired &= ~MAXIMUM_ALLOWED_ACCESS;
280 return get_max_access( the_acl, token, acc_granted, tmp_acc_desired,
281 status);
284 for ( i = 0 ; i < the_acl->num_aces && tmp_acc_desired != 0; i++) {
285 SEC_ACE *ace = &the_acl->ace[i];
287 DEBUGADD(10,("se_access_check: ACE %u: type %d, flags = 0x%02x, SID = %s mask = %x, current desired = %x\n",
288 (unsigned int)i, ace->type, ace->flags,
289 sid_to_string(sid_str, &ace->trustee),
290 (unsigned int) ace->info.mask,
291 (unsigned int)tmp_acc_desired ));
293 tmp_acc_desired = check_ace( ace, token, tmp_acc_desired, status);
294 if (NT_STATUS_V(*status)) {
295 *acc_granted = 0;
296 DEBUG(5,("se_access_check: ACE %u denied with status %s.\n", (unsigned int)i, nt_errstr(*status)));
297 return False;
302 * If there are no more desired permissions left then
303 * access was allowed.
306 if (tmp_acc_desired == 0) {
307 *acc_granted = acc_desired;
308 *status = NT_STATUS_OK;
309 DEBUG(5,("se_access_check: access (%x) granted.\n", (unsigned int)acc_desired ));
310 return True;
313 *acc_granted = 0;
314 *status = NT_STATUS_ACCESS_DENIED;
315 DEBUG(5,("se_access_check: access (%x) denied.\n", (unsigned int)acc_desired ));
316 return False;
320 /*******************************************************************
321 samr_make_sam_obj_sd
322 ********************************************************************/
324 NTSTATUS samr_make_sam_obj_sd(TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size)
326 extern DOM_SID global_sid_World;
327 DOM_SID adm_sid;
328 DOM_SID act_sid;
330 SEC_ACE ace[3];
331 SEC_ACCESS mask;
333 SEC_ACL *psa = NULL;
335 sid_copy(&adm_sid, &global_sid_Builtin);
336 sid_append_rid(&adm_sid, BUILTIN_ALIAS_RID_ADMINS);
338 sid_copy(&act_sid, &global_sid_Builtin);
339 sid_append_rid(&act_sid, BUILTIN_ALIAS_RID_ACCOUNT_OPS);
341 /*basic access for every one*/
342 init_sec_access(&mask, GENERIC_RIGHTS_SAM_EXECUTE | GENERIC_RIGHTS_SAM_READ);
343 init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
345 /*full access for builtin aliases Administrators and Account Operators*/
346 init_sec_access(&mask, GENERIC_RIGHTS_SAM_ALL_ACCESS);
347 init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
348 init_sec_ace(&ace[2], &act_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
350 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
351 return NT_STATUS_NO_MEMORY;
353 if ((*psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, sd_size)) == NULL)
354 return NT_STATUS_NO_MEMORY;
356 return NT_STATUS_OK;