vfs: add vfs_shell_snap module
authorDavid Disseldorp <ddiss@samba.org>
Wed, 30 Jan 2013 13:42:46 +0000 (14:42 +0100)
committerDavid Disseldorp <ddiss@samba.org>
Tue, 14 May 2013 22:49:29 +0000 (00:49 +0200)
The shell_snap VFS module plumbs into the snapshot (aka shadow-copy)
management paths used by Samba's File Server Remote Vss Protocol (FSRVP)
server.
The following shell callouts may be configured in smb.conf:

shell_snap:check path command
- Called when an FSRVP client wishes to check whether a given
  share supports snapshot create/delete requests.
- The command is called with a single <share path> argument.
- The command must return 0 if <share path> is capable of being
  snapshotted.

shell_snap:create command
- Called when an FSRVP client wishes to create a snapshot.
- The command is called with a single <share path> argument.
- The command must return 0 status if the snapshot was
  successfully taken.
- The command must output the path of the newly created snapshot
  to stdout.

shell_snap:delete command
- Called when an FSRVP client wishes to delete a snapshot.
- The command is called with <base share path> and
  <snapshot share path> arguments.
- The command must return 0 status if the snapshot was
  successfully removed.

source3/Makefile.in
source3/configure.in
source3/modules/vfs_shell_snap.c [new file with mode: 0644]
source3/modules/wscript_build
source3/wscript

index 52f76a60e2d967bae5b6118bf87a7995dda409f4..d64d753cf90a3ac0da36c4d2d2cde23a59c748a2 100644 (file)
@@ -918,6 +918,7 @@ VFS_TIME_AUDIT_OBJ = modules/vfs_time_audit.o
 VFS_MEDIA_HARMONY_OBJ = modules/vfs_media_harmony.o
 VFS_BTRFS_OBJ = modules/vfs_btrfs.o
 VFS_SNAPPER_OBJ = modules/vfs_snapper.o
+VFS_SHELL_SNAP_OBJ = modules/vfs_shell_snap.o
 
 PAM_ERRORS_OBJ = ../libcli/auth/pam_errors.o
 PLAINTEXT_AUTH_OBJ = auth/pampass.o auth/pass_check.o $(PAM_ERRORS_OBJ)
@@ -2951,6 +2952,10 @@ bin/snapper.@SHLIBEXT@: $(BINARY_PREREQS) $(VFS_SNAPPER_OBJ)
        @echo "Building plugin $@"
        @$(SHLD_MODULE) $(VFS_SNAPPER_OBJ) $(DBUS_LDFLAGS) $(DBUS_CFLAGS)
 
+bin/shell_snap.@SHLIBEXT@: $(BINARY_PREREQS) $(VFS_SHELL_SNAP_OBJ)
+       @echo "Building plugin $@"
+       @$(SHLD_MODULE) $(VFS_SHELL_SNAP_OBJ)
+
 #########################################################
 ## IdMap NSS plugins
 
index 1eca267e952ef8b0faa3582261acde78203ba0fb..85ed52ef98b46a217bc79b95222b2e7dd1333fc6 100644 (file)
@@ -465,6 +465,7 @@ default_shared_modules="$default_shared_modules vfs_linux_xfs_sgid"
 default_shared_modules="$default_shared_modules vfs_time_audit"
 default_shared_modules="$default_shared_modules vfs_media_harmony"
 default_shared_modules="$default_shared_modules vfs_commit"
+default_shared_modules="$default_shared_modules vfs_shell_snap"
 default_shared_modules="$default_shared_modules idmap_autorid"
 default_shared_modules="$default_shared_modules idmap_tdb2"
 default_shared_modules="$default_shared_modules idmap_rid"
@@ -6482,6 +6483,7 @@ SMB_MODULE(vfs_time_audit, \$(VFS_TIME_AUDIT_OBJ), "bin/time_audit.$SHLIBEXT", V
 SMB_MODULE(vfs_media_harmony, \$(VFS_MEDIA_HARMONY_OBJ), "bin/media_harmony.$SHLIBEXT", VFS)
 SMB_MODULE(vfs_btrfs, \$(VFS_BTRFS_OBJ), "bin/btrfs.$SHLIBEXT", VFS)
 SMB_MODULE(vfs_snapper, \$(VFS_SNAPPER_OBJ), "bin/snapper.$SHLIBEXT", VFS)
+SMB_MODULE(vfs_shell_snap, \$(VFS_SHELL_SNAP_OBJ), "bin/shell_snap.$SHLIBEXT", VFS)
 
 SMB_SUBSYSTEM(VFS,smbd/vfs.o)
 
diff --git a/source3/modules/vfs_shell_snap.c b/source3/modules/vfs_shell_snap.c
new file mode 100644 (file)
index 0000000..8d6c75d
--- /dev/null
@@ -0,0 +1,256 @@
+/*
+ * Module for snapshot management using shell callouts
+ *
+ * Copyright (C) David Disseldorp 2013
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "includes.h"
+#include "include/ntioctl.h"
+#include "system/filesys.h"
+#include "smbd/smbd.h"
+#include "lib/util/tevent_ntstatus.h"
+
+/*
+ * Check whether a path can be shadow copied. Return the base volume, allowing
+ * the caller to determine if multiple paths lie on the same base volume.
+ */
+static NTSTATUS shell_snap_check_path(struct vfs_handle_struct *handle,
+                                     TALLOC_CTX *mem_ctx,
+                                     const char *service_path,
+                                     char **base_volume)
+{
+       NTSTATUS status;
+       const char *cmd;
+       char *cmd_run;
+       int ret;
+       TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
+
+       if (tmp_ctx == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       cmd = lp_parm_const_string(handle->conn->params->service,
+                                  "shell_snap", "check path command", "");
+       if ((cmd == NULL) || (strlen(cmd) == 0)) {
+               DEBUG(0,
+                     ("\"shell_snap:check path command\" not configured\n"));
+               status = NT_STATUS_NOT_SUPPORTED;
+               goto err_tmp_free;
+       }
+
+       /* add service path argument */
+       cmd_run = talloc_asprintf(tmp_ctx, "%s %s", cmd, service_path);
+       if (cmd_run == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto err_tmp_free;
+       }
+
+       ret = smbrun(cmd_run, NULL);
+       if (ret != 0) {
+               DEBUG(0, ("%s failed with %d\n", cmd_run, ret));
+               status = NT_STATUS_NOT_SUPPORTED;
+               goto err_tmp_free;
+       }
+
+       /* assume the service path is the base volume */
+       *base_volume = talloc_strdup(mem_ctx, service_path);
+       if (*base_volume == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto err_tmp_free;
+       }
+       status = NT_STATUS_OK;
+err_tmp_free:
+       talloc_free(tmp_ctx);
+
+       return status;
+}
+
+struct shell_snap_create_state {
+       char *base_path;
+       char *snap_path;
+};
+
+static struct tevent_req *shell_snap_create_send(struct vfs_handle_struct *handle,
+                                                TALLOC_CTX *mem_ctx,
+                                                struct tevent_context *ev,
+                                                const char *base_volume,
+                                                time_t *tstamp,
+                                                bool rw)
+{
+       struct tevent_req *req;
+       struct shell_snap_create_state *create_state;
+       const char *cmd;
+       char *cmd_run;
+       char **qlines;
+       int numlines, ret;
+       int fd = -1;
+
+       req = tevent_req_create(mem_ctx, &create_state,
+                               struct shell_snap_create_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       cmd = lp_parm_const_string(handle->conn->params->service,
+                                  "shell_snap", "create command", "");
+       if ((cmd == NULL) || (strlen(cmd) == 0)) {
+               DEBUG(1, ("\"shell_snap:create command\" not configured\n"));
+               tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
+               return tevent_req_post(req, ev);
+       }
+
+       /* add base vol argument */
+       cmd_run = talloc_asprintf(create_state, "%s %s", cmd, base_volume);
+       if (tevent_req_nomem(cmd_run, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       ret = smbrun(cmd_run, &fd);
+       talloc_free(cmd_run);
+       if (ret != 0) {
+               if (fd != -1)
+                       close(fd);
+               tevent_req_nterror(req, NT_STATUS_UNSUCCESSFUL);
+               return tevent_req_post(req, ev);
+       }
+
+       numlines = 0;
+       qlines = fd_lines_load(fd, &numlines, PATH_MAX + 1, create_state);
+       close(fd);
+
+       /* script must return the snapshot path as a single line */
+       if ((numlines == 0) || (qlines == NULL) || (qlines[0] == NULL)) {
+               tevent_req_nterror(req, NT_STATUS_UNSUCCESSFUL);
+               return tevent_req_post(req, ev);
+       }
+
+       create_state->base_path = talloc_strdup(create_state, base_volume);
+       if (tevent_req_nomem(create_state->base_path, req)) {
+               return tevent_req_post(req, ev);
+       }
+       create_state->snap_path = talloc_strdup(create_state, qlines[0]);
+       if (tevent_req_nomem(create_state->snap_path, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       tevent_req_done(req);
+       return tevent_req_post(req, ev);
+}
+
+static NTSTATUS shell_snap_create_recv(struct vfs_handle_struct *handle,
+                                      struct tevent_req *req,
+                                      TALLOC_CTX *mem_ctx,
+                                      char **base_path, char **snap_path)
+{
+       struct shell_snap_create_state *create_state = tevent_req_data(req,
+                                               struct shell_snap_create_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               tevent_req_received(req);
+               return status;
+       }
+
+       *base_path = talloc_steal(mem_ctx, create_state->base_path);
+       if (*base_path == NULL) {
+               tevent_req_received(req);
+               return NT_STATUS_NO_MEMORY;
+       }
+       *snap_path = talloc_steal(mem_ctx, create_state->snap_path);
+       if (*snap_path == NULL) {
+               tevent_req_received(req);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       tevent_req_received(req);
+       return NT_STATUS_OK;
+}
+
+struct shell_snap_delete_state {
+       bool dummy;
+};
+
+static struct tevent_req *shell_snap_delete_send(struct vfs_handle_struct *handle,
+                                                TALLOC_CTX *mem_ctx,
+                                                struct tevent_context *ev,
+                                                char *base_path,
+                                                char *snap_path)
+{
+       struct tevent_req *req;
+       struct shell_snap_delete_state *delete_state;
+       const char *cmd;
+       char *cmd_run;
+       int ret;
+
+       req = tevent_req_create(mem_ctx, &delete_state,
+                               struct shell_snap_delete_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       cmd = lp_parm_const_string(handle->conn->params->service,
+                                  "shell_snap", "delete command", "");
+       if ((cmd == NULL) || (strlen(cmd) == 0)) {
+               DEBUG(1, ("\"shell_snap:delete command\" not configured\n"));
+               tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
+               return tevent_req_post(req, ev);
+       }
+
+       /* add base path and snap path arguments */
+       cmd_run = talloc_asprintf(delete_state, "%s %s %s",
+                                 cmd, base_path, snap_path);
+       if (tevent_req_nomem(cmd_run, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       ret = smbrun(cmd_run, NULL);
+       talloc_free(cmd_run);
+       if (ret != 0) {
+               tevent_req_nterror(req, NT_STATUS_UNSUCCESSFUL);
+               return tevent_req_post(req, ev);
+       }
+
+       tevent_req_done(req);
+       return tevent_req_post(req, ev);
+}
+
+static NTSTATUS shell_snap_delete_recv(struct vfs_handle_struct *handle,
+                                      struct tevent_req *req)
+{
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               tevent_req_received(req);
+               return status;
+       }
+       tevent_req_received(req);
+       return NT_STATUS_OK;
+}
+
+static struct vfs_fn_pointers shell_snap_fns = {
+       .snap_check_path_fn = shell_snap_check_path,
+       .snap_create_send_fn = shell_snap_create_send,
+       .snap_create_recv_fn = shell_snap_create_recv,
+       .snap_delete_send_fn = shell_snap_delete_send,
+       .snap_delete_recv_fn = shell_snap_delete_recv,
+};
+
+NTSTATUS vfs_shell_snap_init(void);
+NTSTATUS vfs_shell_snap_init(void)
+{
+       return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
+                               "shell_snap", &shell_snap_fns);
+}
index ba4eb7f817b46915d709674bf53002ec23ac7a05..b12063dbb43b65b92ddc93bcbf7bd7a57dd53506 100644 (file)
@@ -51,6 +51,7 @@ VFS_LINUX_XFS_SGID_SRC = 'vfs_linux_xfs_sgid.c'
 VFS_TIME_AUDIT_SRC = 'vfs_time_audit.c'
 VFS_MEDIA_HARMONY_SRC = 'vfs_media_harmony.c'
 VFS_BTRFS_SRC = 'vfs_btrfs.c'
+VFS_SHELL_SNAP_SRC = 'vfs_shell_snap.c'
 
 
 bld.SAMBA3_SUBSYSTEM('NFS4_ACLS',
@@ -490,6 +491,14 @@ bld.SAMBA3_MODULE('vfs_btrfs',
                  internal_module=bld.SAMBA3_IS_STATIC_MODULE('vfs_btrfs'),
                  enabled=bld.SAMBA3_IS_ENABLED_MODULE('vfs_btrfs'))
 
+bld.SAMBA3_MODULE('vfs_shell_snap',
+                 subsystem='vfs',
+                 source=VFS_SHELL_SNAP_SRC,
+                 deps='samba-util',
+                 init_function='',
+                 internal_module=bld.SAMBA3_IS_STATIC_MODULE('vfs_shell_snap'),
+                 enabled=bld.SAMBA3_IS_ENABLED_MODULE('vfs_shell_snap'))
+
 PERFCOUNT_TEST_SRC = 'perfcount_test.c'
 
 bld.SAMBA3_SUBSYSTEM('perfcount',
index 604d8613995e1a565e8711941f544ea52fa5f386..56068aa435c60c564dcb511ace7913c6e1538548 100644 (file)
@@ -1696,7 +1696,7 @@ main() {
                                       auth_script vfs_readahead vfs_xattr_tdb vfs_posix_eadb
                                       vfs_streams_xattr vfs_streams_depot vfs_acl_xattr vfs_acl_tdb
                                       vfs_smb_traffic_analyzer vfs_preopen vfs_catia vfs_scannedonly
-                                     vfs_media_harmony
+                                     vfs_media_harmony vfs_shell_snap
                                      vfs_commit
                                       vfs_crossrename vfs_linux_xfs_sgid
                                       vfs_time_audit idmap_autorid idmap_tdb2