2 * shadow_copy2: a shadow copy module (second implementation)
4 * Copyright (C) Andrew Tridgell 2007 (portions taken from shadow_copy2)
5 * Copyright (C) Ed Plese 2009
6 * Copyright (C) Volker Lendecke 2011
7 * Copyright (C) Christian Ambach 2011
8 * Copyright (C) Michael Adam 2013
9 * Copyright (C) Rajesh Joseph 2016
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * This is a second implemetation of a shadow copy module for exposing
28 * file system snapshots to windows clients as shadow copies.
30 * See the manual page for documentation.
34 #include "smbd/smbd.h"
35 #include "system/filesys.h"
36 #include "include/ntioctl.h"
38 #include "lib/util_path.h"
39 #include "libcli/security/security.h"
40 #include "lib/util/tevent_unix.h"
42 struct shadow_copy2_config {
48 bool snapdirseverywhere;
49 bool crossmountpoints;
52 bool snapdir_absolute;
54 char *rel_connectpath; /* share root, relative to a snapshot root */
55 char *snapshot_basepath; /* the absolute version of snapdir */
58 /* Data-structure to hold the list of snap entries */
59 struct shadow_copy2_snapentry {
62 struct shadow_copy2_snapentry *next;
63 struct shadow_copy2_snapentry *prev;
66 struct shadow_copy2_snaplist_info {
67 struct shadow_copy2_snapentry *snaplist; /* snapshot list */
68 regex_t *regex; /* Regex to filter snaps */
69 time_t fetch_time; /* snaplist update time */
74 * shadow_copy2 private structure. This structure will be
75 * used to keep module specific information
77 struct shadow_copy2_private {
78 struct shadow_copy2_config *config;
79 struct shadow_copy2_snaplist_info *snaps;
80 char *shadow_cwd; /* Absolute $cwd path. */
81 /* Absolute connectpath - can vary depending on $cwd. */
82 char *shadow_connectpath;
83 /* talloc'ed realpath return. */
84 struct smb_filename *shadow_realpath;
87 static int shadow_copy2_get_shadow_copy_data(
88 vfs_handle_struct *handle, files_struct *fsp,
89 struct shadow_copy_data *shadow_copy2_data,
93 *This function will create a new snapshot list entry and
94 * return to the caller. This entry will also be added to
95 * the global snapshot list.
97 * @param[in] priv shadow_copy2 specific data structure
98 * @return Newly created snapshot entry or NULL on failure
100 static struct shadow_copy2_snapentry *shadow_copy2_create_snapentry(
101 struct shadow_copy2_private *priv)
103 struct shadow_copy2_snapentry *tmpentry = NULL;
105 tmpentry = talloc_zero(priv->snaps, struct shadow_copy2_snapentry);
106 if (tmpentry == NULL) {
107 DBG_ERR("talloc_zero() failed\n");
112 DLIST_ADD(priv->snaps->snaplist, tmpentry);
118 *This function will delete the entire snaplist and reset
119 * priv->snaps->snaplist to NULL.
121 * @param[in] priv shadow_copye specific data structure
123 static void shadow_copy2_delete_snaplist(struct shadow_copy2_private *priv)
125 struct shadow_copy2_snapentry *tmp = NULL;
127 while ((tmp = priv->snaps->snaplist) != NULL) {
128 DLIST_REMOVE(priv->snaps->snaplist, tmp);
134 * Given a timestamp this function searches the global snapshot list
135 * and returns the complete snapshot directory name saved in the entry.
137 * @param[in] priv shadow_copy2 specific structure
138 * @param[in] timestamp timestamp corresponding to one of the snapshot
139 * @param[out] snap_str buffer to copy the actual snapshot name
140 * @param[in] len length of snap_str buffer
142 * @return Length of actual snapshot name, and -1 on failure
144 static ssize_t shadow_copy2_saved_snapname(struct shadow_copy2_private *priv,
145 struct tm *timestamp,
146 char *snap_str, size_t len)
148 ssize_t snaptime_len = -1;
149 struct shadow_copy2_snapentry *entry = NULL;
151 snaptime_len = strftime(snap_str, len, GMT_FORMAT, timestamp);
152 if (snaptime_len == 0) {
153 DBG_ERR("strftime failed\n");
159 for (entry = priv->snaps->snaplist; entry; entry = entry->next) {
160 if (strcmp(entry->time_fmt, snap_str) == 0) {
161 snaptime_len = snprintf(snap_str, len, "%s",
173 * This function will check if snaplist is updated or not. If snaplist
174 * is empty then it will create a new list. Each time snaplist is updated
175 * the time is recorded. If the snapshot time is greater than the snaplist
176 * update time then chances are we are working on an older list. Then discard
177 * the old list and fetch a new snaplist.
179 * @param[in] handle VFS handle struct
180 * @param[in] snap_time time of snapshot
182 * @return true if the list is updated else false
184 static bool shadow_copy2_update_snaplist(struct vfs_handle_struct *handle,
188 bool snaplist_updated = false;
189 struct files_struct fsp = {0};
190 struct smb_filename smb_fname = {0};
191 double seconds = 0.0;
192 struct shadow_copy2_private *priv = NULL;
194 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
197 seconds = difftime(snap_time, priv->snaps->fetch_time);
200 * Fetch the snapshot list if either the snaplist is empty or the
201 * required snapshot time is greater than the last fetched snaplist
204 if (seconds > 0 || (priv->snaps->snaplist == NULL)) {
205 smb_fname.base_name = discard_const_p(char, ".");
206 fsp.fsp_name = &smb_fname;
208 ret = shadow_copy2_get_shadow_copy_data(handle, &fsp,
211 snaplist_updated = true;
213 DBG_ERR("Failed to get shadow copy data\n");
218 return snaplist_updated;
221 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
223 unsigned *pnum_offsets)
225 unsigned num_offsets;
232 while ((p = strchr(p, '/')) != NULL) {
237 offsets = talloc_array(mem_ctx, size_t, num_offsets);
238 if (offsets == NULL) {
244 while ((p = strchr(p, '/')) != NULL) {
245 offsets[num_offsets] = p-str;
251 *pnum_offsets = num_offsets;
256 * Given a timestamp, build the posix level GMT-tag string
257 * based on the configurable format.
259 static ssize_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
261 char *snaptime_string,
265 ssize_t snaptime_len;
266 struct shadow_copy2_config *config;
267 struct shadow_copy2_private *priv;
269 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
272 config = priv->config;
274 if (config->use_sscanf) {
275 snaptime_len = snprintf(snaptime_string,
278 (unsigned long)snapshot);
279 if (snaptime_len <= 0) {
280 DEBUG(10, ("snprintf failed\n"));
284 if (config->use_localtime) {
285 if (localtime_r(&snapshot, &snap_tm) == 0) {
286 DEBUG(10, ("gmtime_r failed\n"));
290 if (gmtime_r(&snapshot, &snap_tm) == 0) {
291 DEBUG(10, ("gmtime_r failed\n"));
296 if (priv->snaps->regex != NULL) {
297 snaptime_len = shadow_copy2_saved_snapname(priv,
298 &snap_tm, snaptime_string, len);
299 if (snaptime_len >= 0)
303 * If we fail to find the snapshot name, chances are
304 * that we have not updated our snaplist. Make sure the
305 * snaplist is updated.
307 if (!shadow_copy2_update_snaplist(handle, snapshot)) {
308 DBG_DEBUG("shadow_copy2_update_snaplist "
313 return shadow_copy2_saved_snapname(priv,
314 &snap_tm, snaptime_string, len);
317 snaptime_len = strftime(snaptime_string,
321 if (snaptime_len == 0) {
322 DEBUG(10, ("strftime failed\n"));
331 * Given a timestamp, build the string to insert into a path
332 * as a path component for creating the local path to the
333 * snapshot at the given timestamp of the input path.
335 * In the case of a parallel snapdir (specified with an
336 * absolute path), this is the initial portion of the
337 * local path of any snapshot file. The complete path is
338 * obtained by appending the portion of the file's path
339 * below the share root's mountpoint.
341 static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
342 struct vfs_handle_struct *handle,
345 fstring snaptime_string;
346 ssize_t snaptime_len = 0;
348 struct shadow_copy2_config *config;
349 struct shadow_copy2_private *priv;
351 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
354 config = priv->config;
356 snaptime_len = shadow_copy2_posix_gmt_string(handle,
359 sizeof(snaptime_string));
360 if (snaptime_len <= 0) {
364 if (config->snapdir_absolute) {
365 result = talloc_asprintf(mem_ctx, "%s/%s",
366 config->snapdir, snaptime_string);
368 result = talloc_asprintf(mem_ctx, "/%s/%s",
369 config->snapdir, snaptime_string);
371 if (result == NULL) {
372 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
379 * Build the posix snapshot path for the connection
380 * at the given timestamp, i.e. the absolute posix path
381 * that contains the snapshot for this file system.
383 * This only applies to classical case, i.e. not
384 * to the "snapdirseverywhere" mode.
386 static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
387 struct vfs_handle_struct *handle,
390 fstring snaptime_string;
391 ssize_t snaptime_len = 0;
393 struct shadow_copy2_private *priv;
395 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
398 snaptime_len = shadow_copy2_posix_gmt_string(handle,
401 sizeof(snaptime_string));
402 if (snaptime_len <= 0) {
406 result = talloc_asprintf(mem_ctx, "%s/%s",
407 priv->config->snapshot_basepath, snaptime_string);
408 if (result == NULL) {
409 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
415 static char *make_path_absolute(TALLOC_CTX *mem_ctx,
416 struct shadow_copy2_private *priv,
419 char *newpath = NULL;
420 char *abs_path = NULL;
422 if (name[0] != '/') {
423 newpath = talloc_asprintf(mem_ctx,
427 if (newpath == NULL) {
432 abs_path = canonicalize_absolute_path(mem_ctx, name);
433 TALLOC_FREE(newpath);
437 /* Return a $cwd-relative path. */
438 static bool make_relative_path(const char *cwd, char *abs_path)
440 size_t cwd_len = strlen(cwd);
441 size_t abs_len = strlen(abs_path);
443 if (abs_len < cwd_len) {
446 if (memcmp(abs_path, cwd, cwd_len) != 0) {
449 /* The cwd_len != 1 case is for $cwd == '/' */
451 abs_path[cwd_len] != '/' &&
452 abs_path[cwd_len] != '\0')
456 if (abs_path[cwd_len] == '/') {
459 memmove(abs_path, &abs_path[cwd_len], abs_len + 1 - cwd_len);
463 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
465 char *gmt, size_t gmt_len);
468 * Check if an incoming filename is already a snapshot converted pathname.
470 * If so, it returns the pathname truncated at the snapshot point which
471 * will be used as the connectpath.
474 static int check_for_converted_path(TALLOC_CTX *mem_ctx,
475 struct vfs_handle_struct *handle,
476 struct shadow_copy2_private *priv,
478 bool *ppath_already_converted,
481 size_t snapdirlen = 0;
482 char *p = strstr_m(abs_path, priv->config->snapdir);
484 char *connect_path = NULL;
485 char snapshot[GMT_NAME_LEN+1];
487 *ppath_already_converted = false;
490 /* Must at least contain shadow:snapdir. */
494 if (priv->config->snapdir[0] == '/' &&
496 /* Absolute shadow:snapdir must be at the start. */
500 snapdirlen = strlen(priv->config->snapdir);
501 if (p[snapdirlen] != '/') {
502 /* shadow:snapdir must end as a separate component. */
506 if (p > abs_path && p[-1] != '/') {
507 /* shadow:snapdir must start as a separate component. */
512 p++; /* Move past the / */
515 * Need to return up to the next path
516 * component after the time.
517 * This will be used as the connectpath.
522 * No next path component.
525 connect_path = talloc_strdup(mem_ctx,
528 connect_path = talloc_strndup(mem_ctx,
532 if (connect_path == NULL) {
537 * Point p at the same offset in connect_path as
541 p = &connect_path[p - abs_path];
544 * Now ensure there is a time string at p.
545 * The SMB-format @GMT-token string is returned
549 if (!shadow_copy2_snapshot_to_gmt(handle,
553 TALLOC_FREE(connect_path);
557 if (pconnectpath != NULL) {
558 *pconnectpath = connect_path;
561 *ppath_already_converted = true;
563 DBG_DEBUG("path |%s| is already converted. "
564 "connect path = |%s|\n",
572 * This function does two things.
574 * 1). Checks if an incoming filename is already a
575 * snapshot converted pathname.
576 * If so, it returns the pathname truncated
577 * at the snapshot point which will be used
578 * as the connectpath, and then does an early return.
580 * 2). Checks if an incoming filename contains an
581 * SMB-layer @GMT- style timestamp.
582 * If so, it strips the timestamp, and returns
583 * both the timestamp and the stripped path
584 * (making it cwd-relative).
587 static bool shadow_copy2_strip_snapshot_internal(TALLOC_CTX *mem_ctx,
588 struct vfs_handle_struct *handle,
589 const char *orig_name,
593 bool *_already_converted)
596 time_t timestamp = 0;
599 char *stripped = NULL;
600 size_t rest_len, dst_len;
601 struct shadow_copy2_private *priv;
602 ptrdiff_t len_before_gmt;
603 const char *name = orig_name;
604 char *abs_path = NULL;
606 bool already_converted = false;
609 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
612 DEBUG(10, (__location__ ": enter path '%s'\n", name));
614 if (_already_converted != NULL) {
615 *_already_converted = false;
618 abs_path = make_path_absolute(mem_ctx, priv, name);
619 if (abs_path == NULL) {
625 DEBUG(10, (__location__ ": abs path '%s'\n", name));
627 err = check_for_converted_path(mem_ctx,
634 /* error in conversion. */
639 if (already_converted) {
640 if (_already_converted != NULL) {
641 *_already_converted = true;
647 * From here we're only looking to strip an
648 * SMB-layer @GMT- token.
651 p = strstr_m(name, "@GMT-");
653 DEBUG(11, ("@GMT not found\n"));
656 if ((p > name) && (p[-1] != '/')) {
657 /* the GMT-token does not start a path-component */
658 DEBUG(10, ("not at start, p=%p, name=%p, p[-1]=%d\n",
659 p, name, (int)p[-1]));
663 len_before_gmt = p - name;
665 q = strptime(p, GMT_FORMAT, &tm);
667 DEBUG(10, ("strptime failed\n"));
671 timestamp = timegm(&tm);
672 if (timestamp == (time_t)-1) {
673 DEBUG(10, ("timestamp==-1\n"));
678 * The name consists of only the GMT token or the GMT
679 * token is at the end of the path. XP seems to send
680 * @GMT- at the end under certain circumstances even
681 * with a path prefix.
683 if (pstripped != NULL) {
684 if (len_before_gmt > 1) {
686 * There is a path (and not only a slash)
687 * before the @GMT-. Remove the trailing
692 stripped = talloc_strndup(mem_ctx, name,
694 if (stripped == NULL) {
698 if (orig_name[0] != '/') {
699 if (make_relative_path(priv->shadow_cwd,
700 stripped) == false) {
701 DEBUG(10, (__location__ ": path '%s' "
702 "doesn't start with cwd '%s'\n",
703 stripped, priv->shadow_cwd));
709 *pstripped = stripped;
711 *ptimestamp = timestamp;
716 * It is not a complete path component, i.e. the path
717 * component continues after the gmt-token.
719 DEBUG(10, ("q[0] = %d\n", (int)q[0]));
724 rest_len = strlen(q);
725 dst_len = len_before_gmt + rest_len;
727 if (pstripped != NULL) {
728 stripped = talloc_array(mem_ctx, char, dst_len+1);
729 if (stripped == NULL) {
734 memcpy(stripped, name, len_before_gmt);
737 memcpy(stripped + len_before_gmt, q, rest_len);
739 stripped[dst_len] = '\0';
740 if (orig_name[0] != '/') {
741 if (make_relative_path(priv->shadow_cwd,
742 stripped) == false) {
743 DEBUG(10, (__location__ ": path '%s' "
744 "doesn't start with cwd '%s'\n",
745 stripped, priv->shadow_cwd));
751 *pstripped = stripped;
753 *ptimestamp = timestamp;
757 TALLOC_FREE(abs_path);
761 static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
762 struct vfs_handle_struct *handle,
763 const char *orig_name,
767 return shadow_copy2_strip_snapshot_internal(mem_ctx,
776 static bool shadow_copy2_strip_snapshot_converted(TALLOC_CTX *mem_ctx,
777 struct vfs_handle_struct *handle,
778 const char *orig_name,
783 return shadow_copy2_strip_snapshot_internal(mem_ctx,
792 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
793 vfs_handle_struct *handle)
795 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
800 if (stat(path, &st) != 0) {
807 while ((p = strrchr(path, '/')) && p > path) {
809 if (stat(path, &st) != 0) {
813 if (st.st_dev != dev) {
823 * Convert from a name as handed in via the SMB layer
824 * and a timestamp into the local path of the snapshot
825 * of the provided file at the provided time.
826 * Also return the path in the snapshot corresponding
827 * to the file's share root.
829 static char *shadow_copy2_do_convert(TALLOC_CTX *mem_ctx,
830 struct vfs_handle_struct *handle,
831 const char *name, time_t timestamp,
832 size_t *snaproot_len)
834 struct smb_filename converted_fname;
836 size_t *slashes = NULL;
837 unsigned num_slashes;
841 char *converted = NULL;
842 size_t insertlen, connectlen = 0;
846 struct shadow_copy2_config *config;
847 struct shadow_copy2_private *priv;
848 size_t in_share_offset = 0;
850 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
853 config = priv->config;
855 DEBUG(10, ("converting '%s'\n", name));
857 if (!config->snapdirseverywhere) {
861 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
864 if (snapshot_path == NULL) {
868 if (config->rel_connectpath == NULL) {
869 converted = talloc_asprintf(mem_ctx, "%s/%s",
870 snapshot_path, name);
872 converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
874 config->rel_connectpath,
877 if (converted == NULL) {
881 ZERO_STRUCT(converted_fname);
882 converted_fname.base_name = converted;
884 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
885 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
887 ret, ret == 0 ? "ok" : strerror(errno)));
889 DEBUG(10, ("Found %s\n", converted));
892 if (snaproot_len != NULL) {
893 *snaproot_len = strlen(snapshot_path);
894 if (config->rel_connectpath != NULL) {
896 strlen(config->rel_connectpath) + 1;
904 /* never reached ... */
907 connectlen = strlen(handle->conn->connectpath);
909 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
911 path = talloc_asprintf(
912 mem_ctx, "%s/%s", handle->conn->connectpath, name);
918 pathlen = talloc_get_size(path)-1;
920 if (!shadow_copy2_find_slashes(talloc_tos(), path,
921 &slashes, &num_slashes)) {
925 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
926 if (insert == NULL) {
929 insertlen = talloc_get_size(insert)-1;
932 * Note: We deliberatly don't expensively initialize the
933 * array with talloc_zero here: Putting zero into
934 * converted[pathlen+insertlen] below is sufficient, because
935 * in the following for loop, the insert string is inserted
936 * at various slash places. So the memory up to position
937 * pathlen+insertlen will always be initialized when the
938 * converted string is used.
940 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
941 if (converted == NULL) {
945 if (path[pathlen-1] != '/') {
947 * Append a fake slash to find the snapshot root
950 tmp = talloc_realloc(talloc_tos(), slashes,
951 size_t, num_slashes+1);
956 slashes[num_slashes] = pathlen;
962 if (!config->crossmountpoints) {
963 min_offset = strlen(config->mount_point);
966 memcpy(converted, path, pathlen+1);
967 converted[pathlen+insertlen] = '\0';
969 ZERO_STRUCT(converted_fname);
970 converted_fname.base_name = converted;
972 for (i = num_slashes-1; i>=0; i--) {
978 if (offset < min_offset) {
983 if (offset >= connectlen) {
984 in_share_offset = offset;
987 memcpy(converted+offset, insert, insertlen);
990 memcpy(converted+offset, path + slashes[i],
991 pathlen - slashes[i]);
993 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
995 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
997 ret, ret == 0 ? "ok" : strerror(errno)));
1000 if (snaproot_len != NULL) {
1001 *snaproot_len = in_share_offset + insertlen;
1005 if (errno == ENOTDIR) {
1007 * This is a valid condition: We appended the
1008 * .snaphots/@GMT.. to a file name. Just try
1009 * with the upper levels.
1013 if (errno != ENOENT) {
1014 /* Other problem than "not found" */
1023 DEBUG(10, ("Found %s\n", converted));
1030 if (result == NULL) {
1031 saved_errno = errno;
1033 TALLOC_FREE(converted);
1034 TALLOC_FREE(insert);
1035 TALLOC_FREE(slashes);
1037 if (saved_errno != 0) {
1038 errno = saved_errno;
1044 * Convert from a name as handed in via the SMB layer
1045 * and a timestamp into the local path of the snapshot
1046 * of the provided file at the provided time.
1048 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
1049 struct vfs_handle_struct *handle,
1050 const char *name, time_t timestamp)
1052 return shadow_copy2_do_convert(mem_ctx, handle, name, timestamp, NULL);
1056 modify a sbuf return to ensure that inodes in the shadow directory
1057 are different from those in the main directory
1059 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
1060 SMB_STRUCT_STAT *sbuf)
1062 struct shadow_copy2_private *priv;
1064 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1067 if (priv->config->fixinodes) {
1068 /* some snapshot systems, like GPFS, return the name
1069 device:inode for the snapshot files as the current
1070 files. That breaks the 'restore' button in the shadow copy
1071 GUI, as the client gets a sharing violation.
1073 This is a crude way of allowing both files to be
1074 open at once. It has a slight chance of inode
1075 number collision, but I can't see a better approach
1076 without significant VFS changes
1078 TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
1079 .dsize = strlen(fname) };
1082 shash = tdb_jenkins_hash(&key) & 0xFF000000;
1086 sbuf->st_ex_ino ^= shash;
1090 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
1091 const struct smb_filename *smb_fname,
1095 time_t timestamp = 0;
1096 char *stripped = NULL;
1098 int saved_errno = 0;
1100 struct smb_filename *conv_smb_fname = NULL;
1102 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1104 smb_fname->base_name,
1109 if (timestamp == 0) {
1110 return SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
1112 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1113 TALLOC_FREE(stripped);
1117 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1122 if (conv_smb_fname == NULL) {
1126 ret = SMB_VFS_NEXT_OPENDIR(handle, conv_smb_fname, mask, attr);
1128 saved_errno = errno;
1131 TALLOC_FREE(conv_smb_fname);
1132 if (saved_errno != 0) {
1133 errno = saved_errno;
1138 static int shadow_copy2_rename(vfs_handle_struct *handle,
1139 const struct smb_filename *smb_fname_src,
1140 const struct smb_filename *smb_fname_dst)
1142 time_t timestamp_src = 0;
1143 time_t timestamp_dst = 0;
1144 char *snappath_src = NULL;
1145 char *snappath_dst = NULL;
1147 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle,
1148 smb_fname_src->base_name,
1149 ×tamp_src, NULL, &snappath_src,
1153 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle,
1154 smb_fname_dst->base_name,
1155 ×tamp_dst, NULL, &snappath_dst,
1159 if (timestamp_src != 0) {
1163 if (timestamp_dst != 0) {
1168 * Don't allow rename on already converted paths.
1170 if (snappath_src != NULL) {
1174 if (snappath_dst != NULL) {
1178 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
1181 static int shadow_copy2_symlink(vfs_handle_struct *handle,
1182 const char *link_contents,
1183 const struct smb_filename *new_smb_fname)
1185 time_t timestamp_old = 0;
1186 time_t timestamp_new = 0;
1187 char *snappath_old = NULL;
1188 char *snappath_new = NULL;
1190 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1199 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1201 new_smb_fname->base_name,
1208 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1213 * Don't allow symlinks on already converted paths.
1215 if ((snappath_old != NULL) || (snappath_new != NULL)) {
1219 return SMB_VFS_NEXT_SYMLINK(handle, link_contents, new_smb_fname);
1222 static int shadow_copy2_link(vfs_handle_struct *handle,
1223 const struct smb_filename *old_smb_fname,
1224 const struct smb_filename *new_smb_fname)
1226 time_t timestamp_old = 0;
1227 time_t timestamp_new = 0;
1228 char *snappath_old = NULL;
1229 char *snappath_new = NULL;
1231 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1233 old_smb_fname->base_name,
1240 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1242 new_smb_fname->base_name,
1249 if ((timestamp_old != 0) || (timestamp_new != 0)) {
1254 * Don't allow links on already converted paths.
1256 if ((snappath_old != NULL) || (snappath_new != NULL)) {
1260 return SMB_VFS_NEXT_LINK(handle, old_smb_fname, new_smb_fname);
1263 static int shadow_copy2_stat(vfs_handle_struct *handle,
1264 struct smb_filename *smb_fname)
1266 time_t timestamp = 0;
1267 char *stripped = NULL;
1269 int saved_errno = 0;
1272 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1273 smb_fname->base_name,
1274 ×tamp, &stripped)) {
1277 if (timestamp == 0) {
1278 return SMB_VFS_NEXT_STAT(handle, smb_fname);
1281 tmp = smb_fname->base_name;
1282 smb_fname->base_name = shadow_copy2_convert(
1283 talloc_tos(), handle, stripped, timestamp);
1284 TALLOC_FREE(stripped);
1286 if (smb_fname->base_name == NULL) {
1287 smb_fname->base_name = tmp;
1291 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1293 saved_errno = errno;
1296 TALLOC_FREE(smb_fname->base_name);
1297 smb_fname->base_name = tmp;
1300 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1302 if (saved_errno != 0) {
1303 errno = saved_errno;
1308 static int shadow_copy2_lstat(vfs_handle_struct *handle,
1309 struct smb_filename *smb_fname)
1311 time_t timestamp = 0;
1312 char *stripped = NULL;
1314 int saved_errno = 0;
1317 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1318 smb_fname->base_name,
1319 ×tamp, &stripped)) {
1322 if (timestamp == 0) {
1323 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1326 tmp = smb_fname->base_name;
1327 smb_fname->base_name = shadow_copy2_convert(
1328 talloc_tos(), handle, stripped, timestamp);
1329 TALLOC_FREE(stripped);
1331 if (smb_fname->base_name == NULL) {
1332 smb_fname->base_name = tmp;
1336 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1338 saved_errno = errno;
1341 TALLOC_FREE(smb_fname->base_name);
1342 smb_fname->base_name = tmp;
1345 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1347 if (saved_errno != 0) {
1348 errno = saved_errno;
1353 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
1354 SMB_STRUCT_STAT *sbuf)
1356 time_t timestamp = 0;
1357 struct smb_filename *orig_smb_fname = NULL;
1358 struct smb_filename vss_smb_fname;
1359 struct smb_filename *orig_base_smb_fname = NULL;
1360 struct smb_filename vss_base_smb_fname;
1361 char *stripped = NULL;
1362 int saved_errno = 0;
1366 ok = shadow_copy2_strip_snapshot(talloc_tos(), handle,
1367 fsp->fsp_name->base_name,
1368 ×tamp, &stripped);
1373 if (timestamp == 0) {
1374 TALLOC_FREE(stripped);
1375 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1378 vss_smb_fname = *fsp->fsp_name;
1379 vss_smb_fname.base_name = shadow_copy2_convert(talloc_tos(),
1383 TALLOC_FREE(stripped);
1384 if (vss_smb_fname.base_name == NULL) {
1388 orig_smb_fname = fsp->fsp_name;
1389 fsp->fsp_name = &vss_smb_fname;
1391 if (fsp->base_fsp != NULL) {
1392 vss_base_smb_fname = *fsp->base_fsp->fsp_name;
1393 vss_base_smb_fname.base_name = vss_smb_fname.base_name;
1394 orig_base_smb_fname = fsp->base_fsp->fsp_name;
1395 fsp->base_fsp->fsp_name = &vss_base_smb_fname;
1398 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1399 fsp->fsp_name = orig_smb_fname;
1400 if (fsp->base_fsp != NULL) {
1401 fsp->base_fsp->fsp_name = orig_base_smb_fname;
1404 saved_errno = errno;
1408 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
1410 if (saved_errno != 0) {
1411 errno = saved_errno;
1416 static int shadow_copy2_open(vfs_handle_struct *handle,
1417 struct smb_filename *smb_fname, files_struct *fsp,
1418 int flags, mode_t mode)
1420 time_t timestamp = 0;
1421 char *stripped = NULL;
1423 bool is_converted = false;
1424 int saved_errno = 0;
1427 if (!shadow_copy2_strip_snapshot_converted(talloc_tos(), handle,
1428 smb_fname->base_name,
1429 ×tamp, &stripped,
1433 if (timestamp == 0) {
1436 * Just pave over the user requested mode and use
1437 * O_RDONLY. Later attempts by the client to write on
1438 * the handle will fail in the pwrite() syscall with
1439 * EINVAL which we carefully map to EROFS. In sum, this
1440 * matches Windows behaviour.
1444 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1447 tmp = smb_fname->base_name;
1448 smb_fname->base_name = shadow_copy2_convert(
1449 talloc_tos(), handle, stripped, timestamp);
1450 TALLOC_FREE(stripped);
1452 if (smb_fname->base_name == NULL) {
1453 smb_fname->base_name = tmp;
1458 * Just pave over the user requested mode and use O_RDONLY. Later
1459 * attempts by the client to write on the handle will fail in the
1460 * pwrite() syscall with EINVAL which we carefully map to EROFS. In sum,
1461 * this matches Windows behaviour.
1465 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1467 saved_errno = errno;
1470 TALLOC_FREE(smb_fname->base_name);
1471 smb_fname->base_name = tmp;
1473 if (saved_errno != 0) {
1474 errno = saved_errno;
1479 static int shadow_copy2_unlink(vfs_handle_struct *handle,
1480 const struct smb_filename *smb_fname)
1482 time_t timestamp = 0;
1483 char *stripped = NULL;
1484 int saved_errno = 0;
1486 struct smb_filename *conv;
1488 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1489 smb_fname->base_name,
1490 ×tamp, &stripped)) {
1493 if (timestamp == 0) {
1494 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1496 conv = cp_smb_filename(talloc_tos(), smb_fname);
1501 conv->base_name = shadow_copy2_convert(
1502 conv, handle, stripped, timestamp);
1503 TALLOC_FREE(stripped);
1504 if (conv->base_name == NULL) {
1507 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
1509 saved_errno = errno;
1512 if (saved_errno != 0) {
1513 errno = saved_errno;
1518 static int shadow_copy2_chmod(vfs_handle_struct *handle,
1519 const struct smb_filename *smb_fname,
1522 time_t timestamp = 0;
1523 char *stripped = NULL;
1524 int saved_errno = 0;
1527 struct smb_filename *conv_smb_fname;
1529 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1531 smb_fname->base_name,
1536 if (timestamp == 0) {
1537 TALLOC_FREE(stripped);
1538 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1540 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1541 TALLOC_FREE(stripped);
1545 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1550 if (conv_smb_fname == NULL) {
1556 ret = SMB_VFS_NEXT_CHMOD(handle, conv_smb_fname, mode);
1558 saved_errno = errno;
1561 TALLOC_FREE(conv_smb_fname);
1562 if (saved_errno != 0) {
1563 errno = saved_errno;
1568 static int shadow_copy2_chown(vfs_handle_struct *handle,
1569 const struct smb_filename *smb_fname,
1573 time_t timestamp = 0;
1574 char *stripped = NULL;
1575 int saved_errno = 0;
1578 struct smb_filename *conv_smb_fname = NULL;
1580 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1582 smb_fname->base_name,
1587 if (timestamp == 0) {
1588 return SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
1590 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1591 TALLOC_FREE(stripped);
1595 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1600 if (conv_smb_fname == NULL) {
1605 ret = SMB_VFS_NEXT_CHOWN(handle, conv_smb_fname, uid, gid);
1607 saved_errno = errno;
1610 TALLOC_FREE(conv_smb_fname);
1611 if (saved_errno != 0) {
1612 errno = saved_errno;
1617 static void store_cwd_data(vfs_handle_struct *handle,
1618 const char *connectpath)
1620 struct shadow_copy2_private *priv = NULL;
1621 struct smb_filename *cwd_fname = NULL;
1623 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1626 TALLOC_FREE(priv->shadow_cwd);
1627 cwd_fname = SMB_VFS_NEXT_GETWD(handle, talloc_tos());
1628 if (cwd_fname == NULL) {
1629 smb_panic("getwd failed\n");
1631 DBG_DEBUG("shadow cwd = %s\n", cwd_fname->base_name);
1632 priv->shadow_cwd = talloc_strdup(priv, cwd_fname->base_name);
1633 TALLOC_FREE(cwd_fname);
1634 if (priv->shadow_cwd == NULL) {
1635 smb_panic("talloc failed\n");
1637 TALLOC_FREE(priv->shadow_connectpath);
1639 DBG_DEBUG("shadow conectpath = %s\n", connectpath);
1640 priv->shadow_connectpath = talloc_strdup(priv, connectpath);
1641 if (priv->shadow_connectpath == NULL) {
1642 smb_panic("talloc failed\n");
1647 static int shadow_copy2_chdir(vfs_handle_struct *handle,
1648 const struct smb_filename *smb_fname)
1650 time_t timestamp = 0;
1651 char *stripped = NULL;
1652 char *snappath = NULL;
1654 int saved_errno = 0;
1656 size_t rootpath_len = 0;
1657 struct smb_filename *conv_smb_fname = NULL;
1659 if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
1661 smb_fname->base_name,
1668 if (stripped != NULL) {
1669 conv = shadow_copy2_do_convert(talloc_tos(),
1674 TALLOC_FREE(stripped);
1678 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1684 conv_smb_fname = cp_smb_filename(talloc_tos(), smb_fname);
1687 if (conv_smb_fname == NULL) {
1693 ret = SMB_VFS_NEXT_CHDIR(handle, conv_smb_fname);
1695 saved_errno = errno;
1699 if (conv != NULL && rootpath_len != 0) {
1700 conv[rootpath_len] = '\0';
1701 } else if (snappath != 0) {
1705 store_cwd_data(handle, conv);
1708 TALLOC_FREE(stripped);
1710 TALLOC_FREE(conv_smb_fname);
1712 if (saved_errno != 0) {
1713 errno = saved_errno;
1718 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
1719 const struct smb_filename *smb_fname,
1720 struct smb_file_time *ft)
1722 time_t timestamp = 0;
1723 char *stripped = NULL;
1724 int saved_errno = 0;
1726 struct smb_filename *conv;
1728 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1729 smb_fname->base_name,
1730 ×tamp, &stripped)) {
1733 if (timestamp == 0) {
1734 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1736 conv = cp_smb_filename(talloc_tos(), smb_fname);
1741 conv->base_name = shadow_copy2_convert(
1742 conv, handle, stripped, timestamp);
1743 TALLOC_FREE(stripped);
1744 if (conv->base_name == NULL) {
1747 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
1749 saved_errno = errno;
1752 if (saved_errno != 0) {
1753 errno = saved_errno;
1758 static int shadow_copy2_readlink(vfs_handle_struct *handle,
1759 const struct smb_filename *smb_fname,
1763 time_t timestamp = 0;
1764 char *stripped = NULL;
1765 int saved_errno = 0;
1767 struct smb_filename *conv = NULL;
1769 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1770 smb_fname->base_name,
1771 ×tamp, &stripped)) {
1774 if (timestamp == 0) {
1775 return SMB_VFS_NEXT_READLINK(handle, smb_fname, buf, bufsiz);
1777 conv = cp_smb_filename(talloc_tos(), smb_fname);
1779 TALLOC_FREE(stripped);
1783 conv->base_name = shadow_copy2_convert(
1784 conv, handle, stripped, timestamp);
1785 TALLOC_FREE(stripped);
1786 if (conv->base_name == NULL) {
1789 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1791 saved_errno = errno;
1794 if (saved_errno != 0) {
1795 errno = saved_errno;
1800 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1801 const struct smb_filename *smb_fname,
1805 time_t timestamp = 0;
1806 char *stripped = NULL;
1807 int saved_errno = 0;
1809 struct smb_filename *conv = NULL;
1811 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1812 smb_fname->base_name,
1813 ×tamp, &stripped)) {
1816 if (timestamp == 0) {
1817 return SMB_VFS_NEXT_MKNOD(handle, smb_fname, mode, dev);
1819 conv = cp_smb_filename(talloc_tos(), smb_fname);
1824 conv->base_name = shadow_copy2_convert(
1825 conv, handle, stripped, timestamp);
1826 TALLOC_FREE(stripped);
1827 if (conv->base_name == NULL) {
1830 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1832 saved_errno = errno;
1835 if (saved_errno != 0) {
1836 errno = saved_errno;
1841 static struct smb_filename *shadow_copy2_realpath(vfs_handle_struct *handle,
1843 const struct smb_filename *smb_fname)
1845 time_t timestamp = 0;
1846 char *stripped = NULL;
1847 struct smb_filename *result_fname = NULL;
1848 struct smb_filename *conv_fname = NULL;
1849 int saved_errno = 0;
1851 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1852 smb_fname->base_name,
1853 ×tamp, &stripped)) {
1856 if (timestamp == 0) {
1857 return SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
1860 conv_fname = cp_smb_filename(talloc_tos(), smb_fname);
1861 if (conv_fname == NULL) {
1864 conv_fname->base_name = shadow_copy2_convert(
1865 conv_fname, handle, stripped, timestamp);
1866 if (conv_fname->base_name == NULL) {
1870 result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, conv_fname);
1873 if (result_fname == NULL) {
1874 saved_errno = errno;
1876 TALLOC_FREE(conv_fname);
1877 TALLOC_FREE(stripped);
1878 if (saved_errno != 0) {
1879 errno = saved_errno;
1881 return result_fname;
1885 * Check whether a given directory contains a
1886 * snapshot directory as direct subdirectory.
1887 * If yes, return the path of the snapshot-subdir,
1888 * otherwise return NULL.
1890 static char *have_snapdir(struct vfs_handle_struct *handle,
1893 struct smb_filename smb_fname;
1895 struct shadow_copy2_private *priv;
1897 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1900 ZERO_STRUCT(smb_fname);
1901 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1902 path, priv->config->snapdir);
1903 if (smb_fname.base_name == NULL) {
1907 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1908 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1909 return smb_fname.base_name;
1911 TALLOC_FREE(smb_fname.base_name);
1915 static bool check_access_snapdir(struct vfs_handle_struct *handle,
1918 struct smb_filename smb_fname;
1922 ZERO_STRUCT(smb_fname);
1923 smb_fname.base_name = talloc_asprintf(talloc_tos(),
1926 if (smb_fname.base_name == NULL) {
1930 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1931 if (ret != 0 || !S_ISDIR(smb_fname.st.st_ex_mode)) {
1932 TALLOC_FREE(smb_fname.base_name);
1936 status = smbd_check_access_rights(handle->conn,
1940 if (!NT_STATUS_IS_OK(status)) {
1941 DEBUG(0,("user does not have list permission "
1943 smb_fname.base_name));
1944 TALLOC_FREE(smb_fname.base_name);
1947 TALLOC_FREE(smb_fname.base_name);
1952 * Find the snapshot directory (if any) for the given
1953 * filename (which is relative to the share).
1955 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1956 struct vfs_handle_struct *handle,
1957 struct smb_filename *smb_fname)
1960 const char *snapdir;
1961 struct shadow_copy2_config *config;
1962 struct shadow_copy2_private *priv;
1964 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1967 config = priv->config;
1970 * If the non-snapdisrseverywhere mode, we should not search!
1972 if (!config->snapdirseverywhere) {
1973 return config->snapshot_basepath;
1976 path = talloc_asprintf(mem_ctx, "%s/%s",
1977 handle->conn->connectpath,
1978 smb_fname->base_name);
1983 snapdir = have_snapdir(handle, path);
1984 if (snapdir != NULL) {
1989 while ((p = strrchr(path, '/')) && (p > path)) {
1993 snapdir = have_snapdir(handle, path);
1994 if (snapdir != NULL) {
2003 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
2005 char *gmt, size_t gmt_len)
2007 struct tm timestamp;
2009 unsigned long int timestamp_long;
2011 struct shadow_copy2_config *config;
2012 struct shadow_copy2_private *priv;
2013 char *tmpstr = NULL;
2015 bool converted = false;
2018 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2021 config = priv->config;
2023 fmt = config->gmt_format;
2026 * If regex is provided, then we will have to parse the
2027 * filename which will contain both the prefix and the time format.
2028 * e.g. <prefix><delimiter><time_format>
2030 if (priv->snaps->regex != NULL) {
2031 tmpstr = talloc_strdup(talloc_tos(), name);
2032 /* point "name" to the time format */
2033 name = strstr(name, priv->config->delimiter);
2037 /* Extract the prefix */
2038 tmp = strstr(tmpstr, priv->config->delimiter);
2045 ret = regexec(priv->snaps->regex, tmpstr, 0, NULL, 0);
2047 DBG_DEBUG("shadow_copy2_snapshot_to_gmt: "
2048 "no regex match for %s\n", tmpstr);
2053 ZERO_STRUCT(timestamp);
2054 if (config->use_sscanf) {
2055 if (sscanf(name, fmt, ×tamp_long) != 1) {
2056 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
2057 "no sscanf match %s: %s\n",
2061 timestamp_t = timestamp_long;
2062 gmtime_r(×tamp_t, ×tamp);
2064 if (strptime(name, fmt, ×tamp) == NULL) {
2065 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
2066 "no match %s: %s\n",
2070 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
2073 if (config->use_localtime) {
2074 timestamp.tm_isdst = -1;
2075 timestamp_t = mktime(×tamp);
2076 gmtime_r(×tamp_t, ×tamp);
2080 strftime(gmt, gmt_len, GMT_FORMAT, ×tamp);
2084 TALLOC_FREE(tmpstr);
2088 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
2090 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
2093 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
2095 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
2099 sort the shadow copy data in ascending or descending order
2101 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
2102 struct shadow_copy_data *shadow_copy2_data)
2104 int (*cmpfunc)(const void *, const void *);
2106 struct shadow_copy2_private *priv;
2108 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2111 sort = priv->config->sort_order;
2116 if (strcmp(sort, "asc") == 0) {
2117 cmpfunc = shadow_copy2_label_cmp_asc;
2118 } else if (strcmp(sort, "desc") == 0) {
2119 cmpfunc = shadow_copy2_label_cmp_desc;
2124 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
2125 shadow_copy2_data->labels)
2127 TYPESAFE_QSORT(shadow_copy2_data->labels,
2128 shadow_copy2_data->num_volumes,
2133 static int shadow_copy2_get_shadow_copy_data(
2134 vfs_handle_struct *handle, files_struct *fsp,
2135 struct shadow_copy_data *shadow_copy2_data,
2139 const char *snapdir;
2140 struct smb_filename *snapdir_smb_fname = NULL;
2142 TALLOC_CTX *tmp_ctx = talloc_stackframe();
2143 struct shadow_copy2_private *priv = NULL;
2144 struct shadow_copy2_snapentry *tmpentry = NULL;
2145 bool get_snaplist = false;
2146 bool access_granted = false;
2149 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
2150 if (snapdir == NULL) {
2151 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
2152 handle->conn->connectpath));
2157 access_granted = check_access_snapdir(handle, snapdir);
2158 if (!access_granted) {
2159 DEBUG(0,("access denied on listing snapdir %s\n", snapdir));
2164 snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
2168 fsp->fsp_name->flags);
2169 if (snapdir_smb_fname == NULL) {
2174 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir_smb_fname, NULL, 0);
2177 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
2178 " - %s\n", snapdir, strerror(errno)));
2183 if (shadow_copy2_data != NULL) {
2184 shadow_copy2_data->num_volumes = 0;
2185 shadow_copy2_data->labels = NULL;
2188 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2192 * Normally this function is called twice once with labels = false and
2193 * then with labels = true. When labels is false it will return the
2194 * number of volumes so that the caller can allocate memory for that
2195 * many labels. Therefore to eliminate snaplist both the times it is
2196 * good to check if labels is set or not.
2198 * shadow_copy2_data is NULL when we only want to update the list and
2199 * don't want any labels.
2201 if ((priv->snaps->regex != NULL) && (labels || shadow_copy2_data == NULL)) {
2202 get_snaplist = true;
2203 /* Reset the global snaplist */
2204 shadow_copy2_delete_snaplist(priv);
2206 /* Set the current time as snaplist update time */
2207 time(&(priv->snaps->fetch_time));
2210 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
2211 char snapshot[GMT_NAME_LEN+1];
2212 SHADOW_COPY_LABEL *tlabels;
2215 * ignore names not of the right form in the snapshot
2218 if (!shadow_copy2_snapshot_to_gmt(
2220 snapshot, sizeof(snapshot))) {
2222 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
2223 "ignoring %s\n", d->d_name));
2226 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
2227 d->d_name, snapshot));
2231 * Create a snap entry for each successful
2234 tmpentry = shadow_copy2_create_snapentry(priv);
2235 if (tmpentry == NULL) {
2236 DBG_ERR("talloc_zero() failed\n");
2239 tmpentry->snapname = talloc_strdup(tmpentry, d->d_name);
2240 tmpentry->time_fmt = talloc_strdup(tmpentry, snapshot);
2243 if (shadow_copy2_data == NULL) {
2248 /* the caller doesn't want the labels */
2249 shadow_copy2_data->num_volumes++;
2253 tlabels = talloc_realloc(shadow_copy2_data,
2254 shadow_copy2_data->labels,
2256 shadow_copy2_data->num_volumes+1);
2257 if (tlabels == NULL) {
2258 DEBUG(0,("shadow_copy2: out of memory\n"));
2259 SMB_VFS_NEXT_CLOSEDIR(handle, p);
2263 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
2266 shadow_copy2_data->num_volumes++;
2267 shadow_copy2_data->labels = tlabels;
2270 SMB_VFS_NEXT_CLOSEDIR(handle,p);
2272 shadow_copy2_sort_data(handle, shadow_copy2_data);
2276 TALLOC_FREE(tmp_ctx);
2280 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
2281 struct files_struct *fsp,
2282 uint32_t security_info,
2283 TALLOC_CTX *mem_ctx,
2284 struct security_descriptor **ppdesc)
2286 time_t timestamp = 0;
2287 char *stripped = NULL;
2290 struct smb_filename *smb_fname = NULL;
2292 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
2293 fsp->fsp_name->base_name,
2294 ×tamp, &stripped)) {
2295 return map_nt_error_from_unix(errno);
2297 if (timestamp == 0) {
2298 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
2302 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2303 TALLOC_FREE(stripped);
2305 return map_nt_error_from_unix(errno);
2307 smb_fname = synthetic_smb_fname(talloc_tos(),
2311 fsp->fsp_name->flags);
2312 if (smb_fname == NULL) {
2314 return NT_STATUS_NO_MEMORY;
2317 status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
2320 TALLOC_FREE(smb_fname);
2324 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
2325 const struct smb_filename *smb_fname,
2326 uint32_t security_info,
2327 TALLOC_CTX *mem_ctx,
2328 struct security_descriptor **ppdesc)
2330 time_t timestamp = 0;
2331 char *stripped = NULL;
2334 struct smb_filename *conv_smb_fname = NULL;
2336 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2338 smb_fname->base_name,
2341 return map_nt_error_from_unix(errno);
2343 if (timestamp == 0) {
2344 return SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
2347 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2348 TALLOC_FREE(stripped);
2350 return map_nt_error_from_unix(errno);
2352 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2357 if (conv_smb_fname == NULL) {
2359 return NT_STATUS_NO_MEMORY;
2361 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv_smb_fname, security_info,
2364 TALLOC_FREE(conv_smb_fname);
2368 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
2369 const struct smb_filename *smb_fname,
2372 time_t timestamp = 0;
2373 char *stripped = NULL;
2374 int saved_errno = 0;
2377 struct smb_filename *conv_smb_fname = NULL;
2379 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2381 smb_fname->base_name,
2386 if (timestamp == 0) {
2387 return SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
2389 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2390 TALLOC_FREE(stripped);
2394 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2399 if (conv_smb_fname == NULL) {
2403 ret = SMB_VFS_NEXT_MKDIR(handle, conv_smb_fname, mode);
2405 saved_errno = errno;
2408 TALLOC_FREE(conv_smb_fname);
2409 if (saved_errno != 0) {
2410 errno = saved_errno;
2415 static int shadow_copy2_rmdir(vfs_handle_struct *handle,
2416 const struct smb_filename *smb_fname)
2418 time_t timestamp = 0;
2419 char *stripped = NULL;
2420 int saved_errno = 0;
2423 struct smb_filename *conv_smb_fname = NULL;
2425 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2427 smb_fname->base_name,
2432 if (timestamp == 0) {
2433 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
2435 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2436 TALLOC_FREE(stripped);
2440 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2445 if (conv_smb_fname == NULL) {
2449 ret = SMB_VFS_NEXT_RMDIR(handle, conv_smb_fname);
2451 saved_errno = errno;
2453 TALLOC_FREE(conv_smb_fname);
2455 if (saved_errno != 0) {
2456 errno = saved_errno;
2461 static int shadow_copy2_chflags(vfs_handle_struct *handle,
2462 const struct smb_filename *smb_fname,
2465 time_t timestamp = 0;
2466 char *stripped = NULL;
2467 int saved_errno = 0;
2470 struct smb_filename *conv_smb_fname = NULL;
2472 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2474 smb_fname->base_name,
2479 if (timestamp == 0) {
2480 return SMB_VFS_NEXT_CHFLAGS(handle, smb_fname, flags);
2482 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2483 TALLOC_FREE(stripped);
2487 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2492 if (conv_smb_fname == NULL) {
2496 ret = SMB_VFS_NEXT_CHFLAGS(handle, smb_fname, flags);
2498 saved_errno = errno;
2500 TALLOC_FREE(conv_smb_fname);
2502 if (saved_errno != 0) {
2503 errno = saved_errno;
2508 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
2509 const struct smb_filename *smb_fname,
2514 time_t timestamp = 0;
2515 char *stripped = NULL;
2517 int saved_errno = 0;
2519 struct smb_filename *conv_smb_fname = NULL;
2521 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2523 smb_fname->base_name,
2528 if (timestamp == 0) {
2529 return SMB_VFS_NEXT_GETXATTR(handle, smb_fname, aname, value,
2532 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2533 TALLOC_FREE(stripped);
2538 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2543 if (conv_smb_fname == NULL) {
2548 ret = SMB_VFS_NEXT_GETXATTR(handle, conv_smb_fname, aname, value, size);
2550 saved_errno = errno;
2552 TALLOC_FREE(conv_smb_fname);
2554 if (saved_errno != 0) {
2555 errno = saved_errno;
2560 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
2561 const struct smb_filename *smb_fname,
2562 char *list, size_t size)
2564 time_t timestamp = 0;
2565 char *stripped = NULL;
2567 int saved_errno = 0;
2569 struct smb_filename *conv_smb_fname = NULL;
2571 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2573 smb_fname->base_name,
2578 if (timestamp == 0) {
2579 return SMB_VFS_NEXT_LISTXATTR(handle, smb_fname, list, size);
2581 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2582 TALLOC_FREE(stripped);
2586 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2591 if (conv_smb_fname == NULL) {
2595 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv_smb_fname, list, size);
2597 saved_errno = errno;
2599 TALLOC_FREE(conv_smb_fname);
2601 if (saved_errno != 0) {
2602 errno = saved_errno;
2607 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
2608 const struct smb_filename *smb_fname,
2611 time_t timestamp = 0;
2612 char *stripped = NULL;
2613 int saved_errno = 0;
2616 struct smb_filename *conv_smb_fname = NULL;
2618 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2620 smb_fname->base_name,
2625 if (timestamp == 0) {
2626 return SMB_VFS_NEXT_REMOVEXATTR(handle, smb_fname, aname);
2628 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2629 TALLOC_FREE(stripped);
2633 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2638 if (conv_smb_fname == NULL) {
2642 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv_smb_fname, aname);
2644 saved_errno = errno;
2646 TALLOC_FREE(conv_smb_fname);
2648 if (saved_errno != 0) {
2649 errno = saved_errno;
2654 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
2655 const struct smb_filename *smb_fname,
2656 const char *aname, const void *value,
2657 size_t size, int flags)
2659 time_t timestamp = 0;
2660 char *stripped = NULL;
2662 int saved_errno = 0;
2664 struct smb_filename *conv_smb_fname = NULL;
2666 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2668 smb_fname->base_name,
2673 if (timestamp == 0) {
2674 return SMB_VFS_NEXT_SETXATTR(handle, smb_fname,
2675 aname, value, size, flags);
2677 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2678 TALLOC_FREE(stripped);
2682 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2687 if (conv_smb_fname == NULL) {
2691 ret = SMB_VFS_NEXT_SETXATTR(handle, conv_smb_fname,
2692 aname, value, size, flags);
2694 saved_errno = errno;
2696 TALLOC_FREE(conv_smb_fname);
2698 if (saved_errno != 0) {
2699 errno = saved_errno;
2704 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
2707 TALLOC_CTX *mem_ctx,
2710 time_t timestamp = 0;
2711 char *stripped = NULL;
2713 int saved_errno = 0;
2716 DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
2717 "name=[%s]\n", path, name));
2719 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
2720 ×tamp, &stripped)) {
2721 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
2724 if (timestamp == 0) {
2725 DEBUG(10, ("timestamp == 0\n"));
2726 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
2727 mem_ctx, found_name);
2729 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2730 TALLOC_FREE(stripped);
2732 DEBUG(10, ("shadow_copy2_convert failed\n"));
2735 DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
2736 "name=[%s]\n", conv, name));
2737 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
2738 mem_ctx, found_name);
2739 DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
2741 saved_errno = errno;
2744 if (saved_errno != 0) {
2745 errno = saved_errno;
2750 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
2751 const struct smb_filename *smb_fname_in)
2753 time_t timestamp = 0;
2754 char *stripped = NULL;
2756 const char *fname = smb_fname_in->base_name;
2757 struct smb_filename smb_fname = {0};
2758 struct smb_filename *result_fname = NULL;
2759 char *result = NULL;
2760 char *parent_dir = NULL;
2761 int saved_errno = 0;
2762 size_t rootpath_len = 0;
2763 struct shadow_copy2_private *priv = NULL;
2765 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
2768 DBG_DEBUG("Calc connect path for [%s]\n", fname);
2770 if (priv->shadow_connectpath != NULL) {
2771 DBG_DEBUG("cached connect path is [%s]\n",
2772 priv->shadow_connectpath);
2773 return priv->shadow_connectpath;
2776 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2777 ×tamp, &stripped)) {
2780 if (timestamp == 0) {
2781 return SMB_VFS_NEXT_CONNECTPATH(handle, smb_fname_in);
2784 tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
2787 if (errno != ENOENT) {
2792 * If the converted path does not exist, and converting
2793 * the parent yields something that does exist, then
2794 * this path refers to something that has not been
2795 * created yet, relative to the parent path.
2796 * The snapshot finding is relative to the parent.
2797 * (usually snapshots are read/only but this is not
2798 * necessarily true).
2799 * This code also covers getting a wildcard in the
2800 * last component, because this function is called
2801 * prior to sanitizing the path, and in SMB1 we may
2802 * get wildcards in path names.
2804 if (!parent_dirname(talloc_tos(), stripped, &parent_dir,
2810 tmp = shadow_copy2_do_convert(talloc_tos(), handle, parent_dir,
2811 timestamp, &rootpath_len);
2817 DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
2818 (int)rootpath_len, tmp);
2820 tmp[rootpath_len] = '\0';
2821 smb_fname = (struct smb_filename) { .base_name = tmp };
2823 result_fname = SMB_VFS_NEXT_REALPATH(handle, priv, &smb_fname);
2824 if (result_fname == NULL) {
2829 * SMB_VFS_NEXT_REALPATH returns a talloc'ed string.
2830 * Don't leak memory.
2832 TALLOC_FREE(priv->shadow_realpath);
2833 priv->shadow_realpath = result_fname;
2834 result = priv->shadow_realpath->base_name;
2836 DBG_DEBUG("connect path is [%s]\n", result);
2839 if (result == NULL) {
2840 saved_errno = errno;
2843 TALLOC_FREE(stripped);
2844 TALLOC_FREE(parent_dir);
2845 if (saved_errno != 0) {
2846 errno = saved_errno;
2851 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
2852 const struct smb_filename *smb_fname,
2857 time_t timestamp = 0;
2858 char *stripped = NULL;
2859 int saved_errno = 0;
2861 struct smb_filename *conv_smb_fname = NULL;
2862 uint64_t ret = (uint64_t)-1;
2864 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2866 smb_fname->base_name,
2869 return (uint64_t)-1;
2871 if (timestamp == 0) {
2872 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2873 bsize, dfree, dsize);
2875 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2876 TALLOC_FREE(stripped);
2878 return (uint64_t)-1;
2880 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2885 if (conv_smb_fname == NULL) {
2887 return (uint64_t)-1;
2889 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv_smb_fname,
2890 bsize, dfree, dsize);
2891 if (ret == (uint64_t)-1) {
2892 saved_errno = errno;
2895 TALLOC_FREE(conv_smb_fname);
2896 if (saved_errno != 0) {
2897 errno = saved_errno;
2902 static int shadow_copy2_get_quota(vfs_handle_struct *handle,
2903 const struct smb_filename *smb_fname,
2904 enum SMB_QUOTA_TYPE qtype,
2908 time_t timestamp = 0;
2909 char *stripped = NULL;
2911 int saved_errno = 0;
2913 struct smb_filename *conv_smb_fname = NULL;
2915 if (!shadow_copy2_strip_snapshot(talloc_tos(),
2917 smb_fname->base_name,
2922 if (timestamp == 0) {
2923 return SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname, qtype, id, dq);
2926 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2927 TALLOC_FREE(stripped);
2931 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2936 if (conv_smb_fname == NULL) {
2940 ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv_smb_fname, qtype, id, dq);
2943 saved_errno = errno;
2946 TALLOC_FREE(conv_smb_fname);
2947 if (saved_errno != 0) {
2948 errno = saved_errno;
2954 static ssize_t shadow_copy2_pwrite(vfs_handle_struct *handle,
2962 nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2963 if (nwritten == -1) {
2964 if (errno == EBADF && fsp->can_write) {
2972 struct shadow_copy2_pwrite_state {
2973 vfs_handle_struct *handle;
2976 struct vfs_aio_state vfs_aio_state;
2979 static void shadow_copy2_pwrite_done(struct tevent_req *subreq);
2981 static struct tevent_req *shadow_copy2_pwrite_send(
2982 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
2983 struct tevent_context *ev, struct files_struct *fsp,
2984 const void *data, size_t n, off_t offset)
2986 struct tevent_req *req = NULL, *subreq = NULL;
2987 struct shadow_copy2_pwrite_state *state = NULL;
2989 req = tevent_req_create(mem_ctx, &state,
2990 struct shadow_copy2_pwrite_state);
2994 state->handle = handle;
2997 subreq = SMB_VFS_NEXT_PWRITE_SEND(state,
3004 if (tevent_req_nomem(subreq, req)) {
3005 return tevent_req_post(req, ev);
3007 tevent_req_set_callback(subreq, shadow_copy2_pwrite_done, req);
3012 static void shadow_copy2_pwrite_done(struct tevent_req *subreq)
3014 struct tevent_req *req = tevent_req_callback_data(
3015 subreq, struct tevent_req);
3016 struct shadow_copy2_pwrite_state *state = tevent_req_data(
3017 req, struct shadow_copy2_pwrite_state);
3019 state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
3020 TALLOC_FREE(subreq);
3021 if (state->ret == -1) {
3022 tevent_req_error(req, state->vfs_aio_state.error);
3026 tevent_req_done(req);
3029 static ssize_t shadow_copy2_pwrite_recv(struct tevent_req *req,
3030 struct vfs_aio_state *vfs_aio_state)
3032 struct shadow_copy2_pwrite_state *state = tevent_req_data(
3033 req, struct shadow_copy2_pwrite_state);
3035 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
3036 if ((vfs_aio_state->error == EBADF) &&
3037 state->fsp->can_write)
3039 vfs_aio_state->error = EROFS;
3045 *vfs_aio_state = state->vfs_aio_state;
3049 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
3050 const char *service, const char *user)
3052 struct shadow_copy2_config *config;
3053 struct shadow_copy2_private *priv;
3055 const char *snapdir;
3056 const char *snapprefix = NULL;
3057 const char *delimiter;
3058 const char *gmt_format;
3059 const char *sort_order;
3060 const char *basedir = NULL;
3061 const char *snapsharepath = NULL;
3062 const char *mount_point;
3064 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
3065 (unsigned)handle->conn->cnum,
3066 handle->conn->connectpath));
3068 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
3073 priv = talloc_zero(handle->conn, struct shadow_copy2_private);
3075 DBG_ERR("talloc_zero() failed\n");
3080 priv->snaps = talloc_zero(priv, struct shadow_copy2_snaplist_info);
3081 if (priv->snaps == NULL) {
3082 DBG_ERR("talloc_zero() failed\n");
3087 config = talloc_zero(priv, struct shadow_copy2_config);
3088 if (config == NULL) {
3089 DEBUG(0, ("talloc_zero() failed\n"));
3094 priv->config = config;
3096 gmt_format = lp_parm_const_string(SNUM(handle->conn),
3099 config->gmt_format = talloc_strdup(config, gmt_format);
3100 if (config->gmt_format == NULL) {
3101 DEBUG(0, ("talloc_strdup() failed\n"));
3106 /* config->gmt_format must not contain a path separator. */
3107 if (strchr(config->gmt_format, '/') != NULL) {
3108 DEBUG(0, ("shadow:format %s must not contain a /"
3109 "character. Unable to initialize module.\n",
3110 config->gmt_format));
3115 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
3116 "shadow", "sscanf", false);
3118 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
3119 "shadow", "localtime",
3122 snapdir = lp_parm_const_string(SNUM(handle->conn),
3123 "shadow", "snapdir",
3125 config->snapdir = talloc_strdup(config, snapdir);
3126 if (config->snapdir == NULL) {
3127 DEBUG(0, ("talloc_strdup() failed\n"));
3132 snapprefix = lp_parm_const_string(SNUM(handle->conn),
3133 "shadow", "snapprefix",
3135 if (snapprefix != NULL) {
3136 priv->snaps->regex = talloc_zero(priv->snaps, regex_t);
3137 if (priv->snaps->regex == NULL) {
3138 DBG_ERR("talloc_zero() failed\n");
3143 /* pre-compute regex rule for matching pattern later */
3144 ret = regcomp(priv->snaps->regex, snapprefix, 0);
3146 DBG_ERR("Failed to create regex object\n");
3151 delimiter = lp_parm_const_string(SNUM(handle->conn),
3152 "shadow", "delimiter",
3154 if (delimiter != NULL) {
3155 priv->config->delimiter = talloc_strdup(priv->config, delimiter);
3156 if (priv->config->delimiter == NULL) {
3157 DBG_ERR("talloc_strdup() failed\n");
3163 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
3165 "snapdirseverywhere",
3168 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
3169 "shadow", "crossmountpoints",
3172 if (config->crossmountpoints && !config->snapdirseverywhere) {
3173 DBG_WARNING("Warning: 'crossmountpoints' depends on "
3174 "'snapdirseverywhere'. Disabling crossmountpoints.\n");
3177 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
3178 "shadow", "fixinodes",
3181 sort_order = lp_parm_const_string(SNUM(handle->conn),
3182 "shadow", "sort", "desc");
3183 config->sort_order = talloc_strdup(config, sort_order);
3184 if (config->sort_order == NULL) {
3185 DEBUG(0, ("talloc_strdup() failed\n"));
3190 mount_point = lp_parm_const_string(SNUM(handle->conn),
3191 "shadow", "mountpoint", NULL);
3192 if (mount_point != NULL) {
3193 if (mount_point[0] != '/') {
3194 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
3195 "relative ('%s'), but it has to be an "
3196 "absolute path. Ignoring provided value.\n",
3201 p = strstr(handle->conn->connectpath, mount_point);
3202 if (p != handle->conn->connectpath) {
3203 DBG_WARNING("Warning: the share root (%s) is "
3204 "not a subdirectory of the "
3205 "specified mountpoint (%s). "
3206 "Ignoring provided value.\n",
3207 handle->conn->connectpath,
3214 if (mount_point != NULL) {
3215 config->mount_point = talloc_strdup(config, mount_point);
3216 if (config->mount_point == NULL) {
3217 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
3221 config->mount_point = shadow_copy2_find_mount_point(config,
3223 if (config->mount_point == NULL) {
3224 DBG_WARNING("shadow_copy2_find_mount_point "
3225 "of the share root '%s' failed: %s\n",
3226 handle->conn->connectpath, strerror(errno));
3231 basedir = lp_parm_const_string(SNUM(handle->conn),
3232 "shadow", "basedir", NULL);
3234 if (basedir != NULL) {
3235 if (basedir[0] != '/') {
3236 DEBUG(1, (__location__ " Warning: 'basedir' is "
3237 "relative ('%s'), but it has to be an "
3238 "absolute path. Disabling basedir.\n",
3243 p = strstr(basedir, config->mount_point);
3245 DEBUG(1, ("Warning: basedir (%s) is not a "
3246 "subdirectory of the share root's "
3247 "mount point (%s). "
3248 "Disabling basedir\n",
3249 basedir, config->mount_point));
3255 if (config->snapdirseverywhere && basedir != NULL) {
3256 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
3257 "with 'snapdirseverywhere'. Disabling basedir.\n"));
3261 snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
3262 "snapsharepath", NULL);
3263 if (snapsharepath != NULL) {
3264 if (snapsharepath[0] == '/') {
3265 DBG_WARNING("Warning: 'snapsharepath' is "
3266 "absolute ('%s'), but it has to be a "
3267 "relative path. Disabling snapsharepath.\n",
3269 snapsharepath = NULL;
3271 if (config->snapdirseverywhere && snapsharepath != NULL) {
3272 DBG_WARNING("Warning: 'snapsharepath' is incompatible "
3273 "with 'snapdirseverywhere'. Disabling "
3274 "snapsharepath.\n");
3275 snapsharepath = NULL;
3279 if (basedir != NULL && snapsharepath != NULL) {
3280 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
3281 "'basedir'. Disabling snapsharepath\n");
3282 snapsharepath = NULL;
3285 if (snapsharepath != NULL) {
3286 config->rel_connectpath = talloc_strdup(config, snapsharepath);
3287 if (config->rel_connectpath == NULL) {
3288 DBG_ERR("talloc_strdup() failed\n");
3294 if (basedir == NULL) {
3295 basedir = config->mount_point;
3298 if (config->rel_connectpath == NULL &&
3299 strlen(basedir) < strlen(handle->conn->connectpath)) {
3300 config->rel_connectpath = talloc_strdup(config,
3301 handle->conn->connectpath + strlen(basedir));
3302 if (config->rel_connectpath == NULL) {
3303 DEBUG(0, ("talloc_strdup() failed\n"));
3309 if (config->snapdir[0] == '/') {
3310 config->snapdir_absolute = true;
3312 if (config->snapdirseverywhere == true) {
3313 DEBUG(1, (__location__ " Warning: An absolute snapdir "
3314 "is incompatible with 'snapdirseverywhere', "
3315 "setting 'snapdirseverywhere' to false.\n"));
3316 config->snapdirseverywhere = false;
3319 if (config->crossmountpoints == true) {
3320 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
3321 "is not supported with an absolute snapdir. "
3322 "Disabling it.\n"));
3323 config->crossmountpoints = false;
3326 config->snapshot_basepath = config->snapdir;
3328 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
3329 config->mount_point, config->snapdir);
3330 if (config->snapshot_basepath == NULL) {
3331 DEBUG(0, ("talloc_asprintf() failed\n"));
3337 trim_string(config->mount_point, NULL, "/");
3338 trim_string(config->rel_connectpath, "/", "/");
3339 trim_string(config->snapdir, NULL, "/");
3340 trim_string(config->snapshot_basepath, NULL, "/");
3342 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
3343 " share root: '%s'\n"
3344 " mountpoint: '%s'\n"
3345 " rel share root: '%s'\n"
3347 " snapprefix: '%s'\n"
3348 " delimiter: '%s'\n"
3349 " snapshot base path: '%s'\n"
3352 " snapdirs everywhere: %s\n"
3353 " cross mountpoints: %s\n"
3357 handle->conn->connectpath,
3358 config->mount_point,
3359 config->rel_connectpath,
3363 config->snapshot_basepath,
3365 config->use_sscanf ? "yes" : "no",
3366 config->snapdirseverywhere ? "yes" : "no",
3367 config->crossmountpoints ? "yes" : "no",
3368 config->fixinodes ? "yes" : "no",
3373 SMB_VFS_HANDLE_SET_DATA(handle, priv,
3374 NULL, struct shadow_copy2_private,
3380 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
3381 .connect_fn = shadow_copy2_connect,
3382 .opendir_fn = shadow_copy2_opendir,
3383 .disk_free_fn = shadow_copy2_disk_free,
3384 .get_quota_fn = shadow_copy2_get_quota,
3385 .rename_fn = shadow_copy2_rename,
3386 .link_fn = shadow_copy2_link,
3387 .symlink_fn = shadow_copy2_symlink,
3388 .stat_fn = shadow_copy2_stat,
3389 .lstat_fn = shadow_copy2_lstat,
3390 .fstat_fn = shadow_copy2_fstat,
3391 .open_fn = shadow_copy2_open,
3392 .unlink_fn = shadow_copy2_unlink,
3393 .chmod_fn = shadow_copy2_chmod,
3394 .chown_fn = shadow_copy2_chown,
3395 .chdir_fn = shadow_copy2_chdir,
3396 .ntimes_fn = shadow_copy2_ntimes,
3397 .readlink_fn = shadow_copy2_readlink,
3398 .mknod_fn = shadow_copy2_mknod,
3399 .realpath_fn = shadow_copy2_realpath,
3400 .get_nt_acl_fn = shadow_copy2_get_nt_acl,
3401 .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
3402 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
3403 .mkdir_fn = shadow_copy2_mkdir,
3404 .rmdir_fn = shadow_copy2_rmdir,
3405 .getxattr_fn = shadow_copy2_getxattr,
3406 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
3407 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
3408 .listxattr_fn = shadow_copy2_listxattr,
3409 .removexattr_fn = shadow_copy2_removexattr,
3410 .setxattr_fn = shadow_copy2_setxattr,
3411 .chflags_fn = shadow_copy2_chflags,
3412 .get_real_filename_fn = shadow_copy2_get_real_filename,
3413 .pwrite_fn = shadow_copy2_pwrite,
3414 .pwrite_send_fn = shadow_copy2_pwrite_send,
3415 .pwrite_recv_fn = shadow_copy2_pwrite_recv,
3416 .connectpath_fn = shadow_copy2_connectpath,
3420 NTSTATUS vfs_shadow_copy2_init(TALLOC_CTX *ctx)
3422 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
3423 "shadow_copy2", &vfs_shadow_copy2_fns);