Windows: Regenerated libasn1-exports.def
[heimdal.git] / kuser / kinit.c
blobe6201fbbc03894ac3f6a8168e7b31790d69f5a4e
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 struct krb5_dh_moduli;
43 struct AlgorithmIdentifier;
44 struct _krb5_krb_auth_data;
45 struct hx509_certs_data;
46 #include <krb5-private.h>
48 #ifndef NO_NTLM
49 #include "heimntlm.h"
50 #endif
52 int forwardable_flag = -1;
53 int proxiable_flag = -1;
54 int renewable_flag = -1;
55 int renew_flag = 0;
56 int pac_flag = -1;
57 int validate_flag = 0;
58 int version_flag = 0;
59 int help_flag = 0;
60 int addrs_flag = -1;
61 struct getarg_strings extra_addresses;
62 int anonymous_flag = 0;
63 char *lifetime = NULL;
64 char *renew_life = NULL;
65 char *server_str = NULL;
66 char *cred_cache = NULL;
67 char *start_str = NULL;
68 static int switch_cache_flags = 1;
69 struct getarg_strings etype_str;
70 int use_keytab = 0;
71 char *keytab_str = NULL;
72 int do_afslog = -1;
73 int fcache_version;
74 char *password_file = NULL;
75 char *pk_user_id = NULL;
76 int pk_enterprise_flag = 0;
77 struct hx509_certs_data *ent_user_id = NULL;
78 char *pk_x509_anchors = NULL;
79 int pk_use_enckey = 0;
80 static int canonicalize_flag = 0;
81 static int enterprise_flag = 0;
82 static int ok_as_delegate_flag = 0;
83 static int use_referrals_flag = 0;
84 static int windows_flag = 0;
85 #ifndef NO_NTLM
86 static char *ntlm_domain;
87 #endif
90 static struct getargs args[] = {
92 * used by MIT
93 * a: ~A
94 * V: verbose
95 * F: ~f
96 * P: ~p
97 * C: v4 cache name?
98 * 5:
100 * old flags
101 * 4:
102 * 9:
104 { "afslog", 0 , arg_flag, &do_afslog,
105 NP_("obtain afs tokens", "") },
107 { "cache", 'c', arg_string, &cred_cache,
108 NP_("credentials cache", ""), "cachename" },
110 { "forwardable", 'f', arg_negative_flag, &forwardable_flag,
111 NP_("get forwardable tickets", "")},
113 { "keytab", 't', arg_string, &keytab_str,
114 NP_("keytab to use", ""), "keytabname" },
116 { "lifetime", 'l', arg_string, &lifetime,
117 NP_("lifetime of tickets", ""), "time"},
119 { "proxiable", 'p', arg_flag, &proxiable_flag,
120 NP_("get proxiable tickets", "") },
122 { "renew", 'R', arg_flag, &renew_flag,
123 NP_("renew TGT", "") },
125 { "renewable", 0, arg_flag, &renewable_flag,
126 NP_("get renewable tickets", "") },
128 { "renewable-life", 'r', arg_string, &renew_life,
129 NP_("renewable lifetime of tickets", ""), "time" },
131 { "server", 'S', arg_string, &server_str,
132 NP_("server to get ticket for", ""), "principal" },
134 { "start-time", 's', arg_string, &start_str,
135 NP_("when ticket gets valid", ""), "time" },
137 { "use-keytab", 'k', arg_flag, &use_keytab,
138 NP_("get key from keytab", "") },
140 { "validate", 'v', arg_flag, &validate_flag,
141 NP_("validate TGT", "") },
143 { "enctypes", 'e', arg_strings, &etype_str,
144 NP_("encryption types to use", ""), "enctypes" },
146 { "fcache-version", 0, arg_integer, &fcache_version,
147 NP_("file cache version to create", "") },
149 { "addresses", 'A', arg_negative_flag, &addrs_flag,
150 NP_("request a ticket with no addresses", "") },
152 { "extra-addresses",'a', arg_strings, &extra_addresses,
153 NP_("include these extra addresses", ""), "addresses" },
155 { "anonymous", 0, arg_flag, &anonymous_flag,
156 NP_("request an anonymous ticket", "") },
158 { "request-pac", 0, arg_flag, &pac_flag,
159 NP_("request a Windows PAC", "") },
161 { "password-file", 0, arg_string, &password_file,
162 NP_("read the password from a file", "") },
164 { "canonicalize",0, arg_flag, &canonicalize_flag,
165 NP_("canonicalize client principal", "") },
167 { "enterprise",0, arg_flag, &enterprise_flag,
168 NP_("parse principal as a KRB5-NT-ENTERPRISE name", "") },
169 #ifdef PKINIT
170 { "pk-enterprise", 0, arg_flag, &pk_enterprise_flag,
171 NP_("use enterprise name from certificate", "") },
173 { "pk-user", 'C', arg_string, &pk_user_id,
174 NP_("principal's public/private/certificate identifier", ""), "id" },
176 { "x509-anchors", 'D', arg_string, &pk_x509_anchors,
177 NP_("directory with CA certificates", ""), "directory" },
179 { "pk-use-enckey", 0, arg_flag, &pk_use_enckey,
180 NP_("Use RSA encrypted reply (instead of DH)", "") },
181 #endif
182 #ifndef NO_NTLM
183 { "ntlm-domain", 0, arg_string, &ntlm_domain,
184 NP_("NTLM domain", ""), "domain" },
185 #endif
187 { "change-default", 0, arg_negative_flag, &switch_cache_flags,
188 NP_("switch the default cache to the new credentials cache", "") },
190 { "ok-as-delegate", 0, arg_flag, &ok_as_delegate_flag,
191 NP_("honor ok-as-delegate on tickets", "") },
193 { "use-referrals", 0, arg_flag, &use_referrals_flag,
194 NP_("only use referrals, no dns canalisation", "") },
196 { "windows", 0, arg_flag, &windows_flag,
197 NP_("get windows behavior", "") },
199 { "version", 0, arg_flag, &version_flag },
200 { "help", 0, arg_flag, &help_flag }
203 static void
204 usage (int ret)
206 arg_printusage_i18n (args,
207 sizeof(args)/sizeof(*args),
208 N_("Usage: ", ""),
209 NULL,
210 "[principal [command]]",
211 getarg_i18n);
212 exit (ret);
215 static krb5_error_code
216 get_server(krb5_context context,
217 krb5_principal client,
218 const char *server,
219 krb5_principal *princ)
221 krb5_const_realm realm;
222 if(server)
223 return krb5_parse_name(context, server, princ);
225 realm = krb5_principal_get_realm(context, client);
226 return krb5_make_principal(context, princ, realm,
227 KRB5_TGS_NAME, realm, NULL);
230 static int
231 renew_validate(krb5_context context,
232 int renew,
233 int validate,
234 krb5_ccache cache,
235 const char *server,
236 krb5_deltat life)
238 krb5_error_code ret;
239 krb5_creds in, *out = NULL;
240 krb5_kdc_flags flags;
242 memset(&in, 0, sizeof(in));
244 ret = krb5_cc_get_principal(context, cache, &in.client);
245 if(ret) {
246 krb5_warn(context, ret, "krb5_cc_get_principal");
247 return ret;
249 ret = get_server(context, in.client, server, &in.server);
250 if(ret) {
251 krb5_warn(context, ret, "get_server");
252 goto out;
255 if (renew) {
257 * no need to check the error here, it's only to be
258 * friendly to the user
260 krb5_get_credentials(context, KRB5_GC_CACHED, cache, &in, &out);
263 flags.i = 0;
264 flags.b.renewable = flags.b.renew = renew;
265 flags.b.validate = validate;
267 if (forwardable_flag != -1)
268 flags.b.forwardable = forwardable_flag;
269 else if (out)
270 flags.b.forwardable = out->flags.b.forwardable;
272 if (proxiable_flag != -1)
273 flags.b.proxiable = proxiable_flag;
274 else if (out)
275 flags.b.proxiable = out->flags.b.proxiable;
277 if (anonymous_flag)
278 flags.b.request_anonymous = anonymous_flag;
279 if(life)
280 in.times.endtime = time(NULL) + life;
282 if (out) {
283 krb5_free_creds (context, out);
284 out = NULL;
288 ret = krb5_get_kdc_cred(context,
289 cache,
290 flags,
291 NULL,
292 NULL,
293 &in,
294 &out);
295 if(ret) {
296 krb5_warn(context, ret, "krb5_get_kdc_cred");
297 goto out;
299 ret = krb5_cc_initialize(context, cache, in.client);
300 if(ret) {
301 krb5_free_creds (context, out);
302 krb5_warn(context, ret, "krb5_cc_initialize");
303 goto out;
305 ret = krb5_cc_store_cred(context, cache, out);
307 if(ret == 0 && server == NULL) {
308 /* only do this if it's a general renew-my-tgt request */
309 #ifndef NO_AFS
310 if(do_afslog && k_hasafs())
311 krb5_afslog(context, cache, NULL, NULL);
312 #endif
315 krb5_free_creds (context, out);
316 if(ret) {
317 krb5_warn(context, ret, "krb5_cc_store_cred");
318 goto out;
320 out:
321 krb5_free_cred_contents(context, &in);
322 return ret;
325 #ifndef NO_NTLM
327 static krb5_error_code
328 store_ntlmkey(krb5_context context, krb5_ccache id,
329 const char *domain, struct ntlm_buf *buf)
331 krb5_error_code ret;
332 krb5_data data;
333 char *name;
335 asprintf(&name, "ntlm-key-%s", domain);
336 if (name == NULL) {
337 krb5_clear_error_message(context);
338 return ENOMEM;
341 data.length = buf->length;
342 data.data = buf->data;
344 ret = krb5_cc_set_config(context, id, NULL, name, &data);
345 free(name);
346 return ret;
348 #endif
350 static krb5_error_code
351 get_new_tickets(krb5_context context,
352 krb5_principal principal,
353 krb5_ccache ccache,
354 krb5_deltat ticket_life,
355 int interactive)
357 krb5_error_code ret;
358 krb5_get_init_creds_opt *opt;
359 krb5_creds cred;
360 char passwd[256];
361 krb5_deltat start_time = 0;
362 krb5_deltat renew = 0;
363 char *renewstr = NULL;
364 krb5_enctype *enctype = NULL;
365 krb5_ccache tempccache;
366 #ifndef NO_NTLM
367 struct ntlm_buf ntlmkey;
368 memset(&ntlmkey, 0, sizeof(ntlmkey));
369 #endif
370 passwd[0] = '\0';
372 if (password_file) {
373 FILE *f;
375 if (strcasecmp("STDIN", password_file) == 0)
376 f = stdin;
377 else
378 f = fopen(password_file, "r");
379 if (f == NULL)
380 krb5_errx(context, 1, "Failed to open the password file %s",
381 password_file);
383 if (fgets(passwd, sizeof(passwd), f) == NULL)
384 krb5_errx(context, 1,
385 N_("Failed to read password from file %s", ""),
386 password_file);
387 if (f != stdin)
388 fclose(f);
389 passwd[strcspn(passwd, "\n")] = '\0';
392 #ifdef __APPLE__
393 if (passwd[0] == '\0') {
394 const char *realm;
395 OSStatus osret;
396 UInt32 length;
397 void *buffer;
398 char *name;
400 realm = krb5_principal_get_realm(context, principal);
402 ret = krb5_unparse_name_flags(context, principal,
403 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &name);
404 if (ret)
405 goto nopassword;
407 osret = SecKeychainFindGenericPassword(NULL, strlen(realm), realm,
408 strlen(name), name,
409 &length, &buffer, NULL);
410 free(name);
411 if (osret == noErr && length < sizeof(passwd) - 1) {
412 memcpy(passwd, buffer, length);
413 passwd[length] = '\0';
415 nopassword:
416 do { } while(0);
418 #endif
420 memset(&cred, 0, sizeof(cred));
422 ret = krb5_get_init_creds_opt_alloc (context, &opt);
423 if (ret)
424 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc");
426 krb5_get_init_creds_opt_set_default_flags(context, "kinit",
427 krb5_principal_get_realm(context, principal), opt);
429 if(forwardable_flag != -1)
430 krb5_get_init_creds_opt_set_forwardable (opt, forwardable_flag);
431 if(proxiable_flag != -1)
432 krb5_get_init_creds_opt_set_proxiable (opt, proxiable_flag);
433 if(anonymous_flag)
434 krb5_get_init_creds_opt_set_anonymous (opt, anonymous_flag);
435 if (pac_flag != -1)
436 krb5_get_init_creds_opt_set_pac_request(context, opt,
437 pac_flag ? TRUE : FALSE);
438 if (canonicalize_flag)
439 krb5_get_init_creds_opt_set_canonicalize(context, opt, TRUE);
440 if ((pk_enterprise_flag || enterprise_flag || canonicalize_flag) && windows_flag)
441 krb5_get_init_creds_opt_set_win2k(context, opt, TRUE);
442 if (pk_user_id || ent_user_id || anonymous_flag) {
443 ret = krb5_get_init_creds_opt_set_pkinit(context, opt,
444 principal,
445 pk_user_id,
446 pk_x509_anchors,
447 NULL,
448 NULL,
449 pk_use_enckey ? 2 : 0 |
450 anonymous_flag ? 4 : 0,
451 krb5_prompter_posix,
452 NULL,
453 passwd);
454 if (ret)
455 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_set_pkinit");
456 if (ent_user_id)
457 _krb5_get_init_creds_opt_set_pkinit_user_certs(context, opt, ent_user_id);
460 if (addrs_flag != -1)
461 krb5_get_init_creds_opt_set_addressless(context, opt,
462 addrs_flag ? FALSE : TRUE);
464 if (renew_life == NULL && renewable_flag)
465 renewstr = "1 month";
466 if (renew_life)
467 renewstr = renew_life;
468 if (renewstr) {
469 renew = parse_time (renewstr, "s");
470 if (renew < 0)
471 errx (1, "unparsable time: %s", renewstr);
473 krb5_get_init_creds_opt_set_renew_life (opt, renew);
476 if(ticket_life != 0)
477 krb5_get_init_creds_opt_set_tkt_life (opt, ticket_life);
479 if(start_str) {
480 int tmp = parse_time (start_str, "s");
481 if (tmp < 0)
482 errx (1, N_("unparsable time: %s", ""), start_str);
484 start_time = tmp;
487 if(etype_str.num_strings) {
488 int i;
490 enctype = malloc(etype_str.num_strings * sizeof(*enctype));
491 if(enctype == NULL)
492 errx(1, "out of memory");
493 for(i = 0; i < etype_str.num_strings; i++) {
494 ret = krb5_string_to_enctype(context,
495 etype_str.strings[i],
496 &enctype[i]);
497 if(ret)
498 errx(1, "unrecognized enctype: %s", etype_str.strings[i]);
500 krb5_get_init_creds_opt_set_etype_list(opt, enctype,
501 etype_str.num_strings);
504 if(use_keytab || keytab_str) {
505 krb5_keytab kt;
506 if(keytab_str)
507 ret = krb5_kt_resolve(context, keytab_str, &kt);
508 else
509 ret = krb5_kt_default(context, &kt);
510 if (ret)
511 krb5_err (context, 1, ret, "resolving keytab");
512 ret = krb5_get_init_creds_keytab (context,
513 &cred,
514 principal,
516 start_time,
517 server_str,
518 opt);
519 krb5_kt_close(context, kt);
520 } else if (pk_user_id || ent_user_id || anonymous_flag) {
521 ret = krb5_get_init_creds_password (context,
522 &cred,
523 principal,
524 passwd,
525 krb5_prompter_posix,
526 NULL,
527 start_time,
528 server_str,
529 opt);
530 } else if (!interactive) {
531 krb5_warnx(context, "Not interactive, failed to get initial ticket");
532 krb5_get_init_creds_opt_free(context, opt);
533 return 0;
534 } else {
536 if (passwd[0] == '\0') {
537 char *p, *prompt;
539 krb5_unparse_name (context, principal, &p);
540 asprintf (&prompt, N_("%s's Password: ", ""), p);
541 free (p);
543 if (UI_UTIL_read_pw_string(passwd, sizeof(passwd)-1, prompt, 0)){
544 memset(passwd, 0, sizeof(passwd));
545 exit(1);
547 free (prompt);
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);
561 krb5_get_init_creds_opt_free(context, opt);
562 #ifndef NO_NTLM
563 if (ntlm_domain && passwd[0])
564 heim_ntlm_nt_key(passwd, &ntlmkey);
565 #endif
566 memset(passwd, 0, sizeof(passwd));
568 switch(ret){
569 case 0:
570 break;
571 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */
572 exit(1);
573 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
574 case KRB5KRB_AP_ERR_MODIFIED:
575 case KRB5KDC_ERR_PREAUTH_FAILED:
576 krb5_errx(context, 1, N_("Password incorrect", ""));
577 break;
578 case KRB5KRB_AP_ERR_V4_REPLY:
579 krb5_errx(context, 1, N_("Looks like a Kerberos 4 reply", ""));
580 break;
581 default:
582 krb5_err(context, 1, ret, "krb5_get_init_creds");
585 if(ticket_life != 0) {
586 if(abs(cred.times.endtime - cred.times.starttime - ticket_life) > 30) {
587 char life[64];
588 unparse_time_approx(cred.times.endtime - cred.times.starttime,
589 life, sizeof(life));
590 krb5_warnx(context, N_("NOTICE: ticket lifetime is %s", ""), life);
593 if(renew_life) {
594 if(abs(cred.times.renew_till - cred.times.starttime - renew) > 30) {
595 char life[64];
596 unparse_time_approx(cred.times.renew_till - cred.times.starttime,
597 life, sizeof(life));
598 krb5_warnx(context,
599 N_("NOTICE: ticket renewable lifetime is %s", ""),
600 life);
604 ret = krb5_cc_new_unique(context, krb5_cc_get_type(context, ccache),
605 NULL, &tempccache);
606 if (ret)
607 krb5_err (context, 1, ret, "krb5_cc_new_unique");
609 ret = krb5_cc_initialize (context, tempccache, cred.client);
610 if (ret)
611 krb5_err (context, 1, ret, "krb5_cc_initialize");
613 ret = krb5_cc_store_cred (context, tempccache, &cred);
614 if (ret)
615 krb5_err (context, 1, ret, "krb5_cc_store_cred");
617 krb5_free_cred_contents (context, &cred);
619 ret = krb5_cc_move(context, tempccache, ccache);
620 if (ret)
621 krb5_err (context, 1, ret, "krb5_cc_move");
623 if (switch_cache_flags)
624 krb5_cc_switch(context, ccache);
626 #ifndef NO_NTLM
627 if (ntlm_domain && ntlmkey.data)
628 store_ntlmkey(context, ccache, ntlm_domain, &ntlmkey);
629 #endif
631 if (ok_as_delegate_flag || windows_flag || use_referrals_flag) {
632 unsigned char d = 0;
633 krb5_data data;
635 if (ok_as_delegate_flag || windows_flag)
636 d |= 1;
637 if (use_referrals_flag || windows_flag)
638 d |= 2;
640 data.length = 1;
641 data.data = &d;
643 krb5_cc_set_config(context, ccache, NULL, "realm-config", &data);
647 if (enctype)
648 free(enctype);
650 return 0;
653 static time_t
654 ticket_lifetime(krb5_context context, krb5_ccache cache,
655 krb5_principal client, const char *server)
657 krb5_creds in_cred, *cred;
658 krb5_error_code ret;
659 time_t timeout;
661 memset(&in_cred, 0, sizeof(in_cred));
663 ret = krb5_cc_get_principal(context, cache, &in_cred.client);
664 if(ret) {
665 krb5_warn(context, ret, "krb5_cc_get_principal");
666 return 0;
668 ret = get_server(context, in_cred.client, server, &in_cred.server);
669 if(ret) {
670 krb5_free_principal(context, in_cred.client);
671 krb5_warn(context, ret, "get_server");
672 return 0;
675 ret = krb5_get_credentials(context, KRB5_GC_CACHED,
676 cache, &in_cred, &cred);
677 krb5_free_principal(context, in_cred.client);
678 krb5_free_principal(context, in_cred.server);
679 if(ret) {
680 krb5_warn(context, ret, "krb5_get_credentials");
681 return 0;
683 timeout = cred->times.endtime - cred->times.starttime;
684 if (timeout < 0)
685 timeout = 0;
686 krb5_free_creds(context, cred);
687 return timeout;
690 struct renew_ctx {
691 krb5_context context;
692 krb5_ccache ccache;
693 krb5_principal principal;
694 krb5_deltat ticket_life;
697 static time_t
698 renew_func(void *ptr)
700 struct renew_ctx *ctx = ptr;
701 krb5_error_code ret;
702 time_t expire;
703 int new_tickets = 0;
705 if (renewable_flag) {
706 ret = renew_validate(ctx->context, renewable_flag, validate_flag,
707 ctx->ccache, server_str, ctx->ticket_life);
708 if (ret)
709 new_tickets = 1;
710 } else
711 new_tickets = 1;
713 if (new_tickets)
714 get_new_tickets(ctx->context, ctx->principal,
715 ctx->ccache, ctx->ticket_life, 0);
717 #ifndef NO_AFS
718 if(do_afslog && k_hasafs())
719 krb5_afslog(ctx->context, ctx->ccache, NULL, NULL);
720 #endif
722 expire = ticket_lifetime(ctx->context, ctx->ccache, ctx->principal,
723 server_str) / 2;
724 return expire + 1;
728 main (int argc, char **argv)
730 krb5_error_code ret;
731 krb5_context context;
732 krb5_ccache ccache;
733 krb5_principal principal;
734 int optidx = 0;
735 krb5_deltat ticket_life = 0;
736 int parseflags = 0;
738 setprogname (argv[0]);
740 setlocale (LC_ALL, "");
741 bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR);
742 textdomain("heimdal_kuser");
744 ret = krb5_init_context (&context);
745 if (ret == KRB5_CONFIG_BADFORMAT)
746 errx (1, "krb5_init_context failed to parse configuration file");
747 else if (ret)
748 errx(1, "krb5_init_context failed: %d", ret);
750 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
751 usage(1);
753 if (help_flag)
754 usage (0);
756 if(version_flag) {
757 print_version(NULL);
758 exit(0);
761 argc -= optidx;
762 argv += optidx;
764 if (canonicalize_flag || enterprise_flag)
765 parseflags |= KRB5_PRINCIPAL_PARSE_ENTERPRISE;
767 if (pk_enterprise_flag) {
768 ret = _krb5_pk_enterprise_cert(context, pk_user_id,
769 argv[0], &principal,
770 &ent_user_id);
771 if (ret)
772 krb5_err(context, 1, ret, "krb5_pk_enterprise_certs");
774 pk_user_id = NULL;
776 } else if (anonymous_flag) {
778 ret = krb5_make_principal(context, &principal, argv[0],
779 KRB5_WELLKNOWN_NAME, KRB5_ANON_NAME,
780 NULL);
781 if (ret)
782 krb5_err(context, 1, ret, "krb5_make_principal");
783 krb5_principal_set_type(context, principal, KRB5_NT_WELLKNOWN);
785 } else {
786 if (argv[0]) {
787 ret = krb5_parse_name_flags (context, argv[0], parseflags,
788 &principal);
789 if (ret)
790 krb5_err (context, 1, ret, "krb5_parse_name");
791 } else {
792 ret = krb5_get_default_principal (context, &principal);
793 if (ret)
794 krb5_err (context, 1, ret, "krb5_get_default_principal");
798 if(fcache_version)
799 krb5_set_fcache_version(context, fcache_version);
801 if(renewable_flag == -1)
802 /* this seems somewhat pointless, but whatever */
803 krb5_appdefault_boolean(context, "kinit",
804 krb5_principal_get_realm(context, principal),
805 "renewable", FALSE, &renewable_flag);
806 if(do_afslog == -1)
807 krb5_appdefault_boolean(context, "kinit",
808 krb5_principal_get_realm(context, principal),
809 "afslog", TRUE, &do_afslog);
811 if(cred_cache)
812 ret = krb5_cc_resolve(context, cred_cache, &ccache);
813 else {
814 if(argc > 1) {
815 char s[1024];
816 ret = krb5_cc_new_unique(context, NULL, NULL, &ccache);
817 if(ret)
818 krb5_err(context, 1, ret, "creating cred cache");
819 snprintf(s, sizeof(s), "%s:%s",
820 krb5_cc_get_type(context, ccache),
821 krb5_cc_get_name(context, ccache));
822 setenv("KRB5CCNAME", s, 1);
823 } else {
824 ret = krb5_cc_cache_match(context, principal, &ccache);
825 if (ret) {
826 const char *type;
827 ret = krb5_cc_default (context, &ccache);
828 if (ret)
829 krb5_err (context, 1, ret, N_("resolving credentials cache", ""));
832 * Check if the type support switching, and we do,
833 * then do that instead over overwriting the current
834 * default credential
836 type = krb5_cc_get_type(context, ccache);
837 if (krb5_cc_support_switch(context, type)) {
838 krb5_cc_close(context, ccache);
839 ret = krb5_cc_new_unique(context, type, NULL, &ccache);
844 if (ret)
845 krb5_err (context, 1, ret, N_("resolving credentials cache", ""));
847 #ifndef NO_AFS
848 if(argc > 1 && k_hasafs ())
849 k_setpag();
850 #endif
852 if (lifetime) {
853 int tmp = parse_time (lifetime, "s");
854 if (tmp < 0)
855 errx (1, N_("unparsable time: %s", ""), lifetime);
857 ticket_life = tmp;
860 if(addrs_flag == 0 && extra_addresses.num_strings > 0)
861 krb5_errx(context, 1,
862 N_("specifying both extra addresses and "
863 "no addresses makes no sense", ""));
865 int i;
866 krb5_addresses addresses;
867 memset(&addresses, 0, sizeof(addresses));
868 for(i = 0; i < extra_addresses.num_strings; i++) {
869 ret = krb5_parse_address(context, extra_addresses.strings[i],
870 &addresses);
871 if (ret == 0) {
872 krb5_add_extra_addresses(context, &addresses);
873 krb5_free_addresses(context, &addresses);
876 free_getarg_strings(&extra_addresses);
879 if(renew_flag || validate_flag) {
880 ret = renew_validate(context, renew_flag, validate_flag,
881 ccache, server_str, ticket_life);
882 exit(ret != 0);
885 get_new_tickets(context, principal, ccache, ticket_life, 1);
887 #ifndef NO_AFS
888 if(do_afslog && k_hasafs())
889 krb5_afslog(context, ccache, NULL, NULL);
890 #endif
891 if(argc > 1) {
892 struct renew_ctx ctx;
893 time_t timeout;
895 timeout = ticket_lifetime(context, ccache, principal, server_str) / 2;
897 ctx.context = context;
898 ctx.ccache = ccache;
899 ctx.principal = principal;
900 ctx.ticket_life = ticket_life;
902 ret = simple_execvp_timed(argv[1], argv+1,
903 renew_func, &ctx, timeout);
904 #define EX_NOEXEC 126
905 #define EX_NOTFOUND 127
906 if(ret == EX_NOEXEC)
907 krb5_warnx(context, N_("permission denied: %s", ""), argv[1]);
908 else if(ret == EX_NOTFOUND)
909 krb5_warnx(context, N_("command not found: %s", ""), argv[1]);
911 krb5_cc_destroy(context, ccache);
912 #ifndef NO_AFS
913 if(k_hasafs())
914 k_unlog();
915 #endif
916 } else {
917 krb5_cc_close (context, ccache);
918 ret = 0;
920 krb5_free_principal(context, principal);
921 krb5_free_context (context);
922 return ret;