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