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