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"
31 #include "source3/lib/substitute.h"
33 #include "lib/crypto/gnutls_helpers.h"
34 #include <gnutls/gnutls.h>
35 #include <gnutls/crypto.h>
38 #define DBGC_CLASS DBGC_SMB2
40 static struct tevent_req
*smbd_smb2_session_setup_wrap_send(TALLOC_CTX
*mem_ctx
,
41 struct tevent_context
*ev
,
42 struct smbd_smb2_request
*smb2req
,
43 uint64_t in_session_id
,
45 uint8_t in_security_mode
,
46 uint64_t in_previous_session_id
,
47 DATA_BLOB in_security_buffer
);
48 static NTSTATUS
smbd_smb2_session_setup_wrap_recv(struct tevent_req
*req
,
49 uint16_t *out_session_flags
,
51 DATA_BLOB
*out_security_buffer
,
52 uint64_t *out_session_id
);
54 static void smbd_smb2_request_sesssetup_done(struct tevent_req
*subreq
);
56 NTSTATUS
smbd_smb2_request_process_sesssetup(struct smbd_smb2_request
*smb2req
)
59 const uint8_t *inbody
;
60 uint64_t in_session_id
;
62 uint8_t in_security_mode
;
63 uint64_t in_previous_session_id
;
64 uint16_t in_security_offset
;
65 uint16_t in_security_length
;
66 DATA_BLOB in_security_buffer
;
68 struct tevent_req
*subreq
;
70 status
= smbd_smb2_request_verify_sizes(smb2req
, 0x19);
71 if (!NT_STATUS_IS_OK(status
)) {
72 return smbd_smb2_request_error(smb2req
, status
);
74 inhdr
= SMBD_SMB2_IN_HDR_PTR(smb2req
);
75 inbody
= SMBD_SMB2_IN_BODY_PTR(smb2req
);
77 in_session_id
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
79 in_flags
= CVAL(inbody
, 0x02);
80 in_security_mode
= CVAL(inbody
, 0x03);
81 /* Capabilities = IVAL(inbody, 0x04) */
82 /* Channel = IVAL(inbody, 0x08) */
83 in_security_offset
= SVAL(inbody
, 0x0C);
84 in_security_length
= SVAL(inbody
, 0x0E);
85 in_previous_session_id
= BVAL(inbody
, 0x10);
87 if (in_security_offset
!= (SMB2_HDR_BODY
+ SMBD_SMB2_IN_BODY_LEN(smb2req
))) {
88 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
91 if (in_security_length
> SMBD_SMB2_IN_DYN_LEN(smb2req
)) {
92 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
95 in_security_buffer
.data
= SMBD_SMB2_IN_DYN_PTR(smb2req
);
96 in_security_buffer
.length
= in_security_length
;
98 subreq
= smbd_smb2_session_setup_wrap_send(smb2req
,
99 smb2req
->sconn
->ev_ctx
,
104 in_previous_session_id
,
106 if (subreq
== NULL
) {
107 return smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
109 tevent_req_set_callback(subreq
, smbd_smb2_request_sesssetup_done
, smb2req
);
112 * Avoid sending a STATUS_PENDING message, which
113 * matches a Windows Server and avoids problems with
116 * Even after 90 seconds a Windows Server doesn't return
117 * STATUS_PENDING if using NTLMSSP against a non reachable
120 return smbd_smb2_request_pending_queue(smb2req
, subreq
, 0);
123 static void smbd_smb2_request_sesssetup_done(struct tevent_req
*subreq
)
125 struct smbd_smb2_request
*smb2req
=
126 tevent_req_callback_data(subreq
,
127 struct smbd_smb2_request
);
131 uint16_t out_session_flags
= 0;
132 uint64_t out_session_id
= 0;
133 uint16_t out_security_offset
;
134 DATA_BLOB out_security_buffer
= data_blob_null
;
136 NTSTATUS error
; /* transport error */
138 status
= smbd_smb2_session_setup_wrap_recv(subreq
,
141 &out_security_buffer
,
144 if (!NT_STATUS_IS_OK(status
) &&
145 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
146 status
= nt_status_squash(status
);
147 error
= smbd_smb2_request_error(smb2req
, status
);
148 if (!NT_STATUS_IS_OK(error
)) {
149 smbd_server_connection_terminate(smb2req
->xconn
,
156 out_security_offset
= SMB2_HDR_BODY
+ 0x08;
158 outhdr
= SMBD_SMB2_OUT_HDR_PTR(smb2req
);
160 outbody
= smbd_smb2_generate_outbody(smb2req
, 0x08);
161 if (outbody
.data
== NULL
) {
162 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
163 if (!NT_STATUS_IS_OK(error
)) {
164 smbd_server_connection_terminate(smb2req
->xconn
,
171 SBVAL(outhdr
, SMB2_HDR_SESSION_ID
, out_session_id
);
173 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
174 SSVAL(outbody
.data
, 0x02,
175 out_session_flags
); /* session flags */
176 SSVAL(outbody
.data
, 0x04,
177 out_security_offset
); /* security buffer offset */
178 SSVAL(outbody
.data
, 0x06,
179 out_security_buffer
.length
); /* security buffer length */
181 outdyn
= out_security_buffer
;
183 error
= smbd_smb2_request_done_ex(smb2req
, status
, outbody
, &outdyn
,
185 if (!NT_STATUS_IS_OK(error
)) {
186 smbd_server_connection_terminate(smb2req
->xconn
,
192 static NTSTATUS
smbd_smb2_auth_generic_return(struct smbXsrv_session
*session
,
193 struct smbXsrv_session_auth0
**_auth
,
194 struct smbd_smb2_request
*smb2req
,
195 uint8_t in_security_mode
,
196 struct auth_session_info
*session_info
,
197 uint16_t *out_session_flags
,
198 uint64_t *out_session_id
)
202 struct smbXsrv_session
*x
= session
;
203 struct smbXsrv_session_auth0
*auth
= *_auth
;
204 struct smbXsrv_connection
*xconn
= smb2req
->xconn
;
206 struct smb2_signing_derivations derivations
= {
209 DATA_BLOB preauth_hash
= data_blob_null
;
213 if (xconn
->protocol
>= PROTOCOL_SMB3_11
) {
214 struct smbXsrv_preauth
*preauth
;
215 gnutls_hash_hd_t hash_hnd
;
218 preauth
= talloc_move(smb2req
, &auth
->preauth
);
220 rc
= gnutls_hash_init(&hash_hnd
, GNUTLS_DIG_SHA512
);
222 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
224 rc
= gnutls_hash(hash_hnd
,
225 preauth
->sha512_value
,
226 sizeof(preauth
->sha512_value
));
228 gnutls_hash_deinit(hash_hnd
, NULL
);
229 return NT_STATUS_ACCESS_DENIED
;
231 for (i
= 1; i
< smb2req
->in
.vector_count
; i
++) {
232 rc
= gnutls_hash(hash_hnd
,
233 smb2req
->in
.vector
[i
].iov_base
,
234 smb2req
->in
.vector
[i
].iov_len
);
236 gnutls_hash_deinit(hash_hnd
, NULL
);
237 return NT_STATUS_ACCESS_DENIED
;
240 gnutls_hash_deinit(hash_hnd
, preauth
->sha512_value
);
242 preauth_hash
= data_blob_const(preauth
->sha512_value
,
243 sizeof(preauth
->sha512_value
));
246 smb2_signing_derivations_fill_const_stack(&derivations
,
250 if ((in_security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) ||
251 (xconn
->smb2
.server
.security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
))
253 x
->global
->signing_flags
= SMBXSRV_SIGNING_REQUIRED
;
256 if ((lp_server_smb_encrypt(-1) >= SMB_ENCRYPTION_DESIRED
) &&
257 (xconn
->smb2
.client
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
258 x
->global
->encryption_flags
= SMBXSRV_ENCRYPTION_DESIRED
;
261 if (lp_server_smb_encrypt(-1) == SMB_ENCRYPTION_REQUIRED
) {
262 x
->global
->encryption_flags
= SMBXSRV_ENCRYPTION_REQUIRED
|
263 SMBXSRV_ENCRYPTION_DESIRED
;
266 if (security_session_user_level(session_info
, NULL
) < SECURITY_USER
) {
267 if (security_session_user_level(session_info
, NULL
) == SECURITY_GUEST
) {
268 *out_session_flags
|= SMB2_SESSION_FLAG_IS_GUEST
;
270 /* force no signing */
271 x
->global
->signing_flags
&= ~SMBXSRV_SIGNING_REQUIRED
;
272 /* we map anonymous to guest internally */
276 if (guest
&& (x
->global
->encryption_flags
& SMBXSRV_ENCRYPTION_REQUIRED
)) {
277 DEBUG(1,("reject guest session as encryption is required\n"));
278 return NT_STATUS_ACCESS_DENIED
;
281 if (xconn
->smb2
.server
.cipher
== 0) {
282 if (x
->global
->encryption_flags
& SMBXSRV_ENCRYPTION_REQUIRED
) {
283 DEBUG(1,("reject session with dialect[0x%04X] "
284 "as encryption is required\n",
285 xconn
->smb2
.server
.dialect
));
286 return NT_STATUS_ACCESS_DENIED
;
289 x
->global
->signing_algo
= xconn
->smb2
.server
.sign_algo
;
290 x
->global
->encryption_cipher
= xconn
->smb2
.server
.cipher
;
292 x
->global
->encryption_cipher
= SMB2_ENCRYPTION_NONE
;
295 if (x
->global
->encryption_flags
& SMBXSRV_ENCRYPTION_DESIRED
) {
296 *out_session_flags
|= SMB2_SESSION_FLAG_ENCRYPT_DATA
;
299 status
= smb2_signing_key_sign_create(x
->global
,
300 x
->global
->signing_algo
,
301 &session_info
->session_key
,
303 &x
->global
->signing_key
);
304 if (!NT_STATUS_IS_OK(status
)) {
307 x
->global
->signing_key_blob
= x
->global
->signing_key
->blob
;
309 if (x
->global
->encryption_cipher
!= SMB2_ENCRYPTION_NONE
) {
312 status
= smb2_signing_key_cipher_create(x
->global
,
313 x
->global
->encryption_cipher
,
314 &session_info
->session_key
,
315 derivations
.cipher_s2c
,
316 &x
->global
->encryption_key
);
317 if (!NT_STATUS_IS_OK(status
)) {
320 x
->global
->encryption_key_blob
= x
->global
->encryption_key
->blob
;
322 status
= smb2_signing_key_cipher_create(x
->global
,
323 x
->global
->encryption_cipher
,
324 &session_info
->session_key
,
325 derivations
.cipher_c2s
,
326 &x
->global
->decryption_key
);
327 if (!NT_STATUS_IS_OK(status
)) {
330 x
->global
->decryption_key_blob
= x
->global
->decryption_key
->blob
;
333 * CCM and GCM algorithms must never have their
334 * nonce wrap, or the security of the whole
335 * communication and the keys is destroyed.
336 * We must drop the connection once we have
337 * transfered too much data.
339 * NOTE: We assume nonces greater than 8 bytes.
341 generate_nonce_buffer((uint8_t *)&x
->nonce_high_random
,
342 sizeof(x
->nonce_high_random
));
343 switch (xconn
->smb2
.server
.cipher
) {
344 case SMB2_ENCRYPTION_AES128_CCM
:
345 nonce_size
= SMB2_AES_128_CCM_NONCE_SIZE
;
347 case SMB2_ENCRYPTION_AES128_GCM
:
348 nonce_size
= gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_128_GCM
);
350 case SMB2_ENCRYPTION_AES256_CCM
:
351 nonce_size
= SMB2_AES_128_CCM_NONCE_SIZE
;
353 case SMB2_ENCRYPTION_AES256_GCM
:
354 nonce_size
= gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_256_GCM
);
360 x
->nonce_high_max
= SMB2_NONCE_HIGH_MAX(nonce_size
);
365 status
= smb2_signing_key_sign_create(x
->global
,
366 x
->global
->signing_algo
,
367 &session_info
->session_key
,
368 derivations
.application
,
369 &x
->global
->application_key
);
370 if (!NT_STATUS_IS_OK(status
)) {
373 x
->global
->application_key_blob
= x
->global
->application_key
->blob
;
375 if (xconn
->protocol
>= PROTOCOL_SMB3_00
&& lp_debug_encryption()) {
376 DEBUG(0, ("debug encryption: dumping generated session keys\n"));
377 DEBUGADD(0, ("Session Id "));
378 dump_data(0, (uint8_t*)&session
->global
->session_wire_id
,
379 sizeof(session
->global
->session_wire_id
));
380 DEBUGADD(0, ("Session Key "));
381 dump_data(0, session_info
->session_key
.data
,
382 session_info
->session_key
.length
);
383 DEBUGADD(0, ("Signing Algo: %u\n", x
->global
->signing_algo
));
384 DEBUGADD(0, ("Signing Key "));
385 dump_data(0, x
->global
->signing_key_blob
.data
,
386 x
->global
->signing_key_blob
.length
);
387 DEBUGADD(0, ("App Key "));
388 dump_data(0, x
->global
->application_key_blob
.data
,
389 x
->global
->application_key_blob
.length
);
391 /* In server code, ServerIn is the decryption key */
393 DEBUGADD(0, ("Cipher Algo: %u\n", x
->global
->encryption_cipher
));
394 DEBUGADD(0, ("ServerIn Key "));
395 dump_data(0, x
->global
->decryption_key_blob
.data
,
396 x
->global
->decryption_key_blob
.length
);
397 DEBUGADD(0, ("ServerOut Key "));
398 dump_data(0, x
->global
->encryption_key_blob
.data
,
399 x
->global
->encryption_key_blob
.length
);
402 status
= smb2_signing_key_copy(x
->global
->channels
,
403 x
->global
->signing_key
,
404 &x
->global
->channels
[0].signing_key
);
405 if (!NT_STATUS_IS_OK(status
)) {
408 x
->global
->channels
[0].signing_key_blob
=
409 x
->global
->channels
[0].signing_key
->blob
;
410 x
->global
->channels
[0].signing_algo
= x
->global
->signing_algo
;
411 x
->global
->channels
[0].encryption_cipher
= x
->global
->encryption_cipher
;
413 data_blob_clear_free(&session_info
->session_key
);
414 session_info
->session_key
= data_blob_dup_talloc(session_info
,
415 x
->global
->application_key_blob
);
416 if (session_info
->session_key
.data
== NULL
) {
417 return NT_STATUS_NO_MEMORY
;
419 talloc_keep_secret(session_info
->session_key
.data
);
421 smb2req
->sconn
->num_users
++;
423 if (security_session_user_level(session_info
, NULL
) >= SECURITY_USER
) {
424 session
->homes_snum
=
425 register_homes_share(session_info
->unix_info
->unix_name
);
428 set_current_user_info(session_info
->unix_info
->sanitized_username
,
429 session_info
->unix_info
->unix_name
,
430 session_info
->info
->domain_name
);
432 reload_services(smb2req
->sconn
, conn_snum_used
, true);
434 session
->status
= NT_STATUS_OK
;
435 session
->global
->auth_session_info
= talloc_move(session
->global
,
437 session
->global
->auth_session_info_seqnum
+= 1;
438 for (i
=0; i
< session
->global
->num_channels
; i
++) {
439 struct smbXsrv_channel_global0
*_c
=
440 &session
->global
->channels
[i
];
442 _c
->auth_session_info_seqnum
=
443 session
->global
->auth_session_info_seqnum
;
445 session
->global
->auth_time
= timeval_to_nttime(&smb2req
->request_time
);
446 session
->global
->expiration_time
= gensec_expire_time(auth
->gensec
);
448 if (!session_claim(session
)) {
449 DEBUG(1, ("smb2: Failed to claim session "
451 (unsigned long long)session
->global
->session_wire_id
));
452 return NT_STATUS_LOGON_FAILURE
;
456 status
= smbXsrv_session_update(session
);
457 if (!NT_STATUS_IS_OK(status
)) {
458 DEBUG(0, ("smb2: Failed to update session for vuid=%llu - %s\n",
459 (unsigned long long)session
->global
->session_wire_id
,
461 return NT_STATUS_LOGON_FAILURE
;
465 * we attach the session to the request
466 * so that the response can be signed
469 smb2req
->do_signing
= true;
472 global_client_caps
|= (CAP_LEVEL_II_OPLOCKS
|CAP_STATUS32
);
474 *out_session_id
= session
->global
->session_wire_id
;
475 smb2req
->last_session_id
= session
->global
->session_wire_id
;
480 static NTSTATUS
smbd_smb2_reauth_generic_return(struct smbXsrv_session
*session
,
481 struct smbXsrv_session_auth0
**_auth
,
482 struct smbd_smb2_request
*smb2req
,
483 struct auth_session_info
*session_info
,
484 uint16_t *out_session_flags
,
485 uint64_t *out_session_id
)
488 struct smbXsrv_session
*x
= session
;
489 struct smbXsrv_session_auth0
*auth
= *_auth
;
490 struct smbXsrv_connection
*xconn
= smb2req
->xconn
;
495 data_blob_clear_free(&session_info
->session_key
);
496 session_info
->session_key
= data_blob_dup_talloc(session_info
,
497 x
->global
->application_key_blob
);
498 if (session_info
->session_key
.data
== NULL
) {
499 return NT_STATUS_NO_MEMORY
;
501 talloc_keep_secret(session_info
->session_key
.data
);
503 session
->homes_snum
=
504 register_homes_share(session_info
->unix_info
->unix_name
);
506 set_current_user_info(session_info
->unix_info
->sanitized_username
,
507 session_info
->unix_info
->unix_name
,
508 session_info
->info
->domain_name
);
510 reload_services(smb2req
->sconn
, conn_snum_used
, true);
512 if (security_session_user_level(session_info
, NULL
) >= SECURITY_USER
) {
513 smb2req
->do_signing
= true;
516 session
->status
= NT_STATUS_OK
;
517 TALLOC_FREE(session
->global
->auth_session_info
);
518 session
->global
->auth_session_info
= talloc_move(session
->global
,
520 session
->global
->auth_session_info_seqnum
+= 1;
521 for (i
=0; i
< session
->global
->num_channels
; i
++) {
522 struct smbXsrv_channel_global0
*_c
=
523 &session
->global
->channels
[i
];
525 _c
->auth_session_info_seqnum
=
526 session
->global
->auth_session_info_seqnum
;
528 session
->global
->auth_time
= timeval_to_nttime(&smb2req
->request_time
);
529 session
->global
->expiration_time
= gensec_expire_time(auth
->gensec
);
532 status
= smbXsrv_session_update(session
);
533 if (!NT_STATUS_IS_OK(status
)) {
534 DEBUG(0, ("smb2: Failed to update session for vuid=%llu - %s\n",
535 (unsigned long long)session
->global
->session_wire_id
,
537 return NT_STATUS_LOGON_FAILURE
;
540 conn_clear_vuid_caches(xconn
->client
->sconn
,
541 session
->global
->session_wire_id
);
543 *out_session_id
= session
->global
->session_wire_id
;
548 static NTSTATUS
smbd_smb2_bind_auth_return(struct smbXsrv_session
*session
,
549 struct smbXsrv_session_auth0
**_auth
,
550 struct smbd_smb2_request
*smb2req
,
551 struct auth_session_info
*session_info
,
552 uint16_t *out_session_flags
,
553 uint64_t *out_session_id
)
556 struct smbXsrv_session
*x
= session
;
557 struct smbXsrv_session_auth0
*auth
= *_auth
;
558 struct smbXsrv_connection
*xconn
= smb2req
->xconn
;
559 struct smbXsrv_channel_global0
*c
= NULL
;
561 struct smb2_signing_derivations derivations
= {
564 DATA_BLOB preauth_hash
= data_blob_null
;
569 if (xconn
->protocol
>= PROTOCOL_SMB3_11
) {
570 struct smbXsrv_preauth
*preauth
;
571 gnutls_hash_hd_t hash_hnd
= NULL
;
574 preauth
= talloc_move(smb2req
, &auth
->preauth
);
576 rc
= gnutls_hash_init(&hash_hnd
, GNUTLS_DIG_SHA512
);
578 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
581 rc
= gnutls_hash(hash_hnd
,
582 preauth
->sha512_value
,
583 sizeof(preauth
->sha512_value
));
585 gnutls_hash_deinit(hash_hnd
, NULL
);
586 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
588 for (i
= 1; i
< smb2req
->in
.vector_count
; i
++) {
589 rc
= gnutls_hash(hash_hnd
,
590 smb2req
->in
.vector
[i
].iov_base
,
591 smb2req
->in
.vector
[i
].iov_len
);
593 gnutls_hash_deinit(hash_hnd
, NULL
);
594 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
597 gnutls_hash_deinit(hash_hnd
, preauth
->sha512_value
);
599 preauth_hash
= data_blob_const(preauth
->sha512_value
,
600 sizeof(preauth
->sha512_value
));
603 smb2_signing_derivations_fill_const_stack(&derivations
,
607 status
= smbXsrv_session_find_channel(session
, xconn
, &c
);
608 if (!NT_STATUS_IS_OK(status
)) {
612 ok
= security_token_is_sid(session_info
->security_token
,
613 &x
->global
->auth_session_info
->security_token
->sids
[0]);
615 return NT_STATUS_ACCESS_DENIED
;
618 if (session_info
->session_key
.length
== 0) {
619 /* See [MS-SMB2] 3.3.5.2.4 for the return code. */
620 return NT_STATUS_NOT_SUPPORTED
;
623 c
->signing_algo
= xconn
->smb2
.server
.sign_algo
;
624 c
->encryption_cipher
= xconn
->smb2
.server
.cipher
;
626 status
= smb2_signing_key_sign_create(x
->global
->channels
,
628 &session_info
->session_key
,
631 if (!NT_STATUS_IS_OK(status
)) {
634 c
->signing_key_blob
= c
->signing_key
->blob
;
637 status
= smbXsrv_session_update(session
);
638 if (!NT_STATUS_IS_OK(status
)) {
639 DEBUG(0, ("smb2: Failed to update session for vuid=%llu - %s\n",
640 (unsigned long long)session
->global
->session_wire_id
,
642 return NT_STATUS_LOGON_FAILURE
;
645 *out_session_id
= session
->global
->session_wire_id
;
650 struct smbd_smb2_session_setup_state
{
651 struct tevent_context
*ev
;
652 struct smbd_smb2_request
*smb2req
;
653 uint64_t in_session_id
;
655 uint8_t in_security_mode
;
656 uint64_t in_previous_session_id
;
657 DATA_BLOB in_security_buffer
;
658 struct smbXsrv_session
*session
;
659 struct smbXsrv_session_auth0
*auth
;
660 struct auth_session_info
*session_info
;
661 uint16_t out_session_flags
;
662 DATA_BLOB out_security_buffer
;
663 uint64_t out_session_id
;
666 static void smbd_smb2_session_setup_gensec_done(struct tevent_req
*subreq
);
667 static void smbd_smb2_session_setup_previous_done(struct tevent_req
*subreq
);
668 static void smbd_smb2_session_setup_auth_return(struct tevent_req
*req
);
670 static struct tevent_req
*smbd_smb2_session_setup_send(TALLOC_CTX
*mem_ctx
,
671 struct tevent_context
*ev
,
672 struct smbd_smb2_request
*smb2req
,
673 uint64_t in_session_id
,
675 uint8_t in_security_mode
,
676 uint64_t in_previous_session_id
,
677 DATA_BLOB in_security_buffer
)
679 struct tevent_req
*req
;
680 struct smbd_smb2_session_setup_state
*state
;
682 NTTIME now
= timeval_to_nttime(&smb2req
->request_time
);
683 struct tevent_req
*subreq
;
684 struct smbXsrv_channel_global0
*c
= NULL
;
685 enum security_user_level seclvl
;
687 req
= tevent_req_create(mem_ctx
, &state
,
688 struct smbd_smb2_session_setup_state
);
693 state
->smb2req
= smb2req
;
694 state
->in_session_id
= in_session_id
;
695 state
->in_flags
= in_flags
;
696 state
->in_security_mode
= in_security_mode
;
697 state
->in_previous_session_id
= in_previous_session_id
;
698 state
->in_security_buffer
= in_security_buffer
;
700 if (in_flags
& SMB2_SESSION_FLAG_BINDING
) {
701 if (in_session_id
== 0) {
702 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
703 return tevent_req_post(req
, ev
);
706 if (smb2req
->session
== NULL
) {
707 tevent_req_nterror(req
, NT_STATUS_USER_SESSION_DELETED
);
708 return tevent_req_post(req
, ev
);
711 if ((smb2req
->session
->global
->signing_algo
>= SMB2_SIGNING_AES128_GMAC
) &&
712 (smb2req
->xconn
->smb2
.server
.sign_algo
!= smb2req
->session
->global
->signing_algo
))
714 tevent_req_nterror(req
, NT_STATUS_REQUEST_OUT_OF_SEQUENCE
);
715 return tevent_req_post(req
, ev
);
717 if ((smb2req
->xconn
->smb2
.server
.sign_algo
>= SMB2_SIGNING_AES128_GMAC
) &&
718 (smb2req
->session
->global
->signing_algo
!= smb2req
->xconn
->smb2
.server
.sign_algo
))
720 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
721 return tevent_req_post(req
, ev
);
724 if (smb2req
->xconn
->protocol
< PROTOCOL_SMB3_00
) {
725 tevent_req_nterror(req
, NT_STATUS_REQUEST_NOT_ACCEPTED
);
726 return tevent_req_post(req
, ev
);
729 if (!smb2req
->xconn
->client
->server_multi_channel_enabled
) {
730 tevent_req_nterror(req
, NT_STATUS_REQUEST_NOT_ACCEPTED
);
731 return tevent_req_post(req
, ev
);
734 if (!smb2req
->do_signing
) {
735 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
736 return tevent_req_post(req
, ev
);
739 if (smb2req
->session
->global
->connection_dialect
740 != smb2req
->xconn
->smb2
.server
.dialect
)
742 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
743 return tevent_req_post(req
, ev
);
746 if (smb2req
->session
->global
->encryption_cipher
747 != smb2req
->xconn
->smb2
.server
.cipher
)
749 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
750 return tevent_req_post(req
, ev
);
753 status
= smb2req
->session
->status
;
754 if (NT_STATUS_EQUAL(status
, NT_STATUS_BAD_LOGON_SESSION_STATE
)) {
756 * This comes from smb2srv_session_lookup_global().
758 tevent_req_nterror(req
, NT_STATUS_USER_SESSION_DELETED
);
759 return tevent_req_post(req
, ev
);
762 status
= smbXsrv_session_find_channel(smb2req
->session
,
765 if (NT_STATUS_IS_OK(status
)) {
766 if (!smb2_signing_key_valid(c
->signing_key
)) {
769 tevent_req_nterror(req
, NT_STATUS_REQUEST_NOT_ACCEPTED
);
770 return tevent_req_post(req
, ev
);
773 seclvl
= security_session_user_level(
774 smb2req
->session
->global
->auth_session_info
,
776 if (seclvl
< SECURITY_USER
) {
777 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
778 return tevent_req_post(req
, ev
);
781 status
= smbXsrv_session_add_channel(smb2req
->session
,
785 if (tevent_req_nterror(req
, status
)) {
786 return tevent_req_post(req
, ev
);
789 status
= smbXsrv_session_update(smb2req
->session
);
790 if (tevent_req_nterror(req
, status
)) {
791 return tevent_req_post(req
, ev
);
797 if (state
->in_session_id
== 0) {
798 /* create a new session */
799 status
= smbXsrv_session_create(state
->smb2req
->xconn
,
800 now
, &state
->session
);
801 if (tevent_req_nterror(req
, status
)) {
802 return tevent_req_post(req
, ev
);
804 smb2req
->session
= state
->session
;
806 if (smb2req
->session
== NULL
) {
807 tevent_req_nterror(req
, NT_STATUS_USER_SESSION_DELETED
);
808 return tevent_req_post(req
, ev
);
811 state
->session
= smb2req
->session
;
812 status
= state
->session
->status
;
813 if (NT_STATUS_EQUAL(status
, NT_STATUS_BAD_LOGON_SESSION_STATE
)) {
815 * This comes from smb2srv_session_lookup_global().
817 tevent_req_nterror(req
, NT_STATUS_USER_SESSION_DELETED
);
818 return tevent_req_post(req
, ev
);
820 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
)) {
821 status
= NT_STATUS_OK
;
823 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
824 status
= NT_STATUS_OK
;
826 if (tevent_req_nterror(req
, status
)) {
827 return tevent_req_post(req
, ev
);
831 status
= smbXsrv_session_find_channel(smb2req
->session
,
833 if (tevent_req_nterror(req
, status
)) {
834 return tevent_req_post(req
, ev
);
837 if (!(in_flags
& SMB2_SESSION_FLAG_BINDING
)) {
838 state
->session
->status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
841 status
= smbXsrv_session_find_auth(state
->session
, smb2req
->xconn
,
843 if (!NT_STATUS_IS_OK(status
)) {
844 status
= smbXsrv_session_create_auth(state
->session
,
846 in_flags
, in_security_mode
,
848 if (tevent_req_nterror(req
, status
)) {
849 return tevent_req_post(req
, ev
);
853 if (state
->auth
->gensec
== NULL
) {
854 status
= auth_generic_prepare(state
->auth
,
855 state
->smb2req
->xconn
->remote_address
,
856 state
->smb2req
->xconn
->local_address
,
858 &state
->auth
->gensec
);
859 if (tevent_req_nterror(req
, status
)) {
860 return tevent_req_post(req
, ev
);
863 gensec_want_feature(state
->auth
->gensec
, GENSEC_FEATURE_SESSION_KEY
);
864 gensec_want_feature(state
->auth
->gensec
, GENSEC_FEATURE_UNIX_TOKEN
);
865 gensec_want_feature(state
->auth
->gensec
, GENSEC_FEATURE_SMB_TRANSPORT
);
867 status
= gensec_start_mech_by_oid(state
->auth
->gensec
,
869 if (tevent_req_nterror(req
, status
)) {
870 return tevent_req_post(req
, ev
);
874 status
= smbXsrv_session_update(state
->session
);
875 if (tevent_req_nterror(req
, status
)) {
876 return tevent_req_post(req
, ev
);
880 subreq
= gensec_update_send(state
, state
->ev
,
882 state
->in_security_buffer
);
884 if (tevent_req_nomem(subreq
, req
)) {
885 return tevent_req_post(req
, ev
);
887 tevent_req_set_callback(subreq
, smbd_smb2_session_setup_gensec_done
, req
);
892 static void smbd_smb2_session_setup_gensec_done(struct tevent_req
*subreq
)
894 struct tevent_req
*req
=
895 tevent_req_callback_data(subreq
,
897 struct smbd_smb2_session_setup_state
*state
=
899 struct smbd_smb2_session_setup_state
);
903 status
= gensec_update_recv(subreq
, state
,
904 &state
->out_security_buffer
);
907 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) &&
908 !NT_STATUS_IS_OK(status
)) {
909 tevent_req_nterror(req
, status
);
913 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
914 state
->out_session_id
= state
->session
->global
->session_wire_id
;
915 state
->smb2req
->preauth
= state
->auth
->preauth
;
916 tevent_req_nterror(req
, status
);
920 status
= gensec_session_info(state
->auth
->gensec
,
922 &state
->session_info
);
923 if (tevent_req_nterror(req
, status
)) {
927 if ((state
->in_previous_session_id
!= 0) &&
928 (state
->session
->global
->session_wire_id
!=
929 state
->in_previous_session_id
))
931 subreq
= smb2srv_session_close_previous_send(state
, state
->ev
,
932 state
->smb2req
->xconn
,
934 state
->in_previous_session_id
,
935 state
->session
->global
->session_wire_id
);
936 if (tevent_req_nomem(subreq
, req
)) {
939 tevent_req_set_callback(subreq
,
940 smbd_smb2_session_setup_previous_done
,
945 smbd_smb2_session_setup_auth_return(req
);
948 static void smbd_smb2_session_setup_previous_done(struct tevent_req
*subreq
)
950 struct tevent_req
*req
=
951 tevent_req_callback_data(subreq
,
955 status
= smb2srv_session_close_previous_recv(subreq
);
957 if (tevent_req_nterror(req
, status
)) {
961 smbd_smb2_session_setup_auth_return(req
);
964 static void smbd_smb2_session_setup_auth_return(struct tevent_req
*req
)
966 struct smbd_smb2_session_setup_state
*state
=
968 struct smbd_smb2_session_setup_state
);
971 if (state
->in_flags
& SMB2_SESSION_FLAG_BINDING
) {
972 status
= smbd_smb2_bind_auth_return(state
->session
,
976 &state
->out_session_flags
,
977 &state
->out_session_id
);
978 if (tevent_req_nterror(req
, status
)) {
981 tevent_req_done(req
);
985 if (state
->session
->global
->auth_session_info
!= NULL
) {
986 status
= smbd_smb2_reauth_generic_return(state
->session
,
990 &state
->out_session_flags
,
991 &state
->out_session_id
);
992 if (tevent_req_nterror(req
, status
)) {
995 tevent_req_done(req
);
999 status
= smbd_smb2_auth_generic_return(state
->session
,
1002 state
->in_security_mode
,
1003 state
->session_info
,
1004 &state
->out_session_flags
,
1005 &state
->out_session_id
);
1006 if (tevent_req_nterror(req
, status
)) {
1010 tevent_req_done(req
);
1014 static NTSTATUS
smbd_smb2_session_setup_recv(struct tevent_req
*req
,
1015 uint16_t *out_session_flags
,
1016 TALLOC_CTX
*mem_ctx
,
1017 DATA_BLOB
*out_security_buffer
,
1018 uint64_t *out_session_id
)
1020 struct smbd_smb2_session_setup_state
*state
=
1021 tevent_req_data(req
,
1022 struct smbd_smb2_session_setup_state
);
1025 if (tevent_req_is_nterror(req
, &status
)) {
1026 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1027 tevent_req_received(req
);
1028 return nt_status_squash(status
);
1031 status
= NT_STATUS_OK
;
1034 *out_session_flags
= state
->out_session_flags
;
1035 *out_security_buffer
= state
->out_security_buffer
;
1036 *out_session_id
= state
->out_session_id
;
1038 talloc_steal(mem_ctx
, out_security_buffer
->data
);
1039 tevent_req_received(req
);
1043 struct smbd_smb2_session_setup_wrap_state
{
1044 struct tevent_context
*ev
;
1045 struct smbd_smb2_request
*smb2req
;
1046 uint64_t in_session_id
;
1048 uint8_t in_security_mode
;
1049 uint64_t in_previous_session_id
;
1050 DATA_BLOB in_security_buffer
;
1051 uint16_t out_session_flags
;
1052 DATA_BLOB out_security_buffer
;
1053 uint64_t out_session_id
;
1057 static void smbd_smb2_session_setup_wrap_setup_done(struct tevent_req
*subreq
);
1058 static void smbd_smb2_session_setup_wrap_shutdown_done(struct tevent_req
*subreq
);
1060 static struct tevent_req
*smbd_smb2_session_setup_wrap_send(TALLOC_CTX
*mem_ctx
,
1061 struct tevent_context
*ev
,
1062 struct smbd_smb2_request
*smb2req
,
1063 uint64_t in_session_id
,
1065 uint8_t in_security_mode
,
1066 uint64_t in_previous_session_id
,
1067 DATA_BLOB in_security_buffer
)
1069 struct tevent_req
*req
;
1070 struct smbd_smb2_session_setup_wrap_state
*state
;
1071 struct tevent_req
*subreq
;
1073 req
= tevent_req_create(mem_ctx
, &state
,
1074 struct smbd_smb2_session_setup_wrap_state
);
1079 state
->smb2req
= smb2req
;
1080 state
->in_session_id
= in_session_id
;
1081 state
->in_flags
= in_flags
;
1082 state
->in_security_mode
= in_security_mode
;
1083 state
->in_previous_session_id
= in_previous_session_id
;
1084 state
->in_security_buffer
= in_security_buffer
;
1086 subreq
= smbd_smb2_session_setup_send(state
, state
->ev
,
1088 state
->in_session_id
,
1090 state
->in_security_mode
,
1091 state
->in_previous_session_id
,
1092 state
->in_security_buffer
);
1093 if (tevent_req_nomem(subreq
, req
)) {
1094 return tevent_req_post(req
, ev
);
1096 tevent_req_set_callback(subreq
,
1097 smbd_smb2_session_setup_wrap_setup_done
, req
);
1102 static void smbd_smb2_session_setup_wrap_setup_done(struct tevent_req
*subreq
)
1104 struct tevent_req
*req
=
1105 tevent_req_callback_data(subreq
,
1107 struct smbd_smb2_session_setup_wrap_state
*state
=
1108 tevent_req_data(req
,
1109 struct smbd_smb2_session_setup_wrap_state
);
1112 status
= smbd_smb2_session_setup_recv(subreq
,
1113 &state
->out_session_flags
,
1115 &state
->out_security_buffer
,
1116 &state
->out_session_id
);
1117 TALLOC_FREE(subreq
);
1118 if (NT_STATUS_IS_OK(status
)) {
1119 tevent_req_done(req
);
1122 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1123 tevent_req_nterror(req
, status
);
1127 if (state
->smb2req
->session
== NULL
) {
1128 tevent_req_nterror(req
, status
);
1132 state
->error
= status
;
1134 if (state
->in_flags
& SMB2_SESSION_FLAG_BINDING
) {
1135 status
= smbXsrv_session_remove_channel(state
->smb2req
->session
,
1136 state
->smb2req
->xconn
);
1137 if (tevent_req_nterror(req
, status
)) {
1140 tevent_req_nterror(req
, state
->error
);
1144 if (NT_STATUS_EQUAL(state
->error
, NT_STATUS_USER_SESSION_DELETED
)) {
1145 tevent_req_nterror(req
, state
->error
);
1149 subreq
= smb2srv_session_shutdown_send(state
, state
->ev
,
1150 state
->smb2req
->session
,
1152 if (tevent_req_nomem(subreq
, req
)) {
1155 tevent_req_set_callback(subreq
,
1156 smbd_smb2_session_setup_wrap_shutdown_done
,
1160 static void smbd_smb2_session_setup_wrap_shutdown_done(struct tevent_req
*subreq
)
1162 struct tevent_req
*req
=
1163 tevent_req_callback_data(subreq
,
1165 struct smbd_smb2_session_setup_wrap_state
*state
=
1166 tevent_req_data(req
,
1167 struct smbd_smb2_session_setup_wrap_state
);
1170 status
= smb2srv_session_shutdown_recv(subreq
);
1171 TALLOC_FREE(subreq
);
1172 if (tevent_req_nterror(req
, status
)) {
1177 * we may need to sign the response, so we need to keep
1178 * the session until the response is sent to the wire.
1180 talloc_steal(state
->smb2req
, state
->smb2req
->session
);
1182 tevent_req_nterror(req
, state
->error
);
1185 static NTSTATUS
smbd_smb2_session_setup_wrap_recv(struct tevent_req
*req
,
1186 uint16_t *out_session_flags
,
1187 TALLOC_CTX
*mem_ctx
,
1188 DATA_BLOB
*out_security_buffer
,
1189 uint64_t *out_session_id
)
1191 struct smbd_smb2_session_setup_wrap_state
*state
=
1192 tevent_req_data(req
,
1193 struct smbd_smb2_session_setup_wrap_state
);
1196 if (tevent_req_is_nterror(req
, &status
)) {
1197 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1198 tevent_req_received(req
);
1199 return nt_status_squash(status
);
1202 status
= NT_STATUS_OK
;
1205 *out_session_flags
= state
->out_session_flags
;
1206 *out_security_buffer
= state
->out_security_buffer
;
1207 *out_session_id
= state
->out_session_id
;
1209 talloc_steal(mem_ctx
, out_security_buffer
->data
);
1210 tevent_req_received(req
);
1214 static struct tevent_req
*smbd_smb2_logoff_send(TALLOC_CTX
*mem_ctx
,
1215 struct tevent_context
*ev
,
1216 struct smbd_smb2_request
*smb2req
);
1217 static NTSTATUS
smbd_smb2_logoff_recv(struct tevent_req
*req
);
1218 static void smbd_smb2_request_logoff_done(struct tevent_req
*subreq
);
1220 NTSTATUS
smbd_smb2_request_process_logoff(struct smbd_smb2_request
*req
)
1223 struct tevent_req
*subreq
= NULL
;
1225 status
= smbd_smb2_request_verify_sizes(req
, 0x04);
1226 if (!NT_STATUS_IS_OK(status
)) {
1227 return smbd_smb2_request_error(req
, status
);
1230 subreq
= smbd_smb2_logoff_send(req
, req
->sconn
->ev_ctx
, req
);
1231 if (subreq
== NULL
) {
1232 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
1234 tevent_req_set_callback(subreq
, smbd_smb2_request_logoff_done
, req
);
1237 * Avoid sending a STATUS_PENDING message, it's very likely
1238 * the client won't expect that.
1240 return smbd_smb2_request_pending_queue(req
, subreq
, 0);
1243 static void smbd_smb2_request_logoff_done(struct tevent_req
*subreq
)
1245 struct smbd_smb2_request
*smb2req
=
1246 tevent_req_callback_data(subreq
,
1247 struct smbd_smb2_request
);
1252 status
= smbd_smb2_logoff_recv(subreq
);
1253 TALLOC_FREE(subreq
);
1254 if (!NT_STATUS_IS_OK(status
)) {
1255 error
= smbd_smb2_request_error(smb2req
, status
);
1256 if (!NT_STATUS_IS_OK(error
)) {
1257 smbd_server_connection_terminate(smb2req
->xconn
,
1264 outbody
= smbd_smb2_generate_outbody(smb2req
, 0x04);
1265 if (outbody
.data
== NULL
) {
1266 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
1267 if (!NT_STATUS_IS_OK(error
)) {
1268 smbd_server_connection_terminate(smb2req
->xconn
,
1275 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
1276 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
1278 error
= smbd_smb2_request_done(smb2req
, outbody
, NULL
);
1279 if (!NT_STATUS_IS_OK(error
)) {
1280 smbd_server_connection_terminate(smb2req
->xconn
,
1286 struct smbd_smb2_logoff_state
{
1287 struct smbd_smb2_request
*smb2req
;
1290 static void smbd_smb2_logoff_shutdown_done(struct tevent_req
*subreq
);
1292 static struct tevent_req
*smbd_smb2_logoff_send(TALLOC_CTX
*mem_ctx
,
1293 struct tevent_context
*ev
,
1294 struct smbd_smb2_request
*smb2req
)
1296 struct tevent_req
*req
;
1297 struct smbd_smb2_logoff_state
*state
;
1298 struct tevent_req
*subreq
;
1300 req
= tevent_req_create(mem_ctx
, &state
,
1301 struct smbd_smb2_logoff_state
);
1305 state
->smb2req
= smb2req
;
1307 subreq
= smb2srv_session_shutdown_send(state
, ev
,
1310 if (tevent_req_nomem(subreq
, req
)) {
1311 return tevent_req_post(req
, ev
);
1313 tevent_req_set_callback(subreq
, smbd_smb2_logoff_shutdown_done
, req
);
1318 static void smbd_smb2_logoff_shutdown_done(struct tevent_req
*subreq
)
1320 struct tevent_req
*req
= tevent_req_callback_data(
1321 subreq
, struct tevent_req
);
1322 struct smbd_smb2_logoff_state
*state
= tevent_req_data(
1323 req
, struct smbd_smb2_logoff_state
);
1326 const struct GUID
*client_guid
=
1327 &state
->smb2req
->session
->client
->global
->client_guid
;
1329 status
= smb2srv_session_shutdown_recv(subreq
);
1330 if (tevent_req_nterror(req
, status
)) {
1333 TALLOC_FREE(subreq
);
1335 if (!GUID_all_zero(client_guid
)) {
1336 ok
= remote_arch_cache_delete(client_guid
);
1338 /* Most likely not an error, but not in cache */
1339 DBG_DEBUG("Deletion from remote arch cache failed\n");
1344 * As we've been awoken, we may have changed
1345 * uid in the meantime. Ensure we're still
1346 * root (SMB2_OP_LOGOFF has .as_root = true).
1348 change_to_root_user();
1350 status
= smbXsrv_session_logoff(state
->smb2req
->session
);
1351 if (tevent_req_nterror(req
, status
)) {
1356 * we may need to sign the response, so we need to keep
1357 * the session until the response is sent to the wire.
1359 talloc_steal(state
->smb2req
, state
->smb2req
->session
);
1361 tevent_req_done(req
);
1364 static NTSTATUS
smbd_smb2_logoff_recv(struct tevent_req
*req
)
1366 return tevent_req_simple_recv_ntstatus(req
);