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"
39 struct shadow_copy2_config {
45 bool snapdirseverywhere;
46 bool crossmountpoints;
49 bool snapdir_absolute;
51 char *rel_connectpath; /* share root, relative to a snapshot root */
52 char *snapshot_basepath; /* the absolute version of snapdir */
55 /* Data-structure to hold the list of snap entries */
56 struct shadow_copy2_snapentry {
59 struct shadow_copy2_snapentry *next;
60 struct shadow_copy2_snapentry *prev;
63 struct shadow_copy2_snaplist_info {
64 struct shadow_copy2_snapentry *snaplist; /* snapshot list */
65 regex_t *regex; /* Regex to filter snaps */
66 time_t fetch_time; /* snaplist update time */
71 * shadow_copy2 private structure. This structure will be
72 * used to keep module specific information
74 struct shadow_copy2_private {
75 struct shadow_copy2_config *config;
76 struct shadow_copy2_snaplist_info *snaps;
77 char *shadow_cwd; /* Absolute $cwd path. */
78 /* Absolute connectpath - can vary depending on $cwd. */
79 char *shadow_connectpath;
82 static int shadow_copy2_get_shadow_copy_data(
83 vfs_handle_struct *handle, files_struct *fsp,
84 struct shadow_copy_data *shadow_copy2_data,
88 *This function will create a new snapshot list entry and
89 * return to the caller. This entry will also be added to
90 * the global snapshot list.
92 * @param[in] priv shadow_copy2 specific data structure
93 * @return Newly created snapshot entry or NULL on failure
95 static struct shadow_copy2_snapentry *shadow_copy2_create_snapentry(
96 struct shadow_copy2_private *priv)
98 struct shadow_copy2_snapentry *tmpentry = NULL;
100 tmpentry = talloc_zero(priv->snaps, struct shadow_copy2_snapentry);
101 if (tmpentry == NULL) {
102 DBG_ERR("talloc_zero() failed\n");
107 DLIST_ADD(priv->snaps->snaplist, tmpentry);
113 *This function will delete the entire snaplist and reset
114 * priv->snaps->snaplist to NULL.
116 * @param[in] priv shadow_copye specific data structure
118 static void shadow_copy2_delete_snaplist(struct shadow_copy2_private *priv)
120 struct shadow_copy2_snapentry *tmp = NULL;
122 while ((tmp = priv->snaps->snaplist) != NULL) {
123 DLIST_REMOVE(priv->snaps->snaplist, tmp);
129 * Given a timestamp this function searches the global snapshot list
130 * and returns the complete snapshot directory name saved in the entry.
132 * @param[in] priv shadow_copy2 specific structure
133 * @param[in] timestamp timestamp corresponding to one of the snapshot
134 * @param[out] snap_str buffer to copy the actual snapshot name
135 * @param[in] len length of snap_str buffer
137 * @return Length of actual snapshot name, and -1 on failure
139 static ssize_t shadow_copy2_saved_snapname(struct shadow_copy2_private *priv,
140 struct tm *timestamp,
141 char *snap_str, size_t len)
143 ssize_t snaptime_len = -1;
144 struct shadow_copy2_snapentry *entry = NULL;
146 snaptime_len = strftime(snap_str, len, GMT_FORMAT, timestamp);
147 if (snaptime_len == 0) {
148 DBG_ERR("strftime failed\n");
154 for (entry = priv->snaps->snaplist; entry; entry = entry->next) {
155 if (strcmp(entry->time_fmt, snap_str) == 0) {
156 snaptime_len = snprintf(snap_str, len, "%s",
168 * This function will check if snaplist is updated or not. If snaplist
169 * is empty then it will create a new list. Each time snaplist is updated
170 * the time is recorded. If the snapshot time is greater than the snaplist
171 * update time then chances are we are working on an older list. Then discard
172 * the old list and fetch a new snaplist.
174 * @param[in] handle VFS handle struct
175 * @param[in] snap_time time of snapshot
177 * @return true if the list is updated else false
179 static bool shadow_copy2_update_snaplist(struct vfs_handle_struct *handle,
183 bool snaplist_updated = false;
184 struct files_struct fsp = {0};
185 struct smb_filename smb_fname = {0};
186 double seconds = 0.0;
187 struct shadow_copy2_private *priv = NULL;
189 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
192 seconds = difftime(snap_time, priv->snaps->fetch_time);
195 * Fetch the snapshot list if either the snaplist is empty or the
196 * required snapshot time is greater than the last fetched snaplist
199 if (seconds > 0 || (priv->snaps->snaplist == NULL)) {
200 smb_fname.base_name = discard_const_p(char, ".");
201 fsp.fsp_name = &smb_fname;
203 ret = shadow_copy2_get_shadow_copy_data(handle, &fsp,
206 snaplist_updated = true;
208 DBG_ERR("Failed to get shadow copy data\n");
213 return snaplist_updated;
216 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
218 unsigned *pnum_offsets)
220 unsigned num_offsets;
227 while ((p = strchr(p, '/')) != NULL) {
232 offsets = talloc_array(mem_ctx, size_t, num_offsets);
233 if (offsets == NULL) {
239 while ((p = strchr(p, '/')) != NULL) {
240 offsets[num_offsets] = p-str;
246 *pnum_offsets = num_offsets;
251 * Given a timestamp, build the posix level GMT-tag string
252 * based on the configurable format.
254 static ssize_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
256 char *snaptime_string,
260 ssize_t snaptime_len;
261 struct shadow_copy2_config *config;
262 struct shadow_copy2_private *priv;
264 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
267 config = priv->config;
269 if (config->use_sscanf) {
270 snaptime_len = snprintf(snaptime_string,
273 (unsigned long)snapshot);
274 if (snaptime_len <= 0) {
275 DEBUG(10, ("snprintf failed\n"));
279 if (config->use_localtime) {
280 if (localtime_r(&snapshot, &snap_tm) == 0) {
281 DEBUG(10, ("gmtime_r failed\n"));
285 if (gmtime_r(&snapshot, &snap_tm) == 0) {
286 DEBUG(10, ("gmtime_r failed\n"));
291 if (priv->snaps->regex != NULL) {
292 snaptime_len = shadow_copy2_saved_snapname(priv,
293 &snap_tm, snaptime_string, len);
294 if (snaptime_len >= 0)
298 * If we fail to find the snapshot name, chances are
299 * that we have not updated our snaplist. Make sure the
300 * snaplist is updated.
302 if (!shadow_copy2_update_snaplist(handle, snapshot)) {
303 DBG_DEBUG("shadow_copy2_update_snaplist "
308 return shadow_copy2_saved_snapname(priv,
309 &snap_tm, snaptime_string, len);
312 snaptime_len = strftime(snaptime_string,
316 if (snaptime_len == 0) {
317 DEBUG(10, ("strftime failed\n"));
326 * Given a timestamp, build the string to insert into a path
327 * as a path component for creating the local path to the
328 * snapshot at the given timestamp of the input path.
330 * In the case of a parallel snapdir (specified with an
331 * absolute path), this is the inital portion of the
332 * local path of any snapshot file. The complete path is
333 * obtained by appending the portion of the file's path
334 * below the share root's mountpoint.
336 static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
337 struct vfs_handle_struct *handle,
340 fstring snaptime_string;
341 ssize_t snaptime_len = 0;
343 struct shadow_copy2_config *config;
344 struct shadow_copy2_private *priv;
346 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
349 config = priv->config;
351 snaptime_len = shadow_copy2_posix_gmt_string(handle,
354 sizeof(snaptime_string));
355 if (snaptime_len <= 0) {
359 if (config->snapdir_absolute) {
360 result = talloc_asprintf(mem_ctx, "%s/%s",
361 config->snapdir, snaptime_string);
363 result = talloc_asprintf(mem_ctx, "/%s/%s",
364 config->snapdir, snaptime_string);
366 if (result == NULL) {
367 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
374 * Build the posix snapshot path for the connection
375 * at the given timestamp, i.e. the absolute posix path
376 * that contains the snapshot for this file system.
378 * This only applies to classical case, i.e. not
379 * to the "snapdirseverywhere" mode.
381 static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
382 struct vfs_handle_struct *handle,
385 fstring snaptime_string;
386 ssize_t snaptime_len = 0;
388 struct shadow_copy2_private *priv;
390 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
393 snaptime_len = shadow_copy2_posix_gmt_string(handle,
396 sizeof(snaptime_string));
397 if (snaptime_len <= 0) {
401 result = talloc_asprintf(mem_ctx, "%s/%s",
402 priv->config->snapshot_basepath, snaptime_string);
403 if (result == NULL) {
404 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
411 * Strip a snapshot component from a filename as
412 * handed in via the smb layer.
413 * Returns the parsed timestamp and the stripped filename.
415 static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
416 struct vfs_handle_struct *handle,
422 time_t timestamp = 0;
425 char *stripped = NULL;
426 size_t rest_len, dst_len;
427 struct shadow_copy2_private *priv;
430 ptrdiff_t len_before_gmt;
432 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
435 DEBUG(10, (__location__ ": enter path '%s'\n", name));
437 p = strstr_m(name, "@GMT-");
439 DEBUG(11, ("@GMT not found\n"));
442 if ((p > name) && (p[-1] != '/')) {
443 /* the GMT-token does not start a path-component */
444 DEBUG(10, ("not at start, p=%p, name=%p, p[-1]=%d\n",
445 p, name, (int)p[-1]));
450 * Figure out whether we got an already converted string. One
451 * case where this happens is in a smb2 create call with the
452 * mxac create blob set. We do the get_acl call on
453 * fsp->fsp_name, which is already converted. We are converted
454 * if we got a file name of the form ".snapshots/@GMT-",
455 * i.e. ".snapshots/" precedes "p".
458 snapdir = lp_parm_const_string(SNUM(handle->conn), "shadow", "snapdir",
460 snapdirlen = strlen(snapdir);
461 len_before_gmt = p - name;
463 if ((len_before_gmt >= (snapdirlen + 1)) && (p[-1] == '/')) {
464 const char *parent_snapdir = p - (snapdirlen+1);
466 DEBUG(10, ("parent_snapdir = %s\n", parent_snapdir));
468 if (strncmp(parent_snapdir, snapdir, snapdirlen) == 0) {
469 DEBUG(10, ("name=%s is already converted\n", name));
473 q = strptime(p, GMT_FORMAT, &tm);
475 DEBUG(10, ("strptime failed\n"));
479 timestamp = timegm(&tm);
480 if (timestamp == (time_t)-1) {
481 DEBUG(10, ("timestamp==-1\n"));
486 * The name consists of only the GMT token or the GMT
487 * token is at the end of the path. XP seems to send
488 * @GMT- at the end under certain circumstances even
489 * with a path prefix.
491 if (pstripped != NULL) {
492 if (len_before_gmt > 0) {
494 * There is a slash before
495 * the @GMT-. Remove it.
499 stripped = talloc_strndup(mem_ctx, name,
501 if (stripped == NULL) {
504 *pstripped = stripped;
506 *ptimestamp = timestamp;
511 * It is not a complete path component, i.e. the path
512 * component continues after the gmt-token.
514 DEBUG(10, ("q[0] = %d\n", (int)q[0]));
519 rest_len = strlen(q);
520 dst_len = len_before_gmt + rest_len;
522 if (priv->config->snapdirseverywhere) {
525 insert = shadow_copy2_insert_string(talloc_tos(), handle,
527 if (insert == NULL) {
532 DEBUG(10, (__location__ ": snapdirseverywhere mode.\n"
534 "insert string '%s'\n", name, insert));
536 have_insert = (strstr(name, insert+1) != NULL);
537 DEBUG(10, ("have_insert=%d, name=%s, insert+1=%s\n",
538 (int)have_insert, name, insert+1));
540 DEBUG(10, (__location__ ": insert string '%s' found in "
541 "path '%s' found in snapdirseverywhere mode "
542 "==> already converted\n", insert, name));
551 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
554 if (snapshot_path == NULL) {
559 DEBUG(10, (__location__ " path: '%s'.\n"
560 "snapshot path: '%s'\n", name, snapshot_path));
562 s = strstr(name, snapshot_path);
565 * this starts with "snapshot_basepath/GMT-Token"
566 * so it is already a converted absolute
567 * path. Don't process further.
569 DEBUG(10, (__location__ ": path '%s' starts with "
570 "snapshot path '%s' (not in "
571 "snapdirseverywhere mode) ==> "
572 "already converted\n", name, snapshot_path));
573 talloc_free(snapshot_path);
576 talloc_free(snapshot_path);
579 if (pstripped != NULL) {
580 stripped = talloc_array(mem_ctx, char, dst_len+1);
581 if (stripped == NULL) {
586 memcpy(stripped, name, len_before_gmt);
589 memcpy(stripped + len_before_gmt, q, rest_len);
591 stripped[dst_len] = '\0';
592 *pstripped = stripped;
594 *ptimestamp = timestamp;
601 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
602 vfs_handle_struct *handle)
604 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
609 if (stat(path, &st) != 0) {
616 while ((p = strrchr(path, '/')) && p > path) {
618 if (stat(path, &st) != 0) {
622 if (st.st_dev != dev) {
632 * Convert from a name as handed in via the SMB layer
633 * and a timestamp into the local path of the snapshot
634 * of the provided file at the provided time.
635 * Also return the path in the snapshot corresponding
636 * to the file's share root.
638 static char *shadow_copy2_do_convert(TALLOC_CTX *mem_ctx,
639 struct vfs_handle_struct *handle,
640 const char *name, time_t timestamp,
641 size_t *snaproot_len)
643 struct smb_filename converted_fname;
645 size_t *slashes = NULL;
646 unsigned num_slashes;
650 char *converted = NULL;
651 size_t insertlen, connectlen = 0;
654 struct shadow_copy2_config *config;
655 struct shadow_copy2_private *priv;
656 size_t in_share_offset = 0;
658 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
661 config = priv->config;
663 DEBUG(10, ("converting '%s'\n", name));
665 if (!config->snapdirseverywhere) {
669 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
672 if (snapshot_path == NULL) {
676 if (config->rel_connectpath == NULL) {
677 converted = talloc_asprintf(mem_ctx, "%s/%s",
678 snapshot_path, name);
680 converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
682 config->rel_connectpath,
685 if (converted == NULL) {
689 ZERO_STRUCT(converted_fname);
690 converted_fname.base_name = converted;
692 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
693 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
695 ret, ret == 0 ? "ok" : strerror(errno)));
697 DEBUG(10, ("Found %s\n", converted));
700 if (snaproot_len != NULL) {
701 *snaproot_len = strlen(snapshot_path);
702 if (config->rel_connectpath != NULL) {
704 strlen(config->rel_connectpath) + 1;
712 /* never reached ... */
715 connectlen = strlen(handle->conn->connectpath);
717 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
719 path = talloc_asprintf(
720 mem_ctx, "%s/%s", handle->conn->connectpath, name);
726 pathlen = talloc_get_size(path)-1;
728 if (!shadow_copy2_find_slashes(talloc_tos(), path,
729 &slashes, &num_slashes)) {
733 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
734 if (insert == NULL) {
737 insertlen = talloc_get_size(insert)-1;
740 * Note: We deliberatly don't expensively initialize the
741 * array with talloc_zero here: Putting zero into
742 * converted[pathlen+insertlen] below is sufficient, because
743 * in the following for loop, the insert string is inserted
744 * at various slash places. So the memory up to position
745 * pathlen+insertlen will always be initialized when the
746 * converted string is used.
748 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
749 if (converted == NULL) {
753 if (path[pathlen-1] != '/') {
755 * Append a fake slash to find the snapshot root
758 tmp = talloc_realloc(talloc_tos(), slashes,
759 size_t, num_slashes+1);
764 slashes[num_slashes] = pathlen;
770 if (!config->crossmountpoints) {
771 min_offset = strlen(config->mount_point);
774 memcpy(converted, path, pathlen+1);
775 converted[pathlen+insertlen] = '\0';
777 ZERO_STRUCT(converted_fname);
778 converted_fname.base_name = converted;
780 for (i = num_slashes-1; i>=0; i--) {
786 if (offset < min_offset) {
791 if (offset >= connectlen) {
792 in_share_offset = offset;
795 memcpy(converted+offset, insert, insertlen);
798 memcpy(converted+offset, path + slashes[i],
799 pathlen - slashes[i]);
801 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
803 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
805 ret, ret == 0 ? "ok" : strerror(errno)));
808 if (snaproot_len != NULL) {
809 *snaproot_len = in_share_offset + insertlen;
813 if (errno == ENOTDIR) {
815 * This is a valid condition: We appended the
816 * .snaphots/@GMT.. to a file name. Just try
817 * with the upper levels.
821 if (errno != ENOENT) {
822 /* Other problem than "not found" */
831 DEBUG(10, ("Found %s\n", converted));
839 TALLOC_FREE(converted);
841 TALLOC_FREE(slashes);
848 * Convert from a name as handed in via the SMB layer
849 * and a timestamp into the local path of the snapshot
850 * of the provided file at the provided time.
852 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
853 struct vfs_handle_struct *handle,
854 const char *name, time_t timestamp)
856 return shadow_copy2_do_convert(mem_ctx, handle, name, timestamp, NULL);
860 modify a sbuf return to ensure that inodes in the shadow directory
861 are different from those in the main directory
863 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
864 SMB_STRUCT_STAT *sbuf)
866 struct shadow_copy2_private *priv;
868 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
871 if (priv->config->fixinodes) {
872 /* some snapshot systems, like GPFS, return the name
873 device:inode for the snapshot files as the current
874 files. That breaks the 'restore' button in the shadow copy
875 GUI, as the client gets a sharing violation.
877 This is a crude way of allowing both files to be
878 open at once. It has a slight chance of inode
879 number collision, but I can't see a better approach
880 without significant VFS changes
882 TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
883 .dsize = strlen(fname) };
886 shash = tdb_jenkins_hash(&key) & 0xFF000000;
890 sbuf->st_ex_ino ^= shash;
894 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
895 const struct smb_filename *smb_fname,
899 time_t timestamp = 0;
900 char *stripped = NULL;
904 struct smb_filename *conv_smb_fname = NULL;
906 if (!shadow_copy2_strip_snapshot(talloc_tos(),
908 smb_fname->base_name,
913 if (timestamp == 0) {
914 return SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
916 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
917 TALLOC_FREE(stripped);
921 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
926 if (conv_smb_fname == NULL) {
930 ret = SMB_VFS_NEXT_OPENDIR(handle, conv_smb_fname, mask, attr);
933 TALLOC_FREE(conv_smb_fname);
938 static int shadow_copy2_rename(vfs_handle_struct *handle,
939 const struct smb_filename *smb_fname_src,
940 const struct smb_filename *smb_fname_dst)
942 time_t timestamp_src = 0;
943 time_t timestamp_dst = 0;
945 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
946 smb_fname_src->base_name,
947 ×tamp_src, NULL)) {
950 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
951 smb_fname_dst->base_name,
952 ×tamp_dst, NULL)) {
955 if (timestamp_src != 0) {
959 if (timestamp_dst != 0) {
963 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
966 static int shadow_copy2_symlink(vfs_handle_struct *handle,
967 const char *oldname, const char *newname)
969 time_t timestamp_old = 0;
970 time_t timestamp_new = 0;
972 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
973 ×tamp_old, NULL)) {
976 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
977 ×tamp_new, NULL)) {
980 if ((timestamp_old != 0) || (timestamp_new != 0)) {
984 return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
987 static int shadow_copy2_link(vfs_handle_struct *handle,
988 const char *oldname, const char *newname)
990 time_t timestamp_old = 0;
991 time_t timestamp_new = 0;
993 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
994 ×tamp_old, NULL)) {
997 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
998 ×tamp_new, NULL)) {
1001 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1005 return SMB_VFS_NEXT_LINK(handle, oldname, newname);
1008 static int shadow_copy2_stat(vfs_handle_struct *handle,
1009 struct smb_filename *smb_fname)
1011 time_t timestamp = 0;
1012 char *stripped = NULL;
1014 int ret, saved_errno;
1016 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1017 smb_fname->base_name,
1018 ×tamp, &stripped)) {
1021 if (timestamp == 0) {
1022 return SMB_VFS_NEXT_STAT(handle, smb_fname);
1025 tmp = smb_fname->base_name;
1026 smb_fname->base_name = shadow_copy2_convert(
1027 talloc_tos(), handle, stripped, timestamp);
1028 TALLOC_FREE(stripped);
1030 if (smb_fname->base_name == NULL) {
1031 smb_fname->base_name = tmp;
1035 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1036 saved_errno = errno;
1038 TALLOC_FREE(smb_fname->base_name);
1039 smb_fname->base_name = tmp;
1042 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1044 errno = saved_errno;
1048 static int shadow_copy2_lstat(vfs_handle_struct *handle,
1049 struct smb_filename *smb_fname)
1051 time_t timestamp = 0;
1052 char *stripped = NULL;
1054 int ret, saved_errno;
1056 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1057 smb_fname->base_name,
1058 ×tamp, &stripped)) {
1061 if (timestamp == 0) {
1062 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1065 tmp = smb_fname->base_name;
1066 smb_fname->base_name = shadow_copy2_convert(
1067 talloc_tos(), handle, stripped, timestamp);
1068 TALLOC_FREE(stripped);
1070 if (smb_fname->base_name == NULL) {
1071 smb_fname->base_name = tmp;
1075 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1076 saved_errno = errno;
1078 TALLOC_FREE(smb_fname->base_name);
1079 smb_fname->base_name = tmp;
1082 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1084 errno = saved_errno;
1088 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
1089 SMB_STRUCT_STAT *sbuf)
1091 time_t timestamp = 0;
1094 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1098 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1099 fsp->fsp_name->base_name,
1100 ×tamp, NULL)) {
1103 if (timestamp != 0) {
1104 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
1109 static int shadow_copy2_open(vfs_handle_struct *handle,
1110 struct smb_filename *smb_fname, files_struct *fsp,
1111 int flags, mode_t mode)
1113 time_t timestamp = 0;
1114 char *stripped = NULL;
1116 int ret, saved_errno;
1118 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1119 smb_fname->base_name,
1120 ×tamp, &stripped)) {
1123 if (timestamp == 0) {
1124 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1127 tmp = smb_fname->base_name;
1128 smb_fname->base_name = shadow_copy2_convert(
1129 talloc_tos(), handle, stripped, timestamp);
1130 TALLOC_FREE(stripped);
1132 if (smb_fname->base_name == NULL) {
1133 smb_fname->base_name = tmp;
1137 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1138 saved_errno = errno;
1140 TALLOC_FREE(smb_fname->base_name);
1141 smb_fname->base_name = tmp;
1143 errno = saved_errno;
1147 static int shadow_copy2_unlink(vfs_handle_struct *handle,
1148 const struct smb_filename *smb_fname)
1150 time_t timestamp = 0;
1151 char *stripped = NULL;
1152 int ret, saved_errno;
1153 struct smb_filename *conv;
1155 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1156 smb_fname->base_name,
1157 ×tamp, &stripped)) {
1160 if (timestamp == 0) {
1161 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1163 conv = cp_smb_filename(talloc_tos(), smb_fname);
1168 conv->base_name = shadow_copy2_convert(
1169 conv, handle, stripped, timestamp);
1170 TALLOC_FREE(stripped);
1171 if (conv->base_name == NULL) {
1174 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
1175 saved_errno = errno;
1177 errno = saved_errno;
1181 static int shadow_copy2_chmod(vfs_handle_struct *handle,
1182 const struct smb_filename *smb_fname,
1185 time_t timestamp = 0;
1186 char *stripped = NULL;
1187 int ret, saved_errno;
1189 struct smb_filename *conv_smb_fname;
1191 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1193 smb_fname->base_name,
1198 if (timestamp == 0) {
1199 TALLOC_FREE(stripped);
1200 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1202 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1203 TALLOC_FREE(stripped);
1207 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1212 if (conv_smb_fname == NULL) {
1218 ret = SMB_VFS_NEXT_CHMOD(handle, conv_smb_fname, mode);
1219 saved_errno = errno;
1221 TALLOC_FREE(conv_smb_fname);
1222 errno = saved_errno;
1226 static int shadow_copy2_chown(vfs_handle_struct *handle,
1227 const struct smb_filename *smb_fname,
1231 time_t timestamp = 0;
1232 char *stripped = NULL;
1233 int ret, saved_errno;
1235 struct smb_filename *conv_smb_fname = NULL;
1237 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1239 smb_fname->base_name,
1244 if (timestamp == 0) {
1245 return SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
1247 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1248 TALLOC_FREE(stripped);
1252 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1257 if (conv_smb_fname == NULL) {
1262 ret = SMB_VFS_NEXT_CHOWN(handle, conv_smb_fname, uid, gid);
1263 saved_errno = errno;
1265 TALLOC_FREE(conv_smb_fname);
1266 errno = saved_errno;
1270 static int shadow_copy2_chdir(vfs_handle_struct *handle,
1273 time_t timestamp = 0;
1274 char *stripped = NULL;
1275 int ret, saved_errno;
1278 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1279 ×tamp, &stripped)) {
1282 if (timestamp == 0) {
1283 return SMB_VFS_NEXT_CHDIR(handle, fname);
1285 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1286 TALLOC_FREE(stripped);
1290 ret = SMB_VFS_NEXT_CHDIR(handle, conv);
1291 saved_errno = errno;
1293 errno = saved_errno;
1297 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
1298 const struct smb_filename *smb_fname,
1299 struct smb_file_time *ft)
1301 time_t timestamp = 0;
1302 char *stripped = NULL;
1303 int ret, saved_errno;
1304 struct smb_filename *conv;
1306 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1307 smb_fname->base_name,
1308 ×tamp, &stripped)) {
1311 if (timestamp == 0) {
1312 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1314 conv = cp_smb_filename(talloc_tos(), smb_fname);
1319 conv->base_name = shadow_copy2_convert(
1320 conv, handle, stripped, timestamp);
1321 TALLOC_FREE(stripped);
1322 if (conv->base_name == NULL) {
1325 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
1326 saved_errno = errno;
1328 errno = saved_errno;
1332 static int shadow_copy2_readlink(vfs_handle_struct *handle,
1333 const char *fname, char *buf, size_t bufsiz)
1335 time_t timestamp = 0;
1336 char *stripped = NULL;
1337 int ret, saved_errno;
1340 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1341 ×tamp, &stripped)) {
1344 if (timestamp == 0) {
1345 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
1347 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1348 TALLOC_FREE(stripped);
1352 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1353 saved_errno = errno;
1355 errno = saved_errno;
1359 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1360 const char *fname, mode_t mode, SMB_DEV_T dev)
1362 time_t timestamp = 0;
1363 char *stripped = NULL;
1364 int ret, saved_errno;
1367 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1368 ×tamp, &stripped)) {
1371 if (timestamp == 0) {
1372 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
1374 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1375 TALLOC_FREE(stripped);
1379 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1380 saved_errno = errno;
1382 errno = saved_errno;
1386 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
1389 time_t timestamp = 0;
1390 char *stripped = NULL;
1392 char *result = NULL;
1395 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1396 ×tamp, &stripped)) {
1399 if (timestamp == 0) {
1400 return SMB_VFS_NEXT_REALPATH(handle, fname);
1403 tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1408 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1411 saved_errno = errno;
1413 TALLOC_FREE(stripped);
1414 errno = saved_errno;
1419 * Check whether a given directory contains a
1420 * snapshot directory as direct subdirectory.
1421 * If yes, return the path of the snapshot-subdir,
1422 * otherwise return NULL.
1424 static char *have_snapdir(struct vfs_handle_struct *handle,
1427 struct smb_filename smb_fname;
1429 struct shadow_copy2_private *priv;
1431 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1434 ZERO_STRUCT(smb_fname);
1435 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1436 path, priv->config->snapdir);
1437 if (smb_fname.base_name == NULL) {
1441 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1442 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1443 return smb_fname.base_name;
1445 TALLOC_FREE(smb_fname.base_name);
1449 static bool check_access_snapdir(struct vfs_handle_struct *handle,
1452 struct smb_filename smb_fname;
1456 ZERO_STRUCT(smb_fname);
1457 smb_fname.base_name = talloc_asprintf(talloc_tos(),
1460 if (smb_fname.base_name == NULL) {
1464 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1465 if (ret != 0 || !S_ISDIR(smb_fname.st.st_ex_mode)) {
1466 TALLOC_FREE(smb_fname.base_name);
1470 status = smbd_check_access_rights(handle->conn,
1474 if (!NT_STATUS_IS_OK(status)) {
1475 DEBUG(0,("user does not have list permission "
1477 smb_fname.base_name));
1478 TALLOC_FREE(smb_fname.base_name);
1481 TALLOC_FREE(smb_fname.base_name);
1486 * Find the snapshot directory (if any) for the given
1487 * filename (which is relative to the share).
1489 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1490 struct vfs_handle_struct *handle,
1491 struct smb_filename *smb_fname)
1494 const char *snapdir;
1495 struct shadow_copy2_config *config;
1496 struct shadow_copy2_private *priv;
1498 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1501 config = priv->config;
1504 * If the non-snapdisrseverywhere mode, we should not search!
1506 if (!config->snapdirseverywhere) {
1507 return config->snapshot_basepath;
1510 path = talloc_asprintf(mem_ctx, "%s/%s",
1511 handle->conn->connectpath,
1512 smb_fname->base_name);
1517 snapdir = have_snapdir(handle, path);
1518 if (snapdir != NULL) {
1523 while ((p = strrchr(path, '/')) && (p > path)) {
1527 snapdir = have_snapdir(handle, path);
1528 if (snapdir != NULL) {
1537 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1539 char *gmt, size_t gmt_len)
1541 struct tm timestamp;
1543 unsigned long int timestamp_long;
1545 struct shadow_copy2_config *config;
1546 struct shadow_copy2_private *priv;
1547 char *tmpstr = NULL;
1549 bool converted = false;
1552 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1555 config = priv->config;
1557 fmt = config->gmt_format;
1560 * If regex is provided, then we will have to parse the
1561 * filename which will contain both the prefix and the time format.
1562 * e.g. <prefix><delimiter><time_format>
1564 if (priv->snaps->regex != NULL) {
1565 tmpstr = talloc_strdup(talloc_tos(), name);
1566 /* point "name" to the time format */
1567 name = strstr(name, priv->config->delimiter);
1571 /* Extract the prefix */
1572 tmp = strstr(tmpstr, priv->config->delimiter);
1576 ret = regexec(priv->snaps->regex, tmpstr, 0, NULL, 0);
1578 DBG_DEBUG("shadow_copy2_snapshot_to_gmt: "
1579 "no regex match for %s\n", tmpstr);
1584 ZERO_STRUCT(timestamp);
1585 if (config->use_sscanf) {
1586 if (sscanf(name, fmt, ×tamp_long) != 1) {
1587 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1588 "no sscanf match %s: %s\n",
1592 timestamp_t = timestamp_long;
1593 gmtime_r(×tamp_t, ×tamp);
1595 if (strptime(name, fmt, ×tamp) == NULL) {
1596 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1597 "no match %s: %s\n",
1601 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1604 if (config->use_localtime) {
1605 timestamp.tm_isdst = -1;
1606 timestamp_t = mktime(×tamp);
1607 gmtime_r(×tamp_t, ×tamp);
1611 strftime(gmt, gmt_len, GMT_FORMAT, ×tamp);
1615 TALLOC_FREE(tmpstr);
1619 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1621 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1624 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1626 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1630 sort the shadow copy data in ascending or descending order
1632 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1633 struct shadow_copy_data *shadow_copy2_data)
1635 int (*cmpfunc)(const void *, const void *);
1637 struct shadow_copy2_private *priv;
1639 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1642 sort = priv->config->sort_order;
1647 if (strcmp(sort, "asc") == 0) {
1648 cmpfunc = shadow_copy2_label_cmp_asc;
1649 } else if (strcmp(sort, "desc") == 0) {
1650 cmpfunc = shadow_copy2_label_cmp_desc;
1655 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1656 shadow_copy2_data->labels)
1658 TYPESAFE_QSORT(shadow_copy2_data->labels,
1659 shadow_copy2_data->num_volumes,
1664 static int shadow_copy2_get_shadow_copy_data(
1665 vfs_handle_struct *handle, files_struct *fsp,
1666 struct shadow_copy_data *shadow_copy2_data,
1670 const char *snapdir;
1671 struct smb_filename *snapdir_smb_fname = NULL;
1673 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1674 struct shadow_copy2_private *priv = NULL;
1675 struct shadow_copy2_snapentry *tmpentry = NULL;
1676 bool get_snaplist = false;
1677 bool access_granted = false;
1680 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1681 if (snapdir == NULL) {
1682 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1683 handle->conn->connectpath));
1688 access_granted = check_access_snapdir(handle, snapdir);
1689 if (!access_granted) {
1690 DEBUG(0,("access denied on listing snapdir %s\n", snapdir));
1695 snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
1699 fsp->fsp_name->flags);
1700 if (snapdir_smb_fname == NULL) {
1705 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir_smb_fname, NULL, 0);
1708 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1709 " - %s\n", snapdir, strerror(errno)));
1714 if (shadow_copy2_data != NULL) {
1715 shadow_copy2_data->num_volumes = 0;
1716 shadow_copy2_data->labels = NULL;
1719 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1723 * Normally this function is called twice once with labels = false and
1724 * then with labels = true. When labels is false it will return the
1725 * number of volumes so that the caller can allocate memory for that
1726 * many labels. Therefore to eliminate snaplist both the times it is
1727 * good to check if labels is set or not.
1729 * shadow_copy2_data is NULL when we only want to update the list and
1730 * don't want any labels.
1732 if ((priv->snaps->regex != NULL) && (labels || shadow_copy2_data == NULL)) {
1733 get_snaplist = true;
1734 /* Reset the global snaplist */
1735 shadow_copy2_delete_snaplist(priv);
1737 /* Set the current time as snaplist update time */
1738 time(&(priv->snaps->fetch_time));
1741 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
1742 char snapshot[GMT_NAME_LEN+1];
1743 SHADOW_COPY_LABEL *tlabels;
1746 * ignore names not of the right form in the snapshot
1749 if (!shadow_copy2_snapshot_to_gmt(
1751 snapshot, sizeof(snapshot))) {
1753 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1754 "ignoring %s\n", d->d_name));
1757 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1758 d->d_name, snapshot));
1762 * Create a snap entry for each successful
1765 tmpentry = shadow_copy2_create_snapentry(priv);
1766 if (tmpentry == NULL) {
1767 DBG_ERR("talloc_zero() failed\n");
1770 tmpentry->snapname = talloc_strdup(tmpentry, d->d_name);
1771 tmpentry->time_fmt = talloc_strdup(tmpentry, snapshot);
1774 if (shadow_copy2_data == NULL) {
1779 /* the caller doesn't want the labels */
1780 shadow_copy2_data->num_volumes++;
1784 tlabels = talloc_realloc(shadow_copy2_data,
1785 shadow_copy2_data->labels,
1787 shadow_copy2_data->num_volumes+1);
1788 if (tlabels == NULL) {
1789 DEBUG(0,("shadow_copy2: out of memory\n"));
1790 SMB_VFS_NEXT_CLOSEDIR(handle, p);
1794 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
1797 shadow_copy2_data->num_volumes++;
1798 shadow_copy2_data->labels = tlabels;
1801 SMB_VFS_NEXT_CLOSEDIR(handle,p);
1803 shadow_copy2_sort_data(handle, shadow_copy2_data);
1807 TALLOC_FREE(tmp_ctx);
1811 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
1812 struct files_struct *fsp,
1813 uint32_t security_info,
1814 TALLOC_CTX *mem_ctx,
1815 struct security_descriptor **ppdesc)
1817 time_t timestamp = 0;
1818 char *stripped = NULL;
1821 struct smb_filename *smb_fname = NULL;
1823 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1824 fsp->fsp_name->base_name,
1825 ×tamp, &stripped)) {
1826 return map_nt_error_from_unix(errno);
1828 if (timestamp == 0) {
1829 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1833 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1834 TALLOC_FREE(stripped);
1836 return map_nt_error_from_unix(errno);
1838 smb_fname = synthetic_smb_fname(talloc_tos(),
1842 fsp->fsp_name->flags);
1843 if (smb_fname == NULL) {
1845 return NT_STATUS_NO_MEMORY;
1848 status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1851 TALLOC_FREE(smb_fname);
1855 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
1856 const struct smb_filename *smb_fname,
1857 uint32_t security_info,
1858 TALLOC_CTX *mem_ctx,
1859 struct security_descriptor **ppdesc)
1861 time_t timestamp = 0;
1862 char *stripped = NULL;
1865 struct smb_filename *conv_smb_fname = NULL;
1867 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1869 smb_fname->base_name,
1872 return map_nt_error_from_unix(errno);
1874 if (timestamp == 0) {
1875 return SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1878 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1879 TALLOC_FREE(stripped);
1881 return map_nt_error_from_unix(errno);
1883 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1888 if (conv_smb_fname == NULL) {
1890 return NT_STATUS_NO_MEMORY;
1892 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv_smb_fname, security_info,
1895 TALLOC_FREE(conv_smb_fname);
1899 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
1900 const struct smb_filename *smb_fname,
1903 time_t timestamp = 0;
1904 char *stripped = NULL;
1905 int ret, saved_errno;
1907 struct smb_filename *conv_smb_fname = NULL;
1909 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1911 smb_fname->base_name,
1916 if (timestamp == 0) {
1917 return SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
1919 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1920 TALLOC_FREE(stripped);
1924 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1929 if (conv_smb_fname == NULL) {
1933 ret = SMB_VFS_NEXT_MKDIR(handle, conv_smb_fname, mode);
1934 saved_errno = errno;
1936 TALLOC_FREE(conv_smb_fname);
1937 errno = saved_errno;
1941 static int shadow_copy2_rmdir(vfs_handle_struct *handle,
1942 const struct smb_filename *smb_fname)
1944 time_t timestamp = 0;
1945 char *stripped = NULL;
1946 int ret, saved_errno;
1948 struct smb_filename *conv_smb_fname = NULL;
1950 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1952 smb_fname->base_name,
1957 if (timestamp == 0) {
1958 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
1960 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1961 TALLOC_FREE(stripped);
1965 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1970 if (conv_smb_fname == NULL) {
1974 ret = SMB_VFS_NEXT_RMDIR(handle, conv_smb_fname);
1975 saved_errno = errno;
1976 TALLOC_FREE(conv_smb_fname);
1978 errno = saved_errno;
1982 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
1985 time_t timestamp = 0;
1986 char *stripped = NULL;
1987 int ret, saved_errno;
1990 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1991 ×tamp, &stripped)) {
1994 if (timestamp == 0) {
1995 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
1997 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1998 TALLOC_FREE(stripped);
2002 ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
2003 saved_errno = errno;
2005 errno = saved_errno;
2009 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
2010 const char *fname, const char *aname,
2011 void *value, size_t size)
2013 time_t timestamp = 0;
2014 char *stripped = NULL;
2019 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2020 ×tamp, &stripped)) {
2023 if (timestamp == 0) {
2024 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
2027 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2028 TALLOC_FREE(stripped);
2032 ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
2033 saved_errno = errno;
2035 errno = saved_errno;
2039 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
2041 char *list, size_t size)
2043 time_t timestamp = 0;
2044 char *stripped = NULL;
2049 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2050 ×tamp, &stripped)) {
2053 if (timestamp == 0) {
2054 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
2056 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2057 TALLOC_FREE(stripped);
2061 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
2062 saved_errno = errno;
2064 errno = saved_errno;
2068 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
2069 const char *fname, const char *aname)
2071 time_t timestamp = 0;
2072 char *stripped = NULL;
2073 int ret, saved_errno;
2076 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2077 ×tamp, &stripped)) {
2080 if (timestamp == 0) {
2081 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
2083 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2084 TALLOC_FREE(stripped);
2088 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
2089 saved_errno = errno;
2091 errno = saved_errno;
2095 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
2097 const char *aname, const void *value,
2098 size_t size, int flags)
2100 time_t timestamp = 0;
2101 char *stripped = NULL;
2106 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2107 ×tamp, &stripped)) {
2110 if (timestamp == 0) {
2111 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
2114 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2115 TALLOC_FREE(stripped);
2119 ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
2120 saved_errno = errno;
2122 errno = saved_errno;
2126 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
2127 const struct smb_filename *smb_fname,
2130 time_t timestamp = 0;
2131 char *stripped = NULL;
2135 struct smb_filename *conv_smb_fname = NULL;
2137 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2139 smb_fname->base_name,
2144 if (timestamp == 0) {
2145 return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
2147 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2148 TALLOC_FREE(stripped);
2152 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2157 if (conv_smb_fname == NULL) {
2162 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv_smb_fname, mode);
2163 saved_errno = errno;
2165 TALLOC_FREE(conv_smb_fname);
2166 errno = saved_errno;
2170 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
2173 TALLOC_CTX *mem_ctx,
2176 time_t timestamp = 0;
2177 char *stripped = NULL;
2182 DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
2183 "name=[%s]\n", path, name));
2185 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
2186 ×tamp, &stripped)) {
2187 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
2190 if (timestamp == 0) {
2191 DEBUG(10, ("timestamp == 0\n"));
2192 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
2193 mem_ctx, found_name);
2195 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2196 TALLOC_FREE(stripped);
2198 DEBUG(10, ("shadow_copy2_convert failed\n"));
2201 DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
2202 "name=[%s]\n", conv, name));
2203 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
2204 mem_ctx, found_name);
2205 DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
2206 saved_errno = errno;
2208 errno = saved_errno;
2212 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
2215 time_t timestamp = 0;
2216 char *stripped = NULL;
2218 char *result = NULL;
2219 char *parent_dir = NULL;
2221 size_t rootpath_len = 0;
2223 DBG_DEBUG("Calc connect path for [%s]\n", fname);
2225 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2226 ×tamp, &stripped)) {
2229 if (timestamp == 0) {
2230 return SMB_VFS_NEXT_CONNECTPATH(handle, fname);
2233 tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
2236 if (errno != ENOENT) {
2241 * If the converted path does not exist, and converting
2242 * the parent yields something that does exist, then
2243 * this path refers to something that has not been
2244 * created yet, relative to the parent path.
2245 * The snapshot finding is relative to the parent.
2246 * (usually snapshots are read/only but this is not
2247 * necessarily true).
2248 * This code also covers getting a wildcard in the
2249 * last component, because this function is called
2250 * prior to sanitizing the path, and in SMB1 we may
2251 * get wildcards in path names.
2253 if (!parent_dirname(talloc_tos(), stripped, &parent_dir,
2259 tmp = shadow_copy2_do_convert(talloc_tos(), handle, parent_dir,
2260 timestamp, &rootpath_len);
2266 DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
2267 (int)rootpath_len, tmp);
2269 tmp[rootpath_len] = '\0';
2270 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
2271 if (result == NULL) {
2275 DBG_DEBUG("connect path is [%s]\n", result);
2278 saved_errno = errno;
2280 TALLOC_FREE(stripped);
2281 TALLOC_FREE(parent_dir);
2282 errno = saved_errno;
2286 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
2287 const char *path, uint64_t *bsize,
2288 uint64_t *dfree, uint64_t *dsize)
2290 time_t timestamp = 0;
2291 char *stripped = NULL;
2296 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
2297 ×tamp, &stripped)) {
2300 if (timestamp == 0) {
2301 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2302 bsize, dfree, dsize);
2305 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2306 TALLOC_FREE(stripped);
2311 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, bsize, dfree, dsize);
2313 saved_errno = errno;
2315 errno = saved_errno;
2320 static int shadow_copy2_get_quota(vfs_handle_struct *handle, const char *path,
2321 enum SMB_QUOTA_TYPE qtype, unid_t id,
2324 time_t timestamp = 0;
2325 char *stripped = NULL;
2330 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path, ×tamp,
2334 if (timestamp == 0) {
2335 return SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, dq);
2338 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2339 TALLOC_FREE(stripped);
2344 ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv, qtype, id, dq);
2346 saved_errno = errno;
2348 errno = saved_errno;
2353 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
2354 const char *service, const char *user)
2356 struct shadow_copy2_config *config;
2357 struct shadow_copy2_private *priv;
2359 const char *snapdir;
2360 const char *snapprefix = NULL;
2361 const char *delimiter;
2362 const char *gmt_format;
2363 const char *sort_order;
2364 const char *basedir = NULL;
2365 const char *snapsharepath = NULL;
2366 const char *mount_point;
2368 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
2369 (unsigned)handle->conn->cnum,
2370 handle->conn->connectpath));
2372 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
2377 priv = talloc_zero(handle->conn, struct shadow_copy2_private);
2379 DBG_ERR("talloc_zero() failed\n");
2384 priv->snaps = talloc_zero(priv, struct shadow_copy2_snaplist_info);
2385 if (priv->snaps == NULL) {
2386 DBG_ERR("talloc_zero() failed\n");
2391 config = talloc_zero(priv, struct shadow_copy2_config);
2392 if (config == NULL) {
2393 DEBUG(0, ("talloc_zero() failed\n"));
2398 priv->config = config;
2400 gmt_format = lp_parm_const_string(SNUM(handle->conn),
2403 config->gmt_format = talloc_strdup(config, gmt_format);
2404 if (config->gmt_format == NULL) {
2405 DEBUG(0, ("talloc_strdup() failed\n"));
2410 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
2411 "shadow", "sscanf", false);
2413 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
2414 "shadow", "localtime",
2417 snapdir = lp_parm_const_string(SNUM(handle->conn),
2418 "shadow", "snapdir",
2420 config->snapdir = talloc_strdup(config, snapdir);
2421 if (config->snapdir == NULL) {
2422 DEBUG(0, ("talloc_strdup() failed\n"));
2427 snapprefix = lp_parm_const_string(SNUM(handle->conn),
2428 "shadow", "snapprefix",
2430 if (snapprefix != NULL) {
2431 priv->snaps->regex = talloc_zero(priv->snaps, regex_t);
2432 if (priv->snaps->regex == NULL) {
2433 DBG_ERR("talloc_zero() failed\n");
2438 /* pre-compute regex rule for matching pattern later */
2439 ret = regcomp(priv->snaps->regex, snapprefix, 0);
2441 DBG_ERR("Failed to create regex object\n");
2446 delimiter = lp_parm_const_string(SNUM(handle->conn),
2447 "shadow", "delimiter",
2449 if (delimiter != NULL) {
2450 priv->config->delimiter = talloc_strdup(priv->config, delimiter);
2451 if (priv->config->delimiter == NULL) {
2452 DBG_ERR("talloc_strdup() failed\n");
2458 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
2460 "snapdirseverywhere",
2463 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
2464 "shadow", "crossmountpoints",
2467 if (config->crossmountpoints && !config->snapdirseverywhere) {
2468 DBG_WARNING("Warning: 'crossmountpoints' depends on "
2469 "'snapdirseverywhere'. Disabling crossmountpoints.\n");
2472 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
2473 "shadow", "fixinodes",
2476 sort_order = lp_parm_const_string(SNUM(handle->conn),
2477 "shadow", "sort", "desc");
2478 config->sort_order = talloc_strdup(config, sort_order);
2479 if (config->sort_order == NULL) {
2480 DEBUG(0, ("talloc_strdup() failed\n"));
2485 mount_point = lp_parm_const_string(SNUM(handle->conn),
2486 "shadow", "mountpoint", NULL);
2487 if (mount_point != NULL) {
2488 if (mount_point[0] != '/') {
2489 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
2490 "relative ('%s'), but it has to be an "
2491 "absolute path. Ignoring provided value.\n",
2496 p = strstr(handle->conn->connectpath, mount_point);
2497 if (p != handle->conn->connectpath) {
2498 DBG_WARNING("Warning: the share root (%s) is "
2499 "not a subdirectory of the "
2500 "specified mountpoint (%s). "
2501 "Ignoring provided value.\n",
2502 handle->conn->connectpath,
2509 if (mount_point != NULL) {
2510 config->mount_point = talloc_strdup(config, mount_point);
2511 if (config->mount_point == NULL) {
2512 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
2516 config->mount_point = shadow_copy2_find_mount_point(config,
2518 if (config->mount_point == NULL) {
2519 DBG_WARNING("shadow_copy2_find_mount_point "
2520 "of the share root '%s' failed: %s\n",
2521 handle->conn->connectpath, strerror(errno));
2526 basedir = lp_parm_const_string(SNUM(handle->conn),
2527 "shadow", "basedir", NULL);
2529 if (basedir != NULL) {
2530 if (basedir[0] != '/') {
2531 DEBUG(1, (__location__ " Warning: 'basedir' is "
2532 "relative ('%s'), but it has to be an "
2533 "absolute path. Disabling basedir.\n",
2538 p = strstr(basedir, config->mount_point);
2540 DEBUG(1, ("Warning: basedir (%s) is not a "
2541 "subdirectory of the share root's "
2542 "mount point (%s). "
2543 "Disabling basedir\n",
2544 basedir, config->mount_point));
2550 if (config->snapdirseverywhere && basedir != NULL) {
2551 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
2552 "with 'snapdirseverywhere'. Disabling basedir.\n"));
2556 snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
2557 "snapsharepath", NULL);
2558 if (snapsharepath != NULL) {
2559 if (snapsharepath[0] == '/') {
2560 DBG_WARNING("Warning: 'snapsharepath' is "
2561 "absolute ('%s'), but it has to be a "
2562 "relative path. Disabling snapsharepath.\n",
2564 snapsharepath = NULL;
2566 if (config->snapdirseverywhere && snapsharepath != NULL) {
2567 DBG_WARNING("Warning: 'snapsharepath' is incompatible "
2568 "with 'snapdirseverywhere'. Disabling "
2569 "snapsharepath.\n");
2570 snapsharepath = NULL;
2574 if (basedir != NULL && snapsharepath != NULL) {
2575 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
2576 "'basedir'. Disabling snapsharepath\n");
2577 snapsharepath = NULL;
2580 if (snapsharepath != NULL) {
2581 config->rel_connectpath = talloc_strdup(config, snapsharepath);
2582 if (config->rel_connectpath == NULL) {
2583 DBG_ERR("talloc_strdup() failed\n");
2589 if (basedir == NULL) {
2590 basedir = config->mount_point;
2593 if (config->rel_connectpath == NULL &&
2594 strlen(basedir) < strlen(handle->conn->connectpath)) {
2595 config->rel_connectpath = talloc_strdup(config,
2596 handle->conn->connectpath + strlen(basedir));
2597 if (config->rel_connectpath == NULL) {
2598 DEBUG(0, ("talloc_strdup() failed\n"));
2604 if (config->snapdir[0] == '/') {
2605 config->snapdir_absolute = true;
2607 if (config->snapdirseverywhere == true) {
2608 DEBUG(1, (__location__ " Warning: An absolute snapdir "
2609 "is incompatible with 'snapdirseverywhere', "
2610 "setting 'snapdirseverywhere' to false.\n"));
2611 config->snapdirseverywhere = false;
2614 if (config->crossmountpoints == true) {
2615 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
2616 "is not supported with an absolute snapdir. "
2617 "Disabling it.\n"));
2618 config->crossmountpoints = false;
2621 config->snapshot_basepath = config->snapdir;
2623 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
2624 config->mount_point, config->snapdir);
2625 if (config->snapshot_basepath == NULL) {
2626 DEBUG(0, ("talloc_asprintf() failed\n"));
2632 trim_string(config->mount_point, NULL, "/");
2633 trim_string(config->rel_connectpath, "/", "/");
2634 trim_string(config->snapdir, NULL, "/");
2635 trim_string(config->snapshot_basepath, NULL, "/");
2637 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
2638 " share root: '%s'\n"
2639 " mountpoint: '%s'\n"
2640 " rel share root: '%s'\n"
2642 " snapprefix: '%s'\n"
2643 " delimiter: '%s'\n"
2644 " snapshot base path: '%s'\n"
2647 " snapdirs everywhere: %s\n"
2648 " cross mountpoints: %s\n"
2652 handle->conn->connectpath,
2653 config->mount_point,
2654 config->rel_connectpath,
2658 config->snapshot_basepath,
2660 config->use_sscanf ? "yes" : "no",
2661 config->snapdirseverywhere ? "yes" : "no",
2662 config->crossmountpoints ? "yes" : "no",
2663 config->fixinodes ? "yes" : "no",
2668 SMB_VFS_HANDLE_SET_DATA(handle, priv,
2669 NULL, struct shadow_copy2_private,
2675 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
2676 .connect_fn = shadow_copy2_connect,
2677 .opendir_fn = shadow_copy2_opendir,
2678 .disk_free_fn = shadow_copy2_disk_free,
2679 .get_quota_fn = shadow_copy2_get_quota,
2680 .rename_fn = shadow_copy2_rename,
2681 .link_fn = shadow_copy2_link,
2682 .symlink_fn = shadow_copy2_symlink,
2683 .stat_fn = shadow_copy2_stat,
2684 .lstat_fn = shadow_copy2_lstat,
2685 .fstat_fn = shadow_copy2_fstat,
2686 .open_fn = shadow_copy2_open,
2687 .unlink_fn = shadow_copy2_unlink,
2688 .chmod_fn = shadow_copy2_chmod,
2689 .chown_fn = shadow_copy2_chown,
2690 .chdir_fn = shadow_copy2_chdir,
2691 .ntimes_fn = shadow_copy2_ntimes,
2692 .readlink_fn = shadow_copy2_readlink,
2693 .mknod_fn = shadow_copy2_mknod,
2694 .realpath_fn = shadow_copy2_realpath,
2695 .get_nt_acl_fn = shadow_copy2_get_nt_acl,
2696 .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
2697 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
2698 .mkdir_fn = shadow_copy2_mkdir,
2699 .rmdir_fn = shadow_copy2_rmdir,
2700 .getxattr_fn = shadow_copy2_getxattr,
2701 .listxattr_fn = shadow_copy2_listxattr,
2702 .removexattr_fn = shadow_copy2_removexattr,
2703 .setxattr_fn = shadow_copy2_setxattr,
2704 .chmod_acl_fn = shadow_copy2_chmod_acl,
2705 .chflags_fn = shadow_copy2_chflags,
2706 .get_real_filename_fn = shadow_copy2_get_real_filename,
2707 .connectpath_fn = shadow_copy2_connectpath,
2710 NTSTATUS vfs_shadow_copy2_init(void);
2711 NTSTATUS vfs_shadow_copy2_init(void)
2713 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2714 "shadow_copy2", &vfs_shadow_copy2_fns);