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>
42 struct krb5_dh_moduli;
43 struct AlgorithmIdentifier;
44 struct _krb5_krb_auth_data;
45 struct hx509_certs_data;
46 #include <krb5-private.h>
52 int forwardable_flag = -1;
53 int proxiable_flag = -1;
54 int renewable_flag = -1;
57 int validate_flag = 0;
61 struct getarg_strings extra_addresses;
62 int anonymous_flag = 0;
63 char *lifetime = NULL;
64 char *renew_life = NULL;
65 char *server_str = NULL;
66 char *cred_cache = NULL;
67 char *start_str = NULL;
68 static int switch_cache_flags = 1;
69 struct getarg_strings etype_str;
71 char *keytab_str = 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 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", "") },
107 { "cache", 'c', arg_string, &cred_cache,
108 NP_("credentials cache", ""), "cachename" },
110 { "forwardable", 0, arg_negative_flag, &forwardable_flag,
111 NP_("get tickets not forwardable", "")},
113 { NULL, 'f', arg_flag, &forwardable_flag,
114 NP_("get forwardable tickets", "")},
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", "") },
125 { "renew", 'R', arg_flag, &renew_flag,
126 NP_("renew TGT", "") },
128 { "renewable", 0, arg_flag, &renewable_flag,
129 NP_("get renewable tickets", "") },
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", "") },
143 { "validate", 'v', arg_flag, &validate_flag,
144 NP_("validate TGT", "") },
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", "") },
152 { "addresses", 'A', arg_negative_flag, &addrs_flag,
153 NP_("request a ticket with no addresses", "") },
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", "") },
161 { "request-pac", 0, arg_flag, &pac_flag,
162 NP_("request a Windows PAC", "") },
164 { "password-file", 0, arg_string, &password_file,
165 NP_("read the password from a file", "") },
167 { "canonicalize",0, arg_flag, &canonicalize_flag,
168 NP_("canonicalize client principal", "") },
170 { "enterprise",0, arg_flag, &enterprise_flag,
171 NP_("parse principal as a KRB5-NT-ENTERPRISE name", "") },
173 { "pk-enterprise", 0, arg_flag, &pk_enterprise_flag,
174 NP_("use enterprise name from certificate", "") },
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)", "") },
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", "") },
193 { "ok-as-delegate", 0, arg_flag, &ok_as_delegate_flag,
194 NP_("honor ok-as-delegate on tickets", "") },
196 { "use-referrals", 0, arg_flag, &use_referrals_flag,
197 NP_("only use referrals, no dns canalisation", "") },
199 { "windows", 0, arg_flag, &windows_flag,
200 NP_("get windows behavior", "") },
202 { "version", 0, arg_flag, &version_flag },
203 { "help", 0, arg_flag, &help_flag }
209 arg_printusage_i18n (args,
210 sizeof(args)/sizeof(*args),
213 "[principal [command]]",
218 static krb5_error_code
219 get_server(krb5_context context,
220 krb5_principal client,
222 krb5_principal *princ)
224 krb5_const_realm realm;
226 return krb5_parse_name(context, server, princ);
228 realm = krb5_principal_get_realm(context, client);
229 return krb5_make_principal(context, princ, realm,
230 KRB5_TGS_NAME, realm, NULL);
234 renew_validate(krb5_context context,
242 krb5_creds in, *out = NULL;
243 krb5_kdc_flags flags;
245 memset(&in, 0, sizeof(in));
247 ret = krb5_cc_get_principal(context, cache, &in.client);
249 krb5_warn(context, ret, "krb5_cc_get_principal");
252 ret = get_server(context, in.client, server, &in.server);
254 krb5_warn(context, ret, "get_server");
260 * no need to check the error here, it's only to be
261 * friendly to the user
263 krb5_get_credentials(context, KRB5_GC_CACHED, cache, &in, &out);
267 flags.b.renewable = flags.b.renew = renew;
268 flags.b.validate = validate;
270 if (forwardable_flag != -1)
271 flags.b.forwardable = forwardable_flag;
273 flags.b.forwardable = out->flags.b.forwardable;
275 if (proxiable_flag != -1)
276 flags.b.proxiable = proxiable_flag;
278 flags.b.proxiable = out->flags.b.proxiable;
281 flags.b.request_anonymous = anonymous_flag;
283 in.times.endtime = time(NULL) + life;
286 krb5_free_creds (context, out);
291 ret = krb5_get_kdc_cred(context,
299 krb5_warn(context, ret, "krb5_get_kdc_cred");
302 ret = krb5_cc_initialize(context, cache, in.client);
304 krb5_free_creds (context, out);
305 krb5_warn(context, ret, "krb5_cc_initialize");
308 ret = krb5_cc_store_cred(context, cache, out);
310 if(ret == 0 && server == NULL) {
311 /* only do this if it's a general renew-my-tgt request */
313 if(do_afslog && k_hasafs())
314 krb5_afslog(context, cache, NULL, NULL);
318 krb5_free_creds (context, out);
320 krb5_warn(context, ret, "krb5_cc_store_cred");
324 krb5_free_cred_contents(context, &in);
330 static krb5_error_code
331 store_ntlmkey(krb5_context context, krb5_ccache id,
332 const char *domain, struct ntlm_buf *buf)
338 asprintf(&name, "ntlm-key-%s", domain);
340 krb5_clear_error_message(context);
344 data.length = buf->length;
345 data.data = buf->data;
347 ret = krb5_cc_set_config(context, id, NULL, name, &data);
353 static krb5_error_code
354 get_new_tickets(krb5_context context,
355 krb5_principal principal,
357 krb5_deltat ticket_life,
361 krb5_get_init_creds_opt *opt;
364 krb5_deltat start_time = 0;
365 krb5_deltat renew = 0;
366 char *renewstr = NULL;
367 krb5_enctype *enctype = NULL;
368 krb5_ccache tempccache;
370 struct ntlm_buf ntlmkey;
371 memset(&ntlmkey, 0, sizeof(ntlmkey));
378 if (strcasecmp("STDIN", password_file) == 0)
381 f = fopen(password_file, "r");
383 krb5_errx(context, 1, "Failed to open the password file %s",
386 if (fgets(passwd, sizeof(passwd), f) == NULL)
387 krb5_errx(context, 1,
388 N_("Failed to read password from file %s", ""),
392 passwd[strcspn(passwd, "\n")] = '\0';
396 if (passwd[0] == '\0') {
403 realm = krb5_principal_get_realm(context, principal);
405 ret = krb5_unparse_name_flags(context, principal,
406 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &name);
410 osret = SecKeychainFindGenericPassword(NULL, strlen(realm), realm,
412 &length, &buffer, NULL);
414 if (osret == noErr && length < sizeof(passwd) - 1) {
415 memcpy(passwd, buffer, length);
416 passwd[length] = '\0';
423 memset(&cred, 0, sizeof(cred));
425 ret = krb5_get_init_creds_opt_alloc (context, &opt);
427 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc");
429 krb5_get_init_creds_opt_set_default_flags(context, "kinit",
430 krb5_principal_get_realm(context, principal), opt);
432 if(forwardable_flag != -1)
433 krb5_get_init_creds_opt_set_forwardable (opt, forwardable_flag);
434 if(proxiable_flag != -1)
435 krb5_get_init_creds_opt_set_proxiable (opt, proxiable_flag);
437 krb5_get_init_creds_opt_set_anonymous (opt, anonymous_flag);
439 krb5_get_init_creds_opt_set_pac_request(context, opt,
440 pac_flag ? TRUE : FALSE);
441 if (canonicalize_flag)
442 krb5_get_init_creds_opt_set_canonicalize(context, opt, TRUE);
443 if ((pk_enterprise_flag || enterprise_flag || canonicalize_flag) && windows_flag)
444 krb5_get_init_creds_opt_set_win2k(context, opt, TRUE);
445 if (pk_user_id || ent_user_id || anonymous_flag) {
446 ret = krb5_get_init_creds_opt_set_pkinit(context, opt,
452 pk_use_enckey ? 2 : 0 |
453 anonymous_flag ? 4 : 0,
458 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_set_pkinit");
460 _krb5_get_init_creds_opt_set_pkinit_user_certs(context, opt, ent_user_id);
463 if (addrs_flag != -1)
464 krb5_get_init_creds_opt_set_addressless(context, opt,
465 addrs_flag ? FALSE : TRUE);
467 if (renew_life == NULL && renewable_flag)
468 renewstr = "1 month";
470 renewstr = renew_life;
472 renew = parse_time (renewstr, "s");
474 errx (1, "unparsable time: %s", renewstr);
476 krb5_get_init_creds_opt_set_renew_life (opt, renew);
480 krb5_get_init_creds_opt_set_tkt_life (opt, ticket_life);
483 int tmp = parse_time (start_str, "s");
485 errx (1, N_("unparsable time: %s", ""), start_str);
490 if(etype_str.num_strings) {
493 enctype = malloc(etype_str.num_strings * sizeof(*enctype));
495 errx(1, "out of memory");
496 for(i = 0; i < etype_str.num_strings; i++) {
497 ret = krb5_string_to_enctype(context,
498 etype_str.strings[i],
501 errx(1, "unrecognized enctype: %s", etype_str.strings[i]);
503 krb5_get_init_creds_opt_set_etype_list(opt, enctype,
504 etype_str.num_strings);
507 if(use_keytab || keytab_str) {
510 ret = krb5_kt_resolve(context, keytab_str, &kt);
512 ret = krb5_kt_default(context, &kt);
514 krb5_err (context, 1, ret, "resolving keytab");
515 ret = krb5_get_init_creds_keytab (context,
522 krb5_kt_close(context, kt);
523 } else if (pk_user_id || ent_user_id || anonymous_flag) {
524 ret = krb5_get_init_creds_password (context,
533 } else if (!interactive) {
534 krb5_warnx(context, "Not interactive, failed to get initial ticket");
535 krb5_get_init_creds_opt_free(context, opt);
539 if (passwd[0] == '\0') {
542 krb5_unparse_name (context, principal, &p);
543 asprintf (&prompt, N_("%s's Password: ", ""), p);
546 if (UI_UTIL_read_pw_string(passwd, sizeof(passwd)-1, prompt, 0)){
547 memset(passwd, 0, sizeof(passwd));
554 ret = krb5_get_init_creds_password (context,
564 krb5_get_init_creds_opt_free(context, opt);
566 if (ntlm_domain && passwd[0])
567 heim_ntlm_nt_key(passwd, &ntlmkey);
569 memset(passwd, 0, sizeof(passwd));
574 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
576 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
577 case KRB5KRB_AP_ERR_MODIFIED:
578 case KRB5KDC_ERR_PREAUTH_FAILED:
579 krb5_errx(context, 1, N_("Password incorrect", ""));
581 case KRB5KRB_AP_ERR_V4_REPLY:
582 krb5_errx(context, 1, N_("Looks like a Kerberos 4 reply", ""));
585 krb5_err(context, 1, ret, "krb5_get_init_creds");
588 if(ticket_life != 0) {
589 if(abs(cred.times.endtime - cred.times.starttime - ticket_life) > 30) {
591 unparse_time_approx(cred.times.endtime - cred.times.starttime,
593 krb5_warnx(context, N_("NOTICE: ticket lifetime is %s", ""), life);
597 if(abs(cred.times.renew_till - cred.times.starttime - renew) > 30) {
599 unparse_time_approx(cred.times.renew_till - cred.times.starttime,
602 N_("NOTICE: ticket renewable lifetime is %s", ""),
607 ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, ccache),
610 krb5_err (context, 1, ret, "krb5_cc_new_unique");
612 ret = krb5_cc_initialize (context, tempccache, cred.client);
614 krb5_err (context, 1, ret, "krb5_cc_initialize");
616 ret = krb5_cc_store_cred (context, tempccache, &cred);
618 krb5_err (context, 1, ret, "krb5_cc_store_cred");
620 krb5_free_cred_contents (context, &cred);
622 ret = krb5_cc_move(context, tempccache, ccache);
624 krb5_err (context, 1, ret, "krb5_cc_move");
626 if (switch_cache_flags)
627 krb5_cc_switch(context, ccache);
630 if (ntlm_domain && ntlmkey.data)
631 store_ntlmkey(context, ccache, ntlm_domain, &ntlmkey);
634 if (ok_as_delegate_flag || windows_flag || use_referrals_flag) {
638 if (ok_as_delegate_flag || windows_flag)
640 if (use_referrals_flag || windows_flag)
646 krb5_cc_set_config(context, ccache, NULL, "realm-config", &data);
657 ticket_lifetime(krb5_context context, krb5_ccache cache,
658 krb5_principal client, const char *server)
660 krb5_creds in_cred, *cred;
664 memset(&in_cred, 0, sizeof(in_cred));
666 ret = krb5_cc_get_principal(context, cache, &in_cred.client);
668 krb5_warn(context, ret, "krb5_cc_get_principal");
671 ret = get_server(context, in_cred.client, server, &in_cred.server);
673 krb5_free_principal(context, in_cred.client);
674 krb5_warn(context, ret, "get_server");
678 ret = krb5_get_credentials(context, KRB5_GC_CACHED,
679 cache, &in_cred, &cred);
680 krb5_free_principal(context, in_cred.client);
681 krb5_free_principal(context, in_cred.server);
683 krb5_warn(context, ret, "krb5_get_credentials");
686 timeout = cred->times.endtime - cred->times.starttime;
689 krb5_free_creds(context, cred);
694 krb5_context context;
696 krb5_principal principal;
697 krb5_deltat ticket_life;
701 renew_func(void *ptr)
703 struct renew_ctx *ctx = ptr;
708 if (renewable_flag) {
709 ret = renew_validate(ctx->context, renewable_flag, validate_flag,
710 ctx->ccache, server_str, ctx->ticket_life);
717 get_new_tickets(ctx->context, ctx->principal,
718 ctx->ccache, ctx->ticket_life, 0);
721 if(do_afslog && k_hasafs())
722 krb5_afslog(ctx->context, ctx->ccache, NULL, NULL);
725 expire = ticket_lifetime(ctx->context, ctx->ccache, ctx->principal,
731 main (int argc, char **argv)
734 krb5_context context;
736 krb5_principal principal;
738 krb5_deltat ticket_life = 0;
741 setprogname (argv[0]);
743 setlocale (LC_ALL, "");
744 bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR);
745 textdomain("heimdal_kuser");
747 ret = krb5_init_context (&context);
748 if (ret == KRB5_CONFIG_BADFORMAT)
749 errx (1, "krb5_init_context failed to parse configuration file");
751 errx(1, "krb5_init_context failed: %d", ret);
753 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
767 if (canonicalize_flag || enterprise_flag)
768 parseflags |= KRB5_PRINCIPAL_PARSE_ENTERPRISE;
770 if (pk_enterprise_flag) {
771 ret = _krb5_pk_enterprise_cert(context, pk_user_id,
775 krb5_err(context, 1, ret, "krb5_pk_enterprise_certs");
779 } else if (anonymous_flag) {
781 ret = krb5_make_principal(context, &principal, argv[0],
782 KRB5_WELLKNOWN_NAME, KRB5_ANON_NAME,
785 krb5_err(context, 1, ret, "krb5_make_principal");
786 krb5_principal_set_type(context, principal, KRB5_NT_WELLKNOWN);
790 ret = krb5_parse_name_flags (context, argv[0], parseflags,
793 krb5_err (context, 1, ret, "krb5_parse_name");
795 ret = krb5_get_default_principal (context, &principal);
797 krb5_err (context, 1, ret, "krb5_get_default_principal");
802 krb5_set_fcache_version(context, fcache_version);
804 if(renewable_flag == -1)
805 /* this seems somewhat pointless, but whatever */
806 krb5_appdefault_boolean(context, "kinit",
807 krb5_principal_get_realm(context, principal),
808 "renewable", FALSE, &renewable_flag);
810 krb5_appdefault_boolean(context, "kinit",
811 krb5_principal_get_realm(context, principal),
812 "afslog", TRUE, &do_afslog);
815 ret = krb5_cc_resolve(context, cred_cache, &ccache);
819 ret = krb5_cc_new_unique(context, NULL, NULL, &ccache);
821 krb5_err(context, 1, ret, "creating cred cache");
822 snprintf(s, sizeof(s), "%s:%s",
823 krb5_cc_get_type(context, ccache),
824 krb5_cc_get_name(context, ccache));
825 setenv("KRB5CCNAME", s, 1);
827 ret = krb5_cc_cache_match(context, principal, &ccache);
830 ret = krb5_cc_default (context, &ccache);
832 krb5_err (context, 1, ret, N_("resolving credentials cache", ""));
835 * Check if the type support switching, and we do,
836 * then do that instead over overwriting the current
839 type = krb5_cc_get_type(context, ccache);
840 if (krb5_cc_support_switch(context, type)) {
841 krb5_cc_close(context, ccache);
842 ret = krb5_cc_new_unique(context, type, NULL, &ccache);
848 krb5_err (context, 1, ret, N_("resolving credentials cache", ""));
851 if(argc > 1 && k_hasafs ())
856 int tmp = parse_time (lifetime, "s");
858 errx (1, N_("unparsable time: %s", ""), lifetime);
863 if(addrs_flag == 0 && extra_addresses.num_strings > 0)
864 krb5_errx(context, 1,
865 N_("specifying both extra addresses and "
866 "no addresses makes no sense", ""));
869 krb5_addresses addresses;
870 memset(&addresses, 0, sizeof(addresses));
871 for(i = 0; i < extra_addresses.num_strings; i++) {
872 ret = krb5_parse_address(context, extra_addresses.strings[i],
875 krb5_add_extra_addresses(context, &addresses);
876 krb5_free_addresses(context, &addresses);
879 free_getarg_strings(&extra_addresses);
882 if(renew_flag || validate_flag) {
883 ret = renew_validate(context, renew_flag, validate_flag,
884 ccache, server_str, ticket_life);
888 get_new_tickets(context, principal, ccache, ticket_life, 1);
891 if(do_afslog && k_hasafs())
892 krb5_afslog(context, ccache, NULL, NULL);
895 struct renew_ctx ctx;
898 timeout = ticket_lifetime(context, ccache, principal, server_str) / 2;
900 ctx.context = context;
902 ctx.principal = principal;
903 ctx.ticket_life = ticket_life;
905 ret = simple_execvp_timed(argv[1], argv+1,
906 renew_func, &ctx, timeout);
907 #define EX_NOEXEC 126
908 #define EX_NOTFOUND 127
910 krb5_warnx(context, N_("permission denied: %s", ""), argv[1]);
911 else if(ret == EX_NOTFOUND)
912 krb5_warnx(context, N_("command not found: %s", ""), argv[1]);
914 krb5_cc_destroy(context, ccache);
920 krb5_cc_close (context, ccache);
923 krb5_free_principal(context, principal);
924 krb5_free_context (context);