lib: Remove unused parmlist code
[Samba.git] / source3 / smbd / smb2_tcon.c
blob99e2f215ca4d98ec6135b2769afbaafb10d2df48
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 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
31 struct tevent_context *ev,
32 struct smbd_smb2_request *smb2req,
33 const char *in_path);
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,
40 bool *disconnect);
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 bool disconnect = false;
118 NTSTATUS status;
119 NTSTATUS error;
121 status = smbd_smb2_tree_connect_recv(subreq,
122 &out_share_type,
123 &out_share_flags,
124 &out_capabilities,
125 &out_maximal_access,
126 &out_tree_id,
127 &disconnect);
128 TALLOC_FREE(subreq);
129 if (!NT_STATUS_IS_OK(status)) {
130 if (disconnect) {
131 smbd_server_connection_terminate(req->xconn,
132 nt_errstr(status));
133 return;
135 error = smbd_smb2_request_error(req, status);
136 if (!NT_STATUS_IS_OK(error)) {
137 smbd_server_connection_terminate(req->xconn,
138 nt_errstr(error));
139 return;
141 return;
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,
151 nt_errstr(error));
152 return;
154 return;
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,
173 nt_errstr(error));
174 return;
178 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
179 const char *in_path,
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,
185 bool *disconnect)
187 struct smbXsrv_connection *conn = req->xconn;
188 const char *share = in_path;
189 char *service = NULL;
190 int snum = -1;
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;
195 NTSTATUS status;
196 bool encryption_desired = req->session->encryption_desired;
197 bool encryption_required = req->session->global->encryption_required;
198 bool guest_session = false;
199 bool require_signed_tcon = false;
201 *disconnect = false;
203 if (strncmp(share, "\\\\", 2) == 0) {
204 const char *p = strchr(share+2, '\\');
205 if (p) {
206 share = p + 1;
210 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
211 in_path, share));
213 if (security_session_user_level(compat_vuser->session_info, NULL) < SECURITY_USER) {
214 guest_session = true;
217 if (conn->protocol >= PROTOCOL_SMB3_11 && !guest_session) {
218 require_signed_tcon = true;
221 if (require_signed_tcon && !req->do_encryption && !req->do_signing) {
222 DEBUG(1, ("smbd_smb2_tree_connect: reject request to share "
223 "[%s] as '%s\\%s' without encryption or signing. "
224 "Disconnecting.\n",
225 share,
226 req->session->global->auth_session_info->info->domain_name,
227 req->session->global->auth_session_info->info->account_name));
228 *disconnect = true;
229 return NT_STATUS_ACCESS_DENIED;
232 service = talloc_strdup(talloc_tos(), share);
233 if(!service) {
234 return NT_STATUS_NO_MEMORY;
237 if (!strlower_m(service)) {
238 DEBUG(2, ("strlower_m %s failed\n", service));
239 return NT_STATUS_INVALID_PARAMETER;
242 /* TODO: do more things... */
243 if (strequal(service,HOMES_NAME)) {
244 if (compat_vuser->homes_snum == -1) {
245 DEBUG(2, ("[homes] share not available for "
246 "user %s because it was not found "
247 "or created at session setup "
248 "time\n",
249 compat_vuser->session_info->unix_info->unix_name));
250 return NT_STATUS_BAD_NETWORK_NAME;
252 snum = compat_vuser->homes_snum;
253 } else if ((compat_vuser->homes_snum != -1)
254 && strequal(service,
255 lp_servicename(talloc_tos(), compat_vuser->homes_snum))) {
256 snum = compat_vuser->homes_snum;
257 } else {
258 snum = find_service(talloc_tos(), service, &service);
259 if (!service) {
260 return NT_STATUS_NO_MEMORY;
264 if (snum < 0) {
265 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
266 service));
267 return NT_STATUS_BAD_NETWORK_NAME;
270 if ((lp_smb_encrypt(snum) >= SMB_SIGNING_DESIRED) &&
271 (conn->smb2.client.capabilities & SMB2_CAP_ENCRYPTION)) {
272 encryption_desired = true;
275 if (lp_smb_encrypt(snum) == SMB_SIGNING_REQUIRED) {
276 encryption_desired = true;
277 encryption_required = true;
280 if (guest_session && encryption_required) {
281 DEBUG(1,("reject guest as encryption is required for service %s\n",
282 service));
283 return NT_STATUS_ACCESS_DENIED;
286 if (conn->smb2.server.cipher == 0) {
287 if (encryption_required) {
288 DEBUG(1,("reject tcon with dialect[0x%04X] "
289 "as encryption is required for service %s\n",
290 conn->smb2.server.dialect, service));
291 return NT_STATUS_ACCESS_DENIED;
295 /* create a new tcon as child of the session */
296 status = smb2srv_tcon_create(req->session, now, &tcon);
297 if (!NT_STATUS_IS_OK(status)) {
298 return status;
301 tcon->encryption_desired = encryption_desired;
302 tcon->global->encryption_required = encryption_required;
304 compat_conn = make_connection_smb2(req,
305 tcon, snum,
306 req->session->compat,
307 "???",
308 &status);
309 if (compat_conn == NULL) {
310 TALLOC_FREE(tcon);
311 return status;
314 tcon->global->share_name = lp_servicename(tcon->global,
315 SNUM(compat_conn));
316 if (tcon->global->share_name == NULL) {
317 conn_free(compat_conn);
318 TALLOC_FREE(tcon);
319 return NT_STATUS_NO_MEMORY;
321 tcon->global->session_global_id =
322 req->session->global->session_global_id;
324 tcon->compat = talloc_move(tcon, &compat_conn);
326 tcon->status = NT_STATUS_OK;
328 status = smbXsrv_tcon_update(tcon);
329 if (!NT_STATUS_IS_OK(status)) {
330 TALLOC_FREE(tcon);
331 return status;
334 if (IS_PRINT(tcon->compat)) {
335 *out_share_type = SMB2_SHARE_TYPE_PRINT;
336 } else if (IS_IPC(tcon->compat)) {
337 *out_share_type = SMB2_SHARE_TYPE_PIPE;
338 } else {
339 *out_share_type = SMB2_SHARE_TYPE_DISK;
342 *out_share_flags = 0;
344 if (lp_msdfs_root(SNUM(tcon->compat)) && lp_host_msdfs()) {
345 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
346 *out_capabilities = SMB2_SHARE_CAP_DFS;
347 } else {
348 *out_capabilities = 0;
351 switch(lp_csc_policy(SNUM(tcon->compat))) {
352 case CSC_POLICY_MANUAL:
353 break;
354 case CSC_POLICY_DOCUMENTS:
355 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
356 break;
357 case CSC_POLICY_PROGRAMS:
358 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
359 break;
360 case CSC_POLICY_DISABLE:
361 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
362 break;
363 default:
364 break;
367 if (lp_hide_unreadable(SNUM(tcon->compat)) ||
368 lp_hide_unwriteable_files(SNUM(tcon->compat))) {
369 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
372 if (encryption_desired) {
373 *out_share_flags |= SMB2_SHAREFLAG_ENCRYPT_DATA;
376 *out_maximal_access = tcon->compat->share_access;
378 *out_tree_id = tcon->global->tcon_wire_id;
379 return NT_STATUS_OK;
382 struct smbd_smb2_tree_connect_state {
383 const char *in_path;
384 uint8_t out_share_type;
385 uint32_t out_share_flags;
386 uint32_t out_capabilities;
387 uint32_t out_maximal_access;
388 uint32_t out_tree_id;
389 bool disconnect;
392 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
393 struct tevent_context *ev,
394 struct smbd_smb2_request *smb2req,
395 const char *in_path)
397 struct tevent_req *req;
398 struct smbd_smb2_tree_connect_state *state;
399 NTSTATUS status;
401 req = tevent_req_create(mem_ctx, &state,
402 struct smbd_smb2_tree_connect_state);
403 if (req == NULL) {
404 return NULL;
406 state->in_path = in_path;
408 status = smbd_smb2_tree_connect(smb2req,
409 state->in_path,
410 &state->out_share_type,
411 &state->out_share_flags,
412 &state->out_capabilities,
413 &state->out_maximal_access,
414 &state->out_tree_id,
415 &state->disconnect);
416 if (tevent_req_nterror(req, status)) {
417 return tevent_req_post(req, ev);
420 tevent_req_done(req);
421 return tevent_req_post(req, ev);
424 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
425 uint8_t *out_share_type,
426 uint32_t *out_share_flags,
427 uint32_t *out_capabilities,
428 uint32_t *out_maximal_access,
429 uint32_t *out_tree_id,
430 bool *disconnect)
432 struct smbd_smb2_tree_connect_state *state =
433 tevent_req_data(req,
434 struct smbd_smb2_tree_connect_state);
435 NTSTATUS status;
437 if (tevent_req_is_nterror(req, &status)) {
438 tevent_req_received(req);
439 return status;
442 *out_share_type = state->out_share_type;
443 *out_share_flags = state->out_share_flags;
444 *out_capabilities = state->out_capabilities;
445 *out_maximal_access = state->out_maximal_access;
446 *out_tree_id = state->out_tree_id;
447 *disconnect = state->disconnect;
449 tevent_req_received(req);
450 return NT_STATUS_OK;
453 static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
454 struct tevent_context *ev,
455 struct smbd_smb2_request *smb2req);
456 static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req);
457 static void smbd_smb2_request_tdis_done(struct tevent_req *subreq);
459 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
461 NTSTATUS status;
462 struct tevent_req *subreq = NULL;
464 status = smbd_smb2_request_verify_sizes(req, 0x04);
465 if (!NT_STATUS_IS_OK(status)) {
466 return smbd_smb2_request_error(req, status);
469 subreq = smbd_smb2_tdis_send(req, req->sconn->ev_ctx, req);
470 if (subreq == NULL) {
471 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
473 tevent_req_set_callback(subreq, smbd_smb2_request_tdis_done, req);
476 * Wait a long time before going async on this to allow
477 * requests we're waiting on to finish. Set timeout to 10 secs.
479 return smbd_smb2_request_pending_queue(req, subreq, 10000000);
482 static void smbd_smb2_request_tdis_done(struct tevent_req *subreq)
484 struct smbd_smb2_request *smb2req =
485 tevent_req_callback_data(subreq,
486 struct smbd_smb2_request);
487 DATA_BLOB outbody;
488 NTSTATUS status;
489 NTSTATUS error;
491 status = smbd_smb2_tdis_recv(subreq);
492 TALLOC_FREE(subreq);
493 if (!NT_STATUS_IS_OK(status)) {
494 error = smbd_smb2_request_error(smb2req, status);
495 if (!NT_STATUS_IS_OK(error)) {
496 smbd_server_connection_terminate(smb2req->xconn,
497 nt_errstr(error));
498 return;
500 return;
503 outbody = smbd_smb2_generate_outbody(smb2req, 0x04);
504 if (outbody.data == NULL) {
505 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
506 if (!NT_STATUS_IS_OK(error)) {
507 smbd_server_connection_terminate(smb2req->xconn,
508 nt_errstr(error));
509 return;
511 return;
514 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
515 SSVAL(outbody.data, 0x02, 0); /* reserved */
517 error = smbd_smb2_request_done(smb2req, outbody, NULL);
518 if (!NT_STATUS_IS_OK(error)) {
519 smbd_server_connection_terminate(smb2req->xconn,
520 nt_errstr(error));
521 return;
525 struct smbd_smb2_tdis_state {
526 struct smbd_smb2_request *smb2req;
527 struct tevent_queue *wait_queue;
530 static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq);
532 static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
533 struct tevent_context *ev,
534 struct smbd_smb2_request *smb2req)
536 struct tevent_req *req;
537 struct smbd_smb2_tdis_state *state;
538 struct tevent_req *subreq;
539 struct smbXsrv_connection *xconn = NULL;
541 req = tevent_req_create(mem_ctx, &state,
542 struct smbd_smb2_tdis_state);
543 if (req == NULL) {
544 return NULL;
546 state->smb2req = smb2req;
548 state->wait_queue = tevent_queue_create(state, "tdis_wait_queue");
549 if (tevent_req_nomem(state->wait_queue, req)) {
550 return tevent_req_post(req, ev);
554 * Make sure that no new request will be able to use this tcon.
556 smb2req->tcon->status = NT_STATUS_NETWORK_NAME_DELETED;
558 xconn = smb2req->xconn->client->connections;
559 for (; xconn != NULL; xconn = xconn->next) {
560 struct smbd_smb2_request *preq;
562 for (preq = xconn->smb2.requests; preq != NULL; preq = preq->next) {
563 if (preq == smb2req) {
564 /* Can't cancel current request. */
565 continue;
567 if (preq->tcon != smb2req->tcon) {
568 /* Request on different tcon. */
569 continue;
573 * Never cancel anything in a compound
574 * request. Way too hard to deal with
575 * the result.
577 if (!preq->compound_related && preq->subreq != NULL) {
578 tevent_req_cancel(preq->subreq);
582 * Now wait until the request is finished.
584 * We don't set a callback, as we just want to block the
585 * wait queue and the talloc_free() of the request will
586 * remove the item from the wait queue.
588 subreq = tevent_queue_wait_send(preq, ev, state->wait_queue);
589 if (tevent_req_nomem(subreq, req)) {
590 return tevent_req_post(req, ev);
596 * Now we add our own waiter to the end of the queue,
597 * this way we get notified when all pending requests are finished
598 * and send to the socket.
600 subreq = tevent_queue_wait_send(state, ev, state->wait_queue);
601 if (tevent_req_nomem(subreq, req)) {
602 return tevent_req_post(req, ev);
604 tevent_req_set_callback(subreq, smbd_smb2_tdis_wait_done, req);
606 return req;
609 static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq)
611 struct tevent_req *req = tevent_req_callback_data(
612 subreq, struct tevent_req);
613 struct smbd_smb2_tdis_state *state = tevent_req_data(
614 req, struct smbd_smb2_tdis_state);
615 NTSTATUS status;
617 tevent_queue_wait_recv(subreq);
618 TALLOC_FREE(subreq);
621 * As we've been awoken, we may have changed
622 * uid in the meantime. Ensure we're still
623 * root (SMB2_OP_TDIS has .as_root = true).
625 change_to_root_user();
627 status = smbXsrv_tcon_disconnect(state->smb2req->tcon,
628 state->smb2req->tcon->compat->vuid);
629 if (tevent_req_nterror(req, status)) {
630 return;
633 /* We did tear down the tcon. */
634 TALLOC_FREE(state->smb2req->tcon);
635 tevent_req_done(req);
638 static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req)
640 return tevent_req_simple_recv_ntstatus(req);