auth/ntlmssp: add more compat for GENSEC_FEATURE_LDAP_STYLE
[nivanova/samba-autobuild/.git] / auth / ntlmssp / ntlmssp_client.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, client server side parsing
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8    Copyright (C) Stefan Metzmacher 2005
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 struct auth_session_info;
25
26 #include "includes.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "../lib/crypto/crypto.h"
29 #include "../libcli/auth/libcli_auth.h"
30 #include "auth/credentials/credentials.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/gensec/gensec_internal.h"
33 #include "param/param.h"
34 #include "auth/ntlmssp/ntlmssp_private.h"
35 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
36 #include "../auth/ntlmssp/ntlmssp_ndr.h"
37 #include "../nsswitch/libwbclient/wbclient.h"
38
39 /*********************************************************************
40  Client side NTLMSSP
41 *********************************************************************/
42
43 /**
44  * Next state function for the Initial packet
45  *
46  * @param ntlmssp_state NTLMSSP State
47  * @param out_mem_ctx The DATA_BLOB *out will be allocated on this context
48  * @param in A NULL data blob (input ignored)
49  * @param out The initial negotiate request to the server, as an talloc()ed DATA_BLOB, on out_mem_ctx
50  * @return Errors or NT_STATUS_OK.
51  */
52
53 NTSTATUS ntlmssp_client_initial(struct gensec_security *gensec_security,
54                                 TALLOC_CTX *out_mem_ctx,
55                                 DATA_BLOB in, DATA_BLOB *out)
56 {
57         struct gensec_ntlmssp_context *gensec_ntlmssp =
58                 talloc_get_type_abort(gensec_security->private_data,
59                                       struct gensec_ntlmssp_context);
60         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
61         NTSTATUS status;
62         const DATA_BLOB version_blob = ntlmssp_version_blob();
63
64         /* generate the ntlmssp negotiate packet */
65         status = msrpc_gen(out_mem_ctx,
66                   out, "CddAAb",
67                   "NTLMSSP",
68                   NTLMSSP_NEGOTIATE,
69                   ntlmssp_state->neg_flags,
70                   "", /* domain */
71                   "", /* workstation */
72                   version_blob.data, version_blob.length);
73         if (!NT_STATUS_IS_OK(status)) {
74                 DEBUG(0, ("ntlmssp_client_initial: failed to generate "
75                           "ntlmssp negotiate packet\n"));
76                 return status;
77         }
78
79         if (DEBUGLEVEL >= 10) {
80                 struct NEGOTIATE_MESSAGE *negotiate = talloc(
81                         ntlmssp_state, struct NEGOTIATE_MESSAGE);
82                 if (negotiate != NULL) {
83                         status = ntlmssp_pull_NEGOTIATE_MESSAGE(
84                                 out, negotiate, negotiate);
85                         if (NT_STATUS_IS_OK(status)) {
86                                 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
87                                                 negotiate);
88                         }
89                         TALLOC_FREE(negotiate);
90                 }
91         }
92
93         ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
94
95         return NT_STATUS_MORE_PROCESSING_REQUIRED;
96 }
97
98 NTSTATUS gensec_ntlmssp_resume_ccache(struct gensec_security *gensec_security,
99                                 TALLOC_CTX *out_mem_ctx,
100                                 DATA_BLOB in, DATA_BLOB *out)
101 {
102         struct gensec_ntlmssp_context *gensec_ntlmssp =
103                 talloc_get_type_abort(gensec_security->private_data,
104                                       struct gensec_ntlmssp_context);
105         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
106         uint32_t neg_flags = 0;
107         uint32_t ntlmssp_command;
108         NTSTATUS status;
109         bool ok;
110
111         *out = data_blob_null;
112
113         if (in.length == 0) {
114                 /*
115                  * This is compat code for older callers
116                  * which were missing the "initial_blob"
117                  */
118                 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
119                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
120         }
121
122         /* parse the NTLMSSP packet */
123
124         if (in.length > UINT16_MAX) {
125                 DEBUG(1, ("%s: reject large request of length %u\n",
126                         __func__, (unsigned int)in.length));
127                 return NT_STATUS_INVALID_PARAMETER;
128         }
129
130         ok = msrpc_parse(ntlmssp_state, &in, "Cdd",
131                          "NTLMSSP",
132                          &ntlmssp_command,
133                          &neg_flags);
134         if (!ok) {
135                 DEBUG(1, ("%s: failed to parse NTLMSSP Negotiate of length %u\n",
136                         __func__, (unsigned int)in.length));
137                 dump_data(2, in.data, in.length);
138                 return NT_STATUS_INVALID_PARAMETER;
139         }
140
141         if (ntlmssp_command != NTLMSSP_NEGOTIATE) {
142                 DEBUG(1, ("%s: no NTLMSSP Negotiate message (length %u)\n",
143                         __func__, (unsigned int)in.length));
144                 dump_data(2, in.data, in.length);
145                 return NT_STATUS_INVALID_PARAMETER;
146         }
147
148         ntlmssp_state->neg_flags = neg_flags;
149         DEBUG(3, ("Imported Negotiate flags:\n"));
150         debug_ntlmssp_flags(neg_flags);
151
152         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_UNICODE) {
153                 ntlmssp_state->unicode = true;
154         } else {
155                 ntlmssp_state->unicode = false;
156         }
157
158         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN) {
159                 gensec_security->want_features |= GENSEC_FEATURE_SIGN;
160
161                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
162         }
163
164         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
165                 gensec_security->want_features |= GENSEC_FEATURE_SEAL;
166
167                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
168                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
169         }
170
171         if (DEBUGLEVEL >= 10) {
172                 struct NEGOTIATE_MESSAGE *negotiate = talloc(
173                         ntlmssp_state, struct NEGOTIATE_MESSAGE);
174                 if (negotiate != NULL) {
175                         status = ntlmssp_pull_NEGOTIATE_MESSAGE(
176                                 &in, negotiate, negotiate);
177                         if (NT_STATUS_IS_OK(status)) {
178                                 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
179                                                 negotiate);
180                         }
181                         TALLOC_FREE(negotiate);
182                 }
183         }
184
185         ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
186
187         return NT_STATUS_MORE_PROCESSING_REQUIRED;
188 }
189
190 /**
191  * Next state function for the Challenge Packet.  Generate an auth packet.
192  *
193  * @param gensec_security GENSEC state
194  * @param out_mem_ctx Memory context for *out
195  * @param in The server challnege, as a DATA_BLOB.  reply.data must be NULL
196  * @param out The next request (auth packet) to the server, as an allocated DATA_BLOB, on the out_mem_ctx context
197  * @return Errors or NT_STATUS_OK.
198  */
199
200 NTSTATUS ntlmssp_client_challenge(struct gensec_security *gensec_security,
201                                   TALLOC_CTX *out_mem_ctx,
202                                   const DATA_BLOB in, DATA_BLOB *out)
203 {
204         struct gensec_ntlmssp_context *gensec_ntlmssp =
205                 talloc_get_type_abort(gensec_security->private_data,
206                                       struct gensec_ntlmssp_context);
207         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
208         uint32_t chal_flags, ntlmssp_command, unkn1 = 0, unkn2 = 0;
209         DATA_BLOB server_domain_blob;
210         DATA_BLOB challenge_blob;
211         DATA_BLOB target_info = data_blob(NULL, 0);
212         char *server_domain;
213         const char *chal_parse_string;
214         const char *chal_parse_string_short = NULL;
215         const char *auth_gen_string;
216         DATA_BLOB lm_response = data_blob(NULL, 0);
217         DATA_BLOB nt_response = data_blob(NULL, 0);
218         DATA_BLOB session_key = data_blob(NULL, 0);
219         DATA_BLOB lm_session_key = data_blob(NULL, 0);
220         DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
221         NTSTATUS nt_status;
222         int flags = 0;
223         const char *user = NULL, *domain = NULL, *workstation = NULL;
224         bool is_anonymous = false;
225         const DATA_BLOB version_blob = ntlmssp_version_blob();
226
227         TALLOC_CTX *mem_ctx = talloc_new(out_mem_ctx);
228         if (!mem_ctx) {
229                 return NT_STATUS_NO_MEMORY;
230         }
231
232         if (!msrpc_parse(mem_ctx,
233                          &in, "CdBd",
234                          "NTLMSSP",
235                          &ntlmssp_command,
236                          &server_domain_blob,
237                          &chal_flags)) {
238                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
239                 dump_data(2, in.data, in.length);
240                 talloc_free(mem_ctx);
241
242                 return NT_STATUS_INVALID_PARAMETER;
243         }
244
245         data_blob_free(&server_domain_blob);
246
247         DEBUG(3, ("Got challenge flags:\n"));
248         debug_ntlmssp_flags(chal_flags);
249
250         ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, ntlmssp_state->allow_lm_key);
251
252         if (ntlmssp_state->unicode) {
253                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
254                         chal_parse_string = "CdUdbddB";
255                 } else {
256                         chal_parse_string = "CdUdbdd";
257                         chal_parse_string_short = "CdUdb";
258                 }
259                 auth_gen_string = "CdBBUUUBdb";
260         } else {
261                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
262                         chal_parse_string = "CdAdbddB";
263                 } else {
264                         chal_parse_string = "CdAdbdd";
265                         chal_parse_string_short = "CdAdb";
266                 }
267
268                 auth_gen_string = "CdBBAAABdb";
269         }
270
271         if (!msrpc_parse(mem_ctx,
272                          &in, chal_parse_string,
273                          "NTLMSSP",
274                          &ntlmssp_command,
275                          &server_domain,
276                          &chal_flags,
277                          &challenge_blob, 8,
278                          &unkn1, &unkn2,
279                          &target_info)) {
280
281                 bool ok = false;
282
283                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
284
285                 if (chal_parse_string_short != NULL) {
286                         /*
287                          * In the case where NTLMSSP_NEGOTIATE_TARGET_INFO
288                          * is not used, some NTLMSSP servers don't return
289                          * the unused unkn1 and unkn2 fields.
290                          * See bug:
291                          * https://bugzilla.samba.org/show_bug.cgi?id=10016
292                          * for packet traces.
293                          * Try and parse again without them.
294                          */
295                         ok = msrpc_parse(mem_ctx,
296                                 &in, chal_parse_string_short,
297                                 "NTLMSSP",
298                                 &ntlmssp_command,
299                                 &server_domain,
300                                 &chal_flags,
301                                 &challenge_blob, 8);
302                         if (!ok) {
303                                 DEBUG(1, ("Failed to short parse "
304                                         "the NTLMSSP Challenge: (#2)\n"));
305                         }
306                 }
307
308                 if (!ok) {
309                         dump_data(2, in.data, in.length);
310                         talloc_free(mem_ctx);
311                         return NT_STATUS_INVALID_PARAMETER;
312                 }
313         }
314
315         if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
316                 ntlmssp_state->server.is_standalone = true;
317         } else {
318                 ntlmssp_state->server.is_standalone = false;
319         }
320         /* TODO: parse struct_blob and fill in the rest */
321         ntlmssp_state->server.netbios_name = "";
322         ntlmssp_state->server.netbios_domain = talloc_move(ntlmssp_state, &server_domain);
323         ntlmssp_state->server.dns_name = "";
324         ntlmssp_state->server.dns_domain = "";
325
326         if (challenge_blob.length != 8) {
327                 talloc_free(mem_ctx);
328                 return NT_STATUS_INVALID_PARAMETER;
329         }
330
331         is_anonymous = cli_credentials_is_anonymous(gensec_security->credentials);
332         cli_credentials_get_ntlm_username_domain(gensec_security->credentials, mem_ctx,
333                                                  &user, &domain);
334
335         workstation = cli_credentials_get_workstation(gensec_security->credentials);
336
337         if (user == NULL) {
338                 DEBUG(10, ("User is NULL, returning INVALID_PARAMETER\n"));
339                 return NT_STATUS_INVALID_PARAMETER;
340         }
341
342         if (domain == NULL) {
343                 DEBUG(10, ("Domain is NULL, returning INVALID_PARAMETER\n"));
344                 return NT_STATUS_INVALID_PARAMETER;
345         }
346
347         if (workstation == NULL) {
348                 DEBUG(10, ("Workstation is NULL, returning INVALID_PARAMETER\n"));
349                 return NT_STATUS_INVALID_PARAMETER;
350         }
351
352         if (is_anonymous) {
353                 ntlmssp_state->neg_flags |= NTLMSSP_ANONYMOUS;
354                 /*
355                  * don't use the ccache for anonymous auth
356                  */
357                 ntlmssp_state->use_ccache = false;
358         }
359         if (ntlmssp_state->use_ccache) {
360                 struct samr_Password *nt_hash = NULL;
361
362                 /*
363                  * If we have a password given we don't
364                  * use the ccache
365                  */
366                 nt_hash = cli_credentials_get_nt_hash(gensec_security->credentials,
367                                                       mem_ctx);
368                 if (nt_hash != NULL) {
369                         ZERO_STRUCTP(nt_hash);
370                         TALLOC_FREE(nt_hash);
371                         ntlmssp_state->use_ccache = false;
372                 }
373         }
374
375         if (ntlmssp_state->use_ccache) {
376                 struct wbcCredentialCacheParams params;
377                 struct wbcCredentialCacheInfo *info = NULL;
378                 struct wbcAuthErrorInfo *error = NULL;
379                 struct wbcNamedBlob auth_blobs[1];
380                 const struct wbcBlob *wbc_auth_blob = NULL;
381                 const struct wbcBlob *wbc_session_key = NULL;
382                 wbcErr wbc_status;
383                 int i;
384
385                 params.account_name = user;
386                 params.domain_name = domain;
387                 params.level = WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP;
388
389                 auth_blobs[0].name = "challenge_blob";
390                 auth_blobs[0].flags = 0;
391                 auth_blobs[0].blob.data = in.data;
392                 auth_blobs[0].blob.length = in.length;
393                 params.num_blobs = ARRAY_SIZE(auth_blobs);
394                 params.blobs = auth_blobs;
395
396                 wbc_status = wbcCredentialCache(&params, &info, &error);
397                 wbcFreeMemory(error);
398                 if (!WBC_ERROR_IS_OK(wbc_status)) {
399                         return NT_STATUS_WRONG_CREDENTIAL_HANDLE;
400                 }
401
402                 for (i=0; i<info->num_blobs; i++) {
403                         if (strequal(info->blobs[i].name, "auth_blob")) {
404                                 wbc_auth_blob = &info->blobs[i].blob;
405                         }
406                         if (strequal(info->blobs[i].name, "session_key")) {
407                                 wbc_session_key = &info->blobs[i].blob;
408                         }
409                 }
410                 if ((wbc_auth_blob == NULL) || (wbc_session_key == NULL)) {
411                         wbcFreeMemory(info);
412                         return NT_STATUS_WRONG_CREDENTIAL_HANDLE;
413                 }
414
415                 session_key = data_blob_talloc(mem_ctx,
416                                                wbc_session_key->data,
417                                                wbc_session_key->length);
418                 if (session_key.length != wbc_session_key->length) {
419                         wbcFreeMemory(info);
420                         return NT_STATUS_NO_MEMORY;
421                 }
422                 *out = data_blob_talloc(mem_ctx,
423                                         wbc_auth_blob->data,
424                                         wbc_auth_blob->length);
425                 if (out->length != wbc_auth_blob->length) {
426                         wbcFreeMemory(info);
427                         return NT_STATUS_NO_MEMORY;
428                 }
429
430                 wbcFreeMemory(info);
431                 goto done;
432         }
433
434         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
435                 flags |= CLI_CRED_NTLM2;
436         }
437         if (ntlmssp_state->use_ntlmv2) {
438                 flags |= CLI_CRED_NTLMv2_AUTH;
439         }
440         if (ntlmssp_state->use_nt_response) {
441                 flags |= CLI_CRED_NTLM_AUTH;
442         }
443         if (lpcfg_client_lanman_auth(gensec_security->settings->lp_ctx)) {
444                 flags |= CLI_CRED_LANMAN_AUTH;
445         }
446
447         nt_status = cli_credentials_get_ntlm_response(gensec_security->credentials, mem_ctx,
448                                                       &flags, challenge_blob, target_info,
449                                                       &lm_response, &nt_response,
450                                                       &lm_session_key, &session_key);
451
452         if (!NT_STATUS_IS_OK(nt_status)) {
453                 return nt_status;
454         }
455
456         if (!(flags & CLI_CRED_LANMAN_AUTH)) {
457                 /* LM Key is still possible, just silly, so we do not
458                  * allow it. Fortunetly all LM crypto is off by
459                  * default and we require command line options to end
460                  * up here */
461                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
462         }
463
464         if (!(flags & CLI_CRED_NTLM2)) {
465                 /* NTLM2 is incompatible... */
466                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
467         }
468
469         if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
470             && lpcfg_client_lanman_auth(gensec_security->settings->lp_ctx) && lm_session_key.length == 16) {
471                 DATA_BLOB new_session_key = data_blob_talloc(mem_ctx, NULL, 16);
472                 if (lm_response.length == 24) {
473                         SMBsesskeygen_lm_sess_key(lm_session_key.data, lm_response.data,
474                                                   new_session_key.data);
475                 } else {
476                         static const uint8_t zeros[24];
477                         SMBsesskeygen_lm_sess_key(lm_session_key.data, zeros,
478                                                   new_session_key.data);
479                 }
480                 session_key = new_session_key;
481                 dump_data_pw("LM session key\n", session_key.data, session_key.length);
482         }
483
484
485         /* Key exchange encryptes a new client-generated session key with
486            the password-derived key */
487         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
488                 /* Make up a new session key */
489                 uint8_t client_session_key[16];
490                 generate_secret_buffer(client_session_key, sizeof(client_session_key));
491
492                 /* Encrypt the new session key with the old one */
493                 encrypted_session_key = data_blob_talloc(ntlmssp_state,
494                                                          client_session_key, sizeof(client_session_key));
495                 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
496                 arcfour_crypt(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
497                 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
498
499                 /* Mark the new session key as the 'real' session key */
500                 session_key = data_blob_talloc(mem_ctx, client_session_key, sizeof(client_session_key));
501         }
502
503         /* this generates the actual auth packet */
504         nt_status = msrpc_gen(mem_ctx,
505                        out, auth_gen_string,
506                        "NTLMSSP",
507                        NTLMSSP_AUTH,
508                        lm_response.data, lm_response.length,
509                        nt_response.data, nt_response.length,
510                        domain,
511                        user,
512                        workstation,
513                        encrypted_session_key.data, encrypted_session_key.length,
514                        ntlmssp_state->neg_flags,
515                        version_blob.data, version_blob.length);
516         if (!NT_STATUS_IS_OK(nt_status)) {
517                 talloc_free(mem_ctx);
518                 return nt_status;
519         }
520
521 done:
522         ntlmssp_state->session_key = session_key;
523         talloc_steal(ntlmssp_state, session_key.data);
524
525         DEBUG(3, ("NTLMSSP: Set final flags:\n"));
526         debug_ntlmssp_flags(ntlmssp_state->neg_flags);
527
528         talloc_steal(out_mem_ctx, out->data);
529
530         ntlmssp_state->expected_state = NTLMSSP_DONE;
531
532         if (gensec_security->want_features & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL)) {
533                 nt_status = ntlmssp_sign_init(ntlmssp_state);
534                 if (!NT_STATUS_IS_OK(nt_status)) {
535                         DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n",
536                                   nt_errstr(nt_status)));
537                         talloc_free(mem_ctx);
538                         return nt_status;
539                 }
540         }
541
542         talloc_free(mem_ctx);
543         return NT_STATUS_OK;
544 }
545
546 NTSTATUS gensec_ntlmssp_client_start(struct gensec_security *gensec_security)
547 {
548         struct gensec_ntlmssp_context *gensec_ntlmssp;
549         struct ntlmssp_state *ntlmssp_state;
550         NTSTATUS nt_status;
551
552         nt_status = gensec_ntlmssp_start(gensec_security);
553         NT_STATUS_NOT_OK_RETURN(nt_status);
554
555         gensec_ntlmssp =
556                 talloc_get_type_abort(gensec_security->private_data,
557                                       struct gensec_ntlmssp_context);
558
559         ntlmssp_state = talloc_zero(gensec_ntlmssp,
560                                     struct ntlmssp_state);
561         if (!ntlmssp_state) {
562                 return NT_STATUS_NO_MEMORY;
563         }
564
565         gensec_ntlmssp->ntlmssp_state = ntlmssp_state;
566
567         ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
568
569         ntlmssp_state->role = NTLMSSP_CLIENT;
570
571         ntlmssp_state->client.netbios_domain = lpcfg_workgroup(gensec_security->settings->lp_ctx);
572         ntlmssp_state->client.netbios_name = cli_credentials_get_workstation(gensec_security->credentials);
573
574         ntlmssp_state->unicode = gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "unicode", true);
575
576         ntlmssp_state->use_nt_response = gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "send_nt_reponse", true);
577
578         ntlmssp_state->allow_lm_key = (lpcfg_client_lanman_auth(gensec_security->settings->lp_ctx)
579                                               && (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "allow_lm_key", false)
580                                                   || gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "lm_key", false)));
581
582         ntlmssp_state->use_ntlmv2 = lpcfg_client_ntlmv2_auth(gensec_security->settings->lp_ctx);
583
584         ntlmssp_state->expected_state = NTLMSSP_INITIAL;
585
586         ntlmssp_state->neg_flags =
587                 NTLMSSP_NEGOTIATE_NTLM |
588                 NTLMSSP_NEGOTIATE_VERSION |
589                 NTLMSSP_REQUEST_TARGET;
590
591         if (ntlmssp_state->unicode) {
592                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
593         } else {
594                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
595         }
596
597         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "128bit", true)) {
598                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
599         }
600
601         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "56bit", false)) {
602                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
603         }
604
605         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "lm_key", false)) {
606                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
607         }
608
609         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "keyexchange", true)) {
610                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
611         }
612
613         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "alwayssign", true)) {
614                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
615         }
616
617         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "ntlm2", true)) {
618                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
619         } else {
620                 /* apparently we can't do ntlmv2 if we don't do ntlm2 */
621                 ntlmssp_state->use_ntlmv2 = false;
622         }
623
624         if (ntlmssp_state->use_ntlmv2) {
625                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
626         }
627
628         if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
629                 /*
630                  * We need to set this to allow a later SetPassword
631                  * via the SAMR pipe to succeed. Strange.... We could
632                  * also add  NTLMSSP_NEGOTIATE_SEAL here. JRA.
633                  *
634                  * Without this, Windows will not create the master key
635                  * that it thinks is only used for NTLMSSP signing and
636                  * sealing.  (It is actually pulled out and used directly)
637                  */
638                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
639         }
640         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
641                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
642
643                 if (gensec_security->want_features & GENSEC_FEATURE_LDAP_STYLE) {
644                         /*
645                          * We need to handle NTLMSSP_NEGOTIATE_SIGN as
646                          * NTLMSSP_NEGOTIATE_SEAL if GENSEC_FEATURE_LDAP_STYLE
647                          * is requested.
648                          */
649                         ntlmssp_state->force_wrap_seal = true;
650                         /*
651                          * We want also work against old Samba servers
652                          * which didn't had GENSEC_FEATURE_LDAP_STYLE
653                          * we negotiate SEAL too. We may remove this
654                          * in a few years. As all servers should have
655                          * GENSEC_FEATURE_LDAP_STYLE by then.
656                          */
657                         ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
658                 }
659         }
660         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
661                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
662                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
663         }
664         if (gensec_security->want_features & GENSEC_FEATURE_NTLM_CCACHE) {
665                 ntlmssp_state->use_ccache = true;
666         }
667
668         return NT_STATUS_OK;
669 }
670
671 NTSTATUS gensec_ntlmssp_resume_ccache_start(struct gensec_security *gensec_security)
672 {
673         struct gensec_ntlmssp_context *gensec_ntlmssp = NULL;
674         NTSTATUS status;
675
676         status = gensec_ntlmssp_client_start(gensec_security);
677         if (!NT_STATUS_IS_OK(status)) {
678                 return status;
679         }
680
681         gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
682                                                struct gensec_ntlmssp_context);
683         gensec_ntlmssp->ntlmssp_state->use_ccache = false;
684         gensec_ntlmssp->ntlmssp_state->resume_ccache = true;
685         gensec_ntlmssp->ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
686
687         return NT_STATUS_OK;
688 }