b32881cc69bf8d3c1c4800553c0db333e86fc159
[kai/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.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
141         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
142         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
143         pac_wrapped_struct.ChecksumAndSignature = payload
144                 = data_blob_talloc(tmp_ctx, NULL, 
145                                    pac_wrapped_struct.ChecksumLength
146                                    + pac_wrapped_struct.SignatureLength);
147         memcpy(&payload.data[0], 
148                session_info->server_info->pac_srv_sig.signature.data, 
149                pac_wrapped_struct.ChecksumLength);
150         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
151                session_info->server_info->pac_kdc_sig.signature.data, 
152                pac_wrapped_struct.SignatureLength);
153
154         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
155                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
156         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
157                 
158         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
159         creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
160
161         generic.length = pac_wrapped.length;
162         generic.data = pac_wrapped.data;
163
164         /* Validate it over the netlogon pipe */
165
166         generic.identity_info.parameter_control = 0;
167         generic.identity_info.logon_id_high = 0;
168         generic.identity_info.logon_id_low = 0;
169         generic.identity_info.domain_name.string = session_info->server_info->domain_name;
170         generic.identity_info.account_name.string = session_info->server_info->account_name;
171         generic.identity_info.workstation.string = TEST_MACHINE_NAME;
172
173         generic.package_name.string = "Kerberos";
174
175         ZERO_STRUCT(auth2);
176         creds_client_authenticator(creds, &auth);
177         r.in.credential = &auth;
178         r.in.return_authenticator = &auth2;
179         r.in.logon_level = NetlogonGenericInformation;
180         r.in.logon.generic = &generic;
181         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
182         r.in.computer_name = cli_credentials_get_workstation(credentials);
183         r.in.validation_level = NetlogonValidationGenericInfo2;
184
185         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
186
187         torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
188         
189         /* This will break the signature nicely (even in the crypto wrapping), check we get a logon failure */
190         generic.data[generic.length-1]++;
191
192         ZERO_STRUCT(auth2);
193         creds_client_authenticator(creds, &auth);
194         r.in.credential = &auth;
195         r.in.return_authenticator = &auth2;
196         r.in.logon_level = NetlogonGenericInformation;
197         r.in.logon.generic = &generic;
198         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
199         r.in.computer_name = cli_credentials_get_workstation(credentials);
200         r.in.validation_level = NetlogonValidationGenericInfo2;
201
202         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
203
204         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
205         
206         torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred), 
207                        "Credential chaining failed");
208
209         /* This will break the parsing nicely (even in the crypto wrapping), check we get INVALID_PARAMETER */
210         generic.length--;
211
212         ZERO_STRUCT(auth2);
213         creds_client_authenticator(creds, &auth);
214         r.in.credential = &auth;
215         r.in.return_authenticator = &auth2;
216         r.in.logon_level = NetlogonGenericInformation;
217         r.in.logon.generic = &generic;
218         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
219         r.in.computer_name = cli_credentials_get_workstation(credentials);
220         r.in.validation_level = NetlogonValidationGenericInfo2;
221
222         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
223
224         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
225         
226         torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred), 
227                        "Credential chaining failed");
228
229         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
230         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
231         
232         /* Break the SignatureType */
233         pac_wrapped_struct.SignatureType++;
234
235         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
236         pac_wrapped_struct.ChecksumAndSignature = payload
237                 = data_blob_talloc(tmp_ctx, NULL, 
238                                    pac_wrapped_struct.ChecksumLength
239                                    + pac_wrapped_struct.SignatureLength);
240         memcpy(&payload.data[0], 
241                session_info->server_info->pac_srv_sig.signature.data, 
242                pac_wrapped_struct.ChecksumLength);
243         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
244                session_info->server_info->pac_kdc_sig.signature.data, 
245                pac_wrapped_struct.SignatureLength);
246         
247         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
248                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
249         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
250         
251         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
252         creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
253         
254         generic.length = pac_wrapped.length;
255         generic.data = pac_wrapped.data;
256         
257         ZERO_STRUCT(auth2);
258         creds_client_authenticator(creds, &auth);
259         r.in.credential = &auth;
260         r.in.return_authenticator = &auth2;
261         r.in.logon_level = NetlogonGenericInformation;
262         r.in.logon.generic = &generic;
263         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
264         r.in.computer_name = cli_credentials_get_workstation(credentials);
265         r.in.validation_level = NetlogonValidationGenericInfo2;
266         
267         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
268         
269         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
270         
271         torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred), 
272                        "Credential chaining failed");
273
274         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
275         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
276         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
277
278         pac_wrapped_struct.ChecksumAndSignature = payload
279                 = data_blob_talloc(tmp_ctx, NULL, 
280                                    pac_wrapped_struct.ChecksumLength
281                                    + pac_wrapped_struct.SignatureLength);
282         memcpy(&payload.data[0], 
283                session_info->server_info->pac_srv_sig.signature.data, 
284                pac_wrapped_struct.ChecksumLength);
285         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
286                session_info->server_info->pac_kdc_sig.signature.data, 
287                pac_wrapped_struct.SignatureLength);
288         
289         /* Break the signature length */
290         pac_wrapped_struct.SignatureLength++;
291
292         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
293                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
294         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
295         
296         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
297         creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
298         
299         generic.length = pac_wrapped.length;
300         generic.data = pac_wrapped.data;
301         
302         ZERO_STRUCT(auth2);
303         creds_client_authenticator(creds, &auth);
304         r.in.credential = &auth;
305         r.in.return_authenticator = &auth2;
306         r.in.logon_level = NetlogonGenericInformation;
307         r.in.logon.generic = &generic;
308         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
309         r.in.computer_name = cli_credentials_get_workstation(credentials);
310         r.in.validation_level = NetlogonValidationGenericInfo2;
311         
312         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
313         
314         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
315         
316         torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred), 
317                        "Credential chaining failed");
318         return true;
319 }
320
321 struct torture_suite *torture_rpc_remote_pac(TALLOC_CTX *mem_ctx)
322 {
323         struct torture_suite *suite = torture_suite_create(mem_ctx, "PAC");
324         struct torture_rpc_tcase *tcase;
325
326         tcase = torture_suite_add_machine_rpc_iface_tcase(suite, "netlogon", 
327                                                   &ndr_table_netlogon, TEST_MACHINE_NAME);
328         torture_rpc_tcase_add_test_creds(tcase, "verify", test_PACVerify);
329
330         return suite;
331 }