s3:ntlmssp Don't reply with the LM_KEY negotiation flag when not available
[amitay/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-2003
8    Copyright (C) Andrew Bartlett 2005 (Updated from gensec).
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 "../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"
33
34 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
35                                        DATA_BLOB reply, DATA_BLOB *next_request);
36 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
37                                          const DATA_BLOB in, DATA_BLOB *out);
38 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
39                                          const DATA_BLOB reply, DATA_BLOB *next_request);
40 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
41                                     const DATA_BLOB request, DATA_BLOB *reply);
42
43 /**
44  * Callbacks for NTLMSSP - for both client and server operating modes
45  *
46  */
47
48 static const struct ntlmssp_callbacks {
49         enum ntlmssp_role role;
50         enum ntlmssp_message_type ntlmssp_command;
51         NTSTATUS (*fn)(struct ntlmssp_state *ntlmssp_state,
52                        DATA_BLOB in, DATA_BLOB *out);
53 } ntlmssp_callbacks[] = {
54         {NTLMSSP_CLIENT, NTLMSSP_INITIAL, ntlmssp_client_initial},
55         {NTLMSSP_SERVER, NTLMSSP_NEGOTIATE, ntlmssp_server_negotiate},
56         {NTLMSSP_CLIENT, NTLMSSP_CHALLENGE, ntlmssp_client_challenge},
57         {NTLMSSP_SERVER, NTLMSSP_AUTH, ntlmssp_server_auth},
58         {NTLMSSP_CLIENT, NTLMSSP_UNKNOWN, NULL},
59         {NTLMSSP_SERVER, NTLMSSP_UNKNOWN, NULL}
60 };
61
62
63 /**
64  * Default challenge generation code.
65  *
66  */
67
68 static NTSTATUS get_challenge(const struct ntlmssp_state *ntlmssp_state,
69                               uint8_t chal[8])
70 {
71         generate_random_buffer(chal, 8);
72         return NT_STATUS_OK;
73 }
74
75 /**
76  * Default 'we can set the challenge to anything we like' implementation
77  *
78  */
79
80 static bool may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
81 {
82         return True;
83 }
84
85 /**
86  * Default 'we can set the challenge to anything we like' implementation
87  *
88  * Does not actually do anything, as the value is always in the structure anyway.
89  *
90  */
91
92 static NTSTATUS set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
93 {
94         SMB_ASSERT(challenge->length == 8);
95         return NT_STATUS_OK;
96 }
97
98 /**
99  * Set a username on an NTLMSSP context - ensures it is talloc()ed
100  *
101  */
102
103 NTSTATUS ntlmssp_set_username(struct ntlmssp_state *ntlmssp_state, const char *user)
104 {
105         ntlmssp_state->user = talloc_strdup(ntlmssp_state, user ? user : "" );
106         if (!ntlmssp_state->user) {
107                 return NT_STATUS_NO_MEMORY;
108         }
109         return NT_STATUS_OK;
110 }
111
112 /**
113  * Store NT and LM hashes on an NTLMSSP context - ensures they are talloc()ed
114  *
115  */
116 NTSTATUS ntlmssp_set_hashes(struct ntlmssp_state *ntlmssp_state,
117                             const uint8_t lm_hash[16],
118                             const uint8_t nt_hash[16])
119 {
120         ntlmssp_state->lm_hash = (uint8_t *)
121                 TALLOC_MEMDUP(ntlmssp_state, lm_hash, 16);
122         ntlmssp_state->nt_hash = (uint8_t *)
123                 TALLOC_MEMDUP(ntlmssp_state, nt_hash, 16);
124         if (!ntlmssp_state->lm_hash || !ntlmssp_state->nt_hash) {
125                 TALLOC_FREE(ntlmssp_state->lm_hash);
126                 TALLOC_FREE(ntlmssp_state->nt_hash);
127                 return NT_STATUS_NO_MEMORY;
128         }
129         return NT_STATUS_OK;
130 }
131
132 /**
133  * Converts a password to the hashes on an NTLMSSP context.
134  *
135  */
136 NTSTATUS ntlmssp_set_password(struct ntlmssp_state *ntlmssp_state, const char *password)
137 {
138         if (!password) {
139                 ntlmssp_state->lm_hash = NULL;
140                 ntlmssp_state->nt_hash = NULL;
141         } else {
142                 uint8_t lm_hash[16];
143                 uint8_t nt_hash[16];
144
145                 E_deshash(password, lm_hash);
146                 E_md4hash(password, nt_hash);
147                 return ntlmssp_set_hashes(ntlmssp_state, lm_hash, nt_hash);
148         }
149         return NT_STATUS_OK;
150 }
151
152 /**
153  * Set a domain on an NTLMSSP context - ensures it is talloc()ed
154  *
155  */
156 NTSTATUS ntlmssp_set_domain(struct ntlmssp_state *ntlmssp_state, const char *domain)
157 {
158         ntlmssp_state->domain = talloc_strdup(ntlmssp_state,
159                                               domain ? domain : "" );
160         if (!ntlmssp_state->domain) {
161                 return NT_STATUS_NO_MEMORY;
162         }
163         return NT_STATUS_OK;
164 }
165
166 /**
167  * Request features for the NTLMSSP negotiation
168  *
169  * @param ntlmssp_state NTLMSSP state
170  * @param feature_list List of space seperated features requested from NTLMSSP.
171  */
172 void ntlmssp_want_feature_list(struct ntlmssp_state *ntlmssp_state, char *feature_list)
173 {
174         /*
175          * We need to set this to allow a later SetPassword
176          * via the SAMR pipe to succeed. Strange.... We could
177          * also add  NTLMSSP_NEGOTIATE_SEAL here. JRA.
178          */
179         if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
180                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
181         }
182         if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
183                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
184         }
185         if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
186                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
187         }
188         if (in_list("NTLMSSP_FEATURE_CCACHE", feature_list, true)) {
189                 ntlmssp_state->use_ccache = true;
190         }
191 }
192
193 /**
194  * Request a feature for the NTLMSSP negotiation
195  *
196  * @param ntlmssp_state NTLMSSP state
197  * @param feature Bit flag specifying the requested feature
198  */
199 void ntlmssp_want_feature(struct ntlmssp_state *ntlmssp_state, uint32_t feature)
200 {
201         /* As per JRA's comment above */
202         if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
203                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
204         }
205         if (feature & NTLMSSP_FEATURE_SIGN) {
206                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
207         }
208         if (feature & NTLMSSP_FEATURE_SEAL) {
209                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
210         }
211         if (feature & NTLMSSP_FEATURE_CCACHE) {
212                 ntlmssp_state->use_ccache = true;
213         }
214 }
215
216 /**
217  * Next state function for the NTLMSSP state machine
218  *
219  * @param ntlmssp_state NTLMSSP State
220  * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
221  * @param out The reply, as an allocated DATA_BLOB, caller to free.
222  * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
223  */
224
225 NTSTATUS ntlmssp_update(struct ntlmssp_state *ntlmssp_state,
226                         const DATA_BLOB input, DATA_BLOB *out)
227 {
228         uint32_t ntlmssp_command;
229         int i;
230
231         if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
232                 /* Called update after negotiations finished. */
233                 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
234                 return NT_STATUS_INVALID_PARAMETER;
235         }
236
237         *out = data_blob_null;
238
239         if (!input.length) {
240                 switch (ntlmssp_state->role) {
241                 case NTLMSSP_CLIENT:
242                         ntlmssp_command = NTLMSSP_INITIAL;
243                         break;
244                 case NTLMSSP_SERVER:
245                         /* 'datagram' mode - no neg packet */
246                         ntlmssp_command = NTLMSSP_NEGOTIATE;
247                         break;
248                 }
249         } else {
250                 if (!msrpc_parse(ntlmssp_state, &input, "Cd",
251                                  "NTLMSSP",
252                                  &ntlmssp_command)) {
253                         DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
254                         dump_data(2, input.data, input.length);
255                         return NT_STATUS_INVALID_PARAMETER;
256                 }
257         }
258
259         if (ntlmssp_command != ntlmssp_state->expected_state) {
260                 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
261                 return NT_STATUS_INVALID_PARAMETER;
262         }
263
264         for (i=0; ntlmssp_callbacks[i].fn; i++) {
265                 if (ntlmssp_callbacks[i].role == ntlmssp_state->role
266                     && ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
267                         return ntlmssp_callbacks[i].fn(ntlmssp_state, input, out);
268                 }
269         }
270
271         DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
272                   ntlmssp_state->role, ntlmssp_command));
273
274         return NT_STATUS_INVALID_PARAMETER;
275 }
276
277 /**
278  * Next state function for the Negotiate packet
279  *
280  * @param ntlmssp_state NTLMSSP State
281  * @param request The request, as a DATA_BLOB
282  * @param request The reply, as an allocated DATA_BLOB, caller to free.
283  * @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent.
284  */
285
286 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
287                                          const DATA_BLOB request, DATA_BLOB *reply)
288 {
289         DATA_BLOB struct_blob;
290         uint32_t neg_flags = 0;
291         uint32_t ntlmssp_command, chal_flags;
292         uint8_t cryptkey[8];
293         const char *target_name;
294         NTSTATUS status;
295
296         /* parse the NTLMSSP packet */
297 #if 0
298         file_save("ntlmssp_negotiate.dat", request.data, request.length);
299 #endif
300
301         if (request.length) {
302                 if ((request.length < 16) || !msrpc_parse(ntlmssp_state, &request, "Cdd",
303                                                           "NTLMSSP",
304                                                           &ntlmssp_command,
305                                                           &neg_flags)) {
306                         DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
307                                 (unsigned int)request.length));
308                         dump_data(2, request.data, request.length);
309                         return NT_STATUS_INVALID_PARAMETER;
310                 }
311                 debug_ntlmssp_flags(neg_flags);
312
313                 if (DEBUGLEVEL >= 10) {
314                         struct NEGOTIATE_MESSAGE *negotiate = talloc(
315                                 talloc_tos(), struct NEGOTIATE_MESSAGE);
316                         if (negotiate != NULL) {
317                                 status = ntlmssp_pull_NEGOTIATE_MESSAGE(
318                                         &request, negotiate, negotiate);
319                                 if (NT_STATUS_IS_OK(status)) {
320                                         NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
321                                                         negotiate);
322                                 }
323                                 TALLOC_FREE(negotiate);
324                         }
325                 }
326         }
327
328         ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, ntlmssp_state->allow_lm_key);
329
330         /* Ask our caller what challenge they would like in the packet */
331         status = ntlmssp_state->get_challenge(ntlmssp_state, cryptkey);
332         if (!NT_STATUS_IS_OK(status)) {
333                 DEBUG(1, ("ntlmssp_server_negotiate: backend doesn't give a challenge: %s\n",
334                           nt_errstr(status)));
335                 return status;
336         }
337
338         /* Check if we may set the challenge */
339         if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
340                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
341         }
342
343         /* The flags we send back are not just the negotiated flags,
344          * they are also 'what is in this packet'.  Therfore, we
345          * operate on 'chal_flags' from here on
346          */
347
348         chal_flags = ntlmssp_state->neg_flags;
349
350         /* get the right name to fill in as 'target' */
351         target_name = ntlmssp_target_name(ntlmssp_state,
352                                           neg_flags, &chal_flags);
353         if (target_name == NULL)
354                 return NT_STATUS_INVALID_PARAMETER;
355
356         ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
357         ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
358                                                         cryptkey, 8);
359
360         /* This creates the 'blob' of names that appears at the end of the packet */
361         if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
362         {
363                 msrpc_gen(ntlmssp_state, &struct_blob, "aaaaa",
364                           MsvAvNbDomainName, target_name,
365                           MsvAvNbComputerName, ntlmssp_state->server.netbios_name,
366                           MsvAvDnsDomainName, ntlmssp_state->server.dns_domain,
367                           MsvAvDnsComputerName, ntlmssp_state->server.dns_name,
368                           MsvAvEOL, "");
369         } else {
370                 struct_blob = data_blob_null;
371         }
372
373         {
374                 /* Marshal the packet in the right format, be it unicode or ASCII */
375                 const char *gen_string;
376                 DATA_BLOB version_blob = data_blob_null;
377
378                 if (chal_flags & NTLMSSP_NEGOTIATE_VERSION) {
379                         enum ndr_err_code err;
380                         struct VERSION vers;
381
382                         /* "What Windows returns" as a version number. */
383                         ZERO_STRUCT(vers);
384                         vers.ProductMajorVersion = NTLMSSP_WINDOWS_MAJOR_VERSION_6;
385                         vers.ProductMinorVersion = NTLMSSP_WINDOWS_MINOR_VERSION_1;
386                         vers.ProductBuild = 0;
387                         vers.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
388
389                         err = ndr_push_struct_blob(&version_blob,
390                                                 ntlmssp_state,
391                                                 &vers,
392                                                 (ndr_push_flags_fn_t)ndr_push_VERSION);
393
394                         if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
395                                 return NT_STATUS_NO_MEMORY;
396                         }
397                 }
398
399                 if (ntlmssp_state->unicode) {
400                         gen_string = "CdUdbddBb";
401                 } else {
402                         gen_string = "CdAdbddBb";
403                 }
404
405                 msrpc_gen(ntlmssp_state, reply, gen_string,
406                         "NTLMSSP",
407                         NTLMSSP_CHALLENGE,
408                         target_name,
409                         chal_flags,
410                         cryptkey, 8,
411                         0, 0,
412                         struct_blob.data, struct_blob.length,
413                         version_blob.data, version_blob.length);
414
415                 data_blob_free(&version_blob);
416
417                 if (DEBUGLEVEL >= 10) {
418                         struct CHALLENGE_MESSAGE *challenge = talloc(
419                                 talloc_tos(), struct CHALLENGE_MESSAGE);
420                         if (challenge != NULL) {
421                                 challenge->NegotiateFlags = chal_flags;
422                                 status = ntlmssp_pull_CHALLENGE_MESSAGE(
423                                         reply, challenge, challenge);
424                                 if (NT_STATUS_IS_OK(status)) {
425                                         NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
426                                                         challenge);
427                                 }
428                                 TALLOC_FREE(challenge);
429                         }
430                 }
431         }
432
433         data_blob_free(&struct_blob);
434
435         ntlmssp_state->expected_state = NTLMSSP_AUTH;
436
437         return NT_STATUS_MORE_PROCESSING_REQUIRED;
438 }
439
440 /**
441  * Next state function for the Authenticate packet
442  *
443  * @param ntlmssp_state NTLMSSP State
444  * @param request The request, as a DATA_BLOB
445  * @param request The reply, as an allocated DATA_BLOB, caller to free.
446  * @return Errors or NT_STATUS_OK.
447  */
448
449 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
450                                     const DATA_BLOB request, DATA_BLOB *reply)
451 {
452         DATA_BLOB encrypted_session_key = data_blob_null;
453         DATA_BLOB user_session_key = data_blob_null;
454         DATA_BLOB lm_session_key = data_blob_null;
455         DATA_BLOB session_key = data_blob_null;
456         uint32_t ntlmssp_command, auth_flags;
457         NTSTATUS nt_status = NT_STATUS_OK;
458
459         /* used by NTLM2 */
460         bool doing_ntlm2 = False;
461
462         uint8_t session_nonce[16];
463         uint8_t session_nonce_hash[16];
464
465         const char *parse_string;
466
467         /* parse the NTLMSSP packet */
468         *reply = data_blob_null;
469
470 #if 0
471         file_save("ntlmssp_auth.dat", request.data, request.length);
472 #endif
473
474         if (ntlmssp_state->unicode) {
475                 parse_string = "CdBBUUUBd";
476         } else {
477                 parse_string = "CdBBAAABd";
478         }
479
480         data_blob_free(&ntlmssp_state->lm_resp);
481         data_blob_free(&ntlmssp_state->nt_resp);
482
483         ntlmssp_state->user = NULL;
484         ntlmssp_state->domain = NULL;
485
486         /* now the NTLMSSP encoded auth hashes */
487         if (!msrpc_parse(ntlmssp_state, &request, parse_string,
488                          "NTLMSSP",
489                          &ntlmssp_command,
490                          &ntlmssp_state->lm_resp,
491                          &ntlmssp_state->nt_resp,
492                          &ntlmssp_state->domain,
493                          &ntlmssp_state->user,
494                          &ntlmssp_state->client.netbios_name,
495                          &encrypted_session_key,
496                          &auth_flags)) {
497                 auth_flags = 0;
498
499                 /* Try again with a shorter string (Win9X truncates this packet) */
500                 if (ntlmssp_state->unicode) {
501                         parse_string = "CdBBUUU";
502                 } else {
503                         parse_string = "CdBBAAA";
504                 }
505
506                 /* now the NTLMSSP encoded auth hashes */
507                 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
508                                  "NTLMSSP",
509                                  &ntlmssp_command,
510                                  &ntlmssp_state->lm_resp,
511                                  &ntlmssp_state->nt_resp,
512                                  &ntlmssp_state->domain,
513                                  &ntlmssp_state->user,
514                                  &ntlmssp_state->client.netbios_name)) {
515                         DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
516                         dump_data(2, request.data, request.length);
517
518                         return NT_STATUS_INVALID_PARAMETER;
519                 }
520         }
521
522         if (auth_flags)
523                 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, ntlmssp_state->allow_lm_key);
524
525         if (DEBUGLEVEL >= 10) {
526                 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
527                         talloc_tos(), struct AUTHENTICATE_MESSAGE);
528                 if (authenticate != NULL) {
529                         NTSTATUS status;
530                         authenticate->NegotiateFlags = auth_flags;
531                         status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
532                                 &request, authenticate, authenticate);
533                         if (NT_STATUS_IS_OK(status)) {
534                                 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
535                                                 authenticate);
536                         }
537                         TALLOC_FREE(authenticate);
538                 }
539         }
540
541         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
542                  ntlmssp_state->user, ntlmssp_state->domain,
543                  ntlmssp_state->client.netbios_name,
544                  (unsigned long)ntlmssp_state->lm_resp.length,
545                  (unsigned long)ntlmssp_state->nt_resp.length));
546
547 #if 0
548         file_save("nthash1.dat",  &ntlmssp_state->nt_resp.data,  &ntlmssp_state->nt_resp.length);
549         file_save("lmhash1.dat",  &ntlmssp_state->lm_resp.data,  &ntlmssp_state->lm_resp.length);
550 #endif
551
552         /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
553            client challenge
554
555            However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
556         */
557         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
558                 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
559                         struct MD5Context md5_session_nonce_ctx;
560                         SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
561
562                         doing_ntlm2 = True;
563
564                         memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
565                         memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
566
567                         MD5Init(&md5_session_nonce_ctx);
568                         MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
569                         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
570
571                         ntlmssp_state->chal = data_blob_talloc(
572                                 ntlmssp_state, session_nonce_hash, 8);
573
574                         /* LM response is no longer useful */
575                         data_blob_free(&ntlmssp_state->lm_resp);
576
577                         /* We changed the effective challenge - set it */
578                         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
579                                 data_blob_free(&encrypted_session_key);
580                                 return nt_status;
581                         }
582
583                         /* LM Key is incompatible. */
584                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
585                 }
586         }
587
588         /*
589          * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
590          * is required (by "ntlm auth = no" and "lm auth = no" being set in the
591          * smb.conf file) and no NTLMv2 response was sent then the password check
592          * will fail here. JRA.
593          */
594
595         /* Finally, actually ask if the password is OK */
596
597         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
598                                                                        &user_session_key, &lm_session_key))) {
599                 data_blob_free(&encrypted_session_key);
600                 return nt_status;
601         }
602
603         dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
604         dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
605
606         /* Handle the different session key derivation for NTLM2 */
607         if (doing_ntlm2) {
608                 if (user_session_key.data && user_session_key.length == 16) {
609                         session_key = data_blob_talloc(ntlmssp_state,
610                                                        NULL, 16);
611                         hmac_md5(user_session_key.data, session_nonce,
612                                  sizeof(session_nonce), session_key.data);
613                         DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
614                         dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
615
616                 } else {
617                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
618                         session_key = data_blob_null;
619                 }
620         } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
621                 if (lm_session_key.data && lm_session_key.length >= 8) {
622                         if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
623                                 session_key = data_blob_talloc(ntlmssp_state,
624                                                                NULL, 16);
625                                 if (session_key.data == NULL) {
626                                         return NT_STATUS_NO_MEMORY;
627                                 }
628                                 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
629                                                           session_key.data);
630                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
631                         } else {
632                                 static const uint8_t zeros[24] = {0, };
633                                 session_key = data_blob_talloc(
634                                         ntlmssp_state, NULL, 16);
635                                 if (session_key.data == NULL) {
636                                         return NT_STATUS_NO_MEMORY;
637                                 }
638                                 SMBsesskeygen_lm_sess_key(zeros, zeros,
639                                                           session_key.data);
640                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
641                         }
642                         dump_data_pw("LM session key:\n", session_key.data,
643                                      session_key.length);
644                 } else {
645                         /* LM Key not selected */
646                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
647
648                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
649                         session_key = data_blob_null;
650                 }
651         } else if (user_session_key.data) {
652                 session_key = user_session_key;
653                 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
654                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
655
656                 /* LM Key not selected */
657                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
658
659         } else if (lm_session_key.data) {
660                 /* Very weird to have LM key, but no user session key, but anyway.. */
661                 session_key = lm_session_key;
662                 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
663                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
664
665                 /* LM Key not selected */
666                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
667
668         } else {
669                 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
670                 session_key = data_blob_null;
671
672                 /* LM Key not selected */
673                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
674         }
675
676         /* With KEY_EXCH, the client supplies the proposed session key,
677            but encrypts it with the long-term key */
678         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
679                 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
680                         data_blob_free(&encrypted_session_key);
681                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
682                                   (unsigned int)encrypted_session_key.length));
683                         return NT_STATUS_INVALID_PARAMETER;
684                 } else if (!session_key.data || session_key.length != 16) {
685                         DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
686                                   (unsigned int)session_key.length));
687                         ntlmssp_state->session_key = session_key;
688                 } else {
689                         dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
690                         arcfour_crypt_blob(encrypted_session_key.data,
691                                            encrypted_session_key.length,
692                                            &session_key);
693                         ntlmssp_state->session_key = data_blob_talloc(
694                                 ntlmssp_state, encrypted_session_key.data,
695                                 encrypted_session_key.length);
696                         dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
697                                      encrypted_session_key.length);
698                 }
699         } else {
700                 ntlmssp_state->session_key = session_key;
701         }
702
703         if (!NT_STATUS_IS_OK(nt_status)) {
704                 ntlmssp_state->session_key = data_blob_null;
705         } else if (ntlmssp_state->session_key.length) {
706                 nt_status = ntlmssp_sign_init(ntlmssp_state);
707         }
708
709         data_blob_free(&encrypted_session_key);
710
711         /* Only one authentication allowed per server state. */
712         ntlmssp_state->expected_state = NTLMSSP_DONE;
713
714         return nt_status;
715 }
716
717 /**
718  * Create an NTLMSSP state machine
719  *
720  * @param ntlmssp_state NTLMSSP State, allocated by this function
721  */
722
723 NTSTATUS ntlmssp_server_start(TALLOC_CTX *mem_ctx,
724                               bool is_standalone,
725                               const char *netbios_name,
726                               const char *netbios_domain,
727                               const char *dns_name,
728                               const char *dns_domain,
729                               struct ntlmssp_state **_ntlmssp_state)
730 {
731         struct ntlmssp_state *ntlmssp_state;
732
733         if (!netbios_name) {
734                 netbios_name = "";
735         }
736
737         if (!netbios_domain) {
738                 netbios_domain = "";
739         }
740
741         if (!dns_domain) {
742                 dns_domain = "";
743         }
744
745         if (!dns_name) {
746                 dns_name = "";
747         }
748
749         ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
750         if (!ntlmssp_state) {
751                 return NT_STATUS_NO_MEMORY;
752         }
753
754         ntlmssp_state->role = NTLMSSP_SERVER;
755
756         ntlmssp_state->get_challenge = get_challenge;
757         ntlmssp_state->set_challenge = set_challenge;
758         ntlmssp_state->may_set_challenge = may_set_challenge;
759
760         ntlmssp_state->server.is_standalone = is_standalone;
761
762         ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
763
764         ntlmssp_state->allow_lm_key = lp_lanman_auth();
765
766         ntlmssp_state->neg_flags =
767                 NTLMSSP_NEGOTIATE_128 |
768                 NTLMSSP_NEGOTIATE_56 |
769                 NTLMSSP_NEGOTIATE_VERSION |
770                 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
771                 NTLMSSP_NEGOTIATE_NTLM |
772                 NTLMSSP_NEGOTIATE_NTLM2 |
773                 NTLMSSP_NEGOTIATE_KEY_EXCH |
774                 NTLMSSP_NEGOTIATE_SIGN |
775                 NTLMSSP_NEGOTIATE_SEAL;
776
777         ntlmssp_state->server.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
778         if (!ntlmssp_state->server.netbios_name) {
779                 talloc_free(ntlmssp_state);
780                 return NT_STATUS_NO_MEMORY;
781         }
782         ntlmssp_state->server.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
783         if (!ntlmssp_state->server.netbios_domain) {
784                 talloc_free(ntlmssp_state);
785                 return NT_STATUS_NO_MEMORY;
786         }
787         ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state, dns_name);
788         if (!ntlmssp_state->server.dns_name) {
789                 talloc_free(ntlmssp_state);
790                 return NT_STATUS_NO_MEMORY;
791         }
792         ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state, dns_domain);
793         if (!ntlmssp_state->server.dns_domain) {
794                 talloc_free(ntlmssp_state);
795                 return NT_STATUS_NO_MEMORY;
796         }
797
798         *_ntlmssp_state = ntlmssp_state;
799         return NT_STATUS_OK;
800 }
801
802 /*********************************************************************
803  Client side NTLMSSP
804 *********************************************************************/
805
806 /**
807  * Next state function for the Initial packet
808  *
809  * @param ntlmssp_state NTLMSSP State
810  * @param request The request, as a DATA_BLOB.  reply.data must be NULL
811  * @param request The reply, as an allocated DATA_BLOB, caller to free.
812  * @return Errors or NT_STATUS_OK.
813  */
814
815 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
816                                   DATA_BLOB reply, DATA_BLOB *next_request)
817 {
818         if (ntlmssp_state->unicode) {
819                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
820         } else {
821                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
822         }
823
824         if (ntlmssp_state->use_ntlmv2) {
825                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
826         }
827
828         /* generate the ntlmssp negotiate packet */
829         msrpc_gen(ntlmssp_state, next_request, "CddAA",
830                   "NTLMSSP",
831                   NTLMSSP_NEGOTIATE,
832                   ntlmssp_state->neg_flags,
833                   ntlmssp_state->client.netbios_domain,
834                   ntlmssp_state->client.netbios_name);
835
836         if (DEBUGLEVEL >= 10) {
837                 struct NEGOTIATE_MESSAGE *negotiate = talloc(
838                         talloc_tos(), struct NEGOTIATE_MESSAGE);
839                 if (negotiate != NULL) {
840                         NTSTATUS status;
841                         status = ntlmssp_pull_NEGOTIATE_MESSAGE(
842                                 next_request, negotiate, negotiate);
843                         if (NT_STATUS_IS_OK(status)) {
844                                 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
845                                                 negotiate);
846                         }
847                         TALLOC_FREE(negotiate);
848                 }
849         }
850
851         ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
852
853         return NT_STATUS_MORE_PROCESSING_REQUIRED;
854 }
855
856 /**
857  * Next state function for the Challenge Packet.  Generate an auth packet.
858  *
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.
863  */
864
865 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
866                                          const DATA_BLOB reply, DATA_BLOB *next_request)
867 {
868         uint32_t chal_flags, ntlmssp_command, unkn1, unkn2;
869         DATA_BLOB server_domain_blob;
870         DATA_BLOB challenge_blob;
871         DATA_BLOB struct_blob = data_blob_null;
872         char *server_domain;
873         const char *chal_parse_string;
874         const char *auth_gen_string;
875         DATA_BLOB lm_response = data_blob_null;
876         DATA_BLOB nt_response = data_blob_null;
877         DATA_BLOB session_key = data_blob_null;
878         DATA_BLOB encrypted_session_key = data_blob_null;
879         NTSTATUS nt_status = NT_STATUS_OK;
880
881         if (ntlmssp_state->use_ccache) {
882                 struct wbcCredentialCacheParams params;
883                 struct wbcCredentialCacheInfo *info = NULL;
884                 struct wbcAuthErrorInfo *error = NULL;
885                 struct wbcNamedBlob auth_blob;
886                 struct wbcBlob *wbc_next = NULL;
887                 struct wbcBlob *wbc_session_key = NULL;
888                 wbcErr wbc_status;
889                 int i;
890
891                 params.account_name = ntlmssp_state->user;
892                 params.domain_name = ntlmssp_state->domain;
893                 params.level = WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP;
894
895                 auth_blob.name = "challenge_blob";
896                 auth_blob.flags = 0;
897                 auth_blob.blob.data = reply.data;
898                 auth_blob.blob.length = reply.length;
899                 params.num_blobs = 1;
900                 params.blobs = &auth_blob;
901
902                 wbc_status = wbcCredentialCache(&params, &info, &error);
903                 if (error != NULL) {
904                         wbcFreeMemory(error);
905                 }
906                 if (!WBC_ERROR_IS_OK(wbc_status)) {
907                         goto noccache;
908                 }
909
910                 for (i=0; i<info->num_blobs; i++) {
911                         if (strequal(info->blobs[i].name, "auth_blob")) {
912                                 wbc_next = &info->blobs[i].blob;
913                         }
914                         if (strequal(info->blobs[i].name, "session_key")) {
915                                 wbc_session_key = &info->blobs[i].blob;
916                         }
917                 }
918                 if ((wbc_next == NULL) || (wbc_session_key == NULL)) {
919                         wbcFreeMemory(info);
920                         goto noccache;
921                 }
922
923                 *next_request = data_blob(wbc_next->data, wbc_next->length);
924                 ntlmssp_state->session_key = data_blob(
925                         wbc_session_key->data, wbc_session_key->length);
926
927                 wbcFreeMemory(info);
928                 goto done;
929         }
930
931 noccache:
932
933         if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
934                          "NTLMSSP",
935                          &ntlmssp_command,
936                          &server_domain_blob,
937                          &chal_flags)) {
938                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
939                 dump_data(2, reply.data, reply.length);
940
941                 return NT_STATUS_INVALID_PARAMETER;
942         }
943
944         if (DEBUGLEVEL >= 10) {
945                 struct CHALLENGE_MESSAGE *challenge = talloc(
946                         talloc_tos(), struct CHALLENGE_MESSAGE);
947                 if (challenge != NULL) {
948                         NTSTATUS status;
949                         challenge->NegotiateFlags = chal_flags;
950                         status = ntlmssp_pull_CHALLENGE_MESSAGE(
951                                 &reply, challenge, challenge);
952                         if (NT_STATUS_IS_OK(status)) {
953                                 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
954                                                 challenge);
955                         }
956                         TALLOC_FREE(challenge);
957                 }
958         }
959
960         data_blob_free(&server_domain_blob);
961
962         DEBUG(3, ("Got challenge flags:\n"));
963         debug_ntlmssp_flags(chal_flags);
964
965         ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
966
967         if (ntlmssp_state->unicode) {
968                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
969                         chal_parse_string = "CdUdbddB";
970                 } else {
971                         chal_parse_string = "CdUdbdd";
972                 }
973                 auth_gen_string = "CdBBUUUBd";
974         } else {
975                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
976                         chal_parse_string = "CdAdbddB";
977                 } else {
978                         chal_parse_string = "CdAdbdd";
979                 }
980
981                 auth_gen_string = "CdBBAAABd";
982         }
983
984         DEBUG(3, ("NTLMSSP: Set final flags:\n"));
985         debug_ntlmssp_flags(ntlmssp_state->neg_flags);
986
987         if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
988                          "NTLMSSP",
989                          &ntlmssp_command,
990                          &server_domain,
991                          &chal_flags,
992                          &challenge_blob, 8,
993                          &unkn1, &unkn2,
994                          &struct_blob)) {
995                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
996                 dump_data(2, reply.data, reply.length);
997                 return NT_STATUS_INVALID_PARAMETER;
998         }
999
1000         if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
1001                 ntlmssp_state->server.is_standalone = true;
1002         } else {
1003                 ntlmssp_state->server.is_standalone = false;
1004         }
1005         /* TODO: parse struct_blob and fill in the rest */
1006         ntlmssp_state->server.netbios_name = "";
1007         ntlmssp_state->server.netbios_domain = server_domain;
1008         ntlmssp_state->server.dns_name = "";
1009         ntlmssp_state->server.dns_domain = "";
1010
1011         if (challenge_blob.length != 8) {
1012                 data_blob_free(&struct_blob);
1013                 return NT_STATUS_INVALID_PARAMETER;
1014         }
1015
1016         if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
1017                 static const uint8_t zeros[16] = {0, };
1018                 /* do nothing - blobs are zero length */
1019
1020                 /* session key is all zeros */
1021                 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
1022
1023                 /* not doing NLTM2 without a password */
1024                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
1025         } else if (ntlmssp_state->use_ntlmv2) {
1026                 if (!struct_blob.length) {
1027                         /* be lazy, match win2k - we can't do NTLMv2 without it */
1028                         DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1029                         return NT_STATUS_INVALID_PARAMETER;
1030                 }
1031
1032                 /* TODO: if the remote server is standalone, then we should replace 'domain'
1033                    with the server name as supplied above */
1034
1035                 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
1036                                            ntlmssp_state->user,
1037                                            ntlmssp_state->domain,
1038                                            ntlmssp_state->nt_hash, &challenge_blob,
1039                                            &struct_blob,
1040                                            &lm_response, &nt_response, NULL,
1041                                            &session_key)) {
1042                         data_blob_free(&challenge_blob);
1043                         data_blob_free(&struct_blob);
1044                         return NT_STATUS_NO_MEMORY;
1045                 }
1046         } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1047                 struct MD5Context md5_session_nonce_ctx;
1048                 uint8_t session_nonce[16];
1049                 uint8_t session_nonce_hash[16];
1050                 uint8_t user_session_key[16];
1051
1052                 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1053                 generate_random_buffer(lm_response.data, 8);
1054                 memset(lm_response.data+8, 0, 16);
1055
1056                 memcpy(session_nonce, challenge_blob.data, 8);
1057                 memcpy(&session_nonce[8], lm_response.data, 8);
1058
1059                 MD5Init(&md5_session_nonce_ctx);
1060                 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1061                 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1062                 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1063
1064                 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1065                 DEBUG(5, ("challenge is: \n"));
1066                 dump_data(5, session_nonce_hash, 8);
1067
1068                 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1069                 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
1070                                   session_nonce_hash,
1071                                   nt_response.data);
1072
1073                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1074
1075                 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
1076                 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1077                 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1078         } else {
1079                 /* lanman auth is insecure, it may be disabled */
1080                 if (lp_client_lanman_auth()) {
1081                         lm_response = data_blob_talloc(ntlmssp_state,
1082                                                        NULL, 24);
1083                         SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
1084                                    lm_response.data);
1085                 }
1086
1087                 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1088                 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
1089                              nt_response.data);
1090
1091                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1092                 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1093                     && lp_client_lanman_auth()) {
1094                         SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
1095                                         session_key.data);
1096                         dump_data_pw("LM session key\n", session_key.data, session_key.length);
1097                 } else {
1098                         SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
1099                         dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1100                 }
1101         }
1102         data_blob_free(&struct_blob);
1103
1104         /* Key exchange encryptes a new client-generated session key with
1105            the password-derived key */
1106         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1107                 /* Make up a new session key */
1108                 uint8_t client_session_key[16];
1109                 generate_random_buffer(client_session_key, sizeof(client_session_key));
1110
1111                 /* Encrypt the new session key with the old one */
1112                 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1113                 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1114                 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
1115                 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1116
1117                 /* Mark the new session key as the 'real' session key */
1118                 data_blob_free(&session_key);
1119                 session_key = data_blob_talloc(ntlmssp_state,
1120                                                client_session_key,
1121                                                sizeof(client_session_key));
1122         }
1123
1124         /* this generates the actual auth packet */
1125         if (!msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
1126                        "NTLMSSP",
1127                        NTLMSSP_AUTH,
1128                        lm_response.data, lm_response.length,
1129                        nt_response.data, nt_response.length,
1130                        ntlmssp_state->domain,
1131                        ntlmssp_state->user,
1132                        ntlmssp_state->client.netbios_name,
1133                        encrypted_session_key.data, encrypted_session_key.length,
1134                        ntlmssp_state->neg_flags)) {
1135
1136                 return NT_STATUS_NO_MEMORY;
1137         }
1138
1139         if (DEBUGLEVEL >= 10) {
1140                 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
1141                         talloc_tos(), struct AUTHENTICATE_MESSAGE);
1142                 if (authenticate != NULL) {
1143                         NTSTATUS status;
1144                         authenticate->NegotiateFlags =
1145                                 ntlmssp_state->neg_flags;
1146                         status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
1147                                 next_request, authenticate, authenticate);
1148                         if (NT_STATUS_IS_OK(status)) {
1149                                 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
1150                                                 authenticate);
1151                         }
1152                         TALLOC_FREE(authenticate);
1153                 }
1154         }
1155
1156         data_blob_free(&encrypted_session_key);
1157
1158         data_blob_free(&ntlmssp_state->chal);
1159
1160         ntlmssp_state->session_key = session_key;
1161
1162         ntlmssp_state->chal = challenge_blob;
1163         ntlmssp_state->lm_resp = lm_response;
1164         ntlmssp_state->nt_resp = nt_response;
1165
1166 done:
1167
1168         ntlmssp_state->expected_state = NTLMSSP_DONE;
1169
1170         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1171                 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1172         }
1173
1174         return nt_status;
1175 }
1176
1177 NTSTATUS ntlmssp_client_start(TALLOC_CTX *mem_ctx,
1178                               const char *netbios_name,
1179                               const char *netbios_domain,
1180                               bool use_ntlmv2,
1181                               struct ntlmssp_state **_ntlmssp_state)
1182 {
1183         struct ntlmssp_state *ntlmssp_state;
1184
1185         if (!netbios_name) {
1186                 netbios_name = "";
1187         }
1188
1189         if (!netbios_domain) {
1190                 netbios_domain = "";
1191         }
1192
1193         ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
1194         if (!ntlmssp_state) {
1195                 return NT_STATUS_NO_MEMORY;
1196         }
1197
1198         ntlmssp_state->role = NTLMSSP_CLIENT;
1199
1200         ntlmssp_state->unicode = True;
1201
1202         ntlmssp_state->use_ntlmv2 = use_ntlmv2;
1203
1204         ntlmssp_state->expected_state = NTLMSSP_INITIAL;
1205
1206         ntlmssp_state->neg_flags =
1207                 NTLMSSP_NEGOTIATE_128 |
1208                 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
1209                 NTLMSSP_NEGOTIATE_NTLM |
1210                 NTLMSSP_NEGOTIATE_NTLM2 |
1211                 NTLMSSP_NEGOTIATE_KEY_EXCH |
1212                 NTLMSSP_REQUEST_TARGET;
1213
1214         ntlmssp_state->client.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
1215         if (!ntlmssp_state->client.netbios_name) {
1216                 talloc_free(ntlmssp_state);
1217                 return NT_STATUS_NO_MEMORY;
1218         }
1219         ntlmssp_state->client.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
1220         if (!ntlmssp_state->client.netbios_domain) {
1221                 talloc_free(ntlmssp_state);
1222                 return NT_STATUS_NO_MEMORY;
1223         }
1224
1225         *_ntlmssp_state = ntlmssp_state;
1226         return NT_STATUS_OK;
1227 }