s3-lsasd: Add missing ncalrpc listeners.
[Samba/bjacke.git] / libcli / security / create_descriptor.c
blob92528965d1fb63e86c1c7e290b593929f58ff9ba
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 struct dom_sid *co, *cg;
136 if (!tmp_acl) {
137 return NULL;
140 if (!acl) {
141 return NULL;
143 co = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_OWNER);
144 cg = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_GROUP);
146 for (i=0; i < acl->num_aces; i++) {
147 struct security_ace *ace = &acl->aces[i];
148 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ||
149 (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT)) {
150 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces,
151 struct security_ace,
152 tmp_acl->num_aces+1);
153 if (tmp_acl->aces == NULL) {
154 talloc_free(tmp_ctx);
155 return NULL;
158 tmp_acl->aces[tmp_acl->num_aces] = *ace;
159 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERITED_ACE;
160 /* remove IO flag from the child's ace */
161 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
162 !desc_ace_has_generic(tmp_ctx, ace)) {
163 tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_INHERIT_ONLY;
166 if (is_container && (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
167 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
169 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
170 ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT) {
171 if (!object_in_list(object_list, &ace->object.object.type.type)) {
172 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
176 tmp_acl->num_aces++;
177 if (is_container) {
178 if (!(ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) &&
179 (desc_ace_has_generic(tmp_ctx, ace))) {
180 tmp_acl->aces = talloc_realloc(tmp_acl,
181 tmp_acl->aces,
182 struct security_ace,
183 tmp_acl->num_aces+1);
184 if (tmp_acl->aces == NULL) {
185 talloc_free(tmp_ctx);
186 return NULL;
188 tmp_acl->aces[tmp_acl->num_aces] = *ace;
189 desc_expand_generic(tmp_ctx,
190 &tmp_acl->aces[tmp_acl->num_aces],
191 owner,
192 group);
193 tmp_acl->aces[tmp_acl->num_aces].flags = SEC_ACE_FLAG_INHERITED_ACE;
194 tmp_acl->num_aces++;
199 if (tmp_acl->num_aces == 0) {
200 return NULL;
202 if (acl) {
203 tmp_acl->revision = acl->revision;
205 return tmp_acl;
208 static struct security_acl *process_user_acl(TALLOC_CTX *mem_ctx,
209 struct security_acl *acl,
210 bool is_container,
211 struct dom_sid *owner,
212 struct dom_sid *group,
213 struct GUID *object_list,
214 bool is_protected)
216 uint32_t i;
217 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
218 struct security_acl *tmp_acl = talloc_zero(tmp_ctx, struct security_acl);
219 struct security_acl *new_acl;
220 struct dom_sid *co, *cg;
222 if (!acl)
223 return NULL;
225 if (!tmp_acl)
226 return NULL;
228 tmp_acl->revision = acl->revision;
229 DEBUG(6,(__location__ ": acl revision %d\n", acl->revision));
231 co = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_OWNER);
232 cg = dom_sid_parse_talloc(tmp_ctx, SID_CREATOR_GROUP);
234 for (i=0; i < acl->num_aces; i++){
235 struct security_ace *ace = &acl->aces[i];
236 /* Remove ID flags from user-provided ACEs
237 * if we break inheritance, ignore them otherwise */
238 if (ace->flags & SEC_ACE_FLAG_INHERITED_ACE) {
239 if (is_protected) {
240 ace->flags &= ~SEC_ACE_FLAG_INHERITED_ACE;
241 } else {
242 continue;
246 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
247 !(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT ||
248 ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
249 continue;
251 tmp_acl->aces = talloc_realloc(tmp_acl,
252 tmp_acl->aces,
253 struct security_ace,
254 tmp_acl->num_aces+1);
255 tmp_acl->aces[tmp_acl->num_aces] = *ace;
256 tmp_acl->num_aces++;
257 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
258 continue;
260 /* if the ACE contains CO, CG, GA, GE, GR or GW, and is inheritable
261 * it has to be expanded to two aces, the original as IO,
262 * and another one where these are translated */
263 if (desc_ace_has_generic(tmp_ctx, ace)) {
264 if (!(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
265 desc_expand_generic(tmp_ctx,
266 &tmp_acl->aces[tmp_acl->num_aces-1],
267 owner,
268 group);
269 } else {
270 /*The original ACE becomes read only */
271 tmp_acl->aces[tmp_acl->num_aces-1].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
272 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces,
273 struct security_ace,
274 tmp_acl->num_aces+1);
275 /* add a new ACE with expanded generic info */
276 tmp_acl->aces[tmp_acl->num_aces] = *ace;
277 desc_expand_generic(tmp_ctx,
278 &tmp_acl->aces[tmp_acl->num_aces],
279 owner,
280 group);
281 tmp_acl->num_aces++;
285 new_acl = security_acl_dup(mem_ctx,tmp_acl);
287 if (new_acl)
288 new_acl->revision = acl->revision;
290 talloc_free(tmp_ctx);
291 return new_acl;
294 static void cr_descr_log_descriptor(struct security_descriptor *sd,
295 const char *message,
296 int level)
298 if (sd) {
299 DEBUG(level,("%s: %s\n", message,
300 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,
301 "", sd)));
303 else {
304 DEBUG(level,("%s: NULL\n", message));
308 #if 0
309 static void cr_descr_log_acl(struct security_acl *acl,
310 const char *message,
311 int level)
313 if (acl) {
314 DEBUG(level,("%s: %s\n", message,
315 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_acl,
316 "", acl)));
318 else {
319 DEBUG(level,("%s: NULL\n", message));
322 #endif
324 static bool compute_acl(struct security_descriptor *parent_sd,
325 struct security_descriptor *creator_sd,
326 bool is_container,
327 uint32_t inherit_flags,
328 struct GUID *object_list,
329 uint32_t (*generic_map)(uint32_t access_mask),
330 struct security_token *token,
331 struct security_descriptor *new_sd) /* INOUT argument */
333 struct security_acl *user_dacl, *user_sacl, *inherited_dacl, *inherited_sacl;
334 int level = 10;
336 if (!parent_sd || !(inherit_flags & SEC_DACL_AUTO_INHERIT)) {
337 inherited_dacl = NULL;
338 } else if (creator_sd && (creator_sd->type & SEC_DESC_DACL_PROTECTED)) {
339 inherited_dacl = NULL;
340 } else {
341 inherited_dacl = calculate_inherited_from_parent(new_sd,
342 parent_sd->dacl,
343 is_container,
344 new_sd->owner_sid,
345 new_sd->group_sid,
346 object_list);
350 if (!parent_sd || !(inherit_flags & SEC_SACL_AUTO_INHERIT)) {
351 inherited_sacl = NULL;
352 } else if (creator_sd && (creator_sd->type & SEC_DESC_SACL_PROTECTED)) {
353 inherited_sacl = NULL;
354 } else {
355 inherited_sacl = calculate_inherited_from_parent(new_sd,
356 parent_sd->sacl,
357 is_container,
358 new_sd->owner_sid,
359 new_sd->group_sid,
360 object_list);
363 if (!creator_sd || (inherit_flags & SEC_DEFAULT_DESCRIPTOR)) {
364 user_dacl = NULL;
365 user_sacl = NULL;
366 } else {
367 user_dacl = process_user_acl(new_sd,
368 creator_sd->dacl,
369 is_container,
370 new_sd->owner_sid,
371 new_sd->group_sid,
372 object_list,
373 creator_sd->type & SEC_DESC_DACL_PROTECTED);
374 user_sacl = process_user_acl(new_sd,
375 creator_sd->sacl,
376 is_container,
377 new_sd->owner_sid,
378 new_sd->group_sid,
379 object_list,
380 creator_sd->type & SEC_DESC_SACL_PROTECTED);
382 cr_descr_log_descriptor(parent_sd, __location__"parent_sd", level);
383 cr_descr_log_descriptor(creator_sd,__location__ "creator_sd", level);
385 new_sd->dacl = security_acl_concatenate(new_sd, user_dacl, inherited_dacl);
386 if (new_sd->dacl) {
387 new_sd->type |= SEC_DESC_DACL_PRESENT;
389 if (inherited_dacl) {
390 new_sd->type |= SEC_DESC_DACL_AUTO_INHERITED;
393 new_sd->sacl = security_acl_concatenate(new_sd, user_sacl, inherited_sacl);
394 if (new_sd->sacl) {
395 new_sd->type |= SEC_DESC_SACL_PRESENT;
397 if (inherited_sacl) {
398 new_sd->type |= SEC_DESC_SACL_AUTO_INHERITED;
400 /* This is a hack to handle the fact that
401 * apprantly any AI flag provided by the user is preserved */
402 if (creator_sd)
403 new_sd->type |= creator_sd->type;
404 cr_descr_log_descriptor(new_sd, __location__"final sd", level);
405 return true;
408 struct security_descriptor *create_security_descriptor(TALLOC_CTX *mem_ctx,
409 struct security_descriptor *parent_sd,
410 struct security_descriptor *creator_sd,
411 bool is_container,
412 struct GUID *object_list,
413 uint32_t inherit_flags,
414 struct security_token *token,
415 struct dom_sid *default_owner, /* valid only for DS, NULL for the other RSs */
416 struct dom_sid *default_group, /* valid only for DS, NULL for the other RSs */
417 uint32_t (*generic_map)(uint32_t access_mask))
419 struct security_descriptor *new_sd;
420 struct dom_sid *new_owner = NULL;
421 struct dom_sid *new_group = NULL;
423 new_sd = security_descriptor_initialise(mem_ctx);
424 if (!new_sd) {
425 return NULL;
428 if (!creator_sd || !creator_sd->owner_sid) {
429 if ((inherit_flags & SEC_OWNER_FROM_PARENT) && parent_sd) {
430 new_owner = parent_sd->owner_sid;
431 } else if (!default_owner) {
432 new_owner = &token->sids[PRIMARY_USER_SID_INDEX];
433 } else {
434 new_owner = default_owner;
435 new_sd->type |= SEC_DESC_OWNER_DEFAULTED;
437 } else {
438 new_owner = creator_sd->owner_sid;
441 if (!creator_sd || !creator_sd->group_sid){
442 if ((inherit_flags & SEC_GROUP_FROM_PARENT) && parent_sd) {
443 new_group = parent_sd->group_sid;
444 } else if (!default_group && token->num_sids > PRIMARY_GROUP_SID_INDEX) {
445 new_group = &token->sids[PRIMARY_GROUP_SID_INDEX];
446 } else if (!default_group) {
447 /* This will happen only for anonymous, which has no other groups */
448 new_group = &token->sids[PRIMARY_USER_SID_INDEX];
449 } else {
450 new_group = default_group;
451 new_sd->type |= SEC_DESC_GROUP_DEFAULTED;
453 } else {
454 new_group = creator_sd->group_sid;
457 new_sd->owner_sid = talloc_memdup(new_sd, new_owner, sizeof(struct dom_sid));
458 new_sd->group_sid = talloc_memdup(new_sd, new_group, sizeof(struct dom_sid));
459 if (!new_sd->owner_sid || !new_sd->group_sid){
460 talloc_free(new_sd);
461 return NULL;
464 if (!compute_acl(parent_sd, creator_sd,
465 is_container, inherit_flags, object_list,
466 generic_map,token,new_sd)){
467 talloc_free(new_sd);
468 return NULL;
471 return new_sd;