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