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