2 * shadow_copy2: a shadow copy module (second implementation)
4 * Copyright (C) Andrew Tridgell 2007 (portions taken from shadow_copy2)
5 * Copyright (C) Ed Plese 2009
6 * Copyright (C) Volker Lendecke 2011
7 * Copyright (C) Christian Ambach 2011
8 * Copyright (C) Michael Adam 2013
9 * Copyright (C) Rajesh Joseph 2016
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * This is a second implemetation of a shadow copy module for exposing
28 * file system snapshots to windows clients as shadow copies.
30 * See the manual page for documentation.
34 #include "smbd/smbd.h"
35 #include "system/filesys.h"
36 #include "include/ntioctl.h"
38 #include "lib/util_path.h"
39 #include "libcli/security/security.h"
40 #include "lib/util/tevent_unix.h"
42 struct shadow_copy2_config {
48 bool snapdirseverywhere;
49 bool crossmountpoints;
52 bool snapdir_absolute;
54 char *rel_connectpath; /* share root, relative to a snapshot root */
55 char *snapshot_basepath; /* the absolute version of snapdir */
58 /* Data-structure to hold the list of snap entries */
59 struct shadow_copy2_snapentry {
62 struct shadow_copy2_snapentry *next;
63 struct shadow_copy2_snapentry *prev;
66 struct shadow_copy2_snaplist_info {
67 struct shadow_copy2_snapentry *snaplist; /* snapshot list */
68 regex_t *regex; /* Regex to filter snaps */
69 time_t fetch_time; /* snaplist update time */
74 * shadow_copy2 private structure. This structure will be
75 * used to keep module specific information
77 struct shadow_copy2_private {
78 struct shadow_copy2_config *config;
79 struct shadow_copy2_snaplist_info *snaps;
80 char *shadow_cwd; /* Absolute $cwd path. */
81 /* Absolute connectpath - can vary depending on $cwd. */
82 char *shadow_connectpath;
83 /* talloc'ed realpath return. */
84 struct smb_filename *shadow_realpath;
87 static int shadow_copy2_get_shadow_copy_data(
88 vfs_handle_struct *handle, files_struct *fsp,
89 struct shadow_copy_data *shadow_copy2_data,
93 *This function will create a new snapshot list entry and
94 * return to the caller. This entry will also be added to
95 * the global snapshot list.
97 * @param[in] priv shadow_copy2 specific data structure
98 * @return Newly created snapshot entry or NULL on failure
100 static struct shadow_copy2_snapentry *shadow_copy2_create_snapentry(
101 struct shadow_copy2_private *priv)
103 struct shadow_copy2_snapentry *tmpentry = NULL;
105 tmpentry = talloc_zero(priv->snaps, struct shadow_copy2_snapentry);
106 if (tmpentry == NULL) {
107 DBG_ERR("talloc_zero() failed\n");
112 DLIST_ADD(priv->snaps->snaplist, tmpentry);
118 *This function will delete the entire snaplist and reset
119 * priv->snaps->snaplist to NULL.
121 * @param[in] priv shadow_copye specific data structure
123 static void shadow_copy2_delete_snaplist(struct shadow_copy2_private *priv)
125 struct shadow_copy2_snapentry *tmp = NULL;
127 while ((tmp = priv->snaps->snaplist) != NULL) {
128 DLIST_REMOVE(priv->snaps->snaplist, tmp);
134 * Given a timestamp this function searches the global snapshot list
135 * and returns the complete snapshot directory name saved in the entry.
137 * @param[in] priv shadow_copy2 specific structure
138 * @param[in] timestamp timestamp corresponding to one of the snapshot
139 * @param[out] snap_str buffer to copy the actual snapshot name
140 * @param[in] len length of snap_str buffer
142 * @return Length of actual snapshot name, and -1 on failure
144 static ssize_t shadow_copy2_saved_snapname(struct shadow_copy2_private *priv,
145 struct tm *timestamp,
146 char *snap_str, size_t len)
148 ssize_t snaptime_len = -1;
149 struct shadow_copy2_snapentry *entry = NULL;
151 snaptime_len = strftime(snap_str, len, GMT_FORMAT, timestamp);
152 if (snaptime_len == 0) {
153 DBG_ERR("strftime failed\n");
159 for (entry = priv->snaps->snaplist; entry; entry = entry->next) {
160 if (strcmp(entry->time_fmt, snap_str) == 0) {
161 snaptime_len = snprintf(snap_str, len, "%s",
173 * This function will check if snaplist is updated or not. If snaplist
174 * is empty then it will create a new list. Each time snaplist is updated
175 * the time is recorded. If the snapshot time is greater than the snaplist
176 * update time then chances are we are working on an older list. Then discard
177 * the old list and fetch a new snaplist.
179 * @param[in] handle VFS handle struct
180 * @param[in] snap_time time of snapshot
182 * @return true if the list is updated else false
184 static bool shadow_copy2_update_snaplist(struct vfs_handle_struct *handle,
188 bool snaplist_updated = false;
189 struct files_struct fsp = {0};
190 struct smb_filename smb_fname = {0};
191 double seconds = 0.0;
192 struct shadow_copy2_private *priv = NULL;
194 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
197 seconds = difftime(snap_time, priv->snaps->fetch_time);
200 * Fetch the snapshot list if either the snaplist is empty or the
201 * required snapshot time is greater than the last fetched snaplist
204 if (seconds > 0 || (priv->snaps->snaplist == NULL)) {
205 smb_fname.base_name = discard_const_p(char, ".");
206 fsp.fsp_name = &smb_fname;
208 ret = shadow_copy2_get_shadow_copy_data(handle, &fsp,
211 snaplist_updated = true;
213 DBG_ERR("Failed to get shadow copy data\n");
218 return snaplist_updated;
221 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
223 unsigned *pnum_offsets)
225 unsigned num_offsets;
232 while ((p = strchr(p, '/')) != NULL) {
237 offsets = talloc_array(mem_ctx, size_t, num_offsets);
238 if (offsets == NULL) {
244 while ((p = strchr(p, '/')) != NULL) {
245 offsets[num_offsets] = p-str;
251 *pnum_offsets = num_offsets;
256 * Given a timestamp, build the posix level GMT-tag string
257 * based on the configurable format.
259 static ssize_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
261 char *snaptime_string,
265 ssize_t snaptime_len;
266 struct shadow_copy2_config *config;
267 struct shadow_copy2_private *priv;
269 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
272 config = priv->config;
274 if (config->use_sscanf) {
275 snaptime_len = snprintf(snaptime_string,
278 (unsigned long)snapshot);
279 if (snaptime_len <= 0) {
280 DEBUG(10, ("snprintf failed\n"));
284 if (config->use_localtime) {
285 if (localtime_r(&snapshot, &snap_tm) == 0) {
286 DEBUG(10, ("gmtime_r failed\n"));
290 if (gmtime_r(&snapshot, &snap_tm) == 0) {
291 DEBUG(10, ("gmtime_r failed\n"));
296 if (priv->snaps->regex != NULL) {
297 snaptime_len = shadow_copy2_saved_snapname(priv,
298 &snap_tm, snaptime_string, len);
299 if (snaptime_len >= 0)
303 * If we fail to find the snapshot name, chances are
304 * that we have not updated our snaplist. Make sure the
305 * snaplist is updated.
307 if (!shadow_copy2_update_snaplist(handle, snapshot)) {
308 DBG_DEBUG("shadow_copy2_update_snaplist "
313 return shadow_copy2_saved_snapname(priv,
314 &snap_tm, snaptime_string, len);
317 snaptime_len = strftime(snaptime_string,
321 if (snaptime_len == 0) {
322 DEBUG(10, ("strftime failed\n"));
331 * Given a timestamp, build the string to insert into a path
332 * as a path component for creating the local path to the
333 * snapshot at the given timestamp of the input path.
335 * In the case of a parallel snapdir (specified with an
336 * absolute path), this is the initial portion of the
337 * local path of any snapshot file. The complete path is
338 * obtained by appending the portion of the file's path
339 * below the share root's mountpoint.
341 static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
342 struct vfs_handle_struct *handle,
345 fstring snaptime_string;
346 ssize_t snaptime_len = 0;
348 struct shadow_copy2_config *config;
349 struct shadow_copy2_private *priv;
351 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
354 config = priv->config;
356 snaptime_len = shadow_copy2_posix_gmt_string(handle,
359 sizeof(snaptime_string));
360 if (snaptime_len <= 0) {
364 if (config->snapdir_absolute) {
365 result = talloc_asprintf(mem_ctx, "%s/%s",
366 config->snapdir, snaptime_string);
368 result = talloc_asprintf(mem_ctx, "/%s/%s",
369 config->snapdir, snaptime_string);
371 if (result == NULL) {
372 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
379 * Build the posix snapshot path for the connection
380 * at the given timestamp, i.e. the absolute posix path
381 * that contains the snapshot for this file system.
383 * This only applies to classical case, i.e. not
384 * to the "snapdirseverywhere" mode.
386 static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
387 struct vfs_handle_struct *handle,
390 fstring snaptime_string;
391 ssize_t snaptime_len = 0;
393 struct shadow_copy2_private *priv;
395 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
398 snaptime_len = shadow_copy2_posix_gmt_string(handle,
401 sizeof(snaptime_string));
402 if (snaptime_len <= 0) {
406 result = talloc_asprintf(mem_ctx, "%s/%s",
407 priv->config->snapshot_basepath, snaptime_string);
408 if (result == NULL) {
409 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
415 static char *make_path_absolute(TALLOC_CTX *mem_ctx,
416 struct shadow_copy2_private *priv,
419 char *newpath = NULL;
420 char *abs_path = NULL;
422 if (name[0] != '/') {
423 newpath = talloc_asprintf(mem_ctx,
427 if (newpath == NULL) {
432 abs_path = canonicalize_absolute_path(mem_ctx, name);
433 TALLOC_FREE(newpath);
437 /* Return a $cwd-relative path. */
438 static bool make_relative_path(const char *cwd, char *abs_path)
440 size_t cwd_len = strlen(cwd);
441 size_t abs_len = strlen(abs_path);
443 if (abs_len < cwd_len) {
446 if (memcmp(abs_path, cwd, cwd_len) != 0) {
449 /* The cwd_len != 1 case is for $cwd == '/' */
451 abs_path[cwd_len] != '/' &&
452 abs_path[cwd_len] != '\0')
456 if (abs_path[cwd_len] == '/') {
459 memmove(abs_path, &abs_path[cwd_len], abs_len + 1 - cwd_len);
463 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
465 char *gmt, size_t gmt_len);
468 * Check if an incoming filename is already a snapshot converted pathname.
470 * If so, it returns the pathname truncated at the snapshot point which
471 * will be used as the connectpath.
474 static int check_for_converted_path(TALLOC_CTX *mem_ctx,
475 struct vfs_handle_struct *handle,
476 struct shadow_copy2_private *priv,
478 bool *ppath_already_converted,
481 size_t snapdirlen = 0;
482 char *p = strstr_m(abs_path, priv->config->snapdir);
484 char *connect_path = NULL;
485 char snapshot[GMT_NAME_LEN+1];
487 *ppath_already_converted = false;
490 /* Must at least contain shadow:snapdir. */
494 if (priv->config->snapdir[0] == '/' &&
496 /* Absolute shadow:snapdir must be at the start. */
500 snapdirlen = strlen(priv->config->snapdir);
501 if (p[snapdirlen] != '/') {
502 /* shadow:snapdir must end as a separate component. */
506 if (p > abs_path && p[-1] != '/') {
507 /* shadow:snapdir must start as a separate component. */
512 p++; /* Move past the / */
515 * Need to return up to the next path
516 * component after the time.
517 * This will be used as the connectpath.
522 * No next path component.
525 connect_path = talloc_strdup(mem_ctx,
528 connect_path = talloc_strndup(mem_ctx,
532 if (connect_path == NULL) {
537 * Point p at the same offset in connect_path as
541 p = &connect_path[p - abs_path];
544 * Now ensure there is a time string at p.
545 * The SMB-format @GMT-token string is returned
549 if (!shadow_copy2_snapshot_to_gmt(handle,
553 TALLOC_FREE(connect_path);
557 if (pconnectpath != NULL) {
558 *pconnectpath = connect_path;
561 *ppath_already_converted = true;
563 DBG_DEBUG("path |%s| is already converted. "
564 "connect path = |%s|\n",
572 * This function does two things.
574 * 1). Checks if an incoming filename is already a
575 * snapshot converted pathname.
576 * If so, it returns the pathname truncated
577 * at the snapshot point which will be used
578 * as the connectpath, and then does an early return.
580 * 2). Checks if an incoming filename contains an
581 * SMB-layer @GMT- style timestamp.
582 * If so, it strips the timestamp, and returns
583 * both the timestamp and the stripped path
584 * (making it cwd-relative).
587 static bool _shadow_copy2_strip_snapshot_internal(TALLOC_CTX *mem_ctx,
588 struct vfs_handle_struct *handle,
589 const struct smb_filename *smb_fname,
593 bool *_already_converted,
594 const char *function)
596 char *stripped = NULL;
597 struct shadow_copy2_private *priv;
598 char *abs_path = NULL;
600 bool already_converted = false;
603 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
606 DBG_DEBUG("[from %s()] Path '%s'\n",
607 function, smb_fname_str_dbg(smb_fname));
609 if (_already_converted != NULL) {
610 *_already_converted = false;
613 abs_path = make_path_absolute(mem_ctx, priv, smb_fname->base_name);
614 if (abs_path == NULL) {
619 DBG_DEBUG("abs path '%s'\n", abs_path);
621 err = check_for_converted_path(mem_ctx,
628 /* error in conversion. */
633 if (already_converted) {
634 if (_already_converted != NULL) {
635 *_already_converted = true;
640 if (smb_fname->twrp == 0) {
644 if (ptimestamp != NULL) {
645 *ptimestamp = nt_time_to_unix(smb_fname->twrp);
648 if (pstripped != NULL) {
649 stripped = talloc_strdup(mem_ctx, abs_path);
650 if (stripped == NULL) {
655 if (smb_fname->base_name[0] != '/') {
656 ret = make_relative_path(priv->shadow_cwd, stripped);
658 DBG_DEBUG("Path '%s' "
659 "doesn't start with cwd '%s'\n",
660 stripped, priv->shadow_cwd);
666 *pstripped = stripped;
672 TALLOC_FREE(abs_path);
676 #define shadow_copy2_strip_snapshot_internal(mem_ctx, handle, orig_name, \
677 ptimestamp, pstripped, psnappath, _already_converted) \
678 _shadow_copy2_strip_snapshot_internal((mem_ctx), (handle), (orig_name), \
679 (ptimestamp), (pstripped), (psnappath), (_already_converted), \
682 static bool _shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
683 struct vfs_handle_struct *handle,
684 const struct smb_filename *orig_name,
687 const char *function)
689 return _shadow_copy2_strip_snapshot_internal(mem_ctx,
699 #define shadow_copy2_strip_snapshot(mem_ctx, handle, orig_name, \
700 ptimestamp, pstripped) \
701 _shadow_copy2_strip_snapshot((mem_ctx), (handle), (orig_name), \
702 (ptimestamp), (pstripped), __FUNCTION__)
704 static bool _shadow_copy2_strip_snapshot_converted(TALLOC_CTX *mem_ctx,
705 struct vfs_handle_struct *handle,
706 const struct smb_filename *orig_name,
710 const char *function)
712 return _shadow_copy2_strip_snapshot_internal(mem_ctx,
722 #define shadow_copy2_strip_snapshot_converted(mem_ctx, handle, orig_name, \
723 ptimestamp, pstripped, is_converted) \
724 _shadow_copy2_strip_snapshot_converted((mem_ctx), (handle), (orig_name), \
725 (ptimestamp), (pstripped), (is_converted), __FUNCTION__)
727 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
728 vfs_handle_struct *handle)
730 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
735 if (stat(path, &st) != 0) {
742 while ((p = strrchr(path, '/')) && p > path) {
744 if (stat(path, &st) != 0) {
748 if (st.st_dev != dev) {
758 * Convert from a name as handed in via the SMB layer
759 * and a timestamp into the local path of the snapshot
760 * of the provided file at the provided time.
761 * Also return the path in the snapshot corresponding
762 * to the file's share root.
764 static char *shadow_copy2_do_convert(TALLOC_CTX *mem_ctx,
765 struct vfs_handle_struct *handle,
766 const char *name, time_t timestamp,
767 size_t *snaproot_len)
769 struct smb_filename converted_fname;
771 size_t *slashes = NULL;
772 unsigned num_slashes;
776 char *converted = NULL;
777 size_t insertlen, connectlen = 0;
781 struct shadow_copy2_config *config;
782 struct shadow_copy2_private *priv;
783 size_t in_share_offset = 0;
785 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
788 config = priv->config;
790 DEBUG(10, ("converting '%s'\n", name));
792 if (!config->snapdirseverywhere) {
796 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
799 if (snapshot_path == NULL) {
803 if (config->rel_connectpath == NULL) {
804 converted = talloc_asprintf(mem_ctx, "%s/%s",
805 snapshot_path, name);
807 converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
809 config->rel_connectpath,
812 if (converted == NULL) {
816 ZERO_STRUCT(converted_fname);
817 converted_fname.base_name = converted;
819 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
820 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
822 ret, ret == 0 ? "ok" : strerror(errno)));
824 DEBUG(10, ("Found %s\n", converted));
827 if (snaproot_len != NULL) {
828 *snaproot_len = strlen(snapshot_path);
829 if (config->rel_connectpath != NULL) {
831 strlen(config->rel_connectpath) + 1;
839 /* never reached ... */
842 connectlen = strlen(handle->conn->connectpath);
844 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
846 path = talloc_asprintf(
847 mem_ctx, "%s/%s", handle->conn->connectpath, name);
853 pathlen = talloc_get_size(path)-1;
855 if (!shadow_copy2_find_slashes(talloc_tos(), path,
856 &slashes, &num_slashes)) {
860 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
861 if (insert == NULL) {
864 insertlen = talloc_get_size(insert)-1;
867 * Note: We deliberatly don't expensively initialize the
868 * array with talloc_zero here: Putting zero into
869 * converted[pathlen+insertlen] below is sufficient, because
870 * in the following for loop, the insert string is inserted
871 * at various slash places. So the memory up to position
872 * pathlen+insertlen will always be initialized when the
873 * converted string is used.
875 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
876 if (converted == NULL) {
880 if (path[pathlen-1] != '/') {
882 * Append a fake slash to find the snapshot root
885 tmp = talloc_realloc(talloc_tos(), slashes,
886 size_t, num_slashes+1);
891 slashes[num_slashes] = pathlen;
897 if (!config->crossmountpoints) {
898 min_offset = strlen(config->mount_point);
901 memcpy(converted, path, pathlen+1);
902 converted[pathlen+insertlen] = '\0';
904 ZERO_STRUCT(converted_fname);
905 converted_fname.base_name = converted;
907 for (i = num_slashes-1; i>=0; i--) {
913 if (offset < min_offset) {
918 if (offset >= connectlen) {
919 in_share_offset = offset;
922 memcpy(converted+offset, insert, insertlen);
925 memcpy(converted+offset, path + slashes[i],
926 pathlen - slashes[i]);
928 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
930 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
932 ret, ret == 0 ? "ok" : strerror(errno)));
935 if (snaproot_len != NULL) {
936 *snaproot_len = in_share_offset + insertlen;
940 if (errno == ENOTDIR) {
942 * This is a valid condition: We appended the
943 * .snapshots/@GMT.. to a file name. Just try
944 * with the upper levels.
948 if (errno != ENOENT) {
949 /* Other problem than "not found" */
958 DEBUG(10, ("Found %s\n", converted));
965 if (result == NULL) {
968 TALLOC_FREE(converted);
970 TALLOC_FREE(slashes);
972 if (saved_errno != 0) {
979 * Convert from a name as handed in via the SMB layer
980 * and a timestamp into the local path of the snapshot
981 * of the provided file at the provided time.
983 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
984 struct vfs_handle_struct *handle,
985 const char *name, time_t timestamp)
987 return shadow_copy2_do_convert(mem_ctx, handle, name, timestamp, NULL);
991 modify a sbuf return to ensure that inodes in the shadow directory
992 are different from those in the main directory
994 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
995 SMB_STRUCT_STAT *sbuf)
997 struct shadow_copy2_private *priv;
999 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1002 if (priv->config->fixinodes) {
1003 /* some snapshot systems, like GPFS, return the same
1004 device:inode for the snapshot files as the current
1005 files. That breaks the 'restore' button in the shadow copy
1006 GUI, as the client gets a sharing violation.
1008 This is a crude way of allowing both files to be
1009 open at once. It has a slight chance of inode
1010 number collision, but I can't see a better approach
1011 without significant VFS changes
1013 TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
1014 .dsize = strlen(fname) };
1017 shash = tdb_jenkins_hash(&key) & 0xFF000000;
1021 sbuf->st_ex_ino ^= shash;
1025 static int shadow_copy2_renameat(vfs_handle_struct *handle,
1026 files_struct *srcfsp,
1027 const struct smb_filename *smb_fname_src,
1028 files_struct *dstfsp,
1029 const struct smb_filename *smb_fname_dst)
1031 time_t timestamp_src = 0;
1032 time_t timestamp_dst = 0;
1033 char *snappath_src = NULL;
1034 char *snappath_dst = NULL;
1036 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle,
1038 ×tamp_src, NULL, &snappath_src,
1042 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle,
1044 ×tamp_dst, NULL, &snappath_dst,
1048 if (timestamp_src != 0) {
1052 if (timestamp_dst != 0) {
1057 * Don't allow rename on already converted paths.
1059 if (snappath_src != NULL) {
1063 if (snappath_dst != NULL) {
1067 return SMB_VFS_NEXT_RENAMEAT(handle,
1074 static int shadow_copy2_symlinkat(vfs_handle_struct *handle,
1075 const struct smb_filename *link_contents,
1076 struct files_struct *dirfsp,
1077 const struct smb_filename *new_smb_fname)
1079 time_t timestamp_old = 0;
1080 time_t timestamp_new = 0;
1081 char *snappath_old = NULL;
1082 char *snappath_new = NULL;
1084 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1093 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1102 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1107 * Don't allow symlinks on already converted paths.
1109 if ((snappath_old != NULL) || (snappath_new != NULL)) {
1113 return SMB_VFS_NEXT_SYMLINKAT(handle,
1119 static int shadow_copy2_linkat(vfs_handle_struct *handle,
1120 files_struct *srcfsp,
1121 const struct smb_filename *old_smb_fname,
1122 files_struct *dstfsp,
1123 const struct smb_filename *new_smb_fname,
1126 time_t timestamp_old = 0;
1127 time_t timestamp_new = 0;
1128 char *snappath_old = NULL;
1129 char *snappath_new = NULL;
1131 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1140 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1149 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1154 * Don't allow links on already converted paths.
1156 if ((snappath_old != NULL) || (snappath_new != NULL)) {
1160 return SMB_VFS_NEXT_LINKAT(handle,
1168 static int shadow_copy2_stat(vfs_handle_struct *handle,
1169 struct smb_filename *smb_fname)
1171 time_t timestamp = 0;
1172 char *stripped = NULL;
1174 int saved_errno = 0;
1177 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1179 ×tamp, &stripped)) {
1182 if (timestamp == 0) {
1183 return SMB_VFS_NEXT_STAT(handle, smb_fname);
1186 tmp = smb_fname->base_name;
1187 smb_fname->base_name = shadow_copy2_convert(
1188 talloc_tos(), handle, stripped, timestamp);
1189 TALLOC_FREE(stripped);
1191 if (smb_fname->base_name == NULL) {
1192 smb_fname->base_name = tmp;
1196 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1198 saved_errno = errno;
1201 TALLOC_FREE(smb_fname->base_name);
1202 smb_fname->base_name = tmp;
1205 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1207 if (saved_errno != 0) {
1208 errno = saved_errno;
1213 static int shadow_copy2_lstat(vfs_handle_struct *handle,
1214 struct smb_filename *smb_fname)
1216 time_t timestamp = 0;
1217 char *stripped = NULL;
1219 int saved_errno = 0;
1222 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1224 ×tamp, &stripped)) {
1227 if (timestamp == 0) {
1228 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1231 tmp = smb_fname->base_name;
1232 smb_fname->base_name = shadow_copy2_convert(
1233 talloc_tos(), handle, stripped, timestamp);
1234 TALLOC_FREE(stripped);
1236 if (smb_fname->base_name == NULL) {
1237 smb_fname->base_name = tmp;
1241 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1243 saved_errno = errno;
1246 TALLOC_FREE(smb_fname->base_name);
1247 smb_fname->base_name = tmp;
1250 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1252 if (saved_errno != 0) {
1253 errno = saved_errno;
1258 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
1259 SMB_STRUCT_STAT *sbuf)
1261 time_t timestamp = 0;
1262 struct smb_filename *orig_smb_fname = NULL;
1263 struct smb_filename vss_smb_fname;
1264 struct smb_filename *orig_base_smb_fname = NULL;
1265 struct smb_filename vss_base_smb_fname;
1266 char *stripped = NULL;
1267 int saved_errno = 0;
1271 ok = shadow_copy2_strip_snapshot(talloc_tos(), handle,
1273 ×tamp, &stripped);
1278 if (timestamp == 0) {
1279 TALLOC_FREE(stripped);
1280 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1283 vss_smb_fname = *fsp->fsp_name;
1284 vss_smb_fname.base_name = shadow_copy2_convert(talloc_tos(),
1288 TALLOC_FREE(stripped);
1289 if (vss_smb_fname.base_name == NULL) {
1293 orig_smb_fname = fsp->fsp_name;
1294 fsp->fsp_name = &vss_smb_fname;
1296 if (fsp->base_fsp != NULL) {
1297 vss_base_smb_fname = *fsp->base_fsp->fsp_name;
1298 vss_base_smb_fname.base_name = vss_smb_fname.base_name;
1299 orig_base_smb_fname = fsp->base_fsp->fsp_name;
1300 fsp->base_fsp->fsp_name = &vss_base_smb_fname;
1303 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1304 fsp->fsp_name = orig_smb_fname;
1305 if (fsp->base_fsp != NULL) {
1306 fsp->base_fsp->fsp_name = orig_base_smb_fname;
1309 saved_errno = errno;
1313 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
1315 if (saved_errno != 0) {
1316 errno = saved_errno;
1321 static int shadow_copy2_openat(vfs_handle_struct *handle,
1322 const struct files_struct *dirfsp,
1323 const struct smb_filename *smb_fname_in,
1324 struct files_struct *fsp,
1328 struct smb_filename *smb_fname = NULL;
1329 time_t timestamp = 0;
1330 char *stripped = NULL;
1331 bool is_converted = false;
1332 int saved_errno = 0;
1336 smb_fname = full_path_from_dirfsp_atname(talloc_tos(),
1339 if (smb_fname == NULL) {
1344 ok = shadow_copy2_strip_snapshot_converted(talloc_tos(),
1353 if (timestamp == 0) {
1356 * Just pave over the user requested mode and use
1357 * O_RDONLY. Later attempts by the client to write on
1358 * the handle will fail in the pwrite() syscall with
1359 * EINVAL which we carefully map to EROFS. In sum, this
1360 * matches Windows behaviour.
1362 flags &= ~(O_WRONLY | O_RDWR | O_CREAT);
1364 return SMB_VFS_NEXT_OPENAT(handle,
1372 smb_fname->base_name = shadow_copy2_convert(smb_fname,
1376 TALLOC_FREE(stripped);
1377 if (smb_fname->base_name == NULL) {
1378 TALLOC_FREE(smb_fname);
1384 * Just pave over the user requested mode and use O_RDONLY. Later
1385 * attempts by the client to write on the handle will fail in the
1386 * pwrite() syscall with EINVAL which we carefully map to EROFS. In sum,
1387 * this matches Windows behaviour.
1389 flags &= ~(O_WRONLY | O_RDWR | O_CREAT);
1391 ret = SMB_VFS_NEXT_OPENAT(handle,
1398 saved_errno = errno;
1401 TALLOC_FREE(smb_fname);
1403 if (saved_errno != 0) {
1404 errno = saved_errno;
1409 static int shadow_copy2_unlinkat(vfs_handle_struct *handle,
1410 struct files_struct *dirfsp,
1411 const struct smb_filename *smb_fname,
1414 time_t timestamp = 0;
1416 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1418 ×tamp, NULL)) {
1421 if (timestamp != 0) {
1425 return SMB_VFS_NEXT_UNLINKAT(handle,
1431 static int shadow_copy2_chmod(vfs_handle_struct *handle,
1432 const struct smb_filename *smb_fname,
1435 time_t timestamp = 0;
1437 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1444 if (timestamp != 0) {
1448 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1451 static void store_cwd_data(vfs_handle_struct *handle,
1452 const char *connectpath)
1454 struct shadow_copy2_private *priv = NULL;
1455 struct smb_filename *cwd_fname = NULL;
1457 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1460 TALLOC_FREE(priv->shadow_cwd);
1461 cwd_fname = SMB_VFS_NEXT_GETWD(handle, talloc_tos());
1462 if (cwd_fname == NULL) {
1463 smb_panic("getwd failed\n");
1465 DBG_DEBUG("shadow cwd = %s\n", cwd_fname->base_name);
1466 priv->shadow_cwd = talloc_strdup(priv, cwd_fname->base_name);
1467 TALLOC_FREE(cwd_fname);
1468 if (priv->shadow_cwd == NULL) {
1469 smb_panic("talloc failed\n");
1471 TALLOC_FREE(priv->shadow_connectpath);
1473 DBG_DEBUG("shadow connectpath = %s\n", connectpath);
1474 priv->shadow_connectpath = talloc_strdup(priv, connectpath);
1475 if (priv->shadow_connectpath == NULL) {
1476 smb_panic("talloc failed\n");
1481 static int shadow_copy2_chdir(vfs_handle_struct *handle,
1482 const struct smb_filename *smb_fname)
1484 time_t timestamp = 0;
1485 char *stripped = NULL;
1486 char *snappath = NULL;
1488 int saved_errno = 0;
1490 size_t rootpath_len = 0;
1491 struct smb_filename *conv_smb_fname = NULL;
1493 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1502 if (stripped != NULL) {
1503 conv = shadow_copy2_do_convert(talloc_tos(),
1508 TALLOC_FREE(stripped);
1512 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1519 conv_smb_fname = cp_smb_filename(talloc_tos(), smb_fname);
1522 if (conv_smb_fname == NULL) {
1528 ret = SMB_VFS_NEXT_CHDIR(handle, conv_smb_fname);
1530 saved_errno = errno;
1534 if (conv != NULL && rootpath_len != 0) {
1535 conv[rootpath_len] = '\0';
1536 } else if (snappath != 0) {
1540 store_cwd_data(handle, conv);
1543 TALLOC_FREE(stripped);
1545 TALLOC_FREE(conv_smb_fname);
1547 if (saved_errno != 0) {
1548 errno = saved_errno;
1553 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
1554 const struct smb_filename *smb_fname,
1555 struct smb_file_time *ft)
1557 time_t timestamp = 0;
1559 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1561 ×tamp, NULL)) {
1564 if (timestamp != 0) {
1568 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1571 static int shadow_copy2_readlinkat(vfs_handle_struct *handle,
1572 const struct files_struct *dirfsp,
1573 const struct smb_filename *smb_fname,
1577 time_t timestamp = 0;
1578 char *stripped = NULL;
1579 int saved_errno = 0;
1581 struct smb_filename *full_fname = NULL;
1582 struct smb_filename *conv = NULL;
1584 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1587 if (full_fname == NULL) {
1592 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1597 TALLOC_FREE(full_fname);
1601 if (timestamp == 0) {
1602 TALLOC_FREE(full_fname);
1603 TALLOC_FREE(stripped);
1604 return SMB_VFS_NEXT_READLINKAT(handle,
1610 conv = cp_smb_filename(talloc_tos(), full_fname);
1612 TALLOC_FREE(full_fname);
1613 TALLOC_FREE(stripped);
1617 TALLOC_FREE(full_fname);
1618 conv->base_name = shadow_copy2_convert(
1619 conv, handle, stripped, timestamp);
1620 TALLOC_FREE(stripped);
1621 if (conv->base_name == NULL) {
1624 ret = SMB_VFS_NEXT_READLINKAT(handle,
1625 handle->conn->cwd_fsp,
1630 saved_errno = errno;
1633 if (saved_errno != 0) {
1634 errno = saved_errno;
1639 static int shadow_copy2_mknodat(vfs_handle_struct *handle,
1640 files_struct *dirfsp,
1641 const struct smb_filename *smb_fname,
1645 time_t timestamp = 0;
1647 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1649 ×tamp, NULL)) {
1652 if (timestamp != 0) {
1656 return SMB_VFS_NEXT_MKNODAT(handle,
1663 static struct smb_filename *shadow_copy2_realpath(vfs_handle_struct *handle,
1665 const struct smb_filename *smb_fname)
1667 time_t timestamp = 0;
1668 char *stripped = NULL;
1669 struct smb_filename *result_fname = NULL;
1670 struct smb_filename *conv_fname = NULL;
1671 int saved_errno = 0;
1673 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1675 ×tamp, &stripped)) {
1678 if (timestamp == 0) {
1679 return SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
1682 conv_fname = cp_smb_filename(talloc_tos(), smb_fname);
1683 if (conv_fname == NULL) {
1686 conv_fname->base_name = shadow_copy2_convert(
1687 conv_fname, handle, stripped, timestamp);
1688 if (conv_fname->base_name == NULL) {
1692 result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, conv_fname);
1695 if (result_fname == NULL) {
1696 saved_errno = errno;
1698 TALLOC_FREE(conv_fname);
1699 TALLOC_FREE(stripped);
1700 if (saved_errno != 0) {
1701 errno = saved_errno;
1703 return result_fname;
1707 * Check whether a given directory contains a
1708 * snapshot directory as direct subdirectory.
1709 * If yes, return the path of the snapshot-subdir,
1710 * otherwise return NULL.
1712 static char *have_snapdir(struct vfs_handle_struct *handle,
1715 struct smb_filename smb_fname;
1717 struct shadow_copy2_private *priv;
1719 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1722 ZERO_STRUCT(smb_fname);
1723 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1724 path, priv->config->snapdir);
1725 if (smb_fname.base_name == NULL) {
1729 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1730 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1731 return smb_fname.base_name;
1733 TALLOC_FREE(smb_fname.base_name);
1737 static bool check_access_snapdir(struct vfs_handle_struct *handle,
1740 struct smb_filename smb_fname;
1744 ZERO_STRUCT(smb_fname);
1745 smb_fname.base_name = talloc_asprintf(talloc_tos(),
1748 if (smb_fname.base_name == NULL) {
1752 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1753 if (ret != 0 || !S_ISDIR(smb_fname.st.st_ex_mode)) {
1754 TALLOC_FREE(smb_fname.base_name);
1758 status = smbd_check_access_rights(handle->conn,
1759 handle->conn->cwd_fsp,
1763 if (!NT_STATUS_IS_OK(status)) {
1764 DEBUG(0,("user does not have list permission "
1766 smb_fname.base_name));
1767 TALLOC_FREE(smb_fname.base_name);
1770 TALLOC_FREE(smb_fname.base_name);
1775 * Find the snapshot directory (if any) for the given
1776 * filename (which is relative to the share).
1778 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1779 struct vfs_handle_struct *handle,
1780 struct smb_filename *smb_fname)
1783 const char *snapdir;
1784 struct shadow_copy2_config *config;
1785 struct shadow_copy2_private *priv;
1787 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1790 config = priv->config;
1793 * If the non-snapdisrseverywhere mode, we should not search!
1795 if (!config->snapdirseverywhere) {
1796 return config->snapshot_basepath;
1799 path = talloc_asprintf(mem_ctx, "%s/%s",
1800 handle->conn->connectpath,
1801 smb_fname->base_name);
1806 snapdir = have_snapdir(handle, path);
1807 if (snapdir != NULL) {
1812 while ((p = strrchr(path, '/')) && (p > path)) {
1816 snapdir = have_snapdir(handle, path);
1817 if (snapdir != NULL) {
1826 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1828 char *gmt, size_t gmt_len)
1830 struct tm timestamp;
1832 unsigned long int timestamp_long;
1834 struct shadow_copy2_config *config;
1835 struct shadow_copy2_private *priv;
1836 char *tmpstr = NULL;
1838 bool converted = false;
1841 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1844 config = priv->config;
1846 fmt = config->gmt_format;
1849 * If regex is provided, then we will have to parse the
1850 * filename which will contain both the prefix and the time format.
1851 * e.g. <prefix><delimiter><time_format>
1853 if (priv->snaps->regex != NULL) {
1854 tmpstr = talloc_strdup(talloc_tos(), name);
1855 /* point "name" to the time format */
1856 name = strstr(name, priv->config->delimiter);
1860 /* Extract the prefix */
1861 tmp = strstr(tmpstr, priv->config->delimiter);
1868 ret = regexec(priv->snaps->regex, tmpstr, 0, NULL, 0);
1870 DBG_DEBUG("shadow_copy2_snapshot_to_gmt: "
1871 "no regex match for %s\n", tmpstr);
1876 ZERO_STRUCT(timestamp);
1877 if (config->use_sscanf) {
1878 if (sscanf(name, fmt, ×tamp_long) != 1) {
1879 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1880 "no sscanf match %s: %s\n",
1884 timestamp_t = timestamp_long;
1885 gmtime_r(×tamp_t, ×tamp);
1887 if (strptime(name, fmt, ×tamp) == NULL) {
1888 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1889 "no match %s: %s\n",
1893 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1896 if (config->use_localtime) {
1897 timestamp.tm_isdst = -1;
1898 timestamp_t = mktime(×tamp);
1899 gmtime_r(×tamp_t, ×tamp);
1903 strftime(gmt, gmt_len, GMT_FORMAT, ×tamp);
1907 TALLOC_FREE(tmpstr);
1911 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1913 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1916 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1918 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1922 sort the shadow copy data in ascending or descending order
1924 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1925 struct shadow_copy_data *shadow_copy2_data)
1927 int (*cmpfunc)(const void *, const void *);
1929 struct shadow_copy2_private *priv;
1931 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1934 sort = priv->config->sort_order;
1939 if (strcmp(sort, "asc") == 0) {
1940 cmpfunc = shadow_copy2_label_cmp_asc;
1941 } else if (strcmp(sort, "desc") == 0) {
1942 cmpfunc = shadow_copy2_label_cmp_desc;
1947 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1948 shadow_copy2_data->labels)
1950 TYPESAFE_QSORT(shadow_copy2_data->labels,
1951 shadow_copy2_data->num_volumes,
1956 static int shadow_copy2_get_shadow_copy_data(
1957 vfs_handle_struct *handle, files_struct *fsp,
1958 struct shadow_copy_data *shadow_copy2_data,
1962 const char *snapdir;
1963 struct smb_filename *snapdir_smb_fname = NULL;
1964 struct files_struct *dirfsp = NULL;
1965 struct files_struct *fspcwd = NULL;
1967 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1968 struct shadow_copy2_private *priv = NULL;
1969 struct shadow_copy2_snapentry *tmpentry = NULL;
1970 bool get_snaplist = false;
1971 bool access_granted = false;
1972 int open_flags = O_RDONLY;
1977 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1978 if (snapdir == NULL) {
1979 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1980 handle->conn->connectpath));
1985 access_granted = check_access_snapdir(handle, snapdir);
1986 if (!access_granted) {
1987 DEBUG(0,("access denied on listing snapdir %s\n", snapdir));
1992 snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
1997 fsp->fsp_name->flags);
1998 if (snapdir_smb_fname == NULL) {
2003 status = create_internal_dirfsp(handle->conn,
2006 if (!NT_STATUS_IS_OK(status)) {
2007 DBG_WARNING("create_internal_dir_fsp() failed for '%s'"
2008 " - %s\n", snapdir, nt_errstr(status));
2013 status = vfs_at_fspcwd(talloc_tos(), handle->conn, &fspcwd);
2014 if (!NT_STATUS_IS_OK(status)) {
2020 open_flags |= O_DIRECTORY;
2023 fd = SMB_VFS_NEXT_OPENAT(handle,
2030 DBG_WARNING("SMB_VFS_NEXT_OPEN failed for '%s'"
2031 " - %s\n", snapdir, strerror(errno));
2035 fsp_set_fd(dirfsp, fd);
2037 p = SMB_VFS_NEXT_FDOPENDIR(handle, dirfsp, NULL, 0);
2039 DBG_NOTICE("shadow_copy2: SMB_VFS_NEXT_FDOPENDIR() failed for '%s'"
2040 " - %s\n", snapdir, strerror(errno));
2045 if (shadow_copy2_data != NULL) {
2046 shadow_copy2_data->num_volumes = 0;
2047 shadow_copy2_data->labels = NULL;
2050 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2054 * Normally this function is called twice once with labels = false and
2055 * then with labels = true. When labels is false it will return the
2056 * number of volumes so that the caller can allocate memory for that
2057 * many labels. Therefore to eliminate snaplist both the times it is
2058 * good to check if labels is set or not.
2060 * shadow_copy2_data is NULL when we only want to update the list and
2061 * don't want any labels.
2063 if ((priv->snaps->regex != NULL) && (labels || shadow_copy2_data == NULL)) {
2064 get_snaplist = true;
2065 /* Reset the global snaplist */
2066 shadow_copy2_delete_snaplist(priv);
2068 /* Set the current time as snaplist update time */
2069 time(&(priv->snaps->fetch_time));
2072 while ((d = SMB_VFS_NEXT_READDIR(handle, dirfsp, p, NULL))) {
2073 char snapshot[GMT_NAME_LEN+1];
2074 SHADOW_COPY_LABEL *tlabels;
2077 * ignore names not of the right form in the snapshot
2080 if (!shadow_copy2_snapshot_to_gmt(
2082 snapshot, sizeof(snapshot))) {
2084 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
2085 "ignoring %s\n", d->d_name));
2088 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
2089 d->d_name, snapshot));
2093 * Create a snap entry for each successful
2096 tmpentry = shadow_copy2_create_snapentry(priv);
2097 if (tmpentry == NULL) {
2098 DBG_ERR("talloc_zero() failed\n");
2101 tmpentry->snapname = talloc_strdup(tmpentry, d->d_name);
2102 tmpentry->time_fmt = talloc_strdup(tmpentry, snapshot);
2105 if (shadow_copy2_data == NULL) {
2110 /* the caller doesn't want the labels */
2111 shadow_copy2_data->num_volumes++;
2115 tlabels = talloc_realloc(shadow_copy2_data,
2116 shadow_copy2_data->labels,
2118 shadow_copy2_data->num_volumes+1);
2119 if (tlabels == NULL) {
2120 DEBUG(0,("shadow_copy2: out of memory\n"));
2124 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
2127 shadow_copy2_data->num_volumes++;
2128 shadow_copy2_data->labels = tlabels;
2131 shadow_copy2_sort_data(handle, shadow_copy2_data);
2135 TALLOC_FREE(fspcwd );
2137 SMB_VFS_NEXT_CLOSEDIR(handle, p);
2139 if (dirfsp != NULL) {
2141 * VFS_CLOSEDIR implicitly
2142 * closed the associated fd.
2144 fsp_set_fd(dirfsp, -1);
2147 if (dirfsp != NULL) {
2149 file_free(NULL, dirfsp);
2151 TALLOC_FREE(tmp_ctx);
2155 static NTSTATUS shadow_copy2_get_nt_acl_at(vfs_handle_struct *handle,
2156 struct files_struct *dirfsp,
2157 const struct smb_filename *smb_fname,
2158 uint32_t security_info,
2159 TALLOC_CTX *mem_ctx,
2160 struct security_descriptor **ppdesc)
2162 time_t timestamp = 0;
2163 char *stripped = NULL;
2166 struct smb_filename *conv_smb_fname = NULL;
2168 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2173 return map_nt_error_from_unix(errno);
2175 if (timestamp == 0) {
2176 return SMB_VFS_NEXT_GET_NT_ACL_AT(handle,
2183 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2184 TALLOC_FREE(stripped);
2186 return map_nt_error_from_unix(errno);
2188 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2194 if (conv_smb_fname == NULL) {
2196 return NT_STATUS_NO_MEMORY;
2198 status = SMB_VFS_NEXT_GET_NT_ACL_AT(handle,
2205 TALLOC_FREE(conv_smb_fname);
2209 static int shadow_copy2_mkdirat(vfs_handle_struct *handle,
2210 struct files_struct *dirfsp,
2211 const struct smb_filename *smb_fname,
2214 struct smb_filename *full_fname = NULL;
2215 time_t timestamp = 0;
2217 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2220 if (full_fname == NULL) {
2225 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2232 TALLOC_FREE(full_fname);
2233 if (timestamp != 0) {
2237 return SMB_VFS_NEXT_MKDIRAT(handle,
2243 static int shadow_copy2_chflags(vfs_handle_struct *handle,
2244 const struct smb_filename *smb_fname,
2247 time_t timestamp = 0;
2249 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2256 if (timestamp != 0) {
2260 return SMB_VFS_NEXT_CHFLAGS(handle, smb_fname, flags);
2263 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
2264 const struct smb_filename *smb_fname,
2269 time_t timestamp = 0;
2270 char *stripped = NULL;
2272 int saved_errno = 0;
2274 struct smb_filename *conv_smb_fname = NULL;
2276 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2283 if (timestamp == 0) {
2284 return SMB_VFS_NEXT_GETXATTR(handle, smb_fname, aname, value,
2287 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2288 TALLOC_FREE(stripped);
2293 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2299 if (conv_smb_fname == NULL) {
2304 ret = SMB_VFS_NEXT_GETXATTR(handle, conv_smb_fname, aname, value, size);
2306 saved_errno = errno;
2308 TALLOC_FREE(conv_smb_fname);
2310 if (saved_errno != 0) {
2311 errno = saved_errno;
2316 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
2317 const struct smb_filename *smb_fname,
2320 time_t timestamp = 0;
2322 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2329 if (timestamp != 0) {
2333 return SMB_VFS_NEXT_REMOVEXATTR(handle, smb_fname, aname);
2336 static int shadow_copy2_fsetxattr(struct vfs_handle_struct *handle,
2337 struct files_struct *fsp,
2338 const char *aname, const void *value,
2339 size_t size, int flags)
2341 time_t timestamp = 0;
2342 const struct smb_filename *smb_fname = NULL;
2344 smb_fname = fsp->fsp_name;
2345 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2352 if (timestamp != 0) {
2356 return SMB_VFS_NEXT_FSETXATTR(handle, fsp,
2357 aname, value, size, flags);
2360 static NTSTATUS shadow_copy2_create_dfs_pathat(struct vfs_handle_struct *handle,
2361 struct files_struct *dirfsp,
2362 const struct smb_filename *smb_fname,
2363 const struct referral *reflist,
2364 size_t referral_count)
2366 time_t timestamp = 0;
2368 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2373 return NT_STATUS_NO_MEMORY;
2375 if (timestamp != 0) {
2376 return NT_STATUS_MEDIA_WRITE_PROTECTED;
2378 return SMB_VFS_NEXT_CREATE_DFS_PATHAT(handle,
2385 static NTSTATUS shadow_copy2_read_dfs_pathat(struct vfs_handle_struct *handle,
2386 TALLOC_CTX *mem_ctx,
2387 struct files_struct *dirfsp,
2388 struct smb_filename *smb_fname,
2389 struct referral **ppreflist,
2390 size_t *preferral_count)
2392 time_t timestamp = 0;
2393 char *stripped = NULL;
2394 struct smb_filename *conv = NULL;
2397 if (!shadow_copy2_strip_snapshot(mem_ctx,
2402 return NT_STATUS_NO_MEMORY;
2404 if (timestamp == 0) {
2405 return SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
2413 conv = cp_smb_filename(mem_ctx, smb_fname);
2415 TALLOC_FREE(stripped);
2416 return NT_STATUS_NO_MEMORY;
2418 conv->base_name = shadow_copy2_convert(conv,
2422 TALLOC_FREE(stripped);
2423 if (conv->base_name == NULL) {
2425 return NT_STATUS_NO_MEMORY;
2428 status = SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
2435 if (NT_STATUS_IS_OK(status)) {
2436 /* Return any stat(2) info. */
2437 smb_fname->st = conv->st;
2444 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
2445 const struct smb_filename *fname,
2447 TALLOC_CTX *mem_ctx,
2450 struct shadow_copy2_private *priv = NULL;
2451 struct shadow_copy2_config *config = NULL;
2452 time_t timestamp = 0;
2453 char *stripped = NULL;
2455 int saved_errno = 0;
2457 struct smb_filename conv_fname;
2459 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2461 config = priv->config;
2463 DBG_DEBUG("Path=[%s] name=[%s]\n", smb_fname_str_dbg(fname), name);
2465 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2466 ×tamp, &stripped)) {
2467 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
2470 if (timestamp == 0) {
2471 DEBUG(10, ("timestamp == 0\n"));
2472 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, fname, name,
2473 mem_ctx, found_name);
2477 * Note that stripped may be an empty string "" if path was ".". As
2478 * shadow_copy2_convert() combines "" with the shadow-copy tree connect
2479 * root fullpath and get_real_filename_full_scan() has an explicit check
2480 * for "" this works.
2482 DBG_DEBUG("stripped [%s]\n", stripped);
2484 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2486 if (!config->snapdirseverywhere) {
2487 DBG_DEBUG("shadow_copy2_convert [%s] failed\n", stripped);
2492 * We're called in the path traversal loop in unix_convert()
2493 * walking down the directory hierarchy. shadow_copy2_convert()
2494 * will fail if the snapshot directory is futher down in the
2495 * hierachy. Set conv to the original stripped path and try to
2496 * look it up in the filesystem with
2497 * SMB_VFS_NEXT_GET_REAL_FILENAME() or
2498 * get_real_filename_full_scan().
2500 DBG_DEBUG("Use stripped [%s] as conv\n", stripped);
2501 conv = talloc_strdup(talloc_tos(), stripped);
2503 TALLOC_FREE(stripped);
2508 conv_fname = (struct smb_filename) {
2512 DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
2513 "name=[%s]\n", conv, name));
2514 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, &conv_fname, name,
2515 mem_ctx, found_name);
2516 DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
2520 if (errno != EOPNOTSUPP) {
2526 ret = get_real_filename_full_scan(handle->conn,
2533 saved_errno = errno;
2534 DBG_DEBUG("Scan [%s] for [%s] failed\n",
2536 errno = saved_errno;
2540 DBG_DEBUG("Scan [%s] for [%s] returned [%s]\n",
2541 conv, name, *found_name);
2547 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
2548 const struct smb_filename *smb_fname_in)
2550 time_t timestamp = 0;
2551 char *stripped = NULL;
2553 const char *fname = smb_fname_in->base_name;
2554 struct smb_filename smb_fname = {0};
2555 struct smb_filename *result_fname = NULL;
2556 char *result = NULL;
2557 char *parent_dir = NULL;
2558 int saved_errno = 0;
2559 size_t rootpath_len = 0;
2560 struct shadow_copy2_private *priv = NULL;
2562 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2565 DBG_DEBUG("Calc connect path for [%s]\n", fname);
2567 if (priv->shadow_connectpath != NULL) {
2568 DBG_DEBUG("cached connect path is [%s]\n",
2569 priv->shadow_connectpath);
2570 return priv->shadow_connectpath;
2573 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, smb_fname_in,
2574 ×tamp, &stripped)) {
2577 if (timestamp == 0) {
2578 return SMB_VFS_NEXT_CONNECTPATH(handle, smb_fname_in);
2581 tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
2584 if (errno != ENOENT) {
2589 * If the converted path does not exist, and converting
2590 * the parent yields something that does exist, then
2591 * this path refers to something that has not been
2592 * created yet, relative to the parent path.
2593 * The snapshot finding is relative to the parent.
2594 * (usually snapshots are read/only but this is not
2595 * necessarily true).
2596 * This code also covers getting a wildcard in the
2597 * last component, because this function is called
2598 * prior to sanitizing the path, and in SMB1 we may
2599 * get wildcards in path names.
2601 if (!parent_dirname(talloc_tos(), stripped, &parent_dir,
2607 tmp = shadow_copy2_do_convert(talloc_tos(), handle, parent_dir,
2608 timestamp, &rootpath_len);
2614 DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
2615 (int)rootpath_len, tmp);
2617 tmp[rootpath_len] = '\0';
2618 smb_fname = (struct smb_filename) { .base_name = tmp };
2620 result_fname = SMB_VFS_NEXT_REALPATH(handle, priv, &smb_fname);
2621 if (result_fname == NULL) {
2626 * SMB_VFS_NEXT_REALPATH returns a talloc'ed string.
2627 * Don't leak memory.
2629 TALLOC_FREE(priv->shadow_realpath);
2630 priv->shadow_realpath = result_fname;
2631 result = priv->shadow_realpath->base_name;
2633 DBG_DEBUG("connect path is [%s]\n", result);
2636 if (result == NULL) {
2637 saved_errno = errno;
2640 TALLOC_FREE(stripped);
2641 TALLOC_FREE(parent_dir);
2642 if (saved_errno != 0) {
2643 errno = saved_errno;
2648 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
2649 const struct smb_filename *smb_fname,
2654 time_t timestamp = 0;
2655 char *stripped = NULL;
2656 int saved_errno = 0;
2658 struct smb_filename *conv_smb_fname = NULL;
2659 uint64_t ret = (uint64_t)-1;
2661 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2666 return (uint64_t)-1;
2668 if (timestamp == 0) {
2669 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2670 bsize, dfree, dsize);
2672 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2673 TALLOC_FREE(stripped);
2675 return (uint64_t)-1;
2677 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2683 if (conv_smb_fname == NULL) {
2685 return (uint64_t)-1;
2687 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv_smb_fname,
2688 bsize, dfree, dsize);
2689 if (ret == (uint64_t)-1) {
2690 saved_errno = errno;
2693 TALLOC_FREE(conv_smb_fname);
2694 if (saved_errno != 0) {
2695 errno = saved_errno;
2700 static int shadow_copy2_get_quota(vfs_handle_struct *handle,
2701 const struct smb_filename *smb_fname,
2702 enum SMB_QUOTA_TYPE qtype,
2706 time_t timestamp = 0;
2707 char *stripped = NULL;
2709 int saved_errno = 0;
2711 struct smb_filename *conv_smb_fname = NULL;
2713 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2720 if (timestamp == 0) {
2721 return SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname, qtype, id, dq);
2724 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2725 TALLOC_FREE(stripped);
2729 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2735 if (conv_smb_fname == NULL) {
2739 ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv_smb_fname, qtype, id, dq);
2742 saved_errno = errno;
2745 TALLOC_FREE(conv_smb_fname);
2746 if (saved_errno != 0) {
2747 errno = saved_errno;
2753 static ssize_t shadow_copy2_pwrite(vfs_handle_struct *handle,
2761 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2762 if (nwritten == -1) {
2763 if (errno == EBADF && fsp->fsp_flags.can_write) {
2771 struct shadow_copy2_pwrite_state {
2772 vfs_handle_struct *handle;
2775 struct vfs_aio_state vfs_aio_state;
2778 static void shadow_copy2_pwrite_done(struct tevent_req *subreq);
2780 static struct tevent_req *shadow_copy2_pwrite_send(
2781 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
2782 struct tevent_context *ev, struct files_struct *fsp,
2783 const void *data, size_t n, off_t offset)
2785 struct tevent_req *req = NULL, *subreq = NULL;
2786 struct shadow_copy2_pwrite_state *state = NULL;
2788 req = tevent_req_create(mem_ctx, &state,
2789 struct shadow_copy2_pwrite_state);
2793 state->handle = handle;
2796 subreq = SMB_VFS_NEXT_PWRITE_SEND(state,
2803 if (tevent_req_nomem(subreq, req)) {
2804 return tevent_req_post(req, ev);
2806 tevent_req_set_callback(subreq, shadow_copy2_pwrite_done, req);
2811 static void shadow_copy2_pwrite_done(struct tevent_req *subreq)
2813 struct tevent_req *req = tevent_req_callback_data(
2814 subreq, struct tevent_req);
2815 struct shadow_copy2_pwrite_state *state = tevent_req_data(
2816 req, struct shadow_copy2_pwrite_state);
2818 state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
2819 TALLOC_FREE(subreq);
2820 if (state->ret == -1) {
2821 tevent_req_error(req, state->vfs_aio_state.error);
2825 tevent_req_done(req);
2828 static ssize_t shadow_copy2_pwrite_recv(struct tevent_req *req,
2829 struct vfs_aio_state *vfs_aio_state)
2831 struct shadow_copy2_pwrite_state *state = tevent_req_data(
2832 req, struct shadow_copy2_pwrite_state);
2834 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2835 if ((vfs_aio_state->error == EBADF) &&
2836 state->fsp->fsp_flags.can_write)
2838 vfs_aio_state->error = EROFS;
2844 *vfs_aio_state = state->vfs_aio_state;
2848 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
2849 const char *service, const char *user)
2851 struct shadow_copy2_config *config;
2852 struct shadow_copy2_private *priv;
2854 const char *snapdir;
2855 const char *snapprefix = NULL;
2856 const char *delimiter;
2857 const char *gmt_format;
2858 const char *sort_order;
2859 const char *basedir = NULL;
2860 const char *snapsharepath = NULL;
2861 const char *mount_point;
2863 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
2864 (unsigned)handle->conn->cnum,
2865 handle->conn->connectpath));
2867 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
2872 priv = talloc_zero(handle->conn, struct shadow_copy2_private);
2874 DBG_ERR("talloc_zero() failed\n");
2879 priv->snaps = talloc_zero(priv, struct shadow_copy2_snaplist_info);
2880 if (priv->snaps == NULL) {
2881 DBG_ERR("talloc_zero() failed\n");
2886 config = talloc_zero(priv, struct shadow_copy2_config);
2887 if (config == NULL) {
2888 DEBUG(0, ("talloc_zero() failed\n"));
2893 priv->config = config;
2895 gmt_format = lp_parm_const_string(SNUM(handle->conn),
2898 config->gmt_format = talloc_strdup(config, gmt_format);
2899 if (config->gmt_format == NULL) {
2900 DEBUG(0, ("talloc_strdup() failed\n"));
2905 /* config->gmt_format must not contain a path separator. */
2906 if (strchr(config->gmt_format, '/') != NULL) {
2907 DEBUG(0, ("shadow:format %s must not contain a /"
2908 "character. Unable to initialize module.\n",
2909 config->gmt_format));
2914 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
2915 "shadow", "sscanf", false);
2917 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
2918 "shadow", "localtime",
2921 snapdir = lp_parm_const_string(SNUM(handle->conn),
2922 "shadow", "snapdir",
2924 config->snapdir = talloc_strdup(config, snapdir);
2925 if (config->snapdir == NULL) {
2926 DEBUG(0, ("talloc_strdup() failed\n"));
2931 snapprefix = lp_parm_const_string(SNUM(handle->conn),
2932 "shadow", "snapprefix",
2934 if (snapprefix != NULL) {
2935 priv->snaps->regex = talloc_zero(priv->snaps, regex_t);
2936 if (priv->snaps->regex == NULL) {
2937 DBG_ERR("talloc_zero() failed\n");
2942 /* pre-compute regex rule for matching pattern later */
2943 ret = regcomp(priv->snaps->regex, snapprefix, 0);
2945 DBG_ERR("Failed to create regex object\n");
2950 delimiter = lp_parm_const_string(SNUM(handle->conn),
2951 "shadow", "delimiter",
2953 if (delimiter != NULL) {
2954 priv->config->delimiter = talloc_strdup(priv->config, delimiter);
2955 if (priv->config->delimiter == NULL) {
2956 DBG_ERR("talloc_strdup() failed\n");
2962 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
2964 "snapdirseverywhere",
2967 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
2968 "shadow", "crossmountpoints",
2971 if (config->crossmountpoints && !config->snapdirseverywhere) {
2972 DBG_WARNING("Warning: 'crossmountpoints' depends on "
2973 "'snapdirseverywhere'. Disabling crossmountpoints.\n");
2976 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
2977 "shadow", "fixinodes",
2980 sort_order = lp_parm_const_string(SNUM(handle->conn),
2981 "shadow", "sort", "desc");
2982 config->sort_order = talloc_strdup(config, sort_order);
2983 if (config->sort_order == NULL) {
2984 DEBUG(0, ("talloc_strdup() failed\n"));
2989 mount_point = lp_parm_const_string(SNUM(handle->conn),
2990 "shadow", "mountpoint", NULL);
2991 if (mount_point != NULL) {
2992 if (mount_point[0] != '/') {
2993 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
2994 "relative ('%s'), but it has to be an "
2995 "absolute path. Ignoring provided value.\n",
3000 p = strstr(handle->conn->connectpath, mount_point);
3001 if (p != handle->conn->connectpath) {
3002 DBG_WARNING("Warning: the share root (%s) is "
3003 "not a subdirectory of the "
3004 "specified mountpoint (%s). "
3005 "Ignoring provided value.\n",
3006 handle->conn->connectpath,
3013 if (mount_point != NULL) {
3014 config->mount_point = talloc_strdup(config, mount_point);
3015 if (config->mount_point == NULL) {
3016 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
3020 config->mount_point = shadow_copy2_find_mount_point(config,
3022 if (config->mount_point == NULL) {
3023 DBG_WARNING("shadow_copy2_find_mount_point "
3024 "of the share root '%s' failed: %s\n",
3025 handle->conn->connectpath, strerror(errno));
3030 basedir = lp_parm_const_string(SNUM(handle->conn),
3031 "shadow", "basedir", NULL);
3033 if (basedir != NULL) {
3034 if (basedir[0] != '/') {
3035 DEBUG(1, (__location__ " Warning: 'basedir' is "
3036 "relative ('%s'), but it has to be an "
3037 "absolute path. Disabling basedir.\n",
3042 p = strstr(basedir, config->mount_point);
3044 DEBUG(1, ("Warning: basedir (%s) is not a "
3045 "subdirectory of the share root's "
3046 "mount point (%s). "
3047 "Disabling basedir\n",
3048 basedir, config->mount_point));
3054 if (config->snapdirseverywhere && basedir != NULL) {
3055 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
3056 "with 'snapdirseverywhere'. Disabling basedir.\n"));
3060 snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
3061 "snapsharepath", NULL);
3062 if (snapsharepath != NULL) {
3063 if (snapsharepath[0] == '/') {
3064 DBG_WARNING("Warning: 'snapsharepath' is "
3065 "absolute ('%s'), but it has to be a "
3066 "relative path. Disabling snapsharepath.\n",
3068 snapsharepath = NULL;
3070 if (config->snapdirseverywhere && snapsharepath != NULL) {
3071 DBG_WARNING("Warning: 'snapsharepath' is incompatible "
3072 "with 'snapdirseverywhere'. Disabling "
3073 "snapsharepath.\n");
3074 snapsharepath = NULL;
3078 if (basedir != NULL && snapsharepath != NULL) {
3079 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
3080 "'basedir'. Disabling snapsharepath\n");
3081 snapsharepath = NULL;
3084 if (snapsharepath != NULL) {
3085 config->rel_connectpath = talloc_strdup(config, snapsharepath);
3086 if (config->rel_connectpath == NULL) {
3087 DBG_ERR("talloc_strdup() failed\n");
3093 if (basedir == NULL) {
3094 basedir = config->mount_point;
3097 if (config->rel_connectpath == NULL &&
3098 strlen(basedir) < strlen(handle->conn->connectpath)) {
3099 config->rel_connectpath = talloc_strdup(config,
3100 handle->conn->connectpath + strlen(basedir));
3101 if (config->rel_connectpath == NULL) {
3102 DEBUG(0, ("talloc_strdup() failed\n"));
3108 if (config->snapdir[0] == '/') {
3109 config->snapdir_absolute = true;
3111 if (config->snapdirseverywhere == true) {
3112 DEBUG(1, (__location__ " Warning: An absolute snapdir "
3113 "is incompatible with 'snapdirseverywhere', "
3114 "setting 'snapdirseverywhere' to false.\n"));
3115 config->snapdirseverywhere = false;
3118 if (config->crossmountpoints == true) {
3119 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
3120 "is not supported with an absolute snapdir. "
3121 "Disabling it.\n"));
3122 config->crossmountpoints = false;
3125 config->snapshot_basepath = config->snapdir;
3127 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
3128 config->mount_point, config->snapdir);
3129 if (config->snapshot_basepath == NULL) {
3130 DEBUG(0, ("talloc_asprintf() failed\n"));
3136 trim_string(config->mount_point, NULL, "/");
3137 trim_string(config->rel_connectpath, "/", "/");
3138 trim_string(config->snapdir, NULL, "/");
3139 trim_string(config->snapshot_basepath, NULL, "/");
3141 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
3142 " share root: '%s'\n"
3143 " mountpoint: '%s'\n"
3144 " rel share root: '%s'\n"
3146 " snapprefix: '%s'\n"
3147 " delimiter: '%s'\n"
3148 " snapshot base path: '%s'\n"
3151 " snapdirs everywhere: %s\n"
3152 " cross mountpoints: %s\n"
3156 handle->conn->connectpath,
3157 config->mount_point,
3158 config->rel_connectpath,
3162 config->snapshot_basepath,
3164 config->use_sscanf ? "yes" : "no",
3165 config->snapdirseverywhere ? "yes" : "no",
3166 config->crossmountpoints ? "yes" : "no",
3167 config->fixinodes ? "yes" : "no",
3172 SMB_VFS_HANDLE_SET_DATA(handle, priv,
3173 NULL, struct shadow_copy2_private,
3179 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
3180 .connect_fn = shadow_copy2_connect,
3181 .disk_free_fn = shadow_copy2_disk_free,
3182 .get_quota_fn = shadow_copy2_get_quota,
3183 .create_dfs_pathat_fn = shadow_copy2_create_dfs_pathat,
3184 .read_dfs_pathat_fn = shadow_copy2_read_dfs_pathat,
3185 .renameat_fn = shadow_copy2_renameat,
3186 .linkat_fn = shadow_copy2_linkat,
3187 .symlinkat_fn = shadow_copy2_symlinkat,
3188 .stat_fn = shadow_copy2_stat,
3189 .lstat_fn = shadow_copy2_lstat,
3190 .fstat_fn = shadow_copy2_fstat,
3191 .openat_fn = shadow_copy2_openat,
3192 .unlinkat_fn = shadow_copy2_unlinkat,
3193 .chmod_fn = shadow_copy2_chmod,
3194 .chdir_fn = shadow_copy2_chdir,
3195 .ntimes_fn = shadow_copy2_ntimes,
3196 .readlinkat_fn = shadow_copy2_readlinkat,
3197 .mknodat_fn = shadow_copy2_mknodat,
3198 .realpath_fn = shadow_copy2_realpath,
3199 .get_nt_acl_at_fn = shadow_copy2_get_nt_acl_at,
3200 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
3201 .mkdirat_fn = shadow_copy2_mkdirat,
3202 .getxattr_fn = shadow_copy2_getxattr,
3203 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
3204 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
3205 .removexattr_fn = shadow_copy2_removexattr,
3206 .fsetxattr_fn = shadow_copy2_fsetxattr,
3207 .chflags_fn = shadow_copy2_chflags,
3208 .get_real_filename_fn = shadow_copy2_get_real_filename,
3209 .pwrite_fn = shadow_copy2_pwrite,
3210 .pwrite_send_fn = shadow_copy2_pwrite_send,
3211 .pwrite_recv_fn = shadow_copy2_pwrite_recv,
3212 .connectpath_fn = shadow_copy2_connectpath,
3216 NTSTATUS vfs_shadow_copy2_init(TALLOC_CTX *ctx)
3218 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
3219 "shadow_copy2", &vfs_shadow_copy2_fns);