s3:smb2_tcon: cancel and wait for pending requests on tdis
[Samba.git] / source3 / smbd / smb2_tcon.c
blobef2e318b24c30ccd20340050adc9bcf440334068
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_required = req->session->global->encryption_required;
189 bool guest_session = false;
191 if (strncmp(share, "\\\\", 2) == 0) {
192 const char *p = strchr(share+2, '\\');
193 if (p) {
194 share = p + 1;
198 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
199 in_path, share));
201 service = talloc_strdup(talloc_tos(), share);
202 if(!service) {
203 return NT_STATUS_NO_MEMORY;
206 if (!strlower_m(service)) {
207 DEBUG(2, ("strlower_m %s failed\n", service));
208 return NT_STATUS_INVALID_PARAMETER;
211 /* TODO: do more things... */
212 if (strequal(service,HOMES_NAME)) {
213 if (compat_vuser->homes_snum == -1) {
214 DEBUG(2, ("[homes] share not available for "
215 "user %s because it was not found "
216 "or created at session setup "
217 "time\n",
218 compat_vuser->session_info->unix_info->unix_name));
219 return NT_STATUS_BAD_NETWORK_NAME;
221 snum = compat_vuser->homes_snum;
222 } else if ((compat_vuser->homes_snum != -1)
223 && strequal(service,
224 lp_servicename(talloc_tos(), compat_vuser->homes_snum))) {
225 snum = compat_vuser->homes_snum;
226 } else {
227 snum = find_service(talloc_tos(), service, &service);
228 if (!service) {
229 return NT_STATUS_NO_MEMORY;
233 if (snum < 0) {
234 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
235 service));
236 return NT_STATUS_BAD_NETWORK_NAME;
239 if (lp_smb_encrypt(snum) == SMB_SIGNING_REQUIRED) {
240 encryption_required = true;
243 if (security_session_user_level(compat_vuser->session_info, NULL) < SECURITY_USER) {
244 guest_session = true;
247 if (guest_session && encryption_required) {
248 DEBUG(1,("reject guest as encryption is required for service %s\n",
249 service));
250 return NT_STATUS_ACCESS_DENIED;
253 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
254 if (encryption_required) {
255 DEBUG(1,("reject tcon with dialect[0x%04X] "
256 "as encryption is required for service %s\n",
257 conn->smb2.server.dialect, service));
258 return NT_STATUS_ACCESS_DENIED;
262 /* create a new tcon as child of the session */
263 status = smb2srv_tcon_create(req->session, now, &tcon);
264 if (!NT_STATUS_IS_OK(status)) {
265 return status;
268 tcon->global->encryption_required = encryption_required;
270 compat_conn = make_connection_smb2(req->sconn,
271 tcon, snum,
272 req->session->compat,
273 "???",
274 &status);
275 if (compat_conn == NULL) {
276 TALLOC_FREE(tcon);
277 return status;
280 tcon->global->share_name = lp_servicename(tcon->global,
281 SNUM(compat_conn));
282 if (tcon->global->share_name == NULL) {
283 conn_free(compat_conn);
284 TALLOC_FREE(tcon);
285 return NT_STATUS_NO_MEMORY;
287 tcon->global->session_global_id =
288 req->session->global->session_global_id;
290 tcon->compat = talloc_move(tcon, &compat_conn);
292 tcon->status = NT_STATUS_OK;
294 status = smbXsrv_tcon_update(tcon);
295 if (!NT_STATUS_IS_OK(status)) {
296 TALLOC_FREE(tcon);
297 return status;
300 if (IS_PRINT(tcon->compat)) {
301 *out_share_type = SMB2_SHARE_TYPE_PRINT;
302 } else if (IS_IPC(tcon->compat)) {
303 *out_share_type = SMB2_SHARE_TYPE_PIPE;
304 } else {
305 *out_share_type = SMB2_SHARE_TYPE_DISK;
308 *out_share_flags = 0;
310 if (lp_msdfs_root(SNUM(tcon->compat)) && lp_host_msdfs()) {
311 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
312 *out_capabilities = SMB2_SHARE_CAP_DFS;
313 } else {
314 *out_capabilities = 0;
317 switch(lp_csc_policy(SNUM(tcon->compat))) {
318 case CSC_POLICY_MANUAL:
319 break;
320 case CSC_POLICY_DOCUMENTS:
321 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
322 break;
323 case CSC_POLICY_PROGRAMS:
324 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
325 break;
326 case CSC_POLICY_DISABLE:
327 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
328 break;
329 default:
330 break;
333 if (lp_hideunreadable(SNUM(tcon->compat)) ||
334 lp_hideunwriteable_files(SNUM(tcon->compat))) {
335 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
338 if (encryption_required) {
339 *out_share_flags |= SMB2_SHAREFLAG_ENCRYPT_DATA;
342 *out_maximal_access = tcon->compat->share_access;
344 *out_tree_id = tcon->global->tcon_wire_id;
345 return NT_STATUS_OK;
348 struct smbd_smb2_tree_connect_state {
349 const char *in_path;
350 uint8_t out_share_type;
351 uint32_t out_share_flags;
352 uint32_t out_capabilities;
353 uint32_t out_maximal_access;
354 uint32_t out_tree_id;
357 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
358 struct tevent_context *ev,
359 struct smbd_smb2_request *smb2req,
360 const char *in_path)
362 struct tevent_req *req;
363 struct smbd_smb2_tree_connect_state *state;
364 NTSTATUS status;
366 req = tevent_req_create(mem_ctx, &state,
367 struct smbd_smb2_tree_connect_state);
368 if (req == NULL) {
369 return NULL;
371 state->in_path = in_path;
373 status = smbd_smb2_tree_connect(smb2req,
374 state->in_path,
375 &state->out_share_type,
376 &state->out_share_flags,
377 &state->out_capabilities,
378 &state->out_maximal_access,
379 &state->out_tree_id);
380 if (tevent_req_nterror(req, status)) {
381 return tevent_req_post(req, ev);
384 tevent_req_done(req);
385 return tevent_req_post(req, ev);
388 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
389 uint8_t *out_share_type,
390 uint32_t *out_share_flags,
391 uint32_t *out_capabilities,
392 uint32_t *out_maximal_access,
393 uint32_t *out_tree_id)
395 struct smbd_smb2_tree_connect_state *state =
396 tevent_req_data(req,
397 struct smbd_smb2_tree_connect_state);
398 NTSTATUS status;
400 if (tevent_req_is_nterror(req, &status)) {
401 tevent_req_received(req);
402 return status;
405 *out_share_type = state->out_share_type;
406 *out_share_flags = state->out_share_flags;
407 *out_capabilities = state->out_capabilities;
408 *out_maximal_access = state->out_maximal_access;
409 *out_tree_id = state->out_tree_id;
411 tevent_req_received(req);
412 return NT_STATUS_OK;
415 static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
416 struct tevent_context *ev,
417 struct smbd_smb2_request *smb2req);
418 static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req);
419 static void smbd_smb2_request_tdis_done(struct tevent_req *subreq);
421 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
423 NTSTATUS status;
424 struct tevent_req *subreq = NULL;
426 status = smbd_smb2_request_verify_sizes(req, 0x04);
427 if (!NT_STATUS_IS_OK(status)) {
428 return smbd_smb2_request_error(req, status);
431 subreq = smbd_smb2_tdis_send(req, req->sconn->ev_ctx, req);
432 if (subreq == NULL) {
433 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
435 tevent_req_set_callback(subreq, smbd_smb2_request_tdis_done, req);
438 * Wait a long time before going async on this to allow
439 * requests we're waiting on to finish. Set timeout to 10 secs.
441 return smbd_smb2_request_pending_queue(req, subreq, 10000000);
444 static void smbd_smb2_request_tdis_done(struct tevent_req *subreq)
446 struct smbd_smb2_request *smb2req =
447 tevent_req_callback_data(subreq,
448 struct smbd_smb2_request);
449 DATA_BLOB outbody;
450 NTSTATUS status;
451 NTSTATUS error;
453 status = smbd_smb2_tdis_recv(subreq);
454 TALLOC_FREE(subreq);
455 if (!NT_STATUS_IS_OK(status)) {
456 error = smbd_smb2_request_error(smb2req, status);
457 if (!NT_STATUS_IS_OK(error)) {
458 smbd_server_connection_terminate(smb2req->sconn,
459 nt_errstr(error));
460 return;
462 return;
465 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x04);
466 if (outbody.data == NULL) {
467 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
468 if (!NT_STATUS_IS_OK(error)) {
469 smbd_server_connection_terminate(smb2req->sconn,
470 nt_errstr(error));
471 return;
473 return;
476 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
477 SSVAL(outbody.data, 0x02, 0); /* reserved */
479 error = smbd_smb2_request_done(smb2req, outbody, NULL);
480 if (!NT_STATUS_IS_OK(error)) {
481 smbd_server_connection_terminate(smb2req->sconn,
482 nt_errstr(error));
483 return;
487 struct smbd_smb2_tdis_state {
488 struct smbd_smb2_request *smb2req;
489 struct tevent_queue *wait_queue;
492 static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq);
494 static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
495 struct tevent_context *ev,
496 struct smbd_smb2_request *smb2req)
498 struct tevent_req *req;
499 struct smbd_smb2_tdis_state *state;
500 struct tevent_req *subreq;
501 struct smbd_smb2_request *preq;
503 req = tevent_req_create(mem_ctx, &state,
504 struct smbd_smb2_tdis_state);
505 if (req == NULL) {
506 return NULL;
508 state->smb2req = smb2req;
510 state->wait_queue = tevent_queue_create(state, "tdis_wait_queue");
511 if (tevent_req_nomem(state->wait_queue, req)) {
512 return tevent_req_post(req, ev);
516 * Make sure that no new request will be able to use this tcon.
518 smb2req->tcon->status = NT_STATUS_NETWORK_NAME_DELETED;
520 for (preq = smb2req->sconn->smb2.requests; preq != NULL; preq = preq->next) {
521 if (preq == smb2req) {
522 /* Can't cancel current request. */
523 continue;
525 if (preq->tcon != smb2req->tcon) {
526 /* Request on different tcon. */
527 continue;
531 * Never cancel anything in a compound
532 * request. Way too hard to deal with
533 * the result.
535 if (!preq->compound_related && preq->subreq != NULL) {
536 tevent_req_cancel(preq->subreq);
540 * Now wait until the request is finished.
542 * We don't set a callback, as we just want to block the
543 * wait queue and the talloc_free() of the request will
544 * remove the item from the wait queue.
546 subreq = smbd_tevent_queue_wait_send(preq, ev, state->wait_queue);
547 if (tevent_req_nomem(subreq, req)) {
548 return tevent_req_post(req, ev);
553 * Now we add our own waiter to the end of the queue,
554 * this way we get notified when all pending requests are finished
555 * and send to the socket.
557 subreq = smbd_tevent_queue_wait_send(state, ev, state->wait_queue);
558 if (tevent_req_nomem(subreq, req)) {
559 return tevent_req_post(req, ev);
561 tevent_req_set_callback(subreq, smbd_smb2_tdis_wait_done, req);
563 return req;
566 static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq)
568 struct tevent_req *req = tevent_req_callback_data(
569 subreq, struct tevent_req);
570 struct smbd_smb2_tdis_state *state = tevent_req_data(
571 req, struct smbd_smb2_tdis_state);
572 NTSTATUS status;
574 smbd_tevent_queue_wait_recv(subreq);
575 TALLOC_FREE(subreq);
578 * As we've been awoken, we may have changed
579 * uid in the meantime. Ensure we're still
580 * root (SMB2_OP_TDIS has .as_root = true).
582 change_to_root_user();
584 status = smbXsrv_tcon_disconnect(state->smb2req->tcon,
585 state->smb2req->tcon->compat->vuid);
586 if (tevent_req_nterror(req, status)) {
587 return;
590 /* We did tear down the tcon. */
591 TALLOC_FREE(state->smb2req->tcon);
592 tevent_req_done(req);
595 static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req)
597 return tevent_req_simple_recv_ntstatus(req);