1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* clients/kswitch/kswitch.c - Switch primary credential cache */
4 * Copyright 2011 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
34 #define GET_PROGNAME(x) (strrchr((x), '/') ? strrchr((x), '/')+1 : (x))
36 #define GET_PROGNAME(x) max(max(strrchr((x), '/'), strrchr((x), '\\')) + 1,(x))
39 static char *progname;
44 fprintf(stderr, _("Usage: %s {-c cache_name | -p principal}\n"), progname);
45 fprintf(stderr, _("\t-c specify name of credentials cache\n"));
46 fprintf(stderr, _("\t-p specify name of principal\n"));
51 main(int argc, char **argv)
56 krb5_ccache cache = NULL;
57 krb5_principal princ = NULL;
58 const char *cache_name = NULL, *princ_name = NULL;
59 krb5_boolean errflag = FALSE;
61 setlocale(LC_ALL, "");
62 progname = GET_PROGNAME(argv[0]);
64 while ((c = getopt(argc, argv, "c:p:")) != -1) {
68 if (cache_name || princ_name) {
69 fprintf(stderr, _("Only one -c or -p option allowed\n"));
71 } else if (c == 'c') {
87 if (!cache_name && !princ_name) {
88 fprintf(stderr, _("One of -c or -p must be specified\n"));
95 ret = krb5_init_context(&context);
97 com_err(progname, ret, _("while initializing krb5"));
102 ret = krb5_cc_resolve(context, cache_name, &cache);
104 com_err(progname, ret, _("while resolving %s"), cache_name);
108 ret = krb5_parse_name(context, princ_name, &princ);
110 com_err(progname, ret, _("while parsing principal name %s"),
114 ret = krb5_cc_cache_match(context, princ, &cache);
116 com_err(progname, ret, _("while searching for ccache for %s"),
120 krb5_free_principal(context, princ);
123 ret = krb5_cc_switch(context, cache);
125 com_err(progname, ret, _("while switching to credential cache"));
129 krb5_cc_close(context, cache);
130 krb5_free_context(context);