2 * Third attempt at a shadow copy module
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
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 This is a 3rd implemetation of a shadow copy module for exposing
27 snapshots to windows clients as shadow copies. This version has the
30 1) you don't need to populate your shares with symlinks to the
31 snapshots. This can be very important when you have thousands of
32 shares, or use [homes]
34 2) the inode number of the files is altered so it is different
35 from the original. This allows the 'restore' button to work
36 without a sharing violation
38 3) shadow copy results can be sorted before being sent to the
39 client. This is beneficial for filesystems that don't read
40 directories alphabetically (the default unix).
42 4) vanity naming for snapshots. Snapshots can be named in any
43 format compatible with str[fp]time conversions.
45 5) time stamps in snapshot names can be represented in localtime
50 shadow:snapdir = <directory where snapshots are kept>
52 This is the directory containing the @GMT-* snapshot directories. If it is an absolute
53 path it is used as-is. If it is a relative path, then it is taken relative to the mount
54 point of the filesystem that the root of this share is on
56 shadow:basedir = <base directory that snapshots are from>
58 This is an optional parameter that specifies the directory that
59 the snapshots are relative to. It defaults to the filesystem
62 shadow:fixinodes = yes/no
64 If you enable shadow:fixinodes then this module will modify the
65 apparent inode number of files in the snapshot directories using
66 a hash of the files path. This is needed for snapshot systems
67 where the snapshots have the same device:inode number as the
68 original files (such as happens with GPFS snapshots). If you
69 don't set this option then the 'restore' button in the shadow
70 copy UI will fail with a sharing violation.
72 shadow:sort = asc/desc, or not specified for unsorted (default)
74 This is an optional parameter that specifies that the shadow
75 copy directories should be sorted before sending them to the
76 client. This can be beneficial as unix filesystems are usually
77 not listed alphabetically sorted. If enabled, you typically
78 want to specify descending order.
80 shadow:format = <format specification for snapshot names>
82 This is an optional parameter that specifies the format
83 specification for the naming of snapshots. The format must
84 be compatible with the conversion specifications recognized
85 by str[fp]time. The default value is "@GMT-%Y.%m.%d-%H.%M.%S".
87 shadow:sscanf = yes/no (default is no)
89 The time is the unsigned long integer (%lu) in the format string
90 rather than a time strptime() can parse. The result must be a unix time_t
93 shadow:localtime = yes/no (default is no)
95 This is an optional parameter that indicates whether the
96 snapshot names are in UTC/GMT or the local time.
99 The following command would generate a correctly formatted directory name
100 for use with the default parameters:
101 date -u +@GMT-%Y.%m.%d-%H.%M.%S
104 #include "includes.h"
105 #include "system/filesys.h"
106 #include "include/ntioctl.h"
107 #include <ccan/hash/hash.h>
108 #include "util_tdb.h"
110 static bool shadow_copy2_find_slashes(TALLOC_CTX
*mem_ctx
, const char *str
,
112 unsigned *pnum_offsets
)
114 unsigned num_offsets
;
121 while ((p
= strchr(p
, '/')) != NULL
) {
126 offsets
= talloc_array(mem_ctx
, size_t, num_offsets
);
127 if (offsets
== NULL
) {
133 while ((p
= strchr(p
, '/')) != NULL
) {
134 offsets
[num_offsets
] = p
-str
;
140 *pnum_offsets
= num_offsets
;
145 * Given a timstamp, build the string to insert into a path
146 * as a path component for creating the local path to the
147 * snapshot at the given timestamp of the input path.
149 static char *shadow_copy2_insert_string(TALLOC_CTX
*mem_ctx
,
150 struct vfs_handle_struct
*handle
,
155 fstring snaptime_string
;
158 fmt
= lp_parm_const_string(SNUM(handle
->conn
), "shadow",
159 "format", GMT_FORMAT
);
161 if (lp_parm_bool(SNUM(handle
->conn
), "shadow", "sscanf", false)) {
162 snaptime_len
= snprintf(snaptime_string
, sizeof(snaptime_string
), fmt
,
163 (unsigned long)snapshot
);
164 if (snaptime_len
<= 0) {
165 DEBUG(10, ("snprintf failed\n"));
169 if (lp_parm_bool(SNUM(handle
->conn
), "shadow", "localtime", false)) {
170 if (localtime_r(&snapshot
, &snap_tm
) == 0) {
171 DEBUG(10, ("gmtime_r failed\n"));
175 if (gmtime_r(&snapshot
, &snap_tm
) == 0) {
176 DEBUG(10, ("gmtime_r failed\n"));
180 snaptime_len
= strftime(snaptime_string
, sizeof(snaptime_string
), fmt
,
182 if (snaptime_len
== 0) {
183 DEBUG(10, ("strftime failed\n"));
187 return talloc_asprintf(mem_ctx
, "/%s/%s",
188 lp_parm_const_string(
189 SNUM(handle
->conn
), "shadow", "snapdir",
195 * Strip a snapshot component from an filename as
196 * handed in via the smb layer.
197 * Returns the parsed timestamp and the stripped filename.
199 static bool shadow_copy2_strip_snapshot(TALLOC_CTX
*mem_ctx
,
200 struct vfs_handle_struct
*handle
,
210 size_t rest_len
, dst_len
;
212 p
= strstr_m(name
, "@GMT-");
216 if ((p
> name
) && (p
[-1] != '/')) {
219 q
= strptime(p
, GMT_FORMAT
, &tm
);
224 timestamp
= timegm(&tm
);
225 if (timestamp
== (time_t)-1) {
228 if ((p
== name
) && (q
[0] == '\0')) {
229 if (pstripped
!= NULL
) {
230 stripped
= talloc_strdup(mem_ctx
, "");
231 if (stripped
== NULL
) {
234 *pstripped
= stripped
;
236 *ptimestamp
= timestamp
;
244 rest_len
= strlen(q
);
245 dst_len
= (p
-name
) + rest_len
;
247 if (lp_parm_bool(SNUM(handle
->conn
), "shadow", "snapdirseverywhere",
251 insert
= shadow_copy2_insert_string(talloc_tos(), handle
,
253 if (insert
== NULL
) {
258 have_insert
= (strstr(name
, insert
+1) != NULL
);
265 if (pstripped
!= NULL
) {
266 stripped
= talloc_array(mem_ctx
, char, dst_len
+1);
267 if (stripped
== NULL
) {
272 memcpy(stripped
, name
, p
-name
);
275 memcpy(stripped
+ (p
-name
), q
, rest_len
);
277 stripped
[dst_len
] = '\0';
278 *pstripped
= stripped
;
280 *ptimestamp
= timestamp
;
287 static char *shadow_copy2_find_mount_point(TALLOC_CTX
*mem_ctx
,
288 vfs_handle_struct
*handle
)
290 char *path
= talloc_strdup(mem_ctx
, handle
->conn
->connectpath
);
295 if (stat(path
, &st
) != 0) {
302 while ((p
= strrchr(path
, '/')) && p
> path
) {
304 if (stat(path
, &st
) != 0) {
308 if (st
.st_dev
!= dev
) {
317 static char *shadow_copy2_convert(TALLOC_CTX
*mem_ctx
,
318 struct vfs_handle_struct
*handle
,
319 const char *name
, time_t timestamp
)
321 struct smb_filename converted_fname
;
323 size_t *slashes
= NULL
;
324 unsigned num_slashes
;
328 char *converted
= NULL
;
333 path
= talloc_asprintf(mem_ctx
, "%s/%s", handle
->conn
->connectpath
,
339 pathlen
= talloc_get_size(path
)-1;
341 DEBUG(10, ("converting %s\n", path
));
343 if (!shadow_copy2_find_slashes(talloc_tos(), path
,
344 &slashes
, &num_slashes
)) {
347 insert
= shadow_copy2_insert_string(talloc_tos(), handle
, timestamp
);
348 if (insert
== NULL
) {
351 insertlen
= talloc_get_size(insert
)-1;
352 converted
= talloc_array(mem_ctx
, char, pathlen
+ insertlen
+ 1);
353 if (converted
== NULL
) {
357 if (path
[pathlen
-1] != '/') {
359 * Append a fake slash to find the snapshot root
362 tmp
= talloc_realloc(talloc_tos(), slashes
,
363 size_t, num_slashes
+1);
368 slashes
[num_slashes
] = pathlen
;
374 if (!lp_parm_bool(SNUM(handle
->conn
), "shadow", "crossmountpoints",
378 mount_point
= shadow_copy2_find_mount_point(talloc_tos(),
380 if (mount_point
== NULL
) {
383 min_offset
= strlen(mount_point
);
384 TALLOC_FREE(mount_point
);
387 memcpy(converted
, path
, pathlen
+1);
388 converted
[pathlen
+insertlen
] = '\0';
390 ZERO_STRUCT(converted_fname
);
391 converted_fname
.base_name
= converted
;
393 for (i
= num_slashes
-1; i
>=0; i
--) {
399 if (offset
< min_offset
) {
404 memcpy(converted
+offset
, insert
, insertlen
);
407 memcpy(converted
+offset
, path
+ slashes
[i
],
408 pathlen
- slashes
[i
]);
410 ret
= SMB_VFS_NEXT_LSTAT(handle
, &converted_fname
);
412 DEBUG(10, ("Trying %s: %d (%s)\n", converted
,
413 ret
, ret
== 0 ? "ok" : strerror(errno
)));
418 if (errno
== ENOTDIR
) {
420 * This is a valid condition: We appended the
421 * .snaphots/@GMT.. to a file name. Just try
422 * with the upper levels.
426 if (errno
!= ENOENT
) {
427 /* Other problem than "not found" */
436 DEBUG(10, ("Found %s\n", converted
));
444 TALLOC_FREE(converted
);
446 TALLOC_FREE(slashes
);
453 modify a sbuf return to ensure that inodes in the shadow directory
454 are different from those in the main directory
456 static void convert_sbuf(vfs_handle_struct
*handle
, const char *fname
,
457 SMB_STRUCT_STAT
*sbuf
)
459 if (lp_parm_bool(SNUM(handle
->conn
), "shadow", "fixinodes", False
)) {
460 /* some snapshot systems, like GPFS, return the name
461 device:inode for the snapshot files as the current
462 files. That breaks the 'restore' button in the shadow copy
463 GUI, as the client gets a sharing violation.
465 This is a crude way of allowing both files to be
466 open at once. It has a slight chance of inode
467 number collision, but I can't see a better approach
468 without significant VFS changes
472 shash
= hash(fname
, strlen(fname
), 0) & 0xFF000000;
476 sbuf
->st_ex_ino
^= shash
;
480 static DIR *shadow_copy2_opendir(vfs_handle_struct
*handle
,
491 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
492 ×tamp
, &stripped
)) {
495 if (timestamp
== 0) {
496 return SMB_VFS_NEXT_OPENDIR(handle
, fname
, mask
, attr
);
498 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
499 TALLOC_FREE(stripped
);
503 ret
= SMB_VFS_NEXT_OPENDIR(handle
, conv
, mask
, attr
);
510 static int shadow_copy2_rename(vfs_handle_struct
*handle
,
511 const struct smb_filename
*smb_fname_src
,
512 const struct smb_filename
*smb_fname_dst
)
514 time_t timestamp_src
, timestamp_dst
;
516 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
517 smb_fname_src
->base_name
,
518 ×tamp_src
, NULL
)) {
521 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
522 smb_fname_dst
->base_name
,
523 ×tamp_dst
, NULL
)) {
526 if (timestamp_src
!= 0) {
530 if (timestamp_dst
!= 0) {
534 return SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, smb_fname_dst
);
537 static int shadow_copy2_symlink(vfs_handle_struct
*handle
,
538 const char *oldname
, const char *newname
)
540 time_t timestamp_old
, timestamp_new
;
542 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, oldname
,
543 ×tamp_old
, NULL
)) {
546 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, newname
,
547 ×tamp_new
, NULL
)) {
550 if ((timestamp_old
!= 0) || (timestamp_new
!= 0)) {
554 return SMB_VFS_NEXT_SYMLINK(handle
, oldname
, newname
);
557 static int shadow_copy2_link(vfs_handle_struct
*handle
,
558 const char *oldname
, const char *newname
)
560 time_t timestamp_old
, timestamp_new
;
562 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, oldname
,
563 ×tamp_old
, NULL
)) {
566 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, newname
,
567 ×tamp_new
, NULL
)) {
570 if ((timestamp_old
!= 0) || (timestamp_new
!= 0)) {
574 return SMB_VFS_NEXT_LINK(handle
, oldname
, newname
);
577 static int shadow_copy2_stat(vfs_handle_struct
*handle
,
578 struct smb_filename
*smb_fname
)
581 char *stripped
, *tmp
;
582 int ret
, saved_errno
;
584 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
585 smb_fname
->base_name
,
586 ×tamp
, &stripped
)) {
589 if (timestamp
== 0) {
590 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
593 tmp
= smb_fname
->base_name
;
594 smb_fname
->base_name
= shadow_copy2_convert(
595 talloc_tos(), handle
, stripped
, timestamp
);
596 TALLOC_FREE(stripped
);
598 if (smb_fname
->base_name
== NULL
) {
599 smb_fname
->base_name
= tmp
;
603 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
606 TALLOC_FREE(smb_fname
->base_name
);
607 smb_fname
->base_name
= tmp
;
610 convert_sbuf(handle
, smb_fname
->base_name
, &smb_fname
->st
);
616 static int shadow_copy2_lstat(vfs_handle_struct
*handle
,
617 struct smb_filename
*smb_fname
)
620 char *stripped
, *tmp
;
621 int ret
, saved_errno
;
623 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
624 smb_fname
->base_name
,
625 ×tamp
, &stripped
)) {
628 if (timestamp
== 0) {
629 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
632 tmp
= smb_fname
->base_name
;
633 smb_fname
->base_name
= shadow_copy2_convert(
634 talloc_tos(), handle
, stripped
, timestamp
);
635 TALLOC_FREE(stripped
);
637 if (smb_fname
->base_name
== NULL
) {
638 smb_fname
->base_name
= tmp
;
642 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
645 TALLOC_FREE(smb_fname
->base_name
);
646 smb_fname
->base_name
= tmp
;
649 convert_sbuf(handle
, smb_fname
->base_name
, &smb_fname
->st
);
655 static int shadow_copy2_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
656 SMB_STRUCT_STAT
*sbuf
)
661 ret
= SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
665 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
666 fsp
->fsp_name
->base_name
,
670 if (timestamp
!= 0) {
671 convert_sbuf(handle
, fsp
->fsp_name
->base_name
, sbuf
);
676 static int shadow_copy2_open(vfs_handle_struct
*handle
,
677 struct smb_filename
*smb_fname
, files_struct
*fsp
,
678 int flags
, mode_t mode
)
681 char *stripped
, *tmp
;
682 int ret
, saved_errno
;
684 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
685 smb_fname
->base_name
,
686 ×tamp
, &stripped
)) {
689 if (timestamp
== 0) {
690 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
693 tmp
= smb_fname
->base_name
;
694 smb_fname
->base_name
= shadow_copy2_convert(
695 talloc_tos(), handle
, stripped
, timestamp
);
696 TALLOC_FREE(stripped
);
698 if (smb_fname
->base_name
== NULL
) {
699 smb_fname
->base_name
= tmp
;
703 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
706 TALLOC_FREE(smb_fname
->base_name
);
707 smb_fname
->base_name
= tmp
;
713 static int shadow_copy2_unlink(vfs_handle_struct
*handle
,
714 const struct smb_filename
*smb_fname
)
718 int ret
, saved_errno
;
719 struct smb_filename
*conv
;
721 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
722 smb_fname
->base_name
,
723 ×tamp
, &stripped
)) {
726 if (timestamp
== 0) {
727 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
729 conv
= cp_smb_filename(talloc_tos(), smb_fname
);
734 conv
->base_name
= shadow_copy2_convert(
735 conv
, handle
, stripped
, timestamp
);
736 TALLOC_FREE(stripped
);
737 if (conv
->base_name
== NULL
) {
740 ret
= SMB_VFS_NEXT_UNLINK(handle
, conv
);
747 static int shadow_copy2_chmod(vfs_handle_struct
*handle
, const char *fname
,
752 int ret
, saved_errno
;
755 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
756 ×tamp
, &stripped
)) {
759 if (timestamp
== 0) {
760 return SMB_VFS_NEXT_CHMOD(handle
, fname
, mode
);
762 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
763 TALLOC_FREE(stripped
);
767 ret
= SMB_VFS_NEXT_CHMOD(handle
, conv
, mode
);
774 static int shadow_copy2_chown(vfs_handle_struct
*handle
, const char *fname
,
775 uid_t uid
, gid_t gid
)
779 int ret
, saved_errno
;
782 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
783 ×tamp
, &stripped
)) {
786 if (timestamp
== 0) {
787 return SMB_VFS_NEXT_CHOWN(handle
, fname
, uid
, gid
);
789 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
790 TALLOC_FREE(stripped
);
794 ret
= SMB_VFS_NEXT_CHOWN(handle
, conv
, uid
, gid
);
801 static int shadow_copy2_chdir(vfs_handle_struct
*handle
,
806 int ret
, saved_errno
;
809 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
810 ×tamp
, &stripped
)) {
813 if (timestamp
== 0) {
814 return SMB_VFS_NEXT_CHDIR(handle
, fname
);
816 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
817 TALLOC_FREE(stripped
);
821 ret
= SMB_VFS_NEXT_CHDIR(handle
, conv
);
828 static int shadow_copy2_ntimes(vfs_handle_struct
*handle
,
829 const struct smb_filename
*smb_fname
,
830 struct smb_file_time
*ft
)
834 int ret
, saved_errno
;
835 struct smb_filename
*conv
;
837 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
838 smb_fname
->base_name
,
839 ×tamp
, &stripped
)) {
842 if (timestamp
== 0) {
843 return SMB_VFS_NEXT_NTIMES(handle
, smb_fname
, ft
);
845 conv
= cp_smb_filename(talloc_tos(), smb_fname
);
850 conv
->base_name
= shadow_copy2_convert(
851 conv
, handle
, stripped
, timestamp
);
852 TALLOC_FREE(stripped
);
853 if (conv
->base_name
== NULL
) {
856 ret
= SMB_VFS_NEXT_NTIMES(handle
, conv
, ft
);
863 static int shadow_copy2_readlink(vfs_handle_struct
*handle
,
864 const char *fname
, char *buf
, size_t bufsiz
)
868 int ret
, saved_errno
;
871 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
872 ×tamp
, &stripped
)) {
875 if (timestamp
== 0) {
876 return SMB_VFS_NEXT_READLINK(handle
, fname
, buf
, bufsiz
);
878 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
879 TALLOC_FREE(stripped
);
883 ret
= SMB_VFS_NEXT_READLINK(handle
, conv
, buf
, bufsiz
);
890 static int shadow_copy2_mknod(vfs_handle_struct
*handle
,
891 const char *fname
, mode_t mode
, SMB_DEV_T dev
)
895 int ret
, saved_errno
;
898 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
899 ×tamp
, &stripped
)) {
902 if (timestamp
== 0) {
903 return SMB_VFS_NEXT_MKNOD(handle
, fname
, mode
, dev
);
905 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
906 TALLOC_FREE(stripped
);
910 ret
= SMB_VFS_NEXT_MKNOD(handle
, conv
, mode
, dev
);
917 static char *shadow_copy2_realpath(vfs_handle_struct
*handle
,
921 char *stripped
= NULL
;
924 char *inserted
= NULL
;
925 char *inserted_to
, *inserted_end
;
928 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
929 ×tamp
, &stripped
)) {
932 if (timestamp
== 0) {
933 return SMB_VFS_NEXT_REALPATH(handle
, fname
);
936 tmp
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
941 result
= SMB_VFS_NEXT_REALPATH(handle
, tmp
);
942 if (result
== NULL
) {
947 * Take away what we've inserted. This removes the @GMT-thingy
948 * completely, but will give a path under the share root.
950 inserted
= shadow_copy2_insert_string(talloc_tos(), handle
, timestamp
);
951 if (inserted
== NULL
) {
954 inserted_to
= strstr_m(result
, inserted
);
955 if (inserted_to
== NULL
) {
956 DEBUG(2, ("SMB_VFS_NEXT_REALPATH removed %s\n", inserted
));
959 inserted_end
= inserted_to
+ talloc_get_size(inserted
) - 1;
960 memmove(inserted_to
, inserted_end
, strlen(inserted_end
)+1);
964 TALLOC_FREE(inserted
);
966 TALLOC_FREE(stripped
);
972 * Check whether a given directory contains a
973 * snapshot directory as direct subdirectory.
974 * If yes, return the path of the snapshot-subdir,
975 * otherwise return NULL.
977 static char *have_snapdir(struct vfs_handle_struct
*handle
,
980 struct smb_filename smb_fname
;
983 ZERO_STRUCT(smb_fname
);
984 smb_fname
.base_name
= talloc_asprintf(
985 talloc_tos(), "%s/%s", path
,
986 lp_parm_const_string(SNUM(handle
->conn
), "shadow", "snapdir",
988 if (smb_fname
.base_name
== NULL
) {
992 ret
= SMB_VFS_NEXT_STAT(handle
, &smb_fname
);
993 if ((ret
== 0) && (S_ISDIR(smb_fname
.st
.st_ex_mode
))) {
994 return smb_fname
.base_name
;
996 TALLOC_FREE(smb_fname
.base_name
);
1001 * Find the snapshot directory (if any) for the given
1002 * filename (which is relative to the share).
1004 static char *shadow_copy2_find_snapdir(TALLOC_CTX
*mem_ctx
,
1005 struct vfs_handle_struct
*handle
,
1006 struct smb_filename
*smb_fname
)
1011 path
= talloc_asprintf(mem_ctx
, "%s/%s",
1012 handle
->conn
->connectpath
,
1013 smb_fname
->base_name
);
1018 snapdir
= have_snapdir(handle
, path
);
1019 if (snapdir
!= NULL
) {
1024 while ((p
= strrchr(path
, '/')) && (p
> path
)) {
1028 snapdir
= have_snapdir(handle
, path
);
1029 if (snapdir
!= NULL
) {
1038 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct
*handle
,
1040 char *gmt
, size_t gmt_len
)
1042 struct tm timestamp
;
1044 unsigned long int timestamp_long
;
1047 fmt
= lp_parm_const_string(SNUM(handle
->conn
), "shadow",
1048 "format", GMT_FORMAT
);
1050 ZERO_STRUCT(timestamp
);
1051 if (lp_parm_bool(SNUM(handle
->conn
), "shadow", "sscanf", false)) {
1052 if (sscanf(name
, fmt
, ×tamp_long
) != 1) {
1053 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1054 "no sscanf match %s: %s\n",
1058 timestamp_t
= timestamp_long
;
1059 gmtime_r(×tamp_t
, ×tamp
);
1061 if (strptime(name
, fmt
, ×tamp
) == NULL
) {
1062 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1063 "no match %s: %s\n",
1067 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1070 if (lp_parm_bool(SNUM(handle
->conn
), "shadow", "localtime", false)) {
1071 timestamp
.tm_isdst
= -1;
1072 timestamp_t
= mktime(×tamp
);
1073 gmtime_r(×tamp_t
, ×tamp
);
1077 strftime(gmt
, gmt_len
, GMT_FORMAT
, ×tamp
);
1081 static int shadow_copy2_label_cmp_asc(const void *x
, const void *y
)
1083 return strncmp((const char *)x
, (const char *)y
, sizeof(SHADOW_COPY_LABEL
));
1086 static int shadow_copy2_label_cmp_desc(const void *x
, const void *y
)
1088 return -strncmp((const char *)x
, (const char *)y
, sizeof(SHADOW_COPY_LABEL
));
1092 sort the shadow copy data in ascending or descending order
1094 static void shadow_copy2_sort_data(vfs_handle_struct
*handle
,
1095 struct shadow_copy_data
*shadow_copy2_data
)
1097 int (*cmpfunc
)(const void *, const void *);
1100 sort
= lp_parm_const_string(SNUM(handle
->conn
), "shadow",
1106 if (strcmp(sort
, "asc") == 0) {
1107 cmpfunc
= shadow_copy2_label_cmp_asc
;
1108 } else if (strcmp(sort
, "desc") == 0) {
1109 cmpfunc
= shadow_copy2_label_cmp_desc
;
1114 if (shadow_copy2_data
&& shadow_copy2_data
->num_volumes
> 0 &&
1115 shadow_copy2_data
->labels
)
1117 TYPESAFE_QSORT(shadow_copy2_data
->labels
,
1118 shadow_copy2_data
->num_volumes
,
1123 static int shadow_copy2_get_shadow_copy_data(
1124 vfs_handle_struct
*handle
, files_struct
*fsp
,
1125 struct shadow_copy_data
*shadow_copy2_data
,
1129 const char *snapdir
;
1131 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1133 snapdir
= shadow_copy2_find_snapdir(tmp_ctx
, handle
, fsp
->fsp_name
);
1134 if (snapdir
== NULL
) {
1135 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1136 handle
->conn
->connectpath
));
1138 talloc_free(tmp_ctx
);
1142 p
= SMB_VFS_NEXT_OPENDIR(handle
, snapdir
, NULL
, 0);
1145 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1146 " - %s\n", snapdir
, strerror(errno
)));
1147 talloc_free(tmp_ctx
);
1152 shadow_copy2_data
->num_volumes
= 0;
1153 shadow_copy2_data
->labels
= NULL
;
1155 while ((d
= SMB_VFS_NEXT_READDIR(handle
, p
, NULL
))) {
1156 char snapshot
[GMT_NAME_LEN
+1];
1157 SHADOW_COPY_LABEL
*tlabels
;
1160 * ignore names not of the right form in the snapshot
1163 if (!shadow_copy2_snapshot_to_gmt(
1165 snapshot
, sizeof(snapshot
))) {
1167 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1168 "ignoring %s\n", d
->d_name
));
1171 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1172 d
->d_name
, snapshot
));
1175 /* the caller doesn't want the labels */
1176 shadow_copy2_data
->num_volumes
++;
1180 tlabels
= talloc_realloc(shadow_copy2_data
,
1181 shadow_copy2_data
->labels
,
1183 shadow_copy2_data
->num_volumes
+1);
1184 if (tlabels
== NULL
) {
1185 DEBUG(0,("shadow_copy2: out of memory\n"));
1186 SMB_VFS_NEXT_CLOSEDIR(handle
, p
);
1187 talloc_free(tmp_ctx
);
1191 strlcpy(tlabels
[shadow_copy2_data
->num_volumes
], snapshot
,
1194 shadow_copy2_data
->num_volumes
++;
1195 shadow_copy2_data
->labels
= tlabels
;
1198 SMB_VFS_NEXT_CLOSEDIR(handle
,p
);
1200 shadow_copy2_sort_data(handle
, shadow_copy2_data
);
1202 talloc_free(tmp_ctx
);
1206 static NTSTATUS
shadow_copy2_fget_nt_acl(vfs_handle_struct
*handle
,
1207 struct files_struct
*fsp
,
1208 uint32 security_info
,
1209 TALLOC_CTX
*mem_ctx
,
1210 struct security_descriptor
**ppdesc
)
1217 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
1218 fsp
->fsp_name
->base_name
,
1219 ×tamp
, &stripped
)) {
1220 return map_nt_error_from_unix(errno
);
1222 if (timestamp
== 0) {
1223 return SMB_VFS_NEXT_FGET_NT_ACL(handle
, fsp
, security_info
,
1227 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1228 TALLOC_FREE(stripped
);
1230 return map_nt_error_from_unix(errno
);
1232 status
= SMB_VFS_NEXT_GET_NT_ACL(handle
, conv
, security_info
,
1238 static NTSTATUS
shadow_copy2_get_nt_acl(vfs_handle_struct
*handle
,
1240 uint32 security_info
,
1241 TALLOC_CTX
*mem_ctx
,
1242 struct security_descriptor
**ppdesc
)
1249 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1250 ×tamp
, &stripped
)) {
1251 return map_nt_error_from_unix(errno
);
1253 if (timestamp
== 0) {
1254 return SMB_VFS_NEXT_GET_NT_ACL(handle
, fname
, security_info
,
1257 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1258 TALLOC_FREE(stripped
);
1260 return map_nt_error_from_unix(errno
);
1262 status
= SMB_VFS_NEXT_GET_NT_ACL(handle
, conv
, security_info
,
1268 static int shadow_copy2_mkdir(vfs_handle_struct
*handle
,
1269 const char *fname
, mode_t mode
)
1273 int ret
, saved_errno
;
1276 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1277 ×tamp
, &stripped
)) {
1280 if (timestamp
== 0) {
1281 return SMB_VFS_NEXT_MKDIR(handle
, fname
, mode
);
1283 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1284 TALLOC_FREE(stripped
);
1288 ret
= SMB_VFS_NEXT_MKDIR(handle
, conv
, mode
);
1289 saved_errno
= errno
;
1291 errno
= saved_errno
;
1295 static int shadow_copy2_rmdir(vfs_handle_struct
*handle
, const char *fname
)
1299 int ret
, saved_errno
;
1302 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1303 ×tamp
, &stripped
)) {
1306 if (timestamp
== 0) {
1307 return SMB_VFS_NEXT_RMDIR(handle
, fname
);
1309 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1310 TALLOC_FREE(stripped
);
1314 ret
= SMB_VFS_NEXT_RMDIR(handle
, conv
);
1315 saved_errno
= errno
;
1317 errno
= saved_errno
;
1321 static int shadow_copy2_chflags(vfs_handle_struct
*handle
, const char *fname
,
1326 int ret
, saved_errno
;
1329 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1330 ×tamp
, &stripped
)) {
1333 if (timestamp
== 0) {
1334 return SMB_VFS_NEXT_CHFLAGS(handle
, fname
, flags
);
1336 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1337 TALLOC_FREE(stripped
);
1341 ret
= SMB_VFS_NEXT_CHFLAGS(handle
, conv
, flags
);
1342 saved_errno
= errno
;
1344 errno
= saved_errno
;
1348 static ssize_t
shadow_copy2_getxattr(vfs_handle_struct
*handle
,
1349 const char *fname
, const char *aname
,
1350 void *value
, size_t size
)
1358 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1359 ×tamp
, &stripped
)) {
1362 if (timestamp
== 0) {
1363 return SMB_VFS_NEXT_GETXATTR(handle
, fname
, aname
, value
,
1366 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1367 TALLOC_FREE(stripped
);
1371 ret
= SMB_VFS_NEXT_GETXATTR(handle
, conv
, aname
, value
, size
);
1372 saved_errno
= errno
;
1374 errno
= saved_errno
;
1378 static ssize_t
shadow_copy2_listxattr(struct vfs_handle_struct
*handle
,
1380 char *list
, size_t size
)
1388 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1389 ×tamp
, &stripped
)) {
1392 if (timestamp
== 0) {
1393 return SMB_VFS_NEXT_LISTXATTR(handle
, fname
, list
, size
);
1395 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1396 TALLOC_FREE(stripped
);
1400 ret
= SMB_VFS_NEXT_LISTXATTR(handle
, conv
, list
, size
);
1401 saved_errno
= errno
;
1403 errno
= saved_errno
;
1407 static int shadow_copy2_removexattr(vfs_handle_struct
*handle
,
1408 const char *fname
, const char *aname
)
1412 int ret
, saved_errno
;
1415 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1416 ×tamp
, &stripped
)) {
1419 if (timestamp
== 0) {
1420 return SMB_VFS_NEXT_REMOVEXATTR(handle
, fname
, aname
);
1422 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1423 TALLOC_FREE(stripped
);
1427 ret
= SMB_VFS_NEXT_REMOVEXATTR(handle
, conv
, aname
);
1428 saved_errno
= errno
;
1430 errno
= saved_errno
;
1434 static int shadow_copy2_setxattr(struct vfs_handle_struct
*handle
,
1436 const char *aname
, const void *value
,
1437 size_t size
, int flags
)
1445 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1446 ×tamp
, &stripped
)) {
1449 if (timestamp
== 0) {
1450 return SMB_VFS_NEXT_SETXATTR(handle
, fname
, aname
, value
, size
,
1453 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1454 TALLOC_FREE(stripped
);
1458 ret
= SMB_VFS_NEXT_SETXATTR(handle
, conv
, aname
, value
, size
, flags
);
1459 saved_errno
= errno
;
1461 errno
= saved_errno
;
1465 static int shadow_copy2_chmod_acl(vfs_handle_struct
*handle
,
1466 const char *fname
, mode_t mode
)
1474 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1475 ×tamp
, &stripped
)) {
1478 if (timestamp
== 0) {
1479 return SMB_VFS_NEXT_CHMOD_ACL(handle
, fname
, mode
);
1481 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1482 TALLOC_FREE(stripped
);
1486 ret
= SMB_VFS_NEXT_CHMOD_ACL(handle
, conv
, mode
);
1487 saved_errno
= errno
;
1489 errno
= saved_errno
;
1493 static int shadow_copy2_get_real_filename(struct vfs_handle_struct
*handle
,
1496 TALLOC_CTX
*mem_ctx
,
1505 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, path
,
1506 ×tamp
, &stripped
)) {
1509 if (timestamp
== 0) {
1510 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle
, path
, name
,
1511 mem_ctx
, found_name
);
1513 if (stripped
[0] == '\0') {
1514 *found_name
= talloc_strdup(mem_ctx
, name
);
1515 if (*found_name
== NULL
) {
1521 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1522 TALLOC_FREE(stripped
);
1526 ret
= SMB_VFS_NEXT_GET_REAL_FILENAME(handle
, conv
, name
,
1527 mem_ctx
, found_name
);
1528 saved_errno
= errno
;
1530 errno
= saved_errno
;
1535 static struct vfs_fn_pointers vfs_shadow_copy2_fns
= {
1536 .opendir_fn
= shadow_copy2_opendir
,
1537 .rename_fn
= shadow_copy2_rename
,
1538 .link_fn
= shadow_copy2_link
,
1539 .symlink_fn
= shadow_copy2_symlink
,
1540 .stat_fn
= shadow_copy2_stat
,
1541 .lstat_fn
= shadow_copy2_lstat
,
1542 .fstat_fn
= shadow_copy2_fstat
,
1543 .open_fn
= shadow_copy2_open
,
1544 .unlink_fn
= shadow_copy2_unlink
,
1545 .chmod_fn
= shadow_copy2_chmod
,
1546 .chown_fn
= shadow_copy2_chown
,
1547 .chdir_fn
= shadow_copy2_chdir
,
1548 .ntimes_fn
= shadow_copy2_ntimes
,
1549 .readlink_fn
= shadow_copy2_readlink
,
1550 .mknod_fn
= shadow_copy2_mknod
,
1551 .realpath_fn
= shadow_copy2_realpath
,
1552 .get_nt_acl_fn
= shadow_copy2_get_nt_acl
,
1553 .fget_nt_acl_fn
= shadow_copy2_fget_nt_acl
,
1554 .get_shadow_copy_data_fn
= shadow_copy2_get_shadow_copy_data
,
1555 .mkdir_fn
= shadow_copy2_mkdir
,
1556 .rmdir_fn
= shadow_copy2_rmdir
,
1557 .getxattr_fn
= shadow_copy2_getxattr
,
1558 .listxattr_fn
= shadow_copy2_listxattr
,
1559 .removexattr_fn
= shadow_copy2_removexattr
,
1560 .setxattr_fn
= shadow_copy2_setxattr
,
1561 .chmod_acl_fn
= shadow_copy2_chmod_acl
,
1562 .chflags_fn
= shadow_copy2_chflags
,
1563 .get_real_filename_fn
= shadow_copy2_get_real_filename
,
1566 NTSTATUS
vfs_shadow_copy2_init(void);
1567 NTSTATUS
vfs_shadow_copy2_init(void)
1569 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
1570 "shadow_copy2", &vfs_shadow_copy2_fns
);