2 Unix SMB/CIFS implementation.
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/>.
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "../auth/gensec/gensec.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
,
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
,
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
)
51 const uint8_t *inbody
;
52 uint64_t in_session_id
;
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
;
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
,
96 in_previous_session_id
,
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
);
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
;
119 NTSTATUS error
; /* transport error */
121 status
= smbd_smb2_session_setup_recv(subreq
,
124 &out_security_buffer
,
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
,
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
,
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
,
168 if (!NT_STATUS_IS_OK(error
)) {
169 smbd_server_connection_terminate(smb2req
->sconn
,
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
)
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;
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
,
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
,
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
,
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
));
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 "
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
,
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
;
367 smb2req
->do_signing
= true;
370 global_client_caps
|= (CAP_LEVEL_II_OPLOCKS
|CAP_STATUS32
);
372 *out_session_id
= session
->global
->session_wire_id
;
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
)
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
,
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
;
429 struct smbd_smb2_session_setup_state
{
430 struct tevent_context
*ev
;
431 struct smbd_smb2_request
*smb2req
;
432 uint64_t in_session_id
;
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
454 (*pp_state
)->pp_self_ref
= NULL
;
458 static int smbd_smb2_session_setup_state_destructor(struct smbd_smb2_session_setup_state
*state
)
460 struct smbd_smb2_request
*preq
;
463 * If state->session is not NULL,
464 * we move the session from the session table to the request on failure
465 * so that the error response can be correctly signed, but the session
466 * is then really deleted when the request is done.
469 if (state
->session
== NULL
) {
473 state
->session
->status
= NT_STATUS_USER_SESSION_DELETED
;
474 state
->smb2req
->session
= talloc_move(state
->smb2req
, &state
->session
);
477 * We've made this session owned by the current request.
478 * Ensure that any outstanding requests don't also refer
482 for (preq
= state
->smb2req
->sconn
->smb2
.requests
; preq
!= NULL
; preq
= preq
->next
) {
483 if (preq
== state
->smb2req
) {
486 if (preq
->session
== state
->smb2req
->session
) {
487 preq
->session
= NULL
;
489 * If we no longer have a session we can't
490 * sign or encrypt replies.
492 preq
->do_signing
= false;
493 preq
->do_encryption
= false;
500 static void smbd_smb2_session_setup_gensec_done(struct tevent_req
*subreq
);
501 static void smbd_smb2_session_setup_previous_done(struct tevent_req
*subreq
);
503 /************************************************************************
504 We have to tag the state->session pointer with memory talloc'ed
505 on it to ensure it gets NULL'ed out if the underlying struct smbXsrv_session
506 is deleted by shutdown whilst this request is in flight.
507 ************************************************************************/
509 static NTSTATUS
tag_state_session_ptr(struct smbd_smb2_session_setup_state
*state
)
511 state
->pp_self_ref
= talloc_zero(state
->session
,
512 struct smbd_smb2_session_setup_state
*);
513 if (state
->pp_self_ref
== NULL
) {
514 return NT_STATUS_NO_MEMORY
;
516 *state
->pp_self_ref
= state
;
517 talloc_set_destructor(state
->pp_self_ref
, pp_self_ref_destructor
);
521 static struct tevent_req
*smbd_smb2_session_setup_send(TALLOC_CTX
*mem_ctx
,
522 struct tevent_context
*ev
,
523 struct smbd_smb2_request
*smb2req
,
524 uint64_t in_session_id
,
526 uint8_t in_security_mode
,
527 uint64_t in_previous_session_id
,
528 DATA_BLOB in_security_buffer
)
530 struct tevent_req
*req
;
531 struct smbd_smb2_session_setup_state
*state
;
533 NTTIME now
= timeval_to_nttime(&smb2req
->request_time
);
534 struct tevent_req
*subreq
;
536 req
= tevent_req_create(mem_ctx
, &state
,
537 struct smbd_smb2_session_setup_state
);
542 state
->smb2req
= smb2req
;
543 state
->in_session_id
= in_session_id
;
544 state
->in_flags
= in_flags
;
545 state
->in_security_mode
= in_security_mode
;
546 state
->in_previous_session_id
= in_previous_session_id
;
547 state
->in_security_buffer
= in_security_buffer
;
549 if (in_flags
& SMB2_SESSION_FLAG_BINDING
) {
550 if (smb2req
->sconn
->conn
->protocol
< PROTOCOL_SMB2_22
) {
551 tevent_req_nterror(req
, NT_STATUS_REQUEST_NOT_ACCEPTED
);
552 return tevent_req_post(req
, ev
);
556 * We do not support multi channel.
558 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
559 return tevent_req_post(req
, ev
);
562 talloc_set_destructor(state
, smbd_smb2_session_setup_state_destructor
);
564 if (state
->in_session_id
== 0) {
565 /* create a new session */
566 status
= smbXsrv_session_create(state
->smb2req
->sconn
->conn
,
567 now
, &state
->session
);
568 if (tevent_req_nterror(req
, status
)) {
569 return tevent_req_post(req
, ev
);
572 status
= smb2srv_session_lookup(state
->smb2req
->sconn
->conn
,
573 state
->in_session_id
, now
,
575 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
)) {
576 status
= NT_STATUS_OK
;
578 if (NT_STATUS_IS_OK(status
)) {
579 state
->session
->status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
580 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
581 TALLOC_FREE(state
->session
->gensec
);
583 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
584 tevent_req_nterror(req
, status
);
585 return tevent_req_post(req
, ev
);
589 status
= tag_state_session_ptr(state
);
590 if (tevent_req_nterror(req
, status
)) {
591 return tevent_req_post(req
, ev
);
594 if (state
->session
->gensec
== NULL
) {
595 status
= auth_generic_prepare(state
->session
,
596 state
->session
->connection
->remote_address
,
597 &state
->session
->gensec
);
598 if (tevent_req_nterror(req
, status
)) {
599 return tevent_req_post(req
, ev
);
602 gensec_want_feature(state
->session
->gensec
, GENSEC_FEATURE_SESSION_KEY
);
603 gensec_want_feature(state
->session
->gensec
, GENSEC_FEATURE_UNIX_TOKEN
);
605 status
= gensec_start_mech_by_oid(state
->session
->gensec
,
607 if (tevent_req_nterror(req
, status
)) {
608 return tevent_req_post(req
, ev
);
613 subreq
= gensec_update_send(state
, state
->ev
,
614 state
->session
->gensec
,
615 state
->in_security_buffer
);
617 if (tevent_req_nomem(subreq
, req
)) {
618 return tevent_req_post(req
, ev
);
620 tevent_req_set_callback(subreq
, smbd_smb2_session_setup_gensec_done
, req
);
625 static void smbd_smb2_session_setup_gensec_done(struct tevent_req
*subreq
)
627 struct tevent_req
*req
=
628 tevent_req_callback_data(subreq
,
630 struct smbd_smb2_session_setup_state
*state
=
632 struct smbd_smb2_session_setup_state
);
636 status
= gensec_update_recv(subreq
, state
,
637 &state
->out_security_buffer
);
640 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) &&
641 !NT_STATUS_IS_OK(status
)) {
642 tevent_req_nterror(req
, status
);
646 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
647 state
->out_session_id
= state
->session
->global
->session_wire_id
;
648 /* we want to keep the session */
649 state
->session
= NULL
;
650 TALLOC_FREE(state
->pp_self_ref
);
651 tevent_req_nterror(req
, status
);
655 status
= gensec_session_info(state
->session
->gensec
,
656 state
->session
->global
,
657 &state
->session_info
);
658 if (tevent_req_nterror(req
, status
)) {
662 if ((state
->in_previous_session_id
!= 0) &&
663 (state
->session
->global
->session_wire_id
!=
664 state
->in_previous_session_id
))
666 subreq
= smb2srv_session_close_previous_send(state
, state
->ev
,
667 state
->session
->connection
,
669 state
->in_previous_session_id
,
670 state
->session
->global
->session_wire_id
);
671 if (tevent_req_nomem(subreq
, req
)) {
674 tevent_req_set_callback(subreq
,
675 smbd_smb2_session_setup_previous_done
,
680 if (state
->session
->global
->auth_session_info
!= NULL
) {
681 status
= smbd_smb2_reauth_generic_return(state
->session
,
684 &state
->out_session_flags
,
685 &state
->out_session_id
);
686 if (tevent_req_nterror(req
, status
)) {
689 /* we want to keep the session */
690 state
->session
= NULL
;
691 TALLOC_FREE(state
->pp_self_ref
);
692 tevent_req_done(req
);
696 status
= smbd_smb2_auth_generic_return(state
->session
,
698 state
->in_security_mode
,
700 &state
->out_session_flags
,
701 &state
->out_session_id
);
702 if (tevent_req_nterror(req
, status
)) {
706 /* we want to keep the session */
707 state
->session
= NULL
;
708 TALLOC_FREE(state
->pp_self_ref
);
709 tevent_req_done(req
);
713 static void smbd_smb2_session_setup_previous_done(struct tevent_req
*subreq
)
715 struct tevent_req
*req
=
716 tevent_req_callback_data(subreq
,
718 struct smbd_smb2_session_setup_state
*state
=
720 struct smbd_smb2_session_setup_state
);
723 status
= smb2srv_session_close_previous_recv(subreq
);
725 if (tevent_req_nterror(req
, status
)) {
729 if (state
->session
->global
->auth_session_info
!= NULL
) {
730 status
= smbd_smb2_reauth_generic_return(state
->session
,
733 &state
->out_session_flags
,
734 &state
->out_session_id
);
735 if (tevent_req_nterror(req
, status
)) {
738 /* we want to keep the session */
739 state
->session
= NULL
;
740 TALLOC_FREE(state
->pp_self_ref
);
741 tevent_req_done(req
);
745 status
= smbd_smb2_auth_generic_return(state
->session
,
747 state
->in_security_mode
,
749 &state
->out_session_flags
,
750 &state
->out_session_id
);
751 if (tevent_req_nterror(req
, status
)) {
755 /* we want to keep the session */
756 state
->session
= NULL
;
757 TALLOC_FREE(state
->pp_self_ref
);
758 tevent_req_done(req
);
762 static NTSTATUS
smbd_smb2_session_setup_recv(struct tevent_req
*req
,
763 uint16_t *out_session_flags
,
765 DATA_BLOB
*out_security_buffer
,
766 uint64_t *out_session_id
)
768 struct smbd_smb2_session_setup_state
*state
=
770 struct smbd_smb2_session_setup_state
);
773 if (tevent_req_is_nterror(req
, &status
)) {
774 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
775 tevent_req_received(req
);
776 return nt_status_squash(status
);
779 status
= NT_STATUS_OK
;
782 *out_session_flags
= state
->out_session_flags
;
783 *out_security_buffer
= state
->out_security_buffer
;
784 *out_session_id
= state
->out_session_id
;
786 talloc_steal(mem_ctx
, out_security_buffer
->data
);
787 tevent_req_received(req
);
791 static struct tevent_req
*smbd_smb2_logoff_send(TALLOC_CTX
*mem_ctx
,
792 struct tevent_context
*ev
,
793 struct smbd_smb2_request
*smb2req
);
794 static NTSTATUS
smbd_smb2_logoff_recv(struct tevent_req
*req
);
795 static void smbd_smb2_request_logoff_done(struct tevent_req
*subreq
);
797 NTSTATUS
smbd_smb2_request_process_logoff(struct smbd_smb2_request
*req
)
800 struct tevent_req
*subreq
= NULL
;
802 status
= smbd_smb2_request_verify_sizes(req
, 0x04);
803 if (!NT_STATUS_IS_OK(status
)) {
804 return smbd_smb2_request_error(req
, status
);
807 subreq
= smbd_smb2_logoff_send(req
, req
->sconn
->ev_ctx
, req
);
808 if (subreq
== NULL
) {
809 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
811 tevent_req_set_callback(subreq
, smbd_smb2_request_logoff_done
, req
);
814 * Wait a long time before going async on this to allow
815 * requests we're waiting on to finish. Set timeout to 10 secs.
817 return smbd_smb2_request_pending_queue(req
, subreq
, 10000000);
820 static void smbd_smb2_request_logoff_done(struct tevent_req
*subreq
)
822 struct smbd_smb2_request
*smb2req
=
823 tevent_req_callback_data(subreq
,
824 struct smbd_smb2_request
);
829 status
= smbd_smb2_logoff_recv(subreq
);
831 if (!NT_STATUS_IS_OK(status
)) {
832 error
= smbd_smb2_request_error(smb2req
, status
);
833 if (!NT_STATUS_IS_OK(error
)) {
834 smbd_server_connection_terminate(smb2req
->sconn
,
841 outbody
= smbd_smb2_generate_outbody(smb2req
, 0x04);
842 if (outbody
.data
== NULL
) {
843 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
844 if (!NT_STATUS_IS_OK(error
)) {
845 smbd_server_connection_terminate(smb2req
->sconn
,
852 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
853 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
855 error
= smbd_smb2_request_done(smb2req
, outbody
, NULL
);
856 if (!NT_STATUS_IS_OK(error
)) {
857 smbd_server_connection_terminate(smb2req
->sconn
,
863 struct smbd_smb2_logout_state
{
864 struct smbd_smb2_request
*smb2req
;
865 struct tevent_queue
*wait_queue
;
868 static void smbd_smb2_logoff_wait_done(struct tevent_req
*subreq
);
870 static struct tevent_req
*smbd_smb2_logoff_send(TALLOC_CTX
*mem_ctx
,
871 struct tevent_context
*ev
,
872 struct smbd_smb2_request
*smb2req
)
874 struct tevent_req
*req
;
875 struct smbd_smb2_logout_state
*state
;
876 struct tevent_req
*subreq
;
877 struct smbd_smb2_request
*preq
;
879 req
= tevent_req_create(mem_ctx
, &state
,
880 struct smbd_smb2_logout_state
);
884 state
->smb2req
= smb2req
;
886 state
->wait_queue
= tevent_queue_create(state
, "logoff_wait_queue");
887 if (tevent_req_nomem(state
->wait_queue
, req
)) {
888 return tevent_req_post(req
, ev
);
892 * Make sure that no new request will be able to use this session.
894 smb2req
->session
->status
= NT_STATUS_USER_SESSION_DELETED
;
896 for (preq
= smb2req
->sconn
->smb2
.requests
; preq
!= NULL
; preq
= preq
->next
) {
897 if (preq
== smb2req
) {
898 /* Can't cancel current request. */
901 if (preq
->session
!= smb2req
->session
) {
902 /* Request on different session. */
907 * Never cancel anything in a compound
908 * request. Way too hard to deal with
911 if (!preq
->compound_related
&& preq
->subreq
!= NULL
) {
912 tevent_req_cancel(preq
->subreq
);
916 * Now wait until the request is finished.
918 * We don't set a callback, as we just want to block the
919 * wait queue and the talloc_free() of the request will
920 * remove the item from the wait queue.
922 subreq
= tevent_queue_wait_send(preq
, ev
, state
->wait_queue
);
923 if (tevent_req_nomem(subreq
, req
)) {
924 return tevent_req_post(req
, ev
);
929 * Now we add our own waiter to the end of the queue,
930 * this way we get notified when all pending requests are finished
931 * and send to the socket.
933 subreq
= tevent_queue_wait_send(state
, ev
, state
->wait_queue
);
934 if (tevent_req_nomem(subreq
, req
)) {
935 return tevent_req_post(req
, ev
);
937 tevent_req_set_callback(subreq
, smbd_smb2_logoff_wait_done
, req
);
942 static void smbd_smb2_logoff_wait_done(struct tevent_req
*subreq
)
944 struct tevent_req
*req
= tevent_req_callback_data(
945 subreq
, struct tevent_req
);
946 struct smbd_smb2_logout_state
*state
= tevent_req_data(
947 req
, struct smbd_smb2_logout_state
);
950 tevent_queue_wait_recv(subreq
);
954 * As we've been awoken, we may have changed
955 * uid in the meantime. Ensure we're still
956 * root (SMB2_OP_LOGOFF has .as_root = true).
958 change_to_root_user();
960 status
= smbXsrv_session_logoff(state
->smb2req
->session
);
961 if (tevent_req_nterror(req
, status
)) {
966 * we may need to sign the response, so we need to keep
967 * the session until the response is sent to the wire.
969 talloc_steal(state
->smb2req
, state
->smb2req
->session
);
971 tevent_req_done(req
);
974 static NTSTATUS
smbd_smb2_logoff_recv(struct tevent_req
*req
)
976 return tevent_req_simple_recv_ntstatus(req
);