CVE-2016-2110: auth/ntlmssp: let ntlmssp_handle_neg_flags() return NTSTATUS
[samba.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         nt_status = ntlmssp_handle_neg_flags(ntlmssp_state,
251                                              chal_flags, "challenge");
252         if (!NT_STATUS_IS_OK(nt_status)) {
253                 return nt_status;
254         }
255
256         if (ntlmssp_state->unicode) {
257                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
258                         chal_parse_string = "CdUdbddB";
259                 } else {
260                         chal_parse_string = "CdUdbdd";
261                         chal_parse_string_short = "CdUdb";
262                 }
263                 auth_gen_string = "CdBBUUUBdb";
264         } else {
265                 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
266                         chal_parse_string = "CdAdbddB";
267                 } else {
268                         chal_parse_string = "CdAdbdd";
269                         chal_parse_string_short = "CdAdb";
270                 }
271
272                 auth_gen_string = "CdBBAAABdb";
273         }
274
275         if (!msrpc_parse(mem_ctx,
276                          &in, chal_parse_string,
277                          "NTLMSSP",
278                          &ntlmssp_command,
279                          &server_domain,
280                          &chal_flags,
281                          &challenge_blob, 8,
282                          &unkn1, &unkn2,
283                          &target_info)) {
284
285                 bool ok = false;
286
287                 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
288
289                 if (chal_parse_string_short != NULL) {
290                         /*
291                          * In the case where NTLMSSP_NEGOTIATE_TARGET_INFO
292                          * is not used, some NTLMSSP servers don't return
293                          * the unused unkn1 and unkn2 fields.
294                          * See bug:
295                          * https://bugzilla.samba.org/show_bug.cgi?id=10016
296                          * for packet traces.
297                          * Try and parse again without them.
298                          */
299                         ok = msrpc_parse(mem_ctx,
300                                 &in, chal_parse_string_short,
301                                 "NTLMSSP",
302                                 &ntlmssp_command,
303                                 &server_domain,
304                                 &chal_flags,
305                                 &challenge_blob, 8);
306                         if (!ok) {
307                                 DEBUG(1, ("Failed to short parse "
308                                         "the NTLMSSP Challenge: (#2)\n"));
309                         }
310                 }
311
312                 if (!ok) {
313                         dump_data(2, in.data, in.length);
314                         talloc_free(mem_ctx);
315                         return NT_STATUS_INVALID_PARAMETER;
316                 }
317         }
318
319         if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
320                 ntlmssp_state->server.is_standalone = true;
321         } else {
322                 ntlmssp_state->server.is_standalone = false;
323         }
324         /* TODO: parse struct_blob and fill in the rest */
325         ntlmssp_state->server.netbios_name = "";
326         ntlmssp_state->server.netbios_domain = talloc_move(ntlmssp_state, &server_domain);
327         ntlmssp_state->server.dns_name = "";
328         ntlmssp_state->server.dns_domain = "";
329
330         if (challenge_blob.length != 8) {
331                 talloc_free(mem_ctx);
332                 return NT_STATUS_INVALID_PARAMETER;
333         }
334
335         is_anonymous = cli_credentials_is_anonymous(gensec_security->credentials);
336         cli_credentials_get_ntlm_username_domain(gensec_security->credentials, mem_ctx,
337                                                  &user, &domain);
338
339         workstation = cli_credentials_get_workstation(gensec_security->credentials);
340
341         if (user == NULL) {
342                 DEBUG(10, ("User is NULL, returning INVALID_PARAMETER\n"));
343                 return NT_STATUS_INVALID_PARAMETER;
344         }
345
346         if (domain == NULL) {
347                 DEBUG(10, ("Domain is NULL, returning INVALID_PARAMETER\n"));
348                 return NT_STATUS_INVALID_PARAMETER;
349         }
350
351         if (workstation == NULL) {
352                 DEBUG(10, ("Workstation is NULL, returning INVALID_PARAMETER\n"));
353                 return NT_STATUS_INVALID_PARAMETER;
354         }
355
356         if (is_anonymous) {
357                 ntlmssp_state->neg_flags |= NTLMSSP_ANONYMOUS;
358                 /*
359                  * don't use the ccache for anonymous auth
360                  */
361                 ntlmssp_state->use_ccache = false;
362         }
363         if (ntlmssp_state->use_ccache) {
364                 struct samr_Password *nt_hash = NULL;
365
366                 /*
367                  * If we have a password given we don't
368                  * use the ccache
369                  */
370                 nt_hash = cli_credentials_get_nt_hash(gensec_security->credentials,
371                                                       mem_ctx);
372                 if (nt_hash != NULL) {
373                         ZERO_STRUCTP(nt_hash);
374                         TALLOC_FREE(nt_hash);
375                         ntlmssp_state->use_ccache = false;
376                 }
377         }
378
379         if (ntlmssp_state->use_ccache) {
380                 struct wbcCredentialCacheParams params;
381                 struct wbcCredentialCacheInfo *info = NULL;
382                 struct wbcAuthErrorInfo *error = NULL;
383                 struct wbcNamedBlob auth_blobs[1];
384                 const struct wbcBlob *wbc_auth_blob = NULL;
385                 const struct wbcBlob *wbc_session_key = NULL;
386                 wbcErr wbc_status;
387                 int i;
388
389                 params.account_name = user;
390                 params.domain_name = domain;
391                 params.level = WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP;
392
393                 auth_blobs[0].name = "challenge_blob";
394                 auth_blobs[0].flags = 0;
395                 auth_blobs[0].blob.data = in.data;
396                 auth_blobs[0].blob.length = in.length;
397                 params.num_blobs = ARRAY_SIZE(auth_blobs);
398                 params.blobs = auth_blobs;
399
400                 wbc_status = wbcCredentialCache(&params, &info, &error);
401                 wbcFreeMemory(error);
402                 if (!WBC_ERROR_IS_OK(wbc_status)) {
403                         return NT_STATUS_WRONG_CREDENTIAL_HANDLE;
404                 }
405
406                 for (i=0; i<info->num_blobs; i++) {
407                         if (strequal(info->blobs[i].name, "auth_blob")) {
408                                 wbc_auth_blob = &info->blobs[i].blob;
409                         }
410                         if (strequal(info->blobs[i].name, "session_key")) {
411                                 wbc_session_key = &info->blobs[i].blob;
412                         }
413                 }
414                 if ((wbc_auth_blob == NULL) || (wbc_session_key == NULL)) {
415                         wbcFreeMemory(info);
416                         return NT_STATUS_WRONG_CREDENTIAL_HANDLE;
417                 }
418
419                 session_key = data_blob_talloc(mem_ctx,
420                                                wbc_session_key->data,
421                                                wbc_session_key->length);
422                 if (session_key.length != wbc_session_key->length) {
423                         wbcFreeMemory(info);
424                         return NT_STATUS_NO_MEMORY;
425                 }
426                 *out = data_blob_talloc(mem_ctx,
427                                         wbc_auth_blob->data,
428                                         wbc_auth_blob->length);
429                 if (out->length != wbc_auth_blob->length) {
430                         wbcFreeMemory(info);
431                         return NT_STATUS_NO_MEMORY;
432                 }
433
434                 wbcFreeMemory(info);
435                 goto done;
436         }
437
438         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
439                 flags |= CLI_CRED_NTLM2;
440         }
441         if (ntlmssp_state->use_ntlmv2) {
442                 flags |= CLI_CRED_NTLMv2_AUTH;
443         }
444         if (ntlmssp_state->use_nt_response) {
445                 flags |= CLI_CRED_NTLM_AUTH;
446         }
447         if (lpcfg_client_lanman_auth(gensec_security->settings->lp_ctx)) {
448                 flags |= CLI_CRED_LANMAN_AUTH;
449         }
450
451         nt_status = cli_credentials_get_ntlm_response(gensec_security->credentials, mem_ctx,
452                                                       &flags, challenge_blob, target_info,
453                                                       &lm_response, &nt_response,
454                                                       &lm_session_key, &session_key);
455
456         if (!NT_STATUS_IS_OK(nt_status)) {
457                 return nt_status;
458         }
459
460         if (!(flags & CLI_CRED_LANMAN_AUTH)) {
461                 /* LM Key is still possible, just silly, so we do not
462                  * allow it. Fortunetly all LM crypto is off by
463                  * default and we require command line options to end
464                  * up here */
465                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
466         }
467
468         if (!(flags & CLI_CRED_NTLM2)) {
469                 /* NTLM2 is incompatible... */
470                 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
471         }
472
473         if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
474             && lpcfg_client_lanman_auth(gensec_security->settings->lp_ctx) && lm_session_key.length == 16) {
475                 DATA_BLOB new_session_key = data_blob_talloc(mem_ctx, NULL, 16);
476                 if (lm_response.length == 24) {
477                         SMBsesskeygen_lm_sess_key(lm_session_key.data, lm_response.data,
478                                                   new_session_key.data);
479                 } else {
480                         static const uint8_t zeros[24];
481                         SMBsesskeygen_lm_sess_key(lm_session_key.data, zeros,
482                                                   new_session_key.data);
483                 }
484                 session_key = new_session_key;
485                 dump_data_pw("LM session key\n", session_key.data, session_key.length);
486         }
487
488
489         /* Key exchange encryptes a new client-generated session key with
490            the password-derived key */
491         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
492                 /* Make up a new session key */
493                 uint8_t client_session_key[16];
494                 generate_secret_buffer(client_session_key, sizeof(client_session_key));
495
496                 /* Encrypt the new session key with the old one */
497                 encrypted_session_key = data_blob_talloc(ntlmssp_state,
498                                                          client_session_key, sizeof(client_session_key));
499                 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
500                 arcfour_crypt(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
501                 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
502
503                 /* Mark the new session key as the 'real' session key */
504                 session_key = data_blob_talloc(mem_ctx, client_session_key, sizeof(client_session_key));
505         }
506
507         /* this generates the actual auth packet */
508         nt_status = msrpc_gen(mem_ctx,
509                        out, auth_gen_string,
510                        "NTLMSSP",
511                        NTLMSSP_AUTH,
512                        lm_response.data, lm_response.length,
513                        nt_response.data, nt_response.length,
514                        domain,
515                        user,
516                        workstation,
517                        encrypted_session_key.data, encrypted_session_key.length,
518                        ntlmssp_state->neg_flags,
519                        version_blob.data, version_blob.length);
520         if (!NT_STATUS_IS_OK(nt_status)) {
521                 talloc_free(mem_ctx);
522                 return nt_status;
523         }
524
525 done:
526         ntlmssp_state->session_key = session_key;
527         talloc_steal(ntlmssp_state, session_key.data);
528
529         DEBUG(3, ("NTLMSSP: Set final flags:\n"));
530         debug_ntlmssp_flags(ntlmssp_state->neg_flags);
531
532         talloc_steal(out_mem_ctx, out->data);
533
534         ntlmssp_state->expected_state = NTLMSSP_DONE;
535
536         if (gensec_security->want_features & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL)) {
537                 nt_status = ntlmssp_sign_init(ntlmssp_state);
538                 if (!NT_STATUS_IS_OK(nt_status)) {
539                         DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n",
540                                   nt_errstr(nt_status)));
541                         talloc_free(mem_ctx);
542                         return nt_status;
543                 }
544         }
545
546         talloc_free(mem_ctx);
547         return NT_STATUS_OK;
548 }
549
550 NTSTATUS gensec_ntlmssp_client_start(struct gensec_security *gensec_security)
551 {
552         struct gensec_ntlmssp_context *gensec_ntlmssp;
553         struct ntlmssp_state *ntlmssp_state;
554         NTSTATUS nt_status;
555
556         nt_status = gensec_ntlmssp_start(gensec_security);
557         NT_STATUS_NOT_OK_RETURN(nt_status);
558
559         gensec_ntlmssp =
560                 talloc_get_type_abort(gensec_security->private_data,
561                                       struct gensec_ntlmssp_context);
562
563         ntlmssp_state = talloc_zero(gensec_ntlmssp,
564                                     struct ntlmssp_state);
565         if (!ntlmssp_state) {
566                 return NT_STATUS_NO_MEMORY;
567         }
568
569         gensec_ntlmssp->ntlmssp_state = ntlmssp_state;
570
571         ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
572
573         ntlmssp_state->role = NTLMSSP_CLIENT;
574
575         ntlmssp_state->client.netbios_domain = lpcfg_workgroup(gensec_security->settings->lp_ctx);
576         ntlmssp_state->client.netbios_name = cli_credentials_get_workstation(gensec_security->credentials);
577
578         ntlmssp_state->unicode = gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "unicode", true);
579
580         ntlmssp_state->use_nt_response = gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "send_nt_reponse", true);
581
582         ntlmssp_state->allow_lm_key = (lpcfg_client_lanman_auth(gensec_security->settings->lp_ctx)
583                                               && (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "allow_lm_key", false)
584                                                   || gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "lm_key", false)));
585
586         ntlmssp_state->use_ntlmv2 = lpcfg_client_ntlmv2_auth(gensec_security->settings->lp_ctx);
587
588         ntlmssp_state->expected_state = NTLMSSP_INITIAL;
589
590         ntlmssp_state->neg_flags =
591                 NTLMSSP_NEGOTIATE_NTLM |
592                 NTLMSSP_NEGOTIATE_VERSION |
593                 NTLMSSP_REQUEST_TARGET;
594
595         if (ntlmssp_state->unicode) {
596                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
597         } else {
598                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
599         }
600
601         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "128bit", true)) {
602                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
603         }
604
605         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "56bit", false)) {
606                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
607         }
608
609         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "lm_key", false)) {
610                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
611         }
612
613         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "keyexchange", true)) {
614                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
615         }
616
617         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "alwayssign", true)) {
618                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
619         }
620
621         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "ntlm2", true)) {
622                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
623         } else {
624                 /* apparently we can't do ntlmv2 if we don't do ntlm2 */
625                 ntlmssp_state->use_ntlmv2 = false;
626         }
627
628         if (ntlmssp_state->use_ntlmv2) {
629                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
630         }
631
632         if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
633                 /*
634                  * We need to set this to allow a later SetPassword
635                  * via the SAMR pipe to succeed. Strange.... We could
636                  * also add  NTLMSSP_NEGOTIATE_SEAL here. JRA.
637                  *
638                  * Without this, Windows will not create the master key
639                  * that it thinks is only used for NTLMSSP signing and
640                  * sealing.  (It is actually pulled out and used directly)
641                  */
642                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
643         }
644         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
645                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
646
647                 if (gensec_security->want_features & GENSEC_FEATURE_LDAP_STYLE) {
648                         /*
649                          * We need to handle NTLMSSP_NEGOTIATE_SIGN as
650                          * NTLMSSP_NEGOTIATE_SEAL if GENSEC_FEATURE_LDAP_STYLE
651                          * is requested.
652                          */
653                         ntlmssp_state->force_wrap_seal = true;
654                         /*
655                          * We want also work against old Samba servers
656                          * which didn't had GENSEC_FEATURE_LDAP_STYLE
657                          * we negotiate SEAL too. We may remove this
658                          * in a few years. As all servers should have
659                          * GENSEC_FEATURE_LDAP_STYLE by then.
660                          */
661                         ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
662                 }
663         }
664         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
665                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
666                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
667         }
668         if (gensec_security->want_features & GENSEC_FEATURE_NTLM_CCACHE) {
669                 ntlmssp_state->use_ccache = true;
670         }
671
672         return NT_STATUS_OK;
673 }
674
675 NTSTATUS gensec_ntlmssp_resume_ccache_start(struct gensec_security *gensec_security)
676 {
677         struct gensec_ntlmssp_context *gensec_ntlmssp = NULL;
678         NTSTATUS status;
679
680         status = gensec_ntlmssp_client_start(gensec_security);
681         if (!NT_STATUS_IS_OK(status)) {
682                 return status;
683         }
684
685         gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
686                                                struct gensec_ntlmssp_context);
687         gensec_ntlmssp->ntlmssp_state->use_ccache = false;
688         gensec_ntlmssp->ntlmssp_state->resume_ccache = true;
689         gensec_ntlmssp->ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
690
691         return NT_STATUS_OK;
692 }