2 * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "kuser_locl.h"
37 #include "krb5-v4compat.h"
41 int forwardable_flag = -1;
42 int proxiable_flag = -1;
43 int renewable_flag = -1;
46 int validate_flag = 0;
50 struct getarg_strings extra_addresses;
51 int anonymous_flag = 0;
52 char *lifetime = NULL;
53 char *renew_life = NULL;
54 char *server_str = NULL;
55 char *cred_cache = NULL;
56 char *start_str = NULL;
57 struct getarg_strings etype_str;
59 char *keytab_str = NULL;
61 #ifndef HEIMDAL_SMALLER
64 static char *krb4_cc_name;
67 char *password_file = NULL;
68 char *pk_user_id = NULL;
69 char *pk_x509_anchors = NULL;
70 int pk_use_enckey = 0;
71 static int canonicalize_flag = 0;
72 static int ok_as_delegate_flag = 0;
73 static int use_referrals_flag = 0;
74 static int windows_flag = 0;
75 static char *ntlm_domain;
78 static struct getargs args[] = {
88 #ifndef HEIMDAL_SMALLER
89 { "524init", '4', arg_flag, &get_v4_tgt,
90 NP_("obtain version 4 TGT", "") },
92 { "524convert", '9', arg_flag, &convert_524,
93 NP_("only convert ticket to version 4", "") },
95 { "afslog", 0 , arg_flag, &do_afslog,
96 NP_("obtain afs tokens", "") },
98 { "cache", 'c', arg_string, &cred_cache,
99 NP_("credentials cache", ""), "cachename" },
101 { "forwardable", 'f', arg_flag, &forwardable_flag,
102 NP_("get forwardable tickets", "")},
104 { "keytab", 't', arg_string, &keytab_str,
105 NP_("keytab to use", ""), "keytabname" },
107 { "lifetime", 'l', arg_string, &lifetime,
108 NP_("lifetime of tickets", ""), "time"},
110 { "proxiable", 'p', arg_flag, &proxiable_flag,
111 NP_("get proxiable tickets", "") },
113 { "renew", 'R', arg_flag, &renew_flag,
114 NP_("renew TGT", "") },
116 { "renewable", 0, arg_flag, &renewable_flag,
117 NP_("get renewable tickets", "") },
119 { "renewable-life", 'r', arg_string, &renew_life,
120 NP_("renewable lifetime of tickets", ""), "time" },
122 { "server", 'S', arg_string, &server_str,
123 NP_("server to get ticket for", ""), "principal" },
125 { "start-time", 's', arg_string, &start_str,
126 NP_("when ticket gets valid", ""), "time" },
128 { "use-keytab", 'k', arg_flag, &use_keytab,
129 NP_("get key from keytab", "") },
131 { "validate", 'v', arg_flag, &validate_flag,
132 NP_("validate TGT", "") },
134 { "enctypes", 'e', arg_strings, &etype_str,
135 NP_("encryption types to use", ""), "enctypes" },
137 { "fcache-version", 0, arg_integer, &fcache_version,
138 NP_("file cache version to create", "") },
140 { "addresses", 'A', arg_negative_flag, &addrs_flag,
141 NP_("request a ticket with no addresses", "") },
143 { "extra-addresses",'a', arg_strings, &extra_addresses,
144 NP_("include these extra addresses", ""), "addresses" },
146 { "anonymous", 0, arg_flag, &anonymous_flag,
147 NP_("request an anonymous ticket", "") },
149 { "request-pac", 0, arg_flag, &pac_flag,
150 NP_("request a Windows PAC", "") },
152 { "password-file", 0, arg_string, &password_file,
153 NP_("read the password from a file", "") },
155 { "canonicalize",0, arg_flag, &canonicalize_flag,
156 NP_("canonicalize client principal", "") },
158 { "pk-user", 'C', arg_string, &pk_user_id,
159 NP_("principal's public/private/certificate identifier", ""), "id" },
161 { "x509-anchors", 'D', arg_string, &pk_x509_anchors,
162 NP_("directory with CA certificates", ""), "directory" },
164 { "pk-use-enckey", 0, arg_flag, &pk_use_enckey,
165 NP_("Use RSA encrypted reply (instead of DH)", "") },
167 { "ntlm-domain", 0, arg_string, &ntlm_domain,
168 NP_("NTLM domain", ""), "domain" },
170 { "ok-as-delegate", 0, arg_flag, &ok_as_delegate_flag,
171 NP_("honor ok-as-delegate on tickets", "") },
173 { "use-referrals", 0, arg_flag, &use_referrals_flag,
174 NP_("only use referrals, no dns canalisation", "") },
176 { "windows", 0, arg_flag, &windows_flag,
177 NP_("get windows behavior", "") },
179 { "version", 0, arg_flag, &version_flag },
180 { "help", 0, arg_flag, &help_flag }
186 arg_printusage_i18n (args,
187 sizeof(args)/sizeof(*args),
190 "[principal [command]]",
195 static krb5_error_code
196 get_server(krb5_context context,
197 krb5_principal client,
199 krb5_principal *princ)
201 krb5_realm *client_realm;
203 return krb5_parse_name(context, server, princ);
205 client_realm = krb5_princ_realm (context, client);
206 return krb5_make_principal(context, princ, *client_realm,
207 KRB5_TGS_NAME, *client_realm, NULL);
210 #ifndef HEIMDAL_SMALLER
212 static krb5_error_code
213 do_524init(krb5_context context, krb5_ccache ccache,
214 krb5_creds *creds, const char *server)
218 struct credentials c;
219 krb5_creds in_creds, *real_creds;
224 krb5_principal client;
225 krb5_cc_get_principal(context, ccache, &client);
226 memset(&in_creds, 0, sizeof(in_creds));
227 ret = get_server(context, client, server, &in_creds.server);
229 krb5_free_principal(context, client);
232 in_creds.client = client;
233 ret = krb5_get_credentials(context, 0, ccache, &in_creds, &real_creds);
234 krb5_free_principal(context, client);
235 krb5_free_principal(context, in_creds.server);
239 ret = krb524_convert_creds_kdc_ccache(context, ccache, real_creds, &c);
241 krb5_warn(context, ret, "converting creds");
243 krb5_error_code tret = _krb5_krb_tf_setup(context, &c, NULL, 0);
245 krb5_warn(context, tret, "saving v4 creds");
249 krb5_free_creds(context, real_creds);
250 memset(&c, 0, sizeof(c));
258 renew_validate(krb5_context context,
266 krb5_creds in, *out = NULL;
267 krb5_kdc_flags flags;
269 memset(&in, 0, sizeof(in));
271 ret = krb5_cc_get_principal(context, cache, &in.client);
273 krb5_warn(context, ret, "krb5_cc_get_principal");
276 ret = get_server(context, in.client, server, &in.server);
278 krb5_warn(context, ret, "get_server");
284 * no need to check the error here, it's only to be
285 * friendly to the user
287 krb5_get_credentials(context, KRB5_GC_CACHED, cache, &in, &out);
291 flags.b.renewable = flags.b.renew = renew;
292 flags.b.validate = validate;
294 if (forwardable_flag != -1)
295 flags.b.forwardable = forwardable_flag;
297 flags.b.forwardable = out->flags.b.forwardable;
299 if (proxiable_flag != -1)
300 flags.b.proxiable = proxiable_flag;
302 flags.b.proxiable = out->flags.b.proxiable;
304 if (anonymous_flag != -1)
305 flags.b.request_anonymous = anonymous_flag;
307 in.times.endtime = time(NULL) + life;
310 krb5_free_creds (context, out);
315 ret = krb5_get_kdc_cred(context,
323 krb5_warn(context, ret, "krb5_get_kdc_cred");
326 ret = krb5_cc_initialize(context, cache, in.client);
328 krb5_free_creds (context, out);
329 krb5_warn(context, ret, "krb5_cc_initialize");
332 ret = krb5_cc_store_cred(context, cache, out);
334 if(ret == 0 && server == NULL) {
335 /* only do this if it's a general renew-my-tgt request */
336 #ifndef HEIMDAL_SMALLER
338 do_524init(context, cache, out, NULL);
340 if(do_afslog && k_hasafs())
341 krb5_afslog(context, cache, NULL, NULL);
344 krb5_free_creds (context, out);
346 krb5_warn(context, ret, "krb5_cc_store_cred");
350 krb5_free_cred_contents(context, &in);
354 static krb5_error_code
355 store_ntlmkey(krb5_context context, krb5_ccache id,
356 const char *domain, struct ntlm_buf *buf)
362 asprintf(&name, "ntlm-key-%s", domain);
364 krb5_clear_error_message(context);
368 data.length = buf->length;
369 data.data = buf->data;
371 ret = krb5_cc_set_config(context, id, NULL, name, &data);
376 static krb5_error_code
377 get_new_tickets(krb5_context context,
378 krb5_principal principal,
380 krb5_deltat ticket_life,
384 krb5_get_init_creds_opt *opt;
387 krb5_deltat start_time = 0;
388 krb5_deltat renew = 0;
389 char *renewstr = NULL;
390 krb5_enctype *enctype = NULL;
391 struct ntlm_buf ntlmkey;
392 krb5_ccache tempccache;
394 memset(&ntlmkey, 0, sizeof(ntlmkey));
400 if (strcasecmp("STDIN", password_file) == 0)
403 f = fopen(password_file, "r");
405 krb5_errx(context, 1, "Failed to open the password file %s",
408 if (fgets(passwd, sizeof(passwd), f) == NULL)
409 krb5_errx(context, 1,
410 N_("Failed to read password from file %s", ""),
414 passwd[strcspn(passwd, "\n")] = '\0';
418 memset(&cred, 0, sizeof(cred));
420 ret = krb5_get_init_creds_opt_alloc (context, &opt);
422 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc");
424 krb5_get_init_creds_opt_set_default_flags(context, "kinit",
425 krb5_principal_get_realm(context, principal), opt);
427 if(forwardable_flag != -1)
428 krb5_get_init_creds_opt_set_forwardable (opt, forwardable_flag);
429 if(proxiable_flag != -1)
430 krb5_get_init_creds_opt_set_proxiable (opt, proxiable_flag);
431 if(anonymous_flag != -1)
432 krb5_get_init_creds_opt_set_anonymous (opt, anonymous_flag);
434 krb5_get_init_creds_opt_set_pac_request(context, opt,
435 pac_flag ? TRUE : FALSE);
436 if (canonicalize_flag)
437 krb5_get_init_creds_opt_set_canonicalize(context, opt, TRUE);
439 ret = krb5_get_init_creds_opt_set_pkinit(context, opt,
445 pk_use_enckey ? 2 : 0,
450 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_set_pkinit");
453 if (addrs_flag != -1)
454 krb5_get_init_creds_opt_set_addressless(context, opt,
455 addrs_flag ? FALSE : TRUE);
457 if (renew_life == NULL && renewable_flag)
458 renewstr = "1 month";
460 renewstr = renew_life;
462 renew = parse_time (renewstr, "s");
464 errx (1, "unparsable time: %s", renewstr);
466 krb5_get_init_creds_opt_set_renew_life (opt, renew);
470 krb5_get_init_creds_opt_set_tkt_life (opt, ticket_life);
473 int tmp = parse_time (start_str, "s");
475 errx (1, N_("unparsable time: %s", ""), start_str);
480 if(etype_str.num_strings) {
483 enctype = malloc(etype_str.num_strings * sizeof(*enctype));
485 errx(1, "out of memory");
486 for(i = 0; i < etype_str.num_strings; i++) {
487 ret = krb5_string_to_enctype(context,
488 etype_str.strings[i],
491 errx(1, "unrecognized enctype: %s", etype_str.strings[i]);
493 krb5_get_init_creds_opt_set_etype_list(opt, enctype,
494 etype_str.num_strings);
497 if(use_keytab || keytab_str) {
500 ret = krb5_kt_resolve(context, keytab_str, &kt);
502 ret = krb5_kt_default(context, &kt);
504 krb5_err (context, 1, ret, "resolving keytab");
505 ret = krb5_get_init_creds_keytab (context,
512 krb5_kt_close(context, kt);
513 } else if (pk_user_id) {
514 ret = krb5_get_init_creds_password (context,
523 } else if (!interactive) {
524 krb5_warnx(context, "Not interactive, failed to get initial ticket");
525 krb5_get_init_creds_opt_free(context, opt);
529 if (passwd[0] == '\0') {
532 krb5_unparse_name (context, principal, &p);
533 asprintf (&prompt, N_("%s's Password: ", ""), p);
536 if (UI_UTIL_read_pw_string(passwd, sizeof(passwd)-1, prompt, 0)){
537 memset(passwd, 0, sizeof(passwd));
544 ret = krb5_get_init_creds_password (context,
554 krb5_get_init_creds_opt_free(context, opt);
555 if (ntlm_domain && passwd[0])
556 heim_ntlm_nt_key(passwd, &ntlmkey);
557 memset(passwd, 0, sizeof(passwd));
562 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
564 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
565 case KRB5KRB_AP_ERR_MODIFIED:
566 case KRB5KDC_ERR_PREAUTH_FAILED:
567 krb5_errx(context, 1, N_("Password incorrect", ""));
569 case KRB5KRB_AP_ERR_V4_REPLY:
570 krb5_errx(context, 1, N_("Looks like a Kerberos 4 reply", ""));
573 krb5_err(context, 1, ret, "krb5_get_init_creds");
576 if(ticket_life != 0) {
577 if(abs(cred.times.endtime - cred.times.starttime - ticket_life) > 30) {
579 unparse_time_approx(cred.times.endtime - cred.times.starttime,
581 krb5_warnx(context, N_("NOTICE: ticket lifetime is %s", ""), life);
585 if(abs(cred.times.renew_till - cred.times.starttime - renew) > 30) {
587 unparse_time_approx(cred.times.renew_till - cred.times.starttime,
590 N_("NOTICE: ticket renewable lifetime is %s", ""),
595 ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, ccache),
598 krb5_err (context, 1, ret, "krb5_cc_new_unique");
600 ret = krb5_cc_initialize (context, tempccache, cred.client);
602 krb5_err (context, 1, ret, "krb5_cc_initialize");
604 ret = krb5_cc_store_cred (context, tempccache, &cred);
606 krb5_err (context, 1, ret, "krb5_cc_store_cred");
608 krb5_free_cred_contents (context, &cred);
610 ret = krb5_cc_move(context, tempccache, ccache);
612 krb5_err (context, 1, ret, "krb5_cc_move");
614 if (ntlm_domain && ntlmkey.data)
615 store_ntlmkey(context, ccache, ntlm_domain, &ntlmkey);
617 if (ok_as_delegate_flag || windows_flag || use_referrals_flag) {
621 if (ok_as_delegate_flag || windows_flag)
623 if (use_referrals_flag || windows_flag)
629 krb5_cc_set_config(context, ccache, NULL, "realm-config", &data);
640 ticket_lifetime(krb5_context context, krb5_ccache cache,
641 krb5_principal client, const char *server)
643 krb5_creds in_cred, *cred;
647 memset(&in_cred, 0, sizeof(in_cred));
649 ret = krb5_cc_get_principal(context, cache, &in_cred.client);
651 krb5_warn(context, ret, "krb5_cc_get_principal");
654 ret = get_server(context, in_cred.client, server, &in_cred.server);
656 krb5_free_principal(context, in_cred.client);
657 krb5_warn(context, ret, "get_server");
661 ret = krb5_get_credentials(context, KRB5_GC_CACHED,
662 cache, &in_cred, &cred);
663 krb5_free_principal(context, in_cred.client);
664 krb5_free_principal(context, in_cred.server);
666 krb5_warn(context, ret, "krb5_get_credentials");
669 timeout = cred->times.endtime - cred->times.starttime;
672 krb5_free_creds(context, cred);
677 krb5_context context;
679 krb5_principal principal;
680 krb5_deltat ticket_life;
684 renew_func(void *ptr)
686 struct renew_ctx *ctx = ptr;
691 if (renewable_flag) {
692 ret = renew_validate(ctx->context, renewable_flag, validate_flag,
693 ctx->ccache, server_str, ctx->ticket_life);
700 get_new_tickets(ctx->context, ctx->principal,
701 ctx->ccache, ctx->ticket_life, 0);
703 #ifndef HEIMDAL_SMALLER
704 if(get_v4_tgt || convert_524)
705 do_524init(ctx->context, ctx->ccache, NULL, server_str);
707 if(do_afslog && k_hasafs())
708 krb5_afslog(ctx->context, ctx->ccache, NULL, NULL);
710 expire = ticket_lifetime(ctx->context, ctx->ccache, ctx->principal,
716 main (int argc, char **argv)
719 krb5_context context;
721 krb5_principal principal;
723 krb5_deltat ticket_life = 0;
726 setprogname (argv[0]);
728 setlocale (LC_ALL, "");
729 #if defined(HEIMDAL_LOCALEDIR)
730 bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR);
731 textdomain("heimdal_kuser");
734 ret = krb5_init_context (&context);
735 if (ret == KRB5_CONFIG_BADFORMAT)
736 errx (1, "krb5_init_context failed to parse configuration file");
738 errx(1, "krb5_init_context failed: %d", ret);
740 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
754 if (canonicalize_flag)
755 parseflags |= KRB5_PRINCIPAL_PARSE_ENTERPRISE;
758 ret = krb5_parse_name_flags (context, argv[0], parseflags, &principal);
760 krb5_err (context, 1, ret, "krb5_parse_name");
762 ret = krb5_get_default_principal (context, &principal);
764 krb5_err (context, 1, ret, "krb5_get_default_principal");
768 krb5_set_fcache_version(context, fcache_version);
770 if(renewable_flag == -1)
771 /* this seems somewhat pointless, but whatever */
772 krb5_appdefault_boolean(context, "kinit",
773 krb5_principal_get_realm(context, principal),
774 "renewable", FALSE, &renewable_flag);
775 #ifndef HEIMDAL_SMALLER
777 krb5_appdefault_boolean(context, "kinit",
778 krb5_principal_get_realm(context, principal),
779 "krb4_get_tickets", FALSE, &get_v4_tgt);
782 krb5_appdefault_boolean(context, "kinit",
783 krb5_principal_get_realm(context, principal),
784 "afslog", TRUE, &do_afslog);
787 ret = krb5_cc_resolve(context, cred_cache, &ccache);
791 ret = krb5_cc_gen_new(context, &krb5_fcc_ops, &ccache);
793 krb5_err(context, 1, ret, "creating cred cache");
794 snprintf(s, sizeof(s), "%s:%s",
795 krb5_cc_get_type(context, ccache),
796 krb5_cc_get_name(context, ccache));
797 setenv("KRB5CCNAME", s, 1);
798 #ifndef HEIMDAL_SMALLER
801 if (asprintf(&krb4_cc_name, "%s_XXXXXX", TKT_ROOT) < 0)
802 krb5_errx(context, 1, "out of memory");
803 if((fd = mkstemp(krb4_cc_name)) >= 0) {
805 setenv("KRBTKFILE", krb4_cc_name, 1);
813 ret = krb5_cc_cache_match(context, principal, &ccache);
815 ret = krb5_cc_default (context, &ccache);
819 krb5_err (context, 1, ret, N_("resolving credentials cache", ""));
821 if(argc > 1 && k_hasafs ())
825 int tmp = parse_time (lifetime, "s");
827 errx (1, N_("unparsable time: %s", ""), lifetime);
832 if(addrs_flag == 0 && extra_addresses.num_strings > 0)
833 krb5_errx(context, 1,
834 N_("specifying both extra addresses and "
835 "no addresses makes no sense", ""));
838 krb5_addresses addresses;
839 memset(&addresses, 0, sizeof(addresses));
840 for(i = 0; i < extra_addresses.num_strings; i++) {
841 ret = krb5_parse_address(context, extra_addresses.strings[i],
844 krb5_add_extra_addresses(context, &addresses);
845 krb5_free_addresses(context, &addresses);
848 free_getarg_strings(&extra_addresses);
851 if(renew_flag || validate_flag) {
852 ret = renew_validate(context, renew_flag, validate_flag,
853 ccache, server_str, ticket_life);
857 #ifndef HEIMDAL_SMALLER
860 get_new_tickets(context, principal, ccache, ticket_life, 1);
862 #ifndef HEIMDAL_SMALLER
863 if(get_v4_tgt || convert_524)
864 do_524init(context, ccache, NULL, server_str);
866 if(do_afslog && k_hasafs())
867 krb5_afslog(context, ccache, NULL, NULL);
869 struct renew_ctx ctx;
872 timeout = ticket_lifetime(context, ccache, principal, server_str) / 2;
874 ctx.context = context;
876 ctx.principal = principal;
877 ctx.ticket_life = ticket_life;
879 ret = simple_execvp_timed(argv[1], argv+1,
880 renew_func, &ctx, timeout);
881 #define EX_NOEXEC 126
882 #define EX_NOTFOUND 127
884 krb5_warnx(context, N_("permission denied: %s", ""), argv[1]);
885 else if(ret == EX_NOTFOUND)
886 krb5_warnx(context, N_("command not found: %s", ""), argv[1]);
888 krb5_cc_destroy(context, ccache);
889 #ifndef HEIMDAL_SMALLER
890 _krb5_krb_dest_tkt(context, krb4_cc_name);
895 krb5_cc_close (context, ccache);
898 krb5_free_principal(context, principal);
899 krb5_free_context (context);