d3179de992829e515ffe81aeab3a51cd45b7cd27
[sfrench/samba-autobuild/.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  * Copyright (C) Rajesh Joseph     2016
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 /*
27  * This is a second implemetation of a shadow copy module for exposing
28  * file system snapshots to windows clients as shadow copies.
29  *
30  * See the manual page for documentation.
31  */
32
33 #include "includes.h"
34 #include "smbd/smbd.h"
35 #include "system/filesys.h"
36 #include "include/ntioctl.h"
37 #include "util_tdb.h"
38 #include "lib/util_path.h"
39
40 struct shadow_copy2_config {
41         char *gmt_format;
42         bool use_sscanf;
43         bool use_localtime;
44         char *snapdir;
45         char *delimiter;
46         bool snapdirseverywhere;
47         bool crossmountpoints;
48         bool fixinodes;
49         char *sort_order;
50         bool snapdir_absolute;
51         char *mount_point;
52         char *rel_connectpath; /* share root, relative to a snapshot root */
53         char *snapshot_basepath; /* the absolute version of snapdir */
54 };
55
56 /* Data-structure to hold the list of snap entries */
57 struct shadow_copy2_snapentry {
58         char *snapname;
59         char *time_fmt;
60         struct shadow_copy2_snapentry *next;
61         struct shadow_copy2_snapentry *prev;
62 };
63
64 struct shadow_copy2_snaplist_info {
65         struct shadow_copy2_snapentry *snaplist; /* snapshot list */
66         regex_t *regex; /* Regex to filter snaps */
67         time_t fetch_time; /* snaplist update time */
68 };
69
70
71 /*
72  * shadow_copy2 private structure. This structure will be
73  * used to keep module specific information
74  */
75 struct shadow_copy2_private {
76         struct shadow_copy2_config *config;
77         struct shadow_copy2_snaplist_info *snaps;
78         char *shadow_cwd; /* Absolute $cwd path. */
79         /* Absolute connectpath - can vary depending on $cwd. */
80         char *shadow_connectpath;
81 };
82
83 static int shadow_copy2_get_shadow_copy_data(
84         vfs_handle_struct *handle, files_struct *fsp,
85         struct shadow_copy_data *shadow_copy2_data,
86         bool labels);
87
88 /**
89  *This function will create a new snapshot list entry and
90  * return to the caller. This entry will also be added to
91  * the global snapshot list.
92  *
93  * @param[in]   priv    shadow_copy2 specific data structure
94  * @return      Newly   created snapshot entry or NULL on failure
95  */
96 static struct shadow_copy2_snapentry *shadow_copy2_create_snapentry(
97                                         struct shadow_copy2_private *priv)
98 {
99         struct shadow_copy2_snapentry *tmpentry = NULL;
100
101         tmpentry = talloc_zero(priv->snaps, struct shadow_copy2_snapentry);
102         if (tmpentry == NULL) {
103                 DBG_ERR("talloc_zero() failed\n");
104                 errno = ENOMEM;
105                 return NULL;
106         }
107
108         DLIST_ADD(priv->snaps->snaplist, tmpentry);
109
110         return tmpentry;
111 }
112
113 /**
114  *This function will delete the entire snaplist and reset
115  * priv->snaps->snaplist to NULL.
116  *
117  * @param[in] priv shadow_copye specific data structure
118  */
119 static void shadow_copy2_delete_snaplist(struct shadow_copy2_private *priv)
120 {
121         struct shadow_copy2_snapentry *tmp = NULL;
122
123         while ((tmp = priv->snaps->snaplist) != NULL) {
124                 DLIST_REMOVE(priv->snaps->snaplist, tmp);
125                 talloc_free(tmp);
126         }
127 }
128
129 /**
130  * Given a timestamp this function searches the global snapshot list
131  * and returns the complete snapshot directory name saved in the entry.
132  *
133  * @param[in]   priv            shadow_copy2 specific structure
134  * @param[in]   timestamp       timestamp corresponding to one of the snapshot
135  * @param[out]  snap_str        buffer to copy the actual snapshot name
136  * @param[in]   len             length of snap_str buffer
137  *
138  * @return      Length of actual snapshot name, and -1 on failure
139  */
140 static ssize_t shadow_copy2_saved_snapname(struct shadow_copy2_private *priv,
141                                           struct tm *timestamp,
142                                           char *snap_str, size_t len)
143 {
144         ssize_t snaptime_len = -1;
145         struct shadow_copy2_snapentry *entry = NULL;
146
147         snaptime_len = strftime(snap_str, len, GMT_FORMAT, timestamp);
148         if (snaptime_len == 0) {
149                 DBG_ERR("strftime failed\n");
150                 return -1;
151         }
152
153         snaptime_len = -1;
154
155         for (entry = priv->snaps->snaplist; entry; entry = entry->next) {
156                 if (strcmp(entry->time_fmt, snap_str) == 0) {
157                         snaptime_len = snprintf(snap_str, len, "%s",
158                                                 entry->snapname);
159                         return snaptime_len;
160                 }
161         }
162
163         snap_str[0] = 0;
164         return snaptime_len;
165 }
166
167
168 /**
169  * This function will check if snaplist is updated or not. If snaplist
170  * is empty then it will create a new list. Each time snaplist is updated
171  * the time is recorded. If the snapshot time is greater than the snaplist
172  * update time then chances are we are working on an older list. Then discard
173  * the old list and fetch a new snaplist.
174  *
175  * @param[in]   handle          VFS handle struct
176  * @param[in]   snap_time       time of snapshot
177  *
178  * @return      true if the list is updated else false
179  */
180 static bool shadow_copy2_update_snaplist(struct vfs_handle_struct *handle,
181                 time_t snap_time)
182 {
183         int ret = -1;
184         bool snaplist_updated = false;
185         struct files_struct fsp = {0};
186         struct smb_filename smb_fname = {0};
187         double seconds = 0.0;
188         struct shadow_copy2_private *priv = NULL;
189
190         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
191                                 return false);
192
193         seconds = difftime(snap_time, priv->snaps->fetch_time);
194
195         /*
196          * Fetch the snapshot list if either the snaplist is empty or the
197          * required snapshot time is greater than the last fetched snaplist
198          * time.
199          */
200         if (seconds > 0 || (priv->snaps->snaplist == NULL)) {
201                 smb_fname.base_name = discard_const_p(char, ".");
202                 fsp.fsp_name = &smb_fname;
203
204                 ret = shadow_copy2_get_shadow_copy_data(handle, &fsp,
205                                                         NULL, false);
206                 if (ret == 0) {
207                         snaplist_updated = true;
208                 } else {
209                         DBG_ERR("Failed to get shadow copy data\n");
210                 }
211
212         }
213
214         return snaplist_updated;
215 }
216
217 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
218                                       size_t **poffsets,
219                                       unsigned *pnum_offsets)
220 {
221         unsigned num_offsets;
222         size_t *offsets;
223         const char *p;
224
225         num_offsets = 0;
226
227         p = str;
228         while ((p = strchr(p, '/')) != NULL) {
229                 num_offsets += 1;
230                 p += 1;
231         }
232
233         offsets = talloc_array(mem_ctx, size_t, num_offsets);
234         if (offsets == NULL) {
235                 return false;
236         }
237
238         p = str;
239         num_offsets = 0;
240         while ((p = strchr(p, '/')) != NULL) {
241                 offsets[num_offsets] = p-str;
242                 num_offsets += 1;
243                 p += 1;
244         }
245
246         *poffsets = offsets;
247         *pnum_offsets = num_offsets;
248         return true;
249 }
250
251 /**
252  * Given a timestamp, build the posix level GMT-tag string
253  * based on the configurable format.
254  */
255 static ssize_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
256                                             time_t snapshot,
257                                             char *snaptime_string,
258                                             size_t len)
259 {
260         struct tm snap_tm;
261         ssize_t snaptime_len;
262         struct shadow_copy2_config *config;
263         struct shadow_copy2_private *priv;
264
265         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
266                                 return 0);
267
268         config = priv->config;
269
270         if (config->use_sscanf) {
271                 snaptime_len = snprintf(snaptime_string,
272                                         len,
273                                         config->gmt_format,
274                                         (unsigned long)snapshot);
275                 if (snaptime_len <= 0) {
276                         DEBUG(10, ("snprintf failed\n"));
277                         return -1;
278                 }
279         } else {
280                 if (config->use_localtime) {
281                         if (localtime_r(&snapshot, &snap_tm) == 0) {
282                                 DEBUG(10, ("gmtime_r failed\n"));
283                                 return -1;
284                         }
285                 } else {
286                         if (gmtime_r(&snapshot, &snap_tm) == 0) {
287                                 DEBUG(10, ("gmtime_r failed\n"));
288                                 return -1;
289                         }
290                 }
291
292                 if (priv->snaps->regex != NULL) {
293                         snaptime_len = shadow_copy2_saved_snapname(priv,
294                                                 &snap_tm, snaptime_string, len);
295                         if (snaptime_len >= 0)
296                                 return snaptime_len;
297
298                         /*
299                          * If we fail to find the snapshot name, chances are
300                          * that we have not updated our snaplist. Make sure the
301                          * snaplist is updated.
302                          */
303                         if (!shadow_copy2_update_snaplist(handle, snapshot)) {
304                                 DBG_DEBUG("shadow_copy2_update_snaplist "
305                                           "failed\n");
306                                 return -1;
307                         }
308
309                         return shadow_copy2_saved_snapname(priv,
310                                                 &snap_tm, snaptime_string, len);
311                 }
312
313                 snaptime_len = strftime(snaptime_string,
314                                         len,
315                                         config->gmt_format,
316                                         &snap_tm);
317                 if (snaptime_len == 0) {
318                         DEBUG(10, ("strftime failed\n"));
319                         return -1;
320                 }
321         }
322
323         return snaptime_len;
324 }
325
326 /**
327  * Given a timestamp, build the string to insert into a path
328  * as a path component for creating the local path to the
329  * snapshot at the given timestamp of the input path.
330  *
331  * In the case of a parallel snapdir (specified with an
332  * absolute path), this is the inital portion of the
333  * local path of any snapshot file. The complete path is
334  * obtained by appending the portion of the file's path
335  * below the share root's mountpoint.
336  */
337 static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
338                                         struct vfs_handle_struct *handle,
339                                         time_t snapshot)
340 {
341         fstring snaptime_string;
342         ssize_t snaptime_len = 0;
343         char *result = NULL;
344         struct shadow_copy2_config *config;
345         struct shadow_copy2_private *priv;
346
347         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
348                                 return NULL);
349
350         config = priv->config;
351
352         snaptime_len = shadow_copy2_posix_gmt_string(handle,
353                                                      snapshot,
354                                                      snaptime_string,
355                                                      sizeof(snaptime_string));
356         if (snaptime_len <= 0) {
357                 return NULL;
358         }
359
360         if (config->snapdir_absolute) {
361                 result = talloc_asprintf(mem_ctx, "%s/%s",
362                                          config->snapdir, snaptime_string);
363         } else {
364                 result = talloc_asprintf(mem_ctx, "/%s/%s",
365                                          config->snapdir, snaptime_string);
366         }
367         if (result == NULL) {
368                 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
369         }
370
371         return result;
372 }
373
374 /**
375  * Build the posix snapshot path for the connection
376  * at the given timestamp, i.e. the absolute posix path
377  * that contains the snapshot for this file system.
378  *
379  * This only applies to classical case, i.e. not
380  * to the "snapdirseverywhere" mode.
381  */
382 static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
383                                         struct vfs_handle_struct *handle,
384                                         time_t snapshot)
385 {
386         fstring snaptime_string;
387         ssize_t snaptime_len = 0;
388         char *result = NULL;
389         struct shadow_copy2_private *priv;
390
391         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
392                                 return NULL);
393
394         snaptime_len = shadow_copy2_posix_gmt_string(handle,
395                                                      snapshot,
396                                                      snaptime_string,
397                                                      sizeof(snaptime_string));
398         if (snaptime_len <= 0) {
399                 return NULL;
400         }
401
402         result = talloc_asprintf(mem_ctx, "%s/%s",
403                                  priv->config->snapshot_basepath, snaptime_string);
404         if (result == NULL) {
405                 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
406         }
407
408         return result;
409 }
410
411 static char *make_path_absolute(TALLOC_CTX *mem_ctx,
412                                 struct shadow_copy2_private *priv,
413                                 const char *name)
414 {
415         char *newpath = NULL;
416         char *abs_path = NULL;
417
418         if (name[0] != '/') {
419                 newpath = talloc_asprintf(mem_ctx,
420                                         "%s/%s",
421                                         priv->shadow_cwd,
422                                         name);
423                 if (newpath == NULL) {
424                         return NULL;
425                 }
426                 name = newpath;
427         }
428         abs_path = canonicalize_absolute_path(mem_ctx, name);
429         TALLOC_FREE(newpath);
430         return abs_path;
431 }
432
433 /* Return a $cwd-relative path. */
434 static bool make_relative_path(const char *cwd, char *abs_path)
435 {
436         size_t cwd_len = strlen(cwd);
437         size_t abs_len = strlen(abs_path);
438
439         if (abs_len < cwd_len) {
440                 return false;
441         }
442         if (memcmp(abs_path, cwd, cwd_len) != 0) {
443                 return false;
444         }
445         if (abs_path[cwd_len] != '/' && abs_path[cwd_len] != '\0') {
446                 return false;
447         }
448         if (abs_path[cwd_len] == '/') {
449                 cwd_len++;
450         }
451         memmove(abs_path, &abs_path[cwd_len], abs_len + 1 - cwd_len);
452         return true;
453 }
454
455 /**
456  * Strip a snapshot component from a filename as
457  * handed in via the smb layer.
458  * Returns the parsed timestamp and the stripped filename.
459  */
460 static bool shadow_copy2_strip_snapshot_internal(TALLOC_CTX *mem_ctx,
461                                         struct vfs_handle_struct *handle,
462                                         const char *orig_name,
463                                         time_t *ptimestamp,
464                                         char **pstripped,
465                                         char **psnappath)
466 {
467         struct tm tm;
468         time_t timestamp = 0;
469         const char *p;
470         char *q;
471         char *stripped = NULL;
472         size_t rest_len, dst_len;
473         struct shadow_copy2_private *priv;
474         const char *snapdir;
475         ssize_t snapdirlen;
476         ptrdiff_t len_before_gmt;
477         const char *name = orig_name;
478
479         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
480                                 return false);
481
482         DEBUG(10, (__location__ ": enter path '%s'\n", name));
483
484         p = strstr_m(name, "@GMT-");
485         if (p == NULL) {
486                 DEBUG(11, ("@GMT not found\n"));
487                 goto no_snapshot;
488         }
489         if ((p > name) && (p[-1] != '/')) {
490                 /* the GMT-token does not start a path-component */
491                 DEBUG(10, ("not at start, p=%p, name=%p, p[-1]=%d\n",
492                            p, name, (int)p[-1]));
493                 goto no_snapshot;
494         }
495
496         /*
497          * Figure out whether we got an already converted string. One
498          * case where this happens is in a smb2 create call with the
499          * mxac create blob set. We do the get_acl call on
500          * fsp->fsp_name, which is already converted. We are converted
501          * if we got a file name of the form ".snapshots/@GMT-",
502          * i.e. ".snapshots/" precedes "p".
503          */
504
505         snapdir = lp_parm_const_string(SNUM(handle->conn), "shadow", "snapdir",
506                                        ".snapshots");
507         snapdirlen = strlen(snapdir);
508         len_before_gmt = p - name;
509
510         if ((len_before_gmt >= (snapdirlen + 1)) && (p[-1] == '/')) {
511                 const char *parent_snapdir = p - (snapdirlen+1);
512
513                 DEBUG(10, ("parent_snapdir = %s\n", parent_snapdir));
514
515                 if (strncmp(parent_snapdir, snapdir, snapdirlen) == 0) {
516                         DEBUG(10, ("name=%s is already converted\n", name));
517                         goto no_snapshot;
518                 }
519         }
520         q = strptime(p, GMT_FORMAT, &tm);
521         if (q == NULL) {
522                 DEBUG(10, ("strptime failed\n"));
523                 goto no_snapshot;
524         }
525         tm.tm_isdst = -1;
526         timestamp = timegm(&tm);
527         if (timestamp == (time_t)-1) {
528                 DEBUG(10, ("timestamp==-1\n"));
529                 goto no_snapshot;
530         }
531         if (q[0] == '\0') {
532                 /*
533                  * The name consists of only the GMT token or the GMT
534                  * token is at the end of the path. XP seems to send
535                  * @GMT- at the end under certain circumstances even
536                  * with a path prefix.
537                  */
538                 if (pstripped != NULL) {
539                         if (len_before_gmt > 0) {
540                                 /*
541                                  * There is a slash before
542                                  * the @GMT-. Remove it.
543                                  */
544                                 len_before_gmt -= 1;
545                         }
546                         stripped = talloc_strndup(mem_ctx, name,
547                                         len_before_gmt);
548                         if (stripped == NULL) {
549                                 return false;
550                         }
551                         *pstripped = stripped;
552                 }
553                 *ptimestamp = timestamp;
554                 return true;
555         }
556         if (q[0] != '/') {
557                 /*
558                  * It is not a complete path component, i.e. the path
559                  * component continues after the gmt-token.
560                  */
561                 DEBUG(10, ("q[0] = %d\n", (int)q[0]));
562                 goto no_snapshot;
563         }
564         q += 1;
565
566         rest_len = strlen(q);
567         dst_len = len_before_gmt + rest_len;
568
569         if (priv->config->snapdirseverywhere) {
570                 char *insert;
571                 bool have_insert;
572                 insert = shadow_copy2_insert_string(talloc_tos(), handle,
573                                                     timestamp);
574                 if (insert == NULL) {
575                         errno = ENOMEM;
576                         return false;
577                 }
578
579                 DEBUG(10, (__location__ ": snapdirseverywhere mode.\n"
580                            "path '%s'.\n"
581                            "insert string '%s'\n", name, insert));
582
583                 have_insert = (strstr(name, insert+1) != NULL);
584                 DEBUG(10, ("have_insert=%d, name=%s, insert+1=%s\n",
585                            (int)have_insert, name, insert+1));
586                 if (have_insert) {
587                         DEBUG(10, (__location__ ": insert string '%s' found in "
588                                    "path '%s' found in snapdirseverywhere mode "
589                                    "==> already converted\n", insert, name));
590                         TALLOC_FREE(insert);
591                         goto no_snapshot;
592                 }
593                 TALLOC_FREE(insert);
594         } else {
595                 char *snapshot_path;
596                 char *s;
597
598                 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
599                                                            handle,
600                                                            timestamp);
601                 if (snapshot_path == NULL) {
602                         errno = ENOMEM;
603                         return false;
604                 }
605
606                 DEBUG(10, (__location__ " path: '%s'.\n"
607                            "snapshot path: '%s'\n", name, snapshot_path));
608
609                 s = strstr(name, snapshot_path);
610                 if (s == name) {
611                         /*
612                          * this starts with "snapshot_basepath/GMT-Token"
613                          * so it is already a converted absolute
614                          * path. Don't process further.
615                          */
616                         DEBUG(10, (__location__ ": path '%s' starts with "
617                                    "snapshot path '%s' (not in "
618                                    "snapdirseverywhere mode) ==> "
619                                    "already converted\n", name, snapshot_path));
620                         talloc_free(snapshot_path);
621                         goto no_snapshot;
622                 }
623                 talloc_free(snapshot_path);
624         }
625
626         if (pstripped != NULL) {
627                 stripped = talloc_array(mem_ctx, char, dst_len+1);
628                 if (stripped == NULL) {
629                         errno = ENOMEM;
630                         return false;
631                 }
632                 if (p > name) {
633                         memcpy(stripped, name, len_before_gmt);
634                 }
635                 if (rest_len > 0) {
636                         memcpy(stripped + len_before_gmt, q, rest_len);
637                 }
638                 stripped[dst_len] = '\0';
639                 *pstripped = stripped;
640         }
641         *ptimestamp = timestamp;
642         return true;
643 no_snapshot:
644         *ptimestamp = 0;
645         return true;
646 }
647
648 static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
649                                         struct vfs_handle_struct *handle,
650                                         const char *orig_name,
651                                         time_t *ptimestamp,
652                                         char **pstripped)
653 {
654         return shadow_copy2_strip_snapshot_internal(mem_ctx,
655                                         handle,
656                                         orig_name,
657                                         ptimestamp,
658                                         pstripped,
659                                         NULL);
660 }
661
662 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
663                                            vfs_handle_struct *handle)
664 {
665         char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
666         dev_t dev;
667         struct stat st;
668         char *p;
669
670         if (stat(path, &st) != 0) {
671                 talloc_free(path);
672                 return NULL;
673         }
674
675         dev = st.st_dev;
676
677         while ((p = strrchr(path, '/')) && p > path) {
678                 *p = 0;
679                 if (stat(path, &st) != 0) {
680                         talloc_free(path);
681                         return NULL;
682                 }
683                 if (st.st_dev != dev) {
684                         *p = '/';
685                         break;
686                 }
687         }
688
689         return path;
690 }
691
692 /**
693  * Convert from a name as handed in via the SMB layer
694  * and a timestamp into the local path of the snapshot
695  * of the provided file at the provided time.
696  * Also return the path in the snapshot corresponding
697  * to the file's share root.
698  */
699 static char *shadow_copy2_do_convert(TALLOC_CTX *mem_ctx,
700                                      struct vfs_handle_struct *handle,
701                                      const char *name, time_t timestamp,
702                                      size_t *snaproot_len)
703 {
704         struct smb_filename converted_fname;
705         char *result = NULL;
706         size_t *slashes = NULL;
707         unsigned num_slashes;
708         char *path = NULL;
709         size_t pathlen;
710         char *insert = NULL;
711         char *converted = NULL;
712         size_t insertlen, connectlen = 0;
713         int i, saved_errno;
714         size_t min_offset;
715         struct shadow_copy2_config *config;
716         struct shadow_copy2_private *priv;
717         size_t in_share_offset = 0;
718
719         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
720                                 return NULL);
721
722         config = priv->config;
723
724         DEBUG(10, ("converting '%s'\n", name));
725
726         if (!config->snapdirseverywhere) {
727                 int ret;
728                 char *snapshot_path;
729
730                 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
731                                                            handle,
732                                                            timestamp);
733                 if (snapshot_path == NULL) {
734                         goto fail;
735                 }
736
737                 if (config->rel_connectpath == NULL) {
738                         converted = talloc_asprintf(mem_ctx, "%s/%s",
739                                                     snapshot_path, name);
740                 } else {
741                         converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
742                                                     snapshot_path,
743                                                     config->rel_connectpath,
744                                                     name);
745                 }
746                 if (converted == NULL) {
747                         goto fail;
748                 }
749
750                 ZERO_STRUCT(converted_fname);
751                 converted_fname.base_name = converted;
752
753                 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
754                 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
755                            converted,
756                            ret, ret == 0 ? "ok" : strerror(errno)));
757                 if (ret == 0) {
758                         DEBUG(10, ("Found %s\n", converted));
759                         result = converted;
760                         converted = NULL;
761                         if (snaproot_len != NULL) {
762                                 *snaproot_len = strlen(snapshot_path);
763                                 if (config->rel_connectpath != NULL) {
764                                         *snaproot_len +=
765                                             strlen(config->rel_connectpath) + 1;
766                                 }
767                         }
768                         goto fail;
769                 } else {
770                         errno = ENOENT;
771                         goto fail;
772                 }
773                 /* never reached ... */
774         }
775
776         connectlen = strlen(handle->conn->connectpath);
777         if (name[0] == 0) {
778                 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
779         } else {
780                 path = talloc_asprintf(
781                         mem_ctx, "%s/%s", handle->conn->connectpath, name);
782         }
783         if (path == NULL) {
784                 errno = ENOMEM;
785                 goto fail;
786         }
787         pathlen = talloc_get_size(path)-1;
788
789         if (!shadow_copy2_find_slashes(talloc_tos(), path,
790                                        &slashes, &num_slashes)) {
791                 goto fail;
792         }
793
794         insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
795         if (insert == NULL) {
796                 goto fail;
797         }
798         insertlen = talloc_get_size(insert)-1;
799
800         /*
801          * Note: We deliberatly don't expensively initialize the
802          * array with talloc_zero here: Putting zero into
803          * converted[pathlen+insertlen] below is sufficient, because
804          * in the following for loop, the insert string is inserted
805          * at various slash places. So the memory up to position
806          * pathlen+insertlen will always be initialized when the
807          * converted string is used.
808          */
809         converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
810         if (converted == NULL) {
811                 goto fail;
812         }
813
814         if (path[pathlen-1] != '/') {
815                 /*
816                  * Append a fake slash to find the snapshot root
817                  */
818                 size_t *tmp;
819                 tmp = talloc_realloc(talloc_tos(), slashes,
820                                      size_t, num_slashes+1);
821                 if (tmp == NULL) {
822                         goto fail;
823                 }
824                 slashes = tmp;
825                 slashes[num_slashes] = pathlen;
826                 num_slashes += 1;
827         }
828
829         min_offset = 0;
830
831         if (!config->crossmountpoints) {
832                 min_offset = strlen(config->mount_point);
833         }
834
835         memcpy(converted, path, pathlen+1);
836         converted[pathlen+insertlen] = '\0';
837
838         ZERO_STRUCT(converted_fname);
839         converted_fname.base_name = converted;
840
841         for (i = num_slashes-1; i>=0; i--) {
842                 int ret;
843                 size_t offset;
844
845                 offset = slashes[i];
846
847                 if (offset < min_offset) {
848                         errno = ENOENT;
849                         goto fail;
850                 }
851
852                 if (offset >= connectlen) {
853                         in_share_offset = offset;
854                 }
855
856                 memcpy(converted+offset, insert, insertlen);
857
858                 offset += insertlen;
859                 memcpy(converted+offset, path + slashes[i],
860                        pathlen - slashes[i]);
861
862                 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
863
864                 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
865                            converted,
866                            ret, ret == 0 ? "ok" : strerror(errno)));
867                 if (ret == 0) {
868                         /* success */
869                         if (snaproot_len != NULL) {
870                                 *snaproot_len = in_share_offset + insertlen;
871                         }
872                         break;
873                 }
874                 if (errno == ENOTDIR) {
875                         /*
876                          * This is a valid condition: We appended the
877                          * .snaphots/@GMT.. to a file name. Just try
878                          * with the upper levels.
879                          */
880                         continue;
881                 }
882                 if (errno != ENOENT) {
883                         /* Other problem than "not found" */
884                         goto fail;
885                 }
886         }
887
888         if (i >= 0) {
889                 /*
890                  * Found something
891                  */
892                 DEBUG(10, ("Found %s\n", converted));
893                 result = converted;
894                 converted = NULL;
895         } else {
896                 errno = ENOENT;
897         }
898 fail:
899         saved_errno = errno;
900         TALLOC_FREE(converted);
901         TALLOC_FREE(insert);
902         TALLOC_FREE(slashes);
903         TALLOC_FREE(path);
904         errno = saved_errno;
905         return result;
906 }
907
908 /**
909  * Convert from a name as handed in via the SMB layer
910  * and a timestamp into the local path of the snapshot
911  * of the provided file at the provided time.
912  */
913 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
914                                   struct vfs_handle_struct *handle,
915                                   const char *name, time_t timestamp)
916 {
917         return shadow_copy2_do_convert(mem_ctx, handle, name, timestamp, NULL);
918 }
919
920 /*
921   modify a sbuf return to ensure that inodes in the shadow directory
922   are different from those in the main directory
923  */
924 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
925                          SMB_STRUCT_STAT *sbuf)
926 {
927         struct shadow_copy2_private *priv;
928
929         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
930                                 return);
931
932         if (priv->config->fixinodes) {
933                 /* some snapshot systems, like GPFS, return the name
934                    device:inode for the snapshot files as the current
935                    files. That breaks the 'restore' button in the shadow copy
936                    GUI, as the client gets a sharing violation.
937
938                    This is a crude way of allowing both files to be
939                    open at once. It has a slight chance of inode
940                    number collision, but I can't see a better approach
941                    without significant VFS changes
942                 */
943                 TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
944                                  .dsize = strlen(fname) };
945                 uint32_t shash;
946
947                 shash = tdb_jenkins_hash(&key) & 0xFF000000;
948                 if (shash == 0) {
949                         shash = 1;
950                 }
951                 sbuf->st_ex_ino ^= shash;
952         }
953 }
954
955 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
956                         const struct smb_filename *smb_fname,
957                         const char *mask,
958                         uint32_t attr)
959 {
960         time_t timestamp = 0;
961         char *stripped = NULL;
962         DIR *ret;
963         int saved_errno;
964         char *conv;
965         struct smb_filename *conv_smb_fname = NULL;
966
967         if (!shadow_copy2_strip_snapshot(talloc_tos(),
968                                 handle,
969                                 smb_fname->base_name,
970                                 &timestamp,
971                                 &stripped)) {
972                 return NULL;
973         }
974         if (timestamp == 0) {
975                 return SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
976         }
977         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
978         TALLOC_FREE(stripped);
979         if (conv == NULL) {
980                 return NULL;
981         }
982         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
983                                         conv,
984                                         NULL,
985                                         NULL,
986                                         smb_fname->flags);
987         if (conv_smb_fname == NULL) {
988                 TALLOC_FREE(conv);
989                 return NULL;
990         }
991         ret = SMB_VFS_NEXT_OPENDIR(handle, conv_smb_fname, mask, attr);
992         saved_errno = errno;
993         TALLOC_FREE(conv);
994         TALLOC_FREE(conv_smb_fname);
995         errno = saved_errno;
996         return ret;
997 }
998
999 static int shadow_copy2_rename(vfs_handle_struct *handle,
1000                                const struct smb_filename *smb_fname_src,
1001                                const struct smb_filename *smb_fname_dst)
1002 {
1003         time_t timestamp_src = 0;
1004         time_t timestamp_dst = 0;
1005
1006         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1007                                          smb_fname_src->base_name,
1008                                          &timestamp_src, NULL)) {
1009                 return -1;
1010         }
1011         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1012                                          smb_fname_dst->base_name,
1013                                          &timestamp_dst, NULL)) {
1014                 return -1;
1015         }
1016         if (timestamp_src != 0) {
1017                 errno = EXDEV;
1018                 return -1;
1019         }
1020         if (timestamp_dst != 0) {
1021                 errno = EROFS;
1022                 return -1;
1023         }
1024         return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
1025 }
1026
1027 static int shadow_copy2_symlink(vfs_handle_struct *handle,
1028                                 const char *oldname, const char *newname)
1029 {
1030         time_t timestamp_old = 0;
1031         time_t timestamp_new = 0;
1032
1033         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
1034                                          &timestamp_old, NULL)) {
1035                 return -1;
1036         }
1037         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
1038                                          &timestamp_new, NULL)) {
1039                 return -1;
1040         }
1041         if ((timestamp_old != 0) || (timestamp_new != 0)) {
1042                 errno = EROFS;
1043                 return -1;
1044         }
1045         return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
1046 }
1047
1048 static int shadow_copy2_link(vfs_handle_struct *handle,
1049                              const char *oldname, const char *newname)
1050 {
1051         time_t timestamp_old = 0;
1052         time_t timestamp_new = 0;
1053
1054         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
1055                                          &timestamp_old, NULL)) {
1056                 return -1;
1057         }
1058         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
1059                                          &timestamp_new, NULL)) {
1060                 return -1;
1061         }
1062         if ((timestamp_old != 0) || (timestamp_new != 0)) {
1063                 errno = EROFS;
1064                 return -1;
1065         }
1066         return SMB_VFS_NEXT_LINK(handle, oldname, newname);
1067 }
1068
1069 static int shadow_copy2_stat(vfs_handle_struct *handle,
1070                              struct smb_filename *smb_fname)
1071 {
1072         time_t timestamp = 0;
1073         char *stripped = NULL;
1074         char *tmp;
1075         int ret, saved_errno;
1076
1077         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1078                                          smb_fname->base_name,
1079                                          &timestamp, &stripped)) {
1080                 return -1;
1081         }
1082         if (timestamp == 0) {
1083                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
1084         }
1085
1086         tmp = smb_fname->base_name;
1087         smb_fname->base_name = shadow_copy2_convert(
1088                 talloc_tos(), handle, stripped, timestamp);
1089         TALLOC_FREE(stripped);
1090
1091         if (smb_fname->base_name == NULL) {
1092                 smb_fname->base_name = tmp;
1093                 return -1;
1094         }
1095
1096         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1097         saved_errno = errno;
1098
1099         TALLOC_FREE(smb_fname->base_name);
1100         smb_fname->base_name = tmp;
1101
1102         if (ret == 0) {
1103                 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1104         }
1105         errno = saved_errno;
1106         return ret;
1107 }
1108
1109 static int shadow_copy2_lstat(vfs_handle_struct *handle,
1110                               struct smb_filename *smb_fname)
1111 {
1112         time_t timestamp = 0;
1113         char *stripped = NULL;
1114         char *tmp;
1115         int ret, saved_errno;
1116
1117         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1118                                          smb_fname->base_name,
1119                                          &timestamp, &stripped)) {
1120                 return -1;
1121         }
1122         if (timestamp == 0) {
1123                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1124         }
1125
1126         tmp = smb_fname->base_name;
1127         smb_fname->base_name = shadow_copy2_convert(
1128                 talloc_tos(), handle, stripped, timestamp);
1129         TALLOC_FREE(stripped);
1130
1131         if (smb_fname->base_name == NULL) {
1132                 smb_fname->base_name = tmp;
1133                 return -1;
1134         }
1135
1136         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1137         saved_errno = errno;
1138
1139         TALLOC_FREE(smb_fname->base_name);
1140         smb_fname->base_name = tmp;
1141
1142         if (ret == 0) {
1143                 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
1144         }
1145         errno = saved_errno;
1146         return ret;
1147 }
1148
1149 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
1150                               SMB_STRUCT_STAT *sbuf)
1151 {
1152         time_t timestamp = 0;
1153         int ret;
1154
1155         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1156         if (ret == -1) {
1157                 return ret;
1158         }
1159         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1160                                          fsp->fsp_name->base_name,
1161                                          &timestamp, NULL)) {
1162                 return 0;
1163         }
1164         if (timestamp != 0) {
1165                 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
1166         }
1167         return 0;
1168 }
1169
1170 static int shadow_copy2_open(vfs_handle_struct *handle,
1171                              struct smb_filename *smb_fname, files_struct *fsp,
1172                              int flags, mode_t mode)
1173 {
1174         time_t timestamp = 0;
1175         char *stripped = NULL;
1176         char *tmp;
1177         int ret, saved_errno;
1178
1179         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1180                                          smb_fname->base_name,
1181                                          &timestamp, &stripped)) {
1182                 return -1;
1183         }
1184         if (timestamp == 0) {
1185                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1186         }
1187
1188         tmp = smb_fname->base_name;
1189         smb_fname->base_name = shadow_copy2_convert(
1190                 talloc_tos(), handle, stripped, timestamp);
1191         TALLOC_FREE(stripped);
1192
1193         if (smb_fname->base_name == NULL) {
1194                 smb_fname->base_name = tmp;
1195                 return -1;
1196         }
1197
1198         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
1199         saved_errno = errno;
1200
1201         TALLOC_FREE(smb_fname->base_name);
1202         smb_fname->base_name = tmp;
1203
1204         errno = saved_errno;
1205         return ret;
1206 }
1207
1208 static int shadow_copy2_unlink(vfs_handle_struct *handle,
1209                                const struct smb_filename *smb_fname)
1210 {
1211         time_t timestamp = 0;
1212         char *stripped = NULL;
1213         int ret, saved_errno;
1214         struct smb_filename *conv;
1215
1216         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1217                                          smb_fname->base_name,
1218                                          &timestamp, &stripped)) {
1219                 return -1;
1220         }
1221         if (timestamp == 0) {
1222                 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1223         }
1224         conv = cp_smb_filename(talloc_tos(), smb_fname);
1225         if (conv == NULL) {
1226                 errno = ENOMEM;
1227                 return -1;
1228         }
1229         conv->base_name = shadow_copy2_convert(
1230                 conv, handle, stripped, timestamp);
1231         TALLOC_FREE(stripped);
1232         if (conv->base_name == NULL) {
1233                 return -1;
1234         }
1235         ret = SMB_VFS_NEXT_UNLINK(handle, conv);
1236         saved_errno = errno;
1237         TALLOC_FREE(conv);
1238         errno = saved_errno;
1239         return ret;
1240 }
1241
1242 static int shadow_copy2_chmod(vfs_handle_struct *handle,
1243                         const struct smb_filename *smb_fname,
1244                         mode_t mode)
1245 {
1246         time_t timestamp = 0;
1247         char *stripped = NULL;
1248         int ret, saved_errno;
1249         char *conv = NULL;
1250         struct smb_filename *conv_smb_fname;
1251
1252         if (!shadow_copy2_strip_snapshot(talloc_tos(),
1253                                 handle,
1254                                 smb_fname->base_name,
1255                                 &timestamp,
1256                                 &stripped)) {
1257                 return -1;
1258         }
1259         if (timestamp == 0) {
1260                 TALLOC_FREE(stripped);
1261                 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1262         }
1263         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1264         TALLOC_FREE(stripped);
1265         if (conv == NULL) {
1266                 return -1;
1267         }
1268         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1269                                         conv,
1270                                         NULL,
1271                                         NULL,
1272                                         smb_fname->flags);
1273         if (conv_smb_fname == NULL) {
1274                 TALLOC_FREE(conv);
1275                 errno = ENOMEM;
1276                 return -1;
1277         }
1278
1279         ret = SMB_VFS_NEXT_CHMOD(handle, conv_smb_fname, mode);
1280         saved_errno = errno;
1281         TALLOC_FREE(conv);
1282         TALLOC_FREE(conv_smb_fname);
1283         errno = saved_errno;
1284         return ret;
1285 }
1286
1287 static int shadow_copy2_chown(vfs_handle_struct *handle,
1288                         const struct smb_filename *smb_fname,
1289                         uid_t uid,
1290                         gid_t gid)
1291 {
1292         time_t timestamp = 0;
1293         char *stripped = NULL;
1294         int ret, saved_errno;
1295         char *conv = NULL;
1296         struct smb_filename *conv_smb_fname = NULL;
1297
1298         if (!shadow_copy2_strip_snapshot(talloc_tos(),
1299                                 handle,
1300                                 smb_fname->base_name,
1301                                 &timestamp,
1302                                 &stripped)) {
1303                 return -1;
1304         }
1305         if (timestamp == 0) {
1306                 return SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
1307         }
1308         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1309         TALLOC_FREE(stripped);
1310         if (conv == NULL) {
1311                 return -1;
1312         }
1313         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1314                                         conv,
1315                                         NULL,
1316                                         NULL,
1317                                         smb_fname->flags);
1318         if (conv_smb_fname == NULL) {
1319                 TALLOC_FREE(conv);
1320                 errno = ENOMEM;
1321                 return -1;
1322         }
1323         ret = SMB_VFS_NEXT_CHOWN(handle, conv_smb_fname, uid, gid);
1324         saved_errno = errno;
1325         TALLOC_FREE(conv);
1326         TALLOC_FREE(conv_smb_fname);
1327         errno = saved_errno;
1328         return ret;
1329 }
1330
1331 static int shadow_copy2_chdir(vfs_handle_struct *handle,
1332                               const char *fname)
1333 {
1334         time_t timestamp = 0;
1335         char *stripped = NULL;
1336         int ret, saved_errno;
1337         char *conv;
1338
1339         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1340                                          &timestamp, &stripped)) {
1341                 return -1;
1342         }
1343         if (timestamp == 0) {
1344                 return SMB_VFS_NEXT_CHDIR(handle, fname);
1345         }
1346         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1347         TALLOC_FREE(stripped);
1348         if (conv == NULL) {
1349                 return -1;
1350         }
1351         ret = SMB_VFS_NEXT_CHDIR(handle, conv);
1352         saved_errno = errno;
1353         TALLOC_FREE(conv);
1354         errno = saved_errno;
1355         return ret;
1356 }
1357
1358 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
1359                                const struct smb_filename *smb_fname,
1360                                struct smb_file_time *ft)
1361 {
1362         time_t timestamp = 0;
1363         char *stripped = NULL;
1364         int ret, saved_errno;
1365         struct smb_filename *conv;
1366
1367         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1368                                          smb_fname->base_name,
1369                                          &timestamp, &stripped)) {
1370                 return -1;
1371         }
1372         if (timestamp == 0) {
1373                 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1374         }
1375         conv = cp_smb_filename(talloc_tos(), smb_fname);
1376         if (conv == NULL) {
1377                 errno = ENOMEM;
1378                 return -1;
1379         }
1380         conv->base_name = shadow_copy2_convert(
1381                 conv, handle, stripped, timestamp);
1382         TALLOC_FREE(stripped);
1383         if (conv->base_name == NULL) {
1384                 return -1;
1385         }
1386         ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
1387         saved_errno = errno;
1388         TALLOC_FREE(conv);
1389         errno = saved_errno;
1390         return ret;
1391 }
1392
1393 static int shadow_copy2_readlink(vfs_handle_struct *handle,
1394                                  const char *fname, char *buf, size_t bufsiz)
1395 {
1396         time_t timestamp = 0;
1397         char *stripped = NULL;
1398         int ret, saved_errno;
1399         char *conv;
1400
1401         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1402                                          &timestamp, &stripped)) {
1403                 return -1;
1404         }
1405         if (timestamp == 0) {
1406                 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
1407         }
1408         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1409         TALLOC_FREE(stripped);
1410         if (conv == NULL) {
1411                 return -1;
1412         }
1413         ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
1414         saved_errno = errno;
1415         TALLOC_FREE(conv);
1416         errno = saved_errno;
1417         return ret;
1418 }
1419
1420 static int shadow_copy2_mknod(vfs_handle_struct *handle,
1421                               const char *fname, mode_t mode, SMB_DEV_T dev)
1422 {
1423         time_t timestamp = 0;
1424         char *stripped = NULL;
1425         int ret, saved_errno;
1426         char *conv;
1427
1428         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1429                                          &timestamp, &stripped)) {
1430                 return -1;
1431         }
1432         if (timestamp == 0) {
1433                 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
1434         }
1435         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1436         TALLOC_FREE(stripped);
1437         if (conv == NULL) {
1438                 return -1;
1439         }
1440         ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
1441         saved_errno = errno;
1442         TALLOC_FREE(conv);
1443         errno = saved_errno;
1444         return ret;
1445 }
1446
1447 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
1448                                    const char *fname)
1449 {
1450         time_t timestamp = 0;
1451         char *stripped = NULL;
1452         char *tmp = NULL;
1453         char *result = NULL;
1454         int saved_errno;
1455
1456         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1457                                          &timestamp, &stripped)) {
1458                 goto done;
1459         }
1460         if (timestamp == 0) {
1461                 return SMB_VFS_NEXT_REALPATH(handle, fname);
1462         }
1463
1464         tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1465         if (tmp == NULL) {
1466                 goto done;
1467         }
1468
1469         result = SMB_VFS_NEXT_REALPATH(handle, tmp);
1470
1471 done:
1472         saved_errno = errno;
1473         TALLOC_FREE(tmp);
1474         TALLOC_FREE(stripped);
1475         errno = saved_errno;
1476         return result;
1477 }
1478
1479 /**
1480  * Check whether a given directory contains a
1481  * snapshot directory as direct subdirectory.
1482  * If yes, return the path of the snapshot-subdir,
1483  * otherwise return NULL.
1484  */
1485 static char *have_snapdir(struct vfs_handle_struct *handle,
1486                           const char *path)
1487 {
1488         struct smb_filename smb_fname;
1489         int ret;
1490         struct shadow_copy2_private *priv;
1491
1492         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1493                                 return NULL);
1494
1495         ZERO_STRUCT(smb_fname);
1496         smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1497                                               path, priv->config->snapdir);
1498         if (smb_fname.base_name == NULL) {
1499                 return NULL;
1500         }
1501
1502         ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1503         if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1504                 return smb_fname.base_name;
1505         }
1506         TALLOC_FREE(smb_fname.base_name);
1507         return NULL;
1508 }
1509
1510 static bool check_access_snapdir(struct vfs_handle_struct *handle,
1511                                 const char *path)
1512 {
1513         struct smb_filename smb_fname;
1514         int ret;
1515         NTSTATUS status;
1516
1517         ZERO_STRUCT(smb_fname);
1518         smb_fname.base_name = talloc_asprintf(talloc_tos(),
1519                                                 "%s",
1520                                                 path);
1521         if (smb_fname.base_name == NULL) {
1522                 return false;
1523         }
1524
1525         ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1526         if (ret != 0 || !S_ISDIR(smb_fname.st.st_ex_mode)) {
1527                 TALLOC_FREE(smb_fname.base_name);
1528                 return false;
1529         }
1530
1531         status = smbd_check_access_rights(handle->conn,
1532                                         &smb_fname,
1533                                         false,
1534                                         SEC_DIR_LIST);
1535         if (!NT_STATUS_IS_OK(status)) {
1536                 DEBUG(0,("user does not have list permission "
1537                         "on snapdir %s\n",
1538                         smb_fname.base_name));
1539                 TALLOC_FREE(smb_fname.base_name);
1540                 return false;
1541         }
1542         TALLOC_FREE(smb_fname.base_name);
1543         return true;
1544 }
1545
1546 /**
1547  * Find the snapshot directory (if any) for the given
1548  * filename (which is relative to the share).
1549  */
1550 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1551                                              struct vfs_handle_struct *handle,
1552                                              struct smb_filename *smb_fname)
1553 {
1554         char *path, *p;
1555         const char *snapdir;
1556         struct shadow_copy2_config *config;
1557         struct shadow_copy2_private *priv;
1558
1559         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1560                                 return NULL);
1561
1562         config = priv->config;
1563
1564         /*
1565          * If the non-snapdisrseverywhere mode, we should not search!
1566          */
1567         if (!config->snapdirseverywhere) {
1568                 return config->snapshot_basepath;
1569         }
1570
1571         path = talloc_asprintf(mem_ctx, "%s/%s",
1572                                handle->conn->connectpath,
1573                                smb_fname->base_name);
1574         if (path == NULL) {
1575                 return NULL;
1576         }
1577
1578         snapdir = have_snapdir(handle, path);
1579         if (snapdir != NULL) {
1580                 TALLOC_FREE(path);
1581                 return snapdir;
1582         }
1583
1584         while ((p = strrchr(path, '/')) && (p > path)) {
1585
1586                 p[0] = '\0';
1587
1588                 snapdir = have_snapdir(handle, path);
1589                 if (snapdir != NULL) {
1590                         TALLOC_FREE(path);
1591                         return snapdir;
1592                 }
1593         }
1594         TALLOC_FREE(path);
1595         return NULL;
1596 }
1597
1598 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1599                                          const char *name,
1600                                          char *gmt, size_t gmt_len)
1601 {
1602         struct tm timestamp;
1603         time_t timestamp_t;
1604         unsigned long int timestamp_long;
1605         const char *fmt;
1606         struct shadow_copy2_config *config;
1607         struct shadow_copy2_private *priv;
1608         char *tmpstr = NULL;
1609         char *tmp = NULL;
1610         bool converted = false;
1611         int ret = -1;
1612
1613         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1614                                 return NULL);
1615
1616         config = priv->config;
1617
1618         fmt = config->gmt_format;
1619
1620         /*
1621          * If regex is provided, then we will have to parse the
1622          * filename which will contain both the prefix and the time format.
1623          * e.g. <prefix><delimiter><time_format>
1624          */
1625         if (priv->snaps->regex != NULL) {
1626                 tmpstr = talloc_strdup(talloc_tos(), name);
1627                 /* point "name" to the time format */
1628                 name = strstr(name, priv->config->delimiter);
1629                 if (name == NULL) {
1630                         goto done;
1631                 }
1632                 /* Extract the prefix */
1633                 tmp = strstr(tmpstr, priv->config->delimiter);
1634                 *tmp = '\0';
1635
1636                 /* Parse regex */
1637                 ret = regexec(priv->snaps->regex, tmpstr, 0, NULL, 0);
1638                 if (ret) {
1639                         DBG_DEBUG("shadow_copy2_snapshot_to_gmt: "
1640                                   "no regex match for %s\n", tmpstr);
1641                         goto done;
1642                 }
1643         }
1644
1645         ZERO_STRUCT(timestamp);
1646         if (config->use_sscanf) {
1647                 if (sscanf(name, fmt, &timestamp_long) != 1) {
1648                         DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1649                                    "no sscanf match %s: %s\n",
1650                                    fmt, name));
1651                         goto done;
1652                 }
1653                 timestamp_t = timestamp_long;
1654                 gmtime_r(&timestamp_t, &timestamp);
1655         } else {
1656                 if (strptime(name, fmt, &timestamp) == NULL) {
1657                         DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1658                                    "no match %s: %s\n",
1659                                    fmt, name));
1660                         goto done;
1661                 }
1662                 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1663                            fmt, name));
1664                 
1665                 if (config->use_localtime) {
1666                         timestamp.tm_isdst = -1;
1667                         timestamp_t = mktime(&timestamp);
1668                         gmtime_r(&timestamp_t, &timestamp);
1669                 }
1670         }
1671
1672         strftime(gmt, gmt_len, GMT_FORMAT, &timestamp);
1673         converted = true;
1674
1675 done:
1676         TALLOC_FREE(tmpstr);
1677         return converted;
1678 }
1679
1680 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1681 {
1682         return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1683 }
1684
1685 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1686 {
1687         return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1688 }
1689
1690 /*
1691   sort the shadow copy data in ascending or descending order
1692  */
1693 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1694                                    struct shadow_copy_data *shadow_copy2_data)
1695 {
1696         int (*cmpfunc)(const void *, const void *);
1697         const char *sort;
1698         struct shadow_copy2_private *priv;
1699
1700         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1701                                 return);
1702
1703         sort = priv->config->sort_order;
1704         if (sort == NULL) {
1705                 return;
1706         }
1707
1708         if (strcmp(sort, "asc") == 0) {
1709                 cmpfunc = shadow_copy2_label_cmp_asc;
1710         } else if (strcmp(sort, "desc") == 0) {
1711                 cmpfunc = shadow_copy2_label_cmp_desc;
1712         } else {
1713                 return;
1714         }
1715
1716         if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1717             shadow_copy2_data->labels)
1718         {
1719                 TYPESAFE_QSORT(shadow_copy2_data->labels,
1720                                shadow_copy2_data->num_volumes,
1721                                cmpfunc);
1722         }
1723 }
1724
1725 static int shadow_copy2_get_shadow_copy_data(
1726         vfs_handle_struct *handle, files_struct *fsp,
1727         struct shadow_copy_data *shadow_copy2_data,
1728         bool labels)
1729 {
1730         DIR *p;
1731         const char *snapdir;
1732         struct smb_filename *snapdir_smb_fname = NULL;
1733         struct dirent *d;
1734         TALLOC_CTX *tmp_ctx = talloc_stackframe();
1735         struct shadow_copy2_private *priv = NULL;
1736         struct shadow_copy2_snapentry *tmpentry = NULL;
1737         bool get_snaplist = false;
1738         bool access_granted = false;
1739         int ret = -1;
1740
1741         snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1742         if (snapdir == NULL) {
1743                 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1744                          handle->conn->connectpath));
1745                 errno = EINVAL;
1746                 goto done;
1747         }
1748
1749         access_granted = check_access_snapdir(handle, snapdir);
1750         if (!access_granted) {
1751                 DEBUG(0,("access denied on listing snapdir %s\n", snapdir));
1752                 errno = EACCES;
1753                 goto done;
1754         }
1755
1756         snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
1757                                         snapdir,
1758                                         NULL,
1759                                         NULL,
1760                                         fsp->fsp_name->flags);
1761         if (snapdir_smb_fname == NULL) {
1762                 errno = ENOMEM;
1763                 goto done;
1764         }
1765
1766         p = SMB_VFS_NEXT_OPENDIR(handle, snapdir_smb_fname, NULL, 0);
1767
1768         if (!p) {
1769                 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1770                          " - %s\n", snapdir, strerror(errno)));
1771                 errno = ENOSYS;
1772                 goto done;
1773         }
1774
1775         if (shadow_copy2_data != NULL) {
1776                 shadow_copy2_data->num_volumes = 0;
1777                 shadow_copy2_data->labels      = NULL;
1778         }
1779
1780         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
1781                                 goto done);
1782
1783         /*
1784          * Normally this function is called twice once with labels = false and
1785          * then with labels = true. When labels is false it will return the
1786          * number of volumes so that the caller can allocate memory for that
1787          * many labels. Therefore to eliminate snaplist both the times it is
1788          * good to check if labels is set or not.
1789          *
1790          * shadow_copy2_data is NULL when we only want to update the list and
1791          * don't want any labels.
1792          */
1793         if ((priv->snaps->regex != NULL) && (labels || shadow_copy2_data == NULL)) {
1794                 get_snaplist = true;
1795                 /* Reset the global snaplist */
1796                 shadow_copy2_delete_snaplist(priv);
1797
1798                 /* Set the current time as snaplist update time */
1799                 time(&(priv->snaps->fetch_time));
1800         }
1801
1802         while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
1803                 char snapshot[GMT_NAME_LEN+1];
1804                 SHADOW_COPY_LABEL *tlabels;
1805
1806                 /*
1807                  * ignore names not of the right form in the snapshot
1808                  * directory
1809                  */
1810                 if (!shadow_copy2_snapshot_to_gmt(
1811                             handle, d->d_name,
1812                             snapshot, sizeof(snapshot))) {
1813
1814                         DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1815                                   "ignoring %s\n", d->d_name));
1816                         continue;
1817                 }
1818                 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1819                          d->d_name, snapshot));
1820
1821                 if (get_snaplist) {
1822                         /*
1823                          * Create a snap entry for each successful
1824                          * pattern match.
1825                          */
1826                         tmpentry = shadow_copy2_create_snapentry(priv);
1827                         if (tmpentry == NULL) {
1828                                 DBG_ERR("talloc_zero() failed\n");
1829                                 goto done;
1830                         }
1831                         tmpentry->snapname = talloc_strdup(tmpentry, d->d_name);
1832                         tmpentry->time_fmt = talloc_strdup(tmpentry, snapshot);
1833                 }
1834
1835                 if (shadow_copy2_data == NULL) {
1836                         continue;
1837                 }
1838
1839                 if (!labels) {
1840                         /* the caller doesn't want the labels */
1841                         shadow_copy2_data->num_volumes++;
1842                         continue;
1843                 }
1844
1845                 tlabels = talloc_realloc(shadow_copy2_data,
1846                                          shadow_copy2_data->labels,
1847                                          SHADOW_COPY_LABEL,
1848                                          shadow_copy2_data->num_volumes+1);
1849                 if (tlabels == NULL) {
1850                         DEBUG(0,("shadow_copy2: out of memory\n"));
1851                         SMB_VFS_NEXT_CLOSEDIR(handle, p);
1852                         goto done;
1853                 }
1854
1855                 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
1856                         sizeof(*tlabels));
1857
1858                 shadow_copy2_data->num_volumes++;
1859                 shadow_copy2_data->labels = tlabels;
1860         }
1861
1862         SMB_VFS_NEXT_CLOSEDIR(handle,p);
1863
1864         shadow_copy2_sort_data(handle, shadow_copy2_data);
1865         ret = 0;
1866
1867 done:
1868         TALLOC_FREE(tmp_ctx);
1869         return ret;
1870 }
1871
1872 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
1873                                         struct files_struct *fsp,
1874                                         uint32_t security_info,
1875                                          TALLOC_CTX *mem_ctx,
1876                                         struct security_descriptor **ppdesc)
1877 {
1878         time_t timestamp = 0;
1879         char *stripped = NULL;
1880         NTSTATUS status;
1881         char *conv;
1882         struct smb_filename *smb_fname = NULL;
1883
1884         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1885                                          fsp->fsp_name->base_name,
1886                                          &timestamp, &stripped)) {
1887                 return map_nt_error_from_unix(errno);
1888         }
1889         if (timestamp == 0) {
1890                 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1891                                                 mem_ctx,
1892                                                 ppdesc);
1893         }
1894         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1895         TALLOC_FREE(stripped);
1896         if (conv == NULL) {
1897                 return map_nt_error_from_unix(errno);
1898         }
1899         smb_fname = synthetic_smb_fname(talloc_tos(),
1900                                         conv,
1901                                         NULL,
1902                                         NULL,
1903                                         fsp->fsp_name->flags);
1904         if (smb_fname == NULL) {
1905                 TALLOC_FREE(conv);
1906                 return NT_STATUS_NO_MEMORY;
1907         }
1908
1909         status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1910                                          mem_ctx, ppdesc);
1911         TALLOC_FREE(conv);
1912         TALLOC_FREE(smb_fname);
1913         return status;
1914 }
1915
1916 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
1917                                         const struct smb_filename *smb_fname,
1918                                         uint32_t security_info,
1919                                         TALLOC_CTX *mem_ctx,
1920                                         struct security_descriptor **ppdesc)
1921 {
1922         time_t timestamp = 0;
1923         char *stripped = NULL;
1924         NTSTATUS status;
1925         char *conv;
1926         struct smb_filename *conv_smb_fname = NULL;
1927
1928         if (!shadow_copy2_strip_snapshot(talloc_tos(),
1929                                         handle,
1930                                         smb_fname->base_name,
1931                                         &timestamp,
1932                                         &stripped)) {
1933                 return map_nt_error_from_unix(errno);
1934         }
1935         if (timestamp == 0) {
1936                 return SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1937                                                mem_ctx, ppdesc);
1938         }
1939         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1940         TALLOC_FREE(stripped);
1941         if (conv == NULL) {
1942                 return map_nt_error_from_unix(errno);
1943         }
1944         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1945                                         conv,
1946                                         NULL,
1947                                         NULL,
1948                                         smb_fname->flags);
1949         if (conv_smb_fname == NULL) {
1950                 TALLOC_FREE(conv);
1951                 return NT_STATUS_NO_MEMORY;
1952         }
1953         status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv_smb_fname, security_info,
1954                                          mem_ctx, ppdesc);
1955         TALLOC_FREE(conv);
1956         TALLOC_FREE(conv_smb_fname);
1957         return status;
1958 }
1959
1960 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
1961                                 const struct smb_filename *smb_fname,
1962                                 mode_t mode)
1963 {
1964         time_t timestamp = 0;
1965         char *stripped = NULL;
1966         int ret, saved_errno;
1967         char *conv;
1968         struct smb_filename *conv_smb_fname = NULL;
1969
1970         if (!shadow_copy2_strip_snapshot(talloc_tos(),
1971                                         handle,
1972                                         smb_fname->base_name,
1973                                         &timestamp,
1974                                         &stripped)) {
1975                 return -1;
1976         }
1977         if (timestamp == 0) {
1978                 return SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
1979         }
1980         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1981         TALLOC_FREE(stripped);
1982         if (conv == NULL) {
1983                 return -1;
1984         }
1985         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
1986                                         conv,
1987                                         NULL,
1988                                         NULL,
1989                                         smb_fname->flags);
1990         if (conv_smb_fname == NULL) {
1991                 TALLOC_FREE(conv);
1992                 return -1;
1993         }
1994         ret = SMB_VFS_NEXT_MKDIR(handle, conv_smb_fname, mode);
1995         saved_errno = errno;
1996         TALLOC_FREE(conv);
1997         TALLOC_FREE(conv_smb_fname);
1998         errno = saved_errno;
1999         return ret;
2000 }
2001
2002 static int shadow_copy2_rmdir(vfs_handle_struct *handle,
2003                                 const struct smb_filename *smb_fname)
2004 {
2005         time_t timestamp = 0;
2006         char *stripped = NULL;
2007         int ret, saved_errno;
2008         char *conv;
2009         struct smb_filename *conv_smb_fname = NULL;
2010
2011         if (!shadow_copy2_strip_snapshot(talloc_tos(),
2012                                         handle,
2013                                         smb_fname->base_name,
2014                                         &timestamp,
2015                                         &stripped)) {
2016                 return -1;
2017         }
2018         if (timestamp == 0) {
2019                 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
2020         }
2021         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2022         TALLOC_FREE(stripped);
2023         if (conv == NULL) {
2024                 return -1;
2025         }
2026         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2027                                         conv,
2028                                         NULL,
2029                                         NULL,
2030                                         smb_fname->flags);
2031         if (conv_smb_fname == NULL) {
2032                 TALLOC_FREE(conv);
2033                 return -1;
2034         }
2035         ret = SMB_VFS_NEXT_RMDIR(handle, conv_smb_fname);
2036         saved_errno = errno;
2037         TALLOC_FREE(conv_smb_fname);
2038         TALLOC_FREE(conv);
2039         errno = saved_errno;
2040         return ret;
2041 }
2042
2043 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
2044                                 unsigned int flags)
2045 {
2046         time_t timestamp = 0;
2047         char *stripped = NULL;
2048         int ret, saved_errno;
2049         char *conv;
2050
2051         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2052                                          &timestamp, &stripped)) {
2053                 return -1;
2054         }
2055         if (timestamp == 0) {
2056                 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
2057         }
2058         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2059         TALLOC_FREE(stripped);
2060         if (conv == NULL) {
2061                 return -1;
2062         }
2063         ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
2064         saved_errno = errno;
2065         TALLOC_FREE(conv);
2066         errno = saved_errno;
2067         return ret;
2068 }
2069
2070 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
2071                                      const char *fname, const char *aname,
2072                                      void *value, size_t size)
2073 {
2074         time_t timestamp = 0;
2075         char *stripped = NULL;
2076         ssize_t ret;
2077         int saved_errno;
2078         char *conv;
2079
2080         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2081                                          &timestamp, &stripped)) {
2082                 return -1;
2083         }
2084         if (timestamp == 0) {
2085                 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
2086                                              size);
2087         }
2088         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2089         TALLOC_FREE(stripped);
2090         if (conv == NULL) {
2091                 return -1;
2092         }
2093         ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
2094         saved_errno = errno;
2095         TALLOC_FREE(conv);
2096         errno = saved_errno;
2097         return ret;
2098 }
2099
2100 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
2101                                       const char *fname,
2102                                       char *list, size_t size)
2103 {
2104         time_t timestamp = 0;
2105         char *stripped = NULL;
2106         ssize_t ret;
2107         int saved_errno;
2108         char *conv;
2109
2110         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2111                                          &timestamp, &stripped)) {
2112                 return -1;
2113         }
2114         if (timestamp == 0) {
2115                 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
2116         }
2117         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2118         TALLOC_FREE(stripped);
2119         if (conv == NULL) {
2120                 return -1;
2121         }
2122         ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
2123         saved_errno = errno;
2124         TALLOC_FREE(conv);
2125         errno = saved_errno;
2126         return ret;
2127 }
2128
2129 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
2130                                     const char *fname, const char *aname)
2131 {
2132         time_t timestamp = 0;
2133         char *stripped = NULL;
2134         int ret, saved_errno;
2135         char *conv;
2136
2137         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2138                                          &timestamp, &stripped)) {
2139                 return -1;
2140         }
2141         if (timestamp == 0) {
2142                 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
2143         }
2144         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2145         TALLOC_FREE(stripped);
2146         if (conv == NULL) {
2147                 return -1;
2148         }
2149         ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
2150         saved_errno = errno;
2151         TALLOC_FREE(conv);
2152         errno = saved_errno;
2153         return ret;
2154 }
2155
2156 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
2157                                  const char *fname,
2158                                  const char *aname, const void *value,
2159                                  size_t size, int flags)
2160 {
2161         time_t timestamp = 0;
2162         char *stripped = NULL;
2163         ssize_t ret;
2164         int saved_errno;
2165         char *conv;
2166
2167         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2168                                          &timestamp, &stripped)) {
2169                 return -1;
2170         }
2171         if (timestamp == 0) {
2172                 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
2173                                              flags);
2174         }
2175         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2176         TALLOC_FREE(stripped);
2177         if (conv == NULL) {
2178                 return -1;
2179         }
2180         ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
2181         saved_errno = errno;
2182         TALLOC_FREE(conv);
2183         errno = saved_errno;
2184         return ret;
2185 }
2186
2187 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
2188                         const struct smb_filename *smb_fname,
2189                         mode_t mode)
2190 {
2191         time_t timestamp = 0;
2192         char *stripped = NULL;
2193         ssize_t ret;
2194         int saved_errno;
2195         char *conv = NULL;
2196         struct smb_filename *conv_smb_fname = NULL;
2197
2198         if (!shadow_copy2_strip_snapshot(talloc_tos(),
2199                                 handle,
2200                                 smb_fname->base_name,
2201                                 &timestamp,
2202                                 &stripped)) {
2203                 return -1;
2204         }
2205         if (timestamp == 0) {
2206                 return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
2207         }
2208         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2209         TALLOC_FREE(stripped);
2210         if (conv == NULL) {
2211                 return -1;
2212         }
2213         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
2214                                         conv,
2215                                         NULL,
2216                                         NULL,
2217                                         smb_fname->flags);
2218         if (conv_smb_fname == NULL) {
2219                 TALLOC_FREE(conv);
2220                 errno = ENOMEM;
2221                 return -1;
2222         }
2223         ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv_smb_fname, mode);
2224         saved_errno = errno;
2225         TALLOC_FREE(conv);
2226         TALLOC_FREE(conv_smb_fname);
2227         errno = saved_errno;
2228         return ret;
2229 }
2230
2231 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
2232                                           const char *path,
2233                                           const char *name,
2234                                           TALLOC_CTX *mem_ctx,
2235                                           char **found_name)
2236 {
2237         time_t timestamp = 0;
2238         char *stripped = NULL;
2239         ssize_t ret;
2240         int saved_errno;
2241         char *conv;
2242
2243         DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
2244                    "name=[%s]\n", path, name));
2245
2246         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
2247                                          &timestamp, &stripped)) {
2248                 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
2249                 return -1;
2250         }
2251         if (timestamp == 0) {
2252                 DEBUG(10, ("timestamp == 0\n"));
2253                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
2254                                                       mem_ctx, found_name);
2255         }
2256         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2257         TALLOC_FREE(stripped);
2258         if (conv == NULL) {
2259                 DEBUG(10, ("shadow_copy2_convert failed\n"));
2260                 return -1;
2261         }
2262         DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
2263                    "name=[%s]\n", conv, name));
2264         ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
2265                                              mem_ctx, found_name);
2266         DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
2267         saved_errno = errno;
2268         TALLOC_FREE(conv);
2269         errno = saved_errno;
2270         return ret;
2271 }
2272
2273 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
2274                                             const char *fname)
2275 {
2276         time_t timestamp = 0;
2277         char *stripped = NULL;
2278         char *tmp = NULL;
2279         char *result = NULL;
2280         char *parent_dir = NULL;
2281         int saved_errno;
2282         size_t rootpath_len = 0;
2283
2284         DBG_DEBUG("Calc connect path for [%s]\n", fname);
2285
2286         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
2287                                          &timestamp, &stripped)) {
2288                 goto done;
2289         }
2290         if (timestamp == 0) {
2291                 return SMB_VFS_NEXT_CONNECTPATH(handle, fname);
2292         }
2293
2294         tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
2295                                       &rootpath_len);
2296         if (tmp == NULL) {
2297                 if (errno != ENOENT) {
2298                         goto done;
2299                 }
2300
2301                 /*
2302                  * If the converted path does not exist, and converting
2303                  * the parent yields something that does exist, then
2304                  * this path refers to something that has not been
2305                  * created yet, relative to the parent path.
2306                  * The snapshot finding is relative to the parent.
2307                  * (usually snapshots are read/only but this is not
2308                  * necessarily true).
2309                  * This code also covers getting a wildcard in the
2310                  * last component, because this function is called
2311                  * prior to sanitizing the path, and in SMB1 we may
2312                  * get wildcards in path names.
2313                  */
2314                 if (!parent_dirname(talloc_tos(), stripped, &parent_dir,
2315                                     NULL)) {
2316                         errno = ENOMEM;
2317                         goto done;
2318                 }
2319
2320                 tmp = shadow_copy2_do_convert(talloc_tos(), handle, parent_dir,
2321                                               timestamp, &rootpath_len);
2322                 if (tmp == NULL) {
2323                         goto done;
2324                 }
2325         }
2326
2327         DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
2328                   (int)rootpath_len, tmp);
2329
2330         tmp[rootpath_len] = '\0';
2331         result = SMB_VFS_NEXT_REALPATH(handle, tmp);
2332         if (result == NULL) {
2333                 goto done;
2334         }
2335
2336         DBG_DEBUG("connect path is [%s]\n", result);
2337
2338 done:
2339         saved_errno = errno;
2340         TALLOC_FREE(tmp);
2341         TALLOC_FREE(stripped);
2342         TALLOC_FREE(parent_dir);
2343         errno = saved_errno;
2344         return result;
2345 }
2346
2347 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
2348                                        const char *path, uint64_t *bsize,
2349                                        uint64_t *dfree, uint64_t *dsize)
2350 {
2351         time_t timestamp = 0;
2352         char *stripped = NULL;
2353         ssize_t ret;
2354         int saved_errno;
2355         char *conv;
2356
2357         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
2358                                          &timestamp, &stripped)) {
2359                 return -1;
2360         }
2361         if (timestamp == 0) {
2362                 return SMB_VFS_NEXT_DISK_FREE(handle, path,
2363                                               bsize, dfree, dsize);
2364         }
2365
2366         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2367         TALLOC_FREE(stripped);
2368         if (conv == NULL) {
2369                 return -1;
2370         }
2371
2372         ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, bsize, dfree, dsize);
2373
2374         saved_errno = errno;
2375         TALLOC_FREE(conv);
2376         errno = saved_errno;
2377
2378         return ret;
2379 }
2380
2381 static int shadow_copy2_get_quota(vfs_handle_struct *handle, const char *path,
2382                                   enum SMB_QUOTA_TYPE qtype, unid_t id,
2383                                   SMB_DISK_QUOTA *dq)
2384 {
2385         time_t timestamp = 0;
2386         char *stripped = NULL;
2387         int ret;
2388         int saved_errno;
2389         char *conv;
2390
2391         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path, &timestamp,
2392                                          &stripped)) {
2393                 return -1;
2394         }
2395         if (timestamp == 0) {
2396                 return SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, dq);
2397         }
2398
2399         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
2400         TALLOC_FREE(stripped);
2401         if (conv == NULL) {
2402                 return -1;
2403         }
2404
2405         ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv, qtype, id, dq);
2406
2407         saved_errno = errno;
2408         TALLOC_FREE(conv);
2409         errno = saved_errno;
2410
2411         return ret;
2412 }
2413
2414 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
2415                                 const char *service, const char *user)
2416 {
2417         struct shadow_copy2_config *config;
2418         struct shadow_copy2_private *priv;
2419         int ret;
2420         const char *snapdir;
2421         const char *snapprefix = NULL;
2422         const char *delimiter;
2423         const char *gmt_format;
2424         const char *sort_order;
2425         const char *basedir = NULL;
2426         const char *snapsharepath = NULL;
2427         const char *mount_point;
2428
2429         DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
2430                    (unsigned)handle->conn->cnum,
2431                    handle->conn->connectpath));
2432
2433         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
2434         if (ret < 0) {
2435                 return ret;
2436         }
2437
2438         priv = talloc_zero(handle->conn, struct shadow_copy2_private);
2439         if (priv == NULL) {
2440                 DBG_ERR("talloc_zero() failed\n");
2441                 errno = ENOMEM;
2442                 return -1;
2443         }
2444
2445         priv->snaps = talloc_zero(priv, struct shadow_copy2_snaplist_info);
2446         if (priv->snaps == NULL) {
2447                 DBG_ERR("talloc_zero() failed\n");
2448                 errno = ENOMEM;
2449                 return -1;
2450         }
2451
2452         config = talloc_zero(priv, struct shadow_copy2_config);
2453         if (config == NULL) {
2454                 DEBUG(0, ("talloc_zero() failed\n"));
2455                 errno = ENOMEM;
2456                 return -1;
2457         }
2458
2459         priv->config = config;
2460
2461         gmt_format = lp_parm_const_string(SNUM(handle->conn),
2462                                           "shadow", "format",
2463                                           GMT_FORMAT);
2464         config->gmt_format = talloc_strdup(config, gmt_format);
2465         if (config->gmt_format == NULL) {
2466                 DEBUG(0, ("talloc_strdup() failed\n"));
2467                 errno = ENOMEM;
2468                 return -1;
2469         }
2470
2471         config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
2472                                           "shadow", "sscanf", false);
2473
2474         config->use_localtime = lp_parm_bool(SNUM(handle->conn),
2475                                              "shadow", "localtime",
2476                                              false);
2477
2478         snapdir = lp_parm_const_string(SNUM(handle->conn),
2479                                        "shadow", "snapdir",
2480                                        ".snapshots");
2481         config->snapdir = talloc_strdup(config, snapdir);
2482         if (config->snapdir == NULL) {
2483                 DEBUG(0, ("talloc_strdup() failed\n"));
2484                 errno = ENOMEM;
2485                 return -1;
2486         }
2487
2488         snapprefix = lp_parm_const_string(SNUM(handle->conn),
2489                                        "shadow", "snapprefix",
2490                                        NULL);
2491         if (snapprefix != NULL) {
2492                 priv->snaps->regex = talloc_zero(priv->snaps, regex_t);
2493                 if (priv->snaps->regex == NULL) {
2494                         DBG_ERR("talloc_zero() failed\n");
2495                         errno = ENOMEM;
2496                         return -1;
2497                 }
2498
2499                 /* pre-compute regex rule for matching pattern later */
2500                 ret = regcomp(priv->snaps->regex, snapprefix, 0);
2501                 if (ret) {
2502                         DBG_ERR("Failed to create regex object\n");
2503                         return -1;
2504                 }
2505         }
2506
2507         delimiter = lp_parm_const_string(SNUM(handle->conn),
2508                                        "shadow", "delimiter",
2509                                        "_GMT");
2510         if (delimiter != NULL) {
2511                 priv->config->delimiter = talloc_strdup(priv->config, delimiter);
2512                 if (priv->config->delimiter == NULL) {
2513                         DBG_ERR("talloc_strdup() failed\n");
2514                         errno = ENOMEM;
2515                         return -1;
2516                 }
2517         }
2518
2519         config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
2520                                                   "shadow",
2521                                                   "snapdirseverywhere",
2522                                                   false);
2523
2524         config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
2525                                                 "shadow", "crossmountpoints",
2526                                                 false);
2527
2528         if (config->crossmountpoints && !config->snapdirseverywhere) {
2529                 DBG_WARNING("Warning: 'crossmountpoints' depends on "
2530                             "'snapdirseverywhere'. Disabling crossmountpoints.\n");
2531         }
2532
2533         config->fixinodes = lp_parm_bool(SNUM(handle->conn),
2534                                          "shadow", "fixinodes",
2535                                          false);
2536
2537         sort_order = lp_parm_const_string(SNUM(handle->conn),
2538                                           "shadow", "sort", "desc");
2539         config->sort_order = talloc_strdup(config, sort_order);
2540         if (config->sort_order == NULL) {
2541                 DEBUG(0, ("talloc_strdup() failed\n"));
2542                 errno = ENOMEM;
2543                 return -1;
2544         }
2545
2546         mount_point = lp_parm_const_string(SNUM(handle->conn),
2547                                            "shadow", "mountpoint", NULL);
2548         if (mount_point != NULL) {
2549                 if (mount_point[0] != '/') {
2550                         DEBUG(1, (__location__ " Warning: 'mountpoint' is "
2551                                   "relative ('%s'), but it has to be an "
2552                                   "absolute path. Ignoring provided value.\n",
2553                                   mount_point));
2554                         mount_point = NULL;
2555                 } else {
2556                         char *p;
2557                         p = strstr(handle->conn->connectpath, mount_point);
2558                         if (p != handle->conn->connectpath) {
2559                                 DBG_WARNING("Warning: the share root (%s) is "
2560                                             "not a subdirectory of the "
2561                                             "specified mountpoint (%s). "
2562                                             "Ignoring provided value.\n",
2563                                             handle->conn->connectpath,
2564                                             mount_point);
2565                                 mount_point = NULL;
2566                         }
2567                 }
2568         }
2569
2570         if (mount_point != NULL) {
2571                 config->mount_point = talloc_strdup(config, mount_point);
2572                 if (config->mount_point == NULL) {
2573                         DEBUG(0, (__location__ " talloc_strdup() failed\n"));
2574                         return -1;
2575                 }
2576         } else {
2577                 config->mount_point = shadow_copy2_find_mount_point(config,
2578                                                                     handle);
2579                 if (config->mount_point == NULL) {
2580                         DBG_WARNING("shadow_copy2_find_mount_point "
2581                                     "of the share root '%s' failed: %s\n",
2582                                     handle->conn->connectpath, strerror(errno));
2583                         return -1;
2584                 }
2585         }
2586
2587         basedir = lp_parm_const_string(SNUM(handle->conn),
2588                                        "shadow", "basedir", NULL);
2589
2590         if (basedir != NULL) {
2591                 if (basedir[0] != '/') {
2592                         DEBUG(1, (__location__ " Warning: 'basedir' is "
2593                                   "relative ('%s'), but it has to be an "
2594                                   "absolute path. Disabling basedir.\n",
2595                                   basedir));
2596                         basedir = NULL;
2597                 } else {
2598                         char *p;
2599                         p = strstr(basedir, config->mount_point);
2600                         if (p != basedir) {
2601                                 DEBUG(1, ("Warning: basedir (%s) is not a "
2602                                           "subdirectory of the share root's "
2603                                           "mount point (%s). "
2604                                           "Disabling basedir\n",
2605                                           basedir, config->mount_point));
2606                                 basedir = NULL;
2607                         }
2608                 }
2609         }
2610
2611         if (config->snapdirseverywhere && basedir != NULL) {
2612                 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
2613                           "with 'snapdirseverywhere'. Disabling basedir.\n"));
2614                 basedir = NULL;
2615         }
2616
2617         snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
2618                                              "snapsharepath", NULL);
2619         if (snapsharepath != NULL) {
2620                 if (snapsharepath[0] == '/') {
2621                         DBG_WARNING("Warning: 'snapsharepath' is "
2622                                     "absolute ('%s'), but it has to be a "
2623                                     "relative path. Disabling snapsharepath.\n",
2624                                     snapsharepath);
2625                         snapsharepath = NULL;
2626                 }
2627                 if (config->snapdirseverywhere && snapsharepath != NULL) {
2628                         DBG_WARNING("Warning: 'snapsharepath' is incompatible "
2629                                     "with 'snapdirseverywhere'. Disabling "
2630                                     "snapsharepath.\n");
2631                         snapsharepath = NULL;
2632                 }
2633         }
2634
2635         if (basedir != NULL && snapsharepath != NULL) {
2636                 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
2637                             "'basedir'. Disabling snapsharepath\n");
2638                 snapsharepath = NULL;
2639         }
2640
2641         if (snapsharepath != NULL) {
2642                 config->rel_connectpath = talloc_strdup(config, snapsharepath);
2643                 if (config->rel_connectpath == NULL) {
2644                         DBG_ERR("talloc_strdup() failed\n");
2645                         errno = ENOMEM;
2646                         return -1;
2647                 }
2648         }
2649
2650         if (basedir == NULL) {
2651                 basedir = config->mount_point;
2652         }
2653
2654         if (config->rel_connectpath == NULL &&
2655             strlen(basedir) < strlen(handle->conn->connectpath)) {
2656                 config->rel_connectpath = talloc_strdup(config,
2657                         handle->conn->connectpath + strlen(basedir));
2658                 if (config->rel_connectpath == NULL) {
2659                         DEBUG(0, ("talloc_strdup() failed\n"));
2660                         errno = ENOMEM;
2661                         return -1;
2662                 }
2663         }
2664
2665         if (config->snapdir[0] == '/') {
2666                 config->snapdir_absolute = true;
2667
2668                 if (config->snapdirseverywhere == true) {
2669                         DEBUG(1, (__location__ " Warning: An absolute snapdir "
2670                                   "is incompatible with 'snapdirseverywhere', "
2671                                   "setting 'snapdirseverywhere' to false.\n"));
2672                         config->snapdirseverywhere = false;
2673                 }
2674
2675                 if (config->crossmountpoints == true) {
2676                         DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
2677                                   "is not supported with an absolute snapdir. "
2678                                   "Disabling it.\n"));
2679                         config->crossmountpoints = false;
2680                 }
2681
2682                 config->snapshot_basepath = config->snapdir;
2683         } else {
2684                 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
2685                                 config->mount_point, config->snapdir);
2686                 if (config->snapshot_basepath == NULL) {
2687                         DEBUG(0, ("talloc_asprintf() failed\n"));
2688                         errno = ENOMEM;
2689                         return -1;
2690                 }
2691         }
2692
2693         trim_string(config->mount_point, NULL, "/");
2694         trim_string(config->rel_connectpath, "/", "/");
2695         trim_string(config->snapdir, NULL, "/");
2696         trim_string(config->snapshot_basepath, NULL, "/");
2697
2698         DEBUG(10, ("shadow_copy2_connect: configuration:\n"
2699                    "  share root: '%s'\n"
2700                    "  mountpoint: '%s'\n"
2701                    "  rel share root: '%s'\n"
2702                    "  snapdir: '%s'\n"
2703                    "  snapprefix: '%s'\n"
2704                    "  delimiter: '%s'\n"
2705                    "  snapshot base path: '%s'\n"
2706                    "  format: '%s'\n"
2707                    "  use sscanf: %s\n"
2708                    "  snapdirs everywhere: %s\n"
2709                    "  cross mountpoints: %s\n"
2710                    "  fix inodes: %s\n"
2711                    "  sort order: %s\n"
2712                    "",
2713                    handle->conn->connectpath,
2714                    config->mount_point,
2715                    config->rel_connectpath,
2716                    config->snapdir,
2717                    snapprefix,
2718                    config->delimiter,
2719                    config->snapshot_basepath,
2720                    config->gmt_format,
2721                    config->use_sscanf ? "yes" : "no",
2722                    config->snapdirseverywhere ? "yes" : "no",
2723                    config->crossmountpoints ? "yes" : "no",
2724                    config->fixinodes ? "yes" : "no",
2725                    config->sort_order
2726                    ));
2727
2728
2729         SMB_VFS_HANDLE_SET_DATA(handle, priv,
2730                                 NULL, struct shadow_copy2_private,
2731                                 return -1);
2732
2733         return 0;
2734 }
2735
2736 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
2737         .connect_fn = shadow_copy2_connect,
2738         .opendir_fn = shadow_copy2_opendir,
2739         .disk_free_fn = shadow_copy2_disk_free,
2740         .get_quota_fn = shadow_copy2_get_quota,
2741         .rename_fn = shadow_copy2_rename,
2742         .link_fn = shadow_copy2_link,
2743         .symlink_fn = shadow_copy2_symlink,
2744         .stat_fn = shadow_copy2_stat,
2745         .lstat_fn = shadow_copy2_lstat,
2746         .fstat_fn = shadow_copy2_fstat,
2747         .open_fn = shadow_copy2_open,
2748         .unlink_fn = shadow_copy2_unlink,
2749         .chmod_fn = shadow_copy2_chmod,
2750         .chown_fn = shadow_copy2_chown,
2751         .chdir_fn = shadow_copy2_chdir,
2752         .ntimes_fn = shadow_copy2_ntimes,
2753         .readlink_fn = shadow_copy2_readlink,
2754         .mknod_fn = shadow_copy2_mknod,
2755         .realpath_fn = shadow_copy2_realpath,
2756         .get_nt_acl_fn = shadow_copy2_get_nt_acl,
2757         .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
2758         .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
2759         .mkdir_fn = shadow_copy2_mkdir,
2760         .rmdir_fn = shadow_copy2_rmdir,
2761         .getxattr_fn = shadow_copy2_getxattr,
2762         .listxattr_fn = shadow_copy2_listxattr,
2763         .removexattr_fn = shadow_copy2_removexattr,
2764         .setxattr_fn = shadow_copy2_setxattr,
2765         .chmod_acl_fn = shadow_copy2_chmod_acl,
2766         .chflags_fn = shadow_copy2_chflags,
2767         .get_real_filename_fn = shadow_copy2_get_real_filename,
2768         .connectpath_fn = shadow_copy2_connectpath,
2769 };
2770
2771 NTSTATUS vfs_shadow_copy2_init(void);
2772 NTSTATUS vfs_shadow_copy2_init(void)
2773 {
2774         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2775                                 "shadow_copy2", &vfs_shadow_copy2_fns);
2776 }