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