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 uint16_t in_path_offset
;
47 uint16_t in_path_length
;
48 DATA_BLOB in_path_buffer
;
50 size_t in_path_string_size
;
53 struct tevent_req
*subreq
;
55 status
= smbd_smb2_request_verify_sizes(req
, 0x09);
56 if (!NT_STATUS_IS_OK(status
)) {
57 return smbd_smb2_request_error(req
, status
);
59 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
61 in_path_offset
= SVAL(inbody
, 0x04);
62 in_path_length
= SVAL(inbody
, 0x06);
64 if (in_path_offset
!= (SMB2_HDR_BODY
+ SMBD_SMB2_IN_BODY_LEN(req
))) {
65 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
68 if (in_path_length
> SMBD_SMB2_IN_DYN_LEN(req
)) {
69 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
72 in_path_buffer
.data
= SMBD_SMB2_IN_DYN_PTR(req
);
73 in_path_buffer
.length
= in_path_length
;
75 ok
= convert_string_talloc(req
, CH_UTF16
, CH_UNIX
,
77 in_path_buffer
.length
,
79 &in_path_string_size
);
81 return smbd_smb2_request_error(req
, NT_STATUS_ILLEGAL_CHARACTER
);
84 if (in_path_buffer
.length
== 0) {
85 in_path_string_size
= 0;
88 if (strlen(in_path_string
) != in_path_string_size
) {
89 return smbd_smb2_request_error(req
, NT_STATUS_BAD_NETWORK_NAME
);
92 subreq
= smbd_smb2_tree_connect_send(req
,
97 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
99 tevent_req_set_callback(subreq
, smbd_smb2_request_tcon_done
, req
);
101 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
104 static void smbd_smb2_request_tcon_done(struct tevent_req
*subreq
)
106 struct smbd_smb2_request
*req
=
107 tevent_req_callback_data(subreq
,
108 struct smbd_smb2_request
);
111 uint8_t out_share_type
= 0;
112 uint32_t out_share_flags
= 0;
113 uint32_t out_capabilities
= 0;
114 uint32_t out_maximal_access
= 0;
115 uint32_t out_tree_id
= 0;
119 status
= smbd_smb2_tree_connect_recv(subreq
,
126 if (!NT_STATUS_IS_OK(status
)) {
127 error
= smbd_smb2_request_error(req
, status
);
128 if (!NT_STATUS_IS_OK(error
)) {
129 smbd_server_connection_terminate(req
->xconn
,
136 outhdr
= SMBD_SMB2_OUT_HDR_PTR(req
);
138 outbody
= smbd_smb2_generate_outbody(req
, 0x10);
139 if (outbody
.data
== NULL
) {
140 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
141 if (!NT_STATUS_IS_OK(error
)) {
142 smbd_server_connection_terminate(req
->xconn
,
149 SIVAL(outhdr
, SMB2_HDR_TID
, out_tree_id
);
151 SSVAL(outbody
.data
, 0x00, 0x10); /* struct size */
152 SCVAL(outbody
.data
, 0x02,
153 out_share_type
); /* share type */
154 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
155 SIVAL(outbody
.data
, 0x04,
156 out_share_flags
); /* share flags */
157 SIVAL(outbody
.data
, 0x08,
158 out_capabilities
); /* capabilities */
159 SIVAL(outbody
.data
, 0x0C,
160 out_maximal_access
); /* maximal access */
162 error
= smbd_smb2_request_done(req
, outbody
, NULL
);
163 if (!NT_STATUS_IS_OK(error
)) {
164 smbd_server_connection_terminate(req
->xconn
,
170 static NTSTATUS
smbd_smb2_tree_connect(struct smbd_smb2_request
*req
,
172 uint8_t *out_share_type
,
173 uint32_t *out_share_flags
,
174 uint32_t *out_capabilities
,
175 uint32_t *out_maximal_access
,
176 uint32_t *out_tree_id
)
178 struct smbXsrv_connection
*conn
= req
->xconn
;
179 const char *share
= in_path
;
180 char *service
= NULL
;
182 struct smbXsrv_tcon
*tcon
;
183 NTTIME now
= timeval_to_nttime(&req
->request_time
);
184 connection_struct
*compat_conn
= NULL
;
185 struct user_struct
*compat_vuser
= req
->session
->compat
;
187 bool encryption_required
= req
->session
->global
->encryption_required
;
188 bool guest_session
= false;
190 if (strncmp(share
, "\\\\", 2) == 0) {
191 const char *p
= strchr(share
+2, '\\');
197 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
200 service
= talloc_strdup(talloc_tos(), share
);
202 return NT_STATUS_NO_MEMORY
;
205 if (!strlower_m(service
)) {
206 DEBUG(2, ("strlower_m %s failed\n", service
));
207 return NT_STATUS_INVALID_PARAMETER
;
210 /* TODO: do more things... */
211 if (strequal(service
,HOMES_NAME
)) {
212 if (compat_vuser
->homes_snum
== -1) {
213 DEBUG(2, ("[homes] share not available for "
214 "user %s because it was not found "
215 "or created at session setup "
217 compat_vuser
->session_info
->unix_info
->unix_name
));
218 return NT_STATUS_BAD_NETWORK_NAME
;
220 snum
= compat_vuser
->homes_snum
;
221 } else if ((compat_vuser
->homes_snum
!= -1)
223 lp_servicename(talloc_tos(), compat_vuser
->homes_snum
))) {
224 snum
= compat_vuser
->homes_snum
;
226 snum
= find_service(talloc_tos(), service
, &service
);
228 return NT_STATUS_NO_MEMORY
;
233 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
235 return NT_STATUS_BAD_NETWORK_NAME
;
238 if ((lp_smb_encrypt(snum
) > SMB_SIGNING_OFF
) &&
239 (conn
->smb2
.client
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
240 encryption_required
= true;
243 if (lp_smb_encrypt(snum
) == SMB_SIGNING_REQUIRED
) {
244 encryption_required
= true;
247 if (security_session_user_level(compat_vuser
->session_info
, NULL
) < SECURITY_USER
) {
248 guest_session
= true;
251 if (guest_session
&& encryption_required
) {
252 DEBUG(1,("reject guest as encryption is required for service %s\n",
254 return NT_STATUS_ACCESS_DENIED
;
257 if (conn
->smb2
.server
.cipher
== 0) {
258 if (encryption_required
) {
259 DEBUG(1,("reject tcon with dialect[0x%04X] "
260 "as encryption is required for service %s\n",
261 conn
->smb2
.server
.dialect
, service
));
262 return NT_STATUS_ACCESS_DENIED
;
266 /* create a new tcon as child of the session */
267 status
= smb2srv_tcon_create(req
->session
, now
, &tcon
);
268 if (!NT_STATUS_IS_OK(status
)) {
272 tcon
->global
->encryption_required
= encryption_required
;
274 compat_conn
= make_connection_smb2(req
,
276 req
->session
->compat
,
279 if (compat_conn
== NULL
) {
284 tcon
->global
->share_name
= lp_servicename(tcon
->global
,
286 if (tcon
->global
->share_name
== NULL
) {
287 conn_free(compat_conn
);
289 return NT_STATUS_NO_MEMORY
;
291 tcon
->global
->session_global_id
=
292 req
->session
->global
->session_global_id
;
294 tcon
->compat
= talloc_move(tcon
, &compat_conn
);
296 tcon
->status
= NT_STATUS_OK
;
298 status
= smbXsrv_tcon_update(tcon
);
299 if (!NT_STATUS_IS_OK(status
)) {
304 if (IS_PRINT(tcon
->compat
)) {
305 *out_share_type
= SMB2_SHARE_TYPE_PRINT
;
306 } else if (IS_IPC(tcon
->compat
)) {
307 *out_share_type
= SMB2_SHARE_TYPE_PIPE
;
309 *out_share_type
= SMB2_SHARE_TYPE_DISK
;
312 *out_share_flags
= 0;
314 if (lp_msdfs_root(SNUM(tcon
->compat
)) && lp_host_msdfs()) {
315 *out_share_flags
|= (SMB2_SHAREFLAG_DFS
|SMB2_SHAREFLAG_DFS_ROOT
);
316 *out_capabilities
= SMB2_SHARE_CAP_DFS
;
318 *out_capabilities
= 0;
321 switch(lp_csc_policy(SNUM(tcon
->compat
))) {
322 case CSC_POLICY_MANUAL
:
324 case CSC_POLICY_DOCUMENTS
:
325 *out_share_flags
|= SMB2_SHAREFLAG_AUTO_CACHING
;
327 case CSC_POLICY_PROGRAMS
:
328 *out_share_flags
|= SMB2_SHAREFLAG_VDO_CACHING
;
330 case CSC_POLICY_DISABLE
:
331 *out_share_flags
|= SMB2_SHAREFLAG_NO_CACHING
;
337 if (lp_hide_unreadable(SNUM(tcon
->compat
)) ||
338 lp_hide_unwriteable_files(SNUM(tcon
->compat
))) {
339 *out_share_flags
|= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM
;
342 if (encryption_required
) {
343 *out_share_flags
|= SMB2_SHAREFLAG_ENCRYPT_DATA
;
346 *out_maximal_access
= tcon
->compat
->share_access
;
348 *out_tree_id
= tcon
->global
->tcon_wire_id
;
352 struct smbd_smb2_tree_connect_state
{
354 uint8_t out_share_type
;
355 uint32_t out_share_flags
;
356 uint32_t out_capabilities
;
357 uint32_t out_maximal_access
;
358 uint32_t out_tree_id
;
361 static struct tevent_req
*smbd_smb2_tree_connect_send(TALLOC_CTX
*mem_ctx
,
362 struct tevent_context
*ev
,
363 struct smbd_smb2_request
*smb2req
,
366 struct tevent_req
*req
;
367 struct smbd_smb2_tree_connect_state
*state
;
370 req
= tevent_req_create(mem_ctx
, &state
,
371 struct smbd_smb2_tree_connect_state
);
375 state
->in_path
= in_path
;
377 status
= smbd_smb2_tree_connect(smb2req
,
379 &state
->out_share_type
,
380 &state
->out_share_flags
,
381 &state
->out_capabilities
,
382 &state
->out_maximal_access
,
383 &state
->out_tree_id
);
384 if (tevent_req_nterror(req
, status
)) {
385 return tevent_req_post(req
, ev
);
388 tevent_req_done(req
);
389 return tevent_req_post(req
, ev
);
392 static NTSTATUS
smbd_smb2_tree_connect_recv(struct tevent_req
*req
,
393 uint8_t *out_share_type
,
394 uint32_t *out_share_flags
,
395 uint32_t *out_capabilities
,
396 uint32_t *out_maximal_access
,
397 uint32_t *out_tree_id
)
399 struct smbd_smb2_tree_connect_state
*state
=
401 struct smbd_smb2_tree_connect_state
);
404 if (tevent_req_is_nterror(req
, &status
)) {
405 tevent_req_received(req
);
409 *out_share_type
= state
->out_share_type
;
410 *out_share_flags
= state
->out_share_flags
;
411 *out_capabilities
= state
->out_capabilities
;
412 *out_maximal_access
= state
->out_maximal_access
;
413 *out_tree_id
= state
->out_tree_id
;
415 tevent_req_received(req
);
419 static struct tevent_req
*smbd_smb2_tdis_send(TALLOC_CTX
*mem_ctx
,
420 struct tevent_context
*ev
,
421 struct smbd_smb2_request
*smb2req
);
422 static NTSTATUS
smbd_smb2_tdis_recv(struct tevent_req
*req
);
423 static void smbd_smb2_request_tdis_done(struct tevent_req
*subreq
);
425 NTSTATUS
smbd_smb2_request_process_tdis(struct smbd_smb2_request
*req
)
428 struct tevent_req
*subreq
= NULL
;
430 status
= smbd_smb2_request_verify_sizes(req
, 0x04);
431 if (!NT_STATUS_IS_OK(status
)) {
432 return smbd_smb2_request_error(req
, status
);
435 subreq
= smbd_smb2_tdis_send(req
, req
->sconn
->ev_ctx
, req
);
436 if (subreq
== NULL
) {
437 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
439 tevent_req_set_callback(subreq
, smbd_smb2_request_tdis_done
, req
);
442 * Wait a long time before going async on this to allow
443 * requests we're waiting on to finish. Set timeout to 10 secs.
445 return smbd_smb2_request_pending_queue(req
, subreq
, 10000000);
448 static void smbd_smb2_request_tdis_done(struct tevent_req
*subreq
)
450 struct smbd_smb2_request
*smb2req
=
451 tevent_req_callback_data(subreq
,
452 struct smbd_smb2_request
);
457 status
= smbd_smb2_tdis_recv(subreq
);
459 if (!NT_STATUS_IS_OK(status
)) {
460 error
= smbd_smb2_request_error(smb2req
, status
);
461 if (!NT_STATUS_IS_OK(error
)) {
462 smbd_server_connection_terminate(smb2req
->xconn
,
469 outbody
= smbd_smb2_generate_outbody(smb2req
, 0x04);
470 if (outbody
.data
== NULL
) {
471 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
472 if (!NT_STATUS_IS_OK(error
)) {
473 smbd_server_connection_terminate(smb2req
->xconn
,
480 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
481 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
483 error
= smbd_smb2_request_done(smb2req
, outbody
, NULL
);
484 if (!NT_STATUS_IS_OK(error
)) {
485 smbd_server_connection_terminate(smb2req
->xconn
,
491 struct smbd_smb2_tdis_state
{
492 struct smbd_smb2_request
*smb2req
;
493 struct tevent_queue
*wait_queue
;
496 static void smbd_smb2_tdis_wait_done(struct tevent_req
*subreq
);
498 static struct tevent_req
*smbd_smb2_tdis_send(TALLOC_CTX
*mem_ctx
,
499 struct tevent_context
*ev
,
500 struct smbd_smb2_request
*smb2req
)
502 struct tevent_req
*req
;
503 struct smbd_smb2_tdis_state
*state
;
504 struct tevent_req
*subreq
;
505 struct smbd_smb2_request
*preq
;
506 struct smbXsrv_connection
*xconn
= smb2req
->xconn
;
508 req
= tevent_req_create(mem_ctx
, &state
,
509 struct smbd_smb2_tdis_state
);
513 state
->smb2req
= smb2req
;
515 state
->wait_queue
= tevent_queue_create(state
, "tdis_wait_queue");
516 if (tevent_req_nomem(state
->wait_queue
, req
)) {
517 return tevent_req_post(req
, ev
);
521 * Make sure that no new request will be able to use this tcon.
523 smb2req
->tcon
->status
= NT_STATUS_NETWORK_NAME_DELETED
;
525 for (preq
= xconn
->smb2
.requests
; preq
!= NULL
; preq
= preq
->next
) {
526 if (preq
== smb2req
) {
527 /* Can't cancel current request. */
530 if (preq
->tcon
!= smb2req
->tcon
) {
531 /* Request on different tcon. */
536 * Never cancel anything in a compound
537 * request. Way too hard to deal with
540 if (!preq
->compound_related
&& preq
->subreq
!= NULL
) {
541 tevent_req_cancel(preq
->subreq
);
545 * Now wait until the request is finished.
547 * We don't set a callback, as we just want to block the
548 * wait queue and the talloc_free() of the request will
549 * remove the item from the wait queue.
551 subreq
= tevent_queue_wait_send(preq
, ev
, state
->wait_queue
);
552 if (tevent_req_nomem(subreq
, req
)) {
553 return tevent_req_post(req
, ev
);
558 * Now we add our own waiter to the end of the queue,
559 * this way we get notified when all pending requests are finished
560 * and send to the socket.
562 subreq
= tevent_queue_wait_send(state
, ev
, state
->wait_queue
);
563 if (tevent_req_nomem(subreq
, req
)) {
564 return tevent_req_post(req
, ev
);
566 tevent_req_set_callback(subreq
, smbd_smb2_tdis_wait_done
, req
);
571 static void smbd_smb2_tdis_wait_done(struct tevent_req
*subreq
)
573 struct tevent_req
*req
= tevent_req_callback_data(
574 subreq
, struct tevent_req
);
575 struct smbd_smb2_tdis_state
*state
= tevent_req_data(
576 req
, struct smbd_smb2_tdis_state
);
579 tevent_queue_wait_recv(subreq
);
583 * As we've been awoken, we may have changed
584 * uid in the meantime. Ensure we're still
585 * root (SMB2_OP_TDIS has .as_root = true).
587 change_to_root_user();
589 status
= smbXsrv_tcon_disconnect(state
->smb2req
->tcon
,
590 state
->smb2req
->tcon
->compat
->vuid
);
591 if (tevent_req_nterror(req
, status
)) {
595 /* We did tear down the tcon. */
596 TALLOC_FREE(state
->smb2req
->tcon
);
597 tevent_req_done(req
);
600 static NTSTATUS
smbd_smb2_tdis_recv(struct tevent_req
*req
)
602 return tevent_req_simple_recv_ntstatus(req
);