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