r11076: Still working on bug #1828, PPC hell. The PPC client sends the
[jelmer/samba4-debian.git] / source / auth / ntlmssp / ntlmssp_server.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, client server side parsing
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8    Copyright (C) Stefan Metzmacher 2005
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 #include "auth/auth.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "lib/crypto/crypto.h"
29 #include "pstring.h"
30 #include "system/filesys.h"
31
32 /** 
33  * Set a username on an NTLMSSP context - ensures it is talloc()ed 
34  *
35  */
36
37 static NTSTATUS ntlmssp_set_username(struct gensec_ntlmssp_state *gensec_ntlmssp_state, const char *user) 
38 {
39         if (!user) {
40                 /* it should be at least "" */
41                 DEBUG(1, ("NTLMSSP failed to set username - cannot accept NULL username\n"));
42                 return NT_STATUS_INVALID_PARAMETER;
43         }
44         gensec_ntlmssp_state->user = talloc_strdup(gensec_ntlmssp_state, user);
45         if (!gensec_ntlmssp_state->user) {
46                 return NT_STATUS_NO_MEMORY;
47         }
48         return NT_STATUS_OK;
49 }
50
51 /** 
52  * Set a domain on an NTLMSSP context - ensures it is talloc()ed 
53  *
54  */
55 static NTSTATUS ntlmssp_set_domain(struct gensec_ntlmssp_state *gensec_ntlmssp_state, const char *domain) 
56 {
57         gensec_ntlmssp_state->domain = talloc_strdup(gensec_ntlmssp_state, domain);
58         if (!gensec_ntlmssp_state->domain) {
59                 return NT_STATUS_NO_MEMORY;
60         }
61         return NT_STATUS_OK;
62 }
63
64 /** 
65  * Set a workstation on an NTLMSSP context - ensures it is talloc()ed 
66  *
67  */
68 static NTSTATUS ntlmssp_set_workstation(struct gensec_ntlmssp_state *gensec_ntlmssp_state, const char *workstation) 
69 {
70         gensec_ntlmssp_state->workstation = talloc_strdup(gensec_ntlmssp_state, workstation);
71         if (!gensec_ntlmssp_state->workstation) {
72                 return NT_STATUS_NO_MEMORY;
73         }
74         return NT_STATUS_OK;
75 }
76
77 /**
78  * Determine correct target name flags for reply, given server role 
79  * and negotiated flags
80  * 
81  * @param gensec_ntlmssp_state NTLMSSP State
82  * @param neg_flags The flags from the packet
83  * @param chal_flags The flags to be set in the reply packet
84  * @return The 'target name' string.
85  */
86
87 static const char *ntlmssp_target_name(struct gensec_ntlmssp_state *gensec_ntlmssp_state,
88                                        uint32_t neg_flags, uint32_t *chal_flags) 
89 {
90         if (neg_flags & NTLMSSP_REQUEST_TARGET) {
91                 *chal_flags |= NTLMSSP_CHAL_TARGET_INFO;
92                 *chal_flags |= NTLMSSP_REQUEST_TARGET;
93                 if (gensec_ntlmssp_state->server_role == ROLE_STANDALONE) {
94                         *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
95                         return gensec_ntlmssp_state->server_name;
96                 } else {
97                         *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
98                         return gensec_ntlmssp_state->get_domain();
99                 };
100         } else {
101                 return "";
102         }
103 }
104
105 /*
106   Andrew, please remove these totally bogus calls when you get time
107 */
108 static BOOL get_myfullname(char *my_name)
109 {
110         pstring hostname;
111
112         *hostname = 0;
113
114         /* get my host name */
115         if (gethostname(hostname, sizeof(hostname)) == -1) {
116                 DEBUG(0,("gethostname failed\n"));
117                 return False;
118         } 
119
120         /* Ensure null termination. */
121         hostname[sizeof(hostname)-1] = '\0';
122
123         if (my_name)
124                 fstrcpy(my_name, hostname);
125         return True;
126 }
127
128 static BOOL get_mydomname(char *my_domname)
129 {
130         pstring hostname;
131         char *p;
132
133         /* arrgh! relies on full name in system */
134
135         *hostname = 0;
136         /* get my host name */
137         if (gethostname(hostname, sizeof(hostname)) == -1) {
138                 DEBUG(0,("gethostname failed\n"));
139                 return False;
140         } 
141
142         /* Ensure null termination. */
143         hostname[sizeof(hostname)-1] = '\0';
144
145         p = strchr_m(hostname, '.');
146
147         if (!p)
148                 return False;
149
150         p++;
151         
152         if (my_domname)
153                 fstrcpy(my_domname, p);
154
155         return True;
156 }
157
158
159
160 /**
161  * Next state function for the Negotiate packet
162  * 
163  * @param gensec_security GENSEC state
164  * @param out_mem_ctx Memory context for *out
165  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
166  * @param out The reply, as an allocated DATA_BLOB, caller to free.
167  * @return Errors or MORE_PROCESSING_REQUIRED if (normal) a reply is required. 
168  */
169
170 NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security, 
171                                   TALLOC_CTX *out_mem_ctx, 
172                                   const DATA_BLOB in, DATA_BLOB *out) 
173 {
174         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
175         DATA_BLOB struct_blob;
176         fstring dnsname, dnsdomname;
177         uint32_t neg_flags = 0;
178         uint32_t ntlmssp_command, chal_flags;
179         char *cliname=NULL, *domname=NULL;
180         const uint8_t *cryptkey;
181         const char *target_name;
182
183         /* parse the NTLMSSP packet */
184 #if 0
185         file_save("ntlmssp_negotiate.dat", request.data, request.length);
186 #endif
187
188         if (in.length) {
189                 BOOL parse_ok = msrpc_parse(out_mem_ctx,
190                                  &in, "CddAA",
191                                  "NTLMSSP",
192                                  &ntlmssp_command,
193                                  &neg_flags,
194                                  &cliname,
195                                  &domname);
196                 if (!parse_ok) {
197                         parse_ok = msrpc_parse(out_mem_ctx,
198                                          &in, "CddUU",
199                                          "NTLMSSP",
200                                          &ntlmssp_command,
201                                          &neg_flags,
202                                          &cliname,
203                                          &domname);
204                 }
205                 if (!parse_ok) {
206                         DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP:\n"));
207                         dump_data(2, in.data, in.length);
208                         return NT_STATUS_INVALID_PARAMETER;
209                 }
210                 
211                 debug_ntlmssp_flags(neg_flags);
212         }
213         
214         ntlmssp_handle_neg_flags(gensec_ntlmssp_state, neg_flags, gensec_ntlmssp_state->allow_lm_key);
215
216         /* Ask our caller what challenge they would like in the packet */
217         cryptkey = gensec_ntlmssp_state->get_challenge(gensec_ntlmssp_state);
218
219         /* Check if we may set the challenge */
220         if (!gensec_ntlmssp_state->may_set_challenge(gensec_ntlmssp_state)) {
221                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
222         }
223
224         /* The flags we send back are not just the negotiated flags,
225          * they are also 'what is in this packet'.  Therfore, we
226          * operate on 'chal_flags' from here on 
227          */
228
229         chal_flags = gensec_ntlmssp_state->neg_flags;
230
231         /* get the right name to fill in as 'target' */
232         target_name = ntlmssp_target_name(gensec_ntlmssp_state, 
233                                           neg_flags, &chal_flags); 
234         if (target_name == NULL) 
235                 return NT_STATUS_INVALID_PARAMETER;
236
237         gensec_ntlmssp_state->chal = data_blob_talloc(gensec_ntlmssp_state, cryptkey, 8);
238         gensec_ntlmssp_state->internal_chal = data_blob_talloc(gensec_ntlmssp_state, cryptkey, 8);
239
240         /* This should be a 'netbios domain -> DNS domain' mapping */
241         dnsdomname[0] = '\0';
242         get_mydomname(dnsdomname);
243         strlower_m(dnsdomname);
244         
245         dnsname[0] = '\0';
246         get_myfullname(dnsname);
247         
248         /* This creates the 'blob' of names that appears at the end of the packet */
249         if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) 
250         {
251                 const char *target_name_dns = "";
252                 if (chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN) {
253                         target_name_dns = dnsdomname;
254                 } else if (chal_flags |= NTLMSSP_TARGET_TYPE_SERVER) {
255                         target_name_dns = dnsname;
256                 }
257
258                 msrpc_gen(out_mem_ctx, 
259                           &struct_blob, "aaaaa",
260                           NTLMSSP_NAME_TYPE_DOMAIN, target_name,
261                           NTLMSSP_NAME_TYPE_SERVER, gensec_ntlmssp_state->server_name,
262                           NTLMSSP_NAME_TYPE_DOMAIN_DNS, dnsdomname,
263                           NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
264                           0, "");
265         } else {
266                 struct_blob = data_blob(NULL, 0);
267         }
268
269         {
270                 /* Marshel the packet in the right format, be it unicode or ASCII */
271                 const char *gen_string;
272                 if (gensec_ntlmssp_state->unicode) {
273                         gen_string = "CdUdbddB";
274                 } else {
275                         gen_string = "CdAdbddB";
276                 }
277                 
278                 msrpc_gen(out_mem_ctx, 
279                           out, gen_string,
280                           "NTLMSSP", 
281                           NTLMSSP_CHALLENGE,
282                           target_name,
283                           chal_flags,
284                           cryptkey, 8,
285                           0, 0,
286                           struct_blob.data, struct_blob.length);
287         }
288                 
289         gensec_ntlmssp_state->expected_state = NTLMSSP_AUTH;
290
291         return NT_STATUS_MORE_PROCESSING_REQUIRED;
292 }
293
294 /**
295  * Next state function for the Authenticate packet
296  * 
297  * @param gensec_ntlmssp_state NTLMSSP State
298  * @param request The request, as a DATA_BLOB
299  * @return Errors or NT_STATUS_OK. 
300  */
301
302 static NTSTATUS ntlmssp_server_preauth(struct gensec_ntlmssp_state *gensec_ntlmssp_state,
303                                        const DATA_BLOB request) 
304 {
305         uint32_t ntlmssp_command, auth_flags;
306         NTSTATUS nt_status;
307
308         uint8_t session_nonce_hash[16];
309
310         const char *parse_string;
311         char *domain = NULL;
312         char *user = NULL;
313         char *workstation = NULL;
314
315 #if 0
316         file_save("ntlmssp_auth.dat", request.data, request.length);
317 #endif
318
319         if (gensec_ntlmssp_state->unicode) {
320                 parse_string = "CdBBUUUBd";
321         } else {
322                 parse_string = "CdBBAAABd";
323         }
324
325         /* zero these out */
326         data_blob_free(&gensec_ntlmssp_state->lm_resp);
327         data_blob_free(&gensec_ntlmssp_state->nt_resp);
328         data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
329
330         gensec_ntlmssp_state->user = NULL;
331         gensec_ntlmssp_state->domain = NULL;
332         gensec_ntlmssp_state->workstation = NULL;
333
334         /* now the NTLMSSP encoded auth hashes */
335         if (!msrpc_parse(gensec_ntlmssp_state, 
336                          &request, parse_string,
337                          "NTLMSSP", 
338                          &ntlmssp_command, 
339                          &gensec_ntlmssp_state->lm_resp,
340                          &gensec_ntlmssp_state->nt_resp,
341                          &domain, 
342                          &user, 
343                          &workstation,
344                          &gensec_ntlmssp_state->encrypted_session_key,
345                          &auth_flags)) {
346                 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
347                 dump_data(10, request.data, request.length);
348
349                 /* zero this out */
350                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
351                 auth_flags = 0;
352                 
353                 /* Try again with a shorter string (Win9X truncates this packet) */
354                 if (gensec_ntlmssp_state->unicode) {
355                         parse_string = "CdBBUUU";
356                 } else {
357                         parse_string = "CdBBAAA";
358                 }
359
360                 /* now the NTLMSSP encoded auth hashes */
361                 if (!msrpc_parse(gensec_ntlmssp_state, 
362                                  &request, parse_string,
363                                  "NTLMSSP", 
364                                  &ntlmssp_command, 
365                                  &gensec_ntlmssp_state->lm_resp,
366                                  &gensec_ntlmssp_state->nt_resp,
367                                  &domain, 
368                                  &user, 
369                                  &workstation)) {
370                         DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP:\n"));
371                         dump_data(2, request.data, request.length);
372
373                         return NT_STATUS_INVALID_PARAMETER;
374                 }
375         }
376
377         if (auth_flags)
378                 ntlmssp_handle_neg_flags(gensec_ntlmssp_state, auth_flags, gensec_ntlmssp_state->allow_lm_key);
379
380         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(gensec_ntlmssp_state, domain))) {
381                 /* zero this out */
382                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
383                 return nt_status;
384         }
385
386         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(gensec_ntlmssp_state, user))) {
387                 /* zero this out */
388                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
389                 return nt_status;
390         }
391
392         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(gensec_ntlmssp_state, workstation))) {
393                 /* zero this out */
394                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
395                 return nt_status;
396         }
397
398         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
399                  gensec_ntlmssp_state->user, gensec_ntlmssp_state->domain, gensec_ntlmssp_state->workstation, (unsigned long)gensec_ntlmssp_state->lm_resp.length, (unsigned long)gensec_ntlmssp_state->nt_resp.length));
400
401 #if 0
402         file_save("nthash1.dat",  &gensec_ntlmssp_state->nt_resp.data,  &gensec_ntlmssp_state->nt_resp.length);
403         file_save("lmhash1.dat",  &gensec_ntlmssp_state->lm_resp.data,  &gensec_ntlmssp_state->lm_resp.length);
404 #endif
405
406         /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a 
407            client challenge 
408         
409            However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
410         */
411         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
412                 if (gensec_ntlmssp_state->nt_resp.length == 24 && gensec_ntlmssp_state->lm_resp.length == 24) {
413                         struct MD5Context md5_session_nonce_ctx;
414                         SMB_ASSERT(gensec_ntlmssp_state->internal_chal.data 
415                                    && gensec_ntlmssp_state->internal_chal.length == 8);
416                         
417                         gensec_ntlmssp_state->doing_ntlm2 = True;
418
419                         memcpy(gensec_ntlmssp_state->crypt.ntlm2.session_nonce, gensec_ntlmssp_state->internal_chal.data, 8);
420                         memcpy(&gensec_ntlmssp_state->crypt.ntlm2.session_nonce[8], gensec_ntlmssp_state->lm_resp.data, 8);
421                         
422                         MD5Init(&md5_session_nonce_ctx);
423                         MD5Update(&md5_session_nonce_ctx, gensec_ntlmssp_state->crypt.ntlm2.session_nonce, 16);
424                         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
425                         
426                         gensec_ntlmssp_state->chal = data_blob_talloc(gensec_ntlmssp_state, 
427                                                                session_nonce_hash, 8);
428
429                         /* LM response is no longer useful, zero it out */
430                         data_blob_free(&gensec_ntlmssp_state->lm_resp);
431
432                         /* We changed the effective challenge - set it */
433                         if (!NT_STATUS_IS_OK(nt_status = 
434                                              gensec_ntlmssp_state->set_challenge(gensec_ntlmssp_state, 
435                                                                                  &gensec_ntlmssp_state->chal))) {
436                                 /* zero this out */
437                                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
438                                 return nt_status;
439                         }
440
441                         /* LM Key is incompatible... */
442                         gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
443                 }
444         }
445         return NT_STATUS_OK;
446 }
447
448 /**
449  * Next state function for the Authenticate packet 
450  * (after authentication - figures out the session keys etc)
451  * 
452  * @param gensec_ntlmssp_state NTLMSSP State
453  * @return Errors or NT_STATUS_OK. 
454  */
455
456 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security, 
457                                         DATA_BLOB *user_session_key, 
458                                         DATA_BLOB *lm_session_key) 
459 {
460         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
461         NTSTATUS nt_status;
462         DATA_BLOB session_key = data_blob(NULL, 0);
463
464         if (user_session_key)
465                 dump_data_pw("USER session key:\n", user_session_key->data, user_session_key->length);
466
467         if (lm_session_key) 
468                 dump_data_pw("LM first-8:\n", lm_session_key->data, lm_session_key->length);
469
470         /* Handle the different session key derivation for NTLM2 */
471         if (gensec_ntlmssp_state->doing_ntlm2) {
472                 if (user_session_key && user_session_key->data && user_session_key->length == 16) {
473                         session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
474                         hmac_md5(user_session_key->data, gensec_ntlmssp_state->crypt.ntlm2.session_nonce, 
475                                  sizeof(gensec_ntlmssp_state->crypt.ntlm2.session_nonce), session_key.data);
476                         DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
477                         dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
478                         
479                 } else {
480                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
481                         session_key = data_blob(NULL, 0);
482                 }
483         } else if ((gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
484                 /* Ensure we can never get here on NTLMv2 */
485                 && (gensec_ntlmssp_state->nt_resp.length == 0 || gensec_ntlmssp_state->nt_resp.length == 24)) {
486
487                 if (lm_session_key && lm_session_key->data && lm_session_key->length >= 8) {
488                         if (gensec_ntlmssp_state->lm_resp.data && gensec_ntlmssp_state->lm_resp.length == 24) {
489                                 session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
490                                 SMBsesskeygen_lm_sess_key(lm_session_key->data, gensec_ntlmssp_state->lm_resp.data, 
491                                                           session_key.data);
492                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
493                                 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
494                         } else {
495                                 
496                                 /* When there is no LM response, just use zeros */
497                                 static const uint8_t zeros[24];
498                                 session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
499                                 SMBsesskeygen_lm_sess_key(zeros, zeros, 
500                                                           session_key.data);
501                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
502                                 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
503                         }
504                 } else {
505                         /* LM Key not selected */
506                         gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
507
508                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
509                         session_key = data_blob(NULL, 0);
510                 }
511
512         } else if (user_session_key && user_session_key->data) {
513                 session_key = *user_session_key;
514                 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
515                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
516
517                 /* LM Key not selected */
518                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
519
520         } else if (lm_session_key && lm_session_key->data) {
521                 /* Very weird to have LM key, but no user session key, but anyway.. */
522                 session_key = *lm_session_key;
523                 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
524                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
525
526                 /* LM Key not selected */
527                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
528
529         } else {
530                 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
531                 session_key = data_blob(NULL, 0);
532
533                 /* LM Key not selected */
534                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
535         }
536
537         /* With KEY_EXCH, the client supplies the proposed session key, 
538            but encrypts it with the long-term key */
539         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
540                 if (!gensec_ntlmssp_state->encrypted_session_key.data 
541                     || gensec_ntlmssp_state->encrypted_session_key.length != 16) {
542                         data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
543                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n", 
544                                   (unsigned)gensec_ntlmssp_state->encrypted_session_key.length));
545                         return NT_STATUS_INVALID_PARAMETER;
546                 } else if (!session_key.data || session_key.length != 16) {
547                         DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n", 
548                                   (unsigned)session_key.length));
549                         gensec_ntlmssp_state->session_key = session_key;
550                 } else {
551                         dump_data_pw("KEY_EXCH session key (enc):\n", 
552                                      gensec_ntlmssp_state->encrypted_session_key.data, 
553                                      gensec_ntlmssp_state->encrypted_session_key.length);
554                         arcfour_crypt(gensec_ntlmssp_state->encrypted_session_key.data, 
555                                       session_key.data, 
556                                       gensec_ntlmssp_state->encrypted_session_key.length);
557                         gensec_ntlmssp_state->session_key = data_blob_talloc(gensec_ntlmssp_state, 
558                                                                       gensec_ntlmssp_state->encrypted_session_key.data, 
559                                                                       gensec_ntlmssp_state->encrypted_session_key.length);
560                         dump_data_pw("KEY_EXCH session key:\n", gensec_ntlmssp_state->encrypted_session_key.data, 
561                                      gensec_ntlmssp_state->encrypted_session_key.length);
562                 }
563         } else {
564                 gensec_ntlmssp_state->session_key = session_key;
565         }
566
567         /* The server might need us to use a partial-strength session key */
568         ntlmssp_weaken_keys(gensec_ntlmssp_state);
569
570         if ((gensec_security->want_features & GENSEC_FEATURE_SIGN)
571             || (gensec_security->want_features & GENSEC_FEATURE_SEAL)) {
572                 nt_status = ntlmssp_sign_init(gensec_ntlmssp_state);
573         } else {
574                 nt_status = NT_STATUS_OK;
575         }
576
577         data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
578         
579         /* allow arbitarily many authentications, but watch that this will cause a 
580            memory leak, until the gensec_ntlmssp_state is shutdown 
581         */
582
583         if (gensec_ntlmssp_state->server_multiple_authentications) {
584                 gensec_ntlmssp_state->expected_state = NTLMSSP_AUTH;
585         } else {
586                 gensec_ntlmssp_state->expected_state = NTLMSSP_DONE;
587         }
588
589         return nt_status;
590 }
591
592
593 /**
594  * Next state function for the Authenticate packet
595  * 
596  * @param gensec_security GENSEC state
597  * @param out_mem_ctx Memory context for *out
598  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
599  * @param out The reply, as an allocated DATA_BLOB, caller to free.
600  * @return Errors or NT_STATUS_OK if authentication sucessful
601  */
602
603 NTSTATUS ntlmssp_server_auth(struct gensec_security *gensec_security, 
604                              TALLOC_CTX *out_mem_ctx, 
605                              const DATA_BLOB in, DATA_BLOB *out) 
606 {       
607         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
608         DATA_BLOB user_session_key = data_blob(NULL, 0);
609         DATA_BLOB lm_session_key = data_blob(NULL, 0);
610         NTSTATUS nt_status;
611
612         /* zero the outbound NTLMSSP packet */
613         *out = data_blob_talloc(out_mem_ctx, NULL, 0);
614
615         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_server_preauth(gensec_ntlmssp_state, in))) {
616                 return nt_status;
617         }
618
619         /*
620          * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
621          * is required (by "ntlm auth = no" and "lm auth = no" being set in the
622          * smb.conf file) and no NTLMv2 response was sent then the password check
623          * will fail here. JRA.
624          */
625
626         /* Finally, actually ask if the password is OK */
627
628         if (!NT_STATUS_IS_OK(nt_status = gensec_ntlmssp_state->check_password(gensec_ntlmssp_state, 
629                                                                               &user_session_key, &lm_session_key))) {
630                 return nt_status;
631         }
632         
633         if (gensec_security->want_features
634             & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL|GENSEC_FEATURE_SESSION_KEY)) {
635                 return ntlmssp_server_postauth(gensec_security, &user_session_key, &lm_session_key);
636         } else {
637                 gensec_ntlmssp_state->session_key = data_blob(NULL, 0);
638                 return NT_STATUS_OK;
639         }
640 }
641
642 /**
643  * Return the challenge as determined by the authentication subsystem 
644  * @return an 8 byte random challenge
645  */
646
647 static const uint8_t *auth_ntlmssp_get_challenge(const struct gensec_ntlmssp_state *gensec_ntlmssp_state)
648 {
649         NTSTATUS status;
650         const uint8_t *chal;
651
652         status = auth_get_challenge(gensec_ntlmssp_state->auth_context, &chal);
653         if (!NT_STATUS_IS_OK(status)) {
654                 return NULL;
655         }
656
657         return chal;
658 }
659
660 /**
661  * Some authentication methods 'fix' the challenge, so we may not be able to set it
662  *
663  * @return If the effective challenge used by the auth subsystem may be modified
664  */
665 static BOOL auth_ntlmssp_may_set_challenge(const struct gensec_ntlmssp_state *gensec_ntlmssp_state)
666 {
667         return auth_challenge_may_be_modified(gensec_ntlmssp_state->auth_context);
668 }
669
670 /**
671  * NTLM2 authentication modifies the effective challenge, 
672  * @param challenge The new challenge value
673  */
674 static NTSTATUS auth_ntlmssp_set_challenge(struct gensec_ntlmssp_state *gensec_ntlmssp_state, DATA_BLOB *challenge)
675 {
676         NTSTATUS nt_status;
677         struct auth_context *auth_context = gensec_ntlmssp_state->auth_context;
678         const uint8_t *chal;
679
680         if (challenge->length != 8) {
681                 return NT_STATUS_INVALID_PARAMETER;
682         }
683
684         chal = challenge->data;
685
686         nt_status = auth_context_set_challenge(auth_context, chal, "NTLMSSP callback (NTLM2)");
687
688         return nt_status;
689 }
690
691 /**
692  * Check the password on an NTLMSSP login.  
693  *
694  * Return the session keys used on the connection.
695  */
696
697 static NTSTATUS auth_ntlmssp_check_password(struct gensec_ntlmssp_state *gensec_ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key) 
698 {
699         NTSTATUS nt_status;
700         struct auth_usersupplied_info *user_info = talloc(gensec_ntlmssp_state, struct auth_usersupplied_info);
701         if (!user_info) {
702                 return NT_STATUS_NO_MEMORY;
703         }
704
705         user_info->flags = 0;
706         user_info->mapped_state = False;
707         user_info->client.account_name = gensec_ntlmssp_state->user;
708         user_info->client.domain_name = gensec_ntlmssp_state->domain;
709         user_info->workstation_name = gensec_ntlmssp_state->workstation;
710         
711         user_info->password_state = AUTH_PASSWORD_RESPONSE;
712         user_info->password.response.lanman = gensec_ntlmssp_state->lm_resp;
713         user_info->password.response.lanman.data = talloc_steal(user_info, gensec_ntlmssp_state->lm_resp.data);
714         user_info->password.response.nt = gensec_ntlmssp_state->nt_resp;
715         user_info->password.response.nt.data = talloc_steal(user_info, gensec_ntlmssp_state->nt_resp.data);
716
717         nt_status = auth_check_password(gensec_ntlmssp_state->auth_context, gensec_ntlmssp_state,
718                                         user_info, &gensec_ntlmssp_state->server_info);
719         talloc_free(user_info);
720         NT_STATUS_NOT_OK_RETURN(nt_status);
721
722         if (gensec_ntlmssp_state->server_info->user_session_key.length) {
723                 DEBUG(10, ("Got NT session key of length %u\n", 
724                            (unsigned)gensec_ntlmssp_state->server_info->user_session_key.length));
725                 *user_session_key = data_blob_talloc(gensec_ntlmssp_state, 
726                                                    gensec_ntlmssp_state->server_info->user_session_key.data,
727                                                    gensec_ntlmssp_state->server_info->user_session_key.length);
728         }
729         if (gensec_ntlmssp_state->server_info->lm_session_key.length) {
730                 DEBUG(10, ("Got LM session key of length %u\n", 
731                            (unsigned)gensec_ntlmssp_state->server_info->lm_session_key.length));
732                 *lm_session_key = data_blob_talloc(gensec_ntlmssp_state, 
733                                                    gensec_ntlmssp_state->server_info->lm_session_key.data,
734                                                    gensec_ntlmssp_state->server_info->lm_session_key.length);
735         }
736         return nt_status;
737 }
738
739 /** 
740  * Return the credentials of a logged on user, including session keys
741  * etc.
742  *
743  * Only valid after a successful authentication
744  *
745  * May only be called once per authentication.
746  *
747  */
748
749 NTSTATUS gensec_ntlmssp_session_info(struct gensec_security *gensec_security,
750                                      struct auth_session_info **session_info) 
751 {
752         NTSTATUS nt_status;
753         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
754
755         nt_status = auth_generate_session_info(gensec_ntlmssp_state, gensec_ntlmssp_state->server_info, session_info);
756         NT_STATUS_NOT_OK_RETURN(nt_status);
757
758         (*session_info)->session_key = data_blob_talloc(*session_info, 
759                                                         gensec_ntlmssp_state->session_key.data,
760                                                         gensec_ntlmssp_state->session_key.length);
761
762         return NT_STATUS_OK;
763 }
764
765 /**
766  * Start NTLMSSP on the server side 
767  *
768  */
769 NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
770 {
771         NTSTATUS nt_status;
772         struct gensec_ntlmssp_state *gensec_ntlmssp_state;
773
774         nt_status = gensec_ntlmssp_start(gensec_security);
775         NT_STATUS_NOT_OK_RETURN(nt_status);
776
777         gensec_ntlmssp_state = gensec_security->private_data;
778
779         gensec_ntlmssp_state->role = NTLMSSP_SERVER;
780
781         gensec_ntlmssp_state->workstation = NULL;
782         gensec_ntlmssp_state->server_name = lp_netbios_name();
783
784         gensec_ntlmssp_state->get_domain = lp_workgroup;
785
786         gensec_ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
787
788         gensec_ntlmssp_state->allow_lm_key = (lp_lanman_auth() 
789                                           && lp_parm_bool(-1, "ntlmssp_server", "allow_lm_key", False));
790
791         gensec_ntlmssp_state->server_multiple_authentications = False;
792         
793         gensec_ntlmssp_state->neg_flags = 
794                 NTLMSSP_NEGOTIATE_NTLM;
795
796         gensec_ntlmssp_state->lm_resp = data_blob(NULL, 0);
797         gensec_ntlmssp_state->nt_resp = data_blob(NULL, 0);
798         gensec_ntlmssp_state->encrypted_session_key = data_blob(NULL, 0);
799
800         if (lp_parm_bool(-1, "ntlmssp_server", "128bit", True)) {
801                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;               
802         }
803
804         if (lp_parm_bool(-1, "ntlmssp_server", "keyexchange", True)) {
805                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;          
806         }
807
808         if (lp_parm_bool(-1, "ntlmssp_server", "ntlm2", True)) {
809                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;             
810         }
811
812         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
813                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
814         }
815         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
816                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
817         }
818
819         nt_status = auth_context_create(gensec_ntlmssp_state, lp_auth_methods(), 
820                                         &gensec_ntlmssp_state->auth_context,
821                                         gensec_security->event_ctx);
822         NT_STATUS_NOT_OK_RETURN(nt_status);
823
824         gensec_ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
825         gensec_ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
826         gensec_ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
827         gensec_ntlmssp_state->check_password = auth_ntlmssp_check_password;
828         gensec_ntlmssp_state->server_role = lp_server_role();
829
830         return NT_STATUS_OK;
831 }
832