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