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