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