s4:auth Change auth_generate_session_info to take an auth context
[gd/samba-autobuild/.git] / source4 / auth / ntlmssp / ntlmssp_server.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, client server side parsing
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8    Copyright (C) Stefan Metzmacher 2005
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "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/gensec/gensec_proto.h"
32 #include "auth/auth.h"
33 #include "param/param.h"
34
35 /** 
36  * Set a username on an NTLMSSP context - ensures it is talloc()ed 
37  *
38  */
39
40 static NTSTATUS ntlmssp_set_username(struct ntlmssp_state *ntlmssp_state, const char *user)
41 {
42         if (!user) {
43                 /* it should be at least "" */
44                 DEBUG(1, ("NTLMSSP failed to set username - cannot accept NULL username\n"));
45                 return NT_STATUS_INVALID_PARAMETER;
46         }
47         ntlmssp_state->user = talloc_strdup(ntlmssp_state, user);
48         if (!ntlmssp_state->user) {
49                 return NT_STATUS_NO_MEMORY;
50         }
51         return NT_STATUS_OK;
52 }
53
54 /** 
55  * Set a domain on an NTLMSSP context - ensures it is talloc()ed 
56  *
57  */
58 static NTSTATUS ntlmssp_set_domain(struct ntlmssp_state *ntlmssp_state, const char *domain)
59 {
60         ntlmssp_state->domain = talloc_strdup(ntlmssp_state, domain);
61         if (!ntlmssp_state->domain) {
62                 return NT_STATUS_NO_MEMORY;
63         }
64         return NT_STATUS_OK;
65 }
66
67 /** 
68  * Set a workstation on an NTLMSSP context - ensures it is talloc()ed 
69  *
70  */
71 static NTSTATUS ntlmssp_set_workstation(struct ntlmssp_state *ntlmssp_state, const char *workstation)
72 {
73         ntlmssp_state->workstation = talloc_strdup(ntlmssp_state, workstation);
74         if (!ntlmssp_state->workstation) {
75                 return NT_STATUS_NO_MEMORY;
76         }
77         return NT_STATUS_OK;
78 }
79
80 /**
81  * Determine correct target name flags for reply, given server role 
82  * and negotiated flags
83  * 
84  * @param ntlmssp_state NTLMSSP State
85  * @param neg_flags The flags from the packet
86  * @param chal_flags The flags to be set in the reply packet
87  * @return The 'target name' string.
88  */
89
90 static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
91                                        uint32_t neg_flags, uint32_t *chal_flags) 
92 {
93         if (neg_flags & NTLMSSP_REQUEST_TARGET) {
94                 *chal_flags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
95                 *chal_flags |= NTLMSSP_REQUEST_TARGET;
96                 if (ntlmssp_state->server.is_standalone) {
97                         *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
98                         return ntlmssp_state->server.netbios_name;
99                 } else {
100                         *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
101                         return ntlmssp_state->server.netbios_domain;
102                 };
103         } else {
104                 return "";
105         }
106 }
107
108
109
110 /**
111  * Next state function for the Negotiate packet
112  * 
113  * @param gensec_security GENSEC state
114  * @param out_mem_ctx Memory context for *out
115  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
116  * @param out The reply, as an allocated DATA_BLOB, caller to free.
117  * @return Errors or MORE_PROCESSING_REQUIRED if (normal) a reply is required. 
118  */
119
120 NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security, 
121                                   TALLOC_CTX *out_mem_ctx, 
122                                   const DATA_BLOB in, DATA_BLOB *out) 
123 {
124         struct gensec_ntlmssp_context *gensec_ntlmssp =
125                 talloc_get_type_abort(gensec_security->private_data,
126                                       struct gensec_ntlmssp_context);
127         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
128         DATA_BLOB struct_blob;
129         uint32_t neg_flags = 0;
130         uint32_t ntlmssp_command, chal_flags;
131         uint8_t cryptkey[8];
132         const char *target_name;
133         NTSTATUS status;
134
135         /* parse the NTLMSSP packet */
136 #if 0
137         file_save("ntlmssp_negotiate.dat", request.data, request.length);
138 #endif
139
140         if (in.length) {
141                 if ((in.length < 16) || !msrpc_parse(out_mem_ctx, 
142                                                          &in, "Cdd",
143                                                          "NTLMSSP",
144                                                          &ntlmssp_command,
145                                                          &neg_flags)) {
146                         DEBUG(1, ("ntlmssp_server_negotiate: failed to parse "
147                                 "NTLMSSP Negotiate of length %u:\n",
148                                 (unsigned int)in.length ));
149                         dump_data(2, in.data, in.length);
150                         return NT_STATUS_INVALID_PARAMETER;
151                 }
152                 debug_ntlmssp_flags(neg_flags);
153         }
154         
155         ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, ntlmssp_state->allow_lm_key);
156
157         /* Ask our caller what challenge they would like in the packet */
158         status = ntlmssp_state->get_challenge(ntlmssp_state, cryptkey);
159         if (!NT_STATUS_IS_OK(status)) {
160                 DEBUG(1, ("ntlmssp_server_negotiate: backend doesn't give a challenge: %s\n",
161                           nt_errstr(status)));
162                 return status;
163         }
164
165         /* Check if we may set the challenge */
166         if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
167                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
168         }
169
170         /* The flags we send back are not just the negotiated flags,
171          * they are also 'what is in this packet'.  Therfore, we
172          * operate on 'chal_flags' from here on 
173          */
174
175         chal_flags = ntlmssp_state->neg_flags;
176
177         /* get the right name to fill in as 'target' */
178         target_name = ntlmssp_target_name(ntlmssp_state,
179                                           neg_flags, &chal_flags); 
180         if (target_name == NULL) 
181                 return NT_STATUS_INVALID_PARAMETER;
182
183         ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
184         ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
185
186         /* This creates the 'blob' of names that appears at the end of the packet */
187         if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
188                 msrpc_gen(out_mem_ctx, 
189                           &struct_blob, "aaaaa",
190                           MsvAvNbDomainName, target_name,
191                           MsvAvNbComputerName, ntlmssp_state->server.netbios_name,
192                           MsvAvDnsDomainName, ntlmssp_state->server.dns_domain,
193                           MsvAvDnsComputerName, ntlmssp_state->server.dns_name,
194                           MsvAvEOL, "");
195         } else {
196                 struct_blob = data_blob(NULL, 0);
197         }
198
199         {
200                 /* Marshal the packet in the right format, be it unicode or ASCII */
201                 const char *gen_string;
202                 if (ntlmssp_state->unicode) {
203                         gen_string = "CdUdbddB";
204                 } else {
205                         gen_string = "CdAdbddB";
206                 }
207                 
208                 msrpc_gen(out_mem_ctx, 
209                           out, gen_string,
210                           "NTLMSSP", 
211                           NTLMSSP_CHALLENGE,
212                           target_name,
213                           chal_flags,
214                           cryptkey, 8,
215                           0, 0,
216                           struct_blob.data, struct_blob.length);
217         }
218                 
219         ntlmssp_state->expected_state = NTLMSSP_AUTH;
220
221         return NT_STATUS_MORE_PROCESSING_REQUIRED;
222 }
223
224 struct ntlmssp_server_auth_state {
225         DATA_BLOB user_session_key;
226         DATA_BLOB lm_session_key;
227         /* internal variables used by KEY_EXCH (client-supplied user session key */
228         DATA_BLOB encrypted_session_key;
229         bool doing_ntlm2;
230         /* internal variables used by NTLM2 */
231         uint8_t session_nonce[16];
232 };
233
234 /**
235  * Next state function for the Authenticate packet
236  * 
237  * @param 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 ntlmssp_state *ntlmssp_state,
243                                        struct ntlmssp_server_auth_state *state,
244                                        const DATA_BLOB request) 
245 {
246         uint32_t ntlmssp_command, auth_flags;
247         NTSTATUS nt_status;
248
249         uint8_t session_nonce_hash[16];
250
251         const char *parse_string;
252         char *domain = NULL;
253         char *user = NULL;
254         char *workstation = NULL;
255
256 #if 0
257         file_save("ntlmssp_auth.dat", request.data, request.length);
258 #endif
259
260         if (ntlmssp_state->unicode) {
261                 parse_string = "CdBBUUUBd";
262         } else {
263                 parse_string = "CdBBAAABd";
264         }
265
266         /* zero these out */
267         data_blob_free(&ntlmssp_state->session_key);
268         data_blob_free(&ntlmssp_state->lm_resp);
269         data_blob_free(&ntlmssp_state->nt_resp);
270
271         ntlmssp_state->user = NULL;
272         ntlmssp_state->domain = NULL;
273         ntlmssp_state->workstation = NULL;
274
275         /* now the NTLMSSP encoded auth hashes */
276         if (!msrpc_parse(ntlmssp_state,
277                          &request, parse_string,
278                          "NTLMSSP", 
279                          &ntlmssp_command, 
280                          &ntlmssp_state->lm_resp,
281                          &ntlmssp_state->nt_resp,
282                          &domain, 
283                          &user, 
284                          &workstation,
285                          &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(&state->encrypted_session_key);
292                 auth_flags = 0;
293                 
294                 /* Try again with a shorter string (Win9X truncates this packet) */
295                 if (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(ntlmssp_state,
303                                  &request, parse_string,
304                                  "NTLMSSP", 
305                                  &ntlmssp_command, 
306                                  &ntlmssp_state->lm_resp,
307                                  &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         talloc_steal(state, state->encrypted_session_key.data);
319
320         if (auth_flags)
321                 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, ntlmssp_state->allow_lm_key);
322
323         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(ntlmssp_state, domain))) {
324                 return nt_status;
325         }
326
327         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(ntlmssp_state, user))) {
328                 return nt_status;
329         }
330
331         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(ntlmssp_state, workstation))) {
332                 return nt_status;
333         }
334
335         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
336                  ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
337
338 #if 0
339         file_save("nthash1.dat",  &ntlmssp_state->nt_resp.data,  &ntlmssp_state->nt_resp.length);
340         file_save("lmhash1.dat",  &ntlmssp_state->lm_resp.data,  &ntlmssp_state->lm_resp.length);
341 #endif
342
343         /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a 
344            client challenge 
345         
346            However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
347         */
348         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
349                 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
350                         struct MD5Context md5_session_nonce_ctx;
351                         SMB_ASSERT(ntlmssp_state->internal_chal.data
352                                    && ntlmssp_state->internal_chal.length == 8);
353                         
354                         state->doing_ntlm2 = true;
355
356                         memcpy(state->session_nonce, ntlmssp_state->internal_chal.data, 8);
357                         memcpy(&state->session_nonce[8], ntlmssp_state->lm_resp.data, 8);
358                         
359                         MD5Init(&md5_session_nonce_ctx);
360                         MD5Update(&md5_session_nonce_ctx, state->session_nonce, 16);
361                         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
362                         
363                         ntlmssp_state->chal = data_blob_talloc(ntlmssp_state,
364                                                                session_nonce_hash, 8);
365
366                         /* LM response is no longer useful, zero it out */
367                         data_blob_free(&ntlmssp_state->lm_resp);
368
369                         /* We changed the effective challenge - set it */
370                         if (!NT_STATUS_IS_OK(nt_status = 
371                                              ntlmssp_state->set_challenge(ntlmssp_state,
372                                                                                  &ntlmssp_state->chal))) {
373                                 return nt_status;
374                         }
375
376                         /* LM Key is incompatible... */
377                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
378                 }
379         }
380         return NT_STATUS_OK;
381 }
382
383 /**
384  * Next state function for the Authenticate packet 
385  * (after authentication - figures out the session keys etc)
386  * 
387  * @param ntlmssp_state NTLMSSP State
388  * @return Errors or NT_STATUS_OK. 
389  */
390
391 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security, 
392                                         struct ntlmssp_server_auth_state *state)
393 {
394         struct gensec_ntlmssp_context *gensec_ntlmssp =
395                 talloc_get_type_abort(gensec_security->private_data,
396                                       struct gensec_ntlmssp_context);
397         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
398         DATA_BLOB *user_session_key = &state->user_session_key;
399         DATA_BLOB *lm_session_key = &state->lm_session_key;
400         NTSTATUS nt_status;
401         DATA_BLOB session_key = data_blob(NULL, 0);
402
403         if (!(gensec_security->want_features
404               & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL|GENSEC_FEATURE_SESSION_KEY))) {
405                 return NT_STATUS_OK;
406         }
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 (state->doing_ntlm2) {
416                 if (user_session_key && user_session_key->data && user_session_key->length == 16) {
417                         session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
418                         hmac_md5(user_session_key->data, state->session_nonce,
419                                  sizeof(state->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 ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
428                 /* Ensure we can never get here on NTLMv2 */
429                 && (ntlmssp_state->nt_resp.length == 0 || ntlmssp_state->nt_resp.length == 24)) {
430
431                 if (lm_session_key && lm_session_key->data && lm_session_key->length >= 8) {
432                         if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
433                                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
434                                 SMBsesskeygen_lm_sess_key(lm_session_key->data, 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(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                         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 = data_blob_talloc(ntlmssp_state, user_session_key->data, user_session_key->length);
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                 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 = data_blob_talloc(ntlmssp_state, lm_session_key->data, lm_session_key->length);
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                 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                 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 (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
484                 if (!state->encrypted_session_key.data
485                     || state->encrypted_session_key.length != 16) {
486                         data_blob_free(&state->encrypted_session_key);
487                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n", 
488                                   (unsigned)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                         ntlmssp_state->session_key = session_key;
494                 } else {
495                         dump_data_pw("KEY_EXCH session key (enc):\n", 
496                                      state->encrypted_session_key.data,
497                                      state->encrypted_session_key.length);
498                         arcfour_crypt(state->encrypted_session_key.data,
499                                       session_key.data, 
500                                       state->encrypted_session_key.length);
501                         ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state,
502                                                                       state->encrypted_session_key.data,
503                                                                       state->encrypted_session_key.length);
504                         dump_data_pw("KEY_EXCH session key:\n",
505                                      state->encrypted_session_key.data,
506                                      state->encrypted_session_key.length);
507                         talloc_free(session_key.data);
508                 }
509         } else {
510                 ntlmssp_state->session_key = session_key;
511         }
512
513         if ((gensec_security->want_features & GENSEC_FEATURE_SIGN)
514             || (gensec_security->want_features & GENSEC_FEATURE_SEAL)) {
515                 nt_status = ntlmssp_sign_init(ntlmssp_state);
516         } else {
517                 nt_status = NT_STATUS_OK;
518         }
519
520         ntlmssp_state->expected_state = NTLMSSP_DONE;
521
522         return nt_status;
523 }
524
525
526 /**
527  * Next state function for the Authenticate packet
528  * 
529  * @param gensec_security GENSEC state
530  * @param out_mem_ctx Memory context for *out
531  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
532  * @param out The reply, as an allocated DATA_BLOB, caller to free.
533  * @return Errors or NT_STATUS_OK if authentication sucessful
534  */
535
536 NTSTATUS ntlmssp_server_auth(struct gensec_security *gensec_security, 
537                              TALLOC_CTX *out_mem_ctx, 
538                              const DATA_BLOB in, DATA_BLOB *out) 
539 {       
540         struct gensec_ntlmssp_context *gensec_ntlmssp =
541                 talloc_get_type_abort(gensec_security->private_data,
542                                       struct gensec_ntlmssp_context);
543         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
544         struct ntlmssp_server_auth_state *state;
545         NTSTATUS nt_status;
546
547         /* zero the outbound NTLMSSP packet */
548         *out = data_blob_null;
549
550         state = talloc_zero(ntlmssp_state, struct ntlmssp_server_auth_state);
551         if (state == NULL) {
552                 return NT_STATUS_NO_MEMORY;
553         }
554
555         nt_status = ntlmssp_server_preauth(ntlmssp_state, state, in);
556         if (!NT_STATUS_IS_OK(nt_status)) {
557                 TALLOC_FREE(state);
558                 return nt_status;
559         }
560
561         /*
562          * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
563          * is required (by "ntlm auth = no" and "lm auth = no" being set in the
564          * smb.conf file) and no NTLMv2 response was sent then the password check
565          * will fail here. JRA.
566          */
567
568         /* Finally, actually ask if the password is OK */
569         nt_status = ntlmssp_state->check_password(ntlmssp_state,
570                                                   &state->user_session_key,
571                                                   &state->lm_session_key);
572         if (!NT_STATUS_IS_OK(nt_status)) {
573                 TALLOC_FREE(state);
574                 return nt_status;
575         }
576
577         nt_status = ntlmssp_server_postauth(gensec_security, state);
578         if (!NT_STATUS_IS_OK(nt_status)) {
579                 TALLOC_FREE(state);
580                 return nt_status;
581         }
582
583         TALLOC_FREE(state);
584         return NT_STATUS_OK;
585 }
586
587 /**
588  * Return the challenge as determined by the authentication subsystem 
589  * @return an 8 byte random challenge
590  */
591
592 static NTSTATUS auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state,
593                                            uint8_t chal[8])
594 {
595         struct gensec_ntlmssp_context *gensec_ntlmssp =
596                 talloc_get_type_abort(ntlmssp_state->callback_private,
597                                       struct gensec_ntlmssp_context);
598         struct auth_context *auth_context = gensec_ntlmssp->auth_context;
599         NTSTATUS status;
600
601         status = auth_context->get_challenge(auth_context, chal);
602         if (!NT_STATUS_IS_OK(status)) {
603                 DEBUG(1, ("auth_ntlmssp_get_challenge: failed to get challenge: %s\n",
604                         nt_errstr(status)));
605                 return status;
606         }
607
608         return NT_STATUS_OK;
609 }
610
611 /**
612  * Some authentication methods 'fix' the challenge, so we may not be able to set it
613  *
614  * @return If the effective challenge used by the auth subsystem may be modified
615  */
616 static bool auth_ntlmssp_may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
617 {
618         struct gensec_ntlmssp_context *gensec_ntlmssp =
619                 talloc_get_type_abort(ntlmssp_state->callback_private,
620                                       struct gensec_ntlmssp_context);
621         struct auth_context *auth_context = gensec_ntlmssp->auth_context;
622
623         return auth_context->challenge_may_be_modified(auth_context);
624 }
625
626 /**
627  * NTLM2 authentication modifies the effective challenge, 
628  * @param challenge The new challenge value
629  */
630 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
631 {
632         struct gensec_ntlmssp_context *gensec_ntlmssp =
633                 talloc_get_type_abort(ntlmssp_state->callback_private,
634                                       struct gensec_ntlmssp_context);
635         struct auth_context *auth_context = gensec_ntlmssp->auth_context;
636         NTSTATUS nt_status;
637         const uint8_t *chal;
638
639         if (challenge->length != 8) {
640                 return NT_STATUS_INVALID_PARAMETER;
641         }
642
643         chal = challenge->data;
644
645         nt_status = auth_context->set_challenge(auth_context,
646                                                 chal,
647                                                 "NTLMSSP callback (NTLM2)");
648
649         return nt_status;
650 }
651
652 /**
653  * Check the password on an NTLMSSP login.  
654  *
655  * Return the session keys used on the connection.
656  */
657
658 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state,
659                                             DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
660 {
661         struct gensec_ntlmssp_context *gensec_ntlmssp =
662                 talloc_get_type_abort(ntlmssp_state->callback_private,
663                                       struct gensec_ntlmssp_context);
664         struct auth_context *auth_context = gensec_ntlmssp->auth_context;
665         NTSTATUS nt_status;
666         struct auth_usersupplied_info *user_info;
667
668         user_info = talloc(ntlmssp_state, struct auth_usersupplied_info);
669         if (!user_info) {
670                 return NT_STATUS_NO_MEMORY;
671         }
672
673         user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
674         user_info->flags = 0;
675         user_info->mapped_state = false;
676         user_info->client.account_name = ntlmssp_state->user;
677         user_info->client.domain_name = ntlmssp_state->domain;
678         user_info->workstation_name = ntlmssp_state->workstation;
679         user_info->remote_host = gensec_get_remote_address(gensec_ntlmssp->gensec_security);
680
681         user_info->password_state = AUTH_PASSWORD_RESPONSE;
682         user_info->password.response.lanman = ntlmssp_state->lm_resp;
683         user_info->password.response.lanman.data = talloc_steal(user_info, ntlmssp_state->lm_resp.data);
684         user_info->password.response.nt = ntlmssp_state->nt_resp;
685         user_info->password.response.nt.data = talloc_steal(user_info, ntlmssp_state->nt_resp.data);
686
687         nt_status = auth_context->check_password(auth_context,
688                                                  gensec_ntlmssp,
689                                                  user_info,
690                                                  &gensec_ntlmssp->server_info);
691         talloc_free(user_info);
692         NT_STATUS_NOT_OK_RETURN(nt_status);
693
694         if (gensec_ntlmssp->server_info->user_session_key.length) {
695                 DEBUG(10, ("Got NT session key of length %u\n",
696                            (unsigned)gensec_ntlmssp->server_info->user_session_key.length));
697                 *user_session_key = gensec_ntlmssp->server_info->user_session_key;
698         }
699         if (gensec_ntlmssp->server_info->lm_session_key.length) {
700                 DEBUG(10, ("Got LM session key of length %u\n",
701                            (unsigned)gensec_ntlmssp->server_info->lm_session_key.length));
702                 *lm_session_key = gensec_ntlmssp->server_info->lm_session_key;
703         }
704         return nt_status;
705 }
706
707 /** 
708  * Return the credentials of a logged on user, including session keys
709  * etc.
710  *
711  * Only valid after a successful authentication
712  *
713  * May only be called once per authentication.
714  *
715  */
716
717 NTSTATUS gensec_ntlmssp_session_info(struct gensec_security *gensec_security,
718                                      struct auth_session_info **session_info) 
719 {
720         NTSTATUS nt_status;
721         struct gensec_ntlmssp_context *gensec_ntlmssp =
722                 talloc_get_type_abort(gensec_security->private_data,
723                                       struct gensec_ntlmssp_context);
724         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
725
726         nt_status = gensec_generate_session_info(ntlmssp_state,
727                                                  gensec_security,
728                                                  gensec_ntlmssp->server_info,
729                                                  session_info);
730         NT_STATUS_NOT_OK_RETURN(nt_status);
731
732         (*session_info)->session_key = data_blob_talloc(*session_info, 
733                                                         ntlmssp_state->session_key.data,
734                                                         ntlmssp_state->session_key.length);
735
736         return NT_STATUS_OK;
737 }
738
739 /**
740  * Start NTLMSSP on the server side 
741  *
742  */
743 NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
744 {
745         NTSTATUS nt_status;
746         struct ntlmssp_state *ntlmssp_state;
747         struct gensec_ntlmssp_context *gensec_ntlmssp;
748
749         nt_status = gensec_ntlmssp_start(gensec_security);
750         NT_STATUS_NOT_OK_RETURN(nt_status);
751
752         gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
753                                                struct gensec_ntlmssp_context);
754         ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
755
756         ntlmssp_state->role = NTLMSSP_SERVER;
757
758         ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
759
760         ntlmssp_state->allow_lm_key = (lp_lanman_auth(gensec_security->settings->lp_ctx)
761                                           && gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "allow_lm_key", false));
762
763         ntlmssp_state->neg_flags =
764                 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_VERSION;
765
766         ntlmssp_state->lm_resp = data_blob(NULL, 0);
767         ntlmssp_state->nt_resp = data_blob(NULL, 0);
768
769         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "128bit", true)) {
770                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
771         }
772
773         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "56bit", true)) {
774                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
775         }
776
777         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "keyexchange", true)) {
778                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
779         }
780
781         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "alwayssign", true)) {
782                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
783         }
784
785         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "ntlm2", true)) {
786                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
787         }
788
789         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
790                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
791         }
792         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
793                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
794         }
795
796         gensec_ntlmssp->auth_context = gensec_security->auth_context;
797
798         ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
799         ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
800         ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
801         ntlmssp_state->check_password = auth_ntlmssp_check_password;
802         if (lp_server_role(gensec_security->settings->lp_ctx) == ROLE_STANDALONE) {
803                 ntlmssp_state->server.is_standalone = true;
804         } else {
805                 ntlmssp_state->server.is_standalone = false;
806         }
807
808         ntlmssp_state->server.netbios_name = lp_netbios_name(gensec_security->settings->lp_ctx);
809
810         ntlmssp_state->server.netbios_domain = lp_workgroup(gensec_security->settings->lp_ctx);
811
812         {
813                 char dnsdomname[MAXHOSTNAMELEN], dnsname[MAXHOSTNAMELEN];
814
815                 /* Find out the DNS domain name */
816                 dnsdomname[0] = '\0';
817                 safe_strcpy(dnsdomname, lp_dnsdomain(gensec_security->settings->lp_ctx), sizeof(dnsdomname) - 1);
818
819                 /* Find out the DNS host name */
820                 safe_strcpy(dnsname, ntlmssp_state->server.netbios_name, sizeof(dnsname) - 1);
821                 if (dnsdomname[0] != '\0') {
822                         safe_strcat(dnsname, ".", sizeof(dnsname) - 1);
823                         safe_strcat(dnsname, dnsdomname, sizeof(dnsname) - 1);
824                 }
825                 strlower_m(dnsname);
826
827                 ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state,
828                                                                       dnsname);
829                 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_name);
830
831                 ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state,
832                                                                         dnsdomname);
833                 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_domain);
834         }
835
836         return NT_STATUS_OK;
837 }
838