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