LCOV - code coverage report
Current view: top level - libcli/security - create_descriptor.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 204 248 82.3 %
Date: 2024-06-13 04:01:37 Functions: 9 9 100.0 %

          Line data    Source code
       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       25420 : uint32_t map_generic_rights_ds(uint32_t access_mask)
      55             : {
      56       25420 :         if (access_mask & SEC_GENERIC_ALL) {
      57          84 :                 access_mask |= SEC_ADS_GENERIC_ALL;
      58          84 :                 access_mask &= ~SEC_GENERIC_ALL;
      59             :         }
      60             : 
      61       25420 :         if (access_mask & SEC_GENERIC_EXECUTE) {
      62           0 :                 access_mask |= SEC_ADS_GENERIC_EXECUTE;
      63           0 :                 access_mask &= ~SEC_GENERIC_EXECUTE;
      64             :         }
      65             : 
      66       25420 :         if (access_mask & SEC_GENERIC_WRITE) {
      67           0 :                 access_mask |= SEC_ADS_GENERIC_WRITE;
      68           0 :                 access_mask &= ~SEC_GENERIC_WRITE;
      69             :         }
      70             : 
      71       25420 :         if (access_mask & SEC_GENERIC_READ) {
      72           0 :                 access_mask |= SEC_ADS_GENERIC_READ;
      73           0 :                 access_mask &= ~SEC_GENERIC_READ;
      74             :         }
      75             : 
      76       25420 :         return access_mask;
      77             : }
      78             : 
      79             : /* Not sure what this has to be,
      80             : * and it does not seem to have any influence */
      81     2474894 : static bool object_in_list(const struct GUID *object_list, const struct GUID *object)
      82             : {
      83             :         size_t i;
      84             : 
      85     2474894 :         if (object_list == NULL) {
      86           0 :                 return true;
      87             :         }
      88             : 
      89     2474894 :         if (GUID_all_zero(object)) {
      90           0 :                 return true;
      91             :         }
      92             : 
      93     4667875 :         for (i=0; ; i++) {
      94     6678178 :                 if (GUID_all_zero(&object_list[i])) {
      95     2192981 :                         return false;
      96             :                 }
      97     2474894 :                 if (!GUID_equal(&object_list[i], object)) {
      98     2192981 :                         continue;
      99             :                 }
     100             : 
     101      281913 :                 return true;
     102             :         }
     103             : 
     104             :         return false;
     105             : }
     106             : 
     107             : /* returns true if the ACE gontains generic information
     108             :  * that needs to be processed additionally */
     109             :  
     110     7247107 : static bool desc_ace_has_generic(const struct security_ace *ace)
     111             : {
     112    13015106 :         if (ace->access_mask & SEC_GENERIC_ALL || ace->access_mask & SEC_GENERIC_READ ||
     113    13015022 :             ace->access_mask & SEC_GENERIC_WRITE || ace->access_mask & SEC_GENERIC_EXECUTE) {
     114          84 :                 return true;
     115             :         }
     116    14468710 :         if (dom_sid_equal(&ace->trustee, &global_sid_Creator_Owner) ||
     117     7221687 :             dom_sid_equal(&ace->trustee, &global_sid_Creator_Group)) {
     118       25336 :                 return true;
     119             :         }
     120     7221687 :         return false;
     121             : }
     122             : 
     123             : /* creates an ace in which the generic information is expanded */
     124             : 
     125       25420 : static void desc_expand_generic(struct security_ace *new_ace,
     126             :                                 struct dom_sid *owner,
     127             :                                 struct dom_sid *group)
     128             : {
     129       25420 :         new_ace->access_mask = map_generic_rights_ds(new_ace->access_mask);
     130       25420 :         if (dom_sid_equal(&new_ace->trustee, &global_sid_Creator_Owner)) {
     131       25336 :                 new_ace->trustee = *owner;
     132             :         }
     133       25420 :         if (dom_sid_equal(&new_ace->trustee, &global_sid_Creator_Group)) {
     134           0 :                 new_ace->trustee = *group;
     135             :         }
     136       25420 :         new_ace->flags = 0x0;
     137       25420 : }
     138             : 
     139     1962564 : static struct security_acl *calculate_inherited_from_parent(TALLOC_CTX *mem_ctx,
     140             :                                                             struct security_acl *acl,
     141             :                                                             bool is_container,
     142             :                                                             struct dom_sid *owner,
     143             :                                                             struct dom_sid *group,
     144             :                                                             struct GUID *object_list)
     145             : {
     146             :         uint32_t i;
     147     1962564 :         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
     148     1962564 :         struct security_acl *tmp_acl = talloc_zero(mem_ctx, struct security_acl);
     149     1962564 :         if (!tmp_acl) {
     150           0 :                 return NULL;
     151             :         }
     152             : 
     153     1962564 :         if (!acl) {
     154      350755 :                 return NULL;
     155             :         }
     156             : 
     157    20469209 :         for (i=0; i < acl->num_aces; i++) {
     158    18857400 :                 const struct security_ace *ace = &acl->aces[i];
     159    18857400 :                 const struct GUID *inherited_object = NULL;
     160    18857400 :                 const struct GUID *inherited_property = NULL;
     161    18857400 :                 struct security_ace *tmp_ace = NULL;
     162    18857400 :                 bool applies = false;
     163    18857400 :                 bool inherited_only = false;
     164    18857400 :                 bool expand_ace = false;
     165    18857400 :                 bool expand_only = false;
     166             : 
     167    18857400 :                 if (is_container && (ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
     168     6858832 :                         applies = true;
     169    11998568 :                 } else if (!is_container && (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT)) {
     170           0 :                         applies = true;
     171             :                 }
     172             : 
     173    18857400 :                 if (!applies) {
     174             :                         /*
     175             :                          * If the ace doesn't apply to the
     176             :                          * current node, we should only keep
     177             :                          * it as SEC_ACE_FLAG_OBJECT_INHERIT
     178             :                          * on a container. We'll add
     179             :                          * SEC_ACE_FLAG_INHERITED_ACE
     180             :                          * and SEC_ACE_FLAG_INHERIT_ONLY below.
     181             :                          *
     182             :                          * Otherwise we should completely ignore it.
     183             :                          */
     184    11998568 :                         if (!(ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT)) {
     185    11998514 :                                 continue;
     186             :                         }
     187             :                 }
     188             : 
     189     6858886 :                 switch (ace->type) {
     190     4245099 :                 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     4245099 :                         break;
     196             : 
     197     2613787 :                 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     2613787 :                         if (ace->object.object.flags & SEC_ACE_OBJECT_TYPE_PRESENT) {
     202     2208472 :                                 inherited_property = &ace->object.object.type.type;
     203             :                         }
     204     2613787 :                         if (ace->object.object.flags & SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT) {
     205     2474894 :                                 inherited_object = &ace->object.object.inherited_type.inherited_type;
     206             :                         }
     207             : 
     208     2613787 :                         if (inherited_object != NULL && !object_in_list(object_list, inherited_object)) {
     209             :                                 /*
     210             :                                  * An explicit object class schemaId is given,
     211             :                                  * but doesn't belong to the current object.
     212             :                                  */
     213     2192981 :                                 applies = false;
     214             :                         }
     215             : 
     216     2613787 :                         break;
     217             :                 }
     218             : 
     219     6858886 :                 if (ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
     220           0 :                         if (!applies) {
     221             :                                 /*
     222             :                                  * If the ACE doesn't apply to
     223             :                                  * the current object, we should
     224             :                                  * ignore it as it should not be
     225             :                                  * inherited any further
     226             :                                  */
     227           0 :                                 continue;
     228             :                         }
     229             :                         /*
     230             :                          * We should only keep the expanded version
     231             :                          * of the ACE on the current object.
     232             :                          */
     233           0 :                         expand_ace = true;
     234           0 :                         expand_only = true;
     235     6858886 :                 } else if (applies) {
     236             :                         /*
     237             :                          * We check if should also add
     238             :                          * the expanded version of the ACE
     239             :                          * in addition, in case we should
     240             :                          * expand generic access bits or
     241             :                          * special sids.
     242             :                          *
     243             :                          * In that case we need to
     244             :                          * keep the original ACE with
     245             :                          * SEC_ACE_FLAG_INHERIT_ONLY.
     246             :                          */
     247     4665851 :                         expand_ace = desc_ace_has_generic(ace);
     248     4665851 :                         if (expand_ace) {
     249        1663 :                                 inherited_only = true;
     250             :                         }
     251             :                 } else {
     252             :                         /*
     253             :                          * If the ACE doesn't apply
     254             :                          * to the current object,
     255             :                          * we need to keep it with
     256             :                          * SEC_ACE_FLAG_INHERIT_ONLY
     257             :                          * in order to apply them to
     258             :                          * grandchildren
     259             :                          */
     260     2193035 :                         inherited_only = true;
     261             :                 }
     262             : 
     263     6858886 :                 if (expand_ace) {
     264        1663 :                         tmp_acl->aces = talloc_realloc(tmp_acl,
     265             :                                                        tmp_acl->aces,
     266             :                                                        struct security_ace,
     267             :                                                        tmp_acl->num_aces+1);
     268        1663 :                         if (tmp_acl->aces == NULL) {
     269           0 :                                 talloc_free(tmp_ctx);
     270           0 :                                 return NULL;
     271             :                         }
     272             : 
     273        1663 :                         tmp_ace = &tmp_acl->aces[tmp_acl->num_aces];
     274        1663 :                         tmp_acl->num_aces++;
     275             : 
     276        1663 :                         *tmp_ace = *ace;
     277             : 
     278             :                         /*
     279             :                          * Expand generic access bits as well as special
     280             :                          * sids.
     281             :                          */
     282        1663 :                         desc_expand_generic(tmp_ace, owner, group);
     283             : 
     284             :                         /*
     285             :                          * Expanded ACEs are marked as inherited,
     286             :                          * but never inherited any further to
     287             :                          * grandchildren.
     288             :                          */
     289        1663 :                         tmp_ace->flags |= SEC_ACE_FLAG_INHERITED_ACE;
     290        1663 :                         tmp_ace->flags &= ~SEC_ACE_FLAG_CONTAINER_INHERIT;
     291        1663 :                         tmp_ace->flags &= ~SEC_ACE_FLAG_OBJECT_INHERIT;
     292        1663 :                         tmp_ace->flags &= ~SEC_ACE_FLAG_NO_PROPAGATE_INHERIT;
     293             : 
     294             :                         /*
     295             :                          * Expanded ACEs never have an explicit
     296             :                          * object class schemaId, so clear it
     297             :                          * if present.
     298             :                          */
     299        1663 :                         if (inherited_object != NULL) {
     300           0 :                                 tmp_ace->object.object.flags &= ~SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT;
     301             :                         }
     302             : 
     303             :                         /*
     304             :                          * If the ACE had an explicit object class
     305             :                          * schemaId, but no attribute/propertySet
     306             :                          * we need to downgrate the _OBJECT variants
     307             :                          * to the normal ones.
     308             :                          */
     309        1663 :                         if (inherited_property == NULL) {
     310        1663 :                                 switch (tmp_ace->type) {
     311        1663 :                                 case SEC_ACE_TYPE_ACCESS_ALLOWED:
     312             :                                 case SEC_ACE_TYPE_ACCESS_DENIED:
     313             :                                 case SEC_ACE_TYPE_SYSTEM_AUDIT:
     314             :                                 case SEC_ACE_TYPE_SYSTEM_ALARM:
     315             :                                 case SEC_ACE_TYPE_ALLOWED_COMPOUND:
     316        1663 :                                         break;
     317           0 :                                 case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
     318           0 :                                         tmp_ace->type = SEC_ACE_TYPE_ACCESS_ALLOWED;
     319           0 :                                         break;
     320           0 :                                 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
     321           0 :                                         tmp_ace->type = SEC_ACE_TYPE_ACCESS_DENIED;
     322           0 :                                         break;
     323           0 :                                 case SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT:
     324           0 :                                         tmp_ace->type = SEC_ACE_TYPE_SYSTEM_ALARM;
     325           0 :                                         break;
     326           0 :                                 case SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT:
     327           0 :                                         tmp_ace->type = SEC_ACE_TYPE_SYSTEM_AUDIT;
     328           0 :                                         break;
     329             :                                 }
     330             :                         }
     331             : 
     332        1663 :                         if (expand_only) {
     333           0 :                                 continue;
     334             :                         }
     335             :                 }
     336             : 
     337     6858886 :                 tmp_acl->aces = talloc_realloc(tmp_acl,
     338             :                                                tmp_acl->aces,
     339             :                                                struct security_ace,
     340             :                                                tmp_acl->num_aces+1);
     341     6858886 :                 if (tmp_acl->aces == NULL) {
     342           0 :                         talloc_free(tmp_ctx);
     343           0 :                         return NULL;
     344             :                 }
     345             : 
     346     6858886 :                 tmp_ace = &tmp_acl->aces[tmp_acl->num_aces];
     347     6858886 :                 tmp_acl->num_aces++;
     348             : 
     349     6858886 :                 *tmp_ace = *ace;
     350     6858886 :                 tmp_ace->flags |= SEC_ACE_FLAG_INHERITED_ACE;
     351             : 
     352     6858886 :                 if (inherited_only) {
     353     2194698 :                         tmp_ace->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
     354             :                 } else {
     355     4664188 :                         tmp_ace->flags &= ~SEC_ACE_FLAG_INHERIT_ONLY;
     356             :                 }
     357             : 
     358     6858886 :                 if (ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
     359           0 :                         tmp_ace->flags &= ~SEC_ACE_FLAG_CONTAINER_INHERIT;
     360           0 :                         tmp_ace->flags &= ~SEC_ACE_FLAG_OBJECT_INHERIT;
     361           0 :                         tmp_ace->flags &= ~SEC_ACE_FLAG_NO_PROPAGATE_INHERIT;
     362             :                 }
     363             :         }
     364     1611809 :         if (tmp_acl->num_aces == 0) {
     365       17652 :                 return NULL;
     366             :         }
     367     1594157 :         if (acl) {
     368     1594157 :                 tmp_acl->revision = acl->revision;
     369             :         }
     370     1594157 :         return tmp_acl;
     371             : }
     372             : 
     373     1998180 : static struct security_acl *process_user_acl(TALLOC_CTX *mem_ctx,
     374             :                                              struct security_acl *acl,
     375             :                                              bool is_container,
     376             :                                              struct dom_sid *owner,
     377             :                                              struct dom_sid *group,
     378             :                                              struct GUID *object_list,
     379             :                                              bool is_protected)
     380             : {
     381             :         uint32_t i;
     382     1998180 :         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
     383     1998180 :         struct security_acl *tmp_acl = talloc_zero(tmp_ctx, struct security_acl);
     384             :         struct security_acl *new_acl;
     385             : 
     386     1998180 :         if (!acl)
     387      436237 :                 return NULL;
     388             : 
     389     1561943 :         if (!tmp_acl)
     390           0 :                 return NULL;
     391             : 
     392     1561943 :         tmp_acl->revision = acl->revision;
     393     1561943 :         DBG_DEBUG("acl revision %d\n", acl->revision);
     394             : 
     395     8772362 :         for (i=0; i < acl->num_aces; i++){
     396     7210419 :                 struct security_ace *ace = &acl->aces[i];
     397             :                 /* Remove ID flags from user-provided ACEs
     398             :                  * if we break inheritance, ignore them otherwise */
     399     7210419 :                 if (ace->flags & SEC_ACE_FLAG_INHERITED_ACE) {
     400     4613908 :                         if (is_protected) {
     401           9 :                                 ace->flags &= ~SEC_ACE_FLAG_INHERITED_ACE;
     402             :                         } else {
     403     4613899 :                                 continue;
     404             :                         }
     405             :                 }
     406             : 
     407     2610541 :                 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
     408       15332 :                     !(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT ||
     409          76 :                       ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
     410          76 :                         continue;
     411             : 
     412     2596444 :                 tmp_acl->aces = talloc_realloc(tmp_acl,
     413             :                                                tmp_acl->aces,
     414             :                                                struct security_ace,
     415             :                                                tmp_acl->num_aces+1);
     416     2596444 :                 tmp_acl->aces[tmp_acl->num_aces] = *ace;
     417     2596444 :                 tmp_acl->num_aces++;
     418     2596444 :                 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
     419       15188 :                         continue;
     420             :                 }
     421             :                 /* if the ACE contains CO, CG, GA, GE, GR or GW, and is inheritable
     422             :                  * it has to be expanded to two aces, the original as IO,
     423             :                  * and another one where these are translated */
     424     2581256 :                 if (desc_ace_has_generic(ace)) {
     425       23757 :                         if (!(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
     426       23393 :                                 desc_expand_generic(&tmp_acl->aces[tmp_acl->num_aces-1],
     427             :                                                     owner,
     428             :                                                     group);
     429             :                         } else {
     430             :                                 /*The original ACE becomes read only */
     431         364 :                                 tmp_acl->aces[tmp_acl->num_aces-1].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
     432         364 :                                 tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces,
     433             :                                                                struct security_ace,
     434             :                                                                tmp_acl->num_aces+1);
     435             :                                 /* add a new ACE with expanded generic info */
     436         364 :                                 tmp_acl->aces[tmp_acl->num_aces] = *ace;
     437         364 :                                 desc_expand_generic(&tmp_acl->aces[tmp_acl->num_aces],
     438             :                                                     owner,
     439             :                                                     group);
     440         364 :                                 tmp_acl->num_aces++;
     441             :                         }
     442             :                 }
     443             :         }
     444     1561943 :         new_acl = security_acl_dup(mem_ctx,tmp_acl);
     445             : 
     446     1561943 :         if (new_acl)
     447     1561943 :                 new_acl->revision = acl->revision;
     448             : 
     449     1561943 :         talloc_free(tmp_ctx);
     450     1561943 :         return new_acl;
     451             : }
     452             : 
     453     2997342 : static void cr_descr_log_descriptor(struct security_descriptor *sd,
     454             :                                     const char *message,
     455             :                                     int level)
     456             : {
     457     2997342 :         if (sd) {
     458     2996402 :                 DEBUG(level,("%s: %s\n", message,
     459             :                              ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,
     460             :                                                      "", sd)));
     461             :         }
     462             :         else {
     463         940 :                 DEBUG(level,("%s: NULL\n", message));
     464             :         }
     465     2997342 : }
     466             : 
     467             : #if 0
     468             : static void cr_descr_log_acl(struct security_acl *acl,
     469             :                                     const char *message,
     470             :                                     int level)
     471             : {
     472             :         if (acl) {
     473             :                 DEBUG(level,("%s: %s\n", message,
     474             :                              ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_acl,
     475             :                                                      "", acl)));
     476             :         }
     477             :         else {
     478             :                 DEBUG(level,("%s: NULL\n", message));
     479             :         }
     480             : }
     481             : #endif
     482             : 
     483      999114 : static bool compute_acl(struct security_descriptor *parent_sd,
     484             :                         struct security_descriptor *creator_sd,
     485             :                         bool is_container,
     486             :                         uint32_t inherit_flags,
     487             :                         struct GUID *object_list,
     488             :                         uint32_t (*generic_map)(uint32_t access_mask),
     489             :                         struct security_token *token,
     490             :                         struct security_descriptor *new_sd) /* INOUT argument */
     491             : {
     492             :         struct security_acl *user_dacl, *user_sacl, *inherited_dacl, *inherited_sacl;
     493      999114 :         int level = 10;
     494             : 
     495      999114 :         if (!parent_sd || !(inherit_flags & SEC_DACL_AUTO_INHERIT)) {
     496         916 :                 inherited_dacl = NULL;
     497      998198 :         } else if (creator_sd && (creator_sd->type & SEC_DESC_DACL_PROTECTED)) {
     498       19553 :                 inherited_dacl = NULL;
     499             :         } else {
     500      978645 :                 inherited_dacl = calculate_inherited_from_parent(new_sd,
     501             :                                                                  parent_sd->dacl,
     502             :                                                                  is_container,
     503             :                                                                  new_sd->owner_sid,
     504             :                                                                  new_sd->group_sid,
     505             :                                                                  object_list);
     506             :         }
     507             : 
     508             : 
     509      999114 :         if (!parent_sd || !(inherit_flags & SEC_SACL_AUTO_INHERIT)) {
     510         916 :                 inherited_sacl = NULL;
     511      998198 :         } else if (creator_sd && (creator_sd->type & SEC_DESC_SACL_PROTECTED)) {
     512       14279 :                 inherited_sacl = NULL;
     513             :         } else {
     514      983919 :                 inherited_sacl = calculate_inherited_from_parent(new_sd,
     515             :                                                                  parent_sd->sacl,
     516             :                                                                  is_container,
     517             :                                                                  new_sd->owner_sid,
     518             :                                                                  new_sd->group_sid,
     519             :                                                                  object_list);
     520             :         }
     521             : 
     522      999114 :         if (!creator_sd || (inherit_flags & SEC_DEFAULT_DESCRIPTOR)) {
     523          24 :                 user_dacl = NULL;
     524          24 :                 user_sacl = NULL;
     525             :         } else {
     526      999090 :                 user_dacl = process_user_acl(new_sd,
     527             :                                              creator_sd->dacl,
     528             :                                              is_container,
     529             :                                              new_sd->owner_sid,
     530             :                                              new_sd->group_sid,
     531             :                                              object_list,
     532      999090 :                                              creator_sd->type & SEC_DESC_DACL_PROTECTED);
     533      999090 :                 user_sacl = process_user_acl(new_sd,
     534             :                                              creator_sd->sacl,
     535             :                                              is_container,
     536             :                                              new_sd->owner_sid,
     537             :                                              new_sd->group_sid,
     538             :                                              object_list,
     539      999090 :                                              creator_sd->type & SEC_DESC_SACL_PROTECTED);
     540             :         }
     541      999114 :         cr_descr_log_descriptor(parent_sd, __location__"parent_sd", level);
     542      999114 :         cr_descr_log_descriptor(creator_sd,__location__ "creator_sd", level);
     543             : 
     544      999114 :         new_sd->dacl = security_acl_concatenate(new_sd, user_dacl, inherited_dacl);
     545      999114 :         if (new_sd->dacl) {
     546      999006 :                 new_sd->type |= SEC_DESC_DACL_PRESENT;
     547             :         }
     548      999114 :         if (inherited_dacl) {
     549      962998 :                 new_sd->type |= SEC_DESC_DACL_AUTO_INHERITED;
     550             :         }
     551             : 
     552      999114 :         new_sd->sacl = security_acl_concatenate(new_sd, user_sacl, inherited_sacl);
     553      999114 :         if (new_sd->sacl) {
     554      632696 :                 new_sd->type |= SEC_DESC_SACL_PRESENT;
     555             :         }
     556      999114 :         if (inherited_sacl) {
     557      631159 :                 new_sd->type |= SEC_DESC_SACL_AUTO_INHERITED;
     558             :         }
     559             :         /* This is a hack to handle the fact that
     560             :          * apprantly any AI flag provided by the user is preserved */
     561      999114 :         if (creator_sd)
     562      999090 :                 new_sd->type |= creator_sd->type;
     563      999114 :         cr_descr_log_descriptor(new_sd, __location__"final sd", level);
     564      999114 :         return true;
     565             : }
     566             : 
     567      999114 : struct security_descriptor *create_security_descriptor(TALLOC_CTX *mem_ctx,
     568             :                                                        struct security_descriptor *parent_sd,
     569             :                                                        struct security_descriptor *creator_sd,
     570             :                                                        bool is_container,
     571             :                                                        struct GUID *object_list,
     572             :                                                        uint32_t inherit_flags,
     573             :                                                        struct security_token *token,
     574             :                                                        struct dom_sid *default_owner, /* valid only for DS, NULL for the other RSs */
     575             :                                                        struct dom_sid *default_group, /* valid only for DS, NULL for the other RSs */
     576             :                                                        uint32_t (*generic_map)(uint32_t access_mask))
     577             : {
     578             :         struct security_descriptor *new_sd;
     579      999114 :         struct dom_sid *new_owner = NULL;
     580      999114 :         struct dom_sid *new_group = NULL;
     581             : 
     582      999114 :         new_sd = security_descriptor_initialise(mem_ctx);
     583      999114 :         if (!new_sd) {
     584           0 :                 return NULL;
     585             :         }
     586             : 
     587      999114 :         if (!creator_sd || !creator_sd->owner_sid) {
     588      588620 :                 if ((inherit_flags & SEC_OWNER_FROM_PARENT) && parent_sd) {
     589           0 :                         new_owner = parent_sd->owner_sid;
     590      325483 :                 } else if (!default_owner) {
     591        6031 :                         new_owner = &token->sids[PRIMARY_USER_SID_INDEX];
     592             :                 } else {
     593      319452 :                         new_owner = default_owner;
     594      319452 :                         new_sd->type |= SEC_DESC_OWNER_DEFAULTED;
     595             :                 }
     596             :         } else {
     597      673631 :                 new_owner = creator_sd->owner_sid;
     598             :         }
     599             : 
     600      999114 :         if (!creator_sd || !creator_sd->group_sid){
     601      588620 :                 if ((inherit_flags & SEC_GROUP_FROM_PARENT) && parent_sd) {
     602           0 :                         new_group = parent_sd->group_sid;
     603      325483 :                 } else if (!default_group && token->num_sids > PRIMARY_GROUP_SID_INDEX) {
     604        5359 :                         new_group = &token->sids[PRIMARY_GROUP_SID_INDEX];
     605      320124 :                 } else if (!default_group) {
     606             :                         /* This will happen only for anonymous, which has no other groups */
     607         672 :                         new_group = &token->sids[PRIMARY_USER_SID_INDEX];
     608             :                 } else {
     609      319452 :                         new_group = default_group;
     610      319452 :                         new_sd->type |= SEC_DESC_GROUP_DEFAULTED;
     611             :                 }
     612             :         } else {
     613      673631 :                 new_group = creator_sd->group_sid;
     614             :         }
     615             : 
     616      999114 :         new_sd->owner_sid = talloc_memdup(new_sd, new_owner, sizeof(struct dom_sid));
     617      999114 :         new_sd->group_sid = talloc_memdup(new_sd, new_group, sizeof(struct dom_sid));
     618      999114 :         if (!new_sd->owner_sid || !new_sd->group_sid){
     619           0 :                 talloc_free(new_sd);
     620           0 :                 return NULL;
     621             :         }
     622             : 
     623      999114 :         if (!compute_acl(parent_sd, creator_sd,
     624             :                          is_container, inherit_flags, object_list,
     625             :                          generic_map,token,new_sd)){
     626           0 :                 talloc_free(new_sd);
     627           0 :                 return NULL;
     628             :         }
     629             : 
     630      999114 :         return new_sd;
     631             : }

Generated by: LCOV version 1.13