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