s4-smbtorture: unify test list to run against single created printers in RPC-SPOOLSS...
[abartlet/samba.git/.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 "auth/auth.h"
24 #include "auth/gensec/gensec.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "torture/rpc/rpc.h"
27 #include "libcli/auth/libcli_auth.h"
28 #include "librpc/gen_ndr/ndr_netlogon_c.h"
29 #include "param/param.h"
30
31 #define TEST_MACHINE_NAME "torturepactest"
32
33 /* Check to see if we can pass the PAC across to the NETLOGON server for validation */
34
35 /* Also happens to be a really good one-step verfication of our Kerberos stack */
36
37 static bool test_PACVerify(struct torture_context *tctx, 
38                            struct dcerpc_pipe *p,
39                            struct cli_credentials *credentials)
40 {
41         NTSTATUS status;
42
43         struct netr_LogonSamLogon r;
44
45         union netr_LogonLevel logon;
46         union netr_Validation validation;
47         uint8_t authoritative;
48         struct netr_Authenticator return_authenticator;
49
50         struct netr_GenericInfo generic;
51         struct netr_Authenticator auth, auth2;
52         
53
54         struct netlogon_creds_CredentialState *creds;
55         struct gensec_security *gensec_client_context;
56         struct gensec_security *gensec_server_context;
57
58         DATA_BLOB client_to_server, server_to_client, pac_wrapped, payload;
59         struct PAC_Validate pac_wrapped_struct;
60         
61         enum ndr_err_code ndr_err;
62
63         struct auth_session_info *session_info;
64
65         char *tmp_dir;
66
67         TALLOC_CTX *tmp_ctx = talloc_new(tctx);
68         
69         torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
70
71         if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS, 
72                                     credentials, SEC_CHAN_BDC, 
73                                     &creds)) {
74                 return false;
75         }
76
77         status = torture_temp_dir(tctx, "PACVerify", &tmp_dir);
78         torture_assert_ntstatus_ok(tctx, status, "torture_temp_dir failed");
79
80         status = gensec_client_start(tctx, &gensec_client_context, tctx->ev, 
81                                      lp_gensec_settings(tctx, tctx->lp_ctx));
82         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
83
84         status = gensec_set_target_hostname(gensec_client_context, TEST_MACHINE_NAME);
85
86         status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
87         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
88
89         status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
90         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
91
92         status = gensec_server_start(tctx, tctx->ev, 
93                                      lp_gensec_settings(tctx, tctx->lp_ctx), 
94                                      NULL, &gensec_server_context);
95         torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
96
97         status = gensec_set_credentials(gensec_server_context, credentials);
98         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
99
100         status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
101         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
102
103         server_to_client = data_blob(NULL, 0);
104         
105         do {
106                 /* Do a client-server update dance */
107                 status = gensec_update(gensec_client_context, tmp_ctx, server_to_client, &client_to_server);
108                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
109                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
110                 }
111
112                 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
113                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
114                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
115                 }
116
117                 if (NT_STATUS_IS_OK(status)) {
118                         break;
119                 }
120         } while (1);
121
122         /* Extract the PAC using Samba's code */
123
124         status = gensec_session_info(gensec_server_context, &session_info);
125         torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
126         
127         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
128         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
129         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
130         pac_wrapped_struct.ChecksumAndSignature = payload
131                 = data_blob_talloc(tmp_ctx, NULL, 
132                                    pac_wrapped_struct.ChecksumLength
133                                    + pac_wrapped_struct.SignatureLength);
134         memcpy(&payload.data[0], 
135                session_info->server_info->pac_srv_sig.signature.data, 
136                pac_wrapped_struct.ChecksumLength);
137         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
138                session_info->server_info->pac_kdc_sig.signature.data, 
139                pac_wrapped_struct.SignatureLength);
140
141         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
142                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
143         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
144                 
145         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
146         netlogon_creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
147
148         generic.length = pac_wrapped.length;
149         generic.data = pac_wrapped.data;
150
151         /* Validate it over the netlogon pipe */
152
153         generic.identity_info.parameter_control = 0;
154         generic.identity_info.logon_id_high = 0;
155         generic.identity_info.logon_id_low = 0;
156         generic.identity_info.domain_name.string = session_info->server_info->domain_name;
157         generic.identity_info.account_name.string = session_info->server_info->account_name;
158         generic.identity_info.workstation.string = TEST_MACHINE_NAME;
159
160         generic.package_name.string = "Kerberos";
161
162         logon.generic = &generic;
163
164         ZERO_STRUCT(auth2);
165         netlogon_creds_client_authenticator(creds, &auth);
166         r.in.credential = &auth;
167         r.in.return_authenticator = &auth2;
168         r.in.logon = &logon;
169         r.in.logon_level = NetlogonGenericInformation;
170         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
171         r.in.computer_name = cli_credentials_get_workstation(credentials);
172         r.in.validation_level = NetlogonValidationGenericInfo2;
173         r.out.validation = &validation;
174         r.out.authoritative = &authoritative;
175         r.out.return_authenticator = &return_authenticator;
176
177         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
178
179         torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
180         
181         /* This will break the signature nicely (even in the crypto wrapping), check we get a logon failure */
182         generic.data[generic.length-1]++;
183
184         logon.generic = &generic;
185
186         ZERO_STRUCT(auth2);
187         netlogon_creds_client_authenticator(creds, &auth);
188         r.in.credential = &auth;
189         r.in.return_authenticator = &auth2;
190         r.in.logon_level = NetlogonGenericInformation;
191         r.in.logon = &logon;
192         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
193         r.in.computer_name = cli_credentials_get_workstation(credentials);
194         r.in.validation_level = NetlogonValidationGenericInfo2;
195
196         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
197
198         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
199         
200         torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred), 
201                        "Credential chaining failed");
202
203         /* This will break the parsing nicely (even in the crypto wrapping), check we get INVALID_PARAMETER */
204         generic.length--;
205
206         logon.generic = &generic;
207
208         ZERO_STRUCT(auth2);
209         netlogon_creds_client_authenticator(creds, &auth);
210         r.in.credential = &auth;
211         r.in.return_authenticator = &auth2;
212         r.in.logon_level = NetlogonGenericInformation;
213         r.in.logon = &logon;
214         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
215         r.in.computer_name = cli_credentials_get_workstation(credentials);
216         r.in.validation_level = NetlogonValidationGenericInfo2;
217
218         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
219
220         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
221         
222         torture_assert(tctx, netlogon_creds_client_check(creds, 
223                                                          &r.out.return_authenticator->cred), 
224                        "Credential chaining failed");
225
226         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
227         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
228         
229         /* Break the SignatureType */
230         pac_wrapped_struct.SignatureType++;
231
232         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
233         pac_wrapped_struct.ChecksumAndSignature = payload
234                 = data_blob_talloc(tmp_ctx, NULL, 
235                                    pac_wrapped_struct.ChecksumLength
236                                    + pac_wrapped_struct.SignatureLength);
237         memcpy(&payload.data[0], 
238                session_info->server_info->pac_srv_sig.signature.data, 
239                pac_wrapped_struct.ChecksumLength);
240         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
241                session_info->server_info->pac_kdc_sig.signature.data, 
242                pac_wrapped_struct.SignatureLength);
243         
244         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
245                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
246         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
247         
248         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
249         netlogon_creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
250         
251         generic.length = pac_wrapped.length;
252         generic.data = pac_wrapped.data;
253
254         logon.generic = &generic;
255
256         ZERO_STRUCT(auth2);
257         netlogon_creds_client_authenticator(creds, &auth);
258         r.in.credential = &auth;
259         r.in.return_authenticator = &auth2;
260         r.in.logon_level = NetlogonGenericInformation;
261         r.in.logon = &logon;
262         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
263         r.in.computer_name = cli_credentials_get_workstation(credentials);
264         r.in.validation_level = NetlogonValidationGenericInfo2;
265         
266         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
267         
268         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
269         
270         torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred), 
271                        "Credential chaining failed");
272
273         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
274         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
275         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
276
277         pac_wrapped_struct.ChecksumAndSignature = payload
278                 = data_blob_talloc(tmp_ctx, NULL, 
279                                    pac_wrapped_struct.ChecksumLength
280                                    + pac_wrapped_struct.SignatureLength);
281         memcpy(&payload.data[0], 
282                session_info->server_info->pac_srv_sig.signature.data, 
283                pac_wrapped_struct.ChecksumLength);
284         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
285                session_info->server_info->pac_kdc_sig.signature.data, 
286                pac_wrapped_struct.SignatureLength);
287         
288         /* Break the signature length */
289         pac_wrapped_struct.SignatureLength++;
290
291         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
292                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
293         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
294         
295         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
296         netlogon_creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
297         
298         generic.length = pac_wrapped.length;
299         generic.data = pac_wrapped.data;
300
301         logon.generic = &generic;
302
303         ZERO_STRUCT(auth2);
304         netlogon_creds_client_authenticator(creds, &auth);
305         r.in.credential = &auth;
306         r.in.return_authenticator = &auth2;
307         r.in.logon_level = NetlogonGenericInformation;
308         r.in.logon = &logon;
309         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
310         r.in.computer_name = cli_credentials_get_workstation(credentials);
311         r.in.validation_level = NetlogonValidationGenericInfo2;
312         
313         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
314         
315         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
316         
317         torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred), 
318                        "Credential chaining failed");
319         return true;
320 }
321
322 struct torture_suite *torture_rpc_remote_pac(TALLOC_CTX *mem_ctx)
323 {
324         struct torture_suite *suite = torture_suite_create(mem_ctx, "PAC");
325         struct torture_rpc_tcase *tcase;
326
327         tcase = torture_suite_add_machine_bdc_rpc_iface_tcase(suite, "netlogon",
328                                                   &ndr_table_netlogon, TEST_MACHINE_NAME);
329         torture_rpc_tcase_add_test_creds(tcase, "verify", test_PACVerify);
330
331         return suite;
332 }