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
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 }
|
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 */
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 <smb/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_gmttoken_response_t snaps;
82
83 ASSERT(sr->fid_ofile);
84 ASSERT(sr->fid_ofile->f_node);
85
86 if (fsctl->MaxOutputResp < SMB_VSS_COUNT_SIZE)
87 return (NT_STATUS_INVALID_PARAMETER);
88
89 /*
90 * smbd will find the root of the lowest filesystem from mntpath of a
91 * file by comparing it agaisnt mnttab, repeatedly removing components
92 * until one matches.
93 */
94 root_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
95 if (smb_node_getmntpath(sr->fid_ofile->f_node, root_path,
96 MAXPATHLEN) != 0)
97 return (NT_STATUS_INVALID_PARAMETER);
98
99 if (fsctl->MaxOutputResp == SMB_VSS_COUNT_SIZE) {
100 count = smb_vss_get_count(sr->tid_tree, root_path);
101 if (smb_mbc_encodef(fsctl->out_mbc, "lllw", count, 0,
102 (count * SMB_VSS_GMT_NET_SIZE(sr) +
103 smb_ascii_or_unicode_null_len(sr)), 0) != 0) {
104 status = NT_STATUS_INVALID_PARAMETER;
105 }
106 } else {
107 count = fsctl->MaxOutputResp / SMB_VSS_GMT_NET_SIZE(sr);
108
109 smb_vss_get_snapshots(sr->tid_tree, root_path,
110 count, &snaps);
111
112 status = smb_vss_encode_gmttokens(sr, fsctl, count, &snaps);
113
114 smb_vss_get_snapshots_free(&snaps);
115 }
116
117 kmem_free(root_path, MAXPATHLEN);
118 return (status);
119 }
120
121 /*
122 * sr - the request info, used to find root of dataset,
123 * unicode or ascii, where the share is rooted in the
124 * dataset
125 * cur_node - where in the share for the command
126 * vss_cur_node - returned value for the snapshot version
127 * of the cur_node
128 * gmttoken - if SMB1, the gmttoken to be used to find the snapshot.
129 * Otherwise, NULL.
130 *
131 * This routine is the processing for handling the
132 * SMB_FLAGS2_REPARSE_PATH bit being set in the smb header.
133 *
134 * By using the cur_node passed in, a new node is found or
135 * created that is the same place in the directory tree, but
136 * in the snapshot.
137 */
138 int
139 smb_vss_lookup_nodes(smb_request_t *sr, smb_node_t *cur_node,
140 smb_node_t **vss_cur_node, char *gmttoken)
141 {
142 smb_arg_open_t *op = &sr->arg.open;
143 char *snapname, *path;
144 vnode_t *fsrootvp = NULL;
145 time_t toktime;
146 int err = 0;
147
148 if (sr->tid_tree == NULL)
149 return (ESTALE);
150
151 if (gmttoken != NULL) {
152 toktime = 0;
153 } else {
154 /* SMB2 and later */
155 toktime = op->timewarp.tv_sec;
156 }
157
158 path = smb_srm_alloc(sr, MAXPATHLEN);
159 snapname = smb_srm_alloc(sr, MAXPATHLEN);
160
161 err = smb_node_getmntpath(cur_node, path, MAXPATHLEN);
162 if (err != 0)
163 return (err);
164
165 /*
166 * Find the corresponding snapshot name. If snapname is
167 * empty after the map call, no such snapshot was found.
168 */
169 *snapname = '\0';
170 smb_vss_map_gmttoken(sr->tid_tree, path, gmttoken, toktime,
171 snapname);
172 if (*snapname == '\0')
173 return (ENOENT);
174
175 /* find snapshot nodes */
176 err = VFS_ROOT(cur_node->vp->v_vfsp, &fsrootvp);
177 if (err != 0)
178 return (err);
179
180 /* find snapshot node corresponding to cur_node */
181 err = smb_vss_lookup_node(sr, cur_node, fsrootvp,
182 snapname, cur_node, vss_cur_node);
183
184 VN_RELE(fsrootvp);
185
186 return (err);
187 }
188
189 /*
190 * Find snapshot node corresponding to 'node', and return it in
191 * 'vss_node', as follows:
192 * - find the path from fsrootvp to node, appending it to the
193 * the snapshot path
194 * - lookup the vnode and smb_node (vss_node).
195 */
196 static int
197 smb_vss_lookup_node(smb_request_t *sr, smb_node_t *node, vnode_t *fsrootvp,
198 char *snapname, smb_node_t *dnode, smb_node_t **vss_node)
199 {
200 char *p, *path;
201 int err, len;
202 vnode_t *vp = NULL;
203
204 *vss_node = NULL;
205
390 * of the path. If the snapshot cannot be found, a string with a NULL
391 * is returned.
392 */
393 static void
394 smb_vss_map_gmttoken(smb_tree_t *tree, char *path, char *gmttoken,
395 time_t toktime, char *snapname)
396 {
397 smb_gmttoken_snapname_t request;
398 smb_string_t result;
399
400 bzero(&result, sizeof (smb_string_t));
401 result.buf = snapname;
402
403 request.gts_path = path;
404 request.gts_gmttoken = gmttoken;
405 request.gts_toktime = toktime;
406
407 (void) smb_kdoor_upcall(tree->t_server, SMB_DR_VSS_MAP_GMTTOKEN,
408 &request, smb_gmttoken_snapname_xdr,
409 &result, smb_string_xdr);
410 }
411
412 int
413 smb_vss_extract_gmttoken(char *buf, char *gmttoken)
414 {
415 const char *p;
416
417 /* get gmttoken from buf */
418 if ((p = smb_vss_find_gmttoken(buf)) == NULL)
419 return (ENOENT);
420
421 bcopy(p, gmttoken, SMB_VSS_GMT_SIZE);
422 gmttoken[SMB_VSS_GMT_SIZE - 1] = '\0';
423
424 smb_vss_remove_first_token_from_path(buf);
425
426 return (0);
427 }
|