LCOV - code coverage report
Current view: top level - source3/modules - vfs_shadow_copy2.c (source / functions) Hit Total Coverage
Test: coverage report for recycleplus df22b230 Lines: 2 1492 0.1 %
Date: 2024-02-14 10:14:15 Functions: 1 59 1.7 %

          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 implemetation of a shadow copy module for exposing
      28             :  * file system snapshots to windows clients as shadow copies.
      29             :  *
      30             :  * See the manual page for documentation.
      31             :  */
      32             : 
      33             : #include "includes.h"
      34             : #include "smbd/smbd.h"
      35             : #include "system/filesys.h"
      36             : #include "include/ntioctl.h"
      37             : #include "util_tdb.h"
      38             : #include "lib/util_path.h"
      39             : #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           0 : static struct shadow_copy2_snapentry *shadow_copy2_create_snapentry(
     100             :                                         struct shadow_copy2_private *priv)
     101             : {
     102           0 :         struct shadow_copy2_snapentry *tmpentry = NULL;
     103             : 
     104           0 :         tmpentry = talloc_zero(priv->snaps, struct shadow_copy2_snapentry);
     105           0 :         if (tmpentry == NULL) {
     106           0 :                 DBG_ERR("talloc_zero() failed\n");
     107           0 :                 errno = ENOMEM;
     108           0 :                 return NULL;
     109             :         }
     110             : 
     111           0 :         DLIST_ADD(priv->snaps->snaplist, tmpentry);
     112             : 
     113           0 :         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           0 : static void shadow_copy2_delete_snaplist(struct shadow_copy2_private *priv)
     123             : {
     124           0 :         struct shadow_copy2_snapentry *tmp = NULL;
     125             : 
     126           0 :         while ((tmp = priv->snaps->snaplist) != NULL) {
     127           0 :                 DLIST_REMOVE(priv->snaps->snaplist, tmp);
     128           0 :                 talloc_free(tmp);
     129             :         }
     130           0 : }
     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           0 : 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           0 :         ssize_t snaptime_len = -1;
     148           0 :         struct shadow_copy2_snapentry *entry = NULL;
     149             : 
     150           0 :         snaptime_len = strftime(snap_str, len, GMT_FORMAT, timestamp);
     151           0 :         if (snaptime_len == 0) {
     152           0 :                 DBG_ERR("strftime failed\n");
     153           0 :                 return -1;
     154             :         }
     155             : 
     156           0 :         snaptime_len = -1;
     157             : 
     158           0 :         for (entry = priv->snaps->snaplist; entry; entry = entry->next) {
     159           0 :                 if (strcmp(entry->time_fmt, snap_str) == 0) {
     160           0 :                         snaptime_len = snprintf(snap_str, len, "%s",
     161             :                                                 entry->snapname);
     162           0 :                         return snaptime_len;
     163             :                 }
     164             :         }
     165             : 
     166           0 :         snap_str[0] = 0;
     167           0 :         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           0 : static bool shadow_copy2_update_snaplist(struct vfs_handle_struct *handle,
     184             :                 time_t snap_time)
     185             : {
     186           0 :         int ret = -1;
     187           0 :         bool snaplist_updated = false;
     188           0 :         struct files_struct fsp = {0};
     189           0 :         struct smb_filename smb_fname = {0};
     190           0 :         double seconds = 0.0;
     191           0 :         struct shadow_copy2_private *priv = NULL;
     192             : 
     193           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
     194             :                                 return false);
     195             : 
     196           0 :         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           0 :         if (seconds > 0 || (priv->snaps->snaplist == NULL)) {
     204           0 :                 smb_fname.base_name = discard_const_p(char, ".");
     205           0 :                 fsp.fsp_name = &smb_fname;
     206             : 
     207           0 :                 ret = shadow_copy2_get_shadow_copy_data(handle, &fsp,
     208             :                                                         NULL, false);
     209           0 :                 if (ret == 0) {
     210           0 :                         snaplist_updated = true;
     211             :                 } else {
     212           0 :                         DBG_ERR("Failed to get shadow copy data\n");
     213             :                 }
     214             : 
     215             :         }
     216             : 
     217           0 :         return snaplist_updated;
     218             : }
     219             : 
     220           0 : 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           0 :         num_offsets = 0;
     229             : 
     230           0 :         p = str;
     231           0 :         while ((p = strchr(p, '/')) != NULL) {
     232           0 :                 num_offsets += 1;
     233           0 :                 p += 1;
     234             :         }
     235             : 
     236           0 :         offsets = talloc_array(mem_ctx, size_t, num_offsets);
     237           0 :         if (offsets == NULL) {
     238           0 :                 return false;
     239             :         }
     240             : 
     241           0 :         p = str;
     242           0 :         num_offsets = 0;
     243           0 :         while ((p = strchr(p, '/')) != NULL) {
     244           0 :                 offsets[num_offsets] = p-str;
     245           0 :                 num_offsets += 1;
     246           0 :                 p += 1;
     247             :         }
     248             : 
     249           0 :         *poffsets = offsets;
     250           0 :         *pnum_offsets = num_offsets;
     251           0 :         return true;
     252             : }
     253             : 
     254             : /**
     255             :  * Given a timestamp, build the posix level GMT-tag string
     256             :  * based on the configurable format.
     257             :  */
     258           0 : 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           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
     269             :                                 return 0);
     270             : 
     271           0 :         config = priv->config;
     272             : 
     273           0 :         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           0 :                 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           0 :                         if (gmtime_r(&snapshot, &snap_tm) == 0) {
     290           0 :                                 DEBUG(10, ("gmtime_r failed\n"));
     291           0 :                                 return -1;
     292             :                         }
     293             :                 }
     294             : 
     295           0 :                 if (priv->snaps->regex != NULL) {
     296           0 :                         snaptime_len = shadow_copy2_saved_snapname(priv,
     297             :                                                 &snap_tm, snaptime_string, len);
     298           0 :                         if (snaptime_len >= 0)
     299           0 :                                 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           0 :                         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           0 :                         return shadow_copy2_saved_snapname(priv,
     313             :                                                 &snap_tm, snaptime_string, len);
     314             :                 }
     315             : 
     316           0 :                 snaptime_len = strftime(snaptime_string,
     317             :                                         len,
     318           0 :                                         config->gmt_format,
     319             :                                         &snap_tm);
     320           0 :                 if (snaptime_len == 0) {
     321           0 :                         DEBUG(10, ("strftime failed\n"));
     322           0 :                         return -1;
     323             :                 }
     324             :         }
     325             : 
     326           0 :         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           0 : 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           0 :         ssize_t snaptime_len = 0;
     346           0 :         char *result = NULL;
     347             :         struct shadow_copy2_config *config;
     348             :         struct shadow_copy2_private *priv;
     349             : 
     350           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
     351             :                                 return NULL);
     352             : 
     353           0 :         config = priv->config;
     354             : 
     355           0 :         snaptime_len = shadow_copy2_posix_gmt_string(handle,
     356             :                                                      snapshot,
     357             :                                                      snaptime_string,
     358             :                                                      sizeof(snaptime_string));
     359           0 :         if (snaptime_len <= 0) {
     360           0 :                 return NULL;
     361             :         }
     362             : 
     363           0 :         if (config->snapdir_absolute) {
     364           0 :                 result = talloc_asprintf(mem_ctx, "%s/%s",
     365             :                                          config->snapdir, snaptime_string);
     366             :         } else {
     367           0 :                 result = talloc_asprintf(mem_ctx, "/%s/%s",
     368             :                                          config->snapdir, snaptime_string);
     369             :         }
     370           0 :         if (result == NULL) {
     371           0 :                 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
     372             :         }
     373             : 
     374           0 :         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           0 : 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           0 :         ssize_t snaptime_len = 0;
     391           0 :         char *result = NULL;
     392             :         struct shadow_copy2_private *priv;
     393             : 
     394           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
     395             :                                 return NULL);
     396             : 
     397           0 :         snaptime_len = shadow_copy2_posix_gmt_string(handle,
     398             :                                                      snapshot,
     399             :                                                      snaptime_string,
     400             :                                                      sizeof(snaptime_string));
     401           0 :         if (snaptime_len <= 0) {
     402           0 :                 return NULL;
     403             :         }
     404             : 
     405           0 :         result = talloc_asprintf(mem_ctx, "%s/%s",
     406           0 :                                  priv->config->snapshot_basepath, snaptime_string);
     407           0 :         if (result == NULL) {
     408           0 :                 DEBUG(1, (__location__ " talloc_asprintf failed\n"));
     409             :         }
     410             : 
     411           0 :         return result;
     412             : }
     413             : 
     414           0 : static char *make_path_absolute(TALLOC_CTX *mem_ctx,
     415             :                                 struct shadow_copy2_private *priv,
     416             :                                 const char *name)
     417             : {
     418           0 :         char *newpath = NULL;
     419           0 :         char *abs_path = NULL;
     420             : 
     421           0 :         if (name[0] != '/') {
     422           0 :                 newpath = talloc_asprintf(mem_ctx,
     423             :                                         "%s/%s",
     424             :                                         priv->shadow_cwd,
     425             :                                         name);
     426           0 :                 if (newpath == NULL) {
     427           0 :                         return NULL;
     428             :                 }
     429           0 :                 name = newpath;
     430             :         }
     431           0 :         abs_path = canonicalize_absolute_path(mem_ctx, name);
     432           0 :         TALLOC_FREE(newpath);
     433           0 :         return abs_path;
     434             : }
     435             : 
     436             : /* Return a $cwd-relative path. */
     437           0 : static bool make_relative_path(const char *cwd, char *abs_path)
     438             : {
     439           0 :         size_t cwd_len = strlen(cwd);
     440           0 :         size_t abs_len = strlen(abs_path);
     441             : 
     442           0 :         if (abs_len < cwd_len) {
     443           0 :                 return false;
     444             :         }
     445           0 :         if (memcmp(abs_path, cwd, cwd_len) != 0) {
     446           0 :                 return false;
     447             :         }
     448             :         /* The cwd_len != 1 case is for $cwd == '/' */
     449           0 :         if (cwd_len != 1 &&
     450           0 :             abs_path[cwd_len] != '/' &&
     451           0 :             abs_path[cwd_len] != '\0')
     452             :         {
     453           0 :                 return false;
     454             :         }
     455           0 :         if (abs_path[cwd_len] == '/') {
     456           0 :                 cwd_len++;
     457             :         }
     458           0 :         memmove(abs_path, &abs_path[cwd_len], abs_len + 1 - cwd_len);
     459           0 :         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           0 : 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           0 :         size_t snapdirlen = 0;
     481           0 :         char *p = strstr_m(abs_path, priv->config->snapdir);
     482           0 :         char *q = NULL;
     483           0 :         char *connect_path = NULL;
     484             :         char snapshot[GMT_NAME_LEN+1];
     485             : 
     486           0 :         *ppath_already_converted = false;
     487             : 
     488           0 :         if (p == NULL) {
     489             :                 /* Must at least contain shadow:snapdir. */
     490           0 :                 return 0;
     491             :         }
     492             : 
     493           0 :         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           0 :         snapdirlen = strlen(priv->config->snapdir);
     500           0 :         if (p[snapdirlen] != '/') {
     501             :                 /* shadow:snapdir must end as a separate component. */
     502           0 :                 return 0;
     503             :         }
     504             : 
     505           0 :         if (p > abs_path && p[-1] != '/') {
     506             :                 /* shadow:snapdir must start as a separate component. */
     507           0 :                 return 0;
     508             :         }
     509             : 
     510           0 :         p += snapdirlen;
     511           0 :         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           0 :         q = strchr(p, '/');
     519           0 :         if (q == NULL) {
     520             :                 /*
     521             :                  * No next path component.
     522             :                  * Use entire string.
     523             :                  */
     524           0 :                 connect_path = talloc_strdup(mem_ctx,
     525             :                                         abs_path);
     526             :         } else {
     527           0 :                 connect_path = talloc_strndup(mem_ctx,
     528             :                                         abs_path,
     529           0 :                                         q - abs_path);
     530             :         }
     531           0 :         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           0 :         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           0 :         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           0 :         if (pconnectpath != NULL) {
     557           0 :                 *pconnectpath = connect_path;
     558             :         }
     559             : 
     560           0 :         *ppath_already_converted = true;
     561             : 
     562           0 :         DBG_DEBUG("path |%s| is already converted. "
     563             :                 "connect path = |%s|\n",
     564             :                 abs_path,
     565             :                 connect_path);
     566             : 
     567           0 :         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           0 : 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           0 :         char *stripped = NULL;
     596             :         struct shadow_copy2_private *priv;
     597           0 :         char *abs_path = NULL;
     598           0 :         bool ret = true;
     599           0 :         bool already_converted = false;
     600           0 :         int err = 0;
     601             : 
     602           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
     603             :                                 return false);
     604             : 
     605           0 :         DBG_DEBUG("[from %s()] Path '%s'\n",
     606             :                   function, smb_fname_str_dbg(smb_fname));
     607             : 
     608           0 :         if (_already_converted != NULL) {
     609           0 :                 *_already_converted = false;
     610             :         }
     611             : 
     612           0 :         abs_path = make_path_absolute(mem_ctx, priv, smb_fname->base_name);
     613           0 :         if (abs_path == NULL) {
     614           0 :                 ret = false;
     615           0 :                 goto out;
     616             :         }
     617             : 
     618           0 :         DBG_DEBUG("abs path '%s'\n", abs_path);
     619             : 
     620           0 :         err = check_for_converted_path(mem_ctx,
     621             :                                         handle,
     622             :                                         priv,
     623             :                                         abs_path,
     624             :                                         &already_converted,
     625             :                                         psnappath);
     626           0 :         if (err != 0) {
     627             :                 /* error in conversion. */
     628           0 :                 ret = false;
     629           0 :                 goto out;
     630             :         }
     631             : 
     632           0 :         if (already_converted) {
     633           0 :                 if (_already_converted != NULL) {
     634           0 :                         *_already_converted = true;
     635             :                 }
     636           0 :                 goto out;
     637             :         }
     638             : 
     639           0 :         if (smb_fname->twrp == 0) {
     640           0 :                 goto out;
     641             :         }
     642             : 
     643           0 :         if (ptimestamp != NULL) {
     644           0 :                 *ptimestamp = nt_time_to_unix(smb_fname->twrp);
     645             :         }
     646             : 
     647           0 :         if (pstripped != NULL) {
     648           0 :                 stripped = talloc_strdup(mem_ctx, abs_path);
     649           0 :                 if (stripped == NULL) {
     650           0 :                         ret = false;
     651           0 :                         goto out;
     652             :                 }
     653             : 
     654           0 :                 if (smb_fname->base_name[0] != '/') {
     655           0 :                         ret = make_relative_path(priv->shadow_cwd, stripped);
     656           0 :                         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           0 :                 *pstripped = stripped;
     666             :         }
     667             : 
     668           0 :         ret = true;
     669             : 
     670           0 :   out:
     671           0 :         TALLOC_FREE(abs_path);
     672           0 :         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           0 : 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           0 :         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           0 : 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           0 :         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           0 : static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
     727             :                                            vfs_handle_struct *handle)
     728             : {
     729           0 :         char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
     730             :         dev_t dev;
     731             :         struct stat st;
     732             :         char *p;
     733             : 
     734           0 :         if (stat(path, &st) != 0) {
     735           0 :                 talloc_free(path);
     736           0 :                 return NULL;
     737             :         }
     738             : 
     739           0 :         dev = st.st_dev;
     740             : 
     741           0 :         while ((p = strrchr(path, '/')) && p > path) {
     742           0 :                 *p = 0;
     743           0 :                 if (stat(path, &st) != 0) {
     744           0 :                         talloc_free(path);
     745           0 :                         return NULL;
     746             :                 }
     747           0 :                 if (st.st_dev != dev) {
     748           0 :                         *p = '/';
     749           0 :                         break;
     750             :                 }
     751             :         }
     752             : 
     753           0 :         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           0 : 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           0 :         char *result = NULL;
     770           0 :         size_t *slashes = NULL;
     771             :         unsigned num_slashes;
     772           0 :         char *path = NULL;
     773             :         size_t pathlen;
     774           0 :         char *insert = NULL;
     775           0 :         char *converted = NULL;
     776           0 :         size_t insertlen, connectlen = 0;
     777           0 :         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           0 :         size_t in_share_offset = 0;
     783             : 
     784           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
     785             :                                 return NULL);
     786             : 
     787           0 :         config = priv->config;
     788             : 
     789           0 :         DEBUG(10, ("converting '%s'\n", name));
     790             : 
     791           0 :         if (!config->snapdirseverywhere) {
     792             :                 int ret;
     793             :                 char *snapshot_path;
     794             : 
     795           0 :                 snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
     796             :                                                            handle,
     797             :                                                            timestamp);
     798           0 :                 if (snapshot_path == NULL) {
     799           0 :                         goto fail;
     800             :                 }
     801             : 
     802           0 :                 if (config->rel_connectpath == NULL) {
     803           0 :                         converted = talloc_asprintf(mem_ctx, "%s/%s",
     804             :                                                     snapshot_path, name);
     805             :                 } else {
     806           0 :                         converted = talloc_asprintf(mem_ctx, "%s/%s/%s",
     807             :                                                     snapshot_path,
     808             :                                                     config->rel_connectpath,
     809             :                                                     name);
     810             :                 }
     811           0 :                 if (converted == NULL) {
     812           0 :                         goto fail;
     813             :                 }
     814             : 
     815           0 :                 converted_fname = (struct smb_filename) {
     816             :                         .base_name = converted,
     817             :                 };
     818             : 
     819           0 :                 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
     820           0 :                 DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
     821             :                            converted,
     822             :                            ret, ret == 0 ? "ok" : strerror(errno)));
     823           0 :                 if (ret == 0) {
     824           0 :                         DEBUG(10, ("Found %s\n", converted));
     825           0 :                         result = converted;
     826           0 :                         converted = NULL;
     827           0 :                         if (snaproot_len != NULL) {
     828           0 :                                 *snaproot_len = strlen(snapshot_path);
     829           0 :                                 if (config->rel_connectpath != NULL) {
     830           0 :                                         *snaproot_len +=
     831           0 :                                             strlen(config->rel_connectpath) + 1;
     832             :                                 }
     833             :                         }
     834           0 :                         goto fail;
     835             :                 } else {
     836           0 :                         errno = ENOENT;
     837           0 :                         goto fail;
     838             :                 }
     839             :                 /* never reached ... */
     840             :         }
     841             : 
     842           0 :         connectlen = strlen(handle->conn->connectpath);
     843           0 :         if (name[0] == 0) {
     844           0 :                 path = talloc_strdup(mem_ctx, handle->conn->connectpath);
     845             :         } else {
     846           0 :                 path = talloc_asprintf(
     847           0 :                         mem_ctx, "%s/%s", handle->conn->connectpath, name);
     848             :         }
     849           0 :         if (path == NULL) {
     850           0 :                 errno = ENOMEM;
     851           0 :                 goto fail;
     852             :         }
     853           0 :         pathlen = talloc_get_size(path)-1;
     854             : 
     855           0 :         if (!shadow_copy2_find_slashes(talloc_tos(), path,
     856             :                                        &slashes, &num_slashes)) {
     857           0 :                 goto fail;
     858             :         }
     859             : 
     860           0 :         insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
     861           0 :         if (insert == NULL) {
     862           0 :                 goto fail;
     863             :         }
     864           0 :         insertlen = talloc_get_size(insert)-1;
     865             : 
     866             :         /*
     867             :          * Note: We deliberatly 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           0 :         converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
     876           0 :         if (converted == NULL) {
     877           0 :                 goto fail;
     878             :         }
     879             : 
     880           0 :         if (path[pathlen-1] != '/') {
     881             :                 /*
     882             :                  * Append a fake slash to find the snapshot root
     883             :                  */
     884             :                 size_t *tmp;
     885           0 :                 tmp = talloc_realloc(talloc_tos(), slashes,
     886             :                                      size_t, num_slashes+1);
     887           0 :                 if (tmp == NULL) {
     888           0 :                         goto fail;
     889             :                 }
     890           0 :                 slashes = tmp;
     891           0 :                 slashes[num_slashes] = pathlen;
     892           0 :                 num_slashes += 1;
     893             :         }
     894             : 
     895           0 :         min_offset = 0;
     896             : 
     897           0 :         if (!config->crossmountpoints) {
     898           0 :                 min_offset = strlen(config->mount_point);
     899             :         }
     900             : 
     901           0 :         memcpy(converted, path, pathlen+1);
     902           0 :         converted[pathlen+insertlen] = '\0';
     903             : 
     904           0 :         converted_fname = (struct smb_filename) {
     905             :                 .base_name = converted,
     906             :         };
     907             : 
     908           0 :         for (i = num_slashes-1; i>=0; i--) {
     909             :                 int ret;
     910             :                 size_t offset;
     911             : 
     912           0 :                 offset = slashes[i];
     913             : 
     914           0 :                 if (offset < min_offset) {
     915           0 :                         errno = ENOENT;
     916           0 :                         goto fail;
     917             :                 }
     918             : 
     919           0 :                 if (offset >= connectlen) {
     920           0 :                         in_share_offset = offset;
     921             :                 }
     922             : 
     923           0 :                 memcpy(converted+offset, insert, insertlen);
     924             : 
     925           0 :                 offset += insertlen;
     926           0 :                 memcpy(converted+offset, path + slashes[i],
     927           0 :                        pathlen - slashes[i]);
     928             : 
     929           0 :                 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
     930             : 
     931           0 :                 DEBUG(10, ("Trying[snapdirseverywhere] %s: %d (%s)\n",
     932             :                            converted,
     933             :                            ret, ret == 0 ? "ok" : strerror(errno)));
     934           0 :                 if (ret == 0) {
     935             :                         /* success */
     936           0 :                         if (snaproot_len != NULL) {
     937           0 :                                 *snaproot_len = in_share_offset + insertlen;
     938             :                         }
     939           0 :                         break;
     940             :                 }
     941           0 :                 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           0 :                         continue;
     948             :                 }
     949           0 :                 if (errno != ENOENT) {
     950             :                         /* Other problem than "not found" */
     951           0 :                         goto fail;
     952             :                 }
     953             :         }
     954             : 
     955           0 :         if (i >= 0) {
     956             :                 /*
     957             :                  * Found something
     958             :                  */
     959           0 :                 DEBUG(10, ("Found %s\n", converted));
     960           0 :                 result = converted;
     961           0 :                 converted = NULL;
     962             :         } else {
     963           0 :                 errno = ENOENT;
     964             :         }
     965           0 : fail:
     966           0 :         if (result == NULL) {
     967           0 :                 saved_errno = errno;
     968             :         }
     969           0 :         TALLOC_FREE(converted);
     970           0 :         TALLOC_FREE(insert);
     971           0 :         TALLOC_FREE(slashes);
     972           0 :         TALLOC_FREE(path);
     973           0 :         if (saved_errno != 0) {
     974           0 :                 errno = saved_errno;
     975             :         }
     976           0 :         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           0 : static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
     985             :                                   struct vfs_handle_struct *handle,
     986             :                                   const char *name, time_t timestamp)
     987             : {
     988           0 :         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           0 : 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           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    1001             :                                 return);
    1002             : 
    1003           0 :         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           0 :                 TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
    1015           0 :                                  .dsize = strlen(fname) };
    1016             :                 uint32_t shash;
    1017             : 
    1018           0 :                 shash = tdb_jenkins_hash(&key) & 0xFF000000;
    1019           0 :                 if (shash == 0) {
    1020           0 :                         shash = 1;
    1021             :                 }
    1022           0 :                 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           0 : static int shadow_copy2_stat(vfs_handle_struct *handle,
    1170             :                              struct smb_filename *smb_fname)
    1171             : {
    1172           0 :         struct shadow_copy2_private *priv = NULL;
    1173           0 :         time_t timestamp = 0;
    1174           0 :         char *stripped = NULL;
    1175           0 :         bool converted = false;
    1176           0 :         char *abspath = NULL;
    1177             :         char *tmp;
    1178           0 :         int ret = 0;
    1179             : 
    1180           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    1181             :                                 return -1);
    1182             : 
    1183           0 :         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           0 :         if (timestamp == 0) {
    1192           0 :                 TALLOC_FREE(stripped);
    1193           0 :                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
    1194           0 :                 if (ret != 0) {
    1195           0 :                         return ret;
    1196             :                 }
    1197           0 :                 if (!converted) {
    1198           0 :                         return 0;
    1199             :                 }
    1200             : 
    1201           0 :                 abspath = make_path_absolute(talloc_tos(),
    1202             :                                              priv,
    1203           0 :                                              smb_fname->base_name);
    1204           0 :                 if (abspath == NULL) {
    1205           0 :                         return -1;
    1206             :                 }
    1207             : 
    1208           0 :                 convert_sbuf(handle, abspath, &smb_fname->st);
    1209           0 :                 TALLOC_FREE(abspath);
    1210           0 :                 return 0;
    1211             :         }
    1212             : 
    1213           0 :         tmp = smb_fname->base_name;
    1214           0 :         smb_fname->base_name = shadow_copy2_convert(
    1215             :                 talloc_tos(), handle, stripped, timestamp);
    1216           0 :         TALLOC_FREE(stripped);
    1217             : 
    1218           0 :         if (smb_fname->base_name == NULL) {
    1219           0 :                 smb_fname->base_name = tmp;
    1220           0 :                 return -1;
    1221             :         }
    1222             : 
    1223           0 :         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
    1224           0 :         if (ret != 0) {
    1225           0 :                 goto out;
    1226             :         }
    1227             : 
    1228           0 :         abspath = make_path_absolute(talloc_tos(),
    1229             :                                      priv,
    1230           0 :                                      smb_fname->base_name);
    1231           0 :         if (abspath == NULL) {
    1232           0 :                 ret = -1;
    1233           0 :                 goto out;
    1234             :         }
    1235             : 
    1236           0 :         convert_sbuf(handle, abspath, &smb_fname->st);
    1237           0 :         TALLOC_FREE(abspath);
    1238             : 
    1239           0 : out:
    1240           0 :         TALLOC_FREE(smb_fname->base_name);
    1241           0 :         smb_fname->base_name = tmp;
    1242             : 
    1243           0 :         return ret;
    1244             : }
    1245             : 
    1246           0 : static int shadow_copy2_lstat(vfs_handle_struct *handle,
    1247             :                               struct smb_filename *smb_fname)
    1248             : {
    1249           0 :         struct shadow_copy2_private *priv = NULL;
    1250           0 :         time_t timestamp = 0;
    1251           0 :         char *stripped = NULL;
    1252           0 :         bool converted = false;
    1253           0 :         char *abspath = NULL;
    1254             :         char *tmp;
    1255           0 :         int ret = 0;
    1256             : 
    1257           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    1258             :                                 return -1);
    1259             : 
    1260           0 :         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           0 :         if (timestamp == 0) {
    1269           0 :                 TALLOC_FREE(stripped);
    1270           0 :                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
    1271           0 :                 if (ret != 0) {
    1272           0 :                         return ret;
    1273             :                 }
    1274           0 :                 if (!converted) {
    1275           0 :                         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           0 : static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
    1324             :                               SMB_STRUCT_STAT *sbuf)
    1325             : {
    1326           0 :         struct shadow_copy2_private *priv = NULL;
    1327           0 :         time_t timestamp = 0;
    1328           0 :         struct smb_filename *orig_smb_fname = NULL;
    1329             :         struct smb_filename vss_smb_fname;
    1330           0 :         struct smb_filename *orig_base_smb_fname = NULL;
    1331             :         struct smb_filename vss_base_smb_fname;
    1332           0 :         char *stripped = NULL;
    1333           0 :         char *abspath = NULL;
    1334           0 :         bool converted = false;
    1335             :         bool ok;
    1336             :         int ret;
    1337             : 
    1338           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    1339             :                                 return -1);
    1340             : 
    1341           0 :         ok = shadow_copy2_strip_snapshot_converted(talloc_tos(),
    1342             :                                                    handle,
    1343             :                                                    fsp->fsp_name,
    1344             :                                                    &timestamp,
    1345             :                                                    &stripped,
    1346             :                                                    &converted);
    1347           0 :         if (!ok) {
    1348           0 :                 return -1;
    1349             :         }
    1350             : 
    1351           0 :         if (timestamp == 0) {
    1352           0 :                 TALLOC_FREE(stripped);
    1353           0 :                 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
    1354           0 :                 if (ret != 0) {
    1355           0 :                         return ret;
    1356             :                 }
    1357           0 :                 if (!converted) {
    1358           0 :                         return 0;
    1359             :                 }
    1360             : 
    1361           0 :                 abspath = make_path_absolute(talloc_tos(),
    1362             :                                              priv,
    1363           0 :                                              fsp->fsp_name->base_name);
    1364           0 :                 if (abspath == NULL) {
    1365           0 :                         return -1;
    1366             :                 }
    1367             : 
    1368           0 :                 convert_sbuf(handle, abspath, sbuf);
    1369           0 :                 TALLOC_FREE(abspath);
    1370           0 :                 return 0;
    1371             :         }
    1372             : 
    1373           0 :         vss_smb_fname = *fsp->fsp_name;
    1374           0 :         vss_smb_fname.base_name = shadow_copy2_convert(talloc_tos(),
    1375             :                                                        handle,
    1376             :                                                        stripped,
    1377             :                                                        timestamp);
    1378           0 :         TALLOC_FREE(stripped);
    1379           0 :         if (vss_smb_fname.base_name == NULL) {
    1380           0 :                 return -1;
    1381             :         }
    1382             : 
    1383           0 :         orig_smb_fname = fsp->fsp_name;
    1384           0 :         fsp->fsp_name = &vss_smb_fname;
    1385             : 
    1386           0 :         if (fsp_is_alternate_stream(fsp)) {
    1387           0 :                 vss_base_smb_fname = *fsp->base_fsp->fsp_name;
    1388           0 :                 vss_base_smb_fname.base_name = vss_smb_fname.base_name;
    1389           0 :                 orig_base_smb_fname = fsp->base_fsp->fsp_name;
    1390           0 :                 fsp->base_fsp->fsp_name = &vss_base_smb_fname;
    1391             :         }
    1392             : 
    1393           0 :         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
    1394           0 :         if (ret != 0) {
    1395           0 :                 goto out;
    1396             :         }
    1397             : 
    1398           0 :         abspath = make_path_absolute(talloc_tos(),
    1399             :                                      priv,
    1400           0 :                                      fsp->fsp_name->base_name);
    1401           0 :         if (abspath == NULL) {
    1402           0 :                 ret = -1;
    1403           0 :                 goto out;
    1404             :         }
    1405             : 
    1406           0 :         convert_sbuf(handle, abspath, sbuf);
    1407           0 :         TALLOC_FREE(abspath);
    1408             : 
    1409           0 : out:
    1410           0 :         fsp->fsp_name = orig_smb_fname;
    1411           0 :         if (fsp_is_alternate_stream(fsp)) {
    1412           0 :                 fsp->base_fsp->fsp_name = orig_base_smb_fname;
    1413             :         }
    1414             : 
    1415           0 :         return ret;
    1416             : }
    1417             : 
    1418           0 : 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           0 :         struct shadow_copy2_private *priv = NULL;
    1426           0 :         struct smb_filename *smb_fname = NULL;
    1427           0 :         time_t timestamp = 0;
    1428           0 :         char *stripped = NULL;
    1429           0 :         char *abspath = NULL;
    1430           0 :         bool converted = false;
    1431             :         int ret;
    1432             :         bool ok;
    1433             : 
    1434           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    1435             :                                 return -1);
    1436             : 
    1437           0 :         smb_fname = full_path_from_dirfsp_atname(talloc_tos(),
    1438             :                                                  dirfsp,
    1439             :                                                  smb_fname_in);
    1440           0 :         if (smb_fname == NULL) {
    1441           0 :                 errno = ENOMEM;
    1442           0 :                 return -1;
    1443             :         }
    1444             : 
    1445           0 :         ok = shadow_copy2_strip_snapshot_converted(talloc_tos(),
    1446             :                                                    handle,
    1447             :                                                    smb_fname,
    1448             :                                                    &timestamp,
    1449             :                                                    &stripped,
    1450             :                                                    &converted);
    1451           0 :         if (!ok) {
    1452           0 :                 return -1;
    1453             :         }
    1454           0 :         if (timestamp == 0) {
    1455           0 :                 TALLOC_FREE(stripped);
    1456           0 :                 ret = SMB_VFS_NEXT_FSTATAT(
    1457             :                         handle, dirfsp, smb_fname_in, sbuf, flags);
    1458           0 :                 if (ret != 0) {
    1459           0 :                         return ret;
    1460             :                 }
    1461           0 :                 if (!converted) {
    1462           0 :                         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           0 :         smb_fname->base_name = shadow_copy2_convert(
    1478             :                 smb_fname, handle, stripped, timestamp);
    1479           0 :         TALLOC_FREE(stripped);
    1480           0 :         if (smb_fname->base_name == NULL) {
    1481           0 :                 TALLOC_FREE(smb_fname);
    1482           0 :                 errno = ENOMEM;
    1483           0 :                 return -1;
    1484             :         }
    1485             : 
    1486           0 :         ret = SMB_VFS_NEXT_FSTATAT(handle,
    1487             :                                    dirfsp,
    1488             :                                    smb_fname,
    1489             :                                    sbuf,
    1490             :                                    flags);
    1491           0 :         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           0 :         abspath = make_path_absolute(
    1499           0 :                 talloc_tos(), priv, smb_fname->base_name);
    1500           0 :         if (abspath == NULL) {
    1501           0 :                 TALLOC_FREE(smb_fname);
    1502           0 :                 errno = ENOMEM;
    1503           0 :                 return -1;
    1504             :         }
    1505             : 
    1506           0 :         convert_sbuf(handle, abspath, sbuf);
    1507           0 :         TALLOC_FREE(abspath);
    1508             : 
    1509           0 :         TALLOC_FREE(smb_fname);
    1510             : 
    1511           0 :         return 0;
    1512             : }
    1513             : 
    1514           0 : 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           0 :         struct smb_filename *result = NULL;
    1521             : 
    1522           0 :         if (fsp->base_fsp != NULL) {
    1523           0 :                 struct smb_filename *base_fname = fsp->base_fsp->fsp_name;
    1524             : 
    1525           0 :                 if (smb_fname_in->base_name[0] == '/') {
    1526             :                         /*
    1527             :                          * Special-case stream names from streams_depot
    1528             :                          */
    1529           0 :                         result = cp_smb_filename(mem_ctx, smb_fname_in);
    1530             :                 } else {
    1531             : 
    1532           0 :                         SMB_ASSERT(is_named_stream(smb_fname_in));
    1533             : 
    1534           0 :                         result = synthetic_smb_fname(mem_ctx,
    1535           0 :                                                      base_fname->base_name,
    1536           0 :                                                      smb_fname_in->stream_name,
    1537             :                                                      &smb_fname_in->st,
    1538           0 :                                                      smb_fname_in->twrp,
    1539           0 :                                                      smb_fname_in->flags);
    1540             :                 }
    1541             :         } else {
    1542           0 :                 result = full_path_from_dirfsp_atname(
    1543             :                         mem_ctx, dirfsp, smb_fname_in);
    1544             :         }
    1545             : 
    1546           0 :         return result;
    1547             : }
    1548             : 
    1549           0 : 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           0 :         struct vfs_open_how how = *_how;
    1556           0 :         struct smb_filename *smb_fname = NULL;
    1557           0 :         time_t timestamp = 0;
    1558           0 :         char *stripped = NULL;
    1559           0 :         int saved_errno = 0;
    1560             :         int ret;
    1561             :         bool ok;
    1562             : 
    1563           0 :         if (how.resolve != 0) {
    1564           0 :                 errno = ENOSYS;
    1565           0 :                 return -1;
    1566             :         }
    1567             : 
    1568           0 :         smb_fname = shadow_copy2_openat_name(
    1569             :                 talloc_tos(), dirfsp, fsp, smb_fname_in);
    1570           0 :         if (smb_fname == NULL) {
    1571           0 :                 errno = ENOMEM;
    1572           0 :                 return -1;
    1573             :         }
    1574             : 
    1575           0 :         ok = shadow_copy2_strip_snapshot(talloc_tos(),
    1576             :                                          handle,
    1577             :                                          smb_fname,
    1578             :                                          &timestamp,
    1579             :                                          &stripped);
    1580           0 :         if (!ok) {
    1581           0 :                 return -1;
    1582             :         }
    1583           0 :         if (timestamp == 0) {
    1584           0 :                 return SMB_VFS_NEXT_OPENAT(handle,
    1585             :                                            dirfsp,
    1586             :                                            smb_fname_in,
    1587             :                                            fsp,
    1588             :                                            &how);
    1589             :         }
    1590             : 
    1591           0 :         smb_fname->base_name = shadow_copy2_convert(smb_fname,
    1592             :                                                handle,
    1593             :                                                stripped,
    1594             :                                                timestamp);
    1595           0 :         if (smb_fname->base_name == NULL) {
    1596           0 :                 int err = errno;
    1597           0 :                 TALLOC_FREE(stripped);
    1598           0 :                 TALLOC_FREE(smb_fname);
    1599           0 :                 errno = err;
    1600           0 :                 return -1;
    1601             :         }
    1602           0 :         TALLOC_FREE(stripped);
    1603             : 
    1604           0 :         ret = SMB_VFS_NEXT_OPENAT(handle,
    1605             :                                   dirfsp,
    1606             :                                   smb_fname,
    1607             :                                   fsp,
    1608             :                                   &how);
    1609           0 :         if (ret == -1) {
    1610           0 :                 saved_errno = errno;
    1611             :         }
    1612             : 
    1613           0 :         TALLOC_FREE(smb_fname);
    1614             : 
    1615           0 :         if (saved_errno != 0) {
    1616           0 :                 errno = saved_errno;
    1617             :         }
    1618           0 :         return ret;
    1619             : }
    1620             : 
    1621           0 : static int shadow_copy2_unlinkat(vfs_handle_struct *handle,
    1622             :                         struct files_struct *dirfsp,
    1623             :                         const struct smb_filename *smb_fname,
    1624             :                         int flags)
    1625             : {
    1626           0 :         time_t timestamp = 0;
    1627             : 
    1628           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
    1629             :                                          smb_fname,
    1630             :                                          &timestamp, NULL)) {
    1631           0 :                 return -1;
    1632             :         }
    1633           0 :         if (timestamp != 0) {
    1634           0 :                 errno = EROFS;
    1635           0 :                 return -1;
    1636             :         }
    1637           0 :         return SMB_VFS_NEXT_UNLINKAT(handle,
    1638             :                         dirfsp,
    1639             :                         smb_fname,
    1640             :                         flags);
    1641             : }
    1642             : 
    1643           0 : static int shadow_copy2_fchmod(vfs_handle_struct *handle,
    1644             :                        struct files_struct *fsp,
    1645             :                        mode_t mode)
    1646             : {
    1647           0 :         time_t timestamp = 0;
    1648           0 :         const struct smb_filename *smb_fname = NULL;
    1649             : 
    1650           0 :         smb_fname = fsp->fsp_name;
    1651           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(),
    1652             :                                         handle,
    1653             :                                         smb_fname,
    1654             :                                         &timestamp,
    1655             :                                         NULL)) {
    1656           0 :                 return -1;
    1657             :         }
    1658           0 :         if (timestamp != 0) {
    1659           0 :                 errno = EROFS;
    1660           0 :                 return -1;
    1661             :         }
    1662           0 :         return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
    1663             : }
    1664             : 
    1665           0 : static void store_cwd_data(vfs_handle_struct *handle,
    1666             :                                 const char *connectpath)
    1667             : {
    1668           0 :         struct shadow_copy2_private *priv = NULL;
    1669           0 :         struct smb_filename *cwd_fname = NULL;
    1670             : 
    1671           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    1672             :                                 return);
    1673             : 
    1674           0 :         TALLOC_FREE(priv->shadow_cwd);
    1675           0 :         cwd_fname = SMB_VFS_NEXT_GETWD(handle, talloc_tos());
    1676           0 :         if (cwd_fname == NULL) {
    1677           0 :                 smb_panic("getwd failed\n");
    1678             :         }
    1679           0 :         DBG_DEBUG("shadow cwd = %s\n", cwd_fname->base_name);
    1680           0 :         priv->shadow_cwd = talloc_strdup(priv, cwd_fname->base_name);
    1681           0 :         TALLOC_FREE(cwd_fname);
    1682           0 :         if (priv->shadow_cwd == NULL) {
    1683           0 :                 smb_panic("talloc failed\n");
    1684             :         }
    1685           0 :         TALLOC_FREE(priv->shadow_connectpath);
    1686           0 :         if (connectpath) {
    1687           0 :                 DBG_DEBUG("shadow connectpath = %s\n", connectpath);
    1688           0 :                 priv->shadow_connectpath = talloc_strdup(priv, connectpath);
    1689           0 :                 if (priv->shadow_connectpath == NULL) {
    1690           0 :                         smb_panic("talloc failed\n");
    1691             :                 }
    1692             :         }
    1693             : }
    1694             : 
    1695           0 : static int shadow_copy2_chdir(vfs_handle_struct *handle,
    1696             :                                const struct smb_filename *smb_fname)
    1697             : {
    1698           0 :         time_t timestamp = 0;
    1699           0 :         char *stripped = NULL;
    1700           0 :         char *snappath = NULL;
    1701           0 :         int ret = -1;
    1702           0 :         int saved_errno = 0;
    1703           0 :         char *conv = NULL;
    1704           0 :         size_t rootpath_len = 0;
    1705           0 :         struct smb_filename *conv_smb_fname = NULL;
    1706             : 
    1707           0 :         if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
    1708             :                                         handle,
    1709             :                                         smb_fname,
    1710             :                                         &timestamp,
    1711             :                                         &stripped,
    1712             :                                         &snappath,
    1713             :                                         NULL)) {
    1714           0 :                 return -1;
    1715             :         }
    1716           0 :         if (stripped != NULL) {
    1717           0 :                 conv = shadow_copy2_do_convert(talloc_tos(),
    1718             :                                                 handle,
    1719             :                                                 stripped,
    1720             :                                                 timestamp,
    1721             :                                                 &rootpath_len);
    1722           0 :                 TALLOC_FREE(stripped);
    1723           0 :                 if (conv == NULL) {
    1724           0 :                         return -1;
    1725             :                 }
    1726           0 :                 conv_smb_fname = synthetic_smb_fname(talloc_tos(),
    1727             :                                         conv,
    1728             :                                         NULL,
    1729             :                                         NULL,
    1730             :                                         0,
    1731           0 :                                         smb_fname->flags);
    1732             :         } else {
    1733           0 :                 conv_smb_fname = cp_smb_filename(talloc_tos(), smb_fname);
    1734             :         }
    1735             : 
    1736           0 :         if (conv_smb_fname == NULL) {
    1737           0 :                 TALLOC_FREE(conv);
    1738           0 :                 errno = ENOMEM;
    1739           0 :                 return -1;
    1740             :         }
    1741             : 
    1742           0 :         ret = SMB_VFS_NEXT_CHDIR(handle, conv_smb_fname);
    1743           0 :         if (ret == -1) {
    1744           0 :                 saved_errno = errno;
    1745             :         }
    1746             : 
    1747           0 :         if (ret == 0) {
    1748           0 :                 if (conv != NULL && rootpath_len != 0) {
    1749           0 :                         conv[rootpath_len] = '\0';
    1750           0 :                 } else if (snappath != 0) {
    1751           0 :                         TALLOC_FREE(conv);
    1752           0 :                         conv = snappath;
    1753             :                 }
    1754           0 :                 store_cwd_data(handle, conv);
    1755             :         }
    1756             : 
    1757           0 :         TALLOC_FREE(stripped);
    1758           0 :         TALLOC_FREE(conv);
    1759           0 :         TALLOC_FREE(conv_smb_fname);
    1760             : 
    1761           0 :         if (saved_errno != 0) {
    1762           0 :                 errno = saved_errno;
    1763             :         }
    1764           0 :         return ret;
    1765             : }
    1766             : 
    1767           0 : static int shadow_copy2_fntimes(vfs_handle_struct *handle,
    1768             :                                 files_struct *fsp,
    1769             :                                 struct smb_file_time *ft)
    1770             : {
    1771           0 :         time_t timestamp = 0;
    1772             : 
    1773           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(),
    1774             :                                          handle,
    1775             :                                          fsp->fsp_name,
    1776             :                                          &timestamp,
    1777             :                                          NULL)) {
    1778           0 :                 return -1;
    1779             :         }
    1780           0 :         if (timestamp != 0) {
    1781           0 :                 errno = EROFS;
    1782           0 :                 return -1;
    1783             :         }
    1784           0 :         return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
    1785             : }
    1786             : 
    1787           0 : static int shadow_copy2_readlinkat(vfs_handle_struct *handle,
    1788             :                                 const struct files_struct *dirfsp,
    1789             :                                 const struct smb_filename *smb_fname,
    1790             :                                 char *buf,
    1791             :                                 size_t bufsiz)
    1792             : {
    1793           0 :         time_t timestamp = 0;
    1794           0 :         char *stripped = NULL;
    1795           0 :         int saved_errno = 0;
    1796             :         int ret;
    1797           0 :         struct smb_filename *full_fname = NULL;
    1798           0 :         struct smb_filename *conv = NULL;
    1799             : 
    1800           0 :         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
    1801             :                                                   dirfsp,
    1802             :                                                   smb_fname);
    1803           0 :         if (full_fname == NULL) {
    1804           0 :                 errno = ENOMEM;
    1805           0 :                 return -1;
    1806             :         }
    1807             : 
    1808           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(),
    1809             :                                         handle,
    1810             :                                         full_fname,
    1811             :                                         &timestamp,
    1812             :                                         &stripped)) {
    1813           0 :                 TALLOC_FREE(full_fname);
    1814           0 :                 return -1;
    1815             :         }
    1816             : 
    1817           0 :         if (timestamp == 0) {
    1818           0 :                 TALLOC_FREE(full_fname);
    1819           0 :                 TALLOC_FREE(stripped);
    1820           0 :                 return SMB_VFS_NEXT_READLINKAT(handle,
    1821             :                                 dirfsp,
    1822             :                                 smb_fname,
    1823             :                                 buf,
    1824             :                                 bufsiz);
    1825             :         }
    1826           0 :         conv = cp_smb_filename(talloc_tos(), full_fname);
    1827           0 :         if (conv == NULL) {
    1828           0 :                 TALLOC_FREE(full_fname);
    1829           0 :                 TALLOC_FREE(stripped);
    1830           0 :                 errno = ENOMEM;
    1831           0 :                 return -1;
    1832             :         }
    1833           0 :         TALLOC_FREE(full_fname);
    1834           0 :         conv->base_name = shadow_copy2_convert(
    1835             :                 conv, handle, stripped, timestamp);
    1836           0 :         TALLOC_FREE(stripped);
    1837           0 :         if (conv->base_name == NULL) {
    1838           0 :                 return -1;
    1839             :         }
    1840           0 :         ret = SMB_VFS_NEXT_READLINKAT(handle,
    1841             :                                 handle->conn->cwd_fsp,
    1842             :                                 conv,
    1843             :                                 buf,
    1844             :                                 bufsiz);
    1845           0 :         if (ret == -1) {
    1846           0 :                 saved_errno = errno;
    1847             :         }
    1848           0 :         TALLOC_FREE(conv);
    1849           0 :         if (saved_errno != 0) {
    1850           0 :                 errno = saved_errno;
    1851             :         }
    1852           0 :         return ret;
    1853             : }
    1854             : 
    1855           0 : static int shadow_copy2_mknodat(vfs_handle_struct *handle,
    1856             :                         files_struct *dirfsp,
    1857             :                         const struct smb_filename *smb_fname,
    1858             :                         mode_t mode,
    1859             :                         SMB_DEV_T dev)
    1860             : {
    1861           0 :         time_t timestamp = 0;
    1862             : 
    1863           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
    1864             :                                          smb_fname,
    1865             :                                          &timestamp, NULL)) {
    1866           0 :                 return -1;
    1867             :         }
    1868           0 :         if (timestamp != 0) {
    1869           0 :                 errno = EROFS;
    1870           0 :                 return -1;
    1871             :         }
    1872           0 :         return SMB_VFS_NEXT_MKNODAT(handle,
    1873             :                         dirfsp,
    1874             :                         smb_fname,
    1875             :                         mode,
    1876             :                         dev);
    1877             : }
    1878             : 
    1879           0 : static struct smb_filename *shadow_copy2_realpath(vfs_handle_struct *handle,
    1880             :                                 TALLOC_CTX *ctx,
    1881             :                                 const struct smb_filename *smb_fname)
    1882             : {
    1883           0 :         time_t timestamp = 0;
    1884           0 :         char *stripped = NULL;
    1885           0 :         struct smb_filename *result_fname = NULL;
    1886           0 :         struct smb_filename *conv_fname = NULL;
    1887           0 :         int saved_errno = 0;
    1888             : 
    1889           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
    1890             :                                 smb_fname,
    1891             :                                 &timestamp, &stripped)) {
    1892           0 :                 goto done;
    1893             :         }
    1894           0 :         if (timestamp == 0) {
    1895           0 :                 return SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
    1896             :         }
    1897             : 
    1898           0 :         conv_fname = cp_smb_filename(talloc_tos(), smb_fname);
    1899           0 :         if (conv_fname == NULL) {
    1900           0 :                 goto done;
    1901             :         }
    1902           0 :         conv_fname->base_name = shadow_copy2_convert(
    1903             :                 conv_fname, handle, stripped, timestamp);
    1904           0 :         if (conv_fname->base_name == NULL) {
    1905           0 :                 goto done;
    1906             :         }
    1907             : 
    1908           0 :         result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, conv_fname);
    1909             : 
    1910           0 : done:
    1911           0 :         if (result_fname == NULL) {
    1912           0 :                 saved_errno = errno;
    1913             :         }
    1914           0 :         TALLOC_FREE(conv_fname);
    1915           0 :         TALLOC_FREE(stripped);
    1916           0 :         if (saved_errno != 0) {
    1917           0 :                 errno = saved_errno;
    1918             :         }
    1919           0 :         return result_fname;
    1920             : }
    1921             : 
    1922             : /**
    1923             :  * Check whether a given directory contains a
    1924             :  * snapshot directory as direct subdirectory.
    1925             :  * If yes, return the path of the snapshot-subdir,
    1926             :  * otherwise return NULL.
    1927             :  */
    1928           0 : static char *have_snapdir(struct vfs_handle_struct *handle,
    1929             :                           TALLOC_CTX *mem_ctx,
    1930             :                           const char *path)
    1931             : {
    1932             :         struct smb_filename smb_fname;
    1933             :         int ret;
    1934             :         struct shadow_copy2_private *priv;
    1935             : 
    1936           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    1937             :                                 return NULL);
    1938             : 
    1939           0 :         smb_fname = (struct smb_filename) {
    1940           0 :                 .base_name = talloc_asprintf(
    1941           0 :                         mem_ctx, "%s/%s", path, priv->config->snapdir),
    1942             :         };
    1943           0 :         if (smb_fname.base_name == NULL) {
    1944           0 :                 return NULL;
    1945             :         }
    1946             : 
    1947           0 :         ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
    1948           0 :         if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
    1949           0 :                 return smb_fname.base_name;
    1950             :         }
    1951           0 :         TALLOC_FREE(smb_fname.base_name);
    1952           0 :         return NULL;
    1953             : }
    1954             : 
    1955             : /**
    1956             :  * Find the snapshot directory (if any) for the given
    1957             :  * filename (which is relative to the share).
    1958             :  */
    1959           0 : static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
    1960             :                                              struct vfs_handle_struct *handle,
    1961             :                                              struct smb_filename *smb_fname)
    1962             : {
    1963             :         char *path, *p;
    1964             :         const char *snapdir;
    1965             :         struct shadow_copy2_config *config;
    1966             :         struct shadow_copy2_private *priv;
    1967             : 
    1968           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    1969             :                                 return NULL);
    1970             : 
    1971           0 :         config = priv->config;
    1972             : 
    1973             :         /*
    1974             :          * If the non-snapdisrseverywhere mode, we should not search!
    1975             :          */
    1976           0 :         if (!config->snapdirseverywhere) {
    1977           0 :                 return config->snapshot_basepath;
    1978             :         }
    1979             : 
    1980           0 :         path = talloc_asprintf(mem_ctx, "%s/%s",
    1981           0 :                                handle->conn->connectpath,
    1982             :                                smb_fname->base_name);
    1983           0 :         if (path == NULL) {
    1984           0 :                 return NULL;
    1985             :         }
    1986             : 
    1987           0 :         snapdir = have_snapdir(handle, talloc_tos(), path);
    1988           0 :         if (snapdir != NULL) {
    1989           0 :                 TALLOC_FREE(path);
    1990           0 :                 return snapdir;
    1991             :         }
    1992             : 
    1993           0 :         while ((p = strrchr(path, '/')) && (p > path)) {
    1994             : 
    1995           0 :                 p[0] = '\0';
    1996             : 
    1997           0 :                 snapdir = have_snapdir(handle, talloc_tos(), path);
    1998           0 :                 if (snapdir != NULL) {
    1999           0 :                         TALLOC_FREE(path);
    2000           0 :                         return snapdir;
    2001             :                 }
    2002             :         }
    2003           0 :         TALLOC_FREE(path);
    2004           0 :         return NULL;
    2005             : }
    2006             : 
    2007           0 : static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
    2008             :                                          const char *name,
    2009             :                                          char *gmt, size_t gmt_len)
    2010             : {
    2011           0 :         struct tm timestamp = { .tm_sec = 0, };
    2012             :         time_t timestamp_t;
    2013             :         unsigned long int timestamp_long;
    2014             :         const char *fmt;
    2015             :         struct shadow_copy2_config *config;
    2016             :         struct shadow_copy2_private *priv;
    2017           0 :         char *tmpstr = NULL;
    2018           0 :         char *tmp = NULL;
    2019           0 :         bool converted = false;
    2020           0 :         int ret = -1;
    2021             : 
    2022           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    2023             :                                 return NULL);
    2024             : 
    2025           0 :         config = priv->config;
    2026             : 
    2027           0 :         fmt = config->gmt_format;
    2028             : 
    2029             :         /*
    2030             :          * If regex is provided, then we will have to parse the
    2031             :          * filename which will contain both the prefix and the time format.
    2032             :          * e.g. <prefix><delimiter><time_format>
    2033             :          */
    2034           0 :         if (priv->snaps->regex != NULL) {
    2035           0 :                 tmpstr = talloc_strdup(talloc_tos(), name);
    2036             :                 /* point "name" to the time format */
    2037           0 :                 name = strstr(name, priv->config->delimiter);
    2038           0 :                 if (name == NULL) {
    2039           0 :                         goto done;
    2040             :                 }
    2041             :                 /* Extract the prefix */
    2042           0 :                 tmp = strstr(tmpstr, priv->config->delimiter);
    2043           0 :                 if (tmp == NULL) {
    2044           0 :                         goto done;
    2045             :                 }
    2046           0 :                 *tmp = '\0';
    2047             : 
    2048             :                 /* Parse regex */
    2049           0 :                 ret = regexec(priv->snaps->regex, tmpstr, 0, NULL, 0);
    2050           0 :                 if (ret) {
    2051           0 :                         DBG_DEBUG("shadow_copy2_snapshot_to_gmt: "
    2052             :                                   "no regex match for %s\n", tmpstr);
    2053           0 :                         goto done;
    2054             :                 }
    2055             :         }
    2056             : 
    2057           0 :         if (config->use_sscanf) {
    2058           0 :                 if (sscanf(name, fmt, &timestamp_long) != 1) {
    2059           0 :                         DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
    2060             :                                    "no sscanf match %s: %s\n",
    2061             :                                    fmt, name));
    2062           0 :                         goto done;
    2063             :                 }
    2064           0 :                 timestamp_t = timestamp_long;
    2065           0 :                 gmtime_r(&timestamp_t, &timestamp);
    2066             :         } else {
    2067           0 :                 if (strptime(name, fmt, &timestamp) == NULL) {
    2068           0 :                         DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
    2069             :                                    "no match %s: %s\n",
    2070             :                                    fmt, name));
    2071           0 :                         goto done;
    2072             :                 }
    2073           0 :                 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
    2074             :                            fmt, name));
    2075             : 
    2076           0 :                 if (config->use_localtime) {
    2077           0 :                         timestamp.tm_isdst = -1;
    2078           0 :                         timestamp_t = mktime(&timestamp);
    2079           0 :                         gmtime_r(&timestamp_t, &timestamp);
    2080             :                 }
    2081             :         }
    2082             : 
    2083           0 :         strftime(gmt, gmt_len, GMT_FORMAT, &timestamp);
    2084           0 :         converted = true;
    2085             : 
    2086           0 : done:
    2087           0 :         TALLOC_FREE(tmpstr);
    2088           0 :         return converted;
    2089             : }
    2090             : 
    2091           0 : static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
    2092             : {
    2093           0 :         return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
    2094             : }
    2095             : 
    2096           0 : static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
    2097             : {
    2098           0 :         return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
    2099             : }
    2100             : 
    2101             : /*
    2102             :   sort the shadow copy data in ascending or descending order
    2103             :  */
    2104           0 : static void shadow_copy2_sort_data(vfs_handle_struct *handle,
    2105             :                                    struct shadow_copy_data *shadow_copy2_data)
    2106             : {
    2107             :         int (*cmpfunc)(const void *, const void *);
    2108             :         const char *sort;
    2109             :         struct shadow_copy2_private *priv;
    2110             : 
    2111           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    2112             :                                 return);
    2113             : 
    2114           0 :         sort = priv->config->sort_order;
    2115           0 :         if (sort == NULL) {
    2116           0 :                 return;
    2117             :         }
    2118             : 
    2119           0 :         if (strcmp(sort, "asc") == 0) {
    2120           0 :                 cmpfunc = shadow_copy2_label_cmp_asc;
    2121           0 :         } else if (strcmp(sort, "desc") == 0) {
    2122           0 :                 cmpfunc = shadow_copy2_label_cmp_desc;
    2123             :         } else {
    2124           0 :                 return;
    2125             :         }
    2126             : 
    2127           0 :         if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
    2128           0 :             shadow_copy2_data->labels)
    2129             :         {
    2130           0 :                 TYPESAFE_QSORT(shadow_copy2_data->labels,
    2131             :                                shadow_copy2_data->num_volumes,
    2132             :                                cmpfunc);
    2133             :         }
    2134             : }
    2135             : 
    2136           0 : static int shadow_copy2_get_shadow_copy_data(
    2137             :         vfs_handle_struct *handle, files_struct *fsp,
    2138             :         struct shadow_copy_data *shadow_copy2_data,
    2139             :         bool labels)
    2140             : {
    2141           0 :         DIR *p = NULL;
    2142             :         const char *snapdir;
    2143           0 :         struct smb_filename *snapdir_smb_fname = NULL;
    2144           0 :         struct files_struct *dirfsp = NULL;
    2145           0 :         struct files_struct *fspcwd = NULL;
    2146             :         struct dirent *d;
    2147           0 :         TALLOC_CTX *tmp_ctx = talloc_stackframe();
    2148           0 :         struct shadow_copy2_private *priv = NULL;
    2149           0 :         struct shadow_copy2_snapentry *tmpentry = NULL;
    2150           0 :         bool get_snaplist = false;
    2151           0 :         struct vfs_open_how how = {
    2152             :                 .flags = O_RDONLY, .mode = 0,
    2153             :         };
    2154             :         int fd;
    2155           0 :         int ret = -1;
    2156             :         NTSTATUS status;
    2157           0 :         int saved_errno = 0;
    2158             : 
    2159           0 :         snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
    2160           0 :         if (snapdir == NULL) {
    2161           0 :                 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
    2162             :                          handle->conn->connectpath));
    2163           0 :                 errno = EINVAL;
    2164           0 :                 goto done;
    2165             :         }
    2166             : 
    2167           0 :         snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
    2168             :                                         snapdir,
    2169             :                                         NULL,
    2170             :                                         NULL,
    2171             :                                         0,
    2172           0 :                                         fsp->fsp_name->flags);
    2173           0 :         if (snapdir_smb_fname == NULL) {
    2174           0 :                 errno = ENOMEM;
    2175           0 :                 goto done;
    2176             :         }
    2177             : 
    2178           0 :         status = create_internal_dirfsp(handle->conn,
    2179             :                                         snapdir_smb_fname,
    2180             :                                         &dirfsp);
    2181           0 :         if (!NT_STATUS_IS_OK(status)) {
    2182           0 :                 DBG_WARNING("create_internal_dir_fsp() failed for '%s'"
    2183             :                             " - %s\n", snapdir, nt_errstr(status));
    2184           0 :                 errno = ENOSYS;
    2185           0 :                 goto done;
    2186             :         }
    2187             : 
    2188           0 :         status = vfs_at_fspcwd(talloc_tos(), handle->conn, &fspcwd);
    2189           0 :         if (!NT_STATUS_IS_OK(status)) {
    2190           0 :                 errno = ENOMEM;
    2191           0 :                 goto done;
    2192             :         }
    2193             : 
    2194             : #ifdef O_DIRECTORY
    2195           0 :         how.flags |= O_DIRECTORY;
    2196             : #endif
    2197             : 
    2198           0 :         fd = SMB_VFS_NEXT_OPENAT(handle,
    2199             :                                  fspcwd,
    2200             :                                  snapdir_smb_fname,
    2201             :                                  dirfsp,
    2202             :                                  &how);
    2203           0 :         if (fd == -1) {
    2204           0 :                 DBG_WARNING("SMB_VFS_NEXT_OPEN failed for '%s'"
    2205             :                             " - %s\n", snapdir, strerror(errno));
    2206           0 :                 errno = ENOSYS;
    2207           0 :                 goto done;
    2208             :         }
    2209           0 :         fsp_set_fd(dirfsp, fd);
    2210             : 
    2211             :         /* Now we have the handle, check access here. */
    2212           0 :         status = smbd_check_access_rights_fsp(fspcwd,
    2213             :                                         dirfsp,
    2214             :                                         false,
    2215             :                                         SEC_DIR_LIST);
    2216           0 :         if (!NT_STATUS_IS_OK(status)) {
    2217           0 :                 DBG_ERR("user does not have list permission "
    2218             :                         "on snapdir %s\n",
    2219             :                         fsp_str_dbg(dirfsp));
    2220           0 :                 errno = EACCES;
    2221           0 :                 goto done;
    2222             :         }
    2223             : 
    2224           0 :         p = SMB_VFS_NEXT_FDOPENDIR(handle, dirfsp, NULL, 0);
    2225           0 :         if (!p) {
    2226           0 :                 DBG_NOTICE("shadow_copy2: SMB_VFS_NEXT_FDOPENDIR() failed for '%s'"
    2227             :                            " - %s\n", snapdir, strerror(errno));
    2228           0 :                 errno = ENOSYS;
    2229           0 :                 goto done;
    2230             :         }
    2231             : 
    2232           0 :         if (shadow_copy2_data != NULL) {
    2233           0 :                 shadow_copy2_data->num_volumes = 0;
    2234           0 :                 shadow_copy2_data->labels      = NULL;
    2235             :         }
    2236             : 
    2237           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    2238             :                                 goto done);
    2239             : 
    2240             :         /*
    2241             :          * Normally this function is called twice once with labels = false and
    2242             :          * then with labels = true. When labels is false it will return the
    2243             :          * number of volumes so that the caller can allocate memory for that
    2244             :          * many labels. Therefore to eliminate snaplist both the times it is
    2245             :          * good to check if labels is set or not.
    2246             :          *
    2247             :          * shadow_copy2_data is NULL when we only want to update the list and
    2248             :          * don't want any labels.
    2249             :          */
    2250           0 :         if ((priv->snaps->regex != NULL) && (labels || shadow_copy2_data == NULL)) {
    2251           0 :                 get_snaplist = true;
    2252             :                 /* Reset the global snaplist */
    2253           0 :                 shadow_copy2_delete_snaplist(priv);
    2254             : 
    2255             :                 /* Set the current time as snaplist update time */
    2256           0 :                 time(&(priv->snaps->fetch_time));
    2257             :         }
    2258             : 
    2259           0 :         while ((d = SMB_VFS_NEXT_READDIR(handle, dirfsp, p, NULL))) {
    2260             :                 char snapshot[GMT_NAME_LEN+1];
    2261             :                 SHADOW_COPY_LABEL *tlabels;
    2262             : 
    2263             :                 /*
    2264             :                  * ignore names not of the right form in the snapshot
    2265             :                  * directory
    2266             :                  */
    2267           0 :                 if (!shadow_copy2_snapshot_to_gmt(
    2268           0 :                             handle, d->d_name,
    2269             :                             snapshot, sizeof(snapshot))) {
    2270             : 
    2271           0 :                         DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
    2272             :                                   "ignoring %s\n", d->d_name));
    2273           0 :                         continue;
    2274             :                 }
    2275           0 :                 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
    2276             :                          d->d_name, snapshot));
    2277             : 
    2278           0 :                 if (get_snaplist) {
    2279             :                         /*
    2280             :                          * Create a snap entry for each successful
    2281             :                          * pattern match.
    2282             :                          */
    2283           0 :                         tmpentry = shadow_copy2_create_snapentry(priv);
    2284           0 :                         if (tmpentry == NULL) {
    2285           0 :                                 DBG_ERR("talloc_zero() failed\n");
    2286           0 :                                 goto done;
    2287             :                         }
    2288           0 :                         tmpentry->snapname = talloc_strdup(tmpentry, d->d_name);
    2289           0 :                         tmpentry->time_fmt = talloc_strdup(tmpentry, snapshot);
    2290             :                 }
    2291             : 
    2292           0 :                 if (shadow_copy2_data == NULL) {
    2293           0 :                         continue;
    2294             :                 }
    2295             : 
    2296           0 :                 if (!labels) {
    2297             :                         /* the caller doesn't want the labels */
    2298           0 :                         shadow_copy2_data->num_volumes++;
    2299           0 :                         continue;
    2300             :                 }
    2301             : 
    2302           0 :                 tlabels = talloc_realloc(shadow_copy2_data,
    2303             :                                          shadow_copy2_data->labels,
    2304             :                                          SHADOW_COPY_LABEL,
    2305             :                                          shadow_copy2_data->num_volumes+1);
    2306           0 :                 if (tlabels == NULL) {
    2307           0 :                         DEBUG(0,("shadow_copy2: out of memory\n"));
    2308           0 :                         goto done;
    2309             :                 }
    2310             : 
    2311           0 :                 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
    2312             :                         sizeof(*tlabels));
    2313             : 
    2314           0 :                 shadow_copy2_data->num_volumes++;
    2315           0 :                 shadow_copy2_data->labels = tlabels;
    2316             :         }
    2317             : 
    2318           0 :         shadow_copy2_sort_data(handle, shadow_copy2_data);
    2319           0 :         ret = 0;
    2320             : 
    2321           0 : done:
    2322           0 :         if (ret != 0) {
    2323           0 :                 saved_errno = errno;
    2324             :         }
    2325           0 :         TALLOC_FREE(fspcwd );
    2326           0 :         if (p != NULL) {
    2327           0 :                 SMB_VFS_NEXT_CLOSEDIR(handle, p);
    2328           0 :                 p = NULL;
    2329           0 :                 if (dirfsp != NULL) {
    2330             :                         /*
    2331             :                          * VFS_CLOSEDIR implicitly
    2332             :                          * closed the associated fd.
    2333             :                          */
    2334           0 :                         fsp_set_fd(dirfsp, -1);
    2335             :                 }
    2336             :         }
    2337           0 :         if (dirfsp != NULL) {
    2338           0 :                 fd_close(dirfsp);
    2339           0 :                 file_free(NULL, dirfsp);
    2340             :         }
    2341           0 :         TALLOC_FREE(tmp_ctx);
    2342           0 :         if (saved_errno != 0) {
    2343           0 :                 errno = saved_errno;
    2344             :         }
    2345           0 :         return ret;
    2346             : }
    2347             : 
    2348           0 : static int shadow_copy2_mkdirat(vfs_handle_struct *handle,
    2349             :                                 struct files_struct *dirfsp,
    2350             :                                 const struct smb_filename *smb_fname,
    2351             :                                 mode_t mode)
    2352             : {
    2353           0 :         struct smb_filename *full_fname = NULL;
    2354           0 :         time_t timestamp = 0;
    2355             : 
    2356           0 :         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
    2357             :                                                   dirfsp,
    2358             :                                                   smb_fname);
    2359           0 :         if (full_fname == NULL) {
    2360           0 :                 errno = ENOMEM;
    2361           0 :                 return -1;
    2362             :         }
    2363             : 
    2364           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(),
    2365             :                                         handle,
    2366             :                                         full_fname,
    2367             :                                         &timestamp,
    2368             :                                         NULL)) {
    2369           0 :                 return -1;
    2370             :         }
    2371           0 :         TALLOC_FREE(full_fname);
    2372           0 :         if (timestamp != 0) {
    2373           0 :                 errno = EROFS;
    2374           0 :                 return -1;
    2375             :         }
    2376           0 :         return SMB_VFS_NEXT_MKDIRAT(handle,
    2377             :                         dirfsp,
    2378             :                         smb_fname,
    2379             :                         mode);
    2380             : }
    2381             : 
    2382           0 : static int shadow_copy2_fchflags(vfs_handle_struct *handle,
    2383             :                                 struct files_struct *fsp,
    2384             :                                 unsigned int flags)
    2385             : {
    2386           0 :         time_t timestamp = 0;
    2387             : 
    2388           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(),
    2389             :                                         handle,
    2390             :                                         fsp->fsp_name,
    2391             :                                         &timestamp,
    2392             :                                         NULL)) {
    2393           0 :                 return -1;
    2394             :         }
    2395           0 :         if (timestamp != 0) {
    2396           0 :                 errno = EROFS;
    2397           0 :                 return -1;
    2398             :         }
    2399           0 :         return SMB_VFS_NEXT_FCHFLAGS(handle, fsp, flags);
    2400             : }
    2401             : 
    2402           0 : static int shadow_copy2_fsetxattr(struct vfs_handle_struct *handle,
    2403             :                                  struct files_struct *fsp,
    2404             :                                  const char *aname, const void *value,
    2405             :                                  size_t size, int flags)
    2406             : {
    2407           0 :         time_t timestamp = 0;
    2408           0 :         const struct smb_filename *smb_fname = NULL;
    2409             : 
    2410           0 :         smb_fname = fsp->fsp_name;
    2411           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(),
    2412             :                                 handle,
    2413             :                                 smb_fname,
    2414             :                                 &timestamp,
    2415             :                                 NULL)) {
    2416           0 :                 return -1;
    2417             :         }
    2418           0 :         if (timestamp != 0) {
    2419           0 :                 errno = EROFS;
    2420           0 :                 return -1;
    2421             :         }
    2422           0 :         return SMB_VFS_NEXT_FSETXATTR(handle, fsp,
    2423             :                                 aname, value, size, flags);
    2424             : }
    2425             : 
    2426           0 : static NTSTATUS shadow_copy2_create_dfs_pathat(struct vfs_handle_struct *handle,
    2427             :                                 struct files_struct *dirfsp,
    2428             :                                 const struct smb_filename *smb_fname,
    2429             :                                 const struct referral *reflist,
    2430             :                                 size_t referral_count)
    2431             : {
    2432           0 :         time_t timestamp = 0;
    2433             : 
    2434           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(),
    2435             :                                         handle,
    2436             :                                         smb_fname,
    2437             :                                         &timestamp,
    2438             :                                         NULL)) {
    2439           0 :                 return NT_STATUS_NO_MEMORY;
    2440             :         }
    2441           0 :         if (timestamp != 0) {
    2442           0 :                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
    2443             :         }
    2444           0 :         return SMB_VFS_NEXT_CREATE_DFS_PATHAT(handle,
    2445             :                         dirfsp,
    2446             :                         smb_fname,
    2447             :                         reflist,
    2448             :                         referral_count);
    2449             : }
    2450             : 
    2451           0 : static NTSTATUS shadow_copy2_read_dfs_pathat(struct vfs_handle_struct *handle,
    2452             :                                 TALLOC_CTX *mem_ctx,
    2453             :                                 struct files_struct *dirfsp,
    2454             :                                 struct smb_filename *smb_fname,
    2455             :                                 struct referral **ppreflist,
    2456             :                                 size_t *preferral_count)
    2457             : {
    2458           0 :         time_t timestamp = 0;
    2459           0 :         char *stripped = NULL;
    2460           0 :         struct smb_filename *full_fname = NULL;
    2461           0 :         struct smb_filename *conv = NULL;
    2462             :         NTSTATUS status;
    2463             : 
    2464           0 :         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
    2465             :                                                   dirfsp,
    2466             :                                                   smb_fname);
    2467           0 :         if (full_fname == NULL) {
    2468           0 :                 return NT_STATUS_NO_MEMORY;
    2469             :         }
    2470             : 
    2471           0 :         if (!shadow_copy2_strip_snapshot(mem_ctx,
    2472             :                                         handle,
    2473             :                                         full_fname,
    2474             :                                         &timestamp,
    2475             :                                         &stripped)) {
    2476           0 :                 TALLOC_FREE(full_fname);
    2477           0 :                 return NT_STATUS_NO_MEMORY;
    2478             :         }
    2479           0 :         if (timestamp == 0) {
    2480           0 :                 TALLOC_FREE(full_fname);
    2481           0 :                 TALLOC_FREE(stripped);
    2482           0 :                 return SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
    2483             :                                         mem_ctx,
    2484             :                                         dirfsp,
    2485             :                                         smb_fname,
    2486             :                                         ppreflist,
    2487             :                                         preferral_count);
    2488             :         }
    2489             : 
    2490           0 :         conv = cp_smb_filename(mem_ctx, full_fname);
    2491           0 :         if (conv == NULL) {
    2492           0 :                 TALLOC_FREE(full_fname);
    2493           0 :                 TALLOC_FREE(stripped);
    2494           0 :                 return NT_STATUS_NO_MEMORY;
    2495             :         }
    2496           0 :         TALLOC_FREE(full_fname);
    2497           0 :         conv->base_name = shadow_copy2_convert(conv,
    2498             :                                         handle,
    2499             :                                         stripped,
    2500             :                                         timestamp);
    2501           0 :         TALLOC_FREE(stripped);
    2502           0 :         if (conv->base_name == NULL) {
    2503           0 :                 TALLOC_FREE(conv);
    2504           0 :                 return NT_STATUS_NO_MEMORY;
    2505             :         }
    2506             : 
    2507           0 :         status = SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
    2508             :                                 mem_ctx,
    2509             :                                 handle->conn->cwd_fsp,
    2510             :                                 conv,
    2511             :                                 ppreflist,
    2512             :                                 preferral_count);
    2513             : 
    2514           0 :         if (NT_STATUS_IS_OK(status)) {
    2515             :                 /* Return any stat(2) info. */
    2516           0 :                 smb_fname->st = conv->st;
    2517             :         }
    2518             : 
    2519           0 :         TALLOC_FREE(conv);
    2520           0 :         return status;
    2521             : }
    2522             : 
    2523           0 : static NTSTATUS shadow_copy2_get_real_filename_at(
    2524             :         struct vfs_handle_struct *handle,
    2525             :         struct files_struct *dirfsp,
    2526             :         const char *name,
    2527             :         TALLOC_CTX *mem_ctx,
    2528             :         char **found_name)
    2529             : {
    2530           0 :         struct shadow_copy2_private *priv = NULL;
    2531           0 :         time_t timestamp = 0;
    2532           0 :         char *stripped = NULL;
    2533             :         char *conv;
    2534           0 :         struct smb_filename *conv_fname = NULL;
    2535             :         NTSTATUS status;
    2536             :         bool ok;
    2537             : 
    2538           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    2539             :                                 return NT_STATUS_INTERNAL_ERROR);
    2540             : 
    2541           0 :         DBG_DEBUG("Path=[%s] name=[%s]\n", fsp_str_dbg(dirfsp), name);
    2542             : 
    2543           0 :         ok = shadow_copy2_strip_snapshot(
    2544             :                 talloc_tos(), handle, dirfsp->fsp_name, &timestamp, &stripped);
    2545           0 :         if (!ok) {
    2546           0 :                 status = map_nt_error_from_unix(errno);
    2547           0 :                 DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
    2548           0 :                 return status;
    2549             :         }
    2550           0 :         if (timestamp == 0) {
    2551           0 :                 DEBUG(10, ("timestamp == 0\n"));
    2552           0 :                 return SMB_VFS_NEXT_GET_REAL_FILENAME_AT(
    2553             :                         handle, dirfsp, name, mem_ctx, found_name);
    2554             :         }
    2555             : 
    2556             :         /*
    2557             :          * Note that stripped may be an empty string "" if path was ".". As
    2558             :          * shadow_copy2_convert() combines "" with the shadow-copy tree connect
    2559             :          * root fullpath and get_real_filename_full_scan() has an explicit check
    2560             :          * for "" this works.
    2561             :          */
    2562           0 :         DBG_DEBUG("stripped [%s]\n", stripped);
    2563             : 
    2564           0 :         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
    2565           0 :         if (conv == NULL) {
    2566           0 :                 status = map_nt_error_from_unix(errno);
    2567           0 :                 DBG_DEBUG("shadow_copy2_convert [%s] failed: %s\n",
    2568             :                           stripped,
    2569             :                           strerror(errno));
    2570           0 :                 return status;
    2571             :         }
    2572             : 
    2573           0 :         status = synthetic_pathref(
    2574             :                 talloc_tos(),
    2575           0 :                 dirfsp->conn->cwd_fsp,
    2576             :                 conv,
    2577             :                 NULL,
    2578             :                 NULL,
    2579             :                 0,
    2580             :                 0,
    2581             :                 &conv_fname);
    2582           0 :         if (!NT_STATUS_IS_OK(status)) {
    2583           0 :                 return status;
    2584             :         }
    2585             : 
    2586           0 :         DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
    2587             :                    "name=[%s]\n", conv, name));
    2588           0 :         status = SMB_VFS_NEXT_GET_REAL_FILENAME_AT(
    2589             :                 handle, conv_fname->fsp, name, mem_ctx, found_name);
    2590           0 :         DEBUG(10, ("NEXT_REAL_FILE_NAME returned %s\n", nt_errstr(status)));
    2591           0 :         if (NT_STATUS_IS_OK(status)) {
    2592           0 :                 TALLOC_FREE(conv_fname);
    2593           0 :                 return NT_STATUS_OK;
    2594             :         }
    2595           0 :         if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
    2596           0 :                 TALLOC_FREE(conv_fname);
    2597           0 :                 TALLOC_FREE(conv);
    2598           0 :                 return NT_STATUS_NOT_SUPPORTED;
    2599             :         }
    2600             : 
    2601           0 :         status = get_real_filename_full_scan_at(
    2602           0 :                 conv_fname->fsp, name, false, mem_ctx, found_name);
    2603           0 :         TALLOC_FREE(conv_fname);
    2604           0 :         if (!NT_STATUS_IS_OK(status)) {
    2605           0 :                 DBG_DEBUG("Scan [%s] for [%s] failed\n",
    2606             :                           conv, name);
    2607           0 :                 return status;
    2608             :         }
    2609             : 
    2610           0 :         DBG_DEBUG("Scan [%s] for [%s] returned [%s]\n",
    2611             :                   conv, name, *found_name);
    2612             : 
    2613           0 :         TALLOC_FREE(conv);
    2614           0 :         return NT_STATUS_OK;
    2615             : }
    2616             : 
    2617           0 : static const char *shadow_copy2_connectpath(
    2618             :         struct vfs_handle_struct *handle,
    2619             :         const struct files_struct *dirfsp,
    2620             :         const struct smb_filename *smb_fname_in)
    2621             : {
    2622           0 :         time_t timestamp = 0;
    2623           0 :         char *stripped = NULL;
    2624           0 :         char *tmp = NULL;
    2625           0 :         const char *fname = smb_fname_in->base_name;
    2626           0 :         const struct smb_filename *full = NULL;
    2627           0 :         struct smb_filename smb_fname = {0};
    2628           0 :         struct smb_filename *result_fname = NULL;
    2629           0 :         char *result = NULL;
    2630           0 :         char *parent_dir = NULL;
    2631           0 :         int saved_errno = 0;
    2632           0 :         size_t rootpath_len = 0;
    2633           0 :         struct shadow_copy2_private *priv = NULL;
    2634             : 
    2635           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    2636             :                                 return NULL);
    2637             : 
    2638           0 :         DBG_DEBUG("Calc connect path for [%s]\n", fname);
    2639             : 
    2640           0 :         if (priv->shadow_connectpath != NULL) {
    2641           0 :                 DBG_DEBUG("cached connect path is [%s]\n",
    2642             :                         priv->shadow_connectpath);
    2643           0 :                 return priv->shadow_connectpath;
    2644             :         }
    2645             : 
    2646           0 :         full = full_path_from_dirfsp_atname(
    2647             :                 talloc_tos(), dirfsp, smb_fname_in);
    2648           0 :         if (full == NULL) {
    2649           0 :                 return NULL;
    2650             :         }
    2651             : 
    2652           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, full,
    2653             :                                          &timestamp, &stripped)) {
    2654           0 :                 goto done;
    2655             :         }
    2656           0 :         if (timestamp == 0) {
    2657           0 :                 return SMB_VFS_NEXT_CONNECTPATH(handle, dirfsp, smb_fname_in);
    2658             :         }
    2659             : 
    2660           0 :         tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
    2661             :                                       &rootpath_len);
    2662           0 :         if (tmp == NULL) {
    2663           0 :                 if (errno != ENOENT) {
    2664           0 :                         goto done;
    2665             :                 }
    2666             : 
    2667             :                 /*
    2668             :                  * If the converted path does not exist, and converting
    2669             :                  * the parent yields something that does exist, then
    2670             :                  * this path refers to something that has not been
    2671             :                  * created yet, relative to the parent path.
    2672             :                  * The snapshot finding is relative to the parent.
    2673             :                  * (usually snapshots are read/only but this is not
    2674             :                  * necessarily true).
    2675             :                  * This code also covers getting a wildcard in the
    2676             :                  * last component, because this function is called
    2677             :                  * prior to sanitizing the path, and in SMB1 we may
    2678             :                  * get wildcards in path names.
    2679             :                  */
    2680           0 :                 if (!parent_dirname(talloc_tos(), stripped, &parent_dir,
    2681             :                                     NULL)) {
    2682           0 :                         errno = ENOMEM;
    2683           0 :                         goto done;
    2684             :                 }
    2685             : 
    2686           0 :                 tmp = shadow_copy2_do_convert(talloc_tos(), handle, parent_dir,
    2687             :                                               timestamp, &rootpath_len);
    2688           0 :                 if (tmp == NULL) {
    2689           0 :                         goto done;
    2690             :                 }
    2691             :         }
    2692             : 
    2693           0 :         DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
    2694             :                   (int)rootpath_len, tmp);
    2695             : 
    2696           0 :         tmp[rootpath_len] = '\0';
    2697           0 :         smb_fname = (struct smb_filename) { .base_name = tmp };
    2698             : 
    2699           0 :         result_fname = SMB_VFS_NEXT_REALPATH(handle, priv, &smb_fname);
    2700           0 :         if (result_fname == NULL) {
    2701           0 :                 goto done;
    2702             :         }
    2703             : 
    2704             :         /*
    2705             :          * SMB_VFS_NEXT_REALPATH returns a talloc'ed string.
    2706             :          * Don't leak memory.
    2707             :          */
    2708           0 :         TALLOC_FREE(priv->shadow_realpath);
    2709           0 :         priv->shadow_realpath = result_fname;
    2710           0 :         result = priv->shadow_realpath->base_name;
    2711             : 
    2712           0 :         DBG_DEBUG("connect path is [%s]\n", result);
    2713             : 
    2714           0 : done:
    2715           0 :         if (result == NULL) {
    2716           0 :                 saved_errno = errno;
    2717             :         }
    2718           0 :         TALLOC_FREE(tmp);
    2719           0 :         TALLOC_FREE(stripped);
    2720           0 :         TALLOC_FREE(parent_dir);
    2721           0 :         if (saved_errno != 0) {
    2722           0 :                 errno = saved_errno;
    2723             :         }
    2724           0 :         return result;
    2725             : }
    2726             : 
    2727           0 : static NTSTATUS shadow_copy2_parent_pathname(vfs_handle_struct *handle,
    2728             :                                              TALLOC_CTX *ctx,
    2729             :                                              const struct smb_filename *smb_fname_in,
    2730             :                                              struct smb_filename **parent_dir_out,
    2731             :                                              struct smb_filename **atname_out)
    2732             : {
    2733           0 :         time_t timestamp = 0;
    2734           0 :         char *stripped = NULL;
    2735           0 :         char *converted_name = NULL;
    2736           0 :         struct smb_filename *smb_fname = NULL;
    2737           0 :         struct smb_filename *parent = NULL;
    2738           0 :         struct smb_filename *atname = NULL;
    2739           0 :         struct shadow_copy2_private *priv = NULL;
    2740           0 :         bool ok = false;
    2741           0 :         bool is_converted = false;
    2742           0 :         NTSTATUS status = NT_STATUS_OK;
    2743           0 :         TALLOC_CTX *frame = NULL;
    2744             : 
    2745           0 :         SMB_VFS_HANDLE_GET_DATA(handle,
    2746             :                                 priv,
    2747             :                                 struct shadow_copy2_private,
    2748             :                                 return NT_STATUS_INTERNAL_ERROR);
    2749             : 
    2750           0 :         frame = talloc_stackframe();
    2751             : 
    2752           0 :         smb_fname = cp_smb_filename(frame, smb_fname_in);
    2753           0 :         if (smb_fname == NULL) {
    2754           0 :                 status = NT_STATUS_NO_MEMORY;
    2755           0 :                 goto fail;
    2756             :         }
    2757             : 
    2758             :         /* First, call the default PARENT_PATHNAME. */
    2759           0 :         status = SMB_VFS_NEXT_PARENT_PATHNAME(handle,
    2760             :                                               frame,
    2761             :                                               smb_fname,
    2762             :                                               &parent,
    2763             :                                               &atname);
    2764           0 :         if (!NT_STATUS_IS_OK(status)) {
    2765           0 :                 goto fail;
    2766             :         }
    2767             : 
    2768           0 :         if (parent->twrp == 0) {
    2769             :                 /*
    2770             :                  * Parent is not a snapshot path, return
    2771             :                  * the regular result.
    2772             :                  */
    2773           0 :                 status = NT_STATUS_OK;
    2774           0 :                 goto out;
    2775             :         }
    2776             : 
    2777             :         /* See if we can find a snapshot for the parent. */
    2778           0 :         ok = shadow_copy2_strip_snapshot_converted(frame,
    2779             :                                                    handle,
    2780             :                                                    parent,
    2781             :                                                    &timestamp,
    2782             :                                                    &stripped,
    2783             :                                                    &is_converted);
    2784           0 :         if (!ok) {
    2785           0 :                 status = map_nt_error_from_unix(errno);
    2786           0 :                 goto fail;
    2787             :         }
    2788             : 
    2789           0 :         if (is_converted) {
    2790             :                 /*
    2791             :                  * Already found snapshot for parent so wipe
    2792             :                  * out the twrp.
    2793             :                  */
    2794           0 :                 parent->twrp = 0;
    2795           0 :                 goto out;
    2796             :         }
    2797             : 
    2798           0 :         converted_name = shadow_copy2_convert(frame,
    2799             :                                               handle,
    2800             :                                               stripped,
    2801             :                                               timestamp);
    2802             : 
    2803           0 :         if (converted_name == NULL) {
    2804             :                 /*
    2805             :                  * Can't find snapshot for parent so wipe
    2806             :                  * out the twrp.
    2807             :                  */
    2808           0 :                 parent->twrp = 0;
    2809             :         }
    2810             : 
    2811           0 :   out:
    2812             : 
    2813           0 :         *parent_dir_out = talloc_move(ctx, &parent);
    2814           0 :         if (atname_out != NULL) {
    2815           0 :                 *atname_out = talloc_move(*parent_dir_out, &atname);
    2816             :         }
    2817             : 
    2818           0 :   fail:
    2819             : 
    2820           0 :         TALLOC_FREE(frame);
    2821           0 :         return status;
    2822             : }
    2823             : 
    2824           0 : static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
    2825             :                                 const struct smb_filename *smb_fname,
    2826             :                                 uint64_t *bsize,
    2827             :                                 uint64_t *dfree,
    2828             :                                 uint64_t *dsize)
    2829             : {
    2830           0 :         time_t timestamp = 0;
    2831           0 :         char *stripped = NULL;
    2832           0 :         int saved_errno = 0;
    2833           0 :         char *conv = NULL;
    2834           0 :         struct smb_filename *conv_smb_fname = NULL;
    2835           0 :         uint64_t ret = (uint64_t)-1;
    2836             : 
    2837           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(),
    2838             :                                 handle,
    2839             :                                 smb_fname,
    2840             :                                 &timestamp,
    2841             :                                 &stripped)) {
    2842           0 :                 return (uint64_t)-1;
    2843             :         }
    2844           0 :         if (timestamp == 0) {
    2845           0 :                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
    2846             :                                               bsize, dfree, dsize);
    2847             :         }
    2848           0 :         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
    2849           0 :         TALLOC_FREE(stripped);
    2850           0 :         if (conv == NULL) {
    2851           0 :                 return (uint64_t)-1;
    2852             :         }
    2853           0 :         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
    2854             :                                         conv,
    2855             :                                         NULL,
    2856             :                                         NULL,
    2857             :                                         0,
    2858           0 :                                         smb_fname->flags);
    2859           0 :         if (conv_smb_fname == NULL) {
    2860           0 :                 TALLOC_FREE(conv);
    2861           0 :                 return (uint64_t)-1;
    2862             :         }
    2863           0 :         ret = SMB_VFS_NEXT_DISK_FREE(handle, conv_smb_fname,
    2864             :                                 bsize, dfree, dsize);
    2865           0 :         if (ret == (uint64_t)-1) {
    2866           0 :                 saved_errno = errno;
    2867             :         }
    2868           0 :         TALLOC_FREE(conv);
    2869           0 :         TALLOC_FREE(conv_smb_fname);
    2870           0 :         if (saved_errno != 0) {
    2871           0 :                 errno = saved_errno;
    2872             :         }
    2873           0 :         return ret;
    2874             : }
    2875             : 
    2876           0 : static int shadow_copy2_get_quota(vfs_handle_struct *handle,
    2877             :                                 const struct smb_filename *smb_fname,
    2878             :                                 enum SMB_QUOTA_TYPE qtype,
    2879             :                                 unid_t id,
    2880             :                                 SMB_DISK_QUOTA *dq)
    2881             : {
    2882           0 :         time_t timestamp = 0;
    2883           0 :         char *stripped = NULL;
    2884             :         int ret;
    2885           0 :         int saved_errno = 0;
    2886             :         char *conv;
    2887           0 :         struct smb_filename *conv_smb_fname = NULL;
    2888             : 
    2889           0 :         if (!shadow_copy2_strip_snapshot(talloc_tos(),
    2890             :                                 handle,
    2891             :                                 smb_fname,
    2892             :                                 &timestamp,
    2893             :                                 &stripped)) {
    2894           0 :                 return -1;
    2895             :         }
    2896           0 :         if (timestamp == 0) {
    2897           0 :                 return SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname, qtype, id, dq);
    2898             :         }
    2899             : 
    2900           0 :         conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
    2901           0 :         TALLOC_FREE(stripped);
    2902           0 :         if (conv == NULL) {
    2903           0 :                 return -1;
    2904             :         }
    2905           0 :         conv_smb_fname = synthetic_smb_fname(talloc_tos(),
    2906             :                                         conv,
    2907             :                                         NULL,
    2908             :                                         NULL,
    2909             :                                         0,
    2910           0 :                                         smb_fname->flags);
    2911           0 :         if (conv_smb_fname == NULL) {
    2912           0 :                 TALLOC_FREE(conv);
    2913           0 :                 return -1;
    2914             :         }
    2915           0 :         ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv_smb_fname, qtype, id, dq);
    2916             : 
    2917           0 :         if (ret == -1) {
    2918           0 :                 saved_errno = errno;
    2919             :         }
    2920           0 :         TALLOC_FREE(conv);
    2921           0 :         TALLOC_FREE(conv_smb_fname);
    2922           0 :         if (saved_errno != 0) {
    2923           0 :                 errno = saved_errno;
    2924             :         }
    2925             : 
    2926           0 :         return ret;
    2927             : }
    2928             : 
    2929           0 : static ssize_t shadow_copy2_pwrite(vfs_handle_struct *handle,
    2930             :                                    files_struct *fsp,
    2931             :                                    const void *data,
    2932             :                                    size_t n,
    2933             :                                    off_t offset)
    2934             : {
    2935             :         ssize_t nwritten;
    2936             : 
    2937           0 :         nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
    2938           0 :         if (nwritten == -1) {
    2939           0 :                 if (errno == EBADF && fsp->fsp_flags.can_write) {
    2940           0 :                         errno = EROFS;
    2941             :                 }
    2942             :         }
    2943             : 
    2944           0 :         return nwritten;
    2945             : }
    2946             : 
    2947             : struct shadow_copy2_pwrite_state {
    2948             :         vfs_handle_struct *handle;
    2949             :         files_struct *fsp;
    2950             :         ssize_t ret;
    2951             :         struct vfs_aio_state vfs_aio_state;
    2952             : };
    2953             : 
    2954             : static void shadow_copy2_pwrite_done(struct tevent_req *subreq);
    2955             : 
    2956           0 : static struct tevent_req *shadow_copy2_pwrite_send(
    2957             :         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
    2958             :         struct tevent_context *ev, struct files_struct *fsp,
    2959             :         const void *data, size_t n, off_t offset)
    2960             : {
    2961           0 :         struct tevent_req *req = NULL, *subreq = NULL;
    2962           0 :         struct shadow_copy2_pwrite_state *state = NULL;
    2963             : 
    2964           0 :         req = tevent_req_create(mem_ctx, &state,
    2965             :                                 struct shadow_copy2_pwrite_state);
    2966           0 :         if (req == NULL) {
    2967           0 :                 return NULL;
    2968             :         }
    2969           0 :         state->handle = handle;
    2970           0 :         state->fsp = fsp;
    2971             : 
    2972           0 :         subreq = SMB_VFS_NEXT_PWRITE_SEND(state,
    2973             :                                           ev,
    2974             :                                           handle,
    2975             :                                           fsp,
    2976             :                                           data,
    2977             :                                           n,
    2978             :                                           offset);
    2979           0 :         if (tevent_req_nomem(subreq, req)) {
    2980           0 :                 return tevent_req_post(req, ev);
    2981             :         }
    2982           0 :         tevent_req_set_callback(subreq, shadow_copy2_pwrite_done, req);
    2983             : 
    2984           0 :         return req;
    2985             : }
    2986             : 
    2987           0 : static void shadow_copy2_pwrite_done(struct tevent_req *subreq)
    2988             : {
    2989           0 :         struct tevent_req *req = tevent_req_callback_data(
    2990             :                 subreq, struct tevent_req);
    2991           0 :         struct shadow_copy2_pwrite_state *state = tevent_req_data(
    2992             :                 req, struct shadow_copy2_pwrite_state);
    2993             : 
    2994           0 :         state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
    2995           0 :         TALLOC_FREE(subreq);
    2996           0 :         if (state->ret == -1) {
    2997           0 :                 tevent_req_error(req, state->vfs_aio_state.error);
    2998           0 :                 return;
    2999             :         }
    3000             : 
    3001           0 :         tevent_req_done(req);
    3002             : }
    3003             : 
    3004           0 : static ssize_t shadow_copy2_pwrite_recv(struct tevent_req *req,
    3005             :                                           struct vfs_aio_state *vfs_aio_state)
    3006             : {
    3007           0 :         struct shadow_copy2_pwrite_state *state = tevent_req_data(
    3008             :                 req, struct shadow_copy2_pwrite_state);
    3009             : 
    3010           0 :         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
    3011           0 :                 if ((vfs_aio_state->error == EBADF) &&
    3012           0 :                     state->fsp->fsp_flags.can_write)
    3013             :                 {
    3014           0 :                         vfs_aio_state->error = EROFS;
    3015           0 :                         errno = EROFS;
    3016             :                 }
    3017           0 :                 return -1;
    3018             :         }
    3019             : 
    3020           0 :         *vfs_aio_state = state->vfs_aio_state;
    3021           0 :         return state->ret;
    3022             : }
    3023             : 
    3024           0 : static int shadow_copy2_connect(struct vfs_handle_struct *handle,
    3025             :                                 const char *service, const char *user)
    3026             : {
    3027             :         struct shadow_copy2_config *config;
    3028             :         struct shadow_copy2_private *priv;
    3029             :         int ret;
    3030             :         const char *snapdir;
    3031           0 :         const char *snapprefix = NULL;
    3032             :         const char *delimiter;
    3033             :         const char *gmt_format;
    3034             :         const char *sort_order;
    3035           0 :         const char *basedir = NULL;
    3036           0 :         const char *snapsharepath = NULL;
    3037             :         const char *mount_point;
    3038             : 
    3039           0 :         DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
    3040             :                    (unsigned)handle->conn->cnum,
    3041             :                    handle->conn->connectpath));
    3042             : 
    3043           0 :         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
    3044           0 :         if (ret < 0) {
    3045           0 :                 return ret;
    3046             :         }
    3047             : 
    3048           0 :         priv = talloc_zero(handle->conn, struct shadow_copy2_private);
    3049           0 :         if (priv == NULL) {
    3050           0 :                 DBG_ERR("talloc_zero() failed\n");
    3051           0 :                 errno = ENOMEM;
    3052           0 :                 return -1;
    3053             :         }
    3054             : 
    3055           0 :         priv->snaps = talloc_zero(priv, struct shadow_copy2_snaplist_info);
    3056           0 :         if (priv->snaps == NULL) {
    3057           0 :                 DBG_ERR("talloc_zero() failed\n");
    3058           0 :                 errno = ENOMEM;
    3059           0 :                 return -1;
    3060             :         }
    3061             : 
    3062           0 :         config = talloc_zero(priv, struct shadow_copy2_config);
    3063           0 :         if (config == NULL) {
    3064           0 :                 DEBUG(0, ("talloc_zero() failed\n"));
    3065           0 :                 errno = ENOMEM;
    3066           0 :                 return -1;
    3067             :         }
    3068             : 
    3069           0 :         priv->config = config;
    3070             : 
    3071           0 :         gmt_format = lp_parm_const_string(SNUM(handle->conn),
    3072             :                                           "shadow", "format",
    3073             :                                           GMT_FORMAT);
    3074           0 :         config->gmt_format = talloc_strdup(config, gmt_format);
    3075           0 :         if (config->gmt_format == NULL) {
    3076           0 :                 DEBUG(0, ("talloc_strdup() failed\n"));
    3077           0 :                 errno = ENOMEM;
    3078           0 :                 return -1;
    3079             :         }
    3080             : 
    3081             :         /* config->gmt_format must not contain a path separator. */
    3082           0 :         if (strchr(config->gmt_format, '/') != NULL) {
    3083           0 :                 DEBUG(0, ("shadow:format %s must not contain a /"
    3084             :                         "character. Unable to initialize module.\n",
    3085             :                         config->gmt_format));
    3086           0 :                 errno = EINVAL;
    3087           0 :                 return -1;
    3088             :         }
    3089             : 
    3090           0 :         config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
    3091             :                                           "shadow", "sscanf", false);
    3092             : 
    3093           0 :         config->use_localtime = lp_parm_bool(SNUM(handle->conn),
    3094             :                                              "shadow", "localtime",
    3095             :                                              false);
    3096             : 
    3097           0 :         snapdir = lp_parm_const_string(SNUM(handle->conn),
    3098             :                                        "shadow", "snapdir",
    3099             :                                        ".snapshots");
    3100           0 :         config->snapdir = talloc_strdup(config, snapdir);
    3101           0 :         if (config->snapdir == NULL) {
    3102           0 :                 DEBUG(0, ("talloc_strdup() failed\n"));
    3103           0 :                 errno = ENOMEM;
    3104           0 :                 return -1;
    3105             :         }
    3106             : 
    3107           0 :         snapprefix = lp_parm_const_string(SNUM(handle->conn),
    3108             :                                        "shadow", "snapprefix",
    3109             :                                        NULL);
    3110           0 :         if (snapprefix != NULL) {
    3111           0 :                 priv->snaps->regex = talloc_zero(priv->snaps, regex_t);
    3112           0 :                 if (priv->snaps->regex == NULL) {
    3113           0 :                         DBG_ERR("talloc_zero() failed\n");
    3114           0 :                         errno = ENOMEM;
    3115           0 :                         return -1;
    3116             :                 }
    3117             : 
    3118             :                 /* pre-compute regex rule for matching pattern later */
    3119           0 :                 ret = regcomp(priv->snaps->regex, snapprefix, 0);
    3120           0 :                 if (ret) {
    3121           0 :                         DBG_ERR("Failed to create regex object\n");
    3122           0 :                         return -1;
    3123             :                 }
    3124             :         }
    3125             : 
    3126           0 :         delimiter = lp_parm_const_string(SNUM(handle->conn),
    3127             :                                        "shadow", "delimiter",
    3128             :                                        "_GMT");
    3129           0 :         if (delimiter != NULL) {
    3130           0 :                 priv->config->delimiter = talloc_strdup(priv->config, delimiter);
    3131           0 :                 if (priv->config->delimiter == NULL) {
    3132           0 :                         DBG_ERR("talloc_strdup() failed\n");
    3133           0 :                         errno = ENOMEM;
    3134           0 :                         return -1;
    3135             :                 }
    3136             :         }
    3137             : 
    3138           0 :         config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
    3139             :                                                   "shadow",
    3140             :                                                   "snapdirseverywhere",
    3141             :                                                   false);
    3142             : 
    3143           0 :         config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
    3144             :                                                 "shadow", "crossmountpoints",
    3145             :                                                 false);
    3146             : 
    3147           0 :         if (config->crossmountpoints && !config->snapdirseverywhere) {
    3148           0 :                 DBG_WARNING("Warning: 'crossmountpoints' depends on "
    3149             :                             "'snapdirseverywhere'. Disabling crossmountpoints.\n");
    3150             :         }
    3151             : 
    3152           0 :         config->fixinodes = lp_parm_bool(SNUM(handle->conn),
    3153             :                                          "shadow", "fixinodes",
    3154             :                                          false);
    3155             : 
    3156           0 :         sort_order = lp_parm_const_string(SNUM(handle->conn),
    3157             :                                           "shadow", "sort", "desc");
    3158           0 :         config->sort_order = talloc_strdup(config, sort_order);
    3159           0 :         if (config->sort_order == NULL) {
    3160           0 :                 DEBUG(0, ("talloc_strdup() failed\n"));
    3161           0 :                 errno = ENOMEM;
    3162           0 :                 return -1;
    3163             :         }
    3164             : 
    3165           0 :         mount_point = lp_parm_const_string(SNUM(handle->conn),
    3166             :                                            "shadow", "mountpoint", NULL);
    3167           0 :         if (mount_point != NULL) {
    3168           0 :                 if (mount_point[0] != '/') {
    3169           0 :                         DEBUG(1, (__location__ " Warning: 'mountpoint' is "
    3170             :                                   "relative ('%s'), but it has to be an "
    3171             :                                   "absolute path. Ignoring provided value.\n",
    3172             :                                   mount_point));
    3173           0 :                         mount_point = NULL;
    3174             :                 } else {
    3175             :                         char *p;
    3176           0 :                         p = strstr(handle->conn->connectpath, mount_point);
    3177           0 :                         if (p != handle->conn->connectpath) {
    3178           0 :                                 DBG_WARNING("Warning: the share root (%s) is "
    3179             :                                             "not a subdirectory of the "
    3180             :                                             "specified mountpoint (%s). "
    3181             :                                             "Ignoring provided value.\n",
    3182             :                                             handle->conn->connectpath,
    3183             :                                             mount_point);
    3184           0 :                                 mount_point = NULL;
    3185             :                         }
    3186             :                 }
    3187             :         }
    3188             : 
    3189           0 :         if (mount_point != NULL) {
    3190           0 :                 config->mount_point = talloc_strdup(config, mount_point);
    3191           0 :                 if (config->mount_point == NULL) {
    3192           0 :                         DEBUG(0, (__location__ " talloc_strdup() failed\n"));
    3193           0 :                         return -1;
    3194             :                 }
    3195             :         } else {
    3196           0 :                 config->mount_point = shadow_copy2_find_mount_point(config,
    3197             :                                                                     handle);
    3198           0 :                 if (config->mount_point == NULL) {
    3199           0 :                         DBG_WARNING("shadow_copy2_find_mount_point "
    3200             :                                     "of the share root '%s' failed: %s\n",
    3201             :                                     handle->conn->connectpath, strerror(errno));
    3202           0 :                         return -1;
    3203             :                 }
    3204             :         }
    3205             : 
    3206           0 :         basedir = lp_parm_const_string(SNUM(handle->conn),
    3207             :                                        "shadow", "basedir", NULL);
    3208             : 
    3209           0 :         if (basedir != NULL) {
    3210           0 :                 if (basedir[0] != '/') {
    3211           0 :                         DEBUG(1, (__location__ " Warning: 'basedir' is "
    3212             :                                   "relative ('%s'), but it has to be an "
    3213             :                                   "absolute path. Disabling basedir.\n",
    3214             :                                   basedir));
    3215           0 :                         basedir = NULL;
    3216             :                 } else {
    3217             :                         char *p;
    3218           0 :                         p = strstr(basedir, config->mount_point);
    3219           0 :                         if (p != basedir) {
    3220           0 :                                 DEBUG(1, ("Warning: basedir (%s) is not a "
    3221             :                                           "subdirectory of the share root's "
    3222             :                                           "mount point (%s). "
    3223             :                                           "Disabling basedir\n",
    3224             :                                           basedir, config->mount_point));
    3225           0 :                                 basedir = NULL;
    3226             :                         }
    3227             :                 }
    3228             :         }
    3229             : 
    3230           0 :         if (config->snapdirseverywhere && basedir != NULL) {
    3231           0 :                 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
    3232             :                           "with 'snapdirseverywhere'. Disabling basedir.\n"));
    3233           0 :                 basedir = NULL;
    3234             :         }
    3235             : 
    3236           0 :         snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
    3237             :                                              "snapsharepath", NULL);
    3238           0 :         if (snapsharepath != NULL) {
    3239           0 :                 if (snapsharepath[0] == '/') {
    3240           0 :                         DBG_WARNING("Warning: 'snapsharepath' is "
    3241             :                                     "absolute ('%s'), but it has to be a "
    3242             :                                     "relative path. Disabling snapsharepath.\n",
    3243             :                                     snapsharepath);
    3244           0 :                         snapsharepath = NULL;
    3245             :                 }
    3246           0 :                 if (config->snapdirseverywhere && snapsharepath != NULL) {
    3247           0 :                         DBG_WARNING("Warning: 'snapsharepath' is incompatible "
    3248             :                                     "with 'snapdirseverywhere'. Disabling "
    3249             :                                     "snapsharepath.\n");
    3250           0 :                         snapsharepath = NULL;
    3251             :                 }
    3252             :         }
    3253             : 
    3254           0 :         if (basedir != NULL && snapsharepath != NULL) {
    3255           0 :                 DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
    3256             :                             "'basedir'. Disabling snapsharepath\n");
    3257           0 :                 snapsharepath = NULL;
    3258             :         }
    3259             : 
    3260           0 :         if (snapsharepath != NULL) {
    3261           0 :                 config->rel_connectpath = talloc_strdup(config, snapsharepath);
    3262           0 :                 if (config->rel_connectpath == NULL) {
    3263           0 :                         DBG_ERR("talloc_strdup() failed\n");
    3264           0 :                         errno = ENOMEM;
    3265           0 :                         return -1;
    3266             :                 }
    3267             :         }
    3268             : 
    3269           0 :         if (basedir == NULL) {
    3270           0 :                 basedir = config->mount_point;
    3271             :         }
    3272             : 
    3273           0 :         if (config->rel_connectpath == NULL &&
    3274           0 :             strlen(basedir) < strlen(handle->conn->connectpath)) {
    3275           0 :                 config->rel_connectpath = talloc_strdup(config,
    3276           0 :                         handle->conn->connectpath + strlen(basedir));
    3277           0 :                 if (config->rel_connectpath == NULL) {
    3278           0 :                         DEBUG(0, ("talloc_strdup() failed\n"));
    3279           0 :                         errno = ENOMEM;
    3280           0 :                         return -1;
    3281             :                 }
    3282             :         }
    3283             : 
    3284           0 :         if (config->snapdir[0] == '/') {
    3285           0 :                 config->snapdir_absolute = true;
    3286             : 
    3287           0 :                 if (config->snapdirseverywhere == true) {
    3288           0 :                         DEBUG(1, (__location__ " Warning: An absolute snapdir "
    3289             :                                   "is incompatible with 'snapdirseverywhere', "
    3290             :                                   "setting 'snapdirseverywhere' to false.\n"));
    3291           0 :                         config->snapdirseverywhere = false;
    3292             :                 }
    3293             : 
    3294           0 :                 if (config->crossmountpoints == true) {
    3295           0 :                         DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
    3296             :                                   "is not supported with an absolute snapdir. "
    3297             :                                   "Disabling it.\n"));
    3298           0 :                         config->crossmountpoints = false;
    3299             :                 }
    3300             : 
    3301           0 :                 config->snapshot_basepath = config->snapdir;
    3302             :         } else {
    3303           0 :                 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
    3304             :                                 config->mount_point, config->snapdir);
    3305           0 :                 if (config->snapshot_basepath == NULL) {
    3306           0 :                         DEBUG(0, ("talloc_asprintf() failed\n"));
    3307           0 :                         errno = ENOMEM;
    3308           0 :                         return -1;
    3309             :                 }
    3310             :         }
    3311             : 
    3312           0 :         trim_string(config->mount_point, NULL, "/");
    3313           0 :         trim_string(config->rel_connectpath, "/", "/");
    3314           0 :         trim_string(config->snapdir, NULL, "/");
    3315           0 :         trim_string(config->snapshot_basepath, NULL, "/");
    3316             : 
    3317           0 :         DEBUG(10, ("shadow_copy2_connect: configuration:\n"
    3318             :                    "  share root: '%s'\n"
    3319             :                    "  mountpoint: '%s'\n"
    3320             :                    "  rel share root: '%s'\n"
    3321             :                    "  snapdir: '%s'\n"
    3322             :                    "  snapprefix: '%s'\n"
    3323             :                    "  delimiter: '%s'\n"
    3324             :                    "  snapshot base path: '%s'\n"
    3325             :                    "  format: '%s'\n"
    3326             :                    "  use sscanf: %s\n"
    3327             :                    "  snapdirs everywhere: %s\n"
    3328             :                    "  cross mountpoints: %s\n"
    3329             :                    "  fix inodes: %s\n"
    3330             :                    "  sort order: %s\n"
    3331             :                    "",
    3332             :                    handle->conn->connectpath,
    3333             :                    config->mount_point,
    3334             :                    config->rel_connectpath,
    3335             :                    config->snapdir,
    3336             :                    snapprefix,
    3337             :                    config->delimiter,
    3338             :                    config->snapshot_basepath,
    3339             :                    config->gmt_format,
    3340             :                    config->use_sscanf ? "yes" : "no",
    3341             :                    config->snapdirseverywhere ? "yes" : "no",
    3342             :                    config->crossmountpoints ? "yes" : "no",
    3343             :                    config->fixinodes ? "yes" : "no",
    3344             :                    config->sort_order
    3345             :                    ));
    3346             : 
    3347             : 
    3348           0 :         SMB_VFS_HANDLE_SET_DATA(handle, priv,
    3349             :                                 NULL, struct shadow_copy2_private,
    3350             :                                 return -1);
    3351             : 
    3352           0 :         return 0;
    3353             : }
    3354             : 
    3355           0 : static struct dirent *shadow_copy2_readdir(vfs_handle_struct *handle,
    3356             :                                            struct files_struct *dirfsp,
    3357             :                                            DIR *dirp,
    3358             :                                            SMB_STRUCT_STAT *sbuf)
    3359             : {
    3360           0 :         struct shadow_copy2_private *priv = NULL;
    3361           0 :         struct dirent *ent = NULL;
    3362             :         struct smb_filename atname;
    3363           0 :         struct smb_filename *full_fname = NULL;
    3364           0 :         time_t timestamp = 0;
    3365           0 :         char *stripped = NULL;
    3366           0 :         char *conv = NULL;
    3367           0 :         char *abspath = NULL;
    3368           0 :         bool converted = false;
    3369             : 
    3370           0 :         SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
    3371             :                                 return NULL);
    3372             : 
    3373           0 :         ent = SMB_VFS_NEXT_READDIR(handle, dirfsp, dirp, sbuf);
    3374           0 :         if (ent == NULL) {
    3375           0 :                 return NULL;
    3376             :         }
    3377           0 :         if (sbuf == NULL) {
    3378           0 :                 return ent;
    3379             :         }
    3380           0 :         if (ISDOT(dirfsp->fsp_name->base_name) && ISDOTDOT(ent->d_name)) {
    3381           0 :                 return ent;
    3382             :         }
    3383             : 
    3384           0 :         atname = (struct smb_filename) {
    3385           0 :                 .base_name = ent->d_name,
    3386           0 :                 .twrp = dirfsp->fsp_name->twrp,
    3387           0 :                 .flags = dirfsp->fsp_name->flags,
    3388             :         };
    3389             : 
    3390           0 :         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
    3391             :                                                   dirfsp,
    3392             :                                                   &atname);
    3393           0 :         if (full_fname == NULL) {
    3394           0 :                 return NULL;
    3395             :         }
    3396             : 
    3397           0 :         if (!shadow_copy2_strip_snapshot_converted(talloc_tos(),
    3398             :                                                    handle,
    3399             :                                                    full_fname,
    3400             :                                                    &timestamp,
    3401             :                                                    &stripped,
    3402             :                                                    &converted)) {
    3403           0 :                 TALLOC_FREE(full_fname);
    3404           0 :                 return NULL;
    3405             :         }
    3406             : 
    3407           0 :         if (timestamp == 0 && !converted) {
    3408             :                 /* Not a snapshot path, no need for convert_sbuf() */
    3409           0 :                 TALLOC_FREE(stripped);
    3410           0 :                 TALLOC_FREE(full_fname);
    3411           0 :                 return ent;
    3412             :         }
    3413             : 
    3414           0 :         if (timestamp == 0) {
    3415           0 :                 abspath = make_path_absolute(talloc_tos(),
    3416             :                                              priv,
    3417           0 :                                              full_fname->base_name);
    3418           0 :                 TALLOC_FREE(full_fname);
    3419           0 :                 if (abspath == NULL) {
    3420           0 :                         return NULL;
    3421             :                 }
    3422             :         } else {
    3423           0 :                 conv = shadow_copy2_convert(talloc_tos(),
    3424             :                                             handle,
    3425             :                                             stripped,
    3426             :                                             timestamp);
    3427           0 :                 TALLOC_FREE(stripped);
    3428           0 :                 if (conv == NULL) {
    3429           0 :                         return NULL;
    3430             :                 }
    3431             : 
    3432           0 :                 abspath = make_path_absolute(talloc_tos(), priv, conv);
    3433           0 :                 TALLOC_FREE(conv);
    3434           0 :                 if (abspath == NULL) {
    3435           0 :                         return NULL;
    3436             :                 }
    3437             :         }
    3438             : 
    3439           0 :         convert_sbuf(handle, abspath, sbuf);
    3440             : 
    3441           0 :         TALLOC_FREE(abspath);
    3442           0 :         return ent;
    3443             : }
    3444             : 
    3445             : static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
    3446             :         .connect_fn = shadow_copy2_connect,
    3447             :         .disk_free_fn = shadow_copy2_disk_free,
    3448             :         .get_quota_fn = shadow_copy2_get_quota,
    3449             :         .create_dfs_pathat_fn = shadow_copy2_create_dfs_pathat,
    3450             :         .read_dfs_pathat_fn = shadow_copy2_read_dfs_pathat,
    3451             :         .renameat_fn = shadow_copy2_renameat,
    3452             :         .linkat_fn = shadow_copy2_linkat,
    3453             :         .symlinkat_fn = shadow_copy2_symlinkat,
    3454             :         .stat_fn = shadow_copy2_stat,
    3455             :         .lstat_fn = shadow_copy2_lstat,
    3456             :         .fstat_fn = shadow_copy2_fstat,
    3457             :         .fstatat_fn = shadow_copy2_fstatat,
    3458             :         .openat_fn = shadow_copy2_openat,
    3459             :         .unlinkat_fn = shadow_copy2_unlinkat,
    3460             :         .fchmod_fn = shadow_copy2_fchmod,
    3461             :         .chdir_fn = shadow_copy2_chdir,
    3462             :         .fntimes_fn = shadow_copy2_fntimes,
    3463             :         .readlinkat_fn = shadow_copy2_readlinkat,
    3464             :         .mknodat_fn = shadow_copy2_mknodat,
    3465             :         .realpath_fn = shadow_copy2_realpath,
    3466             :         .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
    3467             :         .mkdirat_fn = shadow_copy2_mkdirat,
    3468             :         .fsetxattr_fn = shadow_copy2_fsetxattr,
    3469             :         .fchflags_fn = shadow_copy2_fchflags,
    3470             :         .get_real_filename_at_fn = shadow_copy2_get_real_filename_at,
    3471             :         .pwrite_fn = shadow_copy2_pwrite,
    3472             :         .pwrite_send_fn = shadow_copy2_pwrite_send,
    3473             :         .pwrite_recv_fn = shadow_copy2_pwrite_recv,
    3474             :         .connectpath_fn = shadow_copy2_connectpath,
    3475             :         .parent_pathname_fn = shadow_copy2_parent_pathname,
    3476             :         .readdir_fn = shadow_copy2_readdir,
    3477             : };
    3478             : 
    3479             : static_decl_vfs;
    3480          27 : NTSTATUS vfs_shadow_copy2_init(TALLOC_CTX *ctx)
    3481             : {
    3482          27 :         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
    3483             :                                 "shadow_copy2", &vfs_shadow_copy2_fns);
    3484             : }

Generated by: LCOV version 1.14