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