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 gensec_ntlmssp_state
*gensec_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 gensec_ntlmssp_state
->user
= talloc_strdup(gensec_ntlmssp_state
, user
);
47 if (!gensec_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 gensec_ntlmssp_state
*gensec_ntlmssp_state
, const char *domain
)
59 gensec_ntlmssp_state
->domain
= talloc_strdup(gensec_ntlmssp_state
, domain
);
60 if (!gensec_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 gensec_ntlmssp_state
*gensec_ntlmssp_state
, const char *workstation
)
72 gensec_ntlmssp_state
->workstation
= talloc_strdup(gensec_ntlmssp_state
, workstation
);
73 if (!gensec_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 gensec_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 gensec_ntlmssp_state
*gensec_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 (gensec_ntlmssp_state
->server
.is_standalone
) {
96 *chal_flags
|= NTLMSSP_TARGET_TYPE_SERVER
;
97 return gensec_ntlmssp_state
->server_name
;
99 *chal_flags
|= NTLMSSP_TARGET_TYPE_DOMAIN
;
100 return gensec_ntlmssp_state
->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 gensec_ntlmssp_state
*gensec_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(gensec_ntlmssp_state
, neg_flags
, gensec_ntlmssp_state
->allow_lm_key
);
156 /* Ask our caller what challenge they would like in the packet */
157 status
= gensec_ntlmssp_state
->get_challenge(gensec_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 (!gensec_ntlmssp_state
->may_set_challenge(gensec_ntlmssp_state
)) {
166 gensec_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
= gensec_ntlmssp_state
->neg_flags
;
176 /* get the right name to fill in as 'target' */
177 target_name
= ntlmssp_target_name(gensec_ntlmssp_state
,
178 neg_flags
, &chal_flags
);
179 if (target_name
== NULL
)
180 return NT_STATUS_INVALID_PARAMETER
;
182 gensec_ntlmssp_state
->chal
= data_blob_talloc(gensec_ntlmssp_state
, cryptkey
, 8);
183 gensec_ntlmssp_state
->internal_chal
= data_blob_talloc(gensec_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 char dnsdomname
[MAXHOSTNAMELEN
], dnsname
[MAXHOSTNAMELEN
];
189 /* Find out the DNS domain name */
190 dnsdomname
[0] = '\0';
191 safe_strcpy(dnsdomname
, lp_dnsdomain(gensec_security
->settings
->lp_ctx
), sizeof(dnsdomname
) - 1);
193 /* Find out the DNS host name */
194 safe_strcpy(dnsname
, gensec_ntlmssp_state
->server_name
, sizeof(dnsname
) - 1);
195 if (dnsdomname
[0] != '\0') {
196 safe_strcat(dnsname
, ".", sizeof(dnsname
) - 1);
197 safe_strcat(dnsname
, dnsdomname
, sizeof(dnsname
) - 1);
201 msrpc_gen(out_mem_ctx
,
202 &struct_blob
, "aaaaa",
203 MsvAvNbDomainName
, target_name
,
204 MsvAvNbComputerName
, gensec_ntlmssp_state
->server_name
,
205 MsvAvDnsDomainName
, dnsdomname
,
206 MsvAvDnsComputerName
, dnsname
,
209 struct_blob
= data_blob(NULL
, 0);
213 /* Marshal the packet in the right format, be it unicode or ASCII */
214 const char *gen_string
;
215 if (gensec_ntlmssp_state
->unicode
) {
216 gen_string
= "CdUdbddB";
218 gen_string
= "CdAdbddB";
221 msrpc_gen(out_mem_ctx
,
229 struct_blob
.data
, struct_blob
.length
);
232 gensec_ntlmssp_state
->expected_state
= NTLMSSP_AUTH
;
234 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
238 * Next state function for the Authenticate packet
240 * @param gensec_ntlmssp_state NTLMSSP State
241 * @param request The request, as a DATA_BLOB
242 * @return Errors or NT_STATUS_OK.
245 static NTSTATUS
ntlmssp_server_preauth(struct gensec_ntlmssp_state
*gensec_ntlmssp_state
,
246 const DATA_BLOB request
)
248 uint32_t ntlmssp_command
, auth_flags
;
251 uint8_t session_nonce_hash
[16];
253 const char *parse_string
;
256 char *workstation
= NULL
;
259 file_save("ntlmssp_auth.dat", request
.data
, request
.length
);
262 if (gensec_ntlmssp_state
->unicode
) {
263 parse_string
= "CdBBUUUBd";
265 parse_string
= "CdBBAAABd";
269 data_blob_free(&gensec_ntlmssp_state
->session_key
);
270 data_blob_free(&gensec_ntlmssp_state
->lm_resp
);
271 data_blob_free(&gensec_ntlmssp_state
->nt_resp
);
272 data_blob_free(&gensec_ntlmssp_state
->encrypted_session_key
);
274 gensec_ntlmssp_state
->user
= NULL
;
275 gensec_ntlmssp_state
->domain
= NULL
;
276 gensec_ntlmssp_state
->workstation
= NULL
;
278 /* now the NTLMSSP encoded auth hashes */
279 if (!msrpc_parse(gensec_ntlmssp_state
,
280 &request
, parse_string
,
283 &gensec_ntlmssp_state
->lm_resp
,
284 &gensec_ntlmssp_state
->nt_resp
,
288 &gensec_ntlmssp_state
->encrypted_session_key
,
290 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
291 dump_data(10, request
.data
, request
.length
);
294 data_blob_free(&gensec_ntlmssp_state
->encrypted_session_key
);
297 /* Try again with a shorter string (Win9X truncates this packet) */
298 if (gensec_ntlmssp_state
->unicode
) {
299 parse_string
= "CdBBUUU";
301 parse_string
= "CdBBAAA";
304 /* now the NTLMSSP encoded auth hashes */
305 if (!msrpc_parse(gensec_ntlmssp_state
,
306 &request
, parse_string
,
309 &gensec_ntlmssp_state
->lm_resp
,
310 &gensec_ntlmssp_state
->nt_resp
,
314 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP:\n"));
315 dump_data(2, request
.data
, request
.length
);
317 return NT_STATUS_INVALID_PARAMETER
;
322 ntlmssp_handle_neg_flags(gensec_ntlmssp_state
, auth_flags
, gensec_ntlmssp_state
->allow_lm_key
);
324 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_set_domain(gensec_ntlmssp_state
, domain
))) {
326 data_blob_free(&gensec_ntlmssp_state
->encrypted_session_key
);
330 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_set_username(gensec_ntlmssp_state
, user
))) {
332 data_blob_free(&gensec_ntlmssp_state
->encrypted_session_key
);
336 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_set_workstation(gensec_ntlmssp_state
, workstation
))) {
338 data_blob_free(&gensec_ntlmssp_state
->encrypted_session_key
);
342 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
343 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
));
346 file_save("nthash1.dat", &gensec_ntlmssp_state
->nt_resp
.data
, &gensec_ntlmssp_state
->nt_resp
.length
);
347 file_save("lmhash1.dat", &gensec_ntlmssp_state
->lm_resp
.data
, &gensec_ntlmssp_state
->lm_resp
.length
);
350 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
353 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
355 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_NTLM2
) {
356 if (gensec_ntlmssp_state
->nt_resp
.length
== 24 && gensec_ntlmssp_state
->lm_resp
.length
== 24) {
357 struct MD5Context md5_session_nonce_ctx
;
358 SMB_ASSERT(gensec_ntlmssp_state
->internal_chal
.data
359 && gensec_ntlmssp_state
->internal_chal
.length
== 8);
361 gensec_ntlmssp_state
->doing_ntlm2
= true;
363 memcpy(gensec_ntlmssp_state
->crypt
.ntlm2
.session_nonce
, gensec_ntlmssp_state
->internal_chal
.data
, 8);
364 memcpy(&gensec_ntlmssp_state
->crypt
.ntlm2
.session_nonce
[8], gensec_ntlmssp_state
->lm_resp
.data
, 8);
366 MD5Init(&md5_session_nonce_ctx
);
367 MD5Update(&md5_session_nonce_ctx
, gensec_ntlmssp_state
->crypt
.ntlm2
.session_nonce
, 16);
368 MD5Final(session_nonce_hash
, &md5_session_nonce_ctx
);
370 gensec_ntlmssp_state
->chal
= data_blob_talloc(gensec_ntlmssp_state
,
371 session_nonce_hash
, 8);
373 /* LM response is no longer useful, zero it out */
374 data_blob_free(&gensec_ntlmssp_state
->lm_resp
);
376 /* We changed the effective challenge - set it */
377 if (!NT_STATUS_IS_OK(nt_status
=
378 gensec_ntlmssp_state
->set_challenge(gensec_ntlmssp_state
,
379 &gensec_ntlmssp_state
->chal
))) {
381 data_blob_free(&gensec_ntlmssp_state
->encrypted_session_key
);
385 /* LM Key is incompatible... */
386 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_LM_KEY
;
393 * Next state function for the Authenticate packet
394 * (after authentication - figures out the session keys etc)
396 * @param gensec_ntlmssp_state NTLMSSP State
397 * @return Errors or NT_STATUS_OK.
400 static NTSTATUS
ntlmssp_server_postauth(struct gensec_security
*gensec_security
,
401 DATA_BLOB
*user_session_key
,
402 DATA_BLOB
*lm_session_key
)
404 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
405 talloc_get_type_abort(gensec_security
->private_data
,
406 struct gensec_ntlmssp_context
);
407 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
= gensec_ntlmssp
->ntlmssp_state
;
409 DATA_BLOB session_key
= data_blob(NULL
, 0);
411 if (!(gensec_security
->want_features
412 & (GENSEC_FEATURE_SIGN
|GENSEC_FEATURE_SEAL
|GENSEC_FEATURE_SESSION_KEY
))) {
416 if (user_session_key
)
417 dump_data_pw("USER session key:\n", user_session_key
->data
, user_session_key
->length
);
420 dump_data_pw("LM first-8:\n", lm_session_key
->data
, lm_session_key
->length
);
422 /* Handle the different session key derivation for NTLM2 */
423 if (gensec_ntlmssp_state
->doing_ntlm2
) {
424 if (user_session_key
&& user_session_key
->data
&& user_session_key
->length
== 16) {
425 session_key
= data_blob_talloc(gensec_ntlmssp_state
, NULL
, 16);
426 hmac_md5(user_session_key
->data
, gensec_ntlmssp_state
->crypt
.ntlm2
.session_nonce
,
427 sizeof(gensec_ntlmssp_state
->crypt
.ntlm2
.session_nonce
), session_key
.data
);
428 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
429 dump_data_pw("NTLM2 session key:\n", session_key
.data
, session_key
.length
);
432 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
433 session_key
= data_blob(NULL
, 0);
435 } else if ((gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_LM_KEY
)
436 /* Ensure we can never get here on NTLMv2 */
437 && (gensec_ntlmssp_state
->nt_resp
.length
== 0 || gensec_ntlmssp_state
->nt_resp
.length
== 24)) {
439 if (lm_session_key
&& lm_session_key
->data
&& lm_session_key
->length
>= 8) {
440 if (gensec_ntlmssp_state
->lm_resp
.data
&& gensec_ntlmssp_state
->lm_resp
.length
== 24) {
441 session_key
= data_blob_talloc(gensec_ntlmssp_state
, NULL
, 16);
442 SMBsesskeygen_lm_sess_key(lm_session_key
->data
, gensec_ntlmssp_state
->lm_resp
.data
,
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 /* When there is no LM response, just use zeros */
449 static const uint8_t zeros
[24];
450 session_key
= data_blob_talloc(gensec_ntlmssp_state
, NULL
, 16);
451 SMBsesskeygen_lm_sess_key(zeros
, zeros
,
453 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
454 dump_data_pw("LM session key:\n", session_key
.data
, session_key
.length
);
457 /* LM Key not selected */
458 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_LM_KEY
;
460 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
461 session_key
= data_blob(NULL
, 0);
464 } else if (user_session_key
&& user_session_key
->data
) {
465 session_key
= data_blob_talloc(gensec_ntlmssp_state
, user_session_key
->data
, user_session_key
->length
);
466 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
467 dump_data_pw("unmodified session key:\n", session_key
.data
, session_key
.length
);
469 /* LM Key not selected */
470 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_LM_KEY
;
472 } else if (lm_session_key
&& lm_session_key
->data
) {
473 /* Very weird to have LM key, but no user session key, but anyway.. */
474 session_key
= data_blob_talloc(gensec_ntlmssp_state
, lm_session_key
->data
, lm_session_key
->length
);
475 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
476 dump_data_pw("unmodified session key:\n", session_key
.data
, session_key
.length
);
478 /* LM Key not selected */
479 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_LM_KEY
;
482 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
483 session_key
= data_blob(NULL
, 0);
485 /* LM Key not selected */
486 gensec_ntlmssp_state
->neg_flags
&= ~NTLMSSP_NEGOTIATE_LM_KEY
;
489 /* With KEY_EXCH, the client supplies the proposed session key,
490 but encrypts it with the long-term key */
491 if (gensec_ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_KEY_EXCH
) {
492 if (!gensec_ntlmssp_state
->encrypted_session_key
.data
493 || gensec_ntlmssp_state
->encrypted_session_key
.length
!= 16) {
494 data_blob_free(&gensec_ntlmssp_state
->encrypted_session_key
);
495 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
496 (unsigned)gensec_ntlmssp_state
->encrypted_session_key
.length
));
497 return NT_STATUS_INVALID_PARAMETER
;
498 } else if (!session_key
.data
|| session_key
.length
!= 16) {
499 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
500 (unsigned)session_key
.length
));
501 gensec_ntlmssp_state
->session_key
= session_key
;
503 dump_data_pw("KEY_EXCH session key (enc):\n",
504 gensec_ntlmssp_state
->encrypted_session_key
.data
,
505 gensec_ntlmssp_state
->encrypted_session_key
.length
);
506 arcfour_crypt(gensec_ntlmssp_state
->encrypted_session_key
.data
,
508 gensec_ntlmssp_state
->encrypted_session_key
.length
);
509 gensec_ntlmssp_state
->session_key
= data_blob_talloc(gensec_ntlmssp_state
,
510 gensec_ntlmssp_state
->encrypted_session_key
.data
,
511 gensec_ntlmssp_state
->encrypted_session_key
.length
);
512 dump_data_pw("KEY_EXCH session key:\n", gensec_ntlmssp_state
->encrypted_session_key
.data
,
513 gensec_ntlmssp_state
->encrypted_session_key
.length
);
514 talloc_free(session_key
.data
);
517 gensec_ntlmssp_state
->session_key
= session_key
;
520 if ((gensec_security
->want_features
& GENSEC_FEATURE_SIGN
)
521 || (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
)) {
522 nt_status
= ntlmssp_sign_init(gensec_ntlmssp_state
);
524 nt_status
= NT_STATUS_OK
;
527 data_blob_free(&gensec_ntlmssp_state
->encrypted_session_key
);
529 /* allow arbitarily many authentications, but watch that this will cause a
530 memory leak, until the gensec_ntlmssp_state is shutdown
533 if (gensec_ntlmssp_state
->server_multiple_authentications
) {
534 gensec_ntlmssp_state
->expected_state
= NTLMSSP_AUTH
;
536 gensec_ntlmssp_state
->expected_state
= NTLMSSP_DONE
;
544 * Next state function for the Authenticate packet
546 * @param gensec_security GENSEC state
547 * @param out_mem_ctx Memory context for *out
548 * @param in The request, as a DATA_BLOB. reply.data must be NULL
549 * @param out The reply, as an allocated DATA_BLOB, caller to free.
550 * @return Errors or NT_STATUS_OK if authentication sucessful
553 NTSTATUS
ntlmssp_server_auth(struct gensec_security
*gensec_security
,
554 TALLOC_CTX
*out_mem_ctx
,
555 const DATA_BLOB in
, DATA_BLOB
*out
)
557 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
558 talloc_get_type_abort(gensec_security
->private_data
,
559 struct gensec_ntlmssp_context
);
560 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
= gensec_ntlmssp
->ntlmssp_state
;
561 DATA_BLOB user_session_key
= data_blob_null
;
562 DATA_BLOB lm_session_key
= data_blob_null
;
565 /* zero the outbound NTLMSSP packet */
566 *out
= data_blob_null
;
568 nt_status
= ntlmssp_server_preauth(gensec_ntlmssp_state
, in
);
569 if (!NT_STATUS_IS_OK(nt_status
)) {
574 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
575 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
576 * smb.conf file) and no NTLMv2 response was sent then the password check
577 * will fail here. JRA.
580 /* Finally, actually ask if the password is OK */
581 nt_status
= gensec_ntlmssp_state
->check_password(gensec_ntlmssp_state
,
584 if (!NT_STATUS_IS_OK(nt_status
)) {
588 nt_status
= ntlmssp_server_postauth(gensec_security
,
591 if (!NT_STATUS_IS_OK(nt_status
)) {
599 * Return the challenge as determined by the authentication subsystem
600 * @return an 8 byte random challenge
603 static NTSTATUS
auth_ntlmssp_get_challenge(const struct gensec_ntlmssp_state
*gensec_ntlmssp_state
,
606 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
607 talloc_get_type_abort(gensec_ntlmssp_state
->callback_private
,
608 struct gensec_ntlmssp_context
);
609 struct auth_context
*auth_context
= gensec_ntlmssp
->auth_context
;
612 status
= auth_context
->get_challenge(auth_context
, chal
);
613 if (!NT_STATUS_IS_OK(status
)) {
614 DEBUG(1, ("auth_ntlmssp_get_challenge: failed to get challenge: %s\n",
623 * Some authentication methods 'fix' the challenge, so we may not be able to set it
625 * @return If the effective challenge used by the auth subsystem may be modified
627 static bool auth_ntlmssp_may_set_challenge(const struct gensec_ntlmssp_state
*gensec_ntlmssp_state
)
629 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
630 talloc_get_type_abort(gensec_ntlmssp_state
->callback_private
,
631 struct gensec_ntlmssp_context
);
632 struct auth_context
*auth_context
= gensec_ntlmssp
->auth_context
;
634 return auth_context
->challenge_may_be_modified(auth_context
);
638 * NTLM2 authentication modifies the effective challenge,
639 * @param challenge The new challenge value
641 static NTSTATUS
auth_ntlmssp_set_challenge(struct gensec_ntlmssp_state
*gensec_ntlmssp_state
, DATA_BLOB
*challenge
)
643 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
644 talloc_get_type_abort(gensec_ntlmssp_state
->callback_private
,
645 struct gensec_ntlmssp_context
);
646 struct auth_context
*auth_context
= gensec_ntlmssp
->auth_context
;
650 if (challenge
->length
!= 8) {
651 return NT_STATUS_INVALID_PARAMETER
;
654 chal
= challenge
->data
;
656 nt_status
= auth_context
->set_challenge(auth_context
,
658 "NTLMSSP callback (NTLM2)");
664 * Check the password on an NTLMSSP login.
666 * Return the session keys used on the connection.
669 static NTSTATUS
auth_ntlmssp_check_password(struct gensec_ntlmssp_state
*gensec_ntlmssp_state
,
670 DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
672 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
673 talloc_get_type_abort(gensec_ntlmssp_state
->callback_private
,
674 struct gensec_ntlmssp_context
);
675 struct auth_context
*auth_context
= gensec_ntlmssp
->auth_context
;
677 struct auth_usersupplied_info
*user_info
;
679 user_info
= talloc(gensec_ntlmssp_state
, struct auth_usersupplied_info
);
681 return NT_STATUS_NO_MEMORY
;
684 user_info
->logon_parameters
= MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
| MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
;
685 user_info
->flags
= 0;
686 user_info
->mapped_state
= false;
687 user_info
->client
.account_name
= gensec_ntlmssp_state
->user
;
688 user_info
->client
.domain_name
= gensec_ntlmssp_state
->domain
;
689 user_info
->workstation_name
= gensec_ntlmssp_state
->workstation
;
690 user_info
->remote_host
= gensec_get_remote_address(gensec_ntlmssp
->gensec_security
);
692 user_info
->password_state
= AUTH_PASSWORD_RESPONSE
;
693 user_info
->password
.response
.lanman
= gensec_ntlmssp_state
->lm_resp
;
694 user_info
->password
.response
.lanman
.data
= talloc_steal(user_info
, gensec_ntlmssp_state
->lm_resp
.data
);
695 user_info
->password
.response
.nt
= gensec_ntlmssp_state
->nt_resp
;
696 user_info
->password
.response
.nt
.data
= talloc_steal(user_info
, gensec_ntlmssp_state
->nt_resp
.data
);
698 nt_status
= auth_context
->check_password(auth_context
,
701 &gensec_ntlmssp
->server_info
);
702 talloc_free(user_info
);
703 NT_STATUS_NOT_OK_RETURN(nt_status
);
705 if (gensec_ntlmssp
->server_info
->user_session_key
.length
) {
706 DEBUG(10, ("Got NT session key of length %u\n",
707 (unsigned)gensec_ntlmssp
->server_info
->user_session_key
.length
));
708 *user_session_key
= gensec_ntlmssp
->server_info
->user_session_key
;
710 if (gensec_ntlmssp
->server_info
->lm_session_key
.length
) {
711 DEBUG(10, ("Got LM session key of length %u\n",
712 (unsigned)gensec_ntlmssp
->server_info
->lm_session_key
.length
));
713 *lm_session_key
= gensec_ntlmssp
->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_context
*gensec_ntlmssp
=
733 talloc_get_type_abort(gensec_security
->private_data
,
734 struct gensec_ntlmssp_context
);
735 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
= gensec_ntlmssp
->ntlmssp_state
;
737 nt_status
= auth_generate_session_info(gensec_ntlmssp_state
,
738 gensec_security
->event_ctx
,
739 gensec_security
->settings
->lp_ctx
,
740 gensec_ntlmssp
->server_info
,
742 NT_STATUS_NOT_OK_RETURN(nt_status
);
744 (*session_info
)->session_key
= data_blob_talloc(*session_info
,
745 gensec_ntlmssp_state
->session_key
.data
,
746 gensec_ntlmssp_state
->session_key
.length
);
752 * Start NTLMSSP on the server side
755 NTSTATUS
gensec_ntlmssp_server_start(struct gensec_security
*gensec_security
)
758 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
;
759 struct gensec_ntlmssp_context
*gensec_ntlmssp
;
761 nt_status
= gensec_ntlmssp_start(gensec_security
);
762 NT_STATUS_NOT_OK_RETURN(nt_status
);
764 gensec_ntlmssp
= talloc_get_type_abort(gensec_security
->private_data
,
765 struct gensec_ntlmssp_context
);
766 gensec_ntlmssp_state
= gensec_ntlmssp
->ntlmssp_state
;
768 gensec_ntlmssp_state
->role
= NTLMSSP_SERVER
;
770 gensec_ntlmssp_state
->workstation
= NULL
;
771 gensec_ntlmssp_state
->server_name
= lp_netbios_name(gensec_security
->settings
->lp_ctx
);
773 gensec_ntlmssp_state
->domain
= lp_workgroup(gensec_security
->settings
->lp_ctx
);
775 gensec_ntlmssp_state
->expected_state
= NTLMSSP_NEGOTIATE
;
777 gensec_ntlmssp_state
->allow_lm_key
= (lp_lanman_auth(gensec_security
->settings
->lp_ctx
)
778 && gensec_setting_bool(gensec_security
->settings
, "ntlmssp_server", "allow_lm_key", false));
780 gensec_ntlmssp_state
->server_multiple_authentications
= false;
782 gensec_ntlmssp_state
->neg_flags
=
783 NTLMSSP_NEGOTIATE_NTLM
| NTLMSSP_NEGOTIATE_VERSION
;
785 gensec_ntlmssp_state
->lm_resp
= data_blob(NULL
, 0);
786 gensec_ntlmssp_state
->nt_resp
= data_blob(NULL
, 0);
787 gensec_ntlmssp_state
->encrypted_session_key
= data_blob(NULL
, 0);
789 if (gensec_setting_bool(gensec_security
->settings
, "ntlmssp_server", "128bit", true)) {
790 gensec_ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_128
;
793 if (gensec_setting_bool(gensec_security
->settings
, "ntlmssp_server", "56bit", true)) {
794 gensec_ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_56
;
797 if (gensec_setting_bool(gensec_security
->settings
, "ntlmssp_server", "keyexchange", true)) {
798 gensec_ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_KEY_EXCH
;
801 if (gensec_setting_bool(gensec_security
->settings
, "ntlmssp_server", "alwayssign", true)) {
802 gensec_ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_ALWAYS_SIGN
;
805 if (gensec_setting_bool(gensec_security
->settings
, "ntlmssp_server", "ntlm2", true)) {
806 gensec_ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_NTLM2
;
809 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
810 gensec_ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_SIGN
;
812 if (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
813 gensec_ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_SEAL
;
816 gensec_ntlmssp
->auth_context
= gensec_security
->auth_context
;
818 gensec_ntlmssp_state
->get_challenge
= auth_ntlmssp_get_challenge
;
819 gensec_ntlmssp_state
->may_set_challenge
= auth_ntlmssp_may_set_challenge
;
820 gensec_ntlmssp_state
->set_challenge
= auth_ntlmssp_set_challenge
;
821 gensec_ntlmssp_state
->check_password
= auth_ntlmssp_check_password
;
822 if (lp_server_role(gensec_security
->settings
->lp_ctx
) == ROLE_STANDALONE
) {
823 gensec_ntlmssp_state
->server
.is_standalone
= true;
825 gensec_ntlmssp_state
->server
.is_standalone
= false;