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 "auth/auth.h"
24 #include "auth/auth_sam_reply.h"
25 #include "auth/gensec/gensec.h"
26 #include "system/kerberos.h"
27 #include "auth/kerberos/kerberos.h"
28 #include "auth/credentials/credentials.h"
29 #include "auth/credentials/credentials_krb5.h"
30 #include "lib/cmdline/popt_common.h"
31 #include "torture/rpc/torture_rpc.h"
32 #include "libcli/auth/libcli_auth.h"
33 #include "libcli/security/security.h"
34 #include "librpc/gen_ndr/ndr_netlogon_c.h"
35 #include "librpc/gen_ndr/ndr_krb5pac.h"
36 #include "librpc/gen_ndr/ndr_samr_c.h"
37 #include "param/param.h"
39 #define TEST_MACHINE_NAME_BDC "torturepacbdc"
40 #define TEST_MACHINE_NAME_WKSTA "torturepacwksta"
41 #define TEST_MACHINE_NAME_WKSTA_DES "torturepacwkdes"
42 #define TEST_MACHINE_NAME_S2U4SELF_BDC "tests2u4selfbdc"
43 #define TEST_MACHINE_NAME_S2U4SELF_WKSTA "tests2u4selfwk"
45 /* A helper function which avoids touching the local databases to
46 * generate the session info, as we just want to verify the PAC
47 * details, not the full local token */
48 static NTSTATUS test_generate_session_info_pac(struct auth4_context *auth_ctx,
49 TALLOC_CTX *mem_ctx_out,
50 struct smb_krb5_context *smb_krb5_context,
52 const char *principal_name,
53 const struct tsocket_address *remote_address,
54 uint32_t session_info_flags,
55 struct auth_session_info **session_info)
58 struct auth_user_info_dc *user_info_dc;
59 struct PAC_SIGNATURE_DATA *pac_srv_sig = NULL;
60 struct PAC_SIGNATURE_DATA *pac_kdc_sig = NULL;
63 mem_ctx = talloc_named(mem_ctx_out, 0, "gensec_gssapi_session_info context");
64 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
66 pac_srv_sig = talloc(mem_ctx, struct PAC_SIGNATURE_DATA);
69 return NT_STATUS_NO_MEMORY;
71 pac_kdc_sig = talloc(mem_ctx, struct PAC_SIGNATURE_DATA);
74 return NT_STATUS_NO_MEMORY;
77 nt_status = kerberos_pac_blob_to_user_info_dc(mem_ctx,
79 smb_krb5_context->krb5_context,
83 if (!NT_STATUS_IS_OK(nt_status)) {
88 session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
89 nt_status = auth_generate_session_info(mem_ctx_out,
92 user_info_dc, session_info_flags,
94 if (!NT_STATUS_IS_OK(nt_status)) {
99 if ((*session_info)->torture) {
100 (*session_info)->torture->pac_srv_sig
101 = talloc_steal((*session_info)->torture, pac_srv_sig);
102 (*session_info)->torture->pac_kdc_sig
103 = talloc_steal((*session_info)->torture, pac_kdc_sig);
106 talloc_free(mem_ctx);
110 /* Check to see if we can pass the PAC across to the NETLOGON server for validation */
112 /* Also happens to be a really good one-step verfication of our Kerberos stack */
114 static bool test_PACVerify(struct torture_context *tctx,
115 struct dcerpc_pipe *p,
116 struct cli_credentials *credentials,
117 enum netr_SchannelType secure_channel_type,
118 const char *test_machine_name)
122 struct netr_LogonSamLogon r;
124 union netr_LogonLevel logon;
125 union netr_Validation validation;
126 uint8_t authoritative;
127 struct netr_Authenticator return_authenticator;
129 struct netr_GenericInfo generic;
130 struct netr_Authenticator auth, auth2;
132 struct netlogon_creds_CredentialState *creds;
133 struct gensec_security *gensec_client_context;
134 struct gensec_security *gensec_server_context;
136 DATA_BLOB client_to_server, server_to_client, pac_wrapped, payload;
137 struct PAC_Validate pac_wrapped_struct;
139 enum ndr_err_code ndr_err;
141 struct auth4_context *auth_context;
142 struct auth_session_info *session_info;
144 struct dcerpc_binding_handle *b = p->binding_handle;
145 TALLOC_CTX *tmp_ctx = talloc_new(tctx);
146 torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
148 if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS,
149 credentials, secure_channel_type,
154 auth_context = talloc_zero(tmp_ctx, struct auth4_context);
155 torture_assert(tctx, auth_context != NULL, "talloc_new() failed");
157 auth_context->generate_session_info_pac = test_generate_session_info_pac;
159 status = gensec_client_start(tctx, &gensec_client_context,
160 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
161 torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
163 status = gensec_set_target_hostname(gensec_client_context, test_machine_name);
165 status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
166 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
168 status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
169 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
171 status = gensec_server_start(tctx,
172 lpcfg_gensec_settings(tctx, tctx->lp_ctx),
173 auth_context, &gensec_server_context);
174 torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
176 status = gensec_set_credentials(gensec_server_context, credentials);
177 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
179 status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
180 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
182 server_to_client = data_blob(NULL, 0);
185 /* Do a client-server update dance */
186 status = gensec_update(gensec_client_context, tmp_ctx, tctx->ev, server_to_client, &client_to_server);
187 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
188 torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
191 status = gensec_update(gensec_server_context, tmp_ctx, tctx->ev, client_to_server, &server_to_client);
192 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
193 torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
196 if (NT_STATUS_IS_OK(status)) {
201 /* Extract the PAC using Samba's code */
203 status = gensec_session_info(gensec_server_context, gensec_server_context, &session_info);
204 torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
205 torture_assert(tctx, session_info->torture != NULL, "gensec_session_info failed to fill in torture sub struct");
206 torture_assert(tctx, session_info->torture->pac_srv_sig != NULL, "pac_srv_sig not present");
207 torture_assert(tctx, session_info->torture->pac_kdc_sig != NULL, "pac_kdc_sig not present");
209 pac_wrapped_struct.ChecksumLength = session_info->torture->pac_srv_sig->signature.length;
210 pac_wrapped_struct.SignatureType = session_info->torture->pac_kdc_sig->type;
211 pac_wrapped_struct.SignatureLength = session_info->torture->pac_kdc_sig->signature.length;
212 pac_wrapped_struct.ChecksumAndSignature = payload
213 = data_blob_talloc(tmp_ctx, NULL,
214 pac_wrapped_struct.ChecksumLength
215 + pac_wrapped_struct.SignatureLength);
216 memcpy(&payload.data[0],
217 session_info->torture->pac_srv_sig->signature.data,
218 pac_wrapped_struct.ChecksumLength);
219 memcpy(&payload.data[pac_wrapped_struct.ChecksumLength],
220 session_info->torture->pac_kdc_sig->signature.data,
221 pac_wrapped_struct.SignatureLength);
223 ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, &pac_wrapped_struct,
224 (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
225 torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
227 torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
228 netlogon_creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
230 generic.length = pac_wrapped.length;
231 generic.data = pac_wrapped.data;
233 /* Validate it over the netlogon pipe */
235 generic.identity_info.parameter_control = 0;
236 generic.identity_info.logon_id_high = 0;
237 generic.identity_info.logon_id_low = 0;
238 generic.identity_info.domain_name.string = session_info->info->domain_name;
239 generic.identity_info.account_name.string = session_info->info->account_name;
240 generic.identity_info.workstation.string = test_machine_name;
242 generic.package_name.string = "Kerberos";
244 logon.generic = &generic;
247 netlogon_creds_client_authenticator(creds, &auth);
248 r.in.credential = &auth;
249 r.in.return_authenticator = &auth2;
251 r.in.logon_level = NetlogonGenericInformation;
252 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
253 r.in.computer_name = cli_credentials_get_workstation(credentials);
254 r.in.validation_level = NetlogonValidationGenericInfo2;
255 r.out.validation = &validation;
256 r.out.authoritative = &authoritative;
257 r.out.return_authenticator = &return_authenticator;
259 torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
260 "LogonSamLogon failed");
262 torture_assert_ntstatus_ok(tctx, r.out.result, "LogonSamLogon failed");
264 /* This will break the signature nicely (even in the crypto wrapping), check we get a logon failure */
265 generic.data[generic.length-1]++;
267 logon.generic = &generic;
270 netlogon_creds_client_authenticator(creds, &auth);
271 r.in.credential = &auth;
272 r.in.return_authenticator = &auth2;
273 r.in.logon_level = NetlogonGenericInformation;
275 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
276 r.in.computer_name = cli_credentials_get_workstation(credentials);
277 r.in.validation_level = NetlogonValidationGenericInfo2;
279 torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
280 "LogonSamLogon failed");
282 torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
284 torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred),
285 "Credential chaining failed");
287 /* This will break the parsing nicely (even in the crypto wrapping), check we get INVALID_PARAMETER */
290 logon.generic = &generic;
293 netlogon_creds_client_authenticator(creds, &auth);
294 r.in.credential = &auth;
295 r.in.return_authenticator = &auth2;
296 r.in.logon_level = NetlogonGenericInformation;
298 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
299 r.in.computer_name = cli_credentials_get_workstation(credentials);
300 r.in.validation_level = NetlogonValidationGenericInfo2;
302 torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
303 "LogonSamLogon failed");
305 torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
307 torture_assert(tctx, netlogon_creds_client_check(creds,
308 &r.out.return_authenticator->cred),
309 "Credential chaining failed");
311 pac_wrapped_struct.ChecksumLength = session_info->torture->pac_srv_sig->signature.length;
312 pac_wrapped_struct.SignatureType = session_info->torture->pac_kdc_sig->type;
314 /* Break the SignatureType */
315 pac_wrapped_struct.SignatureType++;
317 pac_wrapped_struct.SignatureLength = session_info->torture->pac_kdc_sig->signature.length;
318 pac_wrapped_struct.ChecksumAndSignature = payload
319 = data_blob_talloc(tmp_ctx, NULL,
320 pac_wrapped_struct.ChecksumLength
321 + pac_wrapped_struct.SignatureLength);
322 memcpy(&payload.data[0],
323 session_info->torture->pac_srv_sig->signature.data,
324 pac_wrapped_struct.ChecksumLength);
325 memcpy(&payload.data[pac_wrapped_struct.ChecksumLength],
326 session_info->torture->pac_kdc_sig->signature.data,
327 pac_wrapped_struct.SignatureLength);
329 ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, &pac_wrapped_struct,
330 (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
331 torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
333 torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
334 netlogon_creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
336 generic.length = pac_wrapped.length;
337 generic.data = pac_wrapped.data;
339 logon.generic = &generic;
342 netlogon_creds_client_authenticator(creds, &auth);
343 r.in.credential = &auth;
344 r.in.return_authenticator = &auth2;
345 r.in.logon_level = NetlogonGenericInformation;
347 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
348 r.in.computer_name = cli_credentials_get_workstation(credentials);
349 r.in.validation_level = NetlogonValidationGenericInfo2;
351 torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
352 "LogonSamLogon failed");
354 torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
356 torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred),
357 "Credential chaining failed");
359 pac_wrapped_struct.ChecksumLength = session_info->torture->pac_srv_sig->signature.length;
360 pac_wrapped_struct.SignatureType = session_info->torture->pac_kdc_sig->type;
361 pac_wrapped_struct.SignatureLength = session_info->torture->pac_kdc_sig->signature.length;
363 pac_wrapped_struct.ChecksumAndSignature = payload
364 = data_blob_talloc(tmp_ctx, NULL,
365 pac_wrapped_struct.ChecksumLength
366 + pac_wrapped_struct.SignatureLength);
367 memcpy(&payload.data[0],
368 session_info->torture->pac_srv_sig->signature.data,
369 pac_wrapped_struct.ChecksumLength);
370 memcpy(&payload.data[pac_wrapped_struct.ChecksumLength],
371 session_info->torture->pac_kdc_sig->signature.data,
372 pac_wrapped_struct.SignatureLength);
374 /* Break the signature length */
375 pac_wrapped_struct.SignatureLength++;
377 ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, &pac_wrapped_struct,
378 (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
379 torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
381 torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
382 netlogon_creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
384 generic.length = pac_wrapped.length;
385 generic.data = pac_wrapped.data;
387 logon.generic = &generic;
390 netlogon_creds_client_authenticator(creds, &auth);
391 r.in.credential = &auth;
392 r.in.return_authenticator = &auth2;
393 r.in.logon_level = NetlogonGenericInformation;
395 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
396 r.in.computer_name = cli_credentials_get_workstation(credentials);
397 r.in.validation_level = NetlogonValidationGenericInfo2;
399 torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
400 "LogonSamLogon failed");
402 torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
404 torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred),
405 "Credential chaining failed");
410 static bool test_PACVerify_bdc(struct torture_context *tctx,
411 struct dcerpc_pipe *p,
412 struct cli_credentials *credentials)
414 return test_PACVerify(tctx, p, credentials, SEC_CHAN_BDC, TEST_MACHINE_NAME_BDC);
417 static bool test_PACVerify_workstation(struct torture_context *tctx,
418 struct dcerpc_pipe *p,
419 struct cli_credentials *credentials)
421 return test_PACVerify(tctx, p, credentials, SEC_CHAN_WKSTA, TEST_MACHINE_NAME_WKSTA);
424 static bool test_PACVerify_workstation_des(struct torture_context *tctx,
425 struct dcerpc_pipe *p, struct cli_credentials *credentials, struct test_join *join_ctx)
427 struct samr_SetUserInfo r;
428 union samr_UserInfo user_info;
429 struct dcerpc_pipe *samr_pipe = torture_join_samr_pipe(join_ctx);
430 struct smb_krb5_context *smb_krb5_context;
433 ret = cli_credentials_get_krb5_context(cmdline_credentials, tctx->lp_ctx, &smb_krb5_context);
434 torture_assert_int_equal(tctx, ret, 0, "cli_credentials_get_krb5_context() failed");
436 if (krb5_config_get_bool_default(smb_krb5_context->krb5_context, NULL, FALSE,
438 "allow_weak_crypto", NULL) == FALSE) {
439 torture_skip(tctx, "Cannot test DES without [libdefaults] allow_weak_crypto = yes");
442 /* Mark this workstation with DES-only */
443 user_info.info16.acct_flags = ACB_USE_DES_KEY_ONLY | ACB_WSTRUST;
444 r.in.user_handle = torture_join_samr_user_policy(join_ctx);
446 r.in.info = &user_info;
448 torture_assert_ntstatus_ok(tctx, dcerpc_samr_SetUserInfo_r(samr_pipe->binding_handle, tctx, &r),
449 "failed to set DES info account flags");
450 torture_assert_ntstatus_ok(tctx, r.out.result,
451 "failed to set DES into account flags");
453 return test_PACVerify(tctx, p, credentials, SEC_CHAN_WKSTA, TEST_MACHINE_NAME_WKSTA_DES);
457 /* Check various ways to get the PAC, in particular check the group membership and other details between the PAC from a normal kinit, S2U4Self and a SamLogon */
458 static bool test_S2U4Self(struct torture_context *tctx,
459 struct dcerpc_pipe *p,
460 struct cli_credentials *credentials,
461 enum netr_SchannelType secure_channel_type,
462 const char *test_machine_name)
465 struct dcerpc_binding_handle *b = p->binding_handle;
467 struct netr_LogonSamLogon r;
469 union netr_LogonLevel logon;
470 union netr_Validation validation;
471 uint8_t authoritative;
473 struct netr_Authenticator auth, auth2;
475 DATA_BLOB client_to_server, server_to_client;
477 struct netlogon_creds_CredentialState *creds;
478 struct gensec_security *gensec_client_context;
479 struct gensec_security *gensec_server_context;
481 struct auth4_context *auth_context;
482 struct auth_session_info *kinit_session_info;
483 struct auth_session_info *s2u4self_session_info;
484 struct auth_user_info_dc *netlogon_user_info_dc;
486 struct netr_NetworkInfo ninfo;
487 DATA_BLOB names_blob, chal, lm_resp, nt_resp;
489 int flags = CLI_CRED_NTLMv2_AUTH;
491 struct dom_sid *builtin_domain;
493 TALLOC_CTX *tmp_ctx = talloc_new(tctx);
495 torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
497 auth_context = talloc_zero(tmp_ctx, struct auth4_context);
498 torture_assert(tctx, auth_context != NULL, "talloc_new() failed");
500 auth_context->generate_session_info_pac = test_generate_session_info_pac;
502 /* First, do a normal Kerberos connection */
504 status = gensec_client_start(tctx, &gensec_client_context,
505 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
506 torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
508 status = gensec_set_target_hostname(gensec_client_context, test_machine_name);
510 status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
511 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
513 status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
514 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
516 status = gensec_server_start(tctx,
517 lpcfg_gensec_settings(tctx, tctx->lp_ctx),
518 auth_context, &gensec_server_context);
519 torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
521 status = gensec_set_credentials(gensec_server_context, credentials);
522 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
524 status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
525 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
527 server_to_client = data_blob(NULL, 0);
530 /* Do a client-server update dance */
531 status = gensec_update(gensec_client_context, tmp_ctx, tctx->ev, server_to_client, &client_to_server);
532 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
533 torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
536 status = gensec_update(gensec_server_context, tmp_ctx, tctx->ev, client_to_server, &server_to_client);
537 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
538 torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
541 if (NT_STATUS_IS_OK(status)) {
546 /* Extract the PAC using Samba's code */
548 status = gensec_session_info(gensec_server_context, gensec_server_context, &kinit_session_info);
549 torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
552 /* Now do the dance with S2U4Self */
554 /* Wipe out any existing ccache */
555 cli_credentials_invalidate_ccache(credentials, CRED_SPECIFIED);
556 cli_credentials_set_impersonate_principal(credentials,
557 cli_credentials_get_principal(cmdline_credentials, tmp_ctx),
558 talloc_asprintf(tmp_ctx, "host/%s", test_machine_name));
560 status = gensec_client_start(tctx, &gensec_client_context,
561 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
562 torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
564 status = gensec_set_target_hostname(gensec_client_context, test_machine_name);
566 /* We now set the same credentials on both client and server contexts */
567 status = gensec_set_credentials(gensec_client_context, credentials);
568 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
570 status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
571 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
573 status = gensec_server_start(tctx,
574 lpcfg_gensec_settings(tctx, tctx->lp_ctx),
575 auth_context, &gensec_server_context);
576 torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
578 status = gensec_set_credentials(gensec_server_context, credentials);
579 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
581 status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
582 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
584 server_to_client = data_blob(NULL, 0);
587 /* Do a client-server update dance */
588 status = gensec_update(gensec_client_context, tmp_ctx, tctx->ev, server_to_client, &client_to_server);
589 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
590 torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
593 status = gensec_update(gensec_server_context, tmp_ctx, tctx->ev, client_to_server, &server_to_client);
594 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
595 torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
598 if (NT_STATUS_IS_OK(status)) {
603 /* Don't pollute the remaining tests with the changed credentials */
604 cli_credentials_invalidate_ccache(credentials, CRED_SPECIFIED);
605 cli_credentials_set_target_service(credentials, NULL);
606 cli_credentials_set_impersonate_principal(credentials, NULL, NULL);
608 /* Extract the PAC using Samba's code */
610 status = gensec_session_info(gensec_server_context, gensec_server_context, &s2u4self_session_info);
611 torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
613 cli_credentials_get_ntlm_username_domain(cmdline_credentials, tctx,
614 &ninfo.identity_info.account_name.string,
615 &ninfo.identity_info.domain_name.string);
617 /* Now try with SamLogon */
618 generate_random_buffer(ninfo.challenge,
619 sizeof(ninfo.challenge));
620 chal = data_blob_const(ninfo.challenge,
621 sizeof(ninfo.challenge));
623 names_blob = NTLMv2_generate_names_blob(tctx, cli_credentials_get_workstation(credentials),
624 cli_credentials_get_domain(credentials));
626 status = cli_credentials_get_ntlm_response(cmdline_credentials, tctx,
632 torture_assert_ntstatus_ok(tctx, status, "cli_credentials_get_ntlm_response failed");
634 ninfo.lm.data = lm_resp.data;
635 ninfo.lm.length = lm_resp.length;
637 ninfo.nt.data = nt_resp.data;
638 ninfo.nt.length = nt_resp.length;
640 ninfo.identity_info.parameter_control = 0;
641 ninfo.identity_info.logon_id_low = 0;
642 ninfo.identity_info.logon_id_high = 0;
643 ninfo.identity_info.workstation.string = cli_credentials_get_workstation(credentials);
645 logon.network = &ninfo;
647 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
648 r.in.computer_name = cli_credentials_get_workstation(credentials);
649 r.in.credential = &auth;
650 r.in.return_authenticator = &auth2;
651 r.in.logon_level = 2;
653 r.out.validation = &validation;
654 r.out.authoritative = &authoritative;
656 if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS,
657 credentials, secure_channel_type,
663 netlogon_creds_client_authenticator(creds, &auth);
665 r.in.validation_level = 3;
667 status = dcerpc_netr_LogonSamLogon_r(b, tctx, &r);
668 torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
670 torture_assert(tctx, netlogon_creds_client_check(creds,
671 &r.out.return_authenticator->cred),
672 "Credential chaining failed");
674 status = make_user_info_dc_netlogon_validation(tmp_ctx,
675 ninfo.identity_info.account_name.string,
676 r.in.validation_level,
678 true, /* This user was authenticated */
679 &netlogon_user_info_dc);
681 torture_assert_ntstatus_ok(tctx, status, "make_user_info_dc_netlogon_validation failed");
683 torture_assert_str_equal(tctx, netlogon_user_info_dc->info->account_name == NULL ? "" : netlogon_user_info_dc->info->account_name,
684 kinit_session_info->info->account_name, "Account name differs for kinit-based PAC");
685 torture_assert_str_equal(tctx,netlogon_user_info_dc->info->account_name == NULL ? "" : netlogon_user_info_dc->info->account_name,
686 s2u4self_session_info->info->account_name, "Account name differs for S2U4Self");
687 torture_assert_str_equal(tctx, netlogon_user_info_dc->info->full_name == NULL ? "" : netlogon_user_info_dc->info->full_name, kinit_session_info->info->full_name, "Full name differs for kinit-based PAC");
688 torture_assert_str_equal(tctx, netlogon_user_info_dc->info->full_name == NULL ? "" : netlogon_user_info_dc->info->full_name, s2u4self_session_info->info->full_name, "Full name differs for S2U4Self");
689 torture_assert_int_equal(tctx, netlogon_user_info_dc->num_sids, kinit_session_info->torture->num_dc_sids, "Different numbers of domain groups for kinit-based PAC");
690 torture_assert_int_equal(tctx, netlogon_user_info_dc->num_sids, s2u4self_session_info->torture->num_dc_sids, "Different numbers of domain groups for S2U4Self");
692 builtin_domain = dom_sid_parse_talloc(tmp_ctx, SID_BUILTIN);
694 for (i = 0; i < kinit_session_info->torture->num_dc_sids; i++) {
695 torture_assert(tctx, dom_sid_equal(&netlogon_user_info_dc->sids[i], &kinit_session_info->torture->dc_sids[i]), "Different domain groups for kinit-based PAC");
696 torture_assert(tctx, dom_sid_equal(&netlogon_user_info_dc->sids[i], &s2u4self_session_info->torture->dc_sids[i]), "Different domain groups for S2U4Self");
697 torture_assert(tctx, !dom_sid_in_domain(builtin_domain, &s2u4self_session_info->torture->dc_sids[i]), "Returned BUILTIN domain in groups for S2U4Self");
698 torture_assert(tctx, !dom_sid_in_domain(builtin_domain, &kinit_session_info->torture->dc_sids[i]), "Returned BUILTIN domain in groups kinit-based PAC");
699 torture_assert(tctx, !dom_sid_in_domain(builtin_domain, &netlogon_user_info_dc->sids[i]), "Returned BUILTIN domian in groups from NETLOGON SamLogon reply");
705 static bool test_S2U4Self_bdc(struct torture_context *tctx,
706 struct dcerpc_pipe *p,
707 struct cli_credentials *credentials)
709 return test_S2U4Self(tctx, p, credentials, SEC_CHAN_BDC, TEST_MACHINE_NAME_S2U4SELF_BDC);
712 static bool test_S2U4Self_workstation(struct torture_context *tctx,
713 struct dcerpc_pipe *p,
714 struct cli_credentials *credentials)
716 return test_S2U4Self(tctx, p, credentials, SEC_CHAN_WKSTA, TEST_MACHINE_NAME_S2U4SELF_WKSTA);
719 struct torture_suite *torture_rpc_remote_pac(TALLOC_CTX *mem_ctx)
721 struct torture_suite *suite = torture_suite_create(mem_ctx, "pac");
722 struct torture_rpc_tcase *tcase;
724 /* It is important to use different names, so that old entries in our credential cache are not used */
725 tcase = torture_suite_add_machine_bdc_rpc_iface_tcase(suite, "netlogon-bdc",
726 &ndr_table_netlogon, TEST_MACHINE_NAME_BDC);
727 torture_rpc_tcase_add_test_creds(tcase, "verify-sig", test_PACVerify_bdc);
729 tcase = torture_suite_add_machine_workstation_rpc_iface_tcase(suite, "netlogon-member",
730 &ndr_table_netlogon, TEST_MACHINE_NAME_WKSTA);
731 torture_rpc_tcase_add_test_creds(tcase, "verify-sig", test_PACVerify_workstation);
733 tcase = torture_suite_add_machine_workstation_rpc_iface_tcase(suite, "netlogon-member-des",
734 &ndr_table_netlogon, TEST_MACHINE_NAME_WKSTA_DES);
735 torture_rpc_tcase_add_test_join(tcase, "verify-sig", test_PACVerify_workstation_des);
737 tcase = torture_suite_add_machine_bdc_rpc_iface_tcase(suite, "netlogon-bdc",
738 &ndr_table_netlogon, TEST_MACHINE_NAME_S2U4SELF_BDC);
739 torture_rpc_tcase_add_test_creds(tcase, "s2u4self", test_S2U4Self_bdc);
741 tcase = torture_suite_add_machine_workstation_rpc_iface_tcase(suite, "netlogon-member",
742 &ndr_table_netlogon, TEST_MACHINE_NAME_S2U4SELF_WKSTA);
744 torture_rpc_tcase_add_test_creds(tcase, "s2u4self", test_S2U4Self_workstation);