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"
27 #include "lib/param/loadparm.h"
29 static NTSTATUS
smbd_smb2_tree_connect(struct smbd_smb2_request
*req
,
31 uint8_t *out_share_type
,
32 uint32_t *out_share_flags
,
33 uint32_t *out_capabilities
,
34 uint32_t *out_maximal_access
,
35 uint32_t *out_tree_id
);
37 NTSTATUS
smbd_smb2_request_process_tcon(struct smbd_smb2_request
*req
)
39 const uint8_t *inbody
;
40 int i
= req
->current_idx
;
43 size_t expected_body_size
= 0x09;
45 uint16_t in_path_offset
;
46 uint16_t in_path_length
;
47 DATA_BLOB in_path_buffer
;
49 size_t in_path_string_size
;
50 uint8_t out_share_type
= 0;
51 uint32_t out_share_flags
= 0;
52 uint32_t out_capabilities
= 0;
53 uint32_t out_maximal_access
= 0;
54 uint32_t out_tree_id
= 0;
58 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
59 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
62 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
64 body_size
= SVAL(inbody
, 0x00);
65 if (body_size
!= expected_body_size
) {
66 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
69 in_path_offset
= SVAL(inbody
, 0x04);
70 in_path_length
= SVAL(inbody
, 0x06);
72 if (in_path_offset
!= (SMB2_HDR_BODY
+ (body_size
& 0xFFFFFFFE))) {
73 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
76 if (in_path_length
> req
->in
.vector
[i
+2].iov_len
) {
77 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
80 in_path_buffer
.data
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
81 in_path_buffer
.length
= in_path_length
;
83 ok
= convert_string_talloc(req
, CH_UTF16
, CH_UNIX
,
85 in_path_buffer
.length
,
87 &in_path_string_size
);
89 return smbd_smb2_request_error(req
, NT_STATUS_ILLEGAL_CHARACTER
);
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_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_snum(req
->sconn
,
232 snum
, req
->session
->compat_vuser
,
233 data_blob_null
, "???",
235 if (compat_conn
== NULL
) {
239 tcon
->compat_conn
= talloc_move(tcon
, &compat_conn
);
240 tcon
->compat_conn
->cnum
= tcon
->tid
;
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_process_tdis(struct smbd_smb2_request
*req
)
283 const uint8_t *inbody
;
284 int i
= req
->current_idx
;
286 size_t expected_body_size
= 0x04;
289 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
290 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
293 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
295 body_size
= SVAL(inbody
, 0x00);
296 if (body_size
!= expected_body_size
) {
297 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
301 * TODO: cancel all outstanding requests on the tcon
302 * and delete all file handles.
304 TALLOC_FREE(req
->tcon
);
306 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x04);
307 if (outbody
.data
== NULL
) {
308 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
311 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
312 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
314 return smbd_smb2_request_done(req
, outbody
, NULL
);