security: Fixed incorrect indentation in create_descriptor.c
[kai/samba.git] / libcli / security / create_descriptor.c
1 /*
2    Copyright (C) Nadezhda Ivanova 2009
3
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.
8
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.
13
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/>.
16 */
17
18 /*
19  *  Name: create_descriptor
20  *
21  *  Component: routines for calculating and creating security descriptors
22  *  as described in MS-DTYP 2.5.3.x
23  *
24  *  Description:
25  *
26  *
27  *  Author: Nadezhda Ivanova
28  */
29 #include "includes.h"
30 #include "libcli/security/security.h"
31 #include "librpc/gen_ndr/ndr_security.h"
32
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
38  *
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
43  */
44
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
52  */
53
54 uint32_t map_generic_rights_ds(uint32_t access_mask)
55 {
56         if (access_mask & SEC_GENERIC_ALL) {
57                 access_mask |= SEC_ADS_GENERIC_ALL;
58                 access_mask &= ~SEC_GENERIC_ALL;
59         }
60
61         if (access_mask & SEC_GENERIC_EXECUTE) {
62                 access_mask |= SEC_ADS_GENERIC_EXECUTE;
63                 access_mask &= ~SEC_GENERIC_EXECUTE;
64         }
65
66         if (access_mask & SEC_GENERIC_WRITE) {
67                 access_mask |= SEC_ADS_GENERIC_WRITE;
68                 access_mask &= ~SEC_GENERIC_WRITE;
69         }
70
71         if (access_mask & SEC_GENERIC_READ) {
72                 access_mask |= SEC_ADS_GENERIC_READ;
73                 access_mask &= ~SEC_GENERIC_READ;
74         }
75
76         return access_mask;
77 }
78
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)
82 {
83         return true;
84 }
85  
86 /* returns true if the ACE gontains generic information
87  * that needs to be processed additionally */
88  
89 static bool desc_ace_has_generic(TALLOC_CTX *mem_ctx,
90                              struct security_ace *ace)
91 {
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;
98         }
99         if (dom_sid_equal(&ace->trustee, co) || dom_sid_equal(&ace->trustee, cg)) {
100                 return true;
101         }
102         return false;
103 }
104
105 /* creates an ace in which the generic information is expanded */
106
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)
111 {
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;
118         }
119         if (dom_sid_equal(&new_ace->trustee, cg)) {
120                 new_ace->trustee = *group;
121         }
122         new_ace->flags = 0x0;
123 }
124
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)
131 {
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;
138         }
139
140         if (!acl) {
141                 return NULL;
142         }
143         co = dom_sid_parse_talloc(tmp_ctx,  SID_CREATOR_OWNER);
144         cg = dom_sid_parse_talloc(tmp_ctx,  SID_CREATOR_GROUP);
145
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;
156                         }
157
158                         tmp_acl->aces[tmp_acl->num_aces] = *ace;
159                         tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERITED_ACE;
160
161                         if (is_container && (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
162                             tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
163
164                         if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
165                             ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT) {
166                                 if (!object_in_list(object_list, &ace->object.object.type.type)) {
167                                         tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
168                                 }
169
170                         }
171                         tmp_acl->num_aces++;
172                         if (is_container) {
173                                 if (!(ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) &&
174                                     (desc_ace_has_generic(tmp_ctx, ace))) {
175                                             tmp_acl->aces = talloc_realloc(tmp_acl,
176                                                                            tmp_acl->aces,
177                                                                            struct security_ace,
178                                                                            tmp_acl->num_aces+1);
179                                             if (tmp_acl->aces == NULL) {
180                                                     talloc_free(tmp_ctx);
181                                                     return NULL;
182                                             }
183                                             tmp_acl->aces[tmp_acl->num_aces] = *ace;
184                                             desc_expand_generic(tmp_ctx,
185                                                                 &tmp_acl->aces[tmp_acl->num_aces],
186                                                                 owner,
187                                                                 group);
188                                             tmp_acl->aces[tmp_acl->num_aces].flags = SEC_ACE_FLAG_INHERITED_ACE;
189                                             tmp_acl->num_aces++;
190                                 }
191                         }
192                 }
193         }
194         if (tmp_acl->num_aces == 0) {
195                 return NULL;
196         }
197         if (acl) {
198                 tmp_acl->revision = acl->revision;
199         }
200         return tmp_acl;
201 }
202
203 static struct security_acl *process_user_acl(TALLOC_CTX *mem_ctx,
204                                              struct security_acl *acl,
205                                              bool is_container,
206                                              struct dom_sid *owner,
207                                              struct dom_sid *group,
208                                              struct GUID *object_list)
209 {
210         uint32_t i;
211         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
212         struct security_acl *tmp_acl = talloc_zero(tmp_ctx, struct security_acl);
213         struct security_acl *new_acl;
214         struct dom_sid *co, *cg;
215
216         if (!acl)
217                 return NULL;
218
219         if (!tmp_acl)
220                 return NULL;
221
222         tmp_acl->revision = acl->revision;
223         DEBUG(6,(__location__ ": acl revision %u\n", acl->revision));
224
225         co = dom_sid_parse_talloc(tmp_ctx,  SID_CREATOR_OWNER);
226         cg = dom_sid_parse_talloc(tmp_ctx,  SID_CREATOR_GROUP);
227
228         for (i=0; i < acl->num_aces; i++){
229                 struct security_ace *ace = &acl->aces[i];
230                 if (ace->flags & SEC_ACE_FLAG_INHERITED_ACE)
231                         continue;
232                 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
233                     !(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT ||
234                       ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
235                         continue;
236
237                 tmp_acl->aces = talloc_realloc(tmp_acl,
238                                                tmp_acl->aces,
239                                                struct security_ace,
240                                                tmp_acl->num_aces+1);
241                 tmp_acl->aces[tmp_acl->num_aces] = *ace;
242                 tmp_acl->num_aces++;
243                 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
244                         continue;
245                 }
246                 /* if the ACE contains CO, CG, GA, GE, GR or GW, and is inheritable
247                  * it has to be expanded to two aces, the original as IO,
248                  * and another one where these are translated */
249                 if (desc_ace_has_generic(tmp_ctx, ace)) {
250                         if (!(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
251                                 desc_expand_generic(tmp_ctx,
252                                                     &tmp_acl->aces[tmp_acl->num_aces-1],
253                                                     owner,
254                                                     group);
255                         } else {
256                                 /*The original ACE becomes read only */
257                                 tmp_acl->aces[tmp_acl->num_aces-1].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
258                                 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces,
259                                                                struct security_ace,
260                                                                tmp_acl->num_aces+1);
261                                 /* add a new ACE with expanded generic info */
262                                 tmp_acl->aces[tmp_acl->num_aces] = *ace;
263                                 desc_expand_generic(tmp_ctx,
264                                                     &tmp_acl->aces[tmp_acl->num_aces],
265                                                     owner,
266                                                     group);
267                                 tmp_acl->num_aces++;
268                         }
269                 }
270         }
271         new_acl = security_acl_dup(mem_ctx,tmp_acl);
272
273         if (new_acl)
274                 new_acl->revision = acl->revision;
275
276         talloc_free(tmp_ctx);
277         return new_acl;
278 }
279
280 static void cr_descr_log_descriptor(struct security_descriptor *sd,
281                                     const char *message,
282                                     int level)
283 {
284         if (sd) {
285                 DEBUG(level,("%s: %s\n", message,
286                              ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,
287                                                      "", sd)));
288         }
289         else {
290                 DEBUG(level,("%s: NULL\n", message));
291         }
292 }
293
294 static void cr_descr_log_acl(struct security_acl *acl,
295                                     const char *message,
296                                     int level)
297 {
298         if (acl) {
299                 DEBUG(level,("%s: %s\n", message,
300                              ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_acl,
301                                                      "", acl)));
302         }
303         else {
304                 DEBUG(level,("%s: NULL\n", message));
305         }
306 }
307
308 static bool compute_acl(struct security_descriptor *parent_sd,
309                         struct security_descriptor *creator_sd,
310                         bool is_container,
311                         uint32_t inherit_flags,
312                         struct GUID *object_list,
313                         uint32_t (*generic_map)(uint32_t access_mask),
314                         struct security_token *token,
315                         struct security_descriptor *new_sd) /* INOUT argument */
316 {
317         struct security_acl *user_dacl, *user_sacl, *inherited_dacl, *inherited_sacl;
318         int level = 10;
319
320         if (!parent_sd || !(inherit_flags & SEC_DACL_AUTO_INHERIT)) {
321                 inherited_dacl = NULL;
322         } else if (creator_sd && (creator_sd->type & SEC_DESC_DACL_PROTECTED)) {
323                 inherited_dacl = NULL;
324         } else {
325                 inherited_dacl = calculate_inherited_from_parent(new_sd,
326                                                                  parent_sd->dacl,
327                                                                  is_container,
328                                                                  new_sd->owner_sid,
329                                                                  new_sd->group_sid,
330                                                                  object_list);
331         }
332
333
334         if (!parent_sd || !(inherit_flags & SEC_SACL_AUTO_INHERIT)) {
335                 inherited_sacl = NULL;
336         } else if (creator_sd && (creator_sd->type & SEC_DESC_SACL_PROTECTED)) {
337                 inherited_sacl = NULL;
338         } else {
339                 inherited_sacl = calculate_inherited_from_parent(new_sd,
340                                                                  parent_sd->sacl,
341                                                                  is_container,
342                                                                  new_sd->owner_sid,
343                                                                  new_sd->group_sid,
344                                                                  object_list);
345         }
346
347         if (!creator_sd || (inherit_flags & SEC_DEFAULT_DESCRIPTOR)) {
348                 user_dacl = NULL;
349                 user_sacl = NULL;
350         } else {
351                 user_dacl = process_user_acl(new_sd,
352                                              creator_sd->dacl,
353                                              is_container,
354                                              new_sd->owner_sid,
355                                              new_sd->group_sid,
356                                              object_list);
357                 user_sacl = process_user_acl(new_sd,
358                                              creator_sd->sacl,
359                                              is_container,
360                                              new_sd->owner_sid,
361                                              new_sd->group_sid,
362                                              object_list);
363         }
364         cr_descr_log_descriptor(parent_sd, __location__"parent_sd", level);
365         cr_descr_log_descriptor(creator_sd,__location__ "creator_sd", level);
366
367         new_sd->dacl = security_acl_concatenate(new_sd, user_dacl, inherited_dacl);
368         if (new_sd->dacl) {
369                 new_sd->type |= SEC_DESC_DACL_PRESENT;
370         }
371         if (inherited_dacl) {
372                 new_sd->type |= SEC_DESC_DACL_AUTO_INHERITED;
373         }
374
375         new_sd->sacl = security_acl_concatenate(new_sd, user_sacl, inherited_sacl);
376         if (new_sd->sacl) {
377                 new_sd->type |= SEC_DESC_SACL_PRESENT;
378         }
379         if (inherited_sacl) {
380                 new_sd->type |= SEC_DESC_SACL_AUTO_INHERITED;
381         }
382         /* This is a hack to handle the fact that
383          * apprantly any AI flag provided by the user is preserved */
384         if (creator_sd)
385                 new_sd->type |= creator_sd->type;
386         cr_descr_log_descriptor(new_sd, __location__"final sd", level);
387         return true;
388 }
389
390 struct security_descriptor *create_security_descriptor(TALLOC_CTX *mem_ctx,
391                                                        struct security_descriptor *parent_sd,
392                                                        struct security_descriptor *creator_sd,
393                                                        bool is_container,
394                                                        struct GUID *object_list,
395                                                        uint32_t inherit_flags,
396                                                        struct security_token *token,
397                                                        struct dom_sid *default_owner, /* valid only for DS, NULL for the other RSs */
398                                                        struct dom_sid *default_group, /* valid only for DS, NULL for the other RSs */
399                                                        uint32_t (*generic_map)(uint32_t access_mask))
400 {
401         struct security_descriptor *new_sd;
402         struct dom_sid *new_owner = NULL;
403         struct dom_sid *new_group = NULL;
404
405         new_sd = security_descriptor_initialise(mem_ctx);
406         if (!new_sd) {
407                 return NULL;
408         }
409
410         if (!creator_sd || !creator_sd->owner_sid) {
411                 if ((inherit_flags & SEC_OWNER_FROM_PARENT) && parent_sd) {
412                         new_owner = parent_sd->owner_sid;
413                 } else if (!default_owner) {
414                         new_owner = &token->sids[PRIMARY_USER_SID_INDEX];
415                 } else {
416                         new_owner = default_owner;
417                         new_sd->type |= SEC_DESC_OWNER_DEFAULTED;
418                 }
419         } else {
420                 new_owner = creator_sd->owner_sid;
421         }
422
423         if (!creator_sd || !creator_sd->group_sid){
424                 if ((inherit_flags & SEC_GROUP_FROM_PARENT) && parent_sd) {
425                         new_group = parent_sd->group_sid;
426                 } else if (!default_group && token->num_sids > PRIMARY_GROUP_SID_INDEX) {
427                         new_group = &token->sids[PRIMARY_GROUP_SID_INDEX];
428                 } else if (!default_group) {
429                         /* This will happen only for anonymous, which has no other groups */
430                         new_group = &token->sids[PRIMARY_USER_SID_INDEX];
431                 } else {
432                         new_group = default_group;
433                         new_sd->type |= SEC_DESC_GROUP_DEFAULTED;
434                 }
435         } else {
436                 new_group = creator_sd->group_sid;
437         }
438
439         new_sd->owner_sid = talloc_memdup(new_sd, new_owner, sizeof(struct dom_sid));
440         new_sd->group_sid = talloc_memdup(new_sd, new_group, sizeof(struct dom_sid));
441         if (!new_sd->owner_sid || !new_sd->group_sid){
442                 talloc_free(new_sd);
443                 return NULL;
444         }
445
446         if (!compute_acl(parent_sd, creator_sd,
447                          is_container, inherit_flags, object_list,
448                          generic_map,token,new_sd)){
449                 talloc_free(new_sd);
450                 return NULL;
451         }
452
453         return new_sd;
454 }