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