LCOV - code coverage report
Current view: top level - source3/modules - vfs_shadow_copy2.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 912 1372 66.5 %
Date: 2021-09-23 10:06:22 Functions: 46 57 80.7 %

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

Generated by: LCOV version 1.13