2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
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.
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.
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/>.
22 #include "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
24 #include "../libcli/security/security.h"
26 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
28 uint8_t *out_share_type,
29 uint32_t *out_share_flags,
30 uint32_t *out_capabilities,
31 uint32_t *out_maximal_access,
32 uint32_t *out_tree_id);
34 NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req)
36 const uint8_t *inbody;
37 int i = req->current_idx;
40 size_t expected_body_size = 0x09;
42 uint16_t in_path_offset;
43 uint16_t in_path_length;
44 DATA_BLOB in_path_buffer;
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;
55 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
56 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
59 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
61 body_size = SVAL(inbody, 0x00);
62 if (body_size != expected_body_size) {
63 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
66 in_path_offset = SVAL(inbody, 0x04);
67 in_path_length = SVAL(inbody, 0x06);
69 if (in_path_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
70 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
73 if (in_path_length > req->in.vector[i+2].iov_len) {
74 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
77 in_path_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
78 in_path_buffer.length = in_path_length;
80 ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
82 in_path_buffer.length,
84 &in_path_string_size, false);
86 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
89 status = smbd_smb2_tree_connect(req, in_path_string,
95 if (!NT_STATUS_IS_OK(status)) {
96 return smbd_smb2_request_error(req, status);
99 outhdr = (uint8_t *)req->out.vector[i].iov_base;
101 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
102 if (outbody.data == NULL) {
103 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
106 SIVAL(outhdr, SMB2_HDR_TID, out_tree_id);
108 SSVAL(outbody.data, 0x00, 0x10); /* struct size */
109 SCVAL(outbody.data, 0x02,
110 out_share_type); /* share type */
111 SCVAL(outbody.data, 0x03, 0); /* reserved */
112 SIVAL(outbody.data, 0x04,
113 out_share_flags); /* share flags */
114 SIVAL(outbody.data, 0x08,
115 out_capabilities); /* capabilities */
116 SIVAL(outbody.data, 0x0C,
117 out_maximal_access); /* maximal access */
119 return smbd_smb2_request_done(req, outbody, NULL);
122 static int smbd_smb2_tcon_destructor(struct smbd_smb2_tcon *tcon)
124 if (tcon->session == NULL) {
128 idr_remove(tcon->session->tcons.idtree, tcon->tid);
129 DLIST_REMOVE(tcon->session->tcons.list, tcon);
130 SMB_ASSERT(tcon->session->sconn->num_tcons_open > 0);
131 tcon->session->sconn->num_tcons_open--;
133 if (tcon->compat_conn) {
134 set_current_service(tcon->compat_conn, 0, true);
135 close_cnum(tcon->compat_conn, tcon->session->vuid);
138 tcon->compat_conn = NULL;
140 tcon->session = NULL;
145 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
147 uint8_t *out_share_type,
148 uint32_t *out_share_flags,
149 uint32_t *out_capabilities,
150 uint32_t *out_maximal_access,
151 uint32_t *out_tree_id)
153 const char *share = in_path;
156 struct smbd_smb2_tcon *tcon;
157 connection_struct *compat_conn = NULL;
158 user_struct *compat_vuser = req->session->compat_vuser;
162 if (strncmp(share, "\\\\", 2) == 0) {
163 const char *p = strchr(share+2, '\\');
169 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
172 fstrcpy(service, share);
176 /* TODO: do more things... */
177 if (strequal(service,HOMES_NAME)) {
178 if (compat_vuser->homes_snum == -1) {
179 DEBUG(2, ("[homes] share not available for "
180 "user %s because it was not found "
181 "or created at session setup "
183 compat_vuser->server_info->unix_name));
184 return NT_STATUS_BAD_NETWORK_NAME;
186 snum = compat_vuser->homes_snum;
187 } else if ((compat_vuser->homes_snum != -1)
189 lp_servicename(compat_vuser->homes_snum))) {
190 snum = compat_vuser->homes_snum;
192 snum = find_service(service);
196 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
198 return NT_STATUS_BAD_NETWORK_NAME;
201 /* create a new tcon as child of the session */
202 tcon = talloc_zero(req->session, struct smbd_smb2_tcon);
204 return NT_STATUS_NO_MEMORY;
206 id = idr_get_new_random(req->session->tcons.idtree,
208 req->session->tcons.limit);
211 return NT_STATUS_INSUFFICIENT_RESOURCES;
216 DLIST_ADD_END(req->session->tcons.list, tcon,
217 struct smbd_smb2_tcon *);
218 tcon->session = req->session;
219 tcon->session->sconn->num_tcons_open++;
220 talloc_set_destructor(tcon, smbd_smb2_tcon_destructor);
222 compat_conn = make_connection_snum(req->sconn,
223 snum, req->session->compat_vuser,
224 data_blob_null, "???",
226 if (compat_conn == NULL) {
230 tcon->compat_conn = talloc_move(tcon, &compat_conn);
231 tcon->compat_conn->cnum = tcon->tid;
233 if (IS_PRINT(tcon->compat_conn)) {
234 *out_share_type = SMB2_SHARE_TYPE_PRINT;
235 } else if (IS_IPC(tcon->compat_conn)) {
236 *out_share_type = SMB2_SHARE_TYPE_PIPE;
238 *out_share_type = SMB2_SHARE_TYPE_DISK;
241 *out_share_flags = SMB2_SHAREFLAG_ALLOW_NAMESPACE_CACHING;
243 if (lp_msdfs_root(SNUM(tcon->compat_conn)) && lp_host_msdfs()) {
244 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
245 *out_capabilities = SMB2_SHARE_CAP_DFS;
247 *out_capabilities = 0;
250 switch(lp_csc_policy(SNUM(tcon->compat_conn))) {
251 case CSC_POLICY_MANUAL:
253 case CSC_POLICY_DOCUMENTS:
254 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
256 case CSC_POLICY_PROGRAMS:
257 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
259 case CSC_POLICY_DISABLE:
260 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
266 *out_maximal_access = FILE_GENERIC_ALL;
268 *out_tree_id = tcon->tid;
272 NTSTATUS smbd_smb2_request_check_tcon(struct smbd_smb2_request *req)
274 const uint8_t *inhdr;
275 const uint8_t *outhdr;
276 int i = req->current_idx;
279 struct smbd_smb2_tcon *tcon;
280 bool chained_fixup = false;
282 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
284 in_tid = IVAL(inhdr, SMB2_HDR_TID);
286 if (in_tid == (0xFFFFFFFF)) {
289 * async request - fill in tid from
290 * already setup out.vector[].iov_base.
292 outhdr = (const uint8_t *)req->out.vector[i].iov_base;
293 in_tid = IVAL(outhdr, SMB2_HDR_TID);
296 * Chained request - fill in tid from
297 * the previous request out.vector[].iov_base.
299 outhdr = (const uint8_t *)req->out.vector[i-3].iov_base;
300 in_tid = IVAL(outhdr, SMB2_HDR_TID);
301 chained_fixup = true;
305 /* lookup an existing session */
306 p = idr_find(req->session->tcons.idtree, in_tid);
308 return NT_STATUS_NETWORK_NAME_DELETED;
310 tcon = talloc_get_type_abort(p, struct smbd_smb2_tcon);
312 if (!change_to_user(tcon->compat_conn,req->session->vuid)) {
313 return NT_STATUS_ACCESS_DENIED;
316 /* should we pass FLAG_CASELESS_PATHNAMES here? */
317 if (!set_current_service(tcon->compat_conn, 0, true)) {
318 return NT_STATUS_ACCESS_DENIED;
324 /* Fix up our own outhdr. */
325 outhdr = (const uint8_t *)req->out.vector[i].iov_base;
326 SIVAL(outhdr, SMB2_HDR_TID, in_tid);
332 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
334 const uint8_t *inbody;
335 int i = req->current_idx;
337 size_t expected_body_size = 0x04;
340 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
341 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
344 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
346 body_size = SVAL(inbody, 0x00);
347 if (body_size != expected_body_size) {
348 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
352 * TODO: cancel all outstanding requests on the tcon
353 * and delete all file handles.
355 TALLOC_FREE(req->tcon);
357 outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
358 if (outbody.data == NULL) {
359 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
362 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
363 SSVAL(outbody.data, 0x02, 0); /* reserved */
365 return smbd_smb2_request_done(req, outbody, NULL);