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