s3: Remove some calls to memset -- reduces text size by some bytes for me
[Samba/fernandojvsilva.git] / source3 / libsmb / ntlmssp.c
blob1d20ee5026dd6d4b3f7410749f4dcff75779d76c
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 handle NLTMSSP, server side
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett 2001-2003
8 Copyright (C) Andrew Bartlett 2005 (Updated from gensec).
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 "ntlmssp.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
28 #include "../libcli/auth/ntlmssp_ndr.h"
30 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
31 DATA_BLOB reply, DATA_BLOB *next_request);
32 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
33 const DATA_BLOB in, DATA_BLOB *out);
34 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
35 const DATA_BLOB reply, DATA_BLOB *next_request);
36 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
37 const DATA_BLOB request, DATA_BLOB *reply);
39 /**
40 * Callbacks for NTLMSSP - for both client and server operating modes
44 static const struct ntlmssp_callbacks {
45 enum ntlmssp_role role;
46 enum ntlmssp_message_type ntlmssp_command;
47 NTSTATUS (*fn)(struct ntlmssp_state *ntlmssp_state,
48 DATA_BLOB in, DATA_BLOB *out);
49 } ntlmssp_callbacks[] = {
50 {NTLMSSP_CLIENT, NTLMSSP_INITIAL, ntlmssp_client_initial},
51 {NTLMSSP_SERVER, NTLMSSP_NEGOTIATE, ntlmssp_server_negotiate},
52 {NTLMSSP_CLIENT, NTLMSSP_CHALLENGE, ntlmssp_client_challenge},
53 {NTLMSSP_SERVER, NTLMSSP_AUTH, ntlmssp_server_auth},
54 {NTLMSSP_CLIENT, NTLMSSP_UNKNOWN, NULL},
55 {NTLMSSP_SERVER, NTLMSSP_UNKNOWN, NULL}
59 /**
60 * Print out the NTLMSSP flags for debugging
61 * @param neg_flags The flags from the packet
64 void debug_ntlmssp_flags(uint32 neg_flags)
66 DEBUG(3,("Got NTLMSSP neg_flags=0x%08x\n", neg_flags));
68 if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE)
69 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_UNICODE\n"));
70 if (neg_flags & NTLMSSP_NEGOTIATE_OEM)
71 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM\n"));
72 if (neg_flags & NTLMSSP_REQUEST_TARGET)
73 DEBUGADD(4, (" NTLMSSP_REQUEST_TARGET\n"));
74 if (neg_flags & NTLMSSP_NEGOTIATE_SIGN)
75 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SIGN\n"));
76 if (neg_flags & NTLMSSP_NEGOTIATE_SEAL)
77 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SEAL\n"));
78 if (neg_flags & NTLMSSP_NEGOTIATE_DATAGRAM)
79 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_DATAGRAM\n"));
80 if (neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
81 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_LM_KEY\n"));
82 if (neg_flags & NTLMSSP_NEGOTIATE_NETWARE)
83 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NETWARE\n"));
84 if (neg_flags & NTLMSSP_NEGOTIATE_NTLM)
85 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM\n"));
86 if (neg_flags & NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED)
87 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED\n"));
88 if (neg_flags & NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED)
89 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED\n"));
90 if (neg_flags & NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL)
91 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL\n"));
92 if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
93 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_ALWAYS_SIGN\n"));
94 if (neg_flags & NTLMSSP_REQUEST_NON_NT_SESSION_KEY)
95 DEBUGADD(4, (" NTLMSSP_REQUEST_NON_NT_SESSION_KEY\n"));
96 if (neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
97 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM2\n"));
98 if (neg_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
99 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_TARGET_INFO\n"));
100 if (neg_flags & NTLMSSP_NEGOTIATE_VERSION)
101 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_VERSION\n"));
102 if (neg_flags & NTLMSSP_NEGOTIATE_128)
103 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_128\n"));
104 if (neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)
105 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_KEY_EXCH\n"));
106 if (neg_flags & NTLMSSP_NEGOTIATE_56)
107 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_56\n"));
111 * Default challenge generation code.
115 static NTSTATUS get_challenge(const struct ntlmssp_state *ntlmssp_state,
116 uint8_t chal[8])
118 generate_random_buffer(chal, 8);
119 return NT_STATUS_OK;
123 * Default 'we can set the challenge to anything we like' implementation
127 static bool may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
129 return True;
133 * Default 'we can set the challenge to anything we like' implementation
135 * Does not actually do anything, as the value is always in the structure anyway.
139 static NTSTATUS set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
141 SMB_ASSERT(challenge->length == 8);
142 return NT_STATUS_OK;
146 * Set a username on an NTLMSSP context - ensures it is talloc()ed
150 NTSTATUS ntlmssp_set_username(struct ntlmssp_state *ntlmssp_state, const char *user)
152 ntlmssp_state->user = talloc_strdup(ntlmssp_state, user ? user : "" );
153 if (!ntlmssp_state->user) {
154 return NT_STATUS_NO_MEMORY;
156 return NT_STATUS_OK;
160 * Store NT and LM hashes on an NTLMSSP context - ensures they are talloc()ed
163 NTSTATUS ntlmssp_set_hashes(struct ntlmssp_state *ntlmssp_state,
164 const unsigned char lm_hash[16],
165 const unsigned char nt_hash[16])
167 ntlmssp_state->lm_hash = (unsigned char *)
168 TALLOC_MEMDUP(ntlmssp_state, lm_hash, 16);
169 ntlmssp_state->nt_hash = (unsigned char *)
170 TALLOC_MEMDUP(ntlmssp_state, nt_hash, 16);
171 if (!ntlmssp_state->lm_hash || !ntlmssp_state->nt_hash) {
172 TALLOC_FREE(ntlmssp_state->lm_hash);
173 TALLOC_FREE(ntlmssp_state->nt_hash);
174 return NT_STATUS_NO_MEMORY;
176 return NT_STATUS_OK;
180 * Converts a password to the hashes on an NTLMSSP context.
183 NTSTATUS ntlmssp_set_password(struct ntlmssp_state *ntlmssp_state, const char *password)
185 if (!password) {
186 ntlmssp_state->lm_hash = NULL;
187 ntlmssp_state->nt_hash = NULL;
188 } else {
189 unsigned char lm_hash[16];
190 unsigned char nt_hash[16];
192 E_deshash(password, lm_hash);
193 E_md4hash(password, nt_hash);
194 return ntlmssp_set_hashes(ntlmssp_state, lm_hash, nt_hash);
196 return NT_STATUS_OK;
200 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
203 NTSTATUS ntlmssp_set_domain(struct ntlmssp_state *ntlmssp_state, const char *domain)
205 ntlmssp_state->domain = talloc_strdup(ntlmssp_state,
206 domain ? domain : "" );
207 if (!ntlmssp_state->domain) {
208 return NT_STATUS_NO_MEMORY;
210 return NT_STATUS_OK;
214 * Set a workstation on an NTLMSSP context - ensures it is talloc()ed
217 NTSTATUS ntlmssp_set_workstation(struct ntlmssp_state *ntlmssp_state, const char *workstation)
219 ntlmssp_state->workstation = talloc_strdup(ntlmssp_state, workstation);
220 if (!ntlmssp_state->workstation) {
221 return NT_STATUS_NO_MEMORY;
223 return NT_STATUS_OK;
227 * Request features for the NTLMSSP negotiation
229 * @param ntlmssp_state NTLMSSP state
230 * @param feature_list List of space seperated features requested from NTLMSSP.
232 void ntlmssp_want_feature_list(struct ntlmssp_state *ntlmssp_state, char *feature_list)
235 * We need to set this to allow a later SetPassword
236 * via the SAMR pipe to succeed. Strange.... We could
237 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
239 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
240 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
242 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
243 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
245 if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
246 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
251 * Request a feature for the NTLMSSP negotiation
253 * @param ntlmssp_state NTLMSSP state
254 * @param feature Bit flag specifying the requested feature
256 void ntlmssp_want_feature(struct ntlmssp_state *ntlmssp_state, uint32 feature)
258 /* As per JRA's comment above */
259 if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
260 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
262 if (feature & NTLMSSP_FEATURE_SIGN) {
263 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
265 if (feature & NTLMSSP_FEATURE_SEAL) {
266 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
271 * Next state function for the NTLMSSP state machine
273 * @param ntlmssp_state NTLMSSP State
274 * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
275 * @param out The reply, as an allocated DATA_BLOB, caller to free.
276 * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
279 NTSTATUS ntlmssp_update(struct ntlmssp_state *ntlmssp_state,
280 const DATA_BLOB input, DATA_BLOB *out)
282 uint32 ntlmssp_command;
283 int i;
285 if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
286 /* Called update after negotiations finished. */
287 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
288 return NT_STATUS_INVALID_PARAMETER;
291 *out = data_blob_null;
293 if (!input.length) {
294 switch (ntlmssp_state->role) {
295 case NTLMSSP_CLIENT:
296 ntlmssp_command = NTLMSSP_INITIAL;
297 break;
298 case NTLMSSP_SERVER:
299 /* 'datagram' mode - no neg packet */
300 ntlmssp_command = NTLMSSP_NEGOTIATE;
301 break;
303 } else {
304 if (!msrpc_parse(ntlmssp_state, &input, "Cd",
305 "NTLMSSP",
306 &ntlmssp_command)) {
307 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
308 dump_data(2, input.data, input.length);
309 return NT_STATUS_INVALID_PARAMETER;
313 if (ntlmssp_command != ntlmssp_state->expected_state) {
314 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
315 return NT_STATUS_INVALID_PARAMETER;
318 for (i=0; ntlmssp_callbacks[i].fn; i++) {
319 if (ntlmssp_callbacks[i].role == ntlmssp_state->role
320 && ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
321 return ntlmssp_callbacks[i].fn(ntlmssp_state, input, out);
325 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
326 ntlmssp_state->role, ntlmssp_command));
328 return NT_STATUS_INVALID_PARAMETER;
332 * End an NTLMSSP state machine
334 * @param ntlmssp_state NTLMSSP State, free()ed by this function
337 void ntlmssp_end(struct ntlmssp_state **ntlmssp_state)
339 data_blob_free(&(*ntlmssp_state)->chal);
340 data_blob_free(&(*ntlmssp_state)->lm_resp);
341 data_blob_free(&(*ntlmssp_state)->nt_resp);
342 TALLOC_FREE(*ntlmssp_state);
344 *ntlmssp_state = NULL;
345 return;
349 * Determine correct target name flags for reply, given server role
350 * and negotiated flags
352 * @param ntlmssp_state NTLMSSP State
353 * @param neg_flags The flags from the packet
354 * @param chal_flags The flags to be set in the reply packet
355 * @return The 'target name' string.
358 static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
359 uint32 neg_flags, uint32 *chal_flags)
361 if (neg_flags & NTLMSSP_REQUEST_TARGET) {
362 *chal_flags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
363 *chal_flags |= NTLMSSP_REQUEST_TARGET;
364 if (ntlmssp_state->server_role == ROLE_STANDALONE) {
365 *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
366 return ntlmssp_state->get_global_myname();
367 } else {
368 *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
369 return ntlmssp_state->get_domain();
371 } else {
372 return "";
376 static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
377 uint32 neg_flags, bool allow_lm) {
378 if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
379 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
380 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_OEM;
381 ntlmssp_state->unicode = True;
382 } else {
383 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_UNICODE;
384 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
385 ntlmssp_state->unicode = False;
388 if ((neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) && allow_lm) {
389 /* other end forcing us to use LM */
390 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
391 ntlmssp_state->use_ntlmv2 = False;
392 } else {
393 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
396 if (!(neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
397 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
400 if (!(neg_flags & NTLMSSP_NEGOTIATE_NTLM2)) {
401 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
404 if (!(neg_flags & NTLMSSP_NEGOTIATE_128)) {
405 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_128;
408 if (!(neg_flags & NTLMSSP_NEGOTIATE_56)) {
409 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_56;
412 if (!(neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
413 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_KEY_EXCH;
416 if (!(neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
417 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
420 if (!(neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
421 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SEAL;
424 /* Woop Woop - unknown flag for Windows compatibility...
425 What does this really do ? JRA. */
426 if (!(neg_flags & NTLMSSP_NEGOTIATE_VERSION)) {
427 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_VERSION;
430 if ((neg_flags & NTLMSSP_REQUEST_TARGET)) {
431 ntlmssp_state->neg_flags |= NTLMSSP_REQUEST_TARGET;
436 Weaken NTLMSSP keys to cope with down-level clients and servers.
438 We probably should have some parameters to control this, but as
439 it only occours for LM_KEY connections, and this is controlled
440 by the client lanman auth/lanman auth parameters, it isn't too bad.
443 DATA_BLOB ntlmssp_weaken_keys(struct ntlmssp_state *ntlmssp_state, TALLOC_CTX *mem_ctx)
445 DATA_BLOB weakened_key = data_blob_talloc(mem_ctx,
446 ntlmssp_state->session_key.data,
447 ntlmssp_state->session_key.length);
449 /* Nothing to weaken. We certainly don't want to 'extend' the length... */
450 if (weakened_key.length < 16) {
451 /* perhaps there was no key? */
452 return weakened_key;
455 /* Key weakening not performed on the master key for NTLM2
456 and does not occour for NTLM1. Therefore we only need
457 to do this for the LM_KEY.
460 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
461 /* LM key doesn't support 128 bit crypto, so this is
462 * the best we can do. If you negotiate 128 bit, but
463 * not 56, you end up with 40 bit... */
464 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
465 weakened_key.data[7] = 0xa0;
466 } else { /* forty bits */
467 weakened_key.data[5] = 0xe5;
468 weakened_key.data[6] = 0x38;
469 weakened_key.data[7] = 0xb0;
471 weakened_key.length = 8;
473 return weakened_key;
477 * Next state function for the Negotiate packet
479 * @param ntlmssp_state NTLMSSP State
480 * @param request The request, as a DATA_BLOB
481 * @param request The reply, as an allocated DATA_BLOB, caller to free.
482 * @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent.
485 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
486 const DATA_BLOB request, DATA_BLOB *reply)
488 DATA_BLOB struct_blob;
489 const char *dnsname;
490 char *dnsdomname = NULL;
491 uint32 neg_flags = 0;
492 uint32 ntlmssp_command, chal_flags;
493 uint8_t cryptkey[8];
494 const char *target_name;
495 struct NEGOTIATE_MESSAGE negotiate;
496 struct CHALLENGE_MESSAGE challenge;
497 NTSTATUS status;
499 /* parse the NTLMSSP packet */
500 #if 0
501 file_save("ntlmssp_negotiate.dat", request.data, request.length);
502 #endif
504 if (request.length) {
505 if ((request.length < 16) || !msrpc_parse(ntlmssp_state, &request, "Cdd",
506 "NTLMSSP",
507 &ntlmssp_command,
508 &neg_flags)) {
509 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
510 (unsigned int)request.length));
511 dump_data(2, request.data, request.length);
512 return NT_STATUS_INVALID_PARAMETER;
514 debug_ntlmssp_flags(neg_flags);
516 if (DEBUGLEVEL >= 10) {
517 if (NT_STATUS_IS_OK(ntlmssp_pull_NEGOTIATE_MESSAGE(&request,
518 ntlmssp_state,
519 NULL,
520 &negotiate)))
522 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE, &negotiate);
527 ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, lp_lanman_auth());
529 /* Ask our caller what challenge they would like in the packet */
530 status = ntlmssp_state->get_challenge(ntlmssp_state, cryptkey);
531 if (!NT_STATUS_IS_OK(status)) {
532 return status;
535 /* Check if we may set the challenge */
536 if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
537 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
540 /* The flags we send back are not just the negotiated flags,
541 * they are also 'what is in this packet'. Therfore, we
542 * operate on 'chal_flags' from here on
545 chal_flags = ntlmssp_state->neg_flags;
547 /* get the right name to fill in as 'target' */
548 target_name = ntlmssp_target_name(ntlmssp_state,
549 neg_flags, &chal_flags);
550 if (target_name == NULL)
551 return NT_STATUS_INVALID_PARAMETER;
553 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
554 ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
555 cryptkey, 8);
557 /* This should be a 'netbios domain -> DNS domain' mapping */
558 dnsdomname = get_mydnsdomname(ntlmssp_state);
559 if (!dnsdomname) {
560 dnsdomname = talloc_strdup(ntlmssp_state, "");
562 if (!dnsdomname) {
563 return NT_STATUS_NO_MEMORY;
565 strlower_m(dnsdomname);
567 dnsname = get_mydnsfullname();
568 if (!dnsname) {
569 dnsname = "";
572 /* This creates the 'blob' of names that appears at the end of the packet */
573 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
575 msrpc_gen(ntlmssp_state, &struct_blob, "aaaaa",
576 MsvAvNbDomainName, target_name,
577 MsvAvNbComputerName, ntlmssp_state->get_global_myname(),
578 MsvAvDnsDomainName, dnsdomname,
579 MsvAvDnsComputerName, dnsname,
580 MsvAvEOL, "");
581 } else {
582 struct_blob = data_blob_null;
586 /* Marshel the packet in the right format, be it unicode or ASCII */
587 const char *gen_string;
588 if (ntlmssp_state->unicode) {
589 gen_string = "CdUdbddB";
590 } else {
591 gen_string = "CdAdbddB";
594 msrpc_gen(ntlmssp_state, reply, gen_string,
595 "NTLMSSP",
596 NTLMSSP_CHALLENGE,
597 target_name,
598 chal_flags,
599 cryptkey, 8,
600 0, 0,
601 struct_blob.data, struct_blob.length);
603 if (DEBUGLEVEL >= 10) {
604 if (NT_STATUS_IS_OK(ntlmssp_pull_CHALLENGE_MESSAGE(reply,
605 ntlmssp_state,
606 NULL,
607 &challenge)))
609 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE, &challenge);
614 data_blob_free(&struct_blob);
616 ntlmssp_state->expected_state = NTLMSSP_AUTH;
618 return NT_STATUS_MORE_PROCESSING_REQUIRED;
622 * Next state function for the Authenticate packet
624 * @param ntlmssp_state NTLMSSP State
625 * @param request The request, as a DATA_BLOB
626 * @param request The reply, as an allocated DATA_BLOB, caller to free.
627 * @return Errors or NT_STATUS_OK.
630 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
631 const DATA_BLOB request, DATA_BLOB *reply)
633 DATA_BLOB encrypted_session_key = data_blob_null;
634 DATA_BLOB user_session_key = data_blob_null;
635 DATA_BLOB lm_session_key = data_blob_null;
636 DATA_BLOB session_key = data_blob_null;
637 uint32 ntlmssp_command, auth_flags;
638 NTSTATUS nt_status = NT_STATUS_OK;
639 struct AUTHENTICATE_MESSAGE authenticate;
641 /* used by NTLM2 */
642 bool doing_ntlm2 = False;
644 uchar session_nonce[16];
645 uchar session_nonce_hash[16];
647 const char *parse_string;
649 /* parse the NTLMSSP packet */
650 *reply = data_blob_null;
652 #if 0
653 file_save("ntlmssp_auth.dat", request.data, request.length);
654 #endif
656 if (ntlmssp_state->unicode) {
657 parse_string = "CdBBUUUBd";
658 } else {
659 parse_string = "CdBBAAABd";
662 data_blob_free(&ntlmssp_state->lm_resp);
663 data_blob_free(&ntlmssp_state->nt_resp);
665 ntlmssp_state->user = NULL;
666 ntlmssp_state->domain = NULL;
667 ntlmssp_state->workstation = NULL;
669 /* now the NTLMSSP encoded auth hashes */
670 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
671 "NTLMSSP",
672 &ntlmssp_command,
673 &ntlmssp_state->lm_resp,
674 &ntlmssp_state->nt_resp,
675 &ntlmssp_state->domain,
676 &ntlmssp_state->user,
677 &ntlmssp_state->workstation,
678 &encrypted_session_key,
679 &auth_flags)) {
680 auth_flags = 0;
682 /* Try again with a shorter string (Win9X truncates this packet) */
683 if (ntlmssp_state->unicode) {
684 parse_string = "CdBBUUU";
685 } else {
686 parse_string = "CdBBAAA";
689 /* now the NTLMSSP encoded auth hashes */
690 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
691 "NTLMSSP",
692 &ntlmssp_command,
693 &ntlmssp_state->lm_resp,
694 &ntlmssp_state->nt_resp,
695 &ntlmssp_state->domain,
696 &ntlmssp_state->user,
697 &ntlmssp_state->workstation)) {
698 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
699 dump_data(2, request.data, request.length);
701 return NT_STATUS_INVALID_PARAMETER;
705 if (auth_flags)
706 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, lp_lanman_auth());
708 if (DEBUGLEVEL >= 10) {
709 if (NT_STATUS_IS_OK(ntlmssp_pull_AUTHENTICATE_MESSAGE(&request,
710 ntlmssp_state,
711 NULL,
712 &authenticate)))
714 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE, &authenticate);
718 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
719 ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
721 #if 0
722 file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
723 file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
724 #endif
726 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
727 client challenge
729 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
731 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
732 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
733 struct MD5Context md5_session_nonce_ctx;
734 SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
736 doing_ntlm2 = True;
738 memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
739 memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
741 MD5Init(&md5_session_nonce_ctx);
742 MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
743 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
745 ntlmssp_state->chal = data_blob_talloc(
746 ntlmssp_state, session_nonce_hash, 8);
748 /* LM response is no longer useful */
749 data_blob_free(&ntlmssp_state->lm_resp);
751 /* We changed the effective challenge - set it */
752 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
753 data_blob_free(&encrypted_session_key);
754 return nt_status;
757 /* LM Key is incompatible. */
758 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
763 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
764 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
765 * smb.conf file) and no NTLMv2 response was sent then the password check
766 * will fail here. JRA.
769 /* Finally, actually ask if the password is OK */
771 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
772 &user_session_key, &lm_session_key))) {
773 data_blob_free(&encrypted_session_key);
774 return nt_status;
777 dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
778 dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
780 /* Handle the different session key derivation for NTLM2 */
781 if (doing_ntlm2) {
782 if (user_session_key.data && user_session_key.length == 16) {
783 session_key = data_blob_talloc(ntlmssp_state,
784 NULL, 16);
785 hmac_md5(user_session_key.data, session_nonce,
786 sizeof(session_nonce), session_key.data);
787 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
788 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
790 } else {
791 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
792 session_key = data_blob_null;
794 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
795 if (lm_session_key.data && lm_session_key.length >= 8) {
796 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
797 session_key = data_blob_talloc(ntlmssp_state,
798 NULL, 16);
799 if (session_key.data == NULL) {
800 return NT_STATUS_NO_MEMORY;
802 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
803 session_key.data);
804 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
805 } else {
806 static const uint8 zeros[24] = {0, };
807 session_key = data_blob_talloc(
808 ntlmssp_state, NULL, 16);
809 if (session_key.data == NULL) {
810 return NT_STATUS_NO_MEMORY;
812 SMBsesskeygen_lm_sess_key(
813 lm_session_key.data, zeros,
814 session_key.data);
816 dump_data_pw("LM session key:\n", session_key.data,
817 session_key.length);
818 } else {
819 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
820 session_key = data_blob_null;
822 } else if (user_session_key.data) {
823 session_key = user_session_key;
824 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
825 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
826 } else if (lm_session_key.data) {
827 session_key = lm_session_key;
828 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
829 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
830 } else {
831 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
832 session_key = data_blob_null;
835 /* With KEY_EXCH, the client supplies the proposed session key,
836 but encrypts it with the long-term key */
837 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
838 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
839 data_blob_free(&encrypted_session_key);
840 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
841 (unsigned int)encrypted_session_key.length));
842 return NT_STATUS_INVALID_PARAMETER;
843 } else if (!session_key.data || session_key.length != 16) {
844 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
845 (unsigned int)session_key.length));
846 ntlmssp_state->session_key = session_key;
847 } else {
848 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
849 arcfour_crypt_blob(encrypted_session_key.data,
850 encrypted_session_key.length,
851 &session_key);
852 ntlmssp_state->session_key = data_blob_talloc(
853 ntlmssp_state, encrypted_session_key.data,
854 encrypted_session_key.length);
855 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
856 encrypted_session_key.length);
858 } else {
859 ntlmssp_state->session_key = session_key;
862 if (!NT_STATUS_IS_OK(nt_status)) {
863 ntlmssp_state->session_key = data_blob_null;
864 } else if (ntlmssp_state->session_key.length) {
865 nt_status = ntlmssp_sign_init(ntlmssp_state);
868 data_blob_free(&encrypted_session_key);
870 /* Only one authentication allowed per server state. */
871 ntlmssp_state->expected_state = NTLMSSP_DONE;
873 return nt_status;
877 * Create an NTLMSSP state machine
879 * @param ntlmssp_state NTLMSSP State, allocated by this function
882 NTSTATUS ntlmssp_server_start(struct ntlmssp_state **ntlmssp_state)
884 *ntlmssp_state = TALLOC_ZERO_P(NULL, struct ntlmssp_state);
885 if (!*ntlmssp_state) {
886 DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
887 talloc_destroy(*ntlmssp_state);
888 return NT_STATUS_NO_MEMORY;
891 (*ntlmssp_state)->role = NTLMSSP_SERVER;
893 (*ntlmssp_state)->get_challenge = get_challenge;
894 (*ntlmssp_state)->set_challenge = set_challenge;
895 (*ntlmssp_state)->may_set_challenge = may_set_challenge;
897 (*ntlmssp_state)->get_global_myname = global_myname;
898 (*ntlmssp_state)->get_domain = lp_workgroup;
899 (*ntlmssp_state)->server_role = ROLE_DOMAIN_MEMBER; /* a good default */
901 (*ntlmssp_state)->expected_state = NTLMSSP_NEGOTIATE;
903 (*ntlmssp_state)->neg_flags =
904 NTLMSSP_NEGOTIATE_128 |
905 NTLMSSP_NEGOTIATE_56 |
906 NTLMSSP_NEGOTIATE_VERSION |
907 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
908 NTLMSSP_NEGOTIATE_NTLM |
909 NTLMSSP_NEGOTIATE_NTLM2 |
910 NTLMSSP_NEGOTIATE_KEY_EXCH |
911 NTLMSSP_NEGOTIATE_SIGN |
912 NTLMSSP_NEGOTIATE_SEAL;
914 return NT_STATUS_OK;
917 /*********************************************************************
918 Client side NTLMSSP
919 *********************************************************************/
922 * Next state function for the Initial packet
924 * @param ntlmssp_state NTLMSSP State
925 * @param request The request, as a DATA_BLOB. reply.data must be NULL
926 * @param request The reply, as an allocated DATA_BLOB, caller to free.
927 * @return Errors or NT_STATUS_OK.
930 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
931 DATA_BLOB reply, DATA_BLOB *next_request)
933 struct NEGOTIATE_MESSAGE negotiate;
935 if (ntlmssp_state->unicode) {
936 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
937 } else {
938 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
941 if (ntlmssp_state->use_ntlmv2) {
942 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
945 /* generate the ntlmssp negotiate packet */
946 msrpc_gen(ntlmssp_state, next_request, "CddAA",
947 "NTLMSSP",
948 NTLMSSP_NEGOTIATE,
949 ntlmssp_state->neg_flags,
950 ntlmssp_state->get_domain(),
951 ntlmssp_state->get_global_myname());
953 if (DEBUGLEVEL >= 10) {
954 if (NT_STATUS_IS_OK(ntlmssp_pull_NEGOTIATE_MESSAGE(next_request,
955 ntlmssp_state,
956 NULL,
957 &negotiate)))
959 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE, &negotiate);
963 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
965 return NT_STATUS_MORE_PROCESSING_REQUIRED;
969 * Next state function for the Challenge Packet. Generate an auth packet.
971 * @param ntlmssp_state NTLMSSP State
972 * @param request The request, as a DATA_BLOB. reply.data must be NULL
973 * @param request The reply, as an allocated DATA_BLOB, caller to free.
974 * @return Errors or NT_STATUS_OK.
977 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
978 const DATA_BLOB reply, DATA_BLOB *next_request)
980 uint32 chal_flags, ntlmssp_command, unkn1, unkn2;
981 DATA_BLOB server_domain_blob;
982 DATA_BLOB challenge_blob;
983 DATA_BLOB struct_blob = data_blob_null;
984 char *server_domain;
985 const char *chal_parse_string;
986 const char *auth_gen_string;
987 DATA_BLOB lm_response = data_blob_null;
988 DATA_BLOB nt_response = data_blob_null;
989 DATA_BLOB session_key = data_blob_null;
990 DATA_BLOB encrypted_session_key = data_blob_null;
991 NTSTATUS nt_status = NT_STATUS_OK;
992 struct CHALLENGE_MESSAGE challenge;
993 struct AUTHENTICATE_MESSAGE authenticate;
995 if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
996 "NTLMSSP",
997 &ntlmssp_command,
998 &server_domain_blob,
999 &chal_flags)) {
1000 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
1001 dump_data(2, reply.data, reply.length);
1003 return NT_STATUS_INVALID_PARAMETER;
1006 if (DEBUGLEVEL >= 10) {
1007 if (NT_STATUS_IS_OK(ntlmssp_pull_CHALLENGE_MESSAGE(&reply,
1008 ntlmssp_state,
1009 NULL,
1010 &challenge)))
1012 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE, &challenge);
1016 data_blob_free(&server_domain_blob);
1018 DEBUG(3, ("Got challenge flags:\n"));
1019 debug_ntlmssp_flags(chal_flags);
1021 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
1023 if (ntlmssp_state->unicode) {
1024 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
1025 chal_parse_string = "CdUdbddB";
1026 } else {
1027 chal_parse_string = "CdUdbdd";
1029 auth_gen_string = "CdBBUUUBd";
1030 } else {
1031 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
1032 chal_parse_string = "CdAdbddB";
1033 } else {
1034 chal_parse_string = "CdAdbdd";
1037 auth_gen_string = "CdBBAAABd";
1040 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
1041 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
1043 if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
1044 "NTLMSSP",
1045 &ntlmssp_command,
1046 &server_domain,
1047 &chal_flags,
1048 &challenge_blob, 8,
1049 &unkn1, &unkn2,
1050 &struct_blob)) {
1051 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
1052 dump_data(2, reply.data, reply.length);
1053 return NT_STATUS_INVALID_PARAMETER;
1056 ntlmssp_state->server_domain = server_domain;
1058 if (challenge_blob.length != 8) {
1059 data_blob_free(&struct_blob);
1060 return NT_STATUS_INVALID_PARAMETER;
1063 if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
1064 static const uint8_t zeros[16] = {0, };
1065 /* do nothing - blobs are zero length */
1067 /* session key is all zeros */
1068 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
1070 /* not doing NLTM2 without a password */
1071 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
1072 } else if (ntlmssp_state->use_ntlmv2) {
1073 if (!struct_blob.length) {
1074 /* be lazy, match win2k - we can't do NTLMv2 without it */
1075 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1076 return NT_STATUS_INVALID_PARAMETER;
1079 /* TODO: if the remote server is standalone, then we should replace 'domain'
1080 with the server name as supplied above */
1082 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
1083 ntlmssp_state->user,
1084 ntlmssp_state->domain,
1085 ntlmssp_state->nt_hash, &challenge_blob,
1086 &struct_blob,
1087 &lm_response, &nt_response, NULL,
1088 &session_key)) {
1089 data_blob_free(&challenge_blob);
1090 data_blob_free(&struct_blob);
1091 return NT_STATUS_NO_MEMORY;
1093 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1094 struct MD5Context md5_session_nonce_ctx;
1095 uchar session_nonce[16];
1096 uchar session_nonce_hash[16];
1097 uchar user_session_key[16];
1099 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1100 generate_random_buffer(lm_response.data, 8);
1101 memset(lm_response.data+8, 0, 16);
1103 memcpy(session_nonce, challenge_blob.data, 8);
1104 memcpy(&session_nonce[8], lm_response.data, 8);
1106 MD5Init(&md5_session_nonce_ctx);
1107 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1108 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1109 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1111 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1112 DEBUG(5, ("challenge is: \n"));
1113 dump_data(5, session_nonce_hash, 8);
1115 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1116 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
1117 session_nonce_hash,
1118 nt_response.data);
1120 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1122 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
1123 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1124 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1125 } else {
1126 /* lanman auth is insecure, it may be disabled */
1127 if (lp_client_lanman_auth()) {
1128 lm_response = data_blob_talloc(ntlmssp_state,
1129 NULL, 24);
1130 SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
1131 lm_response.data);
1134 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1135 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
1136 nt_response.data);
1138 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1139 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1140 && lp_client_lanman_auth()) {
1141 SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
1142 session_key.data);
1143 dump_data_pw("LM session key\n", session_key.data, session_key.length);
1144 } else {
1145 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
1146 dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1149 data_blob_free(&struct_blob);
1151 /* Key exchange encryptes a new client-generated session key with
1152 the password-derived key */
1153 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1154 /* Make up a new session key */
1155 uint8 client_session_key[16];
1156 generate_random_buffer(client_session_key, sizeof(client_session_key));
1158 /* Encrypt the new session key with the old one */
1159 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1160 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1161 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
1162 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1164 /* Mark the new session key as the 'real' session key */
1165 data_blob_free(&session_key);
1166 session_key = data_blob_talloc(ntlmssp_state,
1167 client_session_key,
1168 sizeof(client_session_key));
1171 /* this generates the actual auth packet */
1172 if (!msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
1173 "NTLMSSP",
1174 NTLMSSP_AUTH,
1175 lm_response.data, lm_response.length,
1176 nt_response.data, nt_response.length,
1177 ntlmssp_state->domain,
1178 ntlmssp_state->user,
1179 ntlmssp_state->get_global_myname(),
1180 encrypted_session_key.data, encrypted_session_key.length,
1181 ntlmssp_state->neg_flags)) {
1183 return NT_STATUS_NO_MEMORY;
1186 if (DEBUGLEVEL >= 10) {
1187 if (NT_STATUS_IS_OK(ntlmssp_pull_AUTHENTICATE_MESSAGE(next_request,
1188 ntlmssp_state,
1189 NULL,
1190 &authenticate)))
1192 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE, &authenticate);
1196 data_blob_free(&encrypted_session_key);
1198 data_blob_free(&ntlmssp_state->chal);
1200 ntlmssp_state->session_key = session_key;
1202 ntlmssp_state->chal = challenge_blob;
1203 ntlmssp_state->lm_resp = lm_response;
1204 ntlmssp_state->nt_resp = nt_response;
1206 ntlmssp_state->expected_state = NTLMSSP_DONE;
1208 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1209 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1212 return nt_status;
1215 NTSTATUS ntlmssp_client_start(struct ntlmssp_state **ntlmssp_state)
1217 *ntlmssp_state = TALLOC_ZERO_P(NULL, struct ntlmssp_state);
1218 if (!*ntlmssp_state) {
1219 DEBUG(0,("ntlmssp_client_start: talloc failed!\n"));
1220 talloc_destroy(*ntlmssp_state);
1221 return NT_STATUS_NO_MEMORY;
1224 (*ntlmssp_state)->role = NTLMSSP_CLIENT;
1226 (*ntlmssp_state)->get_global_myname = global_myname;
1227 (*ntlmssp_state)->get_domain = lp_workgroup;
1229 (*ntlmssp_state)->unicode = True;
1231 (*ntlmssp_state)->use_ntlmv2 = lp_client_ntlmv2_auth();
1233 (*ntlmssp_state)->expected_state = NTLMSSP_INITIAL;
1235 (*ntlmssp_state)->neg_flags =
1236 NTLMSSP_NEGOTIATE_128 |
1237 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
1238 NTLMSSP_NEGOTIATE_NTLM |
1239 NTLMSSP_NEGOTIATE_NTLM2 |
1240 NTLMSSP_NEGOTIATE_KEY_EXCH |
1241 NTLMSSP_REQUEST_TARGET;
1243 return NT_STATUS_OK;