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;
83 static int shadow_copy2_get_shadow_copy_data(
84 vfs_handle_struct *handle, files_struct *fsp,
85 struct shadow_copy_data *shadow_copy2_data,
89 *This function will create a new snapshot list entry and
90 * return to the caller. This entry will also be added to
91 * the global snapshot list.
93 * @param[in] priv shadow_copy2 specific data structure
94 * @return Newly created snapshot entry or NULL on failure
96 static struct shadow_copy2_snapentry *shadow_copy2_create_snapentry(
97 struct shadow_copy2_private *priv)
99 struct shadow_copy2_snapentry *tmpentry = NULL;
101 tmpentry = talloc_zero(priv->snaps, struct shadow_copy2_snapentry);
102 if (tmpentry == NULL) {
103 DBG_ERR("talloc_zero() failed\n");
108 DLIST_ADD(priv->snaps->snaplist, tmpentry);
114 *This function will delete the entire snaplist and reset
115 * priv->snaps->snaplist to NULL.
117 * @param[in] priv shadow_copye specific data structure
119 static void shadow_copy2_delete_snaplist(struct shadow_copy2_private *priv)
121 struct shadow_copy2_snapentry *tmp = NULL;
123 while ((tmp = priv->snaps->snaplist) != NULL) {
124 DLIST_REMOVE(priv->snaps->snaplist, tmp);
130 * Given a timestamp this function searches the global snapshot list
131 * and returns the complete snapshot directory name saved in the entry.
133 * @param[in] priv shadow_copy2 specific structure
134 * @param[in] timestamp timestamp corresponding to one of the snapshot
135 * @param[out] snap_str buffer to copy the actual snapshot name
136 * @param[in] len length of snap_str buffer
138 * @return Length of actual snapshot name, and -1 on failure
140 static ssize_t shadow_copy2_saved_snapname(struct shadow_copy2_private *priv,
141 struct tm *timestamp,
142 char *snap_str, size_t len)
144 ssize_t snaptime_len = -1;
145 struct shadow_copy2_snapentry *entry = NULL;
147 snaptime_len = strftime(snap_str, len, GMT_FORMAT, timestamp);
148 if (snaptime_len == 0) {
149 DBG_ERR("strftime failed\n");
155 for (entry = priv->snaps->snaplist; entry; entry = entry->next) {
156 if (strcmp(entry->time_fmt, snap_str) == 0) {
157 snaptime_len = snprintf(snap_str, len, "%s",
169 * This function will check if snaplist is updated or not. If snaplist
170 * is empty then it will create a new list. Each time snaplist is updated
171 * the time is recorded. If the snapshot time is greater than the snaplist
172 * update time then chances are we are working on an older list. Then discard
173 * the old list and fetch a new snaplist.
175 * @param[in] handle VFS handle struct
176 * @param[in] snap_time time of snapshot
178 * @return true if the list is updated else false
180 static bool shadow_copy2_update_snaplist(struct vfs_handle_struct *handle,
184 bool snaplist_updated = false;
185 struct files_struct fsp = {0};
186 struct smb_filename smb_fname = {0};
187 double seconds = 0.0;
188 struct shadow_copy2_private *priv = NULL;
190 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
193 seconds = difftime(snap_time, priv->snaps->fetch_time);
196 * Fetch the snapshot list if either the snaplist is empty or the
197 * required snapshot time is greater than the last fetched snaplist
200 if (seconds > 0 || (priv->snaps->snaplist == NULL)) {
201 smb_fname.base_name = discard_const_p(char, ".");
202 fsp.fsp_name = &smb_fname;
204 ret = shadow_copy2_get_shadow_copy_data(handle, &fsp,
207 snaplist_updated = true;
209 DBG_ERR("Failed to get shadow copy data\n");
214 return snaplist_updated;
217 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
219 unsigned *pnum_offsets)
221 unsigned num_offsets;
228 while ((p = strchr(p, '/')) != NULL) {
233 offsets = talloc_array(mem_ctx, size_t, num_offsets);
234 if (offsets == NULL) {
240 while ((p = strchr(p, '/')) != NULL) {
241 offsets[num_offsets] = p-str;
247 *pnum_offsets = num_offsets;
252 * Given a timestamp, build the posix level GMT-tag string
253 * based on the configurable format.
255 static ssize_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
257 char *snaptime_string,
261 ssize_t snaptime_len;
262 struct shadow_copy2_config *config;
263 struct shadow_copy2_private *priv;
265 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
268 config = priv->config;
270 if (config->use_sscanf) {
271 snaptime_len = snprintf(snaptime_string,
274 (unsigned long)snapshot);
275 if (snaptime_len <= 0) {
276 DEBUG(10, ("snprintf failed\n"));
280 if (config->use_localtime) {
281 if (localtime_r(&snapshot, &snap_tm) == 0) {
282 DEBUG(10, ("gmtime_r failed\n"));
286 if (gmtime_r(&snapshot, &snap_tm) == 0) {
287 DEBUG(10, ("gmtime_r failed\n"));
292 if (priv->snaps->regex != NULL) {
293 snaptime_len = shadow_copy2_saved_snapname(priv,
294 &snap_tm, snaptime_string, len);
295 if (snaptime_len >= 0)
299 * If we fail to find the snapshot name, chances are
300 * that we have not updated our snaplist. Make sure the
301 * snaplist is updated.
303 if (!shadow_copy2_update_snaplist(handle, snapshot)) {
304 DBG_DEBUG("shadow_copy2_update_snaplist "
309 return shadow_copy2_saved_snapname(priv,
310 &snap_tm, snaptime_string, len);
313 snaptime_len = strftime(snaptime_string,
317 if (snaptime_len == 0) {
318 DEBUG(10, ("strftime failed\n"));
327 * Given a timestamp, build the string to insert into a path
328 * as a path component for creating the local path to the
329 * snapshot at the given timestamp of the input path.
331 * In the case of a parallel snapdir (specified with an
332 * absolute path), this is the inital portion of the
333 * local path of any snapshot file. The complete path is
334 * obtained by appending the portion of the file's path
335 * below the share root's mountpoint.
337 static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
338 struct vfs_handle_struct *handle,
341 fstring snaptime_string;
342 ssize_t snaptime_len = 0;
344 struct shadow_copy2_config *config;
345 struct shadow_copy2_private *priv;
347 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
350 config = priv->config;
352 snaptime_len = shadow_copy2_posix_gmt_string(handle,
355 sizeof(snaptime_string));
356 if (snaptime_len <= 0) {
360 if (config->snapdir_absolute) {
361 result = talloc_asprintf(mem_ctx, "%s/%s",
362 config->snapdir, snaptime_string);
364 result = talloc_asprintf(mem_ctx, "/%s/%s",
365 config->snapdir, snaptime_string);
367 if (result == NULL) {
368 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
375 * Build the posix snapshot path for the connection
376 * at the given timestamp, i.e. the absolute posix path
377 * that contains the snapshot for this file system.
379 * This only applies to classical case, i.e. not
380 * to the "snapdirseverywhere" mode.
382 static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
383 struct vfs_handle_struct *handle,
386 fstring snaptime_string;
387 ssize_t snaptime_len = 0;
389 struct shadow_copy2_private *priv;
391 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
394 snaptime_len = shadow_copy2_posix_gmt_string(handle,
397 sizeof(snaptime_string));
398 if (snaptime_len <= 0) {
402 result = talloc_asprintf(mem_ctx, "%s/%s",
403 priv->config->snapshot_basepath, snaptime_string);
404 if (result == NULL) {
405 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
411 static char *make_path_absolute(TALLOC_CTX *mem_ctx,
412 struct shadow_copy2_private *priv,
415 char *newpath = NULL;
416 char *abs_path = NULL;
418 if (name[0] != '/') {
419 newpath = talloc_asprintf(mem_ctx,
423 if (newpath == NULL) {
428 abs_path = canonicalize_absolute_path(mem_ctx, name);
429 TALLOC_FREE(newpath);
433 /* Return a $cwd-relative path. */
434 static bool make_relative_path(const char *cwd, char *abs_path)
436 size_t cwd_len = strlen(cwd);
437 size_t abs_len = strlen(abs_path);
439 if (abs_len < cwd_len) {
442 if (memcmp(abs_path, cwd, cwd_len) != 0) {
445 if (abs_path[cwd_len] != '/' && abs_path[cwd_len] != '\0') {
448 if (abs_path[cwd_len] == '/') {
451 memmove(abs_path, &abs_path[cwd_len], abs_len + 1 - cwd_len);
456 * Strip a snapshot component from a filename as
457 * handed in via the smb layer.
458 * Returns the parsed timestamp and the stripped filename.
460 static bool shadow_copy2_strip_snapshot_internal(TALLOC_CTX *mem_ctx,
461 struct vfs_handle_struct *handle,
462 const char *orig_name,
468 time_t timestamp = 0;
471 char *stripped = NULL;
472 size_t rest_len, dst_len;
473 struct shadow_copy2_private *priv;
476 ptrdiff_t len_before_gmt;
477 const char *name = orig_name;
479 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
482 DEBUG(10, (__location__ ": enter path '%s'\n", name));
484 p = strstr_m(name, "@GMT-");
486 DEBUG(11, ("@GMT not found\n"));
489 if ((p > name) && (p[-1] != '/')) {
490 /* the GMT-token does not start a path-component */
491 DEBUG(10, ("not at start, p=%p, name=%p, p[-1]=%d\n",
492 p, name, (int)p[-1]));
497 * Figure out whether we got an already converted string. One
498 * case where this happens is in a smb2 create call with the
499 * mxac create blob set. We do the get_acl call on
500 * fsp->fsp_name, which is already converted. We are converted
501 * if we got a file name of the form ".snapshots/@GMT-",
502 * i.e. ".snapshots/" precedes "p".
505 snapdir = lp_parm_const_string(SNUM(handle->conn), "shadow", "snapdir",
507 snapdirlen = strlen(snapdir);
508 len_before_gmt = p - name;
510 if ((len_before_gmt >= (snapdirlen + 1)) && (p[-1] == '/')) {
511 const char *parent_snapdir = p - (snapdirlen+1);
513 DEBUG(10, ("parent_snapdir = %s\n", parent_snapdir));
515 if (strncmp(parent_snapdir, snapdir, snapdirlen) == 0) {
516 DEBUG(10, ("name=%s is already converted\n", name));
520 q = strptime(p, GMT_FORMAT, &tm);
522 DEBUG(10, ("strptime failed\n"));
526 timestamp = timegm(&tm);
527 if (timestamp == (time_t)-1) {
528 DEBUG(10, ("timestamp==-1\n"));
533 * The name consists of only the GMT token or the GMT
534 * token is at the end of the path. XP seems to send
535 * @GMT- at the end under certain circumstances even
536 * with a path prefix.
538 if (pstripped != NULL) {
539 if (len_before_gmt > 0) {
541 * There is a slash before
542 * the @GMT-. Remove it.
546 stripped = talloc_strndup(mem_ctx, name,
548 if (stripped == NULL) {
551 *pstripped = stripped;
553 *ptimestamp = timestamp;
558 * It is not a complete path component, i.e. the path
559 * component continues after the gmt-token.
561 DEBUG(10, ("q[0] = %d\n", (int)q[0]));
566 rest_len = strlen(q);
567 dst_len = len_before_gmt + rest_len;
569 if (priv->config->snapdirseverywhere) {
572 insert = shadow_copy2_insert_string(talloc_tos(), handle,
574 if (insert == NULL) {
579 DEBUG(10, (__location__ ": snapdirseverywhere mode.\n"
581 "insert string '%s'\n", name, insert));
583 have_insert = (strstr(name, insert+1) != NULL);
584 DEBUG(10, ("have_insert=%d, name=%s, insert+1=%s\n",
585 (int)have_insert, name, insert+1));
587 DEBUG(10, (__location__ ": insert string '%s' found in "
588 "path '%s' found in snapdirseverywhere mode "
589 "==> already converted\n", insert, name));
598 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
601 if (snapshot_path == NULL) {
606 DEBUG(10, (__location__ " path: '%s'.\n"
607 "snapshot path: '%s'\n", name, snapshot_path));
609 s = strstr(name, snapshot_path);
612 * this starts with "snapshot_basepath/GMT-Token"
613 * so it is already a converted absolute
614 * path. Don't process further.
616 DEBUG(10, (__location__ ": path '%s' starts with "
617 "snapshot path '%s' (not in "
618 "snapdirseverywhere mode) ==> "
619 "already converted\n", name, snapshot_path));
620 talloc_free(snapshot_path);
623 talloc_free(snapshot_path);
626 if (pstripped != NULL) {
627 stripped = talloc_array(mem_ctx, char, dst_len+1);
628 if (stripped == NULL) {
633 memcpy(stripped, name, len_before_gmt);
636 memcpy(stripped + len_before_gmt, q, rest_len);
638 stripped[dst_len] = '\0';
639 *pstripped = stripped;
641 *ptimestamp = timestamp;
648 static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
649 struct vfs_handle_struct *handle,
650 const char *orig_name,
654 return shadow_copy2_strip_snapshot_internal(mem_ctx,
662 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
663 vfs_handle_struct *handle)
665 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
670 if (stat(path, &st) != 0) {
677 while ((p = strrchr(path, '/')) && p > path) {
679 if (stat(path, &st) != 0) {
683 if (st.st_dev != dev) {
693 * Convert from a name as handed in via the SMB layer
694 * and a timestamp into the local path of the snapshot
695 * of the provided file at the provided time.
696 * Also return the path in the snapshot corresponding
697 * to the file's share root.
699 static char *shadow_copy2_do_convert(TALLOC_CTX *mem_ctx,
700 struct vfs_handle_struct *handle,
701 const char *name, time_t timestamp,
702 size_t *snaproot_len)
704 struct smb_filename converted_fname;
706 size_t *slashes = NULL;
707 unsigned num_slashes;
711 char *converted = NULL;
712 size_t insertlen, connectlen = 0;
715 struct shadow_copy2_config *config;
716 struct shadow_copy2_private *priv;
717 size_t in_share_offset = 0;
719 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
722 config = priv->config;
724 DEBUG(10, ("converting '%s'\n", name));
726 if (!config->snapdirseverywhere) {
730 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
733 if (snapshot_path == NULL) {
737 if (config->rel_connectpath == NULL) {
738 converted = talloc_asprintf(mem_ctx, "%s/%s",
739 snapshot_path, name);
741 converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
743 config->rel_connectpath,
746 if (converted == NULL) {
750 ZERO_STRUCT(converted_fname);
751 converted_fname.base_name = converted;
753 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
754 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
756 ret, ret == 0 ? "ok" : strerror(errno)));
758 DEBUG(10, ("Found %s\n", converted));
761 if (snaproot_len != NULL) {
762 *snaproot_len = strlen(snapshot_path);
763 if (config->rel_connectpath != NULL) {
765 strlen(config->rel_connectpath) + 1;
773 /* never reached ... */
776 connectlen = strlen(handle->conn->connectpath);
778 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
780 path = talloc_asprintf(
781 mem_ctx, "%s/%s", handle->conn->connectpath, name);
787 pathlen = talloc_get_size(path)-1;
789 if (!shadow_copy2_find_slashes(talloc_tos(), path,
790 &slashes, &num_slashes)) {
794 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
795 if (insert == NULL) {
798 insertlen = talloc_get_size(insert)-1;
801 * Note: We deliberatly don't expensively initialize the
802 * array with talloc_zero here: Putting zero into
803 * converted[pathlen+insertlen] below is sufficient, because
804 * in the following for loop, the insert string is inserted
805 * at various slash places. So the memory up to position
806 * pathlen+insertlen will always be initialized when the
807 * converted string is used.
809 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
810 if (converted == NULL) {
814 if (path[pathlen-1] != '/') {
816 * Append a fake slash to find the snapshot root
819 tmp = talloc_realloc(talloc_tos(), slashes,
820 size_t, num_slashes+1);
825 slashes[num_slashes] = pathlen;
831 if (!config->crossmountpoints) {
832 min_offset = strlen(config->mount_point);
835 memcpy(converted, path, pathlen+1);
836 converted[pathlen+insertlen] = '\0';
838 ZERO_STRUCT(converted_fname);
839 converted_fname.base_name = converted;
841 for (i = num_slashes-1; i>=0; i--) {
847 if (offset < min_offset) {
852 if (offset >= connectlen) {
853 in_share_offset = offset;
856 memcpy(converted+offset, insert, insertlen);
859 memcpy(converted+offset, path + slashes[i],
860 pathlen - slashes[i]);
862 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
864 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
866 ret, ret == 0 ? "ok" : strerror(errno)));
869 if (snaproot_len != NULL) {
870 *snaproot_len = in_share_offset + insertlen;
874 if (errno == ENOTDIR) {
876 * This is a valid condition: We appended the
877 * .snaphots/@GMT.. to a file name. Just try
878 * with the upper levels.
882 if (errno != ENOENT) {
883 /* Other problem than "not found" */
892 DEBUG(10, ("Found %s\n", converted));
900 TALLOC_FREE(converted);
902 TALLOC_FREE(slashes);
909 * Convert from a name as handed in via the SMB layer
910 * and a timestamp into the local path of the snapshot
911 * of the provided file at the provided time.
913 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
914 struct vfs_handle_struct *handle,
915 const char *name, time_t timestamp)
917 return shadow_copy2_do_convert(mem_ctx, handle, name, timestamp, NULL);
921 modify a sbuf return to ensure that inodes in the shadow directory
922 are different from those in the main directory
924 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
925 SMB_STRUCT_STAT *sbuf)
927 struct shadow_copy2_private *priv;
929 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
932 if (priv->config->fixinodes) {
933 /* some snapshot systems, like GPFS, return the name
934 device:inode for the snapshot files as the current
935 files. That breaks the 'restore' button in the shadow copy
936 GUI, as the client gets a sharing violation.
938 This is a crude way of allowing both files to be
939 open at once. It has a slight chance of inode
940 number collision, but I can't see a better approach
941 without significant VFS changes
943 TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
944 .dsize = strlen(fname) };
947 shash = tdb_jenkins_hash(&key) & 0xFF000000;
951 sbuf->st_ex_ino ^= shash;
955 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
956 const struct smb_filename *smb_fname,
960 time_t timestamp = 0;
961 char *stripped = NULL;
965 struct smb_filename *conv_smb_fname = NULL;
967 if (!shadow_copy2_strip_snapshot(talloc_tos(),
969 smb_fname->base_name,
974 if (timestamp == 0) {
975 return SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
977 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
978 TALLOC_FREE(stripped);
982 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
987 if (conv_smb_fname == NULL) {
991 ret = SMB_VFS_NEXT_OPENDIR(handle, conv_smb_fname, mask, attr);
994 TALLOC_FREE(conv_smb_fname);
999 static int shadow_copy2_rename(vfs_handle_struct *handle,
1000 const struct smb_filename *smb_fname_src,
1001 const struct smb_filename *smb_fname_dst)
1003 time_t timestamp_src = 0;
1004 time_t timestamp_dst = 0;
1006 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1007 smb_fname_src->base_name,
1008 ×tamp_src, NULL)) {
1011 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1012 smb_fname_dst->base_name,
1013 ×tamp_dst, NULL)) {
1016 if (timestamp_src != 0) {
1020 if (timestamp_dst != 0) {
1024 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
1027 static int shadow_copy2_symlink(vfs_handle_struct *handle,
1028 const char *oldname, const char *newname)
1030 time_t timestamp_old = 0;
1031 time_t timestamp_new = 0;
1033 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
1034 ×tamp_old, NULL)) {
1037 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
1038 ×tamp_new, NULL)) {
1041 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1045 return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
1048 static int shadow_copy2_link(vfs_handle_struct *handle,
1049 const char *oldname, const char *newname)
1051 time_t timestamp_old = 0;
1052 time_t timestamp_new = 0;
1054 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
1055 ×tamp_old, NULL)) {
1058 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
1059 ×tamp_new, NULL)) {
1062 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1066 return SMB_VFS_NEXT_LINK(handle, oldname, newname);
1069 static int shadow_copy2_stat(vfs_handle_struct *handle,
1070 struct smb_filename *smb_fname)
1072 time_t timestamp = 0;
1073 char *stripped = NULL;
1075 int ret, saved_errno;
1077 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1078 smb_fname->base_name,
1079 ×tamp, &stripped)) {
1082 if (timestamp == 0) {
1083 return SMB_VFS_NEXT_STAT(handle, smb_fname);
1086 tmp = smb_fname->base_name;
1087 smb_fname->base_name = shadow_copy2_convert(
1088 talloc_tos(), handle, stripped, timestamp);
1089 TALLOC_FREE(stripped);
1091 if (smb_fname->base_name == NULL) {
1092 smb_fname->base_name = tmp;
1096 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1097 saved_errno = errno;
1099 TALLOC_FREE(smb_fname->base_name);
1100 smb_fname->base_name = tmp;
1103 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1105 errno = saved_errno;
1109 static int shadow_copy2_lstat(vfs_handle_struct *handle,
1110 struct smb_filename *smb_fname)
1112 time_t timestamp = 0;
1113 char *stripped = NULL;
1115 int ret, saved_errno;
1117 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1118 smb_fname->base_name,
1119 ×tamp, &stripped)) {
1122 if (timestamp == 0) {
1123 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1126 tmp = smb_fname->base_name;
1127 smb_fname->base_name = shadow_copy2_convert(
1128 talloc_tos(), handle, stripped, timestamp);
1129 TALLOC_FREE(stripped);
1131 if (smb_fname->base_name == NULL) {
1132 smb_fname->base_name = tmp;
1136 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1137 saved_errno = errno;
1139 TALLOC_FREE(smb_fname->base_name);
1140 smb_fname->base_name = tmp;
1143 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1145 errno = saved_errno;
1149 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
1150 SMB_STRUCT_STAT *sbuf)
1152 time_t timestamp = 0;
1155 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1159 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1160 fsp->fsp_name->base_name,
1161 ×tamp, NULL)) {
1164 if (timestamp != 0) {
1165 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
1170 static int shadow_copy2_open(vfs_handle_struct *handle,
1171 struct smb_filename *smb_fname, files_struct *fsp,
1172 int flags, mode_t mode)
1174 time_t timestamp = 0;
1175 char *stripped = NULL;
1177 int ret, saved_errno;
1179 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1180 smb_fname->base_name,
1181 ×tamp, &stripped)) {
1184 if (timestamp == 0) {
1185 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1188 tmp = smb_fname->base_name;
1189 smb_fname->base_name = shadow_copy2_convert(
1190 talloc_tos(), handle, stripped, timestamp);
1191 TALLOC_FREE(stripped);
1193 if (smb_fname->base_name == NULL) {
1194 smb_fname->base_name = tmp;
1198 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1199 saved_errno = errno;
1201 TALLOC_FREE(smb_fname->base_name);
1202 smb_fname->base_name = tmp;
1204 errno = saved_errno;
1208 static int shadow_copy2_unlink(vfs_handle_struct *handle,
1209 const struct smb_filename *smb_fname)
1211 time_t timestamp = 0;
1212 char *stripped = NULL;
1213 int ret, saved_errno;
1214 struct smb_filename *conv;
1216 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1217 smb_fname->base_name,
1218 ×tamp, &stripped)) {
1221 if (timestamp == 0) {
1222 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1224 conv = cp_smb_filename(talloc_tos(), smb_fname);
1229 conv->base_name = shadow_copy2_convert(
1230 conv, handle, stripped, timestamp);
1231 TALLOC_FREE(stripped);
1232 if (conv->base_name == NULL) {
1235 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
1236 saved_errno = errno;
1238 errno = saved_errno;
1242 static int shadow_copy2_chmod(vfs_handle_struct *handle,
1243 const struct smb_filename *smb_fname,
1246 time_t timestamp = 0;
1247 char *stripped = NULL;
1248 int ret, saved_errno;
1250 struct smb_filename *conv_smb_fname;
1252 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1254 smb_fname->base_name,
1259 if (timestamp == 0) {
1260 TALLOC_FREE(stripped);
1261 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1263 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1264 TALLOC_FREE(stripped);
1268 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1273 if (conv_smb_fname == NULL) {
1279 ret = SMB_VFS_NEXT_CHMOD(handle, conv_smb_fname, mode);
1280 saved_errno = errno;
1282 TALLOC_FREE(conv_smb_fname);
1283 errno = saved_errno;
1287 static int shadow_copy2_chown(vfs_handle_struct *handle,
1288 const struct smb_filename *smb_fname,
1292 time_t timestamp = 0;
1293 char *stripped = NULL;
1294 int ret, saved_errno;
1296 struct smb_filename *conv_smb_fname = NULL;
1298 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1300 smb_fname->base_name,
1305 if (timestamp == 0) {
1306 return SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
1308 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1309 TALLOC_FREE(stripped);
1313 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1318 if (conv_smb_fname == NULL) {
1323 ret = SMB_VFS_NEXT_CHOWN(handle, conv_smb_fname, uid, gid);
1324 saved_errno = errno;
1326 TALLOC_FREE(conv_smb_fname);
1327 errno = saved_errno;
1331 static int shadow_copy2_chdir(vfs_handle_struct *handle,
1334 time_t timestamp = 0;
1335 char *stripped = NULL;
1336 int ret, saved_errno;
1339 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1340 ×tamp, &stripped)) {
1343 if (timestamp == 0) {
1344 return SMB_VFS_NEXT_CHDIR(handle, fname);
1346 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1347 TALLOC_FREE(stripped);
1351 ret = SMB_VFS_NEXT_CHDIR(handle, conv);
1352 saved_errno = errno;
1354 errno = saved_errno;
1358 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
1359 const struct smb_filename *smb_fname,
1360 struct smb_file_time *ft)
1362 time_t timestamp = 0;
1363 char *stripped = NULL;
1364 int ret, saved_errno;
1365 struct smb_filename *conv;
1367 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1368 smb_fname->base_name,
1369 ×tamp, &stripped)) {
1372 if (timestamp == 0) {
1373 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1375 conv = cp_smb_filename(talloc_tos(), smb_fname);
1380 conv->base_name = shadow_copy2_convert(
1381 conv, handle, stripped, timestamp);
1382 TALLOC_FREE(stripped);
1383 if (conv->base_name == NULL) {
1386 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
1387 saved_errno = errno;
1389 errno = saved_errno;
1393 static int shadow_copy2_readlink(vfs_handle_struct *handle,
1394 const char *fname, char *buf, size_t bufsiz)
1396 time_t timestamp = 0;
1397 char *stripped = NULL;
1398 int ret, saved_errno;
1401 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1402 ×tamp, &stripped)) {
1405 if (timestamp == 0) {
1406 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
1408 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1409 TALLOC_FREE(stripped);
1413 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1414 saved_errno = errno;
1416 errno = saved_errno;
1420 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1421 const char *fname, mode_t mode, SMB_DEV_T dev)
1423 time_t timestamp = 0;
1424 char *stripped = NULL;
1425 int ret, saved_errno;
1428 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1429 ×tamp, &stripped)) {
1432 if (timestamp == 0) {
1433 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
1435 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1436 TALLOC_FREE(stripped);
1440 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1441 saved_errno = errno;
1443 errno = saved_errno;
1447 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
1450 time_t timestamp = 0;
1451 char *stripped = NULL;
1453 char *result = NULL;
1456 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1457 ×tamp, &stripped)) {
1460 if (timestamp == 0) {
1461 return SMB_VFS_NEXT_REALPATH(handle, fname);
1464 tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1469 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1472 saved_errno = errno;
1474 TALLOC_FREE(stripped);
1475 errno = saved_errno;
1480 * Check whether a given directory contains a
1481 * snapshot directory as direct subdirectory.
1482 * If yes, return the path of the snapshot-subdir,
1483 * otherwise return NULL.
1485 static char *have_snapdir(struct vfs_handle_struct *handle,
1488 struct smb_filename smb_fname;
1490 struct shadow_copy2_private *priv;
1492 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1495 ZERO_STRUCT(smb_fname);
1496 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1497 path, priv->config->snapdir);
1498 if (smb_fname.base_name == NULL) {
1502 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1503 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1504 return smb_fname.base_name;
1506 TALLOC_FREE(smb_fname.base_name);
1510 static bool check_access_snapdir(struct vfs_handle_struct *handle,
1513 struct smb_filename smb_fname;
1517 ZERO_STRUCT(smb_fname);
1518 smb_fname.base_name = talloc_asprintf(talloc_tos(),
1521 if (smb_fname.base_name == NULL) {
1525 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1526 if (ret != 0 || !S_ISDIR(smb_fname.st.st_ex_mode)) {
1527 TALLOC_FREE(smb_fname.base_name);
1531 status = smbd_check_access_rights(handle->conn,
1535 if (!NT_STATUS_IS_OK(status)) {
1536 DEBUG(0,("user does not have list permission "
1538 smb_fname.base_name));
1539 TALLOC_FREE(smb_fname.base_name);
1542 TALLOC_FREE(smb_fname.base_name);
1547 * Find the snapshot directory (if any) for the given
1548 * filename (which is relative to the share).
1550 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1551 struct vfs_handle_struct *handle,
1552 struct smb_filename *smb_fname)
1555 const char *snapdir;
1556 struct shadow_copy2_config *config;
1557 struct shadow_copy2_private *priv;
1559 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1562 config = priv->config;
1565 * If the non-snapdisrseverywhere mode, we should not search!
1567 if (!config->snapdirseverywhere) {
1568 return config->snapshot_basepath;
1571 path = talloc_asprintf(mem_ctx, "%s/%s",
1572 handle->conn->connectpath,
1573 smb_fname->base_name);
1578 snapdir = have_snapdir(handle, path);
1579 if (snapdir != NULL) {
1584 while ((p = strrchr(path, '/')) && (p > path)) {
1588 snapdir = have_snapdir(handle, path);
1589 if (snapdir != NULL) {
1598 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1600 char *gmt, size_t gmt_len)
1602 struct tm timestamp;
1604 unsigned long int timestamp_long;
1606 struct shadow_copy2_config *config;
1607 struct shadow_copy2_private *priv;
1608 char *tmpstr = NULL;
1610 bool converted = false;
1613 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1616 config = priv->config;
1618 fmt = config->gmt_format;
1621 * If regex is provided, then we will have to parse the
1622 * filename which will contain both the prefix and the time format.
1623 * e.g. <prefix><delimiter><time_format>
1625 if (priv->snaps->regex != NULL) {
1626 tmpstr = talloc_strdup(talloc_tos(), name);
1627 /* point "name" to the time format */
1628 name = strstr(name, priv->config->delimiter);
1632 /* Extract the prefix */
1633 tmp = strstr(tmpstr, priv->config->delimiter);
1637 ret = regexec(priv->snaps->regex, tmpstr, 0, NULL, 0);
1639 DBG_DEBUG("shadow_copy2_snapshot_to_gmt: "
1640 "no regex match for %s\n", tmpstr);
1645 ZERO_STRUCT(timestamp);
1646 if (config->use_sscanf) {
1647 if (sscanf(name, fmt, ×tamp_long) != 1) {
1648 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1649 "no sscanf match %s: %s\n",
1653 timestamp_t = timestamp_long;
1654 gmtime_r(×tamp_t, ×tamp);
1656 if (strptime(name, fmt, ×tamp) == NULL) {
1657 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1658 "no match %s: %s\n",
1662 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1665 if (config->use_localtime) {
1666 timestamp.tm_isdst = -1;
1667 timestamp_t = mktime(×tamp);
1668 gmtime_r(×tamp_t, ×tamp);
1672 strftime(gmt, gmt_len, GMT_FORMAT, ×tamp);
1676 TALLOC_FREE(tmpstr);
1680 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1682 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1685 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1687 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1691 sort the shadow copy data in ascending or descending order
1693 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1694 struct shadow_copy_data *shadow_copy2_data)
1696 int (*cmpfunc)(const void *, const void *);
1698 struct shadow_copy2_private *priv;
1700 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1703 sort = priv->config->sort_order;
1708 if (strcmp(sort, "asc") == 0) {
1709 cmpfunc = shadow_copy2_label_cmp_asc;
1710 } else if (strcmp(sort, "desc") == 0) {
1711 cmpfunc = shadow_copy2_label_cmp_desc;
1716 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1717 shadow_copy2_data->labels)
1719 TYPESAFE_QSORT(shadow_copy2_data->labels,
1720 shadow_copy2_data->num_volumes,
1725 static int shadow_copy2_get_shadow_copy_data(
1726 vfs_handle_struct *handle, files_struct *fsp,
1727 struct shadow_copy_data *shadow_copy2_data,
1731 const char *snapdir;
1732 struct smb_filename *snapdir_smb_fname = NULL;
1734 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1735 struct shadow_copy2_private *priv = NULL;
1736 struct shadow_copy2_snapentry *tmpentry = NULL;
1737 bool get_snaplist = false;
1738 bool access_granted = false;
1741 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1742 if (snapdir == NULL) {
1743 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1744 handle->conn->connectpath));
1749 access_granted = check_access_snapdir(handle, snapdir);
1750 if (!access_granted) {
1751 DEBUG(0,("access denied on listing snapdir %s\n", snapdir));
1756 snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
1760 fsp->fsp_name->flags);
1761 if (snapdir_smb_fname == NULL) {
1766 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir_smb_fname, NULL, 0);
1769 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1770 " - %s\n", snapdir, strerror(errno)));
1775 if (shadow_copy2_data != NULL) {
1776 shadow_copy2_data->num_volumes = 0;
1777 shadow_copy2_data->labels = NULL;
1780 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1784 * Normally this function is called twice once with labels = false and
1785 * then with labels = true. When labels is false it will return the
1786 * number of volumes so that the caller can allocate memory for that
1787 * many labels. Therefore to eliminate snaplist both the times it is
1788 * good to check if labels is set or not.
1790 * shadow_copy2_data is NULL when we only want to update the list and
1791 * don't want any labels.
1793 if ((priv->snaps->regex != NULL) && (labels || shadow_copy2_data == NULL)) {
1794 get_snaplist = true;
1795 /* Reset the global snaplist */
1796 shadow_copy2_delete_snaplist(priv);
1798 /* Set the current time as snaplist update time */
1799 time(&(priv->snaps->fetch_time));
1802 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
1803 char snapshot[GMT_NAME_LEN+1];
1804 SHADOW_COPY_LABEL *tlabels;
1807 * ignore names not of the right form in the snapshot
1810 if (!shadow_copy2_snapshot_to_gmt(
1812 snapshot, sizeof(snapshot))) {
1814 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1815 "ignoring %s\n", d->d_name));
1818 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1819 d->d_name, snapshot));
1823 * Create a snap entry for each successful
1826 tmpentry = shadow_copy2_create_snapentry(priv);
1827 if (tmpentry == NULL) {
1828 DBG_ERR("talloc_zero() failed\n");
1831 tmpentry->snapname = talloc_strdup(tmpentry, d->d_name);
1832 tmpentry->time_fmt = talloc_strdup(tmpentry, snapshot);
1835 if (shadow_copy2_data == NULL) {
1840 /* the caller doesn't want the labels */
1841 shadow_copy2_data->num_volumes++;
1845 tlabels = talloc_realloc(shadow_copy2_data,
1846 shadow_copy2_data->labels,
1848 shadow_copy2_data->num_volumes+1);
1849 if (tlabels == NULL) {
1850 DEBUG(0,("shadow_copy2: out of memory\n"));
1851 SMB_VFS_NEXT_CLOSEDIR(handle, p);
1855 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
1858 shadow_copy2_data->num_volumes++;
1859 shadow_copy2_data->labels = tlabels;
1862 SMB_VFS_NEXT_CLOSEDIR(handle,p);
1864 shadow_copy2_sort_data(handle, shadow_copy2_data);
1868 TALLOC_FREE(tmp_ctx);
1872 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
1873 struct files_struct *fsp,
1874 uint32_t security_info,
1875 TALLOC_CTX *mem_ctx,
1876 struct security_descriptor **ppdesc)
1878 time_t timestamp = 0;
1879 char *stripped = NULL;
1882 struct smb_filename *smb_fname = NULL;
1884 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1885 fsp->fsp_name->base_name,
1886 ×tamp, &stripped)) {
1887 return map_nt_error_from_unix(errno);
1889 if (timestamp == 0) {
1890 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1894 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1895 TALLOC_FREE(stripped);
1897 return map_nt_error_from_unix(errno);
1899 smb_fname = synthetic_smb_fname(talloc_tos(),
1903 fsp->fsp_name->flags);
1904 if (smb_fname == NULL) {
1906 return NT_STATUS_NO_MEMORY;
1909 status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1912 TALLOC_FREE(smb_fname);
1916 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
1917 const struct smb_filename *smb_fname,
1918 uint32_t security_info,
1919 TALLOC_CTX *mem_ctx,
1920 struct security_descriptor **ppdesc)
1922 time_t timestamp = 0;
1923 char *stripped = NULL;
1926 struct smb_filename *conv_smb_fname = NULL;
1928 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1930 smb_fname->base_name,
1933 return map_nt_error_from_unix(errno);
1935 if (timestamp == 0) {
1936 return SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1939 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1940 TALLOC_FREE(stripped);
1942 return map_nt_error_from_unix(errno);
1944 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1949 if (conv_smb_fname == NULL) {
1951 return NT_STATUS_NO_MEMORY;
1953 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv_smb_fname, security_info,
1956 TALLOC_FREE(conv_smb_fname);
1960 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
1961 const struct smb_filename *smb_fname,
1964 time_t timestamp = 0;
1965 char *stripped = NULL;
1966 int ret, saved_errno;
1968 struct smb_filename *conv_smb_fname = NULL;
1970 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1972 smb_fname->base_name,
1977 if (timestamp == 0) {
1978 return SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
1980 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1981 TALLOC_FREE(stripped);
1985 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1990 if (conv_smb_fname == NULL) {
1994 ret = SMB_VFS_NEXT_MKDIR(handle, conv_smb_fname, mode);
1995 saved_errno = errno;
1997 TALLOC_FREE(conv_smb_fname);
1998 errno = saved_errno;
2002 static int shadow_copy2_rmdir(vfs_handle_struct *handle,
2003 const struct smb_filename *smb_fname)
2005 time_t timestamp = 0;
2006 char *stripped = NULL;
2007 int ret, saved_errno;
2009 struct smb_filename *conv_smb_fname = NULL;
2011 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2013 smb_fname->base_name,
2018 if (timestamp == 0) {
2019 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
2021 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2022 TALLOC_FREE(stripped);
2026 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2031 if (conv_smb_fname == NULL) {
2035 ret = SMB_VFS_NEXT_RMDIR(handle, conv_smb_fname);
2036 saved_errno = errno;
2037 TALLOC_FREE(conv_smb_fname);
2039 errno = saved_errno;
2043 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
2046 time_t timestamp = 0;
2047 char *stripped = NULL;
2048 int ret, saved_errno;
2051 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2052 ×tamp, &stripped)) {
2055 if (timestamp == 0) {
2056 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
2058 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2059 TALLOC_FREE(stripped);
2063 ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
2064 saved_errno = errno;
2066 errno = saved_errno;
2070 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
2071 const char *fname, const char *aname,
2072 void *value, size_t size)
2074 time_t timestamp = 0;
2075 char *stripped = NULL;
2080 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2081 ×tamp, &stripped)) {
2084 if (timestamp == 0) {
2085 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
2088 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2089 TALLOC_FREE(stripped);
2093 ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
2094 saved_errno = errno;
2096 errno = saved_errno;
2100 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
2102 char *list, size_t size)
2104 time_t timestamp = 0;
2105 char *stripped = NULL;
2110 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2111 ×tamp, &stripped)) {
2114 if (timestamp == 0) {
2115 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
2117 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2118 TALLOC_FREE(stripped);
2122 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
2123 saved_errno = errno;
2125 errno = saved_errno;
2129 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
2130 const char *fname, const char *aname)
2132 time_t timestamp = 0;
2133 char *stripped = NULL;
2134 int ret, saved_errno;
2137 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2138 ×tamp, &stripped)) {
2141 if (timestamp == 0) {
2142 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
2144 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2145 TALLOC_FREE(stripped);
2149 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
2150 saved_errno = errno;
2152 errno = saved_errno;
2156 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
2158 const char *aname, const void *value,
2159 size_t size, int flags)
2161 time_t timestamp = 0;
2162 char *stripped = NULL;
2167 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2168 ×tamp, &stripped)) {
2171 if (timestamp == 0) {
2172 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
2175 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2176 TALLOC_FREE(stripped);
2180 ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
2181 saved_errno = errno;
2183 errno = saved_errno;
2187 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
2188 const struct smb_filename *smb_fname,
2191 time_t timestamp = 0;
2192 char *stripped = NULL;
2196 struct smb_filename *conv_smb_fname = NULL;
2198 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2200 smb_fname->base_name,
2205 if (timestamp == 0) {
2206 return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
2208 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2209 TALLOC_FREE(stripped);
2213 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2218 if (conv_smb_fname == NULL) {
2223 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv_smb_fname, mode);
2224 saved_errno = errno;
2226 TALLOC_FREE(conv_smb_fname);
2227 errno = saved_errno;
2231 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
2234 TALLOC_CTX *mem_ctx,
2237 time_t timestamp = 0;
2238 char *stripped = NULL;
2243 DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
2244 "name=[%s]\n", path, name));
2246 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
2247 ×tamp, &stripped)) {
2248 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
2251 if (timestamp == 0) {
2252 DEBUG(10, ("timestamp == 0\n"));
2253 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
2254 mem_ctx, found_name);
2256 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2257 TALLOC_FREE(stripped);
2259 DEBUG(10, ("shadow_copy2_convert failed\n"));
2262 DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
2263 "name=[%s]\n", conv, name));
2264 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
2265 mem_ctx, found_name);
2266 DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
2267 saved_errno = errno;
2269 errno = saved_errno;
2273 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
2276 time_t timestamp = 0;
2277 char *stripped = NULL;
2279 char *result = NULL;
2280 char *parent_dir = NULL;
2282 size_t rootpath_len = 0;
2284 DBG_DEBUG("Calc connect path for [%s]\n", fname);
2286 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2287 ×tamp, &stripped)) {
2290 if (timestamp == 0) {
2291 return SMB_VFS_NEXT_CONNECTPATH(handle, fname);
2294 tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
2297 if (errno != ENOENT) {
2302 * If the converted path does not exist, and converting
2303 * the parent yields something that does exist, then
2304 * this path refers to something that has not been
2305 * created yet, relative to the parent path.
2306 * The snapshot finding is relative to the parent.
2307 * (usually snapshots are read/only but this is not
2308 * necessarily true).
2309 * This code also covers getting a wildcard in the
2310 * last component, because this function is called
2311 * prior to sanitizing the path, and in SMB1 we may
2312 * get wildcards in path names.
2314 if (!parent_dirname(talloc_tos(), stripped, &parent_dir,
2320 tmp = shadow_copy2_do_convert(talloc_tos(), handle, parent_dir,
2321 timestamp, &rootpath_len);
2327 DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
2328 (int)rootpath_len, tmp);
2330 tmp[rootpath_len] = '\0';
2331 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
2332 if (result == NULL) {
2336 DBG_DEBUG("connect path is [%s]\n", result);
2339 saved_errno = errno;
2341 TALLOC_FREE(stripped);
2342 TALLOC_FREE(parent_dir);
2343 errno = saved_errno;
2347 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
2348 const char *path, uint64_t *bsize,
2349 uint64_t *dfree, uint64_t *dsize)
2351 time_t timestamp = 0;
2352 char *stripped = NULL;
2357 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
2358 ×tamp, &stripped)) {
2361 if (timestamp == 0) {
2362 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2363 bsize, dfree, dsize);
2366 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2367 TALLOC_FREE(stripped);
2372 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, bsize, dfree, dsize);
2374 saved_errno = errno;
2376 errno = saved_errno;
2381 static int shadow_copy2_get_quota(vfs_handle_struct *handle, const char *path,
2382 enum SMB_QUOTA_TYPE qtype, unid_t id,
2385 time_t timestamp = 0;
2386 char *stripped = NULL;
2391 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path, ×tamp,
2395 if (timestamp == 0) {
2396 return SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, dq);
2399 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2400 TALLOC_FREE(stripped);
2405 ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv, qtype, id, dq);
2407 saved_errno = errno;
2409 errno = saved_errno;
2414 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
2415 const char *service, const char *user)
2417 struct shadow_copy2_config *config;
2418 struct shadow_copy2_private *priv;
2420 const char *snapdir;
2421 const char *snapprefix = NULL;
2422 const char *delimiter;
2423 const char *gmt_format;
2424 const char *sort_order;
2425 const char *basedir = NULL;
2426 const char *snapsharepath = NULL;
2427 const char *mount_point;
2429 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
2430 (unsigned)handle->conn->cnum,
2431 handle->conn->connectpath));
2433 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
2438 priv = talloc_zero(handle->conn, struct shadow_copy2_private);
2440 DBG_ERR("talloc_zero() failed\n");
2445 priv->snaps = talloc_zero(priv, struct shadow_copy2_snaplist_info);
2446 if (priv->snaps == NULL) {
2447 DBG_ERR("talloc_zero() failed\n");
2452 config = talloc_zero(priv, struct shadow_copy2_config);
2453 if (config == NULL) {
2454 DEBUG(0, ("talloc_zero() failed\n"));
2459 priv->config = config;
2461 gmt_format = lp_parm_const_string(SNUM(handle->conn),
2464 config->gmt_format = talloc_strdup(config, gmt_format);
2465 if (config->gmt_format == NULL) {
2466 DEBUG(0, ("talloc_strdup() failed\n"));
2471 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
2472 "shadow", "sscanf", false);
2474 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
2475 "shadow", "localtime",
2478 snapdir = lp_parm_const_string(SNUM(handle->conn),
2479 "shadow", "snapdir",
2481 config->snapdir = talloc_strdup(config, snapdir);
2482 if (config->snapdir == NULL) {
2483 DEBUG(0, ("talloc_strdup() failed\n"));
2488 snapprefix = lp_parm_const_string(SNUM(handle->conn),
2489 "shadow", "snapprefix",
2491 if (snapprefix != NULL) {
2492 priv->snaps->regex = talloc_zero(priv->snaps, regex_t);
2493 if (priv->snaps->regex == NULL) {
2494 DBG_ERR("talloc_zero() failed\n");
2499 /* pre-compute regex rule for matching pattern later */
2500 ret = regcomp(priv->snaps->regex, snapprefix, 0);
2502 DBG_ERR("Failed to create regex object\n");
2507 delimiter = lp_parm_const_string(SNUM(handle->conn),
2508 "shadow", "delimiter",
2510 if (delimiter != NULL) {
2511 priv->config->delimiter = talloc_strdup(priv->config, delimiter);
2512 if (priv->config->delimiter == NULL) {
2513 DBG_ERR("talloc_strdup() failed\n");
2519 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
2521 "snapdirseverywhere",
2524 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
2525 "shadow", "crossmountpoints",
2528 if (config->crossmountpoints && !config->snapdirseverywhere) {
2529 DBG_WARNING("Warning: 'crossmountpoints' depends on "
2530 "'snapdirseverywhere'. Disabling crossmountpoints.\n");
2533 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
2534 "shadow", "fixinodes",
2537 sort_order = lp_parm_const_string(SNUM(handle->conn),
2538 "shadow", "sort", "desc");
2539 config->sort_order = talloc_strdup(config, sort_order);
2540 if (config->sort_order == NULL) {
2541 DEBUG(0, ("talloc_strdup() failed\n"));
2546 mount_point = lp_parm_const_string(SNUM(handle->conn),
2547 "shadow", "mountpoint", NULL);
2548 if (mount_point != NULL) {
2549 if (mount_point[0] != '/') {
2550 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
2551 "relative ('%s'), but it has to be an "
2552 "absolute path. Ignoring provided value.\n",
2557 p = strstr(handle->conn->connectpath, mount_point);
2558 if (p != handle->conn->connectpath) {
2559 DBG_WARNING("Warning: the share root (%s) is "
2560 "not a subdirectory of the "
2561 "specified mountpoint (%s). "
2562 "Ignoring provided value.\n",
2563 handle->conn->connectpath,
2570 if (mount_point != NULL) {
2571 config->mount_point = talloc_strdup(config, mount_point);
2572 if (config->mount_point == NULL) {
2573 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
2577 config->mount_point = shadow_copy2_find_mount_point(config,
2579 if (config->mount_point == NULL) {
2580 DBG_WARNING("shadow_copy2_find_mount_point "
2581 "of the share root '%s' failed: %s\n",
2582 handle->conn->connectpath, strerror(errno));
2587 basedir = lp_parm_const_string(SNUM(handle->conn),
2588 "shadow", "basedir", NULL);
2590 if (basedir != NULL) {
2591 if (basedir[0] != '/') {
2592 DEBUG(1, (__location__ " Warning: 'basedir' is "
2593 "relative ('%s'), but it has to be an "
2594 "absolute path. Disabling basedir.\n",
2599 p = strstr(basedir, config->mount_point);
2601 DEBUG(1, ("Warning: basedir (%s) is not a "
2602 "subdirectory of the share root's "
2603 "mount point (%s). "
2604 "Disabling basedir\n",
2605 basedir, config->mount_point));
2611 if (config->snapdirseverywhere && basedir != NULL) {
2612 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
2613 "with 'snapdirseverywhere'. Disabling basedir.\n"));
2617 snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
2618 "snapsharepath", NULL);
2619 if (snapsharepath != NULL) {
2620 if (snapsharepath[0] == '/') {
2621 DBG_WARNING("Warning: 'snapsharepath' is "
2622 "absolute ('%s'), but it has to be a "
2623 "relative path. Disabling snapsharepath.\n",
2625 snapsharepath = NULL;
2627 if (config->snapdirseverywhere && snapsharepath != NULL) {
2628 DBG_WARNING("Warning: 'snapsharepath' is incompatible "
2629 "with 'snapdirseverywhere'. Disabling "
2630 "snapsharepath.\n");
2631 snapsharepath = NULL;
2635 if (basedir != NULL && snapsharepath != NULL) {
2636 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
2637 "'basedir'. Disabling snapsharepath\n");
2638 snapsharepath = NULL;
2641 if (snapsharepath != NULL) {
2642 config->rel_connectpath = talloc_strdup(config, snapsharepath);
2643 if (config->rel_connectpath == NULL) {
2644 DBG_ERR("talloc_strdup() failed\n");
2650 if (basedir == NULL) {
2651 basedir = config->mount_point;
2654 if (config->rel_connectpath == NULL &&
2655 strlen(basedir) < strlen(handle->conn->connectpath)) {
2656 config->rel_connectpath = talloc_strdup(config,
2657 handle->conn->connectpath + strlen(basedir));
2658 if (config->rel_connectpath == NULL) {
2659 DEBUG(0, ("talloc_strdup() failed\n"));
2665 if (config->snapdir[0] == '/') {
2666 config->snapdir_absolute = true;
2668 if (config->snapdirseverywhere == true) {
2669 DEBUG(1, (__location__ " Warning: An absolute snapdir "
2670 "is incompatible with 'snapdirseverywhere', "
2671 "setting 'snapdirseverywhere' to false.\n"));
2672 config->snapdirseverywhere = false;
2675 if (config->crossmountpoints == true) {
2676 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
2677 "is not supported with an absolute snapdir. "
2678 "Disabling it.\n"));
2679 config->crossmountpoints = false;
2682 config->snapshot_basepath = config->snapdir;
2684 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
2685 config->mount_point, config->snapdir);
2686 if (config->snapshot_basepath == NULL) {
2687 DEBUG(0, ("talloc_asprintf() failed\n"));
2693 trim_string(config->mount_point, NULL, "/");
2694 trim_string(config->rel_connectpath, "/", "/");
2695 trim_string(config->snapdir, NULL, "/");
2696 trim_string(config->snapshot_basepath, NULL, "/");
2698 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
2699 " share root: '%s'\n"
2700 " mountpoint: '%s'\n"
2701 " rel share root: '%s'\n"
2703 " snapprefix: '%s'\n"
2704 " delimiter: '%s'\n"
2705 " snapshot base path: '%s'\n"
2708 " snapdirs everywhere: %s\n"
2709 " cross mountpoints: %s\n"
2713 handle->conn->connectpath,
2714 config->mount_point,
2715 config->rel_connectpath,
2719 config->snapshot_basepath,
2721 config->use_sscanf ? "yes" : "no",
2722 config->snapdirseverywhere ? "yes" : "no",
2723 config->crossmountpoints ? "yes" : "no",
2724 config->fixinodes ? "yes" : "no",
2729 SMB_VFS_HANDLE_SET_DATA(handle, priv,
2730 NULL, struct shadow_copy2_private,
2736 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
2737 .connect_fn = shadow_copy2_connect,
2738 .opendir_fn = shadow_copy2_opendir,
2739 .disk_free_fn = shadow_copy2_disk_free,
2740 .get_quota_fn = shadow_copy2_get_quota,
2741 .rename_fn = shadow_copy2_rename,
2742 .link_fn = shadow_copy2_link,
2743 .symlink_fn = shadow_copy2_symlink,
2744 .stat_fn = shadow_copy2_stat,
2745 .lstat_fn = shadow_copy2_lstat,
2746 .fstat_fn = shadow_copy2_fstat,
2747 .open_fn = shadow_copy2_open,
2748 .unlink_fn = shadow_copy2_unlink,
2749 .chmod_fn = shadow_copy2_chmod,
2750 .chown_fn = shadow_copy2_chown,
2751 .chdir_fn = shadow_copy2_chdir,
2752 .ntimes_fn = shadow_copy2_ntimes,
2753 .readlink_fn = shadow_copy2_readlink,
2754 .mknod_fn = shadow_copy2_mknod,
2755 .realpath_fn = shadow_copy2_realpath,
2756 .get_nt_acl_fn = shadow_copy2_get_nt_acl,
2757 .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
2758 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
2759 .mkdir_fn = shadow_copy2_mkdir,
2760 .rmdir_fn = shadow_copy2_rmdir,
2761 .getxattr_fn = shadow_copy2_getxattr,
2762 .listxattr_fn = shadow_copy2_listxattr,
2763 .removexattr_fn = shadow_copy2_removexattr,
2764 .setxattr_fn = shadow_copy2_setxattr,
2765 .chmod_acl_fn = shadow_copy2_chmod_acl,
2766 .chflags_fn = shadow_copy2_chflags,
2767 .get_real_filename_fn = shadow_copy2_get_real_filename,
2768 .connectpath_fn = shadow_copy2_connectpath,
2771 NTSTATUS vfs_shadow_copy2_init(void);
2772 NTSTATUS vfs_shadow_copy2_init(void)
2774 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2775 "shadow_copy2", &vfs_shadow_copy2_fns);