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