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