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