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
,
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;
117 bool disconnect
= false;
121 status
= smbd_smb2_tree_connect_recv(subreq
,
129 if (!NT_STATUS_IS_OK(status
)) {
131 smbd_server_connection_terminate(req
->xconn
,
135 error
= smbd_smb2_request_error(req
, status
);
136 if (!NT_STATUS_IS_OK(error
)) {
137 smbd_server_connection_terminate(req
->xconn
,
144 outhdr
= SMBD_SMB2_OUT_HDR_PTR(req
);
146 outbody
= smbd_smb2_generate_outbody(req
, 0x10);
147 if (outbody
.data
== NULL
) {
148 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
149 if (!NT_STATUS_IS_OK(error
)) {
150 smbd_server_connection_terminate(req
->xconn
,
157 SIVAL(outhdr
, SMB2_HDR_TID
, out_tree_id
);
159 SSVAL(outbody
.data
, 0x00, 0x10); /* struct size */
160 SCVAL(outbody
.data
, 0x02,
161 out_share_type
); /* share type */
162 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
163 SIVAL(outbody
.data
, 0x04,
164 out_share_flags
); /* share flags */
165 SIVAL(outbody
.data
, 0x08,
166 out_capabilities
); /* capabilities */
167 SIVAL(outbody
.data
, 0x0C,
168 out_maximal_access
); /* maximal access */
170 error
= smbd_smb2_request_done(req
, outbody
, NULL
);
171 if (!NT_STATUS_IS_OK(error
)) {
172 smbd_server_connection_terminate(req
->xconn
,
178 static NTSTATUS
smbd_smb2_tree_connect(struct smbd_smb2_request
*req
,
180 uint8_t *out_share_type
,
181 uint32_t *out_share_flags
,
182 uint32_t *out_capabilities
,
183 uint32_t *out_maximal_access
,
184 uint32_t *out_tree_id
,
187 struct smbXsrv_connection
*conn
= req
->xconn
;
188 const char *share
= in_path
;
189 char *service
= NULL
;
191 struct smbXsrv_tcon
*tcon
;
192 NTTIME now
= timeval_to_nttime(&req
->request_time
);
193 connection_struct
*compat_conn
= NULL
;
194 struct user_struct
*compat_vuser
= req
->session
->compat
;
196 bool encryption_required
= req
->session
->global
->encryption_required
;
197 bool guest_session
= false;
198 bool require_signed_tcon
= false;
202 if (strncmp(share
, "\\\\", 2) == 0) {
203 const char *p
= strchr(share
+2, '\\');
209 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
212 if (security_session_user_level(compat_vuser
->session_info
, NULL
) < SECURITY_USER
) {
213 guest_session
= true;
216 if (conn
->protocol
>= PROTOCOL_SMB3_11
&& !guest_session
) {
217 require_signed_tcon
= true;
220 if (require_signed_tcon
&& !req
->do_encryption
&& !req
->do_signing
) {
221 DEBUG(1, ("smbd_smb2_tree_connect: reject request to share "
222 "[%s] as '%s\\%s' without encryption or signing. "
225 req
->session
->global
->auth_session_info
->info
->domain_name
,
226 req
->session
->global
->auth_session_info
->info
->account_name
));
228 return NT_STATUS_ACCESS_DENIED
;
231 service
= talloc_strdup(talloc_tos(), share
);
233 return NT_STATUS_NO_MEMORY
;
236 if (!strlower_m(service
)) {
237 DEBUG(2, ("strlower_m %s failed\n", service
));
238 return NT_STATUS_INVALID_PARAMETER
;
241 /* TODO: do more things... */
242 if (strequal(service
,HOMES_NAME
)) {
243 if (compat_vuser
->homes_snum
== -1) {
244 DEBUG(2, ("[homes] share not available for "
245 "user %s because it was not found "
246 "or created at session setup "
248 compat_vuser
->session_info
->unix_info
->unix_name
));
249 return NT_STATUS_BAD_NETWORK_NAME
;
251 snum
= compat_vuser
->homes_snum
;
252 } else if ((compat_vuser
->homes_snum
!= -1)
254 lp_servicename(talloc_tos(), compat_vuser
->homes_snum
))) {
255 snum
= compat_vuser
->homes_snum
;
257 snum
= find_service(talloc_tos(), service
, &service
);
259 return NT_STATUS_NO_MEMORY
;
264 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
266 return NT_STATUS_BAD_NETWORK_NAME
;
269 if ((lp_smb_encrypt(snum
) > SMB_SIGNING_OFF
) &&
270 (conn
->smb2
.client
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
271 encryption_required
= true;
274 if (lp_smb_encrypt(snum
) == SMB_SIGNING_REQUIRED
) {
275 encryption_required
= true;
278 if (guest_session
&& encryption_required
) {
279 DEBUG(1,("reject guest as encryption is required for service %s\n",
281 return NT_STATUS_ACCESS_DENIED
;
284 if (conn
->smb2
.server
.cipher
== 0) {
285 if (encryption_required
) {
286 DEBUG(1,("reject tcon with dialect[0x%04X] "
287 "as encryption is required for service %s\n",
288 conn
->smb2
.server
.dialect
, service
));
289 return NT_STATUS_ACCESS_DENIED
;
293 /* create a new tcon as child of the session */
294 status
= smb2srv_tcon_create(req
->session
, now
, &tcon
);
295 if (!NT_STATUS_IS_OK(status
)) {
299 tcon
->global
->encryption_required
= encryption_required
;
301 compat_conn
= make_connection_smb2(req
,
303 req
->session
->compat
,
306 if (compat_conn
== NULL
) {
311 tcon
->global
->share_name
= lp_servicename(tcon
->global
,
313 if (tcon
->global
->share_name
== NULL
) {
314 conn_free(compat_conn
);
316 return NT_STATUS_NO_MEMORY
;
318 tcon
->global
->session_global_id
=
319 req
->session
->global
->session_global_id
;
321 tcon
->compat
= talloc_move(tcon
, &compat_conn
);
323 tcon
->status
= NT_STATUS_OK
;
325 status
= smbXsrv_tcon_update(tcon
);
326 if (!NT_STATUS_IS_OK(status
)) {
331 if (IS_PRINT(tcon
->compat
)) {
332 *out_share_type
= SMB2_SHARE_TYPE_PRINT
;
333 } else if (IS_IPC(tcon
->compat
)) {
334 *out_share_type
= SMB2_SHARE_TYPE_PIPE
;
336 *out_share_type
= SMB2_SHARE_TYPE_DISK
;
339 *out_share_flags
= 0;
341 if (lp_msdfs_root(SNUM(tcon
->compat
)) && lp_host_msdfs()) {
342 *out_share_flags
|= (SMB2_SHAREFLAG_DFS
|SMB2_SHAREFLAG_DFS_ROOT
);
343 *out_capabilities
= SMB2_SHARE_CAP_DFS
;
345 *out_capabilities
= 0;
348 switch(lp_csc_policy(SNUM(tcon
->compat
))) {
349 case CSC_POLICY_MANUAL
:
351 case CSC_POLICY_DOCUMENTS
:
352 *out_share_flags
|= SMB2_SHAREFLAG_AUTO_CACHING
;
354 case CSC_POLICY_PROGRAMS
:
355 *out_share_flags
|= SMB2_SHAREFLAG_VDO_CACHING
;
357 case CSC_POLICY_DISABLE
:
358 *out_share_flags
|= SMB2_SHAREFLAG_NO_CACHING
;
364 if (lp_hide_unreadable(SNUM(tcon
->compat
)) ||
365 lp_hide_unwriteable_files(SNUM(tcon
->compat
))) {
366 *out_share_flags
|= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM
;
369 if (encryption_required
) {
370 *out_share_flags
|= SMB2_SHAREFLAG_ENCRYPT_DATA
;
373 *out_maximal_access
= tcon
->compat
->share_access
;
375 *out_tree_id
= tcon
->global
->tcon_wire_id
;
379 struct smbd_smb2_tree_connect_state
{
381 uint8_t out_share_type
;
382 uint32_t out_share_flags
;
383 uint32_t out_capabilities
;
384 uint32_t out_maximal_access
;
385 uint32_t out_tree_id
;
389 static struct tevent_req
*smbd_smb2_tree_connect_send(TALLOC_CTX
*mem_ctx
,
390 struct tevent_context
*ev
,
391 struct smbd_smb2_request
*smb2req
,
394 struct tevent_req
*req
;
395 struct smbd_smb2_tree_connect_state
*state
;
398 req
= tevent_req_create(mem_ctx
, &state
,
399 struct smbd_smb2_tree_connect_state
);
403 state
->in_path
= in_path
;
405 status
= smbd_smb2_tree_connect(smb2req
,
407 &state
->out_share_type
,
408 &state
->out_share_flags
,
409 &state
->out_capabilities
,
410 &state
->out_maximal_access
,
413 if (tevent_req_nterror(req
, status
)) {
414 return tevent_req_post(req
, ev
);
417 tevent_req_done(req
);
418 return tevent_req_post(req
, ev
);
421 static NTSTATUS
smbd_smb2_tree_connect_recv(struct tevent_req
*req
,
422 uint8_t *out_share_type
,
423 uint32_t *out_share_flags
,
424 uint32_t *out_capabilities
,
425 uint32_t *out_maximal_access
,
426 uint32_t *out_tree_id
,
429 struct smbd_smb2_tree_connect_state
*state
=
431 struct smbd_smb2_tree_connect_state
);
434 if (tevent_req_is_nterror(req
, &status
)) {
435 tevent_req_received(req
);
439 *out_share_type
= state
->out_share_type
;
440 *out_share_flags
= state
->out_share_flags
;
441 *out_capabilities
= state
->out_capabilities
;
442 *out_maximal_access
= state
->out_maximal_access
;
443 *out_tree_id
= state
->out_tree_id
;
444 *disconnect
= state
->disconnect
;
446 tevent_req_received(req
);
450 static struct tevent_req
*smbd_smb2_tdis_send(TALLOC_CTX
*mem_ctx
,
451 struct tevent_context
*ev
,
452 struct smbd_smb2_request
*smb2req
);
453 static NTSTATUS
smbd_smb2_tdis_recv(struct tevent_req
*req
);
454 static void smbd_smb2_request_tdis_done(struct tevent_req
*subreq
);
456 NTSTATUS
smbd_smb2_request_process_tdis(struct smbd_smb2_request
*req
)
459 struct tevent_req
*subreq
= NULL
;
461 status
= smbd_smb2_request_verify_sizes(req
, 0x04);
462 if (!NT_STATUS_IS_OK(status
)) {
463 return smbd_smb2_request_error(req
, status
);
466 subreq
= smbd_smb2_tdis_send(req
, req
->sconn
->ev_ctx
, req
);
467 if (subreq
== NULL
) {
468 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
470 tevent_req_set_callback(subreq
, smbd_smb2_request_tdis_done
, req
);
473 * Wait a long time before going async on this to allow
474 * requests we're waiting on to finish. Set timeout to 10 secs.
476 return smbd_smb2_request_pending_queue(req
, subreq
, 10000000);
479 static void smbd_smb2_request_tdis_done(struct tevent_req
*subreq
)
481 struct smbd_smb2_request
*smb2req
=
482 tevent_req_callback_data(subreq
,
483 struct smbd_smb2_request
);
488 status
= smbd_smb2_tdis_recv(subreq
);
490 if (!NT_STATUS_IS_OK(status
)) {
491 error
= smbd_smb2_request_error(smb2req
, status
);
492 if (!NT_STATUS_IS_OK(error
)) {
493 smbd_server_connection_terminate(smb2req
->xconn
,
500 outbody
= smbd_smb2_generate_outbody(smb2req
, 0x04);
501 if (outbody
.data
== NULL
) {
502 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
503 if (!NT_STATUS_IS_OK(error
)) {
504 smbd_server_connection_terminate(smb2req
->xconn
,
511 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
512 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
514 error
= smbd_smb2_request_done(smb2req
, outbody
, NULL
);
515 if (!NT_STATUS_IS_OK(error
)) {
516 smbd_server_connection_terminate(smb2req
->xconn
,
522 struct smbd_smb2_tdis_state
{
523 struct smbd_smb2_request
*smb2req
;
524 struct tevent_queue
*wait_queue
;
527 static void smbd_smb2_tdis_wait_done(struct tevent_req
*subreq
);
529 static struct tevent_req
*smbd_smb2_tdis_send(TALLOC_CTX
*mem_ctx
,
530 struct tevent_context
*ev
,
531 struct smbd_smb2_request
*smb2req
)
533 struct tevent_req
*req
;
534 struct smbd_smb2_tdis_state
*state
;
535 struct tevent_req
*subreq
;
536 struct smbXsrv_connection
*xconn
= NULL
;
538 req
= tevent_req_create(mem_ctx
, &state
,
539 struct smbd_smb2_tdis_state
);
543 state
->smb2req
= smb2req
;
545 state
->wait_queue
= tevent_queue_create(state
, "tdis_wait_queue");
546 if (tevent_req_nomem(state
->wait_queue
, req
)) {
547 return tevent_req_post(req
, ev
);
551 * Make sure that no new request will be able to use this tcon.
553 smb2req
->tcon
->status
= NT_STATUS_NETWORK_NAME_DELETED
;
555 xconn
= smb2req
->xconn
->client
->connections
;
556 for (; xconn
!= NULL
; xconn
= xconn
->next
) {
557 struct smbd_smb2_request
*preq
;
559 for (preq
= xconn
->smb2
.requests
; preq
!= NULL
; preq
= preq
->next
) {
560 if (preq
== smb2req
) {
561 /* Can't cancel current request. */
564 if (preq
->tcon
!= smb2req
->tcon
) {
565 /* Request on different tcon. */
570 * Never cancel anything in a compound
571 * request. Way too hard to deal with
574 if (!preq
->compound_related
&& preq
->subreq
!= NULL
) {
575 tevent_req_cancel(preq
->subreq
);
579 * Now wait until the request is finished.
581 * We don't set a callback, as we just want to block the
582 * wait queue and the talloc_free() of the request will
583 * remove the item from the wait queue.
585 subreq
= tevent_queue_wait_send(preq
, ev
, state
->wait_queue
);
586 if (tevent_req_nomem(subreq
, req
)) {
587 return tevent_req_post(req
, ev
);
593 * Now we add our own waiter to the end of the queue,
594 * this way we get notified when all pending requests are finished
595 * and send to the socket.
597 subreq
= tevent_queue_wait_send(state
, ev
, state
->wait_queue
);
598 if (tevent_req_nomem(subreq
, req
)) {
599 return tevent_req_post(req
, ev
);
601 tevent_req_set_callback(subreq
, smbd_smb2_tdis_wait_done
, req
);
606 static void smbd_smb2_tdis_wait_done(struct tevent_req
*subreq
)
608 struct tevent_req
*req
= tevent_req_callback_data(
609 subreq
, struct tevent_req
);
610 struct smbd_smb2_tdis_state
*state
= tevent_req_data(
611 req
, struct smbd_smb2_tdis_state
);
614 tevent_queue_wait_recv(subreq
);
618 * As we've been awoken, we may have changed
619 * uid in the meantime. Ensure we're still
620 * root (SMB2_OP_TDIS has .as_root = true).
622 change_to_root_user();
624 status
= smbXsrv_tcon_disconnect(state
->smb2req
->tcon
,
625 state
->smb2req
->tcon
->compat
->vuid
);
626 if (tevent_req_nterror(req
, status
)) {
630 /* We did tear down the tcon. */
631 TALLOC_FREE(state
->smb2req
->tcon
);
632 tevent_req_done(req
);
635 static NTSTATUS
smbd_smb2_tdis_recv(struct tevent_req
*req
)
637 return tevent_req_simple_recv_ntstatus(req
);