more text about TheseCells and ThisCell, from Harald Barth
[heimdal.git] / kuser / kinit.c
blob5640c3eba3abfc7d02c67464ec03e4ad17e95bb4
1 /*
2 * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "kuser_locl.h"
36 #ifndef HEIMDAL_SMALLER
37 #include "krb5-v4compat.h"
38 #endif
40 struct krb5_dh_moduli;
41 struct AlgorithmIdentifier;
42 struct _krb5_krb_auth_data;
43 struct hx509_certs_data;
44 #include <krb5-private.h>
46 #ifndef NO_NTLM
47 #include "heimntlm.h"
48 #endif
50 int forwardable_flag = -1;
51 int proxiable_flag = -1;
52 int renewable_flag = -1;
53 int renew_flag = 0;
54 int pac_flag = -1;
55 int validate_flag = 0;
56 int version_flag = 0;
57 int help_flag = 0;
58 int addrs_flag = -1;
59 struct getarg_strings extra_addresses;
60 int anonymous_flag = 0;
61 char *lifetime = NULL;
62 char *renew_life = NULL;
63 char *server_str = NULL;
64 char *cred_cache = NULL;
65 char *start_str = NULL;
66 static int switch_cache_flags = 1;
67 struct getarg_strings etype_str;
68 int use_keytab = 0;
69 char *keytab_str = NULL;
70 int do_afslog = -1;
71 #ifndef HEIMDAL_SMALLER
72 int get_v4_tgt = -1;
73 int convert_524 = 0;
74 static char *krb4_cc_name;
75 #endif
76 int fcache_version;
77 char *password_file = NULL;
78 char *pk_user_id = NULL;
79 int pk_enterprise_flag = 0;
80 struct hx509_certs_data *ent_user_id = NULL;
81 char *pk_x509_anchors = NULL;
82 int pk_use_enckey = 0;
83 static int canonicalize_flag = 0;
84 static int enterprise_flag = 0;
85 static int ok_as_delegate_flag = 0;
86 static int use_referrals_flag = 0;
87 static int windows_flag = 0;
88 #ifndef NO_NTLM
89 static char *ntlm_domain;
90 #endif
93 static struct getargs args[] = {
95 * used by MIT
96 * a: ~A
97 * V: verbose
98 * F: ~f
99 * P: ~p
100 * C: v4 cache name?
101 * 5:
103 #ifndef HEIMDAL_SMALLER
104 { "524init", '4', arg_flag, &get_v4_tgt,
105 NP_("obtain version 4 TGT", "") },
107 { "524convert", '9', arg_flag, &convert_524,
108 NP_("only convert ticket to version 4", "") },
109 #endif
110 { "afslog", 0 , arg_flag, &do_afslog,
111 NP_("obtain afs tokens", "") },
113 { "cache", 'c', arg_string, &cred_cache,
114 NP_("credentials cache", ""), "cachename" },
116 { "forwardable", 'f', arg_flag, &forwardable_flag,
117 NP_("get forwardable tickets", "")},
119 { "keytab", 't', arg_string, &keytab_str,
120 NP_("keytab to use", ""), "keytabname" },
122 { "lifetime", 'l', arg_string, &lifetime,
123 NP_("lifetime of tickets", ""), "time"},
125 { "proxiable", 'p', arg_flag, &proxiable_flag,
126 NP_("get proxiable tickets", "") },
128 { "renew", 'R', arg_flag, &renew_flag,
129 NP_("renew TGT", "") },
131 { "renewable", 0, arg_flag, &renewable_flag,
132 NP_("get renewable tickets", "") },
134 { "renewable-life", 'r', arg_string, &renew_life,
135 NP_("renewable lifetime of tickets", ""), "time" },
137 { "server", 'S', arg_string, &server_str,
138 NP_("server to get ticket for", ""), "principal" },
140 { "start-time", 's', arg_string, &start_str,
141 NP_("when ticket gets valid", ""), "time" },
143 { "use-keytab", 'k', arg_flag, &use_keytab,
144 NP_("get key from keytab", "") },
146 { "validate", 'v', arg_flag, &validate_flag,
147 NP_("validate TGT", "") },
149 { "enctypes", 'e', arg_strings, &etype_str,
150 NP_("encryption types to use", ""), "enctypes" },
152 { "fcache-version", 0, arg_integer, &fcache_version,
153 NP_("file cache version to create", "") },
155 { "addresses", 'A', arg_negative_flag, &addrs_flag,
156 NP_("request a ticket with no addresses", "") },
158 { "extra-addresses",'a', arg_strings, &extra_addresses,
159 NP_("include these extra addresses", ""), "addresses" },
161 { "anonymous", 0, arg_flag, &anonymous_flag,
162 NP_("request an anonymous ticket", "") },
164 { "request-pac", 0, arg_flag, &pac_flag,
165 NP_("request a Windows PAC", "") },
167 { "password-file", 0, arg_string, &password_file,
168 NP_("read the password from a file", "") },
170 { "canonicalize",0, arg_flag, &canonicalize_flag,
171 NP_("canonicalize client principal", "") },
173 { "enterprise",0, arg_flag, &enterprise_flag,
174 NP_("parse principal as a KRB5-NT-ENTERPRISE name", "") },
175 #ifdef PKINIT
176 { "pk-enterprise", 0, arg_flag, &pk_enterprise_flag,
177 NP_("use enterprise name from certificate", "") },
179 { "pk-user", 'C', arg_string, &pk_user_id,
180 NP_("principal's public/private/certificate identifier", ""), "id" },
182 { "x509-anchors", 'D', arg_string, &pk_x509_anchors,
183 NP_("directory with CA certificates", ""), "directory" },
185 { "pk-use-enckey", 0, arg_flag, &pk_use_enckey,
186 NP_("Use RSA encrypted reply (instead of DH)", "") },
187 #endif
188 #ifndef NO_NTLM
189 { "ntlm-domain", 0, arg_string, &ntlm_domain,
190 NP_("NTLM domain", ""), "domain" },
191 #endif
193 { "change-default", 0, arg_negative_flag, &switch_cache_flags,
194 NP_("switch the default cache to the new credentials cache", "") },
196 { "ok-as-delegate", 0, arg_flag, &ok_as_delegate_flag,
197 NP_("honor ok-as-delegate on tickets", "") },
199 { "use-referrals", 0, arg_flag, &use_referrals_flag,
200 NP_("only use referrals, no dns canalisation", "") },
202 { "windows", 0, arg_flag, &windows_flag,
203 NP_("get windows behavior", "") },
205 { "version", 0, arg_flag, &version_flag },
206 { "help", 0, arg_flag, &help_flag }
209 static void
210 usage (int ret)
212 arg_printusage_i18n (args,
213 sizeof(args)/sizeof(*args),
214 N_("Usage: ", ""),
215 NULL,
216 "[principal [command]]",
217 getarg_i18n);
218 exit (ret);
221 static krb5_error_code
222 get_server(krb5_context context,
223 krb5_principal client,
224 const char *server,
225 krb5_principal *princ)
227 krb5_const_realm realm;
228 if(server)
229 return krb5_parse_name(context, server, princ);
231 realm = krb5_principal_get_realm(context, client);
232 return krb5_make_principal(context, princ, realm,
233 KRB5_TGS_NAME, realm, NULL);
236 #ifndef HEIMDAL_SMALLER
238 static krb5_error_code
239 do_524init(krb5_context context, krb5_ccache ccache,
240 krb5_creds *creds, const char *server)
242 krb5_error_code ret;
244 struct credentials c;
245 krb5_creds in_creds, *real_creds;
247 if(creds != NULL)
248 real_creds = creds;
249 else {
250 krb5_principal client;
251 krb5_cc_get_principal(context, ccache, &client);
252 memset(&in_creds, 0, sizeof(in_creds));
253 ret = get_server(context, client, server, &in_creds.server);
254 if(ret) {
255 krb5_free_principal(context, client);
256 return ret;
258 in_creds.client = client;
259 ret = krb5_get_credentials(context, 0, ccache, &in_creds, &real_creds);
260 krb5_free_principal(context, client);
261 krb5_free_principal(context, in_creds.server);
262 if(ret)
263 return ret;
265 ret = krb524_convert_creds_kdc_ccache(context, ccache, real_creds, &c);
266 if(ret)
267 krb5_warn(context, ret, "converting creds");
268 else {
269 krb5_error_code tret = _krb5_krb_tf_setup(context, &c, NULL, 0);
270 if(tret)
271 krb5_warn(context, tret, "saving v4 creds");
274 if(creds == NULL)
275 krb5_free_creds(context, real_creds);
276 memset(&c, 0, sizeof(c));
278 return ret;
281 #endif
283 static int
284 renew_validate(krb5_context context,
285 int renew,
286 int validate,
287 krb5_ccache cache,
288 const char *server,
289 krb5_deltat life)
291 krb5_error_code ret;
292 krb5_creds in, *out = NULL;
293 krb5_kdc_flags flags;
295 memset(&in, 0, sizeof(in));
297 ret = krb5_cc_get_principal(context, cache, &in.client);
298 if(ret) {
299 krb5_warn(context, ret, "krb5_cc_get_principal");
300 return ret;
302 ret = get_server(context, in.client, server, &in.server);
303 if(ret) {
304 krb5_warn(context, ret, "get_server");
305 goto out;
308 if (renew) {
310 * no need to check the error here, it's only to be
311 * friendly to the user
313 krb5_get_credentials(context, KRB5_GC_CACHED, cache, &in, &out);
316 flags.i = 0;
317 flags.b.renewable = flags.b.renew = renew;
318 flags.b.validate = validate;
320 if (forwardable_flag != -1)
321 flags.b.forwardable = forwardable_flag;
322 else if (out)
323 flags.b.forwardable = out->flags.b.forwardable;
325 if (proxiable_flag != -1)
326 flags.b.proxiable = proxiable_flag;
327 else if (out)
328 flags.b.proxiable = out->flags.b.proxiable;
330 if (anonymous_flag)
331 flags.b.request_anonymous = anonymous_flag;
332 if(life)
333 in.times.endtime = time(NULL) + life;
335 if (out) {
336 krb5_free_creds (context, out);
337 out = NULL;
341 ret = krb5_get_kdc_cred(context,
342 cache,
343 flags,
344 NULL,
345 NULL,
346 &in,
347 &out);
348 if(ret) {
349 krb5_warn(context, ret, "krb5_get_kdc_cred");
350 goto out;
352 ret = krb5_cc_initialize(context, cache, in.client);
353 if(ret) {
354 krb5_free_creds (context, out);
355 krb5_warn(context, ret, "krb5_cc_initialize");
356 goto out;
358 ret = krb5_cc_store_cred(context, cache, out);
360 if(ret == 0 && server == NULL) {
361 /* only do this if it's a general renew-my-tgt request */
362 #ifndef HEIMDAL_SMALLER
363 if(get_v4_tgt)
364 do_524init(context, cache, out, NULL);
365 #endif
366 #ifndef NO_AFS
367 if(do_afslog && k_hasafs())
368 krb5_afslog(context, cache, NULL, NULL);
369 #endif
372 krb5_free_creds (context, out);
373 if(ret) {
374 krb5_warn(context, ret, "krb5_cc_store_cred");
375 goto out;
377 out:
378 krb5_free_cred_contents(context, &in);
379 return ret;
382 #ifndef NO_NTLM
384 static krb5_error_code
385 store_ntlmkey(krb5_context context, krb5_ccache id,
386 const char *domain, struct ntlm_buf *buf)
388 krb5_error_code ret;
389 krb5_data data;
390 char *name;
392 asprintf(&name, "ntlm-key-%s", domain);
393 if (name == NULL) {
394 krb5_clear_error_message(context);
395 return ENOMEM;
398 data.length = buf->length;
399 data.data = buf->data;
401 ret = krb5_cc_set_config(context, id, NULL, name, &data);
402 free(name);
403 return ret;
405 #endif
407 static krb5_error_code
408 get_new_tickets(krb5_context context,
409 krb5_principal principal,
410 krb5_ccache ccache,
411 krb5_deltat ticket_life,
412 int interactive)
414 krb5_error_code ret;
415 krb5_get_init_creds_opt *opt;
416 krb5_creds cred;
417 char passwd[256];
418 krb5_deltat start_time = 0;
419 krb5_deltat renew = 0;
420 const char *renewstr = NULL;
421 krb5_enctype *enctype = NULL;
422 krb5_ccache tempccache;
423 #ifndef NO_NTLM
424 struct ntlm_buf ntlmkey;
425 memset(&ntlmkey, 0, sizeof(ntlmkey));
426 #endif
427 passwd[0] = '\0';
429 if (password_file) {
430 FILE *f;
432 if (strcasecmp("STDIN", password_file) == 0)
433 f = stdin;
434 else
435 f = fopen(password_file, "r");
436 if (f == NULL)
437 krb5_errx(context, 1, "Failed to open the password file %s",
438 password_file);
440 if (fgets(passwd, sizeof(passwd), f) == NULL)
441 krb5_errx(context, 1,
442 N_("Failed to read password from file %s", ""),
443 password_file);
444 if (f != stdin)
445 fclose(f);
446 passwd[strcspn(passwd, "\n")] = '\0';
450 memset(&cred, 0, sizeof(cred));
452 ret = krb5_get_init_creds_opt_alloc (context, &opt);
453 if (ret)
454 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc");
456 krb5_get_init_creds_opt_set_default_flags(context, "kinit",
457 krb5_principal_get_realm(context, principal), opt);
459 if(forwardable_flag != -1)
460 krb5_get_init_creds_opt_set_forwardable (opt, forwardable_flag);
461 if(proxiable_flag != -1)
462 krb5_get_init_creds_opt_set_proxiable (opt, proxiable_flag);
463 if(anonymous_flag)
464 krb5_get_init_creds_opt_set_anonymous (opt, anonymous_flag);
465 if (pac_flag != -1)
466 krb5_get_init_creds_opt_set_pac_request(context, opt,
467 pac_flag ? TRUE : FALSE);
468 if (canonicalize_flag)
469 krb5_get_init_creds_opt_set_canonicalize(context, opt, TRUE);
470 if (pk_enterprise_flag && windows_flag)
471 krb5_get_init_creds_opt_set_win2k(context, opt, TRUE);
472 if (pk_user_id || ent_user_id || anonymous_flag) {
473 ret = krb5_get_init_creds_opt_set_pkinit(context, opt,
474 principal,
475 pk_user_id,
476 pk_x509_anchors,
477 NULL,
478 NULL,
479 pk_use_enckey ? 2 : 0 |
480 anonymous_flag ? 4 : 0,
481 krb5_prompter_posix,
482 NULL,
483 passwd);
484 if (ret)
485 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_set_pkinit");
486 if (ent_user_id)
487 _krb5_get_init_creds_opt_set_pkinit_user_certs(context, opt, ent_user_id);
490 if (addrs_flag != -1)
491 krb5_get_init_creds_opt_set_addressless(context, opt,
492 addrs_flag ? FALSE : TRUE);
494 if (renew_life == NULL && renewable_flag)
495 renewstr = "1 month";
496 if (renew_life)
497 renewstr = renew_life;
498 if (renewstr) {
499 renew = parse_time (renewstr, "s");
500 if (renew < 0)
501 errx (1, "unparsable time: %s", renewstr);
503 krb5_get_init_creds_opt_set_renew_life (opt, renew);
506 if(ticket_life != 0)
507 krb5_get_init_creds_opt_set_tkt_life (opt, ticket_life);
509 if(start_str) {
510 int tmp = parse_time (start_str, "s");
511 if (tmp < 0)
512 errx (1, N_("unparsable time: %s", ""), start_str);
514 start_time = tmp;
517 if(etype_str.num_strings) {
518 int i;
520 enctype = malloc(etype_str.num_strings * sizeof(*enctype));
521 if(enctype == NULL)
522 errx(1, "out of memory");
523 for(i = 0; i < etype_str.num_strings; i++) {
524 ret = krb5_string_to_enctype(context,
525 etype_str.strings[i],
526 &enctype[i]);
527 if(ret)
528 errx(1, "unrecognized enctype: %s", etype_str.strings[i]);
530 krb5_get_init_creds_opt_set_etype_list(opt, enctype,
531 etype_str.num_strings);
534 if(use_keytab || keytab_str) {
535 krb5_keytab kt;
536 if(keytab_str)
537 ret = krb5_kt_resolve(context, keytab_str, &kt);
538 else
539 ret = krb5_kt_default(context, &kt);
540 if (ret)
541 krb5_err (context, 1, ret, "resolving keytab");
542 ret = krb5_get_init_creds_keytab (context,
543 &cred,
544 principal,
546 start_time,
547 server_str,
548 opt);
549 krb5_kt_close(context, kt);
550 } else if (pk_user_id || ent_user_id || anonymous_flag) {
551 ret = krb5_get_init_creds_password (context,
552 &cred,
553 principal,
554 passwd,
555 krb5_prompter_posix,
556 NULL,
557 start_time,
558 server_str,
559 opt);
560 } else if (!interactive) {
561 krb5_warnx(context, "Not interactive, failed to get initial ticket");
562 krb5_get_init_creds_opt_free(context, opt);
563 return 0;
564 } else {
566 if (passwd[0] == '\0') {
567 char *p, *prompt;
569 krb5_unparse_name (context, principal, &p);
570 asprintf (&prompt, N_("%s's Password: ", ""), p);
571 free (p);
573 if (UI_UTIL_read_pw_string(passwd, sizeof(passwd)-1, prompt, 0)){
574 memset(passwd, 0, sizeof(passwd));
575 exit(1);
577 free (prompt);
581 ret = krb5_get_init_creds_password (context,
582 &cred,
583 principal,
584 passwd,
585 krb5_prompter_posix,
586 NULL,
587 start_time,
588 server_str,
589 opt);
591 krb5_get_init_creds_opt_free(context, opt);
592 #ifndef NO_NTLM
593 if (ntlm_domain && passwd[0])
594 heim_ntlm_nt_key(passwd, &ntlmkey);
595 #endif
596 memset(passwd, 0, sizeof(passwd));
598 switch(ret){
599 case 0:
600 break;
601 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
602 exit(1);
603 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
604 case KRB5KRB_AP_ERR_MODIFIED:
605 case KRB5KDC_ERR_PREAUTH_FAILED:
606 krb5_errx(context, 1, N_("Password incorrect", ""));
607 break;
608 case KRB5KRB_AP_ERR_V4_REPLY:
609 krb5_errx(context, 1, N_("Looks like a Kerberos 4 reply", ""));
610 break;
611 default:
612 krb5_err(context, 1, ret, "krb5_get_init_creds");
615 if(ticket_life != 0) {
616 if(abs(cred.times.endtime - cred.times.starttime - ticket_life) > 30) {
617 char life[64];
618 unparse_time_approx(cred.times.endtime - cred.times.starttime,
619 life, sizeof(life));
620 krb5_warnx(context, N_("NOTICE: ticket lifetime is %s", ""), life);
623 if(renew_life) {
624 if(abs(cred.times.renew_till - cred.times.starttime - renew) > 30) {
625 char life[64];
626 unparse_time_approx(cred.times.renew_till - cred.times.starttime,
627 life, sizeof(life));
628 krb5_warnx(context,
629 N_("NOTICE: ticket renewable lifetime is %s", ""),
630 life);
634 ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, ccache),
635 NULL, &tempccache);
636 if (ret)
637 krb5_err (context, 1, ret, "krb5_cc_new_unique");
639 ret = krb5_cc_initialize (context, tempccache, cred.client);
640 if (ret)
641 krb5_err (context, 1, ret, "krb5_cc_initialize");
643 ret = krb5_cc_store_cred (context, tempccache, &cred);
644 if (ret)
645 krb5_err (context, 1, ret, "krb5_cc_store_cred");
647 krb5_free_cred_contents (context, &cred);
649 ret = krb5_cc_move(context, tempccache, ccache);
650 if (ret)
651 krb5_err (context, 1, ret, "krb5_cc_move");
653 if (switch_cache_flags)
654 krb5_cc_switch(context, ccache);
656 #ifndef NO_NTLM
657 if (ntlm_domain && ntlmkey.data)
658 store_ntlmkey(context, ccache, ntlm_domain, &ntlmkey);
659 #endif
661 if (ok_as_delegate_flag || windows_flag || use_referrals_flag) {
662 unsigned char d = 0;
663 krb5_data data;
665 if (ok_as_delegate_flag || windows_flag)
666 d |= 1;
667 if (use_referrals_flag || windows_flag)
668 d |= 2;
670 data.length = 1;
671 data.data = &d;
673 krb5_cc_set_config(context, ccache, NULL, "realm-config", &data);
677 if (enctype)
678 free(enctype);
680 return 0;
683 static time_t
684 ticket_lifetime(krb5_context context, krb5_ccache cache,
685 krb5_principal client, const char *server)
687 krb5_creds in_cred, *cred;
688 krb5_error_code ret;
689 time_t timeout;
691 memset(&in_cred, 0, sizeof(in_cred));
693 ret = krb5_cc_get_principal(context, cache, &in_cred.client);
694 if(ret) {
695 krb5_warn(context, ret, "krb5_cc_get_principal");
696 return 0;
698 ret = get_server(context, in_cred.client, server, &in_cred.server);
699 if(ret) {
700 krb5_free_principal(context, in_cred.client);
701 krb5_warn(context, ret, "get_server");
702 return 0;
705 ret = krb5_get_credentials(context, KRB5_GC_CACHED,
706 cache, &in_cred, &cred);
707 krb5_free_principal(context, in_cred.client);
708 krb5_free_principal(context, in_cred.server);
709 if(ret) {
710 krb5_warn(context, ret, "krb5_get_credentials");
711 return 0;
713 timeout = cred->times.endtime - cred->times.starttime;
714 if (timeout < 0)
715 timeout = 0;
716 krb5_free_creds(context, cred);
717 return timeout;
720 struct renew_ctx {
721 krb5_context context;
722 krb5_ccache ccache;
723 krb5_principal principal;
724 krb5_deltat ticket_life;
727 static time_t
728 renew_func(void *ptr)
730 struct renew_ctx *ctx = ptr;
731 krb5_error_code ret;
732 time_t expire;
733 int new_tickets = 0;
735 if (renewable_flag) {
736 ret = renew_validate(ctx->context, renewable_flag, validate_flag,
737 ctx->ccache, server_str, ctx->ticket_life);
738 if (ret)
739 new_tickets = 1;
740 } else
741 new_tickets = 1;
743 if (new_tickets)
744 get_new_tickets(ctx->context, ctx->principal,
745 ctx->ccache, ctx->ticket_life, 0);
747 #ifndef HEIMDAL_SMALLER
748 if(get_v4_tgt || convert_524)
749 do_524init(ctx->context, ctx->ccache, NULL, server_str);
750 #endif
751 #ifndef NO_AFS
752 if(do_afslog && k_hasafs())
753 krb5_afslog(ctx->context, ctx->ccache, NULL, NULL);
754 #endif
756 expire = ticket_lifetime(ctx->context, ctx->ccache, ctx->principal,
757 server_str) / 2;
758 return expire + 1;
762 main (int argc, char **argv)
764 krb5_error_code ret;
765 krb5_context context;
766 krb5_ccache ccache;
767 krb5_principal principal;
768 int optidx = 0;
769 krb5_deltat ticket_life = 0;
770 int parseflags = 0;
772 setprogname (argv[0]);
774 setlocale (LC_ALL, "");
775 bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR);
776 textdomain("heimdal_kuser");
778 ret = krb5_init_context (&context);
779 if (ret == KRB5_CONFIG_BADFORMAT)
780 errx (1, "krb5_init_context failed to parse configuration file");
781 else if (ret)
782 errx(1, "krb5_init_context failed: %d", ret);
784 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
785 usage(1);
787 if (help_flag)
788 usage (0);
790 if(version_flag) {
791 print_version(NULL);
792 exit(0);
795 argc -= optidx;
796 argv += optidx;
798 if (canonicalize_flag || enterprise_flag)
799 parseflags |= KRB5_PRINCIPAL_PARSE_ENTERPRISE;
801 if (pk_enterprise_flag) {
802 ret = _krb5_pk_enterprise_cert(context, pk_user_id,
803 argv[0], &principal,
804 &ent_user_id);
805 if (ret)
806 krb5_err(context, 1, ret, "krb5_pk_enterprise_certs");
808 pk_user_id = NULL;
810 } else if (anonymous_flag) {
812 ret = krb5_make_principal(context, &principal, argv[0],
813 KRB5_WELLKNOWN_NAME, KRB5_ANON_NAME,
814 NULL);
815 if (ret)
816 krb5_err(context, 1, ret, "krb5_make_principal");
817 krb5_principal_set_type(context, principal, KRB5_NT_WELLKNOWN);
819 } else {
820 if (argv[0]) {
821 ret = krb5_parse_name_flags (context, argv[0], parseflags,
822 &principal);
823 if (ret)
824 krb5_err (context, 1, ret, "krb5_parse_name");
825 } else {
826 ret = krb5_get_default_principal (context, &principal);
827 if (ret)
828 krb5_err (context, 1, ret, "krb5_get_default_principal");
832 if(fcache_version)
833 krb5_set_fcache_version(context, fcache_version);
835 if(renewable_flag == -1)
836 /* this seems somewhat pointless, but whatever */
837 krb5_appdefault_boolean(context, "kinit",
838 krb5_principal_get_realm(context, principal),
839 "renewable", FALSE, &renewable_flag);
840 #ifndef HEIMDAL_SMALLER
841 if(get_v4_tgt == -1)
842 krb5_appdefault_boolean(context, "kinit",
843 krb5_principal_get_realm(context, principal),
844 "krb4_get_tickets", FALSE, &get_v4_tgt);
845 #endif
846 if(do_afslog == -1)
847 krb5_appdefault_boolean(context, "kinit",
848 krb5_principal_get_realm(context, principal),
849 "afslog", TRUE, &do_afslog);
851 if(cred_cache)
852 ret = krb5_cc_resolve(context, cred_cache, &ccache);
853 else {
854 if(argc > 1) {
855 char s[1024];
856 ret = krb5_cc_new_unique(context, NULL, NULL, &ccache);
857 if(ret)
858 krb5_err(context, 1, ret, "creating cred cache");
859 snprintf(s, sizeof(s), "%s:%s",
860 krb5_cc_get_type(context, ccache),
861 krb5_cc_get_name(context, ccache));
862 setenv("KRB5CCNAME", s, 1);
863 #ifndef HEIMDAL_SMALLER
864 if (get_v4_tgt) {
865 int fd;
866 if (asprintf(&krb4_cc_name, "%s_XXXXXX", TKT_ROOT) < 0)
867 krb5_errx(context, 1, "out of memory");
868 if((fd = mkstemp(krb4_cc_name)) >= 0) {
869 close(fd);
870 setenv("KRBTKFILE", krb4_cc_name, 1);
871 } else {
872 free(krb4_cc_name);
873 krb4_cc_name = NULL;
876 #endif
877 } else {
878 ret = krb5_cc_cache_match(context, principal, &ccache);
879 if (ret)
880 ret = krb5_cc_default (context, &ccache);
883 if (ret)
884 krb5_err (context, 1, ret, N_("resolving credentials cache", ""));
886 #ifndef NO_AFS
887 if(argc > 1 && k_hasafs ())
888 k_setpag();
889 #endif
891 if (lifetime) {
892 int tmp = parse_time (lifetime, "s");
893 if (tmp < 0)
894 errx (1, N_("unparsable time: %s", ""), lifetime);
896 ticket_life = tmp;
899 if(addrs_flag == 0 && extra_addresses.num_strings > 0)
900 krb5_errx(context, 1,
901 N_("specifying both extra addresses and "
902 "no addresses makes no sense", ""));
904 int i;
905 krb5_addresses addresses;
906 memset(&addresses, 0, sizeof(addresses));
907 for(i = 0; i < extra_addresses.num_strings; i++) {
908 ret = krb5_parse_address(context, extra_addresses.strings[i],
909 &addresses);
910 if (ret == 0) {
911 krb5_add_extra_addresses(context, &addresses);
912 krb5_free_addresses(context, &addresses);
915 free_getarg_strings(&extra_addresses);
918 if(renew_flag || validate_flag) {
919 ret = renew_validate(context, renew_flag, validate_flag,
920 ccache, server_str, ticket_life);
921 exit(ret != 0);
924 #ifndef HEIMDAL_SMALLER
925 if(!convert_524)
926 #endif
927 get_new_tickets(context, principal, ccache, ticket_life, 1);
929 #ifndef HEIMDAL_SMALLER
930 if(get_v4_tgt || convert_524)
931 do_524init(context, ccache, NULL, server_str);
932 #endif
933 #ifndef NO_AFS
934 if(do_afslog && k_hasafs())
935 krb5_afslog(context, ccache, NULL, NULL);
936 #endif
937 if(argc > 1) {
938 struct renew_ctx ctx;
939 time_t timeout;
941 timeout = ticket_lifetime(context, ccache, principal, server_str) / 2;
943 ctx.context = context;
944 ctx.ccache = ccache;
945 ctx.principal = principal;
946 ctx.ticket_life = ticket_life;
948 ret = simple_execvp_timed(argv[1], argv+1,
949 renew_func, &ctx, timeout);
950 #define EX_NOEXEC 126
951 #define EX_NOTFOUND 127
952 if(ret == EX_NOEXEC)
953 krb5_warnx(context, N_("permission denied: %s", ""), argv[1]);
954 else if(ret == EX_NOTFOUND)
955 krb5_warnx(context, N_("command not found: %s", ""), argv[1]);
957 krb5_cc_destroy(context, ccache);
958 #ifndef HEIMDAL_SMALLER
959 _krb5_krb_dest_tkt(context, krb4_cc_name);
960 #endif
961 #ifndef NO_AFS
962 if(k_hasafs())
963 k_unlog();
964 #endif
965 } else {
966 krb5_cc_close (context, ccache);
967 ret = 0;
969 krb5_free_principal(context, principal);
970 krb5_free_context (context);
971 return ret;