s4:security Change struct security_token->sids from struct dom_sid * to struct dom_sid
[nivanova/samba.git] / source4 / 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.2.2
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 static struct security_acl *calculate_inherited_from_parent(TALLOC_CTX *mem_ctx,
87                                                             struct security_acl *acl,
88                                                             bool is_container,
89                                                             struct dom_sid *owner,
90                                                             struct dom_sid *group,
91                                                             struct GUID *object_list)
92 {
93         int i;
94         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
95         struct security_acl *tmp_acl = talloc_zero(mem_ctx, struct security_acl);
96         struct dom_sid *co, *cg;
97         if (!tmp_acl) {
98                 return NULL;
99         }
100
101         if (!acl) {
102                 return NULL;
103         }
104         co = dom_sid_parse_talloc(tmp_ctx,  SID_CREATOR_OWNER);
105         cg = dom_sid_parse_talloc(tmp_ctx,  SID_CREATOR_GROUP);
106
107         for (i=0; i < acl->num_aces; i++) {
108                 struct security_ace *ace = &acl->aces[i];
109                 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ||
110                     (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT)) {
111                         tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces, struct security_ace,
112                                                        tmp_acl->num_aces+1);
113                         if (tmp_acl->aces == NULL) {
114                                 talloc_free(tmp_ctx);
115                                 return NULL;
116                         }
117
118                         tmp_acl->aces[tmp_acl->num_aces] = *ace;
119                         tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERITED_ACE;
120
121                         if (is_container && (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
122                             tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
123
124                         if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
125                             ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT) {
126                                 if (!object_in_list(object_list, &ace->object.object.type.type)) {
127                                         tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
128                                 }
129
130                         }
131                         tmp_acl->aces[tmp_acl->num_aces].access_mask =
132                                                     map_generic_rights_ds(ace->access_mask);
133                         tmp_acl->num_aces++;
134                         if (is_container) {
135                                 if (!(ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) &&
136                                     ((dom_sid_equal(&ace->trustee, co) || dom_sid_equal(&ace->trustee, cg)))) {
137                                             tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces, struct security_ace,
138                                                                            tmp_acl->num_aces+1);
139                                             if (tmp_acl->aces == NULL) {
140                                                     talloc_free(tmp_ctx);
141                                                     return NULL;
142                                             }
143                                             tmp_acl->aces[tmp_acl->num_aces] = *ace;
144                                             tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_INHERIT_ONLY;
145                                             tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERITED_ACE;
146                                             if (dom_sid_equal(&tmp_acl->aces[tmp_acl->num_aces].trustee, co)) {
147                                                     tmp_acl->aces[tmp_acl->num_aces].trustee = *owner;
148                                             }
149                                             if (dom_sid_equal(&tmp_acl->aces[tmp_acl->num_aces].trustee, cg)) {
150                                                     tmp_acl->aces[tmp_acl->num_aces].trustee = *group;
151                                             }
152                                             tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_CONTAINER_INHERIT;
153                                             tmp_acl->aces[tmp_acl->num_aces].access_mask =
154                                                     map_generic_rights_ds(ace->access_mask);
155                                             tmp_acl->num_aces++;
156                                 }
157                         }
158                 }
159         }
160         if (tmp_acl->num_aces == 0) {
161                 return NULL;
162         }
163         if (acl) {
164                 tmp_acl->revision = acl->revision;
165         }
166         return tmp_acl;
167 }
168
169 static struct security_acl *process_user_acl(TALLOC_CTX *mem_ctx,
170                                              struct security_acl *acl,
171                                              bool is_container,
172                                              struct dom_sid *owner,
173                                              struct dom_sid *group,
174                                              struct GUID *object_list)
175 {
176         int i;
177         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
178         struct security_acl *tmp_acl = talloc_zero(tmp_ctx, struct security_acl);
179         struct security_acl *new_acl;
180         struct dom_sid *co, *cg;
181
182         if (!acl)
183                 return NULL;
184
185         if (!tmp_acl)
186                 return NULL;
187
188         tmp_acl->revision = acl->revision;
189         DEBUG(6,(__location__ ": acl revision %u\n", acl->revision));
190
191         co = dom_sid_parse_talloc(tmp_ctx,  SID_CREATOR_OWNER);
192         cg = dom_sid_parse_talloc(tmp_ctx,  SID_CREATOR_GROUP);
193
194         for (i=0; i < acl->num_aces; i++){
195                 struct security_ace *ace = &acl->aces[i];
196                 if (ace->flags & SEC_ACE_FLAG_INHERITED_ACE)
197                         continue;
198                 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
199                     !(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT ||
200                       ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
201                         continue;
202
203                 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces, struct security_ace,
204                                               tmp_acl->num_aces+1);
205                 tmp_acl->aces[tmp_acl->num_aces] = *ace;
206                 if (dom_sid_equal(&(tmp_acl->aces[tmp_acl->num_aces].trustee), co)) {
207                         tmp_acl->aces[tmp_acl->num_aces].trustee = *owner;
208                         tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_CONTAINER_INHERIT;
209                 }
210                 if (dom_sid_equal(&(tmp_acl->aces[tmp_acl->num_aces].trustee), cg)) {
211                         tmp_acl->aces[tmp_acl->num_aces].trustee = *group;
212                         tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_CONTAINER_INHERIT;
213                         }
214                 tmp_acl->aces[tmp_acl->num_aces].access_mask =
215                         map_generic_rights_ds(tmp_acl->aces[tmp_acl->num_aces].access_mask);
216                 tmp_acl->num_aces++;
217
218                 if (!dom_sid_equal(&ace->trustee, co) && !dom_sid_equal(&ace->trustee, cg))
219                         continue;
220
221                 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces, struct security_ace,
222                                                tmp_acl->num_aces+1);
223                 tmp_acl->aces[tmp_acl->num_aces] = *ace;
224                 tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
225                 tmp_acl->num_aces++;
226         }
227         new_acl = security_acl_dup(mem_ctx,tmp_acl);
228
229         if (new_acl)
230                 new_acl->revision = acl->revision;
231
232         talloc_free(tmp_ctx);
233         return new_acl;
234 }
235
236 static void cr_descr_log_descriptor(struct security_descriptor *sd,
237                                     const char *message,
238                                     int level)
239 {
240         if (sd) {
241                 DEBUG(level,("%s: %s\n", message,
242                              ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,
243                                                      "", sd)));
244         }
245         else {
246                 DEBUG(level,("%s: NULL\n", message));
247         }
248 }
249
250 static void cr_descr_log_acl(struct security_acl *acl,
251                                     const char *message,
252                                     int level)
253 {
254         if (acl) {
255                 DEBUG(level,("%s: %s\n", message,
256                              ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_acl,
257                                                      "", acl)));
258         }
259         else {
260                 DEBUG(level,("%s: NULL\n", message));
261         }
262 }
263
264 static bool compute_acl(struct security_descriptor *parent_sd,
265                         struct security_descriptor *creator_sd,
266                         bool is_container,
267                         uint32_t inherit_flags,
268                         struct GUID *object_list,
269                         uint32_t (*generic_map)(uint32_t access_mask),
270                         struct security_token *token,
271                         struct security_descriptor *new_sd) /* INOUT argument */
272 {
273         struct security_acl *user_dacl, *user_sacl, *inherited_dacl, *inherited_sacl;
274         int level = 10;
275
276         if (!parent_sd || !(inherit_flags & SEC_DACL_AUTO_INHERIT)) {
277                 inherited_dacl = NULL;
278         } else if (creator_sd && (creator_sd->type & SEC_DESC_DACL_PROTECTED)) {
279                 inherited_dacl = NULL;
280         } else {
281                 inherited_dacl = calculate_inherited_from_parent(new_sd,
282                                                                  parent_sd->dacl,
283                                                                  is_container,
284                                                                  new_sd->owner_sid,
285                                                                  new_sd->group_sid,
286                                                                  object_list);
287         }
288
289
290         if (!parent_sd || !(inherit_flags & SEC_SACL_AUTO_INHERIT)) {
291                 inherited_sacl = NULL;
292         } else if (creator_sd && (creator_sd->type & SEC_DESC_SACL_PROTECTED)) {
293                 inherited_sacl = NULL;
294         } else {
295                 inherited_sacl = calculate_inherited_from_parent(new_sd,
296                                                                  parent_sd->sacl,
297                                                                  is_container,
298                                                                  new_sd->owner_sid,
299                                                                  new_sd->group_sid,
300                                                                  object_list);
301         }
302
303         if (!creator_sd || (inherit_flags & SEC_DEFAULT_DESCRIPTOR)) {
304                 user_dacl = NULL;
305                 user_sacl = NULL;
306         } else {
307                 user_dacl = process_user_acl(new_sd,
308                                              creator_sd->dacl,
309                                              is_container,
310                                              new_sd->owner_sid,
311                                              new_sd->group_sid,
312                                              object_list);
313                 user_sacl = process_user_acl(new_sd,
314                                              creator_sd->sacl,
315                                              is_container,
316                                              new_sd->owner_sid,
317                                              new_sd->group_sid,
318                                              object_list);
319         }
320         cr_descr_log_descriptor(parent_sd, __location__"parent_sd", level);
321         cr_descr_log_descriptor(creator_sd,__location__ "creator_sd", level);
322
323         new_sd->dacl = security_acl_concatenate(new_sd, user_dacl, inherited_dacl);
324         if (new_sd->dacl) {
325                 new_sd->type |= SEC_DESC_DACL_PRESENT;
326         }
327         if (inherited_dacl) {
328                 new_sd->type |= SEC_DESC_DACL_AUTO_INHERITED;
329         }
330
331         new_sd->sacl = security_acl_concatenate(new_sd, user_sacl, inherited_sacl);
332         if (new_sd->sacl) {
333                 new_sd->type |= SEC_DESC_SACL_PRESENT;
334         }
335         if (inherited_sacl) {
336                 new_sd->type |= SEC_DESC_SACL_AUTO_INHERITED;
337         }
338         /* This is a hack to handle the fact that
339          * apprantly any AI flag provided by the user is preserved */
340         if (creator_sd)
341                 new_sd->type |= creator_sd->type;
342         cr_descr_log_descriptor(new_sd, __location__"final sd", level);
343         return true;
344 }
345
346 struct security_descriptor *create_security_descriptor(TALLOC_CTX *mem_ctx,
347                                                        struct security_descriptor *parent_sd,
348                                                        struct security_descriptor *creator_sd,
349                                                        bool is_container,
350                                                        struct GUID *object_list,
351                                                        uint32_t inherit_flags,
352                                                        struct security_token *token,
353                                                        struct dom_sid *default_owner, /* valid only for DS, NULL for the other RSs */
354                                                        struct dom_sid *default_group, /* valid only for DS, NULL for the other RSs */
355                                                        uint32_t (*generic_map)(uint32_t access_mask))
356 {
357         struct security_descriptor *new_sd;
358         struct dom_sid *new_owner = NULL;
359         struct dom_sid *new_group = NULL;
360
361         new_sd = security_descriptor_initialise(mem_ctx);
362         if (!new_sd) {
363                 return NULL;
364         }
365
366         if (!creator_sd || !creator_sd->owner_sid) {
367                 if ((inherit_flags & SEC_OWNER_FROM_PARENT) && parent_sd) {
368                         new_owner = parent_sd->owner_sid;
369                 } else if (!default_owner) {
370                         new_owner = &token->sids[PRIMARY_USER_SID_INDEX];
371                 } else {
372                         new_owner = default_owner;
373                         new_sd->type |= SEC_DESC_OWNER_DEFAULTED;
374                 }
375         } else {
376                 new_owner = creator_sd->owner_sid;
377         }
378
379         if (!creator_sd || !creator_sd->group_sid){
380                 if ((inherit_flags & SEC_GROUP_FROM_PARENT) && parent_sd) {
381                         new_group = parent_sd->group_sid;
382                 } else if (!default_group && token->num_sids > PRIMARY_GROUP_SID_INDEX) {
383                         new_group = &token->sids[PRIMARY_GROUP_SID_INDEX];
384                 } else if (!default_group) {
385                         /* This will happen only for anonymous, which has no other groups */
386                         new_group = &token->sids[PRIMARY_USER_SID_INDEX];
387                 } else {
388                         new_group = default_group;
389                         new_sd->type |= SEC_DESC_GROUP_DEFAULTED;
390                 }
391         } else {
392                 new_group = creator_sd->group_sid;
393         }
394
395         new_sd->owner_sid = talloc_memdup(new_sd, new_owner, sizeof(struct dom_sid));
396         new_sd->group_sid = talloc_memdup(new_sd, new_group, sizeof(struct dom_sid));
397         if (!new_sd->owner_sid || !new_sd->group_sid){
398                 talloc_free(new_sd);
399                 return NULL;
400         }
401
402         if (!compute_acl(parent_sd, creator_sd,
403                          is_container, inherit_flags, object_list,
404                          generic_map,token,new_sd)){
405                 talloc_free(new_sd);
406                 return NULL;
407         }
408
409         return new_sd;
410 }