smbd:smb2: only enable encryption in tcon if desired
[Samba.git] / source3 / smbd / smb2_tcon.c
blob014264d9abd2a2bbb15a2e7f3f93b349edaf9999
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"
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,
34 const char *in_path);
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;
50 char *in_path_string;
51 size_t in_path_string_size;
52 NTSTATUS status;
53 bool ok;
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,
77 in_path_buffer.data,
78 in_path_buffer.length,
79 &in_path_string,
80 &in_path_string_size);
81 if (!ok) {
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,
94 req->sconn->ev_ctx,
95 req,
96 in_path_string);
97 if (subreq == NULL) {
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);
110 uint8_t *outhdr;
111 DATA_BLOB outbody;
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 NTSTATUS status;
118 NTSTATUS error;
120 status = smbd_smb2_tree_connect_recv(subreq,
121 &out_share_type,
122 &out_share_flags,
123 &out_capabilities,
124 &out_maximal_access,
125 &out_tree_id);
126 TALLOC_FREE(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,
131 nt_errstr(error));
132 return;
134 return;
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,
144 nt_errstr(error));
145 return;
147 return;
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,
166 nt_errstr(error));
167 return;
171 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
172 const char *in_path,
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;
182 int snum = -1;
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;
187 NTSTATUS status;
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, '\\');
194 if (p) {
195 share = p + 1;
199 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
200 in_path, share));
202 service = talloc_strdup(talloc_tos(), share);
203 if(!service) {
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 "
218 "time\n",
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)
224 && strequal(service,
225 lp_servicename(talloc_tos(), compat_vuser->homes_snum))) {
226 snum = compat_vuser->homes_snum;
227 } else {
228 snum = find_service(talloc_tos(), service, &service);
229 if (!service) {
230 return NT_STATUS_NO_MEMORY;
234 if (snum < 0) {
235 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
236 service));
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",
256 service));
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)) {
272 return status;
275 tcon->encryption_desired = encryption_desired;
276 tcon->global->encryption_required = encryption_required;
278 compat_conn = make_connection_smb2(req->sconn,
279 tcon, snum,
280 req->session->compat,
281 "???",
282 &status);
283 if (compat_conn == NULL) {
284 TALLOC_FREE(tcon);
285 return status;
288 tcon->global->share_name = lp_servicename(tcon->global,
289 SNUM(compat_conn));
290 if (tcon->global->share_name == NULL) {
291 conn_free(compat_conn);
292 TALLOC_FREE(tcon);
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)) {
304 TALLOC_FREE(tcon);
305 return 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;
312 } else {
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;
321 } else {
322 *out_capabilities = 0;
325 switch(lp_csc_policy(SNUM(tcon->compat))) {
326 case CSC_POLICY_MANUAL:
327 break;
328 case CSC_POLICY_DOCUMENTS:
329 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
330 break;
331 case CSC_POLICY_PROGRAMS:
332 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
333 break;
334 case CSC_POLICY_DISABLE:
335 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
336 break;
337 default:
338 break;
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;
353 return NT_STATUS_OK;
356 struct smbd_smb2_tree_connect_state {
357 const char *in_path;
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,
368 const char *in_path)
370 struct tevent_req *req;
371 struct smbd_smb2_tree_connect_state *state;
372 NTSTATUS status;
374 req = tevent_req_create(mem_ctx, &state,
375 struct smbd_smb2_tree_connect_state);
376 if (req == NULL) {
377 return NULL;
379 state->in_path = in_path;
381 status = smbd_smb2_tree_connect(smb2req,
382 state->in_path,
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 =
404 tevent_req_data(req,
405 struct smbd_smb2_tree_connect_state);
406 NTSTATUS status;
408 if (tevent_req_is_nterror(req, &status)) {
409 tevent_req_received(req);
410 return status;
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);
420 return NT_STATUS_OK;
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)
431 NTSTATUS status;
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);
457 DATA_BLOB outbody;
458 NTSTATUS status;
459 NTSTATUS error;
461 status = smbd_smb2_tdis_recv(subreq);
462 TALLOC_FREE(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,
467 nt_errstr(error));
468 return;
470 return;
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,
478 nt_errstr(error));
479 return;
481 return;
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,
490 nt_errstr(error));
491 return;
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);
513 if (req == NULL) {
514 return NULL;
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. */
531 continue;
533 if (preq->tcon != smb2req->tcon) {
534 /* Request on different tcon. */
535 continue;
539 * Never cancel anything in a compound
540 * request. Way too hard to deal with
541 * the result.
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);
571 return 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);
580 NTSTATUS status;
582 smbd_tevent_queue_wait_recv(subreq);
583 TALLOC_FREE(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)) {
595 return;
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);