Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into 4-0-abartlet
[samba.git] / source4 / torture / rpc / remote_pac.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test suite for netlogon PAC operations
5
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "lib/events/events.h"
25 #include "auth/auth.h"
26 #include "auth/gensec/gensec.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "torture/rpc/rpc.h"
29 #include "torture/rpc/netlogon.h"
30 #include "libcli/auth/libcli_auth.h"
31 #include "librpc/gen_ndr/ndr_netlogon_c.h"
32 #include "librpc/gen_ndr/ndr_krb5pac.h"
33 #include "param/param.h"
34 #include "lib/messaging/irpc.h"
35 #include "cluster/cluster.h"
36
37 #define TEST_MACHINE_NAME "torturepactest"
38
39 /* Check to see if we can pass the PAC across to the NETLOGON server for validation */
40
41 /* Also happens to be a really good one-step verfication of our Kerberos stack */
42
43 static bool test_PACVerify(struct torture_context *tctx, 
44                            struct dcerpc_pipe *p,
45                            struct cli_credentials *credentials)
46 {
47         NTSTATUS status;
48
49         struct netr_LogonSamLogon r;
50         
51         struct netr_GenericInfo generic;
52         struct netr_Authenticator auth, auth2;
53         
54
55         struct creds_CredentialState *creds;
56         struct gensec_security *gensec_client_context;
57         struct gensec_security *gensec_server_context;
58
59         struct messaging_context *msg_server_ctx;
60         DATA_BLOB client_to_server, server_to_client, pac_wrapped, payload;
61         struct PAC_Validate pac_wrapped_struct;
62         
63         enum ndr_err_code ndr_err;
64
65         struct auth_session_info *session_info;
66
67         char *tmp_dir;
68
69         TALLOC_CTX *tmp_ctx = talloc_new(tctx);
70         
71         int i;
72
73         torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
74
75         if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS, 
76                                     credentials, SEC_CHAN_BDC, 
77                                     &creds)) {
78                 return false;
79         }
80
81         status = torture_temp_dir(tctx, "PACVerify", &tmp_dir);
82         torture_assert_ntstatus_ok(tctx, status, "torture_temp_dir failed");
83
84         msg_server_ctx = messaging_init(tctx, 
85                                         tmp_dir,
86                                         cluster_id(0, 1), 
87                                         lp_iconv_convenience(tctx->lp_ctx),
88                                         tctx->ev);
89         
90         torture_assert(tctx, msg_server_ctx != NULL, "Failed to init messaging context");
91
92         status = gensec_client_start(tctx, &gensec_client_context, tctx->ev, tctx->lp_ctx);
93         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
94
95         status = gensec_set_target_hostname(gensec_client_context, TEST_MACHINE_NAME);
96
97         status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
98         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
99
100         status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
101         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
102
103         status = gensec_server_start(tctx, tctx->ev, tctx->lp_ctx, msg_server_ctx, &gensec_server_context);
104         torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
105
106         status = gensec_set_credentials(gensec_server_context, credentials);
107         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
108
109         status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
110         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
111
112         server_to_client = data_blob(NULL, 0);
113         
114         do {
115                 /* Do a client-server update dance */
116                 status = gensec_update(gensec_client_context, tmp_ctx, server_to_client, &client_to_server);
117                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
118                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
119                 }
120
121                 if (client_to_server.length == 0) {
122                         break;
123                 }
124
125                 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
126                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
127                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
128                 }
129
130                 if (server_to_client.length == 0) {
131                         break;
132                 }
133         } while (1);
134
135         /* Extract the PAC using Samba's code */
136
137         status = gensec_session_info(gensec_server_context, &session_info);
138         torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
139         
140         pac_wrapped_struct.MessageType = 0x3;
141         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
142         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
143         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
144         pac_wrapped_struct.ChecksumAndSignature = payload
145                 = data_blob_talloc(tmp_ctx, NULL, 
146                                    pac_wrapped_struct.ChecksumLength
147                                    + pac_wrapped_struct.SignatureLength);
148         memcpy(&payload.data[0], 
149                session_info->server_info->pac_srv_sig.signature.data, 
150                pac_wrapped_struct.ChecksumLength);
151         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
152                session_info->server_info->pac_kdc_sig.signature.data, 
153                pac_wrapped_struct.SignatureLength);
154
155         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
156                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
157         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
158                 
159         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
160         creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
161
162         generic.length = pac_wrapped.length;
163         generic.data = pac_wrapped.data;
164
165         /* Validate it over the netlogon pipe */
166
167         generic.identity_info.parameter_control = 0;
168         generic.identity_info.logon_id_high = 0;
169         generic.identity_info.logon_id_low = 0;
170         generic.identity_info.domain_name.string = session_info->server_info->domain_name;
171         generic.identity_info.account_name.string = session_info->server_info->account_name;
172         generic.identity_info.workstation.string = TEST_MACHINE_NAME;
173
174         generic.package_name.string = "Kerberos";
175
176         ZERO_STRUCT(auth2);
177         creds_client_authenticator(creds, &auth);
178         r.in.credential = &auth;
179         r.in.return_authenticator = &auth2;
180         r.in.logon_level = NetlogonGenericInformation;
181         r.in.logon.generic = &generic;
182         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
183         r.in.computer_name = cli_credentials_get_workstation(credentials);
184         r.in.validation_level = NetlogonValidationGenericInfo2;
185
186         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
187
188         torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
189         
190         /* This will break the signature nicely (even in the crypto wrapping), check we get a logon failure */
191         generic.data[generic.length-1]++;
192
193         ZERO_STRUCT(auth2);
194         creds_client_authenticator(creds, &auth);
195         r.in.credential = &auth;
196         r.in.return_authenticator = &auth2;
197         r.in.logon_level = NetlogonGenericInformation;
198         r.in.logon.generic = &generic;
199         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
200         r.in.computer_name = cli_credentials_get_workstation(credentials);
201         r.in.validation_level = NetlogonValidationGenericInfo2;
202
203         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
204
205         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
206         
207         torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred), 
208                        "Credential chaining failed");
209
210         /* This will break message type, check that however we still get NT_STATUS_OK */
211         for (i=0; i < 256; i++) {
212                 pac_wrapped_struct.MessageType = i;
213                 pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
214                 pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
215                 pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
216                 pac_wrapped_struct.ChecksumAndSignature = payload
217                         = data_blob_talloc(tmp_ctx, NULL, 
218                                            pac_wrapped_struct.ChecksumLength
219                                            + pac_wrapped_struct.SignatureLength);
220                 memcpy(&payload.data[0], 
221                        session_info->server_info->pac_srv_sig.signature.data, 
222                        pac_wrapped_struct.ChecksumLength);
223                 memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
224                        session_info->server_info->pac_kdc_sig.signature.data, 
225                        pac_wrapped_struct.SignatureLength);
226                 
227                 ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
228                                                (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
229                 torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
230                 
231                 torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
232                 creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
233                 
234                 generic.length = pac_wrapped.length;
235                 generic.data = pac_wrapped.data;
236                 
237                 ZERO_STRUCT(auth2);
238                 creds_client_authenticator(creds, &auth);
239                 r.in.credential = &auth;
240                 r.in.return_authenticator = &auth2;
241                 r.in.logon_level = NetlogonGenericInformation;
242                 r.in.logon.generic = &generic;
243                 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
244                 r.in.computer_name = cli_credentials_get_workstation(credentials);
245                 r.in.validation_level = NetlogonValidationGenericInfo2;
246                 
247                 status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
248                 
249                 torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
250
251                 torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred), 
252                                "Credential chaining failed");
253         }
254
255         /* This will break the parsing nicely (even in the crypto wrapping), check we get INVALID_PARAMETER */
256         generic.length--;
257
258         ZERO_STRUCT(auth2);
259         creds_client_authenticator(creds, &auth);
260         r.in.credential = &auth;
261         r.in.return_authenticator = &auth2;
262         r.in.logon_level = NetlogonGenericInformation;
263         r.in.logon.generic = &generic;
264         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
265         r.in.computer_name = cli_credentials_get_workstation(credentials);
266         r.in.validation_level = NetlogonValidationGenericInfo2;
267
268         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
269
270         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
271         
272         torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred), 
273                        "Credential chaining failed");
274
275         pac_wrapped_struct.MessageType = 0x3;
276         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
277         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
278         
279         /* Break the SignatureType */
280         pac_wrapped_struct.SignatureType++;
281
282         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
283         pac_wrapped_struct.ChecksumAndSignature = payload
284                 = data_blob_talloc(tmp_ctx, NULL, 
285                                    pac_wrapped_struct.ChecksumLength
286                                    + pac_wrapped_struct.SignatureLength);
287         memcpy(&payload.data[0], 
288                session_info->server_info->pac_srv_sig.signature.data, 
289                pac_wrapped_struct.ChecksumLength);
290         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
291                session_info->server_info->pac_kdc_sig.signature.data, 
292                pac_wrapped_struct.SignatureLength);
293         
294         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
295                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
296         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
297         
298         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
299         creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
300         
301         generic.length = pac_wrapped.length;
302         generic.data = pac_wrapped.data;
303         
304         ZERO_STRUCT(auth2);
305         creds_client_authenticator(creds, &auth);
306         r.in.credential = &auth;
307         r.in.return_authenticator = &auth2;
308         r.in.logon_level = NetlogonGenericInformation;
309         r.in.logon.generic = &generic;
310         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
311         r.in.computer_name = cli_credentials_get_workstation(credentials);
312         r.in.validation_level = NetlogonValidationGenericInfo2;
313         
314         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
315         
316         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
317         
318         torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred), 
319                        "Credential chaining failed");
320
321
322         pac_wrapped_struct.MessageType = 0x3;
323         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
324         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
325         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
326
327         pac_wrapped_struct.ChecksumAndSignature = payload
328                 = data_blob_talloc(tmp_ctx, NULL, 
329                                    pac_wrapped_struct.ChecksumLength
330                                    + pac_wrapped_struct.SignatureLength);
331         memcpy(&payload.data[0], 
332                session_info->server_info->pac_srv_sig.signature.data, 
333                pac_wrapped_struct.ChecksumLength);
334         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
335                session_info->server_info->pac_kdc_sig.signature.data, 
336                pac_wrapped_struct.SignatureLength);
337         
338         /* Break the signature length */
339         pac_wrapped_struct.SignatureLength++;
340
341         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
342                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
343         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
344         
345         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
346         creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
347         
348         generic.length = pac_wrapped.length;
349         generic.data = pac_wrapped.data;
350         
351         ZERO_STRUCT(auth2);
352         creds_client_authenticator(creds, &auth);
353         r.in.credential = &auth;
354         r.in.return_authenticator = &auth2;
355         r.in.logon_level = NetlogonGenericInformation;
356         r.in.logon.generic = &generic;
357         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
358         r.in.computer_name = cli_credentials_get_workstation(credentials);
359         r.in.validation_level = NetlogonValidationGenericInfo2;
360         
361         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
362         
363         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
364         
365         torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred), 
366                        "Credential chaining failed");
367         return true;
368 }
369
370 struct torture_suite *torture_rpc_remote_pac(TALLOC_CTX *mem_ctx)
371 {
372         struct torture_suite *suite = torture_suite_create(mem_ctx, "PAC");
373         struct torture_rpc_tcase *tcase;
374
375         tcase = torture_suite_add_machine_rpc_iface_tcase(suite, "netlogon", 
376                                                   &ndr_table_netlogon, TEST_MACHINE_NAME);
377         torture_rpc_tcase_add_test_creds(tcase, "verify", test_PACVerify);
378
379         return suite;
380 }