auth_log: Also log the final type of authentication (ntlmssp,krb5)
[Samba.git] / auth / ntlmssp / ntlmssp_server.c
blobc525a93941f45d3109c6338a60d06a646f1f50d4
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 handle NTLMSSP, server side
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett 2001-2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "lib/util/time_basic.h"
25 #include "auth/ntlmssp/ntlmssp.h"
26 #include "auth/ntlmssp/ntlmssp_private.h"
27 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
28 #include "auth/ntlmssp/ntlmssp_ndr.h"
29 #include "../libcli/auth/libcli_auth.h"
30 #include "../lib/crypto/crypto.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/gensec/gensec_internal.h"
33 #include "auth/common_auth.h"
34 #include "param/param.h"
35 #include "param/loadparm.h"
36 #include "libcli/security/session.h"
38 /**
39 * Determine correct target name flags for reply, given server role
40 * and negotiated flags
42 * @param ntlmssp_state NTLMSSP State
43 * @param neg_flags The flags from the packet
44 * @param chal_flags The flags to be set in the reply packet
45 * @return The 'target name' string.
48 const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
49 uint32_t neg_flags, uint32_t *chal_flags)
51 if (neg_flags & NTLMSSP_REQUEST_TARGET) {
52 *chal_flags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
53 *chal_flags |= NTLMSSP_REQUEST_TARGET;
54 if (ntlmssp_state->server.is_standalone) {
55 *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
56 return ntlmssp_state->server.netbios_name;
57 } else {
58 *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
59 return ntlmssp_state->server.netbios_domain;
61 } else {
62 return "";
66 /**
67 * Next state function for the NTLMSSP Negotiate packet
69 * @param gensec_security GENSEC state
70 * @param out_mem_ctx Memory context for *out
71 * @param in The request, as a DATA_BLOB. reply.data must be NULL
72 * @param out The reply, as an allocated DATA_BLOB, caller to free.
73 * @return Errors or MORE_PROCESSING_REQUIRED if (normal) a reply is required.
76 NTSTATUS gensec_ntlmssp_server_negotiate(struct gensec_security *gensec_security,
77 TALLOC_CTX *out_mem_ctx,
78 const DATA_BLOB request, DATA_BLOB *reply)
80 struct gensec_ntlmssp_context *gensec_ntlmssp =
81 talloc_get_type_abort(gensec_security->private_data,
82 struct gensec_ntlmssp_context);
83 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
84 struct auth4_context *auth_context = gensec_security->auth_context;
85 DATA_BLOB struct_blob;
86 uint32_t neg_flags = 0;
87 uint32_t ntlmssp_command, chal_flags;
88 uint8_t cryptkey[8];
89 const char *target_name;
90 NTSTATUS status;
91 struct timeval tv_now = timeval_current();
93 * See [MS-NLMP]
95 * Windows NT 4.0, windows_2000: use 30 minutes,
96 * Windows XP, Windows Server 2003, Windows Vista,
97 * Windows Server 2008, Windows 7, and Windows Server 2008 R2
98 * use 36 hours.
100 * Newer systems doesn't check this, likely because the
101 * connectionless NTLMSSP is no longer supported.
103 * As we expect the AUTHENTICATION_MESSAGE to arrive
104 * directly after the NEGOTIATE_MESSAGE (typically less than
105 * as 1 second later). We use a hard timeout of 30 Minutes.
107 * We don't look at AUTHENTICATE_MESSAGE.NtChallengeResponse.TimeStamp
108 * instead we just remember our own time.
110 uint32_t max_lifetime = 30 * 60;
111 struct timeval tv_end = timeval_add(&tv_now, max_lifetime, 0);
113 /* parse the NTLMSSP packet */
114 #if 0
115 file_save("ntlmssp_negotiate.dat", request.data, request.length);
116 #endif
118 if (request.length) {
119 if (request.length > UINT16_MAX) {
120 DEBUG(1, ("ntlmssp_server_negotiate: reject large request of length %u\n",
121 (unsigned int)request.length));
122 return NT_STATUS_INVALID_PARAMETER;
125 if ((request.length < 16) || !msrpc_parse(ntlmssp_state, &request, "Cdd",
126 "NTLMSSP",
127 &ntlmssp_command,
128 &neg_flags)) {
129 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
130 (unsigned int)request.length));
131 dump_data(2, request.data, request.length);
132 return NT_STATUS_INVALID_PARAMETER;
134 debug_ntlmssp_flags(neg_flags);
136 if (DEBUGLEVEL >= 10) {
137 struct NEGOTIATE_MESSAGE *negotiate = talloc(
138 ntlmssp_state, struct NEGOTIATE_MESSAGE);
139 if (negotiate != NULL) {
140 status = ntlmssp_pull_NEGOTIATE_MESSAGE(
141 &request, negotiate, negotiate);
142 if (NT_STATUS_IS_OK(status)) {
143 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
144 negotiate);
146 TALLOC_FREE(negotiate);
151 status = ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, "negotiate");
152 if (!NT_STATUS_IS_OK(status)){
153 return status;
156 /* Ask our caller what challenge they would like in the packet */
157 if (auth_context->get_ntlm_challenge) {
158 status = auth_context->get_ntlm_challenge(auth_context, cryptkey);
159 if (!NT_STATUS_IS_OK(status)) {
160 DEBUG(1, ("gensec_ntlmssp_server_negotiate: failed to get challenge: %s\n",
161 nt_errstr(status)));
162 return status;
164 } else {
165 DEBUG(1, ("gensec_ntlmssp_server_negotiate: backend doesn't give a challenge\n"));
166 return NT_STATUS_NOT_IMPLEMENTED;
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;
175 ntlmssp_state->server.challenge_endtime = timeval_to_nttime(&tv_end);
177 /* get the right name to fill in as 'target' */
178 target_name = ntlmssp_target_name(ntlmssp_state,
179 neg_flags, &chal_flags);
180 if (target_name == NULL)
181 return NT_STATUS_INVALID_PARAMETER;
183 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
184 ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
185 cryptkey, 8);
187 /* This creates the 'blob' of names that appears at the end of the packet */
188 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
189 enum ndr_err_code err;
190 struct AV_PAIR *pairs = NULL;
191 uint32_t count = 5;
193 pairs = talloc_zero_array(ntlmssp_state, struct AV_PAIR, count + 1);
194 if (pairs == NULL) {
195 return NT_STATUS_NO_MEMORY;
198 pairs[0].AvId = MsvAvNbDomainName;
199 pairs[0].Value.AvNbDomainName = target_name;
201 pairs[1].AvId = MsvAvNbComputerName;
202 pairs[1].Value.AvNbComputerName = ntlmssp_state->server.netbios_name;
204 pairs[2].AvId = MsvAvDnsDomainName;
205 pairs[2].Value.AvDnsDomainName = ntlmssp_state->server.dns_domain;
207 pairs[3].AvId = MsvAvDnsComputerName;
208 pairs[3].Value.AvDnsComputerName= ntlmssp_state->server.dns_name;
210 if (!ntlmssp_state->force_old_spnego) {
211 pairs[4].AvId = MsvAvTimestamp;
212 pairs[4].Value.AvTimestamp =
213 timeval_to_nttime(&tv_now);
214 count += 1;
216 pairs[5].AvId = MsvAvEOL;
217 } else {
218 pairs[4].AvId = MsvAvEOL;
221 ntlmssp_state->server.av_pair_list.count = count;
222 ntlmssp_state->server.av_pair_list.pair = pairs;
224 err = ndr_push_struct_blob(&struct_blob,
225 ntlmssp_state,
226 &ntlmssp_state->server.av_pair_list,
227 (ndr_push_flags_fn_t)ndr_push_AV_PAIR_LIST);
228 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
229 return NT_STATUS_NO_MEMORY;
231 } else {
232 struct_blob = data_blob_null;
236 /* Marshal the packet in the right format, be it unicode or ASCII */
237 const char *gen_string;
238 const DATA_BLOB version_blob = ntlmssp_version_blob();
240 if (ntlmssp_state->unicode) {
241 gen_string = "CdUdbddBb";
242 } else {
243 gen_string = "CdAdbddBb";
246 status = msrpc_gen(out_mem_ctx, reply, gen_string,
247 "NTLMSSP",
248 NTLMSSP_CHALLENGE,
249 target_name,
250 chal_flags,
251 cryptkey, 8,
252 0, 0,
253 struct_blob.data, struct_blob.length,
254 version_blob.data, version_blob.length);
256 if (!NT_STATUS_IS_OK(status)) {
257 data_blob_free(&struct_blob);
258 return status;
261 if (DEBUGLEVEL >= 10) {
262 struct CHALLENGE_MESSAGE *challenge = talloc(
263 ntlmssp_state, struct CHALLENGE_MESSAGE);
264 if (challenge != NULL) {
265 challenge->NegotiateFlags = chal_flags;
266 status = ntlmssp_pull_CHALLENGE_MESSAGE(
267 reply, challenge, challenge);
268 if (NT_STATUS_IS_OK(status)) {
269 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
270 challenge);
272 TALLOC_FREE(challenge);
277 data_blob_free(&struct_blob);
279 ntlmssp_state->negotiate_blob = data_blob_dup_talloc(ntlmssp_state,
280 request);
281 if (ntlmssp_state->negotiate_blob.length != request.length) {
282 return NT_STATUS_NO_MEMORY;
285 ntlmssp_state->challenge_blob = data_blob_dup_talloc(ntlmssp_state,
286 *reply);
287 if (ntlmssp_state->challenge_blob.length != reply->length) {
288 return NT_STATUS_NO_MEMORY;
291 ntlmssp_state->expected_state = NTLMSSP_AUTH;
293 return NT_STATUS_MORE_PROCESSING_REQUIRED;
296 struct ntlmssp_server_auth_state {
297 DATA_BLOB user_session_key;
298 DATA_BLOB lm_session_key;
299 /* internal variables used by KEY_EXCH (client-supplied user session key */
300 DATA_BLOB encrypted_session_key;
301 bool doing_ntlm2;
302 /* internal variables used by NTLM2 */
303 uint8_t session_nonce[16];
307 * Next state function for the Authenticate packet
309 * @param ntlmssp_state NTLMSSP State
310 * @param request The request, as a DATA_BLOB
311 * @return Errors or NT_STATUS_OK.
314 static NTSTATUS ntlmssp_server_preauth(struct gensec_security *gensec_security,
315 struct gensec_ntlmssp_context *gensec_ntlmssp,
316 struct ntlmssp_server_auth_state *state,
317 const DATA_BLOB request)
319 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
320 struct auth4_context *auth_context = gensec_security->auth_context;
321 uint32_t ntlmssp_command, auth_flags;
322 NTSTATUS nt_status;
323 const unsigned int version_len = 8;
324 DATA_BLOB version_blob = data_blob_null;
325 const unsigned int mic_len = NTLMSSP_MIC_SIZE;
326 DATA_BLOB mic_blob = data_blob_null;
327 uint8_t session_nonce_hash[16];
328 const char *parse_string;
329 bool ok;
330 struct timeval endtime;
331 bool expired = false;
333 #if 0
334 file_save("ntlmssp_auth.dat", request.data, request.length);
335 #endif
337 if (ntlmssp_state->unicode) {
338 parse_string = "CdBBUUUBdbb";
339 } else {
340 parse_string = "CdBBAAABdbb";
343 /* zero these out */
344 data_blob_free(&ntlmssp_state->session_key);
345 data_blob_free(&ntlmssp_state->lm_resp);
346 data_blob_free(&ntlmssp_state->nt_resp);
348 ntlmssp_state->user = NULL;
349 ntlmssp_state->domain = NULL;
350 ntlmssp_state->client.netbios_name = NULL;
352 /* now the NTLMSSP encoded auth hashes */
353 ok = msrpc_parse(ntlmssp_state, &request, parse_string,
354 "NTLMSSP",
355 &ntlmssp_command,
356 &ntlmssp_state->lm_resp,
357 &ntlmssp_state->nt_resp,
358 &ntlmssp_state->domain,
359 &ntlmssp_state->user,
360 &ntlmssp_state->client.netbios_name,
361 &state->encrypted_session_key,
362 &auth_flags,
363 &version_blob, version_len,
364 &mic_blob, mic_len);
365 if (!ok) {
366 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
367 dump_data(10, request.data, request.length);
369 data_blob_free(&version_blob);
370 data_blob_free(&mic_blob);
372 if (ntlmssp_state->unicode) {
373 parse_string = "CdBBUUUBd";
374 } else {
375 parse_string = "CdBBAAABd";
378 ok = msrpc_parse(ntlmssp_state, &request, parse_string,
379 "NTLMSSP",
380 &ntlmssp_command,
381 &ntlmssp_state->lm_resp,
382 &ntlmssp_state->nt_resp,
383 &ntlmssp_state->domain,
384 &ntlmssp_state->user,
385 &ntlmssp_state->client.netbios_name,
386 &state->encrypted_session_key,
387 &auth_flags);
390 if (!ok) {
391 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
392 dump_data(10, request.data, request.length);
394 /* zero this out */
395 data_blob_free(&state->encrypted_session_key);
396 auth_flags = 0;
398 /* Try again with a shorter string (Win9X truncates this packet) */
399 if (ntlmssp_state->unicode) {
400 parse_string = "CdBBUUU";
401 } else {
402 parse_string = "CdBBAAA";
405 /* now the NTLMSSP encoded auth hashes */
406 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
407 "NTLMSSP",
408 &ntlmssp_command,
409 &ntlmssp_state->lm_resp,
410 &ntlmssp_state->nt_resp,
411 &ntlmssp_state->domain,
412 &ntlmssp_state->user,
413 &ntlmssp_state->client.netbios_name)) {
414 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
415 dump_data(2, request.data, request.length);
417 return NT_STATUS_INVALID_PARAMETER;
421 talloc_steal(state, state->encrypted_session_key.data);
423 if (auth_flags != 0) {
424 nt_status = ntlmssp_handle_neg_flags(ntlmssp_state,
425 auth_flags,
426 "authenticate");
427 if (!NT_STATUS_IS_OK(nt_status)){
428 return nt_status;
432 if (DEBUGLEVEL >= 10) {
433 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
434 ntlmssp_state, struct AUTHENTICATE_MESSAGE);
435 if (authenticate != NULL) {
436 NTSTATUS status;
437 authenticate->NegotiateFlags = auth_flags;
438 status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
439 &request, authenticate, authenticate);
440 if (NT_STATUS_IS_OK(status)) {
441 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
442 authenticate);
444 TALLOC_FREE(authenticate);
448 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
449 ntlmssp_state->user, ntlmssp_state->domain,
450 ntlmssp_state->client.netbios_name,
451 (unsigned long)ntlmssp_state->lm_resp.length,
452 (unsigned long)ntlmssp_state->nt_resp.length));
454 #if 0
455 file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
456 file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
457 #endif
459 if (ntlmssp_state->nt_resp.length > 24) {
460 struct NTLMv2_RESPONSE v2_resp;
461 enum ndr_err_code err;
462 uint32_t i = 0;
463 uint32_t count = 0;
464 const struct AV_PAIR *flags = NULL;
465 const struct AV_PAIR *eol = NULL;
466 uint32_t av_flags = 0;
468 err = ndr_pull_struct_blob(&ntlmssp_state->nt_resp,
469 ntlmssp_state,
470 &v2_resp,
471 (ndr_pull_flags_fn_t)ndr_pull_NTLMv2_RESPONSE);
472 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
473 nt_status = ndr_map_error2ntstatus(err);
474 DEBUG(1,("%s: failed to parse NTLMv2_RESPONSE of length %zu for "
475 "user=[%s] domain=[%s] workstation=[%s] - %s %s\n",
476 __func__, ntlmssp_state->nt_resp.length,
477 ntlmssp_state->user, ntlmssp_state->domain,
478 ntlmssp_state->client.netbios_name,
479 ndr_errstr(err), nt_errstr(nt_status)));
480 return nt_status;
483 if (DEBUGLVL(10)) {
484 NDR_PRINT_DEBUG(NTLMv2_RESPONSE, &v2_resp);
487 eol = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
488 MsvAvEOL);
489 if (eol == NULL) {
490 DEBUG(1,("%s: missing MsvAvEOL for "
491 "user=[%s] domain=[%s] workstation=[%s]\n",
492 __func__, ntlmssp_state->user, ntlmssp_state->domain,
493 ntlmssp_state->client.netbios_name));
494 return NT_STATUS_INVALID_PARAMETER;
497 flags = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
498 MsvAvFlags);
499 if (flags != NULL) {
500 av_flags = flags->Value.AvFlags;
503 if (av_flags & NTLMSSP_AVFLAG_MIC_IN_AUTHENTICATE_MESSAGE) {
504 if (mic_blob.length != NTLMSSP_MIC_SIZE) {
505 DEBUG(1,("%s: mic_blob.length[%u] for "
506 "user=[%s] domain=[%s] workstation=[%s]\n",
507 __func__,
508 (unsigned)mic_blob.length,
509 ntlmssp_state->user,
510 ntlmssp_state->domain,
511 ntlmssp_state->client.netbios_name));
512 return NT_STATUS_INVALID_PARAMETER;
515 if (request.length <
516 (NTLMSSP_MIC_OFFSET + NTLMSSP_MIC_SIZE))
518 DEBUG(1,("%s: missing MIC "
519 "request.length[%u] for "
520 "user=[%s] domain=[%s] workstation=[%s]\n",
521 __func__,
522 (unsigned)request.length,
523 ntlmssp_state->user,
524 ntlmssp_state->domain,
525 ntlmssp_state->client.netbios_name));
526 return NT_STATUS_INVALID_PARAMETER;
529 ntlmssp_state->new_spnego = true;
532 count = ntlmssp_state->server.av_pair_list.count;
533 if (v2_resp.Challenge.AvPairs.count < count) {
534 return NT_STATUS_INVALID_PARAMETER;
537 for (i = 0; i < count; i++) {
538 const struct AV_PAIR *sp =
539 &ntlmssp_state->server.av_pair_list.pair[i];
540 const struct AV_PAIR *cp = NULL;
542 if (sp->AvId == MsvAvEOL) {
543 continue;
546 cp = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
547 sp->AvId);
548 if (cp == NULL) {
549 DEBUG(1,("%s: AvId 0x%x missing for"
550 "user=[%s] domain=[%s] "
551 "workstation=[%s]\n",
552 __func__,
553 (unsigned)sp->AvId,
554 ntlmssp_state->user,
555 ntlmssp_state->domain,
556 ntlmssp_state->client.netbios_name));
557 return NT_STATUS_INVALID_PARAMETER;
560 switch (cp->AvId) {
561 #define CASE_STRING(v) case Msv ## v: do { \
562 int cmp; \
563 if (sp->Value.v == NULL) { \
564 return NT_STATUS_INTERNAL_ERROR; \
566 if (cp->Value.v == NULL) { \
567 DEBUG(1,("%s: invalid %s " \
568 "got[%s] expect[%s] for " \
569 "user=[%s] domain=[%s] workstation=[%s]\n", \
570 __func__, #v, \
571 cp->Value.v, \
572 sp->Value.v, \
573 ntlmssp_state->user, \
574 ntlmssp_state->domain, \
575 ntlmssp_state->client.netbios_name)); \
576 return NT_STATUS_INVALID_PARAMETER; \
578 cmp = strcmp(cp->Value.v, sp->Value.v); \
579 if (cmp != 0) { \
580 DEBUG(1,("%s: invalid %s " \
581 "got[%s] expect[%s] for " \
582 "user=[%s] domain=[%s] workstation=[%s]\n", \
583 __func__, #v, \
584 cp->Value.v, \
585 sp->Value.v, \
586 ntlmssp_state->user, \
587 ntlmssp_state->domain, \
588 ntlmssp_state->client.netbios_name)); \
589 return NT_STATUS_INVALID_PARAMETER; \
591 } while(0); break
592 CASE_STRING(AvNbComputerName);
593 CASE_STRING(AvNbDomainName);
594 CASE_STRING(AvDnsComputerName);
595 CASE_STRING(AvDnsDomainName);
596 CASE_STRING(AvDnsTreeName);
597 case MsvAvTimestamp:
598 if (cp->Value.AvTimestamp != sp->Value.AvTimestamp) {
599 struct timeval ct;
600 struct timeval st;
601 struct timeval_buf tmp1;
602 struct timeval_buf tmp2;
604 nttime_to_timeval(&ct,
605 cp->Value.AvTimestamp);
606 nttime_to_timeval(&st,
607 sp->Value.AvTimestamp);
609 DEBUG(1,("%s: invalid AvTimestamp "
610 "got[%s] expect[%s] for "
611 "user=[%s] domain=[%s] "
612 "workstation=[%s]\n",
613 __func__,
614 timeval_str_buf(&ct, false,
615 true, &tmp1),
616 timeval_str_buf(&st, false,
617 true, &tmp2),
618 ntlmssp_state->user,
619 ntlmssp_state->domain,
620 ntlmssp_state->client.netbios_name));
621 return NT_STATUS_INVALID_PARAMETER;
623 break;
624 default:
626 * This can't happen as we control
627 * ntlmssp_state->server.av_pair_list
629 return NT_STATUS_INTERNAL_ERROR;
634 nttime_to_timeval(&endtime, ntlmssp_state->server.challenge_endtime);
635 expired = timeval_expired(&endtime);
636 if (expired) {
637 struct timeval_buf tmp;
638 DEBUG(1,("%s: challenge invalid (expired %s) for "
639 "user=[%s] domain=[%s] workstation=[%s]\n",
640 __func__,
641 timeval_str_buf(&endtime, false, true, &tmp),
642 ntlmssp_state->user, ntlmssp_state->domain,
643 ntlmssp_state->client.netbios_name));
644 return NT_STATUS_INVALID_PARAMETER;
647 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
648 client challenge
650 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
652 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
653 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
654 MD5_CTX md5_session_nonce_ctx;
655 state->doing_ntlm2 = true;
657 memcpy(state->session_nonce, ntlmssp_state->internal_chal.data, 8);
658 memcpy(&state->session_nonce[8], ntlmssp_state->lm_resp.data, 8);
660 SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
662 MD5Init(&md5_session_nonce_ctx);
663 MD5Update(&md5_session_nonce_ctx, state->session_nonce, 16);
664 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
666 /* LM response is no longer useful */
667 data_blob_free(&ntlmssp_state->lm_resp);
669 /* We changed the effective challenge - set it */
670 if (auth_context->set_ntlm_challenge) {
671 nt_status = auth_context->set_ntlm_challenge(auth_context,
672 session_nonce_hash,
673 "NTLMSSP callback (NTLM2)");
674 if (!NT_STATUS_IS_OK(nt_status)) {
675 DEBUG(1, ("gensec_ntlmssp_server_negotiate: failed to get challenge: %s\n",
676 nt_errstr(nt_status)));
677 return nt_status;
679 } else {
680 DEBUG(1, ("gensec_ntlmssp_server_negotiate: backend doesn't have facility for challenge to be set\n"));
682 return NT_STATUS_NOT_IMPLEMENTED;
685 /* LM Key is incompatible. */
686 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
689 return NT_STATUS_OK;
693 * Check the password on an NTLMSSP login.
695 * Return the session keys used on the connection.
698 static NTSTATUS ntlmssp_server_check_password(struct gensec_security *gensec_security,
699 struct gensec_ntlmssp_context *gensec_ntlmssp,
700 TALLOC_CTX *mem_ctx,
701 DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
703 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
704 struct auth4_context *auth_context = gensec_security->auth_context;
705 NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
706 struct auth_session_info *session_info = NULL;
707 struct auth_usersupplied_info *user_info;
709 user_info = talloc_zero(ntlmssp_state, struct auth_usersupplied_info);
710 if (!user_info) {
711 return NT_STATUS_NO_MEMORY;
714 user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
715 user_info->flags = 0;
716 user_info->mapped_state = false;
717 user_info->client.account_name = ntlmssp_state->user;
718 user_info->client.domain_name = ntlmssp_state->domain;
719 user_info->workstation_name = ntlmssp_state->client.netbios_name;
720 user_info->remote_host = gensec_get_remote_address(gensec_security);
721 user_info->local_host = gensec_get_local_address(gensec_security);
722 user_info->service_description
723 = gensec_get_target_service_description(gensec_security);
726 * This will just be the string "NTLMSSP" from
727 * gensec_ntlmssp_final_auth_type, but ensures it stays in sync
728 * with the same use in the authorization logging triggered by
729 * gensec_session_info() later
731 user_info->auth_description = gensec_final_auth_type(gensec_security);
733 user_info->password_state = AUTH_PASSWORD_RESPONSE;
734 user_info->password.response.lanman = ntlmssp_state->lm_resp;
735 user_info->password.response.lanman.data = talloc_steal(user_info, ntlmssp_state->lm_resp.data);
736 user_info->password.response.nt = ntlmssp_state->nt_resp;
737 user_info->password.response.nt.data = talloc_steal(user_info, ntlmssp_state->nt_resp.data);
739 if (auth_context->check_ntlm_password) {
740 uint8_t authoritative = 0;
742 nt_status = auth_context->check_ntlm_password(auth_context,
743 gensec_ntlmssp,
744 user_info,
745 &authoritative,
746 &gensec_ntlmssp->server_returned_info,
747 user_session_key, lm_session_key);
750 if (!NT_STATUS_IS_OK(nt_status)) {
751 DEBUG(5, (__location__ ": Checking NTLMSSP password for %s\\%s failed: %s\n", user_info->client.domain_name, user_info->client.account_name, nt_errstr(nt_status)));
753 TALLOC_FREE(user_info);
755 NT_STATUS_NOT_OK_RETURN(nt_status);
757 if (lpcfg_map_to_guest(gensec_security->settings->lp_ctx) != NEVER_MAP_TO_GUEST
758 && auth_context->generate_session_info != NULL)
760 NTSTATUS tmp_status;
763 * We need to check if the auth is anonymous or mapped to guest
765 tmp_status = auth_context->generate_session_info(auth_context, mem_ctx,
766 gensec_ntlmssp->server_returned_info,
767 gensec_ntlmssp->ntlmssp_state->user,
768 AUTH_SESSION_INFO_SIMPLE_PRIVILEGES,
769 &session_info);
770 if (!NT_STATUS_IS_OK(tmp_status)) {
772 * We don't care about failures,
773 * the worst result is that we try MIC checking
774 * for a map to guest authentication.
776 TALLOC_FREE(session_info);
780 if (session_info != NULL) {
781 if (security_session_user_level(session_info, NULL) < SECURITY_USER) {
783 * Anonymous and GUEST are not secure anyway.
784 * avoid new_spnego and MIC checking.
786 ntlmssp_state->new_spnego = false;
787 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
788 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SEAL;
790 TALLOC_FREE(session_info);
793 talloc_steal(mem_ctx, user_session_key->data);
794 talloc_steal(mem_ctx, lm_session_key->data);
796 return nt_status;
800 * Next state function for the Authenticate packet
801 * (after authentication - figures out the session keys etc)
803 * @param ntlmssp_state NTLMSSP State
804 * @return Errors or NT_STATUS_OK.
807 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security,
808 struct gensec_ntlmssp_context *gensec_ntlmssp,
809 struct ntlmssp_server_auth_state *state,
810 DATA_BLOB request)
812 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
813 DATA_BLOB user_session_key = state->user_session_key;
814 DATA_BLOB lm_session_key = state->lm_session_key;
815 NTSTATUS nt_status = NT_STATUS_OK;
816 DATA_BLOB session_key = data_blob(NULL, 0);
818 dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
819 dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
821 /* Handle the different session key derivation for NTLM2 */
822 if (state->doing_ntlm2) {
823 if (user_session_key.data && user_session_key.length == 16) {
824 session_key = data_blob_talloc(ntlmssp_state,
825 NULL, 16);
826 hmac_md5(user_session_key.data, state->session_nonce,
827 sizeof(state->session_nonce), session_key.data);
828 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
829 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
831 } else {
832 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
833 session_key = data_blob_null;
835 } else if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
836 /* Ensure we can never get here on NTLMv2 */
837 && (ntlmssp_state->nt_resp.length == 0 || ntlmssp_state->nt_resp.length == 24)) {
839 if (lm_session_key.data && lm_session_key.length >= 8) {
840 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
841 session_key = data_blob_talloc(ntlmssp_state,
842 NULL, 16);
843 if (session_key.data == NULL) {
844 return NT_STATUS_NO_MEMORY;
846 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
847 session_key.data);
848 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
849 } else {
850 static const uint8_t zeros[24] = {0, };
851 session_key = data_blob_talloc(
852 ntlmssp_state, NULL, 16);
853 if (session_key.data == NULL) {
854 return NT_STATUS_NO_MEMORY;
856 SMBsesskeygen_lm_sess_key(zeros, zeros,
857 session_key.data);
858 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
860 dump_data_pw("LM session key:\n", session_key.data,
861 session_key.length);
862 } else {
863 /* LM Key not selected */
864 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
866 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
867 session_key = data_blob_null;
870 } else if (user_session_key.data) {
871 session_key = user_session_key;
872 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
873 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
875 /* LM Key not selected */
876 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
878 } else if (lm_session_key.data) {
879 /* Very weird to have LM key, but no user session key, but anyway.. */
880 session_key = lm_session_key;
881 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
882 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
884 /* LM Key not selected */
885 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
887 } else {
888 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
889 session_key = data_blob_null;
891 /* LM Key not selected */
892 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
895 /* With KEY_EXCH, the client supplies the proposed session key,
896 but encrypts it with the long-term key */
897 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
898 if (!state->encrypted_session_key.data
899 || state->encrypted_session_key.length != 16) {
900 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
901 (unsigned)state->encrypted_session_key.length));
902 return NT_STATUS_INVALID_PARAMETER;
903 } else if (!session_key.data || session_key.length != 16) {
904 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
905 (unsigned int)session_key.length));
906 ntlmssp_state->session_key = session_key;
907 talloc_steal(ntlmssp_state, session_key.data);
908 } else {
909 dump_data_pw("KEY_EXCH session key (enc):\n",
910 state->encrypted_session_key.data,
911 state->encrypted_session_key.length);
912 arcfour_crypt(state->encrypted_session_key.data,
913 session_key.data,
914 state->encrypted_session_key.length);
915 ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state,
916 state->encrypted_session_key.data,
917 state->encrypted_session_key.length);
918 dump_data_pw("KEY_EXCH session key:\n",
919 state->encrypted_session_key.data,
920 state->encrypted_session_key.length);
922 } else {
923 ntlmssp_state->session_key = session_key;
924 talloc_steal(ntlmssp_state, session_key.data);
927 if (ntlmssp_state->new_spnego) {
928 HMACMD5Context ctx;
929 uint8_t mic_buffer[NTLMSSP_MIC_SIZE] = { 0, };
930 int cmp;
932 hmac_md5_init_limK_to_64(ntlmssp_state->session_key.data,
933 ntlmssp_state->session_key.length,
934 &ctx);
936 hmac_md5_update(ntlmssp_state->negotiate_blob.data,
937 ntlmssp_state->negotiate_blob.length,
938 &ctx);
939 hmac_md5_update(ntlmssp_state->challenge_blob.data,
940 ntlmssp_state->challenge_blob.length,
941 &ctx);
943 /* checked were we set ntlmssp_state->new_spnego */
944 SMB_ASSERT(request.length >
945 (NTLMSSP_MIC_OFFSET + NTLMSSP_MIC_SIZE));
947 hmac_md5_update(request.data, NTLMSSP_MIC_OFFSET, &ctx);
948 hmac_md5_update(mic_buffer, NTLMSSP_MIC_SIZE, &ctx);
949 hmac_md5_update(request.data +
950 (NTLMSSP_MIC_OFFSET + NTLMSSP_MIC_SIZE),
951 request.length -
952 (NTLMSSP_MIC_OFFSET + NTLMSSP_MIC_SIZE),
953 &ctx);
954 hmac_md5_final(mic_buffer, &ctx);
956 cmp = memcmp(request.data + NTLMSSP_MIC_OFFSET,
957 mic_buffer, NTLMSSP_MIC_SIZE);
958 if (cmp != 0) {
959 DEBUG(1,("%s: invalid NTLMSSP_MIC for "
960 "user=[%s] domain=[%s] workstation=[%s]\n",
961 __func__,
962 ntlmssp_state->user,
963 ntlmssp_state->domain,
964 ntlmssp_state->client.netbios_name));
965 dump_data(1, request.data + NTLMSSP_MIC_OFFSET,
966 NTLMSSP_MIC_SIZE);
967 dump_data(1, mic_buffer,
968 NTLMSSP_MIC_SIZE);
969 return NT_STATUS_INVALID_PARAMETER;
973 data_blob_free(&ntlmssp_state->negotiate_blob);
974 data_blob_free(&ntlmssp_state->challenge_blob);
976 if (gensec_ntlmssp_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
977 nt_status = ntlmssp_sign_init(ntlmssp_state);
980 ntlmssp_state->expected_state = NTLMSSP_DONE;
982 return nt_status;
987 * Next state function for the NTLMSSP Authenticate packet
989 * @param gensec_security GENSEC state
990 * @param out_mem_ctx Memory context for *out
991 * @param in The request, as a DATA_BLOB. reply.data must be NULL
992 * @param out The reply, as an allocated DATA_BLOB, caller to free.
993 * @return Errors or NT_STATUS_OK if authentication sucessful
996 NTSTATUS gensec_ntlmssp_server_auth(struct gensec_security *gensec_security,
997 TALLOC_CTX *out_mem_ctx,
998 const DATA_BLOB in, DATA_BLOB *out)
1000 struct gensec_ntlmssp_context *gensec_ntlmssp =
1001 talloc_get_type_abort(gensec_security->private_data,
1002 struct gensec_ntlmssp_context);
1003 struct ntlmssp_server_auth_state *state;
1004 NTSTATUS nt_status;
1006 /* zero the outbound NTLMSSP packet */
1007 *out = data_blob_null;
1009 state = talloc_zero(gensec_ntlmssp, struct ntlmssp_server_auth_state);
1010 if (state == NULL) {
1011 return NT_STATUS_NO_MEMORY;
1014 nt_status = ntlmssp_server_preauth(gensec_security, gensec_ntlmssp, state, in);
1015 if (!NT_STATUS_IS_OK(nt_status)) {
1016 TALLOC_FREE(state);
1017 return nt_status;
1021 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
1022 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
1023 * smb.conf file) and no NTLMv2 response was sent then the password check
1024 * will fail here. JRA.
1027 /* Finally, actually ask if the password is OK */
1028 nt_status = ntlmssp_server_check_password(gensec_security, gensec_ntlmssp,
1029 state,
1030 &state->user_session_key,
1031 &state->lm_session_key);
1032 if (!NT_STATUS_IS_OK(nt_status)) {
1033 TALLOC_FREE(state);
1034 return nt_status;
1037 /* When we get more async in the auth code behind
1038 ntlmssp_state->check_password, the ntlmssp_server_postpath
1039 can be done in a callback */
1041 nt_status = ntlmssp_server_postauth(gensec_security, gensec_ntlmssp, state, in);
1042 TALLOC_FREE(state);
1043 return nt_status;