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>
46 int forwardable_flag
= -1;
47 int proxiable_flag
= -1;
48 int renewable_flag
= -1;
51 int validate_flag
= 0;
55 struct getarg_strings extra_addresses
;
56 int anonymous_flag
= 0;
57 char *lifetime
= NULL
;
58 char *renew_life
= NULL
;
59 char *server_str
= NULL
;
60 char *cred_cache
= NULL
;
61 char *start_str
= NULL
;
62 static int switch_cache_flags
= 1;
63 struct getarg_strings etype_str
;
65 char *keytab_str
= NULL
;
66 static krb5_keytab kt
= NULL
;
69 char *password_file
= NULL
;
70 char *pk_user_id
= NULL
;
71 int pk_enterprise_flag
= 0;
72 struct hx509_certs_data
*ent_user_id
= NULL
;
73 char *pk_x509_anchors
= NULL
;
74 int pk_use_enckey
= 0;
75 static int canonicalize_flag
= 0;
76 static int enterprise_flag
= 0;
77 static int ok_as_delegate_flag
= 0;
78 static char *fast_armor_cache_string
= NULL
;
79 static int use_referrals_flag
= 0;
80 static int windows_flag
= 0;
82 static char *ntlm_domain
;
86 static struct getargs args
[] = {
100 { "afslog", 0 , arg_flag
, &do_afslog
,
101 NP_("obtain afs tokens", ""), NULL
},
103 { "cache", 'c', arg_string
, &cred_cache
,
104 NP_("credentials cache", ""), "cachename" },
106 { "forwardable", 'F', arg_negative_flag
, &forwardable_flag
,
107 NP_("get tickets not forwardable", ""), NULL
},
109 { NULL
, 'f', arg_flag
, &forwardable_flag
,
110 NP_("get forwardable tickets", ""), NULL
},
112 { "keytab", 't', arg_string
, &keytab_str
,
113 NP_("keytab to use", ""), "keytabname" },
115 { "lifetime", 'l', arg_string
, &lifetime
,
116 NP_("lifetime of tickets", ""), "time" },
118 { "proxiable", 'p', arg_flag
, &proxiable_flag
,
119 NP_("get proxiable tickets", ""), NULL
},
121 { "renew", 'R', arg_flag
, &renew_flag
,
122 NP_("renew TGT", ""), NULL
},
124 { "renewable", 0, arg_flag
, &renewable_flag
,
125 NP_("get renewable tickets", ""), NULL
},
127 { "renewable-life", 'r', arg_string
, &renew_life
,
128 NP_("renewable lifetime of tickets", ""), "time" },
130 { "server", 'S', arg_string
, &server_str
,
131 NP_("server to get ticket for", ""), "principal" },
133 { "start-time", 's', arg_string
, &start_str
,
134 NP_("when ticket gets valid", ""), "time" },
136 { "use-keytab", 'k', arg_flag
, &use_keytab
,
137 NP_("get key from keytab", ""), NULL
},
139 { "validate", 'v', arg_flag
, &validate_flag
,
140 NP_("validate TGT", ""), NULL
},
142 { "enctypes", 'e', arg_strings
, &etype_str
,
143 NP_("encryption types to use", ""), "enctypes" },
145 { "fcache-version", 0, arg_integer
, &fcache_version
,
146 NP_("file cache version to create", ""), NULL
},
148 { "addresses", 'A', arg_negative_flag
, &addrs_flag
,
149 NP_("request a ticket with no addresses", ""), NULL
},
151 { "extra-addresses",'a', arg_strings
, &extra_addresses
,
152 NP_("include these extra addresses", ""), "addresses" },
154 { "anonymous", 0, arg_flag
, &anonymous_flag
,
155 NP_("request an anonymous ticket", ""), NULL
},
157 { "request-pac", 0, arg_flag
, &pac_flag
,
158 NP_("request a Windows PAC", ""), NULL
},
160 { "password-file", 0, arg_string
, &password_file
,
161 NP_("read the password from a file", ""), NULL
},
163 { "canonicalize",0, arg_flag
, &canonicalize_flag
,
164 NP_("canonicalize client principal", ""), NULL
},
166 { "enterprise",0, arg_flag
, &enterprise_flag
,
167 NP_("parse principal as a KRB5-NT-ENTERPRISE name", ""), NULL
},
169 { "pk-enterprise", 0, arg_flag
, &pk_enterprise_flag
,
170 NP_("use enterprise name from certificate", ""), NULL
},
172 { "pk-user", 'C', arg_string
, &pk_user_id
,
173 NP_("principal's public/private/certificate identifier", ""), "id" },
175 { "x509-anchors", 'D', arg_string
, &pk_x509_anchors
,
176 NP_("directory with CA certificates", ""), "directory" },
178 { "pk-use-enckey", 0, arg_flag
, &pk_use_enckey
,
179 NP_("Use RSA encrypted reply (instead of DH)", ""), NULL
},
182 { "ntlm-domain", 0, arg_string
, &ntlm_domain
,
183 NP_("NTLM domain", ""), "domain" },
186 { "change-default", 0, arg_negative_flag
, &switch_cache_flags
,
187 NP_("switch the default cache to the new credentials cache", ""), NULL
},
189 { "ok-as-delegate", 0, arg_flag
, &ok_as_delegate_flag
,
190 NP_("honor ok-as-delegate on tickets", ""), NULL
},
192 { "fast-armor-cache", 0, arg_string
, &fast_armor_cache_string
,
193 NP_("use this credential cache as FAST armor cache", ""), "cache" },
195 { "use-referrals", 0, arg_flag
, &use_referrals_flag
,
196 NP_("only use referrals, no dns canalisation", ""), NULL
},
198 { "windows", 0, arg_flag
, &windows_flag
,
199 NP_("get windows behavior", ""), NULL
},
201 { "version", 0, arg_flag
, &version_flag
, NULL
, NULL
},
202 { "help", 0, arg_flag
, &help_flag
, NULL
, NULL
}
208 arg_printusage_i18n(args
, sizeof(args
)/sizeof(*args
), N_("Usage: ", ""),
209 NULL
, "[principal [command]]", getarg_i18n
);
213 static krb5_error_code
214 get_server(krb5_context context
,
215 krb5_principal client
,
217 krb5_principal
*princ
)
219 krb5_const_realm realm
;
221 return krb5_parse_name(context
, server
, princ
);
223 realm
= krb5_principal_get_realm(context
, client
);
224 return krb5_make_principal(context
, princ
, realm
,
225 KRB5_TGS_NAME
, realm
, NULL
);
228 static krb5_error_code
229 copy_configs(krb5_context context
,
232 krb5_principal start_ticket_server
)
235 const char *cfg_names
[] = {"realm-config", "FriendlyName", NULL
};
236 const char *cfg_names_w_pname
[] = {"fast_avail", NULL
};
240 for (i
= 0; cfg_names
[i
]; i
++) {
241 ret
= krb5_cc_get_config(context
, src
, NULL
, cfg_names
[i
], &cfg_data
);
242 if (ret
== KRB5_CC_NOTFOUND
|| ret
== KRB5_CC_END
) {
245 krb5_warn(context
, ret
, "krb5_cc_get_config");
248 ret
= krb5_cc_set_config(context
, dst
, NULL
, cfg_names
[i
], &cfg_data
);
250 krb5_warn(context
, ret
, "krb5_cc_set_config");
252 for (i
= 0; start_ticket_server
&& cfg_names_w_pname
[i
]; i
++) {
253 ret
= krb5_cc_get_config(context
, src
, start_ticket_server
,
254 cfg_names_w_pname
[i
], &cfg_data
);
255 if (ret
== KRB5_CC_NOTFOUND
|| ret
== KRB5_CC_END
) {
258 krb5_warn(context
, ret
, "krb5_cc_get_config");
261 ret
= krb5_cc_set_config(context
, dst
, start_ticket_server
,
262 cfg_names_w_pname
[i
], &cfg_data
);
263 if (ret
&& ret
!= KRB5_CC_NOTFOUND
)
264 krb5_warn(context
, ret
, "krb5_cc_set_config");
267 * We don't copy cc configs for any other principals though (mostly
268 * those are per-target time offsets and the like, so it's bad to
269 * lose them, but hardly the end of the world, and as they may not
270 * expire anyways, it's good to let them go).
276 renew_validate(krb5_context context
,
284 krb5_ccache tempccache
= NULL
;
285 krb5_creds in
, *out
= NULL
;
286 krb5_kdc_flags flags
;
288 memset(&in
, 0, sizeof(in
));
290 ret
= krb5_cc_get_principal(context
, cache
, &in
.client
);
292 krb5_warn(context
, ret
, "krb5_cc_get_principal");
295 ret
= get_server(context
, in
.client
, server
, &in
.server
);
297 krb5_warn(context
, ret
, "get_server");
303 * no need to check the error here, it's only to be
304 * friendly to the user
306 krb5_get_credentials(context
, KRB5_GC_CACHED
, cache
, &in
, &out
);
310 flags
.b
.renewable
= flags
.b
.renew
= renew
;
311 flags
.b
.validate
= validate
;
313 if (forwardable_flag
!= -1)
314 flags
.b
.forwardable
= forwardable_flag
;
316 flags
.b
.forwardable
= out
->flags
.b
.forwardable
;
318 if (proxiable_flag
!= -1)
319 flags
.b
.proxiable
= proxiable_flag
;
321 flags
.b
.proxiable
= out
->flags
.b
.proxiable
;
324 flags
.b
.request_anonymous
= anonymous_flag
;
326 in
.times
.endtime
= time(NULL
) + life
;
329 krb5_free_creds(context
, out
);
334 ret
= krb5_get_kdc_cred(context
,
342 krb5_warn(context
, ret
, "krb5_get_kdc_cred");
346 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, cache
),
349 krb5_warn(context
, ret
, "krb5_cc_new_unique");
353 ret
= krb5_cc_initialize(context
, tempccache
, in
.client
);
355 krb5_warn(context
, ret
, "krb5_cc_initialize");
359 ret
= krb5_cc_store_cred(context
, tempccache
, out
);
361 krb5_warn(context
, ret
, "krb5_cc_store_cred");
366 * We want to preserve cc configs as some are security-relevant, and
367 * anyways it's the friendly thing to do.
369 ret
= copy_configs(context
, tempccache
, cache
, out
->server
);
373 ret
= krb5_cc_move(context
, tempccache
, cache
);
375 krb5_warn(context
, ret
, "krb5_cc_move");
381 /* only do this if it's a general renew-my-tgt request */
382 if (server
== NULL
&& do_afslog
&& k_hasafs())
383 krb5_afslog(context
, cache
, NULL
, NULL
);
388 krb5_cc_close(context
, tempccache
);
390 krb5_free_creds(context
, out
);
391 krb5_free_cred_contents(context
, &in
);
397 static krb5_error_code
398 store_ntlmkey(krb5_context context
, krb5_ccache id
,
399 const char *domain
, struct ntlm_buf
*buf
)
406 aret
= asprintf(&name
, "ntlm-key-%s", domain
);
407 if (aret
== -1 || name
== NULL
) {
408 krb5_clear_error_message(context
);
412 data
.length
= buf
->length
;
413 data
.data
= buf
->data
;
415 ret
= krb5_cc_set_config(context
, id
, NULL
, name
, &data
);
421 static krb5_error_code
422 get_new_tickets(krb5_context context
,
423 krb5_principal principal
,
425 krb5_deltat ticket_life
,
431 krb5_deltat start_time
= 0;
432 krb5_deltat renew
= 0;
433 const char *renewstr
= NULL
;
434 krb5_enctype
*enctype
= NULL
;
435 krb5_ccache tempccache
= NULL
;
436 krb5_init_creds_context ctx
= NULL
;
437 krb5_get_init_creds_opt
*opt
= NULL
;
438 krb5_prompter_fct prompter
= krb5_prompter_posix
;
440 struct ntlm_buf ntlmkey
;
441 memset(&ntlmkey
, 0, sizeof(ntlmkey
));
451 if (strcasecmp("STDIN", password_file
) == 0)
454 f
= fopen(password_file
, "r");
456 krb5_warnx(context
, "Failed to open the password file %s",
461 if (fgets(passwd
, sizeof(passwd
), f
) == NULL
) {
462 krb5_warnx(context
, N_("Failed to read password from file %s", ""),
465 return EINVAL
; /* XXX Need a better error */
469 passwd
[strcspn(passwd
, "\n")] = '\0';
473 if (passwd
[0] == '\0') {
480 realm
= krb5_principal_get_realm(context
, principal
);
482 ret
= krb5_unparse_name_flags(context
, principal
,
483 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &name
);
487 osret
= SecKeychainFindGenericPassword(NULL
, strlen(realm
), realm
,
489 &length
, &buffer
, NULL
);
491 if (osret
== noErr
&& length
< sizeof(passwd
) - 1) {
492 memcpy(passwd
, buffer
, length
);
493 passwd
[length
] = '\0';
500 memset(&cred
, 0, sizeof(cred
));
502 ret
= krb5_get_init_creds_opt_alloc(context
, &opt
);
504 krb5_warn(context
, ret
, "krb5_get_init_creds_opt_alloc");
508 krb5_get_init_creds_opt_set_default_flags(context
, "kinit",
509 krb5_principal_get_realm(context
, principal
), opt
);
511 if (forwardable_flag
!= -1)
512 krb5_get_init_creds_opt_set_forwardable(opt
, forwardable_flag
);
513 if (proxiable_flag
!= -1)
514 krb5_get_init_creds_opt_set_proxiable(opt
, proxiable_flag
);
516 krb5_get_init_creds_opt_set_anonymous(opt
, anonymous_flag
);
518 krb5_get_init_creds_opt_set_pac_request(context
, opt
,
519 pac_flag
? TRUE
: FALSE
);
520 if (canonicalize_flag
)
521 krb5_get_init_creds_opt_set_canonicalize(context
, opt
, TRUE
);
522 if (pk_enterprise_flag
|| enterprise_flag
|| canonicalize_flag
|| windows_flag
)
523 krb5_get_init_creds_opt_set_win2k(context
, opt
, TRUE
);
524 if (pk_user_id
|| ent_user_id
|| anonymous_flag
) {
525 ret
= krb5_get_init_creds_opt_set_pkinit(context
, opt
,
531 pk_use_enckey
? 2 : 0 |
532 anonymous_flag
? 4 : 0,
537 krb5_warn(context
, ret
, "krb5_get_init_creds_opt_set_pkinit");
541 krb5_get_init_creds_opt_set_pkinit_user_certs(context
, opt
, ent_user_id
);
544 if (addrs_flag
!= -1)
545 krb5_get_init_creds_opt_set_addressless(context
, opt
,
546 addrs_flag
? FALSE
: TRUE
);
548 if (renew_life
== NULL
&& renewable_flag
)
549 renewstr
= "1 month";
551 renewstr
= renew_life
;
553 renew
= parse_time(renewstr
, "s");
555 errx(1, "unparsable time: %s", renewstr
);
557 krb5_get_init_creds_opt_set_renew_life(opt
, renew
);
560 if (ticket_life
!= 0)
561 krb5_get_init_creds_opt_set_tkt_life(opt
, ticket_life
);
564 int tmp
= parse_time(start_str
, "s");
566 errx(1, N_("unparsable time: %s", ""), start_str
);
571 if (etype_str
.num_strings
) {
574 enctype
= malloc(etype_str
.num_strings
* sizeof(*enctype
));
576 errx(1, "out of memory");
577 for(i
= 0; i
< etype_str
.num_strings
; i
++) {
578 ret
= krb5_string_to_enctype(context
,
579 etype_str
.strings
[i
],
582 errx(1, "unrecognized enctype: %s", etype_str
.strings
[i
]);
584 krb5_get_init_creds_opt_set_etype_list(opt
, enctype
,
585 etype_str
.num_strings
);
588 ret
= krb5_init_creds_init(context
, principal
, prompter
, NULL
, start_time
, opt
, &ctx
);
590 krb5_warn(context
, ret
, "krb5_init_creds_init");
595 ret
= krb5_init_creds_set_service(context
, ctx
, server_str
);
597 krb5_warn(context
, ret
, "krb5_init_creds_set_service");
602 if (fast_armor_cache_string
) {
605 ret
= krb5_cc_resolve(context
, fast_armor_cache_string
, &fastid
);
607 krb5_warn(context
, ret
, "krb5_cc_resolve(FAST cache)");
611 ret
= krb5_init_creds_set_fast_ccache(context
, ctx
, fastid
);
613 krb5_warn(context
, ret
, "krb5_init_creds_set_fast_ccache");
618 if (use_keytab
|| keytab_str
) {
619 ret
= krb5_init_creds_set_keytab(context
, ctx
, kt
);
621 krb5_warn(context
, ret
, "krb5_init_creds_set_keytab");
624 } else if (pk_user_id
|| ent_user_id
|| anonymous_flag
) {
626 } else if (!interactive
&& passwd
[0] == '\0') {
627 static int already_warned
= 0;
630 krb5_warnx(context
, "Not interactive, failed to get "
632 krb5_get_init_creds_opt_free(context
, opt
);
637 if (passwd
[0] == '\0') {
641 ret
= krb5_unparse_name(context
, principal
, &p
);
643 aret
= asprintf(&prompt
, N_("%s's Password: ", ""), p
);
646 if (ret
|| aret
== -1)
647 errx(1, "failed to generate passwd prompt: not enough memory");
649 if (UI_UTIL_read_pw_string(passwd
, sizeof(passwd
)-1, prompt
, 0)){
650 memset(passwd
, 0, sizeof(passwd
));
651 errx(1, "failed to read password");
657 ret
= krb5_init_creds_set_password(context
, ctx
, passwd
);
659 krb5_warn(context
, ret
, "krb5_init_creds_set_password");
665 ret
= krb5_init_creds_get(context
, ctx
);
668 if (ntlm_domain
&& passwd
[0])
669 heim_ntlm_nt_key(passwd
, &ntlmkey
);
671 memset(passwd
, 0, sizeof(passwd
));
676 case KRB5_LIBOS_PWDINTR
: /* don't print anything if it was just C-c:ed */
678 case KRB5KRB_AP_ERR_BAD_INTEGRITY
:
679 case KRB5KRB_AP_ERR_MODIFIED
:
680 case KRB5KDC_ERR_PREAUTH_FAILED
:
681 case KRB5_GET_IN_TKT_LOOP
:
682 krb5_warnx(context
, N_("Password incorrect", ""));
684 case KRB5KRB_AP_ERR_V4_REPLY
:
685 krb5_warnx(context
, N_("Looks like a Kerberos 4 reply", ""));
688 krb5_warn(context
, ret
, "krb5_get_init_creds");
692 krb5_process_last_request(context
, opt
, ctx
);
694 ret
= krb5_init_creds_get_creds(context
, ctx
, &cred
);
696 krb5_warn(context
, ret
, "krb5_init_creds_get_creds");
700 if (ticket_life
!= 0) {
701 if (abs(cred
.times
.endtime
- cred
.times
.starttime
- ticket_life
) > 30) {
703 unparse_time_approx(cred
.times
.endtime
- cred
.times
.starttime
,
705 krb5_warnx(context
, N_("NOTICE: ticket lifetime is %s", ""), life
);
709 if (abs(cred
.times
.renew_till
- cred
.times
.starttime
- renew
) > 30) {
711 unparse_time_approx(cred
.times
.renew_till
- cred
.times
.starttime
,
714 N_("NOTICE: ticket renewable lifetime is %s", ""),
719 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, ccache
),
722 krb5_warn(context
, ret
, "krb5_cc_new_unique");
726 ret
= krb5_init_creds_store(context
, ctx
, tempccache
);
728 krb5_warn(context
, ret
, "krb5_init_creds_store");
732 krb5_init_creds_free(context
, ctx
);
735 ret
= krb5_cc_move(context
, tempccache
, ccache
);
737 krb5_warn(context
, ret
, "krb5_cc_move");
742 if (switch_cache_flags
)
743 krb5_cc_switch(context
, ccache
);
746 if (ntlm_domain
&& ntlmkey
.data
)
747 store_ntlmkey(context
, ccache
, ntlm_domain
, &ntlmkey
);
750 if (ok_as_delegate_flag
|| windows_flag
|| use_referrals_flag
) {
754 if (ok_as_delegate_flag
|| windows_flag
)
756 if (use_referrals_flag
|| windows_flag
)
762 krb5_cc_set_config(context
, ccache
, NULL
, "realm-config", &data
);
766 krb5_get_init_creds_opt_free(context
, opt
);
768 krb5_init_creds_free(context
, ctx
);
770 krb5_cc_close(context
, tempccache
);
779 ticket_lifetime(krb5_context context
, krb5_ccache cache
, krb5_principal client
,
780 const char *server
, time_t *renew
)
782 krb5_creds in_cred
, *cred
;
787 memset(&in_cred
, 0, sizeof(in_cred
));
789 ret
= krb5_cc_get_principal(context
, cache
, &in_cred
.client
);
791 krb5_warn(context
, ret
, "krb5_cc_get_principal");
794 ret
= get_server(context
, in_cred
.client
, server
, &in_cred
.server
);
796 krb5_free_principal(context
, in_cred
.client
);
797 krb5_warn(context
, ret
, "get_server");
801 ret
= krb5_get_credentials(context
, KRB5_GC_CACHED
,
802 cache
, &in_cred
, &cred
);
803 krb5_free_principal(context
, in_cred
.client
);
804 krb5_free_principal(context
, in_cred
.server
);
806 krb5_warn(context
, ret
, "krb5_get_credentials");
809 curtime
= time(NULL
);
810 timeout
= cred
->times
.endtime
- curtime
;
814 *renew
= cred
->times
.renew_till
- curtime
;
818 krb5_free_creds(context
, cred
);
823 krb5_context context
;
825 krb5_principal principal
;
826 krb5_deltat ticket_life
;
830 renew_func(void *ptr
)
832 struct renew_ctx
*ctx
= ptr
;
835 static time_t exp_delay
= 1;
837 expire
= ticket_lifetime(ctx
->context
, ctx
->ccache
, ctx
->principal
,
838 server_str
, &renew_expire
);
840 if (renew_expire
> expire
) {
841 renew_validate(ctx
->context
, 1, validate_flag
, ctx
->ccache
,
842 server_str
, ctx
->ticket_life
);
843 expire
= ticket_lifetime(ctx
->context
, ctx
->ccache
, ctx
->principal
,
844 server_str
, &renew_expire
);
847 if (expire
< ctx
->ticket_life
/ 2) {
848 get_new_tickets(ctx
->context
, ctx
->principal
,
849 ctx
->ccache
, ctx
->ticket_life
, 0);
850 expire
= ticket_lifetime(ctx
->context
, ctx
->ccache
, ctx
->principal
,
851 server_str
, &renew_expire
);
855 if (do_afslog
&& k_hasafs())
856 krb5_afslog(ctx
->context
, ctx
->ccache
, NULL
, NULL
);
860 * If our tickets have expired and we been able to either renew them
861 * or obtain new tickets, then we still call this function but we use
862 * an exponential backoff. This should take care of the case where
863 * we are using stored credentials but the KDC has been unavailable
868 if (exp_delay
< 7200)
874 return expire
/ 2 + 1;
878 set_princ_realm(krb5_context context
,
879 krb5_principal principal
,
884 if ((ret
= krb5_principal_set_realm(context
, principal
, realm
)) != 0)
885 krb5_err(context
, 1, ret
, "krb5_principal_set_realm");
889 parse_name_realm(krb5_context context
,
893 krb5_principal
*princ
)
898 flags
|= KRB5_PRINCIPAL_PARSE_NO_DEF_REALM
;
899 if ((ret
= krb5_parse_name_flags(context
, name
, flags
, princ
)) != 0)
900 krb5_err(context
, 1, ret
, "krb5_parse_name_flags");
901 if (realm
&& krb5_principal_get_realm(context
, *princ
) == NULL
)
902 set_princ_realm(context
, *princ
, realm
);
906 get_default_realm(krb5_context context
)
911 if ((ret
= krb5_get_default_realm(context
, &realm
)) != 0)
912 krb5_err(context
, 1, ret
, "krb5_get_default_realm");
917 get_default_principal(krb5_context context
, krb5_principal
*princ
)
921 if ((ret
= krb5_get_default_principal(context
, princ
)) != 0)
922 krb5_err(context
, 1, ret
, "krb5_get_default_principal");
926 get_user_realm(krb5_context context
)
929 char *user_realm
= NULL
;
932 * If memory allocation fails, we don't try to use the wrong realm,
933 * that will trigger misleading error messages complicate support.
935 krb5_appdefault_string(context
, "kinit", NULL
, "user_realm", "",
937 if (user_realm
== NULL
) {
938 ret
= krb5_enomem(context
);
939 krb5_err(context
, 1, ret
, "krb5_appdefault_string");
942 if (*user_realm
== 0) {
951 get_princ(krb5_context context
, krb5_principal
*principal
, const char *name
)
961 /* If credential cache provides a client principal, use that. */
962 if (krb5_cc_default(context
, &ccache
) == 0) {
963 ret
= krb5_cc_get_principal(context
, ccache
, principal
);
964 krb5_cc_close(context
, ccache
);
970 user_realm
= get_user_realm(context
);
973 if (canonicalize_flag
|| enterprise_flag
)
974 parseflags
|= KRB5_PRINCIPAL_PARSE_ENTERPRISE
;
976 parse_name_realm(context
, name
, parseflags
, user_realm
, &tmp
);
978 if (user_realm
&& krb5_principal_get_num_comp(context
, tmp
) > 1) {
979 /* Principal is instance qualified, reparse with default realm. */
980 krb5_free_principal(context
, tmp
);
981 parse_name_realm(context
, name
, parseflags
, NULL
, principal
);
986 get_default_principal(context
, principal
);
988 set_princ_realm(context
, *principal
, user_realm
);
996 get_princ_kt(krb5_context context
,
997 krb5_principal
*principal
,
1000 krb5_error_code ret
;
1003 krb5_kt_cursor cursor
;
1004 krb5_keytab_entry entry
;
1010 * If the credential cache exists and specifies a client principal,
1013 if (krb5_cc_default(context
, &ccache
) == 0) {
1014 ret
= krb5_cc_get_principal(context
, ccache
, principal
);
1015 krb5_cc_close(context
, ccache
);
1022 /* If the principal specifies an explicit realm, just use that. */
1023 parseflags
|= KRB5_PRINCIPAL_PARSE_NO_DEF_REALM
;
1024 parse_name_realm(context
, name
, parseflags
, NULL
, &tmp
);
1025 if (krb5_principal_get_realm(context
, tmp
) != NULL
) {
1030 /* Otherwise, search keytab for bare name of the default principal. */
1031 get_default_principal(context
, &tmp
);
1032 set_princ_realm(context
, tmp
, NULL
);
1035 def_realm
= get_default_realm(context
);
1037 ret
= krb5_kt_start_seq_get(context
, kt
, &cursor
);
1039 krb5_err(context
, 1, ret
, "krb5_kt_start_seq_get");
1042 krb5_kt_next_entry(context
, kt
, &entry
, &cursor
) == 0) {
1045 if (!krb5_principal_compare_any_realm(context
, tmp
, entry
.principal
))
1048 krb5_principal_compare(context
, *principal
, entry
.principal
))
1050 /* The default realm takes precedence */
1051 realm
= krb5_principal_get_realm(context
, entry
.principal
);
1052 if (*principal
&& strcmp(def_realm
, realm
) == 0) {
1053 krb5_free_principal(context
, *principal
);
1054 ret
= krb5_copy_principal(context
, entry
.principal
, principal
);
1058 ret
= krb5_copy_principal(context
, entry
.principal
, principal
);
1060 if (ret
!= 0 || (ret
= krb5_kt_end_seq_get(context
, kt
, &cursor
)) != 0)
1061 krb5_err(context
, 1, ret
, "get_princ_kt");
1063 parse_name_realm(context
, name
, parseflags
, NULL
, principal
);
1065 krb5_free_principal(context
, tmp
);
1070 main(int argc
, char **argv
)
1072 krb5_error_code ret
;
1073 krb5_context context
;
1075 krb5_principal principal
= NULL
;
1077 krb5_deltat ticket_life
= 0;
1079 setprogname(argv
[0]);
1081 setlocale(LC_ALL
, "");
1082 bindtextdomain("heimdal_kuser", HEIMDAL_LOCALEDIR
);
1083 textdomain("heimdal_kuser");
1085 ret
= krb5_init_context(&context
);
1086 if (ret
== KRB5_CONFIG_BADFORMAT
)
1087 errx(1, "krb5_init_context failed to parse configuration file");
1089 errx(1, "krb5_init_context failed: %d", ret
);
1091 if (getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
1098 print_version(NULL
);
1106 * Open the keytab now, we use the keytab to determine the principal's
1107 * realm when the requested principal has no realm.
1109 if (use_keytab
|| keytab_str
) {
1111 ret
= krb5_kt_resolve(context
, keytab_str
, &kt
);
1113 ret
= krb5_kt_default(context
, &kt
);
1115 krb5_err(context
, 1, ret
, "resolving keytab");
1118 if (pk_enterprise_flag
) {
1119 ret
= krb5_pk_enterprise_cert(context
, pk_user_id
,
1120 argv
[0], &principal
,
1123 krb5_err(context
, 1, ret
, "krb5_pk_enterprise_certs");
1127 } else if (anonymous_flag
) {
1129 ret
= krb5_make_principal(context
, &principal
, argv
[0],
1130 KRB5_WELLKNOWN_NAME
, KRB5_ANON_NAME
,
1133 krb5_err(context
, 1, ret
, "krb5_make_principal");
1134 krb5_principal_set_type(context
, principal
, KRB5_NT_WELLKNOWN
);
1136 } else if (use_keytab
|| keytab_str
) {
1137 get_princ_kt(context
, &principal
, argv
[0]);
1139 get_princ(context
, &principal
, argv
[0]);
1143 krb5_set_fcache_version(context
, fcache_version
);
1145 if (renewable_flag
== -1)
1146 /* this seems somewhat pointless, but whatever */
1147 krb5_appdefault_boolean(context
, "kinit",
1148 krb5_principal_get_realm(context
, principal
),
1149 "renewable", FALSE
, &renewable_flag
);
1150 if (do_afslog
== -1)
1151 krb5_appdefault_boolean(context
, "kinit",
1152 krb5_principal_get_realm(context
, principal
),
1153 "afslog", TRUE
, &do_afslog
);
1156 ret
= krb5_cc_resolve(context
, cred_cache
, &ccache
);
1160 ret
= krb5_cc_new_unique(context
, NULL
, NULL
, &ccache
);
1162 krb5_err(context
, 1, ret
, "creating cred cache");
1163 snprintf(s
, sizeof(s
), "%s:%s",
1164 krb5_cc_get_type(context
, ccache
),
1165 krb5_cc_get_name(context
, ccache
));
1166 setenv("KRB5CCNAME", s
, 1);
1168 ret
= krb5_cc_cache_match(context
, principal
, &ccache
);
1171 ret
= krb5_cc_default(context
, &ccache
);
1173 krb5_err(context
, 1, ret
,
1174 N_("resolving credentials cache", ""));
1177 * Check if the type support switching, and we do,
1178 * then do that instead over overwriting the current
1179 * default credential
1181 type
= krb5_cc_get_type(context
, ccache
);
1182 if (krb5_cc_support_switch(context
, type
)) {
1183 krb5_cc_close(context
, ccache
);
1184 ret
= krb5_cc_new_unique(context
, type
, NULL
, &ccache
);
1190 krb5_err(context
, 1, ret
, N_("resolving credentials cache", ""));
1193 if (argc
> 1 && k_hasafs())
1198 int tmp
= parse_time(lifetime
, "s");
1200 errx(1, N_("unparsable time: %s", ""), lifetime
);
1205 if (addrs_flag
== 0 && extra_addresses
.num_strings
> 0)
1206 krb5_errx(context
, 1,
1207 N_("specifying both extra addresses and "
1208 "no addresses makes no sense", ""));
1211 krb5_addresses addresses
;
1212 memset(&addresses
, 0, sizeof(addresses
));
1213 for(i
= 0; i
< extra_addresses
.num_strings
; i
++) {
1214 ret
= krb5_parse_address(context
, extra_addresses
.strings
[i
],
1217 krb5_add_extra_addresses(context
, &addresses
);
1218 krb5_free_addresses(context
, &addresses
);
1221 free_getarg_strings(&extra_addresses
);
1224 if (renew_flag
|| validate_flag
) {
1225 ret
= renew_validate(context
, renew_flag
, validate_flag
,
1226 ccache
, server_str
, ticket_life
);
1230 ret
= get_new_tickets(context
, principal
, ccache
, ticket_life
, 1);
1235 if (do_afslog
&& k_hasafs())
1236 krb5_afslog(context
, ccache
, NULL
, NULL
);
1239 struct renew_ctx ctx
;
1242 timeout
= ticket_lifetime(context
, ccache
, principal
,
1243 server_str
, NULL
) / 2;
1245 ctx
.context
= context
;
1246 ctx
.ccache
= ccache
;
1247 ctx
.principal
= principal
;
1248 ctx
.ticket_life
= ticket_life
;
1250 ret
= simple_execvp_timed(argv
[1], argv
+1,
1251 renew_func
, &ctx
, timeout
);
1252 #define EX_NOEXEC 126
1253 #define EX_NOTFOUND 127
1254 if (ret
== EX_NOEXEC
)
1255 krb5_warnx(context
, N_("permission denied: %s", ""), argv
[1]);
1256 else if (ret
== EX_NOTFOUND
)
1257 krb5_warnx(context
, N_("command not found: %s", ""), argv
[1]);
1259 krb5_cc_destroy(context
, ccache
);
1265 krb5_cc_close(context
, ccache
);
1268 krb5_free_principal(context
, principal
);
1270 krb5_kt_close(context
, kt
);
1271 krb5_free_context(context
);