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"
31 #define DBGC_CLASS DBGC_SMB2
33 static struct tevent_req
*smbd_smb2_tree_connect_send(TALLOC_CTX
*mem_ctx
,
34 struct tevent_context
*ev
,
35 struct smbd_smb2_request
*smb2req
,
38 static NTSTATUS
smbd_smb2_tree_connect_recv(struct tevent_req
*req
,
39 uint8_t *out_share_type
,
40 uint32_t *out_share_flags
,
41 uint32_t *out_capabilities
,
42 uint32_t *out_maximal_access
,
43 uint32_t *out_tree_id
,
46 static void smbd_smb2_request_tcon_done(struct tevent_req
*subreq
);
48 NTSTATUS
smbd_smb2_request_process_tcon(struct smbd_smb2_request
*req
)
50 struct smbXsrv_connection
*xconn
= req
->xconn
;
51 const uint8_t *inbody
;
53 uint16_t in_path_offset
;
54 uint16_t in_path_length
;
55 DATA_BLOB in_path_buffer
;
57 size_t in_path_string_size
;
60 struct tevent_req
*subreq
;
62 status
= smbd_smb2_request_verify_sizes(req
, 0x09);
63 if (!NT_STATUS_IS_OK(status
)) {
64 return smbd_smb2_request_error(req
, status
);
66 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
68 if (xconn
->protocol
>= PROTOCOL_SMB3_11
) {
69 in_flags
= SVAL(inbody
, 0x02);
73 in_path_offset
= SVAL(inbody
, 0x04);
74 in_path_length
= SVAL(inbody
, 0x06);
76 if (in_path_offset
!= (SMB2_HDR_BODY
+ SMBD_SMB2_IN_BODY_LEN(req
))) {
77 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
80 if (in_path_length
> SMBD_SMB2_IN_DYN_LEN(req
)) {
81 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
84 in_path_buffer
.data
= SMBD_SMB2_IN_DYN_PTR(req
);
85 in_path_buffer
.length
= in_path_length
;
87 ok
= convert_string_talloc(req
, CH_UTF16
, CH_UNIX
,
89 in_path_buffer
.length
,
91 &in_path_string_size
);
93 return smbd_smb2_request_error(req
, NT_STATUS_ILLEGAL_CHARACTER
);
96 if (in_path_buffer
.length
== 0) {
97 in_path_string_size
= 0;
100 if (strlen(in_path_string
) != in_path_string_size
) {
101 return smbd_smb2_request_error(req
, NT_STATUS_BAD_NETWORK_NAME
);
104 subreq
= smbd_smb2_tree_connect_send(req
,
109 if (subreq
== NULL
) {
110 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
112 tevent_req_set_callback(subreq
, smbd_smb2_request_tcon_done
, req
);
115 * Avoid sending a STATUS_PENDING message, it's very likely
116 * the client won't expect that.
118 return smbd_smb2_request_pending_queue(req
, subreq
, 0);
121 static void smbd_smb2_request_tcon_done(struct tevent_req
*subreq
)
123 struct smbd_smb2_request
*req
=
124 tevent_req_callback_data(subreq
,
125 struct smbd_smb2_request
);
128 uint8_t out_share_type
= 0;
129 uint32_t out_share_flags
= 0;
130 uint32_t out_capabilities
= 0;
131 uint32_t out_maximal_access
= 0;
132 uint32_t out_tree_id
= 0;
133 bool disconnect
= false;
137 status
= smbd_smb2_tree_connect_recv(subreq
,
145 if (!NT_STATUS_IS_OK(status
)) {
147 smbd_server_connection_terminate(req
->xconn
,
151 error
= smbd_smb2_request_error(req
, status
);
152 if (!NT_STATUS_IS_OK(error
)) {
153 smbd_server_connection_terminate(req
->xconn
,
160 outhdr
= SMBD_SMB2_OUT_HDR_PTR(req
);
162 outbody
= smbd_smb2_generate_outbody(req
, 0x10);
163 if (outbody
.data
== NULL
) {
164 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
165 if (!NT_STATUS_IS_OK(error
)) {
166 smbd_server_connection_terminate(req
->xconn
,
173 SIVAL(outhdr
, SMB2_HDR_TID
, out_tree_id
);
175 SSVAL(outbody
.data
, 0x00, 0x10); /* struct size */
176 SCVAL(outbody
.data
, 0x02,
177 out_share_type
); /* share type */
178 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
179 SIVAL(outbody
.data
, 0x04,
180 out_share_flags
); /* share flags */
181 SIVAL(outbody
.data
, 0x08,
182 out_capabilities
); /* capabilities */
183 SIVAL(outbody
.data
, 0x0C,
184 out_maximal_access
); /* maximal access */
186 error
= smbd_smb2_request_done(req
, outbody
, NULL
);
187 if (!NT_STATUS_IS_OK(error
)) {
188 smbd_server_connection_terminate(req
->xconn
,
194 static NTSTATUS
smbd_smb2_tree_connect(struct smbd_smb2_request
*req
,
196 uint8_t *out_share_type
,
197 uint32_t *out_share_flags
,
198 uint32_t *out_capabilities
,
199 uint32_t *out_maximal_access
,
200 uint32_t *out_tree_id
,
203 const struct loadparm_substitution
*lp_sub
=
204 loadparm_s3_global_substitution();
205 struct smbXsrv_connection
*conn
= req
->xconn
;
206 struct smbXsrv_session
*session
= req
->session
;
207 struct auth_session_info
*session_info
=
208 session
->global
->auth_session_info
;
209 const char *share
= in_path
;
210 char *service
= NULL
;
212 struct smbXsrv_tcon
*tcon
;
213 NTTIME now
= timeval_to_nttime(&req
->request_time
);
214 connection_struct
*compat_conn
= NULL
;
216 bool encryption_desired
= req
->session
->global
->encryption_flags
& SMBXSRV_ENCRYPTION_DESIRED
;
217 bool encryption_required
= req
->session
->global
->encryption_flags
& SMBXSRV_ENCRYPTION_REQUIRED
;
218 bool guest_session
= false;
219 bool require_signed_tcon
= false;
220 uint32_t session_global_id
;
221 char *share_name
= NULL
;
222 uint8_t encryption_flags
= 0;
226 if (strncmp(share
, "\\\\", 2) == 0) {
227 const char *p
= strchr(share
+2, '\\');
233 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
236 if (security_session_user_level(session_info
, NULL
) < SECURITY_USER
) {
237 guest_session
= true;
240 if (conn
->protocol
>= PROTOCOL_SMB3_11
&& !guest_session
) {
241 require_signed_tcon
= true;
244 if (require_signed_tcon
&& !req
->do_encryption
&& !req
->do_signing
) {
245 DEBUG(1, ("smbd_smb2_tree_connect: reject request to share "
246 "[%s] as '%s\\%s' without encryption or signing. "
249 req
->session
->global
->auth_session_info
->info
->domain_name
,
250 req
->session
->global
->auth_session_info
->info
->account_name
));
252 return NT_STATUS_ACCESS_DENIED
;
255 service
= talloc_strdup(talloc_tos(), share
);
257 return NT_STATUS_NO_MEMORY
;
260 if (!strlower_m(service
)) {
261 DEBUG(2, ("strlower_m %s failed\n", service
));
262 return NT_STATUS_INVALID_PARAMETER
;
265 /* TODO: do more things... */
266 if (strequal(service
,HOMES_NAME
)) {
267 if (session
->homes_snum
== -1) {
268 DEBUG(2, ("[homes] share not available for "
269 "user %s because it was not found "
270 "or created at session setup "
272 session_info
->unix_info
->unix_name
));
273 return NT_STATUS_BAD_NETWORK_NAME
;
275 snum
= session
->homes_snum
;
276 } else if ((session
->homes_snum
!= -1)
278 lp_servicename(talloc_tos(), lp_sub
, session
->homes_snum
))) {
279 snum
= session
->homes_snum
;
281 snum
= find_service(talloc_tos(), service
, &service
);
283 return NT_STATUS_NO_MEMORY
;
288 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
290 return NT_STATUS_BAD_NETWORK_NAME
;
293 /* Handle non-DFS clients attempting connections to msdfs proxy */
294 if (lp_host_msdfs()) {
295 char *proxy
= lp_msdfs_proxy(talloc_tos(), lp_sub
, snum
);
297 if ((proxy
!= NULL
) && (*proxy
!= '\0')) {
298 DBG_NOTICE("refusing connection to dfs proxy share "
299 "'%s' (pointing to %s)\n",
303 return NT_STATUS_BAD_NETWORK_NAME
;
308 if ((lp_server_smb_encrypt(snum
) >= SMB_ENCRYPTION_DESIRED
) &&
309 (conn
->smb2
.server
.cipher
!= 0))
311 encryption_desired
= true;
314 if (lp_server_smb_encrypt(snum
) == SMB_ENCRYPTION_REQUIRED
) {
315 encryption_desired
= true;
316 encryption_required
= true;
319 if (guest_session
&& encryption_required
) {
320 DEBUG(1,("reject guest as encryption is required for service %s\n",
322 return NT_STATUS_ACCESS_DENIED
;
325 if (conn
->smb2
.server
.cipher
== 0) {
326 if (encryption_required
) {
327 DEBUG(1,("reject tcon with dialect[0x%04X] "
328 "as encryption is required for service %s\n",
329 conn
->smb2
.server
.dialect
, service
));
330 return NT_STATUS_ACCESS_DENIED
;
334 if (encryption_desired
) {
335 encryption_flags
|= SMBXSRV_ENCRYPTION_DESIRED
;
337 if (encryption_required
) {
338 encryption_flags
|= SMBXSRV_ENCRYPTION_REQUIRED
;
341 session_global_id
= req
->session
->global
->session_global_id
;
342 share_name
= lp_servicename(talloc_tos(), lp_sub
, snum
);
343 if (share_name
== NULL
) {
344 return NT_STATUS_NO_MEMORY
;
347 if ((lp_max_connections(snum
) > 0)
348 && (count_current_connections(lp_const_servicename(snum
), true) >=
349 lp_max_connections(snum
))) {
351 DBG_WARNING("Max connections (%d) exceeded for [%s][%s]\n",
352 lp_max_connections(snum
),
353 lp_const_servicename(snum
), share_name
);
354 TALLOC_FREE(share_name
);
355 return NT_STATUS_INSUFFICIENT_RESOURCES
;
358 /* create a new tcon as child of the session */
359 status
= smb2srv_tcon_create(req
->session
,
364 TALLOC_FREE(share_name
);
365 if (!NT_STATUS_IS_OK(status
)) {
369 compat_conn
= make_connection_smb2(req
,
373 if (compat_conn
== NULL
) {
378 tcon
->compat
= talloc_move(tcon
, &compat_conn
);
380 tcon
->status
= NT_STATUS_OK
;
382 if (IS_PRINT(tcon
->compat
)) {
383 *out_share_type
= SMB2_SHARE_TYPE_PRINT
;
384 } else if (IS_IPC(tcon
->compat
)) {
385 *out_share_type
= SMB2_SHARE_TYPE_PIPE
;
387 *out_share_type
= SMB2_SHARE_TYPE_DISK
;
390 *out_share_flags
= 0;
392 if (lp_msdfs_root(SNUM(tcon
->compat
)) && lp_host_msdfs()) {
393 *out_share_flags
|= (SMB2_SHAREFLAG_DFS
|SMB2_SHAREFLAG_DFS_ROOT
);
394 *out_capabilities
= SMB2_SHARE_CAP_DFS
;
396 *out_capabilities
= 0;
399 switch(lp_csc_policy(SNUM(tcon
->compat
))) {
400 case CSC_POLICY_MANUAL
:
402 case CSC_POLICY_DOCUMENTS
:
403 *out_share_flags
|= SMB2_SHAREFLAG_AUTO_CACHING
;
405 case CSC_POLICY_PROGRAMS
:
406 *out_share_flags
|= SMB2_SHAREFLAG_VDO_CACHING
;
408 case CSC_POLICY_DISABLE
:
409 *out_share_flags
|= SMB2_SHAREFLAG_NO_CACHING
;
415 if (lp_hide_unreadable(SNUM(tcon
->compat
)) ||
416 lp_hide_unwriteable_files(SNUM(tcon
->compat
))) {
417 *out_share_flags
|= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM
;
420 if (encryption_desired
) {
421 *out_share_flags
|= SMB2_SHAREFLAG_ENCRYPT_DATA
;
425 * For disk shares we can change the client
426 * behavior on a cluster...
428 if (conn
->protocol
>= PROTOCOL_SMB3_00
&&
429 *out_share_type
== SMB2_SHARE_TYPE_DISK
)
431 bool persistent
= false; /* persistent handles not implemented yet */
432 bool cluster
= lp_clustering();
433 bool scaleout
= cluster
;
434 bool witness
= cluster
&& !lp_rpc_start_on_demand_helpers();
435 bool asymmetric
= false; /* shares are symmetric by default */
439 * In a ctdb cluster shares are continuously available,
440 * but windows clients mix this with the global persistent
443 * Persistent handles are requested if
444 * SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY is present
445 * even without SMB2_CAP_PERSISTENT_HANDLES.
447 * And SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY is
448 * required for SMB2_SHARE_CAP_CLUSTER to have
451 * So we better don't announce this by default
452 * until we support persistent handles.
454 announce
= lp_parm_bool(SNUM(tcon
->compat
),
456 "CONTINUOUS AVAILABILITY",
459 *out_capabilities
|= SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY
;
463 * ctdb clusters are always scale out...
465 announce
= lp_parm_bool(SNUM(tcon
->compat
),
470 *out_capabilities
|= SMB2_SHARE_CAP_SCALEOUT
;
474 * We support the witness service when ctdb is active
476 announce
= lp_parm_bool(SNUM(tcon
->compat
),
481 *out_capabilities
|= SMB2_SHARE_CAP_CLUSTER
;
485 * Shares in a ctdb cluster are symmetric by design.
487 * But it might be useful to let the client use
488 * an isolated transport and witness registration for the
491 if (conn
->protocol
>= PROTOCOL_SMB3_02
) {
492 announce
= lp_parm_bool(SNUM(tcon
->compat
),
498 *out_capabilities
|= SMB2_SHARE_CAP_ASYMMETRIC
;
502 *out_maximal_access
= tcon
->compat
->share_access
;
504 *out_tree_id
= tcon
->global
->tcon_wire_id
;
505 req
->last_tid
= tcon
->global
->tcon_wire_id
;
510 struct smbd_smb2_tree_connect_state
{
512 uint8_t out_share_type
;
513 uint32_t out_share_flags
;
514 uint32_t out_capabilities
;
515 uint32_t out_maximal_access
;
516 uint32_t out_tree_id
;
520 static struct tevent_req
*smbd_smb2_tree_connect_send(TALLOC_CTX
*mem_ctx
,
521 struct tevent_context
*ev
,
522 struct smbd_smb2_request
*smb2req
,
526 struct tevent_req
*req
;
527 struct smbd_smb2_tree_connect_state
*state
;
530 req
= tevent_req_create(mem_ctx
, &state
,
531 struct smbd_smb2_tree_connect_state
);
535 state
->in_path
= in_path
;
537 status
= smbd_smb2_tree_connect(smb2req
,
539 &state
->out_share_type
,
540 &state
->out_share_flags
,
541 &state
->out_capabilities
,
542 &state
->out_maximal_access
,
545 if (tevent_req_nterror(req
, status
)) {
546 return tevent_req_post(req
, ev
);
549 tevent_req_done(req
);
550 return tevent_req_post(req
, ev
);
553 static NTSTATUS
smbd_smb2_tree_connect_recv(struct tevent_req
*req
,
554 uint8_t *out_share_type
,
555 uint32_t *out_share_flags
,
556 uint32_t *out_capabilities
,
557 uint32_t *out_maximal_access
,
558 uint32_t *out_tree_id
,
561 struct smbd_smb2_tree_connect_state
*state
=
563 struct smbd_smb2_tree_connect_state
);
566 if (tevent_req_is_nterror(req
, &status
)) {
567 tevent_req_received(req
);
571 *out_share_type
= state
->out_share_type
;
572 *out_share_flags
= state
->out_share_flags
;
573 *out_capabilities
= state
->out_capabilities
;
574 *out_maximal_access
= state
->out_maximal_access
;
575 *out_tree_id
= state
->out_tree_id
;
576 *disconnect
= state
->disconnect
;
578 tevent_req_received(req
);
582 static struct tevent_req
*smbd_smb2_tdis_send(TALLOC_CTX
*mem_ctx
,
583 struct tevent_context
*ev
,
584 struct smbd_smb2_request
*smb2req
);
585 static NTSTATUS
smbd_smb2_tdis_recv(struct tevent_req
*req
);
586 static void smbd_smb2_request_tdis_done(struct tevent_req
*subreq
);
588 NTSTATUS
smbd_smb2_request_process_tdis(struct smbd_smb2_request
*req
)
591 struct tevent_req
*subreq
= NULL
;
593 status
= smbd_smb2_request_verify_sizes(req
, 0x04);
594 if (!NT_STATUS_IS_OK(status
)) {
595 return smbd_smb2_request_error(req
, status
);
598 subreq
= smbd_smb2_tdis_send(req
, req
->sconn
->ev_ctx
, req
);
599 if (subreq
== NULL
) {
600 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
602 tevent_req_set_callback(subreq
, smbd_smb2_request_tdis_done
, req
);
605 * Avoid sending a STATUS_PENDING message, it's very likely
606 * the client won't expect that.
608 return smbd_smb2_request_pending_queue(req
, subreq
, 0);
611 static void smbd_smb2_request_tdis_done(struct tevent_req
*subreq
)
613 struct smbd_smb2_request
*smb2req
=
614 tevent_req_callback_data(subreq
,
615 struct smbd_smb2_request
);
620 status
= smbd_smb2_tdis_recv(subreq
);
622 if (!NT_STATUS_IS_OK(status
)) {
623 error
= smbd_smb2_request_error(smb2req
, status
);
624 if (!NT_STATUS_IS_OK(error
)) {
625 smbd_server_connection_terminate(smb2req
->xconn
,
632 outbody
= smbd_smb2_generate_outbody(smb2req
, 0x04);
633 if (outbody
.data
== NULL
) {
634 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
635 if (!NT_STATUS_IS_OK(error
)) {
636 smbd_server_connection_terminate(smb2req
->xconn
,
643 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
644 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
646 error
= smbd_smb2_request_done(smb2req
, outbody
, NULL
);
647 if (!NT_STATUS_IS_OK(error
)) {
648 smbd_server_connection_terminate(smb2req
->xconn
,
654 struct smbd_smb2_tdis_state
{
655 struct smbd_smb2_request
*smb2req
;
656 struct tevent_queue
*wait_queue
;
659 static void smbd_smb2_tdis_wait_done(struct tevent_req
*subreq
);
661 static struct tevent_req
*smbd_smb2_tdis_send(TALLOC_CTX
*mem_ctx
,
662 struct tevent_context
*ev
,
663 struct smbd_smb2_request
*smb2req
)
665 struct tevent_req
*req
;
666 struct smbd_smb2_tdis_state
*state
;
667 struct tevent_req
*subreq
;
668 struct smbXsrv_connection
*xconn
= NULL
;
670 req
= tevent_req_create(mem_ctx
, &state
,
671 struct smbd_smb2_tdis_state
);
675 state
->smb2req
= smb2req
;
677 state
->wait_queue
= tevent_queue_create(state
, "tdis_wait_queue");
678 if (tevent_req_nomem(state
->wait_queue
, req
)) {
679 return tevent_req_post(req
, ev
);
683 * Make sure that no new request will be able to use this tcon.
685 smb2req
->tcon
->status
= NT_STATUS_NETWORK_NAME_DELETED
;
687 xconn
= smb2req
->xconn
->client
->connections
;
688 for (; xconn
!= NULL
; xconn
= xconn
->next
) {
689 struct smbd_smb2_request
*preq
;
691 for (preq
= xconn
->smb2
.requests
; preq
!= NULL
; preq
= preq
->next
) {
692 if (preq
== smb2req
) {
693 /* Can't cancel current request. */
696 if (preq
->tcon
!= smb2req
->tcon
) {
697 /* Request on different tcon. */
701 if (preq
->subreq
!= NULL
) {
702 tevent_req_cancel(preq
->subreq
);
706 * Now wait until the request is finished.
708 * We don't set a callback, as we just want to block the
709 * wait queue and the talloc_free() of the request will
710 * remove the item from the wait queue.
712 subreq
= tevent_queue_wait_send(preq
, ev
, state
->wait_queue
);
713 if (tevent_req_nomem(subreq
, req
)) {
714 return tevent_req_post(req
, ev
);
720 * Now we add our own waiter to the end of the queue,
721 * this way we get notified when all pending requests are finished
722 * and send to the socket.
724 subreq
= tevent_queue_wait_send(state
, ev
, state
->wait_queue
);
725 if (tevent_req_nomem(subreq
, req
)) {
726 return tevent_req_post(req
, ev
);
728 tevent_req_set_callback(subreq
, smbd_smb2_tdis_wait_done
, req
);
733 static void smbd_smb2_tdis_wait_done(struct tevent_req
*subreq
)
735 struct tevent_req
*req
= tevent_req_callback_data(
736 subreq
, struct tevent_req
);
737 struct smbd_smb2_tdis_state
*state
= tevent_req_data(
738 req
, struct smbd_smb2_tdis_state
);
741 tevent_queue_wait_recv(subreq
);
745 * As we've been awoken, we may have changed
746 * uid in the meantime. Ensure we're still
747 * root (SMB2_OP_TDIS has .as_root = true).
749 change_to_root_user();
751 status
= smbXsrv_tcon_disconnect(state
->smb2req
->tcon
,
752 state
->smb2req
->tcon
->compat
->vuid
);
753 if (tevent_req_nterror(req
, status
)) {
757 /* We did tear down the tcon. */
758 TALLOC_FREE(state
->smb2req
->tcon
);
759 tevent_req_done(req
);
762 static NTSTATUS
smbd_smb2_tdis_recv(struct tevent_req
*req
)
764 return tevent_req_simple_recv_ntstatus(req
);