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