LCOV - code coverage report
Current view: top level - source3/modules - vfs_shadow_copy2.c (source / functions) Hit Total Coverage
Test: coverage report for support-claim-type-attributes 6b5c566e Lines: 956 1452 65.8 %
Date: 2023-11-21 12:31:41 Functions: 51 59 86.4 %

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

Generated by: LCOV version 1.14