1 /* 2 * Copyright (c) 1997-2002 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 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. 16 * 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. 20 * 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. 32 */ 33 34 #include "kuser_locl.h" 35 RCSID("$Id: kinit.c,v 1.89 2002/08/21 12:21:31 joda Exp $"); 36 37 int forwardable_flag = -1; 38 int proxiable_flag = -1; 39 int renewable_flag = -1; 40 int renew_flag = 0; 41 int validate_flag = 0; 42 int version_flag = 0; 43 int help_flag = 0; 44 int addrs_flag = 1; 45 struct getarg_strings extra_addresses; 46 int anonymous_flag = 0; 47 char *lifetime = NULL; 48 char *renew_life = NULL; 49 char *server = NULL; 50 char *cred_cache = NULL; 51 char *start_str = NULL; 52 struct getarg_strings etype_str; 53 int use_keytab = 0; 54 char *keytab_str = NULL; 55 #ifdef KRB4 56 int get_v4_tgt = -1; 57 int do_afslog = -1; 58 int convert_524; 59 #endif 60 int fcache_version; 61 62 static struct getargs args[] = { 63 #ifdef KRB4 64 { "524init", '4', arg_flag, &get_v4_tgt, 65 "obtain version 4 TGT" }, 66 67 { "524convert", '9', arg_flag, &convert_524, 68 "only convert ticket to version 4" }, 69 70 { "afslog", 0 , arg_flag, &do_afslog, 71 "obtain afs tokens" }, 72 #endif 73 { "cache", 'c', arg_string, &cred_cache, 74 "credentials cache", "cachename" }, 75 76 { "forwardable", 'f', arg_flag, &forwardable_flag, 77 "get forwardable tickets"}, 78 79 { "keytab", 't', arg_string, &keytab_str, 80 "keytab to use", "keytabname" }, 81 82 { "lifetime", 'l', arg_string, &lifetime, 83 "lifetime of tickets", "time"}, 84 85 { "proxiable", 'p', arg_flag, &proxiable_flag, 86 "get proxiable tickets" }, 87 88 { "renew", 'R', arg_flag, &renew_flag, 89 "renew TGT" }, 90 91 { "renewable", 0, arg_flag, &renewable_flag, 92 "get renewable tickets" }, 93 94 { "renewable-life", 'r', arg_string, &renew_life, 95 "renewable lifetime of tickets", "time" }, 96 97 { "server", 'S', arg_string, &server, 98 "server to get ticket for", "principal" }, 99 100 { "start-time", 's', arg_string, &start_str, 101 "when ticket gets valid", "time" }, 102 103 { "use-keytab", 'k', arg_flag, &use_keytab, 104 "get key from keytab" }, 105 106 { "validate", 'v', arg_flag, &validate_flag, 107 "validate TGT" }, 108 109 { "enctypes", 'e', arg_strings, &etype_str, 110 "encryption types to use", "enctypes" }, 111 112 { "fcache-version", 0, arg_integer, &fcache_version, 113 "file cache version to create" }, 114 115 { "addresses", 0, arg_negative_flag, &addrs_flag, 116 "request a ticket with no addresses" }, 117 118 { "extra-addresses",'a', arg_strings, &extra_addresses, 119 "include these extra addresses", "addresses" }, 120 121 { "anonymous", 0, arg_flag, &anonymous_flag, 122 "request an anonymous ticket" }, 123 124 { "version", 0, arg_flag, &version_flag }, 125 { "help", 0, arg_flag, &help_flag } 126 }; 127 128 static void 129 usage (int ret) 130 { 131 arg_printusage (args, 132 sizeof(args)/sizeof(*args), 133 NULL, 134 "[principal [command]]"); 135 exit (ret); 136 } 137 138 #ifdef KRB4 139 /* for when the KDC tells us it's a v4 one, we try to talk that */ 140 141 static int 142 key_to_key(const char *user, 143 char *instance, 144 const char *realm, 145 const void *arg, 146 des_cblock *key) 147 { 148 memcpy(key, arg, sizeof(des_cblock)); 149 return 0; 150 } 151 152 static int 153 do_v4_fallback (krb5_context context, 154 const krb5_principal principal, 155 int lifetime, 156 int use_srvtab, const char *srvtab_str, 157 const char *passwd) 158 { 159 int ret; 160 krb_principal princ; 161 des_cblock key; 162 krb5_error_code kret; 163 164 if (lifetime == 0) 165 lifetime = DEFAULT_TKT_LIFE; 166 else 167 lifetime = krb_time_to_life (0, lifetime); 168 169 kret = krb5_524_conv_principal (context, principal, 170 princ.name, 171 princ.instance, 172 princ.realm); 173 if (kret) { 174 krb5_warn (context, kret, "krb5_524_conv_principal"); 175 return 1; 176 } 177 178 if (use_srvtab || srvtab_str) { 179 if (srvtab_str == NULL) 180 srvtab_str = KEYFILE; 181 182 ret = read_service_key (princ.name, princ.instance, princ.realm, 183 0, srvtab_str, (char *)&key); 184 if (ret) { 185 warnx ("read_service_key %s: %s", srvtab_str, 186 krb_get_err_text (ret)); 187 return 1; 188 } 189 ret = krb_get_in_tkt (princ.name, princ.instance, princ.realm, 190 KRB_TICKET_GRANTING_TICKET, princ.realm, 191 lifetime, key_to_key, NULL, key); 192 } else { 193 ret = krb_get_pw_in_tkt(princ.name, princ.instance, princ.realm, 194 KRB_TICKET_GRANTING_TICKET, princ.realm, 195 lifetime, passwd); 196 } 197 memset (key, 0, sizeof(key)); 198 if (ret) { 199 warnx ("%s", krb_get_err_text(ret)); 200 return 1; 201 } 202 if (do_afslog && k_hasafs()) { 203 if ((ret = krb_afslog(NULL, NULL)) != 0 && ret != KDC_PR_UNKNOWN) { 204 if(ret > 0) 205 warnx ("%s", krb_get_err_text(ret)); 206 else 207 warnx ("failed to store AFS token"); 208 } 209 } 210 return 0; 211 } 212 213 214 /* 215 * the special version of get_default_principal that takes v4 into account 216 */ 217 218 static krb5_error_code 219 kinit_get_default_principal (krb5_context context, 220 krb5_principal *princ) 221 { 222 krb5_error_code ret; 223 krb5_ccache id; 224 krb_principal v4_princ; 225 int kret; 226 227 ret = krb5_cc_default (context, &id); 228 if (ret == 0) { 229 ret = krb5_cc_get_principal (context, id, princ); 230 krb5_cc_close (context, id); 231 if (ret == 0) 232 return 0; 233 } 234 235 kret = krb_get_tf_fullname (tkt_string(), 236 v4_princ.name, 237 v4_princ.instance, 238 v4_princ.realm); 239 if (kret == KSUCCESS) { 240 ret = krb5_425_conv_principal (context, 241 v4_princ.name, 242 v4_princ.instance, 243 v4_princ.realm, 244 princ); 245 if (ret == 0) 246 return 0; 247 } 248 return krb5_get_default_principal (context, princ); 249 } 250 251 #else /* !KRB4 */ 252 253 static krb5_error_code 254 kinit_get_default_principal (krb5_context context, 255 krb5_principal *princ) 256 { 257 return krb5_get_default_principal (context, princ); 258 } 259 260 #endif /* !KRB4 */ 261 262 static krb5_error_code 263 get_server(krb5_context context, 264 krb5_principal client, 265 const char *server, 266 krb5_principal *princ) 267 { 268 krb5_realm *client_realm; 269 if(server) 270 return krb5_parse_name(context, server, princ); 271 272 client_realm = krb5_princ_realm (context, client); 273 return krb5_make_principal(context, princ, *client_realm, 274 KRB5_TGS_NAME, *client_realm, NULL); 275 } 276 277 #ifdef KRB4 278 static krb5_error_code 279 do_524init(krb5_context context, krb5_ccache ccache, 280 krb5_creds *creds, const char *server) 281 { 282 krb5_error_code ret; 283 CREDENTIALS c; 284 krb5_creds in_creds, *real_creds; 285 286 if(creds != NULL) 287 real_creds = creds; 288 else { 289 krb5_principal client; 290 krb5_cc_get_principal(context, ccache, &client); 291 memset(&in_creds, 0, sizeof(in_creds)); 292 ret = get_server(context, client, server, &in_creds.server); 293 if(ret) 294 return ret; 295 ret = krb5_get_credentials(context, 0, ccache, &in_creds, &real_creds); 296 if(ret) 297 return ret; 298 } 299 ret = krb524_convert_creds_kdc_ccache(context, ccache, real_creds, &c); 300 if(ret) 301 krb5_warn(context, ret, "converting creds"); 302 else { 303 int tret = tf_setup(&c, c.pname, c.pinst); 304 if(tret) 305 krb5_warnx(context, "saving v4 creds: %s", krb_get_err_text(tret)); 306 } 307 308 if(creds == NULL) 309 krb5_free_creds(context, real_creds); 310 memset(&c, 0, sizeof(c)); 311 312 return ret; 313 } 314 #endif 315 316 static int 317 renew_validate(krb5_context context, 318 int renew, 319 int validate, 320 krb5_ccache cache, 321 const char *server, 322 krb5_deltat life) 323 { 324 krb5_error_code ret; 325 krb5_creds in, *out; 326 krb5_kdc_flags flags; 327 328 memset(&in, 0, sizeof(in)); 329 330 ret = krb5_cc_get_principal(context, cache, &in.client); 331 if(ret) { 332 krb5_warn(context, ret, "krb5_cc_get_principal"); 333 return ret; 334 } 335 ret = get_server(context, in.client, server, &in.server); 336 if(ret) { 337 krb5_warn(context, ret, "get_server"); 338 goto out; 339 } 340 flags.i = 0; 341 flags.b.renewable = flags.b.renew = renew; 342 flags.b.validate = validate; 343 if (forwardable_flag != -1) 344 flags.b.forwardable = forwardable_flag; 345 if (proxiable_flag != -1) 346 flags.b.proxiable = proxiable_flag; 347 if (anonymous_flag != -1) 348 flags.b.request_anonymous = anonymous_flag; 349 if(life) 350 in.times.endtime = time(NULL) + life; 351 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; 362 } 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; 368 } 369 ret = krb5_cc_store_cred(context, cache, out); 370 371 #ifdef KRB4 372 if(ret == 0 && server == NULL) { 373 /* only do this if it's a general renew-my-tgt request */ 374 if(get_v4_tgt) 375 do_524init(context, cache, out, NULL); 376 377 if(do_afslog && k_hasafs()) 378 krb5_afslog(context, cache, NULL, NULL); 379 } 380 #endif 381 382 krb5_free_creds (context, out); 383 if(ret) { 384 krb5_warn(context, ret, "krb5_cc_store_cred"); 385 goto out; 386 } 387 out: 388 krb5_free_creds_contents(context, &in); 389 return ret; 390 } 391 392 static krb5_error_code 393 get_new_tickets(krb5_context context, 394 krb5_principal principal, 395 krb5_ccache ccache, 396 krb5_deltat ticket_life) 397 { 398 krb5_error_code ret; 399 krb5_get_init_creds_opt opt; 400 krb5_addresses no_addrs; 401 krb5_creds cred; 402 char passwd[256]; 403 krb5_deltat start_time = 0; 404 krb5_deltat renew = 0; 405 406 memset(&cred, 0, sizeof(cred)); 407 408 krb5_get_init_creds_opt_init (&opt); 409 410 krb5_get_init_creds_opt_set_default_flags(context, "kinit", 411 /* XXX */principal->realm, &opt); 412 413 if(forwardable_flag != -1) 414 krb5_get_init_creds_opt_set_forwardable (&opt, forwardable_flag); 415 if(proxiable_flag != -1) 416 krb5_get_init_creds_opt_set_proxiable (&opt, proxiable_flag); 417 if(anonymous_flag != -1) 418 krb5_get_init_creds_opt_set_anonymous (&opt, anonymous_flag); 419 420 if (!addrs_flag) { 421 no_addrs.len = 0; 422 no_addrs.val = NULL; 423 424 krb5_get_init_creds_opt_set_address_list (&opt, &no_addrs); 425 } 426 427 if(renew_life) { 428 renew = parse_time (renew_life, "s"); 429 if (renew < 0) 430 errx (1, "unparsable time: %s", renew_life); 431 432 krb5_get_init_creds_opt_set_renew_life (&opt, renew); 433 } else if (renewable_flag == 1) 434 krb5_get_init_creds_opt_set_renew_life (&opt, 1 << 30); 435 436 437 if(ticket_life != 0) 438 krb5_get_init_creds_opt_set_tkt_life (&opt, ticket_life); 439 440 if(start_str) { 441 int tmp = parse_time (start_str, "s"); 442 if (tmp < 0) 443 errx (1, "unparsable time: %s", start_str); 444 445 start_time = tmp; 446 } 447 448 if(etype_str.num_strings) { 449 krb5_enctype *enctype = NULL; 450 int i; 451 enctype = malloc(etype_str.num_strings * sizeof(*enctype)); 452 if(enctype == NULL) 453 errx(1, "out of memory"); 454 for(i = 0; i < etype_str.num_strings; i++) { 455 ret = krb5_string_to_enctype(context, 456 etype_str.strings[i], 457 &enctype[i]); 458 if(ret) 459 errx(1, "unrecognized enctype: %s", etype_str.strings[i]); 460 } 461 krb5_get_init_creds_opt_set_etype_list(&opt, enctype, 462 etype_str.num_strings); 463 } 464 465 if(use_keytab || keytab_str) { 466 krb5_keytab kt; 467 if(keytab_str) 468 ret = krb5_kt_resolve(context, keytab_str, &kt); 469 else 470 ret = krb5_kt_default(context, &kt); 471 if (ret) 472 krb5_err (context, 1, ret, "resolving keytab"); 473 ret = krb5_get_init_creds_keytab (context, 474 &cred, 475 principal, 476 kt, 477 start_time, 478 server, 479 &opt); 480 krb5_kt_close(context, kt); 481 } else { 482 char *p, *prompt; 483 484 krb5_unparse_name (context, principal, &p); 485 asprintf (&prompt, "%s's Password: ", p); 486 free (p); 487 488 if (des_read_pw_string(passwd, sizeof(passwd)-1, prompt, 0)){ 489 memset(passwd, 0, sizeof(passwd)); 490 exit(1); 491 } 492 493 free (prompt); 494 495 ret = krb5_get_init_creds_password (context, 496 &cred, 497 principal, 498 passwd, 499 krb5_prompter_posix, 500 NULL, 501 start_time, 502 server, 503 &opt); 504 } 505 #ifdef KRB4 506 if (ret == KRB5KRB_AP_ERR_V4_REPLY || ret == KRB5_KDC_UNREACH) { 507 int exit_val; 508 509 exit_val = do_v4_fallback (context, principal, ticket_life, 510 use_keytab, keytab_str, passwd); 511 get_v4_tgt = 0; 512 do_afslog = 0; 513 memset(passwd, 0, sizeof(passwd)); 514 if (exit_val == 0 || ret == KRB5KRB_AP_ERR_V4_REPLY) 515 return exit_val; 516 } 517 #endif 518 memset(passwd, 0, sizeof(passwd)); 519 520 switch(ret){ 521 case 0: 522 break; 523 case KRB5_LIBOS_PWDINTR: /* don't print anything if it was just C-c:ed */ 524 exit(1); 525 case KRB5KRB_AP_ERR_BAD_INTEGRITY: 526 case KRB5KRB_AP_ERR_MODIFIED: 527 krb5_errx(context, 1, "Password incorrect"); 528 break; 529 default: 530 krb5_err(context, 1, ret, "krb5_get_init_creds"); 531 } 532 533 if(ticket_life != 0) { 534 if(abs(cred.times.endtime - cred.times.starttime - ticket_life) > 30) { 535 char life[32]; 536 unparse_time(cred.times.endtime - cred.times.starttime, 537 life, sizeof(life)); 538 krb5_warnx(context, "NOTICE: ticket lifetime is %s", life); 539 } 540 } 541 if(renew != 0) { 542 if(abs(cred.times.renew_till - cred.times.starttime - renew) > 30) { 543 char life[32]; 544 unparse_time(cred.times.renew_till - cred.times.starttime, 545 life, sizeof(life)); 546 krb5_warnx(context, "NOTICE: ticket renewable lifetime is %s", 547 life); 548 } 549 } 550 551 ret = krb5_cc_initialize (context, ccache, cred.client); 552 if (ret) 553 krb5_err (context, 1, ret, "krb5_cc_initialize"); 554 555 ret = krb5_cc_store_cred (context, ccache, &cred); 556 if (ret) 557 krb5_err (context, 1, ret, "krb5_cc_store_cred"); 558 559 krb5_free_creds_contents (context, &cred); 560 561 return 0; 562 } 563 564 int 565 main (int argc, char **argv) 566 { 567 krb5_error_code ret; 568 krb5_context context; 569 krb5_ccache ccache; 570 krb5_principal principal; 571 int optind = 0; 572 krb5_deltat ticket_life = 0; 573 574 setprogname (argv[0]); 575 576 ret = krb5_init_context (&context); 577 if (ret) 578 errx(1, "krb5_init_context failed: %d", ret); 579 580 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind)) 581 usage(1); 582 583 if (help_flag) 584 usage (0); 585 586 if(version_flag) { 587 print_version(NULL); 588 exit(0); 589 } 590 591 argc -= optind; 592 argv += optind; 593 594 if (argv[0]) { 595 ret = krb5_parse_name (context, argv[0], &principal); 596 if (ret) 597 krb5_err (context, 1, ret, "krb5_parse_name"); 598 } else { 599 ret = kinit_get_default_principal (context, &principal); 600 if (ret) 601 krb5_err (context, 1, ret, "krb5_get_default_principal"); 602 } 603 604 if(fcache_version) 605 krb5_set_fcache_version(context, fcache_version); 606 607 if(cred_cache) 608 ret = krb5_cc_resolve(context, cred_cache, &ccache); 609 else { 610 if(argc > 1) { 611 char s[1024]; 612 ret = krb5_cc_gen_new(context, &krb5_fcc_ops, &ccache); 613 if(ret) 614 krb5_err(context, 1, ret, "creating cred cache"); 615 snprintf(s, sizeof(s), "%s:%s", 616 krb5_cc_get_type(context, ccache), 617 krb5_cc_get_name(context, ccache)); 618 setenv("KRB5CCNAME", s, 1); 619 #ifdef KRB4 620 { 621 int fd; 622 snprintf(s, sizeof(s), "%s_XXXXXX", TKT_ROOT); 623 if((fd = mkstemp(s)) >= 0) { 624 close(fd); 625 setenv("KRBTKFILE", s, 1); 626 if (k_hasafs ()) 627 k_setpag(); 628 } 629 } 630 #endif 631 } else 632 ret = krb5_cc_default (context, &ccache); 633 } 634 if (ret) 635 krb5_err (context, 1, ret, "resolving credentials cache"); 636 637 if (lifetime) { 638 int tmp = parse_time (lifetime, "s"); 639 if (tmp < 0) 640 errx (1, "unparsable time: %s", lifetime); 641 642 ticket_life = tmp; 643 } 644 #ifdef KRB4 645 if(get_v4_tgt == -1) 646 krb5_appdefault_boolean(context, "kinit", 647 krb5_principal_get_realm(context, principal), 648 "krb4_get_tickets", TRUE, &get_v4_tgt); 649 if(do_afslog == -1) 650 krb5_appdefault_boolean(context, "kinit", 651 krb5_principal_get_realm(context, principal), 652 "afslog", TRUE, &do_afslog); 653 #endif 654 655 if(!addrs_flag && extra_addresses.num_strings > 0) 656 krb5_errx(context, 1, "specifying both extra addresses and " 657 "no addresses makes no sense"); 658 { 659 int i; 660 krb5_addresses addresses; 661 memset(&addresses, 0, sizeof(addresses)); 662 for(i = 0; i < extra_addresses.num_strings; i++) { 663 ret = krb5_parse_address(context, extra_addresses.strings[i], 664 &addresses); 665 if (ret == 0) { 666 krb5_add_extra_addresses(context, &addresses); 667 krb5_free_addresses(context, &addresses); 668 } 669 } 670 free_getarg_strings(&extra_addresses); 671 } 672 673 674 if(renew_flag || validate_flag) { 675 ret = renew_validate(context, renew_flag, validate_flag, 676 ccache, server, ticket_life); 677 exit(ret != 0); 678 } 679 680 #ifdef KRB4 681 if(!convert_524) 682 #endif 683 get_new_tickets(context, principal, ccache, ticket_life); 684 685 #ifdef KRB4 686 if(get_v4_tgt) 687 do_524init(context, ccache, NULL, server); 688 if(do_afslog && k_hasafs()) 689 krb5_afslog(context, ccache, NULL, NULL); 690 #endif 691 if(argc > 1) { 692 simple_execvp(argv[1], argv+1); 693 krb5_cc_destroy(context, ccache); 694 #ifdef KRB4 695 dest_tkt(); 696 if(k_hasafs()) 697 k_unlog(); 698 #endif 699 } else 700 krb5_cc_close (context, ccache); 701 krb5_free_principal(context, principal); 702 krb5_free_context (context); 703 return 0; 704 } 705