kuser: kinit renew_func do not update expire if no new tickets
[heimdal.git] / kuser / kinit.c
blob01cb8ed1a41b59a9d3bba1496b6a3a72f41cc816
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"
37 #undef HC_DEPRECATED_CRYPTO
38 #include <krb5_locl.h>
40 #ifdef HAVE_FRAMEWORK_SECURITY
41 #include <Security/Security.h>
42 #endif
44 #ifndef NO_NTLM
45 #include "heimntlm.h"
46 #endif
48 #ifndef SIGINFO
49 #define SIGINFO SIGUSR1
50 #endif
52 int forwardable_flag = -1;
53 int proxiable_flag = -1;
54 int renewable_flag = -1;
55 int renew_flag = 0;
56 int pac_flag = -1;
57 int validate_flag = 0;
58 int version_flag = 0;
59 int help_flag = 0;
60 int addrs_flag = -1;
61 struct getarg_strings extra_addresses;
62 int anonymous_flag = 0;
63 char *lifetime = NULL;
64 char *renew_life = NULL;
65 char *server_str = NULL;
66 static krb5_principal tgs_service;
67 char *cred_cache = NULL;
68 char *start_str = NULL;
69 static int default_for = 0;
70 static int switch_cache_flags = -1;
71 struct getarg_strings etype_str;
72 int use_keytab = 0;
73 char *keytab_str = NULL;
74 static krb5_keytab kt = NULL;
75 int do_afslog = -1;
76 int fcache_version;
77 char *password_file = NULL;
78 char *pk_user_id = NULL;
79 int pk_enterprise_flag = 0;
80 struct hx509_certs_data *ent_user_id = NULL;
81 char *pk_x509_anchors = NULL;
82 int pk_use_enckey = 0;
83 int pk_anon_fast_armor = -1;
84 char *gss_preauth_mech = NULL;
85 char *gss_preauth_name = NULL;
86 char *kdc_hostname = NULL;
87 static int canonicalize_flag = 0;
88 static int enterprise_flag = 0;
89 static int ok_as_delegate_flag = 0;
90 static char *fast_armor_cache_string = NULL;
91 static int use_referrals_flag = 0;
92 static int windows_flag = 0;
93 #ifndef NO_NTLM
94 static char *ntlm_domain;
95 #endif
98 static struct getargs args[] = {
100 * used by MIT
101 * a: ~A
102 * V: verbose
103 * F: ~f
104 * P: ~p
105 * C: v4 cache name?
106 * 5:
108 * old flags
109 * 4:
110 * 9:
112 { "afslog", 0 , arg_flag, &do_afslog,
113 NP_("obtain afs tokens", ""), NULL },
115 { "cache", 'c', arg_string, &cred_cache,
116 NP_("credentials cache", ""), "cachename" },
118 { "forwardable", 'F', arg_negative_flag, &forwardable_flag,
119 NP_("get tickets not forwardable", ""), NULL },
121 { NULL, 'f', arg_flag, &forwardable_flag,
122 NP_("get forwardable tickets", ""), NULL },
124 { "keytab", 't', arg_string, &keytab_str,
125 NP_("keytab to use", ""), "keytabname" },
127 { "lifetime", 'l', arg_string, &lifetime,
128 NP_("lifetime of tickets", ""), "time" },
130 { "proxiable", 'p', arg_flag, &proxiable_flag,
131 NP_("get proxiable tickets", ""), NULL },
133 { "renew", 'R', arg_flag, &renew_flag,
134 NP_("renew TGT", ""), NULL },
136 { "renewable", 0, arg_flag, &renewable_flag,
137 NP_("get renewable tickets", ""), NULL },
139 { "renewable-life", 'r', arg_string, &renew_life,
140 NP_("renewable lifetime of tickets", ""), "time" },
142 { "server", 'S', arg_string, &server_str,
143 NP_("server to get ticket for", ""), "principal" },
145 { "start-time", 's', arg_string, &start_str,
146 NP_("when ticket gets valid", ""), "time" },
148 { "use-keytab", 'k', arg_flag, &use_keytab,
149 NP_("get key from keytab", ""), NULL },
151 { "validate", 'v', arg_flag, &validate_flag,
152 NP_("validate TGT", ""), NULL },
154 { "enctypes", 'e', arg_strings, &etype_str,
155 NP_("encryption types to use", ""), "enctypes" },
157 { "fcache-version", 0, arg_integer, &fcache_version,
158 NP_("file cache version to create", ""), NULL },
160 { "addresses", 'A', arg_negative_flag, &addrs_flag,
161 NP_("request a ticket with no addresses", ""), NULL },
163 { "extra-addresses",'a', arg_strings, &extra_addresses,
164 NP_("include these extra addresses", ""), "addresses" },
166 { "anonymous", 'n', arg_flag, &anonymous_flag,
167 NP_("request an anonymous ticket", ""), NULL },
169 { "request-pac", 0, arg_flag, &pac_flag,
170 NP_("request a Windows PAC", ""), NULL },
172 { "password-file", 0, arg_string, &password_file,
173 NP_("read the password from a file", ""), NULL },
175 { "canonicalize",0, arg_flag, &canonicalize_flag,
176 NP_("canonicalize client principal", ""), NULL },
178 { "enterprise",0, arg_flag, &enterprise_flag,
179 NP_("parse principal as a KRB5-NT-ENTERPRISE name", ""), NULL },
180 #ifdef PKINIT
181 { "pk-enterprise", 0, arg_flag, &pk_enterprise_flag,
182 NP_("use enterprise name from certificate", ""), NULL },
184 { "pk-user", 'C', arg_string, &pk_user_id,
185 NP_("principal's public/private/certificate identifier", ""), "id" },
187 { "x509-anchors", 'D', arg_string, &pk_x509_anchors,
188 NP_("directory with CA certificates", ""), "directory" },
190 { "pk-use-enckey", 0, arg_flag, &pk_use_enckey,
191 NP_("Use RSA encrypted reply (instead of DH)", ""), NULL },
193 { "pk-anon-fast-armor", 0, arg_flag, &pk_anon_fast_armor,
194 NP_("use unauthenticated anonymous PKINIT as FAST armor", ""), NULL },
195 #endif
197 { "gss-mech", 0, arg_string, &gss_preauth_mech,
198 NP_("use GSS mechanism for pre-authentication", ""), NULL },
200 { "gss-name", 0, arg_string, &gss_preauth_name,
201 NP_("use distinct GSS identity for pre-authentication", ""), NULL },
203 { "kdc-hostname", 0, arg_string, &kdc_hostname,
204 NP_("KDC host name", ""), "hostname" },
206 #ifndef NO_NTLM
207 { "ntlm-domain", 0, arg_string, &ntlm_domain,
208 NP_("NTLM domain", ""), "domain" },
209 #endif
211 { "change-default", 0, arg_negative_flag, &switch_cache_flags,
212 NP_("switch the default cache to the new credentials cache", ""), NULL },
214 { "default-for-principal", 0, arg_flag, &default_for,
215 NP_("Use a default cache appropriate for the client principal", ""), NULL },
217 { "ok-as-delegate", 0, arg_flag, &ok_as_delegate_flag,
218 NP_("honor ok-as-delegate on tickets", ""), NULL },
220 { "fast-armor-cache", 0, arg_string, &fast_armor_cache_string,
221 NP_("use this credential cache as FAST armor cache", ""), "cache" },
223 { "use-referrals", 0, arg_flag, &use_referrals_flag,
224 NP_("only use referrals, no dns canalisation", ""), NULL },
226 { "windows", 0, arg_flag, &windows_flag,
227 NP_("get windows behavior", ""), NULL },
229 { "version", 0, arg_flag, &version_flag, NULL, NULL },
230 { "help", 0, arg_flag, &help_flag, NULL, NULL }
233 static char *
234 get_default_realm(krb5_context context);
236 static void
237 usage(int ret)
239 arg_printusage_i18n(args, sizeof(args)/sizeof(*args), N_("Usage: ", ""),
240 NULL, "[principal [command]]", getarg_i18n);
241 exit(ret);
244 static krb5_error_code
245 tgs_principal(krb5_context context,
246 krb5_ccache cache,
247 krb5_principal client,
248 krb5_const_realm tgs_realm,
249 krb5_principal *out_princ)
251 krb5_error_code ret;
252 krb5_principal tgs_princ;
253 krb5_creds creds;
254 krb5_creds *tick;
255 krb5_flags options;
257 ret = krb5_make_principal(context, &tgs_princ, tgs_realm,
258 KRB5_TGS_NAME, tgs_realm, NULL);
259 if (ret)
260 return ret;
263 * Don't fail-over to a different realm just because a TGT expired
265 options = KRB5_GC_CACHED | KRB5_GC_EXPIRED_OK;
267 memset(&creds, 0, sizeof(creds));
268 creds.client = client;
269 creds.server = tgs_princ;
270 ret = krb5_get_credentials(context, options, cache, &creds, &tick);
271 if (ret == 0) {
272 krb5_free_creds(context, tick);
273 *out_princ = tgs_princ;
274 } else {
275 krb5_free_principal(context, tgs_princ);
278 return ret;
283 * Try TGS specified with '-S',
284 * then TGS of client realm,
285 * then if fallback is FALSE: fail,
286 * otherwise try TGS of default realm,
287 * and finally first TGT in ccache.
289 static krb5_error_code
290 get_server(krb5_context context,
291 krb5_ccache cache,
292 krb5_principal client,
293 const char *server,
294 krb5_boolean fallback,
295 krb5_principal *princ)
297 krb5_error_code ret = 0;
298 krb5_const_realm realm;
299 krb5_realm def_realm;
300 krb5_cc_cursor cursor;
301 krb5_creds creds;
302 const char *pcomp;
304 if (tgs_service)
305 goto done;
307 if (server) {
308 ret = krb5_parse_name(context, server, &tgs_service);
309 goto done;
312 /* Try the client realm first */
313 realm = krb5_principal_get_realm(context, client);
314 ret = tgs_principal(context, cache, client, realm, &tgs_service);
315 if (ret == 0 || ret != KRB5_CC_NOTFOUND)
316 goto done;
318 if (!fallback)
319 return ret;
321 /* Next try the default realm */
322 ret = krb5_get_default_realm(context, &def_realm);
323 if (ret)
324 return ret;
325 ret = tgs_principal(context, cache, client, def_realm, &tgs_service);
326 free(def_realm);
327 if (ret == 0 || ret != KRB5_CC_NOTFOUND)
328 goto done;
330 /* Finally try the first TGT with instance == realm in the cache */
331 ret = krb5_cc_start_seq_get(context, cache, &cursor);
332 if (ret)
333 return ret;
335 for (/**/; ret == 0; krb5_free_cred_contents (context, &creds)) {
337 ret = krb5_cc_next_cred(context, cache, &cursor, &creds);
338 if (ret)
339 break;
340 if (creds.server->name.name_string.len != 2)
341 continue;
342 pcomp = krb5_principal_get_comp_string(context, creds.server, 0);
343 if (strcmp(pcomp, KRB5_TGS_NAME) != 0)
344 continue;
345 realm = krb5_principal_get_realm(context, creds.server);
346 pcomp = krb5_principal_get_comp_string(context, creds.server, 1);
347 if (strcmp(realm, pcomp) != 0)
348 continue;
349 ret = krb5_copy_principal(context, creds.server, &tgs_service);
350 break;
352 if (ret == KRB5_CC_END) {
353 ret = KRB5_CC_NOTFOUND;
354 krb5_set_error_message(context, ret,
355 N_("Credential cache contains no TGTs", ""));
357 krb5_cc_end_seq_get(context, cache, &cursor);
359 done:
360 if (!ret)
361 ret = krb5_copy_principal(context, tgs_service, princ);
362 return ret;
365 static krb5_error_code
366 copy_configs(krb5_context context,
367 krb5_ccache dst,
368 krb5_ccache src,
369 krb5_principal start_ticket_server)
371 krb5_error_code ret;
372 const char *cfg_names[] = {"realm-config", "FriendlyName", "anon_pkinit_realm", NULL};
373 const char *cfg_names_w_pname[] = {"fast_avail", NULL};
374 krb5_data cfg_data;
375 size_t i;
377 for (i = 0; cfg_names[i]; i++) {
378 ret = krb5_cc_get_config(context, src, NULL, cfg_names[i], &cfg_data);
379 if (ret == KRB5_CC_NOTFOUND || ret == KRB5_CC_END) {
380 continue;
381 } else if (ret) {
382 krb5_warn(context, ret, "krb5_cc_get_config");
383 return ret;
385 ret = krb5_cc_set_config(context, dst, NULL, cfg_names[i], &cfg_data);
386 if (ret)
387 krb5_warn(context, ret, "krb5_cc_set_config");
389 for (i = 0; start_ticket_server && cfg_names_w_pname[i]; i++) {
390 ret = krb5_cc_get_config(context, src, start_ticket_server,
391 cfg_names_w_pname[i], &cfg_data);
392 if (ret == KRB5_CC_NOTFOUND || ret == KRB5_CC_END) {
393 continue;
394 } else if (ret) {
395 krb5_warn(context, ret, "krb5_cc_get_config");
396 return ret;
398 ret = krb5_cc_set_config(context, dst, start_ticket_server,
399 cfg_names_w_pname[i], &cfg_data);
400 if (ret && ret != KRB5_CC_NOTFOUND)
401 krb5_warn(context, ret, "krb5_cc_set_config");
404 * We don't copy cc configs for any other principals though (mostly
405 * those are per-target time offsets and the like, so it's bad to
406 * lose them, but hardly the end of the world, and as they may not
407 * expire anyways, it's good to let them go).
409 return 0;
412 static krb5_error_code
413 get_anon_pkinit_tgs_name(krb5_context context,
414 krb5_ccache ccache,
415 krb5_principal *tgs_name)
417 krb5_error_code ret;
418 krb5_data data;
419 char *realm;
421 ret = krb5_cc_get_config(context, ccache, NULL, "anon_pkinit_realm", &data);
422 if (ret == 0)
423 realm = strndup(data.data, data.length);
424 else
425 realm = get_default_realm(context);
427 krb5_data_free(&data);
429 if (realm == NULL)
430 return krb5_enomem(context);
432 ret = krb5_make_principal(context, tgs_name, realm,
433 KRB5_TGS_NAME, realm, NULL);
435 free(realm);
437 return ret;
440 static krb5_error_code
441 renew_validate(krb5_context context,
442 int renew,
443 int validate,
444 krb5_ccache *cachep,
445 krb5_const_principal principal,
446 krb5_boolean cache_is_default_for,
447 const char *server,
448 krb5_deltat life)
450 krb5_error_code ret;
451 krb5_ccache tempccache = NULL;
452 krb5_ccache cache = *cachep;
453 krb5_creds in, *out = NULL;
454 krb5_kdc_flags flags;
456 memset(&in, 0, sizeof(in));
458 ret = krb5_cc_get_principal(context, cache, &in.client);
459 if (ret && cache_is_default_for && principal) {
460 krb5_error_code ret2;
461 krb5_ccache def_ccache = NULL;
463 ret2 = krb5_cc_default(context, &def_ccache);
464 if (ret2 == 0)
465 ret2 = krb5_cc_get_principal(context, def_ccache, &in.client);
466 if (ret2 == 0 &&
467 krb5_principal_compare(context, principal, in.client)) {
468 krb5_cc_close(context, *cachep);
469 cache = *cachep = def_ccache;
470 def_ccache = NULL;
471 ret = 0;
473 krb5_cc_close(context, def_ccache);
475 if (ret) {
476 krb5_warn(context, ret, "krb5_cc_get_principal");
477 return ret;
480 if (principal && !krb5_principal_compare(context, principal, in.client)) {
481 char *ccname = NULL;
483 (void) krb5_cc_get_full_name(context, cache, &ccname);
484 krb5_errx(context, 1, "Credentials in cache %s do not match requested "
485 "principal", ccname ? ccname : "requested");
486 free(ccname);
489 if (server == NULL &&
490 krb5_principal_is_anonymous(context, in.client,
491 KRB5_ANON_MATCH_UNAUTHENTICATED))
492 ret = get_anon_pkinit_tgs_name(context, cache, &in.server);
493 else
494 ret = get_server(context, cache, in.client, server, TRUE, &in.server);
495 if (ret) {
496 krb5_warn(context, ret, "get_server");
497 goto out;
500 if (renew) {
502 * no need to check the error here, it's only to be
503 * friendly to the user
505 (void) krb5_get_credentials(context, KRB5_GC_CACHED, cache, &in, &out);
508 flags.i = 0;
509 flags.b.renewable = flags.b.renew = renew;
510 flags.b.validate = validate;
512 if (forwardable_flag != -1)
513 flags.b.forwardable = forwardable_flag;
514 else if (out)
515 flags.b.forwardable = out->flags.b.forwardable;
517 if (proxiable_flag != -1)
518 flags.b.proxiable = proxiable_flag;
519 else if (out)
520 flags.b.proxiable = out->flags.b.proxiable;
522 if (anonymous_flag)
523 flags.b.request_anonymous = anonymous_flag;
524 if (life)
525 in.times.endtime = time(NULL) + life;
527 if (out) {
528 krb5_free_creds(context, out);
529 out = NULL;
533 ret = krb5_get_kdc_cred(context,
534 cache,
535 flags,
536 NULL,
537 NULL,
538 &in,
539 &out);
540 if (ret) {
541 krb5_warn(context, ret, "krb5_get_kdc_cred");
542 goto out;
545 ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, cache),
546 NULL, &tempccache);
547 if (ret) {
548 krb5_warn(context, ret, "krb5_cc_new_unique");
549 goto out;
552 ret = krb5_cc_initialize(context, tempccache, in.client);
553 if (ret) {
554 krb5_warn(context, ret, "krb5_cc_initialize");
555 goto out;
558 ret = krb5_cc_store_cred(context, tempccache, out);
559 if (ret) {
560 krb5_warn(context, ret, "krb5_cc_store_cred");
561 goto out;
565 * We want to preserve cc configs as some are security-relevant, and
566 * anyways it's the friendly thing to do.
568 ret = copy_configs(context, tempccache, cache, out->server);
569 if (ret)
570 goto out;
572 ret = krb5_cc_move(context, tempccache, cache);
573 if (ret) {
574 krb5_warn(context, ret, "krb5_cc_move");
575 goto out;
577 tempccache = NULL;
579 out:
580 if (tempccache)
581 krb5_cc_destroy(context, tempccache);
582 if (out)
583 krb5_free_creds(context, out);
584 krb5_free_cred_contents(context, &in);
585 return ret;
588 static krb5_error_code
589 make_wellknown_name(krb5_context context,
590 krb5_const_realm realm,
591 const char *instance,
592 krb5_principal *principal)
594 krb5_error_code ret;
596 ret = krb5_make_principal(context, principal, realm,
597 KRB5_WELLKNOWN_NAME, instance, NULL);
598 if (ret == 0)
599 krb5_principal_set_type(context, *principal, KRB5_NT_WELLKNOWN);
601 return ret;
604 static krb5_error_code
605 acquire_gss_cred(krb5_context context,
606 krb5_const_principal client,
607 krb5_deltat life,
608 const char *passwd,
609 gss_cred_id_t *cred,
610 gss_OID *mech)
612 krb5_error_code ret;
613 OM_uint32 major, minor;
614 gss_name_t name = GSS_C_NO_NAME;
615 gss_key_value_element_desc cred_element;
616 gss_key_value_set_desc cred_store;
617 gss_OID_set_desc mechs;
619 *cred = GSS_C_NO_CREDENTIAL;
620 *mech = GSS_C_NO_OID;
622 if (gss_preauth_mech) {
623 *mech = gss_name_to_oid(gss_preauth_mech);
624 if (*mech == GSS_C_NO_OID)
625 return EINVAL;
628 if (gss_preauth_name) {
629 gss_buffer_desc buf;
631 buf.value = gss_preauth_name;
632 buf.length = strlen(gss_preauth_name);
634 major = gss_import_name(&minor, &buf, GSS_C_NT_USER_NAME, &name);
635 ret = _krb5_gss_map_error(major, minor);
636 } else if (!krb5_principal_is_federated(context, client)) {
637 ret = _krb5_gss_pa_unparse_name(context, client, &name);
638 } else {
640 * WELLKNOWN/FEDERATED is used a placeholder where the user
641 * did not specify either a Kerberos credential or a GSS-API
642 * initiator name. It avoids the expense of acquiring a default
643 * credential purely to interrogate the credential name.
645 name = GSS_C_NO_NAME;
646 ret = 0;
648 if (ret)
649 goto out;
651 cred_store.count = 1;
652 cred_store.elements = &cred_element;
654 if (passwd && passwd[0]) {
655 cred_element.key = "password";
656 cred_element.value = passwd;
657 } else if (keytab_str) {
658 cred_element.key = "client_keytab",
659 cred_element.value = keytab_str;
660 } else {
661 cred_store.count = 0;
664 if (*mech) {
665 mechs.count = 1;
666 mechs.elements = (gss_OID)*mech;
669 major = gss_acquire_cred_from(&minor,
670 name,
671 life ? life : GSS_C_INDEFINITE,
672 *mech ? &mechs : GSS_C_NO_OID_SET,
673 GSS_C_INITIATE,
674 &cred_store,
675 cred,
676 NULL,
677 NULL);
678 if (major != GSS_S_COMPLETE) {
679 ret = _krb5_gss_map_error(major, minor);
680 goto out;
683 out:
684 gss_release_name(&minor, &name);
685 return ret;
688 #ifndef NO_NTLM
690 static krb5_error_code
691 store_ntlmkey(krb5_context context, krb5_ccache id,
692 const char *domain, struct ntlm_buf *buf)
694 krb5_error_code ret;
695 krb5_data data;
696 char *name;
697 int aret;
699 ret = krb5_cc_get_config(context, id, NULL, "default-ntlm-domain", &data);
700 if (ret == 0) {
701 krb5_data_free(&data);
702 } else {
703 data.length = strlen(domain);
704 data.data = rk_UNCONST(domain);
705 ret = krb5_cc_set_config(context, id, NULL, "default-ntlm-domain", &data);
706 if (ret != 0)
707 return ret;
710 aret = asprintf(&name, "ntlm-key-%s", domain);
711 if (aret == -1 || name == NULL)
712 return krb5_enomem(context);
714 data.length = buf->length;
715 data.data = buf->data;
717 ret = krb5_cc_set_config(context, id, NULL, name, &data);
718 free(name);
719 return ret;
721 #endif
723 static krb5_error_code
724 get_new_tickets(krb5_context context,
725 krb5_principal principal,
726 krb5_ccache ccache,
727 krb5_deltat ticket_life,
728 int interactive,
729 int anonymous_pkinit)
731 krb5_error_code ret;
732 krb5_creds cred;
733 char passwd[256];
734 krb5_deltat start_time = 0;
735 krb5_deltat renew = 0;
736 const char *renewstr = NULL;
737 krb5_enctype *enctype = NULL;
738 krb5_ccache tempccache = NULL;
739 krb5_init_creds_context ctx = NULL;
740 krb5_get_init_creds_opt *opt = NULL;
741 krb5_prompter_fct prompter = krb5_prompter_posix;
742 gss_cred_id_t gss_cred = GSS_C_NO_CREDENTIAL;
743 gss_OID gss_mech = GSS_C_NO_OID;
744 krb5_principal federated_name = NULL;
746 #ifndef NO_NTLM
747 struct ntlm_buf ntlmkey;
748 memset(&ntlmkey, 0, sizeof(ntlmkey));
749 #endif
750 passwd[0] = '\0';
752 if (!interactive)
753 prompter = NULL;
755 if (password_file) {
756 FILE *f;
758 if (strcasecmp("STDIN", password_file) == 0)
759 f = stdin;
760 else
761 f = fopen(password_file, "r");
762 if (f == NULL) {
763 krb5_warnx(context, N_("Failed to open the password file %s", ""),
764 password_file);
765 return errno;
768 if (fgets(passwd, sizeof(passwd), f) == NULL) {
769 krb5_warnx(context, N_("Failed to read password from file %s", ""),
770 password_file);
771 if (f != stdin)
772 fclose(f);
773 return EINVAL; /* XXX Need a better error */
775 if (f != stdin)
776 fclose(f);
777 passwd[strcspn(passwd, "\n")] = '\0';
780 #ifdef HAVE_FRAMEWORK_SECURITY
781 if (passwd[0] == '\0') {
782 const char *realm;
783 OSStatus osret;
784 UInt32 length;
785 void *buffer;
786 char *name;
788 realm = krb5_principal_get_realm(context, principal);
790 ret = krb5_unparse_name_flags(context, principal,
791 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &name);
792 if (ret)
793 goto nopassword;
795 osret = SecKeychainFindGenericPassword(NULL, strlen(realm), realm,
796 strlen(name), name,
797 &length, &buffer, NULL);
798 free(name);
799 if (osret == noErr && length < sizeof(passwd) - 1) {
800 memcpy(passwd, buffer, length);
801 passwd[length] = '\0';
803 nopassword:
804 do { } while(0);
806 #endif
808 memset(&cred, 0, sizeof(cred));
810 ret = krb5_get_init_creds_opt_alloc(context, &opt);
811 if (ret) {
812 krb5_warn(context, ret, "krb5_get_init_creds_opt_alloc");
813 goto out;
816 krb5_get_init_creds_opt_set_default_flags(context, "kinit",
817 krb5_principal_get_realm(context, principal), opt);
819 if (forwardable_flag != -1)
820 krb5_get_init_creds_opt_set_forwardable(opt, forwardable_flag);
821 if (proxiable_flag != -1)
822 krb5_get_init_creds_opt_set_proxiable(opt, proxiable_flag);
823 if (anonymous_flag)
824 krb5_get_init_creds_opt_set_anonymous(opt, anonymous_flag);
825 if (pac_flag != -1)
826 krb5_get_init_creds_opt_set_pac_request(context, opt,
827 pac_flag ? TRUE : FALSE);
828 if (canonicalize_flag)
829 krb5_get_init_creds_opt_set_canonicalize(context, opt, TRUE);
830 if (pk_enterprise_flag || enterprise_flag || canonicalize_flag || windows_flag)
831 krb5_get_init_creds_opt_set_win2k(context, opt, TRUE);
832 if (pk_user_id || ent_user_id || anonymous_pkinit) {
833 if (pk_anon_fast_armor == -1)
834 pk_anon_fast_armor = 0;
835 ret = krb5_get_init_creds_opt_set_pkinit(context, opt,
836 principal,
837 pk_user_id,
838 pk_x509_anchors,
839 NULL,
840 NULL,
841 pk_use_enckey ? KRB5_GIC_OPT_PKINIT_USE_ENCKEY : 0 |
842 anonymous_pkinit ? KRB5_GIC_OPT_PKINIT_ANONYMOUS : 0,
843 prompter,
844 NULL,
845 passwd);
846 if (ret) {
847 krb5_warn(context, ret, "krb5_get_init_creds_opt_set_pkinit");
848 goto out;
850 if (ent_user_id)
851 krb5_get_init_creds_opt_set_pkinit_user_certs(context, opt, ent_user_id);
854 if (addrs_flag != -1)
855 krb5_get_init_creds_opt_set_addressless(context, opt,
856 addrs_flag ? FALSE : TRUE);
858 if (renew_life == NULL && renewable_flag)
859 renewstr = "6 months";
860 if (renew_life)
861 renewstr = renew_life;
862 if (renewstr) {
863 renew = parse_time(renewstr, "s");
864 if (renew < 0)
865 errx(1, "unparsable time: %s", renewstr);
867 krb5_get_init_creds_opt_set_renew_life(opt, renew);
870 if (ticket_life != 0)
871 krb5_get_init_creds_opt_set_tkt_life(opt, ticket_life);
873 if (start_str) {
874 int tmp = parse_time(start_str, "s");
875 if (tmp < 0)
876 errx(1, N_("unparsable time: %s", ""), start_str);
878 start_time = tmp;
881 if (etype_str.num_strings) {
882 int i;
884 enctype = malloc(etype_str.num_strings * sizeof(*enctype));
885 if (enctype == NULL)
886 errx(1, "out of memory");
887 for(i = 0; i < etype_str.num_strings; i++) {
888 ret = krb5_string_to_enctype(context,
889 etype_str.strings[i],
890 &enctype[i]);
891 if (ret)
892 errx(1, "unrecognized enctype: %s", etype_str.strings[i]);
894 krb5_get_init_creds_opt_set_etype_list(opt, enctype,
895 etype_str.num_strings);
898 if (gss_preauth_mech || gss_preauth_name) {
899 ret = acquire_gss_cred(context, principal, ticket_life,
900 passwd, &gss_cred, &gss_mech);
901 if (ret)
902 goto out;
905 * The principal specified on the command line is used as the GSS-API
906 * initiator name, unless the --gss-name option was present, in which
907 * case the initiator name is specified independently.
909 if (gss_preauth_name == NULL) {
910 krb5_const_realm realm = krb5_principal_get_realm(context, principal);
912 ret = make_wellknown_name(context, realm,
913 KRB5_FEDERATED_NAME, &federated_name);
914 if (ret)
915 goto out;
917 principal = federated_name;
921 ret = krb5_init_creds_init(context, principal, prompter, NULL, start_time, opt, &ctx);
922 if (ret) {
923 krb5_warn(context, ret, "krb5_init_creds_init");
924 goto out;
927 if (server_str) {
928 ret = krb5_init_creds_set_service(context, ctx, server_str);
929 if (ret) {
930 krb5_warn(context, ret, "krb5_init_creds_set_service");
931 goto out;
935 if (kdc_hostname) {
936 ret = krb5_init_creds_set_kdc_hostname(context, ctx, kdc_hostname);
937 if (ret) {
938 krb5_warn(context, ret, "krb5_init_creds_set_kdc_hostname");
939 goto out;
943 if (anonymous_flag && pk_anon_fast_armor == -1)
944 pk_anon_fast_armor = 0;
945 if (!gss_preauth_mech && anonymous_flag && pk_anon_fast_armor) {
946 krb5_warnx(context, N_("Ignoring --pk-anon-fast-armor because "
947 "--anonymous given", ""));
948 pk_anon_fast_armor = 0;
951 if (fast_armor_cache_string) {
952 krb5_ccache fastid = NULL;
954 if (pk_anon_fast_armor > 0)
955 krb5_errx(context, 1,
956 N_("cannot specify FAST armor cache with FAST "
957 "anonymous PKINIT option", ""));
958 pk_anon_fast_armor = 0;
960 ret = krb5_cc_resolve(context, fast_armor_cache_string, &fastid);
961 if (ret) {
962 krb5_warn(context, ret, "krb5_cc_resolve(FAST cache)");
963 goto out;
966 ret = krb5_init_creds_set_fast_ccache(context, ctx, fastid);
967 if (ret) {
968 krb5_warn(context, ret, "krb5_init_creds_set_fast_ccache");
969 goto out;
971 } else if (pk_anon_fast_armor == -1) {
972 ret = _krb5_init_creds_set_fast_anon_pkinit_optimistic(context, ctx);
973 if (ret) {
974 krb5_warn(context, ret, "_krb5_init_creds_set_fast_anon_pkinit_optimistic");
975 goto out;
977 } else if (pk_anon_fast_armor) {
978 ret = krb5_init_creds_set_fast_anon_pkinit(context, ctx);
979 if (ret) {
980 krb5_warn(context, ret, "krb5_init_creds_set_fast_anon_pkinit");
981 goto out;
985 if (gss_mech != GSS_C_NO_OID) {
986 ret = krb5_gss_set_init_creds(context, ctx, gss_cred, gss_mech);
987 if (ret) {
988 krb5_warn(context, ret, "krb5_gss_set_init_creds");
989 goto out;
991 } else if (use_keytab || keytab_str) {
992 ret = krb5_init_creds_set_keytab(context, ctx, kt);
993 if (ret) {
994 krb5_warn(context, ret, "krb5_init_creds_set_keytab");
995 goto out;
997 } else if (pk_user_id || ent_user_id ||
998 krb5_principal_is_anonymous(context, principal, KRB5_ANON_MATCH_ANY)) {
999 /* nop */;
1000 } else if (!interactive && passwd[0] == '\0') {
1001 static int already_warned = 0;
1003 if (!already_warned)
1004 krb5_warnx(context, "Not interactive, failed to get "
1005 "initial ticket");
1006 krb5_get_init_creds_opt_free(context, opt);
1007 already_warned = 1;
1008 return 0;
1009 } else {
1011 if (passwd[0] == '\0') {
1012 char *p, *prompt;
1013 int aret = 0;
1015 ret = krb5_unparse_name(context, principal, &p);
1016 if (ret)
1017 errx(1, "failed to generate passwd prompt: not enough memory");
1019 aret = asprintf(&prompt, N_("%s's Password: ", ""), p);
1020 free(p);
1021 if (aret == -1)
1022 errx(1, "failed to generate passwd prompt: not enough memory");
1024 if (UI_UTIL_read_pw_string(passwd, sizeof(passwd)-1, prompt, 0)){
1025 memset(passwd, 0, sizeof(passwd));
1026 errx(1, "failed to read password");
1028 free(prompt);
1031 if (passwd[0]) {
1032 ret = krb5_init_creds_set_password(context, ctx, passwd);
1033 if (ret) {
1034 krb5_warn(context, ret, "krb5_init_creds_set_password");
1035 goto out;
1040 ret = krb5_init_creds_get(context, ctx);
1042 #ifndef NO_NTLM
1043 if (ntlm_domain && passwd[0])
1044 heim_ntlm_nt_key(passwd, &ntlmkey);
1045 #endif
1046 memset_s(passwd, sizeof(passwd), 0, sizeof(passwd));
1048 switch(ret){
1049 case 0:
1050 break;
1051 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
1052 exit(1);
1053 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
1054 case KRB5KRB_AP_ERR_MODIFIED:
1055 case KRB5KDC_ERR_PREAUTH_FAILED:
1056 case KRB5_GET_IN_TKT_LOOP:
1057 krb5_warnx(context, N_("Password incorrect", ""));
1058 goto out;
1059 case KRB5KRB_AP_ERR_V4_REPLY:
1060 krb5_warnx(context, N_("Looks like a Kerberos 4 reply", ""));
1061 goto out;
1062 case KRB5KDC_ERR_KEY_EXPIRED:
1063 krb5_warnx(context, N_("Password expired", ""));
1064 goto out;
1065 default:
1066 krb5_warn(context, ret, "krb5_get_init_creds");
1067 goto out;
1070 krb5_process_last_request(context, opt, ctx);
1072 ret = krb5_init_creds_get_creds(context, ctx, &cred);
1073 if (ret) {
1074 krb5_warn(context, ret, "krb5_init_creds_get_creds");
1075 goto out;
1078 if (ticket_life != 0) {
1079 if (labs(cred.times.endtime - cred.times.starttime - ticket_life) > 30) {
1080 char life[64];
1081 unparse_time_approx(cred.times.endtime - cred.times.starttime,
1082 life, sizeof(life));
1083 krb5_warnx(context, N_("NOTICE: ticket lifetime is %s", ""), life);
1086 if (renew_life) {
1087 if (labs(cred.times.renew_till - cred.times.starttime - renew) > 30) {
1088 char life[64];
1089 unparse_time_approx(cred.times.renew_till - cred.times.starttime,
1090 life, sizeof(life));
1091 krb5_warnx(context,
1092 N_("NOTICE: ticket renewable lifetime is %s", ""),
1093 life);
1096 krb5_free_cred_contents(context, &cred);
1098 ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, ccache),
1099 NULL, &tempccache);
1100 if (ret) {
1101 krb5_warn(context, ret, "krb5_cc_new_unique");
1102 goto out;
1105 ret = krb5_init_creds_store(context, ctx, tempccache);
1106 if (ret) {
1107 krb5_warn(context, ret, "krb5_init_creds_store");
1108 goto out;
1111 ret = krb5_init_creds_store_config(context, ctx, tempccache);
1112 if (ret) {
1113 krb5_warn(context, ret, "krb5_init_creds_store_config");
1114 goto out;
1117 ret = krb5_init_creds_warn_user(context, ctx);
1118 if (ret) {
1119 krb5_warn(context, ret, "krb5_init_creds_warn_user");
1120 goto out;
1123 krb5_init_creds_free(context, ctx);
1124 ctx = NULL;
1126 ret = krb5_cc_move(context, tempccache, ccache);
1127 if (ret) {
1128 krb5_warn(context, ret, "krb5_cc_move");
1129 goto out;
1131 tempccache = NULL;
1133 if (switch_cache_flags)
1134 krb5_cc_switch(context, ccache);
1136 #ifndef NO_NTLM
1137 if (ntlm_domain && ntlmkey.data)
1138 store_ntlmkey(context, ccache, ntlm_domain, &ntlmkey);
1139 #endif
1141 if (ok_as_delegate_flag || windows_flag || use_referrals_flag) {
1142 unsigned char d = 0;
1143 krb5_data data;
1145 if (ok_as_delegate_flag || windows_flag)
1146 d |= 1;
1147 if (use_referrals_flag || windows_flag)
1148 d |= 2;
1150 data.length = 1;
1151 data.data = &d;
1153 krb5_cc_set_config(context, ccache, NULL, "realm-config", &data);
1156 if (anonymous_pkinit) {
1157 krb5_data data;
1159 data.length = strlen(principal->realm);
1160 data.data = principal->realm;
1162 krb5_cc_set_config(context, ccache, NULL, "anon_pkinit_realm", &data);
1165 out:
1167 OM_uint32 minor;
1168 gss_release_cred(&minor, &gss_cred);
1170 krb5_free_principal(context, federated_name);
1171 krb5_get_init_creds_opt_free(context, opt);
1172 if (ctx)
1173 krb5_init_creds_free(context, ctx);
1174 if (tempccache)
1175 krb5_cc_destroy(context, tempccache);
1176 if (enctype)
1177 free(enctype);
1179 return ret;
1182 static time_t
1183 ticket_lifetime(krb5_context context, krb5_ccache cache, krb5_principal client,
1184 const char *server, time_t *renew)
1186 krb5_creds in_cred, *cred;
1187 krb5_error_code ret;
1188 time_t timeout;
1189 time_t curtime;
1191 memset(&in_cred, 0, sizeof(in_cred));
1193 if (renew != NULL)
1194 *renew = 0;
1196 ret = krb5_cc_get_principal(context, cache, &in_cred.client);
1197 if (ret) {
1198 krb5_warn(context, ret, "krb5_cc_get_principal");
1199 return 0;
1202 /* Determine TGS principal without fallback */
1203 ret = get_server(context, cache, in_cred.client, server, FALSE,
1204 &in_cred.server);
1205 if (ret) {
1206 krb5_free_principal(context, in_cred.client);
1207 krb5_warn(context, ret, "get_server");
1208 return 0;
1211 ret = krb5_get_credentials(context, KRB5_GC_CACHED,
1212 cache, &in_cred, &cred);
1213 krb5_free_principal(context, in_cred.client);
1214 krb5_free_principal(context, in_cred.server);
1215 if (ret) {
1216 krb5_warn(context, ret, "krb5_get_credentials");
1217 return 0;
1219 curtime = time(NULL);
1220 timeout = cred->times.endtime - curtime;
1221 if (timeout < 0)
1222 timeout = 0;
1223 if (renew) {
1224 *renew = cred->times.renew_till - curtime;
1225 if (*renew < 0)
1226 *renew = 0;
1228 krb5_free_creds(context, cred);
1229 return timeout;
1232 static time_t expire;
1234 static char siginfo_msg[1024] = "No credentials\n";
1236 static void
1237 update_siginfo_msg(time_t exp, const char *srv)
1239 /* Note that exp is relative time */
1240 memset(siginfo_msg, 0, sizeof(siginfo_msg));
1241 memcpy(&siginfo_msg, "Updating...\n", sizeof("Updating...\n"));
1242 if (exp) {
1243 if (srv == NULL) {
1244 snprintf(siginfo_msg, sizeof(siginfo_msg),
1245 N_("kinit: TGT expires in %llu seconds\n", ""),
1246 (unsigned long long)expire);
1247 } else {
1248 snprintf(siginfo_msg, sizeof(siginfo_msg),
1249 N_("kinit: Ticket for %s expired\n", ""), srv);
1251 return;
1254 /* Expired creds */
1255 if (srv == NULL) {
1256 snprintf(siginfo_msg, sizeof(siginfo_msg),
1257 N_("kinit: TGT expired\n", ""));
1258 } else {
1259 snprintf(siginfo_msg, sizeof(siginfo_msg),
1260 N_("kinit: Ticket for %s expired\n", ""), srv);
1264 #ifdef HAVE_SIGACTION
1265 static void
1266 handle_siginfo(int sig)
1268 struct iovec iov[2];
1270 iov[0].iov_base = rk_UNCONST(siginfo_msg);
1271 iov[0].iov_len = strlen(siginfo_msg);
1272 iov[1].iov_base = "\n";
1273 iov[1].iov_len = 1;
1275 writev(STDERR_FILENO, iov, sizeof(iov)/sizeof(iov[0]));
1277 #endif
1279 struct renew_ctx {
1280 krb5_context context;
1281 krb5_ccache ccache;
1282 krb5_principal principal;
1283 krb5_deltat ticket_life;
1284 krb5_deltat timeout;
1285 int anonymous_pkinit;
1288 static time_t
1289 renew_func(void *ptr)
1291 krb5_error_code ret;
1292 struct renew_ctx *ctx = ptr;
1293 time_t renew_expire;
1294 static time_t exp_delay = 1;
1297 * NOTE: We count on the ccache implementation to notice changes to the
1298 * actual ccache filesystem/whatever objects. There should be no ccache
1299 * types for which this is not the case, but it might not hurt to
1300 * re-krb5_cc_resolve() after each successful renew_validate()/
1301 * get_new_tickets() call.
1304 expire = ticket_lifetime(ctx->context, ctx->ccache, ctx->principal,
1305 server_str, &renew_expire);
1308 * When a keytab is available to obtain new tickets, if we are within
1309 * half of the original ticket lifetime of the renew limit, get a new
1310 * TGT instead of renewing the existing TGT. Note, ctx->ticket_life
1311 * is zero by default (without a '-l' option) and cannot be used to
1312 * set the time scale on which we decide whether we're "close to the
1313 * renew limit".
1315 if (use_keytab || keytab_str)
1316 expire += ctx->timeout;
1317 if (renew_expire > expire) {
1318 ret = renew_validate(ctx->context, 1, validate_flag, &ctx->ccache,
1319 NULL, FALSE, server_str, ctx->ticket_life);
1320 } else {
1321 ret = get_new_tickets(ctx->context, ctx->principal, ctx->ccache,
1322 ctx->ticket_life, 0, ctx->anonymous_pkinit);
1324 if (ret == 0) {
1325 expire = ticket_lifetime(ctx->context, ctx->ccache, ctx->principal,
1326 server_str, &renew_expire);
1328 #ifndef NO_AFS
1329 if (server_str == NULL && do_afslog && k_hasafs())
1330 krb5_afslog(ctx->context, ctx->ccache, NULL, NULL);
1331 #endif
1334 update_siginfo_msg(expire, server_str);
1337 * If our tickets have expired and we been able to either renew them
1338 * or obtain new tickets, then we still call this function but we use
1339 * an exponential backoff. This should take care of the case where
1340 * we are using stored credentials but the KDC has been unavailable
1341 * for some reason...
1344 if (expire < 1) {
1346 * We can't ask to keep spamming stderr but not syslog, so we warn
1347 * only once.
1349 if (exp_delay == 1) {
1350 krb5_warnx(ctx->context, N_("NOTICE: Could not renew/refresh "
1351 "tickets", ""));
1353 if (exp_delay < 7200)
1354 exp_delay += exp_delay / 2 + 1;
1355 return exp_delay;
1357 exp_delay = 1;
1359 return expire / 2 + 1;
1362 static void
1363 set_princ_realm(krb5_context context,
1364 krb5_principal principal,
1365 const char *realm)
1367 krb5_error_code ret;
1369 if ((ret = krb5_principal_set_realm(context, principal, realm)) != 0)
1370 krb5_err(context, 1, ret, "krb5_principal_set_realm");
1373 static void
1374 parse_name_realm(krb5_context context,
1375 const char *name,
1376 int flags,
1377 const char *realm,
1378 krb5_principal *princ)
1380 krb5_error_code ret;
1382 if (realm)
1383 flags |= KRB5_PRINCIPAL_PARSE_NO_DEF_REALM;
1384 if ((ret = krb5_parse_name_flags(context, name, flags, princ)) != 0)
1385 krb5_err(context, 1, ret, "krb5_parse_name_flags");
1386 if (realm && krb5_principal_get_realm(context, *princ) == NULL)
1387 set_princ_realm(context, *princ, realm);
1390 static char *
1391 get_default_realm(krb5_context context)
1393 char *realm;
1394 krb5_error_code ret;
1396 if ((ret = krb5_get_default_realm(context, &realm)) != 0)
1397 krb5_err(context, 1, ret, "krb5_get_default_realm");
1398 return realm;
1401 static void
1402 get_default_principal(krb5_context context, krb5_principal *princ)
1404 krb5_error_code ret;
1406 if ((ret = krb5_get_default_principal(context, princ)) != 0)
1407 krb5_err(context, 1, ret, "krb5_get_default_principal");
1410 static char *
1411 get_user_realm(krb5_context context)
1413 krb5_error_code ret;
1414 char *user_realm = NULL;
1417 * If memory allocation fails, we don't try to use the wrong realm,
1418 * that will trigger misleading error messages complicate support.
1420 krb5_appdefault_string(context, "kinit", NULL, "user_realm", "",
1421 &user_realm);
1422 if (user_realm == NULL) {
1423 ret = krb5_enomem(context);
1424 krb5_err(context, 1, ret, "krb5_appdefault_string");
1427 if (*user_realm == 0) {
1428 free(user_realm);
1429 user_realm = NULL;
1432 return user_realm;
1435 static void
1436 get_princ(krb5_context context,
1437 krb5_principal *principal,
1438 const char *ccname,
1439 const char *name)
1441 krb5_error_code ret = 0;
1442 krb5_principal tmp;
1443 int parseflags = 0;
1444 char *user_realm;
1446 if (name == NULL) {
1447 krb5_ccache ccache = NULL;
1449 /* If credential cache provides a client principal, use that. */
1450 if (ccname)
1451 ret = krb5_cc_resolve(context, ccname, &ccache);
1452 else
1453 ret = krb5_cc_default(context, &ccache);
1454 if (ret == 0)
1455 ret = krb5_cc_get_principal(context, ccache, principal);
1456 krb5_cc_close(context, ccache);
1457 if (ret == 0)
1458 return;
1461 user_realm = get_user_realm(context);
1463 if (name) {
1464 if (enterprise_flag)
1465 parseflags |= KRB5_PRINCIPAL_PARSE_ENTERPRISE;
1467 parse_name_realm(context, name, parseflags, user_realm, &tmp);
1469 if (user_realm && krb5_principal_get_num_comp(context, tmp) > 1) {
1470 /* Principal is instance qualified, reparse with default realm. */
1471 krb5_free_principal(context, tmp);
1472 parse_name_realm(context, name, parseflags, NULL, principal);
1473 } else {
1474 *principal = tmp;
1476 } else {
1477 get_default_principal(context, principal);
1478 if (user_realm)
1479 set_princ_realm(context, *principal, user_realm);
1482 if (user_realm)
1483 free(user_realm);
1486 static void
1487 get_princ_kt(krb5_context context,
1488 krb5_principal *principal,
1489 char *name)
1491 krb5_error_code ret;
1492 krb5_principal tmp;
1493 krb5_ccache ccache;
1494 krb5_kt_cursor cursor;
1495 krb5_keytab_entry entry;
1496 char *def_realm;
1498 if (name == NULL) {
1500 * If the credential cache exists and specifies a client principal,
1501 * use that.
1503 if (krb5_cc_default(context, &ccache) == 0) {
1504 ret = krb5_cc_get_principal(context, ccache, principal);
1505 krb5_cc_close(context, ccache);
1506 if (ret == 0)
1507 return;
1511 if (name) {
1512 /* If the principal specifies an explicit realm, just use that. */
1513 int parseflags = KRB5_PRINCIPAL_PARSE_NO_DEF_REALM;
1515 parse_name_realm(context, name, parseflags, NULL, &tmp);
1516 if (krb5_principal_get_realm(context, tmp) != NULL) {
1517 *principal = tmp;
1518 return;
1520 } else {
1521 /* Otherwise, search keytab for bare name of the default principal. */
1522 get_default_principal(context, &tmp);
1523 set_princ_realm(context, tmp, NULL);
1526 def_realm = get_default_realm(context);
1528 ret = krb5_kt_start_seq_get(context, kt, &cursor);
1529 if (ret)
1530 krb5_err(context, 1, ret, "krb5_kt_start_seq_get");
1532 while (ret == 0 &&
1533 krb5_kt_next_entry(context, kt, &entry, &cursor) == 0) {
1534 const char *realm;
1536 if (!krb5_principal_compare_any_realm(context, tmp, entry.principal))
1537 continue;
1538 if (*principal &&
1539 krb5_principal_compare(context, *principal, entry.principal))
1540 continue;
1541 /* The default realm takes precedence */
1542 realm = krb5_principal_get_realm(context, entry.principal);
1543 if (*principal && strcmp(def_realm, realm) == 0) {
1544 krb5_free_principal(context, *principal);
1545 ret = krb5_copy_principal(context, entry.principal, principal);
1546 break;
1548 if (!*principal)
1549 ret = krb5_copy_principal(context, entry.principal, principal);
1551 if (ret != 0 || (ret = krb5_kt_end_seq_get(context, kt, &cursor)) != 0)
1552 krb5_err(context, 1, ret, "get_princ_kt");
1553 if (!*principal) {
1554 if (name)
1555 parse_name_realm(context, name, 0, NULL, principal);
1556 else
1557 krb5_err(context, 1, KRB5_CC_NOTFOUND, "get_princ_kt");
1560 krb5_free_principal(context, tmp);
1561 free(def_realm);
1564 static krb5_error_code
1565 get_switched_ccache(krb5_context context,
1566 const char * type,
1567 krb5_principal principal,
1568 krb5_ccache *ccache)
1570 krb5_error_code ret;
1572 #ifdef _WIN32
1573 if (strcmp(type, "API") == 0) {
1575 * Windows stores the default ccache name in the
1576 * registry which is shared across multiple logon
1577 * sessions for the same user. The API credential
1578 * cache provides a unique name space per logon
1579 * session. Therefore there is no need to generate
1580 * a unique ccache name. Instead use the principal
1581 * name. This provides a friendlier user experience.
1583 char * unparsed_name;
1584 char * cred_cache;
1586 ret = krb5_unparse_name(context, principal,
1587 &unparsed_name);
1588 if (ret)
1589 krb5_err(context, 1, ret,
1590 N_("unparsing principal name", ""));
1592 ret = asprintf(&cred_cache, "API:%s", unparsed_name);
1593 krb5_free_unparsed_name(context, unparsed_name);
1594 if (ret == -1 || cred_cache == NULL)
1595 krb5_err(context, 1, ret,
1596 N_("building credential cache name", ""));
1598 ret = krb5_cc_resolve(context, cred_cache, ccache);
1599 free(cred_cache);
1600 } else if (strcmp(type, "MSLSA") == 0) {
1602 * The Windows MSLSA cache when it is writeable
1603 * stores tickets for multiple client principals
1604 * in a single credential cache.
1606 ret = krb5_cc_resolve(context, "MSLSA:", ccache);
1607 } else {
1608 ret = krb5_cc_new_unique(context, type, NULL, ccache);
1610 #else /* !_WIN32 */
1611 ret = krb5_cc_new_unique(context, type, NULL, ccache);
1612 #endif /* _WIN32 */
1614 return ret;
1618 main(int argc, char **argv)
1620 krb5_error_code ret;
1621 krb5_context context;
1622 krb5_ccache ccache;
1623 krb5_principal principal = NULL;
1624 int optidx = 0;
1625 krb5_deltat ticket_life = 0;
1626 #ifdef HAVE_SIGACTION
1627 struct sigaction sa;
1628 #endif
1629 krb5_boolean unique_ccache = FALSE;
1630 krb5_boolean historical_anon_pkinit = FALSE;
1631 int anonymous_pkinit = FALSE;
1633 setprogname(argv[0]);
1635 setlocale(LC_ALL, "");
1636 bindtextdomain("heimdal_kuser", HEIMDAL_LOCALEDIR);
1637 textdomain("heimdal_kuser");
1639 ret = krb5_init_context(&context);
1640 if (ret == KRB5_CONFIG_BADFORMAT)
1641 errx(1, "krb5_init_context failed to parse configuration file");
1642 else if (ret)
1643 errx(1, "krb5_init_context failed: %d", ret);
1645 if (getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
1646 usage(1);
1648 if (help_flag)
1649 usage(0);
1651 if (version_flag) {
1652 print_version(NULL);
1653 exit(0);
1656 argc -= optidx;
1657 argv += optidx;
1659 krb5_appdefault_boolean(context, "kinit", NULL, "historical_anon_pkinit",
1660 FALSE, &historical_anon_pkinit);
1663 * Open the keytab now, we use the keytab to determine the principal's
1664 * realm when the requested principal has no realm.
1666 if (use_keytab || keytab_str) {
1667 if (keytab_str)
1668 ret = krb5_kt_resolve(context, keytab_str, &kt);
1669 else
1670 ret = krb5_kt_default(context, &kt);
1671 if (ret)
1672 krb5_err(context, 1, ret, "resolving keytab");
1675 if (pk_enterprise_flag) {
1676 ret = krb5_pk_enterprise_cert(context, pk_user_id,
1677 argv[0], &principal,
1678 &ent_user_id);
1679 if (ret)
1680 krb5_err(context, 1, ret, "krb5_pk_enterprise_certs");
1682 pk_user_id = NULL;
1683 if (pk_anon_fast_armor > 0)
1684 krb5_warnx(context, N_("Ignoring --pk-anon-fast-armor "
1685 "because --pk-user given", ""));
1686 pk_anon_fast_armor = 0;
1687 } else if (argc && argv[0][0] == '@' &&
1688 (gss_preauth_mech || anonymous_flag)) {
1689 const char *instance;
1691 if (gss_preauth_mech) {
1692 instance = KRB5_FEDERATED_NAME;
1693 } else if (anonymous_flag) {
1694 instance = KRB5_ANON_NAME;
1695 anonymous_pkinit = TRUE;
1698 ret = make_wellknown_name(context, &argv[0][1], instance, &principal);
1699 if (ret)
1700 krb5_err(context, 1, ret, "make_wellknown_name");
1701 if (!gss_preauth_mech && pk_anon_fast_armor > 1) {
1702 krb5_warnx(context, N_("Ignoring --pk-anon-fast-armor "
1703 "because --anonymous given", ""));
1704 pk_anon_fast_armor = 0;
1706 } else if (anonymous_flag && historical_anon_pkinit) {
1707 char *realm = argc == 0 ? get_default_realm(context) :
1708 argv[0][0] == '@' ? &argv[0][1] : argv[0];
1710 ret = krb5_make_principal(context, &principal, realm,
1711 KRB5_WELLKNOWN_NAME, KRB5_ANON_NAME, NULL);
1712 if (ret)
1713 krb5_err(context, 1, ret, "krb5_make_principal");
1714 krb5_principal_set_type(context, principal, KRB5_NT_WELLKNOWN);
1715 anonymous_pkinit = TRUE;
1716 } else if (use_keytab || keytab_str) {
1717 get_princ_kt(context, &principal, argv[0]);
1718 } else if (gss_preauth_mech && argc == 0 && gss_preauth_name == NULL) {
1720 * Use the federated name as a placeholder if we have neither a Kerberos
1721 * nor a GSS-API client name, and we are performing GSS-API preauth.
1723 ret = make_wellknown_name(context, get_default_realm(context),
1724 KRB5_FEDERATED_NAME, &principal);
1725 if (ret)
1726 krb5_err(context, 1, ret, "make_wellknown_name");
1727 } else {
1728 get_princ(context, &principal, cred_cache, argv[0]);
1731 if (fcache_version)
1732 krb5_set_fcache_version(context, fcache_version);
1734 if (renewable_flag == -1)
1735 /* this seems somewhat pointless, but whatever */
1736 krb5_appdefault_boolean(context, "kinit",
1737 krb5_principal_get_realm(context, principal),
1738 "renewable", FALSE, &renewable_flag);
1739 if (do_afslog == -1)
1740 krb5_appdefault_boolean(context, "kinit",
1741 krb5_principal_get_realm(context, principal),
1742 "afslog", TRUE, &do_afslog);
1745 * Cases:
1747 * - use the given ccache
1748 * - use a new unique ccache for running a command with (in this case we
1749 * get to set KRB5CCNAME, so a new unique ccache makes sense)
1750 * - use the default ccache for the given principal as requested and do
1751 * not later switch the collection's default/primary to it
1752 * - use the default cache, possibly a new unique one that later gets
1753 * switched to it
1755 * The important thing is that, except for the case where we're running a
1756 * command, we _can't set KRB5CCNAME_, and we can't expect the user to read
1757 * our output and figure out to set it (we could have an output-for-shell-
1758 * eval mode, like ssh-agent and such, but we don't). Therefore, in all
1759 * cases where we can't set KRB5CCNAME we must do something that makes
1760 * sense to the user, and that is to either initialize a given ccache, use
1761 * the default, or use a subsidiary ccache named after the principal whose
1762 * creds we're initializing.
1764 if (cred_cache) {
1765 /* Use the given ccache */
1766 ret = krb5_cc_resolve(context, cred_cache, &ccache);
1767 } else if (argc > 1) {
1768 char s[1024];
1771 * A command was given, so use a new unique ccache (and destroy it
1772 * later).
1774 ret = krb5_cc_new_unique(context, NULL, NULL, &ccache);
1775 if (ret)
1776 krb5_err(context, 1, ret, "creating cred cache");
1777 snprintf(s, sizeof(s), "%s:%s",
1778 krb5_cc_get_type(context, ccache),
1779 krb5_cc_get_name(context, ccache));
1780 setenv("KRB5CCNAME", s, 1);
1781 unique_ccache = TRUE;
1782 switch_cache_flags = 0;
1783 } else if (default_for) {
1784 ret = krb5_cc_default_for(context, principal, &ccache);
1785 if (switch_cache_flags == -1)
1786 switch_cache_flags = 0;
1787 } else {
1788 ret = krb5_cc_cache_match(context, principal, &ccache);
1789 if (ret) {
1790 const char *type;
1791 ret = krb5_cc_default(context, &ccache);
1792 if (ret)
1793 krb5_err(context, 1, ret,
1794 N_("resolving credentials cache", ""));
1797 * Check if the type support switching, and we do,
1798 * then do that instead over overwriting the current
1799 * default credential
1801 type = krb5_cc_get_type(context, ccache);
1802 if (krb5_cc_support_switch(context, type) &&
1803 strcmp(type, "FILE")) {
1804 krb5_cc_close(context, ccache);
1805 ret = get_switched_ccache(context, type, principal,
1806 &ccache);
1807 if (ret == 0)
1808 unique_ccache = TRUE;
1812 if (ret)
1813 krb5_err(context, 1, ret, N_("resolving credentials cache", ""));
1815 #ifndef NO_AFS
1816 if (argc > 1 && k_hasafs())
1817 k_setpag();
1818 #endif
1820 if (lifetime) {
1821 int tmp = parse_time(lifetime, "s");
1822 if (tmp < 0)
1823 errx(1, N_("unparsable time: %s", ""), lifetime);
1825 ticket_life = tmp;
1828 if (addrs_flag == 0 && extra_addresses.num_strings > 0)
1829 krb5_errx(context, 1,
1830 N_("specifying both extra addresses and "
1831 "no addresses makes no sense", ""));
1833 int i;
1834 krb5_addresses addresses;
1835 memset(&addresses, 0, sizeof(addresses));
1836 for(i = 0; i < extra_addresses.num_strings; i++) {
1837 ret = krb5_parse_address(context, extra_addresses.strings[i],
1838 &addresses);
1839 if (ret == 0) {
1840 krb5_add_extra_addresses(context, &addresses);
1841 krb5_free_addresses(context, &addresses);
1844 free_getarg_strings(&extra_addresses);
1847 if (renew_flag || validate_flag) {
1848 ret = renew_validate(context, renew_flag, validate_flag,
1849 &ccache, principal,
1850 default_for ? TRUE : FALSE, server_str,
1851 ticket_life);
1853 #ifndef NO_AFS
1854 if (ret == 0 && server_str == NULL && do_afslog && k_hasafs())
1855 krb5_afslog(context, ccache, NULL, NULL);
1856 #endif
1858 if (unique_ccache)
1859 krb5_cc_destroy(context, ccache);
1860 exit(ret != 0);
1863 ret = get_new_tickets(context, principal, ccache, ticket_life,
1864 1, anonymous_pkinit);
1865 if (ret) {
1866 if (unique_ccache)
1867 krb5_cc_destroy(context, ccache);
1868 exit(1);
1871 #ifndef NO_AFS
1872 if (ret == 0 && server_str == NULL && do_afslog && k_hasafs())
1873 krb5_afslog(context, ccache, NULL, NULL);
1874 #endif
1876 if (argc > 1) {
1877 struct renew_ctx ctx;
1878 time_t timeout;
1880 timeout = ticket_lifetime(context, ccache, principal,
1881 server_str, NULL) / 2;
1883 ctx.context = context;
1884 ctx.ccache = ccache;
1885 ctx.principal = principal;
1886 ctx.ticket_life = ticket_life;
1887 ctx.timeout = timeout;
1888 ctx.anonymous_pkinit = anonymous_pkinit;
1890 #ifdef HAVE_SIGACTION
1891 memset(&sa, 0, sizeof(sa));
1892 sigemptyset(&sa.sa_mask);
1893 sa.sa_handler = handle_siginfo;
1895 sigaction(SIGINFO, &sa, NULL);
1896 #endif
1898 ret = simple_execvp_timed(argv[1], argv+1,
1899 renew_func, &ctx, timeout);
1900 #define EX_NOEXEC 126
1901 #define EX_NOTFOUND 127
1902 if (ret == EX_NOEXEC)
1903 krb5_warnx(context, N_("permission denied: %s", ""), argv[1]);
1904 else if (ret == EX_NOTFOUND)
1905 krb5_warnx(context, N_("command not found: %s", ""), argv[1]);
1907 krb5_cc_destroy(context, ccache);
1908 #ifndef NO_AFS
1909 if (k_hasafs())
1910 k_unlog();
1911 #endif
1912 } else {
1913 krb5_cc_close(context, ccache);
1914 ret = 0;
1916 krb5_free_principal(context, principal);
1917 if (kt)
1918 krb5_kt_close(context, kt);
1919 krb5_free_context(context);
1920 return ret;