r13642: Fix install* scripts to use DESTDIR.
[Samba.git] / source / libsmb / ntlmssp.c
blobe1ef69aed9911eda45757f9bc7718512c1487a54
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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
27 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
28 DATA_BLOB reply, DATA_BLOB *next_request);
29 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
30 const DATA_BLOB in, DATA_BLOB *out);
31 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
32 const DATA_BLOB reply, DATA_BLOB *next_request);
33 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
34 const DATA_BLOB request, DATA_BLOB *reply);
36 /**
37 * Callbacks for NTLMSSP - for both client and server operating modes
41 static const struct ntlmssp_callbacks {
42 enum NTLMSSP_ROLE role;
43 enum NTLM_MESSAGE_TYPE ntlmssp_command;
44 NTSTATUS (*fn)(struct ntlmssp_state *ntlmssp_state,
45 DATA_BLOB in, DATA_BLOB *out);
46 } ntlmssp_callbacks[] = {
47 {NTLMSSP_CLIENT, NTLMSSP_INITIAL, ntlmssp_client_initial},
48 {NTLMSSP_SERVER, NTLMSSP_NEGOTIATE, ntlmssp_server_negotiate},
49 {NTLMSSP_CLIENT, NTLMSSP_CHALLENGE, ntlmssp_client_challenge},
50 {NTLMSSP_SERVER, NTLMSSP_AUTH, ntlmssp_server_auth},
51 {NTLMSSP_CLIENT, NTLMSSP_UNKNOWN, NULL},
52 {NTLMSSP_SERVER, NTLMSSP_UNKNOWN, NULL}
56 /**
57 * Print out the NTLMSSP flags for debugging
58 * @param neg_flags The flags from the packet
61 void debug_ntlmssp_flags(uint32 neg_flags)
63 DEBUG(3,("Got NTLMSSP neg_flags=0x%08x\n", neg_flags));
65 if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE)
66 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_UNICODE\n"));
67 if (neg_flags & NTLMSSP_NEGOTIATE_OEM)
68 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_OEM\n"));
69 if (neg_flags & NTLMSSP_REQUEST_TARGET)
70 DEBUGADD(4, (" NTLMSSP_REQUEST_TARGET\n"));
71 if (neg_flags & NTLMSSP_NEGOTIATE_SIGN)
72 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SIGN\n"));
73 if (neg_flags & NTLMSSP_NEGOTIATE_SEAL)
74 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_SEAL\n"));
75 if (neg_flags & NTLMSSP_NEGOTIATE_DATAGRAM_STYLE)
76 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_DATAGRAM_STYLE\n"));
77 if (neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
78 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_LM_KEY\n"));
79 if (neg_flags & NTLMSSP_NEGOTIATE_NETWARE)
80 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NETWARE\n"));
81 if (neg_flags & NTLMSSP_NEGOTIATE_NTLM)
82 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM\n"));
83 if (neg_flags & NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED)
84 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED\n"));
85 if (neg_flags & NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED)
86 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED\n"));
87 if (neg_flags & NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL)
88 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL\n"));
89 if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
90 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_ALWAYS_SIGN\n"));
91 if (neg_flags & NTLMSSP_CHAL_ACCEPT_RESPONSE)
92 DEBUGADD(4, (" NTLMSSP_CHAL_ACCEPT_RESPONSE\n"));
93 if (neg_flags & NTLMSSP_CHAL_NON_NT_SESSION_KEY)
94 DEBUGADD(4, (" NTLMSSP_CHAL_NON_NT_SESSION_KEY\n"));
95 if (neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
96 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_NTLM2\n"));
97 if (neg_flags & NTLMSSP_CHAL_TARGET_INFO)
98 DEBUGADD(4, (" NTLMSSP_CHAL_TARGET_INFO\n"));
99 if (neg_flags & NTLMSSP_NEGOTIATE_128)
100 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_128\n"));
101 if (neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)
102 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_KEY_EXCH\n"));
103 if (neg_flags & NTLMSSP_NEGOTIATE_56)
104 DEBUGADD(4, (" NTLMSSP_NEGOTIATE_56\n"));
108 * Default challenge generation code.
112 static const uint8 *get_challenge(const struct ntlmssp_state *ntlmssp_state)
114 static uchar chal[8];
115 generate_random_buffer(chal, sizeof(chal));
117 return chal;
121 * Default 'we can set the challenge to anything we like' implementation
125 static BOOL may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
127 return True;
131 * Default 'we can set the challenge to anything we like' implementation
133 * Does not actually do anything, as the value is always in the structure anyway.
137 static NTSTATUS set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
139 SMB_ASSERT(challenge->length == 8);
140 return NT_STATUS_OK;
143 /**
144 * Set a username on an NTLMSSP context - ensures it is talloc()ed
148 NTSTATUS ntlmssp_set_username(NTLMSSP_STATE *ntlmssp_state, const char *user)
150 ntlmssp_state->user = talloc_strdup(ntlmssp_state->mem_ctx, user ? user : "" );
151 if (!ntlmssp_state->user) {
152 return NT_STATUS_NO_MEMORY;
154 return NT_STATUS_OK;
157 /**
158 * Set a password on an NTLMSSP context - ensures it is talloc()ed
161 NTSTATUS ntlmssp_set_password(NTLMSSP_STATE *ntlmssp_state, const char *password)
163 if (!password) {
164 ntlmssp_state->password = NULL;
165 } else {
166 ntlmssp_state->password = talloc_strdup(ntlmssp_state->mem_ctx, password);
167 if (!ntlmssp_state->password) {
168 return NT_STATUS_NO_MEMORY;
171 return NT_STATUS_OK;
174 /**
175 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
178 NTSTATUS ntlmssp_set_domain(NTLMSSP_STATE *ntlmssp_state, const char *domain)
180 ntlmssp_state->domain = talloc_strdup(ntlmssp_state->mem_ctx, domain ? domain : "" );
181 if (!ntlmssp_state->domain) {
182 return NT_STATUS_NO_MEMORY;
184 return NT_STATUS_OK;
187 /**
188 * Set a workstation on an NTLMSSP context - ensures it is talloc()ed
191 NTSTATUS ntlmssp_set_workstation(NTLMSSP_STATE *ntlmssp_state, const char *workstation)
193 ntlmssp_state->workstation = talloc_strdup(ntlmssp_state->mem_ctx, workstation);
194 if (!ntlmssp_state->workstation) {
195 return NT_STATUS_NO_MEMORY;
197 return NT_STATUS_OK;
201 * Store a DATA_BLOB containing an NTLMSSP response, for use later.
202 * This copies the data blob
205 NTSTATUS ntlmssp_store_response(NTLMSSP_STATE *ntlmssp_state,
206 DATA_BLOB response)
208 ntlmssp_state->stored_response = data_blob_talloc(ntlmssp_state->mem_ctx,
209 response.data, response.length);
210 return NT_STATUS_OK;
214 * Next state function for the NTLMSSP state machine
216 * @param ntlmssp_state NTLMSSP State
217 * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
218 * @param out The reply, as an allocated DATA_BLOB, caller to free.
219 * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
222 NTSTATUS ntlmssp_update(NTLMSSP_STATE *ntlmssp_state,
223 const DATA_BLOB in, DATA_BLOB *out)
225 DATA_BLOB input;
226 uint32 ntlmssp_command;
227 int i;
229 if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
230 /* Called update after negotiations finished. */
231 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
232 return NT_STATUS_INVALID_PARAMETER;
235 *out = data_blob(NULL, 0);
237 if (!in.length && ntlmssp_state->stored_response.length) {
238 input = ntlmssp_state->stored_response;
240 /* we only want to read the stored response once - overwrite it */
241 ntlmssp_state->stored_response = data_blob(NULL, 0);
242 } else {
243 input = in;
246 if (!input.length) {
247 switch (ntlmssp_state->role) {
248 case NTLMSSP_CLIENT:
249 ntlmssp_command = NTLMSSP_INITIAL;
250 break;
251 case NTLMSSP_SERVER:
252 /* 'datagram' mode - no neg packet */
253 ntlmssp_command = NTLMSSP_NEGOTIATE;
254 break;
256 } else {
257 if (!msrpc_parse(&input, "Cd",
258 "NTLMSSP",
259 &ntlmssp_command)) {
260 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
261 dump_data(2, (const char *)input.data, input.length);
262 return NT_STATUS_INVALID_PARAMETER;
266 if (ntlmssp_command != ntlmssp_state->expected_state) {
267 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
268 return NT_STATUS_INVALID_PARAMETER;
271 for (i=0; ntlmssp_callbacks[i].fn; i++) {
272 if (ntlmssp_callbacks[i].role == ntlmssp_state->role
273 && ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
274 return ntlmssp_callbacks[i].fn(ntlmssp_state, input, out);
278 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
279 ntlmssp_state->role, ntlmssp_command));
281 return NT_STATUS_INVALID_PARAMETER;
285 * End an NTLMSSP state machine
287 * @param ntlmssp_state NTLMSSP State, free()ed by this function
290 void ntlmssp_end(NTLMSSP_STATE **ntlmssp_state)
292 TALLOC_CTX *mem_ctx = (*ntlmssp_state)->mem_ctx;
294 (*ntlmssp_state)->ref_count--;
296 if ((*ntlmssp_state)->ref_count == 0) {
297 data_blob_free(&(*ntlmssp_state)->chal);
298 data_blob_free(&(*ntlmssp_state)->lm_resp);
299 data_blob_free(&(*ntlmssp_state)->nt_resp);
301 talloc_destroy(mem_ctx);
304 *ntlmssp_state = NULL;
305 return;
309 * Determine correct target name flags for reply, given server role
310 * and negotiated flags
312 * @param ntlmssp_state NTLMSSP State
313 * @param neg_flags The flags from the packet
314 * @param chal_flags The flags to be set in the reply packet
315 * @return The 'target name' string.
318 static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
319 uint32 neg_flags, uint32 *chal_flags)
321 if (neg_flags & NTLMSSP_REQUEST_TARGET) {
322 *chal_flags |= NTLMSSP_CHAL_TARGET_INFO;
323 *chal_flags |= NTLMSSP_REQUEST_TARGET;
324 if (ntlmssp_state->server_role == ROLE_STANDALONE) {
325 *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
326 return ntlmssp_state->get_global_myname();
327 } else {
328 *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
329 return ntlmssp_state->get_domain();
331 } else {
332 return "";
336 static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
337 uint32 neg_flags, BOOL allow_lm) {
338 if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
339 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
340 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_OEM;
341 ntlmssp_state->unicode = True;
342 } else {
343 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_UNICODE;
344 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
345 ntlmssp_state->unicode = False;
348 if ((neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) && allow_lm) {
349 /* other end forcing us to use LM */
350 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
351 ntlmssp_state->use_ntlmv2 = False;
352 } else {
353 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
356 if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN) {
357 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
360 if (!(neg_flags & NTLMSSP_NEGOTIATE_NTLM2)) {
361 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
364 if (!(neg_flags & NTLMSSP_NEGOTIATE_128)) {
365 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_128;
366 if (neg_flags & NTLMSSP_NEGOTIATE_56) {
367 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
371 if (!(neg_flags & NTLMSSP_NEGOTIATE_56)) {
372 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_56;
375 if (!(neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
376 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_KEY_EXCH;
379 if ((neg_flags & NTLMSSP_REQUEST_TARGET)) {
380 ntlmssp_state->neg_flags |= NTLMSSP_REQUEST_TARGET;
386 Weaken NTLMSSP keys to cope with down-level clients and servers.
388 We probably should have some parameters to control this, but as
389 it only occours for LM_KEY connections, and this is controlled
390 by the client lanman auth/lanman auth parameters, it isn't too bad.
393 DATA_BLOB ntlmssp_weaken_keys(NTLMSSP_STATE *ntlmssp_state, TALLOC_CTX *mem_ctx)
395 DATA_BLOB weakened_key = data_blob_talloc(mem_ctx,
396 ntlmssp_state->session_key.data,
397 ntlmssp_state->session_key.length);
399 /* Nothing to weaken. We certainly don't want to 'extend' the length... */
400 if (weakened_key.length < 16) {
401 /* perhaps there was no key? */
402 return weakened_key;
405 /* Key weakening not performed on the master key for NTLM2
406 and does not occour for NTLM1. Therefore we only need
407 to do this for the LM_KEY.
410 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
411 /* LM key doesn't support 128 bit crypto, so this is
412 * the best we can do. If you negotiate 128 bit, but
413 * not 56, you end up with 40 bit... */
414 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
415 weakened_key.data[7] = 0xa0;
416 } else { /* forty bits */
417 weakened_key.data[5] = 0xe5;
418 weakened_key.data[6] = 0x38;
419 weakened_key.data[7] = 0xb0;
421 weakened_key.length = 8;
423 return weakened_key;
427 * Next state function for the Negotiate packet
429 * @param ntlmssp_state NTLMSSP State
430 * @param request The request, as a DATA_BLOB
431 * @param request The reply, as an allocated DATA_BLOB, caller to free.
432 * @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent.
435 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
436 const DATA_BLOB request, DATA_BLOB *reply)
438 DATA_BLOB struct_blob;
439 fstring dnsname, dnsdomname;
440 uint32 neg_flags = 0;
441 uint32 ntlmssp_command, chal_flags;
442 const uint8 *cryptkey;
443 const char *target_name;
445 /* parse the NTLMSSP packet */
446 #if 0
447 file_save("ntlmssp_negotiate.dat", request.data, request.length);
448 #endif
450 if (request.length) {
451 if ((request.length < 16) || !msrpc_parse(&request, "Cdd",
452 "NTLMSSP",
453 &ntlmssp_command,
454 &neg_flags)) {
455 DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
456 (unsigned int)request.length));
457 dump_data(2, (const char *)request.data, request.length);
458 return NT_STATUS_INVALID_PARAMETER;
460 debug_ntlmssp_flags(neg_flags);
463 ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, lp_lanman_auth());
465 /* Ask our caller what challenge they would like in the packet */
466 cryptkey = ntlmssp_state->get_challenge(ntlmssp_state);
468 /* Check if we may set the challenge */
469 if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
470 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
473 /* The flags we send back are not just the negotiated flags,
474 * they are also 'what is in this packet'. Therfore, we
475 * operate on 'chal_flags' from here on
478 chal_flags = ntlmssp_state->neg_flags;
480 /* get the right name to fill in as 'target' */
481 target_name = ntlmssp_target_name(ntlmssp_state,
482 neg_flags, &chal_flags);
483 if (target_name == NULL)
484 return NT_STATUS_INVALID_PARAMETER;
486 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state->mem_ctx, cryptkey, 8);
487 ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state->mem_ctx, cryptkey, 8);
490 /* This should be a 'netbios domain -> DNS domain' mapping */
491 dnsdomname[0] = '\0';
492 get_mydnsdomname(dnsdomname);
493 strlower_m(dnsdomname);
495 dnsname[0] = '\0';
496 get_mydnsfullname(dnsname);
498 /* This creates the 'blob' of names that appears at the end of the packet */
499 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO)
501 msrpc_gen(&struct_blob, "aaaaa",
502 NTLMSSP_NAME_TYPE_DOMAIN, target_name,
503 NTLMSSP_NAME_TYPE_SERVER, ntlmssp_state->get_global_myname(),
504 NTLMSSP_NAME_TYPE_DOMAIN_DNS, dnsdomname,
505 NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
506 0, "");
507 } else {
508 struct_blob = data_blob(NULL, 0);
512 /* Marshel the packet in the right format, be it unicode or ASCII */
513 const char *gen_string;
514 if (ntlmssp_state->unicode) {
515 gen_string = "CdUdbddB";
516 } else {
517 gen_string = "CdAdbddB";
520 msrpc_gen(reply, gen_string,
521 "NTLMSSP",
522 NTLMSSP_CHALLENGE,
523 target_name,
524 chal_flags,
525 cryptkey, 8,
526 0, 0,
527 struct_blob.data, struct_blob.length);
530 data_blob_free(&struct_blob);
532 ntlmssp_state->expected_state = NTLMSSP_AUTH;
534 return NT_STATUS_MORE_PROCESSING_REQUIRED;
538 * Next state function for the Authenticate packet
540 * @param ntlmssp_state NTLMSSP State
541 * @param request The request, as a DATA_BLOB
542 * @param request The reply, as an allocated DATA_BLOB, caller to free.
543 * @return Errors or NT_STATUS_OK.
546 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
547 const DATA_BLOB request, DATA_BLOB *reply)
549 DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
550 DATA_BLOB user_session_key = data_blob(NULL, 0);
551 DATA_BLOB lm_session_key = data_blob(NULL, 0);
552 DATA_BLOB session_key = data_blob(NULL, 0);
553 uint32 ntlmssp_command, auth_flags;
554 NTSTATUS nt_status = NT_STATUS_OK;
556 /* used by NTLM2 */
557 BOOL doing_ntlm2 = False;
559 uchar session_nonce[16];
560 uchar session_nonce_hash[16];
562 const char *parse_string;
563 char *domain = NULL;
564 char *user = NULL;
565 char *workstation = NULL;
567 /* parse the NTLMSSP packet */
568 *reply = data_blob(NULL, 0);
570 #if 0
571 file_save("ntlmssp_auth.dat", request.data, request.length);
572 #endif
574 if (ntlmssp_state->unicode) {
575 parse_string = "CdBBUUUBd";
576 } else {
577 parse_string = "CdBBAAABd";
580 data_blob_free(&ntlmssp_state->lm_resp);
581 data_blob_free(&ntlmssp_state->nt_resp);
583 ntlmssp_state->user = NULL;
584 ntlmssp_state->domain = NULL;
585 ntlmssp_state->workstation = NULL;
587 /* now the NTLMSSP encoded auth hashes */
588 if (!msrpc_parse(&request, parse_string,
589 "NTLMSSP",
590 &ntlmssp_command,
591 &ntlmssp_state->lm_resp,
592 &ntlmssp_state->nt_resp,
593 &domain,
594 &user,
595 &workstation,
596 &encrypted_session_key,
597 &auth_flags)) {
598 SAFE_FREE(domain);
599 SAFE_FREE(user);
600 SAFE_FREE(workstation);
601 data_blob_free(&encrypted_session_key);
602 auth_flags = 0;
604 /* Try again with a shorter string (Win9X truncates this packet) */
605 if (ntlmssp_state->unicode) {
606 parse_string = "CdBBUUU";
607 } else {
608 parse_string = "CdBBAAA";
611 /* now the NTLMSSP encoded auth hashes */
612 if (!msrpc_parse(&request, parse_string,
613 "NTLMSSP",
614 &ntlmssp_command,
615 &ntlmssp_state->lm_resp,
616 &ntlmssp_state->nt_resp,
617 &domain,
618 &user,
619 &workstation)) {
620 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
621 dump_data(2, (const char *)request.data, request.length);
622 SAFE_FREE(domain);
623 SAFE_FREE(user);
624 SAFE_FREE(workstation);
626 return NT_STATUS_INVALID_PARAMETER;
630 if (auth_flags)
631 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, lp_lanman_auth());
633 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(ntlmssp_state, domain))) {
634 SAFE_FREE(domain);
635 SAFE_FREE(user);
636 SAFE_FREE(workstation);
637 data_blob_free(&encrypted_session_key);
638 return nt_status;
641 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(ntlmssp_state, user))) {
642 SAFE_FREE(domain);
643 SAFE_FREE(user);
644 SAFE_FREE(workstation);
645 data_blob_free(&encrypted_session_key);
646 return nt_status;
649 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(ntlmssp_state, workstation))) {
650 SAFE_FREE(domain);
651 SAFE_FREE(user);
652 SAFE_FREE(workstation);
653 data_blob_free(&encrypted_session_key);
654 return nt_status;
657 SAFE_FREE(domain);
658 SAFE_FREE(user);
659 SAFE_FREE(workstation);
661 DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
662 ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
664 #if 0
665 file_save("nthash1.dat", &ntlmssp_state->nt_resp.data, &ntlmssp_state->nt_resp.length);
666 file_save("lmhash1.dat", &ntlmssp_state->lm_resp.data, &ntlmssp_state->lm_resp.length);
667 #endif
669 /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
670 client challenge
672 However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
674 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
675 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
676 struct MD5Context md5_session_nonce_ctx;
677 SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
679 doing_ntlm2 = True;
681 memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
682 memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
684 MD5Init(&md5_session_nonce_ctx);
685 MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
686 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
688 ntlmssp_state->chal = data_blob_talloc(ntlmssp_state->mem_ctx, session_nonce_hash, 8);
690 /* LM response is no longer useful */
691 data_blob_free(&ntlmssp_state->lm_resp);
693 /* We changed the effective challenge - set it */
694 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
695 data_blob_free(&encrypted_session_key);
696 return nt_status;
699 /* LM Key is incompatible. */
700 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
705 * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
706 * is required (by "ntlm auth = no" and "lm auth = no" being set in the
707 * smb.conf file) and no NTLMv2 response was sent then the password check
708 * will fail here. JRA.
711 /* Finally, actually ask if the password is OK */
713 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
714 &user_session_key, &lm_session_key))) {
715 data_blob_free(&encrypted_session_key);
716 return nt_status;
719 dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
720 dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
722 /* Handle the different session key derivation for NTLM2 */
723 if (doing_ntlm2) {
724 if (user_session_key.data && user_session_key.length == 16) {
725 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
726 hmac_md5(user_session_key.data, session_nonce,
727 sizeof(session_nonce), session_key.data);
728 DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
729 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
731 } else {
732 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
733 session_key = data_blob(NULL, 0);
735 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
736 if (lm_session_key.data && lm_session_key.length >= 8) {
737 if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
738 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
739 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
740 session_key.data);
741 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
742 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
743 } else {
744 /* use the key unmodified - it's
745 * probably a NULL key from the guest
746 * login */
747 session_key = lm_session_key;
749 } else {
750 DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
751 session_key = data_blob(NULL, 0);
753 } else if (user_session_key.data) {
754 session_key = user_session_key;
755 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
756 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
757 } else if (lm_session_key.data) {
758 session_key = lm_session_key;
759 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
760 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
761 } else {
762 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
763 session_key = data_blob(NULL, 0);
766 /* With KEY_EXCH, the client supplies the proposed session key,
767 but encrypts it with the long-term key */
768 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
769 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
770 data_blob_free(&encrypted_session_key);
771 DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
772 (unsigned int)encrypted_session_key.length));
773 return NT_STATUS_INVALID_PARAMETER;
774 } else if (!session_key.data || session_key.length != 16) {
775 DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
776 (unsigned int)session_key.length));
777 ntlmssp_state->session_key = session_key;
778 } else {
779 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
780 SamOEMhash(encrypted_session_key.data,
781 session_key.data,
782 encrypted_session_key.length);
783 ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state->mem_ctx,
784 encrypted_session_key.data,
785 encrypted_session_key.length);
786 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
787 encrypted_session_key.length);
789 } else {
790 ntlmssp_state->session_key = session_key;
793 if (!NT_STATUS_IS_OK(nt_status)) {
794 ntlmssp_state->session_key = data_blob(NULL, 0);
795 } else if (ntlmssp_state->session_key.length) {
796 nt_status = ntlmssp_sign_init(ntlmssp_state);
799 data_blob_free(&encrypted_session_key);
801 /* Only one authentication allowed per server state. */
802 ntlmssp_state->expected_state = NTLMSSP_DONE;
804 return nt_status;
808 * Create an NTLMSSP state machine
810 * @param ntlmssp_state NTLMSSP State, allocated by this function
813 NTSTATUS ntlmssp_server_start(NTLMSSP_STATE **ntlmssp_state)
815 TALLOC_CTX *mem_ctx;
817 mem_ctx = talloc_init("NTLMSSP context");
819 *ntlmssp_state = TALLOC_ZERO_P(mem_ctx, NTLMSSP_STATE);
820 if (!*ntlmssp_state) {
821 DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
822 talloc_destroy(mem_ctx);
823 return NT_STATUS_NO_MEMORY;
826 (*ntlmssp_state)->role = NTLMSSP_SERVER;
828 (*ntlmssp_state)->mem_ctx = mem_ctx;
829 (*ntlmssp_state)->get_challenge = get_challenge;
830 (*ntlmssp_state)->set_challenge = set_challenge;
831 (*ntlmssp_state)->may_set_challenge = may_set_challenge;
833 (*ntlmssp_state)->get_global_myname = global_myname;
834 (*ntlmssp_state)->get_domain = lp_workgroup;
835 (*ntlmssp_state)->server_role = ROLE_DOMAIN_MEMBER; /* a good default */
837 (*ntlmssp_state)->expected_state = NTLMSSP_NEGOTIATE;
839 (*ntlmssp_state)->ref_count = 1;
841 (*ntlmssp_state)->neg_flags =
842 NTLMSSP_NEGOTIATE_128 |
843 NTLMSSP_NEGOTIATE_NTLM |
844 NTLMSSP_NEGOTIATE_NTLM2 |
845 NTLMSSP_NEGOTIATE_KEY_EXCH |
846 NTLMSSP_NEGOTIATE_SIGN |
847 NTLMSSP_NEGOTIATE_SEAL;
849 return NT_STATUS_OK;
852 /*********************************************************************
853 Client side NTLMSSP
854 *********************************************************************/
857 * Next state function for the Initial packet
859 * @param ntlmssp_state NTLMSSP State
860 * @param request The request, as a DATA_BLOB. reply.data must be NULL
861 * @param request The reply, as an allocated DATA_BLOB, caller to free.
862 * @return Errors or NT_STATUS_OK.
865 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
866 DATA_BLOB reply, DATA_BLOB *next_request)
868 if (ntlmssp_state->unicode) {
869 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
870 } else {
871 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
874 if (ntlmssp_state->use_ntlmv2) {
875 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
878 /* generate the ntlmssp negotiate packet */
879 msrpc_gen(next_request, "CddAA",
880 "NTLMSSP",
881 NTLMSSP_NEGOTIATE,
882 ntlmssp_state->neg_flags,
883 ntlmssp_state->get_domain(),
884 ntlmssp_state->get_global_myname());
886 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
888 return NT_STATUS_MORE_PROCESSING_REQUIRED;
892 * Next state function for the Challenge Packet. Generate an auth packet.
894 * @param ntlmssp_state NTLMSSP State
895 * @param request The request, as a DATA_BLOB. reply.data must be NULL
896 * @param request The reply, as an allocated DATA_BLOB, caller to free.
897 * @return Errors or NT_STATUS_OK.
900 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
901 const DATA_BLOB reply, DATA_BLOB *next_request)
903 uint32 chal_flags, ntlmssp_command, unkn1, unkn2;
904 DATA_BLOB server_domain_blob;
905 DATA_BLOB challenge_blob;
906 DATA_BLOB struct_blob = data_blob(NULL, 0);
907 char *server_domain;
908 const char *chal_parse_string;
909 const char *auth_gen_string;
910 DATA_BLOB lm_response = data_blob(NULL, 0);
911 DATA_BLOB nt_response = data_blob(NULL, 0);
912 DATA_BLOB session_key = data_blob(NULL, 0);
913 DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
914 NTSTATUS nt_status = NT_STATUS_OK;
916 if (!msrpc_parse(&reply, "CdBd",
917 "NTLMSSP",
918 &ntlmssp_command,
919 &server_domain_blob,
920 &chal_flags)) {
921 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
922 dump_data(2, (const char *)reply.data, reply.length);
924 return NT_STATUS_INVALID_PARAMETER;
927 data_blob_free(&server_domain_blob);
929 DEBUG(3, ("Got challenge flags:\n"));
930 debug_ntlmssp_flags(chal_flags);
932 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
934 if (ntlmssp_state->unicode) {
935 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
936 chal_parse_string = "CdUdbddB";
937 } else {
938 chal_parse_string = "CdUdbdd";
940 auth_gen_string = "CdBBUUUBd";
941 } else {
942 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
943 chal_parse_string = "CdAdbddB";
944 } else {
945 chal_parse_string = "CdAdbdd";
948 auth_gen_string = "CdBBAAABd";
951 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
952 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
954 if (!msrpc_parse(&reply, chal_parse_string,
955 "NTLMSSP",
956 &ntlmssp_command,
957 &server_domain,
958 &chal_flags,
959 &challenge_blob, 8,
960 &unkn1, &unkn2,
961 &struct_blob)) {
962 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
963 dump_data(2, (const char *)reply.data, reply.length);
964 return NT_STATUS_INVALID_PARAMETER;
967 ntlmssp_state->server_domain = talloc_strdup(ntlmssp_state->mem_ctx,
968 server_domain);
970 SAFE_FREE(server_domain);
971 if (challenge_blob.length != 8) {
972 data_blob_free(&struct_blob);
973 return NT_STATUS_INVALID_PARAMETER;
976 if (!ntlmssp_state->password) {
977 static const uchar zeros[16];
978 /* do nothing - blobs are zero length */
980 /* session key is all zeros */
981 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, zeros, 16);
983 /* not doing NLTM2 without a password */
984 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
985 } else if (ntlmssp_state->use_ntlmv2) {
987 if (!struct_blob.length) {
988 /* be lazy, match win2k - we can't do NTLMv2 without it */
989 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
990 return NT_STATUS_INVALID_PARAMETER;
993 /* TODO: if the remote server is standalone, then we should replace 'domain'
994 with the server name as supplied above */
996 if (!SMBNTLMv2encrypt(ntlmssp_state->user,
997 ntlmssp_state->domain,
998 ntlmssp_state->password, &challenge_blob,
999 &struct_blob,
1000 &lm_response, &nt_response, &session_key)) {
1001 data_blob_free(&challenge_blob);
1002 data_blob_free(&struct_blob);
1003 return NT_STATUS_NO_MEMORY;
1005 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1006 struct MD5Context md5_session_nonce_ctx;
1007 uchar nt_hash[16];
1008 uchar session_nonce[16];
1009 uchar session_nonce_hash[16];
1010 uchar user_session_key[16];
1011 E_md4hash(ntlmssp_state->password, nt_hash);
1013 lm_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1014 generate_random_buffer(lm_response.data, 8);
1015 memset(lm_response.data+8, 0, 16);
1017 memcpy(session_nonce, challenge_blob.data, 8);
1018 memcpy(&session_nonce[8], lm_response.data, 8);
1020 MD5Init(&md5_session_nonce_ctx);
1021 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1022 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1023 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1025 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1026 DEBUG(5, ("challenge is: \n"));
1027 dump_data(5, (const char *)session_nonce_hash, 8);
1029 nt_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1030 SMBNTencrypt(ntlmssp_state->password,
1031 session_nonce_hash,
1032 nt_response.data);
1034 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
1036 SMBsesskeygen_ntv1(nt_hash, NULL, user_session_key);
1037 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1038 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1039 } else {
1040 uchar lm_hash[16];
1041 uchar nt_hash[16];
1042 E_deshash(ntlmssp_state->password, lm_hash);
1043 E_md4hash(ntlmssp_state->password, nt_hash);
1045 /* lanman auth is insecure, it may be disabled */
1046 if (lp_client_lanman_auth()) {
1047 lm_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1048 SMBencrypt(ntlmssp_state->password,challenge_blob.data,
1049 lm_response.data);
1052 nt_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
1053 SMBNTencrypt(ntlmssp_state->password,challenge_blob.data,
1054 nt_response.data);
1056 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
1057 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1058 && lp_client_lanman_auth()) {
1059 SMBsesskeygen_lm_sess_key(lm_hash, lm_response.data,
1060 session_key.data);
1061 dump_data_pw("LM session key\n", session_key.data, session_key.length);
1062 } else {
1063 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1064 dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1067 data_blob_free(&struct_blob);
1069 /* Key exchange encryptes a new client-generated session key with
1070 the password-derived key */
1071 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1072 /* Make up a new session key */
1073 uint8 client_session_key[16];
1074 generate_random_buffer(client_session_key, sizeof(client_session_key));
1076 /* Encrypt the new session key with the old one */
1077 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1078 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1079 SamOEMhash(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
1080 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1082 /* Mark the new session key as the 'real' session key */
1083 data_blob_free(&session_key);
1084 session_key = data_blob_talloc(ntlmssp_state->mem_ctx, client_session_key, sizeof(client_session_key));
1087 /* this generates the actual auth packet */
1088 if (!msrpc_gen(next_request, auth_gen_string,
1089 "NTLMSSP",
1090 NTLMSSP_AUTH,
1091 lm_response.data, lm_response.length,
1092 nt_response.data, nt_response.length,
1093 ntlmssp_state->domain,
1094 ntlmssp_state->user,
1095 ntlmssp_state->get_global_myname(),
1096 encrypted_session_key.data, encrypted_session_key.length,
1097 ntlmssp_state->neg_flags)) {
1099 return NT_STATUS_NO_MEMORY;
1102 data_blob_free(&encrypted_session_key);
1104 data_blob_free(&ntlmssp_state->chal);
1106 ntlmssp_state->session_key = session_key;
1108 ntlmssp_state->chal = challenge_blob;
1109 ntlmssp_state->lm_resp = lm_response;
1110 ntlmssp_state->nt_resp = nt_response;
1112 ntlmssp_state->expected_state = NTLMSSP_DONE;
1114 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1115 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1118 return nt_status;
1121 NTSTATUS ntlmssp_client_start(NTLMSSP_STATE **ntlmssp_state)
1123 TALLOC_CTX *mem_ctx;
1125 mem_ctx = talloc_init("NTLMSSP Client context");
1127 *ntlmssp_state = TALLOC_ZERO_P(mem_ctx, NTLMSSP_STATE);
1128 if (!*ntlmssp_state) {
1129 DEBUG(0,("ntlmssp_client_start: talloc failed!\n"));
1130 talloc_destroy(mem_ctx);
1131 return NT_STATUS_NO_MEMORY;
1134 (*ntlmssp_state)->role = NTLMSSP_CLIENT;
1136 (*ntlmssp_state)->mem_ctx = mem_ctx;
1138 (*ntlmssp_state)->get_global_myname = global_myname;
1139 (*ntlmssp_state)->get_domain = lp_workgroup;
1141 (*ntlmssp_state)->unicode = True;
1143 (*ntlmssp_state)->use_ntlmv2 = lp_client_ntlmv2_auth();
1145 (*ntlmssp_state)->expected_state = NTLMSSP_INITIAL;
1147 (*ntlmssp_state)->ref_count = 1;
1149 (*ntlmssp_state)->neg_flags =
1150 NTLMSSP_NEGOTIATE_128 |
1151 NTLMSSP_NEGOTIATE_NTLM |
1152 NTLMSSP_NEGOTIATE_NTLM2 |
1153 NTLMSSP_NEGOTIATE_KEY_EXCH |
1155 * We need to set this to allow a later SetPassword
1156 * via the SAMR pipe to succeed. Strange.... We could
1157 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
1158 * */
1159 NTLMSSP_NEGOTIATE_SIGN |
1160 NTLMSSP_REQUEST_TARGET;
1162 return NT_STATUS_OK;