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