vfs: perform impersonation in smb_vfs_call_get_dos_attributes_done()
[samba.git] / source3 / smbd / vfs.c
index 59702203612e4ec5d61d3c828204b3ad63455b40..2910128ccf6dfe2666993509260b3b7bffaa333a 100644 (file)
@@ -31,6 +31,7 @@
 #include "transfer_file.h"
 #include "ntioctl.h"
 #include "lib/util/tevent_unix.h"
+#include "lib/util/tevent_ntstatus.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_VFS
@@ -398,78 +399,6 @@ NTSTATUS vfs_file_exist(connection_struct *conn, struct smb_filename *smb_fname)
        return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 }
 
-/****************************************************************************
- Read data from fsp on the vfs. (note: EINTR re-read differs from vfs_write_data)
-****************************************************************************/
-
-ssize_t vfs_read_data(files_struct *fsp, char *buf, size_t byte_count)
-{
-       size_t total=0;
-
-       while (total < byte_count)
-       {
-               ssize_t ret = SMB_VFS_READ(fsp, buf + total,
-                                          byte_count - total);
-
-               if (ret == 0) return total;
-               if (ret == -1) {
-                       if (errno == EINTR)
-                               continue;
-                       else
-                               return -1;
-               }
-               total += ret;
-       }
-       return (ssize_t)total;
-}
-
-/****************************************************************************
- Write data to a fd on the vfs.
-****************************************************************************/
-
-ssize_t vfs_write_data(struct smb_request *req,
-                       files_struct *fsp,
-                       const char *buffer,
-                       size_t N)
-{
-       size_t total=0;
-       ssize_t ret;
-
-       if (req && req->unread_bytes) {
-               int sockfd = req->xconn->transport.sock;
-               int old_flags;
-               SMB_ASSERT(req->unread_bytes == N);
-               /* VFS_RECVFILE must drain the socket
-                * before returning. */
-               req->unread_bytes = 0;
-               /* Ensure the socket is blocking. */
-               old_flags = fcntl(sockfd, F_GETFL, 0);
-               if (set_blocking(sockfd, true) == -1) {
-                       return (ssize_t)-1;
-               }
-               ret = SMB_VFS_RECVFILE(sockfd,
-                                       fsp,
-                                       (off_t)-1,
-                                       N);
-               if (fcntl(sockfd, F_SETFL, old_flags) == -1) {
-                       return (ssize_t)-1;
-               }
-               return ret;
-       }
-
-       while (total < N) {
-               ret = SMB_VFS_WRITE(fsp, buffer + total, N - total);
-
-               if (ret == -1)
-                       return -1;
-               if (ret == 0)
-                       return total;
-
-               total += ret;
-       }
-       return (ssize_t)total;
-}
-
 ssize_t vfs_pwrite_data(struct smb_request *req,
                        files_struct *fsp,
                        const char *buffer,
@@ -858,8 +787,7 @@ const char *vfs_readdirname(connection_struct *conn, void *p,
 int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
 {
        int ret;
-       int saved_errno = 0;
-       struct smb_filename *saved_cwd = NULL;
+       struct smb_filename *old_cwd = conn->cwd_fname;
 
        if (!LastDir) {
                LastDir = SMB_STRDUP("");
@@ -874,24 +802,10 @@ int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
                return 0;
        }
 
-       if (conn->cwd_fname != NULL) {
-               /*
-                * Save off where we are in case we need to return
-                * on vfs_GetWd() failure after successful SMB_VFS_CHDIR().
-                */
-               saved_cwd = cp_smb_filename(conn, conn->cwd_fname);
-               if (saved_cwd == NULL) {
-                       return -1;
-               }
-       }
-
        DEBUG(4,("vfs_ChDir to %s\n", smb_fname->base_name));
 
        ret = SMB_VFS_CHDIR(conn, smb_fname);
        if (ret != 0) {
-               saved_errno = errno;
-               TALLOC_FREE(saved_cwd);
-               errno = saved_errno;
                return -1;
        }
 
@@ -902,7 +816,6 @@ int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
         */
 
        /* conn cache. */
-       TALLOC_FREE(conn->cwd_fname);
        conn->cwd_fname = vfs_GetWd(conn, conn);
        if (conn->cwd_fname == NULL) {
                /*
@@ -911,9 +824,9 @@ int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
                 * Return to original directory
                 * and return -1.
                 */
-               saved_errno = errno;
+               int saved_errno = errno;
 
-               if (saved_cwd == NULL) {
+               if (old_cwd == NULL) {
                        /*
                         * Failed on the very first chdir()+getwd()
                         * for this connection. We can't
@@ -923,16 +836,16 @@ int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
                        /* NOTREACHED */
                        return -1;
                }
+               /* Restore original conn->cwd_fname. */
+               conn->cwd_fname = old_cwd;
 
                /* Return to the previous $cwd. */
-               ret = SMB_VFS_CHDIR(conn, saved_cwd);
+               ret = SMB_VFS_CHDIR(conn, conn->cwd_fname);
                if (ret != 0) {
                        smb_panic("conn->cwd getwd failed\n");
                        /* NOTREACHED */
                        return -1;
                }
-               /* Restore original conn->cwd_fname. */
-               conn->cwd_fname = saved_cwd;
                errno = saved_errno;
                /* And fail the chdir(). */
                return -1;
@@ -945,10 +858,7 @@ int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
 
        DEBUG(4,("vfs_ChDir got %s\n", conn->cwd_fname->base_name));
 
-       TALLOC_FREE(saved_cwd);
-       if (saved_errno != 0) {
-               errno = saved_errno;
-       }
+       TALLOC_FREE(old_cwd);
        return ret;
 }
 
@@ -1069,7 +979,7 @@ struct smb_filename *vfs_GetWd(TALLOC_CTX *ctx, connection_struct *conn)
 
 /*******************************************************************
  Reduce a file name, removing .. elements and checking that
- it is below dir in the heirachy. This uses realpath.
+ it is below dir in the hierarchy. This uses realpath.
  This function must run as root, and will return names
  and valid stat structs that can be checked on open.
 ********************************************************************/
@@ -1258,7 +1168,7 @@ NTSTATUS check_reduced_name_with_privilege(connection_struct *conn,
 
 /*******************************************************************
  Reduce a file name, removing .. elements and checking that
- it is below dir in the heirachy. This uses realpath.
+ it is below dir in the hierarchy. This uses realpath.
 
  If cwd_name == NULL then fname is a client given path relative
  to the root path of the share.
@@ -1729,13 +1639,6 @@ int smb_vfs_call_close(struct vfs_handle_struct *handle,
        return handle->fns->close_fn(handle, fsp);
 }
 
-ssize_t smb_vfs_call_read(struct vfs_handle_struct *handle,
-                         struct files_struct *fsp, void *data, size_t n)
-{
-       VFS_FIND(read);
-       return handle->fns->read_fn(handle, fsp, data, n);
-}
-
 ssize_t smb_vfs_call_pread(struct vfs_handle_struct *handle,
                           struct files_struct *fsp, void *data, size_t n,
                           off_t offset)
@@ -1800,20 +1703,16 @@ ssize_t SMB_VFS_PREAD_RECV(struct tevent_req *req,
 {
        struct smb_vfs_call_pread_state *state = tevent_req_data(
                req, struct smb_vfs_call_pread_state);
+       ssize_t retval;
 
        if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
+               tevent_req_received(req);
                return -1;
        }
        *vfs_aio_state = state->vfs_aio_state;
-       return state->retval;
-}
-
-ssize_t smb_vfs_call_write(struct vfs_handle_struct *handle,
-                          struct files_struct *fsp, const void *data,
-                          size_t n)
-{
-       VFS_FIND(write);
-       return handle->fns->write_fn(handle, fsp, data, n);
+       retval = state->retval;
+       tevent_req_received(req);
+       return retval;
 }
 
 ssize_t smb_vfs_call_pwrite(struct vfs_handle_struct *handle,
@@ -1880,12 +1779,16 @@ ssize_t SMB_VFS_PWRITE_RECV(struct tevent_req *req,
 {
        struct smb_vfs_call_pwrite_state *state = tevent_req_data(
                req, struct smb_vfs_call_pwrite_state);
+       ssize_t retval;
 
        if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
+               tevent_req_received(req);
                return -1;
        }
        *vfs_aio_state = state->vfs_aio_state;
-       return state->retval;
+       retval = state->retval;
+       tevent_req_received(req);
+       return retval;
 }
 
 off_t smb_vfs_call_lseek(struct vfs_handle_struct *handle,
@@ -1921,13 +1824,6 @@ int smb_vfs_call_rename(struct vfs_handle_struct *handle,
        return handle->fns->rename_fn(handle, smb_fname_src, smb_fname_dst);
 }
 
-int smb_vfs_call_fsync(struct vfs_handle_struct *handle,
-                      struct files_struct *fsp)
-{
-       VFS_FIND(fsync);
-       return handle->fns->fsync_fn(handle, fsp);
-}
-
 struct smb_vfs_call_fsync_state {
        int (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
        int retval;
@@ -1980,14 +1876,57 @@ int SMB_VFS_FSYNC_RECV(struct tevent_req *req, struct vfs_aio_state *vfs_aio_sta
 {
        struct smb_vfs_call_fsync_state *state = tevent_req_data(
                req, struct smb_vfs_call_fsync_state);
+       ssize_t retval;
 
        if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
+               tevent_req_received(req);
                return -1;
        }
        *vfs_aio_state = state->vfs_aio_state;
-       return state->retval;
+       retval = state->retval;
+       tevent_req_received(req);
+       return retval;
 }
 
+/*
+ * Synchronous version of fsync, built from backend
+ * async VFS primitives. Uses a temporary sub-event
+ * context (NOT NESTED).
+ */
+
+int smb_vfs_fsync_sync(files_struct *fsp)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct tevent_req *req = NULL;
+       struct vfs_aio_state aio_state = { 0 };
+       int ret = -1;
+       bool ok;
+       struct tevent_context *ev = samba_tevent_context_init(frame);
+
+       if (ev == NULL) {
+               goto out;
+       }
+
+       req = SMB_VFS_FSYNC_SEND(talloc_tos(), ev, fsp);
+       if (req == NULL) {
+               goto out;
+       }
+
+       ok = tevent_req_poll(req, ev);
+       if (!ok) {
+               goto out;
+       }
+
+       ret = SMB_VFS_FSYNC_RECV(req, &aio_state);
+
+  out:
+
+       TALLOC_FREE(frame);
+       if (aio_state.error != 0) {
+               errno = aio_state.error;
+       }
+       return ret;
+}
 
 int smb_vfs_call_stat(struct vfs_handle_struct *handle,
                      struct smb_filename *smb_fname)
@@ -2442,6 +2381,107 @@ NTSTATUS smb_vfs_call_offload_write_recv(struct vfs_handle_struct *handle,
        return handle->fns->offload_write_recv_fn(handle, req, copied);
 }
 
+struct smb_vfs_call_get_dos_attributes_state {
+       files_struct *dir_fsp;
+       NTSTATUS (*recv_fn)(struct tevent_req *req,
+                           struct vfs_aio_state *aio_state,
+                           uint32_t *dosmode);
+       struct vfs_aio_state aio_state;
+       uint32_t dos_attributes;
+};
+
+static void smb_vfs_call_get_dos_attributes_done(struct tevent_req *subreq);
+
+struct tevent_req *smb_vfs_call_get_dos_attributes_send(
+                       TALLOC_CTX *mem_ctx,
+                       struct tevent_context *ev,
+                       struct vfs_handle_struct *handle,
+                       files_struct *dir_fsp,
+                       struct smb_filename *smb_fname)
+{
+       struct tevent_req *req = NULL;
+       struct smb_vfs_call_get_dos_attributes_state *state = NULL;
+       struct tevent_req *subreq = NULL;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct smb_vfs_call_get_dos_attributes_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       VFS_FIND(get_dos_attributes_send);
+
+       *state = (struct smb_vfs_call_get_dos_attributes_state) {
+               .dir_fsp = dir_fsp,
+               .recv_fn = handle->fns->get_dos_attributes_recv_fn,
+       };
+
+       subreq = handle->fns->get_dos_attributes_send_fn(mem_ctx,
+                                                        ev,
+                                                        handle,
+                                                        dir_fsp,
+                                                        smb_fname);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_defer_callback(req, ev);
+
+       tevent_req_set_callback(subreq,
+                               smb_vfs_call_get_dos_attributes_done,
+                               req);
+
+       return req;
+}
+
+static void smb_vfs_call_get_dos_attributes_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req =
+               tevent_req_callback_data(subreq,
+               struct tevent_req);
+       struct smb_vfs_call_get_dos_attributes_state *state =
+               tevent_req_data(req,
+               struct smb_vfs_call_get_dos_attributes_state);
+       NTSTATUS status;
+       bool ok;
+
+       /*
+        * Make sure we run as the user again
+        */
+       ok = change_to_user_by_fsp(state->dir_fsp);
+       SMB_ASSERT(ok);
+
+       status = state->recv_fn(subreq,
+                               &state->aio_state,
+                               &state->dos_attributes);
+       TALLOC_FREE(subreq);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+
+       tevent_req_done(req);
+}
+
+NTSTATUS smb_vfs_call_get_dos_attributes_recv(
+               struct tevent_req *req,
+               struct vfs_aio_state *aio_state,
+               uint32_t *dos_attributes)
+{
+       struct smb_vfs_call_get_dos_attributes_state *state =
+               tevent_req_data(req,
+               struct smb_vfs_call_get_dos_attributes_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               tevent_req_received(req);
+               return status;
+       }
+
+       *aio_state = state->aio_state;
+       *dos_attributes = state->dos_attributes;
+       tevent_req_received(req);
+       return NT_STATUS_OK;
+}
+
 NTSTATUS smb_vfs_call_get_compression(vfs_handle_struct *handle,
                                      TALLOC_CTX *mem_ctx,
                                      struct files_struct *fsp,
@@ -2545,21 +2585,6 @@ NTSTATUS smb_vfs_call_audit_file(struct vfs_handle_struct *handle,
                                          access_denied);
 }
 
-int smb_vfs_call_chmod_acl(struct vfs_handle_struct *handle,
-               const struct smb_filename *smb_fname,
-               mode_t mode)
-{
-       VFS_FIND(chmod_acl);
-       return handle->fns->chmod_acl_fn(handle, smb_fname, mode);
-}
-
-int smb_vfs_call_fchmod_acl(struct vfs_handle_struct *handle,
-                           struct files_struct *fsp, mode_t mode)
-{
-       VFS_FIND(fchmod_acl);
-       return handle->fns->fchmod_acl_fn(handle, fsp, mode);
-}
-
 SMB_ACL_T smb_vfs_call_sys_acl_get_file(struct vfs_handle_struct *handle,
                                        const struct smb_filename *smb_fname,
                                        SMB_ACL_TYPE_T type,
@@ -2632,6 +2657,101 @@ ssize_t smb_vfs_call_getxattr(struct vfs_handle_struct *handle,
        return handle->fns->getxattr_fn(handle, smb_fname, name, value, size);
 }
 
+
+struct smb_vfs_call_getxattrat_state {
+       ssize_t (*recv_fn)(struct tevent_req *req,
+                          struct vfs_aio_state *aio_state,
+                          TALLOC_CTX *mem_ctx,
+                          uint8_t **xattr_value);
+       ssize_t retval;
+       uint8_t *xattr_value;
+       struct vfs_aio_state aio_state;
+};
+
+static void smb_vfs_call_getxattrat_done(struct tevent_req *subreq);
+
+struct tevent_req *smb_vfs_call_getxattrat_send(
+                       TALLOC_CTX *mem_ctx,
+                       struct tevent_context *ev,
+                       struct vfs_handle_struct *handle,
+                       files_struct *dir_fsp,
+                       const struct smb_filename *smb_fname,
+                       const char *xattr_name,
+                       size_t alloc_hint)
+{
+       struct tevent_req *req = NULL;
+       struct smb_vfs_call_getxattrat_state *state = NULL;
+       struct tevent_req *subreq = NULL;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct smb_vfs_call_getxattrat_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       VFS_FIND(getxattrat_send);
+       state->recv_fn = handle->fns->getxattrat_recv_fn;
+
+       subreq = handle->fns->getxattrat_send_fn(mem_ctx,
+                                                ev,
+                                                handle,
+                                                dir_fsp,
+                                                smb_fname,
+                                                xattr_name,
+                                                alloc_hint);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_defer_callback(req, ev);
+
+       tevent_req_set_callback(subreq, smb_vfs_call_getxattrat_done, req);
+       return req;
+}
+
+static void smb_vfs_call_getxattrat_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct smb_vfs_call_getxattrat_state *state = tevent_req_data(
+               req, struct smb_vfs_call_getxattrat_state);
+
+       state->retval = state->recv_fn(subreq,
+                                      &state->aio_state,
+                                      state,
+                                      &state->xattr_value);
+       TALLOC_FREE(subreq);
+       if (state->retval == -1) {
+               tevent_req_error(req, state->aio_state.error);
+               return;
+       }
+
+       tevent_req_done(req);
+}
+
+ssize_t smb_vfs_call_getxattrat_recv(struct tevent_req *req,
+                                    struct vfs_aio_state *aio_state,
+                                    TALLOC_CTX *mem_ctx,
+                                    uint8_t **xattr_value)
+{
+       struct smb_vfs_call_getxattrat_state *state = tevent_req_data(
+               req, struct smb_vfs_call_getxattrat_state);
+       size_t xattr_size;
+
+       if (tevent_req_is_unix_error(req, &aio_state->error)) {
+               tevent_req_received(req);
+               return -1;
+       }
+
+       *aio_state = state->aio_state;
+       xattr_size = state->retval;
+       if (xattr_value != NULL) {
+               *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
+       }
+
+       tevent_req_received(req);
+       return xattr_size;
+}
+
 ssize_t smb_vfs_call_fgetxattr(struct vfs_handle_struct *handle,
                               struct files_struct *fsp, const char *name,
                               void *value, size_t size)