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/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../libcli/security/security.h"
28 static NTSTATUS
smbd_smb2_tree_connect(struct smbd_smb2_request
*req
,
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
);
36 NTSTATUS
smbd_smb2_request_process_tcon(struct smbd_smb2_request
*req
)
38 const uint8_t *inbody
;
39 int i
= req
->current_idx
;
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 status
= smbd_smb2_request_verify_sizes(req
, 0x09);
56 if (!NT_STATUS_IS_OK(status
)) {
57 return smbd_smb2_request_error(req
, status
);
59 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
61 in_path_offset
= SVAL(inbody
, 0x04);
62 in_path_length
= SVAL(inbody
, 0x06);
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
);
68 if (in_path_length
> req
->in
.vector
[i
+2].iov_len
) {
69 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
72 in_path_buffer
.data
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
73 in_path_buffer
.length
= in_path_length
;
75 ok
= convert_string_talloc(req
, CH_UTF16
, CH_UNIX
,
77 in_path_buffer
.length
,
79 &in_path_string_size
, false);
81 return smbd_smb2_request_error(req
, NT_STATUS_ILLEGAL_CHARACTER
);
84 if (in_path_buffer
.length
== 0) {
85 in_path_string_size
= 0;
88 if (strlen(in_path_string
) != in_path_string_size
) {
89 return smbd_smb2_request_error(req
, NT_STATUS_BAD_NETWORK_NAME
);
92 status
= smbd_smb2_tree_connect(req
, in_path_string
,
98 if (!NT_STATUS_IS_OK(status
)) {
99 return smbd_smb2_request_error(req
, status
);
102 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
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
);
109 SIVAL(outhdr
, SMB2_HDR_TID
, out_tree_id
);
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 */
122 return smbd_smb2_request_done(req
, outbody
, NULL
);
125 static int smbd_smb2_tcon_destructor(struct smbd_smb2_tcon
*tcon
)
127 if (tcon
->session
== NULL
) {
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
--;
136 if (tcon
->compat_conn
) {
137 set_current_service(tcon
->compat_conn
, 0, true);
138 close_cnum(tcon
->compat_conn
, tcon
->session
->vuid
);
141 tcon
->compat_conn
= NULL
;
143 tcon
->session
= NULL
;
148 static NTSTATUS
smbd_smb2_tree_connect(struct smbd_smb2_request
*req
,
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
)
156 const char *share
= in_path
;
157 char *service
= NULL
;
159 struct smbd_smb2_tcon
*tcon
;
160 connection_struct
*compat_conn
= NULL
;
161 user_struct
*compat_vuser
= req
->session
->compat_vuser
;
165 if (strncmp(share
, "\\\\", 2) == 0) {
166 const char *p
= strchr(share
+2, '\\');
172 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
175 service
= talloc_strdup(talloc_tos(), share
);
177 return NT_STATUS_NO_MEMORY
;
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 "
189 compat_vuser
->session_info
->unix_name
));
190 return NT_STATUS_BAD_NETWORK_NAME
;
192 snum
= compat_vuser
->homes_snum
;
193 } else if ((compat_vuser
->homes_snum
!= -1)
195 lp_servicename(compat_vuser
->homes_snum
))) {
196 snum
= compat_vuser
->homes_snum
;
198 snum
= find_service(talloc_tos(), service
, &service
);
200 return NT_STATUS_NO_MEMORY
;
205 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
207 return NT_STATUS_BAD_NETWORK_NAME
;
210 /* create a new tcon as child of the session */
211 tcon
= talloc_zero(req
->session
, struct smbd_smb2_tcon
);
213 return NT_STATUS_NO_MEMORY
;
215 id
= idr_get_new_random(req
->session
->tcons
.idtree
,
217 req
->session
->tcons
.limit
);
220 return NT_STATUS_INSUFFICIENT_RESOURCES
;
225 DLIST_ADD_END(req
->session
->tcons
.list
, tcon
,
226 struct smbd_smb2_tcon
*);
227 tcon
->session
= req
->session
;
228 tcon
->session
->sconn
->num_tcons_open
++;
229 talloc_set_destructor(tcon
, smbd_smb2_tcon_destructor
);
231 compat_conn
= make_connection_smb2(req
->sconn
,
233 req
->session
->compat_vuser
,
234 data_blob_null
, "???",
236 if (compat_conn
== NULL
) {
240 tcon
->compat_conn
= talloc_move(tcon
, &compat_conn
);
242 if (IS_PRINT(tcon
->compat_conn
)) {
243 *out_share_type
= SMB2_SHARE_TYPE_PRINT
;
244 } else if (IS_IPC(tcon
->compat_conn
)) {
245 *out_share_type
= SMB2_SHARE_TYPE_PIPE
;
247 *out_share_type
= SMB2_SHARE_TYPE_DISK
;
250 *out_share_flags
= SMB2_SHAREFLAG_ALLOW_NAMESPACE_CACHING
;
252 if (lp_msdfs_root(SNUM(tcon
->compat_conn
)) && lp_host_msdfs()) {
253 *out_share_flags
|= (SMB2_SHAREFLAG_DFS
|SMB2_SHAREFLAG_DFS_ROOT
);
254 *out_capabilities
= SMB2_SHARE_CAP_DFS
;
256 *out_capabilities
= 0;
259 switch(lp_csc_policy(SNUM(tcon
->compat_conn
))) {
260 case CSC_POLICY_MANUAL
:
262 case CSC_POLICY_DOCUMENTS
:
263 *out_share_flags
|= SMB2_SHAREFLAG_AUTO_CACHING
;
265 case CSC_POLICY_PROGRAMS
:
266 *out_share_flags
|= SMB2_SHAREFLAG_VDO_CACHING
;
268 case CSC_POLICY_DISABLE
:
269 *out_share_flags
|= SMB2_SHAREFLAG_NO_CACHING
;
275 *out_maximal_access
= tcon
->compat_conn
->share_access
;
277 *out_tree_id
= tcon
->tid
;
281 NTSTATUS
smbd_smb2_request_check_tcon(struct smbd_smb2_request
*req
)
283 const uint8_t *inhdr
;
284 int i
= req
->current_idx
;
288 struct smbd_smb2_tcon
*tcon
;
292 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
294 in_flags
= IVAL(inhdr
, SMB2_HDR_FLAGS
);
295 in_tid
= IVAL(inhdr
, SMB2_HDR_TID
);
297 if (in_flags
& SMB2_HDR_FLAG_CHAINED
) {
298 in_tid
= req
->last_tid
;
301 req
->last_tid
= UINT32_MAX
;
303 /* lookup an existing session */
304 p
= idr_find(req
->session
->tcons
.idtree
, in_tid
);
306 return NT_STATUS_NETWORK_NAME_DELETED
;
308 tcon
= talloc_get_type_abort(p
, struct smbd_smb2_tcon
);
310 if (!change_to_user(tcon
->compat_conn
,req
->session
->vuid
)) {
311 return NT_STATUS_ACCESS_DENIED
;
314 /* should we pass FLAG_CASELESS_PATHNAMES here? */
315 if (!set_current_service(tcon
->compat_conn
, 0, true)) {
316 return NT_STATUS_ACCESS_DENIED
;
320 req
->last_tid
= in_tid
;
325 NTSTATUS
smbd_smb2_request_process_tdis(struct smbd_smb2_request
*req
)
330 status
= smbd_smb2_request_verify_sizes(req
, 0x04);
331 if (!NT_STATUS_IS_OK(status
)) {
332 return smbd_smb2_request_error(req
, status
);
336 * TODO: cancel all outstanding requests on the tcon
337 * and delete all file handles.
339 TALLOC_FREE(req
->tcon
);
341 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x04);
342 if (outbody
.data
== NULL
) {
343 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
346 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
347 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
349 return smbd_smb2_request_done(req
, outbody
, NULL
);