German translation of 2 error files
[heimdal.git] / kuser / kgetcred.c
blobb95bc9d05e2e5e21e175f53281ff83a8db6c2228
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 debug_flag;
50 static int version_flag;
51 static int help_flag;
53 struct getargs args[] = {
54 { "cache", 'c', arg_string, &cache_str,
55 NP_("credential cache to use", ""), "cache"},
56 { "out-cache", 0, arg_string, &out_cache_str,
57 NP_("credential cache to store credential in", ""), "cache"},
58 { "delegation-credential-cache",0,arg_string, &delegation_cred_str,
59 NP_("where to find the ticket use for delegation", ""), "cache"},
60 { "canonicalize", 0, arg_flag, &canonicalize_flag,
61 NP_("canonicalize the principal (chase referrals)", ""), NULL },
62 { "canonical", 0, arg_flag, &is_canonical_flag,
63 NP_("the name components are canonical", ""), NULL },
64 { "forwardable", 0, arg_flag, &forwardable_flag,
65 NP_("forwardable ticket requested", ""), NULL},
66 { "transit-check", 0, arg_negative_flag, &transit_flag, NULL, NULL },
67 { "enctype", 'e', arg_string, &etype_str,
68 NP_("encryption type to use", ""), "enctype"},
69 { "impersonate", 0, arg_string, &impersonate_str,
70 NP_("client to impersonate", ""), "principal"},
71 { "name-type", 0, arg_string, &nametype_str,
72 NP_("Kerberos name type", ""), NULL },
73 { "hostbased", 'H', arg_flag, &is_hostbased_flag,
74 NP_("indicate that the name is a host-based service name", ""), NULL },
75 { "store", 0, arg_negative_flag, &store_flag,
76 NP_("don't store the tickets obtained in the cache", ""), NULL },
77 { "cached-only", 0, arg_flag, &cached_only_flag,
78 NP_("don't talk to the KDC, just search the cache", ""), NULL },
79 { "debug", 0, arg_flag, &debug_flag, NULL, NULL },
80 { "version", 0, arg_flag, &version_flag, NULL, NULL },
81 { "help", 0, arg_flag, &help_flag, NULL, NULL }
84 static void
85 usage(int ret)
87 arg_printusage(args,
88 sizeof(args)/sizeof(*args),
89 NULL,
90 "service");
91 exit (ret);
94 int
95 main(int argc, char **argv)
97 krb5_error_code ret;
98 krb5_context context;
99 krb5_ccache cache;
100 krb5_creds *out;
101 int optidx = 0;
102 int32_t nametype = KRB5_NT_UNKNOWN;
103 krb5_get_creds_opt opt;
104 krb5_principal server = NULL;
105 krb5_principal impersonate;
107 setprogname(argv[0]);
109 ret = krb5_init_context(&context);
110 if (ret)
111 errx(1, "krb5_init_context failed: %d", ret);
113 if (getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
114 usage(1);
116 if (help_flag)
117 usage (0);
119 if (version_flag) {
120 print_version(NULL);
121 exit(0);
124 argc -= optidx;
125 argv += optidx;
127 if (debug_flag) {
128 ret = krb5_set_debug_dest(context, getprogname(), "STDERR");
129 if (ret)
130 krb5_warn(context, ret, "krb5_set_debug_dest");
133 if (cache_str) {
134 ret = krb5_cc_resolve(context, cache_str, &cache);
135 if (ret)
136 krb5_err(context, 1, ret, "%s", cache_str);
137 } else {
138 ret = krb5_cc_default (context, &cache);
139 if (ret)
140 krb5_err(context, 1, ret, "krb5_cc_resolve");
143 ret = krb5_get_creds_opt_alloc(context, &opt);
144 if (ret)
145 krb5_err(context, 1, ret, "krb5_get_creds_opt_alloc");
147 if (etype_str) {
148 krb5_enctype enctype;
150 ret = krb5_string_to_enctype(context, etype_str, &enctype);
151 if (ret)
152 krb5_errx(context, 1, N_("unrecognized enctype: %s", ""),
153 etype_str);
154 krb5_get_creds_opt_set_enctype(context, opt, enctype);
157 if (impersonate_str) {
158 ret = krb5_parse_name(context, impersonate_str, &impersonate);
159 if (ret)
160 krb5_err(context, 1, ret, "krb5_parse_name %s", impersonate_str);
161 krb5_get_creds_opt_set_impersonate(context, opt, impersonate);
162 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_STORE);
163 krb5_free_principal(context, impersonate);
166 if (out_cache_str)
167 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_STORE);
169 if (forwardable_flag)
170 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
171 if (!transit_flag)
172 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_TRANSIT_CHECK);
173 if (canonicalize_flag)
174 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_CANONICALIZE);
175 if (!store_flag)
176 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_NO_STORE);
177 if (cached_only_flag)
178 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_CACHED);
180 if (delegation_cred_str) {
181 krb5_ccache id;
182 krb5_creds c, mc;
183 Ticket ticket;
185 krb5_cc_clear_mcred(&mc);
186 ret = krb5_cc_get_principal(context, cache, &mc.server);
187 if (ret)
188 krb5_err(context, 1, ret, "krb5_cc_get_principal");
190 ret = krb5_cc_resolve(context, delegation_cred_str, &id);
191 if(ret)
192 krb5_err(context, 1, ret, "krb5_cc_resolve");
194 ret = krb5_cc_retrieve_cred(context, id, 0, &mc, &c);
195 if(ret)
196 krb5_err(context, 1, ret, "krb5_cc_retrieve_cred");
198 ret = decode_Ticket(c.ticket.data, c.ticket.length, &ticket, NULL);
199 if (ret) {
200 krb5_clear_error_message(context);
201 krb5_err(context, 1, ret, "decode_Ticket");
203 krb5_free_cred_contents(context, &c);
205 ret = krb5_get_creds_opt_set_ticket(context, opt, &ticket);
206 if(ret)
207 krb5_err(context, 1, ret, "krb5_get_creds_opt_set_ticket");
208 free_Ticket(&ticket);
210 krb5_cc_close(context, id);
211 krb5_free_principal(context, mc.server);
213 krb5_get_creds_opt_add_options(context, opt,
214 KRB5_GC_CONSTRAINED_DELEGATION);
217 if (nametype_str != NULL) {
218 ret = krb5_parse_nametype(context, nametype_str, &nametype);
219 if (ret)
220 krb5_err(context, 1, ret, "krb5_parse_nametype");
223 if (nametype == KRB5_NT_SRV_HST ||
224 nametype == KRB5_NT_SRV_HST_NEEDS_CANON)
225 is_hostbased_flag = 1;
227 if (is_hostbased_flag) {
228 const char *sname = NULL;
229 const char *hname = NULL;
231 if (nametype_str != NULL &&
232 nametype != KRB5_NT_SRV_HST &&
233 nametype != KRB5_NT_SRV_HST_NEEDS_CANON)
234 krb5_errx(context, 1, "--hostbased not compatible with "
235 "non-hostbased --name-type");
237 if (is_canonical_flag)
238 nametype = KRB5_NT_SRV_HST;
239 else
240 nametype = KRB5_NT_SRV_HST_NEEDS_CANON;
243 * Host-based service names can have more than one component.
245 * RFC5179 did not, but should have, assign a Kerberos name-type
246 * corresponding to GSS_C_NT_DOMAINBASED. But it's basically a
247 * host-based service name type with one additional component.
249 * So that's how we're treating host-based service names here:
250 * two or more components.
253 if (argc == 0) {
254 usage(1);
255 } else if (argc == 1) {
256 krb5_principal server2;
259 * In this case the one argument is a principal name, not the
260 * service name.
262 * We parse the argument as a principal name, extract the service
263 * and hostname components, use krb5_sname_to_principal(), then
264 * extract the service and hostname components from that.
267 ret = krb5_parse_name(context, argv[0], &server);
268 if (ret)
269 krb5_err(context, 1, ret, "krb5_parse_name %s", argv[0]);
270 sname = krb5_principal_get_comp_string(context, server, 0);
273 * If a single-component principal name is given, then we'll
274 * default the hostname, as krb5_principal_get_comp_string()
275 * returns NULL in this case.
277 hname = krb5_principal_get_comp_string(context, server, 1);
279 ret = krb5_sname_to_principal(context, hname, sname,
280 KRB5_NT_SRV_HST, &server2);
281 sname = krb5_principal_get_comp_string(context, server2, 0);
282 hname = krb5_principal_get_comp_string(context, server2, 1);
285 * Modify the original with the new sname/hname. This way we
286 * retain any additional principal name components from the given
287 * principal name.
289 * The name-type is set further below.
291 ret = krb5_principal_set_comp_string(context, server, 0, sname);
292 if (ret)
293 krb5_err(context, 1, ret, "krb5_principal_set_comp_string %s", argv[0]);
294 ret = krb5_principal_set_comp_string(context, server, 1, hname);
295 if (ret)
296 krb5_err(context, 1, ret, "krb5_principal_set_comp_string %s", argv[0]);
297 krb5_free_principal(context, server2);
298 } else {
299 size_t i;
302 * In this case the arguments are principal name components.
304 * The service and hostname components can be defaulted by passing
305 * empty strings.
307 sname = argv[0];
308 if (*sname == '\0')
309 sname = NULL;
310 hname = argv[1];
311 if (hname == NULL || *hname == '\0')
312 hname = NULL;
313 ret = krb5_sname_to_principal(context, hname, sname,
314 KRB5_NT_SRV_HST, &server);
315 if (ret)
316 krb5_err(context, 1, ret, "krb5_sname_to_principal");
318 for (i = 2; i < argc; i++) {
319 ret = krb5_principal_set_comp_string(context, server, i, argv[i]);
320 if (ret)
321 krb5_err(context, 1, ret, "krb5_principal_set_comp_string");
324 } else if (argc == 1) {
325 ret = krb5_parse_name(context, argv[0], &server);
326 if (ret)
327 krb5_err(context, 1, ret, "krb5_parse_name %s", argv[0]);
328 } else {
329 usage(1);
332 if (nametype != KRB5_NT_UNKNOWN)
333 server->name.name_type = (NAME_TYPE)nametype;
335 ret = krb5_get_creds(context, opt, cache, server, &out);
336 if (ret)
337 krb5_err(context, 1, ret, "krb5_get_creds");
339 if (out_cache_str) {
340 krb5_ccache id;
342 ret = krb5_cc_resolve(context, out_cache_str, &id);
343 if(ret)
344 krb5_err(context, 1, ret, "krb5_cc_resolve");
346 ret = krb5_cc_initialize(context, id, out->client);
347 if(ret)
348 krb5_err(context, 1, ret, "krb5_cc_initialize");
350 ret = krb5_cc_store_cred(context, id, out);
351 if(ret)
352 krb5_err(context, 1, ret, "krb5_cc_store_cred");
353 krb5_cc_close(context, id);
356 krb5_free_creds(context, out);
357 krb5_free_principal(context, server);
358 krb5_get_creds_opt_free(context, opt);
359 krb5_cc_close (context, cache);
360 krb5_free_context (context);
362 return 0;