source: vendor/3.6.0/libcli/security/secace.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 8.6 KB
Line 
1/*
2 * Unix SMB/Netbios implementation.
3 * struct security_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.
8 *
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 3 of the License, or
12 * (at your option) any later version.
13 *
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.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include "includes.h"
24#include "librpc/gen_ndr/ndr_security.h"
25#include "libcli/security/security.h"
26#include "lib/util/tsort.h"
27
28#define SEC_ACE_HEADER_SIZE (2 * sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t))
29
30/**
31 * Check if ACE has OBJECT type.
32 */
33bool sec_ace_object(uint8_t type)
34{
35 if (type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
36 type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT ||
37 type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT ||
38 type == SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT) {
39 return true;
40 }
41 return false;
42}
43
44/**
45 * copy a struct security_ace structure.
46 */
47void sec_ace_copy(struct security_ace *ace_dest, const struct security_ace *ace_src)
48{
49 ace_dest->type = ace_src->type;
50 ace_dest->flags = ace_src->flags;
51 ace_dest->size = ace_src->size;
52 ace_dest->access_mask = ace_src->access_mask;
53 ace_dest->object = ace_src->object;
54 ace_dest->trustee = ace_src->trustee;
55}
56
57/*******************************************************************
58 Sets up a struct security_ace structure.
59********************************************************************/
60
61void init_sec_ace(struct security_ace *t, const struct dom_sid *sid, enum security_ace_type type,
62 uint32_t mask, uint8_t flag)
63{
64 t->type = type;
65 t->flags = flag;
66 t->size = ndr_size_dom_sid(sid, 0) + 8;
67 t->access_mask = mask;
68
69 t->trustee = *sid;
70}
71
72/*******************************************************************
73 adds new SID with its permissions to ACE list
74********************************************************************/
75
76NTSTATUS sec_ace_add_sid(TALLOC_CTX *ctx, struct security_ace **pp_new, struct security_ace *old, unsigned *num, const struct dom_sid *sid, uint32_t mask)
77{
78 unsigned int i = 0;
79
80 if (!ctx || !pp_new || !old || !sid || !num) return NT_STATUS_INVALID_PARAMETER;
81
82 *num += 1;
83
84 if((pp_new[0] = talloc_zero_array(ctx, struct security_ace, *num )) == 0)
85 return NT_STATUS_NO_MEMORY;
86
87 for (i = 0; i < *num - 1; i ++)
88 sec_ace_copy(&(*pp_new)[i], &old[i]);
89
90 (*pp_new)[i].type = SEC_ACE_TYPE_ACCESS_ALLOWED;
91 (*pp_new)[i].flags = 0;
92 (*pp_new)[i].size = SEC_ACE_HEADER_SIZE + ndr_size_dom_sid(sid, 0);
93 (*pp_new)[i].access_mask = mask;
94 (*pp_new)[i].trustee = *sid;
95 return NT_STATUS_OK;
96}
97
98/*******************************************************************
99 modify SID's permissions at ACL
100********************************************************************/
101
102NTSTATUS sec_ace_mod_sid(struct security_ace *ace, size_t num, const struct dom_sid *sid, uint32_t mask)
103{
104 unsigned int i = 0;
105
106 if (!ace || !sid) return NT_STATUS_INVALID_PARAMETER;
107
108 for (i = 0; i < num; i ++) {
109 if (dom_sid_equal(&ace[i].trustee, sid)) {
110 ace[i].access_mask = mask;
111 return NT_STATUS_OK;
112 }
113 }
114 return NT_STATUS_NOT_FOUND;
115}
116
117/*******************************************************************
118 delete SID from ACL
119********************************************************************/
120
121NTSTATUS sec_ace_del_sid(TALLOC_CTX *ctx, struct security_ace **pp_new, struct security_ace *old, uint32_t *num, const struct dom_sid *sid)
122{
123 unsigned int i = 0;
124 unsigned int n_del = 0;
125
126 if (!ctx || !pp_new || !old || !sid || !num) return NT_STATUS_INVALID_PARAMETER;
127
128 if (*num) {
129 if((pp_new[0] = talloc_zero_array(ctx, struct security_ace, *num )) == 0)
130 return NT_STATUS_NO_MEMORY;
131 } else {
132 pp_new[0] = NULL;
133 }
134
135 for (i = 0; i < *num; i ++) {
136 if (!dom_sid_equal(&old[i].trustee, sid))
137 sec_ace_copy(&(*pp_new)[i], &old[i]);
138 else
139 n_del ++;
140 }
141 if (n_del == 0)
142 return NT_STATUS_NOT_FOUND;
143 else {
144 *num -= n_del;
145 return NT_STATUS_OK;
146 }
147}
148
149/*******************************************************************
150 Compares two struct security_ace structures
151********************************************************************/
152
153bool sec_ace_equal(const struct security_ace *s1, const struct security_ace *s2)
154{
155 /* Trivial case */
156
157 if (!s1 && !s2) {
158 return true;
159 }
160
161 if (!s1 || !s2) {
162 return false;
163 }
164
165 /* Check top level stuff */
166
167 if (s1->type != s2->type || s1->flags != s2->flags ||
168 s1->access_mask != s2->access_mask) {
169 return false;
170 }
171
172 /* Check SID */
173
174 if (!dom_sid_equal(&s1->trustee, &s2->trustee)) {
175 return false;
176 }
177
178 return true;
179}
180
181int nt_ace_inherit_comp(const struct security_ace *a1, const struct security_ace *a2)
182{
183 int a1_inh = a1->flags & SEC_ACE_FLAG_INHERITED_ACE;
184 int a2_inh = a2->flags & SEC_ACE_FLAG_INHERITED_ACE;
185
186 if (a1_inh == a2_inh)
187 return 0;
188
189 if (!a1_inh && a2_inh)
190 return -1;
191 return 1;
192}
193
194/*******************************************************************
195 Comparison function to apply the order explained below in a group.
196*******************************************************************/
197
198int nt_ace_canon_comp( const struct security_ace *a1, const struct security_ace *a2)
199{
200 if ((a1->type == SEC_ACE_TYPE_ACCESS_DENIED) &&
201 (a2->type != SEC_ACE_TYPE_ACCESS_DENIED))
202 return -1;
203
204 if ((a2->type == SEC_ACE_TYPE_ACCESS_DENIED) &&
205 (a1->type != SEC_ACE_TYPE_ACCESS_DENIED))
206 return 1;
207
208 /* Both access denied or access allowed. */
209
210 /* 1. ACEs that apply to the object itself */
211
212 if (!(a1->flags & SEC_ACE_FLAG_INHERIT_ONLY) &&
213 (a2->flags & SEC_ACE_FLAG_INHERIT_ONLY))
214 return -1;
215 else if (!(a2->flags & SEC_ACE_FLAG_INHERIT_ONLY) &&
216 (a1->flags & SEC_ACE_FLAG_INHERIT_ONLY))
217 return 1;
218
219 /* 2. ACEs that apply to a subobject of the object, such as
220 * a property set or property. */
221
222 if (a1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT) &&
223 !(a2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT)))
224 return -1;
225 else if (a2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT) &&
226 !(a1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT)))
227 return 1;
228
229 return 0;
230}
231
232/*******************************************************************
233 Functions to convert a SEC_DESC ACE DACL list into canonical order.
234 JRA.
235
236--- from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/security/order_of_aces_in_a_dacl.asp
237
238The following describes the preferred order:
239
240 To ensure that noninherited ACEs have precedence over inherited ACEs,
241 place all noninherited ACEs in a group before any inherited ACEs.
242 This ordering ensures, for example, that a noninherited access-denied ACE
243 is enforced regardless of any inherited ACE that allows access.
244
245 Within the groups of noninherited ACEs and inherited ACEs, order ACEs according to ACE type, as the following shows:
246 1. Access-denied ACEs that apply to the object itself
247 2. Access-denied ACEs that apply to a subobject of the object, such as a property set or property
248 3. Access-allowed ACEs that apply to the object itself
249 4. Access-allowed ACEs that apply to a subobject of the object"
250
251********************************************************************/
252
253void dacl_sort_into_canonical_order(struct security_ace *srclist, unsigned int num_aces)
254{
255 unsigned int i;
256
257 if (!srclist || num_aces == 0)
258 return;
259
260 /* Sort so that non-inherited ACE's come first. */
261 TYPESAFE_QSORT(srclist, num_aces, nt_ace_inherit_comp);
262
263 /* Find the boundary between non-inherited ACEs. */
264 for (i = 0; i < num_aces; i++ ) {
265 struct security_ace *curr_ace = &srclist[i];
266
267 if (curr_ace->flags & SEC_ACE_FLAG_INHERITED_ACE)
268 break;
269 }
270
271 /* i now points at entry number of the first inherited ACE. */
272
273 /* Sort the non-inherited ACEs. */
274 TYPESAFE_QSORT(srclist, i, nt_ace_canon_comp);
275
276 /* Now sort the inherited ACEs. */
277 TYPESAFE_QSORT(&srclist[i], num_aces - i, nt_ace_canon_comp);
278}
279
280
Note: See TracBrowser for help on using the repository browser.