1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* clients/kswitch/kswitch.c - Switch primary credential cache */ 3 /* 4 * Copyright 2011 by the Massachusetts Institute of Technology. 5 * All Rights Reserved. 6 * 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. 11 * 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. 25 */ 26 27 #include "k5-int.h" 28 #include <locale.h> 29 30 #ifndef _WIN32 31 #define GET_PROGNAME(x) (strrchr((x), '/') ? strrchr((x), '/')+1 : (x)) 32 #else 33 #define GET_PROGNAME(x) max(max(strrchr((x), '/'), strrchr((x), '\\')) + 1,(x)) 34 #endif 35 36 static char *progname; 37 38 static void 39 usage(void) 40 { 41 fprintf(stderr, _("Usage: %s {-c cache_name | -p principal}\n"), progname); 42 fprintf(stderr, _("\t-c specify name of credentials cache\n")); 43 fprintf(stderr, _("\t-p specify name of principal\n")); 44 exit(2); 45 } 46 47 int 48 main(int argc, char **argv) 49 { 50 krb5_context context; 51 krb5_error_code ret; 52 int c; 53 krb5_ccache cache = NULL; 54 krb5_principal princ = NULL; 55 const char *cache_name = NULL, *princ_name = NULL; 56 krb5_boolean errflag = FALSE; 57 58 setlocale(LC_ALL, ""); 59 progname = GET_PROGNAME(argv[0]); 60 61 while ((c = getopt(argc, argv, "c:p:")) != -1) { 62 switch (c) { 63 case 'c': 64 case 'p': 65 if (cache_name || princ_name) { 66 fprintf(stderr, _("Only one -c or -p option allowed\n")); 67 errflag = TRUE; 68 } else if (c == 'c') { 69 cache_name = optarg; 70 } else { 71 princ_name = optarg; 72 } 73 break; 74 case '?': 75 default: 76 errflag = TRUE; 77 break; 78 } 79 } 80 81 if (optind != argc) 82 errflag = TRUE; 83 84 if (!cache_name && !princ_name) { 85 fprintf(stderr, _("One of -c or -p must be specified\n")); 86 errflag = TRUE; 87 } 88 89 if (errflag) 90 usage(); 91 92 ret = krb5_init_context(&context); 93 if (ret) { 94 com_err(progname, ret, _("while initializing krb5")); 95 exit(1); 96 } 97 98 if (cache_name) { 99 ret = krb5_cc_resolve(context, cache_name, &cache); 100 if (ret != 0) { 101 com_err(progname, ret, _("while resolving %s"), cache_name); 102 exit(1); 103 } 104 } else { 105 ret = krb5_parse_name(context, princ_name, &princ); 106 if (ret) { 107 com_err(progname, ret, _("while parsing principal name %s"), 108 princ_name); 109 exit(1); 110 } 111 ret = krb5_cc_cache_match(context, princ, &cache); 112 if (ret) { 113 com_err(progname, ret, _("while searching for ccache for %s"), 114 princ_name); 115 exit(1); 116 } 117 krb5_free_principal(context, princ); 118 } 119 120 ret = krb5_cc_switch(context, cache); 121 if (ret != 0) { 122 com_err(progname, ret, _("while switching to credential cache")); 123 exit(1); 124 } 125 126 krb5_cc_close(context, cache); 127 krb5_free_context(context); 128 return 0; 129 } 130