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 p = smb_vss_find_gmttoken(buf); 160 161 if (!p) 162 return (ENOENT); 163 164 bcopy(p, gmttoken, SMB_VSS_GMT_SIZE); 165 gmttoken[SMB_VSS_GMT_SIZE - 1] = '\0'; 166 167 (void) VFS_ROOT(sr->tid_tree->t_snode->vp->v_vfsp, &fsrootvp); 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 179 *snapname = '\0'; 180 181 smb_upcall_vss_map_gmttoken(rootpath, gmttoken, snapname); 182 183 if (!*snapname) { 184 err = ENOENT; 185 goto error; 186 } 187 188 /* note the value of root_node->vp */ 189 err = vnodetopath(fsrootvp, root_node->vp, nodepath, 190 MAXPATHLEN, kcred); 191 192 if (err != 0) 193 goto error; 194 195 (void) snprintf(rootpath, MAXPATHLEN, ".zfs/snapshot/%s/%s", 196 snapname, nodepath); 197 198 vp = smb_lookuppathvptovp(sr, rootpath, fsrootvp, fsrootvp); 199 200 if (vp) { 201 /* note the value of cur_node->vp */ 202 err = vnodetopath(fsrootvp, cur_node->vp, nodepath, 203 MAXPATHLEN, kcred); 204 if (err != 0) { 205 VN_RELE(vp); 206 goto error; 207 } 208 209 *vss_root_node = smb_node_lookup(sr, NULL, kcred, vp, 210 gmttoken, cur_node, NULL, &attr); 211 VN_RELE(vp); 212 213 if (*vss_root_node == NULL) { 214 err = ENOENT; 215 goto error; 216 } 217 218 (void) snprintf(rootpath, MAXPATHLEN, ".zfs/snapshot/%s/%s", 219 snapname, nodepath); 220 221 222 vp = smb_lookuppathvptovp(sr, rootpath, fsrootvp, fsrootvp); 223 224 if (vp) { 225 *vss_cur_node = smb_node_lookup(sr, NULL, kcred, vp, 226 gmttoken, cur_node, NULL, &attr); 227 VN_RELE(vp); 228 229 if (*vss_cur_node != NULL) { 230 smb_vss_remove_first_token_from_path(buf); 231 } else { 232 (void) smb_node_release(*vss_root_node); 233 err = ENOENT; 234 } 235 } else { 236 (void) smb_node_release(*vss_root_node); 237 err = ENOENT; 238 } 239 } else { 240 err = ENOENT; 241 } 242 243 error: 244 VN_RELE(fsrootvp); 245 kmem_free(rootpath, MAXPATHLEN); 246 kmem_free(snapname, MAXNAMELEN); 247 kmem_free(nodepath, MAXPATHLEN); 248 249 return (err); 250 } 251 252 253 static boolean_t 254 smb_vss_is_gmttoken(const char *s) 255 { 256 char *t = "@GMT-NNNN.NN.NN-NN.NN.NN"; 257 const char *str; 258 char *template; 259 260 template = t; 261 str = s; 262 263 while (*template) { 264 if (*template == 'N') { 265 if (!mts_isdigit(*str)) 266 return (B_FALSE); 267 } else if (*template != *str) { 268 return (B_FALSE); 269 } 270 271 template++; 272 str++; 273 } 274 275 /* Make sure it is JUST the @GMT token */ 276 if ((*str == '\0') || (*str == '/')) 277 return (B_TRUE); 278 279 return (B_FALSE); 280 } 281 282 static const char * 283 smb_vss_find_gmttoken(const char *path) 284 { 285 const char *p; 286 287 p = path; 288 289 while (*p) { 290 if (smb_vss_is_gmttoken(p)) 291 return (p); 292 p++; 293 } 294 return (NULL); 295 } 296 297 static int 298 smb_vss_get_fsmountpath(smb_request_t *sr, char *buf, uint32_t buflen) 299 { 300 vnode_t *vp, *root_vp; 301 vfs_t *vfsp; 302 int err; 303 304 ASSERT(sr->tid_tree); 305 ASSERT(sr->tid_tree->t_snode); 306 ASSERT(sr->tid_tree->t_snode->vp); 307 ASSERT(sr->tid_tree->t_snode->vp->v_vfsp); 308 309 vp = sr->tid_tree->t_snode->vp; 310 vfsp = vp->v_vfsp; 311 312 if (VFS_ROOT(vfsp, &root_vp)) 313 return (ENOENT); 314 315 VN_HOLD(vp); 316 317 /* NULL is passed in as we want to start at "/" */ 318 err = vnodetopath(NULL, root_vp, buf, buflen, sr->user_cr); 319 320 VN_RELE(vp); 321 VN_RELE(root_vp); 322 return (err); 323 } 324 325 static uint32_t 326 smb_vss_encode_gmttokens(smb_request_t *sr, smb_xa_t *xa, 327 int32_t count, smb_dr_return_gmttokens_t *snap_data) 328 { 329 uint32_t i; 330 uint32_t returned_count; 331 uint32_t num_gmttokens; 332 char **gmttokens; 333 uint32_t err = SDRC_SUCCESS; 334 uint32_t data_size; 335 336 returned_count = snap_data->rg_count; 337 num_gmttokens = snap_data->rg_gmttokens.rg_gmttokens_len; 338 gmttokens = snap_data->rg_gmttokens.rg_gmttokens_val; 339 340 if (returned_count > count) { 341 err = NT_STATUS_BUFFER_TOO_SMALL; 342 } 343 344 data_size = returned_count * SMB_VSS_GMT_NET_SIZE(sr) + 345 smb_ascii_or_unicode_null_len(sr); 346 347 if (smb_mbc_encodef(&xa->rep_data_mb, "lll", returned_count, 348 num_gmttokens, data_size) != 0) { 349 smbsr_error(sr, 0, ERRSRV, ERRerror); 350 err = SDRC_ERROR; 351 } 352 353 if (err == SDRC_SUCCESS) { 354 for (i = 0; i < num_gmttokens; i++) { 355 if (smb_mbc_encodef(&xa->rep_data_mb, "%u", sr, 356 *gmttokens) != 0) { 357 smbsr_error(sr, 0, ERRSRV, ERRerror); 358 err = SDRC_ERROR; 359 } 360 gmttokens++; 361 } 362 } 363 364 return (err); 365 } 366 367 /* This removes the first @GMT from the path */ 368 static void 369 smb_vss_remove_first_token_from_path(char *path) 370 { 371 boolean_t found; 372 char *src, *dest; 373 374 src = path; 375 dest = path; 376 377 found = B_FALSE; 378 379 while (*src != '\0') { 380 if (!found && smb_vss_is_gmttoken(src)) { 381 src += SMB_VSS_GMT_SIZE - 1; 382 if (*src == '/') 383 src += 1; 384 found = B_TRUE; 385 continue; 386 } 387 *dest = *src; 388 src++; 389 dest++; 390 } 391 *dest = *src; 392 } 393