2 Unix SMB/CIFS implementation.
4 test suite for netlogon PAC operations
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
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.
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.
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/>.
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"
37 #define TEST_MACHINE_NAME "torturepactest"
39 /* Check to see if we can pass the PAC across to the NETLOGON server for validation */
41 /* Also happens to be a really good one-step verfication of our Kerberos stack */
43 static bool test_PACVerify(struct torture_context *tctx,
44 struct dcerpc_pipe *p,
45 struct cli_credentials *credentials)
49 struct netr_LogonSamLogon r;
51 struct netr_GenericInfo generic;
52 struct netr_Authenticator auth, auth2;
55 struct creds_CredentialState *creds;
56 struct gensec_security *gensec_client_context;
57 struct gensec_security *gensec_server_context;
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;
63 enum ndr_err_code ndr_err;
65 struct auth_session_info *session_info;
69 TALLOC_CTX *tmp_ctx = talloc_new(tctx);
71 torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
73 if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS,
74 credentials, SEC_CHAN_BDC,
79 status = torture_temp_dir(tctx, "PACVerify", &tmp_dir);
80 torture_assert_ntstatus_ok(tctx, status, "torture_temp_dir failed");
82 msg_server_ctx = messaging_init(tctx,
85 lp_iconv_convenience(tctx->lp_ctx),
88 torture_assert(tctx, msg_server_ctx != NULL, "Failed to init messaging context");
90 status = gensec_client_start(tctx, &gensec_client_context, tctx->ev, tctx->lp_ctx);
91 torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
93 status = gensec_set_target_hostname(gensec_client_context, TEST_MACHINE_NAME);
95 status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
96 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
98 status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
99 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
101 status = gensec_server_start(tctx, tctx->ev, tctx->lp_ctx, msg_server_ctx, &gensec_server_context);
102 torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
104 status = gensec_set_credentials(gensec_server_context, credentials);
105 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
107 status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
108 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
110 server_to_client = data_blob(NULL, 0);
113 /* Do a client-server update dance */
114 status = gensec_update(gensec_client_context, tmp_ctx, server_to_client, &client_to_server);
115 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
116 torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
119 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
120 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
121 torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
124 if (NT_STATUS_IS_OK(status)) {
129 /* Extract the PAC using Samba's code */
131 status = gensec_session_info(gensec_server_context, &session_info);
132 torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
134 pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
135 pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
136 pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
137 pac_wrapped_struct.ChecksumAndSignature = payload
138 = data_blob_talloc(tmp_ctx, NULL,
139 pac_wrapped_struct.ChecksumLength
140 + pac_wrapped_struct.SignatureLength);
141 memcpy(&payload.data[0],
142 session_info->server_info->pac_srv_sig.signature.data,
143 pac_wrapped_struct.ChecksumLength);
144 memcpy(&payload.data[pac_wrapped_struct.ChecksumLength],
145 session_info->server_info->pac_kdc_sig.signature.data,
146 pac_wrapped_struct.SignatureLength);
148 ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
149 (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
150 torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
152 torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
153 creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
155 generic.length = pac_wrapped.length;
156 generic.data = pac_wrapped.data;
158 /* Validate it over the netlogon pipe */
160 generic.identity_info.parameter_control = 0;
161 generic.identity_info.logon_id_high = 0;
162 generic.identity_info.logon_id_low = 0;
163 generic.identity_info.domain_name.string = session_info->server_info->domain_name;
164 generic.identity_info.account_name.string = session_info->server_info->account_name;
165 generic.identity_info.workstation.string = TEST_MACHINE_NAME;
167 generic.package_name.string = "Kerberos";
170 creds_client_authenticator(creds, &auth);
171 r.in.credential = &auth;
172 r.in.return_authenticator = &auth2;
173 r.in.logon_level = NetlogonGenericInformation;
174 r.in.logon.generic = &generic;
175 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
176 r.in.computer_name = cli_credentials_get_workstation(credentials);
177 r.in.validation_level = NetlogonValidationGenericInfo2;
179 status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
181 torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
183 /* This will break the signature nicely (even in the crypto wrapping), check we get a logon failure */
184 generic.data[generic.length-1]++;
187 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.generic = &generic;
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;
196 status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
198 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
200 torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred),
201 "Credential chaining failed");
203 /* This will break the parsing nicely (even in the crypto wrapping), check we get INVALID_PARAMETER */
207 creds_client_authenticator(creds, &auth);
208 r.in.credential = &auth;
209 r.in.return_authenticator = &auth2;
210 r.in.logon_level = NetlogonGenericInformation;
211 r.in.logon.generic = &generic;
212 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
213 r.in.computer_name = cli_credentials_get_workstation(credentials);
214 r.in.validation_level = NetlogonValidationGenericInfo2;
216 status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
218 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
220 torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred),
221 "Credential chaining failed");
223 pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
224 pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
226 /* Break the SignatureType */
227 pac_wrapped_struct.SignatureType++;
229 pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
230 pac_wrapped_struct.ChecksumAndSignature = payload
231 = data_blob_talloc(tmp_ctx, NULL,
232 pac_wrapped_struct.ChecksumLength
233 + pac_wrapped_struct.SignatureLength);
234 memcpy(&payload.data[0],
235 session_info->server_info->pac_srv_sig.signature.data,
236 pac_wrapped_struct.ChecksumLength);
237 memcpy(&payload.data[pac_wrapped_struct.ChecksumLength],
238 session_info->server_info->pac_kdc_sig.signature.data,
239 pac_wrapped_struct.SignatureLength);
241 ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
242 (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
243 torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
245 torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
246 creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
248 generic.length = pac_wrapped.length;
249 generic.data = pac_wrapped.data;
252 creds_client_authenticator(creds, &auth);
253 r.in.credential = &auth;
254 r.in.return_authenticator = &auth2;
255 r.in.logon_level = NetlogonGenericInformation;
256 r.in.logon.generic = &generic;
257 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
258 r.in.computer_name = cli_credentials_get_workstation(credentials);
259 r.in.validation_level = NetlogonValidationGenericInfo2;
261 status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
263 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
265 torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred),
266 "Credential chaining failed");
268 pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
269 pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
270 pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
272 pac_wrapped_struct.ChecksumAndSignature = payload
273 = data_blob_talloc(tmp_ctx, NULL,
274 pac_wrapped_struct.ChecksumLength
275 + pac_wrapped_struct.SignatureLength);
276 memcpy(&payload.data[0],
277 session_info->server_info->pac_srv_sig.signature.data,
278 pac_wrapped_struct.ChecksumLength);
279 memcpy(&payload.data[pac_wrapped_struct.ChecksumLength],
280 session_info->server_info->pac_kdc_sig.signature.data,
281 pac_wrapped_struct.SignatureLength);
283 /* Break the signature length */
284 pac_wrapped_struct.SignatureLength++;
286 ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
287 (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
288 torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
290 torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
291 creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
293 generic.length = pac_wrapped.length;
294 generic.data = pac_wrapped.data;
297 creds_client_authenticator(creds, &auth);
298 r.in.credential = &auth;
299 r.in.return_authenticator = &auth2;
300 r.in.logon_level = NetlogonGenericInformation;
301 r.in.logon.generic = &generic;
302 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
303 r.in.computer_name = cli_credentials_get_workstation(credentials);
304 r.in.validation_level = NetlogonValidationGenericInfo2;
306 status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
308 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
310 torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred),
311 "Credential chaining failed");
315 struct torture_suite *torture_rpc_remote_pac(TALLOC_CTX *mem_ctx)
317 struct torture_suite *suite = torture_suite_create(mem_ctx, "PAC");
318 struct torture_rpc_tcase *tcase;
320 tcase = torture_suite_add_machine_rpc_iface_tcase(suite, "netlogon",
321 &ndr_table_netlogon, TEST_MACHINE_NAME);
322 torture_rpc_tcase_add_test_creds(tcase, "verify", test_PACVerify);