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"
29 #include "lib/smbd_tevent_queue.h"
31 static struct tevent_req
*smbd_smb2_tree_connect_send(TALLOC_CTX
*mem_ctx
,
32 struct tevent_context
*ev
,
33 struct smbd_smb2_request
*smb2req
,
35 static NTSTATUS
smbd_smb2_tree_connect_recv(struct tevent_req
*req
,
36 uint8_t *out_share_type
,
37 uint32_t *out_share_flags
,
38 uint32_t *out_capabilities
,
39 uint32_t *out_maximal_access
,
40 uint32_t *out_tree_id
);
42 static void smbd_smb2_request_tcon_done(struct tevent_req
*subreq
);
44 NTSTATUS
smbd_smb2_request_process_tcon(struct smbd_smb2_request
*req
)
46 const uint8_t *inbody
;
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
= SMBD_SMB2_IN_BODY_PTR(req
);
62 in_path_offset
= SVAL(inbody
, 0x04);
63 in_path_length
= SVAL(inbody
, 0x06);
65 if (in_path_offset
!= (SMB2_HDR_BODY
+ SMBD_SMB2_IN_BODY_LEN(req
))) {
66 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
69 if (in_path_length
> SMBD_SMB2_IN_DYN_LEN(req
)) {
70 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
73 in_path_buffer
.data
= SMBD_SMB2_IN_DYN_PTR(req
);
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
);
112 uint8_t out_share_type
= 0;
113 uint32_t out_share_flags
= 0;
114 uint32_t out_capabilities
= 0;
115 uint32_t out_maximal_access
= 0;
116 uint32_t out_tree_id
= 0;
120 status
= smbd_smb2_tree_connect_recv(subreq
,
127 if (!NT_STATUS_IS_OK(status
)) {
128 error
= smbd_smb2_request_error(req
, status
);
129 if (!NT_STATUS_IS_OK(error
)) {
130 smbd_server_connection_terminate(req
->sconn
,
137 outhdr
= SMBD_SMB2_OUT_HDR_PTR(req
);
139 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x10);
140 if (outbody
.data
== NULL
) {
141 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
142 if (!NT_STATUS_IS_OK(error
)) {
143 smbd_server_connection_terminate(req
->sconn
,
150 SIVAL(outhdr
, SMB2_HDR_TID
, out_tree_id
);
152 SSVAL(outbody
.data
, 0x00, 0x10); /* struct size */
153 SCVAL(outbody
.data
, 0x02,
154 out_share_type
); /* share type */
155 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
156 SIVAL(outbody
.data
, 0x04,
157 out_share_flags
); /* share flags */
158 SIVAL(outbody
.data
, 0x08,
159 out_capabilities
); /* capabilities */
160 SIVAL(outbody
.data
, 0x0C,
161 out_maximal_access
); /* maximal access */
163 error
= smbd_smb2_request_done(req
, outbody
, NULL
);
164 if (!NT_STATUS_IS_OK(error
)) {
165 smbd_server_connection_terminate(req
->sconn
,
171 static NTSTATUS
smbd_smb2_tree_connect(struct smbd_smb2_request
*req
,
173 uint8_t *out_share_type
,
174 uint32_t *out_share_flags
,
175 uint32_t *out_capabilities
,
176 uint32_t *out_maximal_access
,
177 uint32_t *out_tree_id
)
179 struct smbXsrv_connection
*conn
= req
->sconn
->conn
;
180 const char *share
= in_path
;
181 char *service
= NULL
;
183 struct smbXsrv_tcon
*tcon
;
184 NTTIME now
= timeval_to_nttime(&req
->request_time
);
185 connection_struct
*compat_conn
= NULL
;
186 struct user_struct
*compat_vuser
= req
->session
->compat
;
188 bool encryption_desired
= req
->session
->encryption_desired
;
189 bool encryption_required
= req
->session
->global
->encryption_required
;
190 bool guest_session
= false;
192 if (strncmp(share
, "\\\\", 2) == 0) {
193 const char *p
= strchr(share
+2, '\\');
199 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
202 service
= talloc_strdup(talloc_tos(), share
);
204 return NT_STATUS_NO_MEMORY
;
207 if (!strlower_m(service
)) {
208 DEBUG(2, ("strlower_m %s failed\n", service
));
209 return NT_STATUS_INVALID_PARAMETER
;
212 /* TODO: do more things... */
213 if (strequal(service
,HOMES_NAME
)) {
214 if (compat_vuser
->homes_snum
== -1) {
215 DEBUG(2, ("[homes] share not available for "
216 "user %s because it was not found "
217 "or created at session setup "
219 compat_vuser
->session_info
->unix_info
->unix_name
));
220 return NT_STATUS_BAD_NETWORK_NAME
;
222 snum
= compat_vuser
->homes_snum
;
223 } else if ((compat_vuser
->homes_snum
!= -1)
225 lp_servicename(talloc_tos(), compat_vuser
->homes_snum
))) {
226 snum
= compat_vuser
->homes_snum
;
228 snum
= find_service(talloc_tos(), service
, &service
);
230 return NT_STATUS_NO_MEMORY
;
235 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
237 return NT_STATUS_BAD_NETWORK_NAME
;
240 if ((lp_smb_encrypt(snum
) >= SMB_SIGNING_DESIRED
) &&
241 (conn
->smb2
.client
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
242 encryption_desired
= true;
245 if (lp_smb_encrypt(snum
) == SMB_SIGNING_REQUIRED
) {
246 encryption_desired
= true;
247 encryption_required
= true;
250 if (security_session_user_level(compat_vuser
->session_info
, NULL
) < SECURITY_USER
) {
251 guest_session
= true;
254 if (guest_session
&& encryption_required
) {
255 DEBUG(1,("reject guest as encryption is required for service %s\n",
257 return NT_STATUS_ACCESS_DENIED
;
260 if (!(conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
261 if (encryption_required
) {
262 DEBUG(1,("reject tcon with dialect[0x%04X] "
263 "as encryption is required for service %s\n",
264 conn
->smb2
.server
.dialect
, service
));
265 return NT_STATUS_ACCESS_DENIED
;
269 /* create a new tcon as child of the session */
270 status
= smb2srv_tcon_create(req
->session
, now
, &tcon
);
271 if (!NT_STATUS_IS_OK(status
)) {
275 tcon
->encryption_desired
= encryption_desired
;
276 tcon
->global
->encryption_required
= encryption_required
;
278 compat_conn
= make_connection_smb2(req
->sconn
,
280 req
->session
->compat
,
283 if (compat_conn
== NULL
) {
288 tcon
->global
->share_name
= lp_servicename(tcon
->global
,
290 if (tcon
->global
->share_name
== NULL
) {
291 conn_free(compat_conn
);
293 return NT_STATUS_NO_MEMORY
;
295 tcon
->global
->session_global_id
=
296 req
->session
->global
->session_global_id
;
298 tcon
->compat
= talloc_move(tcon
, &compat_conn
);
300 tcon
->status
= NT_STATUS_OK
;
302 status
= smbXsrv_tcon_update(tcon
);
303 if (!NT_STATUS_IS_OK(status
)) {
308 if (IS_PRINT(tcon
->compat
)) {
309 *out_share_type
= SMB2_SHARE_TYPE_PRINT
;
310 } else if (IS_IPC(tcon
->compat
)) {
311 *out_share_type
= SMB2_SHARE_TYPE_PIPE
;
313 *out_share_type
= SMB2_SHARE_TYPE_DISK
;
316 *out_share_flags
= 0;
318 if (lp_msdfs_root(SNUM(tcon
->compat
)) && lp_host_msdfs()) {
319 *out_share_flags
|= (SMB2_SHAREFLAG_DFS
|SMB2_SHAREFLAG_DFS_ROOT
);
320 *out_capabilities
= SMB2_SHARE_CAP_DFS
;
322 *out_capabilities
= 0;
325 switch(lp_csc_policy(SNUM(tcon
->compat
))) {
326 case CSC_POLICY_MANUAL
:
328 case CSC_POLICY_DOCUMENTS
:
329 *out_share_flags
|= SMB2_SHAREFLAG_AUTO_CACHING
;
331 case CSC_POLICY_PROGRAMS
:
332 *out_share_flags
|= SMB2_SHAREFLAG_VDO_CACHING
;
334 case CSC_POLICY_DISABLE
:
335 *out_share_flags
|= SMB2_SHAREFLAG_NO_CACHING
;
341 if (lp_hideunreadable(SNUM(tcon
->compat
)) ||
342 lp_hideunwriteable_files(SNUM(tcon
->compat
))) {
343 *out_share_flags
|= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM
;
346 if (encryption_desired
) {
347 *out_share_flags
|= SMB2_SHAREFLAG_ENCRYPT_DATA
;
350 *out_maximal_access
= tcon
->compat
->share_access
;
352 *out_tree_id
= tcon
->global
->tcon_wire_id
;
356 struct smbd_smb2_tree_connect_state
{
358 uint8_t out_share_type
;
359 uint32_t out_share_flags
;
360 uint32_t out_capabilities
;
361 uint32_t out_maximal_access
;
362 uint32_t out_tree_id
;
365 static struct tevent_req
*smbd_smb2_tree_connect_send(TALLOC_CTX
*mem_ctx
,
366 struct tevent_context
*ev
,
367 struct smbd_smb2_request
*smb2req
,
370 struct tevent_req
*req
;
371 struct smbd_smb2_tree_connect_state
*state
;
374 req
= tevent_req_create(mem_ctx
, &state
,
375 struct smbd_smb2_tree_connect_state
);
379 state
->in_path
= in_path
;
381 status
= smbd_smb2_tree_connect(smb2req
,
383 &state
->out_share_type
,
384 &state
->out_share_flags
,
385 &state
->out_capabilities
,
386 &state
->out_maximal_access
,
387 &state
->out_tree_id
);
388 if (tevent_req_nterror(req
, status
)) {
389 return tevent_req_post(req
, ev
);
392 tevent_req_done(req
);
393 return tevent_req_post(req
, ev
);
396 static NTSTATUS
smbd_smb2_tree_connect_recv(struct tevent_req
*req
,
397 uint8_t *out_share_type
,
398 uint32_t *out_share_flags
,
399 uint32_t *out_capabilities
,
400 uint32_t *out_maximal_access
,
401 uint32_t *out_tree_id
)
403 struct smbd_smb2_tree_connect_state
*state
=
405 struct smbd_smb2_tree_connect_state
);
408 if (tevent_req_is_nterror(req
, &status
)) {
409 tevent_req_received(req
);
413 *out_share_type
= state
->out_share_type
;
414 *out_share_flags
= state
->out_share_flags
;
415 *out_capabilities
= state
->out_capabilities
;
416 *out_maximal_access
= state
->out_maximal_access
;
417 *out_tree_id
= state
->out_tree_id
;
419 tevent_req_received(req
);
423 static struct tevent_req
*smbd_smb2_tdis_send(TALLOC_CTX
*mem_ctx
,
424 struct tevent_context
*ev
,
425 struct smbd_smb2_request
*smb2req
);
426 static NTSTATUS
smbd_smb2_tdis_recv(struct tevent_req
*req
);
427 static void smbd_smb2_request_tdis_done(struct tevent_req
*subreq
);
429 NTSTATUS
smbd_smb2_request_process_tdis(struct smbd_smb2_request
*req
)
432 struct tevent_req
*subreq
= NULL
;
434 status
= smbd_smb2_request_verify_sizes(req
, 0x04);
435 if (!NT_STATUS_IS_OK(status
)) {
436 return smbd_smb2_request_error(req
, status
);
439 subreq
= smbd_smb2_tdis_send(req
, req
->sconn
->ev_ctx
, req
);
440 if (subreq
== NULL
) {
441 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
443 tevent_req_set_callback(subreq
, smbd_smb2_request_tdis_done
, req
);
446 * Wait a long time before going async on this to allow
447 * requests we're waiting on to finish. Set timeout to 10 secs.
449 return smbd_smb2_request_pending_queue(req
, subreq
, 10000000);
452 static void smbd_smb2_request_tdis_done(struct tevent_req
*subreq
)
454 struct smbd_smb2_request
*smb2req
=
455 tevent_req_callback_data(subreq
,
456 struct smbd_smb2_request
);
461 status
= smbd_smb2_tdis_recv(subreq
);
463 if (!NT_STATUS_IS_OK(status
)) {
464 error
= smbd_smb2_request_error(smb2req
, status
);
465 if (!NT_STATUS_IS_OK(error
)) {
466 smbd_server_connection_terminate(smb2req
->sconn
,
473 outbody
= data_blob_talloc(smb2req
->out
.vector
, NULL
, 0x04);
474 if (outbody
.data
== NULL
) {
475 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
476 if (!NT_STATUS_IS_OK(error
)) {
477 smbd_server_connection_terminate(smb2req
->sconn
,
484 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
485 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
487 error
= smbd_smb2_request_done(smb2req
, outbody
, NULL
);
488 if (!NT_STATUS_IS_OK(error
)) {
489 smbd_server_connection_terminate(smb2req
->sconn
,
495 struct smbd_smb2_tdis_state
{
496 struct smbd_smb2_request
*smb2req
;
497 struct tevent_queue
*wait_queue
;
500 static void smbd_smb2_tdis_wait_done(struct tevent_req
*subreq
);
502 static struct tevent_req
*smbd_smb2_tdis_send(TALLOC_CTX
*mem_ctx
,
503 struct tevent_context
*ev
,
504 struct smbd_smb2_request
*smb2req
)
506 struct tevent_req
*req
;
507 struct smbd_smb2_tdis_state
*state
;
508 struct tevent_req
*subreq
;
509 struct smbd_smb2_request
*preq
;
511 req
= tevent_req_create(mem_ctx
, &state
,
512 struct smbd_smb2_tdis_state
);
516 state
->smb2req
= smb2req
;
518 state
->wait_queue
= tevent_queue_create(state
, "tdis_wait_queue");
519 if (tevent_req_nomem(state
->wait_queue
, req
)) {
520 return tevent_req_post(req
, ev
);
524 * Make sure that no new request will be able to use this tcon.
526 smb2req
->tcon
->status
= NT_STATUS_NETWORK_NAME_DELETED
;
528 for (preq
= smb2req
->sconn
->smb2
.requests
; preq
!= NULL
; preq
= preq
->next
) {
529 if (preq
== smb2req
) {
530 /* Can't cancel current request. */
533 if (preq
->tcon
!= smb2req
->tcon
) {
534 /* Request on different tcon. */
539 * Never cancel anything in a compound
540 * request. Way too hard to deal with
543 if (!preq
->compound_related
&& preq
->subreq
!= NULL
) {
544 tevent_req_cancel(preq
->subreq
);
548 * Now wait until the request is finished.
550 * We don't set a callback, as we just want to block the
551 * wait queue and the talloc_free() of the request will
552 * remove the item from the wait queue.
554 subreq
= smbd_tevent_queue_wait_send(preq
, ev
, state
->wait_queue
);
555 if (tevent_req_nomem(subreq
, req
)) {
556 return tevent_req_post(req
, ev
);
561 * Now we add our own waiter to the end of the queue,
562 * this way we get notified when all pending requests are finished
563 * and send to the socket.
565 subreq
= smbd_tevent_queue_wait_send(state
, ev
, state
->wait_queue
);
566 if (tevent_req_nomem(subreq
, req
)) {
567 return tevent_req_post(req
, ev
);
569 tevent_req_set_callback(subreq
, smbd_smb2_tdis_wait_done
, req
);
574 static void smbd_smb2_tdis_wait_done(struct tevent_req
*subreq
)
576 struct tevent_req
*req
= tevent_req_callback_data(
577 subreq
, struct tevent_req
);
578 struct smbd_smb2_tdis_state
*state
= tevent_req_data(
579 req
, struct smbd_smb2_tdis_state
);
582 smbd_tevent_queue_wait_recv(subreq
);
586 * As we've been awoken, we may have changed
587 * uid in the meantime. Ensure we're still
588 * root (SMB2_OP_TDIS has .as_root = true).
590 change_to_root_user();
592 status
= smbXsrv_tcon_disconnect(state
->smb2req
->tcon
,
593 state
->smb2req
->tcon
->compat
->vuid
);
594 if (tevent_req_nterror(req
, status
)) {
598 /* We did tear down the tcon. */
599 TALLOC_FREE(state
->smb2req
->tcon
);
600 tevent_req_done(req
);
603 static NTSTATUS
smbd_smb2_tdis_recv(struct tevent_req
*req
)
605 return tevent_req_simple_recv_ntstatus(req
);