tests/krb5: Add more encryption type constants
[Samba.git] / source3 / smbd / smb2_tcon.c
blob14229366efa9ca3c309febabf9eb0751f58a56da
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
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/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../libcli/security/security.h"
26 #include "auth.h"
27 #include "lib/param/loadparm.h"
28 #include "../lib/util/tevent_ntstatus.h"
30 #undef DBGC_CLASS
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,
36 uint16_t in_flags,
37 const char *in_path);
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,
44 bool *disconnect);
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;
52 uint16_t in_flags;
53 uint16_t in_path_offset;
54 uint16_t in_path_length;
55 DATA_BLOB in_path_buffer;
56 char *in_path_string;
57 size_t in_path_string_size;
58 NTSTATUS status;
59 bool ok;
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);
70 } else {
71 in_flags = 0;
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,
88 in_path_buffer.data,
89 in_path_buffer.length,
90 &in_path_string,
91 &in_path_string_size);
92 if (!ok) {
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,
105 req->sconn->ev_ctx,
106 req,
107 in_flags,
108 in_path_string);
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);
126 uint8_t *outhdr;
127 DATA_BLOB outbody;
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;
134 NTSTATUS status;
135 NTSTATUS error;
137 status = smbd_smb2_tree_connect_recv(subreq,
138 &out_share_type,
139 &out_share_flags,
140 &out_capabilities,
141 &out_maximal_access,
142 &out_tree_id,
143 &disconnect);
144 TALLOC_FREE(subreq);
145 if (!NT_STATUS_IS_OK(status)) {
146 if (disconnect) {
147 smbd_server_connection_terminate(req->xconn,
148 nt_errstr(status));
149 return;
151 error = smbd_smb2_request_error(req, status);
152 if (!NT_STATUS_IS_OK(error)) {
153 smbd_server_connection_terminate(req->xconn,
154 nt_errstr(error));
155 return;
157 return;
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,
167 nt_errstr(error));
168 return;
170 return;
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,
189 nt_errstr(error));
190 return;
194 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
195 const char *in_path,
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,
201 bool *disconnect)
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;
211 int snum = -1;
212 struct smbXsrv_tcon *tcon;
213 NTTIME now = timeval_to_nttime(&req->request_time);
214 connection_struct *compat_conn = NULL;
215 NTSTATUS status;
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;
221 *disconnect = false;
223 if (strncmp(share, "\\\\", 2) == 0) {
224 const char *p = strchr(share+2, '\\');
225 if (p) {
226 share = p + 1;
230 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
231 in_path, share));
233 if (security_session_user_level(session_info, NULL) < SECURITY_USER) {
234 guest_session = true;
237 if (conn->protocol >= PROTOCOL_SMB3_11 && !guest_session) {
238 require_signed_tcon = true;
241 if (require_signed_tcon && !req->do_encryption && !req->do_signing) {
242 DEBUG(1, ("smbd_smb2_tree_connect: reject request to share "
243 "[%s] as '%s\\%s' without encryption or signing. "
244 "Disconnecting.\n",
245 share,
246 req->session->global->auth_session_info->info->domain_name,
247 req->session->global->auth_session_info->info->account_name));
248 *disconnect = true;
249 return NT_STATUS_ACCESS_DENIED;
252 service = talloc_strdup(talloc_tos(), share);
253 if(!service) {
254 return NT_STATUS_NO_MEMORY;
257 if (!strlower_m(service)) {
258 DEBUG(2, ("strlower_m %s failed\n", service));
259 return NT_STATUS_INVALID_PARAMETER;
262 /* TODO: do more things... */
263 if (strequal(service,HOMES_NAME)) {
264 if (session->homes_snum == -1) {
265 DEBUG(2, ("[homes] share not available for "
266 "user %s because it was not found "
267 "or created at session setup "
268 "time\n",
269 session_info->unix_info->unix_name));
270 return NT_STATUS_BAD_NETWORK_NAME;
272 snum = session->homes_snum;
273 } else if ((session->homes_snum != -1)
274 && strequal(service,
275 lp_servicename(talloc_tos(), lp_sub, session->homes_snum))) {
276 snum = session->homes_snum;
277 } else {
278 snum = find_service(talloc_tos(), service, &service);
279 if (!service) {
280 return NT_STATUS_NO_MEMORY;
284 if (snum < 0) {
285 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
286 service));
287 return NT_STATUS_BAD_NETWORK_NAME;
290 /* Handle non-DFS clients attempting connections to msdfs proxy */
291 if (lp_host_msdfs()) {
292 char *proxy = lp_msdfs_proxy(talloc_tos(), lp_sub, snum);
294 if ((proxy != NULL) && (*proxy != '\0')) {
295 DBG_NOTICE("refusing connection to dfs proxy share "
296 "'%s' (pointing to %s)\n",
297 service,
298 proxy);
299 TALLOC_FREE(proxy);
300 return NT_STATUS_BAD_NETWORK_NAME;
302 TALLOC_FREE(proxy);
305 if ((lp_server_smb_encrypt(snum) >= SMB_ENCRYPTION_DESIRED) &&
306 (conn->smb2.server.cipher != 0))
308 encryption_desired = true;
311 if (lp_server_smb_encrypt(snum) == SMB_ENCRYPTION_REQUIRED) {
312 encryption_desired = true;
313 encryption_required = true;
316 if (guest_session && encryption_required) {
317 DEBUG(1,("reject guest as encryption is required for service %s\n",
318 service));
319 return NT_STATUS_ACCESS_DENIED;
322 if (conn->smb2.server.cipher == 0) {
323 if (encryption_required) {
324 DEBUG(1,("reject tcon with dialect[0x%04X] "
325 "as encryption is required for service %s\n",
326 conn->smb2.server.dialect, service));
327 return NT_STATUS_ACCESS_DENIED;
331 /* create a new tcon as child of the session */
332 status = smb2srv_tcon_create(req->session, now, &tcon);
333 if (!NT_STATUS_IS_OK(status)) {
334 return status;
337 if (encryption_desired) {
338 tcon->global->encryption_flags |= SMBXSRV_ENCRYPTION_DESIRED;
340 if (encryption_required) {
341 tcon->global->encryption_flags |= SMBXSRV_ENCRYPTION_REQUIRED;
344 compat_conn = make_connection_smb2(req,
345 tcon, snum,
346 "???",
347 &status);
348 if (compat_conn == NULL) {
349 TALLOC_FREE(tcon);
350 return status;
353 tcon->global->share_name = lp_servicename(tcon->global,
354 lp_sub,
355 SNUM(compat_conn));
356 if (tcon->global->share_name == NULL) {
357 conn_free(compat_conn);
358 TALLOC_FREE(tcon);
359 return NT_STATUS_NO_MEMORY;
361 tcon->global->session_global_id =
362 req->session->global->session_global_id;
364 tcon->compat = talloc_move(tcon, &compat_conn);
366 tcon->status = NT_STATUS_OK;
368 status = smbXsrv_tcon_update(tcon);
369 if (!NT_STATUS_IS_OK(status)) {
370 TALLOC_FREE(tcon);
371 return status;
374 if (IS_PRINT(tcon->compat)) {
375 *out_share_type = SMB2_SHARE_TYPE_PRINT;
376 } else if (IS_IPC(tcon->compat)) {
377 *out_share_type = SMB2_SHARE_TYPE_PIPE;
378 } else {
379 *out_share_type = SMB2_SHARE_TYPE_DISK;
382 *out_share_flags = 0;
384 if (lp_msdfs_root(SNUM(tcon->compat)) && lp_host_msdfs()) {
385 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
386 *out_capabilities = SMB2_SHARE_CAP_DFS;
387 } else {
388 *out_capabilities = 0;
391 switch(lp_csc_policy(SNUM(tcon->compat))) {
392 case CSC_POLICY_MANUAL:
393 break;
394 case CSC_POLICY_DOCUMENTS:
395 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
396 break;
397 case CSC_POLICY_PROGRAMS:
398 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
399 break;
400 case CSC_POLICY_DISABLE:
401 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
402 break;
403 default:
404 break;
407 if (lp_hide_unreadable(SNUM(tcon->compat)) ||
408 lp_hide_unwriteable_files(SNUM(tcon->compat))) {
409 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
412 if (encryption_desired) {
413 *out_share_flags |= SMB2_SHAREFLAG_ENCRYPT_DATA;
416 *out_maximal_access = tcon->compat->share_access;
418 *out_tree_id = tcon->global->tcon_wire_id;
419 req->last_tid = tcon->global->tcon_wire_id;
421 return NT_STATUS_OK;
424 struct smbd_smb2_tree_connect_state {
425 const char *in_path;
426 uint8_t out_share_type;
427 uint32_t out_share_flags;
428 uint32_t out_capabilities;
429 uint32_t out_maximal_access;
430 uint32_t out_tree_id;
431 bool disconnect;
434 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
435 struct tevent_context *ev,
436 struct smbd_smb2_request *smb2req,
437 uint16_t in_flags,
438 const char *in_path)
440 struct tevent_req *req;
441 struct smbd_smb2_tree_connect_state *state;
442 NTSTATUS status;
444 req = tevent_req_create(mem_ctx, &state,
445 struct smbd_smb2_tree_connect_state);
446 if (req == NULL) {
447 return NULL;
449 state->in_path = in_path;
451 status = smbd_smb2_tree_connect(smb2req,
452 state->in_path,
453 &state->out_share_type,
454 &state->out_share_flags,
455 &state->out_capabilities,
456 &state->out_maximal_access,
457 &state->out_tree_id,
458 &state->disconnect);
459 if (tevent_req_nterror(req, status)) {
460 return tevent_req_post(req, ev);
463 tevent_req_done(req);
464 return tevent_req_post(req, ev);
467 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
468 uint8_t *out_share_type,
469 uint32_t *out_share_flags,
470 uint32_t *out_capabilities,
471 uint32_t *out_maximal_access,
472 uint32_t *out_tree_id,
473 bool *disconnect)
475 struct smbd_smb2_tree_connect_state *state =
476 tevent_req_data(req,
477 struct smbd_smb2_tree_connect_state);
478 NTSTATUS status;
480 if (tevent_req_is_nterror(req, &status)) {
481 tevent_req_received(req);
482 return status;
485 *out_share_type = state->out_share_type;
486 *out_share_flags = state->out_share_flags;
487 *out_capabilities = state->out_capabilities;
488 *out_maximal_access = state->out_maximal_access;
489 *out_tree_id = state->out_tree_id;
490 *disconnect = state->disconnect;
492 tevent_req_received(req);
493 return NT_STATUS_OK;
496 static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
497 struct tevent_context *ev,
498 struct smbd_smb2_request *smb2req);
499 static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req);
500 static void smbd_smb2_request_tdis_done(struct tevent_req *subreq);
502 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
504 NTSTATUS status;
505 struct tevent_req *subreq = NULL;
507 status = smbd_smb2_request_verify_sizes(req, 0x04);
508 if (!NT_STATUS_IS_OK(status)) {
509 return smbd_smb2_request_error(req, status);
512 subreq = smbd_smb2_tdis_send(req, req->sconn->ev_ctx, req);
513 if (subreq == NULL) {
514 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
516 tevent_req_set_callback(subreq, smbd_smb2_request_tdis_done, req);
519 * Avoid sending a STATUS_PENDING message, it's very likely
520 * the client won't expect that.
522 return smbd_smb2_request_pending_queue(req, subreq, 0);
525 static void smbd_smb2_request_tdis_done(struct tevent_req *subreq)
527 struct smbd_smb2_request *smb2req =
528 tevent_req_callback_data(subreq,
529 struct smbd_smb2_request);
530 DATA_BLOB outbody;
531 NTSTATUS status;
532 NTSTATUS error;
534 status = smbd_smb2_tdis_recv(subreq);
535 TALLOC_FREE(subreq);
536 if (!NT_STATUS_IS_OK(status)) {
537 error = smbd_smb2_request_error(smb2req, status);
538 if (!NT_STATUS_IS_OK(error)) {
539 smbd_server_connection_terminate(smb2req->xconn,
540 nt_errstr(error));
541 return;
543 return;
546 outbody = smbd_smb2_generate_outbody(smb2req, 0x04);
547 if (outbody.data == NULL) {
548 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
549 if (!NT_STATUS_IS_OK(error)) {
550 smbd_server_connection_terminate(smb2req->xconn,
551 nt_errstr(error));
552 return;
554 return;
557 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
558 SSVAL(outbody.data, 0x02, 0); /* reserved */
560 error = smbd_smb2_request_done(smb2req, outbody, NULL);
561 if (!NT_STATUS_IS_OK(error)) {
562 smbd_server_connection_terminate(smb2req->xconn,
563 nt_errstr(error));
564 return;
568 struct smbd_smb2_tdis_state {
569 struct smbd_smb2_request *smb2req;
570 struct tevent_queue *wait_queue;
573 static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq);
575 static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
576 struct tevent_context *ev,
577 struct smbd_smb2_request *smb2req)
579 struct tevent_req *req;
580 struct smbd_smb2_tdis_state *state;
581 struct tevent_req *subreq;
582 struct smbXsrv_connection *xconn = NULL;
584 req = tevent_req_create(mem_ctx, &state,
585 struct smbd_smb2_tdis_state);
586 if (req == NULL) {
587 return NULL;
589 state->smb2req = smb2req;
591 state->wait_queue = tevent_queue_create(state, "tdis_wait_queue");
592 if (tevent_req_nomem(state->wait_queue, req)) {
593 return tevent_req_post(req, ev);
597 * Make sure that no new request will be able to use this tcon.
599 smb2req->tcon->status = NT_STATUS_NETWORK_NAME_DELETED;
601 xconn = smb2req->xconn->client->connections;
602 for (; xconn != NULL; xconn = xconn->next) {
603 struct smbd_smb2_request *preq;
605 for (preq = xconn->smb2.requests; preq != NULL; preq = preq->next) {
606 if (preq == smb2req) {
607 /* Can't cancel current request. */
608 continue;
610 if (preq->tcon != smb2req->tcon) {
611 /* Request on different tcon. */
612 continue;
615 if (preq->subreq != NULL) {
616 tevent_req_cancel(preq->subreq);
620 * Now wait until the request is finished.
622 * We don't set a callback, as we just want to block the
623 * wait queue and the talloc_free() of the request will
624 * remove the item from the wait queue.
626 subreq = tevent_queue_wait_send(preq, ev, state->wait_queue);
627 if (tevent_req_nomem(subreq, req)) {
628 return tevent_req_post(req, ev);
634 * Now we add our own waiter to the end of the queue,
635 * this way we get notified when all pending requests are finished
636 * and send to the socket.
638 subreq = tevent_queue_wait_send(state, ev, state->wait_queue);
639 if (tevent_req_nomem(subreq, req)) {
640 return tevent_req_post(req, ev);
642 tevent_req_set_callback(subreq, smbd_smb2_tdis_wait_done, req);
644 return req;
647 static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq)
649 struct tevent_req *req = tevent_req_callback_data(
650 subreq, struct tevent_req);
651 struct smbd_smb2_tdis_state *state = tevent_req_data(
652 req, struct smbd_smb2_tdis_state);
653 NTSTATUS status;
655 tevent_queue_wait_recv(subreq);
656 TALLOC_FREE(subreq);
659 * As we've been awoken, we may have changed
660 * uid in the meantime. Ensure we're still
661 * root (SMB2_OP_TDIS has .as_root = true).
663 change_to_root_user();
665 status = smbXsrv_tcon_disconnect(state->smb2req->tcon,
666 state->smb2req->tcon->compat->vuid);
667 if (tevent_req_nterror(req, status)) {
668 return;
671 /* We did tear down the tcon. */
672 TALLOC_FREE(state->smb2req->tcon);
673 tevent_req_done(req);
676 static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req)
678 return tevent_req_simple_recv_ntstatus(req);