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