summaryrefslogtreecommitdiffstats
path: root/contrib/bind/bin/named/ns_req.c
blob: 42039f9c3bae78da2eb57ce11e78ddb4a400317f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
#if !defined(lint) && !defined(SABER)
static const char sccsid[] = "@(#)ns_req.c	4.47 (Berkeley) 7/1/91";
static const char rcsid[] = "$Id: ns_req.c,v 8.169 2002/05/12 23:41:52 marka Exp $";
#endif /* not lint */

/*
 * Copyright (c) 1986, 1988, 1990
 *    The Regents of the University of California.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 * 	This product includes software developed by the University of
 * 	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies, and that
 * the name of Digital Equipment Corporation not be used in advertising or
 * publicity pertaining to distribution of the document or software without
 * specific, written prior permission.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

/*
 * Portions Copyright (c) 1995 by International Business Machines, Inc.
 *
 * International Business Machines, Inc. (hereinafter called IBM) grants
 * permission under its copyrights to use, copy, modify, and distribute this
 * Software with or without fee, provided that the above copyright notice and
 * all paragraphs of this notice appear in all copies, and that the name of IBM
 * not be used in connection with the marketing of any product incorporating
 * the Software or modifications thereof, without specific, written prior
 * permission.
 *
 * To the extent it has a right to do so, IBM grants an immunity from suit
 * under its patents, if any, for the use, sale or manufacture of products to
 * the extent that such products are used for performing Domain Name System
 * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
 * granted for any product per se or for any other function of any product.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
 */

/*
 * Portions Copyright (c) 1996-2000 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#include "port_before.h"

#include <sys/types.h>
#include <sys/param.h>
#include <sys/uio.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <netinet/in.h>
#include <arpa/nameser.h>
#include <arpa/inet.h>

#include <errno.h>
#include <fcntl.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>

#include <isc/eventlib.h>
#include <isc/logging.h>
#include <isc/memcluster.h>

#include <isc/dst.h>

#include "port_after.h"

#include "named.h"

struct addinfo {
	char		*a_dname;		/* domain name */
	char		*a_rname;		/* referred by */
	u_int16_t	a_rtype;		/* referred by */
	u_int16_t	a_type;			/* type for data */
	u_int16_t	a_class;		/* class for data */
};


#ifndef BIND_UPDATE
enum req_action { Finish, Refuse, Return };
#endif

static struct addinfo	addinfo[NADDRECS];
static void		addname(const char *, const char *,
				u_int16_t, u_int16_t, u_int16_t);
static void		copyCharString(u_char **, const char *);

static enum req_action	req_query(HEADER *hp, u_char **cpp, u_char *eom,
				  struct qstream *qsp,
				  int *buflenp, int *msglenp,
				  u_char *msg, int dfd, int *ra,
				  struct sockaddr_in from,
				  struct tsig_record *in_tsig,
				  u_int16_t udpsize);

static enum req_action	req_iquery(HEADER *hp, u_char **cpp, u_char *eom,
				   int *buflenp, struct sockaddr_in from);

#ifdef BIND_NOTIFY
static enum req_action	req_notify(HEADER *hp, u_char **cpp, u_char *eom,
				   u_char *msg,struct sockaddr_in from);
#endif

/*
 * See if there is a OPT record at the end of the message.
 *
 * Results:
 * -1	FORMERR
 *  0	last RR is not a OPT record
 * n>0	lenght of OPT record
 */
int
ns_get_opt(u_char *msg, u_char *eom,
	   u_int8_t *versionp, u_int16_t *rcodep, u_int16_t *flagp,
	   u_int16_t *bufsizep, u_char **optionsp, size_t *optsizep)
{
	HEADER *hp = (HEADER *) msg;
	u_char *start, *options, *cp;
	u_int8_t version;
	u_int16_t rdlen, type, bufsize, flags, optsize, rcode;
	int i, n, root;

	if (msg == NULL || eom == NULL || (msg + HFIXEDSZ) > eom)
		return (-1);

	if (ntohs(hp->arcount) == 0)
		return (0);

	cp = msg + HFIXEDSZ;
	n = ns_skiprr(cp, eom, ns_s_qd, ntohs(hp->qdcount));
	if (n < 0)
		return (-1);
	cp += n;
	n = ns_skiprr(cp, eom, ns_s_an, ntohs(hp->ancount));
	if (n < 0)
		return (-1);
	cp += n;
	n = ns_skiprr(cp, eom, ns_s_ns, ntohs(hp->nscount));
	if (n < 0)
		return (-1);
	cp += n;
	i = ntohs(hp->arcount);
	while (i-- > 0) {
		start = cp;
		if (cp >= eom)
			return (-1);
		root = (*cp == 0);
		n = dn_skipname(cp, eom);
		if (n < 0)
			return (-1);
		cp += n;
		if (cp + (2 + 2 + 4 + 2) > eom)
			return (-1);
		GETSHORT(type, cp);
		if (type != ns_t_opt) {
			cp += INT16SZ + INT32SZ;	/* class, ttl */
			GETSHORT(rdlen, cp);
			if (cp + rdlen > eom)
				return (-1);
			cp += rdlen;
			continue;
		}
		/* We have the OPT record.  Check it out in detail. */
		if (!root)
			return (-1);
		GETSHORT(bufsize, cp);
		rcode = (*cp++ <<4) + hp->rcode ;
		version = *cp++;
		GETSHORT(flags, cp);
		GETSHORT(rdlen, cp);
		if (cp + rdlen > eom)
			return (-1);
		options = cp;
		optsize = rdlen;
		if (versionp != NULL)
			*versionp = version;
		if (rcodep != NULL)
			*rcodep = rcode;
		if (flagp != NULL)
			*flagp = flags;
		if (bufsizep != NULL)
			*bufsizep = bufsize;
		if (optionsp != NULL)
			*optionsp = options;
		if (optsizep != NULL)
			*optsizep = optsize;
		return (cp - start);
	}
	/* OPT not found */
	return (0);
}

int
ns_add_opt(u_char *msg, u_char *cp, size_t buflen, u_int8_t version,
	   u_int16_t rcode, u_int16_t size, u_int16_t flags,
	   u_char *options, size_t optlen)
{
	HEADER *hp = (HEADER *) msg;

	if ((cp + 1 + 2 + 2 + 4 + 2 + optlen) > (msg + buflen))
		return (-1);

	*cp++ = 0;			/* "." */
	PUTSHORT(ns_t_opt, cp);		/* type */
	PUTSHORT(size, cp);		/* class (udp size) */
	*cp++ = (rcode >> 4) & 0xff;	/* ttl (rcode + version + flags) */
	hp->rcode = rcode & 0xf;
	*cp++ = version;
	PUTSHORT(flags, cp);
	PUTSHORT(optlen, cp);		/* rdlen */
	memcpy(cp, options, optlen);	/* options */
	hp->arcount = htons(ntohs(hp->arcount) + 1);
	return (1 + 2 + 2 + 4 + 2 + optlen);
}

/*
 * Process request using database; assemble and send response.
 */
void
ns_req(u_char *msg, int msglen, int buflen, struct qstream *qsp,
       struct sockaddr_in from, int dfd)
{
	HEADER *hp = (HEADER *) msg;
	u_char *cp, *eom;
	enum req_action action = Return;
	int n, ra, has_tsig, tsig_size = 0, opt_size = 0, sig2len;
	u_char *tsigstart;
	u_char sig[TSIG_SIG_SIZE], sig2[TSIG_SIG_SIZE];
	struct tsig_record *in_tsig = NULL;
	int error = NOERROR;
	int msglen_orig = msglen;
	int buflen_orig = buflen;
	int siglen = sizeof sig;
	DST_KEY *key = NULL;
	time_t tsig_time;
	int opt = 0;
	u_int8_t version = 0;
	u_int16_t rcode = ns_r_noerror;
	u_int16_t udpsize = 0;
	int drop;
	int tsig_adjust = 0;

#ifdef DEBUG
	if (debug > 3) {
		ns_debug(ns_log_packet, 3, "ns_req(from %s)", sin_ntoa(from));
		fp_nquery(msg, msglen, log_get_stream(packet_channel));
	}
#endif
	
	drop = drop_port(ntohs(from.sin_port));
	if (qsp == NULL && drop == 1)
		return;

	tsigstart = ns_find_tsig(msg, msg + msglen);
	if (tsigstart == NULL)
		has_tsig = 0;
	else {
		char buf[MAXDNAME];
		u_char tmp[NS_MAXCDNAME];

		has_tsig = 1;
		n = ns_name_unpack(msg, msg + msglen, tsigstart,
				   tmp, sizeof tmp);
		if (n > 0) {
			tsig_adjust = dn_skipname(tmp, tmp + sizeof(tmp)) - n;
			if (ns_name_ntop(tmp, buf, sizeof buf) == -1)
				n = -1;
			else if (buf[0] == '.')
				buf[0] = '\0';
		}
		if (n < 0) {
			ns_debug(ns_log_default, 1,
				 "ns_req: bad TSIG key name");
			error = ns_r_formerr;
			hp->rcode = ns_r_formerr;
			key = NULL;
		} else if ((key = find_key(buf, NULL)) == NULL) {
			error = ns_r_badkey;
			hp->rcode = ns_r_notauth;
			ns_debug(ns_log_default, 1,
				 "ns_req: TSIG verify failed - unknown key %s",
				 buf);
		}
#ifdef LOG_TSIG_BUG
		if (n < 0 || key == NULL)
			ns_error(ns_log_security,
	  "SECURITY: POSSIBLE ATTEMPT TO EXERCISE \"TSIG BUG\" FROM %s: %s%s%s",
				 sin_ntoa(from),
				 (n < 0) ? "bad key (formerr)" :
					   "unknown key (",
				 (n < 0) ? "" : (buf[0] != '\0' ? buf : "."),
				 (n < 0) ? "" : ")");
#endif
	}
	if (has_tsig && key != NULL) {
		n = ns_verify(msg, &msglen, key, NULL, 0, sig, &siglen, 
			      &tsig_time, 0);
		if (n != 0) {
			hp->rcode = ns_r_notauth;
			/* A query should never have an error code set */
			if (n == ns_r_badsig || n == ns_r_badkey ||
			    n == ns_r_badtime) {
				ns_debug(ns_log_default, 1,
		"ns_req: TSIG verify failed - query had error %s (%d) set",
				p_rcode(n), n);
				error = n;
				action = Return;
			}
			/* If there's a processing error just respond */
			else if (n == -ns_r_badsig || n == -ns_r_badkey ||
				 n == -ns_r_badtime) {
				n = -n;
				ns_debug(ns_log_default, 1,
					 "ns_req: TSIG verify failed - %s (%d)",
					 p_rcode(n), n);
				error = n;
			} else {
				ns_debug(ns_log_default, 1,
					"ns_req: TSIG verify failed - FORMERR");
				error = ns_r_formerr;
			}
			action = Finish;
		}
		in_tsig = memget(sizeof(struct tsig_record));
		if (in_tsig == NULL)
			ns_panic(ns_log_default, 1, "memget failed");
		in_tsig->key = key;
		in_tsig->siglen = siglen;
		memcpy(in_tsig->sig, sig, siglen);
		tsig_size = msglen_orig - msglen;
		/* AXFR/IXFR need the uncompressed tsig size. */
		in_tsig->tsig_size = tsig_size + tsig_adjust;
	} else if (has_tsig) {
		action = Finish;
		in_tsig = memget(sizeof(struct tsig_record));
		if (in_tsig == NULL)
			ns_panic(ns_log_default, 1, "memget failed");
		in_tsig->key = NULL;
		in_tsig->siglen = 0;
		tsig_size = msg + msglen - tsigstart;
		in_tsig->tsig_size = tsig_size;
		msglen = tsigstart - msg;
	}

	/* Hash some stuff so it's nice and random */
	nsid_hash((u_char *)&tt, sizeof(tt));
	nsid_hash(msg, (msglen > 512) ? 512 : msglen);

	if (error == NOERROR) {

		opt = ns_get_opt(msg, msg + msglen, &version,
				 NULL, NULL, &udpsize, NULL, NULL);
		if (opt < 0) {
			rcode = ns_r_formerr;
			action = Finish;
		} else if (opt == 0) {
			if (qsp == NULL && buflen > PACKETSZ)
				buflen_orig = buflen = PACKETSZ;
		} else if (opt > 0) {
			if (version != 0) {
				rcode = ns_r_badvers;
				action = Finish;
			}
			opt_size = 11;
			if (udpsize < 512)
				udpsize = 512;
			if (qsp == NULL && buflen > udpsize)
				buflen_orig = buflen = udpsize;
		}
	} else if (qsp == NULL && buflen > PACKETSZ)
		buflen_orig = buflen = PACKETSZ;

	/*
	 * It's not a response so these bits have no business
	 * being set. will later simplify work if we can
	 * safely assume these are always 0 when a query
	 * comes in.
	 */
#ifdef BIND_NOTIFY
	if (hp->opcode != ns_o_notify)
#endif
		hp->aa = 0;
	hp->ra = 0;
	ra = (NS_OPTION_P(OPTION_NORECURSE) == 0);

	if (error == NOERROR)
		hp->rcode = ns_r_noerror;
	if (rcode == ns_r_noerror)
		rcode = hp->rcode;
	cp = msg + HFIXEDSZ;
	eom = msg + msglen;
	buflen -= HFIXEDSZ;

	free_addinfo();	/* sets addcount to zero */
	dnptrs[0] = NULL;

	if (error == NOERROR && rcode == ns_r_noerror) {
		switch (hp->opcode) {
		case ns_o_query:
			action = req_query(hp, &cp, eom, qsp,
					   &buflen, &msglen,
					   msg, dfd, &ra, from,
					   in_tsig, udpsize);
			break;

		case ns_o_iquery:
			action = req_iquery(hp, &cp, eom, &buflen, from);
			break;

#ifdef BIND_NOTIFY
		case ns_o_notify:
			action = req_notify(hp, &cp, eom, msg, from);
			break;
#endif

#ifdef BIND_UPDATE
		case ns_o_update:
			action = req_update(hp, cp, eom, msg, from, in_tsig);
			break;
#endif /* BIND_UPDATE */

		default:
			ns_debug(ns_log_default, 1,
				 "ns_req: Opcode %d not implemented",
				 hp->opcode);
			/* XXX - should syslog, limited by haveComplained */
			hp->qdcount = htons(0);
			hp->ancount = htons(0);
			hp->nscount = htons(0);
			hp->arcount = htons(0);
			hp->rcode = ns_r_notimpl;
			action = Finish;
		}
		rcode = hp->rcode;
	}

	if (in_tsig != NULL) {
		memput(in_tsig, sizeof(struct tsig_record));
		in_tsig = NULL;
	}

	/*
	 * Loop advoidance.
	 */
	if (qsp == NULL && drop == 2 && 
	    (hp->rcode == FORMERR || hp->rcode == NOTIMP))
			action = Return;

	/*
	 * Vector via internal opcode.
	 */
	switch (action) {
	case Return:
		return;
	case Refuse:
		rcode = hp->rcode = ns_r_refused;
		cp = eom;
		/*FALLTHROUGH*/
	case Finish:
		/* rest of the function handles this case */
		break;
	default:
		panic("ns_req: bad action variable", NULL);
		/*NOTREACHED*/
	}

	/*
	 * Apply final polish.
	 */
	hp->qr = 1;		/* set Response flag */
	hp->ra = ra;		/* init above, may be modified by req_query */

	if (!hp->tc && (has_tsig > 0 || opt > 0) &&
	    buflen < (tsig_size + opt_size))
		hp->tc = 1;

	/*
	 * If there was a format error, then we don't know what the msg has.
	 */
	if (hp->rcode == ns_r_formerr || rcode == ns_r_badvers) {
		hp->qdcount = htons(0);
		hp->ancount = htons(0);
		hp->nscount = htons(0);
		hp->arcount = htons(0);
		cp = msg + HFIXEDSZ;
	}

	/*
	 * If the query had a TSIG / OPT and the message is truncated or
	 * there was a TSIG error, build a new message with no data and a
	 * TSIG / OPT.
	 */
	if ((hp->tc || error != NOERROR) && (has_tsig > 0 || opt > 0)) {
 sign_again:
		hp->ancount = htons(0);
		hp->nscount = htons(0);
		hp->arcount = htons(0);
		cp = msg + HFIXEDSZ;
		cp += ns_skiprr(cp, msg + msglen, ns_s_qd, ntohs(hp->qdcount));
		ns_name_rollback(cp, (const u_char **)dnptrs,
				 (const u_char **)dnptrs_end);
		if (opt > 0) {
			n = ns_add_opt(msg, cp, buflen_orig, 0,
				       rcode, EDNS_MESSAGE_SZ, 0, NULL, 0);
			if (n < 0) {
				hp->qdcount = htons(0);
				goto sign_again;
			}
			cp += n;
		}
		if (has_tsig > 0) {
			sig2len = sizeof sig2;
			msglen = cp - msg;
			buflen = buflen_orig - msglen;
			n = ns_sign2(msg, &msglen, msglen + buflen, error, key,
				     sig, siglen, sig2, &sig2len, tsig_time,
				     dnptrs, dnptrs_end);
			if (n == NS_TSIG_ERROR_NO_SPACE &&
				ntohs(hp->qdcount) != 0) {
				hp->qdcount = htons(0);
				goto sign_again;
			}
			if (n != 0)
				ns_info(ns_log_default,
					"ns_req: unable to sign response");
			cp = msg + msglen;
		}
	}
	/* Either the message is not truncated or there was no TSIG & OPT */
	else {
		/*
		 * Reserve space for tsig if required.
		 */
		if (has_tsig > 0 || opt_size != 0)
			buflen -= tsig_size + opt_size;
		INSIST(buflen >= 0);
		msglen = cp - msg;
		n = doaddinfo(hp, cp, buflen);
		cp += n;
		buflen -= n;
		msglen += n;
		if (opt > 0) {
			buflen += opt_size;
			n = ns_add_opt(msg, cp, msglen + buflen, 0,
				       rcode, EDNS_MESSAGE_SZ, 0, NULL, 0);
			INSIST(n > 0);
			cp += n;
			buflen -= n;
			msglen += n;
		}
		if (has_tsig > 0) {
			buflen += tsig_size;
			sig2len = sizeof sig2;
			n = ns_sign2(msg, &msglen, msglen + buflen, error, key,
				     sig, siglen, sig2, &sig2len, tsig_time,
				     dnptrs, dnptrs_end);
			if (n != 0) {
				INSIST(0);
			}
			cp = msg + msglen;
		}
	}

#ifdef DEBUG
	if (ns_wouldlog(ns_log_default, 1)) {
		ns_debug(ns_log_default, 1,
			 "ns_req: answer -> %s fd=%d id=%d size=%d rc=%d",
			 sin_ntoa(from), (qsp == NULL) ? dfd : qsp->s_rfd,
			 ntohs(hp->id), cp - msg, hp->rcode);
	}
	if (debug >= 10)
		res_pquery(&res, msg, cp - msg,
			    log_get_stream(packet_channel));
#endif /*DEBUG*/
	if (qsp == NULL) {
		if (sendto(dfd, (char*)msg, cp - msg, 0,
			   (struct sockaddr *)&from,
			   sizeof from) < 0) {
			if (!haveComplained(ina_ulong(from.sin_addr),
					    (u_long)sendtoStr))
				ns_info(ns_log_default,
					"ns_req: sendto(%s): %s",
					sin_ntoa(from), strerror(errno));
			nameserIncr(from.sin_addr, nssSendtoErr);
		}
		nameserIncr(from.sin_addr, nssSentAns);
		if (hp->rcode == ns_r_nxdomain) 
			nameserIncr(from.sin_addr, nssSentNXD);
		if (!hp->aa) 
			nameserIncr(from.sin_addr, nssSentNaAns);
	} else
		writestream(qsp, msg, cp - msg);

	/* Is now a safe time? */
	if (needs_prime_cache)
		prime_cache();
}

#ifdef BIND_NOTIFY
int
findZonePri(const struct zoneinfo *zp, const struct sockaddr_in from) {
	struct in_addr ina;
	int i;

	ina = from.sin_addr;
	for (i = 0; (u_int)i < zp->z_addrcnt; i++)
		if (ina_equal(zp->z_addr[i], ina))
			return (i);
	return (-1);
}

static enum req_action
req_notify(HEADER *hp, u_char **cpp, u_char *eom, u_char *msg,
	   struct sockaddr_in from)
{
	int n, type, class;
	char dnbuf[MAXDNAME];
	struct zoneinfo *zp;

	/* valid notify's are authoritative */
	if (!hp->aa) {
		ns_debug(ns_log_notify, 1,
			 "FORMERR Notify request without AA");
#ifdef not_yet
		hp->rcode = ns_r_formerr;
		return (Finish);
#endif
	}
	hp->aa = 0;

	/* valid notify's have one question */
	if (ntohs(hp->qdcount) != 1) {
		ns_debug(ns_log_notify, 1,
			 "FORMERR Notify header counts wrong");
		hp->rcode = ns_r_formerr;
		return (Finish);
	}

	n = dn_expand(msg, eom, *cpp, dnbuf, sizeof dnbuf);
	if (n < 0) {
		ns_debug(ns_log_notify, 1,
			 "FORMERR Query expand name failed");
		hp->rcode = ns_r_formerr;
		return (Finish);
	}
	*cpp += n;
	if (*cpp + 2 * INT16SZ > eom) {
		ns_debug(ns_log_notify, 1,
			 "FORMERR notify too short");
		hp->rcode = ns_r_formerr;
		return (Finish);
	}
	GETSHORT(type, *cpp);
	GETSHORT(class, *cpp);
	ns_info(ns_log_notify, "rcvd NOTIFY(%s, %s, %s) from %s",
		dnbuf, p_class(class), p_type(type), sin_ntoa(from));
	/* XXX - when answers are allowed, we'll need to do compression
	 * correctly here, and we will need to check for packet underflow.
	 */
	/*
	 * We are ignoring the other field, make sure the header reflects
	 * *cpp.
	 */
	hp->ancount = htons(0);
	hp->nscount = htons(0);
	hp->arcount = htons(0);
	/* Find the zone this NOTIFY refers to. */
	zp = find_auth_zone(dnbuf, class);
	if (zp == NULL) {
		ns_info(ns_log_notify,
			"rcvd NOTIFY for \"%s\", name not one of our zones",
			dnbuf);
		hp->rcode = ns_r_servfail;
		return (Finish);
	}
	/* Access control. */
	switch (type) {
	case T_SOA:
		if (zp->z_type != z_slave) {
			/*
			 * This can come if a user did an AXFR of some zone
			 * somewhere and that zone's server now wants to
			 * tell us that the SOA has changed.  AXFR's always
			 * come from nonpriv ports so it isn't possible to
			 * know whether it was the server or just "dig".
			 * This condition can be avoided by using secure
			 * zones since that way only real secondaries can
			 * AXFR from you.
			 */
			ns_info(ns_log_notify,
			    "NOTIFY(SOA) for non-slave zone (%s), from %s",
				dnbuf, sin_ntoa(from));
			goto refuse;
		}
		if (ns_samename(dnbuf, zp->z_origin) != 1) {
			ns_info(ns_log_notify,
				"NOTIFY(SOA) for non-origin (%s), from %s",
				dnbuf, sin_ntoa(from));
			goto refuse;
		}
		if (findZonePri(zp, from) == -1) {
			ns_debug(ns_log_notify, 1,
			"NOTIFY(SOA) from non-master server (zone %s), from %s",
				zp->z_origin, sin_ntoa(from));
			goto refuse;
		}
		break;
	default:
		/* No access requirements defined for other types. */
		break;
	}
	/* The work occurs here. */
	switch (type) {
	case T_SOA:
		if (zp->z_flags &
		    (Z_NEED_RELOAD|Z_NEED_XFER|Z_QSERIAL|Z_XFER_RUNNING)) {
			ns_info(ns_log_notify,
				"NOTIFY(SOA) for zone already xferring (%s)",
				dnbuf);
			zp->z_flags |= Z_NEEDREFRESH;
			goto noerror;
		}
		zp->z_time = tt.tv_sec;
		qserial_query(zp);
		sched_zone_maint(zp);
		break;
	default:
		/*
		 * Unimplemented, but it's not a protocol error, just
		 * something to be ignored.
		 */
		hp->rcode = ns_r_notimpl;
		return (Finish);
	}
 noerror:
	hp->rcode = ns_r_noerror;
	hp->aa = 1;
	return (Finish);
 refuse:
	hp->rcode = ns_r_refused;
	return (Finish);
}
#endif /*BIND_NOTIFY*/

static int
add_bind(HEADER *hp, u_char **cpp, u_char *msg, int *msglenp,
	 const char *label, const char *data)
{
	u_char *tp;

	hp->ancount = htons(1);
	hp->nscount = htons(0);
	hp->arcount = htons(0);
	hp->rcode = ns_r_noerror;
	hp->aa = 1;
	hp->ra = 0;
	copyCharString(cpp, label);	/* Name */
	copyCharString(cpp, "BIND");
	*(*cpp)++ = 0x00;
	PUTSHORT(T_TXT, *cpp);		/* Type */
	PUTSHORT(C_CHAOS, *cpp);	/* Class */
	PUTLONG(0, *cpp);		/* TTL */
	tp = *cpp;			/* Temp RdLength */
	PUTSHORT(0, *cpp);
	copyCharString(cpp, data);
	PUTSHORT((*cpp) - (tp + INT16SZ), tp);	/* Real RdLength */
	*msglenp = *cpp - msg;		/* Total message length */
	return (Finish);
}

static enum req_action
req_query(HEADER *hp, u_char **cpp, u_char *eom, struct qstream *qsp,
	  int *buflenp, int *msglenp, u_char *msg, int dfd, int *ra,
	  struct sockaddr_in from, struct tsig_record *in_tsig,
	  u_int16_t udpsize)
{
	int n, class, type, count, zone, foundname, founddata, omsglen, cname;
	int recursion_blocked_by_acl;
	u_int16_t id;
	u_int32_t serial_ixfr = 0;
	int ixfr_found;
	int ixfr_error = 0;
	char dnbuf2[MAXDNAME];
	u_char **dpp, *omsg, *answers, *afterq;
	char dnbuf[MAXDNAME], *dname;
	const char *fname;
	struct hashbuf *htp;
	struct databuf *nsp[NSMAX];
	struct namebuf *np, *anp;
	struct qinfo *qp;
	struct zoneinfo *zp;
	struct databuf *dp;
	DST_KEY *in_key = (in_tsig != NULL) ? in_tsig->key : NULL;
	int access_class;
	int adjustlen = 0;

	nameserIncr(from.sin_addr, nssRcvdQ);

	nsp[0] = NULL;
	dpp = dnptrs;
	*dpp++ = msg;
	*dpp = NULL;
	/*
	 * Make gcc happy.
	 */
	omsglen = 0;
	omsg = NULL;
	id = 0;
	recursion_blocked_by_acl = 0;

	/* valid queries have one question and zero answers */
	if ((ntohs(hp->qdcount) != 1)
	    || ntohs(hp->ancount) != 0) {
		ns_debug(ns_log_default, 1,
			 "FORMERR Query header counts wrong");
		hp->rcode = ns_r_formerr;
		return (Finish);
	}

	if (ntohs(hp->arcount) != 0) {
		ns_debug(ns_log_default, 1, "Ignoring addition section");
		hp->arcount = htons(0);
		adjustlen = 1;
	}

	/*
	 * Get domain name, class, and type.
	 */
	if ((**cpp & INDIR_MASK) == 0)
		*dpp++ = *cpp;	/* remember name for compression */
	*dpp = NULL;
	n = dn_expand(msg, eom, *cpp, dnbuf, sizeof dnbuf);
	if (n < 0) {
		ns_debug(ns_log_default, 1,
			 "FORMERR Query expand name failed");
		hp->rcode = ns_r_formerr;
		return (Finish);
	}
	*cpp += n;
	answers = *cpp;
	if (*cpp + 2 * INT16SZ > eom) {
		ns_debug(ns_log_default, 1,
			 "FORMERR Query message length short");
		hp->rcode = ns_r_formerr;
		return (Finish);
	}
	GETSHORT(type, *cpp);
	GETSHORT(class, *cpp);
	if (*cpp < eom && type != ns_t_ixfr) {
		if (!adjustlen)
			ns_debug(ns_log_default, 6,
				 "message length > received message");
		*msglenp = *cpp - msg;
	}

	if (((ntohs(hp->nscount) != 0) && (type != ns_t_ixfr)) ||
	    ((ntohs(hp->nscount) != 1) && (type == ns_t_ixfr)))
	{
		ns_debug(ns_log_default, 1, "FORMERR Query nscount wrong"); 
		hp->rcode = ns_r_formerr;
		return (Finish);
	} 

	afterq = *cpp;
	qtypeIncr(type);

	/*
	 * Process query.
	 */
	if (type == ns_t_ixfr) {
		ns_info(ns_log_security, "Request %s from %s",
			p_type(type), sin_ntoa(from));
		hp->nscount = htons(0);
		hp->rd = 0; /* Force IXFR queries to be non recursive. */
		n = dn_expand(msg, eom, *cpp, dnbuf2, sizeof dnbuf2);
		if (n < 0) {
			ns_debug(ns_log_default, 1,
				 "FORMERR Query expand name failed");
			hp->rcode = ns_r_formerr;
			return (Finish);
		}
		*cpp += n;
		if (*cpp + 3 * INT16SZ + INT32SZ > eom) {
			ns_debug(ns_log_default, 1,
				 "ran out of data in IXFR query");
			hp->rcode = ns_r_formerr;
			return (Finish);
		}
		GETSHORT(n, *cpp);
		if (n != ns_t_soa || ns_samename(dnbuf, dnbuf2) != 1) {
			ns_debug(ns_log_default, 1,
				 "FORMERR SOA record expected");
			hp->rcode = ns_r_formerr;
			return (Finish);
		}
		*cpp += INT32SZ + INT16SZ * 2; /* skip class, ttl, dlen */
		if (0 >= (n = dn_skipname(*cpp, eom))) {
			ns_debug(ns_log_default, 1,
				 "FORMERR Query expand name failed");
			hp->rcode = ns_r_formerr;
			return (Finish);
		}
		*cpp += n;  /* mname */
		if (0 >= (n = dn_skipname(*cpp, eom))) {
			ns_debug(ns_log_default, 1,
				 "FORMERR Query expand name failed");
			hp->rcode = ns_r_formerr;
			return (Finish);
		}
		*cpp += n;  /* rname */
		if (*cpp + 5 * INT32SZ > eom) {
			ns_debug(ns_log_default, 1,
				 "ran out of data in IXFR query");
			hp->rcode = ns_r_formerr;
			return (Finish);
		}
		GETLONG(serial_ixfr, *cpp);
		/* ignore other soa counters */
		if ((*cpp + (4 * INT32SZ)) < eom && !adjustlen)
			ns_debug(ns_log_default, 6,
				 "ixfr: message length > received message");
		/* Reset msglenp to cover just the question. */
		*msglenp = afterq - msg;
	}
	*cpp = afterq;

	if (!ns_t_udp_p(type)) {
		/* Refuse request if not a TCP connection. */
		if (qsp == NULL) {
			ns_info(ns_log_default,
				"rejected UDP %s from %s for \"%s\"",
				p_type(type), sin_ntoa(from),
				*dnbuf ? dnbuf : ".");
			return (Refuse);
		}
		/* The position of this is subtle. */
		nameserIncr(from.sin_addr, nssRcvdAXFR);
		hp->rd = 0;		/* Recursion not possible. */
	}
	*buflenp -= (*msglenp - HFIXEDSZ);
	count = 0;
	founddata = 0;
	dname = dnbuf;
	cname = 0;

#ifdef QRYLOG
	if (qrylog) {
		ns_info(ns_log_queries, "%s/%s/%s/%s/%s",
			(hp->rd) ? "XX+" : "XX ",
			inet_ntoa(from.sin_addr), 
			(dname[0] == '\0') ? "." : dname, 
			p_type(type), p_class(class));
	}
#endif /*QRYLOG*/

 try_again:
	foundname = 0;
	ns_debug(ns_log_default, 1, "req: nlookup(%s) id %d type=%d class=%d",
		 dname, ntohs(hp->id), type, class);
	htp = hashtab;		/* lookup relative to root */
	if ((anp = np = nlookup(dname, &htp, &fname, 0)) == NULL)
		fname = "";
	ns_debug(ns_log_default, 1, "req: %s '%s' as '%s' (cname=%d)",
		 np == NULL ? "missed" : "found",
		 dname, fname, cname);

#ifdef YPKLUDGE
	/* Some braindamaged resolver software will not 
	   recognize internet addresses in dot notation and 
	   send out address  queries for "names" such as 
	   128.93.8.1.  This kludge will prevent those 
	   from flooding higher level servers.
	   We simply claim to be authoritative and that
	   the domain doesn't exist.
	   Note that we could return the address but we
	   don't do that in order to encourage that broken
	   software is fixed.
	*/

	if (!np && type == T_A && class == C_IN && dname) {
		struct in_addr ina;

		if (inet_aton(dname, &ina)) {
			hp->rcode = ns_r_nxdomain;
			hp->aa = 1;
			ns_debug(ns_log_default, 3,
				 "ypkludge: hit as '%s'", dname);
			return (Finish);
		}
	}
#endif /*YPKLUDGE*/

	/*
	 * Don't accept in a query names which would be rejected in responses.
	 * (This is primarily in case we have to forward it, but it's also a
	 * matter of architectural symmetry.)
	 */
	if (!ns_nameok(NULL, dname, class, NULL, response_trans,
		       ns_ownercontext(type, response_trans),
		       dname, from.sin_addr)) {
		ns_debug(ns_log_default, 1, "bad name in query"); 
		hp->rcode = ns_r_formerr;
		return (Refuse);
	}

	/*
	 * Begin Access Control Point
	 */
	zone = DB_Z_CACHE;

	/*
	 * Map class ANY to to class IN for the purpose of access control.
	 */
	access_class = (class == C_ANY && !ns_t_xfr_p(type)) ? C_IN : class;

	if (np) {
#ifndef FORWARD_ALLOWS
		struct namebuf *access_np;

		/*
		 * Find out which zone this will be answered from.  Note
		 * that we look for a zone with the same class as ours.
		 * The np that we found in the database might not be the
		 * one we asked for (i.e. dname might not equal fname).  This
		 * is OK, since if a name doesn't exist, we need to go up
		 * the tree until we find the closest enclosing zone that
		 * is of the same class.
		 */
		for (access_np = np; access_np != NULL;
		     access_np = np_parent(access_np)) {
			dp = access_np->n_data;
			while (dp && dp->d_class != access_class)
				dp = dp->d_next;
			if (dp != NULL) {
				zone = dp->d_zone;
				break;
			}
		}
#else
		/*
		 * Try looking for forward zone.  It can be deeper than
		 * any entry in the cache.
		 */
		if (zone == DB_Z_CACHE) {
			char *s = dname;
			int escape = 0;
			while ((zp = find_zone(s, access_class)) == NULL) {
				if (*s == '\0')
					break;
				while (*s != '\0' && (escape || *s != '.')) {
					escape = escape ? 0 : (*s == '\\');
					s++;
				}   
				if (*s == '.')
					s++;
			}
			if (zp != NULL)
				zone = zp - zones;
		}
#endif
	}

	zp = &zones[zone];

	ixfr_found = 0;
	if (type == ns_t_ixfr && zone != DB_Z_CACHE) {
		if (SEQ_GT(serial_ixfr, zp->z_serial))
			ixfr_found = 0;
		else {
			ixfr_error = ixfr_have_log(zp, serial_ixfr,
						   zp->z_serial);
			if (ixfr_error < 0) {
				ns_info(ns_log_security, "No %s log from %d for \"%s\"",
					p_type(type), serial_ixfr, *dname ? dname : ".");
				ns_debug(ns_log_default,
					 	1, "ixfr_have_log(%d %d) failed %d", 
			 			serial_ixfr, zp->z_serial, ixfr_error);
				ixfr_found = 0; /* Refuse IXFR and send AXFR */
			} else if (ixfr_error == 1) {
				ixfr_found = 1;
			}
		}   
		ns_debug(ns_log_default, 1, "IXFR log lowest serial: %d", 
			 zp->z_serial_ixfr_start);
	}
	/*
	 * If recursion is turned on, we need to check recursion ACL
	 * if it exists - and return result to caller.
	 */
	{
		ip_match_list recursion_acl;

		recursion_acl = server_options->recursion_acl;
		if (!NS_OPTION_P(OPTION_NORECURSE) && recursion_acl != NULL
		    && !ip_address_allowed(recursion_acl, from.sin_addr)) {
			recursion_blocked_by_acl = 1;
			*ra = 0;
		}
	}

	/*
	 * Are queries allowed from this host?
	 */
	if (!ns_t_xfr_p(type)) {
		ip_match_list query_acl;

		if (zp->z_query_acl != NULL)
			query_acl = zp->z_query_acl;
		else
			query_acl = server_options->query_acl;

		if (query_acl != NULL
		    && !ip_addr_or_key_allowed(query_acl, from.sin_addr,
					       in_key))
		{
			/*
			 * If this is *not* a zone acl and we would not
			 * have recursed and we have some answer return
			 * what we have with a referral.
			 */
			if ((zp->z_query_acl == NULL) &&
			    (!hp->rd || NS_OPTION_P(OPTION_NORECURSE) ||
			     recursion_blocked_by_acl) &&
			    (ntohs(hp->ancount) != 0)) {
				goto fetchns;
			}

			/*
			 * See if we would have made a referral from 
			 * an enclosing zone if we are actually in the
			 * cache.
			 */
			if (zp->z_type == z_cache && np != NULL) {
				struct namebuf *access_np;

				zone = DB_Z_CACHE;
				for (access_np = np; access_np != NULL;
				     access_np = np_parent(access_np)) {
					dp = access_np->n_data;
					while (dp &&
					       (dp->d_class != access_class ||
					        dp->d_zone == DB_Z_CACHE))
						dp = dp->d_next;
					if (dp != NULL) {
						zone = dp->d_zone;
						np = access_np;
						break;
					}
				}
				zp = &zones[zone];
				if (zp->z_type != z_cache &&
				    zp->z_query_acl != NULL &&
				    ip_addr_or_key_allowed(zp->z_query_acl,
						   from.sin_addr, in_key) &&
				    (!hp->rd || recursion_blocked_by_acl ||
				     NS_OPTION_P(OPTION_NORECURSE))) {
					goto fetchns;
				}
			}
#ifdef NXDOMAIN_ON_DENIAL
			hp->rcode = ns_r_nxdomain;
			return (Finish);
#else
			ns_notice(ns_log_security,
				  "denied query from %s for \"%s\" %s/%s",
				  sin_ntoa(from), *dname ? dname : ".",
				  p_type(type), p_class(class));
			nameserIncr(from.sin_addr, nssRcvdUQ);
			return (Refuse);
#endif
		}
	} else {
		ip_match_list transfer_acl;

		/* Do they have permission to do a zone transfer? */

		if (zp->z_transfer_acl != NULL)
			transfer_acl = zp->z_transfer_acl;
		else
			transfer_acl = server_options->transfer_acl;

		if (transfer_acl != NULL
		    && !ip_addr_or_key_allowed(transfer_acl, from.sin_addr,
					       in_key))
		{
			ns_notice(ns_log_security,
				  "denied %s from %s for \"%s\" %s (acl)",
				  p_type(type), sin_ntoa(from),
				  *dname ? dname : ".", p_class(class));
			nameserIncr(from.sin_addr, nssRcvdUXFR);
			if (type == ns_t_ixfr) {
				hp->rcode = ns_r_refused;
				return (Finish);
			}
			return (Refuse);
		}

		/* Are we master or slave? */

		if (zp->z_type != z_master && zp->z_type != z_slave) {
			ns_notice(ns_log_security,
			 "denied %s from %s for \"%s\" (not master/slave)",
				  p_type(type), sin_ntoa(from),
				  *dname ? dname : ".");
			nameserIncr(from.sin_addr, nssRcvdUXFR);
			if (type == ns_t_ixfr) {
				hp->rcode = ns_r_refused;
				return (Finish);
			}
			return (Refuse);
		}

		/* Are we authoritative? */

		if ((zp->z_flags & Z_AUTH) == 0) {
			ns_notice(ns_log_security,
			 "denied %s from %s for \"%s\" %s (not authoritative)",
				  p_type(type), sin_ntoa(from),
				  *dname ? dname : ".", p_class(class));
			nameserIncr(from.sin_addr, nssRcvdUXFR);
			if (type == ns_t_ixfr) {
				hp->rcode = ns_r_refused;
				return (Finish);
			}
			return (Refuse);
		}

		/* Is the name at a zone cut? */

		if (ns_samename(zp->z_origin, dname) != 1) {
			ns_notice(ns_log_security,
			  "denied %s from %s for \"%s\" %s (not zone top)",
				  p_type(type), sin_ntoa(from),
				  *dname ? dname : ".", p_class(class));
			nameserIncr(from.sin_addr, nssRcvdUXFR);
			if (type == ns_t_ixfr) {
				hp->rcode = ns_r_refused;
				return (Finish);
			}
			return (Refuse);
		}

		if (type == ns_t_ixfr) { 
		    ns_info(ns_log_security, "approved %s from %s for \"%s\"",
		    	(ixfr_found) ? p_type(type) : "IXFR/AXFR", 
			sin_ntoa(from), *dname ? dname : ".");
		} else
		    ns_info(ns_log_security, "approved %s from %s for \"%s\"",
		    	p_type(type), sin_ntoa(from), *dname ? dname : ".");
	}

	/*
	 * End Access Control Point
	 */
	/*
	 * Yow!
	 */
	if (class == ns_c_chaos && type == ns_t_txt &&
	    ns_samename(dnbuf, "VERSION.BIND") == 1 &&
	    server_options->version != NULL &&
	    server_options->version[0] != '\0')
		return (add_bind(hp, cpp, msg, msglenp,
			"VERSION", server_options->version));

	if (class == ns_c_chaos && type == ns_t_txt &&
	    ns_samename(dnbuf, "HOSTNAME.BIND") == 1 &&
	    server_options->hostname != NULL &&
	    server_options->hostname[0] != '\0')
		return (add_bind(hp, cpp, msg, msglenp,
			"HOSTNAME", server_options->hostname));

	/*
	 * If we don't know anything about the requested name,
	 * go look for nameservers.
	 */
	if (!np || fname != dname)
		goto fetchns;

	foundname++;
	answers = *cpp;
	count = *cpp - msg;

	/* The response is authoritative until we add insecure data */
	hp->ad = 1;

	/* Look for NXDOMAIN record with appropriate class
	 * if found return immediately
	 */
	for (dp = np->n_data; dp; dp = dp->d_next) {
		if (!stale(dp) && (dp->d_rcode == ns_r_nxdomain) &&
		    (dp->d_class == class)) {
#ifdef RETURNSOA
			n = finddata(np, class, T_SOA, hp, &dname,
				     buflenp, &count);
			if (n != 0) {
				if (count) {
					*cpp += n;
					*buflenp -= n;
					*msglenp += n;
					hp->nscount = htons((u_int16_t)count);
				}
				if (hp->rcode == NOERROR_NODATA) {
					/* this should not occur */
					hp->rcode = ns_r_noerror;
					return (Finish);
				}
			}
#else
			count = 0;
#endif
			hp->rcode = ns_r_nxdomain;
			/* 
			 * XXX forcing AA all the time isn't right, but
			 * we have to work that way by default
			 * for compatibility with older servers.
			 */
			if (!NS_OPTION_P(OPTION_NONAUTH_NXDOMAIN))
			    hp->aa = 1;
			ns_debug(ns_log_default, 3, "NXDOMAIN aa = %d",
				 hp->aa);
			if ((count == 0) || NS_OPTION_P(OPTION_NORFC2308_TYPE1))
				return (Finish);
			founddata = 1;
			goto fetchns;
		}
	}

	/*
	 * If not NXDOMAIN, the NOERROR_NODATA record might be
	 * anywhere in the chain.  Have to go through the grind.
	 */

	n = finddata(np, class, type, hp, &dname, buflenp, &count);
	if (n == 0) {
		/*
		 * NO data available.  Refuse transfer requests, or
		 * look for better servers for other requests.
		 */
		if (ns_t_xfr_p(type)) {
			ns_debug(ns_log_default, 1,	
				 "transfer refused: no data");
			return (Refuse);
		}
		goto fetchns;
	}

	if (hp->rcode == NOERROR_NODATA) {
		hp->rcode = ns_r_noerror;
#ifdef RETURNSOA
		if (count) {
		        *cpp += n;
			*buflenp -= n;
			*msglenp += n;
			hp->nscount = htons(count);
		}
#endif
		founddata = 1;
		ns_debug(ns_log_default, 1, "count = %d", count);
		if ((count == 0) || NS_OPTION_P(OPTION_NORFC2308_TYPE1))
			return (Finish);
		goto fetchns;
	}

	*cpp += n;
	*buflenp -= n;
	*msglenp += n;
	hp->ancount = htons(ntohs(hp->ancount) + (u_int16_t)count);
	if (fname != dname && type != T_CNAME && type != T_ANY) {
		if (cname++ >= MAXCNAMES) {
			ns_debug(ns_log_default, 3,
				 "resp: leaving, MAXCNAMES exceeded");
			hp->rcode = ns_r_servfail;
			return (Finish);
		}
		goto try_again;
	}
	founddata = 1;
	ns_debug(ns_log_default, 3,
		 "req: foundname=%d, count=%d, founddata=%d, cname=%d",
		 foundname, count, founddata, cname);

	if (ns_t_xfr_p(type)) {
#ifdef BIND_UPDATE
		if ((zp->z_flags & Z_NEED_SOAUPDATE) != 0)
			if (incr_serial(zp) < 0)
				ns_error(ns_log_default,
			   "error updating serial number for %s from %d",
					 zp->z_origin, zp->z_serial);
#endif
		/*
		 * Just return SOA if "up to date".
		 */
 		if (type == ns_t_ixfr) {
 			hp->aa = 1;
 			if ((SEQ_GT(serial_ixfr, zp->z_serial) ||
			     serial_ixfr == zp->z_serial)) {
 				return (Finish);
			}
 		}

		/*
		 * We don't handle UDP based IXFR queries (yet).
		 * Tell client to retry with TCP by returning SOA.
		 */
		if (qsp == NULL)
			return (Finish);
		else {
			if (!ixfr_found && type == ns_t_ixfr) {
				qsp->flags |= STREAM_AXFRIXFR;
				hp->qdcount = htons(1);
			}
			ns_xfr(qsp, np, zone, class, type,
				       hp->opcode, ntohs(hp->id),
				       serial_ixfr, in_tsig);
		}
		return (Return);
	}

	if (count > 1 && type == T_A && !NS_OPTION_P(OPTION_NORECURSE) &&
	    hp->rd)
		sort_response(answers, *cpp, count, &from);

 fetchns:
	/*
	 * If we're already out of room in the response, we're done.
	 */
	if (hp->tc)
		return (Finish);

	if (hp->ancount == 0)
		hp->ad = 0;

	/*
 	 * Look for name servers to refer to and fill in the authority
 	 * section or record the address for forwarding the query
 	 * (recursion desired).
 	 */
	free_nsp(nsp);
	nsp[0] = NULL;
	count = 0;
	switch (findns(&np, class, nsp, &count, 0)) {
	case NXDOMAIN:
		/* We are authoritative for this np. */
		if (!foundname)
			hp->rcode = ns_r_nxdomain;
		ns_debug(ns_log_default, 3, "req: leaving (%s, rcode %d)",
			 dname, hp->rcode);
		if (class != C_ANY) {
			hp->aa = 1;
			if (np && (!foundname || !founddata)) {
				n = doaddauth(hp, *cpp, *buflenp, np, nsp[0]);
				*cpp += n;
				*buflenp -= n;
#ifdef ADDAUTH
			} else if (ntohs(hp->ancount) != 0) {
				/* don't add NS records for NOERROR NODATA
				   as some servers can get confused */
				free_nsp(nsp);
				switch (findns(&np, class, nsp, &count, 1)) {
				case NXDOMAIN:
				case SERVFAIL:
					break;
				default:
					if (np &&
					    (type != T_NS || np != anp)
					    ) {
						n = add_data(np, nsp, *cpp,
							     *buflenp, &count);
						if (n < 0) {
							hp->tc = 1;
							n = (-n);
						}
						*cpp += n;
						*buflenp -= n;
						hp->nscount = 
							htons((u_int16_t)
							      count);
					}
				}
#endif /*ADDAUTH*/
			}
		}
		free_nsp(nsp);
		return (Finish);

	case SERVFAIL:
		/* We're authoritative but the zone isn't loaded. */
		if (!founddata &&
		    !(NS_ZOPTION_P(zp, OPTION_FORWARD_ONLY) && 
		      NS_ZFWDTAB(zp))) {
			hp->rcode = ns_r_servfail;
			free_nsp(nsp);
			return (Finish);
		}
	}

	if (!founddata && hp->rd && recursion_blocked_by_acl) {
		ns_notice(ns_log_security,
			  "denied recursion for query from %s for %s %s",
			  sin_ntoa(from), *dname ? dname : ".", p_class(class));
		nameserIncr(from.sin_addr, nssRcvdURQ);
	}

	/*
	 *  If we successfully found the answer in the cache,
	 *  or this is not a recursive query, or we are purposely
	 *  never recursing, or recursion is prohibited by ACL, then
	 *  add the nameserver references("authority section") here
	 *  and we're done.
	 */
	if (founddata || !hp->rd || NS_OPTION_P(OPTION_NORECURSE)
	    || recursion_blocked_by_acl) {
		/*
		 * If the qtype was NS, and the np of the authority is
		 * the same as the np of the data, we don't need to add
		 * another copy of the answer here in the authority
		 * section.
		 */
		if (!founddata || type != T_NS || anp != np) {
			n = add_data(np, nsp, *cpp, *buflenp, &count);
			if (n < 0) {
				hp->tc = 1;
				n = (-n);
			}
			*cpp += n;
			*buflenp -= n;
			hp->nscount = htons(ntohs(hp->nscount) +
						  (u_int16_t)count);
		}
		free_nsp(nsp);

		/* Our caller will handle the Additional section. */
		return (Finish);
	}

	/*
	 *  At this point, we don't have the answer, but we do
	 *  have some NS's to try.  If the user would like us
	 *  to recurse, create the initial query.  If a cname
	 *  is involved, we need to build a new query and save
	 *  the old one in cmsg/cmsglen.
	 */
	if (cname) {
		omsg = (u_char *)memget((unsigned) *msglenp);
		if (omsg == NULL) {
			ns_info(ns_log_default, "ns_req: Out Of Memory");
			hp->rcode = ns_r_servfail;
			free_nsp(nsp);
			return (Finish);
		}
		id = hp->id;
		omsglen = *msglenp;
		memcpy(omsg, msg, omsglen);
		n = res_nmkquery(&res, QUERY, dname, class, type,
				 NULL, 0, NULL, msg,
				 *msglenp + *buflenp);
		if (n < 0) {
			ns_info(ns_log_default, "res_mkquery(%s) failed",
				dname);
			memcpy(msg, omsg, omsglen);
			memput(omsg, omsglen);
			hp->rcode = ns_r_servfail;
			free_nsp(nsp);
			return (Finish);
		}
		*msglenp = n;
	}
	n = ns_forw(nsp, msg, *msglenp, from, qsp, dfd, &qp,
		    dname, class, type, np, 0, in_tsig);
	if (n != FW_OK && cname) {
		memcpy(msg, omsg, omsglen);
		memput(omsg, omsglen);
		*msglenp = omsglen;
		omsg = NULL;
	}
	switch (n) {
	case FW_OK:
		if (cname) {
			qp->q_cname = cname;
			qp->q_cmsg = omsg;
			qp->q_cmsglen = omsglen;
			qp->q_cmsgsize = omsglen;
			qp->q_id = id;
		}
		if (udpsize != 0) {
			qp->q_flags |= Q_EDNS;
			qp->q_udpsize = udpsize;
		} else
			qp->q_udpsize = PACKETSZ;
		break;
	case FW_DUP:
		break;		/* Duplicate request dropped */
	case FW_NOSERVER:
		/* 
		 * Don't go into an infinite loop if 
		 * the admin gave root NS records in the cache
		 * file without giving address records
		 * for the root servers.
		 */
		if (np) {
			if (NAME(*np)[0] == '\0') {
				ns_notice(ns_log_default,
					"ns_req: no address for root server");
				hp->rcode = ns_r_servfail;
				free_nsp(nsp);
				return (Finish);
			}
			for (dp = np->n_data; dp ; dp = dp->d_next)
				if (dp->d_zone && match(dp, class, T_NS))
					break;
			if (dp) {
				/*
				 * we know the child zone exists but are
				 * missing glue.
				 *
				 * nslookup has called sysquery() to get the
				 * missing glue.
				 *
				 * for UDP, drop the response and let the
				 * client retry.  for TCP, we should probably
				 * (XXX) hold open the TCP connection for a
				 * while in case the sysquery() comes back
				 * soon.  meanwhile we SERVFAIL.
				 */
				if (qsp)
					goto do_servfail;
				break;
			}
			np = np_parent(np);
		}
		goto fetchns;	/* Try again. */
	case FW_SERVFAIL:
 do_servfail:
		hp->rcode = ns_r_servfail;
		free_nsp(nsp);
		return (Finish);
	}
	free_nsp(nsp);
	return (Return);
}

static enum req_action
req_iquery(HEADER *hp, u_char **cpp, u_char *eom, int *buflenp,
	   struct sockaddr_in from)
{
	u_int rdata_offset;
	size_t alen;
	int dlen, n;
	ns_type type;
	ns_class class;
	u_char anbuf[PACKETSZ], *anptr;
	char dnbuf[MAXDNAME];

	nameserIncr(from.sin_addr, nssRcvdIQ);

	if (ntohs(hp->ancount) != 1 ||
	    ntohs(hp->qdcount) != 0 ||
	    ntohs(hp->nscount) != 0 ||
	    ntohs(hp->arcount) != 0) {
		ns_debug(ns_log_default, 1,
			 "FORMERR IQuery header counts wrong");
		hp->rcode = ns_r_formerr;
		return (Finish);
	}

	/*
	 * Skip domain name, get class, and type.
	 */
	anptr = *cpp;
	n = dn_skipname(*cpp, eom);
	if (n < 0) {
		ns_debug(ns_log_default, 1,
			 "FORMERR IQuery packet name problem");
		hp->rcode = ns_r_formerr;
		return (Finish);
	}
	*cpp += n;
	if (*cpp + 3 * INT16SZ + INT32SZ > eom) {
		ns_debug(ns_log_default, 1,
			 "FORMERR IQuery message too short");
		hp->rcode = ns_r_formerr;
		return (Finish);
	}
	GETSHORT(type, *cpp);
	GETSHORT(class, *cpp);
	*cpp += INT32SZ;	/* ttl */
	GETSHORT(dlen, *cpp);
	if (*cpp + dlen != eom) {
		ns_debug(ns_log_default, 1,
			 "FORMERR IQuery message length off");
		hp->rcode = ns_r_formerr;
		return (Finish);
	}
	rdata_offset = *cpp - anptr;
	*cpp += dlen;
	INSIST(*cpp == eom);

	/*
	 * Not all inverse queries are handled.
	 */
	if (type != ns_t_a) {
		ns_warning(ns_log_security,
			   "unsupported iquery type from %s",
			   inet_ntoa(from.sin_addr));
		return (Refuse);
	}
	if (dlen != INT32SZ) {
		ns_warning(ns_log_security,
			   "bad iquery from %s",
			   inet_ntoa(from.sin_addr));
		return (Refuse);
	}
	if (!NS_OPTION_P(OPTION_FAKE_IQUERY))
		return (Refuse);

	ns_debug(ns_log_default, 1,
		 "req: IQuery class %d type %d", class, type);

	alen = eom - anptr;
	if (alen > sizeof anbuf) {
		ns_warning(ns_log_security,
			   "bad iquery from %s",
			   inet_ntoa(from.sin_addr));
		return (Refuse);
	}
	memcpy(anbuf, anptr, alen);
	*cpp = anptr;
	*buflenp -= HFIXEDSZ;

#ifdef QRYLOG
	if (qrylog) {
		char tmp[sizeof "255.255.255.255"];

		strcpy(tmp, inet_ntoa(from.sin_addr));
		ns_info(ns_log_queries, "XX /%s/%s/-%s",
			tmp, inet_ntoa(ina_get(&anbuf[rdata_offset])),
			p_type(type));
	}
#endif /*QRYLOG*/

	/*
	 * We can only get here if the option "fake-iquery" is on in the boot
	 * file.
	 *
	 * What we do here is send back a bogus response of "[dottedquad]".
	 * A better strategy would be to turn this into a PTR query, but that
	 * would legitimize inverse queries in a way they do not deserve.
	 */
	sprintf(dnbuf, "[%s]", inet_ntoa(ina_get(&anbuf[rdata_offset])));
	*buflenp -= QFIXEDSZ;
	n = dn_comp(dnbuf, *cpp, *buflenp, NULL, NULL);
	if (n < 0) {
		hp->tc = 1;
		return (Finish);
	}
	*cpp += n;
	*buflenp -= n;
	PUTSHORT((u_int16_t)type, *cpp);
	*buflenp -= INT16SZ;
	PUTSHORT((u_int16_t)class, *cpp);
	*buflenp -= INT16SZ;

	hp->qdcount = htons(1);
	if ((int)alen > *buflenp) {
		hp->tc = 1;
		return (Finish);
	}
	memcpy(*cpp, anbuf, alen);
	*cpp += alen;
	*buflenp -= alen;
	return (Finish);
}

/*
 *  Test a datum for validity and return non-zero if it is out of date.
 */
int
stale(struct databuf *dp) {
	struct zoneinfo *zp = &zones[dp->d_zone];

#ifdef CHECK_MAGIC
	INSIST(dp->d_magic == DATABUF_MAGIC);
#endif

	switch (zp->z_type) {

	case z_master:
		return (0);

#ifdef STUBS
	case z_stub:
		/* root stub zones have DB_F_HINT set */
		if (dp->d_flags & DB_F_HINT)
			return (0);
		/* FALLTROUGH */
#endif
	case z_slave:
		/*
		 * Check to see whether a slave zone has expired or
		 * time warped; if so clear authority flag for zone,
		 * schedule the zone for immediate maintenance, and
		 * return true.
		 */
		if ((int32_t)(tt.tv_sec - zp->z_lastupdate)
		    > (int32_t)zp->z_expire) {
			ns_debug(ns_log_default, 1,
				 "stale: slave zone %s expired",
				zp->z_origin);
			if (!haveComplained((u_long)zp, (u_long)stale)) {
				ns_notice(ns_log_default,
					  "slave zone \"%s\" expired",
					  zp->z_origin);
			}
			zp->z_flags &= ~Z_AUTH;
			if ((zp->z_flags & (Z_QSERIAL|Z_XFER_RUNNING)) == 0) {
				zp->z_time = tt.tv_sec;
				sched_zone_maint(zp);
			}
			return (1);
		}
		if (zp->z_lastupdate > tt.tv_sec) {
			if (!haveComplained((u_long)zp, (u_long)stale)) {
				ns_notice(ns_log_default,
					  "slave zone \"%s\" time warp",
					  zp->z_origin);
			}
			zp->z_flags &= ~Z_AUTH;
			if ((zp->z_flags & (Z_QSERIAL|Z_XFER_RUNNING)) == 0) {
				zp->z_time = tt.tv_sec;
				sched_zone_maint(zp);
			}
			return (1);
		}
		return (0);

	case z_hint:
	case z_cache:
		if (dp->d_flags & DB_F_HINT ||
		    dp->d_ttl >= (u_int32_t)tt.tv_sec)
			return (0);
		ns_debug(ns_log_default, 3, "stale: ttl %d %ld (x%lx)",
			 dp->d_ttl, (long)(dp->d_ttl - tt.tv_sec),
			 (u_long)dp->d_flags);
		return (1);

	default:
		/* FALLTHROUGH */ ;
	}
	panic("stale: impossible condition", NULL);
	/* NOTREACHED */
	return (0);	/* Make gcc happy. */
}

/*
 * Copy databuf into a resource record for replies.
 * Return size of RR if OK, -1 if buffer is full.
 */
int
make_rr(const char *name, struct databuf *dp, u_char *buf,
	int buflen, int doadd, u_char **comp_ptrs, u_char **edp,
	int use_minimum)
{
	u_char *cp;
	u_char *cp1, *sp;
	struct zoneinfo *zp;
	int32_t n;
	int16_t type = dp->d_type;
	u_int32_t ttl;
	u_char naptr_flag;

	ns_debug(ns_log_default, 5,
		 "make_rr(%s, %lx, %lx, %d, %d) %d zone %d ttl %lu",
		 name, (u_long)dp, (u_long)buf,
		 buflen, doadd, dp->d_size, dp->d_zone, (u_long)dp->d_ttl);

	if (dp->d_rcode && dp->d_size == 0)
		panic("make_rr: impossible d_rcode value", NULL);

	zp = &zones[dp->d_zone];
	/* check for outdated RR before updating comp_ptrs[] by dn_comp() */
	if (zp->z_type == Z_CACHE) {
		if ((dp->d_flags & DB_F_HINT) != 0
		    || dp->d_ttl < (u_int32_t)tt.tv_sec) {
			ttl = 0;
		} else
			ttl = dp->d_ttl - (u_int32_t) tt.tv_sec;
	} else {
		if (dp->d_ttl != USE_MINIMUM && !use_minimum)
			ttl = dp->d_ttl;
		else
			ttl = zp->z_minimum;		/* really default */
	}

	buflen -= RRFIXEDSZ;
	if (buflen < 0)
		return (-1);
#ifdef RETURNSOA
	if (dp->d_rcode) {
		name = (char *)dp->d_data;
		name += strlen(name) +1;
		name += strlen(name) +1;
		name += 5 * INT32SZ;
		type = T_SOA;
	}
#endif
	if ((n = dn_comp(name, buf, buflen, comp_ptrs, edp)) < 0)
		goto cleanup;
	cp = buf + n;
	buflen -= n;
	if (buflen < 0)
		goto cleanup;
	PUTSHORT((u_int16_t)type, cp);
	PUTSHORT((u_int16_t)dp->d_class, cp);
	PUTLONG(ttl, cp);
	sp = cp;
	cp += INT16SZ;
	switch (type) {
	case T_CNAME:
	case T_MG:
	case T_MR:
	case T_PTR:
		n = dn_comp((char *)dp->d_data, cp, buflen, comp_ptrs, edp);
		if (n < 0)
			goto cleanup;
		PUTSHORT((u_int16_t)n, sp);
		cp += n;
		break;

	case T_MB:
	case T_NS:
		/* Store domain name in answer */
		n = dn_comp((char *)dp->d_data, cp, buflen, comp_ptrs, edp);
		if (n < 0)
			goto cleanup;
		PUTSHORT((u_int16_t)n, sp);
		cp += n;
		if (doadd) {
			addname((char*)dp->d_data, name,
				type, T_A, dp->d_class);
			addname(name, name, type, T_KEY, dp->d_class);
		}
		break;

	case T_SOA:
	case T_MINFO:
	case T_RP:
		cp1 = dp->d_data;
		n = dn_comp((char *)cp1, cp, buflen, comp_ptrs, edp);
		if (n < 0)
			goto cleanup;
		cp += n;
		buflen -= type == T_SOA ? n + 5 * INT32SZ : n;
		if (buflen < 0)
			goto cleanup;
		cp1 += strlen((char *)cp1) + 1;
		n = dn_comp((char *)cp1, cp, buflen, comp_ptrs, edp);
		if (n < 0)
			goto cleanup;
		cp += n;
		if (type == T_SOA) {
			cp1 += strlen((char *)cp1) + 1;
#ifdef BIND_UPDATE
			if (zp->z_flags & Z_NEED_SOAUPDATE)
				if (incr_serial(zp) < 0)
					ns_error(ns_log_default,
			   "error updating serial number for %s from %d",
						 zp->z_origin, zp->z_serial);
#endif
			n = 5 * INT32SZ;
			memcpy(cp, cp1, n);
			cp += n;
			if (doadd)
				addname(name, name, type, T_KEY, dp->d_class);
		}
		n = (u_int16_t)((cp - sp) - INT16SZ);
		PUTSHORT((u_int16_t)n, sp);
		break;

	case T_NAPTR:
		/* cp1 == our data/ cp == data of RR */
		cp1 = dp->d_data;

 		/* copy order */
		buflen -= INT16SZ;
		if (buflen < 0)
			goto cleanup;
 		memcpy(cp, cp1, INT16SZ);
 		cp += INT16SZ;
 		cp1 += INT16SZ;
		n = (u_int16_t)((cp - sp) - INT16SZ);
		ns_debug(ns_log_default, 1, "current size n = %u", n);

		/* copy preference */
		buflen -= INT16SZ;
		if (buflen < 0)
			goto cleanup;
		memcpy(cp, cp1, INT16SZ);
		cp += INT16SZ;
		cp1 += INT16SZ;
		n = (u_int16_t)((cp - sp) - INT16SZ);
		ns_debug(ns_log_default, 1, "current size n = %u", n);

		/* Flags */
		n = *cp1++;
		ns_debug(ns_log_default, 1, "size of n at flags = %d", n);
		buflen -= n + 1;
		if (buflen < 0)
			goto cleanup;
		naptr_flag = (n == 1) ? *cp1 : 0;
		*cp++ = n;
		memcpy(cp, cp1, n);
		cp += n;
		cp1 += n;
		n = (u_int16_t)((cp - sp) - INT16SZ);
		ns_debug(ns_log_default, 1, "current size n = %u", n);
		
		/* Service */
		n = *cp1++;
		buflen -= n + 1;
		if (buflen < 0)
			goto cleanup;
		*cp++ = n;
		memcpy(cp, cp1, n);
		cp += n;
		cp1 += n;
		n = (u_int16_t)((cp - sp) - INT16SZ);
		ns_debug(ns_log_default, 1, "current size n = %u", n);

		/* Regexp */
		n = *cp1++;
		buflen -= n + 1;
		if (buflen < 0)
			goto cleanup;
		*cp++ = n;
		memcpy(cp, cp1, n);
		cp += n;
		cp1 += n;
		n = (u_int16_t)((cp - sp) - INT16SZ);
		ns_debug(ns_log_default, 1, "current size n = %u", n);

		/* Replacement */
		ns_debug(ns_log_default, 1, "Replacement = %s", cp1);
		n = dn_comp((char *)cp1, cp, buflen, NULL, NULL);
		ns_debug(ns_log_default, 1, "dn_comp's n = %u", n);
		if (n < 0)
			goto cleanup;
		cp += n;
		if (doadd && *cp1 != 0) {
			if (naptr_flag == 's' || naptr_flag == 'S')
				addname((char*)cp1, name, type, T_SRV,
					dp->d_class);
			if (naptr_flag == 'a' || naptr_flag == 'A')
				addname((char*)cp1, name, type, T_A,
					dp->d_class);
		}

		/* save data length */
		n = (u_int16_t)((cp - sp) - INT16SZ);
		ns_debug(ns_log_default, 1, "saved size n = %u", n);
		PUTSHORT((u_int16_t)n, sp);

		break;

	case T_MX:
	case T_AFSDB:
	case T_RT:
	case T_SRV:
		/* cp1 == our data/ cp == data of RR */
		cp1 = dp->d_data;

 		if ((buflen -= INT16SZ) < 0)
			goto cleanup;

 		/* copy preference */
 		memcpy(cp, cp1, INT16SZ);
 		cp += INT16SZ;
 		cp1 += INT16SZ;

		if (type == T_SRV) {
			buflen -= INT16SZ*2;
			if (buflen < 0)
				goto cleanup;
			memcpy(cp, cp1, INT16SZ*2);
			cp += INT16SZ*2;
			cp1 += INT16SZ*2;
		}

		n = dn_comp((char *)cp1, cp, buflen,
			    (type == ns_t_mx) ? comp_ptrs : NULL,
			    (type == ns_t_mx) ? edp : NULL);
		if (n < 0)
			goto cleanup;
		cp += n;

		/* save data length */
		n = (u_int16_t)((cp - sp) - INT16SZ);
		PUTSHORT((u_int16_t)n, sp);
		if (doadd)
			addname((char*)cp1, name, type, T_A, dp->d_class);
		break;

	case T_PX:
		cp1 = dp->d_data;

		if ((buflen -= INT16SZ) < 0)
			goto cleanup;

		/* copy preference */
		memcpy(cp, cp1, INT16SZ);
		cp += INT16SZ;
		cp1 += INT16SZ;

		n = dn_comp((char *)cp1, cp, buflen, comp_ptrs, edp);
		if (n < 0)
			goto cleanup;
		cp += n;
		buflen -= n;
		cp1 += strlen((char *)cp1) + 1;
		n = dn_comp((char *)cp1, cp, buflen, comp_ptrs, edp);
		if (n < 0)
			goto cleanup;
		cp += n;

		/* save data length */
		n = (u_int16_t)((cp - sp) - INT16SZ);
		PUTSHORT((u_int16_t)n, sp);
		break;

	case T_SIG:
		/* cp1 == our data; cp == data of target RR */
		cp1 = dp->d_data;

		/* first just copy over the type_covered, algorithm, */
		/* labels, orig ttl, two timestamps, and the footprint */
		if (buflen < 18)
			goto cleanup;  /* out of room! */
		memcpy(cp, cp1, 18);
		cp  += 18;
		cp1 += 18;
		buflen -= 18;

		/* then the signer's name */
		n = dn_comp((char *)cp1, cp, buflen, NULL, NULL);
		if (n < 0)
			goto cleanup;
		cp += n;
		buflen -= n;
		cp1 += strlen((char*)cp1)+1;

		/* finally, we copy over the variable-length signature */
		n = dp->d_size - (u_int16_t)((cp1 - dp->d_data));
		if (n > buflen)
			goto cleanup;  /* out of room! */
		memcpy(cp, cp1, n);
		cp += n;
		
		/* save data length & return */
		n = (u_int16_t)((cp - sp) - INT16SZ);
		PUTSHORT((u_int16_t)n, sp);
		break;

	case T_NXT:
		cp1 = dp->d_data;
		n = dn_comp((char *)cp1, cp, buflen, NULL, NULL);
		if (n < 0)
			goto cleanup;

		cp += n;
		buflen -=n;
		cp1 += strlen((char *)cp1) + 1; 

		/* copy nxt bit map */
		n = dp->d_size - (u_int16_t)((cp1 - dp->d_data));
		if (n > buflen)
			goto cleanup;  /* out of room! */
		memcpy(cp, cp1, n);
		cp += n;
		buflen -= n;

		n = (u_int16_t)((cp - sp) - INT16SZ);
		PUTSHORT((u_int16_t)n, sp);

		break;

	default:
		if ((type == T_A || type == T_AAAA) && doadd)
			addname(name, name, type, T_KEY, dp->d_class);
		if (dp->d_size > buflen)
			goto cleanup;
		memcpy(cp, dp->d_data, dp->d_size);
		PUTSHORT((u_int16_t)dp->d_size, sp);
		cp += dp->d_size;
	}
	return (cp - buf);

 cleanup:
	/* Rollback RR. */
	ns_name_rollback(buf, (const u_char **)comp_ptrs,
			 (const u_char **)edp);
	return (-1);
}

static void
addname(const char *dname, const char *rname,
	u_int16_t rtype, u_int16_t type, u_int16_t class)
{
	struct addinfo *ap;
	int n;

	for (ap = addinfo, n = addcount; --n >= 0; ap++)
		if (ns_samename(ap->a_dname, dname) == 1 && ap->a_type == type)
			return;

	/* add domain name to additional section */
	if (addcount < NADDRECS) {
		addcount++;
		ap->a_dname = savestr(dname, 1);
		ap->a_rname = savestr(rname, 1);
		ap->a_rtype = rtype;
		ap->a_type = type;
		ap->a_class = class;
	}
}

/*
 * Lookup addresses/keys for names in addinfo and put into the message's
 * additional section.
 */
int
doaddinfo(HEADER *hp, u_char *msg, int msglen) {
	register struct namebuf *np;
	register struct databuf *dp;
	register struct addinfo *ap;
	register u_char *cp;
	struct hashbuf *htp;
	const char *fname;
	register int n, count;
	register int ns_logging;
	int pass = 0;
	int i, doadd;
	

	if (!addcount)
		return (0);

	ns_logging = ns_wouldlog(ns_log_default, 3);

	if (ns_logging)
		ns_debug(ns_log_default, 3,
			"doaddinfo() addcount = %d", addcount);

	if (hp->tc) {
		ns_debug(ns_log_default, 4,
			 "doaddinfo(): tc already set, bailing");
		return (0);
	}

	count = 0;
	cp = msg;
loop:
	for (ap = addinfo, i = 0; i < addcount; ap++, i++) {
		int     auth = 0,
			founda = 0,
			foundaaaa = 0,
			founda6 = 0,
			foundcname = 0,
			save_count = count,
			save_msglen = msglen;
		u_char	*save_cp = cp;

		if ((pass != 0 &&
	             (pass != 1 || server_options->preferred_glue == 0) &&
		     ap->a_type == T_A) ||
		    (pass != 0 && ap->a_type == T_SRV) ||
		    (pass != 2 && ap->a_type == T_KEY))
			continue;
		if (ns_logging)
			ns_debug(ns_log_default, 3,
				 "do additional \"%s\" (from \"%s\")",
				 ap->a_dname, ap->a_rname);
		htp = hashtab;	/* because "nlookup" stomps on arg. */
		np = nlookup(ap->a_dname, &htp, &fname, 0);
		if (np == NULL || fname != ap->a_dname)
			goto next_rr;
		if (ns_logging)
		    ns_debug(ns_log_default, 3, "found it");
		/* look for the data */
		(void)delete_stale(np);
		for (dp = np->n_data; dp != NULL; dp = dp->d_next) {
			if (dp->d_class != ap->a_class)
				continue;
			if (dp->d_rcode == NXDOMAIN) {
				founda = founda6 = foundaaaa = 1;
				continue;
			}
			switch (dp->d_type) {
			case ns_t_a: founda = 1; break;
			case ns_t_a6: founda6 = 1; break;
			case ns_t_aaaa: foundaaaa = 1; break;
			}
			if (!dp->d_rcode && dp->d_type == T_CNAME) {
				foundcname++;
				break;
			}
			if (auth == 0 && ap->a_type == T_A &&
			    (dp->d_type == ns_t_a || dp->d_type == ns_t_a6 ||
			     dp->d_type == ns_t_aaaa) &&
			    (zones[dp->d_zone].z_type == z_master ||
			     zones[dp->d_zone].z_type == z_slave))
				auth = 1;
			if (pass == 0 && ap->a_type == T_A && 
			    server_options->preferred_glue != 0 &&
			    !match(dp, (int)ap->a_class,
				   server_options->preferred_glue)) {
				continue;
			} 
			if (pass != 0 && ap->a_type == T_A &&
			    server_options->preferred_glue != 0 &&
			    match(dp, (int)ap->a_class,
				   server_options->preferred_glue)) {
				continue;
			} 
			if (ap->a_type == T_A &&
			    !match(dp, (int)ap->a_class, T_A) &&
			    !match(dp, (int)ap->a_class, T_AAAA) &&
			    !match(dp, (int)ap->a_class, ns_t_a6)) {
				continue;
			}
			if (ap->a_type == T_KEY &&
			    !match(dp, (int)ap->a_class, T_KEY))
				continue;
			if (ap->a_type == T_SRV &&
			    !match(dp, (int)ap->a_class, T_SRV))
				continue;
			if (dp->d_rcode)
				continue;
			/*
			 *  Should be smart and eliminate duplicate
			 *  data here.	XXX
			 */
			doadd = 0;
			if (ap->a_type == T_SRV)
				doadd = 1;
			if ((n = make_rr(ap->a_dname, dp, cp, msglen, doadd,
					 dnptrs, dnptrs_end, 0)) < 0) {
				/* truncation in the additional-data section
				 * is not all that serious.  we do not set TC,
				 * since the answer and authority sections are
				 * OK; however, since we're not setting TC we
				 * have to make sure that none of the RR's for
				 * this name go out (!TC implies that all
				 * {name,type} appearances are complete -- and
				 * since we only do A RR's here, the name is
				 * the key).	vixie, 23apr93
				 */
				ns_debug(ns_log_default, 5,
			  "addinfo: not enough room, remaining msglen = %d",
					 save_msglen);
				/* Rollback RRset. */
				ns_name_rollback(save_cp,
						 (const u_char **)dnptrs,
						 (const u_char **)dnptrs_end);
				cp = save_cp;
				msglen = save_msglen;
				count = save_count;
				break;
			}
			ns_debug(ns_log_default, 5,
				 "addinfo: adding address data n = %d", n);
			cp += n;
			msglen -= n;
			count++;
		}
 next_rr:
		if (!NS_OPTION_P(OPTION_NOFETCHGLUE) && 
		    !foundcname && ap->a_type == T_A) {
			/* ask a real server for this info */
			if (!founda && !auth)
				(void) sysquery(ap->a_dname, (int)ap->a_class,
						ns_t_a, NULL, NULL, 0, ns_port,
						QUERY, 0);
			if (!foundaaaa && !auth)
				(void) sysquery(ap->a_dname, (int)ap->a_class,
						ns_t_aaaa, NULL, NULL, 0,
						ns_port, QUERY, 0);
			if (!founda6 && !auth)
				(void) sysquery(ap->a_dname, (int)ap->a_class,
						ns_t_a6, NULL, NULL, 0, ns_port,
						QUERY, 0);
		}
		if (foundcname) {
			if (!haveComplained(nhash(ap->a_dname),
					    nhash(ap->a_rname))) {
				ns_info(ns_log_cname,
					"\"%s %s %s\" points to a CNAME (%s)",
					ap->a_rname, p_class(ap->a_class),
					p_type(ap->a_rtype), ap->a_dname);
			}
		}
	}
	if (pass++ < 2)
		goto loop; /* now do the KEYs... */
	hp->arcount = htons((u_int16_t)count);
	for (ap = addinfo, i = 0; i < addcount; ap++, i++) {
		ap->a_dname = freestr(ap->a_dname);
		ap->a_rname = freestr(ap->a_rname);
	}
	addcount = 0;
	return (cp - msg);
}

int
doaddauth(HEADER *hp, u_char *cp, int buflen,
	  struct namebuf *np, struct databuf *dp)
{
	char dnbuf[MAXDNAME];
	int n;

	getname(np, dnbuf, sizeof dnbuf);
	if (stale(dp)) {
		ns_debug(ns_log_default, 1,
			 "doaddauth: can't add stale '%s' (%d)",
			dnbuf, buflen);
		return (0);
	}
	n = make_rr(dnbuf, dp, cp, buflen, 1, dnptrs, dnptrs_end, 1);
	if (n <= 0) {
		ns_debug(ns_log_default, 1,
			 "doaddauth: can't add oversize '%s' (%d) (n=%d)",
			 dnbuf, buflen, n);
		if (n < 0) {
			hp->tc = 1;
		}
		return (0);
	}
	if (dp->d_secure != DB_S_SECURE)
		hp->ad = 0;
	hp->nscount = htons(ntohs(hp->nscount) + 1);
	return (n);
}

void
free_addinfo() {
	struct addinfo *ap;

	for (ap = addinfo; --addcount >= 0; ap++) {
		ap->a_dname = freestr(ap->a_dname);
		ap->a_rname = freestr(ap->a_rname);
	}
	addcount = 0;
}

void
free_nsp(struct databuf **nsp) {
	while (*nsp)
		db_detach(nsp++);
}

static void
copyCharString(u_char **dst, const char *src) {
	size_t len = strlen(src) & 0xff;
	*(*dst)++ = (u_char) len;
	memcpy(*dst, src, len);
	*dst += len;
}

/*
 * Questionable source ports for queries / responses.
 */
int
drop_port(u_int16_t port) {
	switch (port) {
	case 7: /* echo */
	case 13: /* daytime */
	case 19: /* chargen */
	case 37: /* time */
		return (1);
	case 464: /* kpasswd */
		return (2);
	}
	return (0);
}
OpenPOWER on IntegriCloud