WHATSNEW: Update changed parameters.
[Samba/gebeck_regimport.git] / libcli / security / create_descriptor.c
blobd5bb21bb544dd7948c9e0932f27e98942c0d516c
1 /*
2 Copyright (C) Nadezhda Ivanova 2009
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * Name: create_descriptor
21 * Component: routines for calculating and creating security descriptors
22 * as described in MS-DTYP 2.5.3.x
24 * Description:
27 * Author: Nadezhda Ivanova
29 #include "includes.h"
30 #include "libcli/security/security.h"
31 #include "librpc/gen_ndr/ndr_security.h"
33 /* Todos:
34 * build the security token dacl as follows:
35 * SYSTEM: GA, OWNER: GA, LOGIN_SID:GW|GE
36 * Need session id information for the login SID. Probably
37 * the best place for this is during token creation
39 * Implement SD Invariants
40 * ACE sorting rules
41 * LDAP_SERVER_SD_FLAGS_OID control
42 * ADTS 7.1.3.3 needs to be clarified
45 /* the mapping function for generic rights for DS.(GA,GR,GW,GX)
46 * The mapping function is passed as an argument to the
47 * descriptor calculating routine and depends on the security
48 * manager that calls the calculating routine.
49 * TODO: need similar mappings for the file system and
50 * registry security managers in order to make this code
51 * generic for all security managers
54 uint32_t map_generic_rights_ds(uint32_t access_mask)
56 if (access_mask & SEC_GENERIC_ALL) {
57 access_mask |= SEC_ADS_GENERIC_ALL;
58 access_mask = ~SEC_GENERIC_ALL;
61 if (access_mask & SEC_GENERIC_EXECUTE) {
62 access_mask |= SEC_ADS_GENERIC_EXECUTE;
63 access_mask = ~SEC_GENERIC_EXECUTE;
66 if (access_mask & SEC_GENERIC_WRITE) {
67 access_mask |= SEC_ADS_GENERIC_WRITE;
68 access_mask &= ~SEC_GENERIC_WRITE;
71 if (access_mask & SEC_GENERIC_READ) {
72 access_mask |= SEC_ADS_GENERIC_READ;
73 access_mask &= ~SEC_GENERIC_READ;
76 return access_mask;
79 /* Not sure what this has to be,
80 * and it does not seem to have any influence */
81 static bool object_in_list(struct GUID *object_list, struct GUID *object)
83 return true;
86 static struct security_acl *calculate_inherited_from_parent(TALLOC_CTX *mem_ctx,
87 struct security_acl *acl,
88 bool is_container,
89 struct dom_sid *owner,
90 struct dom_sid *group,
91 struct GUID *object_list)
93 uint32_t i;
94 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
95 struct security_acl *tmp_acl = talloc_zero(mem_ctx, struct security_acl);
96 struct dom_sid *co, *cg;
97 if (!tmp_acl) {
98 return NULL;
101 if (!acl) {
102 return NULL;
104 co = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_OWNER);
105 cg = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_GROUP);
107 for (i=0; i < acl->num_aces; i++) {
108 struct security_ace *ace = &acl->aces[i];
109 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ||
110 (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT)) {
111 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces, struct security_ace,
112 tmp_acl->num_aces+1);
113 if (tmp_acl->aces == NULL) {
114 talloc_free(tmp_ctx);
115 return NULL;
118 tmp_acl->aces[tmp_acl->num_aces] = *ace;
119 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERITED_ACE;
121 if (is_container && (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
122 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
124 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
125 ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT) {
126 if (!object_in_list(object_list, &ace->object.object.type.type)) {
127 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
131 tmp_acl->aces[tmp_acl->num_aces].access_mask =
132 map_generic_rights_ds(ace->access_mask);
133 tmp_acl->num_aces++;
134 if (is_container) {
135 if (!(ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) &&
136 ((dom_sid_equal(&ace->trustee, co) || dom_sid_equal(&ace->trustee, cg)))) {
137 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces, struct security_ace,
138 tmp_acl->num_aces+1);
139 if (tmp_acl->aces == NULL) {
140 talloc_free(tmp_ctx);
141 return NULL;
143 tmp_acl->aces[tmp_acl->num_aces] = *ace;
144 tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_INHERIT_ONLY;
145 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERITED_ACE;
146 if (dom_sid_equal(&tmp_acl->aces[tmp_acl->num_aces].trustee, co)) {
147 tmp_acl->aces[tmp_acl->num_aces].trustee = *owner;
149 if (dom_sid_equal(&tmp_acl->aces[tmp_acl->num_aces].trustee, cg)) {
150 tmp_acl->aces[tmp_acl->num_aces].trustee = *group;
152 tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_CONTAINER_INHERIT;
153 tmp_acl->aces[tmp_acl->num_aces].access_mask =
154 map_generic_rights_ds(ace->access_mask);
155 tmp_acl->num_aces++;
160 if (tmp_acl->num_aces == 0) {
161 return NULL;
163 if (acl) {
164 tmp_acl->revision = acl->revision;
166 return tmp_acl;
169 static struct security_acl *process_user_acl(TALLOC_CTX *mem_ctx,
170 struct security_acl *acl,
171 bool is_container,
172 struct dom_sid *owner,
173 struct dom_sid *group,
174 struct GUID *object_list)
176 uint32_t i;
177 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
178 struct security_acl *tmp_acl = talloc_zero(tmp_ctx, struct security_acl);
179 struct security_acl *new_acl;
180 struct dom_sid *co, *cg;
182 if (!acl)
183 return NULL;
185 if (!tmp_acl)
186 return NULL;
188 tmp_acl->revision = acl->revision;
189 DEBUG(6,(__location__ ": acl revision %u\n", acl->revision));
191 co = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_OWNER);
192 cg = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_GROUP);
194 for (i=0; i < acl->num_aces; i++){
195 struct security_ace *ace = &acl->aces[i];
196 if (ace->flags & SEC_ACE_FLAG_INHERITED_ACE)
197 continue;
198 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
199 !(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT ||
200 ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
201 continue;
203 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces, struct security_ace,
204 tmp_acl->num_aces+1);
205 tmp_acl->aces[tmp_acl->num_aces] = *ace;
206 if (dom_sid_equal(&(tmp_acl->aces[tmp_acl->num_aces].trustee), co)) {
207 tmp_acl->aces[tmp_acl->num_aces].trustee = *owner;
208 tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_CONTAINER_INHERIT;
210 if (dom_sid_equal(&(tmp_acl->aces[tmp_acl->num_aces].trustee), cg)) {
211 tmp_acl->aces[tmp_acl->num_aces].trustee = *group;
212 tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_CONTAINER_INHERIT;
214 tmp_acl->aces[tmp_acl->num_aces].access_mask =
215 map_generic_rights_ds(tmp_acl->aces[tmp_acl->num_aces].access_mask);
216 tmp_acl->num_aces++;
218 if (!dom_sid_equal(&ace->trustee, co) && !dom_sid_equal(&ace->trustee, cg))
219 continue;
221 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces, struct security_ace,
222 tmp_acl->num_aces+1);
223 tmp_acl->aces[tmp_acl->num_aces] = *ace;
224 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
225 tmp_acl->num_aces++;
227 new_acl = security_acl_dup(mem_ctx,tmp_acl);
229 if (new_acl)
230 new_acl->revision = acl->revision;
232 talloc_free(tmp_ctx);
233 return new_acl;
236 static void cr_descr_log_descriptor(struct security_descriptor *sd,
237 const char *message,
238 int level)
240 if (sd) {
241 DEBUG(level,("%s: %s\n", message,
242 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,
243 "", sd)));
245 else {
246 DEBUG(level,("%s: NULL\n", message));
250 static void cr_descr_log_acl(struct security_acl *acl,
251 const char *message,
252 int level)
254 if (acl) {
255 DEBUG(level,("%s: %s\n", message,
256 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_acl,
257 "", acl)));
259 else {
260 DEBUG(level,("%s: NULL\n", message));
264 static bool compute_acl(struct security_descriptor *parent_sd,
265 struct security_descriptor *creator_sd,
266 bool is_container,
267 uint32_t inherit_flags,
268 struct GUID *object_list,
269 uint32_t (*generic_map)(uint32_t access_mask),
270 struct security_token *token,
271 struct security_descriptor *new_sd) /* INOUT argument */
273 struct security_acl *user_dacl, *user_sacl, *inherited_dacl, *inherited_sacl;
274 int level = 10;
276 if (!parent_sd || !(inherit_flags & SEC_DACL_AUTO_INHERIT)) {
277 inherited_dacl = NULL;
278 } else if (creator_sd && (creator_sd->type & SEC_DESC_DACL_PROTECTED)) {
279 inherited_dacl = NULL;
280 } else {
281 inherited_dacl = calculate_inherited_from_parent(new_sd,
282 parent_sd->dacl,
283 is_container,
284 new_sd->owner_sid,
285 new_sd->group_sid,
286 object_list);
290 if (!parent_sd || !(inherit_flags & SEC_SACL_AUTO_INHERIT)) {
291 inherited_sacl = NULL;
292 } else if (creator_sd && (creator_sd->type & SEC_DESC_SACL_PROTECTED)) {
293 inherited_sacl = NULL;
294 } else {
295 inherited_sacl = calculate_inherited_from_parent(new_sd,
296 parent_sd->sacl,
297 is_container,
298 new_sd->owner_sid,
299 new_sd->group_sid,
300 object_list);
303 if (!creator_sd || (inherit_flags & SEC_DEFAULT_DESCRIPTOR)) {
304 user_dacl = NULL;
305 user_sacl = NULL;
306 } else {
307 user_dacl = process_user_acl(new_sd,
308 creator_sd->dacl,
309 is_container,
310 new_sd->owner_sid,
311 new_sd->group_sid,
312 object_list);
313 user_sacl = process_user_acl(new_sd,
314 creator_sd->sacl,
315 is_container,
316 new_sd->owner_sid,
317 new_sd->group_sid,
318 object_list);
320 cr_descr_log_descriptor(parent_sd, __location__"parent_sd", level);
321 cr_descr_log_descriptor(creator_sd,__location__ "creator_sd", level);
323 new_sd->dacl = security_acl_concatenate(new_sd, user_dacl, inherited_dacl);
324 if (new_sd->dacl) {
325 new_sd->type |= SEC_DESC_DACL_PRESENT;
327 if (inherited_dacl) {
328 new_sd->type |= SEC_DESC_DACL_AUTO_INHERITED;
331 new_sd->sacl = security_acl_concatenate(new_sd, user_sacl, inherited_sacl);
332 if (new_sd->sacl) {
333 new_sd->type |= SEC_DESC_SACL_PRESENT;
335 if (inherited_sacl) {
336 new_sd->type |= SEC_DESC_SACL_AUTO_INHERITED;
338 /* This is a hack to handle the fact that
339 * apprantly any AI flag provided by the user is preserved */
340 if (creator_sd)
341 new_sd->type |= creator_sd->type;
342 cr_descr_log_descriptor(new_sd, __location__"final sd", level);
343 return true;
346 struct security_descriptor *create_security_descriptor(TALLOC_CTX *mem_ctx,
347 struct security_descriptor *parent_sd,
348 struct security_descriptor *creator_sd,
349 bool is_container,
350 struct GUID *object_list,
351 uint32_t inherit_flags,
352 struct security_token *token,
353 struct dom_sid *default_owner, /* valid only for DS, NULL for the other RSs */
354 struct dom_sid *default_group, /* valid only for DS, NULL for the other RSs */
355 uint32_t (*generic_map)(uint32_t access_mask))
357 struct security_descriptor *new_sd;
358 struct dom_sid *new_owner = NULL;
359 struct dom_sid *new_group = NULL;
361 new_sd = security_descriptor_initialise(mem_ctx);
362 if (!new_sd) {
363 return NULL;
366 if (!creator_sd || !creator_sd->owner_sid) {
367 if ((inherit_flags & SEC_OWNER_FROM_PARENT) && parent_sd) {
368 new_owner = parent_sd->owner_sid;
369 } else if (!default_owner) {
370 new_owner = &token->sids[PRIMARY_USER_SID_INDEX];
371 } else {
372 new_owner = default_owner;
373 new_sd->type |= SEC_DESC_OWNER_DEFAULTED;
375 } else {
376 new_owner = creator_sd->owner_sid;
379 if (!creator_sd || !creator_sd->group_sid){
380 if ((inherit_flags & SEC_GROUP_FROM_PARENT) && parent_sd) {
381 new_group = parent_sd->group_sid;
382 } else if (!default_group && token->num_sids > PRIMARY_GROUP_SID_INDEX) {
383 new_group = &token->sids[PRIMARY_GROUP_SID_INDEX];
384 } else if (!default_group) {
385 /* This will happen only for anonymous, which has no other groups */
386 new_group = &token->sids[PRIMARY_USER_SID_INDEX];
387 } else {
388 new_group = default_group;
389 new_sd->type |= SEC_DESC_GROUP_DEFAULTED;
391 } else {
392 new_group = creator_sd->group_sid;
395 new_sd->owner_sid = talloc_memdup(new_sd, new_owner, sizeof(struct dom_sid));
396 new_sd->group_sid = talloc_memdup(new_sd, new_group, sizeof(struct dom_sid));
397 if (!new_sd->owner_sid || !new_sd->group_sid){
398 talloc_free(new_sd);
399 return NULL;
402 if (!compute_acl(parent_sd, creator_sd,
403 is_container, inherit_flags, object_list,
404 generic_map,token,new_sd)){
405 talloc_free(new_sd);
406 return NULL;
409 return new_sd;