VFS: Modify opendir to take a const struct smb_filename * instead of const char *
[samba.git] / source3 / modules / vfs_shadow_copy2.c
1 /*
2  * shadow_copy2: a shadow copy module (second implementation)
3  *
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
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 /*
26  * This is a second implemetation of a shadow copy module for exposing
27  * file system snapshots to windows clients as shadow copies.
28  *
29  * See the manual page for documentation.
30  */
31
32 #include "includes.h"
33 #include "smbd/smbd.h"
34 #include "system/filesys.h"
35 #include "include/ntioctl.h"
36 #include "util_tdb.h"
37
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 *mount_point;
49         char *rel_connectpath; /* share root, relative to a snapshot root */
50         char *snapshot_basepath; /* the absolute version of snapdir */
51 };
52
53 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
54                                       size_t **poffsets,
55                                       unsigned *pnum_offsets)
56 {
57         unsigned num_offsets;
58         size_t *offsets;
59         const char *p;
60
61         num_offsets = 0;
62
63         p = str;
64         while ((p = strchr(p, '/')) != NULL) {
65                 num_offsets += 1;
66                 p += 1;
67         }
68
69         offsets = talloc_array(mem_ctx, size_t, num_offsets);
70         if (offsets == NULL) {
71                 return false;
72         }
73
74         p = str;
75         num_offsets = 0;
76         while ((p = strchr(p, '/')) != NULL) {
77                 offsets[num_offsets] = p-str;
78                 num_offsets += 1;
79                 p += 1;
80         }
81
82         *poffsets = offsets;
83         *pnum_offsets = num_offsets;
84         return true;
85 }
86
87 /**
88  * Given a timestamp, build the posix level GMT-tag string
89  * based on the configurable format.
90  */
91 static size_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
92                                             time_t snapshot,
93                                             char *snaptime_string,
94                                             size_t len)
95 {
96         struct tm snap_tm;
97         size_t snaptime_len;
98         struct shadow_copy2_config *config;
99
100         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
101                                 return 0);
102
103         if (config->use_sscanf) {
104                 snaptime_len = snprintf(snaptime_string,
105                                         len,
106                                         config->gmt_format,
107                                         (unsigned long)snapshot);
108                 if (snaptime_len <= 0) {
109                         DEBUG(10, ("snprintf failed\n"));
110                         return snaptime_len;
111                 }
112         } else {
113                 if (config->use_localtime) {
114                         if (localtime_r(&snapshot, &snap_tm) == 0) {
115                                 DEBUG(10, ("gmtime_r failed\n"));
116                                 return -1;
117                         }
118                 } else {
119                         if (gmtime_r(&snapshot, &snap_tm) == 0) {
120                                 DEBUG(10, ("gmtime_r failed\n"));
121                                 return -1;
122                         }
123                 }
124                 snaptime_len = strftime(snaptime_string,
125                                         len,
126                                         config->gmt_format,
127                                         &snap_tm);
128                 if (snaptime_len == 0) {
129                         DEBUG(10, ("strftime failed\n"));
130                         return 0;
131                 }
132         }
133
134         return snaptime_len;
135 }
136
137 /**
138  * Given a timestamp, build the string to insert into a path
139  * as a path component for creating the local path to the
140  * snapshot at the given timestamp of the input path.
141  *
142  * In the case of a parallel snapdir (specified with an
143  * absolute path), this is the inital portion of the
144  * local path of any snapshot file. The complete path is
145  * obtained by appending the portion of the file's path
146  * below the share root's mountpoint.
147  */
148 static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
149                                         struct vfs_handle_struct *handle,
150                                         time_t snapshot)
151 {
152         fstring snaptime_string;
153         size_t snaptime_len = 0;
154         char *result = NULL;
155         struct shadow_copy2_config *config;
156
157         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
158                                 return NULL);
159
160         snaptime_len = shadow_copy2_posix_gmt_string(handle,
161                                                      snapshot,
162                                                      snaptime_string,
163                                                      sizeof(snaptime_string));
164         if (snaptime_len <= 0) {
165                 return NULL;
166         }
167
168         if (config->snapdir_absolute) {
169                 result = talloc_asprintf(mem_ctx, "%s/%s",
170                                          config->snapdir, snaptime_string);
171         } else {
172                 result = talloc_asprintf(mem_ctx, "/%s/%s",
173                                          config->snapdir, snaptime_string);
174         }
175         if (result == NULL) {
176                 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
177         }
178
179         return result;
180 }
181
182 /**
183  * Build the posix snapshot path for the connection
184  * at the given timestamp, i.e. the absolute posix path
185  * that contains the snapshot for this file system.
186  *
187  * This only applies to classical case, i.e. not
188  * to the "snapdirseverywhere" mode.
189  */
190 static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
191                                         struct vfs_handle_struct *handle,
192                                         time_t snapshot)
193 {
194         fstring snaptime_string;
195         size_t snaptime_len = 0;
196         char *result = NULL;
197         struct shadow_copy2_config *config;
198
199         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
200                                 return NULL);
201
202         snaptime_len = shadow_copy2_posix_gmt_string(handle,
203                                                      snapshot,
204                                                      snaptime_string,
205                                                      sizeof(snaptime_string));
206         if (snaptime_len <= 0) {
207                 return NULL;
208         }
209
210         result = talloc_asprintf(mem_ctx, "%s/%s",
211                                  config->snapshot_basepath, snaptime_string);
212         if (result == NULL) {
213                 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
214         }
215
216         return result;
217 }
218
219 /**
220  * Strip a snapshot component from a filename as
221  * handed in via the smb layer.
222  * Returns the parsed timestamp and the stripped filename.
223  */
224 static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
225                                         struct vfs_handle_struct *handle,
226                                         const char *name,
227                                         time_t *ptimestamp,
228                                         char **pstripped)
229 {
230         struct tm tm;
231         time_t timestamp;
232         const char *p;
233         char *q;
234         char *stripped;
235         size_t rest_len, dst_len;
236         struct shadow_copy2_config *config;
237         const char *snapdir;
238         ssize_t snapdirlen;
239         ptrdiff_t len_before_gmt;
240
241         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
242                                 return false);
243
244         DEBUG(10, (__location__ ": enter path '%s'\n", name));
245
246         p = strstr_m(name, "@GMT-");
247         if (p == NULL) {
248                 DEBUG(11, ("@GMT not found\n"));
249                 goto no_snapshot;
250         }
251         if ((p > name) && (p[-1] != '/')) {
252                 /* the GMT-token does not start a path-component */
253                 DEBUG(10, ("not at start, p=%p, name=%p, p[-1]=%d\n",
254                            p, name, (int)p[-1]));
255                 goto no_snapshot;
256         }
257
258         /*
259          * Figure out whether we got an already converted string. One
260          * case where this happens is in a smb2 create call with the
261          * mxac create blob set. We do the get_acl call on
262          * fsp->fsp_name, which is already converted. We are converted
263          * if we got a file name of the form ".snapshots/@GMT-",
264          * i.e. ".snapshots/" precedes "p".
265          */
266
267         snapdir = lp_parm_const_string(SNUM(handle->conn), "shadow", "snapdir",
268                                        ".snapshots");
269         snapdirlen = strlen(snapdir);
270         len_before_gmt = p - name;
271
272         if ((len_before_gmt >= (snapdirlen + 1)) && (p[-1] == '/')) {
273                 const char *parent_snapdir = p - (snapdirlen+1);
274
275                 DEBUG(10, ("parent_snapdir = %s\n", parent_snapdir));
276
277                 if (strncmp(parent_snapdir, snapdir, snapdirlen) == 0) {
278                         DEBUG(10, ("name=%s is already converted\n", name));
279                         goto no_snapshot;
280                 }
281         }
282         q = strptime(p, GMT_FORMAT, &tm);
283         if (q == NULL) {
284                 DEBUG(10, ("strptime failed\n"));
285                 goto no_snapshot;
286         }
287         tm.tm_isdst = -1;
288         timestamp = timegm(&tm);
289         if (timestamp == (time_t)-1) {
290                 DEBUG(10, ("timestamp==-1\n"));
291                 goto no_snapshot;
292         }
293         if (q[0] == '\0') {
294                 /*
295                  * The name consists of only the GMT token or the GMT
296                  * token is at the end of the path. XP seems to send
297                  * @GMT- at the end under certain circumstances even
298                  * with a path prefix.
299                  */
300                 if (pstripped != NULL) {
301                         stripped = talloc_strndup(mem_ctx, name, p - name);
302                         if (stripped == NULL) {
303                                 return false;
304                         }
305                         *pstripped = stripped;
306                 }
307                 *ptimestamp = timestamp;
308                 return true;
309         }
310         if (q[0] != '/') {
311                 /*
312                  * It is not a complete path component, i.e. the path
313                  * component continues after the gmt-token.
314                  */
315                 DEBUG(10, ("q[0] = %d\n", (int)q[0]));
316                 goto no_snapshot;
317         }
318         q += 1;
319
320         rest_len = strlen(q);
321         dst_len = (p-name) + rest_len;
322
323         if (config->snapdirseverywhere) {
324                 char *insert;
325                 bool have_insert;
326                 insert = shadow_copy2_insert_string(talloc_tos(), handle,
327                                                     timestamp);
328                 if (insert == NULL) {
329                         errno = ENOMEM;
330                         return false;
331                 }
332
333                 DEBUG(10, (__location__ ": snapdirseverywhere mode.\n"
334                            "path '%s'.\n"
335                            "insert string '%s'\n", name, insert));
336
337                 have_insert = (strstr(name, insert+1) != NULL);
338                 DEBUG(10, ("have_insert=%d, name=%s, insert+1=%s\n",
339                            (int)have_insert, name, insert+1));
340                 if (have_insert) {
341                         DEBUG(10, (__location__ ": insert string '%s' found in "
342                                    "path '%s' found in snapdirseverywhere mode "
343                                    "==> already converted\n", insert, name));
344                         TALLOC_FREE(insert);
345                         goto no_snapshot;
346                 }
347                 TALLOC_FREE(insert);
348         } else {
349                 char *snapshot_path;
350                 char *s;
351
352                 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
353                                                            handle,
354                                                            timestamp);
355                 if (snapshot_path == NULL) {
356                         errno = ENOMEM;
357                         return false;
358                 }
359
360                 DEBUG(10, (__location__ " path: '%s'.\n"
361                            "snapshot path: '%s'\n", name, snapshot_path));
362
363                 s = strstr(name, snapshot_path);
364                 if (s == name) {
365                         /*
366                          * this starts with "snapshot_basepath/GMT-Token"
367                          * so it is already a converted absolute
368                          * path. Don't process further.
369                          */
370                         DEBUG(10, (__location__ ": path '%s' starts with "
371                                    "snapshot path '%s' (not in "
372                                    "snapdirseverywhere mode) ==> "
373                                    "already converted\n", name, snapshot_path));
374                         talloc_free(snapshot_path);
375                         goto no_snapshot;
376                 }
377                 talloc_free(snapshot_path);
378         }
379
380         if (pstripped != NULL) {
381                 stripped = talloc_array(mem_ctx, char, dst_len+1);
382                 if (stripped == NULL) {
383                         errno = ENOMEM;
384                         return false;
385                 }
386                 if (p > name) {
387                         memcpy(stripped, name, p-name);
388                 }
389                 if (rest_len > 0) {
390                         memcpy(stripped + (p-name), q, rest_len);
391                 }
392                 stripped[dst_len] = '\0';
393                 *pstripped = stripped;
394         }
395         *ptimestamp = timestamp;
396         return true;
397 no_snapshot:
398         *ptimestamp = 0;
399         return true;
400 }
401
402 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
403                                            vfs_handle_struct *handle)
404 {
405         char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
406         dev_t dev;
407         struct stat st;
408         char *p;
409
410         if (stat(path, &st) != 0) {
411                 talloc_free(path);
412                 return NULL;
413         }
414
415         dev = st.st_dev;
416
417         while ((p = strrchr(path, '/')) && p > path) {
418                 *p = 0;
419                 if (stat(path, &st) != 0) {
420                         talloc_free(path);
421                         return NULL;
422                 }
423                 if (st.st_dev != dev) {
424                         *p = '/';
425                         break;
426                 }
427         }
428
429         return path;
430 }
431
432 /**
433  * Convert from a name as handed in via the SMB layer
434  * and a timestamp into the local path of the snapshot
435  * of the provided file at the provided time.
436  * Also return the path in the snapshot corresponding
437  * to the file's share root.
438  */
439 static char *shadow_copy2_do_convert(TALLOC_CTX *mem_ctx,
440                                      struct vfs_handle_struct *handle,
441                                      const char *name, time_t timestamp,
442                                      size_t *snaproot_len)
443 {
444         struct smb_filename converted_fname;
445         char *result = NULL;
446         size_t *slashes = NULL;
447         unsigned num_slashes;
448         char *path = NULL;
449         size_t pathlen;
450         char *insert = NULL;
451         char *converted = NULL;
452         size_t insertlen, connectlen = 0;
453         int i, saved_errno;
454         size_t min_offset;
455         struct shadow_copy2_config *config;
456         size_t in_share_offset = 0;
457
458         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
459                                 return NULL);
460
461         DEBUG(10, ("converting '%s'\n", name));
462
463         if (!config->snapdirseverywhere) {
464                 int ret;
465                 char *snapshot_path;
466
467                 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
468                                                            handle,
469                                                            timestamp);
470                 if (snapshot_path == NULL) {
471                         goto fail;
472                 }
473
474                 if (config->rel_connectpath == NULL) {
475                         converted = talloc_asprintf(mem_ctx, "%s/%s",
476                                                     snapshot_path, name);
477                 } else {
478                         converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
479                                                     snapshot_path,
480                                                     config->rel_connectpath,
481                                                     name);
482                 }
483                 if (converted == NULL) {
484                         goto fail;
485                 }
486
487                 ZERO_STRUCT(converted_fname);
488                 converted_fname.base_name = converted;
489
490                 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
491                 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
492                            converted,
493                            ret, ret == 0 ? "ok" : strerror(errno)));
494                 if (ret == 0) {
495                         DEBUG(10, ("Found %s\n", converted));
496                         result = converted;
497                         converted = NULL;
498                         if (snaproot_len != NULL) {
499                                 *snaproot_len = strlen(snapshot_path);
500                                 if (config->rel_connectpath != NULL) {
501                                         *snaproot_len +=
502                                             strlen(config->rel_connectpath) + 1;
503                                 }
504                         }
505                         goto fail;
506                 } else {
507                         errno = ENOENT;
508                         goto fail;
509                 }
510                 /* never reached ... */
511         }
512
513         connectlen = strlen(handle->conn->connectpath);
514         if (name[0] == 0) {
515                 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
516         } else {
517                 path = talloc_asprintf(
518                         mem_ctx, "%s/%s", handle->conn->connectpath, name);
519         }
520         if (path == NULL) {
521                 errno = ENOMEM;
522                 goto fail;
523         }
524         pathlen = talloc_get_size(path)-1;
525
526         if (!shadow_copy2_find_slashes(talloc_tos(), path,
527                                        &slashes, &num_slashes)) {
528                 goto fail;
529         }
530
531         insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
532         if (insert == NULL) {
533                 goto fail;
534         }
535         insertlen = talloc_get_size(insert)-1;
536
537         /*
538          * Note: We deliberatly don't expensively initialize the
539          * array with talloc_zero here: Putting zero into
540          * converted[pathlen+insertlen] below is sufficient, because
541          * in the following for loop, the insert string is inserted
542          * at various slash places. So the memory up to position
543          * pathlen+insertlen will always be initialized when the
544          * converted string is used.
545          */
546         converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
547         if (converted == NULL) {
548                 goto fail;
549         }
550
551         if (path[pathlen-1] != '/') {
552                 /*
553                  * Append a fake slash to find the snapshot root
554                  */
555                 size_t *tmp;
556                 tmp = talloc_realloc(talloc_tos(), slashes,
557                                      size_t, num_slashes+1);
558                 if (tmp == NULL) {
559                         goto fail;
560                 }
561                 slashes = tmp;
562                 slashes[num_slashes] = pathlen;
563                 num_slashes += 1;
564         }
565
566         min_offset = 0;
567
568         if (!config->crossmountpoints) {
569                 min_offset = strlen(config->mount_point);
570         }
571
572         memcpy(converted, path, pathlen+1);
573         converted[pathlen+insertlen] = '\0';
574
575         ZERO_STRUCT(converted_fname);
576         converted_fname.base_name = converted;
577
578         for (i = num_slashes-1; i>=0; i--) {
579                 int ret;
580                 size_t offset;
581
582                 offset = slashes[i];
583
584                 if (offset < min_offset) {
585                         errno = ENOENT;
586                         goto fail;
587                 }
588
589                 if (offset >= connectlen) {
590                         in_share_offset = offset;
591                 }
592
593                 memcpy(converted+offset, insert, insertlen);
594
595                 offset += insertlen;
596                 memcpy(converted+offset, path + slashes[i],
597                        pathlen - slashes[i]);
598
599                 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
600
601                 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
602                            converted,
603                            ret, ret == 0 ? "ok" : strerror(errno)));
604                 if (ret == 0) {
605                         /* success */
606                         if (snaproot_len != NULL) {
607                                 *snaproot_len = in_share_offset + insertlen;
608                         }
609                         break;
610                 }
611                 if (errno == ENOTDIR) {
612                         /*
613                          * This is a valid condition: We appended the
614                          * .snaphots/@GMT.. to a file name. Just try
615                          * with the upper levels.
616                          */
617                         continue;
618                 }
619                 if (errno != ENOENT) {
620                         /* Other problem than "not found" */
621                         goto fail;
622                 }
623         }
624
625         if (i >= 0) {
626                 /*
627                  * Found something
628                  */
629                 DEBUG(10, ("Found %s\n", converted));
630                 result = converted;
631                 converted = NULL;
632         } else {
633                 errno = ENOENT;
634         }
635 fail:
636         saved_errno = errno;
637         TALLOC_FREE(converted);
638         TALLOC_FREE(insert);
639         TALLOC_FREE(slashes);
640         TALLOC_FREE(path);
641         errno = saved_errno;
642         return result;
643 }
644
645 /**
646  * Convert from a name as handed in via the SMB layer
647  * and a timestamp into the local path of the snapshot
648  * of the provided file at the provided time.
649  */
650 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
651                                   struct vfs_handle_struct *handle,
652                                   const char *name, time_t timestamp)
653 {
654         return shadow_copy2_do_convert(mem_ctx, handle, name, timestamp, NULL);
655 }
656
657 /*
658   modify a sbuf return to ensure that inodes in the shadow directory
659   are different from those in the main directory
660  */
661 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
662                          SMB_STRUCT_STAT *sbuf)
663 {
664         struct shadow_copy2_config *config;
665
666         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
667                                 return);
668
669         if (config->fixinodes) {
670                 /* some snapshot systems, like GPFS, return the name
671                    device:inode for the snapshot files as the current
672                    files. That breaks the 'restore' button in the shadow copy
673                    GUI, as the client gets a sharing violation.
674
675                    This is a crude way of allowing both files to be
676                    open at once. It has a slight chance of inode
677                    number collision, but I can't see a better approach
678                    without significant VFS changes
679                 */
680                 TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
681                                  .dsize = strlen(fname) };
682                 uint32_t shash;
683
684                 shash = tdb_jenkins_hash(&key) & 0xFF000000;
685                 if (shash == 0) {
686                         shash = 1;
687                 }
688                 sbuf->st_ex_ino ^= shash;
689         }
690 }
691
692 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
693                         const struct smb_filename *smb_fname,
694                         const char *mask,
695                         uint32_t attr)
696 {
697         time_t timestamp;
698         char *stripped;
699         DIR *ret;
700         int saved_errno;
701         char *conv;
702         struct smb_filename *conv_smb_fname = NULL;
703
704         if (!shadow_copy2_strip_snapshot(talloc_tos(),
705                                 handle,
706                                 smb_fname->base_name,
707                                 &timestamp,
708                                 &stripped)) {
709                 return NULL;
710         }
711         if (timestamp == 0) {
712                 return SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
713         }
714         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
715         TALLOC_FREE(stripped);
716         if (conv == NULL) {
717                 return NULL;
718         }
719         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
720                                         conv,
721                                         NULL,
722                                         NULL);
723         if (conv_smb_fname == NULL) {
724                 TALLOC_FREE(conv);
725                 return NULL;
726         }
727         ret = SMB_VFS_NEXT_OPENDIR(handle, conv_smb_fname, mask, attr);
728         saved_errno = errno;
729         TALLOC_FREE(conv);
730         TALLOC_FREE(conv_smb_fname);
731         errno = saved_errno;
732         return ret;
733 }
734
735 static int shadow_copy2_rename(vfs_handle_struct *handle,
736                                const struct smb_filename *smb_fname_src,
737                                const struct smb_filename *smb_fname_dst)
738 {
739         time_t timestamp_src, timestamp_dst;
740
741         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
742                                          smb_fname_src->base_name,
743                                          &timestamp_src, NULL)) {
744                 return -1;
745         }
746         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
747                                          smb_fname_dst->base_name,
748                                          &timestamp_dst, NULL)) {
749                 return -1;
750         }
751         if (timestamp_src != 0) {
752                 errno = EXDEV;
753                 return -1;
754         }
755         if (timestamp_dst != 0) {
756                 errno = EROFS;
757                 return -1;
758         }
759         return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
760 }
761
762 static int shadow_copy2_symlink(vfs_handle_struct *handle,
763                                 const char *oldname, const char *newname)
764 {
765         time_t timestamp_old, timestamp_new;
766
767         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
768                                          &timestamp_old, NULL)) {
769                 return -1;
770         }
771         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
772                                          &timestamp_new, NULL)) {
773                 return -1;
774         }
775         if ((timestamp_old != 0) || (timestamp_new != 0)) {
776                 errno = EROFS;
777                 return -1;
778         }
779         return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
780 }
781
782 static int shadow_copy2_link(vfs_handle_struct *handle,
783                              const char *oldname, const char *newname)
784 {
785         time_t timestamp_old, timestamp_new;
786
787         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
788                                          &timestamp_old, NULL)) {
789                 return -1;
790         }
791         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
792                                          &timestamp_new, NULL)) {
793                 return -1;
794         }
795         if ((timestamp_old != 0) || (timestamp_new != 0)) {
796                 errno = EROFS;
797                 return -1;
798         }
799         return SMB_VFS_NEXT_LINK(handle, oldname, newname);
800 }
801
802 static int shadow_copy2_stat(vfs_handle_struct *handle,
803                              struct smb_filename *smb_fname)
804 {
805         time_t timestamp;
806         char *stripped, *tmp;
807         int ret, saved_errno;
808
809         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
810                                          smb_fname->base_name,
811                                          &timestamp, &stripped)) {
812                 return -1;
813         }
814         if (timestamp == 0) {
815                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
816         }
817
818         tmp = smb_fname->base_name;
819         smb_fname->base_name = shadow_copy2_convert(
820                 talloc_tos(), handle, stripped, timestamp);
821         TALLOC_FREE(stripped);
822
823         if (smb_fname->base_name == NULL) {
824                 smb_fname->base_name = tmp;
825                 return -1;
826         }
827
828         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
829         saved_errno = errno;
830
831         TALLOC_FREE(smb_fname->base_name);
832         smb_fname->base_name = tmp;
833
834         if (ret == 0) {
835                 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
836         }
837         errno = saved_errno;
838         return ret;
839 }
840
841 static int shadow_copy2_lstat(vfs_handle_struct *handle,
842                               struct smb_filename *smb_fname)
843 {
844         time_t timestamp;
845         char *stripped, *tmp;
846         int ret, saved_errno;
847
848         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
849                                          smb_fname->base_name,
850                                          &timestamp, &stripped)) {
851                 return -1;
852         }
853         if (timestamp == 0) {
854                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
855         }
856
857         tmp = smb_fname->base_name;
858         smb_fname->base_name = shadow_copy2_convert(
859                 talloc_tos(), handle, stripped, timestamp);
860         TALLOC_FREE(stripped);
861
862         if (smb_fname->base_name == NULL) {
863                 smb_fname->base_name = tmp;
864                 return -1;
865         }
866
867         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
868         saved_errno = errno;
869
870         TALLOC_FREE(smb_fname->base_name);
871         smb_fname->base_name = tmp;
872
873         if (ret == 0) {
874                 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
875         }
876         errno = saved_errno;
877         return ret;
878 }
879
880 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
881                               SMB_STRUCT_STAT *sbuf)
882 {
883         time_t timestamp;
884         int ret;
885
886         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
887         if (ret == -1) {
888                 return ret;
889         }
890         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
891                                          fsp->fsp_name->base_name,
892                                          &timestamp, NULL)) {
893                 return 0;
894         }
895         if (timestamp != 0) {
896                 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
897         }
898         return 0;
899 }
900
901 static int shadow_copy2_open(vfs_handle_struct *handle,
902                              struct smb_filename *smb_fname, files_struct *fsp,
903                              int flags, mode_t mode)
904 {
905         time_t timestamp;
906         char *stripped, *tmp;
907         int ret, saved_errno;
908
909         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
910                                          smb_fname->base_name,
911                                          &timestamp, &stripped)) {
912                 return -1;
913         }
914         if (timestamp == 0) {
915                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
916         }
917
918         tmp = smb_fname->base_name;
919         smb_fname->base_name = shadow_copy2_convert(
920                 talloc_tos(), handle, stripped, timestamp);
921         TALLOC_FREE(stripped);
922
923         if (smb_fname->base_name == NULL) {
924                 smb_fname->base_name = tmp;
925                 return -1;
926         }
927
928         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
929         saved_errno = errno;
930
931         TALLOC_FREE(smb_fname->base_name);
932         smb_fname->base_name = tmp;
933
934         errno = saved_errno;
935         return ret;
936 }
937
938 static int shadow_copy2_unlink(vfs_handle_struct *handle,
939                                const struct smb_filename *smb_fname)
940 {
941         time_t timestamp;
942         char *stripped;
943         int ret, saved_errno;
944         struct smb_filename *conv;
945
946         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
947                                          smb_fname->base_name,
948                                          &timestamp, &stripped)) {
949                 return -1;
950         }
951         if (timestamp == 0) {
952                 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
953         }
954         conv = cp_smb_filename(talloc_tos(), smb_fname);
955         if (conv == NULL) {
956                 errno = ENOMEM;
957                 return -1;
958         }
959         conv->base_name = shadow_copy2_convert(
960                 conv, handle, stripped, timestamp);
961         TALLOC_FREE(stripped);
962         if (conv->base_name == NULL) {
963                 return -1;
964         }
965         ret = SMB_VFS_NEXT_UNLINK(handle, conv);
966         saved_errno = errno;
967         TALLOC_FREE(conv);
968         errno = saved_errno;
969         return ret;
970 }
971
972 static int shadow_copy2_chmod(vfs_handle_struct *handle, const char *fname,
973                               mode_t mode)
974 {
975         time_t timestamp;
976         char *stripped;
977         int ret, saved_errno;
978         char *conv;
979
980         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
981                                          &timestamp, &stripped)) {
982                 return -1;
983         }
984         if (timestamp == 0) {
985                 return SMB_VFS_NEXT_CHMOD(handle, fname, mode);
986         }
987         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
988         TALLOC_FREE(stripped);
989         if (conv == NULL) {
990                 return -1;
991         }
992         ret = SMB_VFS_NEXT_CHMOD(handle, conv, mode);
993         saved_errno = errno;
994         TALLOC_FREE(conv);
995         errno = saved_errno;
996         return ret;
997 }
998
999 static int shadow_copy2_chown(vfs_handle_struct *handle, const char *fname,
1000                               uid_t uid, gid_t gid)
1001 {
1002         time_t timestamp;
1003         char *stripped;
1004         int ret, saved_errno;
1005         char *conv;
1006
1007         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1008                                          &timestamp, &stripped)) {
1009                 return -1;
1010         }
1011         if (timestamp == 0) {
1012                 return SMB_VFS_NEXT_CHOWN(handle, fname, uid, gid);
1013         }
1014         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1015         TALLOC_FREE(stripped);
1016         if (conv == NULL) {
1017                 return -1;
1018         }
1019         ret = SMB_VFS_NEXT_CHOWN(handle, conv, uid, gid);
1020         saved_errno = errno;
1021         TALLOC_FREE(conv);
1022         errno = saved_errno;
1023         return ret;
1024 }
1025
1026 static int shadow_copy2_chdir(vfs_handle_struct *handle,
1027                               const char *fname)
1028 {
1029         time_t timestamp;
1030         char *stripped;
1031         int ret, saved_errno;
1032         char *conv;
1033
1034         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1035                                          &timestamp, &stripped)) {
1036                 return -1;
1037         }
1038         if (timestamp == 0) {
1039                 return SMB_VFS_NEXT_CHDIR(handle, fname);
1040         }
1041         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1042         TALLOC_FREE(stripped);
1043         if (conv == NULL) {
1044                 return -1;
1045         }
1046         ret = SMB_VFS_NEXT_CHDIR(handle, conv);
1047         saved_errno = errno;
1048         TALLOC_FREE(conv);
1049         errno = saved_errno;
1050         return ret;
1051 }
1052
1053 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
1054                                const struct smb_filename *smb_fname,
1055                                struct smb_file_time *ft)
1056 {
1057         time_t timestamp;
1058         char *stripped;
1059         int ret, saved_errno;
1060         struct smb_filename *conv;
1061
1062         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1063                                          smb_fname->base_name,
1064                                          &timestamp, &stripped)) {
1065                 return -1;
1066         }
1067         if (timestamp == 0) {
1068                 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1069         }
1070         conv = cp_smb_filename(talloc_tos(), smb_fname);
1071         if (conv == NULL) {
1072                 errno = ENOMEM;
1073                 return -1;
1074         }
1075         conv->base_name = shadow_copy2_convert(
1076                 conv, handle, stripped, timestamp);
1077         TALLOC_FREE(stripped);
1078         if (conv->base_name == NULL) {
1079                 return -1;
1080         }
1081         ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
1082         saved_errno = errno;
1083         TALLOC_FREE(conv);
1084         errno = saved_errno;
1085         return ret;
1086 }
1087
1088 static int shadow_copy2_readlink(vfs_handle_struct *handle,
1089                                  const char *fname, char *buf, size_t bufsiz)
1090 {
1091         time_t timestamp;
1092         char *stripped;
1093         int ret, saved_errno;
1094         char *conv;
1095
1096         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1097                                          &timestamp, &stripped)) {
1098                 return -1;
1099         }
1100         if (timestamp == 0) {
1101                 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
1102         }
1103         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1104         TALLOC_FREE(stripped);
1105         if (conv == NULL) {
1106                 return -1;
1107         }
1108         ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1109         saved_errno = errno;
1110         TALLOC_FREE(conv);
1111         errno = saved_errno;
1112         return ret;
1113 }
1114
1115 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1116                               const char *fname, mode_t mode, SMB_DEV_T dev)
1117 {
1118         time_t timestamp;
1119         char *stripped;
1120         int ret, saved_errno;
1121         char *conv;
1122
1123         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1124                                          &timestamp, &stripped)) {
1125                 return -1;
1126         }
1127         if (timestamp == 0) {
1128                 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
1129         }
1130         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1131         TALLOC_FREE(stripped);
1132         if (conv == NULL) {
1133                 return -1;
1134         }
1135         ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1136         saved_errno = errno;
1137         TALLOC_FREE(conv);
1138         errno = saved_errno;
1139         return ret;
1140 }
1141
1142 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
1143                                    const char *fname)
1144 {
1145         time_t timestamp;
1146         char *stripped = NULL;
1147         char *tmp = NULL;
1148         char *result = NULL;
1149         int saved_errno;
1150
1151         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1152                                          &timestamp, &stripped)) {
1153                 goto done;
1154         }
1155         if (timestamp == 0) {
1156                 return SMB_VFS_NEXT_REALPATH(handle, fname);
1157         }
1158
1159         tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1160         if (tmp == NULL) {
1161                 goto done;
1162         }
1163
1164         result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1165
1166 done:
1167         saved_errno = errno;
1168         TALLOC_FREE(tmp);
1169         TALLOC_FREE(stripped);
1170         errno = saved_errno;
1171         return result;
1172 }
1173
1174 /**
1175  * Check whether a given directory contains a
1176  * snapshot directory as direct subdirectory.
1177  * If yes, return the path of the snapshot-subdir,
1178  * otherwise return NULL.
1179  */
1180 static char *have_snapdir(struct vfs_handle_struct *handle,
1181                           const char *path)
1182 {
1183         struct smb_filename smb_fname;
1184         int ret;
1185         struct shadow_copy2_config *config;
1186
1187         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1188                                 return NULL);
1189
1190         ZERO_STRUCT(smb_fname);
1191         smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1192                                               path, config->snapdir);
1193         if (smb_fname.base_name == NULL) {
1194                 return NULL;
1195         }
1196
1197         ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1198         if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1199                 return smb_fname.base_name;
1200         }
1201         TALLOC_FREE(smb_fname.base_name);
1202         return NULL;
1203 }
1204
1205 static bool check_access_snapdir(struct vfs_handle_struct *handle,
1206                                 const char *path)
1207 {
1208         struct smb_filename smb_fname;
1209         int ret;
1210         NTSTATUS status;
1211
1212         ZERO_STRUCT(smb_fname);
1213         smb_fname.base_name = talloc_asprintf(talloc_tos(),
1214                                                 "%s",
1215                                                 path);
1216         if (smb_fname.base_name == NULL) {
1217                 return false;
1218         }
1219
1220         ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1221         if (ret != 0 || !S_ISDIR(smb_fname.st.st_ex_mode)) {
1222                 TALLOC_FREE(smb_fname.base_name);
1223                 return false;
1224         }
1225
1226         status = smbd_check_access_rights(handle->conn,
1227                                         &smb_fname,
1228                                         false,
1229                                         SEC_DIR_LIST);
1230         if (!NT_STATUS_IS_OK(status)) {
1231                 DEBUG(0,("user does not have list permission "
1232                         "on snapdir %s\n",
1233                         smb_fname.base_name));
1234                 TALLOC_FREE(smb_fname.base_name);
1235                 return false;
1236         }
1237         TALLOC_FREE(smb_fname.base_name);
1238         return true;
1239 }
1240
1241 /**
1242  * Find the snapshot directory (if any) for the given
1243  * filename (which is relative to the share).
1244  */
1245 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1246                                              struct vfs_handle_struct *handle,
1247                                              struct smb_filename *smb_fname)
1248 {
1249         char *path, *p;
1250         const char *snapdir;
1251         struct shadow_copy2_config *config;
1252
1253         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1254                                 return NULL);
1255
1256         /*
1257          * If the non-snapdisrseverywhere mode, we should not search!
1258          */
1259         if (!config->snapdirseverywhere) {
1260                 return config->snapshot_basepath;
1261         }
1262
1263         path = talloc_asprintf(mem_ctx, "%s/%s",
1264                                handle->conn->connectpath,
1265                                smb_fname->base_name);
1266         if (path == NULL) {
1267                 return NULL;
1268         }
1269
1270         snapdir = have_snapdir(handle, path);
1271         if (snapdir != NULL) {
1272                 TALLOC_FREE(path);
1273                 return snapdir;
1274         }
1275
1276         while ((p = strrchr(path, '/')) && (p > path)) {
1277
1278                 p[0] = '\0';
1279
1280                 snapdir = have_snapdir(handle, path);
1281                 if (snapdir != NULL) {
1282                         TALLOC_FREE(path);
1283                         return snapdir;
1284                 }
1285         }
1286         TALLOC_FREE(path);
1287         return NULL;
1288 }
1289
1290 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1291                                          const char *name,
1292                                          char *gmt, size_t gmt_len)
1293 {
1294         struct tm timestamp;
1295         time_t timestamp_t;
1296         unsigned long int timestamp_long;
1297         const char *fmt;
1298         struct shadow_copy2_config *config;
1299
1300         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1301                                 return NULL);
1302
1303         fmt = config->gmt_format;
1304
1305         ZERO_STRUCT(timestamp);
1306         if (config->use_sscanf) {
1307                 if (sscanf(name, fmt, &timestamp_long) != 1) {
1308                         DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1309                                    "no sscanf match %s: %s\n",
1310                                    fmt, name));
1311                         return false;
1312                 }
1313                 timestamp_t = timestamp_long;
1314                 gmtime_r(&timestamp_t, &timestamp);
1315         } else {
1316                 if (strptime(name, fmt, &timestamp) == NULL) {
1317                         DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1318                                    "no match %s: %s\n",
1319                                    fmt, name));
1320                         return false;
1321                 }
1322                 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1323                            fmt, name));
1324                 
1325                 if (config->use_localtime) {
1326                         timestamp.tm_isdst = -1;
1327                         timestamp_t = mktime(&timestamp);
1328                         gmtime_r(&timestamp_t, &timestamp);
1329                 }
1330         }
1331
1332         strftime(gmt, gmt_len, GMT_FORMAT, &timestamp);
1333         return true;
1334 }
1335
1336 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1337 {
1338         return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1339 }
1340
1341 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1342 {
1343         return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1344 }
1345
1346 /*
1347   sort the shadow copy data in ascending or descending order
1348  */
1349 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1350                                    struct shadow_copy_data *shadow_copy2_data)
1351 {
1352         int (*cmpfunc)(const void *, const void *);
1353         const char *sort;
1354         struct shadow_copy2_config *config;
1355
1356         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1357                                 return);
1358
1359         sort = config->sort_order;
1360         if (sort == NULL) {
1361                 return;
1362         }
1363
1364         if (strcmp(sort, "asc") == 0) {
1365                 cmpfunc = shadow_copy2_label_cmp_asc;
1366         } else if (strcmp(sort, "desc") == 0) {
1367                 cmpfunc = shadow_copy2_label_cmp_desc;
1368         } else {
1369                 return;
1370         }
1371
1372         if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1373             shadow_copy2_data->labels)
1374         {
1375                 TYPESAFE_QSORT(shadow_copy2_data->labels,
1376                                shadow_copy2_data->num_volumes,
1377                                cmpfunc);
1378         }
1379 }
1380
1381 static int shadow_copy2_get_shadow_copy_data(
1382         vfs_handle_struct *handle, files_struct *fsp,
1383         struct shadow_copy_data *shadow_copy2_data,
1384         bool labels)
1385 {
1386         DIR *p;
1387         const char *snapdir;
1388         struct smb_filename *snapdir_smb_fname = NULL;
1389         struct dirent *d;
1390         TALLOC_CTX *tmp_ctx = talloc_stackframe();
1391         bool ret;
1392
1393         snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1394         if (snapdir == NULL) {
1395                 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1396                          handle->conn->connectpath));
1397                 errno = EINVAL;
1398                 talloc_free(tmp_ctx);
1399                 return -1;
1400         }
1401         ret = check_access_snapdir(handle, snapdir);
1402         if (!ret) {
1403                 DEBUG(0,("access denied on listing snapdir %s\n", snapdir));
1404                 errno = EACCES;
1405                 talloc_free(tmp_ctx);
1406                 return -1;
1407         }
1408
1409         snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
1410                                         snapdir,
1411                                         NULL,
1412                                         NULL);
1413         if (snapdir_smb_fname == NULL) {
1414                 errno = ENOMEM;
1415                 talloc_free(tmp_ctx);
1416                 return -1;
1417         }
1418
1419         p = SMB_VFS_NEXT_OPENDIR(handle, snapdir_smb_fname, NULL, 0);
1420
1421         if (!p) {
1422                 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1423                          " - %s\n", snapdir, strerror(errno)));
1424                 talloc_free(tmp_ctx);
1425                 errno = ENOSYS;
1426                 return -1;
1427         }
1428
1429         shadow_copy2_data->num_volumes = 0;
1430         shadow_copy2_data->labels      = NULL;
1431
1432         while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
1433                 char snapshot[GMT_NAME_LEN+1];
1434                 SHADOW_COPY_LABEL *tlabels;
1435
1436                 /*
1437                  * ignore names not of the right form in the snapshot
1438                  * directory
1439                  */
1440                 if (!shadow_copy2_snapshot_to_gmt(
1441                             handle, d->d_name,
1442                             snapshot, sizeof(snapshot))) {
1443
1444                         DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1445                                   "ignoring %s\n", d->d_name));
1446                         continue;
1447                 }
1448                 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1449                          d->d_name, snapshot));
1450
1451                 if (!labels) {
1452                         /* the caller doesn't want the labels */
1453                         shadow_copy2_data->num_volumes++;
1454                         continue;
1455                 }
1456
1457                 tlabels = talloc_realloc(shadow_copy2_data,
1458                                          shadow_copy2_data->labels,
1459                                          SHADOW_COPY_LABEL,
1460                                          shadow_copy2_data->num_volumes+1);
1461                 if (tlabels == NULL) {
1462                         DEBUG(0,("shadow_copy2: out of memory\n"));
1463                         SMB_VFS_NEXT_CLOSEDIR(handle, p);
1464                         talloc_free(tmp_ctx);
1465                         return -1;
1466                 }
1467
1468                 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
1469                         sizeof(*tlabels));
1470
1471                 shadow_copy2_data->num_volumes++;
1472                 shadow_copy2_data->labels = tlabels;
1473         }
1474
1475         SMB_VFS_NEXT_CLOSEDIR(handle,p);
1476
1477         shadow_copy2_sort_data(handle, shadow_copy2_data);
1478
1479         talloc_free(tmp_ctx);
1480         return 0;
1481 }
1482
1483 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
1484                                         struct files_struct *fsp,
1485                                         uint32_t security_info,
1486                                          TALLOC_CTX *mem_ctx,
1487                                         struct security_descriptor **ppdesc)
1488 {
1489         time_t timestamp;
1490         char *stripped;
1491         NTSTATUS status;
1492         char *conv;
1493         struct smb_filename *smb_fname = NULL;
1494
1495         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1496                                          fsp->fsp_name->base_name,
1497                                          &timestamp, &stripped)) {
1498                 return map_nt_error_from_unix(errno);
1499         }
1500         if (timestamp == 0) {
1501                 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1502                                                 mem_ctx,
1503                                                 ppdesc);
1504         }
1505         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1506         TALLOC_FREE(stripped);
1507         if (conv == NULL) {
1508                 return map_nt_error_from_unix(errno);
1509         }
1510         smb_fname = synthetic_smb_fname(talloc_tos(),
1511                                         conv,
1512                                         NULL,
1513                                         NULL);
1514         if (smb_fname == NULL) {
1515                 TALLOC_FREE(conv);
1516                 return NT_STATUS_NO_MEMORY;
1517         }
1518
1519         status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1520                                          mem_ctx, ppdesc);
1521         TALLOC_FREE(conv);
1522         TALLOC_FREE(smb_fname);
1523         return status;
1524 }
1525
1526 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
1527                                         const struct smb_filename *smb_fname,
1528                                         uint32_t security_info,
1529                                         TALLOC_CTX *mem_ctx,
1530                                         struct security_descriptor **ppdesc)
1531 {
1532         time_t timestamp;
1533         char *stripped;
1534         NTSTATUS status;
1535         char *conv;
1536         struct smb_filename *conv_smb_fname = NULL;
1537
1538         if (!shadow_copy2_strip_snapshot(talloc_tos(),
1539                                         handle,
1540                                         smb_fname->base_name,
1541                                         &timestamp,
1542                                         &stripped)) {
1543                 return map_nt_error_from_unix(errno);
1544         }
1545         if (timestamp == 0) {
1546                 return SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1547                                                mem_ctx, ppdesc);
1548         }
1549         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1550         TALLOC_FREE(stripped);
1551         if (conv == NULL) {
1552                 return map_nt_error_from_unix(errno);
1553         }
1554         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1555                                         conv,
1556                                         NULL,
1557                                         NULL);
1558         if (conv_smb_fname == NULL) {
1559                 TALLOC_FREE(conv);
1560                 return NT_STATUS_NO_MEMORY;
1561         }
1562         status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv_smb_fname, security_info,
1563                                          mem_ctx, ppdesc);
1564         TALLOC_FREE(conv);
1565         TALLOC_FREE(conv_smb_fname);
1566         return status;
1567 }
1568
1569 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
1570                                 const struct smb_filename *smb_fname,
1571                                 mode_t mode)
1572 {
1573         time_t timestamp;
1574         char *stripped;
1575         int ret, saved_errno;
1576         char *conv;
1577         struct smb_filename *conv_smb_fname = NULL;
1578
1579         if (!shadow_copy2_strip_snapshot(talloc_tos(),
1580                                         handle,
1581                                         smb_fname->base_name,
1582                                         &timestamp,
1583                                         &stripped)) {
1584                 return -1;
1585         }
1586         if (timestamp == 0) {
1587                 return SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
1588         }
1589         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1590         TALLOC_FREE(stripped);
1591         if (conv == NULL) {
1592                 return -1;
1593         }
1594         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1595                                         conv,
1596                                         NULL,
1597                                         NULL);
1598         if (conv_smb_fname == NULL) {
1599                 TALLOC_FREE(conv);
1600                 return -1;
1601         }
1602         ret = SMB_VFS_NEXT_MKDIR(handle, conv_smb_fname, mode);
1603         saved_errno = errno;
1604         TALLOC_FREE(conv);
1605         TALLOC_FREE(conv_smb_fname);
1606         errno = saved_errno;
1607         return ret;
1608 }
1609
1610 static int shadow_copy2_rmdir(vfs_handle_struct *handle,
1611                                 const struct smb_filename *smb_fname)
1612 {
1613         time_t timestamp;
1614         char *stripped;
1615         int ret, saved_errno;
1616         char *conv;
1617         struct smb_filename *conv_smb_fname = NULL;
1618
1619         if (!shadow_copy2_strip_snapshot(talloc_tos(),
1620                                         handle,
1621                                         smb_fname->base_name,
1622                                         &timestamp,
1623                                         &stripped)) {
1624                 return -1;
1625         }
1626         if (timestamp == 0) {
1627                 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
1628         }
1629         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1630         TALLOC_FREE(stripped);
1631         if (conv == NULL) {
1632                 return -1;
1633         }
1634         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1635                                         conv,
1636                                         NULL,
1637                                         NULL);
1638         if (conv_smb_fname == NULL) {
1639                 TALLOC_FREE(conv);
1640                 return -1;
1641         }
1642         ret = SMB_VFS_NEXT_RMDIR(handle, conv_smb_fname);
1643         saved_errno = errno;
1644         TALLOC_FREE(conv_smb_fname);
1645         TALLOC_FREE(conv);
1646         errno = saved_errno;
1647         return ret;
1648 }
1649
1650 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
1651                                 unsigned int flags)
1652 {
1653         time_t timestamp;
1654         char *stripped;
1655         int ret, saved_errno;
1656         char *conv;
1657
1658         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1659                                          &timestamp, &stripped)) {
1660                 return -1;
1661         }
1662         if (timestamp == 0) {
1663                 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
1664         }
1665         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1666         TALLOC_FREE(stripped);
1667         if (conv == NULL) {
1668                 return -1;
1669         }
1670         ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
1671         saved_errno = errno;
1672         TALLOC_FREE(conv);
1673         errno = saved_errno;
1674         return ret;
1675 }
1676
1677 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
1678                                      const char *fname, const char *aname,
1679                                      void *value, size_t size)
1680 {
1681         time_t timestamp;
1682         char *stripped;
1683         ssize_t ret;
1684         int saved_errno;
1685         char *conv;
1686
1687         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1688                                          &timestamp, &stripped)) {
1689                 return -1;
1690         }
1691         if (timestamp == 0) {
1692                 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
1693                                              size);
1694         }
1695         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1696         TALLOC_FREE(stripped);
1697         if (conv == NULL) {
1698                 return -1;
1699         }
1700         ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
1701         saved_errno = errno;
1702         TALLOC_FREE(conv);
1703         errno = saved_errno;
1704         return ret;
1705 }
1706
1707 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
1708                                       const char *fname,
1709                                       char *list, size_t size)
1710 {
1711         time_t timestamp;
1712         char *stripped;
1713         ssize_t ret;
1714         int saved_errno;
1715         char *conv;
1716
1717         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1718                                          &timestamp, &stripped)) {
1719                 return -1;
1720         }
1721         if (timestamp == 0) {
1722                 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
1723         }
1724         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1725         TALLOC_FREE(stripped);
1726         if (conv == NULL) {
1727                 return -1;
1728         }
1729         ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
1730         saved_errno = errno;
1731         TALLOC_FREE(conv);
1732         errno = saved_errno;
1733         return ret;
1734 }
1735
1736 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
1737                                     const char *fname, const char *aname)
1738 {
1739         time_t timestamp;
1740         char *stripped;
1741         int ret, saved_errno;
1742         char *conv;
1743
1744         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1745                                          &timestamp, &stripped)) {
1746                 return -1;
1747         }
1748         if (timestamp == 0) {
1749                 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
1750         }
1751         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1752         TALLOC_FREE(stripped);
1753         if (conv == NULL) {
1754                 return -1;
1755         }
1756         ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
1757         saved_errno = errno;
1758         TALLOC_FREE(conv);
1759         errno = saved_errno;
1760         return ret;
1761 }
1762
1763 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
1764                                  const char *fname,
1765                                  const char *aname, const void *value,
1766                                  size_t size, int flags)
1767 {
1768         time_t timestamp;
1769         char *stripped;
1770         ssize_t ret;
1771         int saved_errno;
1772         char *conv;
1773
1774         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1775                                          &timestamp, &stripped)) {
1776                 return -1;
1777         }
1778         if (timestamp == 0) {
1779                 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
1780                                              flags);
1781         }
1782         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1783         TALLOC_FREE(stripped);
1784         if (conv == NULL) {
1785                 return -1;
1786         }
1787         ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
1788         saved_errno = errno;
1789         TALLOC_FREE(conv);
1790         errno = saved_errno;
1791         return ret;
1792 }
1793
1794 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
1795                                   const char *fname, mode_t mode)
1796 {
1797         time_t timestamp;
1798         char *stripped;
1799         ssize_t ret;
1800         int saved_errno;
1801         char *conv;
1802
1803         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1804                                          &timestamp, &stripped)) {
1805                 return -1;
1806         }
1807         if (timestamp == 0) {
1808                 return SMB_VFS_NEXT_CHMOD_ACL(handle, fname, mode);
1809         }
1810         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1811         TALLOC_FREE(stripped);
1812         if (conv == NULL) {
1813                 return -1;
1814         }
1815         ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv, mode);
1816         saved_errno = errno;
1817         TALLOC_FREE(conv);
1818         errno = saved_errno;
1819         return ret;
1820 }
1821
1822 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
1823                                           const char *path,
1824                                           const char *name,
1825                                           TALLOC_CTX *mem_ctx,
1826                                           char **found_name)
1827 {
1828         time_t timestamp;
1829         char *stripped;
1830         ssize_t ret;
1831         int saved_errno;
1832         char *conv;
1833
1834         DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
1835                    "name=[%s]\n", path, name));
1836
1837         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1838                                          &timestamp, &stripped)) {
1839                 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
1840                 return -1;
1841         }
1842         if (timestamp == 0) {
1843                 DEBUG(10, ("timestamp == 0\n"));
1844                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
1845                                                       mem_ctx, found_name);
1846         }
1847         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1848         TALLOC_FREE(stripped);
1849         if (conv == NULL) {
1850                 DEBUG(10, ("shadow_copy2_convert failed\n"));
1851                 return -1;
1852         }
1853         DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
1854                    "name=[%s]\n", conv, name));
1855         ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
1856                                              mem_ctx, found_name);
1857         DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
1858         saved_errno = errno;
1859         TALLOC_FREE(conv);
1860         errno = saved_errno;
1861         return ret;
1862 }
1863
1864 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
1865                                             const char *fname)
1866 {
1867         time_t timestamp;
1868         char *stripped = NULL;
1869         char *tmp = NULL;
1870         char *result = NULL;
1871         int saved_errno;
1872         size_t rootpath_len = 0;
1873
1874         DBG_DEBUG("Calc connect path for [%s]\n", fname);
1875
1876         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1877                                          &timestamp, &stripped)) {
1878                 goto done;
1879         }
1880         if (timestamp == 0) {
1881                 return SMB_VFS_NEXT_CONNECTPATH(handle, fname);
1882         }
1883
1884         tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
1885                                       &rootpath_len);
1886         if (tmp == NULL) {
1887                 goto done;
1888         }
1889
1890         DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
1891                   (int)rootpath_len, tmp);
1892
1893         tmp[rootpath_len] = '\0';
1894         result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1895         if (result == NULL) {
1896                 goto done;
1897         }
1898
1899         DBG_DEBUG("connect path is [%s]\n", result);
1900
1901 done:
1902         saved_errno = errno;
1903         TALLOC_FREE(tmp);
1904         TALLOC_FREE(stripped);
1905         errno = saved_errno;
1906         return result;
1907 }
1908
1909 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
1910                                        const char *path, uint64_t *bsize,
1911                                        uint64_t *dfree, uint64_t *dsize)
1912 {
1913         time_t timestamp;
1914         char *stripped;
1915         ssize_t ret;
1916         int saved_errno;
1917         char *conv;
1918
1919         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1920                                          &timestamp, &stripped)) {
1921                 return -1;
1922         }
1923         if (timestamp == 0) {
1924                 return SMB_VFS_NEXT_DISK_FREE(handle, path,
1925                                               bsize, dfree, dsize);
1926         }
1927
1928         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1929         TALLOC_FREE(stripped);
1930         if (conv == NULL) {
1931                 return -1;
1932         }
1933
1934         ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, bsize, dfree, dsize);
1935
1936         saved_errno = errno;
1937         TALLOC_FREE(conv);
1938         errno = saved_errno;
1939
1940         return ret;
1941 }
1942
1943 static int shadow_copy2_get_quota(vfs_handle_struct *handle, const char *path,
1944                                   enum SMB_QUOTA_TYPE qtype, unid_t id,
1945                                   SMB_DISK_QUOTA *dq)
1946 {
1947         time_t timestamp;
1948         char *stripped;
1949         int ret;
1950         int saved_errno;
1951         char *conv;
1952
1953         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path, &timestamp,
1954                                          &stripped)) {
1955                 return -1;
1956         }
1957         if (timestamp == 0) {
1958                 return SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, dq);
1959         }
1960
1961         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1962         TALLOC_FREE(stripped);
1963         if (conv == NULL) {
1964                 return -1;
1965         }
1966
1967         ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv, qtype, id, dq);
1968
1969         saved_errno = errno;
1970         TALLOC_FREE(conv);
1971         errno = saved_errno;
1972
1973         return ret;
1974 }
1975
1976 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
1977                                 const char *service, const char *user)
1978 {
1979         struct shadow_copy2_config *config;
1980         int ret;
1981         const char *snapdir;
1982         const char *gmt_format;
1983         const char *sort_order;
1984         const char *basedir = NULL;
1985         const char *snapsharepath = NULL;
1986         const char *mount_point;
1987
1988         DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
1989                    (unsigned)handle->conn->cnum,
1990                    handle->conn->connectpath));
1991
1992         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1993         if (ret < 0) {
1994                 return ret;
1995         }
1996
1997         config = talloc_zero(handle->conn, struct shadow_copy2_config);
1998         if (config == NULL) {
1999                 DEBUG(0, ("talloc_zero() failed\n"));
2000                 errno = ENOMEM;
2001                 return -1;
2002         }
2003
2004         gmt_format = lp_parm_const_string(SNUM(handle->conn),
2005                                           "shadow", "format",
2006                                           GMT_FORMAT);
2007         config->gmt_format = talloc_strdup(config, gmt_format);
2008         if (config->gmt_format == NULL) {
2009                 DEBUG(0, ("talloc_strdup() failed\n"));
2010                 errno = ENOMEM;
2011                 return -1;
2012         }
2013
2014         config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
2015                                           "shadow", "sscanf", false);
2016
2017         config->use_localtime = lp_parm_bool(SNUM(handle->conn),
2018                                              "shadow", "localtime",
2019                                              false);
2020
2021         snapdir = lp_parm_const_string(SNUM(handle->conn),
2022                                        "shadow", "snapdir",
2023                                        ".snapshots");
2024         config->snapdir = talloc_strdup(config, snapdir);
2025         if (config->snapdir == NULL) {
2026                 DEBUG(0, ("talloc_strdup() failed\n"));
2027                 errno = ENOMEM;
2028                 return -1;
2029         }
2030
2031         config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
2032                                                   "shadow",
2033                                                   "snapdirseverywhere",
2034                                                   false);
2035
2036         config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
2037                                                 "shadow", "crossmountpoints",
2038                                                 false);
2039
2040         if (config->crossmountpoints && !config->snapdirseverywhere) {
2041                 DBG_WARNING("Warning: 'crossmountpoints' depends on "
2042                             "'snapdirseverywhere'. Disabling crossmountpoints.\n");
2043         }
2044
2045         config->fixinodes = lp_parm_bool(SNUM(handle->conn),
2046                                          "shadow", "fixinodes",
2047                                          false);
2048
2049         sort_order = lp_parm_const_string(SNUM(handle->conn),
2050                                           "shadow", "sort", "desc");
2051         config->sort_order = talloc_strdup(config, sort_order);
2052         if (config->sort_order == NULL) {
2053                 DEBUG(0, ("talloc_strdup() failed\n"));
2054                 errno = ENOMEM;
2055                 return -1;
2056         }
2057
2058         mount_point = lp_parm_const_string(SNUM(handle->conn),
2059                                            "shadow", "mountpoint", NULL);
2060         if (mount_point != NULL) {
2061                 if (mount_point[0] != '/') {
2062                         DEBUG(1, (__location__ " Warning: 'mountpoint' is "
2063                                   "relative ('%s'), but it has to be an "
2064                                   "absolute path. Ignoring provided value.\n",
2065                                   mount_point));
2066                         mount_point = NULL;
2067                 } else {
2068                         char *p;
2069                         p = strstr(handle->conn->connectpath, mount_point);
2070                         if (p != handle->conn->connectpath) {
2071                                 DBG_WARNING("Warning: the share root (%s) is "
2072                                             "not a subdirectory of the "
2073                                             "specified mountpoint (%s). "
2074                                             "Ignoring provided value.\n",
2075                                             handle->conn->connectpath,
2076                                             mount_point);
2077                                 mount_point = NULL;
2078                         }
2079                 }
2080         }
2081
2082         if (mount_point != NULL) {
2083                 config->mount_point = talloc_strdup(config, mount_point);
2084                 if (config->mount_point == NULL) {
2085                         DEBUG(0, (__location__ " talloc_strdup() failed\n"));
2086                         return -1;
2087                 }
2088         } else {
2089                 config->mount_point = shadow_copy2_find_mount_point(config,
2090                                                                     handle);
2091                 if (config->mount_point == NULL) {
2092                         DBG_WARNING("shadow_copy2_find_mount_point "
2093                                     "of the share root '%s' failed: %s\n",
2094                                     handle->conn->connectpath, strerror(errno));
2095                         return -1;
2096                 }
2097         }
2098
2099         basedir = lp_parm_const_string(SNUM(handle->conn),
2100                                        "shadow", "basedir", NULL);
2101
2102         if (basedir != NULL) {
2103                 if (basedir[0] != '/') {
2104                         DEBUG(1, (__location__ " Warning: 'basedir' is "
2105                                   "relative ('%s'), but it has to be an "
2106                                   "absolute path. Disabling basedir.\n",
2107                                   basedir));
2108                         basedir = NULL;
2109                 } else {
2110                         char *p;
2111                         p = strstr(basedir, config->mount_point);
2112                         if (p != basedir) {
2113                                 DEBUG(1, ("Warning: basedir (%s) is not a "
2114                                           "subdirectory of the share root's "
2115                                           "mount point (%s). "
2116                                           "Disabling basedir\n",
2117                                           basedir, config->mount_point));
2118                                 basedir = NULL;
2119                         }
2120                 }
2121         }
2122
2123         if (config->snapdirseverywhere && basedir != NULL) {
2124                 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
2125                           "with 'snapdirseverywhere'. Disabling basedir.\n"));
2126                 basedir = NULL;
2127         }
2128
2129         snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
2130                                              "snapsharepath", NULL);
2131         if (snapsharepath != NULL) {
2132                 if (snapsharepath[0] == '/') {
2133                         DBG_WARNING("Warning: 'snapsharepath' is "
2134                                     "absolute ('%s'), but it has to be a "
2135                                     "relative path. Disabling snapsharepath.\n",
2136                                     snapsharepath);
2137                         snapsharepath = NULL;
2138                 }
2139                 if (config->snapdirseverywhere && snapsharepath != NULL) {
2140                         DBG_WARNING("Warning: 'snapsharepath' is incompatible "
2141                                     "with 'snapdirseverywhere'. Disabling "
2142                                     "snapsharepath.\n");
2143                         snapsharepath = NULL;
2144                 }
2145         }
2146
2147         if (basedir != NULL && snapsharepath != NULL) {
2148                 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
2149                             "'basedir'. Disabling snapsharepath\n");
2150                 snapsharepath = NULL;
2151         }
2152
2153         if (snapsharepath != NULL) {
2154                 config->rel_connectpath = talloc_strdup(config, snapsharepath);
2155                 if (config->rel_connectpath == NULL) {
2156                         DBG_ERR("talloc_strdup() failed\n");
2157                         errno = ENOMEM;
2158                         return -1;
2159                 }
2160         }
2161
2162         if (basedir == NULL) {
2163                 basedir = config->mount_point;
2164         }
2165
2166         if (config->rel_connectpath == NULL &&
2167             strlen(basedir) != strlen(handle->conn->connectpath)) {
2168                 config->rel_connectpath = talloc_strdup(config,
2169                         handle->conn->connectpath + strlen(basedir));
2170                 if (config->rel_connectpath == NULL) {
2171                         DEBUG(0, ("talloc_strdup() failed\n"));
2172                         errno = ENOMEM;
2173                         return -1;
2174                 }
2175         }
2176
2177         if (config->snapdir[0] == '/') {
2178                 config->snapdir_absolute = true;
2179
2180                 if (config->snapdirseverywhere == true) {
2181                         DEBUG(1, (__location__ " Warning: An absolute snapdir "
2182                                   "is incompatible with 'snapdirseverywhere', "
2183                                   "setting 'snapdirseverywhere' to false.\n"));
2184                         config->snapdirseverywhere = false;
2185                 }
2186
2187                 if (config->crossmountpoints == true) {
2188                         DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
2189                                   "is not supported with an absolute snapdir. "
2190                                   "Disabling it.\n"));
2191                         config->crossmountpoints = false;
2192                 }
2193
2194                 config->snapshot_basepath = config->snapdir;
2195         } else {
2196                 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
2197                                 config->mount_point, config->snapdir);
2198                 if (config->snapshot_basepath == NULL) {
2199                         DEBUG(0, ("talloc_asprintf() failed\n"));
2200                         errno = ENOMEM;
2201                         return -1;
2202                 }
2203         }
2204
2205         DEBUG(10, ("shadow_copy2_connect: configuration:\n"
2206                    "  share root: '%s'\n"
2207                    "  mountpoint: '%s'\n"
2208                    "  rel share root: '%s'\n"
2209                    "  snapdir: '%s'\n"
2210                    "  snapshot base path: '%s'\n"
2211                    "  format: '%s'\n"
2212                    "  use sscanf: %s\n"
2213                    "  snapdirs everywhere: %s\n"
2214                    "  cross mountpoints: %s\n"
2215                    "  fix inodes: %s\n"
2216                    "  sort order: %s\n"
2217                    "",
2218                    handle->conn->connectpath,
2219                    config->mount_point,
2220                    config->rel_connectpath,
2221                    config->snapdir,
2222                    config->snapshot_basepath,
2223                    config->gmt_format,
2224                    config->use_sscanf ? "yes" : "no",
2225                    config->snapdirseverywhere ? "yes" : "no",
2226                    config->crossmountpoints ? "yes" : "no",
2227                    config->fixinodes ? "yes" : "no",
2228                    config->sort_order
2229                    ));
2230
2231
2232         SMB_VFS_HANDLE_SET_DATA(handle, config,
2233                                 NULL, struct shadow_copy2_config,
2234                                 return -1);
2235
2236         return 0;
2237 }
2238
2239 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
2240         .connect_fn = shadow_copy2_connect,
2241         .opendir_fn = shadow_copy2_opendir,
2242         .disk_free_fn = shadow_copy2_disk_free,
2243         .get_quota_fn = shadow_copy2_get_quota,
2244         .rename_fn = shadow_copy2_rename,
2245         .link_fn = shadow_copy2_link,
2246         .symlink_fn = shadow_copy2_symlink,
2247         .stat_fn = shadow_copy2_stat,
2248         .lstat_fn = shadow_copy2_lstat,
2249         .fstat_fn = shadow_copy2_fstat,
2250         .open_fn = shadow_copy2_open,
2251         .unlink_fn = shadow_copy2_unlink,
2252         .chmod_fn = shadow_copy2_chmod,
2253         .chown_fn = shadow_copy2_chown,
2254         .chdir_fn = shadow_copy2_chdir,
2255         .ntimes_fn = shadow_copy2_ntimes,
2256         .readlink_fn = shadow_copy2_readlink,
2257         .mknod_fn = shadow_copy2_mknod,
2258         .realpath_fn = shadow_copy2_realpath,
2259         .get_nt_acl_fn = shadow_copy2_get_nt_acl,
2260         .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
2261         .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
2262         .mkdir_fn = shadow_copy2_mkdir,
2263         .rmdir_fn = shadow_copy2_rmdir,
2264         .getxattr_fn = shadow_copy2_getxattr,
2265         .listxattr_fn = shadow_copy2_listxattr,
2266         .removexattr_fn = shadow_copy2_removexattr,
2267         .setxattr_fn = shadow_copy2_setxattr,
2268         .chmod_acl_fn = shadow_copy2_chmod_acl,
2269         .chflags_fn = shadow_copy2_chflags,
2270         .get_real_filename_fn = shadow_copy2_get_real_filename,
2271         .connectpath_fn = shadow_copy2_connectpath,
2272 };
2273
2274 NTSTATUS vfs_shadow_copy2_init(void);
2275 NTSTATUS vfs_shadow_copy2_init(void)
2276 {
2277         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2278                                 "shadow_copy2", &vfs_shadow_copy2_fns);
2279 }