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
,
703 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
704 ×tamp
, &stripped
)) {
707 if (timestamp
== 0) {
708 return SMB_VFS_NEXT_OPENDIR(handle
, fname
, mask
, attr
);
710 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
711 TALLOC_FREE(stripped
);
715 ret
= SMB_VFS_NEXT_OPENDIR(handle
, conv
, mask
, attr
);
722 static int shadow_copy2_rename(vfs_handle_struct
*handle
,
723 const struct smb_filename
*smb_fname_src
,
724 const struct smb_filename
*smb_fname_dst
)
726 time_t timestamp_src
, timestamp_dst
;
728 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
729 smb_fname_src
->base_name
,
730 ×tamp_src
, NULL
)) {
733 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
734 smb_fname_dst
->base_name
,
735 ×tamp_dst
, NULL
)) {
738 if (timestamp_src
!= 0) {
742 if (timestamp_dst
!= 0) {
746 return SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, smb_fname_dst
);
749 static int shadow_copy2_symlink(vfs_handle_struct
*handle
,
750 const char *oldname
, const char *newname
)
752 time_t timestamp_old
, timestamp_new
;
754 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, oldname
,
755 ×tamp_old
, NULL
)) {
758 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, newname
,
759 ×tamp_new
, NULL
)) {
762 if ((timestamp_old
!= 0) || (timestamp_new
!= 0)) {
766 return SMB_VFS_NEXT_SYMLINK(handle
, oldname
, newname
);
769 static int shadow_copy2_link(vfs_handle_struct
*handle
,
770 const char *oldname
, const char *newname
)
772 time_t timestamp_old
, timestamp_new
;
774 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, oldname
,
775 ×tamp_old
, NULL
)) {
778 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, newname
,
779 ×tamp_new
, NULL
)) {
782 if ((timestamp_old
!= 0) || (timestamp_new
!= 0)) {
786 return SMB_VFS_NEXT_LINK(handle
, oldname
, newname
);
789 static int shadow_copy2_stat(vfs_handle_struct
*handle
,
790 struct smb_filename
*smb_fname
)
793 char *stripped
, *tmp
;
794 int ret
, saved_errno
;
796 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
797 smb_fname
->base_name
,
798 ×tamp
, &stripped
)) {
801 if (timestamp
== 0) {
802 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
805 tmp
= smb_fname
->base_name
;
806 smb_fname
->base_name
= shadow_copy2_convert(
807 talloc_tos(), handle
, stripped
, timestamp
);
808 TALLOC_FREE(stripped
);
810 if (smb_fname
->base_name
== NULL
) {
811 smb_fname
->base_name
= tmp
;
815 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
818 TALLOC_FREE(smb_fname
->base_name
);
819 smb_fname
->base_name
= tmp
;
822 convert_sbuf(handle
, smb_fname
->base_name
, &smb_fname
->st
);
828 static int shadow_copy2_lstat(vfs_handle_struct
*handle
,
829 struct smb_filename
*smb_fname
)
832 char *stripped
, *tmp
;
833 int ret
, saved_errno
;
835 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
836 smb_fname
->base_name
,
837 ×tamp
, &stripped
)) {
840 if (timestamp
== 0) {
841 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
844 tmp
= smb_fname
->base_name
;
845 smb_fname
->base_name
= shadow_copy2_convert(
846 talloc_tos(), handle
, stripped
, timestamp
);
847 TALLOC_FREE(stripped
);
849 if (smb_fname
->base_name
== NULL
) {
850 smb_fname
->base_name
= tmp
;
854 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
857 TALLOC_FREE(smb_fname
->base_name
);
858 smb_fname
->base_name
= tmp
;
861 convert_sbuf(handle
, smb_fname
->base_name
, &smb_fname
->st
);
867 static int shadow_copy2_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
868 SMB_STRUCT_STAT
*sbuf
)
873 ret
= SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
877 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
878 fsp
->fsp_name
->base_name
,
882 if (timestamp
!= 0) {
883 convert_sbuf(handle
, fsp
->fsp_name
->base_name
, sbuf
);
888 static int shadow_copy2_open(vfs_handle_struct
*handle
,
889 struct smb_filename
*smb_fname
, files_struct
*fsp
,
890 int flags
, mode_t mode
)
893 char *stripped
, *tmp
;
894 int ret
, saved_errno
;
896 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
897 smb_fname
->base_name
,
898 ×tamp
, &stripped
)) {
901 if (timestamp
== 0) {
902 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
905 tmp
= smb_fname
->base_name
;
906 smb_fname
->base_name
= shadow_copy2_convert(
907 talloc_tos(), handle
, stripped
, timestamp
);
908 TALLOC_FREE(stripped
);
910 if (smb_fname
->base_name
== NULL
) {
911 smb_fname
->base_name
= tmp
;
915 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
918 TALLOC_FREE(smb_fname
->base_name
);
919 smb_fname
->base_name
= tmp
;
925 static int shadow_copy2_unlink(vfs_handle_struct
*handle
,
926 const struct smb_filename
*smb_fname
)
930 int ret
, saved_errno
;
931 struct smb_filename
*conv
;
933 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
934 smb_fname
->base_name
,
935 ×tamp
, &stripped
)) {
938 if (timestamp
== 0) {
939 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
941 conv
= cp_smb_filename(talloc_tos(), smb_fname
);
946 conv
->base_name
= shadow_copy2_convert(
947 conv
, handle
, stripped
, timestamp
);
948 TALLOC_FREE(stripped
);
949 if (conv
->base_name
== NULL
) {
952 ret
= SMB_VFS_NEXT_UNLINK(handle
, conv
);
959 static int shadow_copy2_chmod(vfs_handle_struct
*handle
, const char *fname
,
964 int ret
, saved_errno
;
967 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
968 ×tamp
, &stripped
)) {
971 if (timestamp
== 0) {
972 return SMB_VFS_NEXT_CHMOD(handle
, fname
, mode
);
974 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
975 TALLOC_FREE(stripped
);
979 ret
= SMB_VFS_NEXT_CHMOD(handle
, conv
, mode
);
986 static int shadow_copy2_chown(vfs_handle_struct
*handle
, const char *fname
,
987 uid_t uid
, gid_t gid
)
991 int ret
, saved_errno
;
994 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
995 ×tamp
, &stripped
)) {
998 if (timestamp
== 0) {
999 return SMB_VFS_NEXT_CHOWN(handle
, fname
, uid
, gid
);
1001 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1002 TALLOC_FREE(stripped
);
1006 ret
= SMB_VFS_NEXT_CHOWN(handle
, conv
, uid
, gid
);
1007 saved_errno
= errno
;
1009 errno
= saved_errno
;
1013 static int shadow_copy2_chdir(vfs_handle_struct
*handle
,
1018 int ret
, saved_errno
;
1021 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1022 ×tamp
, &stripped
)) {
1025 if (timestamp
== 0) {
1026 return SMB_VFS_NEXT_CHDIR(handle
, fname
);
1028 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1029 TALLOC_FREE(stripped
);
1033 ret
= SMB_VFS_NEXT_CHDIR(handle
, conv
);
1034 saved_errno
= errno
;
1036 errno
= saved_errno
;
1040 static int shadow_copy2_ntimes(vfs_handle_struct
*handle
,
1041 const struct smb_filename
*smb_fname
,
1042 struct smb_file_time
*ft
)
1046 int ret
, saved_errno
;
1047 struct smb_filename
*conv
;
1049 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
1050 smb_fname
->base_name
,
1051 ×tamp
, &stripped
)) {
1054 if (timestamp
== 0) {
1055 return SMB_VFS_NEXT_NTIMES(handle
, smb_fname
, ft
);
1057 conv
= cp_smb_filename(talloc_tos(), smb_fname
);
1062 conv
->base_name
= shadow_copy2_convert(
1063 conv
, handle
, stripped
, timestamp
);
1064 TALLOC_FREE(stripped
);
1065 if (conv
->base_name
== NULL
) {
1068 ret
= SMB_VFS_NEXT_NTIMES(handle
, conv
, ft
);
1069 saved_errno
= errno
;
1071 errno
= saved_errno
;
1075 static int shadow_copy2_readlink(vfs_handle_struct
*handle
,
1076 const char *fname
, char *buf
, size_t bufsiz
)
1080 int ret
, saved_errno
;
1083 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1084 ×tamp
, &stripped
)) {
1087 if (timestamp
== 0) {
1088 return SMB_VFS_NEXT_READLINK(handle
, fname
, buf
, bufsiz
);
1090 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1091 TALLOC_FREE(stripped
);
1095 ret
= SMB_VFS_NEXT_READLINK(handle
, conv
, buf
, bufsiz
);
1096 saved_errno
= errno
;
1098 errno
= saved_errno
;
1102 static int shadow_copy2_mknod(vfs_handle_struct
*handle
,
1103 const char *fname
, mode_t mode
, SMB_DEV_T dev
)
1107 int ret
, saved_errno
;
1110 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1111 ×tamp
, &stripped
)) {
1114 if (timestamp
== 0) {
1115 return SMB_VFS_NEXT_MKNOD(handle
, fname
, mode
, dev
);
1117 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1118 TALLOC_FREE(stripped
);
1122 ret
= SMB_VFS_NEXT_MKNOD(handle
, conv
, mode
, dev
);
1123 saved_errno
= errno
;
1125 errno
= saved_errno
;
1129 static char *shadow_copy2_realpath(vfs_handle_struct
*handle
,
1133 char *stripped
= NULL
;
1135 char *result
= NULL
;
1138 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1139 ×tamp
, &stripped
)) {
1142 if (timestamp
== 0) {
1143 return SMB_VFS_NEXT_REALPATH(handle
, fname
);
1146 tmp
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1151 result
= SMB_VFS_NEXT_REALPATH(handle
, tmp
);
1154 saved_errno
= errno
;
1156 TALLOC_FREE(stripped
);
1157 errno
= saved_errno
;
1162 * Check whether a given directory contains a
1163 * snapshot directory as direct subdirectory.
1164 * If yes, return the path of the snapshot-subdir,
1165 * otherwise return NULL.
1167 static char *have_snapdir(struct vfs_handle_struct
*handle
,
1170 struct smb_filename smb_fname
;
1172 struct shadow_copy2_config
*config
;
1174 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct shadow_copy2_config
,
1177 ZERO_STRUCT(smb_fname
);
1178 smb_fname
.base_name
= talloc_asprintf(talloc_tos(), "%s/%s",
1179 path
, config
->snapdir
);
1180 if (smb_fname
.base_name
== NULL
) {
1184 ret
= SMB_VFS_NEXT_STAT(handle
, &smb_fname
);
1185 if ((ret
== 0) && (S_ISDIR(smb_fname
.st
.st_ex_mode
))) {
1186 return smb_fname
.base_name
;
1188 TALLOC_FREE(smb_fname
.base_name
);
1192 static bool check_access_snapdir(struct vfs_handle_struct
*handle
,
1195 struct smb_filename smb_fname
;
1199 ZERO_STRUCT(smb_fname
);
1200 smb_fname
.base_name
= talloc_asprintf(talloc_tos(),
1203 if (smb_fname
.base_name
== NULL
) {
1207 ret
= SMB_VFS_NEXT_STAT(handle
, &smb_fname
);
1208 if (ret
!= 0 || !S_ISDIR(smb_fname
.st
.st_ex_mode
)) {
1209 TALLOC_FREE(smb_fname
.base_name
);
1213 status
= smbd_check_access_rights(handle
->conn
,
1217 if (!NT_STATUS_IS_OK(status
)) {
1218 DEBUG(0,("user does not have list permission "
1220 smb_fname
.base_name
));
1221 TALLOC_FREE(smb_fname
.base_name
);
1224 TALLOC_FREE(smb_fname
.base_name
);
1229 * Find the snapshot directory (if any) for the given
1230 * filename (which is relative to the share).
1232 static const char *shadow_copy2_find_snapdir(TALLOC_CTX
*mem_ctx
,
1233 struct vfs_handle_struct
*handle
,
1234 struct smb_filename
*smb_fname
)
1237 const char *snapdir
;
1238 struct shadow_copy2_config
*config
;
1240 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct shadow_copy2_config
,
1244 * If the non-snapdisrseverywhere mode, we should not search!
1246 if (!config
->snapdirseverywhere
) {
1247 return config
->snapshot_basepath
;
1250 path
= talloc_asprintf(mem_ctx
, "%s/%s",
1251 handle
->conn
->connectpath
,
1252 smb_fname
->base_name
);
1257 snapdir
= have_snapdir(handle
, path
);
1258 if (snapdir
!= NULL
) {
1263 while ((p
= strrchr(path
, '/')) && (p
> path
)) {
1267 snapdir
= have_snapdir(handle
, path
);
1268 if (snapdir
!= NULL
) {
1277 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct
*handle
,
1279 char *gmt
, size_t gmt_len
)
1281 struct tm timestamp
;
1283 unsigned long int timestamp_long
;
1285 struct shadow_copy2_config
*config
;
1287 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct shadow_copy2_config
,
1290 fmt
= config
->gmt_format
;
1292 ZERO_STRUCT(timestamp
);
1293 if (config
->use_sscanf
) {
1294 if (sscanf(name
, fmt
, ×tamp_long
) != 1) {
1295 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1296 "no sscanf match %s: %s\n",
1300 timestamp_t
= timestamp_long
;
1301 gmtime_r(×tamp_t
, ×tamp
);
1303 if (strptime(name
, fmt
, ×tamp
) == NULL
) {
1304 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1305 "no match %s: %s\n",
1309 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1312 if (config
->use_localtime
) {
1313 timestamp
.tm_isdst
= -1;
1314 timestamp_t
= mktime(×tamp
);
1315 gmtime_r(×tamp_t
, ×tamp
);
1319 strftime(gmt
, gmt_len
, GMT_FORMAT
, ×tamp
);
1323 static int shadow_copy2_label_cmp_asc(const void *x
, const void *y
)
1325 return strncmp((const char *)x
, (const char *)y
, sizeof(SHADOW_COPY_LABEL
));
1328 static int shadow_copy2_label_cmp_desc(const void *x
, const void *y
)
1330 return -strncmp((const char *)x
, (const char *)y
, sizeof(SHADOW_COPY_LABEL
));
1334 sort the shadow copy data in ascending or descending order
1336 static void shadow_copy2_sort_data(vfs_handle_struct
*handle
,
1337 struct shadow_copy_data
*shadow_copy2_data
)
1339 int (*cmpfunc
)(const void *, const void *);
1341 struct shadow_copy2_config
*config
;
1343 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct shadow_copy2_config
,
1346 sort
= config
->sort_order
;
1351 if (strcmp(sort
, "asc") == 0) {
1352 cmpfunc
= shadow_copy2_label_cmp_asc
;
1353 } else if (strcmp(sort
, "desc") == 0) {
1354 cmpfunc
= shadow_copy2_label_cmp_desc
;
1359 if (shadow_copy2_data
&& shadow_copy2_data
->num_volumes
> 0 &&
1360 shadow_copy2_data
->labels
)
1362 TYPESAFE_QSORT(shadow_copy2_data
->labels
,
1363 shadow_copy2_data
->num_volumes
,
1368 static int shadow_copy2_get_shadow_copy_data(
1369 vfs_handle_struct
*handle
, files_struct
*fsp
,
1370 struct shadow_copy_data
*shadow_copy2_data
,
1374 const char *snapdir
;
1376 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1379 snapdir
= shadow_copy2_find_snapdir(tmp_ctx
, handle
, fsp
->fsp_name
);
1380 if (snapdir
== NULL
) {
1381 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1382 handle
->conn
->connectpath
));
1384 talloc_free(tmp_ctx
);
1387 ret
= check_access_snapdir(handle
, snapdir
);
1389 DEBUG(0,("access denied on listing snapdir %s\n", snapdir
));
1391 talloc_free(tmp_ctx
);
1395 p
= SMB_VFS_NEXT_OPENDIR(handle
, snapdir
, NULL
, 0);
1398 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1399 " - %s\n", snapdir
, strerror(errno
)));
1400 talloc_free(tmp_ctx
);
1405 shadow_copy2_data
->num_volumes
= 0;
1406 shadow_copy2_data
->labels
= NULL
;
1408 while ((d
= SMB_VFS_NEXT_READDIR(handle
, p
, NULL
))) {
1409 char snapshot
[GMT_NAME_LEN
+1];
1410 SHADOW_COPY_LABEL
*tlabels
;
1413 * ignore names not of the right form in the snapshot
1416 if (!shadow_copy2_snapshot_to_gmt(
1418 snapshot
, sizeof(snapshot
))) {
1420 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1421 "ignoring %s\n", d
->d_name
));
1424 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1425 d
->d_name
, snapshot
));
1428 /* the caller doesn't want the labels */
1429 shadow_copy2_data
->num_volumes
++;
1433 tlabels
= talloc_realloc(shadow_copy2_data
,
1434 shadow_copy2_data
->labels
,
1436 shadow_copy2_data
->num_volumes
+1);
1437 if (tlabels
== NULL
) {
1438 DEBUG(0,("shadow_copy2: out of memory\n"));
1439 SMB_VFS_NEXT_CLOSEDIR(handle
, p
);
1440 talloc_free(tmp_ctx
);
1444 strlcpy(tlabels
[shadow_copy2_data
->num_volumes
], snapshot
,
1447 shadow_copy2_data
->num_volumes
++;
1448 shadow_copy2_data
->labels
= tlabels
;
1451 SMB_VFS_NEXT_CLOSEDIR(handle
,p
);
1453 shadow_copy2_sort_data(handle
, shadow_copy2_data
);
1455 talloc_free(tmp_ctx
);
1459 static NTSTATUS
shadow_copy2_fget_nt_acl(vfs_handle_struct
*handle
,
1460 struct files_struct
*fsp
,
1461 uint32_t security_info
,
1462 TALLOC_CTX
*mem_ctx
,
1463 struct security_descriptor
**ppdesc
)
1470 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
,
1471 fsp
->fsp_name
->base_name
,
1472 ×tamp
, &stripped
)) {
1473 return map_nt_error_from_unix(errno
);
1475 if (timestamp
== 0) {
1476 return SMB_VFS_NEXT_FGET_NT_ACL(handle
, fsp
, security_info
,
1480 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1481 TALLOC_FREE(stripped
);
1483 return map_nt_error_from_unix(errno
);
1485 status
= SMB_VFS_NEXT_GET_NT_ACL(handle
, conv
, security_info
,
1491 static NTSTATUS
shadow_copy2_get_nt_acl(vfs_handle_struct
*handle
,
1493 uint32_t security_info
,
1494 TALLOC_CTX
*mem_ctx
,
1495 struct security_descriptor
**ppdesc
)
1502 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1503 ×tamp
, &stripped
)) {
1504 return map_nt_error_from_unix(errno
);
1506 if (timestamp
== 0) {
1507 return SMB_VFS_NEXT_GET_NT_ACL(handle
, fname
, security_info
,
1510 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1511 TALLOC_FREE(stripped
);
1513 return map_nt_error_from_unix(errno
);
1515 status
= SMB_VFS_NEXT_GET_NT_ACL(handle
, conv
, security_info
,
1521 static int shadow_copy2_mkdir(vfs_handle_struct
*handle
,
1522 const char *fname
, mode_t mode
)
1526 int ret
, saved_errno
;
1529 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1530 ×tamp
, &stripped
)) {
1533 if (timestamp
== 0) {
1534 return SMB_VFS_NEXT_MKDIR(handle
, fname
, mode
);
1536 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1537 TALLOC_FREE(stripped
);
1541 ret
= SMB_VFS_NEXT_MKDIR(handle
, conv
, mode
);
1542 saved_errno
= errno
;
1544 errno
= saved_errno
;
1548 static int shadow_copy2_rmdir(vfs_handle_struct
*handle
, const char *fname
)
1552 int ret
, saved_errno
;
1555 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1556 ×tamp
, &stripped
)) {
1559 if (timestamp
== 0) {
1560 return SMB_VFS_NEXT_RMDIR(handle
, fname
);
1562 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1563 TALLOC_FREE(stripped
);
1567 ret
= SMB_VFS_NEXT_RMDIR(handle
, conv
);
1568 saved_errno
= errno
;
1570 errno
= saved_errno
;
1574 static int shadow_copy2_chflags(vfs_handle_struct
*handle
, const char *fname
,
1579 int ret
, saved_errno
;
1582 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1583 ×tamp
, &stripped
)) {
1586 if (timestamp
== 0) {
1587 return SMB_VFS_NEXT_CHFLAGS(handle
, fname
, flags
);
1589 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1590 TALLOC_FREE(stripped
);
1594 ret
= SMB_VFS_NEXT_CHFLAGS(handle
, conv
, flags
);
1595 saved_errno
= errno
;
1597 errno
= saved_errno
;
1601 static ssize_t
shadow_copy2_getxattr(vfs_handle_struct
*handle
,
1602 const char *fname
, const char *aname
,
1603 void *value
, size_t size
)
1611 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1612 ×tamp
, &stripped
)) {
1615 if (timestamp
== 0) {
1616 return SMB_VFS_NEXT_GETXATTR(handle
, fname
, aname
, value
,
1619 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1620 TALLOC_FREE(stripped
);
1624 ret
= SMB_VFS_NEXT_GETXATTR(handle
, conv
, aname
, value
, size
);
1625 saved_errno
= errno
;
1627 errno
= saved_errno
;
1631 static ssize_t
shadow_copy2_listxattr(struct vfs_handle_struct
*handle
,
1633 char *list
, size_t size
)
1641 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1642 ×tamp
, &stripped
)) {
1645 if (timestamp
== 0) {
1646 return SMB_VFS_NEXT_LISTXATTR(handle
, fname
, list
, size
);
1648 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1649 TALLOC_FREE(stripped
);
1653 ret
= SMB_VFS_NEXT_LISTXATTR(handle
, conv
, list
, size
);
1654 saved_errno
= errno
;
1656 errno
= saved_errno
;
1660 static int shadow_copy2_removexattr(vfs_handle_struct
*handle
,
1661 const char *fname
, const char *aname
)
1665 int ret
, saved_errno
;
1668 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1669 ×tamp
, &stripped
)) {
1672 if (timestamp
== 0) {
1673 return SMB_VFS_NEXT_REMOVEXATTR(handle
, fname
, aname
);
1675 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1676 TALLOC_FREE(stripped
);
1680 ret
= SMB_VFS_NEXT_REMOVEXATTR(handle
, conv
, aname
);
1681 saved_errno
= errno
;
1683 errno
= saved_errno
;
1687 static int shadow_copy2_setxattr(struct vfs_handle_struct
*handle
,
1689 const char *aname
, const void *value
,
1690 size_t size
, int flags
)
1698 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1699 ×tamp
, &stripped
)) {
1702 if (timestamp
== 0) {
1703 return SMB_VFS_NEXT_SETXATTR(handle
, fname
, aname
, value
, size
,
1706 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1707 TALLOC_FREE(stripped
);
1711 ret
= SMB_VFS_NEXT_SETXATTR(handle
, conv
, aname
, value
, size
, flags
);
1712 saved_errno
= errno
;
1714 errno
= saved_errno
;
1718 static int shadow_copy2_chmod_acl(vfs_handle_struct
*handle
,
1719 const char *fname
, mode_t mode
)
1727 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1728 ×tamp
, &stripped
)) {
1731 if (timestamp
== 0) {
1732 return SMB_VFS_NEXT_CHMOD_ACL(handle
, fname
, mode
);
1734 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1735 TALLOC_FREE(stripped
);
1739 ret
= SMB_VFS_NEXT_CHMOD_ACL(handle
, conv
, mode
);
1740 saved_errno
= errno
;
1742 errno
= saved_errno
;
1746 static int shadow_copy2_get_real_filename(struct vfs_handle_struct
*handle
,
1749 TALLOC_CTX
*mem_ctx
,
1758 DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
1759 "name=[%s]\n", path
, name
));
1761 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, path
,
1762 ×tamp
, &stripped
)) {
1763 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
1766 if (timestamp
== 0) {
1767 DEBUG(10, ("timestamp == 0\n"));
1768 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle
, path
, name
,
1769 mem_ctx
, found_name
);
1771 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1772 TALLOC_FREE(stripped
);
1774 DEBUG(10, ("shadow_copy2_convert failed\n"));
1777 DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
1778 "name=[%s]\n", conv
, name
));
1779 ret
= SMB_VFS_NEXT_GET_REAL_FILENAME(handle
, conv
, name
,
1780 mem_ctx
, found_name
);
1781 DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret
));
1782 saved_errno
= errno
;
1784 errno
= saved_errno
;
1788 static const char *shadow_copy2_connectpath(struct vfs_handle_struct
*handle
,
1792 char *stripped
= NULL
;
1794 char *result
= NULL
;
1796 size_t rootpath_len
= 0;
1798 DBG_DEBUG("Calc connect path for [%s]\n", fname
);
1800 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, fname
,
1801 ×tamp
, &stripped
)) {
1804 if (timestamp
== 0) {
1805 return SMB_VFS_NEXT_CONNECTPATH(handle
, fname
);
1808 tmp
= shadow_copy2_do_convert(talloc_tos(), handle
, stripped
, timestamp
,
1814 DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp
,
1815 (int)rootpath_len
, tmp
);
1817 tmp
[rootpath_len
] = '\0';
1818 result
= SMB_VFS_NEXT_REALPATH(handle
, tmp
);
1819 if (result
== NULL
) {
1823 DBG_DEBUG("connect path is [%s]\n", result
);
1826 saved_errno
= errno
;
1828 TALLOC_FREE(stripped
);
1829 errno
= saved_errno
;
1833 static uint64_t shadow_copy2_disk_free(vfs_handle_struct
*handle
,
1834 const char *path
, uint64_t *bsize
,
1835 uint64_t *dfree
, uint64_t *dsize
)
1843 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, path
,
1844 ×tamp
, &stripped
)) {
1847 if (timestamp
== 0) {
1848 return SMB_VFS_NEXT_DISK_FREE(handle
, path
,
1849 bsize
, dfree
, dsize
);
1852 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1853 TALLOC_FREE(stripped
);
1858 ret
= SMB_VFS_NEXT_DISK_FREE(handle
, conv
, bsize
, dfree
, dsize
);
1860 saved_errno
= errno
;
1862 errno
= saved_errno
;
1867 static int shadow_copy2_get_quota(vfs_handle_struct
*handle
, const char *path
,
1868 enum SMB_QUOTA_TYPE qtype
, unid_t id
,
1877 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle
, path
, ×tamp
,
1881 if (timestamp
== 0) {
1882 return SMB_VFS_NEXT_GET_QUOTA(handle
, path
, qtype
, id
, dq
);
1885 conv
= shadow_copy2_convert(talloc_tos(), handle
, stripped
, timestamp
);
1886 TALLOC_FREE(stripped
);
1891 ret
= SMB_VFS_NEXT_GET_QUOTA(handle
, conv
, qtype
, id
, dq
);
1893 saved_errno
= errno
;
1895 errno
= saved_errno
;
1900 static int shadow_copy2_connect(struct vfs_handle_struct
*handle
,
1901 const char *service
, const char *user
)
1903 struct shadow_copy2_config
*config
;
1905 const char *snapdir
;
1906 const char *gmt_format
;
1907 const char *sort_order
;
1908 const char *basedir
= NULL
;
1909 const char *snapsharepath
= NULL
;
1910 const char *mount_point
;
1912 DEBUG(10, (__location__
": cnum[%u], connectpath[%s]\n",
1913 (unsigned)handle
->conn
->cnum
,
1914 handle
->conn
->connectpath
));
1916 ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
1921 config
= talloc_zero(handle
->conn
, struct shadow_copy2_config
);
1922 if (config
== NULL
) {
1923 DEBUG(0, ("talloc_zero() failed\n"));
1928 gmt_format
= lp_parm_const_string(SNUM(handle
->conn
),
1931 config
->gmt_format
= talloc_strdup(config
, gmt_format
);
1932 if (config
->gmt_format
== NULL
) {
1933 DEBUG(0, ("talloc_strdup() failed\n"));
1938 config
->use_sscanf
= lp_parm_bool(SNUM(handle
->conn
),
1939 "shadow", "sscanf", false);
1941 config
->use_localtime
= lp_parm_bool(SNUM(handle
->conn
),
1942 "shadow", "localtime",
1945 snapdir
= lp_parm_const_string(SNUM(handle
->conn
),
1946 "shadow", "snapdir",
1948 config
->snapdir
= talloc_strdup(config
, snapdir
);
1949 if (config
->snapdir
== NULL
) {
1950 DEBUG(0, ("talloc_strdup() failed\n"));
1955 config
->snapdirseverywhere
= lp_parm_bool(SNUM(handle
->conn
),
1957 "snapdirseverywhere",
1960 config
->crossmountpoints
= lp_parm_bool(SNUM(handle
->conn
),
1961 "shadow", "crossmountpoints",
1964 if (config
->crossmountpoints
&& !config
->snapdirseverywhere
) {
1965 DBG_WARNING("Warning: 'crossmountpoints' depends on "
1966 "'snapdirseverywhere'. Disabling crossmountpoints.\n");
1969 config
->fixinodes
= lp_parm_bool(SNUM(handle
->conn
),
1970 "shadow", "fixinodes",
1973 sort_order
= lp_parm_const_string(SNUM(handle
->conn
),
1974 "shadow", "sort", "desc");
1975 config
->sort_order
= talloc_strdup(config
, sort_order
);
1976 if (config
->sort_order
== NULL
) {
1977 DEBUG(0, ("talloc_strdup() failed\n"));
1982 mount_point
= lp_parm_const_string(SNUM(handle
->conn
),
1983 "shadow", "mountpoint", NULL
);
1984 if (mount_point
!= NULL
) {
1985 if (mount_point
[0] != '/') {
1986 DEBUG(1, (__location__
" Warning: 'mountpoint' is "
1987 "relative ('%s'), but it has to be an "
1988 "absolute path. Ignoring provided value.\n",
1993 p
= strstr(handle
->conn
->connectpath
, mount_point
);
1994 if (p
!= handle
->conn
->connectpath
) {
1995 DBG_WARNING("Warning: the share root (%s) is "
1996 "not a subdirectory of the "
1997 "specified mountpoint (%s). "
1998 "Ignoring provided value.\n",
1999 handle
->conn
->connectpath
,
2006 if (mount_point
!= NULL
) {
2007 config
->mount_point
= talloc_strdup(config
, mount_point
);
2008 if (config
->mount_point
== NULL
) {
2009 DEBUG(0, (__location__
" talloc_strdup() failed\n"));
2013 config
->mount_point
= shadow_copy2_find_mount_point(config
,
2015 if (config
->mount_point
== NULL
) {
2016 DBG_WARNING("shadow_copy2_find_mount_point "
2017 "of the share root '%s' failed: %s\n",
2018 handle
->conn
->connectpath
, strerror(errno
));
2023 basedir
= lp_parm_const_string(SNUM(handle
->conn
),
2024 "shadow", "basedir", NULL
);
2026 if (basedir
!= NULL
) {
2027 if (basedir
[0] != '/') {
2028 DEBUG(1, (__location__
" Warning: 'basedir' is "
2029 "relative ('%s'), but it has to be an "
2030 "absolute path. Disabling basedir.\n",
2035 p
= strstr(basedir
, config
->mount_point
);
2037 DEBUG(1, ("Warning: basedir (%s) is not a "
2038 "subdirectory of the share root's "
2039 "mount point (%s). "
2040 "Disabling basedir\n",
2041 basedir
, config
->mount_point
));
2047 if (config
->snapdirseverywhere
&& basedir
!= NULL
) {
2048 DEBUG(1, (__location__
" Warning: 'basedir' is incompatible "
2049 "with 'snapdirseverywhere'. Disabling basedir.\n"));
2053 snapsharepath
= lp_parm_const_string(SNUM(handle
->conn
), "shadow",
2054 "snapsharepath", NULL
);
2055 if (snapsharepath
!= NULL
) {
2056 if (snapsharepath
[0] == '/') {
2057 DBG_WARNING("Warning: 'snapsharepath' is "
2058 "absolute ('%s'), but it has to be a "
2059 "relative path. Disabling snapsharepath.\n",
2061 snapsharepath
= NULL
;
2063 if (config
->snapdirseverywhere
&& snapsharepath
!= NULL
) {
2064 DBG_WARNING("Warning: 'snapsharepath' is incompatible "
2065 "with 'snapdirseverywhere'. Disabling "
2066 "snapsharepath.\n");
2067 snapsharepath
= NULL
;
2071 if (basedir
!= NULL
&& snapsharepath
!= NULL
) {
2072 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
2073 "'basedir'. Disabling snapsharepath\n");
2074 snapsharepath
= NULL
;
2077 if (snapsharepath
!= NULL
) {
2078 config
->rel_connectpath
= talloc_strdup(config
, snapsharepath
);
2079 if (config
->rel_connectpath
== NULL
) {
2080 DBG_ERR("talloc_strdup() failed\n");
2086 if (basedir
== NULL
) {
2087 basedir
= config
->mount_point
;
2090 if (config
->rel_connectpath
== NULL
&&
2091 strlen(basedir
) != strlen(handle
->conn
->connectpath
)) {
2092 config
->rel_connectpath
= talloc_strdup(config
,
2093 handle
->conn
->connectpath
+ strlen(basedir
));
2094 if (config
->rel_connectpath
== NULL
) {
2095 DEBUG(0, ("talloc_strdup() failed\n"));
2101 if (config
->snapdir
[0] == '/') {
2102 config
->snapdir_absolute
= true;
2104 if (config
->snapdirseverywhere
== true) {
2105 DEBUG(1, (__location__
" Warning: An absolute snapdir "
2106 "is incompatible with 'snapdirseverywhere', "
2107 "setting 'snapdirseverywhere' to false.\n"));
2108 config
->snapdirseverywhere
= false;
2111 if (config
->crossmountpoints
== true) {
2112 DEBUG(1, (__location__
" Warning: 'crossmountpoints' "
2113 "is not supported with an absolute snapdir. "
2114 "Disabling it.\n"));
2115 config
->crossmountpoints
= false;
2118 config
->snapshot_basepath
= config
->snapdir
;
2120 config
->snapshot_basepath
= talloc_asprintf(config
, "%s/%s",
2121 config
->mount_point
, config
->snapdir
);
2122 if (config
->snapshot_basepath
== NULL
) {
2123 DEBUG(0, ("talloc_asprintf() failed\n"));
2129 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
2130 " share root: '%s'\n"
2131 " mountpoint: '%s'\n"
2132 " rel share root: '%s'\n"
2134 " snapshot base path: '%s'\n"
2137 " snapdirs everywhere: %s\n"
2138 " cross mountpoints: %s\n"
2142 handle
->conn
->connectpath
,
2143 config
->mount_point
,
2144 config
->rel_connectpath
,
2146 config
->snapshot_basepath
,
2148 config
->use_sscanf
? "yes" : "no",
2149 config
->snapdirseverywhere
? "yes" : "no",
2150 config
->crossmountpoints
? "yes" : "no",
2151 config
->fixinodes
? "yes" : "no",
2156 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
2157 NULL
, struct shadow_copy2_config
,
2163 static struct vfs_fn_pointers vfs_shadow_copy2_fns
= {
2164 .connect_fn
= shadow_copy2_connect
,
2165 .opendir_fn
= shadow_copy2_opendir
,
2166 .disk_free_fn
= shadow_copy2_disk_free
,
2167 .get_quota_fn
= shadow_copy2_get_quota
,
2168 .rename_fn
= shadow_copy2_rename
,
2169 .link_fn
= shadow_copy2_link
,
2170 .symlink_fn
= shadow_copy2_symlink
,
2171 .stat_fn
= shadow_copy2_stat
,
2172 .lstat_fn
= shadow_copy2_lstat
,
2173 .fstat_fn
= shadow_copy2_fstat
,
2174 .open_fn
= shadow_copy2_open
,
2175 .unlink_fn
= shadow_copy2_unlink
,
2176 .chmod_fn
= shadow_copy2_chmod
,
2177 .chown_fn
= shadow_copy2_chown
,
2178 .chdir_fn
= shadow_copy2_chdir
,
2179 .ntimes_fn
= shadow_copy2_ntimes
,
2180 .readlink_fn
= shadow_copy2_readlink
,
2181 .mknod_fn
= shadow_copy2_mknod
,
2182 .realpath_fn
= shadow_copy2_realpath
,
2183 .get_nt_acl_fn
= shadow_copy2_get_nt_acl
,
2184 .fget_nt_acl_fn
= shadow_copy2_fget_nt_acl
,
2185 .get_shadow_copy_data_fn
= shadow_copy2_get_shadow_copy_data
,
2186 .mkdir_fn
= shadow_copy2_mkdir
,
2187 .rmdir_fn
= shadow_copy2_rmdir
,
2188 .getxattr_fn
= shadow_copy2_getxattr
,
2189 .listxattr_fn
= shadow_copy2_listxattr
,
2190 .removexattr_fn
= shadow_copy2_removexattr
,
2191 .setxattr_fn
= shadow_copy2_setxattr
,
2192 .chmod_acl_fn
= shadow_copy2_chmod_acl
,
2193 .chflags_fn
= shadow_copy2_chflags
,
2194 .get_real_filename_fn
= shadow_copy2_get_real_filename
,
2195 .connectpath_fn
= shadow_copy2_connectpath
,
2198 NTSTATUS
vfs_shadow_copy2_init(void);
2199 NTSTATUS
vfs_shadow_copy2_init(void)
2201 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
2202 "shadow_copy2", &vfs_shadow_copy2_fns
);