s4-loadparm: 2nd half of lp_ to lpcfg_ conversion
[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 "../librpc/gen_ndr/ndr_ntlmssp.h"
29 #include "../libcli/auth/libcli_auth.h"
30 #include "../lib/crypto/crypto.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/gensec/gensec_proto.h"
33 #include "auth/auth.h"
34 #include "param/param.h"
35
36 /**
37  * Determine correct target name flags for reply, given server role 
38  * and negotiated flags
39  * 
40  * @param ntlmssp_state NTLMSSP State
41  * @param neg_flags The flags from the packet
42  * @param chal_flags The flags to be set in the reply packet
43  * @return The 'target name' string.
44  */
45
46 static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
47                                        uint32_t neg_flags, uint32_t *chal_flags) 
48 {
49         if (neg_flags & NTLMSSP_REQUEST_TARGET) {
50                 *chal_flags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
51                 *chal_flags |= NTLMSSP_REQUEST_TARGET;
52                 if (ntlmssp_state->server.is_standalone) {
53                         *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
54                         return ntlmssp_state->server.netbios_name;
55                 } else {
56                         *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
57                         return ntlmssp_state->server.netbios_domain;
58                 };
59         } else {
60                 return "";
61         }
62 }
63
64
65
66 /**
67  * Next state function for the Negotiate packet
68  * 
69  * @param gensec_security GENSEC state
70  * @param out_mem_ctx Memory context for *out
71  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
72  * @param out The reply, as an allocated DATA_BLOB, caller to free.
73  * @return Errors or MORE_PROCESSING_REQUIRED if (normal) a reply is required. 
74  */
75
76 NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security, 
77                                   TALLOC_CTX *out_mem_ctx, 
78                                   const DATA_BLOB in, DATA_BLOB *out) 
79 {
80         struct gensec_ntlmssp_context *gensec_ntlmssp =
81                 talloc_get_type_abort(gensec_security->private_data,
82                                       struct gensec_ntlmssp_context);
83         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
84         DATA_BLOB struct_blob;
85         uint32_t neg_flags = 0;
86         uint32_t ntlmssp_command, chal_flags;
87         uint8_t cryptkey[8];
88         const char *target_name;
89         NTSTATUS status;
90
91         /* parse the NTLMSSP packet */
92 #if 0
93         file_save("ntlmssp_negotiate.dat", request.data, request.length);
94 #endif
95
96         if (in.length) {
97                 if ((in.length < 16) || !msrpc_parse(out_mem_ctx, 
98                                                          &in, "Cdd",
99                                                          "NTLMSSP",
100                                                          &ntlmssp_command,
101                                                          &neg_flags)) {
102                         DEBUG(1, ("ntlmssp_server_negotiate: failed to parse "
103                                 "NTLMSSP Negotiate of length %u:\n",
104                                 (unsigned int)in.length ));
105                         dump_data(2, in.data, in.length);
106                         return NT_STATUS_INVALID_PARAMETER;
107                 }
108                 debug_ntlmssp_flags(neg_flags);
109         }
110         
111         ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, ntlmssp_state->allow_lm_key);
112
113         /* Ask our caller what challenge they would like in the packet */
114         status = ntlmssp_state->get_challenge(ntlmssp_state, cryptkey);
115         if (!NT_STATUS_IS_OK(status)) {
116                 DEBUG(1, ("ntlmssp_server_negotiate: backend doesn't give a challenge: %s\n",
117                           nt_errstr(status)));
118                 return status;
119         }
120
121         /* Check if we may set the challenge */
122         if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
123                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
124         }
125
126         /* The flags we send back are not just the negotiated flags,
127          * they are also 'what is in this packet'.  Therfore, we
128          * operate on 'chal_flags' from here on 
129          */
130
131         chal_flags = ntlmssp_state->neg_flags;
132
133         /* get the right name to fill in as 'target' */
134         target_name = ntlmssp_target_name(ntlmssp_state,
135                                           neg_flags, &chal_flags); 
136         if (target_name == NULL) 
137                 return NT_STATUS_INVALID_PARAMETER;
138
139         ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
140         ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
141
142         /* This creates the 'blob' of names that appears at the end of the packet */
143         if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
144                 msrpc_gen(out_mem_ctx, 
145                           &struct_blob, "aaaaa",
146                           MsvAvNbDomainName, target_name,
147                           MsvAvNbComputerName, ntlmssp_state->server.netbios_name,
148                           MsvAvDnsDomainName, ntlmssp_state->server.dns_domain,
149                           MsvAvDnsComputerName, ntlmssp_state->server.dns_name,
150                           MsvAvEOL, "");
151         } else {
152                 struct_blob = data_blob(NULL, 0);
153         }
154
155         {
156                 /* Marshal the packet in the right format, be it unicode or ASCII */
157                 const char *gen_string;
158                 DATA_BLOB version_blob = data_blob_null;
159
160                 if (chal_flags & NTLMSSP_NEGOTIATE_VERSION) {
161                         enum ndr_err_code err;
162                         struct VERSION vers;
163
164                         /* "What Windows returns" as a version number. */
165                         ZERO_STRUCT(vers);
166                         vers.ProductMajorVersion = NTLMSSP_WINDOWS_MAJOR_VERSION_6;
167                         vers.ProductMinorVersion = NTLMSSP_WINDOWS_MINOR_VERSION_1;
168                         vers.ProductBuild = 0;
169                         vers.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
170
171                         err = ndr_push_struct_blob(&version_blob,
172                                                 out_mem_ctx,
173                                                 &vers,
174                                                 (ndr_push_flags_fn_t)ndr_push_VERSION);
175
176                         if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
177                                 return NT_STATUS_NO_MEMORY;
178                         }
179                 }
180
181                 if (ntlmssp_state->unicode) {
182                         gen_string = "CdUdbddBb";
183                 } else {
184                         gen_string = "CdAdbddBb";
185                 }
186                 
187                 msrpc_gen(out_mem_ctx, 
188                           out, gen_string,
189                           "NTLMSSP", 
190                           NTLMSSP_CHALLENGE,
191                           target_name,
192                           chal_flags,
193                           cryptkey, 8,
194                           0, 0,
195                           struct_blob.data, struct_blob.length,
196                           version_blob.data, version_blob.length);
197
198                 data_blob_free(&version_blob);
199         }
200                 
201         ntlmssp_state->expected_state = NTLMSSP_AUTH;
202
203         return NT_STATUS_MORE_PROCESSING_REQUIRED;
204 }
205
206 struct ntlmssp_server_auth_state {
207         DATA_BLOB user_session_key;
208         DATA_BLOB lm_session_key;
209         /* internal variables used by KEY_EXCH (client-supplied user session key */
210         DATA_BLOB encrypted_session_key;
211         bool doing_ntlm2;
212         /* internal variables used by NTLM2 */
213         uint8_t session_nonce[16];
214 };
215
216 /**
217  * Next state function for the Authenticate packet
218  * 
219  * @param ntlmssp_state NTLMSSP State
220  * @param request The request, as a DATA_BLOB
221  * @return Errors or NT_STATUS_OK. 
222  */
223
224 static NTSTATUS ntlmssp_server_preauth(struct ntlmssp_state *ntlmssp_state,
225                                        struct ntlmssp_server_auth_state *state,
226                                        const DATA_BLOB request) 
227 {
228         uint32_t ntlmssp_command, auth_flags;
229         NTSTATUS nt_status;
230
231         uint8_t session_nonce_hash[16];
232
233         const char *parse_string;
234
235 #if 0
236         file_save("ntlmssp_auth.dat", request.data, request.length);
237 #endif
238
239         if (ntlmssp_state->unicode) {
240                 parse_string = "CdBBUUUBd";
241         } else {
242                 parse_string = "CdBBAAABd";
243         }
244
245         /* zero these out */
246         data_blob_free(&ntlmssp_state->session_key);
247         data_blob_free(&ntlmssp_state->lm_resp);
248         data_blob_free(&ntlmssp_state->nt_resp);
249
250         ntlmssp_state->user = NULL;
251         ntlmssp_state->domain = NULL;
252         ntlmssp_state->client.netbios_name = NULL;
253
254         /* now the NTLMSSP encoded auth hashes */
255         if (!msrpc_parse(ntlmssp_state,
256                          &request, parse_string,
257                          "NTLMSSP", 
258                          &ntlmssp_command, 
259                          &ntlmssp_state->lm_resp,
260                          &ntlmssp_state->nt_resp,
261                          &ntlmssp_state->domain,
262                          &ntlmssp_state->user,
263                          &ntlmssp_state->client.netbios_name,
264                          &state->encrypted_session_key,
265                          &auth_flags)) {
266                 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
267                 dump_data(10, request.data, request.length);
268
269                 /* zero this out */
270                 data_blob_free(&state->encrypted_session_key);
271                 auth_flags = 0;
272                 
273                 /* Try again with a shorter string (Win9X truncates this packet) */
274                 if (ntlmssp_state->unicode) {
275                         parse_string = "CdBBUUU";
276                 } else {
277                         parse_string = "CdBBAAA";
278                 }
279
280                 /* now the NTLMSSP encoded auth hashes */
281                 if (!msrpc_parse(ntlmssp_state,
282                                  &request, parse_string,
283                                  "NTLMSSP", 
284                                  &ntlmssp_command, 
285                                  &ntlmssp_state->lm_resp,
286                                  &ntlmssp_state->nt_resp,
287                                  &ntlmssp_state->domain,
288                                  &ntlmssp_state->user,
289                                  &ntlmssp_state->client.netbios_name)) {
290                         DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP:\n"));
291                         dump_data(2, request.data, request.length);
292
293                         return NT_STATUS_INVALID_PARAMETER;
294                 }
295         }
296
297         talloc_steal(state, state->encrypted_session_key.data);
298
299         if (auth_flags)
300                 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, ntlmssp_state->allow_lm_key);
301
302         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
303                  ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->client.netbios_name, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
304
305 #if 0
306         file_save("nthash1.dat",  &ntlmssp_state->nt_resp.data,  &ntlmssp_state->nt_resp.length);
307         file_save("lmhash1.dat",  &ntlmssp_state->lm_resp.data,  &ntlmssp_state->lm_resp.length);
308 #endif
309
310         /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a 
311            client challenge 
312         
313            However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
314         */
315         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
316                 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
317                         struct MD5Context md5_session_nonce_ctx;
318                         SMB_ASSERT(ntlmssp_state->internal_chal.data
319                                    && ntlmssp_state->internal_chal.length == 8);
320                         
321                         state->doing_ntlm2 = true;
322
323                         memcpy(state->session_nonce, ntlmssp_state->internal_chal.data, 8);
324                         memcpy(&state->session_nonce[8], ntlmssp_state->lm_resp.data, 8);
325                         
326                         MD5Init(&md5_session_nonce_ctx);
327                         MD5Update(&md5_session_nonce_ctx, state->session_nonce, 16);
328                         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
329                         
330                         ntlmssp_state->chal = data_blob_talloc(ntlmssp_state,
331                                                                session_nonce_hash, 8);
332
333                         /* LM response is no longer useful, zero it out */
334                         data_blob_free(&ntlmssp_state->lm_resp);
335
336                         /* We changed the effective challenge - set it */
337                         if (!NT_STATUS_IS_OK(nt_status = 
338                                              ntlmssp_state->set_challenge(ntlmssp_state,
339                                                                                  &ntlmssp_state->chal))) {
340                                 return nt_status;
341                         }
342
343                         /* LM Key is incompatible... */
344                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
345                 }
346         }
347         return NT_STATUS_OK;
348 }
349
350 /**
351  * Next state function for the Authenticate packet 
352  * (after authentication - figures out the session keys etc)
353  * 
354  * @param ntlmssp_state NTLMSSP State
355  * @return Errors or NT_STATUS_OK. 
356  */
357
358 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security, 
359                                         struct ntlmssp_server_auth_state *state)
360 {
361         struct gensec_ntlmssp_context *gensec_ntlmssp =
362                 talloc_get_type_abort(gensec_security->private_data,
363                                       struct gensec_ntlmssp_context);
364         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
365         DATA_BLOB *user_session_key = &state->user_session_key;
366         DATA_BLOB *lm_session_key = &state->lm_session_key;
367         NTSTATUS nt_status;
368         DATA_BLOB session_key = data_blob(NULL, 0);
369
370         if (!(gensec_security->want_features
371               & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL|GENSEC_FEATURE_SESSION_KEY))) {
372                 return NT_STATUS_OK;
373         }
374
375         if (user_session_key)
376                 dump_data_pw("USER session key:\n", user_session_key->data, user_session_key->length);
377
378         if (lm_session_key) 
379                 dump_data_pw("LM first-8:\n", lm_session_key->data, lm_session_key->length);
380
381         /* Handle the different session key derivation for NTLM2 */
382         if (state->doing_ntlm2) {
383                 if (user_session_key && user_session_key->data && user_session_key->length == 16) {
384                         session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
385                         hmac_md5(user_session_key->data, state->session_nonce,
386                                  sizeof(state->session_nonce), session_key.data);
387                         DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
388                         dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
389                         
390                 } else {
391                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
392                         session_key = data_blob(NULL, 0);
393                 }
394         } else if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
395                 /* Ensure we can never get here on NTLMv2 */
396                 && (ntlmssp_state->nt_resp.length == 0 || ntlmssp_state->nt_resp.length == 24)) {
397
398                 if (lm_session_key && lm_session_key->data && lm_session_key->length >= 8) {
399                         if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
400                                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
401                                 SMBsesskeygen_lm_sess_key(lm_session_key->data, ntlmssp_state->lm_resp.data,
402                                                           session_key.data);
403                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
404                                 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
405                         } else {
406                                 
407                                 /* When there is no LM response, just use zeros */
408                                 static const uint8_t zeros[24];
409                                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
410                                 SMBsesskeygen_lm_sess_key(zeros, zeros, 
411                                                           session_key.data);
412                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
413                                 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
414                         }
415                 } else {
416                         /* LM Key not selected */
417                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
418
419                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
420                         session_key = data_blob(NULL, 0);
421                 }
422
423         } else if (user_session_key && user_session_key->data) {
424                 session_key = data_blob_talloc(ntlmssp_state, user_session_key->data, user_session_key->length);
425                 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
426                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
427
428                 /* LM Key not selected */
429                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
430
431         } else if (lm_session_key && lm_session_key->data) {
432                 /* Very weird to have LM key, but no user session key, but anyway.. */
433                 session_key = data_blob_talloc(ntlmssp_state, lm_session_key->data, lm_session_key->length);
434                 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
435                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
436
437                 /* LM Key not selected */
438                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
439
440         } else {
441                 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
442                 session_key = data_blob(NULL, 0);
443
444                 /* LM Key not selected */
445                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
446         }
447
448         /* With KEY_EXCH, the client supplies the proposed session key, 
449            but encrypts it with the long-term key */
450         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
451                 if (!state->encrypted_session_key.data
452                     || state->encrypted_session_key.length != 16) {
453                         data_blob_free(&state->encrypted_session_key);
454                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n", 
455                                   (unsigned)state->encrypted_session_key.length));
456                         return NT_STATUS_INVALID_PARAMETER;
457                 } else if (!session_key.data || session_key.length != 16) {
458                         DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n", 
459                                   (unsigned)session_key.length));
460                         ntlmssp_state->session_key = session_key;
461                 } else {
462                         dump_data_pw("KEY_EXCH session key (enc):\n", 
463                                      state->encrypted_session_key.data,
464                                      state->encrypted_session_key.length);
465                         arcfour_crypt(state->encrypted_session_key.data,
466                                       session_key.data, 
467                                       state->encrypted_session_key.length);
468                         ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state,
469                                                                       state->encrypted_session_key.data,
470                                                                       state->encrypted_session_key.length);
471                         dump_data_pw("KEY_EXCH session key:\n",
472                                      state->encrypted_session_key.data,
473                                      state->encrypted_session_key.length);
474                         talloc_free(session_key.data);
475                 }
476         } else {
477                 ntlmssp_state->session_key = session_key;
478         }
479
480         if ((gensec_security->want_features & GENSEC_FEATURE_SIGN)
481             || (gensec_security->want_features & GENSEC_FEATURE_SEAL)) {
482                 nt_status = ntlmssp_sign_init(ntlmssp_state);
483         } else {
484                 nt_status = NT_STATUS_OK;
485         }
486
487         ntlmssp_state->expected_state = NTLMSSP_DONE;
488
489         return nt_status;
490 }
491
492
493 /**
494  * Next state function for the Authenticate packet
495  * 
496  * @param gensec_security GENSEC state
497  * @param out_mem_ctx Memory context for *out
498  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
499  * @param out The reply, as an allocated DATA_BLOB, caller to free.
500  * @return Errors or NT_STATUS_OK if authentication sucessful
501  */
502
503 NTSTATUS ntlmssp_server_auth(struct gensec_security *gensec_security, 
504                              TALLOC_CTX *out_mem_ctx, 
505                              const DATA_BLOB in, DATA_BLOB *out) 
506 {       
507         struct gensec_ntlmssp_context *gensec_ntlmssp =
508                 talloc_get_type_abort(gensec_security->private_data,
509                                       struct gensec_ntlmssp_context);
510         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
511         struct ntlmssp_server_auth_state *state;
512         NTSTATUS nt_status;
513
514         /* zero the outbound NTLMSSP packet */
515         *out = data_blob_null;
516
517         state = talloc_zero(ntlmssp_state, struct ntlmssp_server_auth_state);
518         if (state == NULL) {
519                 return NT_STATUS_NO_MEMORY;
520         }
521
522         nt_status = ntlmssp_server_preauth(ntlmssp_state, state, in);
523         if (!NT_STATUS_IS_OK(nt_status)) {
524                 TALLOC_FREE(state);
525                 return nt_status;
526         }
527
528         /*
529          * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
530          * is required (by "ntlm auth = no" and "lm auth = no" being set in the
531          * smb.conf file) and no NTLMv2 response was sent then the password check
532          * will fail here. JRA.
533          */
534
535         /* Finally, actually ask if the password is OK */
536         nt_status = ntlmssp_state->check_password(ntlmssp_state,
537                                                   &state->user_session_key,
538                                                   &state->lm_session_key);
539         if (!NT_STATUS_IS_OK(nt_status)) {
540                 TALLOC_FREE(state);
541                 return nt_status;
542         }
543
544         nt_status = ntlmssp_server_postauth(gensec_security, state);
545         if (!NT_STATUS_IS_OK(nt_status)) {
546                 TALLOC_FREE(state);
547                 return nt_status;
548         }
549
550         TALLOC_FREE(state);
551         return NT_STATUS_OK;
552 }
553
554 /**
555  * Return the challenge as determined by the authentication subsystem 
556  * @return an 8 byte random challenge
557  */
558
559 static NTSTATUS auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state,
560                                            uint8_t chal[8])
561 {
562         struct gensec_ntlmssp_context *gensec_ntlmssp =
563                 talloc_get_type_abort(ntlmssp_state->callback_private,
564                                       struct gensec_ntlmssp_context);
565         struct auth_context *auth_context = gensec_ntlmssp->auth_context;
566         NTSTATUS status;
567
568         status = auth_context->get_challenge(auth_context, chal);
569         if (!NT_STATUS_IS_OK(status)) {
570                 DEBUG(1, ("auth_ntlmssp_get_challenge: failed to get challenge: %s\n",
571                         nt_errstr(status)));
572                 return status;
573         }
574
575         return NT_STATUS_OK;
576 }
577
578 /**
579  * Some authentication methods 'fix' the challenge, so we may not be able to set it
580  *
581  * @return If the effective challenge used by the auth subsystem may be modified
582  */
583 static bool auth_ntlmssp_may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
584 {
585         struct gensec_ntlmssp_context *gensec_ntlmssp =
586                 talloc_get_type_abort(ntlmssp_state->callback_private,
587                                       struct gensec_ntlmssp_context);
588         struct auth_context *auth_context = gensec_ntlmssp->auth_context;
589
590         return auth_context->challenge_may_be_modified(auth_context);
591 }
592
593 /**
594  * NTLM2 authentication modifies the effective challenge, 
595  * @param challenge The new challenge value
596  */
597 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
598 {
599         struct gensec_ntlmssp_context *gensec_ntlmssp =
600                 talloc_get_type_abort(ntlmssp_state->callback_private,
601                                       struct gensec_ntlmssp_context);
602         struct auth_context *auth_context = gensec_ntlmssp->auth_context;
603         NTSTATUS nt_status;
604         const uint8_t *chal;
605
606         if (challenge->length != 8) {
607                 return NT_STATUS_INVALID_PARAMETER;
608         }
609
610         chal = challenge->data;
611
612         nt_status = auth_context->set_challenge(auth_context,
613                                                 chal,
614                                                 "NTLMSSP callback (NTLM2)");
615
616         return nt_status;
617 }
618
619 /**
620  * Check the password on an NTLMSSP login.  
621  *
622  * Return the session keys used on the connection.
623  */
624
625 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state,
626                                             DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
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         struct auth_usersupplied_info *user_info;
634
635         user_info = talloc(ntlmssp_state, struct auth_usersupplied_info);
636         if (!user_info) {
637                 return NT_STATUS_NO_MEMORY;
638         }
639
640         user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
641         user_info->flags = 0;
642         user_info->mapped_state = false;
643         user_info->client.account_name = ntlmssp_state->user;
644         user_info->client.domain_name = ntlmssp_state->domain;
645         user_info->workstation_name = ntlmssp_state->client.netbios_name;
646         user_info->remote_host = gensec_get_remote_address(gensec_ntlmssp->gensec_security);
647
648         user_info->password_state = AUTH_PASSWORD_RESPONSE;
649         user_info->password.response.lanman = ntlmssp_state->lm_resp;
650         user_info->password.response.lanman.data = talloc_steal(user_info, ntlmssp_state->lm_resp.data);
651         user_info->password.response.nt = ntlmssp_state->nt_resp;
652         user_info->password.response.nt.data = talloc_steal(user_info, ntlmssp_state->nt_resp.data);
653
654         nt_status = auth_context->check_password(auth_context,
655                                                  gensec_ntlmssp,
656                                                  user_info,
657                                                  &gensec_ntlmssp->server_info);
658         talloc_free(user_info);
659         NT_STATUS_NOT_OK_RETURN(nt_status);
660
661         if (gensec_ntlmssp->server_info->user_session_key.length) {
662                 DEBUG(10, ("Got NT session key of length %u\n",
663                            (unsigned)gensec_ntlmssp->server_info->user_session_key.length));
664                 *user_session_key = gensec_ntlmssp->server_info->user_session_key;
665         }
666         if (gensec_ntlmssp->server_info->lm_session_key.length) {
667                 DEBUG(10, ("Got LM session key of length %u\n",
668                            (unsigned)gensec_ntlmssp->server_info->lm_session_key.length));
669                 *lm_session_key = gensec_ntlmssp->server_info->lm_session_key;
670         }
671         return nt_status;
672 }
673
674 /** 
675  * Return the credentials of a logged on user, including session keys
676  * etc.
677  *
678  * Only valid after a successful authentication
679  *
680  * May only be called once per authentication.
681  *
682  */
683
684 NTSTATUS gensec_ntlmssp_session_info(struct gensec_security *gensec_security,
685                                      struct auth_session_info **session_info) 
686 {
687         NTSTATUS nt_status;
688         struct gensec_ntlmssp_context *gensec_ntlmssp =
689                 talloc_get_type_abort(gensec_security->private_data,
690                                       struct gensec_ntlmssp_context);
691         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
692
693         nt_status = gensec_generate_session_info(ntlmssp_state,
694                                                  gensec_security,
695                                                  gensec_ntlmssp->server_info,
696                                                  session_info);
697         NT_STATUS_NOT_OK_RETURN(nt_status);
698
699         (*session_info)->session_key = data_blob_talloc(*session_info, 
700                                                         ntlmssp_state->session_key.data,
701                                                         ntlmssp_state->session_key.length);
702
703         return NT_STATUS_OK;
704 }
705
706 /**
707  * Start NTLMSSP on the server side 
708  *
709  */
710 NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
711 {
712         NTSTATUS nt_status;
713         struct ntlmssp_state *ntlmssp_state;
714         struct gensec_ntlmssp_context *gensec_ntlmssp;
715
716         nt_status = gensec_ntlmssp_start(gensec_security);
717         NT_STATUS_NOT_OK_RETURN(nt_status);
718
719         gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
720                                                struct gensec_ntlmssp_context);
721         ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
722
723         ntlmssp_state->role = NTLMSSP_SERVER;
724
725         ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
726
727         ntlmssp_state->allow_lm_key = (lpcfg_lanman_auth(gensec_security->settings->lp_ctx)
728                                           && gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "allow_lm_key", false));
729
730         ntlmssp_state->neg_flags =
731                 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_VERSION;
732
733         ntlmssp_state->lm_resp = data_blob(NULL, 0);
734         ntlmssp_state->nt_resp = data_blob(NULL, 0);
735
736         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "128bit", true)) {
737                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
738         }
739
740         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "56bit", true)) {
741                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
742         }
743
744         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "keyexchange", true)) {
745                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
746         }
747
748         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "alwayssign", true)) {
749                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
750         }
751
752         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "ntlm2", true)) {
753                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
754         }
755
756         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
757                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
758         }
759         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
760                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
761         }
762
763         gensec_ntlmssp->auth_context = gensec_security->auth_context;
764
765         ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
766         ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
767         ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
768         ntlmssp_state->check_password = auth_ntlmssp_check_password;
769         if (lpcfg_server_role(gensec_security->settings->lp_ctx) == ROLE_STANDALONE) {
770                 ntlmssp_state->server.is_standalone = true;
771         } else {
772                 ntlmssp_state->server.is_standalone = false;
773         }
774
775         ntlmssp_state->server.netbios_name = lpcfg_netbios_name(gensec_security->settings->lp_ctx);
776
777         ntlmssp_state->server.netbios_domain = lpcfg_workgroup(gensec_security->settings->lp_ctx);
778
779         {
780                 char dnsdomname[MAXHOSTNAMELEN], dnsname[MAXHOSTNAMELEN];
781
782                 /* Find out the DNS domain name */
783                 dnsdomname[0] = '\0';
784                 safe_strcpy(dnsdomname, lpcfg_dnsdomain(gensec_security->settings->lp_ctx), sizeof(dnsdomname) - 1);
785
786                 /* Find out the DNS host name */
787                 safe_strcpy(dnsname, ntlmssp_state->server.netbios_name, sizeof(dnsname) - 1);
788                 if (dnsdomname[0] != '\0') {
789                         safe_strcat(dnsname, ".", sizeof(dnsname) - 1);
790                         safe_strcat(dnsname, dnsdomname, sizeof(dnsname) - 1);
791                 }
792                 strlower_m(dnsname);
793
794                 ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state,
795                                                                       dnsname);
796                 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_name);
797
798                 ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state,
799                                                                         dnsdomname);
800                 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_domain);
801         }
802
803         return NT_STATUS_OK;
804 }
805