smbd:smb2: only enable encryption in session if desired
[Samba.git] / source3 / smbd / smb2_sesssetup.c
blobbf40ab49157d9d93a8b9acc17b972d53ec306478
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_wrap_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_wrap_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_wrap_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;
115 uint64_t out_session_id;
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_wrap_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 = data_blob_talloc(smb2req->out.vector, NULL, 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_DESIRED) &&
194 (conn->smb2.client.capabilities & SMB2_CAP_ENCRYPTION)) {
195 x->encryption_desired = true;
198 if (lp_smb_encrypt(-1) == SMB_SIGNING_REQUIRED) {
199 x->encryption_desired = true;
200 x->global->encryption_required = true;
203 if (security_session_user_level(session_info, NULL) < SECURITY_USER) {
204 /* we map anonymous to guest internally */
205 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
206 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
207 /* force no signing */
208 x->global->signing_required = false;
209 guest = true;
212 if (guest && x->global->encryption_required) {
213 DEBUG(1,("reject guest session as encryption is required\n"));
214 return NT_STATUS_ACCESS_DENIED;
217 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
218 if (x->global->encryption_required) {
219 DEBUG(1,("reject session with dialect[0x%04X] "
220 "as encryption is required\n",
221 conn->smb2.server.dialect));
222 return NT_STATUS_ACCESS_DENIED;
226 if (x->encryption_desired) {
227 *out_session_flags |= SMB2_SESSION_FLAG_ENCRYPT_DATA;
230 ZERO_STRUCT(session_key);
231 memcpy(session_key, session_info->session_key.data,
232 MIN(session_info->session_key.length, sizeof(session_key)));
234 x->global->signing_key = data_blob_talloc(x->global,
235 session_key,
236 sizeof(session_key));
237 if (x->global->signing_key.data == NULL) {
238 ZERO_STRUCT(session_key);
239 return NT_STATUS_NO_MEMORY;
242 if (conn->protocol >= PROTOCOL_SMB2_24) {
243 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
244 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
246 smb2_key_derivation(session_key, sizeof(session_key),
247 label.data, label.length,
248 context.data, context.length,
249 x->global->signing_key.data);
252 if (conn->protocol >= PROTOCOL_SMB2_24) {
253 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
254 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
256 x->global->decryption_key = data_blob_talloc(x->global,
257 session_key,
258 sizeof(session_key));
259 if (x->global->decryption_key.data == NULL) {
260 ZERO_STRUCT(session_key);
261 return NT_STATUS_NO_MEMORY;
264 smb2_key_derivation(session_key, sizeof(session_key),
265 label.data, label.length,
266 context.data, context.length,
267 x->global->decryption_key.data);
270 if (conn->protocol >= PROTOCOL_SMB2_24) {
271 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
272 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
274 x->global->encryption_key = data_blob_talloc(x->global,
275 session_key,
276 sizeof(session_key));
277 if (x->global->encryption_key.data == NULL) {
278 ZERO_STRUCT(session_key);
279 return NT_STATUS_NO_MEMORY;
282 smb2_key_derivation(session_key, sizeof(session_key),
283 label.data, label.length,
284 context.data, context.length,
285 x->global->encryption_key.data);
287 generate_random_buffer((uint8_t *)&x->nonce_high, sizeof(x->nonce_high));
288 x->nonce_low = 1;
291 x->global->application_key = data_blob_dup_talloc(x->global,
292 x->global->signing_key);
293 if (x->global->application_key.data == NULL) {
294 ZERO_STRUCT(session_key);
295 return NT_STATUS_NO_MEMORY;
298 if (conn->protocol >= PROTOCOL_SMB2_24) {
299 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
300 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
302 smb2_key_derivation(session_key, sizeof(session_key),
303 label.data, label.length,
304 context.data, context.length,
305 x->global->application_key.data);
307 ZERO_STRUCT(session_key);
309 x->global->channels[0].signing_key = data_blob_dup_talloc(x->global->channels,
310 x->global->signing_key);
311 if (x->global->channels[0].signing_key.data == NULL) {
312 return NT_STATUS_NO_MEMORY;
315 data_blob_clear_free(&session_info->session_key);
316 session_info->session_key = data_blob_dup_talloc(session_info,
317 x->global->application_key);
318 if (session_info->session_key.data == NULL) {
319 return NT_STATUS_NO_MEMORY;
322 session->compat = talloc_zero(session, struct user_struct);
323 if (session->compat == NULL) {
324 return NT_STATUS_NO_MEMORY;
326 session->compat->session = session;
327 session->compat->homes_snum = -1;
328 session->compat->session_info = session_info;
329 session->compat->session_keystr = NULL;
330 session->compat->vuid = session->global->session_wire_id;
331 DLIST_ADD(smb2req->sconn->users, session->compat);
332 smb2req->sconn->num_users++;
334 if (security_session_user_level(session_info, NULL) >= SECURITY_USER) {
335 session->compat->homes_snum =
336 register_homes_share(session_info->unix_info->unix_name);
339 set_current_user_info(session_info->unix_info->sanitized_username,
340 session_info->unix_info->unix_name,
341 session_info->info->domain_name);
343 reload_services(smb2req->sconn, conn_snum_used, true);
345 session->status = NT_STATUS_OK;
346 session->global->auth_session_info = session_info;
347 session->global->auth_session_info_seqnum += 1;
348 session->global->channels[0].auth_session_info_seqnum =
349 session->global->auth_session_info_seqnum;
350 session->global->expiration_time = gensec_expire_time(session->gensec);
352 if (!session_claim(session)) {
353 DEBUG(1, ("smb2: Failed to claim session "
354 "for vuid=%llu\n",
355 (unsigned long long)session->compat->vuid));
356 return NT_STATUS_LOGON_FAILURE;
359 status = smbXsrv_session_update(session);
360 if (!NT_STATUS_IS_OK(status)) {
361 DEBUG(0, ("smb2: Failed to update session for vuid=%llu - %s\n",
362 (unsigned long long)session->compat->vuid,
363 nt_errstr(status)));
364 return NT_STATUS_LOGON_FAILURE;
368 * we attach the session to the request
369 * so that the response can be signed
371 if (!guest) {
372 smb2req->do_signing = true;
375 global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
377 *out_session_id = session->global->session_wire_id;
379 return NT_STATUS_OK;
382 static NTSTATUS smbd_smb2_reauth_generic_return(struct smbXsrv_session *session,
383 struct smbd_smb2_request *smb2req,
384 struct auth_session_info *session_info,
385 uint16_t *out_session_flags,
386 uint64_t *out_session_id)
388 NTSTATUS status;
389 struct smbXsrv_session *x = session;
390 struct smbXsrv_connection *conn = session->connection;
392 data_blob_clear_free(&session_info->session_key);
393 session_info->session_key = data_blob_dup_talloc(session_info,
394 x->global->application_key);
395 if (session_info->session_key.data == NULL) {
396 return NT_STATUS_NO_MEMORY;
399 session->compat->session_info = session_info;
400 session->compat->vuid = session->global->session_wire_id;
402 session->compat->homes_snum =
403 register_homes_share(session_info->unix_info->unix_name);
405 set_current_user_info(session_info->unix_info->sanitized_username,
406 session_info->unix_info->unix_name,
407 session_info->info->domain_name);
409 reload_services(smb2req->sconn, conn_snum_used, true);
411 session->status = NT_STATUS_OK;
412 TALLOC_FREE(session->global->auth_session_info);
413 session->global->auth_session_info = session_info;
414 session->global->auth_session_info_seqnum += 1;
415 session->global->channels[0].auth_session_info_seqnum =
416 session->global->auth_session_info_seqnum;
417 session->global->expiration_time = gensec_expire_time(session->gensec);
419 status = smbXsrv_session_update(session);
420 if (!NT_STATUS_IS_OK(status)) {
421 DEBUG(0, ("smb2: Failed to update session for vuid=%llu - %s\n",
422 (unsigned long long)session->compat->vuid,
423 nt_errstr(status)));
424 return NT_STATUS_LOGON_FAILURE;
427 conn_clear_vuid_caches(conn->sconn, session->compat->vuid);
429 if (security_session_user_level(session_info, NULL) >= SECURITY_USER) {
430 smb2req->do_signing = true;
433 *out_session_id = session->global->session_wire_id;
435 return NT_STATUS_OK;
438 struct smbd_smb2_session_setup_state {
439 struct tevent_context *ev;
440 struct smbd_smb2_request *smb2req;
441 uint64_t in_session_id;
442 uint8_t in_flags;
443 uint8_t in_security_mode;
444 uint64_t in_previous_session_id;
445 DATA_BLOB in_security_buffer;
446 struct smbXsrv_session *session;
447 struct auth_session_info *session_info;
448 uint16_t out_session_flags;
449 DATA_BLOB out_security_buffer;
450 uint64_t out_session_id;
453 static void smbd_smb2_session_setup_gensec_done(struct tevent_req *subreq);
454 static void smbd_smb2_session_setup_previous_done(struct tevent_req *subreq);
456 static struct tevent_req *smbd_smb2_session_setup_send(TALLOC_CTX *mem_ctx,
457 struct tevent_context *ev,
458 struct smbd_smb2_request *smb2req,
459 uint64_t in_session_id,
460 uint8_t in_flags,
461 uint8_t in_security_mode,
462 uint64_t in_previous_session_id,
463 DATA_BLOB in_security_buffer)
465 struct tevent_req *req;
466 struct smbd_smb2_session_setup_state *state;
467 NTSTATUS status;
468 NTTIME now = timeval_to_nttime(&smb2req->request_time);
469 struct tevent_req *subreq;
471 req = tevent_req_create(mem_ctx, &state,
472 struct smbd_smb2_session_setup_state);
473 if (req == NULL) {
474 return NULL;
476 state->ev = ev;
477 state->smb2req = smb2req;
478 state->in_session_id = in_session_id;
479 state->in_flags = in_flags;
480 state->in_security_mode = in_security_mode;
481 state->in_previous_session_id = in_previous_session_id;
482 state->in_security_buffer = in_security_buffer;
484 if (in_flags & SMB2_SESSION_FLAG_BINDING) {
485 if (smb2req->sconn->conn->protocol < PROTOCOL_SMB2_22) {
486 tevent_req_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
487 return tevent_req_post(req, ev);
491 * We do not support multi channel.
493 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
494 return tevent_req_post(req, ev);
497 if (state->in_session_id == 0) {
498 /* create a new session */
499 status = smbXsrv_session_create(state->smb2req->sconn->conn,
500 now, &state->session);
501 if (tevent_req_nterror(req, status)) {
502 return tevent_req_post(req, ev);
504 smb2req->session = state->session;
505 } else {
506 status = smb2srv_session_lookup(state->smb2req->sconn->conn,
507 state->in_session_id, now,
508 &state->session);
509 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
510 status = NT_STATUS_OK;
512 if (NT_STATUS_IS_OK(status)) {
513 state->session->status = NT_STATUS_MORE_PROCESSING_REQUIRED;
514 status = NT_STATUS_MORE_PROCESSING_REQUIRED;
515 TALLOC_FREE(state->session->gensec);
517 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
518 tevent_req_nterror(req, status);
519 return tevent_req_post(req, ev);
523 if (state->session->gensec == NULL) {
524 status = auth_generic_prepare(state->session,
525 state->session->connection->remote_address,
526 &state->session->gensec);
527 if (tevent_req_nterror(req, status)) {
528 return tevent_req_post(req, ev);
531 gensec_want_feature(state->session->gensec, GENSEC_FEATURE_SESSION_KEY);
532 gensec_want_feature(state->session->gensec, GENSEC_FEATURE_UNIX_TOKEN);
534 status = gensec_start_mech_by_oid(state->session->gensec,
535 GENSEC_OID_SPNEGO);
536 if (tevent_req_nterror(req, status)) {
537 return tevent_req_post(req, ev);
541 become_root();
542 subreq = gensec_update_send(state, state->ev,
543 state->session->gensec,
544 state->in_security_buffer);
545 unbecome_root();
546 if (tevent_req_nomem(subreq, req)) {
547 return tevent_req_post(req, ev);
549 tevent_req_set_callback(subreq, smbd_smb2_session_setup_gensec_done, req);
551 return req;
554 static void smbd_smb2_session_setup_gensec_done(struct tevent_req *subreq)
556 struct tevent_req *req =
557 tevent_req_callback_data(subreq,
558 struct tevent_req);
559 struct smbd_smb2_session_setup_state *state =
560 tevent_req_data(req,
561 struct smbd_smb2_session_setup_state);
562 NTSTATUS status;
564 become_root();
565 status = gensec_update_recv(subreq, state,
566 &state->out_security_buffer);
567 unbecome_root();
568 TALLOC_FREE(subreq);
569 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
570 !NT_STATUS_IS_OK(status)) {
571 tevent_req_nterror(req, status);
572 return;
575 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
576 state->out_session_id = state->session->global->session_wire_id;
577 tevent_req_nterror(req, status);
578 return;
581 status = gensec_session_info(state->session->gensec,
582 state->session->global,
583 &state->session_info);
584 if (tevent_req_nterror(req, status)) {
585 return;
588 if ((state->in_previous_session_id != 0) &&
589 (state->session->global->session_wire_id !=
590 state->in_previous_session_id))
592 subreq = smb2srv_session_close_previous_send(state, state->ev,
593 state->session->connection,
594 state->session_info,
595 state->in_previous_session_id,
596 state->session->global->session_wire_id);
597 if (tevent_req_nomem(subreq, req)) {
598 return;
600 tevent_req_set_callback(subreq,
601 smbd_smb2_session_setup_previous_done,
602 req);
603 return;
606 if (state->session->global->auth_session_info != NULL) {
607 status = smbd_smb2_reauth_generic_return(state->session,
608 state->smb2req,
609 state->session_info,
610 &state->out_session_flags,
611 &state->out_session_id);
612 if (tevent_req_nterror(req, status)) {
613 return;
615 tevent_req_done(req);
616 return;
619 status = smbd_smb2_auth_generic_return(state->session,
620 state->smb2req,
621 state->in_security_mode,
622 state->session_info,
623 &state->out_session_flags,
624 &state->out_session_id);
625 if (tevent_req_nterror(req, status)) {
626 return;
629 tevent_req_done(req);
630 return;
633 static void smbd_smb2_session_setup_previous_done(struct tevent_req *subreq)
635 struct tevent_req *req =
636 tevent_req_callback_data(subreq,
637 struct tevent_req);
638 struct smbd_smb2_session_setup_state *state =
639 tevent_req_data(req,
640 struct smbd_smb2_session_setup_state);
641 NTSTATUS status;
643 status = smb2srv_session_close_previous_recv(subreq);
644 TALLOC_FREE(subreq);
645 if (tevent_req_nterror(req, status)) {
646 return;
649 if (state->session->global->auth_session_info != NULL) {
650 status = smbd_smb2_reauth_generic_return(state->session,
651 state->smb2req,
652 state->session_info,
653 &state->out_session_flags,
654 &state->out_session_id);
655 if (tevent_req_nterror(req, status)) {
656 return;
658 tevent_req_done(req);
659 return;
662 status = smbd_smb2_auth_generic_return(state->session,
663 state->smb2req,
664 state->in_security_mode,
665 state->session_info,
666 &state->out_session_flags,
667 &state->out_session_id);
668 if (tevent_req_nterror(req, status)) {
669 return;
672 tevent_req_done(req);
673 return;
676 static NTSTATUS smbd_smb2_session_setup_recv(struct tevent_req *req,
677 uint16_t *out_session_flags,
678 TALLOC_CTX *mem_ctx,
679 DATA_BLOB *out_security_buffer,
680 uint64_t *out_session_id)
682 struct smbd_smb2_session_setup_state *state =
683 tevent_req_data(req,
684 struct smbd_smb2_session_setup_state);
685 NTSTATUS status;
687 if (tevent_req_is_nterror(req, &status)) {
688 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
689 tevent_req_received(req);
690 return nt_status_squash(status);
692 } else {
693 status = NT_STATUS_OK;
696 *out_session_flags = state->out_session_flags;
697 *out_security_buffer = state->out_security_buffer;
698 *out_session_id = state->out_session_id;
700 talloc_steal(mem_ctx, out_security_buffer->data);
701 tevent_req_received(req);
702 return status;
705 struct smbd_smb2_session_setup_wrap_state {
706 struct tevent_context *ev;
707 struct smbd_smb2_request *smb2req;
708 uint64_t in_session_id;
709 uint8_t in_flags;
710 uint8_t in_security_mode;
711 uint64_t in_previous_session_id;
712 DATA_BLOB in_security_buffer;
713 uint16_t out_session_flags;
714 DATA_BLOB out_security_buffer;
715 uint64_t out_session_id;
716 NTSTATUS error;
719 static void smbd_smb2_session_setup_wrap_setup_done(struct tevent_req *subreq);
720 static void smbd_smb2_session_setup_wrap_shutdown_done(struct tevent_req *subreq);
722 static struct tevent_req *smbd_smb2_session_setup_wrap_send(TALLOC_CTX *mem_ctx,
723 struct tevent_context *ev,
724 struct smbd_smb2_request *smb2req,
725 uint64_t in_session_id,
726 uint8_t in_flags,
727 uint8_t in_security_mode,
728 uint64_t in_previous_session_id,
729 DATA_BLOB in_security_buffer)
731 struct tevent_req *req;
732 struct smbd_smb2_session_setup_wrap_state *state;
733 struct tevent_req *subreq;
735 req = tevent_req_create(mem_ctx, &state,
736 struct smbd_smb2_session_setup_wrap_state);
737 if (req == NULL) {
738 return NULL;
740 state->ev = ev;
741 state->smb2req = smb2req;
742 state->in_session_id = in_session_id;
743 state->in_flags = in_flags;
744 state->in_security_mode = in_security_mode;
745 state->in_previous_session_id = in_previous_session_id;
746 state->in_security_buffer = in_security_buffer;
748 subreq = smbd_smb2_session_setup_send(state, state->ev,
749 state->smb2req,
750 state->in_session_id,
751 state->in_flags,
752 state->in_security_mode,
753 state->in_previous_session_id,
754 state->in_security_buffer);
755 if (tevent_req_nomem(subreq, req)) {
756 return tevent_req_post(req, ev);
758 tevent_req_set_callback(subreq,
759 smbd_smb2_session_setup_wrap_setup_done, req);
761 return req;
764 static void smbd_smb2_session_setup_wrap_setup_done(struct tevent_req *subreq)
766 struct tevent_req *req =
767 tevent_req_callback_data(subreq,
768 struct tevent_req);
769 struct smbd_smb2_session_setup_wrap_state *state =
770 tevent_req_data(req,
771 struct smbd_smb2_session_setup_wrap_state);
772 NTSTATUS status;
774 status = smbd_smb2_session_setup_recv(subreq,
775 &state->out_session_flags,
776 state,
777 &state->out_security_buffer,
778 &state->out_session_id);
779 TALLOC_FREE(subreq);
780 if (NT_STATUS_IS_OK(status)) {
781 tevent_req_done(req);
782 return;
784 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
785 tevent_req_nterror(req, status);
786 return;
789 if (state->smb2req->session == NULL) {
790 tevent_req_nterror(req, status);
791 return;
794 state->error = status;
796 subreq = smb2srv_session_shutdown_send(state, state->ev,
797 state->smb2req->session,
798 state->smb2req);
799 if (tevent_req_nomem(subreq, req)) {
800 return;
802 tevent_req_set_callback(subreq,
803 smbd_smb2_session_setup_wrap_shutdown_done,
804 req);
807 static void smbd_smb2_session_setup_wrap_shutdown_done(struct tevent_req *subreq)
809 struct tevent_req *req =
810 tevent_req_callback_data(subreq,
811 struct tevent_req);
812 struct smbd_smb2_session_setup_wrap_state *state =
813 tevent_req_data(req,
814 struct smbd_smb2_session_setup_wrap_state);
815 NTSTATUS status;
817 status = smb2srv_session_shutdown_recv(subreq);
818 TALLOC_FREE(subreq);
819 if (tevent_req_nterror(req, status)) {
820 return;
824 * we may need to sign the response, so we need to keep
825 * the session until the response is sent to the wire.
827 talloc_steal(state->smb2req, state->smb2req->session);
829 tevent_req_nterror(req, state->error);
832 static NTSTATUS smbd_smb2_session_setup_wrap_recv(struct tevent_req *req,
833 uint16_t *out_session_flags,
834 TALLOC_CTX *mem_ctx,
835 DATA_BLOB *out_security_buffer,
836 uint64_t *out_session_id)
838 struct smbd_smb2_session_setup_wrap_state *state =
839 tevent_req_data(req,
840 struct smbd_smb2_session_setup_wrap_state);
841 NTSTATUS status;
843 if (tevent_req_is_nterror(req, &status)) {
844 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
845 tevent_req_received(req);
846 return nt_status_squash(status);
848 } else {
849 status = NT_STATUS_OK;
852 *out_session_flags = state->out_session_flags;
853 *out_security_buffer = state->out_security_buffer;
854 *out_session_id = state->out_session_id;
856 talloc_steal(mem_ctx, out_security_buffer->data);
857 tevent_req_received(req);
858 return status;
861 static struct tevent_req *smbd_smb2_logoff_send(TALLOC_CTX *mem_ctx,
862 struct tevent_context *ev,
863 struct smbd_smb2_request *smb2req);
864 static NTSTATUS smbd_smb2_logoff_recv(struct tevent_req *req);
865 static void smbd_smb2_request_logoff_done(struct tevent_req *subreq);
867 NTSTATUS smbd_smb2_request_process_logoff(struct smbd_smb2_request *req)
869 NTSTATUS status;
870 struct tevent_req *subreq = NULL;
872 status = smbd_smb2_request_verify_sizes(req, 0x04);
873 if (!NT_STATUS_IS_OK(status)) {
874 return smbd_smb2_request_error(req, status);
877 subreq = smbd_smb2_logoff_send(req, req->sconn->ev_ctx, req);
878 if (subreq == NULL) {
879 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
881 tevent_req_set_callback(subreq, smbd_smb2_request_logoff_done, req);
884 * Wait a long time before going async on this to allow
885 * requests we're waiting on to finish. Set timeout to 10 secs.
887 return smbd_smb2_request_pending_queue(req, subreq, 10000000);
890 static void smbd_smb2_request_logoff_done(struct tevent_req *subreq)
892 struct smbd_smb2_request *smb2req =
893 tevent_req_callback_data(subreq,
894 struct smbd_smb2_request);
895 DATA_BLOB outbody;
896 NTSTATUS status;
897 NTSTATUS error;
899 status = smbd_smb2_logoff_recv(subreq);
900 TALLOC_FREE(subreq);
901 if (!NT_STATUS_IS_OK(status)) {
902 error = smbd_smb2_request_error(smb2req, status);
903 if (!NT_STATUS_IS_OK(error)) {
904 smbd_server_connection_terminate(smb2req->sconn,
905 nt_errstr(error));
906 return;
908 return;
911 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x04);
912 if (outbody.data == NULL) {
913 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
914 if (!NT_STATUS_IS_OK(error)) {
915 smbd_server_connection_terminate(smb2req->sconn,
916 nt_errstr(error));
917 return;
919 return;
922 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
923 SSVAL(outbody.data, 0x02, 0); /* reserved */
925 error = smbd_smb2_request_done(smb2req, outbody, NULL);
926 if (!NT_STATUS_IS_OK(error)) {
927 smbd_server_connection_terminate(smb2req->sconn,
928 nt_errstr(error));
929 return;
933 struct smbd_smb2_logoff_state {
934 struct smbd_smb2_request *smb2req;
937 static void smbd_smb2_logoff_shutdown_done(struct tevent_req *subreq);
939 static struct tevent_req *smbd_smb2_logoff_send(TALLOC_CTX *mem_ctx,
940 struct tevent_context *ev,
941 struct smbd_smb2_request *smb2req)
943 struct tevent_req *req;
944 struct smbd_smb2_logoff_state *state;
945 struct tevent_req *subreq;
947 req = tevent_req_create(mem_ctx, &state,
948 struct smbd_smb2_logoff_state);
949 if (req == NULL) {
950 return NULL;
952 state->smb2req = smb2req;
954 subreq = smb2srv_session_shutdown_send(state, ev,
955 smb2req->session,
956 smb2req);
957 if (tevent_req_nomem(subreq, req)) {
958 return tevent_req_post(req, ev);
960 tevent_req_set_callback(subreq, smbd_smb2_logoff_shutdown_done, req);
962 return req;
965 static void smbd_smb2_logoff_shutdown_done(struct tevent_req *subreq)
967 struct tevent_req *req = tevent_req_callback_data(
968 subreq, struct tevent_req);
969 struct smbd_smb2_logoff_state *state = tevent_req_data(
970 req, struct smbd_smb2_logoff_state);
971 NTSTATUS status;
973 status = smb2srv_session_shutdown_recv(subreq);
974 if (tevent_req_nterror(req, status)) {
975 return;
977 TALLOC_FREE(subreq);
980 * As we've been awoken, we may have changed
981 * uid in the meantime. Ensure we're still
982 * root (SMB2_OP_LOGOFF has .as_root = true).
984 change_to_root_user();
986 status = smbXsrv_session_logoff(state->smb2req->session);
987 if (tevent_req_nterror(req, status)) {
988 return;
992 * we may need to sign the response, so we need to keep
993 * the session until the response is sent to the wire.
995 talloc_steal(state->smb2req, state->smb2req->session);
997 tevent_req_done(req);
1000 static NTSTATUS smbd_smb2_logoff_recv(struct tevent_req *req)
1002 return tevent_req_simple_recv_ntstatus(req);