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