source: trunk/server/source4/auth/ntlmssp/ntlmssp_client.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 13.4 KB
Line 
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#include "includes.h"
25#include "auth/ntlmssp/ntlmssp.h"
26#include "../lib/crypto/crypto.h"
27#include "../libcli/auth/libcli_auth.h"
28#include "auth/credentials/credentials.h"
29#include "auth/gensec/gensec.h"
30#include "param/param.h"
31#include "libcli/auth/ntlmssp_private.h"
32
33/*********************************************************************
34 Client side NTLMSSP
35*********************************************************************/
36
37/**
38 * Next state function for the Initial packet
39 *
40 * @param ntlmssp_state NTLMSSP State
41 * @param out_mem_ctx The DATA_BLOB *out will be allocated on this context
42 * @param in A NULL data blob (input ignored)
43 * @param out The initial negotiate request to the server, as an talloc()ed DATA_BLOB, on out_mem_ctx
44 * @return Errors or NT_STATUS_OK.
45 */
46
47NTSTATUS ntlmssp_client_initial(struct gensec_security *gensec_security,
48 TALLOC_CTX *out_mem_ctx,
49 DATA_BLOB in, DATA_BLOB *out)
50{
51 struct gensec_ntlmssp_context *gensec_ntlmssp =
52 talloc_get_type_abort(gensec_security->private_data,
53 struct gensec_ntlmssp_context);
54 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
55 const char *domain = ntlmssp_state->domain;
56 const char *workstation = cli_credentials_get_workstation(gensec_security->credentials);
57 NTSTATUS status;
58
59 /* These don't really matter in the initial packet, so don't panic if they are not set */
60 if (!domain) {
61 domain = "";
62 }
63
64 if (!workstation) {
65 workstation = "";
66 }
67
68 if (ntlmssp_state->unicode) {
69 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_UNICODE;
70 } else {
71 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_OEM;
72 }
73
74 if (ntlmssp_state->use_ntlmv2) {
75 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
76 }
77
78 /* generate the ntlmssp negotiate packet */
79 status = msrpc_gen(out_mem_ctx,
80 out, "CddAA",
81 "NTLMSSP",
82 NTLMSSP_NEGOTIATE,
83 ntlmssp_state->neg_flags,
84 domain,
85 workstation);
86
87 if (!NT_STATUS_IS_OK(status)) {
88 return status;
89 }
90
91 ntlmssp_state->expected_state = NTLMSSP_CHALLENGE;
92
93 return NT_STATUS_MORE_PROCESSING_REQUIRED;
94}
95
96/**
97 * Next state function for the Challenge Packet. Generate an auth packet.
98 *
99 * @param gensec_security GENSEC state
100 * @param out_mem_ctx Memory context for *out
101 * @param in The server challnege, as a DATA_BLOB. reply.data must be NULL
102 * @param out The next request (auth packet) to the server, as an allocated DATA_BLOB, on the out_mem_ctx context
103 * @return Errors or NT_STATUS_OK.
104 */
105
106NTSTATUS ntlmssp_client_challenge(struct gensec_security *gensec_security,
107 TALLOC_CTX *out_mem_ctx,
108 const DATA_BLOB in, DATA_BLOB *out)
109{
110 struct gensec_ntlmssp_context *gensec_ntlmssp =
111 talloc_get_type_abort(gensec_security->private_data,
112 struct gensec_ntlmssp_context);
113 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
114 uint32_t chal_flags, ntlmssp_command, unkn1, unkn2;
115 DATA_BLOB server_domain_blob;
116 DATA_BLOB challenge_blob;
117 DATA_BLOB target_info = data_blob(NULL, 0);
118 char *server_domain;
119 const char *chal_parse_string;
120 const char *auth_gen_string;
121 DATA_BLOB lm_response = data_blob(NULL, 0);
122 DATA_BLOB nt_response = data_blob(NULL, 0);
123 DATA_BLOB session_key = data_blob(NULL, 0);
124 DATA_BLOB lm_session_key = data_blob(NULL, 0);
125 DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
126 NTSTATUS nt_status;
127 int flags = 0;
128 const char *user, *domain;
129
130 TALLOC_CTX *mem_ctx = talloc_new(out_mem_ctx);
131 if (!mem_ctx) {
132 return NT_STATUS_NO_MEMORY;
133 }
134
135 if (!msrpc_parse(mem_ctx,
136 &in, "CdBd",
137 "NTLMSSP",
138 &ntlmssp_command,
139 &server_domain_blob,
140 &chal_flags)) {
141 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
142 dump_data(2, in.data, in.length);
143 talloc_free(mem_ctx);
144
145 return NT_STATUS_INVALID_PARAMETER;
146 }
147
148 data_blob_free(&server_domain_blob);
149
150 DEBUG(3, ("Got challenge flags:\n"));
151 debug_ntlmssp_flags(chal_flags);
152
153 ntlmssp_handle_neg_flags(ntlmssp_state, chal_flags, ntlmssp_state->allow_lm_key);
154
155 if (ntlmssp_state->unicode) {
156 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
157 chal_parse_string = "CdUdbddB";
158 } else {
159 chal_parse_string = "CdUdbdd";
160 }
161 auth_gen_string = "CdBBUUUBd";
162 } else {
163 if (chal_flags & NTLMSSP_NEGOTIATE_TARGET_INFO) {
164 chal_parse_string = "CdAdbddB";
165 } else {
166 chal_parse_string = "CdAdbdd";
167 }
168
169 auth_gen_string = "CdBBAAABd";
170 }
171
172 if (!msrpc_parse(mem_ctx,
173 &in, chal_parse_string,
174 "NTLMSSP",
175 &ntlmssp_command,
176 &server_domain,
177 &chal_flags,
178 &challenge_blob, 8,
179 &unkn1, &unkn2,
180 &target_info)) {
181 DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
182 dump_data(2, in.data, in.length);
183 talloc_free(mem_ctx);
184 return NT_STATUS_INVALID_PARAMETER;
185 }
186
187 if (chal_flags & NTLMSSP_TARGET_TYPE_SERVER) {
188 ntlmssp_state->server.is_standalone = true;
189 } else {
190 ntlmssp_state->server.is_standalone = false;
191 }
192 /* TODO: parse struct_blob and fill in the rest */
193 ntlmssp_state->server.netbios_name = "";
194 ntlmssp_state->server.netbios_domain = server_domain;
195 ntlmssp_state->server.dns_name = "";
196 ntlmssp_state->server.dns_domain = "";
197
198 if (challenge_blob.length != 8) {
199 talloc_free(mem_ctx);
200 return NT_STATUS_INVALID_PARAMETER;
201 }
202
203 cli_credentials_get_ntlm_username_domain(gensec_security->credentials, mem_ctx,
204 &user, &domain);
205
206 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
207 flags |= CLI_CRED_NTLM2;
208 }
209 if (ntlmssp_state->use_ntlmv2) {
210 flags |= CLI_CRED_NTLMv2_AUTH;
211 }
212 if (ntlmssp_state->use_nt_response) {
213 flags |= CLI_CRED_NTLM_AUTH;
214 }
215 if (lpcfg_client_lanman_auth(gensec_security->settings->lp_ctx)) {
216 flags |= CLI_CRED_LANMAN_AUTH;
217 }
218
219 nt_status = cli_credentials_get_ntlm_response(gensec_security->credentials, mem_ctx,
220 &flags, challenge_blob, target_info,
221 &lm_response, &nt_response,
222 &lm_session_key, &session_key);
223
224 if (!NT_STATUS_IS_OK(nt_status)) {
225 return nt_status;
226 }
227
228 if (!(flags & CLI_CRED_LANMAN_AUTH)) {
229 /* LM Key is still possible, just silly. Fortunetly
230 * we require command line options to end up here */
231 /* ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY; */
232 }
233
234 if (!(flags & CLI_CRED_NTLM2)) {
235 /* NTLM2 is incompatible... */
236 ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
237 }
238
239 if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY)
240 && lpcfg_client_lanman_auth(gensec_security->settings->lp_ctx) && lm_session_key.length == 16) {
241 DATA_BLOB new_session_key = data_blob_talloc(mem_ctx, NULL, 16);
242 if (lm_response.length == 24) {
243 SMBsesskeygen_lm_sess_key(lm_session_key.data, lm_response.data,
244 new_session_key.data);
245 } else {
246 static const uint8_t zeros[24];
247 SMBsesskeygen_lm_sess_key(lm_session_key.data, zeros,
248 new_session_key.data);
249 }
250 session_key = new_session_key;
251 dump_data_pw("LM session key\n", session_key.data, session_key.length);
252 }
253
254
255 /* Key exchange encryptes a new client-generated session key with
256 the password-derived key */
257 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
258 /* Make up a new session key */
259 uint8_t client_session_key[16];
260 generate_secret_buffer(client_session_key, sizeof(client_session_key));
261
262 /* Encrypt the new session key with the old one */
263 encrypted_session_key = data_blob_talloc(ntlmssp_state,
264 client_session_key, sizeof(client_session_key));
265 dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
266 arcfour_crypt(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
267 dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
268
269 /* Mark the new session key as the 'real' session key */
270 session_key = data_blob_talloc(mem_ctx, client_session_key, sizeof(client_session_key));
271 }
272
273 DEBUG(3, ("NTLMSSP: Set final flags:\n"));
274 debug_ntlmssp_flags(ntlmssp_state->neg_flags);
275
276 /* this generates the actual auth packet */
277 nt_status = msrpc_gen(mem_ctx,
278 out, auth_gen_string,
279 "NTLMSSP",
280 NTLMSSP_AUTH,
281 lm_response.data, lm_response.length,
282 nt_response.data, nt_response.length,
283 domain,
284 user,
285 cli_credentials_get_workstation(gensec_security->credentials),
286 encrypted_session_key.data, encrypted_session_key.length,
287 ntlmssp_state->neg_flags);
288 if (!NT_STATUS_IS_OK(nt_status)) {
289 talloc_free(mem_ctx);
290 return nt_status;
291 }
292
293 ntlmssp_state->session_key = session_key;
294 talloc_steal(ntlmssp_state, session_key.data);
295
296 talloc_steal(out_mem_ctx, out->data);
297
298 ntlmssp_state->chal = challenge_blob;
299 ntlmssp_state->lm_resp = lm_response;
300 talloc_steal(ntlmssp_state->lm_resp.data, lm_response.data);
301 ntlmssp_state->nt_resp = nt_response;
302 talloc_steal(ntlmssp_state->nt_resp.data, nt_response.data);
303
304 ntlmssp_state->expected_state = NTLMSSP_DONE;
305
306 if (gensec_security->want_features & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL)) {
307 nt_status = ntlmssp_sign_init(ntlmssp_state);
308 if (!NT_STATUS_IS_OK(nt_status)) {
309 DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n",
310 nt_errstr(nt_status)));
311 talloc_free(mem_ctx);
312 return nt_status;
313 }
314 }
315
316 talloc_free(mem_ctx);
317 return NT_STATUS_OK;
318}
319
320NTSTATUS gensec_ntlmssp_client_start(struct gensec_security *gensec_security)
321{
322 struct gensec_ntlmssp_context *gensec_ntlmssp;
323 struct ntlmssp_state *ntlmssp_state;
324 NTSTATUS nt_status;
325
326 nt_status = gensec_ntlmssp_start(gensec_security);
327 NT_STATUS_NOT_OK_RETURN(nt_status);
328
329 gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
330 struct gensec_ntlmssp_context);
331 ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
332
333 ntlmssp_state->role = NTLMSSP_CLIENT;
334
335 ntlmssp_state->domain = lpcfg_workgroup(gensec_security->settings->lp_ctx);
336
337 ntlmssp_state->unicode = gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "unicode", true);
338
339 ntlmssp_state->use_nt_response = gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "send_nt_reponse", true);
340
341 ntlmssp_state->allow_lm_key = (lpcfg_client_lanman_auth(gensec_security->settings->lp_ctx)
342 && (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "allow_lm_key", false)
343 || gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "lm_key", false)));
344
345 ntlmssp_state->use_ntlmv2 = lpcfg_client_ntlmv2_auth(gensec_security->settings->lp_ctx);
346
347 ntlmssp_state->expected_state = NTLMSSP_INITIAL;
348
349 ntlmssp_state->neg_flags =
350 NTLMSSP_NEGOTIATE_NTLM |
351 NTLMSSP_REQUEST_TARGET;
352
353 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "128bit", true)) {
354 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
355 }
356
357 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "56bit", false)) {
358 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
359 }
360
361 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "lm_key", false)) {
362 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
363 }
364
365 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "keyexchange", true)) {
366 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
367 }
368
369 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "alwayssign", true)) {
370 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
371 }
372
373 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_client", "ntlm2", true)) {
374 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
375 } else {
376 /* apparently we can't do ntlmv2 if we don't do ntlm2 */
377 ntlmssp_state->use_ntlmv2 = false;
378 }
379
380 if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
381 /*
382 * We need to set this to allow a later SetPassword
383 * via the SAMR pipe to succeed. Strange.... We could
384 * also add NTLMSSP_NEGOTIATE_SEAL here. JRA.
385 *
386 * Without this, Windows will not create the master key
387 * that it thinks is only used for NTLMSSP signing and
388 * sealing. (It is actually pulled out and used directly)
389 */
390 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
391 }
392 if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
393 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
394 }
395 if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
396 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
397 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
398 }
399
400 return NT_STATUS_OK;
401}
402
Note: See TracBrowser for help on using the repository browser.