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
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * This is a second implemetation of a shadow copy module for exposing
27 * file system snapshots to windows clients as shadow copies.
29 * See the manual page for documentation.
33 #include "smbd/smbd.h"
34 #include "system/filesys.h"
35 #include "include/ntioctl.h"
38 struct shadow_copy2_config {
43 bool snapdirseverywhere;
44 bool crossmountpoints;
47 bool snapdir_absolute;
49 char *rel_connectpath; /* share root, relative to a snapshot root */
50 char *snapshot_basepath; /* the absolute version of snapdir */
53 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
55 unsigned *pnum_offsets)
64 while ((p = strchr(p, '/')) != NULL) {
69 offsets = talloc_array(mem_ctx, size_t, num_offsets);
70 if (offsets == NULL) {
76 while ((p = strchr(p, '/')) != NULL) {
77 offsets[num_offsets] = p-str;
83 *pnum_offsets = num_offsets;
88 * Given a timestamp, build the posix level GMT-tag string
89 * based on the configurable format.
91 static size_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
93 char *snaptime_string,
98 struct shadow_copy2_config *config;
100 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
103 if (config->use_sscanf) {
104 snaptime_len = snprintf(snaptime_string,
107 (unsigned long)snapshot);
108 if (snaptime_len <= 0) {
109 DEBUG(10, ("snprintf failed\n"));
113 if (config->use_localtime) {
114 if (localtime_r(&snapshot, &snap_tm) == 0) {
115 DEBUG(10, ("gmtime_r failed\n"));
119 if (gmtime_r(&snapshot, &snap_tm) == 0) {
120 DEBUG(10, ("gmtime_r failed\n"));
124 snaptime_len = strftime(snaptime_string,
128 if (snaptime_len == 0) {
129 DEBUG(10, ("strftime failed\n"));
138 * Given a timestamp, build the string to insert into a path
139 * as a path component for creating the local path to the
140 * snapshot at the given timestamp of the input path.
142 * In the case of a parallel snapdir (specified with an
143 * absolute path), this is the inital portion of the
144 * local path of any snapshot file. The complete path is
145 * obtained by appending the portion of the file's path
146 * below the share root's mountpoint.
148 static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
149 struct vfs_handle_struct *handle,
152 fstring snaptime_string;
153 size_t snaptime_len = 0;
155 struct shadow_copy2_config *config;
157 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
160 snaptime_len = shadow_copy2_posix_gmt_string(handle,
163 sizeof(snaptime_string));
164 if (snaptime_len <= 0) {
168 if (config->snapdir_absolute) {
169 result = talloc_asprintf(mem_ctx, "%s/%s",
170 config->snapdir, snaptime_string);
172 result = talloc_asprintf(mem_ctx, "/%s/%s",
173 config->snapdir, snaptime_string);
175 if (result == NULL) {
176 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
183 * Build the posix snapshot path for the connection
184 * at the given timestamp, i.e. the absolute posix path
185 * that contains the snapshot for this file system.
187 * This only applies to classical case, i.e. not
188 * to the "snapdirseverywhere" mode.
190 static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
191 struct vfs_handle_struct *handle,
194 fstring snaptime_string;
195 size_t snaptime_len = 0;
197 struct shadow_copy2_config *config;
199 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
202 snaptime_len = shadow_copy2_posix_gmt_string(handle,
205 sizeof(snaptime_string));
206 if (snaptime_len <= 0) {
210 result = talloc_asprintf(mem_ctx, "%s/%s",
211 config->snapshot_basepath, snaptime_string);
212 if (result == NULL) {
213 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
220 * Strip a snapshot component from a filename as
221 * handed in via the smb layer.
222 * Returns the parsed timestamp and the stripped filename.
224 static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
225 struct vfs_handle_struct *handle,
235 size_t rest_len, dst_len;
236 struct shadow_copy2_config *config;
239 ptrdiff_t len_before_gmt;
241 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
244 DEBUG(10, (__location__ ": enter path '%s'\n", name));
246 p = strstr_m(name, "@GMT-");
248 DEBUG(11, ("@GMT not found\n"));
251 if ((p > name) && (p[-1] != '/')) {
252 /* the GMT-token does not start a path-component */
253 DEBUG(10, ("not at start, p=%p, name=%p, p[-1]=%d\n",
254 p, name, (int)p[-1]));
259 * Figure out whether we got an already converted string. One
260 * case where this happens is in a smb2 create call with the
261 * mxac create blob set. We do the get_acl call on
262 * fsp->fsp_name, which is already converted. We are converted
263 * if we got a file name of the form ".snapshots/@GMT-",
264 * i.e. ".snapshots/" precedes "p".
267 snapdir = lp_parm_const_string(SNUM(handle->conn), "shadow", "snapdir",
269 snapdirlen = strlen(snapdir);
270 len_before_gmt = p - name;
272 if ((len_before_gmt >= (snapdirlen + 1)) && (p[-1] == '/')) {
273 const char *parent_snapdir = p - (snapdirlen+1);
275 DEBUG(10, ("parent_snapdir = %s\n", parent_snapdir));
277 if (strncmp(parent_snapdir, snapdir, snapdirlen) == 0) {
278 DEBUG(10, ("name=%s is already converted\n", name));
282 q = strptime(p, GMT_FORMAT, &tm);
284 DEBUG(10, ("strptime failed\n"));
288 timestamp = timegm(&tm);
289 if (timestamp == (time_t)-1) {
290 DEBUG(10, ("timestamp==-1\n"));
295 * The name consists of only the GMT token or the GMT
296 * token is at the end of the path. XP seems to send
297 * @GMT- at the end under certain circumstances even
298 * with a path prefix.
300 if (pstripped != NULL) {
301 stripped = talloc_strndup(mem_ctx, name, p - name);
302 if (stripped == NULL) {
305 *pstripped = stripped;
307 *ptimestamp = timestamp;
312 * It is not a complete path component, i.e. the path
313 * component continues after the gmt-token.
315 DEBUG(10, ("q[0] = %d\n", (int)q[0]));
320 rest_len = strlen(q);
321 dst_len = (p-name) + rest_len;
323 if (config->snapdirseverywhere) {
326 insert = shadow_copy2_insert_string(talloc_tos(), handle,
328 if (insert == NULL) {
333 DEBUG(10, (__location__ ": snapdirseverywhere mode.\n"
335 "insert string '%s'\n", name, insert));
337 have_insert = (strstr(name, insert+1) != NULL);
338 DEBUG(10, ("have_insert=%d, name=%s, insert+1=%s\n",
339 (int)have_insert, name, insert+1));
341 DEBUG(10, (__location__ ": insert string '%s' found in "
342 "path '%s' found in snapdirseverywhere mode "
343 "==> already converted\n", insert, name));
352 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
355 if (snapshot_path == NULL) {
360 DEBUG(10, (__location__ " path: '%s'.\n"
361 "snapshot path: '%s'\n", name, snapshot_path));
363 s = strstr(name, snapshot_path);
366 * this starts with "snapshot_basepath/GMT-Token"
367 * so it is already a converted absolute
368 * path. Don't process further.
370 DEBUG(10, (__location__ ": path '%s' starts with "
371 "snapshot path '%s' (not in "
372 "snapdirseverywhere mode) ==> "
373 "already converted\n", name, snapshot_path));
374 talloc_free(snapshot_path);
377 talloc_free(snapshot_path);
380 if (pstripped != NULL) {
381 stripped = talloc_array(mem_ctx, char, dst_len+1);
382 if (stripped == NULL) {
387 memcpy(stripped, name, p-name);
390 memcpy(stripped + (p-name), q, rest_len);
392 stripped[dst_len] = '\0';
393 *pstripped = stripped;
395 *ptimestamp = timestamp;
402 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
403 vfs_handle_struct *handle)
405 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
410 if (stat(path, &st) != 0) {
417 while ((p = strrchr(path, '/')) && p > path) {
419 if (stat(path, &st) != 0) {
423 if (st.st_dev != dev) {
433 * Convert from a name as handed in via the SMB layer
434 * and a timestamp into the local path of the snapshot
435 * of the provided file at the provided time.
436 * Also return the path in the snapshot corresponding
437 * to the file's share root.
439 static char *shadow_copy2_do_convert(TALLOC_CTX *mem_ctx,
440 struct vfs_handle_struct *handle,
441 const char *name, time_t timestamp,
442 size_t *snaproot_len)
444 struct smb_filename converted_fname;
446 size_t *slashes = NULL;
447 unsigned num_slashes;
451 char *converted = NULL;
452 size_t insertlen, connectlen = 0;
455 struct shadow_copy2_config *config;
456 size_t in_share_offset = 0;
458 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
461 DEBUG(10, ("converting '%s'\n", name));
463 if (!config->snapdirseverywhere) {
467 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
470 if (snapshot_path == NULL) {
474 if (config->rel_connectpath == NULL) {
475 converted = talloc_asprintf(mem_ctx, "%s/%s",
476 snapshot_path, name);
478 converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
480 config->rel_connectpath,
483 if (converted == NULL) {
487 ZERO_STRUCT(converted_fname);
488 converted_fname.base_name = converted;
490 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
491 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
493 ret, ret == 0 ? "ok" : strerror(errno)));
495 DEBUG(10, ("Found %s\n", converted));
498 if (snaproot_len != NULL) {
499 *snaproot_len = strlen(snapshot_path);
500 if (config->rel_connectpath != NULL) {
502 strlen(config->rel_connectpath) + 1;
510 /* never reached ... */
513 connectlen = strlen(handle->conn->connectpath);
515 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
517 path = talloc_asprintf(
518 mem_ctx, "%s/%s", handle->conn->connectpath, name);
524 pathlen = talloc_get_size(path)-1;
526 if (!shadow_copy2_find_slashes(talloc_tos(), path,
527 &slashes, &num_slashes)) {
531 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
532 if (insert == NULL) {
535 insertlen = talloc_get_size(insert)-1;
538 * Note: We deliberatly don't expensively initialize the
539 * array with talloc_zero here: Putting zero into
540 * converted[pathlen+insertlen] below is sufficient, because
541 * in the following for loop, the insert string is inserted
542 * at various slash places. So the memory up to position
543 * pathlen+insertlen will always be initialized when the
544 * converted string is used.
546 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
547 if (converted == NULL) {
551 if (path[pathlen-1] != '/') {
553 * Append a fake slash to find the snapshot root
556 tmp = talloc_realloc(talloc_tos(), slashes,
557 size_t, num_slashes+1);
562 slashes[num_slashes] = pathlen;
568 if (!config->crossmountpoints) {
569 min_offset = strlen(config->mount_point);
572 memcpy(converted, path, pathlen+1);
573 converted[pathlen+insertlen] = '\0';
575 ZERO_STRUCT(converted_fname);
576 converted_fname.base_name = converted;
578 for (i = num_slashes-1; i>=0; i--) {
584 if (offset < min_offset) {
589 if (offset >= connectlen) {
590 in_share_offset = offset;
593 memcpy(converted+offset, insert, insertlen);
596 memcpy(converted+offset, path + slashes[i],
597 pathlen - slashes[i]);
599 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
601 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
603 ret, ret == 0 ? "ok" : strerror(errno)));
606 if (snaproot_len != NULL) {
607 *snaproot_len = in_share_offset + insertlen;
611 if (errno == ENOTDIR) {
613 * This is a valid condition: We appended the
614 * .snaphots/@GMT.. to a file name. Just try
615 * with the upper levels.
619 if (errno != ENOENT) {
620 /* Other problem than "not found" */
629 DEBUG(10, ("Found %s\n", converted));
637 TALLOC_FREE(converted);
639 TALLOC_FREE(slashes);
646 * Convert from a name as handed in via the SMB layer
647 * and a timestamp into the local path of the snapshot
648 * of the provided file at the provided time.
650 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
651 struct vfs_handle_struct *handle,
652 const char *name, time_t timestamp)
654 return shadow_copy2_do_convert(mem_ctx, handle, name, timestamp, NULL);
658 modify a sbuf return to ensure that inodes in the shadow directory
659 are different from those in the main directory
661 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
662 SMB_STRUCT_STAT *sbuf)
664 struct shadow_copy2_config *config;
666 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
669 if (config->fixinodes) {
670 /* some snapshot systems, like GPFS, return the name
671 device:inode for the snapshot files as the current
672 files. That breaks the 'restore' button in the shadow copy
673 GUI, as the client gets a sharing violation.
675 This is a crude way of allowing both files to be
676 open at once. It has a slight chance of inode
677 number collision, but I can't see a better approach
678 without significant VFS changes
680 TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
681 .dsize = strlen(fname) };
684 shash = tdb_jenkins_hash(&key) & 0xFF000000;
688 sbuf->st_ex_ino ^= shash;
692 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
693 const struct smb_filename *smb_fname,
702 struct smb_filename *conv_smb_fname = NULL;
704 if (!shadow_copy2_strip_snapshot(talloc_tos(),
706 smb_fname->base_name,
711 if (timestamp == 0) {
712 return SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
714 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
715 TALLOC_FREE(stripped);
719 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
723 if (conv_smb_fname == NULL) {
727 ret = SMB_VFS_NEXT_OPENDIR(handle, conv_smb_fname, mask, attr);
730 TALLOC_FREE(conv_smb_fname);
735 static int shadow_copy2_rename(vfs_handle_struct *handle,
736 const struct smb_filename *smb_fname_src,
737 const struct smb_filename *smb_fname_dst)
739 time_t timestamp_src, timestamp_dst;
741 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
742 smb_fname_src->base_name,
743 ×tamp_src, NULL)) {
746 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
747 smb_fname_dst->base_name,
748 ×tamp_dst, NULL)) {
751 if (timestamp_src != 0) {
755 if (timestamp_dst != 0) {
759 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
762 static int shadow_copy2_symlink(vfs_handle_struct *handle,
763 const char *oldname, const char *newname)
765 time_t timestamp_old, timestamp_new;
767 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
768 ×tamp_old, NULL)) {
771 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
772 ×tamp_new, NULL)) {
775 if ((timestamp_old != 0) || (timestamp_new != 0)) {
779 return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
782 static int shadow_copy2_link(vfs_handle_struct *handle,
783 const char *oldname, const char *newname)
785 time_t timestamp_old, timestamp_new;
787 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
788 ×tamp_old, NULL)) {
791 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
792 ×tamp_new, NULL)) {
795 if ((timestamp_old != 0) || (timestamp_new != 0)) {
799 return SMB_VFS_NEXT_LINK(handle, oldname, newname);
802 static int shadow_copy2_stat(vfs_handle_struct *handle,
803 struct smb_filename *smb_fname)
806 char *stripped, *tmp;
807 int ret, saved_errno;
809 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
810 smb_fname->base_name,
811 ×tamp, &stripped)) {
814 if (timestamp == 0) {
815 return SMB_VFS_NEXT_STAT(handle, smb_fname);
818 tmp = smb_fname->base_name;
819 smb_fname->base_name = shadow_copy2_convert(
820 talloc_tos(), handle, stripped, timestamp);
821 TALLOC_FREE(stripped);
823 if (smb_fname->base_name == NULL) {
824 smb_fname->base_name = tmp;
828 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
831 TALLOC_FREE(smb_fname->base_name);
832 smb_fname->base_name = tmp;
835 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
841 static int shadow_copy2_lstat(vfs_handle_struct *handle,
842 struct smb_filename *smb_fname)
845 char *stripped, *tmp;
846 int ret, saved_errno;
848 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
849 smb_fname->base_name,
850 ×tamp, &stripped)) {
853 if (timestamp == 0) {
854 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
857 tmp = smb_fname->base_name;
858 smb_fname->base_name = shadow_copy2_convert(
859 talloc_tos(), handle, stripped, timestamp);
860 TALLOC_FREE(stripped);
862 if (smb_fname->base_name == NULL) {
863 smb_fname->base_name = tmp;
867 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
870 TALLOC_FREE(smb_fname->base_name);
871 smb_fname->base_name = tmp;
874 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
880 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
881 SMB_STRUCT_STAT *sbuf)
886 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
890 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
891 fsp->fsp_name->base_name,
895 if (timestamp != 0) {
896 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
901 static int shadow_copy2_open(vfs_handle_struct *handle,
902 struct smb_filename *smb_fname, files_struct *fsp,
903 int flags, mode_t mode)
906 char *stripped, *tmp;
907 int ret, saved_errno;
909 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
910 smb_fname->base_name,
911 ×tamp, &stripped)) {
914 if (timestamp == 0) {
915 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
918 tmp = smb_fname->base_name;
919 smb_fname->base_name = shadow_copy2_convert(
920 talloc_tos(), handle, stripped, timestamp);
921 TALLOC_FREE(stripped);
923 if (smb_fname->base_name == NULL) {
924 smb_fname->base_name = tmp;
928 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
931 TALLOC_FREE(smb_fname->base_name);
932 smb_fname->base_name = tmp;
938 static int shadow_copy2_unlink(vfs_handle_struct *handle,
939 const struct smb_filename *smb_fname)
943 int ret, saved_errno;
944 struct smb_filename *conv;
946 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
947 smb_fname->base_name,
948 ×tamp, &stripped)) {
951 if (timestamp == 0) {
952 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
954 conv = cp_smb_filename(talloc_tos(), smb_fname);
959 conv->base_name = shadow_copy2_convert(
960 conv, handle, stripped, timestamp);
961 TALLOC_FREE(stripped);
962 if (conv->base_name == NULL) {
965 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
972 static int shadow_copy2_chmod(vfs_handle_struct *handle,
973 const struct smb_filename *smb_fname,
977 char *stripped = NULL;
978 int ret, saved_errno;
980 struct smb_filename *conv_smb_fname;
982 if (!shadow_copy2_strip_snapshot(talloc_tos(),
984 smb_fname->base_name,
989 if (timestamp == 0) {
990 TALLOC_FREE(stripped);
991 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
993 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
994 TALLOC_FREE(stripped);
998 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1002 if (conv_smb_fname == NULL) {
1008 ret = SMB_VFS_NEXT_CHMOD(handle, conv_smb_fname, mode);
1009 saved_errno = errno;
1011 TALLOC_FREE(conv_smb_fname);
1012 errno = saved_errno;
1016 static int shadow_copy2_chown(vfs_handle_struct *handle, const char *fname,
1017 uid_t uid, gid_t gid)
1021 int ret, saved_errno;
1024 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1025 ×tamp, &stripped)) {
1028 if (timestamp == 0) {
1029 return SMB_VFS_NEXT_CHOWN(handle, fname, uid, gid);
1031 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1032 TALLOC_FREE(stripped);
1036 ret = SMB_VFS_NEXT_CHOWN(handle, conv, uid, gid);
1037 saved_errno = errno;
1039 errno = saved_errno;
1043 static int shadow_copy2_chdir(vfs_handle_struct *handle,
1048 int ret, saved_errno;
1051 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1052 ×tamp, &stripped)) {
1055 if (timestamp == 0) {
1056 return SMB_VFS_NEXT_CHDIR(handle, fname);
1058 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1059 TALLOC_FREE(stripped);
1063 ret = SMB_VFS_NEXT_CHDIR(handle, conv);
1064 saved_errno = errno;
1066 errno = saved_errno;
1070 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
1071 const struct smb_filename *smb_fname,
1072 struct smb_file_time *ft)
1076 int ret, saved_errno;
1077 struct smb_filename *conv;
1079 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1080 smb_fname->base_name,
1081 ×tamp, &stripped)) {
1084 if (timestamp == 0) {
1085 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1087 conv = cp_smb_filename(talloc_tos(), smb_fname);
1092 conv->base_name = shadow_copy2_convert(
1093 conv, handle, stripped, timestamp);
1094 TALLOC_FREE(stripped);
1095 if (conv->base_name == NULL) {
1098 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
1099 saved_errno = errno;
1101 errno = saved_errno;
1105 static int shadow_copy2_readlink(vfs_handle_struct *handle,
1106 const char *fname, char *buf, size_t bufsiz)
1110 int ret, saved_errno;
1113 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1114 ×tamp, &stripped)) {
1117 if (timestamp == 0) {
1118 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
1120 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1121 TALLOC_FREE(stripped);
1125 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1126 saved_errno = errno;
1128 errno = saved_errno;
1132 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1133 const char *fname, mode_t mode, SMB_DEV_T dev)
1137 int ret, saved_errno;
1140 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1141 ×tamp, &stripped)) {
1144 if (timestamp == 0) {
1145 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
1147 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1148 TALLOC_FREE(stripped);
1152 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1153 saved_errno = errno;
1155 errno = saved_errno;
1159 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
1163 char *stripped = NULL;
1165 char *result = NULL;
1168 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1169 ×tamp, &stripped)) {
1172 if (timestamp == 0) {
1173 return SMB_VFS_NEXT_REALPATH(handle, fname);
1176 tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1181 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1184 saved_errno = errno;
1186 TALLOC_FREE(stripped);
1187 errno = saved_errno;
1192 * Check whether a given directory contains a
1193 * snapshot directory as direct subdirectory.
1194 * If yes, return the path of the snapshot-subdir,
1195 * otherwise return NULL.
1197 static char *have_snapdir(struct vfs_handle_struct *handle,
1200 struct smb_filename smb_fname;
1202 struct shadow_copy2_config *config;
1204 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1207 ZERO_STRUCT(smb_fname);
1208 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1209 path, config->snapdir);
1210 if (smb_fname.base_name == NULL) {
1214 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1215 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1216 return smb_fname.base_name;
1218 TALLOC_FREE(smb_fname.base_name);
1222 static bool check_access_snapdir(struct vfs_handle_struct *handle,
1225 struct smb_filename smb_fname;
1229 ZERO_STRUCT(smb_fname);
1230 smb_fname.base_name = talloc_asprintf(talloc_tos(),
1233 if (smb_fname.base_name == NULL) {
1237 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1238 if (ret != 0 || !S_ISDIR(smb_fname.st.st_ex_mode)) {
1239 TALLOC_FREE(smb_fname.base_name);
1243 status = smbd_check_access_rights(handle->conn,
1247 if (!NT_STATUS_IS_OK(status)) {
1248 DEBUG(0,("user does not have list permission "
1250 smb_fname.base_name));
1251 TALLOC_FREE(smb_fname.base_name);
1254 TALLOC_FREE(smb_fname.base_name);
1259 * Find the snapshot directory (if any) for the given
1260 * filename (which is relative to the share).
1262 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1263 struct vfs_handle_struct *handle,
1264 struct smb_filename *smb_fname)
1267 const char *snapdir;
1268 struct shadow_copy2_config *config;
1270 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1274 * If the non-snapdisrseverywhere mode, we should not search!
1276 if (!config->snapdirseverywhere) {
1277 return config->snapshot_basepath;
1280 path = talloc_asprintf(mem_ctx, "%s/%s",
1281 handle->conn->connectpath,
1282 smb_fname->base_name);
1287 snapdir = have_snapdir(handle, path);
1288 if (snapdir != NULL) {
1293 while ((p = strrchr(path, '/')) && (p > path)) {
1297 snapdir = have_snapdir(handle, path);
1298 if (snapdir != NULL) {
1307 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1309 char *gmt, size_t gmt_len)
1311 struct tm timestamp;
1313 unsigned long int timestamp_long;
1315 struct shadow_copy2_config *config;
1317 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1320 fmt = config->gmt_format;
1322 ZERO_STRUCT(timestamp);
1323 if (config->use_sscanf) {
1324 if (sscanf(name, fmt, ×tamp_long) != 1) {
1325 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1326 "no sscanf match %s: %s\n",
1330 timestamp_t = timestamp_long;
1331 gmtime_r(×tamp_t, ×tamp);
1333 if (strptime(name, fmt, ×tamp) == NULL) {
1334 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1335 "no match %s: %s\n",
1339 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1342 if (config->use_localtime) {
1343 timestamp.tm_isdst = -1;
1344 timestamp_t = mktime(×tamp);
1345 gmtime_r(×tamp_t, ×tamp);
1349 strftime(gmt, gmt_len, GMT_FORMAT, ×tamp);
1353 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1355 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1358 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1360 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1364 sort the shadow copy data in ascending or descending order
1366 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1367 struct shadow_copy_data *shadow_copy2_data)
1369 int (*cmpfunc)(const void *, const void *);
1371 struct shadow_copy2_config *config;
1373 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1376 sort = config->sort_order;
1381 if (strcmp(sort, "asc") == 0) {
1382 cmpfunc = shadow_copy2_label_cmp_asc;
1383 } else if (strcmp(sort, "desc") == 0) {
1384 cmpfunc = shadow_copy2_label_cmp_desc;
1389 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1390 shadow_copy2_data->labels)
1392 TYPESAFE_QSORT(shadow_copy2_data->labels,
1393 shadow_copy2_data->num_volumes,
1398 static int shadow_copy2_get_shadow_copy_data(
1399 vfs_handle_struct *handle, files_struct *fsp,
1400 struct shadow_copy_data *shadow_copy2_data,
1404 const char *snapdir;
1405 struct smb_filename *snapdir_smb_fname = NULL;
1407 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1410 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1411 if (snapdir == NULL) {
1412 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1413 handle->conn->connectpath));
1415 talloc_free(tmp_ctx);
1418 ret = check_access_snapdir(handle, snapdir);
1420 DEBUG(0,("access denied on listing snapdir %s\n", snapdir));
1422 talloc_free(tmp_ctx);
1426 snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
1430 if (snapdir_smb_fname == NULL) {
1432 talloc_free(tmp_ctx);
1436 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir_smb_fname, NULL, 0);
1439 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1440 " - %s\n", snapdir, strerror(errno)));
1441 talloc_free(tmp_ctx);
1446 shadow_copy2_data->num_volumes = 0;
1447 shadow_copy2_data->labels = NULL;
1449 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
1450 char snapshot[GMT_NAME_LEN+1];
1451 SHADOW_COPY_LABEL *tlabels;
1454 * ignore names not of the right form in the snapshot
1457 if (!shadow_copy2_snapshot_to_gmt(
1459 snapshot, sizeof(snapshot))) {
1461 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1462 "ignoring %s\n", d->d_name));
1465 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1466 d->d_name, snapshot));
1469 /* the caller doesn't want the labels */
1470 shadow_copy2_data->num_volumes++;
1474 tlabels = talloc_realloc(shadow_copy2_data,
1475 shadow_copy2_data->labels,
1477 shadow_copy2_data->num_volumes+1);
1478 if (tlabels == NULL) {
1479 DEBUG(0,("shadow_copy2: out of memory\n"));
1480 SMB_VFS_NEXT_CLOSEDIR(handle, p);
1481 talloc_free(tmp_ctx);
1485 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
1488 shadow_copy2_data->num_volumes++;
1489 shadow_copy2_data->labels = tlabels;
1492 SMB_VFS_NEXT_CLOSEDIR(handle,p);
1494 shadow_copy2_sort_data(handle, shadow_copy2_data);
1496 talloc_free(tmp_ctx);
1500 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
1501 struct files_struct *fsp,
1502 uint32_t security_info,
1503 TALLOC_CTX *mem_ctx,
1504 struct security_descriptor **ppdesc)
1510 struct smb_filename *smb_fname = NULL;
1512 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1513 fsp->fsp_name->base_name,
1514 ×tamp, &stripped)) {
1515 return map_nt_error_from_unix(errno);
1517 if (timestamp == 0) {
1518 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1522 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1523 TALLOC_FREE(stripped);
1525 return map_nt_error_from_unix(errno);
1527 smb_fname = synthetic_smb_fname(talloc_tos(),
1531 if (smb_fname == NULL) {
1533 return NT_STATUS_NO_MEMORY;
1536 status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1539 TALLOC_FREE(smb_fname);
1543 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
1544 const struct smb_filename *smb_fname,
1545 uint32_t security_info,
1546 TALLOC_CTX *mem_ctx,
1547 struct security_descriptor **ppdesc)
1553 struct smb_filename *conv_smb_fname = NULL;
1555 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1557 smb_fname->base_name,
1560 return map_nt_error_from_unix(errno);
1562 if (timestamp == 0) {
1563 return SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1566 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1567 TALLOC_FREE(stripped);
1569 return map_nt_error_from_unix(errno);
1571 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1575 if (conv_smb_fname == NULL) {
1577 return NT_STATUS_NO_MEMORY;
1579 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv_smb_fname, security_info,
1582 TALLOC_FREE(conv_smb_fname);
1586 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
1587 const struct smb_filename *smb_fname,
1592 int ret, saved_errno;
1594 struct smb_filename *conv_smb_fname = NULL;
1596 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1598 smb_fname->base_name,
1603 if (timestamp == 0) {
1604 return SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
1606 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1607 TALLOC_FREE(stripped);
1611 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1615 if (conv_smb_fname == NULL) {
1619 ret = SMB_VFS_NEXT_MKDIR(handle, conv_smb_fname, mode);
1620 saved_errno = errno;
1622 TALLOC_FREE(conv_smb_fname);
1623 errno = saved_errno;
1627 static int shadow_copy2_rmdir(vfs_handle_struct *handle,
1628 const struct smb_filename *smb_fname)
1632 int ret, saved_errno;
1634 struct smb_filename *conv_smb_fname = NULL;
1636 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1638 smb_fname->base_name,
1643 if (timestamp == 0) {
1644 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
1646 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1647 TALLOC_FREE(stripped);
1651 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1655 if (conv_smb_fname == NULL) {
1659 ret = SMB_VFS_NEXT_RMDIR(handle, conv_smb_fname);
1660 saved_errno = errno;
1661 TALLOC_FREE(conv_smb_fname);
1663 errno = saved_errno;
1667 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
1672 int ret, saved_errno;
1675 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1676 ×tamp, &stripped)) {
1679 if (timestamp == 0) {
1680 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
1682 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1683 TALLOC_FREE(stripped);
1687 ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
1688 saved_errno = errno;
1690 errno = saved_errno;
1694 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
1695 const char *fname, const char *aname,
1696 void *value, size_t size)
1704 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1705 ×tamp, &stripped)) {
1708 if (timestamp == 0) {
1709 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
1712 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1713 TALLOC_FREE(stripped);
1717 ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
1718 saved_errno = errno;
1720 errno = saved_errno;
1724 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
1726 char *list, size_t size)
1734 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1735 ×tamp, &stripped)) {
1738 if (timestamp == 0) {
1739 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
1741 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1742 TALLOC_FREE(stripped);
1746 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
1747 saved_errno = errno;
1749 errno = saved_errno;
1753 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
1754 const char *fname, const char *aname)
1758 int ret, saved_errno;
1761 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1762 ×tamp, &stripped)) {
1765 if (timestamp == 0) {
1766 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
1768 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1769 TALLOC_FREE(stripped);
1773 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
1774 saved_errno = errno;
1776 errno = saved_errno;
1780 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
1782 const char *aname, const void *value,
1783 size_t size, int flags)
1791 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1792 ×tamp, &stripped)) {
1795 if (timestamp == 0) {
1796 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
1799 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1800 TALLOC_FREE(stripped);
1804 ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
1805 saved_errno = errno;
1807 errno = saved_errno;
1811 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
1812 const struct smb_filename *smb_fname,
1820 struct smb_filename *conv_smb_fname = NULL;
1822 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1824 smb_fname->base_name,
1829 if (timestamp == 0) {
1830 return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
1832 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1833 TALLOC_FREE(stripped);
1837 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1841 if (conv_smb_fname == NULL) {
1846 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv_smb_fname, mode);
1847 saved_errno = errno;
1849 TALLOC_FREE(conv_smb_fname);
1850 errno = saved_errno;
1854 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
1857 TALLOC_CTX *mem_ctx,
1866 DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
1867 "name=[%s]\n", path, name));
1869 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1870 ×tamp, &stripped)) {
1871 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
1874 if (timestamp == 0) {
1875 DEBUG(10, ("timestamp == 0\n"));
1876 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
1877 mem_ctx, found_name);
1879 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1880 TALLOC_FREE(stripped);
1882 DEBUG(10, ("shadow_copy2_convert failed\n"));
1885 DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
1886 "name=[%s]\n", conv, name));
1887 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
1888 mem_ctx, found_name);
1889 DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
1890 saved_errno = errno;
1892 errno = saved_errno;
1896 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
1900 char *stripped = NULL;
1902 char *result = NULL;
1904 size_t rootpath_len = 0;
1906 DBG_DEBUG("Calc connect path for [%s]\n", fname);
1908 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1909 ×tamp, &stripped)) {
1912 if (timestamp == 0) {
1913 return SMB_VFS_NEXT_CONNECTPATH(handle, fname);
1916 tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
1922 DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
1923 (int)rootpath_len, tmp);
1925 tmp[rootpath_len] = '\0';
1926 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1927 if (result == NULL) {
1931 DBG_DEBUG("connect path is [%s]\n", result);
1934 saved_errno = errno;
1936 TALLOC_FREE(stripped);
1937 errno = saved_errno;
1941 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
1942 const char *path, uint64_t *bsize,
1943 uint64_t *dfree, uint64_t *dsize)
1951 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1952 ×tamp, &stripped)) {
1955 if (timestamp == 0) {
1956 return SMB_VFS_NEXT_DISK_FREE(handle, path,
1957 bsize, dfree, dsize);
1960 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1961 TALLOC_FREE(stripped);
1966 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, bsize, dfree, dsize);
1968 saved_errno = errno;
1970 errno = saved_errno;
1975 static int shadow_copy2_get_quota(vfs_handle_struct *handle, const char *path,
1976 enum SMB_QUOTA_TYPE qtype, unid_t id,
1985 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path, ×tamp,
1989 if (timestamp == 0) {
1990 return SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, dq);
1993 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1994 TALLOC_FREE(stripped);
1999 ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv, qtype, id, dq);
2001 saved_errno = errno;
2003 errno = saved_errno;
2008 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
2009 const char *service, const char *user)
2011 struct shadow_copy2_config *config;
2013 const char *snapdir;
2014 const char *gmt_format;
2015 const char *sort_order;
2016 const char *basedir = NULL;
2017 const char *snapsharepath = NULL;
2018 const char *mount_point;
2020 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
2021 (unsigned)handle->conn->cnum,
2022 handle->conn->connectpath));
2024 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
2029 config = talloc_zero(handle->conn, struct shadow_copy2_config);
2030 if (config == NULL) {
2031 DEBUG(0, ("talloc_zero() failed\n"));
2036 gmt_format = lp_parm_const_string(SNUM(handle->conn),
2039 config->gmt_format = talloc_strdup(config, gmt_format);
2040 if (config->gmt_format == NULL) {
2041 DEBUG(0, ("talloc_strdup() failed\n"));
2046 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
2047 "shadow", "sscanf", false);
2049 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
2050 "shadow", "localtime",
2053 snapdir = lp_parm_const_string(SNUM(handle->conn),
2054 "shadow", "snapdir",
2056 config->snapdir = talloc_strdup(config, snapdir);
2057 if (config->snapdir == NULL) {
2058 DEBUG(0, ("talloc_strdup() failed\n"));
2063 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
2065 "snapdirseverywhere",
2068 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
2069 "shadow", "crossmountpoints",
2072 if (config->crossmountpoints && !config->snapdirseverywhere) {
2073 DBG_WARNING("Warning: 'crossmountpoints' depends on "
2074 "'snapdirseverywhere'. Disabling crossmountpoints.\n");
2077 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
2078 "shadow", "fixinodes",
2081 sort_order = lp_parm_const_string(SNUM(handle->conn),
2082 "shadow", "sort", "desc");
2083 config->sort_order = talloc_strdup(config, sort_order);
2084 if (config->sort_order == NULL) {
2085 DEBUG(0, ("talloc_strdup() failed\n"));
2090 mount_point = lp_parm_const_string(SNUM(handle->conn),
2091 "shadow", "mountpoint", NULL);
2092 if (mount_point != NULL) {
2093 if (mount_point[0] != '/') {
2094 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
2095 "relative ('%s'), but it has to be an "
2096 "absolute path. Ignoring provided value.\n",
2101 p = strstr(handle->conn->connectpath, mount_point);
2102 if (p != handle->conn->connectpath) {
2103 DBG_WARNING("Warning: the share root (%s) is "
2104 "not a subdirectory of the "
2105 "specified mountpoint (%s). "
2106 "Ignoring provided value.\n",
2107 handle->conn->connectpath,
2114 if (mount_point != NULL) {
2115 config->mount_point = talloc_strdup(config, mount_point);
2116 if (config->mount_point == NULL) {
2117 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
2121 config->mount_point = shadow_copy2_find_mount_point(config,
2123 if (config->mount_point == NULL) {
2124 DBG_WARNING("shadow_copy2_find_mount_point "
2125 "of the share root '%s' failed: %s\n",
2126 handle->conn->connectpath, strerror(errno));
2131 basedir = lp_parm_const_string(SNUM(handle->conn),
2132 "shadow", "basedir", NULL);
2134 if (basedir != NULL) {
2135 if (basedir[0] != '/') {
2136 DEBUG(1, (__location__ " Warning: 'basedir' is "
2137 "relative ('%s'), but it has to be an "
2138 "absolute path. Disabling basedir.\n",
2143 p = strstr(basedir, config->mount_point);
2145 DEBUG(1, ("Warning: basedir (%s) is not a "
2146 "subdirectory of the share root's "
2147 "mount point (%s). "
2148 "Disabling basedir\n",
2149 basedir, config->mount_point));
2155 if (config->snapdirseverywhere && basedir != NULL) {
2156 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
2157 "with 'snapdirseverywhere'. Disabling basedir.\n"));
2161 snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
2162 "snapsharepath", NULL);
2163 if (snapsharepath != NULL) {
2164 if (snapsharepath[0] == '/') {
2165 DBG_WARNING("Warning: 'snapsharepath' is "
2166 "absolute ('%s'), but it has to be a "
2167 "relative path. Disabling snapsharepath.\n",
2169 snapsharepath = NULL;
2171 if (config->snapdirseverywhere && snapsharepath != NULL) {
2172 DBG_WARNING("Warning: 'snapsharepath' is incompatible "
2173 "with 'snapdirseverywhere'. Disabling "
2174 "snapsharepath.\n");
2175 snapsharepath = NULL;
2179 if (basedir != NULL && snapsharepath != NULL) {
2180 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
2181 "'basedir'. Disabling snapsharepath\n");
2182 snapsharepath = NULL;
2185 if (snapsharepath != NULL) {
2186 config->rel_connectpath = talloc_strdup(config, snapsharepath);
2187 if (config->rel_connectpath == NULL) {
2188 DBG_ERR("talloc_strdup() failed\n");
2194 if (basedir == NULL) {
2195 basedir = config->mount_point;
2198 if (config->rel_connectpath == NULL &&
2199 strlen(basedir) != strlen(handle->conn->connectpath)) {
2200 config->rel_connectpath = talloc_strdup(config,
2201 handle->conn->connectpath + strlen(basedir));
2202 if (config->rel_connectpath == NULL) {
2203 DEBUG(0, ("talloc_strdup() failed\n"));
2209 if (config->snapdir[0] == '/') {
2210 config->snapdir_absolute = true;
2212 if (config->snapdirseverywhere == true) {
2213 DEBUG(1, (__location__ " Warning: An absolute snapdir "
2214 "is incompatible with 'snapdirseverywhere', "
2215 "setting 'snapdirseverywhere' to false.\n"));
2216 config->snapdirseverywhere = false;
2219 if (config->crossmountpoints == true) {
2220 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
2221 "is not supported with an absolute snapdir. "
2222 "Disabling it.\n"));
2223 config->crossmountpoints = false;
2226 config->snapshot_basepath = config->snapdir;
2228 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
2229 config->mount_point, config->snapdir);
2230 if (config->snapshot_basepath == NULL) {
2231 DEBUG(0, ("talloc_asprintf() failed\n"));
2237 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
2238 " share root: '%s'\n"
2239 " mountpoint: '%s'\n"
2240 " rel share root: '%s'\n"
2242 " snapshot base path: '%s'\n"
2245 " snapdirs everywhere: %s\n"
2246 " cross mountpoints: %s\n"
2250 handle->conn->connectpath,
2251 config->mount_point,
2252 config->rel_connectpath,
2254 config->snapshot_basepath,
2256 config->use_sscanf ? "yes" : "no",
2257 config->snapdirseverywhere ? "yes" : "no",
2258 config->crossmountpoints ? "yes" : "no",
2259 config->fixinodes ? "yes" : "no",
2264 SMB_VFS_HANDLE_SET_DATA(handle, config,
2265 NULL, struct shadow_copy2_config,
2271 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
2272 .connect_fn = shadow_copy2_connect,
2273 .opendir_fn = shadow_copy2_opendir,
2274 .disk_free_fn = shadow_copy2_disk_free,
2275 .get_quota_fn = shadow_copy2_get_quota,
2276 .rename_fn = shadow_copy2_rename,
2277 .link_fn = shadow_copy2_link,
2278 .symlink_fn = shadow_copy2_symlink,
2279 .stat_fn = shadow_copy2_stat,
2280 .lstat_fn = shadow_copy2_lstat,
2281 .fstat_fn = shadow_copy2_fstat,
2282 .open_fn = shadow_copy2_open,
2283 .unlink_fn = shadow_copy2_unlink,
2284 .chmod_fn = shadow_copy2_chmod,
2285 .chown_fn = shadow_copy2_chown,
2286 .chdir_fn = shadow_copy2_chdir,
2287 .ntimes_fn = shadow_copy2_ntimes,
2288 .readlink_fn = shadow_copy2_readlink,
2289 .mknod_fn = shadow_copy2_mknod,
2290 .realpath_fn = shadow_copy2_realpath,
2291 .get_nt_acl_fn = shadow_copy2_get_nt_acl,
2292 .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
2293 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
2294 .mkdir_fn = shadow_copy2_mkdir,
2295 .rmdir_fn = shadow_copy2_rmdir,
2296 .getxattr_fn = shadow_copy2_getxattr,
2297 .listxattr_fn = shadow_copy2_listxattr,
2298 .removexattr_fn = shadow_copy2_removexattr,
2299 .setxattr_fn = shadow_copy2_setxattr,
2300 .chmod_acl_fn = shadow_copy2_chmod_acl,
2301 .chflags_fn = shadow_copy2_chflags,
2302 .get_real_filename_fn = shadow_copy2_get_real_filename,
2303 .connectpath_fn = shadow_copy2_connectpath,
2306 NTSTATUS vfs_shadow_copy2_init(void);
2307 NTSTATUS vfs_shadow_copy2_init(void)
2309 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2310 "shadow_copy2", &vfs_shadow_copy2_fns);