torture: convert torture_comment() -> torture_result() so we can knownfail flapping...
[Samba/wip.git] / libcli / security / create_descriptor.c
blob03c0658485eb1356124a5fffc3d3e0f14ad3b9b4
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 size_t i;
85 if (object_list == NULL) {
86 return true;
89 if (GUID_all_zero(object)) {
90 return true;
93 for (i=0; ; i++) {
94 if (GUID_all_zero(&object_list[i])) {
95 return false;
97 if (!GUID_equal(&object_list[i], object)) {
98 continue;
101 return true;
104 return false;
107 /* returns true if the ACE gontains generic information
108 * that needs to be processed additionally */
110 static bool desc_ace_has_generic(struct security_ace *ace)
112 if (ace->access_mask & SEC_GENERIC_ALL || ace->access_mask & SEC_GENERIC_READ ||
113 ace->access_mask & SEC_GENERIC_WRITE || ace->access_mask & SEC_GENERIC_EXECUTE) {
114 return true;
116 if (dom_sid_equal(&ace->trustee, &global_sid_Creator_Owner) ||
117 dom_sid_equal(&ace->trustee, &global_sid_Creator_Group)) {
118 return true;
120 return false;
123 /* creates an ace in which the generic information is expanded */
125 static void desc_expand_generic(struct security_ace *new_ace,
126 struct dom_sid *owner,
127 struct dom_sid *group)
129 new_ace->access_mask = map_generic_rights_ds(new_ace->access_mask);
130 if (dom_sid_equal(&new_ace->trustee, &global_sid_Creator_Owner)) {
131 new_ace->trustee = *owner;
133 if (dom_sid_equal(&new_ace->trustee, &global_sid_Creator_Group)) {
134 new_ace->trustee = *group;
136 new_ace->flags = 0x0;
139 static struct security_acl *calculate_inherited_from_parent(TALLOC_CTX *mem_ctx,
140 struct security_acl *acl,
141 bool is_container,
142 struct dom_sid *owner,
143 struct dom_sid *group,
144 struct GUID *object_list)
146 uint32_t i;
147 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
148 struct security_acl *tmp_acl = talloc_zero(mem_ctx, struct security_acl);
149 if (!tmp_acl) {
150 return NULL;
153 if (!acl) {
154 return NULL;
157 for (i=0; i < acl->num_aces; i++) {
158 struct security_ace *ace = &acl->aces[i];
159 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ||
160 (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT)) {
161 struct GUID inherited_object = GUID_zero();
163 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces,
164 struct security_ace,
165 tmp_acl->num_aces+1);
166 if (tmp_acl->aces == NULL) {
167 talloc_free(tmp_ctx);
168 return NULL;
171 tmp_acl->aces[tmp_acl->num_aces] = *ace;
172 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERITED_ACE;
173 /* remove IO flag from the child's ace */
174 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
175 !desc_ace_has_generic(ace)) {
176 tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_INHERIT_ONLY;
179 if (is_container && (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
180 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
182 switch (ace->type) {
183 case SEC_ACE_TYPE_ACCESS_ALLOWED:
184 case SEC_ACE_TYPE_ACCESS_DENIED:
185 case SEC_ACE_TYPE_SYSTEM_AUDIT:
186 case SEC_ACE_TYPE_SYSTEM_ALARM:
187 case SEC_ACE_TYPE_ALLOWED_COMPOUND:
188 break;
190 case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
191 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
192 case SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT:
193 case SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT:
194 if (ace->object.object.flags & SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT) {
195 inherited_object = ace->object.object.inherited_type.inherited_type;
198 if (!object_in_list(object_list, &inherited_object)) {
199 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
202 break;
205 tmp_acl->num_aces++;
206 if (is_container) {
207 if (!(ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) &&
208 (desc_ace_has_generic(ace))) {
209 tmp_acl->aces = talloc_realloc(tmp_acl,
210 tmp_acl->aces,
211 struct security_ace,
212 tmp_acl->num_aces+1);
213 if (tmp_acl->aces == NULL) {
214 talloc_free(tmp_ctx);
215 return NULL;
217 tmp_acl->aces[tmp_acl->num_aces] = *ace;
218 desc_expand_generic(&tmp_acl->aces[tmp_acl->num_aces],
219 owner,
220 group);
221 tmp_acl->aces[tmp_acl->num_aces].flags = SEC_ACE_FLAG_INHERITED_ACE;
222 tmp_acl->num_aces++;
227 if (tmp_acl->num_aces == 0) {
228 return NULL;
230 if (acl) {
231 tmp_acl->revision = acl->revision;
233 return tmp_acl;
236 static struct security_acl *process_user_acl(TALLOC_CTX *mem_ctx,
237 struct security_acl *acl,
238 bool is_container,
239 struct dom_sid *owner,
240 struct dom_sid *group,
241 struct GUID *object_list,
242 bool is_protected)
244 uint32_t i;
245 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
246 struct security_acl *tmp_acl = talloc_zero(tmp_ctx, struct security_acl);
247 struct security_acl *new_acl;
249 if (!acl)
250 return NULL;
252 if (!tmp_acl)
253 return NULL;
255 tmp_acl->revision = acl->revision;
256 DEBUG(6,(__location__ ": acl revision %d\n", acl->revision));
258 for (i=0; i < acl->num_aces; i++){
259 struct security_ace *ace = &acl->aces[i];
260 /* Remove ID flags from user-provided ACEs
261 * if we break inheritance, ignore them otherwise */
262 if (ace->flags & SEC_ACE_FLAG_INHERITED_ACE) {
263 if (is_protected) {
264 ace->flags &= ~SEC_ACE_FLAG_INHERITED_ACE;
265 } else {
266 continue;
270 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
271 !(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT ||
272 ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
273 continue;
275 tmp_acl->aces = talloc_realloc(tmp_acl,
276 tmp_acl->aces,
277 struct security_ace,
278 tmp_acl->num_aces+1);
279 tmp_acl->aces[tmp_acl->num_aces] = *ace;
280 tmp_acl->num_aces++;
281 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
282 continue;
284 /* if the ACE contains CO, CG, GA, GE, GR or GW, and is inheritable
285 * it has to be expanded to two aces, the original as IO,
286 * and another one where these are translated */
287 if (desc_ace_has_generic(ace)) {
288 if (!(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
289 desc_expand_generic(&tmp_acl->aces[tmp_acl->num_aces-1],
290 owner,
291 group);
292 } else {
293 /*The original ACE becomes read only */
294 tmp_acl->aces[tmp_acl->num_aces-1].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
295 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces,
296 struct security_ace,
297 tmp_acl->num_aces+1);
298 /* add a new ACE with expanded generic info */
299 tmp_acl->aces[tmp_acl->num_aces] = *ace;
300 desc_expand_generic(&tmp_acl->aces[tmp_acl->num_aces],
301 owner,
302 group);
303 tmp_acl->num_aces++;
307 new_acl = security_acl_dup(mem_ctx,tmp_acl);
309 if (new_acl)
310 new_acl->revision = acl->revision;
312 talloc_free(tmp_ctx);
313 return new_acl;
316 static void cr_descr_log_descriptor(struct security_descriptor *sd,
317 const char *message,
318 int level)
320 if (sd) {
321 DEBUG(level,("%s: %s\n", message,
322 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,
323 "", sd)));
325 else {
326 DEBUG(level,("%s: NULL\n", message));
330 #if 0
331 static void cr_descr_log_acl(struct security_acl *acl,
332 const char *message,
333 int level)
335 if (acl) {
336 DEBUG(level,("%s: %s\n", message,
337 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_acl,
338 "", acl)));
340 else {
341 DEBUG(level,("%s: NULL\n", message));
344 #endif
346 static bool compute_acl(struct security_descriptor *parent_sd,
347 struct security_descriptor *creator_sd,
348 bool is_container,
349 uint32_t inherit_flags,
350 struct GUID *object_list,
351 uint32_t (*generic_map)(uint32_t access_mask),
352 struct security_token *token,
353 struct security_descriptor *new_sd) /* INOUT argument */
355 struct security_acl *user_dacl, *user_sacl, *inherited_dacl, *inherited_sacl;
356 int level = 10;
358 if (!parent_sd || !(inherit_flags & SEC_DACL_AUTO_INHERIT)) {
359 inherited_dacl = NULL;
360 } else if (creator_sd && (creator_sd->type & SEC_DESC_DACL_PROTECTED)) {
361 inherited_dacl = NULL;
362 } else {
363 inherited_dacl = calculate_inherited_from_parent(new_sd,
364 parent_sd->dacl,
365 is_container,
366 new_sd->owner_sid,
367 new_sd->group_sid,
368 object_list);
372 if (!parent_sd || !(inherit_flags & SEC_SACL_AUTO_INHERIT)) {
373 inherited_sacl = NULL;
374 } else if (creator_sd && (creator_sd->type & SEC_DESC_SACL_PROTECTED)) {
375 inherited_sacl = NULL;
376 } else {
377 inherited_sacl = calculate_inherited_from_parent(new_sd,
378 parent_sd->sacl,
379 is_container,
380 new_sd->owner_sid,
381 new_sd->group_sid,
382 object_list);
385 if (!creator_sd || (inherit_flags & SEC_DEFAULT_DESCRIPTOR)) {
386 user_dacl = NULL;
387 user_sacl = NULL;
388 } else {
389 user_dacl = process_user_acl(new_sd,
390 creator_sd->dacl,
391 is_container,
392 new_sd->owner_sid,
393 new_sd->group_sid,
394 object_list,
395 creator_sd->type & SEC_DESC_DACL_PROTECTED);
396 user_sacl = process_user_acl(new_sd,
397 creator_sd->sacl,
398 is_container,
399 new_sd->owner_sid,
400 new_sd->group_sid,
401 object_list,
402 creator_sd->type & SEC_DESC_SACL_PROTECTED);
404 cr_descr_log_descriptor(parent_sd, __location__"parent_sd", level);
405 cr_descr_log_descriptor(creator_sd,__location__ "creator_sd", level);
407 new_sd->dacl = security_acl_concatenate(new_sd, user_dacl, inherited_dacl);
408 if (new_sd->dacl) {
409 new_sd->type |= SEC_DESC_DACL_PRESENT;
411 if (inherited_dacl) {
412 new_sd->type |= SEC_DESC_DACL_AUTO_INHERITED;
415 new_sd->sacl = security_acl_concatenate(new_sd, user_sacl, inherited_sacl);
416 if (new_sd->sacl) {
417 new_sd->type |= SEC_DESC_SACL_PRESENT;
419 if (inherited_sacl) {
420 new_sd->type |= SEC_DESC_SACL_AUTO_INHERITED;
422 /* This is a hack to handle the fact that
423 * apprantly any AI flag provided by the user is preserved */
424 if (creator_sd)
425 new_sd->type |= creator_sd->type;
426 cr_descr_log_descriptor(new_sd, __location__"final sd", level);
427 return true;
430 struct security_descriptor *create_security_descriptor(TALLOC_CTX *mem_ctx,
431 struct security_descriptor *parent_sd,
432 struct security_descriptor *creator_sd,
433 bool is_container,
434 struct GUID *object_list,
435 uint32_t inherit_flags,
436 struct security_token *token,
437 struct dom_sid *default_owner, /* valid only for DS, NULL for the other RSs */
438 struct dom_sid *default_group, /* valid only for DS, NULL for the other RSs */
439 uint32_t (*generic_map)(uint32_t access_mask))
441 struct security_descriptor *new_sd;
442 struct dom_sid *new_owner = NULL;
443 struct dom_sid *new_group = NULL;
445 new_sd = security_descriptor_initialise(mem_ctx);
446 if (!new_sd) {
447 return NULL;
450 if (!creator_sd || !creator_sd->owner_sid) {
451 if ((inherit_flags & SEC_OWNER_FROM_PARENT) && parent_sd) {
452 new_owner = parent_sd->owner_sid;
453 } else if (!default_owner) {
454 new_owner = &token->sids[PRIMARY_USER_SID_INDEX];
455 } else {
456 new_owner = default_owner;
457 new_sd->type |= SEC_DESC_OWNER_DEFAULTED;
459 } else {
460 new_owner = creator_sd->owner_sid;
463 if (!creator_sd || !creator_sd->group_sid){
464 if ((inherit_flags & SEC_GROUP_FROM_PARENT) && parent_sd) {
465 new_group = parent_sd->group_sid;
466 } else if (!default_group && token->num_sids > PRIMARY_GROUP_SID_INDEX) {
467 new_group = &token->sids[PRIMARY_GROUP_SID_INDEX];
468 } else if (!default_group) {
469 /* This will happen only for anonymous, which has no other groups */
470 new_group = &token->sids[PRIMARY_USER_SID_INDEX];
471 } else {
472 new_group = default_group;
473 new_sd->type |= SEC_DESC_GROUP_DEFAULTED;
475 } else {
476 new_group = creator_sd->group_sid;
479 new_sd->owner_sid = talloc_memdup(new_sd, new_owner, sizeof(struct dom_sid));
480 new_sd->group_sid = talloc_memdup(new_sd, new_group, sizeof(struct dom_sid));
481 if (!new_sd->owner_sid || !new_sd->group_sid){
482 talloc_free(new_sd);
483 return NULL;
486 if (!compute_acl(parent_sd, creator_sd,
487 is_container, inherit_flags, object_list,
488 generic_map,token,new_sd)){
489 talloc_free(new_sd);
490 return NULL;
493 return new_sd;