ldb: version 1.1.20
[Samba.git] / source3 / smbd / smb2_tcon.c
blobbf33fcbf723a526765b7d39e77e667744c4ad613
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);
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;
49 char *in_path_string;
50 size_t in_path_string_size;
51 NTSTATUS status;
52 bool ok;
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,
76 in_path_buffer.data,
77 in_path_buffer.length,
78 &in_path_string,
79 &in_path_string_size);
80 if (!ok) {
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,
93 req->sconn->ev_ctx,
94 req,
95 in_path_string);
96 if (subreq == NULL) {
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);
109 uint8_t *outhdr;
110 DATA_BLOB outbody;
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;
116 NTSTATUS status;
117 NTSTATUS error;
119 status = smbd_smb2_tree_connect_recv(subreq,
120 &out_share_type,
121 &out_share_flags,
122 &out_capabilities,
123 &out_maximal_access,
124 &out_tree_id);
125 TALLOC_FREE(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,
130 nt_errstr(error));
131 return;
133 return;
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,
143 nt_errstr(error));
144 return;
146 return;
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,
165 nt_errstr(error));
166 return;
170 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
171 const char *in_path,
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;
181 int snum = -1;
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;
186 NTSTATUS status;
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, '\\');
192 if (p) {
193 share = p + 1;
197 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
198 in_path, share));
200 service = talloc_strdup(talloc_tos(), share);
201 if(!service) {
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 "
216 "time\n",
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)
222 && strequal(service,
223 lp_servicename(talloc_tos(), compat_vuser->homes_snum))) {
224 snum = compat_vuser->homes_snum;
225 } else {
226 snum = find_service(talloc_tos(), service, &service);
227 if (!service) {
228 return NT_STATUS_NO_MEMORY;
232 if (snum < 0) {
233 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
234 service));
235 return NT_STATUS_BAD_NETWORK_NAME;
238 if (lp_smb_encrypt(snum) == SMB_SIGNING_REQUIRED) {
239 encryption_required = true;
242 if (security_session_user_level(compat_vuser->session_info, NULL) < SECURITY_USER) {
243 guest_session = true;
246 if (guest_session && encryption_required) {
247 DEBUG(1,("reject guest as encryption is required for service %s\n",
248 service));
249 return NT_STATUS_ACCESS_DENIED;
252 if (conn->smb2.server.cipher == 0) {
253 if (encryption_required) {
254 DEBUG(1,("reject tcon with dialect[0x%04X] "
255 "as encryption is required for service %s\n",
256 conn->smb2.server.dialect, service));
257 return NT_STATUS_ACCESS_DENIED;
261 /* create a new tcon as child of the session */
262 status = smb2srv_tcon_create(req->session, now, &tcon);
263 if (!NT_STATUS_IS_OK(status)) {
264 return status;
267 tcon->global->encryption_required = encryption_required;
269 compat_conn = make_connection_smb2(req,
270 tcon, snum,
271 req->session->compat,
272 "???",
273 &status);
274 if (compat_conn == NULL) {
275 TALLOC_FREE(tcon);
276 return status;
279 tcon->global->share_name = lp_servicename(tcon->global,
280 SNUM(compat_conn));
281 if (tcon->global->share_name == NULL) {
282 conn_free(compat_conn);
283 TALLOC_FREE(tcon);
284 return NT_STATUS_NO_MEMORY;
286 tcon->global->session_global_id =
287 req->session->global->session_global_id;
289 tcon->compat = talloc_move(tcon, &compat_conn);
291 tcon->status = NT_STATUS_OK;
293 status = smbXsrv_tcon_update(tcon);
294 if (!NT_STATUS_IS_OK(status)) {
295 TALLOC_FREE(tcon);
296 return status;
299 if (IS_PRINT(tcon->compat)) {
300 *out_share_type = SMB2_SHARE_TYPE_PRINT;
301 } else if (IS_IPC(tcon->compat)) {
302 *out_share_type = SMB2_SHARE_TYPE_PIPE;
303 } else {
304 *out_share_type = SMB2_SHARE_TYPE_DISK;
307 *out_share_flags = 0;
309 if (lp_msdfs_root(SNUM(tcon->compat)) && lp_host_msdfs()) {
310 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
311 *out_capabilities = SMB2_SHARE_CAP_DFS;
312 } else {
313 *out_capabilities = 0;
316 switch(lp_csc_policy(SNUM(tcon->compat))) {
317 case CSC_POLICY_MANUAL:
318 break;
319 case CSC_POLICY_DOCUMENTS:
320 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
321 break;
322 case CSC_POLICY_PROGRAMS:
323 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
324 break;
325 case CSC_POLICY_DISABLE:
326 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
327 break;
328 default:
329 break;
332 if (lp_hide_unreadable(SNUM(tcon->compat)) ||
333 lp_hide_unwriteable_files(SNUM(tcon->compat))) {
334 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
337 if (encryption_required) {
338 *out_share_flags |= SMB2_SHAREFLAG_ENCRYPT_DATA;
341 *out_maximal_access = tcon->compat->share_access;
343 *out_tree_id = tcon->global->tcon_wire_id;
344 return NT_STATUS_OK;
347 struct smbd_smb2_tree_connect_state {
348 const char *in_path;
349 uint8_t out_share_type;
350 uint32_t out_share_flags;
351 uint32_t out_capabilities;
352 uint32_t out_maximal_access;
353 uint32_t out_tree_id;
356 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
357 struct tevent_context *ev,
358 struct smbd_smb2_request *smb2req,
359 const char *in_path)
361 struct tevent_req *req;
362 struct smbd_smb2_tree_connect_state *state;
363 NTSTATUS status;
365 req = tevent_req_create(mem_ctx, &state,
366 struct smbd_smb2_tree_connect_state);
367 if (req == NULL) {
368 return NULL;
370 state->in_path = in_path;
372 status = smbd_smb2_tree_connect(smb2req,
373 state->in_path,
374 &state->out_share_type,
375 &state->out_share_flags,
376 &state->out_capabilities,
377 &state->out_maximal_access,
378 &state->out_tree_id);
379 if (tevent_req_nterror(req, status)) {
380 return tevent_req_post(req, ev);
383 tevent_req_done(req);
384 return tevent_req_post(req, ev);
387 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
388 uint8_t *out_share_type,
389 uint32_t *out_share_flags,
390 uint32_t *out_capabilities,
391 uint32_t *out_maximal_access,
392 uint32_t *out_tree_id)
394 struct smbd_smb2_tree_connect_state *state =
395 tevent_req_data(req,
396 struct smbd_smb2_tree_connect_state);
397 NTSTATUS status;
399 if (tevent_req_is_nterror(req, &status)) {
400 tevent_req_received(req);
401 return status;
404 *out_share_type = state->out_share_type;
405 *out_share_flags = state->out_share_flags;
406 *out_capabilities = state->out_capabilities;
407 *out_maximal_access = state->out_maximal_access;
408 *out_tree_id = state->out_tree_id;
410 tevent_req_received(req);
411 return NT_STATUS_OK;
414 static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
415 struct tevent_context *ev,
416 struct smbd_smb2_request *smb2req);
417 static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req);
418 static void smbd_smb2_request_tdis_done(struct tevent_req *subreq);
420 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
422 NTSTATUS status;
423 struct tevent_req *subreq = NULL;
425 status = smbd_smb2_request_verify_sizes(req, 0x04);
426 if (!NT_STATUS_IS_OK(status)) {
427 return smbd_smb2_request_error(req, status);
430 subreq = smbd_smb2_tdis_send(req, req->sconn->ev_ctx, req);
431 if (subreq == NULL) {
432 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
434 tevent_req_set_callback(subreq, smbd_smb2_request_tdis_done, req);
437 * Wait a long time before going async on this to allow
438 * requests we're waiting on to finish. Set timeout to 10 secs.
440 return smbd_smb2_request_pending_queue(req, subreq, 10000000);
443 static void smbd_smb2_request_tdis_done(struct tevent_req *subreq)
445 struct smbd_smb2_request *smb2req =
446 tevent_req_callback_data(subreq,
447 struct smbd_smb2_request);
448 DATA_BLOB outbody;
449 NTSTATUS status;
450 NTSTATUS error;
452 status = smbd_smb2_tdis_recv(subreq);
453 TALLOC_FREE(subreq);
454 if (!NT_STATUS_IS_OK(status)) {
455 error = smbd_smb2_request_error(smb2req, status);
456 if (!NT_STATUS_IS_OK(error)) {
457 smbd_server_connection_terminate(smb2req->xconn,
458 nt_errstr(error));
459 return;
461 return;
464 outbody = smbd_smb2_generate_outbody(smb2req, 0x04);
465 if (outbody.data == NULL) {
466 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
467 if (!NT_STATUS_IS_OK(error)) {
468 smbd_server_connection_terminate(smb2req->xconn,
469 nt_errstr(error));
470 return;
472 return;
475 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
476 SSVAL(outbody.data, 0x02, 0); /* reserved */
478 error = smbd_smb2_request_done(smb2req, outbody, NULL);
479 if (!NT_STATUS_IS_OK(error)) {
480 smbd_server_connection_terminate(smb2req->xconn,
481 nt_errstr(error));
482 return;
486 struct smbd_smb2_tdis_state {
487 struct smbd_smb2_request *smb2req;
488 struct tevent_queue *wait_queue;
491 static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq);
493 static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
494 struct tevent_context *ev,
495 struct smbd_smb2_request *smb2req)
497 struct tevent_req *req;
498 struct smbd_smb2_tdis_state *state;
499 struct tevent_req *subreq;
500 struct smbd_smb2_request *preq;
501 struct smbXsrv_connection *xconn = smb2req->xconn;
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 = xconn->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 = 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 = 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 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);