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 char *orig_name,
593 bool *_already_converted)
596 time_t timestamp = 0;
599 char *stripped = NULL;
600 size_t rest_len, dst_len;
601 struct shadow_copy2_private *priv;
602 ptrdiff_t len_before_gmt;
603 const char *name = orig_name;
604 char *abs_path = NULL;
606 bool already_converted = false;
609 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
612 DEBUG(10, (__location__ ": enter path '%s'\n", name));
614 if (_already_converted != NULL) {
615 *_already_converted = false;
618 abs_path = make_path_absolute(mem_ctx, priv, name);
619 if (abs_path == NULL) {
625 DEBUG(10, (__location__ ": abs path '%s'\n", name));
627 err = check_for_converted_path(mem_ctx,
634 /* error in conversion. */
639 if (already_converted) {
640 if (_already_converted != NULL) {
641 *_already_converted = true;
647 * From here we're only looking to strip an
648 * SMB-layer @GMT- token.
651 p = strstr_m(name, "@GMT-");
653 DEBUG(11, ("@GMT not found\n"));
656 if ((p > name) && (p[-1] != '/')) {
657 /* the GMT-token does not start a path-component */
658 DEBUG(10, ("not at start, p=%p, name=%p, p[-1]=%d\n",
659 p, name, (int)p[-1]));
663 len_before_gmt = p - name;
665 q = strptime(p, GMT_FORMAT, &tm);
667 DEBUG(10, ("strptime failed\n"));
671 timestamp = timegm(&tm);
672 if (timestamp == (time_t)-1) {
673 DEBUG(10, ("timestamp==-1\n"));
678 * The name consists of only the GMT token or the GMT
679 * token is at the end of the path. XP seems to send
680 * @GMT- at the end under certain circumstances even
681 * with a path prefix.
683 if (pstripped != NULL) {
684 if (len_before_gmt > 1) {
686 * There is a path (and not only a slash)
687 * before the @GMT-. Remove the trailing
692 stripped = talloc_strndup(mem_ctx, name,
694 if (stripped == NULL) {
698 if (orig_name[0] != '/') {
699 if (make_relative_path(priv->shadow_cwd,
700 stripped) == false) {
701 DEBUG(10, (__location__ ": path '%s' "
702 "doesn't start with cwd '%s'\n",
703 stripped, priv->shadow_cwd));
709 *pstripped = stripped;
711 *ptimestamp = timestamp;
716 * It is not a complete path component, i.e. the path
717 * component continues after the gmt-token.
719 DEBUG(10, ("q[0] = %d\n", (int)q[0]));
724 rest_len = strlen(q);
725 dst_len = len_before_gmt + rest_len;
727 if (pstripped != NULL) {
728 stripped = talloc_array(mem_ctx, char, dst_len+1);
729 if (stripped == NULL) {
734 memcpy(stripped, name, len_before_gmt);
737 memcpy(stripped + len_before_gmt, q, rest_len);
739 stripped[dst_len] = '\0';
740 if (orig_name[0] != '/') {
741 if (make_relative_path(priv->shadow_cwd,
742 stripped) == false) {
743 DEBUG(10, (__location__ ": path '%s' "
744 "doesn't start with cwd '%s'\n",
745 stripped, priv->shadow_cwd));
751 *pstripped = stripped;
753 *ptimestamp = timestamp;
757 TALLOC_FREE(abs_path);
761 static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
762 struct vfs_handle_struct *handle,
763 const char *orig_name,
767 return shadow_copy2_strip_snapshot_internal(mem_ctx,
776 static bool shadow_copy2_strip_snapshot_converted(TALLOC_CTX *mem_ctx,
777 struct vfs_handle_struct *handle,
778 const char *orig_name,
783 return shadow_copy2_strip_snapshot_internal(mem_ctx,
792 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
793 vfs_handle_struct *handle)
795 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
800 if (stat(path, &st) != 0) {
807 while ((p = strrchr(path, '/')) && p > path) {
809 if (stat(path, &st) != 0) {
813 if (st.st_dev != dev) {
823 * Convert from a name as handed in via the SMB layer
824 * and a timestamp into the local path of the snapshot
825 * of the provided file at the provided time.
826 * Also return the path in the snapshot corresponding
827 * to the file's share root.
829 static char *shadow_copy2_do_convert(TALLOC_CTX *mem_ctx,
830 struct vfs_handle_struct *handle,
831 const char *name, time_t timestamp,
832 size_t *snaproot_len)
834 struct smb_filename converted_fname;
836 size_t *slashes = NULL;
837 unsigned num_slashes;
841 char *converted = NULL;
842 size_t insertlen, connectlen = 0;
846 struct shadow_copy2_config *config;
847 struct shadow_copy2_private *priv;
848 size_t in_share_offset = 0;
850 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
853 config = priv->config;
855 DEBUG(10, ("converting '%s'\n", name));
857 if (!config->snapdirseverywhere) {
861 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
864 if (snapshot_path == NULL) {
868 if (config->rel_connectpath == NULL) {
869 converted = talloc_asprintf(mem_ctx, "%s/%s",
870 snapshot_path, name);
872 converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
874 config->rel_connectpath,
877 if (converted == NULL) {
881 ZERO_STRUCT(converted_fname);
882 converted_fname.base_name = converted;
884 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
885 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
887 ret, ret == 0 ? "ok" : strerror(errno)));
889 DEBUG(10, ("Found %s\n", converted));
892 if (snaproot_len != NULL) {
893 *snaproot_len = strlen(snapshot_path);
894 if (config->rel_connectpath != NULL) {
896 strlen(config->rel_connectpath) + 1;
904 /* never reached ... */
907 connectlen = strlen(handle->conn->connectpath);
909 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
911 path = talloc_asprintf(
912 mem_ctx, "%s/%s", handle->conn->connectpath, name);
918 pathlen = talloc_get_size(path)-1;
920 if (!shadow_copy2_find_slashes(talloc_tos(), path,
921 &slashes, &num_slashes)) {
925 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
926 if (insert == NULL) {
929 insertlen = talloc_get_size(insert)-1;
932 * Note: We deliberatly don't expensively initialize the
933 * array with talloc_zero here: Putting zero into
934 * converted[pathlen+insertlen] below is sufficient, because
935 * in the following for loop, the insert string is inserted
936 * at various slash places. So the memory up to position
937 * pathlen+insertlen will always be initialized when the
938 * converted string is used.
940 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
941 if (converted == NULL) {
945 if (path[pathlen-1] != '/') {
947 * Append a fake slash to find the snapshot root
950 tmp = talloc_realloc(talloc_tos(), slashes,
951 size_t, num_slashes+1);
956 slashes[num_slashes] = pathlen;
962 if (!config->crossmountpoints) {
963 min_offset = strlen(config->mount_point);
966 memcpy(converted, path, pathlen+1);
967 converted[pathlen+insertlen] = '\0';
969 ZERO_STRUCT(converted_fname);
970 converted_fname.base_name = converted;
972 for (i = num_slashes-1; i>=0; i--) {
978 if (offset < min_offset) {
983 if (offset >= connectlen) {
984 in_share_offset = offset;
987 memcpy(converted+offset, insert, insertlen);
990 memcpy(converted+offset, path + slashes[i],
991 pathlen - slashes[i]);
993 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
995 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
997 ret, ret == 0 ? "ok" : strerror(errno)));
1000 if (snaproot_len != NULL) {
1001 *snaproot_len = in_share_offset + insertlen;
1005 if (errno == ENOTDIR) {
1007 * This is a valid condition: We appended the
1008 * .snapshots/@GMT.. to a file name. Just try
1009 * with the upper levels.
1013 if (errno != ENOENT) {
1014 /* Other problem than "not found" */
1023 DEBUG(10, ("Found %s\n", converted));
1030 if (result == NULL) {
1031 saved_errno = errno;
1033 TALLOC_FREE(converted);
1034 TALLOC_FREE(insert);
1035 TALLOC_FREE(slashes);
1037 if (saved_errno != 0) {
1038 errno = saved_errno;
1044 * Convert from a name as handed in via the SMB layer
1045 * and a timestamp into the local path of the snapshot
1046 * of the provided file at the provided time.
1048 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
1049 struct vfs_handle_struct *handle,
1050 const char *name, time_t timestamp)
1052 return shadow_copy2_do_convert(mem_ctx, handle, name, timestamp, NULL);
1056 modify a sbuf return to ensure that inodes in the shadow directory
1057 are different from those in the main directory
1059 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
1060 SMB_STRUCT_STAT *sbuf)
1062 struct shadow_copy2_private *priv;
1064 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1067 if (priv->config->fixinodes) {
1068 /* some snapshot systems, like GPFS, return the name
1069 device:inode for the snapshot files as the current
1070 files. That breaks the 'restore' button in the shadow copy
1071 GUI, as the client gets a sharing violation.
1073 This is a crude way of allowing both files to be
1074 open at once. It has a slight chance of inode
1075 number collision, but I can't see a better approach
1076 without significant VFS changes
1078 TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
1079 .dsize = strlen(fname) };
1082 shash = tdb_jenkins_hash(&key) & 0xFF000000;
1086 sbuf->st_ex_ino ^= shash;
1090 static int shadow_copy2_renameat(vfs_handle_struct *handle,
1091 files_struct *srcfsp,
1092 const struct smb_filename *smb_fname_src,
1093 files_struct *dstfsp,
1094 const struct smb_filename *smb_fname_dst)
1096 time_t timestamp_src = 0;
1097 time_t timestamp_dst = 0;
1098 char *snappath_src = NULL;
1099 char *snappath_dst = NULL;
1101 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle,
1102 smb_fname_src->base_name,
1103 ×tamp_src, NULL, &snappath_src,
1107 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle,
1108 smb_fname_dst->base_name,
1109 ×tamp_dst, NULL, &snappath_dst,
1113 if (timestamp_src != 0) {
1117 if (timestamp_dst != 0) {
1122 * Don't allow rename on already converted paths.
1124 if (snappath_src != NULL) {
1128 if (snappath_dst != NULL) {
1132 return SMB_VFS_NEXT_RENAMEAT(handle,
1139 static int shadow_copy2_symlinkat(vfs_handle_struct *handle,
1140 const char *link_contents,
1141 struct files_struct *dirfsp,
1142 const struct smb_filename *new_smb_fname)
1144 time_t timestamp_old = 0;
1145 time_t timestamp_new = 0;
1146 char *snappath_old = NULL;
1147 char *snappath_new = NULL;
1149 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1158 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1160 new_smb_fname->base_name,
1167 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1172 * Don't allow symlinks on already converted paths.
1174 if ((snappath_old != NULL) || (snappath_new != NULL)) {
1178 return SMB_VFS_NEXT_SYMLINKAT(handle,
1184 static int shadow_copy2_linkat(vfs_handle_struct *handle,
1185 files_struct *srcfsp,
1186 const struct smb_filename *old_smb_fname,
1187 files_struct *dstfsp,
1188 const struct smb_filename *new_smb_fname,
1191 time_t timestamp_old = 0;
1192 time_t timestamp_new = 0;
1193 char *snappath_old = NULL;
1194 char *snappath_new = NULL;
1196 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1198 old_smb_fname->base_name,
1205 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1207 new_smb_fname->base_name,
1214 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1219 * Don't allow links on already converted paths.
1221 if ((snappath_old != NULL) || (snappath_new != NULL)) {
1225 return SMB_VFS_NEXT_LINKAT(handle,
1233 static int shadow_copy2_stat(vfs_handle_struct *handle,
1234 struct smb_filename *smb_fname)
1236 time_t timestamp = 0;
1237 char *stripped = NULL;
1239 int saved_errno = 0;
1242 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1243 smb_fname->base_name,
1244 ×tamp, &stripped)) {
1247 if (timestamp == 0) {
1248 return SMB_VFS_NEXT_STAT(handle, smb_fname);
1251 tmp = smb_fname->base_name;
1252 smb_fname->base_name = shadow_copy2_convert(
1253 talloc_tos(), handle, stripped, timestamp);
1254 TALLOC_FREE(stripped);
1256 if (smb_fname->base_name == NULL) {
1257 smb_fname->base_name = tmp;
1261 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1263 saved_errno = errno;
1266 TALLOC_FREE(smb_fname->base_name);
1267 smb_fname->base_name = tmp;
1270 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1272 if (saved_errno != 0) {
1273 errno = saved_errno;
1278 static int shadow_copy2_lstat(vfs_handle_struct *handle,
1279 struct smb_filename *smb_fname)
1281 time_t timestamp = 0;
1282 char *stripped = NULL;
1284 int saved_errno = 0;
1287 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1288 smb_fname->base_name,
1289 ×tamp, &stripped)) {
1292 if (timestamp == 0) {
1293 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1296 tmp = smb_fname->base_name;
1297 smb_fname->base_name = shadow_copy2_convert(
1298 talloc_tos(), handle, stripped, timestamp);
1299 TALLOC_FREE(stripped);
1301 if (smb_fname->base_name == NULL) {
1302 smb_fname->base_name = tmp;
1306 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1308 saved_errno = errno;
1311 TALLOC_FREE(smb_fname->base_name);
1312 smb_fname->base_name = tmp;
1315 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1317 if (saved_errno != 0) {
1318 errno = saved_errno;
1323 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
1324 SMB_STRUCT_STAT *sbuf)
1326 time_t timestamp = 0;
1327 struct smb_filename *orig_smb_fname = NULL;
1328 struct smb_filename vss_smb_fname;
1329 struct smb_filename *orig_base_smb_fname = NULL;
1330 struct smb_filename vss_base_smb_fname;
1331 char *stripped = NULL;
1332 int saved_errno = 0;
1336 ok = shadow_copy2_strip_snapshot(talloc_tos(), handle,
1337 fsp->fsp_name->base_name,
1338 ×tamp, &stripped);
1343 if (timestamp == 0) {
1344 TALLOC_FREE(stripped);
1345 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1348 vss_smb_fname = *fsp->fsp_name;
1349 vss_smb_fname.base_name = shadow_copy2_convert(talloc_tos(),
1353 TALLOC_FREE(stripped);
1354 if (vss_smb_fname.base_name == NULL) {
1358 orig_smb_fname = fsp->fsp_name;
1359 fsp->fsp_name = &vss_smb_fname;
1361 if (fsp->base_fsp != NULL) {
1362 vss_base_smb_fname = *fsp->base_fsp->fsp_name;
1363 vss_base_smb_fname.base_name = vss_smb_fname.base_name;
1364 orig_base_smb_fname = fsp->base_fsp->fsp_name;
1365 fsp->base_fsp->fsp_name = &vss_base_smb_fname;
1368 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1369 fsp->fsp_name = orig_smb_fname;
1370 if (fsp->base_fsp != NULL) {
1371 fsp->base_fsp->fsp_name = orig_base_smb_fname;
1374 saved_errno = errno;
1378 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
1380 if (saved_errno != 0) {
1381 errno = saved_errno;
1386 static int shadow_copy2_open(vfs_handle_struct *handle,
1387 struct smb_filename *smb_fname, files_struct *fsp,
1388 int flags, mode_t mode)
1390 time_t timestamp = 0;
1391 char *stripped = NULL;
1393 bool is_converted = false;
1394 int saved_errno = 0;
1397 if (!shadow_copy2_strip_snapshot_converted(talloc_tos(), handle,
1398 smb_fname->base_name,
1399 ×tamp, &stripped,
1403 if (timestamp == 0) {
1406 * Just pave over the user requested mode and use
1407 * O_RDONLY. Later attempts by the client to write on
1408 * the handle will fail in the pwrite() syscall with
1409 * EINVAL which we carefully map to EROFS. In sum, this
1410 * matches Windows behaviour.
1414 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1417 tmp = smb_fname->base_name;
1418 smb_fname->base_name = shadow_copy2_convert(
1419 talloc_tos(), handle, stripped, timestamp);
1420 TALLOC_FREE(stripped);
1422 if (smb_fname->base_name == NULL) {
1423 smb_fname->base_name = tmp;
1428 * Just pave over the user requested mode and use O_RDONLY. Later
1429 * attempts by the client to write on the handle will fail in the
1430 * pwrite() syscall with EINVAL which we carefully map to EROFS. In sum,
1431 * this matches Windows behaviour.
1435 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1437 saved_errno = errno;
1440 TALLOC_FREE(smb_fname->base_name);
1441 smb_fname->base_name = tmp;
1443 if (saved_errno != 0) {
1444 errno = saved_errno;
1449 static int shadow_copy2_unlinkat(vfs_handle_struct *handle,
1450 struct files_struct *dirfsp,
1451 const struct smb_filename *smb_fname,
1454 time_t timestamp = 0;
1456 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1457 smb_fname->base_name,
1458 ×tamp, NULL)) {
1461 if (timestamp != 0) {
1465 return SMB_VFS_NEXT_UNLINKAT(handle,
1471 static int shadow_copy2_chmod(vfs_handle_struct *handle,
1472 const struct smb_filename *smb_fname,
1475 time_t timestamp = 0;
1477 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1479 smb_fname->base_name,
1484 if (timestamp != 0) {
1488 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1491 static void store_cwd_data(vfs_handle_struct *handle,
1492 const char *connectpath)
1494 struct shadow_copy2_private *priv = NULL;
1495 struct smb_filename *cwd_fname = NULL;
1497 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1500 TALLOC_FREE(priv->shadow_cwd);
1501 cwd_fname = SMB_VFS_NEXT_GETWD(handle, talloc_tos());
1502 if (cwd_fname == NULL) {
1503 smb_panic("getwd failed\n");
1505 DBG_DEBUG("shadow cwd = %s\n", cwd_fname->base_name);
1506 priv->shadow_cwd = talloc_strdup(priv, cwd_fname->base_name);
1507 TALLOC_FREE(cwd_fname);
1508 if (priv->shadow_cwd == NULL) {
1509 smb_panic("talloc failed\n");
1511 TALLOC_FREE(priv->shadow_connectpath);
1513 DBG_DEBUG("shadow connectpath = %s\n", connectpath);
1514 priv->shadow_connectpath = talloc_strdup(priv, connectpath);
1515 if (priv->shadow_connectpath == NULL) {
1516 smb_panic("talloc failed\n");
1521 static int shadow_copy2_chdir(vfs_handle_struct *handle,
1522 const struct smb_filename *smb_fname)
1524 time_t timestamp = 0;
1525 char *stripped = NULL;
1526 char *snappath = NULL;
1528 int saved_errno = 0;
1530 size_t rootpath_len = 0;
1531 struct smb_filename *conv_smb_fname = NULL;
1533 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1535 smb_fname->base_name,
1542 if (stripped != NULL) {
1543 conv = shadow_copy2_do_convert(talloc_tos(),
1548 TALLOC_FREE(stripped);
1552 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1559 conv_smb_fname = cp_smb_filename(talloc_tos(), smb_fname);
1562 if (conv_smb_fname == NULL) {
1568 ret = SMB_VFS_NEXT_CHDIR(handle, conv_smb_fname);
1570 saved_errno = errno;
1574 if (conv != NULL && rootpath_len != 0) {
1575 conv[rootpath_len] = '\0';
1576 } else if (snappath != 0) {
1580 store_cwd_data(handle, conv);
1583 TALLOC_FREE(stripped);
1585 TALLOC_FREE(conv_smb_fname);
1587 if (saved_errno != 0) {
1588 errno = saved_errno;
1593 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
1594 const struct smb_filename *smb_fname,
1595 struct smb_file_time *ft)
1597 time_t timestamp = 0;
1599 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1600 smb_fname->base_name,
1601 ×tamp, NULL)) {
1604 if (timestamp != 0) {
1608 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1611 static int shadow_copy2_readlinkat(vfs_handle_struct *handle,
1612 files_struct *dirfsp,
1613 const struct smb_filename *smb_fname,
1617 time_t timestamp = 0;
1618 char *stripped = NULL;
1619 int saved_errno = 0;
1621 struct smb_filename *conv = NULL;
1623 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1624 smb_fname->base_name,
1625 ×tamp, &stripped)) {
1628 if (timestamp == 0) {
1629 return SMB_VFS_NEXT_READLINKAT(handle,
1635 conv = cp_smb_filename(talloc_tos(), smb_fname);
1637 TALLOC_FREE(stripped);
1641 conv->base_name = shadow_copy2_convert(
1642 conv, handle, stripped, timestamp);
1643 TALLOC_FREE(stripped);
1644 if (conv->base_name == NULL) {
1647 ret = SMB_VFS_NEXT_READLINKAT(handle,
1653 saved_errno = errno;
1656 if (saved_errno != 0) {
1657 errno = saved_errno;
1662 static int shadow_copy2_mknodat(vfs_handle_struct *handle,
1663 files_struct *dirfsp,
1664 const struct smb_filename *smb_fname,
1668 time_t timestamp = 0;
1670 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1671 smb_fname->base_name,
1672 ×tamp, NULL)) {
1675 if (timestamp != 0) {
1679 return SMB_VFS_NEXT_MKNODAT(handle,
1686 static struct smb_filename *shadow_copy2_realpath(vfs_handle_struct *handle,
1688 const struct smb_filename *smb_fname)
1690 time_t timestamp = 0;
1691 char *stripped = NULL;
1692 struct smb_filename *result_fname = NULL;
1693 struct smb_filename *conv_fname = NULL;
1694 int saved_errno = 0;
1696 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1697 smb_fname->base_name,
1698 ×tamp, &stripped)) {
1701 if (timestamp == 0) {
1702 return SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
1705 conv_fname = cp_smb_filename(talloc_tos(), smb_fname);
1706 if (conv_fname == NULL) {
1709 conv_fname->base_name = shadow_copy2_convert(
1710 conv_fname, handle, stripped, timestamp);
1711 if (conv_fname->base_name == NULL) {
1715 result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, conv_fname);
1718 if (result_fname == NULL) {
1719 saved_errno = errno;
1721 TALLOC_FREE(conv_fname);
1722 TALLOC_FREE(stripped);
1723 if (saved_errno != 0) {
1724 errno = saved_errno;
1726 return result_fname;
1730 * Check whether a given directory contains a
1731 * snapshot directory as direct subdirectory.
1732 * If yes, return the path of the snapshot-subdir,
1733 * otherwise return NULL.
1735 static char *have_snapdir(struct vfs_handle_struct *handle,
1738 struct smb_filename smb_fname;
1740 struct shadow_copy2_private *priv;
1742 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1745 ZERO_STRUCT(smb_fname);
1746 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1747 path, priv->config->snapdir);
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 return smb_fname.base_name;
1756 TALLOC_FREE(smb_fname.base_name);
1760 static bool check_access_snapdir(struct vfs_handle_struct *handle,
1763 struct smb_filename smb_fname;
1767 ZERO_STRUCT(smb_fname);
1768 smb_fname.base_name = talloc_asprintf(talloc_tos(),
1771 if (smb_fname.base_name == NULL) {
1775 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1776 if (ret != 0 || !S_ISDIR(smb_fname.st.st_ex_mode)) {
1777 TALLOC_FREE(smb_fname.base_name);
1781 status = smbd_check_access_rights(handle->conn,
1782 handle->conn->cwd_fsp,
1786 if (!NT_STATUS_IS_OK(status)) {
1787 DEBUG(0,("user does not have list permission "
1789 smb_fname.base_name));
1790 TALLOC_FREE(smb_fname.base_name);
1793 TALLOC_FREE(smb_fname.base_name);
1798 * Find the snapshot directory (if any) for the given
1799 * filename (which is relative to the share).
1801 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1802 struct vfs_handle_struct *handle,
1803 struct smb_filename *smb_fname)
1806 const char *snapdir;
1807 struct shadow_copy2_config *config;
1808 struct shadow_copy2_private *priv;
1810 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1813 config = priv->config;
1816 * If the non-snapdisrseverywhere mode, we should not search!
1818 if (!config->snapdirseverywhere) {
1819 return config->snapshot_basepath;
1822 path = talloc_asprintf(mem_ctx, "%s/%s",
1823 handle->conn->connectpath,
1824 smb_fname->base_name);
1829 snapdir = have_snapdir(handle, path);
1830 if (snapdir != NULL) {
1835 while ((p = strrchr(path, '/')) && (p > path)) {
1839 snapdir = have_snapdir(handle, path);
1840 if (snapdir != NULL) {
1849 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1851 char *gmt, size_t gmt_len)
1853 struct tm timestamp;
1855 unsigned long int timestamp_long;
1857 struct shadow_copy2_config *config;
1858 struct shadow_copy2_private *priv;
1859 char *tmpstr = NULL;
1861 bool converted = false;
1864 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1867 config = priv->config;
1869 fmt = config->gmt_format;
1872 * If regex is provided, then we will have to parse the
1873 * filename which will contain both the prefix and the time format.
1874 * e.g. <prefix><delimiter><time_format>
1876 if (priv->snaps->regex != NULL) {
1877 tmpstr = talloc_strdup(talloc_tos(), name);
1878 /* point "name" to the time format */
1879 name = strstr(name, priv->config->delimiter);
1883 /* Extract the prefix */
1884 tmp = strstr(tmpstr, priv->config->delimiter);
1891 ret = regexec(priv->snaps->regex, tmpstr, 0, NULL, 0);
1893 DBG_DEBUG("shadow_copy2_snapshot_to_gmt: "
1894 "no regex match for %s\n", tmpstr);
1899 ZERO_STRUCT(timestamp);
1900 if (config->use_sscanf) {
1901 if (sscanf(name, fmt, ×tamp_long) != 1) {
1902 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1903 "no sscanf match %s: %s\n",
1907 timestamp_t = timestamp_long;
1908 gmtime_r(×tamp_t, ×tamp);
1910 if (strptime(name, fmt, ×tamp) == NULL) {
1911 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1912 "no match %s: %s\n",
1916 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1919 if (config->use_localtime) {
1920 timestamp.tm_isdst = -1;
1921 timestamp_t = mktime(×tamp);
1922 gmtime_r(×tamp_t, ×tamp);
1926 strftime(gmt, gmt_len, GMT_FORMAT, ×tamp);
1930 TALLOC_FREE(tmpstr);
1934 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1936 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1939 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1941 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1945 sort the shadow copy data in ascending or descending order
1947 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1948 struct shadow_copy_data *shadow_copy2_data)
1950 int (*cmpfunc)(const void *, const void *);
1952 struct shadow_copy2_private *priv;
1954 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1957 sort = priv->config->sort_order;
1962 if (strcmp(sort, "asc") == 0) {
1963 cmpfunc = shadow_copy2_label_cmp_asc;
1964 } else if (strcmp(sort, "desc") == 0) {
1965 cmpfunc = shadow_copy2_label_cmp_desc;
1970 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1971 shadow_copy2_data->labels)
1973 TYPESAFE_QSORT(shadow_copy2_data->labels,
1974 shadow_copy2_data->num_volumes,
1979 static int shadow_copy2_get_shadow_copy_data(
1980 vfs_handle_struct *handle, files_struct *fsp,
1981 struct shadow_copy_data *shadow_copy2_data,
1985 const char *snapdir;
1986 struct smb_filename *snapdir_smb_fname = NULL;
1987 struct files_struct *dirfsp = NULL;
1989 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1990 struct shadow_copy2_private *priv = NULL;
1991 struct shadow_copy2_snapentry *tmpentry = NULL;
1992 bool get_snaplist = false;
1993 bool access_granted = false;
1994 int open_flags = O_RDONLY;
1998 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1999 if (snapdir == NULL) {
2000 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
2001 handle->conn->connectpath));
2006 access_granted = check_access_snapdir(handle, snapdir);
2007 if (!access_granted) {
2008 DEBUG(0,("access denied on listing snapdir %s\n", snapdir));
2013 snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
2018 fsp->fsp_name->flags);
2019 if (snapdir_smb_fname == NULL) {
2024 status = create_internal_dirfsp_at(handle->conn,
2025 handle->conn->cwd_fsp,
2028 if (!NT_STATUS_IS_OK(status)) {
2029 DBG_WARNING("create_internal_dir_fsp() failed for '%s'"
2030 " - %s\n", snapdir, nt_errstr(status));
2036 open_flags |= O_DIRECTORY;
2039 dirfsp->fh->fd = SMB_VFS_NEXT_OPEN(handle,
2044 if (dirfsp->fh->fd == -1) {
2045 DBG_WARNING("SMB_VFS_NEXT_OPEN failed for '%s'"
2046 " - %s\n", snapdir, strerror(errno));
2051 p = SMB_VFS_NEXT_FDOPENDIR(handle, dirfsp, NULL, 0);
2053 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
2054 " - %s\n", snapdir, strerror(errno)));
2059 if (shadow_copy2_data != NULL) {
2060 shadow_copy2_data->num_volumes = 0;
2061 shadow_copy2_data->labels = NULL;
2064 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2068 * Normally this function is called twice once with labels = false and
2069 * then with labels = true. When labels is false it will return the
2070 * number of volumes so that the caller can allocate memory for that
2071 * many labels. Therefore to eliminate snaplist both the times it is
2072 * good to check if labels is set or not.
2074 * shadow_copy2_data is NULL when we only want to update the list and
2075 * don't want any labels.
2077 if ((priv->snaps->regex != NULL) && (labels || shadow_copy2_data == NULL)) {
2078 get_snaplist = true;
2079 /* Reset the global snaplist */
2080 shadow_copy2_delete_snaplist(priv);
2082 /* Set the current time as snaplist update time */
2083 time(&(priv->snaps->fetch_time));
2086 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
2087 char snapshot[GMT_NAME_LEN+1];
2088 SHADOW_COPY_LABEL *tlabels;
2091 * ignore names not of the right form in the snapshot
2094 if (!shadow_copy2_snapshot_to_gmt(
2096 snapshot, sizeof(snapshot))) {
2098 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
2099 "ignoring %s\n", d->d_name));
2102 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
2103 d->d_name, snapshot));
2107 * Create a snap entry for each successful
2110 tmpentry = shadow_copy2_create_snapentry(priv);
2111 if (tmpentry == NULL) {
2112 DBG_ERR("talloc_zero() failed\n");
2115 tmpentry->snapname = talloc_strdup(tmpentry, d->d_name);
2116 tmpentry->time_fmt = talloc_strdup(tmpentry, snapshot);
2119 if (shadow_copy2_data == NULL) {
2124 /* the caller doesn't want the labels */
2125 shadow_copy2_data->num_volumes++;
2129 tlabels = talloc_realloc(shadow_copy2_data,
2130 shadow_copy2_data->labels,
2132 shadow_copy2_data->num_volumes+1);
2133 if (tlabels == NULL) {
2134 DEBUG(0,("shadow_copy2: out of memory\n"));
2138 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
2141 shadow_copy2_data->num_volumes++;
2142 shadow_copy2_data->labels = tlabels;
2145 shadow_copy2_sort_data(handle, shadow_copy2_data);
2150 SMB_VFS_NEXT_CLOSEDIR(handle, p);
2153 if (dirfsp != NULL) {
2155 file_free(NULL, dirfsp);
2157 TALLOC_FREE(tmp_ctx);
2161 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
2162 const struct smb_filename *smb_fname,
2163 uint32_t security_info,
2164 TALLOC_CTX *mem_ctx,
2165 struct security_descriptor **ppdesc)
2167 time_t timestamp = 0;
2168 char *stripped = NULL;
2171 struct smb_filename *conv_smb_fname = NULL;
2173 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2175 smb_fname->base_name,
2178 return map_nt_error_from_unix(errno);
2180 if (timestamp == 0) {
2181 return SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
2184 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2185 TALLOC_FREE(stripped);
2187 return map_nt_error_from_unix(errno);
2189 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2195 if (conv_smb_fname == NULL) {
2197 return NT_STATUS_NO_MEMORY;
2199 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv_smb_fname, security_info,
2202 TALLOC_FREE(conv_smb_fname);
2206 static int shadow_copy2_mkdirat(vfs_handle_struct *handle,
2207 struct files_struct *dirfsp,
2208 const struct smb_filename *smb_fname,
2211 time_t timestamp = 0;
2213 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2215 smb_fname->base_name,
2220 if (timestamp != 0) {
2224 return SMB_VFS_NEXT_MKDIRAT(handle,
2230 static int shadow_copy2_chflags(vfs_handle_struct *handle,
2231 const struct smb_filename *smb_fname,
2234 time_t timestamp = 0;
2236 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2238 smb_fname->base_name,
2243 if (timestamp != 0) {
2247 return SMB_VFS_NEXT_CHFLAGS(handle, smb_fname, flags);
2250 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
2251 const struct smb_filename *smb_fname,
2256 time_t timestamp = 0;
2257 char *stripped = NULL;
2259 int saved_errno = 0;
2261 struct smb_filename *conv_smb_fname = NULL;
2263 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2265 smb_fname->base_name,
2270 if (timestamp == 0) {
2271 return SMB_VFS_NEXT_GETXATTR(handle, smb_fname, aname, value,
2274 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2275 TALLOC_FREE(stripped);
2280 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2286 if (conv_smb_fname == NULL) {
2291 ret = SMB_VFS_NEXT_GETXATTR(handle, conv_smb_fname, aname, value, size);
2293 saved_errno = errno;
2295 TALLOC_FREE(conv_smb_fname);
2297 if (saved_errno != 0) {
2298 errno = saved_errno;
2303 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
2304 const struct smb_filename *smb_fname,
2305 char *list, size_t size)
2307 time_t timestamp = 0;
2308 char *stripped = NULL;
2310 int saved_errno = 0;
2312 struct smb_filename *conv_smb_fname = NULL;
2314 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2316 smb_fname->base_name,
2321 if (timestamp == 0) {
2322 return SMB_VFS_NEXT_LISTXATTR(handle, smb_fname, list, size);
2324 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2325 TALLOC_FREE(stripped);
2329 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2335 if (conv_smb_fname == NULL) {
2339 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv_smb_fname, list, size);
2341 saved_errno = errno;
2343 TALLOC_FREE(conv_smb_fname);
2345 if (saved_errno != 0) {
2346 errno = saved_errno;
2351 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
2352 const struct smb_filename *smb_fname,
2355 time_t timestamp = 0;
2357 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2359 smb_fname->base_name,
2364 if (timestamp != 0) {
2368 return SMB_VFS_NEXT_REMOVEXATTR(handle, smb_fname, aname);
2371 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
2372 const struct smb_filename *smb_fname,
2373 const char *aname, const void *value,
2374 size_t size, int flags)
2376 time_t timestamp = 0;
2378 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2380 smb_fname->base_name,
2385 if (timestamp != 0) {
2389 return SMB_VFS_NEXT_SETXATTR(handle, smb_fname,
2390 aname, value, size, flags);
2393 static NTSTATUS shadow_copy2_create_dfs_pathat(struct vfs_handle_struct *handle,
2394 struct files_struct *dirfsp,
2395 const struct smb_filename *smb_fname,
2396 const struct referral *reflist,
2397 size_t referral_count)
2399 time_t timestamp = 0;
2401 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2403 smb_fname->base_name,
2406 return NT_STATUS_NO_MEMORY;
2408 if (timestamp != 0) {
2409 return NT_STATUS_MEDIA_WRITE_PROTECTED;
2411 return SMB_VFS_NEXT_CREATE_DFS_PATHAT(handle,
2418 static NTSTATUS shadow_copy2_read_dfs_pathat(struct vfs_handle_struct *handle,
2419 TALLOC_CTX *mem_ctx,
2420 struct files_struct *dirfsp,
2421 const struct smb_filename *smb_fname,
2422 struct referral **ppreflist,
2423 size_t *preferral_count)
2425 time_t timestamp = 0;
2426 char *stripped = NULL;
2427 struct smb_filename *conv = NULL;
2430 if (!shadow_copy2_strip_snapshot(mem_ctx,
2432 smb_fname->base_name,
2435 return NT_STATUS_NO_MEMORY;
2437 if (timestamp == 0) {
2438 return SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
2446 conv = cp_smb_filename(mem_ctx, smb_fname);
2448 TALLOC_FREE(stripped);
2449 return NT_STATUS_NO_MEMORY;
2451 conv->base_name = shadow_copy2_convert(conv,
2455 TALLOC_FREE(stripped);
2456 if (conv->base_name == NULL) {
2458 return NT_STATUS_NO_MEMORY;
2461 status = SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
2472 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
2475 TALLOC_CTX *mem_ctx,
2478 time_t timestamp = 0;
2479 char *stripped = NULL;
2481 int saved_errno = 0;
2484 DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
2485 "name=[%s]\n", path, name));
2487 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
2488 ×tamp, &stripped)) {
2489 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
2492 if (timestamp == 0) {
2493 DEBUG(10, ("timestamp == 0\n"));
2494 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
2495 mem_ctx, found_name);
2497 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2498 TALLOC_FREE(stripped);
2500 DEBUG(10, ("shadow_copy2_convert failed\n"));
2503 DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
2504 "name=[%s]\n", conv, name));
2505 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
2506 mem_ctx, found_name);
2507 DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
2509 saved_errno = errno;
2512 if (saved_errno != 0) {
2513 errno = saved_errno;
2518 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
2519 const struct smb_filename *smb_fname_in)
2521 time_t timestamp = 0;
2522 char *stripped = NULL;
2524 const char *fname = smb_fname_in->base_name;
2525 struct smb_filename smb_fname = {0};
2526 struct smb_filename *result_fname = NULL;
2527 char *result = NULL;
2528 char *parent_dir = NULL;
2529 int saved_errno = 0;
2530 size_t rootpath_len = 0;
2531 struct shadow_copy2_private *priv = NULL;
2533 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2536 DBG_DEBUG("Calc connect path for [%s]\n", fname);
2538 if (priv->shadow_connectpath != NULL) {
2539 DBG_DEBUG("cached connect path is [%s]\n",
2540 priv->shadow_connectpath);
2541 return priv->shadow_connectpath;
2544 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2545 ×tamp, &stripped)) {
2548 if (timestamp == 0) {
2549 return SMB_VFS_NEXT_CONNECTPATH(handle, smb_fname_in);
2552 tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
2555 if (errno != ENOENT) {
2560 * If the converted path does not exist, and converting
2561 * the parent yields something that does exist, then
2562 * this path refers to something that has not been
2563 * created yet, relative to the parent path.
2564 * The snapshot finding is relative to the parent.
2565 * (usually snapshots are read/only but this is not
2566 * necessarily true).
2567 * This code also covers getting a wildcard in the
2568 * last component, because this function is called
2569 * prior to sanitizing the path, and in SMB1 we may
2570 * get wildcards in path names.
2572 if (!parent_dirname(talloc_tos(), stripped, &parent_dir,
2578 tmp = shadow_copy2_do_convert(talloc_tos(), handle, parent_dir,
2579 timestamp, &rootpath_len);
2585 DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
2586 (int)rootpath_len, tmp);
2588 tmp[rootpath_len] = '\0';
2589 smb_fname = (struct smb_filename) { .base_name = tmp };
2591 result_fname = SMB_VFS_NEXT_REALPATH(handle, priv, &smb_fname);
2592 if (result_fname == NULL) {
2597 * SMB_VFS_NEXT_REALPATH returns a talloc'ed string.
2598 * Don't leak memory.
2600 TALLOC_FREE(priv->shadow_realpath);
2601 priv->shadow_realpath = result_fname;
2602 result = priv->shadow_realpath->base_name;
2604 DBG_DEBUG("connect path is [%s]\n", result);
2607 if (result == NULL) {
2608 saved_errno = errno;
2611 TALLOC_FREE(stripped);
2612 TALLOC_FREE(parent_dir);
2613 if (saved_errno != 0) {
2614 errno = saved_errno;
2619 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
2620 const struct smb_filename *smb_fname,
2625 time_t timestamp = 0;
2626 char *stripped = NULL;
2627 int saved_errno = 0;
2629 struct smb_filename *conv_smb_fname = NULL;
2630 uint64_t ret = (uint64_t)-1;
2632 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2634 smb_fname->base_name,
2637 return (uint64_t)-1;
2639 if (timestamp == 0) {
2640 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2641 bsize, dfree, dsize);
2643 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2644 TALLOC_FREE(stripped);
2646 return (uint64_t)-1;
2648 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2654 if (conv_smb_fname == NULL) {
2656 return (uint64_t)-1;
2658 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv_smb_fname,
2659 bsize, dfree, dsize);
2660 if (ret == (uint64_t)-1) {
2661 saved_errno = errno;
2664 TALLOC_FREE(conv_smb_fname);
2665 if (saved_errno != 0) {
2666 errno = saved_errno;
2671 static int shadow_copy2_get_quota(vfs_handle_struct *handle,
2672 const struct smb_filename *smb_fname,
2673 enum SMB_QUOTA_TYPE qtype,
2677 time_t timestamp = 0;
2678 char *stripped = NULL;
2680 int saved_errno = 0;
2682 struct smb_filename *conv_smb_fname = NULL;
2684 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2686 smb_fname->base_name,
2691 if (timestamp == 0) {
2692 return SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname, qtype, id, dq);
2695 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2696 TALLOC_FREE(stripped);
2700 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2706 if (conv_smb_fname == NULL) {
2710 ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv_smb_fname, qtype, id, dq);
2713 saved_errno = errno;
2716 TALLOC_FREE(conv_smb_fname);
2717 if (saved_errno != 0) {
2718 errno = saved_errno;
2724 static ssize_t shadow_copy2_pwrite(vfs_handle_struct *handle,
2732 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2733 if (nwritten == -1) {
2734 if (errno == EBADF && fsp->fsp_flags.can_write) {
2742 struct shadow_copy2_pwrite_state {
2743 vfs_handle_struct *handle;
2746 struct vfs_aio_state vfs_aio_state;
2749 static void shadow_copy2_pwrite_done(struct tevent_req *subreq);
2751 static struct tevent_req *shadow_copy2_pwrite_send(
2752 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
2753 struct tevent_context *ev, struct files_struct *fsp,
2754 const void *data, size_t n, off_t offset)
2756 struct tevent_req *req = NULL, *subreq = NULL;
2757 struct shadow_copy2_pwrite_state *state = NULL;
2759 req = tevent_req_create(mem_ctx, &state,
2760 struct shadow_copy2_pwrite_state);
2764 state->handle = handle;
2767 subreq = SMB_VFS_NEXT_PWRITE_SEND(state,
2774 if (tevent_req_nomem(subreq, req)) {
2775 return tevent_req_post(req, ev);
2777 tevent_req_set_callback(subreq, shadow_copy2_pwrite_done, req);
2782 static void shadow_copy2_pwrite_done(struct tevent_req *subreq)
2784 struct tevent_req *req = tevent_req_callback_data(
2785 subreq, struct tevent_req);
2786 struct shadow_copy2_pwrite_state *state = tevent_req_data(
2787 req, struct shadow_copy2_pwrite_state);
2789 state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
2790 TALLOC_FREE(subreq);
2791 if (state->ret == -1) {
2792 tevent_req_error(req, state->vfs_aio_state.error);
2796 tevent_req_done(req);
2799 static ssize_t shadow_copy2_pwrite_recv(struct tevent_req *req,
2800 struct vfs_aio_state *vfs_aio_state)
2802 struct shadow_copy2_pwrite_state *state = tevent_req_data(
2803 req, struct shadow_copy2_pwrite_state);
2805 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2806 if ((vfs_aio_state->error == EBADF) &&
2807 state->fsp->fsp_flags.can_write)
2809 vfs_aio_state->error = EROFS;
2815 *vfs_aio_state = state->vfs_aio_state;
2819 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
2820 const char *service, const char *user)
2822 struct shadow_copy2_config *config;
2823 struct shadow_copy2_private *priv;
2825 const char *snapdir;
2826 const char *snapprefix = NULL;
2827 const char *delimiter;
2828 const char *gmt_format;
2829 const char *sort_order;
2830 const char *basedir = NULL;
2831 const char *snapsharepath = NULL;
2832 const char *mount_point;
2834 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
2835 (unsigned)handle->conn->cnum,
2836 handle->conn->connectpath));
2838 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
2843 priv = talloc_zero(handle->conn, struct shadow_copy2_private);
2845 DBG_ERR("talloc_zero() failed\n");
2850 priv->snaps = talloc_zero(priv, struct shadow_copy2_snaplist_info);
2851 if (priv->snaps == NULL) {
2852 DBG_ERR("talloc_zero() failed\n");
2857 config = talloc_zero(priv, struct shadow_copy2_config);
2858 if (config == NULL) {
2859 DEBUG(0, ("talloc_zero() failed\n"));
2864 priv->config = config;
2866 gmt_format = lp_parm_const_string(SNUM(handle->conn),
2869 config->gmt_format = talloc_strdup(config, gmt_format);
2870 if (config->gmt_format == NULL) {
2871 DEBUG(0, ("talloc_strdup() failed\n"));
2876 /* config->gmt_format must not contain a path separator. */
2877 if (strchr(config->gmt_format, '/') != NULL) {
2878 DEBUG(0, ("shadow:format %s must not contain a /"
2879 "character. Unable to initialize module.\n",
2880 config->gmt_format));
2885 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
2886 "shadow", "sscanf", false);
2888 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
2889 "shadow", "localtime",
2892 snapdir = lp_parm_const_string(SNUM(handle->conn),
2893 "shadow", "snapdir",
2895 config->snapdir = talloc_strdup(config, snapdir);
2896 if (config->snapdir == NULL) {
2897 DEBUG(0, ("talloc_strdup() failed\n"));
2902 snapprefix = lp_parm_const_string(SNUM(handle->conn),
2903 "shadow", "snapprefix",
2905 if (snapprefix != NULL) {
2906 priv->snaps->regex = talloc_zero(priv->snaps, regex_t);
2907 if (priv->snaps->regex == NULL) {
2908 DBG_ERR("talloc_zero() failed\n");
2913 /* pre-compute regex rule for matching pattern later */
2914 ret = regcomp(priv->snaps->regex, snapprefix, 0);
2916 DBG_ERR("Failed to create regex object\n");
2921 delimiter = lp_parm_const_string(SNUM(handle->conn),
2922 "shadow", "delimiter",
2924 if (delimiter != NULL) {
2925 priv->config->delimiter = talloc_strdup(priv->config, delimiter);
2926 if (priv->config->delimiter == NULL) {
2927 DBG_ERR("talloc_strdup() failed\n");
2933 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
2935 "snapdirseverywhere",
2938 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
2939 "shadow", "crossmountpoints",
2942 if (config->crossmountpoints && !config->snapdirseverywhere) {
2943 DBG_WARNING("Warning: 'crossmountpoints' depends on "
2944 "'snapdirseverywhere'. Disabling crossmountpoints.\n");
2947 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
2948 "shadow", "fixinodes",
2951 sort_order = lp_parm_const_string(SNUM(handle->conn),
2952 "shadow", "sort", "desc");
2953 config->sort_order = talloc_strdup(config, sort_order);
2954 if (config->sort_order == NULL) {
2955 DEBUG(0, ("talloc_strdup() failed\n"));
2960 mount_point = lp_parm_const_string(SNUM(handle->conn),
2961 "shadow", "mountpoint", NULL);
2962 if (mount_point != NULL) {
2963 if (mount_point[0] != '/') {
2964 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
2965 "relative ('%s'), but it has to be an "
2966 "absolute path. Ignoring provided value.\n",
2971 p = strstr(handle->conn->connectpath, mount_point);
2972 if (p != handle->conn->connectpath) {
2973 DBG_WARNING("Warning: the share root (%s) is "
2974 "not a subdirectory of the "
2975 "specified mountpoint (%s). "
2976 "Ignoring provided value.\n",
2977 handle->conn->connectpath,
2984 if (mount_point != NULL) {
2985 config->mount_point = talloc_strdup(config, mount_point);
2986 if (config->mount_point == NULL) {
2987 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
2991 config->mount_point = shadow_copy2_find_mount_point(config,
2993 if (config->mount_point == NULL) {
2994 DBG_WARNING("shadow_copy2_find_mount_point "
2995 "of the share root '%s' failed: %s\n",
2996 handle->conn->connectpath, strerror(errno));
3001 basedir = lp_parm_const_string(SNUM(handle->conn),
3002 "shadow", "basedir", NULL);
3004 if (basedir != NULL) {
3005 if (basedir[0] != '/') {
3006 DEBUG(1, (__location__ " Warning: 'basedir' is "
3007 "relative ('%s'), but it has to be an "
3008 "absolute path. Disabling basedir.\n",
3013 p = strstr(basedir, config->mount_point);
3015 DEBUG(1, ("Warning: basedir (%s) is not a "
3016 "subdirectory of the share root's "
3017 "mount point (%s). "
3018 "Disabling basedir\n",
3019 basedir, config->mount_point));
3025 if (config->snapdirseverywhere && basedir != NULL) {
3026 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
3027 "with 'snapdirseverywhere'. Disabling basedir.\n"));
3031 snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
3032 "snapsharepath", NULL);
3033 if (snapsharepath != NULL) {
3034 if (snapsharepath[0] == '/') {
3035 DBG_WARNING("Warning: 'snapsharepath' is "
3036 "absolute ('%s'), but it has to be a "
3037 "relative path. Disabling snapsharepath.\n",
3039 snapsharepath = NULL;
3041 if (config->snapdirseverywhere && snapsharepath != NULL) {
3042 DBG_WARNING("Warning: 'snapsharepath' is incompatible "
3043 "with 'snapdirseverywhere'. Disabling "
3044 "snapsharepath.\n");
3045 snapsharepath = NULL;
3049 if (basedir != NULL && snapsharepath != NULL) {
3050 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
3051 "'basedir'. Disabling snapsharepath\n");
3052 snapsharepath = NULL;
3055 if (snapsharepath != NULL) {
3056 config->rel_connectpath = talloc_strdup(config, snapsharepath);
3057 if (config->rel_connectpath == NULL) {
3058 DBG_ERR("talloc_strdup() failed\n");
3064 if (basedir == NULL) {
3065 basedir = config->mount_point;
3068 if (config->rel_connectpath == NULL &&
3069 strlen(basedir) < strlen(handle->conn->connectpath)) {
3070 config->rel_connectpath = talloc_strdup(config,
3071 handle->conn->connectpath + strlen(basedir));
3072 if (config->rel_connectpath == NULL) {
3073 DEBUG(0, ("talloc_strdup() failed\n"));
3079 if (config->snapdir[0] == '/') {
3080 config->snapdir_absolute = true;
3082 if (config->snapdirseverywhere == true) {
3083 DEBUG(1, (__location__ " Warning: An absolute snapdir "
3084 "is incompatible with 'snapdirseverywhere', "
3085 "setting 'snapdirseverywhere' to false.\n"));
3086 config->snapdirseverywhere = false;
3089 if (config->crossmountpoints == true) {
3090 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
3091 "is not supported with an absolute snapdir. "
3092 "Disabling it.\n"));
3093 config->crossmountpoints = false;
3096 config->snapshot_basepath = config->snapdir;
3098 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
3099 config->mount_point, config->snapdir);
3100 if (config->snapshot_basepath == NULL) {
3101 DEBUG(0, ("talloc_asprintf() failed\n"));
3107 trim_string(config->mount_point, NULL, "/");
3108 trim_string(config->rel_connectpath, "/", "/");
3109 trim_string(config->snapdir, NULL, "/");
3110 trim_string(config->snapshot_basepath, NULL, "/");
3112 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
3113 " share root: '%s'\n"
3114 " mountpoint: '%s'\n"
3115 " rel share root: '%s'\n"
3117 " snapprefix: '%s'\n"
3118 " delimiter: '%s'\n"
3119 " snapshot base path: '%s'\n"
3122 " snapdirs everywhere: %s\n"
3123 " cross mountpoints: %s\n"
3127 handle->conn->connectpath,
3128 config->mount_point,
3129 config->rel_connectpath,
3133 config->snapshot_basepath,
3135 config->use_sscanf ? "yes" : "no",
3136 config->snapdirseverywhere ? "yes" : "no",
3137 config->crossmountpoints ? "yes" : "no",
3138 config->fixinodes ? "yes" : "no",
3143 SMB_VFS_HANDLE_SET_DATA(handle, priv,
3144 NULL, struct shadow_copy2_private,
3150 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
3151 .connect_fn = shadow_copy2_connect,
3152 .disk_free_fn = shadow_copy2_disk_free,
3153 .get_quota_fn = shadow_copy2_get_quota,
3154 .create_dfs_pathat_fn = shadow_copy2_create_dfs_pathat,
3155 .read_dfs_pathat_fn = shadow_copy2_read_dfs_pathat,
3156 .renameat_fn = shadow_copy2_renameat,
3157 .linkat_fn = shadow_copy2_linkat,
3158 .symlinkat_fn = shadow_copy2_symlinkat,
3159 .stat_fn = shadow_copy2_stat,
3160 .lstat_fn = shadow_copy2_lstat,
3161 .fstat_fn = shadow_copy2_fstat,
3162 .open_fn = shadow_copy2_open,
3163 .unlinkat_fn = shadow_copy2_unlinkat,
3164 .chmod_fn = shadow_copy2_chmod,
3165 .chdir_fn = shadow_copy2_chdir,
3166 .ntimes_fn = shadow_copy2_ntimes,
3167 .readlinkat_fn = shadow_copy2_readlinkat,
3168 .mknodat_fn = shadow_copy2_mknodat,
3169 .realpath_fn = shadow_copy2_realpath,
3170 .get_nt_acl_fn = shadow_copy2_get_nt_acl,
3171 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
3172 .mkdirat_fn = shadow_copy2_mkdirat,
3173 .getxattr_fn = shadow_copy2_getxattr,
3174 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
3175 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
3176 .listxattr_fn = shadow_copy2_listxattr,
3177 .removexattr_fn = shadow_copy2_removexattr,
3178 .setxattr_fn = shadow_copy2_setxattr,
3179 .chflags_fn = shadow_copy2_chflags,
3180 .get_real_filename_fn = shadow_copy2_get_real_filename,
3181 .pwrite_fn = shadow_copy2_pwrite,
3182 .pwrite_send_fn = shadow_copy2_pwrite_send,
3183 .pwrite_recv_fn = shadow_copy2_pwrite_recv,
3184 .connectpath_fn = shadow_copy2_connectpath,
3188 NTSTATUS vfs_shadow_copy2_init(TALLOC_CTX *ctx)
3190 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
3191 "shadow_copy2", &vfs_shadow_copy2_fns);