xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_vss.c (revision 99abc82310f6fbd4f04276f195f54d54f458be33)
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 2015 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 <smbsrv/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_node_t *tnode;
82 	smb_gmttoken_response_t snaps;
83 
84 	ASSERT(sr->tid_tree);
85 	ASSERT(sr->tid_tree->t_snode);
86 
87 	if (fsctl->MaxOutputResp < SMB_VSS_COUNT_SIZE)
88 		return (NT_STATUS_INVALID_PARAMETER);
89 
90 	tnode = sr->tid_tree->t_snode;
91 	root_path  = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
92 	if (smb_node_getmntpath(tnode, root_path, MAXPATHLEN) != 0)
93 		return (NT_STATUS_INVALID_PARAMETER);
94 
95 	if (fsctl->MaxOutputResp == SMB_VSS_COUNT_SIZE) {
96 		count = smb_vss_get_count(sr->tid_tree, root_path);
97 		if (smb_mbc_encodef(fsctl->out_mbc, "lllw", count, 0,
98 		    (count * SMB_VSS_GMT_NET_SIZE(sr) +
99 		    smb_ascii_or_unicode_null_len(sr)), 0) != 0) {
100 			status = NT_STATUS_INVALID_PARAMETER;
101 		}
102 	} else {
103 		count = fsctl->MaxOutputResp / SMB_VSS_GMT_NET_SIZE(sr);
104 
105 		smb_vss_get_snapshots(sr->tid_tree, root_path,
106 		    count, &snaps);
107 
108 		status = smb_vss_encode_gmttokens(sr, fsctl, count, &snaps);
109 
110 		smb_vss_get_snapshots_free(&snaps);
111 	}
112 
113 	kmem_free(root_path, MAXPATHLEN);
114 	return (status);
115 }
116 
117 /*
118  * sr - the request info, used to find root of dataset,
119  *      unicode or ascii, where the share is rooted in the
120  *      dataset
121  * root_node - root of the share
122  * cur_node - where in the share for the command
123  * buf - is the path for the command to be processed
124  *       returned without @GMT if processed
125  * vss_cur_node - returned value for the snapshot version
126  *                of the cur_node
127  * vss_root_node - returned value for the snapshot version
128  *                 of the root_node
129  *
130  * This routine is the processing for handling the
131  * SMB_FLAGS2_REPARSE_PATH bit being set in the smb header.
132  *
133  * By using the cur_node passed in, a new node is found or
134  * created that is the same place in the directory tree, but
135  * in the snapshot. We also use root_node to do the same for
136  * the root.
137  * Once the new smb node is found, the path is modified by
138  * removing the @GMT token from the path in the buf.
139  */
140 int
141 smb_vss_lookup_nodes(smb_request_t *sr, smb_node_t *root_node,
142     smb_node_t *cur_node, char *buf, smb_node_t **vss_cur_node,
143     smb_node_t **vss_root_node)
144 {
145 	smb_arg_open_t	*op = &sr->arg.open;
146 	smb_node_t	*tnode;
147 	char		*snapname, *path;
148 	char		*gmttoken;
149 	char		gmttok_buf[SMB_VSS_GMT_SIZE];
150 	vnode_t		*fsrootvp = NULL;
151 	time_t		toktime;
152 	int		err = 0;
153 	boolean_t	smb1;
154 
155 	if (sr->tid_tree == NULL)
156 		return (ESTALE);
157 
158 	tnode = sr->tid_tree->t_snode;
159 
160 	ASSERT(tnode);
161 	ASSERT(tnode->vp);
162 	ASSERT(tnode->vp->v_vfsp);
163 
164 	smb1 = (sr->session->dialect < SMB_VERS_2_BASE);
165 	if (smb1) {
166 		const char *p;
167 
168 		/* get gmttoken from buf */
169 		if ((p = smb_vss_find_gmttoken(buf)) == NULL)
170 			return (ENOENT);
171 
172 		bcopy(p, gmttok_buf, SMB_VSS_GMT_SIZE);
173 		gmttok_buf[SMB_VSS_GMT_SIZE - 1] = '\0';
174 		gmttoken = gmttok_buf;
175 		toktime = 0;
176 	} else {
177 		/* SMB2 and later */
178 		gmttoken = NULL;
179 		toktime = op->timewarp.tv_sec;
180 	}
181 
182 	path = smb_srm_alloc(sr, MAXPATHLEN);
183 	snapname = smb_srm_alloc(sr, MAXPATHLEN);
184 
185 	err = smb_node_getmntpath(tnode, path, MAXPATHLEN);
186 	if (err != 0)
187 		return (err);
188 
189 	/*
190 	 * Find the corresponding snapshot name.  If snapname is
191 	 * empty after the map call, no such snapshot was found.
192 	 */
193 	*snapname = '\0';
194 	smb_vss_map_gmttoken(sr->tid_tree, path, gmttoken, toktime,
195 	    snapname);
196 	if (*snapname == '\0')
197 		return (ENOENT);
198 
199 	/* find snapshot nodes */
200 	err = VFS_ROOT(tnode->vp->v_vfsp, &fsrootvp);
201 	if (err != 0)
202 		return (err);
203 
204 	/* find snapshot node corresponding to root_node */
205 	err = smb_vss_lookup_node(sr, root_node, fsrootvp,
206 	    snapname, cur_node, vss_root_node);
207 	if (err == 0) {
208 		/* find snapshot node corresponding to cur_node */
209 		err = smb_vss_lookup_node(sr, cur_node, fsrootvp,
210 		    snapname, cur_node, vss_cur_node);
211 		if (err != 0)
212 			smb_node_release(*vss_root_node);
213 	}
214 
215 	VN_RELE(fsrootvp);
216 
217 	if (smb1)
218 		smb_vss_remove_first_token_from_path(buf);
219 
220 	return (err);
221 }
222 
223 /*
224  * Find snapshot node corresponding to 'node', and return it in
225  * 'vss_node', as follows:
226  * - find the path from fsrootvp to node, appending it to the
227  *   the snapshot path
228  * - lookup the vnode and smb_node (vss_node).
229  */
230 static int
231 smb_vss_lookup_node(smb_request_t *sr, smb_node_t *node, vnode_t *fsrootvp,
232     char *snapname, smb_node_t *dnode, smb_node_t **vss_node)
233 {
234 	char *p, *path;
235 	int err, len;
236 	vnode_t *vp = NULL;
237 
238 	*vss_node = NULL;
239 
240 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
241 	(void) snprintf(path, MAXPATHLEN, ".zfs/snapshot/%s/", snapname);
242 	len = strlen(path);
243 	p = path + len;
244 
245 	err = smb_node_getpath(node, fsrootvp, p, MAXPATHLEN - len);
246 	if (err == 0) {
247 		vp = smb_lookuppathvptovp(sr, path, fsrootvp, fsrootvp);
248 		if (vp) {
249 			*vss_node = smb_node_lookup(sr, NULL, zone_kcred(),
250 			    vp, snapname, dnode, NULL);
251 			VN_RELE(vp);
252 		}
253 	}
254 
255 	kmem_free(path, MAXPATHLEN);
256 
257 	if (*vss_node != NULL)
258 		return (0);
259 
260 	return (err ? err : ENOENT);
261 }
262 
263 
264 static boolean_t
265 smb_vss_is_gmttoken(const char *s)
266 {
267 	char *t = "@GMT-NNNN.NN.NN-NN.NN.NN";
268 	const char *str;
269 	char *template;
270 
271 	template = t;
272 	str = s;
273 
274 	while (*template) {
275 		if (*template == 'N') {
276 			if (!smb_isdigit(*str))
277 				return (B_FALSE);
278 		} else if (*template != *str) {
279 			return (B_FALSE);
280 		}
281 
282 		template++;
283 		str++;
284 	}
285 
286 	/* Make sure it is JUST the @GMT token */
287 	if ((*str == '\0') || (*str == '/'))
288 		return (B_TRUE);
289 
290 	return (B_FALSE);
291 }
292 
293 static const char *
294 smb_vss_find_gmttoken(const char *path)
295 {
296 	const char *p;
297 
298 	p = path;
299 
300 	while (*p) {
301 		if (*p == '@' && smb_vss_is_gmttoken(p))
302 			return (p);
303 		p++;
304 	}
305 	return (NULL);
306 }
307 
308 static uint32_t
309 smb_vss_encode_gmttokens(smb_request_t *sr, smb_fsctl_t *fsctl,
310     int32_t count, smb_gmttoken_response_t *snap_data)
311 {
312 	uint32_t i;
313 	uint32_t returned_count;
314 	uint32_t num_gmttokens;
315 	char **gmttokens;
316 	uint32_t status = NT_STATUS_SUCCESS;
317 	uint32_t data_size;
318 
319 	returned_count = snap_data->gtr_count;
320 	num_gmttokens = snap_data->gtr_gmttokens.gtr_gmttokens_len;
321 	gmttokens = snap_data->gtr_gmttokens.gtr_gmttokens_val;
322 
323 	if (returned_count > count)
324 		status = NT_STATUS_BUFFER_TOO_SMALL;
325 
326 	data_size = returned_count * SMB_VSS_GMT_NET_SIZE(sr) +
327 	    smb_ascii_or_unicode_null_len(sr);
328 
329 	if (smb_mbc_encodef(fsctl->out_mbc, "lll", returned_count,
330 	    num_gmttokens, data_size) != 0)
331 		return (NT_STATUS_INVALID_PARAMETER);
332 
333 	if (status == NT_STATUS_SUCCESS) {
334 		for (i = 0; i < num_gmttokens; i++) {
335 			if (smb_mbc_encodef(fsctl->out_mbc, "%u", sr,
336 			    *gmttokens) != 0)
337 				status = NT_STATUS_INVALID_PARAMETER;
338 			gmttokens++;
339 		}
340 	}
341 
342 	return (status);
343 }
344 
345 /* This removes the first @GMT from the path */
346 static void
347 smb_vss_remove_first_token_from_path(char *path)
348 {
349 	boolean_t found;
350 	char *src, *dest;
351 
352 	src = path;
353 	dest = path;
354 
355 	found = B_FALSE;
356 
357 	while (*src != '\0') {
358 		if (!found && smb_vss_is_gmttoken(src)) {
359 			src += SMB_VSS_GMT_SIZE - 1;
360 			if (*src == '/')
361 				src += 1;
362 			found = B_TRUE;
363 			continue;
364 		}
365 		*dest = *src;
366 		src++;
367 		dest++;
368 	}
369 	*dest = *src;
370 }
371 
372 /*
373  * This returns the number of snapshots for the dataset
374  * of the path provided.
375  */
376 static uint32_t
377 smb_vss_get_count(smb_tree_t *tree, char *resource_path)
378 {
379 	uint32_t	count = 0;
380 	int		rc;
381 	smb_string_t	path;
382 
383 	path.buf = resource_path;
384 
385 	rc = smb_kdoor_upcall(tree->t_server, SMB_DR_VSS_GET_COUNT,
386 	    &path, smb_string_xdr, &count, xdr_uint32_t);
387 
388 	if (rc != 0)
389 		count = 0;
390 
391 	return (count);
392 }
393 
394 /*
395  * This takes a path for the root of the dataset and gets the counts of
396  * snapshots for that dataset and the list of @GMT tokens (one for each
397  * snapshot) up to the count provided.
398  *
399  * Call smb_vss_get_snapshots_free after to free up the data.
400  */
401 static void
402 smb_vss_get_snapshots(smb_tree_t *tree, char *resource_path,
403     uint32_t count, smb_gmttoken_response_t *gmttokens)
404 {
405 	smb_gmttoken_query_t	request;
406 
407 	request.gtq_count = count;
408 	request.gtq_path = resource_path;
409 	bzero(gmttokens, sizeof (smb_gmttoken_response_t));
410 
411 	(void) smb_kdoor_upcall(tree->t_server, SMB_DR_VSS_GET_SNAPSHOTS,
412 	    &request, smb_gmttoken_query_xdr,
413 	    gmttokens, smb_gmttoken_response_xdr);
414 }
415 
416 static void
417 smb_vss_get_snapshots_free(smb_gmttoken_response_t *reply)
418 {
419 	xdr_free(smb_gmttoken_response_xdr, (char *)reply);
420 }
421 
422 /*
423  * Returns the snapshot name for the @GMT token provided for the dataset
424  * of the path.  If the snapshot cannot be found, a string with a NULL
425  * is returned.
426  */
427 static void
428 smb_vss_map_gmttoken(smb_tree_t *tree, char *path, char *gmttoken,
429 	time_t toktime, char *snapname)
430 {
431 	smb_gmttoken_snapname_t	request;
432 	smb_string_t		result;
433 
434 	bzero(&result, sizeof (smb_string_t));
435 	result.buf = snapname;
436 
437 	request.gts_path = path;
438 	request.gts_gmttoken = gmttoken;
439 	request.gts_toktime = toktime;
440 
441 	(void) smb_kdoor_upcall(tree->t_server, SMB_DR_VSS_MAP_GMTTOKEN,
442 	    &request, smb_gmttoken_snapname_xdr,
443 	    &result, smb_string_xdr);
444 }
445