aca518799dd37ff20839c21c57c9bf6d0c96d57a
[ddiss/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 "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"
38
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"
44
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,
51                                                DATA_BLOB *pac_blob,
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)
56 {
57         NTSTATUS nt_status;
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;
61         TALLOC_CTX *mem_ctx;
62         
63         mem_ctx = talloc_named(mem_ctx_out, 0, "gensec_gssapi_session_info context");
64         NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
65
66         pac_srv_sig = talloc(mem_ctx, struct PAC_SIGNATURE_DATA);
67         if (!pac_srv_sig) {
68                 talloc_free(mem_ctx);
69                 return NT_STATUS_NO_MEMORY;
70         }
71         pac_kdc_sig = talloc(mem_ctx, struct PAC_SIGNATURE_DATA);
72         if (!pac_kdc_sig) {
73                 talloc_free(mem_ctx);
74                 return NT_STATUS_NO_MEMORY;
75         }
76
77         nt_status = kerberos_pac_blob_to_user_info_dc(mem_ctx,
78                                                       *pac_blob,
79                                                       smb_krb5_context->krb5_context,
80                                                       &user_info_dc,
81                                                       pac_srv_sig,
82                                                       pac_kdc_sig);
83         if (!NT_STATUS_IS_OK(nt_status)) {
84                 talloc_free(mem_ctx);
85                 return nt_status;
86         }
87
88         session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
89         nt_status = auth_generate_session_info(mem_ctx_out,
90                                                NULL,
91                                                NULL,
92                                                user_info_dc, session_info_flags,
93                                                session_info);
94         if (!NT_STATUS_IS_OK(nt_status)) {
95                 talloc_free(mem_ctx);
96                 return nt_status;
97         }
98
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);
104         }
105
106         talloc_free(mem_ctx);
107         return nt_status;
108 }
109
110 /* Check to see if we can pass the PAC across to the NETLOGON server for validation */
111
112 /* Also happens to be a really good one-step verfication of our Kerberos stack */
113
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)
119 {
120         NTSTATUS status;
121
122         struct netr_LogonSamLogon r;
123
124         union netr_LogonLevel logon;
125         union netr_Validation validation;
126         uint8_t authoritative;
127         struct netr_Authenticator return_authenticator;
128
129         struct netr_GenericInfo generic;
130         struct netr_Authenticator auth, auth2;
131         
132         struct netlogon_creds_CredentialState *creds;
133         struct gensec_security *gensec_client_context;
134         struct gensec_security *gensec_server_context;
135
136         DATA_BLOB client_to_server, server_to_client, pac_wrapped, payload;
137         struct PAC_Validate pac_wrapped_struct;
138         
139         enum ndr_err_code ndr_err;
140
141         struct auth4_context *auth_context;
142         struct auth_session_info *session_info;
143
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");
147
148         if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS, 
149                                     credentials, secure_channel_type,
150                                     &creds)) {
151                 return false;
152         }
153
154         auth_context = talloc_zero(tmp_ctx, struct auth4_context);
155         torture_assert(tctx, auth_context != NULL, "talloc_new() failed");
156
157         auth_context->generate_session_info_pac = test_generate_session_info_pac;
158
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");
162
163         status = gensec_set_target_hostname(gensec_client_context, test_machine_name);
164
165         status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
166         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
167
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");
170
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");
175
176         status = gensec_set_credentials(gensec_server_context, credentials);
177         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
178
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");
181
182         server_to_client = data_blob(NULL, 0);
183         
184         do {
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");
189                 }
190
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");
194                 }
195
196                 if (NT_STATUS_IS_OK(status)) {
197                         break;
198                 }
199         } while (1);
200
201         /* Extract the PAC using Samba's code */
202
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");
208
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);
222
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");
226                 
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);
229
230         generic.length = pac_wrapped.length;
231         generic.data = pac_wrapped.data;
232
233         /* Validate it over the netlogon pipe */
234
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;
241
242         generic.package_name.string = "Kerberos";
243
244         logon.generic = &generic;
245
246         ZERO_STRUCT(auth2);
247         netlogon_creds_client_authenticator(creds, &auth);
248         r.in.credential = &auth;
249         r.in.return_authenticator = &auth2;
250         r.in.logon = &logon;
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;
258
259         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
260                 "LogonSamLogon failed");
261
262         torture_assert_ntstatus_ok(tctx, r.out.result, "LogonSamLogon failed");
263         
264         /* This will break the signature nicely (even in the crypto wrapping), check we get a logon failure */
265         generic.data[generic.length-1]++;
266
267         logon.generic = &generic;
268
269         ZERO_STRUCT(auth2);
270         netlogon_creds_client_authenticator(creds, &auth);
271         r.in.credential = &auth;
272         r.in.return_authenticator = &auth2;
273         r.in.logon_level = NetlogonGenericInformation;
274         r.in.logon = &logon;
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;
278
279         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
280                 "LogonSamLogon failed");
281
282         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
283         
284         torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred), 
285                        "Credential chaining failed");
286
287         /* This will break the parsing nicely (even in the crypto wrapping), check we get INVALID_PARAMETER */
288         generic.length--;
289
290         logon.generic = &generic;
291
292         ZERO_STRUCT(auth2);
293         netlogon_creds_client_authenticator(creds, &auth);
294         r.in.credential = &auth;
295         r.in.return_authenticator = &auth2;
296         r.in.logon_level = NetlogonGenericInformation;
297         r.in.logon = &logon;
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;
301
302         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
303                 "LogonSamLogon failed");
304
305         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
306         
307         torture_assert(tctx, netlogon_creds_client_check(creds, 
308                                                          &r.out.return_authenticator->cred), 
309                        "Credential chaining failed");
310
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;
313         
314         /* Break the SignatureType */
315         pac_wrapped_struct.SignatureType++;
316
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);
328         
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");
332         
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);
335         
336         generic.length = pac_wrapped.length;
337         generic.data = pac_wrapped.data;
338
339         logon.generic = &generic;
340
341         ZERO_STRUCT(auth2);
342         netlogon_creds_client_authenticator(creds, &auth);
343         r.in.credential = &auth;
344         r.in.return_authenticator = &auth2;
345         r.in.logon_level = NetlogonGenericInformation;
346         r.in.logon = &logon;
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;
350         
351         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
352                 "LogonSamLogon failed");
353         
354         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
355         
356         torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred), 
357                        "Credential chaining failed");
358
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;
362
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);
373         
374         /* Break the signature length */
375         pac_wrapped_struct.SignatureLength++;
376
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");
380         
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);
383         
384         generic.length = pac_wrapped.length;
385         generic.data = pac_wrapped.data;
386
387         logon.generic = &generic;
388
389         ZERO_STRUCT(auth2);
390         netlogon_creds_client_authenticator(creds, &auth);
391         r.in.credential = &auth;
392         r.in.return_authenticator = &auth2;
393         r.in.logon_level = NetlogonGenericInformation;
394         r.in.logon = &logon;
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;
398         
399         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
400                 "LogonSamLogon failed");
401         
402         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
403         
404         torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred), 
405                        "Credential chaining failed");
406
407         return true;
408 }
409
410 static bool test_PACVerify_bdc(struct torture_context *tctx,
411                                struct dcerpc_pipe *p,
412                                struct cli_credentials *credentials)
413 {
414         return test_PACVerify(tctx, p, credentials, SEC_CHAN_BDC, TEST_MACHINE_NAME_BDC);
415 }
416
417 static bool test_PACVerify_workstation(struct torture_context *tctx,
418                                   struct dcerpc_pipe *p,
419                                   struct cli_credentials *credentials)
420 {
421         return test_PACVerify(tctx, p, credentials, SEC_CHAN_WKSTA, TEST_MACHINE_NAME_WKSTA);
422 }
423
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)
426 {
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;
431         krb5_error_code ret;
432
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");
435
436         if (krb5_config_get_bool_default(smb_krb5_context->krb5_context, NULL, FALSE,
437                                          "libdefaults",
438                                          "allow_weak_crypto", NULL) == FALSE) {
439                 torture_skip(tctx, "Cannot test DES without [libdefaults] allow_weak_crypto = yes");
440         }
441
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);
445         r.in.level = 16;
446         r.in.info = &user_info;
447
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");
452
453         return test_PACVerify(tctx, p, credentials, SEC_CHAN_WKSTA, TEST_MACHINE_NAME_WKSTA_DES);
454 }
455
456
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)
463 {
464         NTSTATUS status;
465         struct dcerpc_binding_handle *b = p->binding_handle;
466
467         struct netr_LogonSamLogon r;
468
469         union netr_LogonLevel logon;
470         union netr_Validation validation;
471         uint8_t authoritative;
472
473         struct netr_Authenticator auth, auth2;
474
475         DATA_BLOB client_to_server, server_to_client;
476
477         struct netlogon_creds_CredentialState *creds;
478         struct gensec_security *gensec_client_context;
479         struct gensec_security *gensec_server_context;
480
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;
485
486         struct netr_NetworkInfo ninfo;
487         DATA_BLOB names_blob, chal, lm_resp, nt_resp;
488         size_t i;
489         int flags = CLI_CRED_NTLMv2_AUTH;
490
491         struct dom_sid *builtin_domain;
492
493         TALLOC_CTX *tmp_ctx = talloc_new(tctx);
494
495         torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
496
497         auth_context = talloc_zero(tmp_ctx, struct auth4_context);
498         torture_assert(tctx, auth_context != NULL, "talloc_new() failed");
499
500         auth_context->generate_session_info_pac = test_generate_session_info_pac;
501
502         /* First, do a normal Kerberos connection */
503
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");
507
508         status = gensec_set_target_hostname(gensec_client_context, test_machine_name);
509
510         status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
511         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
512
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");
515
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");
520
521         status = gensec_set_credentials(gensec_server_context, credentials);
522         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
523
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");
526
527         server_to_client = data_blob(NULL, 0);
528
529         do {
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");
534                 }
535
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");
539                 }
540
541                 if (NT_STATUS_IS_OK(status)) {
542                         break;
543                 }
544         } while (1);
545
546         /* Extract the PAC using Samba's code */
547
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");
550
551
552         /* Now do the dance with S2U4Self */
553
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));
559
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");
563
564         status = gensec_set_target_hostname(gensec_client_context, test_machine_name);
565
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");
569
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");
572
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");
577
578         status = gensec_set_credentials(gensec_server_context, credentials);
579         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
580
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");
583
584         server_to_client = data_blob(NULL, 0);
585
586         do {
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");
591                 }
592
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");
596                 }
597
598                 if (NT_STATUS_IS_OK(status)) {
599                         break;
600                 }
601         } while (1);
602
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);
607
608         /* Extract the PAC using Samba's code */
609
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");
612
613         cli_credentials_get_ntlm_username_domain(cmdline_credentials, tctx,
614                                                  &ninfo.identity_info.account_name.string,
615                                                  &ninfo.identity_info.domain_name.string);
616
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));
622
623         names_blob = NTLMv2_generate_names_blob(tctx, cli_credentials_get_workstation(credentials),
624                                                 cli_credentials_get_domain(credentials));
625
626         status = cli_credentials_get_ntlm_response(cmdline_credentials, tctx,
627                                                    &flags,
628                                                    chal,
629                                                    names_blob,
630                                                    &lm_resp, &nt_resp,
631                                                    NULL, NULL);
632         torture_assert_ntstatus_ok(tctx, status, "cli_credentials_get_ntlm_response failed");
633
634         ninfo.lm.data = lm_resp.data;
635         ninfo.lm.length = lm_resp.length;
636
637         ninfo.nt.data = nt_resp.data;
638         ninfo.nt.length = nt_resp.length;
639
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);
644
645         logon.network = &ninfo;
646
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;
652         r.in.logon = &logon;
653         r.out.validation = &validation;
654         r.out.authoritative = &authoritative;
655
656         if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS,
657                                     credentials, secure_channel_type,
658                                     &creds)) {
659                 return false;
660         }
661
662         ZERO_STRUCT(auth2);
663         netlogon_creds_client_authenticator(creds, &auth);
664
665         r.in.validation_level = 3;
666
667         status = dcerpc_netr_LogonSamLogon_r(b, tctx, &r);
668         torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
669
670         torture_assert(tctx, netlogon_creds_client_check(creds,
671                                                          &r.out.return_authenticator->cred),
672                        "Credential chaining failed");
673
674         status = make_user_info_dc_netlogon_validation(tmp_ctx,
675                                                       ninfo.identity_info.account_name.string,
676                                                       r.in.validation_level,
677                                                       r.out.validation,
678                                                           true, /* This user was authenticated */
679                                                       &netlogon_user_info_dc);
680
681         torture_assert_ntstatus_ok(tctx, status, "make_user_info_dc_netlogon_validation failed");
682
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");
691
692         builtin_domain = dom_sid_parse_talloc(tmp_ctx, SID_BUILTIN);
693
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");
700         }
701
702         return true;
703 }
704
705 static bool test_S2U4Self_bdc(struct torture_context *tctx,
706                                struct dcerpc_pipe *p,
707                                struct cli_credentials *credentials)
708 {
709         return test_S2U4Self(tctx, p, credentials, SEC_CHAN_BDC, TEST_MACHINE_NAME_S2U4SELF_BDC);
710 }
711
712 static bool test_S2U4Self_workstation(struct torture_context *tctx,
713                                   struct dcerpc_pipe *p,
714                                   struct cli_credentials *credentials)
715 {
716         return test_S2U4Self(tctx, p, credentials, SEC_CHAN_WKSTA, TEST_MACHINE_NAME_S2U4SELF_WKSTA);
717 }
718
719 struct torture_suite *torture_rpc_remote_pac(TALLOC_CTX *mem_ctx)
720 {
721         struct torture_suite *suite = torture_suite_create(mem_ctx, "pac");
722         struct torture_rpc_tcase *tcase;
723
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);
728
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);
732
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);
736
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);
740
741         tcase = torture_suite_add_machine_workstation_rpc_iface_tcase(suite, "netlogon-member",
742                                                                       &ndr_table_netlogon, TEST_MACHINE_NAME_S2U4SELF_WKSTA);
743
744         torture_rpc_tcase_add_test_creds(tcase, "s2u4self", test_S2U4Self_workstation);
745         return suite;
746 }