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 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 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_incl.h> 43 #include <smbsrv/winioctl.h> 44 #include <smbsrv/ntstatus.h> 45 #include <smbsrv/smb_door_svc.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 *str); 54 static const char *smb_vss_find_gmttoken(const char *path); 55 static int smb_vss_get_fsmountpath(smb_request_t *sr, char *buf, 56 uint32_t buflen); 57 static uint32_t smb_vss_encode_gmttokens(smb_request_t *sr, smb_xa_t *xa, 58 int32_t count, smb_dr_return_gmttokens_t *snap_data); 59 static void smb_vss_remove_first_token_from_path(char *c); 60 61 /* 62 * This is to respond to the nt_transact_ioctl to either respond with the 63 * number of snapshots, or to respond with the list. It needs to be sorted 64 * before the reply. If the the max data bytes to return is 65 * SMB_VSS_COUNT_SIZE, then all that is requested is the count, otherwise 66 * return the count and the list of @GMT tokens (one token for each 67 * snapshot). 68 */ 69 uint32_t 70 smb_vss_ioctl_enumerate_snaps(smb_request_t *sr, smb_xa_t *xa) 71 { 72 uint32_t count = 0; 73 char *root_path; 74 uint32_t err = SDRC_SUCCESS; 75 smb_dr_return_gmttokens_t gmttokens; 76 77 if (xa->smb_mdrcnt < SMB_VSS_COUNT_SIZE) { 78 smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0); 79 return (SDRC_ERROR); 80 } 81 82 root_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 83 err = smb_vss_get_fsmountpath(sr, root_path, MAXPATHLEN); 84 85 if (err != SDRC_SUCCESS) { 86 smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0); 87 return (SDRC_ERROR); 88 } 89 if (xa->smb_mdrcnt == SMB_VSS_COUNT_SIZE) { 90 count = smb_upcall_vss_get_count(root_path); 91 if (smb_mbc_encodef(&xa->rep_data_mb, "lllw", count, 0, 92 (count * SMB_VSS_GMT_NET_SIZE(sr) + 93 smb_ascii_or_unicode_null_len(sr)), 0) != 0) { 94 smbsr_error(sr, 0, ERRSRV, ERRerror); 95 err = SDRC_ERROR; 96 } 97 } else { 98 count = xa->smb_mdrcnt / SMB_VSS_GMT_NET_SIZE(sr); 99 100 smb_upcall_vss_get_snapshots(root_path, count, &gmttokens); 101 102 err = smb_vss_encode_gmttokens(sr, xa, count, &gmttokens); 103 104 smb_upcall_vss_get_snapshots_free(&gmttokens); 105 } 106 107 kmem_free(root_path, MAXPATHLEN); 108 109 return (err); 110 } 111 112 113 /* 114 * sr - the request info, used to find root of dataset, 115 * unicode or ascii, where the share is rooted in the 116 * dataset 117 * root_node - root of the share 118 * cur_node - where in the share for the command 119 * buf - is the path for the command to be processed 120 * returned without @GMT if processed 121 * vss_cur_node - returned value for the snapshot version 122 * of the cur_node 123 * vss_root_node - returned value for the snapshot version 124 * of the root_node 125 * 126 * This routine is the processing for handling the 127 * SMB_FLAGS2_REPARSE_PATH bit being set in the smb header. 128 * 129 * By using the cur_node passed in, a new node is found or 130 * created that is the same place in the directory tree, but 131 * in the snapshot. We also use root_node to do the same for 132 * the root. 133 * One the new smb node is found, the path is modified by 134 * removing the @GMT token from the path in the buf. 135 */ 136 137 int 138 smb_vss_lookup_nodes(smb_request_t *sr, smb_node_t *root_node, 139 smb_node_t *cur_node, char *buf, smb_node_t **vss_cur_node, 140 smb_node_t **vss_root_node) 141 { 142 const char *p; 143 char *rootpath; 144 char *snapname; 145 char *nodepath; 146 char gmttoken[SMB_VSS_GMT_SIZE]; 147 smb_attr_t attr; 148 vnode_t *fsrootvp; 149 vnode_t *vp = NULL; 150 int err = 0; 151 152 if (sr->tid_tree == NULL) 153 return (ESTALE); 154 155 ASSERT(sr->tid_tree->t_snode); 156 ASSERT(sr->tid_tree->t_snode->vp); 157 ASSERT(sr->tid_tree->t_snode->vp->v_vfsp); 158 159 if ((p = smb_vss_find_gmttoken(buf)) == NULL) 160 return (ENOENT); 161 162 bcopy(p, gmttoken, SMB_VSS_GMT_SIZE); 163 gmttoken[SMB_VSS_GMT_SIZE - 1] = '\0'; 164 165 err = VFS_ROOT(sr->tid_tree->t_snode->vp->v_vfsp, &fsrootvp); 166 if (err != 0) 167 return (err); 168 169 rootpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 170 snapname = kmem_alloc(MAXNAMELEN, KM_SLEEP); 171 nodepath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 172 173 err = smb_vss_get_fsmountpath(sr, rootpath, MAXPATHLEN); 174 175 if (err != 0) 176 goto error; 177 178 *snapname = '\0'; 179 180 smb_upcall_vss_map_gmttoken(rootpath, gmttoken, snapname); 181 182 if (!*snapname) { 183 err = ENOENT; 184 goto error; 185 } 186 187 /* note the value of root_node->vp */ 188 err = vnodetopath(fsrootvp, root_node->vp, nodepath, 189 MAXPATHLEN, kcred); 190 191 if (err != 0) 192 goto error; 193 194 (void) snprintf(rootpath, MAXPATHLEN, ".zfs/snapshot/%s/%s", 195 snapname, nodepath); 196 197 vp = smb_lookuppathvptovp(sr, rootpath, fsrootvp, fsrootvp); 198 199 if (vp) { 200 /* note the value of cur_node->vp */ 201 err = vnodetopath(fsrootvp, cur_node->vp, nodepath, 202 MAXPATHLEN, kcred); 203 if (err != 0) { 204 VN_RELE(vp); 205 goto error; 206 } 207 208 *vss_root_node = smb_node_lookup(sr, NULL, kcred, vp, 209 gmttoken, cur_node, NULL, &attr); 210 VN_RELE(vp); 211 212 if (*vss_root_node == NULL) { 213 err = ENOENT; 214 goto error; 215 } 216 217 (void) snprintf(rootpath, MAXPATHLEN, ".zfs/snapshot/%s/%s", 218 snapname, nodepath); 219 220 221 vp = smb_lookuppathvptovp(sr, rootpath, fsrootvp, fsrootvp); 222 223 if (vp) { 224 *vss_cur_node = smb_node_lookup(sr, NULL, kcred, vp, 225 gmttoken, cur_node, NULL, &attr); 226 VN_RELE(vp); 227 228 if (*vss_cur_node != NULL) { 229 smb_vss_remove_first_token_from_path(buf); 230 } else { 231 (void) smb_node_release(*vss_root_node); 232 err = ENOENT; 233 } 234 } else { 235 (void) smb_node_release(*vss_root_node); 236 err = ENOENT; 237 } 238 } else { 239 err = ENOENT; 240 } 241 242 error: 243 VN_RELE(fsrootvp); 244 kmem_free(rootpath, MAXPATHLEN); 245 kmem_free(snapname, MAXNAMELEN); 246 kmem_free(nodepath, MAXPATHLEN); 247 248 return (err); 249 } 250 251 252 static boolean_t 253 smb_vss_is_gmttoken(const char *s) 254 { 255 char *t = "@GMT-NNNN.NN.NN-NN.NN.NN"; 256 const char *str; 257 char *template; 258 259 template = t; 260 str = s; 261 262 while (*template) { 263 if (*template == 'N') { 264 if (!mts_isdigit(*str)) 265 return (B_FALSE); 266 } else if (*template != *str) { 267 return (B_FALSE); 268 } 269 270 template++; 271 str++; 272 } 273 274 /* Make sure it is JUST the @GMT token */ 275 if ((*str == '\0') || (*str == '/')) 276 return (B_TRUE); 277 278 return (B_FALSE); 279 } 280 281 static const char * 282 smb_vss_find_gmttoken(const char *path) 283 { 284 const char *p; 285 286 p = path; 287 288 while (*p) { 289 if (smb_vss_is_gmttoken(p)) 290 return (p); 291 p++; 292 } 293 return (NULL); 294 } 295 296 static int 297 smb_vss_get_fsmountpath(smb_request_t *sr, char *buf, uint32_t buflen) 298 { 299 vnode_t *vp, *root_vp; 300 vfs_t *vfsp; 301 int err; 302 303 ASSERT(sr->tid_tree); 304 ASSERT(sr->tid_tree->t_snode); 305 ASSERT(sr->tid_tree->t_snode->vp); 306 ASSERT(sr->tid_tree->t_snode->vp->v_vfsp); 307 308 vp = sr->tid_tree->t_snode->vp; 309 vfsp = vp->v_vfsp; 310 311 if (VFS_ROOT(vfsp, &root_vp)) 312 return (ENOENT); 313 314 VN_HOLD(vp); 315 316 /* NULL is passed in as we want to start at "/" */ 317 err = vnodetopath(NULL, root_vp, buf, buflen, sr->user_cr); 318 319 VN_RELE(vp); 320 VN_RELE(root_vp); 321 return (err); 322 } 323 324 static uint32_t 325 smb_vss_encode_gmttokens(smb_request_t *sr, smb_xa_t *xa, 326 int32_t count, smb_dr_return_gmttokens_t *snap_data) 327 { 328 uint32_t i; 329 uint32_t returned_count; 330 uint32_t num_gmttokens; 331 char **gmttokens; 332 uint32_t err = SDRC_SUCCESS; 333 uint32_t data_size; 334 335 returned_count = snap_data->rg_count; 336 num_gmttokens = snap_data->rg_gmttokens.rg_gmttokens_len; 337 gmttokens = snap_data->rg_gmttokens.rg_gmttokens_val; 338 339 if (returned_count > count) { 340 err = NT_STATUS_BUFFER_TOO_SMALL; 341 } 342 343 data_size = returned_count * SMB_VSS_GMT_NET_SIZE(sr) + 344 smb_ascii_or_unicode_null_len(sr); 345 346 if (smb_mbc_encodef(&xa->rep_data_mb, "lll", returned_count, 347 num_gmttokens, data_size) != 0) { 348 smbsr_error(sr, 0, ERRSRV, ERRerror); 349 err = SDRC_ERROR; 350 } 351 352 if (err == SDRC_SUCCESS) { 353 for (i = 0; i < num_gmttokens; i++) { 354 if (smb_mbc_encodef(&xa->rep_data_mb, "%u", sr, 355 *gmttokens) != 0) { 356 smbsr_error(sr, 0, ERRSRV, ERRerror); 357 err = SDRC_ERROR; 358 } 359 gmttokens++; 360 } 361 } 362 363 return (err); 364 } 365 366 /* This removes the first @GMT from the path */ 367 static void 368 smb_vss_remove_first_token_from_path(char *path) 369 { 370 boolean_t found; 371 char *src, *dest; 372 373 src = path; 374 dest = path; 375 376 found = B_FALSE; 377 378 while (*src != '\0') { 379 if (!found && smb_vss_is_gmttoken(src)) { 380 src += SMB_VSS_GMT_SIZE - 1; 381 if (*src == '/') 382 src += 1; 383 found = B_TRUE; 384 continue; 385 } 386 *dest = *src; 387 src++; 388 dest++; 389 } 390 *dest = *src; 391 } 392