From 55e96fabf2923ac812eee3a39ff5af84241e8284 Mon Sep 17 00:00:00 2001 From: Love Hornquist Astrand Date: Thu, 25 Nov 2010 09:29:29 -0800 Subject: [PATCH] switch kswitch to kcc --- kuser/Makefile.am | 7 +-- kuser/kcc-commands.in | 29 +++++++++ kuser/kcc.c | 2 +- kuser/kswitch.c | 140 ++++++++++++----------------------------- tests/bin/setup-env.in | 2 +- 5 files changed, 74 insertions(+), 106 deletions(-) diff --git a/kuser/Makefile.am b/kuser/Makefile.am index 043f33caa..305f3779c 100644 --- a/kuser/Makefile.am +++ b/kuser/Makefile.am @@ -15,7 +15,7 @@ man_MANS = \ kgetcred.1 \ kimpersonate.8 -bin_PROGRAMS = kinit kdestroy kgetcred kswitch kcc +bin_PROGRAMS = kinit kdestroy kgetcred kcc libexec_PROGRAMS = kdigest kimpersonate noinst_PROGRAMS = kverify kdecode_ticket generate-requests copy_cred_cache @@ -33,13 +33,11 @@ kdestroy_LDADD = $(kinit_LDADD) kimpersonate_LDADD = $(kinit_LDADD) -kswitch_LDADD = $(kinit_LDADD) $(LIB_readline) - kcc_LDADD = \ $(top_builddir)/lib/sl/libsl.la \ $(kinit_LDADD) -dist_kcc_SOURCES = kcc.c klist.c +dist_kcc_SOURCES = kcc.c klist.c kswitch.c nodist_kcc_SOURCES = kcc-commands.c $(kcc_OBJECTS): kcc-commands.h @@ -79,4 +77,5 @@ EXTRA_DIST = $(man_MANS) \ # make sure install-exec-hook doesn't have any commands in Makefile.am.common install-exec-hook: (cd $(DESTDIR)$(bindir) && rm -f klist && $(LN_S) kcc klist) + (cd $(DESTDIR)$(bindir) && rm -f kswitch && $(LN_S) kcc kswitch) diff --git a/kuser/kcc-commands.in b/kuser/kcc-commands.in index 5e2b3e2c6..7b849cfa2 100644 --- a/kuser/kcc-commands.in +++ b/kuser/kcc-commands.in @@ -123,6 +123,35 @@ command = { help = "Credentials cache" } } +command = { + name = "kswitch" + name = "switch" + help = "Switch default kerberos cache" + option = { + long = "type" + short = "t" + type = "string" + help = "type of credential cache" + } + option = { + long = "cache" + short = "c" + type = "string" + help = "name of credential cache" + } + option = { + long = "principal" + short = "p" + type = "string" + help = "name of principal" + } + option = { + long = "interactive" + short = "i" + type = "flag" + help = "interactive selection" + } +}; command = { name = "kvno" help = "Acquire a Kerberos ticket" diff --git a/kuser/kcc.c b/kuser/kcc.c index fde4d4c10..9a392fc1c 100644 --- a/kuser/kcc.c +++ b/kuser/kcc.c @@ -89,7 +89,7 @@ static int command_alias(const char *name) { const char *aliases[] = { - "kinit", "klist", "kgetcred", "kdeltkt", + "kinit", "klist", "kswitch", "kgetcred", "kvno", "kdeltkt", "kdestroy", "kcpytkt", NULL }, **p = aliases; diff --git a/kuser/kswitch.c b/kuser/kswitch.c index c53676d41..45305f07f 100644 --- a/kuser/kswitch.c +++ b/kuser/kswitch.c @@ -32,6 +32,7 @@ */ #include "kuser_locl.h" +#include "kcc-commands.h" #ifdef HAVE_READLINE char *readline(char *prompt); @@ -41,105 +42,43 @@ char *readline(char *prompt); * */ -static int version_flag = 0; -static int help_flag = 0; -static char *cache; -static char *principal; -static char *type; -static int interactive_flag; - -static struct getargs args[] = { - { "type", 't', arg_string, &type, - NP_("type of credential cache", ""), "type" }, - { "cache", 'c', arg_string, &cache, - NP_("name of credential cache", ""), "cache" }, - { "principal", 'p', arg_string, &principal, - NP_("name of principal", ""), "principal" }, - { "interactive", 'i', arg_flag, &interactive_flag, - NP_("interactive selection", ""), NULL }, - { "version", 0, arg_flag, &version_flag, - NP_("print version", ""), NULL }, - { "help", 0, arg_flag, &help_flag, NULL, NULL} -}; - -static void -usage (int ret) __attribute__((noreturn)); - -static void -usage (int ret) -{ - arg_printusage (args, sizeof(args)/sizeof(*args), NULL, ""); - exit (ret); -} - int -main (int argc, char **argv) +kswitch(struct kswitch_options *opt, int argc, char **argv) { - krb5_context context; krb5_error_code ret; krb5_ccache id = NULL; - int optidx = 0; - setprogname (argv[0]); - - setlocale (LC_ALL, ""); - bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR); - textdomain("heimdal_kuser"); - - ret = krb5_init_context (&context); - if (ret == KRB5_CONFIG_BADFORMAT) - errx (1, "krb5_init_context failed to parse configuration file"); - else if (ret) - errx(1, "krb5_init_context failed: %d", ret); - - if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx)) - usage(1); - - if (help_flag) - usage (0); - - if(version_flag){ - print_version(NULL); - exit(0); - } - - argc -= optidx; - argv += optidx; - - if (argc != 0) - usage (1); - - if (cache && principal) - krb5_errx(context, 1, + if (opt->cache_string && opt->principal_string) + krb5_errx(kcc_context, 1, N_("Both --cache and --principal given, choose one", "")); - if (interactive_flag) { + if (opt->interactive_flag) { krb5_cc_cache_cursor cursor; - krb5_ccache *ids; + krb5_ccache *ids = NULL; size_t i, len = 0; char *name; rtbl_t ct; ct = rtbl_create(); - rtbl_add_column (ct, "", 0); - rtbl_add_column (ct, "Principal", 0); + rtbl_add_column(ct, "", 0); + rtbl_add_column(ct, "Principal", 0); rtbl_set_column_prefix(ct, "Principal", " "); - ret = krb5_cc_cache_get_first (context, NULL, &cursor); + ret = krb5_cc_cache_get_first(kcc_context, NULL, &cursor); if (ret) - krb5_err (context, 1, ret, "krb5_cc_cache_get_first"); + krb5_err(kcc_context, 1, ret, "krb5_cc_cache_get_first"); - while (krb5_cc_cache_next (context, cursor, &id) == 0) { + while (krb5_cc_cache_next(kcc_context, cursor, &id) == 0) { krb5_principal p; char num[10]; - ret = krb5_cc_get_principal(context, id, &p); + ret = krb5_cc_get_principal(kcc_context, id, &p); if (ret) continue; - ret = krb5_unparse_name(context, p, &name); - krb5_free_principal(context, p); + ret = krb5_unparse_name(kcc_context, p, &name); + krb5_free_principal(kcc_context, p); snprintf(num, sizeof(num), "%d", (int)(len + 1)); rtbl_add_column_entry(ct, "", num); @@ -150,7 +89,7 @@ main (int argc, char **argv) ids[len] = id; len++; } - krb5_cc_cache_end_seq_get(context, cursor); + krb5_cc_cache_end_seq_get(kcc_context, cursor); rtbl_format(ct, stdout); rtbl_destroy(ct); @@ -159,57 +98,58 @@ main (int argc, char **argv) if (name) { i = atoi(name); if (i == 0) - krb5_errx(context, 1, "Cache number '%s' is invalid", name); + krb5_errx(kcc_context, 1, "Cache number '%s' is invalid", name); if (i > len) - krb5_errx(context, 1, "Cache number '%s' is too large", name); + krb5_errx(kcc_context, 1, "Cache number '%s' is too large", name); id = ids[i - 1]; ids[i - 1] = NULL; } else - krb5_errx(context, 1, "No cache selected"); + krb5_errx(kcc_context, 1, "No cache selected"); for (i = 0; i < len; i++) if (ids[i]) - krb5_cc_close(context, ids[i]); + krb5_cc_close(kcc_context, ids[i]); - } else if (principal) { + } else if (opt->principal_string) { krb5_principal p; - ret = krb5_parse_name(context, principal, &p); + ret = krb5_parse_name(kcc_context, opt->principal_string, &p); if (ret) - krb5_err (context, 1, ret, "krb5_parse_name: %s", principal); + krb5_err(kcc_context, 1, ret, "krb5_parse_name: %s", + opt->principal_string); - ret = krb5_cc_cache_match(context, p, &id); + ret = krb5_cc_cache_match(kcc_context, p, &id); if (ret) - krb5_err (context, 1, ret, - N_("Did not find principal: %s", ""), principal); + krb5_err(kcc_context, 1, ret, + N_("Did not find principal: %s", ""), + opt->principal_string); - krb5_free_principal(context, p); + krb5_free_principal(kcc_context, p); - } else if (cache) { + } else if (opt->cache_string) { const krb5_cc_ops *ops; char *str; - ops = krb5_cc_get_prefix_ops(context, type); + ops = krb5_cc_get_prefix_ops(kcc_context, opt->type_string); if (ops == NULL) - krb5_err (context, 1, 0, "krb5_cc_get_prefix_ops"); + krb5_err(kcc_context, 1, 0, "krb5_cc_get_prefix_ops"); - asprintf(&str, "%s:%s", ops->prefix, cache); + asprintf(&str, "%s:%s", ops->prefix, opt->cache_string); if (str == NULL) - krb5_errx(context, 1, N_("out of memory", "")); + krb5_errx(kcc_context, 1, N_("out of memory", "")); - ret = krb5_cc_resolve(context, str, &id); + ret = krb5_cc_resolve(kcc_context, str, &id); if (ret) - krb5_err (context, 1, ret, "krb5_cc_resolve: %s", str); + krb5_err(kcc_context, 1, ret, "krb5_cc_resolve: %s", str); free(str); - } else - usage(1); + } else { + krb5_errx(kcc_context, 1, "missing option for kswitch"); + } - ret = krb5_cc_switch(context, id); + ret = krb5_cc_switch(kcc_context, id); if (ret) - krb5_err (context, 1, ret, "krb5_cc_switch"); - - krb5_cc_close(context, id); + krb5_err(kcc_context, 1, ret, "krb5_cc_switch"); return 0; } diff --git a/tests/bin/setup-env.in b/tests/bin/setup-env.in index b76e3e83c..99335819e 100644 --- a/tests/bin/setup-env.in +++ b/tests/bin/setup-env.in @@ -27,7 +27,7 @@ kinit="${TESTS_ENVIRONMENT} ${top_builddir}/kuser/kinit" klist="${TESTS_ENVIRONMENT} ${top_builddir}/kuser/kcc klist" kpasswd="${TESTS_ENVIRONMENT} ${top_builddir}/kpasswd/kpasswd" kpasswdd="${TESTS_ENVIRONMENT} ${top_builddir}/kpasswd/kpasswdd" -kswitch="${TESTS_ENVIRONMENT} ${top_builddir}/kuser/kswitch" +kswitch="${TESTS_ENVIRONMENT} ${top_builddir}/kuser/kcc kswitch" ktutil="${TESTS_ENVIRONMENT} ${top_builddir}/admin/ktutil" # regression test tools