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 */
55 * shadow_copy2 private structure. This structure will be
56 * used to keep module specific information
58 struct shadow_copy2_private {
59 struct shadow_copy2_config *config;
62 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
64 unsigned *pnum_offsets)
73 while ((p = strchr(p, '/')) != NULL) {
78 offsets = talloc_array(mem_ctx, size_t, num_offsets);
79 if (offsets == NULL) {
85 while ((p = strchr(p, '/')) != NULL) {
86 offsets[num_offsets] = p-str;
92 *pnum_offsets = num_offsets;
97 * Given a timestamp, build the posix level GMT-tag string
98 * based on the configurable format.
100 static ssize_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
102 char *snaptime_string,
106 ssize_t snaptime_len;
107 struct shadow_copy2_config *config;
108 struct shadow_copy2_private *priv;
110 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
113 config = priv->config;
115 if (config->use_sscanf) {
116 snaptime_len = snprintf(snaptime_string,
119 (unsigned long)snapshot);
120 if (snaptime_len <= 0) {
121 DEBUG(10, ("snprintf failed\n"));
125 if (config->use_localtime) {
126 if (localtime_r(&snapshot, &snap_tm) == 0) {
127 DEBUG(10, ("gmtime_r failed\n"));
131 if (gmtime_r(&snapshot, &snap_tm) == 0) {
132 DEBUG(10, ("gmtime_r failed\n"));
136 snaptime_len = strftime(snaptime_string,
140 if (snaptime_len == 0) {
141 DEBUG(10, ("strftime failed\n"));
150 * Given a timestamp, build the string to insert into a path
151 * as a path component for creating the local path to the
152 * snapshot at the given timestamp of the input path.
154 * In the case of a parallel snapdir (specified with an
155 * absolute path), this is the inital portion of the
156 * local path of any snapshot file. The complete path is
157 * obtained by appending the portion of the file's path
158 * below the share root's mountpoint.
160 static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
161 struct vfs_handle_struct *handle,
164 fstring snaptime_string;
165 ssize_t snaptime_len = 0;
167 struct shadow_copy2_config *config;
168 struct shadow_copy2_private *priv;
170 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
173 config = priv->config;
175 snaptime_len = shadow_copy2_posix_gmt_string(handle,
178 sizeof(snaptime_string));
179 if (snaptime_len <= 0) {
183 if (config->snapdir_absolute) {
184 result = talloc_asprintf(mem_ctx, "%s/%s",
185 config->snapdir, snaptime_string);
187 result = talloc_asprintf(mem_ctx, "/%s/%s",
188 config->snapdir, snaptime_string);
190 if (result == NULL) {
191 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
198 * Build the posix snapshot path for the connection
199 * at the given timestamp, i.e. the absolute posix path
200 * that contains the snapshot for this file system.
202 * This only applies to classical case, i.e. not
203 * to the "snapdirseverywhere" mode.
205 static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
206 struct vfs_handle_struct *handle,
209 fstring snaptime_string;
210 ssize_t snaptime_len = 0;
212 struct shadow_copy2_private *priv;
214 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
217 snaptime_len = shadow_copy2_posix_gmt_string(handle,
220 sizeof(snaptime_string));
221 if (snaptime_len <= 0) {
225 result = talloc_asprintf(mem_ctx, "%s/%s",
226 priv->config->snapshot_basepath, snaptime_string);
227 if (result == NULL) {
228 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
235 * Strip a snapshot component from a filename as
236 * handed in via the smb layer.
237 * Returns the parsed timestamp and the stripped filename.
239 static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
240 struct vfs_handle_struct *handle,
250 size_t rest_len, dst_len;
251 struct shadow_copy2_private *priv;
254 ptrdiff_t len_before_gmt;
256 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
259 DEBUG(10, (__location__ ": enter path '%s'\n", name));
261 p = strstr_m(name, "@GMT-");
263 DEBUG(11, ("@GMT not found\n"));
266 if ((p > name) && (p[-1] != '/')) {
267 /* the GMT-token does not start a path-component */
268 DEBUG(10, ("not at start, p=%p, name=%p, p[-1]=%d\n",
269 p, name, (int)p[-1]));
274 * Figure out whether we got an already converted string. One
275 * case where this happens is in a smb2 create call with the
276 * mxac create blob set. We do the get_acl call on
277 * fsp->fsp_name, which is already converted. We are converted
278 * if we got a file name of the form ".snapshots/@GMT-",
279 * i.e. ".snapshots/" precedes "p".
282 snapdir = lp_parm_const_string(SNUM(handle->conn), "shadow", "snapdir",
284 snapdirlen = strlen(snapdir);
285 len_before_gmt = p - name;
287 if ((len_before_gmt >= (snapdirlen + 1)) && (p[-1] == '/')) {
288 const char *parent_snapdir = p - (snapdirlen+1);
290 DEBUG(10, ("parent_snapdir = %s\n", parent_snapdir));
292 if (strncmp(parent_snapdir, snapdir, snapdirlen) == 0) {
293 DEBUG(10, ("name=%s is already converted\n", name));
297 q = strptime(p, GMT_FORMAT, &tm);
299 DEBUG(10, ("strptime failed\n"));
303 timestamp = timegm(&tm);
304 if (timestamp == (time_t)-1) {
305 DEBUG(10, ("timestamp==-1\n"));
310 * The name consists of only the GMT token or the GMT
311 * token is at the end of the path. XP seems to send
312 * @GMT- at the end under certain circumstances even
313 * with a path prefix.
315 if (pstripped != NULL) {
316 stripped = talloc_strndup(mem_ctx, name, p - name);
317 if (stripped == NULL) {
320 *pstripped = stripped;
322 *ptimestamp = timestamp;
327 * It is not a complete path component, i.e. the path
328 * component continues after the gmt-token.
330 DEBUG(10, ("q[0] = %d\n", (int)q[0]));
335 rest_len = strlen(q);
336 dst_len = (p-name) + rest_len;
338 if (priv->config->snapdirseverywhere) {
341 insert = shadow_copy2_insert_string(talloc_tos(), handle,
343 if (insert == NULL) {
348 DEBUG(10, (__location__ ": snapdirseverywhere mode.\n"
350 "insert string '%s'\n", name, insert));
352 have_insert = (strstr(name, insert+1) != NULL);
353 DEBUG(10, ("have_insert=%d, name=%s, insert+1=%s\n",
354 (int)have_insert, name, insert+1));
356 DEBUG(10, (__location__ ": insert string '%s' found in "
357 "path '%s' found in snapdirseverywhere mode "
358 "==> already converted\n", insert, name));
367 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
370 if (snapshot_path == NULL) {
375 DEBUG(10, (__location__ " path: '%s'.\n"
376 "snapshot path: '%s'\n", name, snapshot_path));
378 s = strstr(name, snapshot_path);
381 * this starts with "snapshot_basepath/GMT-Token"
382 * so it is already a converted absolute
383 * path. Don't process further.
385 DEBUG(10, (__location__ ": path '%s' starts with "
386 "snapshot path '%s' (not in "
387 "snapdirseverywhere mode) ==> "
388 "already converted\n", name, snapshot_path));
389 talloc_free(snapshot_path);
392 talloc_free(snapshot_path);
395 if (pstripped != NULL) {
396 stripped = talloc_array(mem_ctx, char, dst_len+1);
397 if (stripped == NULL) {
402 memcpy(stripped, name, p-name);
405 memcpy(stripped + (p-name), q, rest_len);
407 stripped[dst_len] = '\0';
408 *pstripped = stripped;
410 *ptimestamp = timestamp;
417 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
418 vfs_handle_struct *handle)
420 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
425 if (stat(path, &st) != 0) {
432 while ((p = strrchr(path, '/')) && p > path) {
434 if (stat(path, &st) != 0) {
438 if (st.st_dev != dev) {
448 * Convert from a name as handed in via the SMB layer
449 * and a timestamp into the local path of the snapshot
450 * of the provided file at the provided time.
451 * Also return the path in the snapshot corresponding
452 * to the file's share root.
454 static char *shadow_copy2_do_convert(TALLOC_CTX *mem_ctx,
455 struct vfs_handle_struct *handle,
456 const char *name, time_t timestamp,
457 size_t *snaproot_len)
459 struct smb_filename converted_fname;
461 size_t *slashes = NULL;
462 unsigned num_slashes;
466 char *converted = NULL;
467 size_t insertlen, connectlen = 0;
470 struct shadow_copy2_config *config;
471 struct shadow_copy2_private *priv;
472 size_t in_share_offset = 0;
474 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
477 config = priv->config;
479 DEBUG(10, ("converting '%s'\n", name));
481 if (!config->snapdirseverywhere) {
485 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
488 if (snapshot_path == NULL) {
492 if (config->rel_connectpath == NULL) {
493 converted = talloc_asprintf(mem_ctx, "%s/%s",
494 snapshot_path, name);
496 converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
498 config->rel_connectpath,
501 if (converted == NULL) {
505 ZERO_STRUCT(converted_fname);
506 converted_fname.base_name = converted;
508 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
509 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
511 ret, ret == 0 ? "ok" : strerror(errno)));
513 DEBUG(10, ("Found %s\n", converted));
516 if (snaproot_len != NULL) {
517 *snaproot_len = strlen(snapshot_path);
518 if (config->rel_connectpath != NULL) {
520 strlen(config->rel_connectpath) + 1;
528 /* never reached ... */
531 connectlen = strlen(handle->conn->connectpath);
533 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
535 path = talloc_asprintf(
536 mem_ctx, "%s/%s", handle->conn->connectpath, name);
542 pathlen = talloc_get_size(path)-1;
544 if (!shadow_copy2_find_slashes(talloc_tos(), path,
545 &slashes, &num_slashes)) {
549 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
550 if (insert == NULL) {
553 insertlen = talloc_get_size(insert)-1;
556 * Note: We deliberatly don't expensively initialize the
557 * array with talloc_zero here: Putting zero into
558 * converted[pathlen+insertlen] below is sufficient, because
559 * in the following for loop, the insert string is inserted
560 * at various slash places. So the memory up to position
561 * pathlen+insertlen will always be initialized when the
562 * converted string is used.
564 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
565 if (converted == NULL) {
569 if (path[pathlen-1] != '/') {
571 * Append a fake slash to find the snapshot root
574 tmp = talloc_realloc(talloc_tos(), slashes,
575 size_t, num_slashes+1);
580 slashes[num_slashes] = pathlen;
586 if (!config->crossmountpoints) {
587 min_offset = strlen(config->mount_point);
590 memcpy(converted, path, pathlen+1);
591 converted[pathlen+insertlen] = '\0';
593 ZERO_STRUCT(converted_fname);
594 converted_fname.base_name = converted;
596 for (i = num_slashes-1; i>=0; i--) {
602 if (offset < min_offset) {
607 if (offset >= connectlen) {
608 in_share_offset = offset;
611 memcpy(converted+offset, insert, insertlen);
614 memcpy(converted+offset, path + slashes[i],
615 pathlen - slashes[i]);
617 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
619 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
621 ret, ret == 0 ? "ok" : strerror(errno)));
624 if (snaproot_len != NULL) {
625 *snaproot_len = in_share_offset + insertlen;
629 if (errno == ENOTDIR) {
631 * This is a valid condition: We appended the
632 * .snaphots/@GMT.. to a file name. Just try
633 * with the upper levels.
637 if (errno != ENOENT) {
638 /* Other problem than "not found" */
647 DEBUG(10, ("Found %s\n", converted));
655 TALLOC_FREE(converted);
657 TALLOC_FREE(slashes);
664 * Convert from a name as handed in via the SMB layer
665 * and a timestamp into the local path of the snapshot
666 * of the provided file at the provided time.
668 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
669 struct vfs_handle_struct *handle,
670 const char *name, time_t timestamp)
672 return shadow_copy2_do_convert(mem_ctx, handle, name, timestamp, NULL);
676 modify a sbuf return to ensure that inodes in the shadow directory
677 are different from those in the main directory
679 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
680 SMB_STRUCT_STAT *sbuf)
682 struct shadow_copy2_private *priv;
684 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
687 if (priv->config->fixinodes) {
688 /* some snapshot systems, like GPFS, return the name
689 device:inode for the snapshot files as the current
690 files. That breaks the 'restore' button in the shadow copy
691 GUI, as the client gets a sharing violation.
693 This is a crude way of allowing both files to be
694 open at once. It has a slight chance of inode
695 number collision, but I can't see a better approach
696 without significant VFS changes
698 TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
699 .dsize = strlen(fname) };
702 shash = tdb_jenkins_hash(&key) & 0xFF000000;
706 sbuf->st_ex_ino ^= shash;
710 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
711 const struct smb_filename *smb_fname,
720 struct smb_filename *conv_smb_fname = NULL;
722 if (!shadow_copy2_strip_snapshot(talloc_tos(),
724 smb_fname->base_name,
729 if (timestamp == 0) {
730 return SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
732 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
733 TALLOC_FREE(stripped);
737 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
742 if (conv_smb_fname == NULL) {
746 ret = SMB_VFS_NEXT_OPENDIR(handle, conv_smb_fname, mask, attr);
749 TALLOC_FREE(conv_smb_fname);
754 static int shadow_copy2_rename(vfs_handle_struct *handle,
755 const struct smb_filename *smb_fname_src,
756 const struct smb_filename *smb_fname_dst)
758 time_t timestamp_src, timestamp_dst;
760 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
761 smb_fname_src->base_name,
762 ×tamp_src, NULL)) {
765 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
766 smb_fname_dst->base_name,
767 ×tamp_dst, NULL)) {
770 if (timestamp_src != 0) {
774 if (timestamp_dst != 0) {
778 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
781 static int shadow_copy2_symlink(vfs_handle_struct *handle,
782 const char *oldname, const char *newname)
784 time_t timestamp_old, timestamp_new;
786 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
787 ×tamp_old, NULL)) {
790 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
791 ×tamp_new, NULL)) {
794 if ((timestamp_old != 0) || (timestamp_new != 0)) {
798 return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
801 static int shadow_copy2_link(vfs_handle_struct *handle,
802 const char *oldname, const char *newname)
804 time_t timestamp_old, timestamp_new;
806 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
807 ×tamp_old, NULL)) {
810 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
811 ×tamp_new, NULL)) {
814 if ((timestamp_old != 0) || (timestamp_new != 0)) {
818 return SMB_VFS_NEXT_LINK(handle, oldname, newname);
821 static int shadow_copy2_stat(vfs_handle_struct *handle,
822 struct smb_filename *smb_fname)
825 char *stripped, *tmp;
826 int ret, saved_errno;
828 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
829 smb_fname->base_name,
830 ×tamp, &stripped)) {
833 if (timestamp == 0) {
834 return SMB_VFS_NEXT_STAT(handle, smb_fname);
837 tmp = smb_fname->base_name;
838 smb_fname->base_name = shadow_copy2_convert(
839 talloc_tos(), handle, stripped, timestamp);
840 TALLOC_FREE(stripped);
842 if (smb_fname->base_name == NULL) {
843 smb_fname->base_name = tmp;
847 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
850 TALLOC_FREE(smb_fname->base_name);
851 smb_fname->base_name = tmp;
854 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
860 static int shadow_copy2_lstat(vfs_handle_struct *handle,
861 struct smb_filename *smb_fname)
864 char *stripped, *tmp;
865 int ret, saved_errno;
867 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
868 smb_fname->base_name,
869 ×tamp, &stripped)) {
872 if (timestamp == 0) {
873 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
876 tmp = smb_fname->base_name;
877 smb_fname->base_name = shadow_copy2_convert(
878 talloc_tos(), handle, stripped, timestamp);
879 TALLOC_FREE(stripped);
881 if (smb_fname->base_name == NULL) {
882 smb_fname->base_name = tmp;
886 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
889 TALLOC_FREE(smb_fname->base_name);
890 smb_fname->base_name = tmp;
893 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
899 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
900 SMB_STRUCT_STAT *sbuf)
905 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
909 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
910 fsp->fsp_name->base_name,
914 if (timestamp != 0) {
915 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
920 static int shadow_copy2_open(vfs_handle_struct *handle,
921 struct smb_filename *smb_fname, files_struct *fsp,
922 int flags, mode_t mode)
925 char *stripped, *tmp;
926 int ret, saved_errno;
928 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
929 smb_fname->base_name,
930 ×tamp, &stripped)) {
933 if (timestamp == 0) {
934 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
937 tmp = smb_fname->base_name;
938 smb_fname->base_name = shadow_copy2_convert(
939 talloc_tos(), handle, stripped, timestamp);
940 TALLOC_FREE(stripped);
942 if (smb_fname->base_name == NULL) {
943 smb_fname->base_name = tmp;
947 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
950 TALLOC_FREE(smb_fname->base_name);
951 smb_fname->base_name = tmp;
957 static int shadow_copy2_unlink(vfs_handle_struct *handle,
958 const struct smb_filename *smb_fname)
962 int ret, saved_errno;
963 struct smb_filename *conv;
965 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
966 smb_fname->base_name,
967 ×tamp, &stripped)) {
970 if (timestamp == 0) {
971 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
973 conv = cp_smb_filename(talloc_tos(), smb_fname);
978 conv->base_name = shadow_copy2_convert(
979 conv, handle, stripped, timestamp);
980 TALLOC_FREE(stripped);
981 if (conv->base_name == NULL) {
984 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
991 static int shadow_copy2_chmod(vfs_handle_struct *handle,
992 const struct smb_filename *smb_fname,
996 char *stripped = NULL;
997 int ret, saved_errno;
999 struct smb_filename *conv_smb_fname;
1001 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1003 smb_fname->base_name,
1008 if (timestamp == 0) {
1009 TALLOC_FREE(stripped);
1010 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1012 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1013 TALLOC_FREE(stripped);
1017 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1022 if (conv_smb_fname == NULL) {
1028 ret = SMB_VFS_NEXT_CHMOD(handle, conv_smb_fname, mode);
1029 saved_errno = errno;
1031 TALLOC_FREE(conv_smb_fname);
1032 errno = saved_errno;
1036 static int shadow_copy2_chown(vfs_handle_struct *handle,
1037 const struct smb_filename *smb_fname,
1043 int ret, saved_errno;
1045 struct smb_filename *conv_smb_fname = NULL;
1047 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1049 smb_fname->base_name,
1054 if (timestamp == 0) {
1055 return SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
1057 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1058 TALLOC_FREE(stripped);
1062 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1067 if (conv_smb_fname == NULL) {
1072 ret = SMB_VFS_NEXT_CHOWN(handle, conv_smb_fname, uid, gid);
1073 saved_errno = errno;
1075 TALLOC_FREE(conv_smb_fname);
1076 errno = saved_errno;
1080 static int shadow_copy2_chdir(vfs_handle_struct *handle,
1085 int ret, saved_errno;
1088 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1089 ×tamp, &stripped)) {
1092 if (timestamp == 0) {
1093 return SMB_VFS_NEXT_CHDIR(handle, fname);
1095 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1096 TALLOC_FREE(stripped);
1100 ret = SMB_VFS_NEXT_CHDIR(handle, conv);
1101 saved_errno = errno;
1103 errno = saved_errno;
1107 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
1108 const struct smb_filename *smb_fname,
1109 struct smb_file_time *ft)
1113 int ret, saved_errno;
1114 struct smb_filename *conv;
1116 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1117 smb_fname->base_name,
1118 ×tamp, &stripped)) {
1121 if (timestamp == 0) {
1122 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1124 conv = cp_smb_filename(talloc_tos(), smb_fname);
1129 conv->base_name = shadow_copy2_convert(
1130 conv, handle, stripped, timestamp);
1131 TALLOC_FREE(stripped);
1132 if (conv->base_name == NULL) {
1135 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
1136 saved_errno = errno;
1138 errno = saved_errno;
1142 static int shadow_copy2_readlink(vfs_handle_struct *handle,
1143 const char *fname, char *buf, size_t bufsiz)
1147 int ret, saved_errno;
1150 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1151 ×tamp, &stripped)) {
1154 if (timestamp == 0) {
1155 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
1157 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1158 TALLOC_FREE(stripped);
1162 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1163 saved_errno = errno;
1165 errno = saved_errno;
1169 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1170 const char *fname, mode_t mode, SMB_DEV_T dev)
1174 int ret, saved_errno;
1177 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1178 ×tamp, &stripped)) {
1181 if (timestamp == 0) {
1182 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
1184 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1185 TALLOC_FREE(stripped);
1189 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1190 saved_errno = errno;
1192 errno = saved_errno;
1196 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
1200 char *stripped = NULL;
1202 char *result = NULL;
1205 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1206 ×tamp, &stripped)) {
1209 if (timestamp == 0) {
1210 return SMB_VFS_NEXT_REALPATH(handle, fname);
1213 tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1218 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1221 saved_errno = errno;
1223 TALLOC_FREE(stripped);
1224 errno = saved_errno;
1229 * Check whether a given directory contains a
1230 * snapshot directory as direct subdirectory.
1231 * If yes, return the path of the snapshot-subdir,
1232 * otherwise return NULL.
1234 static char *have_snapdir(struct vfs_handle_struct *handle,
1237 struct smb_filename smb_fname;
1239 struct shadow_copy2_private *priv;
1241 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1244 ZERO_STRUCT(smb_fname);
1245 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1246 path, priv->config->snapdir);
1247 if (smb_fname.base_name == NULL) {
1251 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1252 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1253 return smb_fname.base_name;
1255 TALLOC_FREE(smb_fname.base_name);
1259 static bool check_access_snapdir(struct vfs_handle_struct *handle,
1262 struct smb_filename smb_fname;
1266 ZERO_STRUCT(smb_fname);
1267 smb_fname.base_name = talloc_asprintf(talloc_tos(),
1270 if (smb_fname.base_name == NULL) {
1274 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1275 if (ret != 0 || !S_ISDIR(smb_fname.st.st_ex_mode)) {
1276 TALLOC_FREE(smb_fname.base_name);
1280 status = smbd_check_access_rights(handle->conn,
1284 if (!NT_STATUS_IS_OK(status)) {
1285 DEBUG(0,("user does not have list permission "
1287 smb_fname.base_name));
1288 TALLOC_FREE(smb_fname.base_name);
1291 TALLOC_FREE(smb_fname.base_name);
1296 * Find the snapshot directory (if any) for the given
1297 * filename (which is relative to the share).
1299 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1300 struct vfs_handle_struct *handle,
1301 struct smb_filename *smb_fname)
1304 const char *snapdir;
1305 struct shadow_copy2_config *config;
1306 struct shadow_copy2_private *priv;
1308 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1311 config = priv->config;
1314 * If the non-snapdisrseverywhere mode, we should not search!
1316 if (!config->snapdirseverywhere) {
1317 return config->snapshot_basepath;
1320 path = talloc_asprintf(mem_ctx, "%s/%s",
1321 handle->conn->connectpath,
1322 smb_fname->base_name);
1327 snapdir = have_snapdir(handle, path);
1328 if (snapdir != NULL) {
1333 while ((p = strrchr(path, '/')) && (p > path)) {
1337 snapdir = have_snapdir(handle, path);
1338 if (snapdir != NULL) {
1347 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1349 char *gmt, size_t gmt_len)
1351 struct tm timestamp;
1353 unsigned long int timestamp_long;
1355 struct shadow_copy2_config *config;
1356 struct shadow_copy2_private *priv;
1358 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1361 config = priv->config;
1363 fmt = config->gmt_format;
1365 ZERO_STRUCT(timestamp);
1366 if (config->use_sscanf) {
1367 if (sscanf(name, fmt, ×tamp_long) != 1) {
1368 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1369 "no sscanf match %s: %s\n",
1373 timestamp_t = timestamp_long;
1374 gmtime_r(×tamp_t, ×tamp);
1376 if (strptime(name, fmt, ×tamp) == NULL) {
1377 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1378 "no match %s: %s\n",
1382 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1385 if (config->use_localtime) {
1386 timestamp.tm_isdst = -1;
1387 timestamp_t = mktime(×tamp);
1388 gmtime_r(×tamp_t, ×tamp);
1392 strftime(gmt, gmt_len, GMT_FORMAT, ×tamp);
1396 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1398 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1401 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1403 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1407 sort the shadow copy data in ascending or descending order
1409 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1410 struct shadow_copy_data *shadow_copy2_data)
1412 int (*cmpfunc)(const void *, const void *);
1414 struct shadow_copy2_private *priv;
1416 SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1419 sort = priv->config->sort_order;
1424 if (strcmp(sort, "asc") == 0) {
1425 cmpfunc = shadow_copy2_label_cmp_asc;
1426 } else if (strcmp(sort, "desc") == 0) {
1427 cmpfunc = shadow_copy2_label_cmp_desc;
1432 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1433 shadow_copy2_data->labels)
1435 TYPESAFE_QSORT(shadow_copy2_data->labels,
1436 shadow_copy2_data->num_volumes,
1441 static int shadow_copy2_get_shadow_copy_data(
1442 vfs_handle_struct *handle, files_struct *fsp,
1443 struct shadow_copy_data *shadow_copy2_data,
1447 const char *snapdir;
1448 struct smb_filename *snapdir_smb_fname = NULL;
1450 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1453 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1454 if (snapdir == NULL) {
1455 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1456 handle->conn->connectpath));
1458 talloc_free(tmp_ctx);
1461 ret = check_access_snapdir(handle, snapdir);
1463 DEBUG(0,("access denied on listing snapdir %s\n", snapdir));
1465 talloc_free(tmp_ctx);
1469 snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
1473 fsp->fsp_name->flags);
1474 if (snapdir_smb_fname == NULL) {
1476 talloc_free(tmp_ctx);
1480 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir_smb_fname, NULL, 0);
1483 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1484 " - %s\n", snapdir, strerror(errno)));
1485 talloc_free(tmp_ctx);
1490 shadow_copy2_data->num_volumes = 0;
1491 shadow_copy2_data->labels = NULL;
1493 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
1494 char snapshot[GMT_NAME_LEN+1];
1495 SHADOW_COPY_LABEL *tlabels;
1498 * ignore names not of the right form in the snapshot
1501 if (!shadow_copy2_snapshot_to_gmt(
1503 snapshot, sizeof(snapshot))) {
1505 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1506 "ignoring %s\n", d->d_name));
1509 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1510 d->d_name, snapshot));
1513 /* the caller doesn't want the labels */
1514 shadow_copy2_data->num_volumes++;
1518 tlabels = talloc_realloc(shadow_copy2_data,
1519 shadow_copy2_data->labels,
1521 shadow_copy2_data->num_volumes+1);
1522 if (tlabels == NULL) {
1523 DEBUG(0,("shadow_copy2: out of memory\n"));
1524 SMB_VFS_NEXT_CLOSEDIR(handle, p);
1525 talloc_free(tmp_ctx);
1529 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
1532 shadow_copy2_data->num_volumes++;
1533 shadow_copy2_data->labels = tlabels;
1536 SMB_VFS_NEXT_CLOSEDIR(handle,p);
1538 shadow_copy2_sort_data(handle, shadow_copy2_data);
1540 talloc_free(tmp_ctx);
1544 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
1545 struct files_struct *fsp,
1546 uint32_t security_info,
1547 TALLOC_CTX *mem_ctx,
1548 struct security_descriptor **ppdesc)
1554 struct smb_filename *smb_fname = NULL;
1556 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1557 fsp->fsp_name->base_name,
1558 ×tamp, &stripped)) {
1559 return map_nt_error_from_unix(errno);
1561 if (timestamp == 0) {
1562 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, 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 smb_fname = synthetic_smb_fname(talloc_tos(),
1575 fsp->fsp_name->flags);
1576 if (smb_fname == NULL) {
1578 return NT_STATUS_NO_MEMORY;
1581 status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1584 TALLOC_FREE(smb_fname);
1588 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
1589 const struct smb_filename *smb_fname,
1590 uint32_t security_info,
1591 TALLOC_CTX *mem_ctx,
1592 struct security_descriptor **ppdesc)
1598 struct smb_filename *conv_smb_fname = NULL;
1600 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1602 smb_fname->base_name,
1605 return map_nt_error_from_unix(errno);
1607 if (timestamp == 0) {
1608 return SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1611 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1612 TALLOC_FREE(stripped);
1614 return map_nt_error_from_unix(errno);
1616 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1621 if (conv_smb_fname == NULL) {
1623 return NT_STATUS_NO_MEMORY;
1625 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv_smb_fname, security_info,
1628 TALLOC_FREE(conv_smb_fname);
1632 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
1633 const struct smb_filename *smb_fname,
1638 int ret, saved_errno;
1640 struct smb_filename *conv_smb_fname = NULL;
1642 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1644 smb_fname->base_name,
1649 if (timestamp == 0) {
1650 return SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
1652 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1653 TALLOC_FREE(stripped);
1657 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1662 if (conv_smb_fname == NULL) {
1666 ret = SMB_VFS_NEXT_MKDIR(handle, conv_smb_fname, mode);
1667 saved_errno = errno;
1669 TALLOC_FREE(conv_smb_fname);
1670 errno = saved_errno;
1674 static int shadow_copy2_rmdir(vfs_handle_struct *handle,
1675 const struct smb_filename *smb_fname)
1679 int ret, saved_errno;
1681 struct smb_filename *conv_smb_fname = NULL;
1683 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1685 smb_fname->base_name,
1690 if (timestamp == 0) {
1691 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
1693 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1694 TALLOC_FREE(stripped);
1698 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1703 if (conv_smb_fname == NULL) {
1707 ret = SMB_VFS_NEXT_RMDIR(handle, conv_smb_fname);
1708 saved_errno = errno;
1709 TALLOC_FREE(conv_smb_fname);
1711 errno = saved_errno;
1715 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
1720 int ret, saved_errno;
1723 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1724 ×tamp, &stripped)) {
1727 if (timestamp == 0) {
1728 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
1730 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1731 TALLOC_FREE(stripped);
1735 ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
1736 saved_errno = errno;
1738 errno = saved_errno;
1742 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
1743 const char *fname, const char *aname,
1744 void *value, size_t size)
1752 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1753 ×tamp, &stripped)) {
1756 if (timestamp == 0) {
1757 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
1760 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1761 TALLOC_FREE(stripped);
1765 ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
1766 saved_errno = errno;
1768 errno = saved_errno;
1772 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
1774 char *list, size_t size)
1782 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1783 ×tamp, &stripped)) {
1786 if (timestamp == 0) {
1787 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
1789 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1790 TALLOC_FREE(stripped);
1794 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
1795 saved_errno = errno;
1797 errno = saved_errno;
1801 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
1802 const char *fname, const char *aname)
1806 int ret, saved_errno;
1809 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1810 ×tamp, &stripped)) {
1813 if (timestamp == 0) {
1814 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
1816 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1817 TALLOC_FREE(stripped);
1821 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
1822 saved_errno = errno;
1824 errno = saved_errno;
1828 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
1830 const char *aname, const void *value,
1831 size_t size, int flags)
1839 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1840 ×tamp, &stripped)) {
1843 if (timestamp == 0) {
1844 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
1847 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1848 TALLOC_FREE(stripped);
1852 ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
1853 saved_errno = errno;
1855 errno = saved_errno;
1859 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
1860 const struct smb_filename *smb_fname,
1868 struct smb_filename *conv_smb_fname = NULL;
1870 if (!shadow_copy2_strip_snapshot(talloc_tos(),
1872 smb_fname->base_name,
1877 if (timestamp == 0) {
1878 return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
1880 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1881 TALLOC_FREE(stripped);
1885 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1890 if (conv_smb_fname == NULL) {
1895 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv_smb_fname, mode);
1896 saved_errno = errno;
1898 TALLOC_FREE(conv_smb_fname);
1899 errno = saved_errno;
1903 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
1906 TALLOC_CTX *mem_ctx,
1915 DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
1916 "name=[%s]\n", path, name));
1918 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1919 ×tamp, &stripped)) {
1920 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
1923 if (timestamp == 0) {
1924 DEBUG(10, ("timestamp == 0\n"));
1925 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
1926 mem_ctx, found_name);
1928 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1929 TALLOC_FREE(stripped);
1931 DEBUG(10, ("shadow_copy2_convert failed\n"));
1934 DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
1935 "name=[%s]\n", conv, name));
1936 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
1937 mem_ctx, found_name);
1938 DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
1939 saved_errno = errno;
1941 errno = saved_errno;
1945 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
1949 char *stripped = NULL;
1951 char *result = NULL;
1953 size_t rootpath_len = 0;
1955 DBG_DEBUG("Calc connect path for [%s]\n", fname);
1957 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1958 ×tamp, &stripped)) {
1961 if (timestamp == 0) {
1962 return SMB_VFS_NEXT_CONNECTPATH(handle, fname);
1965 tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
1971 DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
1972 (int)rootpath_len, tmp);
1974 tmp[rootpath_len] = '\0';
1975 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1976 if (result == NULL) {
1980 DBG_DEBUG("connect path is [%s]\n", result);
1983 saved_errno = errno;
1985 TALLOC_FREE(stripped);
1986 errno = saved_errno;
1990 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
1991 const char *path, uint64_t *bsize,
1992 uint64_t *dfree, uint64_t *dsize)
2000 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
2001 ×tamp, &stripped)) {
2004 if (timestamp == 0) {
2005 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2006 bsize, dfree, dsize);
2009 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2010 TALLOC_FREE(stripped);
2015 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, bsize, dfree, dsize);
2017 saved_errno = errno;
2019 errno = saved_errno;
2024 static int shadow_copy2_get_quota(vfs_handle_struct *handle, const char *path,
2025 enum SMB_QUOTA_TYPE qtype, unid_t id,
2034 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path, ×tamp,
2038 if (timestamp == 0) {
2039 return SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, dq);
2042 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2043 TALLOC_FREE(stripped);
2048 ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv, qtype, id, dq);
2050 saved_errno = errno;
2052 errno = saved_errno;
2057 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
2058 const char *service, const char *user)
2060 struct shadow_copy2_config *config;
2061 struct shadow_copy2_private *priv;
2063 const char *snapdir;
2064 const char *gmt_format;
2065 const char *sort_order;
2066 const char *basedir = NULL;
2067 const char *snapsharepath = NULL;
2068 const char *mount_point;
2070 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
2071 (unsigned)handle->conn->cnum,
2072 handle->conn->connectpath));
2074 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
2079 priv = talloc_zero(handle->conn, struct shadow_copy2_private);
2081 DEBUG(0, ("talloc_zero() failed\n"));
2086 config = talloc_zero(priv, struct shadow_copy2_config);
2087 if (config == NULL) {
2088 DEBUG(0, ("talloc_zero() failed\n"));
2093 priv->config = config;
2095 gmt_format = lp_parm_const_string(SNUM(handle->conn),
2098 config->gmt_format = talloc_strdup(config, gmt_format);
2099 if (config->gmt_format == NULL) {
2100 DEBUG(0, ("talloc_strdup() failed\n"));
2105 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
2106 "shadow", "sscanf", false);
2108 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
2109 "shadow", "localtime",
2112 snapdir = lp_parm_const_string(SNUM(handle->conn),
2113 "shadow", "snapdir",
2115 config->snapdir = talloc_strdup(config, snapdir);
2116 if (config->snapdir == NULL) {
2117 DEBUG(0, ("talloc_strdup() failed\n"));
2122 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
2124 "snapdirseverywhere",
2127 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
2128 "shadow", "crossmountpoints",
2131 if (config->crossmountpoints && !config->snapdirseverywhere) {
2132 DBG_WARNING("Warning: 'crossmountpoints' depends on "
2133 "'snapdirseverywhere'. Disabling crossmountpoints.\n");
2136 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
2137 "shadow", "fixinodes",
2140 sort_order = lp_parm_const_string(SNUM(handle->conn),
2141 "shadow", "sort", "desc");
2142 config->sort_order = talloc_strdup(config, sort_order);
2143 if (config->sort_order == NULL) {
2144 DEBUG(0, ("talloc_strdup() failed\n"));
2149 mount_point = lp_parm_const_string(SNUM(handle->conn),
2150 "shadow", "mountpoint", NULL);
2151 if (mount_point != NULL) {
2152 if (mount_point[0] != '/') {
2153 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
2154 "relative ('%s'), but it has to be an "
2155 "absolute path. Ignoring provided value.\n",
2160 p = strstr(handle->conn->connectpath, mount_point);
2161 if (p != handle->conn->connectpath) {
2162 DBG_WARNING("Warning: the share root (%s) is "
2163 "not a subdirectory of the "
2164 "specified mountpoint (%s). "
2165 "Ignoring provided value.\n",
2166 handle->conn->connectpath,
2173 if (mount_point != NULL) {
2174 config->mount_point = talloc_strdup(config, mount_point);
2175 if (config->mount_point == NULL) {
2176 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
2180 config->mount_point = shadow_copy2_find_mount_point(config,
2182 if (config->mount_point == NULL) {
2183 DBG_WARNING("shadow_copy2_find_mount_point "
2184 "of the share root '%s' failed: %s\n",
2185 handle->conn->connectpath, strerror(errno));
2190 basedir = lp_parm_const_string(SNUM(handle->conn),
2191 "shadow", "basedir", NULL);
2193 if (basedir != NULL) {
2194 if (basedir[0] != '/') {
2195 DEBUG(1, (__location__ " Warning: 'basedir' is "
2196 "relative ('%s'), but it has to be an "
2197 "absolute path. Disabling basedir.\n",
2202 p = strstr(basedir, config->mount_point);
2204 DEBUG(1, ("Warning: basedir (%s) is not a "
2205 "subdirectory of the share root's "
2206 "mount point (%s). "
2207 "Disabling basedir\n",
2208 basedir, config->mount_point));
2214 if (config->snapdirseverywhere && basedir != NULL) {
2215 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
2216 "with 'snapdirseverywhere'. Disabling basedir.\n"));
2220 snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
2221 "snapsharepath", NULL);
2222 if (snapsharepath != NULL) {
2223 if (snapsharepath[0] == '/') {
2224 DBG_WARNING("Warning: 'snapsharepath' is "
2225 "absolute ('%s'), but it has to be a "
2226 "relative path. Disabling snapsharepath.\n",
2228 snapsharepath = NULL;
2230 if (config->snapdirseverywhere && snapsharepath != NULL) {
2231 DBG_WARNING("Warning: 'snapsharepath' is incompatible "
2232 "with 'snapdirseverywhere'. Disabling "
2233 "snapsharepath.\n");
2234 snapsharepath = NULL;
2238 if (basedir != NULL && snapsharepath != NULL) {
2239 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
2240 "'basedir'. Disabling snapsharepath\n");
2241 snapsharepath = NULL;
2244 if (snapsharepath != NULL) {
2245 config->rel_connectpath = talloc_strdup(config, snapsharepath);
2246 if (config->rel_connectpath == NULL) {
2247 DBG_ERR("talloc_strdup() failed\n");
2253 if (basedir == NULL) {
2254 basedir = config->mount_point;
2257 if (config->rel_connectpath == NULL &&
2258 strlen(basedir) != strlen(handle->conn->connectpath)) {
2259 config->rel_connectpath = talloc_strdup(config,
2260 handle->conn->connectpath + strlen(basedir));
2261 if (config->rel_connectpath == NULL) {
2262 DEBUG(0, ("talloc_strdup() failed\n"));
2268 if (config->snapdir[0] == '/') {
2269 config->snapdir_absolute = true;
2271 if (config->snapdirseverywhere == true) {
2272 DEBUG(1, (__location__ " Warning: An absolute snapdir "
2273 "is incompatible with 'snapdirseverywhere', "
2274 "setting 'snapdirseverywhere' to false.\n"));
2275 config->snapdirseverywhere = false;
2278 if (config->crossmountpoints == true) {
2279 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
2280 "is not supported with an absolute snapdir. "
2281 "Disabling it.\n"));
2282 config->crossmountpoints = false;
2285 config->snapshot_basepath = config->snapdir;
2287 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
2288 config->mount_point, config->snapdir);
2289 if (config->snapshot_basepath == NULL) {
2290 DEBUG(0, ("talloc_asprintf() failed\n"));
2296 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
2297 " share root: '%s'\n"
2298 " mountpoint: '%s'\n"
2299 " rel share root: '%s'\n"
2301 " snapshot base path: '%s'\n"
2304 " snapdirs everywhere: %s\n"
2305 " cross mountpoints: %s\n"
2309 handle->conn->connectpath,
2310 config->mount_point,
2311 config->rel_connectpath,
2313 config->snapshot_basepath,
2315 config->use_sscanf ? "yes" : "no",
2316 config->snapdirseverywhere ? "yes" : "no",
2317 config->crossmountpoints ? "yes" : "no",
2318 config->fixinodes ? "yes" : "no",
2323 SMB_VFS_HANDLE_SET_DATA(handle, priv,
2324 NULL, struct shadow_copy2_private,
2330 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
2331 .connect_fn = shadow_copy2_connect,
2332 .opendir_fn = shadow_copy2_opendir,
2333 .disk_free_fn = shadow_copy2_disk_free,
2334 .get_quota_fn = shadow_copy2_get_quota,
2335 .rename_fn = shadow_copy2_rename,
2336 .link_fn = shadow_copy2_link,
2337 .symlink_fn = shadow_copy2_symlink,
2338 .stat_fn = shadow_copy2_stat,
2339 .lstat_fn = shadow_copy2_lstat,
2340 .fstat_fn = shadow_copy2_fstat,
2341 .open_fn = shadow_copy2_open,
2342 .unlink_fn = shadow_copy2_unlink,
2343 .chmod_fn = shadow_copy2_chmod,
2344 .chown_fn = shadow_copy2_chown,
2345 .chdir_fn = shadow_copy2_chdir,
2346 .ntimes_fn = shadow_copy2_ntimes,
2347 .readlink_fn = shadow_copy2_readlink,
2348 .mknod_fn = shadow_copy2_mknod,
2349 .realpath_fn = shadow_copy2_realpath,
2350 .get_nt_acl_fn = shadow_copy2_get_nt_acl,
2351 .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
2352 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
2353 .mkdir_fn = shadow_copy2_mkdir,
2354 .rmdir_fn = shadow_copy2_rmdir,
2355 .getxattr_fn = shadow_copy2_getxattr,
2356 .listxattr_fn = shadow_copy2_listxattr,
2357 .removexattr_fn = shadow_copy2_removexattr,
2358 .setxattr_fn = shadow_copy2_setxattr,
2359 .chmod_acl_fn = shadow_copy2_chmod_acl,
2360 .chflags_fn = shadow_copy2_chflags,
2361 .get_real_filename_fn = shadow_copy2_get_real_filename,
2362 .connectpath_fn = shadow_copy2_connectpath,
2365 NTSTATUS vfs_shadow_copy2_init(void);
2366 NTSTATUS vfs_shadow_copy2_init(void)
2368 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2369 "shadow_copy2", &vfs_shadow_copy2_fns);