s3:smb2_*: make use of smb2req->xconn where possible
[Samba.git] / source3 / smbd / smb2_sesssetup.c
blob3a80afdf2d08be0d95adb3849e27e7882a70823d
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "../auth/gensec/gensec.h"
27 #include "auth.h"
28 #include "../lib/tsocket/tsocket.h"
29 #include "../libcli/security/security.h"
30 #include "../lib/util/tevent_ntstatus.h"
32 static struct tevent_req *smbd_smb2_session_setup_send(TALLOC_CTX *mem_ctx,
33 struct tevent_context *ev,
34 struct smbd_smb2_request *smb2req,
35 uint64_t in_session_id,
36 uint8_t in_flags,
37 uint8_t in_security_mode,
38 uint64_t in_previous_session_id,
39 DATA_BLOB in_security_buffer);
40 static NTSTATUS smbd_smb2_session_setup_recv(struct tevent_req *req,
41 uint16_t *out_session_flags,
42 TALLOC_CTX *mem_ctx,
43 DATA_BLOB *out_security_buffer,
44 uint64_t *out_session_id);
46 static void smbd_smb2_request_sesssetup_done(struct tevent_req *subreq);
48 NTSTATUS smbd_smb2_request_process_sesssetup(struct smbd_smb2_request *smb2req)
50 const uint8_t *inhdr;
51 const uint8_t *inbody;
52 uint64_t in_session_id;
53 uint8_t in_flags;
54 uint8_t in_security_mode;
55 uint64_t in_previous_session_id;
56 uint16_t in_security_offset;
57 uint16_t in_security_length;
58 DATA_BLOB in_security_buffer;
59 NTSTATUS status;
60 struct tevent_req *subreq;
62 status = smbd_smb2_request_verify_sizes(smb2req, 0x19);
63 if (!NT_STATUS_IS_OK(status)) {
64 return smbd_smb2_request_error(smb2req, status);
66 inhdr = SMBD_SMB2_IN_HDR_PTR(smb2req);
67 inbody = SMBD_SMB2_IN_BODY_PTR(smb2req);
69 in_session_id = BVAL(inhdr, SMB2_HDR_SESSION_ID);
71 in_flags = CVAL(inbody, 0x02);
72 in_security_mode = CVAL(inbody, 0x03);
73 /* Capabilities = IVAL(inbody, 0x04) */
74 /* Channel = IVAL(inbody, 0x08) */
75 in_security_offset = SVAL(inbody, 0x0C);
76 in_security_length = SVAL(inbody, 0x0E);
77 in_previous_session_id = BVAL(inbody, 0x10);
79 if (in_security_offset != (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(smb2req))) {
80 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
83 if (in_security_length > SMBD_SMB2_IN_DYN_LEN(smb2req)) {
84 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
87 in_security_buffer.data = SMBD_SMB2_IN_DYN_PTR(smb2req);
88 in_security_buffer.length = in_security_length;
90 subreq = smbd_smb2_session_setup_send(smb2req,
91 smb2req->sconn->ev_ctx,
92 smb2req,
93 in_session_id,
94 in_flags,
95 in_security_mode,
96 in_previous_session_id,
97 in_security_buffer);
98 if (subreq == NULL) {
99 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
101 tevent_req_set_callback(subreq, smbd_smb2_request_sesssetup_done, smb2req);
103 return smbd_smb2_request_pending_queue(smb2req, subreq, 500);
106 static void smbd_smb2_request_sesssetup_done(struct tevent_req *subreq)
108 struct smbd_smb2_request *smb2req =
109 tevent_req_callback_data(subreq,
110 struct smbd_smb2_request);
111 uint8_t *outhdr;
112 DATA_BLOB outbody;
113 DATA_BLOB outdyn;
114 uint16_t out_session_flags = 0;
115 uint64_t out_session_id = 0;
116 uint16_t out_security_offset;
117 DATA_BLOB out_security_buffer = data_blob_null;
118 NTSTATUS status;
119 NTSTATUS error; /* transport error */
121 status = smbd_smb2_session_setup_recv(subreq,
122 &out_session_flags,
123 smb2req,
124 &out_security_buffer,
125 &out_session_id);
126 TALLOC_FREE(subreq);
127 if (!NT_STATUS_IS_OK(status) &&
128 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
129 status = nt_status_squash(status);
130 error = smbd_smb2_request_error(smb2req, status);
131 if (!NT_STATUS_IS_OK(error)) {
132 smbd_server_connection_terminate(smb2req->sconn,
133 nt_errstr(error));
134 return;
136 return;
139 out_security_offset = SMB2_HDR_BODY + 0x08;
141 outhdr = SMBD_SMB2_OUT_HDR_PTR(smb2req);
143 outbody = smbd_smb2_generate_outbody(smb2req, 0x08);
144 if (outbody.data == NULL) {
145 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
146 if (!NT_STATUS_IS_OK(error)) {
147 smbd_server_connection_terminate(smb2req->sconn,
148 nt_errstr(error));
149 return;
151 return;
154 SBVAL(outhdr, SMB2_HDR_SESSION_ID, out_session_id);
156 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
157 SSVAL(outbody.data, 0x02,
158 out_session_flags); /* session flags */
159 SSVAL(outbody.data, 0x04,
160 out_security_offset); /* security buffer offset */
161 SSVAL(outbody.data, 0x06,
162 out_security_buffer.length); /* security buffer length */
164 outdyn = out_security_buffer;
166 error = smbd_smb2_request_done_ex(smb2req, status, outbody, &outdyn,
167 __location__);
168 if (!NT_STATUS_IS_OK(error)) {
169 smbd_server_connection_terminate(smb2req->sconn,
170 nt_errstr(error));
171 return;
175 static NTSTATUS smbd_smb2_auth_generic_return(struct smbXsrv_session *session,
176 struct smbd_smb2_request *smb2req,
177 uint8_t in_security_mode,
178 struct auth_session_info *session_info,
179 uint16_t *out_session_flags,
180 uint64_t *out_session_id)
182 NTSTATUS status;
183 bool guest = false;
184 uint8_t session_key[16];
185 struct smbXsrv_session *x = session;
186 struct smbXsrv_connection *conn = session->connection;
188 if ((in_security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
189 lp_server_signing() == SMB_SIGNING_REQUIRED) {
190 x->global->signing_required = true;
193 if (lp_smb_encrypt(-1) == SMB_SIGNING_REQUIRED) {
194 x->global->encryption_required = true;
197 if (security_session_user_level(session_info, NULL) < SECURITY_USER) {
198 /* we map anonymous to guest internally */
199 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
200 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
201 /* force no signing */
202 x->global->signing_required = false;
203 guest = true;
206 if (guest && x->global->encryption_required) {
207 DEBUG(1,("reject guest session as encryption is required\n"));
208 return NT_STATUS_ACCESS_DENIED;
211 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
212 if (x->global->encryption_required) {
213 DEBUG(1,("reject session with dialect[0x%04X] "
214 "as encryption is required\n",
215 conn->smb2.server.dialect));
216 return NT_STATUS_ACCESS_DENIED;
220 if (x->global->encryption_required) {
221 *out_session_flags |= SMB2_SESSION_FLAG_ENCRYPT_DATA;
224 ZERO_STRUCT(session_key);
225 memcpy(session_key, session_info->session_key.data,
226 MIN(session_info->session_key.length, sizeof(session_key)));
228 x->global->signing_key = data_blob_talloc(x->global,
229 session_key,
230 sizeof(session_key));
231 if (x->global->signing_key.data == NULL) {
232 ZERO_STRUCT(session_key);
233 return NT_STATUS_NO_MEMORY;
236 if (conn->protocol >= PROTOCOL_SMB2_24) {
237 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
238 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
240 smb2_key_derivation(session_key, sizeof(session_key),
241 label.data, label.length,
242 context.data, context.length,
243 x->global->signing_key.data);
246 if (conn->protocol >= PROTOCOL_SMB2_24) {
247 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
248 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
250 x->global->decryption_key = data_blob_talloc(x->global,
251 session_key,
252 sizeof(session_key));
253 if (x->global->decryption_key.data == NULL) {
254 ZERO_STRUCT(session_key);
255 return NT_STATUS_NO_MEMORY;
258 smb2_key_derivation(session_key, sizeof(session_key),
259 label.data, label.length,
260 context.data, context.length,
261 x->global->decryption_key.data);
264 if (conn->protocol >= PROTOCOL_SMB2_24) {
265 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
266 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
268 x->global->encryption_key = data_blob_talloc(x->global,
269 session_key,
270 sizeof(session_key));
271 if (x->global->encryption_key.data == NULL) {
272 ZERO_STRUCT(session_key);
273 return NT_STATUS_NO_MEMORY;
276 smb2_key_derivation(session_key, sizeof(session_key),
277 label.data, label.length,
278 context.data, context.length,
279 x->global->encryption_key.data);
281 generate_random_buffer((uint8_t *)&x->nonce_high, sizeof(x->nonce_high));
282 x->nonce_low = 1;
285 x->global->application_key = data_blob_dup_talloc(x->global,
286 x->global->signing_key);
287 if (x->global->application_key.data == NULL) {
288 ZERO_STRUCT(session_key);
289 return NT_STATUS_NO_MEMORY;
292 if (conn->protocol >= PROTOCOL_SMB2_24) {
293 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
294 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
296 smb2_key_derivation(session_key, sizeof(session_key),
297 label.data, label.length,
298 context.data, context.length,
299 x->global->application_key.data);
301 ZERO_STRUCT(session_key);
303 x->global->channels[0].signing_key = data_blob_dup_talloc(x->global->channels,
304 x->global->signing_key);
305 if (x->global->channels[0].signing_key.data == NULL) {
306 return NT_STATUS_NO_MEMORY;
309 data_blob_clear_free(&session_info->session_key);
310 session_info->session_key = data_blob_dup_talloc(session_info,
311 x->global->application_key);
312 if (session_info->session_key.data == NULL) {
313 return NT_STATUS_NO_MEMORY;
316 session->compat = talloc_zero(session, struct user_struct);
317 if (session->compat == NULL) {
318 return NT_STATUS_NO_MEMORY;
320 session->compat->session = session;
321 session->compat->homes_snum = -1;
322 session->compat->session_info = session_info;
323 session->compat->session_keystr = NULL;
324 session->compat->vuid = session->global->session_wire_id;
325 DLIST_ADD(smb2req->sconn->users, session->compat);
326 smb2req->sconn->num_users++;
328 if (security_session_user_level(session_info, NULL) >= SECURITY_USER) {
329 session->compat->homes_snum =
330 register_homes_share(session_info->unix_info->unix_name);
333 set_current_user_info(session_info->unix_info->sanitized_username,
334 session_info->unix_info->unix_name,
335 session_info->info->domain_name);
337 reload_services(smb2req->sconn, conn_snum_used, true);
339 session->status = NT_STATUS_OK;
340 session->global->auth_session_info = session_info;
341 session->global->auth_session_info_seqnum += 1;
342 session->global->channels[0].auth_session_info_seqnum =
343 session->global->auth_session_info_seqnum;
344 session->global->expiration_time = gensec_expire_time(session->gensec);
346 if (!session_claim(session)) {
347 DEBUG(1, ("smb2: Failed to claim session "
348 "for vuid=%llu\n",
349 (unsigned long long)session->compat->vuid));
350 return NT_STATUS_LOGON_FAILURE;
353 status = smbXsrv_session_update(session);
354 if (!NT_STATUS_IS_OK(status)) {
355 DEBUG(0, ("smb2: Failed to update session for vuid=%llu - %s\n",
356 (unsigned long long)session->compat->vuid,
357 nt_errstr(status)));
358 return NT_STATUS_LOGON_FAILURE;
362 * we attach the session to the request
363 * so that the response can be signed
365 smb2req->session = session;
366 if (!guest) {
367 smb2req->do_signing = true;
370 global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
372 *out_session_id = session->global->session_wire_id;
374 return NT_STATUS_OK;
377 static NTSTATUS smbd_smb2_reauth_generic_return(struct smbXsrv_session *session,
378 struct smbd_smb2_request *smb2req,
379 struct auth_session_info *session_info,
380 uint16_t *out_session_flags,
381 uint64_t *out_session_id)
383 NTSTATUS status;
384 struct smbXsrv_session *x = session;
385 struct smbXsrv_connection *conn = session->connection;
387 data_blob_clear_free(&session_info->session_key);
388 session_info->session_key = data_blob_dup_talloc(session_info,
389 x->global->application_key);
390 if (session_info->session_key.data == NULL) {
391 return NT_STATUS_NO_MEMORY;
394 session->compat->session_info = session_info;
395 session->compat->vuid = session->global->session_wire_id;
397 session->compat->homes_snum =
398 register_homes_share(session_info->unix_info->unix_name);
400 set_current_user_info(session_info->unix_info->sanitized_username,
401 session_info->unix_info->unix_name,
402 session_info->info->domain_name);
404 reload_services(smb2req->sconn, conn_snum_used, true);
406 session->status = NT_STATUS_OK;
407 TALLOC_FREE(session->global->auth_session_info);
408 session->global->auth_session_info = session_info;
409 session->global->auth_session_info_seqnum += 1;
410 session->global->channels[0].auth_session_info_seqnum =
411 session->global->auth_session_info_seqnum;
412 session->global->expiration_time = gensec_expire_time(session->gensec);
414 status = smbXsrv_session_update(session);
415 if (!NT_STATUS_IS_OK(status)) {
416 DEBUG(0, ("smb2: Failed to update session for vuid=%llu - %s\n",
417 (unsigned long long)session->compat->vuid,
418 nt_errstr(status)));
419 return NT_STATUS_LOGON_FAILURE;
422 conn_clear_vuid_caches(conn->sconn, session->compat->vuid);
424 *out_session_id = session->global->session_wire_id;
426 return NT_STATUS_OK;
429 struct smbd_smb2_session_setup_state {
430 struct tevent_context *ev;
431 struct smbd_smb2_request *smb2req;
432 uint64_t in_session_id;
433 uint8_t in_flags;
434 uint8_t in_security_mode;
435 uint64_t in_previous_session_id;
436 DATA_BLOB in_security_buffer;
437 struct smbXsrv_session *session;
438 struct auth_session_info *session_info;
439 uint16_t out_session_flags;
440 DATA_BLOB out_security_buffer;
441 uint64_t out_session_id;
442 /* The following pointer is owned by state->session. */
443 struct smbd_smb2_session_setup_state **pp_self_ref;
446 static int pp_self_ref_destructor(struct smbd_smb2_session_setup_state **pp_state)
448 (*pp_state)->session = NULL;
450 * To make things clearer, ensure the pp_self_ref
451 * pointer is nulled out. We're never going to
452 * access this again.
454 (*pp_state)->pp_self_ref = NULL;
455 return 0;
458 static int smbd_smb2_session_setup_state_destructor(struct smbd_smb2_session_setup_state *state)
460 struct smbXsrv_connection *xconn;
461 struct smbd_smb2_request *preq;
464 * If state->session is not NULL,
465 * we move the session from the session table to the request on failure
466 * so that the error response can be correctly signed, but the session
467 * is then really deleted when the request is done.
470 if (state->session == NULL) {
471 return 0;
474 state->session->status = NT_STATUS_USER_SESSION_DELETED;
475 state->smb2req->session = talloc_move(state->smb2req, &state->session);
478 * We've made this session owned by the current request.
479 * Ensure that any outstanding requests don't also refer
480 * to it.
482 xconn = state->smb2req->xconn;
484 for (preq = xconn->smb2.requests; preq != NULL; preq = preq->next) {
485 if (preq == state->smb2req) {
486 continue;
488 if (preq->session == state->smb2req->session) {
489 preq->session = NULL;
491 * If we no longer have a session we can't
492 * sign or encrypt replies.
494 preq->do_signing = false;
495 preq->do_encryption = false;
499 return 0;
502 static void smbd_smb2_session_setup_gensec_done(struct tevent_req *subreq);
503 static void smbd_smb2_session_setup_previous_done(struct tevent_req *subreq);
505 /************************************************************************
506 We have to tag the state->session pointer with memory talloc'ed
507 on it to ensure it gets NULL'ed out if the underlying struct smbXsrv_session
508 is deleted by shutdown whilst this request is in flight.
509 ************************************************************************/
511 static NTSTATUS tag_state_session_ptr(struct smbd_smb2_session_setup_state *state)
513 state->pp_self_ref = talloc_zero(state->session,
514 struct smbd_smb2_session_setup_state *);
515 if (state->pp_self_ref == NULL) {
516 return NT_STATUS_NO_MEMORY;
518 *state->pp_self_ref = state;
519 talloc_set_destructor(state->pp_self_ref, pp_self_ref_destructor);
520 return NT_STATUS_OK;
523 static struct tevent_req *smbd_smb2_session_setup_send(TALLOC_CTX *mem_ctx,
524 struct tevent_context *ev,
525 struct smbd_smb2_request *smb2req,
526 uint64_t in_session_id,
527 uint8_t in_flags,
528 uint8_t in_security_mode,
529 uint64_t in_previous_session_id,
530 DATA_BLOB in_security_buffer)
532 struct tevent_req *req;
533 struct smbd_smb2_session_setup_state *state;
534 NTSTATUS status;
535 NTTIME now = timeval_to_nttime(&smb2req->request_time);
536 struct tevent_req *subreq;
538 req = tevent_req_create(mem_ctx, &state,
539 struct smbd_smb2_session_setup_state);
540 if (req == NULL) {
541 return NULL;
543 state->ev = ev;
544 state->smb2req = smb2req;
545 state->in_session_id = in_session_id;
546 state->in_flags = in_flags;
547 state->in_security_mode = in_security_mode;
548 state->in_previous_session_id = in_previous_session_id;
549 state->in_security_buffer = in_security_buffer;
551 if (in_flags & SMB2_SESSION_FLAG_BINDING) {
552 if (smb2req->xconn->protocol < PROTOCOL_SMB2_22) {
553 tevent_req_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
554 return tevent_req_post(req, ev);
558 * We do not support multi channel.
560 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
561 return tevent_req_post(req, ev);
564 talloc_set_destructor(state, smbd_smb2_session_setup_state_destructor);
566 if (state->in_session_id == 0) {
567 /* create a new session */
568 status = smbXsrv_session_create(state->smb2req->xconn,
569 now, &state->session);
570 if (tevent_req_nterror(req, status)) {
571 return tevent_req_post(req, ev);
573 } else {
574 status = smb2srv_session_lookup(state->smb2req->xconn,
575 state->in_session_id, now,
576 &state->session);
577 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
578 status = NT_STATUS_OK;
580 if (NT_STATUS_IS_OK(status)) {
581 state->session->status = NT_STATUS_MORE_PROCESSING_REQUIRED;
582 status = NT_STATUS_MORE_PROCESSING_REQUIRED;
583 TALLOC_FREE(state->session->gensec);
585 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
586 tevent_req_nterror(req, status);
587 return tevent_req_post(req, ev);
591 status = tag_state_session_ptr(state);
592 if (tevent_req_nterror(req, status)) {
593 return tevent_req_post(req, ev);
596 if (state->session->gensec == NULL) {
597 status = auth_generic_prepare(state->session,
598 state->session->connection->remote_address,
599 &state->session->gensec);
600 if (tevent_req_nterror(req, status)) {
601 return tevent_req_post(req, ev);
604 gensec_want_feature(state->session->gensec, GENSEC_FEATURE_SESSION_KEY);
605 gensec_want_feature(state->session->gensec, GENSEC_FEATURE_UNIX_TOKEN);
607 status = gensec_start_mech_by_oid(state->session->gensec,
608 GENSEC_OID_SPNEGO);
609 if (tevent_req_nterror(req, status)) {
610 return tevent_req_post(req, ev);
614 become_root();
615 subreq = gensec_update_send(state, state->ev,
616 state->session->gensec,
617 state->in_security_buffer);
618 unbecome_root();
619 if (tevent_req_nomem(subreq, req)) {
620 return tevent_req_post(req, ev);
622 tevent_req_set_callback(subreq, smbd_smb2_session_setup_gensec_done, req);
624 return req;
627 static void smbd_smb2_session_setup_gensec_done(struct tevent_req *subreq)
629 struct tevent_req *req =
630 tevent_req_callback_data(subreq,
631 struct tevent_req);
632 struct smbd_smb2_session_setup_state *state =
633 tevent_req_data(req,
634 struct smbd_smb2_session_setup_state);
635 NTSTATUS status;
637 become_root();
638 status = gensec_update_recv(subreq, state,
639 &state->out_security_buffer);
640 unbecome_root();
641 TALLOC_FREE(subreq);
642 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
643 !NT_STATUS_IS_OK(status)) {
644 tevent_req_nterror(req, status);
645 return;
648 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
649 state->out_session_id = state->session->global->session_wire_id;
650 /* we want to keep the session */
651 state->session = NULL;
652 TALLOC_FREE(state->pp_self_ref);
653 tevent_req_nterror(req, status);
654 return;
657 status = gensec_session_info(state->session->gensec,
658 state->session->global,
659 &state->session_info);
660 if (tevent_req_nterror(req, status)) {
661 return;
664 if ((state->in_previous_session_id != 0) &&
665 (state->session->global->session_wire_id !=
666 state->in_previous_session_id))
668 subreq = smb2srv_session_close_previous_send(state, state->ev,
669 state->session->connection,
670 state->session_info,
671 state->in_previous_session_id,
672 state->session->global->session_wire_id);
673 if (tevent_req_nomem(subreq, req)) {
674 return;
676 tevent_req_set_callback(subreq,
677 smbd_smb2_session_setup_previous_done,
678 req);
679 return;
682 if (state->session->global->auth_session_info != NULL) {
683 status = smbd_smb2_reauth_generic_return(state->session,
684 state->smb2req,
685 state->session_info,
686 &state->out_session_flags,
687 &state->out_session_id);
688 if (tevent_req_nterror(req, status)) {
689 return;
691 /* we want to keep the session */
692 state->session = NULL;
693 TALLOC_FREE(state->pp_self_ref);
694 tevent_req_done(req);
695 return;
698 status = smbd_smb2_auth_generic_return(state->session,
699 state->smb2req,
700 state->in_security_mode,
701 state->session_info,
702 &state->out_session_flags,
703 &state->out_session_id);
704 if (tevent_req_nterror(req, status)) {
705 return;
708 /* we want to keep the session */
709 state->session = NULL;
710 TALLOC_FREE(state->pp_self_ref);
711 tevent_req_done(req);
712 return;
715 static void smbd_smb2_session_setup_previous_done(struct tevent_req *subreq)
717 struct tevent_req *req =
718 tevent_req_callback_data(subreq,
719 struct tevent_req);
720 struct smbd_smb2_session_setup_state *state =
721 tevent_req_data(req,
722 struct smbd_smb2_session_setup_state);
723 NTSTATUS status;
725 status = smb2srv_session_close_previous_recv(subreq);
726 TALLOC_FREE(subreq);
727 if (tevent_req_nterror(req, status)) {
728 return;
731 if (state->session->global->auth_session_info != NULL) {
732 status = smbd_smb2_reauth_generic_return(state->session,
733 state->smb2req,
734 state->session_info,
735 &state->out_session_flags,
736 &state->out_session_id);
737 if (tevent_req_nterror(req, status)) {
738 return;
740 /* we want to keep the session */
741 state->session = NULL;
742 TALLOC_FREE(state->pp_self_ref);
743 tevent_req_done(req);
744 return;
747 status = smbd_smb2_auth_generic_return(state->session,
748 state->smb2req,
749 state->in_security_mode,
750 state->session_info,
751 &state->out_session_flags,
752 &state->out_session_id);
753 if (tevent_req_nterror(req, status)) {
754 return;
757 /* we want to keep the session */
758 state->session = NULL;
759 TALLOC_FREE(state->pp_self_ref);
760 tevent_req_done(req);
761 return;
764 static NTSTATUS smbd_smb2_session_setup_recv(struct tevent_req *req,
765 uint16_t *out_session_flags,
766 TALLOC_CTX *mem_ctx,
767 DATA_BLOB *out_security_buffer,
768 uint64_t *out_session_id)
770 struct smbd_smb2_session_setup_state *state =
771 tevent_req_data(req,
772 struct smbd_smb2_session_setup_state);
773 NTSTATUS status;
775 if (tevent_req_is_nterror(req, &status)) {
776 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
777 tevent_req_received(req);
778 return nt_status_squash(status);
780 } else {
781 status = NT_STATUS_OK;
784 *out_session_flags = state->out_session_flags;
785 *out_security_buffer = state->out_security_buffer;
786 *out_session_id = state->out_session_id;
788 talloc_steal(mem_ctx, out_security_buffer->data);
789 tevent_req_received(req);
790 return status;
793 static struct tevent_req *smbd_smb2_logoff_send(TALLOC_CTX *mem_ctx,
794 struct tevent_context *ev,
795 struct smbd_smb2_request *smb2req);
796 static NTSTATUS smbd_smb2_logoff_recv(struct tevent_req *req);
797 static void smbd_smb2_request_logoff_done(struct tevent_req *subreq);
799 NTSTATUS smbd_smb2_request_process_logoff(struct smbd_smb2_request *req)
801 NTSTATUS status;
802 struct tevent_req *subreq = NULL;
804 status = smbd_smb2_request_verify_sizes(req, 0x04);
805 if (!NT_STATUS_IS_OK(status)) {
806 return smbd_smb2_request_error(req, status);
809 subreq = smbd_smb2_logoff_send(req, req->sconn->ev_ctx, req);
810 if (subreq == NULL) {
811 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
813 tevent_req_set_callback(subreq, smbd_smb2_request_logoff_done, req);
816 * Wait a long time before going async on this to allow
817 * requests we're waiting on to finish. Set timeout to 10 secs.
819 return smbd_smb2_request_pending_queue(req, subreq, 10000000);
822 static void smbd_smb2_request_logoff_done(struct tevent_req *subreq)
824 struct smbd_smb2_request *smb2req =
825 tevent_req_callback_data(subreq,
826 struct smbd_smb2_request);
827 DATA_BLOB outbody;
828 NTSTATUS status;
829 NTSTATUS error;
831 status = smbd_smb2_logoff_recv(subreq);
832 TALLOC_FREE(subreq);
833 if (!NT_STATUS_IS_OK(status)) {
834 error = smbd_smb2_request_error(smb2req, status);
835 if (!NT_STATUS_IS_OK(error)) {
836 smbd_server_connection_terminate(smb2req->sconn,
837 nt_errstr(error));
838 return;
840 return;
843 outbody = smbd_smb2_generate_outbody(smb2req, 0x04);
844 if (outbody.data == NULL) {
845 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
846 if (!NT_STATUS_IS_OK(error)) {
847 smbd_server_connection_terminate(smb2req->sconn,
848 nt_errstr(error));
849 return;
851 return;
854 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
855 SSVAL(outbody.data, 0x02, 0); /* reserved */
857 error = smbd_smb2_request_done(smb2req, outbody, NULL);
858 if (!NT_STATUS_IS_OK(error)) {
859 smbd_server_connection_terminate(smb2req->sconn,
860 nt_errstr(error));
861 return;
865 struct smbd_smb2_logout_state {
866 struct smbd_smb2_request *smb2req;
867 struct tevent_queue *wait_queue;
870 static void smbd_smb2_logoff_wait_done(struct tevent_req *subreq);
872 static struct tevent_req *smbd_smb2_logoff_send(TALLOC_CTX *mem_ctx,
873 struct tevent_context *ev,
874 struct smbd_smb2_request *smb2req)
876 struct tevent_req *req;
877 struct smbd_smb2_logout_state *state;
878 struct tevent_req *subreq;
879 struct smbd_smb2_request *preq;
880 struct smbXsrv_connection *xconn = smb2req->xconn;
882 req = tevent_req_create(mem_ctx, &state,
883 struct smbd_smb2_logout_state);
884 if (req == NULL) {
885 return NULL;
887 state->smb2req = smb2req;
889 state->wait_queue = tevent_queue_create(state, "logoff_wait_queue");
890 if (tevent_req_nomem(state->wait_queue, req)) {
891 return tevent_req_post(req, ev);
895 * Make sure that no new request will be able to use this session.
897 smb2req->session->status = NT_STATUS_USER_SESSION_DELETED;
899 for (preq = xconn->smb2.requests; preq != NULL; preq = preq->next) {
900 if (preq == smb2req) {
901 /* Can't cancel current request. */
902 continue;
904 if (preq->session != smb2req->session) {
905 /* Request on different session. */
906 continue;
910 * Never cancel anything in a compound
911 * request. Way too hard to deal with
912 * the result.
914 if (!preq->compound_related && preq->subreq != NULL) {
915 tevent_req_cancel(preq->subreq);
919 * Now wait until the request is finished.
921 * We don't set a callback, as we just want to block the
922 * wait queue and the talloc_free() of the request will
923 * remove the item from the wait queue.
925 subreq = tevent_queue_wait_send(preq, ev, state->wait_queue);
926 if (tevent_req_nomem(subreq, req)) {
927 return tevent_req_post(req, ev);
932 * Now we add our own waiter to the end of the queue,
933 * this way we get notified when all pending requests are finished
934 * and send to the socket.
936 subreq = tevent_queue_wait_send(state, ev, state->wait_queue);
937 if (tevent_req_nomem(subreq, req)) {
938 return tevent_req_post(req, ev);
940 tevent_req_set_callback(subreq, smbd_smb2_logoff_wait_done, req);
942 return req;
945 static void smbd_smb2_logoff_wait_done(struct tevent_req *subreq)
947 struct tevent_req *req = tevent_req_callback_data(
948 subreq, struct tevent_req);
949 struct smbd_smb2_logout_state *state = tevent_req_data(
950 req, struct smbd_smb2_logout_state);
951 NTSTATUS status;
953 tevent_queue_wait_recv(subreq);
954 TALLOC_FREE(subreq);
957 * As we've been awoken, we may have changed
958 * uid in the meantime. Ensure we're still
959 * root (SMB2_OP_LOGOFF has .as_root = true).
961 change_to_root_user();
963 status = smbXsrv_session_logoff(state->smb2req->session);
964 if (tevent_req_nterror(req, status)) {
965 return;
969 * we may need to sign the response, so we need to keep
970 * the session until the response is sent to the wire.
972 talloc_steal(state->smb2req, state->smb2req->session);
974 tevent_req_done(req);
977 static NTSTATUS smbd_smb2_logoff_recv(struct tevent_req *req)
979 return tevent_req_simple_recv_ntstatus(req);