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