From f51f1cc825bbbee917c4c6a7a79c813a97d415ca Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Mon, 19 Nov 2018 15:17:54 +0100 Subject: [PATCH] vfs_shadow_copy2: nicely deal with attempts to open previous version for writing Bug: https://bugzilla.samba.org/show_bug.cgi?id=13688 --- source3/modules/vfs_shadow_copy2.c | 100 ++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/source3/modules/vfs_shadow_copy2.c b/source3/modules/vfs_shadow_copy2.c index 79c1ee5cf33..cd83ee66587 100644 --- a/source3/modules/vfs_shadow_copy2.c +++ b/source3/modules/vfs_shadow_copy2.c @@ -36,6 +36,8 @@ #include "include/ntioctl.h" #include "util_tdb.h" #include "lib/util_path.h" +#include "libcli/security/security.h" +#include "lib/util/tevent_unix.h" struct shadow_copy2_config { char *gmt_format; @@ -1367,7 +1369,13 @@ static int shadow_copy2_open(vfs_handle_struct *handle, return -1; } - ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode); + /* + * Just pave over the user requested mode and use O_RDONLY. Later + * attempts by the client to write on the handle will fail in the + * pwrite() syscall with EINVAL which we carefully map to EROFS. In sum, + * this matches Windows behaviour. + */ + ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, O_RDONLY); if (ret == -1) { saved_errno = errno; } @@ -2855,6 +2863,96 @@ static int shadow_copy2_get_quota(vfs_handle_struct *handle, return ret; } +static ssize_t shadow_copy2_pwrite(vfs_handle_struct *handle, + files_struct *fsp, + const void *data, + size_t n, + off_t offset) +{ + ssize_t nwritten; + + nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset); + if (nwritten == -1) { + if (errno == EINVAL && fsp->can_write) { + errno = EROFS; + } + } + + return nwritten; +} + +struct shadow_copy2_pwrite_state { + vfs_handle_struct *handle; + files_struct *fsp; + ssize_t ret; + struct vfs_aio_state vfs_aio_state; +}; + +static void shadow_copy2_pwrite_done(struct tevent_req *subreq); + +static struct tevent_req *shadow_copy2_pwrite_send( + struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx, + struct tevent_context *ev, struct files_struct *fsp, + const void *data, size_t n, off_t offset) +{ + struct tevent_req *req = NULL, *subreq = NULL; + struct shadow_copy2_pwrite_state *state = NULL; + + req = tevent_req_create(mem_ctx, &state, + struct shadow_copy2_pwrite_state); + if (req == NULL) { + return NULL; + } + state->handle = handle; + state->fsp = fsp; + + subreq = SMB_VFS_NEXT_PWRITE_SEND(state, + ev, + handle, + fsp, + data, + n, + offset); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, shadow_copy2_pwrite_done, req); + + return req; +} + +static void shadow_copy2_pwrite_done(struct tevent_req *subreq) +{ + struct tevent_req *req = tevent_req_callback_data( + subreq, struct tevent_req); + struct shadow_copy2_pwrite_state *state = tevent_req_data( + req, struct shadow_copy2_pwrite_state); + + state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state); + TALLOC_FREE(subreq); + tevent_req_done(req); +} + +static ssize_t shadow_copy2_pwrite_recv(struct tevent_req *req, + struct vfs_aio_state *vfs_aio_state) +{ + struct shadow_copy2_pwrite_state *state = tevent_req_data( + req, struct shadow_copy2_pwrite_state); + + if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) { + if ((vfs_aio_state->error == EINVAL) && + state->fsp->can_write) + { + vfs_aio_state->error = EROFS; + errno = EROFS; + } + return -1; + } + + *vfs_aio_state = state->vfs_aio_state; + return state->ret; +} + static int shadow_copy2_connect(struct vfs_handle_struct *handle, const char *service, const char *user) { -- 2.17.2