r11366: Pass around the flags which indicate if we should support plaintext
[sfrench/samba-autobuild/.git] / source4 / auth / ntlmssp / ntlmssp_server.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, client server side parsing
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8    Copyright (C) Stefan Metzmacher 2005
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "auth/auth.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "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         const uint8_t *cryptkey;
180         const char *target_name;
181
182         /* parse the NTLMSSP packet */
183 #if 0
184         file_save("ntlmssp_negotiate.dat", request.data, request.length);
185 #endif
186
187         if (in.length) {
188                 if ((in.length < 16) || !msrpc_parse(out_mem_ctx, &in, "Cdd",
189                                                          "NTLMSSP",
190                                                          &ntlmssp_command,
191                                                          &neg_flags)) {
192                         DEBUG(1, ("ntlmssp_server_negotiate: failed to parse "
193                                 "NTLMSSP Negotiate of length %u:\n",
194                                 (unsigned int)in.length ));
195                         dump_data(2, in.data, in.length);
196                         return NT_STATUS_INVALID_PARAMETER;
197                 }
198                 debug_ntlmssp_flags(neg_flags);
199         }
200         
201         ntlmssp_handle_neg_flags(gensec_ntlmssp_state, neg_flags, gensec_ntlmssp_state->allow_lm_key);
202
203         /* Ask our caller what challenge they would like in the packet */
204         cryptkey = gensec_ntlmssp_state->get_challenge(gensec_ntlmssp_state);
205
206         /* Check if we may set the challenge */
207         if (!gensec_ntlmssp_state->may_set_challenge(gensec_ntlmssp_state)) {
208                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
209         }
210
211         /* The flags we send back are not just the negotiated flags,
212          * they are also 'what is in this packet'.  Therfore, we
213          * operate on 'chal_flags' from here on 
214          */
215
216         chal_flags = gensec_ntlmssp_state->neg_flags;
217
218         /* get the right name to fill in as 'target' */
219         target_name = ntlmssp_target_name(gensec_ntlmssp_state, 
220                                           neg_flags, &chal_flags); 
221         if (target_name == NULL) 
222                 return NT_STATUS_INVALID_PARAMETER;
223
224         gensec_ntlmssp_state->chal = data_blob_talloc(gensec_ntlmssp_state, cryptkey, 8);
225         gensec_ntlmssp_state->internal_chal = data_blob_talloc(gensec_ntlmssp_state, cryptkey, 8);
226
227         /* This should be a 'netbios domain -> DNS domain' mapping */
228         dnsdomname[0] = '\0';
229         get_mydomname(dnsdomname);
230         strlower_m(dnsdomname);
231         
232         dnsname[0] = '\0';
233         get_myfullname(dnsname);
234         
235         /* This creates the 'blob' of names that appears at the end of the packet */
236         if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) 
237         {
238                 const char *target_name_dns = "";
239                 if (chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN) {
240                         target_name_dns = dnsdomname;
241                 } else if (chal_flags |= NTLMSSP_TARGET_TYPE_SERVER) {
242                         target_name_dns = dnsname;
243                 }
244
245                 msrpc_gen(out_mem_ctx, 
246                           &struct_blob, "aaaaa",
247                           NTLMSSP_NAME_TYPE_DOMAIN, target_name,
248                           NTLMSSP_NAME_TYPE_SERVER, gensec_ntlmssp_state->server_name,
249                           NTLMSSP_NAME_TYPE_DOMAIN_DNS, dnsdomname,
250                           NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
251                           0, "");
252         } else {
253                 struct_blob = data_blob(NULL, 0);
254         }
255
256         {
257                 /* Marshel the packet in the right format, be it unicode or ASCII */
258                 const char *gen_string;
259                 if (gensec_ntlmssp_state->unicode) {
260                         gen_string = "CdUdbddB";
261                 } else {
262                         gen_string = "CdAdbddB";
263                 }
264                 
265                 msrpc_gen(out_mem_ctx, 
266                           out, gen_string,
267                           "NTLMSSP", 
268                           NTLMSSP_CHALLENGE,
269                           target_name,
270                           chal_flags,
271                           cryptkey, 8,
272                           0, 0,
273                           struct_blob.data, struct_blob.length);
274         }
275                 
276         gensec_ntlmssp_state->expected_state = NTLMSSP_AUTH;
277
278         return NT_STATUS_MORE_PROCESSING_REQUIRED;
279 }
280
281 /**
282  * Next state function for the Authenticate packet
283  * 
284  * @param gensec_ntlmssp_state NTLMSSP State
285  * @param request The request, as a DATA_BLOB
286  * @return Errors or NT_STATUS_OK. 
287  */
288
289 static NTSTATUS ntlmssp_server_preauth(struct gensec_ntlmssp_state *gensec_ntlmssp_state,
290                                        const DATA_BLOB request) 
291 {
292         uint32_t ntlmssp_command, auth_flags;
293         NTSTATUS nt_status;
294
295         uint8_t session_nonce_hash[16];
296
297         const char *parse_string;
298         char *domain = NULL;
299         char *user = NULL;
300         char *workstation = NULL;
301
302 #if 0
303         file_save("ntlmssp_auth.dat", request.data, request.length);
304 #endif
305
306         if (gensec_ntlmssp_state->unicode) {
307                 parse_string = "CdBBUUUBd";
308         } else {
309                 parse_string = "CdBBAAABd";
310         }
311
312         /* zero these out */
313         data_blob_free(&gensec_ntlmssp_state->lm_resp);
314         data_blob_free(&gensec_ntlmssp_state->nt_resp);
315         data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
316
317         gensec_ntlmssp_state->user = NULL;
318         gensec_ntlmssp_state->domain = NULL;
319         gensec_ntlmssp_state->workstation = NULL;
320
321         /* now the NTLMSSP encoded auth hashes */
322         if (!msrpc_parse(gensec_ntlmssp_state, 
323                          &request, parse_string,
324                          "NTLMSSP", 
325                          &ntlmssp_command, 
326                          &gensec_ntlmssp_state->lm_resp,
327                          &gensec_ntlmssp_state->nt_resp,
328                          &domain, 
329                          &user, 
330                          &workstation,
331                          &gensec_ntlmssp_state->encrypted_session_key,
332                          &auth_flags)) {
333                 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
334                 dump_data(10, request.data, request.length);
335
336                 /* zero this out */
337                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
338                 auth_flags = 0;
339                 
340                 /* Try again with a shorter string (Win9X truncates this packet) */
341                 if (gensec_ntlmssp_state->unicode) {
342                         parse_string = "CdBBUUU";
343                 } else {
344                         parse_string = "CdBBAAA";
345                 }
346
347                 /* now the NTLMSSP encoded auth hashes */
348                 if (!msrpc_parse(gensec_ntlmssp_state, 
349                                  &request, parse_string,
350                                  "NTLMSSP", 
351                                  &ntlmssp_command, 
352                                  &gensec_ntlmssp_state->lm_resp,
353                                  &gensec_ntlmssp_state->nt_resp,
354                                  &domain, 
355                                  &user, 
356                                  &workstation)) {
357                         DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP:\n"));
358                         dump_data(2, request.data, request.length);
359
360                         return NT_STATUS_INVALID_PARAMETER;
361                 }
362         }
363
364         if (auth_flags)
365                 ntlmssp_handle_neg_flags(gensec_ntlmssp_state, auth_flags, gensec_ntlmssp_state->allow_lm_key);
366
367         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(gensec_ntlmssp_state, domain))) {
368                 /* zero this out */
369                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
370                 return nt_status;
371         }
372
373         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(gensec_ntlmssp_state, user))) {
374                 /* zero this out */
375                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
376                 return nt_status;
377         }
378
379         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(gensec_ntlmssp_state, workstation))) {
380                 /* zero this out */
381                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
382                 return nt_status;
383         }
384
385         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
386                  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));
387
388 #if 0
389         file_save("nthash1.dat",  &gensec_ntlmssp_state->nt_resp.data,  &gensec_ntlmssp_state->nt_resp.length);
390         file_save("lmhash1.dat",  &gensec_ntlmssp_state->lm_resp.data,  &gensec_ntlmssp_state->lm_resp.length);
391 #endif
392
393         /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a 
394            client challenge 
395         
396            However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
397         */
398         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
399                 if (gensec_ntlmssp_state->nt_resp.length == 24 && gensec_ntlmssp_state->lm_resp.length == 24) {
400                         struct MD5Context md5_session_nonce_ctx;
401                         SMB_ASSERT(gensec_ntlmssp_state->internal_chal.data 
402                                    && gensec_ntlmssp_state->internal_chal.length == 8);
403                         
404                         gensec_ntlmssp_state->doing_ntlm2 = True;
405
406                         memcpy(gensec_ntlmssp_state->crypt.ntlm2.session_nonce, gensec_ntlmssp_state->internal_chal.data, 8);
407                         memcpy(&gensec_ntlmssp_state->crypt.ntlm2.session_nonce[8], gensec_ntlmssp_state->lm_resp.data, 8);
408                         
409                         MD5Init(&md5_session_nonce_ctx);
410                         MD5Update(&md5_session_nonce_ctx, gensec_ntlmssp_state->crypt.ntlm2.session_nonce, 16);
411                         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
412                         
413                         gensec_ntlmssp_state->chal = data_blob_talloc(gensec_ntlmssp_state, 
414                                                                session_nonce_hash, 8);
415
416                         /* LM response is no longer useful, zero it out */
417                         data_blob_free(&gensec_ntlmssp_state->lm_resp);
418
419                         /* We changed the effective challenge - set it */
420                         if (!NT_STATUS_IS_OK(nt_status = 
421                                              gensec_ntlmssp_state->set_challenge(gensec_ntlmssp_state, 
422                                                                                  &gensec_ntlmssp_state->chal))) {
423                                 /* zero this out */
424                                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
425                                 return nt_status;
426                         }
427
428                         /* LM Key is incompatible... */
429                         gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
430                 }
431         }
432         return NT_STATUS_OK;
433 }
434
435 /**
436  * Next state function for the Authenticate packet 
437  * (after authentication - figures out the session keys etc)
438  * 
439  * @param gensec_ntlmssp_state NTLMSSP State
440  * @return Errors or NT_STATUS_OK. 
441  */
442
443 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security, 
444                                         DATA_BLOB *user_session_key, 
445                                         DATA_BLOB *lm_session_key) 
446 {
447         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
448         NTSTATUS nt_status;
449         DATA_BLOB session_key = data_blob(NULL, 0);
450
451         if (user_session_key)
452                 dump_data_pw("USER session key:\n", user_session_key->data, user_session_key->length);
453
454         if (lm_session_key) 
455                 dump_data_pw("LM first-8:\n", lm_session_key->data, lm_session_key->length);
456
457         /* Handle the different session key derivation for NTLM2 */
458         if (gensec_ntlmssp_state->doing_ntlm2) {
459                 if (user_session_key && user_session_key->data && user_session_key->length == 16) {
460                         session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
461                         hmac_md5(user_session_key->data, gensec_ntlmssp_state->crypt.ntlm2.session_nonce, 
462                                  sizeof(gensec_ntlmssp_state->crypt.ntlm2.session_nonce), session_key.data);
463                         DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
464                         dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
465                         
466                 } else {
467                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
468                         session_key = data_blob(NULL, 0);
469                 }
470         } else if ((gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
471                 /* Ensure we can never get here on NTLMv2 */
472                 && (gensec_ntlmssp_state->nt_resp.length == 0 || gensec_ntlmssp_state->nt_resp.length == 24)) {
473
474                 if (lm_session_key && lm_session_key->data && lm_session_key->length >= 8) {
475                         if (gensec_ntlmssp_state->lm_resp.data && gensec_ntlmssp_state->lm_resp.length == 24) {
476                                 session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
477                                 SMBsesskeygen_lm_sess_key(lm_session_key->data, gensec_ntlmssp_state->lm_resp.data, 
478                                                           session_key.data);
479                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
480                                 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
481                         } else {
482                                 
483                                 /* When there is no LM response, just use zeros */
484                                 static const uint8_t zeros[24];
485                                 session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
486                                 SMBsesskeygen_lm_sess_key(zeros, zeros, 
487                                                           session_key.data);
488                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
489                                 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
490                         }
491                 } else {
492                         /* LM Key not selected */
493                         gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
494
495                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
496                         session_key = data_blob(NULL, 0);
497                 }
498
499         } else if (user_session_key && user_session_key->data) {
500                 session_key = *user_session_key;
501                 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
502                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
503
504                 /* LM Key not selected */
505                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
506
507         } else if (lm_session_key && lm_session_key->data) {
508                 /* Very weird to have LM key, but no user session key, but anyway.. */
509                 session_key = *lm_session_key;
510                 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
511                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
512
513                 /* LM Key not selected */
514                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
515
516         } else {
517                 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
518                 session_key = data_blob(NULL, 0);
519
520                 /* LM Key not selected */
521                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
522         }
523
524         /* With KEY_EXCH, the client supplies the proposed session key, 
525            but encrypts it with the long-term key */
526         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
527                 if (!gensec_ntlmssp_state->encrypted_session_key.data 
528                     || gensec_ntlmssp_state->encrypted_session_key.length != 16) {
529                         data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
530                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n", 
531                                   (unsigned)gensec_ntlmssp_state->encrypted_session_key.length));
532                         return NT_STATUS_INVALID_PARAMETER;
533                 } else if (!session_key.data || session_key.length != 16) {
534                         DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n", 
535                                   (unsigned)session_key.length));
536                         gensec_ntlmssp_state->session_key = session_key;
537                 } else {
538                         dump_data_pw("KEY_EXCH session key (enc):\n", 
539                                      gensec_ntlmssp_state->encrypted_session_key.data, 
540                                      gensec_ntlmssp_state->encrypted_session_key.length);
541                         arcfour_crypt(gensec_ntlmssp_state->encrypted_session_key.data, 
542                                       session_key.data, 
543                                       gensec_ntlmssp_state->encrypted_session_key.length);
544                         gensec_ntlmssp_state->session_key = data_blob_talloc(gensec_ntlmssp_state, 
545                                                                       gensec_ntlmssp_state->encrypted_session_key.data, 
546                                                                       gensec_ntlmssp_state->encrypted_session_key.length);
547                         dump_data_pw("KEY_EXCH session key:\n", gensec_ntlmssp_state->encrypted_session_key.data, 
548                                      gensec_ntlmssp_state->encrypted_session_key.length);
549                 }
550         } else {
551                 gensec_ntlmssp_state->session_key = session_key;
552         }
553
554         /* The server might need us to use a partial-strength session key */
555         ntlmssp_weaken_keys(gensec_ntlmssp_state);
556
557         if ((gensec_security->want_features & GENSEC_FEATURE_SIGN)
558             || (gensec_security->want_features & GENSEC_FEATURE_SEAL)) {
559                 nt_status = ntlmssp_sign_init(gensec_ntlmssp_state);
560         } else {
561                 nt_status = NT_STATUS_OK;
562         }
563
564         data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
565         
566         /* allow arbitarily many authentications, but watch that this will cause a 
567            memory leak, until the gensec_ntlmssp_state is shutdown 
568         */
569
570         if (gensec_ntlmssp_state->server_multiple_authentications) {
571                 gensec_ntlmssp_state->expected_state = NTLMSSP_AUTH;
572         } else {
573                 gensec_ntlmssp_state->expected_state = NTLMSSP_DONE;
574         }
575
576         return nt_status;
577 }
578
579
580 /**
581  * Next state function for the Authenticate packet
582  * 
583  * @param gensec_security GENSEC state
584  * @param out_mem_ctx Memory context for *out
585  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
586  * @param out The reply, as an allocated DATA_BLOB, caller to free.
587  * @return Errors or NT_STATUS_OK if authentication sucessful
588  */
589
590 NTSTATUS ntlmssp_server_auth(struct gensec_security *gensec_security, 
591                              TALLOC_CTX *out_mem_ctx, 
592                              const DATA_BLOB in, DATA_BLOB *out) 
593 {       
594         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
595         DATA_BLOB user_session_key = data_blob(NULL, 0);
596         DATA_BLOB lm_session_key = data_blob(NULL, 0);
597         NTSTATUS nt_status;
598
599         /* zero the outbound NTLMSSP packet */
600         *out = data_blob_talloc(out_mem_ctx, NULL, 0);
601
602         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_server_preauth(gensec_ntlmssp_state, in))) {
603                 return nt_status;
604         }
605
606         /*
607          * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
608          * is required (by "ntlm auth = no" and "lm auth = no" being set in the
609          * smb.conf file) and no NTLMv2 response was sent then the password check
610          * will fail here. JRA.
611          */
612
613         /* Finally, actually ask if the password is OK */
614
615         if (!NT_STATUS_IS_OK(nt_status = gensec_ntlmssp_state->check_password(gensec_ntlmssp_state, 
616                                                                               &user_session_key, &lm_session_key))) {
617                 return nt_status;
618         }
619         
620         if (gensec_security->want_features
621             & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL|GENSEC_FEATURE_SESSION_KEY)) {
622                 return ntlmssp_server_postauth(gensec_security, &user_session_key, &lm_session_key);
623         } else {
624                 gensec_ntlmssp_state->session_key = data_blob(NULL, 0);
625                 return NT_STATUS_OK;
626         }
627 }
628
629 /**
630  * Return the challenge as determined by the authentication subsystem 
631  * @return an 8 byte random challenge
632  */
633
634 static const uint8_t *auth_ntlmssp_get_challenge(const struct gensec_ntlmssp_state *gensec_ntlmssp_state)
635 {
636         NTSTATUS status;
637         const uint8_t *chal;
638
639         status = auth_get_challenge(gensec_ntlmssp_state->auth_context, &chal);
640         if (!NT_STATUS_IS_OK(status)) {
641                 return NULL;
642         }
643
644         return chal;
645 }
646
647 /**
648  * Some authentication methods 'fix' the challenge, so we may not be able to set it
649  *
650  * @return If the effective challenge used by the auth subsystem may be modified
651  */
652 static BOOL auth_ntlmssp_may_set_challenge(const struct gensec_ntlmssp_state *gensec_ntlmssp_state)
653 {
654         return auth_challenge_may_be_modified(gensec_ntlmssp_state->auth_context);
655 }
656
657 /**
658  * NTLM2 authentication modifies the effective challenge, 
659  * @param challenge The new challenge value
660  */
661 static NTSTATUS auth_ntlmssp_set_challenge(struct gensec_ntlmssp_state *gensec_ntlmssp_state, DATA_BLOB *challenge)
662 {
663         NTSTATUS nt_status;
664         struct auth_context *auth_context = gensec_ntlmssp_state->auth_context;
665         const uint8_t *chal;
666
667         if (challenge->length != 8) {
668                 return NT_STATUS_INVALID_PARAMETER;
669         }
670
671         chal = challenge->data;
672
673         nt_status = auth_context_set_challenge(auth_context, chal, "NTLMSSP callback (NTLM2)");
674
675         return nt_status;
676 }
677
678 /**
679  * Check the password on an NTLMSSP login.  
680  *
681  * Return the session keys used on the connection.
682  */
683
684 static NTSTATUS auth_ntlmssp_check_password(struct gensec_ntlmssp_state *gensec_ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key) 
685 {
686         NTSTATUS nt_status;
687         struct auth_usersupplied_info *user_info = talloc(gensec_ntlmssp_state, struct auth_usersupplied_info);
688         if (!user_info) {
689                 return NT_STATUS_NO_MEMORY;
690         }
691
692         user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
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