Fix TGS client to request renewable/forwardable/proxiable if possible
[heimdal.git] / kuser / kinit.c
blobb4c532564008eb5e0f2be57d1c0adb4547795285
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 NO_NTLM
43 #include "heimntlm.h"
44 #endif
46 int forwardable_flag = -1;
47 int proxiable_flag = -1;
48 int renewable_flag = -1;
49 int renew_flag = 0;
50 int pac_flag = -1;
51 int validate_flag = 0;
52 int version_flag = 0;
53 int help_flag = 0;
54 int addrs_flag = -1;
55 struct getarg_strings extra_addresses;
56 int anonymous_flag = 0;
57 char *lifetime = NULL;
58 char *renew_life = NULL;
59 char *server_str = NULL;
60 char *cred_cache = NULL;
61 char *start_str = NULL;
62 static int switch_cache_flags = 1;
63 struct getarg_strings etype_str;
64 int use_keytab = 0;
65 char *keytab_str = NULL;
66 int do_afslog = -1;
67 int fcache_version;
68 char *password_file = NULL;
69 char *pk_user_id = NULL;
70 int pk_enterprise_flag = 0;
71 struct hx509_certs_data *ent_user_id = NULL;
72 char *pk_x509_anchors = NULL;
73 int pk_use_enckey = 0;
74 static int canonicalize_flag = 0;
75 static int enterprise_flag = 0;
76 static int ok_as_delegate_flag = 0;
77 static char *fast_armor_cache_string = NULL;
78 static int use_referrals_flag = 0;
79 static int windows_flag = 0;
80 #ifndef NO_NTLM
81 static char *ntlm_domain;
82 #endif
85 static struct getargs args[] = {
87 * used by MIT
88 * a: ~A
89 * V: verbose
90 * F: ~f
91 * P: ~p
92 * C: v4 cache name?
93 * 5:
95 * old flags
96 * 4:
97 * 9:
99 { "afslog", 0 , arg_flag, &do_afslog,
100 NP_("obtain afs tokens", ""), NULL },
102 { "cache", 'c', arg_string, &cred_cache,
103 NP_("credentials cache", ""), "cachename" },
105 { "forwardable", 0, arg_negative_flag, &forwardable_flag,
106 NP_("get tickets not forwardable", ""), NULL },
108 { NULL, 'f', arg_flag, &forwardable_flag,
109 NP_("get forwardable tickets", ""), NULL },
111 { "keytab", 't', arg_string, &keytab_str,
112 NP_("keytab to use", ""), "keytabname" },
114 { "lifetime", 'l', arg_string, &lifetime,
115 NP_("lifetime of tickets", ""), "time" },
117 { "proxiable", 'p', arg_flag, &proxiable_flag,
118 NP_("get proxiable tickets", ""), NULL },
120 { "renew", 'R', arg_flag, &renew_flag,
121 NP_("renew TGT", ""), NULL },
123 { "renewable", 0, arg_flag, &renewable_flag,
124 NP_("get renewable tickets", ""), NULL },
126 { "renewable-life", 'r', arg_string, &renew_life,
127 NP_("renewable lifetime of tickets", ""), "time" },
129 { "server", 'S', arg_string, &server_str,
130 NP_("server to get ticket for", ""), "principal" },
132 { "start-time", 's', arg_string, &start_str,
133 NP_("when ticket gets valid", ""), "time" },
135 { "use-keytab", 'k', arg_flag, &use_keytab,
136 NP_("get key from keytab", ""), NULL },
138 { "validate", 'v', arg_flag, &validate_flag,
139 NP_("validate TGT", ""), NULL },
141 { "enctypes", 'e', arg_strings, &etype_str,
142 NP_("encryption types to use", ""), "enctypes" },
144 { "fcache-version", 0, arg_integer, &fcache_version,
145 NP_("file cache version to create", ""), NULL },
147 { "addresses", 'A', arg_negative_flag, &addrs_flag,
148 NP_("request a ticket with no addresses", ""), NULL },
150 { "extra-addresses",'a', arg_strings, &extra_addresses,
151 NP_("include these extra addresses", ""), "addresses" },
153 { "anonymous", 0, arg_flag, &anonymous_flag,
154 NP_("request an anonymous ticket", ""), NULL },
156 { "request-pac", 0, arg_flag, &pac_flag,
157 NP_("request a Windows PAC", ""), NULL },
159 { "password-file", 0, arg_string, &password_file,
160 NP_("read the password from a file", ""), NULL },
162 { "canonicalize",0, arg_flag, &canonicalize_flag,
163 NP_("canonicalize client principal", ""), NULL },
165 { "enterprise",0, arg_flag, &enterprise_flag,
166 NP_("parse principal as a KRB5-NT-ENTERPRISE name", ""), NULL },
167 #ifdef PKINIT
168 { "pk-enterprise", 0, arg_flag, &pk_enterprise_flag,
169 NP_("use enterprise name from certificate", ""), NULL },
171 { "pk-user", 'C', arg_string, &pk_user_id,
172 NP_("principal's public/private/certificate identifier", ""), "id" },
174 { "x509-anchors", 'D', arg_string, &pk_x509_anchors,
175 NP_("directory with CA certificates", ""), "directory" },
177 { "pk-use-enckey", 0, arg_flag, &pk_use_enckey,
178 NP_("Use RSA encrypted reply (instead of DH)", ""), NULL },
179 #endif
180 #ifndef NO_NTLM
181 { "ntlm-domain", 0, arg_string, &ntlm_domain,
182 NP_("NTLM domain", ""), "domain" },
183 #endif
185 { "change-default", 0, arg_negative_flag, &switch_cache_flags,
186 NP_("switch the default cache to the new credentials cache", ""), NULL },
188 { "ok-as-delegate", 0, arg_flag, &ok_as_delegate_flag,
189 NP_("honor ok-as-delegate on tickets", ""), NULL },
191 { "fast-armor-cache", 0, arg_string, &fast_armor_cache_string,
192 NP_("use this credential cache as FAST armor cache", ""), "cache" },
194 { "use-referrals", 0, arg_flag, &use_referrals_flag,
195 NP_("only use referrals, no dns canalisation", ""), NULL },
197 { "windows", 0, arg_flag, &windows_flag,
198 NP_("get windows behavior", ""), NULL },
200 { "version", 0, arg_flag, &version_flag, NULL, NULL },
201 { "help", 0, arg_flag, &help_flag, NULL, NULL }
204 static void
205 usage (int ret)
207 arg_printusage_i18n (args,
208 sizeof(args)/sizeof(*args),
209 N_("Usage: ", ""),
210 NULL,
211 "[principal [command]]",
212 getarg_i18n);
213 exit (ret);
216 static krb5_error_code
217 get_server(krb5_context context,
218 krb5_principal client,
219 const char *server,
220 krb5_principal *princ)
222 krb5_const_realm realm;
223 if(server)
224 return krb5_parse_name(context, server, princ);
226 realm = krb5_principal_get_realm(context, client);
227 return krb5_make_principal(context, princ, realm,
228 KRB5_TGS_NAME, realm, NULL);
231 static int
232 renew_validate(krb5_context context,
233 int renew,
234 int validate,
235 krb5_ccache cache,
236 const char *server,
237 krb5_deltat life)
239 krb5_error_code ret;
240 krb5_creds in, *out = NULL;
241 krb5_kdc_flags flags;
243 memset(&in, 0, sizeof(in));
245 ret = krb5_cc_get_principal(context, cache, &in.client);
246 if(ret) {
247 krb5_warn(context, ret, "krb5_cc_get_principal");
248 return ret;
250 ret = get_server(context, in.client, server, &in.server);
251 if(ret) {
252 krb5_warn(context, ret, "get_server");
253 goto out;
256 if (renew) {
258 * no need to check the error here, it's only to be
259 * friendly to the user
261 krb5_get_credentials(context, KRB5_GC_CACHED, cache, &in, &out);
264 flags.i = 0;
265 flags.b.renewable = flags.b.renew = renew;
266 flags.b.validate = validate;
268 if (forwardable_flag != -1)
269 flags.b.forwardable = forwardable_flag;
270 else if (out)
271 flags.b.forwardable = out->flags.b.forwardable;
273 if (proxiable_flag != -1)
274 flags.b.proxiable = proxiable_flag;
275 else if (out)
276 flags.b.proxiable = out->flags.b.proxiable;
278 if (anonymous_flag)
279 flags.b.request_anonymous = anonymous_flag;
280 if(life)
281 in.times.endtime = time(NULL) + life;
283 if (out) {
284 krb5_free_creds (context, out);
285 out = NULL;
289 ret = krb5_get_kdc_cred(context,
290 cache,
291 flags,
292 NULL,
293 NULL,
294 &in,
295 &out);
296 if(ret) {
297 krb5_warn(context, ret, "krb5_get_kdc_cred");
298 goto out;
300 ret = krb5_cc_initialize(context, cache, in.client);
301 if(ret) {
302 krb5_free_creds (context, out);
303 krb5_warn(context, ret, "krb5_cc_initialize");
304 goto out;
306 ret = krb5_cc_store_cred(context, cache, out);
308 if(ret == 0 && server == NULL) {
309 /* only do this if it's a general renew-my-tgt request */
310 #ifndef NO_AFS
311 if(do_afslog && k_hasafs())
312 krb5_afslog(context, cache, NULL, NULL);
313 #endif
316 krb5_free_creds (context, out);
317 if(ret) {
318 krb5_warn(context, ret, "krb5_cc_store_cred");
319 goto out;
321 out:
322 krb5_free_cred_contents(context, &in);
323 return ret;
326 #ifndef NO_NTLM
328 static krb5_error_code
329 store_ntlmkey(krb5_context context, krb5_ccache id,
330 const char *domain, struct ntlm_buf *buf)
332 krb5_error_code ret;
333 krb5_data data;
334 char *name;
336 asprintf(&name, "ntlm-key-%s", domain);
337 if (name == NULL) {
338 krb5_clear_error_message(context);
339 return ENOMEM;
342 data.length = buf->length;
343 data.data = buf->data;
345 ret = krb5_cc_set_config(context, id, NULL, name, &data);
346 free(name);
347 return ret;
349 #endif
351 static krb5_error_code
352 get_new_tickets(krb5_context context,
353 krb5_principal principal,
354 krb5_ccache ccache,
355 krb5_deltat ticket_life,
356 int interactive)
358 krb5_error_code ret;
359 krb5_get_init_creds_opt *opt;
360 krb5_creds cred;
361 char passwd[256];
362 krb5_deltat start_time = 0;
363 krb5_deltat renew = 0;
364 const char *renewstr = NULL;
365 krb5_enctype *enctype = NULL;
366 krb5_ccache tempccache;
367 krb5_keytab kt = NULL;
368 krb5_init_creds_context ctx;
369 #ifndef NO_NTLM
370 struct ntlm_buf ntlmkey;
371 memset(&ntlmkey, 0, sizeof(ntlmkey));
372 #endif
373 passwd[0] = '\0';
375 if (password_file) {
376 FILE *f;
378 if (strcasecmp("STDIN", password_file) == 0)
379 f = stdin;
380 else
381 f = fopen(password_file, "r");
382 if (f == NULL)
383 krb5_errx(context, 1, "Failed to open the password file %s",
384 password_file);
386 if (fgets(passwd, sizeof(passwd), f) == NULL)
387 krb5_errx(context, 1,
388 N_("Failed to read password from file %s", ""),
389 password_file);
390 if (f != stdin)
391 fclose(f);
392 passwd[strcspn(passwd, "\n")] = '\0';
395 #ifdef __APPLE__
396 if (passwd[0] == '\0') {
397 const char *realm;
398 OSStatus osret;
399 UInt32 length;
400 void *buffer;
401 char *name;
403 realm = krb5_principal_get_realm(context, principal);
405 ret = krb5_unparse_name_flags(context, principal,
406 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &name);
407 if (ret)
408 goto nopassword;
410 osret = SecKeychainFindGenericPassword(NULL, strlen(realm), realm,
411 strlen(name), name,
412 &length, &buffer, NULL);
413 free(name);
414 if (osret == noErr && length < sizeof(passwd) - 1) {
415 memcpy(passwd, buffer, length);
416 passwd[length] = '\0';
418 nopassword:
419 do { } while(0);
421 #endif
423 memset(&cred, 0, sizeof(cred));
425 ret = krb5_get_init_creds_opt_alloc (context, &opt);
426 if (ret)
427 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc");
429 krb5_get_init_creds_opt_set_default_flags(context, "kinit",
430 krb5_principal_get_realm(context, principal), opt);
432 if(forwardable_flag != -1)
433 krb5_get_init_creds_opt_set_forwardable (opt, forwardable_flag);
434 if(proxiable_flag != -1)
435 krb5_get_init_creds_opt_set_proxiable (opt, proxiable_flag);
436 if(anonymous_flag)
437 krb5_get_init_creds_opt_set_anonymous (opt, anonymous_flag);
438 if (pac_flag != -1)
439 krb5_get_init_creds_opt_set_pac_request(context, opt,
440 pac_flag ? TRUE : FALSE);
441 if (canonicalize_flag)
442 krb5_get_init_creds_opt_set_canonicalize(context, opt, TRUE);
443 if (pk_enterprise_flag || enterprise_flag || canonicalize_flag || windows_flag)
444 krb5_get_init_creds_opt_set_win2k(context, opt, TRUE);
445 if (pk_user_id || ent_user_id || anonymous_flag) {
446 ret = krb5_get_init_creds_opt_set_pkinit(context, opt,
447 principal,
448 pk_user_id,
449 pk_x509_anchors,
450 NULL,
451 NULL,
452 pk_use_enckey ? 2 : 0 |
453 anonymous_flag ? 4 : 0,
454 krb5_prompter_posix,
455 NULL,
456 passwd);
457 if (ret)
458 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_set_pkinit");
459 if (ent_user_id)
460 krb5_get_init_creds_opt_set_pkinit_user_certs(context, opt, ent_user_id);
463 if (addrs_flag != -1)
464 krb5_get_init_creds_opt_set_addressless(context, opt,
465 addrs_flag ? FALSE : TRUE);
467 if (renew_life == NULL && renewable_flag)
468 renewstr = "1 month";
469 if (renew_life)
470 renewstr = renew_life;
471 if (renewstr) {
472 renew = parse_time (renewstr, "s");
473 if (renew < 0)
474 errx (1, "unparsable time: %s", renewstr);
476 krb5_get_init_creds_opt_set_renew_life (opt, renew);
479 if(ticket_life != 0)
480 krb5_get_init_creds_opt_set_tkt_life (opt, ticket_life);
482 if(start_str) {
483 int tmp = parse_time (start_str, "s");
484 if (tmp < 0)
485 errx (1, N_("unparsable time: %s", ""), start_str);
487 start_time = tmp;
490 if(etype_str.num_strings) {
491 int i;
493 enctype = malloc(etype_str.num_strings * sizeof(*enctype));
494 if(enctype == NULL)
495 errx(1, "out of memory");
496 for(i = 0; i < etype_str.num_strings; i++) {
497 ret = krb5_string_to_enctype(context,
498 etype_str.strings[i],
499 &enctype[i]);
500 if(ret)
501 errx(1, "unrecognized enctype: %s", etype_str.strings[i]);
503 krb5_get_init_creds_opt_set_etype_list(opt, enctype,
504 etype_str.num_strings);
507 ret = krb5_init_creds_init(context, principal, krb5_prompter_posix, NULL, start_time, opt, &ctx);
508 if (ret)
509 krb5_err(context, 1, ret, "krb5_init_creds_init");
511 if (server_str) {
512 ret = krb5_init_creds_set_service(context, ctx, server_str);
513 if (ret)
514 krb5_err(context, 1, ret, "krb5_init_creds_set_service");
517 if (fast_armor_cache_string) {
518 krb5_ccache fastid;
520 ret = krb5_cc_resolve(context, fast_armor_cache_string, &fastid);
521 if (ret)
522 krb5_err(context, 1, ret, "krb5_cc_resolve(FAST cache)");
524 ret = krb5_init_creds_set_fast_ccache(context, ctx, fastid);
525 if (ret)
526 krb5_err(context, 1, ret, "krb5_init_creds_set_fast_ccache");
529 if(use_keytab || keytab_str) {
531 if(keytab_str)
532 ret = krb5_kt_resolve(context, keytab_str, &kt);
533 else
534 ret = krb5_kt_default(context, &kt);
535 if (ret)
536 krb5_err(context, 1, ret, "resolving keytab");
538 ret = krb5_init_creds_set_keytab(context, ctx, kt);
539 if (ret)
540 krb5_err(context, 1, ret, "krb5_init_creds_set_keytab");
542 } else if (pk_user_id || ent_user_id || anonymous_flag) {
544 } else if (!interactive) {
545 krb5_warnx(context, "Not interactive, failed to get initial ticket");
546 krb5_get_init_creds_opt_free(context, opt);
547 return 0;
548 } else {
550 if (passwd[0] == '\0') {
551 char *p, *prompt;
553 krb5_unparse_name (context, principal, &p);
554 asprintf (&prompt, N_("%s's Password: ", ""), p);
555 free (p);
557 if (UI_UTIL_read_pw_string(passwd, sizeof(passwd)-1, prompt, 0)){
558 memset(passwd, 0, sizeof(passwd));
559 errx(1, "failed to read password");
561 free (prompt);
564 if (passwd[0]) {
565 ret = krb5_init_creds_set_password(context, ctx, passwd);
566 if (ret)
567 krb5_err(context, 1, ret, "krb5_init_creds_set_password");
571 ret = krb5_init_creds_get(context, ctx);
573 #ifndef NO_NTLM
574 if (ntlm_domain && passwd[0])
575 heim_ntlm_nt_key(passwd, &ntlmkey);
576 #endif
577 memset(passwd, 0, sizeof(passwd));
579 switch(ret){
580 case 0:
581 break;
582 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
583 exit(1);
584 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
585 case KRB5KRB_AP_ERR_MODIFIED:
586 case KRB5KDC_ERR_PREAUTH_FAILED:
587 krb5_errx(context, 1, N_("Password incorrect", ""));
588 break;
589 case KRB5KRB_AP_ERR_V4_REPLY:
590 krb5_errx(context, 1, N_("Looks like a Kerberos 4 reply", ""));
591 break;
592 default:
593 krb5_err(context, 1, ret, "krb5_get_init_creds");
596 krb5_process_last_request(context, opt, ctx);
598 ret = krb5_init_creds_get_creds(context, ctx, &cred);
599 if (ret)
600 krb5_err(context, 1, ret, "krb5_init_creds_get_creds");
602 if(ticket_life != 0) {
603 if(abs(cred.times.endtime - cred.times.starttime - ticket_life) > 30) {
604 char life[64];
605 unparse_time_approx(cred.times.endtime - cred.times.starttime,
606 life, sizeof(life));
607 krb5_warnx(context, N_("NOTICE: ticket lifetime is %s", ""), life);
610 if(renew_life) {
611 if(abs(cred.times.renew_till - cred.times.starttime - renew) > 30) {
612 char life[64];
613 unparse_time_approx(cred.times.renew_till - cred.times.starttime,
614 life, sizeof(life));
615 krb5_warnx(context,
616 N_("NOTICE: ticket renewable lifetime is %s", ""),
617 life);
621 ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, ccache),
622 NULL, &tempccache);
623 if (ret)
624 krb5_err (context, 1, ret, "krb5_cc_new_unique");
626 ret = krb5_init_creds_store(context, ctx, tempccache);
627 if (ret)
628 krb5_err(context, 1, ret, "krb5_init_creds_store");
630 krb5_init_creds_free(context, ctx);
632 ret = krb5_cc_move(context, tempccache, ccache);
633 if (ret)
634 krb5_err (context, 1, ret, "krb5_cc_move");
636 if (switch_cache_flags)
637 krb5_cc_switch(context, ccache);
639 #ifndef NO_NTLM
640 if (ntlm_domain && ntlmkey.data)
641 store_ntlmkey(context, ccache, ntlm_domain, &ntlmkey);
642 #endif
644 if (ok_as_delegate_flag || windows_flag || use_referrals_flag) {
645 unsigned char d = 0;
646 krb5_data data;
648 if (ok_as_delegate_flag || windows_flag)
649 d |= 1;
650 if (use_referrals_flag || windows_flag)
651 d |= 2;
653 data.length = 1;
654 data.data = &d;
656 krb5_cc_set_config(context, ccache, NULL, "realm-config", &data);
659 krb5_get_init_creds_opt_free(context, opt);
661 if (kt)
662 krb5_kt_close(context, kt);
663 if (enctype)
664 free(enctype);
666 return 0;
669 static time_t
670 ticket_lifetime(krb5_context context, krb5_ccache cache,
671 krb5_principal client, const char *server)
673 krb5_creds in_cred, *cred;
674 krb5_error_code ret;
675 time_t timeout;
677 memset(&in_cred, 0, sizeof(in_cred));
679 ret = krb5_cc_get_principal(context, cache, &in_cred.client);
680 if(ret) {
681 krb5_warn(context, ret, "krb5_cc_get_principal");
682 return 0;
684 ret = get_server(context, in_cred.client, server, &in_cred.server);
685 if(ret) {
686 krb5_free_principal(context, in_cred.client);
687 krb5_warn(context, ret, "get_server");
688 return 0;
691 ret = krb5_get_credentials(context, KRB5_GC_CACHED,
692 cache, &in_cred, &cred);
693 krb5_free_principal(context, in_cred.client);
694 krb5_free_principal(context, in_cred.server);
695 if(ret) {
696 krb5_warn(context, ret, "krb5_get_credentials");
697 return 0;
699 timeout = cred->times.endtime - cred->times.starttime;
700 if (timeout < 0)
701 timeout = 0;
702 krb5_free_creds(context, cred);
703 return timeout;
706 struct renew_ctx {
707 krb5_context context;
708 krb5_ccache ccache;
709 krb5_principal principal;
710 krb5_deltat ticket_life;
713 static time_t
714 renew_func(void *ptr)
716 struct renew_ctx *ctx = ptr;
717 krb5_error_code ret;
718 time_t expire;
719 int new_tickets = 0;
721 if (renewable_flag) {
722 ret = renew_validate(ctx->context, renewable_flag, validate_flag,
723 ctx->ccache, server_str, ctx->ticket_life);
724 if (ret)
725 new_tickets = 1;
726 } else
727 new_tickets = 1;
729 if (new_tickets)
730 get_new_tickets(ctx->context, ctx->principal,
731 ctx->ccache, ctx->ticket_life, 0);
733 #ifndef NO_AFS
734 if(do_afslog && k_hasafs())
735 krb5_afslog(ctx->context, ctx->ccache, NULL, NULL);
736 #endif
738 expire = ticket_lifetime(ctx->context, ctx->ccache, ctx->principal,
739 server_str) / 2;
740 return expire + 1;
744 main (int argc, char **argv)
746 krb5_error_code ret;
747 krb5_context context;
748 krb5_ccache ccache;
749 krb5_principal principal;
750 int optidx = 0;
751 krb5_deltat ticket_life = 0;
752 int parseflags = 0;
754 setprogname (argv[0]);
756 setlocale (LC_ALL, "");
757 bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR);
758 textdomain("heimdal_kuser");
760 ret = krb5_init_context (&context);
761 if (ret == KRB5_CONFIG_BADFORMAT)
762 errx (1, "krb5_init_context failed to parse configuration file");
763 else if (ret)
764 errx(1, "krb5_init_context failed: %d", ret);
766 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
767 usage(1);
769 if (help_flag)
770 usage (0);
772 if(version_flag) {
773 print_version(NULL);
774 exit(0);
777 argc -= optidx;
778 argv += optidx;
780 if (canonicalize_flag || enterprise_flag)
781 parseflags |= KRB5_PRINCIPAL_PARSE_ENTERPRISE;
783 if (pk_enterprise_flag) {
784 ret = krb5_pk_enterprise_cert(context, pk_user_id,
785 argv[0], &principal,
786 &ent_user_id);
787 if (ret)
788 krb5_err(context, 1, ret, "krb5_pk_enterprise_certs");
790 pk_user_id = NULL;
792 } else if (anonymous_flag) {
794 ret = krb5_make_principal(context, &principal, argv[0],
795 KRB5_WELLKNOWN_NAME, KRB5_ANON_NAME,
796 NULL);
797 if (ret)
798 krb5_err(context, 1, ret, "krb5_make_principal");
799 krb5_principal_set_type(context, principal, KRB5_NT_WELLKNOWN);
801 } else {
802 if (argv[0]) {
803 ret = krb5_parse_name_flags (context, argv[0], parseflags,
804 &principal);
805 if (ret)
806 krb5_err (context, 1, ret, "krb5_parse_name");
807 } else {
808 ret = krb5_get_default_principal (context, &principal);
809 if (ret)
810 krb5_err (context, 1, ret, "krb5_get_default_principal");
814 if(fcache_version)
815 krb5_set_fcache_version(context, fcache_version);
817 if(renewable_flag == -1)
818 /* this seems somewhat pointless, but whatever */
819 krb5_appdefault_boolean(context, "kinit",
820 krb5_principal_get_realm(context, principal),
821 "renewable", FALSE, &renewable_flag);
822 if(do_afslog == -1)
823 krb5_appdefault_boolean(context, "kinit",
824 krb5_principal_get_realm(context, principal),
825 "afslog", TRUE, &do_afslog);
827 if(cred_cache)
828 ret = krb5_cc_resolve(context, cred_cache, &ccache);
829 else {
830 if(argc > 1) {
831 char s[1024];
832 ret = krb5_cc_new_unique(context, NULL, NULL, &ccache);
833 if(ret)
834 krb5_err(context, 1, ret, "creating cred cache");
835 snprintf(s, sizeof(s), "%s:%s",
836 krb5_cc_get_type(context, ccache),
837 krb5_cc_get_name(context, ccache));
838 setenv("KRB5CCNAME", s, 1);
839 } else {
840 ret = krb5_cc_cache_match(context, principal, &ccache);
841 if (ret) {
842 const char *type;
843 ret = krb5_cc_default (context, &ccache);
844 if (ret)
845 krb5_err (context, 1, ret, N_("resolving credentials cache", ""));
848 * Check if the type support switching, and we do,
849 * then do that instead over overwriting the current
850 * default credential
852 type = krb5_cc_get_type(context, ccache);
853 if (krb5_cc_support_switch(context, type)) {
854 krb5_cc_close(context, ccache);
855 ret = krb5_cc_new_unique(context, type, NULL, &ccache);
860 if (ret)
861 krb5_err (context, 1, ret, N_("resolving credentials cache", ""));
863 #ifndef NO_AFS
864 if(argc > 1 && k_hasafs ())
865 k_setpag();
866 #endif
868 if (lifetime) {
869 int tmp = parse_time (lifetime, "s");
870 if (tmp < 0)
871 errx (1, N_("unparsable time: %s", ""), lifetime);
873 ticket_life = tmp;
876 if(addrs_flag == 0 && extra_addresses.num_strings > 0)
877 krb5_errx(context, 1,
878 N_("specifying both extra addresses and "
879 "no addresses makes no sense", ""));
881 int i;
882 krb5_addresses addresses;
883 memset(&addresses, 0, sizeof(addresses));
884 for(i = 0; i < extra_addresses.num_strings; i++) {
885 ret = krb5_parse_address(context, extra_addresses.strings[i],
886 &addresses);
887 if (ret == 0) {
888 krb5_add_extra_addresses(context, &addresses);
889 krb5_free_addresses(context, &addresses);
892 free_getarg_strings(&extra_addresses);
895 if(renew_flag || validate_flag) {
896 ret = renew_validate(context, renew_flag, validate_flag,
897 ccache, server_str, ticket_life);
898 exit(ret != 0);
901 get_new_tickets(context, principal, ccache, ticket_life, 1);
903 #ifndef NO_AFS
904 if(do_afslog && k_hasafs())
905 krb5_afslog(context, ccache, NULL, NULL);
906 #endif
907 if(argc > 1) {
908 struct renew_ctx ctx;
909 time_t timeout;
911 timeout = ticket_lifetime(context, ccache, principal, server_str) / 2;
913 ctx.context = context;
914 ctx.ccache = ccache;
915 ctx.principal = principal;
916 ctx.ticket_life = ticket_life;
918 ret = simple_execvp_timed(argv[1], argv+1,
919 renew_func, &ctx, timeout);
920 #define EX_NOEXEC 126
921 #define EX_NOTFOUND 127
922 if(ret == EX_NOEXEC)
923 krb5_warnx(context, N_("permission denied: %s", ""), argv[1]);
924 else if(ret == EX_NOTFOUND)
925 krb5_warnx(context, N_("command not found: %s", ""), argv[1]);
927 krb5_cc_destroy(context, ccache);
928 #ifndef NO_AFS
929 if(k_hasafs())
930 k_unlog();
931 #endif
932 } else {
933 krb5_cc_close (context, ccache);
934 ret = 0;
936 krb5_free_principal(context, principal);
937 krb5_free_context (context);
938 return ret;