No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / test_cc.c
blobbf258f1a68896d0c75e0a838a4745911a33e6784
1 /*
2 * Copyright (c) 2003 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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"
34 #include <getarg.h>
35 #include <err.h>
37 __RCSID("$Heimdal: test_cc.c 22115 2007-12-03 21:21:42Z lha $"
38 "$NetBSD$");
40 static int debug_flag = 0;
41 static int version_flag = 0;
42 static int help_flag = 0;
44 static void
45 test_default_name(krb5_context context)
47 krb5_error_code ret;
48 const char *p, *test_cc_name = "/tmp/krb5-cc-test-foo";
49 char *p1, *p2, *p3;
51 p = krb5_cc_default_name(context);
52 if (p == NULL)
53 krb5_errx (context, 1, "krb5_cc_default_name 1 failed");
54 p1 = estrdup(p);
56 ret = krb5_cc_set_default_name(context, NULL);
57 if (p == NULL)
58 krb5_errx (context, 1, "krb5_cc_set_default_name failed");
60 p = krb5_cc_default_name(context);
61 if (p == NULL)
62 krb5_errx (context, 1, "krb5_cc_default_name 2 failed");
63 p2 = estrdup(p);
65 if (strcmp(p1, p2) != 0)
66 krb5_errx (context, 1, "krb5_cc_default_name no longer same");
68 ret = krb5_cc_set_default_name(context, test_cc_name);
69 if (p == NULL)
70 krb5_errx (context, 1, "krb5_cc_set_default_name 1 failed");
72 p = krb5_cc_default_name(context);
73 if (p == NULL)
74 krb5_errx (context, 1, "krb5_cc_default_name 2 failed");
75 p3 = estrdup(p);
77 if (strcmp(p3, test_cc_name) != 0)
78 krb5_errx (context, 1, "krb5_cc_set_default_name 1 failed");
80 free(p1);
81 free(p2);
82 free(p3);
86 * Check that a closed cc still keeps it data and that it's no longer
87 * there when it's destroyed.
90 static void
91 test_mcache(krb5_context context)
93 krb5_error_code ret;
94 krb5_ccache id, id2;
95 const char *nc, *tc;
96 char *c;
97 krb5_principal p, p2;
99 ret = krb5_parse_name(context, "lha@SU.SE", &p);
100 if (ret)
101 krb5_err(context, 1, ret, "krb5_parse_name");
103 ret = krb5_cc_gen_new(context, &krb5_mcc_ops, &id);
104 if (ret)
105 krb5_err(context, 1, ret, "krb5_cc_gen_new");
107 ret = krb5_cc_initialize(context, id, p);
108 if (ret)
109 krb5_err(context, 1, ret, "krb5_cc_initialize");
111 nc = krb5_cc_get_name(context, id);
112 if (nc == NULL)
113 krb5_errx(context, 1, "krb5_cc_get_name");
115 tc = krb5_cc_get_type(context, id);
116 if (tc == NULL)
117 krb5_errx(context, 1, "krb5_cc_get_name");
119 asprintf(&c, "%s:%s", tc, nc);
121 krb5_cc_close(context, id);
123 ret = krb5_cc_resolve(context, c, &id2);
124 if (ret)
125 krb5_err(context, 1, ret, "krb5_cc_resolve");
127 ret = krb5_cc_get_principal(context, id2, &p2);
128 if (ret)
129 krb5_err(context, 1, ret, "krb5_cc_get_principal");
131 if (krb5_principal_compare(context, p, p2) == FALSE)
132 krb5_errx(context, 1, "p != p2");
134 krb5_cc_destroy(context, id2);
135 krb5_free_principal(context, p);
136 krb5_free_principal(context, p2);
138 ret = krb5_cc_resolve(context, c, &id2);
139 if (ret)
140 krb5_err(context, 1, ret, "krb5_cc_resolve");
142 ret = krb5_cc_get_principal(context, id2, &p2);
143 if (ret == 0)
144 krb5_errx(context, 1, "krb5_cc_get_principal");
146 krb5_cc_destroy(context, id2);
147 free(c);
151 * Test that init works on a destroyed cc.
154 static void
155 test_init_vs_destroy(krb5_context context, const krb5_cc_ops *ops)
157 krb5_error_code ret;
158 krb5_ccache id, id2;
159 krb5_principal p, p2;
160 char *n;
162 ret = krb5_parse_name(context, "lha@SU.SE", &p);
163 if (ret)
164 krb5_err(context, 1, ret, "krb5_parse_name");
166 ret = krb5_cc_gen_new(context, ops, &id);
167 if (ret)
168 krb5_err(context, 1, ret, "krb5_cc_gen_new");
170 asprintf(&n, "%s:%s",
171 krb5_cc_get_type(context, id),
172 krb5_cc_get_name(context, id));
174 ret = krb5_cc_resolve(context, n, &id2);
175 free(n);
176 if (ret)
177 krb5_err(context, 1, ret, "krb5_cc_resolve");
179 krb5_cc_destroy(context, id);
181 ret = krb5_cc_initialize(context, id2, p);
182 if (ret)
183 krb5_err(context, 1, ret, "krb5_cc_initialize");
185 ret = krb5_cc_get_principal(context, id2, &p2);
186 if (ret)
187 krb5_err(context, 1, ret, "krb5_cc_get_principal");
189 krb5_cc_destroy(context, id2);
190 krb5_free_principal(context, p);
191 krb5_free_principal(context, p2);
194 static void
195 test_fcache_remove(krb5_context context)
197 krb5_error_code ret;
198 krb5_ccache id;
199 krb5_principal p;
200 krb5_creds cred;
202 ret = krb5_parse_name(context, "lha@SU.SE", &p);
203 if (ret)
204 krb5_err(context, 1, ret, "krb5_parse_name");
206 ret = krb5_cc_gen_new(context, &krb5_fcc_ops, &id);
207 if (ret)
208 krb5_err(context, 1, ret, "krb5_cc_gen_new");
210 ret = krb5_cc_initialize(context, id, p);
211 if (ret)
212 krb5_err(context, 1, ret, "krb5_cc_initialize");
214 /* */
215 memset(&cred, 0, sizeof(cred));
216 ret = krb5_parse_name(context, "krbtgt/SU.SE@SU.SE", &cred.server);
217 if (ret)
218 krb5_err(context, 1, ret, "krb5_parse_name");
219 ret = krb5_parse_name(context, "lha@SU.SE", &cred.client);
220 if (ret)
221 krb5_err(context, 1, ret, "krb5_parse_name");
223 ret = krb5_cc_store_cred(context, id, &cred);
224 if (ret)
225 krb5_err(context, 1, ret, "krb5_cc_store_cred");
227 ret = krb5_cc_remove_cred(context, id, 0, &cred);
228 if (ret)
229 krb5_err(context, 1, ret, "krb5_cc_remove_cred");
231 ret = krb5_cc_destroy(context, id);
232 if (ret)
233 krb5_err(context, 1, ret, "krb5_cc_destroy");
235 krb5_free_principal(context, p);
236 krb5_free_principal(context, cred.server);
237 krb5_free_principal(context, cred.client);
240 static void
241 test_mcc_default(void)
243 krb5_context context;
244 krb5_error_code ret;
245 krb5_ccache id, id2;
246 int i;
248 for (i = 0; i < 10; i++) {
250 ret = krb5_init_context(&context);
251 if (ret)
252 krb5_err(context, 1, ret, "krb5_init_context");
254 ret = krb5_cc_set_default_name(context, "MEMORY:foo");
255 if (ret)
256 krb5_err(context, 1, ret, "krb5_cc_set_default_name");
258 ret = krb5_cc_default(context, &id);
259 if (ret)
260 krb5_err(context, 1, ret, "krb5_cc_default");
262 ret = krb5_cc_default(context, &id2);
263 if (ret)
264 krb5_err(context, 1, ret, "krb5_cc_default");
266 ret = krb5_cc_close(context, id);
267 if (ret)
268 krb5_err(context, 1, ret, "krb5_cc_close");
270 ret = krb5_cc_close(context, id2);
271 if (ret)
272 krb5_err(context, 1, ret, "krb5_cc_close");
274 krb5_free_context(context);
278 struct {
279 char *str;
280 int fail;
281 char *res;
282 } cc_names[] = {
283 { "foo", 0, "foo" },
284 { "%{uid}", 0 },
285 { "foo%{null}", 0, "foo" },
286 { "foo%{null}bar", 0, "foobar" },
287 { "%{", 1 },
288 { "%{foo %{", 1 },
289 { "%{{", 1 },
292 static void
293 test_def_cc_name(krb5_context context)
295 krb5_error_code ret;
296 char *str;
297 int i;
299 for (i = 0; i < sizeof(cc_names)/sizeof(cc_names[0]); i++) {
300 ret = _krb5_expand_default_cc_name(context, cc_names[i].str, &str);
301 if (ret) {
302 if (cc_names[i].fail == 0)
303 krb5_errx(context, 1, "test %d \"%s\" failed",
304 i, cc_names[i].str);
305 } else {
306 if (cc_names[i].fail)
307 krb5_errx(context, 1, "test %d \"%s\" was successful",
308 i, cc_names[i].str);
309 if (cc_names[i].res && strcmp(cc_names[i].res, str) != 0)
310 krb5_errx(context, 1, "test %d %s != %s",
311 i, cc_names[i].res, str);
312 if (debug_flag)
313 printf("%s => %s\n", cc_names[i].str, str);
314 free(str);
319 static void
320 test_cache_find(krb5_context context, const char *type, const char *principal,
321 int find)
323 krb5_principal client;
324 krb5_error_code ret;
325 krb5_ccache id = NULL;
327 ret = krb5_parse_name(context, principal, &client);
328 if (ret)
329 krb5_err(context, 1, ret, "parse_name for %s failed", principal);
331 ret = krb5_cc_cache_match(context, client, type, &id);
332 if (ret && find)
333 krb5_err(context, 1, ret, "cc_cache_match for %s failed", principal);
334 if (ret == 0 && !find)
335 krb5_err(context, 1, ret, "cc_cache_match for %s found", principal);
337 if (id)
338 krb5_cc_close(context, id);
339 krb5_free_principal(context, client);
343 static void
344 test_cache_iter(krb5_context context, const char *type, int destroy)
346 krb5_cc_cache_cursor cursor;
347 krb5_error_code ret;
348 krb5_ccache id;
350 ret = krb5_cc_cache_get_first (context, type, &cursor);
351 if (ret == KRB5_CC_NOSUPP)
352 return;
353 else if (ret)
354 krb5_err(context, 1, ret, "krb5_cc_cache_get_first(%s)", type);
357 while ((ret = krb5_cc_cache_next (context, cursor, &id)) == 0) {
358 krb5_principal principal;
359 char *name;
361 if (debug_flag)
362 printf("name: %s\n", krb5_cc_get_name(context, id));
363 ret = krb5_cc_get_principal(context, id, &principal);
364 if (ret == 0) {
365 ret = krb5_unparse_name(context, principal, &name);
366 if (ret == 0) {
367 if (debug_flag)
368 printf("\tprincipal: %s\n", name);
369 free(name);
371 krb5_free_principal(context, principal);
373 if (destroy)
374 krb5_cc_destroy(context, id);
375 else
376 krb5_cc_close(context, id);
379 krb5_cc_cache_end_seq_get(context, cursor);
382 static void
383 test_copy(krb5_context context, const char *fromtype, const char *totype)
385 const krb5_cc_ops *from, *to;
386 krb5_ccache fromid, toid;
387 krb5_error_code ret;
388 krb5_principal p, p2;
390 from = krb5_cc_get_prefix_ops(context, fromtype);
391 if (from == NULL)
392 krb5_errx(context, 1, "%s isn't a type", fromtype);
394 to = krb5_cc_get_prefix_ops(context, totype);
395 if (to == NULL)
396 krb5_errx(context, 1, "%s isn't a type", totype);
398 ret = krb5_parse_name(context, "lha@SU.SE", &p);
399 if (ret)
400 krb5_err(context, 1, ret, "krb5_parse_name");
402 ret = krb5_cc_gen_new(context, from, &fromid);
403 if (ret)
404 krb5_err(context, 1, ret, "krb5_cc_gen_new");
406 ret = krb5_cc_initialize(context, fromid, p);
407 if (ret)
408 krb5_err(context, 1, ret, "krb5_cc_initialize");
410 ret = krb5_cc_gen_new(context, to, &toid);
411 if (ret)
412 krb5_err(context, 1, ret, "krb5_cc_gen_new");
414 ret = krb5_cc_copy_cache(context, fromid, toid);
415 if (ret)
416 krb5_err(context, 1, ret, "krb5_cc_copy_cache");
418 ret = krb5_cc_get_principal(context, toid, &p2);
419 if (ret)
420 krb5_err(context, 1, ret, "krb5_cc_get_principal");
422 if (krb5_principal_compare(context, p, p2) == FALSE)
423 krb5_errx(context, 1, "p != p2");
425 krb5_free_principal(context, p);
426 krb5_free_principal(context, p2);
428 krb5_cc_destroy(context, fromid);
429 krb5_cc_destroy(context, toid);
432 static void
433 test_prefix_ops(krb5_context context, const char *name, const krb5_cc_ops *ops)
435 const krb5_cc_ops *o;
437 o = krb5_cc_get_prefix_ops(context, name);
438 if (o == NULL)
439 krb5_errx(context, 1, "found no match for prefix '%s'", name);
440 if (strcmp(o->prefix, ops->prefix) != 0)
441 krb5_errx(context, 1, "ops for prefix '%s' is not "
442 "the expected %s != %s", name, o->prefix, ops->prefix);
446 static struct getargs args[] = {
447 {"debug", 'd', arg_flag, &debug_flag,
448 "turn on debuggin", NULL },
449 {"version", 0, arg_flag, &version_flag,
450 "print version", NULL },
451 {"help", 0, arg_flag, &help_flag,
452 NULL, NULL }
455 static void
456 usage (int ret)
458 arg_printusage (args, sizeof(args)/sizeof(*args), NULL, "hostname ...");
459 exit (ret);
463 main(int argc, char **argv)
465 krb5_context context;
466 krb5_error_code ret;
467 int optidx = 0;
468 krb5_ccache id1, id2;
470 setprogname(argv[0]);
472 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
473 usage(1);
475 if (help_flag)
476 usage (0);
478 if(version_flag){
479 print_version(NULL);
480 exit(0);
483 argc -= optidx;
484 argv += optidx;
486 ret = krb5_init_context(&context);
487 if (ret)
488 errx (1, "krb5_init_context failed: %d", ret);
490 test_fcache_remove(context);
491 test_default_name(context);
492 test_mcache(context);
493 test_init_vs_destroy(context, &krb5_mcc_ops);
494 test_init_vs_destroy(context, &krb5_fcc_ops);
495 test_mcc_default();
496 test_def_cc_name(context);
497 test_cache_iter(context, "MEMORY", 0);
499 krb5_principal p;
500 krb5_cc_new_unique(context, "MEMORY", "bar", &id1);
501 krb5_cc_new_unique(context, "MEMORY", "baz", &id2);
502 krb5_parse_name(context, "lha@SU.SE", &p);
503 krb5_cc_initialize(context, id1, p);
504 krb5_free_principal(context, p);
507 test_cache_find(context, "MEMORY", "lha@SU.SE", 1);
508 test_cache_find(context, "MEMORY", "hulabundulahotentot@SU.SE", 0);
510 test_cache_iter(context, "MEMORY", 0);
511 test_cache_iter(context, "MEMORY", 1);
512 test_cache_iter(context, "MEMORY", 0);
513 test_cache_iter(context, "FILE", 0);
514 test_cache_iter(context, "API", 0);
516 test_copy(context, "FILE", "FILE");
517 test_copy(context, "MEMORY", "MEMORY");
518 test_copy(context, "FILE", "MEMORY");
519 test_copy(context, "MEMORY", "FILE");
521 test_prefix_ops(context, "FILE:/tmp/foo", &krb5_fcc_ops);
522 test_prefix_ops(context, "FILE", &krb5_fcc_ops);
523 test_prefix_ops(context, "MEMORY", &krb5_mcc_ops);
524 test_prefix_ops(context, "MEMORY:foo", &krb5_mcc_ops);
525 test_prefix_ops(context, "/tmp/kaka", &krb5_fcc_ops);
527 krb5_cc_destroy(context, id1);
528 krb5_cc_destroy(context, id2);
530 krb5_free_context(context);
532 return 0;