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
|
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <assert.h>
#include <stdlib.h>
#include <inttypes.h>
#include "updates/ddns.h"
#include "updates/changesets.h"
#include "util/debug.h"
#include "packet/packet.h"
#include "common.h"
#include "consts.h"
#include "common/mempattern.h"
#include "nameserver/name-server.h" // ns_serial_compare() - TODO: extract
#include "updates/xfr-in.h"
#include "common/descriptor.h"
/*----------------------------------------------------------------------------*/
// Copied from XFR - maybe extract somewhere else
static int knot_ddns_prereq_check_rrsets(knot_rrset_t ***rrsets,
size_t *count, size_t *allocated)
{
/* This is really confusing, it's ptr -> array of "knot_rrset_t*" */
char *arr = (char*)*rrsets;
int ret = 0;
ret = mreserve(&arr, sizeof(knot_rrset_t*), *count + 1, 0, allocated);
if (ret < 0) {
return KNOT_ENOMEM;
}
*rrsets = (knot_rrset_t**)arr;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_prereq_check_dnames(knot_dname_t ***dnames,
size_t *count, size_t *allocated)
{
/* This is really confusing, it's ptr -> array of "knot_dname_t*" */
char *arr = (char*)*dnames;
int ret = 0;
ret = mreserve(&arr, sizeof(knot_dname_t*), *count + 1, 0, allocated);
if (ret < 0) {
return KNOT_ENOMEM;
}
*dnames = (knot_dname_t**)arr;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_prereq_rrset(const knot_rrset_t *rrset,
knot_rrset_t ***rrsets,
size_t *count, size_t *allocd)
{
// check if such RRSet is not already there and merge if needed
int ret;
for (int i = 0; i < *count; ++i) {
if (knot_rrset_equal(rrset, (*rrsets)[i],
KNOT_RRSET_COMPARE_HEADER) == 1) {
ret = knot_rrset_merge((*rrsets)[i], rrset);
if (ret != KNOT_EOK) {
return ret;
} else {
return KNOT_EOK;
}
}
}
// if we are here, the RRSet was not found
ret = knot_ddns_prereq_check_rrsets(rrsets, count, allocd);
if (ret != KNOT_EOK) {
return ret;
}
knot_rrset_t *new_rrset = NULL;
ret = knot_rrset_deep_copy(rrset, &new_rrset, 0);
if (ret != KNOT_EOK) {
return ret;
}
(*rrsets)[(*count)++] = new_rrset;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_prereq_dname(const knot_dname_t *dname,
knot_dname_t ***dnames,
size_t *count, size_t *allocd)
{
// we do not have to check if the name is not already there
// if it is, we will just check it twice in the zone
int ret = knot_ddns_prereq_check_dnames(dnames, count, allocd);
if (ret != KNOT_EOK) {
return ret;
}
knot_dname_t *dname_new = knot_dname_deep_copy(dname);
if (dname_new == NULL) {
return KNOT_ENOMEM;
}
(*dnames)[(*count)++] = dname_new;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_prereq(knot_ddns_prereq_t *prereqs,
const knot_rrset_t *rrset, uint16_t qclass)
{
assert(prereqs != NULL);
assert(rrset != NULL);
if (knot_rrset_ttl(rrset) != 0) {
dbg_ddns("ddns: add_prereq: Wrong TTL.\n");
return KNOT_EMALF;
}
int ret;
if (knot_rrset_class(rrset) == KNOT_CLASS_ANY) {
if (knot_rrset_rdata_rr_count(rrset)) {
dbg_ddns("ddns: add_prereq: Extra data\n");
return KNOT_EMALF;
}
if (knot_rrset_type(rrset) == KNOT_RRTYPE_ANY) {
ret = knot_ddns_add_prereq_dname(
knot_rrset_owner(rrset), &prereqs->in_use,
&prereqs->in_use_count,
&prereqs->in_use_allocd);
} else {
ret = knot_ddns_add_prereq_rrset(rrset,
&prereqs->exist,
&prereqs->exist_count,
&prereqs->exist_allocd);
}
} else if (knot_rrset_class(rrset) == KNOT_CLASS_NONE) {
if (knot_rrset_rdata_rr_count(rrset)) {
dbg_ddns("ddns: add_prereq: Extra data\n");
return KNOT_EMALF;
}
if (knot_rrset_type(rrset) == KNOT_RRTYPE_ANY) {
ret = knot_ddns_add_prereq_dname(
knot_rrset_owner(rrset), &prereqs->not_in_use,
&prereqs->not_in_use_count,
&prereqs->not_in_use_allocd);
} else {
ret = knot_ddns_add_prereq_rrset(rrset,
&prereqs->not_exist,
&prereqs->not_exist_count,
&prereqs->not_exist_allocd);
}
} else if (knot_rrset_class(rrset) == qclass) {
ret = knot_ddns_add_prereq_rrset(rrset,
&prereqs->exist_full,
&prereqs->exist_full_count,
&prereqs->exist_full_allocd);
} else {
dbg_ddns("ddns: add_prereq: Bad class.\n");
return KNOT_EMALF;
}
return ret;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_remove_rr(knot_changeset_t *changeset,
const knot_rrset_t *rr)
{
dbg_ddns_verb("Removing possible redundant RRs from changeset.\n");
for (int i = 0; i < changeset->add_count; ++i) {
// Removing RR(s) from this owner
if (knot_dname_compare(knot_rrset_owner(rr),
knot_rrset_owner(changeset->add[i])) == 0) {
// Removing one or all RRSets
if (knot_rrset_class(rr) == KNOT_CLASS_ANY) {
dbg_ddns_detail("Removing one or all "
"RRSets\n");
if (knot_rrset_type(rr)
== knot_rrset_type(changeset->add[i])
|| knot_rrset_type(rr) == KNOT_RRTYPE_ANY) {
knot_rrset_t *remove =
knot_changeset_remove_rr(
changeset->add,
&changeset->add_count, i);
dbg_ddns_detail("Removed RRSet from "
"chgset:\n");
knot_rrset_dump(remove);
knot_rrset_deep_free(&remove, 1, 1);
}
} else if (knot_rrset_type(rr)
== knot_rrset_type(changeset->add[i])){
/* All other classes are checked in
* knot_ddns_check_update().
*/
assert(knot_rrset_class(rr) == KNOT_CLASS_NONE);
// Removing specific RR from a RRSet
int ret =
knot_rrset_remove_rr_using_rrset_del(changeset->add[i], rr);
if (ret != KNOT_EOK) {
dbg_ddns("ddns: Could not remove RDATA from RRSet (%s).\n",
knot_strerror(ret));
return ret;
}
// if the RRSet is empty, remove from changeset
if (knot_rrset_rdata_rr_count(changeset->add[i])
== 0) {
knot_rrset_t *remove =
knot_changeset_remove_rr(
changeset->add,
&changeset->add_count, i);
dbg_ddns_detail("RRSet empty, removing."
"\n");
knot_rrset_deep_free(&remove, 1, 1);
}
}
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_update(knot_changeset_t *changeset,
const knot_rrset_t *rrset, uint16_t qclass,
knot_rrset_t **rrset_copy)
{
assert(changeset != NULL);
assert(rrset != NULL);
assert(rrset_copy != NULL);
int ret;
// create a copy of the RRSet
/*! \todo ref #937 If the packet was not parsed all at once, we could save this
* copy.
*/
*rrset_copy = NULL;
ret = knot_rrset_deep_copy(rrset, rrset_copy, 1);
if (ret != KNOT_EOK) {
return ret;
}
if (knot_rrset_class(rrset) == qclass) {
// this RRSet should be added to the zone
dbg_ddns_detail(" * adding RR %p\n", *rrset_copy);
ret = knot_changeset_add_rr(&changeset->add,
&changeset->add_count,
&changeset->add_allocated,
*rrset_copy);
} else {
// this RRSet marks removal of something from zone
/* To imitate in-order processing of UPDATE RRs, we must check
* If this REMOVE RR does not affect any of the previous
* ADD RRs in this update. If yes, they must be removed from
* the changeset.
*
* See https://git.nic.cz/redmine/issues/937#note-14 and below.
*/
// TODO: finish, disabled for now
dbg_ddns_detail(" * removing RR %p\n", *rrset_copy);
ret = knot_ddns_check_remove_rr(changeset, *rrset_copy);
if (ret != KNOT_EOK) {
knot_rrset_deep_free(rrset_copy, 1, 1);
return ret;
}
ret = knot_changeset_add_rr(&changeset->remove,
&changeset->remove_count,
&changeset->remove_allocated,
*rrset_copy);
}
return ret;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_exist(const knot_zone_contents_t *zone,
const knot_rrset_t *rrset, knot_rcode_t *rcode)
{
assert(zone != NULL);
assert(rrset != NULL);
assert(rcode != NULL);
assert(knot_rrset_rdata_rr_count(rrset) == 0);
assert(knot_rrset_type(rrset) != KNOT_RRTYPE_ANY);
assert(knot_rrset_ttl(rrset) == 0);
assert(knot_rrset_class(rrset) == KNOT_CLASS_ANY);
if (!knot_dname_is_subdomain(knot_rrset_owner(rrset),
knot_node_owner(knot_zone_contents_apex(zone)))) {
*rcode = KNOT_RCODE_NOTZONE;
return KNOT_EBADZONE;
}
const knot_node_t *node;
node = knot_zone_contents_find_node(zone, knot_rrset_owner(rrset));
if (node == NULL) {
*rcode = KNOT_RCODE_NXRRSET;
return KNOT_ENONODE;
} else if (knot_node_rrset(node, knot_rrset_type(rrset)) == NULL) {
*rcode = KNOT_RCODE_NXRRSET;
return KNOT_ENORRSET;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_exist_full(const knot_zone_contents_t *zone,
const knot_rrset_t *rrset,
knot_rcode_t *rcode)
{
assert(zone != NULL);
assert(rrset != NULL);
assert(rcode != NULL);
assert(knot_rrset_rdata_rr_count(rrset) == 0);
assert(knot_rrset_type(rrset) != KNOT_RRTYPE_ANY);
assert(knot_rrset_ttl(rrset) == 0);
assert(knot_rrset_class(rrset) == KNOT_CLASS_ANY);
if (!knot_dname_is_subdomain(knot_rrset_owner(rrset),
knot_node_owner(knot_zone_contents_apex(zone)))) {
*rcode = KNOT_RCODE_NOTZONE;
return KNOT_EBADZONE;
}
const knot_node_t *node;
const knot_rrset_t *found;
node = knot_zone_contents_find_node(zone, knot_rrset_owner(rrset));
if (node == NULL) {
*rcode = KNOT_RCODE_NXRRSET;
return KNOT_EPREREQ;
} else if ((found = knot_node_rrset(node, knot_rrset_type(rrset)))
== NULL) {
*rcode = KNOT_RCODE_NXRRSET;
return KNOT_EPREREQ;
} else {
// do not have to compare the header, it is already done
assert(knot_rrset_type(found) == knot_rrset_type(rrset));
assert(knot_dname_compare(knot_rrset_owner(found),
knot_rrset_owner(rrset)) == 0);
if (knot_rrset_rdata_equal(found, rrset) <= 0) {
*rcode = KNOT_RCODE_NXRRSET;
return KNOT_EPREREQ;
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_not_exist(const knot_zone_contents_t *zone,
const knot_rrset_t *rrset,
knot_rcode_t *rcode)
{
assert(zone != NULL);
assert(rrset != NULL);
assert(rcode != NULL);
assert(knot_rrset_rdata_rr_count(rrset) == 0);
assert(knot_rrset_type(rrset) != KNOT_RRTYPE_ANY);
assert(knot_rrset_ttl(rrset) == 0);
assert(knot_rrset_class(rrset) == KNOT_CLASS_NONE);
if (!knot_dname_is_subdomain(knot_rrset_owner(rrset),
knot_node_owner(knot_zone_contents_apex(zone)))) {
*rcode = KNOT_RCODE_NOTZONE;
return KNOT_EBADZONE;
}
const knot_node_t *node;
node = knot_zone_contents_find_node(zone, knot_rrset_owner(rrset));
if (node == NULL) {
return KNOT_EOK;
} else if (knot_node_rrset(node, knot_rrset_type(rrset)) == NULL) {
return KNOT_EOK;
}
/* RDATA is always empty for simple RRset checks. */
*rcode = KNOT_RCODE_YXRRSET;
return KNOT_EPREREQ;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_in_use(const knot_zone_contents_t *zone,
const knot_dname_t *dname,
knot_rcode_t *rcode)
{
assert(zone != NULL);
assert(dname != NULL);
assert(rcode != NULL);
if (!knot_dname_is_subdomain(dname,
knot_node_owner(knot_zone_contents_apex(zone)))) {
*rcode = KNOT_RCODE_NOTZONE;
return KNOT_EBADZONE;
}
const knot_node_t *node;
node = knot_zone_contents_find_node(zone, dname);
if (node == NULL) {
*rcode = KNOT_RCODE_NXDOMAIN;
return KNOT_EPREREQ;
} else if (knot_node_rrset_count(node) == 0) {
*rcode = KNOT_RCODE_NXDOMAIN;
return KNOT_EPREREQ;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_not_in_use(const knot_zone_contents_t *zone,
const knot_dname_t *dname,
knot_rcode_t *rcode)
{
assert(zone != NULL);
assert(dname != NULL);
assert(rcode != NULL);
if (!knot_dname_is_subdomain(dname,
knot_node_owner(knot_zone_contents_apex(zone)))) {
*rcode = KNOT_RCODE_NOTZONE;
return KNOT_EBADZONE;
}
const knot_node_t *node;
node = knot_zone_contents_find_node(zone, dname);
if (node == NULL) {
return KNOT_EOK;
} else if (knot_node_rrset_count(node) == 0) {
return KNOT_EOK;
}
*rcode = KNOT_RCODE_YXDOMAIN;
return KNOT_EPREREQ;
}
/*----------------------------------------------------------------------------*/
/* API functions */
/*----------------------------------------------------------------------------*/
int knot_ddns_check_zone(const knot_zone_contents_t *zone,
const knot_packet_t *query, knot_rcode_t *rcode)
{
if (zone == NULL || query == NULL || rcode == NULL) {
if (rcode != NULL) {
*rcode = KNOT_RCODE_SERVFAIL;
}
return KNOT_EINVAL;
}
if (knot_packet_qtype(query) != KNOT_RRTYPE_SOA) {
*rcode = KNOT_RCODE_FORMERR;
return KNOT_EMALF;
}
// check zone CLASS
if (knot_zone_contents_class(zone) !=
knot_packet_qclass(query)) {
*rcode = KNOT_RCODE_NOTAUTH;
return KNOT_ENOZONE;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
int knot_ddns_process_prereqs(const knot_packet_t *query,
knot_ddns_prereq_t **prereqs, knot_rcode_t *rcode)
{
/*! \todo Consider not parsing the whole packet at once, but
* parsing one RR at a time - could save some memory and time.
*/
if (query == NULL || prereqs == NULL || rcode == NULL) {
return KNOT_EINVAL;
}
dbg_ddns("Processing prerequisities.\n");
// allocate space for the prerequisities
*prereqs = (knot_ddns_prereq_t *)calloc(1, sizeof(knot_ddns_prereq_t));
CHECK_ALLOC_LOG(*prereqs, KNOT_ENOMEM);
int ret;
for (int i = 0; i < knot_packet_answer_rrset_count(query); ++i) {
// we must copy the RRSets, because all those stored in the
// packet will be destroyed
dbg_ddns_detail("Creating prereqs from following RRSet:\n");
knot_rrset_dump(knot_packet_answer_rrset(query, i));
ret = knot_ddns_add_prereq(*prereqs,
knot_packet_answer_rrset(query, i),
knot_packet_qclass(query));
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add prerequisity RRSet:%s\n",
knot_strerror(ret));
*rcode = (ret == KNOT_EMALF) ? KNOT_RCODE_FORMERR
: KNOT_RCODE_SERVFAIL;
knot_ddns_prereqs_free(prereqs);
return ret;
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
int knot_ddns_check_prereqs(const knot_zone_contents_t *zone,
knot_ddns_prereq_t **prereqs, knot_rcode_t *rcode)
{
int i, ret;
dbg_ddns("Checking 'exist' prerequisities.\n");
for (i = 0; i < (*prereqs)->exist_count; ++i) {
ret = knot_ddns_check_exist(zone, (*prereqs)->exist[i], rcode);
if (ret != KNOT_EOK) {
return ret;
}
}
dbg_ddns("Checking 'exist full' prerequisities.\n");
for (i = 0; i < (*prereqs)->exist_full_count; ++i) {
ret = knot_ddns_check_exist_full(zone,
(*prereqs)->exist_full[i],
rcode);
if (ret != KNOT_EOK) {
return ret;
}
}
dbg_ddns("Checking 'not exist' prerequisities.\n");
for (i = 0; i < (*prereqs)->not_exist_count; ++i) {
ret = knot_ddns_check_not_exist(zone, (*prereqs)->not_exist[i],
rcode);
if (ret != KNOT_EOK) {
return ret;
}
}
dbg_ddns("Checking 'in use' prerequisities.\n");
for (i = 0; i < (*prereqs)->in_use_count; ++i) {
ret = knot_ddns_check_in_use(zone, (*prereqs)->in_use[i],
rcode);
if (ret != KNOT_EOK) {
return ret;
}
}
dbg_ddns("Checking 'not in use' prerequisities.\n");
for (i = 0; i < (*prereqs)->not_in_use_count; ++i) {
ret = knot_ddns_check_not_in_use(zone,
(*prereqs)->not_in_use[i],
rcode);
if (ret != KNOT_EOK) {
return ret;
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_update(const knot_rrset_t *rrset,
const knot_packet_t *query,
knot_rcode_t *rcode)
{
/* Accept both subdomain and dname match. */
dbg_ddns("Checking UPDATE packet.\n");
const knot_dname_t *owner = knot_rrset_owner(rrset);
const knot_dname_t *qname = knot_packet_qname(query);
int is_sub = knot_dname_is_subdomain(owner, qname);
if (!is_sub && knot_dname_compare(owner, qname) != 0) {
*rcode = KNOT_RCODE_NOTZONE;
return KNOT_EBADZONE;
}
if (knot_rrset_class(rrset) == knot_packet_qclass(query)) {
if (knot_rrtype_is_metatype(knot_rrset_type(rrset))) {
*rcode = KNOT_RCODE_FORMERR;
return KNOT_EMALF;
}
} else if (knot_rrset_class(rrset) == KNOT_CLASS_ANY) {
if (knot_rrset_rdata_rr_count(rrset)
|| (knot_rrtype_is_metatype(knot_rrset_type(rrset))
&& knot_rrset_type(rrset) != KNOT_RRTYPE_ANY)) {
*rcode = KNOT_RCODE_FORMERR;
return KNOT_EMALF;
}
} else if (knot_rrset_class(rrset) == KNOT_CLASS_NONE) {
if (knot_rrset_ttl(rrset) != 0
|| knot_rrtype_is_metatype(knot_rrset_type(rrset))) {
*rcode = KNOT_RCODE_FORMERR;
return KNOT_EMALF;
}
} else {
*rcode = KNOT_RCODE_FORMERR;
return KNOT_EMALF;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
int knot_ddns_process_update(const knot_zone_contents_t *zone,
const knot_packet_t *query,
knot_changeset_t *changeset, knot_rcode_t *rcode)
{
// just put all RRSets from query's Authority section
// it will be distinguished when applying to the zone
if (query == NULL || changeset == NULL || rcode == NULL) {
return KNOT_EINVAL;
}
int ret = KNOT_EOK;
/* Copy base SOA query. */
const knot_rrset_t *soa = knot_node_rrset(knot_zone_contents_apex(zone),
KNOT_RRTYPE_SOA);
knot_rrset_t *soa_begin = NULL;
knot_rrset_t *soa_end = NULL;
ret = knot_rrset_deep_copy(soa, &soa_begin, 1);
if (ret == KNOT_EOK) {
knot_changeset_store_soa(&changeset->soa_from,
&changeset->serial_from, soa_begin);
} else {
*rcode = KNOT_RCODE_SERVFAIL;
return ret;
}
/* Current SERIAL */
int64_t sn = knot_rrset_rdata_soa_serial(soa_begin);
int64_t sn_new;
/* Incremented SERIAL
* We must set it now to be able to compare SERIAL from SOAs in the
* UPDATE to it. Although we do not have the new SOA yet.
*/
if (sn > -1) {
sn_new = (uint32_t)sn + 1;
} else {
*rcode = KNOT_RCODE_SERVFAIL;
return ret;
}
const knot_rrset_t *rrset = NULL;
knot_rrset_t *rrset_copy = NULL;
dbg_ddns("Processing UPDATE section.\n");
for (int i = 0; i < knot_packet_authority_rrset_count(query); ++i) {
rrset = knot_packet_authority_rrset(query, i);
ret = knot_ddns_check_update(rrset, query, rcode);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to check update RRSet:%s\n",
knot_strerror(ret));
return ret;
}
ret = knot_ddns_add_update(changeset, rrset,
knot_packet_qclass(query),
&rrset_copy);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add update RRSet:%s\n",
knot_strerror(ret));
*rcode = (ret == KNOT_EMALF) ? KNOT_RCODE_FORMERR
: KNOT_RCODE_SERVFAIL;
return ret;
}
/* Check if the added record is SOA. If yes, check the SERIAL.
* If this record should cause the SOA to be replaced in the
* zone, use it as the ending SOA.
*
* Also handle cases where there are multiple SOAs to be added
* in the same UPDATE. The one with the largest SERIAL should
* be used.
*
* TODO: If there are more SOAs in the UPDATE one after another,
* the ddns_add_update() function will merge them into a
* RRSet. This should be handled somehow.
*/
if (knot_rrset_type(rrset) == KNOT_RRTYPE_SOA
&& ns_serial_compare(knot_rrset_rdata_soa_serial(rrset),
sn_new) > 0) {
sn_new = knot_rrset_rdata_soa_serial(rrset);
soa_end = (knot_rrset_t *)rrset_copy;
}
}
/* Ending SOA */
if (soa_end == NULL) {
/* If not set */
assert(sn_new == (uint32_t)sn + 1);
ret = knot_rrset_deep_copy(soa, &soa_end, 1);
if (ret != KNOT_EOK) {
dbg_ddns("ddns: Could not copy SOA RRSet (%s).\n ",
knot_strerror(ret));
return ret;
}
knot_rrset_rdata_soa_serial_set(soa_end, sn_new);
}
knot_changeset_store_soa(&changeset->soa_to,
&changeset->serial_to,
soa_end);
return ret;
}
/*----------------------------------------------------------------------------*/
void knot_ddns_prereqs_free(knot_ddns_prereq_t **prereq)
{
dbg_ddns("Freeing prerequisities.\n");
int i;
for (i = 0; i < (*prereq)->exist_count; ++i) {
knot_rrset_deep_free(&(*prereq)->exist[i], 1, 1);
}
free((*prereq)->exist);
for (i = 0; i < (*prereq)->exist_full_count; ++i) {
knot_rrset_deep_free(&(*prereq)->exist_full[i], 1, 1);
}
free((*prereq)->exist_full);
for (i = 0; i < (*prereq)->not_exist_count; ++i) {
knot_rrset_deep_free(&(*prereq)->not_exist[i], 1, 1);
}
free((*prereq)->not_exist);
for (i = 0; i < (*prereq)->in_use_count; ++i) {
knot_dname_free(&(*prereq)->in_use[i]);
}
free((*prereq)->in_use);
for (i = 0; i < (*prereq)->not_in_use_count; ++i) {
knot_dname_free(&(*prereq)->not_in_use[i]);
}
free((*prereq)->not_in_use);
free(*prereq);
*prereq = NULL;
}
/*----------------------------------------------------------------------------*/
/* New DDNS processing - */
/*----------------------------------------------------------------------------*/
static int knot_ddns_check_remove_rr2(knot_changeset_t *changeset,
const knot_dname_t *owner,
const knot_rrset_t *rr,
knot_rrset_t ***removed,
size_t *removed_count)
{
assert(changeset != NULL);
assert(removed != NULL);
assert(removed_count != NULL);
*removed_count = 0;
*removed = (knot_rrset_t **)malloc(changeset->add_count
* sizeof(knot_rrset_t *));
if (*removed == NULL) {
ERR_ALLOC_FAILED;
return KNOT_ENOMEM;
}
knot_rrset_t *remove = NULL;
/*
* We assume that each RR in the ADD section of the changeset is in its
* own RRSet. It should be as this is how they are stored there by the
* ddns_process_add() function.
*/
dbg_ddns_verb("Removing possible redundant RRs from changeset.\n");
for (int i = 0; i < changeset->add_count; ++i) {
// Removing RR(s) from this owner
dbg_ddns_exec_detail(
char *name = knot_dname_to_str(changeset->add[i]->owner);
dbg_ddns_detail("ddns: remove_rr2: Removing RR of type=%u owned by %s\n",
knot_rrset_type(changeset->add[i]), name);
free(name);
);
if (knot_dname_compare_non_canon(knot_rrset_owner(changeset->add[i]),
owner) == 0) {
// Removing one or all RRSets
if ((knot_rrset_rdata_rr_count(rr) == 0)
&& (knot_rrset_type(rr) == knot_rrset_type(changeset->add[i])
|| knot_rrset_type(rr) == KNOT_RRTYPE_ANY)) {
dbg_ddns_detail("Removing one or all RRSets\n");
remove = knot_changeset_remove_rr(
changeset->add,
&changeset->add_count, i);
} else if (knot_rrset_type(rr) ==
knot_rrset_type(changeset->add[i])) {
// Removing specific RR
assert(knot_rrset_rdata_rr_count(rr) != 0);
// We must check if the RDATA match
if (knot_rrset_rdata_equal(rr,
changeset->add[i])) {
remove = knot_changeset_remove_rr(
changeset->add,
&changeset->add_count, i);
}
}
dbg_ddns_detail("Removed RRSet from chgset:\n");
knot_rrset_dump(remove);
(*removed)[(*removed_count)++] = remove;
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static void knot_ddns_check_add_rr(knot_changeset_t *changeset,
const knot_rrset_t *rr,
knot_rrset_t **removed)
{
assert(changeset != NULL);
assert(rr != NULL);
assert(removed != NULL);
*removed = NULL;
dbg_ddns_verb("Removing possible redundant RRs from changeset.\n");
for (int i = 0; i < changeset->remove_count; ++i) {
/* Just check exact match, the changeset contains only
* whole RRs that have been removed.
*/
if (knot_rrset_equal(rr, changeset->remove[i],
KNOT_RRSET_COMPARE_WHOLE) == 1) {
*removed = knot_changeset_remove_rr(
changeset->remove,
&changeset->remove_count, i);
dbg_ddns_detail("Removed RRSet from chgset:\n");
knot_rrset_dump(*removed);
break;
}
}
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_rr_is_nsec3(const knot_rrset_t *rr)
{
assert(rr != NULL);
if ((knot_rrset_type(rr) == KNOT_RRTYPE_NSEC3)
|| (knot_rrset_type(rr) == KNOT_RRTYPE_RRSIG
&& knot_rrset_rdata_rr_count(rr)
&& knot_rrset_rdata_rrsig_type_covered(rr)
== KNOT_RRTYPE_NSEC3))
{
dbg_ddns_detail("This is NSEC3-related RRSet.\n");
return 1;
} else {
return 0;
}
}
/*----------------------------------------------------------------------------*/
/*! \note Copied from xfrin_add_new_node(). */
static knot_node_t *knot_ddns_add_new_node(knot_zone_contents_t *zone,
knot_dname_t *owner, int is_nsec3)
{
assert(zone != NULL);
assert(owner != NULL);
knot_node_t *node = knot_node_new(owner, NULL, 0);
if (node == NULL) {
dbg_xfrin("Failed to create a new node.\n");
return NULL;
}
int ret = 0;
// insert the node into zone structures and create parents if
// necessary
if (is_nsec3) {
ret = knot_zone_contents_add_nsec3_node(zone, node, 1, 0);
} else {
ret = knot_zone_contents_add_node(zone, node, 1, 0);
}
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add new node to zone contents.\n");
knot_node_free(&node);
return NULL;
}
/*!
* \note It is not needed to set the previous node, we will do this
* in adjusting after the transfer.
*/
assert(zone->zone != NULL);
//knot_node_set_zone(node, zone->zone);
assert(node->zone == zone->zone);
return node;
}
/*----------------------------------------------------------------------------*/
static knot_node_t *knot_ddns_get_node(knot_zone_contents_t *zone,
const knot_rrset_t *rr)
{
assert(zone != NULL);
assert(rr != NULL);
knot_node_t *node = NULL;
knot_dname_t *owner = knot_rrset_get_owner(rr);
dbg_ddns_detail("Searching for node...\n");
if (knot_ddns_rr_is_nsec3(rr)) {
node = knot_zone_contents_get_nsec3_node(zone, owner);
} else {
node = knot_zone_contents_get_node(zone, owner);
}
return node;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_process_add_cname(knot_node_t *node,
const knot_rrset_t *rr,
knot_changeset_t *changeset,
knot_changes_t *changes)
{
assert(node != NULL);
assert(rr != NULL);
assert(changeset != NULL);
assert(changes != NULL);
dbg_ddns_detail("Adding CNAME RR.\n");
int ret = 0;
/* Get the current CNAME RR from the node. */
knot_rrset_t *removed = knot_node_get_rrset(node, KNOT_RRTYPE_CNAME);
if (removed != NULL) {
/* If they are identical, ignore. */
if (knot_rrset_equal(removed, rr, KNOT_RRSET_COMPARE_WHOLE)
== 1) {
dbg_ddns_verb("CNAME identical to one in the node.\n");
return 1;
}
/*! \note
* Together with the removed CNAME we remove also its RRSIGs as
* they would not be valid for the new CNAME anyway.
*
* \todo Document!!
*/
/* b) Store it to 'changes', together with its RRSIGs. */
ret = knot_changes_add_old_rrsets(&removed, 1, changes, 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add removed RRSet to "
"'changes': %s\n", knot_strerror(ret));
return ret;
}
/* c) And remove it from the node. */
UNUSED(knot_node_remove_rrset(node, KNOT_RRTYPE_CNAME));
/* d) Check if this CNAME was not previously added by
* the UPDATE. If yes, remove it from the ADD
* section and do not add it to the REMOVE section.
*/
knot_rrset_t **from_chgset = NULL;
size_t from_chgset_count = 0;
ret = knot_ddns_check_remove_rr2(
changeset, knot_rrset_owner(removed),
removed, &from_chgset, &from_chgset_count);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to remove possible redundant "
"RRs from ADD section: %s.\n",
knot_strerror(ret));
free(from_chgset);
return ret;
}
assert(from_chgset_count <= 1);
if (from_chgset_count == 1) {
/* Just delete the RRSet. */
knot_rrset_deep_free(&(from_chgset[0]), 1, 1);
/* Okay, &(from_chgset[0]) is basically equal to just
* from_chgset, but it's more clear this way that we are
* deleting the first RRSet in the array ;-)
*/
} else {
/* Otherwise copy the removed CNAME and add it
* to the REMOVE section.
*/
knot_rrset_t *removed_copy;
ret = knot_rrset_deep_copy(removed,
&removed_copy, 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to copy removed RRSet:"
" %s\n", knot_strerror(ret));
free(from_chgset);
return ret;
}
ret = knot_changeset_add_rrset(
&changeset->remove,
&changeset->remove_count,
&changeset->remove_allocated,
removed_copy);
if (ret != KNOT_EOK) {
knot_rrset_deep_free(&removed_copy,
1, 1);
dbg_ddns("Failed to add removed CNAME "
"to changeset: %s\n",
knot_strerror(ret));
free(from_chgset);
return ret;
}
}
free(from_chgset);
} else if (knot_node_rrset_count(node) != 0) {
/* 2) Other occupied node => ignore. */
return 1;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_process_add_soa(knot_node_t *node,
const knot_rrset_t *rr,
knot_changes_t *changes)
{
assert(node != NULL);
assert(rr != NULL);
assert(changes != NULL);
dbg_ddns_detail("Adding SOA RR.\n");
int ret = 0;
/*
* Just remove the SOA from the node, together with its RRSIGs.
* Adding the RR is done in the caller function. Note that only SOA
* with larger SERIAL than the current one will get to these functions,
* so we don't have to check the SERIALS again. But an assert won't
* hurt.
*/
/* Get the current SOA RR from the node. */
knot_rrset_t *removed = knot_node_get_rrset(node, KNOT_RRTYPE_SOA);
if (removed != NULL) {
dbg_ddns_detail("Found SOA in the node.\n");
/* If they are identical, ignore. */
if (knot_rrset_equal(removed, rr, KNOT_RRSET_COMPARE_WHOLE)
== 1) {
dbg_ddns_detail("Old and new SOA identical.\n");
return 1;
}
/* Check that the serial is indeed larger than the current one*/
assert(ns_serial_compare(knot_rrset_rdata_soa_serial(removed),
knot_rrset_rdata_soa_serial(rr)) < 0);
/* 1) Store it to 'changes', together with its RRSIGs. */
ret = knot_changes_add_old_rrsets(
&removed, 1, changes, 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add removed RRSet to "
"'changes': %s\n", knot_strerror(ret));
return ret;
}
/* 2) And remove it from the node. */
UNUSED(knot_node_remove_rrset(node, KNOT_RRTYPE_SOA));
/* No changeset processing needed in this case. */
} else {
dbg_ddns_detail("No SOA in node, ignoring.\n");
/* If there is no SOA in the node, ignore. */
return 1;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_rr_new_normal(knot_node_t *node, knot_rrset_t *rr_copy,
knot_changes_t *changes)
{
assert(node != NULL);
assert(rr_copy != NULL);
assert(changes != NULL);
dbg_ddns_verb("Adding normal RR.\n");
/* Add the RRSet to 'changes'. */
int ret = knot_changes_add_new_rrsets(&rr_copy, 1, changes, 1);
if (ret != KNOT_EOK) {
knot_rrset_deep_free(&rr_copy, 1, 1);
dbg_ddns("Failed to store copy of the added RR: "
"%s\n", knot_strerror(ret));
return ret;
}
/* Add the RRSet to the node. */
ret = knot_node_add_rrset_no_merge(node, rr_copy);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add RR to node: %s\n", knot_strerror(ret));
return ret;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_rr_new_rrsig(knot_node_t *node, knot_rrset_t *rr_copy,
knot_changes_t *changes,
uint16_t type_covered)
{
assert(node != NULL);
assert(rr_copy != NULL);
assert(changes != NULL);
dbg_ddns_verb("Adding RRSIG RR.\n");
/* Create RRSet to be covered by the RRSIG. */
knot_rrset_t *covered_rrset = knot_rrset_new(
knot_rrset_get_owner(rr_copy), type_covered,
knot_rrset_class(rr_copy),
knot_rrset_ttl(rr_copy));
if (covered_rrset == NULL) {
dbg_ddns("Failed to create RRSet to be covered"
" by the UPDATE RRSIG RR.\n");
knot_rrset_deep_free(&rr_copy, 1, 1);
return KNOT_ENOMEM;
}
/* Add the RRSet to the node. */
int ret = knot_node_add_rrset_no_merge(node, covered_rrset);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add the RRSet to be covered to the node: %s"
".\n", knot_strerror(ret));
knot_rrset_deep_free(&rr_copy, 1, 1);
knot_rrset_deep_free(&covered_rrset, 1, 1);
return KNOT_ENOMEM;
}
/* Add the RRSet to 'changes'. */
ret = knot_changes_add_new_rrsets(&covered_rrset, 1, changes, 0);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add new RRSet (covered) to list: %s.\n",
knot_strerror(ret));
knot_rrset_deep_free(&rr_copy, 1, 1);
knot_rrset_deep_free(&covered_rrset, 1, 1);
return ret;
}
/* Add the RRSIG RRSet to 'changes'. */
ret = knot_changes_add_new_rrsets(&rr_copy, 1, changes, 1);
if (ret != KNOT_EOK) {
knot_rrset_deep_free(&rr_copy, 1, 1);
dbg_ddns("Failed to store copy of the added RRSIG: %s\n",
knot_strerror(ret));
return ret;
}
/* Add the RRSIG RRSet to the covered RRSet. */
ret = knot_rrset_add_rrsigs(covered_rrset, rr_copy,
KNOT_RRSET_DUPL_SKIP);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add RRSIG RR to the covered RRSet.\n");
return ret;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_rr_merge_normal(knot_rrset_t *node_rrset_copy,
knot_rrset_t **rr_copy)
{
assert(node_rrset_copy != NULL);
assert(rr_copy != NULL);
assert(*rr_copy != NULL);
dbg_ddns_verb("Merging normal RR to existing RRSet.\n");
/* In case the RRSet is empty (and only remained there because
* of the RRSIGs) it may happen that the TTL may be different
* than that of he new RRs. Update the TTL according to the
* first RR.
*/
if (knot_rrset_rdata_rr_count(node_rrset_copy) == 0
&& knot_rrset_ttl(node_rrset_copy)
!= knot_rrset_ttl(*rr_copy)) {
knot_rrset_set_ttl(node_rrset_copy,
knot_rrset_ttl(*rr_copy));
}
int rdata_in_copy = knot_rrset_rdata_rr_count(*rr_copy);
int merged, deleted_rrs;
int ret = knot_rrset_merge_no_dupl(node_rrset_copy, *rr_copy, &merged,
&deleted_rrs);
dbg_ddns_detail("Merge returned: %d\n", ret);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to merge UPDATE RR to node RRSet: %s."
"\n", knot_strerror(ret));
return ret;
}
knot_rrset_deep_free(rr_copy, 1, 0);
if (rdata_in_copy == deleted_rrs) {
/* All RDATA have been removed, because they were duplicates
* or there were none (0). In general this means, that no
* change was made.
*/
return 1;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_rr_merge_rrsig(knot_rrset_t *node_rrset_copy,
knot_rrset_t **rr_copy,
knot_changes_t *changes)
{
assert(node_rrset_copy != NULL);
assert(rr_copy != NULL);
assert(*rr_copy != NULL);
assert(changes != NULL);
dbg_ddns_verb("Adding RRSIG RR to existing RRSet.\n");
knot_rrset_t *rrsigs_old = knot_rrset_get_rrsigs(node_rrset_copy);
int ret = 0;
if (rrsigs_old != NULL) {
/* If there is an RRSIG RRSet already, copy it too. */
knot_rrset_t *rrsigs_copy = NULL;
ret = xfrin_copy_old_rrset(rrsigs_old, &rrsigs_copy,
changes, 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to copy RRSIG RRSet: "
"%s\n", knot_strerror(ret));
return ret;
}
/* Replace the RRSIGs by the copy. */
ret = knot_rrset_set_rrsigs(node_rrset_copy, rrsigs_copy);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to replace RRSIGs in "
"the RRSet: %s\n",
knot_strerror(ret));
return ret;
}
/* Merge the UPDATE RR to the copied RRSIG
* RRSet.
*/
dbg_ddns_detail("Merging RRSIG to the one in the RRSet.\n");
int rdata_in_copy = knot_rrset_rdata_rr_count(*rr_copy);
int merged, deleted_rrs;
ret = knot_rrset_merge_no_dupl(rrsigs_copy, *rr_copy, &merged,
&deleted_rrs);
if (ret < 0) {
dbg_xfrin("Failed to merge UPDATE RRSIG to copy: %s.\n",
knot_strerror(ret));
return KNOT_ERROR;
}
knot_rrset_deep_free(rr_copy, 1, 0);
if (rdata_in_copy == deleted_rrs) {
/* All RDATA have been removed, because they were
* duplicates or there were none (0). In general this
* means, that no change was made.
*/
return 1;
}
} else {
/* If there is no RRSIG RRSet yet, just add the
* UPDATE RR to the copied covered RRSet.
*/
/* Add the RRSet to 'changes'. */
ret = knot_changes_add_new_rrsets(rr_copy, 1, changes, 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to store copy of the added RR: %s\n",
knot_strerror(ret));
return ret;
}
/* Add the RRSet to the covered RRSet. */
ret = knot_rrset_add_rrsigs(node_rrset_copy, *rr_copy,
KNOT_RRSET_DUPL_SKIP);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add RRSIG RR to the"
" covered RRSet.\n");
return ret;
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
/*!
* \todo We should check, how it's possible that IXFR is not leaking due to the
* same issue with merge. Or maybe it is, we should try it!!
*/
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_rr(knot_node_t *node, const knot_rrset_t *rr,
knot_changes_t *changes, knot_rrset_t **rr_copy)
{
assert(node != NULL);
assert(rr != NULL);
assert(changes != NULL);
assert(rr_copy != NULL);
/* Copy the RRSet from the packet. */
//knot_rrset_t *rr_copy;
int ret = knot_rrset_deep_copy(rr, rr_copy, 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to copy RR: %s\n", knot_strerror(ret));
return ret;
}
uint16_t type = knot_rrset_type(rr);
uint16_t type_covered = (type == KNOT_RRTYPE_RRSIG)
? knot_rrset_rdata_rrsig_type_covered(rr)
: type;
/* If the RR belongs to a RRSet already present in the node, we must
* take this RRSet from the node, copy it, and merge this RR into it.
*
* This code is more or less copied from xfr-in.c.
*/
knot_rrset_t *node_rrset_copy = NULL;
ret = xfrin_copy_rrset(node, type_covered, &node_rrset_copy, changes,
0);
if (node_rrset_copy == NULL) {
/* No such RRSet in the node. Add the whole UPDATE RRSet. */
dbg_ddns_detail("Adding whole UPDATE RR to the zone.\n");
if (type_covered != type) {
/* Adding RRSIG. */
ret = knot_ddns_add_rr_new_rrsig(node, *rr_copy,
changes, type_covered);
} else {
ret = knot_ddns_add_rr_new_normal(node, *rr_copy,
changes);
}
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add new RR to node.\n");
return ret;
}
dbg_ddns_detail("RRSet added successfully.\n");
} else {
/* We have copied the RRSet from the node. */
dbg_ddns_exec_detail(
dbg_ddns_detail("Merging RR to an existing RRSet.\n");
knot_rrset_dump(node_rrset_copy);
dbg_ddns_detail("New RR:\n");
knot_rrset_dump(*rr_copy);
);
if (type_covered != type) {
/* Adding RRSIG. */
ret = knot_ddns_add_rr_merge_rrsig(node_rrset_copy,
rr_copy, changes);
} else {
ret = knot_ddns_add_rr_merge_normal(node_rrset_copy,
rr_copy);
}
dbg_ddns_exec_detail(
dbg_ddns_detail("After merge:\n");
knot_rrset_dump(node_rrset_copy);
);
if (ret < KNOT_EOK) {
dbg_ddns("Failed to merge UPDATE RR to node RRSet.\n");
knot_rrset_deep_free(rr_copy, 1, 1);
knot_rrset_deep_free(&node_rrset_copy, 1, 1);
return ret;
}
// save the new RRSet together with the new RDATA to 'changes'
// do not overwrite 'ret', it have to be returned
int r = knot_changes_add_new_rrsets(&node_rrset_copy, 1,
changes, 1);
if (r != KNOT_EOK) {
dbg_ddns("Failed to store RRSet copy to 'changes'\n");
knot_rrset_deep_free(&node_rrset_copy, 1, 1);
return r;
}
}
assert(ret >= 0);
return ret;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_final_soa_to_chgset(const knot_rrset_t *soa,
knot_changeset_t *changeset)
{
assert(soa != NULL);
assert(changeset != NULL);
knot_rrset_t *soa_copy = NULL;
int ret = knot_rrset_deep_copy(soa, &soa_copy, 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to copy SOA RR to the changeset: "
"%s\n", knot_strerror(ret));
return ret;
}
knot_changeset_store_soa(&changeset->soa_to,
&changeset->serial_to,
soa_copy);
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_add_rr_to_chgset(const knot_rrset_t *rr,
knot_changeset_t *changeset)
{
assert(rr != NULL);
assert(changeset != NULL);
int ret = 0;
knot_rrset_t *chgset_rr = NULL;
knot_ddns_check_add_rr(changeset, rr, &chgset_rr);
if (chgset_rr == NULL) {
ret = knot_rrset_deep_copy(rr, &chgset_rr, 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to copy RR to the changeset: "
"%s\n", knot_strerror(ret));
return ret;
}
/* No such RR in the changeset, add it. */
ret = knot_changeset_add_rrset(&changeset->add,
&changeset->add_count,
&changeset->add_allocated,
chgset_rr);
if (ret != KNOT_EOK) {
knot_rrset_deep_free(&chgset_rr, 1, 1);
dbg_ddns("Failed to add RR to changeset: %s.\n",
knot_strerror(ret));
return ret;
}
} else {
knot_rrset_deep_free(&chgset_rr, 1, 1);
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_process_add(const knot_rrset_t *rr,
knot_node_t *node,
knot_zone_contents_t *zone,
knot_changeset_t *changeset,
knot_changes_t *changes,
knot_rrset_t **rr_copy)
{
assert(rr != NULL);
assert(zone != NULL);
assert(changeset != NULL);
assert(changes != NULL);
assert(rr_copy != NULL);
dbg_ddns_verb("Adding RR.\n");
if (node == NULL) {
// create new node, connect it properly to the
// zone nodes
dbg_ddns_detail("Node not found. Creating new.\n");
node = knot_ddns_add_new_node(zone, knot_rrset_get_owner(rr),
knot_ddns_rr_is_nsec3(rr));
if (node == NULL) {
dbg_xfrin("Failed to create new node in zone.\n");
return KNOT_ERROR;
}
}
uint16_t type = knot_rrset_type(rr);
*rr_copy = NULL;
int ret = 0;
/*
* First, rule out special cases: CNAME, SOA and adding to CNAME node.
*/
if (type == KNOT_RRTYPE_CNAME) {
/* 1) CNAME */
ret = knot_ddns_process_add_cname(node, rr, changeset, changes);
} else if (type == KNOT_RRTYPE_SOA) {
/* 2) SOA */
ret = knot_ddns_process_add_soa(node, rr, changes);
} else if (knot_node_rrset(node, KNOT_RRTYPE_CNAME) != NULL) {
/*
* Adding RR to CNAME node. Ignore the UPDATE RR.
*
* TODO: This may or may not be according to the RFC, it's quite
* unclear (see 3.4.2.2)
*/
return KNOT_EOK;
}
if (ret == 1) {
dbg_ddns_detail("Ignoring the added RR.\n");
// Ignore
return KNOT_EOK;
} else if (ret != KNOT_EOK) {
dbg_ddns_detail("Adding RR failed.\n");
return ret;
}
/*
* In all other cases, the RR should just be added to the node.
*/
/* Add the RRSet to the node (RRSIGs handled in the function). */
dbg_ddns_detail("Adding RR to the node.\n");
ret = knot_ddns_add_rr(node, rr, changes, rr_copy);
if (ret < 0) {
dbg_ddns("Failed to add RR to the node.\n");
return ret;
}
/*
* If adding SOA, it should not be stored in the changeset.
* (This is done in the calling function, and the SOA is stored in the
* soa_final field.)
*/
if (type == KNOT_RRTYPE_SOA) {
return KNOT_EOK;
}
/* Add the RR to ADD section of the changeset. */
/* If the RR was previously removed, do not add it to the
* changeset, and remove the entry from the REMOVE section.
*
* If there was no change (i.e. all RDATA were duplicates), do not add
* the RR to the changeset.
*/
if (ret == KNOT_EOK) {
dbg_ddns_detail("Adding RR to the changeset.\n");
ret = knot_ddns_add_rr_to_chgset(rr, changeset);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add the UPDATE RR to the changeset."
"\n");
return ret;
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
/*!
* \todo Geez, this is soooooo long even I don't exactly know what it does...
* Refactor!
*/
static int knot_ddns_process_rem_rr(const knot_rrset_t *rr,
knot_node_t *node,
knot_zone_contents_t *zone,
knot_changeset_t *changeset,
knot_changes_t *changes, uint16_t qclass)
{
assert(rr != NULL);
assert(node != NULL);
assert(zone != NULL);
assert(changeset != NULL);
assert(changes != NULL);
uint16_t type = knot_rrset_type(rr);
dbg_ddns_verb("Removing one RR.\n");
/*
* When doing changes to RRSets, we must:
* 1) Copy the RRSet (same as in IXFR changeset applying, maybe the
* function xfrin_copy_rrset() may be used for this).
* 2) Remove the RDATA (in this case only one). Check if it is not the
* last NS RR in the zone.
* 3) Store the removed RDATA in 'changes'.
* 4) If the RRSet is empty, remove it and store in 'changes'.
* 5) Check redundant RRs in changeset.
* 6) Store the RRSet containing the one RDATA in the changeset. We may
* use the RRSet from the packet for this - copy it, set CLASS
* and TTL.
*
* Special handling of RRSIGs is required in that the RRSet containing
* them must be copied as well. However, copying of RRSet copies also
* the RRSIGs, so copying the base RRSet is enough for both cases!
*/
assert(type != KNOT_RRTYPE_SOA);
int is_apex = knot_node_rrset(node, KNOT_RRTYPE_SOA) != NULL;
/* If removing NS from an apex and there is only one NS left, ignore
* this removal right away. We do not have to check if the RRs match:
* - if they don't match, the removal will be ignored
* - if they match, the last NS cannot be removed anyway.
*/
if (is_apex && type == KNOT_RRTYPE_NS
&& knot_rrset_rdata_rr_count(knot_node_rrset(node, type)) == 1) {
return KNOT_EOK;
}
/*
* 1) Copy the RRSet.
*/
uint16_t type_to_copy = (type != KNOT_RRTYPE_RRSIG) ? type
: knot_rrset_rdata_rrsig_type_covered(rr);
knot_rrset_t *rrset_copy = NULL;
int ret = xfrin_copy_rrset(node, type_to_copy, &rrset_copy, changes, 1);
if (ret < 0) {
dbg_ddns("Failed to copy RRSet for removal: %s\n",
knot_strerror(ret));
return ret;
}
if (rrset_copy == NULL) {
dbg_ddns_verb("RRSet not found.\n");
return KNOT_EOK;
}
/*
* Set some variables needed, according to the modified RR type.
*/
int rdata_count = 0;
knot_rrset_t *to_modify;
if (type == KNOT_RRTYPE_RRSIG) {
rdata_count = knot_rrset_rdata_rr_count(knot_rrset_rrsigs(rrset_copy));
to_modify = knot_rrset_get_rrsigs(rrset_copy);
} else {
rdata_count = knot_rrset_rdata_rr_count(rrset_copy);
to_modify = rrset_copy;
}
/*
* 1.5) Prepare place for the removed RDATA.
* We don't know if there are some, but if this fails, at least we
* haven't removed them yet.
*/
ret = knot_changes_rdata_reserve(&changes->old_rdata,
changes->old_rdata_count,
&changes->old_rdata_allocated,
rdata_count);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to reserve place for RDATA.\n");
return ret;
}
/*
* 2) Remove the proper RDATA from the RRSet copy, or its RRSIGs.
*/
knot_rrset_t *rr_remove = NULL;
ret = knot_rrset_remove_rr_using_rrset(to_modify, rr, &rr_remove, 0);
if (ret != KNOT_EOK) {
dbg_ddns("ddns: proces_rem_rr: Could not remove RDATA from"
" RRSet (%s).\n", knot_strerror(ret));
return ret;
}
/* No such RR in the RRSet. */
if (knot_rrset_rdata_rr_count(rr_remove) == 0) {
knot_rrset_free(&rr_remove);
dbg_ddns_detail("No such RR found to be removed.\n");
return KNOT_EOK;
}
/* If we removed NS from apex, there should be at least one more. */
assert(!is_apex || type != KNOT_RRTYPE_NS
|| knot_rrset_rdata_rr_count(rrset_copy));
/*
* 3) Store the removed RDATA in 'changes'.
*/
knot_changes_add_rdata(changes->old_rdata, &changes->old_rdata_count,
rr_remove);
/*
* 4) If the RRSet is empty, remove it and store in 'changes'.
* Do this also if the RRSIGs are empty.
* And if both are empty, remove both.
* RRSIG handling first,
*/
if (type == KNOT_RRTYPE_RRSIG &&
knot_rrset_rdata_rr_count(to_modify) == 0) {
/* Empty RRSIGs, remove the RRSIG RRSet */
ret = knot_changes_rrsets_reserve(&changes->old_rrsets,
&changes->old_rrsets_count,
&changes->old_rrsets_allocated,
1);
if (ret == KNOT_EOK) {
knot_rrset_t *rrsig = knot_rrset_get_rrsigs(rrset_copy);
dbg_xfrin_detail("Removed RRSIG RRSet (%p).\n", rrsig);
assert(rrsig && rrsig == to_modify);
// add the removed RRSet to list of old RRSets
changes->old_rrsets[changes->old_rrsets_count++]
= rrsig;
// remove it from the RRSet
knot_rrset_set_rrsigs(rrset_copy, NULL);
} else {
dbg_ddns("Failed to reserve space for empty RRSet.\n");
}
}
// Remove empty RRSet from node and store to changeset
if (type != KNOT_RRTYPE_RRSIG &&
knot_rrset_rdata_rr_count(to_modify) == 0) {
// The RRSet should not be empty if we were removing NSs from
// apex in case of DDNS
assert(!is_apex);
ret = knot_changes_rrsets_reserve(&changes->old_rrsets,
&changes->old_rrsets_count,
&changes->old_rrsets_allocated,
1);
if (ret == KNOT_EOK) {
knot_rrset_t *tmp = knot_node_remove_rrset(node, type);
dbg_xfrin_detail("Removed whole RRSet (%p).\n", tmp);
assert(tmp == rrset_copy);
// add the removed RRSet to list of old RRSets
changes->old_rrsets[changes->old_rrsets_count++]
= rrset_copy;
} else {
dbg_ddns("Failed to reserve space for empty RRSet.\n");
}
}
/*
* 5) Check if the RR is not in the ADD section. If yes, remove it
* from there and do not add it to the REMOVE section.
*/
knot_rrset_t **from_chgset = NULL;
size_t from_chgset_count = 0;
ret = knot_ddns_check_remove_rr2(changeset, knot_node_owner(node),
rr, &from_chgset, &from_chgset_count);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to remove possible redundant RRs from ADD "
"section: %s.\n", knot_strerror(ret));
free(from_chgset);
return ret;
}
assert(from_chgset_count <= 1);
if (from_chgset_count == 1) {
/* Just delete the RRSet. */
knot_rrset_deep_free(&(from_chgset[0]), 1, 1);
/* Finish processing, no adding to changeset. */
free(from_chgset);
return KNOT_EOK;
}
free(from_chgset);
/*
* 6) Store the RRSet containing the one RDATA in the changeset. We may
* use the RRSet from the packet for this - copy it, set CLASS
* and TTL.
*/
knot_rrset_t *to_chgset = NULL;
ret = knot_rrset_deep_copy(rr, &to_chgset, 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to copy RRSet from packet to changeset.\n");
return ret;
}
knot_rrset_set_class(to_chgset, qclass);
knot_rrset_set_ttl(to_chgset, knot_rrset_ttl(to_modify));
ret = knot_changeset_add_rrset(&changeset->remove,
&changeset->remove_count,
&changeset->remove_allocated,
to_chgset);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to store the RRSet copy to changeset: %s.\n",
knot_strerror(ret));
knot_rrset_deep_free(&to_chgset, 1, 1);
return ret;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_process_rem_rrsig(knot_node_t *node,
knot_rrset_t *rrset,
knot_changes_t *changes,
knot_rrset_t **rrsig)
{
assert(node != NULL);
assert(rrset != NULL);
assert(changes != NULL);
knot_rrset_t *rrset_copy = NULL;
/* Copy RRSet. */
int ret = xfrin_copy_old_rrset(rrset, &rrset_copy, changes, 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to copy RRSet from node: %s.\n",
knot_strerror(ret));
return ret;
}
/* Remove RRSIGs from the copy. */
*rrsig = knot_rrset_get_rrsigs(rrset_copy);
if (*rrsig != NULL) {
knot_rrset_set_rrsigs(rrset_copy, NULL);
}
/* Put the copy to the node. */
ret = knot_node_add_rrset_no_merge(node, rrset_copy);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add RRSet copy to the node: %s\n",
knot_strerror(ret));
knot_rrset_deep_free(&rrset_copy, 1, 1);
return ret;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_process_rem_rrsigs(knot_node_t *node,
knot_changes_t *changes,
knot_rrset_t ***removed,
size_t *removed_count)
{
assert(node != NULL);
assert(removed != NULL);
assert(removed_count != NULL);
assert(changes != NULL);
/* If removing RRSIGs, we must remove them from all RRSets in
* the node. This means to copy all RRSets and then remove the
* RRSIGs from them.
*/
dbg_ddns_verb("Removing all RRSIGs from node.\n");
knot_rrset_t **rrsets = knot_node_get_rrsets(node);
if (rrsets == NULL) {
// No RRSets in the node, nothing to remove
return KNOT_EOK;
}
/* Allocate space for the removed RRSIGs. There may be as many as there
* are RRSets.
*/
short rrset_count = knot_node_rrset_count(node);
*removed = malloc(rrset_count * sizeof(knot_rrset_t *));
if (*removed == NULL) {
ERR_ALLOC_FAILED;
free(rrsets);
return KNOT_ENOMEM;
}
*removed_count = 0;
/* Remove all the RRSets from the node, so that we may insert the copies
* right away.
*/
knot_node_remove_all_rrsets(node);
knot_rrset_t *rrsig = NULL;
int ret = 0;
for (int i = 0; i < rrset_count; ++i) {
ret = knot_ddns_process_rem_rrsig(node, rrsets[i], changes,
&rrsig);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to remove RRSIG.\n");
free(rrsets);
free(*removed);
*removed = NULL;
return ret;
}
/* Store the RRSIGs to the array of removed RRSets. */
(*removed)[(*removed_count)++] = rrsig;
}
free(rrsets);
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_process_rem_rrset(const knot_rrset_t *rrset,
knot_node_t *node,
knot_changeset_t *changeset,
knot_changes_t *changes)
{
assert(node != NULL);
assert(rrset != NULL);
assert(changeset != NULL);
assert(changes != NULL);
uint16_t type = knot_rrset_type(rrset);
/*! \note
* We decided to automatically remove RRSIGs together with the removed
* RRSet as they are no longer valid or required anyway.
*
* Also refer to RFC3007, section 4.3:
* 'When the contents of an RRset are updated, the server MAY delete
* all associated SIG records, since they will no longer be valid.'
*
* (Although we are compliant with this RFC only selectively. The next
* section says: 'If any changes are made, the server MUST, if
* necessary, generate a new SOA record and new NXT records, and sign
* these with the appropriate zone keys.' and we are definitely not
* doing this...
*
* \todo Document!!
*/
// this should be ruled out before
assert(type != KNOT_RRTYPE_SOA);
if (knot_node_rrset(node, KNOT_RRTYPE_SOA) != NULL
&& type == KNOT_RRTYPE_NS) {
// if removing NS from apex, ignore
return KNOT_EOK;
}
knot_rrset_t **removed = NULL;
size_t removed_count = 0;
int ret = 0;
if (type == KNOT_RRTYPE_RRSIG) {
/* Remove all RRSIGs from the node. */
ret = knot_ddns_process_rem_rrsigs(node, changes, &removed,
&removed_count);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to remove RRSIGs. (%s)\n",
knot_strerror(ret));
return ret;
}
} else {
/* Remove the RRSet from the node. */
removed = malloc(sizeof(knot_rrset_t *));
if (!removed) {
ERR_ALLOC_FAILED;
return KNOT_ENOMEM;
}
dbg_ddns_detail("Removing RRSet of type: %d\n", type);
*removed = knot_node_remove_rrset(node, type);
if (*removed != NULL) {
removed_count = 1;
} else {
removed_count = 0;
}
}
dbg_ddns_detail("Removed: %p (first item: %p), removed count: %zu\n",
removed, (removed == NULL) ? (void *)"none" : *removed,
removed_count);
// no such RR
if (removed_count == 0) {
// ignore
free(removed);
return KNOT_EOK;
}
/* 2) Store them to 'changes' for later deallocation, together with
* their RRSIGs.
*/
ret = knot_changes_add_old_rrsets(removed, removed_count, changes, 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to add removed RRSet to 'changes': %s.\n",
knot_strerror(ret));
free(removed);
return ret;
}
/* 3) Copy the RRSets, so that they can be stored to the changeset. */
knot_rrset_t **to_chgset = malloc(removed_count
* sizeof(knot_rrset_t *));
if (to_chgset == NULL) {
dbg_ddns("Failed to allocate space for RRSets going to "
"changeset.\n");
free(removed);
return KNOT_ENOMEM;
}
for (int i = 0; i < removed_count; ++i) {
ret = knot_rrset_deep_copy(removed[i], &to_chgset[i], 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to copy the removed RRSet: %s.\n",
knot_strerror(ret));
for (int j = 0; j < i; ++j) {
knot_rrset_deep_free(&to_chgset[j], 1, 1);
}
free(to_chgset);
free(removed);
return ret;
}
}
free(removed);
/* 4) But we must check if some of the RRs were not previously added
* by the same UPDATE. If yes, these must be removed from the ADD
* section of the changeset and also from this RRSet copy (so they
* are neither stored in the REMOVE section of the changeset).
*/
knot_rrset_t **from_chgset = NULL;
size_t from_chgset_count = 0;
/* 4 a) Remove redundant RRs from the ADD section of the changeset. */
knot_rrset_t *empty_rrset =
knot_rrset_new(rrset->owner, type, rrset->rclass, rrset->ttl);
if (empty_rrset == NULL) {
return KNOT_ENOMEM;
}
ret = knot_ddns_check_remove_rr2(changeset, knot_node_owner(node),
empty_rrset, &from_chgset,
&from_chgset_count);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to remove possible redundant RRs from ADD "
"section: %s.\n", knot_strerror(ret));
for (int i = 0; i < removed_count; ++i) {
knot_rrset_deep_free(&to_chgset[i], 1, 1);
}
free(from_chgset);
free(to_chgset);
knot_rrset_free(&empty_rrset);
return ret;
}
knot_rrset_free(&empty_rrset);
/* 4 b) Remove these RRs from the copy of the RRSets removed from zone*/
for (int j = 0; j < removed_count; ++j) {
/* In each RRSet removed from the node (each can have more
* RDATAs) ...
*/
for (int i = 0; i < from_chgset_count; ++i) {
/* ...try to remove redundant RDATA. Each RRSet in
* 'from_chgset' contains only one RDATA.
*/
ret = knot_rrset_remove_rr_using_rrset_del(to_chgset[j],
from_chgset[i]);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to remove RR from RRSet"
"(%s).\n", knot_strerror(ret));
free(from_chgset);
free(to_chgset);
return ret;
}
}
}
/* The array is cleared, we may delete the redundant RRs. */
for (int i = 0; i < from_chgset_count; ++i) {
knot_rrset_deep_free(&from_chgset[i], 1, 1);
}
free(from_chgset);
/* 5) Store the remaining RRSet to the changeset. Do not try to merge
* to some previous RRSet, there should be none.
*/
for (int i = 0; i < removed_count; ++i) {
ret = knot_changeset_add_rrset(&changeset->remove,
&changeset->remove_count,
&changeset->remove_allocated,
to_chgset[i]);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to store the RRSet copy to changeset: "
"%s.\n", knot_strerror(ret));
for (int j = i; j < removed_count; ++j) {
knot_rrset_deep_free(&to_chgset[j], 1, 1);
}
free(to_chgset);
return ret;
}
}
free(to_chgset);
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_process_rem_all(knot_node_t *node,
knot_changeset_t *changeset,
knot_changes_t *changes)
{
assert(node != NULL);
assert(changeset != NULL);
assert(changes != NULL);
/*! \note
* This basically means to call knot_ddns_process_rem_rrset() for every
* type present in the node.
*
* In case of SOA and NS in apex, the RRSets should not be removed, but
* what about their RRSIGs??
*
* If the zone has to remain properly signed, the UPDATE will have to
* contain at least new SOA and RRSIGs for it (as the auto-incremented
* SOA would not be signed). So it should not matter if we leave the
* RRSIGs there or not. But in case of the NSs it's not that clear.
*
* For now, we will leave the RRSIGs there. It's easier to implement.
*
* \todo Should document this!!
*/
int ret = 0;
knot_rrset_t **rrsets = knot_node_get_rrsets(node);
int count = knot_node_rrset_count(node);
if (rrsets == NULL && count != 0) {
dbg_ddns("Failed to fetch RRSets from node.\n");
return KNOT_ENOMEM;
}
int is_apex = knot_node_rrset(node, KNOT_RRTYPE_SOA) != NULL;
dbg_ddns_verb("Removing all RRSets (count: %d).\n", count);
for (int i = 0; i < count; ++i) {
// If the node is apex, skip NS and SOA
if (is_apex &&
(knot_rrset_type(rrsets[i]) == KNOT_RRTYPE_SOA
|| knot_rrset_type(rrsets[i]) == KNOT_RRTYPE_NS)) {
/* Do not remove these RRSets, nor their RRSIGs. */
continue;
}
ret = knot_ddns_process_rem_rrset(rrsets[i], node, changeset,
changes);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to remove RRSet: %s\n",
knot_strerror(ret));
free(rrsets);
return ret;
}
}
free(rrsets);
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int knot_ddns_process_rr(const knot_rrset_t *rr,
knot_zone_contents_t *zone,
knot_changeset_t *changeset,
knot_changes_t *changes, uint16_t qclass,
knot_rrset_t **rr_copy)
{
assert(rr != NULL);
assert(zone != NULL);
assert(changeset != NULL);
assert(changes != NULL);
assert(rr_copy != NULL);
/* 1) Find node that will be affected. */
knot_node_t *node = knot_ddns_get_node(zone, rr);
/* 2) Decide what to do. */
if (knot_rrset_class(rr) == knot_zone_contents_class(zone)) {
return knot_ddns_process_add(rr, node, zone, changeset,
changes, rr_copy);
} else if (node == NULL) {
// Removing from non-existing node, just ignore the entry
return KNOT_EOK;
} else if (knot_rrset_class(rr) == KNOT_CLASS_NONE) {
return knot_ddns_process_rem_rr(rr, node, zone, changeset,
changes, qclass);
} else if (knot_rrset_class(rr) == KNOT_CLASS_ANY) {
if (knot_rrset_type(rr) == KNOT_RRTYPE_ANY) {
return knot_ddns_process_rem_all(node, changeset,
changes);
} else {
return knot_ddns_process_rem_rrset(rr, node, changeset,
changes);
}
} else {
assert(0);
return KNOT_ERROR;
}
}
/*----------------------------------------------------------------------------*/
/*
* NOTES:
* - 'zone' must be a copy of the current zone.
* - changeset must be allocated
* - changes must be allocated
*
* All this is done in the first parts of xfrin_apply_changesets() - extract
* to separate function, if possible.
*
* If anything fails, rollback must be done. The xfrin_rollback_update() may
* be good for this.
*/
int knot_ddns_process_update2(knot_zone_contents_t *zone,
const knot_packet_t *query,
knot_changeset_t *changeset,
knot_changes_t *changes,
knot_rcode_t *rcode)
{
if (zone == NULL || query == NULL || changeset == NULL || rcode == NULL
|| changes == NULL) {
return KNOT_EINVAL;
}
int ret = KNOT_EOK;
/* Copy base SOA RR. */
const knot_rrset_t *soa = knot_node_rrset(knot_zone_contents_apex(zone),
KNOT_RRTYPE_SOA);
knot_rrset_t *soa_begin = NULL;
knot_rrset_t *soa_end = NULL;
ret = knot_rrset_deep_copy(soa, &soa_begin, 1);
if (ret == KNOT_EOK) {
knot_changeset_store_soa(&changeset->soa_from,
&changeset->serial_from, soa_begin);
} else {
*rcode = KNOT_RCODE_SERVFAIL;
return ret;
}
/* Current SERIAL */
int64_t sn = knot_rrset_rdata_soa_serial(soa_begin);
int64_t sn_new;
/* Incremented SERIAL
* We must set it now to be able to compare SERIAL from SOAs in the
* UPDATE to it. Although we do not have the new SOA yet.
*/
if (sn > -1) {
sn_new = (uint32_t)sn + 1;
} else {
*rcode = KNOT_RCODE_SERVFAIL;
return ret;
}
/* Process all RRs the Authority (Update) section. */
const knot_rrset_t *rr = NULL;
knot_rrset_t *rr_copy = NULL;
dbg_ddns("Processing UPDATE section.\n");
for (int i = 0; i < knot_packet_authority_rrset_count(query); ++i) {
rr = knot_packet_authority_rrset(query, i);
/* Check if the entry is correct. */
ret = knot_ddns_check_update(rr, query, rcode);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to check update RRSet:%s\n",
knot_strerror(ret));
return ret;
}
/* Check if the record is SOA. If yes, check the SERIAL.
* If this record should cause the SOA to be replaced in the
* zone, use it as the ending SOA.
*
* Also handle cases where there are multiple SOAs to be added
* in the same UPDATE. The one with the largest SERIAL should
* be used.
*
* TODO: If there are more SOAs in the UPDATE one after another,
* the ddns_add_update() function will merge them into a
* RRSet. This should be handled somehow.
*
* If the serial is not larger than the current zone serial,
* ignore the record and continue. This will ensure that the
* RR processing function receives only SOA RRs that should be
* added to the zone (replacing the old one).
*/
if (knot_rrset_type(rr) == KNOT_RRTYPE_SOA
&& (knot_rrset_class(rr) == KNOT_CLASS_NONE
|| knot_rrset_class(rr) == KNOT_CLASS_ANY
|| ns_serial_compare(knot_rrset_rdata_soa_serial(rr),
sn_new) < 0)) {
// This ignores also SOA removals
dbg_ddns_verb("Ignoring SOA...\n");
continue;
}
dbg_ddns_verb("Processing RR %p...\n", rr);
ret = knot_ddns_process_rr(rr, zone, changeset, changes,
knot_packet_qclass(query),
&rr_copy);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to process update RR:%s\n",
knot_strerror(ret));
*rcode = (ret == KNOT_EMALF) ? KNOT_RCODE_FORMERR
: KNOT_RCODE_SERVFAIL;
return ret;
}
// we need the RR copy, that's why this code is here
if (knot_rrset_type(rr) == KNOT_RRTYPE_SOA) {
int64_t sn_rr = knot_rrset_rdata_soa_serial(rr);
dbg_ddns_verb("Replacing SOA. Old serial: %"PRId64", "
"new serial: %"PRId64"\n", sn_new, sn_rr);
assert(ns_serial_compare(sn_rr, sn_new) >= 0);
assert(rr_copy != NULL);
sn_new = sn_rr;
soa_end = (knot_rrset_t *)rr_copy;
}
}
/* Ending SOA (not in the UPDATE) */
if (soa_end == NULL) {
/* If the changeset is empty, do not process anything further
* and indicate this to the caller, so that the changeset is not
* saved and zone is not switched.
*/
if (knot_changeset_is_empty(changeset)) {
return 1;
}
/* If not set, create new SOA. */
assert(sn_new == (uint32_t)sn + 1);
ret = knot_rrset_deep_copy(soa, &soa_end, 1);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to copy ending SOA: %s\n",
knot_strerror(ret));
*rcode = KNOT_RCODE_SERVFAIL;
return ret;
}
knot_rrset_rdata_soa_serial_set(soa_end, sn_new);
/* And replace it in the zone. */
ret = xfrin_replace_rrset_in_node(
knot_zone_contents_get_apex(zone),
soa_end, changes, zone);
if (ret != KNOT_EOK) {
dbg_ddns("Failed to copy replace SOA in zone: %s\n",
knot_strerror(ret));
*rcode = KNOT_RCODE_SERVFAIL;
return ret;
}
}
ret = knot_ddns_final_soa_to_chgset(soa_end, changeset);
return ret;
}
|