LCOV - code coverage report
Current view: top level - source3/modules - vfs_shadow_copy2.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 933 1404 66.5 %
Date: 2024-04-21 15:09:00 Functions: 49 58 84.5 %

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

Generated by: LCOV version 1.14