Debian Codegraph @

source4/torture/krb5/kdc-canon-heimdal.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3
4 Validate the krb5 pac generation routines
5
6 Copyright (C) Andrew Bartlett <[email protected]> 2005-2015
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
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "system/kerberos.h"
25 #include "torture/smbtorture.h"
26 #include "torture/krb5/proto.h"
27 #include "auth/credentials/credentials.h"
28 #include "lib/cmdline/cmdline.h"
29 #include "source4/auth/kerberos/kerberos.h"
30 #include "source4/auth/kerberos/kerberos_util.h"
31 #include "lib/util/util_net.h"
32 #include "auth/auth.h"
33 #include "auth/auth_sam_reply.h"
34 #include "auth/gensec/gensec.h"
35 #include "param/param.h"
36
37 #undef strcasecmp
38
39 #define TEST_CANONICALIZE 0x0000001
40 #define TEST_ENTERPRISE 0x0000002
41 #define TEST_UPPER_USERNAME 0x0000004
42 #define TEST_WIN2K 0x0000008
43 #define TEST_UPN 0x0000010
44 #define TEST_S4U2SELF 0x0000020
45 #define TEST_REMOVEDOLLAR 0x0000040
46 #define TEST_AS_REQ_SPN 0x0000080
47 #define TEST_ALL 0x00000FF
48
49 struct test_data {
50 const char *test_name;
51 const char *realm;
52 const char *real_realm;
53 const char *real_domain;
54 const char *username;
55 const char *real_username;
56 bool canonicalize;
57 bool enterprise;
58 bool upper_username;
59 bool win2k;
60 bool upn;
61 bool other_upn_suffix;
62 bool s4u2self;
63 bool removedollar;
64 bool as_req_spn;
65 bool spn_is_upn;
66 const char *krb5_service;
67 const char *krb5_hostname;
68 };
69
70 struct torture_krb5_context {
71 struct smb_krb5_context *smb_krb5_context;
72 struct torture_context *tctx;
73 struct addrinfo *server;
74 struct test_data *test_data;
75 int packet_count;
76 };
77
78 struct pac_data {
79 const char *principal_name;
80 };
81
82 /*
83 * A helper function which avoids touching the local databases to
84 * generate the session info, as we just want to verify the principal
85 * name that we found in the ticket not the full local token
86 */
87 static NTSTATUS test_generate_session_info_pac(struct auth4_context *auth_ctx,
88 TALLOC_CTX *mem_ctx,
89 struct smb_krb5_context *smb_krb5_context,
90 DATA_BLOB *pac_blob,
91 const char *principal_name,
92 const struct tsocket_address *remote_address,
93 uint32_t session_info_flags,
94 struct auth_session_info **session_info)
95 {
96 NTSTATUS nt_status;
97 struct auth_user_info_dc *user_info_dc;
98 TALLOC_CTX *tmp_ctx;
99 struct pac_data *pac_data;
100
101 if (pac_blob == NULL) {
102 DBG_ERR("pac_blob missing\n");
103 return NT_STATUS_NO_IMPERSONATION_TOKEN;
104 }
105
106 tmp_ctx = talloc_named(mem_ctx, 0, "gensec_gssapi_session_info context");
107 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
108
109 auth_ctx->private_data = pac_data = talloc_zero(auth_ctx, struct pac_data);
110
111 pac_data->principal_name = talloc_strdup(pac_data, principal_name);
112 if (!pac_data->principal_name) {
113 talloc_free(tmp_ctx);
114 return NT_STATUS_NO_MEMORY;
115 }
116
117 nt_status = kerberos_pac_blob_to_user_info_dc(tmp_ctx,
118 *pac_blob,
119 smb_krb5_context->krb5_context,
120 &user_info_dc, NULL, NULL);
121 if (!NT_STATUS_IS_OK(nt_status)) {
122 talloc_free(tmp_ctx);
123 return nt_status;
124 }
125
126 if (!(user_info_dc->info->user_flags & NETLOGON_GUEST)) {
127 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
128 }
129
130 session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
131 nt_status = auth_generate_session_info(mem_ctx,
132 NULL,
133 NULL,
134 user_info_dc, session_info_flags,
135 session_info);
136 if (!NT_STATUS_IS_OK(nt_status)) {
137 talloc_free(tmp_ctx);
138 return nt_status;
139 }
140
141 talloc_free(tmp_ctx);
142 return NT_STATUS_OK;
143 }
144
145 /* Check to see if we can pass the PAC across to the NETLOGON server for validation */
146
147 /* Also happens to be a really good one-step verification of our Kerberos stack */
148
149 static bool test_accept_ticket(struct torture_context *tctx,
150 struct cli_credentials *credentials,
151 const char *principal,
152 DATA_BLOB client_to_server)
153 {
154 NTSTATUS status;
155 struct gensec_security *gensec_server_context;
156 DATA_BLOB server_to_client;
157 struct auth4_context *auth_context;
158 struct auth_session_info *session_info;
159 struct pac_data *pac_data;
160 TALLOC_CTX *tmp_ctx = talloc_new(tctx);
161
162 torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
163
164 auth_context = talloc_zero(tmp_ctx, struct auth4_context);
165 torture_assert(tctx, auth_context != NULL, "talloc_new() failed");
166
167 auth_context->generate_session_info_pac = test_generate_session_info_pac;
168
169 status = gensec_server_start(tctx,
170 lpcfg_gensec_settings(tctx, tctx->lp_ctx),
171 auth_context, &gensec_server_context);
172 torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
173
174 status = gensec_set_credentials(gensec_server_context, credentials);
175 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
176
177 status = gensec_start_mech_by_name(gensec_server_context, "krb5");
178 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_name (server) failed");
179
180 server_to_client = data_blob(NULL, 0);
181
182 /* Do a client-server update dance */
183 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
184 torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
185
186 /* Extract the PAC using Samba's code */
187
188 status = gensec_session_info(gensec_server_context, gensec_server_context, &session_info);
189 torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
190
191 pac_data = talloc_get_type(auth_context->private_data, struct pac_data);
192
193 torture_assert(tctx, pac_data != NULL, "gensec_update failed to fill in pac_data in auth_context");
194 torture_assert(tctx, pac_data->principal_name != NULL, "principal_name not present");
195 torture_assert_str_equal(tctx, pac_data->principal_name, principal, "wrong principal name");
196 return true;
197 }
198
199 /*
200 * This function is set in torture_krb5_init_context_canon as krb5
201 * send_and_recv function. This allows us to override what server the
202 * test is aimed at, and to inspect the packets just before they are
203 * sent to the network, and before they are processed on the recv
204 * side.
205 *
206 */
207 static krb5_error_code test_krb5_send_to_realm_canon_override(struct smb_krb5_context *smb_krb5_context,
208 void *data, /* struct torture_krb5_context */
209 krb5_const_realm realm,
210 time_t timeout,
211 const krb5_data *send_buf,
212 krb5_data *recv_buf)
213 {
214 krb5_error_code k5ret;
215
216 struct torture_krb5_context *test_context
217 = talloc_get_type_abort(data, struct torture_krb5_context);
218
219 SMB_ASSERT(smb_krb5_context == test_context->smb_krb5_context);
220
221 k5ret = smb_krb5_send_and_recv_func_forced_tcp(smb_krb5_context,
222 test_context->server,
223 timeout,
224 send_buf,
225 recv_buf);
226 if (k5ret != 0) {
227 return k5ret;
228 }
229
230 test_context->packet_count++;
231
232 return k5ret;
233 }
234
235 static int test_context_destructor(struct torture_krb5_context *test_context)
236 {
237 freeaddrinfo(test_context->server);
238 return 0;
239 }
240
241
242 static bool torture_krb5_init_context_canon(struct torture_context *tctx,
243 struct test_data *test_data,
244 struct torture_krb5_context **torture_krb5_context)
245 {
246 const char *host = torture_setting_string(tctx, "host", NULL);
247 krb5_error_code k5ret;
248 bool ok;
249
250 struct torture_krb5_context *test_context = talloc_zero(tctx, struct torture_krb5_context);
251 torture_assert(tctx, test_context != NULL, "Failed to allocate");
252
253 test_context->test_data = test_data;
254 test_context->tctx = tctx;
255
256 k5ret = smb_krb5_init_context(test_context, tctx->lp_ctx, &test_context->smb_krb5_context);
257 torture_assert_int_equal(tctx, k5ret, 0, "smb_krb5_init_context failed");
258
259 ok = interpret_string_addr_internal(&test_context->server, host, AI_NUMERICHOST);
260 torture_assert(tctx, ok, "Failed to parse target server");
261
262 talloc_set_destructor(test_context, test_context_destructor);
263
264 set_sockaddr_port(test_context->server->ai_addr, 88);
265
266 k5ret = smb_krb5_set_send_to_kdc_func(test_context->smb_krb5_context,
267 test_krb5_send_to_realm_canon_override,
268 NULL, /* send_to_kdc */
269 test_context);
270 torture_assert_int_equal(tctx, k5ret, 0, "krb5_set_send_to_kdc_func failed");
271 *torture_krb5_context = test_context;
272 return true;
273 }
274
275
276 static bool torture_krb5_as_req_canon(struct torture_context *tctx, const void *tcase_data)
277 {
278 krb5_error_code k5ret;
279 krb5_get_init_creds_opt *krb_options = NULL;
280 struct test_data *test_data = talloc_get_type_abort(tcase_data, struct test_data);
281 krb5_principal principal;
282 krb5_principal krbtgt_other;
283 krb5_principal canonical_principal;
284 krb5_principal expected_principal;
285 const char *principal_string = NULL;
286 char *krbtgt_other_string;
287 int principal_flags;
288 const char *canonical_principal_string = NULL;
289 const char *expected_principal_string = NULL;
290 char *canonical_unparse_principal_string;
291 char *expected_unparse_principal_string;
292 int expected_principal_flags;
293 char *got_principal_string;
294 char *assertion_message;
295 const char *password = cli_credentials_get_password(
296 samba_cmdline_get_creds());
297 krb5_context k5_context;
298 struct torture_krb5_context *test_context;
299 bool ok;
300 krb5_creds my_creds;
301 krb5_creds *server_creds;
302 krb5_ccache ccache;
303 krb5_auth_context auth_context;
304 char *cc_name;
305 krb5_data in_data, enc_ticket;
306 krb5_get_creds_opt opt;
307
308 bool require_canon = \
309 lpcfg_kdc_require_canonicalization(tctx->lp_ctx);
310
311 bool implicit_dollar_requires_canonicalize = \
312 ! lpcfg_kdc_name_match_implicit_dollar_without_canonicalization(
313 tctx->lp_ctx);
314 bool krb5_acceptor_report_canonical_client_name =
315 lpcfg_krb5_acceptor_report_canonical_client_name(tctx->lp_ctx);
316
317 const char *spn = NULL;
318 const char *spn_real_realm = NULL;
319 const char *upn = torture_setting_string(tctx, "krb5-upn", "");
320 test_data->krb5_service = torture_setting_string(tctx, "krb5-service", "host");
321 test_data->krb5_hostname = torture_setting_string(tctx, "krb5-hostname", "");
322
323 /*
324 * If we have not passed a UPN on the command line,
325 * then skip the UPN tests.
326 */
327 if (test_data->upn && upn[0] == '\0') {
328 torture_skip(tctx, "This test needs a UPN specified as --option=torture:[email protected] to run");
329 }
330
331 /*
332 * If we have not passed a SPN on the command line,
333 * then skip the SPN tests.
334 */
335 if (test_data->as_req_spn && test_data->krb5_hostname[0] == '\0') {
336 torture_skip(tctx, "This test needs a hostname specified as --option=torture:krb5-hostname=hostname.example.com and optionally --option=torture:krb5-service=service (defaults to host) to run");
337 }
338
339 if (test_data->removedollar &&
340 !torture_setting_bool(tctx, "run_removedollar_test", false))
341 {
342 torture_skip(tctx, "--option=torture:run_removedollar_test=true not specified");
343 }
344
345 test_data->realm = test_data->real_realm;
346
347 if (test_data->upn) {
348 char *p;
349 test_data->username = talloc_strdup(test_data, upn);
350 p = strchr(test_data->username, '@');
351 if (p) {
352 *p = '\0';
353 p++;
354 }
355 /*
356 * Test the UPN behaviour carefully. We can
357 * test in two different modes, depending on
358 * what UPN has been set up for us.
359 *
360 * If the UPN is in our realm, then we do all the tests with this name also.
361 *
362 * If the UPN is not in our realm, then we
363 * expect the tests that replace the realm to
364 * fail (as it won't match)
365 */
366 if (strcasecmp(p, test_data->real_realm) != 0) {
367 test_data->other_upn_suffix = true;
368 } else {
369 test_data->other_upn_suffix = false;
370 }
371
372 /*
373 * This lets us test the combination of the UPN prefix
374 * with a valid domain, without adding even more
375 * combinations
376 */
377 test_data->realm = p;
378 }
379
380 ok = torture_krb5_init_context_canon(tctx, test_data, &test_context);
381 torture_assert(tctx, ok, "torture_krb5_init_context failed");
382 k5_context = test_context->smb_krb5_context->krb5_context;
383
384 test_data->realm = strupper_talloc(test_data, test_data->realm);
385 if (test_data->upper_username) {
386 test_data->username = strupper_talloc(test_data, test_data->username);
387 } else {
388 test_data->username = talloc_strdup(test_data, test_data->username);
389 }
390
391 if (test_data->removedollar) {
392 char *p;
393
394 p = strchr_m(test_data->username, '$');
395 torture_assert(tctx, p != NULL, talloc_asprintf(tctx,
396 "username[%s] contains no '$'\n",
397 test_data->username));
398 *p = '\0';
399 }
400
401 spn = talloc_asprintf(test_data, "%s/%s@%s",
402 test_data->krb5_service,
403 test_data->krb5_hostname,
404 test_data->realm);
405
406 spn_real_realm = talloc_asprintf(test_data, "%s/%s@%s",
407 test_data->krb5_service,
408 test_data->krb5_hostname,
409 test_data->real_realm);
410
411 if (!test_data->canonicalize && test_data->enterprise) {
412 torture_skip(tctx,
413 "This test combination "
414 "is skipped intentionally");
415 }
416
417 if (test_data->as_req_spn) {
418 if (test_data->enterprise) {
419 torture_skip(tctx,
420 "This test combination "
421 "is skipped intentionally");
422 }
423 principal_string = spn;
424 } else {
425 principal_string = talloc_asprintf(test_data,
426 "%s@%s",
427 test_data->username,
428 test_data->realm);
429
430 }
431
432 test_data->spn_is_upn
433 = (strcasecmp(upn, spn) == 0);
434
435 if (test_data->as_req_spn && !test_data->spn_is_upn) {
436 canonical_principal_string = spn;
437 } else {
438 canonical_principal_string = talloc_asprintf(
439 test_data,
440 "%s@%s",
441 test_data->real_username,
442 test_data->real_realm);
443 }
444
445 /*
446 * If we are set to canonicalize, we get back the fixed UPPER
447 * case realm, and the real username (ie matching LDAP
448 * samAccountName)
449 *
450 * Otherwise, if we are set to enterprise, we
451 * get back the whole principal as-sent
452 *
453 * Finally, if we are not set to canonicalize, we get back the
454 * fixed UPPER case realm, but the as-sent username
455 */
456 if (test_data->as_req_spn && !test_data->spn_is_upn) {
457 expected_principal_string = spn;
458 } else if (test_data->canonicalize) {
459 expected_principal_string = talloc_asprintf(test_data,
460 "%s@%s",
461 test_data->real_username,
462 test_data->real_realm);
463 } else if (test_data->as_req_spn && test_data->spn_is_upn) {
464 expected_principal_string = spn_real_realm;
465 } else {
466 expected_principal_string = talloc_asprintf(test_data,
467 "%s@%s",
468 test_data->username,
469 test_data->real_realm);
470 }
471
472 if (test_data->enterprise) {
473 principal_flags = KRB5_PRINCIPAL_PARSE_ENTERPRISE;
474 } else {
475 if (test_data->upn && test_data->other_upn_suffix) {
476 torture_skip(tctx, "UPN test for UPN with other UPN suffix only runs with enterprise principals");
477 }
478 principal_flags = 0;
479 }
480
481 if (test_data->canonicalize) {
482 expected_principal_flags = 0;
483 } else {
484 expected_principal_flags = principal_flags;
485 }
486
487 torture_assert_int_equal(tctx,
488 krb5_parse_name_flags(k5_context,
489 principal_string,
490 principal_flags,
491 &principal),
492 0, "krb5_parse_name_flags failed");
493 torture_assert_int_equal(tctx,
494 krb5_parse_name_flags(k5_context,
495 canonical_principal_string,
496 expected_principal_flags,
497 &canonical_principal),
498 0, "krb5_parse_name_flags failed");
499 torture_assert_int_equal(tctx,
500 krb5_parse_name_flags(k5_context,
501 expected_principal_string,
502 expected_principal_flags,
503 &expected_principal),
504 0, "krb5_parse_name_flags failed");
505
506 if (test_data->as_req_spn) {
507 if (test_data->upn) {
508 krb5_principal_set_type(k5_context,
509 principal,
510 KRB5_NT_PRINCIPAL);
511 krb5_principal_set_type(k5_context,
512 canonical_principal,
513 KRB5_NT_PRINCIPAL);
514 krb5_principal_set_type(k5_context,
515 expected_principal,
516 KRB5_NT_PRINCIPAL);
517 } else {
518 krb5_principal_set_type(k5_context,
519 principal,
520 KRB5_NT_SRV_HST);
521 krb5_principal_set_type(k5_context,
522 canonical_principal,
523 KRB5_NT_SRV_HST);
524 krb5_principal_set_type(k5_context,
525 expected_principal,
526 KRB5_NT_SRV_HST);
527 }
528 }
529
530 torture_assert_int_equal(tctx,
531 krb5_unparse_name(k5_context,
532 canonical_principal,
533 &canonical_unparse_principal_string),
534 0, "krb5_unparse_name failed");
535 torture_assert_int_equal(tctx,
536 krb5_unparse_name(k5_context,
537 expected_principal,
538 &expected_unparse_principal_string),
539 0, "krb5_unparse_name failed");
540 /*
541 * Prepare a AS-REQ and run the TEST_AS_REQ tests
542 *
543 */
544
545 test_context->packet_count = 0;
546
547 /*
548 * Set the canonicalize flag if this test requires it
549 */
550 torture_assert_int_equal(tctx,
551 krb5_get_init_creds_opt_alloc(k5_context, &krb_options),
552 0, "krb5_get_init_creds_opt_alloc failed");
553
554 torture_assert_int_equal(tctx,
555 krb5_get_init_creds_opt_set_canonicalize(k5_context,
556 krb_options,
557 test_data->canonicalize),
558 0, "krb5_get_init_creds_opt_set_canonicalize failed");
559
560 torture_assert_int_equal(tctx,
561 krb5_get_init_creds_opt_set_win2k(k5_context,
562 krb_options,
563 test_data->win2k),
564 0, "krb5_get_init_creds_opt_set_win2k failed");
565
566 k5ret = krb5_get_init_creds_password(k5_context, &my_creds, principal,
567 password, NULL, NULL, 0,
568 NULL, krb_options);
569
570 if (test_context->test_data->as_req_spn
571 && !test_context->test_data->spn_is_upn) {
572 torture_assert_int_equal(tctx, k5ret,
573 KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN,
574 "Got wrong error_code from "
575 "krb5_get_init_creds_password");
576 /* We can't proceed with more checks */
577 return true;
578 } else if (implicit_dollar_requires_canonicalize &&
579 test_context->test_data->removedollar &&
580 ! test_context->test_data->canonicalize) {
581 /*
582 * We are trying to match "foo" to "foo$", but we the
583 * server is configured to not make that match without
584 * canonicalization.
585 */
586 torture_assert_int_equal(tctx, k5ret,
587 KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN,
588 "Got wrong error_code from "
589 "krb5_get_init_creds_password "
590 "(with no implicit dollar config)");
591 return true;
592 } else if (require_canon && ! test_context->test_data->canonicalize) {
593 /*
594 * The server is requiring canonicalization, and we are not using it.
595 * This should always fail.
596 */
597 torture_assert_int_equal(tctx, k5ret,
598 KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN,
599 "Principal should not match with "
600 "'require canonicalization = yes' "
601 "when canonicalization is not used.");
602 return true;
603 } else {
604 assertion_message = talloc_asprintf(tctx,
605 "krb5_get_init_creds_password for %s failed: %s",
606 principal_string,
607 smb_get_krb5_error_message(k5_context, k5ret, tctx));
608 torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
609 }
610
611 torture_assert(tctx,
612 test_context->packet_count > 1,
613 "Expected krb5_get_init_creds_password to send more packets");
614
615 /*
616 * Assert that the reply was with the correct type of
617 * principal, depending on the flags we set
618 */
619 if (!test_data->canonicalize && test_data->as_req_spn) {
620 torture_assert_int_equal(tctx,
621 krb5_principal_get_type(k5_context,
622 my_creds.client),
623 KRB5_NT_SRV_HST,
624 "smb_krb5_init_context gave incorrect client->name.name_type");
625 } else {
626 torture_assert_int_equal(tctx,
627 krb5_principal_get_type(k5_context,
628 my_creds.client),
629 KRB5_NT_PRINCIPAL,
630 "smb_krb5_init_context gave incorrect client->name.name_type");
631 }
632
633 torture_assert_int_equal(tctx,
634 krb5_unparse_name(k5_context,
635 my_creds.client, &got_principal_string), 0,
636 "krb5_unparse_name failed");
637
638 assertion_message = talloc_asprintf(tctx,
639 "krb5_get_init_creds_password returned a different principal %s to what was expected %s",
640 got_principal_string, expected_principal_string);
641 krb5_xfree(got_principal_string);
642
643 torture_assert(tctx, krb5_principal_compare(k5_context,
644 my_creds.client, expected_principal),
645 assertion_message);
646
647
648 torture_assert_int_equal(tctx,
649 krb5_principal_get_type(k5_context,
650 my_creds.server), KRB5_NT_SRV_INST,
651 "smb_krb5_init_context gave incorrect server->name.name_type");
652
653 torture_assert_int_equal(tctx,
654 krb5_principal_get_num_comp(k5_context,
655 my_creds.server), 2,
656 "smb_krb5_init_context gave incorrect number of components in my_creds.server->name");
657
658 torture_assert_str_equal(tctx,
659 krb5_principal_get_comp_string(k5_context,
660 my_creds.server, 0),
661 "krbtgt",
662 "smb_krb5_init_context gave incorrect my_creds.server->name.name_string[0]");
663
664 if (test_data->canonicalize) {
665 torture_assert_str_equal(tctx,
666 krb5_principal_get_comp_string(k5_context,
667 my_creds.server, 1),
668 test_data->real_realm,
669
670 "smb_krb5_init_context gave incorrect my_creds.server->name.name_string[1]");
671 } else {
672 torture_assert_str_equal(tctx,
673 krb5_principal_get_comp_string(k5_context,
674 my_creds.server, 1),
675 test_data->realm,
676
677 "smb_krb5_init_context gave incorrect my_creds.server->name.name_string[1]");
678 }
679 torture_assert_str_equal(tctx,
680 krb5_principal_get_realm(k5_context,
681 my_creds.server),
682 test_data->real_realm,
683 "smb_krb5_init_context gave incorrect my_creds.server->realm");
684
685 /* Store the result of the 'kinit' above into a memory ccache */
686 cc_name = talloc_asprintf(tctx, "MEMORY:%s", test_data->test_name);
687 torture_assert_int_equal(tctx, krb5_cc_resolve(k5_context, cc_name,
688 &ccache),
689 0, "krb5_cc_resolve failed");
690
691 torture_assert_int_equal(tctx, krb5_cc_initialize(k5_context,
692 ccache, my_creds.client),
693 0, "krb5_cc_initialize failed");
694
695 torture_assert_int_equal(tctx, krb5_cc_store_cred(k5_context,
696 ccache, &my_creds),
697 0, "krb5_cc_store_cred failed");
698
699 /*
700 * Prepare a TGS-REQ and run the TEST_TGS_REQ_KRBTGT_CANON tests
701 *
702 * This tests krb5_get_creds behaviour, which allows us to set
703 * the KRB5_GC_CANONICALIZE option against the krbtgt/ principal
704 */
705
706 krbtgt_other_string = talloc_asprintf(test_data, "krbtgt/%s@%s", test_data->real_domain, test_data->real_realm);
707 torture_assert_int_equal(tctx,
708 krb5_make_principal(k5_context, &krbtgt_other,
709 test_data->real_realm, "krbtgt",
710 test_data->real_domain, NULL),
711 0, "krb5_make_principal failed");
712
713 test_context->packet_count = 0;
714
715 torture_assert_int_equal(tctx,
716 krb5_get_creds_opt_alloc(k5_context, &opt),
717 0, "krb5_get_creds_opt_alloc");
718
719 krb5_get_creds_opt_add_options(k5_context,
720 opt,
721 KRB5_GC_CANONICALIZE);
722
723 krb5_get_creds_opt_add_options(k5_context,
724 opt,
725 KRB5_GC_NO_STORE);
726
727 /* Confirm if we can get a ticket krbtgt/realm that we got back with the initial kinit */
728 k5ret = krb5_get_creds(k5_context, opt, ccache, krbtgt_other, &server_creds);
729
730 if (require_canon && ! test_context->test_data->canonicalize) {
731 /*
732 * The server is requiring canonicalization, and we are not using it.
733 * This should always fail.
734 */
735 torture_assert_int_equal(tctx, k5ret,
736 KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN,
737 "Principal should not match with "
738 "'require canonicalization = yes' "
739 "when canonicalization is not used.");
740 return true;
741 }
742 {
743 /*
744 * In these situations, the code above does not store a
745 * principal in the credentials cache matching what
746 * krb5_get_creds() needs without talking to the KDC, so the
747 * test fails with looping detected because when we set
748 * canonicalize we confuse the client libs.
749 *
750 */
751 assertion_message = talloc_asprintf(tctx,
752 "krb5_get_creds for %s should have failed with looping detected: %s",
753 krbtgt_other_string,
754 smb_get_krb5_error_message(k5_context, k5ret,
755 tctx));
756
757 torture_assert_int_equal(tctx, k5ret, KRB5_GET_IN_TKT_LOOP, assertion_message);
758 torture_assert_int_equal(tctx,
759 test_context->packet_count,
760 2, "Expected krb5_get_creds to send packets");
761 }
762
763 /*
764 * Prepare a TGS-REQ and run the TEST_TGS_REQ_CANON tests
765 *
766 * This tests krb5_get_creds behaviour, which allows us to set
767 * the KRB5_GC_CANONICALIZE option
768 */
769
770 test_context->packet_count = 0;
771
772 torture_assert_int_equal(tctx,
773 krb5_get_creds_opt_alloc(k5_context, &opt),
774 0, "krb5_get_creds_opt_alloc");
775
776 krb5_get_creds_opt_add_options(k5_context,
777 opt,
778 KRB5_GC_CANONICALIZE);
779
780 krb5_get_creds_opt_add_options(k5_context,
781 opt,
782 KRB5_GC_NO_STORE);
783
784 if (test_data->s4u2self) {
785 torture_assert_int_equal(tctx,
786 krb5_get_creds_opt_set_impersonate(k5_context,
787 opt,
788 principal),
789 0, "krb5_get_creds_opt_set_impersonate failed");
790 }
791
792 /* Confirm if we can get a ticket to our own name */
793 k5ret = krb5_get_creds(k5_context, opt, ccache, principal, &server_creds);
794
795 /*
796 * In these situations, the code above does not store a
797 * principal in the credentials cache matching what
798 * krb5_get_creds() needs, so the test fails.
799 *
800 */
801 {
802 assertion_message = talloc_asprintf(tctx,
803 "krb5_get_creds for %s failed: %s",
804 principal_string,
805 smb_get_krb5_error_message(k5_context, k5ret,
806 tctx));
807
808 /*
809 * Only machine accounts (strictly, accounts with a
810 * servicePrincipalName) can expect this test to succeed
811 */
812 if (torture_setting_bool(tctx, "expect_machine_account", false)
813 && (test_data->enterprise
814 || test_data->spn_is_upn
815 || !test_data->upn)) {
816 torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
817 torture_assert_int_equal(tctx, krb5_cc_store_cred(k5_context,
818 ccache, server_creds),
819 0, "krb5_cc_store_cred failed");
820
821 torture_assert_int_equal(tctx,
822 krb5_free_creds(k5_context,
823 server_creds),
824 0, "krb5_free_cred_contents failed");
825
826 torture_assert_int_equal(tctx,
827 test_context->packet_count,
828 1, "Expected krb5_get_creds to send one packet");
829
830 } else {
831 torture_assert_int_equal(tctx, k5ret, KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN,
832 assertion_message);
833 /* Account for get_cred_kdc_capath() and get_cred_kdc_referral() fallback */
834 torture_assert_int_equal(tctx,
835 test_context->packet_count,
836 2, "Expected krb5_get_creds to send 2 packets");
837 }
838 }
839
840 /*
841 * Confirm getting a ticket to pass to the server, running
842 * either the TEST_TGS_REQ or TEST_SELF_TRUST_TGS_REQ stage.
843 *
844 * This triggers the client to attempt to get a
845 * cross-realm ticket between the alternate names of
846 * the server, and we need to confirm that behaviour.
847 *
848 */
849
850 test_context->packet_count = 0;
851 torture_assert_int_equal(tctx, krb5_auth_con_init(k5_context, &auth_context),
852 0, "krb5_auth_con_init failed");
853
854 in_data.length = 0;
855 k5ret = krb5_mk_req_exact(k5_context,
856 &auth_context,
857 AP_OPTS_USE_SUBKEY,
858 principal,
859 &in_data, ccache,
860 &enc_ticket);
861 assertion_message = talloc_asprintf(tctx,
862 "krb5_mk_req_exact for %s failed: %s",
863 principal_string,
864 smb_get_krb5_error_message(k5_context, k5ret, tctx));
865
866 /*
867 * Only machine accounts (strictly, accounts with a
868 * servicePrincipalName) can expect this test to succeed
869 */
870 if (torture_setting_bool(tctx, "expect_machine_account", false)
871 && (test_data->enterprise ||
872 (test_context->test_data->as_req_spn
873 || test_context->test_data->spn_is_upn)
874 || !test_data->upn)) {
875 DATA_BLOB client_to_server;
876 torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
877 client_to_server = data_blob_const(enc_ticket.data, enc_ticket.length);
878
879 if (krb5_acceptor_report_canonical_client_name) {
880 torture_assert(tctx,
881 test_accept_ticket(tctx,
882 samba_cmdline_get_creds(),
883 canonical_unparse_principal_string,
884 client_to_server),
885 "test_accept_ticket failed - failed to accept the ticket we just created");
886 /* This is very weird */
887 } else if (!test_data->canonicalize
888 && test_context->test_data->as_req_spn
889 && test_context->test_data->spn_is_upn
890 && test_context->test_data->s4u2self) {
891
892 torture_assert(tctx,
893 test_accept_ticket(tctx,
894 samba_cmdline_get_creds(),
895 spn_real_realm,
896 client_to_server),
897 "test_accept_ticket failed - failed to accept the ticket we just created");
898 } else if (test_data->canonicalize
899 && test_context->test_data->as_req_spn
900 && test_context->test_data->spn_is_upn
901 && test_context->test_data->s4u2self) {
902
903 torture_assert(tctx,
904 test_accept_ticket(tctx,
905 samba_cmdline_get_creds(),
906 expected_principal_string,
907 client_to_server),
908 "test_accept_ticket failed - failed to accept the ticket we just created");
909 } else if (test_data->canonicalize
910 && !test_data->enterprise
911 && test_context->test_data->upn
912 && test_context->test_data->spn_is_upn
913 && test_context->test_data->s4u2self) {
914
915 torture_assert(tctx,
916 test_accept_ticket(tctx,
917 samba_cmdline_get_creds(),
918 expected_principal_string,
919 client_to_server),
920 "test_accept_ticket failed - failed to accept the ticket we just created");
921 } else if (!test_data->canonicalize
922 && test_context->test_data->upn
923 && test_context->test_data->spn_is_upn
924 && test_context->test_data->s4u2self) {
925
926 const char *accept_expected_principal_string
927 = talloc_asprintf(test_data,
928 "%s@%s",
929 test_data->username,
930 test_data->real_realm);
931
932 torture_assert(tctx,
933 test_accept_ticket(tctx,
934 samba_cmdline_get_creds(),
935 accept_expected_principal_string,
936 client_to_server),
937 "test_accept_ticket failed - failed to accept the ticket we just created");
938 } else {
939
940 torture_assert(tctx,
941 test_accept_ticket(tctx,
942 samba_cmdline_get_creds(),
943 expected_unparse_principal_string,
944 client_to_server),
945 "test_accept_ticket failed - failed to accept the ticket we just created");
946 }
947 krb5_data_free(&enc_ticket);
948 } else {
949 torture_assert_int_equal(tctx, k5ret, KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN,
950 assertion_message);
951 }
952
953 /*
954 * Confirm getting a ticket to pass to the server, running
955 * the TEST_TGS_REQ_HOST, TEST_TGS_REQ_HOST_SRV_INST, TEST_TGS_REQ_HOST_SRV_HST stage
956 *
957 * This triggers the client to attempt to get a
958 * cross-realm ticket between the alternate names of
959 * the server, and we need to confirm that behaviour.
960 *
961 */
962
963 if (*test_data->krb5_service && *test_data->krb5_hostname) {
964 krb5_principal host_principal_srv_inst;
965 /*
966 * This tries to guess when the krb5 libs will ask for a
967 * cross-realm ticket, and when they will just ask the KDC
968 * directly.
969 */
970 test_context->packet_count = 0;
971 torture_assert_int_equal(tctx, krb5_auth_con_init(k5_context, &auth_context),
972 0, "krb5_auth_con_init failed");
973
974 in_data.length = 0;
975 k5ret = krb5_mk_req(k5_context,
976 &auth_context,
977 0,
978 test_data->krb5_service,
979 test_data->krb5_hostname,
980 &in_data, ccache,
981 &enc_ticket);
982
983 {
984 assertion_message = talloc_asprintf(tctx,
985 "krb5_mk_req for %s/%s failed: %s",
986 test_data->krb5_service,
987 test_data->krb5_hostname,
988 smb_get_krb5_error_message(k5_context, k5ret, tctx));
989
990 torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
991
992 if (!test_data->spn_is_upn) {
993 /*
994 * Only in these cases would the above
995 * code have needed to send packets to
996 * the network
997 */
998 torture_assert(tctx,
999 test_context->packet_count > 0,
1000 "Expected krb5_get_creds to send packets");
1001 }
1002 }
1003
1004
1005 test_context->packet_count = 0;
1006
1007 torture_assert_int_equal(tctx,
1008 krb5_make_principal(k5_context, &host_principal_srv_inst,
1009 test_data->real_realm,
1010 strupper_talloc(tctx, test_data->krb5_service),
1011 test_data->krb5_hostname,
1012 NULL),
1013 0, "krb5_make_principal failed");
1014
1015 krb5_principal_set_type(k5_context, host_principal_srv_inst, KRB5_NT_SRV_INST);
1016
1017 torture_assert_int_equal(tctx, krb5_auth_con_init(k5_context, &auth_context),
1018 0, "krb5_auth_con_init failed");
1019
1020 in_data.length = 0;
1021 k5ret = krb5_mk_req_exact(k5_context,
1022 &auth_context,
1023 0,
1024 host_principal_srv_inst,
1025 &in_data, ccache,
1026 &enc_ticket);
1027 krb5_free_principal(k5_context, host_principal_srv_inst);
1028 {
1029 assertion_message = talloc_asprintf(tctx,
1030 "krb5_mk_req for %s/%s KRB5_NT_SRV_INST failed: %s",
1031 test_data->krb5_service,
1032 test_data->krb5_hostname,
1033 smb_get_krb5_error_message(k5_context, k5ret, tctx));
1034
1035 torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
1036 /*
1037 * Only in these cases would the above code have needed to
1038 * send packets to the network
1039 */
1040 torture_assert(tctx,
1041 test_context->packet_count > 0,
1042 "Expected krb5_get_creds to send packets");
1043 }
1044
1045
1046 test_context->packet_count = 0;
1047
1048 torture_assert_int_equal(tctx,
1049 krb5_make_principal(k5_context, &host_principal_srv_inst,
1050 test_data->real_realm,
1051 test_data->krb5_service,
1052 strupper_talloc(tctx, test_data->krb5_hostname),
1053 NULL),
1054 0, "krb5_make_principal failed");
1055
1056 krb5_principal_set_type(k5_context, host_principal_srv_inst, KRB5_NT_SRV_HST);
1057
1058 torture_assert_int_equal(tctx, krb5_auth_con_init(k5_context, &auth_context),
1059 0, "krb5_auth_con_init failed");
1060
1061 in_data.length = 0;
1062 k5ret = krb5_mk_req_exact(k5_context,
1063 &auth_context,
1064 0,
1065 host_principal_srv_inst,
1066 &in_data, ccache,
1067 &enc_ticket);
1068 krb5_free_principal(k5_context, host_principal_srv_inst);
1069 {
1070 assertion_message = talloc_asprintf(tctx,
1071 "krb5_mk_req for %s/%s KRB5_NT_SRV_INST failed: %s",
1072 test_data->krb5_service,
1073 test_data->krb5_hostname,
1074 smb_get_krb5_error_message(k5_context, k5ret, tctx));
1075
1076 torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
1077 /*
1078 * Only in these cases would the above code have needed to
1079 * send packets to the network
1080 */
1081 torture_assert(tctx,
1082 test_context->packet_count > 0,
1083 "Expected krb5_get_creds to send packets");
1084 }
1085 }
1086
1087 /*
1088 * Confirm getting a ticket for the same krbtgt/realm that we
1089 * got back with the initial ticket, running the
1090 * TEST_TGS_REQ_KRBTGT stage.
1091 *
1092 */
1093
1094 test_context->packet_count = 0;
1095
1096 in_data.length = 0;
1097 k5ret = krb5_mk_req_exact(k5_context,
1098 &auth_context,
1099 0,
1100 my_creds.server,
1101 &in_data, ccache,
1102 &enc_ticket);
1103
1104 assertion_message = talloc_asprintf(tctx,
1105 "krb5_mk_req_exact for %s failed: %s",
1106 principal_string,
1107 smb_get_krb5_error_message(k5_context, k5ret, tctx));
1108 torture_assert_int_equal(tctx, k5ret, 0, assertion_message);
1109
1110 krb5_free_principal(k5_context, principal);
1111 krb5_get_init_creds_opt_free(k5_context, krb_options);
1112
1113 torture_assert_int_equal(tctx, krb5_free_cred_contents(k5_context, &my_creds),
1114 0, "krb5_free_cred_contents failed");
1115
1116 return true;
1117 }
1118
1119 struct torture_suite *torture_krb5_canon(TALLOC_CTX *mem_ctx)
1120 {
1121 unsigned int i;
1122 struct torture_suite *suite = torture_suite_create(mem_ctx, "canon");
1123 suite->description = talloc_strdup(suite, "Kerberos Canonicalisation tests");
1124
1125 for (i = 0; i < TEST_ALL; i++) {
1126 char *name = talloc_asprintf(suite, "%s.%s.%s.%s.%s.%s",
1127 (i & TEST_CANONICALIZE) ? "canon" : "no-canon",
1128 (i & TEST_ENTERPRISE) ? "enterprise" : "no-enterprise",
1129 (i & TEST_UPPER_USERNAME) ? "uc-user" : "lc-user",
1130 (i & TEST_WIN2K) ? "win2k" : "no-win2k",
1131 (i & TEST_UPN) ? "upn" :
1132 ((i & TEST_AS_REQ_SPN) ? "spn" :
1133 ((i & TEST_REMOVEDOLLAR) ? "removedollar" : "samaccountname")),
1134 (i & TEST_S4U2SELF) ? "s4u2self" : "normal");
1135 struct torture_suite *sub_suite = torture_suite_create(mem_ctx, name);
1136
1137 struct test_data *test_data = talloc_zero(suite, struct test_data);
1138 if (i & TEST_UPN) {
1139 if (i & TEST_AS_REQ_SPN) {
1140 continue;
1141 }
1142 }
1143 if ((i & TEST_UPN) || (i & TEST_AS_REQ_SPN)) {
1144 if (i & TEST_REMOVEDOLLAR) {
1145 continue;
1146 }
1147 }
1148
1149 test_data->test_name = name;
1150 test_data->real_realm
1151 = strupper_talloc(test_data,
1152 cli_credentials_get_realm(
1153 samba_cmdline_get_creds()));
1154 test_data->real_domain = cli_credentials_get_domain(
1155 samba_cmdline_get_creds());
1156 test_data->username = cli_credentials_get_username(
1157 samba_cmdline_get_creds());
1158 test_data->real_username = cli_credentials_get_username(
1159 samba_cmdline_get_creds());
1160 test_data->canonicalize = (i & TEST_CANONICALIZE) != 0;
1161 test_data->enterprise = (i & TEST_ENTERPRISE) != 0;
1162 test_data->upper_username = (i & TEST_UPPER_USERNAME) != 0;
1163 test_data->win2k = (i & TEST_WIN2K) != 0;
1164 test_data->upn = (i & TEST_UPN) != 0;
1165 test_data->s4u2self = (i & TEST_S4U2SELF) != 0;
1166 test_data->removedollar = (i & TEST_REMOVEDOLLAR) != 0;
1167 test_data->as_req_spn = (i & TEST_AS_REQ_SPN) != 0;
1168 torture_suite_add_simple_tcase_const(sub_suite, name, torture_krb5_as_req_canon,
1169 test_data);
1170 torture_suite_add_suite(suite, sub_suite);
1171
1172 }
1173 return suite;
1174 }