s3:dom_sid Global replace of DOM_SID with struct dom_sid
[kai/samba.git] / source3 / lib / secdesc.c
index 34954aa7a565bf5bd7dfef6fcb60ae7ce31c8996..a1599a845e2c5fc2b475b87cbe27319d228903b2 100644 (file)
@@ -24,7 +24,7 @@
 
 /* Map generic permissions to file object specific permissions */
 
-struct generic_mapping file_generic_mapping = {
+const struct generic_mapping file_generic_mapping = {
        FILE_GENERIC_READ,
        FILE_GENERIC_WRITE,
        FILE_GENERIC_EXECUTE,
@@ -32,121 +32,43 @@ struct generic_mapping file_generic_mapping = {
 };
 
 /*******************************************************************
Works out the linearization size of a SEC_DESC.
Given a security_descriptor return the sec_info.
 ********************************************************************/
 
-size_t sec_desc_size(SEC_DESC *psd)
+uint32_t get_sec_info(const struct security_descriptor *sd)
 {
-       size_t offset;
+       uint32_t sec_info = ALL_SECURITY_INFORMATION;
 
-       if (!psd) return 0;
+       SMB_ASSERT(sd);
 
-       offset = SEC_DESC_HEADER_SIZE;
-
-       /* don't align */
-
-       if (psd->owner_sid != NULL)
-               offset += sid_size(psd->owner_sid);
-
-       if (psd->group_sid != NULL)
-               offset += sid_size(psd->group_sid);
-
-       if (psd->sacl != NULL)
-               offset += psd->sacl->size;
-
-       if (psd->dacl != NULL)
-               offset += psd->dacl->size;
-
-       return offset;
-}
-
-/*******************************************************************
- Compares two SEC_DESC structures
-********************************************************************/
-
-BOOL sec_desc_equal(SEC_DESC *s1, SEC_DESC *s2)
-{
-       /* Trivial case */
-
-       if (!s1 && !s2) {
-               goto done;
+       if (sd->owner_sid == NULL) {
+               sec_info &= ~OWNER_SECURITY_INFORMATION;
        }
-
-       if (!s1 || !s2) {
-               return False;
+       if (sd->group_sid == NULL) {
+               sec_info &= ~GROUP_SECURITY_INFORMATION;
        }
-
-       /* Check top level stuff */
-
-       if (s1->revision != s2->revision) {
-               DEBUG(10, ("sec_desc_equal(): revision differs (%d != %d)\n",
-                          s1->revision, s2->revision));
-               return False;
+       if (sd->sacl == NULL) {
+               sec_info &= ~SACL_SECURITY_INFORMATION;
        }
-
-       if (s1->type!= s2->type) {
-               DEBUG(10, ("sec_desc_equal(): type differs (%d != %d)\n",
-                          s1->type, s2->type));
-               return False;
+       if (sd->dacl == NULL) {
+               sec_info &= ~DACL_SECURITY_INFORMATION;
        }
 
-       /* Check owner and group */
-
-       if (!sid_equal(s1->owner_sid, s2->owner_sid)) {
-               fstring str1, str2;
-
-               sid_to_string(str1, s1->owner_sid);
-               sid_to_string(str2, s2->owner_sid);
-
-               DEBUG(10, ("sec_desc_equal(): owner differs (%s != %s)\n",
-                          str1, str2));
-               return False;
-       }
-
-       if (!sid_equal(s1->group_sid, s2->group_sid)) {
-               fstring str1, str2;
-
-               sid_to_string(str1, s1->group_sid);
-               sid_to_string(str2, s2->group_sid);
-
-               DEBUG(10, ("sec_desc_equal(): group differs (%s != %s)\n",
-                          str1, str2));
-               return False;
-       }
-
-       /* Check ACLs present in one but not the other */
-
-       if ((s1->dacl && !s2->dacl) || (!s1->dacl && s2->dacl) ||
-           (s1->sacl && !s2->sacl) || (!s1->sacl && s2->sacl)) {
-               DEBUG(10, ("sec_desc_equal(): dacl or sacl not present\n"));
-               return False;
-       }
-
-       /* Sigh - we have to do it the hard way by iterating over all
-          the ACEs in the ACLs */
-
-       if (!sec_acl_equal(s1->dacl, s2->dacl) ||
-           !sec_acl_equal(s1->sacl, s2->sacl)) {
-               DEBUG(10, ("sec_desc_equal(): dacl/sacl list not equal\n"));
-               return False;
-       }
-
- done:
-       DEBUG(10, ("sec_desc_equal(): secdescs are identical\n"));
-       return True;
+       return sec_info;
 }
 
+
 /*******************************************************************
  Merge part of security descriptor old_sec in to the empty sections of 
  security descriptor new_sec.
 ********************************************************************/
 
-SEC_DESC_BUF *sec_desc_merge(TALLOC_CTX *ctx, SEC_DESC_BUF *new_sdb, SEC_DESC_BUF *old_sdb)
+struct sec_desc_buf *sec_desc_merge_buf(TALLOC_CTX *ctx, struct sec_desc_buf *new_sdb, struct sec_desc_buf *old_sdb)
 {
-       DOM_SID *owner_sid, *group_sid;
-       SEC_DESC_BUF *return_sdb;
-       SEC_ACL *dacl, *sacl;
-       SEC_DESC *psd = NULL;
+       struct dom_sid *owner_sid, *group_sid;
+       struct sec_desc_buf *return_sdb;
+       struct security_acl *dacl, *sacl;
+       struct security_descriptor *psd = NULL;
        uint16 secdesc_type;
        size_t secdesc_size;
 
@@ -186,20 +108,65 @@ SEC_DESC_BUF *sec_desc_merge(TALLOC_CTX *ctx, SEC_DESC_BUF *new_sdb, SEC_DESC_BU
        return(return_sdb);
 }
 
+struct security_descriptor *sec_desc_merge(TALLOC_CTX *ctx, struct security_descriptor *new_sdb, struct security_descriptor *old_sdb)
+{
+       struct dom_sid *owner_sid, *group_sid;
+       struct security_acl *dacl, *sacl;
+       struct security_descriptor *psd = NULL;
+       uint16 secdesc_type;
+       size_t secdesc_size;
+
+       /* Copy over owner and group sids.  There seems to be no flag for
+          this so just check the pointer values. */
+
+       owner_sid = new_sdb->owner_sid ? new_sdb->owner_sid :
+               old_sdb->owner_sid;
+
+       group_sid = new_sdb->group_sid ? new_sdb->group_sid :
+               old_sdb->group_sid;
+
+       secdesc_type = new_sdb->type;
+
+       /* Ignore changes to the system ACL.  This has the effect of making
+          changes through the security tab audit button not sticking.
+          Perhaps in future Samba could implement these settings somehow. */
+
+       sacl = NULL;
+       secdesc_type &= ~SEC_DESC_SACL_PRESENT;
+
+       /* Copy across discretionary ACL */
+
+       if (secdesc_type & SEC_DESC_DACL_PRESENT) {
+               dacl = new_sdb->dacl;
+       } else {
+               dacl = old_sdb->dacl;
+       }
+
+       /* Create new security descriptor from bits */
+       psd = make_sec_desc(ctx, new_sdb->revision, secdesc_type,
+                           owner_sid, group_sid, sacl, dacl, &secdesc_size);
+
+       return psd;
+}
+
 /*******************************************************************
- Creates a SEC_DESC structure
+ Creates a struct security_descriptor structure
 ********************************************************************/
 
-SEC_DESC *make_sec_desc(TALLOC_CTX *ctx, uint16 revision, uint16 type,
-                       const DOM_SID *owner_sid, const DOM_SID *grp_sid,
-                       SEC_ACL *sacl, SEC_ACL *dacl, size_t *sd_size)
+#define  SEC_DESC_HEADER_SIZE (2 * sizeof(uint16) + 4 * sizeof(uint32))
+
+struct security_descriptor *make_sec_desc(TALLOC_CTX *ctx,
+                       enum security_descriptor_revision revision,
+                       uint16 type,
+                       const struct dom_sid *owner_sid, const struct dom_sid *grp_sid,
+                       struct security_acl *sacl, struct security_acl *dacl, size_t *sd_size)
 {
-       SEC_DESC *dst;
+       struct security_descriptor *dst;
        uint32 offset     = 0;
 
        *sd_size = 0;
 
-       if(( dst = TALLOC_ZERO_P(ctx, SEC_DESC)) == NULL)
+       if(( dst = TALLOC_ZERO_P(ctx, struct security_descriptor)) == NULL)
                return NULL;
 
        dst->revision = revision;
@@ -241,11 +208,11 @@ SEC_DESC *make_sec_desc(TALLOC_CTX *ctx, uint16 revision, uint16 type,
        }
 
        if (dst->owner_sid != NULL) {
-               offset += sid_size(dst->owner_sid);
+               offset += ndr_size_dom_sid(dst->owner_sid, 0);
        }
 
        if (dst->group_sid != NULL) {
-               offset += sid_size(dst->group_sid);
+               offset += ndr_size_dom_sid(dst->group_sid, 0);
        }
 
        *sd_size = (size_t)offset;
@@ -258,10 +225,10 @@ error_exit:
 }
 
 /*******************************************************************
- Duplicate a SEC_DESC structure.  
+ Duplicate a struct security_descriptor structure.
 ********************************************************************/
 
-SEC_DESC *dup_sec_desc(TALLOC_CTX *ctx, const SEC_DESC *src)
+struct security_descriptor *dup_sec_desc(TALLOC_CTX *ctx, const struct security_descriptor *src)
 {
        size_t dummy;
 
@@ -280,25 +247,47 @@ NTSTATUS marshall_sec_desc(TALLOC_CTX *mem_ctx,
                           struct security_descriptor *secdesc,
                           uint8 **data, size_t *len)
 {
-       prs_struct ps;
-       
-       if (!prs_init(&ps, sec_desc_size(secdesc), mem_ctx, MARSHALL)) {
-               return NT_STATUS_NO_MEMORY;
-       }
+       DATA_BLOB blob;
+       enum ndr_err_code ndr_err;
 
-       if (!sec_io_desc("security_descriptor", &secdesc, &ps, 1)) {
-               prs_mem_free(&ps);
-               return NT_STATUS_INVALID_PARAMETER;
+       ndr_err = ndr_push_struct_blob(
+               &blob, mem_ctx, secdesc,
+               (ndr_push_flags_fn_t)ndr_push_security_descriptor);
+
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               DEBUG(0, ("ndr_push_security_descriptor failed: %s\n",
+                         ndr_errstr(ndr_err)));
+               return ndr_map_error2ntstatus(ndr_err);;
        }
 
-       if (!(*data = (uint8 *)talloc_memdup(mem_ctx, ps.data_p,
-                                            prs_offset(&ps)))) {
-               prs_mem_free(&ps);
-               return NT_STATUS_NO_MEMORY;
+       *data = blob.data;
+       *len = blob.length;
+       return NT_STATUS_OK;
+}
+
+/*******************************************************************
+ Convert a secdesc_buf into a byte stream
+********************************************************************/
+
+NTSTATUS marshall_sec_desc_buf(TALLOC_CTX *mem_ctx,
+                              struct sec_desc_buf *secdesc_buf,
+                              uint8_t **data, size_t *len)
+{
+       DATA_BLOB blob;
+       enum ndr_err_code ndr_err;
+
+       ndr_err = ndr_push_struct_blob(
+               &blob, mem_ctx, secdesc_buf,
+               (ndr_push_flags_fn_t)ndr_push_sec_desc_buf);
+
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               DEBUG(0, ("ndr_push_sec_desc_buf failed: %s\n",
+                         ndr_errstr(ndr_err)));
+               return ndr_map_error2ntstatus(ndr_err);;
        }
 
-       *len = prs_offset(&ps);
-       prs_mem_free(&ps);
+       *data = blob.data;
+       *len = blob.length;
        return NT_STATUS_OK;
 }
 
@@ -308,48 +297,92 @@ NTSTATUS marshall_sec_desc(TALLOC_CTX *mem_ctx,
 NTSTATUS unmarshall_sec_desc(TALLOC_CTX *mem_ctx, uint8 *data, size_t len,
                             struct security_descriptor **psecdesc)
 {
-       prs_struct ps;
-       struct security_descriptor *secdesc = NULL;
+       DATA_BLOB blob;
+       enum ndr_err_code ndr_err;
+       struct security_descriptor *result;
 
-       if (!(secdesc = TALLOC_ZERO_P(mem_ctx, struct security_descriptor))) {
-               return NT_STATUS_NO_MEMORY;
+       if ((data == NULL) || (len == 0)) {
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       if (!prs_init(&ps, 0, secdesc, UNMARSHALL)) {
+       result = TALLOC_ZERO_P(mem_ctx, struct security_descriptor);
+       if (result == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       prs_give_memory(&ps, (char *)data, len, False);
+       blob = data_blob_const(data, len);
+
+       ndr_err = ndr_pull_struct_blob(&blob, result, result,
+               (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
+
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               DEBUG(0, ("ndr_pull_security_descriptor failed: %s\n",
+                         ndr_errstr(ndr_err)));
+               TALLOC_FREE(result);
+               return ndr_map_error2ntstatus(ndr_err);;
+       }
+
+       *psecdesc = result;
+       return NT_STATUS_OK;
+}
 
-       if (!sec_io_desc("security_descriptor", &secdesc, &ps, 1)) {
+/*******************************************************************
+ Parse a byte stream into a sec_desc_buf
+********************************************************************/
+
+NTSTATUS unmarshall_sec_desc_buf(TALLOC_CTX *mem_ctx, uint8_t *data, size_t len,
+                                struct sec_desc_buf **psecdesc_buf)
+{
+       DATA_BLOB blob;
+       enum ndr_err_code ndr_err;
+       struct sec_desc_buf *result;
+
+       if ((data == NULL) || (len == 0)) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       prs_mem_free(&ps);
-       *psecdesc = secdesc;
+       result = TALLOC_ZERO_P(mem_ctx, struct sec_desc_buf);
+       if (result == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       blob = data_blob_const(data, len);
+
+       ndr_err = ndr_pull_struct_blob(&blob, result, result,
+               (ndr_pull_flags_fn_t)ndr_pull_sec_desc_buf);
+
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               DEBUG(0, ("ndr_pull_sec_desc_buf failed: %s\n",
+                         ndr_errstr(ndr_err)));
+               TALLOC_FREE(result);
+               return ndr_map_error2ntstatus(ndr_err);;
+       }
+
+       *psecdesc_buf = result;
        return NT_STATUS_OK;
 }
 
 /*******************************************************************
- Creates a SEC_DESC structure with typical defaults.
+ Creates a struct security_descriptor structure with typical defaults.
 ********************************************************************/
 
-SEC_DESC *make_standard_sec_desc(TALLOC_CTX *ctx, const DOM_SID *owner_sid, const DOM_SID *grp_sid,
-                                SEC_ACL *dacl, size_t *sd_size)
+struct security_descriptor *make_standard_sec_desc(TALLOC_CTX *ctx, const struct dom_sid *owner_sid, const struct dom_sid *grp_sid,
+                                struct security_acl *dacl, size_t *sd_size)
 {
-       return make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
-                            owner_sid, grp_sid, NULL, dacl, sd_size);
+       return make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
+                            SEC_DESC_SELF_RELATIVE, owner_sid, grp_sid, NULL,
+                            dacl, sd_size);
 }
 
 /*******************************************************************
- Creates a SEC_DESC_BUF structure.
+ Creates a struct sec_desc_buf structure.
 ********************************************************************/
 
-SEC_DESC_BUF *make_sec_desc_buf(TALLOC_CTX *ctx, size_t len, SEC_DESC *sec_desc)
+struct sec_desc_buf *make_sec_desc_buf(TALLOC_CTX *ctx, size_t len, struct security_descriptor *sec_desc)
 {
-       SEC_DESC_BUF *dst;
+       struct sec_desc_buf *dst;
 
-       if((dst = TALLOC_ZERO_P(ctx, SEC_DESC_BUF)) == NULL)
+       if((dst = TALLOC_ZERO_P(ctx, struct sec_desc_buf)) == NULL)
                return NULL;
 
        /* max buffer size (allocated size) */
@@ -363,10 +396,10 @@ SEC_DESC_BUF *make_sec_desc_buf(TALLOC_CTX *ctx, size_t len, SEC_DESC *sec_desc)
 }
 
 /*******************************************************************
- Duplicates a SEC_DESC_BUF structure.
+ Duplicates a struct sec_desc_buf structure.
 ********************************************************************/
 
-SEC_DESC_BUF *dup_sec_desc_buf(TALLOC_CTX *ctx, SEC_DESC_BUF *src)
+struct sec_desc_buf *dup_sec_desc_buf(TALLOC_CTX *ctx, struct sec_desc_buf *src)
 {
        if(src == NULL)
                return NULL;
@@ -375,14 +408,14 @@ SEC_DESC_BUF *dup_sec_desc_buf(TALLOC_CTX *ctx, SEC_DESC_BUF *src)
 }
 
 /*******************************************************************
- Add a new SID with its permissions to SEC_DESC.
+ Add a new SID with its permissions to struct security_descriptor.
 ********************************************************************/
 
-NTSTATUS sec_desc_add_sid(TALLOC_CTX *ctx, SEC_DESC **psd, DOM_SID *sid, uint32 mask, size_t *sd_size)
+NTSTATUS sec_desc_add_sid(TALLOC_CTX *ctx, struct security_descriptor **psd, struct dom_sid *sid, uint32 mask, size_t *sd_size)
 {
-       SEC_DESC *sd   = 0;
-       SEC_ACL  *dacl = 0;
-       SEC_ACE  *ace  = 0;
+       struct security_descriptor *sd   = 0;
+       struct security_acl  *dacl = 0;
+       struct security_ace  *ace  = 0;
        NTSTATUS  status;
 
        if (!ctx || !psd || !sid || !sd_size)
@@ -408,10 +441,10 @@ NTSTATUS sec_desc_add_sid(TALLOC_CTX *ctx, SEC_DESC **psd, DOM_SID *sid, uint32
 }
 
 /*******************************************************************
- Modify a SID's permissions in a SEC_DESC.
+ Modify a SID's permissions in a struct security_descriptor.
 ********************************************************************/
 
-NTSTATUS sec_desc_mod_sid(SEC_DESC *sd, DOM_SID *sid, uint32 mask)
+NTSTATUS sec_desc_mod_sid(struct security_descriptor *sd, struct dom_sid *sid, uint32 mask)
 {
        NTSTATUS status;
 
@@ -427,14 +460,14 @@ NTSTATUS sec_desc_mod_sid(SEC_DESC *sd, DOM_SID *sid, uint32 mask)
 }
 
 /*******************************************************************
- Delete a SID from a SEC_DESC.
+ Delete a SID from a struct security_descriptor.
 ********************************************************************/
 
-NTSTATUS sec_desc_del_sid(TALLOC_CTX *ctx, SEC_DESC **psd, DOM_SID *sid, size_t *sd_size)
+NTSTATUS sec_desc_del_sid(TALLOC_CTX *ctx, struct security_descriptor **psd, struct dom_sid *sid, size_t *sd_size)
 {
-       SEC_DESC *sd   = 0;
-       SEC_ACL  *dacl = 0;
-       SEC_ACE  *ace  = 0;
+       struct security_descriptor *sd   = 0;
+       struct security_acl  *dacl = 0;
+       struct security_ace  *ace  = 0;
        NTSTATUS  status;
 
        if (!ctx || !psd[0] || !sid || !sd_size)
@@ -459,19 +492,67 @@ NTSTATUS sec_desc_del_sid(TALLOC_CTX *ctx, SEC_DESC **psd, DOM_SID *sid, size_t
        return NT_STATUS_OK;
 }
 
+/*
+ * Determine if an struct security_ace is inheritable
+ */
+
+static bool is_inheritable_ace(const struct security_ace *ace,
+                               bool container)
+{
+       if (!container) {
+               return ((ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) != 0);
+       }
+
+       if (ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) {
+               return true;
+       }
+
+       if ((ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) &&
+                       !(ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT)) {
+               return true;
+       }
+
+       return false;
+}
+
+/*
+ * Does a security descriptor have any inheritable components for
+ * the newly created type ?
+ */
+
+bool sd_has_inheritable_components(const struct security_descriptor *parent_ctr, bool container)
+{
+       unsigned int i;
+       const struct security_acl *the_acl = parent_ctr->dacl;
+
+       for (i = 0; i < the_acl->num_aces; i++) {
+               const struct security_ace *ace = &the_acl->aces[i];
+
+               if (is_inheritable_ace(ace, container)) {
+                       return true;
+               }
+       }
+       return false;
+}
+
 /* Create a child security descriptor using another security descriptor as
    the parent container.  This child object can either be a container or
    non-container object. */
 
-SEC_DESC_BUF *se_create_child_secdesc(TALLOC_CTX *ctx, SEC_DESC *parent_ctr, 
-                                     BOOL child_container)
+NTSTATUS se_create_child_secdesc(TALLOC_CTX *ctx,
+                                       struct security_descriptor **ppsd,
+                                       size_t *psize,
+                                       const struct security_descriptor *parent_ctr,
+                                       const struct dom_sid *owner_sid,
+                                       const struct dom_sid *group_sid,
+                                       bool container)
 {
-       SEC_DESC_BUF *sdb;
-       SEC_DESC *sd;
-       SEC_ACL *new_dacl, *the_acl;
-       SEC_ACE *new_ace_list = NULL;
+       struct security_acl *new_dacl = NULL, *the_acl = NULL;
+       struct security_ace *new_ace_list = NULL;
        unsigned int new_ace_list_ndx = 0, i;
-       size_t size;
+
+       *ppsd = NULL;
+       *psize = 0;
 
        /* Currently we only process the dacl when creating the child.  The
           sacl should also be processed but this is left out as sacls are
@@ -480,110 +561,143 @@ SEC_DESC_BUF *se_create_child_secdesc(TALLOC_CTX *ctx, SEC_DESC *parent_ctr,
        the_acl = parent_ctr->dacl;
 
        if (the_acl->num_aces) {
-               if (!(new_ace_list = TALLOC_ARRAY(ctx, SEC_ACE, the_acl->num_aces))) 
-                       return NULL;
+               if (2*the_acl->num_aces < the_acl->num_aces) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               if (!(new_ace_list = TALLOC_ARRAY(ctx, struct security_ace,
+                                               2*the_acl->num_aces))) {
+                       return NT_STATUS_NO_MEMORY;
+               }
        } else {
                new_ace_list = NULL;
        }
 
        for (i = 0; i < the_acl->num_aces; i++) {
-               SEC_ACE *ace = &the_acl->aces[i];
-               SEC_ACE *new_ace = &new_ace_list[new_ace_list_ndx];
-               uint8 new_flags = 0;
-               BOOL inherit = False;
-               fstring sid_str;
-
-               /* The OBJECT_INHERIT_ACE flag causes the ACE to be
-                  inherited by non-container children objects.  Container
-                  children objects will inherit it as an INHERIT_ONLY
-                  ACE. */
-
-               if (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
-
-                       if (!child_container) {
-                               new_flags |= SEC_ACE_FLAG_OBJECT_INHERIT;
-                       } else {
-                               new_flags |= SEC_ACE_FLAG_INHERIT_ONLY;
-                       }
+               const struct security_ace *ace = &the_acl->aces[i];
+               struct security_ace *new_ace = &new_ace_list[new_ace_list_ndx];
+               const struct dom_sid *ptrustee = &ace->trustee;
+               const struct dom_sid *creator = NULL;
+               uint8 new_flags = ace->flags;
 
-                       inherit = True;
+               if (!is_inheritable_ace(ace, container)) {
+                       continue;
                }
 
-               /* The CONAINER_INHERIT_ACE flag means all child container
-                  objects will inherit and use the ACE. */
+               /* see the RAW-ACLS inheritance test for details on these rules */
+               if (!container) {
+                       new_flags = 0;
+               } else {
+                       new_flags &= ~SEC_ACE_FLAG_INHERIT_ONLY;
 
-               if (ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) {
-                       if (!child_container) {
-                               inherit = False;
-                       } else {
-                               new_flags |= SEC_ACE_FLAG_CONTAINER_INHERIT;
+                       if (!(new_flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
+                               new_flags |= SEC_ACE_FLAG_INHERIT_ONLY;
+                       }
+                       if (new_flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
+                               new_flags = 0;
                        }
                }
 
-               /* The INHERIT_ONLY_ACE is not used by the se_access_check()
-                  function for the parent container, but is inherited by
-                  all child objects as a normal ACE. */
-
-               if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
-                       /* Move along, nothing to see here */
+               /* The CREATOR sids are special when inherited */
+               if (sid_equal(ptrustee, &global_sid_Creator_Owner)) {
+                       creator = &global_sid_Creator_Owner;
+                       ptrustee = owner_sid;
+               } else if (sid_equal(ptrustee, &global_sid_Creator_Group)) {
+                       creator = &global_sid_Creator_Group;
+                       ptrustee = group_sid;
                }
 
-               /* The SEC_ACE_FLAG_NO_PROPAGATE_INHERIT flag means the ACE
-                  is inherited by child objects but not grandchildren
-                  objects.  We clear the object inherit and container
-                  inherit flags in the inherited ACE. */
+               if (creator && container &&
+                               (new_flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
 
-               if (ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
-                       new_flags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT |
-                                      SEC_ACE_FLAG_CONTAINER_INHERIT);
-               }
+                       /* First add the regular ACE entry. */
+                       init_sec_ace(new_ace, ptrustee, ace->type,
+                               ace->access_mask, 0);
 
-               /* Add ACE to ACE list */
+                       DEBUG(5,("se_create_child_secdesc(): %s:%d/0x%02x/0x%08x"
+                               " inherited as %s:%d/0x%02x/0x%08x\n",
+                               sid_string_dbg(&ace->trustee),
+                               ace->type, ace->flags, ace->access_mask,
+                               sid_string_dbg(&new_ace->trustee),
+                               new_ace->type, new_ace->flags,
+                               new_ace->access_mask));
 
-               if (!inherit)
-                       continue;
+                       new_ace_list_ndx++;
 
-               init_sec_access(&new_ace->access_mask, ace->access_mask);
-               init_sec_ace(new_ace, &ace->trustee, ace->type,
-                            new_ace->access_mask, new_flags);
+                       /* Now add the extra creator ACE. */
+                       new_ace = &new_ace_list[new_ace_list_ndx];
 
-               sid_to_string(sid_str, &ace->trustee);
+                       ptrustee = creator;
+                       new_flags |= SEC_ACE_FLAG_INHERIT_ONLY;
+               } else if (container &&
+                               !(ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT)) {
+                       ptrustee = &ace->trustee;
+               }
+
+               init_sec_ace(new_ace, ptrustee, ace->type,
+                            ace->access_mask, new_flags);
 
                DEBUG(5, ("se_create_child_secdesc(): %s:%d/0x%02x/0x%08x "
-                         " inherited as %s:%d/0x%02x/0x%08x\n", sid_str,
+                         " inherited as %s:%d/0x%02x/0x%08x\n",
+                         sid_string_dbg(&ace->trustee),
                          ace->type, ace->flags, ace->access_mask,
-                         sid_str, new_ace->type, new_ace->flags,
+                         sid_string_dbg(&ace->trustee),
+                         new_ace->type, new_ace->flags,
                          new_ace->access_mask));
 
                new_ace_list_ndx++;
        }
 
        /* Create child security descriptor to return */
-       
-       new_dacl = make_sec_acl(ctx, ACL_REVISION, new_ace_list_ndx, new_ace_list);
-
-       /* Use the existing user and group sids.  I don't think this is
-          correct.  Perhaps the user and group should be passed in as
-          parameters by the caller? */
-
-       sd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
-                          parent_ctr->owner_sid,
-                          parent_ctr->group_sid,
-                          parent_ctr->sacl,
-                          new_dacl, &size);
-
-       sdb = make_sec_desc_buf(ctx, size, sd);
+       if (new_ace_list_ndx) {
+               new_dacl = make_sec_acl(ctx,
+                               NT4_ACL_REVISION,
+                               new_ace_list_ndx,
+                               new_ace_list);
+
+               if (!new_dacl) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
 
-       return sdb;
+       *ppsd = make_sec_desc(ctx,
+                       SECURITY_DESCRIPTOR_REVISION_1,
+                       SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
+                       owner_sid,
+                       group_sid,
+                       NULL,
+                       new_dacl,
+                       psize);
+       if (!*ppsd) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       return NT_STATUS_OK;
 }
 
-/*******************************************************************
- Sets up a SEC_ACCESS structure.
-********************************************************************/
-
-void init_sec_access(uint32 *t, uint32 mask)
+NTSTATUS se_create_child_secdesc_buf(TALLOC_CTX *ctx,
+                                       struct sec_desc_buf **ppsdb,
+                                       const struct security_descriptor *parent_ctr,
+                                       bool container)
 {
-       *t = mask;
-}
-
+       NTSTATUS status;
+       size_t size = 0;
+       struct security_descriptor *sd = NULL;
+
+       *ppsdb = NULL;
+       status = se_create_child_secdesc(ctx,
+                                       &sd,
+                                       &size,
+                                       parent_ctr,
+                                       parent_ctr->owner_sid,
+                                       parent_ctr->group_sid,
+                                       container);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
+       *ppsdb = make_sec_desc_buf(ctx, size, sd);
+       if (!*ppsdb) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       return NT_STATUS_OK;
+}