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