s4:ntlmssp: move encrypted_session_key to ntlmssp_server_auth_state
[nivanova/samba.git] / source4 / auth / ntlmssp / ntlmssp_server.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, client server side parsing
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8    Copyright (C) Stefan Metzmacher 2005
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "system/network.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #include "../lib/crypto/crypto.h"
30 #include "auth/gensec/gensec.h"
31 #include "auth/auth.h"
32 #include "param/param.h"
33
34 /** 
35  * Set a username on an NTLMSSP context - ensures it is talloc()ed 
36  *
37  */
38
39 static NTSTATUS ntlmssp_set_username(struct 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 };
229
230 /**
231  * Next state function for the Authenticate packet
232  * 
233  * @param ntlmssp_state NTLMSSP State
234  * @param request The request, as a DATA_BLOB
235  * @return Errors or NT_STATUS_OK. 
236  */
237
238 static NTSTATUS ntlmssp_server_preauth(struct ntlmssp_state *ntlmssp_state,
239                                        struct ntlmssp_server_auth_state *state,
240                                        const DATA_BLOB request) 
241 {
242         uint32_t ntlmssp_command, auth_flags;
243         NTSTATUS nt_status;
244
245         uint8_t session_nonce_hash[16];
246
247         const char *parse_string;
248         char *domain = NULL;
249         char *user = NULL;
250         char *workstation = NULL;
251
252 #if 0
253         file_save("ntlmssp_auth.dat", request.data, request.length);
254 #endif
255
256         if (ntlmssp_state->unicode) {
257                 parse_string = "CdBBUUUBd";
258         } else {
259                 parse_string = "CdBBAAABd";
260         }
261
262         /* zero these out */
263         data_blob_free(&ntlmssp_state->session_key);
264         data_blob_free(&ntlmssp_state->lm_resp);
265         data_blob_free(&ntlmssp_state->nt_resp);
266
267         ntlmssp_state->user = NULL;
268         ntlmssp_state->domain = NULL;
269         ntlmssp_state->workstation = NULL;
270
271         /* now the NTLMSSP encoded auth hashes */
272         if (!msrpc_parse(ntlmssp_state,
273                          &request, parse_string,
274                          "NTLMSSP", 
275                          &ntlmssp_command, 
276                          &ntlmssp_state->lm_resp,
277                          &ntlmssp_state->nt_resp,
278                          &domain, 
279                          &user, 
280                          &workstation,
281                          &state->encrypted_session_key,
282                          &auth_flags)) {
283                 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
284                 dump_data(10, request.data, request.length);
285
286                 /* zero this out */
287                 data_blob_free(&state->encrypted_session_key);
288                 auth_flags = 0;
289                 
290                 /* Try again with a shorter string (Win9X truncates this packet) */
291                 if (ntlmssp_state->unicode) {
292                         parse_string = "CdBBUUU";
293                 } else {
294                         parse_string = "CdBBAAA";
295                 }
296
297                 /* now the NTLMSSP encoded auth hashes */
298                 if (!msrpc_parse(ntlmssp_state,
299                                  &request, parse_string,
300                                  "NTLMSSP", 
301                                  &ntlmssp_command, 
302                                  &ntlmssp_state->lm_resp,
303                                  &ntlmssp_state->nt_resp,
304                                  &domain, 
305                                  &user, 
306                                  &workstation)) {
307                         DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP:\n"));
308                         dump_data(2, request.data, request.length);
309
310                         return NT_STATUS_INVALID_PARAMETER;
311                 }
312         }
313
314         talloc_steal(state, state->encrypted_session_key.data);
315
316         if (auth_flags)
317                 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, ntlmssp_state->allow_lm_key);
318
319         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(ntlmssp_state, domain))) {
320                 return nt_status;
321         }
322
323         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(ntlmssp_state, user))) {
324                 return nt_status;
325         }
326
327         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(ntlmssp_state, workstation))) {
328                 return nt_status;
329         }
330
331         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
332                  ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
333
334 #if 0
335         file_save("nthash1.dat",  &ntlmssp_state->nt_resp.data,  &ntlmssp_state->nt_resp.length);
336         file_save("lmhash1.dat",  &ntlmssp_state->lm_resp.data,  &ntlmssp_state->lm_resp.length);
337 #endif
338
339         /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a 
340            client challenge 
341         
342            However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
343         */
344         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
345                 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
346                         struct MD5Context md5_session_nonce_ctx;
347                         SMB_ASSERT(ntlmssp_state->internal_chal.data
348                                    && ntlmssp_state->internal_chal.length == 8);
349                         
350                         ntlmssp_state->doing_ntlm2 = true;
351
352                         memcpy(ntlmssp_state->crypt.ntlm2.session_nonce, ntlmssp_state->internal_chal.data, 8);
353                         memcpy(&ntlmssp_state->crypt.ntlm2.session_nonce[8], ntlmssp_state->lm_resp.data, 8);
354                         
355                         MD5Init(&md5_session_nonce_ctx);
356                         MD5Update(&md5_session_nonce_ctx, ntlmssp_state->crypt.ntlm2.session_nonce, 16);
357                         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
358                         
359                         ntlmssp_state->chal = data_blob_talloc(ntlmssp_state,
360                                                                session_nonce_hash, 8);
361
362                         /* LM response is no longer useful, zero it out */
363                         data_blob_free(&ntlmssp_state->lm_resp);
364
365                         /* We changed the effective challenge - set it */
366                         if (!NT_STATUS_IS_OK(nt_status = 
367                                              ntlmssp_state->set_challenge(ntlmssp_state,
368                                                                                  &ntlmssp_state->chal))) {
369                                 return nt_status;
370                         }
371
372                         /* LM Key is incompatible... */
373                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
374                 }
375         }
376         return NT_STATUS_OK;
377 }
378
379 /**
380  * Next state function for the Authenticate packet 
381  * (after authentication - figures out the session keys etc)
382  * 
383  * @param ntlmssp_state NTLMSSP State
384  * @return Errors or NT_STATUS_OK. 
385  */
386
387 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security, 
388                                         struct ntlmssp_server_auth_state *state)
389 {
390         struct gensec_ntlmssp_context *gensec_ntlmssp =
391                 talloc_get_type_abort(gensec_security->private_data,
392                                       struct gensec_ntlmssp_context);
393         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
394         DATA_BLOB *user_session_key = &state->user_session_key;
395         DATA_BLOB *lm_session_key = &state->lm_session_key;
396         NTSTATUS nt_status;
397         DATA_BLOB session_key = data_blob(NULL, 0);
398
399         if (!(gensec_security->want_features
400               & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL|GENSEC_FEATURE_SESSION_KEY))) {
401                 return NT_STATUS_OK;
402         }
403
404         if (user_session_key)
405                 dump_data_pw("USER session key:\n", user_session_key->data, user_session_key->length);
406
407         if (lm_session_key) 
408                 dump_data_pw("LM first-8:\n", lm_session_key->data, lm_session_key->length);
409
410         /* Handle the different session key derivation for NTLM2 */
411         if (ntlmssp_state->doing_ntlm2) {
412                 if (user_session_key && user_session_key->data && user_session_key->length == 16) {
413                         session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
414                         hmac_md5(user_session_key->data, ntlmssp_state->crypt.ntlm2.session_nonce,
415                                  sizeof(ntlmssp_state->crypt.ntlm2.session_nonce), session_key.data);
416                         DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
417                         dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
418                         
419                 } else {
420                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
421                         session_key = data_blob(NULL, 0);
422                 }
423         } else if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
424                 /* Ensure we can never get here on NTLMv2 */
425                 && (ntlmssp_state->nt_resp.length == 0 || ntlmssp_state->nt_resp.length == 24)) {
426
427                 if (lm_session_key && lm_session_key->data && lm_session_key->length >= 8) {
428                         if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
429                                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
430                                 SMBsesskeygen_lm_sess_key(lm_session_key->data, ntlmssp_state->lm_resp.data,
431                                                           session_key.data);
432                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
433                                 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
434                         } else {
435                                 
436                                 /* When there is no LM response, just use zeros */
437                                 static const uint8_t zeros[24];
438                                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
439                                 SMBsesskeygen_lm_sess_key(zeros, zeros, 
440                                                           session_key.data);
441                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
442                                 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
443                         }
444                 } else {
445                         /* LM Key not selected */
446                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
447
448                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
449                         session_key = data_blob(NULL, 0);
450                 }
451
452         } else if (user_session_key && user_session_key->data) {
453                 session_key = data_blob_talloc(ntlmssp_state, user_session_key->data, user_session_key->length);
454                 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
455                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
456
457                 /* LM Key not selected */
458                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
459
460         } else if (lm_session_key && lm_session_key->data) {
461                 /* Very weird to have LM key, but no user session key, but anyway.. */
462                 session_key = data_blob_talloc(ntlmssp_state, lm_session_key->data, lm_session_key->length);
463                 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
464                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
465
466                 /* LM Key not selected */
467                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
468
469         } else {
470                 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
471                 session_key = data_blob(NULL, 0);
472
473                 /* LM Key not selected */
474                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
475         }
476
477         /* With KEY_EXCH, the client supplies the proposed session key, 
478            but encrypts it with the long-term key */
479         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
480                 if (!state->encrypted_session_key.data
481                     || state->encrypted_session_key.length != 16) {
482                         data_blob_free(&state->encrypted_session_key);
483                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n", 
484                                   (unsigned)state->encrypted_session_key.length));
485                         return NT_STATUS_INVALID_PARAMETER;
486                 } else if (!session_key.data || session_key.length != 16) {
487                         DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n", 
488                                   (unsigned)session_key.length));
489                         ntlmssp_state->session_key = session_key;
490                 } else {
491                         dump_data_pw("KEY_EXCH session key (enc):\n", 
492                                      state->encrypted_session_key.data,
493                                      state->encrypted_session_key.length);
494                         arcfour_crypt(state->encrypted_session_key.data,
495                                       session_key.data, 
496                                       state->encrypted_session_key.length);
497                         ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state,
498                                                                       state->encrypted_session_key.data,
499                                                                       state->encrypted_session_key.length);
500                         dump_data_pw("KEY_EXCH session key:\n",
501                                      state->encrypted_session_key.data,
502                                      state->encrypted_session_key.length);
503                         talloc_free(session_key.data);
504                 }
505         } else {
506                 ntlmssp_state->session_key = session_key;
507         }
508
509         if ((gensec_security->want_features & GENSEC_FEATURE_SIGN)
510             || (gensec_security->want_features & GENSEC_FEATURE_SEAL)) {
511                 nt_status = ntlmssp_sign_init(ntlmssp_state);
512         } else {
513                 nt_status = NT_STATUS_OK;
514         }
515
516         ntlmssp_state->expected_state = NTLMSSP_DONE;
517
518         return nt_status;
519 }
520
521
522 /**
523  * Next state function for the Authenticate packet
524  * 
525  * @param gensec_security GENSEC state
526  * @param out_mem_ctx Memory context for *out
527  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
528  * @param out The reply, as an allocated DATA_BLOB, caller to free.
529  * @return Errors or NT_STATUS_OK if authentication sucessful
530  */
531
532 NTSTATUS ntlmssp_server_auth(struct gensec_security *gensec_security, 
533                              TALLOC_CTX *out_mem_ctx, 
534                              const DATA_BLOB in, DATA_BLOB *out) 
535 {       
536         struct gensec_ntlmssp_context *gensec_ntlmssp =
537                 talloc_get_type_abort(gensec_security->private_data,
538                                       struct gensec_ntlmssp_context);
539         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
540         struct ntlmssp_server_auth_state *state;
541         NTSTATUS nt_status;
542
543         /* zero the outbound NTLMSSP packet */
544         *out = data_blob_null;
545
546         state = talloc_zero(ntlmssp_state, struct ntlmssp_server_auth_state);
547         if (state == NULL) {
548                 return NT_STATUS_NO_MEMORY;
549         }
550
551         nt_status = ntlmssp_server_preauth(ntlmssp_state, state, in);
552         if (!NT_STATUS_IS_OK(nt_status)) {
553                 TALLOC_FREE(state);
554                 return nt_status;
555         }
556
557         /*
558          * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
559          * is required (by "ntlm auth = no" and "lm auth = no" being set in the
560          * smb.conf file) and no NTLMv2 response was sent then the password check
561          * will fail here. JRA.
562          */
563
564         /* Finally, actually ask if the password is OK */
565         nt_status = ntlmssp_state->check_password(ntlmssp_state,
566                                                   &state->user_session_key,
567                                                   &state->lm_session_key);
568         if (!NT_STATUS_IS_OK(nt_status)) {
569                 TALLOC_FREE(state);
570                 return nt_status;
571         }
572
573         nt_status = ntlmssp_server_postauth(gensec_security, state);
574         if (!NT_STATUS_IS_OK(nt_status)) {
575                 TALLOC_FREE(state);
576                 return nt_status;
577         }
578
579         TALLOC_FREE(state);
580         return NT_STATUS_OK;
581 }
582
583 /**
584  * Return the challenge as determined by the authentication subsystem 
585  * @return an 8 byte random challenge
586  */
587
588 static NTSTATUS auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state,
589                                            uint8_t chal[8])
590 {
591         struct gensec_ntlmssp_context *gensec_ntlmssp =
592                 talloc_get_type_abort(ntlmssp_state->callback_private,
593                                       struct gensec_ntlmssp_context);
594         struct auth_context *auth_context = gensec_ntlmssp->auth_context;
595         NTSTATUS status;
596
597         status = auth_context->get_challenge(auth_context, chal);
598         if (!NT_STATUS_IS_OK(status)) {
599                 DEBUG(1, ("auth_ntlmssp_get_challenge: failed to get challenge: %s\n",
600                         nt_errstr(status)));
601                 return status;
602         }
603
604         return NT_STATUS_OK;
605 }
606
607 /**
608  * Some authentication methods 'fix' the challenge, so we may not be able to set it
609  *
610  * @return If the effective challenge used by the auth subsystem may be modified
611  */
612 static bool auth_ntlmssp_may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
613 {
614         struct gensec_ntlmssp_context *gensec_ntlmssp =
615                 talloc_get_type_abort(ntlmssp_state->callback_private,
616                                       struct gensec_ntlmssp_context);
617         struct auth_context *auth_context = gensec_ntlmssp->auth_context;
618
619         return auth_context->challenge_may_be_modified(auth_context);
620 }
621
622 /**
623  * NTLM2 authentication modifies the effective challenge, 
624  * @param challenge The new challenge value
625  */
626 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
627 {
628         struct gensec_ntlmssp_context *gensec_ntlmssp =
629                 talloc_get_type_abort(ntlmssp_state->callback_private,
630                                       struct gensec_ntlmssp_context);
631         struct auth_context *auth_context = gensec_ntlmssp->auth_context;
632         NTSTATUS nt_status;
633         const uint8_t *chal;
634
635         if (challenge->length != 8) {
636                 return NT_STATUS_INVALID_PARAMETER;
637         }
638
639         chal = challenge->data;
640
641         nt_status = auth_context->set_challenge(auth_context,
642                                                 chal,
643                                                 "NTLMSSP callback (NTLM2)");
644
645         return nt_status;
646 }
647
648 /**
649  * Check the password on an NTLMSSP login.  
650  *
651  * Return the session keys used on the connection.
652  */
653
654 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state,
655                                             DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
656 {
657         struct gensec_ntlmssp_context *gensec_ntlmssp =
658                 talloc_get_type_abort(ntlmssp_state->callback_private,
659                                       struct gensec_ntlmssp_context);
660         struct auth_context *auth_context = gensec_ntlmssp->auth_context;
661         NTSTATUS nt_status;
662         struct auth_usersupplied_info *user_info;
663
664         user_info = talloc(ntlmssp_state, struct auth_usersupplied_info);
665         if (!user_info) {
666                 return NT_STATUS_NO_MEMORY;
667         }
668
669         user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
670         user_info->flags = 0;
671         user_info->mapped_state = false;
672         user_info->client.account_name = ntlmssp_state->user;
673         user_info->client.domain_name = ntlmssp_state->domain;
674         user_info->workstation_name = ntlmssp_state->workstation;
675         user_info->remote_host = gensec_get_remote_address(gensec_ntlmssp->gensec_security);
676
677         user_info->password_state = AUTH_PASSWORD_RESPONSE;
678         user_info->password.response.lanman = ntlmssp_state->lm_resp;
679         user_info->password.response.lanman.data = talloc_steal(user_info, ntlmssp_state->lm_resp.data);
680         user_info->password.response.nt = ntlmssp_state->nt_resp;
681         user_info->password.response.nt.data = talloc_steal(user_info, ntlmssp_state->nt_resp.data);
682
683         nt_status = auth_context->check_password(auth_context,
684                                                  gensec_ntlmssp,
685                                                  user_info,
686                                                  &gensec_ntlmssp->server_info);
687         talloc_free(user_info);
688         NT_STATUS_NOT_OK_RETURN(nt_status);
689
690         if (gensec_ntlmssp->server_info->user_session_key.length) {
691                 DEBUG(10, ("Got NT session key of length %u\n",
692                            (unsigned)gensec_ntlmssp->server_info->user_session_key.length));
693                 *user_session_key = gensec_ntlmssp->server_info->user_session_key;
694         }
695         if (gensec_ntlmssp->server_info->lm_session_key.length) {
696                 DEBUG(10, ("Got LM session key of length %u\n",
697                            (unsigned)gensec_ntlmssp->server_info->lm_session_key.length));
698                 *lm_session_key = gensec_ntlmssp->server_info->lm_session_key;
699         }
700         return nt_status;
701 }
702
703 /** 
704  * Return the credentials of a logged on user, including session keys
705  * etc.
706  *
707  * Only valid after a successful authentication
708  *
709  * May only be called once per authentication.
710  *
711  */
712
713 NTSTATUS gensec_ntlmssp_session_info(struct gensec_security *gensec_security,
714                                      struct auth_session_info **session_info) 
715 {
716         NTSTATUS nt_status;
717         struct gensec_ntlmssp_context *gensec_ntlmssp =
718                 talloc_get_type_abort(gensec_security->private_data,
719                                       struct gensec_ntlmssp_context);
720         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
721
722         nt_status = auth_generate_session_info(ntlmssp_state,
723                                                gensec_security->event_ctx,
724                                                gensec_security->settings->lp_ctx,
725                                                gensec_ntlmssp->server_info,
726                                                session_info);
727         NT_STATUS_NOT_OK_RETURN(nt_status);
728
729         (*session_info)->session_key = data_blob_talloc(*session_info, 
730                                                         ntlmssp_state->session_key.data,
731                                                         ntlmssp_state->session_key.length);
732
733         return NT_STATUS_OK;
734 }
735
736 /**
737  * Start NTLMSSP on the server side 
738  *
739  */
740 NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
741 {
742         NTSTATUS nt_status;
743         struct ntlmssp_state *ntlmssp_state;
744         struct gensec_ntlmssp_context *gensec_ntlmssp;
745
746         nt_status = gensec_ntlmssp_start(gensec_security);
747         NT_STATUS_NOT_OK_RETURN(nt_status);
748
749         gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
750                                                struct gensec_ntlmssp_context);
751         ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
752
753         ntlmssp_state->role = NTLMSSP_SERVER;
754
755         ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
756
757         ntlmssp_state->allow_lm_key = (lp_lanman_auth(gensec_security->settings->lp_ctx)
758                                           && gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "allow_lm_key", false));
759
760         ntlmssp_state->neg_flags =
761                 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_VERSION;
762
763         ntlmssp_state->lm_resp = data_blob(NULL, 0);
764         ntlmssp_state->nt_resp = data_blob(NULL, 0);
765
766         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "128bit", true)) {
767                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
768         }
769
770         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "56bit", true)) {
771                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
772         }
773
774         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "keyexchange", true)) {
775                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
776         }
777
778         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "alwayssign", true)) {
779                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
780         }
781
782         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "ntlm2", true)) {
783                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
784         }
785
786         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
787                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
788         }
789         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
790                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
791         }
792
793         gensec_ntlmssp->auth_context = gensec_security->auth_context;
794
795         ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
796         ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
797         ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
798         ntlmssp_state->check_password = auth_ntlmssp_check_password;
799         if (lp_server_role(gensec_security->settings->lp_ctx) == ROLE_STANDALONE) {
800                 ntlmssp_state->server.is_standalone = true;
801         } else {
802                 ntlmssp_state->server.is_standalone = false;
803         }
804
805         ntlmssp_state->server.netbios_name = lp_netbios_name(gensec_security->settings->lp_ctx);
806
807         ntlmssp_state->server.netbios_domain = lp_workgroup(gensec_security->settings->lp_ctx);
808
809         {
810                 char dnsdomname[MAXHOSTNAMELEN], dnsname[MAXHOSTNAMELEN];
811
812                 /* Find out the DNS domain name */
813                 dnsdomname[0] = '\0';
814                 safe_strcpy(dnsdomname, lp_dnsdomain(gensec_security->settings->lp_ctx), sizeof(dnsdomname) - 1);
815
816                 /* Find out the DNS host name */
817                 safe_strcpy(dnsname, ntlmssp_state->server.netbios_name, sizeof(dnsname) - 1);
818                 if (dnsdomname[0] != '\0') {
819                         safe_strcat(dnsname, ".", sizeof(dnsname) - 1);
820                         safe_strcat(dnsname, dnsdomname, sizeof(dnsname) - 1);
821                 }
822                 strlower_m(dnsname);
823
824                 ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state,
825                                                                       dnsname);
826                 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_name);
827
828                 ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state,
829                                                                         dnsdomname);
830                 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_domain);
831         }
832
833         return NT_STATUS_OK;
834 }
835