Path: blob/main/crypto/krb5/src/clients/kswitch/kswitch.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* clients/kswitch/kswitch.c - Switch primary credential cache */2/*3* Copyright 2011 by the Massachusetts Institute of Technology.4* All Rights Reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526#include "k5-int.h"27#include <locale.h>2829#ifndef _WIN3230#define GET_PROGNAME(x) (strrchr((x), '/') ? strrchr((x), '/')+1 : (x))31#else32#define GET_PROGNAME(x) max(max(strrchr((x), '/'), strrchr((x), '\\')) + 1,(x))33#endif3435static char *progname;3637static void38usage(void)39{40fprintf(stderr, _("Usage: %s {-c cache_name | -p principal}\n"), progname);41fprintf(stderr, _("\t-c specify name of credentials cache\n"));42fprintf(stderr, _("\t-p specify name of principal\n"));43exit(2);44}4546int47main(int argc, char **argv)48{49krb5_context context;50krb5_error_code ret;51int c;52krb5_ccache cache = NULL;53krb5_principal princ = NULL;54const char *cache_name = NULL, *princ_name = NULL;55krb5_boolean errflag = FALSE;5657setlocale(LC_ALL, "");58progname = GET_PROGNAME(argv[0]);5960while ((c = getopt(argc, argv, "c:p:")) != -1) {61switch (c) {62case 'c':63case 'p':64if (cache_name || princ_name) {65fprintf(stderr, _("Only one -c or -p option allowed\n"));66errflag = TRUE;67} else if (c == 'c') {68cache_name = optarg;69} else {70princ_name = optarg;71}72break;73case '?':74default:75errflag = TRUE;76break;77}78}7980if (optind != argc)81errflag = TRUE;8283if (!cache_name && !princ_name) {84fprintf(stderr, _("One of -c or -p must be specified\n"));85errflag = TRUE;86}8788if (errflag)89usage();9091ret = krb5_init_context(&context);92if (ret) {93com_err(progname, ret, _("while initializing krb5"));94exit(1);95}9697if (cache_name) {98ret = krb5_cc_resolve(context, cache_name, &cache);99if (ret != 0) {100com_err(progname, ret, _("while resolving %s"), cache_name);101exit(1);102}103} else {104ret = krb5_parse_name(context, princ_name, &princ);105if (ret) {106com_err(progname, ret, _("while parsing principal name %s"),107princ_name);108exit(1);109}110ret = krb5_cc_cache_match(context, princ, &cache);111if (ret) {112com_err(progname, ret, _("while searching for ccache for %s"),113princ_name);114exit(1);115}116krb5_free_principal(context, princ);117}118119ret = krb5_cc_switch(context, cache);120if (ret != 0) {121com_err(progname, ret, _("while switching to credential cache"));122exit(1);123}124125krb5_cc_close(context, cache);126krb5_free_context(context);127return 0;128}129130131