No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / kuser / kinit.c
blobdd1162fed42e829a8e62f8054c48791dae54f3c7
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"
35 __RCSID("$Heimdal: kinit.c 22116 2007-12-03 21:22:58Z lha $"
36 "$NetBSD$");
38 #include "krb5-v4compat.h"
40 #include "heimntlm.h"
42 int forwardable_flag = -1;
43 int proxiable_flag = -1;
44 int renewable_flag = -1;
45 int renew_flag = 0;
46 int pac_flag = -1;
47 int validate_flag = 0;
48 int version_flag = 0;
49 int help_flag = 0;
50 int addrs_flag = -1;
51 struct getarg_strings extra_addresses;
52 int anonymous_flag = 0;
53 char *lifetime = NULL;
54 char *renew_life = NULL;
55 char *server_str = NULL;
56 char *cred_cache = NULL;
57 char *start_str = NULL;
58 struct getarg_strings etype_str;
59 int use_keytab = 0;
60 char *keytab_str = NULL;
61 int do_afslog = -1;
62 int get_v4_tgt = -1;
63 int convert_524 = 0;
64 int fcache_version;
65 char *password_file = NULL;
66 char *pk_user_id = NULL;
67 char *pk_x509_anchors = NULL;
68 int pk_use_enckey = 0;
69 static int canonicalize_flag = 0;
70 static char *ntlm_domain;
72 static char *krb4_cc_name;
74 static struct getargs args[] = {
75 /*
76 * used by MIT
77 * a: ~A
78 * V: verbose
79 * F: ~f
80 * P: ~p
81 * C: v4 cache name?
82 * 5:
84 { "524init", '4', arg_flag, &get_v4_tgt,
85 "obtain version 4 TGT" },
87 { "524convert", '9', arg_flag, &convert_524,
88 "only convert ticket to version 4" },
90 { "afslog", 0 , arg_flag, &do_afslog,
91 "obtain afs tokens" },
93 { "cache", 'c', arg_string, &cred_cache,
94 "credentials cache", "cachename" },
96 { "forwardable", 'f', arg_flag, &forwardable_flag,
97 "get forwardable tickets"},
99 { "keytab", 't', arg_string, &keytab_str,
100 "keytab to use", "keytabname" },
102 { "lifetime", 'l', arg_string, &lifetime,
103 "lifetime of tickets", "time"},
105 { "proxiable", 'p', arg_flag, &proxiable_flag,
106 "get proxiable tickets" },
108 { "renew", 'R', arg_flag, &renew_flag,
109 "renew TGT" },
111 { "renewable", 0, arg_flag, &renewable_flag,
112 "get renewable tickets" },
114 { "renewable-life", 'r', arg_string, &renew_life,
115 "renewable lifetime of tickets", "time" },
117 { "server", 'S', arg_string, &server_str,
118 "server to get ticket for", "principal" },
120 { "start-time", 's', arg_string, &start_str,
121 "when ticket gets valid", "time" },
123 { "use-keytab", 'k', arg_flag, &use_keytab,
124 "get key from keytab" },
126 { "validate", 'v', arg_flag, &validate_flag,
127 "validate TGT" },
129 { "enctypes", 'e', arg_strings, &etype_str,
130 "encryption types to use", "enctypes" },
132 { "fcache-version", 0, arg_integer, &fcache_version,
133 "file cache version to create" },
135 { "addresses", 'A', arg_negative_flag, &addrs_flag,
136 "request a ticket with no addresses" },
138 { "extra-addresses",'a', arg_strings, &extra_addresses,
139 "include these extra addresses", "addresses" },
141 { "anonymous", 0, arg_flag, &anonymous_flag,
142 "request an anonymous ticket" },
144 { "request-pac", 0, arg_flag, &pac_flag,
145 "request a Windows PAC" },
147 { "password-file", 0, arg_string, &password_file,
148 "read the password from a file" },
150 { "canonicalize",0, arg_flag, &canonicalize_flag,
151 "canonicalize client principal" },
152 #ifdef PKINIT
153 { "pk-user", 'C', arg_string, &pk_user_id,
154 "principal's public/private/certificate identifier", "id" },
156 { "x509-anchors", 'D', arg_string, &pk_x509_anchors,
157 "directory with CA certificates", "directory" },
159 { "pk-use-enckey", 0, arg_flag, &pk_use_enckey,
160 "Use RSA encrypted reply (instead of DH)" },
161 #endif
162 { "ntlm-domain", 0, arg_string, &ntlm_domain,
163 "NTLM domain", "domain" },
165 { "version", 0, arg_flag, &version_flag },
166 { "help", 0, arg_flag, &help_flag }
169 static void
170 usage (int ret)
172 arg_printusage (args,
173 sizeof(args)/sizeof(*args),
174 NULL,
175 "[principal [command]]");
176 exit (ret);
179 static krb5_error_code
180 get_server(krb5_context context,
181 krb5_principal client,
182 const char *server,
183 krb5_principal *princ)
185 krb5_realm *client_realm;
186 if(server)
187 return krb5_parse_name(context, server, princ);
189 client_realm = krb5_princ_realm (context, client);
190 return krb5_make_principal(context, princ, *client_realm,
191 KRB5_TGS_NAME, *client_realm, NULL);
194 static krb5_error_code
195 do_524init(krb5_context context, krb5_ccache ccache,
196 krb5_creds *creds, const char *server)
198 krb5_error_code ret;
200 struct credentials c;
201 krb5_creds in_creds, *real_creds;
203 if(creds != NULL)
204 real_creds = creds;
205 else {
206 krb5_principal client;
207 krb5_cc_get_principal(context, ccache, &client);
208 memset(&in_creds, 0, sizeof(in_creds));
209 ret = get_server(context, client, server, &in_creds.server);
210 if(ret) {
211 krb5_free_principal(context, client);
212 return ret;
214 in_creds.client = client;
215 ret = krb5_get_credentials(context, 0, ccache, &in_creds, &real_creds);
216 krb5_free_principal(context, client);
217 krb5_free_principal(context, in_creds.server);
218 if(ret)
219 return ret;
221 ret = krb524_convert_creds_kdc_ccache(context, ccache, real_creds, &c);
222 if(ret)
223 krb5_warn(context, ret, "converting creds");
224 else {
225 krb5_error_code tret = _krb5_krb_tf_setup(context, &c, NULL, 0);
226 if(tret)
227 krb5_warn(context, tret, "saving v4 creds");
230 if(creds == NULL)
231 krb5_free_creds(context, real_creds);
232 memset(&c, 0, sizeof(c));
234 return ret;
237 static int
238 renew_validate(krb5_context context,
239 int renew,
240 int validate,
241 krb5_ccache cache,
242 const char *server,
243 krb5_deltat life)
245 krb5_error_code ret;
246 krb5_creds in, *out = NULL;
247 krb5_kdc_flags flags;
249 memset(&in, 0, sizeof(in));
251 ret = krb5_cc_get_principal(context, cache, &in.client);
252 if(ret) {
253 krb5_warn(context, ret, "krb5_cc_get_principal");
254 return ret;
256 ret = get_server(context, in.client, server, &in.server);
257 if(ret) {
258 krb5_warn(context, ret, "get_server");
259 goto out;
262 if (renew) {
264 * no need to check the error here, it's only to be
265 * friendly to the user
267 krb5_get_credentials(context, KRB5_GC_CACHED, cache, &in, &out);
270 flags.i = 0;
271 flags.b.renewable = flags.b.renew = renew;
272 flags.b.validate = validate;
274 if (forwardable_flag != -1)
275 flags.b.forwardable = forwardable_flag;
276 else if (out)
277 flags.b.forwardable = out->flags.b.forwardable;
279 if (proxiable_flag != -1)
280 flags.b.proxiable = proxiable_flag;
281 else if (out)
282 flags.b.proxiable = out->flags.b.proxiable;
284 if (anonymous_flag != -1)
285 flags.b.request_anonymous = anonymous_flag;
286 if(life)
287 in.times.endtime = time(NULL) + life;
289 if (out) {
290 krb5_free_creds (context, out);
291 out = NULL;
295 ret = krb5_get_kdc_cred(context,
296 cache,
297 flags,
298 NULL,
299 NULL,
300 &in,
301 &out);
302 if(ret) {
303 krb5_warn(context, ret, "krb5_get_kdc_cred");
304 goto out;
306 ret = krb5_cc_initialize(context, cache, in.client);
307 if(ret) {
308 krb5_free_creds (context, out);
309 krb5_warn(context, ret, "krb5_cc_initialize");
310 goto out;
312 ret = krb5_cc_store_cred(context, cache, out);
314 if(ret == 0 && server == NULL) {
315 /* only do this if it's a general renew-my-tgt request */
316 if(get_v4_tgt)
317 do_524init(context, cache, out, NULL);
318 if(do_afslog && k_hasafs())
319 krb5_afslog(context, cache, NULL, NULL);
322 krb5_free_creds (context, out);
323 if(ret) {
324 krb5_warn(context, ret, "krb5_cc_store_cred");
325 goto out;
327 out:
328 krb5_free_cred_contents(context, &in);
329 return ret;
332 static krb5_error_code
333 store_ntlmkey(krb5_context context, krb5_ccache id,
334 const char *domain, krb5_const_principal client,
335 struct ntlm_buf *buf)
337 krb5_error_code ret;
338 krb5_creds cred;
340 memset(&cred, 0, sizeof(cred));
342 ret = krb5_make_principal(context, &cred.server,
343 krb5_principal_get_realm(context, client),
344 "@ntlm-key", domain, NULL);
345 if (ret)
346 goto out;
347 ret = krb5_copy_principal(context, client, &cred.client);
348 if (ret)
349 goto out;
351 cred.times.authtime = time(NULL);
352 cred.times.endtime = time(NULL) + 3600 * 24 * 30; /* XXX */
353 cred.session.keytype = ENCTYPE_ARCFOUR_HMAC_MD5;
354 ret = krb5_data_copy(&cred.session.keyvalue, buf->data, buf->length);
355 if (ret)
356 goto out;
358 ret = krb5_cc_store_cred(context, id, &cred);
360 out:
361 krb5_free_cred_contents (context, &cred);
362 return 0;
365 static krb5_error_code
366 get_new_tickets(krb5_context context,
367 krb5_principal principal,
368 krb5_ccache ccache,
369 krb5_deltat ticket_life,
370 int interactive)
372 krb5_error_code ret;
373 krb5_get_init_creds_opt *opt;
374 krb5_creds cred;
375 char passwd[256];
376 krb5_deltat start_time = 0;
377 krb5_deltat renew = 0;
378 char *renewstr = NULL;
379 krb5_enctype *enctype = NULL;
380 struct ntlm_buf ntlmkey;
381 krb5_ccache tempccache;
383 memset(&ntlmkey, 0, sizeof(ntlmkey));
384 passwd[0] = '\0';
386 if (password_file) {
387 FILE *f;
389 if (strcasecmp("STDIN", password_file) == 0)
390 f = stdin;
391 else
392 f = fopen(password_file, "r");
393 if (f == NULL)
394 krb5_errx(context, 1, "Failed to open the password file %s",
395 password_file);
397 if (fgets(passwd, sizeof(passwd), f) == NULL)
398 krb5_errx(context, 1,
399 "Failed to read password from file %s", password_file);
400 if (f != stdin)
401 fclose(f);
402 passwd[strcspn(passwd, "\n")] = '\0';
406 memset(&cred, 0, sizeof(cred));
408 ret = krb5_get_init_creds_opt_alloc (context, &opt);
409 if (ret)
410 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc");
412 krb5_get_init_creds_opt_set_default_flags(context, "kinit",
413 krb5_principal_get_realm(context, principal), opt);
415 if(forwardable_flag != -1)
416 krb5_get_init_creds_opt_set_forwardable (opt, forwardable_flag);
417 if(proxiable_flag != -1)
418 krb5_get_init_creds_opt_set_proxiable (opt, proxiable_flag);
419 if(anonymous_flag != -1)
420 krb5_get_init_creds_opt_set_anonymous (opt, anonymous_flag);
421 if (pac_flag != -1)
422 krb5_get_init_creds_opt_set_pac_request(context, opt,
423 pac_flag ? TRUE : FALSE);
424 if (canonicalize_flag)
425 krb5_get_init_creds_opt_set_canonicalize(context, opt, TRUE);
426 if (pk_user_id) {
427 ret = krb5_get_init_creds_opt_set_pkinit(context, opt,
428 principal,
429 pk_user_id,
430 pk_x509_anchors,
431 NULL,
432 NULL,
433 pk_use_enckey ? 2 : 0,
434 krb5_prompter_posix,
435 NULL,
436 passwd);
437 if (ret)
438 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_set_pkinit");
441 if (addrs_flag != -1)
442 krb5_get_init_creds_opt_set_addressless(context, opt,
443 addrs_flag ? FALSE : TRUE);
445 if (renew_life == NULL && renewable_flag)
446 renewstr = "1 month";
447 if (renew_life)
448 renewstr = renew_life;
449 if (renewstr) {
450 renew = parse_time (renewstr, "s");
451 if (renew < 0)
452 errx (1, "unparsable time: %s", renewstr);
454 krb5_get_init_creds_opt_set_renew_life (opt, renew);
457 if(ticket_life != 0)
458 krb5_get_init_creds_opt_set_tkt_life (opt, ticket_life);
460 if(start_str) {
461 int tmp = parse_time (start_str, "s");
462 if (tmp < 0)
463 errx (1, "unparsable time: %s", start_str);
465 start_time = tmp;
468 if(etype_str.num_strings) {
469 int i;
471 enctype = malloc(etype_str.num_strings * sizeof(*enctype));
472 if(enctype == NULL)
473 errx(1, "out of memory");
474 for(i = 0; i < etype_str.num_strings; i++) {
475 ret = krb5_string_to_enctype(context,
476 etype_str.strings[i],
477 &enctype[i]);
478 if(ret)
479 errx(1, "unrecognized enctype: %s", etype_str.strings[i]);
481 krb5_get_init_creds_opt_set_etype_list(opt, enctype,
482 etype_str.num_strings);
485 if(use_keytab || keytab_str) {
486 krb5_keytab kt;
487 if(keytab_str)
488 ret = krb5_kt_resolve(context, keytab_str, &kt);
489 else
490 ret = krb5_kt_default(context, &kt);
491 if (ret)
492 krb5_err (context, 1, ret, "resolving keytab");
493 ret = krb5_get_init_creds_keytab (context,
494 &cred,
495 principal,
497 start_time,
498 server_str,
499 opt);
500 krb5_kt_close(context, kt);
501 } else if (pk_user_id) {
502 ret = krb5_get_init_creds_password (context,
503 &cred,
504 principal,
505 passwd,
506 krb5_prompter_posix,
507 NULL,
508 start_time,
509 server_str,
510 opt);
511 } else if (!interactive) {
512 krb5_warnx(context, "Not interactive, failed to get initial ticket");
513 krb5_get_init_creds_opt_free(context, opt);
514 return 0;
515 } else {
517 if (passwd[0] == '\0') {
518 char *p, *prompt;
520 krb5_unparse_name (context, principal, &p);
521 asprintf (&prompt, "%s's Password: ", p);
522 free (p);
524 if (UI_UTIL_read_pw_string(passwd, sizeof(passwd)-1, prompt, 0)){
525 memset(passwd, 0, sizeof(passwd));
526 exit(1);
528 free (prompt);
532 ret = krb5_get_init_creds_password (context,
533 &cred,
534 principal,
535 passwd,
536 krb5_prompter_posix,
537 NULL,
538 start_time,
539 server_str,
540 opt);
542 krb5_get_init_creds_opt_free(context, opt);
543 if (ntlm_domain && passwd[0])
544 heim_ntlm_nt_key(passwd, &ntlmkey);
545 memset(passwd, 0, sizeof(passwd));
547 switch(ret){
548 case 0:
549 break;
550 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
551 exit(1);
552 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
553 case KRB5KRB_AP_ERR_MODIFIED:
554 case KRB5KDC_ERR_PREAUTH_FAILED:
555 krb5_errx(context, 1, "Password incorrect");
556 break;
557 case KRB5KRB_AP_ERR_V4_REPLY:
558 krb5_errx(context, 1, "Looks like a Kerberos 4 reply");
559 break;
560 default:
561 krb5_err(context, 1, ret, "krb5_get_init_creds");
564 if(ticket_life != 0) {
565 if(abs(cred.times.endtime - cred.times.starttime - ticket_life) > 30) {
566 char life[64];
567 unparse_time_approx(cred.times.endtime - cred.times.starttime,
568 life, sizeof(life));
569 krb5_warnx(context, "NOTICE: ticket lifetime is %s", life);
572 if(renew_life) {
573 if(abs(cred.times.renew_till - cred.times.starttime - renew) > 30) {
574 char life[64];
575 unparse_time_approx(cred.times.renew_till - cred.times.starttime,
576 life, sizeof(life));
577 krb5_warnx(context, "NOTICE: ticket renewable lifetime is %s",
578 life);
582 ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, ccache),
583 NULL, &tempccache);
584 if (ret)
585 krb5_err (context, 1, ret, "krb5_cc_new_unique");
587 ret = krb5_cc_initialize (context, tempccache, cred.client);
588 if (ret)
589 krb5_err (context, 1, ret, "krb5_cc_initialize");
591 ret = krb5_cc_store_cred (context, tempccache, &cred);
592 if (ret)
593 krb5_err (context, 1, ret, "krb5_cc_store_cred");
595 krb5_free_cred_contents (context, &cred);
597 ret = krb5_cc_move(context, tempccache, ccache);
598 if (ret)
599 krb5_err (context, 1, ret, "krb5_cc_move");
601 if (ntlm_domain && ntlmkey.data)
602 store_ntlmkey(context, ccache, ntlm_domain, principal, &ntlmkey);
604 if (enctype)
605 free(enctype);
607 return 0;
610 static time_t
611 ticket_lifetime(krb5_context context, krb5_ccache cache,
612 krb5_principal client, const char *server)
614 krb5_creds in_cred, *cred;
615 krb5_error_code ret;
616 time_t timeout;
618 memset(&in_cred, 0, sizeof(in_cred));
620 ret = krb5_cc_get_principal(context, cache, &in_cred.client);
621 if(ret) {
622 krb5_warn(context, ret, "krb5_cc_get_principal");
623 return 0;
625 ret = get_server(context, in_cred.client, server, &in_cred.server);
626 if(ret) {
627 krb5_free_principal(context, in_cred.client);
628 krb5_warn(context, ret, "get_server");
629 return 0;
632 ret = krb5_get_credentials(context, KRB5_GC_CACHED,
633 cache, &in_cred, &cred);
634 krb5_free_principal(context, in_cred.client);
635 krb5_free_principal(context, in_cred.server);
636 if(ret) {
637 krb5_warn(context, ret, "krb5_get_credentials");
638 return 0;
640 timeout = cred->times.endtime - cred->times.starttime;
641 if (timeout < 0)
642 timeout = 0;
643 krb5_free_creds(context, cred);
644 return timeout;
647 struct renew_ctx {
648 krb5_context context;
649 krb5_ccache ccache;
650 krb5_principal principal;
651 krb5_deltat ticket_life;
654 static time_t
655 renew_func(void *ptr)
657 struct renew_ctx *ctx = ptr;
658 krb5_error_code ret;
659 time_t expire;
660 int new_tickets = 0;
662 if (renewable_flag) {
663 ret = renew_validate(ctx->context, renewable_flag, validate_flag,
664 ctx->ccache, server_str, ctx->ticket_life);
665 if (ret)
666 new_tickets = 1;
667 } else
668 new_tickets = 1;
670 if (new_tickets)
671 get_new_tickets(ctx->context, ctx->principal,
672 ctx->ccache, ctx->ticket_life, 0);
674 if(get_v4_tgt || convert_524)
675 do_524init(ctx->context, ctx->ccache, NULL, server_str);
676 if(do_afslog && k_hasafs())
677 krb5_afslog(ctx->context, ctx->ccache, NULL, NULL);
679 expire = ticket_lifetime(ctx->context, ctx->ccache, ctx->principal,
680 server_str) / 2;
681 return expire + 1;
685 main (int argc, char **argv)
687 krb5_error_code ret;
688 krb5_context context;
689 krb5_ccache ccache;
690 krb5_principal principal;
691 int optidx = 0;
692 krb5_deltat ticket_life = 0;
693 int parseflags = 0;
695 setprogname (argv[0]);
697 ret = krb5_init_context (&context);
698 if (ret == KRB5_CONFIG_BADFORMAT)
699 errx (1, "krb5_init_context failed to parse configuration file");
700 else if (ret)
701 errx(1, "krb5_init_context failed: %d", ret);
703 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
704 usage(1);
706 if (help_flag)
707 usage (0);
709 if(version_flag) {
710 print_version(NULL);
711 exit(0);
714 argc -= optidx;
715 argv += optidx;
717 if (canonicalize_flag)
718 parseflags |= KRB5_PRINCIPAL_PARSE_ENTERPRISE;
720 if (argv[0]) {
721 ret = krb5_parse_name_flags (context, argv[0], parseflags, &principal);
722 if (ret)
723 krb5_err (context, 1, ret, "krb5_parse_name");
724 } else {
725 ret = krb5_get_default_principal (context, &principal);
726 if (ret)
727 krb5_err (context, 1, ret, "krb5_get_default_principal");
730 if(fcache_version)
731 krb5_set_fcache_version(context, fcache_version);
733 if(renewable_flag == -1)
734 /* this seems somewhat pointless, but whatever */
735 krb5_appdefault_boolean(context, "kinit",
736 krb5_principal_get_realm(context, principal),
737 "renewable", FALSE, &renewable_flag);
738 if(get_v4_tgt == -1)
739 krb5_appdefault_boolean(context, "kinit",
740 krb5_principal_get_realm(context, principal),
741 "krb4_get_tickets", FALSE, &get_v4_tgt);
742 if(do_afslog == -1)
743 krb5_appdefault_boolean(context, "kinit",
744 krb5_principal_get_realm(context, principal),
745 "afslog", TRUE, &do_afslog);
747 if(cred_cache)
748 ret = krb5_cc_resolve(context, cred_cache, &ccache);
749 else {
750 if(argc > 1) {
751 char s[1024];
752 ret = krb5_cc_gen_new(context, &krb5_fcc_ops, &ccache);
753 if(ret)
754 krb5_err(context, 1, ret, "creating cred cache");
755 snprintf(s, sizeof(s), "%s:%s",
756 krb5_cc_get_type(context, ccache),
757 krb5_cc_get_name(context, ccache));
758 setenv("KRB5CCNAME", s, 1);
759 if (get_v4_tgt) {
760 int fd;
761 if (asprintf(&krb4_cc_name, "%s_XXXXXX", TKT_ROOT) < 0)
762 krb5_errx(context, 1, "out of memory");
763 if((fd = mkstemp(krb4_cc_name)) >= 0) {
764 close(fd);
765 setenv("KRBTKFILE", krb4_cc_name, 1);
766 } else {
767 free(krb4_cc_name);
768 krb4_cc_name = NULL;
771 } else {
772 ret = krb5_cc_cache_match(context, principal, NULL, &ccache);
773 if (ret)
774 ret = krb5_cc_default (context, &ccache);
777 if (ret)
778 krb5_err (context, 1, ret, "resolving credentials cache");
780 if(argc > 1 && k_hasafs ())
781 k_setpag();
783 if (lifetime) {
784 int tmp = parse_time (lifetime, "s");
785 if (tmp < 0)
786 errx (1, "unparsable time: %s", lifetime);
788 ticket_life = tmp;
791 if(addrs_flag == 0 && extra_addresses.num_strings > 0)
792 krb5_errx(context, 1, "specifying both extra addresses and "
793 "no addresses makes no sense");
795 int i;
796 krb5_addresses addresses;
797 memset(&addresses, 0, sizeof(addresses));
798 for(i = 0; i < extra_addresses.num_strings; i++) {
799 ret = krb5_parse_address(context, extra_addresses.strings[i],
800 &addresses);
801 if (ret == 0) {
802 krb5_add_extra_addresses(context, &addresses);
803 krb5_free_addresses(context, &addresses);
806 free_getarg_strings(&extra_addresses);
809 if(renew_flag || validate_flag) {
810 ret = renew_validate(context, renew_flag, validate_flag,
811 ccache, server_str, ticket_life);
812 exit(ret != 0);
815 if(!convert_524)
816 get_new_tickets(context, principal, ccache, ticket_life, 1);
818 if(get_v4_tgt || convert_524)
819 do_524init(context, ccache, NULL, server_str);
820 if(do_afslog && k_hasafs())
821 krb5_afslog(context, ccache, NULL, NULL);
822 if(argc > 1) {
823 struct renew_ctx ctx;
824 time_t timeout;
826 timeout = ticket_lifetime(context, ccache, principal, server_str) / 2;
828 ctx.context = context;
829 ctx.ccache = ccache;
830 ctx.principal = principal;
831 ctx.ticket_life = ticket_life;
833 ret = simple_execvp_timed(argv[1], argv+1,
834 renew_func, &ctx, timeout);
835 #define EX_NOEXEC 126
836 #define EX_NOTFOUND 127
837 if(ret == EX_NOEXEC)
838 krb5_warnx(context, "permission denied: %s", argv[1]);
839 else if(ret == EX_NOTFOUND)
840 krb5_warnx(context, "command not found: %s", argv[1]);
842 krb5_cc_destroy(context, ccache);
843 _krb5_krb_dest_tkt(context, krb4_cc_name);
844 if(k_hasafs())
845 k_unlog();
846 } else {
847 krb5_cc_close (context, ccache);
848 ret = 0;
850 krb5_free_principal(context, principal);
851 krb5_free_context (context);
852 return ret;