VFS: Modify chown to take a const struct smb_filename * instead of const char *
[amitay/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,
973                         const struct smb_filename *smb_fname,
974                         mode_t mode)
975 {
976         time_t timestamp;
977         char *stripped = NULL;
978         int ret, saved_errno;
979         char *conv = NULL;
980         struct smb_filename *conv_smb_fname;
981
982         if (!shadow_copy2_strip_snapshot(talloc_tos(),
983                                 handle,
984                                 smb_fname->base_name,
985                                 &timestamp,
986                                 &stripped)) {
987                 return -1;
988         }
989         if (timestamp == 0) {
990                 TALLOC_FREE(stripped);
991                 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
992         }
993         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
994         TALLOC_FREE(stripped);
995         if (conv == NULL) {
996                 return -1;
997         }
998         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
999                                         conv,
1000                                         NULL,
1001                                         NULL);
1002         if (conv_smb_fname == NULL) {
1003                 TALLOC_FREE(conv);
1004                 errno = ENOMEM;
1005                 return -1;
1006         }
1007
1008         ret = SMB_VFS_NEXT_CHMOD(handle, conv_smb_fname, mode);
1009         saved_errno = errno;
1010         TALLOC_FREE(conv);
1011         TALLOC_FREE(conv_smb_fname);
1012         errno = saved_errno;
1013         return ret;
1014 }
1015
1016 static int shadow_copy2_chown(vfs_handle_struct *handle,
1017                         const struct smb_filename *smb_fname,
1018                         uid_t uid,
1019                         gid_t gid)
1020 {
1021         time_t timestamp;
1022         char *stripped;
1023         int ret, saved_errno;
1024         char *conv = NULL;
1025         struct smb_filename *conv_smb_fname = NULL;
1026
1027         if (!shadow_copy2_strip_snapshot(talloc_tos(),
1028                                 handle,
1029                                 smb_fname->base_name,
1030                                 &timestamp,
1031                                 &stripped)) {
1032                 return -1;
1033         }
1034         if (timestamp == 0) {
1035                 return SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
1036         }
1037         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1038         TALLOC_FREE(stripped);
1039         if (conv == NULL) {
1040                 return -1;
1041         }
1042         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1043                                         conv,
1044                                         NULL,
1045                                         NULL);
1046         if (conv_smb_fname == NULL) {
1047                 TALLOC_FREE(conv);
1048                 errno = ENOMEM;
1049                 return -1;
1050         }
1051         ret = SMB_VFS_NEXT_CHOWN(handle, conv_smb_fname, uid, gid);
1052         saved_errno = errno;
1053         TALLOC_FREE(conv);
1054         TALLOC_FREE(conv_smb_fname);
1055         errno = saved_errno;
1056         return ret;
1057 }
1058
1059 static int shadow_copy2_chdir(vfs_handle_struct *handle,
1060                               const char *fname)
1061 {
1062         time_t timestamp;
1063         char *stripped;
1064         int ret, saved_errno;
1065         char *conv;
1066
1067         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1068                                          &timestamp, &stripped)) {
1069                 return -1;
1070         }
1071         if (timestamp == 0) {
1072                 return SMB_VFS_NEXT_CHDIR(handle, fname);
1073         }
1074         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1075         TALLOC_FREE(stripped);
1076         if (conv == NULL) {
1077                 return -1;
1078         }
1079         ret = SMB_VFS_NEXT_CHDIR(handle, conv);
1080         saved_errno = errno;
1081         TALLOC_FREE(conv);
1082         errno = saved_errno;
1083         return ret;
1084 }
1085
1086 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
1087                                const struct smb_filename *smb_fname,
1088                                struct smb_file_time *ft)
1089 {
1090         time_t timestamp;
1091         char *stripped;
1092         int ret, saved_errno;
1093         struct smb_filename *conv;
1094
1095         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1096                                          smb_fname->base_name,
1097                                          &timestamp, &stripped)) {
1098                 return -1;
1099         }
1100         if (timestamp == 0) {
1101                 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1102         }
1103         conv = cp_smb_filename(talloc_tos(), smb_fname);
1104         if (conv == NULL) {
1105                 errno = ENOMEM;
1106                 return -1;
1107         }
1108         conv->base_name = shadow_copy2_convert(
1109                 conv, handle, stripped, timestamp);
1110         TALLOC_FREE(stripped);
1111         if (conv->base_name == NULL) {
1112                 return -1;
1113         }
1114         ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
1115         saved_errno = errno;
1116         TALLOC_FREE(conv);
1117         errno = saved_errno;
1118         return ret;
1119 }
1120
1121 static int shadow_copy2_readlink(vfs_handle_struct *handle,
1122                                  const char *fname, char *buf, size_t bufsiz)
1123 {
1124         time_t timestamp;
1125         char *stripped;
1126         int ret, saved_errno;
1127         char *conv;
1128
1129         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1130                                          &timestamp, &stripped)) {
1131                 return -1;
1132         }
1133         if (timestamp == 0) {
1134                 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
1135         }
1136         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1137         TALLOC_FREE(stripped);
1138         if (conv == NULL) {
1139                 return -1;
1140         }
1141         ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1142         saved_errno = errno;
1143         TALLOC_FREE(conv);
1144         errno = saved_errno;
1145         return ret;
1146 }
1147
1148 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1149                               const char *fname, mode_t mode, SMB_DEV_T dev)
1150 {
1151         time_t timestamp;
1152         char *stripped;
1153         int ret, saved_errno;
1154         char *conv;
1155
1156         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1157                                          &timestamp, &stripped)) {
1158                 return -1;
1159         }
1160         if (timestamp == 0) {
1161                 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
1162         }
1163         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1164         TALLOC_FREE(stripped);
1165         if (conv == NULL) {
1166                 return -1;
1167         }
1168         ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1169         saved_errno = errno;
1170         TALLOC_FREE(conv);
1171         errno = saved_errno;
1172         return ret;
1173 }
1174
1175 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
1176                                    const char *fname)
1177 {
1178         time_t timestamp;
1179         char *stripped = NULL;
1180         char *tmp = NULL;
1181         char *result = NULL;
1182         int saved_errno;
1183
1184         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1185                                          &timestamp, &stripped)) {
1186                 goto done;
1187         }
1188         if (timestamp == 0) {
1189                 return SMB_VFS_NEXT_REALPATH(handle, fname);
1190         }
1191
1192         tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1193         if (tmp == NULL) {
1194                 goto done;
1195         }
1196
1197         result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1198
1199 done:
1200         saved_errno = errno;
1201         TALLOC_FREE(tmp);
1202         TALLOC_FREE(stripped);
1203         errno = saved_errno;
1204         return result;
1205 }
1206
1207 /**
1208  * Check whether a given directory contains a
1209  * snapshot directory as direct subdirectory.
1210  * If yes, return the path of the snapshot-subdir,
1211  * otherwise return NULL.
1212  */
1213 static char *have_snapdir(struct vfs_handle_struct *handle,
1214                           const char *path)
1215 {
1216         struct smb_filename smb_fname;
1217         int ret;
1218         struct shadow_copy2_config *config;
1219
1220         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1221                                 return NULL);
1222
1223         ZERO_STRUCT(smb_fname);
1224         smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1225                                               path, config->snapdir);
1226         if (smb_fname.base_name == NULL) {
1227                 return NULL;
1228         }
1229
1230         ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1231         if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1232                 return smb_fname.base_name;
1233         }
1234         TALLOC_FREE(smb_fname.base_name);
1235         return NULL;
1236 }
1237
1238 static bool check_access_snapdir(struct vfs_handle_struct *handle,
1239                                 const char *path)
1240 {
1241         struct smb_filename smb_fname;
1242         int ret;
1243         NTSTATUS status;
1244
1245         ZERO_STRUCT(smb_fname);
1246         smb_fname.base_name = talloc_asprintf(talloc_tos(),
1247                                                 "%s",
1248                                                 path);
1249         if (smb_fname.base_name == NULL) {
1250                 return false;
1251         }
1252
1253         ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1254         if (ret != 0 || !S_ISDIR(smb_fname.st.st_ex_mode)) {
1255                 TALLOC_FREE(smb_fname.base_name);
1256                 return false;
1257         }
1258
1259         status = smbd_check_access_rights(handle->conn,
1260                                         &smb_fname,
1261                                         false,
1262                                         SEC_DIR_LIST);
1263         if (!NT_STATUS_IS_OK(status)) {
1264                 DEBUG(0,("user does not have list permission "
1265                         "on snapdir %s\n",
1266                         smb_fname.base_name));
1267                 TALLOC_FREE(smb_fname.base_name);
1268                 return false;
1269         }
1270         TALLOC_FREE(smb_fname.base_name);
1271         return true;
1272 }
1273
1274 /**
1275  * Find the snapshot directory (if any) for the given
1276  * filename (which is relative to the share).
1277  */
1278 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1279                                              struct vfs_handle_struct *handle,
1280                                              struct smb_filename *smb_fname)
1281 {
1282         char *path, *p;
1283         const char *snapdir;
1284         struct shadow_copy2_config *config;
1285
1286         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1287                                 return NULL);
1288
1289         /*
1290          * If the non-snapdisrseverywhere mode, we should not search!
1291          */
1292         if (!config->snapdirseverywhere) {
1293                 return config->snapshot_basepath;
1294         }
1295
1296         path = talloc_asprintf(mem_ctx, "%s/%s",
1297                                handle->conn->connectpath,
1298                                smb_fname->base_name);
1299         if (path == NULL) {
1300                 return NULL;
1301         }
1302
1303         snapdir = have_snapdir(handle, path);
1304         if (snapdir != NULL) {
1305                 TALLOC_FREE(path);
1306                 return snapdir;
1307         }
1308
1309         while ((p = strrchr(path, '/')) && (p > path)) {
1310
1311                 p[0] = '\0';
1312
1313                 snapdir = have_snapdir(handle, path);
1314                 if (snapdir != NULL) {
1315                         TALLOC_FREE(path);
1316                         return snapdir;
1317                 }
1318         }
1319         TALLOC_FREE(path);
1320         return NULL;
1321 }
1322
1323 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1324                                          const char *name,
1325                                          char *gmt, size_t gmt_len)
1326 {
1327         struct tm timestamp;
1328         time_t timestamp_t;
1329         unsigned long int timestamp_long;
1330         const char *fmt;
1331         struct shadow_copy2_config *config;
1332
1333         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1334                                 return NULL);
1335
1336         fmt = config->gmt_format;
1337
1338         ZERO_STRUCT(timestamp);
1339         if (config->use_sscanf) {
1340                 if (sscanf(name, fmt, &timestamp_long) != 1) {
1341                         DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1342                                    "no sscanf match %s: %s\n",
1343                                    fmt, name));
1344                         return false;
1345                 }
1346                 timestamp_t = timestamp_long;
1347                 gmtime_r(&timestamp_t, &timestamp);
1348         } else {
1349                 if (strptime(name, fmt, &timestamp) == NULL) {
1350                         DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1351                                    "no match %s: %s\n",
1352                                    fmt, name));
1353                         return false;
1354                 }
1355                 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1356                            fmt, name));
1357                 
1358                 if (config->use_localtime) {
1359                         timestamp.tm_isdst = -1;
1360                         timestamp_t = mktime(&timestamp);
1361                         gmtime_r(&timestamp_t, &timestamp);
1362                 }
1363         }
1364
1365         strftime(gmt, gmt_len, GMT_FORMAT, &timestamp);
1366         return true;
1367 }
1368
1369 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1370 {
1371         return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1372 }
1373
1374 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1375 {
1376         return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1377 }
1378
1379 /*
1380   sort the shadow copy data in ascending or descending order
1381  */
1382 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1383                                    struct shadow_copy_data *shadow_copy2_data)
1384 {
1385         int (*cmpfunc)(const void *, const void *);
1386         const char *sort;
1387         struct shadow_copy2_config *config;
1388
1389         SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1390                                 return);
1391
1392         sort = config->sort_order;
1393         if (sort == NULL) {
1394                 return;
1395         }
1396
1397         if (strcmp(sort, "asc") == 0) {
1398                 cmpfunc = shadow_copy2_label_cmp_asc;
1399         } else if (strcmp(sort, "desc") == 0) {
1400                 cmpfunc = shadow_copy2_label_cmp_desc;
1401         } else {
1402                 return;
1403         }
1404
1405         if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1406             shadow_copy2_data->labels)
1407         {
1408                 TYPESAFE_QSORT(shadow_copy2_data->labels,
1409                                shadow_copy2_data->num_volumes,
1410                                cmpfunc);
1411         }
1412 }
1413
1414 static int shadow_copy2_get_shadow_copy_data(
1415         vfs_handle_struct *handle, files_struct *fsp,
1416         struct shadow_copy_data *shadow_copy2_data,
1417         bool labels)
1418 {
1419         DIR *p;
1420         const char *snapdir;
1421         struct smb_filename *snapdir_smb_fname = NULL;
1422         struct dirent *d;
1423         TALLOC_CTX *tmp_ctx = talloc_stackframe();
1424         bool ret;
1425
1426         snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1427         if (snapdir == NULL) {
1428                 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1429                          handle->conn->connectpath));
1430                 errno = EINVAL;
1431                 talloc_free(tmp_ctx);
1432                 return -1;
1433         }
1434         ret = check_access_snapdir(handle, snapdir);
1435         if (!ret) {
1436                 DEBUG(0,("access denied on listing snapdir %s\n", snapdir));
1437                 errno = EACCES;
1438                 talloc_free(tmp_ctx);
1439                 return -1;
1440         }
1441
1442         snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
1443                                         snapdir,
1444                                         NULL,
1445                                         NULL);
1446         if (snapdir_smb_fname == NULL) {
1447                 errno = ENOMEM;
1448                 talloc_free(tmp_ctx);
1449                 return -1;
1450         }
1451
1452         p = SMB_VFS_NEXT_OPENDIR(handle, snapdir_smb_fname, NULL, 0);
1453
1454         if (!p) {
1455                 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1456                          " - %s\n", snapdir, strerror(errno)));
1457                 talloc_free(tmp_ctx);
1458                 errno = ENOSYS;
1459                 return -1;
1460         }
1461
1462         shadow_copy2_data->num_volumes = 0;
1463         shadow_copy2_data->labels      = NULL;
1464
1465         while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
1466                 char snapshot[GMT_NAME_LEN+1];
1467                 SHADOW_COPY_LABEL *tlabels;
1468
1469                 /*
1470                  * ignore names not of the right form in the snapshot
1471                  * directory
1472                  */
1473                 if (!shadow_copy2_snapshot_to_gmt(
1474                             handle, d->d_name,
1475                             snapshot, sizeof(snapshot))) {
1476
1477                         DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1478                                   "ignoring %s\n", d->d_name));
1479                         continue;
1480                 }
1481                 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1482                          d->d_name, snapshot));
1483
1484                 if (!labels) {
1485                         /* the caller doesn't want the labels */
1486                         shadow_copy2_data->num_volumes++;
1487                         continue;
1488                 }
1489
1490                 tlabels = talloc_realloc(shadow_copy2_data,
1491                                          shadow_copy2_data->labels,
1492                                          SHADOW_COPY_LABEL,
1493                                          shadow_copy2_data->num_volumes+1);
1494                 if (tlabels == NULL) {
1495                         DEBUG(0,("shadow_copy2: out of memory\n"));
1496                         SMB_VFS_NEXT_CLOSEDIR(handle, p);
1497                         talloc_free(tmp_ctx);
1498                         return -1;
1499                 }
1500
1501                 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
1502                         sizeof(*tlabels));
1503
1504                 shadow_copy2_data->num_volumes++;
1505                 shadow_copy2_data->labels = tlabels;
1506         }
1507
1508         SMB_VFS_NEXT_CLOSEDIR(handle,p);
1509
1510         shadow_copy2_sort_data(handle, shadow_copy2_data);
1511
1512         talloc_free(tmp_ctx);
1513         return 0;
1514 }
1515
1516 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
1517                                         struct files_struct *fsp,
1518                                         uint32_t security_info,
1519                                          TALLOC_CTX *mem_ctx,
1520                                         struct security_descriptor **ppdesc)
1521 {
1522         time_t timestamp;
1523         char *stripped;
1524         NTSTATUS status;
1525         char *conv;
1526         struct smb_filename *smb_fname = NULL;
1527
1528         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1529                                          fsp->fsp_name->base_name,
1530                                          &timestamp, &stripped)) {
1531                 return map_nt_error_from_unix(errno);
1532         }
1533         if (timestamp == 0) {
1534                 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1535                                                 mem_ctx,
1536                                                 ppdesc);
1537         }
1538         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1539         TALLOC_FREE(stripped);
1540         if (conv == NULL) {
1541                 return map_nt_error_from_unix(errno);
1542         }
1543         smb_fname = synthetic_smb_fname(talloc_tos(),
1544                                         conv,
1545                                         NULL,
1546                                         NULL);
1547         if (smb_fname == NULL) {
1548                 TALLOC_FREE(conv);
1549                 return NT_STATUS_NO_MEMORY;
1550         }
1551
1552         status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1553                                          mem_ctx, ppdesc);
1554         TALLOC_FREE(conv);
1555         TALLOC_FREE(smb_fname);
1556         return status;
1557 }
1558
1559 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
1560                                         const struct smb_filename *smb_fname,
1561                                         uint32_t security_info,
1562                                         TALLOC_CTX *mem_ctx,
1563                                         struct security_descriptor **ppdesc)
1564 {
1565         time_t timestamp;
1566         char *stripped;
1567         NTSTATUS status;
1568         char *conv;
1569         struct smb_filename *conv_smb_fname = NULL;
1570
1571         if (!shadow_copy2_strip_snapshot(talloc_tos(),
1572                                         handle,
1573                                         smb_fname->base_name,
1574                                         &timestamp,
1575                                         &stripped)) {
1576                 return map_nt_error_from_unix(errno);
1577         }
1578         if (timestamp == 0) {
1579                 return SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1580                                                mem_ctx, ppdesc);
1581         }
1582         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1583         TALLOC_FREE(stripped);
1584         if (conv == NULL) {
1585                 return map_nt_error_from_unix(errno);
1586         }
1587         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1588                                         conv,
1589                                         NULL,
1590                                         NULL);
1591         if (conv_smb_fname == NULL) {
1592                 TALLOC_FREE(conv);
1593                 return NT_STATUS_NO_MEMORY;
1594         }
1595         status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv_smb_fname, security_info,
1596                                          mem_ctx, ppdesc);
1597         TALLOC_FREE(conv);
1598         TALLOC_FREE(conv_smb_fname);
1599         return status;
1600 }
1601
1602 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
1603                                 const struct smb_filename *smb_fname,
1604                                 mode_t mode)
1605 {
1606         time_t timestamp;
1607         char *stripped;
1608         int ret, saved_errno;
1609         char *conv;
1610         struct smb_filename *conv_smb_fname = NULL;
1611
1612         if (!shadow_copy2_strip_snapshot(talloc_tos(),
1613                                         handle,
1614                                         smb_fname->base_name,
1615                                         &timestamp,
1616                                         &stripped)) {
1617                 return -1;
1618         }
1619         if (timestamp == 0) {
1620                 return SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
1621         }
1622         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1623         TALLOC_FREE(stripped);
1624         if (conv == NULL) {
1625                 return -1;
1626         }
1627         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1628                                         conv,
1629                                         NULL,
1630                                         NULL);
1631         if (conv_smb_fname == NULL) {
1632                 TALLOC_FREE(conv);
1633                 return -1;
1634         }
1635         ret = SMB_VFS_NEXT_MKDIR(handle, conv_smb_fname, mode);
1636         saved_errno = errno;
1637         TALLOC_FREE(conv);
1638         TALLOC_FREE(conv_smb_fname);
1639         errno = saved_errno;
1640         return ret;
1641 }
1642
1643 static int shadow_copy2_rmdir(vfs_handle_struct *handle,
1644                                 const struct smb_filename *smb_fname)
1645 {
1646         time_t timestamp;
1647         char *stripped;
1648         int ret, saved_errno;
1649         char *conv;
1650         struct smb_filename *conv_smb_fname = NULL;
1651
1652         if (!shadow_copy2_strip_snapshot(talloc_tos(),
1653                                         handle,
1654                                         smb_fname->base_name,
1655                                         &timestamp,
1656                                         &stripped)) {
1657                 return -1;
1658         }
1659         if (timestamp == 0) {
1660                 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
1661         }
1662         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1663         TALLOC_FREE(stripped);
1664         if (conv == NULL) {
1665                 return -1;
1666         }
1667         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1668                                         conv,
1669                                         NULL,
1670                                         NULL);
1671         if (conv_smb_fname == NULL) {
1672                 TALLOC_FREE(conv);
1673                 return -1;
1674         }
1675         ret = SMB_VFS_NEXT_RMDIR(handle, conv_smb_fname);
1676         saved_errno = errno;
1677         TALLOC_FREE(conv_smb_fname);
1678         TALLOC_FREE(conv);
1679         errno = saved_errno;
1680         return ret;
1681 }
1682
1683 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
1684                                 unsigned int flags)
1685 {
1686         time_t timestamp;
1687         char *stripped;
1688         int ret, saved_errno;
1689         char *conv;
1690
1691         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1692                                          &timestamp, &stripped)) {
1693                 return -1;
1694         }
1695         if (timestamp == 0) {
1696                 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
1697         }
1698         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1699         TALLOC_FREE(stripped);
1700         if (conv == NULL) {
1701                 return -1;
1702         }
1703         ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
1704         saved_errno = errno;
1705         TALLOC_FREE(conv);
1706         errno = saved_errno;
1707         return ret;
1708 }
1709
1710 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
1711                                      const char *fname, const char *aname,
1712                                      void *value, size_t size)
1713 {
1714         time_t timestamp;
1715         char *stripped;
1716         ssize_t ret;
1717         int saved_errno;
1718         char *conv;
1719
1720         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1721                                          &timestamp, &stripped)) {
1722                 return -1;
1723         }
1724         if (timestamp == 0) {
1725                 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
1726                                              size);
1727         }
1728         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1729         TALLOC_FREE(stripped);
1730         if (conv == NULL) {
1731                 return -1;
1732         }
1733         ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
1734         saved_errno = errno;
1735         TALLOC_FREE(conv);
1736         errno = saved_errno;
1737         return ret;
1738 }
1739
1740 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
1741                                       const char *fname,
1742                                       char *list, size_t size)
1743 {
1744         time_t timestamp;
1745         char *stripped;
1746         ssize_t ret;
1747         int saved_errno;
1748         char *conv;
1749
1750         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1751                                          &timestamp, &stripped)) {
1752                 return -1;
1753         }
1754         if (timestamp == 0) {
1755                 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
1756         }
1757         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1758         TALLOC_FREE(stripped);
1759         if (conv == NULL) {
1760                 return -1;
1761         }
1762         ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
1763         saved_errno = errno;
1764         TALLOC_FREE(conv);
1765         errno = saved_errno;
1766         return ret;
1767 }
1768
1769 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
1770                                     const char *fname, const char *aname)
1771 {
1772         time_t timestamp;
1773         char *stripped;
1774         int ret, saved_errno;
1775         char *conv;
1776
1777         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1778                                          &timestamp, &stripped)) {
1779                 return -1;
1780         }
1781         if (timestamp == 0) {
1782                 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
1783         }
1784         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1785         TALLOC_FREE(stripped);
1786         if (conv == NULL) {
1787                 return -1;
1788         }
1789         ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
1790         saved_errno = errno;
1791         TALLOC_FREE(conv);
1792         errno = saved_errno;
1793         return ret;
1794 }
1795
1796 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
1797                                  const char *fname,
1798                                  const char *aname, const void *value,
1799                                  size_t size, int flags)
1800 {
1801         time_t timestamp;
1802         char *stripped;
1803         ssize_t ret;
1804         int saved_errno;
1805         char *conv;
1806
1807         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1808                                          &timestamp, &stripped)) {
1809                 return -1;
1810         }
1811         if (timestamp == 0) {
1812                 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
1813                                              flags);
1814         }
1815         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1816         TALLOC_FREE(stripped);
1817         if (conv == NULL) {
1818                 return -1;
1819         }
1820         ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
1821         saved_errno = errno;
1822         TALLOC_FREE(conv);
1823         errno = saved_errno;
1824         return ret;
1825 }
1826
1827 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
1828                         const struct smb_filename *smb_fname,
1829                         mode_t mode)
1830 {
1831         time_t timestamp;
1832         char *stripped;
1833         ssize_t ret;
1834         int saved_errno;
1835         char *conv = NULL;
1836         struct smb_filename *conv_smb_fname = NULL;
1837
1838         if (!shadow_copy2_strip_snapshot(talloc_tos(),
1839                                 handle,
1840                                 smb_fname->base_name,
1841                                 &timestamp,
1842                                 &stripped)) {
1843                 return -1;
1844         }
1845         if (timestamp == 0) {
1846                 return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
1847         }
1848         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1849         TALLOC_FREE(stripped);
1850         if (conv == NULL) {
1851                 return -1;
1852         }
1853         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1854                                         conv,
1855                                         NULL,
1856                                         NULL);
1857         if (conv_smb_fname == NULL) {
1858                 TALLOC_FREE(conv);
1859                 errno = ENOMEM;
1860                 return -1;
1861         }
1862         ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv_smb_fname, mode);
1863         saved_errno = errno;
1864         TALLOC_FREE(conv);
1865         TALLOC_FREE(conv_smb_fname);
1866         errno = saved_errno;
1867         return ret;
1868 }
1869
1870 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
1871                                           const char *path,
1872                                           const char *name,
1873                                           TALLOC_CTX *mem_ctx,
1874                                           char **found_name)
1875 {
1876         time_t timestamp;
1877         char *stripped;
1878         ssize_t ret;
1879         int saved_errno;
1880         char *conv;
1881
1882         DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
1883                    "name=[%s]\n", path, name));
1884
1885         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1886                                          &timestamp, &stripped)) {
1887                 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
1888                 return -1;
1889         }
1890         if (timestamp == 0) {
1891                 DEBUG(10, ("timestamp == 0\n"));
1892                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
1893                                                       mem_ctx, found_name);
1894         }
1895         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1896         TALLOC_FREE(stripped);
1897         if (conv == NULL) {
1898                 DEBUG(10, ("shadow_copy2_convert failed\n"));
1899                 return -1;
1900         }
1901         DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
1902                    "name=[%s]\n", conv, name));
1903         ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
1904                                              mem_ctx, found_name);
1905         DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
1906         saved_errno = errno;
1907         TALLOC_FREE(conv);
1908         errno = saved_errno;
1909         return ret;
1910 }
1911
1912 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
1913                                             const char *fname)
1914 {
1915         time_t timestamp;
1916         char *stripped = NULL;
1917         char *tmp = NULL;
1918         char *result = NULL;
1919         int saved_errno;
1920         size_t rootpath_len = 0;
1921
1922         DBG_DEBUG("Calc connect path for [%s]\n", fname);
1923
1924         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1925                                          &timestamp, &stripped)) {
1926                 goto done;
1927         }
1928         if (timestamp == 0) {
1929                 return SMB_VFS_NEXT_CONNECTPATH(handle, fname);
1930         }
1931
1932         tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
1933                                       &rootpath_len);
1934         if (tmp == NULL) {
1935                 goto done;
1936         }
1937
1938         DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
1939                   (int)rootpath_len, tmp);
1940
1941         tmp[rootpath_len] = '\0';
1942         result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1943         if (result == NULL) {
1944                 goto done;
1945         }
1946
1947         DBG_DEBUG("connect path is [%s]\n", result);
1948
1949 done:
1950         saved_errno = errno;
1951         TALLOC_FREE(tmp);
1952         TALLOC_FREE(stripped);
1953         errno = saved_errno;
1954         return result;
1955 }
1956
1957 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
1958                                        const char *path, uint64_t *bsize,
1959                                        uint64_t *dfree, uint64_t *dsize)
1960 {
1961         time_t timestamp;
1962         char *stripped;
1963         ssize_t ret;
1964         int saved_errno;
1965         char *conv;
1966
1967         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1968                                          &timestamp, &stripped)) {
1969                 return -1;
1970         }
1971         if (timestamp == 0) {
1972                 return SMB_VFS_NEXT_DISK_FREE(handle, path,
1973                                               bsize, dfree, dsize);
1974         }
1975
1976         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1977         TALLOC_FREE(stripped);
1978         if (conv == NULL) {
1979                 return -1;
1980         }
1981
1982         ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, bsize, dfree, dsize);
1983
1984         saved_errno = errno;
1985         TALLOC_FREE(conv);
1986         errno = saved_errno;
1987
1988         return ret;
1989 }
1990
1991 static int shadow_copy2_get_quota(vfs_handle_struct *handle, const char *path,
1992                                   enum SMB_QUOTA_TYPE qtype, unid_t id,
1993                                   SMB_DISK_QUOTA *dq)
1994 {
1995         time_t timestamp;
1996         char *stripped;
1997         int ret;
1998         int saved_errno;
1999         char *conv;
2000
2001         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path, &timestamp,
2002                                          &stripped)) {
2003                 return -1;
2004         }
2005         if (timestamp == 0) {
2006                 return SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, dq);
2007         }
2008
2009         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2010         TALLOC_FREE(stripped);
2011         if (conv == NULL) {
2012                 return -1;
2013         }
2014
2015         ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv, qtype, id, dq);
2016
2017         saved_errno = errno;
2018         TALLOC_FREE(conv);
2019         errno = saved_errno;
2020
2021         return ret;
2022 }
2023
2024 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
2025                                 const char *service, const char *user)
2026 {
2027         struct shadow_copy2_config *config;
2028         int ret;
2029         const char *snapdir;
2030         const char *gmt_format;
2031         const char *sort_order;
2032         const char *basedir = NULL;
2033         const char *snapsharepath = NULL;
2034         const char *mount_point;
2035
2036         DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
2037                    (unsigned)handle->conn->cnum,
2038                    handle->conn->connectpath));
2039
2040         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
2041         if (ret < 0) {
2042                 return ret;
2043         }
2044
2045         config = talloc_zero(handle->conn, struct shadow_copy2_config);
2046         if (config == NULL) {
2047                 DEBUG(0, ("talloc_zero() failed\n"));
2048                 errno = ENOMEM;
2049                 return -1;
2050         }
2051
2052         gmt_format = lp_parm_const_string(SNUM(handle->conn),
2053                                           "shadow", "format",
2054                                           GMT_FORMAT);
2055         config->gmt_format = talloc_strdup(config, gmt_format);
2056         if (config->gmt_format == NULL) {
2057                 DEBUG(0, ("talloc_strdup() failed\n"));
2058                 errno = ENOMEM;
2059                 return -1;
2060         }
2061
2062         config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
2063                                           "shadow", "sscanf", false);
2064
2065         config->use_localtime = lp_parm_bool(SNUM(handle->conn),
2066                                              "shadow", "localtime",
2067                                              false);
2068
2069         snapdir = lp_parm_const_string(SNUM(handle->conn),
2070                                        "shadow", "snapdir",
2071                                        ".snapshots");
2072         config->snapdir = talloc_strdup(config, snapdir);
2073         if (config->snapdir == NULL) {
2074                 DEBUG(0, ("talloc_strdup() failed\n"));
2075                 errno = ENOMEM;
2076                 return -1;
2077         }
2078
2079         config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
2080                                                   "shadow",
2081                                                   "snapdirseverywhere",
2082                                                   false);
2083
2084         config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
2085                                                 "shadow", "crossmountpoints",
2086                                                 false);
2087
2088         if (config->crossmountpoints && !config->snapdirseverywhere) {
2089                 DBG_WARNING("Warning: 'crossmountpoints' depends on "
2090                             "'snapdirseverywhere'. Disabling crossmountpoints.\n");
2091         }
2092
2093         config->fixinodes = lp_parm_bool(SNUM(handle->conn),
2094                                          "shadow", "fixinodes",
2095                                          false);
2096
2097         sort_order = lp_parm_const_string(SNUM(handle->conn),
2098                                           "shadow", "sort", "desc");
2099         config->sort_order = talloc_strdup(config, sort_order);
2100         if (config->sort_order == NULL) {
2101                 DEBUG(0, ("talloc_strdup() failed\n"));
2102                 errno = ENOMEM;
2103                 return -1;
2104         }
2105
2106         mount_point = lp_parm_const_string(SNUM(handle->conn),
2107                                            "shadow", "mountpoint", NULL);
2108         if (mount_point != NULL) {
2109                 if (mount_point[0] != '/') {
2110                         DEBUG(1, (__location__ " Warning: 'mountpoint' is "
2111                                   "relative ('%s'), but it has to be an "
2112                                   "absolute path. Ignoring provided value.\n",
2113                                   mount_point));
2114                         mount_point = NULL;
2115                 } else {
2116                         char *p;
2117                         p = strstr(handle->conn->connectpath, mount_point);
2118                         if (p != handle->conn->connectpath) {
2119                                 DBG_WARNING("Warning: the share root (%s) is "
2120                                             "not a subdirectory of the "
2121                                             "specified mountpoint (%s). "
2122                                             "Ignoring provided value.\n",
2123                                             handle->conn->connectpath,
2124                                             mount_point);
2125                                 mount_point = NULL;
2126                         }
2127                 }
2128         }
2129
2130         if (mount_point != NULL) {
2131                 config->mount_point = talloc_strdup(config, mount_point);
2132                 if (config->mount_point == NULL) {
2133                         DEBUG(0, (__location__ " talloc_strdup() failed\n"));
2134                         return -1;
2135                 }
2136         } else {
2137                 config->mount_point = shadow_copy2_find_mount_point(config,
2138                                                                     handle);
2139                 if (config->mount_point == NULL) {
2140                         DBG_WARNING("shadow_copy2_find_mount_point "
2141                                     "of the share root '%s' failed: %s\n",
2142                                     handle->conn->connectpath, strerror(errno));
2143                         return -1;
2144                 }
2145         }
2146
2147         basedir = lp_parm_const_string(SNUM(handle->conn),
2148                                        "shadow", "basedir", NULL);
2149
2150         if (basedir != NULL) {
2151                 if (basedir[0] != '/') {
2152                         DEBUG(1, (__location__ " Warning: 'basedir' is "
2153                                   "relative ('%s'), but it has to be an "
2154                                   "absolute path. Disabling basedir.\n",
2155                                   basedir));
2156                         basedir = NULL;
2157                 } else {
2158                         char *p;
2159                         p = strstr(basedir, config->mount_point);
2160                         if (p != basedir) {
2161                                 DEBUG(1, ("Warning: basedir (%s) is not a "
2162                                           "subdirectory of the share root's "
2163                                           "mount point (%s). "
2164                                           "Disabling basedir\n",
2165                                           basedir, config->mount_point));
2166                                 basedir = NULL;
2167                         }
2168                 }
2169         }
2170
2171         if (config->snapdirseverywhere && basedir != NULL) {
2172                 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
2173                           "with 'snapdirseverywhere'. Disabling basedir.\n"));
2174                 basedir = NULL;
2175         }
2176
2177         snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
2178                                              "snapsharepath", NULL);
2179         if (snapsharepath != NULL) {
2180                 if (snapsharepath[0] == '/') {
2181                         DBG_WARNING("Warning: 'snapsharepath' is "
2182                                     "absolute ('%s'), but it has to be a "
2183                                     "relative path. Disabling snapsharepath.\n",
2184                                     snapsharepath);
2185                         snapsharepath = NULL;
2186                 }
2187                 if (config->snapdirseverywhere && snapsharepath != NULL) {
2188                         DBG_WARNING("Warning: 'snapsharepath' is incompatible "
2189                                     "with 'snapdirseverywhere'. Disabling "
2190                                     "snapsharepath.\n");
2191                         snapsharepath = NULL;
2192                 }
2193         }
2194
2195         if (basedir != NULL && snapsharepath != NULL) {
2196                 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
2197                             "'basedir'. Disabling snapsharepath\n");
2198                 snapsharepath = NULL;
2199         }
2200
2201         if (snapsharepath != NULL) {
2202                 config->rel_connectpath = talloc_strdup(config, snapsharepath);
2203                 if (config->rel_connectpath == NULL) {
2204                         DBG_ERR("talloc_strdup() failed\n");
2205                         errno = ENOMEM;
2206                         return -1;
2207                 }
2208         }
2209
2210         if (basedir == NULL) {
2211                 basedir = config->mount_point;
2212         }
2213
2214         if (config->rel_connectpath == NULL &&
2215             strlen(basedir) != strlen(handle->conn->connectpath)) {
2216                 config->rel_connectpath = talloc_strdup(config,
2217                         handle->conn->connectpath + strlen(basedir));
2218                 if (config->rel_connectpath == NULL) {
2219                         DEBUG(0, ("talloc_strdup() failed\n"));
2220                         errno = ENOMEM;
2221                         return -1;
2222                 }
2223         }
2224
2225         if (config->snapdir[0] == '/') {
2226                 config->snapdir_absolute = true;
2227
2228                 if (config->snapdirseverywhere == true) {
2229                         DEBUG(1, (__location__ " Warning: An absolute snapdir "
2230                                   "is incompatible with 'snapdirseverywhere', "
2231                                   "setting 'snapdirseverywhere' to false.\n"));
2232                         config->snapdirseverywhere = false;
2233                 }
2234
2235                 if (config->crossmountpoints == true) {
2236                         DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
2237                                   "is not supported with an absolute snapdir. "
2238                                   "Disabling it.\n"));
2239                         config->crossmountpoints = false;
2240                 }
2241
2242                 config->snapshot_basepath = config->snapdir;
2243         } else {
2244                 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
2245                                 config->mount_point, config->snapdir);
2246                 if (config->snapshot_basepath == NULL) {
2247                         DEBUG(0, ("talloc_asprintf() failed\n"));
2248                         errno = ENOMEM;
2249                         return -1;
2250                 }
2251         }
2252
2253         DEBUG(10, ("shadow_copy2_connect: configuration:\n"
2254                    "  share root: '%s'\n"
2255                    "  mountpoint: '%s'\n"
2256                    "  rel share root: '%s'\n"
2257                    "  snapdir: '%s'\n"
2258                    "  snapshot base path: '%s'\n"
2259                    "  format: '%s'\n"
2260                    "  use sscanf: %s\n"
2261                    "  snapdirs everywhere: %s\n"
2262                    "  cross mountpoints: %s\n"
2263                    "  fix inodes: %s\n"
2264                    "  sort order: %s\n"
2265                    "",
2266                    handle->conn->connectpath,
2267                    config->mount_point,
2268                    config->rel_connectpath,
2269                    config->snapdir,
2270                    config->snapshot_basepath,
2271                    config->gmt_format,
2272                    config->use_sscanf ? "yes" : "no",
2273                    config->snapdirseverywhere ? "yes" : "no",
2274                    config->crossmountpoints ? "yes" : "no",
2275                    config->fixinodes ? "yes" : "no",
2276                    config->sort_order
2277                    ));
2278
2279
2280         SMB_VFS_HANDLE_SET_DATA(handle, config,
2281                                 NULL, struct shadow_copy2_config,
2282                                 return -1);
2283
2284         return 0;
2285 }
2286
2287 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
2288         .connect_fn = shadow_copy2_connect,
2289         .opendir_fn = shadow_copy2_opendir,
2290         .disk_free_fn = shadow_copy2_disk_free,
2291         .get_quota_fn = shadow_copy2_get_quota,
2292         .rename_fn = shadow_copy2_rename,
2293         .link_fn = shadow_copy2_link,
2294         .symlink_fn = shadow_copy2_symlink,
2295         .stat_fn = shadow_copy2_stat,
2296         .lstat_fn = shadow_copy2_lstat,
2297         .fstat_fn = shadow_copy2_fstat,
2298         .open_fn = shadow_copy2_open,
2299         .unlink_fn = shadow_copy2_unlink,
2300         .chmod_fn = shadow_copy2_chmod,
2301         .chown_fn = shadow_copy2_chown,
2302         .chdir_fn = shadow_copy2_chdir,
2303         .ntimes_fn = shadow_copy2_ntimes,
2304         .readlink_fn = shadow_copy2_readlink,
2305         .mknod_fn = shadow_copy2_mknod,
2306         .realpath_fn = shadow_copy2_realpath,
2307         .get_nt_acl_fn = shadow_copy2_get_nt_acl,
2308         .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
2309         .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
2310         .mkdir_fn = shadow_copy2_mkdir,
2311         .rmdir_fn = shadow_copy2_rmdir,
2312         .getxattr_fn = shadow_copy2_getxattr,
2313         .listxattr_fn = shadow_copy2_listxattr,
2314         .removexattr_fn = shadow_copy2_removexattr,
2315         .setxattr_fn = shadow_copy2_setxattr,
2316         .chmod_acl_fn = shadow_copy2_chmod_acl,
2317         .chflags_fn = shadow_copy2_chflags,
2318         .get_real_filename_fn = shadow_copy2_get_real_filename,
2319         .connectpath_fn = shadow_copy2_connectpath,
2320 };
2321
2322 NTSTATUS vfs_shadow_copy2_init(void);
2323 NTSTATUS vfs_shadow_copy2_init(void)
2324 {
2325         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2326                                 "shadow_copy2", &vfs_shadow_copy2_fns);
2327 }