c51b599b0411c40c1abc43665bf064d66d919fa6
[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
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 /**
27  * Print out the NTLMSSP flags for debugging 
28  * @param neg_flags The flags from the packet
29  */
30
31 void debug_ntlmssp_flags(uint32 neg_flags)
32 {
33         DEBUG(3,("Got NTLMSSP neg_flags=0x%08x\n", neg_flags));
34         
35         if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) 
36                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_UNICODE\n"));
37         if (neg_flags & NTLMSSP_NEGOTIATE_OEM) 
38                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_OEM\n"));
39         if (neg_flags & NTLMSSP_REQUEST_TARGET) 
40                 DEBUGADD(4, ("  NTLMSSP_REQUEST_TARGET\n"));
41         if (neg_flags & NTLMSSP_NEGOTIATE_SIGN) 
42                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_SIGN\n"));
43         if (neg_flags & NTLMSSP_NEGOTIATE_SEAL) 
44                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_SEAL\n"));
45         if (neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
46                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_LM_KEY\n"));
47         if (neg_flags & NTLMSSP_NEGOTIATE_NETWARE) 
48                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_NETWARE\n"));
49         if (neg_flags & NTLMSSP_NEGOTIATE_NTLM) 
50                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_NTLM\n"));
51         if (neg_flags & NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED) 
52                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED\n"));
53         if (neg_flags & NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED) 
54                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED\n"));
55         if (neg_flags & NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL) 
56                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL\n"));
57         if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN) 
58                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_ALWAYS_SIGN\n"));
59         if (neg_flags & NTLMSSP_NEGOTIATE_NTLM2) 
60                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_NTLM2\n"));
61         if (neg_flags & NTLMSSP_CHAL_TARGET_INFO) 
62                 DEBUGADD(4, ("  NTLMSSP_CHAL_TARGET_INFO\n"));
63         if (neg_flags & NTLMSSP_NEGOTIATE_128) 
64                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_128\n"));
65         if (neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) 
66                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_KEY_EXCH\n"));
67 }
68
69 /**
70  * Default challenge generation code.
71  *
72  */
73    
74 static const uint8 *get_challenge(struct ntlmssp_state *ntlmssp_state)
75 {
76         static uchar chal[8];
77         generate_random_buffer(chal, sizeof(chal), False);
78
79         return chal;
80 }
81
82 /**
83  * Determine correct target name flags for reply, given server role 
84  * and negotiated flags
85  * 
86  * @param ntlmssp_state NTLMSSP State
87  * @param neg_flags The flags from the packet
88  * @param chal_flags The flags to be set in the reply packet
89  * @return The 'target name' string.
90  */
91
92 static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
93                                        uint32 neg_flags, uint32 *chal_flags) 
94 {
95         if (neg_flags & NTLMSSP_REQUEST_TARGET) {
96                 *chal_flags |= NTLMSSP_CHAL_TARGET_INFO;
97                 *chal_flags |= NTLMSSP_REQUEST_TARGET;
98                 if (ntlmssp_state->server_role == ROLE_STANDALONE) {
99                         *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
100                         return ntlmssp_state->get_global_myname();
101                 } else {
102                         *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
103                         return ntlmssp_state->get_domain();
104                 };
105         } else {
106                 return "";
107         }
108 }
109
110 /**
111  * Next state function for the Negotiate packet
112  * 
113  * @param ntlmssp_state NTLMSSP State
114  * @param request The request, as a DATA_BLOB
115  * @param request The reply, as an allocated DATA_BLOB, caller to free.
116  * @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent. 
117  */
118
119 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
120                                          const DATA_BLOB request, DATA_BLOB *reply) 
121 {
122         DATA_BLOB struct_blob;
123         fstring dnsname, dnsdomname;
124         uint32 neg_flags = 0;
125         uint32 ntlmssp_command, chal_flags;
126         char *cliname=NULL, *domname=NULL;
127         const uint8 *cryptkey;
128         const char *target_name;
129
130         /* parse the NTLMSSP packet */
131 #if 0
132         file_save("ntlmssp_negotiate.dat", request.data, request.length);
133 #endif
134
135         if (request.length) {
136                 if (!msrpc_parse(&request, "CddAA",
137                                  "NTLMSSP",
138                                  &ntlmssp_command,
139                                  &neg_flags,
140                                  &cliname,
141                                  &domname)) {
142                         DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP:\n"));
143                         dump_data(2, (const char *)request.data, request.length);
144                         return NT_STATUS_INVALID_PARAMETER;
145                 }
146                 
147                 SAFE_FREE(cliname);
148                 SAFE_FREE(domname);
149                 
150                 debug_ntlmssp_flags(neg_flags);
151         }
152         
153         cryptkey = ntlmssp_state->get_challenge(ntlmssp_state);
154
155         data_blob_free(&ntlmssp_state->chal);
156         ntlmssp_state->chal = data_blob(cryptkey, 8);
157
158         /* Give them the challenge. For now, ignore neg_flags and just
159            return the flags we want. Obviously this is not correct */
160         
161         chal_flags = 
162                 NTLMSSP_NEGOTIATE_128 | 
163                 NTLMSSP_NEGOTIATE_NTLM;
164         
165         if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
166                 chal_flags |= NTLMSSP_NEGOTIATE_UNICODE;
167                 ntlmssp_state->unicode = True;
168         } else {
169                 chal_flags |= NTLMSSP_NEGOTIATE_OEM;
170         }
171
172         target_name = ntlmssp_target_name(ntlmssp_state, 
173                                           neg_flags, &chal_flags); 
174
175         if (target_name == NULL)
176                 return NT_STATUS_INVALID_PARAMETER;
177
178         /* This should be a 'netbios domain -> DNS domain' mapping */
179         dnsdomname[0] = '\0';
180         get_mydomname(dnsdomname);
181         strlower_m(dnsdomname);
182         
183         dnsname[0] = '\0';
184         get_myfullname(dnsname);
185         strlower_m(dnsname);
186         
187         if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) 
188         {
189                 const char *target_name_dns = "";
190                 if (chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN) {
191                         target_name_dns = dnsdomname;
192                 } else if (chal_flags |= NTLMSSP_TARGET_TYPE_SERVER) {
193                         target_name_dns = dnsname;
194                 }
195
196                 msrpc_gen(&struct_blob, "aaaaa",
197                           ntlmssp_state->unicode, NTLMSSP_NAME_TYPE_DOMAIN, target_name,
198                           ntlmssp_state->unicode, NTLMSSP_NAME_TYPE_SERVER, ntlmssp_state->get_global_myname(),
199                           ntlmssp_state->unicode, NTLMSSP_NAME_TYPE_DOMAIN_DNS, target_name_dns,
200                           ntlmssp_state->unicode, NTLMSSP_NAME_TYPE_SERVER_DNS, dnsdomname,
201                           ntlmssp_state->unicode, 0, "");
202         } else {
203                 struct_blob = data_blob(NULL, 0);
204         }
205
206         {
207                 const char *gen_string;
208                 if (ntlmssp_state->unicode) {
209                         gen_string = "CdUdbddB";
210                 } else {
211                         gen_string = "CdAdbddB";
212                 }
213                 
214                 msrpc_gen(reply, gen_string,
215                           "NTLMSSP", 
216                           NTLMSSP_CHALLENGE,
217                           target_name,
218                           chal_flags,
219                           cryptkey, 8,
220                           0, 0,
221                           struct_blob.data, struct_blob.length);
222         }
223                 
224         data_blob_free(&struct_blob);
225
226         ntlmssp_state->expected_state = NTLMSSP_AUTH;
227
228         return NT_STATUS_MORE_PROCESSING_REQUIRED;
229 }
230
231 /**
232  * Next state function for the Authenticate packet
233  * 
234  * @param ntlmssp_state NTLMSSP State
235  * @param request The request, as a DATA_BLOB
236  * @param request The reply, as an allocated DATA_BLOB, caller to free.
237  * @return Errors or NT_STATUS_OK. 
238  */
239
240 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
241                                     const DATA_BLOB request, DATA_BLOB *reply) 
242 {
243         DATA_BLOB sess_key;
244         uint32 ntlmssp_command, neg_flags;
245         NTSTATUS nt_status;
246
247         const char *parse_string;
248
249         /* parse the NTLMSSP packet */
250 #if 0
251         file_save("ntlmssp_auth.dat", request.data, request.length);
252 #endif
253
254         if (ntlmssp_state->unicode) {
255                 parse_string = "CdBBUUUBd";
256         } else {
257                 parse_string = "CdBBAAABd";
258         }
259
260         data_blob_free(&ntlmssp_state->lm_resp);
261         data_blob_free(&ntlmssp_state->nt_resp);
262
263         SAFE_FREE(ntlmssp_state->user);
264         SAFE_FREE(ntlmssp_state->domain);
265         SAFE_FREE(ntlmssp_state->workstation);
266
267         /* now the NTLMSSP encoded auth hashes */
268         if (!msrpc_parse(&request, parse_string,
269                          "NTLMSSP", 
270                          &ntlmssp_command, 
271                          &ntlmssp_state->lm_resp,
272                          &ntlmssp_state->nt_resp,
273                          &ntlmssp_state->domain, 
274                          &ntlmssp_state->user, 
275                          &ntlmssp_state->workstation,
276                          &sess_key,
277                          &neg_flags)) {
278                 DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP:\n"));
279                 dump_data(2, (const char *)request.data, request.length);
280                 return NT_STATUS_INVALID_PARAMETER;
281         }
282
283         data_blob_free(&sess_key);
284         
285         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
286                  ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
287
288 #if 0
289         file_save("nthash1.dat",  &ntlmssp_state->nt_resp.data,  &ntlmssp_state->nt_resp.length);
290         file_save("lmhash1.dat",  &ntlmssp_state->lm_resp.data,  &ntlmssp_state->lm_resp.length);
291 #endif
292
293         nt_status = ntlmssp_state->check_password(ntlmssp_state);
294         
295         *reply = data_blob(NULL, 0);
296
297         return nt_status;
298 }
299
300 /**
301  * Create an NTLMSSP state machine
302  * 
303  * @param ntlmssp_state NTLMSSP State, allocated by this function
304  */
305
306 NTSTATUS ntlmssp_server_start(NTLMSSP_STATE **ntlmssp_state)
307 {
308         TALLOC_CTX *mem_ctx;
309
310         mem_ctx = talloc_init("NTLMSSP context");
311         
312         *ntlmssp_state = talloc_zero(mem_ctx, sizeof(**ntlmssp_state));
313         if (!*ntlmssp_state) {
314                 DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
315                 talloc_destroy(mem_ctx);
316                 return NT_STATUS_NO_MEMORY;
317         }
318
319         (*ntlmssp_state)->mem_ctx = mem_ctx;
320         (*ntlmssp_state)->get_challenge = get_challenge;
321
322         (*ntlmssp_state)->get_global_myname = global_myname;
323         (*ntlmssp_state)->get_domain = lp_workgroup;
324         (*ntlmssp_state)->server_role = ROLE_DOMAIN_MEMBER; /* a good default */
325
326         (*ntlmssp_state)->expected_state = NTLMSSP_NEGOTIATE;
327
328         return NT_STATUS_OK;
329 }
330
331 /**
332  * End an NTLMSSP state machine
333  * 
334  * @param ntlmssp_state NTLMSSP State, free()ed by this function
335  */
336
337 NTSTATUS ntlmssp_server_end(NTLMSSP_STATE **ntlmssp_state)
338 {
339         TALLOC_CTX *mem_ctx = (*ntlmssp_state)->mem_ctx;
340
341         data_blob_free(&(*ntlmssp_state)->chal);
342         data_blob_free(&(*ntlmssp_state)->lm_resp);
343         data_blob_free(&(*ntlmssp_state)->nt_resp);
344
345         SAFE_FREE((*ntlmssp_state)->user);
346         SAFE_FREE((*ntlmssp_state)->domain);
347         SAFE_FREE((*ntlmssp_state)->workstation);
348
349         talloc_destroy(mem_ctx);
350         *ntlmssp_state = NULL;
351         return NT_STATUS_OK;
352 }
353
354 /**
355  * Next state function for the NTLMSSP state machine
356  * 
357  * @param ntlmssp_state NTLMSSP State
358  * @param request The request, as a DATA_BLOB
359  * @param request The reply, as an allocated DATA_BLOB, caller to free.
360  * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK. 
361  */
362
363 NTSTATUS ntlmssp_server_update(NTLMSSP_STATE *ntlmssp_state, 
364                                const DATA_BLOB request, DATA_BLOB *reply) 
365 {
366         uint32 ntlmssp_command;
367         *reply = data_blob(NULL, 0);
368
369         if (request.length) {
370                 if (!msrpc_parse(&request, "Cd",
371                                  "NTLMSSP",
372                                  &ntlmssp_command)) {
373                         return NT_STATUS_INVALID_PARAMETER;
374                 }
375         } else {
376                 /* 'datagram' mode - no neg packet */
377                 ntlmssp_command = NTLMSSP_NEGOTIATE;
378         }
379
380         if (ntlmssp_command != ntlmssp_state->expected_state) {
381                 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
382                 return NT_STATUS_INVALID_PARAMETER;
383         }
384
385         if (ntlmssp_command == NTLMSSP_NEGOTIATE) {
386                 return ntlmssp_server_negotiate(ntlmssp_state, request, reply);
387         } else if (ntlmssp_command == NTLMSSP_AUTH) {
388                 return ntlmssp_server_auth(ntlmssp_state, request, reply);
389         } else {
390                 DEBUG(1, ("unknown NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
391                 return NT_STATUS_INVALID_PARAMETER;
392         }
393 }
394
395 /*********************************************************************
396  Client side NTLMSSP
397 *********************************************************************/
398
399 /**
400  * Next state function for the Initial packet
401  * 
402  * @param ntlmssp_state NTLMSSP State
403  * @param request The request, as a DATA_BLOB.  reply.data must be NULL
404  * @param request The reply, as an allocated DATA_BLOB, caller to free.
405  * @return Errors or NT_STATUS_OK. 
406  */
407
408 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_client_state *ntlmssp_state, 
409                                   DATA_BLOB reply, DATA_BLOB *next_request) 
410 {
411         if (ntlmssp_state->unicode) {
412                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
413         }
414         
415         if (ntlmssp_state->use_ntlmv2) {
416                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
417         }
418
419         ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
420         
421         /* generate the ntlmssp negotiate packet */
422         msrpc_gen(next_request, "CddAA",
423                   "NTLMSSP",
424                   NTLMSSP_NEGOTIATE,
425                   ntlmssp_state->neg_flags,
426                   ntlmssp_state->get_domain(), 
427                   ntlmssp_state->get_global_myname());
428
429         return NT_STATUS_MORE_PROCESSING_REQUIRED;
430 }
431
432 /**
433  * Next state function for the Challenge Packet.  Generate an auth packet.
434  * 
435  * @param ntlmssp_state NTLMSSP State
436  * @param request The request, as a DATA_BLOB.  reply.data must be NULL
437  * @param request The reply, as an allocated DATA_BLOB, caller to free.
438  * @return Errors or NT_STATUS_OK. 
439  */
440
441 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_client_state *ntlmssp_state, 
442                                          const DATA_BLOB reply, DATA_BLOB *next_request) 
443 {
444         uint32 chal_flags, ntlmssp_command, unkn1, unkn2;
445         DATA_BLOB server_domain_blob;
446         DATA_BLOB challenge_blob;
447         DATA_BLOB struct_blob = data_blob(NULL, 0);
448         char *server_domain;
449         const char *chal_parse_string;
450         const char *auth_gen_string;
451         DATA_BLOB lm_response = data_blob(NULL, 0);
452         DATA_BLOB nt_response = data_blob(NULL, 0);
453         DATA_BLOB session_key = data_blob(NULL, 0);
454         DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
455
456         if (!msrpc_parse(&reply, "CdBd",
457                          "NTLMSSP",
458                          &ntlmssp_command, 
459                          &server_domain_blob,
460                          &chal_flags)) {
461                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
462                 dump_data(2, (const char *)reply.data, reply.length);
463
464                 return NT_STATUS_INVALID_PARAMETER;
465         }
466         
467         data_blob_free(&server_domain_blob);
468
469         DEBUG(3, ("Got challenge flags:\n"));
470         debug_ntlmssp_flags(chal_flags);
471
472         if (chal_flags & NTLMSSP_NEGOTIATE_UNICODE) {
473                 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
474                         chal_parse_string = "CdUdbddB";
475                 } else {
476                         chal_parse_string = "CdUdbdd";
477                 }
478                 auth_gen_string = "CdBBUUUBd";
479                 ntlmssp_state->unicode = True;
480                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
481                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_OEM;
482         } else if (chal_flags & NTLMSSP_NEGOTIATE_OEM) {
483                 if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
484                         chal_parse_string = "CdAdbddB";
485                 } else {
486                         chal_parse_string = "CdAdbdd";
487                 }
488
489                 auth_gen_string = "CdBBAAABd";
490
491                 ntlmssp_state->unicode = False;
492                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_UNICODE;
493                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
494         } else {
495                 return NT_STATUS_INVALID_PARAMETER;
496         }
497
498         if (chal_flags & NTLMSSP_NEGOTIATE_LM_KEY && lp_client_lanman_auth()) {
499                 /* server forcing us to use LM */
500                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
501                 ntlmssp_state->use_ntlmv2 = False;
502         } else {
503                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
504         }
505
506         if (!(chal_flags & NTLMSSP_NEGOTIATE_NTLM2)) {
507                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
508         }
509
510         if (!(chal_flags & NTLMSSP_NEGOTIATE_128)) {
511                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_128;
512         }
513
514         if (!(chal_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
515                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_KEY_EXCH;
516         }
517
518         DEBUG(3, ("NTLMSSP: Set final flags:\n"));
519         debug_ntlmssp_flags(ntlmssp_state->neg_flags);
520
521         if (!msrpc_parse(&reply, chal_parse_string,
522                          "NTLMSSP",
523                          &ntlmssp_command, 
524                          &server_domain,
525                          &chal_flags,
526                          &challenge_blob, 8,
527                          &unkn1, &unkn2,
528                          &struct_blob)) {
529                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
530                 dump_data(2, (const char *)reply.data, reply.length);
531                 return NT_STATUS_INVALID_PARAMETER;
532         }
533
534         ntlmssp_state->server_domain = talloc_strdup(ntlmssp_state->mem_ctx,
535                                                      server_domain);
536
537         SAFE_FREE(server_domain);
538         if (challenge_blob.length != 8) {
539                 data_blob_free(&struct_blob);
540                 return NT_STATUS_INVALID_PARAMETER;
541         }
542
543         if (!ntlmssp_state->password) {
544                 /* do nothing - blobs are zero length */
545         } else if (ntlmssp_state->use_ntlmv2) {
546
547                 if (!struct_blob.length) {
548                         /* be lazy, match win2k - we can't do NTLMv2 without it */
549                         return NT_STATUS_INVALID_PARAMETER;
550                 }
551
552                 /* TODO: if the remote server is standalone, then we should replace 'domain'
553                    with the server name as supplied above */
554                 
555                 if (!SMBNTLMv2encrypt(ntlmssp_state->user, 
556                                       ntlmssp_state->domain, 
557                                       ntlmssp_state->password, &challenge_blob, 
558                                       &struct_blob, 
559                                       &lm_response, &nt_response, &session_key)) {
560                         data_blob_free(&challenge_blob);
561                         data_blob_free(&struct_blob);
562                         return NT_STATUS_NO_MEMORY;
563                 }
564         } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
565                 struct MD5Context md5_session_nonce_ctx;
566                 uchar nt_hash[16];
567                 uchar session_nonce[16];
568                 uchar session_nonce_hash[16];
569                 uchar nt_session_key[16];
570                 E_md4hash(ntlmssp_state->password, nt_hash);
571                 
572                 lm_response = data_blob(NULL, 24);
573                 generate_random_buffer(lm_response.data, 8, False);
574                 memset(lm_response.data+8, 0, 16);
575
576                 memcpy(session_nonce, challenge_blob.data, 8);
577                 memcpy(&session_nonce[8], lm_response.data, 8);
578         
579                 MD5Init(&md5_session_nonce_ctx);
580                 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
581                 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
582                 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
583                 
584                 nt_response = data_blob(NULL, 24);
585                 SMBNTencrypt(ntlmssp_state->password,
586                              session_nonce_hash,
587                              nt_response.data);
588
589                 session_key = data_blob(NULL, 16);
590
591                 SMBsesskeygen_ntv1(nt_hash, NULL, nt_session_key);
592                 hmac_md5(nt_session_key, session_nonce, sizeof(session_nonce), session_key.data);
593         } else {
594                 
595                 
596                 uchar lm_hash[16];
597                 uchar nt_hash[16];
598                 E_deshash(ntlmssp_state->password, lm_hash);
599                 E_md4hash(ntlmssp_state->password, nt_hash);
600                 
601                 /* lanman auth is insecure, it may be disabled */
602                 if (lp_client_lanman_auth()) {
603                         lm_response = data_blob(NULL, 24);
604                         SMBencrypt(ntlmssp_state->password,challenge_blob.data,
605                                    lm_response.data);
606                         }
607                 
608                 nt_response = data_blob(NULL, 24);
609                 SMBNTencrypt(ntlmssp_state->password,challenge_blob.data,
610                              nt_response.data);
611                 
612                 session_key = data_blob(NULL, 16);
613                 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
614                     && lp_client_lanman_auth()) {
615                         SMBsesskeygen_lmv1(lm_hash, lm_response.data, 
616                                            session_key.data);
617                 } else {
618                         SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
619                 }
620         }
621         data_blob_free(&struct_blob);
622
623         /* Key exchange encryptes a new client-generated session key with
624            the password-derived key */
625         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
626                 uint8 client_session_key[16];
627                 
628                 generate_random_buffer(client_session_key, sizeof(client_session_key), False);  
629                 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
630                 SamOEMhash(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
631                 data_blob_free(&session_key);
632                 session_key = data_blob(client_session_key, sizeof(client_session_key));
633         }
634
635         /* this generates the actual auth packet */
636         if (!msrpc_gen(next_request, auth_gen_string, 
637                        "NTLMSSP", 
638                        NTLMSSP_AUTH, 
639                        lm_response.data, lm_response.length,
640                        nt_response.data, nt_response.length,
641                        ntlmssp_state->domain, 
642                        ntlmssp_state->user, 
643                        ntlmssp_state->get_global_myname(), 
644                        encrypted_session_key.data, encrypted_session_key.length,
645                        ntlmssp_state->neg_flags)) {
646                 
647                 data_blob_free(&lm_response);
648                 data_blob_free(&nt_response);
649                 data_blob_free(&session_key);
650                 return NT_STATUS_NO_MEMORY;
651         }
652
653         data_blob_free(&encrypted_session_key);
654
655         data_blob_free(&ntlmssp_state->chal);
656         data_blob_free(&ntlmssp_state->lm_resp);
657         data_blob_free(&ntlmssp_state->nt_resp);
658         data_blob_free(&ntlmssp_state->session_key);
659
660         ntlmssp_state->chal = challenge_blob;
661         ntlmssp_state->lm_resp = lm_response;
662         ntlmssp_state->nt_resp = nt_response;
663         ntlmssp_state->session_key = session_key;
664
665         return NT_STATUS_MORE_PROCESSING_REQUIRED;
666 }
667
668 NTSTATUS ntlmssp_client_start(NTLMSSP_CLIENT_STATE **ntlmssp_state)
669 {
670         TALLOC_CTX *mem_ctx;
671
672         mem_ctx = talloc_init("NTLMSSP Client context");
673         
674         *ntlmssp_state = talloc_zero(mem_ctx, sizeof(**ntlmssp_state));
675         if (!*ntlmssp_state) {
676                 DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
677                 talloc_destroy(mem_ctx);
678                 return NT_STATUS_NO_MEMORY;
679         }
680
681         (*ntlmssp_state)->mem_ctx = mem_ctx;
682
683         (*ntlmssp_state)->get_global_myname = global_myname;
684         (*ntlmssp_state)->get_domain = lp_workgroup;
685
686         (*ntlmssp_state)->unicode = True;
687
688         (*ntlmssp_state)->use_ntlmv2 = lp_client_ntlmv2_auth();
689
690         (*ntlmssp_state)->neg_flags = 
691                 NTLMSSP_NEGOTIATE_128 |
692                 NTLMSSP_NEGOTIATE_NTLM |
693                 NTLMSSP_NEGOTIATE_NTLM2 |
694                 NTLMSSP_NEGOTIATE_KEY_EXCH |
695                 /*
696                  * We need to set this to allow a later SetPassword
697                  * via the SAMR pipe to succeed. Strange.... We could
698                  * also add  NTLMSSP_NEGOTIATE_SEAL here. JRA.
699                  * */
700                 NTLMSSP_NEGOTIATE_SIGN |
701                 NTLMSSP_REQUEST_TARGET;
702
703         (*ntlmssp_state)->ref_count = 1;
704
705         return NT_STATUS_OK;
706 }
707
708 NTSTATUS ntlmssp_client_end(NTLMSSP_CLIENT_STATE **ntlmssp_state)
709 {
710         TALLOC_CTX *mem_ctx = (*ntlmssp_state)->mem_ctx;
711
712         (*ntlmssp_state)->ref_count--;
713
714         if ((*ntlmssp_state)->ref_count == 0) {
715                 data_blob_free(&(*ntlmssp_state)->chal);
716                 data_blob_free(&(*ntlmssp_state)->lm_resp);
717                 data_blob_free(&(*ntlmssp_state)->nt_resp);
718                 data_blob_free(&(*ntlmssp_state)->session_key);
719                 data_blob_free(&(*ntlmssp_state)->stored_response);
720                 talloc_destroy(mem_ctx);
721         }
722
723         *ntlmssp_state = NULL;
724         return NT_STATUS_OK;
725 }
726
727 NTSTATUS ntlmssp_client_update(NTLMSSP_CLIENT_STATE *ntlmssp_state, 
728                                DATA_BLOB reply, DATA_BLOB *next_request) 
729 {
730         NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
731         uint32 ntlmssp_command;
732         *next_request = data_blob(NULL, 0);
733
734         if (!reply.length) {
735                 /* If there is a cached reply, use it - otherwise this is the first packet */
736                 if (!ntlmssp_state->stored_response.length) {
737                         return ntlmssp_client_initial(ntlmssp_state, reply, next_request);
738                 }
739                 
740                 reply = ntlmssp_state->stored_response;
741         }
742
743         if (!msrpc_parse(&reply, "Cd",
744                          "NTLMSSP",
745                          &ntlmssp_command)) {
746                 return NT_STATUS_INVALID_PARAMETER;
747         }
748
749         if (ntlmssp_command == NTLMSSP_CHALLENGE) {
750                 nt_status = ntlmssp_client_challenge(ntlmssp_state, reply, next_request);
751         }
752         if (ntlmssp_state->stored_response.length) {
753                 data_blob_free(&ntlmssp_state->stored_response);
754         }
755         return nt_status;
756 }
757
758 NTSTATUS ntlmssp_set_username(NTLMSSP_CLIENT_STATE *ntlmssp_state, const char *user) 
759 {
760         ntlmssp_state->user = talloc_strdup(ntlmssp_state->mem_ctx, user);
761         if (!ntlmssp_state->user) {
762                 return NT_STATUS_NO_MEMORY;
763         }
764         return NT_STATUS_OK;
765 }
766
767 NTSTATUS ntlmssp_set_password(NTLMSSP_CLIENT_STATE *ntlmssp_state, const char *password) 
768 {
769         if (!password) {
770                 ntlmssp_state->password = NULL;
771         } else {
772                 ntlmssp_state->password = talloc_strdup(ntlmssp_state->mem_ctx, password);
773                 if (!ntlmssp_state->password) {
774                         return NT_STATUS_NO_MEMORY;
775                 }
776         }
777         return NT_STATUS_OK;
778 }
779
780 NTSTATUS ntlmssp_set_domain(NTLMSSP_CLIENT_STATE *ntlmssp_state, const char *domain) 
781 {
782         ntlmssp_state->domain = talloc_strdup(ntlmssp_state->mem_ctx, domain);
783         if (!ntlmssp_state->domain) {
784                 return NT_STATUS_NO_MEMORY;
785         }
786         return NT_STATUS_OK;
787 }
788
789 /**
790  *  Store a DATA_BLOB containing an NTLMSSP response, for use later.
791  *  This 'keeps' the data blob - the caller must *not* free it.
792  */
793
794 NTSTATUS ntlmssp_client_store_response(NTLMSSP_CLIENT_STATE *ntlmssp_state,
795                                        DATA_BLOB response) 
796 {
797         data_blob_free(&ntlmssp_state->stored_response);
798         ntlmssp_state->stored_response = response;
799         return NT_STATUS_OK;
800 }