4990f77f66f1fd2e49252ef0bf438d24fc6fa60e
[samba.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 <tevent.h>
25 #include "lib/util/tevent_ntstatus.h"
26 #include "lib/util/time_basic.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "auth/ntlmssp/ntlmssp_private.h"
29 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
30 #include "auth/ntlmssp/ntlmssp_ndr.h"
31 #include "../libcli/auth/libcli_auth.h"
32 #include "../lib/crypto/crypto.h"
33 #include "auth/gensec/gensec.h"
34 #include "auth/gensec/gensec_internal.h"
35 #include "auth/common_auth.h"
36 #include "param/param.h"
37 #include "param/loadparm.h"
38 #include "libcli/security/session.h"
39
40 /**
41  * Determine correct target name flags for reply, given server role
42  * and negotiated flags
43  *
44  * @param ntlmssp_state NTLMSSP State
45  * @param neg_flags The flags from the packet
46  * @param chal_flags The flags to be set in the reply packet
47  * @return The 'target name' string.
48  */
49
50 const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
51                                 uint32_t neg_flags, uint32_t *chal_flags)
52 {
53         if (neg_flags & NTLMSSP_REQUEST_TARGET) {
54                 *chal_flags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
55                 *chal_flags |= NTLMSSP_REQUEST_TARGET;
56                 if (ntlmssp_state->server.is_standalone) {
57                         *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
58                         return ntlmssp_state->server.netbios_name;
59                 } else {
60                         *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
61                         return ntlmssp_state->server.netbios_domain;
62                 };
63         } else {
64                 return "";
65         }
66 }
67
68 /**
69  * Next state function for the NTLMSSP Negotiate packet
70  *
71  * @param gensec_security GENSEC state
72  * @param out_mem_ctx Memory context for *out
73  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
74  * @param out The reply, as an allocated DATA_BLOB, caller to free.
75  * @return Errors or MORE_PROCESSING_REQUIRED if (normal) a reply is required.
76  */
77
78 NTSTATUS gensec_ntlmssp_server_negotiate(struct gensec_security *gensec_security,
79                                          TALLOC_CTX *out_mem_ctx,
80                                          const DATA_BLOB request, DATA_BLOB *reply)
81 {
82         struct gensec_ntlmssp_context *gensec_ntlmssp =
83                 talloc_get_type_abort(gensec_security->private_data,
84                                       struct gensec_ntlmssp_context);
85         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
86         struct auth4_context *auth_context = gensec_security->auth_context;
87         DATA_BLOB struct_blob;
88         uint32_t neg_flags = 0;
89         uint32_t ntlmssp_command, chal_flags;
90         uint8_t cryptkey[8];
91         const char *target_name;
92         NTSTATUS status;
93         struct timeval tv_now = timeval_current();
94         /*
95          * See [MS-NLMP]
96          *
97          * Windows NT 4.0, windows_2000: use 30 minutes,
98          * Windows XP, Windows Server 2003, Windows Vista,
99          * Windows Server 2008, Windows 7, and Windows Server 2008 R2
100          * use 36 hours.
101          *
102          * Newer systems doesn't check this, likely because the
103          * connectionless NTLMSSP is no longer supported.
104          *
105          * As we expect the AUTHENTICATION_MESSAGE to arrive
106          * directly after the NEGOTIATE_MESSAGE (typically less than
107          * as 1 second later). We use a hard timeout of 30 Minutes.
108          *
109          * We don't look at AUTHENTICATE_MESSAGE.NtChallengeResponse.TimeStamp
110          * instead we just remember our own time.
111          */
112         uint32_t max_lifetime = 30 * 60;
113         struct timeval tv_end = timeval_add(&tv_now, max_lifetime, 0);
114
115         /* parse the NTLMSSP packet */
116 #if 0
117         file_save("ntlmssp_negotiate.dat", request.data, request.length);
118 #endif
119
120         if (request.length) {
121                 if (request.length > UINT16_MAX) {
122                         DEBUG(1, ("ntlmssp_server_negotiate: reject large request of length %u\n",
123                                 (unsigned int)request.length));
124                         return NT_STATUS_INVALID_PARAMETER;
125                 }
126
127                 if ((request.length < 16) || !msrpc_parse(ntlmssp_state, &request, "Cdd",
128                                                           "NTLMSSP",
129                                                           &ntlmssp_command,
130                                                           &neg_flags)) {
131                         DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
132                                 (unsigned int)request.length));
133                         dump_data(2, request.data, request.length);
134                         return NT_STATUS_INVALID_PARAMETER;
135                 }
136                 debug_ntlmssp_flags(neg_flags);
137
138                 if (DEBUGLEVEL >= 10) {
139                         struct NEGOTIATE_MESSAGE *negotiate = talloc(
140                                 ntlmssp_state, struct NEGOTIATE_MESSAGE);
141                         if (negotiate != NULL) {
142                                 status = ntlmssp_pull_NEGOTIATE_MESSAGE(
143                                         &request, negotiate, negotiate);
144                                 if (NT_STATUS_IS_OK(status)) {
145                                         NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
146                                                         negotiate);
147                                 }
148                                 TALLOC_FREE(negotiate);
149                         }
150                 }
151         }
152
153         status = ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, "negotiate");
154         if (!NT_STATUS_IS_OK(status)){
155                 return status;
156         }
157
158         /* Ask our caller what challenge they would like in the packet */
159         if (auth_context->get_ntlm_challenge) {
160                 status = auth_context->get_ntlm_challenge(auth_context, cryptkey);
161                 if (!NT_STATUS_IS_OK(status)) {
162                         DEBUG(1, ("gensec_ntlmssp_server_negotiate: failed to get challenge: %s\n",
163                                   nt_errstr(status)));
164                         return status;
165                 }
166         } else {
167                 DEBUG(1, ("gensec_ntlmssp_server_negotiate: backend doesn't give a challenge\n"));
168                 return NT_STATUS_NOT_IMPLEMENTED;
169         }
170
171         /* The flags we send back are not just the negotiated flags,
172          * they are also 'what is in this packet'.  Therfore, we
173          * operate on 'chal_flags' from here on
174          */
175
176         chal_flags = ntlmssp_state->neg_flags;
177         ntlmssp_state->server.challenge_endtime = timeval_to_nttime(&tv_end);
178
179         /* get the right name to fill in as 'target' */
180         target_name = ntlmssp_target_name(ntlmssp_state,
181                                           neg_flags, &chal_flags);
182         if (target_name == NULL)
183                 return NT_STATUS_INVALID_PARAMETER;
184
185         ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
186         ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
187                                                         cryptkey, 8);
188
189         /* This creates the 'blob' of names that appears at the end of the packet */
190         if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
191                 enum ndr_err_code err;
192                 struct AV_PAIR *pairs = NULL;
193                 uint32_t count = 5;
194
195                 pairs = talloc_zero_array(ntlmssp_state, struct AV_PAIR, count + 1);
196                 if (pairs == NULL) {
197                         return NT_STATUS_NO_MEMORY;
198                 }
199
200                 pairs[0].AvId                   = MsvAvNbDomainName;
201                 pairs[0].Value.AvNbDomainName   = target_name;
202
203                 pairs[1].AvId                   = MsvAvNbComputerName;
204                 pairs[1].Value.AvNbComputerName = ntlmssp_state->server.netbios_name;
205
206                 pairs[2].AvId                   = MsvAvDnsDomainName;
207                 pairs[2].Value.AvDnsDomainName  = ntlmssp_state->server.dns_domain;
208
209                 pairs[3].AvId                   = MsvAvDnsComputerName;
210                 pairs[3].Value.AvDnsComputerName= ntlmssp_state->server.dns_name;
211
212                 if (!ntlmssp_state->force_old_spnego) {
213                         pairs[4].AvId                   = MsvAvTimestamp;
214                         pairs[4].Value.AvTimestamp      =
215                                                 timeval_to_nttime(&tv_now);
216                         count += 1;
217
218                         pairs[5].AvId                   = MsvAvEOL;
219                 } else {
220                         pairs[4].AvId                   = MsvAvEOL;
221                 }
222
223                 ntlmssp_state->server.av_pair_list.count = count;
224                 ntlmssp_state->server.av_pair_list.pair = pairs;
225
226                 err = ndr_push_struct_blob(&struct_blob,
227                                         ntlmssp_state,
228                                         &ntlmssp_state->server.av_pair_list,
229                                         (ndr_push_flags_fn_t)ndr_push_AV_PAIR_LIST);
230                 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
231                         return NT_STATUS_NO_MEMORY;
232                 }
233         } else {
234                 struct_blob = data_blob_null;
235         }
236
237         {
238                 /* Marshal the packet in the right format, be it unicode or ASCII */
239                 const char *gen_string;
240                 const DATA_BLOB version_blob = ntlmssp_version_blob();
241
242                 if (ntlmssp_state->unicode) {
243                         gen_string = "CdUdbddBb";
244                 } else {
245                         gen_string = "CdAdbddBb";
246                 }
247
248                 status = msrpc_gen(out_mem_ctx, reply, gen_string,
249                         "NTLMSSP",
250                         NTLMSSP_CHALLENGE,
251                         target_name,
252                         chal_flags,
253                         cryptkey, 8,
254                         0, 0,
255                         struct_blob.data, struct_blob.length,
256                         version_blob.data, version_blob.length);
257
258                 if (!NT_STATUS_IS_OK(status)) {
259                         data_blob_free(&struct_blob);
260                         return status;
261                 }
262
263                 if (DEBUGLEVEL >= 10) {
264                         struct CHALLENGE_MESSAGE *challenge = talloc(
265                                 ntlmssp_state, struct CHALLENGE_MESSAGE);
266                         if (challenge != NULL) {
267                                 challenge->NegotiateFlags = chal_flags;
268                                 status = ntlmssp_pull_CHALLENGE_MESSAGE(
269                                         reply, challenge, challenge);
270                                 if (NT_STATUS_IS_OK(status)) {
271                                         NDR_PRINT_DEBUG(CHALLENGE_MESSAGE,
272                                                         challenge);
273                                 }
274                                 TALLOC_FREE(challenge);
275                         }
276                 }
277         }
278
279         data_blob_free(&struct_blob);
280
281         ntlmssp_state->negotiate_blob = data_blob_dup_talloc(ntlmssp_state,
282                                                              request);
283         if (ntlmssp_state->negotiate_blob.length != request.length) {
284                 return NT_STATUS_NO_MEMORY;
285         }
286
287         ntlmssp_state->challenge_blob = data_blob_dup_talloc(ntlmssp_state,
288                                                              *reply);
289         if (ntlmssp_state->challenge_blob.length != reply->length) {
290                 return NT_STATUS_NO_MEMORY;
291         }
292
293         ntlmssp_state->expected_state = NTLMSSP_AUTH;
294
295         return NT_STATUS_MORE_PROCESSING_REQUIRED;
296 }
297
298 struct ntlmssp_server_auth_state {
299         struct auth_usersupplied_info *user_info;
300         DATA_BLOB user_session_key;
301         DATA_BLOB lm_session_key;
302         /* internal variables used by KEY_EXCH (client-supplied user session key */
303         DATA_BLOB encrypted_session_key;
304         bool doing_ntlm2;
305         /* internal variables used by NTLM2 */
306         uint8_t session_nonce[16];
307 };
308
309 static NTSTATUS ntlmssp_server_preauth(struct gensec_security *gensec_security,
310                                        struct gensec_ntlmssp_context *gensec_ntlmssp,
311                                        struct ntlmssp_server_auth_state *state,
312                                        const DATA_BLOB request);
313 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security,
314                                         struct gensec_ntlmssp_context *gensec_ntlmssp,
315                                         struct ntlmssp_server_auth_state *state,
316                                         DATA_BLOB request);
317
318 struct tevent_req *ntlmssp_server_auth_send(TALLOC_CTX *mem_ctx,
319                                             struct tevent_context *ev,
320                                             struct gensec_security *gensec_security,
321                                             const DATA_BLOB in)
322 {
323         struct gensec_ntlmssp_context *gensec_ntlmssp =
324                 talloc_get_type_abort(gensec_security->private_data,
325                                       struct gensec_ntlmssp_context);
326         struct auth4_context *auth_context = gensec_security->auth_context;
327         struct tevent_req *req = NULL;
328         struct ntlmssp_server_auth_state *state = NULL;
329         uint8_t authoritative = 0;
330         NTSTATUS status;
331
332         req = tevent_req_create(mem_ctx, &state,
333                                 struct ntlmssp_server_auth_state);
334         if (req == NULL) {
335                 return NULL;
336         }
337
338         status = ntlmssp_server_preauth(gensec_security,
339                                         gensec_ntlmssp,
340                                         state, in);
341         if (tevent_req_nterror(req, status)) {
342                 return tevent_req_post(req, ev);
343         }
344
345         if (auth_context->check_ntlm_password == NULL) {
346                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
347                 return tevent_req_post(req, ev);
348         }
349
350         status = auth_context->check_ntlm_password(auth_context,
351                                                    gensec_ntlmssp,
352                                                    state->user_info,
353                                                    &authoritative,
354                                                    &gensec_ntlmssp->server_returned_info,
355                                                    &state->user_session_key,
356                                                    &state->lm_session_key);
357         if (!NT_STATUS_IS_OK(status)) {
358                 DBG_INFO("Checking NTLMSSP password for %s\\%s failed: %s\n",
359                          state->user_info->client.domain_name,
360                          state->user_info->client.account_name,
361                          nt_errstr(status));
362         }
363         if (tevent_req_nterror(req, status)) {
364                 return tevent_req_post(req, ev);
365         }
366         talloc_steal(state, state->user_session_key.data);
367         talloc_steal(state, state->lm_session_key.data);
368
369         status = ntlmssp_server_postauth(gensec_security,
370                                          gensec_ntlmssp,
371                                          state, in);
372         if (tevent_req_nterror(req, status)) {
373                 return tevent_req_post(req, ev);
374         }
375
376         tevent_req_done(req);
377         return tevent_req_post(req, ev);
378 }
379
380 /**
381  * Next state function for the Authenticate packet
382  *
383  * @param ntlmssp_state NTLMSSP State
384  * @param request The request, as a DATA_BLOB
385  * @return Errors or NT_STATUS_OK.
386  */
387
388 static NTSTATUS ntlmssp_server_preauth(struct gensec_security *gensec_security,
389                                        struct gensec_ntlmssp_context *gensec_ntlmssp,
390                                        struct ntlmssp_server_auth_state *state,
391                                        const DATA_BLOB request)
392 {
393         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
394         struct auth4_context *auth_context = gensec_security->auth_context;
395         struct auth_usersupplied_info *user_info = NULL;
396         uint32_t ntlmssp_command, auth_flags;
397         NTSTATUS nt_status;
398         const unsigned int version_len = 8;
399         DATA_BLOB version_blob = data_blob_null;
400         const unsigned int mic_len = NTLMSSP_MIC_SIZE;
401         DATA_BLOB mic_blob = data_blob_null;
402         uint8_t session_nonce_hash[16];
403         const char *parse_string;
404         bool ok;
405         struct timeval endtime;
406         bool expired = false;
407
408 #if 0
409         file_save("ntlmssp_auth.dat", request.data, request.length);
410 #endif
411
412         if (ntlmssp_state->unicode) {
413                 parse_string = "CdBBUUUBdbb";
414         } else {
415                 parse_string = "CdBBAAABdbb";
416         }
417
418         /* zero these out */
419         data_blob_free(&ntlmssp_state->session_key);
420         data_blob_free(&ntlmssp_state->lm_resp);
421         data_blob_free(&ntlmssp_state->nt_resp);
422
423         ntlmssp_state->user = NULL;
424         ntlmssp_state->domain = NULL;
425         ntlmssp_state->client.netbios_name = NULL;
426
427         /* now the NTLMSSP encoded auth hashes */
428         ok = msrpc_parse(ntlmssp_state, &request, parse_string,
429                          "NTLMSSP",
430                          &ntlmssp_command,
431                          &ntlmssp_state->lm_resp,
432                          &ntlmssp_state->nt_resp,
433                          &ntlmssp_state->domain,
434                          &ntlmssp_state->user,
435                          &ntlmssp_state->client.netbios_name,
436                          &state->encrypted_session_key,
437                          &auth_flags,
438                          &version_blob, version_len,
439                          &mic_blob, mic_len);
440         if (!ok) {
441                 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
442                 dump_data(10, request.data, request.length);
443
444                 data_blob_free(&version_blob);
445                 data_blob_free(&mic_blob);
446
447                 if (ntlmssp_state->unicode) {
448                         parse_string = "CdBBUUUBd";
449                 } else {
450                         parse_string = "CdBBAAABd";
451                 }
452
453                 ok = msrpc_parse(ntlmssp_state, &request, parse_string,
454                                  "NTLMSSP",
455                                  &ntlmssp_command,
456                                  &ntlmssp_state->lm_resp,
457                                  &ntlmssp_state->nt_resp,
458                                  &ntlmssp_state->domain,
459                                  &ntlmssp_state->user,
460                                  &ntlmssp_state->client.netbios_name,
461                                  &state->encrypted_session_key,
462                                  &auth_flags);
463         }
464
465         if (!ok) {
466                 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
467                 dump_data(10, request.data, request.length);
468
469                 /* zero this out */
470                 data_blob_free(&state->encrypted_session_key);
471                 auth_flags = 0;
472
473                 /* Try again with a shorter string (Win9X truncates this packet) */
474                 if (ntlmssp_state->unicode) {
475                         parse_string = "CdBBUUU";
476                 } else {
477                         parse_string = "CdBBAAA";
478                 }
479
480                 /* now the NTLMSSP encoded auth hashes */
481                 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
482                                  "NTLMSSP",
483                                  &ntlmssp_command,
484                                  &ntlmssp_state->lm_resp,
485                                  &ntlmssp_state->nt_resp,
486                                  &ntlmssp_state->domain,
487                                  &ntlmssp_state->user,
488                                  &ntlmssp_state->client.netbios_name)) {
489                         DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
490                         dump_data(2, request.data, request.length);
491
492                         return NT_STATUS_INVALID_PARAMETER;
493                 }
494         }
495
496         talloc_steal(state, state->encrypted_session_key.data);
497
498         if (auth_flags != 0) {
499                 nt_status = ntlmssp_handle_neg_flags(ntlmssp_state,
500                                                      auth_flags,
501                                                      "authenticate");
502                 if (!NT_STATUS_IS_OK(nt_status)){
503                         return nt_status;
504                 }
505         }
506
507         if (DEBUGLEVEL >= 10) {
508                 struct AUTHENTICATE_MESSAGE *authenticate = talloc(
509                         ntlmssp_state, struct AUTHENTICATE_MESSAGE);
510                 if (authenticate != NULL) {
511                         NTSTATUS status;
512                         authenticate->NegotiateFlags = auth_flags;
513                         status = ntlmssp_pull_AUTHENTICATE_MESSAGE(
514                                 &request, authenticate, authenticate);
515                         if (NT_STATUS_IS_OK(status)) {
516                                 NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE,
517                                                 authenticate);
518                         }
519                         TALLOC_FREE(authenticate);
520                 }
521         }
522
523         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
524                  ntlmssp_state->user, ntlmssp_state->domain,
525                  ntlmssp_state->client.netbios_name,
526                  (unsigned long)ntlmssp_state->lm_resp.length,
527                  (unsigned long)ntlmssp_state->nt_resp.length));
528
529 #if 0
530         file_save("nthash1.dat",  &ntlmssp_state->nt_resp.data,  &ntlmssp_state->nt_resp.length);
531         file_save("lmhash1.dat",  &ntlmssp_state->lm_resp.data,  &ntlmssp_state->lm_resp.length);
532 #endif
533
534         if (ntlmssp_state->nt_resp.length > 24) {
535                 struct NTLMv2_RESPONSE v2_resp;
536                 enum ndr_err_code err;
537                 uint32_t i = 0;
538                 uint32_t count = 0;
539                 const struct AV_PAIR *flags = NULL;
540                 const struct AV_PAIR *eol = NULL;
541                 uint32_t av_flags = 0;
542
543                 err = ndr_pull_struct_blob(&ntlmssp_state->nt_resp,
544                                         ntlmssp_state,
545                                         &v2_resp,
546                                         (ndr_pull_flags_fn_t)ndr_pull_NTLMv2_RESPONSE);
547                 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
548                         nt_status = ndr_map_error2ntstatus(err);
549                         DEBUG(1,("%s: failed to parse NTLMv2_RESPONSE of length %zu for "
550                                  "user=[%s] domain=[%s] workstation=[%s] - %s %s\n",
551                                  __func__, ntlmssp_state->nt_resp.length,
552                                  ntlmssp_state->user, ntlmssp_state->domain,
553                                  ntlmssp_state->client.netbios_name,
554                                  ndr_errstr(err), nt_errstr(nt_status)));
555                         return nt_status;
556                 }
557
558                 if (DEBUGLVL(10)) {
559                         NDR_PRINT_DEBUG(NTLMv2_RESPONSE, &v2_resp);
560                 }
561
562                 eol = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
563                                           MsvAvEOL);
564                 if (eol == NULL) {
565                         DEBUG(1,("%s: missing MsvAvEOL for "
566                                  "user=[%s] domain=[%s] workstation=[%s]\n",
567                                  __func__, ntlmssp_state->user, ntlmssp_state->domain,
568                                  ntlmssp_state->client.netbios_name));
569                         return NT_STATUS_INVALID_PARAMETER;
570                 }
571
572                 flags = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
573                                             MsvAvFlags);
574                 if (flags != NULL) {
575                         av_flags = flags->Value.AvFlags;
576                 }
577
578                 if (av_flags & NTLMSSP_AVFLAG_MIC_IN_AUTHENTICATE_MESSAGE) {
579                         if (mic_blob.length != NTLMSSP_MIC_SIZE) {
580                                 DEBUG(1,("%s: mic_blob.length[%u] for "
581                                          "user=[%s] domain=[%s] workstation=[%s]\n",
582                                          __func__,
583                                          (unsigned)mic_blob.length,
584                                          ntlmssp_state->user,
585                                          ntlmssp_state->domain,
586                                          ntlmssp_state->client.netbios_name));
587                                 return NT_STATUS_INVALID_PARAMETER;
588                         }
589
590                         if (request.length <
591                             (NTLMSSP_MIC_OFFSET + NTLMSSP_MIC_SIZE))
592                         {
593                                 DEBUG(1,("%s: missing MIC "
594                                          "request.length[%u] for "
595                                          "user=[%s] domain=[%s] workstation=[%s]\n",
596                                          __func__,
597                                          (unsigned)request.length,
598                                          ntlmssp_state->user,
599                                          ntlmssp_state->domain,
600                                          ntlmssp_state->client.netbios_name));
601                                 return NT_STATUS_INVALID_PARAMETER;
602                         }
603
604                         ntlmssp_state->new_spnego = true;
605                 }
606
607                 count = ntlmssp_state->server.av_pair_list.count;
608                 if (v2_resp.Challenge.AvPairs.count < count) {
609                         return NT_STATUS_INVALID_PARAMETER;
610                 }
611
612                 for (i = 0; i < count; i++) {
613                         const struct AV_PAIR *sp =
614                                 &ntlmssp_state->server.av_pair_list.pair[i];
615                         const struct AV_PAIR *cp = NULL;
616
617                         if (sp->AvId == MsvAvEOL) {
618                                 continue;
619                         }
620
621                         cp = ndr_ntlmssp_find_av(&v2_resp.Challenge.AvPairs,
622                                                  sp->AvId);
623                         if (cp == NULL) {
624                                 DEBUG(1,("%s: AvId 0x%x missing for"
625                                          "user=[%s] domain=[%s] "
626                                          "workstation=[%s]\n",
627                                          __func__,
628                                          (unsigned)sp->AvId,
629                                          ntlmssp_state->user,
630                                          ntlmssp_state->domain,
631                                          ntlmssp_state->client.netbios_name));
632                                 return NT_STATUS_INVALID_PARAMETER;
633                         }
634
635                         switch (cp->AvId) {
636 #define CASE_STRING(v) case Msv ## v: do { \
637         int cmp; \
638         if (sp->Value.v == NULL) { \
639                 return NT_STATUS_INTERNAL_ERROR; \
640         } \
641         if (cp->Value.v == NULL) { \
642                 DEBUG(1,("%s: invalid %s " \
643                          "got[%s] expect[%s] for " \
644                          "user=[%s] domain=[%s] workstation=[%s]\n", \
645                          __func__, #v, \
646                          cp->Value.v, \
647                          sp->Value.v, \
648                          ntlmssp_state->user, \
649                          ntlmssp_state->domain, \
650                          ntlmssp_state->client.netbios_name)); \
651                 return NT_STATUS_INVALID_PARAMETER; \
652         } \
653         cmp = strcmp(cp->Value.v, sp->Value.v); \
654         if (cmp != 0) { \
655                 DEBUG(1,("%s: invalid %s " \
656                          "got[%s] expect[%s] for " \
657                          "user=[%s] domain=[%s] workstation=[%s]\n", \
658                          __func__, #v, \
659                          cp->Value.v, \
660                          sp->Value.v, \
661                          ntlmssp_state->user, \
662                          ntlmssp_state->domain, \
663                          ntlmssp_state->client.netbios_name)); \
664                 return NT_STATUS_INVALID_PARAMETER; \
665         } \
666 } while(0); break
667                         CASE_STRING(AvNbComputerName);
668                         CASE_STRING(AvNbDomainName);
669                         CASE_STRING(AvDnsComputerName);
670                         CASE_STRING(AvDnsDomainName);
671                         CASE_STRING(AvDnsTreeName);
672                         case MsvAvTimestamp:
673                                 if (cp->Value.AvTimestamp != sp->Value.AvTimestamp) {
674                                         struct timeval ct;
675                                         struct timeval st;
676                                         struct timeval_buf tmp1;
677                                         struct timeval_buf tmp2;
678
679                                         nttime_to_timeval(&ct,
680                                                           cp->Value.AvTimestamp);
681                                         nttime_to_timeval(&st,
682                                                           sp->Value.AvTimestamp);
683
684                                         DEBUG(1,("%s: invalid AvTimestamp "
685                                                  "got[%s] expect[%s] for "
686                                                  "user=[%s] domain=[%s] "
687                                                  "workstation=[%s]\n",
688                                                  __func__,
689                                                  timeval_str_buf(&ct, false,
690                                                                  true, &tmp1),
691                                                  timeval_str_buf(&st, false,
692                                                                  true, &tmp2),
693                                                  ntlmssp_state->user,
694                                                  ntlmssp_state->domain,
695                                                  ntlmssp_state->client.netbios_name));
696                                         return NT_STATUS_INVALID_PARAMETER;
697                                 }
698                                 break;
699                         default:
700                                 /*
701                                  * This can't happen as we control
702                                  * ntlmssp_state->server.av_pair_list
703                                  */
704                                 return NT_STATUS_INTERNAL_ERROR;
705                         }
706                 }
707         }
708
709         nttime_to_timeval(&endtime, ntlmssp_state->server.challenge_endtime);
710         expired = timeval_expired(&endtime);
711         if (expired) {
712                 struct timeval_buf tmp;
713                 DEBUG(1,("%s: challenge invalid (expired %s) for "
714                          "user=[%s] domain=[%s] workstation=[%s]\n",
715                          __func__,
716                          timeval_str_buf(&endtime, false, true, &tmp),
717                          ntlmssp_state->user, ntlmssp_state->domain,
718                          ntlmssp_state->client.netbios_name));
719                 return NT_STATUS_INVALID_PARAMETER;
720         }
721
722         /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
723            client challenge
724
725            However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
726         */
727         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
728                 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
729                         MD5_CTX md5_session_nonce_ctx;
730                         state->doing_ntlm2 = true;
731
732                         memcpy(state->session_nonce, ntlmssp_state->internal_chal.data, 8);
733                         memcpy(&state->session_nonce[8], ntlmssp_state->lm_resp.data, 8);
734
735                         SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
736
737                         MD5Init(&md5_session_nonce_ctx);
738                         MD5Update(&md5_session_nonce_ctx, state->session_nonce, 16);
739                         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
740
741                         /* LM response is no longer useful */
742                         data_blob_free(&ntlmssp_state->lm_resp);
743
744                         /* We changed the effective challenge - set it */
745                         if (auth_context->set_ntlm_challenge) {
746                                 nt_status = auth_context->set_ntlm_challenge(auth_context,
747                                                                              session_nonce_hash,
748                                                                              "NTLMSSP callback (NTLM2)");
749                                 if (!NT_STATUS_IS_OK(nt_status)) {
750                                         DEBUG(1, ("gensec_ntlmssp_server_negotiate: failed to get challenge: %s\n",
751                                                   nt_errstr(nt_status)));
752                                         return nt_status;
753                                 }
754                         } else {
755                                 DEBUG(1, ("gensec_ntlmssp_server_negotiate: backend doesn't have facility for challenge to be set\n"));
756
757                                 return NT_STATUS_NOT_IMPLEMENTED;
758                         }
759
760                         /* LM Key is incompatible. */
761                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
762                 }
763         }
764
765         user_info = talloc_zero(state, struct auth_usersupplied_info);
766         if (!user_info) {
767                 return NT_STATUS_NO_MEMORY;
768         }
769
770         user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
771         user_info->flags = 0;
772         user_info->mapped_state = false;
773         user_info->client.account_name = ntlmssp_state->user;
774         user_info->client.domain_name = ntlmssp_state->domain;
775         user_info->workstation_name = ntlmssp_state->client.netbios_name;
776         user_info->remote_host = gensec_get_remote_address(gensec_security);
777         user_info->local_host = gensec_get_local_address(gensec_security);
778         user_info->service_description
779                 = gensec_get_target_service_description(gensec_security);
780
781         /*
782          * This will just be the string "NTLMSSP" from
783          * gensec_ntlmssp_final_auth_type, but ensures it stays in sync
784          * with the same use in the authorization logging triggered by
785          * gensec_session_info() later
786          */
787         user_info->auth_description = gensec_final_auth_type(gensec_security);
788
789         user_info->password_state = AUTH_PASSWORD_RESPONSE;
790         user_info->password.response.lanman = ntlmssp_state->lm_resp;
791         user_info->password.response.nt = ntlmssp_state->nt_resp;
792
793         state->user_info = user_info;
794         return NT_STATUS_OK;
795 }
796
797 /**
798  * Next state function for the Authenticate packet
799  * (after authentication - figures out the session keys etc)
800  *
801  * @param ntlmssp_state NTLMSSP State
802  * @return Errors or NT_STATUS_OK.
803  */
804
805 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security,
806                                         struct gensec_ntlmssp_context *gensec_ntlmssp,
807                                         struct ntlmssp_server_auth_state *state,
808                                         DATA_BLOB request)
809 {
810         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
811         struct auth4_context *auth_context = gensec_security->auth_context;
812         DATA_BLOB user_session_key = state->user_session_key;
813         DATA_BLOB lm_session_key = state->lm_session_key;
814         NTSTATUS nt_status = NT_STATUS_OK;
815         DATA_BLOB session_key = data_blob(NULL, 0);
816         struct auth_session_info *session_info = NULL;
817
818         TALLOC_FREE(state->user_info);
819
820         if (lpcfg_map_to_guest(gensec_security->settings->lp_ctx) != NEVER_MAP_TO_GUEST
821             && auth_context->generate_session_info != NULL)
822         {
823                 NTSTATUS tmp_status;
824
825                 /*
826                  * We need to check if the auth is anonymous or mapped to guest
827                  */
828                 tmp_status = auth_context->generate_session_info(auth_context, state,
829                                                                  gensec_ntlmssp->server_returned_info,
830                                                                  gensec_ntlmssp->ntlmssp_state->user,
831                                                                  AUTH_SESSION_INFO_SIMPLE_PRIVILEGES,
832                                                                  &session_info);
833                 if (!NT_STATUS_IS_OK(tmp_status)) {
834                         /*
835                          * We don't care about failures,
836                          * the worst result is that we try MIC checking
837                          * for a map to guest authentication.
838                          */
839                         TALLOC_FREE(session_info);
840                 }
841         }
842
843         if (session_info != NULL) {
844                 if (security_session_user_level(session_info, NULL) < SECURITY_USER) {
845                         /*
846                          * Anonymous and GUEST are not secure anyway.
847                          * avoid new_spnego and MIC checking.
848                          */
849                         ntlmssp_state->new_spnego = false;
850                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
851                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SEAL;
852                 }
853                 TALLOC_FREE(session_info);
854         }
855
856         dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
857         dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
858
859         /* Handle the different session key derivation for NTLM2 */
860         if (state->doing_ntlm2) {
861                 if (user_session_key.data && user_session_key.length == 16) {
862                         session_key = data_blob_talloc(ntlmssp_state,
863                                                        NULL, 16);
864                         hmac_md5(user_session_key.data, state->session_nonce,
865                                  sizeof(state->session_nonce), session_key.data);
866                         DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
867                         dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
868
869                 } else {
870                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
871                         session_key = data_blob_null;
872                 }
873         } else if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
874                 /* Ensure we can never get here on NTLMv2 */
875                 && (ntlmssp_state->nt_resp.length == 0 || ntlmssp_state->nt_resp.length == 24)) {
876
877                 if (lm_session_key.data && lm_session_key.length >= 8) {
878                         if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
879                                 session_key = data_blob_talloc(ntlmssp_state,
880                                                                NULL, 16);
881                                 if (session_key.data == NULL) {
882                                         return NT_STATUS_NO_MEMORY;
883                                 }
884                                 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
885                                                           session_key.data);
886                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
887                         } else {
888                                 static const uint8_t zeros[24] = {0, };
889                                 session_key = data_blob_talloc(
890                                         ntlmssp_state, NULL, 16);
891                                 if (session_key.data == NULL) {
892                                         return NT_STATUS_NO_MEMORY;
893                                 }
894                                 SMBsesskeygen_lm_sess_key(zeros, zeros,
895                                                           session_key.data);
896                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
897                         }
898                         dump_data_pw("LM session key:\n", session_key.data,
899                                      session_key.length);
900                 } else {
901                         /* LM Key not selected */
902                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
903
904                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
905                         session_key = data_blob_null;
906                 }
907
908         } else if (user_session_key.data) {
909                 session_key = user_session_key;
910                 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
911                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
912
913                 /* LM Key not selected */
914                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
915
916         } else if (lm_session_key.data) {
917                 /* Very weird to have LM key, but no user session key, but anyway.. */
918                 session_key = lm_session_key;
919                 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
920                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
921
922                 /* LM Key not selected */
923                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
924
925         } else {
926                 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
927                 session_key = data_blob_null;
928
929                 /* LM Key not selected */
930                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
931         }
932
933         /* With KEY_EXCH, the client supplies the proposed session key,
934            but encrypts it with the long-term key */
935         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
936                 if (!state->encrypted_session_key.data
937                     || state->encrypted_session_key.length != 16) {
938                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
939                                   (unsigned)state->encrypted_session_key.length));
940                         return NT_STATUS_INVALID_PARAMETER;
941                 } else if (!session_key.data || session_key.length != 16) {
942                         DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
943                                   (unsigned int)session_key.length));
944                         ntlmssp_state->session_key = session_key;
945                         talloc_steal(ntlmssp_state, session_key.data);
946                 } else {
947                         dump_data_pw("KEY_EXCH session key (enc):\n",
948                                      state->encrypted_session_key.data,
949                                      state->encrypted_session_key.length);
950                         arcfour_crypt(state->encrypted_session_key.data,
951                                       session_key.data,
952                                       state->encrypted_session_key.length);
953                         ntlmssp_state->session_key = data_blob_talloc(ntlmssp_state,
954                                                                       state->encrypted_session_key.data,
955                                                                       state->encrypted_session_key.length);
956                         dump_data_pw("KEY_EXCH session key:\n",
957                                      state->encrypted_session_key.data,
958                                      state->encrypted_session_key.length);
959                 }
960         } else {
961                 ntlmssp_state->session_key = session_key;
962                 talloc_steal(ntlmssp_state, session_key.data);
963         }
964
965         if (ntlmssp_state->new_spnego) {
966                 HMACMD5Context ctx;
967                 uint8_t mic_buffer[NTLMSSP_MIC_SIZE] = { 0, };
968                 int cmp;
969
970                 hmac_md5_init_limK_to_64(ntlmssp_state->session_key.data,
971                                          ntlmssp_state->session_key.length,
972                                          &ctx);
973
974                 hmac_md5_update(ntlmssp_state->negotiate_blob.data,
975                                 ntlmssp_state->negotiate_blob.length,
976                                 &ctx);
977                 hmac_md5_update(ntlmssp_state->challenge_blob.data,
978                                 ntlmssp_state->challenge_blob.length,
979                                 &ctx);
980
981                 /* checked were we set ntlmssp_state->new_spnego */
982                 SMB_ASSERT(request.length >
983                            (NTLMSSP_MIC_OFFSET + NTLMSSP_MIC_SIZE));
984
985                 hmac_md5_update(request.data, NTLMSSP_MIC_OFFSET, &ctx);
986                 hmac_md5_update(mic_buffer, NTLMSSP_MIC_SIZE, &ctx);
987                 hmac_md5_update(request.data +
988                                 (NTLMSSP_MIC_OFFSET + NTLMSSP_MIC_SIZE),
989                                 request.length -
990                                 (NTLMSSP_MIC_OFFSET + NTLMSSP_MIC_SIZE),
991                                 &ctx);
992                 hmac_md5_final(mic_buffer, &ctx);
993
994                 cmp = memcmp(request.data + NTLMSSP_MIC_OFFSET,
995                              mic_buffer, NTLMSSP_MIC_SIZE);
996                 if (cmp != 0) {
997                         DEBUG(1,("%s: invalid NTLMSSP_MIC for "
998                                  "user=[%s] domain=[%s] workstation=[%s]\n",
999                                  __func__,
1000                                  ntlmssp_state->user,
1001                                  ntlmssp_state->domain,
1002                                  ntlmssp_state->client.netbios_name));
1003                         dump_data(1, request.data + NTLMSSP_MIC_OFFSET,
1004                                   NTLMSSP_MIC_SIZE);
1005                         dump_data(1, mic_buffer,
1006                                   NTLMSSP_MIC_SIZE);
1007                         return NT_STATUS_INVALID_PARAMETER;
1008                 }
1009         }
1010
1011         data_blob_free(&ntlmssp_state->negotiate_blob);
1012         data_blob_free(&ntlmssp_state->challenge_blob);
1013
1014         if (gensec_ntlmssp_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
1015                 nt_status = ntlmssp_sign_init(ntlmssp_state);
1016         }
1017
1018         data_blob_clear_free(&ntlmssp_state->internal_chal);
1019         data_blob_clear_free(&ntlmssp_state->chal);
1020         data_blob_clear_free(&ntlmssp_state->lm_resp);
1021         data_blob_clear_free(&ntlmssp_state->nt_resp);
1022
1023         ntlmssp_state->expected_state = NTLMSSP_DONE;
1024
1025         return nt_status;
1026 }
1027
1028 NTSTATUS ntlmssp_server_auth_recv(struct tevent_req *req,
1029                                   TALLOC_CTX *out_mem_ctx,
1030                                   DATA_BLOB *out)
1031 {
1032         NTSTATUS status;
1033
1034         *out = data_blob_null;
1035
1036         if (tevent_req_is_nterror(req, &status)) {
1037                 tevent_req_received(req);
1038                 return status;
1039         }
1040
1041         tevent_req_received(req);
1042         return NT_STATUS_OK;
1043 }