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