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