auth/ntlmssp: Remove reference to struct ntlmssp_state
[metze/samba/wip.git] / auth / ntlmssp / ntlmssp_server.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NTLMSSP, server side
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett 2001-2010
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "auth/ntlmssp/ntlmssp.h"
25 #include "auth/ntlmssp/ntlmssp_private.h"
26 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
27 #include "auth/ntlmssp/ntlmssp_ndr.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #include "../lib/crypto/crypto.h"
30 #include "auth/gensec/gensec.h"
31 #include "auth/common_auth.h"
32
33 /**
34  * Determine correct target name flags for reply, given server role
35  * and negotiated flags
36  *
37  * @param ntlmssp_state NTLMSSP State
38  * @param neg_flags The flags from the packet
39  * @param chal_flags The flags to be set in the reply packet
40  * @return The 'target name' string.
41  */
42
43 const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
44                                 uint32_t neg_flags, uint32_t *chal_flags)
45 {
46         if (neg_flags & NTLMSSP_REQUEST_TARGET) {
47                 *chal_flags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
48                 *chal_flags |= NTLMSSP_REQUEST_TARGET;
49                 if (ntlmssp_state->server.is_standalone) {
50                         *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
51                         return ntlmssp_state->server.netbios_name;
52                 } else {
53                         *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
54                         return ntlmssp_state->server.netbios_domain;
55                 };
56         } else {
57                 return "";
58         }
59 }
60
61 /**
62  * Next state function for the NTLMSSP Negotiate packet
63  *
64  * @param gensec_security GENSEC state
65  * @param out_mem_ctx Memory context for *out
66  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
67  * @param out The reply, as an allocated DATA_BLOB, caller to free.
68  * @return Errors or MORE_PROCESSING_REQUIRED if (normal) a reply is required.
69  */
70
71 NTSTATUS gensec_ntlmssp_server_negotiate(struct gensec_security *gensec_security,
72                                          TALLOC_CTX *out_mem_ctx,
73                                          const DATA_BLOB request, DATA_BLOB *reply)
74 {
75         struct gensec_ntlmssp_context *gensec_ntlmssp =
76                 talloc_get_type_abort(gensec_security->private_data,
77                                       struct gensec_ntlmssp_context);
78         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
79         struct auth4_context *auth_context = gensec_security->auth_context;
80         DATA_BLOB struct_blob;
81         uint32_t neg_flags = 0;
82         uint32_t ntlmssp_command, chal_flags;
83         uint8_t cryptkey[8];
84         const char *target_name;
85         NTSTATUS status;
86
87         /* parse the NTLMSSP packet */
88 #if 0
89         file_save("ntlmssp_negotiate.dat", request.data, request.length);
90 #endif
91
92         if (request.length) {
93                 if ((request.length < 16) || !msrpc_parse(ntlmssp_state, &request, "Cdd",
94                                                           "NTLMSSP",
95                                                           &ntlmssp_command,
96                                                           &neg_flags)) {
97                         DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
98                                 (unsigned int)request.length));
99                         dump_data(2, request.data, request.length);
100                         return NT_STATUS_INVALID_PARAMETER;
101                 }
102                 debug_ntlmssp_flags(neg_flags);
103
104                 if (DEBUGLEVEL >= 10) {
105                         struct NEGOTIATE_MESSAGE *negotiate = talloc(
106                                 ntlmssp_state, struct NEGOTIATE_MESSAGE);
107                         if (negotiate != NULL) {
108                                 status = ntlmssp_pull_NEGOTIATE_MESSAGE(
109                                         &request, negotiate, negotiate);
110                                 if (NT_STATUS_IS_OK(status)) {
111                                         NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
112                                                         negotiate);
113                                 }
114                                 TALLOC_FREE(negotiate);
115                         }
116                 }
117         }
118
119         ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, ntlmssp_state->allow_lm_key);
120
121         /* Ask our caller what challenge they would like in the packet */
122         if (auth_context->get_ntlm_challenge) {
123                 status = auth_context->get_ntlm_challenge(auth_context, cryptkey);
124                 if (!NT_STATUS_IS_OK(status)) {
125                         DEBUG(1, ("gensec_ntlmssp_server_negotiate: failed to get challenge: %s\n",
126                                   nt_errstr(status)));
127                         return status;
128                 }
129         } else {
130                 DEBUG(1, ("gensec_ntlmssp_server_negotiate: backend doesn't give a challenge\n"));
131                 return NT_STATUS_NOT_IMPLEMENTED;
132         }
133
134         /* Check if we may set the challenge */
135         if (auth_context->challenge_may_be_modified) {
136                 if (!auth_context->challenge_may_be_modified(auth_context)) {
137                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
138                 }
139         }
140
141         /* The flags we send back are not just the negotiated flags,
142          * they are also 'what is in this packet'.  Therfore, we
143          * operate on 'chal_flags' from here on
144          */
145
146         chal_flags = ntlmssp_state->neg_flags;
147
148         /* get the right name to fill in as 'target' */
149         target_name = ntlmssp_target_name(ntlmssp_state,
150                                           neg_flags, &chal_flags);
151         if (target_name == NULL)
152                 return NT_STATUS_INVALID_PARAMETER;
153
154         ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
155         ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
156                                                         cryptkey, 8);
157
158         /* This creates the 'blob' of names that appears at the end of the packet */
159         if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
160         {
161                 status = msrpc_gen(ntlmssp_state, &struct_blob, "aaaaa",
162                           MsvAvNbDomainName, target_name,
163                           MsvAvNbComputerName, ntlmssp_state->server.netbios_name,
164                           MsvAvDnsDomainName, ntlmssp_state->server.dns_domain,
165                           MsvAvDnsComputerName, ntlmssp_state->server.dns_name,
166                           MsvAvEOL, "");
167                 if (!NT_STATUS_IS_OK(status)) {
168                         return status;
169                 }
170         } else {
171                 struct_blob = data_blob_null;
172         }
173
174         {
175                 /* Marshal the packet in the right format, be it unicode or ASCII */
176                 const char *gen_string;
177                 DATA_BLOB version_blob = data_blob_null;
178
179                 if (chal_flags & NTLMSSP_NEGOTIATE_VERSION) {
180                         enum ndr_err_code err;
181                         struct ntlmssp_VERSION vers;
182
183                         /* "What Windows returns" as a version number. */
184                         ZERO_STRUCT(vers);
185                         vers.ProductMajorVersion = NTLMSSP_WINDOWS_MAJOR_VERSION_6;
186                         vers.ProductMinorVersion = NTLMSSP_WINDOWS_MINOR_VERSION_1;
187                         vers.ProductBuild = 0;
188                         vers.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
189
190                         err = ndr_push_struct_blob(&version_blob,
191                                                 ntlmssp_state,
192                                                 &vers,
193                                                 (ndr_push_flags_fn_t)ndr_push_ntlmssp_VERSION);
194
195                         if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
196                                 data_blob_free(&struct_blob);
197                                 return NT_STATUS_NO_MEMORY;
198                         }
199                 }
200
201                 if (ntlmssp_state->unicode) {
202                         gen_string = "CdUdbddBb";
203                 } else {
204                         gen_string = "CdAdbddBb";
205                 }
206
207                 status = msrpc_gen(out_mem_ctx, reply, gen_string,
208                         "NTLMSSP",
209                         NTLMSSP_CHALLENGE,
210                         target_name,
211                         chal_flags,
212                         cryptkey, 8,
213                         0, 0,
214                         struct_blob.data, struct_blob.length,
215                         version_blob.data, version_blob.length);
216
217                 if (!NT_STATUS_IS_OK(status)) {
218                         data_blob_free(&version_blob);
219                         data_blob_free(&struct_blob);
220                         return status;
221                 }
222
223                 data_blob_free(&version_blob);
224
225                 if (DEBUGLEVEL >= 10) {
226                         struct CHALLENGE_MESSAGE *challenge = talloc(
227                                 ntlmssp_state, struct CHALLENGE_MESSAGE);
228                         if (challenge != NULL) {
229                                 challenge->NegotiateFlags = chal_flags;
230                                 status = ntlmssp_pull_CHALLENGE_MESSAGE(
231                                         reply, challenge, challenge);
232                                 if (NT_STATUS_IS_OK(status)) {
233                                         NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
234                                                         challenge);
235                                 }
236                                 TALLOC_FREE(challenge);
237                         }
238                 }
239         }
240
241         data_blob_free(&struct_blob);
242
243         ntlmssp_state->expected_state = NTLMSSP_AUTH;
244
245         return NT_STATUS_MORE_PROCESSING_REQUIRED;
246 }
247
248 struct ntlmssp_server_auth_state {
249         DATA_BLOB user_session_key;
250         DATA_BLOB lm_session_key;
251         /* internal variables used by KEY_EXCH (client-supplied user session key */
252         DATA_BLOB encrypted_session_key;
253         bool doing_ntlm2;
254         /* internal variables used by NTLM2 */
255         uint8_t session_nonce[16];
256 };
257
258 /**
259  * Next state function for the Authenticate packet
260  *
261  * @param ntlmssp_state NTLMSSP State
262  * @param request The request, as a DATA_BLOB
263  * @return Errors or NT_STATUS_OK.
264  */
265
266 static NTSTATUS ntlmssp_server_preauth(struct gensec_security *gensec_security,
267                                        struct gensec_ntlmssp_context *gensec_ntlmssp,
268                                        struct ntlmssp_server_auth_state *state,
269                                        const DATA_BLOB request)
270 {
271         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
272         struct auth4_context *auth_context = gensec_security->auth_context;
273         uint32_t ntlmssp_command, auth_flags;
274         NTSTATUS nt_status;
275
276         uint8_t session_nonce_hash[16];
277
278         const char *parse_string;
279
280 #if 0
281         file_save("ntlmssp_auth.dat", request.data, request.length);
282 #endif
283
284         if (ntlmssp_state->unicode) {
285                 parse_string = "CdBBUUUBd";
286         } else {
287                 parse_string = "CdBBAAABd";
288         }
289
290         /* zero these out */
291         data_blob_free(&ntlmssp_state->session_key);
292         data_blob_free(&ntlmssp_state->lm_resp);
293         data_blob_free(&ntlmssp_state->nt_resp);
294
295         ntlmssp_state->user = NULL;
296         ntlmssp_state->domain = NULL;
297         ntlmssp_state->client.netbios_name = NULL;
298
299         /* now the NTLMSSP encoded auth hashes */
300         if (!msrpc_parse(ntlmssp_state, &request, parse_string,
301                          "NTLMSSP",
302                          &ntlmssp_command,
303                          &ntlmssp_state->lm_resp,
304                          &ntlmssp_state->nt_resp,
305                          &ntlmssp_state->domain,
306                          &ntlmssp_state->user,
307                          &ntlmssp_state->client.netbios_name,
308                          &state->encrypted_session_key,
309                          &auth_flags)) {
310                 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
311                 dump_data(10, request.data, request.length);
312
313                 /* zero this out */
314                 data_blob_free(&state->encrypted_session_key);
315                 auth_flags = 0;
316
317                 /* Try again with a shorter string (Win9X truncates this packet) */
318                 if (ntlmssp_state->unicode) {
319                         parse_string = "CdBBUUU";
320                 } else {
321                         parse_string = "CdBBAAA";
322                 }
323
324                 /* now the NTLMSSP encoded auth hashes */
325                 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
326                                  "NTLMSSP",
327                                  &ntlmssp_command,
328                                  &ntlmssp_state->lm_resp,
329                                  &ntlmssp_state->nt_resp,
330                                  &ntlmssp_state->domain,
331                                  &ntlmssp_state->user,
332                                  &ntlmssp_state->client.netbios_name)) {
333                         DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
334                         dump_data(2, request.data, request.length);
335
336                         return NT_STATUS_INVALID_PARAMETER;
337                 }
338         }
339
340         talloc_steal(state, state->encrypted_session_key.data);
341
342         if (auth_flags)
343                 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, ntlmssp_state->allow_lm_key);
344
345         if (DEBUGLEVEL >= 10) {
346                 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
347                         ntlmssp_state, struct AUTHENTICATE_MESSAGE);
348                 if (authenticate != NULL) {
349                         NTSTATUS status;
350                         authenticate->NegotiateFlags = auth_flags;
351                         status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
352                                 &request, authenticate, authenticate);
353                         if (NT_STATUS_IS_OK(status)) {
354                                 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
355                                                 authenticate);
356                         }
357                         TALLOC_FREE(authenticate);
358                 }
359         }
360
361         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
362                  ntlmssp_state->user, ntlmssp_state->domain,
363                  ntlmssp_state->client.netbios_name,
364                  (unsigned long)ntlmssp_state->lm_resp.length,
365                  (unsigned long)ntlmssp_state->nt_resp.length));
366
367 #if 0
368         file_save("nthash1.dat",  &ntlmssp_state->nt_resp.data,  &ntlmssp_state->nt_resp.length);
369         file_save("lmhash1.dat",  &ntlmssp_state->lm_resp.data,  &ntlmssp_state->lm_resp.length);
370 #endif
371
372         /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
373            client challenge
374
375            However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
376         */
377         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
378                 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
379                         struct MD5Context md5_session_nonce_ctx;
380                         state->doing_ntlm2 = true;
381
382                         memcpy(state->session_nonce, ntlmssp_state->internal_chal.data, 8);
383                         memcpy(&state->session_nonce[8], ntlmssp_state->lm_resp.data, 8);
384
385                         SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
386
387                         MD5Init(&md5_session_nonce_ctx);
388                         MD5Update(&md5_session_nonce_ctx, state->session_nonce, 16);
389                         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
390
391                         /* LM response is no longer useful */
392                         data_blob_free(&ntlmssp_state->lm_resp);
393
394                         /* We changed the effective challenge - set it */
395                         if (auth_context->set_ntlm_challenge) {
396                                 nt_status = auth_context->set_ntlm_challenge(auth_context,
397                                                                              session_nonce_hash,
398                                                                              "NTLMSSP callback (NTLM2)");
399                                 if (!NT_STATUS_IS_OK(nt_status)) {
400                                         DEBUG(1, ("gensec_ntlmssp_server_negotiate: failed to get challenge: %s\n",
401                                                   nt_errstr(nt_status)));
402                                         return nt_status;
403                                 }
404                         } else {
405                                 DEBUG(1, ("gensec_ntlmssp_server_negotiate: backend doesn't have facility for challenge to be set\n"));
406
407                                 return NT_STATUS_NOT_IMPLEMENTED;
408                         }
409
410                         /* LM Key is incompatible. */
411                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
412                 }
413         }
414         return NT_STATUS_OK;
415 }
416
417 /**
418  * Check the password on an NTLMSSP login.
419  *
420  * Return the session keys used on the connection.
421  */
422
423 static NTSTATUS ntlmssp_server_check_password(struct gensec_security *gensec_security,
424                                               struct gensec_ntlmssp_context *gensec_ntlmssp,
425                                               TALLOC_CTX *mem_ctx,
426                                               DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
427 {
428         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
429         struct auth4_context *auth_context = gensec_security->auth_context;
430         NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
431         struct auth_usersupplied_info *user_info;
432
433         user_info = talloc_zero(ntlmssp_state, struct auth_usersupplied_info);
434         if (!user_info) {
435                 return NT_STATUS_NO_MEMORY;
436         }
437
438         user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
439         user_info->flags = 0;
440         user_info->mapped_state = false;
441         user_info->client.account_name = ntlmssp_state->user;
442         user_info->client.domain_name = ntlmssp_state->domain;
443         user_info->workstation_name = ntlmssp_state->client.netbios_name;
444         user_info->remote_host = gensec_get_remote_address(gensec_security);
445
446         user_info->password_state = AUTH_PASSWORD_RESPONSE;
447         user_info->password.response.lanman = ntlmssp_state->lm_resp;
448         user_info->password.response.lanman.data = talloc_steal(user_info, ntlmssp_state->lm_resp.data);
449         user_info->password.response.nt = ntlmssp_state->nt_resp;
450         user_info->password.response.nt.data = talloc_steal(user_info, ntlmssp_state->nt_resp.data);
451
452         if (auth_context->check_ntlm_password) {
453                 nt_status = auth_context->check_ntlm_password(auth_context,
454                                                               gensec_ntlmssp,
455                                                               user_info,
456                                                               &gensec_ntlmssp->server_returned_info,
457                                                               user_session_key, lm_session_key);
458         }
459         talloc_free(user_info);
460
461         if (!NT_STATUS_IS_OK(nt_status)) {
462                 DEBUG(5, (__location__ ": Checking NTLMSSP password for %s\\%s failed: %s\n", user_info->client.domain_name, user_info->client.account_name, nt_errstr(nt_status)));
463         }
464
465         NT_STATUS_NOT_OK_RETURN(nt_status);
466
467         talloc_steal(mem_ctx, user_session_key->data);
468         talloc_steal(mem_ctx, lm_session_key->data);
469
470         return nt_status;
471 }
472
473 /**
474  * Next state function for the Authenticate packet
475  * (after authentication - figures out the session keys etc)
476  *
477  * @param ntlmssp_state NTLMSSP State
478  * @return Errors or NT_STATUS_OK.
479  */
480
481 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security,
482                                         struct gensec_ntlmssp_context *gensec_ntlmssp,
483                                         struct ntlmssp_server_auth_state *state)
484 {
485         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
486         DATA_BLOB user_session_key = state->user_session_key;
487         DATA_BLOB lm_session_key = state->lm_session_key;
488         NTSTATUS nt_status = NT_STATUS_OK;
489         DATA_BLOB session_key = data_blob(NULL, 0);
490
491         dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
492         dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
493
494         /* Handle the different session key derivation for NTLM2 */
495         if (state->doing_ntlm2) {
496                 if (user_session_key.data && user_session_key.length == 16) {
497                         session_key = data_blob_talloc(ntlmssp_state,
498                                                        NULL, 16);
499                         hmac_md5(user_session_key.data, state->session_nonce,
500                                  sizeof(state->session_nonce), session_key.data);
501                         DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
502                         dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
503
504                 } else {
505                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
506                         session_key = data_blob_null;
507                 }
508         } else if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
509                 /* Ensure we can never get here on NTLMv2 */
510                 && (ntlmssp_state->nt_resp.length == 0 || ntlmssp_state->nt_resp.length == 24)) {
511
512                 if (lm_session_key.data && lm_session_key.length >= 8) {
513                         if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
514                                 session_key = data_blob_talloc(ntlmssp_state,
515                                                                NULL, 16);
516                                 if (session_key.data == NULL) {
517                                         return NT_STATUS_NO_MEMORY;
518                                 }
519                                 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
520                                                           session_key.data);
521                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
522                         } else {
523                                 static const uint8_t zeros[24] = {0, };
524                                 session_key = data_blob_talloc(
525                                         ntlmssp_state, NULL, 16);
526                                 if (session_key.data == NULL) {
527                                         return NT_STATUS_NO_MEMORY;
528                                 }
529                                 SMBsesskeygen_lm_sess_key(zeros, zeros,
530                                                           session_key.data);
531                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
532                         }
533                         dump_data_pw("LM session key:\n", session_key.data,
534                                      session_key.length);
535                 } else {
536                         /* LM Key not selected */
537                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
538
539                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
540                         session_key = data_blob_null;
541                 }
542
543         } else if (user_session_key.data) {
544                 session_key = user_session_key;
545                 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
546                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
547
548                 /* LM Key not selected */
549                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
550
551         } else if (lm_session_key.data) {
552                 /* Very weird to have LM key, but no user session key, but anyway.. */
553                 session_key = lm_session_key;
554                 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
555                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
556
557                 /* LM Key not selected */
558                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
559
560         } else {
561                 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
562                 session_key = data_blob_null;
563
564                 /* LM Key not selected */
565                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
566         }
567
568         /* With KEY_EXCH, the client supplies the proposed session key,
569            but encrypts it with the long-term key */
570         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
571                 if (!state->encrypted_session_key.data
572                     || state->encrypted_session_key.length != 16) {
573                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
574                                   (unsigned)state->encrypted_session_key.length));
575                         return NT_STATUS_INVALID_PARAMETER;
576                 } else if (!session_key.data || session_key.length != 16) {
577                         DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
578                                   (unsigned int)session_key.length));
579                         ntlmssp_state->session_key = session_key;
580                         talloc_steal(ntlmssp_state, session_key.data);
581                 } else {
582                         dump_data_pw("KEY_EXCH session key (enc):\n",
583                                      state->encrypted_session_key.data,
584                                      state->encrypted_session_key.length);
585                         arcfour_crypt(state->encrypted_session_key.data,
586                                       session_key.data,
587                                       state->encrypted_session_key.length);
588                         ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state,
589                                                                       state->encrypted_session_key.data,
590                                                                       state->encrypted_session_key.length);
591                         dump_data_pw("KEY_EXCH session key:\n",
592                                      state->encrypted_session_key.data,
593                                      state->encrypted_session_key.length);
594                 }
595         } else {
596                 ntlmssp_state->session_key = session_key;
597                 talloc_steal(ntlmssp_state, session_key.data);
598         }
599
600         if (ntlmssp_state->session_key.length) {
601                 nt_status = ntlmssp_sign_init(ntlmssp_state);
602         }
603
604         ntlmssp_state->expected_state = NTLMSSP_DONE;
605
606         return nt_status;
607 }
608
609
610 /**
611  * Next state function for the NTLMSSP Authenticate packet
612  *
613  * @param gensec_security GENSEC state
614  * @param out_mem_ctx Memory context for *out
615  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
616  * @param out The reply, as an allocated DATA_BLOB, caller to free.
617  * @return Errors or NT_STATUS_OK if authentication sucessful
618  */
619
620 NTSTATUS gensec_ntlmssp_server_auth(struct gensec_security *gensec_security,
621                                     TALLOC_CTX *out_mem_ctx,
622                                     const DATA_BLOB in, DATA_BLOB *out)
623 {
624         struct gensec_ntlmssp_context *gensec_ntlmssp =
625                 talloc_get_type_abort(gensec_security->private_data,
626                                       struct gensec_ntlmssp_context);
627         struct ntlmssp_server_auth_state *state;
628         NTSTATUS nt_status;
629
630         /* zero the outbound NTLMSSP packet */
631         *out = data_blob_null;
632
633         state = talloc_zero(gensec_ntlmssp, struct ntlmssp_server_auth_state);
634         if (state == NULL) {
635                 return NT_STATUS_NO_MEMORY;
636         }
637
638         nt_status = ntlmssp_server_preauth(gensec_security, gensec_ntlmssp, state, in);
639         if (!NT_STATUS_IS_OK(nt_status)) {
640                 TALLOC_FREE(state);
641                 return nt_status;
642         }
643
644         /*
645          * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
646          * is required (by "ntlm auth = no" and "lm auth = no" being set in the
647          * smb.conf file) and no NTLMv2 response was sent then the password check
648          * will fail here. JRA.
649          */
650
651         /* Finally, actually ask if the password is OK */
652         nt_status = ntlmssp_server_check_password(gensec_security, gensec_ntlmssp,
653                                                   state,
654                                                   &state->user_session_key,
655                                                   &state->lm_session_key);
656         if (!NT_STATUS_IS_OK(nt_status)) {
657                 TALLOC_FREE(state);
658                 return nt_status;
659         }
660
661         /* When we get more async in the auth code behind
662            ntlmssp_state->check_password, the ntlmssp_server_postpath
663            can be done in a callback */
664
665         nt_status = ntlmssp_server_postauth(gensec_security, gensec_ntlmssp, state);
666         TALLOC_FREE(state);
667         return nt_status;
668 }