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