s3:modules:shadow_copy2: remove redundant documentation comment block
[Samba.git] / source3 / modules / vfs_shadow_copy2.c
blobd11ec0da9aa806647ac4bbf6b66887fbd5f73c6b
1 /*
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.
32 #include "includes.h"
33 #include "system/filesys.h"
34 #include "include/ntioctl.h"
35 #include <ccan/hash/hash.h>
36 #include "util_tdb.h"
38 struct shadow_copy2_config {
39 char *gmt_format;
40 bool use_sscanf;
41 bool use_localtime;
42 char *snapdir;
43 bool snapdirseverywhere;
44 bool crossmountpoints;
45 bool fixinodes;
46 char *sort_order;
47 bool snapdir_absolute;
48 char *basedir;
49 char *mount_point;
50 char *rel_connectpath; /* share root, relative to the basedir */
51 char *snapshot_basepath; /* the absolute version of snapdir */
54 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
55 size_t **poffsets,
56 unsigned *pnum_offsets)
58 unsigned num_offsets;
59 size_t *offsets;
60 const char *p;
62 num_offsets = 0;
64 p = str;
65 while ((p = strchr(p, '/')) != NULL) {
66 num_offsets += 1;
67 p += 1;
70 offsets = talloc_array(mem_ctx, size_t, num_offsets);
71 if (offsets == NULL) {
72 return false;
75 p = str;
76 num_offsets = 0;
77 while ((p = strchr(p, '/')) != NULL) {
78 offsets[num_offsets] = p-str;
79 num_offsets += 1;
80 p += 1;
83 *poffsets = offsets;
84 *pnum_offsets = num_offsets;
85 return true;
88 /**
89 * Given a timstamp, build the posix level GTM-tag string
90 * based on the configurable format.
92 static size_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
93 time_t snapshot,
94 char *snaptime_string,
95 size_t len)
97 struct tm snap_tm;
98 size_t snaptime_len;
99 struct shadow_copy2_config *config;
101 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
102 return 0);
104 if (config->use_sscanf) {
105 snaptime_len = snprintf(snaptime_string,
106 len,
107 config->gmt_format,
108 (unsigned long)snapshot);
109 if (snaptime_len <= 0) {
110 DEBUG(10, ("snprintf failed\n"));
111 return snaptime_len;
113 } else {
114 if (config->use_localtime) {
115 if (localtime_r(&snapshot, &snap_tm) == 0) {
116 DEBUG(10, ("gmtime_r failed\n"));
117 return -1;
119 } else {
120 if (gmtime_r(&snapshot, &snap_tm) == 0) {
121 DEBUG(10, ("gmtime_r failed\n"));
122 return -1;
125 snaptime_len = strftime(snaptime_string,
126 len,
127 config->gmt_format,
128 &snap_tm);
129 if (snaptime_len == 0) {
130 DEBUG(10, ("strftime failed\n"));
131 return 0;
135 return snaptime_len;
139 * Given a timstamp, build the string to insert into a path
140 * as a path component for creating the local path to the
141 * snapshot at the given timestamp of the input path.
143 * In the case of a parallel snapdir (specified with an
144 * absolute path), this is the inital portion of the
145 * local path of any snapshot file. The complete path is
146 * obtained by appending the portion of the file's path
147 * below the share root's mountpoint.
149 static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
150 struct vfs_handle_struct *handle,
151 time_t snapshot)
153 fstring snaptime_string;
154 size_t snaptime_len = 0;
155 char *result = NULL;
156 struct shadow_copy2_config *config;
158 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
159 return NULL);
161 snaptime_len = shadow_copy2_posix_gmt_string(handle,
162 snapshot,
163 snaptime_string,
164 sizeof(snaptime_string));
165 if (snaptime_len <= 0) {
166 return NULL;
169 if (config->snapdir_absolute) {
170 result = talloc_asprintf(mem_ctx, "%s/%s",
171 config->snapdir, snaptime_string);
172 } else {
173 result = talloc_asprintf(mem_ctx, "/%s/%s",
174 config->snapdir, snaptime_string);
176 if (result == NULL) {
177 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
180 return result;
184 * Build the posix snapshot path for the connection
185 * at the given timestamp, i.e. the absolute posix path
186 * that contains the snapshot for this file system.
188 * This only applies to classical case, i.e. not
189 * to the "snapdirseverywhere" mode.
191 static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
192 struct vfs_handle_struct *handle,
193 time_t snapshot)
195 fstring snaptime_string;
196 size_t snaptime_len = 0;
197 char *result = NULL;
198 struct shadow_copy2_config *config;
200 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
201 return NULL);
203 snaptime_len = shadow_copy2_posix_gmt_string(handle,
204 snapshot,
205 snaptime_string,
206 sizeof(snaptime_string));
207 if (snaptime_len <= 0) {
208 return NULL;
211 result = talloc_asprintf(mem_ctx, "%s/%s",
212 config->snapshot_basepath, snaptime_string);
213 if (result == NULL) {
214 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
217 return result;
221 * Strip a snapshot component from an filename as
222 * handed in via the smb layer.
223 * Returns the parsed timestamp and the stripped filename.
225 static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
226 struct vfs_handle_struct *handle,
227 const char *name,
228 time_t *ptimestamp,
229 char **pstripped)
231 struct tm tm;
232 time_t timestamp;
233 const char *p;
234 char *q;
235 char *stripped;
236 size_t rest_len, dst_len;
237 struct shadow_copy2_config *config;
239 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
240 return false);
242 DEBUG(10, (__location__ ": enter path '%s'\n", name));
244 p = strstr_m(name, "@GMT-");
245 if (p == NULL) {
246 goto no_snapshot;
248 if ((p > name) && (p[-1] != '/')) {
249 /* the GMT-token does not start a path-component */
250 goto no_snapshot;
252 q = strptime(p, GMT_FORMAT, &tm);
253 if (q == NULL) {
254 goto no_snapshot;
256 tm.tm_isdst = -1;
257 timestamp = timegm(&tm);
258 if (timestamp == (time_t)-1) {
259 goto no_snapshot;
261 if ((p == name) && (q[0] == '\0')) {
262 /* the name consists of only the GMT token */
263 if (pstripped != NULL) {
264 stripped = talloc_strdup(mem_ctx, "");
265 if (stripped == NULL) {
266 return false;
268 *pstripped = stripped;
270 *ptimestamp = timestamp;
271 return true;
273 if (q[0] != '/') {
275 * The GMT token is either at the end of the path
276 * or it is not a complete path component, i.e. the
277 * path component continues after the gmt-token.
279 * TODO: Is this correct? Or would the GMT tag as the
280 * last component be a valid input?
282 goto no_snapshot;
284 q += 1;
286 rest_len = strlen(q);
287 dst_len = (p-name) + rest_len;
289 if (config->snapdirseverywhere) {
290 char *insert;
291 bool have_insert;
292 insert = shadow_copy2_insert_string(talloc_tos(), handle,
293 timestamp);
294 if (insert == NULL) {
295 errno = ENOMEM;
296 return false;
299 DEBUG(10, (__location__ ": snapdirseverywhere mode.\n"
300 "path '%s'.\n"
301 "insert string '%s'\n", name, insert));
303 have_insert = (strstr(name, insert+1) != NULL);
304 if (have_insert) {
305 DEBUG(10, (__location__ ": insert string '%s' found in "
306 "path '%s' found in snapdirseverywhere mode "
307 "==> already converted\n", insert, name));
308 TALLOC_FREE(insert);
309 goto no_snapshot;
311 TALLOC_FREE(insert);
312 } else {
313 char *snapshot_path;
314 char *s;
316 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
317 handle,
318 timestamp);
319 if (snapshot_path == NULL) {
320 errno = ENOMEM;
321 return false;
324 DEBUG(10, (__location__ " path: '%s'.\n"
325 "snapshot path: '%s'\n", name, snapshot_path));
327 s = strstr(name, snapshot_path);
328 if (s == name) {
330 * this starts with "snapshot_basepath/GMT-Token"
331 * so it is already a converted absolute
332 * path. Don't process further.
334 DEBUG(10, (__location__ ": path '%s' starts with "
335 "snapshot path '%s' (not in "
336 "snapdirseverywhere mode) ==> "
337 "already converted\n", name, snapshot_path));
338 talloc_free(snapshot_path);
339 goto no_snapshot;
341 talloc_free(snapshot_path);
344 if (pstripped != NULL) {
345 stripped = talloc_array(mem_ctx, char, dst_len+1);
346 if (stripped == NULL) {
347 errno = ENOMEM;
348 return false;
350 if (p > name) {
351 memcpy(stripped, name, p-name);
353 if (rest_len > 0) {
354 memcpy(stripped + (p-name), q, rest_len);
356 stripped[dst_len] = '\0';
357 *pstripped = stripped;
359 *ptimestamp = timestamp;
360 return true;
361 no_snapshot:
362 *ptimestamp = 0;
363 return true;
366 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
367 vfs_handle_struct *handle)
369 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
370 dev_t dev;
371 struct stat st;
372 char *p;
374 if (stat(path, &st) != 0) {
375 talloc_free(path);
376 return NULL;
379 dev = st.st_dev;
381 while ((p = strrchr(path, '/')) && p > path) {
382 *p = 0;
383 if (stat(path, &st) != 0) {
384 talloc_free(path);
385 return NULL;
387 if (st.st_dev != dev) {
388 *p = '/';
389 break;
393 return path;
397 * Convert from a name as handed in via the SMB layer
398 * and a timestamp into the local path of the snapshot
399 * of the provided file at the provided time.
401 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
402 struct vfs_handle_struct *handle,
403 const char *name, time_t timestamp)
405 struct smb_filename converted_fname;
406 char *result = NULL;
407 size_t *slashes = NULL;
408 unsigned num_slashes;
409 char *path = NULL;
410 size_t pathlen;
411 char *insert = NULL;
412 char *converted = NULL;
413 size_t insertlen;
414 int i, saved_errno;
415 size_t min_offset;
416 struct shadow_copy2_config *config;
418 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
419 return NULL);
421 DEBUG(10, ("converting '%s'\n", name));
423 if (!config->snapdirseverywhere) {
424 int ret;
425 char *snapshot_path;
427 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
428 handle,
429 timestamp);
430 if (snapshot_path == NULL) {
431 goto fail;
434 if (config->rel_connectpath == NULL) {
435 converted = talloc_asprintf(mem_ctx, "%s/%s",
436 snapshot_path, name);
437 } else {
438 converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
439 snapshot_path,
440 config->rel_connectpath,
441 name);
443 if (converted == NULL) {
444 goto fail;
447 ZERO_STRUCT(converted_fname);
448 converted_fname.base_name = converted;
450 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
451 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
452 converted,
453 ret, ret == 0 ? "ok" : strerror(errno)));
454 if (ret == 0) {
455 DEBUG(10, ("Found %s\n", converted));
456 result = converted;
457 converted = NULL;
458 goto fail;
459 } else {
460 errno = ENOENT;
461 goto fail;
463 /* never reached ... */
466 path = talloc_asprintf(mem_ctx, "%s/%s", handle->conn->connectpath,
467 name);
468 if (path == NULL) {
469 errno = ENOMEM;
470 goto fail;
472 pathlen = talloc_get_size(path)-1;
474 if (!shadow_copy2_find_slashes(talloc_tos(), path,
475 &slashes, &num_slashes)) {
476 goto fail;
479 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
480 if (insert == NULL) {
481 goto fail;
483 insertlen = talloc_get_size(insert)-1;
485 converted = talloc_zero_array(mem_ctx, char, pathlen + insertlen + 1);
486 if (converted == NULL) {
487 goto fail;
490 if (path[pathlen-1] != '/') {
492 * Append a fake slash to find the snapshot root
494 size_t *tmp;
495 tmp = talloc_realloc(talloc_tos(), slashes,
496 size_t, num_slashes+1);
497 if (tmp == NULL) {
498 goto fail;
500 slashes = tmp;
501 slashes[num_slashes] = pathlen;
502 num_slashes += 1;
505 min_offset = 0;
507 if (!config->crossmountpoints) {
508 min_offset = strlen(config->mount_point);
511 memcpy(converted, path, pathlen+1);
512 converted[pathlen+insertlen] = '\0';
514 ZERO_STRUCT(converted_fname);
515 converted_fname.base_name = converted;
517 for (i = num_slashes-1; i>=0; i--) {
518 int ret;
519 size_t offset;
521 offset = slashes[i];
523 if (offset < min_offset) {
524 errno = ENOENT;
525 goto fail;
528 memcpy(converted+offset, insert, insertlen);
530 offset += insertlen;
531 memcpy(converted+offset, path + slashes[i],
532 pathlen - slashes[i]);
534 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
536 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
537 converted,
538 ret, ret == 0 ? "ok" : strerror(errno)));
539 if (ret == 0) {
540 /* success */
541 break;
543 if (errno == ENOTDIR) {
545 * This is a valid condition: We appended the
546 * .snaphots/@GMT.. to a file name. Just try
547 * with the upper levels.
549 continue;
551 if (errno != ENOENT) {
552 /* Other problem than "not found" */
553 goto fail;
557 if (i >= 0) {
559 * Found something
561 DEBUG(10, ("Found %s\n", converted));
562 result = converted;
563 converted = NULL;
564 } else {
565 errno = ENOENT;
567 fail:
568 saved_errno = errno;
569 TALLOC_FREE(converted);
570 TALLOC_FREE(insert);
571 TALLOC_FREE(slashes);
572 TALLOC_FREE(path);
573 errno = saved_errno;
574 return result;
578 modify a sbuf return to ensure that inodes in the shadow directory
579 are different from those in the main directory
581 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
582 SMB_STRUCT_STAT *sbuf)
584 struct shadow_copy2_config *config;
586 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
587 return);
589 if (config->fixinodes) {
590 /* some snapshot systems, like GPFS, return the name
591 device:inode for the snapshot files as the current
592 files. That breaks the 'restore' button in the shadow copy
593 GUI, as the client gets a sharing violation.
595 This is a crude way of allowing both files to be
596 open at once. It has a slight chance of inode
597 number collision, but I can't see a better approach
598 without significant VFS changes
600 uint32_t shash;
602 shash = hash(fname, strlen(fname), 0) & 0xFF000000;
603 if (shash == 0) {
604 shash = 1;
606 sbuf->st_ex_ino ^= shash;
610 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
611 const char *fname,
612 const char *mask,
613 uint32 attr)
615 time_t timestamp;
616 char *stripped;
617 DIR *ret;
618 int saved_errno;
619 char *conv;
621 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
622 &timestamp, &stripped)) {
623 return NULL;
625 if (timestamp == 0) {
626 return SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
628 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
629 TALLOC_FREE(stripped);
630 if (conv == NULL) {
631 return NULL;
633 ret = SMB_VFS_NEXT_OPENDIR(handle, conv, mask, attr);
634 saved_errno = errno;
635 TALLOC_FREE(conv);
636 errno = saved_errno;
637 return ret;
640 static int shadow_copy2_rename(vfs_handle_struct *handle,
641 const struct smb_filename *smb_fname_src,
642 const struct smb_filename *smb_fname_dst)
644 time_t timestamp_src, timestamp_dst;
646 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
647 smb_fname_src->base_name,
648 &timestamp_src, NULL)) {
649 return -1;
651 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
652 smb_fname_dst->base_name,
653 &timestamp_dst, NULL)) {
654 return -1;
656 if (timestamp_src != 0) {
657 errno = EXDEV;
658 return -1;
660 if (timestamp_dst != 0) {
661 errno = EROFS;
662 return -1;
664 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
667 static int shadow_copy2_symlink(vfs_handle_struct *handle,
668 const char *oldname, const char *newname)
670 time_t timestamp_old, timestamp_new;
672 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
673 &timestamp_old, NULL)) {
674 return -1;
676 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
677 &timestamp_new, NULL)) {
678 return -1;
680 if ((timestamp_old != 0) || (timestamp_new != 0)) {
681 errno = EROFS;
682 return -1;
684 return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
687 static int shadow_copy2_link(vfs_handle_struct *handle,
688 const char *oldname, const char *newname)
690 time_t timestamp_old, timestamp_new;
692 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
693 &timestamp_old, NULL)) {
694 return -1;
696 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
697 &timestamp_new, NULL)) {
698 return -1;
700 if ((timestamp_old != 0) || (timestamp_new != 0)) {
701 errno = EROFS;
702 return -1;
704 return SMB_VFS_NEXT_LINK(handle, oldname, newname);
707 static int shadow_copy2_stat(vfs_handle_struct *handle,
708 struct smb_filename *smb_fname)
710 time_t timestamp;
711 char *stripped, *tmp;
712 int ret, saved_errno;
714 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
715 smb_fname->base_name,
716 &timestamp, &stripped)) {
717 return -1;
719 if (timestamp == 0) {
720 return SMB_VFS_NEXT_STAT(handle, smb_fname);
723 tmp = smb_fname->base_name;
724 smb_fname->base_name = shadow_copy2_convert(
725 talloc_tos(), handle, stripped, timestamp);
726 TALLOC_FREE(stripped);
728 if (smb_fname->base_name == NULL) {
729 smb_fname->base_name = tmp;
730 return -1;
733 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
734 saved_errno = errno;
736 TALLOC_FREE(smb_fname->base_name);
737 smb_fname->base_name = tmp;
739 if (ret == 0) {
740 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
742 errno = saved_errno;
743 return ret;
746 static int shadow_copy2_lstat(vfs_handle_struct *handle,
747 struct smb_filename *smb_fname)
749 time_t timestamp;
750 char *stripped, *tmp;
751 int ret, saved_errno;
753 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
754 smb_fname->base_name,
755 &timestamp, &stripped)) {
756 return -1;
758 if (timestamp == 0) {
759 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
762 tmp = smb_fname->base_name;
763 smb_fname->base_name = shadow_copy2_convert(
764 talloc_tos(), handle, stripped, timestamp);
765 TALLOC_FREE(stripped);
767 if (smb_fname->base_name == NULL) {
768 smb_fname->base_name = tmp;
769 return -1;
772 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
773 saved_errno = errno;
775 TALLOC_FREE(smb_fname->base_name);
776 smb_fname->base_name = tmp;
778 if (ret == 0) {
779 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
781 errno = saved_errno;
782 return ret;
785 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
786 SMB_STRUCT_STAT *sbuf)
788 time_t timestamp;
789 int ret;
791 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
792 if (ret == -1) {
793 return ret;
795 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
796 fsp->fsp_name->base_name,
797 &timestamp, NULL)) {
798 return 0;
800 if (timestamp != 0) {
801 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
803 return 0;
806 static int shadow_copy2_open(vfs_handle_struct *handle,
807 struct smb_filename *smb_fname, files_struct *fsp,
808 int flags, mode_t mode)
810 time_t timestamp;
811 char *stripped, *tmp;
812 int ret, saved_errno;
814 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
815 smb_fname->base_name,
816 &timestamp, &stripped)) {
817 return -1;
819 if (timestamp == 0) {
820 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
823 tmp = smb_fname->base_name;
824 smb_fname->base_name = shadow_copy2_convert(
825 talloc_tos(), handle, stripped, timestamp);
826 TALLOC_FREE(stripped);
828 if (smb_fname->base_name == NULL) {
829 smb_fname->base_name = tmp;
830 return -1;
833 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
834 saved_errno = errno;
836 TALLOC_FREE(smb_fname->base_name);
837 smb_fname->base_name = tmp;
839 errno = saved_errno;
840 return ret;
843 static int shadow_copy2_unlink(vfs_handle_struct *handle,
844 const struct smb_filename *smb_fname)
846 time_t timestamp;
847 char *stripped;
848 int ret, saved_errno;
849 struct smb_filename *conv;
850 NTSTATUS status;
852 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
853 smb_fname->base_name,
854 &timestamp, &stripped)) {
855 return -1;
857 if (timestamp == 0) {
858 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
860 status = copy_smb_filename(talloc_tos(), smb_fname, &conv);
861 if (!NT_STATUS_IS_OK(status)) {
862 errno = ENOMEM;
863 return -1;
865 conv->base_name = shadow_copy2_convert(
866 conv, handle, stripped, timestamp);
867 TALLOC_FREE(stripped);
868 if (conv->base_name == NULL) {
869 return -1;
871 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
872 saved_errno = errno;
873 TALLOC_FREE(conv);
874 errno = saved_errno;
875 return ret;
878 static int shadow_copy2_chmod(vfs_handle_struct *handle, const char *fname,
879 mode_t mode)
881 time_t timestamp;
882 char *stripped;
883 int ret, saved_errno;
884 char *conv;
886 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
887 &timestamp, &stripped)) {
888 return -1;
890 if (timestamp == 0) {
891 return SMB_VFS_NEXT_CHMOD(handle, fname, mode);
893 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
894 TALLOC_FREE(stripped);
895 if (conv == NULL) {
896 return -1;
898 ret = SMB_VFS_NEXT_CHMOD(handle, conv, mode);
899 saved_errno = errno;
900 TALLOC_FREE(conv);
901 errno = saved_errno;
902 return ret;
905 static int shadow_copy2_chown(vfs_handle_struct *handle, const char *fname,
906 uid_t uid, gid_t gid)
908 time_t timestamp;
909 char *stripped;
910 int ret, saved_errno;
911 char *conv;
913 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
914 &timestamp, &stripped)) {
915 return -1;
917 if (timestamp == 0) {
918 return SMB_VFS_NEXT_CHOWN(handle, fname, uid, gid);
920 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
921 TALLOC_FREE(stripped);
922 if (conv == NULL) {
923 return -1;
925 ret = SMB_VFS_NEXT_CHOWN(handle, conv, uid, gid);
926 saved_errno = errno;
927 TALLOC_FREE(conv);
928 errno = saved_errno;
929 return ret;
932 static int shadow_copy2_chdir(vfs_handle_struct *handle,
933 const char *fname)
935 time_t timestamp;
936 char *stripped;
937 int ret, saved_errno;
938 char *conv;
940 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
941 &timestamp, &stripped)) {
942 return -1;
944 if (timestamp == 0) {
945 return SMB_VFS_NEXT_CHDIR(handle, fname);
947 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
948 TALLOC_FREE(stripped);
949 if (conv == NULL) {
950 return -1;
952 ret = SMB_VFS_NEXT_CHDIR(handle, conv);
953 saved_errno = errno;
954 TALLOC_FREE(conv);
955 errno = saved_errno;
956 return ret;
959 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
960 const struct smb_filename *smb_fname,
961 struct smb_file_time *ft)
963 time_t timestamp;
964 char *stripped;
965 int ret, saved_errno;
966 struct smb_filename *conv;
967 NTSTATUS status;
969 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
970 smb_fname->base_name,
971 &timestamp, &stripped)) {
972 return -1;
974 if (timestamp == 0) {
975 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
977 status = copy_smb_filename(talloc_tos(), smb_fname, &conv);
978 if (!NT_STATUS_IS_OK(status)) {
979 errno = ENOMEM;
980 return -1;
982 conv->base_name = shadow_copy2_convert(
983 conv, handle, stripped, timestamp);
984 TALLOC_FREE(stripped);
985 if (conv->base_name == NULL) {
986 return -1;
988 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
989 saved_errno = errno;
990 TALLOC_FREE(conv);
991 errno = saved_errno;
992 return ret;
995 static int shadow_copy2_readlink(vfs_handle_struct *handle,
996 const char *fname, char *buf, size_t bufsiz)
998 time_t timestamp;
999 char *stripped;
1000 int ret, saved_errno;
1001 char *conv;
1003 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1004 &timestamp, &stripped)) {
1005 return -1;
1007 if (timestamp == 0) {
1008 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
1010 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1011 TALLOC_FREE(stripped);
1012 if (conv == NULL) {
1013 return -1;
1015 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1016 saved_errno = errno;
1017 TALLOC_FREE(conv);
1018 errno = saved_errno;
1019 return ret;
1022 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1023 const char *fname, mode_t mode, SMB_DEV_T dev)
1025 time_t timestamp;
1026 char *stripped;
1027 int ret, saved_errno;
1028 char *conv;
1030 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1031 &timestamp, &stripped)) {
1032 return -1;
1034 if (timestamp == 0) {
1035 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
1037 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1038 TALLOC_FREE(stripped);
1039 if (conv == NULL) {
1040 return -1;
1042 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1043 saved_errno = errno;
1044 TALLOC_FREE(conv);
1045 errno = saved_errno;
1046 return ret;
1049 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
1050 const char *fname)
1052 time_t timestamp;
1053 char *stripped = NULL;
1054 char *tmp = NULL;
1055 char *result = NULL;
1056 char *inserted = NULL;
1057 char *inserted_to, *inserted_end;
1058 int saved_errno;
1060 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1061 &timestamp, &stripped)) {
1062 goto done;
1064 if (timestamp == 0) {
1065 return SMB_VFS_NEXT_REALPATH(handle, fname);
1068 tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1069 if (tmp == NULL) {
1070 goto done;
1073 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1074 if (result == NULL) {
1075 goto done;
1079 * Take away what we've inserted. This removes the @GMT-thingy
1080 * completely, but will give a path under the share root.
1082 inserted = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
1083 if (inserted == NULL) {
1084 goto done;
1086 inserted_to = strstr_m(result, inserted);
1087 if (inserted_to == NULL) {
1088 DEBUG(2, ("SMB_VFS_NEXT_REALPATH removed %s\n", inserted));
1089 goto done;
1091 inserted_end = inserted_to + talloc_get_size(inserted) - 1;
1092 memmove(inserted_to, inserted_end, strlen(inserted_end)+1);
1094 done:
1095 saved_errno = errno;
1096 TALLOC_FREE(inserted);
1097 TALLOC_FREE(tmp);
1098 TALLOC_FREE(stripped);
1099 errno = saved_errno;
1100 return result;
1104 * Check whether a given directory contains a
1105 * snapshot directory as direct subdirectory.
1106 * If yes, return the path of the snapshot-subdir,
1107 * otherwise return NULL.
1109 static char *have_snapdir(struct vfs_handle_struct *handle,
1110 const char *path)
1112 struct smb_filename smb_fname;
1113 int ret;
1114 struct shadow_copy2_config *config;
1116 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1117 return NULL);
1119 ZERO_STRUCT(smb_fname);
1120 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1121 path, config->snapdir);
1122 if (smb_fname.base_name == NULL) {
1123 return NULL;
1126 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1127 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1128 return smb_fname.base_name;
1130 TALLOC_FREE(smb_fname.base_name);
1131 return NULL;
1135 * Find the snapshot directory (if any) for the given
1136 * filename (which is relative to the share).
1138 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1139 struct vfs_handle_struct *handle,
1140 struct smb_filename *smb_fname)
1142 char *path, *p;
1143 const char *snapdir;
1144 struct shadow_copy2_config *config;
1146 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1147 return NULL);
1150 * If the non-snapdisrseverywhere mode, we should not search!
1152 if (!config->snapdirseverywhere) {
1153 return config->snapshot_basepath;
1156 path = talloc_asprintf(mem_ctx, "%s/%s",
1157 handle->conn->connectpath,
1158 smb_fname->base_name);
1159 if (path == NULL) {
1160 return NULL;
1163 snapdir = have_snapdir(handle, path);
1164 if (snapdir != NULL) {
1165 TALLOC_FREE(path);
1166 return snapdir;
1169 while ((p = strrchr(path, '/')) && (p > path)) {
1171 p[0] = '\0';
1173 snapdir = have_snapdir(handle, path);
1174 if (snapdir != NULL) {
1175 TALLOC_FREE(path);
1176 return snapdir;
1179 TALLOC_FREE(path);
1180 return NULL;
1183 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1184 const char *name,
1185 char *gmt, size_t gmt_len)
1187 struct tm timestamp;
1188 time_t timestamp_t;
1189 unsigned long int timestamp_long;
1190 const char *fmt;
1191 struct shadow_copy2_config *config;
1193 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1194 return NULL);
1196 fmt = config->gmt_format;
1198 ZERO_STRUCT(timestamp);
1199 if (config->use_sscanf) {
1200 if (sscanf(name, fmt, &timestamp_long) != 1) {
1201 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1202 "no sscanf match %s: %s\n",
1203 fmt, name));
1204 return false;
1206 timestamp_t = timestamp_long;
1207 gmtime_r(&timestamp_t, &timestamp);
1208 } else {
1209 if (strptime(name, fmt, &timestamp) == NULL) {
1210 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1211 "no match %s: %s\n",
1212 fmt, name));
1213 return false;
1215 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1216 fmt, name));
1218 if (config->use_localtime) {
1219 timestamp.tm_isdst = -1;
1220 timestamp_t = mktime(&timestamp);
1221 gmtime_r(&timestamp_t, &timestamp);
1225 strftime(gmt, gmt_len, GMT_FORMAT, &timestamp);
1226 return true;
1229 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1231 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1234 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1236 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1240 sort the shadow copy data in ascending or descending order
1242 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1243 struct shadow_copy_data *shadow_copy2_data)
1245 int (*cmpfunc)(const void *, const void *);
1246 const char *sort;
1247 struct shadow_copy2_config *config;
1249 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1250 return);
1252 sort = config->sort_order;
1253 if (sort == NULL) {
1254 return;
1257 if (strcmp(sort, "asc") == 0) {
1258 cmpfunc = shadow_copy2_label_cmp_asc;
1259 } else if (strcmp(sort, "desc") == 0) {
1260 cmpfunc = shadow_copy2_label_cmp_desc;
1261 } else {
1262 return;
1265 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1266 shadow_copy2_data->labels)
1268 TYPESAFE_QSORT(shadow_copy2_data->labels,
1269 shadow_copy2_data->num_volumes,
1270 cmpfunc);
1274 static int shadow_copy2_get_shadow_copy_data(
1275 vfs_handle_struct *handle, files_struct *fsp,
1276 struct shadow_copy_data *shadow_copy2_data,
1277 bool labels)
1279 DIR *p;
1280 const char *snapdir;
1281 struct dirent *d;
1282 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1284 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1285 if (snapdir == NULL) {
1286 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1287 handle->conn->connectpath));
1288 errno = EINVAL;
1289 talloc_free(tmp_ctx);
1290 return -1;
1293 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir, NULL, 0);
1295 if (!p) {
1296 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1297 " - %s\n", snapdir, strerror(errno)));
1298 talloc_free(tmp_ctx);
1299 errno = ENOSYS;
1300 return -1;
1303 shadow_copy2_data->num_volumes = 0;
1304 shadow_copy2_data->labels = NULL;
1306 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
1307 char snapshot[GMT_NAME_LEN+1];
1308 SHADOW_COPY_LABEL *tlabels;
1311 * ignore names not of the right form in the snapshot
1312 * directory
1314 if (!shadow_copy2_snapshot_to_gmt(
1315 handle, d->d_name,
1316 snapshot, sizeof(snapshot))) {
1318 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1319 "ignoring %s\n", d->d_name));
1320 continue;
1322 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1323 d->d_name, snapshot));
1325 if (!labels) {
1326 /* the caller doesn't want the labels */
1327 shadow_copy2_data->num_volumes++;
1328 continue;
1331 tlabels = talloc_realloc(shadow_copy2_data,
1332 shadow_copy2_data->labels,
1333 SHADOW_COPY_LABEL,
1334 shadow_copy2_data->num_volumes+1);
1335 if (tlabels == NULL) {
1336 DEBUG(0,("shadow_copy2: out of memory\n"));
1337 SMB_VFS_NEXT_CLOSEDIR(handle, p);
1338 talloc_free(tmp_ctx);
1339 return -1;
1342 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
1343 sizeof(*tlabels));
1345 shadow_copy2_data->num_volumes++;
1346 shadow_copy2_data->labels = tlabels;
1349 SMB_VFS_NEXT_CLOSEDIR(handle,p);
1351 shadow_copy2_sort_data(handle, shadow_copy2_data);
1353 talloc_free(tmp_ctx);
1354 return 0;
1357 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
1358 struct files_struct *fsp,
1359 uint32 security_info,
1360 TALLOC_CTX *mem_ctx,
1361 struct security_descriptor **ppdesc)
1363 time_t timestamp;
1364 char *stripped;
1365 NTSTATUS status;
1366 char *conv;
1368 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1369 fsp->fsp_name->base_name,
1370 &timestamp, &stripped)) {
1371 return map_nt_error_from_unix(errno);
1373 if (timestamp == 0) {
1374 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1375 mem_ctx,
1376 ppdesc);
1378 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1379 TALLOC_FREE(stripped);
1380 if (conv == NULL) {
1381 return map_nt_error_from_unix(errno);
1383 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
1384 mem_ctx, ppdesc);
1385 TALLOC_FREE(conv);
1386 return status;
1389 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
1390 const char *fname,
1391 uint32 security_info,
1392 TALLOC_CTX *mem_ctx,
1393 struct security_descriptor **ppdesc)
1395 time_t timestamp;
1396 char *stripped;
1397 NTSTATUS status;
1398 char *conv;
1400 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1401 &timestamp, &stripped)) {
1402 return map_nt_error_from_unix(errno);
1404 if (timestamp == 0) {
1405 return SMB_VFS_NEXT_GET_NT_ACL(handle, fname, security_info,
1406 mem_ctx, ppdesc);
1408 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1409 TALLOC_FREE(stripped);
1410 if (conv == NULL) {
1411 return map_nt_error_from_unix(errno);
1413 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
1414 mem_ctx, ppdesc);
1415 TALLOC_FREE(conv);
1416 return status;
1419 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
1420 const char *fname, mode_t mode)
1422 time_t timestamp;
1423 char *stripped;
1424 int ret, saved_errno;
1425 char *conv;
1427 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1428 &timestamp, &stripped)) {
1429 return -1;
1431 if (timestamp == 0) {
1432 return SMB_VFS_NEXT_MKDIR(handle, fname, mode);
1434 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1435 TALLOC_FREE(stripped);
1436 if (conv == NULL) {
1437 return -1;
1439 ret = SMB_VFS_NEXT_MKDIR(handle, conv, mode);
1440 saved_errno = errno;
1441 TALLOC_FREE(conv);
1442 errno = saved_errno;
1443 return ret;
1446 static int shadow_copy2_rmdir(vfs_handle_struct *handle, const char *fname)
1448 time_t timestamp;
1449 char *stripped;
1450 int ret, saved_errno;
1451 char *conv;
1453 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1454 &timestamp, &stripped)) {
1455 return -1;
1457 if (timestamp == 0) {
1458 return SMB_VFS_NEXT_RMDIR(handle, fname);
1460 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1461 TALLOC_FREE(stripped);
1462 if (conv == NULL) {
1463 return -1;
1465 ret = SMB_VFS_NEXT_RMDIR(handle, conv);
1466 saved_errno = errno;
1467 TALLOC_FREE(conv);
1468 errno = saved_errno;
1469 return ret;
1472 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
1473 unsigned int flags)
1475 time_t timestamp;
1476 char *stripped;
1477 int ret, saved_errno;
1478 char *conv;
1480 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1481 &timestamp, &stripped)) {
1482 return -1;
1484 if (timestamp == 0) {
1485 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
1487 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1488 TALLOC_FREE(stripped);
1489 if (conv == NULL) {
1490 return -1;
1492 ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
1493 saved_errno = errno;
1494 TALLOC_FREE(conv);
1495 errno = saved_errno;
1496 return ret;
1499 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
1500 const char *fname, const char *aname,
1501 void *value, size_t size)
1503 time_t timestamp;
1504 char *stripped;
1505 ssize_t ret;
1506 int saved_errno;
1507 char *conv;
1509 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1510 &timestamp, &stripped)) {
1511 return -1;
1513 if (timestamp == 0) {
1514 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
1515 size);
1517 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1518 TALLOC_FREE(stripped);
1519 if (conv == NULL) {
1520 return -1;
1522 ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
1523 saved_errno = errno;
1524 TALLOC_FREE(conv);
1525 errno = saved_errno;
1526 return ret;
1529 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
1530 const char *fname,
1531 char *list, size_t size)
1533 time_t timestamp;
1534 char *stripped;
1535 ssize_t ret;
1536 int saved_errno;
1537 char *conv;
1539 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1540 &timestamp, &stripped)) {
1541 return -1;
1543 if (timestamp == 0) {
1544 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
1546 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1547 TALLOC_FREE(stripped);
1548 if (conv == NULL) {
1549 return -1;
1551 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
1552 saved_errno = errno;
1553 TALLOC_FREE(conv);
1554 errno = saved_errno;
1555 return ret;
1558 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
1559 const char *fname, const char *aname)
1561 time_t timestamp;
1562 char *stripped;
1563 int ret, saved_errno;
1564 char *conv;
1566 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1567 &timestamp, &stripped)) {
1568 return -1;
1570 if (timestamp == 0) {
1571 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
1573 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1574 TALLOC_FREE(stripped);
1575 if (conv == NULL) {
1576 return -1;
1578 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
1579 saved_errno = errno;
1580 TALLOC_FREE(conv);
1581 errno = saved_errno;
1582 return ret;
1585 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
1586 const char *fname,
1587 const char *aname, const void *value,
1588 size_t size, int flags)
1590 time_t timestamp;
1591 char *stripped;
1592 ssize_t ret;
1593 int saved_errno;
1594 char *conv;
1596 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1597 &timestamp, &stripped)) {
1598 return -1;
1600 if (timestamp == 0) {
1601 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
1602 flags);
1604 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1605 TALLOC_FREE(stripped);
1606 if (conv == NULL) {
1607 return -1;
1609 ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
1610 saved_errno = errno;
1611 TALLOC_FREE(conv);
1612 errno = saved_errno;
1613 return ret;
1616 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
1617 const char *fname, mode_t mode)
1619 time_t timestamp;
1620 char *stripped;
1621 ssize_t ret;
1622 int saved_errno;
1623 char *conv;
1625 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1626 &timestamp, &stripped)) {
1627 return -1;
1629 if (timestamp == 0) {
1630 return SMB_VFS_NEXT_CHMOD_ACL(handle, fname, mode);
1632 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1633 TALLOC_FREE(stripped);
1634 if (conv == NULL) {
1635 return -1;
1637 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv, mode);
1638 saved_errno = errno;
1639 TALLOC_FREE(conv);
1640 errno = saved_errno;
1641 return ret;
1644 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
1645 const char *path,
1646 const char *name,
1647 TALLOC_CTX *mem_ctx,
1648 char **found_name)
1650 time_t timestamp;
1651 char *stripped;
1652 ssize_t ret;
1653 int saved_errno;
1654 char *conv;
1656 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1657 &timestamp, &stripped)) {
1658 return -1;
1660 if (timestamp == 0) {
1661 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
1662 mem_ctx, found_name);
1664 if (stripped[0] == '\0') {
1665 *found_name = talloc_strdup(mem_ctx, name);
1666 if (*found_name == NULL) {
1667 errno = ENOMEM;
1668 return -1;
1670 return 0;
1672 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1673 TALLOC_FREE(stripped);
1674 if (conv == NULL) {
1675 return -1;
1677 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
1678 mem_ctx, found_name);
1679 saved_errno = errno;
1680 TALLOC_FREE(conv);
1681 errno = saved_errno;
1682 return ret;
1685 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
1686 const char *path, bool small_query,
1687 uint64_t *bsize, uint64_t *dfree,
1688 uint64_t *dsize)
1690 time_t timestamp;
1691 char *stripped;
1692 ssize_t ret;
1693 int saved_errno;
1694 char *conv;
1696 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1697 &timestamp, &stripped)) {
1698 return -1;
1700 if (timestamp == 0) {
1701 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1702 bsize, dfree, dsize);
1705 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1706 TALLOC_FREE(stripped);
1707 if (conv == NULL) {
1708 return -1;
1711 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, small_query, bsize, dfree,
1712 dsize);
1714 saved_errno = errno;
1715 TALLOC_FREE(conv);
1716 errno = saved_errno;
1718 return ret;
1721 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
1722 const char *service, const char *user)
1724 struct shadow_copy2_config *config;
1725 int ret;
1726 const char *snapdir;
1727 const char *gmt_format;
1728 const char *sort_order;
1729 const char *basedir;
1730 const char *mount_point;
1732 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
1733 (unsigned)handle->conn->cnum,
1734 handle->conn->connectpath));
1736 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1737 if (ret < 0) {
1738 return ret;
1741 config = talloc_zero(handle->conn, struct shadow_copy2_config);
1742 if (config == NULL) {
1743 DEBUG(0, ("talloc_zero() failed\n"));
1744 errno = ENOMEM;
1745 return -1;
1748 gmt_format = lp_parm_const_string(SNUM(handle->conn),
1749 "shadow", "format",
1750 GMT_FORMAT);
1751 config->gmt_format = talloc_strdup(config, gmt_format);
1752 if (config->gmt_format == NULL) {
1753 DEBUG(0, ("talloc_strdup() failed\n"));
1754 errno = ENOMEM;
1755 return -1;
1758 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
1759 "shadow", "sscanf", false);
1761 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
1762 "shadow", "localtime",
1763 false);
1765 snapdir = lp_parm_const_string(SNUM(handle->conn),
1766 "shadow", "snapdir",
1767 ".snapshots");
1768 config->snapdir = talloc_strdup(config, snapdir);
1769 if (config->snapdir == NULL) {
1770 DEBUG(0, ("talloc_strdup() failed\n"));
1771 errno = ENOMEM;
1772 return -1;
1775 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
1776 "shadow",
1777 "snapdirseverywhere",
1778 false);
1780 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
1781 "shadow", "crossmountpoints",
1782 false);
1784 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
1785 "shadow", "fixinodes",
1786 false);
1788 sort_order = lp_parm_const_string(SNUM(handle->conn),
1789 "shadow", "sort", "desc");
1790 config->sort_order = talloc_strdup(config, sort_order);
1791 if (config->sort_order == NULL) {
1792 DEBUG(0, ("talloc_strdup() failed\n"));
1793 errno = ENOMEM;
1794 return -1;
1797 mount_point = lp_parm_const_string(SNUM(handle->conn),
1798 "shadow", "mountpoint", NULL);
1799 if (mount_point != NULL) {
1800 if (mount_point[0] != '/') {
1801 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
1802 "relative ('%s'), but it has to be an "
1803 "absolute path. Ignoring provided value.\n",
1804 mount_point));
1805 mount_point = NULL;
1806 } else {
1807 char *p;
1808 p = strstr(handle->conn->connectpath, mount_point);
1809 if (p != handle->conn->connectpath) {
1810 DEBUG(1, ("Warning: mount_point (%s) is not a "
1811 "subdirectory of the share root "
1812 "(%s). Ignoring provided value.\n",
1813 mount_point,
1814 handle->conn->connectpath));
1815 mount_point = NULL;
1820 if (mount_point != NULL) {
1821 config->mount_point = talloc_strdup(config, mount_point);
1822 if (config->mount_point == NULL) {
1823 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
1824 return -1;
1826 } else {
1827 config->mount_point = shadow_copy2_find_mount_point(config,
1828 handle);
1829 if (config->mount_point == NULL) {
1830 DEBUG(0, (__location__ ": shadow_copy2_find_mount_point"
1831 " failed: %s\n", strerror(errno)));
1832 return -1;
1836 basedir = lp_parm_const_string(SNUM(handle->conn),
1837 "shadow", "basedir", NULL);
1839 if (basedir != NULL) {
1840 if (basedir[0] != '/') {
1841 DEBUG(1, (__location__ " Warning: 'basedir' is "
1842 "relative ('%s'), but it has to be an "
1843 "absolute path. Disabling basedir.\n",
1844 basedir));
1845 } else {
1846 char *p;
1847 p = strstr(basedir, config->mount_point);
1848 if (p != basedir) {
1849 DEBUG(1, ("Warning: basedir (%s) is not a "
1850 "subdirectory of the share root's "
1851 "mount point (%s). "
1852 "Disabling basedir\n",
1853 basedir, config->mount_point));
1854 } else {
1855 config->basedir = talloc_strdup(config,
1856 basedir);
1857 if (config->basedir == NULL) {
1858 DEBUG(0, ("talloc_strdup() failed\n"));
1859 errno = ENOMEM;
1860 return -1;
1866 if (config->snapdirseverywhere && config->basedir != NULL) {
1867 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
1868 "with 'snapdirseverywhere'. Disabling basedir.\n"));
1869 TALLOC_FREE(config->basedir);
1872 if (config->crossmountpoints && config->basedir != NULL) {
1873 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
1874 "with 'crossmountpoints'. Disabling basedir.\n"));
1875 TALLOC_FREE(config->basedir);
1878 if (config->basedir == NULL) {
1879 config->basedir = config->mount_point;
1882 if (strlen(config->basedir) != strlen(handle->conn->connectpath)) {
1883 config->rel_connectpath = talloc_strdup(config,
1884 handle->conn->connectpath + strlen(config->basedir));
1885 if (config->rel_connectpath == NULL) {
1886 DEBUG(0, ("talloc_strdup() failed\n"));
1887 errno = ENOMEM;
1888 return -1;
1892 if (config->snapdir[0] == '/') {
1893 config->snapdir_absolute = true;
1895 if (config->snapdirseverywhere == true) {
1896 DEBUG(1, (__location__ " Warning: An absolute snapdir "
1897 "is incompatible with 'snapdirseverywhere', "
1898 "setting 'snapdirseverywhere' to false.\n"));
1899 config->snapdirseverywhere = false;
1902 if (config->crossmountpoints == true) {
1903 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
1904 "is not supported with an absolute snapdir. "
1905 "Disabling it.\n"));
1906 config->crossmountpoints = false;
1909 config->snapshot_basepath = config->snapdir;
1910 } else {
1911 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
1912 config->mount_point, config->snapdir);
1913 if (config->snapshot_basepath == NULL) {
1914 DEBUG(0, ("talloc_asprintf() failed\n"));
1915 errno = ENOMEM;
1916 return -1;
1920 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
1921 " share root: '%s'\n"
1922 " basedir: '%s'\n"
1923 " mountpoint: '%s'\n"
1924 " rel share root: '%s'\n"
1925 " snapdir: '%s'\n"
1926 " snapshot base path: '%s'\n"
1927 " format: '%s'\n"
1928 " use sscanf: %s\n"
1929 " snapdirs everywhere: %s\n"
1930 " cross mountpoints: %s\n"
1931 " fix inodes: %s\n"
1932 " sort order: %s\n"
1934 handle->conn->connectpath,
1935 config->basedir,
1936 config->mount_point,
1937 config->rel_connectpath,
1938 config->snapdir,
1939 config->snapshot_basepath,
1940 config->gmt_format,
1941 config->use_sscanf ? "yes" : "no",
1942 config->snapdirseverywhere ? "yes" : "no",
1943 config->crossmountpoints ? "yes" : "no",
1944 config->fixinodes ? "yes" : "no",
1945 config->sort_order
1949 SMB_VFS_HANDLE_SET_DATA(handle, config,
1950 NULL, struct shadow_copy2_config,
1951 return -1);
1953 return 0;
1956 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
1957 .connect_fn = shadow_copy2_connect,
1958 .opendir_fn = shadow_copy2_opendir,
1959 .disk_free_fn = shadow_copy2_disk_free,
1960 .rename_fn = shadow_copy2_rename,
1961 .link_fn = shadow_copy2_link,
1962 .symlink_fn = shadow_copy2_symlink,
1963 .stat_fn = shadow_copy2_stat,
1964 .lstat_fn = shadow_copy2_lstat,
1965 .fstat_fn = shadow_copy2_fstat,
1966 .open_fn = shadow_copy2_open,
1967 .unlink_fn = shadow_copy2_unlink,
1968 .chmod_fn = shadow_copy2_chmod,
1969 .chown_fn = shadow_copy2_chown,
1970 .chdir_fn = shadow_copy2_chdir,
1971 .ntimes_fn = shadow_copy2_ntimes,
1972 .readlink_fn = shadow_copy2_readlink,
1973 .mknod_fn = shadow_copy2_mknod,
1974 .realpath_fn = shadow_copy2_realpath,
1975 .get_nt_acl_fn = shadow_copy2_get_nt_acl,
1976 .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
1977 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
1978 .mkdir_fn = shadow_copy2_mkdir,
1979 .rmdir_fn = shadow_copy2_rmdir,
1980 .getxattr_fn = shadow_copy2_getxattr,
1981 .listxattr_fn = shadow_copy2_listxattr,
1982 .removexattr_fn = shadow_copy2_removexattr,
1983 .setxattr_fn = shadow_copy2_setxattr,
1984 .chmod_acl_fn = shadow_copy2_chmod_acl,
1985 .chflags_fn = shadow_copy2_chflags,
1986 .get_real_filename_fn = shadow_copy2_get_real_filename,
1989 NTSTATUS vfs_shadow_copy2_init(void);
1990 NTSTATUS vfs_shadow_copy2_init(void)
1992 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1993 "shadow_copy2", &vfs_shadow_copy2_fns);