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