auth/ntlmssp: avoid talloc_tos() in ntlmssp_client_initial()
[Samba/bjacke.git] / auth / ntlmssp / ntlmssp_client.c
blobfc66a8d8e33b402885be7a32d52a58adf4db9227
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 handle NLTMSSP, client server side parsing
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8 Copyright (C) Stefan Metzmacher 2005
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.
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.
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/>.
24 struct auth_session_info;
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 "param/param.h"
33 #include "auth/ntlmssp/ntlmssp_private.h"
34 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
35 #include "../auth/ntlmssp/ntlmssp_ndr.h"
37 /*********************************************************************
38 Client side NTLMSSP
39 *********************************************************************/
41 /**
42 * Next state function for the Initial packet
44 * @param ntlmssp_state NTLMSSP State
45 * @param out_mem_ctx The DATA_BLOB *out will be allocated on this context
46 * @param in A NULL data blob (input ignored)
47 * @param out The initial negotiate request to the server, as an talloc()ed DATA_BLOB, on out_mem_ctx
48 * @return Errors or NT_STATUS_OK.
51 NTSTATUS ntlmssp_client_initial(struct gensec_security *gensec_security,
52 TALLOC_CTX *out_mem_ctx,
53 DATA_BLOB in, DATA_BLOB *out)
55 struct gensec_ntlmssp_context *gensec_ntlmssp =
56 talloc_get_type_abort(gensec_security->private_data,
57 struct gensec_ntlmssp_context);
58 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
59 const char *domain = ntlmssp_state->client.netbios_domain;
60 const char *workstation = ntlmssp_state->client.netbios_name;
61 NTSTATUS status;
63 /* These don't really matter in the initial packet, so don't panic if they are not set */
64 if (!domain) {
65 domain = "";
68 if (!workstation) {
69 workstation = "";
72 if (ntlmssp_state->unicode) {
73 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
74 } else {
75 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
78 if (ntlmssp_state->use_ntlmv2) {
79 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
82 /* generate the ntlmssp negotiate packet */
83 status = msrpc_gen(out_mem_ctx,
84 out, "CddAA",
85 "NTLMSSP",
86 NTLMSSP_NEGOTIATE,
87 ntlmssp_state->neg_flags,
88 domain,
89 workstation);
91 if (!NT_STATUS_IS_OK(status)) {
92 DEBUG(0, ("ntlmssp_client_initial: failed to generate "
93 "ntlmssp negotiate packet\n"));
94 return status;
97 if (DEBUGLEVEL >= 10) {
98 struct NEGOTIATE_MESSAGE *negotiate = talloc(
99 ntlmssp_state, struct NEGOTIATE_MESSAGE);
100 if (negotiate != NULL) {
101 status = ntlmssp_pull_NEGOTIATE_MESSAGE(
102 out, negotiate, negotiate);
103 if (NT_STATUS_IS_OK(status)) {
104 NDR_PRINT_DEBUG(NEGOTIATE_MESSAGE,
105 negotiate);
107 TALLOC_FREE(negotiate);
111 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
113 return NT_STATUS_MORE_PROCESSING_REQUIRED;
117 * Next state function for the Challenge Packet. Generate an auth packet.
119 * @param gensec_security GENSEC state
120 * @param out_mem_ctx Memory context for *out
121 * @param in The server challnege, as a DATA_BLOB. reply.data must be NULL
122 * @param out The next request (auth packet) to the server, as an allocated DATA_BLOB, on the out_mem_ctx context
123 * @return Errors or NT_STATUS_OK.
126 NTSTATUS ntlmssp_client_challenge(struct gensec_security *gensec_security,
127 TALLOC_CTX *out_mem_ctx,
128 const DATA_BLOB in, DATA_BLOB *out)
130 struct gensec_ntlmssp_context *gensec_ntlmssp =
131 talloc_get_type_abort(gensec_security->private_data,
132 struct gensec_ntlmssp_context);
133 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
134 uint32_t chal_flags, ntlmssp_command, unkn1, unkn2;
135 DATA_BLOB server_domain_blob;
136 DATA_BLOB challenge_blob;
137 DATA_BLOB target_info = data_blob(NULL, 0);
138 char *server_domain;
139 const char *chal_parse_string;
140 const char *auth_gen_string;
141 DATA_BLOB lm_response = data_blob(NULL, 0);
142 DATA_BLOB nt_response = data_blob(NULL, 0);
143 DATA_BLOB session_key = data_blob(NULL, 0);
144 DATA_BLOB lm_session_key = data_blob(NULL, 0);
145 DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
146 NTSTATUS nt_status;
147 int flags = 0;
148 const char *user, *domain;
150 TALLOC_CTX *mem_ctx = talloc_new(out_mem_ctx);
151 if (!mem_ctx) {
152 return NT_STATUS_NO_MEMORY;
155 if (!msrpc_parse(mem_ctx,
156 &in, "CdBd",
157 "NTLMSSP",
158 &ntlmssp_command,
159 &server_domain_blob,
160 &chal_flags)) {
161 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
162 dump_data(2, in.data, in.length);
163 talloc_free(mem_ctx);
165 return NT_STATUS_INVALID_PARAMETER;
168 data_blob_free(&server_domain_blob);
170 DEBUG(3, ("Got challenge flags:\n"));
171 debug_ntlmssp_flags(chal_flags);
173 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, ntlmssp_state->allow_lm_key);
175 if (ntlmssp_state->unicode) {
176 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
177 chal_parse_string = "CdUdbddB";
178 } else {
179 chal_parse_string = "CdUdbdd";
181 auth_gen_string = "CdBBUUUBd";
182 } else {
183 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
184 chal_parse_string = "CdAdbddB";
185 } else {
186 chal_parse_string = "CdAdbdd";
189 auth_gen_string = "CdBBAAABd";
192 if (!msrpc_parse(mem_ctx,
193 &in, chal_parse_string,
194 "NTLMSSP",
195 &ntlmssp_command,
196 &server_domain,
197 &chal_flags,
198 &challenge_blob, 8,
199 &unkn1, &unkn2,
200 &target_info)) {
201 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
202 dump_data(2, in.data, in.length);
203 talloc_free(mem_ctx);
204 return NT_STATUS_INVALID_PARAMETER;
207 if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
208 ntlmssp_state->server.is_standalone = true;
209 } else {
210 ntlmssp_state->server.is_standalone = false;
212 /* TODO: parse struct_blob and fill in the rest */
213 ntlmssp_state->server.netbios_name = "";
214 ntlmssp_state->server.netbios_domain = server_domain;
215 ntlmssp_state->server.dns_name = "";
216 ntlmssp_state->server.dns_domain = "";
218 if (challenge_blob.length != 8) {
219 talloc_free(mem_ctx);
220 return NT_STATUS_INVALID_PARAMETER;
223 cli_credentials_get_ntlm_username_domain(gensec_security->credentials, mem_ctx,
224 &user, &domain);
226 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
227 flags |= CLI_CRED_NTLM2;
229 if (ntlmssp_state->use_ntlmv2) {
230 flags |= CLI_CRED_NTLMv2_AUTH;
232 if (ntlmssp_state->use_nt_response) {
233 flags |= CLI_CRED_NTLM_AUTH;
235 if (lpcfg_client_lanman_auth(gensec_security->settings->lp_ctx)) {
236 flags |= CLI_CRED_LANMAN_AUTH;
239 nt_status = cli_credentials_get_ntlm_response(gensec_security->credentials, mem_ctx,
240 &flags, challenge_blob, target_info,
241 &lm_response, &nt_response,
242 &lm_session_key, &session_key);
244 if (!NT_STATUS_IS_OK(nt_status)) {
245 return nt_status;
248 if (!(flags & CLI_CRED_LANMAN_AUTH)) {
249 /* LM Key is still possible, just silly, so we do not
250 * allow it. Fortunetly all LM crypto is off by
251 * default and we require command line options to end
252 * up here */
253 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
256 if (!(flags & CLI_CRED_NTLM2)) {
257 /* NTLM2 is incompatible... */
258 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
261 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
262 && lpcfg_client_lanman_auth(gensec_security->settings->lp_ctx) && lm_session_key.length == 16) {
263 DATA_BLOB new_session_key = data_blob_talloc(mem_ctx, NULL, 16);
264 if (lm_response.length == 24) {
265 SMBsesskeygen_lm_sess_key(lm_session_key.data, lm_response.data,
266 new_session_key.data);
267 } else {
268 static const uint8_t zeros[24];
269 SMBsesskeygen_lm_sess_key(lm_session_key.data, zeros,
270 new_session_key.data);
272 session_key = new_session_key;
273 dump_data_pw("LM session key\n", session_key.data, session_key.length);
277 /* Key exchange encryptes a new client-generated session key with
278 the password-derived key */
279 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
280 /* Make up a new session key */
281 uint8_t client_session_key[16];
282 generate_secret_buffer(client_session_key, sizeof(client_session_key));
284 /* Encrypt the new session key with the old one */
285 encrypted_session_key = data_blob_talloc(ntlmssp_state,
286 client_session_key, sizeof(client_session_key));
287 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
288 arcfour_crypt(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
289 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
291 /* Mark the new session key as the 'real' session key */
292 session_key = data_blob_talloc(mem_ctx, client_session_key, sizeof(client_session_key));
295 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
296 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
298 /* this generates the actual auth packet */
299 nt_status = msrpc_gen(mem_ctx,
300 out, auth_gen_string,
301 "NTLMSSP",
302 NTLMSSP_AUTH,
303 lm_response.data, lm_response.length,
304 nt_response.data, nt_response.length,
305 domain,
306 user,
307 cli_credentials_get_workstation(gensec_security->credentials),
308 encrypted_session_key.data, encrypted_session_key.length,
309 ntlmssp_state->neg_flags);
310 if (!NT_STATUS_IS_OK(nt_status)) {
311 talloc_free(mem_ctx);
312 return nt_status;
315 ntlmssp_state->session_key = session_key;
316 talloc_steal(ntlmssp_state, session_key.data);
318 talloc_steal(out_mem_ctx, out->data);
320 ntlmssp_state->expected_state = NTLMSSP_DONE;
322 if (gensec_security->want_features & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL)) {
323 nt_status = ntlmssp_sign_init(ntlmssp_state);
324 if (!NT_STATUS_IS_OK(nt_status)) {
325 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n",
326 nt_errstr(nt_status)));
327 talloc_free(mem_ctx);
328 return nt_status;
332 talloc_free(mem_ctx);
333 return NT_STATUS_OK;
336 NTSTATUS gensec_ntlmssp_client_start(struct gensec_security *gensec_security)
338 struct gensec_ntlmssp_context *gensec_ntlmssp;
339 struct ntlmssp_state *ntlmssp_state;
340 NTSTATUS nt_status;
342 nt_status = gensec_ntlmssp_start(gensec_security);
343 NT_STATUS_NOT_OK_RETURN(nt_status);
345 gensec_ntlmssp =
346 talloc_get_type_abort(gensec_security->private_data,
347 struct gensec_ntlmssp_context);
349 ntlmssp_state = talloc_zero(gensec_ntlmssp,
350 struct ntlmssp_state);
351 if (!ntlmssp_state) {
352 return NT_STATUS_NO_MEMORY;
355 gensec_ntlmssp->ntlmssp_state = ntlmssp_state;
357 ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
359 ntlmssp_state->role = NTLMSSP_CLIENT;
361 ntlmssp_state->client.netbios_domain = lpcfg_workgroup(gensec_security->settings->lp_ctx);
362 ntlmssp_state->client.netbios_name = cli_credentials_get_workstation(gensec_security->credentials);
364 ntlmssp_state->unicode = gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "unicode", true);
366 ntlmssp_state->use_nt_response = gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "send_nt_reponse", true);
368 ntlmssp_state->allow_lm_key = (lpcfg_client_lanman_auth(gensec_security->settings->lp_ctx)
369 && (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "allow_lm_key", false)
370 || gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "lm_key", false)));
372 ntlmssp_state->use_ntlmv2 = lpcfg_client_ntlmv2_auth(gensec_security->settings->lp_ctx);
374 ntlmssp_state->expected_state = NTLMSSP_INITIAL;
376 ntlmssp_state->neg_flags =
377 NTLMSSP_NEGOTIATE_NTLM |
378 NTLMSSP_REQUEST_TARGET;
380 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "128bit", true)) {
381 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
384 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "56bit", false)) {
385 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
388 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "lm_key", false)) {
389 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
392 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "keyexchange", true)) {
393 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
396 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "alwayssign", true)) {
397 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
400 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "ntlm2", true)) {
401 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
402 } else {
403 /* apparently we can't do ntlmv2 if we don't do ntlm2 */
404 ntlmssp_state->use_ntlmv2 = false;
407 if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
409 * We need to set this to allow a later SetPassword
410 * via the SAMR pipe to succeed. Strange.... We could
411 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
413 * Without this, Windows will not create the master key
414 * that it thinks is only used for NTLMSSP signing and
415 * sealing. (It is actually pulled out and used directly)
417 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
419 if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
420 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
422 if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
423 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
424 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
427 return NT_STATUS_OK;