s4 samba-tool: Implement "testparm" command
[Samba.git] / source3 / smbd / smb2_sesssetup.c
blobd727ee98b02e3db30096cabc577e637aaa633379
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 "../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"
32 #include "auth.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)
44 const uint8_t *inhdr;
45 const uint8_t *inbody;
46 int i = smb2req->current_idx;
47 uint8_t *outhdr;
48 DATA_BLOB outbody;
49 DATA_BLOB outdyn;
50 size_t expected_body_size = 0x19;
51 size_t body_size;
52 uint64_t in_session_id;
53 uint8_t in_security_mode;
54 uint16_t in_security_offset;
55 uint16_t in_security_length;
56 DATA_BLOB in_security_buffer;
57 uint16_t out_session_flags;
58 uint64_t out_session_id;
59 uint16_t out_security_offset;
60 DATA_BLOB out_security_buffer;
61 NTSTATUS status;
63 inhdr = (const uint8_t *)smb2req->in.vector[i+0].iov_base;
65 if (smb2req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
66 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
69 inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
71 body_size = SVAL(inbody, 0x00);
72 if (body_size != expected_body_size) {
73 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
76 in_security_offset = SVAL(inbody, 0x0C);
77 in_security_length = SVAL(inbody, 0x0E);
79 if (in_security_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
80 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
83 if (in_security_length > smb2req->in.vector[i+2].iov_len) {
84 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
87 in_session_id = BVAL(inhdr, SMB2_HDR_SESSION_ID);
88 in_security_mode = CVAL(inbody, 0x03);
89 in_security_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base;
90 in_security_buffer.length = in_security_length;
92 status = smbd_smb2_session_setup(smb2req,
93 in_session_id,
94 in_security_mode,
95 in_security_buffer,
96 &out_session_flags,
97 &out_security_buffer,
98 &out_session_id);
99 if (!NT_STATUS_IS_OK(status) &&
100 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
101 status = nt_status_squash(status);
102 return smbd_smb2_request_error(smb2req, status);
105 out_security_offset = SMB2_HDR_BODY + 0x08;
107 outhdr = (uint8_t *)smb2req->out.vector[i].iov_base;
109 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x08);
110 if (outbody.data == NULL) {
111 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
114 SBVAL(outhdr, SMB2_HDR_SESSION_ID, out_session_id);
116 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
117 SSVAL(outbody.data, 0x02,
118 out_session_flags); /* session flags */
119 SSVAL(outbody.data, 0x04,
120 out_security_offset); /* security buffer offset */
121 SSVAL(outbody.data, 0x06,
122 out_security_buffer.length); /* security buffer length */
124 outdyn = out_security_buffer;
126 return smbd_smb2_request_done_ex(smb2req, status, outbody, &outdyn,
127 __location__);
130 static int smbd_smb2_session_destructor(struct smbd_smb2_session *session)
132 if (session->sconn == NULL) {
133 return 0;
136 /* first free all tcons */
137 while (session->tcons.list) {
138 talloc_free(session->tcons.list);
141 idr_remove(session->sconn->smb2.sessions.idtree, session->vuid);
142 DLIST_REMOVE(session->sconn->smb2.sessions.list, session);
143 invalidate_vuid(session->sconn, session->vuid);
145 session->vuid = 0;
146 session->status = NT_STATUS_USER_SESSION_DELETED;
147 session->sconn = NULL;
149 return 0;
152 static NTSTATUS setup_ntlmssp_session_info(struct smbd_smb2_session *session,
153 NTSTATUS status)
155 if (NT_STATUS_IS_OK(status)) {
156 status = auth_ntlmssp_steal_session_info(session,
157 session->auth_ntlmssp_state,
158 &session->session_info);
159 } else {
160 /* Note that this session_info won't have a session
161 * key. But for map to guest, that's exactly the right
162 * thing - we can't reasonably guess the key the
163 * client wants, as the password was wrong */
164 status = do_map_to_guest(status,
165 &session->session_info,
166 auth_ntlmssp_get_username(session->auth_ntlmssp_state),
167 auth_ntlmssp_get_domain(session->auth_ntlmssp_state));
169 return status;
172 #ifdef HAVE_KRB5
173 static NTSTATUS smbd_smb2_session_setup_krb5(struct smbd_smb2_session *session,
174 struct smbd_smb2_request *smb2req,
175 uint8_t in_security_mode,
176 const DATA_BLOB *secblob,
177 const char *mechOID,
178 uint16_t *out_session_flags,
179 DATA_BLOB *out_security_buffer,
180 uint64_t *out_session_id)
182 DATA_BLOB ap_rep = data_blob_null;
183 DATA_BLOB ap_rep_wrapped = data_blob_null;
184 DATA_BLOB ticket = data_blob_null;
185 DATA_BLOB session_key = data_blob_null;
186 DATA_BLOB secblob_out = data_blob_null;
187 uint8 tok_id[2];
188 struct PAC_LOGON_INFO *logon_info = NULL;
189 char *principal = NULL;
190 char *user = NULL;
191 char *domain = NULL;
192 struct passwd *pw = NULL;
193 NTSTATUS status;
194 char *real_username;
195 fstring tmp;
196 bool username_was_mapped = false;
197 bool map_domainuser_to_guest = false;
199 if (!spnego_parse_krb5_wrap(talloc_tos(), *secblob, &ticket, tok_id)) {
200 status = NT_STATUS_LOGON_FAILURE;
201 goto fail;
204 status = ads_verify_ticket(smb2req, lp_realm(), 0, &ticket,
205 &principal, &logon_info, &ap_rep,
206 &session_key, true);
208 if (!NT_STATUS_IS_OK(status)) {
209 DEBUG(1,("smb2: Failed to verify incoming ticket with error %s!\n",
210 nt_errstr(status)));
211 if (!NT_STATUS_EQUAL(status, NT_STATUS_TIME_DIFFERENCE_AT_DC)) {
212 status = NT_STATUS_LOGON_FAILURE;
214 goto fail;
217 status = get_user_from_kerberos_info(talloc_tos(),
218 smb2req->sconn->client_id.name,
219 principal, logon_info,
220 &username_was_mapped,
221 &map_domainuser_to_guest,
222 &user, &domain,
223 &real_username, &pw);
224 if (!NT_STATUS_IS_OK(status)) {
225 goto fail;
228 /* save the PAC data if we have it */
229 if (logon_info) {
230 netsamlogon_cache_store(user, &logon_info->info3);
233 /* setup the string used by %U */
234 sub_set_smb_name(real_username);
236 /* reload services so that the new %U is taken into account */
237 reload_services(smb2req->sconn->msg_ctx, smb2req->sconn->sock, true);
239 status = make_session_info_krb5(session,
240 user, domain, real_username, pw,
241 logon_info, map_domainuser_to_guest,
242 username_was_mapped,
243 &session_key,
244 &session->session_info);
245 if (!NT_STATUS_IS_OK(status)) {
246 DEBUG(1, ("smb2: make_server_info_krb5 failed\n"));
247 goto fail;
250 if ((in_security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
251 lp_server_signing() == Required) {
252 session->do_signing = true;
255 if (session->session_info->guest) {
256 /* we map anonymous to guest internally */
257 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
258 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
259 /* force no signing */
260 session->do_signing = false;
263 session->session_key = session->session_info->session_key;
265 session->compat_vuser = talloc_zero(session, user_struct);
266 if (session->compat_vuser == NULL) {
267 status = NT_STATUS_NO_MEMORY;
268 goto fail;
270 session->compat_vuser->auth_ntlmssp_state = NULL;
271 session->compat_vuser->homes_snum = -1;
272 session->compat_vuser->session_info = session->session_info;
273 session->compat_vuser->session_keystr = NULL;
274 session->compat_vuser->vuid = session->vuid;
275 DLIST_ADD(session->sconn->smb1.sessions.validated_users, session->compat_vuser);
277 /* This is a potentially untrusted username */
278 alpha_strcpy(tmp, user, ". _-$", sizeof(tmp));
279 session->session_info->sanitized_username =
280 talloc_strdup(session->session_info, tmp);
282 if (!session->session_info->guest) {
283 session->compat_vuser->homes_snum =
284 register_homes_share(session->session_info->unix_name);
287 if (!session_claim(session->sconn, session->compat_vuser)) {
288 DEBUG(1, ("smb2: Failed to claim session "
289 "for vuid=%d\n",
290 session->compat_vuser->vuid));
291 goto fail;
294 session->status = NT_STATUS_OK;
297 * we attach the session to the request
298 * so that the response can be signed
300 smb2req->session = session;
301 if (session->do_signing) {
302 smb2req->do_signing = true;
305 global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
306 status = NT_STATUS_OK;
308 /* wrap that up in a nice GSS-API wrapping */
309 ap_rep_wrapped = spnego_gen_krb5_wrap(talloc_tos(), ap_rep,
310 TOK_ID_KRB_AP_REP);
312 secblob_out = spnego_gen_auth_response(
313 talloc_tos(),
314 &ap_rep_wrapped,
315 status,
316 mechOID);
318 *out_security_buffer = data_blob_talloc(smb2req,
319 secblob_out.data,
320 secblob_out.length);
321 if (secblob_out.data && out_security_buffer->data == NULL) {
322 status = NT_STATUS_NO_MEMORY;
323 goto fail;
326 data_blob_free(&ap_rep);
327 data_blob_free(&ap_rep_wrapped);
328 data_blob_free(&ticket);
329 data_blob_free(&session_key);
330 data_blob_free(&secblob_out);
332 *out_session_id = session->vuid;
334 return NT_STATUS_OK;
336 fail:
338 data_blob_free(&ap_rep);
339 data_blob_free(&ap_rep_wrapped);
340 data_blob_free(&ticket);
341 data_blob_free(&session_key);
342 data_blob_free(&secblob_out);
344 ap_rep_wrapped = data_blob_null;
345 secblob_out = spnego_gen_auth_response(
346 talloc_tos(),
347 &ap_rep_wrapped,
348 status,
349 mechOID);
351 *out_security_buffer = data_blob_talloc(smb2req,
352 secblob_out.data,
353 secblob_out.length);
354 data_blob_free(&secblob_out);
355 return status;
357 #endif
359 static NTSTATUS smbd_smb2_spnego_negotiate(struct smbd_smb2_session *session,
360 struct smbd_smb2_request *smb2req,
361 uint8_t in_security_mode,
362 DATA_BLOB in_security_buffer,
363 uint16_t *out_session_flags,
364 DATA_BLOB *out_security_buffer,
365 uint64_t *out_session_id)
367 DATA_BLOB secblob_in = data_blob_null;
368 DATA_BLOB chal_out = data_blob_null;
369 char *kerb_mech = NULL;
370 NTSTATUS status;
372 /* Ensure we have no old NTLM state around. */
373 TALLOC_FREE(session->auth_ntlmssp_state);
375 status = parse_spnego_mechanisms(talloc_tos(), in_security_buffer,
376 &secblob_in, &kerb_mech);
377 if (!NT_STATUS_IS_OK(status)) {
378 goto out;
381 #ifdef HAVE_KRB5
382 if (kerb_mech && ((lp_security()==SEC_ADS) ||
383 USE_KERBEROS_KEYTAB) ) {
384 status = smbd_smb2_session_setup_krb5(session,
385 smb2req,
386 in_security_mode,
387 &secblob_in,
388 kerb_mech,
389 out_session_flags,
390 out_security_buffer,
391 out_session_id);
393 goto out;
395 #endif
397 if (kerb_mech) {
398 /* The mechtoken is a krb5 ticket, but
399 * we need to fall back to NTLM. */
401 DEBUG(3,("smb2: Got krb5 ticket in SPNEGO "
402 "but set to downgrade to NTLMSSP\n"));
404 status = NT_STATUS_MORE_PROCESSING_REQUIRED;
405 } else {
406 /* Fall back to NTLMSSP. */
407 status = auth_ntlmssp_start(&session->auth_ntlmssp_state);
408 if (!NT_STATUS_IS_OK(status)) {
409 goto out;
412 status = auth_ntlmssp_update(session->auth_ntlmssp_state,
413 secblob_in,
414 &chal_out);
417 if (!NT_STATUS_IS_OK(status) &&
418 !NT_STATUS_EQUAL(status,
419 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
420 goto out;
423 *out_security_buffer = spnego_gen_auth_response(smb2req,
424 &chal_out,
425 status,
426 OID_NTLMSSP);
427 if (out_security_buffer->data == NULL) {
428 status = NT_STATUS_NO_MEMORY;
429 goto out;
431 *out_session_id = session->vuid;
433 out:
435 data_blob_free(&secblob_in);
436 data_blob_free(&chal_out);
437 TALLOC_FREE(kerb_mech);
438 if (!NT_STATUS_IS_OK(status) &&
439 !NT_STATUS_EQUAL(status,
440 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
441 TALLOC_FREE(session->auth_ntlmssp_state);
442 TALLOC_FREE(session);
444 return status;
447 static NTSTATUS smbd_smb2_common_ntlmssp_auth_return(struct smbd_smb2_session *session,
448 struct smbd_smb2_request *smb2req,
449 uint8_t in_security_mode,
450 DATA_BLOB in_security_buffer,
451 uint16_t *out_session_flags,
452 uint64_t *out_session_id)
454 fstring tmp;
456 if ((in_security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
457 lp_server_signing() == Required) {
458 session->do_signing = true;
461 if (session->session_info->guest) {
462 /* we map anonymous to guest internally */
463 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
464 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
465 /* force no signing */
466 session->do_signing = false;
469 session->session_key = session->session_info->session_key;
471 session->compat_vuser = talloc_zero(session, user_struct);
472 if (session->compat_vuser == NULL) {
473 TALLOC_FREE(session->auth_ntlmssp_state);
474 TALLOC_FREE(session);
475 return NT_STATUS_NO_MEMORY;
477 session->compat_vuser->auth_ntlmssp_state = session->auth_ntlmssp_state;
478 session->compat_vuser->homes_snum = -1;
479 session->compat_vuser->session_info = session->session_info;
480 session->compat_vuser->session_keystr = NULL;
481 session->compat_vuser->vuid = session->vuid;
482 DLIST_ADD(session->sconn->smb1.sessions.validated_users, session->compat_vuser);
484 /* This is a potentially untrusted username */
485 alpha_strcpy(tmp,
486 auth_ntlmssp_get_username(session->auth_ntlmssp_state),
487 ". _-$",
488 sizeof(tmp));
489 session->session_info->sanitized_username = talloc_strdup(
490 session->session_info, tmp);
492 if (!session->compat_vuser->session_info->guest) {
493 session->compat_vuser->homes_snum =
494 register_homes_share(session->session_info->unix_name);
497 if (!session_claim(session->sconn, session->compat_vuser)) {
498 DEBUG(1, ("smb2: Failed to claim session "
499 "for vuid=%d\n",
500 session->compat_vuser->vuid));
501 TALLOC_FREE(session->auth_ntlmssp_state);
502 TALLOC_FREE(session);
503 return NT_STATUS_LOGON_FAILURE;
507 session->status = NT_STATUS_OK;
510 * we attach the session to the request
511 * so that the response can be signed
513 smb2req->session = session;
514 if (session->do_signing) {
515 smb2req->do_signing = true;
518 global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
520 *out_session_id = session->vuid;
522 return NT_STATUS_OK;
525 static NTSTATUS smbd_smb2_spnego_auth(struct smbd_smb2_session *session,
526 struct smbd_smb2_request *smb2req,
527 uint8_t in_security_mode,
528 DATA_BLOB in_security_buffer,
529 uint16_t *out_session_flags,
530 DATA_BLOB *out_security_buffer,
531 uint64_t *out_session_id)
533 DATA_BLOB auth = data_blob_null;
534 DATA_BLOB auth_out = data_blob_null;
535 NTSTATUS status;
537 if (!spnego_parse_auth(talloc_tos(), in_security_buffer, &auth)) {
538 TALLOC_FREE(session);
539 return NT_STATUS_LOGON_FAILURE;
542 if (auth.data[0] == ASN1_APPLICATION(0)) {
543 /* Might be a second negTokenTarg packet */
544 DATA_BLOB secblob_in = data_blob_null;
545 char *kerb_mech = NULL;
547 status = parse_spnego_mechanisms(talloc_tos(),
548 in_security_buffer,
549 &secblob_in, &kerb_mech);
550 if (!NT_STATUS_IS_OK(status)) {
551 TALLOC_FREE(session);
552 return status;
555 #ifdef HAVE_KRB5
556 if (kerb_mech && ((lp_security()==SEC_ADS) ||
557 USE_KERBEROS_KEYTAB) ) {
558 status = smbd_smb2_session_setup_krb5(session,
559 smb2req,
560 in_security_mode,
561 &secblob_in,
562 kerb_mech,
563 out_session_flags,
564 out_security_buffer,
565 out_session_id);
567 data_blob_free(&secblob_in);
568 TALLOC_FREE(kerb_mech);
569 if (!NT_STATUS_IS_OK(status)) {
570 TALLOC_FREE(session);
572 return status;
574 #endif
576 /* Can't blunder into NTLMSSP auth if we have
577 * a krb5 ticket. */
579 if (kerb_mech) {
580 DEBUG(3,("smb2: network "
581 "misconfiguration, client sent us a "
582 "krb5 ticket and kerberos security "
583 "not enabled\n"));
584 TALLOC_FREE(session);
585 data_blob_free(&secblob_in);
586 TALLOC_FREE(kerb_mech);
587 return NT_STATUS_LOGON_FAILURE;
590 data_blob_free(&secblob_in);
593 if (session->auth_ntlmssp_state == NULL) {
594 status = auth_ntlmssp_start(&session->auth_ntlmssp_state);
595 if (!NT_STATUS_IS_OK(status)) {
596 data_blob_free(&auth);
597 TALLOC_FREE(session);
598 return status;
602 status = auth_ntlmssp_update(session->auth_ntlmssp_state,
603 auth,
604 &auth_out);
605 /* We need to call setup_ntlmssp_session_info() if status==NT_STATUS_OK,
606 or if status is anything except NT_STATUS_MORE_PROCESSING_REQUIRED,
607 as this can trigger map to guest. */
608 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
609 status = setup_ntlmssp_session_info(session, status);
612 if (!NT_STATUS_IS_OK(status) &&
613 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
614 TALLOC_FREE(session->auth_ntlmssp_state);
615 data_blob_free(&auth);
616 TALLOC_FREE(session);
617 return status;
620 data_blob_free(&auth);
622 *out_security_buffer = spnego_gen_auth_response(smb2req,
623 &auth_out, status, NULL);
625 if (out_security_buffer->data == NULL) {
626 TALLOC_FREE(session->auth_ntlmssp_state);
627 TALLOC_FREE(session);
628 return NT_STATUS_NO_MEMORY;
631 *out_session_id = session->vuid;
633 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
634 return NT_STATUS_MORE_PROCESSING_REQUIRED;
637 /* We're done - claim the session. */
638 return smbd_smb2_common_ntlmssp_auth_return(session,
639 smb2req,
640 in_security_mode,
641 in_security_buffer,
642 out_session_flags,
643 out_session_id);
646 static NTSTATUS smbd_smb2_raw_ntlmssp_auth(struct smbd_smb2_session *session,
647 struct smbd_smb2_request *smb2req,
648 uint8_t in_security_mode,
649 DATA_BLOB in_security_buffer,
650 uint16_t *out_session_flags,
651 DATA_BLOB *out_security_buffer,
652 uint64_t *out_session_id)
654 NTSTATUS status;
655 DATA_BLOB secblob_out = data_blob_null;
657 if (session->auth_ntlmssp_state == NULL) {
658 status = auth_ntlmssp_start(&session->auth_ntlmssp_state);
659 if (!NT_STATUS_IS_OK(status)) {
660 TALLOC_FREE(session);
661 return status;
665 /* RAW NTLMSSP */
666 status = auth_ntlmssp_update(session->auth_ntlmssp_state,
667 in_security_buffer,
668 &secblob_out);
670 if (NT_STATUS_IS_OK(status) ||
671 NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
672 *out_security_buffer = data_blob_talloc(smb2req,
673 secblob_out.data,
674 secblob_out.length);
675 if (secblob_out.data && out_security_buffer->data == NULL) {
676 TALLOC_FREE(session->auth_ntlmssp_state);
677 TALLOC_FREE(session);
678 return NT_STATUS_NO_MEMORY;
682 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
683 *out_session_id = session->vuid;
684 return status;
687 status = setup_ntlmssp_session_info(session, status);
689 if (!NT_STATUS_IS_OK(status)) {
690 TALLOC_FREE(session->auth_ntlmssp_state);
691 TALLOC_FREE(session);
692 return status;
694 *out_session_id = session->vuid;
696 return smbd_smb2_common_ntlmssp_auth_return(session,
697 smb2req,
698 in_security_mode,
699 in_security_buffer,
700 out_session_flags,
701 out_session_id);
704 static NTSTATUS smbd_smb2_session_setup(struct smbd_smb2_request *smb2req,
705 uint64_t in_session_id,
706 uint8_t in_security_mode,
707 DATA_BLOB in_security_buffer,
708 uint16_t *out_session_flags,
709 DATA_BLOB *out_security_buffer,
710 uint64_t *out_session_id)
712 struct smbd_smb2_session *session;
714 *out_session_flags = 0;
715 *out_session_id = 0;
717 if (in_session_id == 0) {
718 int id;
720 /* create a new session */
721 session = talloc_zero(smb2req->sconn, struct smbd_smb2_session);
722 if (session == NULL) {
723 return NT_STATUS_NO_MEMORY;
725 session->status = NT_STATUS_MORE_PROCESSING_REQUIRED;
726 id = idr_get_new_random(smb2req->sconn->smb2.sessions.idtree,
727 session,
728 smb2req->sconn->smb2.sessions.limit);
729 if (id == -1) {
730 return NT_STATUS_INSUFFICIENT_RESOURCES;
732 session->vuid = id;
734 session->tcons.idtree = idr_init(session);
735 if (session->tcons.idtree == NULL) {
736 return NT_STATUS_NO_MEMORY;
738 session->tcons.limit = 0x0000FFFE;
739 session->tcons.list = NULL;
741 DLIST_ADD_END(smb2req->sconn->smb2.sessions.list, session,
742 struct smbd_smb2_session *);
743 session->sconn = smb2req->sconn;
744 talloc_set_destructor(session, smbd_smb2_session_destructor);
745 } else {
746 void *p;
748 /* lookup an existing session */
749 p = idr_find(smb2req->sconn->smb2.sessions.idtree, in_session_id);
750 if (p == NULL) {
751 return NT_STATUS_USER_SESSION_DELETED;
753 session = talloc_get_type_abort(p, struct smbd_smb2_session);
756 if (NT_STATUS_IS_OK(session->status)) {
757 return NT_STATUS_REQUEST_NOT_ACCEPTED;
760 if (in_security_buffer.data[0] == ASN1_APPLICATION(0)) {
761 return smbd_smb2_spnego_negotiate(session,
762 smb2req,
763 in_security_mode,
764 in_security_buffer,
765 out_session_flags,
766 out_security_buffer,
767 out_session_id);
768 } else if (in_security_buffer.data[0] == ASN1_CONTEXT(1)) {
769 return smbd_smb2_spnego_auth(session,
770 smb2req,
771 in_security_mode,
772 in_security_buffer,
773 out_session_flags,
774 out_security_buffer,
775 out_session_id);
776 } else if (strncmp((char *)(in_security_buffer.data), "NTLMSSP", 7) == 0) {
777 return smbd_smb2_raw_ntlmssp_auth(session,
778 smb2req,
779 in_security_mode,
780 in_security_buffer,
781 out_session_flags,
782 out_security_buffer,
783 out_session_id);
786 /* Unknown packet type. */
787 DEBUG(1,("Unknown packet type %u in smb2 sessionsetup\n",
788 (unsigned int)in_security_buffer.data[0] ));
789 TALLOC_FREE(session->auth_ntlmssp_state);
790 TALLOC_FREE(session);
791 return NT_STATUS_LOGON_FAILURE;
794 NTSTATUS smbd_smb2_request_check_session(struct smbd_smb2_request *req)
796 const uint8_t *inhdr;
797 const uint8_t *outhdr;
798 int i = req->current_idx;
799 uint64_t in_session_id;
800 void *p;
801 struct smbd_smb2_session *session;
802 bool chained_fixup = false;
804 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
806 in_session_id = BVAL(inhdr, SMB2_HDR_SESSION_ID);
808 if (in_session_id == (0xFFFFFFFFFFFFFFFFLL)) {
809 if (req->async) {
811 * async request - fill in session_id from
812 * already setup request out.vector[].iov_base.
814 outhdr = (const uint8_t *)req->out.vector[i].iov_base;
815 in_session_id = BVAL(outhdr, SMB2_HDR_SESSION_ID);
816 } else if (i > 2) {
818 * Chained request - fill in session_id from
819 * the previous request out.vector[].iov_base.
821 outhdr = (const uint8_t *)req->out.vector[i-3].iov_base;
822 in_session_id = BVAL(outhdr, SMB2_HDR_SESSION_ID);
823 chained_fixup = true;
827 /* lookup an existing session */
828 p = idr_find(req->sconn->smb2.sessions.idtree, in_session_id);
829 if (p == NULL) {
830 return NT_STATUS_USER_SESSION_DELETED;
832 session = talloc_get_type_abort(p, struct smbd_smb2_session);
834 if (!NT_STATUS_IS_OK(session->status)) {
835 return NT_STATUS_ACCESS_DENIED;
838 set_current_user_info(session->session_info->sanitized_username,
839 session->session_info->unix_name,
840 session->session_info->info3->base.domain.string);
842 req->session = session;
844 if (chained_fixup) {
845 /* Fix up our own outhdr. */
846 outhdr = (const uint8_t *)req->out.vector[i].iov_base;
847 SBVAL(outhdr, SMB2_HDR_SESSION_ID, in_session_id);
849 return NT_STATUS_OK;
852 NTSTATUS smbd_smb2_request_process_logoff(struct smbd_smb2_request *req)
854 const uint8_t *inbody;
855 int i = req->current_idx;
856 DATA_BLOB outbody;
857 size_t expected_body_size = 0x04;
858 size_t body_size;
860 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
861 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
864 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
866 body_size = SVAL(inbody, 0x00);
867 if (body_size != expected_body_size) {
868 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
872 * TODO: cancel all outstanding requests on the session
873 * and delete all tree connections.
875 smbd_smb2_session_destructor(req->session);
877 * we may need to sign the response, so we need to keep
878 * the session until the response is sent to the wire.
880 talloc_steal(req, req->session);
882 outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
883 if (outbody.data == NULL) {
884 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
887 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
888 SSVAL(outbody.data, 0x02, 0); /* reserved */
890 return smbd_smb2_request_done(req, outbody, NULL);