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 */
73 * shadow_copy2 private structure. This structure will be
74 * used to keep module specific information
76 struct shadow_copy2_private {
77 struct shadow_copy2_config *config;
78 struct shadow_copy2_snaplist_info *snaps;
79 char *shadow_cwd; /* Absolute $cwd path. */
80 /* Absolute connectpath - can vary depending on $cwd. */
81 char *shadow_connectpath;
82 /* talloc'ed realpath return. */
83 struct smb_filename *shadow_realpath;
86 static int shadow_copy2_get_shadow_copy_data(
87 vfs_handle_struct *handle, files_struct *fsp,
88 struct shadow_copy_data *shadow_copy2_data,
92 * This function will create a new snapshot list entry and
93 * return to the caller. This entry will also be added to
94 * the global snapshot list.
96 * @param[in] priv shadow_copy2 specific data structure
97 * @return Newly created snapshot entry or NULL on failure
99 static struct shadow_copy2_snapentry *shadow_copy2_create_snapentry(
100 struct shadow_copy2_private *priv)
102 struct shadow_copy2_snapentry *tmpentry = NULL;
104 tmpentry = talloc_zero(priv->snaps, struct shadow_copy2_snapentry);
105 if (tmpentry == NULL) {
106 DBG_ERR("talloc_zero() failed\n");
111 DLIST_ADD(priv->snaps->snaplist, tmpentry);
117 * This function will delete the entire snaplist and reset
118 * priv->snaps->snaplist to NULL.
120 * @param[in] priv shadow_copye specific data structure
122 static void shadow_copy2_delete_snaplist(struct shadow_copy2_private *priv)
124 struct shadow_copy2_snapentry *tmp = NULL;
126 while ((tmp = priv->snaps->snaplist) != NULL) {
127 DLIST_REMOVE(priv->snaps->snaplist, tmp);
133 * Given a timestamp this function searches the global snapshot list
134 * and returns the complete snapshot directory name saved in the entry.
136 * @param[in] priv shadow_copy2 specific structure
137 * @param[in] timestamp timestamp corresponding to one of the snapshot
138 * @param[out] snap_str buffer to copy the actual snapshot name
139 * @param[in] len length of snap_str buffer
141 * @return Length of actual snapshot name, and -1 on failure
143 static ssize_t shadow_copy2_saved_snapname(struct shadow_copy2_private *priv,
144 struct tm *timestamp,
145 char *snap_str, size_t len)
147 ssize_t snaptime_len = -1;
148 struct shadow_copy2_snapentry *entry = NULL;
150 snaptime_len = strftime(snap_str, len, GMT_FORMAT, timestamp);
151 if (snaptime_len == 0) {
152 DBG_ERR("strftime failed\n");
158 for (entry = priv->snaps->snaplist; entry; entry = entry->next) {
159 if (strcmp(entry->time_fmt, snap_str) == 0) {
160 snaptime_len = snprintf(snap_str, len, "%s",
172 * This function will check if snaplist is updated or not. If snaplist
173 * is empty then it will create a new list. Each time snaplist is updated
174 * the time is recorded. If the snapshot time is greater than the snaplist
175 * update time then chances are we are working on an older list. Then discard
176 * the old list and fetch a new snaplist.
178 * @param[in] handle VFS handle struct
179 * @param[in] snap_time time of snapshot
181 * @return true if the list is updated else false
183 static bool shadow_copy2_update_snaplist(struct vfs_handle_struct *handle,
187 bool snaplist_updated = false;
188 struct files_struct fsp = {0};
189 struct smb_filename smb_fname = {0};
190 double seconds = 0.0;
191 struct shadow_copy2_private *priv = NULL;
193 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
196 seconds = difftime(snap_time, priv->snaps->fetch_time);
199 * Fetch the snapshot list if either the snaplist is empty or the
200 * required snapshot time is greater than the last fetched snaplist
203 if (seconds > 0 || (priv->snaps->snaplist == NULL)) {
204 smb_fname.base_name = discard_const_p(char, ".");
205 fsp.fsp_name = &smb_fname;
207 ret = shadow_copy2_get_shadow_copy_data(handle, &fsp,
210 snaplist_updated = true;
212 DBG_ERR("Failed to get shadow copy data\n");
217 return snaplist_updated;
220 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
222 unsigned *pnum_offsets)
224 unsigned num_offsets;
231 while ((p = strchr(p, '/')) != NULL) {
236 offsets = talloc_array(mem_ctx, size_t, num_offsets);
237 if (offsets == NULL) {
243 while ((p = strchr(p, '/')) != NULL) {
244 offsets[num_offsets] = p-str;
250 *pnum_offsets = num_offsets;
255 * Given a timestamp, build the posix level GMT-tag string
256 * based on the configurable format.
258 static ssize_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
260 char *snaptime_string,
264 ssize_t snaptime_len;
265 struct shadow_copy2_config *config;
266 struct shadow_copy2_private *priv;
268 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
271 config = priv->config;
273 if (config->use_sscanf) {
274 snaptime_len = snprintf(snaptime_string,
277 (unsigned long)snapshot);
278 if (snaptime_len <= 0) {
279 DEBUG(10, ("snprintf failed\n"));
283 if (config->use_localtime) {
284 if (localtime_r(&snapshot, &snap_tm) == 0) {
285 DEBUG(10, ("gmtime_r failed\n"));
289 if (gmtime_r(&snapshot, &snap_tm) == 0) {
290 DEBUG(10, ("gmtime_r failed\n"));
295 if (priv->snaps->regex != NULL) {
296 snaptime_len = shadow_copy2_saved_snapname(priv,
297 &snap_tm, snaptime_string, len);
298 if (snaptime_len >= 0)
302 * If we fail to find the snapshot name, chances are
303 * that we have not updated our snaplist. Make sure the
304 * snaplist is updated.
306 if (!shadow_copy2_update_snaplist(handle, snapshot)) {
307 DBG_DEBUG("shadow_copy2_update_snaplist "
312 return shadow_copy2_saved_snapname(priv,
313 &snap_tm, snaptime_string, len);
316 snaptime_len = strftime(snaptime_string,
320 if (snaptime_len == 0) {
321 DEBUG(10, ("strftime failed\n"));
330 * Given a timestamp, build the string to insert into a path
331 * as a path component for creating the local path to the
332 * snapshot at the given timestamp of the input path.
334 * In the case of a parallel snapdir (specified with an
335 * absolute path), this is the initial portion of the
336 * local path of any snapshot file. The complete path is
337 * obtained by appending the portion of the file's path
338 * below the share root's mountpoint.
340 static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
341 struct vfs_handle_struct *handle,
344 fstring snaptime_string;
345 ssize_t snaptime_len = 0;
347 struct shadow_copy2_config *config;
348 struct shadow_copy2_private *priv;
350 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
353 config = priv->config;
355 snaptime_len = shadow_copy2_posix_gmt_string(handle,
358 sizeof(snaptime_string));
359 if (snaptime_len <= 0) {
363 if (config->snapdir_absolute) {
364 result = talloc_asprintf(mem_ctx, "%s/%s",
365 config->snapdir, snaptime_string);
367 result = talloc_asprintf(mem_ctx, "/%s/%s",
368 config->snapdir, snaptime_string);
370 if (result == NULL) {
371 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
378 * Build the posix snapshot path for the connection
379 * at the given timestamp, i.e. the absolute posix path
380 * that contains the snapshot for this file system.
382 * This only applies to classical case, i.e. not
383 * to the "snapdirseverywhere" mode.
385 static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
386 struct vfs_handle_struct *handle,
389 fstring snaptime_string;
390 ssize_t snaptime_len = 0;
392 struct shadow_copy2_private *priv;
394 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
397 snaptime_len = shadow_copy2_posix_gmt_string(handle,
400 sizeof(snaptime_string));
401 if (snaptime_len <= 0) {
405 result = talloc_asprintf(mem_ctx, "%s/%s",
406 priv->config->snapshot_basepath, snaptime_string);
407 if (result == NULL) {
408 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
414 static char *make_path_absolute(TALLOC_CTX *mem_ctx,
415 struct shadow_copy2_private *priv,
418 char *newpath = NULL;
419 char *abs_path = NULL;
421 if (name[0] != '/') {
422 newpath = talloc_asprintf(mem_ctx,
426 if (newpath == NULL) {
431 abs_path = canonicalize_absolute_path(mem_ctx, name);
432 TALLOC_FREE(newpath);
436 /* Return a $cwd-relative path. */
437 static bool make_relative_path(const char *cwd, char *abs_path)
439 size_t cwd_len = strlen(cwd);
440 size_t abs_len = strlen(abs_path);
442 if (abs_len < cwd_len) {
445 if (memcmp(abs_path, cwd, cwd_len) != 0) {
448 /* The cwd_len != 1 case is for $cwd == '/' */
450 abs_path[cwd_len] != '/' &&
451 abs_path[cwd_len] != '\0')
455 if (abs_path[cwd_len] == '/') {
458 memmove(abs_path, &abs_path[cwd_len], abs_len + 1 - cwd_len);
462 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
464 char *gmt, size_t gmt_len);
467 * Check if an incoming filename is already a snapshot converted pathname.
469 * If so, it returns the pathname truncated at the snapshot point which
470 * will be used as the connectpath.
473 static int check_for_converted_path(TALLOC_CTX *mem_ctx,
474 struct vfs_handle_struct *handle,
475 struct shadow_copy2_private *priv,
477 bool *ppath_already_converted,
480 size_t snapdirlen = 0;
481 char *p = strstr_m(abs_path, priv->config->snapdir);
483 char *connect_path = NULL;
484 char snapshot[GMT_NAME_LEN+1];
486 *ppath_already_converted = false;
489 /* Must at least contain shadow:snapdir. */
493 if (priv->config->snapdir[0] == '/' &&
495 /* Absolute shadow:snapdir must be at the start. */
499 snapdirlen = strlen(priv->config->snapdir);
500 if (p[snapdirlen] != '/') {
501 /* shadow:snapdir must end as a separate component. */
505 if (p > abs_path && p[-1] != '/') {
506 /* shadow:snapdir must start as a separate component. */
511 p++; /* Move past the / */
514 * Need to return up to the next path
515 * component after the time.
516 * This will be used as the connectpath.
521 * No next path component.
524 connect_path = talloc_strdup(mem_ctx,
527 connect_path = talloc_strndup(mem_ctx,
531 if (connect_path == NULL) {
536 * Point p at the same offset in connect_path as
540 p = &connect_path[p - abs_path];
543 * Now ensure there is a time string at p.
544 * The SMB-format @GMT-token string is returned
548 if (!shadow_copy2_snapshot_to_gmt(handle,
552 TALLOC_FREE(connect_path);
556 if (pconnectpath != NULL) {
557 *pconnectpath = connect_path;
560 *ppath_already_converted = true;
562 DBG_DEBUG("path |%s| is already converted. "
563 "connect path = |%s|\n",
571 * This function does two things.
573 * 1). Checks if an incoming filename is already a
574 * snapshot converted pathname.
575 * If so, it returns the pathname truncated
576 * at the snapshot point which will be used
577 * as the connectpath, and then does an early return.
579 * 2). Checks if an incoming filename contains an
580 * SMB-layer @GMT- style timestamp.
581 * If so, it strips the timestamp, and returns
582 * both the timestamp and the stripped path
583 * (making it cwd-relative).
586 static bool _shadow_copy2_strip_snapshot_internal(TALLOC_CTX *mem_ctx,
587 struct vfs_handle_struct *handle,
588 const struct smb_filename *smb_fname,
592 bool *_already_converted,
593 const char *function)
595 char *stripped = NULL;
596 struct shadow_copy2_private *priv;
597 char *abs_path = NULL;
599 bool already_converted = false;
602 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
605 DBG_DEBUG("[from %s()] Path '%s'\n",
606 function, smb_fname_str_dbg(smb_fname));
608 if (_already_converted != NULL) {
609 *_already_converted = false;
612 abs_path = make_path_absolute(mem_ctx, priv, smb_fname->base_name);
613 if (abs_path == NULL) {
618 DBG_DEBUG("abs path '%s'\n", abs_path);
620 err = check_for_converted_path(mem_ctx,
627 /* error in conversion. */
632 if (already_converted) {
633 if (_already_converted != NULL) {
634 *_already_converted = true;
639 if (smb_fname->twrp == 0) {
643 if (ptimestamp != NULL) {
644 *ptimestamp = nt_time_to_unix(smb_fname->twrp);
647 if (pstripped != NULL) {
648 stripped = talloc_strdup(mem_ctx, abs_path);
649 if (stripped == NULL) {
654 if (smb_fname->base_name[0] != '/') {
655 ret = make_relative_path(priv->shadow_cwd, stripped);
657 DBG_DEBUG("Path '%s' "
658 "doesn't start with cwd '%s'\n",
659 stripped, priv->shadow_cwd);
665 *pstripped = stripped;
671 TALLOC_FREE(abs_path);
675 #define shadow_copy2_strip_snapshot_internal(mem_ctx, handle, orig_name, \
676 ptimestamp, pstripped, psnappath, _already_converted) \
677 _shadow_copy2_strip_snapshot_internal((mem_ctx), (handle), (orig_name), \
678 (ptimestamp), (pstripped), (psnappath), (_already_converted), \
681 static bool _shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
682 struct vfs_handle_struct *handle,
683 const struct smb_filename *orig_name,
686 const char *function)
688 return _shadow_copy2_strip_snapshot_internal(mem_ctx,
698 #define shadow_copy2_strip_snapshot(mem_ctx, handle, orig_name, \
699 ptimestamp, pstripped) \
700 _shadow_copy2_strip_snapshot((mem_ctx), (handle), (orig_name), \
701 (ptimestamp), (pstripped), __FUNCTION__)
703 static bool _shadow_copy2_strip_snapshot_converted(TALLOC_CTX *mem_ctx,
704 struct vfs_handle_struct *handle,
705 const struct smb_filename *orig_name,
709 const char *function)
711 return _shadow_copy2_strip_snapshot_internal(mem_ctx,
721 #define shadow_copy2_strip_snapshot_converted(mem_ctx, handle, orig_name, \
722 ptimestamp, pstripped, is_converted) \
723 _shadow_copy2_strip_snapshot_converted((mem_ctx), (handle), (orig_name), \
724 (ptimestamp), (pstripped), (is_converted), __FUNCTION__)
726 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
727 vfs_handle_struct *handle)
729 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
734 if (stat(path, &st) != 0) {
741 while ((p = strrchr(path, '/')) && p > path) {
743 if (stat(path, &st) != 0) {
747 if (st.st_dev != dev) {
757 * Convert from a name as handed in via the SMB layer
758 * and a timestamp into the local path of the snapshot
759 * of the provided file at the provided time.
760 * Also return the path in the snapshot corresponding
761 * to the file's share root.
763 static char *shadow_copy2_do_convert(TALLOC_CTX *mem_ctx,
764 struct vfs_handle_struct *handle,
765 const char *name, time_t timestamp,
766 size_t *snaproot_len)
768 struct smb_filename converted_fname;
770 size_t *slashes = NULL;
771 unsigned num_slashes;
775 char *converted = NULL;
776 size_t insertlen, connectlen = 0;
780 struct shadow_copy2_config *config;
781 struct shadow_copy2_private *priv;
782 size_t in_share_offset = 0;
784 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
787 config = priv->config;
789 DEBUG(10, ("converting '%s'\n", name));
791 if (!config->snapdirseverywhere) {
795 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
798 if (snapshot_path == NULL) {
802 if (config->rel_connectpath == NULL) {
803 converted = talloc_asprintf(mem_ctx, "%s/%s",
804 snapshot_path, name);
806 converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
808 config->rel_connectpath,
811 if (converted == NULL) {
815 ZERO_STRUCT(converted_fname);
816 converted_fname.base_name = converted;
818 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
819 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
821 ret, ret == 0 ? "ok" : strerror(errno)));
823 DEBUG(10, ("Found %s\n", converted));
826 if (snaproot_len != NULL) {
827 *snaproot_len = strlen(snapshot_path);
828 if (config->rel_connectpath != NULL) {
830 strlen(config->rel_connectpath) + 1;
838 /* never reached ... */
841 connectlen = strlen(handle->conn->connectpath);
843 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
845 path = talloc_asprintf(
846 mem_ctx, "%s/%s", handle->conn->connectpath, name);
852 pathlen = talloc_get_size(path)-1;
854 if (!shadow_copy2_find_slashes(talloc_tos(), path,
855 &slashes, &num_slashes)) {
859 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
860 if (insert == NULL) {
863 insertlen = talloc_get_size(insert)-1;
866 * Note: We deliberatly don't expensively initialize the
867 * array with talloc_zero here: Putting zero into
868 * converted[pathlen+insertlen] below is sufficient, because
869 * in the following for loop, the insert string is inserted
870 * at various slash places. So the memory up to position
871 * pathlen+insertlen will always be initialized when the
872 * converted string is used.
874 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
875 if (converted == NULL) {
879 if (path[pathlen-1] != '/') {
881 * Append a fake slash to find the snapshot root
884 tmp = talloc_realloc(talloc_tos(), slashes,
885 size_t, num_slashes+1);
890 slashes[num_slashes] = pathlen;
896 if (!config->crossmountpoints) {
897 min_offset = strlen(config->mount_point);
900 memcpy(converted, path, pathlen+1);
901 converted[pathlen+insertlen] = '\0';
903 ZERO_STRUCT(converted_fname);
904 converted_fname.base_name = converted;
906 for (i = num_slashes-1; i>=0; i--) {
912 if (offset < min_offset) {
917 if (offset >= connectlen) {
918 in_share_offset = offset;
921 memcpy(converted+offset, insert, insertlen);
924 memcpy(converted+offset, path + slashes[i],
925 pathlen - slashes[i]);
927 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
929 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
931 ret, ret == 0 ? "ok" : strerror(errno)));
934 if (snaproot_len != NULL) {
935 *snaproot_len = in_share_offset + insertlen;
939 if (errno == ENOTDIR) {
941 * This is a valid condition: We appended the
942 * .snapshots/@GMT.. to a file name. Just try
943 * with the upper levels.
947 if (errno != ENOENT) {
948 /* Other problem than "not found" */
957 DEBUG(10, ("Found %s\n", converted));
964 if (result == NULL) {
967 TALLOC_FREE(converted);
969 TALLOC_FREE(slashes);
971 if (saved_errno != 0) {
978 * Convert from a name as handed in via the SMB layer
979 * and a timestamp into the local path of the snapshot
980 * of the provided file at the provided time.
982 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
983 struct vfs_handle_struct *handle,
984 const char *name, time_t timestamp)
986 return shadow_copy2_do_convert(mem_ctx, handle, name, timestamp, NULL);
990 modify a sbuf return to ensure that inodes in the shadow directory
991 are different from those in the main directory
993 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
994 SMB_STRUCT_STAT *sbuf)
996 struct shadow_copy2_private *priv;
998 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1001 if (priv->config->fixinodes) {
1002 /* some snapshot systems, like GPFS, return the same
1003 device:inode for the snapshot files as the current
1004 files. That breaks the 'restore' button in the shadow copy
1005 GUI, as the client gets a sharing violation.
1007 This is a crude way of allowing both files to be
1008 open at once. It has a slight chance of inode
1009 number collision, but I can't see a better approach
1010 without significant VFS changes
1012 TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
1013 .dsize = strlen(fname) };
1016 shash = tdb_jenkins_hash(&key) & 0xFF000000;
1020 sbuf->st_ex_ino ^= shash;
1024 static int shadow_copy2_renameat(vfs_handle_struct *handle,
1025 files_struct *srcfsp,
1026 const struct smb_filename *smb_fname_src,
1027 files_struct *dstfsp,
1028 const struct smb_filename *smb_fname_dst)
1030 time_t timestamp_src = 0;
1031 time_t timestamp_dst = 0;
1032 char *snappath_src = NULL;
1033 char *snappath_dst = NULL;
1035 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle,
1037 ×tamp_src, NULL, &snappath_src,
1041 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle,
1043 ×tamp_dst, NULL, &snappath_dst,
1047 if (timestamp_src != 0) {
1051 if (timestamp_dst != 0) {
1056 * Don't allow rename on already converted paths.
1058 if (snappath_src != NULL) {
1062 if (snappath_dst != NULL) {
1066 return SMB_VFS_NEXT_RENAMEAT(handle,
1073 static int shadow_copy2_symlinkat(vfs_handle_struct *handle,
1074 const struct smb_filename *link_contents,
1075 struct files_struct *dirfsp,
1076 const struct smb_filename *new_smb_fname)
1078 time_t timestamp_old = 0;
1079 time_t timestamp_new = 0;
1080 char *snappath_old = NULL;
1081 char *snappath_new = NULL;
1083 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1092 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1101 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1106 * Don't allow symlinks on already converted paths.
1108 if ((snappath_old != NULL) || (snappath_new != NULL)) {
1112 return SMB_VFS_NEXT_SYMLINKAT(handle,
1118 static int shadow_copy2_linkat(vfs_handle_struct *handle,
1119 files_struct *srcfsp,
1120 const struct smb_filename *old_smb_fname,
1121 files_struct *dstfsp,
1122 const struct smb_filename *new_smb_fname,
1125 time_t timestamp_old = 0;
1126 time_t timestamp_new = 0;
1127 char *snappath_old = NULL;
1128 char *snappath_new = NULL;
1130 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1139 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1148 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1153 * Don't allow links on already converted paths.
1155 if ((snappath_old != NULL) || (snappath_new != NULL)) {
1159 return SMB_VFS_NEXT_LINKAT(handle,
1167 static int shadow_copy2_stat(vfs_handle_struct *handle,
1168 struct smb_filename *smb_fname)
1170 struct shadow_copy2_private *priv = NULL;
1171 time_t timestamp = 0;
1172 char *stripped = NULL;
1173 bool converted = false;
1174 char *abspath = NULL;
1178 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1181 if (!shadow_copy2_strip_snapshot_converted(talloc_tos(),
1189 if (timestamp == 0) {
1190 TALLOC_FREE(stripped);
1191 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1199 abspath = make_path_absolute(talloc_tos(),
1201 smb_fname->base_name);
1202 if (abspath == NULL) {
1206 convert_sbuf(handle, abspath, &smb_fname->st);
1207 TALLOC_FREE(abspath);
1211 tmp = smb_fname->base_name;
1212 smb_fname->base_name = shadow_copy2_convert(
1213 talloc_tos(), handle, stripped, timestamp);
1214 TALLOC_FREE(stripped);
1216 if (smb_fname->base_name == NULL) {
1217 smb_fname->base_name = tmp;
1221 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1226 abspath = make_path_absolute(talloc_tos(),
1228 smb_fname->base_name);
1229 if (abspath == NULL) {
1234 convert_sbuf(handle, abspath, &smb_fname->st);
1235 TALLOC_FREE(abspath);
1238 TALLOC_FREE(smb_fname->base_name);
1239 smb_fname->base_name = tmp;
1244 static int shadow_copy2_lstat(vfs_handle_struct *handle,
1245 struct smb_filename *smb_fname)
1247 struct shadow_copy2_private *priv = NULL;
1248 time_t timestamp = 0;
1249 char *stripped = NULL;
1250 bool converted = false;
1251 char *abspath = NULL;
1255 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1258 if (!shadow_copy2_strip_snapshot_converted(talloc_tos(),
1266 if (timestamp == 0) {
1267 TALLOC_FREE(stripped);
1268 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1276 abspath = make_path_absolute(talloc_tos(),
1278 smb_fname->base_name);
1279 if (abspath == NULL) {
1283 convert_sbuf(handle, abspath, &smb_fname->st);
1284 TALLOC_FREE(abspath);
1288 tmp = smb_fname->base_name;
1289 smb_fname->base_name = shadow_copy2_convert(
1290 talloc_tos(), handle, stripped, timestamp);
1291 TALLOC_FREE(stripped);
1293 if (smb_fname->base_name == NULL) {
1294 smb_fname->base_name = tmp;
1298 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1303 abspath = make_path_absolute(talloc_tos(),
1305 smb_fname->base_name);
1306 if (abspath == NULL) {
1311 convert_sbuf(handle, abspath, &smb_fname->st);
1312 TALLOC_FREE(abspath);
1315 TALLOC_FREE(smb_fname->base_name);
1316 smb_fname->base_name = tmp;
1321 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
1322 SMB_STRUCT_STAT *sbuf)
1324 struct shadow_copy2_private *priv = NULL;
1325 time_t timestamp = 0;
1326 struct smb_filename *orig_smb_fname = NULL;
1327 struct smb_filename vss_smb_fname;
1328 struct smb_filename *orig_base_smb_fname = NULL;
1329 struct smb_filename vss_base_smb_fname;
1330 char *stripped = NULL;
1331 char *abspath = NULL;
1332 bool converted = false;
1336 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1339 ok = shadow_copy2_strip_snapshot_converted(talloc_tos(),
1349 if (timestamp == 0) {
1350 TALLOC_FREE(stripped);
1351 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1359 abspath = make_path_absolute(talloc_tos(),
1361 fsp->fsp_name->base_name);
1362 if (abspath == NULL) {
1366 convert_sbuf(handle, abspath, sbuf);
1367 TALLOC_FREE(abspath);
1371 vss_smb_fname = *fsp->fsp_name;
1372 vss_smb_fname.base_name = shadow_copy2_convert(talloc_tos(),
1376 TALLOC_FREE(stripped);
1377 if (vss_smb_fname.base_name == NULL) {
1381 orig_smb_fname = fsp->fsp_name;
1382 fsp->fsp_name = &vss_smb_fname;
1384 if (fsp_is_alternate_stream(fsp)) {
1385 vss_base_smb_fname = *fsp->base_fsp->fsp_name;
1386 vss_base_smb_fname.base_name = vss_smb_fname.base_name;
1387 orig_base_smb_fname = fsp->base_fsp->fsp_name;
1388 fsp->base_fsp->fsp_name = &vss_base_smb_fname;
1391 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1396 abspath = make_path_absolute(talloc_tos(),
1398 fsp->fsp_name->base_name);
1399 if (abspath == NULL) {
1404 convert_sbuf(handle, abspath, sbuf);
1405 TALLOC_FREE(abspath);
1408 fsp->fsp_name = orig_smb_fname;
1409 if (fsp_is_alternate_stream(fsp)) {
1410 fsp->base_fsp->fsp_name = orig_base_smb_fname;
1416 static int shadow_copy2_fstatat(
1417 struct vfs_handle_struct *handle,
1418 const struct files_struct *dirfsp,
1419 const struct smb_filename *smb_fname_in,
1420 SMB_STRUCT_STAT *sbuf,
1423 struct shadow_copy2_private *priv = NULL;
1424 struct smb_filename *smb_fname = NULL;
1425 time_t timestamp = 0;
1426 char *stripped = NULL;
1427 char *abspath = NULL;
1428 bool converted = false;
1432 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1435 smb_fname = full_path_from_dirfsp_atname(talloc_tos(),
1438 if (smb_fname == NULL) {
1443 ok = shadow_copy2_strip_snapshot_converted(talloc_tos(),
1452 if (timestamp == 0) {
1453 TALLOC_FREE(stripped);
1454 ret = SMB_VFS_NEXT_FSTATAT(
1455 handle, dirfsp, smb_fname_in, sbuf, flags);
1463 abspath = make_path_absolute(
1464 talloc_tos(), priv, smb_fname->base_name);
1465 if (abspath == NULL) {
1470 convert_sbuf(handle, abspath, sbuf);
1471 TALLOC_FREE(abspath);
1475 smb_fname->base_name = shadow_copy2_convert(
1476 smb_fname, handle, stripped, timestamp);
1477 TALLOC_FREE(stripped);
1478 if (smb_fname->base_name == NULL) {
1479 TALLOC_FREE(smb_fname);
1484 ret = SMB_VFS_NEXT_FSTATAT(handle,
1490 int saved_errno = errno;
1491 TALLOC_FREE(smb_fname);
1492 errno = saved_errno;
1496 abspath = make_path_absolute(
1497 talloc_tos(), priv, smb_fname->base_name);
1498 if (abspath == NULL) {
1499 TALLOC_FREE(smb_fname);
1504 convert_sbuf(handle, abspath, sbuf);
1505 TALLOC_FREE(abspath);
1507 TALLOC_FREE(smb_fname);
1512 static struct smb_filename *shadow_copy2_openat_name(
1513 TALLOC_CTX *mem_ctx,
1514 const struct files_struct *dirfsp,
1515 const struct files_struct *fsp,
1516 const struct smb_filename *smb_fname_in)
1518 struct smb_filename *result = NULL;
1520 if (fsp->base_fsp != NULL) {
1521 struct smb_filename *base_fname = fsp->base_fsp->fsp_name;
1523 SMB_ASSERT(is_named_stream(smb_fname_in));
1525 result = synthetic_smb_fname(
1527 base_fname->base_name,
1528 smb_fname_in->stream_name,
1531 smb_fname_in->flags);
1533 result = full_path_from_dirfsp_atname(
1534 mem_ctx, dirfsp, smb_fname_in);
1540 static int shadow_copy2_openat(vfs_handle_struct *handle,
1541 const struct files_struct *dirfsp,
1542 const struct smb_filename *smb_fname_in,
1543 struct files_struct *fsp,
1544 const struct vfs_open_how *_how)
1546 struct vfs_open_how how = *_how;
1547 struct smb_filename *smb_fname = NULL;
1548 time_t timestamp = 0;
1549 char *stripped = NULL;
1550 bool is_converted = false;
1551 int saved_errno = 0;
1555 if (how.resolve != 0) {
1560 smb_fname = shadow_copy2_openat_name(
1561 talloc_tos(), dirfsp, fsp, smb_fname_in);
1562 if (smb_fname == NULL) {
1567 ok = shadow_copy2_strip_snapshot_converted(talloc_tos(),
1576 if (timestamp == 0) {
1579 * Just pave over the user requested mode and use
1580 * O_RDONLY. Later attempts by the client to write on
1581 * the handle will fail in the pwrite() syscall with
1582 * EINVAL which we carefully map to EROFS. In sum, this
1583 * matches Windows behaviour.
1585 how.flags &= ~(O_WRONLY | O_RDWR | O_CREAT);
1587 return SMB_VFS_NEXT_OPENAT(handle,
1594 smb_fname->base_name = shadow_copy2_convert(smb_fname,
1598 if (smb_fname->base_name == NULL) {
1600 TALLOC_FREE(stripped);
1601 TALLOC_FREE(smb_fname);
1605 TALLOC_FREE(stripped);
1608 * Just pave over the user requested mode and use O_RDONLY. Later
1609 * attempts by the client to write on the handle will fail in the
1610 * pwrite() syscall with EINVAL which we carefully map to EROFS. In sum,
1611 * this matches Windows behaviour.
1613 how.flags &= ~(O_WRONLY | O_RDWR | O_CREAT);
1615 ret = SMB_VFS_NEXT_OPENAT(handle,
1621 saved_errno = errno;
1624 TALLOC_FREE(smb_fname);
1626 if (saved_errno != 0) {
1627 errno = saved_errno;
1632 static int shadow_copy2_unlinkat(vfs_handle_struct *handle,
1633 struct files_struct *dirfsp,
1634 const struct smb_filename *smb_fname,
1637 time_t timestamp = 0;
1639 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1641 ×tamp, NULL)) {
1644 if (timestamp != 0) {
1648 return SMB_VFS_NEXT_UNLINKAT(handle,
1654 static int shadow_copy2_fchmod(vfs_handle_struct *handle,
1655 struct files_struct *fsp,
1658 time_t timestamp = 0;
1659 const struct smb_filename *smb_fname = NULL;
1661 smb_fname = fsp->fsp_name;
1662 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1669 if (timestamp != 0) {
1673 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1676 static void store_cwd_data(vfs_handle_struct *handle,
1677 const char *connectpath)
1679 struct shadow_copy2_private *priv = NULL;
1680 struct smb_filename *cwd_fname = NULL;
1682 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1685 TALLOC_FREE(priv->shadow_cwd);
1686 cwd_fname = SMB_VFS_NEXT_GETWD(handle, talloc_tos());
1687 if (cwd_fname == NULL) {
1688 smb_panic("getwd failed\n");
1690 DBG_DEBUG("shadow cwd = %s\n", cwd_fname->base_name);
1691 priv->shadow_cwd = talloc_strdup(priv, cwd_fname->base_name);
1692 TALLOC_FREE(cwd_fname);
1693 if (priv->shadow_cwd == NULL) {
1694 smb_panic("talloc failed\n");
1696 TALLOC_FREE(priv->shadow_connectpath);
1698 DBG_DEBUG("shadow connectpath = %s\n", connectpath);
1699 priv->shadow_connectpath = talloc_strdup(priv, connectpath);
1700 if (priv->shadow_connectpath == NULL) {
1701 smb_panic("talloc failed\n");
1706 static int shadow_copy2_chdir(vfs_handle_struct *handle,
1707 const struct smb_filename *smb_fname)
1709 time_t timestamp = 0;
1710 char *stripped = NULL;
1711 char *snappath = NULL;
1713 int saved_errno = 0;
1715 size_t rootpath_len = 0;
1716 struct smb_filename *conv_smb_fname = NULL;
1718 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1727 if (stripped != NULL) {
1728 conv = shadow_copy2_do_convert(talloc_tos(),
1733 TALLOC_FREE(stripped);
1737 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1744 conv_smb_fname = cp_smb_filename(talloc_tos(), smb_fname);
1747 if (conv_smb_fname == NULL) {
1753 ret = SMB_VFS_NEXT_CHDIR(handle, conv_smb_fname);
1755 saved_errno = errno;
1759 if (conv != NULL && rootpath_len != 0) {
1760 conv[rootpath_len] = '\0';
1761 } else if (snappath != 0) {
1765 store_cwd_data(handle, conv);
1768 TALLOC_FREE(stripped);
1770 TALLOC_FREE(conv_smb_fname);
1772 if (saved_errno != 0) {
1773 errno = saved_errno;
1778 static int shadow_copy2_fntimes(vfs_handle_struct *handle,
1780 struct smb_file_time *ft)
1782 time_t timestamp = 0;
1784 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1791 if (timestamp != 0) {
1795 return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
1798 static int shadow_copy2_readlinkat(vfs_handle_struct *handle,
1799 const struct files_struct *dirfsp,
1800 const struct smb_filename *smb_fname,
1804 time_t timestamp = 0;
1805 char *stripped = NULL;
1806 int saved_errno = 0;
1808 struct smb_filename *full_fname = NULL;
1809 struct smb_filename *conv = NULL;
1811 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1814 if (full_fname == NULL) {
1819 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1824 TALLOC_FREE(full_fname);
1828 if (timestamp == 0) {
1829 TALLOC_FREE(full_fname);
1830 TALLOC_FREE(stripped);
1831 return SMB_VFS_NEXT_READLINKAT(handle,
1837 conv = cp_smb_filename(talloc_tos(), full_fname);
1839 TALLOC_FREE(full_fname);
1840 TALLOC_FREE(stripped);
1844 TALLOC_FREE(full_fname);
1845 conv->base_name = shadow_copy2_convert(
1846 conv, handle, stripped, timestamp);
1847 TALLOC_FREE(stripped);
1848 if (conv->base_name == NULL) {
1851 ret = SMB_VFS_NEXT_READLINKAT(handle,
1852 handle->conn->cwd_fsp,
1857 saved_errno = errno;
1860 if (saved_errno != 0) {
1861 errno = saved_errno;
1866 static int shadow_copy2_mknodat(vfs_handle_struct *handle,
1867 files_struct *dirfsp,
1868 const struct smb_filename *smb_fname,
1872 time_t timestamp = 0;
1874 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1876 ×tamp, NULL)) {
1879 if (timestamp != 0) {
1883 return SMB_VFS_NEXT_MKNODAT(handle,
1890 static struct smb_filename *shadow_copy2_realpath(vfs_handle_struct *handle,
1892 const struct smb_filename *smb_fname)
1894 time_t timestamp = 0;
1895 char *stripped = NULL;
1896 struct smb_filename *result_fname = NULL;
1897 struct smb_filename *conv_fname = NULL;
1898 int saved_errno = 0;
1900 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1902 ×tamp, &stripped)) {
1905 if (timestamp == 0) {
1906 return SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
1909 conv_fname = cp_smb_filename(talloc_tos(), smb_fname);
1910 if (conv_fname == NULL) {
1913 conv_fname->base_name = shadow_copy2_convert(
1914 conv_fname, handle, stripped, timestamp);
1915 if (conv_fname->base_name == NULL) {
1919 result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, conv_fname);
1922 if (result_fname == NULL) {
1923 saved_errno = errno;
1925 TALLOC_FREE(conv_fname);
1926 TALLOC_FREE(stripped);
1927 if (saved_errno != 0) {
1928 errno = saved_errno;
1930 return result_fname;
1934 * Check whether a given directory contains a
1935 * snapshot directory as direct subdirectory.
1936 * If yes, return the path of the snapshot-subdir,
1937 * otherwise return NULL.
1939 static char *have_snapdir(struct vfs_handle_struct *handle,
1942 struct smb_filename smb_fname;
1944 struct shadow_copy2_private *priv;
1946 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1949 ZERO_STRUCT(smb_fname);
1950 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1951 path, priv->config->snapdir);
1952 if (smb_fname.base_name == NULL) {
1956 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1957 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1958 return smb_fname.base_name;
1960 TALLOC_FREE(smb_fname.base_name);
1965 * Find the snapshot directory (if any) for the given
1966 * filename (which is relative to the share).
1968 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1969 struct vfs_handle_struct *handle,
1970 struct smb_filename *smb_fname)
1973 const char *snapdir;
1974 struct shadow_copy2_config *config;
1975 struct shadow_copy2_private *priv;
1977 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1980 config = priv->config;
1983 * If the non-snapdisrseverywhere mode, we should not search!
1985 if (!config->snapdirseverywhere) {
1986 return config->snapshot_basepath;
1989 path = talloc_asprintf(mem_ctx, "%s/%s",
1990 handle->conn->connectpath,
1991 smb_fname->base_name);
1996 snapdir = have_snapdir(handle, path);
1997 if (snapdir != NULL) {
2002 while ((p = strrchr(path, '/')) && (p > path)) {
2006 snapdir = have_snapdir(handle, path);
2007 if (snapdir != NULL) {
2016 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
2018 char *gmt, size_t gmt_len)
2020 struct tm timestamp;
2022 unsigned long int timestamp_long;
2024 struct shadow_copy2_config *config;
2025 struct shadow_copy2_private *priv;
2026 char *tmpstr = NULL;
2028 bool converted = false;
2031 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2034 config = priv->config;
2036 fmt = config->gmt_format;
2039 * If regex is provided, then we will have to parse the
2040 * filename which will contain both the prefix and the time format.
2041 * e.g. <prefix><delimiter><time_format>
2043 if (priv->snaps->regex != NULL) {
2044 tmpstr = talloc_strdup(talloc_tos(), name);
2045 /* point "name" to the time format */
2046 name = strstr(name, priv->config->delimiter);
2050 /* Extract the prefix */
2051 tmp = strstr(tmpstr, priv->config->delimiter);
2058 ret = regexec(priv->snaps->regex, tmpstr, 0, NULL, 0);
2060 DBG_DEBUG("shadow_copy2_snapshot_to_gmt: "
2061 "no regex match for %s\n", tmpstr);
2066 ZERO_STRUCT(timestamp);
2067 if (config->use_sscanf) {
2068 if (sscanf(name, fmt, ×tamp_long) != 1) {
2069 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
2070 "no sscanf match %s: %s\n",
2074 timestamp_t = timestamp_long;
2075 gmtime_r(×tamp_t, ×tamp);
2077 if (strptime(name, fmt, ×tamp) == NULL) {
2078 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
2079 "no match %s: %s\n",
2083 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
2086 if (config->use_localtime) {
2087 timestamp.tm_isdst = -1;
2088 timestamp_t = mktime(×tamp);
2089 gmtime_r(×tamp_t, ×tamp);
2093 strftime(gmt, gmt_len, GMT_FORMAT, ×tamp);
2097 TALLOC_FREE(tmpstr);
2101 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
2103 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
2106 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
2108 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
2112 sort the shadow copy data in ascending or descending order
2114 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
2115 struct shadow_copy_data *shadow_copy2_data)
2117 int (*cmpfunc)(const void *, const void *);
2119 struct shadow_copy2_private *priv;
2121 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2124 sort = priv->config->sort_order;
2129 if (strcmp(sort, "asc") == 0) {
2130 cmpfunc = shadow_copy2_label_cmp_asc;
2131 } else if (strcmp(sort, "desc") == 0) {
2132 cmpfunc = shadow_copy2_label_cmp_desc;
2137 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
2138 shadow_copy2_data->labels)
2140 TYPESAFE_QSORT(shadow_copy2_data->labels,
2141 shadow_copy2_data->num_volumes,
2146 static int shadow_copy2_get_shadow_copy_data(
2147 vfs_handle_struct *handle, files_struct *fsp,
2148 struct shadow_copy_data *shadow_copy2_data,
2152 const char *snapdir;
2153 struct smb_filename *snapdir_smb_fname = NULL;
2154 struct files_struct *dirfsp = NULL;
2155 struct files_struct *fspcwd = NULL;
2157 TALLOC_CTX *tmp_ctx = talloc_stackframe();
2158 struct shadow_copy2_private *priv = NULL;
2159 struct shadow_copy2_snapentry *tmpentry = NULL;
2160 bool get_snaplist = false;
2161 struct vfs_open_how how = {
2162 .flags = O_RDONLY, .mode = 0,
2167 int saved_errno = 0;
2169 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
2170 if (snapdir == NULL) {
2171 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
2172 handle->conn->connectpath));
2177 snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
2182 fsp->fsp_name->flags);
2183 if (snapdir_smb_fname == NULL) {
2188 status = create_internal_dirfsp(handle->conn,
2191 if (!NT_STATUS_IS_OK(status)) {
2192 DBG_WARNING("create_internal_dir_fsp() failed for '%s'"
2193 " - %s\n", snapdir, nt_errstr(status));
2198 status = vfs_at_fspcwd(talloc_tos(), handle->conn, &fspcwd);
2199 if (!NT_STATUS_IS_OK(status)) {
2205 how.flags |= O_DIRECTORY;
2208 fd = SMB_VFS_NEXT_OPENAT(handle,
2214 DBG_WARNING("SMB_VFS_NEXT_OPEN failed for '%s'"
2215 " - %s\n", snapdir, strerror(errno));
2219 fsp_set_fd(dirfsp, fd);
2221 /* Now we have the handle, check access here. */
2222 status = smbd_check_access_rights_fsp(fspcwd,
2226 if (!NT_STATUS_IS_OK(status)) {
2227 DBG_ERR("user does not have list permission "
2229 fsp_str_dbg(dirfsp));
2234 p = SMB_VFS_NEXT_FDOPENDIR(handle, dirfsp, NULL, 0);
2236 DBG_NOTICE("shadow_copy2: SMB_VFS_NEXT_FDOPENDIR() failed for '%s'"
2237 " - %s\n", snapdir, strerror(errno));
2242 if (shadow_copy2_data != NULL) {
2243 shadow_copy2_data->num_volumes = 0;
2244 shadow_copy2_data->labels = NULL;
2247 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2251 * Normally this function is called twice once with labels = false and
2252 * then with labels = true. When labels is false it will return the
2253 * number of volumes so that the caller can allocate memory for that
2254 * many labels. Therefore to eliminate snaplist both the times it is
2255 * good to check if labels is set or not.
2257 * shadow_copy2_data is NULL when we only want to update the list and
2258 * don't want any labels.
2260 if ((priv->snaps->regex != NULL) && (labels || shadow_copy2_data == NULL)) {
2261 get_snaplist = true;
2262 /* Reset the global snaplist */
2263 shadow_copy2_delete_snaplist(priv);
2265 /* Set the current time as snaplist update time */
2266 time(&(priv->snaps->fetch_time));
2269 while ((d = SMB_VFS_NEXT_READDIR(handle, dirfsp, p, NULL))) {
2270 char snapshot[GMT_NAME_LEN+1];
2271 SHADOW_COPY_LABEL *tlabels;
2274 * ignore names not of the right form in the snapshot
2277 if (!shadow_copy2_snapshot_to_gmt(
2279 snapshot, sizeof(snapshot))) {
2281 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
2282 "ignoring %s\n", d->d_name));
2285 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
2286 d->d_name, snapshot));
2290 * Create a snap entry for each successful
2293 tmpentry = shadow_copy2_create_snapentry(priv);
2294 if (tmpentry == NULL) {
2295 DBG_ERR("talloc_zero() failed\n");
2298 tmpentry->snapname = talloc_strdup(tmpentry, d->d_name);
2299 tmpentry->time_fmt = talloc_strdup(tmpentry, snapshot);
2302 if (shadow_copy2_data == NULL) {
2307 /* the caller doesn't want the labels */
2308 shadow_copy2_data->num_volumes++;
2312 tlabels = talloc_realloc(shadow_copy2_data,
2313 shadow_copy2_data->labels,
2315 shadow_copy2_data->num_volumes+1);
2316 if (tlabels == NULL) {
2317 DEBUG(0,("shadow_copy2: out of memory\n"));
2321 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
2324 shadow_copy2_data->num_volumes++;
2325 shadow_copy2_data->labels = tlabels;
2328 shadow_copy2_sort_data(handle, shadow_copy2_data);
2333 saved_errno = errno;
2335 TALLOC_FREE(fspcwd );
2337 SMB_VFS_NEXT_CLOSEDIR(handle, p);
2339 if (dirfsp != NULL) {
2341 * VFS_CLOSEDIR implicitly
2342 * closed the associated fd.
2344 fsp_set_fd(dirfsp, -1);
2347 if (dirfsp != NULL) {
2349 file_free(NULL, dirfsp);
2351 TALLOC_FREE(tmp_ctx);
2352 if (saved_errno != 0) {
2353 errno = saved_errno;
2358 static int shadow_copy2_mkdirat(vfs_handle_struct *handle,
2359 struct files_struct *dirfsp,
2360 const struct smb_filename *smb_fname,
2363 struct smb_filename *full_fname = NULL;
2364 time_t timestamp = 0;
2366 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2369 if (full_fname == NULL) {
2374 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2381 TALLOC_FREE(full_fname);
2382 if (timestamp != 0) {
2386 return SMB_VFS_NEXT_MKDIRAT(handle,
2392 static int shadow_copy2_fchflags(vfs_handle_struct *handle,
2393 struct files_struct *fsp,
2396 time_t timestamp = 0;
2398 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2405 if (timestamp != 0) {
2409 return SMB_VFS_NEXT_FCHFLAGS(handle, fsp, flags);
2412 static int shadow_copy2_fsetxattr(struct vfs_handle_struct *handle,
2413 struct files_struct *fsp,
2414 const char *aname, const void *value,
2415 size_t size, int flags)
2417 time_t timestamp = 0;
2418 const struct smb_filename *smb_fname = NULL;
2420 smb_fname = fsp->fsp_name;
2421 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2428 if (timestamp != 0) {
2432 return SMB_VFS_NEXT_FSETXATTR(handle, fsp,
2433 aname, value, size, flags);
2436 static NTSTATUS shadow_copy2_create_dfs_pathat(struct vfs_handle_struct *handle,
2437 struct files_struct *dirfsp,
2438 const struct smb_filename *smb_fname,
2439 const struct referral *reflist,
2440 size_t referral_count)
2442 time_t timestamp = 0;
2444 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2449 return NT_STATUS_NO_MEMORY;
2451 if (timestamp != 0) {
2452 return NT_STATUS_MEDIA_WRITE_PROTECTED;
2454 return SMB_VFS_NEXT_CREATE_DFS_PATHAT(handle,
2461 static NTSTATUS shadow_copy2_read_dfs_pathat(struct vfs_handle_struct *handle,
2462 TALLOC_CTX *mem_ctx,
2463 struct files_struct *dirfsp,
2464 struct smb_filename *smb_fname,
2465 struct referral **ppreflist,
2466 size_t *preferral_count)
2468 time_t timestamp = 0;
2469 char *stripped = NULL;
2470 struct smb_filename *full_fname = NULL;
2471 struct smb_filename *conv = NULL;
2474 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
2477 if (full_fname == NULL) {
2478 return NT_STATUS_NO_MEMORY;
2481 if (!shadow_copy2_strip_snapshot(mem_ctx,
2486 TALLOC_FREE(full_fname);
2487 return NT_STATUS_NO_MEMORY;
2489 if (timestamp == 0) {
2490 TALLOC_FREE(full_fname);
2491 TALLOC_FREE(stripped);
2492 return SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
2500 conv = cp_smb_filename(mem_ctx, full_fname);
2502 TALLOC_FREE(full_fname);
2503 TALLOC_FREE(stripped);
2504 return NT_STATUS_NO_MEMORY;
2506 TALLOC_FREE(full_fname);
2507 conv->base_name = shadow_copy2_convert(conv,
2511 TALLOC_FREE(stripped);
2512 if (conv->base_name == NULL) {
2514 return NT_STATUS_NO_MEMORY;
2517 status = SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
2519 handle->conn->cwd_fsp,
2524 if (NT_STATUS_IS_OK(status)) {
2525 /* Return any stat(2) info. */
2526 smb_fname->st = conv->st;
2533 static NTSTATUS shadow_copy2_get_real_filename_at(
2534 struct vfs_handle_struct *handle,
2535 struct files_struct *dirfsp,
2537 TALLOC_CTX *mem_ctx,
2540 struct shadow_copy2_private *priv = NULL;
2541 time_t timestamp = 0;
2542 char *stripped = NULL;
2544 struct smb_filename *conv_fname = NULL;
2548 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2549 return NT_STATUS_INTERNAL_ERROR);
2551 DBG_DEBUG("Path=[%s] name=[%s]\n", fsp_str_dbg(dirfsp), name);
2553 ok = shadow_copy2_strip_snapshot(
2554 talloc_tos(), handle, dirfsp->fsp_name, ×tamp, &stripped);
2556 status = map_nt_error_from_unix(errno);
2557 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
2560 if (timestamp == 0) {
2561 DEBUG(10, ("timestamp == 0\n"));
2562 return SMB_VFS_NEXT_GET_REAL_FILENAME_AT(
2563 handle, dirfsp, name, mem_ctx, found_name);
2567 * Note that stripped may be an empty string "" if path was ".". As
2568 * shadow_copy2_convert() combines "" with the shadow-copy tree connect
2569 * root fullpath and get_real_filename_full_scan() has an explicit check
2570 * for "" this works.
2572 DBG_DEBUG("stripped [%s]\n", stripped);
2574 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2576 status = map_nt_error_from_unix(errno);
2577 DBG_DEBUG("shadow_copy2_convert [%s] failed: %s\n",
2583 status = synthetic_pathref(
2585 dirfsp->conn->cwd_fsp,
2592 if (!NT_STATUS_IS_OK(status)) {
2596 DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
2597 "name=[%s]\n", conv, name));
2598 status = SMB_VFS_NEXT_GET_REAL_FILENAME_AT(
2599 handle, conv_fname->fsp, name, mem_ctx, found_name);
2600 DEBUG(10, ("NEXT_REAL_FILE_NAME returned %s\n", nt_errstr(status)));
2601 if (NT_STATUS_IS_OK(status)) {
2602 TALLOC_FREE(conv_fname);
2603 return NT_STATUS_OK;
2605 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
2606 TALLOC_FREE(conv_fname);
2608 return NT_STATUS_NOT_SUPPORTED;
2611 status = get_real_filename_full_scan_at(
2612 conv_fname->fsp, name, false, mem_ctx, found_name);
2613 TALLOC_FREE(conv_fname);
2614 if (!NT_STATUS_IS_OK(status)) {
2615 DBG_DEBUG("Scan [%s] for [%s] failed\n",
2620 DBG_DEBUG("Scan [%s] for [%s] returned [%s]\n",
2621 conv, name, *found_name);
2624 return NT_STATUS_OK;
2627 static const char *shadow_copy2_connectpath(
2628 struct vfs_handle_struct *handle,
2629 const struct files_struct *dirfsp,
2630 const struct smb_filename *smb_fname_in)
2632 time_t timestamp = 0;
2633 char *stripped = NULL;
2635 const char *fname = smb_fname_in->base_name;
2636 const struct smb_filename *full = NULL;
2637 struct smb_filename smb_fname = {0};
2638 struct smb_filename *result_fname = NULL;
2639 char *result = NULL;
2640 char *parent_dir = NULL;
2641 int saved_errno = 0;
2642 size_t rootpath_len = 0;
2643 struct shadow_copy2_private *priv = NULL;
2645 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2648 DBG_DEBUG("Calc connect path for [%s]\n", fname);
2650 if (priv->shadow_connectpath != NULL) {
2651 DBG_DEBUG("cached connect path is [%s]\n",
2652 priv->shadow_connectpath);
2653 return priv->shadow_connectpath;
2656 full = full_path_from_dirfsp_atname(
2657 talloc_tos(), dirfsp, smb_fname_in);
2662 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, full,
2663 ×tamp, &stripped)) {
2666 if (timestamp == 0) {
2667 return SMB_VFS_NEXT_CONNECTPATH(handle, dirfsp, smb_fname_in);
2670 tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
2673 if (errno != ENOENT) {
2678 * If the converted path does not exist, and converting
2679 * the parent yields something that does exist, then
2680 * this path refers to something that has not been
2681 * created yet, relative to the parent path.
2682 * The snapshot finding is relative to the parent.
2683 * (usually snapshots are read/only but this is not
2684 * necessarily true).
2685 * This code also covers getting a wildcard in the
2686 * last component, because this function is called
2687 * prior to sanitizing the path, and in SMB1 we may
2688 * get wildcards in path names.
2690 if (!parent_dirname(talloc_tos(), stripped, &parent_dir,
2696 tmp = shadow_copy2_do_convert(talloc_tos(), handle, parent_dir,
2697 timestamp, &rootpath_len);
2703 DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
2704 (int)rootpath_len, tmp);
2706 tmp[rootpath_len] = '\0';
2707 smb_fname = (struct smb_filename) { .base_name = tmp };
2709 result_fname = SMB_VFS_NEXT_REALPATH(handle, priv, &smb_fname);
2710 if (result_fname == NULL) {
2715 * SMB_VFS_NEXT_REALPATH returns a talloc'ed string.
2716 * Don't leak memory.
2718 TALLOC_FREE(priv->shadow_realpath);
2719 priv->shadow_realpath = result_fname;
2720 result = priv->shadow_realpath->base_name;
2722 DBG_DEBUG("connect path is [%s]\n", result);
2725 if (result == NULL) {
2726 saved_errno = errno;
2729 TALLOC_FREE(stripped);
2730 TALLOC_FREE(parent_dir);
2731 if (saved_errno != 0) {
2732 errno = saved_errno;
2737 static NTSTATUS shadow_copy2_parent_pathname(vfs_handle_struct *handle,
2739 const struct smb_filename *smb_fname_in,
2740 struct smb_filename **parent_dir_out,
2741 struct smb_filename **atname_out)
2743 time_t timestamp = 0;
2744 char *stripped = NULL;
2745 char *converted_name = NULL;
2746 struct smb_filename *smb_fname = NULL;
2747 struct smb_filename *parent = NULL;
2748 struct smb_filename *atname = NULL;
2749 struct shadow_copy2_private *priv = NULL;
2751 bool is_converted = false;
2752 NTSTATUS status = NT_STATUS_OK;
2753 TALLOC_CTX *frame = NULL;
2755 SMB_VFS_HANDLE_GET_DATA(handle,
2757 struct shadow_copy2_private,
2758 return NT_STATUS_INTERNAL_ERROR);
2760 frame = talloc_stackframe();
2762 smb_fname = cp_smb_filename(frame, smb_fname_in);
2763 if (smb_fname == NULL) {
2764 status = NT_STATUS_NO_MEMORY;
2768 /* First, call the default PARENT_PATHNAME. */
2769 status = SMB_VFS_NEXT_PARENT_PATHNAME(handle,
2774 if (!NT_STATUS_IS_OK(status)) {
2778 if (parent->twrp == 0) {
2780 * Parent is not a snapshot path, return
2781 * the regular result.
2783 status = NT_STATUS_OK;
2787 /* See if we can find a snapshot for the parent. */
2788 ok = shadow_copy2_strip_snapshot_converted(frame,
2795 status = map_nt_error_from_unix(errno);
2801 * Already found snapshot for parent so wipe
2808 converted_name = shadow_copy2_convert(frame,
2813 if (converted_name == NULL) {
2815 * Can't find snapshot for parent so wipe
2823 *parent_dir_out = talloc_move(ctx, &parent);
2824 if (atname_out != NULL) {
2825 *atname_out = talloc_move(*parent_dir_out, &atname);
2834 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
2835 const struct smb_filename *smb_fname,
2840 time_t timestamp = 0;
2841 char *stripped = NULL;
2842 int saved_errno = 0;
2844 struct smb_filename *conv_smb_fname = NULL;
2845 uint64_t ret = (uint64_t)-1;
2847 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2852 return (uint64_t)-1;
2854 if (timestamp == 0) {
2855 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2856 bsize, dfree, dsize);
2858 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2859 TALLOC_FREE(stripped);
2861 return (uint64_t)-1;
2863 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2869 if (conv_smb_fname == NULL) {
2871 return (uint64_t)-1;
2873 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv_smb_fname,
2874 bsize, dfree, dsize);
2875 if (ret == (uint64_t)-1) {
2876 saved_errno = errno;
2879 TALLOC_FREE(conv_smb_fname);
2880 if (saved_errno != 0) {
2881 errno = saved_errno;
2886 static int shadow_copy2_get_quota(vfs_handle_struct *handle,
2887 const struct smb_filename *smb_fname,
2888 enum SMB_QUOTA_TYPE qtype,
2892 time_t timestamp = 0;
2893 char *stripped = NULL;
2895 int saved_errno = 0;
2897 struct smb_filename *conv_smb_fname = NULL;
2899 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2906 if (timestamp == 0) {
2907 return SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname, qtype, id, dq);
2910 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2911 TALLOC_FREE(stripped);
2915 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2921 if (conv_smb_fname == NULL) {
2925 ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv_smb_fname, qtype, id, dq);
2928 saved_errno = errno;
2931 TALLOC_FREE(conv_smb_fname);
2932 if (saved_errno != 0) {
2933 errno = saved_errno;
2939 static ssize_t shadow_copy2_pwrite(vfs_handle_struct *handle,
2947 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2948 if (nwritten == -1) {
2949 if (errno == EBADF && fsp->fsp_flags.can_write) {
2957 struct shadow_copy2_pwrite_state {
2958 vfs_handle_struct *handle;
2961 struct vfs_aio_state vfs_aio_state;
2964 static void shadow_copy2_pwrite_done(struct tevent_req *subreq);
2966 static struct tevent_req *shadow_copy2_pwrite_send(
2967 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
2968 struct tevent_context *ev, struct files_struct *fsp,
2969 const void *data, size_t n, off_t offset)
2971 struct tevent_req *req = NULL, *subreq = NULL;
2972 struct shadow_copy2_pwrite_state *state = NULL;
2974 req = tevent_req_create(mem_ctx, &state,
2975 struct shadow_copy2_pwrite_state);
2979 state->handle = handle;
2982 subreq = SMB_VFS_NEXT_PWRITE_SEND(state,
2989 if (tevent_req_nomem(subreq, req)) {
2990 return tevent_req_post(req, ev);
2992 tevent_req_set_callback(subreq, shadow_copy2_pwrite_done, req);
2997 static void shadow_copy2_pwrite_done(struct tevent_req *subreq)
2999 struct tevent_req *req = tevent_req_callback_data(
3000 subreq, struct tevent_req);
3001 struct shadow_copy2_pwrite_state *state = tevent_req_data(
3002 req, struct shadow_copy2_pwrite_state);
3004 state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
3005 TALLOC_FREE(subreq);
3006 if (state->ret == -1) {
3007 tevent_req_error(req, state->vfs_aio_state.error);
3011 tevent_req_done(req);
3014 static ssize_t shadow_copy2_pwrite_recv(struct tevent_req *req,
3015 struct vfs_aio_state *vfs_aio_state)
3017 struct shadow_copy2_pwrite_state *state = tevent_req_data(
3018 req, struct shadow_copy2_pwrite_state);
3020 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3021 if ((vfs_aio_state->error == EBADF) &&
3022 state->fsp->fsp_flags.can_write)
3024 vfs_aio_state->error = EROFS;
3030 *vfs_aio_state = state->vfs_aio_state;
3034 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
3035 const char *service, const char *user)
3037 struct shadow_copy2_config *config;
3038 struct shadow_copy2_private *priv;
3040 const char *snapdir;
3041 const char *snapprefix = NULL;
3042 const char *delimiter;
3043 const char *gmt_format;
3044 const char *sort_order;
3045 const char *basedir = NULL;
3046 const char *snapsharepath = NULL;
3047 const char *mount_point;
3049 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
3050 (unsigned)handle->conn->cnum,
3051 handle->conn->connectpath));
3053 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
3058 priv = talloc_zero(handle->conn, struct shadow_copy2_private);
3060 DBG_ERR("talloc_zero() failed\n");
3065 priv->snaps = talloc_zero(priv, struct shadow_copy2_snaplist_info);
3066 if (priv->snaps == NULL) {
3067 DBG_ERR("talloc_zero() failed\n");
3072 config = talloc_zero(priv, struct shadow_copy2_config);
3073 if (config == NULL) {
3074 DEBUG(0, ("talloc_zero() failed\n"));
3079 priv->config = config;
3081 gmt_format = lp_parm_const_string(SNUM(handle->conn),
3084 config->gmt_format = talloc_strdup(config, gmt_format);
3085 if (config->gmt_format == NULL) {
3086 DEBUG(0, ("talloc_strdup() failed\n"));
3091 /* config->gmt_format must not contain a path separator. */
3092 if (strchr(config->gmt_format, '/') != NULL) {
3093 DEBUG(0, ("shadow:format %s must not contain a /"
3094 "character. Unable to initialize module.\n",
3095 config->gmt_format));
3100 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
3101 "shadow", "sscanf", false);
3103 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
3104 "shadow", "localtime",
3107 snapdir = lp_parm_const_string(SNUM(handle->conn),
3108 "shadow", "snapdir",
3110 config->snapdir = talloc_strdup(config, snapdir);
3111 if (config->snapdir == NULL) {
3112 DEBUG(0, ("talloc_strdup() failed\n"));
3117 snapprefix = lp_parm_const_string(SNUM(handle->conn),
3118 "shadow", "snapprefix",
3120 if (snapprefix != NULL) {
3121 priv->snaps->regex = talloc_zero(priv->snaps, regex_t);
3122 if (priv->snaps->regex == NULL) {
3123 DBG_ERR("talloc_zero() failed\n");
3128 /* pre-compute regex rule for matching pattern later */
3129 ret = regcomp(priv->snaps->regex, snapprefix, 0);
3131 DBG_ERR("Failed to create regex object\n");
3136 delimiter = lp_parm_const_string(SNUM(handle->conn),
3137 "shadow", "delimiter",
3139 if (delimiter != NULL) {
3140 priv->config->delimiter = talloc_strdup(priv->config, delimiter);
3141 if (priv->config->delimiter == NULL) {
3142 DBG_ERR("talloc_strdup() failed\n");
3148 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
3150 "snapdirseverywhere",
3153 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
3154 "shadow", "crossmountpoints",
3157 if (config->crossmountpoints && !config->snapdirseverywhere) {
3158 DBG_WARNING("Warning: 'crossmountpoints' depends on "
3159 "'snapdirseverywhere'. Disabling crossmountpoints.\n");
3162 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
3163 "shadow", "fixinodes",
3166 sort_order = lp_parm_const_string(SNUM(handle->conn),
3167 "shadow", "sort", "desc");
3168 config->sort_order = talloc_strdup(config, sort_order);
3169 if (config->sort_order == NULL) {
3170 DEBUG(0, ("talloc_strdup() failed\n"));
3175 mount_point = lp_parm_const_string(SNUM(handle->conn),
3176 "shadow", "mountpoint", NULL);
3177 if (mount_point != NULL) {
3178 if (mount_point[0] != '/') {
3179 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
3180 "relative ('%s'), but it has to be an "
3181 "absolute path. Ignoring provided value.\n",
3186 p = strstr(handle->conn->connectpath, mount_point);
3187 if (p != handle->conn->connectpath) {
3188 DBG_WARNING("Warning: the share root (%s) is "
3189 "not a subdirectory of the "
3190 "specified mountpoint (%s). "
3191 "Ignoring provided value.\n",
3192 handle->conn->connectpath,
3199 if (mount_point != NULL) {
3200 config->mount_point = talloc_strdup(config, mount_point);
3201 if (config->mount_point == NULL) {
3202 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
3206 config->mount_point = shadow_copy2_find_mount_point(config,
3208 if (config->mount_point == NULL) {
3209 DBG_WARNING("shadow_copy2_find_mount_point "
3210 "of the share root '%s' failed: %s\n",
3211 handle->conn->connectpath, strerror(errno));
3216 basedir = lp_parm_const_string(SNUM(handle->conn),
3217 "shadow", "basedir", NULL);
3219 if (basedir != NULL) {
3220 if (basedir[0] != '/') {
3221 DEBUG(1, (__location__ " Warning: 'basedir' is "
3222 "relative ('%s'), but it has to be an "
3223 "absolute path. Disabling basedir.\n",
3228 p = strstr(basedir, config->mount_point);
3230 DEBUG(1, ("Warning: basedir (%s) is not a "
3231 "subdirectory of the share root's "
3232 "mount point (%s). "
3233 "Disabling basedir\n",
3234 basedir, config->mount_point));
3240 if (config->snapdirseverywhere && basedir != NULL) {
3241 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
3242 "with 'snapdirseverywhere'. Disabling basedir.\n"));
3246 snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
3247 "snapsharepath", NULL);
3248 if (snapsharepath != NULL) {
3249 if (snapsharepath[0] == '/') {
3250 DBG_WARNING("Warning: 'snapsharepath' is "
3251 "absolute ('%s'), but it has to be a "
3252 "relative path. Disabling snapsharepath.\n",
3254 snapsharepath = NULL;
3256 if (config->snapdirseverywhere && snapsharepath != NULL) {
3257 DBG_WARNING("Warning: 'snapsharepath' is incompatible "
3258 "with 'snapdirseverywhere'. Disabling "
3259 "snapsharepath.\n");
3260 snapsharepath = NULL;
3264 if (basedir != NULL && snapsharepath != NULL) {
3265 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
3266 "'basedir'. Disabling snapsharepath\n");
3267 snapsharepath = NULL;
3270 if (snapsharepath != NULL) {
3271 config->rel_connectpath = talloc_strdup(config, snapsharepath);
3272 if (config->rel_connectpath == NULL) {
3273 DBG_ERR("talloc_strdup() failed\n");
3279 if (basedir == NULL) {
3280 basedir = config->mount_point;
3283 if (config->rel_connectpath == NULL &&
3284 strlen(basedir) < strlen(handle->conn->connectpath)) {
3285 config->rel_connectpath = talloc_strdup(config,
3286 handle->conn->connectpath + strlen(basedir));
3287 if (config->rel_connectpath == NULL) {
3288 DEBUG(0, ("talloc_strdup() failed\n"));
3294 if (config->snapdir[0] == '/') {
3295 config->snapdir_absolute = true;
3297 if (config->snapdirseverywhere == true) {
3298 DEBUG(1, (__location__ " Warning: An absolute snapdir "
3299 "is incompatible with 'snapdirseverywhere', "
3300 "setting 'snapdirseverywhere' to false.\n"));
3301 config->snapdirseverywhere = false;
3304 if (config->crossmountpoints == true) {
3305 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
3306 "is not supported with an absolute snapdir. "
3307 "Disabling it.\n"));
3308 config->crossmountpoints = false;
3311 config->snapshot_basepath = config->snapdir;
3313 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
3314 config->mount_point, config->snapdir);
3315 if (config->snapshot_basepath == NULL) {
3316 DEBUG(0, ("talloc_asprintf() failed\n"));
3322 trim_string(config->mount_point, NULL, "/");
3323 trim_string(config->rel_connectpath, "/", "/");
3324 trim_string(config->snapdir, NULL, "/");
3325 trim_string(config->snapshot_basepath, NULL, "/");
3327 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
3328 " share root: '%s'\n"
3329 " mountpoint: '%s'\n"
3330 " rel share root: '%s'\n"
3332 " snapprefix: '%s'\n"
3333 " delimiter: '%s'\n"
3334 " snapshot base path: '%s'\n"
3337 " snapdirs everywhere: %s\n"
3338 " cross mountpoints: %s\n"
3342 handle->conn->connectpath,
3343 config->mount_point,
3344 config->rel_connectpath,
3348 config->snapshot_basepath,
3350 config->use_sscanf ? "yes" : "no",
3351 config->snapdirseverywhere ? "yes" : "no",
3352 config->crossmountpoints ? "yes" : "no",
3353 config->fixinodes ? "yes" : "no",
3358 SMB_VFS_HANDLE_SET_DATA(handle, priv,
3359 NULL, struct shadow_copy2_private,
3365 static struct dirent *shadow_copy2_readdir(vfs_handle_struct *handle,
3366 struct files_struct *dirfsp,
3368 SMB_STRUCT_STAT *sbuf)
3370 struct shadow_copy2_private *priv = NULL;
3371 struct dirent *ent = NULL;
3372 struct smb_filename atname;
3373 struct smb_filename *full_fname = NULL;
3374 time_t timestamp = 0;
3375 char *stripped = NULL;
3377 char *abspath = NULL;
3378 bool converted = false;
3380 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
3383 ent = SMB_VFS_NEXT_READDIR(handle, dirfsp, dirp, sbuf);
3390 if (ISDOT(dirfsp->fsp_name->base_name) && ISDOTDOT(ent->d_name)) {
3394 atname = (struct smb_filename) {
3395 .base_name = ent->d_name,
3396 .twrp = dirfsp->fsp_name->twrp,
3397 .flags = dirfsp->fsp_name->flags,
3400 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
3403 if (full_fname == NULL) {
3407 if (!shadow_copy2_strip_snapshot_converted(talloc_tos(),
3413 TALLOC_FREE(full_fname);
3417 if (timestamp == 0 && !converted) {
3418 /* Not a snapshot path, no need for convert_sbuf() */
3419 TALLOC_FREE(stripped);
3420 TALLOC_FREE(full_fname);
3424 if (timestamp == 0) {
3425 abspath = make_path_absolute(talloc_tos(),
3427 full_fname->base_name);
3428 TALLOC_FREE(full_fname);
3429 if (abspath == NULL) {
3433 conv = shadow_copy2_convert(talloc_tos(),
3437 TALLOC_FREE(stripped);
3442 abspath = make_path_absolute(talloc_tos(), priv, conv);
3444 if (abspath == NULL) {
3449 convert_sbuf(handle, abspath, sbuf);
3451 TALLOC_FREE(abspath);
3455 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
3456 .connect_fn = shadow_copy2_connect,
3457 .disk_free_fn = shadow_copy2_disk_free,
3458 .get_quota_fn = shadow_copy2_get_quota,
3459 .create_dfs_pathat_fn = shadow_copy2_create_dfs_pathat,
3460 .read_dfs_pathat_fn = shadow_copy2_read_dfs_pathat,
3461 .renameat_fn = shadow_copy2_renameat,
3462 .linkat_fn = shadow_copy2_linkat,
3463 .symlinkat_fn = shadow_copy2_symlinkat,
3464 .stat_fn = shadow_copy2_stat,
3465 .lstat_fn = shadow_copy2_lstat,
3466 .fstat_fn = shadow_copy2_fstat,
3467 .fstatat_fn = shadow_copy2_fstatat,
3468 .openat_fn = shadow_copy2_openat,
3469 .unlinkat_fn = shadow_copy2_unlinkat,
3470 .fchmod_fn = shadow_copy2_fchmod,
3471 .chdir_fn = shadow_copy2_chdir,
3472 .fntimes_fn = shadow_copy2_fntimes,
3473 .readlinkat_fn = shadow_copy2_readlinkat,
3474 .mknodat_fn = shadow_copy2_mknodat,
3475 .realpath_fn = shadow_copy2_realpath,
3476 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
3477 .mkdirat_fn = shadow_copy2_mkdirat,
3478 .fsetxattr_fn = shadow_copy2_fsetxattr,
3479 .fchflags_fn = shadow_copy2_fchflags,
3480 .get_real_filename_at_fn = shadow_copy2_get_real_filename_at,
3481 .pwrite_fn = shadow_copy2_pwrite,
3482 .pwrite_send_fn = shadow_copy2_pwrite_send,
3483 .pwrite_recv_fn = shadow_copy2_pwrite_recv,
3484 .connectpath_fn = shadow_copy2_connectpath,
3485 .parent_pathname_fn = shadow_copy2_parent_pathname,
3486 .readdir_fn = shadow_copy2_readdir,
3490 NTSTATUS vfs_shadow_copy2_init(TALLOC_CTX *ctx)
3492 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
3493 "shadow_copy2", &vfs_shadow_copy2_fns);