build: Build with system md5.h on OpenIndiana
[mat/samba.git] / source3 / libsmb / ntlmssp.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, server side
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett 2001-2010
8    Copyright (C) Stefan Metzmacher 2005
9
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.
14
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.
19
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/>.
22 */
23
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"
34
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
43  *
44  */
45
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}
57 };
58
59 /**
60  * Set a username on an NTLMSSP context - ensures it is talloc()ed
61  *
62  */
63
64 NTSTATUS ntlmssp_set_username(struct ntlmssp_state *ntlmssp_state, const char *user)
65 {
66         ntlmssp_state->user = talloc_strdup(ntlmssp_state, user ? user : "" );
67         if (!ntlmssp_state->user) {
68                 return NT_STATUS_NO_MEMORY;
69         }
70         return NT_STATUS_OK;
71 }
72
73 /**
74  * Converts a password to the hashes on an NTLMSSP context.
75  *
76  */
77 NTSTATUS ntlmssp_set_password(struct ntlmssp_state *ntlmssp_state, const char *password)
78 {
79         uint8_t lm_hash[16];
80         uint8_t nt_hash[16];
81
82         TALLOC_FREE(ntlmssp_state->lm_hash);
83         TALLOC_FREE(ntlmssp_state->nt_hash);
84
85         if (password == NULL) {
86                 return NT_STATUS_OK;
87         }
88
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;
94                 }
95         }
96
97         E_md4hash(password, nt_hash);
98
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;
104         }
105
106         return NT_STATUS_OK;
107 }
108
109 NTSTATUS ntlmssp_set_password_hash(struct ntlmssp_state *state,
110                                    const char *pwhash)
111 {
112         char nt_hash[16];
113         size_t converted;
114
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;
119         }
120
121         TALLOC_FREE(state->lm_hash);
122         TALLOC_FREE(state->nt_hash);
123
124         state->nt_hash = (uint8_t *)talloc_memdup(state, nt_hash, 16);
125         if (!state->nt_hash) {
126                 return NT_STATUS_NO_MEMORY;
127         }
128         return NT_STATUS_OK;
129 }
130
131 /**
132  * Set a domain on an NTLMSSP context - ensures it is talloc()ed
133  *
134  */
135 NTSTATUS ntlmssp_set_domain(struct ntlmssp_state *ntlmssp_state, const char *domain)
136 {
137         ntlmssp_state->domain = talloc_strdup(ntlmssp_state,
138                                               domain ? domain : "" );
139         if (!ntlmssp_state->domain) {
140                 return NT_STATUS_NO_MEMORY;
141         }
142         return NT_STATUS_OK;
143 }
144
145 /**
146  * Request features for the NTLMSSP negotiation
147  *
148  * @param ntlmssp_state NTLMSSP state
149  * @param feature_list List of space separated features requested from NTLMSSP.
150  */
151 void ntlmssp_want_feature_list(struct ntlmssp_state *ntlmssp_state, char *feature_list)
152 {
153         /*
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.
157          */
158         if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
159                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
160         }
161         if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
162                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
163         }
164         if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
165                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
166         }
167         if (in_list("NTLMSSP_FEATURE_CCACHE", feature_list, true)) {
168                 ntlmssp_state->use_ccache = true;
169         }
170 }
171
172 /**
173  * Request a feature for the NTLMSSP negotiation
174  *
175  * @param ntlmssp_state NTLMSSP state
176  * @param feature Bit flag specifying the requested feature
177  */
178 void ntlmssp_want_feature(struct ntlmssp_state *ntlmssp_state, uint32_t feature)
179 {
180         /* As per JRA's comment above */
181         if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
182                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
183         }
184         if (feature & NTLMSSP_FEATURE_SIGN) {
185                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
186         }
187         if (feature & NTLMSSP_FEATURE_SEAL) {
188                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
189                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
190         }
191         if (feature & NTLMSSP_FEATURE_CCACHE) {
192                 ntlmssp_state->use_ccache = true;
193         }
194 }
195
196 /**
197  * Next state function for the NTLMSSP state machine
198  *
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.
203  */
204
205 NTSTATUS ntlmssp_update(struct ntlmssp_state *ntlmssp_state,
206                         const DATA_BLOB input, DATA_BLOB *out)
207 {
208         uint32_t ntlmssp_command;
209         int i;
210
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;
215         }
216
217         *out = data_blob_null;
218
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;
231                 }
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;
239                 }
240         }
241
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;
245         }
246
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);
251                 }
252         }
253
254         DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
255                   ntlmssp_state->role, ntlmssp_command));
256
257         return NT_STATUS_INVALID_PARAMETER;
258 }
259
260 /*********************************************************************
261  Client side NTLMSSP
262 *********************************************************************/
263
264 /**
265  * Next state function for the Initial packet
266  *
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.
271  */
272
273 static NTSTATUS ntlmssp3_client_initial(struct ntlmssp_state *ntlmssp_state,
274                                        TALLOC_CTX *out_mem_ctx,
275                                        DATA_BLOB in, DATA_BLOB *out)
276 {
277         const char *domain = ntlmssp_state->client.netbios_domain;
278         const char *workstation = ntlmssp_state->client.netbios_name;
279         NTSTATUS status;
280
281         /* These don't really matter in the initial packet, so don't panic if they are not set */
282         if (!domain) {
283                 domain = "";
284         }
285
286         if (!workstation) {
287                 workstation = "";
288         }
289
290         if (ntlmssp_state->unicode) {
291                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
292         } else {
293                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
294         }
295
296         if (ntlmssp_state->use_ntlmv2) {
297                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
298         }
299
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);
308
309         if (!NT_STATUS_IS_OK(status)) {
310                 DEBUG(0, ("ntlmssp_client_initial: failed to generate "
311                           "ntlmssp negotiate packet\n"));
312                 return status;
313         }
314
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);
324                         }
325                         TALLOC_FREE(negotiate);
326                 }
327         }
328
329         ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
330
331         return NT_STATUS_MORE_PROCESSING_REQUIRED;
332 }
333
334 bool ntlmssp_is_anonymous(struct ntlmssp_state *ntlmssp_state)
335 {
336         const char *user = ntlmssp_state->user;
337
338         if (user == NULL) {
339                 return true;
340         }
341
342         if (strlen(user) == 0) {
343                 return true;
344         }
345
346         return false;
347 }
348
349 /**
350  * Next state function for the Challenge Packet.  Generate an auth packet.
351  *
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.
356  */
357
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)
361 {
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);
375
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;
385
386                 params.account_name = ntlmssp_state->user;
387                 params.domain_name = ntlmssp_state->domain;
388                 params.level = WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP;
389
390                 auth_blob.name = "challenge_blob";
391                 auth_blob.flags = 0;
392                 auth_blob.blob.data = reply.data;
393                 auth_blob.blob.length = reply.length;
394                 params.num_blobs = 1;
395                 params.blobs = &auth_blob;
396
397                 wbc_status = wbcCredentialCache(&params, &info, &error);
398                 wbcFreeMemory(error);
399                 if (!WBC_ERROR_IS_OK(wbc_status)) {
400                         goto noccache;
401                 }
402
403                 for (i=0; i<info->num_blobs; i++) {
404                         if (strequal(info->blobs[i].name, "auth_blob")) {
405                                 wbc_next = &info->blobs[i].blob;
406                         }
407                         if (strequal(info->blobs[i].name, "session_key")) {
408                                 wbc_session_key = &info->blobs[i].blob;
409                         }
410                 }
411                 if ((wbc_next == NULL) || (wbc_session_key == NULL)) {
412                         wbcFreeMemory(info);
413                         goto noccache;
414                 }
415
416                 *next_request = data_blob(wbc_next->data, wbc_next->length);
417                 ntlmssp_state->session_key = data_blob(
418                         wbc_session_key->data, wbc_session_key->length);
419
420                 wbcFreeMemory(info);
421                 goto done;
422         }
423
424 noccache:
425
426         if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
427                          "NTLMSSP",
428                          &ntlmssp_command,
429                          &server_domain_blob,
430                          &chal_flags)) {
431                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
432                 dump_data(2, reply.data, reply.length);
433
434                 return NT_STATUS_INVALID_PARAMETER;
435         }
436
437         if (DEBUGLEVEL >= 10) {
438                 struct CHALLENGE_MESSAGE *challenge = talloc(
439                         talloc_tos(), struct CHALLENGE_MESSAGE);
440                 if (challenge != NULL) {
441                         NTSTATUS status;
442                         challenge->NegotiateFlags = chal_flags;
443                         status = ntlmssp_pull_CHALLENGE_MESSAGE(
444                                 &reply, challenge, challenge);
445                         if (NT_STATUS_IS_OK(status)) {
446                                 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
447                                                 challenge);
448                         }
449                         TALLOC_FREE(challenge);
450                 }
451         }
452
453         data_blob_free(&server_domain_blob);
454
455         DEBUG(3, ("Got challenge flags:\n"));
456         debug_ntlmssp_flags(chal_flags);
457
458         ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
459
460         if (ntlmssp_state->unicode) {
461                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
462                         chal_parse_string = "CdUdbddB";
463                 } else {
464                         chal_parse_string = "CdUdbdd";
465                 }
466                 auth_gen_string = "CdBBUUUBd";
467         } else {
468                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
469                         chal_parse_string = "CdAdbddB";
470                 } else {
471                         chal_parse_string = "CdAdbdd";
472                 }
473
474                 auth_gen_string = "CdBBAAABd";
475         }
476
477         DEBUG(3, ("NTLMSSP: Set final flags:\n"));
478         debug_ntlmssp_flags(ntlmssp_state->neg_flags);
479
480         if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
481                          "NTLMSSP",
482                          &ntlmssp_command,
483                          &server_domain,
484                          &chal_flags,
485                          &challenge_blob, 8,
486                          &unkn1, &unkn2,
487                          &struct_blob)) {
488                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
489                 dump_data(2, reply.data, reply.length);
490                 return NT_STATUS_INVALID_PARAMETER;
491         }
492
493         if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
494                 ntlmssp_state->server.is_standalone = true;
495         } else {
496                 ntlmssp_state->server.is_standalone = false;
497         }
498         /* TODO: parse struct_blob and fill in the rest */
499         ntlmssp_state->server.netbios_name = "";
500         ntlmssp_state->server.netbios_domain = server_domain;
501         ntlmssp_state->server.dns_name = "";
502         ntlmssp_state->server.dns_domain = "";
503
504         if (challenge_blob.length != 8) {
505                 data_blob_free(&struct_blob);
506                 return NT_STATUS_INVALID_PARAMETER;
507         }
508
509         if (anon || !ntlmssp_state->nt_hash) {
510                 static const uint8_t zeros[16] = {0, };
511                 /* do nothing - blobs are zero length */
512
513                 /* session key is all zeros */
514                 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
515
516                 /* not doing NLTM2 without a password */
517                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
518         } else if (ntlmssp_state->use_ntlmv2) {
519                 if (!struct_blob.length) {
520                         /* be lazy, match win2k - we can't do NTLMv2 without it */
521                         DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
522                         return NT_STATUS_INVALID_PARAMETER;
523                 }
524
525                 /* TODO: if the remote server is standalone, then we should replace 'domain'
526                    with the server name as supplied above */
527
528                 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
529                                            ntlmssp_state->user,
530                                            ntlmssp_state->domain,
531                                            ntlmssp_state->nt_hash, &challenge_blob,
532                                            &struct_blob,
533                                            &lm_response, &nt_response, NULL,
534                                            &session_key)) {
535                         data_blob_free(&challenge_blob);
536                         data_blob_free(&struct_blob);
537                         return NT_STATUS_NO_MEMORY;
538                 }
539         } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
540                 MD5_CTX md5_session_nonce_ctx;
541                 uint8_t session_nonce[16];
542                 uint8_t session_nonce_hash[16];
543                 uint8_t user_session_key[16];
544
545                 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
546                 generate_random_buffer(lm_response.data, 8);
547                 memset(lm_response.data+8, 0, 16);
548
549                 memcpy(session_nonce, challenge_blob.data, 8);
550                 memcpy(&session_nonce[8], lm_response.data, 8);
551
552                 MD5Init(&md5_session_nonce_ctx);
553                 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
554                 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
555                 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
556
557                 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
558                 DEBUG(5, ("challenge is: \n"));
559                 dump_data(5, session_nonce_hash, 8);
560
561                 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
562                 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
563                                   session_nonce_hash,
564                                   nt_response.data);
565
566                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
567
568                 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
569                 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
570                 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
571         } else {
572                 /* lanman auth is insecure, it may be disabled */
573                 if (lp_client_lanman_auth() && ntlmssp_state->lm_hash) {
574                         lm_response = data_blob_talloc(ntlmssp_state,
575                                                        NULL, 24);
576                         SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
577                                    lm_response.data);
578                 }
579
580                 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
581                 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
582                              nt_response.data);
583
584                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
585                 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
586                     && lp_client_lanman_auth() && ntlmssp_state->lm_hash) {
587                         SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
588                                         session_key.data);
589                         dump_data_pw("LM session key\n", session_key.data, session_key.length);
590                 } else {
591                         SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
592                         dump_data_pw("NT session key:\n", session_key.data, session_key.length);
593                 }
594         }
595         data_blob_free(&struct_blob);
596
597         /* Key exchange encryptes a new client-generated session key with
598            the password-derived key */
599         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
600                 /* Make up a new session key */
601                 uint8_t client_session_key[16];
602                 generate_random_buffer(client_session_key, sizeof(client_session_key));
603
604                 /* Encrypt the new session key with the old one */
605                 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
606                 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
607                 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
608                 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
609
610                 /* Mark the new session key as the 'real' session key */
611                 data_blob_free(&session_key);
612                 session_key = data_blob_talloc(ntlmssp_state,
613                                                client_session_key,
614                                                sizeof(client_session_key));
615         }
616
617         /* this generates the actual auth packet */
618         nt_status = msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
619                        "NTLMSSP",
620                        NTLMSSP_AUTH,
621                        lm_response.data, lm_response.length,
622                        nt_response.data, nt_response.length,
623                        ntlmssp_state->domain,
624                        ntlmssp_state->user,
625                        ntlmssp_state->client.netbios_name,
626                        encrypted_session_key.data, encrypted_session_key.length,
627                        ntlmssp_state->neg_flags);
628
629         if (!NT_STATUS_IS_OK(nt_status)) {
630                 return NT_STATUS_NO_MEMORY;
631         }
632
633         if (DEBUGLEVEL >= 10) {
634                 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
635                         talloc_tos(), struct AUTHENTICATE_MESSAGE);
636                 if (authenticate != NULL) {
637                         NTSTATUS status;
638                         authenticate->NegotiateFlags =
639                                 ntlmssp_state->neg_flags;
640                         status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
641                                 next_request, authenticate, authenticate);
642                         if (NT_STATUS_IS_OK(status)) {
643                                 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
644                                                 authenticate);
645                         }
646                         TALLOC_FREE(authenticate);
647                 }
648         }
649
650         data_blob_free(&encrypted_session_key);
651
652         data_blob_free(&ntlmssp_state->chal);
653
654         ntlmssp_state->session_key = session_key;
655
656         ntlmssp_state->chal = challenge_blob;
657         ntlmssp_state->lm_resp = lm_response;
658         ntlmssp_state->nt_resp = nt_response;
659
660 done:
661
662         ntlmssp_state->expected_state = NTLMSSP_DONE;
663
664         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
665                 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
666         }
667
668         return nt_status;
669 }
670
671 NTSTATUS ntlmssp_client_start(TALLOC_CTX *mem_ctx,
672                               const char *netbios_name,
673                               const char *netbios_domain,
674                               bool use_ntlmv2,
675                               struct ntlmssp_state **_ntlmssp_state)
676 {
677         struct ntlmssp_state *ntlmssp_state;
678
679         if (!netbios_name) {
680                 netbios_name = "";
681         }
682
683         if (!netbios_domain) {
684                 netbios_domain = "";
685         }
686
687         ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
688         if (!ntlmssp_state) {
689                 return NT_STATUS_NO_MEMORY;
690         }
691
692         ntlmssp_state->role = NTLMSSP_CLIENT;
693
694         ntlmssp_state->unicode = True;
695
696         ntlmssp_state->use_ntlmv2 = use_ntlmv2;
697
698         ntlmssp_state->expected_state = NTLMSSP_INITIAL;
699
700         ntlmssp_state->neg_flags =
701                 NTLMSSP_NEGOTIATE_128 |
702                 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
703                 NTLMSSP_NEGOTIATE_NTLM |
704                 NTLMSSP_NEGOTIATE_NTLM2 |
705                 NTLMSSP_NEGOTIATE_KEY_EXCH |
706                 NTLMSSP_REQUEST_TARGET;
707
708         ntlmssp_state->client.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
709         if (!ntlmssp_state->client.netbios_name) {
710                 talloc_free(ntlmssp_state);
711                 return NT_STATUS_NO_MEMORY;
712         }
713         ntlmssp_state->client.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
714         if (!ntlmssp_state->client.netbios_domain) {
715                 talloc_free(ntlmssp_state);
716                 return NT_STATUS_NO_MEMORY;
717         }
718
719         *_ntlmssp_state = ntlmssp_state;
720         return NT_STATUS_OK;
721 }