base: set HEIM_JSON_F_NO_DATA_DICT in heim_audit_setkv_object()
[heimdal.git] / kuser / kgetcred.c
blob92eb770990c5c2e640e49cfdf0b8bb9d3b52012a
1 /*
2 * Copyright (c) 1997 - 2008 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"
36 static char *cache_str;
37 static char *out_cache_str;
38 static char *delegation_cred_str;
39 static char *etype_str;
40 static int transit_flag = 1;
41 static int forwardable_flag;
42 static int canonicalize_flag;
43 static int is_hostbased_flag;
44 static int is_canonical_flag;
45 static char *impersonate_str;
46 static char *nametype_str;
47 static int store_flag = 1;
48 static int cached_only_flag;
49 static int anonymous_flag;
50 static int debug_flag;
51 static int version_flag;
52 static int help_flag;
54 struct getargs args[] = {
55 { "cache", 'c', arg_string, &cache_str,
56 NP_("credential cache to use", ""), "cache"},
57 { "out-cache", 0, arg_string, &out_cache_str,
58 NP_("credential cache to store credential in", ""), "cache"},
59 { "delegation-credential-cache",0,arg_string, &delegation_cred_str,
60 NP_("where to find the ticket use for delegation", ""), "cache"},
61 { "canonicalize", 0, arg_flag, &canonicalize_flag,
62 NP_("canonicalize the principal (chase referrals)", ""), NULL },
63 { "canonical", 0, arg_flag, &is_canonical_flag,
64 NP_("the name components are canonical", ""), NULL },
65 { "forwardable", 0, arg_flag, &forwardable_flag,
66 NP_("forwardable ticket requested", ""), NULL},
67 { "transit-check", 0, arg_negative_flag, &transit_flag, NULL, NULL },
68 { "enctype", 'e', arg_string, &etype_str,
69 NP_("encryption type to use", ""), "enctype"},
70 { "impersonate", 0, arg_string, &impersonate_str,
71 NP_("client to impersonate", ""), "principal"},
72 { "name-type", 0, arg_string, &nametype_str,
73 NP_("Kerberos name type", ""), NULL },
74 { "hostbased", 'H', arg_flag, &is_hostbased_flag,
75 NP_("indicate that the name is a host-based service name", ""), NULL },
76 { "store", 0, arg_negative_flag, &store_flag,
77 NP_("don't store the tickets obtained in the cache", ""), NULL },
78 { "cached-only", 0, arg_flag, &cached_only_flag,
79 NP_("don't talk to the KDC, just search the cache", ""), NULL },
80 { "anonymous", 'n', arg_flag, &anonymous_flag,
81 NP_("request an anonymous ticket", ""), NULL },
82 { "debug", 0, arg_flag, &debug_flag, NULL, NULL },
83 { "version", 0, arg_flag, &version_flag, NULL, NULL },
84 { "help", 0, arg_flag, &help_flag, NULL, NULL }
87 static void
88 usage(int ret)
90 arg_printusage(args,
91 sizeof(args)/sizeof(*args),
92 NULL,
93 "service");
94 exit (ret);
97 int
98 main(int argc, char **argv)
100 krb5_error_code ret;
101 krb5_context context;
102 krb5_ccache cache;
103 krb5_creds *out;
104 int optidx = 0;
105 int32_t nametype = KRB5_NT_UNKNOWN;
106 krb5_get_creds_opt opt;
107 krb5_principal server = NULL;
108 krb5_principal impersonate;
110 setprogname(argv[0]);
112 ret = krb5_init_context(&context);
113 if (ret)
114 errx(1, "krb5_init_context failed: %d", ret);
116 if (getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
117 usage(1);
119 if (help_flag)
120 usage (0);
122 if (version_flag) {
123 print_version(NULL);
124 exit(0);
127 argc -= optidx;
128 argv += optidx;
130 if (debug_flag) {
131 ret = krb5_set_debug_dest(context, getprogname(), "STDERR");
132 if (ret)
133 krb5_warn(context, ret, "krb5_set_debug_dest");
136 if (cache_str) {
137 ret = krb5_cc_resolve(context, cache_str, &cache);
138 if (ret)
139 krb5_err(context, 1, ret, "%s", cache_str);
140 } else {
141 ret = krb5_cc_default (context, &cache);
142 if (ret)
143 krb5_err(context, 1, ret, "krb5_cc_resolve");
146 ret = krb5_get_creds_opt_alloc(context, &opt);
147 if (ret)
148 krb5_err(context, 1, ret, "krb5_get_creds_opt_alloc");
150 if (etype_str) {
151 krb5_enctype enctype;
153 ret = krb5_string_to_enctype(context, etype_str, &enctype);
154 if (ret)
155 krb5_errx(context, 1, N_("unrecognized enctype: %s", ""),
156 etype_str);
157 krb5_get_creds_opt_set_enctype(context, opt, enctype);
160 if (impersonate_str) {
161 ret = krb5_parse_name(context, impersonate_str, &impersonate);
162 if (ret)
163 krb5_err(context, 1, ret, "krb5_parse_name %s", impersonate_str);
164 krb5_get_creds_opt_set_impersonate(context, opt, impersonate);
165 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_STORE);
166 krb5_free_principal(context, impersonate);
169 if (out_cache_str)
170 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_STORE);
172 if (forwardable_flag)
173 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
174 if (!transit_flag)
175 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_TRANSIT_CHECK);
176 if (canonicalize_flag)
177 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_CANONICALIZE);
178 if (!store_flag)
179 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_STORE);
180 if (cached_only_flag)
181 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_CACHED);
182 if (anonymous_flag)
183 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_ANONYMOUS);
185 if (delegation_cred_str) {
186 krb5_ccache id;
187 krb5_creds c, mc;
188 Ticket ticket;
190 krb5_cc_clear_mcred(&mc);
191 ret = krb5_cc_get_principal(context, cache, &mc.server);
192 if (ret)
193 krb5_err(context, 1, ret, "krb5_cc_get_principal");
195 ret = krb5_cc_resolve(context, delegation_cred_str, &id);
196 if(ret)
197 krb5_err(context, 1, ret, "krb5_cc_resolve");
199 ret = krb5_cc_retrieve_cred(context, id, 0, &mc, &c);
200 if(ret)
201 krb5_err(context, 1, ret, "krb5_cc_retrieve_cred");
203 ret = decode_Ticket(c.ticket.data, c.ticket.length, &ticket, NULL);
204 if (ret) {
205 krb5_clear_error_message(context);
206 krb5_err(context, 1, ret, "decode_Ticket");
208 krb5_free_cred_contents(context, &c);
210 ret = krb5_get_creds_opt_set_ticket(context, opt, &ticket);
211 if(ret)
212 krb5_err(context, 1, ret, "krb5_get_creds_opt_set_ticket");
213 free_Ticket(&ticket);
215 krb5_cc_close(context, id);
216 krb5_free_principal(context, mc.server);
218 krb5_get_creds_opt_add_options(context, opt,
219 KRB5_GC_CONSTRAINED_DELEGATION);
222 if (nametype_str != NULL) {
223 ret = krb5_parse_nametype(context, nametype_str, &nametype);
224 if (ret)
225 krb5_err(context, 1, ret, "krb5_parse_nametype");
228 if (nametype == KRB5_NT_SRV_HST ||
229 nametype == KRB5_NT_SRV_HST_NEEDS_CANON)
230 is_hostbased_flag = 1;
232 if (is_hostbased_flag) {
233 const char *sname = NULL;
234 const char *hname = NULL;
236 if (nametype_str != NULL &&
237 nametype != KRB5_NT_SRV_HST &&
238 nametype != KRB5_NT_SRV_HST_NEEDS_CANON)
239 krb5_errx(context, 1, "--hostbased not compatible with "
240 "non-hostbased --name-type");
242 if (is_canonical_flag)
243 nametype = KRB5_NT_SRV_HST;
244 else
245 nametype = KRB5_NT_SRV_HST_NEEDS_CANON;
248 * Host-based service names can have more than one component.
250 * RFC5179 did not, but should have, assign a Kerberos name-type
251 * corresponding to GSS_C_NT_DOMAINBASED. But it's basically a
252 * host-based service name type with one additional component.
254 * So that's how we're treating host-based service names here:
255 * two or more components.
258 if (argc == 0) {
259 usage(1);
260 } else if (argc == 1) {
261 krb5_principal server2;
264 * In this case the one argument is a principal name, not the
265 * service name.
267 * We parse the argument as a principal name, extract the service
268 * and hostname components, use krb5_sname_to_principal(), then
269 * extract the service and hostname components from that.
272 ret = krb5_parse_name(context, argv[0], &server);
273 if (ret)
274 krb5_err(context, 1, ret, "krb5_parse_name %s", argv[0]);
275 sname = krb5_principal_get_comp_string(context, server, 0);
278 * If a single-component principal name is given, then we'll
279 * default the hostname, as krb5_principal_get_comp_string()
280 * returns NULL in this case.
282 hname = krb5_principal_get_comp_string(context, server, 1);
284 ret = krb5_sname_to_principal(context, hname, sname,
285 KRB5_NT_SRV_HST, &server2);
286 sname = krb5_principal_get_comp_string(context, server2, 0);
287 hname = krb5_principal_get_comp_string(context, server2, 1);
290 * Modify the original with the new sname/hname. This way we
291 * retain any additional principal name components from the given
292 * principal name.
294 * The name-type is set further below.
296 ret = krb5_principal_set_comp_string(context, server, 0, sname);
297 if (ret)
298 krb5_err(context, 1, ret, "krb5_principal_set_comp_string %s", argv[0]);
299 ret = krb5_principal_set_comp_string(context, server, 1, hname);
300 if (ret)
301 krb5_err(context, 1, ret, "krb5_principal_set_comp_string %s", argv[0]);
302 krb5_free_principal(context, server2);
303 } else {
304 size_t i;
307 * In this case the arguments are principal name components.
309 * The service and hostname components can be defaulted by passing
310 * empty strings.
312 sname = argv[0];
313 if (*sname == '\0')
314 sname = NULL;
315 hname = argv[1];
316 if (hname == NULL || *hname == '\0')
317 hname = NULL;
318 ret = krb5_sname_to_principal(context, hname, sname,
319 KRB5_NT_SRV_HST, &server);
320 if (ret)
321 krb5_err(context, 1, ret, "krb5_sname_to_principal");
323 for (i = 2; i < argc; i++) {
324 ret = krb5_principal_set_comp_string(context, server, i, argv[i]);
325 if (ret)
326 krb5_err(context, 1, ret, "krb5_principal_set_comp_string");
329 } else if (argc == 1) {
330 ret = krb5_parse_name(context, argv[0], &server);
331 if (ret)
332 krb5_err(context, 1, ret, "krb5_parse_name %s", argv[0]);
333 } else {
334 usage(1);
337 if (nametype != KRB5_NT_UNKNOWN)
338 server->name.name_type = (NAME_TYPE)nametype;
340 ret = krb5_get_creds(context, opt, cache, server, &out);
341 if (ret)
342 krb5_err(context, 1, ret, "krb5_get_creds");
344 if (out_cache_str) {
345 krb5_ccache id;
347 ret = krb5_cc_resolve(context, out_cache_str, &id);
348 if(ret)
349 krb5_err(context, 1, ret, "krb5_cc_resolve");
351 ret = krb5_cc_initialize(context, id, out->client);
352 if(ret)
353 krb5_err(context, 1, ret, "krb5_cc_initialize");
355 ret = krb5_cc_store_cred(context, id, out);
356 if(ret)
357 krb5_err(context, 1, ret, "krb5_cc_store_cred");
358 krb5_cc_close(context, id);
361 krb5_free_creds(context, out);
362 krb5_free_principal(context, server);
363 krb5_get_creds_opt_free(context, opt);
364 krb5_cc_close (context, cache);
365 krb5_free_context (context);
367 return 0;