2 * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
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
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
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>
49 #define SIGINFO SIGUSR1
52 int forwardable_flag
= -1;
53 int proxiable_flag
= -1;
54 int renewable_flag
= -1;
57 int validate_flag
= 0;
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
;
73 char *keytab_str
= NULL
;
74 static krb5_keytab kt
= NULL
;
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;
94 static char *ntlm_domain
;
98 static struct getargs args
[] = {
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
},
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
},
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" },
207 { "ntlm-domain", 0, arg_string
, &ntlm_domain
,
208 NP_("NTLM domain", ""), "domain" },
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
}
234 get_default_realm(krb5_context context
);
239 arg_printusage_i18n(args
, sizeof(args
)/sizeof(*args
), N_("Usage: ", ""),
240 NULL
, "[principal [command]]", getarg_i18n
);
244 static krb5_error_code
245 tgs_principal(krb5_context context
,
247 krb5_principal client
,
248 krb5_const_realm tgs_realm
,
249 krb5_principal
*out_princ
)
252 krb5_principal tgs_princ
;
257 ret
= krb5_make_principal(context
, &tgs_princ
, tgs_realm
,
258 KRB5_TGS_NAME
, tgs_realm
, NULL
);
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
);
272 krb5_free_creds(context
, tick
);
273 *out_princ
= tgs_princ
;
275 krb5_free_principal(context
, tgs_princ
);
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
,
292 krb5_principal client
,
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
;
308 ret
= krb5_parse_name(context
, server
, &tgs_service
);
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
)
321 /* Next try the default realm */
322 ret
= krb5_get_default_realm(context
, &def_realm
);
325 ret
= tgs_principal(context
, cache
, client
, def_realm
, &tgs_service
);
327 if (ret
== 0 || ret
!= KRB5_CC_NOTFOUND
)
330 /* Finally try the first TGT with instance == realm in the cache */
331 ret
= krb5_cc_start_seq_get(context
, cache
, &cursor
);
335 for (/**/; ret
== 0; krb5_free_cred_contents (context
, &creds
)) {
337 ret
= krb5_cc_next_cred(context
, cache
, &cursor
, &creds
);
340 if (creds
.server
->name
.name_string
.len
!= 2)
342 pcomp
= krb5_principal_get_comp_string(context
, creds
.server
, 0);
343 if (strcmp(pcomp
, KRB5_TGS_NAME
) != 0)
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)
349 ret
= krb5_copy_principal(context
, creds
.server
, &tgs_service
);
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
);
361 ret
= krb5_copy_principal(context
, tgs_service
, princ
);
365 static krb5_error_code
366 copy_configs(krb5_context context
,
369 krb5_principal start_ticket_server
)
372 const char *cfg_names
[] = {"realm-config", "FriendlyName", "anon_pkinit_realm", NULL
};
373 const char *cfg_names_w_pname
[] = {"fast_avail", NULL
};
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
) {
382 krb5_warn(context
, ret
, "krb5_cc_get_config");
385 ret
= krb5_cc_set_config(context
, dst
, NULL
, cfg_names
[i
], &cfg_data
);
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
) {
395 krb5_warn(context
, ret
, "krb5_cc_get_config");
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).
412 static krb5_error_code
413 get_anon_pkinit_tgs_name(krb5_context context
,
415 krb5_principal
*tgs_name
)
421 ret
= krb5_cc_get_config(context
, ccache
, NULL
, "anon_pkinit_realm", &data
);
423 realm
= strndup(data
.data
, data
.length
);
425 realm
= get_default_realm(context
);
427 krb5_data_free(&data
);
430 return krb5_enomem(context
);
432 ret
= krb5_make_principal(context
, tgs_name
, realm
,
433 KRB5_TGS_NAME
, realm
, NULL
);
440 static krb5_error_code
441 renew_validate(krb5_context context
,
445 krb5_const_principal principal
,
446 krb5_boolean cache_is_default_for
,
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
);
465 ret2
= krb5_cc_get_principal(context
, def_ccache
, &in
.client
);
467 krb5_principal_compare(context
, principal
, in
.client
)) {
468 krb5_cc_close(context
, *cachep
);
469 cache
= *cachep
= def_ccache
;
473 krb5_cc_close(context
, def_ccache
);
476 krb5_warn(context
, ret
, "krb5_cc_get_principal");
480 if (principal
&& !krb5_principal_compare(context
, principal
, in
.client
)) {
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");
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
);
494 ret
= get_server(context
, cache
, in
.client
, server
, TRUE
, &in
.server
);
496 krb5_warn(context
, ret
, "get_server");
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
);
509 flags
.b
.renewable
= flags
.b
.renew
= renew
;
510 flags
.b
.validate
= validate
;
512 if (forwardable_flag
!= -1)
513 flags
.b
.forwardable
= forwardable_flag
;
515 flags
.b
.forwardable
= out
->flags
.b
.forwardable
;
517 if (proxiable_flag
!= -1)
518 flags
.b
.proxiable
= proxiable_flag
;
520 flags
.b
.proxiable
= out
->flags
.b
.proxiable
;
523 flags
.b
.request_anonymous
= anonymous_flag
;
525 in
.times
.endtime
= time(NULL
) + life
;
528 krb5_free_creds(context
, out
);
533 ret
= krb5_get_kdc_cred(context
,
541 krb5_warn(context
, ret
, "krb5_get_kdc_cred");
545 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, cache
),
548 krb5_warn(context
, ret
, "krb5_cc_new_unique");
552 ret
= krb5_cc_initialize(context
, tempccache
, in
.client
);
554 krb5_warn(context
, ret
, "krb5_cc_initialize");
558 ret
= krb5_cc_store_cred(context
, tempccache
, out
);
560 krb5_warn(context
, ret
, "krb5_cc_store_cred");
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
);
572 ret
= krb5_cc_move(context
, tempccache
, cache
);
574 krb5_warn(context
, ret
, "krb5_cc_move");
581 krb5_cc_destroy(context
, tempccache
);
583 krb5_free_creds(context
, out
);
584 krb5_free_cred_contents(context
, &in
);
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
)
596 ret
= krb5_make_principal(context
, principal
, realm
,
597 KRB5_WELLKNOWN_NAME
, instance
, NULL
);
599 krb5_principal_set_type(context
, *principal
, KRB5_NT_WELLKNOWN
);
604 static krb5_error_code
605 acquire_gss_cred(krb5_context context
,
606 krb5_const_principal client
,
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
)
628 if (gss_preauth_name
) {
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
);
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
;
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
;
661 cred_store
.count
= 0;
666 mechs
.elements
= (gss_OID
)*mech
;
669 major
= gss_acquire_cred_from(&minor
,
671 life
? life
: GSS_C_INDEFINITE
,
672 *mech
? &mechs
: GSS_C_NO_OID_SET
,
678 if (major
!= GSS_S_COMPLETE
) {
679 ret
= _krb5_gss_map_error(major
, minor
);
684 gss_release_name(&minor
, &name
);
690 static krb5_error_code
691 store_ntlmkey(krb5_context context
, krb5_ccache id
,
692 const char *domain
, struct ntlm_buf
*buf
)
699 ret
= krb5_cc_get_config(context
, id
, NULL
, "default-ntlm-domain", &data
);
701 krb5_data_free(&data
);
703 data
.length
= strlen(domain
);
704 data
.data
= rk_UNCONST(domain
);
705 ret
= krb5_cc_set_config(context
, id
, NULL
, "default-ntlm-domain", &data
);
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
);
723 static krb5_error_code
724 get_new_tickets(krb5_context context
,
725 krb5_principal principal
,
727 krb5_deltat ticket_life
,
729 int anonymous_pkinit
)
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
;
747 struct ntlm_buf ntlmkey
;
748 memset(&ntlmkey
, 0, sizeof(ntlmkey
));
758 if (strcasecmp("STDIN", password_file
) == 0)
761 f
= fopen(password_file
, "r");
763 krb5_warnx(context
, N_("Failed to open the password file %s", ""),
768 if (fgets(passwd
, sizeof(passwd
), f
) == NULL
) {
769 krb5_warnx(context
, N_("Failed to read password from file %s", ""),
773 return EINVAL
; /* XXX Need a better error */
777 passwd
[strcspn(passwd
, "\n")] = '\0';
780 #ifdef HAVE_FRAMEWORK_SECURITY
781 if (passwd
[0] == '\0') {
788 realm
= krb5_principal_get_realm(context
, principal
);
790 ret
= krb5_unparse_name_flags(context
, principal
,
791 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &name
);
795 osret
= SecKeychainFindGenericPassword(NULL
, strlen(realm
), realm
,
797 &length
, &buffer
, NULL
);
799 if (osret
== noErr
&& length
< sizeof(passwd
) - 1) {
800 memcpy(passwd
, buffer
, length
);
801 passwd
[length
] = '\0';
808 memset(&cred
, 0, sizeof(cred
));
810 ret
= krb5_get_init_creds_opt_alloc(context
, &opt
);
812 krb5_warn(context
, ret
, "krb5_get_init_creds_opt_alloc");
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
);
824 krb5_get_init_creds_opt_set_anonymous(opt
, anonymous_flag
);
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
,
841 pk_use_enckey
? KRB5_GIC_OPT_PKINIT_USE_ENCKEY
: 0 |
842 anonymous_pkinit
? KRB5_GIC_OPT_PKINIT_ANONYMOUS
: 0,
847 krb5_warn(context
, ret
, "krb5_get_init_creds_opt_set_pkinit");
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";
861 renewstr
= renew_life
;
863 renew
= parse_time(renewstr
, "s");
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
);
874 int tmp
= parse_time(start_str
, "s");
876 errx(1, N_("unparsable time: %s", ""), start_str
);
881 if (etype_str
.num_strings
) {
884 enctype
= malloc(etype_str
.num_strings
* sizeof(*enctype
));
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
],
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
);
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
);
917 principal
= federated_name
;
921 ret
= krb5_init_creds_init(context
, principal
, prompter
, NULL
, start_time
, opt
, &ctx
);
923 krb5_warn(context
, ret
, "krb5_init_creds_init");
928 ret
= krb5_init_creds_set_service(context
, ctx
, server_str
);
930 krb5_warn(context
, ret
, "krb5_init_creds_set_service");
936 ret
= krb5_init_creds_set_kdc_hostname(context
, ctx
, kdc_hostname
);
938 krb5_warn(context
, ret
, "krb5_init_creds_set_kdc_hostname");
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
);
962 krb5_warn(context
, ret
, "krb5_cc_resolve(FAST cache)");
966 ret
= krb5_init_creds_set_fast_ccache(context
, ctx
, fastid
);
968 krb5_warn(context
, ret
, "krb5_init_creds_set_fast_ccache");
971 } else if (pk_anon_fast_armor
== -1) {
972 ret
= _krb5_init_creds_set_fast_anon_pkinit_optimistic(context
, ctx
);
974 krb5_warn(context
, ret
, "_krb5_init_creds_set_fast_anon_pkinit_optimistic");
977 } else if (pk_anon_fast_armor
) {
978 ret
= krb5_init_creds_set_fast_anon_pkinit(context
, ctx
);
980 krb5_warn(context
, ret
, "krb5_init_creds_set_fast_anon_pkinit");
985 if (gss_mech
!= GSS_C_NO_OID
) {
986 ret
= krb5_gss_set_init_creds(context
, ctx
, gss_cred
, gss_mech
);
988 krb5_warn(context
, ret
, "krb5_gss_set_init_creds");
991 } else if (use_keytab
|| keytab_str
) {
992 ret
= krb5_init_creds_set_keytab(context
, ctx
, kt
);
994 krb5_warn(context
, ret
, "krb5_init_creds_set_keytab");
997 } else if (pk_user_id
|| ent_user_id
||
998 krb5_principal_is_anonymous(context
, principal
, KRB5_ANON_MATCH_ANY
)) {
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 "
1006 krb5_get_init_creds_opt_free(context
, opt
);
1011 if (passwd
[0] == '\0') {
1015 ret
= krb5_unparse_name(context
, principal
, &p
);
1017 errx(1, "failed to generate passwd prompt: not enough memory");
1019 aret
= asprintf(&prompt
, N_("%s's Password: ", ""), p
);
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");
1032 ret
= krb5_init_creds_set_password(context
, ctx
, passwd
);
1034 krb5_warn(context
, ret
, "krb5_init_creds_set_password");
1040 ret
= krb5_init_creds_get(context
, ctx
);
1043 if (ntlm_domain
&& passwd
[0])
1044 heim_ntlm_nt_key(passwd
, &ntlmkey
);
1046 memset_s(passwd
, sizeof(passwd
), 0, sizeof(passwd
));
1051 case KRB5_LIBOS_PWDINTR
: /* don't print anything if it was just C-c:ed */
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", ""));
1059 case KRB5KRB_AP_ERR_V4_REPLY
:
1060 krb5_warnx(context
, N_("Looks like a Kerberos 4 reply", ""));
1062 case KRB5KDC_ERR_KEY_EXPIRED
:
1063 krb5_warnx(context
, N_("Password expired", ""));
1066 krb5_warn(context
, ret
, "krb5_get_init_creds");
1070 krb5_process_last_request(context
, opt
, ctx
);
1072 ret
= krb5_init_creds_get_creds(context
, ctx
, &cred
);
1074 krb5_warn(context
, ret
, "krb5_init_creds_get_creds");
1078 if (ticket_life
!= 0) {
1079 if (labs(cred
.times
.endtime
- cred
.times
.starttime
- ticket_life
) > 30) {
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
);
1087 if (labs(cred
.times
.renew_till
- cred
.times
.starttime
- renew
) > 30) {
1089 unparse_time_approx(cred
.times
.renew_till
- cred
.times
.starttime
,
1090 life
, sizeof(life
));
1092 N_("NOTICE: ticket renewable lifetime is %s", ""),
1096 krb5_free_cred_contents(context
, &cred
);
1098 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, ccache
),
1101 krb5_warn(context
, ret
, "krb5_cc_new_unique");
1105 ret
= krb5_init_creds_store(context
, ctx
, tempccache
);
1107 krb5_warn(context
, ret
, "krb5_init_creds_store");
1111 ret
= krb5_init_creds_store_config(context
, ctx
, tempccache
);
1113 krb5_warn(context
, ret
, "krb5_init_creds_store_config");
1117 ret
= krb5_init_creds_warn_user(context
, ctx
);
1119 krb5_warn(context
, ret
, "krb5_init_creds_warn_user");
1123 krb5_init_creds_free(context
, ctx
);
1126 ret
= krb5_cc_move(context
, tempccache
, ccache
);
1128 krb5_warn(context
, ret
, "krb5_cc_move");
1133 if (switch_cache_flags
)
1134 krb5_cc_switch(context
, ccache
);
1137 if (ntlm_domain
&& ntlmkey
.data
)
1138 store_ntlmkey(context
, ccache
, ntlm_domain
, &ntlmkey
);
1141 if (ok_as_delegate_flag
|| windows_flag
|| use_referrals_flag
) {
1142 unsigned char d
= 0;
1145 if (ok_as_delegate_flag
|| windows_flag
)
1147 if (use_referrals_flag
|| windows_flag
)
1153 krb5_cc_set_config(context
, ccache
, NULL
, "realm-config", &data
);
1156 if (anonymous_pkinit
) {
1159 data
.length
= strlen(principal
->realm
);
1160 data
.data
= principal
->realm
;
1162 krb5_cc_set_config(context
, ccache
, NULL
, "anon_pkinit_realm", &data
);
1168 gss_release_cred(&minor
, &gss_cred
);
1170 krb5_free_principal(context
, federated_name
);
1171 krb5_get_init_creds_opt_free(context
, opt
);
1173 krb5_init_creds_free(context
, ctx
);
1175 krb5_cc_destroy(context
, tempccache
);
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
;
1191 memset(&in_cred
, 0, sizeof(in_cred
));
1196 ret
= krb5_cc_get_principal(context
, cache
, &in_cred
.client
);
1198 krb5_warn(context
, ret
, "krb5_cc_get_principal");
1202 /* Determine TGS principal without fallback */
1203 ret
= get_server(context
, cache
, in_cred
.client
, server
, FALSE
,
1206 krb5_free_principal(context
, in_cred
.client
);
1207 krb5_warn(context
, ret
, "get_server");
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
);
1216 krb5_warn(context
, ret
, "krb5_get_credentials");
1219 curtime
= time(NULL
);
1220 timeout
= cred
->times
.endtime
- curtime
;
1224 *renew
= cred
->times
.renew_till
- curtime
;
1228 krb5_free_creds(context
, cred
);
1232 static time_t expire
;
1234 static char siginfo_msg
[1024] = "No credentials\n";
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"));
1244 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
1245 N_("kinit: TGT expires in %llu seconds\n", ""),
1246 (unsigned long long)expire
);
1248 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
1249 N_("kinit: Ticket for %s expired\n", ""), srv
);
1256 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
1257 N_("kinit: TGT expired\n", ""));
1259 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
1260 N_("kinit: Ticket for %s expired\n", ""), srv
);
1264 #ifdef HAVE_SIGACTION
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";
1275 writev(STDERR_FILENO
, iov
, sizeof(iov
)/sizeof(iov
[0]));
1280 krb5_context context
;
1282 krb5_principal principal
;
1283 krb5_deltat ticket_life
;
1284 krb5_deltat timeout
;
1285 int anonymous_pkinit
;
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
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
);
1321 ret
= get_new_tickets(ctx
->context
, ctx
->principal
, ctx
->ccache
,
1322 ctx
->ticket_life
, 0, ctx
->anonymous_pkinit
);
1325 expire
= ticket_lifetime(ctx
->context
, ctx
->ccache
, ctx
->principal
,
1326 server_str
, &renew_expire
);
1329 if (server_str
== NULL
&& do_afslog
&& k_hasafs())
1330 krb5_afslog(ctx
->context
, ctx
->ccache
, NULL
, NULL
);
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...
1346 * We can't ask to keep spamming stderr but not syslog, so we warn
1349 if (exp_delay
== 1) {
1350 krb5_warnx(ctx
->context
, N_("NOTICE: Could not renew/refresh "
1353 if (exp_delay
< 7200)
1354 exp_delay
+= exp_delay
/ 2 + 1;
1359 return expire
/ 2 + 1;
1363 set_princ_realm(krb5_context context
,
1364 krb5_principal principal
,
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");
1374 parse_name_realm(krb5_context context
,
1378 krb5_principal
*princ
)
1380 krb5_error_code ret
;
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
);
1391 get_default_realm(krb5_context context
)
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");
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");
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", "",
1422 if (user_realm
== NULL
) {
1423 ret
= krb5_enomem(context
);
1424 krb5_err(context
, 1, ret
, "krb5_appdefault_string");
1427 if (*user_realm
== 0) {
1436 get_princ(krb5_context context
,
1437 krb5_principal
*principal
,
1441 krb5_error_code ret
= 0;
1447 krb5_ccache ccache
= NULL
;
1449 /* If credential cache provides a client principal, use that. */
1451 ret
= krb5_cc_resolve(context
, ccname
, &ccache
);
1453 ret
= krb5_cc_default(context
, &ccache
);
1455 ret
= krb5_cc_get_principal(context
, ccache
, principal
);
1456 krb5_cc_close(context
, ccache
);
1461 user_realm
= get_user_realm(context
);
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
);
1477 get_default_principal(context
, principal
);
1479 set_princ_realm(context
, *principal
, user_realm
);
1487 get_princ_kt(krb5_context context
,
1488 krb5_principal
*principal
,
1491 krb5_error_code ret
;
1494 krb5_kt_cursor cursor
;
1495 krb5_keytab_entry entry
;
1500 * If the credential cache exists and specifies a client principal,
1503 if (krb5_cc_default(context
, &ccache
) == 0) {
1504 ret
= krb5_cc_get_principal(context
, ccache
, principal
);
1505 krb5_cc_close(context
, ccache
);
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
) {
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
);
1530 krb5_err(context
, 1, ret
, "krb5_kt_start_seq_get");
1533 krb5_kt_next_entry(context
, kt
, &entry
, &cursor
) == 0) {
1536 if (!krb5_principal_compare_any_realm(context
, tmp
, entry
.principal
))
1539 krb5_principal_compare(context
, *principal
, entry
.principal
))
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
);
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");
1555 parse_name_realm(context
, name
, 0, NULL
, principal
);
1557 krb5_err(context
, 1, KRB5_CC_NOTFOUND
, "get_princ_kt");
1560 krb5_free_principal(context
, tmp
);
1564 static krb5_error_code
1565 get_switched_ccache(krb5_context context
,
1567 krb5_principal principal
,
1568 krb5_ccache
*ccache
)
1570 krb5_error_code ret
;
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
;
1586 ret
= krb5_unparse_name(context
, principal
,
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
);
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
);
1608 ret
= krb5_cc_new_unique(context
, type
, NULL
, ccache
);
1611 ret
= krb5_cc_new_unique(context
, type
, NULL
, ccache
);
1618 main(int argc
, char **argv
)
1620 krb5_error_code ret
;
1621 krb5_context context
;
1623 krb5_principal principal
= NULL
;
1625 krb5_deltat ticket_life
= 0;
1626 #ifdef HAVE_SIGACTION
1627 struct sigaction sa
;
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");
1643 errx(1, "krb5_init_context failed: %d", ret
);
1645 if (getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
1652 print_version(NULL
);
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
) {
1668 ret
= krb5_kt_resolve(context
, keytab_str
, &kt
);
1670 ret
= krb5_kt_default(context
, &kt
);
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
,
1680 krb5_err(context
, 1, ret
, "krb5_pk_enterprise_certs");
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
);
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
);
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
);
1726 krb5_err(context
, 1, ret
, "make_wellknown_name");
1728 get_princ(context
, &principal
, cred_cache
, argv
[0]);
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
);
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
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.
1765 /* Use the given ccache */
1766 ret
= krb5_cc_resolve(context
, cred_cache
, &ccache
);
1767 } else if (argc
> 1) {
1771 * A command was given, so use a new unique ccache (and destroy it
1774 ret
= krb5_cc_new_unique(context
, NULL
, NULL
, &ccache
);
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;
1788 ret
= krb5_cc_cache_match(context
, principal
, &ccache
);
1791 ret
= krb5_cc_default(context
, &ccache
);
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
,
1808 unique_ccache
= TRUE
;
1813 krb5_err(context
, 1, ret
, N_("resolving credentials cache", ""));
1816 if (argc
> 1 && k_hasafs())
1821 int tmp
= parse_time(lifetime
, "s");
1823 errx(1, N_("unparsable time: %s", ""), lifetime
);
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", ""));
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
],
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
,
1850 default_for
? TRUE
: FALSE
, server_str
,
1854 if (ret
== 0 && server_str
== NULL
&& do_afslog
&& k_hasafs())
1855 krb5_afslog(context
, ccache
, NULL
, NULL
);
1859 krb5_cc_destroy(context
, ccache
);
1863 ret
= get_new_tickets(context
, principal
, ccache
, ticket_life
,
1864 1, anonymous_pkinit
);
1867 krb5_cc_destroy(context
, ccache
);
1872 if (ret
== 0 && server_str
== NULL
&& do_afslog
&& k_hasafs())
1873 krb5_afslog(context
, ccache
, NULL
, NULL
);
1877 struct renew_ctx ctx
;
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
);
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
);
1913 krb5_cc_close(context
, ccache
);
1916 krb5_free_principal(context
, principal
);
1918 krb5_kt_close(context
, kt
);
1919 krb5_free_context(context
);