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