1 /* $NetBSD: util.c,v 1.4 2017/01/28 21:31:44 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan 5 * (Royal Institute of Technology, Stockholm, Sweden). 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 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. 18 * 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. 22 * 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. 34 */ 35 36 #include "kadmin_locl.h" 37 #include <krb5/parse_units.h> 38 39 /* 40 * util.c - functions for parsing, unparsing, and editing different 41 * types of data used in kadmin. 42 */ 43 44 static int 45 get_response(const char *prompt, const char *def, char *buf, size_t len); 46 47 /* 48 * attributes 49 */ 50 51 struct units kdb_attrs[] = { 52 { "allow-digest", KRB5_KDB_ALLOW_DIGEST }, 53 { "allow-kerberos4", KRB5_KDB_ALLOW_KERBEROS4 }, 54 { "trusted-for-delegation", KRB5_KDB_TRUSTED_FOR_DELEGATION }, 55 { "ok-as-delegate", KRB5_KDB_OK_AS_DELEGATE }, 56 { "new-princ", KRB5_KDB_NEW_PRINC }, 57 { "support-desmd5", KRB5_KDB_SUPPORT_DESMD5 }, 58 { "pwchange-service", KRB5_KDB_PWCHANGE_SERVICE }, 59 { "disallow-svr", KRB5_KDB_DISALLOW_SVR }, 60 { "requires-pw-change", KRB5_KDB_REQUIRES_PWCHANGE }, 61 { "requires-hw-auth", KRB5_KDB_REQUIRES_HW_AUTH }, 62 { "requires-pre-auth", KRB5_KDB_REQUIRES_PRE_AUTH }, 63 { "disallow-all-tix", KRB5_KDB_DISALLOW_ALL_TIX }, 64 { "disallow-dup-skey", KRB5_KDB_DISALLOW_DUP_SKEY }, 65 { "disallow-proxiable", KRB5_KDB_DISALLOW_PROXIABLE }, 66 { "disallow-renewable", KRB5_KDB_DISALLOW_RENEWABLE }, 67 { "disallow-tgt-based", KRB5_KDB_DISALLOW_TGT_BASED }, 68 { "disallow-forwardable", KRB5_KDB_DISALLOW_FORWARDABLE }, 69 { "disallow-postdated", KRB5_KDB_DISALLOW_POSTDATED }, 70 { NULL, 0 } 71 }; 72 73 /* 74 * convert the attributes in `attributes' into a printable string 75 * in `str, len' 76 */ 77 78 void 79 attributes2str(krb5_flags attributes, char *str, size_t len) 80 { 81 unparse_flags (attributes, kdb_attrs, str, len); 82 } 83 84 /* 85 * convert the string in `str' into attributes in `flags' 86 * return 0 if parsed ok, else -1. 87 */ 88 89 int 90 str2attributes(const char *str, krb5_flags *flags) 91 { 92 int res; 93 94 res = parse_flags (str, kdb_attrs, *flags); 95 if (res < 0) 96 return res; 97 else { 98 *flags = res; 99 return 0; 100 } 101 } 102 103 /* 104 * try to parse the string `resp' into attributes in `attr', also 105 * setting the `bit' in `mask' if attributes are given and valid. 106 */ 107 108 int 109 parse_attributes (const char *resp, krb5_flags *attr, int *mask, int bit) 110 { 111 krb5_flags tmp = *attr; 112 113 if (str2attributes(resp, &tmp) == 0) { 114 *attr = tmp; 115 if (mask) 116 *mask |= bit; 117 return 0; 118 } else if(*resp == '?') { 119 print_flags_table (kdb_attrs, stderr); 120 } else { 121 fprintf (stderr, "Unable to parse \"%s\"\n", resp); 122 } 123 return -1; 124 } 125 126 /* 127 * allow the user to edit the attributes in `attr', prompting with `prompt' 128 */ 129 130 int 131 edit_attributes (const char *prompt, krb5_flags *attr, int *mask, int bit) 132 { 133 char buf[1024], resp[1024]; 134 135 if (mask && (*mask & bit)) 136 return 0; 137 138 attributes2str(*attr, buf, sizeof(buf)); 139 for (;;) { 140 if(get_response("Attributes", buf, resp, sizeof(resp)) != 0) 141 return 1; 142 if (resp[0] == '\0') 143 break; 144 if (parse_attributes (resp, attr, mask, bit) == 0) 145 break; 146 } 147 return 0; 148 } 149 150 /* 151 * try to parse the string `resp' into policy in `attr', also 152 * setting the `bit' in `mask' if attributes are given and valid. 153 */ 154 155 #define VALID_POLICY_NAME_CHARS \ 156 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_" 157 158 int 159 parse_policy (const char *resp, char **policy, int *mask, int bit) 160 { 161 if (strspn(resp, VALID_POLICY_NAME_CHARS) == strlen(resp) && 162 *resp != '\0') { 163 164 *policy = strdup(resp); 165 if (*policy == NULL) { 166 fprintf (stderr, "Out of memory"); 167 return -1; 168 } 169 if (mask) 170 *mask |= bit; 171 return 0; 172 } else if(*resp == '?') { 173 print_flags_table (kdb_attrs, stderr); 174 } else { 175 fprintf (stderr, "Unable to parse \"%s\"\n", resp); 176 } 177 return -1; 178 } 179 180 /* 181 * allow the user to edit the attributes in `attr', prompting with `prompt' 182 */ 183 184 int 185 edit_policy (const char *prompt, char **policy, int *mask, int bit) 186 { 187 char buf[1024], resp[1024]; 188 189 if (mask && (*mask & bit)) 190 return 0; 191 192 buf[0] = '\0'; 193 strlcpy(buf, "default", sizeof (buf)); 194 for (;;) { 195 if(get_response("Policy", buf, resp, sizeof(resp)) != 0) 196 return 1; 197 if (resp[0] == '\0') 198 break; 199 if (parse_policy (resp, policy, mask, bit) == 0) 200 break; 201 } 202 return 0; 203 } 204 205 /* 206 * time_t 207 * the special value 0 means ``never'' 208 */ 209 210 /* 211 * Convert the time `t' to a string representation in `str' (of max 212 * size `len'). If include_time also include time, otherwise just 213 * date. 214 */ 215 216 void 217 time_t2str(time_t t, char *str, size_t len, int include_time) 218 { 219 if(t) { 220 if(include_time) 221 strftime(str, len, "%Y-%m-%d %H:%M:%S UTC", gmtime(&t)); 222 else 223 strftime(str, len, "%Y-%m-%d", gmtime(&t)); 224 } else 225 snprintf(str, len, "never"); 226 } 227 228 /* 229 * Convert the time representation in `str' to a time in `time'. 230 * Return 0 if succesful, else -1. 231 */ 232 233 int 234 str2time_t (const char *str, time_t *t) 235 { 236 const char *p; 237 struct tm tm, tm2; 238 239 memset (&tm, 0, sizeof (tm)); 240 memset (&tm2, 0, sizeof (tm2)); 241 242 while(isspace((unsigned char)*str)) 243 str++; 244 245 if (str[0] == '+') { 246 str++; 247 *t = parse_time(str, "month"); 248 if (*t < 0) 249 return -1; 250 *t += time(NULL); 251 return 0; 252 } 253 254 if(strcasecmp(str, "never") == 0) { 255 *t = 0; 256 return 0; 257 } 258 259 if(strcasecmp(str, "now") == 0) { 260 *t = time(NULL); 261 return 0; 262 } 263 264 p = strptime (str, "%Y-%m-%d", &tm); 265 266 if (p == NULL) 267 return -1; 268 269 while(isspace((unsigned char)*p)) 270 p++; 271 272 /* XXX this is really a bit optimistic, we should really complain 273 if there was a problem parsing the time */ 274 if(p[0] != '\0' && strptime (p, "%H:%M:%S", &tm2) != NULL) { 275 tm.tm_hour = tm2.tm_hour; 276 tm.tm_min = tm2.tm_min; 277 tm.tm_sec = tm2.tm_sec; 278 } else { 279 /* Do it on the end of the day */ 280 tm.tm_hour = 23; 281 tm.tm_min = 59; 282 tm.tm_sec = 59; 283 } 284 285 *t = tm2time (tm, 0); 286 return 0; 287 } 288 289 /* 290 * try to parse the time in `resp' storing it in `value' 291 */ 292 293 int 294 parse_timet (const char *resp, krb5_timestamp *value, int *mask, int bit) 295 { 296 time_t tmp; 297 298 if (str2time_t(resp, &tmp) == 0) { 299 *value = tmp; 300 if(mask) 301 *mask |= bit; 302 return 0; 303 } 304 if(*resp != '?') 305 fprintf (stderr, "Unable to parse time \"%s\"\n", resp); 306 fprintf (stderr, "Print date on format YYYY-mm-dd [hh:mm:ss]\n"); 307 return -1; 308 } 309 310 /* 311 * allow the user to edit the time in `value' 312 */ 313 314 int 315 edit_timet (const char *prompt, krb5_timestamp *value, int *mask, int bit) 316 { 317 char buf[1024], resp[1024]; 318 319 if (mask && (*mask & bit)) 320 return 0; 321 322 time_t2str (*value, buf, sizeof (buf), 0); 323 324 for (;;) { 325 if(get_response(prompt, buf, resp, sizeof(resp)) != 0) 326 return 1; 327 if (parse_timet (resp, value, mask, bit) == 0) 328 break; 329 } 330 return 0; 331 } 332 333 /* 334 * deltat 335 * the special value 0 means ``unlimited'' 336 */ 337 338 /* 339 * convert the delta_t value in `t' into a printable form in `str, len' 340 */ 341 342 void 343 deltat2str(unsigned t, char *str, size_t len) 344 { 345 if(t == 0 || t == INT_MAX) 346 snprintf(str, len, "unlimited"); 347 else 348 unparse_time(t, str, len); 349 } 350 351 /* 352 * parse the delta value in `str', storing result in `*delta' 353 * return 0 if ok, else -1 354 */ 355 356 int 357 str2deltat(const char *str, krb5_deltat *delta) 358 { 359 int res; 360 361 if(strcasecmp(str, "unlimited") == 0) { 362 *delta = 0; 363 return 0; 364 } 365 res = parse_time(str, "day"); 366 if (res < 0) 367 return res; 368 else { 369 *delta = res; 370 return 0; 371 } 372 } 373 374 /* 375 * try to parse the string in `resp' into a deltad in `value' 376 * `mask' will get the bit `bit' set if a value was given. 377 */ 378 379 int 380 parse_deltat (const char *resp, krb5_deltat *value, int *mask, int bit) 381 { 382 krb5_deltat tmp; 383 384 if (str2deltat(resp, &tmp) == 0) { 385 *value = tmp; 386 if (mask) 387 *mask |= bit; 388 return 0; 389 } else if(*resp == '?') { 390 print_time_table (stderr); 391 } else { 392 fprintf (stderr, "Unable to parse time \"%s\"\n", resp); 393 } 394 return -1; 395 } 396 397 /* 398 * allow the user to edit the deltat in `value' 399 */ 400 401 int 402 edit_deltat (const char *prompt, krb5_deltat *value, int *mask, int bit) 403 { 404 char buf[1024], resp[1024]; 405 406 if (mask && (*mask & bit)) 407 return 0; 408 409 deltat2str(*value, buf, sizeof(buf)); 410 for (;;) { 411 if(get_response(prompt, buf, resp, sizeof(resp)) != 0) 412 return 1; 413 if (parse_deltat (resp, value, mask, bit) == 0) 414 break; 415 } 416 return 0; 417 } 418 419 /* 420 * allow the user to edit `ent' 421 */ 422 423 void 424 set_defaults(kadm5_principal_ent_t ent, int *mask, 425 kadm5_principal_ent_t default_ent, int default_mask) 426 { 427 if (default_ent 428 && (default_mask & KADM5_MAX_LIFE) 429 && !(*mask & KADM5_MAX_LIFE)) 430 ent->max_life = default_ent->max_life; 431 432 if (default_ent 433 && (default_mask & KADM5_MAX_RLIFE) 434 && !(*mask & KADM5_MAX_RLIFE)) 435 ent->max_renewable_life = default_ent->max_renewable_life; 436 437 if (default_ent 438 && (default_mask & KADM5_PRINC_EXPIRE_TIME) 439 && !(*mask & KADM5_PRINC_EXPIRE_TIME)) 440 ent->princ_expire_time = default_ent->princ_expire_time; 441 442 if (default_ent 443 && (default_mask & KADM5_PW_EXPIRATION) 444 && !(*mask & KADM5_PW_EXPIRATION)) 445 ent->pw_expiration = default_ent->pw_expiration; 446 447 if (default_ent 448 && (default_mask & KADM5_ATTRIBUTES) 449 && !(*mask & KADM5_ATTRIBUTES)) 450 ent->attributes = default_ent->attributes & ~KRB5_KDB_DISALLOW_ALL_TIX; 451 452 if (default_ent 453 && (default_mask & KADM5_POLICY) 454 && !(*mask & KADM5_POLICY)) { 455 ent->policy = strdup(default_ent->policy); 456 if (ent->policy == NULL) 457 abort(); 458 } 459 } 460 461 int 462 edit_entry(kadm5_principal_ent_t ent, int *mask, 463 kadm5_principal_ent_t default_ent, int default_mask) 464 { 465 466 set_defaults(ent, mask, default_ent, default_mask); 467 468 if(edit_deltat ("Max ticket life", &ent->max_life, mask, 469 KADM5_MAX_LIFE) != 0) 470 return 1; 471 472 if(edit_deltat ("Max renewable life", &ent->max_renewable_life, mask, 473 KADM5_MAX_RLIFE) != 0) 474 return 1; 475 476 if(edit_timet ("Principal expiration time", &ent->princ_expire_time, mask, 477 KADM5_PRINC_EXPIRE_TIME) != 0) 478 return 1; 479 480 if(edit_timet ("Password expiration time", &ent->pw_expiration, mask, 481 KADM5_PW_EXPIRATION) != 0) 482 return 1; 483 484 if(edit_attributes ("Attributes", &ent->attributes, mask, 485 KADM5_ATTRIBUTES) != 0) 486 return 1; 487 488 if(edit_policy ("Policy", &ent->policy, mask, 489 KADM5_POLICY) != 0) 490 return 1; 491 492 return 0; 493 } 494 495 /* 496 * Parse the arguments, set the fields in `ent' and the `mask' for the 497 * entries having been set. 498 * Return 1 on failure and 0 on success. 499 */ 500 501 int 502 set_entry(krb5_context contextp, 503 kadm5_principal_ent_t ent, 504 int *mask, 505 const char *max_ticket_life, 506 const char *max_renewable_life, 507 const char *expiration, 508 const char *pw_expiration, 509 const char *attributes, 510 const char *policy) 511 { 512 if (max_ticket_life != NULL) { 513 if (parse_deltat (max_ticket_life, &ent->max_life, 514 mask, KADM5_MAX_LIFE)) { 515 krb5_warnx (contextp, "unable to parse `%s'", max_ticket_life); 516 return 1; 517 } 518 } 519 if (max_renewable_life != NULL) { 520 if (parse_deltat (max_renewable_life, &ent->max_renewable_life, 521 mask, KADM5_MAX_RLIFE)) { 522 krb5_warnx (contextp, "unable to parse `%s'", max_renewable_life); 523 return 1; 524 } 525 } 526 527 if (expiration) { 528 if (parse_timet (expiration, &ent->princ_expire_time, 529 mask, KADM5_PRINC_EXPIRE_TIME)) { 530 krb5_warnx (contextp, "unable to parse `%s'", expiration); 531 return 1; 532 } 533 } 534 if (pw_expiration) { 535 if (parse_timet (pw_expiration, &ent->pw_expiration, 536 mask, KADM5_PW_EXPIRATION)) { 537 krb5_warnx (contextp, "unable to parse `%s'", pw_expiration); 538 return 1; 539 } 540 } 541 if (attributes != NULL) { 542 if (parse_attributes (attributes, &ent->attributes, 543 mask, KADM5_ATTRIBUTES)) { 544 krb5_warnx (contextp, "unable to parse `%s'", attributes); 545 return 1; 546 } 547 } 548 if (policy != NULL) { 549 if (parse_policy (policy, &ent->policy, 550 mask, KADM5_POLICY)) { 551 krb5_warnx (contextp, "unable to parse `%s'", attributes); 552 return 1; 553 } 554 } 555 return 0; 556 } 557 558 /* 559 * Does `string' contain any globing characters? 560 */ 561 562 static int 563 is_expression(const char *string) 564 { 565 const char *p; 566 int quote = 0; 567 568 for(p = string; *p; p++) { 569 if(quote) { 570 quote = 0; 571 continue; 572 } 573 if(*p == '\\') 574 quote++; 575 else if(strchr("[]*?", *p) != NULL) 576 return 1; 577 } 578 return 0; 579 } 580 581 /* 582 * Loop over all principals matching exp. If any of calls to `func' 583 * failes, the first error is returned when all principals are 584 * processed. 585 */ 586 int 587 foreach_principal(const char *exp_str, 588 int (*func)(krb5_principal, void*), 589 const char *funcname, 590 void *data) 591 { 592 char **princs = NULL; 593 int num_princs = 0; 594 int i; 595 krb5_error_code saved_ret = 0, ret = 0; 596 krb5_principal princ_ent; 597 int is_expr; 598 599 /* if this isn't an expression, there is no point in wading 600 through the whole database looking for matches */ 601 is_expr = is_expression(exp_str); 602 if(is_expr) 603 ret = kadm5_get_principals(kadm_handle, exp_str, &princs, &num_princs); 604 if(!is_expr || ret == KADM5_AUTH_LIST) { 605 /* we might be able to perform the requested opreration even 606 if we're not allowed to list principals */ 607 num_princs = 1; 608 princs = malloc(sizeof(*princs)); 609 if(princs == NULL) 610 return ENOMEM; 611 princs[0] = strdup(exp_str); 612 if(princs[0] == NULL){ 613 free(princs); 614 return ENOMEM; 615 } 616 } else if(ret) { 617 krb5_warn(context, ret, "kadm5_get_principals"); 618 return ret; 619 } 620 for(i = 0; i < num_princs; i++) { 621 ret = krb5_parse_name(context, princs[i], &princ_ent); 622 if(ret){ 623 krb5_warn(context, ret, "krb5_parse_name(%s)", princs[i]); 624 continue; 625 } 626 ret = (*func)(princ_ent, data); 627 if(ret) { 628 krb5_clear_error_message(context); 629 krb5_warn(context, ret, "%s %s", funcname, princs[i]); 630 if (saved_ret == 0) 631 saved_ret = ret; 632 } 633 krb5_free_principal(context, princ_ent); 634 } 635 if (ret == 0 && saved_ret != 0) 636 ret = saved_ret; 637 kadm5_free_name_list(kadm_handle, princs, &num_princs); 638 return ret; 639 } 640 641 /* 642 * prompt with `prompt' and default value `def', and store the reply 643 * in `buf, len' 644 */ 645 646 #include <setjmp.h> 647 648 static jmp_buf jmpbuf; 649 650 static void 651 interrupt(int sig) 652 { 653 longjmp(jmpbuf, 1); 654 } 655 656 static int 657 get_response(const char *prompt, const char *def, char *buf, size_t len) 658 { 659 char *p; 660 void (*osig)(int); 661 662 osig = signal(SIGINT, interrupt); 663 if(setjmp(jmpbuf)) { 664 signal(SIGINT, osig); 665 fprintf(stderr, "\n"); 666 return 1; 667 } 668 669 fprintf(stderr, "%s [%s]:", prompt, def); 670 if(fgets(buf, len, stdin) == NULL) { 671 int save_errno = errno; 672 if(ferror(stdin)) 673 krb5_err(context, 1, save_errno, "<stdin>"); 674 signal(SIGINT, osig); 675 return 1; 676 } 677 p = strchr(buf, '\n'); 678 if(p) 679 *p = '\0'; 680 if(strcmp(buf, "") == 0) 681 strlcpy(buf, def, len); 682 signal(SIGINT, osig); 683 return 0; 684 } 685 686 /* 687 * return [0, 16) or -1 688 */ 689 690 static int 691 hex2n (char c) 692 { 693 static char hexdigits[] = "0123456789abcdef"; 694 const char *p; 695 696 p = strchr (hexdigits, tolower((unsigned char)c)); 697 if (p == NULL) 698 return -1; 699 else 700 return p - hexdigits; 701 } 702 703 /* 704 * convert a key in a readable format into a keyblock. 705 * return 0 iff succesful, otherwise `err' should point to an error message 706 */ 707 708 int 709 parse_des_key (const char *key_string, krb5_key_data *key_data, 710 const char **error) 711 { 712 const char *p = key_string; 713 unsigned char bits[8]; 714 int i; 715 716 if (strlen (key_string) != 16) { 717 *error = "bad length, should be 16 for DES key"; 718 return 1; 719 } 720 for (i = 0; i < 8; ++i) { 721 int d1, d2; 722 723 d1 = hex2n(p[2 * i]); 724 d2 = hex2n(p[2 * i + 1]); 725 if (d1 < 0 || d2 < 0) { 726 *error = "non-hex character"; 727 return 1; 728 } 729 bits[i] = (d1 << 4) | d2; 730 } 731 for (i = 0; i < 3; ++i) { 732 key_data[i].key_data_ver = 2; 733 key_data[i].key_data_kvno = 0; 734 /* key */ 735 key_data[i].key_data_type[0] = ETYPE_DES_CBC_CRC; 736 key_data[i].key_data_length[0] = 8; 737 key_data[i].key_data_contents[0] = malloc(8); 738 if (key_data[i].key_data_contents[0] == NULL) { 739 *error = "malloc"; 740 return ENOMEM; 741 } 742 memcpy (key_data[i].key_data_contents[0], bits, 8); 743 /* salt */ 744 key_data[i].key_data_type[1] = KRB5_PW_SALT; 745 key_data[i].key_data_length[1] = 0; 746 key_data[i].key_data_contents[1] = NULL; 747 } 748 key_data[0].key_data_type[0] = ETYPE_DES_CBC_MD5; 749 key_data[1].key_data_type[0] = ETYPE_DES_CBC_MD4; 750 return 0; 751 } 752