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"
35 RCSID("$Id: kinit.c 23418 2008-07-26 18:36:48Z lha $");
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;
64 char *password_file = NULL;
65 char *pk_user_id = NULL;
66 char *pk_x509_anchors = NULL;
67 int pk_use_enckey = 0;
68 static int canonicalize_flag = 0;
69 static int ok_as_delegate_flag = 0;
70 static int windows_flag = 0;
71 static char *ntlm_domain;
73 static char *krb4_cc_name;
75 static struct getargs args[] = {
85 { "524init", '4', arg_flag, &get_v4_tgt,
86 "obtain version 4 TGT" },
88 { "524convert", '9', arg_flag, &convert_524,
89 "only convert ticket to version 4" },
91 { "afslog", 0 , arg_flag, &do_afslog,
92 "obtain afs tokens" },
94 { "cache", 'c', arg_string, &cred_cache,
95 "credentials cache", "cachename" },
97 { "forwardable", 'f', arg_flag, &forwardable_flag,
98 "get forwardable tickets"},
100 { "keytab", 't', arg_string, &keytab_str,
101 "keytab to use", "keytabname" },
103 { "lifetime", 'l', arg_string, &lifetime,
104 "lifetime of tickets", "time"},
106 { "proxiable", 'p', arg_flag, &proxiable_flag,
107 "get proxiable tickets" },
109 { "renew", 'R', arg_flag, &renew_flag,
112 { "renewable", 0, arg_flag, &renewable_flag,
113 "get renewable tickets" },
115 { "renewable-life", 'r', arg_string, &renew_life,
116 "renewable lifetime of tickets", "time" },
118 { "server", 'S', arg_string, &server_str,
119 "server to get ticket for", "principal" },
121 { "start-time", 's', arg_string, &start_str,
122 "when ticket gets valid", "time" },
124 { "use-keytab", 'k', arg_flag, &use_keytab,
125 "get key from keytab" },
127 { "validate", 'v', arg_flag, &validate_flag,
130 { "enctypes", 'e', arg_strings, &etype_str,
131 "encryption types to use", "enctypes" },
133 { "fcache-version", 0, arg_integer, &fcache_version,
134 "file cache version to create" },
136 { "addresses", 'A', arg_negative_flag, &addrs_flag,
137 "request a ticket with no addresses" },
139 { "extra-addresses",'a', arg_strings, &extra_addresses,
140 "include these extra addresses", "addresses" },
142 { "anonymous", 0, arg_flag, &anonymous_flag,
143 "request an anonymous ticket" },
145 { "request-pac", 0, arg_flag, &pac_flag,
146 "request a Windows PAC" },
148 { "password-file", 0, arg_string, &password_file,
149 "read the password from a file" },
151 { "canonicalize",0, arg_flag, &canonicalize_flag,
152 "canonicalize client principal" },
154 { "pk-user", 'C', arg_string, &pk_user_id,
155 "principal's public/private/certificate identifier", "id" },
157 { "x509-anchors", 'D', arg_string, &pk_x509_anchors,
158 "directory with CA certificates", "directory" },
160 { "pk-use-enckey", 0, arg_flag, &pk_use_enckey,
161 "Use RSA encrypted reply (instead of DH)" },
163 { "ntlm-domain", 0, arg_string, &ntlm_domain,
164 "NTLM domain", "domain" },
166 { "ok-as-delegate", 0, arg_flag, &ok_as_delegate_flag,
167 "honor ok-as-delegate on tickets" },
169 { "windows", 0, arg_flag, &windows_flag,
170 "get windows behavior" },
172 { "version", 0, arg_flag, &version_flag },
173 { "help", 0, arg_flag, &help_flag }
179 arg_printusage (args,
180 sizeof(args)/sizeof(*args),
182 "[principal [command]]");
186 static krb5_error_code
187 get_server(krb5_context context,
188 krb5_principal client,
190 krb5_principal *princ)
192 krb5_realm *client_realm;
194 return krb5_parse_name(context, server, princ);
196 client_realm = krb5_princ_realm (context, client);
197 return krb5_make_principal(context, princ, *client_realm,
198 KRB5_TGS_NAME, *client_realm, NULL);
201 static krb5_error_code
202 do_524init(krb5_context context, krb5_ccache ccache,
203 krb5_creds *creds, const char *server)
207 struct credentials c;
208 krb5_creds in_creds, *real_creds;
213 krb5_principal client;
214 krb5_cc_get_principal(context, ccache, &client);
215 memset(&in_creds, 0, sizeof(in_creds));
216 ret = get_server(context, client, server, &in_creds.server);
218 krb5_free_principal(context, client);
221 in_creds.client = client;
222 ret = krb5_get_credentials(context, 0, ccache, &in_creds, &real_creds);
223 krb5_free_principal(context, client);
224 krb5_free_principal(context, in_creds.server);
228 ret = krb524_convert_creds_kdc_ccache(context, ccache, real_creds, &c);
230 krb5_warn(context, ret, "converting creds");
232 krb5_error_code tret = _krb5_krb_tf_setup(context, &c, NULL, 0);
234 krb5_warn(context, tret, "saving v4 creds");
238 krb5_free_creds(context, real_creds);
239 memset(&c, 0, sizeof(c));
245 renew_validate(krb5_context context,
253 krb5_creds in, *out = NULL;
254 krb5_kdc_flags flags;
256 memset(&in, 0, sizeof(in));
258 ret = krb5_cc_get_principal(context, cache, &in.client);
260 krb5_warn(context, ret, "krb5_cc_get_principal");
263 ret = get_server(context, in.client, server, &in.server);
265 krb5_warn(context, ret, "get_server");
271 * no need to check the error here, it's only to be
272 * friendly to the user
274 krb5_get_credentials(context, KRB5_GC_CACHED, cache, &in, &out);
278 flags.b.renewable = flags.b.renew = renew;
279 flags.b.validate = validate;
281 if (forwardable_flag != -1)
282 flags.b.forwardable = forwardable_flag;
284 flags.b.forwardable = out->flags.b.forwardable;
286 if (proxiable_flag != -1)
287 flags.b.proxiable = proxiable_flag;
289 flags.b.proxiable = out->flags.b.proxiable;
291 if (anonymous_flag != -1)
292 flags.b.request_anonymous = anonymous_flag;
294 in.times.endtime = time(NULL) + life;
297 krb5_free_creds (context, out);
302 ret = krb5_get_kdc_cred(context,
310 krb5_warn(context, ret, "krb5_get_kdc_cred");
313 ret = krb5_cc_initialize(context, cache, in.client);
315 krb5_free_creds (context, out);
316 krb5_warn(context, ret, "krb5_cc_initialize");
319 ret = krb5_cc_store_cred(context, cache, out);
321 if(ret == 0 && server == NULL) {
322 /* only do this if it's a general renew-my-tgt request */
324 do_524init(context, cache, out, NULL);
325 if(do_afslog && k_hasafs())
326 krb5_afslog(context, cache, NULL, NULL);
329 krb5_free_creds (context, out);
331 krb5_warn(context, ret, "krb5_cc_store_cred");
335 krb5_free_cred_contents(context, &in);
339 static krb5_error_code
340 store_ntlmkey(krb5_context context, krb5_ccache id,
341 const char *domain, struct ntlm_buf *buf)
347 asprintf(&name, "ntlm-key-%s", domain);
349 krb5_clear_error_string(context);
353 data.length = buf->length;
354 data.data = buf->data;
356 ret = krb5_cc_set_config(context, id, NULL, name, &data);
361 static krb5_error_code
362 get_new_tickets(krb5_context context,
363 krb5_principal principal,
365 krb5_deltat ticket_life,
369 krb5_get_init_creds_opt *opt;
372 krb5_deltat start_time = 0;
373 krb5_deltat renew = 0;
374 char *renewstr = NULL;
375 krb5_enctype *enctype = NULL;
376 struct ntlm_buf ntlmkey;
377 krb5_ccache tempccache;
379 memset(&ntlmkey, 0, sizeof(ntlmkey));
385 if (strcasecmp("STDIN", password_file) == 0)
388 f = fopen(password_file, "r");
390 krb5_errx(context, 1, "Failed to open the password file %s",
393 if (fgets(passwd, sizeof(passwd), f) == NULL)
394 krb5_errx(context, 1,
395 "Failed to read password from file %s", password_file);
398 passwd[strcspn(passwd, "\n")] = '\0';
402 memset(&cred, 0, sizeof(cred));
404 ret = krb5_get_init_creds_opt_alloc (context, &opt);
406 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc");
408 krb5_get_init_creds_opt_set_default_flags(context, "kinit",
409 krb5_principal_get_realm(context, principal), opt);
411 if(forwardable_flag != -1)
412 krb5_get_init_creds_opt_set_forwardable (opt, forwardable_flag);
413 if(proxiable_flag != -1)
414 krb5_get_init_creds_opt_set_proxiable (opt, proxiable_flag);
415 if(anonymous_flag != -1)
416 krb5_get_init_creds_opt_set_anonymous (opt, anonymous_flag);
418 krb5_get_init_creds_opt_set_pac_request(context, opt,
419 pac_flag ? TRUE : FALSE);
420 if (canonicalize_flag)
421 krb5_get_init_creds_opt_set_canonicalize(context, opt, TRUE);
423 ret = krb5_get_init_creds_opt_set_pkinit(context, opt,
429 pk_use_enckey ? 2 : 0,
434 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_set_pkinit");
437 if (addrs_flag != -1)
438 krb5_get_init_creds_opt_set_addressless(context, opt,
439 addrs_flag ? FALSE : TRUE);
441 if (renew_life == NULL && renewable_flag)
442 renewstr = "1 month";
444 renewstr = renew_life;
446 renew = parse_time (renewstr, "s");
448 errx (1, "unparsable time: %s", renewstr);
450 krb5_get_init_creds_opt_set_renew_life (opt, renew);
454 krb5_get_init_creds_opt_set_tkt_life (opt, ticket_life);
457 int tmp = parse_time (start_str, "s");
459 errx (1, "unparsable time: %s", start_str);
464 if(etype_str.num_strings) {
467 enctype = malloc(etype_str.num_strings * sizeof(*enctype));
469 errx(1, "out of memory");
470 for(i = 0; i < etype_str.num_strings; i++) {
471 ret = krb5_string_to_enctype(context,
472 etype_str.strings[i],
475 errx(1, "unrecognized enctype: %s", etype_str.strings[i]);
477 krb5_get_init_creds_opt_set_etype_list(opt, enctype,
478 etype_str.num_strings);
481 if(use_keytab || keytab_str) {
484 ret = krb5_kt_resolve(context, keytab_str, &kt);
486 ret = krb5_kt_default(context, &kt);
488 krb5_err (context, 1, ret, "resolving keytab");
489 ret = krb5_get_init_creds_keytab (context,
496 krb5_kt_close(context, kt);
497 } else if (pk_user_id) {
498 ret = krb5_get_init_creds_password (context,
507 } else if (!interactive) {
508 krb5_warnx(context, "Not interactive, failed to get initial ticket");
509 krb5_get_init_creds_opt_free(context, opt);
513 if (passwd[0] == '\0') {
516 krb5_unparse_name (context, principal, &p);
517 asprintf (&prompt, "%s's Password: ", p);
520 if (UI_UTIL_read_pw_string(passwd, sizeof(passwd)-1, prompt, 0)){
521 memset(passwd, 0, sizeof(passwd));
528 ret = krb5_get_init_creds_password (context,
538 krb5_get_init_creds_opt_free(context, opt);
539 if (ntlm_domain && passwd[0])
540 heim_ntlm_nt_key(passwd, &ntlmkey);
541 memset(passwd, 0, sizeof(passwd));
546 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
548 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
549 case KRB5KRB_AP_ERR_MODIFIED:
550 case KRB5KDC_ERR_PREAUTH_FAILED:
551 krb5_errx(context, 1, "Password incorrect");
553 case KRB5KRB_AP_ERR_V4_REPLY:
554 krb5_errx(context, 1, "Looks like a Kerberos 4 reply");
557 krb5_err(context, 1, ret, "krb5_get_init_creds");
560 if(ticket_life != 0) {
561 if(abs(cred.times.endtime - cred.times.starttime - ticket_life) > 30) {
563 unparse_time_approx(cred.times.endtime - cred.times.starttime,
565 krb5_warnx(context, "NOTICE: ticket lifetime is %s", life);
569 if(abs(cred.times.renew_till - cred.times.starttime - renew) > 30) {
571 unparse_time_approx(cred.times.renew_till - cred.times.starttime,
573 krb5_warnx(context, "NOTICE: ticket renewable lifetime is %s",
578 ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, ccache),
581 krb5_err (context, 1, ret, "krb5_cc_new_unique");
583 ret = krb5_cc_initialize (context, tempccache, cred.client);
585 krb5_err (context, 1, ret, "krb5_cc_initialize");
587 ret = krb5_cc_store_cred (context, tempccache, &cred);
589 krb5_err (context, 1, ret, "krb5_cc_store_cred");
591 krb5_free_cred_contents (context, &cred);
593 ret = krb5_cc_move(context, tempccache, ccache);
595 krb5_err (context, 1, ret, "krb5_cc_move");
597 if (ntlm_domain && ntlmkey.data)
598 store_ntlmkey(context, ccache, ntlm_domain, &ntlmkey);
600 if (ok_as_delegate_flag || windows_flag) {
606 krb5_cc_set_config(context, ccache, NULL, "realm-config", &data);
617 ticket_lifetime(krb5_context context, krb5_ccache cache,
618 krb5_principal client, const char *server)
620 krb5_creds in_cred, *cred;
624 memset(&in_cred, 0, sizeof(in_cred));
626 ret = krb5_cc_get_principal(context, cache, &in_cred.client);
628 krb5_warn(context, ret, "krb5_cc_get_principal");
631 ret = get_server(context, in_cred.client, server, &in_cred.server);
633 krb5_free_principal(context, in_cred.client);
634 krb5_warn(context, ret, "get_server");
638 ret = krb5_get_credentials(context, KRB5_GC_CACHED,
639 cache, &in_cred, &cred);
640 krb5_free_principal(context, in_cred.client);
641 krb5_free_principal(context, in_cred.server);
643 krb5_warn(context, ret, "krb5_get_credentials");
646 timeout = cred->times.endtime - cred->times.starttime;
649 krb5_free_creds(context, cred);
654 krb5_context context;
656 krb5_principal principal;
657 krb5_deltat ticket_life;
661 renew_func(void *ptr)
663 struct renew_ctx *ctx = ptr;
668 if (renewable_flag) {
669 ret = renew_validate(ctx->context, renewable_flag, validate_flag,
670 ctx->ccache, server_str, ctx->ticket_life);
677 get_new_tickets(ctx->context, ctx->principal,
678 ctx->ccache, ctx->ticket_life, 0);
680 if(get_v4_tgt || convert_524)
681 do_524init(ctx->context, ctx->ccache, NULL, server_str);
682 if(do_afslog && k_hasafs())
683 krb5_afslog(ctx->context, ctx->ccache, NULL, NULL);
685 expire = ticket_lifetime(ctx->context, ctx->ccache, ctx->principal,
691 main (int argc, char **argv)
694 krb5_context context;
696 krb5_principal principal;
698 krb5_deltat ticket_life = 0;
701 setprogname (argv[0]);
703 ret = krb5_init_context (&context);
704 if (ret == KRB5_CONFIG_BADFORMAT)
705 errx (1, "krb5_init_context failed to parse configuration file");
707 errx(1, "krb5_init_context failed: %d", ret);
709 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
723 if (canonicalize_flag)
724 parseflags |= KRB5_PRINCIPAL_PARSE_ENTERPRISE;
727 ret = krb5_parse_name_flags (context, argv[0], parseflags, &principal);
729 krb5_err (context, 1, ret, "krb5_parse_name");
731 ret = krb5_get_default_principal (context, &principal);
733 krb5_err (context, 1, ret, "krb5_get_default_principal");
737 krb5_set_fcache_version(context, fcache_version);
739 if(renewable_flag == -1)
740 /* this seems somewhat pointless, but whatever */
741 krb5_appdefault_boolean(context, "kinit",
742 krb5_principal_get_realm(context, principal),
743 "renewable", FALSE, &renewable_flag);
745 krb5_appdefault_boolean(context, "kinit",
746 krb5_principal_get_realm(context, principal),
747 "krb4_get_tickets", FALSE, &get_v4_tgt);
749 krb5_appdefault_boolean(context, "kinit",
750 krb5_principal_get_realm(context, principal),
751 "afslog", TRUE, &do_afslog);
754 ret = krb5_cc_resolve(context, cred_cache, &ccache);
758 ret = krb5_cc_gen_new(context, &krb5_fcc_ops, &ccache);
760 krb5_err(context, 1, ret, "creating cred cache");
761 snprintf(s, sizeof(s), "%s:%s",
762 krb5_cc_get_type(context, ccache),
763 krb5_cc_get_name(context, ccache));
764 setenv("KRB5CCNAME", s, 1);
767 if (asprintf(&krb4_cc_name, "%s_XXXXXX", TKT_ROOT) < 0)
768 krb5_errx(context, 1, "out of memory");
769 if((fd = mkstemp(krb4_cc_name)) >= 0) {
771 setenv("KRBTKFILE", krb4_cc_name, 1);
778 ret = krb5_cc_cache_match(context, principal, NULL, &ccache);
780 ret = krb5_cc_default (context, &ccache);
784 krb5_err (context, 1, ret, "resolving credentials cache");
786 if(argc > 1 && k_hasafs ())
790 int tmp = parse_time (lifetime, "s");
792 errx (1, "unparsable time: %s", lifetime);
797 if(addrs_flag == 0 && extra_addresses.num_strings > 0)
798 krb5_errx(context, 1, "specifying both extra addresses and "
799 "no addresses makes no sense");
802 krb5_addresses addresses;
803 memset(&addresses, 0, sizeof(addresses));
804 for(i = 0; i < extra_addresses.num_strings; i++) {
805 ret = krb5_parse_address(context, extra_addresses.strings[i],
808 krb5_add_extra_addresses(context, &addresses);
809 krb5_free_addresses(context, &addresses);
812 free_getarg_strings(&extra_addresses);
815 if(renew_flag || validate_flag) {
816 ret = renew_validate(context, renew_flag, validate_flag,
817 ccache, server_str, ticket_life);
822 get_new_tickets(context, principal, ccache, ticket_life, 1);
824 if(get_v4_tgt || convert_524)
825 do_524init(context, ccache, NULL, server_str);
826 if(do_afslog && k_hasafs())
827 krb5_afslog(context, ccache, NULL, NULL);
829 struct renew_ctx ctx;
832 timeout = ticket_lifetime(context, ccache, principal, server_str) / 2;
834 ctx.context = context;
836 ctx.principal = principal;
837 ctx.ticket_life = ticket_life;
839 ret = simple_execvp_timed(argv[1], argv+1,
840 renew_func, &ctx, timeout);
841 #define EX_NOEXEC 126
842 #define EX_NOTFOUND 127
844 krb5_warnx(context, "permission denied: %s", argv[1]);
845 else if(ret == EX_NOTFOUND)
846 krb5_warnx(context, "command not found: %s", argv[1]);
848 krb5_cc_destroy(context, ccache);
849 _krb5_krb_dest_tkt(context, krb4_cc_name);
853 krb5_cc_close (context, ccache);
856 krb5_free_principal(context, principal);
857 krb5_free_context (context);