s3: smbd: Reformat users of can_write_to_file().
[samba.git] / source3 / smbd / file_access.c
index 8b282e25b6690339be29fc3402c0c728d4bdd5f4..1b9785d670abd356f28d4dfd97487331a7d32850 100644 (file)
 */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "../libcli/security/security.h"
+#include "../librpc/gen_ndr/ndr_security.h"
+#include "smbd/smbd.h"
 
 #undef  DBGC_CLASS
 #define DBGC_CLASS DBGC_ACLS
 
-/**
- * Security descriptor / NT Token level access check function.
- */
-bool can_access_file_acl(struct connection_struct *conn,
-                               const char * fname,
-                               uint32_t access_mask)
-{
-       NTSTATUS status;
-       uint32_t access_granted;
-       struct security_descriptor *secdesc = NULL;
-
-       if (conn->server_info->utok.uid == 0 || conn->admin_user) {
-               /* I'm sorry sir, I didn't know you were root... */
-               return true;
-       }
-
-       status = SMB_VFS_GET_NT_ACL(conn, fname,
-                                   (OWNER_SECURITY_INFORMATION |
-                                    GROUP_SECURITY_INFORMATION |
-                                    DACL_SECURITY_INFORMATION),
-                                   &secdesc);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(5, ("Could not get acl: %s\n", nt_errstr(status)));
-               return false;
-       }
-
-       status = se_access_check(secdesc, conn->server_info->ptok,
-                                access_mask, &access_granted);
-       TALLOC_FREE(secdesc);
-       return NT_STATUS_IS_OK(status);
-}
-
 /****************************************************************************
  Actually emulate the in-kernel access checking for delete access. We need
  this to successfully return ACCESS_DENIED on a file open for delete access.
 ****************************************************************************/
 
 bool can_delete_file_in_directory(connection_struct *conn,
-                                 const struct smb_filename *smb_fname)
+                       struct files_struct *dirfsp,
+                       const struct smb_filename *smb_fname)
 {
-       SMB_STRUCT_STAT sbuf;
        TALLOC_CTX *ctx = talloc_tos();
-       char *dname = NULL;
+       struct smb_filename *smb_fname_parent = NULL;
+       bool ret;
+
+       SMB_ASSERT(dirfsp == conn->cwd_fsp);
 
        if (!CAN_WRITE(conn)) {
                return False;
        }
 
+       if (!lp_acl_check_permissions(SNUM(conn))) {
+               /* This option means don't check. */
+               return true;
+       }
+
        /* Get the parent directory permission mask and owners. */
-       if (!parent_dirname(ctx, smb_fname->base_name, &dname, NULL)) {
-               return False;
+       ret = parent_smb_fname(ctx, smb_fname, &smb_fname_parent, NULL);
+       if (ret != true) {
+               return false;
        }
-       if(SMB_VFS_STAT(conn, dname, &sbuf) != 0) {
-               return False;
+
+       if(SMB_VFS_STAT(conn, smb_fname_parent) != 0) {
+               ret = false;
+               goto out;
        }
 
        /* fast paths first */
 
-       if (!S_ISDIR(sbuf.st_ex_mode)) {
-               return False;
+       if (!S_ISDIR(smb_fname_parent->st.st_ex_mode)) {
+               ret = false;
+               goto out;
        }
-       if (conn->server_info->utok.uid == 0 || conn->admin_user) {
+       if (get_current_uid(conn) == (uid_t)0) {
                /* I'm sorry sir, I didn't know you were root... */
-               return True;
+               ret = true;
+               goto out;
        }
 
 #ifdef S_ISVTX
        /* sticky bit means delete only by owner of file or by root or
         * by owner of directory. */
-       if (sbuf.st_ex_mode & S_ISVTX) {
-               SMB_STRUCT_STAT sbuf_file;
-               char *fname = NULL;
-               NTSTATUS status;
-
-               status = get_full_smb_filename(talloc_tos(), smb_fname,
-                                              &fname);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return false;
-               }
-               if(SMB_VFS_STAT(conn, fname, &sbuf_file) != 0) {
-                       TALLOC_FREE(fname);
-                       if (errno == ENOENT) {
-                               /* If the file doesn't already exist then
-                                * yes we'll be able to delete it. */
-                               return True;
-                       }
-                       DEBUG(10,("can_delete_file_in_directory: can't "
-                                 "stat file %s (%s)",
-                                 smb_fname_str_dbg(smb_fname),
-                                 strerror(errno) ));
-                       return False;
+       if (smb_fname_parent->st.st_ex_mode & S_ISVTX) {
+               if (!VALID_STAT(smb_fname->st)) {
+                       /* If the file doesn't already exist then
+                        * yes we'll be able to delete it. */
+                       ret = true;
+                       goto out;
                }
-               TALLOC_FREE(fname);
 
                /*
                 * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
@@ -125,12 +93,15 @@ bool can_delete_file_in_directory(connection_struct *conn,
                 * or the owner of the directory as we have no possible
                 * chance of deleting. Otherwise, go on and check the ACL.
                 */
-               if ((conn->server_info->utok.uid != sbuf.st_ex_uid) &&
-                               (conn->server_info->utok.uid != sbuf_file.st_ex_uid)) {
+               if ((get_current_uid(conn) !=
+                       smb_fname_parent->st.st_ex_uid) &&
+                   (get_current_uid(conn) != smb_fname->st.st_ex_uid)) {
                        DEBUG(10,("can_delete_file_in_directory: not "
                                  "owner of file %s or directory %s",
-                                 smb_fname_str_dbg(smb_fname), dname));
-                       return False;
+                                 smb_fname_str_dbg(smb_fname),
+                                 smb_fname_str_dbg(smb_fname_parent)));
+                       ret = false;
+                       goto out;
                }
        }
 #endif
@@ -146,86 +117,48 @@ bool can_delete_file_in_directory(connection_struct *conn,
         * check the file DELETE permission separately.
         */
 
-       return can_access_file_acl(conn, dname, FILE_DELETE_CHILD);
-}
-
-/****************************************************************************
- Actually emulate the in-kernel access checking for read/write access. We need
- this to successfully check for ability to write for dos filetimes.
- Note this doesn't take into account share write permissions.
-****************************************************************************/
-
-bool can_access_file_data(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf, uint32 access_mask)
-{
-       if (!(access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))) {
-               return False;
-       }
-       access_mask &= (FILE_READ_DATA|FILE_WRITE_DATA);
-
-       /* some fast paths first */
-
-       DEBUG(10,("can_access_file_data: requesting 0x%x on file %s\n",
-               (unsigned int)access_mask, fname ));
-
-       if (conn->server_info->utok.uid == 0 || conn->admin_user) {
-               /* I'm sorry sir, I didn't know you were root... */
-               return True;
-       }
-
-       if (!VALID_STAT(*psbuf)) {
-               /* Get the file permission mask and owners. */
-               if(SMB_VFS_STAT(conn, fname, psbuf) != 0) {
-                       return False;
-               }
-       }
-
-       /* Check primary owner access. */
-       if (conn->server_info->utok.uid == psbuf->st_ex_uid) {
-               switch (access_mask) {
-                       case FILE_READ_DATA:
-                               return (psbuf->st_ex_mode & S_IRUSR) ? True : False;
-
-                       case FILE_WRITE_DATA:
-                               return (psbuf->st_ex_mode & S_IWUSR) ? True : False;
-
-                       default: /* FILE_READ_DATA|FILE_WRITE_DATA */
-
-                               if ((psbuf->st_ex_mode & (S_IWUSR|S_IRUSR)) == (S_IWUSR|S_IRUSR)) {
-                                       return True;
-                               } else {
-                                       return False;
-                               }
-               }
-       }
-
-       /* now for ACL checks */
-
-       return can_access_file_acl(conn, fname, access_mask);
+       ret = NT_STATUS_IS_OK(smbd_check_access_rights(conn,
+                               smb_fname_parent,
+                               false,
+                               FILE_DELETE_CHILD));
+ out:
+       TALLOC_FREE(smb_fname_parent);
+       return ret;
 }
 
 /****************************************************************************
  Userspace check for write access.
- Note this doesn't take into account share write permissions.
 ****************************************************************************/
 
-bool can_write_to_file(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf)
+bool can_write_to_file(connection_struct *conn,
+                       const struct smb_filename *smb_fname)
 {
-       return can_access_file_data(conn, fname, psbuf, FILE_WRITE_DATA);
+       return NT_STATUS_IS_OK(smbd_check_access_rights(conn,
+                               smb_fname,
+                               false,
+                               FILE_WRITE_DATA));
 }
 
 /****************************************************************************
  Check for an existing default Windows ACL on a directory.
 ****************************************************************************/
 
-bool directory_has_default_acl(connection_struct *conn, const char *fname)
+bool directory_has_default_acl(connection_struct *conn,
+               struct files_struct *dirfsp,
+               struct smb_filename *smb_fname)
 {
-       /* returns talloced off tos. */
        struct security_descriptor *secdesc = NULL;
        unsigned int i;
-       NTSTATUS status = SMB_VFS_GET_NT_ACL(conn, fname,
-                               DACL_SECURITY_INFORMATION, &secdesc);
+       NTSTATUS status;
+
+       status = SMB_VFS_GET_NT_ACL(conn, smb_fname,
+                                            SECINFO_DACL, talloc_tos(),
+                                            &secdesc);
 
-       if (!NT_STATUS_IS_OK(status) || secdesc == NULL) {
+       if (!NT_STATUS_IS_OK(status) ||
+                       secdesc == NULL ||
+                       secdesc->dacl == NULL) {
+               TALLOC_FREE(secdesc);
                return false;
        }
 
@@ -240,3 +173,61 @@ bool directory_has_default_acl(connection_struct *conn, const char *fname)
        TALLOC_FREE(secdesc);
        return false;
 }
+
+/****************************************************************************
+ Check if setting delete on close is allowed on this fsp.
+****************************************************************************/
+
+NTSTATUS can_set_delete_on_close(files_struct *fsp, uint32_t dosmode)
+{
+       /*
+        * Only allow delete on close for writable files.
+        */
+
+       if ((dosmode & FILE_ATTRIBUTE_READONLY) &&
+           !lp_delete_readonly(SNUM(fsp->conn))) {
+               DEBUG(10,("can_set_delete_on_close: file %s delete on close "
+                         "flag set but file attribute is readonly.\n",
+                         fsp_str_dbg(fsp)));
+               return NT_STATUS_CANNOT_DELETE;
+       }
+
+       /*
+        * Only allow delete on close for writable shares.
+        */
+
+       if (!CAN_WRITE(fsp->conn)) {
+               DEBUG(10,("can_set_delete_on_close: file %s delete on "
+                         "close flag set but write access denied on share.\n",
+                         fsp_str_dbg(fsp)));
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       /*
+        * Only allow delete on close for files/directories opened with delete
+        * intent.
+        */
+
+       if (!(fsp->access_mask & DELETE_ACCESS)) {
+               DEBUG(10,("can_set_delete_on_close: file %s delete on "
+                         "close flag set but delete access denied.\n",
+                         fsp_str_dbg(fsp)));
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       /* Don't allow delete on close for non-empty directories. */
+       if (fsp->fsp_flags.is_directory) {
+               SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
+
+               /* Or the root of a share. */
+               if (ISDOT(fsp->fsp_name->base_name)) {
+                       DEBUG(10,("can_set_delete_on_close: can't set delete on "
+                                 "close for the root of a share.\n"));
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+
+               return can_delete_directory_fsp(fsp);
+       }
+
+       return NT_STATUS_OK;
+}