s3:ntlmssp Always call ntlmssp_sign_init()
[abartlet/samba.git/.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                                 ntlmssp_state, 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                         ntlmssp_state, 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                 /* Ensure we can never get here on NTLMv2 */
622                 && (ntlmssp_state->nt_resp.length == 0 || ntlmssp_state->nt_resp.length == 24)) {
623
624                 if (lm_session_key.data && lm_session_key.length >= 8) {
625                         if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
626                                 session_key = data_blob_talloc(ntlmssp_state,
627                                                                NULL, 16);
628                                 if (session_key.data == NULL) {
629                                         return NT_STATUS_NO_MEMORY;
630                                 }
631                                 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
632                                                           session_key.data);
633                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
634                         } else {
635                                 static const uint8_t zeros[24] = {0, };
636                                 session_key = data_blob_talloc(
637                                         ntlmssp_state, NULL, 16);
638                                 if (session_key.data == NULL) {
639                                         return NT_STATUS_NO_MEMORY;
640                                 }
641                                 SMBsesskeygen_lm_sess_key(zeros, zeros,
642                                                           session_key.data);
643                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
644                         }
645                         dump_data_pw("LM session key:\n", session_key.data,
646                                      session_key.length);
647                 } else {
648                         /* LM Key not selected */
649                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
650
651                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
652                         session_key = data_blob_null;
653                 }
654         } else if (user_session_key.data) {
655                 session_key = user_session_key;
656                 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
657                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
658
659                 /* LM Key not selected */
660                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
661
662         } else if (lm_session_key.data) {
663                 /* Very weird to have LM key, but no user session key, but anyway.. */
664                 session_key = lm_session_key;
665                 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
666                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
667
668                 /* LM Key not selected */
669                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
670
671         } else {
672                 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
673                 session_key = data_blob_null;
674
675                 /* LM Key not selected */
676                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
677         }
678
679         /* With KEY_EXCH, the client supplies the proposed session key,
680            but encrypts it with the long-term key */
681         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
682                 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
683                         data_blob_free(&encrypted_session_key);
684                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
685                                   (unsigned int)encrypted_session_key.length));
686                         return NT_STATUS_INVALID_PARAMETER;
687                 } else if (!session_key.data || session_key.length != 16) {
688                         DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
689                                   (unsigned int)session_key.length));
690                         ntlmssp_state->session_key = session_key;
691                 } else {
692                         dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
693                         arcfour_crypt_blob(encrypted_session_key.data,
694                                            encrypted_session_key.length,
695                                            &session_key);
696                         ntlmssp_state->session_key = data_blob_talloc(
697                                 ntlmssp_state, encrypted_session_key.data,
698                                 encrypted_session_key.length);
699                         dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
700                                      encrypted_session_key.length);
701                 }
702         } else {
703                 ntlmssp_state->session_key = session_key;
704         }
705
706         if (ntlmssp_state->session_key.length) {
707                 nt_status = ntlmssp_sign_init(ntlmssp_state);
708         }
709
710         data_blob_free(&encrypted_session_key);
711
712         /* Only one authentication allowed per server state. */
713         ntlmssp_state->expected_state = NTLMSSP_DONE;
714
715         return nt_status;
716 }
717
718 /**
719  * Create an NTLMSSP state machine
720  *
721  * @param ntlmssp_state NTLMSSP State, allocated by this function
722  */
723
724 NTSTATUS ntlmssp_server_start(TALLOC_CTX *mem_ctx,
725                               bool is_standalone,
726                               const char *netbios_name,
727                               const char *netbios_domain,
728                               const char *dns_name,
729                               const char *dns_domain,
730                               struct ntlmssp_state **_ntlmssp_state)
731 {
732         struct ntlmssp_state *ntlmssp_state;
733
734         if (!netbios_name) {
735                 netbios_name = "";
736         }
737
738         if (!netbios_domain) {
739                 netbios_domain = "";
740         }
741
742         if (!dns_domain) {
743                 dns_domain = "";
744         }
745
746         if (!dns_name) {
747                 dns_name = "";
748         }
749
750         ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
751         if (!ntlmssp_state) {
752                 return NT_STATUS_NO_MEMORY;
753         }
754
755         ntlmssp_state->role = NTLMSSP_SERVER;
756
757         ntlmssp_state->get_challenge = get_challenge;
758         ntlmssp_state->set_challenge = set_challenge;
759         ntlmssp_state->may_set_challenge = may_set_challenge;
760
761         ntlmssp_state->server.is_standalone = is_standalone;
762
763         ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
764
765         ntlmssp_state->allow_lm_key = lp_lanman_auth();
766
767         ntlmssp_state->neg_flags =
768                 NTLMSSP_NEGOTIATE_128 |
769                 NTLMSSP_NEGOTIATE_56 |
770                 NTLMSSP_NEGOTIATE_VERSION |
771                 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
772                 NTLMSSP_NEGOTIATE_NTLM |
773                 NTLMSSP_NEGOTIATE_NTLM2 |
774                 NTLMSSP_NEGOTIATE_KEY_EXCH |
775                 NTLMSSP_NEGOTIATE_SIGN |
776                 NTLMSSP_NEGOTIATE_SEAL;
777
778         ntlmssp_state->server.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
779         if (!ntlmssp_state->server.netbios_name) {
780                 talloc_free(ntlmssp_state);
781                 return NT_STATUS_NO_MEMORY;
782         }
783         ntlmssp_state->server.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
784         if (!ntlmssp_state->server.netbios_domain) {
785                 talloc_free(ntlmssp_state);
786                 return NT_STATUS_NO_MEMORY;
787         }
788         ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state, dns_name);
789         if (!ntlmssp_state->server.dns_name) {
790                 talloc_free(ntlmssp_state);
791                 return NT_STATUS_NO_MEMORY;
792         }
793         ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state, dns_domain);
794         if (!ntlmssp_state->server.dns_domain) {
795                 talloc_free(ntlmssp_state);
796                 return NT_STATUS_NO_MEMORY;
797         }
798
799         *_ntlmssp_state = ntlmssp_state;
800         return NT_STATUS_OK;
801 }
802
803 /*********************************************************************
804  Client side NTLMSSP
805 *********************************************************************/
806
807 /**
808  * Next state function for the Initial packet
809  *
810  * @param ntlmssp_state NTLMSSP State
811  * @param request The request, as a DATA_BLOB.  reply.data must be NULL
812  * @param request The reply, as an allocated DATA_BLOB, caller to free.
813  * @return Errors or NT_STATUS_OK.
814  */
815
816 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
817                                   DATA_BLOB reply, DATA_BLOB *next_request)
818 {
819         if (ntlmssp_state->unicode) {
820                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
821         } else {
822                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
823         }
824
825         if (ntlmssp_state->use_ntlmv2) {
826                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
827         }
828
829         /* generate the ntlmssp negotiate packet */
830         msrpc_gen(ntlmssp_state, next_request, "CddAA",
831                   "NTLMSSP",
832                   NTLMSSP_NEGOTIATE,
833                   ntlmssp_state->neg_flags,
834                   ntlmssp_state->client.netbios_domain,
835                   ntlmssp_state->client.netbios_name);
836
837         if (DEBUGLEVEL >= 10) {
838                 struct NEGOTIATE_MESSAGE *negotiate = talloc(
839                         talloc_tos(), struct NEGOTIATE_MESSAGE);
840                 if (negotiate != NULL) {
841                         NTSTATUS status;
842                         status = ntlmssp_pull_NEGOTIATE_MESSAGE(
843                                 next_request, negotiate, negotiate);
844                         if (NT_STATUS_IS_OK(status)) {
845                                 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
846                                                 negotiate);
847                         }
848                         TALLOC_FREE(negotiate);
849                 }
850         }
851
852         ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
853
854         return NT_STATUS_MORE_PROCESSING_REQUIRED;
855 }
856
857 /**
858  * Next state function for the Challenge Packet.  Generate an auth packet.
859  *
860  * @param ntlmssp_state NTLMSSP State
861  * @param request The request, as a DATA_BLOB.  reply.data must be NULL
862  * @param request The reply, as an allocated DATA_BLOB, caller to free.
863  * @return Errors or NT_STATUS_OK.
864  */
865
866 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
867                                          const DATA_BLOB reply, DATA_BLOB *next_request)
868 {
869         uint32_t chal_flags, ntlmssp_command, unkn1, unkn2;
870         DATA_BLOB server_domain_blob;
871         DATA_BLOB challenge_blob;
872         DATA_BLOB struct_blob = data_blob_null;
873         char *server_domain;
874         const char *chal_parse_string;
875         const char *auth_gen_string;
876         DATA_BLOB lm_response = data_blob_null;
877         DATA_BLOB nt_response = data_blob_null;
878         DATA_BLOB session_key = data_blob_null;
879         DATA_BLOB encrypted_session_key = data_blob_null;
880         NTSTATUS nt_status = NT_STATUS_OK;
881
882         if (ntlmssp_state->use_ccache) {
883                 struct wbcCredentialCacheParams params;
884                 struct wbcCredentialCacheInfo *info = NULL;
885                 struct wbcAuthErrorInfo *error = NULL;
886                 struct wbcNamedBlob auth_blob;
887                 struct wbcBlob *wbc_next = NULL;
888                 struct wbcBlob *wbc_session_key = NULL;
889                 wbcErr wbc_status;
890                 int i;
891
892                 params.account_name = ntlmssp_state->user;
893                 params.domain_name = ntlmssp_state->domain;
894                 params.level = WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP;
895
896                 auth_blob.name = "challenge_blob";
897                 auth_blob.flags = 0;
898                 auth_blob.blob.data = reply.data;
899                 auth_blob.blob.length = reply.length;
900                 params.num_blobs = 1;
901                 params.blobs = &auth_blob;
902
903                 wbc_status = wbcCredentialCache(&params, &info, &error);
904                 if (error != NULL) {
905                         wbcFreeMemory(error);
906                 }
907                 if (!WBC_ERROR_IS_OK(wbc_status)) {
908                         goto noccache;
909                 }
910
911                 for (i=0; i<info->num_blobs; i++) {
912                         if (strequal(info->blobs[i].name, "auth_blob")) {
913                                 wbc_next = &info->blobs[i].blob;
914                         }
915                         if (strequal(info->blobs[i].name, "session_key")) {
916                                 wbc_session_key = &info->blobs[i].blob;
917                         }
918                 }
919                 if ((wbc_next == NULL) || (wbc_session_key == NULL)) {
920                         wbcFreeMemory(info);
921                         goto noccache;
922                 }
923
924                 *next_request = data_blob(wbc_next->data, wbc_next->length);
925                 ntlmssp_state->session_key = data_blob(
926                         wbc_session_key->data, wbc_session_key->length);
927
928                 wbcFreeMemory(info);
929                 goto done;
930         }
931
932 noccache:
933
934         if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
935                          "NTLMSSP",
936                          &ntlmssp_command,
937                          &server_domain_blob,
938                          &chal_flags)) {
939                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
940                 dump_data(2, reply.data, reply.length);
941
942                 return NT_STATUS_INVALID_PARAMETER;
943         }
944
945         if (DEBUGLEVEL >= 10) {
946                 struct CHALLENGE_MESSAGE *challenge = talloc(
947                         talloc_tos(), struct CHALLENGE_MESSAGE);
948                 if (challenge != NULL) {
949                         NTSTATUS status;
950                         challenge->NegotiateFlags = chal_flags;
951                         status = ntlmssp_pull_CHALLENGE_MESSAGE(
952                                 &reply, challenge, challenge);
953                         if (NT_STATUS_IS_OK(status)) {
954                                 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
955                                                 challenge);
956                         }
957                         TALLOC_FREE(challenge);
958                 }
959         }
960
961         data_blob_free(&server_domain_blob);
962
963         DEBUG(3, ("Got challenge flags:\n"));
964         debug_ntlmssp_flags(chal_flags);
965
966         ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
967
968         if (ntlmssp_state->unicode) {
969                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
970                         chal_parse_string = "CdUdbddB";
971                 } else {
972                         chal_parse_string = "CdUdbdd";
973                 }
974                 auth_gen_string = "CdBBUUUBd";
975         } else {
976                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
977                         chal_parse_string = "CdAdbddB";
978                 } else {
979                         chal_parse_string = "CdAdbdd";
980                 }
981
982                 auth_gen_string = "CdBBAAABd";
983         }
984
985         DEBUG(3, ("NTLMSSP: Set final flags:\n"));
986         debug_ntlmssp_flags(ntlmssp_state->neg_flags);
987
988         if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
989                          "NTLMSSP",
990                          &ntlmssp_command,
991                          &server_domain,
992                          &chal_flags,
993                          &challenge_blob, 8,
994                          &unkn1, &unkn2,
995                          &struct_blob)) {
996                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
997                 dump_data(2, reply.data, reply.length);
998                 return NT_STATUS_INVALID_PARAMETER;
999         }
1000
1001         if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
1002                 ntlmssp_state->server.is_standalone = true;
1003         } else {
1004                 ntlmssp_state->server.is_standalone = false;
1005         }
1006         /* TODO: parse struct_blob and fill in the rest */
1007         ntlmssp_state->server.netbios_name = "";
1008         ntlmssp_state->server.netbios_domain = server_domain;
1009         ntlmssp_state->server.dns_name = "";
1010         ntlmssp_state->server.dns_domain = "";
1011
1012         if (challenge_blob.length != 8) {
1013                 data_blob_free(&struct_blob);
1014                 return NT_STATUS_INVALID_PARAMETER;
1015         }
1016
1017         if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
1018                 static const uint8_t zeros[16] = {0, };
1019                 /* do nothing - blobs are zero length */
1020
1021                 /* session key is all zeros */
1022                 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
1023
1024                 /* not doing NLTM2 without a password */
1025                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
1026         } else if (ntlmssp_state->use_ntlmv2) {
1027                 if (!struct_blob.length) {
1028                         /* be lazy, match win2k - we can't do NTLMv2 without it */
1029                         DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1030                         return NT_STATUS_INVALID_PARAMETER;
1031                 }
1032
1033                 /* TODO: if the remote server is standalone, then we should replace 'domain'
1034                    with the server name as supplied above */
1035
1036                 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
1037                                            ntlmssp_state->user,
1038                                            ntlmssp_state->domain,
1039                                            ntlmssp_state->nt_hash, &challenge_blob,
1040                                            &struct_blob,
1041                                            &lm_response, &nt_response, NULL,
1042                                            &session_key)) {
1043                         data_blob_free(&challenge_blob);
1044                         data_blob_free(&struct_blob);
1045                         return NT_STATUS_NO_MEMORY;
1046                 }
1047         } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1048                 struct MD5Context md5_session_nonce_ctx;
1049                 uint8_t session_nonce[16];
1050                 uint8_t session_nonce_hash[16];
1051                 uint8_t user_session_key[16];
1052
1053                 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1054                 generate_random_buffer(lm_response.data, 8);
1055                 memset(lm_response.data+8, 0, 16);
1056
1057                 memcpy(session_nonce, challenge_blob.data, 8);
1058                 memcpy(&session_nonce[8], lm_response.data, 8);
1059
1060                 MD5Init(&md5_session_nonce_ctx);
1061                 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1062                 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1063                 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1064
1065                 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1066                 DEBUG(5, ("challenge is: \n"));
1067                 dump_data(5, session_nonce_hash, 8);
1068
1069                 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1070                 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
1071                                   session_nonce_hash,
1072                                   nt_response.data);
1073
1074                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1075
1076                 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
1077                 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1078                 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1079         } else {
1080                 /* lanman auth is insecure, it may be disabled */
1081                 if (lp_client_lanman_auth()) {
1082                         lm_response = data_blob_talloc(ntlmssp_state,
1083                                                        NULL, 24);
1084                         SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
1085                                    lm_response.data);
1086                 }
1087
1088                 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1089                 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
1090                              nt_response.data);
1091
1092                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1093                 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1094                     && lp_client_lanman_auth()) {
1095                         SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
1096                                         session_key.data);
1097                         dump_data_pw("LM session key\n", session_key.data, session_key.length);
1098                 } else {
1099                         SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
1100                         dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1101                 }
1102         }
1103         data_blob_free(&struct_blob);
1104
1105         /* Key exchange encryptes a new client-generated session key with
1106            the password-derived key */
1107         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1108                 /* Make up a new session key */
1109                 uint8_t client_session_key[16];
1110                 generate_random_buffer(client_session_key, sizeof(client_session_key));
1111
1112                 /* Encrypt the new session key with the old one */
1113                 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1114                 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1115                 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
1116                 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1117
1118                 /* Mark the new session key as the 'real' session key */
1119                 data_blob_free(&session_key);
1120                 session_key = data_blob_talloc(ntlmssp_state,
1121                                                client_session_key,
1122                                                sizeof(client_session_key));
1123         }
1124
1125         /* this generates the actual auth packet */
1126         if (!msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
1127                        "NTLMSSP",
1128                        NTLMSSP_AUTH,
1129                        lm_response.data, lm_response.length,
1130                        nt_response.data, nt_response.length,
1131                        ntlmssp_state->domain,
1132                        ntlmssp_state->user,
1133                        ntlmssp_state->client.netbios_name,
1134                        encrypted_session_key.data, encrypted_session_key.length,
1135                        ntlmssp_state->neg_flags)) {
1136
1137                 return NT_STATUS_NO_MEMORY;
1138         }
1139
1140         if (DEBUGLEVEL >= 10) {
1141                 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
1142                         talloc_tos(), struct AUTHENTICATE_MESSAGE);
1143                 if (authenticate != NULL) {
1144                         NTSTATUS status;
1145                         authenticate->NegotiateFlags =
1146                                 ntlmssp_state->neg_flags;
1147                         status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
1148                                 next_request, authenticate, authenticate);
1149                         if (NT_STATUS_IS_OK(status)) {
1150                                 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
1151                                                 authenticate);
1152                         }
1153                         TALLOC_FREE(authenticate);
1154                 }
1155         }
1156
1157         data_blob_free(&encrypted_session_key);
1158
1159         data_blob_free(&ntlmssp_state->chal);
1160
1161         ntlmssp_state->session_key = session_key;
1162
1163         ntlmssp_state->chal = challenge_blob;
1164         ntlmssp_state->lm_resp = lm_response;
1165         ntlmssp_state->nt_resp = nt_response;
1166
1167 done:
1168
1169         ntlmssp_state->expected_state = NTLMSSP_DONE;
1170
1171         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1172                 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1173         }
1174
1175         return nt_status;
1176 }
1177
1178 NTSTATUS ntlmssp_client_start(TALLOC_CTX *mem_ctx,
1179                               const char *netbios_name,
1180                               const char *netbios_domain,
1181                               bool use_ntlmv2,
1182                               struct ntlmssp_state **_ntlmssp_state)
1183 {
1184         struct ntlmssp_state *ntlmssp_state;
1185
1186         if (!netbios_name) {
1187                 netbios_name = "";
1188         }
1189
1190         if (!netbios_domain) {
1191                 netbios_domain = "";
1192         }
1193
1194         ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
1195         if (!ntlmssp_state) {
1196                 return NT_STATUS_NO_MEMORY;
1197         }
1198
1199         ntlmssp_state->role = NTLMSSP_CLIENT;
1200
1201         ntlmssp_state->unicode = True;
1202
1203         ntlmssp_state->use_ntlmv2 = use_ntlmv2;
1204
1205         ntlmssp_state->expected_state = NTLMSSP_INITIAL;
1206
1207         ntlmssp_state->neg_flags =
1208                 NTLMSSP_NEGOTIATE_128 |
1209                 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
1210                 NTLMSSP_NEGOTIATE_NTLM |
1211                 NTLMSSP_NEGOTIATE_NTLM2 |
1212                 NTLMSSP_NEGOTIATE_KEY_EXCH |
1213                 NTLMSSP_REQUEST_TARGET;
1214
1215         ntlmssp_state->client.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
1216         if (!ntlmssp_state->client.netbios_name) {
1217                 talloc_free(ntlmssp_state);
1218                 return NT_STATUS_NO_MEMORY;
1219         }
1220         ntlmssp_state->client.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
1221         if (!ntlmssp_state->client.netbios_domain) {
1222                 talloc_free(ntlmssp_state);
1223                 return NT_STATUS_NO_MEMORY;
1224         }
1225
1226         *_ntlmssp_state = ntlmssp_state;
1227         return NT_STATUS_OK;
1228 }