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.2.2
27 * Author: Nadezhda Ivanova
30 #include "libcli/security/security.h"
31 #include "librpc/gen_ndr/ndr_security.h"
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
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;
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)
87 static bool contains_inheritable_aces(struct security_acl *acl)
93 for (i=0; i < acl->num_aces; i++) {
94 struct security_ace *ace = &acl->aces[i];
95 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ||
96 (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
103 static struct security_acl *preprocess_creator_acl(TALLOC_CTX *mem, struct security_acl *acl)
106 struct security_acl *new_acl;
111 new_acl = talloc_zero(mem, struct security_acl);
113 for (i=0; i < acl->num_aces; i++) {
114 struct security_ace *ace = &acl->aces[i];
115 if (!(ace->flags & SEC_ACE_FLAG_INHERITED_ACE)){
116 new_acl->aces = talloc_realloc(new_acl, new_acl->aces, struct security_ace,
117 new_acl->num_aces+1);
118 if (new_acl->aces == NULL) {
119 talloc_free(new_acl);
122 new_acl->aces[new_acl->num_aces] = *ace;
127 new_acl->revision = acl->revision;
132 /* This is not exactly as described in the docs. The original seemed to return
133 * only a list of the inherited or flagless ones... */
135 static bool postprocess_acl(struct security_acl *acl,
136 struct dom_sid *owner,
137 struct dom_sid *group,
138 uint32_t (*generic_map)(uint32_t access_mask))
141 struct dom_sid *co, *cg;
142 TALLOC_CTX *tmp_ctx = talloc_new(acl);
146 co = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_OWNER);
147 cg = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_GROUP);
148 for (i=0; i < acl->num_aces; i++){
149 struct security_ace *ace = &acl->aces[i];
150 if (!(ace->flags == 0 || ace->flags & SEC_ACE_FLAG_INHERITED_ACE))
152 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY)
154 if (dom_sid_equal(&ace->trustee, co)){
155 ace->trustee = *owner;
156 /* perhaps this should be done somewhere else? */
157 ace->flags &= ~SEC_ACE_FLAG_CONTAINER_INHERIT;
159 if (dom_sid_equal(&ace->trustee, cg)){
160 ace->trustee = *group;
161 ace->flags &= ~SEC_ACE_FLAG_CONTAINER_INHERIT;
163 ace->access_mask = generic_map(ace->access_mask);
166 talloc_free(tmp_ctx);
170 static struct security_acl *calculate_inherited_from_parent(TALLOC_CTX *mem_ctx,
171 struct security_acl *acl,
173 struct GUID *object_list)
176 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
177 struct security_acl *tmp_acl = talloc_zero(tmp_ctx, struct security_acl);
178 struct security_acl *inh_acl = talloc_zero(tmp_ctx, struct security_acl);
179 struct security_acl *new_acl;
180 struct dom_sid *co, *cg;
181 if (!tmp_acl || !inh_acl)
184 co = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_OWNER);
185 cg = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_GROUP);
187 for (i=0; i < acl->num_aces; i++){
188 struct security_ace *ace = &acl->aces[i];
189 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ||
190 (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT)){
191 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces, struct security_ace,
192 tmp_acl->num_aces+1);
193 if (tmp_acl->aces == NULL) {
194 talloc_free(tmp_ctx);
198 tmp_acl->aces[tmp_acl->num_aces] = *ace;
199 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERITED_ACE;
201 if (is_container && (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
202 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
204 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
205 ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT){
206 if (!object_in_list(object_list, &ace->object.object.type.type)){
207 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
216 for (i=0; i < acl->num_aces; i++){
217 struct security_ace *ace = &acl->aces[i];
219 if (ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT)
221 if (!dom_sid_equal(&ace->trustee, co) && !dom_sid_equal(&ace->trustee, cg))
224 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ||
225 (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT)){
226 inh_acl->aces = talloc_realloc(inh_acl, inh_acl->aces, struct security_ace,
227 inh_acl->num_aces+1);
228 if (inh_acl->aces == NULL){
229 talloc_free(tmp_ctx);
232 inh_acl->aces[inh_acl->num_aces] = *ace;
233 inh_acl->aces[inh_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
234 inh_acl->aces[inh_acl->num_aces].flags |= SEC_ACE_FLAG_INHERITED_ACE;
239 new_acl = security_acl_concatenate(mem_ctx,tmp_acl, inh_acl);
241 new_acl->revision = acl->revision;
242 talloc_free(tmp_ctx);
246 /* In the docs this looks == calculate_inherited_from_parent. However,
247 * It shouldn't return the inherited, rather filter them out....
249 static struct security_acl *calculate_inherited_from_creator(TALLOC_CTX *mem_ctx,
250 struct security_acl *acl,
252 struct GUID *object_list)
255 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
256 struct security_acl *tmp_acl = talloc_zero(tmp_ctx, struct security_acl);
257 struct security_acl *new_acl;
258 struct dom_sid *co, *cg;
263 tmp_acl->revision = acl->revision;
264 DEBUG(6,(__location__ ": acl revision %u\n", acl->revision));
266 co = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_OWNER);
267 cg = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_GROUP);
269 for (i=0; i < acl->num_aces; i++){
270 struct security_ace *ace = &acl->aces[i];
271 if (ace->flags & SEC_ACE_FLAG_INHERITED_ACE)
274 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces, struct security_ace,
275 tmp_acl->num_aces+1);
276 tmp_acl->aces[tmp_acl->num_aces] = *ace;
279 if (!dom_sid_equal(&ace->trustee, co) && !dom_sid_equal(&ace->trustee, cg))
282 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces, struct security_ace,
283 tmp_acl->num_aces+1);
284 tmp_acl->aces[tmp_acl->num_aces] = *ace;
285 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
288 new_acl = security_acl_dup(mem_ctx,tmp_acl);
291 new_acl->revision = acl->revision;
293 talloc_free(tmp_ctx);
297 static void cr_descr_log_descriptor(struct security_descriptor *sd,
302 DEBUG(level,("%s: %s\n", message,
303 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,
307 DEBUG(level,("%s: NULL\n", message));
311 static void cr_descr_log_acl(struct security_acl *acl,
316 DEBUG(level,("%s: %s\n", message,
317 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_acl,
321 DEBUG(level,("%s: NULL\n", message));
325 static bool compute_acl(int acl_type,
326 struct security_descriptor *parent_sd,
327 struct security_descriptor *creator_sd,
329 uint32_t inherit_flags,
330 struct GUID *object_list,
331 uint32_t (*generic_map)(uint32_t access_mask),
332 struct security_token *token,
333 struct security_descriptor *new_sd) /* INOUT argument */
335 struct security_acl *p_acl = NULL, *c_acl = NULL, **new_acl;
337 if (acl_type == SEC_DESC_DACL_PRESENT){
339 p_acl = parent_sd->dacl;
341 c_acl = creator_sd->dacl;
342 new_acl = &new_sd->dacl;
346 p_acl = parent_sd->sacl;
348 c_acl = creator_sd->sacl;
349 new_acl = &new_sd->sacl;
352 cr_descr_log_descriptor(parent_sd, __location__"parent_sd", level);
353 cr_descr_log_descriptor(creator_sd,__location__ "creator_sd", level);
355 if (contains_inheritable_aces(p_acl)){
356 if (!c_acl || (c_acl && inherit_flags & SEC_DEFAULT_DESCRIPTOR)){
357 *new_acl = calculate_inherited_from_parent(new_sd,
361 if (*new_acl == NULL)
363 if (!postprocess_acl(*new_acl, new_sd->owner_sid,
364 new_sd->group_sid, generic_map))
367 cr_descr_log_descriptor(new_sd,
368 __location__": Nothing from creator, newsd is", level);
372 if (c_acl && !(inherit_flags & SEC_DEFAULT_DESCRIPTOR)){
373 struct security_acl *pr_acl, *tmp_acl, *tpr_acl;
374 tpr_acl = preprocess_creator_acl(new_sd, c_acl);
375 tmp_acl = calculate_inherited_from_creator(new_sd,
380 cr_descr_log_acl(tmp_acl, __location__"Inherited from creator", level);
381 /* Todo some refactoring here! */
382 if (acl_type == SEC_DESC_DACL_PRESENT &&
383 !(creator_sd->type & SECINFO_PROTECTED_DACL) &&
384 (inherit_flags & SEC_DACL_AUTO_INHERIT)){
385 pr_acl = calculate_inherited_from_parent(new_sd,
389 cr_descr_log_acl(pr_acl, __location__"Inherited from parent", level);
390 *new_acl = security_acl_concatenate(new_sd, tmp_acl, pr_acl);
391 new_sd->type |= SEC_DESC_DACL_AUTO_INHERITED;
393 else if (acl_type == SEC_DESC_SACL_PRESENT &&
394 !(creator_sd->type & SECINFO_PROTECTED_SACL) &&
395 (inherit_flags & SEC_SACL_AUTO_INHERIT)){
396 pr_acl = calculate_inherited_from_parent(new_sd,
400 cr_descr_log_acl(pr_acl, __location__"Inherited from parent", level);
401 *new_acl = security_acl_concatenate(new_sd, tmp_acl, pr_acl);
402 new_sd->type |= SEC_DESC_SACL_AUTO_INHERITED;
405 if (*new_acl == NULL)
407 if (!postprocess_acl(*new_acl, new_sd->owner_sid,
408 new_sd->group_sid,generic_map))
414 *new_acl = preprocess_creator_acl(new_sd,c_acl);
415 if (*new_acl == NULL)
417 if (!postprocess_acl(*new_acl, new_sd->owner_sid,
418 new_sd->group_sid,generic_map))
424 if (acl_type == SEC_DESC_DACL_PRESENT && new_sd->dacl)
425 new_sd->type |= SEC_DESC_DACL_PRESENT;
427 if (acl_type == SEC_DESC_SACL_PRESENT && new_sd->sacl)
428 new_sd->type |= SEC_DESC_SACL_PRESENT;
429 /* This is a hack to handle the fact that
430 * apprantly any AI flag provided by the user is preserved */
432 new_sd->type |= creator_sd->type;
433 cr_descr_log_descriptor(new_sd, __location__"final sd", level);
437 struct security_descriptor *create_security_descriptor(TALLOC_CTX *mem_ctx,
438 struct security_descriptor *parent_sd,
439 struct security_descriptor *creator_sd,
441 struct GUID *object_list,
442 uint32_t inherit_flags,
443 struct security_token *token,
444 struct dom_sid *default_owner, /* valid only for DS, NULL for the other RSs */
445 struct dom_sid *default_group, /* valid only for DS, NULL for the other RSs */
446 uint32_t (*generic_map)(uint32_t access_mask))
448 struct security_descriptor *new_sd;
449 struct dom_sid *new_owner = NULL;
450 struct dom_sid *new_group = NULL;
452 new_sd = security_descriptor_initialise(mem_ctx);
457 if (!creator_sd || !creator_sd->owner_sid) {
458 if ((inherit_flags & SEC_OWNER_FROM_PARENT) && parent_sd) {
459 new_owner = parent_sd->owner_sid;
460 } else if (!default_owner) {
461 new_owner = token->user_sid;
463 new_owner = default_owner;
464 new_sd->type |= SEC_DESC_OWNER_DEFAULTED;
467 new_owner = creator_sd->owner_sid;
470 if (!creator_sd || !creator_sd->group_sid){
471 if ((inherit_flags & SEC_GROUP_FROM_PARENT) && parent_sd) {
472 new_group = parent_sd->group_sid;
473 } else if (!default_group) {
474 new_group = token->group_sid;
476 new_group = default_group;
477 new_sd->type |= SEC_DESC_GROUP_DEFAULTED;
480 new_group = creator_sd->group_sid;
483 new_sd->owner_sid = talloc_memdup(new_sd, new_owner, sizeof(struct dom_sid));
484 new_sd->group_sid = talloc_memdup(new_sd, new_group, sizeof(struct dom_sid));
485 if (!new_sd->owner_sid || !new_sd->group_sid){
490 if (!compute_acl(SEC_DESC_DACL_PRESENT, parent_sd, creator_sd,
491 is_container, inherit_flags, object_list,
492 generic_map,token,new_sd)){
497 if (!compute_acl(SEC_DESC_SACL_PRESENT, parent_sd, creator_sd,
498 is_container, inherit_flags, object_list,
499 generic_map, token,new_sd)){