s3-shadow-copy2: Protect against already converted names
[Samba.git] / source3 / modules / vfs_shadow_copy2.c
blob439df5ddf4d68bd000be701b9012ae01ba8f45a7
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;
238 const char *snapdir;
239 ssize_t snapdirlen;
240 ptrdiff_t len_before_gmt;
242 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
243 return false);
245 DEBUG(10, (__location__ ": enter path '%s'\n", name));
247 p = strstr_m(name, "@GMT-");
248 if (p == NULL) {
249 DEBUG(11, ("@GMT not found\n"));
250 goto no_snapshot;
252 if ((p > name) && (p[-1] != '/')) {
253 /* the GMT-token does not start a path-component */
254 DEBUG(10, ("not at start, p=%p, name=%p, p[-1]=%d\n",
255 p, name, (int)p[-1]));
256 goto no_snapshot;
260 * Figure out whether we got an already converted string. One
261 * case where this happens is in a smb2 create call with the
262 * mxac create blob set. We do the get_acl call on
263 * fsp->fsp_name, which is already converted. We are converted
264 * if we got a file name of the form ".snapshots/@GMT-",
265 * i.e. ".snapshots/" precedes "p".
268 snapdir = lp_parm_const_string(SNUM(handle->conn), "shadow", "snapdir",
269 ".snapshots");
270 snapdirlen = strlen(snapdir);
271 len_before_gmt = p - name;
273 if ((len_before_gmt >= (snapdirlen + 1)) && (p[-1] == '/')) {
274 const char *parent_snapdir = p - (snapdirlen+1);
276 DEBUG(10, ("parent_snapdir = %s\n", parent_snapdir));
278 if (strncmp(parent_snapdir, snapdir, snapdirlen) == 0) {
279 DEBUG(10, ("name=%s is already converted\n", name));
280 goto no_snapshot;
283 q = strptime(p, GMT_FORMAT, &tm);
284 if (q == NULL) {
285 DEBUG(10, ("strptime failed\n"));
286 goto no_snapshot;
288 tm.tm_isdst = -1;
289 timestamp = timegm(&tm);
290 if (timestamp == (time_t)-1) {
291 DEBUG(10, ("timestamp==-1\n"));
292 goto no_snapshot;
294 if (q[0] == '\0') {
296 * The name consists of only the GMT token or the GMT
297 * token is at the end of the path. XP seems to send
298 * @GMT- at the end under certain circumstances even
299 * with a path prefix.
301 if (pstripped != NULL) {
302 stripped = talloc_strndup(mem_ctx, name, p - name);
303 if (stripped == NULL) {
304 return false;
306 *pstripped = stripped;
308 *ptimestamp = timestamp;
309 return true;
311 if (q[0] != '/') {
313 * It is not a complete path component, i.e. the path
314 * component continues after the gmt-token.
316 DEBUG(10, ("q[0] = %d\n", (int)q[0]));
317 goto no_snapshot;
319 q += 1;
321 rest_len = strlen(q);
322 dst_len = (p-name) + rest_len;
324 if (config->snapdirseverywhere) {
325 char *insert;
326 bool have_insert;
327 insert = shadow_copy2_insert_string(talloc_tos(), handle,
328 timestamp);
329 if (insert == NULL) {
330 errno = ENOMEM;
331 return false;
334 DEBUG(10, (__location__ ": snapdirseverywhere mode.\n"
335 "path '%s'.\n"
336 "insert string '%s'\n", name, insert));
338 have_insert = (strstr(name, insert+1) != NULL);
339 DEBUG(10, ("have_insert=%d, name=%s, insert+1=%s\n",
340 (int)have_insert, name, insert+1));
341 if (have_insert) {
342 DEBUG(10, (__location__ ": insert string '%s' found in "
343 "path '%s' found in snapdirseverywhere mode "
344 "==> already converted\n", insert, name));
345 TALLOC_FREE(insert);
346 goto no_snapshot;
348 TALLOC_FREE(insert);
349 } else {
350 char *snapshot_path;
351 char *s;
353 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
354 handle,
355 timestamp);
356 if (snapshot_path == NULL) {
357 errno = ENOMEM;
358 return false;
361 DEBUG(10, (__location__ " path: '%s'.\n"
362 "snapshot path: '%s'\n", name, snapshot_path));
364 s = strstr(name, snapshot_path);
365 if (s == name) {
367 * this starts with "snapshot_basepath/GMT-Token"
368 * so it is already a converted absolute
369 * path. Don't process further.
371 DEBUG(10, (__location__ ": path '%s' starts with "
372 "snapshot path '%s' (not in "
373 "snapdirseverywhere mode) ==> "
374 "already converted\n", name, snapshot_path));
375 talloc_free(snapshot_path);
376 goto no_snapshot;
378 talloc_free(snapshot_path);
381 if (pstripped != NULL) {
382 stripped = talloc_array(mem_ctx, char, dst_len+1);
383 if (stripped == NULL) {
384 errno = ENOMEM;
385 return false;
387 if (p > name) {
388 memcpy(stripped, name, p-name);
390 if (rest_len > 0) {
391 memcpy(stripped + (p-name), q, rest_len);
393 stripped[dst_len] = '\0';
394 *pstripped = stripped;
396 *ptimestamp = timestamp;
397 return true;
398 no_snapshot:
399 *ptimestamp = 0;
400 return true;
403 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
404 vfs_handle_struct *handle)
406 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
407 dev_t dev;
408 struct stat st;
409 char *p;
411 if (stat(path, &st) != 0) {
412 talloc_free(path);
413 return NULL;
416 dev = st.st_dev;
418 while ((p = strrchr(path, '/')) && p > path) {
419 *p = 0;
420 if (stat(path, &st) != 0) {
421 talloc_free(path);
422 return NULL;
424 if (st.st_dev != dev) {
425 *p = '/';
426 break;
430 return path;
434 * Convert from a name as handed in via the SMB layer
435 * and a timestamp into the local path of the snapshot
436 * of the provided file at the provided time.
438 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
439 struct vfs_handle_struct *handle,
440 const char *name, time_t timestamp)
442 struct smb_filename converted_fname;
443 char *result = NULL;
444 size_t *slashes = NULL;
445 unsigned num_slashes;
446 char *path = NULL;
447 size_t pathlen;
448 char *insert = NULL;
449 char *converted = NULL;
450 size_t insertlen;
451 int i, saved_errno;
452 size_t min_offset;
453 struct shadow_copy2_config *config;
455 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
456 return NULL);
458 DEBUG(10, ("converting '%s'\n", name));
460 if (!config->snapdirseverywhere) {
461 int ret;
462 char *snapshot_path;
464 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
465 handle,
466 timestamp);
467 if (snapshot_path == NULL) {
468 goto fail;
471 if (config->rel_connectpath == NULL) {
472 converted = talloc_asprintf(mem_ctx, "%s/%s",
473 snapshot_path, name);
474 } else {
475 converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
476 snapshot_path,
477 config->rel_connectpath,
478 name);
480 if (converted == NULL) {
481 goto fail;
484 ZERO_STRUCT(converted_fname);
485 converted_fname.base_name = converted;
487 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
488 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
489 converted,
490 ret, ret == 0 ? "ok" : strerror(errno)));
491 if (ret == 0) {
492 DEBUG(10, ("Found %s\n", converted));
493 result = converted;
494 converted = NULL;
495 goto fail;
496 } else {
497 errno = ENOENT;
498 goto fail;
500 /* never reached ... */
503 if (name[0] == 0) {
504 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
505 } else {
506 path = talloc_asprintf(
507 mem_ctx, "%s/%s", handle->conn->connectpath, name);
509 if (path == NULL) {
510 errno = ENOMEM;
511 goto fail;
513 pathlen = talloc_get_size(path)-1;
515 if (!shadow_copy2_find_slashes(talloc_tos(), path,
516 &slashes, &num_slashes)) {
517 goto fail;
520 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
521 if (insert == NULL) {
522 goto fail;
524 insertlen = talloc_get_size(insert)-1;
527 * Note: We deliberatly don't expensively initialize the
528 * array with talloc_zero here: Putting zero into
529 * converted[pathlen+insertlen] below is sufficient, because
530 * in the following for loop, the insert string is inserted
531 * at various slash places. So the memory up to position
532 * pathlen+insertlen will always be initialized when the
533 * converted string is used.
535 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
536 if (converted == NULL) {
537 goto fail;
540 if (path[pathlen-1] != '/') {
542 * Append a fake slash to find the snapshot root
544 size_t *tmp;
545 tmp = talloc_realloc(talloc_tos(), slashes,
546 size_t, num_slashes+1);
547 if (tmp == NULL) {
548 goto fail;
550 slashes = tmp;
551 slashes[num_slashes] = pathlen;
552 num_slashes += 1;
555 min_offset = 0;
557 if (!config->crossmountpoints) {
558 min_offset = strlen(config->mount_point);
561 memcpy(converted, path, pathlen+1);
562 converted[pathlen+insertlen] = '\0';
564 ZERO_STRUCT(converted_fname);
565 converted_fname.base_name = converted;
567 for (i = num_slashes-1; i>=0; i--) {
568 int ret;
569 size_t offset;
571 offset = slashes[i];
573 if (offset < min_offset) {
574 errno = ENOENT;
575 goto fail;
578 memcpy(converted+offset, insert, insertlen);
580 offset += insertlen;
581 memcpy(converted+offset, path + slashes[i],
582 pathlen - slashes[i]);
584 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
586 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
587 converted,
588 ret, ret == 0 ? "ok" : strerror(errno)));
589 if (ret == 0) {
590 /* success */
591 break;
593 if (errno == ENOTDIR) {
595 * This is a valid condition: We appended the
596 * .snaphots/@GMT.. to a file name. Just try
597 * with the upper levels.
599 continue;
601 if (errno != ENOENT) {
602 /* Other problem than "not found" */
603 goto fail;
607 if (i >= 0) {
609 * Found something
611 DEBUG(10, ("Found %s\n", converted));
612 result = converted;
613 converted = NULL;
614 } else {
615 errno = ENOENT;
617 fail:
618 saved_errno = errno;
619 TALLOC_FREE(converted);
620 TALLOC_FREE(insert);
621 TALLOC_FREE(slashes);
622 TALLOC_FREE(path);
623 errno = saved_errno;
624 return result;
628 modify a sbuf return to ensure that inodes in the shadow directory
629 are different from those in the main directory
631 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
632 SMB_STRUCT_STAT *sbuf)
634 struct shadow_copy2_config *config;
636 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
637 return);
639 if (config->fixinodes) {
640 /* some snapshot systems, like GPFS, return the name
641 device:inode for the snapshot files as the current
642 files. That breaks the 'restore' button in the shadow copy
643 GUI, as the client gets a sharing violation.
645 This is a crude way of allowing both files to be
646 open at once. It has a slight chance of inode
647 number collision, but I can't see a better approach
648 without significant VFS changes
650 uint32_t shash;
652 shash = hash(fname, strlen(fname), 0) & 0xFF000000;
653 if (shash == 0) {
654 shash = 1;
656 sbuf->st_ex_ino ^= shash;
660 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
661 const char *fname,
662 const char *mask,
663 uint32 attr)
665 time_t timestamp;
666 char *stripped;
667 DIR *ret;
668 int saved_errno;
669 char *conv;
671 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
672 &timestamp, &stripped)) {
673 return NULL;
675 if (timestamp == 0) {
676 return SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
678 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
679 TALLOC_FREE(stripped);
680 if (conv == NULL) {
681 return NULL;
683 ret = SMB_VFS_NEXT_OPENDIR(handle, conv, mask, attr);
684 saved_errno = errno;
685 TALLOC_FREE(conv);
686 errno = saved_errno;
687 return ret;
690 static int shadow_copy2_rename(vfs_handle_struct *handle,
691 const struct smb_filename *smb_fname_src,
692 const struct smb_filename *smb_fname_dst)
694 time_t timestamp_src, timestamp_dst;
696 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
697 smb_fname_src->base_name,
698 &timestamp_src, NULL)) {
699 return -1;
701 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
702 smb_fname_dst->base_name,
703 &timestamp_dst, NULL)) {
704 return -1;
706 if (timestamp_src != 0) {
707 errno = EXDEV;
708 return -1;
710 if (timestamp_dst != 0) {
711 errno = EROFS;
712 return -1;
714 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
717 static int shadow_copy2_symlink(vfs_handle_struct *handle,
718 const char *oldname, const char *newname)
720 time_t timestamp_old, timestamp_new;
722 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
723 &timestamp_old, NULL)) {
724 return -1;
726 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
727 &timestamp_new, NULL)) {
728 return -1;
730 if ((timestamp_old != 0) || (timestamp_new != 0)) {
731 errno = EROFS;
732 return -1;
734 return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
737 static int shadow_copy2_link(vfs_handle_struct *handle,
738 const char *oldname, const char *newname)
740 time_t timestamp_old, timestamp_new;
742 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
743 &timestamp_old, NULL)) {
744 return -1;
746 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
747 &timestamp_new, NULL)) {
748 return -1;
750 if ((timestamp_old != 0) || (timestamp_new != 0)) {
751 errno = EROFS;
752 return -1;
754 return SMB_VFS_NEXT_LINK(handle, oldname, newname);
757 static int shadow_copy2_stat(vfs_handle_struct *handle,
758 struct smb_filename *smb_fname)
760 time_t timestamp;
761 char *stripped, *tmp;
762 int ret, saved_errno;
764 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
765 smb_fname->base_name,
766 &timestamp, &stripped)) {
767 return -1;
769 if (timestamp == 0) {
770 return SMB_VFS_NEXT_STAT(handle, smb_fname);
773 tmp = smb_fname->base_name;
774 smb_fname->base_name = shadow_copy2_convert(
775 talloc_tos(), handle, stripped, timestamp);
776 TALLOC_FREE(stripped);
778 if (smb_fname->base_name == NULL) {
779 smb_fname->base_name = tmp;
780 return -1;
783 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
784 saved_errno = errno;
786 TALLOC_FREE(smb_fname->base_name);
787 smb_fname->base_name = tmp;
789 if (ret == 0) {
790 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
792 errno = saved_errno;
793 return ret;
796 static int shadow_copy2_lstat(vfs_handle_struct *handle,
797 struct smb_filename *smb_fname)
799 time_t timestamp;
800 char *stripped, *tmp;
801 int ret, saved_errno;
803 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
804 smb_fname->base_name,
805 &timestamp, &stripped)) {
806 return -1;
808 if (timestamp == 0) {
809 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
812 tmp = smb_fname->base_name;
813 smb_fname->base_name = shadow_copy2_convert(
814 talloc_tos(), handle, stripped, timestamp);
815 TALLOC_FREE(stripped);
817 if (smb_fname->base_name == NULL) {
818 smb_fname->base_name = tmp;
819 return -1;
822 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
823 saved_errno = errno;
825 TALLOC_FREE(smb_fname->base_name);
826 smb_fname->base_name = tmp;
828 if (ret == 0) {
829 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
831 errno = saved_errno;
832 return ret;
835 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
836 SMB_STRUCT_STAT *sbuf)
838 time_t timestamp;
839 int ret;
841 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
842 if (ret == -1) {
843 return ret;
845 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
846 fsp->fsp_name->base_name,
847 &timestamp, NULL)) {
848 return 0;
850 if (timestamp != 0) {
851 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
853 return 0;
856 static int shadow_copy2_open(vfs_handle_struct *handle,
857 struct smb_filename *smb_fname, files_struct *fsp,
858 int flags, mode_t mode)
860 time_t timestamp;
861 char *stripped, *tmp;
862 int ret, saved_errno;
864 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
865 smb_fname->base_name,
866 &timestamp, &stripped)) {
867 return -1;
869 if (timestamp == 0) {
870 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
873 tmp = smb_fname->base_name;
874 smb_fname->base_name = shadow_copy2_convert(
875 talloc_tos(), handle, stripped, timestamp);
876 TALLOC_FREE(stripped);
878 if (smb_fname->base_name == NULL) {
879 smb_fname->base_name = tmp;
880 return -1;
883 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
884 saved_errno = errno;
886 TALLOC_FREE(smb_fname->base_name);
887 smb_fname->base_name = tmp;
889 errno = saved_errno;
890 return ret;
893 static int shadow_copy2_unlink(vfs_handle_struct *handle,
894 const struct smb_filename *smb_fname)
896 time_t timestamp;
897 char *stripped;
898 int ret, saved_errno;
899 struct smb_filename *conv;
901 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
902 smb_fname->base_name,
903 &timestamp, &stripped)) {
904 return -1;
906 if (timestamp == 0) {
907 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
909 conv = cp_smb_filename(talloc_tos(), smb_fname);
910 if (conv == NULL) {
911 errno = ENOMEM;
912 return -1;
914 conv->base_name = shadow_copy2_convert(
915 conv, handle, stripped, timestamp);
916 TALLOC_FREE(stripped);
917 if (conv->base_name == NULL) {
918 return -1;
920 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
921 saved_errno = errno;
922 TALLOC_FREE(conv);
923 errno = saved_errno;
924 return ret;
927 static int shadow_copy2_chmod(vfs_handle_struct *handle, const char *fname,
928 mode_t mode)
930 time_t timestamp;
931 char *stripped;
932 int ret, saved_errno;
933 char *conv;
935 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
936 &timestamp, &stripped)) {
937 return -1;
939 if (timestamp == 0) {
940 return SMB_VFS_NEXT_CHMOD(handle, fname, mode);
942 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
943 TALLOC_FREE(stripped);
944 if (conv == NULL) {
945 return -1;
947 ret = SMB_VFS_NEXT_CHMOD(handle, conv, mode);
948 saved_errno = errno;
949 TALLOC_FREE(conv);
950 errno = saved_errno;
951 return ret;
954 static int shadow_copy2_chown(vfs_handle_struct *handle, const char *fname,
955 uid_t uid, gid_t gid)
957 time_t timestamp;
958 char *stripped;
959 int ret, saved_errno;
960 char *conv;
962 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
963 &timestamp, &stripped)) {
964 return -1;
966 if (timestamp == 0) {
967 return SMB_VFS_NEXT_CHOWN(handle, fname, uid, gid);
969 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
970 TALLOC_FREE(stripped);
971 if (conv == NULL) {
972 return -1;
974 ret = SMB_VFS_NEXT_CHOWN(handle, conv, uid, gid);
975 saved_errno = errno;
976 TALLOC_FREE(conv);
977 errno = saved_errno;
978 return ret;
981 static int shadow_copy2_chdir(vfs_handle_struct *handle,
982 const char *fname)
984 time_t timestamp;
985 char *stripped;
986 int ret, saved_errno;
987 char *conv;
989 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
990 &timestamp, &stripped)) {
991 return -1;
993 if (timestamp == 0) {
994 return SMB_VFS_NEXT_CHDIR(handle, fname);
996 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
997 TALLOC_FREE(stripped);
998 if (conv == NULL) {
999 return -1;
1001 ret = SMB_VFS_NEXT_CHDIR(handle, conv);
1002 saved_errno = errno;
1003 TALLOC_FREE(conv);
1004 errno = saved_errno;
1005 return ret;
1008 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
1009 const struct smb_filename *smb_fname,
1010 struct smb_file_time *ft)
1012 time_t timestamp;
1013 char *stripped;
1014 int ret, saved_errno;
1015 struct smb_filename *conv;
1017 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1018 smb_fname->base_name,
1019 &timestamp, &stripped)) {
1020 return -1;
1022 if (timestamp == 0) {
1023 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1025 conv = cp_smb_filename(talloc_tos(), smb_fname);
1026 if (conv == NULL) {
1027 errno = ENOMEM;
1028 return -1;
1030 conv->base_name = shadow_copy2_convert(
1031 conv, handle, stripped, timestamp);
1032 TALLOC_FREE(stripped);
1033 if (conv->base_name == NULL) {
1034 return -1;
1036 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
1037 saved_errno = errno;
1038 TALLOC_FREE(conv);
1039 errno = saved_errno;
1040 return ret;
1043 static int shadow_copy2_readlink(vfs_handle_struct *handle,
1044 const char *fname, char *buf, size_t bufsiz)
1046 time_t timestamp;
1047 char *stripped;
1048 int ret, saved_errno;
1049 char *conv;
1051 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1052 &timestamp, &stripped)) {
1053 return -1;
1055 if (timestamp == 0) {
1056 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
1058 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1059 TALLOC_FREE(stripped);
1060 if (conv == NULL) {
1061 return -1;
1063 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1064 saved_errno = errno;
1065 TALLOC_FREE(conv);
1066 errno = saved_errno;
1067 return ret;
1070 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1071 const char *fname, mode_t mode, SMB_DEV_T dev)
1073 time_t timestamp;
1074 char *stripped;
1075 int ret, saved_errno;
1076 char *conv;
1078 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1079 &timestamp, &stripped)) {
1080 return -1;
1082 if (timestamp == 0) {
1083 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
1085 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1086 TALLOC_FREE(stripped);
1087 if (conv == NULL) {
1088 return -1;
1090 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1091 saved_errno = errno;
1092 TALLOC_FREE(conv);
1093 errno = saved_errno;
1094 return ret;
1097 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
1098 const char *fname)
1100 time_t timestamp;
1101 char *stripped = NULL;
1102 char *tmp = NULL;
1103 char *result = NULL;
1104 char *inserted = NULL;
1105 char *inserted_to, *inserted_end;
1106 int saved_errno;
1108 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1109 &timestamp, &stripped)) {
1110 goto done;
1112 if (timestamp == 0) {
1113 return SMB_VFS_NEXT_REALPATH(handle, fname);
1116 tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1117 if (tmp == NULL) {
1118 goto done;
1121 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1122 if (result == NULL) {
1123 goto done;
1127 * Take away what we've inserted. This removes the @GMT-thingy
1128 * completely, but will give a path under the share root.
1130 inserted = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
1131 if (inserted == NULL) {
1132 goto done;
1134 inserted_to = strstr_m(result, inserted);
1135 if (inserted_to == NULL) {
1136 DEBUG(2, ("SMB_VFS_NEXT_REALPATH removed %s\n", inserted));
1137 goto done;
1139 inserted_end = inserted_to + talloc_get_size(inserted) - 1;
1140 memmove(inserted_to, inserted_end, strlen(inserted_end)+1);
1142 done:
1143 saved_errno = errno;
1144 TALLOC_FREE(inserted);
1145 TALLOC_FREE(tmp);
1146 TALLOC_FREE(stripped);
1147 errno = saved_errno;
1148 return result;
1152 * Check whether a given directory contains a
1153 * snapshot directory as direct subdirectory.
1154 * If yes, return the path of the snapshot-subdir,
1155 * otherwise return NULL.
1157 static char *have_snapdir(struct vfs_handle_struct *handle,
1158 const char *path)
1160 struct smb_filename smb_fname;
1161 int ret;
1162 struct shadow_copy2_config *config;
1164 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1165 return NULL);
1167 ZERO_STRUCT(smb_fname);
1168 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1169 path, config->snapdir);
1170 if (smb_fname.base_name == NULL) {
1171 return NULL;
1174 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1175 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1176 return smb_fname.base_name;
1178 TALLOC_FREE(smb_fname.base_name);
1179 return NULL;
1183 * Find the snapshot directory (if any) for the given
1184 * filename (which is relative to the share).
1186 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1187 struct vfs_handle_struct *handle,
1188 struct smb_filename *smb_fname)
1190 char *path, *p;
1191 const char *snapdir;
1192 struct shadow_copy2_config *config;
1194 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1195 return NULL);
1198 * If the non-snapdisrseverywhere mode, we should not search!
1200 if (!config->snapdirseverywhere) {
1201 return config->snapshot_basepath;
1204 path = talloc_asprintf(mem_ctx, "%s/%s",
1205 handle->conn->connectpath,
1206 smb_fname->base_name);
1207 if (path == NULL) {
1208 return NULL;
1211 snapdir = have_snapdir(handle, path);
1212 if (snapdir != NULL) {
1213 TALLOC_FREE(path);
1214 return snapdir;
1217 while ((p = strrchr(path, '/')) && (p > path)) {
1219 p[0] = '\0';
1221 snapdir = have_snapdir(handle, path);
1222 if (snapdir != NULL) {
1223 TALLOC_FREE(path);
1224 return snapdir;
1227 TALLOC_FREE(path);
1228 return NULL;
1231 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1232 const char *name,
1233 char *gmt, size_t gmt_len)
1235 struct tm timestamp;
1236 time_t timestamp_t;
1237 unsigned long int timestamp_long;
1238 const char *fmt;
1239 struct shadow_copy2_config *config;
1241 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1242 return NULL);
1244 fmt = config->gmt_format;
1246 ZERO_STRUCT(timestamp);
1247 if (config->use_sscanf) {
1248 if (sscanf(name, fmt, &timestamp_long) != 1) {
1249 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1250 "no sscanf match %s: %s\n",
1251 fmt, name));
1252 return false;
1254 timestamp_t = timestamp_long;
1255 gmtime_r(&timestamp_t, &timestamp);
1256 } else {
1257 if (strptime(name, fmt, &timestamp) == NULL) {
1258 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1259 "no match %s: %s\n",
1260 fmt, name));
1261 return false;
1263 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1264 fmt, name));
1266 if (config->use_localtime) {
1267 timestamp.tm_isdst = -1;
1268 timestamp_t = mktime(&timestamp);
1269 gmtime_r(&timestamp_t, &timestamp);
1273 strftime(gmt, gmt_len, GMT_FORMAT, &timestamp);
1274 return true;
1277 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1279 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1282 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1284 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1288 sort the shadow copy data in ascending or descending order
1290 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1291 struct shadow_copy_data *shadow_copy2_data)
1293 int (*cmpfunc)(const void *, const void *);
1294 const char *sort;
1295 struct shadow_copy2_config *config;
1297 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1298 return);
1300 sort = config->sort_order;
1301 if (sort == NULL) {
1302 return;
1305 if (strcmp(sort, "asc") == 0) {
1306 cmpfunc = shadow_copy2_label_cmp_asc;
1307 } else if (strcmp(sort, "desc") == 0) {
1308 cmpfunc = shadow_copy2_label_cmp_desc;
1309 } else {
1310 return;
1313 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1314 shadow_copy2_data->labels)
1316 TYPESAFE_QSORT(shadow_copy2_data->labels,
1317 shadow_copy2_data->num_volumes,
1318 cmpfunc);
1322 static int shadow_copy2_get_shadow_copy_data(
1323 vfs_handle_struct *handle, files_struct *fsp,
1324 struct shadow_copy_data *shadow_copy2_data,
1325 bool labels)
1327 DIR *p;
1328 const char *snapdir;
1329 struct dirent *d;
1330 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1332 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1333 if (snapdir == NULL) {
1334 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1335 handle->conn->connectpath));
1336 errno = EINVAL;
1337 talloc_free(tmp_ctx);
1338 return -1;
1341 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir, NULL, 0);
1343 if (!p) {
1344 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1345 " - %s\n", snapdir, strerror(errno)));
1346 talloc_free(tmp_ctx);
1347 errno = ENOSYS;
1348 return -1;
1351 shadow_copy2_data->num_volumes = 0;
1352 shadow_copy2_data->labels = NULL;
1354 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
1355 char snapshot[GMT_NAME_LEN+1];
1356 SHADOW_COPY_LABEL *tlabels;
1359 * ignore names not of the right form in the snapshot
1360 * directory
1362 if (!shadow_copy2_snapshot_to_gmt(
1363 handle, d->d_name,
1364 snapshot, sizeof(snapshot))) {
1366 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1367 "ignoring %s\n", d->d_name));
1368 continue;
1370 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1371 d->d_name, snapshot));
1373 if (!labels) {
1374 /* the caller doesn't want the labels */
1375 shadow_copy2_data->num_volumes++;
1376 continue;
1379 tlabels = talloc_realloc(shadow_copy2_data,
1380 shadow_copy2_data->labels,
1381 SHADOW_COPY_LABEL,
1382 shadow_copy2_data->num_volumes+1);
1383 if (tlabels == NULL) {
1384 DEBUG(0,("shadow_copy2: out of memory\n"));
1385 SMB_VFS_NEXT_CLOSEDIR(handle, p);
1386 talloc_free(tmp_ctx);
1387 return -1;
1390 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
1391 sizeof(*tlabels));
1393 shadow_copy2_data->num_volumes++;
1394 shadow_copy2_data->labels = tlabels;
1397 SMB_VFS_NEXT_CLOSEDIR(handle,p);
1399 shadow_copy2_sort_data(handle, shadow_copy2_data);
1401 talloc_free(tmp_ctx);
1402 return 0;
1405 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
1406 struct files_struct *fsp,
1407 uint32 security_info,
1408 TALLOC_CTX *mem_ctx,
1409 struct security_descriptor **ppdesc)
1411 time_t timestamp;
1412 char *stripped;
1413 NTSTATUS status;
1414 char *conv;
1416 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1417 fsp->fsp_name->base_name,
1418 &timestamp, &stripped)) {
1419 return map_nt_error_from_unix(errno);
1421 if (timestamp == 0) {
1422 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1423 mem_ctx,
1424 ppdesc);
1426 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1427 TALLOC_FREE(stripped);
1428 if (conv == NULL) {
1429 return map_nt_error_from_unix(errno);
1431 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
1432 mem_ctx, ppdesc);
1433 TALLOC_FREE(conv);
1434 return status;
1437 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
1438 const char *fname,
1439 uint32 security_info,
1440 TALLOC_CTX *mem_ctx,
1441 struct security_descriptor **ppdesc)
1443 time_t timestamp;
1444 char *stripped;
1445 NTSTATUS status;
1446 char *conv;
1448 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1449 &timestamp, &stripped)) {
1450 return map_nt_error_from_unix(errno);
1452 if (timestamp == 0) {
1453 return SMB_VFS_NEXT_GET_NT_ACL(handle, fname, security_info,
1454 mem_ctx, ppdesc);
1456 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1457 TALLOC_FREE(stripped);
1458 if (conv == NULL) {
1459 return map_nt_error_from_unix(errno);
1461 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
1462 mem_ctx, ppdesc);
1463 TALLOC_FREE(conv);
1464 return status;
1467 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
1468 const char *fname, mode_t mode)
1470 time_t timestamp;
1471 char *stripped;
1472 int ret, saved_errno;
1473 char *conv;
1475 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1476 &timestamp, &stripped)) {
1477 return -1;
1479 if (timestamp == 0) {
1480 return SMB_VFS_NEXT_MKDIR(handle, fname, mode);
1482 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1483 TALLOC_FREE(stripped);
1484 if (conv == NULL) {
1485 return -1;
1487 ret = SMB_VFS_NEXT_MKDIR(handle, conv, mode);
1488 saved_errno = errno;
1489 TALLOC_FREE(conv);
1490 errno = saved_errno;
1491 return ret;
1494 static int shadow_copy2_rmdir(vfs_handle_struct *handle, const char *fname)
1496 time_t timestamp;
1497 char *stripped;
1498 int ret, saved_errno;
1499 char *conv;
1501 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1502 &timestamp, &stripped)) {
1503 return -1;
1505 if (timestamp == 0) {
1506 return SMB_VFS_NEXT_RMDIR(handle, fname);
1508 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1509 TALLOC_FREE(stripped);
1510 if (conv == NULL) {
1511 return -1;
1513 ret = SMB_VFS_NEXT_RMDIR(handle, conv);
1514 saved_errno = errno;
1515 TALLOC_FREE(conv);
1516 errno = saved_errno;
1517 return ret;
1520 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
1521 unsigned int flags)
1523 time_t timestamp;
1524 char *stripped;
1525 int ret, saved_errno;
1526 char *conv;
1528 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1529 &timestamp, &stripped)) {
1530 return -1;
1532 if (timestamp == 0) {
1533 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
1535 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1536 TALLOC_FREE(stripped);
1537 if (conv == NULL) {
1538 return -1;
1540 ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
1541 saved_errno = errno;
1542 TALLOC_FREE(conv);
1543 errno = saved_errno;
1544 return ret;
1547 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
1548 const char *fname, const char *aname,
1549 void *value, size_t size)
1551 time_t timestamp;
1552 char *stripped;
1553 ssize_t ret;
1554 int saved_errno;
1555 char *conv;
1557 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1558 &timestamp, &stripped)) {
1559 return -1;
1561 if (timestamp == 0) {
1562 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
1563 size);
1565 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1566 TALLOC_FREE(stripped);
1567 if (conv == NULL) {
1568 return -1;
1570 ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
1571 saved_errno = errno;
1572 TALLOC_FREE(conv);
1573 errno = saved_errno;
1574 return ret;
1577 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
1578 const char *fname,
1579 char *list, size_t size)
1581 time_t timestamp;
1582 char *stripped;
1583 ssize_t ret;
1584 int saved_errno;
1585 char *conv;
1587 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1588 &timestamp, &stripped)) {
1589 return -1;
1591 if (timestamp == 0) {
1592 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
1594 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1595 TALLOC_FREE(stripped);
1596 if (conv == NULL) {
1597 return -1;
1599 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
1600 saved_errno = errno;
1601 TALLOC_FREE(conv);
1602 errno = saved_errno;
1603 return ret;
1606 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
1607 const char *fname, const char *aname)
1609 time_t timestamp;
1610 char *stripped;
1611 int ret, saved_errno;
1612 char *conv;
1614 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1615 &timestamp, &stripped)) {
1616 return -1;
1618 if (timestamp == 0) {
1619 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
1621 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1622 TALLOC_FREE(stripped);
1623 if (conv == NULL) {
1624 return -1;
1626 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
1627 saved_errno = errno;
1628 TALLOC_FREE(conv);
1629 errno = saved_errno;
1630 return ret;
1633 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
1634 const char *fname,
1635 const char *aname, const void *value,
1636 size_t size, int flags)
1638 time_t timestamp;
1639 char *stripped;
1640 ssize_t ret;
1641 int saved_errno;
1642 char *conv;
1644 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1645 &timestamp, &stripped)) {
1646 return -1;
1648 if (timestamp == 0) {
1649 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
1650 flags);
1652 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1653 TALLOC_FREE(stripped);
1654 if (conv == NULL) {
1655 return -1;
1657 ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
1658 saved_errno = errno;
1659 TALLOC_FREE(conv);
1660 errno = saved_errno;
1661 return ret;
1664 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
1665 const char *fname, mode_t mode)
1667 time_t timestamp;
1668 char *stripped;
1669 ssize_t ret;
1670 int saved_errno;
1671 char *conv;
1673 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1674 &timestamp, &stripped)) {
1675 return -1;
1677 if (timestamp == 0) {
1678 return SMB_VFS_NEXT_CHMOD_ACL(handle, fname, mode);
1680 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1681 TALLOC_FREE(stripped);
1682 if (conv == NULL) {
1683 return -1;
1685 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv, mode);
1686 saved_errno = errno;
1687 TALLOC_FREE(conv);
1688 errno = saved_errno;
1689 return ret;
1692 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
1693 const char *path,
1694 const char *name,
1695 TALLOC_CTX *mem_ctx,
1696 char **found_name)
1698 time_t timestamp;
1699 char *stripped;
1700 ssize_t ret;
1701 int saved_errno;
1702 char *conv;
1704 DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
1705 "name=[%s]\n", path, name));
1707 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1708 &timestamp, &stripped)) {
1709 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
1710 return -1;
1712 if (timestamp == 0) {
1713 DEBUG(10, ("timestamp == 0\n"));
1714 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
1715 mem_ctx, found_name);
1717 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1718 TALLOC_FREE(stripped);
1719 if (conv == NULL) {
1720 DEBUG(10, ("shadow_copy2_convert failed\n"));
1721 return -1;
1723 DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
1724 "name=[%s]\n", conv, name));
1725 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
1726 mem_ctx, found_name);
1727 DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
1728 saved_errno = errno;
1729 TALLOC_FREE(conv);
1730 errno = saved_errno;
1731 return ret;
1734 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
1735 const char *path, bool small_query,
1736 uint64_t *bsize, uint64_t *dfree,
1737 uint64_t *dsize)
1739 time_t timestamp;
1740 char *stripped;
1741 ssize_t ret;
1742 int saved_errno;
1743 char *conv;
1745 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1746 &timestamp, &stripped)) {
1747 return -1;
1749 if (timestamp == 0) {
1750 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1751 bsize, dfree, dsize);
1754 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1755 TALLOC_FREE(stripped);
1756 if (conv == NULL) {
1757 return -1;
1760 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, small_query, bsize, dfree,
1761 dsize);
1763 saved_errno = errno;
1764 TALLOC_FREE(conv);
1765 errno = saved_errno;
1767 return ret;
1770 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
1771 const char *service, const char *user)
1773 struct shadow_copy2_config *config;
1774 int ret;
1775 const char *snapdir;
1776 const char *gmt_format;
1777 const char *sort_order;
1778 const char *basedir;
1779 const char *mount_point;
1781 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
1782 (unsigned)handle->conn->cnum,
1783 handle->conn->connectpath));
1785 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1786 if (ret < 0) {
1787 return ret;
1790 config = talloc_zero(handle->conn, struct shadow_copy2_config);
1791 if (config == NULL) {
1792 DEBUG(0, ("talloc_zero() failed\n"));
1793 errno = ENOMEM;
1794 return -1;
1797 gmt_format = lp_parm_const_string(SNUM(handle->conn),
1798 "shadow", "format",
1799 GMT_FORMAT);
1800 config->gmt_format = talloc_strdup(config, gmt_format);
1801 if (config->gmt_format == NULL) {
1802 DEBUG(0, ("talloc_strdup() failed\n"));
1803 errno = ENOMEM;
1804 return -1;
1807 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
1808 "shadow", "sscanf", false);
1810 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
1811 "shadow", "localtime",
1812 false);
1814 snapdir = lp_parm_const_string(SNUM(handle->conn),
1815 "shadow", "snapdir",
1816 ".snapshots");
1817 config->snapdir = talloc_strdup(config, snapdir);
1818 if (config->snapdir == NULL) {
1819 DEBUG(0, ("talloc_strdup() failed\n"));
1820 errno = ENOMEM;
1821 return -1;
1824 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
1825 "shadow",
1826 "snapdirseverywhere",
1827 false);
1829 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
1830 "shadow", "crossmountpoints",
1831 false);
1833 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
1834 "shadow", "fixinodes",
1835 false);
1837 sort_order = lp_parm_const_string(SNUM(handle->conn),
1838 "shadow", "sort", "desc");
1839 config->sort_order = talloc_strdup(config, sort_order);
1840 if (config->sort_order == NULL) {
1841 DEBUG(0, ("talloc_strdup() failed\n"));
1842 errno = ENOMEM;
1843 return -1;
1846 mount_point = lp_parm_const_string(SNUM(handle->conn),
1847 "shadow", "mountpoint", NULL);
1848 if (mount_point != NULL) {
1849 if (mount_point[0] != '/') {
1850 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
1851 "relative ('%s'), but it has to be an "
1852 "absolute path. Ignoring provided value.\n",
1853 mount_point));
1854 mount_point = NULL;
1855 } else {
1856 char *p;
1857 p = strstr(handle->conn->connectpath, mount_point);
1858 if (p != handle->conn->connectpath) {
1859 DEBUG(1, ("Warning: mount_point (%s) is not a "
1860 "subdirectory of the share root "
1861 "(%s). Ignoring provided value.\n",
1862 mount_point,
1863 handle->conn->connectpath));
1864 mount_point = NULL;
1869 if (mount_point != NULL) {
1870 config->mount_point = talloc_strdup(config, mount_point);
1871 if (config->mount_point == NULL) {
1872 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
1873 return -1;
1875 } else {
1876 config->mount_point = shadow_copy2_find_mount_point(config,
1877 handle);
1878 if (config->mount_point == NULL) {
1879 DEBUG(0, (__location__ ": shadow_copy2_find_mount_point"
1880 " failed: %s\n", strerror(errno)));
1881 return -1;
1885 basedir = lp_parm_const_string(SNUM(handle->conn),
1886 "shadow", "basedir", NULL);
1888 if (basedir != NULL) {
1889 if (basedir[0] != '/') {
1890 DEBUG(1, (__location__ " Warning: 'basedir' is "
1891 "relative ('%s'), but it has to be an "
1892 "absolute path. Disabling basedir.\n",
1893 basedir));
1894 } else {
1895 char *p;
1896 p = strstr(basedir, config->mount_point);
1897 if (p != basedir) {
1898 DEBUG(1, ("Warning: basedir (%s) is not a "
1899 "subdirectory of the share root's "
1900 "mount point (%s). "
1901 "Disabling basedir\n",
1902 basedir, config->mount_point));
1903 } else {
1904 config->basedir = talloc_strdup(config,
1905 basedir);
1906 if (config->basedir == NULL) {
1907 DEBUG(0, ("talloc_strdup() failed\n"));
1908 errno = ENOMEM;
1909 return -1;
1915 if (config->snapdirseverywhere && config->basedir != NULL) {
1916 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
1917 "with 'snapdirseverywhere'. Disabling basedir.\n"));
1918 TALLOC_FREE(config->basedir);
1921 if (config->crossmountpoints && config->basedir != NULL) {
1922 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
1923 "with 'crossmountpoints'. Disabling basedir.\n"));
1924 TALLOC_FREE(config->basedir);
1927 if (config->basedir == NULL) {
1928 config->basedir = config->mount_point;
1931 if (strlen(config->basedir) != strlen(handle->conn->connectpath)) {
1932 config->rel_connectpath = talloc_strdup(config,
1933 handle->conn->connectpath + strlen(config->basedir));
1934 if (config->rel_connectpath == NULL) {
1935 DEBUG(0, ("talloc_strdup() failed\n"));
1936 errno = ENOMEM;
1937 return -1;
1941 if (config->snapdir[0] == '/') {
1942 config->snapdir_absolute = true;
1944 if (config->snapdirseverywhere == true) {
1945 DEBUG(1, (__location__ " Warning: An absolute snapdir "
1946 "is incompatible with 'snapdirseverywhere', "
1947 "setting 'snapdirseverywhere' to false.\n"));
1948 config->snapdirseverywhere = false;
1951 if (config->crossmountpoints == true) {
1952 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
1953 "is not supported with an absolute snapdir. "
1954 "Disabling it.\n"));
1955 config->crossmountpoints = false;
1958 config->snapshot_basepath = config->snapdir;
1959 } else {
1960 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
1961 config->mount_point, config->snapdir);
1962 if (config->snapshot_basepath == NULL) {
1963 DEBUG(0, ("talloc_asprintf() failed\n"));
1964 errno = ENOMEM;
1965 return -1;
1969 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
1970 " share root: '%s'\n"
1971 " basedir: '%s'\n"
1972 " mountpoint: '%s'\n"
1973 " rel share root: '%s'\n"
1974 " snapdir: '%s'\n"
1975 " snapshot base path: '%s'\n"
1976 " format: '%s'\n"
1977 " use sscanf: %s\n"
1978 " snapdirs everywhere: %s\n"
1979 " cross mountpoints: %s\n"
1980 " fix inodes: %s\n"
1981 " sort order: %s\n"
1983 handle->conn->connectpath,
1984 config->basedir,
1985 config->mount_point,
1986 config->rel_connectpath,
1987 config->snapdir,
1988 config->snapshot_basepath,
1989 config->gmt_format,
1990 config->use_sscanf ? "yes" : "no",
1991 config->snapdirseverywhere ? "yes" : "no",
1992 config->crossmountpoints ? "yes" : "no",
1993 config->fixinodes ? "yes" : "no",
1994 config->sort_order
1998 SMB_VFS_HANDLE_SET_DATA(handle, config,
1999 NULL, struct shadow_copy2_config,
2000 return -1);
2002 return 0;
2005 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
2006 .connect_fn = shadow_copy2_connect,
2007 .opendir_fn = shadow_copy2_opendir,
2008 .disk_free_fn = shadow_copy2_disk_free,
2009 .rename_fn = shadow_copy2_rename,
2010 .link_fn = shadow_copy2_link,
2011 .symlink_fn = shadow_copy2_symlink,
2012 .stat_fn = shadow_copy2_stat,
2013 .lstat_fn = shadow_copy2_lstat,
2014 .fstat_fn = shadow_copy2_fstat,
2015 .open_fn = shadow_copy2_open,
2016 .unlink_fn = shadow_copy2_unlink,
2017 .chmod_fn = shadow_copy2_chmod,
2018 .chown_fn = shadow_copy2_chown,
2019 .chdir_fn = shadow_copy2_chdir,
2020 .ntimes_fn = shadow_copy2_ntimes,
2021 .readlink_fn = shadow_copy2_readlink,
2022 .mknod_fn = shadow_copy2_mknod,
2023 .realpath_fn = shadow_copy2_realpath,
2024 .get_nt_acl_fn = shadow_copy2_get_nt_acl,
2025 .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
2026 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
2027 .mkdir_fn = shadow_copy2_mkdir,
2028 .rmdir_fn = shadow_copy2_rmdir,
2029 .getxattr_fn = shadow_copy2_getxattr,
2030 .listxattr_fn = shadow_copy2_listxattr,
2031 .removexattr_fn = shadow_copy2_removexattr,
2032 .setxattr_fn = shadow_copy2_setxattr,
2033 .chmod_acl_fn = shadow_copy2_chmod_acl,
2034 .chflags_fn = shadow_copy2_chflags,
2035 .get_real_filename_fn = shadow_copy2_get_real_filename,
2038 NTSTATUS vfs_shadow_copy2_init(void);
2039 NTSTATUS vfs_shadow_copy2_init(void)
2041 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2042 "shadow_copy2", &vfs_shadow_copy2_fns);