2 * Copyright (c) 2003 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
33 #include "krb5_locl.h"
37 static int debug_flag
= 0;
38 static int version_flag
= 0;
39 static int help_flag
= 0;
41 #ifdef KRB5_USE_PATH_TOKENS
42 #define TEST_CC_NAME "%{TEMP}/krb5-cc-test-foo"
44 #define TEST_CC_NAME "/tmp/krb5-cc-test-foo"
48 test_default_name(krb5_context context
)
51 const char *p
, *test_cc_name
= TEST_CC_NAME
;
54 p
= krb5_cc_default_name(context
);
56 krb5_errx (context
, 1, "krb5_cc_default_name 1 failed");
59 ret
= krb5_cc_set_default_name(context
, NULL
);
61 krb5_errx (context
, 1, "krb5_cc_set_default_name failed");
63 p
= krb5_cc_default_name(context
);
65 krb5_errx (context
, 1, "krb5_cc_default_name 2 failed");
68 if (strcmp(p1
, p2
) != 0)
69 krb5_errx (context
, 1, "krb5_cc_default_name no longer same");
71 ret
= krb5_cc_set_default_name(context
, test_cc_name
);
73 krb5_errx (context
, 1, "krb5_cc_set_default_name 1 failed");
75 p
= krb5_cc_default_name(context
);
77 krb5_errx (context
, 1, "krb5_cc_default_name 2 failed");
80 #ifndef KRB5_USE_PATH_TOKENS
81 /* If we are using path tokens, we don't expect the p3 and
82 test_cc_name to match since p3 is going to have expanded
84 if (strcmp(p3
, test_cc_name
) != 0)
85 krb5_errx (context
, 1, "krb5_cc_set_default_name 1 failed");
94 * Check that a closed cc still keeps it data and that it's no longer
95 * there when it's destroyed.
99 test_mcache(krb5_context context
)
105 krb5_principal p
, p2
;
107 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
109 krb5_err(context
, 1, ret
, "krb5_parse_name");
111 ret
= krb5_cc_new_unique(context
, krb5_cc_type_memory
, NULL
, &id
);
113 krb5_err(context
, 1, ret
, "krb5_cc_new_unique");
115 ret
= krb5_cc_initialize(context
, id
, p
);
117 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
119 nc
= krb5_cc_get_name(context
, id
);
121 krb5_errx(context
, 1, "krb5_cc_get_name");
123 tc
= krb5_cc_get_type(context
, id
);
125 krb5_errx(context
, 1, "krb5_cc_get_name");
127 if (asprintf(&c
, "%s:%s", tc
, nc
) < 0 || c
== NULL
)
130 krb5_cc_close(context
, id
);
132 ret
= krb5_cc_resolve(context
, c
, &id2
);
134 krb5_err(context
, 1, ret
, "krb5_cc_resolve");
136 ret
= krb5_cc_get_principal(context
, id2
, &p2
);
138 krb5_err(context
, 1, ret
, "krb5_cc_get_principal");
140 if (krb5_principal_compare(context
, p
, p2
) == FALSE
)
141 krb5_errx(context
, 1, "p != p2");
143 krb5_cc_destroy(context
, id2
);
144 krb5_free_principal(context
, p
);
145 krb5_free_principal(context
, p2
);
147 ret
= krb5_cc_resolve(context
, c
, &id2
);
149 krb5_err(context
, 1, ret
, "krb5_cc_resolve");
151 ret
= krb5_cc_get_principal(context
, id2
, &p2
);
153 krb5_errx(context
, 1, "krb5_cc_get_principal");
155 krb5_cc_destroy(context
, id2
);
160 * Test that init works on a destroyed cc.
164 test_init_vs_destroy(krb5_context context
, const char *type
)
168 krb5_principal p
, p2
;
171 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
173 krb5_err(context
, 1, ret
, "krb5_parse_name");
175 ret
= krb5_cc_new_unique(context
, type
, NULL
, &id
);
177 krb5_err(context
, 1, ret
, "krb5_cc_new_unique");
179 if (asprintf(&n
, "%s:%s",
180 krb5_cc_get_type(context
, id
),
181 krb5_cc_get_name(context
, id
)) < 0 || n
== NULL
)
185 ret
= krb5_cc_resolve(context
, n
, &id2
);
188 krb5_err(context
, 1, ret
, "krb5_cc_resolve");
190 krb5_cc_destroy(context
, id
);
192 ret
= krb5_cc_initialize(context
, id2
, p
);
194 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
196 ret
= krb5_cc_get_principal(context
, id2
, &p2
);
198 krb5_err(context
, 1, ret
, "krb5_cc_get_principal");
200 krb5_cc_destroy(context
, id2
);
201 krb5_free_principal(context
, p
);
202 krb5_free_principal(context
, p2
);
206 test_cache_remove(krb5_context context
, const char *type
)
213 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
215 krb5_err(context
, 1, ret
, "krb5_parse_name");
217 ret
= krb5_cc_new_unique(context
, type
, NULL
, &id
);
219 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
221 ret
= krb5_cc_initialize(context
, id
, p
);
223 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
226 memset(&cred
, 0, sizeof(cred
));
227 ret
= krb5_parse_name(context
, "krbtgt/SU.SE@SU.SE", &cred
.server
);
229 krb5_err(context
, 1, ret
, "krb5_parse_name");
230 ret
= krb5_parse_name(context
, "lha@SU.SE", &cred
.client
);
232 krb5_err(context
, 1, ret
, "krb5_parse_name");
234 ret
= krb5_cc_store_cred(context
, id
, &cred
);
236 krb5_err(context
, 1, ret
, "krb5_cc_store_cred");
238 ret
= krb5_cc_remove_cred(context
, id
, 0, &cred
);
240 krb5_err(context
, 1, ret
, "krb5_cc_remove_cred");
242 ret
= krb5_cc_destroy(context
, id
);
244 krb5_err(context
, 1, ret
, "krb5_cc_destroy");
246 krb5_free_principal(context
, p
);
247 krb5_free_principal(context
, cred
.server
);
248 krb5_free_principal(context
, cred
.client
);
252 test_mcc_default(void)
254 krb5_context context
;
259 for (i
= 0; i
< 10; i
++) {
261 ret
= krb5_init_context(&context
);
263 krb5_err(context
, 1, ret
, "krb5_init_context");
265 ret
= krb5_cc_set_default_name(context
, "MEMORY:foo");
267 krb5_err(context
, 1, ret
, "krb5_cc_set_default_name");
269 ret
= krb5_cc_default(context
, &id
);
271 krb5_err(context
, 1, ret
, "krb5_cc_default");
273 ret
= krb5_cc_default(context
, &id2
);
275 krb5_err(context
, 1, ret
, "krb5_cc_default");
277 ret
= krb5_cc_close(context
, id
);
279 krb5_err(context
, 1, ret
, "krb5_cc_close");
281 ret
= krb5_cc_close(context
, id2
);
283 krb5_err(context
, 1, ret
, "krb5_cc_close");
285 krb5_free_context(context
);
295 { "foo%}", 0, "foo%}" },
297 { "foo%{null}", 0, "foo" },
298 { "foo%{null}bar", 0, "foobar" },
304 { "%{does not exist}", 1 },
306 #ifdef KRB5_USE_PATH_TOKENS
308 { "%{COMMON_APPDATA}", 0},
309 { "%{LOCAL_APPDATA}", 0},
315 { "%{USERCONFIG}", 0},
316 { "%{COMMONCONFIG}", 0},
325 test_def_cc_name(krb5_context context
)
331 for (i
= 0; i
< sizeof(cc_names
)/sizeof(cc_names
[0]); i
++) {
332 ret
= _krb5_expand_default_cc_name(context
, cc_names
[i
].str
, &str
);
334 if (cc_names
[i
].fail
== 0)
335 krb5_errx(context
, 1, "test %d \"%s\" failed",
338 if (cc_names
[i
].fail
)
339 krb5_errx(context
, 1, "test %d \"%s\" was successful",
341 if (cc_names
[i
].res
&& strcmp(cc_names
[i
].res
, str
) != 0)
342 krb5_errx(context
, 1, "test %d %s != %s",
343 i
, cc_names
[i
].res
, str
);
345 printf("%s => %s\n", cc_names
[i
].str
, str
);
352 test_cache_find(krb5_context context
, const char *principal
, int find
)
354 krb5_principal client
;
356 krb5_ccache id
= NULL
;
358 ret
= krb5_parse_name(context
, principal
, &client
);
360 krb5_err(context
, 1, ret
, "parse_name for %s failed", principal
);
362 ret
= krb5_cc_cache_match(context
, client
, &id
);
364 krb5_err(context
, 1, ret
, "cc_cache_match for %s failed", principal
);
365 if (ret
== 0 && !find
)
366 krb5_err(context
, 1, ret
, "cc_cache_match for %s found", principal
);
369 krb5_cc_close(context
, id
);
370 krb5_free_principal(context
, client
);
375 test_cache_iter(krb5_context context
, const char *type
, int destroy
)
377 krb5_cc_cache_cursor cursor
;
381 ret
= krb5_cc_cache_get_first (context
, type
, &cursor
);
382 if (ret
== KRB5_CC_NOSUPP
)
385 krb5_err(context
, 1, ret
, "krb5_cc_cache_get_first(%s)", type
);
388 while ((ret
= krb5_cc_cache_next (context
, cursor
, &id
)) == 0) {
389 krb5_principal principal
;
393 printf("name: %s\n", krb5_cc_get_name(context
, id
));
394 ret
= krb5_cc_get_principal(context
, id
, &principal
);
396 ret
= krb5_unparse_name(context
, principal
, &name
);
399 printf("\tprincipal: %s\n", name
);
402 krb5_free_principal(context
, principal
);
405 krb5_cc_destroy(context
, id
);
407 krb5_cc_close(context
, id
);
410 krb5_cc_cache_end_seq_get(context
, cursor
);
414 test_cache_iter_all(krb5_context context
)
416 krb5_cccol_cursor cursor
;
420 ret
= krb5_cccol_cursor_new (context
, &cursor
);
422 krb5_err(context
, 1, ret
, "krb5_cccol_cursor_new");
425 while ((ret
= krb5_cccol_cursor_next (context
, cursor
, &id
)) == 0 && id
!= NULL
) {
426 krb5_principal principal
;
430 printf("name: %s\n", krb5_cc_get_name(context
, id
));
431 ret
= krb5_cc_get_principal(context
, id
, &principal
);
433 ret
= krb5_unparse_name(context
, principal
, &name
);
436 printf("\tprincipal: %s\n", name
);
439 krb5_free_principal(context
, principal
);
441 krb5_cc_close(context
, id
);
444 krb5_cccol_cursor_free(context
, &cursor
);
449 test_copy(krb5_context context
, const char *from
, const char *to
)
451 krb5_ccache fromid
, toid
;
453 krb5_principal p
, p2
;
455 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
457 krb5_err(context
, 1, ret
, "krb5_parse_name");
459 ret
= krb5_cc_new_unique(context
, from
, NULL
, &fromid
);
461 krb5_err(context
, 1, ret
, "krb5_cc_new_unique");
463 ret
= krb5_cc_initialize(context
, fromid
, p
);
465 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
467 ret
= krb5_cc_new_unique(context
, to
, NULL
, &toid
);
469 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
471 ret
= krb5_cc_copy_cache(context
, fromid
, toid
);
473 krb5_err(context
, 1, ret
, "krb5_cc_copy_cache");
475 ret
= krb5_cc_get_principal(context
, toid
, &p2
);
477 krb5_err(context
, 1, ret
, "krb5_cc_get_principal");
479 if (krb5_principal_compare(context
, p
, p2
) == FALSE
)
480 krb5_errx(context
, 1, "p != p2");
482 krb5_free_principal(context
, p
);
483 krb5_free_principal(context
, p2
);
485 krb5_cc_destroy(context
, fromid
);
486 krb5_cc_destroy(context
, toid
);
490 test_move(krb5_context context
, const char *type
)
492 const krb5_cc_ops
*ops
;
493 krb5_ccache fromid
, toid
;
495 krb5_principal p
, p2
;
497 ops
= krb5_cc_get_prefix_ops(context
, type
);
501 ret
= krb5_cc_new_unique(context
, type
, NULL
, &fromid
);
502 if (ret
== KRB5_CC_NOSUPP
)
505 krb5_err(context
, 1, ret
, "krb5_cc_new_unique");
507 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
509 krb5_err(context
, 1, ret
, "krb5_parse_name");
511 ret
= krb5_cc_initialize(context
, fromid
, p
);
513 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
515 ret
= krb5_cc_new_unique(context
, type
, NULL
, &toid
);
517 krb5_err(context
, 1, ret
, "krb5_cc_new_unique");
519 ret
= krb5_cc_initialize(context
, toid
, p
);
521 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
523 ret
= krb5_cc_get_principal(context
, toid
, &p2
);
525 krb5_err(context
, 1, ret
, "krb5_cc_get_principal");
527 if (krb5_principal_compare(context
, p
, p2
) == FALSE
)
528 krb5_errx(context
, 1, "p != p2");
530 krb5_free_principal(context
, p
);
531 krb5_free_principal(context
, p2
);
533 krb5_cc_destroy(context
, toid
);
534 krb5_cc_destroy(context
, fromid
);
539 test_prefix_ops(krb5_context context
, const char *name
, const krb5_cc_ops
*ops
)
541 const krb5_cc_ops
*o
;
543 o
= krb5_cc_get_prefix_ops(context
, name
);
545 krb5_errx(context
, 1, "found no match for prefix '%s'", name
);
546 if (strcmp(o
->prefix
, ops
->prefix
) != 0)
547 krb5_errx(context
, 1, "ops for prefix '%s' is not "
548 "the expected %s != %s", name
, o
->prefix
, ops
->prefix
);
552 test_cc_config(krb5_context context
)
559 ret
= krb5_cc_new_unique(context
, "MEMORY", "bar", &id
);
561 krb5_err(context
, 1, ret
, "krb5_cc_new_unique");
563 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
565 krb5_err(context
, 1, ret
, "krb5_parse_name");
567 ret
= krb5_cc_initialize(context
, id
, p
);
569 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
571 for (i
= 0; i
< 1000; i
++) {
572 krb5_data data
, data2
;
573 const char *name
= "foo";
574 krb5_principal p1
= NULL
;
579 data
.data
= rk_UNCONST(name
);
580 data
.length
= strlen(name
);
582 ret
= krb5_cc_set_config(context
, id
, p1
, "FriendlyName", &data
);
584 krb5_errx(context
, 1, "krb5_cc_set_config: add");
586 ret
= krb5_cc_get_config(context
, id
, p1
, "FriendlyName", &data2
);
588 krb5_errx(context
, 1, "krb5_cc_get_config: first");
589 krb5_data_free(&data2
);
591 ret
= krb5_cc_set_config(context
, id
, p1
, "FriendlyName", &data
);
593 krb5_errx(context
, 1, "krb5_cc_set_config: add -second");
595 ret
= krb5_cc_get_config(context
, id
, p1
, "FriendlyName", &data2
);
597 krb5_errx(context
, 1, "krb5_cc_get_config: second");
598 krb5_data_free(&data2
);
600 ret
= krb5_cc_set_config(context
, id
, p1
, "FriendlyName", NULL
);
602 krb5_errx(context
, 1, "krb5_cc_set_config: delete");
604 ret
= krb5_cc_get_config(context
, id
, p1
, "FriendlyName", &data2
);
606 krb5_errx(context
, 1, "krb5_cc_get_config: non-existant");
609 krb5_cc_destroy(context
, id
);
610 krb5_free_principal(context
, p
);
614 static struct getargs args
[] = {
615 {"debug", 'd', arg_flag
, &debug_flag
,
616 "turn on debuggin", NULL
},
617 {"version", 0, arg_flag
, &version_flag
,
618 "print version", NULL
},
619 {"help", 0, arg_flag
, &help_flag
,
626 arg_printusage (args
, sizeof(args
)/sizeof(*args
), NULL
, "hostname ...");
631 main(int argc
, char **argv
)
633 krb5_context context
;
636 krb5_ccache id1
, id2
;
638 setprogname(argv
[0]);
640 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
654 ret
= krb5_init_context(&context
);
656 errx (1, "krb5_init_context failed: %d", ret
);
658 test_cache_remove(context
, krb5_cc_type_file
);
659 test_cache_remove(context
, krb5_cc_type_memory
);
661 test_cache_remove(context
, krb5_cc_type_scc
);
664 test_default_name(context
);
665 test_mcache(context
);
666 test_init_vs_destroy(context
, krb5_cc_type_memory
);
667 test_init_vs_destroy(context
, krb5_cc_type_file
);
669 test_init_vs_destroy(context
, krb5_cc_type_api
);
671 test_init_vs_destroy(context
, krb5_cc_type_scc
);
673 test_def_cc_name(context
);
675 test_cache_iter_all(context
);
677 test_cache_iter(context
, krb5_cc_type_memory
, 0);
680 krb5_cc_new_unique(context
, krb5_cc_type_memory
, "bar", &id1
);
681 krb5_cc_new_unique(context
, krb5_cc_type_memory
, "baz", &id2
);
682 krb5_parse_name(context
, "lha@SU.SE", &p
);
683 krb5_cc_initialize(context
, id1
, p
);
684 krb5_free_principal(context
, p
);
687 test_cache_find(context
, "lha@SU.SE", 1);
688 test_cache_find(context
, "hulabundulahotentot@SU.SE", 0);
690 test_cache_iter(context
, krb5_cc_type_memory
, 0);
691 test_cache_iter(context
, krb5_cc_type_memory
, 1);
692 test_cache_iter(context
, krb5_cc_type_memory
, 0);
693 test_cache_iter(context
, krb5_cc_type_file
, 0);
694 test_cache_iter(context
, krb5_cc_type_api
, 0);
695 test_cache_iter(context
, krb5_cc_type_scc
, 0);
696 test_cache_iter(context
, krb5_cc_type_scc
, 1);
698 test_copy(context
, krb5_cc_type_file
, krb5_cc_type_file
);
699 test_copy(context
, krb5_cc_type_memory
, krb5_cc_type_memory
);
700 test_copy(context
, krb5_cc_type_file
, krb5_cc_type_memory
);
701 test_copy(context
, krb5_cc_type_memory
, krb5_cc_type_file
);
702 test_copy(context
, krb5_cc_type_scc
, krb5_cc_type_file
);
703 test_copy(context
, krb5_cc_type_file
, krb5_cc_type_scc
);
704 test_copy(context
, krb5_cc_type_scc
, krb5_cc_type_memory
);
705 test_copy(context
, krb5_cc_type_memory
, krb5_cc_type_scc
);
707 test_move(context
, krb5_cc_type_file
);
708 test_move(context
, krb5_cc_type_memory
);
710 test_move(context
, krb5_cc_type_kcm
);
712 test_move(context
, krb5_cc_type_scc
);
714 test_prefix_ops(context
, "FILE:/tmp/foo", &krb5_fcc_ops
);
715 test_prefix_ops(context
, "FILE", &krb5_fcc_ops
);
716 test_prefix_ops(context
, "MEMORY", &krb5_mcc_ops
);
717 test_prefix_ops(context
, "MEMORY:foo", &krb5_mcc_ops
);
718 test_prefix_ops(context
, "/tmp/kaka", &krb5_fcc_ops
);
720 test_prefix_ops(context
, "SCC:", &krb5_scc_ops
);
721 test_prefix_ops(context
, "SCC:foo", &krb5_scc_ops
);
724 krb5_cc_destroy(context
, id1
);
725 krb5_cc_destroy(context
, id2
);
727 test_cc_config(context
);
729 krb5_free_context(context
);