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"
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 char *cred_cache
= NULL
;
65 char *start_str
= NULL
;
66 static int switch_cache_flags
= 1;
67 struct getarg_strings etype_str
;
69 char *keytab_str
= NULL
;
70 static krb5_keytab kt
= NULL
;
73 char *password_file
= NULL
;
74 char *pk_user_id
= NULL
;
75 int pk_enterprise_flag
= 0;
76 struct hx509_certs_data
*ent_user_id
= NULL
;
77 char *pk_x509_anchors
= NULL
;
78 int pk_use_enckey
= 0;
79 static int canonicalize_flag
= 0;
80 static int enterprise_flag
= 0;
81 static int ok_as_delegate_flag
= 0;
82 static char *fast_armor_cache_string
= NULL
;
83 static int use_referrals_flag
= 0;
84 static int windows_flag
= 0;
86 static char *ntlm_domain
;
90 static struct getargs args
[] = {
104 { "afslog", 0 , arg_flag
, &do_afslog
,
105 NP_("obtain afs tokens", ""), NULL
},
107 { "cache", 'c', arg_string
, &cred_cache
,
108 NP_("credentials cache", ""), "cachename" },
110 { "forwardable", 'F', arg_negative_flag
, &forwardable_flag
,
111 NP_("get tickets not forwardable", ""), NULL
},
113 { NULL
, 'f', arg_flag
, &forwardable_flag
,
114 NP_("get forwardable tickets", ""), NULL
},
116 { "keytab", 't', arg_string
, &keytab_str
,
117 NP_("keytab to use", ""), "keytabname" },
119 { "lifetime", 'l', arg_string
, &lifetime
,
120 NP_("lifetime of tickets", ""), "time" },
122 { "proxiable", 'p', arg_flag
, &proxiable_flag
,
123 NP_("get proxiable tickets", ""), NULL
},
125 { "renew", 'R', arg_flag
, &renew_flag
,
126 NP_("renew TGT", ""), NULL
},
128 { "renewable", 0, arg_flag
, &renewable_flag
,
129 NP_("get renewable tickets", ""), NULL
},
131 { "renewable-life", 'r', arg_string
, &renew_life
,
132 NP_("renewable lifetime of tickets", ""), "time" },
134 { "server", 'S', arg_string
, &server_str
,
135 NP_("server to get ticket for", ""), "principal" },
137 { "start-time", 's', arg_string
, &start_str
,
138 NP_("when ticket gets valid", ""), "time" },
140 { "use-keytab", 'k', arg_flag
, &use_keytab
,
141 NP_("get key from keytab", ""), NULL
},
143 { "validate", 'v', arg_flag
, &validate_flag
,
144 NP_("validate TGT", ""), NULL
},
146 { "enctypes", 'e', arg_strings
, &etype_str
,
147 NP_("encryption types to use", ""), "enctypes" },
149 { "fcache-version", 0, arg_integer
, &fcache_version
,
150 NP_("file cache version to create", ""), NULL
},
152 { "addresses", 'A', arg_negative_flag
, &addrs_flag
,
153 NP_("request a ticket with no addresses", ""), NULL
},
155 { "extra-addresses",'a', arg_strings
, &extra_addresses
,
156 NP_("include these extra addresses", ""), "addresses" },
158 { "anonymous", 0, arg_flag
, &anonymous_flag
,
159 NP_("request an anonymous ticket", ""), NULL
},
161 { "request-pac", 0, arg_flag
, &pac_flag
,
162 NP_("request a Windows PAC", ""), NULL
},
164 { "password-file", 0, arg_string
, &password_file
,
165 NP_("read the password from a file", ""), NULL
},
167 { "canonicalize",0, arg_flag
, &canonicalize_flag
,
168 NP_("canonicalize client principal", ""), NULL
},
170 { "enterprise",0, arg_flag
, &enterprise_flag
,
171 NP_("parse principal as a KRB5-NT-ENTERPRISE name", ""), NULL
},
173 { "pk-enterprise", 0, arg_flag
, &pk_enterprise_flag
,
174 NP_("use enterprise name from certificate", ""), NULL
},
176 { "pk-user", 'C', arg_string
, &pk_user_id
,
177 NP_("principal's public/private/certificate identifier", ""), "id" },
179 { "x509-anchors", 'D', arg_string
, &pk_x509_anchors
,
180 NP_("directory with CA certificates", ""), "directory" },
182 { "pk-use-enckey", 0, arg_flag
, &pk_use_enckey
,
183 NP_("Use RSA encrypted reply (instead of DH)", ""), NULL
},
186 { "ntlm-domain", 0, arg_string
, &ntlm_domain
,
187 NP_("NTLM domain", ""), "domain" },
190 { "change-default", 0, arg_negative_flag
, &switch_cache_flags
,
191 NP_("switch the default cache to the new credentials cache", ""), NULL
},
193 { "ok-as-delegate", 0, arg_flag
, &ok_as_delegate_flag
,
194 NP_("honor ok-as-delegate on tickets", ""), NULL
},
196 { "fast-armor-cache", 0, arg_string
, &fast_armor_cache_string
,
197 NP_("use this credential cache as FAST armor cache", ""), "cache" },
199 { "use-referrals", 0, arg_flag
, &use_referrals_flag
,
200 NP_("only use referrals, no dns canalisation", ""), NULL
},
202 { "windows", 0, arg_flag
, &windows_flag
,
203 NP_("get windows behavior", ""), NULL
},
205 { "version", 0, arg_flag
, &version_flag
, NULL
, NULL
},
206 { "help", 0, arg_flag
, &help_flag
, NULL
, NULL
}
212 arg_printusage_i18n(args
, sizeof(args
)/sizeof(*args
), N_("Usage: ", ""),
213 NULL
, "[principal [command]]", getarg_i18n
);
217 static krb5_error_code
218 get_server(krb5_context context
,
219 krb5_principal client
,
221 krb5_principal
*princ
)
223 krb5_const_realm realm
;
225 return krb5_parse_name(context
, server
, princ
);
227 realm
= krb5_principal_get_realm(context
, client
);
228 return krb5_make_principal(context
, princ
, realm
,
229 KRB5_TGS_NAME
, realm
, NULL
);
232 static krb5_error_code
233 copy_configs(krb5_context context
,
236 krb5_principal start_ticket_server
)
239 const char *cfg_names
[] = {"realm-config", "FriendlyName", NULL
};
240 const char *cfg_names_w_pname
[] = {"fast_avail", NULL
};
244 for (i
= 0; cfg_names
[i
]; i
++) {
245 ret
= krb5_cc_get_config(context
, src
, NULL
, cfg_names
[i
], &cfg_data
);
246 if (ret
== KRB5_CC_NOTFOUND
|| ret
== KRB5_CC_END
) {
249 krb5_warn(context
, ret
, "krb5_cc_get_config");
252 ret
= krb5_cc_set_config(context
, dst
, NULL
, cfg_names
[i
], &cfg_data
);
254 krb5_warn(context
, ret
, "krb5_cc_set_config");
256 for (i
= 0; start_ticket_server
&& cfg_names_w_pname
[i
]; i
++) {
257 ret
= krb5_cc_get_config(context
, src
, start_ticket_server
,
258 cfg_names_w_pname
[i
], &cfg_data
);
259 if (ret
== KRB5_CC_NOTFOUND
|| ret
== KRB5_CC_END
) {
262 krb5_warn(context
, ret
, "krb5_cc_get_config");
265 ret
= krb5_cc_set_config(context
, dst
, start_ticket_server
,
266 cfg_names_w_pname
[i
], &cfg_data
);
267 if (ret
&& ret
!= KRB5_CC_NOTFOUND
)
268 krb5_warn(context
, ret
, "krb5_cc_set_config");
271 * We don't copy cc configs for any other principals though (mostly
272 * those are per-target time offsets and the like, so it's bad to
273 * lose them, but hardly the end of the world, and as they may not
274 * expire anyways, it's good to let them go).
279 static krb5_error_code
280 renew_validate(krb5_context context
,
288 krb5_ccache tempccache
= NULL
;
289 krb5_creds in
, *out
= NULL
;
290 krb5_kdc_flags flags
;
292 memset(&in
, 0, sizeof(in
));
294 ret
= krb5_cc_get_principal(context
, cache
, &in
.client
);
296 krb5_warn(context
, ret
, "krb5_cc_get_principal");
299 ret
= get_server(context
, in
.client
, server
, &in
.server
);
301 krb5_warn(context
, ret
, "get_server");
307 * no need to check the error here, it's only to be
308 * friendly to the user
310 krb5_get_credentials(context
, KRB5_GC_CACHED
, cache
, &in
, &out
);
314 flags
.b
.renewable
= flags
.b
.renew
= renew
;
315 flags
.b
.validate
= validate
;
317 if (forwardable_flag
!= -1)
318 flags
.b
.forwardable
= forwardable_flag
;
320 flags
.b
.forwardable
= out
->flags
.b
.forwardable
;
322 if (proxiable_flag
!= -1)
323 flags
.b
.proxiable
= proxiable_flag
;
325 flags
.b
.proxiable
= out
->flags
.b
.proxiable
;
328 flags
.b
.request_anonymous
= anonymous_flag
;
330 in
.times
.endtime
= time(NULL
) + life
;
333 krb5_free_creds(context
, out
);
338 ret
= krb5_get_kdc_cred(context
,
346 krb5_warn(context
, ret
, "krb5_get_kdc_cred");
350 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, cache
),
353 krb5_warn(context
, ret
, "krb5_cc_new_unique");
357 ret
= krb5_cc_initialize(context
, tempccache
, in
.client
);
359 krb5_warn(context
, ret
, "krb5_cc_initialize");
363 ret
= krb5_cc_store_cred(context
, tempccache
, out
);
365 krb5_warn(context
, ret
, "krb5_cc_store_cred");
370 * We want to preserve cc configs as some are security-relevant, and
371 * anyways it's the friendly thing to do.
373 ret
= copy_configs(context
, tempccache
, cache
, out
->server
);
377 ret
= krb5_cc_move(context
, tempccache
, cache
);
379 krb5_warn(context
, ret
, "krb5_cc_move");
386 krb5_cc_close(context
, tempccache
);
388 krb5_free_creds(context
, out
);
389 krb5_free_cred_contents(context
, &in
);
395 static krb5_error_code
396 store_ntlmkey(krb5_context context
, krb5_ccache id
,
397 const char *domain
, struct ntlm_buf
*buf
)
404 ret
= krb5_cc_get_config(context
, id
, NULL
, "default-ntlm-domain", &data
);
406 krb5_data_free(&data
);
408 data
.length
= strlen(domain
);
409 data
.data
= rk_UNCONST(domain
);
410 ret
= krb5_cc_set_config(context
, id
, NULL
, "default-ntlm-domain", &data
);
415 aret
= asprintf(&name
, "ntlm-key-%s", domain
);
416 if (aret
== -1 || name
== NULL
)
417 return krb5_enomem(context
);
419 data
.length
= buf
->length
;
420 data
.data
= buf
->data
;
422 ret
= krb5_cc_set_config(context
, id
, NULL
, name
, &data
);
428 static krb5_error_code
429 get_new_tickets(krb5_context context
,
430 krb5_principal principal
,
432 krb5_deltat ticket_life
,
438 krb5_deltat start_time
= 0;
439 krb5_deltat renew
= 0;
440 const char *renewstr
= NULL
;
441 krb5_enctype
*enctype
= NULL
;
442 krb5_ccache tempccache
= NULL
;
443 krb5_init_creds_context ctx
= NULL
;
444 krb5_get_init_creds_opt
*opt
= NULL
;
445 krb5_prompter_fct prompter
= krb5_prompter_posix
;
447 struct ntlm_buf ntlmkey
;
448 memset(&ntlmkey
, 0, sizeof(ntlmkey
));
458 if (strcasecmp("STDIN", password_file
) == 0)
461 f
= fopen(password_file
, "r");
463 krb5_warnx(context
, "Failed to open the password file %s",
468 if (fgets(passwd
, sizeof(passwd
), f
) == NULL
) {
469 krb5_warnx(context
, N_("Failed to read password from file %s", ""),
472 return EINVAL
; /* XXX Need a better error */
476 passwd
[strcspn(passwd
, "\n")] = '\0';
480 if (passwd
[0] == '\0') {
487 realm
= krb5_principal_get_realm(context
, principal
);
489 ret
= krb5_unparse_name_flags(context
, principal
,
490 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &name
);
494 osret
= SecKeychainFindGenericPassword(NULL
, strlen(realm
), realm
,
496 &length
, &buffer
, NULL
);
498 if (osret
== noErr
&& length
< sizeof(passwd
) - 1) {
499 memcpy(passwd
, buffer
, length
);
500 passwd
[length
] = '\0';
507 memset(&cred
, 0, sizeof(cred
));
509 ret
= krb5_get_init_creds_opt_alloc(context
, &opt
);
511 krb5_warn(context
, ret
, "krb5_get_init_creds_opt_alloc");
515 krb5_get_init_creds_opt_set_default_flags(context
, "kinit",
516 krb5_principal_get_realm(context
, principal
), opt
);
518 if (forwardable_flag
!= -1)
519 krb5_get_init_creds_opt_set_forwardable(opt
, forwardable_flag
);
520 if (proxiable_flag
!= -1)
521 krb5_get_init_creds_opt_set_proxiable(opt
, proxiable_flag
);
523 krb5_get_init_creds_opt_set_anonymous(opt
, anonymous_flag
);
525 krb5_get_init_creds_opt_set_pac_request(context
, opt
,
526 pac_flag
? TRUE
: FALSE
);
527 if (canonicalize_flag
)
528 krb5_get_init_creds_opt_set_canonicalize(context
, opt
, TRUE
);
529 if (pk_enterprise_flag
|| enterprise_flag
|| canonicalize_flag
|| windows_flag
)
530 krb5_get_init_creds_opt_set_win2k(context
, opt
, TRUE
);
531 if (pk_user_id
|| ent_user_id
|| anonymous_flag
) {
532 ret
= krb5_get_init_creds_opt_set_pkinit(context
, opt
,
538 pk_use_enckey
? 2 : 0 |
539 anonymous_flag
? 4 : 0,
544 krb5_warn(context
, ret
, "krb5_get_init_creds_opt_set_pkinit");
548 krb5_get_init_creds_opt_set_pkinit_user_certs(context
, opt
, ent_user_id
);
551 if (addrs_flag
!= -1)
552 krb5_get_init_creds_opt_set_addressless(context
, opt
,
553 addrs_flag
? FALSE
: TRUE
);
555 if (renew_life
== NULL
&& renewable_flag
)
556 renewstr
= "1 month";
558 renewstr
= renew_life
;
560 renew
= parse_time(renewstr
, "s");
562 errx(1, "unparsable time: %s", renewstr
);
564 krb5_get_init_creds_opt_set_renew_life(opt
, renew
);
567 if (ticket_life
!= 0)
568 krb5_get_init_creds_opt_set_tkt_life(opt
, ticket_life
);
571 int tmp
= parse_time(start_str
, "s");
573 errx(1, N_("unparsable time: %s", ""), start_str
);
578 if (etype_str
.num_strings
) {
581 enctype
= malloc(etype_str
.num_strings
* sizeof(*enctype
));
583 errx(1, "out of memory");
584 for(i
= 0; i
< etype_str
.num_strings
; i
++) {
585 ret
= krb5_string_to_enctype(context
,
586 etype_str
.strings
[i
],
589 errx(1, "unrecognized enctype: %s", etype_str
.strings
[i
]);
591 krb5_get_init_creds_opt_set_etype_list(opt
, enctype
,
592 etype_str
.num_strings
);
595 ret
= krb5_init_creds_init(context
, principal
, prompter
, NULL
, start_time
, opt
, &ctx
);
597 krb5_warn(context
, ret
, "krb5_init_creds_init");
602 ret
= krb5_init_creds_set_service(context
, ctx
, server_str
);
604 krb5_warn(context
, ret
, "krb5_init_creds_set_service");
609 if (fast_armor_cache_string
) {
612 ret
= krb5_cc_resolve(context
, fast_armor_cache_string
, &fastid
);
614 krb5_warn(context
, ret
, "krb5_cc_resolve(FAST cache)");
618 ret
= krb5_init_creds_set_fast_ccache(context
, ctx
, fastid
);
620 krb5_warn(context
, ret
, "krb5_init_creds_set_fast_ccache");
625 if (use_keytab
|| keytab_str
) {
626 ret
= krb5_init_creds_set_keytab(context
, ctx
, kt
);
628 krb5_warn(context
, ret
, "krb5_init_creds_set_keytab");
631 } else if (pk_user_id
|| ent_user_id
|| anonymous_flag
) {
633 } else if (!interactive
&& passwd
[0] == '\0') {
634 static int already_warned
= 0;
637 krb5_warnx(context
, "Not interactive, failed to get "
639 krb5_get_init_creds_opt_free(context
, opt
);
644 if (passwd
[0] == '\0') {
648 ret
= krb5_unparse_name(context
, principal
, &p
);
650 errx(1, "failed to generate passwd prompt: not enough memory");
652 aret
= asprintf(&prompt
, N_("%s's Password: ", ""), p
);
655 errx(1, "failed to generate passwd prompt: not enough memory");
657 if (UI_UTIL_read_pw_string(passwd
, sizeof(passwd
)-1, prompt
, 0)){
658 memset(passwd
, 0, sizeof(passwd
));
659 errx(1, "failed to read password");
665 ret
= krb5_init_creds_set_password(context
, ctx
, passwd
);
667 krb5_warn(context
, ret
, "krb5_init_creds_set_password");
673 ret
= krb5_init_creds_get(context
, ctx
);
676 if (ntlm_domain
&& passwd
[0])
677 heim_ntlm_nt_key(passwd
, &ntlmkey
);
679 memset(passwd
, 0, sizeof(passwd
));
684 case KRB5_LIBOS_PWDINTR
: /* don't print anything if it was just C-c:ed */
686 case KRB5KRB_AP_ERR_BAD_INTEGRITY
:
687 case KRB5KRB_AP_ERR_MODIFIED
:
688 case KRB5KDC_ERR_PREAUTH_FAILED
:
689 case KRB5_GET_IN_TKT_LOOP
:
690 krb5_warnx(context
, N_("Password incorrect", ""));
692 case KRB5KRB_AP_ERR_V4_REPLY
:
693 krb5_warnx(context
, N_("Looks like a Kerberos 4 reply", ""));
695 case KRB5KDC_ERR_KEY_EXPIRED
:
696 krb5_warnx(context
, N_("Password expired", ""));
699 krb5_warn(context
, ret
, "krb5_get_init_creds");
703 krb5_process_last_request(context
, opt
, ctx
);
705 ret
= krb5_init_creds_get_creds(context
, ctx
, &cred
);
707 krb5_warn(context
, ret
, "krb5_init_creds_get_creds");
711 if (ticket_life
!= 0) {
712 if (labs(cred
.times
.endtime
- cred
.times
.starttime
- ticket_life
) > 30) {
714 unparse_time_approx(cred
.times
.endtime
- cred
.times
.starttime
,
716 krb5_warnx(context
, N_("NOTICE: ticket lifetime is %s", ""), life
);
720 if (labs(cred
.times
.renew_till
- cred
.times
.starttime
- renew
) > 30) {
722 unparse_time_approx(cred
.times
.renew_till
- cred
.times
.starttime
,
725 N_("NOTICE: ticket renewable lifetime is %s", ""),
729 krb5_free_cred_contents(context
, &cred
);
731 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, ccache
),
734 krb5_warn(context
, ret
, "krb5_cc_new_unique");
738 ret
= krb5_init_creds_store(context
, ctx
, tempccache
);
740 krb5_warn(context
, ret
, "krb5_init_creds_store");
744 krb5_init_creds_free(context
, ctx
);
747 ret
= krb5_cc_move(context
, tempccache
, ccache
);
749 krb5_warn(context
, ret
, "krb5_cc_move");
754 if (switch_cache_flags
)
755 krb5_cc_switch(context
, ccache
);
758 if (ntlm_domain
&& ntlmkey
.data
)
759 store_ntlmkey(context
, ccache
, ntlm_domain
, &ntlmkey
);
762 if (ok_as_delegate_flag
|| windows_flag
|| use_referrals_flag
) {
766 if (ok_as_delegate_flag
|| windows_flag
)
768 if (use_referrals_flag
|| windows_flag
)
774 krb5_cc_set_config(context
, ccache
, NULL
, "realm-config", &data
);
778 krb5_get_init_creds_opt_free(context
, opt
);
780 krb5_init_creds_free(context
, ctx
);
782 krb5_cc_close(context
, tempccache
);
791 ticket_lifetime(krb5_context context
, krb5_ccache cache
, krb5_principal client
,
792 const char *server
, time_t *renew
)
794 krb5_creds in_cred
, *cred
;
799 memset(&in_cred
, 0, sizeof(in_cred
));
801 ret
= krb5_cc_get_principal(context
, cache
, &in_cred
.client
);
803 krb5_warn(context
, ret
, "krb5_cc_get_principal");
806 ret
= get_server(context
, in_cred
.client
, server
, &in_cred
.server
);
808 krb5_free_principal(context
, in_cred
.client
);
809 krb5_warn(context
, ret
, "get_server");
813 ret
= krb5_get_credentials(context
, KRB5_GC_CACHED
,
814 cache
, &in_cred
, &cred
);
815 krb5_free_principal(context
, in_cred
.client
);
816 krb5_free_principal(context
, in_cred
.server
);
818 krb5_warn(context
, ret
, "krb5_get_credentials");
821 curtime
= time(NULL
);
822 timeout
= cred
->times
.endtime
- curtime
;
826 *renew
= cred
->times
.renew_till
- curtime
;
830 krb5_free_creds(context
, cred
);
834 static time_t expire
;
836 static char siginfo_msg
[1024] = "No credentials\n";
839 update_siginfo_msg(time_t exp
, const char *srv
)
841 /* Note that exp is relative time */
842 memset(siginfo_msg
, 0, sizeof(siginfo_msg
));
843 memcpy(&siginfo_msg
, "Updating...\n", sizeof("Updating...\n"));
846 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
847 N_("kinit: TGT expires in %llu seconds\n", ""),
848 (unsigned long long)expire
);
850 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
851 N_("kinit: Ticket for %s expired\n", ""), srv
);
858 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
859 N_("kinit: TGT expired\n", ""));
861 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
862 N_("kinit: Ticket for %s expired\n", ""), srv
);
866 #ifdef HAVE_SIGACTION
868 handle_siginfo(int sig
)
872 iov
[0].iov_base
= rk_UNCONST(siginfo_msg
);
873 iov
[0].iov_len
= strlen(siginfo_msg
);
874 iov
[1].iov_base
= "\n";
877 writev(STDERR_FILENO
, iov
, sizeof(iov
)/sizeof(iov
[0]));
882 krb5_context context
;
884 krb5_principal principal
;
885 krb5_deltat ticket_life
;
890 renew_func(void *ptr
)
893 struct renew_ctx
*ctx
= ptr
;
895 static time_t exp_delay
= 1;
898 * NOTE: We count on the ccache implementation to notice changes to the
899 * actual ccache filesystem/whatever objects. There should be no ccache
900 * types for which this is not the case, but it might not hurt to
901 * re-krb5_cc_resolve() after each successful renew_validate()/
902 * get_new_tickets() call.
905 expire
= ticket_lifetime(ctx
->context
, ctx
->ccache
, ctx
->principal
,
906 server_str
, &renew_expire
);
909 * When a keytab is available to obtain new tickets, if we are within
910 * half of the original ticket lifetime of the renew limit, get a new
911 * TGT instead of renewing the existing TGT. Note, ctx->ticket_life
912 * is zero by default (without a '-l' option) and cannot be used to
913 * set the time scale on which we decide whether we're "close to the
916 if (use_keytab
|| keytab_str
)
917 expire
+= ctx
->timeout
;
918 if (renew_expire
> expire
) {
919 ret
= renew_validate(ctx
->context
, 1, validate_flag
, ctx
->ccache
,
920 server_str
, ctx
->ticket_life
);
922 ret
= get_new_tickets(ctx
->context
, ctx
->principal
, ctx
->ccache
,
923 ctx
->ticket_life
, 0);
925 expire
= ticket_lifetime(ctx
->context
, ctx
->ccache
, ctx
->principal
,
926 server_str
, &renew_expire
);
929 if (ret
== 0 && server_str
== NULL
&& do_afslog
&& k_hasafs())
930 krb5_afslog(ctx
->context
, ctx
->ccache
, NULL
, NULL
);
933 update_siginfo_msg(expire
, server_str
);
936 * If our tickets have expired and we been able to either renew them
937 * or obtain new tickets, then we still call this function but we use
938 * an exponential backoff. This should take care of the case where
939 * we are using stored credentials but the KDC has been unavailable
945 * We can't ask to keep spamming stderr but not syslog, so we warn
948 if (exp_delay
== 1) {
949 krb5_warnx(ctx
->context
, N_("NOTICE: Could not renew/refresh "
952 if (exp_delay
< 7200)
953 exp_delay
+= exp_delay
/ 2 + 1;
958 return expire
/ 2 + 1;
962 set_princ_realm(krb5_context context
,
963 krb5_principal principal
,
968 if ((ret
= krb5_principal_set_realm(context
, principal
, realm
)) != 0)
969 krb5_err(context
, 1, ret
, "krb5_principal_set_realm");
973 parse_name_realm(krb5_context context
,
977 krb5_principal
*princ
)
982 flags
|= KRB5_PRINCIPAL_PARSE_NO_DEF_REALM
;
983 if ((ret
= krb5_parse_name_flags(context
, name
, flags
, princ
)) != 0)
984 krb5_err(context
, 1, ret
, "krb5_parse_name_flags");
985 if (realm
&& krb5_principal_get_realm(context
, *princ
) == NULL
)
986 set_princ_realm(context
, *princ
, realm
);
990 get_default_realm(krb5_context context
)
995 if ((ret
= krb5_get_default_realm(context
, &realm
)) != 0)
996 krb5_err(context
, 1, ret
, "krb5_get_default_realm");
1001 get_default_principal(krb5_context context
, krb5_principal
*princ
)
1003 krb5_error_code ret
;
1005 if ((ret
= krb5_get_default_principal(context
, princ
)) != 0)
1006 krb5_err(context
, 1, ret
, "krb5_get_default_principal");
1010 get_user_realm(krb5_context context
)
1012 krb5_error_code ret
;
1013 char *user_realm
= NULL
;
1016 * If memory allocation fails, we don't try to use the wrong realm,
1017 * that will trigger misleading error messages complicate support.
1019 krb5_appdefault_string(context
, "kinit", NULL
, "user_realm", "",
1021 if (user_realm
== NULL
) {
1022 ret
= krb5_enomem(context
);
1023 krb5_err(context
, 1, ret
, "krb5_appdefault_string");
1026 if (*user_realm
== 0) {
1035 get_princ(krb5_context context
, krb5_principal
*principal
, const char *name
)
1037 krb5_error_code ret
;
1045 /* If credential cache provides a client principal, use that. */
1046 if (krb5_cc_default(context
, &ccache
) == 0) {
1047 ret
= krb5_cc_get_principal(context
, ccache
, principal
);
1048 krb5_cc_close(context
, ccache
);
1054 user_realm
= get_user_realm(context
);
1057 if (canonicalize_flag
|| enterprise_flag
)
1058 parseflags
|= KRB5_PRINCIPAL_PARSE_ENTERPRISE
;
1060 parse_name_realm(context
, name
, parseflags
, user_realm
, &tmp
);
1062 if (user_realm
&& krb5_principal_get_num_comp(context
, tmp
) > 1) {
1063 /* Principal is instance qualified, reparse with default realm. */
1064 krb5_free_principal(context
, tmp
);
1065 parse_name_realm(context
, name
, parseflags
, NULL
, principal
);
1070 get_default_principal(context
, principal
);
1072 set_princ_realm(context
, *principal
, user_realm
);
1080 get_princ_kt(krb5_context context
,
1081 krb5_principal
*principal
,
1084 krb5_error_code ret
;
1087 krb5_kt_cursor cursor
;
1088 krb5_keytab_entry entry
;
1093 * If the credential cache exists and specifies a client principal,
1096 if (krb5_cc_default(context
, &ccache
) == 0) {
1097 ret
= krb5_cc_get_principal(context
, ccache
, principal
);
1098 krb5_cc_close(context
, ccache
);
1105 /* If the principal specifies an explicit realm, just use that. */
1106 int parseflags
= KRB5_PRINCIPAL_PARSE_NO_DEF_REALM
;
1108 parse_name_realm(context
, name
, parseflags
, NULL
, &tmp
);
1109 if (krb5_principal_get_realm(context
, tmp
) != NULL
) {
1114 /* Otherwise, search keytab for bare name of the default principal. */
1115 get_default_principal(context
, &tmp
);
1116 set_princ_realm(context
, tmp
, NULL
);
1119 def_realm
= get_default_realm(context
);
1121 ret
= krb5_kt_start_seq_get(context
, kt
, &cursor
);
1123 krb5_err(context
, 1, ret
, "krb5_kt_start_seq_get");
1126 krb5_kt_next_entry(context
, kt
, &entry
, &cursor
) == 0) {
1129 if (!krb5_principal_compare_any_realm(context
, tmp
, entry
.principal
))
1132 krb5_principal_compare(context
, *principal
, entry
.principal
))
1134 /* The default realm takes precedence */
1135 realm
= krb5_principal_get_realm(context
, entry
.principal
);
1136 if (*principal
&& strcmp(def_realm
, realm
) == 0) {
1137 krb5_free_principal(context
, *principal
);
1138 ret
= krb5_copy_principal(context
, entry
.principal
, principal
);
1142 ret
= krb5_copy_principal(context
, entry
.principal
, principal
);
1144 if (ret
!= 0 || (ret
= krb5_kt_end_seq_get(context
, kt
, &cursor
)) != 0)
1145 krb5_err(context
, 1, ret
, "get_princ_kt");
1148 parse_name_realm(context
, name
, 0, NULL
, principal
);
1150 krb5_err(context
, 1, KRB5_CC_NOTFOUND
, "get_princ_kt");
1153 krb5_free_principal(context
, tmp
);
1157 static krb5_error_code
1158 get_switched_ccache(krb5_context context
,
1160 krb5_principal principal
,
1161 krb5_ccache
*ccache
)
1163 krb5_error_code ret
;
1166 if (strcmp(type
, "API") == 0) {
1168 * Windows stores the default ccache name in the
1169 * registry which is shared across multiple logon
1170 * sessions for the same user. The API credential
1171 * cache provides a unique name space per logon
1172 * session. Therefore there is no need to generate
1173 * a unique ccache name. Instead use the principal
1174 * name. This provides a friendlier user experience.
1176 char * unparsed_name
;
1179 ret
= krb5_unparse_name(context
, principal
,
1182 krb5_err(context
, 1, ret
,
1183 N_("unparsing principal name", ""));
1185 ret
= asprintf(&cred_cache
, "API:%s", unparsed_name
);
1186 krb5_free_unparsed_name(context
, unparsed_name
);
1187 if (ret
== -1 || cred_cache
== NULL
)
1188 krb5_err(context
, 1, ret
,
1189 N_("building credential cache name", ""));
1191 ret
= krb5_cc_resolve(context
, cred_cache
, ccache
);
1193 } else if (strcmp(type
, "MSLSA") == 0) {
1195 * The Windows MSLSA cache when it is writeable
1196 * stores tickets for multiple client principals
1197 * in a single credential cache.
1199 ret
= krb5_cc_resolve(context
, "MSLSA:", ccache
);
1201 ret
= krb5_cc_new_unique(context
, type
, NULL
, ccache
);
1204 ret
= krb5_cc_new_unique(context
, type
, NULL
, ccache
);
1211 main(int argc
, char **argv
)
1213 krb5_error_code ret
;
1214 krb5_context context
;
1216 krb5_principal principal
= NULL
;
1218 krb5_deltat ticket_life
= 0;
1219 #ifdef HAVE_SIGACTION
1220 struct sigaction sa
;
1223 setprogname(argv
[0]);
1225 setlocale(LC_ALL
, "");
1226 bindtextdomain("heimdal_kuser", HEIMDAL_LOCALEDIR
);
1227 textdomain("heimdal_kuser");
1229 ret
= krb5_init_context(&context
);
1230 if (ret
== KRB5_CONFIG_BADFORMAT
)
1231 errx(1, "krb5_init_context failed to parse configuration file");
1233 errx(1, "krb5_init_context failed: %d", ret
);
1235 if (getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
1242 print_version(NULL
);
1250 * Open the keytab now, we use the keytab to determine the principal's
1251 * realm when the requested principal has no realm.
1253 if (use_keytab
|| keytab_str
) {
1255 ret
= krb5_kt_resolve(context
, keytab_str
, &kt
);
1257 ret
= krb5_kt_default(context
, &kt
);
1259 krb5_err(context
, 1, ret
, "resolving keytab");
1262 if (pk_enterprise_flag
) {
1263 ret
= krb5_pk_enterprise_cert(context
, pk_user_id
,
1264 argv
[0], &principal
,
1267 krb5_err(context
, 1, ret
, "krb5_pk_enterprise_certs");
1271 } else if (anonymous_flag
) {
1273 ret
= krb5_make_principal(context
, &principal
, argv
[0],
1274 KRB5_WELLKNOWN_NAME
, KRB5_ANON_NAME
,
1277 krb5_err(context
, 1, ret
, "krb5_make_principal");
1278 krb5_principal_set_type(context
, principal
, KRB5_NT_WELLKNOWN
);
1280 } else if (use_keytab
|| keytab_str
) {
1281 get_princ_kt(context
, &principal
, argv
[0]);
1283 get_princ(context
, &principal
, argv
[0]);
1287 krb5_set_fcache_version(context
, fcache_version
);
1289 if (renewable_flag
== -1)
1290 /* this seems somewhat pointless, but whatever */
1291 krb5_appdefault_boolean(context
, "kinit",
1292 krb5_principal_get_realm(context
, principal
),
1293 "renewable", FALSE
, &renewable_flag
);
1294 if (do_afslog
== -1)
1295 krb5_appdefault_boolean(context
, "kinit",
1296 krb5_principal_get_realm(context
, principal
),
1297 "afslog", TRUE
, &do_afslog
);
1300 ret
= krb5_cc_resolve(context
, cred_cache
, &ccache
);
1304 ret
= krb5_cc_new_unique(context
, NULL
, NULL
, &ccache
);
1306 krb5_err(context
, 1, ret
, "creating cred cache");
1307 snprintf(s
, sizeof(s
), "%s:%s",
1308 krb5_cc_get_type(context
, ccache
),
1309 krb5_cc_get_name(context
, ccache
));
1310 setenv("KRB5CCNAME", s
, 1);
1312 ret
= krb5_cc_cache_match(context
, principal
, &ccache
);
1315 ret
= krb5_cc_default(context
, &ccache
);
1317 krb5_err(context
, 1, ret
,
1318 N_("resolving credentials cache", ""));
1321 * Check if the type support switching, and we do,
1322 * then do that instead over overwriting the current
1323 * default credential
1325 type
= krb5_cc_get_type(context
, ccache
);
1326 if (krb5_cc_support_switch(context
, type
)) {
1327 krb5_cc_close(context
, ccache
);
1328 ret
= get_switched_ccache(context
, type
, principal
,
1335 krb5_err(context
, 1, ret
, N_("resolving credentials cache", ""));
1338 if (argc
> 1 && k_hasafs())
1343 int tmp
= parse_time(lifetime
, "s");
1345 errx(1, N_("unparsable time: %s", ""), lifetime
);
1350 if (addrs_flag
== 0 && extra_addresses
.num_strings
> 0)
1351 krb5_errx(context
, 1,
1352 N_("specifying both extra addresses and "
1353 "no addresses makes no sense", ""));
1356 krb5_addresses addresses
;
1357 memset(&addresses
, 0, sizeof(addresses
));
1358 for(i
= 0; i
< extra_addresses
.num_strings
; i
++) {
1359 ret
= krb5_parse_address(context
, extra_addresses
.strings
[i
],
1362 krb5_add_extra_addresses(context
, &addresses
);
1363 krb5_free_addresses(context
, &addresses
);
1366 free_getarg_strings(&extra_addresses
);
1369 if (renew_flag
|| validate_flag
) {
1370 ret
= renew_validate(context
, renew_flag
, validate_flag
,
1371 ccache
, server_str
, ticket_life
);
1374 if (ret
== 0 && server_str
== NULL
&& do_afslog
&& k_hasafs())
1375 krb5_afslog(context
, ccache
, NULL
, NULL
);
1381 ret
= get_new_tickets(context
, principal
, ccache
, ticket_life
, 1);
1386 if (ret
== 0 && server_str
== NULL
&& do_afslog
&& k_hasafs())
1387 krb5_afslog(context
, ccache
, NULL
, NULL
);
1391 struct renew_ctx ctx
;
1394 timeout
= ticket_lifetime(context
, ccache
, principal
,
1395 server_str
, NULL
) / 2;
1397 ctx
.context
= context
;
1398 ctx
.ccache
= ccache
;
1399 ctx
.principal
= principal
;
1400 ctx
.ticket_life
= ticket_life
;
1401 ctx
.timeout
= timeout
;
1403 #ifdef HAVE_SIGACTION
1404 memset(&sa
, 0, sizeof(sa
));
1405 sigemptyset(&sa
.sa_mask
);
1406 sa
.sa_handler
= handle_siginfo
;
1408 sigaction(SIGINFO
, &sa
, NULL
);
1411 ret
= simple_execvp_timed(argv
[1], argv
+1,
1412 renew_func
, &ctx
, timeout
);
1413 #define EX_NOEXEC 126
1414 #define EX_NOTFOUND 127
1415 if (ret
== EX_NOEXEC
)
1416 krb5_warnx(context
, N_("permission denied: %s", ""), argv
[1]);
1417 else if (ret
== EX_NOTFOUND
)
1418 krb5_warnx(context
, N_("command not found: %s", ""), argv
[1]);
1420 krb5_cc_destroy(context
, ccache
);
1426 krb5_cc_close(context
, ccache
);
1429 krb5_free_principal(context
, principal
);
1431 krb5_kt_close(context
, kt
);
1432 krb5_free_context(context
);