1*afab4e30Schristos /* $NetBSD: kswitch.c,v 1.3 2023/06/19 21:41:42 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 2008 - 2010 Kungliga Tekniska Högskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric * modification, are permitted provided that the following conditions
10ca1c9b0cSelric * are met:
11ca1c9b0cSelric *
12ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric *
15ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric *
19ca1c9b0cSelric * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric * may be used to endorse or promote products derived from this software
21ca1c9b0cSelric * without specific prior written permission.
22ca1c9b0cSelric *
23ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric * SUCH DAMAGE.
34ca1c9b0cSelric */
35ca1c9b0cSelric
36ca1c9b0cSelric #include "kuser_locl.h"
37b9d004c6Schristos #include "heimtools-commands.h"
38ca1c9b0cSelric
39ca1c9b0cSelric #ifdef HAVE_READLINE
404f77a458Spettai char *readline(const char *prompt);
41ca1c9b0cSelric #else
42ca1c9b0cSelric
43ca1c9b0cSelric static char *
readline(const char * prompt)444f77a458Spettai readline(const char *prompt)
45ca1c9b0cSelric {
46ca1c9b0cSelric char buf[BUFSIZ];
47ca1c9b0cSelric printf ("%s", prompt);
48ca1c9b0cSelric fflush (stdout);
49ca1c9b0cSelric if(fgets(buf, sizeof(buf), stdin) == NULL)
50ca1c9b0cSelric return NULL;
51ca1c9b0cSelric buf[strcspn(buf, "\r\n")] = '\0';
52ca1c9b0cSelric return strdup(buf);
53ca1c9b0cSelric }
54ca1c9b0cSelric
55ca1c9b0cSelric #endif
56ca1c9b0cSelric
57ca1c9b0cSelric /*
58ca1c9b0cSelric *
59ca1c9b0cSelric */
60ca1c9b0cSelric
61ca1c9b0cSelric int
kswitch(struct kswitch_options * opt,int argc,char ** argv)62ca1c9b0cSelric kswitch(struct kswitch_options *opt, int argc, char **argv)
63ca1c9b0cSelric {
64ca1c9b0cSelric krb5_error_code ret;
65ca1c9b0cSelric krb5_ccache id = NULL;
66ca1c9b0cSelric
67ca1c9b0cSelric if (opt->cache_string && opt->principal_string)
68b9d004c6Schristos krb5_errx(heimtools_context, 1,
69ca1c9b0cSelric N_("Both --cache and --principal given, choose one", ""));
70ca1c9b0cSelric
71ca1c9b0cSelric if (opt->interactive_flag) {
72ca1c9b0cSelric krb5_cc_cache_cursor cursor;
73ca1c9b0cSelric krb5_ccache *ids = NULL;
74ca1c9b0cSelric size_t i, len = 0;
75ca1c9b0cSelric char *name;
76ca1c9b0cSelric rtbl_t ct;
77ca1c9b0cSelric
78ca1c9b0cSelric ct = rtbl_create();
79ca1c9b0cSelric
804f77a458Spettai rtbl_add_column_by_id(ct, 0, "#", 0);
814f77a458Spettai rtbl_add_column_by_id(ct, 1, "Principal", 0);
824f77a458Spettai rtbl_set_column_affix_by_id(ct, 1, " ", "");
834f77a458Spettai rtbl_add_column_by_id(ct, 2, "Type", 0);
844f77a458Spettai rtbl_set_column_affix_by_id(ct, 2, " ", "");
85ca1c9b0cSelric
86b9d004c6Schristos ret = krb5_cc_cache_get_first(heimtools_context, NULL, &cursor);
87ca1c9b0cSelric if (ret)
88b9d004c6Schristos krb5_err(heimtools_context, 1, ret, "krb5_cc_cache_get_first");
89ca1c9b0cSelric
90b9d004c6Schristos while (krb5_cc_cache_next(heimtools_context, cursor, &id) == 0) {
91*afab4e30Schristos krb5_principal p = NULL;
92ca1c9b0cSelric char num[10];
93ca1c9b0cSelric
94b9d004c6Schristos ret = krb5_cc_get_principal(heimtools_context, id, &p);
95*afab4e30Schristos if (ret == 0)
96*afab4e30Schristos ret = krb5_unparse_name(heimtools_context, p, &name);
97b9d004c6Schristos if (ret) {
98b9d004c6Schristos krb5_cc_close(heimtools_context, id);
99ca1c9b0cSelric continue;
100b9d004c6Schristos }
101ca1c9b0cSelric
102b9d004c6Schristos krb5_free_principal(heimtools_context, p);
103ca1c9b0cSelric
104ca1c9b0cSelric snprintf(num, sizeof(num), "%d", (int)(len + 1));
1054f77a458Spettai rtbl_add_column_entry_by_id(ct, 0, num);
1064f77a458Spettai rtbl_add_column_entry_by_id(ct, 1, name);
107b9d004c6Schristos rtbl_add_column_entry_by_id(ct, 2, krb5_cc_get_type(heimtools_context, id));
108ca1c9b0cSelric free(name);
109ca1c9b0cSelric
110ca1c9b0cSelric ids = erealloc(ids, (len + 1) * sizeof(ids[0]));
111ca1c9b0cSelric ids[len] = id;
112ca1c9b0cSelric len++;
113ca1c9b0cSelric }
114b9d004c6Schristos krb5_cc_cache_end_seq_get(heimtools_context, cursor);
115ca1c9b0cSelric
116ca1c9b0cSelric rtbl_format(ct, stdout);
117ca1c9b0cSelric rtbl_destroy(ct);
118ca1c9b0cSelric
119ca1c9b0cSelric name = readline("Select number: ");
120ca1c9b0cSelric if (name) {
121ca1c9b0cSelric i = atoi(name);
122ca1c9b0cSelric if (i == 0)
123b9d004c6Schristos krb5_errx(heimtools_context, 1, "Cache number '%s' is invalid", name);
124ca1c9b0cSelric if (i > len)
125b9d004c6Schristos krb5_errx(heimtools_context, 1, "Cache number '%s' is too large", name);
126ca1c9b0cSelric
127ca1c9b0cSelric id = ids[i - 1];
128ca1c9b0cSelric ids[i - 1] = NULL;
129b9d004c6Schristos free(name);
130ca1c9b0cSelric } else
131b9d004c6Schristos krb5_errx(heimtools_context, 1, "No cache selected");
132ca1c9b0cSelric for (i = 0; i < len; i++)
133ca1c9b0cSelric if (ids[i])
134b9d004c6Schristos krb5_cc_close(heimtools_context, ids[i]);
135b9d004c6Schristos free(ids);
136ca1c9b0cSelric } else if (opt->principal_string) {
137ca1c9b0cSelric krb5_principal p;
138ca1c9b0cSelric
139b9d004c6Schristos ret = krb5_parse_name(heimtools_context, opt->principal_string, &p);
140ca1c9b0cSelric if (ret)
141b9d004c6Schristos krb5_err(heimtools_context, 1, ret, "krb5_parse_name: %s",
142ca1c9b0cSelric opt->principal_string);
143ca1c9b0cSelric
144b9d004c6Schristos ret = krb5_cc_cache_match(heimtools_context, p, &id);
145ca1c9b0cSelric if (ret)
146b9d004c6Schristos krb5_err(heimtools_context, 1, ret,
147ca1c9b0cSelric N_("Did not find principal: %s", ""),
148ca1c9b0cSelric opt->principal_string);
149ca1c9b0cSelric
150b9d004c6Schristos krb5_free_principal(heimtools_context, p);
151ca1c9b0cSelric
152ca1c9b0cSelric } else if (opt->cache_string) {
153ca1c9b0cSelric const krb5_cc_ops *ops;
154ca1c9b0cSelric char *str;
155b9d004c6Schristos int aret;
156ca1c9b0cSelric
157b9d004c6Schristos ops = krb5_cc_get_prefix_ops(heimtools_context, opt->type_string);
158ca1c9b0cSelric if (ops == NULL)
159b9d004c6Schristos krb5_err(heimtools_context, 1, 0, "krb5_cc_get_prefix_ops");
160ca1c9b0cSelric
161b9d004c6Schristos aret = asprintf(&str, "%s:%s", ops->prefix, opt->cache_string);
162b9d004c6Schristos if (aret == -1)
163b9d004c6Schristos krb5_errx(heimtools_context, 1, N_("out of memory", ""));
164ca1c9b0cSelric
165b9d004c6Schristos ret = krb5_cc_resolve(heimtools_context, str, &id);
166ca1c9b0cSelric if (ret)
167b9d004c6Schristos krb5_err(heimtools_context, 1, ret, "krb5_cc_resolve: %s", str);
168ca1c9b0cSelric
169ca1c9b0cSelric free(str);
170ca1c9b0cSelric } else {
171b9d004c6Schristos krb5_errx(heimtools_context, 1, "missing option for kswitch");
172ca1c9b0cSelric }
173ca1c9b0cSelric
174b9d004c6Schristos ret = krb5_cc_switch(heimtools_context, id);
175ca1c9b0cSelric if (ret)
176b9d004c6Schristos krb5_err(heimtools_context, 1, ret, "krb5_cc_switch");
177b9d004c6Schristos
178b9d004c6Schristos krb5_cc_close(heimtools_context, id);
179ca1c9b0cSelric
180ca1c9b0cSelric return 0;
181ca1c9b0cSelric }
182