lib/base: heim_config_parse_debug initialize output parameters
[heimdal.git] / kuser / kinit.c
blob2a0c2433353972c93dd20d57114276a4a7b24c9b
1 /*
2 * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "kuser_locl.h"
38 #ifdef HAVE_FRAMEWORK_SECURITY
39 #include <Security/Security.h>
40 #endif
42 #ifndef NO_NTLM
43 #include "heimntlm.h"
44 #endif
46 #ifndef SIGINFO
47 #define SIGINFO SIGUSR1
48 #endif
50 int forwardable_flag = -1;
51 int proxiable_flag = -1;
52 int renewable_flag = -1;
53 int renew_flag = 0;
54 int pac_flag = -1;
55 int validate_flag = 0;
56 int version_flag = 0;
57 int help_flag = 0;
58 int addrs_flag = -1;
59 struct getarg_strings extra_addresses;
60 int anonymous_flag = 0;
61 char *lifetime = NULL;
62 char *renew_life = NULL;
63 char *server_str = NULL;
64 static krb5_principal tgs_service;
65 char *cred_cache = NULL;
66 char *start_str = NULL;
67 static int switch_cache_flags = -1;
68 static int default_for = 0;
69 struct getarg_strings etype_str;
70 int use_keytab = 0;
71 char *keytab_str = NULL;
72 static krb5_keytab kt = NULL;
73 int do_afslog = -1;
74 int fcache_version;
75 char *password_file = NULL;
76 char *pk_user_id = NULL;
77 int pk_enterprise_flag = 0;
78 struct hx509_certs_data *ent_user_id = NULL;
79 char *pk_x509_anchors = NULL;
80 int pk_use_enckey = 0;
81 static int canonicalize_flag = 0;
82 static int enterprise_flag = 0;
83 static int ok_as_delegate_flag = 0;
84 static char *fast_armor_cache_string = NULL;
85 static int use_referrals_flag = 0;
86 static int windows_flag = 0;
87 #ifndef NO_NTLM
88 static char *ntlm_domain;
89 #endif
92 static struct getargs args[] = {
94 * used by MIT
95 * a: ~A
96 * V: verbose
97 * F: ~f
98 * P: ~p
99 * C: v4 cache name?
100 * 5:
102 * old flags
103 * 4:
104 * 9:
106 { "afslog", 0 , arg_flag, &do_afslog,
107 NP_("obtain afs tokens", ""), NULL },
109 { "cache", 'c', arg_string, &cred_cache,
110 NP_("credentials cache", ""), "cachename" },
112 { "forwardable", 'F', arg_negative_flag, &forwardable_flag,
113 NP_("get tickets not forwardable", ""), NULL },
115 { NULL, 'f', arg_flag, &forwardable_flag,
116 NP_("get forwardable tickets", ""), NULL },
118 { "keytab", 't', arg_string, &keytab_str,
119 NP_("keytab to use", ""), "keytabname" },
121 { "lifetime", 'l', arg_string, &lifetime,
122 NP_("lifetime of tickets", ""), "time" },
124 { "proxiable", 'p', arg_flag, &proxiable_flag,
125 NP_("get proxiable tickets", ""), NULL },
127 { "renew", 'R', arg_flag, &renew_flag,
128 NP_("renew TGT", ""), NULL },
130 { "renewable", 0, arg_flag, &renewable_flag,
131 NP_("get renewable tickets", ""), NULL },
133 { "renewable-life", 'r', arg_string, &renew_life,
134 NP_("renewable lifetime of tickets", ""), "time" },
136 { "server", 'S', arg_string, &server_str,
137 NP_("server to get ticket for", ""), "principal" },
139 { "start-time", 's', arg_string, &start_str,
140 NP_("when ticket gets valid", ""), "time" },
142 { "use-keytab", 'k', arg_flag, &use_keytab,
143 NP_("get key from keytab", ""), NULL },
145 { "validate", 'v', arg_flag, &validate_flag,
146 NP_("validate TGT", ""), NULL },
148 { "enctypes", 'e', arg_strings, &etype_str,
149 NP_("encryption types to use", ""), "enctypes" },
151 { "fcache-version", 0, arg_integer, &fcache_version,
152 NP_("file cache version to create", ""), NULL },
154 { "addresses", 'A', arg_negative_flag, &addrs_flag,
155 NP_("request a ticket with no addresses", ""), NULL },
157 { "extra-addresses",'a', arg_strings, &extra_addresses,
158 NP_("include these extra addresses", ""), "addresses" },
160 { "anonymous", 'n', arg_flag, &anonymous_flag,
161 NP_("request an anonymous ticket", ""), NULL },
163 { "request-pac", 0, arg_flag, &pac_flag,
164 NP_("request a Windows PAC", ""), NULL },
166 { "password-file", 0, arg_string, &password_file,
167 NP_("read the password from a file", ""), NULL },
169 { "canonicalize",0, arg_flag, &canonicalize_flag,
170 NP_("canonicalize client principal", ""), NULL },
172 { "enterprise",0, arg_flag, &enterprise_flag,
173 NP_("parse principal as a KRB5-NT-ENTERPRISE name", ""), NULL },
174 #ifdef PKINIT
175 { "pk-enterprise", 0, arg_flag, &pk_enterprise_flag,
176 NP_("use enterprise name from certificate", ""), NULL },
178 { "pk-user", 'C', arg_string, &pk_user_id,
179 NP_("principal's public/private/certificate identifier", ""), "id" },
181 { "x509-anchors", 'D', arg_string, &pk_x509_anchors,
182 NP_("directory with CA certificates", ""), "directory" },
184 { "pk-use-enckey", 0, arg_flag, &pk_use_enckey,
185 NP_("Use RSA encrypted reply (instead of DH)", ""), NULL },
186 #endif
187 #ifndef NO_NTLM
188 { "ntlm-domain", 0, arg_string, &ntlm_domain,
189 NP_("NTLM domain", ""), "domain" },
190 #endif
192 { "change-default", 0, arg_negative_flag, &switch_cache_flags,
193 NP_("switch the default cache to the new credentials cache", ""), NULL },
195 { "default-for-principal", 0, arg_negative_flag, &default_for,
196 NP_("use a default cache appropriate for the client principal name", ""), NULL },
198 { "ok-as-delegate", 0, arg_flag, &ok_as_delegate_flag,
199 NP_("honor ok-as-delegate on tickets", ""), NULL },
201 { "fast-armor-cache", 0, arg_string, &fast_armor_cache_string,
202 NP_("use this credential cache as FAST armor cache", ""), "cache" },
204 { "use-referrals", 0, arg_flag, &use_referrals_flag,
205 NP_("only use referrals, no dns canalisation", ""), NULL },
207 { "windows", 0, arg_flag, &windows_flag,
208 NP_("get windows behavior", ""), NULL },
210 { "version", 0, arg_flag, &version_flag, NULL, NULL },
211 { "help", 0, arg_flag, &help_flag, NULL, NULL }
214 static char *
215 get_default_realm(krb5_context context);
217 static void
218 usage(int ret)
220 arg_printusage_i18n(args, sizeof(args)/sizeof(*args), N_("Usage: ", ""),
221 NULL, "[principal [command]]", getarg_i18n);
222 exit(ret);
225 static krb5_error_code
226 tgs_principal(krb5_context context,
227 krb5_ccache cache,
228 krb5_principal client,
229 krb5_const_realm tgs_realm,
230 krb5_principal *out_princ)
232 krb5_error_code ret;
233 krb5_principal tgs_princ;
234 krb5_creds creds;
235 krb5_creds *tick;
236 krb5_flags options;
238 ret = krb5_make_principal(context, &tgs_princ, tgs_realm,
239 KRB5_TGS_NAME, tgs_realm, NULL);
240 if (ret)
241 return ret;
244 * Don't fail-over to a different realm just because a TGT expired
246 options = KRB5_GC_CACHED | KRB5_GC_EXPIRED_OK;
248 memset(&creds, 0, sizeof(creds));
249 creds.client = client;
250 creds.server = tgs_princ;
251 ret = krb5_get_credentials(context, options, cache, &creds, &tick);
252 if (ret == 0) {
253 krb5_free_creds(context, tick);
254 *out_princ = tgs_princ;
255 } else {
256 krb5_free_principal(context, tgs_princ);
259 return ret;
264 * Try TGS specified with '-S',
265 * then TGS of client realm,
266 * then if fallback is FALSE: fail,
267 * otherwise try TGS of default realm,
268 * and finally first TGT in ccache.
270 static krb5_error_code
271 get_server(krb5_context context,
272 krb5_ccache cache,
273 krb5_principal client,
274 const char *server,
275 krb5_boolean fallback,
276 krb5_principal *princ)
278 krb5_error_code ret = 0;
279 krb5_const_realm realm;
280 krb5_realm def_realm;
281 krb5_cc_cursor cursor;
282 krb5_creds creds;
283 const char *pcomp;
285 if (tgs_service)
286 goto done;
288 if (server) {
289 ret = krb5_parse_name(context, server, &tgs_service);
290 goto done;
293 /* Try the client realm first */
294 realm = krb5_principal_get_realm(context, client);
295 ret = tgs_principal(context, cache, client, realm, &tgs_service);
296 if (ret == 0 || ret != KRB5_CC_NOTFOUND)
297 goto done;
299 if (!fallback)
300 return ret;
302 /* Next try the default realm */
303 ret = krb5_get_default_realm(context, &def_realm);
304 if (ret)
305 return ret;
306 ret = tgs_principal(context, cache, client, def_realm, &tgs_service);
307 free(def_realm);
308 if (ret == 0 || ret != KRB5_CC_NOTFOUND)
309 goto done;
311 /* Finally try the first TGT with instance == realm in the cache */
312 ret = krb5_cc_start_seq_get(context, cache, &cursor);
313 if (ret)
314 return ret;
316 for (/**/; ret == 0; krb5_free_cred_contents (context, &creds)) {
318 ret = krb5_cc_next_cred(context, cache, &cursor, &creds);
319 if (ret)
320 break;
321 if (creds.server->name.name_string.len != 2)
322 continue;
323 pcomp = krb5_principal_get_comp_string(context, creds.server, 0);
324 if (strcmp(pcomp, KRB5_TGS_NAME) != 0)
325 continue;
326 realm = krb5_principal_get_realm(context, creds.server);
327 pcomp = krb5_principal_get_comp_string(context, creds.server, 1);
328 if (strcmp(realm, pcomp) != 0)
329 continue;
330 ret = krb5_copy_principal(context, creds.server, &tgs_service);
331 break;
333 if (ret == KRB5_CC_END) {
334 ret = KRB5_CC_NOTFOUND;
335 krb5_set_error_message(context, ret,
336 N_("Credential cache contains no TGTs", ""));
338 krb5_cc_end_seq_get(context, cache, &cursor);
340 done:
341 if (!ret)
342 ret = krb5_copy_principal(context, tgs_service, princ);
343 return ret;
346 static krb5_error_code
347 copy_configs(krb5_context context,
348 krb5_ccache dst,
349 krb5_ccache src,
350 krb5_principal start_ticket_server)
352 krb5_error_code ret;
353 const char *cfg_names[] = {"realm-config", "FriendlyName", "anon_pkinit_realm", NULL};
354 const char *cfg_names_w_pname[] = {"fast_avail", NULL};
355 krb5_data cfg_data;
356 size_t i;
358 for (i = 0; cfg_names[i]; i++) {
359 ret = krb5_cc_get_config(context, src, NULL, cfg_names[i], &cfg_data);
360 if (ret == KRB5_CC_NOTFOUND || ret == KRB5_CC_END) {
361 continue;
362 } else if (ret) {
363 krb5_warn(context, ret, "krb5_cc_get_config");
364 return ret;
366 ret = krb5_cc_set_config(context, dst, NULL, cfg_names[i], &cfg_data);
367 if (ret)
368 krb5_warn(context, ret, "krb5_cc_set_config");
370 for (i = 0; start_ticket_server && cfg_names_w_pname[i]; i++) {
371 ret = krb5_cc_get_config(context, src, start_ticket_server,
372 cfg_names_w_pname[i], &cfg_data);
373 if (ret == KRB5_CC_NOTFOUND || ret == KRB5_CC_END) {
374 continue;
375 } else if (ret) {
376 krb5_warn(context, ret, "krb5_cc_get_config");
377 return ret;
379 ret = krb5_cc_set_config(context, dst, start_ticket_server,
380 cfg_names_w_pname[i], &cfg_data);
381 if (ret && ret != KRB5_CC_NOTFOUND)
382 krb5_warn(context, ret, "krb5_cc_set_config");
385 * We don't copy cc configs for any other principals though (mostly
386 * those are per-target time offsets and the like, so it's bad to
387 * lose them, but hardly the end of the world, and as they may not
388 * expire anyways, it's good to let them go).
390 return 0;
393 static krb5_error_code
394 get_anon_pkinit_tgs_name(krb5_context context,
395 krb5_ccache ccache,
396 krb5_principal *tgs_name)
398 krb5_error_code ret;
399 krb5_data data;
400 char *realm;
402 ret = krb5_cc_get_config(context, ccache, NULL, "anon_pkinit_realm", &data);
403 if (ret == 0)
404 realm = strndup(data.data, data.length);
405 else
406 realm = get_default_realm(context);
408 krb5_data_free(&data);
410 if (realm == NULL)
411 return krb5_enomem(context);
413 ret = krb5_make_principal(context, tgs_name, realm,
414 KRB5_TGS_NAME, realm, NULL);
416 free(realm);
418 return ret;
421 static krb5_error_code
422 renew_validate(krb5_context context,
423 int renew,
424 int validate,
425 krb5_ccache *cachep,
426 krb5_const_principal principal,
427 krb5_boolean cache_is_default_for,
428 const char *server,
429 krb5_deltat life)
431 krb5_error_code ret;
432 krb5_ccache tempccache = NULL;
433 krb5_ccache cache = *cachep;
434 krb5_creds in, *out = NULL;
435 krb5_kdc_flags flags;
437 memset(&in, 0, sizeof(in));
439 ret = krb5_cc_get_principal(context, cache, &in.client);
440 if (ret && cache_is_default_for && principal) {
441 krb5_error_code ret2;
442 krb5_ccache def_ccache = NULL;
444 ret2 = krb5_cc_default(context, &def_ccache);
445 if (ret2 == 0)
446 ret2 = krb5_cc_get_principal(context, def_ccache, &in.client);
447 if (ret2 == 0 &&
448 krb5_principal_compare(context, principal, in.client)) {
449 krb5_cc_close(context, *cachep);
450 *cachep = def_ccache;
451 def_ccache = NULL;
452 ret = 0;
454 krb5_cc_close(context, def_ccache);
456 if (ret) {
457 krb5_warn(context, ret, "krb5_cc_get_principal");
458 return ret;
461 if (principal && !krb5_principal_compare(context, principal, in.client)) {
462 char *ccname = NULL;
464 (void) krb5_cc_get_full_name(context, cache, &ccname);
465 krb5_errx(context, 1, "Credentials in cache %s do not match requested "
466 "principal", ccname ? ccname : "requested");
467 free(ccname);
470 if (server == NULL &&
471 krb5_principal_is_anonymous(context, in.client,
472 KRB5_ANON_MATCH_UNAUTHENTICATED))
473 ret = get_anon_pkinit_tgs_name(context, cache, &in.server);
474 else
475 ret = get_server(context, cache, in.client, server, TRUE, &in.server);
476 if (ret) {
477 krb5_warn(context, ret, "get_server");
478 goto out;
481 if (renew) {
483 * no need to check the error here, it's only to be
484 * friendly to the user
486 krb5_get_credentials(context, KRB5_GC_CACHED, cache, &in, &out);
489 flags.i = 0;
490 flags.b.renewable = flags.b.renew = renew;
491 flags.b.validate = validate;
493 if (forwardable_flag != -1)
494 flags.b.forwardable = forwardable_flag;
495 else if (out)
496 flags.b.forwardable = out->flags.b.forwardable;
498 if (proxiable_flag != -1)
499 flags.b.proxiable = proxiable_flag;
500 else if (out)
501 flags.b.proxiable = out->flags.b.proxiable;
503 if (anonymous_flag)
504 flags.b.request_anonymous = anonymous_flag;
505 if (life)
506 in.times.endtime = time(NULL) + life;
508 if (out) {
509 krb5_free_creds(context, out);
510 out = NULL;
514 ret = krb5_get_kdc_cred(context,
515 cache,
516 flags,
517 NULL,
518 NULL,
519 &in,
520 &out);
521 if (ret) {
522 krb5_warn(context, ret, "krb5_get_kdc_cred");
523 goto out;
526 ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, cache),
527 NULL, &tempccache);
528 if (ret) {
529 krb5_warn(context, ret, "krb5_cc_new_unique");
530 goto out;
533 ret = krb5_cc_initialize(context, tempccache, in.client);
534 if (ret) {
535 krb5_warn(context, ret, "krb5_cc_initialize");
536 goto out;
539 ret = krb5_cc_store_cred(context, tempccache, out);
540 if (ret) {
541 krb5_warn(context, ret, "krb5_cc_store_cred");
542 goto out;
546 * We want to preserve cc configs as some are security-relevant, and
547 * anyways it's the friendly thing to do.
549 ret = copy_configs(context, tempccache, cache, out->server);
550 if (ret)
551 goto out;
553 ret = krb5_cc_move(context, tempccache, cache);
554 if (ret) {
555 krb5_warn(context, ret, "krb5_cc_move");
556 goto out;
558 tempccache = NULL;
560 out:
561 if (tempccache)
562 krb5_cc_destroy(context, tempccache);
563 if (out)
564 krb5_free_creds(context, out);
565 krb5_free_cred_contents(context, &in);
566 return ret;
569 #ifndef NO_NTLM
571 static krb5_error_code
572 store_ntlmkey(krb5_context context, krb5_ccache id,
573 const char *domain, struct ntlm_buf *buf)
575 krb5_error_code ret;
576 krb5_data data;
577 char *name;
578 int aret;
580 ret = krb5_cc_get_config(context, id, NULL, "default-ntlm-domain", &data);
581 if (ret == 0) {
582 krb5_data_free(&data);
583 } else {
584 data.length = strlen(domain);
585 data.data = rk_UNCONST(domain);
586 ret = krb5_cc_set_config(context, id, NULL, "default-ntlm-domain", &data);
587 if (ret != 0)
588 return ret;
591 aret = asprintf(&name, "ntlm-key-%s", domain);
592 if (aret == -1 || name == NULL)
593 return krb5_enomem(context);
595 data.length = buf->length;
596 data.data = buf->data;
598 ret = krb5_cc_set_config(context, id, NULL, name, &data);
599 free(name);
600 return ret;
602 #endif
604 static krb5_error_code
605 get_new_tickets(krb5_context context,
606 krb5_principal principal,
607 krb5_ccache ccache,
608 krb5_deltat ticket_life,
609 int interactive,
610 int anonymous_pkinit)
612 krb5_error_code ret;
613 krb5_creds cred;
614 char passwd[256];
615 krb5_deltat start_time = 0;
616 krb5_deltat renew = 0;
617 const char *renewstr = NULL;
618 krb5_enctype *enctype = NULL;
619 krb5_ccache tempccache = NULL;
620 krb5_init_creds_context ctx = NULL;
621 krb5_get_init_creds_opt *opt = NULL;
622 krb5_prompter_fct prompter = krb5_prompter_posix;
623 #ifndef NO_NTLM
624 struct ntlm_buf ntlmkey;
625 memset(&ntlmkey, 0, sizeof(ntlmkey));
626 #endif
627 passwd[0] = '\0';
629 if (!interactive)
630 prompter = NULL;
632 if (password_file) {
633 FILE *f;
635 if (strcasecmp("STDIN", password_file) == 0)
636 f = stdin;
637 else
638 f = fopen(password_file, "r");
639 if (f == NULL) {
640 krb5_warnx(context, N_("Failed to open the password file %s", ""),
641 password_file);
642 return errno;
645 if (fgets(passwd, sizeof(passwd), f) == NULL) {
646 krb5_warnx(context, N_("Failed to read password from file %s", ""),
647 password_file);
648 if (f != stdin)
649 fclose(f);
650 return EINVAL; /* XXX Need a better error */
652 if (f != stdin)
653 fclose(f);
654 passwd[strcspn(passwd, "\n")] = '\0';
657 #ifdef HAVE_FRAMEWORK_SECURITY
658 if (passwd[0] == '\0') {
659 const char *realm;
660 OSStatus osret;
661 UInt32 length;
662 void *buffer;
663 char *name;
665 realm = krb5_principal_get_realm(context, principal);
667 ret = krb5_unparse_name_flags(context, principal,
668 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &name);
669 if (ret)
670 goto nopassword;
672 osret = SecKeychainFindGenericPassword(NULL, strlen(realm), realm,
673 strlen(name), name,
674 &length, &buffer, NULL);
675 free(name);
676 if (osret == noErr && length < sizeof(passwd) - 1) {
677 memcpy(passwd, buffer, length);
678 passwd[length] = '\0';
680 nopassword:
681 do { } while(0);
683 #endif
685 memset(&cred, 0, sizeof(cred));
687 ret = krb5_get_init_creds_opt_alloc(context, &opt);
688 if (ret) {
689 krb5_warn(context, ret, "krb5_get_init_creds_opt_alloc");
690 goto out;
693 krb5_get_init_creds_opt_set_default_flags(context, "kinit",
694 krb5_principal_get_realm(context, principal), opt);
696 if (forwardable_flag != -1)
697 krb5_get_init_creds_opt_set_forwardable(opt, forwardable_flag);
698 if (proxiable_flag != -1)
699 krb5_get_init_creds_opt_set_proxiable(opt, proxiable_flag);
700 if (anonymous_flag)
701 krb5_get_init_creds_opt_set_anonymous(opt, anonymous_flag);
702 if (pac_flag != -1)
703 krb5_get_init_creds_opt_set_pac_request(context, opt,
704 pac_flag ? TRUE : FALSE);
705 if (canonicalize_flag)
706 krb5_get_init_creds_opt_set_canonicalize(context, opt, TRUE);
707 if (pk_enterprise_flag || enterprise_flag || canonicalize_flag || windows_flag)
708 krb5_get_init_creds_opt_set_win2k(context, opt, TRUE);
709 if (pk_user_id || ent_user_id || anonymous_pkinit) {
710 ret = krb5_get_init_creds_opt_set_pkinit(context, opt,
711 principal,
712 pk_user_id,
713 pk_x509_anchors,
714 NULL,
715 NULL,
716 pk_use_enckey ? KRB5_GIC_OPT_PKINIT_USE_ENCKEY : 0 |
717 anonymous_pkinit ? KRB5_GIC_OPT_PKINIT_ANONYMOUS : 0,
718 prompter,
719 NULL,
720 passwd);
721 if (ret) {
722 krb5_warn(context, ret, "krb5_get_init_creds_opt_set_pkinit");
723 goto out;
725 if (ent_user_id)
726 krb5_get_init_creds_opt_set_pkinit_user_certs(context, opt, ent_user_id);
729 if (addrs_flag != -1)
730 krb5_get_init_creds_opt_set_addressless(context, opt,
731 addrs_flag ? FALSE : TRUE);
733 if (renew_life == NULL && renewable_flag)
734 renewstr = "6 months";
735 if (renew_life)
736 renewstr = renew_life;
737 if (renewstr) {
738 renew = parse_time(renewstr, "s");
739 if (renew < 0)
740 errx(1, "unparsable time: %s", renewstr);
742 krb5_get_init_creds_opt_set_renew_life(opt, renew);
745 if (ticket_life != 0)
746 krb5_get_init_creds_opt_set_tkt_life(opt, ticket_life);
748 if (start_str) {
749 int tmp = parse_time(start_str, "s");
750 if (tmp < 0)
751 errx(1, N_("unparsable time: %s", ""), start_str);
753 start_time = tmp;
756 if (etype_str.num_strings) {
757 int i;
759 enctype = malloc(etype_str.num_strings * sizeof(*enctype));
760 if (enctype == NULL)
761 errx(1, "out of memory");
762 for(i = 0; i < etype_str.num_strings; i++) {
763 ret = krb5_string_to_enctype(context,
764 etype_str.strings[i],
765 &enctype[i]);
766 if (ret)
767 errx(1, "unrecognized enctype: %s", etype_str.strings[i]);
769 krb5_get_init_creds_opt_set_etype_list(opt, enctype,
770 etype_str.num_strings);
773 ret = krb5_init_creds_init(context, principal, prompter, NULL, start_time, opt, &ctx);
774 if (ret) {
775 krb5_warn(context, ret, "krb5_init_creds_init");
776 goto out;
779 if (server_str) {
780 ret = krb5_init_creds_set_service(context, ctx, server_str);
781 if (ret) {
782 krb5_warn(context, ret, "krb5_init_creds_set_service");
783 goto out;
787 if (fast_armor_cache_string) {
788 krb5_ccache fastid;
790 ret = krb5_cc_resolve(context, fast_armor_cache_string, &fastid);
791 if (ret) {
792 krb5_warn(context, ret, "krb5_cc_resolve(FAST cache)");
793 goto out;
796 ret = krb5_init_creds_set_fast_ccache(context, ctx, fastid);
797 if (ret) {
798 krb5_warn(context, ret, "krb5_init_creds_set_fast_ccache");
799 goto out;
803 if (use_keytab || keytab_str) {
804 ret = krb5_init_creds_set_keytab(context, ctx, kt);
805 if (ret) {
806 krb5_warn(context, ret, "krb5_init_creds_set_keytab");
807 goto out;
809 } else if (pk_user_id || ent_user_id ||
810 krb5_principal_is_anonymous(context, principal, KRB5_ANON_MATCH_ANY)) {
811 /* nop */;
812 } else if (!interactive && passwd[0] == '\0') {
813 static int already_warned = 0;
815 if (!already_warned)
816 krb5_warnx(context, "Not interactive, failed to get "
817 "initial ticket");
818 krb5_get_init_creds_opt_free(context, opt);
819 already_warned = 1;
820 return 0;
821 } else {
823 if (passwd[0] == '\0') {
824 char *p, *prompt;
825 int aret = 0;
827 ret = krb5_unparse_name(context, principal, &p);
828 if (ret)
829 errx(1, "failed to generate passwd prompt: not enough memory");
831 aret = asprintf(&prompt, N_("%s's Password: ", ""), p);
832 free(p);
833 if (aret == -1)
834 errx(1, "failed to generate passwd prompt: not enough memory");
836 if (UI_UTIL_read_pw_string(passwd, sizeof(passwd)-1, prompt, 0)){
837 memset(passwd, 0, sizeof(passwd));
838 errx(1, "failed to read password");
840 free(prompt);
843 if (passwd[0]) {
844 ret = krb5_init_creds_set_password(context, ctx, passwd);
845 if (ret) {
846 krb5_warn(context, ret, "krb5_init_creds_set_password");
847 goto out;
852 ret = krb5_init_creds_get(context, ctx);
854 #ifndef NO_NTLM
855 if (ntlm_domain && passwd[0])
856 heim_ntlm_nt_key(passwd, &ntlmkey);
857 #endif
858 memset_s(passwd, sizeof(passwd), 0, sizeof(passwd));
860 switch(ret){
861 case 0:
862 break;
863 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
864 exit(1);
865 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
866 case KRB5KRB_AP_ERR_MODIFIED:
867 case KRB5KDC_ERR_PREAUTH_FAILED:
868 case KRB5_GET_IN_TKT_LOOP:
869 krb5_warnx(context, N_("Password incorrect", ""));
870 goto out;
871 case KRB5KRB_AP_ERR_V4_REPLY:
872 krb5_warnx(context, N_("Looks like a Kerberos 4 reply", ""));
873 goto out;
874 case KRB5KDC_ERR_KEY_EXPIRED:
875 krb5_warnx(context, N_("Password expired", ""));
876 goto out;
877 default:
878 krb5_warn(context, ret, "krb5_get_init_creds");
879 goto out;
882 krb5_process_last_request(context, opt, ctx);
884 ret = krb5_init_creds_get_creds(context, ctx, &cred);
885 if (ret) {
886 krb5_warn(context, ret, "krb5_init_creds_get_creds");
887 goto out;
890 if (ticket_life != 0) {
891 if (labs(cred.times.endtime - cred.times.starttime - ticket_life) > 30) {
892 char life[64];
893 unparse_time_approx(cred.times.endtime - cred.times.starttime,
894 life, sizeof(life));
895 krb5_warnx(context, N_("NOTICE: ticket lifetime is %s", ""), life);
898 if (renew_life) {
899 if (labs(cred.times.renew_till - cred.times.starttime - renew) > 30) {
900 char life[64];
901 unparse_time_approx(cred.times.renew_till - cred.times.starttime,
902 life, sizeof(life));
903 krb5_warnx(context,
904 N_("NOTICE: ticket renewable lifetime is %s", ""),
905 life);
908 krb5_free_cred_contents(context, &cred);
910 ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, ccache),
911 NULL, &tempccache);
912 if (ret) {
913 krb5_warn(context, ret, "krb5_cc_new_unique");
914 goto out;
917 ret = krb5_init_creds_store(context, ctx, tempccache);
918 if (ret) {
919 krb5_warn(context, ret, "krb5_init_creds_store");
920 goto out;
923 krb5_init_creds_free(context, ctx);
924 ctx = NULL;
926 ret = krb5_cc_move(context, tempccache, ccache);
927 if (ret) {
928 krb5_warn(context, ret, "krb5_cc_move");
929 goto out;
931 tempccache = NULL;
933 if (switch_cache_flags)
934 krb5_cc_switch(context, ccache);
936 #ifndef NO_NTLM
937 if (ntlm_domain && ntlmkey.data)
938 store_ntlmkey(context, ccache, ntlm_domain, &ntlmkey);
939 #endif
941 if (ok_as_delegate_flag || windows_flag || use_referrals_flag) {
942 unsigned char d = 0;
943 krb5_data data;
945 if (ok_as_delegate_flag || windows_flag)
946 d |= 1;
947 if (use_referrals_flag || windows_flag)
948 d |= 2;
950 data.length = 1;
951 data.data = &d;
953 krb5_cc_set_config(context, ccache, NULL, "realm-config", &data);
956 if (anonymous_pkinit) {
957 krb5_data data;
959 data.length = strlen(principal->realm);
960 data.data = principal->realm;
962 krb5_cc_set_config(context, ccache, NULL, "anon_pkinit_realm", &data);
965 out:
966 krb5_get_init_creds_opt_free(context, opt);
967 if (ctx)
968 krb5_init_creds_free(context, ctx);
969 if (tempccache)
970 krb5_cc_destroy(context, tempccache);
972 if (enctype)
973 free(enctype);
975 return ret;
978 static time_t
979 ticket_lifetime(krb5_context context, krb5_ccache cache, krb5_principal client,
980 const char *server, time_t *renew)
982 krb5_creds in_cred, *cred;
983 krb5_error_code ret;
984 time_t timeout;
985 time_t curtime;
987 memset(&in_cred, 0, sizeof(in_cred));
989 if (renew != NULL)
990 *renew = 0;
992 ret = krb5_cc_get_principal(context, cache, &in_cred.client);
993 if (ret) {
994 krb5_warn(context, ret, "krb5_cc_get_principal");
995 return 0;
998 /* Determine TGS principal without fallback */
999 ret = get_server(context, cache, in_cred.client, server, FALSE,
1000 &in_cred.server);
1001 if (ret) {
1002 krb5_free_principal(context, in_cred.client);
1003 krb5_warn(context, ret, "get_server");
1004 return 0;
1007 ret = krb5_get_credentials(context, KRB5_GC_CACHED,
1008 cache, &in_cred, &cred);
1009 krb5_free_principal(context, in_cred.client);
1010 krb5_free_principal(context, in_cred.server);
1011 if (ret) {
1012 krb5_warn(context, ret, "krb5_get_credentials");
1013 return 0;
1015 curtime = time(NULL);
1016 timeout = cred->times.endtime - curtime;
1017 if (timeout < 0)
1018 timeout = 0;
1019 if (renew) {
1020 *renew = cred->times.renew_till - curtime;
1021 if (*renew < 0)
1022 *renew = 0;
1024 krb5_free_creds(context, cred);
1025 return timeout;
1028 static time_t expire;
1030 static char siginfo_msg[1024] = "No credentials\n";
1032 static void
1033 update_siginfo_msg(time_t exp, const char *srv)
1035 /* Note that exp is relative time */
1036 memset(siginfo_msg, 0, sizeof(siginfo_msg));
1037 memcpy(&siginfo_msg, "Updating...\n", sizeof("Updating...\n"));
1038 if (exp) {
1039 if (srv == NULL) {
1040 snprintf(siginfo_msg, sizeof(siginfo_msg),
1041 N_("kinit: TGT expires in %llu seconds\n", ""),
1042 (unsigned long long)expire);
1043 } else {
1044 snprintf(siginfo_msg, sizeof(siginfo_msg),
1045 N_("kinit: Ticket for %s expired\n", ""), srv);
1047 return;
1050 /* Expired creds */
1051 if (srv == NULL) {
1052 snprintf(siginfo_msg, sizeof(siginfo_msg),
1053 N_("kinit: TGT expired\n", ""));
1054 } else {
1055 snprintf(siginfo_msg, sizeof(siginfo_msg),
1056 N_("kinit: Ticket for %s expired\n", ""), srv);
1060 #ifdef HAVE_SIGACTION
1061 static void
1062 handle_siginfo(int sig)
1064 struct iovec iov[2];
1066 iov[0].iov_base = rk_UNCONST(siginfo_msg);
1067 iov[0].iov_len = strlen(siginfo_msg);
1068 iov[1].iov_base = "\n";
1069 iov[1].iov_len = 1;
1071 writev(STDERR_FILENO, iov, sizeof(iov)/sizeof(iov[0]));
1073 #endif
1075 struct renew_ctx {
1076 krb5_context context;
1077 krb5_ccache ccache;
1078 krb5_principal principal;
1079 krb5_deltat ticket_life;
1080 krb5_deltat timeout;
1083 static time_t
1084 renew_func(void *ptr)
1086 krb5_error_code ret;
1087 struct renew_ctx *ctx = ptr;
1088 time_t renew_expire;
1089 static time_t exp_delay = 1;
1092 * NOTE: We count on the ccache implementation to notice changes to the
1093 * actual ccache filesystem/whatever objects. There should be no ccache
1094 * types for which this is not the case, but it might not hurt to
1095 * re-krb5_cc_resolve() after each successful renew_validate()/
1096 * get_new_tickets() call.
1099 expire = ticket_lifetime(ctx->context, ctx->ccache, ctx->principal,
1100 server_str, &renew_expire);
1103 * When a keytab is available to obtain new tickets, if we are within
1104 * half of the original ticket lifetime of the renew limit, get a new
1105 * TGT instead of renewing the existing TGT. Note, ctx->ticket_life
1106 * is zero by default (without a '-l' option) and cannot be used to
1107 * set the time scale on which we decide whether we're "close to the
1108 * renew limit".
1110 if (use_keytab || keytab_str)
1111 expire += ctx->timeout;
1112 if (renew_expire > expire) {
1113 ret = renew_validate(ctx->context, 1, validate_flag, &ctx->ccache,
1114 NULL, FALSE, server_str, ctx->ticket_life);
1115 } else {
1116 ret = get_new_tickets(ctx->context, ctx->principal, ctx->ccache,
1117 ctx->ticket_life, 0, 0);
1119 expire = ticket_lifetime(ctx->context, ctx->ccache, ctx->principal,
1120 server_str, &renew_expire);
1122 #ifndef NO_AFS
1123 if (ret == 0 && server_str == NULL && do_afslog && k_hasafs())
1124 krb5_afslog(ctx->context, ctx->ccache, NULL, NULL);
1125 #endif
1127 update_siginfo_msg(expire, server_str);
1130 * If our tickets have expired and we been able to either renew them
1131 * or obtain new tickets, then we still call this function but we use
1132 * an exponential backoff. This should take care of the case where
1133 * we are using stored credentials but the KDC has been unavailable
1134 * for some reason...
1137 if (expire < 1) {
1139 * We can't ask to keep spamming stderr but not syslog, so we warn
1140 * only once.
1142 if (exp_delay == 1) {
1143 krb5_warnx(ctx->context, N_("NOTICE: Could not renew/refresh "
1144 "tickets", ""));
1146 if (exp_delay < 7200)
1147 exp_delay += exp_delay / 2 + 1;
1148 return exp_delay;
1150 exp_delay = 1;
1152 return expire / 2 + 1;
1155 static void
1156 set_princ_realm(krb5_context context,
1157 krb5_principal principal,
1158 const char *realm)
1160 krb5_error_code ret;
1162 if ((ret = krb5_principal_set_realm(context, principal, realm)) != 0)
1163 krb5_err(context, 1, ret, "krb5_principal_set_realm");
1166 static void
1167 parse_name_realm(krb5_context context,
1168 const char *name,
1169 int flags,
1170 const char *realm,
1171 krb5_principal *princ)
1173 krb5_error_code ret;
1175 if (realm)
1176 flags |= KRB5_PRINCIPAL_PARSE_NO_DEF_REALM;
1177 if ((ret = krb5_parse_name_flags(context, name, flags, princ)) != 0)
1178 krb5_err(context, 1, ret, "krb5_parse_name_flags");
1179 if (realm && krb5_principal_get_realm(context, *princ) == NULL)
1180 set_princ_realm(context, *princ, realm);
1183 static char *
1184 get_default_realm(krb5_context context)
1186 char *realm;
1187 krb5_error_code ret;
1189 if ((ret = krb5_get_default_realm(context, &realm)) != 0)
1190 krb5_err(context, 1, ret, "krb5_get_default_realm");
1191 return realm;
1194 static void
1195 get_default_principal(krb5_context context, krb5_principal *princ)
1197 krb5_error_code ret;
1199 if ((ret = krb5_get_default_principal(context, princ)) != 0)
1200 krb5_err(context, 1, ret, "krb5_get_default_principal");
1203 static char *
1204 get_user_realm(krb5_context context)
1206 krb5_error_code ret;
1207 char *user_realm = NULL;
1210 * If memory allocation fails, we don't try to use the wrong realm,
1211 * that will trigger misleading error messages complicate support.
1213 krb5_appdefault_string(context, "kinit", NULL, "user_realm", "",
1214 &user_realm);
1215 if (user_realm == NULL) {
1216 ret = krb5_enomem(context);
1217 krb5_err(context, 1, ret, "krb5_appdefault_string");
1220 if (*user_realm == 0) {
1221 free(user_realm);
1222 user_realm = NULL;
1225 return user_realm;
1228 static void
1229 get_princ(krb5_context context,
1230 krb5_principal *principal,
1231 const char *ccname,
1232 const char *name)
1234 krb5_error_code ret = 0;
1235 krb5_principal tmp;
1236 int parseflags = 0;
1237 char *user_realm;
1239 if (name == NULL) {
1240 krb5_ccache ccache = NULL;
1242 /* If credential cache provides a client principal, use that. */
1243 if (ccname)
1244 ret = krb5_cc_resolve(context, ccname, &ccache);
1245 else
1246 ret = krb5_cc_default(context, &ccache);
1247 if (ret == 0)
1248 ret = krb5_cc_get_principal(context, ccache, principal);
1249 krb5_cc_close(context, ccache);
1250 if (ret == 0)
1251 return;
1254 user_realm = get_user_realm(context);
1256 if (name) {
1257 if (enterprise_flag)
1258 parseflags |= KRB5_PRINCIPAL_PARSE_ENTERPRISE;
1260 parse_name_realm(context, name, parseflags, user_realm, &tmp);
1262 if (user_realm && krb5_principal_get_num_comp(context, tmp) > 1) {
1263 /* Principal is instance qualified, reparse with default realm. */
1264 krb5_free_principal(context, tmp);
1265 parse_name_realm(context, name, parseflags, NULL, principal);
1266 } else {
1267 *principal = tmp;
1269 } else {
1270 get_default_principal(context, principal);
1271 if (user_realm)
1272 set_princ_realm(context, *principal, user_realm);
1275 if (user_realm)
1276 free(user_realm);
1279 static void
1280 get_princ_kt(krb5_context context,
1281 krb5_principal *principal,
1282 char *name)
1284 krb5_error_code ret;
1285 krb5_principal tmp;
1286 krb5_ccache ccache;
1287 krb5_kt_cursor cursor;
1288 krb5_keytab_entry entry;
1289 char *def_realm;
1291 if (name == NULL) {
1293 * If the credential cache exists and specifies a client principal,
1294 * use that.
1296 if (krb5_cc_default(context, &ccache) == 0) {
1297 ret = krb5_cc_get_principal(context, ccache, principal);
1298 krb5_cc_close(context, ccache);
1299 if (ret == 0)
1300 return;
1304 if (name) {
1305 /* If the principal specifies an explicit realm, just use that. */
1306 int parseflags = KRB5_PRINCIPAL_PARSE_NO_DEF_REALM;
1308 parse_name_realm(context, name, parseflags, NULL, &tmp);
1309 if (krb5_principal_get_realm(context, tmp) != NULL) {
1310 *principal = tmp;
1311 return;
1313 } else {
1314 /* Otherwise, search keytab for bare name of the default principal. */
1315 get_default_principal(context, &tmp);
1316 set_princ_realm(context, tmp, NULL);
1319 def_realm = get_default_realm(context);
1321 ret = krb5_kt_start_seq_get(context, kt, &cursor);
1322 if (ret)
1323 krb5_err(context, 1, ret, "krb5_kt_start_seq_get");
1325 while (ret == 0 &&
1326 krb5_kt_next_entry(context, kt, &entry, &cursor) == 0) {
1327 const char *realm;
1329 if (!krb5_principal_compare_any_realm(context, tmp, entry.principal))
1330 continue;
1331 if (*principal &&
1332 krb5_principal_compare(context, *principal, entry.principal))
1333 continue;
1334 /* The default realm takes precedence */
1335 realm = krb5_principal_get_realm(context, entry.principal);
1336 if (*principal && strcmp(def_realm, realm) == 0) {
1337 krb5_free_principal(context, *principal);
1338 ret = krb5_copy_principal(context, entry.principal, principal);
1339 break;
1341 if (!*principal)
1342 ret = krb5_copy_principal(context, entry.principal, principal);
1344 if (ret != 0 || (ret = krb5_kt_end_seq_get(context, kt, &cursor)) != 0)
1345 krb5_err(context, 1, ret, "get_princ_kt");
1346 if (!*principal) {
1347 if (name)
1348 parse_name_realm(context, name, 0, NULL, principal);
1349 else
1350 krb5_err(context, 1, KRB5_CC_NOTFOUND, "get_princ_kt");
1353 krb5_free_principal(context, tmp);
1354 free(def_realm);
1358 main(int argc, char **argv)
1360 krb5_error_code ret;
1361 krb5_context context;
1362 krb5_ccache ccache;
1363 krb5_principal principal = NULL;
1364 int optidx = 0;
1365 krb5_deltat ticket_life = 0;
1366 #ifdef HAVE_SIGACTION
1367 struct sigaction sa;
1368 #endif
1369 krb5_boolean unique_ccache = FALSE;
1370 krb5_boolean historical_anon_pkinit = FALSE;
1371 int anonymous_pkinit = FALSE;
1373 setprogname(argv[0]);
1375 setlocale(LC_ALL, "");
1376 bindtextdomain("heimdal_kuser", HEIMDAL_LOCALEDIR);
1377 textdomain("heimdal_kuser");
1379 ret = krb5_init_context(&context);
1380 if (ret == KRB5_CONFIG_BADFORMAT)
1381 errx(1, "krb5_init_context failed to parse configuration file");
1382 else if (ret)
1383 errx(1, "krb5_init_context failed: %d", ret);
1385 if (getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
1386 usage(1);
1388 if (help_flag)
1389 usage(0);
1391 if (version_flag) {
1392 print_version(NULL);
1393 exit(0);
1396 argc -= optidx;
1397 argv += optidx;
1399 krb5_appdefault_boolean(context, "kinit", NULL, "historical_anon_pkinit",
1400 FALSE, &historical_anon_pkinit);
1403 * Open the keytab now, we use the keytab to determine the principal's
1404 * realm when the requested principal has no realm.
1406 if (use_keytab || keytab_str) {
1407 if (keytab_str)
1408 ret = krb5_kt_resolve(context, keytab_str, &kt);
1409 else
1410 ret = krb5_kt_default(context, &kt);
1411 if (ret)
1412 krb5_err(context, 1, ret, "resolving keytab");
1415 if (pk_enterprise_flag) {
1416 ret = krb5_pk_enterprise_cert(context, pk_user_id,
1417 argv[0], &principal,
1418 &ent_user_id);
1419 if (ret)
1420 krb5_err(context, 1, ret, "krb5_pk_enterprise_certs");
1422 pk_user_id = NULL;
1424 } else if (anonymous_flag && argc && argv[0][0] == '@') {
1425 /* If principal argument as @REALM, try anonymous PKINIT */
1427 ret = krb5_make_principal(context, &principal, &argv[0][1],
1428 KRB5_WELLKNOWN_NAME, KRB5_ANON_NAME,
1429 NULL);
1430 if (ret)
1431 krb5_err(context, 1, ret, "krb5_make_principal");
1432 krb5_principal_set_type(context, principal, KRB5_NT_WELLKNOWN);
1433 anonymous_pkinit = TRUE;
1434 } else if (anonymous_flag && historical_anon_pkinit) {
1435 char *realm = argc == 0 ? get_default_realm(context) :
1436 argv[0][0] == '@' ? &argv[0][1] : argv[0];
1438 ret = krb5_make_principal(context, &principal, realm,
1439 KRB5_WELLKNOWN_NAME, KRB5_ANON_NAME, NULL);
1440 if (ret)
1441 krb5_err(context, 1, ret, "krb5_make_principal");
1442 krb5_principal_set_type(context, principal, KRB5_NT_WELLKNOWN);
1443 anonymous_pkinit = TRUE;
1444 } else if (use_keytab || keytab_str) {
1445 get_princ_kt(context, &principal, argv[0]);
1446 } else {
1447 get_princ(context, &principal, cred_cache, argv[0]);
1450 if (fcache_version)
1451 krb5_set_fcache_version(context, fcache_version);
1453 if (renewable_flag == -1)
1454 /* this seems somewhat pointless, but whatever */
1455 krb5_appdefault_boolean(context, "kinit",
1456 krb5_principal_get_realm(context, principal),
1457 "renewable", FALSE, &renewable_flag);
1458 if (do_afslog == -1)
1459 krb5_appdefault_boolean(context, "kinit",
1460 krb5_principal_get_realm(context, principal),
1461 "afslog", TRUE, &do_afslog);
1464 * Cases:
1466 * - use the given ccache
1467 * - use a new unique ccache for running a command with (in this case we
1468 * get to set KRB5CCNAME, so a new unique ccache makes sense)
1469 * - use the default ccache for the given principal and maybe later switch
1470 * the collection's default/primary to it
1472 * The important thing is that, except for the case where we're running a
1473 * command, we _can't set KRB5CCNAME_, and we can't expect the user to read
1474 * our output and figure out to set it (we could have an output-for-shell-
1475 * eval mode, like ssh-agent and such, but we don't). Therefore, in all
1476 * cases where we can't set KRB5CCNAME we must do something that makes
1477 * sense to the user, and that is to either initialize a given ccache, use
1478 * the default, or use a subsidiary ccache named after the principal whose
1479 * creds we're initializing.
1481 if (cred_cache) {
1482 /* Use the given ccache */
1483 ret = krb5_cc_resolve(context, cred_cache, &ccache);
1484 } else if (argc > 1) {
1485 char s[1024];
1488 * A command was given, so use a new unique ccache (and destroy it
1489 * later).
1491 ret = krb5_cc_new_unique(context, NULL, NULL, &ccache);
1492 if (ret)
1493 krb5_err(context, 1, ret, "creating cred cache");
1494 snprintf(s, sizeof(s), "%s:%s",
1495 krb5_cc_get_type(context, ccache),
1496 krb5_cc_get_name(context, ccache));
1497 setenv("KRB5CCNAME", s, 1);
1498 unique_ccache = TRUE;
1499 } else if (default_for) {
1500 ret = krb5_cc_default_for(context, principal, &ccache);
1501 if (switch_cache_flags == -1)
1502 switch_cache_flags = 0;
1503 } else {
1504 ret = krb5_cc_default(context, &ccache);
1505 if (switch_cache_flags == -1)
1506 switch_cache_flags = 0;
1509 if (switch_cache_flags == -1)
1510 switch_cache_flags = 1;
1512 if (ret)
1513 krb5_err(context, 1, ret, N_("resolving credentials cache", ""));
1515 #ifndef NO_AFS
1516 if (argc > 1 && k_hasafs())
1517 k_setpag();
1518 #endif
1520 if (lifetime) {
1521 int tmp = parse_time(lifetime, "s");
1522 if (tmp < 0)
1523 errx(1, N_("unparsable time: %s", ""), lifetime);
1525 ticket_life = tmp;
1528 if (addrs_flag == 0 && extra_addresses.num_strings > 0)
1529 krb5_errx(context, 1,
1530 N_("specifying both extra addresses and "
1531 "no addresses makes no sense", ""));
1533 int i;
1534 krb5_addresses addresses;
1535 memset(&addresses, 0, sizeof(addresses));
1536 for(i = 0; i < extra_addresses.num_strings; i++) {
1537 ret = krb5_parse_address(context, extra_addresses.strings[i],
1538 &addresses);
1539 if (ret == 0) {
1540 krb5_add_extra_addresses(context, &addresses);
1541 krb5_free_addresses(context, &addresses);
1544 free_getarg_strings(&extra_addresses);
1547 if (renew_flag || validate_flag) {
1548 ret = renew_validate(context, renew_flag, validate_flag,
1549 &ccache, principal,
1550 default_for ? TRUE : FALSE, server_str,
1551 ticket_life);
1553 #ifndef NO_AFS
1554 if (ret == 0 && server_str == NULL && do_afslog && k_hasafs())
1555 krb5_afslog(context, ccache, NULL, NULL);
1556 #endif
1558 if (unique_ccache)
1559 krb5_cc_destroy(context, ccache);
1560 exit(ret != 0);
1563 ret = get_new_tickets(context, principal, ccache, ticket_life,
1564 1, anonymous_pkinit);
1565 if (ret) {
1566 if (unique_ccache)
1567 krb5_cc_destroy(context, ccache);
1568 exit(1);
1571 #ifndef NO_AFS
1572 if (ret == 0 && server_str == NULL && do_afslog && k_hasafs())
1573 krb5_afslog(context, ccache, NULL, NULL);
1574 #endif
1576 if (argc > 1) {
1577 struct renew_ctx ctx;
1578 time_t timeout;
1580 timeout = ticket_lifetime(context, ccache, principal,
1581 server_str, NULL) / 2;
1583 ctx.context = context;
1584 ctx.ccache = ccache;
1585 ctx.principal = principal;
1586 ctx.ticket_life = ticket_life;
1587 ctx.timeout = timeout;
1589 #ifdef HAVE_SIGACTION
1590 memset(&sa, 0, sizeof(sa));
1591 sigemptyset(&sa.sa_mask);
1592 sa.sa_handler = handle_siginfo;
1594 sigaction(SIGINFO, &sa, NULL);
1595 #endif
1597 ret = simple_execvp_timed(argv[1], argv+1,
1598 renew_func, &ctx, timeout);
1599 #define EX_NOEXEC 126
1600 #define EX_NOTFOUND 127
1601 if (ret == EX_NOEXEC)
1602 krb5_warnx(context, N_("permission denied: %s", ""), argv[1]);
1603 else if (ret == EX_NOTFOUND)
1604 krb5_warnx(context, N_("command not found: %s", ""), argv[1]);
1606 krb5_cc_destroy(context, ccache);
1607 #ifndef NO_AFS
1608 if (k_hasafs())
1609 k_unlog();
1610 #endif
1611 } else {
1612 krb5_cc_close(context, ccache);
1613 ret = 0;
1615 krb5_free_principal(context, principal);
1616 if (kt)
1617 krb5_kt_close(context, kt);
1618 krb5_free_context(context);
1619 return ret;