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.
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
,
34 uint32 mask
= ace
->info
.mask
;
37 * Inherit only is ignored.
40 if (ace
->flags
& SEC_ACE_FLAG_INHERIT_ONLY
) {
45 * If this ACE has no SID in common with the token,
46 * ignore it as it cannot be used to make an access
50 if (!token_sid_in_ace( token
, ace
))
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
63 case SEC_ACE_TYPE_ACCESS_DENIED
:
65 * This is explicitly denied.
66 * If any bits match terminate here,
69 if (acc_desired
& mask
) {
70 *status
= NT_STATUS_ACCESS_DENIED
;
74 case SEC_ACE_TYPE_SYSTEM_ALARM
:
75 case SEC_ACE_TYPE_SYSTEM_AUDIT
:
76 *status
= NT_STATUS_NOT_IMPLEMENTED
;
79 *status
= NT_STATUS_INVALID_PARAMETER
;
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
,
95 uint32 acc_denied
= 0;
96 uint32 acc_granted
= 0;
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
))
107 case SEC_ACE_TYPE_ACCESS_ALLOWED
:
108 acc_granted
|= (mask
& ~acc_denied
);
110 case SEC_ACE_TYPE_ACCESS_DENIED
:
111 acc_denied
|= (mask
& ~acc_granted
);
113 case SEC_ACE_TYPE_SYSTEM_ALARM
:
114 case SEC_ACE_TYPE_SYSTEM_AUDIT
:
115 *status
= NT_STATUS_NOT_IMPLEMENTED
;
119 *status
= NT_STATUS_INVALID_PARAMETER
;
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
;
137 * Return the access we did get.
140 *granted
= acc_granted
;
141 *status
= NT_STATUS_OK
;
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
,
220 uint32 tmp_acc_desired
= acc_desired
;
222 if (!status
|| !acc_granted
)
226 token
= &anonymous_token
;
228 *status
= NT_STATUS_OK
;
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"));
249 /* The user sid is the first in the token */
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 ? */
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
;
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
,
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
)) {
295 DEBUG(5,("se_access_check: ACE %u denied with status %s.\n", (unsigned int)i
, nt_errstr(*status
)));
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
));
313 *status
= NT_STATUS_ACCESS_DENIED
;
314 DEBUG(5,("se_access_check: access (%x) denied.\n", (unsigned int)acc_desired
));
319 /*******************************************************************
321 ********************************************************************/
323 NTSTATUS
samr_make_sam_obj_sd(TALLOC_CTX
*ctx
, SEC_DESC
**psd
, size_t *sd_size
)
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
;