spoolssd: Cast getpid() result to unsigned int for GNU/Solaris build
[Samba/gbeck.git] / libcli / security / create_descriptor.c
blob23e7e9ba60b03917259e23eb18937941e6a57fd9
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(TALLOC_CTX *mem_ctx,
111 struct security_ace *ace)
113 struct dom_sid *co, *cg;
114 co = dom_sid_parse_talloc(mem_ctx, SID_CREATOR_OWNER);
115 cg = dom_sid_parse_talloc(mem_ctx, SID_CREATOR_GROUP);
116 if (ace->access_mask & SEC_GENERIC_ALL || ace->access_mask & SEC_GENERIC_READ ||
117 ace->access_mask & SEC_GENERIC_WRITE || ace->access_mask & SEC_GENERIC_EXECUTE) {
118 return true;
120 if (dom_sid_equal(&ace->trustee, co) || dom_sid_equal(&ace->trustee, cg)) {
121 return true;
123 return false;
126 /* creates an ace in which the generic information is expanded */
128 static void desc_expand_generic(TALLOC_CTX *mem_ctx,
129 struct security_ace *new_ace,
130 struct dom_sid *owner,
131 struct dom_sid *group)
133 struct dom_sid *co, *cg;
134 co = dom_sid_parse_talloc(mem_ctx, SID_CREATOR_OWNER);
135 cg = dom_sid_parse_talloc(mem_ctx, SID_CREATOR_GROUP);
136 new_ace->access_mask = map_generic_rights_ds(new_ace->access_mask);
137 if (dom_sid_equal(&new_ace->trustee, co)) {
138 new_ace->trustee = *owner;
140 if (dom_sid_equal(&new_ace->trustee, cg)) {
141 new_ace->trustee = *group;
143 new_ace->flags = 0x0;
146 static struct security_acl *calculate_inherited_from_parent(TALLOC_CTX *mem_ctx,
147 struct security_acl *acl,
148 bool is_container,
149 struct dom_sid *owner,
150 struct dom_sid *group,
151 struct GUID *object_list)
153 uint32_t i;
154 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
155 struct security_acl *tmp_acl = talloc_zero(mem_ctx, struct security_acl);
156 if (!tmp_acl) {
157 return NULL;
160 if (!acl) {
161 return NULL;
164 for (i=0; i < acl->num_aces; i++) {
165 struct security_ace *ace = &acl->aces[i];
166 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ||
167 (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT)) {
168 struct GUID inherited_object = GUID_zero();
170 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces,
171 struct security_ace,
172 tmp_acl->num_aces+1);
173 if (tmp_acl->aces == NULL) {
174 talloc_free(tmp_ctx);
175 return NULL;
178 tmp_acl->aces[tmp_acl->num_aces] = *ace;
179 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERITED_ACE;
180 /* remove IO flag from the child's ace */
181 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
182 !desc_ace_has_generic(tmp_ctx, ace)) {
183 tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_INHERIT_ONLY;
186 if (is_container && (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
187 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
189 switch (ace->type) {
190 case SEC_ACE_TYPE_ACCESS_ALLOWED:
191 case SEC_ACE_TYPE_ACCESS_DENIED:
192 case SEC_ACE_TYPE_SYSTEM_AUDIT:
193 case SEC_ACE_TYPE_SYSTEM_ALARM:
194 case SEC_ACE_TYPE_ALLOWED_COMPOUND:
195 break;
197 case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
198 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
199 case SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT:
200 case SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT:
201 if (ace->object.object.flags & SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT) {
202 inherited_object = ace->object.object.inherited_type.inherited_type;
205 if (!object_in_list(object_list, &inherited_object)) {
206 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
209 break;
212 tmp_acl->num_aces++;
213 if (is_container) {
214 if (!(ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) &&
215 (desc_ace_has_generic(tmp_ctx, ace))) {
216 tmp_acl->aces = talloc_realloc(tmp_acl,
217 tmp_acl->aces,
218 struct security_ace,
219 tmp_acl->num_aces+1);
220 if (tmp_acl->aces == NULL) {
221 talloc_free(tmp_ctx);
222 return NULL;
224 tmp_acl->aces[tmp_acl->num_aces] = *ace;
225 desc_expand_generic(tmp_ctx,
226 &tmp_acl->aces[tmp_acl->num_aces],
227 owner,
228 group);
229 tmp_acl->aces[tmp_acl->num_aces].flags = SEC_ACE_FLAG_INHERITED_ACE;
230 tmp_acl->num_aces++;
235 if (tmp_acl->num_aces == 0) {
236 return NULL;
238 if (acl) {
239 tmp_acl->revision = acl->revision;
241 return tmp_acl;
244 static struct security_acl *process_user_acl(TALLOC_CTX *mem_ctx,
245 struct security_acl *acl,
246 bool is_container,
247 struct dom_sid *owner,
248 struct dom_sid *group,
249 struct GUID *object_list,
250 bool is_protected)
252 uint32_t i;
253 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
254 struct security_acl *tmp_acl = talloc_zero(tmp_ctx, struct security_acl);
255 struct security_acl *new_acl;
257 if (!acl)
258 return NULL;
260 if (!tmp_acl)
261 return NULL;
263 tmp_acl->revision = acl->revision;
264 DEBUG(6,(__location__ ": acl revision %d\n", acl->revision));
266 for (i=0; i < acl->num_aces; i++){
267 struct security_ace *ace = &acl->aces[i];
268 /* Remove ID flags from user-provided ACEs
269 * if we break inheritance, ignore them otherwise */
270 if (ace->flags & SEC_ACE_FLAG_INHERITED_ACE) {
271 if (is_protected) {
272 ace->flags &= ~SEC_ACE_FLAG_INHERITED_ACE;
273 } else {
274 continue;
278 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
279 !(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT ||
280 ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
281 continue;
283 tmp_acl->aces = talloc_realloc(tmp_acl,
284 tmp_acl->aces,
285 struct security_ace,
286 tmp_acl->num_aces+1);
287 tmp_acl->aces[tmp_acl->num_aces] = *ace;
288 tmp_acl->num_aces++;
289 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
290 continue;
292 /* if the ACE contains CO, CG, GA, GE, GR or GW, and is inheritable
293 * it has to be expanded to two aces, the original as IO,
294 * and another one where these are translated */
295 if (desc_ace_has_generic(tmp_ctx, ace)) {
296 if (!(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
297 desc_expand_generic(tmp_ctx,
298 &tmp_acl->aces[tmp_acl->num_aces-1],
299 owner,
300 group);
301 } else {
302 /*The original ACE becomes read only */
303 tmp_acl->aces[tmp_acl->num_aces-1].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
304 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces,
305 struct security_ace,
306 tmp_acl->num_aces+1);
307 /* add a new ACE with expanded generic info */
308 tmp_acl->aces[tmp_acl->num_aces] = *ace;
309 desc_expand_generic(tmp_ctx,
310 &tmp_acl->aces[tmp_acl->num_aces],
311 owner,
312 group);
313 tmp_acl->num_aces++;
317 new_acl = security_acl_dup(mem_ctx,tmp_acl);
319 if (new_acl)
320 new_acl->revision = acl->revision;
322 talloc_free(tmp_ctx);
323 return new_acl;
326 static void cr_descr_log_descriptor(struct security_descriptor *sd,
327 const char *message,
328 int level)
330 if (sd) {
331 DEBUG(level,("%s: %s\n", message,
332 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,
333 "", sd)));
335 else {
336 DEBUG(level,("%s: NULL\n", message));
340 #if 0
341 static void cr_descr_log_acl(struct security_acl *acl,
342 const char *message,
343 int level)
345 if (acl) {
346 DEBUG(level,("%s: %s\n", message,
347 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_acl,
348 "", acl)));
350 else {
351 DEBUG(level,("%s: NULL\n", message));
354 #endif
356 static bool compute_acl(struct security_descriptor *parent_sd,
357 struct security_descriptor *creator_sd,
358 bool is_container,
359 uint32_t inherit_flags,
360 struct GUID *object_list,
361 uint32_t (*generic_map)(uint32_t access_mask),
362 struct security_token *token,
363 struct security_descriptor *new_sd) /* INOUT argument */
365 struct security_acl *user_dacl, *user_sacl, *inherited_dacl, *inherited_sacl;
366 int level = 10;
368 if (!parent_sd || !(inherit_flags & SEC_DACL_AUTO_INHERIT)) {
369 inherited_dacl = NULL;
370 } else if (creator_sd && (creator_sd->type & SEC_DESC_DACL_PROTECTED)) {
371 inherited_dacl = NULL;
372 } else {
373 inherited_dacl = calculate_inherited_from_parent(new_sd,
374 parent_sd->dacl,
375 is_container,
376 new_sd->owner_sid,
377 new_sd->group_sid,
378 object_list);
382 if (!parent_sd || !(inherit_flags & SEC_SACL_AUTO_INHERIT)) {
383 inherited_sacl = NULL;
384 } else if (creator_sd && (creator_sd->type & SEC_DESC_SACL_PROTECTED)) {
385 inherited_sacl = NULL;
386 } else {
387 inherited_sacl = calculate_inherited_from_parent(new_sd,
388 parent_sd->sacl,
389 is_container,
390 new_sd->owner_sid,
391 new_sd->group_sid,
392 object_list);
395 if (!creator_sd || (inherit_flags & SEC_DEFAULT_DESCRIPTOR)) {
396 user_dacl = NULL;
397 user_sacl = NULL;
398 } else {
399 user_dacl = process_user_acl(new_sd,
400 creator_sd->dacl,
401 is_container,
402 new_sd->owner_sid,
403 new_sd->group_sid,
404 object_list,
405 creator_sd->type & SEC_DESC_DACL_PROTECTED);
406 user_sacl = process_user_acl(new_sd,
407 creator_sd->sacl,
408 is_container,
409 new_sd->owner_sid,
410 new_sd->group_sid,
411 object_list,
412 creator_sd->type & SEC_DESC_SACL_PROTECTED);
414 cr_descr_log_descriptor(parent_sd, __location__"parent_sd", level);
415 cr_descr_log_descriptor(creator_sd,__location__ "creator_sd", level);
417 new_sd->dacl = security_acl_concatenate(new_sd, user_dacl, inherited_dacl);
418 if (new_sd->dacl) {
419 new_sd->type |= SEC_DESC_DACL_PRESENT;
421 if (inherited_dacl) {
422 new_sd->type |= SEC_DESC_DACL_AUTO_INHERITED;
425 new_sd->sacl = security_acl_concatenate(new_sd, user_sacl, inherited_sacl);
426 if (new_sd->sacl) {
427 new_sd->type |= SEC_DESC_SACL_PRESENT;
429 if (inherited_sacl) {
430 new_sd->type |= SEC_DESC_SACL_AUTO_INHERITED;
432 /* This is a hack to handle the fact that
433 * apprantly any AI flag provided by the user is preserved */
434 if (creator_sd)
435 new_sd->type |= creator_sd->type;
436 cr_descr_log_descriptor(new_sd, __location__"final sd", level);
437 return true;
440 struct security_descriptor *create_security_descriptor(TALLOC_CTX *mem_ctx,
441 struct security_descriptor *parent_sd,
442 struct security_descriptor *creator_sd,
443 bool is_container,
444 struct GUID *object_list,
445 uint32_t inherit_flags,
446 struct security_token *token,
447 struct dom_sid *default_owner, /* valid only for DS, NULL for the other RSs */
448 struct dom_sid *default_group, /* valid only for DS, NULL for the other RSs */
449 uint32_t (*generic_map)(uint32_t access_mask))
451 struct security_descriptor *new_sd;
452 struct dom_sid *new_owner = NULL;
453 struct dom_sid *new_group = NULL;
455 new_sd = security_descriptor_initialise(mem_ctx);
456 if (!new_sd) {
457 return NULL;
460 if (!creator_sd || !creator_sd->owner_sid) {
461 if ((inherit_flags & SEC_OWNER_FROM_PARENT) && parent_sd) {
462 new_owner = parent_sd->owner_sid;
463 } else if (!default_owner) {
464 new_owner = &token->sids[PRIMARY_USER_SID_INDEX];
465 } else {
466 new_owner = default_owner;
467 new_sd->type |= SEC_DESC_OWNER_DEFAULTED;
469 } else {
470 new_owner = creator_sd->owner_sid;
473 if (!creator_sd || !creator_sd->group_sid){
474 if ((inherit_flags & SEC_GROUP_FROM_PARENT) && parent_sd) {
475 new_group = parent_sd->group_sid;
476 } else if (!default_group && token->num_sids > PRIMARY_GROUP_SID_INDEX) {
477 new_group = &token->sids[PRIMARY_GROUP_SID_INDEX];
478 } else if (!default_group) {
479 /* This will happen only for anonymous, which has no other groups */
480 new_group = &token->sids[PRIMARY_USER_SID_INDEX];
481 } else {
482 new_group = default_group;
483 new_sd->type |= SEC_DESC_GROUP_DEFAULTED;
485 } else {
486 new_group = creator_sd->group_sid;
489 new_sd->owner_sid = talloc_memdup(new_sd, new_owner, sizeof(struct dom_sid));
490 new_sd->group_sid = talloc_memdup(new_sd, new_group, sizeof(struct dom_sid));
491 if (!new_sd->owner_sid || !new_sd->group_sid){
492 talloc_free(new_sd);
493 return NULL;
496 if (!compute_acl(parent_sd, creator_sd,
497 is_container, inherit_flags, object_list,
498 generic_map,token,new_sd)){
499 talloc_free(new_sd);
500 return NULL;
503 return new_sd;