s3:dom_sid Global replace of DOM_SID with struct dom_sid
[sfrench/samba-autobuild/.git] / source3 / modules / vfs_afsacl.c
index a923ce188ffb75ccdbf9f2677f5d2ab793ad45ff..1f495d9448008ce66d9019e281c0737d34f96a20 100644 (file)
 
 #define MAXSIZE 2048
 
-extern const DOM_SID global_sid_World;
-extern const DOM_SID global_sid_Builtin_Administrators;
-extern const DOM_SID global_sid_Builtin_Backup_Operators;
-extern const DOM_SID global_sid_Authenticated_Users;
-extern const DOM_SID global_sid_NULL;
+extern const struct dom_sid global_sid_World;
+extern const struct dom_sid global_sid_Builtin_Administrators;
+extern const struct dom_sid global_sid_Builtin_Backup_Operators;
+extern const struct dom_sid global_sid_Authenticated_Users;
+extern const struct dom_sid global_sid_NULL;
 
 static char space_replacement = '%';
 
@@ -46,7 +46,7 @@ extern int afs_syscall(int, char *, int, char *, int);
 struct afs_ace {
        bool positive;
        char *name;
-       DOM_SID sid;
+       struct dom_sid sid;
        enum lsa_SidType type;
        uint32 rights;
        struct afs_ace *next;
@@ -108,7 +108,7 @@ static struct afs_ace *new_afs_ace(TALLOC_CTX *mem_ctx,
                                   bool positive,
                                   const char *name, uint32 rights)
 {
-       DOM_SID sid;
+       struct dom_sid sid;
        enum lsa_SidType type;
        struct afs_ace *result;
 
@@ -528,7 +528,7 @@ static struct static_dir_ace_mapping {
        { 0, 0, 0, 9999 }
 };
 
-static uint32 nt_to_afs_dir_rights(const char *filename, const SEC_ACE *ace)
+static uint32 nt_to_afs_dir_rights(const char *filename, const struct security_ace *ace)
 {
        uint32 result = 0;
        uint32 rights = ace->access_mask;
@@ -569,7 +569,7 @@ static uint32 nt_to_afs_dir_rights(const char *filename, const SEC_ACE *ace)
        return result;
 }
 
-static uint32 nt_to_afs_file_rights(const char *filename, const SEC_ACE *ace)
+static uint32 nt_to_afs_file_rights(const char *filename, const struct security_ace *ace)
 {
        uint32 result = 0;
        uint32 rights = ace->access_mask;
@@ -585,38 +585,25 @@ static uint32 nt_to_afs_file_rights(const char *filename, const SEC_ACE *ace)
        return result;
 }
 
-static size_t afs_to_nt_acl(struct afs_acl *afs_acl, 
-                           struct files_struct *fsp,
-                           uint32 security_info,
-                           struct security_descriptor **ppdesc)
+static size_t afs_to_nt_acl_common(struct afs_acl *afs_acl,
+                                  SMB_STRUCT_STAT *psbuf,
+                                  uint32 security_info,
+                                  struct security_descriptor **ppdesc)
 {
-       SEC_ACE *nt_ace_list;
-       DOM_SID owner_sid, group_sid;
-       SEC_ACCESS mask;
-       SMB_STRUCT_STAT sbuf;
-       SEC_ACL *psa = NULL;
+       struct security_ace *nt_ace_list;
+       struct dom_sid owner_sid, group_sid;
+       struct security_acl *psa = NULL;
        int good_aces;
        size_t sd_size;
        TALLOC_CTX *mem_ctx = talloc_tos();
 
        struct afs_ace *afs_ace;
 
-       if (fsp->is_directory || fsp->fh->fd == -1) {
-               /* Get the stat struct for the owner info. */
-               if(SMB_VFS_STAT(fsp->conn,fsp->fsp_name, &sbuf) != 0) {
-                       return 0;
-               }
-       } else {
-               if(SMB_VFS_FSTAT(fsp,fsp->fh->fd,&sbuf) != 0) {
-                       return 0;
-               }
-       }
-
-       uid_to_sid(&owner_sid, sbuf.st_uid);
-       gid_to_sid(&group_sid, sbuf.st_gid);
+       uid_to_sid(&owner_sid, psbuf->st_ex_uid);
+       gid_to_sid(&group_sid, psbuf->st_ex_gid);
 
        if (afs_acl->num_aces) {
-               nt_ace_list = TALLOC_ARRAY(mem_ctx, SEC_ACE, afs_acl->num_aces);
+               nt_ace_list = TALLOC_ARRAY(mem_ctx, struct security_ace, afs_acl->num_aces);
 
                if (nt_ace_list == NULL)
                        return 0;
@@ -628,7 +615,7 @@ static size_t afs_to_nt_acl(struct afs_acl *afs_acl,
        good_aces = 0;
 
        while (afs_ace != NULL) {
-               uint32 nt_rights;
+               uint32_t nt_rights;
                uint8 flag = SEC_ACE_FLAG_OBJECT_INHERIT |
                        SEC_ACE_FLAG_CONTAINER_INHERIT;
 
@@ -639,15 +626,14 @@ static size_t afs_to_nt_acl(struct afs_acl *afs_acl,
                        continue;
                }
 
-               if (fsp->is_directory)
+               if (S_ISDIR(psbuf->st_ex_mode))
                        afs_to_nt_dir_rights(afs_ace->rights, &nt_rights,
                                             &flag);
                else
                        nt_rights = afs_to_nt_file_rights(afs_ace->rights);
 
-               init_sec_access(&mask, nt_rights);
                init_sec_ace(&nt_ace_list[good_aces++], &(afs_ace->sid),
-                            SEC_ACE_TYPE_ACCESS_ALLOWED, mask, flag);
+                            SEC_ACE_TYPE_ACCESS_ALLOWED, nt_rights, flag);
                afs_ace = afs_ace->next;
        }
 
@@ -656,8 +642,7 @@ static size_t afs_to_nt_acl(struct afs_acl *afs_acl,
        if (psa == NULL)
                return 0;
 
-       
-       *ppdesc = make_sec_desc(mem_ctx, SEC_DESC_REVISION,
+       *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
                                SEC_DESC_SELF_RELATIVE,
                                (security_info & OWNER_SECURITY_INFORMATION)
                                ? &owner_sid : NULL,
@@ -668,9 +653,51 @@ static size_t afs_to_nt_acl(struct afs_acl *afs_acl,
        return sd_size;
 }
 
-static bool mappable_sid(const DOM_SID *sid)
+static size_t afs_to_nt_acl(struct afs_acl *afs_acl,
+                           struct connection_struct *conn,
+                           struct smb_filename *smb_fname,
+                           uint32 security_info,
+                           struct security_descriptor **ppdesc)
+{
+       int ret;
+
+       /* Get the stat struct for the owner info. */
+       if (lp_posix_pathnames()) {
+               ret = SMB_VFS_LSTAT(conn, smb_fname);
+       } else {
+               ret = SMB_VFS_STAT(conn, smb_fname);
+       }
+       if (ret == -1) {
+               return 0;
+       }
+
+       return afs_to_nt_acl_common(afs_acl, &smb_fname->st, security_info,
+                                   ppdesc);
+}
+
+static size_t afs_fto_nt_acl(struct afs_acl *afs_acl,
+                            struct files_struct *fsp,
+                            uint32 security_info,
+                            struct security_descriptor **ppdesc)
 {
-       DOM_SID domain_sid;
+       SMB_STRUCT_STAT sbuf;
+
+       if (fsp->is_directory || fsp->fh->fd == -1) {
+               /* Get the stat struct for the owner info. */
+               return afs_to_nt_acl(afs_acl, fsp->conn, fsp->fsp_name,
+                                    security_info, ppdesc);
+       }
+
+       if(SMB_VFS_FSTAT(fsp, &sbuf) != 0) {
+               return 0;
+       }
+
+       return afs_to_nt_acl_common(afs_acl, &sbuf, security_info, ppdesc);
+}
+
+static bool mappable_sid(const struct dom_sid *sid)
+{
+       struct dom_sid domain_sid;
        
        if (sid_compare(sid, &global_sid_Builtin_Administrators) == 0)
                return True;
@@ -694,12 +721,12 @@ static bool mappable_sid(const DOM_SID *sid)
 
 static bool nt_to_afs_acl(const char *filename,
                          uint32 security_info_sent,
-                         struct security_descriptor *psd,
+                         const struct security_descriptor *psd,
                          uint32 (*nt_to_afs_rights)(const char *filename,
-                                                    const SEC_ACE *ace),
+                                                    const struct security_ace *ace),
                          struct afs_acl *afs_acl)
 {
-       SEC_ACL *dacl;
+       const struct security_acl *dacl;
        int i;
 
        /* Currently we *only* look at the dacl */
@@ -714,7 +741,7 @@ static bool nt_to_afs_acl(const char *filename,
        dacl = psd->dacl;
 
        for (i = 0; i < dacl->num_aces; i++) {
-               SEC_ACE *ace = &(dacl->aces[i]);
+               const struct security_ace *ace = &(dacl->aces[i]);
                const char *dom_name, *name;
                enum lsa_SidType name_type;
                char *p;
@@ -830,27 +857,6 @@ static bool afs_get_afs_acl(char *filename, struct afs_acl *acl)
        return True;
 }
 
-static NTSTATUS afs_get_nt_acl(struct files_struct *fsp, uint32 security_info,
-                              struct security_descriptor **ppdesc)
-{
-       struct afs_acl acl;
-       size_t sd_size;
-
-       DEBUG(5, ("afs_get_nt_acl: %s\n", fsp->fsp_name));
-
-       sidpts = lp_parm_bool(SNUM(fsp->conn), "afsacl", "sidpts", False);
-
-       if (!afs_get_afs_acl(fsp->fsp_name, &acl)) {
-               return NT_STATUS_ACCESS_DENIED;
-       }
-
-       sd_size = afs_to_nt_acl(&acl, fsp, security_info, ppdesc);
-
-       free_afs_acl(&acl);
-
-       return (sd_size != 0) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
-}
-
 /* For setting an AFS ACL we have to take care of the ACEs we could
  * not properly map to SIDs. Merge all of them into the new ACL. */
 
@@ -885,7 +891,7 @@ static void merge_unknown_aces(struct afs_acl *src, struct afs_acl *dst)
 
 static NTSTATUS afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
                           uint32 security_info_sent,
-                          struct security_descriptor *psd)
+                          const struct security_descriptor *psd)
 {
        struct afs_acl old_afs_acl, new_afs_acl;
        struct afs_acl dir_acl, file_acl;
@@ -905,7 +911,7 @@ static NTSTATUS afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
        ZERO_STRUCT(dir_acl);
        ZERO_STRUCT(file_acl);
 
-       name = talloc_strdup(talloc_tos(), fsp->fsp_name);
+       name = talloc_strdup(talloc_tos(), fsp->fsp_name->base_name);
        if (!name) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -925,7 +931,7 @@ static NTSTATUS afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
        }
 
        if (!afs_get_afs_acl(name, &old_afs_acl)) {
-               DEBUG(3, ("Could not get old ACL of %s\n", fsp->fsp_name));
+               DEBUG(3, ("Could not get old ACL of %s\n", fsp_str_dbg(fsp)));
                goto done;
        }
 
@@ -941,7 +947,8 @@ static NTSTATUS afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
                }
 
                free_afs_acl(&dir_acl);
-               if (!nt_to_afs_acl(fsp->fsp_name, security_info_sent, psd,
+               if (!nt_to_afs_acl(fsp->fsp_name->base_name,
+                                  security_info_sent, psd,
                                   nt_to_afs_dir_rights, &dir_acl))
                        goto done;
        } else {
@@ -956,7 +963,8 @@ static NTSTATUS afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
                }
 
                free_afs_acl(&file_acl);
-               if (!nt_to_afs_acl(fsp->fsp_name, security_info_sent, psd,
+               if (!nt_to_afs_acl(fsp->fsp_name->base_name,
+                                  security_info_sent, psd,
                                   nt_to_afs_file_rights, &file_acl))
                        goto done;
        }
@@ -991,31 +999,64 @@ static NTSTATUS afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
 
 static NTSTATUS afsacl_fget_nt_acl(struct vfs_handle_struct *handle,
                                   struct files_struct *fsp,
-                                  int fd,  uint32 security_info,
+                                  uint32 security_info,
                                   struct security_descriptor **ppdesc)
 {
-       return afs_get_nt_acl(fsp, security_info, ppdesc);
+       struct afs_acl acl;
+       size_t sd_size;
+
+       DEBUG(5, ("afsacl_fget_nt_acl: %s\n", fsp_str_dbg(fsp)));
+
+       sidpts = lp_parm_bool(SNUM(fsp->conn), "afsacl", "sidpts", False);
+
+       if (!afs_get_afs_acl(fsp->fsp_name->base_name, &acl)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       sd_size = afs_fto_nt_acl(&acl, fsp, security_info, ppdesc);
+
+       free_afs_acl(&acl);
+
+       return (sd_size != 0) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
 }
+
 static NTSTATUS afsacl_get_nt_acl(struct vfs_handle_struct *handle,
-                                 struct files_struct *fsp,
                                  const char *name,  uint32 security_info,
                                  struct security_descriptor **ppdesc)
 {
-       return afs_get_nt_acl(fsp, security_info, ppdesc);
+       struct afs_acl acl;
+       size_t sd_size;
+       struct smb_filename *smb_fname = NULL;
+       NTSTATUS status;
+
+       DEBUG(5, ("afsacl_get_nt_acl: %s\n", name));
+
+       sidpts = lp_parm_bool(SNUM(handle->conn), "afsacl", "sidpts", False);
+
+       if (!afs_get_afs_acl(name, &acl)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       status = create_synthetic_smb_fname(talloc_tos(), name, NULL, NULL,
+                                           &smb_fname);
+       if (!NT_STATUS_IS_OK(status)) {
+               free_afs_acl(&acl);
+               return status;
+       }
+
+       sd_size = afs_to_nt_acl(&acl, handle->conn, smb_fname, security_info,
+                               ppdesc);
+       TALLOC_FREE(smb_fname);
+
+       free_afs_acl(&acl);
+
+       return (sd_size != 0) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
 }
 
 NTSTATUS afsacl_fset_nt_acl(vfs_handle_struct *handle,
                         files_struct *fsp,
-                        int fd, uint32 security_info_sent,
-                        SEC_DESC *psd)
-{
-       return afs_set_nt_acl(handle, fsp, security_info_sent, psd);
-}
-
-NTSTATUS afsacl_set_nt_acl(vfs_handle_struct *handle,
-                      files_struct *fsp,
-                      const char *name, uint32 security_info_sent,
-                      SEC_DESC *psd)
+                        uint32 security_info_sent,
+                        const struct security_descriptor *psd)
 {
        return afs_set_nt_acl(handle, fsp, security_info_sent, psd);
 }
@@ -1024,35 +1065,31 @@ static int afsacl_connect(vfs_handle_struct *handle,
                          const char *service, 
                          const char *user)
 {
-                       const char *spc;
+       const char *spc;
+       int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
+
+       if (ret < 0) {
+               return ret;
+       }
 
        spc = lp_parm_const_string(SNUM(handle->conn), "afsacl", "space", "%");
 
        if (spc != NULL)
                space_replacement = spc[0];
-       
-       return SMB_VFS_NEXT_CONNECT(handle, service, user);
+
+       return 0;
 }
 
-/* VFS operations structure */
-
-static vfs_op_tuple afsacl_ops[] = {   
-       {SMB_VFS_OP(afsacl_connect), SMB_VFS_OP_CONNECT,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(afsacl_fget_nt_acl), SMB_VFS_OP_FGET_NT_ACL,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(afsacl_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(afsacl_fset_nt_acl), SMB_VFS_OP_FSET_NT_ACL,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(afsacl_set_nt_acl), SMB_VFS_OP_SET_NT_ACL,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
+static struct vfs_fn_pointers vfs_afsacl_fns = {
+       .connect_fn = afsacl_connect,
+       .fget_nt_acl = afsacl_fget_nt_acl,
+       .get_nt_acl = afsacl_get_nt_acl,
+       .fset_nt_acl = afsacl_fset_nt_acl
 };
 
 NTSTATUS vfs_afsacl_init(void);
 NTSTATUS vfs_afsacl_init(void)
 {
        return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "afsacl",
-                               afsacl_ops);
+                               &vfs_afsacl_fns);
 }