shadow_copy2: add a comment explaining why we don't talloc_zero_array().
[Samba.git] / source3 / modules / vfs_shadow_copy2.c
blob8964c22ac1c1fc0c7c7d0a992243c539764ed5f2
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;
486 * Note: We deliberatly don't expensively initialize the
487 * array with talloc_zero here: Putting zero into
488 * converted[pathlen+insertlen] below is sufficient, because
489 * in the following for loop, the insert string is inserted
490 * at various slash places. So the memory up to position
491 * pathlen+insertlen will always be initialized when the
492 * converted string is used.
494 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
495 if (converted == NULL) {
496 goto fail;
499 if (path[pathlen-1] != '/') {
501 * Append a fake slash to find the snapshot root
503 size_t *tmp;
504 tmp = talloc_realloc(talloc_tos(), slashes,
505 size_t, num_slashes+1);
506 if (tmp == NULL) {
507 goto fail;
509 slashes = tmp;
510 slashes[num_slashes] = pathlen;
511 num_slashes += 1;
514 min_offset = 0;
516 if (!config->crossmountpoints) {
517 min_offset = strlen(config->mount_point);
520 memcpy(converted, path, pathlen+1);
521 converted[pathlen+insertlen] = '\0';
523 ZERO_STRUCT(converted_fname);
524 converted_fname.base_name = converted;
526 for (i = num_slashes-1; i>=0; i--) {
527 int ret;
528 size_t offset;
530 offset = slashes[i];
532 if (offset < min_offset) {
533 errno = ENOENT;
534 goto fail;
537 memcpy(converted+offset, insert, insertlen);
539 offset += insertlen;
540 memcpy(converted+offset, path + slashes[i],
541 pathlen - slashes[i]);
543 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
545 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
546 converted,
547 ret, ret == 0 ? "ok" : strerror(errno)));
548 if (ret == 0) {
549 /* success */
550 break;
552 if (errno == ENOTDIR) {
554 * This is a valid condition: We appended the
555 * .snaphots/@GMT.. to a file name. Just try
556 * with the upper levels.
558 continue;
560 if (errno != ENOENT) {
561 /* Other problem than "not found" */
562 goto fail;
566 if (i >= 0) {
568 * Found something
570 DEBUG(10, ("Found %s\n", converted));
571 result = converted;
572 converted = NULL;
573 } else {
574 errno = ENOENT;
576 fail:
577 saved_errno = errno;
578 TALLOC_FREE(converted);
579 TALLOC_FREE(insert);
580 TALLOC_FREE(slashes);
581 TALLOC_FREE(path);
582 errno = saved_errno;
583 return result;
587 modify a sbuf return to ensure that inodes in the shadow directory
588 are different from those in the main directory
590 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
591 SMB_STRUCT_STAT *sbuf)
593 struct shadow_copy2_config *config;
595 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
596 return);
598 if (config->fixinodes) {
599 /* some snapshot systems, like GPFS, return the name
600 device:inode for the snapshot files as the current
601 files. That breaks the 'restore' button in the shadow copy
602 GUI, as the client gets a sharing violation.
604 This is a crude way of allowing both files to be
605 open at once. It has a slight chance of inode
606 number collision, but I can't see a better approach
607 without significant VFS changes
609 uint32_t shash;
611 shash = hash(fname, strlen(fname), 0) & 0xFF000000;
612 if (shash == 0) {
613 shash = 1;
615 sbuf->st_ex_ino ^= shash;
619 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
620 const char *fname,
621 const char *mask,
622 uint32 attr)
624 time_t timestamp;
625 char *stripped;
626 DIR *ret;
627 int saved_errno;
628 char *conv;
630 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
631 &timestamp, &stripped)) {
632 return NULL;
634 if (timestamp == 0) {
635 return SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
637 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
638 TALLOC_FREE(stripped);
639 if (conv == NULL) {
640 return NULL;
642 ret = SMB_VFS_NEXT_OPENDIR(handle, conv, mask, attr);
643 saved_errno = errno;
644 TALLOC_FREE(conv);
645 errno = saved_errno;
646 return ret;
649 static int shadow_copy2_rename(vfs_handle_struct *handle,
650 const struct smb_filename *smb_fname_src,
651 const struct smb_filename *smb_fname_dst)
653 time_t timestamp_src, timestamp_dst;
655 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
656 smb_fname_src->base_name,
657 &timestamp_src, NULL)) {
658 return -1;
660 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
661 smb_fname_dst->base_name,
662 &timestamp_dst, NULL)) {
663 return -1;
665 if (timestamp_src != 0) {
666 errno = EXDEV;
667 return -1;
669 if (timestamp_dst != 0) {
670 errno = EROFS;
671 return -1;
673 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
676 static int shadow_copy2_symlink(vfs_handle_struct *handle,
677 const char *oldname, const char *newname)
679 time_t timestamp_old, timestamp_new;
681 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
682 &timestamp_old, NULL)) {
683 return -1;
685 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
686 &timestamp_new, NULL)) {
687 return -1;
689 if ((timestamp_old != 0) || (timestamp_new != 0)) {
690 errno = EROFS;
691 return -1;
693 return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
696 static int shadow_copy2_link(vfs_handle_struct *handle,
697 const char *oldname, const char *newname)
699 time_t timestamp_old, timestamp_new;
701 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
702 &timestamp_old, NULL)) {
703 return -1;
705 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
706 &timestamp_new, NULL)) {
707 return -1;
709 if ((timestamp_old != 0) || (timestamp_new != 0)) {
710 errno = EROFS;
711 return -1;
713 return SMB_VFS_NEXT_LINK(handle, oldname, newname);
716 static int shadow_copy2_stat(vfs_handle_struct *handle,
717 struct smb_filename *smb_fname)
719 time_t timestamp;
720 char *stripped, *tmp;
721 int ret, saved_errno;
723 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
724 smb_fname->base_name,
725 &timestamp, &stripped)) {
726 return -1;
728 if (timestamp == 0) {
729 return SMB_VFS_NEXT_STAT(handle, smb_fname);
732 tmp = smb_fname->base_name;
733 smb_fname->base_name = shadow_copy2_convert(
734 talloc_tos(), handle, stripped, timestamp);
735 TALLOC_FREE(stripped);
737 if (smb_fname->base_name == NULL) {
738 smb_fname->base_name = tmp;
739 return -1;
742 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
743 saved_errno = errno;
745 TALLOC_FREE(smb_fname->base_name);
746 smb_fname->base_name = tmp;
748 if (ret == 0) {
749 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
751 errno = saved_errno;
752 return ret;
755 static int shadow_copy2_lstat(vfs_handle_struct *handle,
756 struct smb_filename *smb_fname)
758 time_t timestamp;
759 char *stripped, *tmp;
760 int ret, saved_errno;
762 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
763 smb_fname->base_name,
764 &timestamp, &stripped)) {
765 return -1;
767 if (timestamp == 0) {
768 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
771 tmp = smb_fname->base_name;
772 smb_fname->base_name = shadow_copy2_convert(
773 talloc_tos(), handle, stripped, timestamp);
774 TALLOC_FREE(stripped);
776 if (smb_fname->base_name == NULL) {
777 smb_fname->base_name = tmp;
778 return -1;
781 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
782 saved_errno = errno;
784 TALLOC_FREE(smb_fname->base_name);
785 smb_fname->base_name = tmp;
787 if (ret == 0) {
788 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
790 errno = saved_errno;
791 return ret;
794 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
795 SMB_STRUCT_STAT *sbuf)
797 time_t timestamp;
798 int ret;
800 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
801 if (ret == -1) {
802 return ret;
804 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
805 fsp->fsp_name->base_name,
806 &timestamp, NULL)) {
807 return 0;
809 if (timestamp != 0) {
810 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
812 return 0;
815 static int shadow_copy2_open(vfs_handle_struct *handle,
816 struct smb_filename *smb_fname, files_struct *fsp,
817 int flags, mode_t mode)
819 time_t timestamp;
820 char *stripped, *tmp;
821 int ret, saved_errno;
823 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
824 smb_fname->base_name,
825 &timestamp, &stripped)) {
826 return -1;
828 if (timestamp == 0) {
829 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
832 tmp = smb_fname->base_name;
833 smb_fname->base_name = shadow_copy2_convert(
834 talloc_tos(), handle, stripped, timestamp);
835 TALLOC_FREE(stripped);
837 if (smb_fname->base_name == NULL) {
838 smb_fname->base_name = tmp;
839 return -1;
842 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
843 saved_errno = errno;
845 TALLOC_FREE(smb_fname->base_name);
846 smb_fname->base_name = tmp;
848 errno = saved_errno;
849 return ret;
852 static int shadow_copy2_unlink(vfs_handle_struct *handle,
853 const struct smb_filename *smb_fname)
855 time_t timestamp;
856 char *stripped;
857 int ret, saved_errno;
858 struct smb_filename *conv;
859 NTSTATUS status;
861 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
862 smb_fname->base_name,
863 &timestamp, &stripped)) {
864 return -1;
866 if (timestamp == 0) {
867 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
869 status = copy_smb_filename(talloc_tos(), smb_fname, &conv);
870 if (!NT_STATUS_IS_OK(status)) {
871 errno = ENOMEM;
872 return -1;
874 conv->base_name = shadow_copy2_convert(
875 conv, handle, stripped, timestamp);
876 TALLOC_FREE(stripped);
877 if (conv->base_name == NULL) {
878 return -1;
880 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
881 saved_errno = errno;
882 TALLOC_FREE(conv);
883 errno = saved_errno;
884 return ret;
887 static int shadow_copy2_chmod(vfs_handle_struct *handle, const char *fname,
888 mode_t mode)
890 time_t timestamp;
891 char *stripped;
892 int ret, saved_errno;
893 char *conv;
895 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
896 &timestamp, &stripped)) {
897 return -1;
899 if (timestamp == 0) {
900 return SMB_VFS_NEXT_CHMOD(handle, fname, mode);
902 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
903 TALLOC_FREE(stripped);
904 if (conv == NULL) {
905 return -1;
907 ret = SMB_VFS_NEXT_CHMOD(handle, conv, mode);
908 saved_errno = errno;
909 TALLOC_FREE(conv);
910 errno = saved_errno;
911 return ret;
914 static int shadow_copy2_chown(vfs_handle_struct *handle, const char *fname,
915 uid_t uid, gid_t gid)
917 time_t timestamp;
918 char *stripped;
919 int ret, saved_errno;
920 char *conv;
922 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
923 &timestamp, &stripped)) {
924 return -1;
926 if (timestamp == 0) {
927 return SMB_VFS_NEXT_CHOWN(handle, fname, uid, gid);
929 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
930 TALLOC_FREE(stripped);
931 if (conv == NULL) {
932 return -1;
934 ret = SMB_VFS_NEXT_CHOWN(handle, conv, uid, gid);
935 saved_errno = errno;
936 TALLOC_FREE(conv);
937 errno = saved_errno;
938 return ret;
941 static int shadow_copy2_chdir(vfs_handle_struct *handle,
942 const char *fname)
944 time_t timestamp;
945 char *stripped;
946 int ret, saved_errno;
947 char *conv;
949 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
950 &timestamp, &stripped)) {
951 return -1;
953 if (timestamp == 0) {
954 return SMB_VFS_NEXT_CHDIR(handle, fname);
956 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
957 TALLOC_FREE(stripped);
958 if (conv == NULL) {
959 return -1;
961 ret = SMB_VFS_NEXT_CHDIR(handle, conv);
962 saved_errno = errno;
963 TALLOC_FREE(conv);
964 errno = saved_errno;
965 return ret;
968 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
969 const struct smb_filename *smb_fname,
970 struct smb_file_time *ft)
972 time_t timestamp;
973 char *stripped;
974 int ret, saved_errno;
975 struct smb_filename *conv;
976 NTSTATUS status;
978 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
979 smb_fname->base_name,
980 &timestamp, &stripped)) {
981 return -1;
983 if (timestamp == 0) {
984 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
986 status = copy_smb_filename(talloc_tos(), smb_fname, &conv);
987 if (!NT_STATUS_IS_OK(status)) {
988 errno = ENOMEM;
989 return -1;
991 conv->base_name = shadow_copy2_convert(
992 conv, handle, stripped, timestamp);
993 TALLOC_FREE(stripped);
994 if (conv->base_name == NULL) {
995 return -1;
997 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
998 saved_errno = errno;
999 TALLOC_FREE(conv);
1000 errno = saved_errno;
1001 return ret;
1004 static int shadow_copy2_readlink(vfs_handle_struct *handle,
1005 const char *fname, char *buf, size_t bufsiz)
1007 time_t timestamp;
1008 char *stripped;
1009 int ret, saved_errno;
1010 char *conv;
1012 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1013 &timestamp, &stripped)) {
1014 return -1;
1016 if (timestamp == 0) {
1017 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
1019 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1020 TALLOC_FREE(stripped);
1021 if (conv == NULL) {
1022 return -1;
1024 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1025 saved_errno = errno;
1026 TALLOC_FREE(conv);
1027 errno = saved_errno;
1028 return ret;
1031 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1032 const char *fname, mode_t mode, SMB_DEV_T dev)
1034 time_t timestamp;
1035 char *stripped;
1036 int ret, saved_errno;
1037 char *conv;
1039 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1040 &timestamp, &stripped)) {
1041 return -1;
1043 if (timestamp == 0) {
1044 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
1046 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1047 TALLOC_FREE(stripped);
1048 if (conv == NULL) {
1049 return -1;
1051 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1052 saved_errno = errno;
1053 TALLOC_FREE(conv);
1054 errno = saved_errno;
1055 return ret;
1058 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
1059 const char *fname)
1061 time_t timestamp;
1062 char *stripped = NULL;
1063 char *tmp = NULL;
1064 char *result = NULL;
1065 char *inserted = NULL;
1066 char *inserted_to, *inserted_end;
1067 int saved_errno;
1069 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1070 &timestamp, &stripped)) {
1071 goto done;
1073 if (timestamp == 0) {
1074 return SMB_VFS_NEXT_REALPATH(handle, fname);
1077 tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1078 if (tmp == NULL) {
1079 goto done;
1082 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1083 if (result == NULL) {
1084 goto done;
1088 * Take away what we've inserted. This removes the @GMT-thingy
1089 * completely, but will give a path under the share root.
1091 inserted = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
1092 if (inserted == NULL) {
1093 goto done;
1095 inserted_to = strstr_m(result, inserted);
1096 if (inserted_to == NULL) {
1097 DEBUG(2, ("SMB_VFS_NEXT_REALPATH removed %s\n", inserted));
1098 goto done;
1100 inserted_end = inserted_to + talloc_get_size(inserted) - 1;
1101 memmove(inserted_to, inserted_end, strlen(inserted_end)+1);
1103 done:
1104 saved_errno = errno;
1105 TALLOC_FREE(inserted);
1106 TALLOC_FREE(tmp);
1107 TALLOC_FREE(stripped);
1108 errno = saved_errno;
1109 return result;
1113 * Check whether a given directory contains a
1114 * snapshot directory as direct subdirectory.
1115 * If yes, return the path of the snapshot-subdir,
1116 * otherwise return NULL.
1118 static char *have_snapdir(struct vfs_handle_struct *handle,
1119 const char *path)
1121 struct smb_filename smb_fname;
1122 int ret;
1123 struct shadow_copy2_config *config;
1125 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1126 return NULL);
1128 ZERO_STRUCT(smb_fname);
1129 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1130 path, config->snapdir);
1131 if (smb_fname.base_name == NULL) {
1132 return NULL;
1135 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1136 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1137 return smb_fname.base_name;
1139 TALLOC_FREE(smb_fname.base_name);
1140 return NULL;
1144 * Find the snapshot directory (if any) for the given
1145 * filename (which is relative to the share).
1147 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1148 struct vfs_handle_struct *handle,
1149 struct smb_filename *smb_fname)
1151 char *path, *p;
1152 const char *snapdir;
1153 struct shadow_copy2_config *config;
1155 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1156 return NULL);
1159 * If the non-snapdisrseverywhere mode, we should not search!
1161 if (!config->snapdirseverywhere) {
1162 return config->snapshot_basepath;
1165 path = talloc_asprintf(mem_ctx, "%s/%s",
1166 handle->conn->connectpath,
1167 smb_fname->base_name);
1168 if (path == NULL) {
1169 return NULL;
1172 snapdir = have_snapdir(handle, path);
1173 if (snapdir != NULL) {
1174 TALLOC_FREE(path);
1175 return snapdir;
1178 while ((p = strrchr(path, '/')) && (p > path)) {
1180 p[0] = '\0';
1182 snapdir = have_snapdir(handle, path);
1183 if (snapdir != NULL) {
1184 TALLOC_FREE(path);
1185 return snapdir;
1188 TALLOC_FREE(path);
1189 return NULL;
1192 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1193 const char *name,
1194 char *gmt, size_t gmt_len)
1196 struct tm timestamp;
1197 time_t timestamp_t;
1198 unsigned long int timestamp_long;
1199 const char *fmt;
1200 struct shadow_copy2_config *config;
1202 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1203 return NULL);
1205 fmt = config->gmt_format;
1207 ZERO_STRUCT(timestamp);
1208 if (config->use_sscanf) {
1209 if (sscanf(name, fmt, &timestamp_long) != 1) {
1210 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1211 "no sscanf match %s: %s\n",
1212 fmt, name));
1213 return false;
1215 timestamp_t = timestamp_long;
1216 gmtime_r(&timestamp_t, &timestamp);
1217 } else {
1218 if (strptime(name, fmt, &timestamp) == NULL) {
1219 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1220 "no match %s: %s\n",
1221 fmt, name));
1222 return false;
1224 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1225 fmt, name));
1227 if (config->use_localtime) {
1228 timestamp.tm_isdst = -1;
1229 timestamp_t = mktime(&timestamp);
1230 gmtime_r(&timestamp_t, &timestamp);
1234 strftime(gmt, gmt_len, GMT_FORMAT, &timestamp);
1235 return true;
1238 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1240 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1243 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1245 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1249 sort the shadow copy data in ascending or descending order
1251 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1252 struct shadow_copy_data *shadow_copy2_data)
1254 int (*cmpfunc)(const void *, const void *);
1255 const char *sort;
1256 struct shadow_copy2_config *config;
1258 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1259 return);
1261 sort = config->sort_order;
1262 if (sort == NULL) {
1263 return;
1266 if (strcmp(sort, "asc") == 0) {
1267 cmpfunc = shadow_copy2_label_cmp_asc;
1268 } else if (strcmp(sort, "desc") == 0) {
1269 cmpfunc = shadow_copy2_label_cmp_desc;
1270 } else {
1271 return;
1274 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1275 shadow_copy2_data->labels)
1277 TYPESAFE_QSORT(shadow_copy2_data->labels,
1278 shadow_copy2_data->num_volumes,
1279 cmpfunc);
1283 static int shadow_copy2_get_shadow_copy_data(
1284 vfs_handle_struct *handle, files_struct *fsp,
1285 struct shadow_copy_data *shadow_copy2_data,
1286 bool labels)
1288 DIR *p;
1289 const char *snapdir;
1290 struct dirent *d;
1291 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1293 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1294 if (snapdir == NULL) {
1295 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1296 handle->conn->connectpath));
1297 errno = EINVAL;
1298 talloc_free(tmp_ctx);
1299 return -1;
1302 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir, NULL, 0);
1304 if (!p) {
1305 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1306 " - %s\n", snapdir, strerror(errno)));
1307 talloc_free(tmp_ctx);
1308 errno = ENOSYS;
1309 return -1;
1312 shadow_copy2_data->num_volumes = 0;
1313 shadow_copy2_data->labels = NULL;
1315 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
1316 char snapshot[GMT_NAME_LEN+1];
1317 SHADOW_COPY_LABEL *tlabels;
1320 * ignore names not of the right form in the snapshot
1321 * directory
1323 if (!shadow_copy2_snapshot_to_gmt(
1324 handle, d->d_name,
1325 snapshot, sizeof(snapshot))) {
1327 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1328 "ignoring %s\n", d->d_name));
1329 continue;
1331 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1332 d->d_name, snapshot));
1334 if (!labels) {
1335 /* the caller doesn't want the labels */
1336 shadow_copy2_data->num_volumes++;
1337 continue;
1340 tlabels = talloc_realloc(shadow_copy2_data,
1341 shadow_copy2_data->labels,
1342 SHADOW_COPY_LABEL,
1343 shadow_copy2_data->num_volumes+1);
1344 if (tlabels == NULL) {
1345 DEBUG(0,("shadow_copy2: out of memory\n"));
1346 SMB_VFS_NEXT_CLOSEDIR(handle, p);
1347 talloc_free(tmp_ctx);
1348 return -1;
1351 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
1352 sizeof(*tlabels));
1354 shadow_copy2_data->num_volumes++;
1355 shadow_copy2_data->labels = tlabels;
1358 SMB_VFS_NEXT_CLOSEDIR(handle,p);
1360 shadow_copy2_sort_data(handle, shadow_copy2_data);
1362 talloc_free(tmp_ctx);
1363 return 0;
1366 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
1367 struct files_struct *fsp,
1368 uint32 security_info,
1369 TALLOC_CTX *mem_ctx,
1370 struct security_descriptor **ppdesc)
1372 time_t timestamp;
1373 char *stripped;
1374 NTSTATUS status;
1375 char *conv;
1377 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1378 fsp->fsp_name->base_name,
1379 &timestamp, &stripped)) {
1380 return map_nt_error_from_unix(errno);
1382 if (timestamp == 0) {
1383 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1384 mem_ctx,
1385 ppdesc);
1387 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1388 TALLOC_FREE(stripped);
1389 if (conv == NULL) {
1390 return map_nt_error_from_unix(errno);
1392 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
1393 mem_ctx, ppdesc);
1394 TALLOC_FREE(conv);
1395 return status;
1398 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
1399 const char *fname,
1400 uint32 security_info,
1401 TALLOC_CTX *mem_ctx,
1402 struct security_descriptor **ppdesc)
1404 time_t timestamp;
1405 char *stripped;
1406 NTSTATUS status;
1407 char *conv;
1409 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1410 &timestamp, &stripped)) {
1411 return map_nt_error_from_unix(errno);
1413 if (timestamp == 0) {
1414 return SMB_VFS_NEXT_GET_NT_ACL(handle, fname, security_info,
1415 mem_ctx, ppdesc);
1417 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1418 TALLOC_FREE(stripped);
1419 if (conv == NULL) {
1420 return map_nt_error_from_unix(errno);
1422 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
1423 mem_ctx, ppdesc);
1424 TALLOC_FREE(conv);
1425 return status;
1428 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
1429 const char *fname, mode_t mode)
1431 time_t timestamp;
1432 char *stripped;
1433 int ret, saved_errno;
1434 char *conv;
1436 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1437 &timestamp, &stripped)) {
1438 return -1;
1440 if (timestamp == 0) {
1441 return SMB_VFS_NEXT_MKDIR(handle, fname, mode);
1443 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1444 TALLOC_FREE(stripped);
1445 if (conv == NULL) {
1446 return -1;
1448 ret = SMB_VFS_NEXT_MKDIR(handle, conv, mode);
1449 saved_errno = errno;
1450 TALLOC_FREE(conv);
1451 errno = saved_errno;
1452 return ret;
1455 static int shadow_copy2_rmdir(vfs_handle_struct *handle, const char *fname)
1457 time_t timestamp;
1458 char *stripped;
1459 int ret, saved_errno;
1460 char *conv;
1462 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1463 &timestamp, &stripped)) {
1464 return -1;
1466 if (timestamp == 0) {
1467 return SMB_VFS_NEXT_RMDIR(handle, fname);
1469 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1470 TALLOC_FREE(stripped);
1471 if (conv == NULL) {
1472 return -1;
1474 ret = SMB_VFS_NEXT_RMDIR(handle, conv);
1475 saved_errno = errno;
1476 TALLOC_FREE(conv);
1477 errno = saved_errno;
1478 return ret;
1481 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
1482 unsigned int flags)
1484 time_t timestamp;
1485 char *stripped;
1486 int ret, saved_errno;
1487 char *conv;
1489 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1490 &timestamp, &stripped)) {
1491 return -1;
1493 if (timestamp == 0) {
1494 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
1496 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1497 TALLOC_FREE(stripped);
1498 if (conv == NULL) {
1499 return -1;
1501 ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
1502 saved_errno = errno;
1503 TALLOC_FREE(conv);
1504 errno = saved_errno;
1505 return ret;
1508 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
1509 const char *fname, const char *aname,
1510 void *value, size_t size)
1512 time_t timestamp;
1513 char *stripped;
1514 ssize_t ret;
1515 int saved_errno;
1516 char *conv;
1518 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1519 &timestamp, &stripped)) {
1520 return -1;
1522 if (timestamp == 0) {
1523 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
1524 size);
1526 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1527 TALLOC_FREE(stripped);
1528 if (conv == NULL) {
1529 return -1;
1531 ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
1532 saved_errno = errno;
1533 TALLOC_FREE(conv);
1534 errno = saved_errno;
1535 return ret;
1538 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
1539 const char *fname,
1540 char *list, size_t size)
1542 time_t timestamp;
1543 char *stripped;
1544 ssize_t ret;
1545 int saved_errno;
1546 char *conv;
1548 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1549 &timestamp, &stripped)) {
1550 return -1;
1552 if (timestamp == 0) {
1553 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
1555 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1556 TALLOC_FREE(stripped);
1557 if (conv == NULL) {
1558 return -1;
1560 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
1561 saved_errno = errno;
1562 TALLOC_FREE(conv);
1563 errno = saved_errno;
1564 return ret;
1567 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
1568 const char *fname, const char *aname)
1570 time_t timestamp;
1571 char *stripped;
1572 int ret, saved_errno;
1573 char *conv;
1575 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1576 &timestamp, &stripped)) {
1577 return -1;
1579 if (timestamp == 0) {
1580 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
1582 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1583 TALLOC_FREE(stripped);
1584 if (conv == NULL) {
1585 return -1;
1587 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
1588 saved_errno = errno;
1589 TALLOC_FREE(conv);
1590 errno = saved_errno;
1591 return ret;
1594 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
1595 const char *fname,
1596 const char *aname, const void *value,
1597 size_t size, int flags)
1599 time_t timestamp;
1600 char *stripped;
1601 ssize_t ret;
1602 int saved_errno;
1603 char *conv;
1605 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1606 &timestamp, &stripped)) {
1607 return -1;
1609 if (timestamp == 0) {
1610 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
1611 flags);
1613 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1614 TALLOC_FREE(stripped);
1615 if (conv == NULL) {
1616 return -1;
1618 ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
1619 saved_errno = errno;
1620 TALLOC_FREE(conv);
1621 errno = saved_errno;
1622 return ret;
1625 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
1626 const char *fname, mode_t mode)
1628 time_t timestamp;
1629 char *stripped;
1630 ssize_t ret;
1631 int saved_errno;
1632 char *conv;
1634 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1635 &timestamp, &stripped)) {
1636 return -1;
1638 if (timestamp == 0) {
1639 return SMB_VFS_NEXT_CHMOD_ACL(handle, fname, mode);
1641 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1642 TALLOC_FREE(stripped);
1643 if (conv == NULL) {
1644 return -1;
1646 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv, mode);
1647 saved_errno = errno;
1648 TALLOC_FREE(conv);
1649 errno = saved_errno;
1650 return ret;
1653 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
1654 const char *path,
1655 const char *name,
1656 TALLOC_CTX *mem_ctx,
1657 char **found_name)
1659 time_t timestamp;
1660 char *stripped;
1661 ssize_t ret;
1662 int saved_errno;
1663 char *conv;
1665 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1666 &timestamp, &stripped)) {
1667 return -1;
1669 if (timestamp == 0) {
1670 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
1671 mem_ctx, found_name);
1673 if (stripped[0] == '\0') {
1674 *found_name = talloc_strdup(mem_ctx, name);
1675 if (*found_name == NULL) {
1676 errno = ENOMEM;
1677 return -1;
1679 return 0;
1681 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1682 TALLOC_FREE(stripped);
1683 if (conv == NULL) {
1684 return -1;
1686 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
1687 mem_ctx, found_name);
1688 saved_errno = errno;
1689 TALLOC_FREE(conv);
1690 errno = saved_errno;
1691 return ret;
1694 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
1695 const char *path, bool small_query,
1696 uint64_t *bsize, uint64_t *dfree,
1697 uint64_t *dsize)
1699 time_t timestamp;
1700 char *stripped;
1701 ssize_t ret;
1702 int saved_errno;
1703 char *conv;
1705 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1706 &timestamp, &stripped)) {
1707 return -1;
1709 if (timestamp == 0) {
1710 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1711 bsize, dfree, dsize);
1714 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1715 TALLOC_FREE(stripped);
1716 if (conv == NULL) {
1717 return -1;
1720 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, small_query, bsize, dfree,
1721 dsize);
1723 saved_errno = errno;
1724 TALLOC_FREE(conv);
1725 errno = saved_errno;
1727 return ret;
1730 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
1731 const char *service, const char *user)
1733 struct shadow_copy2_config *config;
1734 int ret;
1735 const char *snapdir;
1736 const char *gmt_format;
1737 const char *sort_order;
1738 const char *basedir;
1739 const char *mount_point;
1741 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
1742 (unsigned)handle->conn->cnum,
1743 handle->conn->connectpath));
1745 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1746 if (ret < 0) {
1747 return ret;
1750 config = talloc_zero(handle->conn, struct shadow_copy2_config);
1751 if (config == NULL) {
1752 DEBUG(0, ("talloc_zero() failed\n"));
1753 errno = ENOMEM;
1754 return -1;
1757 gmt_format = lp_parm_const_string(SNUM(handle->conn),
1758 "shadow", "format",
1759 GMT_FORMAT);
1760 config->gmt_format = talloc_strdup(config, gmt_format);
1761 if (config->gmt_format == NULL) {
1762 DEBUG(0, ("talloc_strdup() failed\n"));
1763 errno = ENOMEM;
1764 return -1;
1767 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
1768 "shadow", "sscanf", false);
1770 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
1771 "shadow", "localtime",
1772 false);
1774 snapdir = lp_parm_const_string(SNUM(handle->conn),
1775 "shadow", "snapdir",
1776 ".snapshots");
1777 config->snapdir = talloc_strdup(config, snapdir);
1778 if (config->snapdir == NULL) {
1779 DEBUG(0, ("talloc_strdup() failed\n"));
1780 errno = ENOMEM;
1781 return -1;
1784 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
1785 "shadow",
1786 "snapdirseverywhere",
1787 false);
1789 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
1790 "shadow", "crossmountpoints",
1791 false);
1793 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
1794 "shadow", "fixinodes",
1795 false);
1797 sort_order = lp_parm_const_string(SNUM(handle->conn),
1798 "shadow", "sort", "desc");
1799 config->sort_order = talloc_strdup(config, sort_order);
1800 if (config->sort_order == NULL) {
1801 DEBUG(0, ("talloc_strdup() failed\n"));
1802 errno = ENOMEM;
1803 return -1;
1806 mount_point = lp_parm_const_string(SNUM(handle->conn),
1807 "shadow", "mountpoint", NULL);
1808 if (mount_point != NULL) {
1809 if (mount_point[0] != '/') {
1810 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
1811 "relative ('%s'), but it has to be an "
1812 "absolute path. Ignoring provided value.\n",
1813 mount_point));
1814 mount_point = NULL;
1815 } else {
1816 char *p;
1817 p = strstr(handle->conn->connectpath, mount_point);
1818 if (p != handle->conn->connectpath) {
1819 DEBUG(1, ("Warning: mount_point (%s) is not a "
1820 "subdirectory of the share root "
1821 "(%s). Ignoring provided value.\n",
1822 mount_point,
1823 handle->conn->connectpath));
1824 mount_point = NULL;
1829 if (mount_point != NULL) {
1830 config->mount_point = talloc_strdup(config, mount_point);
1831 if (config->mount_point == NULL) {
1832 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
1833 return -1;
1835 } else {
1836 config->mount_point = shadow_copy2_find_mount_point(config,
1837 handle);
1838 if (config->mount_point == NULL) {
1839 DEBUG(0, (__location__ ": shadow_copy2_find_mount_point"
1840 " failed: %s\n", strerror(errno)));
1841 return -1;
1845 basedir = lp_parm_const_string(SNUM(handle->conn),
1846 "shadow", "basedir", NULL);
1848 if (basedir != NULL) {
1849 if (basedir[0] != '/') {
1850 DEBUG(1, (__location__ " Warning: 'basedir' is "
1851 "relative ('%s'), but it has to be an "
1852 "absolute path. Disabling basedir.\n",
1853 basedir));
1854 } else {
1855 char *p;
1856 p = strstr(basedir, config->mount_point);
1857 if (p != basedir) {
1858 DEBUG(1, ("Warning: basedir (%s) is not a "
1859 "subdirectory of the share root's "
1860 "mount point (%s). "
1861 "Disabling basedir\n",
1862 basedir, config->mount_point));
1863 } else {
1864 config->basedir = talloc_strdup(config,
1865 basedir);
1866 if (config->basedir == NULL) {
1867 DEBUG(0, ("talloc_strdup() failed\n"));
1868 errno = ENOMEM;
1869 return -1;
1875 if (config->snapdirseverywhere && config->basedir != NULL) {
1876 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
1877 "with 'snapdirseverywhere'. Disabling basedir.\n"));
1878 TALLOC_FREE(config->basedir);
1881 if (config->crossmountpoints && config->basedir != NULL) {
1882 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
1883 "with 'crossmountpoints'. Disabling basedir.\n"));
1884 TALLOC_FREE(config->basedir);
1887 if (config->basedir == NULL) {
1888 config->basedir = config->mount_point;
1891 if (strlen(config->basedir) != strlen(handle->conn->connectpath)) {
1892 config->rel_connectpath = talloc_strdup(config,
1893 handle->conn->connectpath + strlen(config->basedir));
1894 if (config->rel_connectpath == NULL) {
1895 DEBUG(0, ("talloc_strdup() failed\n"));
1896 errno = ENOMEM;
1897 return -1;
1901 if (config->snapdir[0] == '/') {
1902 config->snapdir_absolute = true;
1904 if (config->snapdirseverywhere == true) {
1905 DEBUG(1, (__location__ " Warning: An absolute snapdir "
1906 "is incompatible with 'snapdirseverywhere', "
1907 "setting 'snapdirseverywhere' to false.\n"));
1908 config->snapdirseverywhere = false;
1911 if (config->crossmountpoints == true) {
1912 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
1913 "is not supported with an absolute snapdir. "
1914 "Disabling it.\n"));
1915 config->crossmountpoints = false;
1918 config->snapshot_basepath = config->snapdir;
1919 } else {
1920 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
1921 config->mount_point, config->snapdir);
1922 if (config->snapshot_basepath == NULL) {
1923 DEBUG(0, ("talloc_asprintf() failed\n"));
1924 errno = ENOMEM;
1925 return -1;
1929 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
1930 " share root: '%s'\n"
1931 " basedir: '%s'\n"
1932 " mountpoint: '%s'\n"
1933 " rel share root: '%s'\n"
1934 " snapdir: '%s'\n"
1935 " snapshot base path: '%s'\n"
1936 " format: '%s'\n"
1937 " use sscanf: %s\n"
1938 " snapdirs everywhere: %s\n"
1939 " cross mountpoints: %s\n"
1940 " fix inodes: %s\n"
1941 " sort order: %s\n"
1943 handle->conn->connectpath,
1944 config->basedir,
1945 config->mount_point,
1946 config->rel_connectpath,
1947 config->snapdir,
1948 config->snapshot_basepath,
1949 config->gmt_format,
1950 config->use_sscanf ? "yes" : "no",
1951 config->snapdirseverywhere ? "yes" : "no",
1952 config->crossmountpoints ? "yes" : "no",
1953 config->fixinodes ? "yes" : "no",
1954 config->sort_order
1958 SMB_VFS_HANDLE_SET_DATA(handle, config,
1959 NULL, struct shadow_copy2_config,
1960 return -1);
1962 return 0;
1965 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
1966 .connect_fn = shadow_copy2_connect,
1967 .opendir_fn = shadow_copy2_opendir,
1968 .disk_free_fn = shadow_copy2_disk_free,
1969 .rename_fn = shadow_copy2_rename,
1970 .link_fn = shadow_copy2_link,
1971 .symlink_fn = shadow_copy2_symlink,
1972 .stat_fn = shadow_copy2_stat,
1973 .lstat_fn = shadow_copy2_lstat,
1974 .fstat_fn = shadow_copy2_fstat,
1975 .open_fn = shadow_copy2_open,
1976 .unlink_fn = shadow_copy2_unlink,
1977 .chmod_fn = shadow_copy2_chmod,
1978 .chown_fn = shadow_copy2_chown,
1979 .chdir_fn = shadow_copy2_chdir,
1980 .ntimes_fn = shadow_copy2_ntimes,
1981 .readlink_fn = shadow_copy2_readlink,
1982 .mknod_fn = shadow_copy2_mknod,
1983 .realpath_fn = shadow_copy2_realpath,
1984 .get_nt_acl_fn = shadow_copy2_get_nt_acl,
1985 .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
1986 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
1987 .mkdir_fn = shadow_copy2_mkdir,
1988 .rmdir_fn = shadow_copy2_rmdir,
1989 .getxattr_fn = shadow_copy2_getxattr,
1990 .listxattr_fn = shadow_copy2_listxattr,
1991 .removexattr_fn = shadow_copy2_removexattr,
1992 .setxattr_fn = shadow_copy2_setxattr,
1993 .chmod_acl_fn = shadow_copy2_chmod_acl,
1994 .chflags_fn = shadow_copy2_chflags,
1995 .get_real_filename_fn = shadow_copy2_get_real_filename,
1998 NTSTATUS vfs_shadow_copy2_init(void);
1999 NTSTATUS vfs_shadow_copy2_init(void)
2001 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2002 "shadow_copy2", &vfs_shadow_copy2_fns);