static void test_prefix_ops(krb5_context context, const char *name, const krb5_cc_ops *ops) { const krb5_cc_ops *o; o = krb5_cc_get_prefix_ops(context, name); if (o == NULL) krb5_errx(context, 1, "found no match for prefix '%s'", name); if (strcmp(o->prefix, ops->prefix) != 0) krb5_errx(context, 1, "ops for prefix '%s' is not " "the expected %s != %s", name, o->prefix, ops->prefix); }
static void test_move(krb5_context context, const char *type) { const krb5_cc_ops *ops; krb5_ccache fromid, toid; krb5_error_code ret; krb5_principal p, p2; ops = krb5_cc_get_prefix_ops(context, type); if (ops == NULL) return; ret = krb5_cc_new_unique(context, type, NULL, &fromid); if (ret == KRB5_CC_NOSUPP) return; else if (ret) krb5_err(context, 1, ret, "krb5_cc_new_unique"); ret = krb5_parse_name(context, "*****@*****.**", &p); if (ret) krb5_err(context, 1, ret, "krb5_parse_name"); ret = krb5_cc_initialize(context, fromid, p); if (ret) krb5_err(context, 1, ret, "krb5_cc_initialize"); ret = krb5_cc_new_unique(context, type, NULL, &toid); if (ret) krb5_err(context, 1, ret, "krb5_cc_new_unique"); ret = krb5_cc_initialize(context, toid, p); if (ret) krb5_err(context, 1, ret, "krb5_cc_initialize"); ret = krb5_cc_get_principal(context, toid, &p2); if (ret) krb5_err(context, 1, ret, "krb5_cc_get_principal"); if (krb5_principal_compare(context, p, p2) == FALSE) krb5_errx(context, 1, "p != p2"); krb5_free_principal(context, p); krb5_free_principal(context, p2); krb5_cc_destroy(context, toid); krb5_cc_destroy(context, fromid); }
int kswitch(struct kswitch_options *opt, int argc, char **argv) { krb5_error_code ret; krb5_ccache id = NULL; if (opt->cache_string && opt->principal_string) krb5_errx(kcc_context, 1, N_("Both --cache and --principal given, choose one", "")); if (opt->interactive_flag) { krb5_cc_cache_cursor cursor; krb5_ccache *ids = NULL; size_t i, len = 0; char *name; rtbl_t ct; ct = rtbl_create(); rtbl_add_column_by_id(ct, 0, "#", 0); rtbl_add_column_by_id(ct, 1, "Principal", 0); rtbl_set_column_affix_by_id(ct, 1, " ", ""); rtbl_add_column_by_id(ct, 2, "Type", 0); rtbl_set_column_affix_by_id(ct, 2, " ", ""); ret = krb5_cc_cache_get_first(kcc_context, NULL, &cursor); if (ret) krb5_err(kcc_context, 1, ret, "krb5_cc_cache_get_first"); while (krb5_cc_cache_next(kcc_context, cursor, &id) == 0) { krb5_principal p; char num[10]; ret = krb5_cc_get_principal(kcc_context, id, &p); if (ret) continue; 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_by_id(ct, 0, num); rtbl_add_column_entry_by_id(ct, 1, name); rtbl_add_column_entry_by_id(ct, 2, krb5_cc_get_type(kcc_context, id)); free(name); ids = erealloc(ids, (len + 1) * sizeof(ids[0])); ids[len] = id; len++; } krb5_cc_cache_end_seq_get(kcc_context, cursor); rtbl_format(ct, stdout); rtbl_destroy(ct); name = readline("Select number: "); if (name) { i = atoi(name); if (i == 0) krb5_errx(kcc_context, 1, "Cache number '%s' is invalid", name); if (i > len) krb5_errx(kcc_context, 1, "Cache number '%s' is too large", name); id = ids[i - 1]; ids[i - 1] = NULL; } else krb5_errx(kcc_context, 1, "No cache selected"); for (i = 0; i < len; i++) if (ids[i]) krb5_cc_close(kcc_context, ids[i]); } else if (opt->principal_string) { krb5_principal p; ret = krb5_parse_name(kcc_context, opt->principal_string, &p); if (ret) krb5_err(kcc_context, 1, ret, "krb5_parse_name: %s", opt->principal_string); ret = krb5_cc_cache_match(kcc_context, p, &id); if (ret) krb5_err(kcc_context, 1, ret, N_("Did not find principal: %s", ""), opt->principal_string); krb5_free_principal(kcc_context, p); } else if (opt->cache_string) { const krb5_cc_ops *ops; char *str; ops = krb5_cc_get_prefix_ops(kcc_context, opt->type_string); if (ops == NULL) krb5_err(kcc_context, 1, 0, "krb5_cc_get_prefix_ops"); asprintf(&str, "%s:%s", ops->prefix, opt->cache_string); if (str == NULL) krb5_errx(kcc_context, 1, N_("out of memory", "")); ret = krb5_cc_resolve(kcc_context, str, &id); if (ret) krb5_err(kcc_context, 1, ret, "krb5_cc_resolve: %s", str); free(str); } else { krb5_errx(kcc_context, 1, "missing option for kswitch"); } ret = krb5_cc_switch(kcc_context, id); if (ret) krb5_err(kcc_context, 1, ret, "krb5_cc_switch"); return 0; }
int main(int argc, char **argv) { int f; char tf[1024]; char *p; char *path; char **args; int i; int optind = 0; setprogname(argv[0]); if(getarg(getargs, num_args, argc, argv, &optind)) usage(1); if(help_flag) usage(0); if(version_flag) { print_version(NULL); exit(0); } argc -= optind; argv += optind; #ifdef KRB5 { const krb5_cc_ops *type; krb5_error_code ret; krb5_context context; krb5_ccache id; const char *name; ret = krb5_init_context(&context); if (ret) /* XXX should this really call exit ? */ errx(1, "no kerberos 5 support"); if (typename_arg == NULL) { char *s; name = krb5_cc_default_name(context); if (name == NULL) krb5_errx(context, 1, "Failed getting default " "credential cache type"); typename_arg = strdup(name); if (typename_arg == NULL) errx(1, "strdup"); s = strchr(typename_arg, ':'); if (s) *s = '\0'; } type = krb5_cc_get_prefix_ops(context, typename_arg); if (type == NULL) krb5_err(context, 1, ret, "Failed getting ops for %s " "credential cache", typename_arg); ret = krb5_cc_gen_new(context, type, &id); if (ret) krb5_err(context, 1, ret, "Failed generating credential cache"); name = krb5_cc_get_name(context, id); if (name == NULL) krb5_errx(context, 1, "Generated credential cache have no name"); snprintf(tf, sizeof(tf), "%s:%s", typename_arg, name); ret = krb5_cc_close(context, id); if (ret) krb5_err(context, 1, ret, "Failed closing credential cache"); krb5_free_context(context); esetenv("KRB5CCNAME", tf, 1); } #endif snprintf (tf, sizeof(tf), "%s_XXXXXX", TKT_ROOT); f = mkstemp (tf); if (f < 0) err(1, "mkstemp failed"); close (f); unlink (tf); esetenv("KRBTKFILE", tf, 1); i = 0; args = (char **) malloc((argc + 10)*sizeof(char *)); if (args == NULL) errx (1, "Out of memory allocating %lu bytes", (unsigned long)((argc + 10)*sizeof(char *))); if(*argv == NULL) { path = getenv("SHELL"); if(path == NULL){ struct passwd *pw = k_getpwuid(geteuid()); path = strdup(pw->pw_shell); } } else { path = strdup(*argv++); } if (path == NULL) errx (1, "Out of memory copying path"); p=strrchr(path, '/'); if(p) args[i] = strdup(p+1); else args[i] = strdup(path); if (args[i++] == NULL) errx (1, "Out of memory copying arguments"); while(*argv) args[i++] = *argv++; args[i++] = NULL; if(k_hasafs()) k_setpag(); unsetenv("PAGPID"); execvp(path, args); if (errno == ENOENT || c_flag) { char **sh_args = malloc ((i + 2) * sizeof(char *)); int j; if (sh_args == NULL) errx (1, "Out of memory copying sh arguments"); for (j = 1; j < i; ++j) sh_args[j + 2] = args[j]; sh_args[0] = "sh"; sh_args[1] = "-c"; sh_args[2] = path; execv ("/bin/sh", sh_args); } err (1, "execvp"); }
int main (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, N_("Both --cache and --principal given, choose one", "")); if (principal) { krb5_principal p; ret = krb5_parse_name(context, principal, &p); if (ret) krb5_err (context, 1, ret, "krb5_parse_name: %s", principal); ret = krb5_cc_cache_match(context, p, &id); if (ret) krb5_err (context, 1, ret, N_("Did not find principal: %s", ""), principal); krb5_free_principal(context, p); } else if (cache) { const krb5_cc_ops *ops; char *str; ops = krb5_cc_get_prefix_ops(context, type); if (ops == NULL) krb5_err (context, 1, 0, "krb5_cc_get_prefix_ops"); asprintf(&str, "%s:%s", ops->prefix, cache); if (str == NULL) krb5_errx(context, 1, N_("out of memory", "")); ret = krb5_cc_resolve(context, str, &id); if (ret) krb5_err (context, 1, ret, "krb5_cc_resolve: %s", str); free(str); } else usage(1); ret = krb5_cc_switch(context, id); if (ret) krb5_err (context, 1, ret, "krb5_cc_switch"); krb5_cc_close(context, id); return 0; }