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 "../libcli/auth/spnego.h"
27 #include "../libcli/auth/ntlmssp.h"
28 #include "ntlmssp_wrap.h"
29 #include "../librpc/gen_ndr/krb5pac.h"
30 #include "libads/kerberos_proto.h"
31 #include "../lib/util/asn1.h"
34 static NTSTATUS
smbd_smb2_session_setup(struct smbd_smb2_request
*smb2req
,
35 uint64_t in_session_id
,
36 uint8_t in_security_mode
,
37 DATA_BLOB in_security_buffer
,
38 uint16_t *out_session_flags
,
39 DATA_BLOB
*out_security_buffer
,
40 uint64_t *out_session_id
);
42 NTSTATUS
smbd_smb2_request_process_sesssetup(struct smbd_smb2_request
*smb2req
)
45 const uint8_t *inbody
;
46 int i
= smb2req
->current_idx
;
50 uint64_t in_session_id
;
51 uint8_t in_security_mode
;
52 uint16_t in_security_offset
;
53 uint16_t in_security_length
;
54 DATA_BLOB in_security_buffer
;
55 uint16_t out_session_flags
;
56 uint64_t out_session_id
;
57 uint16_t out_security_offset
;
58 DATA_BLOB out_security_buffer
= data_blob_null
;
61 status
= smbd_smb2_request_verify_sizes(smb2req
, 0x19);
62 if (!NT_STATUS_IS_OK(status
)) {
63 return smbd_smb2_request_error(smb2req
, status
);
65 inhdr
= (const uint8_t *)smb2req
->in
.vector
[i
+0].iov_base
;
66 inbody
= (const uint8_t *)smb2req
->in
.vector
[i
+1].iov_base
;
68 in_security_offset
= SVAL(inbody
, 0x0C);
69 in_security_length
= SVAL(inbody
, 0x0E);
71 if (in_security_offset
!= (SMB2_HDR_BODY
+ smb2req
->in
.vector
[i
+1].iov_len
)) {
72 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
75 if (in_security_length
> smb2req
->in
.vector
[i
+2].iov_len
) {
76 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
79 in_session_id
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
80 in_security_mode
= CVAL(inbody
, 0x03);
81 in_security_buffer
.data
= (uint8_t *)smb2req
->in
.vector
[i
+2].iov_base
;
82 in_security_buffer
.length
= in_security_length
;
84 status
= smbd_smb2_session_setup(smb2req
,
91 if (!NT_STATUS_IS_OK(status
) &&
92 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
93 status
= nt_status_squash(status
);
94 return smbd_smb2_request_error(smb2req
, status
);
97 out_security_offset
= SMB2_HDR_BODY
+ 0x08;
99 outhdr
= (uint8_t *)smb2req
->out
.vector
[i
].iov_base
;
101 outbody
= data_blob_talloc(smb2req
->out
.vector
, NULL
, 0x08);
102 if (outbody
.data
== NULL
) {
103 return smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
106 SBVAL(outhdr
, SMB2_HDR_SESSION_ID
, out_session_id
);
108 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
109 SSVAL(outbody
.data
, 0x02,
110 out_session_flags
); /* session flags */
111 SSVAL(outbody
.data
, 0x04,
112 out_security_offset
); /* security buffer offset */
113 SSVAL(outbody
.data
, 0x06,
114 out_security_buffer
.length
); /* security buffer length */
116 outdyn
= out_security_buffer
;
118 return smbd_smb2_request_done_ex(smb2req
, status
, outbody
, &outdyn
,
122 static int smbd_smb2_session_destructor(struct smbd_smb2_session
*session
)
124 if (session
->sconn
== NULL
) {
128 /* first free all tcons */
129 while (session
->tcons
.list
) {
130 talloc_free(session
->tcons
.list
);
133 idr_remove(session
->sconn
->smb2
.sessions
.idtree
, session
->vuid
);
134 DLIST_REMOVE(session
->sconn
->smb2
.sessions
.list
, session
);
135 invalidate_vuid(session
->sconn
, session
->vuid
);
138 session
->status
= NT_STATUS_USER_SESSION_DELETED
;
139 session
->sconn
= NULL
;
144 static NTSTATUS
setup_ntlmssp_session_info(struct smbd_smb2_session
*session
,
147 if (NT_STATUS_IS_OK(status
)) {
148 status
= auth_ntlmssp_steal_session_info(session
,
149 session
->auth_ntlmssp_state
,
150 &session
->session_info
);
152 /* Note that this session_info won't have a session
153 * key. But for map to guest, that's exactly the right
154 * thing - we can't reasonably guess the key the
155 * client wants, as the password was wrong */
156 status
= do_map_to_guest(status
,
157 &session
->session_info
,
158 auth_ntlmssp_get_username(session
->auth_ntlmssp_state
),
159 auth_ntlmssp_get_domain(session
->auth_ntlmssp_state
));
165 static NTSTATUS
smbd_smb2_session_setup_krb5(struct smbd_smb2_session
*session
,
166 struct smbd_smb2_request
*smb2req
,
167 uint8_t in_security_mode
,
168 const DATA_BLOB
*secblob
,
170 uint16_t *out_session_flags
,
171 DATA_BLOB
*out_security_buffer
,
172 uint64_t *out_session_id
)
174 DATA_BLOB ap_rep
= data_blob_null
;
175 DATA_BLOB ap_rep_wrapped
= data_blob_null
;
176 DATA_BLOB ticket
= data_blob_null
;
177 DATA_BLOB session_key
= data_blob_null
;
178 DATA_BLOB secblob_out
= data_blob_null
;
180 struct PAC_LOGON_INFO
*logon_info
= NULL
;
181 char *principal
= NULL
;
184 struct passwd
*pw
= NULL
;
188 bool username_was_mapped
= false;
189 bool map_domainuser_to_guest
= false;
191 if (!spnego_parse_krb5_wrap(talloc_tos(), *secblob
, &ticket
, tok_id
)) {
192 status
= NT_STATUS_LOGON_FAILURE
;
196 status
= ads_verify_ticket(smb2req
, lp_realm(), 0, &ticket
,
197 &principal
, &logon_info
, &ap_rep
,
200 if (!NT_STATUS_IS_OK(status
)) {
201 DEBUG(1,("smb2: Failed to verify incoming ticket with error %s!\n",
203 if (!NT_STATUS_EQUAL(status
, NT_STATUS_TIME_DIFFERENCE_AT_DC
)) {
204 status
= NT_STATUS_LOGON_FAILURE
;
209 status
= get_user_from_kerberos_info(talloc_tos(),
210 smb2req
->sconn
->client_id
.name
,
211 principal
, logon_info
,
212 &username_was_mapped
,
213 &map_domainuser_to_guest
,
215 &real_username
, &pw
);
216 if (!NT_STATUS_IS_OK(status
)) {
220 /* save the PAC data if we have it */
222 netsamlogon_cache_store(user
, &logon_info
->info3
);
225 /* setup the string used by %U */
226 sub_set_smb_name(real_username
);
228 /* reload services so that the new %U is taken into account */
229 reload_services(smb2req
->sconn
->msg_ctx
, smb2req
->sconn
->sock
, true);
231 status
= make_server_info_krb5(session
,
232 user
, domain
, real_username
, pw
,
233 logon_info
, map_domainuser_to_guest
,
234 &session
->session_info
);
235 if (!NT_STATUS_IS_OK(status
)) {
236 DEBUG(1, ("smb2: make_server_info_krb5 failed\n"));
241 session
->session_info
->nss_token
|= username_was_mapped
;
243 /* we need to build the token for the user. make_session_info_guest()
246 if (!session
->session_info
->security_token
) {
247 status
= create_local_token(session
->session_info
);
248 if (!NT_STATUS_IS_OK(status
)) {
249 DEBUG(10,("smb2: failed to create local token: %s\n",
255 if ((in_security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) ||
256 lp_server_signing() == Required
) {
257 session
->do_signing
= true;
260 if (session
->session_info
->guest
) {
261 /* we map anonymous to guest internally */
262 *out_session_flags
|= SMB2_SESSION_FLAG_IS_GUEST
;
263 *out_session_flags
|= SMB2_SESSION_FLAG_IS_NULL
;
264 /* force no signing */
265 session
->do_signing
= false;
268 data_blob_free(&session
->session_info
->user_session_key
);
269 session
->session_info
->user_session_key
=
271 session
->session_info
,
274 if (session_key
.length
> 0) {
275 if (session
->session_info
->user_session_key
.data
== NULL
) {
276 status
= NT_STATUS_NO_MEMORY
;
280 session
->session_key
= session
->session_info
->user_session_key
;
282 session
->compat_vuser
= talloc_zero(session
, user_struct
);
283 if (session
->compat_vuser
== NULL
) {
284 status
= NT_STATUS_NO_MEMORY
;
287 session
->compat_vuser
->auth_ntlmssp_state
= NULL
;
288 session
->compat_vuser
->homes_snum
= -1;
289 session
->compat_vuser
->session_info
= session
->session_info
;
290 session
->compat_vuser
->session_keystr
= NULL
;
291 session
->compat_vuser
->vuid
= session
->vuid
;
292 DLIST_ADD(session
->sconn
->smb1
.sessions
.validated_users
, session
->compat_vuser
);
294 /* This is a potentially untrusted username */
295 alpha_strcpy(tmp
, user
, ". _-$", sizeof(tmp
));
296 session
->session_info
->sanitized_username
=
297 talloc_strdup(session
->session_info
, tmp
);
299 if (!session
->session_info
->guest
) {
300 session
->compat_vuser
->homes_snum
=
301 register_homes_share(session
->session_info
->unix_name
);
304 if (!session_claim(session
->sconn
, session
->compat_vuser
)) {
305 DEBUG(1, ("smb2: Failed to claim session "
307 session
->compat_vuser
->vuid
));
311 session
->status
= NT_STATUS_OK
;
314 * we attach the session to the request
315 * so that the response can be signed
317 smb2req
->session
= session
;
318 if (session
->do_signing
) {
319 smb2req
->do_signing
= true;
322 global_client_caps
|= (CAP_LEVEL_II_OPLOCKS
|CAP_STATUS32
);
323 status
= NT_STATUS_OK
;
325 /* wrap that up in a nice GSS-API wrapping */
326 ap_rep_wrapped
= spnego_gen_krb5_wrap(talloc_tos(), ap_rep
,
329 secblob_out
= spnego_gen_auth_response(
335 *out_security_buffer
= data_blob_talloc(smb2req
,
338 if (secblob_out
.data
&& out_security_buffer
->data
== NULL
) {
339 status
= NT_STATUS_NO_MEMORY
;
343 data_blob_free(&ap_rep
);
344 data_blob_free(&ap_rep_wrapped
);
345 data_blob_free(&ticket
);
346 data_blob_free(&session_key
);
347 data_blob_free(&secblob_out
);
349 *out_session_id
= session
->vuid
;
355 data_blob_free(&ap_rep
);
356 data_blob_free(&ap_rep_wrapped
);
357 data_blob_free(&ticket
);
358 data_blob_free(&session_key
);
359 data_blob_free(&secblob_out
);
361 ap_rep_wrapped
= data_blob_null
;
362 secblob_out
= spnego_gen_auth_response(
368 *out_security_buffer
= data_blob_talloc(smb2req
,
371 data_blob_free(&secblob_out
);
376 static NTSTATUS
smbd_smb2_spnego_negotiate(struct smbd_smb2_session
*session
,
377 struct smbd_smb2_request
*smb2req
,
378 uint8_t in_security_mode
,
379 DATA_BLOB in_security_buffer
,
380 uint16_t *out_session_flags
,
381 DATA_BLOB
*out_security_buffer
,
382 uint64_t *out_session_id
)
384 DATA_BLOB secblob_in
= data_blob_null
;
385 DATA_BLOB chal_out
= data_blob_null
;
386 char *kerb_mech
= NULL
;
389 /* Ensure we have no old NTLM state around. */
390 TALLOC_FREE(session
->auth_ntlmssp_state
);
392 status
= parse_spnego_mechanisms(talloc_tos(), in_security_buffer
,
393 &secblob_in
, &kerb_mech
);
394 if (!NT_STATUS_IS_OK(status
)) {
399 if (kerb_mech
&& ((lp_security()==SEC_ADS
) ||
400 USE_KERBEROS_KEYTAB
) ) {
401 status
= smbd_smb2_session_setup_krb5(session
,
415 /* The mechtoken is a krb5 ticket, but
416 * we need to fall back to NTLM. */
418 DEBUG(3,("smb2: Got krb5 ticket in SPNEGO "
419 "but set to downgrade to NTLMSSP\n"));
421 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
423 /* Fall back to NTLMSSP. */
424 status
= auth_ntlmssp_start(&session
->auth_ntlmssp_state
);
425 if (!NT_STATUS_IS_OK(status
)) {
429 status
= auth_ntlmssp_update(session
->auth_ntlmssp_state
,
434 if (!NT_STATUS_IS_OK(status
) &&
435 !NT_STATUS_EQUAL(status
,
436 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
440 *out_security_buffer
= spnego_gen_auth_response(smb2req
,
444 if (out_security_buffer
->data
== NULL
) {
445 status
= NT_STATUS_NO_MEMORY
;
448 *out_session_id
= session
->vuid
;
452 data_blob_free(&secblob_in
);
453 data_blob_free(&chal_out
);
454 TALLOC_FREE(kerb_mech
);
455 if (!NT_STATUS_IS_OK(status
) &&
456 !NT_STATUS_EQUAL(status
,
457 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
458 TALLOC_FREE(session
->auth_ntlmssp_state
);
459 TALLOC_FREE(session
);
464 static NTSTATUS
smbd_smb2_common_ntlmssp_auth_return(struct smbd_smb2_session
*session
,
465 struct smbd_smb2_request
*smb2req
,
466 uint8_t in_security_mode
,
467 DATA_BLOB in_security_buffer
,
468 uint16_t *out_session_flags
,
469 uint64_t *out_session_id
)
473 if ((in_security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) ||
474 lp_server_signing() == Required
) {
475 session
->do_signing
= true;
478 if (session
->session_info
->guest
) {
479 /* we map anonymous to guest internally */
480 *out_session_flags
|= SMB2_SESSION_FLAG_IS_GUEST
;
481 *out_session_flags
|= SMB2_SESSION_FLAG_IS_NULL
;
482 /* force no signing */
483 session
->do_signing
= false;
486 session
->session_key
= session
->session_info
->user_session_key
;
488 session
->compat_vuser
= talloc_zero(session
, user_struct
);
489 if (session
->compat_vuser
== NULL
) {
490 TALLOC_FREE(session
->auth_ntlmssp_state
);
491 TALLOC_FREE(session
);
492 return NT_STATUS_NO_MEMORY
;
494 session
->compat_vuser
->auth_ntlmssp_state
= session
->auth_ntlmssp_state
;
495 session
->compat_vuser
->homes_snum
= -1;
496 session
->compat_vuser
->session_info
= session
->session_info
;
497 session
->compat_vuser
->session_keystr
= NULL
;
498 session
->compat_vuser
->vuid
= session
->vuid
;
499 DLIST_ADD(session
->sconn
->smb1
.sessions
.validated_users
, session
->compat_vuser
);
501 /* This is a potentially untrusted username */
503 auth_ntlmssp_get_username(session
->auth_ntlmssp_state
),
506 session
->session_info
->sanitized_username
= talloc_strdup(
507 session
->session_info
, tmp
);
509 if (!session
->compat_vuser
->session_info
->guest
) {
510 session
->compat_vuser
->homes_snum
=
511 register_homes_share(session
->session_info
->unix_name
);
514 if (!session_claim(session
->sconn
, session
->compat_vuser
)) {
515 DEBUG(1, ("smb2: Failed to claim session "
517 session
->compat_vuser
->vuid
));
518 TALLOC_FREE(session
->auth_ntlmssp_state
);
519 TALLOC_FREE(session
);
520 return NT_STATUS_LOGON_FAILURE
;
524 session
->status
= NT_STATUS_OK
;
527 * we attach the session to the request
528 * so that the response can be signed
530 smb2req
->session
= session
;
531 if (session
->do_signing
) {
532 smb2req
->do_signing
= true;
535 global_client_caps
|= (CAP_LEVEL_II_OPLOCKS
|CAP_STATUS32
);
537 *out_session_id
= session
->vuid
;
542 static NTSTATUS
smbd_smb2_spnego_auth(struct smbd_smb2_session
*session
,
543 struct smbd_smb2_request
*smb2req
,
544 uint8_t in_security_mode
,
545 DATA_BLOB in_security_buffer
,
546 uint16_t *out_session_flags
,
547 DATA_BLOB
*out_security_buffer
,
548 uint64_t *out_session_id
)
550 DATA_BLOB auth
= data_blob_null
;
551 DATA_BLOB auth_out
= data_blob_null
;
554 if (!spnego_parse_auth(talloc_tos(), in_security_buffer
, &auth
)) {
555 TALLOC_FREE(session
);
556 return NT_STATUS_LOGON_FAILURE
;
559 if (auth
.data
[0] == ASN1_APPLICATION(0)) {
560 /* Might be a second negTokenTarg packet */
561 DATA_BLOB secblob_in
= data_blob_null
;
562 char *kerb_mech
= NULL
;
564 status
= parse_spnego_mechanisms(talloc_tos(),
566 &secblob_in
, &kerb_mech
);
567 if (!NT_STATUS_IS_OK(status
)) {
568 TALLOC_FREE(session
);
573 if (kerb_mech
&& ((lp_security()==SEC_ADS
) ||
574 USE_KERBEROS_KEYTAB
) ) {
575 status
= smbd_smb2_session_setup_krb5(session
,
584 data_blob_free(&secblob_in
);
585 TALLOC_FREE(kerb_mech
);
586 if (!NT_STATUS_IS_OK(status
)) {
587 TALLOC_FREE(session
);
593 /* Can't blunder into NTLMSSP auth if we have
597 DEBUG(3,("smb2: network "
598 "misconfiguration, client sent us a "
599 "krb5 ticket and kerberos security "
601 TALLOC_FREE(session
);
602 data_blob_free(&secblob_in
);
603 TALLOC_FREE(kerb_mech
);
604 return NT_STATUS_LOGON_FAILURE
;
607 data_blob_free(&secblob_in
);
610 if (session
->auth_ntlmssp_state
== NULL
) {
611 status
= auth_ntlmssp_start(&session
->auth_ntlmssp_state
);
612 if (!NT_STATUS_IS_OK(status
)) {
613 data_blob_free(&auth
);
614 TALLOC_FREE(session
);
619 status
= auth_ntlmssp_update(session
->auth_ntlmssp_state
,
622 /* We need to call setup_ntlmssp_session_info() if status==NT_STATUS_OK,
623 or if status is anything except NT_STATUS_MORE_PROCESSING_REQUIRED,
624 as this can trigger map to guest. */
625 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
626 status
= setup_ntlmssp_session_info(session
, status
);
629 if (!NT_STATUS_IS_OK(status
) &&
630 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
631 TALLOC_FREE(session
->auth_ntlmssp_state
);
632 data_blob_free(&auth
);
633 TALLOC_FREE(session
);
637 data_blob_free(&auth
);
639 *out_security_buffer
= spnego_gen_auth_response(smb2req
,
640 &auth_out
, status
, NULL
);
642 if (out_security_buffer
->data
== NULL
) {
643 TALLOC_FREE(session
->auth_ntlmssp_state
);
644 TALLOC_FREE(session
);
645 return NT_STATUS_NO_MEMORY
;
648 *out_session_id
= session
->vuid
;
650 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
651 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
654 /* We're done - claim the session. */
655 return smbd_smb2_common_ntlmssp_auth_return(session
,
663 static NTSTATUS
smbd_smb2_raw_ntlmssp_auth(struct smbd_smb2_session
*session
,
664 struct smbd_smb2_request
*smb2req
,
665 uint8_t in_security_mode
,
666 DATA_BLOB in_security_buffer
,
667 uint16_t *out_session_flags
,
668 DATA_BLOB
*out_security_buffer
,
669 uint64_t *out_session_id
)
672 DATA_BLOB secblob_out
= data_blob_null
;
674 *out_security_buffer
= data_blob_null
;
676 if (session
->auth_ntlmssp_state
== NULL
) {
677 status
= auth_ntlmssp_start(&session
->auth_ntlmssp_state
);
678 if (!NT_STATUS_IS_OK(status
)) {
679 TALLOC_FREE(session
);
685 status
= auth_ntlmssp_update(session
->auth_ntlmssp_state
,
689 if (NT_STATUS_IS_OK(status
) ||
690 NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
691 *out_security_buffer
= data_blob_talloc(smb2req
,
694 if (secblob_out
.data
&& out_security_buffer
->data
== NULL
) {
695 TALLOC_FREE(session
->auth_ntlmssp_state
);
696 TALLOC_FREE(session
);
697 return NT_STATUS_NO_MEMORY
;
701 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
702 *out_session_id
= session
->vuid
;
706 status
= setup_ntlmssp_session_info(session
, status
);
708 if (!NT_STATUS_IS_OK(status
)) {
709 TALLOC_FREE(session
->auth_ntlmssp_state
);
710 TALLOC_FREE(session
);
713 *out_session_id
= session
->vuid
;
715 return smbd_smb2_common_ntlmssp_auth_return(session
,
723 static NTSTATUS
smbd_smb2_session_setup(struct smbd_smb2_request
*smb2req
,
724 uint64_t in_session_id
,
725 uint8_t in_security_mode
,
726 DATA_BLOB in_security_buffer
,
727 uint16_t *out_session_flags
,
728 DATA_BLOB
*out_security_buffer
,
729 uint64_t *out_session_id
)
731 struct smbd_smb2_session
*session
;
733 *out_session_flags
= 0;
736 if (in_session_id
== 0) {
739 /* create a new session */
740 session
= talloc_zero(smb2req
->sconn
, struct smbd_smb2_session
);
741 if (session
== NULL
) {
742 return NT_STATUS_NO_MEMORY
;
744 session
->status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
745 id
= idr_get_new_random(smb2req
->sconn
->smb2
.sessions
.idtree
,
747 smb2req
->sconn
->smb2
.sessions
.limit
);
749 return NT_STATUS_INSUFFICIENT_RESOURCES
;
753 session
->tcons
.idtree
= idr_init(session
);
754 if (session
->tcons
.idtree
== NULL
) {
755 return NT_STATUS_NO_MEMORY
;
757 session
->tcons
.limit
= 0x0000FFFE;
758 session
->tcons
.list
= NULL
;
760 DLIST_ADD_END(smb2req
->sconn
->smb2
.sessions
.list
, session
,
761 struct smbd_smb2_session
*);
762 session
->sconn
= smb2req
->sconn
;
763 talloc_set_destructor(session
, smbd_smb2_session_destructor
);
767 /* lookup an existing session */
768 p
= idr_find(smb2req
->sconn
->smb2
.sessions
.idtree
, in_session_id
);
770 return NT_STATUS_USER_SESSION_DELETED
;
772 session
= talloc_get_type_abort(p
, struct smbd_smb2_session
);
775 if (NT_STATUS_IS_OK(session
->status
)) {
776 return NT_STATUS_REQUEST_NOT_ACCEPTED
;
779 if (in_security_buffer
.data
[0] == ASN1_APPLICATION(0)) {
780 return smbd_smb2_spnego_negotiate(session
,
787 } else if (in_security_buffer
.data
[0] == ASN1_CONTEXT(1)) {
788 return smbd_smb2_spnego_auth(session
,
795 } else if (strncmp((char *)(in_security_buffer
.data
), "NTLMSSP", 7) == 0) {
796 return smbd_smb2_raw_ntlmssp_auth(session
,
805 /* Unknown packet type. */
806 DEBUG(1,("Unknown packet type %u in smb2 sessionsetup\n",
807 (unsigned int)in_security_buffer
.data
[0] ));
808 TALLOC_FREE(session
->auth_ntlmssp_state
);
809 TALLOC_FREE(session
);
810 return NT_STATUS_LOGON_FAILURE
;
813 NTSTATUS
smbd_smb2_request_check_session(struct smbd_smb2_request
*req
)
815 const uint8_t *inhdr
;
816 int i
= req
->current_idx
;
818 uint64_t in_session_id
;
820 struct smbd_smb2_session
*session
;
825 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
827 in_flags
= IVAL(inhdr
, SMB2_HDR_FLAGS
);
828 in_session_id
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
830 if (in_flags
& SMB2_HDR_FLAG_CHAINED
) {
831 in_session_id
= req
->last_session_id
;
834 /* lookup an existing session */
835 p
= idr_find(req
->sconn
->smb2
.sessions
.idtree
, in_session_id
);
837 return NT_STATUS_USER_SESSION_DELETED
;
839 session
= talloc_get_type_abort(p
, struct smbd_smb2_session
);
841 if (!NT_STATUS_IS_OK(session
->status
)) {
842 return NT_STATUS_ACCESS_DENIED
;
845 set_current_user_info(session
->session_info
->sanitized_username
,
846 session
->session_info
->unix_name
,
847 session
->session_info
->info3
->base
.domain
.string
);
849 req
->session
= session
;
850 req
->last_session_id
= in_session_id
;
855 NTSTATUS
smbd_smb2_request_process_logoff(struct smbd_smb2_request
*req
)
860 status
= smbd_smb2_request_verify_sizes(req
, 0x04);
861 if (!NT_STATUS_IS_OK(status
)) {
862 return smbd_smb2_request_error(req
, status
);
866 * TODO: cancel all outstanding requests on the session
867 * and delete all tree connections.
869 smbd_smb2_session_destructor(req
->session
);
871 * we may need to sign the response, so we need to keep
872 * the session until the response is sent to the wire.
874 talloc_steal(req
, req
->session
);
876 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x04);
877 if (outbody
.data
== NULL
) {
878 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
881 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
882 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
884 return smbd_smb2_request_done(req
, outbody
, NULL
);