s3-shadow-copy2: Add extreme debug output to shadow_copy2_strip_snapshot
[Samba.git] / source3 / modules / vfs_shadow_copy2.c
blob1b3c48416d235e74dd696c3a9ccbe7ce5d0eeceb
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 timestamp, build the posix level GMT-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 timestamp, 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 a 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 DEBUG(11, ("@GMT not found\n"));
247 goto no_snapshot;
249 if ((p > name) && (p[-1] != '/')) {
250 /* the GMT-token does not start a path-component */
251 DEBUG(10, ("not at start, p=%p, name=%p, p[-1]=%d\n",
252 p, name, (int)p[-1]));
253 goto no_snapshot;
255 q = strptime(p, GMT_FORMAT, &tm);
256 if (q == NULL) {
257 DEBUG(10, ("strptime failed\n"));
258 goto no_snapshot;
260 tm.tm_isdst = -1;
261 timestamp = timegm(&tm);
262 if (timestamp == (time_t)-1) {
263 DEBUG(10, ("timestamp==-1\n"));
264 goto no_snapshot;
266 if ((p == name) && (q[0] == '\0')) {
267 /* the name consists of only the GMT token */
268 if (pstripped != NULL) {
269 stripped = talloc_strdup(mem_ctx, "");
270 if (stripped == NULL) {
271 return false;
273 *pstripped = stripped;
275 *ptimestamp = timestamp;
276 return true;
278 if (q[0] != '/') {
280 * The GMT token is either at the end of the path
281 * or it is not a complete path component, i.e. the
282 * path component continues after the gmt-token.
284 * TODO: Is this correct? Or would the GMT tag as the
285 * last component be a valid input?
287 DEBUG(10, ("q[0] = %d\n", (int)q[0]));
288 goto no_snapshot;
290 q += 1;
292 rest_len = strlen(q);
293 dst_len = (p-name) + rest_len;
295 if (config->snapdirseverywhere) {
296 char *insert;
297 bool have_insert;
298 insert = shadow_copy2_insert_string(talloc_tos(), handle,
299 timestamp);
300 if (insert == NULL) {
301 errno = ENOMEM;
302 return false;
305 DEBUG(10, (__location__ ": snapdirseverywhere mode.\n"
306 "path '%s'.\n"
307 "insert string '%s'\n", name, insert));
309 have_insert = (strstr(name, insert+1) != NULL);
310 DEBUG(10, ("have_insert=%d, name=%s, insert+1=%s\n",
311 (int)have_insert, name, insert+1));
312 if (have_insert) {
313 DEBUG(10, (__location__ ": insert string '%s' found in "
314 "path '%s' found in snapdirseverywhere mode "
315 "==> already converted\n", insert, name));
316 TALLOC_FREE(insert);
317 goto no_snapshot;
319 TALLOC_FREE(insert);
320 } else {
321 char *snapshot_path;
322 char *s;
324 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
325 handle,
326 timestamp);
327 if (snapshot_path == NULL) {
328 errno = ENOMEM;
329 return false;
332 DEBUG(10, (__location__ " path: '%s'.\n"
333 "snapshot path: '%s'\n", name, snapshot_path));
335 s = strstr(name, snapshot_path);
336 if (s == name) {
338 * this starts with "snapshot_basepath/GMT-Token"
339 * so it is already a converted absolute
340 * path. Don't process further.
342 DEBUG(10, (__location__ ": path '%s' starts with "
343 "snapshot path '%s' (not in "
344 "snapdirseverywhere mode) ==> "
345 "already converted\n", name, snapshot_path));
346 talloc_free(snapshot_path);
347 goto no_snapshot;
349 talloc_free(snapshot_path);
352 if (pstripped != NULL) {
353 stripped = talloc_array(mem_ctx, char, dst_len+1);
354 if (stripped == NULL) {
355 errno = ENOMEM;
356 return false;
358 if (p > name) {
359 memcpy(stripped, name, p-name);
361 if (rest_len > 0) {
362 memcpy(stripped + (p-name), q, rest_len);
364 stripped[dst_len] = '\0';
365 *pstripped = stripped;
367 *ptimestamp = timestamp;
368 return true;
369 no_snapshot:
370 *ptimestamp = 0;
371 return true;
374 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
375 vfs_handle_struct *handle)
377 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
378 dev_t dev;
379 struct stat st;
380 char *p;
382 if (stat(path, &st) != 0) {
383 talloc_free(path);
384 return NULL;
387 dev = st.st_dev;
389 while ((p = strrchr(path, '/')) && p > path) {
390 *p = 0;
391 if (stat(path, &st) != 0) {
392 talloc_free(path);
393 return NULL;
395 if (st.st_dev != dev) {
396 *p = '/';
397 break;
401 return path;
405 * Convert from a name as handed in via the SMB layer
406 * and a timestamp into the local path of the snapshot
407 * of the provided file at the provided time.
409 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
410 struct vfs_handle_struct *handle,
411 const char *name, time_t timestamp)
413 struct smb_filename converted_fname;
414 char *result = NULL;
415 size_t *slashes = NULL;
416 unsigned num_slashes;
417 char *path = NULL;
418 size_t pathlen;
419 char *insert = NULL;
420 char *converted = NULL;
421 size_t insertlen;
422 int i, saved_errno;
423 size_t min_offset;
424 struct shadow_copy2_config *config;
426 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
427 return NULL);
429 DEBUG(10, ("converting '%s'\n", name));
431 if (!config->snapdirseverywhere) {
432 int ret;
433 char *snapshot_path;
435 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
436 handle,
437 timestamp);
438 if (snapshot_path == NULL) {
439 goto fail;
442 if (config->rel_connectpath == NULL) {
443 converted = talloc_asprintf(mem_ctx, "%s/%s",
444 snapshot_path, name);
445 } else {
446 converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
447 snapshot_path,
448 config->rel_connectpath,
449 name);
451 if (converted == NULL) {
452 goto fail;
455 ZERO_STRUCT(converted_fname);
456 converted_fname.base_name = converted;
458 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
459 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
460 converted,
461 ret, ret == 0 ? "ok" : strerror(errno)));
462 if (ret == 0) {
463 DEBUG(10, ("Found %s\n", converted));
464 result = converted;
465 converted = NULL;
466 goto fail;
467 } else {
468 errno = ENOENT;
469 goto fail;
471 /* never reached ... */
474 path = talloc_asprintf(mem_ctx, "%s/%s", handle->conn->connectpath,
475 name);
476 if (path == NULL) {
477 errno = ENOMEM;
478 goto fail;
480 pathlen = talloc_get_size(path)-1;
482 if (!shadow_copy2_find_slashes(talloc_tos(), path,
483 &slashes, &num_slashes)) {
484 goto fail;
487 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
488 if (insert == NULL) {
489 goto fail;
491 insertlen = talloc_get_size(insert)-1;
494 * Note: We deliberatly don't expensively initialize the
495 * array with talloc_zero here: Putting zero into
496 * converted[pathlen+insertlen] below is sufficient, because
497 * in the following for loop, the insert string is inserted
498 * at various slash places. So the memory up to position
499 * pathlen+insertlen will always be initialized when the
500 * converted string is used.
502 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
503 if (converted == NULL) {
504 goto fail;
507 if (path[pathlen-1] != '/') {
509 * Append a fake slash to find the snapshot root
511 size_t *tmp;
512 tmp = talloc_realloc(talloc_tos(), slashes,
513 size_t, num_slashes+1);
514 if (tmp == NULL) {
515 goto fail;
517 slashes = tmp;
518 slashes[num_slashes] = pathlen;
519 num_slashes += 1;
522 min_offset = 0;
524 if (!config->crossmountpoints) {
525 min_offset = strlen(config->mount_point);
528 memcpy(converted, path, pathlen+1);
529 converted[pathlen+insertlen] = '\0';
531 ZERO_STRUCT(converted_fname);
532 converted_fname.base_name = converted;
534 for (i = num_slashes-1; i>=0; i--) {
535 int ret;
536 size_t offset;
538 offset = slashes[i];
540 if (offset < min_offset) {
541 errno = ENOENT;
542 goto fail;
545 memcpy(converted+offset, insert, insertlen);
547 offset += insertlen;
548 memcpy(converted+offset, path + slashes[i],
549 pathlen - slashes[i]);
551 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
553 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
554 converted,
555 ret, ret == 0 ? "ok" : strerror(errno)));
556 if (ret == 0) {
557 /* success */
558 break;
560 if (errno == ENOTDIR) {
562 * This is a valid condition: We appended the
563 * .snaphots/@GMT.. to a file name. Just try
564 * with the upper levels.
566 continue;
568 if (errno != ENOENT) {
569 /* Other problem than "not found" */
570 goto fail;
574 if (i >= 0) {
576 * Found something
578 DEBUG(10, ("Found %s\n", converted));
579 result = converted;
580 converted = NULL;
581 } else {
582 errno = ENOENT;
584 fail:
585 saved_errno = errno;
586 TALLOC_FREE(converted);
587 TALLOC_FREE(insert);
588 TALLOC_FREE(slashes);
589 TALLOC_FREE(path);
590 errno = saved_errno;
591 return result;
595 modify a sbuf return to ensure that inodes in the shadow directory
596 are different from those in the main directory
598 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
599 SMB_STRUCT_STAT *sbuf)
601 struct shadow_copy2_config *config;
603 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
604 return);
606 if (config->fixinodes) {
607 /* some snapshot systems, like GPFS, return the name
608 device:inode for the snapshot files as the current
609 files. That breaks the 'restore' button in the shadow copy
610 GUI, as the client gets a sharing violation.
612 This is a crude way of allowing both files to be
613 open at once. It has a slight chance of inode
614 number collision, but I can't see a better approach
615 without significant VFS changes
617 uint32_t shash;
619 shash = hash(fname, strlen(fname), 0) & 0xFF000000;
620 if (shash == 0) {
621 shash = 1;
623 sbuf->st_ex_ino ^= shash;
627 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
628 const char *fname,
629 const char *mask,
630 uint32 attr)
632 time_t timestamp;
633 char *stripped;
634 DIR *ret;
635 int saved_errno;
636 char *conv;
638 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
639 &timestamp, &stripped)) {
640 return NULL;
642 if (timestamp == 0) {
643 return SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
645 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
646 TALLOC_FREE(stripped);
647 if (conv == NULL) {
648 return NULL;
650 ret = SMB_VFS_NEXT_OPENDIR(handle, conv, mask, attr);
651 saved_errno = errno;
652 TALLOC_FREE(conv);
653 errno = saved_errno;
654 return ret;
657 static int shadow_copy2_rename(vfs_handle_struct *handle,
658 const struct smb_filename *smb_fname_src,
659 const struct smb_filename *smb_fname_dst)
661 time_t timestamp_src, timestamp_dst;
663 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
664 smb_fname_src->base_name,
665 &timestamp_src, NULL)) {
666 return -1;
668 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
669 smb_fname_dst->base_name,
670 &timestamp_dst, NULL)) {
671 return -1;
673 if (timestamp_src != 0) {
674 errno = EXDEV;
675 return -1;
677 if (timestamp_dst != 0) {
678 errno = EROFS;
679 return -1;
681 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
684 static int shadow_copy2_symlink(vfs_handle_struct *handle,
685 const char *oldname, const char *newname)
687 time_t timestamp_old, timestamp_new;
689 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
690 &timestamp_old, NULL)) {
691 return -1;
693 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
694 &timestamp_new, NULL)) {
695 return -1;
697 if ((timestamp_old != 0) || (timestamp_new != 0)) {
698 errno = EROFS;
699 return -1;
701 return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
704 static int shadow_copy2_link(vfs_handle_struct *handle,
705 const char *oldname, const char *newname)
707 time_t timestamp_old, timestamp_new;
709 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
710 &timestamp_old, NULL)) {
711 return -1;
713 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
714 &timestamp_new, NULL)) {
715 return -1;
717 if ((timestamp_old != 0) || (timestamp_new != 0)) {
718 errno = EROFS;
719 return -1;
721 return SMB_VFS_NEXT_LINK(handle, oldname, newname);
724 static int shadow_copy2_stat(vfs_handle_struct *handle,
725 struct smb_filename *smb_fname)
727 time_t timestamp;
728 char *stripped, *tmp;
729 int ret, saved_errno;
731 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
732 smb_fname->base_name,
733 &timestamp, &stripped)) {
734 return -1;
736 if (timestamp == 0) {
737 return SMB_VFS_NEXT_STAT(handle, smb_fname);
740 tmp = smb_fname->base_name;
741 smb_fname->base_name = shadow_copy2_convert(
742 talloc_tos(), handle, stripped, timestamp);
743 TALLOC_FREE(stripped);
745 if (smb_fname->base_name == NULL) {
746 smb_fname->base_name = tmp;
747 return -1;
750 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
751 saved_errno = errno;
753 TALLOC_FREE(smb_fname->base_name);
754 smb_fname->base_name = tmp;
756 if (ret == 0) {
757 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
759 errno = saved_errno;
760 return ret;
763 static int shadow_copy2_lstat(vfs_handle_struct *handle,
764 struct smb_filename *smb_fname)
766 time_t timestamp;
767 char *stripped, *tmp;
768 int ret, saved_errno;
770 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
771 smb_fname->base_name,
772 &timestamp, &stripped)) {
773 return -1;
775 if (timestamp == 0) {
776 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
779 tmp = smb_fname->base_name;
780 smb_fname->base_name = shadow_copy2_convert(
781 talloc_tos(), handle, stripped, timestamp);
782 TALLOC_FREE(stripped);
784 if (smb_fname->base_name == NULL) {
785 smb_fname->base_name = tmp;
786 return -1;
789 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
790 saved_errno = errno;
792 TALLOC_FREE(smb_fname->base_name);
793 smb_fname->base_name = tmp;
795 if (ret == 0) {
796 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
798 errno = saved_errno;
799 return ret;
802 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
803 SMB_STRUCT_STAT *sbuf)
805 time_t timestamp;
806 int ret;
808 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
809 if (ret == -1) {
810 return ret;
812 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
813 fsp->fsp_name->base_name,
814 &timestamp, NULL)) {
815 return 0;
817 if (timestamp != 0) {
818 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
820 return 0;
823 static int shadow_copy2_open(vfs_handle_struct *handle,
824 struct smb_filename *smb_fname, files_struct *fsp,
825 int flags, mode_t mode)
827 time_t timestamp;
828 char *stripped, *tmp;
829 int ret, saved_errno;
831 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
832 smb_fname->base_name,
833 &timestamp, &stripped)) {
834 return -1;
836 if (timestamp == 0) {
837 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
840 tmp = smb_fname->base_name;
841 smb_fname->base_name = shadow_copy2_convert(
842 talloc_tos(), handle, stripped, timestamp);
843 TALLOC_FREE(stripped);
845 if (smb_fname->base_name == NULL) {
846 smb_fname->base_name = tmp;
847 return -1;
850 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
851 saved_errno = errno;
853 TALLOC_FREE(smb_fname->base_name);
854 smb_fname->base_name = tmp;
856 errno = saved_errno;
857 return ret;
860 static int shadow_copy2_unlink(vfs_handle_struct *handle,
861 const struct smb_filename *smb_fname)
863 time_t timestamp;
864 char *stripped;
865 int ret, saved_errno;
866 struct smb_filename *conv;
868 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
869 smb_fname->base_name,
870 &timestamp, &stripped)) {
871 return -1;
873 if (timestamp == 0) {
874 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
876 conv = cp_smb_filename(talloc_tos(), smb_fname);
877 if (conv == NULL) {
878 errno = ENOMEM;
879 return -1;
881 conv->base_name = shadow_copy2_convert(
882 conv, handle, stripped, timestamp);
883 TALLOC_FREE(stripped);
884 if (conv->base_name == NULL) {
885 return -1;
887 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
888 saved_errno = errno;
889 TALLOC_FREE(conv);
890 errno = saved_errno;
891 return ret;
894 static int shadow_copy2_chmod(vfs_handle_struct *handle, const char *fname,
895 mode_t mode)
897 time_t timestamp;
898 char *stripped;
899 int ret, saved_errno;
900 char *conv;
902 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
903 &timestamp, &stripped)) {
904 return -1;
906 if (timestamp == 0) {
907 return SMB_VFS_NEXT_CHMOD(handle, fname, mode);
909 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
910 TALLOC_FREE(stripped);
911 if (conv == NULL) {
912 return -1;
914 ret = SMB_VFS_NEXT_CHMOD(handle, conv, mode);
915 saved_errno = errno;
916 TALLOC_FREE(conv);
917 errno = saved_errno;
918 return ret;
921 static int shadow_copy2_chown(vfs_handle_struct *handle, const char *fname,
922 uid_t uid, gid_t gid)
924 time_t timestamp;
925 char *stripped;
926 int ret, saved_errno;
927 char *conv;
929 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
930 &timestamp, &stripped)) {
931 return -1;
933 if (timestamp == 0) {
934 return SMB_VFS_NEXT_CHOWN(handle, fname, uid, gid);
936 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
937 TALLOC_FREE(stripped);
938 if (conv == NULL) {
939 return -1;
941 ret = SMB_VFS_NEXT_CHOWN(handle, conv, uid, gid);
942 saved_errno = errno;
943 TALLOC_FREE(conv);
944 errno = saved_errno;
945 return ret;
948 static int shadow_copy2_chdir(vfs_handle_struct *handle,
949 const char *fname)
951 time_t timestamp;
952 char *stripped;
953 int ret, saved_errno;
954 char *conv;
956 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
957 &timestamp, &stripped)) {
958 return -1;
960 if (timestamp == 0) {
961 return SMB_VFS_NEXT_CHDIR(handle, fname);
963 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
964 TALLOC_FREE(stripped);
965 if (conv == NULL) {
966 return -1;
968 ret = SMB_VFS_NEXT_CHDIR(handle, conv);
969 saved_errno = errno;
970 TALLOC_FREE(conv);
971 errno = saved_errno;
972 return ret;
975 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
976 const struct smb_filename *smb_fname,
977 struct smb_file_time *ft)
979 time_t timestamp;
980 char *stripped;
981 int ret, saved_errno;
982 struct smb_filename *conv;
984 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
985 smb_fname->base_name,
986 &timestamp, &stripped)) {
987 return -1;
989 if (timestamp == 0) {
990 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
992 conv = cp_smb_filename(talloc_tos(), smb_fname);
993 if (conv == NULL) {
994 errno = ENOMEM;
995 return -1;
997 conv->base_name = shadow_copy2_convert(
998 conv, handle, stripped, timestamp);
999 TALLOC_FREE(stripped);
1000 if (conv->base_name == NULL) {
1001 return -1;
1003 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
1004 saved_errno = errno;
1005 TALLOC_FREE(conv);
1006 errno = saved_errno;
1007 return ret;
1010 static int shadow_copy2_readlink(vfs_handle_struct *handle,
1011 const char *fname, char *buf, size_t bufsiz)
1013 time_t timestamp;
1014 char *stripped;
1015 int ret, saved_errno;
1016 char *conv;
1018 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1019 &timestamp, &stripped)) {
1020 return -1;
1022 if (timestamp == 0) {
1023 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
1025 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1026 TALLOC_FREE(stripped);
1027 if (conv == NULL) {
1028 return -1;
1030 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1031 saved_errno = errno;
1032 TALLOC_FREE(conv);
1033 errno = saved_errno;
1034 return ret;
1037 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1038 const char *fname, mode_t mode, SMB_DEV_T dev)
1040 time_t timestamp;
1041 char *stripped;
1042 int ret, saved_errno;
1043 char *conv;
1045 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1046 &timestamp, &stripped)) {
1047 return -1;
1049 if (timestamp == 0) {
1050 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
1052 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1053 TALLOC_FREE(stripped);
1054 if (conv == NULL) {
1055 return -1;
1057 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1058 saved_errno = errno;
1059 TALLOC_FREE(conv);
1060 errno = saved_errno;
1061 return ret;
1064 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
1065 const char *fname)
1067 time_t timestamp;
1068 char *stripped = NULL;
1069 char *tmp = NULL;
1070 char *result = NULL;
1071 char *inserted = NULL;
1072 char *inserted_to, *inserted_end;
1073 int saved_errno;
1075 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1076 &timestamp, &stripped)) {
1077 goto done;
1079 if (timestamp == 0) {
1080 return SMB_VFS_NEXT_REALPATH(handle, fname);
1083 tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1084 if (tmp == NULL) {
1085 goto done;
1088 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1089 if (result == NULL) {
1090 goto done;
1094 * Take away what we've inserted. This removes the @GMT-thingy
1095 * completely, but will give a path under the share root.
1097 inserted = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
1098 if (inserted == NULL) {
1099 goto done;
1101 inserted_to = strstr_m(result, inserted);
1102 if (inserted_to == NULL) {
1103 DEBUG(2, ("SMB_VFS_NEXT_REALPATH removed %s\n", inserted));
1104 goto done;
1106 inserted_end = inserted_to + talloc_get_size(inserted) - 1;
1107 memmove(inserted_to, inserted_end, strlen(inserted_end)+1);
1109 done:
1110 saved_errno = errno;
1111 TALLOC_FREE(inserted);
1112 TALLOC_FREE(tmp);
1113 TALLOC_FREE(stripped);
1114 errno = saved_errno;
1115 return result;
1119 * Check whether a given directory contains a
1120 * snapshot directory as direct subdirectory.
1121 * If yes, return the path of the snapshot-subdir,
1122 * otherwise return NULL.
1124 static char *have_snapdir(struct vfs_handle_struct *handle,
1125 const char *path)
1127 struct smb_filename smb_fname;
1128 int ret;
1129 struct shadow_copy2_config *config;
1131 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1132 return NULL);
1134 ZERO_STRUCT(smb_fname);
1135 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1136 path, config->snapdir);
1137 if (smb_fname.base_name == NULL) {
1138 return NULL;
1141 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1142 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1143 return smb_fname.base_name;
1145 TALLOC_FREE(smb_fname.base_name);
1146 return NULL;
1150 * Find the snapshot directory (if any) for the given
1151 * filename (which is relative to the share).
1153 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1154 struct vfs_handle_struct *handle,
1155 struct smb_filename *smb_fname)
1157 char *path, *p;
1158 const char *snapdir;
1159 struct shadow_copy2_config *config;
1161 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1162 return NULL);
1165 * If the non-snapdisrseverywhere mode, we should not search!
1167 if (!config->snapdirseverywhere) {
1168 return config->snapshot_basepath;
1171 path = talloc_asprintf(mem_ctx, "%s/%s",
1172 handle->conn->connectpath,
1173 smb_fname->base_name);
1174 if (path == NULL) {
1175 return NULL;
1178 snapdir = have_snapdir(handle, path);
1179 if (snapdir != NULL) {
1180 TALLOC_FREE(path);
1181 return snapdir;
1184 while ((p = strrchr(path, '/')) && (p > path)) {
1186 p[0] = '\0';
1188 snapdir = have_snapdir(handle, path);
1189 if (snapdir != NULL) {
1190 TALLOC_FREE(path);
1191 return snapdir;
1194 TALLOC_FREE(path);
1195 return NULL;
1198 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1199 const char *name,
1200 char *gmt, size_t gmt_len)
1202 struct tm timestamp;
1203 time_t timestamp_t;
1204 unsigned long int timestamp_long;
1205 const char *fmt;
1206 struct shadow_copy2_config *config;
1208 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1209 return NULL);
1211 fmt = config->gmt_format;
1213 ZERO_STRUCT(timestamp);
1214 if (config->use_sscanf) {
1215 if (sscanf(name, fmt, &timestamp_long) != 1) {
1216 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1217 "no sscanf match %s: %s\n",
1218 fmt, name));
1219 return false;
1221 timestamp_t = timestamp_long;
1222 gmtime_r(&timestamp_t, &timestamp);
1223 } else {
1224 if (strptime(name, fmt, &timestamp) == NULL) {
1225 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1226 "no match %s: %s\n",
1227 fmt, name));
1228 return false;
1230 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1231 fmt, name));
1233 if (config->use_localtime) {
1234 timestamp.tm_isdst = -1;
1235 timestamp_t = mktime(&timestamp);
1236 gmtime_r(&timestamp_t, &timestamp);
1240 strftime(gmt, gmt_len, GMT_FORMAT, &timestamp);
1241 return true;
1244 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1246 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1249 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1251 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1255 sort the shadow copy data in ascending or descending order
1257 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1258 struct shadow_copy_data *shadow_copy2_data)
1260 int (*cmpfunc)(const void *, const void *);
1261 const char *sort;
1262 struct shadow_copy2_config *config;
1264 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1265 return);
1267 sort = config->sort_order;
1268 if (sort == NULL) {
1269 return;
1272 if (strcmp(sort, "asc") == 0) {
1273 cmpfunc = shadow_copy2_label_cmp_asc;
1274 } else if (strcmp(sort, "desc") == 0) {
1275 cmpfunc = shadow_copy2_label_cmp_desc;
1276 } else {
1277 return;
1280 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1281 shadow_copy2_data->labels)
1283 TYPESAFE_QSORT(shadow_copy2_data->labels,
1284 shadow_copy2_data->num_volumes,
1285 cmpfunc);
1289 static int shadow_copy2_get_shadow_copy_data(
1290 vfs_handle_struct *handle, files_struct *fsp,
1291 struct shadow_copy_data *shadow_copy2_data,
1292 bool labels)
1294 DIR *p;
1295 const char *snapdir;
1296 struct dirent *d;
1297 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1299 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1300 if (snapdir == NULL) {
1301 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1302 handle->conn->connectpath));
1303 errno = EINVAL;
1304 talloc_free(tmp_ctx);
1305 return -1;
1308 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir, NULL, 0);
1310 if (!p) {
1311 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1312 " - %s\n", snapdir, strerror(errno)));
1313 talloc_free(tmp_ctx);
1314 errno = ENOSYS;
1315 return -1;
1318 shadow_copy2_data->num_volumes = 0;
1319 shadow_copy2_data->labels = NULL;
1321 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
1322 char snapshot[GMT_NAME_LEN+1];
1323 SHADOW_COPY_LABEL *tlabels;
1326 * ignore names not of the right form in the snapshot
1327 * directory
1329 if (!shadow_copy2_snapshot_to_gmt(
1330 handle, d->d_name,
1331 snapshot, sizeof(snapshot))) {
1333 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1334 "ignoring %s\n", d->d_name));
1335 continue;
1337 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1338 d->d_name, snapshot));
1340 if (!labels) {
1341 /* the caller doesn't want the labels */
1342 shadow_copy2_data->num_volumes++;
1343 continue;
1346 tlabels = talloc_realloc(shadow_copy2_data,
1347 shadow_copy2_data->labels,
1348 SHADOW_COPY_LABEL,
1349 shadow_copy2_data->num_volumes+1);
1350 if (tlabels == NULL) {
1351 DEBUG(0,("shadow_copy2: out of memory\n"));
1352 SMB_VFS_NEXT_CLOSEDIR(handle, p);
1353 talloc_free(tmp_ctx);
1354 return -1;
1357 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
1358 sizeof(*tlabels));
1360 shadow_copy2_data->num_volumes++;
1361 shadow_copy2_data->labels = tlabels;
1364 SMB_VFS_NEXT_CLOSEDIR(handle,p);
1366 shadow_copy2_sort_data(handle, shadow_copy2_data);
1368 talloc_free(tmp_ctx);
1369 return 0;
1372 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
1373 struct files_struct *fsp,
1374 uint32 security_info,
1375 TALLOC_CTX *mem_ctx,
1376 struct security_descriptor **ppdesc)
1378 time_t timestamp;
1379 char *stripped;
1380 NTSTATUS status;
1381 char *conv;
1383 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1384 fsp->fsp_name->base_name,
1385 &timestamp, &stripped)) {
1386 return map_nt_error_from_unix(errno);
1388 if (timestamp == 0) {
1389 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1390 mem_ctx,
1391 ppdesc);
1393 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1394 TALLOC_FREE(stripped);
1395 if (conv == NULL) {
1396 return map_nt_error_from_unix(errno);
1398 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
1399 mem_ctx, ppdesc);
1400 TALLOC_FREE(conv);
1401 return status;
1404 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
1405 const char *fname,
1406 uint32 security_info,
1407 TALLOC_CTX *mem_ctx,
1408 struct security_descriptor **ppdesc)
1410 time_t timestamp;
1411 char *stripped;
1412 NTSTATUS status;
1413 char *conv;
1415 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1416 &timestamp, &stripped)) {
1417 return map_nt_error_from_unix(errno);
1419 if (timestamp == 0) {
1420 return SMB_VFS_NEXT_GET_NT_ACL(handle, fname, security_info,
1421 mem_ctx, ppdesc);
1423 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1424 TALLOC_FREE(stripped);
1425 if (conv == NULL) {
1426 return map_nt_error_from_unix(errno);
1428 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
1429 mem_ctx, ppdesc);
1430 TALLOC_FREE(conv);
1431 return status;
1434 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
1435 const char *fname, mode_t mode)
1437 time_t timestamp;
1438 char *stripped;
1439 int ret, saved_errno;
1440 char *conv;
1442 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1443 &timestamp, &stripped)) {
1444 return -1;
1446 if (timestamp == 0) {
1447 return SMB_VFS_NEXT_MKDIR(handle, fname, mode);
1449 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1450 TALLOC_FREE(stripped);
1451 if (conv == NULL) {
1452 return -1;
1454 ret = SMB_VFS_NEXT_MKDIR(handle, conv, mode);
1455 saved_errno = errno;
1456 TALLOC_FREE(conv);
1457 errno = saved_errno;
1458 return ret;
1461 static int shadow_copy2_rmdir(vfs_handle_struct *handle, const char *fname)
1463 time_t timestamp;
1464 char *stripped;
1465 int ret, saved_errno;
1466 char *conv;
1468 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1469 &timestamp, &stripped)) {
1470 return -1;
1472 if (timestamp == 0) {
1473 return SMB_VFS_NEXT_RMDIR(handle, fname);
1475 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1476 TALLOC_FREE(stripped);
1477 if (conv == NULL) {
1478 return -1;
1480 ret = SMB_VFS_NEXT_RMDIR(handle, conv);
1481 saved_errno = errno;
1482 TALLOC_FREE(conv);
1483 errno = saved_errno;
1484 return ret;
1487 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
1488 unsigned int flags)
1490 time_t timestamp;
1491 char *stripped;
1492 int ret, saved_errno;
1493 char *conv;
1495 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1496 &timestamp, &stripped)) {
1497 return -1;
1499 if (timestamp == 0) {
1500 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
1502 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1503 TALLOC_FREE(stripped);
1504 if (conv == NULL) {
1505 return -1;
1507 ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
1508 saved_errno = errno;
1509 TALLOC_FREE(conv);
1510 errno = saved_errno;
1511 return ret;
1514 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
1515 const char *fname, const char *aname,
1516 void *value, size_t size)
1518 time_t timestamp;
1519 char *stripped;
1520 ssize_t ret;
1521 int saved_errno;
1522 char *conv;
1524 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1525 &timestamp, &stripped)) {
1526 return -1;
1528 if (timestamp == 0) {
1529 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
1530 size);
1532 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1533 TALLOC_FREE(stripped);
1534 if (conv == NULL) {
1535 return -1;
1537 ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
1538 saved_errno = errno;
1539 TALLOC_FREE(conv);
1540 errno = saved_errno;
1541 return ret;
1544 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
1545 const char *fname,
1546 char *list, size_t size)
1548 time_t timestamp;
1549 char *stripped;
1550 ssize_t ret;
1551 int saved_errno;
1552 char *conv;
1554 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1555 &timestamp, &stripped)) {
1556 return -1;
1558 if (timestamp == 0) {
1559 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
1561 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1562 TALLOC_FREE(stripped);
1563 if (conv == NULL) {
1564 return -1;
1566 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
1567 saved_errno = errno;
1568 TALLOC_FREE(conv);
1569 errno = saved_errno;
1570 return ret;
1573 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
1574 const char *fname, const char *aname)
1576 time_t timestamp;
1577 char *stripped;
1578 int ret, saved_errno;
1579 char *conv;
1581 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1582 &timestamp, &stripped)) {
1583 return -1;
1585 if (timestamp == 0) {
1586 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
1588 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1589 TALLOC_FREE(stripped);
1590 if (conv == NULL) {
1591 return -1;
1593 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
1594 saved_errno = errno;
1595 TALLOC_FREE(conv);
1596 errno = saved_errno;
1597 return ret;
1600 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
1601 const char *fname,
1602 const char *aname, const void *value,
1603 size_t size, int flags)
1605 time_t timestamp;
1606 char *stripped;
1607 ssize_t ret;
1608 int saved_errno;
1609 char *conv;
1611 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1612 &timestamp, &stripped)) {
1613 return -1;
1615 if (timestamp == 0) {
1616 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
1617 flags);
1619 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1620 TALLOC_FREE(stripped);
1621 if (conv == NULL) {
1622 return -1;
1624 ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
1625 saved_errno = errno;
1626 TALLOC_FREE(conv);
1627 errno = saved_errno;
1628 return ret;
1631 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
1632 const char *fname, mode_t mode)
1634 time_t timestamp;
1635 char *stripped;
1636 ssize_t ret;
1637 int saved_errno;
1638 char *conv;
1640 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1641 &timestamp, &stripped)) {
1642 return -1;
1644 if (timestamp == 0) {
1645 return SMB_VFS_NEXT_CHMOD_ACL(handle, fname, mode);
1647 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1648 TALLOC_FREE(stripped);
1649 if (conv == NULL) {
1650 return -1;
1652 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv, mode);
1653 saved_errno = errno;
1654 TALLOC_FREE(conv);
1655 errno = saved_errno;
1656 return ret;
1659 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
1660 const char *path,
1661 const char *name,
1662 TALLOC_CTX *mem_ctx,
1663 char **found_name)
1665 time_t timestamp;
1666 char *stripped;
1667 ssize_t ret;
1668 int saved_errno;
1669 char *conv;
1671 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1672 &timestamp, &stripped)) {
1673 return -1;
1675 if (timestamp == 0) {
1676 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
1677 mem_ctx, found_name);
1679 if (stripped[0] == '\0') {
1680 *found_name = talloc_strdup(mem_ctx, name);
1681 if (*found_name == NULL) {
1682 errno = ENOMEM;
1683 return -1;
1685 return 0;
1687 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1688 TALLOC_FREE(stripped);
1689 if (conv == NULL) {
1690 return -1;
1692 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
1693 mem_ctx, found_name);
1694 saved_errno = errno;
1695 TALLOC_FREE(conv);
1696 errno = saved_errno;
1697 return ret;
1700 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
1701 const char *path, bool small_query,
1702 uint64_t *bsize, uint64_t *dfree,
1703 uint64_t *dsize)
1705 time_t timestamp;
1706 char *stripped;
1707 ssize_t ret;
1708 int saved_errno;
1709 char *conv;
1711 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1712 &timestamp, &stripped)) {
1713 return -1;
1715 if (timestamp == 0) {
1716 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1717 bsize, dfree, dsize);
1720 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1721 TALLOC_FREE(stripped);
1722 if (conv == NULL) {
1723 return -1;
1726 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, small_query, bsize, dfree,
1727 dsize);
1729 saved_errno = errno;
1730 TALLOC_FREE(conv);
1731 errno = saved_errno;
1733 return ret;
1736 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
1737 const char *service, const char *user)
1739 struct shadow_copy2_config *config;
1740 int ret;
1741 const char *snapdir;
1742 const char *gmt_format;
1743 const char *sort_order;
1744 const char *basedir;
1745 const char *mount_point;
1747 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
1748 (unsigned)handle->conn->cnum,
1749 handle->conn->connectpath));
1751 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1752 if (ret < 0) {
1753 return ret;
1756 config = talloc_zero(handle->conn, struct shadow_copy2_config);
1757 if (config == NULL) {
1758 DEBUG(0, ("talloc_zero() failed\n"));
1759 errno = ENOMEM;
1760 return -1;
1763 gmt_format = lp_parm_const_string(SNUM(handle->conn),
1764 "shadow", "format",
1765 GMT_FORMAT);
1766 config->gmt_format = talloc_strdup(config, gmt_format);
1767 if (config->gmt_format == NULL) {
1768 DEBUG(0, ("talloc_strdup() failed\n"));
1769 errno = ENOMEM;
1770 return -1;
1773 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
1774 "shadow", "sscanf", false);
1776 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
1777 "shadow", "localtime",
1778 false);
1780 snapdir = lp_parm_const_string(SNUM(handle->conn),
1781 "shadow", "snapdir",
1782 ".snapshots");
1783 config->snapdir = talloc_strdup(config, snapdir);
1784 if (config->snapdir == NULL) {
1785 DEBUG(0, ("talloc_strdup() failed\n"));
1786 errno = ENOMEM;
1787 return -1;
1790 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
1791 "shadow",
1792 "snapdirseverywhere",
1793 false);
1795 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
1796 "shadow", "crossmountpoints",
1797 false);
1799 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
1800 "shadow", "fixinodes",
1801 false);
1803 sort_order = lp_parm_const_string(SNUM(handle->conn),
1804 "shadow", "sort", "desc");
1805 config->sort_order = talloc_strdup(config, sort_order);
1806 if (config->sort_order == NULL) {
1807 DEBUG(0, ("talloc_strdup() failed\n"));
1808 errno = ENOMEM;
1809 return -1;
1812 mount_point = lp_parm_const_string(SNUM(handle->conn),
1813 "shadow", "mountpoint", NULL);
1814 if (mount_point != NULL) {
1815 if (mount_point[0] != '/') {
1816 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
1817 "relative ('%s'), but it has to be an "
1818 "absolute path. Ignoring provided value.\n",
1819 mount_point));
1820 mount_point = NULL;
1821 } else {
1822 char *p;
1823 p = strstr(handle->conn->connectpath, mount_point);
1824 if (p != handle->conn->connectpath) {
1825 DEBUG(1, ("Warning: mount_point (%s) is not a "
1826 "subdirectory of the share root "
1827 "(%s). Ignoring provided value.\n",
1828 mount_point,
1829 handle->conn->connectpath));
1830 mount_point = NULL;
1835 if (mount_point != NULL) {
1836 config->mount_point = talloc_strdup(config, mount_point);
1837 if (config->mount_point == NULL) {
1838 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
1839 return -1;
1841 } else {
1842 config->mount_point = shadow_copy2_find_mount_point(config,
1843 handle);
1844 if (config->mount_point == NULL) {
1845 DEBUG(0, (__location__ ": shadow_copy2_find_mount_point"
1846 " failed: %s\n", strerror(errno)));
1847 return -1;
1851 basedir = lp_parm_const_string(SNUM(handle->conn),
1852 "shadow", "basedir", NULL);
1854 if (basedir != NULL) {
1855 if (basedir[0] != '/') {
1856 DEBUG(1, (__location__ " Warning: 'basedir' is "
1857 "relative ('%s'), but it has to be an "
1858 "absolute path. Disabling basedir.\n",
1859 basedir));
1860 } else {
1861 char *p;
1862 p = strstr(basedir, config->mount_point);
1863 if (p != basedir) {
1864 DEBUG(1, ("Warning: basedir (%s) is not a "
1865 "subdirectory of the share root's "
1866 "mount point (%s). "
1867 "Disabling basedir\n",
1868 basedir, config->mount_point));
1869 } else {
1870 config->basedir = talloc_strdup(config,
1871 basedir);
1872 if (config->basedir == NULL) {
1873 DEBUG(0, ("talloc_strdup() failed\n"));
1874 errno = ENOMEM;
1875 return -1;
1881 if (config->snapdirseverywhere && config->basedir != NULL) {
1882 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
1883 "with 'snapdirseverywhere'. Disabling basedir.\n"));
1884 TALLOC_FREE(config->basedir);
1887 if (config->crossmountpoints && config->basedir != NULL) {
1888 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
1889 "with 'crossmountpoints'. Disabling basedir.\n"));
1890 TALLOC_FREE(config->basedir);
1893 if (config->basedir == NULL) {
1894 config->basedir = config->mount_point;
1897 if (strlen(config->basedir) != strlen(handle->conn->connectpath)) {
1898 config->rel_connectpath = talloc_strdup(config,
1899 handle->conn->connectpath + strlen(config->basedir));
1900 if (config->rel_connectpath == NULL) {
1901 DEBUG(0, ("talloc_strdup() failed\n"));
1902 errno = ENOMEM;
1903 return -1;
1907 if (config->snapdir[0] == '/') {
1908 config->snapdir_absolute = true;
1910 if (config->snapdirseverywhere == true) {
1911 DEBUG(1, (__location__ " Warning: An absolute snapdir "
1912 "is incompatible with 'snapdirseverywhere', "
1913 "setting 'snapdirseverywhere' to false.\n"));
1914 config->snapdirseverywhere = false;
1917 if (config->crossmountpoints == true) {
1918 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
1919 "is not supported with an absolute snapdir. "
1920 "Disabling it.\n"));
1921 config->crossmountpoints = false;
1924 config->snapshot_basepath = config->snapdir;
1925 } else {
1926 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
1927 config->mount_point, config->snapdir);
1928 if (config->snapshot_basepath == NULL) {
1929 DEBUG(0, ("talloc_asprintf() failed\n"));
1930 errno = ENOMEM;
1931 return -1;
1935 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
1936 " share root: '%s'\n"
1937 " basedir: '%s'\n"
1938 " mountpoint: '%s'\n"
1939 " rel share root: '%s'\n"
1940 " snapdir: '%s'\n"
1941 " snapshot base path: '%s'\n"
1942 " format: '%s'\n"
1943 " use sscanf: %s\n"
1944 " snapdirs everywhere: %s\n"
1945 " cross mountpoints: %s\n"
1946 " fix inodes: %s\n"
1947 " sort order: %s\n"
1949 handle->conn->connectpath,
1950 config->basedir,
1951 config->mount_point,
1952 config->rel_connectpath,
1953 config->snapdir,
1954 config->snapshot_basepath,
1955 config->gmt_format,
1956 config->use_sscanf ? "yes" : "no",
1957 config->snapdirseverywhere ? "yes" : "no",
1958 config->crossmountpoints ? "yes" : "no",
1959 config->fixinodes ? "yes" : "no",
1960 config->sort_order
1964 SMB_VFS_HANDLE_SET_DATA(handle, config,
1965 NULL, struct shadow_copy2_config,
1966 return -1);
1968 return 0;
1971 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
1972 .connect_fn = shadow_copy2_connect,
1973 .opendir_fn = shadow_copy2_opendir,
1974 .disk_free_fn = shadow_copy2_disk_free,
1975 .rename_fn = shadow_copy2_rename,
1976 .link_fn = shadow_copy2_link,
1977 .symlink_fn = shadow_copy2_symlink,
1978 .stat_fn = shadow_copy2_stat,
1979 .lstat_fn = shadow_copy2_lstat,
1980 .fstat_fn = shadow_copy2_fstat,
1981 .open_fn = shadow_copy2_open,
1982 .unlink_fn = shadow_copy2_unlink,
1983 .chmod_fn = shadow_copy2_chmod,
1984 .chown_fn = shadow_copy2_chown,
1985 .chdir_fn = shadow_copy2_chdir,
1986 .ntimes_fn = shadow_copy2_ntimes,
1987 .readlink_fn = shadow_copy2_readlink,
1988 .mknod_fn = shadow_copy2_mknod,
1989 .realpath_fn = shadow_copy2_realpath,
1990 .get_nt_acl_fn = shadow_copy2_get_nt_acl,
1991 .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
1992 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
1993 .mkdir_fn = shadow_copy2_mkdir,
1994 .rmdir_fn = shadow_copy2_rmdir,
1995 .getxattr_fn = shadow_copy2_getxattr,
1996 .listxattr_fn = shadow_copy2_listxattr,
1997 .removexattr_fn = shadow_copy2_removexattr,
1998 .setxattr_fn = shadow_copy2_setxattr,
1999 .chmod_acl_fn = shadow_copy2_chmod_acl,
2000 .chflags_fn = shadow_copy2_chflags,
2001 .get_real_filename_fn = shadow_copy2_get_real_filename,
2004 NTSTATUS vfs_shadow_copy2_init(void);
2005 NTSTATUS vfs_shadow_copy2_init(void)
2007 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2008 "shadow_copy2", &vfs_shadow_copy2_fns);