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