1 /* $NetBSD: kswitch.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
4 * Copyright (c) 2008 - 2010 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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
36 #include "kuser_locl.h"
37 #include "kcc-commands.h"
40 char *readline(const char *prompt
);
44 readline(const char *prompt
)
47 printf ("%s", prompt
);
49 if(fgets(buf
, sizeof(buf
), stdin
) == NULL
)
51 buf
[strcspn(buf
, "\r\n")] = '\0';
62 kswitch(struct kswitch_options
*opt
, int argc
, char **argv
)
65 krb5_ccache id
= NULL
;
67 if (opt
->cache_string
&& opt
->principal_string
)
68 krb5_errx(kcc_context
, 1,
69 N_("Both --cache and --principal given, choose one", ""));
71 if (opt
->interactive_flag
) {
72 krb5_cc_cache_cursor cursor
;
73 krb5_ccache
*ids
= NULL
;
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, " ", "");
86 ret
= krb5_cc_cache_get_first(kcc_context
, NULL
, &cursor
);
88 krb5_err(kcc_context
, 1, ret
, "krb5_cc_cache_get_first");
90 while (krb5_cc_cache_next(kcc_context
, cursor
, &id
) == 0) {
94 ret
= krb5_cc_get_principal(kcc_context
, id
, &p
);
98 ret
= krb5_unparse_name(kcc_context
, p
, &name
);
99 krb5_free_principal(kcc_context
, p
);
101 snprintf(num
, sizeof(num
), "%d", (int)(len
+ 1));
102 rtbl_add_column_entry_by_id(ct
, 0, num
);
103 rtbl_add_column_entry_by_id(ct
, 1, name
);
104 rtbl_add_column_entry_by_id(ct
, 2, krb5_cc_get_type(kcc_context
, id
));
107 ids
= erealloc(ids
, (len
+ 1) * sizeof(ids
[0]));
111 krb5_cc_cache_end_seq_get(kcc_context
, cursor
);
113 rtbl_format(ct
, stdout
);
116 name
= readline("Select number: ");
120 krb5_errx(kcc_context
, 1, "Cache number '%s' is invalid", name
);
122 krb5_errx(kcc_context
, 1, "Cache number '%s' is too large", name
);
127 krb5_errx(kcc_context
, 1, "No cache selected");
128 for (i
= 0; i
< len
; i
++)
130 krb5_cc_close(kcc_context
, ids
[i
]);
132 } else if (opt
->principal_string
) {
135 ret
= krb5_parse_name(kcc_context
, opt
->principal_string
, &p
);
137 krb5_err(kcc_context
, 1, ret
, "krb5_parse_name: %s",
138 opt
->principal_string
);
140 ret
= krb5_cc_cache_match(kcc_context
, p
, &id
);
142 krb5_err(kcc_context
, 1, ret
,
143 N_("Did not find principal: %s", ""),
144 opt
->principal_string
);
146 krb5_free_principal(kcc_context
, p
);
148 } else if (opt
->cache_string
) {
149 const krb5_cc_ops
*ops
;
152 ops
= krb5_cc_get_prefix_ops(kcc_context
, opt
->type_string
);
154 krb5_err(kcc_context
, 1, 0, "krb5_cc_get_prefix_ops");
156 asprintf(&str
, "%s:%s", ops
->prefix
, opt
->cache_string
);
158 krb5_errx(kcc_context
, 1, N_("out of memory", ""));
160 ret
= krb5_cc_resolve(kcc_context
, str
, &id
);
162 krb5_err(kcc_context
, 1, ret
, "krb5_cc_resolve: %s", str
);
166 krb5_errx(kcc_context
, 1, "missing option for kswitch");
169 ret
= krb5_cc_switch(kcc_context
, id
);
171 krb5_err(kcc_context
, 1, ret
, "krb5_cc_switch");