Update vfs version as I've added a const to the security_descriptor paramter in fset_...
[samba.git] / source3 / smbd / posix_acls.c
index 33042faf56dcee04c5f1fe5147583ef8df0651b4..48ea993e28fcb7b5a4465485b6684b4f08bc319a 100644 (file)
@@ -904,7 +904,7 @@ static SEC_ACCESS map_canon_ace_perms(int snum,
                if (directory_ace) {
                        nt_mask = UNIX_DIRECTORY_ACCESS_RWX;
                } else {
-                       nt_mask = UNIX_ACCESS_RWX;
+                       nt_mask = (UNIX_ACCESS_RWX & ~DELETE_ACCESS);
                }
        } else if ((perms & ALL_ACE_PERMS) == (mode_t)0) {
                /*
@@ -988,7 +988,7 @@ static mode_t map_nt_perms( uint32 *mask, int type)
  Unpack a SEC_DESC into a UNIX owner and group.
 ****************************************************************************/
 
-NTSTATUS unpack_nt_owners(int snum, uid_t *puser, gid_t *pgrp, uint32 security_info_sent, SEC_DESC *psd)
+NTSTATUS unpack_nt_owners(int snum, uid_t *puser, gid_t *pgrp, uint32 security_info_sent, const SEC_DESC *psd)
 {
        DOM_SID owner_sid;
        DOM_SID grp_sid;
@@ -1329,11 +1329,13 @@ static void check_owning_objs(canon_ace *ace, DOM_SID *pfile_owner_sid, DOM_SID
  Unpack a SEC_DESC into two canonical ace lists.
 ****************************************************************************/
 
-static bool create_canon_ace_lists(files_struct *fsp, SMB_STRUCT_STAT *pst,
-                                                       DOM_SID *pfile_owner_sid,
-                                                       DOM_SID *pfile_grp_sid,
-                                                       canon_ace **ppfile_ace, canon_ace **ppdir_ace,
-                                                       SEC_ACL *dacl)
+static bool create_canon_ace_lists(files_struct *fsp,
+                                       SMB_STRUCT_STAT *pst,
+                                       DOM_SID *pfile_owner_sid,
+                                       DOM_SID *pfile_grp_sid,
+                                       canon_ace **ppfile_ace,
+                                       canon_ace **ppdir_ace,
+                                       const SEC_ACL *dacl)
 {
        bool all_aces_are_inherit_only = (fsp->is_directory ? True : False);
        canon_ace *file_ace = NULL;
@@ -2016,12 +2018,14 @@ static mode_t create_default_mode(files_struct *fsp, bool interitable_mode)
  succeeding.
 ****************************************************************************/
 
-static bool unpack_canon_ace(files_struct *fsp, 
-                                                       SMB_STRUCT_STAT *pst,
-                                                       DOM_SID *pfile_owner_sid,
-                                                       DOM_SID *pfile_grp_sid,
-                                                       canon_ace **ppfile_ace, canon_ace **ppdir_ace,
-                                                       uint32 security_info_sent, SEC_DESC *psd)
+static bool unpack_canon_ace(files_struct *fsp,
+                               SMB_STRUCT_STAT *pst,
+                               DOM_SID *pfile_owner_sid,
+                               DOM_SID *pfile_grp_sid,
+                               canon_ace **ppfile_ace,
+                               canon_ace **ppdir_ace,
+                               uint32 security_info_sent,
+                               const SEC_DESC *psd)
 {
        canon_ace *file_ace = NULL;
        canon_ace *dir_ace = NULL;
@@ -2207,9 +2211,7 @@ static canon_ace *canonicalise_acl(struct connection_struct *conn,
                posix_id unix_ug;
                enum ace_owner owner_type;
 
-               /* get_next... */
-               if (entry_id == SMB_ACL_FIRST_ENTRY)
-                       entry_id = SMB_ACL_NEXT_ENTRY;
+               entry_id = SMB_ACL_NEXT_ENTRY;
 
                /* Is this a MASK entry ? */
                if (SMB_VFS_SYS_ACL_GET_TAG_TYPE(conn, entry, &tagtype) == -1)
@@ -2364,20 +2366,32 @@ static bool current_user_in_group(gid_t gid)
 }
 
 /****************************************************************************
- Should we override a deny ?  Check deprecated 'acl group control'
- and 'dos filemode'
+ Should we override a deny ? Check 'acl group control' and 'dos filemode'.
 ****************************************************************************/
 
-static bool acl_group_override(connection_struct *conn, gid_t prim_gid)
+static bool acl_group_override(connection_struct *conn,
+                               gid_t prim_gid,
+                               const char *fname)
 {
-       if ( (errno == EACCES || errno == EPERM) 
-               && (lp_acl_group_control(SNUM(conn)) || lp_dos_filemode(SNUM(conn)))
-               && current_user_in_group(prim_gid)) 
-       {
-               return True;
-       } 
+       SMB_STRUCT_STAT sbuf;
 
-       return False;
+       if ((errno != EPERM) && (errno != EACCES)) {
+               return false;
+       }
+
+       /* file primary group == user primary or supplementary group */
+       if (lp_acl_group_control(SNUM(conn)) &&
+                       current_user_in_group(prim_gid)) {
+               return true;
+       }
+
+       /* user has writeable permission */
+       if (lp_dos_filemode(SNUM(conn)) &&
+                       can_write_to_file(conn, fname, &sbuf)) {
+               return true;
+       }
+
+       return false;
 }
 
 /****************************************************************************
@@ -2563,7 +2577,7 @@ static bool set_canon_ace_list(files_struct *fsp, canon_ace *the_ace, bool defau
                                *pacl_set_support = False;
                        }
 
-                       if (acl_group_override(conn, prim_gid)) {
+                       if (acl_group_override(conn, prim_gid, fsp->fsp_name)) {
                                int sret;
 
                                DEBUG(5,("set_canon_ace_list: acl group control on and current user in file %s primary group.\n",
@@ -2594,7 +2608,7 @@ static bool set_canon_ace_list(files_struct *fsp, canon_ace *the_ace, bool defau
                                *pacl_set_support = False;
                        }
 
-                       if (acl_group_override(conn, prim_gid)) {
+                       if (acl_group_override(conn, prim_gid, fsp->fsp_name)) {
                                int sret;
 
                                DEBUG(5,("set_canon_ace_list: acl group control on and current user in file %s primary group.\n",
@@ -3214,25 +3228,25 @@ int try_chown(connection_struct *conn, const char *fname, uid_t uid, gid_t gid)
 ****************************************************************************/
 
 static NTSTATUS append_parent_acl(files_struct *fsp,
-                               SMB_STRUCT_STAT *psbuf,
-                               SEC_DESC *psd,
+                               const SEC_DESC *pcsd,
                                SEC_DESC **pp_new_sd)
 {
        SEC_DESC *parent_sd = NULL;
        files_struct *parent_fsp = NULL;
-       TALLOC_CTX *mem_ctx = talloc_parent(psd);
+       TALLOC_CTX *mem_ctx = talloc_tos();
        char *parent_name = NULL;
        SEC_ACE *new_ace = NULL;
-       unsigned int num_aces = psd->dacl->num_aces;
+       unsigned int num_aces = pcsd->dacl->num_aces;
        SMB_STRUCT_STAT sbuf;
        NTSTATUS status;
        int info;
        unsigned int i, j;
-       bool is_dacl_protected = (psd->type & SE_DESC_DACL_PROTECTED);
+       SEC_DESC *psd = dup_sec_desc(talloc_tos(), pcsd);
+       bool is_dacl_protected = (pcsd->type & SE_DESC_DACL_PROTECTED);
 
        ZERO_STRUCT(sbuf);
 
-       if (mem_ctx == NULL) {
+       if (psd == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -3388,10 +3402,12 @@ static NTSTATUS append_parent_acl(files_struct *fsp,
                        parent_name));
        }
 
-       parent_sd->dacl->aces = new_ace;
-       parent_sd->dacl->num_aces = i;
+       psd->dacl->aces = new_ace;
+       psd->dacl->num_aces = i;
+       psd->type &= ~(SE_DESC_DACL_AUTO_INHERITED|
+                         SE_DESC_DACL_AUTO_INHERIT_REQ);
 
-       *pp_new_sd = parent_sd;
+       *pp_new_sd = psd;
        return status;
 }
 
@@ -3401,7 +3417,7 @@ static NTSTATUS append_parent_acl(files_struct *fsp,
  This should be the only external function needed for the UNIX style set ACL.
 ****************************************************************************/
 
-NTSTATUS set_nt_acl(files_struct *fsp, uint32 security_info_sent, SEC_DESC *psd)
+NTSTATUS set_nt_acl(files_struct *fsp, uint32 security_info_sent, const SEC_DESC *psd)
 {
        connection_struct *conn = fsp->conn;
        uid_t user = (uid_t)-1;
@@ -3512,10 +3528,12 @@ NTSTATUS set_nt_acl(files_struct *fsp, uint32 security_info_sent, SEC_DESC *psd)
                              SE_DESC_DACL_AUTO_INHERIT_REQ))==
                        (SE_DESC_DACL_AUTO_INHERITED|
                         SE_DESC_DACL_AUTO_INHERIT_REQ) ) {
-               status = append_parent_acl(fsp, &sbuf, psd, &psd);
+               SEC_DESC *new_sd = NULL;
+               status = append_parent_acl(fsp, psd, &new_sd);
                if (!NT_STATUS_IS_OK(status)) {
                        return status;
                }
+               psd = new_sd;
        }
 
        acl_perms = unpack_canon_ace( fsp, &sbuf, &file_owner_sid, &file_grp_sid,
@@ -3572,7 +3590,7 @@ NTSTATUS set_nt_acl(files_struct *fsp, uint32 security_info_sent, SEC_DESC *psd)
                                        if (SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn, fsp->fsp_name) == -1) {
                                                int sret = -1;
 
-                                               if (acl_group_override(conn, sbuf.st_gid)) {
+                                               if (acl_group_override(conn, sbuf.st_gid, fsp->fsp_name)) {
                                                        DEBUG(5,("set_nt_acl: acl group control on and "
                                                                "current user in file %s primary group. Override delete_def_acl\n",
                                                                fsp->fsp_name ));
@@ -3619,7 +3637,7 @@ NTSTATUS set_nt_acl(files_struct *fsp, uint32 security_info_sent, SEC_DESC *psd)
 
                                        if(SMB_VFS_CHMOD(conn,fsp->fsp_name, posix_perms) == -1) {
                                                int sret = -1;
-                                               if (acl_group_override(conn, sbuf.st_gid)) {
+                                               if (acl_group_override(conn, sbuf.st_gid, fsp->fsp_name)) {
                                                        DEBUG(5,("set_nt_acl: acl group control on and "
                                                                "current user in file %s primary group. Override chmod\n",
                                                                fsp->fsp_name ));
@@ -3684,9 +3702,7 @@ int get_acl_group_bits( connection_struct *conn, const char *fname, mode_t *mode
                SMB_ACL_TAG_T tagtype;
                SMB_ACL_PERMSET_T permset;
 
-               /* get_next... */
-               if (entry_id == SMB_ACL_FIRST_ENTRY)
-                       entry_id = SMB_ACL_NEXT_ENTRY;
+               entry_id = SMB_ACL_NEXT_ENTRY;
 
                if (SMB_VFS_SYS_ACL_GET_TAG_TYPE(conn, entry, &tagtype) ==-1)
                        break;
@@ -3724,9 +3740,7 @@ static int chmod_acl_internals( connection_struct *conn, SMB_ACL_T posix_acl, mo
                SMB_ACL_PERMSET_T permset;
                mode_t perms;
 
-               /* get_next... */
-               if (entry_id == SMB_ACL_FIRST_ENTRY)
-                       entry_id = SMB_ACL_NEXT_ENTRY;
+               entry_id = SMB_ACL_NEXT_ENTRY;
 
                if (SMB_VFS_SYS_ACL_GET_TAG_TYPE(conn, entry, &tagtype) == -1)
                        return -1;
@@ -4146,9 +4160,7 @@ static bool remove_posix_acl(connection_struct *conn, files_struct *fsp, const c
                SMB_ACL_TAG_T tagtype;
                SMB_ACL_PERMSET_T permset;
 
-               /* get_next... */
-               if (entry_id == SMB_ACL_FIRST_ENTRY)
-                       entry_id = SMB_ACL_NEXT_ENTRY;
+               entry_id = SMB_ACL_NEXT_ENTRY;
 
                if (SMB_VFS_SYS_ACL_GET_TAG_TYPE(conn, entry, &tagtype) == -1) {
                        DEBUG(5,("remove_posix_acl: failed to get tagtype from ACL on file %s (%s).\n",