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 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
|
#
# Copyright 2005-2012 SPARTA, Inc. All rights reserved. See the COPYING
# file distributed with this software for details
#
# DNSSEC-Tools
#
# Keyrec file routines.
#
# The routines in this module manipulate a keyrec file for the DNSSEC-
# Tools. The keyrec file contains information about the values used
# to generate a key or to sign a zone.
#
# Entries in the configuration file are of the "key value" format, with
# the value enclosed in quotes. Comments may be included by prefacing
# them with the '#' or ';' comment characters.
#
# These entries are grouped into one of three types of records:
#
# zone records - contains data used to sign a zone
# set records - contains data on the keys in a zone
# key records - contains data used to generate an encryption key
#
# Each record type has several subfields.
#
# A truncated example configuration file follows:
#
# zone "example.com"
# zonefile "db.example.com"
# zskcur "signing-set-42"
# zskpub "signing-set-43"
# endtime "+2764800" # Zone expires in 32 days.
#
# set "signing-set-42"
# zonename "example.com"
# keys "Kexample.com.+005+26000 Kexample.com.+005+55555
# keyrec_type "set"
# set_type "zskcur"
#
# key "Kexample.com.+005+26000"
# zonename "example.com"
# keyrec_type "zskcur"
# algorithm "rsasha1"
# length "2048"
# ksklife "15768000"
# revperiod "3888000"
# revtime "1103277532"
# random "-r /dev/urandom"
#
# The current implementation assumes that only one keyrec file will be
# open at a time. If module use proves this to be a naive assumption,
# this module will have to be rewritten to account for it.
#
package Net::DNS::SEC::Tools::keyrec;
require Exporter;
use strict;
use Net::DNS::SEC::Tools::conf;
use Fcntl qw(:DEFAULT :flock);
our $VERSION = "1.13";
our $MODULE_VERSION = "1.13.0";
#--------------------------------------------------------------------------
our @ISA = qw(Exporter);
our @EXPORT = qw(keyrec_creat keyrec_open keyrec_read
keyrec_filestat keyrec_names keyrec_fullrec
keyrec_recval keyrec_setval keyrec_delval
keyrec_settime keyrec_revoke_check
keyrec_add keyrec_del keyrec_newkeyrec keyrec_exists
keyrec_zonefields keyrec_setfields keyrec_keyfields
keyrec_init keyrec_discard keyrec_close
keyrec_write keyrec_saveas keyrec_keypaths
keyrec_curkrf keyrec_defkrf
keyrec_dump_hash keyrec_dump_array
keyrec_signset_newname keyrec_signset_new keyrec_signset_keys
keyrec_signset_addkey keyrec_signset_delkey
keyrec_signset_haskey keyrec_signset_clear keyrec_signsets
keyrec_signset_prefix
keyrec_fmtchk
);
#--------------------------------------------------------------------------
#
# Fields in a zone keyrec.
#
my @ZONEFIELDS = (
'keyrec_type',
'zonefile',
'signedzone',
'zskdirectory',
'kskdirectory',
'archivedir',
'dsdir',
'ksdir',
'endtime',
'kskcount',
'kskcur',
'kskpub',
'kskrev',
'rfc5011',
'zskcount',
'zskcur',
'zskpub',
'zsknew',
'serial',
'lastset',
'gends',
'szopts',
'rollmgr',
'lastcmd',
'new_kgopts',
'new_ksklength',
'new_ksklife',
'new_random',
'new_revperiod',
'new_revtime',
'new_zsklength',
'new_zsklife',
'keyrec_signsecs',
'keyrec_signdate',
);
#
# Fields in a set keyrec.
#
my @SETFIELDS = (
'keyrec_type',
'set_type',
'keys',
'zonename',
'keyrec_setsecs',
'keyrec_setdate',
);
#
# Fields in a key keyrec.
#
my @KEYFIELDS = (
'keyrec_type',
'algorithm',
'random',
'keypath', # Only set for obsolete ZSKs.
'ksklength',
'ksklife',
'revperiod',
'revtime',
'zsklength',
'zsklife',
'kgopts',
'keyrec_gensec',
'keyrec_gendate',
'zonename',
);
#--------------------------------------------------------------------------
my $DEFSETPREFIX = '-signset-'; # Default signing-set prefix.
my $curkrfname; # Name of open keyrec file.
my @keyreclines; # Keyrec lines.
my $keyreclen; # Number of keyrec lines.
my %keyrecs; # Keyrec hash table (keywords/values.)
my $modified; # File-modified flag.
#--------------------------------------------------------------------------
# Routine: keyrec_creat()
#
# Purpose: Create a DNSSEC-Tools keyrec file, if it does not exist. If
# the file already exists, this function truncates the file.
#
# Returns 1 if the file was created successfully and 0 if
# there was an error in file creation.
#
sub keyrec_creat
{
my $krf = shift; # Key record file.
#
# Create a new keyrec file, or truncate an existing one.
#
open(KEYREC,"+> $krf") || return 0;
close(KEYREC);
#
# Save the name of the new keyrec file.
#
$curkrfname = $krf;
return(1);
}
#--------------------------------------------------------------------------
# Routine: keyrec_open() DEPRECATED
#
# Purpose: This routine used to open an existing DNSSEC-Tools keyrec file.
# However, this was an unnecessary operation since keyrec_read()
# would open the file if it wasn't open.
#
# This call will eventually be removed. For now, it calls
# keyrec_filestat() to check the existence of the specified
# keyrec file. It also saves the keyrec file's name to the
# $curkrfname global.
#
# Return Values:
# 1 - if the file passed all keyrec_filestat()'s checks
# 0 - if any of keyrec_filestat()'s checks failed
#
# The success/failure meaning of these values matches the
# success/failure meaning of keyrec_open()'s original returns.
#
sub keyrec_open
{
my $krf = shift; # Keyrec file.
my $ret; # Return value.
$ret = keyrec_filestat($krf);
$curkrfname = $krf;
return(! $ret);
}
#--------------------------------------------------------------------------
# Routine: keyrec_filestat()
#
# Purpose: Checks that a given file might be a reasonable candidate for
# a DNSSEC-Tools keyrec file. The checks to be performed may
# be gleaned from the list of return values.
#
# Return Values:
# 0 - returned if the tests are all true
# 1 - an actual name wasn't given
# 2 - the file does not exist
# 3 - the file is not a regular file
# 4 - the file is not readable
# 5 - the file is empty
#
sub keyrec_filestat
{
my $krf = shift; # Keyrec file.
#
# Ensure that we were given a name.
#
if(!defined($krf) || ($krf eq ''))
{
return(1);
}
#
# Ensure the name is actually a file.
#
if(! -e $krf)
{
return(2);
}
#
# Ensure the name is actually a *file*.
#
if(! -f $krf)
{
return(3);
}
#
# Ensure the name is a readable file.
#
if(! -r $krf)
{
return(4);
}
#
# Ensure the name is actually a readable non-empty file.
#
if(-z $krf)
{
return(5);
}
#
# It is! It is a readable non-empty file!
#
return(0);
}
#--------------------------------------------------------------------------
# Routine: keyrec_read()
#
# Purpose: Open and read a DNSSEC-Tools keyrec file. The contents are
# read into the @keyreclines array and the keyrecs are broken
# out into the %keyrecs hash table.
#
sub keyrec_read
{
my $krf = shift; # Key record file.
my $name; # Name of the keyrec (zone or key.)
my $krcnt; # Number of keyrecs we found.
my @sbuf; # Buffer for stat().
#
# Make sure a keyrec file was specified.
#
if($krf eq "")
{
err("no keyrec file specified\n",-1);
return(-1);
}
#
# Make sure the keyrec file exists.
#
if(! -e $krf)
{
err("keyrec file $krf does not exist\n",-1);
return(-1);
}
#
# If a keyrec file is already open, we'll flush our buffers and
# save the file.
#
@sbuf = stat(KEYREC);
if(@sbuf != 0)
{
keyrec_close();
}
#
# Open up the keyrec file.
#
if(open(KEYREC,"< $krf") == 0)
{
err("unable to open $krf\n",-1);
return(-2);
}
$curkrfname = $krf;
#
# Initialize some data.
#
keyrec_init();
#
# Grab the lines and pop 'em into the keyreclines array. We'll also
# save each keyrec into a hash table for easy reference.
#
while(<KEYREC>)
{
my $line; # Line from the keyrec file.
my $keyword = ""; # Keyword from the line.
my $value = ""; # Keyword's value.
$line = $_;
#
# Save the line in our array of keyrec lines.
#
$keyreclines[$keyreclen] = $line;
$keyreclen++;
#
# Skip comment lines and empty lines.
#
if(($line =~ /^[ \t]*$/) || ($line =~ /^[ \t]*[;#]/))
{
next;
}
#
# Grab the keyword and value from the line. The keyword
# must be alphabetic. The value can contain alphanumerics,
# and a number of punctuation characters. The value *must*
# be enclosed in double quotes.
#
# $line =~ /^[ \t]*([a-zA-Z_]+)[ \t]+"([a-zA-Z0-9\/\-+_.,: \t]+)"/;
$line =~ /^[ \t]*([a-zA-Z0-9_]+)[ \t]+"([a-zA-Z0-9\/\-+_.,: \t]*)"/;
$keyword = $1;
$value = $2;
# print "keyrec_read: keyword <$keyword>\t\t<$value>\n";
#
# If the keyword is "key" or "zone", then we're starting a
# new record. We'll save the name of the keyrec, as well
# as the record type, and then proceed on to the next line.
#
if(($keyword =~ /^zone$/i) ||
($keyword =~ /^set$/i) ||
($keyword =~ /^key$/i))
{
$name = $value;
#
# If this name has already been used for a keyrec,
# we'll whinge, clean up, and return. No keyrecs
# will be retained.
#
if(exists($keyrecs{$name}))
{
keyrec_discard();
err("keyrec_read: duplicate record name \"$name\"; aborting...\n",-1);
return(-3);
}
keyrec_newkeyrec($name,$keyword);
next;
}
#
# Save this subfield into the keyrec's collection.
#
$keyrecs{$name}{$keyword} = $value;
}
#
# Return the number of keyrecs we found.
#
$krcnt = keys(%keyrecs);
return($krcnt);
}
#--------------------------------------------------------------------------
# Routine: keyrec_revoke_check()
#
# Purpose: Checks if the given KSK is within its revocation period or
# if it has fallen out of the revocation period. The keyrec's
# revtime and revperiod are consulted. If the revoked time
# subtracted from the current time exceeds the revocation, then
# the key should be moved from the revoked state to the obsolete
# state.
#
# This routine performs the revocation check only! It does not
# manipulate the key types itself; that must be done as needed
# by callers of this routine.
#
# Return Values:
# 1 - returned if the key is outside the revocation period and
# should be marked obsolete.
# 0 - returned if the key is in the revocation period and
# should be left revoked.
# -1 - returned on error
#
sub keyrec_revoke_check
{
my $key = shift; # Name of key to check.
my $keytype; # Key's type.
my $revtime; # Key's revoke time.
my $revperiod; # Key's revoke period.
my $now = time(); # Current time.
#
# Get the key's type.
#
$keytype = keyrec_recval($key, 'keyrec_type');
#
# Ensure we were given a revoked KSK keyrec.
#
if($keytype ne 'kskrev')
{
err("keyrec_revoke_check: $key: invalid type \"$keytype\"\n", -1);
return(-1);;
}
#
# Get the key's revocation information.
#
$revtime = keyrec_recval($key, 'revtime');
$revperiod = keyrec_recval($key, 'revperiod');
#
# Ensure the key has a revocation date.
#
if(!defined($revtime))
{
err("keyrec_revoke_check: $key: undefined revocation time\n", -1);
return(-1);
}
#
# Ensure the key has the proper revocation data.
#
if(!defined($revperiod))
{
err("keyrec_revoke_check: $key: undefined revocation period\n", -1);
return(-1);
}
#
# We'll return a flag indicating whether or not the key has exceeded
# its revocation period. If it has, the key is obsolete; if not, it
# will stay in the revoked state.
#
if(($now - $revtime) > $revperiod)
{
return(1);
}
return(0);
}
#--------------------------------------------------------------------------
# Routine: keyrec_names()
#
# Purpose: Smoosh the keyrec names into an array and return the array.
#
sub keyrec_names
{
my $krn; # Keyrec name index.
my @names = (); # Array for keyrec names.
foreach $krn (sort(keys(%keyrecs)))
{
push @names, $krn;
}
return(@names);
}
#--------------------------------------------------------------------------
# Routine: keyrec_keypaths()
#
# Purpose: Return an array of the paths of a zone keyrec's keys.
# The type of key to return is specified by the caller.
#
sub keyrec_keypaths
{
my $krn = shift; # Keyrec name.
my $krt = shift; # Keyrec type.
my $sset; # Signing set name.
my $keylist; # List of keys.
my @paths = (); # Array for keyrec names.
#
# Ensure the key type is in the expected case.
#
$krt = lc($krt);
#
# Ensure this is a zone keyrec.
#
return(@paths) if($keyrecs{$krn}{'keyrec_type'} ne "zone");
#
# Ensure the keyrec type is valid.
#
if(($krt ne "kskcur") && ($krt ne "kskpub") &&
($krt ne "kskrev") && ($krt ne "kskobs") &&
($krt ne "zskcur") && ($krt ne "zskpub") &&
($krt ne "zsknew") && ($krt ne "zskobs") &&
($krt ne "ksk") && ($krt ne "zsk") && ($krt ne "all"))
{
return(@paths);
}
#
# Handle the special keytypes.
#
if($krt eq "all")
{
@paths = keyrec_keypaths($krn,"ksk");
@paths = (@paths, keyrec_keypaths($krn,"zsk"));
return(@paths);
}
elsif($krt eq "ksk")
{
@paths = keyrec_keypaths($krn,"kskcur");
@paths = (@paths, keyrec_keypaths($krn,"kskpub"));
@paths = (@paths, keyrec_keypaths($krn,"kskrev"));
@paths = (@paths, keyrec_keypaths($krn,"kskobs"));
return(@paths);
}
elsif($krt eq "zsk")
{
@paths = keyrec_keypaths($krn,"zskcur");
@paths = (@paths, keyrec_keypaths($krn,"zskpub"));
@paths = (@paths, keyrec_keypaths($krn,"zsknew"));
@paths = (@paths, keyrec_keypaths($krn,"zskobs"));
return(@paths);
}
#
# Get and verify the name of the appropriate signing set.
# We have to handle obsolete keys differently than other keys
# since obsolete keys don't have a reference in the zone keyrec.
#
if(($krt ne 'kskobs') && ($krt ne 'zskobs'))
{
return(@paths) if(!defined($keyrecs{$krn}{$krt}));
#
# Get and verify the key list.
#
$sset = $keyrecs{$krn}{$krt};
return(@paths) if(!defined($keyrecs{$sset}{'keys'}));
$keylist = $keyrecs{$sset}{'keys'};
}
else
{
foreach my $kname (keyrec_names())
{
next if($keyrecs{$kname}{'set_type'} ne $krt);
$keylist .= " $keyrecs{$kname}{'keys'}";
}
}
#
# Get the keys' paths and add 'em to the path array.
#
foreach my $kn (sort(split /[\s,]/, $keylist))
{
my @newpaths;
@newpaths = getsetpaths($kn,$krt);
push @paths, @newpaths if(@newpaths != 0);
}
#
# Return the path list.
#
return(@paths);
}
#--------------------------------------------------------------------------
# Routine: getsetpaths()
#
# Purpose: Build a list of the keypaths for a given set. This will
# fetch the paths for intermediate signing sets.
#
# This routine is not exported.
#
sub getsetpaths
{
my $kn = shift; # Name of key to get.
my $kt = shift; # Type of key to get.
my @paths = (); # Set's path list.
#
# Verify that this key exists.
#
return(@paths) if($kn eq '') || (! defined($keyrecs{$kn}));
#
# If this is a set keyrec, we'll pull the key paths from its keylist.
# Otherwise, it's assumed to be a key keyrec and we'll take the key
# path from itself.
#
if($keyrecs{$kn}{'keyrec_type'} eq 'set')
{
my $keylist = $keyrecs{$kn}{'keys'};
#
# Get the keys' paths and add 'em to the path array.
#
foreach my $kname (split /[\s,]/, $keylist)
{
my @newpaths;
@newpaths = getsetpaths($kname,$kt);
push @paths, @newpaths if(@newpaths != 0);
}
}
else
{
#
# Verify that this key is the right type.
#
return(@paths) if($keyrecs{$kn}{'keyrec_type'} ne $kt);
#
# Push the key's path onto the path list.
#
if($keyrecs{$kn}{'keypath'} ne '')
{
push @paths, $keyrecs{$kn}{'keypath'};
}
}
#
# Return the path list.
#
return(@paths);
}
#--------------------------------------------------------------------------
# Routine: keyrec_exists()
#
# Purpose: Smoosh the keyrec names into an array and return the array.
#
sub keyrec_exists
{
my $name = shift; # The name to check.
return(1) if(exists($keyrecs{$name}));
return(0);
}
#--------------------------------------------------------------------------
# Routine: keyrec_fullrec()
#
# Purpose: Return all entries in a given keyrec.
#
sub keyrec_fullrec
{
my $name = shift; # The record to retrieve.
my $krec = $keyrecs{$name}; # The retrieved record.
return($krec);
}
#--------------------------------------------------------------------------
# Routine: keyrec_recval()
#
# Purpose: Return the value of a name/subfield pair.
#
sub keyrec_recval
{
my $name = shift; # The record to retrieve.
my $field = shift; # The field to retrieve.
my $val = $keyrecs{$name}{$field}; # The retrieved field.
return($val);
}
#--------------------------------------------------------------------------
# Routine: keyrec_setval()
#
# Purpose: Set the value of a name/subfield pair. The value is saved
# in both %keyrecs and in @keyreclines. The $modified file-
# modified flag is updated, along with the length $keyreclen.
#
sub keyrec_setval
{
my $krtype = shift; # Type of keyrec (for new keyrecs.)
my $name = shift; # Name of keyrec we're modifying.
my $field = shift; # Keyrec's subfield to be changed.
my $val = shift; # New value for the keyrec's subfield.
my $found = 0; # Keyrec-found flag.
my $fldind; # Loop index.
my $krind; # Loop index for finding keyrec.
my $lastfld = 0; # Last found field in @keyreclines.
#
# Make sure we've got the correct count of keyrec lines.
#
$keyreclen = @keyreclines;
#
# If a keyrec of the specified name doesn't exist, we'll create a
# new one. If the field is "keyrec_type", then we're creating a new
# keyrec. We'll add it to @keyreclines and %keyrecs.
#
if(!exists($keyrecs{$name}))
{
#
# Add the keyrec to the %keyrecs hash.
#
if(keyrec_newkeyrec($name,$krtype) < 0)
{
return(-1);
}
#
# Start the new keyrec in @keyreclines.
#
$keyreclines[$keyreclen] = "\n";
$keyreclen++;
$keyreclines[$keyreclen] = "$krtype\t\"$name\"\n";
$keyreclen++;
}
#
# Set the new value for the name/field in %keyrecs.
#
$keyrecs{$name}{$field} = $val;
#
# Find the appropriate entry to modify in @keyreclines. If the
# given field isn't set in $name's keyrec, we'll insert this as
# a new field at the end of that keyrec.
#
for($krind=0;$krind<$keyreclen;$krind++)
{
my $line = $keyreclines[$krind]; # Line in keyrec file.
my $krtype; # Keyrec type.
my $krname; # Keyrec name.
#
# Dig out the line's keyword and value.
#
# $line =~ /^[ \t]*([a-zA-Z_]+)[ \t]+"([a-zA-Z0-9\/\-+_.,: \t]+)"/;
$line =~ /^[ \t]*([a-zA-Z0-9_]+)[ \t]+"([a-zA-Z0-9\/\-+_.,: \t]*)"/;
$krtype = $1;
$krname = $2;
#
# If this line has the keyrec's name and is the start of a
# new keyrec, then we've found our man.
#
# IMPORTANT NOTE: We will *always* find the keyrec we're
# looking for. The exists() check above
# ensures that there will be a keyrec with
# the name we want.
#
if((lc($krname) eq lc($name)) &&
((lc($krtype) eq "zone") ||
(lc($krtype) eq "set") ||
(lc($krtype) eq "key")))
{
last;
}
}
#
# Find the specified field's entry in the keyrec's lines in
# @keyreclines. We'll skip over lines that don't have a keyword
# and dquotes-enclosed value. If we hit the next keyrec (marked
# by a zone or key line) then we'll stop looking and add a new
# entry at the end of the keyrec's fields.
#
for($fldind=$krind+1;$fldind<$keyreclen;$fldind++)
{
my $line = $keyreclines[$fldind]; # Line in keyrec file.
my $lkw; # Line's keyword.
my $lval; # Line's value.
#
# Get the line's keyword and value.
#
# $line =~ /^[ \t]*([a-zA-Z_]+)[ \t]+"([a-zA-Z0-9\/\-+_.,: \t]+)"/;
$line =~ /^[ \t]*([a-zA-Z0-9_]+)[ \t]+"([a-zA-Z0-9\/\-+_.,: \t]*)"/;
$lkw = $1;
$lval = $2;
#
# Skip empty lines.
#
if($lkw eq "")
{
next;
}
#
# If we hit the beginning of the next keyrec without
# finding the field, drop out and insert it.
#
if((lc($lkw) eq "zone") ||
(lc($lkw) eq "set") ||
(lc($lkw) eq "key"))
{
last;
}
#
# Save the index of the last field we've looked at that
# belongs to the keyrec.
#
$lastfld = $fldind;
#
# If we found the field, set the found flag, drop out and
# modify it.
#
if(lc($lkw) eq lc($field))
{
$found = 1;
last;
}
}
#
# If we found the entry, we'll modify it in place.
# If we didn't find the entry, we'll insert a new line into the array.
#
if($found)
{
$keyreclines[$fldind] =~ s/"([a-zA-Z0-9\/\-+_.,: \t]*)"/"$val"/;
}
else
{
my $newline; # New keyrec field to save.
my @endarr; # Last part of @keyreclines.
#
# Create the new line. If the keyword is longer than 7
# characters, we'll only use a single tab between the
# keyword and the value. This is to do some rough, simple
# formatting to keep the keyrec file somewhat orderly.
# This assumes eight-character tabstops.
#
if(length($field) > 7)
{
$newline = "\t$field\t\"$val\"\n";
}
else
{
$newline = "\t$field\t\t\"$val\"\n";
}
#
# If the starting keyrec line is the last line in the file,
# we'll just push the new line at the end. If it's somewhere
# in the middle, we'll do the magic to insert it at the start
# of the keyrec.
#
$lastfld++ if($keyreclines[$lastfld] eq "\n");
@endarr = splice(@keyreclines,$lastfld+1);
push @keyreclines, $newline;
push @keyreclines, @endarr;
#
# Update the array length counter.
#
$keyreclen = @keyreclines;
}
#
# Tell the world (or at least the module) that the file has
# been modified.
#
$modified = 1;
return(0);
}
#--------------------------------------------------------------------------
# Routine: keyrec_delval()
#
# Purpose: Delete a name/subfield pair from a keyrec. The value is
# removed from both %keyrecs and in @keyreclines. The $modified
# file-modified flag is updated, along with the length $keyreclen.
#
sub keyrec_delval
{
my $name = shift; # Name of keyrec we're modifying.
my $field = shift; # Keyrec's subfield to be changed.
my $fldind; # Loop index.
my $krind; # Loop index for finding keyrec.
my $lastfld = 0; # Last found field in @keyreclines.
#
# Return if a keyrec of the specified name doesn't exist.
#
return(-1) if(!exists($keyrecs{$name}));
#
# Make sure we've got the correct count of keyrec lines.
#
$keyreclen = @keyreclines;
#
# Find the appropriate entry to delete from @keyreclines. If
# the given field isn't set in $name's keyrec, we'll return.
#
for($krind=0;$krind<$keyreclen;$krind++)
{
my $line = $keyreclines[$krind]; # Line in keyrec file.
my $krtype; # Keyrec type.
my $krname; # Keyrec name.
#
# Dig out the line's keyword and value.
#
$line =~ /^[ \t]*([a-zA-Z0-9_]+)[ \t]+"([a-zA-Z0-9\/\-+_.,: \t]*)"/;
$krtype = $1;
$krname = $2;
#
# If this line has the keyrec's name and is the start of a
# new keyrec, then we've found our man.
#
# IMPORTANT NOTE: We will *always* find the keyrec we're
# looking for. The exists() check above
# ensures that there will be a keyrec with
# the name we want.
#
if((lc($krname) eq lc($name)) &&
((lc($krtype) eq "zone") ||
(lc($krtype) eq "set") ||
(lc($krtype) eq "key")))
{
last;
}
}
#
# Find the specified field's entry in the keyrec's lines in
# @keyreclines. We'll skip over lines that don't have a keyword
# and dquotes-enclosed value. If we hit the next keyrec (marked
# by a zone or key line) then we'll stop looking and add a new
# entry at the end of the keyrec's fields.
#
for($fldind=$krind+1;$fldind<$keyreclen;$fldind++)
{
my $line = $keyreclines[$fldind]; # Line in keyrec file.
my $lkw; # Line's keyword.
my $lval; # Line's value.
#
# Get the line's keyword and value.
#
$line =~ /^[ \t]*([a-zA-Z0-9_]+)[ \t]+"([a-zA-Z0-9\/\-+_.,: \t]*)"/;
$lkw = $1;
$lval = $2;
#
# Skip empty lines.
#
next if($lkw eq "");
#
# If we hit the beginning of the next keyrec without
# finding the field, drop out and insert it.
#
if((lc($lkw) eq "zone") ||
(lc($lkw) eq "set") ||
(lc($lkw) eq "key"))
{
last;
}
#
# Save the index of the last field we've looked at that
# belongs to the keyrec.
#
$lastfld = $fldind;
#
# If we found the field, set the found flag, delete it and
# tell the world (or at least the module) that the file has
# been modified.
#
if(lc($lkw) eq lc($field))
{
#
# Delete the field from %keyrecs and @keyreclines.
#
delete $keyrecs{$name}{$field};
splice(@keyreclines, $fldind, 1);
$modified = 1;
$keyreclen--;
return(1);
}
}
#
# We didn't find the entry.
#
return(0);
}
#--------------------------------------------------------------------------
# Routine: keyrec_add()
#
# Purpose: Adds a new keyrec and fields to %keyrecs and $keyreclines.
#
sub keyrec_add
{
my $krtype = shift; # Type of keyrec we're creating.
my $krname = shift; # Name of keyrec we're creating.
my $flds = shift; # Reference to keyrec fields.
my $chronosecs; # Current time in seconds.
my $chronostr; # Current time string.
my $secsstr; # Hash key for time in seconds.
my $datestr; # Hash key for time string.
my @krlines; # Copy of @keyreclines.
my $krlen; # Length of @krlines.
my %fields; # Keyrec fields.
my @getfields; # Hash fields to retrieve.
#
# Ensure we've got the minimum required arguments.
# (We're checking for a set name here; type is checked below.)
#
if(!defined($krtype) || !defined($krname) || ($krname eq ''))
{
return(-1);
}
#
# Get the timestamp.
#
$chronosecs = time();
$chronostr = gmtime($chronosecs);
#
# Set the fields, by type, that we'll grab from the caller's hash.
#
if($krtype eq "zone")
{
@getfields = @ZONEFIELDS;
$secsstr = 'keyrec_signsecs';
$datestr = 'keyrec_signdate';
}
elsif($krtype eq "set")
{
@getfields = @SETFIELDS;
$secsstr = 'keyrec_setsecs';
$datestr = 'keyrec_setdate';
}
elsif($krtype eq "key")
{
@getfields = @KEYFIELDS;
$secsstr = 'keyrec_gensecs';
$datestr = 'keyrec_gendate';
}
else
{
return(-1);
}
#
# Create the basic keyrec info.
#
if(keyrec_newkeyrec($krname,$krtype) < 0)
{
return(-1);
}
#
# Make sure we've got the correct count of keyrec lines.
#
@krlines = @keyreclines;
$krlen = @krlines;
#
# Add the new keyrec's first line to the end of the keyrec table.
#
$krlines[$krlen] = "\n";
$krlen++;
$krlines[$krlen] = "$krtype\t\"$krname\"\n";
$krlen++;
#
# Fill the new keyrec with the caller's hash fields and add it to
# the end of the keyrec table.
#
if(defined($flds))
{
%fields = %$flds;
foreach my $fn (@getfields)
{
my $spacing = "\t\t"; # Spacing string.
#
# Only add the timestamp at the end, and only
# add the timestamp we're going to put in.
#
if(($fn eq $secsstr) || ($fn eq $datestr))
{
next;
}
#
# Only add fields defined for the keyrec's type.
#
next if(!defined($fields{$fn}));
#
# Handle KSK-specific fields.
#
if($fields{'keyrec_type'} !~ /^ksk/)
{
if(($fn eq 'ksklength') || ($fn eq 'ksklife') ||
($fn eq 'revperiod') || ($fn eq 'revtime'))
{
next;
}
}
#
# Handle ZSK-specific key fields.
#
if($fields{'keyrec_type'} !~ /^zsk/)
{
if(($fn eq 'zsklength') ||
($fn eq 'zsklife'))
{
next;
}
}
#
# Drop back to a single tab between key and value
# if the key name is long.
#
$spacing = "\t" if(length($fn) > 7);
#
# Add the field to the hash table and to the keyrec
# file contents array.
#
$keyrecs{$krname}{$fn} = $fields{$fn};
$krlines[$krlen] = "\t$fn$spacing\"$fields{$fn}\"\n";
$krlen++;
}
}
#
# Set a timestamp for this entry.
#
$keyrecs{$krname}{$secsstr} = $chronosecs;
$keyrecs{$krname}{$datestr} = $chronostr;
$krlines[$krlen] = "\t$secsstr\t\"$chronosecs\"\n";
$krlen++;
$krlines[$krlen] = "\t$datestr\t\"$chronostr\"\n";
$krlen++;
#
# Put a blank line after the final line of the keyrec.
#
$krlines[$krlen] = "\n";
$krlen++;
#
# Save the new keyrec array and mark the keyrec file as modified.
#
@keyreclines = @krlines;
$keyreclen = $krlen;
$modified = 1;
return(0);
}
#--------------------------------------------------------------------------
# Routine: keyrec_del()
#
# Purpose: Deletes a keyrec and fields from %keyrecs and $keyreclines.
#
sub keyrec_del
{
my $krname = shift; # Name of keyrec we're creating.
my %keyrec; # Keyrec to be deleted.
my $krr; # Keyrec reference.
my $krtype; # Keyrec's type.
my $ind; # Index into keyreclines.
my $krind; # Index to keyrec's first line.
my $line; # Keyrec line from @keyreclines.
my $lkey; # Keyrec line's key.
my $lval; # Keyrec line's value.
my $len; # Length of array slice to delete.
#
# Don't allow empty keyrec names.
#
return(-1) if($krname eq "");
#
# Get a copy of the keyrec from the keyrec hash and then delete
# the original.
#
$krr = $keyrecs{$krname};
%keyrec = %$krr;
delete $keyrecs{$krname};
#
# Get the keyrec's type.
#
if($keyrec{'keyrec_type'} eq "zone")
{
$krtype = "zone";
}
elsif($keyrec{'keyrec_type'} eq "set")
{
$krtype = "set";
}
else
{
$krtype = "key";
}
#
# Make sure we've got the correct count of keyrec lines.
#
$keyreclen = @keyreclines;
#
# Find the index of the first line for this keyrec in the
# list of file lines.
#
for($ind = 0;$ind < $keyreclen; $ind++)
{
$line = $keyreclines[$ind];
$line =~ /\s*(\S+)\s+(\S+)/;
$lkey = $1;
$lval = $2;
$lval =~ s/"//g;
last if(($lkey eq $krtype) && ($lval eq $krname));
}
$krind = $ind;
#
# If we didn't find a keyrec with this name, return failure.
#
return(-1) if($ind == $keyreclen);
#
# Find the beginning of the next keyrec.
#
for($ind = $krind+1;$ind < $keyreclen; $ind++)
{
$line = $keyreclines[$ind];
$line =~ /\s*(\S+)\s+(\S+)/;
$lkey = $1;
$lval = $2;
last if(($lkey eq "zone") ||
($lkey eq "set") ||
($lkey eq "key"));
}
$ind--;
#
# Find the end of the previous keyrec (the one to be deleted.)
#
while($ind > $krind)
{
last if($keyreclines[$ind] ne "\n");
$ind--;
}
#
# Delete the keyrec from @keyreclines.
#
$len = $ind - $krind + 1;
splice(@keyreclines,$krind,$len);
$keyreclen -= $len;
#
# Tell the world (or at least the module) that the file has
# been modified.
#
$modified = 1;
return(0);
}
#--------------------------------------------------------------------------
# Routine: keyrec_newkeyrec()
#
# Purpose: Creates a keyrec in %keyrecs. The name and type fields of
# the keyrec are set. This does not add the keyrec to the
# file or @keyreclines.
#
sub keyrec_newkeyrec
{
my $name = shift; # Name of keyrec we're creating.
my $type = shift; # Type of keyrec we're creating.
#
# Ensure we're only getting a valid type.
#
if(($type ne "zone") && ($type ne "set") && ($type ne "key"))
{
return(-1);
}
$keyrecs{$name}{"keyrec_name"} = $name;
$keyrecs{$name}{"keyrec_type"} = $type;
return(0);
}
#--------------------------------------------------------------------------
# Routine: keyrec_settime()
#
# Purpose: Set the time value of a keyrec to the current time. If
# the keyrec (identified by name and type) doesn't exist,
# a new one will be created.
#
sub keyrec_settime
{
my $krtype = shift; # Type of keyrec (for new keyrecs.)
my $name = shift; # Name of keyrec we're modifying.
my $chronosecs; # Seconds since epoch.
my $chronostr; # String version of now.
#
# Get the timestamp.
#
$chronosecs = time();
$chronostr = gmtime($chronosecs);
#
# Set the timestamp in the entry, with the fields set depending
# on the keyrec type.
#
if($krtype eq "zone")
{
keyrec_setval($krtype,$name,'keyrec_signsecs',$chronosecs);
keyrec_setval($krtype,$name,'keyrec_signdate',$chronostr);
}
elsif($krtype eq "set")
{
keyrec_setval($krtype,$name,'keyrec_setsecs',$chronosecs);
keyrec_setval($krtype,$name,'keyrec_setdate',$chronostr);
}
elsif($krtype eq "key")
{
keyrec_setval($krtype,$name,'keyrec_gensecs',$chronosecs);
keyrec_setval($krtype,$name,'keyrec_gendate',$chronostr);
}
}
#--------------------------------------------------------------------------
# Routine: keyrec_zonefields()
#
# Purpose: Return the list of zone fields.
#
sub keyrec_zonefields
{
return(@ZONEFIELDS);
}
#--------------------------------------------------------------------------
# Routine: keyrec_setfields()
#
# Purpose: Return the list of set fields.
#
sub keyrec_setfields
{
return(@SETFIELDS);
}
#--------------------------------------------------------------------------
# Routine: keyrec_keyfields()
#
# Purpose: Return the list of key fields.
#
sub keyrec_keyfields
{
return(@KEYFIELDS);
}
#----------------------------------------------------------------------
# Routine: keyrec_signset_newname()
#
# Purpose: Create a new signing set name based on the last name
# used for the zone.
#
sub keyrec_signset_newname
{
my $zone = shift; # Zone for lastset.
my $setname; # Name of zone's last set.
my $oldind; # Old index of zone's last set.
my $newind; # New index of zone's last set.
my $ssprefix; # Signing-set prefix.
# print "keyrec_signset_newname($zone): down in\n";
#
# Get the zone's last signing set.
#
$setname = $keyrecs{$zone}{'lastset'};
$setname = "$zone-signset-000" if(!defined($keyrecs{$zone}{'lastset'}));
#
# Build the signing-set prefix.
#
$ssprefix = keyrec_signset_prefix($zone);
#
# Get the first number in the set name. If there isn't a number
# in the set name, we'll append a zero.
#
$setname =~ /$ssprefix(\d+)/;
$oldind = $1;
if($oldind eq '')
{
$setname = $setname . "000";
$oldind = 0;
}
#
# We'll increment the old index and use that as our new index.
# The set name in our pile o' options will be changed to the new name.
#
$newind = int($oldind) + 1;
while(length($newind) < 5)
{
$newind = "0$newind";
}
$setname =~ s/$ssprefix$oldind/$ssprefix$newind/;
keyrec_setval('zone',$zone,'lastset',$setname);
#
# Return the generated signing set name.
#
return($setname);
}
#--------------------------------------------------------------------------
# Routine: keyrec_signset_new()
#
# Purpose: Add a new signing set keyrec. If the signing set keyrec hasn't
# yet been added with keyrec_add(), then we'll add it now.
#
sub keyrec_signset_new
{
my $zone = shift; # Signing set's zone.
my $name = shift; # Signing set name we're creating.
my $type = shift; # Type of signing Set we're creating.
my $ret; # Return code from keyrec_setval().
#
# Ensure the given set type is valid.
#
if(($type ne "kskcur") && ($type ne "zskcur") &&
($type ne "kskpub") && ($type ne "zskpub") &&
($type ne "kskrev") && ($type ne "zsknew") &&
($type ne "kskobs") && ($type ne "zskobs"))
{
return(-1);
}
#
# Create a new keyrec for the given name if it doesn't exist.
#
if(!exists($keyrecs{$name}))
{
if(keyrec_add('set',$name) < 0)
{
return(-1);
}
}
#
# Set the type and a zone connection, and we're done.
#
$ret = keyrec_setval('set',$name,'zonename',$zone);
return($ret) if($ret != 0);
$ret = keyrec_setval('set',$name,'set_type',$type);
return($ret);
}
#--------------------------------------------------------------------------
# Routine: keyrec_signset_addkey()
#
# Purpose: Add a key to a signing set for the specified keyrec.
#
sub keyrec_signset_addkey
{
my $name = shift; # Keyrec to modify.
my $keys; # Keyrec's signing set.
my @keys; # Keyrec's signing set array.
my $newkeys; # New keys to add.
my $ret; # Return code from keyrec_setval().
#
# Return failure if the named keyrec doesn't exist.
#
if(!exists($keyrecs{$name}))
{
return(0) if(keyrec_add('set',$name) < 0);
}
#
# Return failure if it isn't a signing set keyrec.
#
if($keyrecs{$name}{'keyrec_type'} ne 'set')
{
return(0) if(keyrec_add('set',$name) < 0);
}
#
# Blob the listed keys together.
#
$newkeys = join ' ', @_;
#
# Get the keyrec's signing set and add the new keys.
#
$keys = $keyrecs{$name}{'keys'};
$keys = "$keys $newkeys";
#
# Format the keys string a bit.
#
$keys =~ s/^[ ]*//;
$keys =~ s/[ ]+/ /g;
#
# Sort the keyrecs names.
# (This isn't essential, but makes things nice and tidy.)
#
@keys = split / /, $keys;
$keys = join ' ', sort(@keys);
#
# Add the set of keys and away we go!
#
$ret = keyrec_setval('set',$name,'keys',$keys);
keyrec_settime('set',$name);
return($ret);
}
#--------------------------------------------------------------------------
# Routine: keyrec_signset_delkey()
#
# Purpose: Delete an entry from a signing set for the specified keyrec.
#
sub keyrec_signset_delkey
{
my $name = shift; # Keyrec to modify.
my $key = shift; # Signing set name to delete.
my $keys; # Keyrec's signing set as a string.
my @keys; # Keyrec's signing set as an array.
my $ret; # Return code from keyrec_setval().
#
# Return failure if the named keyrec doesn't exist.
#
if(!exists($keyrecs{$name}))
{
return(0) if(keyrec_add('set',$name) < 0);
}
#
# Return failure if it isn't a signing set keyrec.
#
if($keyrecs{$name}{'keyrec_type'} ne 'set')
{
return(0) if(keyrec_add('set',$name) < 0);
}
#
# Get the keyrec's signing set into an array of names.
#
$keys = $keyrecs{$name}{'keys'};
@keys = split / /, $keys;
#
# Remove the specified name from the signing-set array.
#
for(my $ind = 0;$ind < @keys; $ind++)
{
if($keys[$ind] eq $key)
{
splice(@keys, $ind, 1);
}
}
#
# Build and format the keys string a bit.
#
$keys = join(' ', @keys);
$keys =~ s/^[ ]*//;
$keys =~ s/[ ]+/ /g;
#
# Delete the key.
#
$ret = keyrec_setval('set',$name,'keys',$keys);
keyrec_settime('set',$name);
return($ret);
}
#--------------------------------------------------------------------------
# Routine: keyrec_signset_haskey()
#
# Purpose: Check if a signing set contains the specified keyrec.
#
# Returns 1 if the set holds the key.
# Returns 0 if the set doesn't hold the key.
#
sub keyrec_signset_haskey
{
my $name = shift; # Keyrec to check.
my $key = shift; # Signing set name to delete.
my $keys; # Keyrec's signing set as a string.
my @keys; # Keyrec's signing set as an array.
my $ret; # Return code from keyrec_setval().
#
# Return failure if the named keyrec doesn't exist.
#
return(0) if(!exists($keyrecs{$name}));
#
# Return failure if it isn't a signing set keyrec.
#
if($keyrecs{$name}{'keyrec_type'} ne 'set')
{
return(0);
}
#
# Get the keyrec's signing set into an array of names.
#
$keys = $keyrecs{$name}{'keys'};
@keys = split / /, $keys;
#
# Return success if the specified name is in the signing-set array.
#
for(my $ind = 0;$ind < @keys; $ind++)
{
return(1) if($keys[$ind] eq $key);
}
return(0);
}
#--------------------------------------------------------------------------
# Routine: keyrec_signset_clear()
#
# Purpose: Delete all keys for the specified signing set.
#
sub keyrec_signset_clear
{
my $name = shift; # Keyrec to modify.
my $ret; # Return code from keyrec_setval().
#
# Return failure if the named keyrec doesn't exist.
#
if(!exists($keyrecs{$name}))
{
return(0) if(keyrec_add('set',$name) < 0);
}
#
# Return failure if it isn't a signing set keyrec.
#
if($keyrecs{$name}{'keyrec_type'} ne 'set')
{
return(0) if(keyrec_add('set',$name) < 0);
}
#
# Clear the set of keys.
#
$ret = keyrec_setval('set',$name,'keys','');
return($ret);
}
#--------------------------------------------------------------------------
# Routine: keyrec_signsets()
#
# Purpose: Return the names of the signing sets in the keyrec file.
#
sub keyrec_signsets
{
my @signset = (); # Signing set.
my $ret; # Return code from keyrec_setval().
#
# Get the names of the signing sets that are listed as being sets.
#
foreach my $kr (keys(%keyrecs))
{
push @signset, $kr if($keyrecs{$kr}{'keyrec_type'} eq 'set');
}
#
# Get the names of the signing sets that are revoked or obsolete.
# These kinda look like they're key keyrecs, but they really aren't.
#
foreach my $kr (keys(%keyrecs))
{
push @signset, $kr if(defined($keyrecs{$kr}{'set_type'}));
}
#
# Return the signing set names.
#
return(@signset);
}
#--------------------------------------------------------------------------
# Routine: keyrec_signsets_keys()
#
# Purpose: Return the names of the keys in a specified signing set.
#
sub keyrec_signset_keys
{
my $krname = shift; # Keyrec name to examine.
my $sstype = shift; # Signing set type to expand.
my $error = ''; # Error return.
#
# Ensure the named keyrec exists.
#
return(undef) if(!defined($keyrecs{$krname}));
#
# If the named keyrec is a zone keyrec, we'll get the signing
# set keyrec specified in the second argument.
#
if($keyrecs{$krname}{'keyrec_type'} eq 'zone')
{
#
# Ensure we were given a proper signing set type.
#
if(($sstype ne 'kskcur') && ($sstype ne 'kskpub') &&
($sstype ne 'kskrev') && ($sstype ne 'kskobs') &&
($sstype ne 'zskcur') && ($sstype ne 'zskpub') &&
($sstype ne 'zsknew') && ($sstype ne 'zskobs'))
{
return(undef);
}
#
# Use the signing set name instead of the zone name.
#
$krname = $keyrecs{$krname}{$sstype};
}
#
# Ensure the supposed signing set is actually a signing set.
#
return(undef) if($keyrecs{$krname}{'keyrec_type'} ne 'set');
#
# Return the signing set's key names.
#
return($keyrecs{$krname}{'keys'});
}
#--------------------------------------------------------------------------
# Routine: keyrec_signset_prefix()
#
# Purpose: Build and return the signing-set prefix.
#
sub keyrec_signset_prefix
{
my $zone = shift; # Zone for this signing set.
my $prefix; # Signing-set prefix.
$prefix = $zone . $DEFSETPREFIX;
return($prefix);
}
#--------------------------------------------------------------------------
# Routine: keyrec_init()
#
# Purpose: Initialize the internal data.
#
sub keyrec_init
{
%keyrecs = ();
@keyreclines = ();
$keyreclen = 0;
$modified = 0;
}
#--------------------------------------------------------------------------
# Routine: keyrec_curkrf()
#
# Purpose: Get the current keyrec file.
#
sub keyrec_curkrf
{
return($curkrfname);
}
#--------------------------------------------------------------------------
# Routine: keyrec_defkrf()
#
# Purpose: Get the default keyrec file defined in the DNSSEC-Tools
# configuration file.
#
sub keyrec_defkrf
{
my %dtconf; # Configuration info.
my $krf; # Keyrec file name.
#
# Get the configuration info.
#
%dtconf = parseconfig();
#
# Check the config file for a default keyrec filename.
#
$krf = $dtconf{'default_keyrec'};
return($krf);
}
#--------------------------------------------------------------------------
# Routine: keyrec_discard()
#
# Purpose: Discard the current keyrec file -- don't save the contents,
# don't delete the file, reset all internal fields.
#
sub keyrec_discard
{
close(KEYREC);
keyrec_init();
}
#--------------------------------------------------------------------------
# Routine: keyrec_close()
#
# Purpose: Save the key record file and close the descriptor.
#
sub keyrec_close
{
keyrec_write();
close(KEYREC);
#
# Reset the keyrec's name.
#
$curkrfname = '';
}
#--------------------------------------------------------------------------
# Routine: keyrec_write()
#
# Purpose: Save the key record file and leave the file handle open.
# We'll get an exclusive lock on the keyrec file in order
# to (try to) ensure we're the only ones writing the file.
#
# We'll make a (hopefully atomic) copy of the in-core keyrec
# lines prior to trying to write. This is an attempt to
# keep the data from being mucked with while we're using it.
#
sub keyrec_write
{
my $krc = ""; # Concatenated keyrec file contents.
my $ofh; # Old file handle.
my @krlines = @keyreclines; # Copy of The Keyrec.
my $krlen; # Number of lines in The Keyrec.
#
# If the file hasn't changed, we'll skip writing.
#
return if(!$modified);
#
# Make sure we've got the correct count of keyrec lines.
#
$krlen = @krlines;
#
# Loop through the array of keyrec lines and concatenate them all.
# We'll also squish multiple consecutive newlines into one.
#
for(my $ind = 0; $ind < $krlen; $ind++)
{
$krc .= $krlines[$ind];
}
$krc =~ s/\n\n+/\n\n/mg;
#
# Close the keyrec file.
#
close(KEYREC);
#
# Open, lock, and truncate the keyrec file.
#
# (On some systems, these could be rolled into the sysopen().
# That may not be portable, so we'll use the separate calls.)
#
sysopen(SOKEYREC, $curkrfname, O_WRONLY | O_CREAT);
flock(SOKEYREC, LOCK_EX);
sysseek(KEYREC,0,0);
truncate(SOKEYREC, 0);
#
# Write the keyrec contents to the file.
#
syswrite SOKEYREC, $krc;
#
# Unlock and close the keyrec file.
#
flock(SOKEYREC,LOCK_UN);
close(SOKEYREC);
}
#--------------------------------------------------------------------------
# Routine: keyrec_saveas()
#
# Purpose: Save the key record file into a user-specified file. A new
# file handle is used and it is closed after writing.
#
sub keyrec_saveas
{
my $newname = shift; # Name of new file.
my $krc = ""; # Concatenated keyrec file contents.
#
# Make sure we've got the correct count of keyrec lines.
#
$keyreclen = @keyreclines;
#
# Loop through the array of keyrec lines and concatenate them all.
#
for(my $ind=0;$ind<$keyreclen;$ind++)
{
$krc .= $keyreclines[$ind];
}
#
# Open the new file.
#
open(NEWKEYREC,">$newname") || return(0);
print NEWKEYREC $krc;
close(NEWKEYREC);
#
# Return success.
#
return(1);
}
#--------------------------------------------------------------------------
# Routine: keyrec_fmtchk()
#
# Purpose: Ensure the keyrec file is in the current format. This
# function only ensure the current format, it does not do
# a sanity check on the keyrec file.
#
# Generally speaking, this function should not be called
# by anyone except dtupdkrf. It absolutely should NOT be
# called on a keyrec file that is in active use. If you
# ignore this warning, you deserve what you get.
#
# zone keyrecs:
# - kskkey:
# Delete the entry if a kskcur entry
# exists. If a kskcur entry doesn't
# exist, the kskkey key will be moved
# into a new kskcur entry.
# - keys:
# Ensure that each of the named keys in
# the zone is really pointing to a signing
# set. If not, a new set is created and
# the key inserted in it.
#
# set keyrecs:
# Nothing.
#
# key keyrecs:
# - ksk:
# Change to a kskcur, kskpub, kskrev,
# or kskobs.
#
# Return Values:
# The number of changes made to the keyrec file
# is returned.
#
#
sub keyrec_fmtchk
{
my $krf = shift; # Name of keyrec file.
my $krn; # Name of keyrec.
my $krec; # Ref to the keyrec's hash.
my %krec; # The keyrec's hash.
my $changes = 0; # Count of changes made.
# print "keyrec_fmtchk: down in\n";
#
# Check zone keyrecs for problems:
# - existence of kskkey fields
# - key fields that don't point to set keyrecs
#
foreach $krn (keyrec_names())
{
#
# Get the keyrec hash.
#
$krec = $keyrecs{$krn};
%krec = %$krec;
#
# Only look at zones right now.
#
next if($krec{'keyrec_type'} ne 'zone');
#
# Check for the old 'kskkey' keyrec field. If we find one,
# we'll make it the new kskcur field if there's no 'kskcur'.
# In either case, we'll delete the 'kskkey' field.
#
if(exists($krec{'kskkey'}))
{
my $set; # Signing set name.
my $newtype; # Type for new signing set.
#
# Figure out the type for the new signing set.
#
if(exists($krec{'kskcur'}))
{
$newtype = 'kskobs';
}
else
{
$newtype = 'kskcur';
}
#
# Create a signing set for the old key.
#
$set = keyrec_signset_newname($krn);
keyrec_signset_new($krn,$set,$newtype);
keyrec_setval('zone',$krn,$newtype,$set);
keyrec_signset_addkey($set,$krec{'kskkey'});
#
# Delete the old kskkey keyrec.
#
keyrec_delval($krn,'kskkey',20);
$changes++;
}
#
# Ensure that each of the zone's key set types is actually
# pointing to a signing set. If it is pointing to a key,
# create a new set, move the key there, and reference the set.
#
foreach my $kst (qw /kskcur kskpub kskrev kskobs zskcur zskpub zsknew/)
{
my $keyname; # Key's name.
my $set; # Name of new signing set.
my $skr; # Sub-keyrec.
#
# Skip this set type if it isn't in the keyrec.
#
next if(!exists($krec{$kst}));
#
# Get the type of this key's keyrec. Delete and
# skip empty entries.
#
$keyname = $krec{$kst};
if($keyname eq '')
{
keyrec_delval($krn,$kst,21);
$changes++;
next;
}
#
# Skip this set type if it's already a signing set.
#
$skr = $keyrecs{$keyname}{'keyrec_type'};
next if($skr eq 'set');
next if(exists($keyrecs{$keyname}{'set_type'}));
#
# Make a new signing set for this key.
#
# There is apparently a problem in this next small block of code. When
# called as a standard part of keyrec_read() (old behavior), bad things
# would happen when rollerd, zonesigner, and company were running. The
# keyrec file would eventually get corrupted.
#
# This "if(0)" seemed to stop the file corruption. Now that keyrec_fmtchk()
# is not called by every invocation of keyrec_read(), and is only called by
# the dtupdkrf command, the "if(0)" may not be necessary.
#
# Further testing is needed...
#
# if(0)
# {
$set = keyrec_signset_newname($krn);
keyrec_signset_new($krn,$set,$kst);
keyrec_setval('zone',$krn,$kst,$set);
keyrec_signset_addkey($set,$keyname);
$changes++;
# }
}
}
#
# This is scaffolding in case we need to add set checks in the future.
#
if(0)
{
foreach $krn (keyrec_names())
{
#
# Get the keyrec hash.
#
$krec = $keyrecs{$krn};
%krec = %$krec;
#
# Only look at sets right now...
#
next if($krec{'keyrec_type'} ne 'set');
#
# ... but we have nothing in sets to update.
#
}
}
#
# Check key keyrecs for problems:
# - Change ksk type keyrecs to kskcur, kskpub, kskrev, or kskobs.
#
foreach $krn (keyrec_names())
{
my $krt; # Keyrec's type.
#
# Get the keyrec hash.
#
$krec = $keyrecs{$krn};
%krec = %$krec;
#
# Get the keyrec's type.
#
$krt = $krec{'keyrec_type'};
#
# Only look at keys right now.
#
next if(($krec{$krt} eq 'zone') || ($krec{$krt} eq 'set'));
next if(exists($krec{'set_type'}));
#
# Convert a KSK keyrec into either a kskcur, kskpub, kskrev,
# or kskobs keyrec.
#
if($krt eq 'ksk')
{
my $zone; # Key's owner zone.
my $key; # Key type.
my $set; # Key's signing set.
my $found = ""; # Found-key flag.
#
# Check the key's zone to find if it's used in any of
# the zone's key sets.
#
$zone = $krec{'zonename'};
foreach my $key (qw /kskcur kskpub kskrev kskobs/)
{
$set = $keyrecs{$zone}{$key};
if(keyrec_signset_haskey($set,$krn))
{
$found = $key;
last;
}
}
#
# If this key is used in the zone, we'll set its type
# appropriately. If not, we'll set it to being an
# obsolete key.
#
if($found ne "")
{
keyrec_setval('key',$krn,'keyrec_type',$found);
}
else
{
keyrec_setval('key',$krn,'keyrec_type','kskobs');
}
$changes++;
}
}
#
# If any problems were found and fixed, write the file.
#
keyrec_write() if($changes);
#
# Return the number of changes made.
#
return($changes);
}
#--------------------------------------------------------------------------
# Routine: keyrec_dump_hash()
#
# Purpose: Dump the parsed keyrec entries.
#
sub keyrec_dump_hash
{
#
# Loop through the hash of keyrecs and print the keyrec names,
# subfields, and values.
#
foreach my $k (sort(keys(%keyrecs)))
{
print "keyrec - $k\n";
my $subp = $keyrecs{$k};
my %subrecs = %$subp;
foreach my $sk (sort(keys(%subrecs)))
{
print "\t$sk\t\t$subrecs{$sk}\n";
}
print "\n";
}
}
#--------------------------------------------------------------------------
# Routine: keyrec_dump_array()
#
# Purpose: Display the contents of @keyreclines.
#
sub keyrec_dump_array
{
#
# Make sure we've got the correct count of keyrec lines.
#
$keyreclen = @keyreclines;
#
# Loop through the array of keyrec lines and print them all.
#
for(my $ind=0;$ind<$keyreclen;$ind++)
{
print $keyreclines[$ind];
}
}
1;
#############################################################################
=pod
=head1 NAME
Net::DNS::SEC::Tools::keyrec - DNSSEC-Tools I<keyrec> file operations
=head1 SYNOPSIS
use Net::DNS::SEC::Tools::keyrec;
keyrec_creat("localzone.keyrec");
keyrec_open("localzone.keyrec"); (DEPRECATED)
$okfile = keyrec_filestat("localzone.keyrec");
keyrec_read("localzone.keyrec");
@krnames = keyrec_names();
$krec = keyrec_fullrec("example.com");
%keyhash = %$krec;
$zname = $keyhash{"algorithm"};
$val = keyrec_recval("example.com","zonefile");
$exists = keyrec_exists("example.com");
keyrec_add("zone","example.com",\%zone_krfields);
keyrec_add("key","Kexample.com.+005+12345",\%keydata);
keyrec_del("example.com");
keyrec_setval("zone","example.com","zonefile","db.example.com");
keyrec_delval("example.com","kskrev");
@kskpaths = keyrec_keypaths("example.com","kskcur");
$obsflag = keyrec_revoke_check("Kexample.com.+005+12345");
$setname = keyrec_signset_newname("example.com");
keyrec_signset_new($zone,"example-set-21","zskcur");
keyrec_signset_addkey("example-keys","Kexample.com+005+12345",
"Kexample.com+005+54321");
keyrec_signset_addkey("example-keys",@keylist);
keyrec_signset_delkey("example-keys","Kexample.com+005+12345");
$flag = keyrec_signset_haskey("example-keys","Kexample.com+005+12345");
keyrec_signset_clear("example-keys","Kexample.com+005+12345");
@signset = keyrec_signsets();
$sset_prefix = keyrec_signset_prefix("example.com");
keyrec_settime("zone","example.com");
keyrec_settime("set","signing-set-42");
keyrec_settime("key","Kexample.com.+005+76543");
@keyfields = keyrec_keyfields();
@zonefields = keyrec_zonefields();
keyrec_write();
keyrec_saveas("filecopy.krf);
keyrec_close();
keyrec_discard();
$current_krf = keyrec_curkrf();
$default_krf = keyrec_defkrf();
=head1 DESCRIPTION
The B<Net::DNS::SEC::Tools::keyrec> module manipulates the contents of
a DNSSEC-Tools I<keyrec> file. I<keyrec> files contain data about
zones signed by and keys generated by the DNSSEC-Tools programs. Module
interfaces exist for looking up I<keyrec> records, creating new
records, and modifying existing records.
A I<keyrec> file is organized in sets of I<keyrec> records. Each I<keyrec>
must be either of I<key> type or I<zone> type. Key I<keyrec>s describe how
encryption keys were generated, zone I<keyrec>s describe how zones were
signed. A I<keyrec> consists of a set of keyword/value entries. The
following is an example of a key I<keyrec>:
key "Kexample.com.+005+30485"
zonename "example.com"
keyrec_type "kskcur"
algorithm "rsasha1"
random "/dev/urandom"
ksklength "2048"
ksklife "15768000"
revperiod "3888000"
revtime "1103277532"
keyrec_gensecs "1101183727"
keyrec_gendate "Tue Nov 23 04:22:07 2004"
The first step in using this module B<must> be to create a new I<keyrec> file
or open and read an existing one. The I<keyrec_creat()> interface creates a
I<keyrec> file if it does not exist. The I<keyrec_read()> interface opens
and reads the file, then parses it into an internal format. The file's
records are copied into a hash table (for easy and fast reference by the
B<Net::DNS::SEC::Tools::keyrec> routines) and in an array (for preserving
formatting and comments.) The I<keyrec_filestat()> interface may be used
check that the given file may be a I<keyrec> file, though it doesn't check
the file's contents.
After the file has been read, the contents are referenced using
I<keyrec_fullrec()> and I<keyrec_recval()>. The I<keyrec> contents are
modified using I<keyrec_add()>, and I<keyrec_setval()>. I<keyrec_settime()>
will update a I<keyrec>'s timestamp to the current time. I<keyrec>s may be
deleted with the I<keyrec_del()> interface.
If the I<keyrec> file has been modified, it must be explicitly written or the
changes are not saved. I<keyrec_write()> saves the new contents to disk.
I<keyrec_saveas()> saves the in-memory I<keyrec> contents to the specified
file name, without affecting the original file.
I<keyrec_close()> saves the file and close the Perl file handle to the
I<keyrec> file. If a I<keyrec> file is no longer wanted to be open, yet the
contents should not be saved, I<keyrec_discard()> gets rid of the data, and
closes the file handle B<without> saving any modified data.
=head1 KEYREC INTERFACES
The interfaces to the B<Net::DNS::SEC::Tools::keyrec> module are given below.
=over 4
=item I<keyrec_add(keyrec_type,keyrec_name,fields)>
This routine adds a new I<keyrec> to the I<keyrec> file and the internal
representation of the file contents. The I<keyrec> is added to both the
I<%keyrecs> hash table and the I<@keyreclines> array.
I<keyrec_type> specifies the type of the I<keyrec> -- "key" or "zone".
I<keyrec_name> is the name of the I<keyrec>. I<fields> is a reference to a
hash table that contains the name/value I<keyrec> fields. The keys of the
hash table are always converted to lowercase, but the entry values are left
as given.
The I<ksklength> entry is only added if the value of the I<keyrec_type>
field is "kskcur".
The I<zsklength> entry is only added if the value of the I<keyrec_type>
field is "zsk", "zskcur", "zskpub", or "zsknew".
Timestamp fields are added at the end of the I<keyrec>. For key I<keyrec>s,
the I<keyrec_gensecs> and I<keyrec_gendate> timestamp fields are added. For
zone I<keyrec>s, the I<keyrec_signsecs> and I<keyrec_signdate> timestamp
fields are added.
If a specified field isn't defined for the I<keyrec> type, the entry isn't
added. This prevents zone I<keyrec> data from getting mingled with key
I<keyrec> data.
A blank line is added after the final line of the new I<keyrec>. After adding
all new I<keyrec> entries, the I<keyrec> file is written but is not closed.
Return values are:
0 success
-1 invalid I<krtype>
=item I<keyrec_close()>
This interface saves the internal version of the I<keyrec> file (opened with
I<keyrec_read()>) and closes the file handle.
=item I<keyrec_creat(keyrec_file)>
This interface creates a I<keyrec> file if it does not exist, and truncates
the file if it already exists.
I<keyrec_creat()> returns 1 if the file was created successfully.
It returns 0 if there was an error in creating the file.
=item I<keyrec_curkrf()>
This routine returns the name of the I<keyrec> file that is currently in use.
This value is the filename passed to I<keyrec_read()> or I<keyrec_creat()>;
it is not guaranteed to be either an absolute or relative filename.
=item I<keyrec_defkrf()>
This routine returns the default I<keyrec> filename from the DNSSEC-Tools
configuration file.
=item I<keyrec_del(keyrec_name)>
This routine deletes a I<keyrec> from the I<keyrec> file and the internal
representation of the file contents. The I<keyrec> is deleted from both
the I<%keyrecs> hash table and the I<@keyreclines> array.
Only the I<keyrec> itself is deleted from the file. Any associated comments
and blank lines surrounding it are left intact.
Return values are:
0 successful keyrec deletion
-1 invalid krtype (empty string or unknown name)
=item I<keyrec_delval(keyrec_name, field)>
This routine deletes the I<field> from the I<keyrec> named by I<keyrec_name>.
The I<keyrec> is deleted from both the I<%keyrecs> hash table and the
I<@keyreclines> array.
Return values are:
-1 keyrec_name not the name of an existing keyrec
0 field not found in keyrec
1 field deleted from keyrec
=item I<keyrec_discard()>
This routine removes a I<keyrec> file from use by a program. The internally
stored data are deleted and the I<keyrec> file handle is closed. However,
modified data are not saved prior to closing the file handle. Thus, modified
and new data will be lost.
=item I<keyrec_exists(keyrec_name)>
I<keyrec_exists()> returns a boolean indicating if a I<keyrec> exists that
has the specified I<keyrec_name>.
=item I<keyrec_filestat(keyrec_name)>
I<keyrec_filestat()> checks that a given file might be a reasonable candidate
for a DNSSEC-Tools I<keyrec> file. The checks to be performed may be gleaned
from the list of return values.
Return values are:
0 - returned if the tests are all succeed
1 - an actual name wasn't given
2 - the file does not exist
3 - the file is not a regular file
4 - the file is not readable
5 - the file is empty
=item I<keyrec_fullrec(keyrec_name)>
I<keyrec_fullrec()> returns a reference to the I<keyrec> specified in
I<keyrec_name>.
=item I<keyrec_keyfields()>
This routine returns a list of the recognized fields for a key I<keyrec>.
=item I<keyrec_keypaths(zonename,keytype)>
I<keyrec_keypaths()> returns a list of paths to a set of key files for a
given zone. The zone is specified in I<zonename> and the type of key is
given in I<keytype>.
I<keytype> must be one of the following: "kskcur", "kskpub", "kskrev",
"kskobs"", "zskcur", "zskpub", "zsknew", "zskobs", "ksk", "zsk", or "all".
Case does not matter for the I<keytype>.
If I<keytype> is one of the special labels ("ksk", "zsk", or "all") then a
set of key paths will be returned.
A I<keytype> of "ksk" will return paths to all KSK keys for the zone,
a I<keytype> of "zsk" will return paths to all ZSK keys for the zone,
and a I<keytype> of "all" will return paths to all keys for the zone,
If the given key type is not defined in the given zone's zone I<keyrec>
or if the key type is not recognized, then a null set is returned.
=item I<keyrec_names()>
This routine returns a list of the I<keyrec> names from the file.
=item I<keyrec_open(keyrec_file)> B<DEPRECATED>
This routine used to open an existing DNSSEC-Tools I<keyrec> file. However,
this was an unnecessary operation since I<keyrec_read()> would open the file
if it wasn't already open.
This call will eventually be removed. For now, it calls I<keyrec_filestat()>
to check the validity of the specified I<keyrec> file.
Return values:
1 is the file passes all of keyrec_filestat()'s tests
0 is the file fails any of keyrec_filestat()'s tests
For backwards compatibility, the success/failure meaning of the return values
matches the success/failure meaning of I<keyrec_open()>'s original returns.
=item I<keyrec_read(keyrec_file)>
This interface reads the specified I<keyrec> file and parses it into a
I<keyrec> hash table and a file contents array. I<keyrec_read()> B<must> be
called prior to any of the other B<Net::DNS::SEC::Tools::keyrec> calls. If
another I<keyrec> is already open, then it is saved and closed prior to
opening the new I<keyrec>.
Upon success, I<keyrec_read()> returns the number of I<keyrec>s read from the
file.
Failure return values:
-1 specified I<keyrec> file doesn't exist
-2 unable to open I<keyrec> file
-3 duplicate I<keyrec> names in file
=item I<keyrec_recval(keyrec_name,keyrec_field)>
This routine returns the value of a specified field in a given I<keyrec>.
I<keyrec_name> is the name of the particular I<keyrec> to consult.
I<keyrec_field> is the field name within that I<keyrec>.
For example, the current I<keyrec> file contains the following I<keyrec>:
zone "example.com"
zonefile "db.example.com"
The call:
keyrec_recval("example.com","zonefile")
will return the value "db.example.com".
=item I<keyrec_revoke_check(key)>
This interface checks a revoked KSK's I<keyrec> to determine if it is in or
out of its revocation period. The key must be a "kskrev" type key, and it
must have "revtime" and "revperiod" fields defined in the I<keyrec>.
The determination is made by subtracting the revoke time from the current
time. If this is greater than the revocation period, the the key has
exceeded the time in which it must be revoked. If not, then it must remain
revoked.
Return values:
1 specified key is outside the revocation period and should be
marked as obsolete
0 specified key is in the revocation period and should be left
revoked
-1 error (invalid key type, missing I<keyrec> data)
=item I<keyrec_saveas(keyrec_file_copy)>
This interface saves the internal version of the I<keyrec> file (opened with
or I<keyrec_read()>) to the file named in the I<keyrec_file_copy> parameter.
The new file's file handle is closed, but the original file and the file
handle to the original file are not affected.
=item I<keyrec_setval(keyrec_type,keyrec_name,field,value)>
Set the value of a I<name/field> pair in a specified I<keyrec>. The file is
B<not> written after updating the value. The value is saved in both
I<%keyrecs> and in I<@keyreclines>, and the file-modified flag is set.
I<keyrec_type> specifies the type of the I<keyrec>. This is only used if a
new I<keyrec> is being created by this call.
I<keyrec_name> is the name of the I<keyrec> that will be modified.
I<field> is the I<keyrec> field which will be modified.
I<value> is the new value for the field.
Return values are:
0 if the creation succeeded
-1 invalid type was given
=item I<keyrec_settime(keyrec_type,keyrec_name)>
Set the timestamp of a specified I<keyrec>. The file is B<not> written
after updating the value. The value is saved in both I<%keyrecs> and in
I<@keyreclines>, and the file-modified flag is set. The I<keyrec>'s
I<keyrec_signdate> and I<keyrec_signsecs> fields are modified.
=item I<keyrec_write()>
This interface saves the internal version of the I<keyrec> file (opened with
or I<keyrec_read()>). It does not close the file handle. As an efficiency
measure, an internal modification flag is checked prior to writing the file.
If the program has not modified the contents of the I<keyrec> file, it is not
rewritten.
I<keyrec_write()> gets an exclusive lock on the I<keyrec> file while writing.
=item I<keyrec_zonefields()>
This routine returns a list of the recognized fields for a zone I<keyrec>.
=back
=head1 KEYREC SIGNING-SET INTERFACES
Signing sets are collections of encryption keys, defined by inclusion in a
particular "set" I<keyrec>. The names of the keys are in the I<keyrec>'s
I<keys> record, which contains the names of the key I<keyrec>s. Due to the
way key names are handled, the names in a signing set must not contain spaces.
The signing-set-specific interfaces are given below.
=over 4
=item I<keyrec_signset_newname(zone_name)>
I<keyrec_signset_newname()> creates a name for a new signing set. The name
will be generated by referencing the I<lastset> field in the I<keyrec> for
zone I<zone_name>, if the I<keyrec> has such a field. The set index number
(described below) will be incremented and the I<lastset> with the new index
number will be returned as the new signing set name. If the zone I<keyrec>
does not have a I<lastset> field, then the default set name of
I<signing-set-0> will be used.
The set index number is the first number found in the I<lastset> field. It
doesn't matter where in the field it is found, the first number will be
considered to be the signing set index. The examples below show how this is
determined:
lastset field index
------------- -----
signing-set-0 0
signing-0-set 0
1-signing-0-set 1
signing-88-set-1 88
signingset4321 4321
=item I<keyrec_signset_new(zone,signing_set_name,set_type)>
I<keyrec_signset_new()> creates the signing set named by I<signing_set_name>
for the zone I<zone>. It is given the type I<type>, which must be one of the
following: "kskcur", "kskpub", "kskrev", "kskobs", "zskcur", "zskpub",
"zsknew", or "zskobs".
It returns 1 if the call is successful; 0 if it is not.
=item I<keyrec_signset_prefix(zone_name)>
I<keyrec_signset_prefix()> returns the signing set prefix formed by
concatenating the zone name and $DEFSETPREFIX. This prefix should be
followed by a numeric index.
=item I<keyrec_signset_addkey(signing_set_name,key_list)>
I<keyrec_signset_addkey()> adds the keys listed in I<key_list> to the signing
set named by I<signing_set_name>. I<key_list> may either be an array or a set
or arguments to the routine. The I<keyrec> is created if it does not already
exist.
It returns 1 if the call is successful; 0 if it is not.
=item I<keyrec_signset_delkey(signing_set_name,key_name)>
I<keyrec_signset_delkey()> deletes the key given in I<key_name> to the
signing set named by I<signing_set_name>.
It returns 1 if the call is successful; 0 if it is not.
=item I<keyrec_signset_haskey(signing_set_name,key_name)>
I<keyrec_signset_haskey()> returns a flag indicating if the key specified
in I<key_name> is one of the keys in the signing set named by
I<signing_set_name>.
It returns 1 if the signing set has the key; 0 if it does not.
=item I<keyrec_signset_clear(keyrec_name)>
I<keyrec_signset_clear()> clears the entire signing set from the I<keyrec>
named by I<keyrec_name>.
It returns 1 if the call is successful; 0 if it is not.
=item I<keyrec_signsets()>
I<keyrec_signsets()> returns the names of the signing sets in the I<keyrec>
file. These names are returned in an array.
=item I<keyrec_signset_keys(keyrec_name,signset_type)>
I<keyrec_signset_keys()> returns the names of the keys that are members of
a given signing set in the I<keyrec> file. The keys are returned in a
space-separated string.
There are two ways of calling I<keyrec_signset_keys()>. The first method
specifies a zone I<keyrec> name and a signing set type. The signing set name
is found by referencing the set field in the zone I<keyrec>, then the I<keys>
field of that signing set is returned.
The second method specifies the signing set directly, and its I<keys> field
is returned.
Valid signing set types are:
kskcur kskpub kskrev kskobs
zskcur zskpub zsknew zskobs
The following errors are recognized, resulting in an undefined return:
keyrec_name is not a defined keyrec
signset_type is an invalid signing set type
the signing set keyrec is not a set keyrec
=back
=head1 KEYREC INTERNAL INTERFACES
The interfaces described in this section are intended for internal use by the
B<keyrec.pm> module. However, there are situations where external entities
may have need of them. Use with caution, as misuse may result in damaged or
lost I<keyrec> files.
=over 4
=item I<keyrec_init()>
This routine initializes the internal I<keyrec> data. Pending changes will
be lost. An open I<keyrec> file handle will remain open, though the data are
no longer held internally. A new I<keyrec> file must be read in order to use
the B<keyrec.pm> interfaces again.
=item I<keyrec_newkeyrec(kr_name,kr_type)>
This interface creates a new I<keyrec>. The I<keyrec_name> and I<keyrec_hash>
fields in the I<keyrec> are set to the values of the I<kr_name> and I<kr_type>
parameters. I<kr_type> must be either "key" or "zone".
Return values are:
0 if the creation succeeded
-1 if an invalid I<keyrec> type was given
=back
=head1 KEYREC DEBUGGING INTERFACES
The following interfaces display information about the currently parsed
I<keyrec> file. They are intended to be used for debugging and testing, but
may be useful at other times.
=over 4
=item I<keyrec_dump_hash()>
This routine prints the I<keyrec> file as it is stored internally in a hash
table. The I<keyrec>s are printed in alphabetical order, with the fields
alphabetized for each I<keyrec>. New I<keyrec>s and I<keyrec> fields are
alphabetized along with current I<keyrec>s and fields. Comments from the
I<keyrec> file are not included with the hash table.
=item I<keyrec_dump_array()>
This routine prints the I<keyrec> file as it is stored internally in
an array. The I<keyrec>s are printed in the order given in the file,
with the fields ordered in the same manner. New I<keyrec>s are
appended to the end of the array. I<keyrec> fields added to existing
I<keyrec>s are added at the beginning of the I<keyrec> entry.
Comments and vertical whitespace are preserved as given in the
I<keyrec> file.
=back
=head1 COPYRIGHT
Copyright 2005-2012 SPARTA, Inc. All rights reserved.
See the COPYING file included with the DNSSEC-Tools package for details.
=head1 AUTHOR
Wayne Morrison, tewok@tislabs.com
=head1 SEE ALSO
B<Net::DNS::SEC::Tools::conf(5)>,
B<Net::DNS::SEC::Tools::keyrec(5)>
=cut
|