s3-pysmbd: Correct comments in python VFS bindings
[Samba.git] / libcli / security / create_descriptor.c
blob0cac2e4a5fc656b153a0f60224987e61a679cd36
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 /* returns true if the ACE gontains generic information
87 * that needs to be processed additionally */
89 static bool desc_ace_has_generic(TALLOC_CTX *mem_ctx,
90 struct security_ace *ace)
92 struct dom_sid *co, *cg;
93 co = dom_sid_parse_talloc(mem_ctx, SID_CREATOR_OWNER);
94 cg = dom_sid_parse_talloc(mem_ctx, SID_CREATOR_GROUP);
95 if (ace->access_mask & SEC_GENERIC_ALL || ace->access_mask & SEC_GENERIC_READ ||
96 ace->access_mask & SEC_GENERIC_WRITE || ace->access_mask & SEC_GENERIC_EXECUTE) {
97 return true;
99 if (dom_sid_equal(&ace->trustee, co) || dom_sid_equal(&ace->trustee, cg)) {
100 return true;
102 return false;
105 /* creates an ace in which the generic information is expanded */
107 static void desc_expand_generic(TALLOC_CTX *mem_ctx,
108 struct security_ace *new_ace,
109 struct dom_sid *owner,
110 struct dom_sid *group)
112 struct dom_sid *co, *cg;
113 co = dom_sid_parse_talloc(mem_ctx, SID_CREATOR_OWNER);
114 cg = dom_sid_parse_talloc(mem_ctx, SID_CREATOR_GROUP);
115 new_ace->access_mask = map_generic_rights_ds(new_ace->access_mask);
116 if (dom_sid_equal(&new_ace->trustee, co)) {
117 new_ace->trustee = *owner;
119 if (dom_sid_equal(&new_ace->trustee, cg)) {
120 new_ace->trustee = *group;
122 new_ace->flags = 0x0;
125 static struct security_acl *calculate_inherited_from_parent(TALLOC_CTX *mem_ctx,
126 struct security_acl *acl,
127 bool is_container,
128 struct dom_sid *owner,
129 struct dom_sid *group,
130 struct GUID *object_list)
132 uint32_t i;
133 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
134 struct security_acl *tmp_acl = talloc_zero(mem_ctx, struct security_acl);
135 if (!tmp_acl) {
136 return NULL;
139 if (!acl) {
140 return NULL;
143 for (i=0; i < acl->num_aces; i++) {
144 struct security_ace *ace = &acl->aces[i];
145 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ||
146 (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT)) {
147 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces,
148 struct security_ace,
149 tmp_acl->num_aces+1);
150 if (tmp_acl->aces == NULL) {
151 talloc_free(tmp_ctx);
152 return NULL;
155 tmp_acl->aces[tmp_acl->num_aces] = *ace;
156 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERITED_ACE;
157 /* remove IO flag from the child's ace */
158 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
159 !desc_ace_has_generic(tmp_ctx, ace)) {
160 tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_INHERIT_ONLY;
163 if (is_container && (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
164 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
166 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
167 ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT) {
168 if (!object_in_list(object_list, &ace->object.object.type.type)) {
169 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
173 tmp_acl->num_aces++;
174 if (is_container) {
175 if (!(ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) &&
176 (desc_ace_has_generic(tmp_ctx, ace))) {
177 tmp_acl->aces = talloc_realloc(tmp_acl,
178 tmp_acl->aces,
179 struct security_ace,
180 tmp_acl->num_aces+1);
181 if (tmp_acl->aces == NULL) {
182 talloc_free(tmp_ctx);
183 return NULL;
185 tmp_acl->aces[tmp_acl->num_aces] = *ace;
186 desc_expand_generic(tmp_ctx,
187 &tmp_acl->aces[tmp_acl->num_aces],
188 owner,
189 group);
190 tmp_acl->aces[tmp_acl->num_aces].flags = SEC_ACE_FLAG_INHERITED_ACE;
191 tmp_acl->num_aces++;
196 if (tmp_acl->num_aces == 0) {
197 return NULL;
199 if (acl) {
200 tmp_acl->revision = acl->revision;
202 return tmp_acl;
205 static struct security_acl *process_user_acl(TALLOC_CTX *mem_ctx,
206 struct security_acl *acl,
207 bool is_container,
208 struct dom_sid *owner,
209 struct dom_sid *group,
210 struct GUID *object_list,
211 bool is_protected)
213 uint32_t i;
214 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
215 struct security_acl *tmp_acl = talloc_zero(tmp_ctx, struct security_acl);
216 struct security_acl *new_acl;
218 if (!acl)
219 return NULL;
221 if (!tmp_acl)
222 return NULL;
224 tmp_acl->revision = acl->revision;
225 DEBUG(6,(__location__ ": acl revision %d\n", acl->revision));
227 for (i=0; i < acl->num_aces; i++){
228 struct security_ace *ace = &acl->aces[i];
229 /* Remove ID flags from user-provided ACEs
230 * if we break inheritance, ignore them otherwise */
231 if (ace->flags & SEC_ACE_FLAG_INHERITED_ACE) {
232 if (is_protected) {
233 ace->flags &= ~SEC_ACE_FLAG_INHERITED_ACE;
234 } else {
235 continue;
239 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
240 !(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT ||
241 ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
242 continue;
244 tmp_acl->aces = talloc_realloc(tmp_acl,
245 tmp_acl->aces,
246 struct security_ace,
247 tmp_acl->num_aces+1);
248 tmp_acl->aces[tmp_acl->num_aces] = *ace;
249 tmp_acl->num_aces++;
250 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
251 continue;
253 /* if the ACE contains CO, CG, GA, GE, GR or GW, and is inheritable
254 * it has to be expanded to two aces, the original as IO,
255 * and another one where these are translated */
256 if (desc_ace_has_generic(tmp_ctx, ace)) {
257 if (!(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
258 desc_expand_generic(tmp_ctx,
259 &tmp_acl->aces[tmp_acl->num_aces-1],
260 owner,
261 group);
262 } else {
263 /*The original ACE becomes read only */
264 tmp_acl->aces[tmp_acl->num_aces-1].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
265 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces,
266 struct security_ace,
267 tmp_acl->num_aces+1);
268 /* add a new ACE with expanded generic info */
269 tmp_acl->aces[tmp_acl->num_aces] = *ace;
270 desc_expand_generic(tmp_ctx,
271 &tmp_acl->aces[tmp_acl->num_aces],
272 owner,
273 group);
274 tmp_acl->num_aces++;
278 new_acl = security_acl_dup(mem_ctx,tmp_acl);
280 if (new_acl)
281 new_acl->revision = acl->revision;
283 talloc_free(tmp_ctx);
284 return new_acl;
287 static void cr_descr_log_descriptor(struct security_descriptor *sd,
288 const char *message,
289 int level)
291 if (sd) {
292 DEBUG(level,("%s: %s\n", message,
293 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,
294 "", sd)));
296 else {
297 DEBUG(level,("%s: NULL\n", message));
301 #if 0
302 static void cr_descr_log_acl(struct security_acl *acl,
303 const char *message,
304 int level)
306 if (acl) {
307 DEBUG(level,("%s: %s\n", message,
308 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_acl,
309 "", acl)));
311 else {
312 DEBUG(level,("%s: NULL\n", message));
315 #endif
317 static bool compute_acl(struct security_descriptor *parent_sd,
318 struct security_descriptor *creator_sd,
319 bool is_container,
320 uint32_t inherit_flags,
321 struct GUID *object_list,
322 uint32_t (*generic_map)(uint32_t access_mask),
323 struct security_token *token,
324 struct security_descriptor *new_sd) /* INOUT argument */
326 struct security_acl *user_dacl, *user_sacl, *inherited_dacl, *inherited_sacl;
327 int level = 10;
329 if (!parent_sd || !(inherit_flags & SEC_DACL_AUTO_INHERIT)) {
330 inherited_dacl = NULL;
331 } else if (creator_sd && (creator_sd->type & SEC_DESC_DACL_PROTECTED)) {
332 inherited_dacl = NULL;
333 } else {
334 inherited_dacl = calculate_inherited_from_parent(new_sd,
335 parent_sd->dacl,
336 is_container,
337 new_sd->owner_sid,
338 new_sd->group_sid,
339 object_list);
343 if (!parent_sd || !(inherit_flags & SEC_SACL_AUTO_INHERIT)) {
344 inherited_sacl = NULL;
345 } else if (creator_sd && (creator_sd->type & SEC_DESC_SACL_PROTECTED)) {
346 inherited_sacl = NULL;
347 } else {
348 inherited_sacl = calculate_inherited_from_parent(new_sd,
349 parent_sd->sacl,
350 is_container,
351 new_sd->owner_sid,
352 new_sd->group_sid,
353 object_list);
356 if (!creator_sd || (inherit_flags & SEC_DEFAULT_DESCRIPTOR)) {
357 user_dacl = NULL;
358 user_sacl = NULL;
359 } else {
360 user_dacl = process_user_acl(new_sd,
361 creator_sd->dacl,
362 is_container,
363 new_sd->owner_sid,
364 new_sd->group_sid,
365 object_list,
366 creator_sd->type & SEC_DESC_DACL_PROTECTED);
367 user_sacl = process_user_acl(new_sd,
368 creator_sd->sacl,
369 is_container,
370 new_sd->owner_sid,
371 new_sd->group_sid,
372 object_list,
373 creator_sd->type & SEC_DESC_SACL_PROTECTED);
375 cr_descr_log_descriptor(parent_sd, __location__"parent_sd", level);
376 cr_descr_log_descriptor(creator_sd,__location__ "creator_sd", level);
378 new_sd->dacl = security_acl_concatenate(new_sd, user_dacl, inherited_dacl);
379 if (new_sd->dacl) {
380 new_sd->type |= SEC_DESC_DACL_PRESENT;
382 if (inherited_dacl) {
383 new_sd->type |= SEC_DESC_DACL_AUTO_INHERITED;
386 new_sd->sacl = security_acl_concatenate(new_sd, user_sacl, inherited_sacl);
387 if (new_sd->sacl) {
388 new_sd->type |= SEC_DESC_SACL_PRESENT;
390 if (inherited_sacl) {
391 new_sd->type |= SEC_DESC_SACL_AUTO_INHERITED;
393 /* This is a hack to handle the fact that
394 * apprantly any AI flag provided by the user is preserved */
395 if (creator_sd)
396 new_sd->type |= creator_sd->type;
397 cr_descr_log_descriptor(new_sd, __location__"final sd", level);
398 return true;
401 struct security_descriptor *create_security_descriptor(TALLOC_CTX *mem_ctx,
402 struct security_descriptor *parent_sd,
403 struct security_descriptor *creator_sd,
404 bool is_container,
405 struct GUID *object_list,
406 uint32_t inherit_flags,
407 struct security_token *token,
408 struct dom_sid *default_owner, /* valid only for DS, NULL for the other RSs */
409 struct dom_sid *default_group, /* valid only for DS, NULL for the other RSs */
410 uint32_t (*generic_map)(uint32_t access_mask))
412 struct security_descriptor *new_sd;
413 struct dom_sid *new_owner = NULL;
414 struct dom_sid *new_group = NULL;
416 new_sd = security_descriptor_initialise(mem_ctx);
417 if (!new_sd) {
418 return NULL;
421 if (!creator_sd || !creator_sd->owner_sid) {
422 if ((inherit_flags & SEC_OWNER_FROM_PARENT) && parent_sd) {
423 new_owner = parent_sd->owner_sid;
424 } else if (!default_owner) {
425 new_owner = &token->sids[PRIMARY_USER_SID_INDEX];
426 } else {
427 new_owner = default_owner;
428 new_sd->type |= SEC_DESC_OWNER_DEFAULTED;
430 } else {
431 new_owner = creator_sd->owner_sid;
434 if (!creator_sd || !creator_sd->group_sid){
435 if ((inherit_flags & SEC_GROUP_FROM_PARENT) && parent_sd) {
436 new_group = parent_sd->group_sid;
437 } else if (!default_group && token->num_sids > PRIMARY_GROUP_SID_INDEX) {
438 new_group = &token->sids[PRIMARY_GROUP_SID_INDEX];
439 } else if (!default_group) {
440 /* This will happen only for anonymous, which has no other groups */
441 new_group = &token->sids[PRIMARY_USER_SID_INDEX];
442 } else {
443 new_group = default_group;
444 new_sd->type |= SEC_DESC_GROUP_DEFAULTED;
446 } else {
447 new_group = creator_sd->group_sid;
450 new_sd->owner_sid = talloc_memdup(new_sd, new_owner, sizeof(struct dom_sid));
451 new_sd->group_sid = talloc_memdup(new_sd, new_group, sizeof(struct dom_sid));
452 if (!new_sd->owner_sid || !new_sd->group_sid){
453 talloc_free(new_sd);
454 return NULL;
457 if (!compute_acl(parent_sd, creator_sd,
458 is_container, inherit_flags, object_list,
459 generic_map,token,new_sd)){
460 talloc_free(new_sd);
461 return NULL;
464 return new_sd;