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 "../auth/gensec/gensec.h"
29 #include "ntlmssp_wrap.h"
30 #include "../librpc/gen_ndr/krb5pac.h"
31 #include "libads/kerberos_proto.h"
32 #include "../lib/util/asn1.h"
34 #include "../lib/tsocket/tsocket.h"
35 #include "../libcli/security/security.h"
37 static NTSTATUS smbd_smb2_session_setup(struct smbd_smb2_request *smb2req,
38 uint64_t in_session_id,
39 uint8_t in_security_mode,
40 DATA_BLOB in_security_buffer,
41 uint16_t *out_session_flags,
42 DATA_BLOB *out_security_buffer,
43 uint64_t *out_session_id);
45 NTSTATUS smbd_smb2_request_process_sesssetup(struct smbd_smb2_request *smb2req)
48 const uint8_t *inbody;
49 int i = smb2req->current_idx;
53 uint64_t in_session_id;
54 uint8_t in_security_mode;
55 uint16_t in_security_offset;
56 uint16_t in_security_length;
57 DATA_BLOB in_security_buffer;
58 uint16_t out_session_flags;
59 uint64_t out_session_id;
60 uint16_t out_security_offset;
61 DATA_BLOB out_security_buffer;
64 status = smbd_smb2_request_verify_sizes(smb2req, 0x19);
65 if (!NT_STATUS_IS_OK(status)) {
66 return smbd_smb2_request_error(smb2req, status);
68 inhdr = (const uint8_t *)smb2req->in.vector[i+0].iov_base;
69 inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
71 in_security_offset = SVAL(inbody, 0x0C);
72 in_security_length = SVAL(inbody, 0x0E);
74 if (in_security_offset != (SMB2_HDR_BODY + smb2req->in.vector[i+1].iov_len)) {
75 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
78 if (in_security_length > smb2req->in.vector[i+2].iov_len) {
79 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
82 in_session_id = BVAL(inhdr, SMB2_HDR_SESSION_ID);
83 in_security_mode = CVAL(inbody, 0x03);
84 in_security_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base;
85 in_security_buffer.length = in_security_length;
87 status = smbd_smb2_session_setup(smb2req,
94 if (!NT_STATUS_IS_OK(status) &&
95 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
96 status = nt_status_squash(status);
97 return smbd_smb2_request_error(smb2req, status);
100 out_security_offset = SMB2_HDR_BODY + 0x08;
102 outhdr = (uint8_t *)smb2req->out.vector[i].iov_base;
104 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x08);
105 if (outbody.data == NULL) {
106 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
109 SBVAL(outhdr, SMB2_HDR_SESSION_ID, out_session_id);
111 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
112 SSVAL(outbody.data, 0x02,
113 out_session_flags); /* session flags */
114 SSVAL(outbody.data, 0x04,
115 out_security_offset); /* security buffer offset */
116 SSVAL(outbody.data, 0x06,
117 out_security_buffer.length); /* security buffer length */
119 outdyn = out_security_buffer;
121 return smbd_smb2_request_done_ex(smb2req, status, outbody, &outdyn,
125 static int smbd_smb2_session_destructor(struct smbd_smb2_session *session)
127 if (session->sconn == NULL) {
131 /* first free all tcons */
132 while (session->tcons.list) {
133 talloc_free(session->tcons.list);
136 idr_remove(session->sconn->smb2.sessions.idtree, session->vuid);
137 DLIST_REMOVE(session->sconn->smb2.sessions.list, session);
138 invalidate_vuid(session->sconn, session->vuid);
141 session->status = NT_STATUS_USER_SESSION_DELETED;
142 session->sconn = NULL;
148 static NTSTATUS smbd_smb2_session_setup_krb5(struct smbd_smb2_session *session,
149 struct smbd_smb2_request *smb2req,
150 uint8_t in_security_mode,
151 const DATA_BLOB *secblob,
153 uint16_t *out_session_flags,
154 DATA_BLOB *out_security_buffer,
155 uint64_t *out_session_id)
157 DATA_BLOB ap_rep = data_blob_null;
158 DATA_BLOB ap_rep_wrapped = data_blob_null;
159 DATA_BLOB ticket = data_blob_null;
160 DATA_BLOB session_key = data_blob_null;
161 DATA_BLOB secblob_out = data_blob_null;
163 struct PAC_LOGON_INFO *logon_info = NULL;
164 char *principal = NULL;
167 struct passwd *pw = NULL;
170 bool username_was_mapped = false;
171 bool map_domainuser_to_guest = false;
173 if (!spnego_parse_krb5_wrap(talloc_tos(), *secblob, &ticket, tok_id)) {
174 status = NT_STATUS_LOGON_FAILURE;
178 status = ads_verify_ticket(smb2req, lp_realm(), 0, &ticket,
179 &principal, &logon_info, &ap_rep,
182 if (!NT_STATUS_IS_OK(status)) {
183 DEBUG(1,("smb2: Failed to verify incoming ticket with error %s!\n",
185 if (!NT_STATUS_EQUAL(status, NT_STATUS_TIME_DIFFERENCE_AT_DC)) {
186 status = NT_STATUS_LOGON_FAILURE;
191 status = get_user_from_kerberos_info(talloc_tos(),
192 session->sconn->remote_hostname,
193 principal, logon_info,
194 &username_was_mapped,
195 &map_domainuser_to_guest,
197 &real_username, &pw);
198 if (!NT_STATUS_IS_OK(status)) {
202 /* save the PAC data if we have it */
204 netsamlogon_cache_store(user, &logon_info->info3);
207 /* setup the string used by %U */
208 sub_set_smb_name(real_username);
210 /* reload services so that the new %U is taken into account */
211 reload_services(smb2req->sconn->msg_ctx, smb2req->sconn->sock, true);
213 status = make_session_info_krb5(session,
214 user, domain, real_username, pw,
215 logon_info, map_domainuser_to_guest,
218 &session->session_info);
219 if (!NT_STATUS_IS_OK(status)) {
220 DEBUG(1, ("smb2: make_server_info_krb5 failed\n"));
224 if ((in_security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
225 lp_server_signing() == Required) {
226 session->do_signing = true;
229 if (security_session_user_level(session->session_info, NULL) < SECURITY_USER) {
230 /* we map anonymous to guest internally */
231 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
232 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
233 /* force no signing */
234 session->do_signing = false;
237 session->session_key = session->session_info->session_key;
239 session->compat_vuser = talloc_zero(session, user_struct);
240 if (session->compat_vuser == NULL) {
241 status = NT_STATUS_NO_MEMORY;
244 session->compat_vuser->auth_ntlmssp_state = NULL;
245 session->compat_vuser->homes_snum = -1;
246 session->compat_vuser->session_info = session->session_info;
247 session->compat_vuser->session_keystr = NULL;
248 session->compat_vuser->vuid = session->vuid;
249 DLIST_ADD(session->sconn->smb1.sessions.validated_users, session->compat_vuser);
251 if (security_session_user_level(session->session_info, NULL) >= SECURITY_USER) {
252 session->compat_vuser->homes_snum =
253 register_homes_share(session->session_info->unix_info->unix_name);
256 if (!session_claim(session->sconn, session->compat_vuser)) {
257 DEBUG(1, ("smb2: Failed to claim session "
259 session->compat_vuser->vuid));
263 session->status = NT_STATUS_OK;
266 * we attach the session to the request
267 * so that the response can be signed
269 smb2req->session = session;
270 if (session->do_signing) {
271 smb2req->do_signing = true;
274 global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
275 status = NT_STATUS_OK;
277 /* wrap that up in a nice GSS-API wrapping */
278 ap_rep_wrapped = spnego_gen_krb5_wrap(talloc_tos(), ap_rep,
281 secblob_out = spnego_gen_auth_response(
287 *out_security_buffer = data_blob_talloc(smb2req,
290 if (secblob_out.data && out_security_buffer->data == NULL) {
291 status = NT_STATUS_NO_MEMORY;
295 data_blob_free(&ap_rep);
296 data_blob_free(&ap_rep_wrapped);
297 data_blob_free(&ticket);
298 data_blob_free(&session_key);
299 data_blob_free(&secblob_out);
301 *out_session_id = session->vuid;
307 data_blob_free(&ap_rep);
308 data_blob_free(&ap_rep_wrapped);
309 data_blob_free(&ticket);
310 data_blob_free(&session_key);
311 data_blob_free(&secblob_out);
313 ap_rep_wrapped = data_blob_null;
314 secblob_out = spnego_gen_auth_response(
320 *out_security_buffer = data_blob_talloc(smb2req,
323 data_blob_free(&secblob_out);
328 static NTSTATUS smbd_smb2_spnego_negotiate(struct smbd_smb2_session *session,
329 struct smbd_smb2_request *smb2req,
330 uint8_t in_security_mode,
331 DATA_BLOB in_security_buffer,
332 uint16_t *out_session_flags,
333 DATA_BLOB *out_security_buffer,
334 uint64_t *out_session_id)
336 DATA_BLOB secblob_in = data_blob_null;
337 DATA_BLOB chal_out = data_blob_null;
338 char *kerb_mech = NULL;
341 /* Ensure we have no old NTLM state around. */
342 TALLOC_FREE(session->auth_ntlmssp_state);
344 status = parse_spnego_mechanisms(talloc_tos(), in_security_buffer,
345 &secblob_in, &kerb_mech);
346 if (!NT_STATUS_IS_OK(status)) {
351 if (kerb_mech && ((lp_security()==SEC_ADS) ||
352 USE_KERBEROS_KEYTAB) ) {
353 status = smbd_smb2_session_setup_krb5(session,
367 /* The mechtoken is a krb5 ticket, but
368 * we need to fall back to NTLM. */
370 DEBUG(3,("smb2: Got krb5 ticket in SPNEGO "
371 "but set to downgrade to NTLMSSP\n"));
373 status = NT_STATUS_MORE_PROCESSING_REQUIRED;
375 /* Fall back to NTLMSSP. */
376 status = auth_ntlmssp_prepare(session->sconn->remote_address,
377 &session->auth_ntlmssp_state);
378 if (!NT_STATUS_IS_OK(status)) {
382 auth_ntlmssp_want_feature(session->auth_ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
384 status = auth_ntlmssp_start(session->auth_ntlmssp_state);
385 if (!NT_STATUS_IS_OK(status)) {
389 status = auth_ntlmssp_update(session->auth_ntlmssp_state,
395 if (!NT_STATUS_IS_OK(status) &&
396 !NT_STATUS_EQUAL(status,
397 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
401 *out_security_buffer = spnego_gen_auth_response(smb2req,
405 if (out_security_buffer->data == NULL) {
406 status = NT_STATUS_NO_MEMORY;
409 *out_session_id = session->vuid;
413 data_blob_free(&secblob_in);
414 data_blob_free(&chal_out);
415 TALLOC_FREE(kerb_mech);
416 if (!NT_STATUS_IS_OK(status) &&
417 !NT_STATUS_EQUAL(status,
418 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
419 TALLOC_FREE(session->auth_ntlmssp_state);
420 TALLOC_FREE(session);
425 static NTSTATUS smbd_smb2_common_ntlmssp_auth_return(struct smbd_smb2_session *session,
426 struct smbd_smb2_request *smb2req,
427 uint8_t in_security_mode,
428 DATA_BLOB in_security_buffer,
429 uint16_t *out_session_flags,
430 uint64_t *out_session_id)
432 if ((in_security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
433 lp_server_signing() == Required) {
434 session->do_signing = true;
437 if (security_session_user_level(session->session_info, NULL) < SECURITY_USER) {
438 /* we map anonymous to guest internally */
439 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
440 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
441 /* force no signing */
442 session->do_signing = false;
445 session->session_key = session->session_info->session_key;
447 session->compat_vuser = talloc_zero(session, user_struct);
448 if (session->compat_vuser == NULL) {
449 TALLOC_FREE(session->auth_ntlmssp_state);
450 TALLOC_FREE(session);
451 return NT_STATUS_NO_MEMORY;
453 session->compat_vuser->auth_ntlmssp_state = session->auth_ntlmssp_state;
454 session->compat_vuser->homes_snum = -1;
455 session->compat_vuser->session_info = session->session_info;
456 session->compat_vuser->session_keystr = NULL;
457 session->compat_vuser->vuid = session->vuid;
458 DLIST_ADD(session->sconn->smb1.sessions.validated_users, session->compat_vuser);
460 if (security_session_user_level(session->session_info, NULL) >= SECURITY_USER) {
461 session->compat_vuser->homes_snum =
462 register_homes_share(session->session_info->unix_info->unix_name);
465 if (!session_claim(session->sconn, session->compat_vuser)) {
466 DEBUG(1, ("smb2: Failed to claim session "
468 session->compat_vuser->vuid));
469 TALLOC_FREE(session->auth_ntlmssp_state);
470 TALLOC_FREE(session);
471 return NT_STATUS_LOGON_FAILURE;
475 session->status = NT_STATUS_OK;
478 * we attach the session to the request
479 * so that the response can be signed
481 smb2req->session = session;
482 if (session->do_signing) {
483 smb2req->do_signing = true;
486 global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
488 *out_session_id = session->vuid;
493 static NTSTATUS smbd_smb2_spnego_auth(struct smbd_smb2_session *session,
494 struct smbd_smb2_request *smb2req,
495 uint8_t in_security_mode,
496 DATA_BLOB in_security_buffer,
497 uint16_t *out_session_flags,
498 DATA_BLOB *out_security_buffer,
499 uint64_t *out_session_id)
501 DATA_BLOB auth = data_blob_null;
502 DATA_BLOB auth_out = data_blob_null;
505 if (!spnego_parse_auth(talloc_tos(), in_security_buffer, &auth)) {
506 TALLOC_FREE(session);
507 return NT_STATUS_LOGON_FAILURE;
510 if (auth.data[0] == ASN1_APPLICATION(0)) {
511 /* Might be a second negTokenTarg packet */
512 DATA_BLOB secblob_in = data_blob_null;
513 char *kerb_mech = NULL;
515 status = parse_spnego_mechanisms(talloc_tos(),
517 &secblob_in, &kerb_mech);
518 if (!NT_STATUS_IS_OK(status)) {
519 TALLOC_FREE(session);
524 if (kerb_mech && ((lp_security()==SEC_ADS) ||
525 USE_KERBEROS_KEYTAB) ) {
526 status = smbd_smb2_session_setup_krb5(session,
535 data_blob_free(&secblob_in);
536 TALLOC_FREE(kerb_mech);
537 if (!NT_STATUS_IS_OK(status)) {
538 TALLOC_FREE(session);
544 /* Can't blunder into NTLMSSP auth if we have
548 DEBUG(3,("smb2: network "
549 "misconfiguration, client sent us a "
550 "krb5 ticket and kerberos security "
552 TALLOC_FREE(session);
553 data_blob_free(&secblob_in);
554 TALLOC_FREE(kerb_mech);
555 return NT_STATUS_LOGON_FAILURE;
558 data_blob_free(&secblob_in);
561 if (session->auth_ntlmssp_state == NULL) {
562 status = auth_ntlmssp_prepare(session->sconn->remote_address,
563 &session->auth_ntlmssp_state);
564 if (!NT_STATUS_IS_OK(status)) {
565 data_blob_free(&auth);
566 TALLOC_FREE(session);
570 auth_ntlmssp_want_feature(session->auth_ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
572 status = auth_ntlmssp_start(session->auth_ntlmssp_state);
573 if (!NT_STATUS_IS_OK(status)) {
574 data_blob_free(&auth);
575 TALLOC_FREE(session);
580 status = auth_ntlmssp_update(session->auth_ntlmssp_state,
583 /* If status is NT_STATUS_OK then we need to get the token.
584 * Map to guest is now internal to auth_ntlmssp */
585 if (NT_STATUS_IS_OK(status)) {
586 status = auth_ntlmssp_session_info(session,
587 session->auth_ntlmssp_state,
588 &session->session_info);
591 if (!NT_STATUS_IS_OK(status) &&
592 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
593 TALLOC_FREE(session->auth_ntlmssp_state);
594 data_blob_free(&auth);
595 TALLOC_FREE(session);
599 data_blob_free(&auth);
601 *out_security_buffer = spnego_gen_auth_response(smb2req,
602 &auth_out, status, NULL);
604 if (out_security_buffer->data == NULL) {
605 TALLOC_FREE(session->auth_ntlmssp_state);
606 TALLOC_FREE(session);
607 return NT_STATUS_NO_MEMORY;
610 *out_session_id = session->vuid;
612 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
613 return NT_STATUS_MORE_PROCESSING_REQUIRED;
616 /* We're done - claim the session. */
617 return smbd_smb2_common_ntlmssp_auth_return(session,
625 static NTSTATUS smbd_smb2_raw_ntlmssp_auth(struct smbd_smb2_session *session,
626 struct smbd_smb2_request *smb2req,
627 uint8_t in_security_mode,
628 DATA_BLOB in_security_buffer,
629 uint16_t *out_session_flags,
630 DATA_BLOB *out_security_buffer,
631 uint64_t *out_session_id)
635 if (session->auth_ntlmssp_state == NULL) {
636 status = auth_ntlmssp_prepare(session->sconn->remote_address,
637 &session->auth_ntlmssp_state);
638 if (!NT_STATUS_IS_OK(status)) {
639 TALLOC_FREE(session);
643 auth_ntlmssp_want_feature(session->auth_ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
645 if (session->sconn->use_gensec_hook) {
646 status = auth_generic_start(session->auth_ntlmssp_state, GENSEC_OID_SPNEGO);
648 status = auth_ntlmssp_start(session->auth_ntlmssp_state);
650 if (!NT_STATUS_IS_OK(status)) {
651 TALLOC_FREE(session);
657 status = auth_ntlmssp_update(session->auth_ntlmssp_state,
660 out_security_buffer);
662 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
663 *out_session_id = session->vuid;
667 status = auth_ntlmssp_session_info(session,
668 session->auth_ntlmssp_state,
669 &session->session_info);
671 if (!NT_STATUS_IS_OK(status)) {
672 TALLOC_FREE(session->auth_ntlmssp_state);
673 TALLOC_FREE(session);
676 *out_session_id = session->vuid;
678 return smbd_smb2_common_ntlmssp_auth_return(session,
686 static NTSTATUS smbd_smb2_session_setup(struct smbd_smb2_request *smb2req,
687 uint64_t in_session_id,
688 uint8_t in_security_mode,
689 DATA_BLOB in_security_buffer,
690 uint16_t *out_session_flags,
691 DATA_BLOB *out_security_buffer,
692 uint64_t *out_session_id)
694 struct smbd_smb2_session *session;
696 *out_session_flags = 0;
699 if (in_session_id == 0) {
702 /* create a new session */
703 session = talloc_zero(smb2req->sconn, struct smbd_smb2_session);
704 if (session == NULL) {
705 return NT_STATUS_NO_MEMORY;
707 session->status = NT_STATUS_MORE_PROCESSING_REQUIRED;
708 id = idr_get_new_random(smb2req->sconn->smb2.sessions.idtree,
710 smb2req->sconn->smb2.sessions.limit);
712 return NT_STATUS_INSUFFICIENT_RESOURCES;
716 session->tcons.idtree = idr_init(session);
717 if (session->tcons.idtree == NULL) {
718 return NT_STATUS_NO_MEMORY;
720 session->tcons.limit = 0x0000FFFE;
721 session->tcons.list = NULL;
723 DLIST_ADD_END(smb2req->sconn->smb2.sessions.list, session,
724 struct smbd_smb2_session *);
725 session->sconn = smb2req->sconn;
726 talloc_set_destructor(session, smbd_smb2_session_destructor);
730 /* lookup an existing session */
731 p = idr_find(smb2req->sconn->smb2.sessions.idtree, in_session_id);
733 return NT_STATUS_USER_SESSION_DELETED;
735 session = talloc_get_type_abort(p, struct smbd_smb2_session);
738 if (NT_STATUS_IS_OK(session->status)) {
739 return NT_STATUS_REQUEST_NOT_ACCEPTED;
742 /* Handle either raw NTLMSSP or hand off the whole blob to
743 * GENSEC. The processing at this layer is essentially
744 * identical regardless. In particular, both rely only on the
745 * status code (not the contents of the packet) and do not
747 if (session->sconn->use_gensec_hook
748 || ntlmssp_blob_matches_magic(&in_security_buffer)) {
749 return smbd_smb2_raw_ntlmssp_auth(session,
756 } else if (in_security_buffer.data[0] == ASN1_APPLICATION(0)) {
757 return smbd_smb2_spnego_negotiate(session,
764 } else if (in_security_buffer.data[0] == ASN1_CONTEXT(1)) {
765 return smbd_smb2_spnego_auth(session,
774 /* Unknown packet type. */
775 DEBUG(1,("Unknown packet type %u in smb2 sessionsetup\n",
776 (unsigned int)in_security_buffer.data[0] ));
777 TALLOC_FREE(session->auth_ntlmssp_state);
778 TALLOC_FREE(session);
779 return NT_STATUS_LOGON_FAILURE;
782 NTSTATUS smbd_smb2_request_process_logoff(struct smbd_smb2_request *req)
787 status = smbd_smb2_request_verify_sizes(req, 0x04);
788 if (!NT_STATUS_IS_OK(status)) {
789 return smbd_smb2_request_error(req, status);
793 * TODO: cancel all outstanding requests on the session
794 * and delete all tree connections.
796 smbd_smb2_session_destructor(req->session);
798 * we may need to sign the response, so we need to keep
799 * the session until the response is sent to the wire.
801 talloc_steal(req, req->session);
803 outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
804 if (outbody.data == NULL) {
805 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
808 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
809 SSVAL(outbody.data, 0x02, 0); /* reserved */
811 return smbd_smb2_request_done(req, outbody, NULL);