1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
|
/* tkts.c --- Ticket set handling.
* Copyright (C) 2002-2022 Simon Josefsson
*
* This file is part of Shishi.
*
* Shishi is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Shishi is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Shishi; if not, see http://www.gnu.org/licenses or write
* to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA
*
*/
#include "internal.h"
#include <minmax.h>
struct Shishi_tkts
{
Shishi *handle;
Shishi_tkt **tkts;
int ntkts;
};
#define TICKET_FILE "tickets"
/**
* shishi_tkts_default_file_guess:
* @handle: Shishi library handle create by shishi_init().
*
* Guesses the default ticket filename; it is $SHISHI_TICKETS,
* $SHISHI_HOME/tickets, or $HOME/.shishi/tickets.
*
* Return value: Returns default tkts filename as a string that
* has to be deallocated with free() by the caller.
**/
char *
shishi_tkts_default_file_guess (Shishi * handle)
{
char *envfile;
envfile = getenv ("SHISHI_TICKETS");
if (envfile)
return xstrdup (envfile);
return shishi_cfg_userdirectory_file (handle, TICKET_FILE);
}
/**
* shishi_tkts_default_file:
* @handle: Shishi library handle create by shishi_init().
*
* Get filename of default ticket set.
*
* Return value: Returns the default ticket set filename used in the
* library. The string is not a copy, so don't modify or deallocate
* it.
**/
const char *
shishi_tkts_default_file (Shishi * handle)
{
if (!handle->tktsdefaultfile)
{
char *p;
p = shishi_tkts_default_file_guess (handle);
shishi_tkts_default_file_set (handle, p);
free (p);
}
return handle->tktsdefaultfile;
}
/**
* shishi_tkts_default_file_set:
* @handle: Shishi library handle create by shishi_init().
* @tktsfile: string with new default tkts file name, or
* NULL to reset to default.
*
* Set the default ticket set filename used in the library. The
* string is copied into the library, so you can dispose of the
* variable immediately after calling this function.
**/
void
shishi_tkts_default_file_set (Shishi * handle, const char *tktsfile)
{
free (handle->tktsdefaultfile);
if (tktsfile)
handle->tktsdefaultfile = xstrdup (tktsfile);
else
handle->tktsdefaultfile = NULL;
}
/**
* shishi_tkts_default:
* @handle: Shishi library handle create by shishi_init().
*
* Get the default ticket set for library handle.
*
* Return value: Return the handle global ticket set.
**/
Shishi_tkts *
shishi_tkts_default (Shishi * handle)
{
if (handle->tkts == NULL &&
(shishi_tkts (handle, &handle->tkts) != SHISHI_OK))
handle->tkts = NULL;
return handle->tkts;
}
int
shishi_tkts_default_to_file (Shishi_tkts * tkts)
{
return shishi_tkts_to_file (tkts, shishi_tkts_default_file (tkts->handle));
}
/**
* shishi_tkts:
* @handle: shishi handle as allocated by shishi_init().
* @tkts: output pointer to newly allocated tkts handle.
*
* Get a new ticket set handle.
*
* Return value: Returns %SHISHI_OK iff successful.
**/
int
shishi_tkts (Shishi * handle, Shishi_tkts ** tkts)
{
*tkts = xcalloc (1, sizeof (**tkts));
(*tkts)->handle = handle;
return SHISHI_OK;
}
/**
* shishi_tkts_done:
* @tkts: ticket set handle as allocated by shishi_tkts().
*
* Deallocates all resources associated with ticket set. The ticket
* set handle must not be used in calls to other shishi_tkts_*()
* functions after this.
**/
void
shishi_tkts_done (Shishi_tkts ** tkts)
{
if (!tkts || !*tkts)
return;
if ((*tkts)->tkts)
free ((*tkts)->tkts);
free (*tkts);
*tkts = NULL;
return;
}
/**
* shishi_tkts_size:
* @tkts: ticket set handle as allocated by shishi_tkts().
*
* Get size of ticket set.
*
* Return value: Returns number of tickets stored in ticket set.
**/
int
shishi_tkts_size (Shishi_tkts * tkts)
{
return tkts ? tkts->ntkts : -1;
}
/**
* shishi_tkts_nth:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @ticketno: integer indicating requested ticket in ticket set.
*
* Get the n:th ticket in ticket set.
*
* Return value: Returns a ticket handle to the ticketno:th ticket in
* the ticket set, or NULL if ticket set is invalid or ticketno is
* out of bounds. The first ticket is ticketno 0, the second
* ticketno 1, and so on.
**/
Shishi_tkt *
shishi_tkts_nth (Shishi_tkts * tkts, int ticketno)
{
if (tkts == NULL || ticketno >= tkts->ntkts)
return NULL;
return tkts->tkts[ticketno];
}
/**
* shishi_tkts_remove:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @ticketno: ticket number of ticket in the set to remove. The first
* ticket is ticket number 0.
*
* Remove a ticket, indexed by @ticketno, in ticket set.
*
* Return value: %SHISHI_OK if successful or if @ticketno larger than
* size of ticket set.
**/
int
shishi_tkts_remove (Shishi_tkts * tkts, int ticketno)
{
if (!tkts)
return SHISHI_INVALID_TKTS;
if (ticketno >= tkts->ntkts)
return SHISHI_OK;
if (ticketno < tkts->ntkts)
memmove (&tkts->tkts[ticketno], &tkts->tkts[ticketno + 1],
sizeof (*tkts->tkts) * (tkts->ntkts - ticketno - 1));
--tkts->ntkts;
if (tkts->ntkts > 0)
{
tkts->tkts = xrealloc (tkts->tkts, sizeof (*tkts->tkts) * tkts->ntkts);
}
else
{
free (tkts->tkts);
tkts->tkts = NULL;
}
return SHISHI_OK;
}
/**
* shishi_tkts_add:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @tkt: ticket to be added to ticket set.
*
* Add a ticket to the ticket set. Only the pointer is stored, so if
* you modify @tkt, the ticket in the ticket set will also be
* modified.
*
* Return value: Returns %SHISHI_OK iff successful.
**/
int
shishi_tkts_add (Shishi_tkts * tkts, Shishi_tkt * tkt)
{
if (!tkt)
return SHISHI_INVALID_TICKET;
if (tkts->ntkts++ == 0)
tkts->tkts = xmalloc (sizeof (*tkts->tkts));
else
tkts->tkts = xrealloc (tkts->tkts, sizeof (*tkts->tkts) * tkts->ntkts);
tkts->tkts[tkts->ntkts - 1] = tkt;
return SHISHI_OK;
}
/**
* shishi_tkts_new:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @ticket: input ticket variable.
* @enckdcreppart: input ticket detail variable.
* @kdcrep: input KDC-REP variable.
*
* Allocate a new ticket and add it to the ticket set.
*
* Note that @ticket, @enckdcreppart and @kdcrep are stored by
* reference, so you must not de-allocate them before the ticket is
* removed from the ticket set and de-allocated.
*
* Return value: Returns %SHISHI_OK iff successful.
**/
int
shishi_tkts_new (Shishi_tkts * tkts,
Shishi_asn1 ticket, Shishi_asn1 enckdcreppart,
Shishi_asn1 kdcrep)
{
Shishi_tkt *tkt;
int res;
/* XXX Who will de-allocate these? */
tkt = shishi_tkt2 (tkts->handle, ticket, enckdcreppart, kdcrep);
res = shishi_tkts_add (tkts, tkt);
if (res != SHISHI_OK)
{
free (tkt);
return res;
}
return SHISHI_OK;
}
/**
* shishi_tkts_read:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @fh: file descriptor to read from.
*
* Read tickets from file descriptor and add them to the ticket set.
*
* Return value: Returns %SHISHI_OK iff successful.
**/
int
shishi_tkts_read (Shishi_tkts * tkts, FILE * fh)
{
int res;
res = SHISHI_OK;
while (!feof (fh))
{
Shishi_asn1 ticket;
Shishi_asn1 enckdcreppart;
Shishi_asn1 kdcrep;
res = shishi_kdcrep_parse (tkts->handle, fh, &kdcrep);
if (res != SHISHI_OK)
{
res = SHISHI_OK;
break;
}
res = shishi_enckdcreppart_parse (tkts->handle, fh, &enckdcreppart);
if (res != SHISHI_OK)
break;
res = shishi_ticket_parse (tkts->handle, fh, &ticket);
if (res != SHISHI_OK)
break;
/* XXX Who will de-allocate these? */
res = shishi_tkts_new (tkts, ticket, enckdcreppart, kdcrep);
if (res != SHISHI_OK)
break;
if (VERBOSEASN1 (tkts->handle))
{
printf ("Read ticket for principal `':\n");
shishi_kdcrep_print (tkts->handle, stdout, kdcrep);
shishi_enckdcreppart_print (tkts->handle, stdout, enckdcreppart);
shishi_ticket_print (tkts->handle, stdout, ticket);
}
}
return res;
}
/**
* shishi_tkts_from_file:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @filename: filename to read tickets from.
*
* Read tickets from file and add them to the ticket set.
*
* Return value: Returns %SHISHI_OK iff successful.
**/
int
shishi_tkts_from_file (Shishi_tkts * tkts, const char *filename)
{
FILE *fh;
int res;
fh = fopen (filename, "r");
if (fh == NULL)
return SHISHI_FOPEN_ERROR;
res = shishi_tkts_read (tkts, fh);
if (res != SHISHI_OK)
{
fclose (fh);
return res;
}
res = fclose (fh);
if (res != 0)
return SHISHI_IO_ERROR;
return SHISHI_OK;
}
/**
* shishi_tkts_write:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @fh: file descriptor to write tickets to.
*
* Write tickets in set to file descriptor.
*
* Return value: Returns %SHISHI_OK iff successful.
**/
int
shishi_tkts_write (Shishi_tkts * tkts, FILE * fh)
{
int res;
int i;
if (!tkts)
return SHISHI_INVALID_TKTS;
for (i = 0; i < tkts->ntkts; i++)
{
res = shishi_kdcrep_print
(tkts->handle, fh, shishi_tkt_kdcrep (tkts->tkts[i]));
if (res != SHISHI_OK)
{
shishi_error_printf (tkts->handle,
"Could not print ticket: %s",
shishi_error (tkts->handle));
return res;
}
res = shishi_enckdcreppart_print
(tkts->handle, fh, shishi_tkt_enckdcreppart (tkts->tkts[i]));
if (res != SHISHI_OK)
{
shishi_error_printf (tkts->handle,
"Could not print ticket: %s",
shishi_error (tkts->handle));
return res;
}
res = shishi_ticket_print (tkts->handle, fh,
shishi_tkt_ticket (tkts->tkts[i]));
if (res != SHISHI_OK)
{
shishi_error_printf (tkts->handle,
"Could not print ticket: %s",
shishi_error (tkts->handle));
return res;
}
fprintf (fh, "\n\n");
}
return SHISHI_OK;
}
/**
* shishi_tkts_expire:
* @tkts: ticket set handle as allocated by shishi_tkts().
*
* Remove expired tickets from ticket set.
*
* Return value: Returns %SHISHI_OK iff successful.
**/
int
shishi_tkts_expire (Shishi_tkts * tkts)
{
int warn = 0;
int i = 0;
if (!tkts)
return SHISHI_INVALID_TKTS;
while (i < tkts->ntkts)
{
if (shishi_tkt_expired_p (tkts->tkts[i]))
{
warn++;
shishi_tkts_remove (tkts, i);
}
else
i++;
}
if (VERBOSE (tkts->handle) && warn)
shishi_warn (tkts->handle,
ngettext ("removed %d expired ticket\n",
"removed %d expired tickets\n", warn), warn);
return SHISHI_OK;
}
/**
* shishi_tkts_to_file:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @filename: filename to write tickets to.
*
* Write tickets in set to file.
*
* Return value: Returns %SHISHI_OK iff successful.
**/
int
shishi_tkts_to_file (Shishi_tkts * tkts, const char *filename)
{
FILE *fh;
int res;
fh = fopen (filename, "w");
if (fh == NULL)
return SHISHI_FOPEN_ERROR;
res = shishi_tkts_write (tkts, fh);
if (res != SHISHI_OK)
{
fclose (fh);
return res;
}
res = fclose (fh);
if (res != 0)
return SHISHI_IO_ERROR;
return SHISHI_OK;
}
/**
* shishi_tkts_print_for_service:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @fh: file descriptor to print to.
* @service: service to limit tickets printed to, or NULL.
*
* Print description of tickets for specified service to file
* descriptor. If service is NULL, all tickets are printed.
*
* Return value: Returns %SHISHI_OK iff successful.
**/
int
shishi_tkts_print_for_service (Shishi_tkts * tkts, FILE * fh,
const char *service)
{
int res;
int found;
int i;
found = 0;
for (i = 0; i < shishi_tkts_size (tkts); i++)
{
Shishi_tkt *tkt = shishi_tkts_nth (tkts, i);
if (service)
{
char *buf;
res = shishi_tkt_server (tkt, &buf, NULL);
if (res != SHISHI_OK)
continue;
if (strcmp (service, buf) != 0)
{
free (buf);
continue;
}
free (buf);
}
printf ("\n");
shishi_tkt_pretty_print (shishi_tkts_nth (tkts, i), fh);
found++;
}
if (found)
{
printf (ngettext ("\n%d ticket found.\n", "\n%d tickets found.\n",
found), found);
}
else
{
if (service)
printf ("\nNo matching tickets found.\n");
else
printf ("\nNo tickets found.\n");
}
return SHISHI_OK;
}
/**
* shishi_tkts_print:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @fh: file descriptor to print to.
*
* Print description of all tickets to file descriptor.
*
* Return value: Returns %SHISHI_OK iff successful.
**/
int
shishi_tkts_print (Shishi_tkts * tkts, FILE * fh)
{
return shishi_tkts_print_for_service (tkts, fh, NULL);
}
/**
* shishi_tkt_match_p:
* @tkt: ticket to test hints on.
* @hint: structure with characteristics of ticket to be found.
*
* Test if a ticket matches specified hints.
*
* Return value: Returns 0 iff ticket fails to match given criteria.
**/
int
shishi_tkt_match_p (Shishi_tkt * tkt, Shishi_tkts_hint * hint)
{
if (hint->server && !shishi_tkt_server_p (tkt, hint->server))
return 0;
if (hint->client && !shishi_tkt_client_p (tkt, hint->client))
return 0;
if (!(hint->flags & SHISHI_TKTSHINTFLAGS_ACCEPT_EXPIRED) &&
!shishi_tkt_valid_now_p (tkt))
return 0;
if ((hint->tktflags & SHISHI_TICKETFLAGS_FORWARDABLE) &&
!shishi_tkt_forwardable_p (tkt))
return 0;
if ((hint->tktflags & SHISHI_TICKETFLAGS_FORWARDED) &&
!shishi_tkt_forwarded_p (tkt))
return 0;
if ((hint->tktflags & SHISHI_TICKETFLAGS_RENEWABLE) &&
!shishi_tkt_renewable_p (tkt))
return 0;
if ((hint->tktflags & SHISHI_TICKETFLAGS_PROXIABLE) &&
!shishi_tkt_proxiable_p (tkt))
return 0;
if ((hint->tktflags & SHISHI_TICKETFLAGS_PROXY) &&
!shishi_tkt_proxy_p (tkt))
return 0;
if (hint->etype && !shishi_tkt_keytype_p (tkt, hint->etype))
return 0;
return 1;
}
/**
* shishi_tkts_find:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @hint: structure with characteristics of ticket to be found.
*
* Search the ticketset sequentially (from ticket number 0 through all
* tickets in the set) for a ticket that fits the given
* characteristics. If a ticket is found, the hint->startpos field is
* updated to point to the next ticket in the set, so this function
* can be called repeatedly with the same hint argument in order to
* find all tickets matching a certain criterium. Note that if
* tickets are added to, or removed from, the ticketset during a query
* with the same hint argument, the hint->startpos field must be
* updated appropriately.
*
* Here is how you would typically use this function:
*
* Shishi_tkts_hint hint;
*
* Shishi_tkt tkt;
*
*
* memset(&hint, 0, sizeof(hint));
*
* hint.server = "imap/mail.example.org";
*
* tkt = shishi_tkts_find (shishi_tkts_default(handle), &hint);
*
* if (!tkt)
*
* printf("No ticket found...\n");
*
* else
*
* do_something_with_ticket (tkt);
*
* Return value: Returns a ticket if found, or NULL if no further
* matching tickets could be found.
**/
Shishi_tkt *
shishi_tkts_find (Shishi_tkts * tkts, Shishi_tkts_hint * hint)
{
int i;
if (!tkts)
return NULL;
if (VERBOSENOISE (tkts->handle))
{
fprintf (stderr, "Searching tickets... ");
if (hint->server)
fprintf (stderr, "server=`%s' ", hint->server);
if (hint->client)
fprintf (stderr, "client=`%s' ", hint->client);
fprintf (stderr, "\n");
}
for (i = hint->startpos; i < tkts->ntkts; i++)
{
if (!shishi_tkt_match_p (tkts->tkts[i], hint))
continue;
hint->startpos = i + 1;
return tkts->tkts[i];
}
hint->startpos = i;
return NULL;
}
/**
* shishi_tkts_find_for_clientserver:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @client: client name to find ticket for.
* @server: server name to find ticket for.
*
* Short-hand function for searching the ticket set for a ticket for
* the given client and server. See shishi_tkts_find().
*
* Return value: Returns a ticket if found, or NULL.
**/
Shishi_tkt *
shishi_tkts_find_for_clientserver (Shishi_tkts * tkts,
const char *client, const char *server)
{
Shishi_tkts_hint hint;
Shishi_tkt *tkt;
memset (&hint, 0, sizeof (hint));
hint.server = (char *) server;
hint.client = (char *) client;
tkt = shishi_tkts_find (tkts, &hint);
return tkt;
}
/**
* shishi_tkts_find_for_server:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @server: server name to find ticket for.
*
* Short-hand function for searching the ticket set for a ticket for
* the given server using the default client principal. See
* shishi_tkts_find_for_clientserver() and shishi_tkts_find().
*
* Return value: Returns a ticket if found, or NULL.
**/
Shishi_tkt *
shishi_tkts_find_for_server (Shishi_tkts * tkts, const char *server)
{
return shishi_tkts_find_for_clientserver
(tkts, shishi_principal_default (tkts->handle), server);
}
/* Set flags and times in KDC-REQ based on hint. */
static int
act_hint_on_kdcreq (Shishi * handle,
Shishi_tkts_hint * hint, Shishi_asn1 kdcreq)
{
time_t starttime = hint->starttime ? hint->starttime : time (NULL);
time_t endtime = hint->endtime ? hint->endtime :
starttime + handle->ticketlife;
time_t renew_till = hint->renew_till ? hint->renew_till :
starttime + handle->renewlife;
int rc;
if (hint->starttime)
{
rc = shishi_asn1_write (handle, kdcreq, "req-body.from",
shishi_generalize_time (handle, starttime), 0);
if (rc != SHISHI_OK)
{
shishi_error_printf (handle, "Cannot set starttime: %s",
shishi_strerror (rc));
return rc;
}
}
if (hint->endtime)
{
rc = shishi_asn1_write (handle, kdcreq, "req-body.till",
shishi_generalize_time (handle, endtime), 0);
if (rc != SHISHI_OK)
{
shishi_error_printf (handle, "Cannot set endtime: %s",
shishi_strerror (rc));
return rc;
}
}
if (hint->tktflags & SHISHI_TICKETFLAGS_FORWARDABLE)
{
rc = shishi_kdcreq_options_add (handle, kdcreq,
SHISHI_KDCOPTIONS_FORWARDABLE);
if (rc != SHISHI_OK)
goto done;
}
if (hint->tktflags & SHISHI_TICKETFLAGS_FORWARDED)
{
rc = shishi_kdcreq_options_add (handle, kdcreq,
SHISHI_KDCOPTIONS_FORWARDED);
if (rc != SHISHI_OK)
goto done;
}
if (hint->tktflags & SHISHI_TICKETFLAGS_RENEWABLE)
{
rc = shishi_kdcreq_options_add (handle, kdcreq,
SHISHI_KDCOPTIONS_RENEWABLE);
if (rc != SHISHI_OK)
goto done;
rc = shishi_asn1_write (handle, kdcreq, "req-body.rtime",
shishi_generalize_time (handle, renew_till), 0);
if (rc != SHISHI_OK)
{
shishi_error_printf (handle, "Cannot set renewtill: %s",
shishi_strerror (rc));
return rc;
}
}
if (hint->tktflags & SHISHI_TICKETFLAGS_PROXIABLE)
{
rc = shishi_kdcreq_options_add (handle, kdcreq,
SHISHI_KDCOPTIONS_PROXIABLE);
if (rc != SHISHI_OK)
goto done;
}
if (hint->tktflags & SHISHI_TICKETFLAGS_PROXY)
{
rc = shishi_kdcreq_options_add (handle, kdcreq,
SHISHI_KDCOPTIONS_PROXY);
if (rc != SHISHI_OK)
goto done;
}
if (hint->etype)
{
rc = shishi_kdcreq_set_etype (handle, kdcreq, &hint->etype, 1);
if (rc != SHISHI_OK)
goto done;
}
return SHISHI_OK;
done:
shishi_error_printf (handle, "Cannot set KDC Options: %s",
shishi_strerror (rc));
return rc;
}
/* Make sure the ticket granting ticket is suitable for the wanted
ticket. E.g., if the wanted ticket should be a PROXY ticket, the
ticket granting ticket must be a PROXIABLE ticket for things to
work. */
static void
set_tgtflags_based_on_hint (Shishi_tkts_hint * tkthint,
Shishi_tkts_hint * tgthint)
{
if (tkthint->tktflags & SHISHI_TICKETFLAGS_FORWARDABLE)
tgthint->tktflags |= SHISHI_TICKETFLAGS_FORWARDABLE;
if (tkthint->tktflags & SHISHI_TICKETFLAGS_FORWARDED)
tgthint->tktflags |= SHISHI_TICKETFLAGS_FORWARDABLE;
if (tkthint->tktflags & SHISHI_TICKETFLAGS_PROXIABLE)
tgthint->tktflags |= SHISHI_TICKETFLAGS_PROXIABLE;
if (tkthint->tktflags & SHISHI_TICKETFLAGS_PROXY)
tgthint->tktflags |= SHISHI_TICKETFLAGS_PROXIABLE;
if (tkthint->tktflags & SHISHI_TICKETFLAGS_RENEWABLE)
tgthint->tktflags |= SHISHI_TICKETFLAGS_RENEWABLE;
if (tkthint->kdcoptions & SHISHI_KDCOPTIONS_RENEW)
tgthint->tktflags |= SHISHI_TICKETFLAGS_RENEWABLE;
if (tkthint->endtime)
tgthint->endtime = tkthint->endtime;
if (tkthint->passwd)
tgthint->passwd = tkthint->passwd;
if (tkthint->preauthetype)
tgthint->preauthetype = tkthint->preauthetype;
if (tkthint->preauthsalt)
{
tgthint->preauthsalt = tkthint->preauthsalt;
tgthint->preauthsaltlen = tkthint->preauthsaltlen;
}
if (tkthint->preauths2kparams)
{
tgthint->preauths2kparams = tkthint->preauths2kparams;
tgthint->preauths2kparamslen = tkthint->preauths2kparamslen;
}
}
/* Pre-authenticate request, based on LOCHINT. Currently only
PA-ENC-TIMESTAMP is supported. */
static int
do_preauth (Shishi_tkts * tkts, Shishi_tkts_hint * lochint, Shishi_as * as)
{
int rc = SHISHI_OK;
if (lochint->preauthetype)
{
Shishi_key *key;
char *user;
/* XXX Don't prompt for password here? */
rc = shishi_asreq_clientrealm (tkts->handle, shishi_as_req (as),
&user, NULL);
if (rc != SHISHI_OK)
return rc;
if (lochint->passwd == NULL)
{
rc = shishi_prompt_password (tkts->handle, &lochint->passwd,
"Enter password for `%s': ", user);
if (rc != SHISHI_OK)
return rc;
}
if (!lochint->preauthsalt)
{
rc = shishi_derive_default_salt (tkts->handle, user,
&lochint->preauthsalt);
if (rc != SHISHI_OK)
return rc;
lochint->preauthsaltlen = strlen (lochint->preauthsalt);
}
rc = shishi_key_from_string (tkts->handle, lochint->preauthetype,
lochint->passwd, strlen (lochint->passwd),
lochint->preauthsalt,
lochint->preauthsaltlen,
lochint->preauths2kparams, &key);
if (rc != SHISHI_OK)
return rc;
rc = shishi_kdcreq_add_padata_preauth (tkts->handle,
shishi_as_req (as), key);
}
return rc;
}
/* Handle ETYPE-INFO and ETYPE-INFO2 pre-auth data. */
static int
recover_preauth_info (Shishi_tkts * tkts,
Shishi_as * as,
Shishi_tkts_hint * lochint,
Shishi_asn1 einfos, bool isinfo2, bool *retry)
{
size_t foundpos = SIZE_MAX;
size_t i, n;
int rc;
shishi_verbose (tkts->handle, "Found INFO-ETYPE(2) pre-auth hints");
if (VERBOSEASN1 (tkts->handle))
{
if (isinfo2)
shishi_etype_info2_print (tkts->handle, stdout, einfos);
else
shishi_etype_info_print (tkts->handle, stdout, einfos);
}
if (lochint->preauthetype)
{
shishi_verbose (tkts->handle, "Pre-auth data already specified");
return SHISHI_OK;
}
rc = shishi_asn1_number_of_elements (tkts->handle, einfos, "", &n);
if (rc != SHISHI_OK)
return rc;
for (i = 1; i <= n; i++)
{
char *format;
int32_t etype;
format = xasprintf ("?%zu.etype", i);
rc = shishi_asn1_read_int32 (tkts->handle, einfos, format, &etype);
free (format);
if (rc == SHISHI_OK)
{
size_t j;
shishi_verbose (tkts->handle, "Server has etype %d", etype);
for (j = 0; j < tkts->handle->nclientkdcetypes; j++)
{
if (etype == tkts->handle->clientkdcetypes[j])
{
if (j < foundpos && VERBOSENOISE (tkts->handle))
{
shishi_verbose (tkts->handle, "New best etype %d",
etype);
/* XXX mem leak. */
format = xasprintf ("?%zu.salt", i);
rc = shishi_asn1_read (tkts->handle, einfos, format,
&lochint->preauthsalt,
&lochint->preauthsaltlen);
free (format);
if (rc != SHISHI_OK && rc != SHISHI_ASN1_NO_ELEMENT)
return rc;
if (isinfo2)
{
format = xasprintf ("?%zu.s2kparams", i);
rc = shishi_asn1_read (tkts->handle, einfos, format,
&lochint->preauths2kparams,
&lochint->preauths2kparamslen);
free (format);
if (rc != SHISHI_OK && rc != SHISHI_ASN1_NO_ELEMENT)
return rc;
}
}
foundpos = MIN (foundpos, j);
}
}
}
}
if (foundpos != SIZE_MAX)
{
lochint->preauthetype = tkts->handle->clientkdcetypes[foundpos];
shishi_verbose (tkts->handle, "Best pre-auth etype was %d",
lochint->preauthetype);
*retry = true;
}
return SHISHI_OK;
}
/* Called when KDC refused with a NEED_PREAUTH error. This function
should look at the METHOD-DATA, figure out what kind of pre-auth is
requested, and if it is able to figure out how to recover from the
error, set *RETRY to true and set any hints in LOCHINT that help
do_preauth() compute the proper pre-auth data. */
static int
recover_preauth (Shishi_tkts * tkts,
Shishi_as * as, Shishi_tkts_hint * lochint, bool *retry)
{
Shishi_asn1 krberror = shishi_as_krberror (as);
Shishi_asn1 pas;
size_t i, n;
int rc;
*retry = false;
shishi_verbose (tkts->handle, "Server requests pre-auth data");
rc = shishi_krberror_methoddata (tkts->handle, krberror, &pas);
if (rc != SHISHI_OK)
return rc;
rc = shishi_asn1_number_of_elements (tkts->handle, pas, "", &n);
if (rc == SHISHI_OK)
{
for (i = 1; i <= n; i++)
{
char *format = xasprintf ("?%zu.padata-type", i);
int32_t padatatype;
rc = shishi_asn1_read_int32 (tkts->handle, pas, format,
&padatatype);
free (format);
if (rc == SHISHI_OK)
{
shishi_verbose (tkts->handle, "Looking at pa-type %d",
padatatype);
switch (padatatype)
{
/* XXX Don't parse INFO structures if there is a
INFO2. */
case SHISHI_PA_ETYPE_INFO:
case SHISHI_PA_ETYPE_INFO2:
{
char *der;
size_t len;
Shishi_asn1 einfos;
format = xasprintf ("?%zu.padata-value", i);
rc = shishi_asn1_read (tkts->handle, pas, format,
&der, &len);
free (format);
if (rc != SHISHI_OK)
{
shishi_error_printf (tkts->handle,
"Can't extract PA-DATA value");
continue;
}
if (padatatype == SHISHI_PA_ETYPE_INFO)
einfos = shishi_der2asn1_etype_info (tkts->handle,
der, len);
else
einfos = shishi_der2asn1_etype_info2 (tkts->handle,
der, len);
free (der);
if (!einfos)
{
shishi_error_printf (tkts->handle,
"Can't DER decode PA-DATA");
continue;
}
rc = recover_preauth_info
(tkts, as, lochint, einfos,
padatatype == SHISHI_PA_ETYPE_INFO2, retry);
if (rc != SHISHI_OK)
{
shishi_error_printf (tkts->handle,
"Could not use pre-auth data: %s",
shishi_strerror (rc));
continue;
}
shishi_asn1_done (tkts->handle, einfos);
}
break;
default:
break;
}
}
}
}
shishi_asn1_done (tkts->handle, pas);
return SHISHI_OK;
}
/**
* shishi_tkts_get_tgt:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @hint: structure with characteristics of ticket to begot.
*
* Get a ticket granting ticket (TGT) suitable for acquiring ticket
* matching the hint. I.e., get a TGT for the server realm in the
* hint structure (hint->serverrealm), or the default realm if the
* serverrealm field is NULL. Can result in AS exchange.
*
* Currently this function do not implement cross realm logic.
*
* This function is used by shishi_tkts_get(), which is probably what
* you really want to use unless you have special needs.
*
* Return value: Returns a ticket granting ticket if successful, or
* NULL if this function is unable to acquire on.
**/
Shishi_tkt *
shishi_tkts_get_tgt (Shishi_tkts * tkts, Shishi_tkts_hint * hint)
{
Shishi_tkts_hint lochint;
Shishi_as *as;
Shishi_tkt *tgt;
int rc;
/* XXX cross-realm operation */
memset (&lochint, 0, sizeof (lochint));
asprintf (&lochint.server, "krbtgt/%s", hint->serverrealm ?
hint->serverrealm : shishi_realm_default (tkts->handle));
set_tgtflags_based_on_hint (hint, &lochint);
tgt = shishi_tkts_find (tkts, &lochint);
free (lochint.server);
lochint.server = NULL;
if (tgt)
return tgt;
if (hint->flags & SHISHI_TKTSHINTFLAGS_NON_INTERACTIVE)
return NULL;
again:
rc = shishi_as (tkts->handle, &as);
if (rc == SHISHI_OK)
rc = act_hint_on_kdcreq (tkts->handle, &lochint, shishi_as_req (as));
if (rc == SHISHI_OK)
rc = do_preauth (tkts, &lochint, as);
if (rc == SHISHI_OK)
rc = shishi_as_req_build (as);
if (rc == SHISHI_OK)
rc = shishi_as_sendrecv_hint (as, &lochint);
if (rc == SHISHI_OK)
rc = shishi_as_rep_process (as, NULL, lochint.passwd);
if (rc == SHISHI_GOT_KRBERROR
&& shishi_krberror_errorcode_fast (tkts->handle,
shishi_as_krberror (as))
== SHISHI_KDC_ERR_PREAUTH_REQUIRED)
{
bool retry = false;
rc = recover_preauth (tkts, as, &lochint, &retry);
if (rc != SHISHI_OK)
return NULL;
if (retry)
{
shishi_as_done (as);
goto again;
}
shishi_error_printf (tkts->handle, "Unsupported pre-auth required");
return NULL;
}
if (rc != SHISHI_OK)
{
shishi_error_printf (tkts->handle,
"AS exchange failed: %s\n%s\n",
shishi_strerror (rc), shishi_error (tkts->handle));
return NULL;
}
/* XXX free lochint members */
tgt = shishi_as_tkt (as);
if (!tgt)
{
shishi_error_printf (tkts->handle, "No ticket in AS-REP");
return NULL;
}
if (VERBOSENOISE (tkts->handle))
{
printf ("Received ticket granting ticket:\n");
shishi_tkt_pretty_print (tgt, stdout);
}
rc = shishi_tkts_add (tkts, tgt);
if (rc != SHISHI_OK)
printf ("Could not add ticket: %s", shishi_strerror (rc));
return tgt;
}
/**
* shishi_tkts_get_tgs:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @hint: structure with characteristics of ticket to begot.
* @tgt: ticket granting ticket to use.
*
* Get a ticket via TGS exchange using specified ticket granting
* ticket.
*
* This function is used by shishi_tkts_get(), which is probably what
* you really want to use unless you have special needs.
*
* Return value: Returns a ticket if successful, or NULL if this
* function is unable to acquire on.
**/
Shishi_tkt *
shishi_tkts_get_tgs (Shishi_tkts * tkts,
Shishi_tkts_hint * hint, Shishi_tkt * tgt)
{
Shishi_tgs *tgs;
Shishi_tkt *tkt;
int rc;
rc = shishi_tgs (tkts->handle, &tgs);
shishi_tgs_tgtkt_set (tgs, tgt);
if (rc == SHISHI_OK)
rc = act_hint_on_kdcreq (tkts->handle, hint, shishi_tgs_req (tgs));
if (rc == SHISHI_OK)
rc = shishi_tgs_set_server (tgs, hint->server);
if (rc == SHISHI_OK)
rc = shishi_tgs_req_build (tgs);
if (rc == SHISHI_OK)
rc = shishi_tgs_sendrecv_hint (tgs, hint);
if (rc == SHISHI_OK)
rc = shishi_tgs_rep_process (tgs);
if (rc != SHISHI_OK)
{
shishi_error_printf (tkts->handle,
"TGS exchange failed: %s\n%s\n",
shishi_strerror (rc), shishi_error (tkts->handle));
if (rc == SHISHI_GOT_KRBERROR)
shishi_krberror_pretty_print (tkts->handle, stdout,
shishi_tgs_krberror (tgs));
return NULL;
}
tkt = shishi_tgs_tkt (tgs);
if (!tkt)
{
shishi_error_printf (tkts->handle, "No ticket in TGS-REP?!: %s",
shishi_error (tkts->handle));
return NULL;
}
if (VERBOSENOISE (tkts->handle))
{
printf ("Received ticket:\n");
shishi_tkt_pretty_print (tkt, stdout);
}
rc = shishi_tkts_add (tkts, tkt);
if (rc != SHISHI_OK)
printf ("Could not add ticket: %s", shishi_strerror (rc));
return tkt;
}
/**
* shishi_tkts_get:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @hint: structure with characteristics of ticket to be found.
*
* Get a ticket matching given characteristics. This function first
* looks in the ticket set for a ticket, then tries to find a
* suitable TGT, possibly via an AS exchange, using
* shishi_tkts_get_tgt(), and then uses that TGT in a TGS exchange to
* get the ticket.
*
* Currently this function does not implement cross realm logic.
*
* Return value: Returns a ticket if found, or NULL if this function
* is unable to get the ticket.
**/
Shishi_tkt *
shishi_tkts_get (Shishi_tkts * tkts, Shishi_tkts_hint * hint)
{
Shishi_tkt *tkt, *tgt;
/* If we already have a matching ticket, avoid getting a new one. */
hint->startpos = 0;
tkt = shishi_tkts_find (tkts, hint);
if (tkt)
return tkt;
tgt = shishi_tkts_get_tgt (tkts, hint);
if (!tgt)
{
shishi_error_printf (tkts->handle, "Could not get TGT for ticket.");
return NULL;
}
if (shishi_tkt_match_p (tgt, hint))
return tgt;
tkt = shishi_tkts_get_tgs (tkts, hint, tgt);
if (!tkt)
{
shishi_error_printf (tkts->handle, "Could not get ticket using TGT.");
return NULL;
}
return tkt;
}
/**
* shishi_tkts_get_for_clientserver:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @client: client name to get ticket for.
* @server: server name to get ticket for.
*
* Short-hand function for getting a ticket for the given client and
* server. See shishi_tkts_get().
*
* Return value: Returns a ticket if found, or NULL.
**/
Shishi_tkt *
shishi_tkts_get_for_clientserver (Shishi_tkts * tkts,
const char *client, const char *server)
{
Shishi_tkts_hint hint;
Shishi_tkt *tkt;
memset (&hint, 0, sizeof (hint));
hint.client = (char *) client;
hint.server = (char *) server;
tkt = shishi_tkts_get (tkts, &hint);
return tkt;
}
/**
* shishi_tkts_get_for_server:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @server: server name to get ticket for.
*
* Short-hand function for getting a ticket to the given server and
* for the default principal client. See shishi_tkts_get().
*
* Return value: Returns a ticket if found, or NULL.
**/
Shishi_tkt *
shishi_tkts_get_for_server (Shishi_tkts * tkts, const char *server)
{
return shishi_tkts_get_for_clientserver
(tkts, shishi_principal_default (tkts->handle), server);
}
/**
* shishi_tkts_get_for_localservicepasswd:
* @tkts: ticket set handle as allocated by shishi_tkts().
* @service: service name to get ticket for.
* @passwd: password for the default client principal.
*
* Short-hand function for getting a ticket to the given
* local service, and for the default principal client.
* The latter's password is given as argument.
* See shishi_tkts_get().
*
* Return value: Returns a ticket if found, or NULL otherwise.
**/
Shishi_tkt *
shishi_tkts_get_for_localservicepasswd (Shishi_tkts * tkts,
const char *service,
const char *passwd)
{
Shishi_tkt *tkt;
Shishi_tkts_hint hint;
memset (&hint, 0, sizeof (hint));
hint.client = (char *) shishi_principal_default (tkts->handle);
hint.server = shishi_server_for_local_service (tkts->handle, service);
hint.passwd = (char *) passwd;
tkt = shishi_tkts_get (tkts, &hint);
free (hint.server);
return tkt;
}
|