s4:ntlmssp: replace server_role by a server.is_standalone in (gensec_)ntlmssp_state
[Samba/ekacnet.git] / source4 / auth / ntlmssp / ntlmssp_server.c
blob484cf3f51d1912d14e77b6bbfd0463a1693cf0df
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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/>.
24 #include "includes.h"
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"
34 /**
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)
41 if (!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;
50 return NT_STATUS_OK;
53 /**
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;
63 return NT_STATUS_OK;
66 /**
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;
76 return NT_STATUS_OK;
79 /**
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;
98 } else {
99 *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
100 return gensec_ntlmssp_state->domain;
102 } else {
103 return "";
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;
130 uint8_t cryptkey[8];
131 const char *target_name;
132 NTSTATUS status;
134 /* parse the NTLMSSP packet */
135 #if 0
136 file_save("ntlmssp_negotiate.dat", request.data, request.length);
137 #endif
139 if (in.length) {
140 if ((in.length < 16) || !msrpc_parse(out_mem_ctx,
141 &in, "Cdd",
142 "NTLMSSP",
143 &ntlmssp_command,
144 &neg_flags)) {
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",
160 nt_errstr(status)));
161 return status;
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);
199 strlower_m(dnsname);
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,
207 MsvAvEOL, "");
208 } else {
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";
217 } else {
218 gen_string = "CdAdbddB";
221 msrpc_gen(out_mem_ctx,
222 out, gen_string,
223 "NTLMSSP",
224 NTLMSSP_CHALLENGE,
225 target_name,
226 chal_flags,
227 cryptkey, 8,
228 0, 0,
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;
249 NTSTATUS nt_status;
251 uint8_t session_nonce_hash[16];
253 const char *parse_string;
254 char *domain = NULL;
255 char *user = NULL;
256 char *workstation = NULL;
258 #if 0
259 file_save("ntlmssp_auth.dat", request.data, request.length);
260 #endif
262 if (gensec_ntlmssp_state->unicode) {
263 parse_string = "CdBBUUUBd";
264 } else {
265 parse_string = "CdBBAAABd";
268 /* zero these out */
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,
281 "NTLMSSP",
282 &ntlmssp_command,
283 &gensec_ntlmssp_state->lm_resp,
284 &gensec_ntlmssp_state->nt_resp,
285 &domain,
286 &user,
287 &workstation,
288 &gensec_ntlmssp_state->encrypted_session_key,
289 &auth_flags)) {
290 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
291 dump_data(10, request.data, request.length);
293 /* zero this out */
294 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
295 auth_flags = 0;
297 /* Try again with a shorter string (Win9X truncates this packet) */
298 if (gensec_ntlmssp_state->unicode) {
299 parse_string = "CdBBUUU";
300 } else {
301 parse_string = "CdBBAAA";
304 /* now the NTLMSSP encoded auth hashes */
305 if (!msrpc_parse(gensec_ntlmssp_state,
306 &request, parse_string,
307 "NTLMSSP",
308 &ntlmssp_command,
309 &gensec_ntlmssp_state->lm_resp,
310 &gensec_ntlmssp_state->nt_resp,
311 &domain,
312 &user,
313 &workstation)) {
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;
321 if (auth_flags)
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))) {
325 /* zero this out */
326 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
327 return nt_status;
330 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(gensec_ntlmssp_state, user))) {
331 /* zero this out */
332 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
333 return nt_status;
336 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(gensec_ntlmssp_state, workstation))) {
337 /* zero this out */
338 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
339 return nt_status;
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));
345 #if 0
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);
348 #endif
350 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
351 client challenge
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))) {
380 /* zero this out */
381 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
382 return nt_status;
385 /* LM Key is incompatible... */
386 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
389 return NT_STATUS_OK;
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;
408 NTSTATUS nt_status;
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))) {
413 return NT_STATUS_OK;
416 if (user_session_key)
417 dump_data_pw("USER session key:\n", user_session_key->data, user_session_key->length);
419 if (lm_session_key)
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);
431 } else {
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,
443 session_key.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);
446 } else {
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,
452 session_key.data);
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);
456 } else {
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;
481 } else {
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;
502 } else {
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,
507 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);
516 } else {
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);
523 } else {
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;
535 } else {
536 gensec_ntlmssp_state->expected_state = NTLMSSP_DONE;
539 return nt_status;
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;
563 NTSTATUS nt_status;
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)) {
570 return 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,
582 &user_session_key,
583 &lm_session_key);
584 if (!NT_STATUS_IS_OK(nt_status)) {
585 return nt_status;
588 nt_status = ntlmssp_server_postauth(gensec_security,
589 &user_session_key,
590 &lm_session_key);
591 if (!NT_STATUS_IS_OK(nt_status)) {
592 return nt_status;
595 return NT_STATUS_OK;
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,
604 uint8_t chal[8])
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;
610 NTSTATUS status;
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",
615 nt_errstr(status)));
616 return status;
619 return NT_STATUS_OK;
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;
647 NTSTATUS nt_status;
648 const uint8_t *chal;
650 if (challenge->length != 8) {
651 return NT_STATUS_INVALID_PARAMETER;
654 chal = challenge->data;
656 nt_status = auth_context->set_challenge(auth_context,
657 chal,
658 "NTLMSSP callback (NTLM2)");
660 return nt_status;
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;
676 NTSTATUS nt_status;
677 struct auth_usersupplied_info *user_info;
679 user_info = talloc(gensec_ntlmssp_state, struct auth_usersupplied_info);
680 if (!user_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,
699 gensec_ntlmssp,
700 user_info,
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;
715 return nt_status;
718 /**
719 * Return the credentials of a logged on user, including session keys
720 * etc.
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)
731 NTSTATUS nt_status;
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,
741 session_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);
748 return NT_STATUS_OK;
752 * Start NTLMSSP on the server side
755 NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
757 NTSTATUS nt_status;
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;
824 } else {
825 gensec_ntlmssp_state->server.is_standalone = false;
828 return NT_STATUS_OK;