s3: Add NTLMSSP_FEATURE_CCACHE
[ira/wip.git] / source3 / libsmb / ntlmssp.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, server side
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett 2001-2003
8    Copyright (C) Andrew Bartlett 2005 (Updated from gensec).
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 "ntlmssp.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
28 #include "../libcli/auth/ntlmssp_ndr.h"
29
30 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
31                                        DATA_BLOB reply, DATA_BLOB *next_request);
32 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
33                                          const DATA_BLOB in, DATA_BLOB *out);
34 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
35                                          const DATA_BLOB reply, DATA_BLOB *next_request);
36 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
37                                     const DATA_BLOB request, DATA_BLOB *reply);
38
39 /**
40  * Callbacks for NTLMSSP - for both client and server operating modes
41  *
42  */
43
44 static const struct ntlmssp_callbacks {
45         enum ntlmssp_role role;
46         enum ntlmssp_message_type ntlmssp_command;
47         NTSTATUS (*fn)(struct ntlmssp_state *ntlmssp_state,
48                        DATA_BLOB in, DATA_BLOB *out);
49 } ntlmssp_callbacks[] = {
50         {NTLMSSP_CLIENT, NTLMSSP_INITIAL, ntlmssp_client_initial},
51         {NTLMSSP_SERVER, NTLMSSP_NEGOTIATE, ntlmssp_server_negotiate},
52         {NTLMSSP_CLIENT, NTLMSSP_CHALLENGE, ntlmssp_client_challenge},
53         {NTLMSSP_SERVER, NTLMSSP_AUTH, ntlmssp_server_auth},
54         {NTLMSSP_CLIENT, NTLMSSP_UNKNOWN, NULL},
55         {NTLMSSP_SERVER, NTLMSSP_UNKNOWN, NULL}
56 };
57
58
59 /**
60  * Print out the NTLMSSP flags for debugging
61  * @param neg_flags The flags from the packet
62  */
63
64 void debug_ntlmssp_flags(uint32 neg_flags)
65 {
66         DEBUG(3,("Got NTLMSSP neg_flags=0x%08x\n", neg_flags));
67
68         if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE)
69                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_UNICODE\n"));
70         if (neg_flags & NTLMSSP_NEGOTIATE_OEM)
71                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_OEM\n"));
72         if (neg_flags & NTLMSSP_REQUEST_TARGET)
73                 DEBUGADD(4, ("  NTLMSSP_REQUEST_TARGET\n"));
74         if (neg_flags & NTLMSSP_NEGOTIATE_SIGN)
75                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_SIGN\n"));
76         if (neg_flags & NTLMSSP_NEGOTIATE_SEAL)
77                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_SEAL\n"));
78         if (neg_flags & NTLMSSP_NEGOTIATE_DATAGRAM)
79                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_DATAGRAM\n"));
80         if (neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
81                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_LM_KEY\n"));
82         if (neg_flags & NTLMSSP_NEGOTIATE_NETWARE)
83                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_NETWARE\n"));
84         if (neg_flags & NTLMSSP_NEGOTIATE_NTLM)
85                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_NTLM\n"));
86         if (neg_flags & NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED)
87                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED\n"));
88         if (neg_flags & NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED)
89                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED\n"));
90         if (neg_flags & NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL)
91                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL\n"));
92         if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
93                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_ALWAYS_SIGN\n"));
94         if (neg_flags & NTLMSSP_REQUEST_NON_NT_SESSION_KEY)
95                 DEBUGADD(4, ("  NTLMSSP_REQUEST_NON_NT_SESSION_KEY\n"));
96         if (neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
97                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_NTLM2\n"));
98         if (neg_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
99                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_TARGET_INFO\n"));
100         if (neg_flags & NTLMSSP_NEGOTIATE_VERSION)
101                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_VERSION\n"));
102         if (neg_flags & NTLMSSP_NEGOTIATE_128)
103                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_128\n"));
104         if (neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)
105                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_KEY_EXCH\n"));
106         if (neg_flags & NTLMSSP_NEGOTIATE_56)
107                 DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_56\n"));
108 }
109
110 /**
111  * Default challenge generation code.
112  *
113  */
114
115 static NTSTATUS get_challenge(const struct ntlmssp_state *ntlmssp_state,
116                               uint8_t chal[8])
117 {
118         generate_random_buffer(chal, 8);
119         return NT_STATUS_OK;
120 }
121
122 /**
123  * Default 'we can set the challenge to anything we like' implementation
124  *
125  */
126
127 static bool may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
128 {
129         return True;
130 }
131
132 /**
133  * Default 'we can set the challenge to anything we like' implementation
134  *
135  * Does not actually do anything, as the value is always in the structure anyway.
136  *
137  */
138
139 static NTSTATUS set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
140 {
141         SMB_ASSERT(challenge->length == 8);
142         return NT_STATUS_OK;
143 }
144
145 /**
146  * Set a username on an NTLMSSP context - ensures it is talloc()ed
147  *
148  */
149
150 NTSTATUS ntlmssp_set_username(struct ntlmssp_state *ntlmssp_state, const char *user)
151 {
152         ntlmssp_state->user = talloc_strdup(ntlmssp_state, user ? user : "" );
153         if (!ntlmssp_state->user) {
154                 return NT_STATUS_NO_MEMORY;
155         }
156         return NT_STATUS_OK;
157 }
158
159 /**
160  * Store NT and LM hashes on an NTLMSSP context - ensures they are talloc()ed
161  *
162  */
163 NTSTATUS ntlmssp_set_hashes(struct ntlmssp_state *ntlmssp_state,
164                 const unsigned char lm_hash[16],
165                 const unsigned char nt_hash[16])
166 {
167         ntlmssp_state->lm_hash = (unsigned char *)
168                 TALLOC_MEMDUP(ntlmssp_state, lm_hash, 16);
169         ntlmssp_state->nt_hash = (unsigned char *)
170                 TALLOC_MEMDUP(ntlmssp_state, nt_hash, 16);
171         if (!ntlmssp_state->lm_hash || !ntlmssp_state->nt_hash) {
172                 TALLOC_FREE(ntlmssp_state->lm_hash);
173                 TALLOC_FREE(ntlmssp_state->nt_hash);
174                 return NT_STATUS_NO_MEMORY;
175         }
176         return NT_STATUS_OK;
177 }
178
179 /**
180  * Converts a password to the hashes on an NTLMSSP context.
181  *
182  */
183 NTSTATUS ntlmssp_set_password(struct ntlmssp_state *ntlmssp_state, const char *password)
184 {
185         if (!password) {
186                 ntlmssp_state->lm_hash = NULL;
187                 ntlmssp_state->nt_hash = NULL;
188         } else {
189                 unsigned char lm_hash[16];
190                 unsigned char nt_hash[16];
191
192                 E_deshash(password, lm_hash);
193                 E_md4hash(password, nt_hash);
194                 return ntlmssp_set_hashes(ntlmssp_state, lm_hash, nt_hash);
195         }
196         return NT_STATUS_OK;
197 }
198
199 /**
200  * Set a domain on an NTLMSSP context - ensures it is talloc()ed
201  *
202  */
203 NTSTATUS ntlmssp_set_domain(struct ntlmssp_state *ntlmssp_state, const char *domain)
204 {
205         ntlmssp_state->domain = talloc_strdup(ntlmssp_state,
206                                               domain ? domain : "" );
207         if (!ntlmssp_state->domain) {
208                 return NT_STATUS_NO_MEMORY;
209         }
210         return NT_STATUS_OK;
211 }
212
213 /**
214  * Set a workstation on an NTLMSSP context - ensures it is talloc()ed
215  *
216  */
217 NTSTATUS ntlmssp_set_workstation(struct ntlmssp_state *ntlmssp_state, const char *workstation)
218 {
219         ntlmssp_state->workstation = talloc_strdup(ntlmssp_state, workstation);
220         if (!ntlmssp_state->workstation) {
221                 return NT_STATUS_NO_MEMORY;
222         }
223         return NT_STATUS_OK;
224 }
225
226 /**
227  * Request features for the NTLMSSP negotiation
228  *
229  * @param ntlmssp_state NTLMSSP state
230  * @param feature_list List of space seperated features requested from NTLMSSP.
231  */
232 void ntlmssp_want_feature_list(struct ntlmssp_state *ntlmssp_state, char *feature_list)
233 {
234         /*
235          * We need to set this to allow a later SetPassword
236          * via the SAMR pipe to succeed. Strange.... We could
237          * also add  NTLMSSP_NEGOTIATE_SEAL here. JRA.
238          */
239         if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
240                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
241         }
242         if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
243                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
244         }
245         if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
246                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
247         }
248         if (in_list("NTLMSSP_FEATURE_CCACHE", feature_list, true)) {
249                 ntlmssp_state->use_ccache = true;
250         }
251 }
252
253 /**
254  * Request a feature for the NTLMSSP negotiation
255  *
256  * @param ntlmssp_state NTLMSSP state
257  * @param feature Bit flag specifying the requested feature
258  */
259 void ntlmssp_want_feature(struct ntlmssp_state *ntlmssp_state, uint32 feature)
260 {
261         /* As per JRA's comment above */
262         if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
263                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
264         }
265         if (feature & NTLMSSP_FEATURE_SIGN) {
266                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
267         }
268         if (feature & NTLMSSP_FEATURE_SEAL) {
269                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
270         }
271         if (feature & NTLMSSP_FEATURE_CCACHE) {
272                 ntlmssp_state->use_ccache = true;
273         }
274 }
275
276 /**
277  * Next state function for the NTLMSSP state machine
278  *
279  * @param ntlmssp_state NTLMSSP State
280  * @param in The packet in from the NTLMSSP partner, as a DATA_BLOB
281  * @param out The reply, as an allocated DATA_BLOB, caller to free.
282  * @return Errors, NT_STATUS_MORE_PROCESSING_REQUIRED or NT_STATUS_OK.
283  */
284
285 NTSTATUS ntlmssp_update(struct ntlmssp_state *ntlmssp_state,
286                         const DATA_BLOB input, DATA_BLOB *out)
287 {
288         uint32 ntlmssp_command;
289         int i;
290
291         if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
292                 /* Called update after negotiations finished. */
293                 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
294                 return NT_STATUS_INVALID_PARAMETER;
295         }
296
297         *out = data_blob_null;
298
299         if (!input.length) {
300                 switch (ntlmssp_state->role) {
301                 case NTLMSSP_CLIENT:
302                         ntlmssp_command = NTLMSSP_INITIAL;
303                         break;
304                 case NTLMSSP_SERVER:
305                         /* 'datagram' mode - no neg packet */
306                         ntlmssp_command = NTLMSSP_NEGOTIATE;
307                         break;
308                 }
309         } else {
310                 if (!msrpc_parse(ntlmssp_state, &input, "Cd",
311                                  "NTLMSSP",
312                                  &ntlmssp_command)) {
313                         DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
314                         dump_data(2, input.data, input.length);
315                         return NT_STATUS_INVALID_PARAMETER;
316                 }
317         }
318
319         if (ntlmssp_command != ntlmssp_state->expected_state) {
320                 DEBUG(1, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
321                 return NT_STATUS_INVALID_PARAMETER;
322         }
323
324         for (i=0; ntlmssp_callbacks[i].fn; i++) {
325                 if (ntlmssp_callbacks[i].role == ntlmssp_state->role
326                     && ntlmssp_callbacks[i].ntlmssp_command == ntlmssp_command) {
327                         return ntlmssp_callbacks[i].fn(ntlmssp_state, input, out);
328                 }
329         }
330
331         DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
332                   ntlmssp_state->role, ntlmssp_command));
333
334         return NT_STATUS_INVALID_PARAMETER;
335 }
336
337 /**
338  * End an NTLMSSP state machine
339  *
340  * @param ntlmssp_state NTLMSSP State, free()ed by this function
341  */
342
343 void ntlmssp_end(struct ntlmssp_state **ntlmssp_state)
344 {
345         data_blob_free(&(*ntlmssp_state)->chal);
346         data_blob_free(&(*ntlmssp_state)->lm_resp);
347         data_blob_free(&(*ntlmssp_state)->nt_resp);
348         TALLOC_FREE(*ntlmssp_state);
349
350         *ntlmssp_state = NULL;
351         return;
352 }
353
354 /**
355  * Determine correct target name flags for reply, given server role
356  * and negotiated flags
357  *
358  * @param ntlmssp_state NTLMSSP State
359  * @param neg_flags The flags from the packet
360  * @param chal_flags The flags to be set in the reply packet
361  * @return The 'target name' string.
362  */
363
364 static const char *ntlmssp_target_name(struct ntlmssp_state *ntlmssp_state,
365                                        uint32 neg_flags, uint32 *chal_flags)
366 {
367         if (neg_flags & NTLMSSP_REQUEST_TARGET) {
368                 *chal_flags |= NTLMSSP_NEGOTIATE_TARGET_INFO;
369                 *chal_flags |= NTLMSSP_REQUEST_TARGET;
370                 if (ntlmssp_state->server_role == ROLE_STANDALONE) {
371                         *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
372                         return ntlmssp_state->get_global_myname();
373                 } else {
374                         *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
375                         return ntlmssp_state->get_domain();
376                 };
377         } else {
378                 return "";
379         }
380 }
381
382 static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
383                                       uint32 neg_flags, bool allow_lm) {
384         if (neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
385                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
386                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_OEM;
387                 ntlmssp_state->unicode = True;
388         } else {
389                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_UNICODE;
390                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
391                 ntlmssp_state->unicode = False;
392         }
393
394         if ((neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) && allow_lm) {
395                 /* other end forcing us to use LM */
396                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
397                 ntlmssp_state->use_ntlmv2 = False;
398         } else {
399                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
400         }
401
402         if (!(neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
403                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
404         }
405
406         if (!(neg_flags & NTLMSSP_NEGOTIATE_NTLM2)) {
407                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
408         }
409
410         if (!(neg_flags & NTLMSSP_NEGOTIATE_128)) {
411                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_128;
412         }
413
414         if (!(neg_flags & NTLMSSP_NEGOTIATE_56)) {
415                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_56;
416         }
417
418         if (!(neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
419                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_KEY_EXCH;
420         }
421
422         if (!(neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
423                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
424         }
425
426         if (!(neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
427                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SEAL;
428         }
429
430         /* Woop Woop - unknown flag for Windows compatibility...
431            What does this really do ? JRA. */
432         if (!(neg_flags & NTLMSSP_NEGOTIATE_VERSION)) {
433                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_VERSION;
434         }
435
436         if ((neg_flags & NTLMSSP_REQUEST_TARGET)) {
437                 ntlmssp_state->neg_flags |= NTLMSSP_REQUEST_TARGET;
438         }
439 }
440
441 /**
442  Weaken NTLMSSP keys to cope with down-level clients and servers.
443
444  We probably should have some parameters to control this, but as
445  it only occours for LM_KEY connections, and this is controlled
446  by the client lanman auth/lanman auth parameters, it isn't too bad.
447 */
448
449 DATA_BLOB ntlmssp_weaken_keys(struct ntlmssp_state *ntlmssp_state, TALLOC_CTX *mem_ctx)
450 {
451         DATA_BLOB weakened_key = data_blob_talloc(mem_ctx,
452                                         ntlmssp_state->session_key.data,
453                                         ntlmssp_state->session_key.length);
454
455         /* Nothing to weaken.  We certainly don't want to 'extend' the length... */
456         if (weakened_key.length < 16) {
457                 /* perhaps there was no key? */
458                 return weakened_key;
459         }
460
461         /* Key weakening not performed on the master key for NTLM2
462            and does not occour for NTLM1.  Therefore we only need
463            to do this for the LM_KEY.
464         */
465
466         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
467                 /* LM key doesn't support 128 bit crypto, so this is
468                  * the best we can do.  If you negotiate 128 bit, but
469                  * not 56, you end up with 40 bit... */
470                 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
471                         weakened_key.data[7] = 0xa0;
472                 } else { /* forty bits */
473                         weakened_key.data[5] = 0xe5;
474                         weakened_key.data[6] = 0x38;
475                         weakened_key.data[7] = 0xb0;
476                 }
477                 weakened_key.length = 8;
478         }
479         return weakened_key;
480 }
481
482 /**
483  * Next state function for the Negotiate packet
484  *
485  * @param ntlmssp_state NTLMSSP State
486  * @param request The request, as a DATA_BLOB
487  * @param request The reply, as an allocated DATA_BLOB, caller to free.
488  * @return Errors or MORE_PROCESSING_REQUIRED if a reply is sent.
489  */
490
491 static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
492                                          const DATA_BLOB request, DATA_BLOB *reply)
493 {
494         DATA_BLOB struct_blob;
495         const char *dnsname;
496         char *dnsdomname = NULL;
497         uint32 neg_flags = 0;
498         uint32 ntlmssp_command, chal_flags;
499         uint8_t cryptkey[8];
500         const char *target_name;
501         struct NEGOTIATE_MESSAGE negotiate;
502         struct CHALLENGE_MESSAGE challenge;
503         NTSTATUS status;
504
505         /* parse the NTLMSSP packet */
506 #if 0
507         file_save("ntlmssp_negotiate.dat", request.data, request.length);
508 #endif
509
510         if (request.length) {
511                 if ((request.length < 16) || !msrpc_parse(ntlmssp_state, &request, "Cdd",
512                                                           "NTLMSSP",
513                                                           &ntlmssp_command,
514                                                           &neg_flags)) {
515                         DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
516                                 (unsigned int)request.length));
517                         dump_data(2, request.data, request.length);
518                         return NT_STATUS_INVALID_PARAMETER;
519                 }
520                 debug_ntlmssp_flags(neg_flags);
521
522                 if (DEBUGLEVEL >= 10) {
523                         if (NT_STATUS_IS_OK(ntlmssp_pull_NEGOTIATE_MESSAGE(&request,
524                                                        ntlmssp_state,
525                                                        NULL,
526                                                        &negotiate)))
527                         {
528                                 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE, &negotiate);
529                         }
530                 }
531         }
532
533         ntlmssp_handle_neg_flags(ntlmssp_state, neg_flags, lp_lanman_auth());
534
535         /* Ask our caller what challenge they would like in the packet */
536         status = ntlmssp_state->get_challenge(ntlmssp_state, cryptkey);
537         if (!NT_STATUS_IS_OK(status)) {
538                 return status;
539         }
540
541         /* Check if we may set the challenge */
542         if (!ntlmssp_state->may_set_challenge(ntlmssp_state)) {
543                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
544         }
545
546         /* The flags we send back are not just the negotiated flags,
547          * they are also 'what is in this packet'.  Therfore, we
548          * operate on 'chal_flags' from here on
549          */
550
551         chal_flags = ntlmssp_state->neg_flags;
552
553         /* get the right name to fill in as 'target' */
554         target_name = ntlmssp_target_name(ntlmssp_state,
555                                           neg_flags, &chal_flags);
556         if (target_name == NULL)
557                 return NT_STATUS_INVALID_PARAMETER;
558
559         ntlmssp_state->chal = data_blob_talloc(ntlmssp_state, cryptkey, 8);
560         ntlmssp_state->internal_chal = data_blob_talloc(ntlmssp_state,
561                                                         cryptkey, 8);
562
563         /* This should be a 'netbios domain -> DNS domain' mapping */
564         dnsdomname = get_mydnsdomname(ntlmssp_state);
565         if (!dnsdomname) {
566                 dnsdomname = talloc_strdup(ntlmssp_state, "");
567         }
568         if (!dnsdomname) {
569                 return NT_STATUS_NO_MEMORY;
570         }
571         strlower_m(dnsdomname);
572
573         dnsname = get_mydnsfullname();
574         if (!dnsname) {
575                 dnsname = "";
576         }
577
578         /* This creates the 'blob' of names that appears at the end of the packet */
579         if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO)
580         {
581                 msrpc_gen(ntlmssp_state, &struct_blob, "aaaaa",
582                           MsvAvNbDomainName, target_name,
583                           MsvAvNbComputerName, ntlmssp_state->get_global_myname(),
584                           MsvAvDnsDomainName, dnsdomname,
585                           MsvAvDnsComputerName, dnsname,
586                           MsvAvEOL, "");
587         } else {
588                 struct_blob = data_blob_null;
589         }
590
591         {
592                 /* Marshel the packet in the right format, be it unicode or ASCII */
593                 const char *gen_string;
594                 if (ntlmssp_state->unicode) {
595                         gen_string = "CdUdbddB";
596                 } else {
597                         gen_string = "CdAdbddB";
598                 }
599
600                 msrpc_gen(ntlmssp_state, reply, gen_string,
601                           "NTLMSSP",
602                           NTLMSSP_CHALLENGE,
603                           target_name,
604                           chal_flags,
605                           cryptkey, 8,
606                           0, 0,
607                           struct_blob.data, struct_blob.length);
608
609                 if (DEBUGLEVEL >= 10) {
610                         if (NT_STATUS_IS_OK(ntlmssp_pull_CHALLENGE_MESSAGE(reply,
611                                                        ntlmssp_state,
612                                                        NULL,
613                                                        &challenge)))
614                         {
615                                 NDR_PRINT_DEBUG(CHALLENGE_MESSAGE, &challenge);
616                         }
617                 }
618         }
619
620         data_blob_free(&struct_blob);
621
622         ntlmssp_state->expected_state = NTLMSSP_AUTH;
623
624         return NT_STATUS_MORE_PROCESSING_REQUIRED;
625 }
626
627 /**
628  * Next state function for the Authenticate packet
629  *
630  * @param ntlmssp_state NTLMSSP State
631  * @param request The request, as a DATA_BLOB
632  * @param request The reply, as an allocated DATA_BLOB, caller to free.
633  * @return Errors or NT_STATUS_OK.
634  */
635
636 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
637                                     const DATA_BLOB request, DATA_BLOB *reply)
638 {
639         DATA_BLOB encrypted_session_key = data_blob_null;
640         DATA_BLOB user_session_key = data_blob_null;
641         DATA_BLOB lm_session_key = data_blob_null;
642         DATA_BLOB session_key = data_blob_null;
643         uint32 ntlmssp_command, auth_flags;
644         NTSTATUS nt_status = NT_STATUS_OK;
645         struct AUTHENTICATE_MESSAGE authenticate;
646
647         /* used by NTLM2 */
648         bool doing_ntlm2 = False;
649
650         uchar session_nonce[16];
651         uchar session_nonce_hash[16];
652
653         const char *parse_string;
654
655         /* parse the NTLMSSP packet */
656         *reply = data_blob_null;
657
658 #if 0
659         file_save("ntlmssp_auth.dat", request.data, request.length);
660 #endif
661
662         if (ntlmssp_state->unicode) {
663                 parse_string = "CdBBUUUBd";
664         } else {
665                 parse_string = "CdBBAAABd";
666         }
667
668         data_blob_free(&ntlmssp_state->lm_resp);
669         data_blob_free(&ntlmssp_state->nt_resp);
670
671         ntlmssp_state->user = NULL;
672         ntlmssp_state->domain = NULL;
673         ntlmssp_state->workstation = NULL;
674
675         /* now the NTLMSSP encoded auth hashes */
676         if (!msrpc_parse(ntlmssp_state, &request, parse_string,
677                          "NTLMSSP",
678                          &ntlmssp_command,
679                          &ntlmssp_state->lm_resp,
680                          &ntlmssp_state->nt_resp,
681                          &ntlmssp_state->domain,
682                          &ntlmssp_state->user,
683                          &ntlmssp_state->workstation,
684                          &encrypted_session_key,
685                          &auth_flags)) {
686                 auth_flags = 0;
687
688                 /* Try again with a shorter string (Win9X truncates this packet) */
689                 if (ntlmssp_state->unicode) {
690                         parse_string = "CdBBUUU";
691                 } else {
692                         parse_string = "CdBBAAA";
693                 }
694
695                 /* now the NTLMSSP encoded auth hashes */
696                 if (!msrpc_parse(ntlmssp_state, &request, parse_string,
697                                  "NTLMSSP",
698                                  &ntlmssp_command,
699                                  &ntlmssp_state->lm_resp,
700                                  &ntlmssp_state->nt_resp,
701                                  &ntlmssp_state->domain,
702                                  &ntlmssp_state->user,
703                                  &ntlmssp_state->workstation)) {
704                         DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
705                         dump_data(2, request.data, request.length);
706
707                         return NT_STATUS_INVALID_PARAMETER;
708                 }
709         }
710
711         if (auth_flags)
712                 ntlmssp_handle_neg_flags(ntlmssp_state, auth_flags, lp_lanman_auth());
713
714         if (DEBUGLEVEL >= 10) {
715                 if (NT_STATUS_IS_OK(ntlmssp_pull_AUTHENTICATE_MESSAGE(&request,
716                                                   ntlmssp_state,
717                                                   NULL,
718                                                   &authenticate)))
719                 {
720                         NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE, &authenticate);
721                 }
722         }
723
724         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
725                  ntlmssp_state->user, ntlmssp_state->domain, ntlmssp_state->workstation, (unsigned long)ntlmssp_state->lm_resp.length, (unsigned long)ntlmssp_state->nt_resp.length));
726
727 #if 0
728         file_save("nthash1.dat",  &ntlmssp_state->nt_resp.data,  &ntlmssp_state->nt_resp.length);
729         file_save("lmhash1.dat",  &ntlmssp_state->lm_resp.data,  &ntlmssp_state->lm_resp.length);
730 #endif
731
732         /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a
733            client challenge
734
735            However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
736         */
737         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
738                 if (ntlmssp_state->nt_resp.length == 24 && ntlmssp_state->lm_resp.length == 24) {
739                         struct MD5Context md5_session_nonce_ctx;
740                         SMB_ASSERT(ntlmssp_state->internal_chal.data && ntlmssp_state->internal_chal.length == 8);
741
742                         doing_ntlm2 = True;
743
744                         memcpy(session_nonce, ntlmssp_state->internal_chal.data, 8);
745                         memcpy(&session_nonce[8], ntlmssp_state->lm_resp.data, 8);
746
747                         MD5Init(&md5_session_nonce_ctx);
748                         MD5Update(&md5_session_nonce_ctx, session_nonce, 16);
749                         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
750
751                         ntlmssp_state->chal = data_blob_talloc(
752                                 ntlmssp_state, session_nonce_hash, 8);
753
754                         /* LM response is no longer useful */
755                         data_blob_free(&ntlmssp_state->lm_resp);
756
757                         /* We changed the effective challenge - set it */
758                         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->set_challenge(ntlmssp_state, &ntlmssp_state->chal))) {
759                                 data_blob_free(&encrypted_session_key);
760                                 return nt_status;
761                         }
762
763                         /* LM Key is incompatible. */
764                         ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
765                 }
766         }
767
768         /*
769          * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
770          * is required (by "ntlm auth = no" and "lm auth = no" being set in the
771          * smb.conf file) and no NTLMv2 response was sent then the password check
772          * will fail here. JRA.
773          */
774
775         /* Finally, actually ask if the password is OK */
776
777         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state,
778                                                                        &user_session_key, &lm_session_key))) {
779                 data_blob_free(&encrypted_session_key);
780                 return nt_status;
781         }
782
783         dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
784         dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
785
786         /* Handle the different session key derivation for NTLM2 */
787         if (doing_ntlm2) {
788                 if (user_session_key.data && user_session_key.length == 16) {
789                         session_key = data_blob_talloc(ntlmssp_state,
790                                                        NULL, 16);
791                         hmac_md5(user_session_key.data, session_nonce,
792                                  sizeof(session_nonce), session_key.data);
793                         DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
794                         dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
795
796                 } else {
797                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
798                         session_key = data_blob_null;
799                 }
800         } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
801                 if (lm_session_key.data && lm_session_key.length >= 8) {
802                         if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
803                                 session_key = data_blob_talloc(ntlmssp_state,
804                                                                NULL, 16);
805                                 if (session_key.data == NULL) {
806                                         return NT_STATUS_NO_MEMORY;
807                                 }
808                                 SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data,
809                                                           session_key.data);
810                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
811                         } else {
812                                 static const uint8 zeros[24] = {0, };
813                                 session_key = data_blob_talloc(
814                                         ntlmssp_state, NULL, 16);
815                                 if (session_key.data == NULL) {
816                                         return NT_STATUS_NO_MEMORY;
817                                 }
818                                 SMBsesskeygen_lm_sess_key(
819                                         lm_session_key.data, zeros,
820                                         session_key.data);
821                         }
822                         dump_data_pw("LM session key:\n", session_key.data,
823                                      session_key.length);
824                 } else {
825                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
826                         session_key = data_blob_null;
827                 }
828         } else if (user_session_key.data) {
829                 session_key = user_session_key;
830                 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
831                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
832         } else if (lm_session_key.data) {
833                 session_key = lm_session_key;
834                 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
835                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
836         } else {
837                 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
838                 session_key = data_blob_null;
839         }
840
841         /* With KEY_EXCH, the client supplies the proposed session key,
842            but encrypts it with the long-term key */
843         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
844                 if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
845                         data_blob_free(&encrypted_session_key);
846                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n",
847                                   (unsigned int)encrypted_session_key.length));
848                         return NT_STATUS_INVALID_PARAMETER;
849                 } else if (!session_key.data || session_key.length != 16) {
850                         DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n",
851                                   (unsigned int)session_key.length));
852                         ntlmssp_state->session_key = session_key;
853                 } else {
854                         dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
855                         arcfour_crypt_blob(encrypted_session_key.data,
856                                            encrypted_session_key.length,
857                                            &session_key);
858                         ntlmssp_state->session_key = data_blob_talloc(
859                                 ntlmssp_state, encrypted_session_key.data,
860                                 encrypted_session_key.length);
861                         dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data,
862                                      encrypted_session_key.length);
863                 }
864         } else {
865                 ntlmssp_state->session_key = session_key;
866         }
867
868         if (!NT_STATUS_IS_OK(nt_status)) {
869                 ntlmssp_state->session_key = data_blob_null;
870         } else if (ntlmssp_state->session_key.length) {
871                 nt_status = ntlmssp_sign_init(ntlmssp_state);
872         }
873
874         data_blob_free(&encrypted_session_key);
875
876         /* Only one authentication allowed per server state. */
877         ntlmssp_state->expected_state = NTLMSSP_DONE;
878
879         return nt_status;
880 }
881
882 /**
883  * Create an NTLMSSP state machine
884  *
885  * @param ntlmssp_state NTLMSSP State, allocated by this function
886  */
887
888 NTSTATUS ntlmssp_server_start(struct ntlmssp_state **ntlmssp_state)
889 {
890         *ntlmssp_state = TALLOC_ZERO_P(NULL, struct ntlmssp_state);
891         if (!*ntlmssp_state) {
892                 DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
893                 talloc_destroy(*ntlmssp_state);
894                 return NT_STATUS_NO_MEMORY;
895         }
896
897         (*ntlmssp_state)->role = NTLMSSP_SERVER;
898
899         (*ntlmssp_state)->get_challenge = get_challenge;
900         (*ntlmssp_state)->set_challenge = set_challenge;
901         (*ntlmssp_state)->may_set_challenge = may_set_challenge;
902
903         (*ntlmssp_state)->get_global_myname = global_myname;
904         (*ntlmssp_state)->get_domain = lp_workgroup;
905         (*ntlmssp_state)->server_role = ROLE_DOMAIN_MEMBER; /* a good default */
906
907         (*ntlmssp_state)->expected_state = NTLMSSP_NEGOTIATE;
908
909         (*ntlmssp_state)->neg_flags =
910                 NTLMSSP_NEGOTIATE_128 |
911                 NTLMSSP_NEGOTIATE_56 |
912                 NTLMSSP_NEGOTIATE_VERSION |
913                 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
914                 NTLMSSP_NEGOTIATE_NTLM |
915                 NTLMSSP_NEGOTIATE_NTLM2 |
916                 NTLMSSP_NEGOTIATE_KEY_EXCH |
917                 NTLMSSP_NEGOTIATE_SIGN |
918                 NTLMSSP_NEGOTIATE_SEAL;
919
920         return NT_STATUS_OK;
921 }
922
923 /*********************************************************************
924  Client side NTLMSSP
925 *********************************************************************/
926
927 /**
928  * Next state function for the Initial packet
929  *
930  * @param ntlmssp_state NTLMSSP State
931  * @param request The request, as a DATA_BLOB.  reply.data must be NULL
932  * @param request The reply, as an allocated DATA_BLOB, caller to free.
933  * @return Errors or NT_STATUS_OK.
934  */
935
936 static NTSTATUS ntlmssp_client_initial(struct ntlmssp_state *ntlmssp_state,
937                                   DATA_BLOB reply, DATA_BLOB *next_request)
938 {
939         struct NEGOTIATE_MESSAGE negotiate;
940
941         if (ntlmssp_state->unicode) {
942                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
943         } else {
944                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
945         }
946
947         if (ntlmssp_state->use_ntlmv2) {
948                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
949         }
950
951         /* generate the ntlmssp negotiate packet */
952         msrpc_gen(ntlmssp_state, next_request, "CddAA",
953                   "NTLMSSP",
954                   NTLMSSP_NEGOTIATE,
955                   ntlmssp_state->neg_flags,
956                   ntlmssp_state->get_domain(),
957                   ntlmssp_state->get_global_myname());
958
959         if (DEBUGLEVEL >= 10) {
960                 if (NT_STATUS_IS_OK(ntlmssp_pull_NEGOTIATE_MESSAGE(next_request,
961                                                ntlmssp_state,
962                                                NULL,
963                                                &negotiate)))
964                 {
965                         NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE, &negotiate);
966                 }
967         }
968
969         ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
970
971         return NT_STATUS_MORE_PROCESSING_REQUIRED;
972 }
973
974 /**
975  * Next state function for the Challenge Packet.  Generate an auth packet.
976  *
977  * @param ntlmssp_state NTLMSSP State
978  * @param request The request, as a DATA_BLOB.  reply.data must be NULL
979  * @param request The reply, as an allocated DATA_BLOB, caller to free.
980  * @return Errors or NT_STATUS_OK.
981  */
982
983 static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
984                                          const DATA_BLOB reply, DATA_BLOB *next_request)
985 {
986         uint32 chal_flags, ntlmssp_command, unkn1, unkn2;
987         DATA_BLOB server_domain_blob;
988         DATA_BLOB challenge_blob;
989         DATA_BLOB struct_blob = data_blob_null;
990         char *server_domain;
991         const char *chal_parse_string;
992         const char *auth_gen_string;
993         DATA_BLOB lm_response = data_blob_null;
994         DATA_BLOB nt_response = data_blob_null;
995         DATA_BLOB session_key = data_blob_null;
996         DATA_BLOB encrypted_session_key = data_blob_null;
997         NTSTATUS nt_status = NT_STATUS_OK;
998         struct CHALLENGE_MESSAGE challenge;
999         struct AUTHENTICATE_MESSAGE authenticate;
1000
1001         if (ntlmssp_state->use_ccache) {
1002                 struct wbcCredentialCacheParams params;
1003                 struct wbcCredentialCacheInfo *info = NULL;
1004                 struct wbcAuthErrorInfo *error = NULL;
1005                 struct wbcNamedBlob auth_blob;
1006                 struct wbcBlob *wbc_next = NULL;
1007                 struct wbcBlob *wbc_session_key = NULL;
1008                 wbcErr wbc_status;
1009                 int i;
1010
1011                 params.account_name = ntlmssp_state->user;
1012                 params.domain_name = ntlmssp_state->domain;
1013                 params.level = WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP;
1014
1015                 auth_blob.name = "challenge_blob";
1016                 auth_blob.flags = 0;
1017                 auth_blob.blob.data = reply.data;
1018                 auth_blob.blob.length = reply.length;
1019                 params.num_blobs = 1;
1020                 params.blobs = &auth_blob;
1021
1022                 wbc_status = wbcCredentialCache(&params, &info, &error);
1023                 if (error != NULL) {
1024                         wbcFreeMemory(error);
1025                 }
1026                 if (!WBC_ERROR_IS_OK(wbc_status)) {
1027                         goto noccache;
1028                 }
1029
1030                 for (i=0; i<info->num_blobs; i++) {
1031                         if (strequal(info->blobs[i].name, "auth_blob")) {
1032                                 wbc_next = &info->blobs[i].blob;
1033                         }
1034                         if (strequal(info->blobs[i].name, "session_key")) {
1035                                 wbc_session_key = &info->blobs[i].blob;
1036                         }
1037                 }
1038                 if ((wbc_next == NULL) || (wbc_session_key == NULL)) {
1039                         wbcFreeMemory(info);
1040                         goto noccache;
1041                 }
1042
1043                 *next_request = data_blob(wbc_next->data, wbc_next->length);
1044                 ntlmssp_state->session_key = data_blob(
1045                         wbc_session_key->data, wbc_session_key->length);
1046
1047                 wbcFreeMemory(info);
1048                 goto done;
1049         }
1050
1051 noccache:
1052
1053         if (!msrpc_parse(ntlmssp_state, &reply, "CdBd",
1054                          "NTLMSSP",
1055                          &ntlmssp_command,
1056                          &server_domain_blob,
1057                          &chal_flags)) {
1058                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
1059                 dump_data(2, reply.data, reply.length);
1060
1061                 return NT_STATUS_INVALID_PARAMETER;
1062         }
1063
1064         if (DEBUGLEVEL >= 10) {
1065                 if (NT_STATUS_IS_OK(ntlmssp_pull_CHALLENGE_MESSAGE(&reply,
1066                                                ntlmssp_state,
1067                                                NULL,
1068                                                &challenge)))
1069                 {
1070                         NDR_PRINT_DEBUG(CHALLENGE_MESSAGE, &challenge);
1071                 }
1072         }
1073
1074         data_blob_free(&server_domain_blob);
1075
1076         DEBUG(3, ("Got challenge flags:\n"));
1077         debug_ntlmssp_flags(chal_flags);
1078
1079         ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, lp_client_lanman_auth());
1080
1081         if (ntlmssp_state->unicode) {
1082                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
1083                         chal_parse_string = "CdUdbddB";
1084                 } else {
1085                         chal_parse_string = "CdUdbdd";
1086                 }
1087                 auth_gen_string = "CdBBUUUBd";
1088         } else {
1089                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
1090                         chal_parse_string = "CdAdbddB";
1091                 } else {
1092                         chal_parse_string = "CdAdbdd";
1093                 }
1094
1095                 auth_gen_string = "CdBBAAABd";
1096         }
1097
1098         DEBUG(3, ("NTLMSSP: Set final flags:\n"));
1099         debug_ntlmssp_flags(ntlmssp_state->neg_flags);
1100
1101         if (!msrpc_parse(ntlmssp_state, &reply, chal_parse_string,
1102                          "NTLMSSP",
1103                          &ntlmssp_command,
1104                          &server_domain,
1105                          &chal_flags,
1106                          &challenge_blob, 8,
1107                          &unkn1, &unkn2,
1108                          &struct_blob)) {
1109                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
1110                 dump_data(2, reply.data, reply.length);
1111                 return NT_STATUS_INVALID_PARAMETER;
1112         }
1113
1114         ntlmssp_state->server_domain = server_domain;
1115
1116         if (challenge_blob.length != 8) {
1117                 data_blob_free(&struct_blob);
1118                 return NT_STATUS_INVALID_PARAMETER;
1119         }
1120
1121         if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
1122                 static const uint8_t zeros[16] = {0, };
1123                 /* do nothing - blobs are zero length */
1124
1125                 /* session key is all zeros */
1126                 session_key = data_blob_talloc(ntlmssp_state, zeros, 16);
1127
1128                 /* not doing NLTM2 without a password */
1129                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
1130         } else if (ntlmssp_state->use_ntlmv2) {
1131                 if (!struct_blob.length) {
1132                         /* be lazy, match win2k - we can't do NTLMv2 without it */
1133                         DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
1134                         return NT_STATUS_INVALID_PARAMETER;
1135                 }
1136
1137                 /* TODO: if the remote server is standalone, then we should replace 'domain'
1138                    with the server name as supplied above */
1139
1140                 if (!SMBNTLMv2encrypt_hash(ntlmssp_state,
1141                                            ntlmssp_state->user,
1142                                            ntlmssp_state->domain,
1143                                            ntlmssp_state->nt_hash, &challenge_blob,
1144                                            &struct_blob,
1145                                            &lm_response, &nt_response, NULL,
1146                                            &session_key)) {
1147                         data_blob_free(&challenge_blob);
1148                         data_blob_free(&struct_blob);
1149                         return NT_STATUS_NO_MEMORY;
1150                 }
1151         } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
1152                 struct MD5Context md5_session_nonce_ctx;
1153                 uchar session_nonce[16];
1154                 uchar session_nonce_hash[16];
1155                 uchar user_session_key[16];
1156
1157                 lm_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1158                 generate_random_buffer(lm_response.data, 8);
1159                 memset(lm_response.data+8, 0, 16);
1160
1161                 memcpy(session_nonce, challenge_blob.data, 8);
1162                 memcpy(&session_nonce[8], lm_response.data, 8);
1163
1164                 MD5Init(&md5_session_nonce_ctx);
1165                 MD5Update(&md5_session_nonce_ctx, challenge_blob.data, 8);
1166                 MD5Update(&md5_session_nonce_ctx, lm_response.data, 8);
1167                 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
1168
1169                 DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
1170                 DEBUG(5, ("challenge is: \n"));
1171                 dump_data(5, session_nonce_hash, 8);
1172
1173                 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1174                 SMBNTencrypt_hash(ntlmssp_state->nt_hash,
1175                                   session_nonce_hash,
1176                                   nt_response.data);
1177
1178                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1179
1180                 SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, user_session_key);
1181                 hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
1182                 dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
1183         } else {
1184                 /* lanman auth is insecure, it may be disabled */
1185                 if (lp_client_lanman_auth()) {
1186                         lm_response = data_blob_talloc(ntlmssp_state,
1187                                                        NULL, 24);
1188                         SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
1189                                    lm_response.data);
1190                 }
1191
1192                 nt_response = data_blob_talloc(ntlmssp_state, NULL, 24);
1193                 SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
1194                              nt_response.data);
1195
1196                 session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
1197                 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
1198                     && lp_client_lanman_auth()) {
1199                         SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
1200                                         session_key.data);
1201                         dump_data_pw("LM session key\n", session_key.data, session_key.length);
1202                 } else {
1203                         SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, session_key.data);
1204                         dump_data_pw("NT session key:\n", session_key.data, session_key.length);
1205                 }
1206         }
1207         data_blob_free(&struct_blob);
1208
1209         /* Key exchange encryptes a new client-generated session key with
1210            the password-derived key */
1211         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
1212                 /* Make up a new session key */
1213                 uint8 client_session_key[16];
1214                 generate_random_buffer(client_session_key, sizeof(client_session_key));
1215
1216                 /* Encrypt the new session key with the old one */
1217                 encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
1218                 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
1219                 arcfour_crypt_blob(encrypted_session_key.data, encrypted_session_key.length, &session_key);
1220                 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
1221
1222                 /* Mark the new session key as the 'real' session key */
1223                 data_blob_free(&session_key);
1224                 session_key = data_blob_talloc(ntlmssp_state,
1225                                                client_session_key,
1226                                                sizeof(client_session_key));
1227         }
1228
1229         /* this generates the actual auth packet */
1230         if (!msrpc_gen(ntlmssp_state, next_request, auth_gen_string,
1231                        "NTLMSSP",
1232                        NTLMSSP_AUTH,
1233                        lm_response.data, lm_response.length,
1234                        nt_response.data, nt_response.length,
1235                        ntlmssp_state->domain,
1236                        ntlmssp_state->user,
1237                        ntlmssp_state->get_global_myname(),
1238                        encrypted_session_key.data, encrypted_session_key.length,
1239                        ntlmssp_state->neg_flags)) {
1240
1241                 return NT_STATUS_NO_MEMORY;
1242         }
1243
1244         if (DEBUGLEVEL >= 10) {
1245                 if (NT_STATUS_IS_OK(ntlmssp_pull_AUTHENTICATE_MESSAGE(next_request,
1246                                                   ntlmssp_state,
1247                                                   NULL,
1248                                                   &authenticate)))
1249                 {
1250                         NDR_PRINT_DEBUG(AUTHENTICATE_MESSAGE, &authenticate);
1251                 }
1252         }
1253
1254         data_blob_free(&encrypted_session_key);
1255
1256         data_blob_free(&ntlmssp_state->chal);
1257
1258         ntlmssp_state->session_key = session_key;
1259
1260         ntlmssp_state->chal = challenge_blob;
1261         ntlmssp_state->lm_resp = lm_response;
1262         ntlmssp_state->nt_resp = nt_response;
1263
1264 done:
1265
1266         ntlmssp_state->expected_state = NTLMSSP_DONE;
1267
1268         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
1269                 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
1270         }
1271
1272         return nt_status;
1273 }
1274
1275 NTSTATUS ntlmssp_client_start(struct ntlmssp_state **ntlmssp_state)
1276 {
1277         *ntlmssp_state = TALLOC_ZERO_P(NULL, struct ntlmssp_state);
1278         if (!*ntlmssp_state) {
1279                 DEBUG(0,("ntlmssp_client_start: talloc failed!\n"));
1280                 talloc_destroy(*ntlmssp_state);
1281                 return NT_STATUS_NO_MEMORY;
1282         }
1283
1284         (*ntlmssp_state)->role = NTLMSSP_CLIENT;
1285
1286         (*ntlmssp_state)->get_global_myname = global_myname;
1287         (*ntlmssp_state)->get_domain = lp_workgroup;
1288
1289         (*ntlmssp_state)->unicode = True;
1290
1291         (*ntlmssp_state)->use_ntlmv2 = lp_client_ntlmv2_auth();
1292
1293         (*ntlmssp_state)->expected_state = NTLMSSP_INITIAL;
1294
1295         (*ntlmssp_state)->neg_flags =
1296                 NTLMSSP_NEGOTIATE_128 |
1297                 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
1298                 NTLMSSP_NEGOTIATE_NTLM |
1299                 NTLMSSP_NEGOTIATE_NTLM2 |
1300                 NTLMSSP_NEGOTIATE_KEY_EXCH |
1301                 NTLMSSP_REQUEST_TARGET;
1302
1303         return NT_STATUS_OK;
1304 }