lib: talloc: Allow destructors to reparent the object they're called on.
[Samba.git] / source3 / libsmb / ntlmssp.c
blob617b34b497722dc570d28c875686c3091cfe74d1
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 "../auth/ntlmssp/ntlmssp.h"
26 #include "../auth/ntlmssp/ntlmssp_private.h"
27 #include "../libcli/auth/libcli_auth.h"
28 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
29 #include "../auth/ntlmssp/ntlmssp_ndr.h"
30 #include "../lib/crypto/md5.h"
31 #include "../lib/crypto/arcfour.h"
32 #include "../lib/crypto/hmacmd5.h"
33 #include "../nsswitch/libwbclient/wbclient.h"
35 static NTSTATUS ntlmssp3_client_initial(struct ntlmssp_state *ntlmssp_state,
36 TALLOC_CTX *out_mem_ctx,
37 DATA_BLOB reply, DATA_BLOB *next_request);
38 static NTSTATUS ntlmssp3_client_challenge(struct ntlmssp_state *ntlmssp_state,
39 TALLOC_CTX *out_mem_ctx, /* Unused at this time */
40 const DATA_BLOB reply, DATA_BLOB *next_request);
41 /**
42 * Callbacks for NTLMSSP - for both client and server operating modes
46 static const struct ntlmssp_callbacks {
47 enum ntlmssp_role role;
48 enum ntlmssp_message_type ntlmssp_command;
49 NTSTATUS (*fn)(struct ntlmssp_state *ntlmssp_state,
50 TALLOC_CTX *out_mem_ctx,
51 DATA_BLOB in, DATA_BLOB *out);
52 } ntlmssp_callbacks[] = {
53 {NTLMSSP_CLIENT, NTLMSSP_INITIAL, ntlmssp3_client_initial},
54 {NTLMSSP_CLIENT, NTLMSSP_CHALLENGE, ntlmssp3_client_challenge},
55 {NTLMSSP_CLIENT, NTLMSSP_UNKNOWN, NULL},
56 {NTLMSSP_SERVER, NTLMSSP_UNKNOWN, NULL}
59 /**
60 * Set a username on an NTLMSSP context - ensures it is talloc()ed
64 NTSTATUS ntlmssp_set_username(struct ntlmssp_state *ntlmssp_state, const char *user)
66 ntlmssp_state->user = talloc_strdup(ntlmssp_state, user ? user : "" );
67 if (!ntlmssp_state->user) {
68 return NT_STATUS_NO_MEMORY;
70 return NT_STATUS_OK;
73 /**
74 * Converts a password to the hashes on an NTLMSSP context.
77 NTSTATUS ntlmssp_set_password(struct ntlmssp_state *ntlmssp_state, const char *password)
79 uint8_t lm_hash[16];
80 uint8_t nt_hash[16];
82 TALLOC_FREE(ntlmssp_state->lm_hash);
83 TALLOC_FREE(ntlmssp_state->nt_hash);
85 if (password == NULL) {
86 return NT_STATUS_OK;
89 if (E_deshash(password, lm_hash)) {
90 ntlmssp_state->lm_hash = (uint8_t *)
91 talloc_memdup(ntlmssp_state, lm_hash, 16);
92 if (!ntlmssp_state->lm_hash) {
93 return NT_STATUS_NO_MEMORY;
97 E_md4hash(password, nt_hash);
99 ntlmssp_state->nt_hash = (uint8_t *)
100 talloc_memdup(ntlmssp_state, nt_hash, 16);
101 if (!ntlmssp_state->nt_hash) {
102 TALLOC_FREE(ntlmssp_state->lm_hash);
103 return NT_STATUS_NO_MEMORY;
106 return NT_STATUS_OK;
109 NTSTATUS ntlmssp_set_password_hash(struct ntlmssp_state *state,
110 const char *pwhash)
112 char nt_hash[16];
113 size_t converted;
115 converted = strhex_to_str(
116 nt_hash, sizeof(nt_hash), pwhash, strlen(pwhash));
117 if (converted != sizeof(nt_hash)) {
118 return NT_STATUS_INVALID_PARAMETER;
121 TALLOC_FREE(state->lm_hash);
122 TALLOC_FREE(state->nt_hash);
124 state->nt_hash = (uint8_t *)talloc_memdup(state, nt_hash, 16);
125 if (!state->nt_hash) {
126 return NT_STATUS_NO_MEMORY;
128 return NT_STATUS_OK;
132 * Set a domain on an NTLMSSP context - ensures it is talloc()ed
135 NTSTATUS ntlmssp_set_domain(struct ntlmssp_state *ntlmssp_state, const char *domain)
137 ntlmssp_state->domain = talloc_strdup(ntlmssp_state,
138 domain ? domain : "" );
139 if (!ntlmssp_state->domain) {
140 return NT_STATUS_NO_MEMORY;
142 return NT_STATUS_OK;
146 * Request features for the NTLMSSP negotiation
148 * @param ntlmssp_state NTLMSSP state
149 * @param feature_list List of space separated features requested from NTLMSSP.
151 void ntlmssp_want_feature_list(struct ntlmssp_state *ntlmssp_state, char *feature_list)
154 * We need to set this to allow a later SetPassword
155 * via the SAMR pipe to succeed. Strange.... We could
156 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
158 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
159 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
161 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
162 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
164 if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
165 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
167 if (in_list("NTLMSSP_FEATURE_CCACHE", feature_list, true)) {
168 ntlmssp_state->use_ccache = true;
173 * Request a feature for the NTLMSSP negotiation
175 * @param ntlmssp_state NTLMSSP state
176 * @param feature Bit flag specifying the requested feature
178 void ntlmssp_want_feature(struct ntlmssp_state *ntlmssp_state, uint32_t feature)
180 /* As per JRA's comment above */
181 if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
182 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
184 if (feature & NTLMSSP_FEATURE_SIGN) {
185 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
187 if (feature & NTLMSSP_FEATURE_SEAL) {
188 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
189 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
191 if (feature & NTLMSSP_FEATURE_CCACHE) {
192 ntlmssp_state->use_ccache = true;
197 * Next state function for the NTLMSSP state machine
199 * @param ntlmssp_state NTLMSSP State
200 * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
201 * @param out The reply, as an allocated DATA_BLOB, caller to free.
202 * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
205 NTSTATUS ntlmssp_update(struct ntlmssp_state *ntlmssp_state,
206 const DATA_BLOB input, DATA_BLOB *out)
208 uint32_t ntlmssp_command;
209 int i;
211 if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
212 /* Called update after negotiations finished. */
213 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
214 return NT_STATUS_INVALID_PARAMETER;
217 *out = data_blob_null;
219 if (!input.length) {
220 switch (ntlmssp_state->role) {
221 case NTLMSSP_CLIENT:
222 ntlmssp_command = NTLMSSP_INITIAL;
223 break;
224 case NTLMSSP_SERVER:
225 /* 'datagram' mode - no neg packet */
226 ntlmssp_command = NTLMSSP_NEGOTIATE;
227 break;
228 default:
229 DEBUG(1, ("Invalid role: %d\n", ntlmssp_state->role));
230 return NT_STATUS_INVALID_PARAMETER;
232 } else {
233 if (!msrpc_parse(ntlmssp_state, &input, "Cd",
234 "NTLMSSP",
235 &ntlmssp_command)) {
236 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
237 dump_data(2, input.data, input.length);
238 return NT_STATUS_INVALID_PARAMETER;
242 if (ntlmssp_command != ntlmssp_state->expected_state) {
243 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
244 return NT_STATUS_INVALID_PARAMETER;
247 for (i=0; ntlmssp_callbacks[i].fn; i++) {
248 if (ntlmssp_callbacks[i].role == ntlmssp_state->role
249 && ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
250 return ntlmssp_callbacks[i].fn(ntlmssp_state, ntlmssp_state, input, out);
254 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
255 ntlmssp_state->role, ntlmssp_command));
257 return NT_STATUS_INVALID_PARAMETER;
260 /*********************************************************************
261 Client side NTLMSSP
262 *********************************************************************/
265 * Next state function for the Initial packet
267 * @param ntlmssp_state NTLMSSP State
268 * @param request The request, as a DATA_BLOB. reply.data must be NULL
269 * @param request The reply, as an allocated DATA_BLOB, caller to free.
270 * @return Errors or NT_STATUS_OK.
273 static NTSTATUS ntlmssp3_client_initial(struct ntlmssp_state *ntlmssp_state,
274 TALLOC_CTX *out_mem_ctx,
275 DATA_BLOB in, DATA_BLOB *out)
277 const char *domain = ntlmssp_state->client.netbios_domain;
278 const char *workstation = ntlmssp_state->client.netbios_name;
279 NTSTATUS status;
281 /* These don't really matter in the initial packet, so don't panic if they are not set */
282 if (!domain) {
283 domain = "";
286 if (!workstation) {
287 workstation = "";
290 if (ntlmssp_state->unicode) {
291 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
292 } else {
293 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
296 if (ntlmssp_state->use_ntlmv2) {
297 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
300 /* generate the ntlmssp negotiate packet */
301 status = msrpc_gen(out_mem_ctx,
302 out, "CddAA",
303 "NTLMSSP",
304 NTLMSSP_NEGOTIATE,
305 ntlmssp_state->neg_flags,
306 domain,
307 workstation);
309 if (!NT_STATUS_IS_OK(status)) {
310 DEBUG(0, ("ntlmssp_client_initial: failed to generate "
311 "ntlmssp negotiate packet\n"));
312 return status;
315 if (DEBUGLEVEL >= 10) {
316 struct NEGOTIATE_MESSAGE *negotiate = talloc(
317 talloc_tos(), struct NEGOTIATE_MESSAGE);
318 if (negotiate != NULL) {
319 status = ntlmssp_pull_NEGOTIATE_MESSAGE(
320 out, negotiate, negotiate);
321 if (NT_STATUS_IS_OK(status)) {
322 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
323 negotiate);
325 TALLOC_FREE(negotiate);
329 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
331 return NT_STATUS_MORE_PROCESSING_REQUIRED;
334 bool ntlmssp_is_anonymous(struct ntlmssp_state *ntlmssp_state)
336 const char *user = ntlmssp_state->user;
338 if (user == NULL) {
339 return true;
342 if (strlen(user) == 0) {
343 return true;
346 return false;
350 * Next state function for the Challenge Packet. Generate an auth packet.
352 * @param ntlmssp_state NTLMSSP State
353 * @param request The request, as a DATA_BLOB. reply.data must be NULL
354 * @param request The reply, as an allocated DATA_BLOB, caller to free.
355 * @return Errors or NT_STATUS_OK.
358 static NTSTATUS ntlmssp3_client_challenge(struct ntlmssp_state *ntlmssp_state,
359 TALLOC_CTX *out_mem_ctx, /* Unused at this time */
360 const DATA_BLOB reply, DATA_BLOB *next_request)
362 uint32_t chal_flags, ntlmssp_command, unkn1, unkn2;
363 DATA_BLOB server_domain_blob;
364 DATA_BLOB challenge_blob;
365 DATA_BLOB struct_blob = data_blob_null;
366 char *server_domain;
367 const char *chal_parse_string;
368 const char *auth_gen_string;
369 DATA_BLOB lm_response = data_blob_null;
370 DATA_BLOB nt_response = data_blob_null;
371 DATA_BLOB session_key = data_blob_null;
372 DATA_BLOB encrypted_session_key = data_blob_null;
373 NTSTATUS nt_status = NT_STATUS_OK;
374 bool anon = ntlmssp_is_anonymous(ntlmssp_state);
376 if (!anon && ntlmssp_state->use_ccache) {
377 struct wbcCredentialCacheParams params;
378 struct wbcCredentialCacheInfo *info = NULL;
379 struct wbcAuthErrorInfo *error = NULL;
380 struct wbcNamedBlob auth_blob;
381 struct wbcBlob *wbc_next = NULL;
382 struct wbcBlob *wbc_session_key = NULL;
383 wbcErr wbc_status;
384 int i;
387 * We need to set the netbios name or we are not able to connect
388 * a Windows DC.
390 if (ntlmssp_state->server.netbios_domain == NULL ||
391 ntlmssp_state->server.netbios_domain[0] == '\0') {
392 ntlmssp_state->server.netbios_domain = ntlmssp_state->domain;
395 params.account_name = ntlmssp_state->user;
396 params.domain_name = ntlmssp_state->domain;
397 params.level = WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP;
399 auth_blob.name = "challenge_blob";
400 auth_blob.flags = 0;
401 auth_blob.blob.data = reply.data;
402 auth_blob.blob.length = reply.length;
403 params.num_blobs = 1;
404 params.blobs = &auth_blob;
406 wbc_status = wbcCredentialCache(&params, &info, &error);
407 wbcFreeMemory(error);
408 if (!WBC_ERROR_IS_OK(wbc_status)) {
409 goto noccache;
412 for (i=0; i<info->num_blobs; i++) {
413 if (strequal(info->blobs[i].name, "auth_blob")) {
414 wbc_next = &info->blobs[i].blob;
416 if (strequal(info->blobs[i].name, "session_key")) {
417 wbc_session_key = &info->blobs[i].blob;
420 if ((wbc_next == NULL) || (wbc_session_key == NULL)) {
421 wbcFreeMemory(info);
422 goto noccache;
425 *next_request = data_blob_talloc(ntlmssp_state,
426 wbc_next->data,
427 wbc_next->length);
428 ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state,
429 wbc_session_key->data,
430 wbc_session_key->length);
432 wbcFreeMemory(info);
433 goto done;
436 noccache:
438 if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
439 "NTLMSSP",
440 &ntlmssp_command,
441 &server_domain_blob,
442 &chal_flags)) {
443 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
444 dump_data(2, reply.data, reply.length);
446 return NT_STATUS_INVALID_PARAMETER;
449 if (DEBUGLEVEL >= 10) {
450 struct CHALLENGE_MESSAGE *challenge = talloc(
451 talloc_tos(), struct CHALLENGE_MESSAGE);
452 if (challenge != NULL) {
453 NTSTATUS status;
454 challenge->NegotiateFlags = chal_flags;
455 status = ntlmssp_pull_CHALLENGE_MESSAGE(
456 &reply, challenge, challenge);
457 if (NT_STATUS_IS_OK(status)) {
458 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
459 challenge);
461 TALLOC_FREE(challenge);
465 data_blob_free(&server_domain_blob);
467 DEBUG(3, ("Got challenge flags:\n"));
468 debug_ntlmssp_flags(chal_flags);
470 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
472 if (ntlmssp_state->unicode) {
473 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
474 chal_parse_string = "CdUdbddB";
475 } else {
476 chal_parse_string = "CdUdbdd";
478 auth_gen_string = "CdBBUUUBd";
479 } else {
480 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
481 chal_parse_string = "CdAdbddB";
482 } else {
483 chal_parse_string = "CdAdbdd";
486 auth_gen_string = "CdBBAAABd";
489 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
490 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
492 if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
493 "NTLMSSP",
494 &ntlmssp_command,
495 &server_domain,
496 &chal_flags,
497 &challenge_blob, 8,
498 &unkn1, &unkn2,
499 &struct_blob)) {
500 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
501 dump_data(2, reply.data, reply.length);
502 return NT_STATUS_INVALID_PARAMETER;
505 if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
506 ntlmssp_state->server.is_standalone = true;
507 } else {
508 ntlmssp_state->server.is_standalone = false;
510 /* TODO: parse struct_blob and fill in the rest */
511 ntlmssp_state->server.netbios_name = "";
512 ntlmssp_state->server.netbios_domain = server_domain;
513 ntlmssp_state->server.dns_name = "";
514 ntlmssp_state->server.dns_domain = "";
516 if (challenge_blob.length != 8) {
517 data_blob_free(&struct_blob);
518 return NT_STATUS_INVALID_PARAMETER;
521 if (anon || !ntlmssp_state->nt_hash) {
522 static const uint8_t zeros[16] = {0, };
523 /* do nothing - blobs are zero length */
525 /* session key is all zeros */
526 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
528 /* not doing NLTM2 without a password */
529 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
530 } else if (ntlmssp_state->use_ntlmv2) {
531 if (!struct_blob.length) {
532 /* be lazy, match win2k - we can't do NTLMv2 without it */
533 DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
534 return NT_STATUS_INVALID_PARAMETER;
537 /* TODO: if the remote server is standalone, then we should replace 'domain'
538 with the server name as supplied above */
540 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
541 ntlmssp_state->user,
542 ntlmssp_state->domain,
543 ntlmssp_state->nt_hash, &challenge_blob,
544 &struct_blob,
545 &lm_response, &nt_response, NULL,
546 &session_key)) {
547 data_blob_free(&challenge_blob);
548 data_blob_free(&struct_blob);
549 return NT_STATUS_NO_MEMORY;
551 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
552 MD5_CTX md5_session_nonce_ctx;
553 uint8_t session_nonce[16];
554 uint8_t session_nonce_hash[16];
555 uint8_t user_session_key[16];
557 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
558 generate_random_buffer(lm_response.data, 8);
559 memset(lm_response.data+8, 0, 16);
561 memcpy(session_nonce, challenge_blob.data, 8);
562 memcpy(&session_nonce[8], lm_response.data, 8);
564 MD5Init(&md5_session_nonce_ctx);
565 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
566 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
567 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
569 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
570 DEBUG(5, ("challenge is: \n"));
571 dump_data(5, session_nonce_hash, 8);
573 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
574 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
575 session_nonce_hash,
576 nt_response.data);
578 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
580 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
581 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
582 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
583 } else {
584 /* lanman auth is insecure, it may be disabled */
585 if (lp_client_lanman_auth() && ntlmssp_state->lm_hash) {
586 lm_response = data_blob_talloc(ntlmssp_state,
587 NULL, 24);
588 SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
589 lm_response.data);
592 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
593 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
594 nt_response.data);
596 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
597 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
598 && lp_client_lanman_auth() && ntlmssp_state->lm_hash) {
599 SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
600 session_key.data);
601 dump_data_pw("LM session key\n", session_key.data, session_key.length);
602 } else {
603 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
604 dump_data_pw("NT session key:\n", session_key.data, session_key.length);
607 data_blob_free(&struct_blob);
609 /* Key exchange encryptes a new client-generated session key with
610 the password-derived key */
611 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
612 /* Make up a new session key */
613 uint8_t client_session_key[16];
614 generate_random_buffer(client_session_key, sizeof(client_session_key));
616 /* Encrypt the new session key with the old one */
617 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
618 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
619 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
620 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
622 /* Mark the new session key as the 'real' session key */
623 data_blob_free(&session_key);
624 session_key = data_blob_talloc(ntlmssp_state,
625 client_session_key,
626 sizeof(client_session_key));
629 /* this generates the actual auth packet */
630 nt_status = msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
631 "NTLMSSP",
632 NTLMSSP_AUTH,
633 lm_response.data, lm_response.length,
634 nt_response.data, nt_response.length,
635 ntlmssp_state->domain,
636 ntlmssp_state->user,
637 ntlmssp_state->client.netbios_name,
638 encrypted_session_key.data, encrypted_session_key.length,
639 ntlmssp_state->neg_flags);
641 if (!NT_STATUS_IS_OK(nt_status)) {
642 return NT_STATUS_NO_MEMORY;
645 if (DEBUGLEVEL >= 10) {
646 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
647 talloc_tos(), struct AUTHENTICATE_MESSAGE);
648 if (authenticate != NULL) {
649 NTSTATUS status;
650 authenticate->NegotiateFlags =
651 ntlmssp_state->neg_flags;
652 status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
653 next_request, authenticate, authenticate);
654 if (NT_STATUS_IS_OK(status)) {
655 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
656 authenticate);
658 TALLOC_FREE(authenticate);
662 data_blob_free(&encrypted_session_key);
664 data_blob_free(&ntlmssp_state->chal);
666 ntlmssp_state->session_key = session_key;
668 ntlmssp_state->chal = challenge_blob;
669 ntlmssp_state->lm_resp = lm_response;
670 ntlmssp_state->nt_resp = nt_response;
672 done:
674 ntlmssp_state->expected_state = NTLMSSP_DONE;
676 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
677 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
680 return nt_status;
683 NTSTATUS ntlmssp_client_start(TALLOC_CTX *mem_ctx,
684 const char *netbios_name,
685 const char *netbios_domain,
686 bool use_ntlmv2,
687 struct ntlmssp_state **_ntlmssp_state)
689 struct ntlmssp_state *ntlmssp_state;
691 if (!netbios_name) {
692 netbios_name = "";
695 if (!netbios_domain) {
696 netbios_domain = "";
699 ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
700 if (!ntlmssp_state) {
701 return NT_STATUS_NO_MEMORY;
704 ntlmssp_state->role = NTLMSSP_CLIENT;
706 ntlmssp_state->unicode = True;
708 ntlmssp_state->use_ntlmv2 = use_ntlmv2;
710 ntlmssp_state->expected_state = NTLMSSP_INITIAL;
712 ntlmssp_state->neg_flags =
713 NTLMSSP_NEGOTIATE_128 |
714 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
715 NTLMSSP_NEGOTIATE_NTLM |
716 NTLMSSP_NEGOTIATE_NTLM2 |
717 NTLMSSP_NEGOTIATE_KEY_EXCH |
718 NTLMSSP_REQUEST_TARGET;
720 ntlmssp_state->client.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
721 if (!ntlmssp_state->client.netbios_name) {
722 talloc_free(ntlmssp_state);
723 return NT_STATUS_NO_MEMORY;
725 ntlmssp_state->client.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
726 if (!ntlmssp_state->client.netbios_domain) {
727 talloc_free(ntlmssp_state);
728 return NT_STATUS_NO_MEMORY;
731 *_ntlmssp_state = ntlmssp_state;
732 return NT_STATUS_OK;