s4-pydsdb: Use local memory context in py_dsdb_get_oid_from_attid()
[Samba.git] / source3 / libsmb / ntlmssp.c
blob477b94c1d0974e9c25068cc5088a27c77442524c
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-2010
8 Copyright (C) Stefan Metzmacher 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "../libcli/auth/ntlmssp.h"
26 #include "../libcli/auth/ntlmssp_private.h"
27 #include "../libcli/auth/libcli_auth.h"
28 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
29 #include "../libcli/auth/ntlmssp_ndr.h"
30 #include "../lib/crypto/md5.h"
31 #include "../lib/crypto/arcfour.h"
32 #include "../lib/crypto/hmacmd5.h"
34 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
35 TALLOC_CTX *out_mem_ctx, /* Unused at this time */
36 DATA_BLOB reply, DATA_BLOB *next_request);
37 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
38 TALLOC_CTX *out_mem_ctx, /* Unused at this time */
39 const DATA_BLOB reply, DATA_BLOB *next_request);
40 /**
41 * Callbacks for NTLMSSP - for both client and server operating modes
45 static const struct ntlmssp_callbacks {
46 enum ntlmssp_role role;
47 enum ntlmssp_message_type ntlmssp_command;
48 NTSTATUS (*fn)(struct ntlmssp_state *ntlmssp_state,
49 TALLOC_CTX *out_mem_ctx,
50 DATA_BLOB in, DATA_BLOB *out);
51 } ntlmssp_callbacks[] = {
52 {NTLMSSP_CLIENT, NTLMSSP_INITIAL, ntlmssp_client_initial},
53 {NTLMSSP_SERVER, NTLMSSP_NEGOTIATE, ntlmssp_server_negotiate},
54 {NTLMSSP_CLIENT, NTLMSSP_CHALLENGE, ntlmssp_client_challenge},
55 {NTLMSSP_SERVER, NTLMSSP_AUTH, ntlmssp_server_auth},
56 {NTLMSSP_CLIENT, NTLMSSP_UNKNOWN, NULL},
57 {NTLMSSP_SERVER, NTLMSSP_UNKNOWN, NULL}
61 /**
62 * Default challenge generation code.
66 static NTSTATUS get_challenge(const struct ntlmssp_state *ntlmssp_state,
67 uint8_t chal[8])
69 generate_random_buffer(chal, 8);
70 return NT_STATUS_OK;
73 /**
74 * Default 'we can set the challenge to anything we like' implementation
78 static bool may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
80 return True;
83 /**
84 * Default 'we can set the challenge to anything we like' implementation
86 * Does not actually do anything, as the value is always in the structure anyway.
90 static NTSTATUS set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
92 SMB_ASSERT(challenge->length == 8);
93 return NT_STATUS_OK;
96 /**
97 * Set a username on an NTLMSSP context - ensures it is talloc()ed
101 NTSTATUS ntlmssp_set_username(struct ntlmssp_state *ntlmssp_state, const char *user)
103 ntlmssp_state->user = talloc_strdup(ntlmssp_state, user ? user : "" );
104 if (!ntlmssp_state->user) {
105 return NT_STATUS_NO_MEMORY;
107 return NT_STATUS_OK;
111 * Store NT and LM hashes on an NTLMSSP context - ensures they are talloc()ed
114 NTSTATUS ntlmssp_set_hashes(struct ntlmssp_state *ntlmssp_state,
115 const uint8_t lm_hash[16],
116 const uint8_t nt_hash[16])
118 ntlmssp_state->lm_hash = (uint8_t *)
119 TALLOC_MEMDUP(ntlmssp_state, lm_hash, 16);
120 ntlmssp_state->nt_hash = (uint8_t *)
121 TALLOC_MEMDUP(ntlmssp_state, nt_hash, 16);
122 if (!ntlmssp_state->lm_hash || !ntlmssp_state->nt_hash) {
123 TALLOC_FREE(ntlmssp_state->lm_hash);
124 TALLOC_FREE(ntlmssp_state->nt_hash);
125 return NT_STATUS_NO_MEMORY;
127 return NT_STATUS_OK;
131 * Converts a password to the hashes on an NTLMSSP context.
134 NTSTATUS ntlmssp_set_password(struct ntlmssp_state *ntlmssp_state, const char *password)
136 if (!password) {
137 ntlmssp_state->lm_hash = NULL;
138 ntlmssp_state->nt_hash = NULL;
139 } else {
140 uint8_t lm_hash[16];
141 uint8_t nt_hash[16];
143 E_deshash(password, lm_hash);
144 E_md4hash(password, nt_hash);
145 return ntlmssp_set_hashes(ntlmssp_state, lm_hash, nt_hash);
147 return NT_STATUS_OK;
151 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
154 NTSTATUS ntlmssp_set_domain(struct ntlmssp_state *ntlmssp_state, const char *domain)
156 ntlmssp_state->domain = talloc_strdup(ntlmssp_state,
157 domain ? domain : "" );
158 if (!ntlmssp_state->domain) {
159 return NT_STATUS_NO_MEMORY;
161 return NT_STATUS_OK;
165 * Request features for the NTLMSSP negotiation
167 * @param ntlmssp_state NTLMSSP state
168 * @param feature_list List of space seperated features requested from NTLMSSP.
170 void ntlmssp_want_feature_list(struct ntlmssp_state *ntlmssp_state, char *feature_list)
173 * We need to set this to allow a later SetPassword
174 * via the SAMR pipe to succeed. Strange.... We could
175 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
177 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
178 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
180 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
181 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
183 if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
184 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
186 if (in_list("NTLMSSP_FEATURE_CCACHE", feature_list, true)) {
187 ntlmssp_state->use_ccache = true;
192 * Request a feature for the NTLMSSP negotiation
194 * @param ntlmssp_state NTLMSSP state
195 * @param feature Bit flag specifying the requested feature
197 void ntlmssp_want_feature(struct ntlmssp_state *ntlmssp_state, uint32_t feature)
199 /* As per JRA's comment above */
200 if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
201 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
203 if (feature & NTLMSSP_FEATURE_SIGN) {
204 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
206 if (feature & NTLMSSP_FEATURE_SEAL) {
207 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
209 if (feature & NTLMSSP_FEATURE_CCACHE) {
210 ntlmssp_state->use_ccache = true;
215 * Next state function for the NTLMSSP state machine
217 * @param ntlmssp_state NTLMSSP State
218 * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
219 * @param out The reply, as an allocated DATA_BLOB, caller to free.
220 * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
223 NTSTATUS ntlmssp_update(struct ntlmssp_state *ntlmssp_state,
224 const DATA_BLOB input, DATA_BLOB *out)
226 uint32_t 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;
237 if (!input.length) {
238 switch (ntlmssp_state->role) {
239 case NTLMSSP_CLIENT:
240 ntlmssp_command = NTLMSSP_INITIAL;
241 break;
242 case NTLMSSP_SERVER:
243 /* 'datagram' mode - no neg packet */
244 ntlmssp_command = NTLMSSP_NEGOTIATE;
245 break;
246 default:
247 DEBUG(1, ("Invalid role: %d\n", ntlmssp_state->role));
248 return NT_STATUS_INVALID_PARAMETER;
250 } else {
251 if (!msrpc_parse(ntlmssp_state, &input, "Cd",
252 "NTLMSSP",
253 &ntlmssp_command)) {
254 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
255 dump_data(2, input.data, input.length);
256 return NT_STATUS_INVALID_PARAMETER;
260 if (ntlmssp_command != ntlmssp_state->expected_state) {
261 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
262 return NT_STATUS_INVALID_PARAMETER;
265 for (i=0; ntlmssp_callbacks[i].fn; i++) {
266 if (ntlmssp_callbacks[i].role == ntlmssp_state->role
267 && ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
268 return ntlmssp_callbacks[i].fn(ntlmssp_state, ntlmssp_state, input, out);
272 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
273 ntlmssp_state->role, ntlmssp_command));
275 return NT_STATUS_INVALID_PARAMETER;
279 * Create an NTLMSSP state machine
281 * @param ntlmssp_state NTLMSSP State, allocated by this function
284 NTSTATUS ntlmssp_server_start(TALLOC_CTX *mem_ctx,
285 bool is_standalone,
286 const char *netbios_name,
287 const char *netbios_domain,
288 const char *dns_name,
289 const char *dns_domain,
290 struct ntlmssp_state **_ntlmssp_state)
292 struct ntlmssp_state *ntlmssp_state;
294 if (!netbios_name) {
295 netbios_name = "";
298 if (!netbios_domain) {
299 netbios_domain = "";
302 if (!dns_domain) {
303 dns_domain = "";
306 if (!dns_name) {
307 dns_name = "";
310 ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
311 if (!ntlmssp_state) {
312 return NT_STATUS_NO_MEMORY;
315 ntlmssp_state->role = NTLMSSP_SERVER;
317 ntlmssp_state->get_challenge = get_challenge;
318 ntlmssp_state->set_challenge = set_challenge;
319 ntlmssp_state->may_set_challenge = may_set_challenge;
321 ntlmssp_state->server.is_standalone = is_standalone;
323 ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
325 ntlmssp_state->allow_lm_key = lp_lanman_auth();
327 ntlmssp_state->neg_flags =
328 NTLMSSP_NEGOTIATE_128 |
329 NTLMSSP_NEGOTIATE_56 |
330 NTLMSSP_NEGOTIATE_VERSION |
331 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
332 NTLMSSP_NEGOTIATE_NTLM |
333 NTLMSSP_NEGOTIATE_NTLM2 |
334 NTLMSSP_NEGOTIATE_KEY_EXCH |
335 NTLMSSP_NEGOTIATE_SIGN |
336 NTLMSSP_NEGOTIATE_SEAL;
338 ntlmssp_state->server.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
339 if (!ntlmssp_state->server.netbios_name) {
340 talloc_free(ntlmssp_state);
341 return NT_STATUS_NO_MEMORY;
343 ntlmssp_state->server.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
344 if (!ntlmssp_state->server.netbios_domain) {
345 talloc_free(ntlmssp_state);
346 return NT_STATUS_NO_MEMORY;
348 ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state, dns_name);
349 if (!ntlmssp_state->server.dns_name) {
350 talloc_free(ntlmssp_state);
351 return NT_STATUS_NO_MEMORY;
353 ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state, dns_domain);
354 if (!ntlmssp_state->server.dns_domain) {
355 talloc_free(ntlmssp_state);
356 return NT_STATUS_NO_MEMORY;
359 *_ntlmssp_state = ntlmssp_state;
360 return NT_STATUS_OK;
363 /*********************************************************************
364 Client side NTLMSSP
365 *********************************************************************/
368 * Next state function for the Initial packet
370 * @param ntlmssp_state NTLMSSP State
371 * @param request The request, as a DATA_BLOB. reply.data must be NULL
372 * @param request The reply, as an allocated DATA_BLOB, caller to free.
373 * @return Errors or NT_STATUS_OK.
376 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
377 TALLOC_CTX *out_mem_ctx, /* Unused at this time */
378 DATA_BLOB reply, DATA_BLOB *next_request)
380 if (ntlmssp_state->unicode) {
381 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
382 } else {
383 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
386 if (ntlmssp_state->use_ntlmv2) {
387 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
390 /* generate the ntlmssp negotiate packet */
391 msrpc_gen(ntlmssp_state, next_request, "CddAA",
392 "NTLMSSP",
393 NTLMSSP_NEGOTIATE,
394 ntlmssp_state->neg_flags,
395 ntlmssp_state->client.netbios_domain,
396 ntlmssp_state->client.netbios_name);
398 if (DEBUGLEVEL >= 10) {
399 struct NEGOTIATE_MESSAGE *negotiate = talloc(
400 talloc_tos(), struct NEGOTIATE_MESSAGE);
401 if (negotiate != NULL) {
402 NTSTATUS status;
403 status = ntlmssp_pull_NEGOTIATE_MESSAGE(
404 next_request, negotiate, negotiate);
405 if (NT_STATUS_IS_OK(status)) {
406 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
407 negotiate);
409 TALLOC_FREE(negotiate);
413 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
415 return NT_STATUS_MORE_PROCESSING_REQUIRED;
419 * Next state function for the Challenge Packet. Generate an auth packet.
421 * @param ntlmssp_state NTLMSSP State
422 * @param request The request, as a DATA_BLOB. reply.data must be NULL
423 * @param request The reply, as an allocated DATA_BLOB, caller to free.
424 * @return Errors or NT_STATUS_OK.
427 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
428 TALLOC_CTX *out_mem_ctx, /* Unused at this time */
429 const DATA_BLOB reply, DATA_BLOB *next_request)
431 uint32_t chal_flags, ntlmssp_command, unkn1, unkn2;
432 DATA_BLOB server_domain_blob;
433 DATA_BLOB challenge_blob;
434 DATA_BLOB struct_blob = data_blob_null;
435 char *server_domain;
436 const char *chal_parse_string;
437 const char *auth_gen_string;
438 DATA_BLOB lm_response = data_blob_null;
439 DATA_BLOB nt_response = data_blob_null;
440 DATA_BLOB session_key = data_blob_null;
441 DATA_BLOB encrypted_session_key = data_blob_null;
442 NTSTATUS nt_status = NT_STATUS_OK;
444 if (ntlmssp_state->use_ccache) {
445 struct wbcCredentialCacheParams params;
446 struct wbcCredentialCacheInfo *info = NULL;
447 struct wbcAuthErrorInfo *error = NULL;
448 struct wbcNamedBlob auth_blob;
449 struct wbcBlob *wbc_next = NULL;
450 struct wbcBlob *wbc_session_key = NULL;
451 wbcErr wbc_status;
452 int i;
454 params.account_name = ntlmssp_state->user;
455 params.domain_name = ntlmssp_state->domain;
456 params.level = WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP;
458 auth_blob.name = "challenge_blob";
459 auth_blob.flags = 0;
460 auth_blob.blob.data = reply.data;
461 auth_blob.blob.length = reply.length;
462 params.num_blobs = 1;
463 params.blobs = &auth_blob;
465 wbc_status = wbcCredentialCache(&params, &info, &error);
466 if (error != NULL) {
467 wbcFreeMemory(error);
469 if (!WBC_ERROR_IS_OK(wbc_status)) {
470 goto noccache;
473 for (i=0; i<info->num_blobs; i++) {
474 if (strequal(info->blobs[i].name, "auth_blob")) {
475 wbc_next = &info->blobs[i].blob;
477 if (strequal(info->blobs[i].name, "session_key")) {
478 wbc_session_key = &info->blobs[i].blob;
481 if ((wbc_next == NULL) || (wbc_session_key == NULL)) {
482 wbcFreeMemory(info);
483 goto noccache;
486 *next_request = data_blob(wbc_next->data, wbc_next->length);
487 ntlmssp_state->session_key = data_blob(
488 wbc_session_key->data, wbc_session_key->length);
490 wbcFreeMemory(info);
491 goto done;
494 noccache:
496 if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
497 "NTLMSSP",
498 &ntlmssp_command,
499 &server_domain_blob,
500 &chal_flags)) {
501 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
502 dump_data(2, reply.data, reply.length);
504 return NT_STATUS_INVALID_PARAMETER;
507 if (DEBUGLEVEL >= 10) {
508 struct CHALLENGE_MESSAGE *challenge = talloc(
509 talloc_tos(), struct CHALLENGE_MESSAGE);
510 if (challenge != NULL) {
511 NTSTATUS status;
512 challenge->NegotiateFlags = chal_flags;
513 status = ntlmssp_pull_CHALLENGE_MESSAGE(
514 &reply, challenge, challenge);
515 if (NT_STATUS_IS_OK(status)) {
516 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
517 challenge);
519 TALLOC_FREE(challenge);
523 data_blob_free(&server_domain_blob);
525 DEBUG(3, ("Got challenge flags:\n"));
526 debug_ntlmssp_flags(chal_flags);
528 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
530 if (ntlmssp_state->unicode) {
531 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
532 chal_parse_string = "CdUdbddB";
533 } else {
534 chal_parse_string = "CdUdbdd";
536 auth_gen_string = "CdBBUUUBd";
537 } else {
538 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
539 chal_parse_string = "CdAdbddB";
540 } else {
541 chal_parse_string = "CdAdbdd";
544 auth_gen_string = "CdBBAAABd";
547 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
548 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
550 if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
551 "NTLMSSP",
552 &ntlmssp_command,
553 &server_domain,
554 &chal_flags,
555 &challenge_blob, 8,
556 &unkn1, &unkn2,
557 &struct_blob)) {
558 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
559 dump_data(2, reply.data, reply.length);
560 return NT_STATUS_INVALID_PARAMETER;
563 if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
564 ntlmssp_state->server.is_standalone = true;
565 } else {
566 ntlmssp_state->server.is_standalone = false;
568 /* TODO: parse struct_blob and fill in the rest */
569 ntlmssp_state->server.netbios_name = "";
570 ntlmssp_state->server.netbios_domain = server_domain;
571 ntlmssp_state->server.dns_name = "";
572 ntlmssp_state->server.dns_domain = "";
574 if (challenge_blob.length != 8) {
575 data_blob_free(&struct_blob);
576 return NT_STATUS_INVALID_PARAMETER;
579 if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
580 static const uint8_t zeros[16] = {0, };
581 /* do nothing - blobs are zero length */
583 /* session key is all zeros */
584 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
586 /* not doing NLTM2 without a password */
587 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
588 } else if (ntlmssp_state->use_ntlmv2) {
589 if (!struct_blob.length) {
590 /* be lazy, match win2k - we can't do NTLMv2 without it */
591 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
592 return NT_STATUS_INVALID_PARAMETER;
595 /* TODO: if the remote server is standalone, then we should replace 'domain'
596 with the server name as supplied above */
598 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
599 ntlmssp_state->user,
600 ntlmssp_state->domain,
601 ntlmssp_state->nt_hash, &challenge_blob,
602 &struct_blob,
603 &lm_response, &nt_response, NULL,
604 &session_key)) {
605 data_blob_free(&challenge_blob);
606 data_blob_free(&struct_blob);
607 return NT_STATUS_NO_MEMORY;
609 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
610 struct MD5Context md5_session_nonce_ctx;
611 uint8_t session_nonce[16];
612 uint8_t session_nonce_hash[16];
613 uint8_t user_session_key[16];
615 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
616 generate_random_buffer(lm_response.data, 8);
617 memset(lm_response.data+8, 0, 16);
619 memcpy(session_nonce, challenge_blob.data, 8);
620 memcpy(&session_nonce[8], lm_response.data, 8);
622 MD5Init(&md5_session_nonce_ctx);
623 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
624 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
625 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
627 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
628 DEBUG(5, ("challenge is: \n"));
629 dump_data(5, session_nonce_hash, 8);
631 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
632 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
633 session_nonce_hash,
634 nt_response.data);
636 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
638 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
639 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
640 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
641 } else {
642 /* lanman auth is insecure, it may be disabled */
643 if (lp_client_lanman_auth()) {
644 lm_response = data_blob_talloc(ntlmssp_state,
645 NULL, 24);
646 SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
647 lm_response.data);
650 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
651 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
652 nt_response.data);
654 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
655 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
656 && lp_client_lanman_auth()) {
657 SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
658 session_key.data);
659 dump_data_pw("LM session key\n", session_key.data, session_key.length);
660 } else {
661 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
662 dump_data_pw("NT session key:\n", session_key.data, session_key.length);
665 data_blob_free(&struct_blob);
667 /* Key exchange encryptes a new client-generated session key with
668 the password-derived key */
669 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
670 /* Make up a new session key */
671 uint8_t client_session_key[16];
672 generate_random_buffer(client_session_key, sizeof(client_session_key));
674 /* Encrypt the new session key with the old one */
675 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
676 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
677 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
678 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
680 /* Mark the new session key as the 'real' session key */
681 data_blob_free(&session_key);
682 session_key = data_blob_talloc(ntlmssp_state,
683 client_session_key,
684 sizeof(client_session_key));
687 /* this generates the actual auth packet */
688 if (!msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
689 "NTLMSSP",
690 NTLMSSP_AUTH,
691 lm_response.data, lm_response.length,
692 nt_response.data, nt_response.length,
693 ntlmssp_state->domain,
694 ntlmssp_state->user,
695 ntlmssp_state->client.netbios_name,
696 encrypted_session_key.data, encrypted_session_key.length,
697 ntlmssp_state->neg_flags)) {
699 return NT_STATUS_NO_MEMORY;
702 if (DEBUGLEVEL >= 10) {
703 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
704 talloc_tos(), struct AUTHENTICATE_MESSAGE);
705 if (authenticate != NULL) {
706 NTSTATUS status;
707 authenticate->NegotiateFlags =
708 ntlmssp_state->neg_flags;
709 status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
710 next_request, authenticate, authenticate);
711 if (NT_STATUS_IS_OK(status)) {
712 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
713 authenticate);
715 TALLOC_FREE(authenticate);
719 data_blob_free(&encrypted_session_key);
721 data_blob_free(&ntlmssp_state->chal);
723 ntlmssp_state->session_key = session_key;
725 ntlmssp_state->chal = challenge_blob;
726 ntlmssp_state->lm_resp = lm_response;
727 ntlmssp_state->nt_resp = nt_response;
729 done:
731 ntlmssp_state->expected_state = NTLMSSP_DONE;
733 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
734 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
737 return nt_status;
740 NTSTATUS ntlmssp_client_start(TALLOC_CTX *mem_ctx,
741 const char *netbios_name,
742 const char *netbios_domain,
743 bool use_ntlmv2,
744 struct ntlmssp_state **_ntlmssp_state)
746 struct ntlmssp_state *ntlmssp_state;
748 if (!netbios_name) {
749 netbios_name = "";
752 if (!netbios_domain) {
753 netbios_domain = "";
756 ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
757 if (!ntlmssp_state) {
758 return NT_STATUS_NO_MEMORY;
761 ntlmssp_state->role = NTLMSSP_CLIENT;
763 ntlmssp_state->unicode = True;
765 ntlmssp_state->use_ntlmv2 = use_ntlmv2;
767 ntlmssp_state->expected_state = NTLMSSP_INITIAL;
769 ntlmssp_state->neg_flags =
770 NTLMSSP_NEGOTIATE_128 |
771 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
772 NTLMSSP_NEGOTIATE_NTLM |
773 NTLMSSP_NEGOTIATE_NTLM2 |
774 NTLMSSP_NEGOTIATE_KEY_EXCH |
775 NTLMSSP_REQUEST_TARGET;
777 ntlmssp_state->client.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
778 if (!ntlmssp_state->client.netbios_name) {
779 talloc_free(ntlmssp_state);
780 return NT_STATUS_NO_MEMORY;
782 ntlmssp_state->client.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
783 if (!ntlmssp_state->client.netbios_domain) {
784 talloc_free(ntlmssp_state);
785 return NT_STATUS_NO_MEMORY;
788 *_ntlmssp_state = ntlmssp_state;
789 return NT_STATUS_OK;