Introduce system MIT krb5 build with --with-system-mitkrb5 option.
[kai/samba-autobuild/.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
441 #ifdef AD_DC_BUILD_IS_ENABLED
442         struct smb_krb5_context *smb_krb5_context;
443         krb5_error_code ret;
444
445         ret = cli_credentials_get_krb5_context(cmdline_credentials, tctx->lp_ctx, &smb_krb5_context);
446         torture_assert_int_equal(tctx, ret, 0, "cli_credentials_get_krb5_context() failed");
447
448         if (krb5_config_get_bool_default(smb_krb5_context->krb5_context, NULL, FALSE,
449                                          "libdefaults",
450                                          "allow_weak_crypto", NULL) == FALSE) {
451                 torture_skip(tctx, "Cannot test DES without [libdefaults] allow_weak_crypto = yes");
452         }
453 #else
454         torture_skip(tctx, "Skipping DES test in non-AD DC build");
455 #endif
456
457         /* Mark this workstation with DES-only */
458         user_info.info16.acct_flags = ACB_USE_DES_KEY_ONLY | ACB_WSTRUST;
459         r.in.user_handle = torture_join_samr_user_policy(join_ctx);
460         r.in.level = 16;
461         r.in.info = &user_info;
462
463         torture_assert_ntstatus_ok(tctx, dcerpc_samr_SetUserInfo_r(samr_pipe->binding_handle, tctx, &r),
464                 "failed to set DES info account flags");
465         torture_assert_ntstatus_ok(tctx, r.out.result,
466                 "failed to set DES into account flags");
467
468         return test_PACVerify(tctx, p, credentials, SEC_CHAN_WKSTA, TEST_MACHINE_NAME_WKSTA_DES);
469 }
470
471
472 /* 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 */
473 static bool test_S2U4Self(struct torture_context *tctx,
474                           struct dcerpc_pipe *p,
475                           struct cli_credentials *credentials,
476                           enum netr_SchannelType secure_channel_type,
477                           const char *test_machine_name)
478 {
479         NTSTATUS status;
480         struct dcerpc_binding_handle *b = p->binding_handle;
481
482         struct netr_LogonSamLogon r;
483
484         union netr_LogonLevel logon;
485         union netr_Validation validation;
486         uint8_t authoritative;
487
488         struct netr_Authenticator auth, auth2;
489
490         DATA_BLOB client_to_server, server_to_client;
491
492         struct netlogon_creds_CredentialState *creds;
493         struct gensec_security *gensec_client_context;
494         struct gensec_security *gensec_server_context;
495
496         struct auth4_context *auth_context;
497         struct auth_session_info *kinit_session_info;
498         struct auth_session_info *s2u4self_session_info;
499         struct auth_user_info_dc *netlogon_user_info_dc;
500
501         struct netr_NetworkInfo ninfo;
502         DATA_BLOB names_blob, chal, lm_resp, nt_resp;
503         size_t i;
504         int flags = CLI_CRED_NTLMv2_AUTH;
505
506         struct dom_sid *builtin_domain;
507
508         TALLOC_CTX *tmp_ctx = talloc_new(tctx);
509
510         torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
511
512         auth_context = talloc_zero(tmp_ctx, struct auth4_context);
513         torture_assert(tctx, auth_context != NULL, "talloc_new() failed");
514
515         auth_context->generate_session_info_pac = test_generate_session_info_pac;
516
517         /* First, do a normal Kerberos connection */
518
519         status = gensec_client_start(tctx, &gensec_client_context,
520                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx));
521         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
522
523         status = gensec_set_target_hostname(gensec_client_context, test_machine_name);
524
525         status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
526         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
527
528         status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
529         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
530
531         status = gensec_server_start(tctx,
532                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx),
533                                      auth_context, &gensec_server_context);
534         torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
535
536         status = gensec_set_credentials(gensec_server_context, credentials);
537         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
538
539         status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
540         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
541
542         server_to_client = data_blob(NULL, 0);
543
544         do {
545                 /* Do a client-server update dance */
546                 status = gensec_update(gensec_client_context, tmp_ctx, tctx->ev, server_to_client, &client_to_server);
547                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
548                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
549                 }
550
551                 status = gensec_update(gensec_server_context, tmp_ctx, tctx->ev, client_to_server, &server_to_client);
552                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
553                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
554                 }
555
556                 if (NT_STATUS_IS_OK(status)) {
557                         break;
558                 }
559         } while (1);
560
561         /* Extract the PAC using Samba's code */
562
563         status = gensec_session_info(gensec_server_context, gensec_server_context, &kinit_session_info);
564         torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
565
566
567         /* Now do the dance with S2U4Self */
568
569         /* Wipe out any existing ccache */
570         cli_credentials_invalidate_ccache(credentials, CRED_SPECIFIED);
571         cli_credentials_set_impersonate_principal(credentials,
572                         cli_credentials_get_principal(cmdline_credentials, tmp_ctx),
573                         talloc_asprintf(tmp_ctx, "host/%s", test_machine_name));
574
575         status = gensec_client_start(tctx, &gensec_client_context,
576                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx));
577         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
578
579         status = gensec_set_target_hostname(gensec_client_context, test_machine_name);
580
581         /* We now set the same credentials on both client and server contexts */
582         status = gensec_set_credentials(gensec_client_context, credentials);
583         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
584
585         status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
586         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
587
588         status = gensec_server_start(tctx,
589                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx),
590                                      auth_context, &gensec_server_context);
591         torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
592
593         status = gensec_set_credentials(gensec_server_context, credentials);
594         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
595
596         status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
597         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
598
599         server_to_client = data_blob(NULL, 0);
600
601         do {
602                 /* Do a client-server update dance */
603                 status = gensec_update(gensec_client_context, tmp_ctx, tctx->ev, server_to_client, &client_to_server);
604                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
605                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
606                 }
607
608                 status = gensec_update(gensec_server_context, tmp_ctx, tctx->ev, client_to_server, &server_to_client);
609                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
610                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
611                 }
612
613                 if (NT_STATUS_IS_OK(status)) {
614                         break;
615                 }
616         } while (1);
617
618         /* Don't pollute the remaining tests with the changed credentials */
619         cli_credentials_invalidate_ccache(credentials, CRED_SPECIFIED);
620         cli_credentials_set_target_service(credentials, NULL);
621         cli_credentials_set_impersonate_principal(credentials, NULL, NULL);
622
623         /* Extract the PAC using Samba's code */
624
625         status = gensec_session_info(gensec_server_context, gensec_server_context, &s2u4self_session_info);
626         torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
627
628         cli_credentials_get_ntlm_username_domain(cmdline_credentials, tctx,
629                                                  &ninfo.identity_info.account_name.string,
630                                                  &ninfo.identity_info.domain_name.string);
631
632         /* Now try with SamLogon */
633         generate_random_buffer(ninfo.challenge,
634                                sizeof(ninfo.challenge));
635         chal = data_blob_const(ninfo.challenge,
636                                sizeof(ninfo.challenge));
637
638         names_blob = NTLMv2_generate_names_blob(tctx, cli_credentials_get_workstation(credentials),
639                                                 cli_credentials_get_domain(credentials));
640
641         status = cli_credentials_get_ntlm_response(cmdline_credentials, tctx,
642                                                    &flags,
643                                                    chal,
644                                                    names_blob,
645                                                    &lm_resp, &nt_resp,
646                                                    NULL, NULL);
647         torture_assert_ntstatus_ok(tctx, status, "cli_credentials_get_ntlm_response failed");
648
649         ninfo.lm.data = lm_resp.data;
650         ninfo.lm.length = lm_resp.length;
651
652         ninfo.nt.data = nt_resp.data;
653         ninfo.nt.length = nt_resp.length;
654
655         ninfo.identity_info.parameter_control = 0;
656         ninfo.identity_info.logon_id_low = 0;
657         ninfo.identity_info.logon_id_high = 0;
658         ninfo.identity_info.workstation.string = cli_credentials_get_workstation(credentials);
659
660         logon.network = &ninfo;
661
662         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
663         r.in.computer_name = cli_credentials_get_workstation(credentials);
664         r.in.credential = &auth;
665         r.in.return_authenticator = &auth2;
666         r.in.logon_level = 2;
667         r.in.logon = &logon;
668         r.out.validation = &validation;
669         r.out.authoritative = &authoritative;
670
671         if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS,
672                                     credentials, secure_channel_type,
673                                     &creds)) {
674                 return false;
675         }
676
677         ZERO_STRUCT(auth2);
678         netlogon_creds_client_authenticator(creds, &auth);
679
680         r.in.validation_level = 3;
681
682         status = dcerpc_netr_LogonSamLogon_r(b, tctx, &r);
683         torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
684
685         torture_assert(tctx, netlogon_creds_client_check(creds,
686                                                          &r.out.return_authenticator->cred),
687                        "Credential chaining failed");
688
689         status = make_user_info_dc_netlogon_validation(tmp_ctx,
690                                                       ninfo.identity_info.account_name.string,
691                                                       r.in.validation_level,
692                                                       r.out.validation,
693                                                           true, /* This user was authenticated */
694                                                       &netlogon_user_info_dc);
695
696         torture_assert_ntstatus_ok(tctx, status, "make_user_info_dc_netlogon_validation failed");
697
698         torture_assert_str_equal(tctx, netlogon_user_info_dc->info->account_name == NULL ? "" : netlogon_user_info_dc->info->account_name,
699                                  kinit_session_info->info->account_name, "Account name differs for kinit-based PAC");
700         torture_assert_str_equal(tctx,netlogon_user_info_dc->info->account_name == NULL ? "" : netlogon_user_info_dc->info->account_name,
701                                  s2u4self_session_info->info->account_name, "Account name differs for S2U4Self");
702         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");
703         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");
704         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");
705         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");
706
707         builtin_domain = dom_sid_parse_talloc(tmp_ctx, SID_BUILTIN);
708
709         for (i = 0; i < kinit_session_info->torture->num_dc_sids; i++) {
710                 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");
711                 torture_assert(tctx, dom_sid_equal(&netlogon_user_info_dc->sids[i], &s2u4self_session_info->torture->dc_sids[i]), "Different domain groups for S2U4Self");
712                 torture_assert(tctx, !dom_sid_in_domain(builtin_domain, &s2u4self_session_info->torture->dc_sids[i]), "Returned BUILTIN domain in groups for S2U4Self");
713                 torture_assert(tctx, !dom_sid_in_domain(builtin_domain, &kinit_session_info->torture->dc_sids[i]), "Returned BUILTIN domain in groups kinit-based PAC");
714                 torture_assert(tctx, !dom_sid_in_domain(builtin_domain, &netlogon_user_info_dc->sids[i]), "Returned BUILTIN domian in groups from NETLOGON SamLogon reply");
715         }
716
717         return true;
718 }
719
720 static bool test_S2U4Self_bdc(struct torture_context *tctx,
721                                struct dcerpc_pipe *p,
722                                struct cli_credentials *credentials)
723 {
724         return test_S2U4Self(tctx, p, credentials, SEC_CHAN_BDC, TEST_MACHINE_NAME_S2U4SELF_BDC);
725 }
726
727 static bool test_S2U4Self_workstation(struct torture_context *tctx,
728                                   struct dcerpc_pipe *p,
729                                   struct cli_credentials *credentials)
730 {
731         return test_S2U4Self(tctx, p, credentials, SEC_CHAN_WKSTA, TEST_MACHINE_NAME_S2U4SELF_WKSTA);
732 }
733
734 struct torture_suite *torture_rpc_remote_pac(TALLOC_CTX *mem_ctx)
735 {
736         struct torture_suite *suite = torture_suite_create(mem_ctx, "pac");
737         struct torture_rpc_tcase *tcase;
738
739         /* It is important to use different names, so that old entries in our credential cache are not used */
740         tcase = torture_suite_add_machine_bdc_rpc_iface_tcase(suite, "netlogon-bdc",
741                                                               &ndr_table_netlogon, TEST_MACHINE_NAME_BDC);
742         torture_rpc_tcase_add_test_creds(tcase, "verify-sig", test_PACVerify_bdc);
743
744         tcase = torture_suite_add_machine_workstation_rpc_iface_tcase(suite, "netlogon-member",
745                                                                       &ndr_table_netlogon, TEST_MACHINE_NAME_WKSTA);
746         torture_rpc_tcase_add_test_creds(tcase, "verify-sig", test_PACVerify_workstation);
747
748         tcase = torture_suite_add_machine_workstation_rpc_iface_tcase(suite, "netlogon-member-des",
749                                                                       &ndr_table_netlogon, TEST_MACHINE_NAME_WKSTA_DES);
750         torture_rpc_tcase_add_test_join(tcase, "verify-sig", test_PACVerify_workstation_des);
751
752         tcase = torture_suite_add_machine_bdc_rpc_iface_tcase(suite, "netlogon-bdc",
753                                                               &ndr_table_netlogon, TEST_MACHINE_NAME_S2U4SELF_BDC);
754         torture_rpc_tcase_add_test_creds(tcase, "s2u4self", test_S2U4Self_bdc);
755
756         tcase = torture_suite_add_machine_workstation_rpc_iface_tcase(suite, "netlogon-member",
757                                                                       &ndr_table_netlogon, TEST_MACHINE_NAME_S2U4SELF_WKSTA);
758
759         torture_rpc_tcase_add_test_creds(tcase, "s2u4self", test_S2U4Self_workstation);
760         return suite;
761 }