2 Unix SMB/Netbios implementation.
4 handle NLTMSSP, client server side parsing
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8 Copyright (C) Stefan Metzmacher 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/network.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #include "../lib/crypto/crypto.h"
30 #include "auth/gensec/gensec.h"
31 #include "auth/auth.h"
32 #include "param/param.h"
35 * Set a username on an NTLMSSP context - ensures it is talloc()ed
39 static NTSTATUS ntlmssp_set_username(struct ntlmssp_state *ntlmssp_state, const char *user)
42 /* it should be at least "" */
43 DEBUG(1, ("NTLMSSP failed to set username - cannot accept NULL username\n"));
44 return NT_STATUS_INVALID_PARAMETER;
46 ntlmssp_state->user = talloc_strdup(ntlmssp_state, user);
47 if (!ntlmssp_state->user) {
48 return NT_STATUS_NO_MEMORY;
54 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
57 static NTSTATUS ntlmssp_set_domain(struct ntlmssp_state *ntlmssp_state, const char *domain)
59 ntlmssp_state->domain = talloc_strdup(ntlmssp_state, domain);
60 if (!ntlmssp_state->domain) {
61 return NT_STATUS_NO_MEMORY;
67 * Set a workstation on an NTLMSSP context - ensures it is talloc()ed
70 static NTSTATUS ntlmssp_set_workstation(struct ntlmssp_state *ntlmssp_state, const char *workstation)
72 ntlmssp_state->workstation = talloc_strdup(ntlmssp_state, workstation);
73 if (!ntlmssp_state->workstation) {
74 return NT_STATUS_NO_MEMORY;
80 * Determine correct target name flags for reply, given server role
81 * and negotiated flags
83 * @param ntlmssp_state NTLMSSP State
84 * @param neg_flags The flags from the packet
85 * @param chal_flags The flags to be set in the reply packet
86 * @return The 'target name' string.
89 static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
90 uint32_t neg_flags, uint32_t *chal_flags)
92 if (neg_flags & NTLMSSP_REQUEST_TARGET) {
93 *chal_flags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
94 *chal_flags |= NTLMSSP_REQUEST_TARGET;
95 if (ntlmssp_state->server.is_standalone) {
96 *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
97 return ntlmssp_state->server.netbios_name;
99 *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
100 return ntlmssp_state->server.netbios_domain;
110 * Next state function for the Negotiate packet
112 * @param gensec_security GENSEC state
113 * @param out_mem_ctx Memory context for *out
114 * @param in The request, as a DATA_BLOB. reply.data must be NULL
115 * @param out The reply, as an allocated DATA_BLOB, caller to free.
116 * @return Errors or MORE_PROCESSING_REQUIRED if (normal) a reply is required.
119 NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security,
120 TALLOC_CTX *out_mem_ctx,
121 const DATA_BLOB in, DATA_BLOB *out)
123 struct gensec_ntlmssp_context *gensec_ntlmssp =
124 talloc_get_type_abort(gensec_security->private_data,
125 struct gensec_ntlmssp_context);
126 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
127 DATA_BLOB struct_blob;
128 uint32_t neg_flags = 0;
129 uint32_t ntlmssp_command, chal_flags;
131 const char *target_name;
134 /* parse the NTLMSSP packet */
136 file_save("ntlmssp_negotiate.dat", request.data, request.length);
140 if ((in.length < 16) || !msrpc_parse(out_mem_ctx,
145 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse "
146 "NTLMSSP Negotiate of length %u:\n",
147 (unsigned int)in.length ));
148 dump_data(2, in.data, in.length);
149 return NT_STATUS_INVALID_PARAMETER;
151 debug_ntlmssp_flags(neg_flags);
154 ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, ntlmssp_state->allow_lm_key);
156 /* Ask our caller what challenge they would like in the packet */
157 status = ntlmssp_state->get_challenge(ntlmssp_state, cryptkey);
158 if (!NT_STATUS_IS_OK(status)) {
159 DEBUG(1, ("ntlmssp_server_negotiate: backend doesn't give a challenge: %s\n",
164 /* Check if we may set the challenge */
165 if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
166 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
169 /* The flags we send back are not just the negotiated flags,
170 * they are also 'what is in this packet'. Therfore, we
171 * operate on 'chal_flags' from here on
174 chal_flags = ntlmssp_state->neg_flags;
176 /* get the right name to fill in as 'target' */
177 target_name = ntlmssp_target_name(ntlmssp_state,
178 neg_flags, &chal_flags);
179 if (target_name == NULL)
180 return NT_STATUS_INVALID_PARAMETER;
182 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
183 ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
185 /* This creates the 'blob' of names that appears at the end of the packet */
186 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
187 msrpc_gen(out_mem_ctx,
188 &struct_blob, "aaaaa",
189 MsvAvNbDomainName, target_name,
190 MsvAvNbComputerName, ntlmssp_state->server.netbios_name,
191 MsvAvDnsDomainName, ntlmssp_state->server.dns_domain,
192 MsvAvDnsComputerName, ntlmssp_state->server.dns_name,
195 struct_blob = data_blob(NULL, 0);
199 /* Marshal the packet in the right format, be it unicode or ASCII */
200 const char *gen_string;
201 if (ntlmssp_state->unicode) {
202 gen_string = "CdUdbddB";
204 gen_string = "CdAdbddB";
207 msrpc_gen(out_mem_ctx,
215 struct_blob.data, struct_blob.length);
218 ntlmssp_state->expected_state = NTLMSSP_AUTH;
220 return NT_STATUS_MORE_PROCESSING_REQUIRED;
223 struct ntlmssp_server_auth_state {
224 DATA_BLOB user_session_key;
225 DATA_BLOB lm_session_key;
226 /* internal variables used by KEY_EXCH (client-supplied user session key */
227 DATA_BLOB encrypted_session_key;
229 /* internal variables used by NTLM2 */
230 uint8_t session_nonce[16];
234 * Next state function for the Authenticate packet
236 * @param ntlmssp_state NTLMSSP State
237 * @param request The request, as a DATA_BLOB
238 * @return Errors or NT_STATUS_OK.
241 static NTSTATUS ntlmssp_server_preauth(struct ntlmssp_state *ntlmssp_state,
242 struct ntlmssp_server_auth_state *state,
243 const DATA_BLOB request)
245 uint32_t ntlmssp_command, auth_flags;
248 uint8_t session_nonce_hash[16];
250 const char *parse_string;
253 char *workstation = NULL;
256 file_save("ntlmssp_auth.dat", request.data, request.length);
259 if (ntlmssp_state->unicode) {
260 parse_string = "CdBBUUUBd";
262 parse_string = "CdBBAAABd";
266 data_blob_free(&ntlmssp_state->session_key);
267 data_blob_free(&ntlmssp_state->lm_resp);
268 data_blob_free(&ntlmssp_state->nt_resp);
270 ntlmssp_state->user = NULL;
271 ntlmssp_state->domain = NULL;
272 ntlmssp_state->workstation = NULL;
274 /* now the NTLMSSP encoded auth hashes */
275 if (!msrpc_parse(ntlmssp_state,
276 &request, parse_string,
279 &ntlmssp_state->lm_resp,
280 &ntlmssp_state->nt_resp,
284 &state->encrypted_session_key,
286 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
287 dump_data(10, request.data, request.length);
290 data_blob_free(&state->encrypted_session_key);
293 /* Try again with a shorter string (Win9X truncates this packet) */
294 if (ntlmssp_state->unicode) {
295 parse_string = "CdBBUUU";
297 parse_string = "CdBBAAA";
300 /* now the NTLMSSP encoded auth hashes */
301 if (!msrpc_parse(ntlmssp_state,
302 &request, parse_string,
305 &ntlmssp_state->lm_resp,
306 &ntlmssp_state->nt_resp,
310 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP:\n"));
311 dump_data(2, request.data, request.length);
313 return NT_STATUS_INVALID_PARAMETER;
317 talloc_steal(state, state->encrypted_session_key.data);
320 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, ntlmssp_state->allow_lm_key);
322 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(ntlmssp_state, domain))) {
326 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(ntlmssp_state, user))) {
330 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(ntlmssp_state, workstation))) {
334 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
335 ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
338 file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
339 file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
342 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
345 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
347 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
348 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
349 struct MD5Context md5_session_nonce_ctx;
350 SMB_ASSERT(ntlmssp_state->internal_chal.data
351 && ntlmssp_state->internal_chal.length == 8);
353 state->doing_ntlm2 = true;
355 memcpy(state->session_nonce, ntlmssp_state->internal_chal.data, 8);
356 memcpy(&state->session_nonce[8], ntlmssp_state->lm_resp.data, 8);
358 MD5Init(&md5_session_nonce_ctx);
359 MD5Update(&md5_session_nonce_ctx, state->session_nonce, 16);
360 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
362 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state,
363 session_nonce_hash, 8);
365 /* LM response is no longer useful, zero it out */
366 data_blob_free(&ntlmssp_state->lm_resp);
368 /* We changed the effective challenge - set it */
369 if (!NT_STATUS_IS_OK(nt_status =
370 ntlmssp_state->set_challenge(ntlmssp_state,
371 &ntlmssp_state->chal))) {
375 /* LM Key is incompatible... */
376 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
383 * Next state function for the Authenticate packet
384 * (after authentication - figures out the session keys etc)
386 * @param ntlmssp_state NTLMSSP State
387 * @return Errors or NT_STATUS_OK.
390 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security,
391 struct ntlmssp_server_auth_state *state)
393 struct gensec_ntlmssp_context *gensec_ntlmssp =
394 talloc_get_type_abort(gensec_security->private_data,
395 struct gensec_ntlmssp_context);
396 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
397 DATA_BLOB *user_session_key = &state->user_session_key;
398 DATA_BLOB *lm_session_key = &state->lm_session_key;
400 DATA_BLOB session_key = data_blob(NULL, 0);
402 if (!(gensec_security->want_features
403 & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL|GENSEC_FEATURE_SESSION_KEY))) {
407 if (user_session_key)
408 dump_data_pw("USER session key:\n", user_session_key->data, user_session_key->length);
411 dump_data_pw("LM first-8:\n", lm_session_key->data, lm_session_key->length);
413 /* Handle the different session key derivation for NTLM2 */
414 if (state->doing_ntlm2) {
415 if (user_session_key && user_session_key->data && user_session_key->length == 16) {
416 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
417 hmac_md5(user_session_key->data, state->session_nonce,
418 sizeof(state->session_nonce), session_key.data);
419 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
420 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
423 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
424 session_key = data_blob(NULL, 0);
426 } else if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
427 /* Ensure we can never get here on NTLMv2 */
428 && (ntlmssp_state->nt_resp.length == 0 || ntlmssp_state->nt_resp.length == 24)) {
430 if (lm_session_key && lm_session_key->data && lm_session_key->length >= 8) {
431 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
432 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
433 SMBsesskeygen_lm_sess_key(lm_session_key->data, ntlmssp_state->lm_resp.data,
435 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
436 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
439 /* When there is no LM response, just use zeros */
440 static const uint8_t zeros[24];
441 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
442 SMBsesskeygen_lm_sess_key(zeros, zeros,
444 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
445 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
448 /* LM Key not selected */
449 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
451 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
452 session_key = data_blob(NULL, 0);
455 } else if (user_session_key && user_session_key->data) {
456 session_key = data_blob_talloc(ntlmssp_state, user_session_key->data, user_session_key->length);
457 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
458 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
460 /* LM Key not selected */
461 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
463 } else if (lm_session_key && lm_session_key->data) {
464 /* Very weird to have LM key, but no user session key, but anyway.. */
465 session_key = data_blob_talloc(ntlmssp_state, lm_session_key->data, lm_session_key->length);
466 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
467 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
469 /* LM Key not selected */
470 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
473 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
474 session_key = data_blob(NULL, 0);
476 /* LM Key not selected */
477 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
480 /* With KEY_EXCH, the client supplies the proposed session key,
481 but encrypts it with the long-term key */
482 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
483 if (!state->encrypted_session_key.data
484 || state->encrypted_session_key.length != 16) {
485 data_blob_free(&state->encrypted_session_key);
486 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
487 (unsigned)state->encrypted_session_key.length));
488 return NT_STATUS_INVALID_PARAMETER;
489 } else if (!session_key.data || session_key.length != 16) {
490 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
491 (unsigned)session_key.length));
492 ntlmssp_state->session_key = session_key;
494 dump_data_pw("KEY_EXCH session key (enc):\n",
495 state->encrypted_session_key.data,
496 state->encrypted_session_key.length);
497 arcfour_crypt(state->encrypted_session_key.data,
499 state->encrypted_session_key.length);
500 ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state,
501 state->encrypted_session_key.data,
502 state->encrypted_session_key.length);
503 dump_data_pw("KEY_EXCH session key:\n",
504 state->encrypted_session_key.data,
505 state->encrypted_session_key.length);
506 talloc_free(session_key.data);
509 ntlmssp_state->session_key = session_key;
512 if ((gensec_security->want_features & GENSEC_FEATURE_SIGN)
513 || (gensec_security->want_features & GENSEC_FEATURE_SEAL)) {
514 nt_status = ntlmssp_sign_init(ntlmssp_state);
516 nt_status = NT_STATUS_OK;
519 ntlmssp_state->expected_state = NTLMSSP_DONE;
526 * Next state function for the Authenticate packet
528 * @param gensec_security GENSEC state
529 * @param out_mem_ctx Memory context for *out
530 * @param in The request, as a DATA_BLOB. reply.data must be NULL
531 * @param out The reply, as an allocated DATA_BLOB, caller to free.
532 * @return Errors or NT_STATUS_OK if authentication sucessful
535 NTSTATUS ntlmssp_server_auth(struct gensec_security *gensec_security,
536 TALLOC_CTX *out_mem_ctx,
537 const DATA_BLOB in, DATA_BLOB *out)
539 struct gensec_ntlmssp_context *gensec_ntlmssp =
540 talloc_get_type_abort(gensec_security->private_data,
541 struct gensec_ntlmssp_context);
542 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
543 struct ntlmssp_server_auth_state *state;
546 /* zero the outbound NTLMSSP packet */
547 *out = data_blob_null;
549 state = talloc_zero(ntlmssp_state, struct ntlmssp_server_auth_state);
551 return NT_STATUS_NO_MEMORY;
554 nt_status = ntlmssp_server_preauth(ntlmssp_state, state, in);
555 if (!NT_STATUS_IS_OK(nt_status)) {
561 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
562 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
563 * smb.conf file) and no NTLMv2 response was sent then the password check
564 * will fail here. JRA.
567 /* Finally, actually ask if the password is OK */
568 nt_status = ntlmssp_state->check_password(ntlmssp_state,
569 &state->user_session_key,
570 &state->lm_session_key);
571 if (!NT_STATUS_IS_OK(nt_status)) {
576 nt_status = ntlmssp_server_postauth(gensec_security, state);
577 if (!NT_STATUS_IS_OK(nt_status)) {
587 * Return the challenge as determined by the authentication subsystem
588 * @return an 8 byte random challenge
591 static NTSTATUS auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state,
594 struct gensec_ntlmssp_context *gensec_ntlmssp =
595 talloc_get_type_abort(ntlmssp_state->callback_private,
596 struct gensec_ntlmssp_context);
597 struct auth_context *auth_context = gensec_ntlmssp->auth_context;
600 status = auth_context->get_challenge(auth_context, chal);
601 if (!NT_STATUS_IS_OK(status)) {
602 DEBUG(1, ("auth_ntlmssp_get_challenge: failed to get challenge: %s\n",
611 * Some authentication methods 'fix' the challenge, so we may not be able to set it
613 * @return If the effective challenge used by the auth subsystem may be modified
615 static bool auth_ntlmssp_may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
617 struct gensec_ntlmssp_context *gensec_ntlmssp =
618 talloc_get_type_abort(ntlmssp_state->callback_private,
619 struct gensec_ntlmssp_context);
620 struct auth_context *auth_context = gensec_ntlmssp->auth_context;
622 return auth_context->challenge_may_be_modified(auth_context);
626 * NTLM2 authentication modifies the effective challenge,
627 * @param challenge The new challenge value
629 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
631 struct gensec_ntlmssp_context *gensec_ntlmssp =
632 talloc_get_type_abort(ntlmssp_state->callback_private,
633 struct gensec_ntlmssp_context);
634 struct auth_context *auth_context = gensec_ntlmssp->auth_context;
638 if (challenge->length != 8) {
639 return NT_STATUS_INVALID_PARAMETER;
642 chal = challenge->data;
644 nt_status = auth_context->set_challenge(auth_context,
646 "NTLMSSP callback (NTLM2)");
652 * Check the password on an NTLMSSP login.
654 * Return the session keys used on the connection.
657 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state,
658 DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
660 struct gensec_ntlmssp_context *gensec_ntlmssp =
661 talloc_get_type_abort(ntlmssp_state->callback_private,
662 struct gensec_ntlmssp_context);
663 struct auth_context *auth_context = gensec_ntlmssp->auth_context;
665 struct auth_usersupplied_info *user_info;
667 user_info = talloc(ntlmssp_state, struct auth_usersupplied_info);
669 return NT_STATUS_NO_MEMORY;
672 user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
673 user_info->flags = 0;
674 user_info->mapped_state = false;
675 user_info->client.account_name = ntlmssp_state->user;
676 user_info->client.domain_name = ntlmssp_state->domain;
677 user_info->workstation_name = ntlmssp_state->workstation;
678 user_info->remote_host = gensec_get_remote_address(gensec_ntlmssp->gensec_security);
680 user_info->password_state = AUTH_PASSWORD_RESPONSE;
681 user_info->password.response.lanman = ntlmssp_state->lm_resp;
682 user_info->password.response.lanman.data = talloc_steal(user_info, ntlmssp_state->lm_resp.data);
683 user_info->password.response.nt = ntlmssp_state->nt_resp;
684 user_info->password.response.nt.data = talloc_steal(user_info, ntlmssp_state->nt_resp.data);
686 nt_status = auth_context->check_password(auth_context,
689 &gensec_ntlmssp->server_info);
690 talloc_free(user_info);
691 NT_STATUS_NOT_OK_RETURN(nt_status);
693 if (gensec_ntlmssp->server_info->user_session_key.length) {
694 DEBUG(10, ("Got NT session key of length %u\n",
695 (unsigned)gensec_ntlmssp->server_info->user_session_key.length));
696 *user_session_key = gensec_ntlmssp->server_info->user_session_key;
698 if (gensec_ntlmssp->server_info->lm_session_key.length) {
699 DEBUG(10, ("Got LM session key of length %u\n",
700 (unsigned)gensec_ntlmssp->server_info->lm_session_key.length));
701 *lm_session_key = gensec_ntlmssp->server_info->lm_session_key;
707 * Return the credentials of a logged on user, including session keys
710 * Only valid after a successful authentication
712 * May only be called once per authentication.
716 NTSTATUS gensec_ntlmssp_session_info(struct gensec_security *gensec_security,
717 struct auth_session_info **session_info)
720 struct gensec_ntlmssp_context *gensec_ntlmssp =
721 talloc_get_type_abort(gensec_security->private_data,
722 struct gensec_ntlmssp_context);
723 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
725 nt_status = auth_generate_session_info(ntlmssp_state,
726 gensec_security->event_ctx,
727 gensec_security->settings->lp_ctx,
728 gensec_ntlmssp->server_info,
730 NT_STATUS_NOT_OK_RETURN(nt_status);
732 (*session_info)->session_key = data_blob_talloc(*session_info,
733 ntlmssp_state->session_key.data,
734 ntlmssp_state->session_key.length);
740 * Start NTLMSSP on the server side
743 NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
746 struct ntlmssp_state *ntlmssp_state;
747 struct gensec_ntlmssp_context *gensec_ntlmssp;
749 nt_status = gensec_ntlmssp_start(gensec_security);
750 NT_STATUS_NOT_OK_RETURN(nt_status);
752 gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
753 struct gensec_ntlmssp_context);
754 ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
756 ntlmssp_state->role = NTLMSSP_SERVER;
758 ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
760 ntlmssp_state->allow_lm_key = (lp_lanman_auth(gensec_security->settings->lp_ctx)
761 && gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "allow_lm_key", false));
763 ntlmssp_state->neg_flags =
764 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_VERSION;
766 ntlmssp_state->lm_resp = data_blob(NULL, 0);
767 ntlmssp_state->nt_resp = data_blob(NULL, 0);
769 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "128bit", true)) {
770 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
773 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "56bit", true)) {
774 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
777 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "keyexchange", true)) {
778 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
781 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "alwayssign", true)) {
782 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
785 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "ntlm2", true)) {
786 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
789 if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
790 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
792 if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
793 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
796 gensec_ntlmssp->auth_context = gensec_security->auth_context;
798 ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
799 ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
800 ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
801 ntlmssp_state->check_password = auth_ntlmssp_check_password;
802 if (lp_server_role(gensec_security->settings->lp_ctx) == ROLE_STANDALONE) {
803 ntlmssp_state->server.is_standalone = true;
805 ntlmssp_state->server.is_standalone = false;
808 ntlmssp_state->server.netbios_name = lp_netbios_name(gensec_security->settings->lp_ctx);
810 ntlmssp_state->server.netbios_domain = lp_workgroup(gensec_security->settings->lp_ctx);
813 char dnsdomname[MAXHOSTNAMELEN], dnsname[MAXHOSTNAMELEN];
815 /* Find out the DNS domain name */
816 dnsdomname[0] = '\0';
817 safe_strcpy(dnsdomname, lp_dnsdomain(gensec_security->settings->lp_ctx), sizeof(dnsdomname) - 1);
819 /* Find out the DNS host name */
820 safe_strcpy(dnsname, ntlmssp_state->server.netbios_name, sizeof(dnsname) - 1);
821 if (dnsdomname[0] != '\0') {
822 safe_strcat(dnsname, ".", sizeof(dnsname) - 1);
823 safe_strcat(dnsname, dnsdomname, sizeof(dnsname) - 1);
827 ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state,
829 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_name);
831 ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state,
833 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_domain);