1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Core SMB2 server
|
---|
4 |
|
---|
5 | Copyright (C) Stefan Metzmacher 2009
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 | #include "smbd/smbd.h"
|
---|
23 | #include "smbd/globals.h"
|
---|
24 | #include "../libcli/smb/smb_common.h"
|
---|
25 | #include "../libcli/security/security.h"
|
---|
26 | #include "auth.h"
|
---|
27 |
|
---|
28 | static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
|
---|
29 | const char *in_path,
|
---|
30 | uint8_t *out_share_type,
|
---|
31 | uint32_t *out_share_flags,
|
---|
32 | uint32_t *out_capabilities,
|
---|
33 | uint32_t *out_maximal_access,
|
---|
34 | uint32_t *out_tree_id);
|
---|
35 |
|
---|
36 | NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req)
|
---|
37 | {
|
---|
38 | const uint8_t *inbody;
|
---|
39 | int i = req->current_idx;
|
---|
40 | uint8_t *outhdr;
|
---|
41 | DATA_BLOB outbody;
|
---|
42 | uint16_t in_path_offset;
|
---|
43 | uint16_t in_path_length;
|
---|
44 | DATA_BLOB in_path_buffer;
|
---|
45 | char *in_path_string;
|
---|
46 | size_t in_path_string_size;
|
---|
47 | uint8_t out_share_type = 0;
|
---|
48 | uint32_t out_share_flags = 0;
|
---|
49 | uint32_t out_capabilities = 0;
|
---|
50 | uint32_t out_maximal_access = 0;
|
---|
51 | uint32_t out_tree_id = 0;
|
---|
52 | NTSTATUS status;
|
---|
53 | bool ok;
|
---|
54 |
|
---|
55 | status = smbd_smb2_request_verify_sizes(req, 0x09);
|
---|
56 | if (!NT_STATUS_IS_OK(status)) {
|
---|
57 | return smbd_smb2_request_error(req, status);
|
---|
58 | }
|
---|
59 | inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
|
---|
60 |
|
---|
61 | in_path_offset = SVAL(inbody, 0x04);
|
---|
62 | in_path_length = SVAL(inbody, 0x06);
|
---|
63 |
|
---|
64 | if (in_path_offset != (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
|
---|
65 | return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (in_path_length > req->in.vector[i+2].iov_len) {
|
---|
69 | return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
|
---|
70 | }
|
---|
71 |
|
---|
72 | in_path_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
|
---|
73 | in_path_buffer.length = in_path_length;
|
---|
74 |
|
---|
75 | ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
|
---|
76 | in_path_buffer.data,
|
---|
77 | in_path_buffer.length,
|
---|
78 | &in_path_string,
|
---|
79 | &in_path_string_size, false);
|
---|
80 | if (!ok) {
|
---|
81 | return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (in_path_buffer.length == 0) {
|
---|
85 | in_path_string_size = 0;
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (strlen(in_path_string) != in_path_string_size) {
|
---|
89 | return smbd_smb2_request_error(req, NT_STATUS_BAD_NETWORK_NAME);
|
---|
90 | }
|
---|
91 |
|
---|
92 | status = smbd_smb2_tree_connect(req, in_path_string,
|
---|
93 | &out_share_type,
|
---|
94 | &out_share_flags,
|
---|
95 | &out_capabilities,
|
---|
96 | &out_maximal_access,
|
---|
97 | &out_tree_id);
|
---|
98 | if (!NT_STATUS_IS_OK(status)) {
|
---|
99 | return smbd_smb2_request_error(req, status);
|
---|
100 | }
|
---|
101 |
|
---|
102 | outhdr = (uint8_t *)req->out.vector[i].iov_base;
|
---|
103 |
|
---|
104 | outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
|
---|
105 | if (outbody.data == NULL) {
|
---|
106 | return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
|
---|
107 | }
|
---|
108 |
|
---|
109 | SIVAL(outhdr, SMB2_HDR_TID, out_tree_id);
|
---|
110 |
|
---|
111 | SSVAL(outbody.data, 0x00, 0x10); /* struct size */
|
---|
112 | SCVAL(outbody.data, 0x02,
|
---|
113 | out_share_type); /* share type */
|
---|
114 | SCVAL(outbody.data, 0x03, 0); /* reserved */
|
---|
115 | SIVAL(outbody.data, 0x04,
|
---|
116 | out_share_flags); /* share flags */
|
---|
117 | SIVAL(outbody.data, 0x08,
|
---|
118 | out_capabilities); /* capabilities */
|
---|
119 | SIVAL(outbody.data, 0x0C,
|
---|
120 | out_maximal_access); /* maximal access */
|
---|
121 |
|
---|
122 | return smbd_smb2_request_done(req, outbody, NULL);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static int smbd_smb2_tcon_destructor(struct smbd_smb2_tcon *tcon)
|
---|
126 | {
|
---|
127 | if (tcon->session == NULL) {
|
---|
128 | return 0;
|
---|
129 | }
|
---|
130 |
|
---|
131 | idr_remove(tcon->session->tcons.idtree, tcon->tid);
|
---|
132 | DLIST_REMOVE(tcon->session->tcons.list, tcon);
|
---|
133 | SMB_ASSERT(tcon->session->sconn->num_tcons_open > 0);
|
---|
134 | tcon->session->sconn->num_tcons_open--;
|
---|
135 |
|
---|
136 | if (tcon->compat_conn) {
|
---|
137 | set_current_service(tcon->compat_conn, 0, true);
|
---|
138 | close_cnum(tcon->compat_conn, tcon->session->vuid);
|
---|
139 | }
|
---|
140 |
|
---|
141 | tcon->compat_conn = NULL;
|
---|
142 | tcon->tid = 0;
|
---|
143 | tcon->session = NULL;
|
---|
144 |
|
---|
145 | return 0;
|
---|
146 | }
|
---|
147 |
|
---|
148 | static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
|
---|
149 | const char *in_path,
|
---|
150 | uint8_t *out_share_type,
|
---|
151 | uint32_t *out_share_flags,
|
---|
152 | uint32_t *out_capabilities,
|
---|
153 | uint32_t *out_maximal_access,
|
---|
154 | uint32_t *out_tree_id)
|
---|
155 | {
|
---|
156 | const char *share = in_path;
|
---|
157 | char *service = NULL;
|
---|
158 | int snum = -1;
|
---|
159 | struct smbd_smb2_tcon *tcon;
|
---|
160 | connection_struct *compat_conn = NULL;
|
---|
161 | user_struct *compat_vuser = req->session->compat_vuser;
|
---|
162 | int id;
|
---|
163 | NTSTATUS status;
|
---|
164 |
|
---|
165 | if (strncmp(share, "\\\\", 2) == 0) {
|
---|
166 | const char *p = strchr(share+2, '\\');
|
---|
167 | if (p) {
|
---|
168 | share = p + 1;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
|
---|
173 | in_path, share));
|
---|
174 |
|
---|
175 | service = talloc_strdup(talloc_tos(), share);
|
---|
176 | if(!service) {
|
---|
177 | return NT_STATUS_NO_MEMORY;
|
---|
178 | }
|
---|
179 |
|
---|
180 | strlower_m(service);
|
---|
181 |
|
---|
182 | /* TODO: do more things... */
|
---|
183 | if (strequal(service,HOMES_NAME)) {
|
---|
184 | if (compat_vuser->homes_snum == -1) {
|
---|
185 | DEBUG(2, ("[homes] share not available for "
|
---|
186 | "user %s because it was not found "
|
---|
187 | "or created at session setup "
|
---|
188 | "time\n",
|
---|
189 | compat_vuser->session_info->unix_name));
|
---|
190 | return NT_STATUS_BAD_NETWORK_NAME;
|
---|
191 | }
|
---|
192 | snum = compat_vuser->homes_snum;
|
---|
193 | } else if ((compat_vuser->homes_snum != -1)
|
---|
194 | && strequal(service,
|
---|
195 | lp_servicename(compat_vuser->homes_snum))) {
|
---|
196 | snum = compat_vuser->homes_snum;
|
---|
197 | } else {
|
---|
198 | snum = find_service(talloc_tos(), service, &service);
|
---|
199 | if (!service) {
|
---|
200 | return NT_STATUS_NO_MEMORY;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (snum < 0) {
|
---|
205 | DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
|
---|
206 | service));
|
---|
207 | return NT_STATUS_BAD_NETWORK_NAME;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /* Don't allow connection if encryption is required. */
|
---|
211 | if (lp_smb_encrypt(snum) == Required) {
|
---|
212 | DEBUG(0,("Connection refused on share %s as encryption is"
|
---|
213 | " required on this share and SMB2 does not support"
|
---|
214 | " this.\n",
|
---|
215 | lp_servicename(snum)));
|
---|
216 | return NT_STATUS_ACCESS_DENIED;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /* create a new tcon as child of the session */
|
---|
220 | tcon = talloc_zero(req->session, struct smbd_smb2_tcon);
|
---|
221 | if (tcon == NULL) {
|
---|
222 | return NT_STATUS_NO_MEMORY;
|
---|
223 | }
|
---|
224 | id = idr_get_new_random(req->session->tcons.idtree,
|
---|
225 | tcon,
|
---|
226 | req->session->tcons.limit);
|
---|
227 | if (id == -1) {
|
---|
228 | TALLOC_FREE(tcon);
|
---|
229 | return NT_STATUS_INSUFFICIENT_RESOURCES;
|
---|
230 | }
|
---|
231 | tcon->tid = id;
|
---|
232 | tcon->snum = snum;
|
---|
233 |
|
---|
234 | DLIST_ADD_END(req->session->tcons.list, tcon,
|
---|
235 | struct smbd_smb2_tcon *);
|
---|
236 | tcon->session = req->session;
|
---|
237 | tcon->session->sconn->num_tcons_open++;
|
---|
238 | talloc_set_destructor(tcon, smbd_smb2_tcon_destructor);
|
---|
239 |
|
---|
240 | compat_conn = make_connection_smb2(req->sconn,
|
---|
241 | tcon,
|
---|
242 | req->session->compat_vuser,
|
---|
243 | data_blob_null, "???",
|
---|
244 | &status);
|
---|
245 | if (compat_conn == NULL) {
|
---|
246 | TALLOC_FREE(tcon);
|
---|
247 | return status;
|
---|
248 | }
|
---|
249 | tcon->compat_conn = talloc_move(tcon, &compat_conn);
|
---|
250 |
|
---|
251 | if (IS_PRINT(tcon->compat_conn)) {
|
---|
252 | *out_share_type = SMB2_SHARE_TYPE_PRINT;
|
---|
253 | } else if (IS_IPC(tcon->compat_conn)) {
|
---|
254 | *out_share_type = SMB2_SHARE_TYPE_PIPE;
|
---|
255 | } else {
|
---|
256 | *out_share_type = SMB2_SHARE_TYPE_DISK;
|
---|
257 | }
|
---|
258 |
|
---|
259 | *out_share_flags = SMB2_SHAREFLAG_ALLOW_NAMESPACE_CACHING;
|
---|
260 |
|
---|
261 | if (lp_msdfs_root(SNUM(tcon->compat_conn)) && lp_host_msdfs()) {
|
---|
262 | *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
|
---|
263 | *out_capabilities = SMB2_SHARE_CAP_DFS;
|
---|
264 | } else {
|
---|
265 | *out_capabilities = 0;
|
---|
266 | }
|
---|
267 |
|
---|
268 | switch(lp_csc_policy(SNUM(tcon->compat_conn))) {
|
---|
269 | case CSC_POLICY_MANUAL:
|
---|
270 | break;
|
---|
271 | case CSC_POLICY_DOCUMENTS:
|
---|
272 | *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
|
---|
273 | break;
|
---|
274 | case CSC_POLICY_PROGRAMS:
|
---|
275 | *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
|
---|
276 | break;
|
---|
277 | case CSC_POLICY_DISABLE:
|
---|
278 | *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
|
---|
279 | break;
|
---|
280 | default:
|
---|
281 | break;
|
---|
282 | }
|
---|
283 |
|
---|
284 | *out_maximal_access = tcon->compat_conn->share_access;
|
---|
285 |
|
---|
286 | *out_tree_id = tcon->tid;
|
---|
287 | return NT_STATUS_OK;
|
---|
288 | }
|
---|
289 |
|
---|
290 | NTSTATUS smbd_smb2_request_check_tcon(struct smbd_smb2_request *req)
|
---|
291 | {
|
---|
292 | const uint8_t *inhdr;
|
---|
293 | int i = req->current_idx;
|
---|
294 | uint32_t in_flags;
|
---|
295 | uint32_t in_tid;
|
---|
296 | void *p;
|
---|
297 | struct smbd_smb2_tcon *tcon;
|
---|
298 |
|
---|
299 | req->tcon = NULL;
|
---|
300 |
|
---|
301 | inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
|
---|
302 |
|
---|
303 | in_flags = IVAL(inhdr, SMB2_HDR_FLAGS);
|
---|
304 | in_tid = IVAL(inhdr, SMB2_HDR_TID);
|
---|
305 |
|
---|
306 | if (in_flags & SMB2_HDR_FLAG_CHAINED) {
|
---|
307 | in_tid = req->last_tid;
|
---|
308 | }
|
---|
309 |
|
---|
310 | req->last_tid = UINT32_MAX;
|
---|
311 |
|
---|
312 | /* lookup an existing session */
|
---|
313 | p = idr_find(req->session->tcons.idtree, in_tid);
|
---|
314 | if (p == NULL) {
|
---|
315 | return NT_STATUS_NETWORK_NAME_DELETED;
|
---|
316 | }
|
---|
317 | tcon = talloc_get_type_abort(p, struct smbd_smb2_tcon);
|
---|
318 |
|
---|
319 | if (!change_to_user(tcon->compat_conn,req->session->vuid)) {
|
---|
320 | return NT_STATUS_ACCESS_DENIED;
|
---|
321 | }
|
---|
322 |
|
---|
323 | /* should we pass FLAG_CASELESS_PATHNAMES here? */
|
---|
324 | if (!set_current_service(tcon->compat_conn, 0, true)) {
|
---|
325 | return NT_STATUS_ACCESS_DENIED;
|
---|
326 | }
|
---|
327 |
|
---|
328 | req->tcon = tcon;
|
---|
329 | req->last_tid = in_tid;
|
---|
330 |
|
---|
331 | return NT_STATUS_OK;
|
---|
332 | }
|
---|
333 |
|
---|
334 | NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
|
---|
335 | {
|
---|
336 | NTSTATUS status;
|
---|
337 | DATA_BLOB outbody;
|
---|
338 |
|
---|
339 | status = smbd_smb2_request_verify_sizes(req, 0x04);
|
---|
340 | if (!NT_STATUS_IS_OK(status)) {
|
---|
341 | return smbd_smb2_request_error(req, status);
|
---|
342 | }
|
---|
343 |
|
---|
344 | /*
|
---|
345 | * TODO: cancel all outstanding requests on the tcon
|
---|
346 | * and delete all file handles.
|
---|
347 | */
|
---|
348 | TALLOC_FREE(req->tcon);
|
---|
349 |
|
---|
350 | outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
|
---|
351 | if (outbody.data == NULL) {
|
---|
352 | return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
|
---|
353 | }
|
---|
354 |
|
---|
355 | SSVAL(outbody.data, 0x00, 0x04); /* struct size */
|
---|
356 | SSVAL(outbody.data, 0x02, 0); /* reserved */
|
---|
357 |
|
---|
358 | return smbd_smb2_request_done(req, outbody, NULL);
|
---|
359 | }
|
---|