s3/docs: Fix serveral typos.
[Samba.git] / source / lib / util_seaccess.c
blob87e70bb95bf1b2eb6475c39c5148ec61b4693255
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 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/>.
21 #include "includes.h"
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,
31 NTSTATUS *status)
33 uint32 mask = ace->access_mask;
36 * Inherit only is ignored.
39 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
40 return acc_desired;
44 * If this ACE has no SID in common with the token,
45 * ignore it as it cannot be used to make an access
46 * determination.
49 if (!token_sid_in_ace( token, ace))
50 return acc_desired;
52 switch (ace->type) {
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
58 * bits needed.
60 acc_desired &= ~mask;
61 break;
62 case SEC_ACE_TYPE_ACCESS_DENIED:
64 * This is explicitly denied.
65 * If any bits match terminate here,
66 * we are denied.
68 if (acc_desired & mask) {
69 *status = NT_STATUS_ACCESS_DENIED;
70 return 0xFFFFFFFF;
72 break;
73 case SEC_ACE_TYPE_SYSTEM_ALARM:
74 case SEC_ACE_TYPE_SYSTEM_AUDIT:
75 *status = NT_STATUS_NOT_IMPLEMENTED;
76 return 0xFFFFFFFF;
77 default:
78 *status = NT_STATUS_INVALID_PARAMETER;
79 return 0xFFFFFFFF;
82 return acc_desired;
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,
91 uint32 desired,
92 NTSTATUS *status)
94 uint32 acc_denied = 0;
95 uint32 acc_granted = 0;
96 size_t i;
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))
103 continue;
105 switch (ace->type) {
106 case SEC_ACE_TYPE_ACCESS_ALLOWED:
107 acc_granted |= (mask & ~acc_denied);
108 break;
109 case SEC_ACE_TYPE_ACCESS_DENIED:
110 acc_denied |= (mask & ~acc_granted);
111 break;
112 case SEC_ACE_TYPE_SYSTEM_ALARM:
113 case SEC_ACE_TYPE_SYSTEM_AUDIT:
114 *status = NT_STATUS_NOT_IMPLEMENTED;
115 *granted = 0;
116 return False;
117 default:
118 *status = NT_STATUS_INVALID_PARAMETER;
119 *granted = 0;
120 return False;
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;
131 *granted = 0;
132 return False;
136 * Return the access we did get.
139 *granted = acc_granted;
140 *status = NT_STATUS_OK;
141 return True;
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 standard access rights to object specific rights. This technique is
180 used to give meaning to assigning read, write, execute and all access to
181 objects. Each type of object has its own mapping of standard to object
182 specific access rights. */
184 void se_map_standard(uint32 *access_mask, struct standard_mapping *mapping)
186 uint32 old_mask = *access_mask;
188 if (*access_mask & READ_CONTROL_ACCESS) {
189 *access_mask &= ~READ_CONTROL_ACCESS;
190 *access_mask |= mapping->std_read;
193 if (*access_mask & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS)) {
194 *access_mask &= ~(DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS);
195 *access_mask |= mapping->std_all;
198 if (old_mask != *access_mask) {
199 DEBUG(10, ("se_map_standard(): mapped mask 0x%08x to 0x%08x\n",
200 old_mask, *access_mask));
204 /*****************************************************************************
205 Check access rights of a user against a security descriptor. Look at
206 each ACE in the security descriptor until an access denied ACE denies
207 any of the desired rights to the user or any of the users groups, or one
208 or more ACEs explicitly grant all requested access rights. See
209 "Access-Checking" document in MSDN.
210 *****************************************************************************/
212 bool se_access_check(const SEC_DESC *sd, const NT_USER_TOKEN *token,
213 uint32 acc_desired, uint32 *acc_granted,
214 NTSTATUS *status)
216 size_t i;
217 SEC_ACL *the_acl;
218 uint32 tmp_acc_desired = acc_desired;
220 if (!status || !acc_granted)
221 return False;
223 if (!token)
224 token = &anonymous_token;
226 *status = NT_STATUS_OK;
227 *acc_granted = 0;
229 DEBUG(10,("se_access_check: requested access 0x%08x, for NT token "
230 "with %u entries and first sid %s.\n",
231 (unsigned int)acc_desired, (unsigned int)token->num_sids,
232 sid_string_dbg(&token->user_sids[0])));
235 * No security descriptor or security descriptor with no DACL
236 * present allows all access.
239 /* ACL must have something in it */
241 if (!sd || (sd && (!(sd->type & SEC_DESC_DACL_PRESENT) || sd->dacl == NULL))) {
242 *status = NT_STATUS_OK;
243 *acc_granted = acc_desired;
244 DEBUG(5, ("se_access_check: no sd or blank DACL, access allowed\n"));
245 return True;
248 /* The user sid is the first in the token */
249 if (DEBUGLVL(3)) {
250 DEBUG(3, ("se_access_check: user sid is %s\n",
251 sid_string_dbg(
252 &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_string_dbg(&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->aces[i];
287 DEBUGADD(10,("se_access_check: ACE %u: type %d, flags = "
288 "0x%02x, SID = %s mask = %x, current desired "
289 "= %x\n", (unsigned int)i, ace->type, ace->flags,
290 sid_string_dbg(&ace->trustee),
291 (unsigned int) ace->access_mask,
292 (unsigned int)tmp_acc_desired ));
294 tmp_acc_desired = check_ace( ace, token, tmp_acc_desired, status);
295 if (NT_STATUS_V(*status)) {
296 *acc_granted = 0;
297 DEBUG(5,("se_access_check: ACE %u denied with status %s.\n", (unsigned int)i, nt_errstr(*status)));
298 return False;
303 * If there are no more desired permissions left then
304 * access was allowed.
307 if (tmp_acc_desired == 0) {
308 *acc_granted = acc_desired;
309 *status = NT_STATUS_OK;
310 DEBUG(5,("se_access_check: access (%x) granted.\n", (unsigned int)acc_desired ));
311 return True;
314 *acc_granted = 0;
315 *status = NT_STATUS_ACCESS_DENIED;
316 DEBUG(5,("se_access_check: access (%x) denied.\n", (unsigned int)acc_desired ));
317 return False;
321 /*******************************************************************
322 samr_make_sam_obj_sd
323 ********************************************************************/
325 NTSTATUS samr_make_sam_obj_sd(TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size)
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, SECURITY_DESCRIPTOR_REVISION_1,
354 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
355 psa, sd_size)) == NULL)
356 return NT_STATUS_NO_MEMORY;
358 return NT_STATUS_OK;