1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * Volume Copy Shadow Services (VSS) provides a way for users to
  28  * restore/recover deleted files/directories.
  29  * For the server to support VSS for Microsoft clients, there is
  30  * two basic functions that need to be implemented.
  31  * The first is to intercept the NT_TRANSACT_IOCTL command with
  32  * the function code of FSCTL_SRV_ENUMERATE_SNAPSHOTS (0x00144064).
  33  * This is to report the count or the count and list of snapshots
  34  * for that share.
  35  * The second function need to trap commands with the
  36  * SMB_FLAGS2_REPARSE_PATH bit set in the smb header.  This bit
  37  * means that there is a @GMT token in path that needs to be
  38  * processed.  The @GMT token means to process this command, but
  39  * in the snapshot.
  40  */
  41 
  42 #include <smbsrv/smb_kproto.h>
  43 #include <smbsrv/string.h>
  44 #include <smb/winioctl.h>
  45 #include <smbsrv/smb_door.h>
  46 
  47 /* Size of the token on the wire due to encoding */
  48 #define SMB_VSS_GMT_NET_SIZE(sr) (smb_ascii_or_unicode_null_len(sr) * \
  49     SMB_VSS_GMT_SIZE)
  50 
  51 #define SMB_VSS_COUNT_SIZE 16
  52 
  53 static boolean_t smb_vss_is_gmttoken(const char *);
  54 static const char *smb_vss_find_gmttoken(const char *);
  55 static uint32_t smb_vss_encode_gmttokens(smb_request_t *, smb_fsctl_t *,
  56     int32_t, smb_gmttoken_response_t *);
  57 static void smb_vss_remove_first_token_from_path(char *);
  58 
  59 static uint32_t smb_vss_get_count(smb_tree_t *, char *);
  60 static void smb_vss_map_gmttoken(smb_tree_t *, char *, char *, time_t, char *);
  61 static void smb_vss_get_snapshots(smb_tree_t *, char *,
  62     uint32_t, smb_gmttoken_response_t *);
  63 static void smb_vss_get_snapshots_free(smb_gmttoken_response_t *);
  64 static int smb_vss_lookup_node(smb_request_t *sr, smb_node_t *, vnode_t *,
  65     char *, smb_node_t *, smb_node_t **);
  66 
  67 /*
  68  * This is to respond to the nt_transact_ioctl to either respond with the
  69  * number of snapshots, or to respond with the list.  It needs to be sorted
  70  * before the reply.  If the the max data bytes to return is
  71  * SMB_VSS_COUNT_SIZE, then all that is requested is the count, otherwise
  72  * return the count and the list of @GMT tokens (one token for each
  73  * snapshot).
  74  */
  75 uint32_t
  76 smb_vss_enum_snapshots(smb_request_t *sr, smb_fsctl_t *fsctl)
  77 {
  78         uint32_t count = 0;
  79         char *root_path;
  80         uint32_t status = NT_STATUS_SUCCESS;
  81         smb_gmttoken_response_t snaps;
  82 
  83         ASSERT(sr->fid_ofile);
  84         ASSERT(sr->fid_ofile->f_node);
  85 
  86         if (fsctl->MaxOutputResp < SMB_VSS_COUNT_SIZE)
  87                 return (NT_STATUS_INVALID_PARAMETER);
  88 
  89         /*
  90          * smbd will find the root of the lowest filesystem from mntpath of a
  91          * file by comparing it agaisnt mnttab, repeatedly removing components
  92          * until one matches.
  93          */
  94         root_path  = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
  95         if (smb_node_getmntpath(sr->fid_ofile->f_node, root_path,
  96             MAXPATHLEN) != 0)
  97                 return (NT_STATUS_INVALID_PARAMETER);
  98 
  99         if (fsctl->MaxOutputResp == SMB_VSS_COUNT_SIZE) {
 100                 count = smb_vss_get_count(sr->tid_tree, root_path);
 101                 if (smb_mbc_encodef(fsctl->out_mbc, "lllw", count, 0,
 102                     (count * SMB_VSS_GMT_NET_SIZE(sr) +
 103                     smb_ascii_or_unicode_null_len(sr)), 0) != 0) {
 104                         status = NT_STATUS_INVALID_PARAMETER;
 105                 }
 106         } else {
 107                 count = fsctl->MaxOutputResp / SMB_VSS_GMT_NET_SIZE(sr);
 108 
 109                 smb_vss_get_snapshots(sr->tid_tree, root_path,
 110                     count, &snaps);
 111 
 112                 status = smb_vss_encode_gmttokens(sr, fsctl, count, &snaps);
 113 
 114                 smb_vss_get_snapshots_free(&snaps);
 115         }
 116 
 117         kmem_free(root_path, MAXPATHLEN);
 118         return (status);
 119 }
 120 
 121 /*
 122  * sr - the request info, used to find root of dataset,
 123  *      unicode or ascii, where the share is rooted in the
 124  *      dataset
 125  * cur_node - where in the share for the command
 126  * vss_cur_node - returned value for the snapshot version
 127  *                of the cur_node
 128  * gmttoken - if SMB1, the gmttoken to be used to find the snapshot.
 129  *            Otherwise, NULL.
 130  *
 131  * This routine is the processing for handling the
 132  * SMB_FLAGS2_REPARSE_PATH bit being set in the smb header.
 133  *
 134  * By using the cur_node passed in, a new node is found or
 135  * created that is the same place in the directory tree, but
 136  * in the snapshot.
 137  */
 138 int
 139 smb_vss_lookup_nodes(smb_request_t *sr, smb_node_t *cur_node,
 140     smb_node_t **vss_cur_node, char *gmttoken)
 141 {
 142         smb_arg_open_t  *op = &sr->arg.open;
 143         char            *snapname, *path;
 144         vnode_t         *fsrootvp = NULL;
 145         time_t          toktime;
 146         int             err = 0;
 147 
 148         if (sr->tid_tree == NULL)
 149                 return (ESTALE);
 150 
 151         if (gmttoken != NULL) {
 152                 toktime = 0;
 153         } else {
 154                 /* SMB2 and later */
 155                 toktime = op->timewarp.tv_sec;
 156         }
 157 
 158         path = smb_srm_alloc(sr, MAXPATHLEN);
 159         snapname = smb_srm_alloc(sr, MAXPATHLEN);
 160 
 161         err = smb_node_getmntpath(cur_node, path, MAXPATHLEN);
 162         if (err != 0)
 163                 return (err);
 164 
 165         /*
 166          * Find the corresponding snapshot name.  If snapname is
 167          * empty after the map call, no such snapshot was found.
 168          */
 169         *snapname = '\0';
 170         smb_vss_map_gmttoken(sr->tid_tree, path, gmttoken, toktime,
 171             snapname);
 172         if (*snapname == '\0')
 173                 return (ENOENT);
 174 
 175         /* find snapshot nodes */
 176         err = VFS_ROOT(cur_node->vp->v_vfsp, &fsrootvp);
 177         if (err != 0)
 178                 return (err);
 179 
 180         /* find snapshot node corresponding to cur_node */
 181         err = smb_vss_lookup_node(sr, cur_node, fsrootvp,
 182             snapname, cur_node, vss_cur_node);
 183 
 184         VN_RELE(fsrootvp);
 185 
 186         return (err);
 187 }
 188 
 189 /*
 190  * Find snapshot node corresponding to 'node', and return it in
 191  * 'vss_node', as follows:
 192  * - find the path from fsrootvp to node, appending it to the
 193  *   the snapshot path
 194  * - lookup the vnode and smb_node (vss_node).
 195  */
 196 static int
 197 smb_vss_lookup_node(smb_request_t *sr, smb_node_t *node, vnode_t *fsrootvp,
 198     char *snapname, smb_node_t *dnode, smb_node_t **vss_node)
 199 {
 200         char *p, *path;
 201         int err, len;
 202         vnode_t *vp = NULL;
 203 
 204         *vss_node = NULL;
 205 
 206         path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
 207         (void) snprintf(path, MAXPATHLEN, ".zfs/snapshot/%s/", snapname);
 208         len = strlen(path);
 209         p = path + len;
 210 
 211         err = smb_node_getpath(node, fsrootvp, p, MAXPATHLEN - len);
 212         if (err == 0) {
 213                 vp = smb_lookuppathvptovp(sr, path, fsrootvp, fsrootvp);
 214                 if (vp) {
 215                         *vss_node = smb_node_lookup(sr, NULL, zone_kcred(),
 216                             vp, snapname, dnode, NULL);
 217                         VN_RELE(vp);
 218                 }
 219         }
 220 
 221         kmem_free(path, MAXPATHLEN);
 222 
 223         if (*vss_node != NULL)
 224                 return (0);
 225 
 226         return (err ? err : ENOENT);
 227 }
 228 
 229 
 230 static boolean_t
 231 smb_vss_is_gmttoken(const char *s)
 232 {
 233         char *t = "@GMT-NNNN.NN.NN-NN.NN.NN";
 234         const char *str;
 235         char *template;
 236 
 237         template = t;
 238         str = s;
 239 
 240         while (*template) {
 241                 if (*template == 'N') {
 242                         if (!smb_isdigit(*str))
 243                                 return (B_FALSE);
 244                 } else if (*template != *str) {
 245                         return (B_FALSE);
 246                 }
 247 
 248                 template++;
 249                 str++;
 250         }
 251 
 252         /* Make sure it is JUST the @GMT token */
 253         if ((*str == '\0') || (*str == '/'))
 254                 return (B_TRUE);
 255 
 256         return (B_FALSE);
 257 }
 258 
 259 static const char *
 260 smb_vss_find_gmttoken(const char *path)
 261 {
 262         const char *p;
 263 
 264         p = path;
 265 
 266         while (*p) {
 267                 if (*p == '@' && smb_vss_is_gmttoken(p))
 268                         return (p);
 269                 p++;
 270         }
 271         return (NULL);
 272 }
 273 
 274 static uint32_t
 275 smb_vss_encode_gmttokens(smb_request_t *sr, smb_fsctl_t *fsctl,
 276     int32_t count, smb_gmttoken_response_t *snap_data)
 277 {
 278         uint32_t i;
 279         uint32_t returned_count;
 280         uint32_t num_gmttokens;
 281         char **gmttokens;
 282         uint32_t status = NT_STATUS_SUCCESS;
 283         uint32_t data_size;
 284 
 285         returned_count = snap_data->gtr_count;
 286         num_gmttokens = snap_data->gtr_gmttokens.gtr_gmttokens_len;
 287         gmttokens = snap_data->gtr_gmttokens.gtr_gmttokens_val;
 288 
 289         if (returned_count > count)
 290                 status = NT_STATUS_BUFFER_TOO_SMALL;
 291 
 292         data_size = returned_count * SMB_VSS_GMT_NET_SIZE(sr) +
 293             smb_ascii_or_unicode_null_len(sr);
 294 
 295         if (smb_mbc_encodef(fsctl->out_mbc, "lll", returned_count,
 296             num_gmttokens, data_size) != 0)
 297                 return (NT_STATUS_INVALID_PARAMETER);
 298 
 299         if (status == NT_STATUS_SUCCESS) {
 300                 for (i = 0; i < num_gmttokens; i++) {
 301                         if (smb_mbc_encodef(fsctl->out_mbc, "%u", sr,
 302                             *gmttokens) != 0)
 303                                 status = NT_STATUS_INVALID_PARAMETER;
 304                         gmttokens++;
 305                 }
 306         }
 307 
 308         return (status);
 309 }
 310 
 311 /* This removes the first @GMT from the path */
 312 static void
 313 smb_vss_remove_first_token_from_path(char *path)
 314 {
 315         boolean_t found;
 316         char *src, *dest;
 317 
 318         src = path;
 319         dest = path;
 320 
 321         found = B_FALSE;
 322 
 323         while (*src != '\0') {
 324                 if (!found && smb_vss_is_gmttoken(src)) {
 325                         src += SMB_VSS_GMT_SIZE - 1;
 326                         if (*src == '/')
 327                                 src += 1;
 328                         found = B_TRUE;
 329                         continue;
 330                 }
 331                 *dest = *src;
 332                 src++;
 333                 dest++;
 334         }
 335         *dest = *src;
 336 }
 337 
 338 /*
 339  * This returns the number of snapshots for the dataset
 340  * of the path provided.
 341  */
 342 static uint32_t
 343 smb_vss_get_count(smb_tree_t *tree, char *resource_path)
 344 {
 345         uint32_t        count = 0;
 346         int             rc;
 347         smb_string_t    path;
 348 
 349         path.buf = resource_path;
 350 
 351         rc = smb_kdoor_upcall(tree->t_server, SMB_DR_VSS_GET_COUNT,
 352             &path, smb_string_xdr, &count, xdr_uint32_t);
 353 
 354         if (rc != 0)
 355                 count = 0;
 356 
 357         return (count);
 358 }
 359 
 360 /*
 361  * This takes a path for the root of the dataset and gets the counts of
 362  * snapshots for that dataset and the list of @GMT tokens (one for each
 363  * snapshot) up to the count provided.
 364  *
 365  * Call smb_vss_get_snapshots_free after to free up the data.
 366  */
 367 static void
 368 smb_vss_get_snapshots(smb_tree_t *tree, char *resource_path,
 369     uint32_t count, smb_gmttoken_response_t *gmttokens)
 370 {
 371         smb_gmttoken_query_t    request;
 372 
 373         request.gtq_count = count;
 374         request.gtq_path = resource_path;
 375         bzero(gmttokens, sizeof (smb_gmttoken_response_t));
 376 
 377         (void) smb_kdoor_upcall(tree->t_server, SMB_DR_VSS_GET_SNAPSHOTS,
 378             &request, smb_gmttoken_query_xdr,
 379             gmttokens, smb_gmttoken_response_xdr);
 380 }
 381 
 382 static void
 383 smb_vss_get_snapshots_free(smb_gmttoken_response_t *reply)
 384 {
 385         xdr_free(smb_gmttoken_response_xdr, (char *)reply);
 386 }
 387 
 388 /*
 389  * Returns the snapshot name for the @GMT token provided for the dataset
 390  * of the path.  If the snapshot cannot be found, a string with a NULL
 391  * is returned.
 392  */
 393 static void
 394 smb_vss_map_gmttoken(smb_tree_t *tree, char *path, char *gmttoken,
 395     time_t toktime, char *snapname)
 396 {
 397         smb_gmttoken_snapname_t request;
 398         smb_string_t            result;
 399 
 400         bzero(&result, sizeof (smb_string_t));
 401         result.buf = snapname;
 402 
 403         request.gts_path = path;
 404         request.gts_gmttoken = gmttoken;
 405         request.gts_toktime = toktime;
 406 
 407         (void) smb_kdoor_upcall(tree->t_server, SMB_DR_VSS_MAP_GMTTOKEN,
 408             &request, smb_gmttoken_snapname_xdr,
 409             &result, smb_string_xdr);
 410 }
 411 
 412 int
 413 smb_vss_extract_gmttoken(char *buf, char *gmttoken)
 414 {
 415         const char *p;
 416 
 417         /* get gmttoken from buf */
 418         if ((p = smb_vss_find_gmttoken(buf)) == NULL)
 419                 return (ENOENT);
 420 
 421         bcopy(p, gmttoken, SMB_VSS_GMT_SIZE);
 422         gmttoken[SMB_VSS_GMT_SIZE - 1] = '\0';
 423 
 424         smb_vss_remove_first_token_from_path(buf);
 425 
 426         return (0);
 427 }