8f4d677c44aaf9dca861f9743d7b2d1df4cf1671
[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 "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_samr_c.h"
36 #include "param/param.h"
37
38 #define TEST_MACHINE_NAME_BDC "torturepacbdc"
39 #define TEST_MACHINE_NAME_WKSTA "torturepacwksta"
40 #define TEST_MACHINE_NAME_WKSTA_DES "torturepacwkdes"
41 #define TEST_MACHINE_NAME_S2U4SELF_BDC "tests2u4selfbdc"
42 #define TEST_MACHINE_NAME_S2U4SELF_WKSTA "tests2u4selfwk"
43
44 /* Check to see if we can pass the PAC across to the NETLOGON server for validation */
45
46 /* Also happens to be a really good one-step verfication of our Kerberos stack */
47
48 static bool test_PACVerify(struct torture_context *tctx, 
49                            struct dcerpc_pipe *p,
50                            struct cli_credentials *credentials,
51                            enum netr_SchannelType secure_channel_type,
52                            const char *test_machine_name)
53 {
54         NTSTATUS status;
55
56         struct netr_LogonSamLogon r;
57
58         union netr_LogonLevel logon;
59         union netr_Validation validation;
60         uint8_t authoritative;
61         struct netr_Authenticator return_authenticator;
62
63         struct netr_GenericInfo generic;
64         struct netr_Authenticator auth, auth2;
65         
66         struct netlogon_creds_CredentialState *creds;
67         struct gensec_security *gensec_client_context;
68         struct gensec_security *gensec_server_context;
69
70         DATA_BLOB client_to_server, server_to_client, pac_wrapped, payload;
71         struct PAC_Validate pac_wrapped_struct;
72         
73         enum ndr_err_code ndr_err;
74
75         struct auth_session_info *session_info;
76
77         struct dcerpc_binding_handle *b = p->binding_handle;
78         TALLOC_CTX *tmp_ctx = talloc_new(tctx);
79         torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
80
81         if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS, 
82                                     credentials, secure_channel_type,
83                                     &creds)) {
84                 return false;
85         }
86
87         status = gensec_client_start(tctx, &gensec_client_context, tctx->ev, 
88                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx));
89         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
90
91         status = gensec_set_target_hostname(gensec_client_context, test_machine_name);
92
93         status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
94         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
95
96         status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
97         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
98
99         status = gensec_server_start(tctx, tctx->ev, 
100                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx),
101                                      NULL, &gensec_server_context);
102         torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
103
104         status = gensec_set_credentials(gensec_server_context, credentials);
105         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
106
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");
109
110         server_to_client = data_blob(NULL, 0);
111         
112         do {
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");
117                 }
118
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");
122                 }
123
124                 if (NT_STATUS_IS_OK(status)) {
125                         break;
126                 }
127         } while (1);
128
129         /* Extract the PAC using Samba's code */
130
131         status = gensec_session_info(gensec_server_context, &session_info);
132         torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
133         
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);
147
148         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_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");
151                 
152         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
153         netlogon_creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
154
155         generic.length = pac_wrapped.length;
156         generic.data = pac_wrapped.data;
157
158         /* Validate it over the netlogon pipe */
159
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;
166
167         generic.package_name.string = "Kerberos";
168
169         logon.generic = &generic;
170
171         ZERO_STRUCT(auth2);
172         netlogon_creds_client_authenticator(creds, &auth);
173         r.in.credential = &auth;
174         r.in.return_authenticator = &auth2;
175         r.in.logon = &logon;
176         r.in.logon_level = NetlogonGenericInformation;
177         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
178         r.in.computer_name = cli_credentials_get_workstation(credentials);
179         r.in.validation_level = NetlogonValidationGenericInfo2;
180         r.out.validation = &validation;
181         r.out.authoritative = &authoritative;
182         r.out.return_authenticator = &return_authenticator;
183
184         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
185                 "LogonSamLogon failed");
186
187         torture_assert_ntstatus_ok(tctx, r.out.result, "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         logon.generic = &generic;
193
194         ZERO_STRUCT(auth2);
195         netlogon_creds_client_authenticator(creds, &auth);
196         r.in.credential = &auth;
197         r.in.return_authenticator = &auth2;
198         r.in.logon_level = NetlogonGenericInformation;
199         r.in.logon = &logon;
200         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
201         r.in.computer_name = cli_credentials_get_workstation(credentials);
202         r.in.validation_level = NetlogonValidationGenericInfo2;
203
204         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
205                 "LogonSamLogon failed");
206
207         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
208         
209         torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred), 
210                        "Credential chaining failed");
211
212         /* This will break the parsing nicely (even in the crypto wrapping), check we get INVALID_PARAMETER */
213         generic.length--;
214
215         logon.generic = &generic;
216
217         ZERO_STRUCT(auth2);
218         netlogon_creds_client_authenticator(creds, &auth);
219         r.in.credential = &auth;
220         r.in.return_authenticator = &auth2;
221         r.in.logon_level = NetlogonGenericInformation;
222         r.in.logon = &logon;
223         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
224         r.in.computer_name = cli_credentials_get_workstation(credentials);
225         r.in.validation_level = NetlogonValidationGenericInfo2;
226
227         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
228                 "LogonSamLogon failed");
229
230         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
231         
232         torture_assert(tctx, netlogon_creds_client_check(creds, 
233                                                          &r.out.return_authenticator->cred), 
234                        "Credential chaining failed");
235
236         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
237         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
238         
239         /* Break the SignatureType */
240         pac_wrapped_struct.SignatureType++;
241
242         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
243         pac_wrapped_struct.ChecksumAndSignature = payload
244                 = data_blob_talloc(tmp_ctx, NULL, 
245                                    pac_wrapped_struct.ChecksumLength
246                                    + pac_wrapped_struct.SignatureLength);
247         memcpy(&payload.data[0], 
248                session_info->server_info->pac_srv_sig.signature.data, 
249                pac_wrapped_struct.ChecksumLength);
250         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
251                session_info->server_info->pac_kdc_sig.signature.data, 
252                pac_wrapped_struct.SignatureLength);
253         
254         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, &pac_wrapped_struct,
255                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
256         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
257         
258         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
259         netlogon_creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
260         
261         generic.length = pac_wrapped.length;
262         generic.data = pac_wrapped.data;
263
264         logon.generic = &generic;
265
266         ZERO_STRUCT(auth2);
267         netlogon_creds_client_authenticator(creds, &auth);
268         r.in.credential = &auth;
269         r.in.return_authenticator = &auth2;
270         r.in.logon_level = NetlogonGenericInformation;
271         r.in.logon = &logon;
272         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
273         r.in.computer_name = cli_credentials_get_workstation(credentials);
274         r.in.validation_level = NetlogonValidationGenericInfo2;
275         
276         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
277                 "LogonSamLogon failed");
278         
279         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
280         
281         torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred), 
282                        "Credential chaining failed");
283
284         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
285         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
286         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
287
288         pac_wrapped_struct.ChecksumAndSignature = payload
289                 = data_blob_talloc(tmp_ctx, NULL, 
290                                    pac_wrapped_struct.ChecksumLength
291                                    + pac_wrapped_struct.SignatureLength);
292         memcpy(&payload.data[0], 
293                session_info->server_info->pac_srv_sig.signature.data, 
294                pac_wrapped_struct.ChecksumLength);
295         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
296                session_info->server_info->pac_kdc_sig.signature.data, 
297                pac_wrapped_struct.SignatureLength);
298         
299         /* Break the signature length */
300         pac_wrapped_struct.SignatureLength++;
301
302         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, &pac_wrapped_struct,
303                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
304         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
305         
306         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
307         netlogon_creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
308         
309         generic.length = pac_wrapped.length;
310         generic.data = pac_wrapped.data;
311
312         logon.generic = &generic;
313
314         ZERO_STRUCT(auth2);
315         netlogon_creds_client_authenticator(creds, &auth);
316         r.in.credential = &auth;
317         r.in.return_authenticator = &auth2;
318         r.in.logon_level = NetlogonGenericInformation;
319         r.in.logon = &logon;
320         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
321         r.in.computer_name = cli_credentials_get_workstation(credentials);
322         r.in.validation_level = NetlogonValidationGenericInfo2;
323         
324         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
325                 "LogonSamLogon failed");
326         
327         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
328         
329         torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred), 
330                        "Credential chaining failed");
331
332         return true;
333 }
334
335 static bool test_PACVerify_bdc(struct torture_context *tctx,
336                                struct dcerpc_pipe *p,
337                                struct cli_credentials *credentials)
338 {
339         return test_PACVerify(tctx, p, credentials, SEC_CHAN_BDC, TEST_MACHINE_NAME_BDC);
340 }
341
342 static bool test_PACVerify_workstation(struct torture_context *tctx,
343                                   struct dcerpc_pipe *p,
344                                   struct cli_credentials *credentials)
345 {
346         return test_PACVerify(tctx, p, credentials, SEC_CHAN_WKSTA, TEST_MACHINE_NAME_WKSTA);
347 }
348
349 static bool test_PACVerify_workstation_des(struct torture_context *tctx,
350                                            struct dcerpc_pipe *p, struct cli_credentials *credentials, struct test_join *join_ctx)
351 {
352         struct samr_SetUserInfo r;
353         union samr_UserInfo user_info;
354         struct dcerpc_pipe *samr_pipe = torture_join_samr_pipe(join_ctx);
355         struct smb_krb5_context *smb_krb5_context;
356         krb5_error_code ret;
357
358         ret = cli_credentials_get_krb5_context(cmdline_credentials, tctx->lp_ctx, &smb_krb5_context);
359         torture_assert_int_equal(tctx, ret, 0, "cli_credentials_get_krb5_context() failed");
360
361         if (krb5_config_get_bool_default(smb_krb5_context->krb5_context, NULL, FALSE,
362                                          "libdefaults",
363                                          "allow_weak_crypto", NULL) == FALSE) {
364                 torture_skip(tctx, "Cannot test DES without [libdefaults] allow_weak_crypto = yes");
365         }
366
367         /* Mark this workstation with DES-only */
368         user_info.info16.acct_flags = ACB_USE_DES_KEY_ONLY | ACB_WSTRUST;
369         r.in.user_handle = torture_join_samr_user_policy(join_ctx);
370         r.in.level = 16;
371         r.in.info = &user_info;
372
373         torture_assert_ntstatus_ok(tctx, dcerpc_samr_SetUserInfo_r(samr_pipe->binding_handle, tctx, &r),
374                 "failed to set DES info account flags");
375         torture_assert_ntstatus_ok(tctx, r.out.result,
376                 "failed to set DES into account flags");
377
378         return test_PACVerify(tctx, p, credentials, SEC_CHAN_WKSTA, TEST_MACHINE_NAME_WKSTA_DES);
379 }
380
381
382 /* 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 */
383 static bool test_S2U4Self(struct torture_context *tctx,
384                           struct dcerpc_pipe *p,
385                           struct cli_credentials *credentials,
386                           enum netr_SchannelType secure_channel_type,
387                           const char *test_machine_name)
388 {
389         NTSTATUS status;
390         struct dcerpc_binding_handle *b = p->binding_handle;
391
392         struct netr_LogonSamLogon r;
393
394         union netr_LogonLevel logon;
395         union netr_Validation validation;
396         uint8_t authoritative;
397
398         struct netr_Authenticator auth, auth2;
399
400         DATA_BLOB client_to_server, server_to_client;
401
402         struct netlogon_creds_CredentialState *creds;
403         struct gensec_security *gensec_client_context;
404         struct gensec_security *gensec_server_context;
405
406         struct auth_session_info *kinit_session_info;
407         struct auth_session_info *s2u4self_session_info;
408         struct auth_serversupplied_info *netlogon_server_info;
409
410         struct netr_NetworkInfo ninfo;
411         DATA_BLOB names_blob, chal, lm_resp, nt_resp;
412         size_t i;
413         int flags = CLI_CRED_NTLMv2_AUTH;
414
415         struct dom_sid *builtin_domain;
416
417         TALLOC_CTX *tmp_ctx = talloc_new(tctx);
418
419         torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
420
421         /* First, do a normal Kerberos connection */
422
423         status = gensec_client_start(tctx, &gensec_client_context, tctx->ev,
424                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx));
425         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
426
427         status = gensec_set_target_hostname(gensec_client_context, test_machine_name);
428
429         status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
430         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
431
432         status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
433         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
434
435         status = gensec_server_start(tctx, tctx->ev,
436                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx),
437                                      NULL, &gensec_server_context);
438         torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
439
440         status = gensec_set_credentials(gensec_server_context, credentials);
441         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
442
443         status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
444         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
445
446         server_to_client = data_blob(NULL, 0);
447
448         do {
449                 /* Do a client-server update dance */
450                 status = gensec_update(gensec_client_context, tmp_ctx, server_to_client, &client_to_server);
451                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
452                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
453                 }
454
455                 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
456                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
457                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
458                 }
459
460                 if (NT_STATUS_IS_OK(status)) {
461                         break;
462                 }
463         } while (1);
464
465         /* Extract the PAC using Samba's code */
466
467         status = gensec_session_info(gensec_server_context, &kinit_session_info);
468         torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
469
470
471         /* Now do the dance with S2U4Self */
472
473         /* Wipe out any existing ccache */
474         cli_credentials_invalidate_ccache(credentials, CRED_SPECIFIED);
475         cli_credentials_set_target_service(credentials, talloc_asprintf(tmp_ctx, "host/%s", test_machine_name));
476         cli_credentials_set_impersonate_principal(credentials, cli_credentials_get_principal(cmdline_credentials, tmp_ctx));
477
478         status = gensec_client_start(tctx, &gensec_client_context, tctx->ev,
479                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx));
480         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
481
482         status = gensec_set_target_hostname(gensec_client_context, test_machine_name);
483
484         /* We now set the same credentials on both client and server contexts */
485         status = gensec_set_credentials(gensec_client_context, credentials);
486         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
487
488         status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
489         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
490
491         status = gensec_server_start(tctx, tctx->ev,
492                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx),
493                                      NULL, &gensec_server_context);
494         torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
495
496         status = gensec_set_credentials(gensec_server_context, credentials);
497         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
498
499         status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
500         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
501
502         server_to_client = data_blob(NULL, 0);
503
504         do {
505                 /* Do a client-server update dance */
506                 status = gensec_update(gensec_client_context, tmp_ctx, server_to_client, &client_to_server);
507                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
508                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
509                 }
510
511                 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
512                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
513                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
514                 }
515
516                 if (NT_STATUS_IS_OK(status)) {
517                         break;
518                 }
519         } while (1);
520
521         /* Don't pollute the remaining tests with the changed credentials */
522         cli_credentials_invalidate_ccache(credentials, CRED_SPECIFIED);
523         cli_credentials_set_target_service(credentials, NULL);
524         cli_credentials_set_impersonate_principal(credentials, NULL);
525
526         /* Extract the PAC using Samba's code */
527
528         status = gensec_session_info(gensec_server_context, &s2u4self_session_info);
529         torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
530
531         cli_credentials_get_ntlm_username_domain(cmdline_credentials, tctx,
532                                                  &ninfo.identity_info.account_name.string,
533                                                  &ninfo.identity_info.domain_name.string);
534
535         /* Now try with SamLogon */
536         generate_random_buffer(ninfo.challenge,
537                                sizeof(ninfo.challenge));
538         chal = data_blob_const(ninfo.challenge,
539                                sizeof(ninfo.challenge));
540
541         names_blob = NTLMv2_generate_names_blob(tctx, cli_credentials_get_workstation(credentials),
542                                                 cli_credentials_get_domain(credentials));
543
544         status = cli_credentials_get_ntlm_response(cmdline_credentials, tctx,
545                                                    &flags,
546                                                    chal,
547                                                    names_blob,
548                                                    &lm_resp, &nt_resp,
549                                                    NULL, NULL);
550         torture_assert_ntstatus_ok(tctx, status, "cli_credentials_get_ntlm_response failed");
551
552         ninfo.lm.data = lm_resp.data;
553         ninfo.lm.length = lm_resp.length;
554
555         ninfo.nt.data = nt_resp.data;
556         ninfo.nt.length = nt_resp.length;
557
558         ninfo.identity_info.parameter_control = 0;
559         ninfo.identity_info.logon_id_low = 0;
560         ninfo.identity_info.logon_id_high = 0;
561         ninfo.identity_info.workstation.string = cli_credentials_get_workstation(credentials);
562
563         logon.network = &ninfo;
564
565         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
566         r.in.computer_name = cli_credentials_get_workstation(credentials);
567         r.in.credential = &auth;
568         r.in.return_authenticator = &auth2;
569         r.in.logon_level = 2;
570         r.in.logon = &logon;
571         r.out.validation = &validation;
572         r.out.authoritative = &authoritative;
573
574         if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS,
575                                     credentials, secure_channel_type,
576                                     &creds)) {
577                 return false;
578         }
579
580         ZERO_STRUCT(auth2);
581         netlogon_creds_client_authenticator(creds, &auth);
582
583         r.in.validation_level = 3;
584
585         status = dcerpc_netr_LogonSamLogon_r(b, tctx, &r);
586         torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
587
588         torture_assert(tctx, netlogon_creds_client_check(creds,
589                                                          &r.out.return_authenticator->cred),
590                        "Credential chaining failed");
591
592         status = make_server_info_netlogon_validation(tmp_ctx,
593                                                       ninfo.identity_info.account_name.string,
594                                                       r.in.validation_level,
595                                                       r.out.validation,
596                                                       &netlogon_server_info);
597
598         torture_assert_ntstatus_ok(tctx, status, "make_server_info_netlogon_validation failed");
599
600         torture_assert_str_equal(tctx, netlogon_server_info->account_name == NULL ? "" : netlogon_server_info->account_name,
601                                  kinit_session_info->server_info->account_name, "Account name differs for kinit-based PAC");
602         torture_assert_str_equal(tctx,netlogon_server_info->account_name == NULL ? "" : netlogon_server_info->account_name,
603                                  s2u4self_session_info->server_info->account_name, "Account name differs for S2U4Self");
604         torture_assert_str_equal(tctx, netlogon_server_info->full_name == NULL ? "" : netlogon_server_info->full_name, kinit_session_info->server_info->full_name, "Full name differs for kinit-based PAC");
605         torture_assert_str_equal(tctx, netlogon_server_info->full_name == NULL ? "" : netlogon_server_info->full_name, s2u4self_session_info->server_info->full_name, "Full name differs for S2U4Self");
606         torture_assert(tctx, dom_sid_equal(netlogon_server_info->account_sid, kinit_session_info->server_info->account_sid), "Account SID differs for kinit-based PAC");
607         torture_assert(tctx, dom_sid_equal(netlogon_server_info->primary_group_sid, kinit_session_info->server_info->primary_group_sid), "Primary Group SID differs for kinit-based PAC");
608         torture_assert(tctx, dom_sid_equal(netlogon_server_info->account_sid, s2u4self_session_info->server_info->account_sid), "Account SID differs for S2U4Self");
609         torture_assert(tctx, dom_sid_equal(netlogon_server_info->primary_group_sid, s2u4self_session_info->server_info->primary_group_sid), "Primary Group SID differs for S2U4Self");
610         torture_assert_int_equal(tctx, netlogon_server_info->n_domain_groups, kinit_session_info->server_info->n_domain_groups, "Different numbers of domain groups for kinit-based PAC");
611         torture_assert_int_equal(tctx, netlogon_server_info->n_domain_groups, s2u4self_session_info->server_info->n_domain_groups, "Different numbers of domain groups for S2U4Self");
612
613         builtin_domain = dom_sid_parse_talloc(tmp_ctx, SID_BUILTIN);
614
615         for (i = 0; i < kinit_session_info->server_info->n_domain_groups; i++) {
616                 torture_assert(tctx, dom_sid_equal(netlogon_server_info->domain_groups[i], kinit_session_info->server_info->domain_groups[i]), "Different domain groups for kinit-based PAC");
617                 torture_assert(tctx, dom_sid_equal(netlogon_server_info->domain_groups[i], s2u4self_session_info->server_info->domain_groups[i]), "Different domain groups for S2U4Self");
618                 torture_assert(tctx, !dom_sid_in_domain(builtin_domain, s2u4self_session_info->server_info->domain_groups[i]), "Returned BUILTIN domain in groups for S2U4Self");
619                 torture_assert(tctx, !dom_sid_in_domain(builtin_domain, kinit_session_info->server_info->domain_groups[i]), "Returned BUILTIN domain in groups kinit-based PAC");
620                 torture_assert(tctx, !dom_sid_in_domain(builtin_domain, netlogon_server_info->domain_groups[i]), "Returned BUILTIN domian in groups from NETLOGON SamLogon reply");
621         }
622
623         return true;
624 }
625
626 static bool test_S2U4Self_bdc(struct torture_context *tctx,
627                                struct dcerpc_pipe *p,
628                                struct cli_credentials *credentials)
629 {
630         return test_S2U4Self(tctx, p, credentials, SEC_CHAN_BDC, TEST_MACHINE_NAME_S2U4SELF_BDC);
631 }
632
633 static bool test_S2U4Self_workstation(struct torture_context *tctx,
634                                   struct dcerpc_pipe *p,
635                                   struct cli_credentials *credentials)
636 {
637         return test_S2U4Self(tctx, p, credentials, SEC_CHAN_WKSTA, TEST_MACHINE_NAME_S2U4SELF_WKSTA);
638 }
639
640 struct torture_suite *torture_rpc_remote_pac(TALLOC_CTX *mem_ctx)
641 {
642         struct torture_suite *suite = torture_suite_create(mem_ctx, "pac");
643         struct torture_rpc_tcase *tcase;
644
645         /* It is important to use different names, so that old entries in our credential cache are not used */
646         tcase = torture_suite_add_machine_bdc_rpc_iface_tcase(suite, "netlogon-bdc",
647                                                               &ndr_table_netlogon, TEST_MACHINE_NAME_BDC);
648         torture_rpc_tcase_add_test_creds(tcase, "verify-sig", test_PACVerify_bdc);
649
650         tcase = torture_suite_add_machine_workstation_rpc_iface_tcase(suite, "netlogon-member",
651                                                                       &ndr_table_netlogon, TEST_MACHINE_NAME_WKSTA);
652         torture_rpc_tcase_add_test_creds(tcase, "verify-sig", test_PACVerify_workstation);
653
654         tcase = torture_suite_add_machine_workstation_rpc_iface_tcase(suite, "netlogon-member-des",
655                                                                       &ndr_table_netlogon, TEST_MACHINE_NAME_WKSTA_DES);
656         torture_rpc_tcase_add_test_join(tcase, "verify-sig", test_PACVerify_workstation_des);
657
658         tcase = torture_suite_add_machine_bdc_rpc_iface_tcase(suite, "netlogon-bdc",
659                                                               &ndr_table_netlogon, TEST_MACHINE_NAME_S2U4SELF_BDC);
660         torture_rpc_tcase_add_test_creds(tcase, "s2u4self", test_S2U4Self_bdc);
661
662         tcase = torture_suite_add_machine_workstation_rpc_iface_tcase(suite, "netlogon-member",
663                                                                       &ndr_table_netlogon, TEST_MACHINE_NAME_S2U4SELF_WKSTA);
664
665         torture_rpc_tcase_add_test_creds(tcase, "s2u4self", test_S2U4Self_workstation);
666         return suite;
667 }