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("$Heimdal: kinit.c 22116 2007-12-03 21:22:58Z lha $"
38 #include "krb5-v4compat.h"
42 int forwardable_flag
= -1;
43 int proxiable_flag
= -1;
44 int renewable_flag
= -1;
47 int validate_flag
= 0;
51 struct getarg_strings extra_addresses
;
52 int anonymous_flag
= 0;
53 char *lifetime
= NULL
;
54 char *renew_life
= NULL
;
55 char *server_str
= NULL
;
56 char *cred_cache
= NULL
;
57 char *start_str
= NULL
;
58 struct getarg_strings etype_str
;
60 char *keytab_str
= NULL
;
65 char *password_file
= NULL
;
66 char *pk_user_id
= NULL
;
67 char *pk_x509_anchors
= NULL
;
68 int pk_use_enckey
= 0;
69 static int canonicalize_flag
= 0;
70 static char *ntlm_domain
;
72 static char *krb4_cc_name
;
74 static struct getargs args
[] = {
84 { "524init", '4', arg_flag
, &get_v4_tgt
,
85 "obtain version 4 TGT" },
87 { "524convert", '9', arg_flag
, &convert_524
,
88 "only convert ticket to version 4" },
90 { "afslog", 0 , arg_flag
, &do_afslog
,
91 "obtain afs tokens" },
93 { "cache", 'c', arg_string
, &cred_cache
,
94 "credentials cache", "cachename" },
96 { "forwardable", 'f', arg_flag
, &forwardable_flag
,
97 "get forwardable tickets"},
99 { "keytab", 't', arg_string
, &keytab_str
,
100 "keytab to use", "keytabname" },
102 { "lifetime", 'l', arg_string
, &lifetime
,
103 "lifetime of tickets", "time"},
105 { "proxiable", 'p', arg_flag
, &proxiable_flag
,
106 "get proxiable tickets" },
108 { "renew", 'R', arg_flag
, &renew_flag
,
111 { "renewable", 0, arg_flag
, &renewable_flag
,
112 "get renewable tickets" },
114 { "renewable-life", 'r', arg_string
, &renew_life
,
115 "renewable lifetime of tickets", "time" },
117 { "server", 'S', arg_string
, &server_str
,
118 "server to get ticket for", "principal" },
120 { "start-time", 's', arg_string
, &start_str
,
121 "when ticket gets valid", "time" },
123 { "use-keytab", 'k', arg_flag
, &use_keytab
,
124 "get key from keytab" },
126 { "validate", 'v', arg_flag
, &validate_flag
,
129 { "enctypes", 'e', arg_strings
, &etype_str
,
130 "encryption types to use", "enctypes" },
132 { "fcache-version", 0, arg_integer
, &fcache_version
,
133 "file cache version to create" },
135 { "addresses", 'A', arg_negative_flag
, &addrs_flag
,
136 "request a ticket with no addresses" },
138 { "extra-addresses",'a', arg_strings
, &extra_addresses
,
139 "include these extra addresses", "addresses" },
141 { "anonymous", 0, arg_flag
, &anonymous_flag
,
142 "request an anonymous ticket" },
144 { "request-pac", 0, arg_flag
, &pac_flag
,
145 "request a Windows PAC" },
147 { "password-file", 0, arg_string
, &password_file
,
148 "read the password from a file" },
150 { "canonicalize",0, arg_flag
, &canonicalize_flag
,
151 "canonicalize client principal" },
153 { "pk-user", 'C', arg_string
, &pk_user_id
,
154 "principal's public/private/certificate identifier", "id" },
156 { "x509-anchors", 'D', arg_string
, &pk_x509_anchors
,
157 "directory with CA certificates", "directory" },
159 { "pk-use-enckey", 0, arg_flag
, &pk_use_enckey
,
160 "Use RSA encrypted reply (instead of DH)" },
162 { "ntlm-domain", 0, arg_string
, &ntlm_domain
,
163 "NTLM domain", "domain" },
165 { "version", 0, arg_flag
, &version_flag
},
166 { "help", 0, arg_flag
, &help_flag
}
172 arg_printusage (args
,
173 sizeof(args
)/sizeof(*args
),
175 "[principal [command]]");
179 static krb5_error_code
180 get_server(krb5_context context
,
181 krb5_principal client
,
183 krb5_principal
*princ
)
185 krb5_realm
*client_realm
;
187 return krb5_parse_name(context
, server
, princ
);
189 client_realm
= krb5_princ_realm (context
, client
);
190 return krb5_make_principal(context
, princ
, *client_realm
,
191 KRB5_TGS_NAME
, *client_realm
, NULL
);
194 static krb5_error_code
195 do_524init(krb5_context context
, krb5_ccache ccache
,
196 krb5_creds
*creds
, const char *server
)
200 struct credentials c
;
201 krb5_creds in_creds
, *real_creds
;
206 krb5_principal client
;
207 krb5_cc_get_principal(context
, ccache
, &client
);
208 memset(&in_creds
, 0, sizeof(in_creds
));
209 ret
= get_server(context
, client
, server
, &in_creds
.server
);
211 krb5_free_principal(context
, client
);
214 in_creds
.client
= client
;
215 ret
= krb5_get_credentials(context
, 0, ccache
, &in_creds
, &real_creds
);
216 krb5_free_principal(context
, client
);
217 krb5_free_principal(context
, in_creds
.server
);
221 ret
= krb524_convert_creds_kdc_ccache(context
, ccache
, real_creds
, &c
);
223 krb5_warn(context
, ret
, "converting creds");
225 krb5_error_code tret
= _krb5_krb_tf_setup(context
, &c
, NULL
, 0);
227 krb5_warn(context
, tret
, "saving v4 creds");
231 krb5_free_creds(context
, real_creds
);
232 memset(&c
, 0, sizeof(c
));
238 renew_validate(krb5_context context
,
246 krb5_creds in
, *out
= NULL
;
247 krb5_kdc_flags flags
;
249 memset(&in
, 0, sizeof(in
));
251 ret
= krb5_cc_get_principal(context
, cache
, &in
.client
);
253 krb5_warn(context
, ret
, "krb5_cc_get_principal");
256 ret
= get_server(context
, in
.client
, server
, &in
.server
);
258 krb5_warn(context
, ret
, "get_server");
264 * no need to check the error here, it's only to be
265 * friendly to the user
267 krb5_get_credentials(context
, KRB5_GC_CACHED
, cache
, &in
, &out
);
271 flags
.b
.renewable
= flags
.b
.renew
= renew
;
272 flags
.b
.validate
= validate
;
274 if (forwardable_flag
!= -1)
275 flags
.b
.forwardable
= forwardable_flag
;
277 flags
.b
.forwardable
= out
->flags
.b
.forwardable
;
279 if (proxiable_flag
!= -1)
280 flags
.b
.proxiable
= proxiable_flag
;
282 flags
.b
.proxiable
= out
->flags
.b
.proxiable
;
284 if (anonymous_flag
!= -1)
285 flags
.b
.request_anonymous
= anonymous_flag
;
287 in
.times
.endtime
= time(NULL
) + life
;
290 krb5_free_creds (context
, out
);
295 ret
= krb5_get_kdc_cred(context
,
303 krb5_warn(context
, ret
, "krb5_get_kdc_cred");
306 ret
= krb5_cc_initialize(context
, cache
, in
.client
);
308 krb5_free_creds (context
, out
);
309 krb5_warn(context
, ret
, "krb5_cc_initialize");
312 ret
= krb5_cc_store_cred(context
, cache
, out
);
314 if(ret
== 0 && server
== NULL
) {
315 /* only do this if it's a general renew-my-tgt request */
317 do_524init(context
, cache
, out
, NULL
);
318 if(do_afslog
&& k_hasafs())
319 krb5_afslog(context
, cache
, NULL
, NULL
);
322 krb5_free_creds (context
, out
);
324 krb5_warn(context
, ret
, "krb5_cc_store_cred");
328 krb5_free_cred_contents(context
, &in
);
332 static krb5_error_code
333 store_ntlmkey(krb5_context context
, krb5_ccache id
,
334 const char *domain
, krb5_const_principal client
,
335 struct ntlm_buf
*buf
)
340 memset(&cred
, 0, sizeof(cred
));
342 ret
= krb5_make_principal(context
, &cred
.server
,
343 krb5_principal_get_realm(context
, client
),
344 "@ntlm-key", domain
, NULL
);
347 ret
= krb5_copy_principal(context
, client
, &cred
.client
);
351 cred
.times
.authtime
= time(NULL
);
352 cred
.times
.endtime
= time(NULL
) + 3600 * 24 * 30; /* XXX */
353 cred
.session
.keytype
= ENCTYPE_ARCFOUR_HMAC_MD5
;
354 ret
= krb5_data_copy(&cred
.session
.keyvalue
, buf
->data
, buf
->length
);
358 ret
= krb5_cc_store_cred(context
, id
, &cred
);
361 krb5_free_cred_contents (context
, &cred
);
365 static krb5_error_code
366 get_new_tickets(krb5_context context
,
367 krb5_principal principal
,
369 krb5_deltat ticket_life
,
373 krb5_get_init_creds_opt
*opt
;
376 krb5_deltat start_time
= 0;
377 krb5_deltat renew
= 0;
378 char *renewstr
= NULL
;
379 krb5_enctype
*enctype
= NULL
;
380 struct ntlm_buf ntlmkey
;
381 krb5_ccache tempccache
;
383 memset(&ntlmkey
, 0, sizeof(ntlmkey
));
389 if (strcasecmp("STDIN", password_file
) == 0)
392 f
= fopen(password_file
, "r");
394 krb5_errx(context
, 1, "Failed to open the password file %s",
397 if (fgets(passwd
, sizeof(passwd
), f
) == NULL
)
398 krb5_errx(context
, 1,
399 "Failed to read password from file %s", password_file
);
402 passwd
[strcspn(passwd
, "\n")] = '\0';
406 memset(&cred
, 0, sizeof(cred
));
408 ret
= krb5_get_init_creds_opt_alloc (context
, &opt
);
410 krb5_err(context
, 1, ret
, "krb5_get_init_creds_opt_alloc");
412 krb5_get_init_creds_opt_set_default_flags(context
, "kinit",
413 krb5_principal_get_realm(context
, principal
), opt
);
415 if(forwardable_flag
!= -1)
416 krb5_get_init_creds_opt_set_forwardable (opt
, forwardable_flag
);
417 if(proxiable_flag
!= -1)
418 krb5_get_init_creds_opt_set_proxiable (opt
, proxiable_flag
);
419 if(anonymous_flag
!= -1)
420 krb5_get_init_creds_opt_set_anonymous (opt
, anonymous_flag
);
422 krb5_get_init_creds_opt_set_pac_request(context
, opt
,
423 pac_flag
? TRUE
: FALSE
);
424 if (canonicalize_flag
)
425 krb5_get_init_creds_opt_set_canonicalize(context
, opt
, TRUE
);
427 ret
= krb5_get_init_creds_opt_set_pkinit(context
, opt
,
433 pk_use_enckey
? 2 : 0,
438 krb5_err(context
, 1, ret
, "krb5_get_init_creds_opt_set_pkinit");
441 if (addrs_flag
!= -1)
442 krb5_get_init_creds_opt_set_addressless(context
, opt
,
443 addrs_flag
? FALSE
: TRUE
);
445 if (renew_life
== NULL
&& renewable_flag
)
446 renewstr
= "1 month";
448 renewstr
= renew_life
;
450 renew
= parse_time (renewstr
, "s");
452 errx (1, "unparsable time: %s", renewstr
);
454 krb5_get_init_creds_opt_set_renew_life (opt
, renew
);
458 krb5_get_init_creds_opt_set_tkt_life (opt
, ticket_life
);
461 int tmp
= parse_time (start_str
, "s");
463 errx (1, "unparsable time: %s", start_str
);
468 if(etype_str
.num_strings
) {
471 enctype
= malloc(etype_str
.num_strings
* sizeof(*enctype
));
473 errx(1, "out of memory");
474 for(i
= 0; i
< etype_str
.num_strings
; i
++) {
475 ret
= krb5_string_to_enctype(context
,
476 etype_str
.strings
[i
],
479 errx(1, "unrecognized enctype: %s", etype_str
.strings
[i
]);
481 krb5_get_init_creds_opt_set_etype_list(opt
, enctype
,
482 etype_str
.num_strings
);
485 if(use_keytab
|| keytab_str
) {
488 ret
= krb5_kt_resolve(context
, keytab_str
, &kt
);
490 ret
= krb5_kt_default(context
, &kt
);
492 krb5_err (context
, 1, ret
, "resolving keytab");
493 ret
= krb5_get_init_creds_keytab (context
,
500 krb5_kt_close(context
, kt
);
501 } else if (pk_user_id
) {
502 ret
= krb5_get_init_creds_password (context
,
511 } else if (!interactive
) {
512 krb5_warnx(context
, "Not interactive, failed to get initial ticket");
513 krb5_get_init_creds_opt_free(context
, opt
);
517 if (passwd
[0] == '\0') {
520 krb5_unparse_name (context
, principal
, &p
);
521 asprintf (&prompt
, "%s's Password: ", p
);
524 if (UI_UTIL_read_pw_string(passwd
, sizeof(passwd
)-1, prompt
, 0)){
525 memset(passwd
, 0, sizeof(passwd
));
532 ret
= krb5_get_init_creds_password (context
,
542 krb5_get_init_creds_opt_free(context
, opt
);
543 if (ntlm_domain
&& passwd
[0])
544 heim_ntlm_nt_key(passwd
, &ntlmkey
);
545 memset(passwd
, 0, sizeof(passwd
));
550 case KRB5_LIBOS_PWDINTR
: /* don't print anything if it was just C-c:ed */
552 case KRB5KRB_AP_ERR_BAD_INTEGRITY
:
553 case KRB5KRB_AP_ERR_MODIFIED
:
554 case KRB5KDC_ERR_PREAUTH_FAILED
:
555 krb5_errx(context
, 1, "Password incorrect");
557 case KRB5KRB_AP_ERR_V4_REPLY
:
558 krb5_errx(context
, 1, "Looks like a Kerberos 4 reply");
561 krb5_err(context
, 1, ret
, "krb5_get_init_creds");
564 if(ticket_life
!= 0) {
565 if(abs(cred
.times
.endtime
- cred
.times
.starttime
- ticket_life
) > 30) {
567 unparse_time_approx(cred
.times
.endtime
- cred
.times
.starttime
,
569 krb5_warnx(context
, "NOTICE: ticket lifetime is %s", life
);
573 if(abs(cred
.times
.renew_till
- cred
.times
.starttime
- renew
) > 30) {
575 unparse_time_approx(cred
.times
.renew_till
- cred
.times
.starttime
,
577 krb5_warnx(context
, "NOTICE: ticket renewable lifetime is %s",
582 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, ccache
),
585 krb5_err (context
, 1, ret
, "krb5_cc_new_unique");
587 ret
= krb5_cc_initialize (context
, tempccache
, cred
.client
);
589 krb5_err (context
, 1, ret
, "krb5_cc_initialize");
591 ret
= krb5_cc_store_cred (context
, tempccache
, &cred
);
593 krb5_err (context
, 1, ret
, "krb5_cc_store_cred");
595 krb5_free_cred_contents (context
, &cred
);
597 ret
= krb5_cc_move(context
, tempccache
, ccache
);
599 krb5_err (context
, 1, ret
, "krb5_cc_move");
601 if (ntlm_domain
&& ntlmkey
.data
)
602 store_ntlmkey(context
, ccache
, ntlm_domain
, principal
, &ntlmkey
);
611 ticket_lifetime(krb5_context context
, krb5_ccache cache
,
612 krb5_principal client
, const char *server
)
614 krb5_creds in_cred
, *cred
;
618 memset(&in_cred
, 0, sizeof(in_cred
));
620 ret
= krb5_cc_get_principal(context
, cache
, &in_cred
.client
);
622 krb5_warn(context
, ret
, "krb5_cc_get_principal");
625 ret
= get_server(context
, in_cred
.client
, server
, &in_cred
.server
);
627 krb5_free_principal(context
, in_cred
.client
);
628 krb5_warn(context
, ret
, "get_server");
632 ret
= krb5_get_credentials(context
, KRB5_GC_CACHED
,
633 cache
, &in_cred
, &cred
);
634 krb5_free_principal(context
, in_cred
.client
);
635 krb5_free_principal(context
, in_cred
.server
);
637 krb5_warn(context
, ret
, "krb5_get_credentials");
640 timeout
= cred
->times
.endtime
- cred
->times
.starttime
;
643 krb5_free_creds(context
, cred
);
648 krb5_context context
;
650 krb5_principal principal
;
651 krb5_deltat ticket_life
;
655 renew_func(void *ptr
)
657 struct renew_ctx
*ctx
= ptr
;
662 if (renewable_flag
) {
663 ret
= renew_validate(ctx
->context
, renewable_flag
, validate_flag
,
664 ctx
->ccache
, server_str
, ctx
->ticket_life
);
671 get_new_tickets(ctx
->context
, ctx
->principal
,
672 ctx
->ccache
, ctx
->ticket_life
, 0);
674 if(get_v4_tgt
|| convert_524
)
675 do_524init(ctx
->context
, ctx
->ccache
, NULL
, server_str
);
676 if(do_afslog
&& k_hasafs())
677 krb5_afslog(ctx
->context
, ctx
->ccache
, NULL
, NULL
);
679 expire
= ticket_lifetime(ctx
->context
, ctx
->ccache
, ctx
->principal
,
685 main (int argc
, char **argv
)
688 krb5_context context
;
690 krb5_principal principal
;
692 krb5_deltat ticket_life
= 0;
695 setprogname (argv
[0]);
697 ret
= krb5_init_context (&context
);
698 if (ret
== KRB5_CONFIG_BADFORMAT
)
699 errx (1, "krb5_init_context failed to parse configuration file");
701 errx(1, "krb5_init_context failed: %d", ret
);
703 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
717 if (canonicalize_flag
)
718 parseflags
|= KRB5_PRINCIPAL_PARSE_ENTERPRISE
;
721 ret
= krb5_parse_name_flags (context
, argv
[0], parseflags
, &principal
);
723 krb5_err (context
, 1, ret
, "krb5_parse_name");
725 ret
= krb5_get_default_principal (context
, &principal
);
727 krb5_err (context
, 1, ret
, "krb5_get_default_principal");
731 krb5_set_fcache_version(context
, fcache_version
);
733 if(renewable_flag
== -1)
734 /* this seems somewhat pointless, but whatever */
735 krb5_appdefault_boolean(context
, "kinit",
736 krb5_principal_get_realm(context
, principal
),
737 "renewable", FALSE
, &renewable_flag
);
739 krb5_appdefault_boolean(context
, "kinit",
740 krb5_principal_get_realm(context
, principal
),
741 "krb4_get_tickets", FALSE
, &get_v4_tgt
);
743 krb5_appdefault_boolean(context
, "kinit",
744 krb5_principal_get_realm(context
, principal
),
745 "afslog", TRUE
, &do_afslog
);
748 ret
= krb5_cc_resolve(context
, cred_cache
, &ccache
);
752 ret
= krb5_cc_gen_new(context
, &krb5_fcc_ops
, &ccache
);
754 krb5_err(context
, 1, ret
, "creating cred cache");
755 snprintf(s
, sizeof(s
), "%s:%s",
756 krb5_cc_get_type(context
, ccache
),
757 krb5_cc_get_name(context
, ccache
));
758 setenv("KRB5CCNAME", s
, 1);
761 if (asprintf(&krb4_cc_name
, "%s_XXXXXX", TKT_ROOT
) < 0)
762 krb5_errx(context
, 1, "out of memory");
763 if((fd
= mkstemp(krb4_cc_name
)) >= 0) {
765 setenv("KRBTKFILE", krb4_cc_name
, 1);
772 ret
= krb5_cc_cache_match(context
, principal
, NULL
, &ccache
);
774 ret
= krb5_cc_default (context
, &ccache
);
778 krb5_err (context
, 1, ret
, "resolving credentials cache");
780 if(argc
> 1 && k_hasafs ())
784 int tmp
= parse_time (lifetime
, "s");
786 errx (1, "unparsable time: %s", lifetime
);
791 if(addrs_flag
== 0 && extra_addresses
.num_strings
> 0)
792 krb5_errx(context
, 1, "specifying both extra addresses and "
793 "no addresses makes no sense");
796 krb5_addresses addresses
;
797 memset(&addresses
, 0, sizeof(addresses
));
798 for(i
= 0; i
< extra_addresses
.num_strings
; i
++) {
799 ret
= krb5_parse_address(context
, extra_addresses
.strings
[i
],
802 krb5_add_extra_addresses(context
, &addresses
);
803 krb5_free_addresses(context
, &addresses
);
806 free_getarg_strings(&extra_addresses
);
809 if(renew_flag
|| validate_flag
) {
810 ret
= renew_validate(context
, renew_flag
, validate_flag
,
811 ccache
, server_str
, ticket_life
);
816 get_new_tickets(context
, principal
, ccache
, ticket_life
, 1);
818 if(get_v4_tgt
|| convert_524
)
819 do_524init(context
, ccache
, NULL
, server_str
);
820 if(do_afslog
&& k_hasafs())
821 krb5_afslog(context
, ccache
, NULL
, NULL
);
823 struct renew_ctx ctx
;
826 timeout
= ticket_lifetime(context
, ccache
, principal
, server_str
) / 2;
828 ctx
.context
= context
;
830 ctx
.principal
= principal
;
831 ctx
.ticket_life
= ticket_life
;
833 ret
= simple_execvp_timed(argv
[1], argv
+1,
834 renew_func
, &ctx
, timeout
);
835 #define EX_NOEXEC 126
836 #define EX_NOTFOUND 127
838 krb5_warnx(context
, "permission denied: %s", argv
[1]);
839 else if(ret
== EX_NOTFOUND
)
840 krb5_warnx(context
, "command not found: %s", argv
[1]);
842 krb5_cc_destroy(context
, ccache
);
843 _krb5_krb_dest_tkt(context
, krb4_cc_name
);
847 krb5_cc_close (context
, ccache
);
850 krb5_free_principal(context
, principal
);
851 krb5_free_context (context
);