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