906d743b919b17fdf10439b6ccf39caa256fc9dd
[nivanova/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                 return status;
334         }
335
336         /* Check if we may set the challenge */
337         if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
338                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
339         }
340
341         /* The flags we send back are not just the negotiated flags,
342          * they are also 'what is in this packet'.  Therfore, we
343          * operate on 'chal_flags' from here on
344          */
345
346         chal_flags = ntlmssp_state->neg_flags;
347
348         /* get the right name to fill in as 'target' */
349         target_name = ntlmssp_target_name(ntlmssp_state,
350                                           neg_flags, &chal_flags);
351         if (target_name == NULL)
352                 return NT_STATUS_INVALID_PARAMETER;
353
354         ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
355         ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
356                                                         cryptkey, 8);
357
358         /* This creates the 'blob' of names that appears at the end of the packet */
359         if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
360         {
361                 msrpc_gen(ntlmssp_state, &struct_blob, "aaaaa",
362                           MsvAvNbDomainName, target_name,
363                           MsvAvNbComputerName, ntlmssp_state->server.netbios_name,
364                           MsvAvDnsDomainName, ntlmssp_state->server.dns_domain,
365                           MsvAvDnsComputerName, ntlmssp_state->server.dns_name,
366                           MsvAvEOL, "");
367         } else {
368                 struct_blob = data_blob_null;
369         }
370
371         {
372                 /* Marshal the packet in the right format, be it unicode or ASCII */
373                 const char *gen_string;
374                 DATA_BLOB version_blob = data_blob_null;
375
376                 if (chal_flags & NTLMSSP_NEGOTIATE_VERSION) {
377                         enum ndr_err_code err;
378                         struct VERSION vers;
379
380                         /* "What Windows returns" as a version number. */
381                         ZERO_STRUCT(vers);
382                         vers.ProductMajorVersion = NTLMSSP_WINDOWS_MAJOR_VERSION_6;
383                         vers.ProductMinorVersion = NTLMSSP_WINDOWS_MINOR_VERSION_1;
384                         vers.ProductBuild = 0;
385                         vers.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
386
387                         err = ndr_push_struct_blob(&version_blob,
388                                                 ntlmssp_state,
389                                                 &vers,
390                                                 (ndr_push_flags_fn_t)ndr_push_VERSION);
391
392                         if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
393                                 return NT_STATUS_NO_MEMORY;
394                         }
395                 }
396
397                 if (ntlmssp_state->unicode) {
398                         gen_string = "CdUdbddBb";
399                 } else {
400                         gen_string = "CdAdbddBb";
401                 }
402
403                 msrpc_gen(ntlmssp_state, reply, gen_string,
404                         "NTLMSSP",
405                         NTLMSSP_CHALLENGE,
406                         target_name,
407                         chal_flags,
408                         cryptkey, 8,
409                         0, 0,
410                         struct_blob.data, struct_blob.length,
411                         version_blob.data, version_blob.length);
412
413                 data_blob_free(&version_blob);
414
415                 if (DEBUGLEVEL >= 10) {
416                         struct CHALLENGE_MESSAGE *challenge = talloc(
417                                 talloc_tos(), struct CHALLENGE_MESSAGE);
418                         if (challenge != NULL) {
419                                 challenge->NegotiateFlags = chal_flags;
420                                 status = ntlmssp_pull_CHALLENGE_MESSAGE(
421                                         reply, challenge, challenge);
422                                 if (NT_STATUS_IS_OK(status)) {
423                                         NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
424                                                         challenge);
425                                 }
426                                 TALLOC_FREE(challenge);
427                         }
428                 }
429         }
430
431         data_blob_free(&struct_blob);
432
433         ntlmssp_state->expected_state = NTLMSSP_AUTH;
434
435         return NT_STATUS_MORE_PROCESSING_REQUIRED;
436 }
437
438 /**
439  * Next state function for the Authenticate packet
440  *
441  * @param ntlmssp_state NTLMSSP State
442  * @param request The request, as a DATA_BLOB
443  * @param request The reply, as an allocated DATA_BLOB, caller to free.
444  * @return Errors or NT_STATUS_OK.
445  */
446
447 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
448                                     const DATA_BLOB request, DATA_BLOB *reply)
449 {
450         DATA_BLOB encrypted_session_key = data_blob_null;
451         DATA_BLOB user_session_key = data_blob_null;
452         DATA_BLOB lm_session_key = data_blob_null;
453         DATA_BLOB session_key = data_blob_null;
454         uint32_t ntlmssp_command, auth_flags;
455         NTSTATUS nt_status = NT_STATUS_OK;
456
457         /* used by NTLM2 */
458         bool doing_ntlm2 = False;
459
460         uint8_t session_nonce[16];
461         uint8_t session_nonce_hash[16];
462
463         const char *parse_string;
464
465         /* parse the NTLMSSP packet */
466         *reply = data_blob_null;
467
468 #if 0
469         file_save("ntlmssp_auth.dat", request.data, request.length);
470 #endif
471
472         if (ntlmssp_state->unicode) {
473                 parse_string = "CdBBUUUBd";
474         } else {
475                 parse_string = "CdBBAAABd";
476         }
477
478         data_blob_free(&ntlmssp_state->lm_resp);
479         data_blob_free(&ntlmssp_state->nt_resp);
480
481         ntlmssp_state->user = NULL;
482         ntlmssp_state->domain = NULL;
483
484         /* now the NTLMSSP encoded auth hashes */
485         if (!msrpc_parse(ntlmssp_state, &request, parse_string,
486                          "NTLMSSP",
487                          &ntlmssp_command,
488                          &ntlmssp_state->lm_resp,
489                          &ntlmssp_state->nt_resp,
490                          &ntlmssp_state->domain,
491                          &ntlmssp_state->user,
492                          &ntlmssp_state->client.netbios_name,
493                          &encrypted_session_key,
494                          &auth_flags)) {
495                 auth_flags = 0;
496
497                 /* Try again with a shorter string (Win9X truncates this packet) */
498                 if (ntlmssp_state->unicode) {
499                         parse_string = "CdBBUUU";
500                 } else {
501                         parse_string = "CdBBAAA";
502                 }
503
504                 /* now the NTLMSSP encoded auth hashes */
505                 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
506                                  "NTLMSSP",
507                                  &ntlmssp_command,
508                                  &ntlmssp_state->lm_resp,
509                                  &ntlmssp_state->nt_resp,
510                                  &ntlmssp_state->domain,
511                                  &ntlmssp_state->user,
512                                  &ntlmssp_state->client.netbios_name)) {
513                         DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
514                         dump_data(2, request.data, request.length);
515
516                         return NT_STATUS_INVALID_PARAMETER;
517                 }
518         }
519
520         if (auth_flags)
521                 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, ntlmssp_state->allow_lm_key);
522
523         if (DEBUGLEVEL >= 10) {
524                 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
525                         talloc_tos(), struct AUTHENTICATE_MESSAGE);
526                 if (authenticate != NULL) {
527                         NTSTATUS status;
528                         authenticate->NegotiateFlags = auth_flags;
529                         status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
530                                 &request, authenticate, authenticate);
531                         if (NT_STATUS_IS_OK(status)) {
532                                 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
533                                                 authenticate);
534                         }
535                         TALLOC_FREE(authenticate);
536                 }
537         }
538
539         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
540                  ntlmssp_state->user, ntlmssp_state->domain,
541                  ntlmssp_state->client.netbios_name,
542                  (unsigned long)ntlmssp_state->lm_resp.length,
543                  (unsigned long)ntlmssp_state->nt_resp.length));
544
545 #if 0
546         file_save("nthash1.dat",  &ntlmssp_state->nt_resp.data,  &ntlmssp_state->nt_resp.length);
547         file_save("lmhash1.dat",  &ntlmssp_state->lm_resp.data,  &ntlmssp_state->lm_resp.length);
548 #endif
549
550         /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
551            client challenge
552
553            However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
554         */
555         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
556                 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
557                         struct MD5Context md5_session_nonce_ctx;
558                         SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
559
560                         doing_ntlm2 = True;
561
562                         memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
563                         memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
564
565                         MD5Init(&md5_session_nonce_ctx);
566                         MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
567                         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
568
569                         ntlmssp_state->chal = data_blob_talloc(
570                                 ntlmssp_state, session_nonce_hash, 8);
571
572                         /* LM response is no longer useful */
573                         data_blob_free(&ntlmssp_state->lm_resp);
574
575                         /* We changed the effective challenge - set it */
576                         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
577                                 data_blob_free(&encrypted_session_key);
578                                 return nt_status;
579                         }
580
581                         /* LM Key is incompatible. */
582                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
583                 }
584         }
585
586         /*
587          * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
588          * is required (by "ntlm auth = no" and "lm auth = no" being set in the
589          * smb.conf file) and no NTLMv2 response was sent then the password check
590          * will fail here. JRA.
591          */
592
593         /* Finally, actually ask if the password is OK */
594
595         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
596                                                                        &user_session_key, &lm_session_key))) {
597                 data_blob_free(&encrypted_session_key);
598                 return nt_status;
599         }
600
601         dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
602         dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
603
604         /* Handle the different session key derivation for NTLM2 */
605         if (doing_ntlm2) {
606                 if (user_session_key.data && user_session_key.length == 16) {
607                         session_key = data_blob_talloc(ntlmssp_state,
608                                                        NULL, 16);
609                         hmac_md5(user_session_key.data, session_nonce,
610                                  sizeof(session_nonce), session_key.data);
611                         DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
612                         dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
613
614                 } else {
615                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
616                         session_key = data_blob_null;
617                 }
618         } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
619                 if (lm_session_key.data && lm_session_key.length >= 8) {
620                         if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
621                                 session_key = data_blob_talloc(ntlmssp_state,
622                                                                NULL, 16);
623                                 if (session_key.data == NULL) {
624                                         return NT_STATUS_NO_MEMORY;
625                                 }
626                                 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
627                                                           session_key.data);
628                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
629                         } else {
630                                 static const uint8_t zeros[24] = {0, };
631                                 session_key = data_blob_talloc(
632                                         ntlmssp_state, NULL, 16);
633                                 if (session_key.data == NULL) {
634                                         return NT_STATUS_NO_MEMORY;
635                                 }
636                                 SMBsesskeygen_lm_sess_key(
637                                         lm_session_key.data, zeros,
638                                         session_key.data);
639                         }
640                         dump_data_pw("LM session key:\n", session_key.data,
641                                      session_key.length);
642                 } else {
643                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
644                         session_key = data_blob_null;
645                 }
646         } else if (user_session_key.data) {
647                 session_key = user_session_key;
648                 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
649                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
650         } else if (lm_session_key.data) {
651                 session_key = lm_session_key;
652                 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
653                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
654         } else {
655                 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
656                 session_key = data_blob_null;
657         }
658
659         /* With KEY_EXCH, the client supplies the proposed session key,
660            but encrypts it with the long-term key */
661         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
662                 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
663                         data_blob_free(&encrypted_session_key);
664                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
665                                   (unsigned int)encrypted_session_key.length));
666                         return NT_STATUS_INVALID_PARAMETER;
667                 } else if (!session_key.data || session_key.length != 16) {
668                         DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
669                                   (unsigned int)session_key.length));
670                         ntlmssp_state->session_key = session_key;
671                 } else {
672                         dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
673                         arcfour_crypt_blob(encrypted_session_key.data,
674                                            encrypted_session_key.length,
675                                            &session_key);
676                         ntlmssp_state->session_key = data_blob_talloc(
677                                 ntlmssp_state, encrypted_session_key.data,
678                                 encrypted_session_key.length);
679                         dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
680                                      encrypted_session_key.length);
681                 }
682         } else {
683                 ntlmssp_state->session_key = session_key;
684         }
685
686         if (!NT_STATUS_IS_OK(nt_status)) {
687                 ntlmssp_state->session_key = data_blob_null;
688         } else if (ntlmssp_state->session_key.length) {
689                 nt_status = ntlmssp_sign_init(ntlmssp_state);
690         }
691
692         data_blob_free(&encrypted_session_key);
693
694         /* Only one authentication allowed per server state. */
695         ntlmssp_state->expected_state = NTLMSSP_DONE;
696
697         return nt_status;
698 }
699
700 /**
701  * Create an NTLMSSP state machine
702  *
703  * @param ntlmssp_state NTLMSSP State, allocated by this function
704  */
705
706 NTSTATUS ntlmssp_server_start(TALLOC_CTX *mem_ctx,
707                               bool is_standalone,
708                               const char *netbios_name,
709                               const char *netbios_domain,
710                               const char *dns_name,
711                               const char *dns_domain,
712                               struct ntlmssp_state **_ntlmssp_state)
713 {
714         struct ntlmssp_state *ntlmssp_state;
715
716         if (!netbios_name) {
717                 netbios_name = "";
718         }
719
720         if (!netbios_domain) {
721                 netbios_domain = "";
722         }
723
724         if (!dns_domain) {
725                 dns_domain = "";
726         }
727
728         if (!dns_name) {
729                 dns_name = "";
730         }
731
732         ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
733         if (!ntlmssp_state) {
734                 return NT_STATUS_NO_MEMORY;
735         }
736
737         ntlmssp_state->role = NTLMSSP_SERVER;
738
739         ntlmssp_state->get_challenge = get_challenge;
740         ntlmssp_state->set_challenge = set_challenge;
741         ntlmssp_state->may_set_challenge = may_set_challenge;
742
743         ntlmssp_state->server.is_standalone = is_standalone;
744
745         ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
746
747         ntlmssp_state->allow_lm_key = lp_lanman_auth();
748
749         ntlmssp_state->neg_flags =
750                 NTLMSSP_NEGOTIATE_128 |
751                 NTLMSSP_NEGOTIATE_56 |
752                 NTLMSSP_NEGOTIATE_VERSION |
753                 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
754                 NTLMSSP_NEGOTIATE_NTLM |
755                 NTLMSSP_NEGOTIATE_NTLM2 |
756                 NTLMSSP_NEGOTIATE_KEY_EXCH |
757                 NTLMSSP_NEGOTIATE_SIGN |
758                 NTLMSSP_NEGOTIATE_SEAL;
759
760         ntlmssp_state->server.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
761         if (!ntlmssp_state->server.netbios_name) {
762                 talloc_free(ntlmssp_state);
763                 return NT_STATUS_NO_MEMORY;
764         }
765         ntlmssp_state->server.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
766         if (!ntlmssp_state->server.netbios_domain) {
767                 talloc_free(ntlmssp_state);
768                 return NT_STATUS_NO_MEMORY;
769         }
770         ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state, dns_name);
771         if (!ntlmssp_state->server.dns_name) {
772                 talloc_free(ntlmssp_state);
773                 return NT_STATUS_NO_MEMORY;
774         }
775         ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state, dns_domain);
776         if (!ntlmssp_state->server.dns_domain) {
777                 talloc_free(ntlmssp_state);
778                 return NT_STATUS_NO_MEMORY;
779         }
780
781         *_ntlmssp_state = ntlmssp_state;
782         return NT_STATUS_OK;
783 }
784
785 /*********************************************************************
786  Client side NTLMSSP
787 *********************************************************************/
788
789 /**
790  * Next state function for the Initial packet
791  *
792  * @param ntlmssp_state NTLMSSP State
793  * @param request The request, as a DATA_BLOB.  reply.data must be NULL
794  * @param request The reply, as an allocated DATA_BLOB, caller to free.
795  * @return Errors or NT_STATUS_OK.
796  */
797
798 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
799                                   DATA_BLOB reply, DATA_BLOB *next_request)
800 {
801         if (ntlmssp_state->unicode) {
802                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
803         } else {
804                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
805         }
806
807         if (ntlmssp_state->use_ntlmv2) {
808                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
809         }
810
811         /* generate the ntlmssp negotiate packet */
812         msrpc_gen(ntlmssp_state, next_request, "CddAA",
813                   "NTLMSSP",
814                   NTLMSSP_NEGOTIATE,
815                   ntlmssp_state->neg_flags,
816                   ntlmssp_state->client.netbios_domain,
817                   ntlmssp_state->client.netbios_name);
818
819         if (DEBUGLEVEL >= 10) {
820                 struct NEGOTIATE_MESSAGE *negotiate = talloc(
821                         talloc_tos(), struct NEGOTIATE_MESSAGE);
822                 if (negotiate != NULL) {
823                         NTSTATUS status;
824                         status = ntlmssp_pull_NEGOTIATE_MESSAGE(
825                                 next_request, negotiate, negotiate);
826                         if (NT_STATUS_IS_OK(status)) {
827                                 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
828                                                 negotiate);
829                         }
830                         TALLOC_FREE(negotiate);
831                 }
832         }
833
834         ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
835
836         return NT_STATUS_MORE_PROCESSING_REQUIRED;
837 }
838
839 /**
840  * Next state function for the Challenge Packet.  Generate an auth packet.
841  *
842  * @param ntlmssp_state NTLMSSP State
843  * @param request The request, as a DATA_BLOB.  reply.data must be NULL
844  * @param request The reply, as an allocated DATA_BLOB, caller to free.
845  * @return Errors or NT_STATUS_OK.
846  */
847
848 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
849                                          const DATA_BLOB reply, DATA_BLOB *next_request)
850 {
851         uint32_t chal_flags, ntlmssp_command, unkn1, unkn2;
852         DATA_BLOB server_domain_blob;
853         DATA_BLOB challenge_blob;
854         DATA_BLOB struct_blob = data_blob_null;
855         char *server_domain;
856         const char *chal_parse_string;
857         const char *auth_gen_string;
858         DATA_BLOB lm_response = data_blob_null;
859         DATA_BLOB nt_response = data_blob_null;
860         DATA_BLOB session_key = data_blob_null;
861         DATA_BLOB encrypted_session_key = data_blob_null;
862         NTSTATUS nt_status = NT_STATUS_OK;
863
864         if (ntlmssp_state->use_ccache) {
865                 struct wbcCredentialCacheParams params;
866                 struct wbcCredentialCacheInfo *info = NULL;
867                 struct wbcAuthErrorInfo *error = NULL;
868                 struct wbcNamedBlob auth_blob;
869                 struct wbcBlob *wbc_next = NULL;
870                 struct wbcBlob *wbc_session_key = NULL;
871                 wbcErr wbc_status;
872                 int i;
873
874                 params.account_name = ntlmssp_state->user;
875                 params.domain_name = ntlmssp_state->domain;
876                 params.level = WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP;
877
878                 auth_blob.name = "challenge_blob";
879                 auth_blob.flags = 0;
880                 auth_blob.blob.data = reply.data;
881                 auth_blob.blob.length = reply.length;
882                 params.num_blobs = 1;
883                 params.blobs = &auth_blob;
884
885                 wbc_status = wbcCredentialCache(&params, &info, &error);
886                 if (error != NULL) {
887                         wbcFreeMemory(error);
888                 }
889                 if (!WBC_ERROR_IS_OK(wbc_status)) {
890                         goto noccache;
891                 }
892
893                 for (i=0; i<info->num_blobs; i++) {
894                         if (strequal(info->blobs[i].name, "auth_blob")) {
895                                 wbc_next = &info->blobs[i].blob;
896                         }
897                         if (strequal(info->blobs[i].name, "session_key")) {
898                                 wbc_session_key = &info->blobs[i].blob;
899                         }
900                 }
901                 if ((wbc_next == NULL) || (wbc_session_key == NULL)) {
902                         wbcFreeMemory(info);
903                         goto noccache;
904                 }
905
906                 *next_request = data_blob(wbc_next->data, wbc_next->length);
907                 ntlmssp_state->session_key = data_blob(
908                         wbc_session_key->data, wbc_session_key->length);
909
910                 wbcFreeMemory(info);
911                 goto done;
912         }
913
914 noccache:
915
916         if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
917                          "NTLMSSP",
918                          &ntlmssp_command,
919                          &server_domain_blob,
920                          &chal_flags)) {
921                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
922                 dump_data(2, reply.data, reply.length);
923
924                 return NT_STATUS_INVALID_PARAMETER;
925         }
926
927         if (DEBUGLEVEL >= 10) {
928                 struct CHALLENGE_MESSAGE *challenge = talloc(
929                         talloc_tos(), struct CHALLENGE_MESSAGE);
930                 if (challenge != NULL) {
931                         NTSTATUS status;
932                         challenge->NegotiateFlags = chal_flags;
933                         status = ntlmssp_pull_CHALLENGE_MESSAGE(
934                                 &reply, challenge, challenge);
935                         if (NT_STATUS_IS_OK(status)) {
936                                 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
937                                                 challenge);
938                         }
939                         TALLOC_FREE(challenge);
940                 }
941         }
942
943         data_blob_free(&server_domain_blob);
944
945         DEBUG(3, ("Got challenge flags:\n"));
946         debug_ntlmssp_flags(chal_flags);
947
948         ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
949
950         if (ntlmssp_state->unicode) {
951                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
952                         chal_parse_string = "CdUdbddB";
953                 } else {
954                         chal_parse_string = "CdUdbdd";
955                 }
956                 auth_gen_string = "CdBBUUUBd";
957         } else {
958                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
959                         chal_parse_string = "CdAdbddB";
960                 } else {
961                         chal_parse_string = "CdAdbdd";
962                 }
963
964                 auth_gen_string = "CdBBAAABd";
965         }
966
967         DEBUG(3, ("NTLMSSP: Set final flags:\n"));
968         debug_ntlmssp_flags(ntlmssp_state->neg_flags);
969
970         if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
971                          "NTLMSSP",
972                          &ntlmssp_command,
973                          &server_domain,
974                          &chal_flags,
975                          &challenge_blob, 8,
976                          &unkn1, &unkn2,
977                          &struct_blob)) {
978                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
979                 dump_data(2, reply.data, reply.length);
980                 return NT_STATUS_INVALID_PARAMETER;
981         }
982
983         if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
984                 ntlmssp_state->server.is_standalone = true;
985         } else {
986                 ntlmssp_state->server.is_standalone = false;
987         }
988         /* TODO: parse struct_blob and fill in the rest */
989         ntlmssp_state->server.netbios_name = "";
990         ntlmssp_state->server.netbios_domain = server_domain;
991         ntlmssp_state->server.dns_name = "";
992         ntlmssp_state->server.dns_domain = "";
993
994         if (challenge_blob.length != 8) {
995                 data_blob_free(&struct_blob);
996                 return NT_STATUS_INVALID_PARAMETER;
997         }
998
999         if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
1000                 static const uint8_t zeros[16] = {0, };
1001                 /* do nothing - blobs are zero length */
1002
1003                 /* session key is all zeros */
1004                 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
1005
1006                 /* not doing NLTM2 without a password */
1007                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
1008         } else if (ntlmssp_state->use_ntlmv2) {
1009                 if (!struct_blob.length) {
1010                         /* be lazy, match win2k - we can't do NTLMv2 without it */
1011                         DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1012                         return NT_STATUS_INVALID_PARAMETER;
1013                 }
1014
1015                 /* TODO: if the remote server is standalone, then we should replace 'domain'
1016                    with the server name as supplied above */
1017
1018                 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
1019                                            ntlmssp_state->user,
1020                                            ntlmssp_state->domain,
1021                                            ntlmssp_state->nt_hash, &challenge_blob,
1022                                            &struct_blob,
1023                                            &lm_response, &nt_response, NULL,
1024                                            &session_key)) {
1025                         data_blob_free(&challenge_blob);
1026                         data_blob_free(&struct_blob);
1027                         return NT_STATUS_NO_MEMORY;
1028                 }
1029         } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1030                 struct MD5Context md5_session_nonce_ctx;
1031                 uint8_t session_nonce[16];
1032                 uint8_t session_nonce_hash[16];
1033                 uint8_t user_session_key[16];
1034
1035                 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1036                 generate_random_buffer(lm_response.data, 8);
1037                 memset(lm_response.data+8, 0, 16);
1038
1039                 memcpy(session_nonce, challenge_blob.data, 8);
1040                 memcpy(&session_nonce[8], lm_response.data, 8);
1041
1042                 MD5Init(&md5_session_nonce_ctx);
1043                 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1044                 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1045                 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1046
1047                 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1048                 DEBUG(5, ("challenge is: \n"));
1049                 dump_data(5, session_nonce_hash, 8);
1050
1051                 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1052                 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
1053                                   session_nonce_hash,
1054                                   nt_response.data);
1055
1056                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1057
1058                 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
1059                 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1060                 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1061         } else {
1062                 /* lanman auth is insecure, it may be disabled */
1063                 if (lp_client_lanman_auth()) {
1064                         lm_response = data_blob_talloc(ntlmssp_state,
1065                                                        NULL, 24);
1066                         SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
1067                                    lm_response.data);
1068                 }
1069
1070                 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1071                 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
1072                              nt_response.data);
1073
1074                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1075                 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1076                     && lp_client_lanman_auth()) {
1077                         SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
1078                                         session_key.data);
1079                         dump_data_pw("LM session key\n", session_key.data, session_key.length);
1080                 } else {
1081                         SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
1082                         dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1083                 }
1084         }
1085         data_blob_free(&struct_blob);
1086
1087         /* Key exchange encryptes a new client-generated session key with
1088            the password-derived key */
1089         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1090                 /* Make up a new session key */
1091                 uint8_t client_session_key[16];
1092                 generate_random_buffer(client_session_key, sizeof(client_session_key));
1093
1094                 /* Encrypt the new session key with the old one */
1095                 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1096                 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1097                 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
1098                 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1099
1100                 /* Mark the new session key as the 'real' session key */
1101                 data_blob_free(&session_key);
1102                 session_key = data_blob_talloc(ntlmssp_state,
1103                                                client_session_key,
1104                                                sizeof(client_session_key));
1105         }
1106
1107         /* this generates the actual auth packet */
1108         if (!msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
1109                        "NTLMSSP",
1110                        NTLMSSP_AUTH,
1111                        lm_response.data, lm_response.length,
1112                        nt_response.data, nt_response.length,
1113                        ntlmssp_state->domain,
1114                        ntlmssp_state->user,
1115                        ntlmssp_state->client.netbios_name,
1116                        encrypted_session_key.data, encrypted_session_key.length,
1117                        ntlmssp_state->neg_flags)) {
1118
1119                 return NT_STATUS_NO_MEMORY;
1120         }
1121
1122         if (DEBUGLEVEL >= 10) {
1123                 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
1124                         talloc_tos(), struct AUTHENTICATE_MESSAGE);
1125                 if (authenticate != NULL) {
1126                         NTSTATUS status;
1127                         authenticate->NegotiateFlags =
1128                                 ntlmssp_state->neg_flags;
1129                         status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
1130                                 next_request, authenticate, authenticate);
1131                         if (NT_STATUS_IS_OK(status)) {
1132                                 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
1133                                                 authenticate);
1134                         }
1135                         TALLOC_FREE(authenticate);
1136                 }
1137         }
1138
1139         data_blob_free(&encrypted_session_key);
1140
1141         data_blob_free(&ntlmssp_state->chal);
1142
1143         ntlmssp_state->session_key = session_key;
1144
1145         ntlmssp_state->chal = challenge_blob;
1146         ntlmssp_state->lm_resp = lm_response;
1147         ntlmssp_state->nt_resp = nt_response;
1148
1149 done:
1150
1151         ntlmssp_state->expected_state = NTLMSSP_DONE;
1152
1153         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1154                 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1155         }
1156
1157         return nt_status;
1158 }
1159
1160 NTSTATUS ntlmssp_client_start(TALLOC_CTX *mem_ctx,
1161                               const char *netbios_name,
1162                               const char *netbios_domain,
1163                               bool use_ntlmv2,
1164                               struct ntlmssp_state **_ntlmssp_state)
1165 {
1166         struct ntlmssp_state *ntlmssp_state;
1167
1168         if (!netbios_name) {
1169                 netbios_name = "";
1170         }
1171
1172         if (!netbios_domain) {
1173                 netbios_domain = "";
1174         }
1175
1176         ntlmssp_state = talloc_zero(mem_ctx, struct ntlmssp_state);
1177         if (!ntlmssp_state) {
1178                 return NT_STATUS_NO_MEMORY;
1179         }
1180
1181         ntlmssp_state->role = NTLMSSP_CLIENT;
1182
1183         ntlmssp_state->unicode = True;
1184
1185         ntlmssp_state->use_ntlmv2 = use_ntlmv2;
1186
1187         ntlmssp_state->expected_state = NTLMSSP_INITIAL;
1188
1189         ntlmssp_state->neg_flags =
1190                 NTLMSSP_NEGOTIATE_128 |
1191                 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
1192                 NTLMSSP_NEGOTIATE_NTLM |
1193                 NTLMSSP_NEGOTIATE_NTLM2 |
1194                 NTLMSSP_NEGOTIATE_KEY_EXCH |
1195                 NTLMSSP_REQUEST_TARGET;
1196
1197         ntlmssp_state->client.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
1198         if (!ntlmssp_state->client.netbios_name) {
1199                 talloc_free(ntlmssp_state);
1200                 return NT_STATUS_NO_MEMORY;
1201         }
1202         ntlmssp_state->client.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
1203         if (!ntlmssp_state->client.netbios_domain) {
1204                 talloc_free(ntlmssp_state);
1205                 return NT_STATUS_NO_MEMORY;
1206         }
1207
1208         *_ntlmssp_state = ntlmssp_state;
1209         return NT_STATUS_OK;
1210 }