xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_vss.c (revision fb2a9bae0030340ad72b9c26ba1ffee2ee3cafec)
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 2010 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_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_xa_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(char *);
60 static void smb_vss_map_gmttoken(char *, char *, char *);
61 static void smb_vss_get_snapshots(char *, uint32_t, smb_gmttoken_response_t *);
62 static void smb_vss_get_snapshots_free(smb_gmttoken_response_t *);
63 
64 /*
65  * This is to respond to the nt_transact_ioctl to either respond with the
66  * number of snapshots, or to respond with the list.  It needs to be sorted
67  * before the reply.  If the the max data bytes to return is
68  * SMB_VSS_COUNT_SIZE, then all that is requested is the count, otherwise
69  * return the count and the list of @GMT tokens (one token for each
70  * snapshot).
71  */
72 uint32_t
73 smb_vss_ioctl_enumerate_snaps(smb_request_t *sr, smb_xa_t *xa)
74 {
75 	uint32_t count = 0;
76 	char *root_path;
77 	uint32_t status = NT_STATUS_SUCCESS;
78 	smb_node_t *tnode;
79 	smb_gmttoken_response_t gmttokens;
80 
81 	ASSERT(sr->tid_tree);
82 	ASSERT(sr->tid_tree->t_snode);
83 
84 	if (xa->smb_mdrcnt < SMB_VSS_COUNT_SIZE)
85 		return (NT_STATUS_INVALID_PARAMETER);
86 
87 	tnode = sr->tid_tree->t_snode;
88 	root_path  = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
89 	if (smb_node_getmntpath(tnode, root_path, MAXPATHLEN) != 0)
90 		return (NT_STATUS_INVALID_PARAMETER);
91 
92 	if (xa->smb_mdrcnt == SMB_VSS_COUNT_SIZE) {
93 		count = smb_vss_get_count(root_path);
94 		if (smb_mbc_encodef(&xa->rep_data_mb, "lllw", count, 0,
95 		    (count * SMB_VSS_GMT_NET_SIZE(sr) +
96 		    smb_ascii_or_unicode_null_len(sr)), 0) != 0) {
97 			status = NT_STATUS_INVALID_PARAMETER;
98 		}
99 	} else {
100 		count = xa->smb_mdrcnt / SMB_VSS_GMT_NET_SIZE(sr);
101 
102 		smb_vss_get_snapshots(root_path, count, &gmttokens);
103 
104 		status = smb_vss_encode_gmttokens(sr, xa, count, &gmttokens);
105 
106 		smb_vss_get_snapshots_free(&gmttokens);
107 	}
108 
109 	kmem_free(root_path, MAXPATHLEN);
110 	return (status);
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 int
137 smb_vss_lookup_nodes(smb_request_t *sr, smb_node_t *root_node,
138     smb_node_t *cur_node, char *buf, smb_node_t **vss_cur_node,
139     smb_node_t **vss_root_node)
140 {
141 	const char *p;
142 	smb_node_t *tnode;
143 	char *rootpath;
144 	char *snapname;
145 	char *nodepath;
146 	char gmttoken[SMB_VSS_GMT_SIZE];
147 	vnode_t *fsrootvp;
148 	vnode_t *vp = NULL;
149 	int err = 0;
150 
151 	if (sr->tid_tree == NULL)
152 		return (ESTALE);
153 
154 	ASSERT(sr->tid_tree->t_snode);
155 	ASSERT(sr->tid_tree->t_snode->vp);
156 	ASSERT(sr->tid_tree->t_snode->vp->v_vfsp);
157 
158 	if ((p = smb_vss_find_gmttoken(buf)) == NULL)
159 		return (ENOENT);
160 
161 	bcopy(p, gmttoken, SMB_VSS_GMT_SIZE);
162 	gmttoken[SMB_VSS_GMT_SIZE - 1] = '\0';
163 
164 	tnode = sr->tid_tree->t_snode;
165 	err = VFS_ROOT(tnode->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_node_getmntpath(tnode, rootpath, MAXPATHLEN);
174 	if (err != 0)
175 		goto error;
176 
177 	*snapname = '\0';
178 
179 	smb_vss_map_gmttoken(rootpath, gmttoken, snapname);
180 
181 	if (!*snapname) {
182 		err = ENOENT;
183 		goto error;
184 	}
185 
186 	/* note the value of root_node->vp */
187 	err = vnodetopath(fsrootvp, root_node->vp, nodepath,
188 	    MAXPATHLEN, kcred);
189 
190 	if (err != 0)
191 		goto error;
192 
193 	(void) snprintf(rootpath, MAXPATHLEN, ".zfs/snapshot/%s/%s",
194 	    snapname, nodepath);
195 
196 	vp = smb_lookuppathvptovp(sr, rootpath, fsrootvp, fsrootvp);
197 
198 	if (vp) {
199 		/* note the value of cur_node->vp */
200 		err = vnodetopath(fsrootvp, cur_node->vp, nodepath,
201 		    MAXPATHLEN, kcred);
202 		if (err != 0) {
203 			VN_RELE(vp);
204 			goto error;
205 		}
206 
207 		*vss_root_node = smb_node_lookup(sr, NULL, kcred, vp,
208 		    gmttoken, cur_node, NULL);
209 		VN_RELE(vp);
210 
211 		if (*vss_root_node == NULL) {
212 			err = ENOENT;
213 			goto error;
214 		}
215 
216 		(void) snprintf(rootpath, MAXPATHLEN, ".zfs/snapshot/%s/%s",
217 		    snapname, nodepath);
218 
219 
220 		vp = smb_lookuppathvptovp(sr, rootpath, fsrootvp, fsrootvp);
221 
222 		if (vp) {
223 			*vss_cur_node = smb_node_lookup(sr, NULL, kcred, vp,
224 			    gmttoken, cur_node, NULL);
225 			VN_RELE(vp);
226 
227 			if (*vss_cur_node != NULL) {
228 				smb_vss_remove_first_token_from_path(buf);
229 			} else {
230 				(void) smb_node_release(*vss_root_node);
231 				err = ENOENT;
232 			}
233 		} else {
234 			(void) smb_node_release(*vss_root_node);
235 			err = ENOENT;
236 		}
237 	} else {
238 		err = ENOENT;
239 	}
240 
241 error:
242 	VN_RELE(fsrootvp);
243 	kmem_free(rootpath, MAXPATHLEN);
244 	kmem_free(snapname, MAXNAMELEN);
245 	kmem_free(nodepath, MAXPATHLEN);
246 
247 	return (err);
248 }
249 
250 static boolean_t
251 smb_vss_is_gmttoken(const char *s)
252 {
253 	char *t = "@GMT-NNNN.NN.NN-NN.NN.NN";
254 	const char *str;
255 	char *template;
256 
257 	template = t;
258 	str = s;
259 
260 	while (*template) {
261 		if (*template == 'N') {
262 			if (!smb_isdigit(*str))
263 				return (B_FALSE);
264 		} else if (*template != *str) {
265 			return (B_FALSE);
266 		}
267 
268 		template++;
269 		str++;
270 	}
271 
272 	/* Make sure it is JUST the @GMT token */
273 	if ((*str == '\0') || (*str == '/'))
274 		return (B_TRUE);
275 
276 	return (B_FALSE);
277 }
278 
279 static const char *
280 smb_vss_find_gmttoken(const char *path)
281 {
282 	const char *p;
283 
284 	p = path;
285 
286 	while (*p) {
287 		if (smb_vss_is_gmttoken(p))
288 			return (p);
289 		p++;
290 	}
291 	return (NULL);
292 }
293 
294 static uint32_t
295 smb_vss_encode_gmttokens(smb_request_t *sr, smb_xa_t *xa,
296     int32_t count, smb_gmttoken_response_t *snap_data)
297 {
298 	uint32_t i;
299 	uint32_t returned_count;
300 	uint32_t num_gmttokens;
301 	char **gmttokens;
302 	uint32_t status = NT_STATUS_SUCCESS;
303 	uint32_t data_size;
304 
305 	returned_count = snap_data->gtr_count;
306 	num_gmttokens = snap_data->gtr_gmttokens.gtr_gmttokens_len;
307 	gmttokens = snap_data->gtr_gmttokens.gtr_gmttokens_val;
308 
309 	if (returned_count > count)
310 		status = NT_STATUS_BUFFER_TOO_SMALL;
311 
312 	data_size = returned_count * SMB_VSS_GMT_NET_SIZE(sr) +
313 	    smb_ascii_or_unicode_null_len(sr);
314 
315 	if (smb_mbc_encodef(&xa->rep_data_mb, "lll", returned_count,
316 	    num_gmttokens, data_size) != 0)
317 		return (NT_STATUS_INVALID_PARAMETER);
318 
319 	if (status == NT_STATUS_SUCCESS) {
320 		for (i = 0; i < num_gmttokens; i++) {
321 			if (smb_mbc_encodef(&xa->rep_data_mb, "%u", sr,
322 			    *gmttokens) != 0)
323 				status = NT_STATUS_INVALID_PARAMETER;
324 			gmttokens++;
325 		}
326 	}
327 
328 	return (status);
329 }
330 
331 /* This removes the first @GMT from the path */
332 static void
333 smb_vss_remove_first_token_from_path(char *path)
334 {
335 	boolean_t found;
336 	char *src, *dest;
337 
338 	src = path;
339 	dest = path;
340 
341 	found = B_FALSE;
342 
343 	while (*src != '\0') {
344 		if (!found && smb_vss_is_gmttoken(src)) {
345 			src += SMB_VSS_GMT_SIZE - 1;
346 			if (*src == '/')
347 				src += 1;
348 			found = B_TRUE;
349 			continue;
350 		}
351 		*dest = *src;
352 		src++;
353 		dest++;
354 	}
355 	*dest = *src;
356 }
357 
358 /*
359  * This returns the number of snapshots for the dataset
360  * of the path provided.
361  */
362 static uint32_t
363 smb_vss_get_count(char *resource_path)
364 {
365 	uint32_t	count = 0;
366 	int		rc;
367 	smb_string_t	path;
368 
369 	path.buf = resource_path;
370 
371 	rc = smb_kdoor_upcall(SMB_DR_VSS_GET_COUNT,
372 	    &path, smb_string_xdr, &count, xdr_uint32_t);
373 
374 	if (rc != 0)
375 		count = 0;
376 
377 	return (count);
378 }
379 
380 /*
381  * This takes a path for the root of the dataset and gets the counts of
382  * snapshots for that dataset and the list of @GMT tokens (one for each
383  * snapshot) up to the count provided.
384  *
385  * Call smb_vss_get_snapshots_free after to free up the data.
386  */
387 static void
388 smb_vss_get_snapshots(char *resource_path, uint32_t count,
389     smb_gmttoken_response_t *gmttokens)
390 {
391 	smb_gmttoken_query_t	request;
392 
393 	request.gtq_count = count;
394 	request.gtq_path = resource_path;
395 	bzero(gmttokens, sizeof (smb_gmttoken_response_t));
396 
397 	(void) smb_kdoor_upcall(SMB_DR_VSS_GET_SNAPSHOTS,
398 	    &request, smb_gmttoken_query_xdr,
399 	    gmttokens, smb_gmttoken_response_xdr);
400 }
401 
402 static void
403 smb_vss_get_snapshots_free(smb_gmttoken_response_t *reply)
404 {
405 	xdr_free(smb_gmttoken_response_xdr, (char *)reply);
406 }
407 
408 /*
409  * Returns the snapshot name for the @GMT token provided for the dataset
410  * of the path.  If the snapshot cannot be found, a string with a NULL
411  * is returned.
412  */
413 static void
414 smb_vss_map_gmttoken(char *path, char *gmttoken, char *snapname)
415 {
416 	smb_gmttoken_snapname_t	request;
417 	smb_string_t		result;
418 
419 	bzero(&result, sizeof (smb_string_t));
420 	result.buf = snapname;
421 
422 	request.gts_path = path;
423 	request.gts_gmttoken = gmttoken;
424 
425 	(void) smb_kdoor_upcall(SMB_DR_VSS_MAP_GMTTOKEN,
426 	    &request, smb_gmttoken_snapname_xdr,
427 	    &result, smb_string_xdr);
428 }
429