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
;
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 char *ntlm_domain
;
71 static char *krb4_cc_name
;
73 static struct getargs args
[] = {
83 { "524init", '4', arg_flag
, &get_v4_tgt
,
84 "obtain version 4 TGT" },
86 { "524convert", '9', arg_flag
, &convert_524
,
87 "only convert ticket to version 4" },
89 { "afslog", 0 , arg_flag
, &do_afslog
,
90 "obtain afs tokens" },
92 { "cache", 'c', arg_string
, &cred_cache
,
93 "credentials cache", "cachename" },
95 { "forwardable", 'f', arg_flag
, &forwardable_flag
,
96 "get forwardable tickets"},
98 { "keytab", 't', arg_string
, &keytab_str
,
99 "keytab to use", "keytabname" },
101 { "lifetime", 'l', arg_string
, &lifetime
,
102 "lifetime of tickets", "time"},
104 { "proxiable", 'p', arg_flag
, &proxiable_flag
,
105 "get proxiable tickets" },
107 { "renew", 'R', arg_flag
, &renew_flag
,
110 { "renewable", 0, arg_flag
, &renewable_flag
,
111 "get renewable tickets" },
113 { "renewable-life", 'r', arg_string
, &renew_life
,
114 "renewable lifetime of tickets", "time" },
116 { "server", 'S', arg_string
, &server_str
,
117 "server to get ticket for", "principal" },
119 { "start-time", 's', arg_string
, &start_str
,
120 "when ticket gets valid", "time" },
122 { "use-keytab", 'k', arg_flag
, &use_keytab
,
123 "get key from keytab" },
125 { "validate", 'v', arg_flag
, &validate_flag
,
128 { "enctypes", 'e', arg_strings
, &etype_str
,
129 "encryption types to use", "enctypes" },
131 { "fcache-version", 0, arg_integer
, &fcache_version
,
132 "file cache version to create" },
134 { "addresses", 'A', arg_negative_flag
, &addrs_flag
,
135 "request a ticket with no addresses" },
137 { "extra-addresses",'a', arg_strings
, &extra_addresses
,
138 "include these extra addresses", "addresses" },
140 { "anonymous", 0, arg_flag
, &anonymous_flag
,
141 "request an anonymous ticket" },
143 { "request-pac", 0, arg_flag
, &pac_flag
,
144 "request a Windows PAC" },
146 { "password-file", 0, arg_string
, &password_file
,
147 "read the password from a file" },
149 { "canonicalize",0, arg_flag
, &canonicalize_flag
,
150 "canonicalize client principal" },
152 { "pk-user", 'C', arg_string
, &pk_user_id
,
153 "principal's public/private/certificate identifier", "id" },
155 { "x509-anchors", 'D', arg_string
, &pk_x509_anchors
,
156 "directory with CA certificates", "directory" },
158 { "pk-use-enckey", 0, arg_flag
, &pk_use_enckey
,
159 "Use RSA encrypted reply (instead of DH)" },
161 { "ntlm-domain", 0, arg_string
, &ntlm_domain
,
162 "NTLM domain", "domain" },
164 { "version", 0, arg_flag
, &version_flag
},
165 { "help", 0, arg_flag
, &help_flag
}
171 arg_printusage (args
,
172 sizeof(args
)/sizeof(*args
),
174 "[principal [command]]");
178 static krb5_error_code
179 get_server(krb5_context context
,
180 krb5_principal client
,
182 krb5_principal
*princ
)
184 krb5_realm
*client_realm
;
186 return krb5_parse_name(context
, server
, princ
);
188 client_realm
= krb5_princ_realm (context
, client
);
189 return krb5_make_principal(context
, princ
, *client_realm
,
190 KRB5_TGS_NAME
, *client_realm
, NULL
);
193 static krb5_error_code
194 do_524init(krb5_context context
, krb5_ccache ccache
,
195 krb5_creds
*creds
, const char *server
)
199 struct credentials c
;
200 krb5_creds in_creds
, *real_creds
;
205 krb5_principal client
;
206 krb5_cc_get_principal(context
, ccache
, &client
);
207 memset(&in_creds
, 0, sizeof(in_creds
));
208 ret
= get_server(context
, client
, server
, &in_creds
.server
);
210 krb5_free_principal(context
, client
);
213 in_creds
.client
= client
;
214 ret
= krb5_get_credentials(context
, 0, ccache
, &in_creds
, &real_creds
);
215 krb5_free_principal(context
, client
);
216 krb5_free_principal(context
, in_creds
.server
);
220 ret
= krb524_convert_creds_kdc_ccache(context
, ccache
, real_creds
, &c
);
222 krb5_warn(context
, ret
, "converting creds");
224 krb5_error_code tret
= _krb5_krb_tf_setup(context
, &c
, NULL
, 0);
226 krb5_warn(context
, tret
, "saving v4 creds");
230 krb5_free_creds(context
, real_creds
);
231 memset(&c
, 0, sizeof(c
));
237 renew_validate(krb5_context context
,
245 krb5_creds in
, *out
= NULL
;
246 krb5_kdc_flags flags
;
248 memset(&in
, 0, sizeof(in
));
250 ret
= krb5_cc_get_principal(context
, cache
, &in
.client
);
252 krb5_warn(context
, ret
, "krb5_cc_get_principal");
255 ret
= get_server(context
, in
.client
, server
, &in
.server
);
257 krb5_warn(context
, ret
, "get_server");
263 * no need to check the error here, it's only to be
264 * friendly to the user
266 krb5_get_credentials(context
, KRB5_GC_CACHED
, cache
, &in
, &out
);
270 flags
.b
.renewable
= flags
.b
.renew
= renew
;
271 flags
.b
.validate
= validate
;
273 if (forwardable_flag
!= -1)
274 flags
.b
.forwardable
= forwardable_flag
;
276 flags
.b
.forwardable
= out
->flags
.b
.forwardable
;
278 if (proxiable_flag
!= -1)
279 flags
.b
.proxiable
= proxiable_flag
;
281 flags
.b
.proxiable
= out
->flags
.b
.proxiable
;
283 if (anonymous_flag
!= -1)
284 flags
.b
.request_anonymous
= anonymous_flag
;
286 in
.times
.endtime
= time(NULL
) + life
;
289 krb5_free_creds (context
, out
);
294 ret
= krb5_get_kdc_cred(context
,
302 krb5_warn(context
, ret
, "krb5_get_kdc_cred");
305 ret
= krb5_cc_initialize(context
, cache
, in
.client
);
307 krb5_free_creds (context
, out
);
308 krb5_warn(context
, ret
, "krb5_cc_initialize");
311 ret
= krb5_cc_store_cred(context
, cache
, out
);
313 if(ret
== 0 && server
== NULL
) {
314 /* only do this if it's a general renew-my-tgt request */
316 do_524init(context
, cache
, out
, NULL
);
317 if(do_afslog
&& k_hasafs())
318 krb5_afslog(context
, cache
, NULL
, NULL
);
321 krb5_free_creds (context
, out
);
323 krb5_warn(context
, ret
, "krb5_cc_store_cred");
327 krb5_free_cred_contents(context
, &in
);
331 static krb5_error_code
332 store_ntlmkey(krb5_context context
, krb5_ccache id
,
333 const char *domain
, krb5_const_principal client
,
334 struct ntlm_buf
*buf
)
339 memset(&cred
, 0, sizeof(cred
));
341 ret
= krb5_make_principal(context
, &cred
.server
,
342 krb5_principal_get_realm(context
, client
),
343 "@ntlm-key", domain
, NULL
);
346 ret
= krb5_copy_principal(context
, client
, &cred
.client
);
350 cred
.times
.authtime
= time(NULL
);
351 cred
.times
.endtime
= time(NULL
) + 3600 * 24 * 30; /* XXX */
352 cred
.session
.keytype
= ENCTYPE_ARCFOUR_HMAC_MD5
;
353 ret
= krb5_data_copy(&cred
.session
.keyvalue
, buf
->data
, buf
->length
);
357 ret
= krb5_cc_store_cred(context
, id
, &cred
);
360 krb5_free_cred_contents (context
, &cred
);
364 static krb5_error_code
365 get_new_tickets(krb5_context context
,
366 krb5_principal principal
,
368 krb5_deltat ticket_life
,
372 krb5_get_init_creds_opt
*opt
;
375 krb5_deltat start_time
= 0;
376 krb5_deltat renew
= 0;
377 char *renewstr
= NULL
;
378 krb5_enctype
*enctype
= NULL
;
379 struct ntlm_buf ntlmkey
;
380 krb5_ccache tempccache
;
382 memset(&ntlmkey
, 0, sizeof(ntlmkey
));
388 if (strcasecmp("STDIN", password_file
) == 0)
391 f
= fopen(password_file
, "r");
393 krb5_errx(context
, 1, "Failed to open the password file %s",
396 if (fgets(passwd
, sizeof(passwd
), f
) == NULL
)
397 krb5_errx(context
, 1,
398 "Failed to read password from file %s", password_file
);
401 passwd
[strcspn(passwd
, "\n")] = '\0';
405 memset(&cred
, 0, sizeof(cred
));
407 ret
= krb5_get_init_creds_opt_alloc (context
, &opt
);
409 krb5_err(context
, 1, ret
, "krb5_get_init_creds_opt_alloc");
411 krb5_get_init_creds_opt_set_default_flags(context
, "kinit",
412 krb5_principal_get_realm(context
, principal
), opt
);
414 if(forwardable_flag
!= -1)
415 krb5_get_init_creds_opt_set_forwardable (opt
, forwardable_flag
);
416 if(proxiable_flag
!= -1)
417 krb5_get_init_creds_opt_set_proxiable (opt
, proxiable_flag
);
418 if(anonymous_flag
!= -1)
419 krb5_get_init_creds_opt_set_anonymous (opt
, anonymous_flag
);
421 krb5_get_init_creds_opt_set_pac_request(context
, opt
,
422 pac_flag
? TRUE
: FALSE
);
423 if (canonicalize_flag
)
424 krb5_get_init_creds_opt_set_canonicalize(context
, opt
, TRUE
);
426 ret
= krb5_get_init_creds_opt_set_pkinit(context
, opt
,
432 pk_use_enckey
? 2 : 0,
437 krb5_err(context
, 1, ret
, "krb5_get_init_creds_opt_set_pkinit");
440 if (addrs_flag
!= -1)
441 krb5_get_init_creds_opt_set_addressless(context
, opt
,
442 addrs_flag
? FALSE
: TRUE
);
444 if (renew_life
== NULL
&& renewable_flag
)
445 renewstr
= "1 month";
447 renewstr
= renew_life
;
449 renew
= parse_time (renewstr
, "s");
451 errx (1, "unparsable time: %s", renewstr
);
453 krb5_get_init_creds_opt_set_renew_life (opt
, renew
);
457 krb5_get_init_creds_opt_set_tkt_life (opt
, ticket_life
);
460 int tmp
= parse_time (start_str
, "s");
462 errx (1, "unparsable time: %s", start_str
);
467 if(etype_str
.num_strings
) {
470 enctype
= malloc(etype_str
.num_strings
* sizeof(*enctype
));
472 errx(1, "out of memory");
473 for(i
= 0; i
< etype_str
.num_strings
; i
++) {
474 ret
= krb5_string_to_enctype(context
,
475 etype_str
.strings
[i
],
478 errx(1, "unrecognized enctype: %s", etype_str
.strings
[i
]);
480 krb5_get_init_creds_opt_set_etype_list(opt
, enctype
,
481 etype_str
.num_strings
);
484 if(use_keytab
|| keytab_str
) {
487 ret
= krb5_kt_resolve(context
, keytab_str
, &kt
);
489 ret
= krb5_kt_default(context
, &kt
);
491 krb5_err (context
, 1, ret
, "resolving keytab");
492 ret
= krb5_get_init_creds_keytab (context
,
499 krb5_kt_close(context
, kt
);
500 } else if (pk_user_id
) {
501 ret
= krb5_get_init_creds_password (context
,
510 } else if (!interactive
) {
511 krb5_warnx(context
, "Not interactive, failed to get initial ticket");
512 krb5_get_init_creds_opt_free(context
, opt
);
516 if (passwd
[0] == '\0') {
519 krb5_unparse_name (context
, principal
, &p
);
520 asprintf (&prompt
, "%s's Password: ", p
);
523 if (UI_UTIL_read_pw_string(passwd
, sizeof(passwd
)-1, prompt
, 0)){
524 memset(passwd
, 0, sizeof(passwd
));
531 ret
= krb5_get_init_creds_password (context
,
541 krb5_get_init_creds_opt_free(context
, opt
);
542 if (ntlm_domain
&& passwd
[0])
543 heim_ntlm_nt_key(passwd
, &ntlmkey
);
544 memset(passwd
, 0, sizeof(passwd
));
549 case KRB5_LIBOS_PWDINTR
: /* don't print anything if it was just C-c:ed */
551 case KRB5KRB_AP_ERR_BAD_INTEGRITY
:
552 case KRB5KRB_AP_ERR_MODIFIED
:
553 case KRB5KDC_ERR_PREAUTH_FAILED
:
554 krb5_errx(context
, 1, "Password incorrect");
556 case KRB5KRB_AP_ERR_V4_REPLY
:
557 krb5_errx(context
, 1, "Looks like a Kerberos 4 reply");
560 krb5_err(context
, 1, ret
, "krb5_get_init_creds");
563 if(ticket_life
!= 0) {
564 if(abs(cred
.times
.endtime
- cred
.times
.starttime
- ticket_life
) > 30) {
566 unparse_time_approx(cred
.times
.endtime
- cred
.times
.starttime
,
568 krb5_warnx(context
, "NOTICE: ticket lifetime is %s", life
);
572 if(abs(cred
.times
.renew_till
- cred
.times
.starttime
- renew
) > 30) {
574 unparse_time_approx(cred
.times
.renew_till
- cred
.times
.starttime
,
576 krb5_warnx(context
, "NOTICE: ticket renewable lifetime is %s",
581 ret
= krb5_cc_new_unique(context
, krb5_cc_get_type(context
, ccache
),
584 krb5_err (context
, 1, ret
, "krb5_cc_new_unique");
586 ret
= krb5_cc_initialize (context
, tempccache
, cred
.client
);
588 krb5_err (context
, 1, ret
, "krb5_cc_initialize");
590 ret
= krb5_cc_store_cred (context
, tempccache
, &cred
);
592 krb5_err (context
, 1, ret
, "krb5_cc_store_cred");
594 krb5_free_cred_contents (context
, &cred
);
596 ret
= krb5_cc_move(context
, tempccache
, ccache
);
598 krb5_err (context
, 1, ret
, "krb5_cc_move");
600 if (ntlm_domain
&& ntlmkey
.data
)
601 store_ntlmkey(context
, ccache
, ntlm_domain
, principal
, &ntlmkey
);
610 ticket_lifetime(krb5_context context
, krb5_ccache cache
,
611 krb5_principal client
, const char *server
)
613 krb5_creds in_cred
, *cred
;
617 memset(&in_cred
, 0, sizeof(in_cred
));
619 ret
= krb5_cc_get_principal(context
, cache
, &in_cred
.client
);
621 krb5_warn(context
, ret
, "krb5_cc_get_principal");
624 ret
= get_server(context
, in_cred
.client
, server
, &in_cred
.server
);
626 krb5_free_principal(context
, in_cred
.client
);
627 krb5_warn(context
, ret
, "get_server");
631 ret
= krb5_get_credentials(context
, KRB5_GC_CACHED
,
632 cache
, &in_cred
, &cred
);
633 krb5_free_principal(context
, in_cred
.client
);
634 krb5_free_principal(context
, in_cred
.server
);
636 krb5_warn(context
, ret
, "krb5_get_credentials");
639 timeout
= cred
->times
.endtime
- cred
->times
.starttime
;
642 krb5_free_creds(context
, cred
);
647 krb5_context context
;
649 krb5_principal principal
;
650 krb5_deltat ticket_life
;
654 renew_func(void *ptr
)
656 struct renew_ctx
*ctx
= ptr
;
661 if (renewable_flag
) {
662 ret
= renew_validate(ctx
->context
, renewable_flag
, validate_flag
,
663 ctx
->ccache
, server_str
, ctx
->ticket_life
);
670 get_new_tickets(ctx
->context
, ctx
->principal
,
671 ctx
->ccache
, ctx
->ticket_life
, 0);
673 if(get_v4_tgt
|| convert_524
)
674 do_524init(ctx
->context
, ctx
->ccache
, NULL
, server_str
);
675 if(do_afslog
&& k_hasafs())
676 krb5_afslog(ctx
->context
, ctx
->ccache
, NULL
, NULL
);
678 expire
= ticket_lifetime(ctx
->context
, ctx
->ccache
, ctx
->principal
,
684 main (int argc
, char **argv
)
687 krb5_context context
;
689 krb5_principal principal
;
691 krb5_deltat ticket_life
= 0;
694 setprogname (argv
[0]);
696 ret
= krb5_init_context (&context
);
697 if (ret
== KRB5_CONFIG_BADFORMAT
)
698 errx (1, "krb5_init_context failed to parse configuration file");
700 errx(1, "krb5_init_context failed: %d", ret
);
702 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
716 if (canonicalize_flag
)
717 parseflags
|= KRB5_PRINCIPAL_PARSE_ENTERPRISE
;
720 ret
= krb5_parse_name_flags (context
, argv
[0], parseflags
, &principal
);
722 krb5_err (context
, 1, ret
, "krb5_parse_name");
724 ret
= krb5_get_default_principal (context
, &principal
);
726 krb5_err (context
, 1, ret
, "krb5_get_default_principal");
730 krb5_set_fcache_version(context
, fcache_version
);
732 if(renewable_flag
== -1)
733 /* this seems somewhat pointless, but whatever */
734 krb5_appdefault_boolean(context
, "kinit",
735 krb5_principal_get_realm(context
, principal
),
736 "renewable", FALSE
, &renewable_flag
);
738 krb5_appdefault_boolean(context
, "kinit",
739 krb5_principal_get_realm(context
, principal
),
740 "krb4_get_tickets", FALSE
, &get_v4_tgt
);
742 krb5_appdefault_boolean(context
, "kinit",
743 krb5_principal_get_realm(context
, principal
),
744 "afslog", TRUE
, &do_afslog
);
747 ret
= krb5_cc_resolve(context
, cred_cache
, &ccache
);
751 ret
= krb5_cc_gen_new(context
, &krb5_fcc_ops
, &ccache
);
753 krb5_err(context
, 1, ret
, "creating cred cache");
754 snprintf(s
, sizeof(s
), "%s:%s",
755 krb5_cc_get_type(context
, ccache
),
756 krb5_cc_get_name(context
, ccache
));
757 setenv("KRB5CCNAME", s
, 1);
760 if (asprintf(&krb4_cc_name
, "%s_XXXXXX", TKT_ROOT
) < 0)
761 krb5_errx(context
, 1, "out of memory");
762 if((fd
= mkstemp(krb4_cc_name
)) >= 0) {
764 setenv("KRBTKFILE", krb4_cc_name
, 1);
771 ret
= krb5_cc_cache_match(context
, principal
, NULL
, &ccache
);
773 ret
= krb5_cc_default (context
, &ccache
);
777 krb5_err (context
, 1, ret
, "resolving credentials cache");
779 if(argc
> 1 && k_hasafs ())
783 int tmp
= parse_time (lifetime
, "s");
785 errx (1, "unparsable time: %s", lifetime
);
790 if(addrs_flag
== 0 && extra_addresses
.num_strings
> 0)
791 krb5_errx(context
, 1, "specifying both extra addresses and "
792 "no addresses makes no sense");
795 krb5_addresses addresses
;
796 memset(&addresses
, 0, sizeof(addresses
));
797 for(i
= 0; i
< extra_addresses
.num_strings
; i
++) {
798 ret
= krb5_parse_address(context
, extra_addresses
.strings
[i
],
801 krb5_add_extra_addresses(context
, &addresses
);
802 krb5_free_addresses(context
, &addresses
);
805 free_getarg_strings(&extra_addresses
);
808 if(renew_flag
|| validate_flag
) {
809 ret
= renew_validate(context
, renew_flag
, validate_flag
,
810 ccache
, server_str
, ticket_life
);
815 get_new_tickets(context
, principal
, ccache
, ticket_life
, 1);
817 if(get_v4_tgt
|| convert_524
)
818 do_524init(context
, ccache
, NULL
, server_str
);
819 if(do_afslog
&& k_hasafs())
820 krb5_afslog(context
, ccache
, NULL
, NULL
);
822 struct renew_ctx ctx
;
825 timeout
= ticket_lifetime(context
, ccache
, principal
, server_str
) / 2;
827 ctx
.context
= context
;
829 ctx
.principal
= principal
;
830 ctx
.ticket_life
= ticket_life
;
832 ret
= simple_execvp_timed(argv
[1], argv
+1,
833 renew_func
, &ctx
, timeout
);
834 #define EX_NOEXEC 126
835 #define EX_NOTFOUND 127
837 krb5_warnx(context
, "permission denied: %s", argv
[1]);
838 else if(ret
== EX_NOTFOUND
)
839 krb5_warnx(context
, "command not found: %s", argv
[1]);
841 krb5_cc_destroy(context
, ccache
);
842 _krb5_krb_dest_tkt(context
, krb4_cc_name
);
846 krb5_cc_close (context
, ccache
);
849 krb5_free_principal(context
, principal
);
850 krb5_free_context (context
);