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"
28 #include "../lib/util/tevent_ntstatus.h"
30 static struct tevent_req
*smbd_smb2_tree_connect_send(TALLOC_CTX
*mem_ctx
,
31 struct tevent_context
*ev
,
32 struct smbd_smb2_request
*smb2req
,
34 static NTSTATUS
smbd_smb2_tree_connect_recv(struct tevent_req
*req
,
35 uint8_t *out_share_type
,
36 uint32_t *out_share_flags
,
37 uint32_t *out_capabilities
,
38 uint32_t *out_maximal_access
,
39 uint32_t *out_tree_id
);
41 static void smbd_smb2_request_tcon_done(struct tevent_req
*subreq
);
43 NTSTATUS
smbd_smb2_request_process_tcon(struct smbd_smb2_request
*req
)
45 const uint8_t *inbody
;
46 int i
= req
->current_idx
;
47 uint16_t in_path_offset
;
48 uint16_t in_path_length
;
49 DATA_BLOB in_path_buffer
;
51 size_t in_path_string_size
;
54 struct tevent_req
*subreq
;
56 status
= smbd_smb2_request_verify_sizes(req
, 0x09);
57 if (!NT_STATUS_IS_OK(status
)) {
58 return smbd_smb2_request_error(req
, status
);
60 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
62 in_path_offset
= SVAL(inbody
, 0x04);
63 in_path_length
= SVAL(inbody
, 0x06);
65 if (in_path_offset
!= (SMB2_HDR_BODY
+ req
->in
.vector
[i
+1].iov_len
)) {
66 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
69 if (in_path_length
> req
->in
.vector
[i
+2].iov_len
) {
70 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
73 in_path_buffer
.data
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
74 in_path_buffer
.length
= in_path_length
;
76 ok
= convert_string_talloc(req
, CH_UTF16
, CH_UNIX
,
78 in_path_buffer
.length
,
80 &in_path_string_size
);
82 return smbd_smb2_request_error(req
, NT_STATUS_ILLEGAL_CHARACTER
);
85 if (in_path_buffer
.length
== 0) {
86 in_path_string_size
= 0;
89 if (strlen(in_path_string
) != in_path_string_size
) {
90 return smbd_smb2_request_error(req
, NT_STATUS_BAD_NETWORK_NAME
);
93 subreq
= smbd_smb2_tree_connect_send(req
,
98 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
100 tevent_req_set_callback(subreq
, smbd_smb2_request_tcon_done
, req
);
102 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
105 static void smbd_smb2_request_tcon_done(struct tevent_req
*subreq
)
107 struct smbd_smb2_request
*req
=
108 tevent_req_callback_data(subreq
,
109 struct smbd_smb2_request
);
110 int i
= req
->current_idx
;
113 uint8_t out_share_type
= 0;
114 uint32_t out_share_flags
= 0;
115 uint32_t out_capabilities
= 0;
116 uint32_t out_maximal_access
= 0;
117 uint32_t out_tree_id
= 0;
121 status
= smbd_smb2_tree_connect_recv(subreq
,
128 if (!NT_STATUS_IS_OK(status
)) {
129 error
= smbd_smb2_request_error(req
, status
);
130 if (!NT_STATUS_IS_OK(error
)) {
131 smbd_server_connection_terminate(req
->sconn
,
138 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
140 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x10);
141 if (outbody
.data
== NULL
) {
142 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
143 if (!NT_STATUS_IS_OK(error
)) {
144 smbd_server_connection_terminate(req
->sconn
,
151 SIVAL(outhdr
, SMB2_HDR_TID
, out_tree_id
);
153 SSVAL(outbody
.data
, 0x00, 0x10); /* struct size */
154 SCVAL(outbody
.data
, 0x02,
155 out_share_type
); /* share type */
156 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
157 SIVAL(outbody
.data
, 0x04,
158 out_share_flags
); /* share flags */
159 SIVAL(outbody
.data
, 0x08,
160 out_capabilities
); /* capabilities */
161 SIVAL(outbody
.data
, 0x0C,
162 out_maximal_access
); /* maximal access */
164 error
= smbd_smb2_request_done(req
, outbody
, NULL
);
165 if (!NT_STATUS_IS_OK(error
)) {
166 smbd_server_connection_terminate(req
->sconn
,
172 static int smbd_smb2_tcon_destructor(struct smbd_smb2_tcon
*tcon
)
174 if (tcon
->session
== NULL
) {
178 idr_remove(tcon
->session
->tcons
.idtree
, tcon
->tid
);
179 DLIST_REMOVE(tcon
->session
->tcons
.list
, tcon
);
181 if (tcon
->compat_conn
) {
182 set_current_service(tcon
->compat_conn
, 0, true);
183 close_cnum(tcon
->compat_conn
, tcon
->session
->vuid
);
186 tcon
->compat_conn
= NULL
;
188 tcon
->session
= NULL
;
193 static NTSTATUS
smbd_smb2_tree_connect(struct smbd_smb2_request
*req
,
195 uint8_t *out_share_type
,
196 uint32_t *out_share_flags
,
197 uint32_t *out_capabilities
,
198 uint32_t *out_maximal_access
,
199 uint32_t *out_tree_id
)
201 const char *share
= in_path
;
202 char *service
= NULL
;
204 struct smbd_smb2_tcon
*tcon
;
205 connection_struct
*compat_conn
= NULL
;
206 struct user_struct
*compat_vuser
= req
->session
->compat_vuser
;
210 if (strncmp(share
, "\\\\", 2) == 0) {
211 const char *p
= strchr(share
+2, '\\');
217 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
220 service
= talloc_strdup(talloc_tos(), share
);
222 return NT_STATUS_NO_MEMORY
;
227 /* TODO: do more things... */
228 if (strequal(service
,HOMES_NAME
)) {
229 if (compat_vuser
->homes_snum
== -1) {
230 DEBUG(2, ("[homes] share not available for "
231 "user %s because it was not found "
232 "or created at session setup "
234 compat_vuser
->session_info
->unix_info
->unix_name
));
235 return NT_STATUS_BAD_NETWORK_NAME
;
237 snum
= compat_vuser
->homes_snum
;
238 } else if ((compat_vuser
->homes_snum
!= -1)
240 lp_servicename(compat_vuser
->homes_snum
))) {
241 snum
= compat_vuser
->homes_snum
;
243 snum
= find_service(talloc_tos(), service
, &service
);
245 return NT_STATUS_NO_MEMORY
;
250 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
252 return NT_STATUS_BAD_NETWORK_NAME
;
255 /* create a new tcon as child of the session */
256 tcon
= talloc_zero(req
->session
, struct smbd_smb2_tcon
);
258 return NT_STATUS_NO_MEMORY
;
260 id
= idr_get_new_random(req
->session
->tcons
.idtree
,
262 req
->session
->tcons
.limit
);
265 return NT_STATUS_INSUFFICIENT_RESOURCES
;
270 DLIST_ADD_END(req
->session
->tcons
.list
, tcon
,
271 struct smbd_smb2_tcon
*);
272 tcon
->session
= req
->session
;
273 talloc_set_destructor(tcon
, smbd_smb2_tcon_destructor
);
275 compat_conn
= make_connection_smb2(req
->sconn
,
277 req
->session
->compat_vuser
,
280 if (compat_conn
== NULL
) {
284 tcon
->compat_conn
= talloc_move(tcon
, &compat_conn
);
286 if (IS_PRINT(tcon
->compat_conn
)) {
287 *out_share_type
= SMB2_SHARE_TYPE_PRINT
;
288 } else if (IS_IPC(tcon
->compat_conn
)) {
289 *out_share_type
= SMB2_SHARE_TYPE_PIPE
;
291 *out_share_type
= SMB2_SHARE_TYPE_DISK
;
294 *out_share_flags
= 0;
296 if (lp_msdfs_root(SNUM(tcon
->compat_conn
)) && lp_host_msdfs()) {
297 *out_share_flags
|= (SMB2_SHAREFLAG_DFS
|SMB2_SHAREFLAG_DFS_ROOT
);
298 *out_capabilities
= SMB2_SHARE_CAP_DFS
;
300 *out_capabilities
= 0;
303 switch(lp_csc_policy(SNUM(tcon
->compat_conn
))) {
304 case CSC_POLICY_MANUAL
:
306 case CSC_POLICY_DOCUMENTS
:
307 *out_share_flags
|= SMB2_SHAREFLAG_AUTO_CACHING
;
309 case CSC_POLICY_PROGRAMS
:
310 *out_share_flags
|= SMB2_SHAREFLAG_VDO_CACHING
;
312 case CSC_POLICY_DISABLE
:
313 *out_share_flags
|= SMB2_SHAREFLAG_NO_CACHING
;
319 if (lp_hideunreadable(SNUM(tcon
->compat_conn
)) ||
320 lp_hideunwriteable_files(SNUM(tcon
->compat_conn
))) {
321 *out_share_flags
|= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM
;
324 *out_maximal_access
= tcon
->compat_conn
->share_access
;
326 *out_tree_id
= tcon
->tid
;
330 struct smbd_smb2_tree_connect_state
{
332 uint8_t out_share_type
;
333 uint32_t out_share_flags
;
334 uint32_t out_capabilities
;
335 uint32_t out_maximal_access
;
336 uint32_t out_tree_id
;
339 static struct tevent_req
*smbd_smb2_tree_connect_send(TALLOC_CTX
*mem_ctx
,
340 struct tevent_context
*ev
,
341 struct smbd_smb2_request
*smb2req
,
344 struct tevent_req
*req
;
345 struct smbd_smb2_tree_connect_state
*state
;
348 req
= tevent_req_create(mem_ctx
, &state
,
349 struct smbd_smb2_tree_connect_state
);
353 state
->in_path
= in_path
;
355 status
= smbd_smb2_tree_connect(smb2req
,
357 &state
->out_share_type
,
358 &state
->out_share_flags
,
359 &state
->out_capabilities
,
360 &state
->out_maximal_access
,
361 &state
->out_tree_id
);
362 if (tevent_req_nterror(req
, status
)) {
363 return tevent_req_post(req
, ev
);
366 tevent_req_done(req
);
367 return tevent_req_post(req
, ev
);
370 static NTSTATUS
smbd_smb2_tree_connect_recv(struct tevent_req
*req
,
371 uint8_t *out_share_type
,
372 uint32_t *out_share_flags
,
373 uint32_t *out_capabilities
,
374 uint32_t *out_maximal_access
,
375 uint32_t *out_tree_id
)
377 struct smbd_smb2_tree_connect_state
*state
=
379 struct smbd_smb2_tree_connect_state
);
382 if (tevent_req_is_nterror(req
, &status
)) {
383 tevent_req_received(req
);
387 *out_share_type
= state
->out_share_type
;
388 *out_share_flags
= state
->out_share_flags
;
389 *out_capabilities
= state
->out_capabilities
;
390 *out_maximal_access
= state
->out_maximal_access
;
391 *out_tree_id
= state
->out_tree_id
;
393 tevent_req_received(req
);
397 NTSTATUS
smbd_smb2_request_process_tdis(struct smbd_smb2_request
*req
)
402 status
= smbd_smb2_request_verify_sizes(req
, 0x04);
403 if (!NT_STATUS_IS_OK(status
)) {
404 return smbd_smb2_request_error(req
, status
);
408 * TODO: cancel all outstanding requests on the tcon
409 * and delete all file handles.
411 TALLOC_FREE(req
->tcon
);
413 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x04);
414 if (outbody
.data
== NULL
) {
415 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
418 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
419 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
421 return smbd_smb2_request_done(req
, outbody
, NULL
);