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 "auth/ntlmssp/ntlmssp.h"
26 #include "auth/ntlmssp/msrpc_parse.h"
27 #include "lib/crypto/crypto.h"
28 #include "system/filesys.h"
29 #include "libcli/auth/libcli_auth.h"
30 #include "auth/credentials/credentials.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/auth.h"
33 #include "auth/ntlm/auth_proto.h"
34 #include "param/param.h"
35 #include "auth/session_proto.h"
38 * Set a username on an NTLMSSP context - ensures it is talloc()ed
42 static NTSTATUS ntlmssp_set_username(struct gensec_ntlmssp_state *gensec_ntlmssp_state, const char *user)
45 /* it should be at least "" */
46 DEBUG(1, ("NTLMSSP failed to set username - cannot accept NULL username\n"));
47 return NT_STATUS_INVALID_PARAMETER;
49 gensec_ntlmssp_state->user = talloc_strdup(gensec_ntlmssp_state, user);
50 if (!gensec_ntlmssp_state->user) {
51 return NT_STATUS_NO_MEMORY;
57 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
60 static NTSTATUS ntlmssp_set_domain(struct gensec_ntlmssp_state *gensec_ntlmssp_state, const char *domain)
62 gensec_ntlmssp_state->domain = talloc_strdup(gensec_ntlmssp_state, domain);
63 if (!gensec_ntlmssp_state->domain) {
64 return NT_STATUS_NO_MEMORY;
70 * Set a workstation on an NTLMSSP context - ensures it is talloc()ed
73 static NTSTATUS ntlmssp_set_workstation(struct gensec_ntlmssp_state *gensec_ntlmssp_state, const char *workstation)
75 gensec_ntlmssp_state->workstation = talloc_strdup(gensec_ntlmssp_state, workstation);
76 if (!gensec_ntlmssp_state->workstation) {
77 return NT_STATUS_NO_MEMORY;
83 * Determine correct target name flags for reply, given server role
84 * and negotiated flags
86 * @param gensec_ntlmssp_state NTLMSSP State
87 * @param neg_flags The flags from the packet
88 * @param chal_flags The flags to be set in the reply packet
89 * @return The 'target name' string.
92 static const char *ntlmssp_target_name(struct gensec_ntlmssp_state *gensec_ntlmssp_state,
93 uint32_t neg_flags, uint32_t *chal_flags)
95 if (neg_flags & NTLMSSP_REQUEST_TARGET) {
96 *chal_flags |= NTLMSSP_CHAL_TARGET_INFO;
97 *chal_flags |= NTLMSSP_REQUEST_TARGET;
98 if (gensec_ntlmssp_state->server_role == ROLE_STANDALONE) {
99 *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
100 return gensec_ntlmssp_state->server_name;
102 *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
103 return gensec_ntlmssp_state->domain;
113 * Next state function for the Negotiate packet
115 * @param gensec_security GENSEC state
116 * @param out_mem_ctx Memory context for *out
117 * @param in The request, as a DATA_BLOB. reply.data must be NULL
118 * @param out The reply, as an allocated DATA_BLOB, caller to free.
119 * @return Errors or MORE_PROCESSING_REQUIRED if (normal) a reply is required.
122 NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security,
123 TALLOC_CTX *out_mem_ctx,
124 const DATA_BLOB in, DATA_BLOB *out)
126 struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
127 DATA_BLOB struct_blob;
128 char dnsname[MAXHOSTNAMELEN], dnsdomname[MAXHOSTNAMELEN];
130 uint32_t neg_flags = 0;
131 uint32_t ntlmssp_command, chal_flags;
132 const uint8_t *cryptkey;
133 const char *target_name;
135 /* parse the NTLMSSP packet */
137 file_save("ntlmssp_negotiate.dat", request.data, request.length);
141 if ((in.length < 16) || !msrpc_parse(out_mem_ctx,
142 lp_iconv_convenience(gensec_security->lp_ctx),
147 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse "
148 "NTLMSSP Negotiate of length %u:\n",
149 (unsigned int)in.length ));
150 dump_data(2, in.data, in.length);
151 return NT_STATUS_INVALID_PARAMETER;
153 debug_ntlmssp_flags(neg_flags);
156 ntlmssp_handle_neg_flags(gensec_ntlmssp_state, neg_flags, gensec_ntlmssp_state->allow_lm_key);
158 /* Ask our caller what challenge they would like in the packet */
159 cryptkey = gensec_ntlmssp_state->get_challenge(gensec_ntlmssp_state);
161 DEBUG(1, ("ntlmssp_server_negotiate: backend doesn't give a challenge\n"));
162 return NT_STATUS_INTERNAL_ERROR;
165 /* Check if we may set the challenge */
166 if (!gensec_ntlmssp_state->may_set_challenge(gensec_ntlmssp_state)) {
167 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
170 /* The flags we send back are not just the negotiated flags,
171 * they are also 'what is in this packet'. Therfore, we
172 * operate on 'chal_flags' from here on
175 chal_flags = gensec_ntlmssp_state->neg_flags;
177 /* get the right name to fill in as 'target' */
178 target_name = ntlmssp_target_name(gensec_ntlmssp_state,
179 neg_flags, &chal_flags);
180 if (target_name == NULL)
181 return NT_STATUS_INVALID_PARAMETER;
183 gensec_ntlmssp_state->chal = data_blob_talloc(gensec_ntlmssp_state, cryptkey, 8);
184 gensec_ntlmssp_state->internal_chal = data_blob_talloc(gensec_ntlmssp_state, cryptkey, 8);
187 if (gethostname(dnsname, sizeof(dnsname)) == -1) {
188 DEBUG(0,("gethostname failed\n"));
189 return NT_STATUS_UNSUCCESSFUL;
192 /* This should be a 'netbios domain -> DNS domain' mapping */
193 p = strchr(dnsname, '.');
195 safe_strcpy(dnsdomname, p+1, sizeof(dnsdomname));
196 strlower_m(dnsdomname);
198 dnsdomname[0] = '\0';
201 /* This creates the 'blob' of names that appears at the end of the packet */
202 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO)
204 const char *target_name_dns = "";
205 if (chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN) {
206 target_name_dns = dnsdomname;
207 } else if (chal_flags |= NTLMSSP_TARGET_TYPE_SERVER) {
208 target_name_dns = dnsname;
211 msrpc_gen(out_mem_ctx,
212 lp_iconv_convenience(gensec_security->lp_ctx),
213 &struct_blob, "aaaaa",
214 NTLMSSP_NAME_TYPE_DOMAIN, target_name,
215 NTLMSSP_NAME_TYPE_SERVER, gensec_ntlmssp_state->server_name,
216 NTLMSSP_NAME_TYPE_DOMAIN_DNS, dnsdomname,
217 NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
220 struct_blob = data_blob(NULL, 0);
224 /* Marshel the packet in the right format, be it unicode or ASCII */
225 const char *gen_string;
226 if (gensec_ntlmssp_state->unicode) {
227 gen_string = "CdUdbddB";
229 gen_string = "CdAdbddB";
232 msrpc_gen(out_mem_ctx,
233 lp_iconv_convenience(gensec_security->lp_ctx),
241 struct_blob.data, struct_blob.length);
244 gensec_ntlmssp_state->expected_state = NTLMSSP_AUTH;
246 return NT_STATUS_MORE_PROCESSING_REQUIRED;
250 * Next state function for the Authenticate packet
252 * @param gensec_ntlmssp_state NTLMSSP State
253 * @param request The request, as a DATA_BLOB
254 * @return Errors or NT_STATUS_OK.
257 static NTSTATUS ntlmssp_server_preauth(struct gensec_ntlmssp_state *gensec_ntlmssp_state,
258 const DATA_BLOB request)
260 uint32_t ntlmssp_command, auth_flags;
263 uint8_t session_nonce_hash[16];
265 const char *parse_string;
268 char *workstation = NULL;
271 file_save("ntlmssp_auth.dat", request.data, request.length);
274 if (gensec_ntlmssp_state->unicode) {
275 parse_string = "CdBBUUUBd";
277 parse_string = "CdBBAAABd";
281 data_blob_free(&gensec_ntlmssp_state->lm_resp);
282 data_blob_free(&gensec_ntlmssp_state->nt_resp);
283 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
285 gensec_ntlmssp_state->user = NULL;
286 gensec_ntlmssp_state->domain = NULL;
287 gensec_ntlmssp_state->workstation = NULL;
289 /* now the NTLMSSP encoded auth hashes */
290 if (!msrpc_parse(gensec_ntlmssp_state,
291 lp_iconv_convenience(gensec_ntlmssp_state->gensec_security->lp_ctx),
292 &request, parse_string,
295 &gensec_ntlmssp_state->lm_resp,
296 &gensec_ntlmssp_state->nt_resp,
300 &gensec_ntlmssp_state->encrypted_session_key,
302 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
303 dump_data(10, request.data, request.length);
306 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
309 /* Try again with a shorter string (Win9X truncates this packet) */
310 if (gensec_ntlmssp_state->unicode) {
311 parse_string = "CdBBUUU";
313 parse_string = "CdBBAAA";
316 /* now the NTLMSSP encoded auth hashes */
317 if (!msrpc_parse(gensec_ntlmssp_state,
318 lp_iconv_convenience(gensec_ntlmssp_state->gensec_security->lp_ctx),
319 &request, parse_string,
322 &gensec_ntlmssp_state->lm_resp,
323 &gensec_ntlmssp_state->nt_resp,
327 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP:\n"));
328 dump_data(2, request.data, request.length);
330 return NT_STATUS_INVALID_PARAMETER;
335 ntlmssp_handle_neg_flags(gensec_ntlmssp_state, auth_flags, gensec_ntlmssp_state->allow_lm_key);
337 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(gensec_ntlmssp_state, domain))) {
339 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
343 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(gensec_ntlmssp_state, user))) {
345 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
349 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(gensec_ntlmssp_state, workstation))) {
351 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
355 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
356 gensec_ntlmssp_state->user, gensec_ntlmssp_state->domain, gensec_ntlmssp_state->workstation, (unsigned long)gensec_ntlmssp_state->lm_resp.length, (unsigned long)gensec_ntlmssp_state->nt_resp.length));
359 file_save("nthash1.dat", &gensec_ntlmssp_state->nt_resp.data, &gensec_ntlmssp_state->nt_resp.length);
360 file_save("lmhash1.dat", &gensec_ntlmssp_state->lm_resp.data, &gensec_ntlmssp_state->lm_resp.length);
363 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
366 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
368 if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
369 if (gensec_ntlmssp_state->nt_resp.length == 24 && gensec_ntlmssp_state->lm_resp.length == 24) {
370 struct MD5Context md5_session_nonce_ctx;
371 SMB_ASSERT(gensec_ntlmssp_state->internal_chal.data
372 && gensec_ntlmssp_state->internal_chal.length == 8);
374 gensec_ntlmssp_state->doing_ntlm2 = true;
376 memcpy(gensec_ntlmssp_state->crypt.ntlm2.session_nonce, gensec_ntlmssp_state->internal_chal.data, 8);
377 memcpy(&gensec_ntlmssp_state->crypt.ntlm2.session_nonce[8], gensec_ntlmssp_state->lm_resp.data, 8);
379 MD5Init(&md5_session_nonce_ctx);
380 MD5Update(&md5_session_nonce_ctx, gensec_ntlmssp_state->crypt.ntlm2.session_nonce, 16);
381 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
383 gensec_ntlmssp_state->chal = data_blob_talloc(gensec_ntlmssp_state,
384 session_nonce_hash, 8);
386 /* LM response is no longer useful, zero it out */
387 data_blob_free(&gensec_ntlmssp_state->lm_resp);
389 /* We changed the effective challenge - set it */
390 if (!NT_STATUS_IS_OK(nt_status =
391 gensec_ntlmssp_state->set_challenge(gensec_ntlmssp_state,
392 &gensec_ntlmssp_state->chal))) {
394 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
398 /* LM Key is incompatible... */
399 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
406 * Next state function for the Authenticate packet
407 * (after authentication - figures out the session keys etc)
409 * @param gensec_ntlmssp_state NTLMSSP State
410 * @return Errors or NT_STATUS_OK.
413 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security,
414 DATA_BLOB *user_session_key,
415 DATA_BLOB *lm_session_key)
417 struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
419 DATA_BLOB session_key = data_blob(NULL, 0);
421 if (user_session_key)
422 dump_data_pw("USER session key:\n", user_session_key->data, user_session_key->length);
425 dump_data_pw("LM first-8:\n", lm_session_key->data, lm_session_key->length);
427 /* Handle the different session key derivation for NTLM2 */
428 if (gensec_ntlmssp_state->doing_ntlm2) {
429 if (user_session_key && user_session_key->data && user_session_key->length == 16) {
430 session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
431 hmac_md5(user_session_key->data, gensec_ntlmssp_state->crypt.ntlm2.session_nonce,
432 sizeof(gensec_ntlmssp_state->crypt.ntlm2.session_nonce), session_key.data);
433 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
434 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
437 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
438 session_key = data_blob(NULL, 0);
440 } else if ((gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
441 /* Ensure we can never get here on NTLMv2 */
442 && (gensec_ntlmssp_state->nt_resp.length == 0 || gensec_ntlmssp_state->nt_resp.length == 24)) {
444 if (lm_session_key && lm_session_key->data && lm_session_key->length >= 8) {
445 if (gensec_ntlmssp_state->lm_resp.data && gensec_ntlmssp_state->lm_resp.length == 24) {
446 session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
447 SMBsesskeygen_lm_sess_key(lm_session_key->data, gensec_ntlmssp_state->lm_resp.data,
449 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
450 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
453 /* When there is no LM response, just use zeros */
454 static const uint8_t zeros[24];
455 session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
456 SMBsesskeygen_lm_sess_key(zeros, zeros,
458 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
459 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
462 /* LM Key not selected */
463 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
465 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
466 session_key = data_blob(NULL, 0);
469 } else if (user_session_key && user_session_key->data) {
470 session_key = *user_session_key;
471 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
472 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
474 /* LM Key not selected */
475 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
477 } else if (lm_session_key && lm_session_key->data) {
478 /* Very weird to have LM key, but no user session key, but anyway.. */
479 session_key = *lm_session_key;
480 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
481 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
483 /* LM Key not selected */
484 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
487 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
488 session_key = data_blob(NULL, 0);
490 /* LM Key not selected */
491 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
494 /* With KEY_EXCH, the client supplies the proposed session key,
495 but encrypts it with the long-term key */
496 if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
497 if (!gensec_ntlmssp_state->encrypted_session_key.data
498 || gensec_ntlmssp_state->encrypted_session_key.length != 16) {
499 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
500 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
501 (unsigned)gensec_ntlmssp_state->encrypted_session_key.length));
502 return NT_STATUS_INVALID_PARAMETER;
503 } else if (!session_key.data || session_key.length != 16) {
504 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
505 (unsigned)session_key.length));
506 gensec_ntlmssp_state->session_key = session_key;
508 dump_data_pw("KEY_EXCH session key (enc):\n",
509 gensec_ntlmssp_state->encrypted_session_key.data,
510 gensec_ntlmssp_state->encrypted_session_key.length);
511 arcfour_crypt(gensec_ntlmssp_state->encrypted_session_key.data,
513 gensec_ntlmssp_state->encrypted_session_key.length);
514 gensec_ntlmssp_state->session_key = data_blob_talloc(gensec_ntlmssp_state,
515 gensec_ntlmssp_state->encrypted_session_key.data,
516 gensec_ntlmssp_state->encrypted_session_key.length);
517 dump_data_pw("KEY_EXCH session key:\n", gensec_ntlmssp_state->encrypted_session_key.data,
518 gensec_ntlmssp_state->encrypted_session_key.length);
521 gensec_ntlmssp_state->session_key = session_key;
524 /* keep the session key around on the new context */
525 talloc_steal(gensec_ntlmssp_state, session_key.data);
527 if ((gensec_security->want_features & GENSEC_FEATURE_SIGN)
528 || (gensec_security->want_features & GENSEC_FEATURE_SEAL)) {
529 nt_status = ntlmssp_sign_init(gensec_ntlmssp_state);
531 nt_status = NT_STATUS_OK;
534 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
536 /* allow arbitarily many authentications, but watch that this will cause a
537 memory leak, until the gensec_ntlmssp_state is shutdown
540 if (gensec_ntlmssp_state->server_multiple_authentications) {
541 gensec_ntlmssp_state->expected_state = NTLMSSP_AUTH;
543 gensec_ntlmssp_state->expected_state = NTLMSSP_DONE;
551 * Next state function for the Authenticate packet
553 * @param gensec_security GENSEC state
554 * @param out_mem_ctx Memory context for *out
555 * @param in The request, as a DATA_BLOB. reply.data must be NULL
556 * @param out The reply, as an allocated DATA_BLOB, caller to free.
557 * @return Errors or NT_STATUS_OK if authentication sucessful
560 NTSTATUS ntlmssp_server_auth(struct gensec_security *gensec_security,
561 TALLOC_CTX *out_mem_ctx,
562 const DATA_BLOB in, DATA_BLOB *out)
564 struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
565 DATA_BLOB user_session_key = data_blob(NULL, 0);
566 DATA_BLOB lm_session_key = data_blob(NULL, 0);
569 TALLOC_CTX *mem_ctx = talloc_new(out_mem_ctx);
571 return NT_STATUS_NO_MEMORY;
574 /* zero the outbound NTLMSSP packet */
575 *out = data_blob_talloc(out_mem_ctx, NULL, 0);
577 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_server_preauth(gensec_ntlmssp_state, in))) {
578 talloc_free(mem_ctx);
583 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
584 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
585 * smb.conf file) and no NTLMv2 response was sent then the password check
586 * will fail here. JRA.
589 /* Finally, actually ask if the password is OK */
591 if (!NT_STATUS_IS_OK(nt_status = gensec_ntlmssp_state->check_password(gensec_ntlmssp_state, mem_ctx,
592 &user_session_key, &lm_session_key))) {
593 talloc_free(mem_ctx);
597 if (gensec_security->want_features
598 & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL|GENSEC_FEATURE_SESSION_KEY)) {
599 nt_status = ntlmssp_server_postauth(gensec_security, &user_session_key, &lm_session_key);
600 talloc_free(mem_ctx);
603 gensec_ntlmssp_state->session_key = data_blob(NULL, 0);
604 talloc_free(mem_ctx);
610 * Return the challenge as determined by the authentication subsystem
611 * @return an 8 byte random challenge
614 static const uint8_t *auth_ntlmssp_get_challenge(const struct gensec_ntlmssp_state *gensec_ntlmssp_state)
619 status = auth_get_challenge(gensec_ntlmssp_state->auth_context, &chal);
620 if (!NT_STATUS_IS_OK(status)) {
621 DEBUG(1, ("auth_ntlmssp_get_challenge: failed to get challenge: %s\n",
630 * Some authentication methods 'fix' the challenge, so we may not be able to set it
632 * @return If the effective challenge used by the auth subsystem may be modified
634 static bool auth_ntlmssp_may_set_challenge(const struct gensec_ntlmssp_state *gensec_ntlmssp_state)
636 return auth_challenge_may_be_modified(gensec_ntlmssp_state->auth_context);
640 * NTLM2 authentication modifies the effective challenge,
641 * @param challenge The new challenge value
643 static NTSTATUS auth_ntlmssp_set_challenge(struct gensec_ntlmssp_state *gensec_ntlmssp_state, DATA_BLOB *challenge)
646 struct auth_context *auth_context = gensec_ntlmssp_state->auth_context;
649 if (challenge->length != 8) {
650 return NT_STATUS_INVALID_PARAMETER;
653 chal = challenge->data;
655 nt_status = auth_context_set_challenge(auth_context, chal, "NTLMSSP callback (NTLM2)");
661 * Check the password on an NTLMSSP login.
663 * Return the session keys used on the connection.
666 static NTSTATUS auth_ntlmssp_check_password(struct gensec_ntlmssp_state *gensec_ntlmssp_state,
668 DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
671 struct auth_usersupplied_info *user_info = talloc(mem_ctx, struct auth_usersupplied_info);
673 return NT_STATUS_NO_MEMORY;
676 user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
677 user_info->flags = 0;
678 user_info->mapped_state = false;
679 user_info->client.account_name = gensec_ntlmssp_state->user;
680 user_info->client.domain_name = gensec_ntlmssp_state->domain;
681 user_info->workstation_name = gensec_ntlmssp_state->workstation;
682 user_info->remote_host = gensec_get_peer_addr(gensec_ntlmssp_state->gensec_security);
684 user_info->password_state = AUTH_PASSWORD_RESPONSE;
685 user_info->password.response.lanman = gensec_ntlmssp_state->lm_resp;
686 user_info->password.response.lanman.data = talloc_steal(user_info, gensec_ntlmssp_state->lm_resp.data);
687 user_info->password.response.nt = gensec_ntlmssp_state->nt_resp;
688 user_info->password.response.nt.data = talloc_steal(user_info, gensec_ntlmssp_state->nt_resp.data);
690 nt_status = auth_check_password(gensec_ntlmssp_state->auth_context, mem_ctx,
691 user_info, &gensec_ntlmssp_state->server_info);
692 talloc_free(user_info);
693 NT_STATUS_NOT_OK_RETURN(nt_status);
695 talloc_steal(gensec_ntlmssp_state, gensec_ntlmssp_state->server_info);
697 if (gensec_ntlmssp_state->server_info->user_session_key.length) {
698 DEBUG(10, ("Got NT session key of length %u\n",
699 (unsigned)gensec_ntlmssp_state->server_info->user_session_key.length));
700 if (!talloc_reference(mem_ctx, gensec_ntlmssp_state->server_info->user_session_key.data)) {
701 return NT_STATUS_NO_MEMORY;
704 *user_session_key = gensec_ntlmssp_state->server_info->user_session_key;
706 if (gensec_ntlmssp_state->server_info->lm_session_key.length) {
707 DEBUG(10, ("Got LM session key of length %u\n",
708 (unsigned)gensec_ntlmssp_state->server_info->lm_session_key.length));
709 if (!talloc_reference(mem_ctx, gensec_ntlmssp_state->server_info->lm_session_key.data)) {
710 return NT_STATUS_NO_MEMORY;
713 *lm_session_key = gensec_ntlmssp_state->server_info->lm_session_key;
719 * Return the credentials of a logged on user, including session keys
722 * Only valid after a successful authentication
724 * May only be called once per authentication.
728 NTSTATUS gensec_ntlmssp_session_info(struct gensec_security *gensec_security,
729 struct auth_session_info **session_info)
732 struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
734 nt_status = auth_generate_session_info(gensec_ntlmssp_state, gensec_security->event_ctx, gensec_security->lp_ctx, gensec_ntlmssp_state->server_info, session_info);
735 NT_STATUS_NOT_OK_RETURN(nt_status);
737 (*session_info)->session_key = data_blob_talloc(*session_info,
738 gensec_ntlmssp_state->session_key.data,
739 gensec_ntlmssp_state->session_key.length);
745 * Start NTLMSSP on the server side
748 NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
751 struct gensec_ntlmssp_state *gensec_ntlmssp_state;
753 nt_status = gensec_ntlmssp_start(gensec_security);
754 NT_STATUS_NOT_OK_RETURN(nt_status);
756 gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
758 gensec_ntlmssp_state->role = NTLMSSP_SERVER;
760 gensec_ntlmssp_state->workstation = NULL;
761 gensec_ntlmssp_state->server_name = lp_netbios_name(gensec_security->lp_ctx);
763 gensec_ntlmssp_state->domain = lp_workgroup(gensec_security->lp_ctx);
765 gensec_ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
767 gensec_ntlmssp_state->allow_lm_key = (lp_lanman_auth(gensec_security->lp_ctx)
768 && lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "allow_lm_key", false));
770 gensec_ntlmssp_state->server_multiple_authentications = false;
772 gensec_ntlmssp_state->neg_flags =
773 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_UNKNOWN_02000000;
775 gensec_ntlmssp_state->lm_resp = data_blob(NULL, 0);
776 gensec_ntlmssp_state->nt_resp = data_blob(NULL, 0);
777 gensec_ntlmssp_state->encrypted_session_key = data_blob(NULL, 0);
779 if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "128bit", true)) {
780 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
783 if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "56bit", true)) {
784 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
787 if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "keyexchange", true)) {
788 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
791 if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "alwayssign", true)) {
792 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
795 if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "ntlm2", true)) {
796 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
799 if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
800 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
802 if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
803 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
806 nt_status = auth_context_create(gensec_ntlmssp_state,
807 gensec_security->event_ctx,
808 gensec_security->msg_ctx,
809 gensec_security->lp_ctx,
810 &gensec_ntlmssp_state->auth_context);
811 NT_STATUS_NOT_OK_RETURN(nt_status);
813 gensec_ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
814 gensec_ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
815 gensec_ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
816 gensec_ntlmssp_state->check_password = auth_ntlmssp_check_password;
817 gensec_ntlmssp_state->server_role = lp_server_role(gensec_security->lp_ctx);