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