Ensure data structures allocated by hprop are initialized
[heimdal.git] / kuser / kinit.c
blob4e9e6ac3d7eef62c092708283713aeba97726c1d
1 /*
2 * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "kuser_locl.h"
38 #ifdef __APPLE__
39 #include <Security/Security.h>
40 #endif
42 #ifndef HEIMDAL_SMALLER
43 #include "krb5-v4compat.h"
44 #endif
46 struct krb5_dh_moduli;
47 struct AlgorithmIdentifier;
48 struct _krb5_krb_auth_data;
49 struct hx509_certs_data;
50 #include <krb5-private.h>
52 #ifndef NO_NTLM
53 #include "heimntlm.h"
54 #endif
56 int forwardable_flag = -1;
57 int proxiable_flag = -1;
58 int renewable_flag = -1;
59 int renew_flag = 0;
60 int pac_flag = -1;
61 int validate_flag = 0;
62 int version_flag = 0;
63 int help_flag = 0;
64 int addrs_flag = -1;
65 struct getarg_strings extra_addresses;
66 int anonymous_flag = 0;
67 char *lifetime = NULL;
68 char *renew_life = NULL;
69 char *server_str = NULL;
70 char *cred_cache = NULL;
71 char *start_str = NULL;
72 static int switch_cache_flags = 1;
73 struct getarg_strings etype_str;
74 int use_keytab = 0;
75 char *keytab_str = NULL;
76 int do_afslog = -1;
77 #ifndef HEIMDAL_SMALLER
78 int get_v4_tgt = -1;
79 int convert_524 = 0;
80 static char *krb4_cc_name;
81 #endif
82 int fcache_version;
83 char *password_file = NULL;
84 char *pk_user_id = NULL;
85 int pk_enterprise_flag = 0;
86 struct hx509_certs_data *ent_user_id = NULL;
87 char *pk_x509_anchors = NULL;
88 int pk_use_enckey = 0;
89 static int canonicalize_flag = 0;
90 static int enterprise_flag = 0;
91 static int ok_as_delegate_flag = 0;
92 static int use_referrals_flag = 0;
93 static int windows_flag = 0;
94 #ifndef NO_NTLM
95 static char *ntlm_domain;
96 #endif
99 static struct getargs args[] = {
101 * used by MIT
102 * a: ~A
103 * V: verbose
104 * F: ~f
105 * P: ~p
106 * C: v4 cache name?
107 * 5:
109 #ifndef HEIMDAL_SMALLER
110 { "524init", '4', arg_flag, &get_v4_tgt,
111 NP_("obtain version 4 TGT", "") },
113 { "524convert", '9', arg_flag, &convert_524,
114 NP_("only convert ticket to version 4", "") },
115 #endif
116 { "afslog", 0 , arg_flag, &do_afslog,
117 NP_("obtain afs tokens", "") },
119 { "cache", 'c', arg_string, &cred_cache,
120 NP_("credentials cache", ""), "cachename" },
122 { "forwardable", 'f', arg_negative_flag, &forwardable_flag,
123 NP_("get forwardable tickets", "")},
125 { "keytab", 't', arg_string, &keytab_str,
126 NP_("keytab to use", ""), "keytabname" },
128 { "lifetime", 'l', arg_string, &lifetime,
129 NP_("lifetime of tickets", ""), "time"},
131 { "proxiable", 'p', arg_flag, &proxiable_flag,
132 NP_("get proxiable tickets", "") },
134 { "renew", 'R', arg_flag, &renew_flag,
135 NP_("renew TGT", "") },
137 { "renewable", 0, arg_flag, &renewable_flag,
138 NP_("get renewable tickets", "") },
140 { "renewable-life", 'r', arg_string, &renew_life,
141 NP_("renewable lifetime of tickets", ""), "time" },
143 { "server", 'S', arg_string, &server_str,
144 NP_("server to get ticket for", ""), "principal" },
146 { "start-time", 's', arg_string, &start_str,
147 NP_("when ticket gets valid", ""), "time" },
149 { "use-keytab", 'k', arg_flag, &use_keytab,
150 NP_("get key from keytab", "") },
152 { "validate", 'v', arg_flag, &validate_flag,
153 NP_("validate TGT", "") },
155 { "enctypes", 'e', arg_strings, &etype_str,
156 NP_("encryption types to use", ""), "enctypes" },
158 { "fcache-version", 0, arg_integer, &fcache_version,
159 NP_("file cache version to create", "") },
161 { "addresses", 'A', arg_negative_flag, &addrs_flag,
162 NP_("request a ticket with no addresses", "") },
164 { "extra-addresses",'a', arg_strings, &extra_addresses,
165 NP_("include these extra addresses", ""), "addresses" },
167 { "anonymous", 0, arg_flag, &anonymous_flag,
168 NP_("request an anonymous ticket", "") },
170 { "request-pac", 0, arg_flag, &pac_flag,
171 NP_("request a Windows PAC", "") },
173 { "password-file", 0, arg_string, &password_file,
174 NP_("read the password from a file", "") },
176 { "canonicalize",0, arg_flag, &canonicalize_flag,
177 NP_("canonicalize client principal", "") },
179 { "enterprise",0, arg_flag, &enterprise_flag,
180 NP_("parse principal as a KRB5-NT-ENTERPRISE name", "") },
181 #ifdef PKINIT
182 { "pk-enterprise", 0, arg_flag, &pk_enterprise_flag,
183 NP_("use enterprise name from certificate", "") },
185 { "pk-user", 'C', arg_string, &pk_user_id,
186 NP_("principal's public/private/certificate identifier", ""), "id" },
188 { "x509-anchors", 'D', arg_string, &pk_x509_anchors,
189 NP_("directory with CA certificates", ""), "directory" },
191 { "pk-use-enckey", 0, arg_flag, &pk_use_enckey,
192 NP_("Use RSA encrypted reply (instead of DH)", "") },
193 #endif
194 #ifndef NO_NTLM
195 { "ntlm-domain", 0, arg_string, &ntlm_domain,
196 NP_("NTLM domain", ""), "domain" },
197 #endif
199 { "change-default", 0, arg_negative_flag, &switch_cache_flags,
200 NP_("switch the default cache to the new credentials cache", "") },
202 { "ok-as-delegate", 0, arg_flag, &ok_as_delegate_flag,
203 NP_("honor ok-as-delegate on tickets", "") },
205 { "use-referrals", 0, arg_flag, &use_referrals_flag,
206 NP_("only use referrals, no dns canalisation", "") },
208 { "windows", 0, arg_flag, &windows_flag,
209 NP_("get windows behavior", "") },
211 { "version", 0, arg_flag, &version_flag },
212 { "help", 0, arg_flag, &help_flag }
215 static void
216 usage (int ret)
218 arg_printusage_i18n (args,
219 sizeof(args)/sizeof(*args),
220 N_("Usage: ", ""),
221 NULL,
222 "[principal [command]]",
223 getarg_i18n);
224 exit (ret);
227 static krb5_error_code
228 get_server(krb5_context context,
229 krb5_principal client,
230 const char *server,
231 krb5_principal *princ)
233 krb5_const_realm realm;
234 if(server)
235 return krb5_parse_name(context, server, princ);
237 realm = krb5_principal_get_realm(context, client);
238 return krb5_make_principal(context, princ, realm,
239 KRB5_TGS_NAME, realm, NULL);
242 #ifndef HEIMDAL_SMALLER
244 static krb5_error_code
245 do_524init(krb5_context context, krb5_ccache ccache,
246 krb5_creds *creds, const char *server)
248 krb5_error_code ret;
250 struct credentials c;
251 krb5_creds in_creds, *real_creds;
253 if(creds != NULL)
254 real_creds = creds;
255 else {
256 krb5_principal client;
257 ret = krb5_cc_get_principal(context, ccache, &client);
258 if (ret) {
259 krb5_warn(context, ret, "524init: can't get client principal");
260 return ret;
262 memset(&in_creds, 0, sizeof(in_creds));
263 ret = get_server(context, client, server, &in_creds.server);
264 if(ret) {
265 krb5_warn(context, ret, "524init: can't get server principal");
266 krb5_free_principal(context, client);
267 return ret;
269 in_creds.client = client;
270 ret = krb5_get_credentials(context, 0, ccache, &in_creds, &real_creds);
271 krb5_free_principal(context, client);
272 krb5_free_principal(context, in_creds.server);
273 if(ret)
274 return ret;
276 ret = krb524_convert_creds_kdc_ccache(context, ccache, real_creds, &c);
277 if(ret)
278 krb5_warn(context, ret, "converting creds");
279 else {
280 krb5_error_code tret = _krb5_krb_tf_setup(context, &c, NULL, 0);
281 if(tret)
282 krb5_warn(context, tret, "saving v4 creds");
285 if(creds == NULL)
286 krb5_free_creds(context, real_creds);
287 memset(&c, 0, sizeof(c));
289 return ret;
292 #endif
294 static int
295 renew_validate(krb5_context context,
296 int renew,
297 int validate,
298 krb5_ccache cache,
299 const char *server,
300 krb5_deltat life)
302 krb5_error_code ret;
303 krb5_creds in, *out = NULL;
304 krb5_kdc_flags flags;
306 memset(&in, 0, sizeof(in));
308 ret = krb5_cc_get_principal(context, cache, &in.client);
309 if(ret) {
310 krb5_warn(context, ret, "krb5_cc_get_principal");
311 return ret;
313 ret = get_server(context, in.client, server, &in.server);
314 if(ret) {
315 krb5_warn(context, ret, "get_server");
316 goto out;
319 if (renew) {
321 * no need to check the error here, it's only to be
322 * friendly to the user
324 krb5_get_credentials(context, KRB5_GC_CACHED, cache, &in, &out);
327 flags.i = 0;
328 flags.b.renewable = flags.b.renew = renew;
329 flags.b.validate = validate;
331 if (forwardable_flag != -1)
332 flags.b.forwardable = forwardable_flag;
333 else if (out)
334 flags.b.forwardable = out->flags.b.forwardable;
336 if (proxiable_flag != -1)
337 flags.b.proxiable = proxiable_flag;
338 else if (out)
339 flags.b.proxiable = out->flags.b.proxiable;
341 if (anonymous_flag)
342 flags.b.request_anonymous = anonymous_flag;
343 if(life)
344 in.times.endtime = time(NULL) + life;
346 if (out) {
347 krb5_free_creds (context, out);
348 out = NULL;
352 ret = krb5_get_kdc_cred(context,
353 cache,
354 flags,
355 NULL,
356 NULL,
357 &in,
358 &out);
359 if(ret) {
360 krb5_warn(context, ret, "krb5_get_kdc_cred");
361 goto out;
363 ret = krb5_cc_initialize(context, cache, in.client);
364 if(ret) {
365 krb5_free_creds (context, out);
366 krb5_warn(context, ret, "krb5_cc_initialize");
367 goto out;
369 ret = krb5_cc_store_cred(context, cache, out);
371 if(ret == 0 && server == NULL) {
372 /* only do this if it's a general renew-my-tgt request */
373 #ifndef HEIMDAL_SMALLER
374 if(get_v4_tgt)
375 do_524init(context, cache, out, NULL);
376 #endif
377 #ifndef NO_AFS
378 if(do_afslog && k_hasafs())
379 krb5_afslog(context, cache, NULL, NULL);
380 #endif
383 krb5_free_creds (context, out);
384 if(ret) {
385 krb5_warn(context, ret, "krb5_cc_store_cred");
386 goto out;
388 out:
389 krb5_free_cred_contents(context, &in);
390 return ret;
393 #ifndef NO_NTLM
395 static krb5_error_code
396 store_ntlmkey(krb5_context context, krb5_ccache id,
397 const char *domain, struct ntlm_buf *buf)
399 krb5_error_code ret;
400 krb5_data data;
401 char *name;
403 asprintf(&name, "ntlm-key-%s", domain);
404 if (name == NULL) {
405 krb5_clear_error_message(context);
406 return ENOMEM;
409 data.length = buf->length;
410 data.data = buf->data;
412 ret = krb5_cc_set_config(context, id, NULL, name, &data);
413 free(name);
414 return ret;
416 #endif
418 static krb5_error_code
419 get_new_tickets(krb5_context context,
420 krb5_principal principal,
421 krb5_ccache ccache,
422 krb5_deltat ticket_life,
423 int interactive)
425 krb5_error_code ret;
426 krb5_get_init_creds_opt *opt;
427 krb5_creds cred;
428 char passwd[256];
429 krb5_deltat start_time = 0;
430 krb5_deltat renew = 0;
431 char *renewstr = NULL;
432 krb5_enctype *enctype = NULL;
433 krb5_ccache tempccache;
434 #ifndef NO_NTLM
435 struct ntlm_buf ntlmkey;
436 memset(&ntlmkey, 0, sizeof(ntlmkey));
437 #endif
438 passwd[0] = '\0';
440 if (password_file) {
441 FILE *f;
443 if (strcasecmp("STDIN", password_file) == 0)
444 f = stdin;
445 else
446 f = fopen(password_file, "r");
447 if (f == NULL)
448 krb5_errx(context, 1, "Failed to open the password file %s",
449 password_file);
451 if (fgets(passwd, sizeof(passwd), f) == NULL)
452 krb5_errx(context, 1,
453 N_("Failed to read password from file %s", ""),
454 password_file);
455 if (f != stdin)
456 fclose(f);
457 passwd[strcspn(passwd, "\n")] = '\0';
460 #ifdef __APPLE__
461 if (passwd[0] == '\0') {
462 const char *realm;
463 OSStatus osret;
464 UInt32 length;
465 void *buffer;
466 char *name;
468 realm = krb5_principal_get_realm(context, principal);
470 ret = krb5_unparse_name_flags(context, principal,
471 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &name);
472 if (ret)
473 goto nopassword;
475 osret = SecKeychainFindGenericPassword(NULL, strlen(realm), realm,
476 strlen(name), name,
477 &length, &buffer, NULL);
478 free(name);
479 if (osret == noErr && length < sizeof(passwd) - 1) {
480 memcpy(passwd, buffer, length);
481 passwd[length] = '\0';
483 nopassword:
484 do { } while(0);
486 #endif
488 memset(&cred, 0, sizeof(cred));
490 ret = krb5_get_init_creds_opt_alloc (context, &opt);
491 if (ret)
492 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc");
494 krb5_get_init_creds_opt_set_default_flags(context, "kinit",
495 krb5_principal_get_realm(context, principal), opt);
497 if(forwardable_flag != -1)
498 krb5_get_init_creds_opt_set_forwardable (opt, forwardable_flag);
499 if(proxiable_flag != -1)
500 krb5_get_init_creds_opt_set_proxiable (opt, proxiable_flag);
501 if(anonymous_flag)
502 krb5_get_init_creds_opt_set_anonymous (opt, anonymous_flag);
503 if (pac_flag != -1)
504 krb5_get_init_creds_opt_set_pac_request(context, opt,
505 pac_flag ? TRUE : FALSE);
506 if (canonicalize_flag)
507 krb5_get_init_creds_opt_set_canonicalize(context, opt, TRUE);
508 if ((pk_enterprise_flag || enterprise_flag || canonicalize_flag) && windows_flag)
509 krb5_get_init_creds_opt_set_win2k(context, opt, TRUE);
510 if (pk_user_id || ent_user_id || anonymous_flag) {
511 ret = krb5_get_init_creds_opt_set_pkinit(context, opt,
512 principal,
513 pk_user_id,
514 pk_x509_anchors,
515 NULL,
516 NULL,
517 pk_use_enckey ? 2 : 0 |
518 anonymous_flag ? 4 : 0,
519 krb5_prompter_posix,
520 NULL,
521 passwd);
522 if (ret)
523 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_set_pkinit");
524 if (ent_user_id)
525 _krb5_get_init_creds_opt_set_pkinit_user_certs(context, opt, ent_user_id);
528 if (addrs_flag != -1)
529 krb5_get_init_creds_opt_set_addressless(context, opt,
530 addrs_flag ? FALSE : TRUE);
532 if (renew_life == NULL && renewable_flag)
533 renewstr = "1 month";
534 if (renew_life)
535 renewstr = renew_life;
536 if (renewstr) {
537 renew = parse_time (renewstr, "s");
538 if (renew < 0)
539 errx (1, "unparsable time: %s", renewstr);
541 krb5_get_init_creds_opt_set_renew_life (opt, renew);
544 if(ticket_life != 0)
545 krb5_get_init_creds_opt_set_tkt_life (opt, ticket_life);
547 if(start_str) {
548 int tmp = parse_time (start_str, "s");
549 if (tmp < 0)
550 errx (1, N_("unparsable time: %s", ""), start_str);
552 start_time = tmp;
555 if(etype_str.num_strings) {
556 int i;
558 enctype = malloc(etype_str.num_strings * sizeof(*enctype));
559 if(enctype == NULL)
560 errx(1, "out of memory");
561 for(i = 0; i < etype_str.num_strings; i++) {
562 ret = krb5_string_to_enctype(context,
563 etype_str.strings[i],
564 &enctype[i]);
565 if(ret)
566 errx(1, "unrecognized enctype: %s", etype_str.strings[i]);
568 krb5_get_init_creds_opt_set_etype_list(opt, enctype,
569 etype_str.num_strings);
572 if(use_keytab || keytab_str) {
573 krb5_keytab kt;
574 if(keytab_str)
575 ret = krb5_kt_resolve(context, keytab_str, &kt);
576 else
577 ret = krb5_kt_default(context, &kt);
578 if (ret)
579 krb5_err (context, 1, ret, "resolving keytab");
580 ret = krb5_get_init_creds_keytab (context,
581 &cred,
582 principal,
584 start_time,
585 server_str,
586 opt);
587 krb5_kt_close(context, kt);
588 } else if (pk_user_id || ent_user_id || anonymous_flag) {
589 ret = krb5_get_init_creds_password (context,
590 &cred,
591 principal,
592 passwd,
593 krb5_prompter_posix,
594 NULL,
595 start_time,
596 server_str,
597 opt);
598 } else if (!interactive) {
599 krb5_warnx(context, "Not interactive, failed to get initial ticket");
600 krb5_get_init_creds_opt_free(context, opt);
601 return 0;
602 } else {
604 if (passwd[0] == '\0') {
605 char *p, *prompt;
607 krb5_unparse_name (context, principal, &p);
608 asprintf (&prompt, N_("%s's Password: ", ""), p);
609 free (p);
611 if (UI_UTIL_read_pw_string(passwd, sizeof(passwd)-1, prompt, 0)){
612 memset(passwd, 0, sizeof(passwd));
613 exit(1);
615 free (prompt);
619 ret = krb5_get_init_creds_password (context,
620 &cred,
621 principal,
622 passwd,
623 krb5_prompter_posix,
624 NULL,
625 start_time,
626 server_str,
627 opt);
629 krb5_get_init_creds_opt_free(context, opt);
630 #ifndef NO_NTLM
631 if (ntlm_domain && passwd[0])
632 heim_ntlm_nt_key(passwd, &ntlmkey);
633 #endif
634 memset(passwd, 0, sizeof(passwd));
636 switch(ret){
637 case 0:
638 break;
639 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
640 exit(1);
641 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
642 case KRB5KRB_AP_ERR_MODIFIED:
643 case KRB5KDC_ERR_PREAUTH_FAILED:
644 krb5_errx(context, 1, N_("Password incorrect", ""));
645 break;
646 case KRB5KRB_AP_ERR_V4_REPLY:
647 krb5_errx(context, 1, N_("Looks like a Kerberos 4 reply", ""));
648 break;
649 default:
650 krb5_err(context, 1, ret, "krb5_get_init_creds");
653 if(ticket_life != 0) {
654 if(abs(cred.times.endtime - cred.times.starttime - ticket_life) > 30) {
655 char life[64];
656 unparse_time_approx(cred.times.endtime - cred.times.starttime,
657 life, sizeof(life));
658 krb5_warnx(context, N_("NOTICE: ticket lifetime is %s", ""), life);
661 if(renew_life) {
662 if(abs(cred.times.renew_till - cred.times.starttime - renew) > 30) {
663 char life[64];
664 unparse_time_approx(cred.times.renew_till - cred.times.starttime,
665 life, sizeof(life));
666 krb5_warnx(context,
667 N_("NOTICE: ticket renewable lifetime is %s", ""),
668 life);
672 ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, ccache),
673 NULL, &tempccache);
674 if (ret)
675 krb5_err (context, 1, ret, "krb5_cc_new_unique");
677 ret = krb5_cc_initialize (context, tempccache, cred.client);
678 if (ret)
679 krb5_err (context, 1, ret, "krb5_cc_initialize");
681 ret = krb5_cc_store_cred (context, tempccache, &cred);
682 if (ret)
683 krb5_err (context, 1, ret, "krb5_cc_store_cred");
685 krb5_free_cred_contents (context, &cred);
687 ret = krb5_cc_move(context, tempccache, ccache);
688 if (ret)
689 krb5_err (context, 1, ret, "krb5_cc_move");
691 if (switch_cache_flags)
692 krb5_cc_switch(context, ccache);
694 #ifndef NO_NTLM
695 if (ntlm_domain && ntlmkey.data)
696 store_ntlmkey(context, ccache, ntlm_domain, &ntlmkey);
697 #endif
699 if (ok_as_delegate_flag || windows_flag || use_referrals_flag) {
700 unsigned char d = 0;
701 krb5_data data;
703 if (ok_as_delegate_flag || windows_flag)
704 d |= 1;
705 if (use_referrals_flag || windows_flag)
706 d |= 2;
708 data.length = 1;
709 data.data = &d;
711 krb5_cc_set_config(context, ccache, NULL, "realm-config", &data);
715 if (enctype)
716 free(enctype);
718 return 0;
721 static time_t
722 ticket_lifetime(krb5_context context, krb5_ccache cache,
723 krb5_principal client, const char *server)
725 krb5_creds in_cred, *cred;
726 krb5_error_code ret;
727 time_t timeout;
729 memset(&in_cred, 0, sizeof(in_cred));
731 ret = krb5_cc_get_principal(context, cache, &in_cred.client);
732 if(ret) {
733 krb5_warn(context, ret, "krb5_cc_get_principal");
734 return 0;
736 ret = get_server(context, in_cred.client, server, &in_cred.server);
737 if(ret) {
738 krb5_free_principal(context, in_cred.client);
739 krb5_warn(context, ret, "get_server");
740 return 0;
743 ret = krb5_get_credentials(context, KRB5_GC_CACHED,
744 cache, &in_cred, &cred);
745 krb5_free_principal(context, in_cred.client);
746 krb5_free_principal(context, in_cred.server);
747 if(ret) {
748 krb5_warn(context, ret, "krb5_get_credentials");
749 return 0;
751 timeout = cred->times.endtime - cred->times.starttime;
752 if (timeout < 0)
753 timeout = 0;
754 krb5_free_creds(context, cred);
755 return timeout;
758 struct renew_ctx {
759 krb5_context context;
760 krb5_ccache ccache;
761 krb5_principal principal;
762 krb5_deltat ticket_life;
765 static time_t
766 renew_func(void *ptr)
768 struct renew_ctx *ctx = ptr;
769 krb5_error_code ret;
770 time_t expire;
771 int new_tickets = 0;
773 if (renewable_flag) {
774 ret = renew_validate(ctx->context, renewable_flag, validate_flag,
775 ctx->ccache, server_str, ctx->ticket_life);
776 if (ret)
777 new_tickets = 1;
778 } else
779 new_tickets = 1;
781 if (new_tickets)
782 get_new_tickets(ctx->context, ctx->principal,
783 ctx->ccache, ctx->ticket_life, 0);
785 #ifndef HEIMDAL_SMALLER
786 if(get_v4_tgt || convert_524)
787 do_524init(ctx->context, ctx->ccache, NULL, server_str);
788 #endif
789 #ifndef NO_AFS
790 if(do_afslog && k_hasafs())
791 krb5_afslog(ctx->context, ctx->ccache, NULL, NULL);
792 #endif
794 expire = ticket_lifetime(ctx->context, ctx->ccache, ctx->principal,
795 server_str) / 2;
796 return expire + 1;
800 main (int argc, char **argv)
802 krb5_error_code ret;
803 krb5_context context;
804 krb5_ccache ccache;
805 krb5_principal principal;
806 int optidx = 0;
807 krb5_deltat ticket_life = 0;
808 int parseflags = 0;
810 setprogname (argv[0]);
812 setlocale (LC_ALL, "");
813 bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR);
814 textdomain("heimdal_kuser");
816 ret = krb5_init_context (&context);
817 if (ret == KRB5_CONFIG_BADFORMAT)
818 errx (1, "krb5_init_context failed to parse configuration file");
819 else if (ret)
820 errx(1, "krb5_init_context failed: %d", ret);
822 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
823 usage(1);
825 if (help_flag)
826 usage (0);
828 if(version_flag) {
829 print_version(NULL);
830 exit(0);
833 argc -= optidx;
834 argv += optidx;
836 if (canonicalize_flag || enterprise_flag)
837 parseflags |= KRB5_PRINCIPAL_PARSE_ENTERPRISE;
839 if (pk_enterprise_flag) {
840 ret = _krb5_pk_enterprise_cert(context, pk_user_id,
841 argv[0], &principal,
842 &ent_user_id);
843 if (ret)
844 krb5_err(context, 1, ret, "krb5_pk_enterprise_certs");
846 pk_user_id = NULL;
848 } else if (anonymous_flag) {
850 ret = krb5_make_principal(context, &principal, argv[0],
851 KRB5_WELLKNOWN_NAME, KRB5_ANON_NAME,
852 NULL);
853 if (ret)
854 krb5_err(context, 1, ret, "krb5_make_principal");
855 krb5_principal_set_type(context, principal, KRB5_NT_WELLKNOWN);
857 } else {
858 if (argv[0]) {
859 ret = krb5_parse_name_flags (context, argv[0], parseflags,
860 &principal);
861 if (ret)
862 krb5_err (context, 1, ret, "krb5_parse_name");
863 } else {
864 ret = krb5_get_default_principal (context, &principal);
865 if (ret)
866 krb5_err (context, 1, ret, "krb5_get_default_principal");
870 if(fcache_version)
871 krb5_set_fcache_version(context, fcache_version);
873 if(renewable_flag == -1)
874 /* this seems somewhat pointless, but whatever */
875 krb5_appdefault_boolean(context, "kinit",
876 krb5_principal_get_realm(context, principal),
877 "renewable", FALSE, &renewable_flag);
878 #ifndef HEIMDAL_SMALLER
879 if(get_v4_tgt == -1)
880 krb5_appdefault_boolean(context, "kinit",
881 krb5_principal_get_realm(context, principal),
882 "krb4_get_tickets", FALSE, &get_v4_tgt);
883 #endif
884 if(do_afslog == -1)
885 krb5_appdefault_boolean(context, "kinit",
886 krb5_principal_get_realm(context, principal),
887 "afslog", TRUE, &do_afslog);
889 if(cred_cache)
890 ret = krb5_cc_resolve(context, cred_cache, &ccache);
891 else {
892 if(argc > 1) {
893 char s[1024];
894 ret = krb5_cc_new_unique(context, NULL, NULL, &ccache);
895 if(ret)
896 krb5_err(context, 1, ret, "creating cred cache");
897 snprintf(s, sizeof(s), "%s:%s",
898 krb5_cc_get_type(context, ccache),
899 krb5_cc_get_name(context, ccache));
900 setenv("KRB5CCNAME", s, 1);
901 #ifndef HEIMDAL_SMALLER
902 if (get_v4_tgt) {
903 int fd;
904 if (asprintf(&krb4_cc_name, "%s_XXXXXX", TKT_ROOT) < 0)
905 krb5_errx(context, 1, "out of memory");
906 if((fd = mkstemp(krb4_cc_name)) >= 0) {
907 close(fd);
908 setenv("KRBTKFILE", krb4_cc_name, 1);
909 } else {
910 free(krb4_cc_name);
911 krb4_cc_name = NULL;
914 #endif
915 } else {
916 ret = krb5_cc_cache_match(context, principal, &ccache);
917 if (ret) {
918 const char *type;
919 ret = krb5_cc_default (context, &ccache);
920 if (ret)
921 krb5_err (context, 1, ret, N_("resolving credentials cache", ""));
924 * Check if the type support switching, and we do,
925 * then do that instead over overwriting the current
926 * default credential
928 type = krb5_cc_get_type(context, ccache);
929 if (krb5_cc_support_switch(context, type)) {
930 krb5_cc_close(context, ccache);
931 ret = krb5_cc_new_unique(context, type, NULL, &ccache);
936 if (ret)
937 krb5_err (context, 1, ret, N_("resolving credentials cache", ""));
939 #ifndef NO_AFS
940 if(argc > 1 && k_hasafs ())
941 k_setpag();
942 #endif
944 if (lifetime) {
945 int tmp = parse_time (lifetime, "s");
946 if (tmp < 0)
947 errx (1, N_("unparsable time: %s", ""), lifetime);
949 ticket_life = tmp;
952 if(addrs_flag == 0 && extra_addresses.num_strings > 0)
953 krb5_errx(context, 1,
954 N_("specifying both extra addresses and "
955 "no addresses makes no sense", ""));
957 int i;
958 krb5_addresses addresses;
959 memset(&addresses, 0, sizeof(addresses));
960 for(i = 0; i < extra_addresses.num_strings; i++) {
961 ret = krb5_parse_address(context, extra_addresses.strings[i],
962 &addresses);
963 if (ret == 0) {
964 krb5_add_extra_addresses(context, &addresses);
965 krb5_free_addresses(context, &addresses);
968 free_getarg_strings(&extra_addresses);
971 if(renew_flag || validate_flag) {
972 ret = renew_validate(context, renew_flag, validate_flag,
973 ccache, server_str, ticket_life);
974 exit(ret != 0);
977 #ifndef HEIMDAL_SMALLER
978 if(!convert_524)
979 #endif
980 get_new_tickets(context, principal, ccache, ticket_life, 1);
982 #ifndef HEIMDAL_SMALLER
983 if(get_v4_tgt || convert_524)
984 do_524init(context, ccache, NULL, server_str);
985 #endif
986 #ifndef NO_AFS
987 if(do_afslog && k_hasafs())
988 krb5_afslog(context, ccache, NULL, NULL);
989 #endif
990 if(argc > 1) {
991 struct renew_ctx ctx;
992 time_t timeout;
994 timeout = ticket_lifetime(context, ccache, principal, server_str) / 2;
996 ctx.context = context;
997 ctx.ccache = ccache;
998 ctx.principal = principal;
999 ctx.ticket_life = ticket_life;
1001 ret = simple_execvp_timed(argv[1], argv+1,
1002 renew_func, &ctx, timeout);
1003 #define EX_NOEXEC 126
1004 #define EX_NOTFOUND 127
1005 if(ret == EX_NOEXEC)
1006 krb5_warnx(context, N_("permission denied: %s", ""), argv[1]);
1007 else if(ret == EX_NOTFOUND)
1008 krb5_warnx(context, N_("command not found: %s", ""), argv[1]);
1010 krb5_cc_destroy(context, ccache);
1011 #ifndef HEIMDAL_SMALLER
1012 _krb5_krb_dest_tkt(context, krb4_cc_name);
1013 #endif
1014 #ifndef NO_AFS
1015 if(k_hasafs())
1016 k_unlog();
1017 #endif
1018 } else {
1019 krb5_cc_close (context, ccache);
1020 ret = 0;
1022 krb5_free_principal(context, principal);
1023 krb5_free_context (context);
1024 return ret;