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