2 * Unix SMB/Netbios implementation.
3 * SEC_ACE handling functions
4 * Copyright (C) Andrew Tridgell 1992-1998,
5 * Copyright (C) Jeremy R. Allison 1995-2003.
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
7 * Copyright (C) Paul Ashton 1997-1998.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /*******************************************************************
27 Check if ACE has OBJECT type.
28 ********************************************************************/
30 BOOL sec_ace_object(uint8 type)
32 if (type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
33 type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT ||
34 type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT ||
35 type == SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT) {
41 /*******************************************************************
42 copy a SEC_ACE structure.
43 ********************************************************************/
44 void sec_ace_copy(SEC_ACE *ace_dest, SEC_ACE *ace_src)
46 ace_dest->type = ace_src->type;
47 ace_dest->flags = ace_src->flags;
48 ace_dest->size = ace_src->size;
49 ace_dest->access_mask = ace_src->access_mask;
50 ace_dest->object = ace_src->object;
51 sid_copy(&ace_dest->trustee, &ace_src->trustee);
54 /*******************************************************************
55 Sets up a SEC_ACE structure.
56 ********************************************************************/
58 void init_sec_ace(SEC_ACE *t, const DOM_SID *sid, uint8 type, uint32 mask, uint8 flag)
62 t->size = sid_size(sid) + 8;
63 t->access_mask = mask;
65 ZERO_STRUCTP(&t->trustee);
66 sid_copy(&t->trustee, sid);
69 /*******************************************************************
70 adds new SID with its permissions to ACE list
71 ********************************************************************/
73 NTSTATUS sec_ace_add_sid(TALLOC_CTX *ctx, SEC_ACE **pp_new, SEC_ACE *old, unsigned *num, DOM_SID *sid, uint32 mask)
77 if (!ctx || !pp_new || !old || !sid || !num) return NT_STATUS_INVALID_PARAMETER;
81 if((pp_new[0] = TALLOC_ZERO_ARRAY(ctx, SEC_ACE, *num )) == 0)
82 return NT_STATUS_NO_MEMORY;
84 for (i = 0; i < *num - 1; i ++)
85 sec_ace_copy(&(*pp_new)[i], &old[i]);
87 (*pp_new)[i].type = 0;
88 (*pp_new)[i].flags = 0;
89 (*pp_new)[i].size = SEC_ACE_HEADER_SIZE + sid_size(sid);
90 (*pp_new)[i].access_mask = mask;
91 sid_copy(&(*pp_new)[i].trustee, sid);
95 /*******************************************************************
96 modify SID's permissions at ACL
97 ********************************************************************/
99 NTSTATUS sec_ace_mod_sid(SEC_ACE *ace, size_t num, DOM_SID *sid, uint32 mask)
103 if (!ace || !sid) return NT_STATUS_INVALID_PARAMETER;
105 for (i = 0; i < num; i ++) {
106 if (sid_compare(&ace[i].trustee, sid) == 0) {
107 ace[i].access_mask = mask;
111 return NT_STATUS_NOT_FOUND;
114 /*******************************************************************
116 ********************************************************************/
118 NTSTATUS sec_ace_del_sid(TALLOC_CTX *ctx, SEC_ACE **pp_new, SEC_ACE *old, uint32 *num, DOM_SID *sid)
121 unsigned int n_del = 0;
123 if (!ctx || !pp_new || !old || !sid || !num) return NT_STATUS_INVALID_PARAMETER;
125 if((pp_new[0] = TALLOC_ZERO_ARRAY(ctx, SEC_ACE, *num )) == 0)
126 return NT_STATUS_NO_MEMORY;
128 for (i = 0; i < *num; i ++) {
129 if (sid_compare(&old[i].trustee, sid) != 0)
130 sec_ace_copy(&(*pp_new)[i], &old[i]);
135 return NT_STATUS_NOT_FOUND;
142 /*******************************************************************
143 Compares two SEC_ACE structures
144 ********************************************************************/
146 BOOL sec_ace_equal(SEC_ACE *s1, SEC_ACE *s2)
158 /* Check top level stuff */
160 if (s1->type != s2->type || s1->flags != s2->flags ||
161 s1->access_mask != s2->access_mask) {
167 if (!sid_equal(&s1->trustee, &s2->trustee)) {
174 int nt_ace_inherit_comp( SEC_ACE *a1, SEC_ACE *a2)
176 int a1_inh = a1->flags & SEC_ACE_FLAG_INHERITED_ACE;
177 int a2_inh = a2->flags & SEC_ACE_FLAG_INHERITED_ACE;
179 if (a1_inh == a2_inh)
182 if (!a1_inh && a2_inh)
187 /*******************************************************************
188 Comparison function to apply the order explained below in a group.
189 *******************************************************************/
191 int nt_ace_canon_comp( SEC_ACE *a1, SEC_ACE *a2)
193 if ((a1->type == SEC_ACE_TYPE_ACCESS_DENIED) &&
194 (a2->type != SEC_ACE_TYPE_ACCESS_DENIED))
197 if ((a2->type == SEC_ACE_TYPE_ACCESS_DENIED) &&
198 (a1->type != SEC_ACE_TYPE_ACCESS_DENIED))
201 /* Both access denied or access allowed. */
203 /* 1. ACEs that apply to the object itself */
205 if (!(a1->flags & SEC_ACE_FLAG_INHERIT_ONLY) &&
206 (a2->flags & SEC_ACE_FLAG_INHERIT_ONLY))
208 else if (!(a2->flags & SEC_ACE_FLAG_INHERIT_ONLY) &&
209 (a1->flags & SEC_ACE_FLAG_INHERIT_ONLY))
212 /* 2. ACEs that apply to a subobject of the object, such as
213 * a property set or property. */
215 if (a1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT) &&
216 !(a2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT)))
218 else if (a2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT) &&
219 !(a1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT)))
225 /*******************************************************************
226 Functions to convert a SEC_DESC ACE DACL list into canonical order.
229 --- from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/security/order_of_aces_in_a_dacl.asp
231 The following describes the preferred order:
233 To ensure that noninherited ACEs have precedence over inherited ACEs,
234 place all noninherited ACEs in a group before any inherited ACEs.
235 This ordering ensures, for example, that a noninherited access-denied ACE
236 is enforced regardless of any inherited ACE that allows access.
238 Within the groups of noninherited ACEs and inherited ACEs, order ACEs according to ACE type, as the following shows:
239 1. Access-denied ACEs that apply to the object itself
240 2. Access-denied ACEs that apply to a subobject of the object, such as a property set or property
241 3. Access-allowed ACEs that apply to the object itself
242 4. Access-allowed ACEs that apply to a subobject of the object"
244 ********************************************************************/
246 void dacl_sort_into_canonical_order(SEC_ACE *srclist, unsigned int num_aces)
250 if (!srclist || num_aces == 0)
253 /* Sort so that non-inherited ACE's come first. */
254 qsort( srclist, num_aces, sizeof(srclist[0]), QSORT_CAST nt_ace_inherit_comp);
256 /* Find the boundary between non-inherited ACEs. */
257 for (i = 0; i < num_aces; i++ ) {
258 SEC_ACE *curr_ace = &srclist[i];
260 if (curr_ace->flags & SEC_ACE_FLAG_INHERITED_ACE)
264 /* i now points at entry number of the first inherited ACE. */
266 /* Sort the non-inherited ACEs. */
268 qsort( srclist, i, sizeof(srclist[0]), QSORT_CAST nt_ace_canon_comp);
270 /* Now sort the inherited ACEs. */
272 qsort( &srclist[i], num_aces - i, sizeof(srclist[0]), QSORT_CAST nt_ace_canon_comp);
275 /*******************************************************************
276 Check if this ACE has a SID in common with the token.
277 ********************************************************************/
279 BOOL token_sid_in_ace(const NT_USER_TOKEN *token, const SEC_ACE *ace)
283 for (i = 0; i < token->num_sids; i++) {
284 if (sid_equal(&ace->trustee, &token->user_sids[i]))