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"
40 struct shadow_copy2_config {
46 bool snapdirseverywhere;
47 bool crossmountpoints;
50 bool snapdir_absolute;
52 char *rel_connectpath; /* share root, relative to a snapshot root */
53 char *snapshot_basepath; /* the absolute version of snapdir */
56 /* Data-structure to hold the list of snap entries */
57 struct shadow_copy2_snapentry {
60 struct shadow_copy2_snapentry *next;
61 struct shadow_copy2_snapentry *prev;
64 struct shadow_copy2_snaplist_info {
65 struct shadow_copy2_snapentry *snaplist; /* snapshot list */
66 regex_t *regex; /* Regex to filter snaps */
67 time_t fetch_time; /* snaplist update time */
72 * shadow_copy2 private structure. This structure will be
73 * used to keep module specific information
75 struct shadow_copy2_private {
76 struct shadow_copy2_config *config;
77 struct shadow_copy2_snaplist_info *snaps;
78 char *shadow_cwd; /* Absolute $cwd path. */
79 /* Absolute connectpath - can vary depending on $cwd. */
80 char *shadow_connectpath;
81 /* malloc'ed realpath return. */
82 char *shadow_realpath;
85 static int shadow_copy2_get_shadow_copy_data(
86 vfs_handle_struct *handle, files_struct *fsp,
87 struct shadow_copy_data *shadow_copy2_data,
91 *This function will create a new snapshot list entry and
92 * return to the caller. This entry will also be added to
93 * the global snapshot list.
95 * @param[in] priv shadow_copy2 specific data structure
96 * @return Newly created snapshot entry or NULL on failure
98 static struct shadow_copy2_snapentry *shadow_copy2_create_snapentry(
99 struct shadow_copy2_private *priv)
101 struct shadow_copy2_snapentry *tmpentry = NULL;
103 tmpentry = talloc_zero(priv->snaps, struct shadow_copy2_snapentry);
104 if (tmpentry == NULL) {
105 DBG_ERR("talloc_zero() failed\n");
110 DLIST_ADD(priv->snaps->snaplist, tmpentry);
116 *This function will delete the entire snaplist and reset
117 * priv->snaps->snaplist to NULL.
119 * @param[in] priv shadow_copye specific data structure
121 static void shadow_copy2_delete_snaplist(struct shadow_copy2_private *priv)
123 struct shadow_copy2_snapentry *tmp = NULL;
125 while ((tmp = priv->snaps->snaplist) != NULL) {
126 DLIST_REMOVE(priv->snaps->snaplist, tmp);
132 * Given a timestamp this function searches the global snapshot list
133 * and returns the complete snapshot directory name saved in the entry.
135 * @param[in] priv shadow_copy2 specific structure
136 * @param[in] timestamp timestamp corresponding to one of the snapshot
137 * @param[out] snap_str buffer to copy the actual snapshot name
138 * @param[in] len length of snap_str buffer
140 * @return Length of actual snapshot name, and -1 on failure
142 static ssize_t shadow_copy2_saved_snapname(struct shadow_copy2_private *priv,
143 struct tm *timestamp,
144 char *snap_str, size_t len)
146 ssize_t snaptime_len = -1;
147 struct shadow_copy2_snapentry *entry = NULL;
149 snaptime_len = strftime(snap_str, len, GMT_FORMAT, timestamp);
150 if (snaptime_len == 0) {
151 DBG_ERR("strftime failed\n");
157 for (entry = priv->snaps->snaplist; entry; entry = entry->next) {
158 if (strcmp(entry->time_fmt, snap_str) == 0) {
159 snaptime_len = snprintf(snap_str, len, "%s",
171 * This function will check if snaplist is updated or not. If snaplist
172 * is empty then it will create a new list. Each time snaplist is updated
173 * the time is recorded. If the snapshot time is greater than the snaplist
174 * update time then chances are we are working on an older list. Then discard
175 * the old list and fetch a new snaplist.
177 * @param[in] handle VFS handle struct
178 * @param[in] snap_time time of snapshot
180 * @return true if the list is updated else false
182 static bool shadow_copy2_update_snaplist(struct vfs_handle_struct *handle,
186 bool snaplist_updated = false;
187 struct files_struct fsp = {0};
188 struct smb_filename smb_fname = {0};
189 double seconds = 0.0;
190 struct shadow_copy2_private *priv = NULL;
192 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
195 seconds = difftime(snap_time, priv->snaps->fetch_time);
198 * Fetch the snapshot list if either the snaplist is empty or the
199 * required snapshot time is greater than the last fetched snaplist
202 if (seconds > 0 || (priv->snaps->snaplist == NULL)) {
203 smb_fname.base_name = discard_const_p(char, ".");
204 fsp.fsp_name = &smb_fname;
206 ret = shadow_copy2_get_shadow_copy_data(handle, &fsp,
209 snaplist_updated = true;
211 DBG_ERR("Failed to get shadow copy data\n");
216 return snaplist_updated;
219 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
221 unsigned *pnum_offsets)
223 unsigned num_offsets;
230 while ((p = strchr(p, '/')) != NULL) {
235 offsets = talloc_array(mem_ctx, size_t, num_offsets);
236 if (offsets == NULL) {
242 while ((p = strchr(p, '/')) != NULL) {
243 offsets[num_offsets] = p-str;
249 *pnum_offsets = num_offsets;
254 * Given a timestamp, build the posix level GMT-tag string
255 * based on the configurable format.
257 static ssize_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
259 char *snaptime_string,
263 ssize_t snaptime_len;
264 struct shadow_copy2_config *config;
265 struct shadow_copy2_private *priv;
267 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
270 config = priv->config;
272 if (config->use_sscanf) {
273 snaptime_len = snprintf(snaptime_string,
276 (unsigned long)snapshot);
277 if (snaptime_len <= 0) {
278 DEBUG(10, ("snprintf failed\n"));
282 if (config->use_localtime) {
283 if (localtime_r(&snapshot, &snap_tm) == 0) {
284 DEBUG(10, ("gmtime_r failed\n"));
288 if (gmtime_r(&snapshot, &snap_tm) == 0) {
289 DEBUG(10, ("gmtime_r failed\n"));
294 if (priv->snaps->regex != NULL) {
295 snaptime_len = shadow_copy2_saved_snapname(priv,
296 &snap_tm, snaptime_string, len);
297 if (snaptime_len >= 0)
301 * If we fail to find the snapshot name, chances are
302 * that we have not updated our snaplist. Make sure the
303 * snaplist is updated.
305 if (!shadow_copy2_update_snaplist(handle, snapshot)) {
306 DBG_DEBUG("shadow_copy2_update_snaplist "
311 return shadow_copy2_saved_snapname(priv,
312 &snap_tm, snaptime_string, len);
315 snaptime_len = strftime(snaptime_string,
319 if (snaptime_len == 0) {
320 DEBUG(10, ("strftime failed\n"));
329 * Given a timestamp, build the string to insert into a path
330 * as a path component for creating the local path to the
331 * snapshot at the given timestamp of the input path.
333 * In the case of a parallel snapdir (specified with an
334 * absolute path), this is the inital portion of the
335 * local path of any snapshot file. The complete path is
336 * obtained by appending the portion of the file's path
337 * below the share root's mountpoint.
339 static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
340 struct vfs_handle_struct *handle,
343 fstring snaptime_string;
344 ssize_t snaptime_len = 0;
346 struct shadow_copy2_config *config;
347 struct shadow_copy2_private *priv;
349 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
352 config = priv->config;
354 snaptime_len = shadow_copy2_posix_gmt_string(handle,
357 sizeof(snaptime_string));
358 if (snaptime_len <= 0) {
362 if (config->snapdir_absolute) {
363 result = talloc_asprintf(mem_ctx, "%s/%s",
364 config->snapdir, snaptime_string);
366 result = talloc_asprintf(mem_ctx, "/%s/%s",
367 config->snapdir, snaptime_string);
369 if (result == NULL) {
370 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
377 * Build the posix snapshot path for the connection
378 * at the given timestamp, i.e. the absolute posix path
379 * that contains the snapshot for this file system.
381 * This only applies to classical case, i.e. not
382 * to the "snapdirseverywhere" mode.
384 static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
385 struct vfs_handle_struct *handle,
388 fstring snaptime_string;
389 ssize_t snaptime_len = 0;
391 struct shadow_copy2_private *priv;
393 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
396 snaptime_len = shadow_copy2_posix_gmt_string(handle,
399 sizeof(snaptime_string));
400 if (snaptime_len <= 0) {
404 result = talloc_asprintf(mem_ctx, "%s/%s",
405 priv->config->snapshot_basepath, snaptime_string);
406 if (result == NULL) {
407 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
413 static char *make_path_absolute(TALLOC_CTX *mem_ctx,
414 struct shadow_copy2_private *priv,
417 char *newpath = NULL;
418 char *abs_path = NULL;
420 if (name[0] != '/') {
421 newpath = talloc_asprintf(mem_ctx,
425 if (newpath == NULL) {
430 abs_path = canonicalize_absolute_path(mem_ctx, name);
431 TALLOC_FREE(newpath);
435 /* Return a $cwd-relative path. */
436 static bool make_relative_path(const char *cwd, char *abs_path)
438 size_t cwd_len = strlen(cwd);
439 size_t abs_len = strlen(abs_path);
441 if (abs_len < cwd_len) {
444 if (memcmp(abs_path, cwd, cwd_len) != 0) {
447 /* The cwd_len != 1 case is for $cwd == '/' */
449 abs_path[cwd_len] != '/' &&
450 abs_path[cwd_len] != '\0')
454 if (abs_path[cwd_len] == '/') {
457 memmove(abs_path, &abs_path[cwd_len], abs_len + 1 - cwd_len);
461 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
463 char *gmt, size_t gmt_len);
466 * Check if an incoming filename is already a snapshot converted pathname.
468 * If so, it returns the pathname truncated at the snapshot point which
469 * will be used as the connectpath.
472 static int check_for_converted_path(TALLOC_CTX *mem_ctx,
473 struct vfs_handle_struct *handle,
474 struct shadow_copy2_private *priv,
476 bool *ppath_already_converted,
479 size_t snapdirlen = 0;
480 char *p = strstr_m(abs_path, priv->config->snapdir);
482 char *connect_path = NULL;
483 char snapshot[GMT_NAME_LEN+1];
485 *ppath_already_converted = false;
488 /* Must at least contain shadow:snapdir. */
492 if (priv->config->snapdir[0] == '/' &&
494 /* Absolute shadow:snapdir must be at the start. */
498 snapdirlen = strlen(priv->config->snapdir);
499 if (p[snapdirlen] != '/') {
500 /* shadow:snapdir must end as a separate component. */
504 if (p > abs_path && p[-1] != '/') {
505 /* shadow:snapdir must start as a separate component. */
510 p++; /* Move past the / */
513 * Need to return up to the next path
514 * component after the time.
515 * This will be used as the connectpath.
520 * No next path component.
523 connect_path = talloc_strdup(mem_ctx,
526 connect_path = talloc_strndup(mem_ctx,
530 if (connect_path == NULL) {
535 * Point p at the same offset in connect_path as
539 p = &connect_path[p - abs_path];
542 * Now ensure there is a time string at p.
543 * The SMB-format @GMT-token string is returned
547 if (!shadow_copy2_snapshot_to_gmt(handle,
551 TALLOC_FREE(connect_path);
555 if (pconnectpath != NULL) {
556 *pconnectpath = connect_path;
559 *ppath_already_converted = true;
561 DBG_DEBUG("path |%s| is already converted. "
562 "connect path = |%s|\n",
570 * This function does two things.
572 * 1). Checks if an incoming filename is already a
573 * snapshot converted pathname.
574 * If so, it returns the pathname truncated
575 * at the snapshot point which will be used
576 * as the connectpath, and then does an early return.
578 * 2). Checks if an incoming filename contains an
579 * SMB-layer @GMT- style timestamp.
580 * If so, it strips the timestamp, and returns
581 * both the timestamp and the stripped path
582 * (making it cwd-relative).
585 static bool shadow_copy2_strip_snapshot_internal(TALLOC_CTX *mem_ctx,
586 struct vfs_handle_struct *handle,
587 const char *orig_name,
593 time_t timestamp = 0;
596 char *stripped = NULL;
597 size_t rest_len, dst_len;
598 struct shadow_copy2_private *priv;
599 ptrdiff_t len_before_gmt;
600 const char *name = orig_name;
601 char *abs_path = NULL;
603 bool already_converted = false;
606 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
609 DEBUG(10, (__location__ ": enter path '%s'\n", name));
611 abs_path = make_path_absolute(mem_ctx, priv, name);
612 if (abs_path == NULL) {
618 DEBUG(10, (__location__ ": abs path '%s'\n", name));
620 err = check_for_converted_path(mem_ctx,
627 /* error in conversion. */
632 if (already_converted) {
637 * From here we're only looking to strip an
638 * SMB-layer @GMT- token.
641 p = strstr_m(name, "@GMT-");
643 DEBUG(11, ("@GMT not found\n"));
646 if ((p > name) && (p[-1] != '/')) {
647 /* the GMT-token does not start a path-component */
648 DEBUG(10, ("not at start, p=%p, name=%p, p[-1]=%d\n",
649 p, name, (int)p[-1]));
653 len_before_gmt = p - name;
655 q = strptime(p, GMT_FORMAT, &tm);
657 DEBUG(10, ("strptime failed\n"));
661 timestamp = timegm(&tm);
662 if (timestamp == (time_t)-1) {
663 DEBUG(10, ("timestamp==-1\n"));
668 * The name consists of only the GMT token or the GMT
669 * token is at the end of the path. XP seems to send
670 * @GMT- at the end under certain circumstances even
671 * with a path prefix.
673 if (pstripped != NULL) {
674 if (len_before_gmt > 1) {
676 * There is a path (and not only a slash)
677 * before the @GMT-. Remove the trailing
682 stripped = talloc_strndup(mem_ctx, name,
684 if (stripped == NULL) {
688 if (orig_name[0] != '/') {
689 if (make_relative_path(priv->shadow_cwd,
690 stripped) == false) {
691 DEBUG(10, (__location__ ": path '%s' "
692 "doesn't start with cwd '%s'\n",
693 stripped, priv->shadow_cwd));
699 *pstripped = stripped;
701 *ptimestamp = timestamp;
706 * It is not a complete path component, i.e. the path
707 * component continues after the gmt-token.
709 DEBUG(10, ("q[0] = %d\n", (int)q[0]));
714 rest_len = strlen(q);
715 dst_len = len_before_gmt + rest_len;
717 if (pstripped != NULL) {
718 stripped = talloc_array(mem_ctx, char, dst_len+1);
719 if (stripped == NULL) {
724 memcpy(stripped, name, len_before_gmt);
727 memcpy(stripped + len_before_gmt, q, rest_len);
729 stripped[dst_len] = '\0';
730 if (orig_name[0] != '/') {
731 if (make_relative_path(priv->shadow_cwd,
732 stripped) == false) {
733 DEBUG(10, (__location__ ": path '%s' "
734 "doesn't start with cwd '%s'\n",
735 stripped, priv->shadow_cwd));
741 *pstripped = stripped;
743 *ptimestamp = timestamp;
747 TALLOC_FREE(abs_path);
751 static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
752 struct vfs_handle_struct *handle,
753 const char *orig_name,
757 return shadow_copy2_strip_snapshot_internal(mem_ctx,
765 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
766 vfs_handle_struct *handle)
768 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
773 if (stat(path, &st) != 0) {
780 while ((p = strrchr(path, '/')) && p > path) {
782 if (stat(path, &st) != 0) {
786 if (st.st_dev != dev) {
796 * Convert from a name as handed in via the SMB layer
797 * and a timestamp into the local path of the snapshot
798 * of the provided file at the provided time.
799 * Also return the path in the snapshot corresponding
800 * to the file's share root.
802 static char *shadow_copy2_do_convert(TALLOC_CTX *mem_ctx,
803 struct vfs_handle_struct *handle,
804 const char *name, time_t timestamp,
805 size_t *snaproot_len)
807 struct smb_filename converted_fname;
809 size_t *slashes = NULL;
810 unsigned num_slashes;
814 char *converted = NULL;
815 size_t insertlen, connectlen = 0;
819 struct shadow_copy2_config *config;
820 struct shadow_copy2_private *priv;
821 size_t in_share_offset = 0;
823 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
826 config = priv->config;
828 DEBUG(10, ("converting '%s'\n", name));
830 if (!config->snapdirseverywhere) {
834 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
837 if (snapshot_path == NULL) {
841 if (config->rel_connectpath == NULL) {
842 converted = talloc_asprintf(mem_ctx, "%s/%s",
843 snapshot_path, name);
845 converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
847 config->rel_connectpath,
850 if (converted == NULL) {
854 ZERO_STRUCT(converted_fname);
855 converted_fname.base_name = converted;
857 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
858 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
860 ret, ret == 0 ? "ok" : strerror(errno)));
862 DEBUG(10, ("Found %s\n", converted));
865 if (snaproot_len != NULL) {
866 *snaproot_len = strlen(snapshot_path);
867 if (config->rel_connectpath != NULL) {
869 strlen(config->rel_connectpath) + 1;
877 /* never reached ... */
880 connectlen = strlen(handle->conn->connectpath);
882 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
884 path = talloc_asprintf(
885 mem_ctx, "%s/%s", handle->conn->connectpath, name);
891 pathlen = talloc_get_size(path)-1;
893 if (!shadow_copy2_find_slashes(talloc_tos(), path,
894 &slashes, &num_slashes)) {
898 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
899 if (insert == NULL) {
902 insertlen = talloc_get_size(insert)-1;
905 * Note: We deliberatly don't expensively initialize the
906 * array with talloc_zero here: Putting zero into
907 * converted[pathlen+insertlen] below is sufficient, because
908 * in the following for loop, the insert string is inserted
909 * at various slash places. So the memory up to position
910 * pathlen+insertlen will always be initialized when the
911 * converted string is used.
913 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
914 if (converted == NULL) {
918 if (path[pathlen-1] != '/') {
920 * Append a fake slash to find the snapshot root
923 tmp = talloc_realloc(talloc_tos(), slashes,
924 size_t, num_slashes+1);
929 slashes[num_slashes] = pathlen;
935 if (!config->crossmountpoints) {
936 min_offset = strlen(config->mount_point);
939 memcpy(converted, path, pathlen+1);
940 converted[pathlen+insertlen] = '\0';
942 ZERO_STRUCT(converted_fname);
943 converted_fname.base_name = converted;
945 for (i = num_slashes-1; i>=0; i--) {
951 if (offset < min_offset) {
956 if (offset >= connectlen) {
957 in_share_offset = offset;
960 memcpy(converted+offset, insert, insertlen);
963 memcpy(converted+offset, path + slashes[i],
964 pathlen - slashes[i]);
966 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
968 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
970 ret, ret == 0 ? "ok" : strerror(errno)));
973 if (snaproot_len != NULL) {
974 *snaproot_len = in_share_offset + insertlen;
978 if (errno == ENOTDIR) {
980 * This is a valid condition: We appended the
981 * .snaphots/@GMT.. to a file name. Just try
982 * with the upper levels.
986 if (errno != ENOENT) {
987 /* Other problem than "not found" */
996 DEBUG(10, ("Found %s\n", converted));
1003 if (result == NULL) {
1004 saved_errno = errno;
1006 TALLOC_FREE(converted);
1007 TALLOC_FREE(insert);
1008 TALLOC_FREE(slashes);
1010 if (saved_errno != 0) {
1011 errno = saved_errno;
1017 * Convert from a name as handed in via the SMB layer
1018 * and a timestamp into the local path of the snapshot
1019 * of the provided file at the provided time.
1021 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
1022 struct vfs_handle_struct *handle,
1023 const char *name, time_t timestamp)
1025 return shadow_copy2_do_convert(mem_ctx, handle, name, timestamp, NULL);
1029 modify a sbuf return to ensure that inodes in the shadow directory
1030 are different from those in the main directory
1032 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
1033 SMB_STRUCT_STAT *sbuf)
1035 struct shadow_copy2_private *priv;
1037 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1040 if (priv->config->fixinodes) {
1041 /* some snapshot systems, like GPFS, return the name
1042 device:inode for the snapshot files as the current
1043 files. That breaks the 'restore' button in the shadow copy
1044 GUI, as the client gets a sharing violation.
1046 This is a crude way of allowing both files to be
1047 open at once. It has a slight chance of inode
1048 number collision, but I can't see a better approach
1049 without significant VFS changes
1051 TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
1052 .dsize = strlen(fname) };
1055 shash = tdb_jenkins_hash(&key) & 0xFF000000;
1059 sbuf->st_ex_ino ^= shash;
1063 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
1064 const struct smb_filename *smb_fname,
1068 time_t timestamp = 0;
1069 char *stripped = NULL;
1071 int saved_errno = 0;
1073 struct smb_filename *conv_smb_fname = NULL;
1075 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1077 smb_fname->base_name,
1082 if (timestamp == 0) {
1083 return SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
1085 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1086 TALLOC_FREE(stripped);
1090 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1095 if (conv_smb_fname == NULL) {
1099 ret = SMB_VFS_NEXT_OPENDIR(handle, conv_smb_fname, mask, attr);
1101 saved_errno = errno;
1104 TALLOC_FREE(conv_smb_fname);
1105 if (saved_errno != 0) {
1106 errno = saved_errno;
1111 static int shadow_copy2_rename(vfs_handle_struct *handle,
1112 const struct smb_filename *smb_fname_src,
1113 const struct smb_filename *smb_fname_dst)
1115 time_t timestamp_src = 0;
1116 time_t timestamp_dst = 0;
1117 char *snappath_src = NULL;
1118 char *snappath_dst = NULL;
1120 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle,
1121 smb_fname_src->base_name,
1122 ×tamp_src, NULL, &snappath_src)) {
1125 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle,
1126 smb_fname_dst->base_name,
1127 ×tamp_dst, NULL, &snappath_dst)) {
1130 if (timestamp_src != 0) {
1134 if (timestamp_dst != 0) {
1139 * Don't allow rename on already converted paths.
1141 if (snappath_src != NULL) {
1145 if (snappath_dst != NULL) {
1149 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
1152 static int shadow_copy2_symlink(vfs_handle_struct *handle,
1153 const char *oldname, const char *newname)
1155 time_t timestamp_old = 0;
1156 time_t timestamp_new = 0;
1157 char *snappath_old = NULL;
1158 char *snappath_new = NULL;
1160 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle, oldname,
1161 ×tamp_old, NULL, &snappath_old)) {
1164 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle, newname,
1165 ×tamp_new, NULL, &snappath_new)) {
1168 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1173 * Don't allow symlinks on already converted paths.
1175 if ((snappath_old != NULL) || (snappath_new != NULL)) {
1179 return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
1182 static int shadow_copy2_link(vfs_handle_struct *handle,
1183 const struct smb_filename *old_smb_fname,
1184 const struct smb_filename *new_smb_fname)
1186 time_t timestamp_old = 0;
1187 time_t timestamp_new = 0;
1188 char *snappath_old = NULL;
1189 char *snappath_new = NULL;
1191 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1193 old_smb_fname->base_name,
1199 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1201 new_smb_fname->base_name,
1207 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1212 * Don't allow links on already converted paths.
1214 if ((snappath_old != NULL) || (snappath_new != NULL)) {
1218 return SMB_VFS_NEXT_LINK(handle, old_smb_fname, new_smb_fname);
1221 static int shadow_copy2_stat(vfs_handle_struct *handle,
1222 struct smb_filename *smb_fname)
1224 time_t timestamp = 0;
1225 char *stripped = NULL;
1227 int saved_errno = 0;
1230 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1231 smb_fname->base_name,
1232 ×tamp, &stripped)) {
1235 if (timestamp == 0) {
1236 return SMB_VFS_NEXT_STAT(handle, smb_fname);
1239 tmp = smb_fname->base_name;
1240 smb_fname->base_name = shadow_copy2_convert(
1241 talloc_tos(), handle, stripped, timestamp);
1242 TALLOC_FREE(stripped);
1244 if (smb_fname->base_name == NULL) {
1245 smb_fname->base_name = tmp;
1249 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1251 saved_errno = errno;
1254 TALLOC_FREE(smb_fname->base_name);
1255 smb_fname->base_name = tmp;
1258 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1260 if (saved_errno != 0) {
1261 errno = saved_errno;
1266 static int shadow_copy2_lstat(vfs_handle_struct *handle,
1267 struct smb_filename *smb_fname)
1269 time_t timestamp = 0;
1270 char *stripped = NULL;
1272 int saved_errno = 0;
1275 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1276 smb_fname->base_name,
1277 ×tamp, &stripped)) {
1280 if (timestamp == 0) {
1281 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1284 tmp = smb_fname->base_name;
1285 smb_fname->base_name = shadow_copy2_convert(
1286 talloc_tos(), handle, stripped, timestamp);
1287 TALLOC_FREE(stripped);
1289 if (smb_fname->base_name == NULL) {
1290 smb_fname->base_name = tmp;
1294 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1296 saved_errno = errno;
1299 TALLOC_FREE(smb_fname->base_name);
1300 smb_fname->base_name = tmp;
1303 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1305 if (saved_errno != 0) {
1306 errno = saved_errno;
1311 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
1312 SMB_STRUCT_STAT *sbuf)
1314 time_t timestamp = 0;
1317 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1321 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1322 fsp->fsp_name->base_name,
1323 ×tamp, NULL)) {
1326 if (timestamp != 0) {
1327 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
1332 static int shadow_copy2_open(vfs_handle_struct *handle,
1333 struct smb_filename *smb_fname, files_struct *fsp,
1334 int flags, mode_t mode)
1336 time_t timestamp = 0;
1337 char *stripped = NULL;
1339 int saved_errno = 0;
1342 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1343 smb_fname->base_name,
1344 ×tamp, &stripped)) {
1347 if (timestamp == 0) {
1348 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1351 tmp = smb_fname->base_name;
1352 smb_fname->base_name = shadow_copy2_convert(
1353 talloc_tos(), handle, stripped, timestamp);
1354 TALLOC_FREE(stripped);
1356 if (smb_fname->base_name == NULL) {
1357 smb_fname->base_name = tmp;
1361 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1363 saved_errno = errno;
1366 TALLOC_FREE(smb_fname->base_name);
1367 smb_fname->base_name = tmp;
1369 if (saved_errno != 0) {
1370 errno = saved_errno;
1375 static int shadow_copy2_unlink(vfs_handle_struct *handle,
1376 const struct smb_filename *smb_fname)
1378 time_t timestamp = 0;
1379 char *stripped = NULL;
1380 int saved_errno = 0;
1382 struct smb_filename *conv;
1384 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1385 smb_fname->base_name,
1386 ×tamp, &stripped)) {
1389 if (timestamp == 0) {
1390 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1392 conv = cp_smb_filename(talloc_tos(), smb_fname);
1397 conv->base_name = shadow_copy2_convert(
1398 conv, handle, stripped, timestamp);
1399 TALLOC_FREE(stripped);
1400 if (conv->base_name == NULL) {
1403 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
1405 saved_errno = errno;
1408 if (saved_errno != 0) {
1409 errno = saved_errno;
1414 static int shadow_copy2_chmod(vfs_handle_struct *handle,
1415 const struct smb_filename *smb_fname,
1418 time_t timestamp = 0;
1419 char *stripped = NULL;
1420 int saved_errno = 0;
1423 struct smb_filename *conv_smb_fname;
1425 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1427 smb_fname->base_name,
1432 if (timestamp == 0) {
1433 TALLOC_FREE(stripped);
1434 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1436 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1437 TALLOC_FREE(stripped);
1441 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1446 if (conv_smb_fname == NULL) {
1452 ret = SMB_VFS_NEXT_CHMOD(handle, conv_smb_fname, mode);
1454 saved_errno = errno;
1457 TALLOC_FREE(conv_smb_fname);
1458 if (saved_errno != 0) {
1459 errno = saved_errno;
1464 static int shadow_copy2_chown(vfs_handle_struct *handle,
1465 const struct smb_filename *smb_fname,
1469 time_t timestamp = 0;
1470 char *stripped = NULL;
1471 int saved_errno = 0;
1474 struct smb_filename *conv_smb_fname = NULL;
1476 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1478 smb_fname->base_name,
1483 if (timestamp == 0) {
1484 return SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
1486 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1487 TALLOC_FREE(stripped);
1491 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1496 if (conv_smb_fname == NULL) {
1501 ret = SMB_VFS_NEXT_CHOWN(handle, conv_smb_fname, uid, gid);
1503 saved_errno = errno;
1506 TALLOC_FREE(conv_smb_fname);
1507 if (saved_errno != 0) {
1508 errno = saved_errno;
1513 static void store_cwd_data(vfs_handle_struct *handle,
1514 const char *connectpath)
1516 struct shadow_copy2_private *priv = NULL;
1519 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1522 TALLOC_FREE(priv->shadow_cwd);
1523 cwd = SMB_VFS_NEXT_GETWD(handle);
1525 smb_panic("getwd failed\n");
1527 DBG_DEBUG("shadow cwd = %s\n", cwd);
1528 priv->shadow_cwd = talloc_strdup(priv, cwd);
1530 if (priv->shadow_cwd == NULL) {
1531 smb_panic("talloc failed\n");
1533 TALLOC_FREE(priv->shadow_connectpath);
1535 DBG_DEBUG("shadow conectpath = %s\n", connectpath);
1536 priv->shadow_connectpath = talloc_strdup(priv, connectpath);
1537 if (priv->shadow_connectpath == NULL) {
1538 smb_panic("talloc failed\n");
1543 static int shadow_copy2_chdir(vfs_handle_struct *handle,
1546 time_t timestamp = 0;
1547 char *stripped = NULL;
1548 char *snappath = NULL;
1550 int saved_errno = 0;
1552 size_t rootpath_len = 0;
1554 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle, fname,
1555 ×tamp, &stripped, &snappath)) {
1558 if (stripped != NULL) {
1559 conv = shadow_copy2_do_convert(talloc_tos(),
1564 TALLOC_FREE(stripped);
1571 ret = SMB_VFS_NEXT_CHDIR(handle, fname);
1573 saved_errno = errno;
1577 if (conv != NULL && rootpath_len != 0) {
1578 conv[rootpath_len] = '\0';
1579 } else if (snappath != 0) {
1583 store_cwd_data(handle, conv);
1586 TALLOC_FREE(stripped);
1589 if (saved_errno != 0) {
1590 errno = saved_errno;
1595 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
1596 const struct smb_filename *smb_fname,
1597 struct smb_file_time *ft)
1599 time_t timestamp = 0;
1600 char *stripped = NULL;
1601 int saved_errno = 0;
1603 struct smb_filename *conv;
1605 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1606 smb_fname->base_name,
1607 ×tamp, &stripped)) {
1610 if (timestamp == 0) {
1611 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1613 conv = cp_smb_filename(talloc_tos(), smb_fname);
1618 conv->base_name = shadow_copy2_convert(
1619 conv, handle, stripped, timestamp);
1620 TALLOC_FREE(stripped);
1621 if (conv->base_name == NULL) {
1624 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
1626 saved_errno = errno;
1629 if (saved_errno != 0) {
1630 errno = saved_errno;
1635 static int shadow_copy2_readlink(vfs_handle_struct *handle,
1636 const struct smb_filename *smb_fname,
1640 time_t timestamp = 0;
1641 char *stripped = NULL;
1642 int saved_errno = 0;
1644 struct smb_filename *conv = NULL;
1646 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1647 smb_fname->base_name,
1648 ×tamp, &stripped)) {
1651 if (timestamp == 0) {
1652 return SMB_VFS_NEXT_READLINK(handle, smb_fname, buf, bufsiz);
1654 conv = cp_smb_filename(talloc_tos(), smb_fname);
1656 TALLOC_FREE(stripped);
1660 conv->base_name = shadow_copy2_convert(
1661 conv, handle, stripped, timestamp);
1662 TALLOC_FREE(stripped);
1663 if (conv->base_name == NULL) {
1666 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1668 saved_errno = errno;
1671 if (saved_errno != 0) {
1672 errno = saved_errno;
1677 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1678 const struct smb_filename *smb_fname,
1682 time_t timestamp = 0;
1683 char *stripped = NULL;
1684 int saved_errno = 0;
1686 struct smb_filename *conv = NULL;
1688 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1689 smb_fname->base_name,
1690 ×tamp, &stripped)) {
1693 if (timestamp == 0) {
1694 return SMB_VFS_NEXT_MKNOD(handle, smb_fname, mode, dev);
1696 conv = cp_smb_filename(talloc_tos(), smb_fname);
1701 conv->base_name = shadow_copy2_convert(
1702 conv, handle, stripped, timestamp);
1703 TALLOC_FREE(stripped);
1704 if (conv->base_name == NULL) {
1707 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1709 saved_errno = errno;
1712 if (saved_errno != 0) {
1713 errno = saved_errno;
1718 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
1721 time_t timestamp = 0;
1722 char *stripped = NULL;
1724 char *result = NULL;
1725 int saved_errno = 0;
1727 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1728 ×tamp, &stripped)) {
1731 if (timestamp == 0) {
1732 return SMB_VFS_NEXT_REALPATH(handle, fname);
1735 tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1740 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1743 if (result == NULL) {
1744 saved_errno = errno;
1747 TALLOC_FREE(stripped);
1748 if (saved_errno != 0) {
1749 errno = saved_errno;
1755 * Check whether a given directory contains a
1756 * snapshot directory as direct subdirectory.
1757 * If yes, return the path of the snapshot-subdir,
1758 * otherwise return NULL.
1760 static char *have_snapdir(struct vfs_handle_struct *handle,
1763 struct smb_filename smb_fname;
1765 struct shadow_copy2_private *priv;
1767 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1770 ZERO_STRUCT(smb_fname);
1771 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1772 path, priv->config->snapdir);
1773 if (smb_fname.base_name == NULL) {
1777 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1778 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1779 return smb_fname.base_name;
1781 TALLOC_FREE(smb_fname.base_name);
1785 static bool check_access_snapdir(struct vfs_handle_struct *handle,
1788 struct smb_filename smb_fname;
1792 ZERO_STRUCT(smb_fname);
1793 smb_fname.base_name = talloc_asprintf(talloc_tos(),
1796 if (smb_fname.base_name == NULL) {
1800 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1801 if (ret != 0 || !S_ISDIR(smb_fname.st.st_ex_mode)) {
1802 TALLOC_FREE(smb_fname.base_name);
1806 status = smbd_check_access_rights(handle->conn,
1810 if (!NT_STATUS_IS_OK(status)) {
1811 DEBUG(0,("user does not have list permission "
1813 smb_fname.base_name));
1814 TALLOC_FREE(smb_fname.base_name);
1817 TALLOC_FREE(smb_fname.base_name);
1822 * Find the snapshot directory (if any) for the given
1823 * filename (which is relative to the share).
1825 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1826 struct vfs_handle_struct *handle,
1827 struct smb_filename *smb_fname)
1830 const char *snapdir;
1831 struct shadow_copy2_config *config;
1832 struct shadow_copy2_private *priv;
1834 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1837 config = priv->config;
1840 * If the non-snapdisrseverywhere mode, we should not search!
1842 if (!config->snapdirseverywhere) {
1843 return config->snapshot_basepath;
1846 path = talloc_asprintf(mem_ctx, "%s/%s",
1847 handle->conn->connectpath,
1848 smb_fname->base_name);
1853 snapdir = have_snapdir(handle, path);
1854 if (snapdir != NULL) {
1859 while ((p = strrchr(path, '/')) && (p > path)) {
1863 snapdir = have_snapdir(handle, path);
1864 if (snapdir != NULL) {
1873 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1875 char *gmt, size_t gmt_len)
1877 struct tm timestamp;
1879 unsigned long int timestamp_long;
1881 struct shadow_copy2_config *config;
1882 struct shadow_copy2_private *priv;
1883 char *tmpstr = NULL;
1885 bool converted = false;
1888 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1891 config = priv->config;
1893 fmt = config->gmt_format;
1896 * If regex is provided, then we will have to parse the
1897 * filename which will contain both the prefix and the time format.
1898 * e.g. <prefix><delimiter><time_format>
1900 if (priv->snaps->regex != NULL) {
1901 tmpstr = talloc_strdup(talloc_tos(), name);
1902 /* point "name" to the time format */
1903 name = strstr(name, priv->config->delimiter);
1907 /* Extract the prefix */
1908 tmp = strstr(tmpstr, priv->config->delimiter);
1915 ret = regexec(priv->snaps->regex, tmpstr, 0, NULL, 0);
1917 DBG_DEBUG("shadow_copy2_snapshot_to_gmt: "
1918 "no regex match for %s\n", tmpstr);
1923 ZERO_STRUCT(timestamp);
1924 if (config->use_sscanf) {
1925 if (sscanf(name, fmt, ×tamp_long) != 1) {
1926 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1927 "no sscanf match %s: %s\n",
1931 timestamp_t = timestamp_long;
1932 gmtime_r(×tamp_t, ×tamp);
1934 if (strptime(name, fmt, ×tamp) == NULL) {
1935 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1936 "no match %s: %s\n",
1940 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1943 if (config->use_localtime) {
1944 timestamp.tm_isdst = -1;
1945 timestamp_t = mktime(×tamp);
1946 gmtime_r(×tamp_t, ×tamp);
1950 strftime(gmt, gmt_len, GMT_FORMAT, ×tamp);
1954 TALLOC_FREE(tmpstr);
1958 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1960 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1963 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1965 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1969 sort the shadow copy data in ascending or descending order
1971 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1972 struct shadow_copy_data *shadow_copy2_data)
1974 int (*cmpfunc)(const void *, const void *);
1976 struct shadow_copy2_private *priv;
1978 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1981 sort = priv->config->sort_order;
1986 if (strcmp(sort, "asc") == 0) {
1987 cmpfunc = shadow_copy2_label_cmp_asc;
1988 } else if (strcmp(sort, "desc") == 0) {
1989 cmpfunc = shadow_copy2_label_cmp_desc;
1994 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1995 shadow_copy2_data->labels)
1997 TYPESAFE_QSORT(shadow_copy2_data->labels,
1998 shadow_copy2_data->num_volumes,
2003 static int shadow_copy2_get_shadow_copy_data(
2004 vfs_handle_struct *handle, files_struct *fsp,
2005 struct shadow_copy_data *shadow_copy2_data,
2009 const char *snapdir;
2010 struct smb_filename *snapdir_smb_fname = NULL;
2012 TALLOC_CTX *tmp_ctx = talloc_stackframe();
2013 struct shadow_copy2_private *priv = NULL;
2014 struct shadow_copy2_snapentry *tmpentry = NULL;
2015 bool get_snaplist = false;
2016 bool access_granted = false;
2019 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
2020 if (snapdir == NULL) {
2021 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
2022 handle->conn->connectpath));
2027 access_granted = check_access_snapdir(handle, snapdir);
2028 if (!access_granted) {
2029 DEBUG(0,("access denied on listing snapdir %s\n", snapdir));
2034 snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
2038 fsp->fsp_name->flags);
2039 if (snapdir_smb_fname == NULL) {
2044 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir_smb_fname, NULL, 0);
2047 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
2048 " - %s\n", snapdir, strerror(errno)));
2053 if (shadow_copy2_data != NULL) {
2054 shadow_copy2_data->num_volumes = 0;
2055 shadow_copy2_data->labels = NULL;
2058 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2062 * Normally this function is called twice once with labels = false and
2063 * then with labels = true. When labels is false it will return the
2064 * number of volumes so that the caller can allocate memory for that
2065 * many labels. Therefore to eliminate snaplist both the times it is
2066 * good to check if labels is set or not.
2068 * shadow_copy2_data is NULL when we only want to update the list and
2069 * don't want any labels.
2071 if ((priv->snaps->regex != NULL) && (labels || shadow_copy2_data == NULL)) {
2072 get_snaplist = true;
2073 /* Reset the global snaplist */
2074 shadow_copy2_delete_snaplist(priv);
2076 /* Set the current time as snaplist update time */
2077 time(&(priv->snaps->fetch_time));
2080 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
2081 char snapshot[GMT_NAME_LEN+1];
2082 SHADOW_COPY_LABEL *tlabels;
2085 * ignore names not of the right form in the snapshot
2088 if (!shadow_copy2_snapshot_to_gmt(
2090 snapshot, sizeof(snapshot))) {
2092 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
2093 "ignoring %s\n", d->d_name));
2096 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
2097 d->d_name, snapshot));
2101 * Create a snap entry for each successful
2104 tmpentry = shadow_copy2_create_snapentry(priv);
2105 if (tmpentry == NULL) {
2106 DBG_ERR("talloc_zero() failed\n");
2109 tmpentry->snapname = talloc_strdup(tmpentry, d->d_name);
2110 tmpentry->time_fmt = talloc_strdup(tmpentry, snapshot);
2113 if (shadow_copy2_data == NULL) {
2118 /* the caller doesn't want the labels */
2119 shadow_copy2_data->num_volumes++;
2123 tlabels = talloc_realloc(shadow_copy2_data,
2124 shadow_copy2_data->labels,
2126 shadow_copy2_data->num_volumes+1);
2127 if (tlabels == NULL) {
2128 DEBUG(0,("shadow_copy2: out of memory\n"));
2129 SMB_VFS_NEXT_CLOSEDIR(handle, p);
2133 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
2136 shadow_copy2_data->num_volumes++;
2137 shadow_copy2_data->labels = tlabels;
2140 SMB_VFS_NEXT_CLOSEDIR(handle,p);
2142 shadow_copy2_sort_data(handle, shadow_copy2_data);
2146 TALLOC_FREE(tmp_ctx);
2150 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
2151 struct files_struct *fsp,
2152 uint32_t security_info,
2153 TALLOC_CTX *mem_ctx,
2154 struct security_descriptor **ppdesc)
2156 time_t timestamp = 0;
2157 char *stripped = NULL;
2160 struct smb_filename *smb_fname = NULL;
2162 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
2163 fsp->fsp_name->base_name,
2164 ×tamp, &stripped)) {
2165 return map_nt_error_from_unix(errno);
2167 if (timestamp == 0) {
2168 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
2172 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2173 TALLOC_FREE(stripped);
2175 return map_nt_error_from_unix(errno);
2177 smb_fname = synthetic_smb_fname(talloc_tos(),
2181 fsp->fsp_name->flags);
2182 if (smb_fname == NULL) {
2184 return NT_STATUS_NO_MEMORY;
2187 status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
2190 TALLOC_FREE(smb_fname);
2194 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
2195 const struct smb_filename *smb_fname,
2196 uint32_t security_info,
2197 TALLOC_CTX *mem_ctx,
2198 struct security_descriptor **ppdesc)
2200 time_t timestamp = 0;
2201 char *stripped = NULL;
2204 struct smb_filename *conv_smb_fname = NULL;
2206 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2208 smb_fname->base_name,
2211 return map_nt_error_from_unix(errno);
2213 if (timestamp == 0) {
2214 return SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
2217 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2218 TALLOC_FREE(stripped);
2220 return map_nt_error_from_unix(errno);
2222 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2227 if (conv_smb_fname == NULL) {
2229 return NT_STATUS_NO_MEMORY;
2231 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv_smb_fname, security_info,
2234 TALLOC_FREE(conv_smb_fname);
2238 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
2239 const struct smb_filename *smb_fname,
2242 time_t timestamp = 0;
2243 char *stripped = NULL;
2244 int saved_errno = 0;
2247 struct smb_filename *conv_smb_fname = NULL;
2249 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2251 smb_fname->base_name,
2256 if (timestamp == 0) {
2257 return SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
2259 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2260 TALLOC_FREE(stripped);
2264 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2269 if (conv_smb_fname == NULL) {
2273 ret = SMB_VFS_NEXT_MKDIR(handle, conv_smb_fname, mode);
2275 saved_errno = errno;
2278 TALLOC_FREE(conv_smb_fname);
2279 if (saved_errno != 0) {
2280 errno = saved_errno;
2285 static int shadow_copy2_rmdir(vfs_handle_struct *handle,
2286 const struct smb_filename *smb_fname)
2288 time_t timestamp = 0;
2289 char *stripped = NULL;
2290 int saved_errno = 0;
2293 struct smb_filename *conv_smb_fname = NULL;
2295 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2297 smb_fname->base_name,
2302 if (timestamp == 0) {
2303 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
2305 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2306 TALLOC_FREE(stripped);
2310 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2315 if (conv_smb_fname == NULL) {
2319 ret = SMB_VFS_NEXT_RMDIR(handle, conv_smb_fname);
2321 saved_errno = errno;
2323 TALLOC_FREE(conv_smb_fname);
2325 if (saved_errno != 0) {
2326 errno = saved_errno;
2331 static int shadow_copy2_chflags(vfs_handle_struct *handle,
2332 const struct smb_filename *smb_fname,
2335 time_t timestamp = 0;
2336 char *stripped = NULL;
2337 int saved_errno = 0;
2340 struct smb_filename *conv_smb_fname = NULL;
2342 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2344 smb_fname->base_name,
2349 if (timestamp == 0) {
2350 return SMB_VFS_NEXT_CHFLAGS(handle, smb_fname, flags);
2352 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2353 TALLOC_FREE(stripped);
2357 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2362 if (conv_smb_fname == NULL) {
2366 ret = SMB_VFS_NEXT_CHFLAGS(handle, smb_fname, flags);
2368 saved_errno = errno;
2370 TALLOC_FREE(conv_smb_fname);
2372 if (saved_errno != 0) {
2373 errno = saved_errno;
2378 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
2379 const struct smb_filename *smb_fname,
2384 time_t timestamp = 0;
2385 char *stripped = NULL;
2387 int saved_errno = 0;
2389 struct smb_filename *conv_smb_fname = NULL;
2391 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2393 smb_fname->base_name,
2398 if (timestamp == 0) {
2399 return SMB_VFS_NEXT_GETXATTR(handle, smb_fname, aname, value,
2402 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2403 TALLOC_FREE(stripped);
2408 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2413 if (conv_smb_fname == NULL) {
2418 ret = SMB_VFS_NEXT_GETXATTR(handle, conv_smb_fname, aname, value, size);
2420 saved_errno = errno;
2422 TALLOC_FREE(conv_smb_fname);
2424 if (saved_errno != 0) {
2425 errno = saved_errno;
2430 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
2431 const struct smb_filename *smb_fname,
2432 char *list, size_t size)
2434 time_t timestamp = 0;
2435 char *stripped = NULL;
2437 int saved_errno = 0;
2439 struct smb_filename *conv_smb_fname = NULL;
2441 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2443 smb_fname->base_name,
2448 if (timestamp == 0) {
2449 return SMB_VFS_NEXT_LISTXATTR(handle, smb_fname, list, size);
2451 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2452 TALLOC_FREE(stripped);
2456 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2461 if (conv_smb_fname == NULL) {
2465 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv_smb_fname, list, size);
2467 saved_errno = errno;
2469 TALLOC_FREE(conv_smb_fname);
2471 if (saved_errno != 0) {
2472 errno = saved_errno;
2477 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
2478 const struct smb_filename *smb_fname,
2481 time_t timestamp = 0;
2482 char *stripped = NULL;
2483 int saved_errno = 0;
2486 struct smb_filename *conv_smb_fname = NULL;
2488 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2490 smb_fname->base_name,
2495 if (timestamp == 0) {
2496 return SMB_VFS_NEXT_REMOVEXATTR(handle, smb_fname, aname);
2498 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2499 TALLOC_FREE(stripped);
2503 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2508 if (conv_smb_fname == NULL) {
2512 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv_smb_fname, aname);
2514 saved_errno = errno;
2516 TALLOC_FREE(conv_smb_fname);
2518 if (saved_errno != 0) {
2519 errno = saved_errno;
2524 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
2525 const struct smb_filename *smb_fname,
2526 const char *aname, const void *value,
2527 size_t size, int flags)
2529 time_t timestamp = 0;
2530 char *stripped = NULL;
2532 int saved_errno = 0;
2534 struct smb_filename *conv_smb_fname = NULL;
2536 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2538 smb_fname->base_name,
2543 if (timestamp == 0) {
2544 return SMB_VFS_NEXT_SETXATTR(handle, smb_fname,
2545 aname, value, size, flags);
2547 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2548 TALLOC_FREE(stripped);
2552 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2557 if (conv_smb_fname == NULL) {
2561 ret = SMB_VFS_NEXT_SETXATTR(handle, conv_smb_fname,
2562 aname, value, size, flags);
2564 saved_errno = errno;
2566 TALLOC_FREE(conv_smb_fname);
2568 if (saved_errno != 0) {
2569 errno = saved_errno;
2574 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
2575 const struct smb_filename *smb_fname,
2578 time_t timestamp = 0;
2579 char *stripped = NULL;
2581 int saved_errno = 0;
2583 struct smb_filename *conv_smb_fname = NULL;
2585 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2587 smb_fname->base_name,
2592 if (timestamp == 0) {
2593 return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
2595 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2596 TALLOC_FREE(stripped);
2600 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2605 if (conv_smb_fname == NULL) {
2610 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv_smb_fname, mode);
2612 saved_errno = errno;
2615 TALLOC_FREE(conv_smb_fname);
2616 if (saved_errno != 0) {
2617 errno = saved_errno;
2622 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
2625 TALLOC_CTX *mem_ctx,
2628 time_t timestamp = 0;
2629 char *stripped = NULL;
2631 int saved_errno = 0;
2634 DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
2635 "name=[%s]\n", path, name));
2637 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
2638 ×tamp, &stripped)) {
2639 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
2642 if (timestamp == 0) {
2643 DEBUG(10, ("timestamp == 0\n"));
2644 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
2645 mem_ctx, found_name);
2647 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2648 TALLOC_FREE(stripped);
2650 DEBUG(10, ("shadow_copy2_convert failed\n"));
2653 DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
2654 "name=[%s]\n", conv, name));
2655 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
2656 mem_ctx, found_name);
2657 DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
2659 saved_errno = errno;
2662 if (saved_errno != 0) {
2663 errno = saved_errno;
2668 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
2671 time_t timestamp = 0;
2672 char *stripped = NULL;
2674 char *result = NULL;
2675 char *parent_dir = NULL;
2676 int saved_errno = 0;
2677 size_t rootpath_len = 0;
2678 struct shadow_copy2_private *priv = NULL;
2680 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2683 DBG_DEBUG("Calc connect path for [%s]\n", fname);
2685 if (priv->shadow_connectpath != NULL) {
2686 DBG_DEBUG("cached connect path is [%s]\n",
2687 priv->shadow_connectpath);
2688 return priv->shadow_connectpath;
2691 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2692 ×tamp, &stripped)) {
2695 if (timestamp == 0) {
2696 return SMB_VFS_NEXT_CONNECTPATH(handle, fname);
2699 tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
2702 if (errno != ENOENT) {
2707 * If the converted path does not exist, and converting
2708 * the parent yields something that does exist, then
2709 * this path refers to something that has not been
2710 * created yet, relative to the parent path.
2711 * The snapshot finding is relative to the parent.
2712 * (usually snapshots are read/only but this is not
2713 * necessarily true).
2714 * This code also covers getting a wildcard in the
2715 * last component, because this function is called
2716 * prior to sanitizing the path, and in SMB1 we may
2717 * get wildcards in path names.
2719 if (!parent_dirname(talloc_tos(), stripped, &parent_dir,
2725 tmp = shadow_copy2_do_convert(talloc_tos(), handle, parent_dir,
2726 timestamp, &rootpath_len);
2732 DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
2733 (int)rootpath_len, tmp);
2735 tmp[rootpath_len] = '\0';
2736 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
2737 if (result == NULL) {
2742 * SMB_VFS_NEXT_REALPATH returns a malloc'ed string.
2743 * Don't leak memory.
2745 SAFE_FREE(priv->shadow_realpath);
2746 priv->shadow_realpath = result;
2748 DBG_DEBUG("connect path is [%s]\n", result);
2751 if (result == NULL) {
2752 saved_errno = errno;
2755 TALLOC_FREE(stripped);
2756 TALLOC_FREE(parent_dir);
2757 if (saved_errno != 0) {
2758 errno = saved_errno;
2763 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
2764 const struct smb_filename *smb_fname,
2769 time_t timestamp = 0;
2770 char *stripped = NULL;
2771 int saved_errno = 0;
2773 struct smb_filename *conv_smb_fname = NULL;
2774 uint64_t ret = (uint64_t)-1;
2776 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2778 smb_fname->base_name,
2781 return (uint64_t)-1;
2783 if (timestamp == 0) {
2784 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2785 bsize, dfree, dsize);
2787 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2788 TALLOC_FREE(stripped);
2790 return (uint64_t)-1;
2792 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2797 if (conv_smb_fname == NULL) {
2799 return (uint64_t)-1;
2801 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv_smb_fname,
2802 bsize, dfree, dsize);
2803 if (ret == (uint64_t)-1) {
2804 saved_errno = errno;
2807 TALLOC_FREE(conv_smb_fname);
2808 if (saved_errno != 0) {
2809 errno = saved_errno;
2814 static int shadow_copy2_get_quota(vfs_handle_struct *handle,
2815 const struct smb_filename *smb_fname,
2816 enum SMB_QUOTA_TYPE qtype,
2820 time_t timestamp = 0;
2821 char *stripped = NULL;
2823 int saved_errno = 0;
2825 struct smb_filename *conv_smb_fname = NULL;
2827 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2829 smb_fname->base_name,
2834 if (timestamp == 0) {
2835 return SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname, qtype, id, dq);
2838 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2839 TALLOC_FREE(stripped);
2843 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2848 if (conv_smb_fname == NULL) {
2852 ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv_smb_fname, qtype, id, dq);
2855 saved_errno = errno;
2858 TALLOC_FREE(conv_smb_fname);
2859 if (saved_errno != 0) {
2860 errno = saved_errno;
2866 static int shadow_copy2_private_destructor(struct shadow_copy2_private *priv)
2868 SAFE_FREE(priv->shadow_realpath);
2872 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
2873 const char *service, const char *user)
2875 struct shadow_copy2_config *config;
2876 struct shadow_copy2_private *priv;
2878 const char *snapdir;
2879 const char *snapprefix = NULL;
2880 const char *delimiter;
2881 const char *gmt_format;
2882 const char *sort_order;
2883 const char *basedir = NULL;
2884 const char *snapsharepath = NULL;
2885 const char *mount_point;
2887 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
2888 (unsigned)handle->conn->cnum,
2889 handle->conn->connectpath));
2891 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
2896 priv = talloc_zero(handle->conn, struct shadow_copy2_private);
2898 DBG_ERR("talloc_zero() failed\n");
2903 talloc_set_destructor(priv, shadow_copy2_private_destructor);
2905 priv->snaps = talloc_zero(priv, struct shadow_copy2_snaplist_info);
2906 if (priv->snaps == NULL) {
2907 DBG_ERR("talloc_zero() failed\n");
2912 config = talloc_zero(priv, struct shadow_copy2_config);
2913 if (config == NULL) {
2914 DEBUG(0, ("talloc_zero() failed\n"));
2919 priv->config = config;
2921 gmt_format = lp_parm_const_string(SNUM(handle->conn),
2924 config->gmt_format = talloc_strdup(config, gmt_format);
2925 if (config->gmt_format == NULL) {
2926 DEBUG(0, ("talloc_strdup() failed\n"));
2931 /* config->gmt_format must not contain a path separator. */
2932 if (strchr(config->gmt_format, '/') != NULL) {
2933 DEBUG(0, ("shadow:format %s must not contain a /"
2934 "character. Unable to initialize module.\n",
2935 config->gmt_format));
2940 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
2941 "shadow", "sscanf", false);
2943 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
2944 "shadow", "localtime",
2947 snapdir = lp_parm_const_string(SNUM(handle->conn),
2948 "shadow", "snapdir",
2950 config->snapdir = talloc_strdup(config, snapdir);
2951 if (config->snapdir == NULL) {
2952 DEBUG(0, ("talloc_strdup() failed\n"));
2957 snapprefix = lp_parm_const_string(SNUM(handle->conn),
2958 "shadow", "snapprefix",
2960 if (snapprefix != NULL) {
2961 priv->snaps->regex = talloc_zero(priv->snaps, regex_t);
2962 if (priv->snaps->regex == NULL) {
2963 DBG_ERR("talloc_zero() failed\n");
2968 /* pre-compute regex rule for matching pattern later */
2969 ret = regcomp(priv->snaps->regex, snapprefix, 0);
2971 DBG_ERR("Failed to create regex object\n");
2976 delimiter = lp_parm_const_string(SNUM(handle->conn),
2977 "shadow", "delimiter",
2979 if (delimiter != NULL) {
2980 priv->config->delimiter = talloc_strdup(priv->config, delimiter);
2981 if (priv->config->delimiter == NULL) {
2982 DBG_ERR("talloc_strdup() failed\n");
2988 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
2990 "snapdirseverywhere",
2993 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
2994 "shadow", "crossmountpoints",
2997 if (config->crossmountpoints && !config->snapdirseverywhere) {
2998 DBG_WARNING("Warning: 'crossmountpoints' depends on "
2999 "'snapdirseverywhere'. Disabling crossmountpoints.\n");
3002 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
3003 "shadow", "fixinodes",
3006 sort_order = lp_parm_const_string(SNUM(handle->conn),
3007 "shadow", "sort", "desc");
3008 config->sort_order = talloc_strdup(config, sort_order);
3009 if (config->sort_order == NULL) {
3010 DEBUG(0, ("talloc_strdup() failed\n"));
3015 mount_point = lp_parm_const_string(SNUM(handle->conn),
3016 "shadow", "mountpoint", NULL);
3017 if (mount_point != NULL) {
3018 if (mount_point[0] != '/') {
3019 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
3020 "relative ('%s'), but it has to be an "
3021 "absolute path. Ignoring provided value.\n",
3026 p = strstr(handle->conn->connectpath, mount_point);
3027 if (p != handle->conn->connectpath) {
3028 DBG_WARNING("Warning: the share root (%s) is "
3029 "not a subdirectory of the "
3030 "specified mountpoint (%s). "
3031 "Ignoring provided value.\n",
3032 handle->conn->connectpath,
3039 if (mount_point != NULL) {
3040 config->mount_point = talloc_strdup(config, mount_point);
3041 if (config->mount_point == NULL) {
3042 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
3046 config->mount_point = shadow_copy2_find_mount_point(config,
3048 if (config->mount_point == NULL) {
3049 DBG_WARNING("shadow_copy2_find_mount_point "
3050 "of the share root '%s' failed: %s\n",
3051 handle->conn->connectpath, strerror(errno));
3056 basedir = lp_parm_const_string(SNUM(handle->conn),
3057 "shadow", "basedir", NULL);
3059 if (basedir != NULL) {
3060 if (basedir[0] != '/') {
3061 DEBUG(1, (__location__ " Warning: 'basedir' is "
3062 "relative ('%s'), but it has to be an "
3063 "absolute path. Disabling basedir.\n",
3068 p = strstr(basedir, config->mount_point);
3070 DEBUG(1, ("Warning: basedir (%s) is not a "
3071 "subdirectory of the share root's "
3072 "mount point (%s). "
3073 "Disabling basedir\n",
3074 basedir, config->mount_point));
3080 if (config->snapdirseverywhere && basedir != NULL) {
3081 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
3082 "with 'snapdirseverywhere'. Disabling basedir.\n"));
3086 snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
3087 "snapsharepath", NULL);
3088 if (snapsharepath != NULL) {
3089 if (snapsharepath[0] == '/') {
3090 DBG_WARNING("Warning: 'snapsharepath' is "
3091 "absolute ('%s'), but it has to be a "
3092 "relative path. Disabling snapsharepath.\n",
3094 snapsharepath = NULL;
3096 if (config->snapdirseverywhere && snapsharepath != NULL) {
3097 DBG_WARNING("Warning: 'snapsharepath' is incompatible "
3098 "with 'snapdirseverywhere'. Disabling "
3099 "snapsharepath.\n");
3100 snapsharepath = NULL;
3104 if (basedir != NULL && snapsharepath != NULL) {
3105 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
3106 "'basedir'. Disabling snapsharepath\n");
3107 snapsharepath = NULL;
3110 if (snapsharepath != NULL) {
3111 config->rel_connectpath = talloc_strdup(config, snapsharepath);
3112 if (config->rel_connectpath == NULL) {
3113 DBG_ERR("talloc_strdup() failed\n");
3119 if (basedir == NULL) {
3120 basedir = config->mount_point;
3123 if (config->rel_connectpath == NULL &&
3124 strlen(basedir) < strlen(handle->conn->connectpath)) {
3125 config->rel_connectpath = talloc_strdup(config,
3126 handle->conn->connectpath + strlen(basedir));
3127 if (config->rel_connectpath == NULL) {
3128 DEBUG(0, ("talloc_strdup() failed\n"));
3134 if (config->snapdir[0] == '/') {
3135 config->snapdir_absolute = true;
3137 if (config->snapdirseverywhere == true) {
3138 DEBUG(1, (__location__ " Warning: An absolute snapdir "
3139 "is incompatible with 'snapdirseverywhere', "
3140 "setting 'snapdirseverywhere' to false.\n"));
3141 config->snapdirseverywhere = false;
3144 if (config->crossmountpoints == true) {
3145 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
3146 "is not supported with an absolute snapdir. "
3147 "Disabling it.\n"));
3148 config->crossmountpoints = false;
3151 config->snapshot_basepath = config->snapdir;
3153 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
3154 config->mount_point, config->snapdir);
3155 if (config->snapshot_basepath == NULL) {
3156 DEBUG(0, ("talloc_asprintf() failed\n"));
3162 trim_string(config->mount_point, NULL, "/");
3163 trim_string(config->rel_connectpath, "/", "/");
3164 trim_string(config->snapdir, NULL, "/");
3165 trim_string(config->snapshot_basepath, NULL, "/");
3167 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
3168 " share root: '%s'\n"
3169 " mountpoint: '%s'\n"
3170 " rel share root: '%s'\n"
3172 " snapprefix: '%s'\n"
3173 " delimiter: '%s'\n"
3174 " snapshot base path: '%s'\n"
3177 " snapdirs everywhere: %s\n"
3178 " cross mountpoints: %s\n"
3182 handle->conn->connectpath,
3183 config->mount_point,
3184 config->rel_connectpath,
3188 config->snapshot_basepath,
3190 config->use_sscanf ? "yes" : "no",
3191 config->snapdirseverywhere ? "yes" : "no",
3192 config->crossmountpoints ? "yes" : "no",
3193 config->fixinodes ? "yes" : "no",
3198 SMB_VFS_HANDLE_SET_DATA(handle, priv,
3199 NULL, struct shadow_copy2_private,
3205 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
3206 .connect_fn = shadow_copy2_connect,
3207 .opendir_fn = shadow_copy2_opendir,
3208 .disk_free_fn = shadow_copy2_disk_free,
3209 .get_quota_fn = shadow_copy2_get_quota,
3210 .rename_fn = shadow_copy2_rename,
3211 .link_fn = shadow_copy2_link,
3212 .symlink_fn = shadow_copy2_symlink,
3213 .stat_fn = shadow_copy2_stat,
3214 .lstat_fn = shadow_copy2_lstat,
3215 .fstat_fn = shadow_copy2_fstat,
3216 .open_fn = shadow_copy2_open,
3217 .unlink_fn = shadow_copy2_unlink,
3218 .chmod_fn = shadow_copy2_chmod,
3219 .chown_fn = shadow_copy2_chown,
3220 .chdir_fn = shadow_copy2_chdir,
3221 .ntimes_fn = shadow_copy2_ntimes,
3222 .readlink_fn = shadow_copy2_readlink,
3223 .mknod_fn = shadow_copy2_mknod,
3224 .realpath_fn = shadow_copy2_realpath,
3225 .get_nt_acl_fn = shadow_copy2_get_nt_acl,
3226 .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
3227 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
3228 .mkdir_fn = shadow_copy2_mkdir,
3229 .rmdir_fn = shadow_copy2_rmdir,
3230 .getxattr_fn = shadow_copy2_getxattr,
3231 .listxattr_fn = shadow_copy2_listxattr,
3232 .removexattr_fn = shadow_copy2_removexattr,
3233 .setxattr_fn = shadow_copy2_setxattr,
3234 .chmod_acl_fn = shadow_copy2_chmod_acl,
3235 .chflags_fn = shadow_copy2_chflags,
3236 .get_real_filename_fn = shadow_copy2_get_real_filename,
3237 .connectpath_fn = shadow_copy2_connectpath,
3240 NTSTATUS vfs_shadow_copy2_init(TALLOC_CTX *);
3241 NTSTATUS vfs_shadow_copy2_init(TALLOC_CTX *ctx)
3243 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
3244 "shadow_copy2", &vfs_shadow_copy2_fns);