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 struct getarg_strings etype_str
;
70 char *keytab_str
= NULL
;
71 static krb5_keytab kt
= NULL
;
74 char *password_file
= NULL
;
75 char *pk_user_id
= NULL
;
76 int pk_enterprise_flag
= 0;
77 struct hx509_certs_data
*ent_user_id
= NULL
;
78 char *pk_x509_anchors
= NULL
;
79 int pk_use_enckey
= 0;
80 static int canonicalize_flag
= 0;
81 static int enterprise_flag
= 0;
82 static int ok_as_delegate_flag
= 0;
83 static char *fast_armor_cache_string
= NULL
;
84 static int use_referrals_flag
= 0;
85 static int windows_flag
= 0;
87 static char *ntlm_domain
;
91 static struct getargs args
[] = {
105 { "afslog", 0 , arg_flag
, &do_afslog
,
106 NP_("obtain afs tokens", ""), NULL
},
108 { "cache", 'c', arg_string
, &cred_cache
,
109 NP_("credentials cache", ""), "cachename" },
111 { "forwardable", 'F', arg_negative_flag
, &forwardable_flag
,
112 NP_("get tickets not forwardable", ""), NULL
},
114 { NULL
, 'f', arg_flag
, &forwardable_flag
,
115 NP_("get forwardable tickets", ""), NULL
},
117 { "keytab", 't', arg_string
, &keytab_str
,
118 NP_("keytab to use", ""), "keytabname" },
120 { "lifetime", 'l', arg_string
, &lifetime
,
121 NP_("lifetime of tickets", ""), "time" },
123 { "proxiable", 'p', arg_flag
, &proxiable_flag
,
124 NP_("get proxiable tickets", ""), NULL
},
126 { "renew", 'R', arg_flag
, &renew_flag
,
127 NP_("renew TGT", ""), NULL
},
129 { "renewable", 0, arg_flag
, &renewable_flag
,
130 NP_("get renewable tickets", ""), NULL
},
132 { "renewable-life", 'r', arg_string
, &renew_life
,
133 NP_("renewable lifetime of tickets", ""), "time" },
135 { "server", 'S', arg_string
, &server_str
,
136 NP_("server to get ticket for", ""), "principal" },
138 { "start-time", 's', arg_string
, &start_str
,
139 NP_("when ticket gets valid", ""), "time" },
141 { "use-keytab", 'k', arg_flag
, &use_keytab
,
142 NP_("get key from keytab", ""), NULL
},
144 { "validate", 'v', arg_flag
, &validate_flag
,
145 NP_("validate TGT", ""), NULL
},
147 { "enctypes", 'e', arg_strings
, &etype_str
,
148 NP_("encryption types to use", ""), "enctypes" },
150 { "fcache-version", 0, arg_integer
, &fcache_version
,
151 NP_("file cache version to create", ""), NULL
},
153 { "addresses", 'A', arg_negative_flag
, &addrs_flag
,
154 NP_("request a ticket with no addresses", ""), NULL
},
156 { "extra-addresses",'a', arg_strings
, &extra_addresses
,
157 NP_("include these extra addresses", ""), "addresses" },
159 { "anonymous", 'n', arg_flag
, &anonymous_flag
,
160 NP_("request an anonymous ticket", ""), NULL
},
162 { "request-pac", 0, arg_flag
, &pac_flag
,
163 NP_("request a Windows PAC", ""), NULL
},
165 { "password-file", 0, arg_string
, &password_file
,
166 NP_("read the password from a file", ""), NULL
},
168 { "canonicalize",0, arg_flag
, &canonicalize_flag
,
169 NP_("canonicalize client principal", ""), NULL
},
171 { "enterprise",0, arg_flag
, &enterprise_flag
,
172 NP_("parse principal as a KRB5-NT-ENTERPRISE name", ""), NULL
},
174 { "pk-enterprise", 0, arg_flag
, &pk_enterprise_flag
,
175 NP_("use enterprise name from certificate", ""), NULL
},
177 { "pk-user", 'C', arg_string
, &pk_user_id
,
178 NP_("principal's public/private/certificate identifier", ""), "id" },
180 { "x509-anchors", 'D', arg_string
, &pk_x509_anchors
,
181 NP_("directory with CA certificates", ""), "directory" },
183 { "pk-use-enckey", 0, arg_flag
, &pk_use_enckey
,
184 NP_("Use RSA encrypted reply (instead of DH)", ""), NULL
},
187 { "ntlm-domain", 0, arg_string
, &ntlm_domain
,
188 NP_("NTLM domain", ""), "domain" },
191 { "change-default", 0, arg_negative_flag
, &switch_cache_flags
,
192 NP_("switch the default cache to the new credentials cache", ""), NULL
},
194 { "ok-as-delegate", 0, arg_flag
, &ok_as_delegate_flag
,
195 NP_("honor ok-as-delegate on tickets", ""), NULL
},
197 { "fast-armor-cache", 0, arg_string
, &fast_armor_cache_string
,
198 NP_("use this credential cache as FAST armor cache", ""), "cache" },
200 { "use-referrals", 0, arg_flag
, &use_referrals_flag
,
201 NP_("only use referrals, no dns canalisation", ""), NULL
},
203 { "windows", 0, arg_flag
, &windows_flag
,
204 NP_("get windows behavior", ""), NULL
},
206 { "version", 0, arg_flag
, &version_flag
, NULL
, NULL
},
207 { "help", 0, arg_flag
, &help_flag
, NULL
, NULL
}
211 get_default_realm(krb5_context context
);
216 arg_printusage_i18n(args
, sizeof(args
)/sizeof(*args
), N_("Usage: ", ""),
217 NULL
, "[principal [command]]", getarg_i18n
);
221 static krb5_error_code
222 tgs_principal(krb5_context context
,
224 krb5_principal client
,
225 krb5_const_realm tgs_realm
,
226 krb5_principal
*out_princ
)
229 krb5_principal tgs_princ
;
234 ret
= krb5_make_principal(context
, &tgs_princ
, tgs_realm
,
235 KRB5_TGS_NAME
, tgs_realm
, NULL
);
240 * Don't fail-over to a different realm just because a TGT expired
242 options
= KRB5_GC_CACHED
| KRB5_GC_EXPIRED_OK
;
244 memset(&creds
, 0, sizeof(creds
));
245 creds
.client
= client
;
246 creds
.server
= tgs_princ
;
247 ret
= krb5_get_credentials(context
, options
, cache
, &creds
, &tick
);
249 krb5_free_creds(context
, tick
);
250 *out_princ
= tgs_princ
;
252 krb5_free_principal(context
, tgs_princ
);
260 * Try TGS specified with '-S',
261 * then TGS of client realm,
262 * then if fallback is FALSE: fail,
263 * otherwise try TGS of default realm,
264 * and finally first TGT in ccache.
266 static krb5_error_code
267 get_server(krb5_context context
,
269 krb5_principal client
,
271 krb5_boolean fallback
,
272 krb5_principal
*princ
)
274 krb5_error_code ret
= 0;
275 krb5_const_realm realm
;
276 krb5_realm def_realm
;
277 krb5_cc_cursor cursor
;
285 ret
= krb5_parse_name(context
, server
, &tgs_service
);
289 /* Try the client realm first */
290 realm
= krb5_principal_get_realm(context
, client
);
291 ret
= tgs_principal(context
, cache
, client
, realm
, &tgs_service
);
292 if (ret
== 0 || ret
!= KRB5_CC_NOTFOUND
)
298 /* Next try the default realm */
299 ret
= krb5_get_default_realm(context
, &def_realm
);
302 ret
= tgs_principal(context
, cache
, client
, def_realm
, &tgs_service
);
304 if (ret
== 0 || ret
!= KRB5_CC_NOTFOUND
)
307 /* Finally try the first TGT with instance == realm in the cache */
308 ret
= krb5_cc_start_seq_get(context
, cache
, &cursor
);
312 for (/**/; ret
== 0; krb5_free_cred_contents (context
, &creds
)) {
314 ret
= krb5_cc_next_cred(context
, cache
, &cursor
, &creds
);
317 if (creds
.server
->name
.name_string
.len
!= 2)
319 pcomp
= krb5_principal_get_comp_string(context
, creds
.server
, 0);
320 if (strcmp(pcomp
, KRB5_TGS_NAME
) != 0)
322 realm
= krb5_principal_get_realm(context
, creds
.server
);
323 pcomp
= krb5_principal_get_comp_string(context
, creds
.server
, 1);
324 if (strcmp(realm
, pcomp
) != 0)
326 ret
= krb5_copy_principal(context
, creds
.server
, &tgs_service
);
329 if (ret
== KRB5_CC_END
) {
330 ret
= KRB5_CC_NOTFOUND
;
331 krb5_set_error_message(context
, ret
,
332 N_("Credential cache contains no TGTs", ""));
334 krb5_cc_end_seq_get(context
, cache
, &cursor
);
338 ret
= krb5_copy_principal(context
, tgs_service
, princ
);
342 static krb5_error_code
343 copy_configs(krb5_context context
,
346 krb5_principal start_ticket_server
)
349 const char *cfg_names
[] = {"realm-config", "FriendlyName", "anon_pkinit_realm", NULL
};
350 const char *cfg_names_w_pname
[] = {"fast_avail", NULL
};
354 for (i
= 0; cfg_names
[i
]; i
++) {
355 ret
= krb5_cc_get_config(context
, src
, NULL
, cfg_names
[i
], &cfg_data
);
356 if (ret
== KRB5_CC_NOTFOUND
|| ret
== KRB5_CC_END
) {
359 krb5_warn(context
, ret
, "krb5_cc_get_config");
362 ret
= krb5_cc_set_config(context
, dst
, NULL
, cfg_names
[i
], &cfg_data
);
364 krb5_warn(context
, ret
, "krb5_cc_set_config");
366 for (i
= 0; start_ticket_server
&& cfg_names_w_pname
[i
]; i
++) {
367 ret
= krb5_cc_get_config(context
, src
, start_ticket_server
,
368 cfg_names_w_pname
[i
], &cfg_data
);
369 if (ret
== KRB5_CC_NOTFOUND
|| ret
== KRB5_CC_END
) {
372 krb5_warn(context
, ret
, "krb5_cc_get_config");
375 ret
= krb5_cc_set_config(context
, dst
, start_ticket_server
,
376 cfg_names_w_pname
[i
], &cfg_data
);
377 if (ret
&& ret
!= KRB5_CC_NOTFOUND
)
378 krb5_warn(context
, ret
, "krb5_cc_set_config");
381 * We don't copy cc configs for any other principals though (mostly
382 * those are per-target time offsets and the like, so it's bad to
383 * lose them, but hardly the end of the world, and as they may not
384 * expire anyways, it's good to let them go).
389 static krb5_error_code
390 get_anon_pkinit_tgs_name(krb5_context context
,
392 krb5_principal
*tgs_name
)
398 ret
= krb5_cc_get_config(context
, ccache
, NULL
, "anon_pkinit_realm", &data
);
400 realm
= strndup(data
.data
, data
.length
);
402 realm
= get_default_realm(context
);
404 krb5_data_free(&data
);
407 return krb5_enomem(context
);
409 ret
= krb5_make_principal(context
, tgs_name
, realm
,
410 KRB5_TGS_NAME
, realm
, NULL
);
417 static krb5_error_code
418 renew_validate(krb5_context context
,
422 krb5_const_principal principal
,
423 krb5_boolean cache_is_default_for
,
428 krb5_ccache tempccache
= NULL
;
429 krb5_ccache cache
= *cachep
;
430 krb5_creds in
, *out
= NULL
;
431 krb5_kdc_flags flags
;
433 memset(&in
, 0, sizeof(in
));
435 ret
= krb5_cc_get_principal(context
, cache
, &in
.client
);
436 if (ret
&& cache_is_default_for
&& principal
) {
437 krb5_error_code ret2
;
438 krb5_ccache def_ccache
= NULL
;
440 ret2
= krb5_cc_default(context
, &def_ccache
);
442 ret2
= krb5_cc_get_principal(context
, def_ccache
, &in
.client
);
444 krb5_principal_compare(context
, principal
, in
.client
)) {
445 krb5_cc_close(context
, *cachep
);
446 *cachep
= def_ccache
;
450 krb5_cc_close(context
, def_ccache
);
453 krb5_warn(context
, ret
, "krb5_cc_get_principal");
457 if (principal
&& !krb5_principal_compare(context
, principal
, in
.client
)) {
460 (void) krb5_cc_get_full_name(context
, cache
, &ccname
);
461 krb5_errx(context
, 1, "Credentials in cache %s do not match requested "
462 "principal", ccname
? ccname
: "requested");
466 if (server
== NULL
&&
467 krb5_principal_is_anonymous(context
, in
.client
,
468 KRB5_ANON_MATCH_UNAUTHENTICATED
))
469 ret
= get_anon_pkinit_tgs_name(context
, cache
, &in
.server
);
471 ret
= get_server(context
, cache
, in
.client
, server
, TRUE
, &in
.server
);
473 krb5_warn(context
, ret
, "get_server");
479 * no need to check the error here, it's only to be
480 * friendly to the user
482 krb5_get_credentials(context
, KRB5_GC_CACHED
, cache
, &in
, &out
);
486 flags
.b
.renewable
= flags
.b
.renew
= renew
;
487 flags
.b
.validate
= validate
;
489 if (forwardable_flag
!= -1)
490 flags
.b
.forwardable
= forwardable_flag
;
492 flags
.b
.forwardable
= out
->flags
.b
.forwardable
;
494 if (proxiable_flag
!= -1)
495 flags
.b
.proxiable
= proxiable_flag
;
497 flags
.b
.proxiable
= out
->flags
.b
.proxiable
;
500 flags
.b
.request_anonymous
= anonymous_flag
;
502 in
.times
.endtime
= time(NULL
) + life
;
505 krb5_free_creds(context
, out
);
510 ret
= krb5_get_kdc_cred(context
,
518 krb5_warn(context
, ret
, "krb5_get_kdc_cred");
522 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, cache
),
525 krb5_warn(context
, ret
, "krb5_cc_new_unique");
529 ret
= krb5_cc_initialize(context
, tempccache
, in
.client
);
531 krb5_warn(context
, ret
, "krb5_cc_initialize");
535 ret
= krb5_cc_store_cred(context
, tempccache
, out
);
537 krb5_warn(context
, ret
, "krb5_cc_store_cred");
542 * We want to preserve cc configs as some are security-relevant, and
543 * anyways it's the friendly thing to do.
545 ret
= copy_configs(context
, tempccache
, cache
, out
->server
);
549 ret
= krb5_cc_move(context
, tempccache
, cache
);
551 krb5_warn(context
, ret
, "krb5_cc_move");
558 krb5_cc_destroy(context
, tempccache
);
560 krb5_free_creds(context
, out
);
561 krb5_free_cred_contents(context
, &in
);
567 static krb5_error_code
568 store_ntlmkey(krb5_context context
, krb5_ccache id
,
569 const char *domain
, struct ntlm_buf
*buf
)
576 ret
= krb5_cc_get_config(context
, id
, NULL
, "default-ntlm-domain", &data
);
578 krb5_data_free(&data
);
580 data
.length
= strlen(domain
);
581 data
.data
= rk_UNCONST(domain
);
582 ret
= krb5_cc_set_config(context
, id
, NULL
, "default-ntlm-domain", &data
);
587 aret
= asprintf(&name
, "ntlm-key-%s", domain
);
588 if (aret
== -1 || name
== NULL
)
589 return krb5_enomem(context
);
591 data
.length
= buf
->length
;
592 data
.data
= buf
->data
;
594 ret
= krb5_cc_set_config(context
, id
, NULL
, name
, &data
);
600 static krb5_error_code
601 get_new_tickets(krb5_context context
,
602 krb5_principal principal
,
604 krb5_deltat ticket_life
,
606 int anonymous_pkinit
)
611 krb5_deltat start_time
= 0;
612 krb5_deltat renew
= 0;
613 const char *renewstr
= NULL
;
614 krb5_enctype
*enctype
= NULL
;
615 krb5_ccache tempccache
= NULL
;
616 krb5_init_creds_context ctx
= NULL
;
617 krb5_get_init_creds_opt
*opt
= NULL
;
618 krb5_prompter_fct prompter
= krb5_prompter_posix
;
620 struct ntlm_buf ntlmkey
;
621 memset(&ntlmkey
, 0, sizeof(ntlmkey
));
631 if (strcasecmp("STDIN", password_file
) == 0)
634 f
= fopen(password_file
, "r");
636 krb5_warnx(context
, N_("Failed to open the password file %s", ""),
641 if (fgets(passwd
, sizeof(passwd
), f
) == NULL
) {
642 krb5_warnx(context
, N_("Failed to read password from file %s", ""),
646 return EINVAL
; /* XXX Need a better error */
650 passwd
[strcspn(passwd
, "\n")] = '\0';
653 #ifdef HAVE_FRAMEWORK_SECURITY
654 if (passwd
[0] == '\0') {
661 realm
= krb5_principal_get_realm(context
, principal
);
663 ret
= krb5_unparse_name_flags(context
, principal
,
664 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &name
);
668 osret
= SecKeychainFindGenericPassword(NULL
, strlen(realm
), realm
,
670 &length
, &buffer
, NULL
);
672 if (osret
== noErr
&& length
< sizeof(passwd
) - 1) {
673 memcpy(passwd
, buffer
, length
);
674 passwd
[length
] = '\0';
681 memset(&cred
, 0, sizeof(cred
));
683 ret
= krb5_get_init_creds_opt_alloc(context
, &opt
);
685 krb5_warn(context
, ret
, "krb5_get_init_creds_opt_alloc");
689 krb5_get_init_creds_opt_set_default_flags(context
, "kinit",
690 krb5_principal_get_realm(context
, principal
), opt
);
692 if (forwardable_flag
!= -1)
693 krb5_get_init_creds_opt_set_forwardable(opt
, forwardable_flag
);
694 if (proxiable_flag
!= -1)
695 krb5_get_init_creds_opt_set_proxiable(opt
, proxiable_flag
);
697 krb5_get_init_creds_opt_set_anonymous(opt
, anonymous_flag
);
699 krb5_get_init_creds_opt_set_pac_request(context
, opt
,
700 pac_flag
? TRUE
: FALSE
);
701 if (canonicalize_flag
)
702 krb5_get_init_creds_opt_set_canonicalize(context
, opt
, TRUE
);
703 if (pk_enterprise_flag
|| enterprise_flag
|| canonicalize_flag
|| windows_flag
)
704 krb5_get_init_creds_opt_set_win2k(context
, opt
, TRUE
);
705 if (pk_user_id
|| ent_user_id
|| anonymous_pkinit
) {
706 ret
= krb5_get_init_creds_opt_set_pkinit(context
, opt
,
712 pk_use_enckey
? KRB5_GIC_OPT_PKINIT_USE_ENCKEY
: 0 |
713 anonymous_pkinit
? KRB5_GIC_OPT_PKINIT_ANONYMOUS
: 0,
718 krb5_warn(context
, ret
, "krb5_get_init_creds_opt_set_pkinit");
722 krb5_get_init_creds_opt_set_pkinit_user_certs(context
, opt
, ent_user_id
);
725 if (addrs_flag
!= -1)
726 krb5_get_init_creds_opt_set_addressless(context
, opt
,
727 addrs_flag
? FALSE
: TRUE
);
729 if (renew_life
== NULL
&& renewable_flag
)
730 renewstr
= "6 months";
732 renewstr
= renew_life
;
734 renew
= parse_time(renewstr
, "s");
736 errx(1, "unparsable time: %s", renewstr
);
738 krb5_get_init_creds_opt_set_renew_life(opt
, renew
);
741 if (ticket_life
!= 0)
742 krb5_get_init_creds_opt_set_tkt_life(opt
, ticket_life
);
745 int tmp
= parse_time(start_str
, "s");
747 errx(1, N_("unparsable time: %s", ""), start_str
);
752 if (etype_str
.num_strings
) {
755 enctype
= malloc(etype_str
.num_strings
* sizeof(*enctype
));
757 errx(1, "out of memory");
758 for(i
= 0; i
< etype_str
.num_strings
; i
++) {
759 ret
= krb5_string_to_enctype(context
,
760 etype_str
.strings
[i
],
763 errx(1, "unrecognized enctype: %s", etype_str
.strings
[i
]);
765 krb5_get_init_creds_opt_set_etype_list(opt
, enctype
,
766 etype_str
.num_strings
);
769 ret
= krb5_init_creds_init(context
, principal
, prompter
, NULL
, start_time
, opt
, &ctx
);
771 krb5_warn(context
, ret
, "krb5_init_creds_init");
776 ret
= krb5_init_creds_set_service(context
, ctx
, server_str
);
778 krb5_warn(context
, ret
, "krb5_init_creds_set_service");
783 if (fast_armor_cache_string
) {
786 ret
= krb5_cc_resolve(context
, fast_armor_cache_string
, &fastid
);
788 krb5_warn(context
, ret
, "krb5_cc_resolve(FAST cache)");
792 ret
= krb5_init_creds_set_fast_ccache(context
, ctx
, fastid
);
794 krb5_warn(context
, ret
, "krb5_init_creds_set_fast_ccache");
799 if (use_keytab
|| keytab_str
) {
800 ret
= krb5_init_creds_set_keytab(context
, ctx
, kt
);
802 krb5_warn(context
, ret
, "krb5_init_creds_set_keytab");
805 } else if (pk_user_id
|| ent_user_id
||
806 krb5_principal_is_anonymous(context
, principal
, KRB5_ANON_MATCH_ANY
)) {
808 } else if (!interactive
&& passwd
[0] == '\0') {
809 static int already_warned
= 0;
812 krb5_warnx(context
, "Not interactive, failed to get "
814 krb5_get_init_creds_opt_free(context
, opt
);
819 if (passwd
[0] == '\0') {
823 ret
= krb5_unparse_name(context
, principal
, &p
);
825 errx(1, "failed to generate passwd prompt: not enough memory");
827 aret
= asprintf(&prompt
, N_("%s's Password: ", ""), p
);
830 errx(1, "failed to generate passwd prompt: not enough memory");
832 if (UI_UTIL_read_pw_string(passwd
, sizeof(passwd
)-1, prompt
, 0)){
833 memset(passwd
, 0, sizeof(passwd
));
834 errx(1, "failed to read password");
840 ret
= krb5_init_creds_set_password(context
, ctx
, passwd
);
842 krb5_warn(context
, ret
, "krb5_init_creds_set_password");
848 ret
= krb5_init_creds_get(context
, ctx
);
851 if (ntlm_domain
&& passwd
[0])
852 heim_ntlm_nt_key(passwd
, &ntlmkey
);
854 memset_s(passwd
, sizeof(passwd
), 0, sizeof(passwd
));
859 case KRB5_LIBOS_PWDINTR
: /* don't print anything if it was just C-c:ed */
861 case KRB5KRB_AP_ERR_BAD_INTEGRITY
:
862 case KRB5KRB_AP_ERR_MODIFIED
:
863 case KRB5KDC_ERR_PREAUTH_FAILED
:
864 case KRB5_GET_IN_TKT_LOOP
:
865 krb5_warnx(context
, N_("Password incorrect", ""));
867 case KRB5KRB_AP_ERR_V4_REPLY
:
868 krb5_warnx(context
, N_("Looks like a Kerberos 4 reply", ""));
870 case KRB5KDC_ERR_KEY_EXPIRED
:
871 krb5_warnx(context
, N_("Password expired", ""));
874 krb5_warn(context
, ret
, "krb5_get_init_creds");
878 krb5_process_last_request(context
, opt
, ctx
);
880 ret
= krb5_init_creds_get_creds(context
, ctx
, &cred
);
882 krb5_warn(context
, ret
, "krb5_init_creds_get_creds");
886 if (ticket_life
!= 0) {
887 if (labs(cred
.times
.endtime
- cred
.times
.starttime
- ticket_life
) > 30) {
889 unparse_time_approx(cred
.times
.endtime
- cred
.times
.starttime
,
891 krb5_warnx(context
, N_("NOTICE: ticket lifetime is %s", ""), life
);
895 if (labs(cred
.times
.renew_till
- cred
.times
.starttime
- renew
) > 30) {
897 unparse_time_approx(cred
.times
.renew_till
- cred
.times
.starttime
,
900 N_("NOTICE: ticket renewable lifetime is %s", ""),
904 krb5_free_cred_contents(context
, &cred
);
906 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, ccache
),
909 krb5_warn(context
, ret
, "krb5_cc_new_unique");
913 ret
= krb5_init_creds_store(context
, ctx
, tempccache
);
915 krb5_warn(context
, ret
, "krb5_init_creds_store");
919 krb5_init_creds_free(context
, ctx
);
922 ret
= krb5_cc_move(context
, tempccache
, ccache
);
924 krb5_warn(context
, ret
, "krb5_cc_move");
929 if (switch_cache_flags
)
930 krb5_cc_switch(context
, ccache
);
933 if (ntlm_domain
&& ntlmkey
.data
)
934 store_ntlmkey(context
, ccache
, ntlm_domain
, &ntlmkey
);
937 if (ok_as_delegate_flag
|| windows_flag
|| use_referrals_flag
) {
941 if (ok_as_delegate_flag
|| windows_flag
)
943 if (use_referrals_flag
|| windows_flag
)
949 krb5_cc_set_config(context
, ccache
, NULL
, "realm-config", &data
);
952 if (anonymous_pkinit
) {
955 data
.length
= strlen(principal
->realm
);
956 data
.data
= principal
->realm
;
958 krb5_cc_set_config(context
, ccache
, NULL
, "anon_pkinit_realm", &data
);
962 krb5_get_init_creds_opt_free(context
, opt
);
964 krb5_init_creds_free(context
, ctx
);
966 krb5_cc_destroy(context
, tempccache
);
975 ticket_lifetime(krb5_context context
, krb5_ccache cache
, krb5_principal client
,
976 const char *server
, time_t *renew
)
978 krb5_creds in_cred
, *cred
;
983 memset(&in_cred
, 0, sizeof(in_cred
));
988 ret
= krb5_cc_get_principal(context
, cache
, &in_cred
.client
);
990 krb5_warn(context
, ret
, "krb5_cc_get_principal");
994 /* Determine TGS principal without fallback */
995 ret
= get_server(context
, cache
, in_cred
.client
, server
, FALSE
,
998 krb5_free_principal(context
, in_cred
.client
);
999 krb5_warn(context
, ret
, "get_server");
1003 ret
= krb5_get_credentials(context
, KRB5_GC_CACHED
,
1004 cache
, &in_cred
, &cred
);
1005 krb5_free_principal(context
, in_cred
.client
);
1006 krb5_free_principal(context
, in_cred
.server
);
1008 krb5_warn(context
, ret
, "krb5_get_credentials");
1011 curtime
= time(NULL
);
1012 timeout
= cred
->times
.endtime
- curtime
;
1016 *renew
= cred
->times
.renew_till
- curtime
;
1020 krb5_free_creds(context
, cred
);
1024 static time_t expire
;
1026 static char siginfo_msg
[1024] = "No credentials\n";
1029 update_siginfo_msg(time_t exp
, const char *srv
)
1031 /* Note that exp is relative time */
1032 memset(siginfo_msg
, 0, sizeof(siginfo_msg
));
1033 memcpy(&siginfo_msg
, "Updating...\n", sizeof("Updating...\n"));
1036 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
1037 N_("kinit: TGT expires in %llu seconds\n", ""),
1038 (unsigned long long)expire
);
1040 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
1041 N_("kinit: Ticket for %s expired\n", ""), srv
);
1048 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
1049 N_("kinit: TGT expired\n", ""));
1051 snprintf(siginfo_msg
, sizeof(siginfo_msg
),
1052 N_("kinit: Ticket for %s expired\n", ""), srv
);
1056 #ifdef HAVE_SIGACTION
1058 handle_siginfo(int sig
)
1060 struct iovec iov
[2];
1062 iov
[0].iov_base
= rk_UNCONST(siginfo_msg
);
1063 iov
[0].iov_len
= strlen(siginfo_msg
);
1064 iov
[1].iov_base
= "\n";
1067 writev(STDERR_FILENO
, iov
, sizeof(iov
)/sizeof(iov
[0]));
1072 krb5_context context
;
1074 krb5_principal principal
;
1075 krb5_deltat ticket_life
;
1076 krb5_deltat timeout
;
1080 renew_func(void *ptr
)
1082 krb5_error_code ret
;
1083 struct renew_ctx
*ctx
= ptr
;
1084 time_t renew_expire
;
1085 static time_t exp_delay
= 1;
1088 * NOTE: We count on the ccache implementation to notice changes to the
1089 * actual ccache filesystem/whatever objects. There should be no ccache
1090 * types for which this is not the case, but it might not hurt to
1091 * re-krb5_cc_resolve() after each successful renew_validate()/
1092 * get_new_tickets() call.
1095 expire
= ticket_lifetime(ctx
->context
, ctx
->ccache
, ctx
->principal
,
1096 server_str
, &renew_expire
);
1099 * When a keytab is available to obtain new tickets, if we are within
1100 * half of the original ticket lifetime of the renew limit, get a new
1101 * TGT instead of renewing the existing TGT. Note, ctx->ticket_life
1102 * is zero by default (without a '-l' option) and cannot be used to
1103 * set the time scale on which we decide whether we're "close to the
1106 if (use_keytab
|| keytab_str
)
1107 expire
+= ctx
->timeout
;
1108 if (renew_expire
> expire
) {
1109 ret
= renew_validate(ctx
->context
, 1, validate_flag
, &ctx
->ccache
,
1110 NULL
, FALSE
, server_str
, ctx
->ticket_life
);
1112 ret
= get_new_tickets(ctx
->context
, ctx
->principal
, ctx
->ccache
,
1113 ctx
->ticket_life
, 0, 0);
1115 expire
= ticket_lifetime(ctx
->context
, ctx
->ccache
, ctx
->principal
,
1116 server_str
, &renew_expire
);
1119 if (ret
== 0 && server_str
== NULL
&& do_afslog
&& k_hasafs())
1120 krb5_afslog(ctx
->context
, ctx
->ccache
, NULL
, NULL
);
1123 update_siginfo_msg(expire
, server_str
);
1126 * If our tickets have expired and we been able to either renew them
1127 * or obtain new tickets, then we still call this function but we use
1128 * an exponential backoff. This should take care of the case where
1129 * we are using stored credentials but the KDC has been unavailable
1130 * for some reason...
1135 * We can't ask to keep spamming stderr but not syslog, so we warn
1138 if (exp_delay
== 1) {
1139 krb5_warnx(ctx
->context
, N_("NOTICE: Could not renew/refresh "
1142 if (exp_delay
< 7200)
1143 exp_delay
+= exp_delay
/ 2 + 1;
1148 return expire
/ 2 + 1;
1152 set_princ_realm(krb5_context context
,
1153 krb5_principal principal
,
1156 krb5_error_code ret
;
1158 if ((ret
= krb5_principal_set_realm(context
, principal
, realm
)) != 0)
1159 krb5_err(context
, 1, ret
, "krb5_principal_set_realm");
1163 parse_name_realm(krb5_context context
,
1167 krb5_principal
*princ
)
1169 krb5_error_code ret
;
1172 flags
|= KRB5_PRINCIPAL_PARSE_NO_DEF_REALM
;
1173 if ((ret
= krb5_parse_name_flags(context
, name
, flags
, princ
)) != 0)
1174 krb5_err(context
, 1, ret
, "krb5_parse_name_flags");
1175 if (realm
&& krb5_principal_get_realm(context
, *princ
) == NULL
)
1176 set_princ_realm(context
, *princ
, realm
);
1180 get_default_realm(krb5_context context
)
1183 krb5_error_code ret
;
1185 if ((ret
= krb5_get_default_realm(context
, &realm
)) != 0)
1186 krb5_err(context
, 1, ret
, "krb5_get_default_realm");
1191 get_default_principal(krb5_context context
, krb5_principal
*princ
)
1193 krb5_error_code ret
;
1195 if ((ret
= krb5_get_default_principal(context
, princ
)) != 0)
1196 krb5_err(context
, 1, ret
, "krb5_get_default_principal");
1200 get_user_realm(krb5_context context
)
1202 krb5_error_code ret
;
1203 char *user_realm
= NULL
;
1206 * If memory allocation fails, we don't try to use the wrong realm,
1207 * that will trigger misleading error messages complicate support.
1209 krb5_appdefault_string(context
, "kinit", NULL
, "user_realm", "",
1211 if (user_realm
== NULL
) {
1212 ret
= krb5_enomem(context
);
1213 krb5_err(context
, 1, ret
, "krb5_appdefault_string");
1216 if (*user_realm
== 0) {
1225 get_princ(krb5_context context
,
1226 krb5_principal
*principal
,
1230 krb5_error_code ret
= 0;
1236 krb5_ccache ccache
= NULL
;
1238 /* If credential cache provides a client principal, use that. */
1240 ret
= krb5_cc_resolve(context
, ccname
, &ccache
);
1242 ret
= krb5_cc_default(context
, &ccache
);
1244 ret
= krb5_cc_get_principal(context
, ccache
, principal
);
1245 krb5_cc_close(context
, ccache
);
1250 user_realm
= get_user_realm(context
);
1253 if (enterprise_flag
)
1254 parseflags
|= KRB5_PRINCIPAL_PARSE_ENTERPRISE
;
1256 parse_name_realm(context
, name
, parseflags
, user_realm
, &tmp
);
1258 if (user_realm
&& krb5_principal_get_num_comp(context
, tmp
) > 1) {
1259 /* Principal is instance qualified, reparse with default realm. */
1260 krb5_free_principal(context
, tmp
);
1261 parse_name_realm(context
, name
, parseflags
, NULL
, principal
);
1266 get_default_principal(context
, principal
);
1268 set_princ_realm(context
, *principal
, user_realm
);
1276 get_princ_kt(krb5_context context
,
1277 krb5_principal
*principal
,
1280 krb5_error_code ret
;
1283 krb5_kt_cursor cursor
;
1284 krb5_keytab_entry entry
;
1289 * If the credential cache exists and specifies a client principal,
1292 if (krb5_cc_default(context
, &ccache
) == 0) {
1293 ret
= krb5_cc_get_principal(context
, ccache
, principal
);
1294 krb5_cc_close(context
, ccache
);
1301 /* If the principal specifies an explicit realm, just use that. */
1302 int parseflags
= KRB5_PRINCIPAL_PARSE_NO_DEF_REALM
;
1304 parse_name_realm(context
, name
, parseflags
, NULL
, &tmp
);
1305 if (krb5_principal_get_realm(context
, tmp
) != NULL
) {
1310 /* Otherwise, search keytab for bare name of the default principal. */
1311 get_default_principal(context
, &tmp
);
1312 set_princ_realm(context
, tmp
, NULL
);
1315 def_realm
= get_default_realm(context
);
1317 ret
= krb5_kt_start_seq_get(context
, kt
, &cursor
);
1319 krb5_err(context
, 1, ret
, "krb5_kt_start_seq_get");
1322 krb5_kt_next_entry(context
, kt
, &entry
, &cursor
) == 0) {
1325 if (!krb5_principal_compare_any_realm(context
, tmp
, entry
.principal
))
1328 krb5_principal_compare(context
, *principal
, entry
.principal
))
1330 /* The default realm takes precedence */
1331 realm
= krb5_principal_get_realm(context
, entry
.principal
);
1332 if (*principal
&& strcmp(def_realm
, realm
) == 0) {
1333 krb5_free_principal(context
, *principal
);
1334 ret
= krb5_copy_principal(context
, entry
.principal
, principal
);
1338 ret
= krb5_copy_principal(context
, entry
.principal
, principal
);
1340 if (ret
!= 0 || (ret
= krb5_kt_end_seq_get(context
, kt
, &cursor
)) != 0)
1341 krb5_err(context
, 1, ret
, "get_princ_kt");
1344 parse_name_realm(context
, name
, 0, NULL
, principal
);
1346 krb5_err(context
, 1, KRB5_CC_NOTFOUND
, "get_princ_kt");
1349 krb5_free_principal(context
, tmp
);
1354 main(int argc
, char **argv
)
1356 krb5_error_code ret
;
1357 krb5_context context
;
1359 krb5_principal principal
= NULL
;
1361 krb5_deltat ticket_life
= 0;
1362 #ifdef HAVE_SIGACTION
1363 struct sigaction sa
;
1365 krb5_boolean unique_ccache
= FALSE
;
1366 krb5_boolean historical_anon_pkinit
= FALSE
;
1367 krb5_boolean default_for
= FALSE
;
1368 int anonymous_pkinit
= FALSE
;
1370 setprogname(argv
[0]);
1372 setlocale(LC_ALL
, "");
1373 bindtextdomain("heimdal_kuser", HEIMDAL_LOCALEDIR
);
1374 textdomain("heimdal_kuser");
1376 ret
= krb5_init_context(&context
);
1377 if (ret
== KRB5_CONFIG_BADFORMAT
)
1378 errx(1, "krb5_init_context failed to parse configuration file");
1380 errx(1, "krb5_init_context failed: %d", ret
);
1382 if (getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
1389 print_version(NULL
);
1396 krb5_appdefault_boolean(context
, "kinit", NULL
, "historical_anon_pkinit",
1397 FALSE
, &historical_anon_pkinit
);
1400 * Open the keytab now, we use the keytab to determine the principal's
1401 * realm when the requested principal has no realm.
1403 if (use_keytab
|| keytab_str
) {
1405 ret
= krb5_kt_resolve(context
, keytab_str
, &kt
);
1407 ret
= krb5_kt_default(context
, &kt
);
1409 krb5_err(context
, 1, ret
, "resolving keytab");
1412 if (pk_enterprise_flag
) {
1413 ret
= krb5_pk_enterprise_cert(context
, pk_user_id
,
1414 argv
[0], &principal
,
1417 krb5_err(context
, 1, ret
, "krb5_pk_enterprise_certs");
1421 } else if (anonymous_flag
&& argc
&& argv
[0][0] == '@') {
1422 /* If principal argument as @REALM, try anonymous PKINIT */
1424 ret
= krb5_make_principal(context
, &principal
, &argv
[0][1],
1425 KRB5_WELLKNOWN_NAME
, KRB5_ANON_NAME
,
1428 krb5_err(context
, 1, ret
, "krb5_make_principal");
1429 krb5_principal_set_type(context
, principal
, KRB5_NT_WELLKNOWN
);
1430 anonymous_pkinit
= TRUE
;
1431 } else if (anonymous_flag
&& historical_anon_pkinit
) {
1432 char *realm
= argc
== 0 ? get_default_realm(context
) :
1433 argv
[0][0] == '@' ? &argv
[0][1] : argv
[0];
1435 ret
= krb5_make_principal(context
, &principal
, realm
,
1436 KRB5_WELLKNOWN_NAME
, KRB5_ANON_NAME
, NULL
);
1438 krb5_err(context
, 1, ret
, "krb5_make_principal");
1439 krb5_principal_set_type(context
, principal
, KRB5_NT_WELLKNOWN
);
1440 anonymous_pkinit
= TRUE
;
1441 } else if (use_keytab
|| keytab_str
) {
1442 get_princ_kt(context
, &principal
, argv
[0]);
1444 get_princ(context
, &principal
, cred_cache
, argv
[0]);
1448 krb5_set_fcache_version(context
, fcache_version
);
1450 if (renewable_flag
== -1)
1451 /* this seems somewhat pointless, but whatever */
1452 krb5_appdefault_boolean(context
, "kinit",
1453 krb5_principal_get_realm(context
, principal
),
1454 "renewable", FALSE
, &renewable_flag
);
1455 if (do_afslog
== -1)
1456 krb5_appdefault_boolean(context
, "kinit",
1457 krb5_principal_get_realm(context
, principal
),
1458 "afslog", TRUE
, &do_afslog
);
1463 * - use the given ccache
1464 * - use a new unique ccache for running a command with (in this case we
1465 * get to set KRB5CCNAME, so a new unique ccache makes sense)
1466 * - use the default ccache for the given principal and maybe later switch
1467 * the collection's default/primary to it
1469 * The important thing is that, except for the case where we're running a
1470 * command, we _can't set KRB5CCNAME_, and we can't expect the user to read
1471 * our output and figure out to set it (we could have an output-for-shell-
1472 * eval mode, like ssh-agent and such, but we don't). Therefore, in all
1473 * cases where we can't set KRB5CCNAME we must do something that makes
1474 * sense to the user, and that is to either initialize a given ccache, use
1475 * the default, or use a subsidiary ccache named after the principal whose
1476 * creds we're initializing.
1479 /* Use the given ccache */
1480 ret
= krb5_cc_resolve(context
, cred_cache
, &ccache
);
1481 } else if (argc
> 1) {
1485 * A command was given, so use a new unique ccache (and destroy it
1488 ret
= krb5_cc_new_unique(context
, NULL
, NULL
, &ccache
);
1490 krb5_err(context
, 1, ret
, "creating cred cache");
1491 snprintf(s
, sizeof(s
), "%s:%s",
1492 krb5_cc_get_type(context
, ccache
),
1493 krb5_cc_get_name(context
, ccache
));
1494 setenv("KRB5CCNAME", s
, 1);
1495 unique_ccache
= TRUE
;
1497 ret
= krb5_cc_default_for(context
, principal
, &ccache
);
1502 krb5_err(context
, 1, ret
, N_("resolving credentials cache", ""));
1505 if (argc
> 1 && k_hasafs())
1510 int tmp
= parse_time(lifetime
, "s");
1512 errx(1, N_("unparsable time: %s", ""), lifetime
);
1517 if (addrs_flag
== 0 && extra_addresses
.num_strings
> 0)
1518 krb5_errx(context
, 1,
1519 N_("specifying both extra addresses and "
1520 "no addresses makes no sense", ""));
1523 krb5_addresses addresses
;
1524 memset(&addresses
, 0, sizeof(addresses
));
1525 for(i
= 0; i
< extra_addresses
.num_strings
; i
++) {
1526 ret
= krb5_parse_address(context
, extra_addresses
.strings
[i
],
1529 krb5_add_extra_addresses(context
, &addresses
);
1530 krb5_free_addresses(context
, &addresses
);
1533 free_getarg_strings(&extra_addresses
);
1536 if (renew_flag
|| validate_flag
) {
1537 ret
= renew_validate(context
, renew_flag
, validate_flag
,
1538 &ccache
, principal
, default_for
, server_str
,
1542 if (ret
== 0 && server_str
== NULL
&& do_afslog
&& k_hasafs())
1543 krb5_afslog(context
, ccache
, NULL
, NULL
);
1547 krb5_cc_destroy(context
, ccache
);
1551 ret
= get_new_tickets(context
, principal
, ccache
, ticket_life
,
1552 1, anonymous_pkinit
);
1555 krb5_cc_destroy(context
, ccache
);
1560 if (ret
== 0 && server_str
== NULL
&& do_afslog
&& k_hasafs())
1561 krb5_afslog(context
, ccache
, NULL
, NULL
);
1565 struct renew_ctx ctx
;
1568 timeout
= ticket_lifetime(context
, ccache
, principal
,
1569 server_str
, NULL
) / 2;
1571 ctx
.context
= context
;
1572 ctx
.ccache
= ccache
;
1573 ctx
.principal
= principal
;
1574 ctx
.ticket_life
= ticket_life
;
1575 ctx
.timeout
= timeout
;
1577 #ifdef HAVE_SIGACTION
1578 memset(&sa
, 0, sizeof(sa
));
1579 sigemptyset(&sa
.sa_mask
);
1580 sa
.sa_handler
= handle_siginfo
;
1582 sigaction(SIGINFO
, &sa
, NULL
);
1585 ret
= simple_execvp_timed(argv
[1], argv
+1,
1586 renew_func
, &ctx
, timeout
);
1587 #define EX_NOEXEC 126
1588 #define EX_NOTFOUND 127
1589 if (ret
== EX_NOEXEC
)
1590 krb5_warnx(context
, N_("permission denied: %s", ""), argv
[1]);
1591 else if (ret
== EX_NOTFOUND
)
1592 krb5_warnx(context
, N_("command not found: %s", ""), argv
[1]);
1594 krb5_cc_destroy(context
, ccache
);
1600 krb5_cc_close(context
, ccache
);
1603 krb5_free_principal(context
, principal
);
1605 krb5_kt_close(context
, kt
);
1606 krb5_free_context(context
);