--- /dev/null
+/*
+ * 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);
+}