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
|
/*
* difffile.c - DIFF file handling source code. Read and write diff files.
*
* Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*/
#include "config.h"
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <inttypes.h>
#include "difffile.h"
#include "xfrd-disk.h"
#include "util.h"
#include "packet.h"
#include "rdata.h"
#include "udb.h"
#include "nsec3.h"
#include "nsd.h"
#include "rrl.h"
#include "ixfr.h"
#include "zonec.h"
#include "xfrd-catalog-zones.h"
static int
write_64(FILE *out, uint64_t val)
{
return write_data(out, &val, sizeof(val));
}
static int
write_32(FILE *out, uint32_t val)
{
val = htonl(val);
return write_data(out, &val, sizeof(val));
}
static int
write_8(FILE *out, uint8_t val)
{
return write_data(out, &val, sizeof(val));
}
static int
write_str(FILE *out, const char* str)
{
uint32_t len = strlen(str);
if(!write_32(out, len))
return 0;
return write_data(out, str, len);
}
void
diff_write_packet(const char* zone, const char* pat, uint32_t old_serial,
uint32_t new_serial, uint32_t seq_nr, uint8_t* data, size_t len,
struct nsd* nsd, uint64_t filenumber)
{
FILE* df = xfrd_open_xfrfile(nsd, filenumber, seq_nr?"a":"w");
if(!df) {
log_msg(LOG_ERR, "could not open transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
return;
}
/* if first part, first write the header */
if(seq_nr == 0) {
struct timeval tv;
if (gettimeofday(&tv, NULL) != 0) {
log_msg(LOG_ERR, "could not get timestamp for %s: %s",
zone, strerror(errno));
}
if(!write_32(df, DIFF_PART_XFRF) ||
!write_8(df, 0) /* notcommitted(yet) */ ||
!write_32(df, 0) /* numberofparts when done */ ||
!write_64(df, (uint64_t) tv.tv_sec) ||
!write_32(df, (uint32_t) tv.tv_usec) ||
!write_32(df, old_serial) ||
!write_32(df, new_serial) ||
!write_64(df, (uint64_t) tv.tv_sec) ||
!write_32(df, (uint32_t) tv.tv_usec) ||
!write_str(df, zone) ||
!write_str(df, pat)) {
log_msg(LOG_ERR, "could not write transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
fclose(df);
return;
}
}
if(!write_32(df, DIFF_PART_XXFR) ||
!write_32(df, len) ||
!write_data(df, data, len) ||
!write_32(df, len))
{
log_msg(LOG_ERR, "could not write transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
}
fclose(df);
}
void
diff_write_commit(const char* zone, uint32_t old_serial, uint32_t new_serial,
uint32_t num_parts, uint8_t commit, const char* log_str,
struct nsd* nsd, uint64_t filenumber)
{
struct timeval tv;
FILE* df;
if (gettimeofday(&tv, NULL) != 0) {
log_msg(LOG_ERR, "could not set timestamp for %s: %s",
zone, strerror(errno));
}
/* overwrite the first part of the file with 'committed = 1',
* as well as the end_time and number of parts.
* also write old_serial and new_serial, so that a bad file mixup
* will result in unusable serial numbers. */
df = xfrd_open_xfrfile(nsd, filenumber, "r+");
if(!df) {
log_msg(LOG_ERR, "could not open transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
return;
}
if(!write_32(df, DIFF_PART_XFRF) ||
!write_8(df, commit) /* committed */ ||
!write_32(df, num_parts) ||
!write_64(df, (uint64_t) tv.tv_sec) ||
!write_32(df, (uint32_t) tv.tv_usec) ||
!write_32(df, old_serial) ||
!write_32(df, new_serial))
{
log_msg(LOG_ERR, "could not write transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
fclose(df);
return;
}
/* append the log_str to the end of the file */
if(fseek(df, 0, SEEK_END) == -1) {
log_msg(LOG_ERR, "could not fseek transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
fclose(df);
return;
}
if(!write_str(df, log_str)) {
log_msg(LOG_ERR, "could not write transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
fclose(df);
return;
}
fflush(df);
fclose(df);
}
void
diff_update_commit(
const char* zone, uint8_t commit, struct nsd* nsd, uint64_t filenumber)
{
FILE *df;
assert(zone != NULL);
assert(nsd != NULL);
assert(commit == DIFF_NOT_COMMITTED ||
commit == DIFF_COMMITTED ||
commit == DIFF_CORRUPT ||
commit == DIFF_INCONSISTENT ||
commit == DIFF_VERIFIED);
df = xfrd_open_xfrfile(nsd, filenumber, "r+");
if(!df) {
log_msg(LOG_ERR, "could not open transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
return;
}
if(!write_32(df, DIFF_PART_XFRF) || !write_8(df, commit)) {
log_msg(LOG_ERR, "could not write transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
fclose(df);
return;
}
fflush(df);
fclose(df);
}
int
diff_read_64(FILE *in, uint64_t* result)
{
if (fread(result, sizeof(*result), 1, in) == 1) {
return 1;
} else {
return 0;
}
}
int
diff_read_32(FILE *in, uint32_t* result)
{
if (fread(result, sizeof(*result), 1, in) == 1) {
*result = ntohl(*result);
return 1;
} else {
return 0;
}
}
int
diff_read_8(FILE *in, uint8_t* result)
{
if (fread(result, sizeof(*result), 1, in) == 1) {
return 1;
} else {
return 0;
}
}
int
diff_read_str(FILE* in, char* buf, size_t len)
{
uint32_t disklen;
if(!diff_read_32(in, &disklen))
return 0;
if(disklen >= len)
return 0;
if(fread(buf, disklen, 1, in) != 1)
return 0;
buf[disklen] = 0;
return 1;
}
static void
add_rr_to_recyclebin(namedb_type* db, rr_type* rr)
{
/* add rr and rdata to recycle bin. */
region_recycle(db->region, rr, sizeof(rr_type) + rr->rdlength);
}
/* this routine determines if below a domain there exist names with
* data (is_existing) or no names below the domain have data.
*/
static int
has_data_below(domain_type* top)
{
domain_type* d = top;
assert(d != NULL);
/* in the canonical ordering subdomains are after this name */
d = domain_next(d);
while(d != NULL && domain_is_subdomain(d, top)) {
if(d->is_existing)
return 1;
d = domain_next(d);
}
return 0;
}
/** check if domain with 0 rrsets has become empty (nonexist) */
static domain_type*
rrset_zero_nonexist_check(domain_type* domain, domain_type* ce)
{
/* is the node now an empty node (completely deleted) */
if(domain->rrsets == 0) {
/* if there is no data below it, it becomes non existing.
also empty nonterminals above it become nonexisting */
/* check for data below this node. */
if(!has_data_below(domain)) {
/* nonexist this domain and all parent empty nonterminals */
domain_type* p = domain;
while(p != NULL && p->rrsets == 0) {
if(p == ce || has_data_below(p))
return p;
p->is_existing = 0;
/* fixup wildcard child of parent */
if(p->parent &&
p->parent->wildcard_child_closest_match == p)
p->parent->wildcard_child_closest_match = domain_previous_existing_child(p);
p = p->parent;
}
}
}
return NULL;
}
/** remove rrset. Adjusts zone params. Does not remove domain */
static void
rrset_delete(namedb_type* db, domain_type* domain, rrset_type* rrset)
{
int i;
/* find previous */
rrset_type** pp = &domain->rrsets;
while(*pp && *pp != rrset) {
pp = &( (*pp)->next );
}
if(!*pp) {
/* rrset does not exist for domain */
return;
}
*pp = rrset->next;
DEBUG(DEBUG_XFRD,2, (LOG_INFO, "delete rrset of %s type %s",
domain_to_string(domain),
rrtype_to_string(rrset_rrtype(rrset))));
/* is this a SOA rrset ? */
if(rrset->zone->soa_rrset == rrset) {
rrset->zone->soa_rrset = 0;
}
if(rrset->zone->ns_rrset == rrset) {
rrset->zone->ns_rrset = 0;
}
if(domain == rrset->zone->apex && rrset_rrtype(rrset) == TYPE_RRSIG) {
for (i = 0; i < rrset->rr_count; ++i) {
if(rr_rrsig_type_covered(rrset->rrs[i])==TYPE_DNSKEY) {
rrset->zone->is_secure = 0;
break;
}
}
}
/* recycle the memory space of the rrset */
for (i = 0; i < rrset->rr_count; ++i)
add_rr_to_recyclebin(db, rrset->rrs[i]);
#ifndef PACKED_STRUCTS
region_recycle(db->region, rrset->rrs, sizeof(rr_type*) * rrset->rr_count);
rrset->rr_count = 0;
#endif
region_recycle(db->region, rrset, sizeof(rrset_type)
#ifdef PACKED_STRUCTS
+ rrset->rr_count*sizeof(rr_type*)
#endif
);
}
static int
rdatas_equal(const rr_type *rr1, const rr_type *rr2, uint16_t type,
int* rdnum, char** reason)
{
size_t offset = 0;
const nsd_type_descriptor_type *descriptor;
if(rr1->rdlength != rr2->rdlength) {
*rdnum = 0;
*reason = "rr length different";
return 0;
}
assert(rr1->rdlength == rr2->rdlength);
/**
* SOA RDATA comparisons in XFR are more lenient,
* only serial rdata is checked.
**/
if (type == TYPE_SOA) {
offset = 2 * sizeof(void*);
if(rr1->rdlength != offset + 20) {
*rdnum = 0;
*reason = "invalid SOA length";
return 0;
}
assert(rr1->rdlength == offset + 20);
if (memcmp(rr1->rdata + offset, rr2->rdata + offset, 4) == 0)
return 1;
*rdnum = 2;
*reason = "rdata data";
return 0;
}
if (memcmp(rr1->rdata, rr2->rdata, rr1->rdlength) == 0)
return 1;
descriptor = nsd_type_descriptor(type);
for (size_t i=0; i < descriptor->rdata.length && offset < rr1->rdlength; i++) {
uint16_t field_len1, field_len2;
struct domain *domain1, *domain2;
if(offset == rr1->rdlength &&
descriptor->rdata.fields[i].is_optional)
break;
if(!lookup_rdata_field_entry(descriptor, i, rr1, offset,
&field_len1, &domain1)) {
*rdnum = i;
*reason = "malformed field rr1";
return 0;
}
if(!lookup_rdata_field_entry(descriptor, i, rr2, offset,
&field_len2, &domain2)) {
*rdnum = i;
*reason = "malformed field rr2";
return 0;
}
if(domain1 && domain2) {
const struct dname *dname1, *dname2;
dname1 = domain_dname(domain1);
dname2 = domain_dname(domain2);
if (dname_compare(dname1, dname2) != 0) {
*rdnum = i;
*reason = "dname data";
return 0;
}
offset += field_len1;
continue;
} else if(descriptor->rdata.fields[i].length ==
RDATA_LITERAL_DNAME) {
uint8_t length1, length2;
const uint8_t *name1 = rr1->rdata + offset;
const uint8_t *name2 = rr2->rdata + offset;
if(field_len1 != field_len2) {
*rdnum = i;
*reason = "literal dname field len";
return 0;
}
length1 = buf_dname_length(name1, rr1->rdlength - offset);
length2 = buf_dname_length(name2, rr2->rdlength - offset);
if (!length1 || !length2 || length1 != length2) {
*rdnum = i;
*reason = "literal dname len";
return 0;
}
if (!dname_equal_nocase((uint8_t*)name1, (uint8_t*)name2, length1)) {
*rdnum = i;
*reason = "literal dname data";
return 0;
}
offset += field_len1;
continue;
}
if(field_len1 != field_len2) {
*rdnum = i;
*reason = "rdata len";
return 0;
}
if(memcmp(rr1->rdata+offset, rr2->rdata+offset, field_len1) != 0) {
*rdnum = i;
*reason = "rdata data";
return 0;
}
offset += field_len1;
}
return 1;
}
static void
debug_find_rr_num(rrset_type* rrset, uint16_t type, uint16_t klass,
rr_type *rr)
{
int i, rd;
char* reason = "";
for(i=0; i < rrset->rr_count; ++i) {
if (rrset->rrs[i]->type != type) {
log_msg(LOG_WARNING, "diff: RR <%s, %s> does not match "
"RR num %d type %s",
dname_to_string(domain_dname(rrset->rrs[i]->owner),0),
rrtype_to_string(type), i,
rrtype_to_string(rrset->rrs[i]->type));
continue; /* rdata equality needs same type. */
}
if (rrset->rrs[i]->klass != klass) {
log_msg(LOG_WARNING, "diff: RR <%s, %s> class %d "
"does not match RR num %d class %d",
dname_to_string(domain_dname(rrset->rrs[i]->owner),0),
rrtype_to_string(type),
klass, i,
rrset->rrs[i]->klass);
continue;
}
if (rrset->rrs[i]->rdlength != rr->rdlength) {
log_msg(LOG_WARNING, "diff: RR <%s, %s> rdlen %u "
"does not match RR num %d rdlen %d",
dname_to_string(domain_dname(rrset->rrs[i]->owner),0),
rrtype_to_string(type),
(unsigned) rr->rdlength, i,
(unsigned) rrset->rrs[i]->rdlength);
}
if (!rdatas_equal(rr, rrset->rrs[i], type,
&rd, &reason)) {
log_msg(LOG_WARNING, "diff: RR <%s, %s> rdata element "
"%d differs from RR num %d rdata (%s)",
dname_to_string(domain_dname(rrset->rrs[i]->owner),0),
rrtype_to_string(type),
rd, i, reason);
}
}
}
static int
find_rr_num(rrset_type* rrset, uint16_t type, uint16_t klass,
rr_type *rr, int add)
{
int i, rd;
char* reason;
for(i=0; i < rrset->rr_count; ++i) {
if(rrset->rrs[i]->type == type &&
rrset->rrs[i]->klass == klass &&
rrset->rrs[i]->rdlength == rr->rdlength &&
rdatas_equal(rrset->rrs[i], rr, type,
&rd, &reason))
{
return i;
}
}
/* this is odd. Log why rr cannot be found. */
if (!add) {
debug_find_rr_num(rrset, type, klass, rr);
}
return -1;
}
#ifdef NSEC3
/* see if nsec3 deletion triggers need action */
static void
nsec3_delete_rr_trigger(namedb_type* db, rr_type* rr, zone_type* zone)
{
/* the RR has not actually been deleted yet, so we can inspect it */
if(!zone->nsec3_param)
return;
/* see if the domain was an NSEC3-domain in the chain, but no longer */
if(rr->type == TYPE_NSEC3 && rr->owner->nsec3 &&
rr->owner->nsec3->nsec3_node.key &&
nsec3_rr_uses_params(rr, zone) &&
nsec3_in_chain_count(rr->owner, zone) <= 1) {
domain_type* prev = nsec3_chain_find_prev(zone, rr->owner);
/* remove from prehash because no longer an NSEC3 domain */
if(domain_is_prehash(db->domains, rr->owner))
prehash_del(db->domains, rr->owner);
/* fixup the last in the zone */
if(rr->owner == zone->nsec3_last)
zone->nsec3_last = prev;
/* unlink from the nsec3tree */
zone_del_domain_in_hash_tree(zone->nsec3tree,
&rr->owner->nsec3->nsec3_node);
/* add previous NSEC3 to the prehash list */
if(prev && prev != rr->owner)
prehash_add(db->domains, prev);
else nsec3_clear_precompile(db, zone);
/* this domain becomes ordinary data domain: done later */
}
/* see if the rr was NSEC3PARAM that we were using */
else if(rr->type == TYPE_NSEC3PARAM && rr == zone->nsec3_param) {
/* clear trees, wipe hashes, wipe precompile */
nsec3_clear_precompile(db, zone);
/* pick up new nsec3param (from udb, or avoid deleted rr) */
nsec3_find_zone_param(db, zone, rr, 0);
/* if no more NSEC3, done */
if(!zone->nsec3_param)
return;
nsec3_precompile_newparam(db, zone);
}
}
/* see if nsec3 prehash can be removed with new rrset content */
static void
nsec3_rrsets_changed_remove_prehash(domain_type* domain, zone_type* zone)
{
/* deletion of rrset already done, we can check if conditions apply */
/* see if the domain is no longer precompiled */
/* it has a hash_node, but no longer fulfills conditions */
if(nsec3_domain_part_of_zone(domain, zone) && domain->nsec3 &&
domain->nsec3->hash_wc &&
domain->nsec3->hash_wc->hash.node.key &&
!nsec3_condition_hash(domain, zone)) {
/* remove precompile */
domain->nsec3->nsec3_cover = NULL;
domain->nsec3->nsec3_wcard_child_cover = NULL;
domain->nsec3->nsec3_is_exact = 0;
/* remove it from the hash tree */
zone_del_domain_in_hash_tree(zone->hashtree,
&domain->nsec3->hash_wc->hash.node);
zone_del_domain_in_hash_tree(zone->wchashtree,
&domain->nsec3->hash_wc->wc.node);
}
if(domain != zone->apex && domain->nsec3 &&
domain->nsec3->ds_parent_hash &&
domain->nsec3->ds_parent_hash->node.key &&
(!domain->parent || nsec3_domain_part_of_zone(domain->parent, zone)) &&
!nsec3_condition_dshash(domain, zone)) {
/* remove precompile */
domain->nsec3->nsec3_ds_parent_cover = NULL;
domain->nsec3->nsec3_ds_parent_is_exact = 0;
/* remove it from the hash tree */
zone_del_domain_in_hash_tree(zone->dshashtree,
&domain->nsec3->ds_parent_hash->node);
}
}
/* see if domain needs to get precompiled info */
static void
nsec3_rrsets_changed_add_prehash(namedb_type* db, domain_type* domain,
zone_type* zone)
{
if(!zone->nsec3_param)
return;
if((!domain->nsec3 || !domain->nsec3->hash_wc
|| !domain->nsec3->hash_wc->hash.node.key)
&& nsec3_condition_hash(domain, zone)) {
region_type* tmpregion = region_create(xalloc, free);
nsec3_precompile_domain(db, domain, zone, tmpregion);
region_destroy(tmpregion);
}
if((!domain->nsec3 || !domain->nsec3->ds_parent_hash
|| !domain->nsec3->ds_parent_hash->node.key)
&& nsec3_condition_dshash(domain, zone)) {
nsec3_precompile_domain_ds(db, domain, zone);
}
}
/* see if nsec3 rrset-deletion triggers need action */
static void
nsec3_delete_rrset_trigger(namedb_type* db, domain_type* domain,
zone_type* zone, uint16_t type)
{
if(!zone->nsec3_param)
return;
nsec3_rrsets_changed_remove_prehash(domain, zone);
/* for type nsec3, or a delegation, the domain may have become a
* 'normal' domain with its remaining data now */
if(type == TYPE_NSEC3 || type == TYPE_NS || type == TYPE_DS)
nsec3_rrsets_changed_add_prehash(db, domain, zone);
/* for type DNAME or a delegation, obscured data may be revealed */
if(type == TYPE_NS || type == TYPE_DS || type == TYPE_DNAME) {
/* walk over subdomains and check them each */
domain_type *d;
for(d=domain_next(domain); d && domain_is_subdomain(d, domain);
d=domain_next(d)) {
nsec3_rrsets_changed_add_prehash(db, d, zone);
}
}
}
/* see if nsec3 addition triggers need action */
static void
nsec3_add_rr_trigger(namedb_type* db, rr_type* rr, zone_type* zone)
{
/* the RR has been added in full, also to UDB (and thus NSEC3PARAM
* in the udb has been adjusted) */
if(zone->nsec3_param && rr->type == TYPE_NSEC3 &&
(!rr->owner->nsec3 || !rr->owner->nsec3->nsec3_node.key)
&& nsec3_rr_uses_params(rr, zone)) {
if(!zone->nsec3_last) {
/* all nsec3s have previously been deleted, but
* we have nsec3 parameters, set it up again from
* being cleared. */
nsec3_precompile_newparam(db, zone);
}
/* added NSEC3 into the chain */
nsec3_precompile_nsec3rr(db, rr->owner, zone);
/* the domain has become an NSEC3-domain, if it was precompiled
* previously, remove that, neatly done in routine above */
nsec3_rrsets_changed_remove_prehash(rr->owner, zone);
/* set this NSEC3 to prehash */
prehash_add(db->domains, rr->owner);
} else if(!zone->nsec3_param && rr->type == TYPE_NSEC3PARAM) {
/* see if this means NSEC3 chain can be used */
nsec3_find_zone_param(db, zone, NULL, 0);
if(!zone->nsec3_param)
return;
nsec3_zone_trees_create(db->region, zone);
nsec3_precompile_newparam(db, zone);
}
}
/* see if nsec3 rrset-addition triggers need action */
static void
nsec3_add_rrset_trigger(namedb_type* db, domain_type* domain, zone_type* zone,
uint16_t type)
{
/* the rrset has been added so we can inspect it */
if(!zone->nsec3_param)
return;
/* because the rrset is added we can check conditions easily.
* check if domain needs to become precompiled now */
nsec3_rrsets_changed_add_prehash(db, domain, zone);
/* if a delegation, it changes from normal name to unhashed referral */
if(type == TYPE_NS || type == TYPE_DS) {
nsec3_rrsets_changed_remove_prehash(domain, zone);
}
/* if delegation or DNAME added, then some RRs may get obscured */
if(type == TYPE_NS || type == TYPE_DS || type == TYPE_DNAME) {
/* walk over subdomains and check them each */
domain_type *d;
for(d=domain_next(domain); d && domain_is_subdomain(d, domain);
d=domain_next(d)) {
nsec3_rrsets_changed_remove_prehash(d, zone);
}
}
}
#endif /* NSEC3 */
static void
rrset_lower_usage(namedb_type* db, rrset_type* rrset)
{
unsigned i;
for(i=0; i<rrset->rr_count; i++)
rr_lower_usage(db, rrset->rrs[i]);
}
int
delete_RR(namedb_type* db, const dname_type* dname,
uint16_t type, uint16_t klass, uint32_t ttl,
buffer_type* packet, size_t rdatalen, zone_type *zone,
region_type* temp_region, int* softfail, struct ixfr_store* ixfr_store)
{
domain_type *domain;
rrset_type *rrset;
#ifdef PACKED_STRUCTS
rrset_type* rrset_prev;
#endif
const nsd_type_descriptor_type *descriptor = nsd_type_descriptor(type);
domain = domain_table_find(db->domains, dname);
if(!domain) {
log_msg(LOG_WARNING, "diff: domain %s does not exist",
dname_to_string(dname,0));
buffer_skip(packet, rdatalen);
*softfail = 1;
return 1; /* not fatal error */
}
#ifndef PACKED_STRUCTS
rrset = domain_find_rrset(domain, zone, type);
#else
rrset = domain_find_rrset_and_prev(domain, zone, type, &rrset_prev);
#endif
if(!rrset) {
log_msg(LOG_WARNING, "diff: rrset %s does not exist",
dname_to_string(dname,0));
buffer_skip(packet, rdatalen);
*softfail = 1;
return 1; /* not fatal error */
} else {
/* find the RR in the rrset */
domain_table_type *temptable;
int32_t rrnum, code;
struct rr *rr;
temptable = domain_table_create(temp_region);
/* This will ensure that the dnames in rdata are
* normalized, conform RFC 4035, section 6.2
*/
code = descriptor->read_rdata(temptable, rdatalen, packet, &rr);
if(code < 0) {
log_msg(LOG_ERR, "diff: could not read rdata for "
"%s %s %s", dname_to_string(dname,0),
rrtype_to_string(type),
read_rdata_fail_str(code));
}
rr->owner = domain;
rr->type = type;
rr->klass = klass;
rr->ttl = ttl;
/* Now that the RR has been read with its RRtype specific read
* routine, store the read data and rdata, for an ixfr store.
* Store the delete RR information, for the softfail cases, the
* IXFR is stopped and an AXFR is attempted, so it would not
* need to be stored as the transfer is rejected. */
if(ixfr_store)
ixfr_store_delrr(ixfr_store, rr);
rrnum = find_rr_num(rrset, type, klass, rr, 0);
if(rrnum == -1 && type == TYPE_SOA && domain == zone->apex
&& rrset->rr_count != 0)
rrnum = 0; /* replace existing SOA if no match */
if(rrnum == -1) {
log_msg(LOG_WARNING, "diff: RR <%s, %s> does not exist",
dname_to_string(dname,0), rrtype_to_string(type));
*softfail = 1;
return 1; /* not fatal error */
}
#ifdef NSEC3
/* process triggers for RR deletions */
nsec3_delete_rr_trigger(db, rrset->rrs[rrnum], zone);
#endif
/* lower usage (possibly deleting other domains, and thus
* invalidating the current RR's domain pointers) */
rr_lower_usage(db, rrset->rrs[rrnum]);
if(rrset->rr_count == 1) {
/* delete entire rrset */
rrset_delete(db, domain, rrset);
/* check if domain is now nonexisting (or parents) */
rrset_zero_nonexist_check(domain, NULL);
#ifdef NSEC3
/* cleanup nsec3 */
nsec3_delete_rrset_trigger(db, domain, zone, type);
#endif
/* see if the domain can be deleted (and inspect parents) */
domain_table_deldomain(db, domain);
} else {
/* swap out the bad RR and decrease the count */
#ifndef PACKED_STRUCTS
rr_type** rrs_orig = rrset->rrs;
#else
rrset_type* rrset_orig = rrset;
#endif
add_rr_to_recyclebin(db, rrset->rrs[rrnum]);
if(rrnum < rrset->rr_count-1)
rrset->rrs[rrnum] = rrset->rrs[rrset->rr_count-1];
rrset->rrs[rrset->rr_count-1] = NULL;
/* realloc the rrs array one smaller */
#ifndef PACKED_STRUCTS
rrset->rrs = region_alloc_array_init(db->region, rrs_orig,
(rrset->rr_count-1), sizeof(rr_type*));
if(!rrset->rrs) {
log_msg(LOG_ERR, "out of memory, %s:%d", __FILE__, __LINE__);
exit(1);
}
region_recycle(db->region, rrs_orig,
sizeof(rr_type*) * rrset->rr_count);
#else
rrset = region_alloc_init(db->region, rrset_orig,
sizeof(rrset_type) +
(rrset_orig->rr_count-1)*sizeof(rr_type*));
if(!rrset) {
log_msg(LOG_ERR, "out of memory, %s:%d", __FILE__, __LINE__);
exit(1);
}
if(rrset_prev)
rrset_prev->next = rrset;
else domain->rrsets = rrset;
region_recycle(db->region, rrset_orig,
sizeof(rrset_type) +
rrset_orig->rr_count*sizeof(rr_type*));
if(domain == zone->apex) {
/* Because the rrset struct is reallocated,
* a pointer to it may need to be set again. */
if(type == TYPE_SOA) {
zone->soa_rrset = rrset;
} else if(type == TYPE_NS) {
zone->ns_rrset = rrset;
}
}
#endif /* PACKED_STRUCTS */
rrset->rr_count --;
#ifdef NSEC3
/* for type nsec3, the domain may have become a
* 'normal' domain with its remaining data now */
if(type == TYPE_NSEC3)
nsec3_rrsets_changed_add_prehash(db, domain,
zone);
#endif /* NSEC3 */
}
}
return 1;
}
static void
commit_RRset(namedb_type* db, zone_type* zone, struct collect_rrs* collect_rrs)
{
struct rrset *rrset;
if(!collect_rrs->domain || collect_rrs->rr_count == 0)
return;
if (!collect_rrs->rrset) {
#ifdef NSEC3
domain_type* p;
#endif
rrset = region_alloc(db->region, sizeof(*rrset)
#ifdef PACKED_STRUCTS
+ sizeof(rr_type*) * collect_rrs->rr_count /* Add space for RRs. */
#endif
);
if(!rrset) {
log_msg(LOG_ERR, "out of memory, %s:%d", __FILE__, __LINE__);
exit(1);
}
rrset->zone = zone;
rrset->rr_count = collect_rrs->rr_count;
#ifndef PACKED_STRUCTS
rrset->rrs = region_alloc(db->region,
sizeof(rr_type*) * collect_rrs->rr_count);
#endif
memcpy(rrset->rrs, collect_rrs->rrs, collect_rrs->rr_count * sizeof(rr_type*));
/* Add it */
domain_add_rrset(collect_rrs->domain, rrset);
#ifdef NSEC3
p = collect_rrs->domain->parent;
nsec3_add_rrset_trigger(db, collect_rrs->domain, zone, collect_rrs->type);
/* go up and process (possibly created) empty nonterminals,
* until we hit the apex or root */
while(p && p->rrsets == NULL && !p->is_apex) {
nsec3_rrsets_changed_add_prehash(db, p, zone);
p = p->parent;
}
#endif
} else {
#ifndef PACKED_STRUCTS
struct rr **rrs;
#else
struct rrset *rrset_orig;
#endif
/* Add it... */
rrset = collect_rrs->rrset;
#ifndef PACKED_STRUCTS
rrs = rrset->rrs;
rrset->rrs = region_alloc_array(
db->region, rrset->rr_count + collect_rrs->rr_count, sizeof(*rrs));
if(!rrset->rrs) {
log_msg(LOG_ERR, "out of memory, %s:%d", __FILE__, __LINE__);
exit(1);
}
if(rrs)
memcpy(rrset->rrs, rrs, rrset->rr_count * sizeof(*rrs));
region_recycle(db->region, rrs, rrset->rr_count * sizeof(*rrs));
#else
rrset_orig = rrset;
rrset = region_alloc(db->region,
sizeof(rrset_type) +
(rrset_orig->rr_count+collect_rrs->rr_count)*sizeof(rr_type*));
if(!rrset) {
log_msg(LOG_ERR, "out of memory, %s:%d", __FILE__, __LINE__);
exit(1);
}
memcpy(rrset, rrset_orig,
sizeof(rrset_type) +
rrset_orig->rr_count*sizeof(rr_type*));
if(collect_rrs->rrset_prev)
collect_rrs->rrset_prev->next = rrset;
else collect_rrs->domain->rrsets = rrset;
region_recycle(db->region, rrset_orig,
sizeof(rrset_type) +
rrset_orig->rr_count*sizeof(rr_type*));
#endif /* PACKED_STRUCTS */
memcpy(rrset->rrs + rrset->rr_count, collect_rrs->rrs, collect_rrs->rr_count * sizeof(rr_type*));
rrset->rr_count += collect_rrs->rr_count;
}
/* Check we have SOA */
if (collect_rrs->domain == zone->apex)
apex_rrset_checks(db, rrset, collect_rrs->domain);
collect_rrs->domain = NULL;
collect_rrs->type = -1;
collect_rrs->rrset = NULL;
#ifdef PACKED_STRUCTS
collect_rrs->rrset_prev = NULL;
#endif
#ifdef NSEC3
for (int i = 0; i < collect_rrs->rr_count; i++) {
nsec3_add_rr_trigger(db, collect_rrs->rrs[i], zone);
}
#endif /* NSEC3 */
collect_rrs->rr_count = 0;
}
int
add_RR(namedb_type* db, const dname_type* dname,
uint16_t type, uint16_t klass, uint32_t ttl,
buffer_type* packet, size_t rdatalen, zone_type *zone,
int* softfail, struct ixfr_store* ixfr_store,
struct collect_rrs* collect_rrs)
{
domain_type* domain;
rr_type *rr;
int32_t code;
const nsd_type_descriptor_type *descriptor;
if (collect_rrs == NULL) {
struct collect_rrs collect_rrs2;
collect_rrs2.domain = NULL;
collect_rrs2.type = -1;
collect_rrs2.rrset = NULL;
#ifdef PACKED_STRUCTS
collect_rrs2.rrset_prev = NULL;
#endif
collect_rrs2.rr_count = 0;
if (!add_RR(db, dname, type, klass, ttl, packet, rdatalen,
zone, softfail, ixfr_store, &collect_rrs2)) {
return 0;
}
commit_RRset(db, zone, &collect_rrs2);
return 1;
}
domain = domain_table_insert(db->domains, dname);
assert(domain);
if (domain != collect_rrs->domain || type != collect_rrs->type
|| collect_rrs->rr_count >= (int)(sizeof(collect_rrs->rrs) / sizeof(*collect_rrs->rrs))){
commit_RRset(db, zone, collect_rrs);
collect_rrs->domain = domain;
collect_rrs->type = type;
collect_rrs->rrset = NULL;
#ifdef PACKED_STRUCTS
collect_rrs->rrset_prev = NULL;
#endif
}
/* dnames in rdata are normalized, conform RFC 4035,
* Section 6.2
*/
descriptor = nsd_type_descriptor(type);
code = descriptor->read_rdata(db->domains, rdatalen, packet, &rr);
if(code < 0) {
log_msg(LOG_ERR, "diff: could not read rdata for %s %s %s",
dname_to_string(dname,0), rrtype_to_string(type),
read_rdata_fail_str(code));
return 0;
}
rr->owner = domain;
rr->type = type;
rr->klass = klass;
rr->ttl = ttl;
/* Now that the RR has been read with its RRtype specific read
* routine, store the read data and rdata, for an ixfr store. */
if (ixfr_store) {
ixfr_store_addrr(ixfr_store, rr);
}
/* With the first RR for a RRset in this position in the zone transfer,
* find the RRset */
if (collect_rrs->rr_count == 0) {
#ifndef PACKED_STRUCTS
collect_rrs->rrset = domain_find_rrset(collect_rrs->domain, zone, collect_rrs->type);
#else
collect_rrs->rrset = domain_find_rrset_and_prev(collect_rrs->domain, zone, collect_rrs->type, &collect_rrs->rrset_prev);
#endif
}
if (collect_rrs->rrset) {
/* Search for possible duplicates in existing RRset */
for (int i = 0; i < collect_rrs->rrset->rr_count; i++) {
if (!equal_rr_rdata(descriptor, rr, collect_rrs->rrset->rrs[i]))
continue;
/* Discard the duplicates... */
/* Lower the usage counter for domains in the rdata. */
rr_lower_usage(db, rr);
region_recycle(db->region, rr, sizeof(*rr) + rr->rdlength);
DEBUG(DEBUG_XFRD, 2, (LOG_ERR, "diff: RR <%s, %s> already exists",
dname_to_string(dname,0), rrtype_to_string(type)));
/* ignore already existing RR: lenient accepting of messages */
*softfail = 1;
return 1;
}
}
/* Search for possible duplicates in already batched RRs */
for (int i = 0; i < collect_rrs->rr_count; i++) {
if (!equal_rr_rdata(descriptor, rr, collect_rrs->rrs[i]))
continue;
/* Discard the duplicates... */
/* Lower the usage counter for domains in the rdata. */
rr_lower_usage(db, rr);
region_recycle(db->region, rr, sizeof(*rr) + rr->rdlength);
DEBUG(DEBUG_XFRD, 2, (LOG_ERR, "diff: RR <%s, %s> already exists",
dname_to_string(dname,0), rrtype_to_string(type)));
/* ignore already existing RR: lenient accepting of messages */
*softfail = 1;
return 1;
}
if(collect_rrs->rr_count + ( collect_rrs->rrset
? collect_rrs->rrset->rr_count : 0) >= 65535) {
log_msg(LOG_ERR, "diff: too many RRs at %s",
dname_to_string(dname,0));
return 0;
}
collect_rrs->rrs[collect_rrs->rr_count++] = rr;
return 1;
}
static zone_type*
find_or_create_zone(namedb_type* db, const dname_type* zone_name,
struct nsd_options* opt, const char* zstr, const char* patname)
{
zone_type* zone;
struct zone_options* zopt;
zone = namedb_find_zone(db, zone_name);
if(zone) {
return zone;
}
zopt = zone_options_find(opt, zone_name);
if(!zopt) {
/* if _implicit_ then insert as _part_of_config */
if(strncmp(patname, PATTERN_IMPLICIT_MARKER,
strlen(PATTERN_IMPLICIT_MARKER)) == 0) {
zopt = zone_options_create(opt->region);
if(!zopt) return 0;
zopt->part_of_config = 1;
zopt->name = region_strdup(opt->region, zstr);
zopt->pattern = pattern_options_find(opt, patname);
if(!zopt->name || !zopt->pattern) return 0;
if(!nsd_options_insert_zone(opt, zopt)) {
log_msg(LOG_ERR, "bad domain name or duplicate zone '%s' "
"pattern %s", zstr, patname);
}
} else {
/* create zone : presumably already added to zonelist
* by xfrd, who wrote the AXFR or IXFR to disk, so we only
* need to add it to our config.
* This process does not need linesize and offset zonelist */
zopt = zone_list_zone_insert(opt, zstr, patname);
if(!zopt)
return 0;
}
}
zone = namedb_zone_create(db, zone_name, zopt);
return zone;
}
void
delete_zone_rrs(namedb_type* db, zone_type* zone)
{
rrset_type *rrset;
domain_type *domain = zone->apex, *next;
int nonexist_check = 0;
/* go through entire tree below the zone apex (incl subzones) */
while(domain && domain_is_subdomain(domain, zone->apex))
{
DEBUG(DEBUG_XFRD,2, (LOG_INFO, "delete zone visit %s",
domain_to_string(domain)));
/* delete all rrsets of the zone */
while((rrset = domain_find_any_rrset(domain, zone))) {
/* lower usage can delete other domains */
rrset_lower_usage(db, rrset);
/* rrset del does not delete our domain(yet) */
rrset_delete(db, domain, rrset);
/* no rrset_zero_nonexist_check, do that later */
if(domain->rrsets == 0)
nonexist_check = 1;
}
/* the delete upcoming could delete parents, but nothing next
* or after the domain so store next ptr */
next = domain_next(domain);
/* see if the domain can be deleted (and inspect parents) */
domain_table_deldomain(db, domain);
domain = next;
}
/* check if data deletions have created nonexisting domain entries,
* but after deleting domains so the checks are faster */
if(nonexist_check) {
domain_type* ce = NULL; /* for speeding up has_data_below */
DEBUG(DEBUG_XFRD, 1, (LOG_INFO, "axfrdel: zero rrset check"));
domain = zone->apex;
while(domain && domain_is_subdomain(domain, zone->apex))
{
/* the interesting domains should be existing==1
* and rrsets==0, speeding up out processing of
* sub-zones, since we only spuriously check empty
* nonterminals */
if(domain->is_existing)
ce = rrset_zero_nonexist_check(domain, ce);
domain = domain_next(domain);
}
}
DEBUG(DEBUG_XFRD, 1, (LOG_INFO, "axfrdel: recyclebin holds %lu bytes",
(unsigned long) region_get_recycle_size(db->region)));
#ifndef NDEBUG
if(nsd_debug_level >= 2)
region_log_stats(db->region);
#endif
assert(zone->soa_rrset == 0);
/* keep zone->soa_nx_rrset alloced: it is reused */
assert(zone->ns_rrset == 0);
assert(zone->is_secure == 0);
}
/* return value 0: syntaxerror,badIXFR, 1:OK, 2:done_and_skip_it */
static int
apply_ixfr(nsd_type* nsd, FILE *in, uint32_t serialno,
uint32_t seq_nr, uint32_t seq_total,
int* is_axfr, int* delete_mode, int* rr_count,
struct zone* zone, uint64_t* bytes,
int* softfail, struct ixfr_store* ixfr_store)
{
uint32_t msglen, checklen, pkttype;
int qcount, ancount;
buffer_type* packet;
region_type* region;
struct collect_rrs collect_rrs;
/* note that errors could not really happen due to format of the
* packet since xfrd has checked all dnames and RRs before commit,
* this is why the errors are fatal (exit process), it must be
* something internal or a bad disk or something. */
/* read ixfr packet RRs and apply to in memory db */
if(!diff_read_32(in, &pkttype) || pkttype != DIFF_PART_XXFR) {
log_msg(LOG_ERR, "could not read type or wrong type");
return 0;
}
if(!diff_read_32(in, &msglen)) {
log_msg(LOG_ERR, "could not read len");
return 0;
}
if(msglen < QHEADERSZ) {
log_msg(LOG_ERR, "msg too short");
return 0;
}
region = region_create(xalloc, free);
if(!region) {
log_msg(LOG_ERR, "out of memory");
return 0;
}
packet = buffer_create(region, QIOBUFSZ);
if(msglen > QIOBUFSZ) {
log_msg(LOG_ERR, "msg too long");
region_destroy(region);
return 0;
}
buffer_clear(packet);
if(fread(buffer_begin(packet), msglen, 1, in) != 1) {
log_msg(LOG_ERR, "short fread: %s", strerror(errno));
region_destroy(region);
return 0;
}
buffer_set_limit(packet, msglen);
/* see if check on data fails: checks that we are not reading
* random garbage */
if(!diff_read_32(in, &checklen) || checklen != msglen) {
log_msg(LOG_ERR, "transfer part has incorrect checkvalue");
return 0;
}
*bytes += msglen;
/* only answer section is really used, question, additional and
authority section RRs are skipped */
qcount = QDCOUNT(packet);
ancount = ANCOUNT(packet);
buffer_skip(packet, QHEADERSZ);
/* qcount should be 0 or 1 really, ancount limited by 64k packet */
if(qcount > 64 || ancount > 65530) {
log_msg(LOG_ERR, "RR count impossibly high");
region_destroy(region);
return 0;
}
/* skip queries */
for(int i=0; i < qcount; ++i) {
if(!packet_skip_rr(packet, 1)) {
log_msg(LOG_ERR, "bad RR in question section");
region_destroy(region);
return 0;
}
}
collect_rrs.domain = NULL;
collect_rrs.type = -1;
collect_rrs.rrset = NULL;
#ifdef PACKED_STRUCTS
collect_rrs.rrset_prev = NULL;
#endif
collect_rrs.rr_count = 0;
DEBUG(DEBUG_XFRD, 2, (LOG_INFO, "diff: started packet %d/%d for zone %s",
(int)seq_nr, (int)seq_total, domain_to_string(zone->apex)));
for(int i=0; i < ancount; ++i, ++(*rr_count)) {
const dname_type *owner;
uint16_t type, klass, rrlen;
uint32_t ttl;
owner = dname_make_from_packet(region, packet, 1, 1);
if(!owner) {
log_msg(LOG_ERR, "bad xfr RR dname %d", *rr_count);
region_destroy(region);
return 0;
}
if(!buffer_available(packet, 10)) {
log_msg(LOG_ERR, "bad xfr RR format %d", *rr_count);
region_destroy(region);
return 0;
}
type = buffer_read_u16(packet);
klass = buffer_read_u16(packet);
ttl = buffer_read_u32(packet);
rrlen = buffer_read_u16(packet);
if(!buffer_available(packet, rrlen)) {
log_msg(LOG_ERR, "bad xfr RR rdata %d, len %d have %d",
*rr_count, rrlen, (int)buffer_remaining(packet));
region_destroy(region);
return 0;
}
DEBUG(DEBUG_XFRD,2, (LOG_INFO, "diff: %s parsed count %d, ax %d, delmode %d",
domain_to_string(zone->apex), *rr_count, *is_axfr, *delete_mode));
if (type == TYPE_SOA) {
size_t position;
uint32_t serial;
position = buffer_position(packet);
if (!packet_skip_dname(packet) ||
!packet_skip_dname(packet) ||
buffer_remaining(packet) < sizeof(uint32_t) * 5)
{
log_msg(LOG_ERR, "bad xfr SOA RR formerr.");
region_destroy(region);
return 0;
}
serial = buffer_read_u32(packet);
buffer_set_position(packet, position);
/* first RR: check if SOA and correct zone & serialno */
if (*rr_count == 0) {
assert(!*is_axfr);
assert(!*delete_mode);
if (klass != CLASS_IN) {
log_msg(LOG_ERR, "first RR not SOA IN");
region_destroy(region);
return 0;
}
if(dname_compare(domain_dname(zone->apex), owner) != 0) {
log_msg(LOG_ERR, "SOA dname not equal to zone %s",
domain_to_string(zone->apex));
region_destroy(region);
return 0;
}
if(serial != serialno) {
log_msg(LOG_ERR, "SOA serial %u different from commit %u",
(unsigned)serial, (unsigned)serialno);
region_destroy(region);
return 0;
}
buffer_skip(packet, rrlen);
if(ixfr_store)
ixfr_store_add_newsoa(ixfr_store, ttl, packet, rrlen);
continue;
} else if (*rr_count == 1) {
assert(!*is_axfr);
assert(!*delete_mode);
/* if the serial no of the SOA equals the serialno, then AXFR */
if (serial == serialno)
goto axfr;
*delete_mode = 1;
/* must have stuff in memory for a successful IXFR,
* the serial number of the SOA has been checked
* previously (by check_for_bad_serial) if it exists */
if(!domain_find_rrset(zone->apex, zone, TYPE_SOA)) {
log_msg(LOG_ERR, "%s SOA serial %u is not "
"in memory, skip IXFR", domain_to_string(zone->apex), serialno);
region_destroy(region);
/* break out and stop the IXFR, ignore it */
return 2;
}
if(ixfr_store)
ixfr_store_add_oldsoa(ixfr_store, ttl, packet, rrlen);
} else if (!*is_axfr) {
/* do not delete final SOA RR for IXFR */
if (i == ancount - 1 && seq_nr == seq_total - 1) {
if (ixfr_store) {
ixfr_store_add_newsoa(ixfr_store, ttl, packet, rrlen);
}
*delete_mode = 0;
buffer_skip(packet, rrlen);
continue;
} else
*delete_mode = !*delete_mode;
if (ixfr_store && *delete_mode) {
ixfr_store_add_newsoa(ixfr_store, ttl, packet, rrlen);
ixfr_store_finish(ixfr_store, nsd, NULL);
ixfr_store_start(zone, ixfr_store);
ixfr_store_add_oldsoa(ixfr_store, ttl, packet, rrlen);
}
/* switch from delete-part to add-part and back again,
just before soa - so it gets deleted and added too */
DEBUG(DEBUG_XFRD,2, (LOG_INFO, "diff: %s IXFRswapdel count %d, ax %d, delmode %d",
domain_to_string(zone->apex), *rr_count, *is_axfr, *delete_mode));
}
} else {
if (*rr_count == 0) {
log_msg(LOG_ERR, "first RR not SOA IN");
region_destroy(region);
return 0;
/* second RR: if not SOA: this is an AXFR; delete all zone contents */
} else if (*rr_count == 1) {
axfr:
*is_axfr = 1;
#ifdef NSEC3
nsec3_clear_precompile(nsd->db, zone);
zone->nsec3_param = NULL;
#endif
delete_zone_rrs(nsd->db, zone);
if(ixfr_store) {
ixfr_store_cancel(ixfr_store);
ixfr_store_delixfrs(zone);
}
DEBUG(DEBUG_XFRD,2, (LOG_INFO, "diff: %s sawAXFR count %d, ax %d, delmode %d",
domain_to_string(zone->apex), *rr_count, *is_axfr, *delete_mode));
}
}
if(type == TYPE_TSIG || type == TYPE_OPT) {
/* ignore pseudo RRs */
buffer_skip(packet, rrlen);
continue;
}
DEBUG(DEBUG_XFRD,2, (LOG_INFO, "xfr %s RR dname is %s type %s",
*delete_mode?"del":"add",
dname_to_string(owner, 0), rrtype_to_string(type)));
if(*delete_mode) {
assert(!*is_axfr);
/* commit previous additions, if any. */
commit_RRset(nsd->db, zone, &collect_rrs);
/* delete this rr */
if(!delete_RR(nsd->db, owner, type, klass, ttl, packet,
rrlen, zone, region, softfail, ixfr_store)) {
region_destroy(region);
return 0;
}
} else {
/* add this rr */
if(!(add_RR(nsd->db, owner, type, klass, ttl, packet,
rrlen, zone, softfail, ixfr_store, &collect_rrs))) {
/* The collect_rrs->rrs that have not been committed yet, need to
have rr_lower_usage(db, collect_rrs.rrs[i]); for them. This is
needed to remove references to the db domain tree. However
the failure is fatal to the process, so it is not necessary.
But we do it anyway in case with a future change this will
not be fatal to the process anymore.
*/
/* With all socked up RRs,
* lower the usage counter for domains in the rdata.
*/
for (int i = 0; i < collect_rrs.rr_count; i++) {
/* Lower the usage counter for domains in the rdata. */
rr_lower_usage( nsd->db, collect_rrs.rrs[i]);
region_recycle( nsd->db->region, collect_rrs.rrs[i]
, sizeof(*collect_rrs.rrs[i])
+ collect_rrs.rrs[i]->rdlength);
}
region_destroy(region);
return 0;
}
}
}
commit_RRset(nsd->db, zone, &collect_rrs);
region_destroy(region);
return 1;
}
static int
check_for_bad_serial(namedb_type* db, const char* zone_str, uint32_t old_serial)
{
/* see if serial OK with in-memory serial */
domain_type* domain;
region_type* region = region_create(xalloc, free);
const dname_type* zone_name = dname_parse(region, zone_str);
zone_type* zone = 0;
domain = domain_table_find(db->domains, zone_name);
if(domain)
zone = domain_find_zone(db, domain);
if(zone && zone->apex == domain && zone->soa_rrset && old_serial)
{
uint32_t memserial = 0;
retrieve_soa_rdata_serial(zone->soa_rrset->rrs[0], &memserial);
if(old_serial != memserial) {
region_destroy(region);
return 1;
}
}
region_destroy(region);
return 0;
}
int
apply_ixfr_for_zone(nsd_type* nsd, zone_type* zone, FILE* in,
struct nsd_options* ATTR_UNUSED(opt), udb_base* taskudb, uint32_t xfrfilenr)
{
char zone_buf[3072];
char log_buf[5120];
char patname_buf[2048];
uint32_t old_serial, new_serial, num_parts, type;
uint64_t time_end_0, time_start_0;
uint32_t time_end_1, time_start_1;
uint8_t committed;
uint32_t i;
uint64_t num_bytes = 0;
assert(zone);
/* read zone name and serial */
if(!diff_read_32(in, &type)) {
log_msg(LOG_ERR, "diff file too short");
return 0;
}
if(type != DIFF_PART_XFRF) {
log_msg(LOG_ERR, "xfr file has wrong format");
return 0;
}
/* committed and num_parts are first because they need to be
* updated once the rest is written. The log buf is not certain
* until its done, so at end of file. The patname is in case a
* new zone is created, we know what the options-pattern is */
if(!diff_read_8(in, &committed) ||
!diff_read_32(in, &num_parts) ||
!diff_read_64(in, &time_end_0) ||
!diff_read_32(in, &time_end_1) ||
!diff_read_32(in, &old_serial) ||
!diff_read_32(in, &new_serial) ||
!diff_read_64(in, &time_start_0) ||
!diff_read_32(in, &time_start_1) ||
!diff_read_str(in, zone_buf, sizeof(zone_buf)) ||
!diff_read_str(in, patname_buf, sizeof(patname_buf))) {
log_msg(LOG_ERR, "diff file bad commit part");
return 0;
}
/* has been read in completely */
if(strcmp(zone_buf, domain_to_string(zone->apex)) != 0) {
log_msg(LOG_ERR, "file %s does not match task %s",
zone_buf, domain_to_string(zone->apex));
return 0;
}
switch(committed) {
case DIFF_NOT_COMMITTED:
log_msg(LOG_ERR, "diff file %s was not committed", zone_buf);
return 0;
case DIFF_CORRUPT:
log_msg(LOG_ERR, "diff file %s was corrupt", zone_buf);
return 0;
case DIFF_INCONSISTENT:
log_msg(LOG_ERR, "diff file %s was inconsistent", zone_buf);
return 0;
case DIFF_VERIFIED:
log_msg(LOG_INFO, "diff file %s already verified", zone_buf);
break;
default:
break;
}
if(num_parts == 0) {
log_msg(LOG_ERR, "diff file %s was not completed", zone_buf);
return 0;
}
if(check_for_bad_serial(nsd->db, zone_buf, old_serial)) {
DEBUG(DEBUG_XFRD,1, (LOG_ERR,
"skipping diff file commit with bad serial"));
return -2; /* Success in "main" process, failure in "xfrd" */
}
if(!zone->is_skipped)
{
int is_axfr=0, delete_mode=0, rr_count=0, softfail=0;
struct ixfr_store* ixfr_store = NULL, ixfr_store_mem;
DEBUG(DEBUG_XFRD,1, (LOG_INFO, "processing xfr: %s", zone_buf));
if(zone_is_ixfr_enabled(zone))
ixfr_store = ixfr_store_start(zone, &ixfr_store_mem);
/* read and apply all of the parts */
for(i=0; i<num_parts; i++) {
int ret;
DEBUG(DEBUG_XFRD,2, (LOG_INFO, "processing xfr: apply part %d", (int)i));
ret = apply_ixfr(nsd, in, new_serial,
i, num_parts, &is_axfr, &delete_mode,
&rr_count, zone,
&num_bytes, &softfail, ixfr_store);
if(ret == 0) {
log_msg(LOG_ERR, "bad ixfr packet part %d in diff file for %s", (int)i, zone_buf);
diff_update_commit(
zone_buf, DIFF_CORRUPT, nsd, xfrfilenr);
/* the udb is still dirty, it is bad */
return -1; /* Fatal! */
} else if(ret == 2) {
break;
}
}
/* read the final log_str: but do not fail on it */
if(!diff_read_str(in, log_buf, sizeof(log_buf))) {
log_msg(LOG_ERR, "could not read log for transfer %s",
zone_buf);
snprintf(log_buf, sizeof(log_buf), "error reading log");
}
#ifdef NSEC3
prehash_zone(nsd->db, zone);
#endif /* NSEC3 */
zone->is_changed = 1;
zone->is_updated = 1;
zone->is_checked = (committed == DIFF_VERIFIED);
zone->mtime.tv_sec = time_end_0;
zone->mtime.tv_nsec = time_end_1*1000;
if(zone->logstr)
region_recycle(nsd->db->region, zone->logstr,
strlen(zone->logstr)+1);
zone->logstr = region_strdup(nsd->db->region, log_buf);
namedb_zone_free_filenames(nsd->db, zone);
if(softfail && taskudb && !is_axfr) {
log_msg(LOG_ERR, "Failed to apply IXFR cleanly "
"(deletes nonexistent RRs, adds existing RRs). "
"Zone %s contents is different from primary, "
"starting AXFR. Transfer %s", zone_buf, log_buf);
/* add/del failures in IXFR, get an AXFR */
diff_update_commit(
zone_buf, DIFF_INCONSISTENT, nsd, xfrfilenr);
return -1; /* Fatal! */
}
if(ixfr_store)
ixfr_store_finish(ixfr_store, nsd, log_buf);
if(1 <= verbosity) {
double elapsed = (double)(time_end_0 - time_start_0)+
(double)((double)time_end_1
-(double)time_start_1) / 1000000.0;
VERBOSITY(1, (LOG_INFO, "zone %s %s of %"PRIu64" bytes in %g seconds",
zone_buf, log_buf, num_bytes, elapsed));
}
}
else {
DEBUG(DEBUG_XFRD,1, (LOG_INFO, "skipping xfr: %s", zone_buf));
}
return 1;
}
static void udb_task_walk_chunk(void* base, void* d, uint64_t s, udb_walk_relptr_cb* cb, void *arg)
{
struct task_list_d* p = (struct task_list_d*)d;
assert(s >= p->size);
(void)s;
(*cb)(base, &p->next, arg);
}
void udb_walkfunc(void* base, void* warg, uint8_t t, void* d, uint64_t s,
udb_walk_relptr_cb* cb, void *arg)
{
(void)warg;
switch(t) {
case udb_chunk_type_task:
udb_task_walk_chunk(base, d, s, cb, arg);
break;
default:
/* no rel ptrs */
break;
}
}
struct udb_base* task_file_create(const char* file)
{
return udb_base_create_new(file, &udb_walkfunc, NULL);
}
static int
task_create_new_elem(struct udb_base* udb, udb_ptr* last, udb_ptr* e,
size_t sz, const dname_type* zname)
{
if(!udb_ptr_alloc_space(e, udb, udb_chunk_type_task, sz)) {
return 0;
}
if(udb_ptr_is_null(last)) {
udb_base_set_userdata(udb, e->data);
} else {
udb_rptr_set_ptr(&TASKLIST(last)->next, udb, e);
}
udb_ptr_set_ptr(last, udb, e);
/* fill in tasklist item */
udb_rel_ptr_init(&TASKLIST(e)->next);
TASKLIST(e)->size = sz;
TASKLIST(e)->oldserial = 0;
TASKLIST(e)->newserial = 0;
TASKLIST(e)->yesno = 0;
if(zname) {
memmove(TASKLIST(e)->zname, zname, dname_total_size(zname));
}
return 1;
}
void task_new_soainfo(struct udb_base* udb, udb_ptr* last, struct zone* z,
enum soainfo_hint hint)
{
/* calculate size */
udb_ptr e;
size_t sz;
const dname_type* apex, *ns, *em;
if(!z || !z->apex || !domain_dname(z->apex))
return; /* safety check */
DEBUG(DEBUG_IPC,1, (LOG_INFO, "nsd: add soa info for zone %s",
domain_to_string(z->apex)));
apex = domain_dname(z->apex);
sz = sizeof(struct task_list_d) + dname_total_size(apex);
if(z->soa_rrset && hint == soainfo_ok) {
ns = domain_dname(rdata_domain_ref(z->soa_rrset->rrs[0]));
em = domain_dname(rdata_domain_ref_offset(
z->soa_rrset->rrs[0], sizeof(void*)));
sz += sizeof(uint32_t)*6 + sizeof(uint8_t)*2
+ ns->name_size + em->name_size;
} else {
ns = NULL;
em = NULL;
}
/* create new task_list item */
if(!task_create_new_elem(udb, last, &e, sz, apex)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add SOAINFO");
return;
}
TASKLIST(&e)->task_type = task_soa_info;
TASKLIST(&e)->yesno = (uint64_t)hint;
if(z->soa_rrset && hint == soainfo_ok) {
uint32_t ttl = htonl(z->soa_rrset->rrs[0]->ttl);
uint8_t* p = (uint8_t*)TASKLIST(&e)->zname;
p += dname_total_size(apex);
memmove(p, &ttl, sizeof(uint32_t));
p += sizeof(uint32_t);
memmove(p, &ns->name_size, sizeof(uint8_t));
p += sizeof(uint8_t);
memmove(p, dname_name(ns), ns->name_size);
p += ns->name_size;
memmove(p, &em->name_size, sizeof(uint8_t));
p += sizeof(uint8_t);
memmove(p, dname_name(em), em->name_size);
p += em->name_size;
memmove(p, z->soa_rrset->rrs[0]->rdata + sizeof(void*)*2,
sizeof(uint32_t)*5);
}
udb_ptr_unlink(&e, udb);
}
void task_process_sync(struct udb_base* taskudb)
{
/* need to sync before other process uses the mmap? */
DEBUG(DEBUG_IPC,1, (LOG_INFO, "task procsync %s size %d",
taskudb->fname, (int)taskudb->base_size));
(void)taskudb;
}
void task_remap(struct udb_base* taskudb)
{
DEBUG(DEBUG_IPC,1, (LOG_INFO, "task remap %s size %d",
taskudb->fname, (int)taskudb->glob_data->fsize));
udb_base_remap_process(taskudb);
}
void task_clear(struct udb_base* taskudb)
{
udb_ptr t, n;
udb_ptr_new(&t, taskudb, udb_base_get_userdata(taskudb));
udb_base_set_userdata(taskudb, 0);
udb_ptr_init(&n, taskudb);
while(!udb_ptr_is_null(&t)) {
udb_ptr_set_rptr(&n, taskudb, &TASKLIST(&t)->next);
udb_rptr_zero(&TASKLIST(&t)->next, taskudb);
udb_ptr_free_space(&t, taskudb, TASKLIST(&t)->size);
udb_ptr_set_ptr(&t, taskudb, &n);
}
udb_ptr_unlink(&t, taskudb);
udb_ptr_unlink(&n, taskudb);
}
void task_new_expire(struct udb_base* udb, udb_ptr* last,
const struct dname* z, int expired)
{
udb_ptr e;
if(!z) return;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add expire info for zone %s",
dname_to_string(z,NULL)));
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d)+
dname_total_size(z), z)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add expire");
return;
}
TASKLIST(&e)->task_type = task_expire;
TASKLIST(&e)->yesno = expired;
udb_ptr_unlink(&e, udb);
}
void task_new_check_zonefiles(udb_base* udb, udb_ptr* last,
const dname_type* zone)
{
udb_ptr e;
xfrd_check_catalog_consumer_zonefiles(zone);
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add task checkzonefiles"));
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d) +
(zone?dname_total_size(zone):0), zone)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add check_zones");
return;
}
TASKLIST(&e)->task_type = task_check_zonefiles;
TASKLIST(&e)->yesno = (zone!=NULL);
udb_ptr_unlink(&e, udb);
}
void task_new_write_zonefiles(udb_base* udb, udb_ptr* last,
const dname_type* zone)
{
udb_ptr e;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add task writezonefiles"));
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d) +
(zone?dname_total_size(zone):0), zone)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add writezones");
return;
}
TASKLIST(&e)->task_type = task_write_zonefiles;
TASKLIST(&e)->yesno = (zone!=NULL);
udb_ptr_unlink(&e, udb);
}
void task_new_set_verbosity(udb_base* udb, udb_ptr* last, int v)
{
udb_ptr e;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add task set_verbosity"));
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d),
NULL)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add set_v");
return;
}
TASKLIST(&e)->task_type = task_set_verbosity;
TASKLIST(&e)->yesno = v;
udb_ptr_unlink(&e, udb);
}
void
task_new_add_zone(udb_base* udb, udb_ptr* last, const char* zone,
const char* pattern, unsigned zonestatid)
{
size_t zlen = strlen(zone);
size_t plen = strlen(pattern);
void *p;
udb_ptr e;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add task addzone %s %s", zone, pattern));
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d)+
zlen + 1 + plen + 1, NULL)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add addz");
return;
}
TASKLIST(&e)->task_type = task_add_zone;
TASKLIST(&e)->yesno = zonestatid;
p = TASKLIST(&e)->zname;
memcpy(p, zone, zlen+1);
memmove((char*)p+zlen+1, pattern, plen+1);
udb_ptr_unlink(&e, udb);
}
void
task_new_del_zone(udb_base* udb, udb_ptr* last, const dname_type* dname)
{
udb_ptr e;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add task delzone %s", dname_to_string(dname, 0)));
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d)
+dname_total_size(dname), dname)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add delz");
return;
}
TASKLIST(&e)->task_type = task_del_zone;
udb_ptr_unlink(&e, udb);
}
void task_new_add_key(udb_base* udb, udb_ptr* last, struct key_options* key)
{
char* p;
udb_ptr e;
assert(key->name && key->algorithm && key->secret);
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add task addkey"));
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d)
+strlen(key->name)+1+strlen(key->algorithm)+1+
strlen(key->secret)+1, NULL)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add addk");
return;
}
TASKLIST(&e)->task_type = task_add_key;
p = (char*)TASKLIST(&e)->zname;
memmove(p, key->name, strlen(key->name)+1);
p+=strlen(key->name)+1;
memmove(p, key->algorithm, strlen(key->algorithm)+1);
p+=strlen(key->algorithm)+1;
memmove(p, key->secret, strlen(key->secret)+1);
udb_ptr_unlink(&e, udb);
}
void task_new_del_key(udb_base* udb, udb_ptr* last, const char* name)
{
char* p;
udb_ptr e;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add task delkey"));
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d)
+strlen(name)+1, NULL)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add delk");
return;
}
TASKLIST(&e)->task_type = task_del_key;
p = (char*)TASKLIST(&e)->zname;
memmove(p, name, strlen(name)+1);
udb_ptr_unlink(&e, udb);
}
void task_new_cookies(udb_base* udb, udb_ptr* last, int answer_cookie,
size_t cookie_count, void* cookie_secrets) {
udb_ptr e;
char* p;
size_t const secrets_size = sizeof(cookie_secrets_type);
DEBUG(DEBUG_IPC, 1, (LOG_INFO, "add task cookies"));
if(!task_create_new_elem(udb, last, &e,
sizeof(struct task_list_d) + secrets_size, NULL)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add cookies");
return;
}
TASKLIST(&e)->task_type = task_cookies;
TASKLIST(&e)->newserial = (uint32_t) answer_cookie;
TASKLIST(&e)->yesno = (uint64_t) cookie_count;
p = (char*)TASKLIST(&e)->zname;
memmove(p, cookie_secrets, secrets_size);
udb_ptr_unlink(&e, udb);
}
void task_new_add_pattern(udb_base* udb, udb_ptr* last,
struct pattern_options* p)
{
region_type* temp;
buffer_type* buffer;
udb_ptr e;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add task addpattern %s", p->pname));
temp = region_create(xalloc, free);
buffer = buffer_create(temp, 4096);
pattern_options_marshal(buffer, p);
buffer_flip(buffer);
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d)
+ buffer_limit(buffer), NULL)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add addp");
region_destroy(temp);
return;
}
TASKLIST(&e)->task_type = task_add_pattern;
TASKLIST(&e)->yesno = buffer_limit(buffer);
memmove(TASKLIST(&e)->zname, buffer_begin(buffer),
buffer_limit(buffer));
udb_ptr_unlink(&e, udb);
region_destroy(temp);
}
void task_new_del_pattern(udb_base* udb, udb_ptr* last, const char* name)
{
char* p;
udb_ptr e;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add task delpattern %s", name));
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d)
+strlen(name)+1, NULL)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add delp");
return;
}
TASKLIST(&e)->task_type = task_del_pattern;
p = (char*)TASKLIST(&e)->zname;
memmove(p, name, strlen(name)+1);
udb_ptr_unlink(&e, udb);
}
void task_new_opt_change(udb_base* udb, udb_ptr* last, struct nsd_options* opt)
{
udb_ptr e;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add task opt_change"));
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d),
NULL)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add o_c");
return;
}
TASKLIST(&e)->task_type = task_opt_change;
#ifdef RATELIMIT
TASKLIST(&e)->oldserial = opt->rrl_ratelimit;
TASKLIST(&e)->newserial = opt->rrl_whitelist_ratelimit;
TASKLIST(&e)->yesno = (uint64_t) opt->rrl_slip;
#else
(void)opt;
#endif
udb_ptr_unlink(&e, udb);
}
void task_new_zonestat_inc(udb_base* udb, udb_ptr* last, unsigned sz)
{
udb_ptr e;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add task zonestat_inc"));
if(sz == 0)
return; /* no need to decrease to 0 */
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d),
NULL)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add z_i");
return;
}
TASKLIST(&e)->task_type = task_zonestat_inc;
TASKLIST(&e)->oldserial = (uint32_t)sz;
udb_ptr_unlink(&e, udb);
}
int
task_new_apply_xfr(udb_base* udb, udb_ptr* last, const dname_type* dname,
uint32_t old_serial, uint32_t new_serial, uint64_t filenumber)
{
udb_ptr e;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "add task apply_xfr"));
if(!task_create_new_elem(udb, last, &e, sizeof(struct task_list_d)
+dname_total_size(dname), dname)) {
log_msg(LOG_ERR, "tasklist: out of space, cannot add applyxfr");
return 0;
}
TASKLIST(&e)->oldserial = old_serial;
TASKLIST(&e)->newserial = new_serial;
TASKLIST(&e)->yesno = filenumber;
TASKLIST(&e)->task_type = task_apply_xfr;
udb_ptr_unlink(&e, udb);
return 1;
}
void
task_process_expire(namedb_type* db, struct task_list_d* task)
{
uint8_t ok;
zone_type* z = namedb_find_zone(db, task->zname);
assert(task->task_type == task_expire);
if(!z) {
DEBUG(DEBUG_IPC, 1, (LOG_WARNING, "zone %s %s but not in zonetree",
dname_to_string(task->zname, NULL),
task->yesno?"expired":"unexpired"));
return;
}
DEBUG(DEBUG_IPC,1, (LOG_INFO, "xfrd: expire task zone %s %s",
dname_to_string(task->zname,0),
task->yesno?"expired":"unexpired"));
/* find zone, set expire flag */
ok = !task->yesno;
/* only update zone->is_ok if needed to minimize copy-on-write
* of memory pages shared after fork() */
if(ok && !z->is_ok)
z->is_ok = 1;
else if(!ok && z->is_ok)
z->is_ok = 0;
}
static void
task_process_set_verbosity(struct task_list_d* task)
{
DEBUG(DEBUG_IPC,1, (LOG_INFO, "verbosity task %d", (int)task->yesno));
verbosity = task->yesno;
}
static void
task_process_checkzones(struct nsd* nsd, udb_base* taskudb, udb_ptr* last_task,
struct task_list_d* task)
{
/* on SIGHUP check if zone-text-files changed and if so,
* reread. When from xfrd-reload, no need to fstat the files */
if(task->yesno) {
struct zone_options* zo = zone_options_find(nsd->options,
task->zname);
if(zo)
namedb_check_zonefile(nsd, taskudb, last_task, zo);
} else {
/* check all zones */
namedb_check_zonefiles(nsd, nsd->options, taskudb, last_task);
}
}
static void
task_process_writezones(struct nsd* nsd, struct task_list_d* task)
{
if(task->yesno) {
struct zone_options* zo = zone_options_find(nsd->options,
task->zname);
if(zo)
namedb_write_zonefile(nsd, zo);
} else {
namedb_write_zonefiles(nsd, nsd->options);
}
}
static void
task_process_add_zone(struct nsd* nsd, udb_base* udb, udb_ptr* last_task,
struct task_list_d* task)
{
zone_type* z;
const dname_type* zdname;
const char* zname = (const char*)task->zname;
const char* pname = zname + strlen(zname)+1;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "addzone task %s %s", zname, pname));
zdname = dname_parse(nsd->db->region, zname);
if(!zdname) {
log_msg(LOG_ERR, "can not parse zone name %s", zname);
return;
}
/* create zone */
z = find_or_create_zone(nsd->db, zdname, nsd->options, zname, pname);
if(!z) {
region_recycle(nsd->db->region, (void*)zdname,
dname_total_size(zdname));
log_msg(LOG_ERR, "can not add zone %s %s", zname, pname);
return;
}
/* zdname is not used by the zone allocation. */
region_recycle(nsd->db->region, (void*)zdname,
dname_total_size(zdname));
z->zonestatid = (unsigned)task->yesno;
/* if zone is empty, attempt to read the zonefile from disk (if any) */
if(!z->soa_rrset && z->opts->pattern->zonefile) {
namedb_read_zonefile(nsd, z, udb, last_task);
}
}
static void
task_process_del_zone(struct nsd* nsd, struct task_list_d* task)
{
zone_type* zone;
struct zone_options* zopt;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "delzone task %s", dname_to_string(
task->zname, NULL)));
zone = namedb_find_zone(nsd->db, task->zname);
if(!zone)
return;
#ifdef NSEC3
nsec3_clear_precompile(nsd->db, zone);
zone->nsec3_param = NULL;
#endif
delete_zone_rrs(nsd->db, zone);
/* remove from zonetree, apex, soa */
zopt = zone->opts;
namedb_zone_delete(nsd->db, zone);
/* remove from options (zone_list already edited by xfrd) */
zone_options_delete(nsd->options, zopt);
}
static void
task_process_add_key(struct nsd* nsd, struct task_list_d* task)
{
struct key_options key;
key.name = (char*)task->zname;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "addkey task %s", key.name));
key.algorithm = key.name + strlen(key.name)+1;
key.secret = key.algorithm + strlen(key.algorithm)+1;
key_options_add_modify(nsd->options, &key);
memset(key.secret, 0xdd, strlen(key.secret)); /* wipe secret */
}
static void
task_process_del_key(struct nsd* nsd, struct task_list_d* task)
{
char* name = (char*)task->zname;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "delkey task %s", name));
/* this is reload and nothing is using the TSIG key right now */
key_options_remove(nsd->options, name);
}
static void
task_process_cookies(struct nsd* nsd, struct task_list_d* task) {
DEBUG(DEBUG_IPC, 1, (LOG_INFO, "cookies task answer: %s, count: %d",
task->newserial ? "yes" : "no", (int)task->yesno));
nsd->do_answer_cookie = (int) task->newserial;
nsd->cookie_count = (size_t) task->yesno;
memmove(nsd->cookie_secrets, task->zname, sizeof(nsd->cookie_secrets));
explicit_bzero(task->zname, sizeof(nsd->cookie_secrets));
}
static void
task_process_add_pattern(struct nsd* nsd, struct task_list_d* task)
{
region_type* temp = region_create(xalloc, free);
buffer_type buffer;
struct pattern_options *pat;
buffer_create_from(&buffer, task->zname, task->yesno);
pat = pattern_options_unmarshal(temp, &buffer);
DEBUG(DEBUG_IPC,1, (LOG_INFO, "addpattern task %s", pat->pname));
pattern_options_add_modify(nsd->options, pat);
region_destroy(temp);
}
static void
task_process_del_pattern(struct nsd* nsd, struct task_list_d* task)
{
char* name = (char*)task->zname;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "delpattern task %s", name));
pattern_options_remove(nsd->options, name);
}
static void
task_process_opt_change(struct nsd* nsd, struct task_list_d* task)
{
DEBUG(DEBUG_IPC,1, (LOG_INFO, "optchange task"));
#ifdef RATELIMIT
nsd->options->rrl_ratelimit = task->oldserial;
nsd->options->rrl_whitelist_ratelimit = task->newserial;
nsd->options->rrl_slip = task->yesno;
rrl_set_limit(nsd->options->rrl_ratelimit, nsd->options->rrl_whitelist_ratelimit,
nsd->options->rrl_slip);
#else
(void)nsd; (void)task;
#endif
}
#ifdef USE_ZONE_STATS
static void
task_process_zonestat_inc(struct nsd* nsd, udb_base* udb, udb_ptr *last_task,
struct task_list_d* task)
{
DEBUG(DEBUG_IPC,1, (LOG_INFO, "zonestat_inc task %u", (unsigned)task->oldserial));
nsd->zonestatdesired = (unsigned)task->oldserial;
/* send echo to xfrd to increment on its end */
task_new_zonestat_inc(udb, last_task, nsd->zonestatdesired);
}
#endif
void
task_process_apply_xfr(struct nsd* nsd, udb_base* udb, udb_ptr* task)
{
/* we have to use an udb_ptr task here, because the apply_xfr procedure
* appends soa_info which may remap and change the pointer. */
zone_type* zone;
FILE* df;
DEBUG(DEBUG_IPC,1, (LOG_INFO, "applyxfr task %s", dname_to_string(
TASKLIST(task)->zname, NULL)));
zone = namedb_find_zone(nsd->db, TASKLIST(task)->zname);
if(!zone) {
/* assume the zone has been deleted and a zone transfer was
* still waiting to be processed */
udb_ptr_free_space(task, udb, TASKLIST(task)->size);
return;
}
/* apply the XFR */
/* oldserial, newserial, yesno is filenumber */
df = xfrd_open_xfrfile(nsd, TASKLIST(task)->yesno, "r");
if(!df) {
/* could not open file to update */
/* soainfo_gone will be communicated from server_reload, unless
preceding updates have been applied */
zone->is_skipped = 1;
udb_ptr_free_space(task, udb, TASKLIST(task)->size);
return;
}
/* read and apply zone transfer */
switch(apply_ixfr_for_zone(nsd, zone, df, nsd->options, udb,
TASKLIST(task)->yesno)) {
case 1: /* Success */
break;
case 0: /* Failure */
/* soainfo_gone will be communicated from server_reload, unless
preceding updates have been applied */
zone->is_skipped = 1;
break;
case -1:/* Fatal */
exit(1);
break;
default:break;
}
fclose(df);
udb_ptr_free_space(task, udb, TASKLIST(task)->size);
}
void task_process_in_reload(struct nsd* nsd, udb_base* udb, udb_ptr *last_task,
udb_ptr* task)
{
switch(TASKLIST(task)->task_type) {
case task_expire:
task_process_expire(nsd->db, TASKLIST(task));
break;
case task_check_zonefiles:
task_process_checkzones(nsd, udb, last_task, TASKLIST(task));
break;
case task_write_zonefiles:
task_process_writezones(nsd, TASKLIST(task));
break;
case task_set_verbosity:
task_process_set_verbosity(TASKLIST(task));
break;
case task_add_zone:
task_process_add_zone(nsd, udb, last_task, TASKLIST(task));
break;
case task_del_zone:
task_process_del_zone(nsd, TASKLIST(task));
break;
case task_add_key:
task_process_add_key(nsd, TASKLIST(task));
break;
case task_del_key:
task_process_del_key(nsd, TASKLIST(task));
break;
case task_add_pattern:
task_process_add_pattern(nsd, TASKLIST(task));
break;
case task_del_pattern:
task_process_del_pattern(nsd, TASKLIST(task));
break;
case task_opt_change:
task_process_opt_change(nsd, TASKLIST(task));
break;
#ifdef USE_ZONE_STATS
case task_zonestat_inc:
task_process_zonestat_inc(nsd, udb, last_task, TASKLIST(task));
break;
#endif
case task_cookies:
task_process_cookies(nsd, TASKLIST(task));
break;
default:
log_msg(LOG_WARNING, "unhandled task in reload type %d",
(int)TASKLIST(task)->task_type);
break;
}
udb_ptr_free_space(task, udb, TASKLIST(task)->size);
}
|