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