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"
38 #ifdef HAVE_FRAMEWORK_SECURITY
39 #include <Security/Security.h>
47 #define SIGINFO SIGUSR1
50 int forwardable_flag
= -1;
51 int proxiable_flag
= -1;
52 int renewable_flag
= -1;
55 int validate_flag
= 0;
59 struct getarg_strings extra_addresses
;
60 int anonymous_flag
= 0;
61 char *lifetime
= NULL
;
62 char *renew_life
= NULL
;
63 char *server_str
= NULL
;
64 static krb5_principal tgs_service
;
65 char *cred_cache
= NULL
;
66 char *start_str
= NULL
;
67 static int switch_cache_flags
= -1;
68 static int default_for
= 0;
69 struct getarg_strings etype_str
;
71 char *keytab_str
= NULL
;
72 static krb5_keytab kt
= NULL
;
75 char *password_file
= NULL
;
76 char *pk_user_id
= NULL
;
77 int pk_enterprise_flag
= 0;
78 struct hx509_certs_data
*ent_user_id
= NULL
;
79 char *pk_x509_anchors
= NULL
;
80 int pk_use_enckey
= 0;
81 static int canonicalize_flag
= 0;
82 static int enterprise_flag
= 0;
83 static int ok_as_delegate_flag
= 0;
84 static char *fast_armor_cache_string
= NULL
;
85 static int use_referrals_flag
= 0;
86 static int windows_flag
= 0;
88 static char *ntlm_domain
;
92 static struct getargs args
[] = {
106 { "afslog", 0 , arg_flag
, &do_afslog
,
107 NP_("obtain afs tokens", ""), NULL
},
109 { "cache", 'c', arg_string
, &cred_cache
,
110 NP_("credentials cache", ""), "cachename" },
112 { "forwardable", 'F', arg_negative_flag
, &forwardable_flag
,
113 NP_("get tickets not forwardable", ""), NULL
},
115 { NULL
, 'f', arg_flag
, &forwardable_flag
,
116 NP_("get forwardable tickets", ""), NULL
},
118 { "keytab", 't', arg_string
, &keytab_str
,
119 NP_("keytab to use", ""), "keytabname" },
121 { "lifetime", 'l', arg_string
, &lifetime
,
122 NP_("lifetime of tickets", ""), "time" },
124 { "proxiable", 'p', arg_flag
, &proxiable_flag
,
125 NP_("get proxiable tickets", ""), NULL
},
127 { "renew", 'R', arg_flag
, &renew_flag
,
128 NP_("renew TGT", ""), NULL
},
130 { "renewable", 0, arg_flag
, &renewable_flag
,
131 NP_("get renewable tickets", ""), NULL
},
133 { "renewable-life", 'r', arg_string
, &renew_life
,
134 NP_("renewable lifetime of tickets", ""), "time" },
136 { "server", 'S', arg_string
, &server_str
,
137 NP_("server to get ticket for", ""), "principal" },
139 { "start-time", 's', arg_string
, &start_str
,
140 NP_("when ticket gets valid", ""), "time" },
142 { "use-keytab", 'k', arg_flag
, &use_keytab
,
143 NP_("get key from keytab", ""), NULL
},
145 { "validate", 'v', arg_flag
, &validate_flag
,
146 NP_("validate TGT", ""), NULL
},
148 { "enctypes", 'e', arg_strings
, &etype_str
,
149 NP_("encryption types to use", ""), "enctypes" },
151 { "fcache-version", 0, arg_integer
, &fcache_version
,
152 NP_("file cache version to create", ""), NULL
},
154 { "addresses", 'A', arg_negative_flag
, &addrs_flag
,
155 NP_("request a ticket with no addresses", ""), NULL
},
157 { "extra-addresses",'a', arg_strings
, &extra_addresses
,
158 NP_("include these extra addresses", ""), "addresses" },
160 { "anonymous", 'n', arg_flag
, &anonymous_flag
,
161 NP_("request an anonymous ticket", ""), NULL
},
163 { "request-pac", 0, arg_flag
, &pac_flag
,
164 NP_("request a Windows PAC", ""), NULL
},
166 { "password-file", 0, arg_string
, &password_file
,
167 NP_("read the password from a file", ""), NULL
},
169 { "canonicalize",0, arg_flag
, &canonicalize_flag
,
170 NP_("canonicalize client principal", ""), NULL
},
172 { "enterprise",0, arg_flag
, &enterprise_flag
,
173 NP_("parse principal as a KRB5-NT-ENTERPRISE name", ""), NULL
},
175 { "pk-enterprise", 0, arg_flag
, &pk_enterprise_flag
,
176 NP_("use enterprise name from certificate", ""), NULL
},
178 { "pk-user", 'C', arg_string
, &pk_user_id
,
179 NP_("principal's public/private/certificate identifier", ""), "id" },
181 { "x509-anchors", 'D', arg_string
, &pk_x509_anchors
,
182 NP_("directory with CA certificates", ""), "directory" },
184 { "pk-use-enckey", 0, arg_flag
, &pk_use_enckey
,
185 NP_("Use RSA encrypted reply (instead of DH)", ""), NULL
},
188 { "ntlm-domain", 0, arg_string
, &ntlm_domain
,
189 NP_("NTLM domain", ""), "domain" },
192 { "change-default", 0, arg_negative_flag
, &switch_cache_flags
,
193 NP_("switch the default cache to the new credentials cache", ""), NULL
},
195 { "default-for-principal", 0, arg_negative_flag
, &default_for
,
196 NP_("use a default cache appropriate for the client principal name", ""), NULL
},
198 { "ok-as-delegate", 0, arg_flag
, &ok_as_delegate_flag
,
199 NP_("honor ok-as-delegate on tickets", ""), NULL
},
201 { "fast-armor-cache", 0, arg_string
, &fast_armor_cache_string
,
202 NP_("use this credential cache as FAST armor cache", ""), "cache" },
204 { "use-referrals", 0, arg_flag
, &use_referrals_flag
,
205 NP_("only use referrals, no dns canalisation", ""), NULL
},
207 { "windows", 0, arg_flag
, &windows_flag
,
208 NP_("get windows behavior", ""), NULL
},
210 { "version", 0, arg_flag
, &version_flag
, NULL
, NULL
},
211 { "help", 0, arg_flag
, &help_flag
, NULL
, NULL
}
215 get_default_realm(krb5_context context
);
220 arg_printusage_i18n(args
, sizeof(args
)/sizeof(*args
), N_("Usage: ", ""),
221 NULL
, "[principal [command]]", getarg_i18n
);
225 static krb5_error_code
226 tgs_principal(krb5_context context
,
228 krb5_principal client
,
229 krb5_const_realm tgs_realm
,
230 krb5_principal
*out_princ
)
233 krb5_principal tgs_princ
;
238 ret
= krb5_make_principal(context
, &tgs_princ
, tgs_realm
,
239 KRB5_TGS_NAME
, tgs_realm
, NULL
);
244 * Don't fail-over to a different realm just because a TGT expired
246 options
= KRB5_GC_CACHED
| KRB5_GC_EXPIRED_OK
;
248 memset(&creds
, 0, sizeof(creds
));
249 creds
.client
= client
;
250 creds
.server
= tgs_princ
;
251 ret
= krb5_get_credentials(context
, options
, cache
, &creds
, &tick
);
253 krb5_free_creds(context
, tick
);
254 *out_princ
= tgs_princ
;
256 krb5_free_principal(context
, tgs_princ
);
264 * Try TGS specified with '-S',
265 * then TGS of client realm,
266 * then if fallback is FALSE: fail,
267 * otherwise try TGS of default realm,
268 * and finally first TGT in ccache.
270 static krb5_error_code
271 get_server(krb5_context context
,
273 krb5_principal client
,
275 krb5_boolean fallback
,
276 krb5_principal
*princ
)
278 krb5_error_code ret
= 0;
279 krb5_const_realm realm
;
280 krb5_realm def_realm
;
281 krb5_cc_cursor cursor
;
289 ret
= krb5_parse_name(context
, server
, &tgs_service
);
293 /* Try the client realm first */
294 realm
= krb5_principal_get_realm(context
, client
);
295 ret
= tgs_principal(context
, cache
, client
, realm
, &tgs_service
);
296 if (ret
== 0 || ret
!= KRB5_CC_NOTFOUND
)
302 /* Next try the default realm */
303 ret
= krb5_get_default_realm(context
, &def_realm
);
306 ret
= tgs_principal(context
, cache
, client
, def_realm
, &tgs_service
);
308 if (ret
== 0 || ret
!= KRB5_CC_NOTFOUND
)
311 /* Finally try the first TGT with instance == realm in the cache */
312 ret
= krb5_cc_start_seq_get(context
, cache
, &cursor
);
316 for (/**/; ret
== 0; krb5_free_cred_contents (context
, &creds
)) {
318 ret
= krb5_cc_next_cred(context
, cache
, &cursor
, &creds
);
321 if (creds
.server
->name
.name_string
.len
!= 2)
323 pcomp
= krb5_principal_get_comp_string(context
, creds
.server
, 0);
324 if (strcmp(pcomp
, KRB5_TGS_NAME
) != 0)
326 realm
= krb5_principal_get_realm(context
, creds
.server
);
327 pcomp
= krb5_principal_get_comp_string(context
, creds
.server
, 1);
328 if (strcmp(realm
, pcomp
) != 0)
330 ret
= krb5_copy_principal(context
, creds
.server
, &tgs_service
);
333 if (ret
== KRB5_CC_END
) {
334 ret
= KRB5_CC_NOTFOUND
;
335 krb5_set_error_message(context
, ret
,
336 N_("Credential cache contains no TGTs", ""));
338 krb5_cc_end_seq_get(context
, cache
, &cursor
);
342 ret
= krb5_copy_principal(context
, tgs_service
, princ
);
346 static krb5_error_code
347 copy_configs(krb5_context context
,
350 krb5_principal start_ticket_server
)
353 const char *cfg_names
[] = {"realm-config", "FriendlyName", "anon_pkinit_realm", NULL
};
354 const char *cfg_names_w_pname
[] = {"fast_avail", NULL
};
358 for (i
= 0; cfg_names
[i
]; i
++) {
359 ret
= krb5_cc_get_config(context
, src
, NULL
, cfg_names
[i
], &cfg_data
);
360 if (ret
== KRB5_CC_NOTFOUND
|| ret
== KRB5_CC_END
) {
363 krb5_warn(context
, ret
, "krb5_cc_get_config");
366 ret
= krb5_cc_set_config(context
, dst
, NULL
, cfg_names
[i
], &cfg_data
);
368 krb5_warn(context
, ret
, "krb5_cc_set_config");
370 for (i
= 0; start_ticket_server
&& cfg_names_w_pname
[i
]; i
++) {
371 ret
= krb5_cc_get_config(context
, src
, start_ticket_server
,
372 cfg_names_w_pname
[i
], &cfg_data
);
373 if (ret
== KRB5_CC_NOTFOUND
|| ret
== KRB5_CC_END
) {
376 krb5_warn(context
, ret
, "krb5_cc_get_config");
379 ret
= krb5_cc_set_config(context
, dst
, start_ticket_server
,
380 cfg_names_w_pname
[i
], &cfg_data
);
381 if (ret
&& ret
!= KRB5_CC_NOTFOUND
)
382 krb5_warn(context
, ret
, "krb5_cc_set_config");
385 * We don't copy cc configs for any other principals though (mostly
386 * those are per-target time offsets and the like, so it's bad to
387 * lose them, but hardly the end of the world, and as they may not
388 * expire anyways, it's good to let them go).
393 static krb5_error_code
394 get_anon_pkinit_tgs_name(krb5_context context
,
396 krb5_principal
*tgs_name
)
402 ret
= krb5_cc_get_config(context
, ccache
, NULL
, "anon_pkinit_realm", &data
);
404 realm
= strndup(data
.data
, data
.length
);
406 realm
= get_default_realm(context
);
408 krb5_data_free(&data
);
411 return krb5_enomem(context
);
413 ret
= krb5_make_principal(context
, tgs_name
, realm
,
414 KRB5_TGS_NAME
, realm
, NULL
);
421 static krb5_error_code
422 renew_validate(krb5_context context
,
426 krb5_const_principal principal
,
427 krb5_boolean cache_is_default_for
,
432 krb5_ccache tempccache
= NULL
;
433 krb5_ccache cache
= *cachep
;
434 krb5_creds in
, *out
= NULL
;
435 krb5_kdc_flags flags
;
437 memset(&in
, 0, sizeof(in
));
439 ret
= krb5_cc_get_principal(context
, cache
, &in
.client
);
440 if (ret
&& cache_is_default_for
&& principal
) {
441 krb5_error_code ret2
;
442 krb5_ccache def_ccache
= NULL
;
444 ret2
= krb5_cc_default(context
, &def_ccache
);
446 ret2
= krb5_cc_get_principal(context
, def_ccache
, &in
.client
);
448 krb5_principal_compare(context
, principal
, in
.client
)) {
449 krb5_cc_close(context
, *cachep
);
450 *cachep
= def_ccache
;
454 krb5_cc_close(context
, def_ccache
);
457 krb5_warn(context
, ret
, "krb5_cc_get_principal");
461 if (principal
&& !krb5_principal_compare(context
, principal
, in
.client
)) {
464 (void) krb5_cc_get_full_name(context
, cache
, &ccname
);
465 krb5_errx(context
, 1, "Credentials in cache %s do not match requested "
466 "principal", ccname
? ccname
: "requested");
470 if (server
== NULL
&&
471 krb5_principal_is_anonymous(context
, in
.client
,
472 KRB5_ANON_MATCH_UNAUTHENTICATED
))
473 ret
= get_anon_pkinit_tgs_name(context
, cache
, &in
.server
);
475 ret
= get_server(context
, cache
, in
.client
, server
, TRUE
, &in
.server
);
477 krb5_warn(context
, ret
, "get_server");
483 * no need to check the error here, it's only to be
484 * friendly to the user
486 krb5_get_credentials(context
, KRB5_GC_CACHED
, cache
, &in
, &out
);
490 flags
.b
.renewable
= flags
.b
.renew
= renew
;
491 flags
.b
.validate
= validate
;
493 if (forwardable_flag
!= -1)
494 flags
.b
.forwardable
= forwardable_flag
;
496 flags
.b
.forwardable
= out
->flags
.b
.forwardable
;
498 if (proxiable_flag
!= -1)
499 flags
.b
.proxiable
= proxiable_flag
;
501 flags
.b
.proxiable
= out
->flags
.b
.proxiable
;
504 flags
.b
.request_anonymous
= anonymous_flag
;
506 in
.times
.endtime
= time(NULL
) + life
;
509 krb5_free_creds(context
, out
);
514 ret
= krb5_get_kdc_cred(context
,
522 krb5_warn(context
, ret
, "krb5_get_kdc_cred");
526 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, cache
),
529 krb5_warn(context
, ret
, "krb5_cc_new_unique");
533 ret
= krb5_cc_initialize(context
, tempccache
, in
.client
);
535 krb5_warn(context
, ret
, "krb5_cc_initialize");
539 ret
= krb5_cc_store_cred(context
, tempccache
, out
);
541 krb5_warn(context
, ret
, "krb5_cc_store_cred");
546 * We want to preserve cc configs as some are security-relevant, and
547 * anyways it's the friendly thing to do.
549 ret
= copy_configs(context
, tempccache
, cache
, out
->server
);
553 ret
= krb5_cc_move(context
, tempccache
, cache
);
555 krb5_warn(context
, ret
, "krb5_cc_move");
562 krb5_cc_destroy(context
, tempccache
);
564 krb5_free_creds(context
, out
);
565 krb5_free_cred_contents(context
, &in
);
571 static krb5_error_code
572 store_ntlmkey(krb5_context context
, krb5_ccache id
,
573 const char *domain
, struct ntlm_buf
*buf
)
580 ret
= krb5_cc_get_config(context
, id
, NULL
, "default-ntlm-domain", &data
);
582 krb5_data_free(&data
);
584 data
.length
= strlen(domain
);
585 data
.data
= rk_UNCONST(domain
);
586 ret
= krb5_cc_set_config(context
, id
, NULL
, "default-ntlm-domain", &data
);
591 aret
= asprintf(&name
, "ntlm-key-%s", domain
);
592 if (aret
== -1 || name
== NULL
)
593 return krb5_enomem(context
);
595 data
.length
= buf
->length
;
596 data
.data
= buf
->data
;
598 ret
= krb5_cc_set_config(context
, id
, NULL
, name
, &data
);
604 static krb5_error_code
605 get_new_tickets(krb5_context context
,
606 krb5_principal principal
,
608 krb5_deltat ticket_life
,
610 int anonymous_pkinit
)
615 krb5_deltat start_time
= 0;
616 krb5_deltat renew
= 0;
617 const char *renewstr
= NULL
;
618 krb5_enctype
*enctype
= NULL
;
619 krb5_ccache tempccache
= NULL
;
620 krb5_init_creds_context ctx
= NULL
;
621 krb5_get_init_creds_opt
*opt
= NULL
;
622 krb5_prompter_fct prompter
= krb5_prompter_posix
;
624 struct ntlm_buf ntlmkey
;
625 memset(&ntlmkey
, 0, sizeof(ntlmkey
));
635 if (strcasecmp("STDIN", password_file
) == 0)
638 f
= fopen(password_file
, "r");
640 krb5_warnx(context
, N_("Failed to open the password file %s", ""),
645 if (fgets(passwd
, sizeof(passwd
), f
) == NULL
) {
646 krb5_warnx(context
, N_("Failed to read password from file %s", ""),
650 return EINVAL
; /* XXX Need a better error */
654 passwd
[strcspn(passwd
, "\n")] = '\0';
657 #ifdef HAVE_FRAMEWORK_SECURITY
658 if (passwd
[0] == '\0') {
665 realm
= krb5_principal_get_realm(context
, principal
);
667 ret
= krb5_unparse_name_flags(context
, principal
,
668 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &name
);
672 osret
= SecKeychainFindGenericPassword(NULL
, strlen(realm
), realm
,
674 &length
, &buffer
, NULL
);
676 if (osret
== noErr
&& length
< sizeof(passwd
) - 1) {
677 memcpy(passwd
, buffer
, length
);
678 passwd
[length
] = '\0';
685 memset(&cred
, 0, sizeof(cred
));
687 ret
= krb5_get_init_creds_opt_alloc(context
, &opt
);
689 krb5_warn(context
, ret
, "krb5_get_init_creds_opt_alloc");
693 krb5_get_init_creds_opt_set_default_flags(context
, "kinit",
694 krb5_principal_get_realm(context
, principal
), opt
);
696 if (forwardable_flag
!= -1)
697 krb5_get_init_creds_opt_set_forwardable(opt
, forwardable_flag
);
698 if (proxiable_flag
!= -1)
699 krb5_get_init_creds_opt_set_proxiable(opt
, proxiable_flag
);
701 krb5_get_init_creds_opt_set_anonymous(opt
, anonymous_flag
);
703 krb5_get_init_creds_opt_set_pac_request(context
, opt
,
704 pac_flag
? TRUE
: FALSE
);
705 if (canonicalize_flag
)
706 krb5_get_init_creds_opt_set_canonicalize(context
, opt
, TRUE
);
707 if (pk_enterprise_flag
|| enterprise_flag
|| canonicalize_flag
|| windows_flag
)
708 krb5_get_init_creds_opt_set_win2k(context
, opt
, TRUE
);
709 if (pk_user_id
|| ent_user_id
|| anonymous_pkinit
) {
710 ret
= krb5_get_init_creds_opt_set_pkinit(context
, opt
,
716 pk_use_enckey
? KRB5_GIC_OPT_PKINIT_USE_ENCKEY
: 0 |
717 anonymous_pkinit
? KRB5_GIC_OPT_PKINIT_ANONYMOUS
: 0,
722 krb5_warn(context
, ret
, "krb5_get_init_creds_opt_set_pkinit");
726 krb5_get_init_creds_opt_set_pkinit_user_certs(context
, opt
, ent_user_id
);
729 if (addrs_flag
!= -1)
730 krb5_get_init_creds_opt_set_addressless(context
, opt
,
731 addrs_flag
? FALSE
: TRUE
);
733 if (renew_life
== NULL
&& renewable_flag
)
734 renewstr
= "6 months";
736 renewstr
= renew_life
;
738 renew
= parse_time(renewstr
, "s");
740 errx(1, "unparsable time: %s", renewstr
);
742 krb5_get_init_creds_opt_set_renew_life(opt
, renew
);
745 if (ticket_life
!= 0)
746 krb5_get_init_creds_opt_set_tkt_life(opt
, ticket_life
);
749 int tmp
= parse_time(start_str
, "s");
751 errx(1, N_("unparsable time: %s", ""), start_str
);
756 if (etype_str
.num_strings
) {
759 enctype
= malloc(etype_str
.num_strings
* sizeof(*enctype
));
761 errx(1, "out of memory");
762 for(i
= 0; i
< etype_str
.num_strings
; i
++) {
763 ret
= krb5_string_to_enctype(context
,
764 etype_str
.strings
[i
],
767 errx(1, "unrecognized enctype: %s", etype_str
.strings
[i
]);
769 krb5_get_init_creds_opt_set_etype_list(opt
, enctype
,
770 etype_str
.num_strings
);
773 ret
= krb5_init_creds_init(context
, principal
, prompter
, NULL
, start_time
, opt
, &ctx
);
775 krb5_warn(context
, ret
, "krb5_init_creds_init");
780 ret
= krb5_init_creds_set_service(context
, ctx
, server_str
);
782 krb5_warn(context
, ret
, "krb5_init_creds_set_service");
787 if (fast_armor_cache_string
) {
790 ret
= krb5_cc_resolve(context
, fast_armor_cache_string
, &fastid
);
792 krb5_warn(context
, ret
, "krb5_cc_resolve(FAST cache)");
796 ret
= krb5_init_creds_set_fast_ccache(context
, ctx
, fastid
);
798 krb5_warn(context
, ret
, "krb5_init_creds_set_fast_ccache");
803 if (use_keytab
|| keytab_str
) {
804 ret
= krb5_init_creds_set_keytab(context
, ctx
, kt
);
806 krb5_warn(context
, ret
, "krb5_init_creds_set_keytab");
809 } else if (pk_user_id
|| ent_user_id
||
810 krb5_principal_is_anonymous(context
, principal
, KRB5_ANON_MATCH_ANY
)) {
812 } else if (!interactive
&& passwd
[0] == '\0') {
813 static int already_warned
= 0;
816 krb5_warnx(context
, "Not interactive, failed to get "
818 krb5_get_init_creds_opt_free(context
, opt
);
823 if (passwd
[0] == '\0') {
827 ret
= krb5_unparse_name(context
, principal
, &p
);
829 errx(1, "failed to generate passwd prompt: not enough memory");
831 aret
= asprintf(&prompt
, N_("%s's Password: ", ""), p
);
834 errx(1, "failed to generate passwd prompt: not enough memory");
836 if (UI_UTIL_read_pw_string(passwd
, sizeof(passwd
)-1, prompt
, 0)){
837 memset(passwd
, 0, sizeof(passwd
));
838 errx(1, "failed to read password");
844 ret
= krb5_init_creds_set_password(context
, ctx
, passwd
);
846 krb5_warn(context
, ret
, "krb5_init_creds_set_password");
852 ret
= krb5_init_creds_get(context
, ctx
);
855 if (ntlm_domain
&& passwd
[0])
856 heim_ntlm_nt_key(passwd
, &ntlmkey
);
858 memset_s(passwd
, sizeof(passwd
), 0, sizeof(passwd
));
863 case KRB5_LIBOS_PWDINTR
: /* don't print anything if it was just C-c:ed */
865 case KRB5KRB_AP_ERR_BAD_INTEGRITY
:
866 case KRB5KRB_AP_ERR_MODIFIED
:
867 case KRB5KDC_ERR_PREAUTH_FAILED
:
868 case KRB5_GET_IN_TKT_LOOP
:
869 krb5_warnx(context
, N_("Password incorrect", ""));
871 case KRB5KRB_AP_ERR_V4_REPLY
:
872 krb5_warnx(context
, N_("Looks like a Kerberos 4 reply", ""));
874 case KRB5KDC_ERR_KEY_EXPIRED
:
875 krb5_warnx(context
, N_("Password expired", ""));
878 krb5_warn(context
, ret
, "krb5_get_init_creds");
882 krb5_process_last_request(context
, opt
, ctx
);
884 ret
= krb5_init_creds_get_creds(context
, ctx
, &cred
);
886 krb5_warn(context
, ret
, "krb5_init_creds_get_creds");
890 if (ticket_life
!= 0) {
891 if (labs(cred
.times
.endtime
- cred
.times
.starttime
- ticket_life
) > 30) {
893 unparse_time_approx(cred
.times
.endtime
- cred
.times
.starttime
,
895 krb5_warnx(context
, N_("NOTICE: ticket lifetime is %s", ""), life
);
899 if (labs(cred
.times
.renew_till
- cred
.times
.starttime
- renew
) > 30) {
901 unparse_time_approx(cred
.times
.renew_till
- cred
.times
.starttime
,
904 N_("NOTICE: ticket renewable lifetime is %s", ""),
908 krb5_free_cred_contents(context
, &cred
);
910 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, ccache
),
913 krb5_warn(context
, ret
, "krb5_cc_new_unique");
917 ret
= krb5_init_creds_store(context
, ctx
, tempccache
);
919 krb5_warn(context
, ret
, "krb5_init_creds_store");
923 krb5_init_creds_free(context
, ctx
);
926 ret
= krb5_cc_move(context
, tempccache
, ccache
);
928 krb5_warn(context
, ret
, "krb5_cc_move");
933 if (switch_cache_flags
)
934 krb5_cc_switch(context
, ccache
);
937 if (ntlm_domain
&& ntlmkey
.data
)
938 store_ntlmkey(context
, ccache
, ntlm_domain
, &ntlmkey
);
941 if (ok_as_delegate_flag
|| windows_flag
|| use_referrals_flag
) {
945 if (ok_as_delegate_flag
|| windows_flag
)
947 if (use_referrals_flag
|| windows_flag
)
953 krb5_cc_set_config(context
, ccache
, NULL
, "realm-config", &data
);
956 if (anonymous_pkinit
) {
959 data
.length
= strlen(principal
->realm
);
960 data
.data
= principal
->realm
;
962 krb5_cc_set_config(context
, ccache
, NULL
, "anon_pkinit_realm", &data
);
966 krb5_get_init_creds_opt_free(context
, opt
);
968 krb5_init_creds_free(context
, ctx
);
970 krb5_cc_destroy(context
, tempccache
);
979 ticket_lifetime(krb5_context context
, krb5_ccache cache
, krb5_principal client
,
980 const char *server
, time_t *renew
)
982 krb5_creds in_cred
, *cred
;
987 memset(&in_cred
, 0, sizeof(in_cred
));
992 ret
= krb5_cc_get_principal(context
, cache
, &in_cred
.client
);
994 krb5_warn(context
, ret
, "krb5_cc_get_principal");
998 /* Determine TGS principal without fallback */
999 ret
= get_server(context
, cache
, in_cred
.client
, server
, FALSE
,
1002 krb5_free_principal(context
, in_cred
.client
);
1003 krb5_warn(context
, ret
, "get_server");
1007 ret
= krb5_get_credentials(context
, KRB5_GC_CACHED
,
1008 cache
, &in_cred
, &cred
);
1009 krb5_free_principal(context
, in_cred
.client
);
1010 krb5_free_principal(context
, in_cred
.server
);
1012 krb5_warn(context
, ret
, "krb5_get_credentials");
1015 curtime
= time(NULL
);
1016 timeout
= cred
->times
.endtime
- curtime
;
1020 *renew
= cred
->times
.renew_till
- curtime
;
1024 krb5_free_creds(context
, cred
);
1028 static time_t expire
;
1030 static char siginfo_msg
[1024] = "No credentials\n";
1033 update_siginfo_msg(time_t exp
, const char *srv
)
1035 /* Note that exp is relative time */
1036 memset(siginfo_msg
, 0, sizeof(siginfo_msg
));
1037 memcpy(&siginfo_msg
, "Updating...\n", sizeof("Updating...\n"));
1040 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
1041 N_("kinit: TGT expires in %llu seconds\n", ""),
1042 (unsigned long long)expire
);
1044 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
1045 N_("kinit: Ticket for %s expired\n", ""), srv
);
1052 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
1053 N_("kinit: TGT expired\n", ""));
1055 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
1056 N_("kinit: Ticket for %s expired\n", ""), srv
);
1060 #ifdef HAVE_SIGACTION
1062 handle_siginfo(int sig
)
1064 struct iovec iov
[2];
1066 iov
[0].iov_base
= rk_UNCONST(siginfo_msg
);
1067 iov
[0].iov_len
= strlen(siginfo_msg
);
1068 iov
[1].iov_base
= "\n";
1071 writev(STDERR_FILENO
, iov
, sizeof(iov
)/sizeof(iov
[0]));
1076 krb5_context context
;
1078 krb5_principal principal
;
1079 krb5_deltat ticket_life
;
1080 krb5_deltat timeout
;
1084 renew_func(void *ptr
)
1086 krb5_error_code ret
;
1087 struct renew_ctx
*ctx
= ptr
;
1088 time_t renew_expire
;
1089 static time_t exp_delay
= 1;
1092 * NOTE: We count on the ccache implementation to notice changes to the
1093 * actual ccache filesystem/whatever objects. There should be no ccache
1094 * types for which this is not the case, but it might not hurt to
1095 * re-krb5_cc_resolve() after each successful renew_validate()/
1096 * get_new_tickets() call.
1099 expire
= ticket_lifetime(ctx
->context
, ctx
->ccache
, ctx
->principal
,
1100 server_str
, &renew_expire
);
1103 * When a keytab is available to obtain new tickets, if we are within
1104 * half of the original ticket lifetime of the renew limit, get a new
1105 * TGT instead of renewing the existing TGT. Note, ctx->ticket_life
1106 * is zero by default (without a '-l' option) and cannot be used to
1107 * set the time scale on which we decide whether we're "close to the
1110 if (use_keytab
|| keytab_str
)
1111 expire
+= ctx
->timeout
;
1112 if (renew_expire
> expire
) {
1113 ret
= renew_validate(ctx
->context
, 1, validate_flag
, &ctx
->ccache
,
1114 NULL
, FALSE
, server_str
, ctx
->ticket_life
);
1116 ret
= get_new_tickets(ctx
->context
, ctx
->principal
, ctx
->ccache
,
1117 ctx
->ticket_life
, 0, 0);
1119 expire
= ticket_lifetime(ctx
->context
, ctx
->ccache
, ctx
->principal
,
1120 server_str
, &renew_expire
);
1123 if (ret
== 0 && server_str
== NULL
&& do_afslog
&& k_hasafs())
1124 krb5_afslog(ctx
->context
, ctx
->ccache
, NULL
, NULL
);
1127 update_siginfo_msg(expire
, server_str
);
1130 * If our tickets have expired and we been able to either renew them
1131 * or obtain new tickets, then we still call this function but we use
1132 * an exponential backoff. This should take care of the case where
1133 * we are using stored credentials but the KDC has been unavailable
1134 * for some reason...
1139 * We can't ask to keep spamming stderr but not syslog, so we warn
1142 if (exp_delay
== 1) {
1143 krb5_warnx(ctx
->context
, N_("NOTICE: Could not renew/refresh "
1146 if (exp_delay
< 7200)
1147 exp_delay
+= exp_delay
/ 2 + 1;
1152 return expire
/ 2 + 1;
1156 set_princ_realm(krb5_context context
,
1157 krb5_principal principal
,
1160 krb5_error_code ret
;
1162 if ((ret
= krb5_principal_set_realm(context
, principal
, realm
)) != 0)
1163 krb5_err(context
, 1, ret
, "krb5_principal_set_realm");
1167 parse_name_realm(krb5_context context
,
1171 krb5_principal
*princ
)
1173 krb5_error_code ret
;
1176 flags
|= KRB5_PRINCIPAL_PARSE_NO_DEF_REALM
;
1177 if ((ret
= krb5_parse_name_flags(context
, name
, flags
, princ
)) != 0)
1178 krb5_err(context
, 1, ret
, "krb5_parse_name_flags");
1179 if (realm
&& krb5_principal_get_realm(context
, *princ
) == NULL
)
1180 set_princ_realm(context
, *princ
, realm
);
1184 get_default_realm(krb5_context context
)
1187 krb5_error_code ret
;
1189 if ((ret
= krb5_get_default_realm(context
, &realm
)) != 0)
1190 krb5_err(context
, 1, ret
, "krb5_get_default_realm");
1195 get_default_principal(krb5_context context
, krb5_principal
*princ
)
1197 krb5_error_code ret
;
1199 if ((ret
= krb5_get_default_principal(context
, princ
)) != 0)
1200 krb5_err(context
, 1, ret
, "krb5_get_default_principal");
1204 get_user_realm(krb5_context context
)
1206 krb5_error_code ret
;
1207 char *user_realm
= NULL
;
1210 * If memory allocation fails, we don't try to use the wrong realm,
1211 * that will trigger misleading error messages complicate support.
1213 krb5_appdefault_string(context
, "kinit", NULL
, "user_realm", "",
1215 if (user_realm
== NULL
) {
1216 ret
= krb5_enomem(context
);
1217 krb5_err(context
, 1, ret
, "krb5_appdefault_string");
1220 if (*user_realm
== 0) {
1229 get_princ(krb5_context context
,
1230 krb5_principal
*principal
,
1234 krb5_error_code ret
= 0;
1240 krb5_ccache ccache
= NULL
;
1242 /* If credential cache provides a client principal, use that. */
1244 ret
= krb5_cc_resolve(context
, ccname
, &ccache
);
1246 ret
= krb5_cc_default(context
, &ccache
);
1248 ret
= krb5_cc_get_principal(context
, ccache
, principal
);
1249 krb5_cc_close(context
, ccache
);
1254 user_realm
= get_user_realm(context
);
1257 if (enterprise_flag
)
1258 parseflags
|= KRB5_PRINCIPAL_PARSE_ENTERPRISE
;
1260 parse_name_realm(context
, name
, parseflags
, user_realm
, &tmp
);
1262 if (user_realm
&& krb5_principal_get_num_comp(context
, tmp
) > 1) {
1263 /* Principal is instance qualified, reparse with default realm. */
1264 krb5_free_principal(context
, tmp
);
1265 parse_name_realm(context
, name
, parseflags
, NULL
, principal
);
1270 get_default_principal(context
, principal
);
1272 set_princ_realm(context
, *principal
, user_realm
);
1280 get_princ_kt(krb5_context context
,
1281 krb5_principal
*principal
,
1284 krb5_error_code ret
;
1287 krb5_kt_cursor cursor
;
1288 krb5_keytab_entry entry
;
1293 * If the credential cache exists and specifies a client principal,
1296 if (krb5_cc_default(context
, &ccache
) == 0) {
1297 ret
= krb5_cc_get_principal(context
, ccache
, principal
);
1298 krb5_cc_close(context
, ccache
);
1305 /* If the principal specifies an explicit realm, just use that. */
1306 int parseflags
= KRB5_PRINCIPAL_PARSE_NO_DEF_REALM
;
1308 parse_name_realm(context
, name
, parseflags
, NULL
, &tmp
);
1309 if (krb5_principal_get_realm(context
, tmp
) != NULL
) {
1314 /* Otherwise, search keytab for bare name of the default principal. */
1315 get_default_principal(context
, &tmp
);
1316 set_princ_realm(context
, tmp
, NULL
);
1319 def_realm
= get_default_realm(context
);
1321 ret
= krb5_kt_start_seq_get(context
, kt
, &cursor
);
1323 krb5_err(context
, 1, ret
, "krb5_kt_start_seq_get");
1326 krb5_kt_next_entry(context
, kt
, &entry
, &cursor
) == 0) {
1329 if (!krb5_principal_compare_any_realm(context
, tmp
, entry
.principal
))
1332 krb5_principal_compare(context
, *principal
, entry
.principal
))
1334 /* The default realm takes precedence */
1335 realm
= krb5_principal_get_realm(context
, entry
.principal
);
1336 if (*principal
&& strcmp(def_realm
, realm
) == 0) {
1337 krb5_free_principal(context
, *principal
);
1338 ret
= krb5_copy_principal(context
, entry
.principal
, principal
);
1342 ret
= krb5_copy_principal(context
, entry
.principal
, principal
);
1344 if (ret
!= 0 || (ret
= krb5_kt_end_seq_get(context
, kt
, &cursor
)) != 0)
1345 krb5_err(context
, 1, ret
, "get_princ_kt");
1348 parse_name_realm(context
, name
, 0, NULL
, principal
);
1350 krb5_err(context
, 1, KRB5_CC_NOTFOUND
, "get_princ_kt");
1353 krb5_free_principal(context
, tmp
);
1358 main(int argc
, char **argv
)
1360 krb5_error_code ret
;
1361 krb5_context context
;
1363 krb5_principal principal
= NULL
;
1365 krb5_deltat ticket_life
= 0;
1366 #ifdef HAVE_SIGACTION
1367 struct sigaction sa
;
1369 krb5_boolean unique_ccache
= FALSE
;
1370 krb5_boolean historical_anon_pkinit
= FALSE
;
1371 int anonymous_pkinit
= FALSE
;
1373 setprogname(argv
[0]);
1375 setlocale(LC_ALL
, "");
1376 bindtextdomain("heimdal_kuser", HEIMDAL_LOCALEDIR
);
1377 textdomain("heimdal_kuser");
1379 ret
= krb5_init_context(&context
);
1380 if (ret
== KRB5_CONFIG_BADFORMAT
)
1381 errx(1, "krb5_init_context failed to parse configuration file");
1383 errx(1, "krb5_init_context failed: %d", ret
);
1385 if (getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
1392 print_version(NULL
);
1399 krb5_appdefault_boolean(context
, "kinit", NULL
, "historical_anon_pkinit",
1400 FALSE
, &historical_anon_pkinit
);
1403 * Open the keytab now, we use the keytab to determine the principal's
1404 * realm when the requested principal has no realm.
1406 if (use_keytab
|| keytab_str
) {
1408 ret
= krb5_kt_resolve(context
, keytab_str
, &kt
);
1410 ret
= krb5_kt_default(context
, &kt
);
1412 krb5_err(context
, 1, ret
, "resolving keytab");
1415 if (pk_enterprise_flag
) {
1416 ret
= krb5_pk_enterprise_cert(context
, pk_user_id
,
1417 argv
[0], &principal
,
1420 krb5_err(context
, 1, ret
, "krb5_pk_enterprise_certs");
1424 } else if (anonymous_flag
&& argc
&& argv
[0][0] == '@') {
1425 /* If principal argument as @REALM, try anonymous PKINIT */
1427 ret
= krb5_make_principal(context
, &principal
, &argv
[0][1],
1428 KRB5_WELLKNOWN_NAME
, KRB5_ANON_NAME
,
1431 krb5_err(context
, 1, ret
, "krb5_make_principal");
1432 krb5_principal_set_type(context
, principal
, KRB5_NT_WELLKNOWN
);
1433 anonymous_pkinit
= TRUE
;
1434 } else if (anonymous_flag
&& historical_anon_pkinit
) {
1435 char *realm
= argc
== 0 ? get_default_realm(context
) :
1436 argv
[0][0] == '@' ? &argv
[0][1] : argv
[0];
1438 ret
= krb5_make_principal(context
, &principal
, realm
,
1439 KRB5_WELLKNOWN_NAME
, KRB5_ANON_NAME
, NULL
);
1441 krb5_err(context
, 1, ret
, "krb5_make_principal");
1442 krb5_principal_set_type(context
, principal
, KRB5_NT_WELLKNOWN
);
1443 anonymous_pkinit
= TRUE
;
1444 } else if (use_keytab
|| keytab_str
) {
1445 get_princ_kt(context
, &principal
, argv
[0]);
1447 get_princ(context
, &principal
, cred_cache
, argv
[0]);
1451 krb5_set_fcache_version(context
, fcache_version
);
1453 if (renewable_flag
== -1)
1454 /* this seems somewhat pointless, but whatever */
1455 krb5_appdefault_boolean(context
, "kinit",
1456 krb5_principal_get_realm(context
, principal
),
1457 "renewable", FALSE
, &renewable_flag
);
1458 if (do_afslog
== -1)
1459 krb5_appdefault_boolean(context
, "kinit",
1460 krb5_principal_get_realm(context
, principal
),
1461 "afslog", TRUE
, &do_afslog
);
1466 * - use the given ccache
1467 * - use a new unique ccache for running a command with (in this case we
1468 * get to set KRB5CCNAME, so a new unique ccache makes sense)
1469 * - use the default ccache for the given principal and maybe later switch
1470 * the collection's default/primary to it
1472 * The important thing is that, except for the case where we're running a
1473 * command, we _can't set KRB5CCNAME_, and we can't expect the user to read
1474 * our output and figure out to set it (we could have an output-for-shell-
1475 * eval mode, like ssh-agent and such, but we don't). Therefore, in all
1476 * cases where we can't set KRB5CCNAME we must do something that makes
1477 * sense to the user, and that is to either initialize a given ccache, use
1478 * the default, or use a subsidiary ccache named after the principal whose
1479 * creds we're initializing.
1482 /* Use the given ccache */
1483 ret
= krb5_cc_resolve(context
, cred_cache
, &ccache
);
1484 } else if (argc
> 1) {
1488 * A command was given, so use a new unique ccache (and destroy it
1491 ret
= krb5_cc_new_unique(context
, NULL
, NULL
, &ccache
);
1493 krb5_err(context
, 1, ret
, "creating cred cache");
1494 snprintf(s
, sizeof(s
), "%s:%s",
1495 krb5_cc_get_type(context
, ccache
),
1496 krb5_cc_get_name(context
, ccache
));
1497 setenv("KRB5CCNAME", s
, 1);
1498 unique_ccache
= TRUE
;
1499 } else if (default_for
) {
1500 ret
= krb5_cc_default_for(context
, principal
, &ccache
);
1501 if (switch_cache_flags
== -1)
1502 switch_cache_flags
= 0;
1504 ret
= krb5_cc_default(context
, &ccache
);
1505 if (switch_cache_flags
== -1)
1506 switch_cache_flags
= 0;
1509 if (switch_cache_flags
== -1)
1510 switch_cache_flags
= 1;
1513 krb5_err(context
, 1, ret
, N_("resolving credentials cache", ""));
1516 if (argc
> 1 && k_hasafs())
1521 int tmp
= parse_time(lifetime
, "s");
1523 errx(1, N_("unparsable time: %s", ""), lifetime
);
1528 if (addrs_flag
== 0 && extra_addresses
.num_strings
> 0)
1529 krb5_errx(context
, 1,
1530 N_("specifying both extra addresses and "
1531 "no addresses makes no sense", ""));
1534 krb5_addresses addresses
;
1535 memset(&addresses
, 0, sizeof(addresses
));
1536 for(i
= 0; i
< extra_addresses
.num_strings
; i
++) {
1537 ret
= krb5_parse_address(context
, extra_addresses
.strings
[i
],
1540 krb5_add_extra_addresses(context
, &addresses
);
1541 krb5_free_addresses(context
, &addresses
);
1544 free_getarg_strings(&extra_addresses
);
1547 if (renew_flag
|| validate_flag
) {
1548 ret
= renew_validate(context
, renew_flag
, validate_flag
,
1550 default_for
? TRUE
: FALSE
, server_str
,
1554 if (ret
== 0 && server_str
== NULL
&& do_afslog
&& k_hasafs())
1555 krb5_afslog(context
, ccache
, NULL
, NULL
);
1559 krb5_cc_destroy(context
, ccache
);
1563 ret
= get_new_tickets(context
, principal
, ccache
, ticket_life
,
1564 1, anonymous_pkinit
);
1567 krb5_cc_destroy(context
, ccache
);
1572 if (ret
== 0 && server_str
== NULL
&& do_afslog
&& k_hasafs())
1573 krb5_afslog(context
, ccache
, NULL
, NULL
);
1577 struct renew_ctx ctx
;
1580 timeout
= ticket_lifetime(context
, ccache
, principal
,
1581 server_str
, NULL
) / 2;
1583 ctx
.context
= context
;
1584 ctx
.ccache
= ccache
;
1585 ctx
.principal
= principal
;
1586 ctx
.ticket_life
= ticket_life
;
1587 ctx
.timeout
= timeout
;
1589 #ifdef HAVE_SIGACTION
1590 memset(&sa
, 0, sizeof(sa
));
1591 sigemptyset(&sa
.sa_mask
);
1592 sa
.sa_handler
= handle_siginfo
;
1594 sigaction(SIGINFO
, &sa
, NULL
);
1597 ret
= simple_execvp_timed(argv
[1], argv
+1,
1598 renew_func
, &ctx
, timeout
);
1599 #define EX_NOEXEC 126
1600 #define EX_NOTFOUND 127
1601 if (ret
== EX_NOEXEC
)
1602 krb5_warnx(context
, N_("permission denied: %s", ""), argv
[1]);
1603 else if (ret
== EX_NOTFOUND
)
1604 krb5_warnx(context
, N_("command not found: %s", ""), argv
[1]);
1606 krb5_cc_destroy(context
, ccache
);
1612 krb5_cc_close(context
, ccache
);
1615 krb5_free_principal(context
, principal
);
1617 krb5_kt_close(context
, kt
);
1618 krb5_free_context(context
);