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"
39 static int debug_flag
= 0;
40 static int version_flag
= 0;
41 static int help_flag
= 0;
44 test_default_name(krb5_context context
)
47 const char *p
, *test_cc_name
= "/tmp/krb5-cc-test-foo";
50 p
= krb5_cc_default_name(context
);
52 krb5_errx (context
, 1, "krb5_cc_default_name 1 failed");
55 ret
= krb5_cc_set_default_name(context
, NULL
);
57 krb5_errx (context
, 1, "krb5_cc_set_default_name failed");
59 p
= krb5_cc_default_name(context
);
61 krb5_errx (context
, 1, "krb5_cc_default_name 2 failed");
64 if (strcmp(p1
, p2
) != 0)
65 krb5_errx (context
, 1, "krb5_cc_default_name no longer same");
67 ret
= krb5_cc_set_default_name(context
, test_cc_name
);
69 krb5_errx (context
, 1, "krb5_cc_set_default_name 1 failed");
71 p
= krb5_cc_default_name(context
);
73 krb5_errx (context
, 1, "krb5_cc_default_name 2 failed");
76 if (strcmp(p3
, test_cc_name
) != 0)
77 krb5_errx (context
, 1, "krb5_cc_set_default_name 1 failed");
85 * Check that a closed cc still keeps it data and that it's no longer
86 * there when it's destroyed.
90 test_mcache(krb5_context context
)
98 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
100 krb5_err(context
, 1, ret
, "krb5_parse_name");
102 ret
= krb5_cc_gen_new(context
, &krb5_mcc_ops
, &id
);
104 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
106 ret
= krb5_cc_initialize(context
, id
, p
);
108 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
110 nc
= krb5_cc_get_name(context
, id
);
112 krb5_errx(context
, 1, "krb5_cc_get_name");
114 tc
= krb5_cc_get_type(context
, id
);
116 krb5_errx(context
, 1, "krb5_cc_get_name");
118 asprintf(&c
, "%s:%s", tc
, nc
);
120 krb5_cc_close(context
, id
);
122 ret
= krb5_cc_resolve(context
, c
, &id2
);
124 krb5_err(context
, 1, ret
, "krb5_cc_resolve");
126 ret
= krb5_cc_get_principal(context
, id2
, &p2
);
128 krb5_err(context
, 1, ret
, "krb5_cc_get_principal");
130 if (krb5_principal_compare(context
, p
, p2
) == FALSE
)
131 krb5_errx(context
, 1, "p != p2");
133 krb5_cc_destroy(context
, id2
);
134 krb5_free_principal(context
, p
);
135 krb5_free_principal(context
, p2
);
137 ret
= krb5_cc_resolve(context
, c
, &id2
);
139 krb5_err(context
, 1, ret
, "krb5_cc_resolve");
141 ret
= krb5_cc_get_principal(context
, id2
, &p2
);
143 krb5_errx(context
, 1, "krb5_cc_get_principal");
145 krb5_cc_destroy(context
, id2
);
150 * Test that init works on a destroyed cc.
154 test_init_vs_destroy(krb5_context context
, const krb5_cc_ops
*ops
)
158 krb5_principal p
, p2
;
161 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
163 krb5_err(context
, 1, ret
, "krb5_parse_name");
165 ret
= krb5_cc_gen_new(context
, ops
, &id
);
167 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
169 asprintf(&n
, "%s:%s",
170 krb5_cc_get_type(context
, id
),
171 krb5_cc_get_name(context
, id
));
173 ret
= krb5_cc_resolve(context
, n
, &id2
);
176 krb5_err(context
, 1, ret
, "krb5_cc_resolve");
178 krb5_cc_destroy(context
, id
);
180 ret
= krb5_cc_initialize(context
, id2
, p
);
182 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
184 ret
= krb5_cc_get_principal(context
, id2
, &p2
);
186 krb5_err(context
, 1, ret
, "krb5_cc_get_principal");
188 krb5_cc_destroy(context
, id2
);
189 krb5_free_principal(context
, p
);
190 krb5_free_principal(context
, p2
);
194 test_cache_remove(krb5_context context
, const krb5_cc_ops
*ops
)
201 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
203 krb5_err(context
, 1, ret
, "krb5_parse_name");
205 ret
= krb5_cc_gen_new(context
, ops
, &id
);
207 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
209 ret
= krb5_cc_initialize(context
, id
, p
);
211 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
214 memset(&cred
, 0, sizeof(cred
));
215 ret
= krb5_parse_name(context
, "krbtgt/SU.SE@SU.SE", &cred
.server
);
217 krb5_err(context
, 1, ret
, "krb5_parse_name");
218 ret
= krb5_parse_name(context
, "lha@SU.SE", &cred
.client
);
220 krb5_err(context
, 1, ret
, "krb5_parse_name");
222 ret
= krb5_cc_store_cred(context
, id
, &cred
);
224 krb5_err(context
, 1, ret
, "krb5_cc_store_cred");
226 ret
= krb5_cc_remove_cred(context
, id
, 0, &cred
);
228 krb5_err(context
, 1, ret
, "krb5_cc_remove_cred");
230 ret
= krb5_cc_destroy(context
, id
);
232 krb5_err(context
, 1, ret
, "krb5_cc_destroy");
234 krb5_free_principal(context
, p
);
235 krb5_free_principal(context
, cred
.server
);
236 krb5_free_principal(context
, cred
.client
);
240 test_mcc_default(void)
242 krb5_context context
;
247 for (i
= 0; i
< 10; i
++) {
249 ret
= krb5_init_context(&context
);
251 krb5_err(context
, 1, ret
, "krb5_init_context");
253 ret
= krb5_cc_set_default_name(context
, "MEMORY:foo");
255 krb5_err(context
, 1, ret
, "krb5_cc_set_default_name");
257 ret
= krb5_cc_default(context
, &id
);
259 krb5_err(context
, 1, ret
, "krb5_cc_default");
261 ret
= krb5_cc_default(context
, &id2
);
263 krb5_err(context
, 1, ret
, "krb5_cc_default");
265 ret
= krb5_cc_close(context
, id
);
267 krb5_err(context
, 1, ret
, "krb5_cc_close");
269 ret
= krb5_cc_close(context
, id2
);
271 krb5_err(context
, 1, ret
, "krb5_cc_close");
273 krb5_free_context(context
);
284 { "foo%{null}", 0, "foo" },
285 { "foo%{null}bar", 0, "foobar" },
292 test_def_cc_name(krb5_context context
)
298 for (i
= 0; i
< sizeof(cc_names
)/sizeof(cc_names
[0]); i
++) {
299 ret
= _krb5_expand_default_cc_name(context
, cc_names
[i
].str
, &str
);
301 if (cc_names
[i
].fail
== 0)
302 krb5_errx(context
, 1, "test %d \"%s\" failed",
305 if (cc_names
[i
].fail
)
306 krb5_errx(context
, 1, "test %d \"%s\" was successful",
308 if (cc_names
[i
].res
&& strcmp(cc_names
[i
].res
, str
) != 0)
309 krb5_errx(context
, 1, "test %d %s != %s",
310 i
, cc_names
[i
].res
, str
);
312 printf("%s => %s\n", cc_names
[i
].str
, str
);
319 test_cache_find(krb5_context context
, const char *type
, const char *principal
,
322 krb5_principal client
;
324 krb5_ccache id
= NULL
;
326 ret
= krb5_parse_name(context
, principal
, &client
);
328 krb5_err(context
, 1, ret
, "parse_name for %s failed", principal
);
330 ret
= krb5_cc_cache_match(context
, client
, type
, &id
);
332 krb5_err(context
, 1, ret
, "cc_cache_match for %s failed", principal
);
333 if (ret
== 0 && !find
)
334 krb5_err(context
, 1, ret
, "cc_cache_match for %s found", principal
);
337 krb5_cc_close(context
, id
);
338 krb5_free_principal(context
, client
);
343 test_cache_iter(krb5_context context
, const char *type
, int destroy
)
345 krb5_cc_cache_cursor cursor
;
349 ret
= krb5_cc_cache_get_first (context
, type
, &cursor
);
350 if (ret
== KRB5_CC_NOSUPP
)
353 krb5_err(context
, 1, ret
, "krb5_cc_cache_get_first(%s)", type
);
356 while ((ret
= krb5_cc_cache_next (context
, cursor
, &id
)) == 0) {
357 krb5_principal principal
;
361 printf("name: %s\n", krb5_cc_get_name(context
, id
));
362 ret
= krb5_cc_get_principal(context
, id
, &principal
);
364 ret
= krb5_unparse_name(context
, principal
, &name
);
367 printf("\tprincipal: %s\n", name
);
370 krb5_free_principal(context
, principal
);
373 krb5_cc_destroy(context
, id
);
375 krb5_cc_close(context
, id
);
378 krb5_cc_cache_end_seq_get(context
, cursor
);
382 test_copy(krb5_context context
, const char *fromtype
, const char *totype
)
384 const krb5_cc_ops
*from
, *to
;
385 krb5_ccache fromid
, toid
;
387 krb5_principal p
, p2
;
389 from
= krb5_cc_get_prefix_ops(context
, fromtype
);
391 krb5_errx(context
, 1, "%s isn't a type", fromtype
);
393 to
= krb5_cc_get_prefix_ops(context
, totype
);
395 krb5_errx(context
, 1, "%s isn't a type", totype
);
397 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
399 krb5_err(context
, 1, ret
, "krb5_parse_name");
401 ret
= krb5_cc_gen_new(context
, from
, &fromid
);
403 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
405 ret
= krb5_cc_initialize(context
, fromid
, p
);
407 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
409 ret
= krb5_cc_gen_new(context
, to
, &toid
);
411 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
413 ret
= krb5_cc_copy_cache(context
, fromid
, toid
);
415 krb5_err(context
, 1, ret
, "krb5_cc_copy_cache");
417 ret
= krb5_cc_get_principal(context
, toid
, &p2
);
419 krb5_err(context
, 1, ret
, "krb5_cc_get_principal");
421 if (krb5_principal_compare(context
, p
, p2
) == FALSE
)
422 krb5_errx(context
, 1, "p != p2");
424 krb5_free_principal(context
, p
);
425 krb5_free_principal(context
, p2
);
427 krb5_cc_destroy(context
, fromid
);
428 krb5_cc_destroy(context
, toid
);
432 test_move(krb5_context context
, const char *type
)
434 const krb5_cc_ops
*ops
;
435 krb5_ccache fromid
, toid
;
437 krb5_principal p
, p2
;
439 ops
= krb5_cc_get_prefix_ops(context
, type
);
441 krb5_errx(context
, 1, "%s isn't a type", type
);
443 ret
= krb5_cc_gen_new(context
, ops
, &fromid
);
444 if (ret
== KRB5_CC_NOSUPP
)
447 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
449 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
451 krb5_err(context
, 1, ret
, "krb5_parse_name");
453 ret
= krb5_cc_initialize(context
, fromid
, p
);
455 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
457 ret
= krb5_cc_gen_new(context
, ops
, &toid
);
459 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
461 ret
= krb5_cc_initialize(context
, toid
, p
);
463 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
465 ret
= krb5_cc_get_principal(context
, toid
, &p2
);
467 krb5_err(context
, 1, ret
, "krb5_cc_get_principal");
469 if (krb5_principal_compare(context
, p
, p2
) == FALSE
)
470 krb5_errx(context
, 1, "p != p2");
472 krb5_free_principal(context
, p
);
473 krb5_free_principal(context
, p2
);
475 krb5_cc_destroy(context
, toid
);
480 test_prefix_ops(krb5_context context
, const char *name
, const krb5_cc_ops
*ops
)
482 const krb5_cc_ops
*o
;
484 o
= krb5_cc_get_prefix_ops(context
, name
);
486 krb5_errx(context
, 1, "found no match for prefix '%s'", name
);
487 if (strcmp(o
->prefix
, ops
->prefix
) != 0)
488 krb5_errx(context
, 1, "ops for prefix '%s' is not "
489 "the expected %s != %s", name
, o
->prefix
, ops
->prefix
);
493 static struct getargs args
[] = {
494 {"debug", 'd', arg_flag
, &debug_flag
,
495 "turn on debuggin", NULL
},
496 {"version", 0, arg_flag
, &version_flag
,
497 "print version", NULL
},
498 {"help", 0, arg_flag
, &help_flag
,
505 arg_printusage (args
, sizeof(args
)/sizeof(*args
), NULL
, "hostname ...");
510 main(int argc
, char **argv
)
512 krb5_context context
;
515 krb5_ccache id1
, id2
;
517 setprogname(argv
[0]);
519 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
533 ret
= krb5_init_context(&context
);
535 errx (1, "krb5_init_context failed: %d", ret
);
537 test_cache_remove(context
, &krb5_fcc_ops
);
538 test_cache_remove(context
, &krb5_mcc_ops
);
539 test_cache_remove(context
, &krb5_scc_ops
);
541 test_default_name(context
);
542 test_mcache(context
);
543 test_init_vs_destroy(context
, &krb5_mcc_ops
);
544 test_init_vs_destroy(context
, &krb5_fcc_ops
);
546 test_init_vs_destroy(context
, &krb5_acc_ops
);
548 test_init_vs_destroy(context
, &krb5_scc_ops
);
550 test_def_cc_name(context
);
551 test_cache_iter(context
, "MEMORY", 0);
554 krb5_cc_new_unique(context
, "MEMORY", "bar", &id1
);
555 krb5_cc_new_unique(context
, "MEMORY", "baz", &id2
);
556 krb5_parse_name(context
, "lha@SU.SE", &p
);
557 krb5_cc_initialize(context
, id1
, p
);
558 krb5_free_principal(context
, p
);
561 test_cache_find(context
, "MEMORY", "lha@SU.SE", 1);
562 test_cache_find(context
, "MEMORY", "hulabundulahotentot@SU.SE", 0);
564 test_cache_iter(context
, "MEMORY", 0);
565 test_cache_iter(context
, "MEMORY", 1);
566 test_cache_iter(context
, "MEMORY", 0);
567 test_cache_iter(context
, "FILE", 0);
568 test_cache_iter(context
, "API", 0);
569 test_cache_iter(context
, "SDB", 0);
570 test_cache_iter(context
, "SDB", 1);
572 test_copy(context
, "FILE", "FILE");
573 test_copy(context
, "MEMORY", "MEMORY");
574 test_copy(context
, "FILE", "MEMORY");
575 test_copy(context
, "MEMORY", "FILE");
576 test_copy(context
, "SDB", "FILE");
577 test_copy(context
, "FILE", "SDB");
578 test_copy(context
, "SDB", "MEMORY");
579 test_copy(context
, "MEMORY", "SDB");
581 test_move(context
, "FILE");
582 test_move(context
, "MEMORY");
583 test_move(context
, "KCM");
584 test_move(context
, "SDB");
586 test_prefix_ops(context
, "FILE:/tmp/foo", &krb5_fcc_ops
);
587 test_prefix_ops(context
, "FILE", &krb5_fcc_ops
);
588 test_prefix_ops(context
, "MEMORY", &krb5_mcc_ops
);
589 test_prefix_ops(context
, "MEMORY:foo", &krb5_mcc_ops
);
590 test_prefix_ops(context
, "/tmp/kaka", &krb5_fcc_ops
);
591 test_prefix_ops(context
, "SDB:", &krb5_scc_ops
);
592 test_prefix_ops(context
, "SDB:foo", &krb5_scc_ops
);
594 krb5_cc_destroy(context
, id1
);
595 krb5_cc_destroy(context
, id2
);
597 krb5_free_context(context
);