~samiam/MaraDNS

MaraDNS/server/MaraDNS.c.orig -rw-r--r-- 161.2 KiB View raw
ccb94b01Sam Trenholme Dockerfile now installs MaraDNS in Docker image. 22 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
/* Copyright (c) 2002-2011 Sam Trenholme
 *
 * TERMS
 *
 * 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.
 *
 * This software is provided 'as is' with no guarantees of correctness or
 * fitness for purpose.
 */

/* This is the core DNS server */

/* Language specific labels */
#include "MaraDNS_locale.h"

/* Include stuff needed to be a UDP server */

#include "../libs/MaraHash.h"
#include "../MaraDns.h"
#include "../qual/qual_timestamp.h"
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#ifndef MINGW32
#include <grp.h>
#endif
#include <fcntl.h>
#ifdef __FreeBSD__
#include <sys/time.h>
#endif
#include <sys/types.h>
#ifndef DARWIN
#endif
#ifndef MINGW32
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <winsock.h>
#include <wininet.h>
#endif
#include "../dns/functions_dns.h"
#include "../parse/functions_parse.h"
#include "../parse/Csv2_database.h"
#include "../parse/Csv2_read.h"
#include "../parse/Csv2_functions.h"
#include "functions_server.h"
#include "timestamp.h"
#include "read_kvars.h"

/* Virutal memory limit */
#ifdef RLIMIT_AS
#define MAX_MEM RLIMIT_AS
#else /* RLIMIT_AS */
#ifdef RLIMIT_VMEM
#define MAX_MEM RLIMIT_VMEM
#endif /* RLIMIT_VMEM */
#endif /* RLIMIT_AS */

/* Our global variables */
mhash *bighash;
int log_level = 1; /* 0: No messages except for fatal errors which stop
                         MaraDNS, 1: Startup and shutdown messages,
                  2: Log queries that generate errors, 3: Log all queries */
int no_fingerprint = 0; /* 0: Have some MaraDNS-specific features, such as
                              DDIP expansion and a special query that
                              tells you the version number of the server.
                           1: Attempts to have as generic an interface as
                              possible. */
int rrany_set = 3; /* (Determined with default_rrany_set in mararc file)
                      3: A request for RR_ANY will only return A and MX
                         records for a given node.
                      15: A request for RR_ANY will return A, MX, NS, and
                         SOA records */
int max_ar_chain = 1; /* Maximum number of records we show of a chain
                         of A records in the additional section of an
                         answer */
int max_chain = 8; /* Total maximum number of records in any chain of
                      records */
int max_total = 20; /* Total maximum number of records we will show */
int debug_delay = 0; /* Delay before sending a reply; only used for
                        debugging MaraDNS */
int default_zonefile_enabled = 0; /* Whether a default zone file is
                                     enabled (whether we are allowing
                                     stars at end of hostnames) */
int no_cname_warnings = 0; /* Whether to supress warnings about dangling
                              CNAMES or not */
int min_visible_ttl = 30; /* The minimum TTL we will show the user */

/* Some variables used to assist in the housekeeping making sure we
   do not display a given RR in the Additional records section twice */
rr *seenlist[256];
int seenlist_where = 0;

int total_count = 0; /* This has to be global to handle udpany's use of it */

int debug_msg_level = 1; /* The level of debug messages to allow */

int reject_aaaa = 0; /* Whether to send a bogus SOA (not there) every time
                        someone asks for an AAAA (works around problems
                        with RH7.2 resolver library) */
int reject_ptr = 0; /* Whether to send a bogus SOA (not there) every time
                       someone asks for a PTR */

rr *ra_data = 0; /* Bogus SOA to give out whenever a SOA request is sent */

/*int rd_value = 0;*/ /* Whether RD is set to 0 or 1 in replies to
                         authoritative queries; disabled because you
                         shouldn't use global variables in threaded
                         code */

int dos_protection_level = 0; /* How many features of MaraDNS we're willing
                                 to give up to make her more immune to
                                 Denial of Service attacks */

int bind_star_handling = 0; /* Handle star records the same way BIND does */

int remote_admin = 0; /* Whether remote administration (currently,
                         changing the verbose_level while MaraDNS is
                         running is the only thing that can be changed
                         remotely) is allowed.  Default off */

int force_auth = 1; /* Whether any non-NS delegation record returned
                     * from the authoritative half is always marked
                     * authoritative */

int dns_port = 53; /* The default port for the server to listen on */

int csv2_tilde_handling = 2; /* How to parse tildes in CSV2 zone files */

int recurse_delegation = 0; /* Whether MaraDNS will recurse when we would
                             * otherwise give out a NS delegation entry */

int dns_records_served = 0; /* The number of DNS records we are serving */

js_string *notthere_ip = 0; /* The Class + TTL (0) + Rdlength (4)
                             * + IP (The IP the user wants) for synthetic
                             * IPs when they mistype a domain name or some
                             * such (Sponsored by XeroBank). */

int recursion_enabled = 0; /* Whether we have recursion */

/* A list of who is and who is not allowed to make recursive DNS queries */
ipv4pair recurse_acl[512];
/* A list of the ipv4 IP addresses we bind MaraDNS to (the netmask portion is
   ignored) */
ipv4pair bind_addresses[512];
/* A list which we will use just in case we need a different list of ips
 * in the synthetic NS records than the list of IPs we're bound to (also
 * useful for binding to "0.0.0.0" for people who don't mind the bugs that
 * causes */
ipv4pair csv2_synthip_list[512];
#ifdef AUTHONLY
/* A list of the ips which we can send long packets to */
ipv4pair long_packet[512];
#endif

/* A list of IPs allowed to administrate MaraDNS (See MaraDNS' version
 * number, see how many threads are running, the number of processes running,
 * etc.). */
ipv4pair admin_acl[512];

/* Some global variables so that the user can change the SOA origin (MINFO)
 * and the format of the SOA serial number if needed */
js_string *synth_soa_origin = 0;
int synth_soa_serial = 1;
/* Some routines so we can see the above variables */
js_string *show_synth_soa_origin() {
        return synth_soa_origin;
}
int show_synth_soa_serial() {
        return synth_soa_serial;
}

/* Define log_lock(); and log_unlock(); for authonly use */
#ifdef AUTHONLY
#define log_lock()
#define log_unlock()
#endif /* AUTHONLY */

/* Debug routine that shows an IP in dotted decimal format on the
   standard output.
   Input:  A uint32 ip
   Output: none
*/
void debug_show_ip(uint32 ip) {
    printf("%d.%d.%d.%d",(ip & 0xff000000) >> 24,
                         (ip & 0x00ff0000) >> 16,
                         (ip & 0x0000ff00) >>  8,
                          ip & 0x000000ff);
    }

/* This displays ipv6 ips; thanks Remmy */

#ifdef IPV6
/* Cygwin doesn't have ipv6 yet */
#ifndef __CYGWIN__
#ifndef MINGW32
void debug_show_socket_ipv6(struct sockaddr_in6 *socket) {
        /* Disabled because it currently doesn't work */
        /* printf(" ipv6 "); return; */
        /* This looks broken */
        char hostname[INET6_ADDRSTRLEN];
        printf("%s", inet_ntop(AF_INET6, &socket->sin6_addr, hostname, sizeof(hostname)));
}
#endif /* MINGW32 */
#endif /* __CYGWIN__ */
#endif /* AUTHONLY */

/* Signal handler for SIGPIPE, so we don't terminate */
void handle_sigpipe() {
    if(log_level > 1) {
        log_lock();
        printf("%s%s",L_CAUGHT_PIPE,L_N); /* "Caught SIGPIPE" */
        log_unlock();
        }
    return;
    }

int got_hup_signal = 0;

/* Signal handler for HUP signal */
void handle_hup() {
    got_hup_signal = 1;
    return;
    }

/* Signal handler for other signals */
void handle_signal() {
    if(log_level > 1) {
        log_lock();
        printf("%s%s",L_CAUGHT_SIG,L_N); /* "Caught Signal" */
        log_unlock();
        }
    return;
    }

#ifdef DEBUG
/* Signal handler which tells us about unfreed memory before exiting */
void display_unfreed() {
    if(log_level > 0)
        js_show_leaks();
    exit(64);
    }
#endif /* DEBUG */

/* Print out log messages
   Input: Null-terminated string with the message to log
   Output: JS_SUCCESS on success, JS_ERROR on error
*/

int mlog(char *logmessage) {

    if(log_level == 0)
        return JS_SUCCESS;

    if(logmessage == 0)
        return JS_ERROR;
    log_lock();
    show_timestamp();
    printf("%s%s%s",L_LOG,logmessage,L_N);
          /* "Log: ", logmessage, "\n" */

    /* Unbuffered output */
    fflush(stdout);
    log_unlock();

    return JS_SUCCESS;
    }

/* Print out log messages of js_string messages
   Input: js_string object to log
   Output: JS_SUCCESS on success, JS_ERROR on error
*/

int jlog(js_string *logmessage) {

    int ret;

    if(log_level == 0)
        return JS_SUCCESS;

    log_lock();
    printf("%s",L_LOG); /* "Log: " */
    ret = show_esc_stdout(logmessage);
    printf("%s",L_N); /* "\n" */

    /* Unbuffered output */
    fflush(stdout);
    log_unlock();

    return ret;
    }

/* Print out log message of a Null-terminated string followed by a js_string
   Input: Null-terminated string, js_string
   Output: JS_SUCCESS on success, JS_ERROR on error */

int zjlog(char *left, js_string *right) {
    int ret;
    if(log_level == 0)
        return JS_SUCCESS;
    if(left == 0)
        return JS_ERROR;
    log_lock();
    printf("%s%s",L_LOG,left); /* "Log: ", left */
    ret = show_esc_stdout(right);
    printf("%s",L_N); /* "\n" */

    /* Unbuffered output */
    fflush(stdout);
    log_unlock();

    return ret;
    }

/* Handler to handle fatal errors.
   Input: Pointer to null-terminalted string with fatal error
   Output: MaraDNS exits
*/

void harderror(char *why) {
    printf("%s%s%s",L_FATAL,why,L_N); /* "Fatal Error: ", why, "\n" */

    /* Unbuffered output */
    fflush(stdout);

    exit(3);
    }

/* Handler to handle system fatal errors.
   Input: Pointer to null-terminalted string with fatal error
   Output: MaraDNS exits
*/

void sys_harderror(char *why) {
    printf("%s%s%s",L_FATAL,why,L_N); /* "Fatal Error: ", why, "\n" */
    printf("%s: %s%s",L_SYSERROR,strerror(errno),L_N);
    /* This outputs to stderr, which duende can not catch (I gave up
       trying to catch stderr messages after trying for two days) */
    /*perror(L_SYSERROR);*/ /* "System said: " */

    /* Unbuffered output */
    fflush(stdout);

    exit(3);
    }

/* This function returns an appropriate RA (Recursion available) value.
 * If the user has not set "recursive_acl", this will always return 0.
 * If the argument give to this is 0, return zero.  Otherwise, return 1
 */

int calc_ra_value(int want_ra) {
#ifdef AUTHONLY
        return 0;
#else /* AUTHONLY */
        if(recursion_enabled == 0) {
                return 0;
        }
        return want_ra;
#endif /* AUTHONLY */
}

/* This function prepares the notthere_ip string so that it we can quickly
 * make synthetic IPs.  Basically, the string is most of the DNS header and
 * data for the generated synthetic IP, in this form:
 *
 * Dname: 2-byte compression pointer to question (0xc00c)
 * Class (16-bit): 1 (Internet)
 * TTL: 0 (Not to be cached)
 * Rdlength: 4 (4-byte IP)
 * Rddata: The dotted-decimal IP given to the function converted in to raw
 *         binary form.
 *
 * Input: A js_string containing the dotted-decimal IP we will convert
 * Output: A js_string containing the above raw data
 */

js_string *make_notthere_ip(js_string *ddip) {
        js_string *out = 0;
        js_string *ip = 0;
        out = js_create(19,1);
        if(out == 0) {
                return 0;
        }
        ip = js_create(10,1);
        if(ip == 0) {
                js_destroy(out);
                return 0;
        }
        if(js_adduint16(out,0xc00c) == JS_ERROR || /* Hostname (compressed) */
           js_adduint16(out,1) == JS_ERROR || /* TYPE (A) */
           js_adduint16(out,1) == JS_ERROR || /* CLASS */
           js_adduint16(out,0) == JS_ERROR || /* TTL pt. 1 */
           js_adduint16(out,0) == JS_ERROR || /* TTL pt. 2 */
           js_adduint16(out,4) == JS_ERROR) { /* Rdlength */
                js_destroy(out);
                js_destroy(ip);
                return 0;
        }
        if(ddip_2_ip(ddip,ip,0) == JS_ERROR) {
                js_destroy(out);
                js_destroy(ip);
                return 0;
        }
        if(js_append(ip,out) == JS_ERROR) {
                js_destroy(out);
                js_destroy(ip);
                return 0;
        }
        js_destroy(ip);
        return out;
}

/* Calculate the TTL age given the expire time (absolute time) and
   the ttl (relative time)
   Input: Exprire time, TTL in question
   Output: The TTL we should give, taking TTL aging in to account
 */

uint32 determine_ttl(qual_timestamp expire,uint32 ttl) {
    qual_timestamp now;

    if(expire == 0) {
        return ttl;
        }
    now = qual_get_time();

    /* If this is a record being resurrected from the expired records, we
       make the TTL 29 seconds */
    if(expire < (now - 10)) {
        return 29;
        }

    if(expire - now > min_visible_ttl) {
        return expire - now;
        }
    return min_visible_ttl;
    }

/* Given a JS_STRING object with a DNS query (starting with the header)
 * in it, determine what the RD bit in that header is. */
int get_header_rd(js_string *query) {
        if(js_has_sanity(query) == JS_ERROR) {
                return 0;
        }
        if(query->unit_size != 1) {
                return 0;
        }
        if(query->unit_count < 3 || query->max_count < 3) {
                return 0;
        }
        return *(query->string + 2) & 0x01;
}

/* This function takes a conn *ect (a MaraDNS-specific description of a
 * connection that can be the IP and port of either a ipv4 or ipv6
 * connection), a socket number, and a js_string to send, and sends
 * a message over the 'net */
int mara_send(conn *ect, int sock, js_string *reply) {
        if(ect == 0 || reply == 0) {
                return JS_ERROR;
        }
        if(ect->type == 4) {
                sendto(sock,reply->string,reply->unit_count,0,
                                (struct sockaddr *)ect->d,ect->addrlen);
                return JS_SUCCESS;
#ifdef IPV6
/* Cygwin doesn't have ipv6 yet */
#ifndef __CYGWIN__
        } else if(ect->type == 6) {
                sendto(sock,reply->string,reply->unit_count,0,
                                (struct sockaddr *)ect->d,ect->addrlen);
                return JS_SUCCESS;
#endif /* __CYGWIN__ */
#endif
        } else {
                return JS_ERROR;
        }
}

/* Return a packet indicating that there was an error in the received
   packet
   input: socket number,
          a js_string object that we get the data from the first two
          bytes from, a sockaddr of who to send the error to,
          the question the error-generating query asked, the error
          to give them in the RCODE part of the header,
          the reason for the error, the minimim log_level to log this
          error (with reason) with
   output: JS_ERROR on error, JS_SUCCESS on success
*/

int udperror(int sock,js_string *raw, struct sockaddr_in *from,
             js_string *question, int error,
             char *why,int min_log_level, int rd_val, conn *ect,int log_msg) {

    q_header header;
    js_string *reply;
    int len_inet = sizeof(struct sockaddr);

    if(log_level >= min_log_level && log_msg == 1) {
        show_timestamp();
        zjlog(L_BAD_QUERY,raw); /* "Bad query received: " */
        if(ect != 0 && ect->type == 4) {
            struct sockaddr_in *clin = 0;
            clin = (struct sockaddr_in *)(ect->d);
            printf("From IP: ");
            debug_show_ip(ntohl(clin->sin_addr.s_addr));
            printf("\n");
            }
        else if(from != 0) {
            printf("From IP: ");
            debug_show_ip(ntohl(from->sin_addr.s_addr));
            printf("\n");
            }
        }
    if(log_level >= 2) /* Tell them why */
        mlog(why);

    if(raw->unit_count < 2 || raw->max_count < 3)
        return JS_ERROR;

    if((reply = js_create(96,1)) == 0)
        return JS_ERROR;

    /* Fill out the header */
    header.id = ((*(raw->string) & 0xff) << 8) | (*(raw->string + 1) & 0xff);
    header.qr = 1;
    header.opcode = 0;
    header.aa = 0; /* Errors are never authoritative (unless they are
                      NXDOMAINS, which this is not) */
    header.tc = 0;
    header.rd = rd_val; /* RDBUG udperror */
    header.ra = 0;
    header.z = 0;
    header.rcode = error;
    if(question == 0)
        header.qdcount = 0;
    else
        header.qdcount = 1;
    header.ancount = 0;
    header.nscount = 0;
    header.arcount = 0;

    /* Make that raw UDP data */
    if(make_hdr(&header,reply) == JS_ERROR) {
        js_destroy(reply);
        return JS_ERROR;
    }

    /* Append the question, if there is one */
    if(question != 0) {
       if(js_append(question,reply) == JS_ERROR) {
           js_destroy(reply);
           return JS_ERROR;
           }
       if(js_adduint16(reply,1) == JS_ERROR) {
           js_destroy(reply);
           return JS_ERROR;
           }
       }

    /* Send them the reply */
    if(ect == 0) {
        sendto(sock,reply->string,reply->unit_count,0,
            (struct sockaddr *)from,len_inet);
    } else {
        mara_send(ect,sock,reply);
    }
    js_destroy(reply);
    return JS_SUCCESS;

    }

/* If we successfully found a record, add the answer to the A record,
   if applicable, add the NS data too, and add the appropriate
   additional records.
   Input: Where a pointer to the rr in question is, a pointer to the
          string where we add answers, pointer to ns data string, a pointer
          to the string where we add additional records, a pointer to the
          number containing the number of answers, a pointer to the
          number containing the number of authority records, a pointer to
          the number containing the number of additional records, whether
          to add records to the authority (ns) section, the real query name
          (used in the case of star records, otherwise 0.)
   Output: JS_ERROR on error, JS_SUCCESS on success

   NOTE: This routine is only called from udpany and is only used to
         process ANY queries.
*/

int add_answer(rr *where,js_string *most, js_string *ns, js_string *ar,
               uint16 *ancount, uint16 *nscount, uint16 *arcount,
               int add_ns, void **rotate_point, js_string *starwhitis,
               unsigned char max_answers) {

    uint16 first_rr_type;
    rr *ipwhere = 0;
    int in_ns = 0;
    int a_count = 0; /* Number of records displayed for a given chain */
    int ar_count = 0; /* Number of records displayed for a chain in the AR
                         section */
    int rotate_done = 0; /* We only rotate data once per call
                            to this function */
    /* The following are used for round robin rotation */
    rr *rotate_1st = 0, *rotate_2nd = 0, *rotate_last = 0;

    /* Sanity check */
    if(where == 0) {
        goto giveerror;
        }
    if(where->query == 0) {
        goto giveerror;
        }
    if(js_has_sanity(where->query) == JS_ERROR) {
        goto giveerror;
        }
    if(where->data == 0) {
        goto giveerror;
        }
    if(js_has_sanity(where->data) == JS_ERROR) {
        goto giveerror;
        }
    if(starwhitis != 0 && js_has_sanity(starwhitis) == JS_ERROR) {
            goto giveerror;
    }
    first_rr_type = get_rtype(where->query);

    /* The data must be between 0 and 65535 bytes in length (16-bit
       unsigned value) */
    if(where->data->unit_count < 0 || where->data->unit_count > 65535) {
        goto giveerror;
        }

    /* Initialize some temporary pointers used for round robin rotation */
    rotate_1st = where;
    rotate_2nd = where->next;
    /* We do not round robin if there is but a single record */
    if(rotate_2nd != 0 && first_rr_type != RR_NS &&
       rotate_2nd->rr_type == RR_NS)
        rotate_2nd = 0;

    /* OK, we now add the answers */
    while(where != 0) {
        /* Increment the number of answers -or- ns records */
        if(first_rr_type != RR_NS && where->rr_type == RR_NS &&
                        max_answers == 0) {

            /* Due to the data structure MaraDNS currently uses, the behavior
               is buggy if we round-robin rotate data when we allow more than
               one additional record to be create per answer/authoritative
               record.  */
            if(rotate_2nd != 0 && max_ar_chain == 1 && rotate_done == 0
               && first_rr_type != RR_NS) {
                rotate_done = 1;
                /* If it makes sense to do a round-robin rotation */
                rotate_1st->next = where;
                rotate_last->next = rotate_1st;
                *rotate_point = rotate_2nd;
                }

            a_count = 0; /* The NS chain is different than the AN
                            chain of answers: If we only allow eight
                            answers in a chain, we can still have 16
                            answers: 8 records in the answer section then
                            8 records in the authority section */
            if(add_ns == 1)
                in_ns = 1;
            else
                return JS_SUCCESS;
            }
        /* Add an answer record */
        if(!in_ns && a_count < max_chain && total_count < max_total) {
            a_count++; /* Counter so we don't exceed the maximum number
                          of records allowed to be seen in a chain */
            total_count++;
            /* This allows us to have multiple records for a given RTYPE
               when an ANY query is made */
            if(where->rr_type == RR_NS && first_rr_type != RR_NS &&
               max_answers != 0) {
                in_ns = 1;
                goto max_answers_skip;
                }
            (*ancount)++; /* This goes in the header of the reply */
            /* Append the name for this answer to the answer */
            if(starwhitis == 0 && /* We don't do following if this fails */
                            js_append(where->query,most) == JS_ERROR) {
                goto giveerror;
            }
            else if(starwhitis != 0 &&
                            js_append(starwhitis,most) == JS_ERROR) {
                    goto giveerror;
            }
            /* Append the class (in) to the answer */
            if(js_adduint16(most,1) == JS_ERROR) {
                goto giveerror;
                }
            /* Append the ttl to the answer */
            if(js_adduint32(most,determine_ttl(where->expire,where->ttl))
                == JS_ERROR) {
                goto giveerror;
                }
            /* Add the rdlength to the answer */
            if(js_adduint16(most,where->data->unit_count) == JS_ERROR) {
                goto giveerror;
                }
            /* Add the record itself to the answer */
            if(js_append(where->data,most) == JS_ERROR) {
                goto giveerror;
                }
max_answers_skip:
            if(max_answers == 1) {
                return JS_SUCCESS;
                }
            }
        else if(a_count < max_chain && total_count < max_total &&
                max_answers == 0) {
            a_count++; total_count++; /* The counters that make sure we do
                                         not have more than, say, eight
                                         records for a given answer */
            /* Append the name for this answer to the answer */
            if(js_append(where->query,ns) == JS_ERROR) {
                goto giveerror;
                }
            /* Append the class (in) to the answer */
            if(js_adduint16(ns,1) == JS_ERROR) {
                goto giveerror;
                }
            /* Append the ttl to the answer */
            if(js_adduint32(ns,determine_ttl(where->expire,where->ttl))
                == JS_ERROR) {
                goto giveerror;
                }
            /* Add the rdlength to the answer */
            if(js_adduint16(ns,where->data->unit_count) == JS_ERROR) {
                goto giveerror;
                }
            /* Add the record itself to the answer */
            if(js_append(where->data,ns) == JS_ERROR) {
                goto giveerror;
                }
            (*nscount)++;
            }
        /* If there is an IP, and this is *not* a CNAME record,
           append the IP of the answer to the AR section */
        if(where->ip != 0 && where->rr_type != RR_CNAME && max_answers == 0) {
            ipwhere = where->ip;
            ar_count = 0; /* Reset for each instance of showing AR
                             records */
            while(ipwhere != 0 && ipwhere->rr_type != RR_NS &&
                  ar_count < max_ar_chain && total_count < max_total) {
                ar_count++; /* Counter so we don't exceed maximum number
                               of AN records allowed to be displayed */
                total_count++; /* Similar to ar_count */
                /* We only show a given additional record once */
                if(ipwhere->seen == 1) { /* If we have displayed this RR
                                            already */
                    /* Go to the next link in the linked list */
                    ipwhere = ipwhere->next;
                    continue;
                    }
                /* Increment the number of additional records */
                (*arcount)++;
                /* Append the name for this answer to the ip */
                if(js_append(ipwhere->query,ar) == JS_ERROR) {
                    goto giveerror;
                    }
                /* Append the class (in) to the ip */
                if(js_adduint16(ar,1) == JS_ERROR) {
                    goto giveerror;
                    }
                /* Append the TTL to the ip */
                if(js_adduint32(ar,determine_ttl(ipwhere->expire,ipwhere->ttl))
                   == JS_ERROR) {
                    goto giveerror;
                    }
                /* Add the rdlength to the ip */
                if(js_adduint16(ar,ipwhere->data->unit_count) == JS_ERROR) {
                    goto giveerror;
                    }
                /* Add the record itself to the ip */
                if(js_append(ipwhere->data,ar) == JS_ERROR) {
                    goto giveerror;
                    }
                /* Mark that we have seen this record already */
                if(seenlist_where < 250) {
                    ipwhere->seen = 1;
                    seenlist[seenlist_where] = ipwhere;
                    seenlist_where++;
                    }
                ipwhere = ipwhere->next;
                }
            }

        /* We do not chase CNAME records in an "RR_ALL" query */

        /* Make a note of this node for round-robin rotation purposes */
        rotate_last = where;
        /* Go on to the next record in the linked list */
        where = where->next;
        }

    return JS_SUCCESS;

    /* We use gotos to make up for C's lack of error trapping */
    giveerror:
        return JS_ERROR;

    }

/* If they asked for a RR_ANY record in the authoritative half, see if
   we have a special RR_ANY record in the cache which is a linked list to
   all of the records types for a given domain node.  If so, give them
   an answer; otherwise give them a NXDOMAIN.

   If they ask for an RR_ANY in the recursive half, do the following:

   1. See if MX records exist.  If so, add it to the answer to give.

   2. See if A records record exist.  If so, add it.

   3. If neither A nor MX exist, look for a CNAME record.  If so,
      return just the CNAME record.

   4. Otherwise, return "query denied".

   Input: ID of the iquery they sent us, socket of the request, a sockaddr
          with their address and port number on it, a js_string containing
          the query (dname + type), the rr_set to return (3: A and MX,
          15: A, MX, SOA, and NS), the hash to look for data in, the
          RD value to set in the headers of the reply, the connection
          this is on, whether this is called from the recursive code
          or not.
   Output: JS_ERROR on error, JS_SUCCESS on success, 0 if no records were
           found
*/

int udpany(int id,int sock,struct sockaddr_in *client, js_string *query,
           int rr_set, mhash *bighash, int rd_val, conn *ect, int
           called_from_recursive, js_string *origq) {
    js_string *most, *ns, *ar; /* The answers, the ns records, the ar records*/

    js_string *starwhitis;

    int length_save;
    int len_inet = sizeof(struct sockaddr);
    int found = 0;
    int authoritative = 1;
    rr_list *answer = 0;
    mhash_e spot_data;
    int counter;

    q_header header;
    /* Initialize the js_string objects */
    if((most = js_create(1024,1)) == 0)
        return JS_ERROR;
    if((ar = js_create(1024,1)) == 0) {
        js_destroy(most);
        return JS_ERROR;
        }
    if((ns = js_create(1024,1)) == 0) {
        js_destroy(most); js_destroy(ar);
        return JS_ERROR;
        }

    /* Initialize the total number of RRs displayed to the DNS client */
    total_count = 0;

    /* Make the header a placeholder for now */
    header.id = id;
    header.rd = rd_val; /* RDBUG udpany */
    if(make_hdr(&header,most) == JS_ERROR)
        goto giveerror;

    /* Append the question to the answer */
    if(origq == 0) {
        if(js_append(query,most) == JS_ERROR) {
            goto giveerror;
            }
    } else {
        if(js_append(origq,most) == JS_ERROR) {
            goto giveerror;
            }
    }
    /* Append the class (in) to the answer */
    if(js_adduint16(most,1) == JS_ERROR) {
        goto giveerror;
        }

    /* We will increment the ancount, nscount, an arcount, starting at 0 */
    header.ancount = 0;
    header.nscount = 0;
    header.arcount = 0;

    /* Start synthesizing the reply */
    /* Look for the list of all of the answers */
    spot_data = mhash_get(bighash,query);
    /* If found, use this list for all of the answers */
    if(spot_data.value != 0 && spot_data.datatype == MARA_DNS_LIST) {
        answer = (rr_list *)spot_data.value;
        for(counter = 0; counter < 100; counter++) {
            if(add_answer(answer->data,most,ns,ar,&(header.ancount),
                   &(header.nscount),&(header.arcount),1,
                   spot_data.point,0,2) == JS_ERROR) {
                goto giveerror;
                }
            if(answer->rr_type != RR_NS) {
                found = 1;
            }
            answer = answer->next;
            if(answer == 0)
                    break;
            }
        goto old_udpany_code_disabled;
        }

    /* OK, no record found, so look for a star record */
    if((starwhitis = js_create(256,1)) == 0) {
        goto giveerror;
        }
    if(js_copy(query,starwhitis) == JS_ERROR) {
        js_destroy(starwhitis);
        goto giveerror;
        }
    if(make_starlabel(starwhitis) == JS_ERROR) {
        js_destroy(starwhitis);
        goto giveerror;
        }

    spot_data = mhash_get(bighash,starwhitis);
    if(spot_data.value != 0 && spot_data.datatype == MARA_DNS_LIST) {
        int rtype_save;
starwhitis_any_found:
        answer = (rr_list *)spot_data.value;
        found = 1;
        for(counter = 0; counter < 100; counter++) {
            rtype_save = get_rtype(query);
            if(change_rtype(query,answer->data->rr_type) == JS_ERROR) {
                js_destroy(starwhitis);
                goto giveerror;
                }
            if(add_answer(answer->data,most,ns,ar,&(header.ancount),
                   &(header.nscount),&(header.arcount),1,
                   spot_data.point,query,2) == JS_ERROR) {
                js_destroy(starwhitis);
                goto giveerror;
                }
            answer = answer->next;
            if(answer == 0)
                    break;
            }
        if(change_rtype(query,rtype_save) == JS_ERROR) {
            js_destroy(starwhitis);
            goto giveerror;
            }
        js_destroy(starwhitis);
        goto old_udpany_code_disabled;
        }

    counter = 0;
    while(bobbit_starlabel(starwhitis) == JS_SUCCESS) {
        spot_data = mhash_get(bighash,starwhitis);
        if(spot_data.value != 0 && spot_data.datatype == MARA_DNS_LIST) {
            goto starwhitis_any_found;
            }
        counter++;
        if(counter > 100) {
            break;
            }
        }

    /* Look for ANY records in a possible csv2_default_zonefile */
    if(default_zonefile_enabled == 1) {
        int labels;
        /* Restore original query, since we are now chopping it up at
           the end instead of the beginning of the record. */
        if(js_copy(query,starwhitis) == JS_ERROR) {
            js_destroy(starwhitis);
            goto giveerror;
            }
        counter = 0;
        while((labels = bobbit_starlabel_end(starwhitis)) > 0) {
            /* limit the number of labels */
            if(labels > 120) {
                continue;
                }
            spot_data = mhash_get(bighash,starwhitis);
            if(spot_data.value != 0 && spot_data.datatype == MARA_DNS_LIST) {
                goto starwhitis_any_found;
                }
            counter++;
            if(counter > 120) {
                break;
                }
            }
        }

    js_destroy(starwhitis);
    goto old_udpany_code_disabled;

old_udpany_code_disabled:

    /* Return with exit code of 0 if no answer was found */
    if(header.ancount == 0 || found == 0) {
        js_destroy(ar);
        js_destroy(ns);
        js_destroy(most);
        return 0;
        }

    /* Customize the header */
    /* header.id already set */
    header.qr = 1;
    header.opcode = 0;
    header.tc = 0;
    header.rd = rd_val; /* RDBUG udpany */
    header.ra = 0;
    header.aa = authoritative; /* Currently always 1 */
    header.z = 0;
    header.rcode = 0; /* No error */
    header.qdcount = 1;

    /* OBhack: Tack on the header at the beginning without molesting the
       rest of the string */
    length_save = most->unit_count;
    make_hdr(&header,most);
    most->unit_count = length_save;

    /* Add the ns and ar records to the end */
    if(js_append(ns,most) == JS_ERROR) {
        goto giveerror;
        }
    if(js_append(ar,most) == JS_ERROR) {
        goto giveerror;
        }

    /* Compress "most" and place the compressed data in "ar" */
    if(compress_data(most,ar) == JS_ERROR) {
        js_destroy(ar);
        js_destroy(ns);
        udperror(sock,most,client,0,SERVER_FAIL,"compression failure",2,
                        rd_val,ect,1);
        js_destroy(most);
        return JS_ERROR;
        }

    /* Check to make sure the data fits in under 512 bytes */
    if(ar->unit_count > 512) {

        /* If this is an ipv4 connection and we didn't get a compress error */
        if(ect->type == 4) {
            struct sockaddr_in *dq;
            uint32 ip_test;
            dq = (struct sockaddr_in *)(ect->d);
            ip_test = ntohl(dq->sin_addr.s_addr);
            /* See if we are allowed to send a long packet up to
             * 4096 bytes to this ip address */
            if(check_ipv4_acl(ip_test,long_packet) == 1) {
                if(ar->unit_count < 4096) {
                    goto long_packet_ok;
                    }
                }
            }

        /* We handle truncation by truncating everything except the
           12-byte header */
        header.tc = 1;
        header.ancount = 0;
        make_hdr(&header,ar);
        /* Append the question, if there is one */
        if(query != 0) {
           js_append(query,ar);
           js_adduint16(ar,1);
           }
        }

long_packet_ok:

    /* Success! Put out the good data */
    if(ect == 0) {
        sendto(sock,ar->string,ar->unit_count,0,
            (struct sockaddr *)client,len_inet);
    } else {
        mara_send(ect,sock,ar);
    }

    js_destroy(ar);
    js_destroy(ns);
    js_destroy(most);

    /* Clean up the seenlist_where list (list marking which ARs we gave out) */
    while(seenlist_where > 0) {
        --seenlist_where;
        if(seenlist[seenlist_where] != 0)
            (seenlist[seenlist_where])->seen = 0;
        }


    return JS_SUCCESS;

    /* We use gotos to make up for C's lack of error trapping */
    giveerror:
        js_destroy(ar);
        js_destroy(ns);
        udperror(sock,most,client,0,SERVER_FAIL,"giveerror in udpany",2,
                        rd_val,ect,1);
        js_destroy(most);

        /* Clean up the seenlist_where list
           (list marking which ARs we gave out) */
        while(seenlist_where > 0) {
            --seenlist_where;
            if(seenlist[seenlist_where] != 0)
                (seenlist[seenlist_where])->seen = 0;
            }

        return JS_ERROR;

    }

/* OK, there are a handful of record types which MaraDNS gives special
   treatment to when a TXT record is asked for the host name in question.
   This routine handles these special domain names.
   Input: ID of the query they sent us, socket of the request, a sockaddr
          with their address and port on it, a js_string containing
          the query (dname + type), The host name that is given special
          treatment (in a pre-hname2rfc1035 format), query type to convert,
          2 strings whose data is dependent on the the query_type to
          convert.
*/

int easter_egg(int id,int sock,conn *ect, js_string *query,
               char *hname, uint16 type, char *opt1, char *opt2) {
    js_string *reply, *hname_js, *data; /* The reply, the query, the answer */
    q_header header;
    int result;

    /* Sanity checks */
    if(js_has_sanity(query) == JS_ERROR)
        return JS_ERROR;
    if(hname == 0 || opt1 == 0)
        return JS_ERROR;

    if((reply = js_create(512,1)) == 0)
        return JS_ERROR;
    if((hname_js = js_create(256,1)) == 0) {
        js_destroy(reply);
        return JS_SUCCESS;
        }
    if((data = js_create(256,1)) == 0) {
        js_destroy(reply); js_destroy(hname_js);
        return JS_SUCCESS;
        }

    /* Make sure that this is the query that they asked for */
    hname_js->encoding = query->encoding;

    if(js_qstr2js(hname_js,hname) == JS_ERROR)
        goto cleanup;

    if(hname_2rfc1035(hname_js) <= 0)
        goto cleanup;

    if(js_adduint16(hname_js,type) == JS_ERROR)
        goto cleanup;

    result = js_issame(hname_js,query);
    if(result == JS_ERROR)
        goto cleanup;

    if(result != 1) {
        js_destroy(reply); js_destroy(hname_js); js_destroy(data);
        return 0;
        }

    /* OK, the hostname matches the "easter egg" name, now we form
       the "easter egg" reply */

    /* Get the data from the options */
    /* If we ever support easter eggs for anything besides TXT
       records, this will become a switch statement */
    if(type != RR_TXT) {
        js_destroy(reply); js_destroy(hname_js); js_destroy(data);
        return 0;
        }

    if(opt2 == 0)
        goto cleanup;

    /* With TXT records, we take the string in opt1, add the string in
       opt2 to the string, and make that the data.  hname_js is used
       as a "throwaway" string */
    if(js_qstr2js(hname_js,"") == JS_ERROR)
        goto cleanup;
    if(js_qappend(opt1,hname_js) == JS_ERROR)
        goto cleanup;
    if(js_qappend(opt2,hname_js) == JS_ERROR)
        goto cleanup;
    if(js_qstr2js(data,"") == JS_ERROR)
        goto cleanup;
    if(hname_js->unit_count > 255)
        goto cleanup;
    if(js_addbyte(data,hname_js->unit_count) == JS_ERROR)
        goto cleanup;
    if(js_append(hname_js,data) == JS_ERROR)
        goto cleanup;

    /* Build up the header for this reply */
    if(id > 0 && id < 65535)
        header.id = id;
    else
        goto cleanup;

    header.qr = 1; /* Reply */
    header.opcode = 0; /* Normal DNS */
    header.aa = 0; /* DDIP to A translations are never authoritative */
    header.tc = 0; /* A labels are too short to be truncated */
    header.rd = 0; /* Recursion not desired */ /* RDBUG easter egg */
    header.ra = 0; /* Recursion not available */
    header.z = 0; /* This must be 0 unless we are EDNS aware (we aren't) */
    header.rcode = 0; /* Success! */
    header.qdcount = 1;
    header.ancount = 1;
    header.nscount = 0;
    header.arcount = 0;

    /* Make a header of the reply */
    if(make_hdr(&header,reply) == JS_ERROR)
        goto cleanup;

    /* Add the question they asked to the reply */
    if(js_append(query,reply) == JS_ERROR)
        goto cleanup;

    /* Add the class (in) to the answer */
    if(js_adduint16(reply,1) == JS_ERROR)
        goto cleanup;

    /* We will now add out manufactured reply */
    if(js_append(query,reply) == JS_ERROR)
        goto cleanup;
    /* Append the class (in) to the answer */
    if(js_adduint16(reply,1) == JS_ERROR)
        goto cleanup;
    /* Append a bogus TTL to the answer */
    if(js_adduint32(reply,770616) == JS_ERROR) /* Was 770616 */
        goto cleanup;
    /* Add the rdlength to the answer */
    if(js_adduint16(reply,data->unit_count) == JS_ERROR)
        goto cleanup;
    /* Add the actual data to the answer */
    if(js_append(data,reply) == JS_ERROR)
        goto cleanup;

    /* Send the reply out */
    mara_send(ect,sock,reply);

    /* And, we are done */
    js_destroy(reply);
    js_destroy(hname_js);
    js_destroy(data);
    return JS_SUCCESS;

    /* We use gotos to work around C's lack of error trapping */
    cleanup:
        js_destroy(reply);
        js_destroy(hname_js);
        js_destroy(data);
        return JS_ERROR;

    }

/* Make a synthetic NS record.  This is what the funciton does:
 * Given a dname they wanted, such as "Awww.example.com.", and a number
 * of labels to truncate from the beginning of said dname (such as 1),
 * create a record like "Nexample.com."  */
js_string *make_synth_ns_record(js_string *dname_they_wanted,
                int labels_to_zap) {
        js_string *out = 0;
        int length,point,labels_seen;

        out = js_create(dname_they_wanted->unit_count + 2,1);

        /* Error check */
        if(out == 0) {
                return 0;
        }

        /* Move point forward to where the part we want to copy over
         * begins */
        point = length = 0;
        labels_seen = 0;
        while(labels_seen < labels_to_zap) {
                length = *(dname_they_wanted->string + point);
                if(point + length > dname_they_wanted->unit_count
                                || length > 63) {
                        js_destroy(out);
                        return 0;
                }
                /* printf("point %d length %d\n",point,length); */
                point += length + 1;
                if(point > dname_they_wanted->unit_count) {
                        js_destroy(out);
                        return 0;
                }
                labels_seen++;
        }

        /* Copy over the rest of the source string to 'out' */
        length = 0;
        while(point < dname_they_wanted->unit_count) {
                if(length >= out->max_count) {
                        js_destroy(out);
                        return 0;
                }
                if(point > dname_they_wanted->unit_count) {
                        js_destroy(out);
                        return 0;
                }
                *(out->string + length) =
                        *(dname_they_wanted->string + point);
                length++;
                point++;
        }

        /* Some other prep on the output string */
        out->unit_count = length;

        if(change_rtype(out,RR_NS) == JS_ERROR) {
                js_destroy(out);
                return 0;
        }
        return out;

}

/* If we successfully found a star record, spit out that record on the
   udp packet.
   Input: Where a pointer to the rr in question is, the id of the
          query they sent us, the socket the
          UDP bind is on, the sockaddr of the client who sent us the message,
          a js_string containing the query (dname + type),
          a js_string containing the answer,
          a number that is set to zero if not using a default hostname
          (see the csv2_default_zonefile mararc variable), and the number
          of dlabels before the star otherwise (so the NS record looks
          correct).
   Output: JS_ERROR on error, JS_SUCCESS on success
*/

int udpstar(rr *where,int id,int sock,struct sockaddr_in *client,
               js_string *query, js_string *answer, int rd_val, int
               endstar, conn *ect) {
    js_string *most, *ar; /* Most of the data then the additional records */

    uint16 first_rr_type;
    int in_ns = 0;
    int length_save;
    int len_inet = sizeof(struct sockaddr);
    js_string *synth_ns_record = 0; /* Used for starwhitis_end stuff */

    q_header header;
    /* Initialize the js_string objects */
    if((most = js_create(1024,1)) == 0)
        return JS_ERROR;
    if((ar = js_create(1024,1)) == 0) {
        js_destroy(most);
        return JS_SUCCESS;
        }

    /* Make the header a placeholder for now */
    init_header(&header);
    header.id = id;
    header.rd = rd_val; /* RDBUG udpstar */
    if(make_hdr(&header,most) == JS_ERROR)
        goto giveerror;

    /* Sanity check */
    if(where == 0) {
        goto giveerror;
        }
    if(where->query == 0) {
        goto giveerror;
        }
    if(js_has_sanity(where->query) == JS_ERROR) {
        goto giveerror;
        }
    if(where->data == 0) {
        goto giveerror;
        }
    if(js_has_sanity(where->data) == JS_ERROR) {
        goto giveerror;
        }
    if(js_has_sanity(query) == JS_ERROR) {
        goto giveerror;
        }
    first_rr_type = get_rtype(query);

    /* We have to add this header here--authoritative depends on the
       authorative status of the first record we find */
    header.aa = where->authoritative;

    /* The data must be between 0 and 65535 bytes in length (16-bit
       unsigned value) */
    if(where->data->unit_count < 0 || where->data->unit_count > 65535) {
        goto giveerror;
        }

    /* Append the question to the answer */
    if(js_append(query,most) == JS_ERROR) {
        goto giveerror;
        }

    /* Append the class (in) to the answer */
    if(js_adduint16(most,1) == JS_ERROR) {
        goto giveerror;
        }

    /* We will increment the ancount, nscount, an arcount, starting at 0 */
    header.ancount = 0;
    header.nscount = 0;
    header.arcount = 0;

    /* OK, we now add the answers */
    while(where != 0) {
        /* Increment the number of answers -or- ns records */
        if(first_rr_type != RR_NS && where->rr_type == RR_NS)
            in_ns = 1;
        if(!in_ns)
            header.ancount++;
        else
            header.nscount++;
        /* Append the name for the user's "canonical" query to the answer */
        if(!in_ns) {
            if(js_append(answer,most) == JS_ERROR) {
                goto giveerror;
                }
            }
        /* (Unless we are telling them the NS records for this RR) */
        else {
            /* If endstar is greater than zero, we need to make
             * a synthetic host name for the NS answer */
            if(endstar > 0 && endstar < 125 && synth_ns_record == 0) {
                synth_ns_record =
                        make_synth_ns_record(answer,endstar - 1);
                if(synth_ns_record == 0) {
                    goto giveerror;
                    }
                }
            if(synth_ns_record == 0) {
                if(js_append(where->query,most) == JS_ERROR) {
                    goto giveerror;
                    }
                } else {
                if(js_append(synth_ns_record,most) == JS_ERROR) {
                    goto giveerror;
                    }
                }
            }
        /* Append the class (in) to the answer */
        if(js_adduint16(most,1) == JS_ERROR) {
            goto giveerror;
            }
        /* Append the ttl to the answer */
        if(js_adduint32(most,determine_ttl(where->expire,where->ttl))
            == JS_ERROR) {
            goto giveerror;
            }
        /* Add the rdlength to the answer */
        if(js_adduint16(most,where->data->unit_count) == JS_ERROR) {
            goto giveerror;
            }
        /* Add the record itself to the answer */
        if(js_append(where->data,most) == JS_ERROR) {
            goto giveerror;
            }
        /* If there is an IP, and this is *not* a CNAME record,
           append the IP of the answer to the AR section */
        if(where->ip != 0 && where->rr_type != RR_CNAME) {
            /* Increment the number of additional records */
            header.arcount++;
            /* Append the name for this answer to the ip */
            if(js_append(where->ip->query,ar) == JS_ERROR) {
                goto giveerror;
                }
            /* Append the class (in) to the ip */
            if(js_adduint16(ar,1) == JS_ERROR) {
                goto giveerror;
                }
            /* Append the TTL to the ip */
            if(js_adduint32(ar,determine_ttl(where->ip->expire,where->ip->ttl))
                == JS_ERROR) {
                goto giveerror;
                }
            /* Add the rdlength to the ip */
            if(js_adduint16(ar,where->ip->data->unit_count) == JS_ERROR) {
                goto giveerror;
                }
            /* Add the record itself to the ip */
            if(js_append(where->ip->data,ar) == JS_ERROR) {
                goto giveerror;
                }
            }
        /* To do: A records attached to CNAMES are added as a second AN
                  record if the originally requested query was not a CNAME
        */
        /* Go on to the next record in the linked list */
        where = where->next;
        }

    /* Customize the header */
    /* header.id already set */
    header.qr = 1;
    header.opcode = 0;
    header.tc = 0; /* To do: truncation handling */
    header.rd = 0; /* RDBUG udpstar */
    header.ra = 0;
    header.z = 0;
    header.rcode = 0; /* No error */
    header.qdcount = 1;

    /* OBhack: Tack on the header at the beginning without molesting the
       rest of the string */
    length_save = most->unit_count;
    make_hdr(&header,most);
    most->unit_count = length_save;

    /* Add the ar records to the end */
    if(js_append(ar,most) == JS_ERROR) {
        goto giveerror;
        }

    /* Compress "most" and place the compressed data in "ar" */
    if(compress_data(most,ar) == JS_ERROR) {
        js_destroy(ar);
        udperror(sock,most,client,0,SERVER_FAIL,"compression failure",2,
                        rd_val,ect,1);
        js_destroy(most);
        if(synth_ns_record != 0) { js_destroy(synth_ns_record); }
        return JS_ERROR;
        }

    /* Check to make sure the data fits in under 512 bytes */
    if(ar->unit_count > 512) {
        /* We handle truncation by truncating everything except the
           12-byte header */
        header.tc = 1;
        make_hdr(&header,ar);
        }

    /* Success! Put out the good data */
    if(ect == 0) {
        sendto(sock,ar->string,ar->unit_count,0,
            (struct sockaddr *)client,len_inet);
    } else {
        mara_send(ect,sock,ar);
    }

    js_destroy(most);
    js_destroy(ar);

    if(synth_ns_record != 0) { js_destroy(synth_ns_record); }

    return JS_SUCCESS;

    /* We use gotos to make up for C's lack of error trapping */
    giveerror:
        if(synth_ns_record != 0) { js_destroy(synth_ns_record); }
        js_destroy(ar);
        udperror(sock,most,client,0,SERVER_FAIL,"giveerror in udpstar",2,
                        rd_val,ect,1);
        js_destroy(most);
        return JS_ERROR;

    }

/* Create a bogus 0-TTL ip answer if we give out these answers instead
 * of SOA answers/NXDOMAINS for non-existant addresses */

int make_notthere_reply(int id, int sock, struct sockaddr_in *client,
                        js_string *query, int rd_val, conn *ect) {
        js_string *most;
        q_header header;
        int len_inet = sizeof(struct sockaddr);

        init_header(&header);
        header.rd = rd_val; /* RDBUG make_notthere_reply */
        header.id = id;
        header.qr = 1;
        header.opcode = 0;
        header.tc = 0;
        header.ra = calc_ra_value(rd_val);
        header.z = 0;
        header.rcode = 0; /* We "found" something */
        /* We return just a single 0-ttl IP */
        header.qdcount = 1; /* Echo the question in the answer */
        header.ancount = 1;
        header.nscount = 0;
        header.arcount = 0;
        if((most = js_create(520,1)) == 0) {
                return JS_ERROR;
        }
        make_hdr(&header,most);
        /* Question */
        js_append(query,most);
        js_adduint16(most,1); /* Class: 1 */
        /* Answer */
        js_append(notthere_ip,most);

        /* Send answer over UDP */
        if(ect == 0) {
                sendto(sock,most->string,most->unit_count,0,
                        (struct sockaddr *)client,len_inet);
        } else {
                mara_send(ect,sock,most);
        }

        js_destroy(most);
        return JS_SUCCESS;
}

/* If we have a NXDOMAIN, deliver that record on the udp packet.
 *        Input: where: A pointer to the rr in question (the SOA record),
 *
 *        id: the id of the query they sent us,
 *
 *        sock: the socket the UDP bind is on,
 *
 *        client: the sockaddr of the client who sent us the message,
 *
 *        query: a js_string containing the query (dname + type),
 *
 *        qtype: if the qtype of the question is different than the
 *        desired qtype of the answer, then specify the qtype of the
 *        original question, otherwise specify 0 for the qtype
 *
 *        rd_val: The value for the "rd" flag in the header
 *
 *        ect: A structure describing from what IP they are connecting from
 *
 *        recursive_call: Whether this is called from the authoritative or
 *                        recursive half of MaraDNS
 *                        0: Called from authoritative half, do not
 *                           make the query the scope of the SOA reply
 *                        1: Called from recursive half, do not make
 *                           the query the scope of the SOA reply
 *                        2: Called from authoritative half, please make
 *                           query the scope of the SOA reply
 *                        3: Called from recursive half, please make
 *                           query the scope of the SOA reply
 *                        4-7: Same as 0-3, but also always return a
 *                             "not there" instead of a NXDOMAIN
 *
 * Output: JS_ERROR on error, JS_SUCCESS on success
 *
 * Note: The qtype for the js_string query changes in this call, and is not
 *       restored.  This may be a bug.
 */

int udpnotfound(rr *where, int id, int sock, struct sockaddr_in *client,
                js_string *query, int qtype, int rd_val, conn *ect,
                int recursive_call) {
    js_string *most, *compressed; /* Most of the data */

    uint16 first_rr_type;
    int length_save = 0, qtype_save = JS_ERROR;
    int len_inet = sizeof(struct sockaddr);

    q_header header;
    int always_not_there = 0;
    if(recursive_call >= 4) {
        recursive_call -= 4;
        always_not_there = 4;
    }

    if(js_has_sanity(query) == JS_ERROR) {
        return JS_ERROR;
        }
    first_rr_type = get_rtype(query);

    /* See if notthere_ip is set, they are using recursion, and
     * that they want an IP */
    if(notthere_ip != 0 && (recursive_call & 1) == 1 && first_rr_type == 1) {
                /* If so, give them a synthetic IP reply */
                return make_notthere_reply(id, sock, client, query, rd_val,
                                           ect);
    }

    /* Initialize the js_string objects */
    if((most = js_create(1024,1)) == 0)
        return JS_ERROR;
    if((compressed = js_create(1024,1)) == 0) {
        js_destroy(most);
        return JS_ERROR;
        }

    /* Make the header a placeholder for now */
    init_header(&header);
    header.rd = rd_val; /* RDBUG udpnotfound */
    header.id = id;
    if(make_hdr(&header,most) == JS_ERROR) {
        js_destroy(most); js_destroy(compressed);
        return JS_ERROR;
        }

    /* Sanity check */
    if(where == 0)
        goto giveerror;
    if(where->query == 0) {
        goto giveerror;
        }
    if(js_has_sanity(where->query) == JS_ERROR) {
        goto giveerror;
        }
    if(where->data == 0) {
        goto giveerror;
        }
    if(js_has_sanity(where->data) == JS_ERROR) {
        goto giveerror;
        }


    /* We have to add this header here--authoritative depends on the
       authorative status of the first record we find */
    header.aa = where->authoritative;

    /* The data must be between 0 and 65535 bytes in length (16-bit
       unsigned value) */
    if(where->data->unit_count < 0 || where->data->unit_count > 65535) {
        goto giveerror;
        }

    /* If they specified that the qtype of the quesiton is differnet than
       the qtype of the answer (this is used in the case of RR_ANY), then
       temporarily change the qtype when we give out the answer */
    if(qtype != 0) {
        qtype_save = get_rtype(query);
        if(qtype_save == JS_ERROR)
            goto giveerror;
        if(change_rtype(query,qtype) == JS_ERROR)
            goto giveerror;
        }

    /* Append the question to the answer */
    if(js_append(query,most) == JS_ERROR) {
        change_rtype(query,qtype_save);
        goto giveerror;
        }

    /* Set the qtype of the query */
    if(change_rtype(query,qtype) == JS_ERROR)
        goto giveerror;

    /* Append the class (in) to the answer */
    if(js_adduint16(most,1) == JS_ERROR) {
        goto giveerror;
        }

    /* These three values are zero, one, and zero */
    header.ancount = 0;
    header.nscount = 1;
    header.arcount = 0;

    /* Append the name for this answer to the answer */
    /* In the case of the default zonefile returning negative data,
     * *(where->query->string) will have the value '_'; a character that is
     * illegal to begin a DNS name with.
     * When recursive_call has a value of 2 or 3, this indicates that
     * we narrow the scope of the SOA reply (see notes on recursive_call
     * above) */
    if(*(where->query->string) == '_' || recursive_call == 2
       || recursive_call == 3) {
        qtype_save = get_rtype(query);
        if(change_rtype(query,RR_SOA) == JS_ERROR) {
            goto giveerror;
            }
        if(js_append(query,most) == JS_ERROR) {
            goto giveerror;
            }
        }
    else if(js_append(where->query,most) == JS_ERROR) {
        goto giveerror;
        }
    /* Append the class (in) to the answer */
    if(js_adduint16(most,1) == JS_ERROR) {
        goto giveerror;
        }
    /* Append the ttl to the answer */
    if(js_adduint32(most,determine_ttl(where->expire,where->ttl)) ==
        JS_ERROR) {
        goto giveerror;
        }
    /* Add the rdlength to the answer */
    if(js_adduint16(most,where->data->unit_count) == JS_ERROR) {
        goto giveerror;
        }
    /* Add the record itself to the answer */
    if(js_append(where->data,most) == JS_ERROR) {
        goto giveerror;
        }

    /* Customize the header */
    /* header.id already set */
    header.qr = 1;
    header.opcode = 0;
    header.tc = 0; /* To do: truncation handling */
    header.rd = rd_val; /* RDBUG udpnotfound */
    header.ra = calc_ra_value(rd_val);
    header.z = 0;
    /* Code that verifies that this host does not exist in
       any class.  If so, then we give them a rcode of NXDOMAIN_RCODE.
       Otherwise, we give them a rcode of 0 */
    if(always_not_there == 4) {
        header.rcode = 0; /* "not there" */
    } else if(recursive_call == 1 || recursive_call == 3) {
    /* For the recursive half, we just copy over the rcode from the
       reply the remote server gives us */
        if(where->rcode == 3) {
            header.rcode = 3; /* NXDOMAIN */
        } else {
            header.rcode = 0; /* "not there" */
        }
    } else {
    /* For the authoritative half, see if we have an ANY record
       for this query.  If we do, make the rcode 0 (not there); otherwise
       make the rcode 3 (NXDOMAIN) */
        mhash_e spot_data;
        qtype_save = get_rtype(query);
        if(change_rtype(query,RR_ANY) == JS_ERROR) {
            goto giveerror;
            }
        /* bighash is a global variable; we take advantage of that
           fact here */
        spot_data = mhash_get(bighash,query);
        if(spot_data.value != 0 && spot_data.datatype == MARA_DNS_LIST) {
            header.rcode = 0; /* "not there" */
        } else {
            header.rcode = 3; /* NXDOMAIN */
        }
    }
    header.qdcount = 1;

    /* OBhack: Tack on the header at the beginning without molesting the
       rest of the string */
    length_save = most->unit_count;
    make_hdr(&header,most);
    most->unit_count = length_save;

    /* Compress "most" and place the compressed data in "compressed" */
    if(compress_data(most,compressed) == JS_ERROR) {
        js_destroy(compressed);
        udperror(sock,most,client,0,SERVER_FAIL,"Compression failure",2,
                        rd_val,ect,1);
        js_destroy(most);
        return JS_ERROR;
        }

    /* Check to make sure the data fits in under 512 bytes */
    if(compressed->unit_count > 512) {
        /* We handle truncation by truncating everything except the
           12-byte header */
        header.tc = 1;
        make_hdr(&header,compressed);
        }

    /* Success! Put out the good data */
    if(ect == 0) {
        sendto(sock,compressed->string,compressed->unit_count,0,
            (struct sockaddr *)client,len_inet);
    } else {
        mara_send(ect,sock,compressed);
    }

    js_destroy(most);
    js_destroy(compressed);

    return JS_SUCCESS;


    /* We use gotos to make up for C's lack of error trapping */
    giveerror:
        js_destroy(compressed);
        udperror(sock,most,client,0,SERVER_FAIL,"giveerror in udpnotfound",2,
                        rd_val,ect,1);
        js_destroy(most);
        return JS_ERROR;

    }

/* Given a domain-label starting with a star record ('_') change this label
   in-place so that the first domain label after the star record is lopped
   off of it.  Eg. '_\003sub\007example\003com\000" becomes
   "_\007example\003com\000"
   input: A pointer to the js_string object in question
   output: JS_ERROR on error, JS_SUCCESS on success, 0 if the label is
           zero-length already
*/

int bobbit_starlabel(js_string *js) {
    int counter = 1;
    unsigned char length;

    if(js->unit_size != 1)
        return JS_ERROR;
    if(js->unit_count >= js->max_count)
        return JS_ERROR;
    if(js->unit_count < 2)
        return JS_ERROR;
    if(*(js->string) != '_')
        return JS_ERROR;
    length = *(js->string + 1);

    if(length + 2 > js->unit_count || length > 63)
        return JS_ERROR;
    else if(length == 0)
        return 0;

    length++;

    while(counter < (js->unit_count - length) + 1) {
        *(js->string + counter) = *(js->string + counter + length);
        counter++;
        }

    js->unit_count -= length;

    return JS_SUCCESS;

    }

/* Given a domain-label without a star record ('_'), change the first
   domain label in to a star record ('_') Eg. "\003www\007example\003com\000"
   becomes "_\007example\003com\000"
   input: A pointer to the js_string object in question
   output: JS_ERROR on error, JS_SUCCESS on success, 0 if the label is
           a star record already
*/

int make_starlabel(js_string *js) {
    int counter = 1;
    unsigned char length;

    if(js->unit_size != 1)
        return JS_ERROR;
    if(js->unit_count >= js->max_count)
        return JS_ERROR;
    if(js->unit_count < 2)
        return JS_ERROR;
    if(*(js->string) == '_')
        return 0;
    length = *(js->string);
    *(js->string) = '_';

    if(length > js->unit_count || length > 63)
        return JS_ERROR;
    if(length == 0) /* We don't convert a "." domain-label */
        return 0;

    while(counter < js->unit_count - length) {
        *(js->string + counter) = *(js->string + counter + length);
        counter++;
        }

    js->unit_count -= length;

    return JS_SUCCESS;

    }

/* Given a domain-label ending with (or without) a star record ('_'),
   change the label
   in-place so that the first domain label before the star record is lopped
   off of it.  Eg. "\003name\007example\003com\000\000\001" becomes
   "\003name\007example\003com_\000\001", and
   "\003name\007example\003com_\000\001" becomes "\003name\007example_\000\001"
   input: A pointer to the js_string object in question
   output: JS_ERROR on error, 0 if the label is
           zero-length already, number of labels including star otherwise
           (130 if we don't know how many labels there are)
*/

int bobbit_starlabel_end(js_string *js) {
    int counter = 1;
    int ret = 0;
    unsigned char toread;
    int length;
    int16 rtype;

    if(js->unit_size != 1)
        return JS_ERROR;
    if(js->unit_count >= js->max_count)
        return JS_ERROR;
    if(js->unit_count < 2)
        return JS_ERROR;
    if(js->unit_count == 3) {
        return 0;
    }
    rtype = *(js->string + js->unit_count - 1);
    rtype += *(js->string + js->unit_count - 2) << 8;
    js->unit_count -= 2;
    counter = dlabel_length(js,0);
    counter--;
    if(counter < 0 || counter > js->unit_count)
        return JS_ERROR;

    /* If this is not a starlabel-at-end label yet, convert it */
    if(*(js->string + counter) == '\0') {
        *(js->string + counter) = '_';
        if(js_adduint16(js,rtype) == JS_ERROR) {
            return JS_ERROR;
            }
        return 130;
        }

    /* Otherwise, lop off the last label */
    length = 0;
    toread = *(js->string);
    counter = 0;
    while(length < 256 && toread > 0 && toread != '_') {
        ret++;
        if(toread > 63)
            return JS_ERROR; /* No EDNS nor compressed label support */
        length += toread + 1;
        /* Go to the next jump */
        if(length > js->unit_count || length >= js->max_count)
            return JS_ERROR;
        counter = toread;
        toread = *(js->string + length);
        }
    if(length >= 256) {
        return JS_ERROR;
        }

    counter++;
    if(counter > js->unit_count || counter > length) {
        return JS_ERROR;
        }

    js->unit_count -= counter;
    if(js->unit_count < 1) {
        return JS_ERROR;
        }
    *(js->string + js->unit_count - 1) = '_';

    if(js_adduint16(js,rtype) == JS_ERROR) {
            return JS_ERROR;
        }
    return ret;

    }

/* Given a query, a record type to query, and whether we have
 * already found a record in question, do an ANY lookup for
 * the query in question
 *
 * Note: This is only called from udpany, and only when using the
 *       old ANY handling code (still used by the recursive resolver);
 *       this particular routine can probably be sucessfully pruned at
 *       this point.
 */

int starwhitis_seek_any(js_string *query, int rr_type, int found,
                q_header *head, rr **w, int *a,
                js_string *most, js_string *ns, js_string *ar) {
        js_string *star; /* Star-record converted query */
        mhash_e spot_data;
        int this_rr_found = 0;
        if(found == JS_ERROR) {
                return JS_ERROR;
        }
        if((star = js_create(256,1)) == 0) {
                return JS_ERROR;
        }
        /* Change the query type for the star record */
        if(change_rtype(query,rr_type) == JS_ERROR) {
                js_destroy(star);
                return JS_ERROR;
        }
        /* Copy the query over */
        if(js_copy(query,star) == JS_ERROR) {
                js_destroy(star);
                return JS_ERROR;
        }
        if(make_starlabel(star) == JS_ERROR) {
                js_destroy(star);
                return JS_ERROR;
        }
        /* Look for a record with the same name as the query */
        while(this_rr_found == 0) {
                spot_data = mhash_get(bighash,star);
                /* If found, add the data to our records */
                if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
                        /* If we have already found other records, there
                         * is no need to add NS records to the authority
                         * section of the answer, nor to determine whether
                         * the record should be marked "authoritative" in the
                         * DNS header */
                        if(found == 1) {
                                this_rr_found = 1;
                                if(add_answer(spot_data.value,most,ns,ar,
                                      &(head->ancount), &(head->nscount),
                                      &(head->arcount),0,spot_data.point,
                                      query,0) == JS_ERROR) {
                                        js_destroy(star);
                                        return JS_ERROR;
                                }
                        /* If we have not already found a record, we
                         * use the NS records for the first record
                         * we find to determine what NS and AR
                         * records go in the authority section.
                         * We also use the "authority" bit in the
                         * first answer we find to set the
                         * AA bit in the DNS header */
                        } else {
                                this_rr_found = 1;
                                found = 1;
                                *w = (rr *)spot_data.value;
                                *a = (*w)->authoritative;
                                if(add_answer(spot_data.value,most,ns,ar,
                                        &(head->ancount),&(head->nscount),
                                        &(head->arcount),1,spot_data.point,
                                        query,0) == JS_ERROR) {
                                        js_destroy(star);
                                        return JS_ERROR;
                                }
                        }
                }
                if(bobbit_starlabel(star) <= 0)
                        break;
        }
        js_destroy(star);
        return found;
}

/* Convert a domain-name query in to its lower-case equivalent
   Input: Pointer to the js string object with the query
   Output: JS_ERROR on error, JS_SUCCESS on sucess, 0 on
           success if no change was made to the string */

int fold_case(js_string *js) {
    int counter = 0;
    int ret = 0;

    if(js->max_count <= js->unit_count) {
        return JS_ERROR;
        }
    if(js->unit_size != 1) {
        return JS_ERROR;
        }
    if(js->unit_count < 2) {
        return JS_ERROR;
        }
    while(counter + 2 < js->unit_count) {
        /* Since A-Z never happen in a domain length label, we can speed
           things up a bit */
        if(*(js->string + counter) >= 'A' && *(js->string + counter) <= 'Z') {
            *(js->string + counter) += 32;
            ret = 1;
            }
        counter++;
        }

    return ret;

    }

/* Check to see if the IP in question is a ddip (e.g.
   "<03>127<01>0<01>0<03>1<00>"), and, if so, convert it in to
   a bare A record
   input: Pointer to js_string object with the query
   output: JS_ERROR on fatal error, 0 on non-ddip query,
           JS_SUCCESS if it was a ddip
*/

int ddip_check(int id, int sock, conn *ect, js_string *query) {
    unsigned char ip[4];
    unsigned char length, val;
    int counter, critter, lenl, value;
    js_string *reply;
    q_header header;

    /* Sanity checks */
    if(query->unit_size != 1)
        return JS_ERROR;
    if(query->unit_count >= query->max_count)
        return JS_ERROR;

    /* We presently only do ddip translation for A and ANY requests
       (DJB only supports this in Dnscache) */
    if(get_rtype(query) != RR_A && get_rtype(query) != RR_ANY)
        return 0;

    if(query->unit_count < 9) /* The minimum possible length for a
                                 ddip domain label */
        return 0;

    lenl = 0;
    for(counter=0;counter<4;counter++) {
        length = *(query->string + lenl);
        if(length < 1 || length > 3)
            return 0;
        critter = lenl + 1;
        lenl += length + 1;
        if(lenl > query->unit_count)
            return JS_ERROR;
        for(value = 0;critter < lenl;critter++) {
            val = *(query->string + critter);
            if(val > '9' || val < '0')
                return 0;
            value *= 10;
            value += val - '0';
            }
        if(value < 0 || value > 255)
            return 0;
        ip[counter] = value;
        }

    if(*(query->string + lenl) != 0)
        return 0;

    /* OK, it is definitely a ddip label.  Convert the ip in to a DNS reply */

    if((reply = js_create(512,1)) == 0)
        return JS_ERROR;

    /* Build up the header for this reply */
    if(id > 0 && id < 65535)
        header.id = id;
    else
        goto cleanup;

    header.qr = 1; /* Reply */
    header.opcode = 0; /* Normal DNS */
    header.aa = 0; /* DDIP to A translations are never authoritative */
    header.tc = 0; /* A labels are too short to be truncated */
    header.rd = 0; /* Recursion not desired */ /* RDBUG ddip_check */
    header.ra = 0; /* Recursion not available */
    header.z = 0; /* This must be 0 unless we are EDNS aware (we aren't) */
    header.rcode = 0; /* Success! */
    header.qdcount = 1;
    header.ancount = 1;
    header.nscount = 0;
    header.arcount = 0;

    /* Make a header of the reply */
    if(make_hdr(&header,reply) == JS_ERROR)
        goto cleanup;

    /* Add the question they asked to the reply */
    if(js_append(query,reply) == JS_ERROR)
        goto cleanup;

    /* Add the class (in) to the answer */
    if(js_adduint16(reply,1) == JS_ERROR)
        goto cleanup;

    /* Make sure the answer is an A record type */
    if(change_rtype(query,RR_A) == JS_ERROR)
        goto cleanup;

    /* We will now add out manufactured A reply */
    if(js_append(query,reply) == JS_ERROR)
        goto cleanup;
    /* Append the class (in) to the answer */
    if(js_adduint16(reply,1) == JS_ERROR)
        goto cleanup;
    /* Append a bogus TTL to the answer */
    if(js_adduint32(reply,19770616) == JS_ERROR)
        goto cleanup;
    /* Add the rdlength to the answer */
    if(js_adduint16(reply,4) == JS_ERROR)
        goto cleanup;
    /* Add the actual 4-byte reply to the answer */
    for(counter = 0; counter < 4; counter++) {
        if(js_addbyte(reply,ip[counter]) == JS_ERROR)
            goto cleanup;
        }

    /* Send the reply out */
    mara_send(ect,sock,reply);

    /* And, we are done */
    js_destroy(reply);
    return JS_SUCCESS;

    /* We use gotos to work around C's lack of error trapping */
    cleanup:
        js_destroy(reply);
        return JS_ERROR;

    }

/* Determine if a given IP is on a given ipv4pair ACL
 * Input: The ip, the ACL list
 * Output: 0 if they do not have authority, 1 if they do
 */
int check_ipv4_acl(uint32 ip, ipv4pair *list) {
    int counter = 0, ret = 0;
    while(counter < 500 && (list[counter]).ip != 0xffffffff) {
        if((ip & (list[counter]).mask) ==
               ((list[counter]).ip & (list[counter]).mask)) {
            /* They are authorized */
            ret = 1;
            break;
            }
        counter++;
        }
    return ret;
}

/* Determine if a given IP has authority to perform recursive DNS lookups
   Input: IP of where they come from
   Ouput: 0 if they do not have authority, 1 if they do
   Global variables used: The recurse_acl array
*/

int check_recursive_acl(uint32 ip) {
    return check_ipv4_acl(ip,recurse_acl);
    }

/* Look for both the upper and lower case versions of a given query.
   Input: The query, and, to give to udpsuccess:
          The ID of this query
          The socket ID of this query
          Where this ID came from
          The original query (to echo in the question)
   Output: 0 on not found, JS_ERROR on error, JS_SUCCESS on success
 */

int hunt_single_query(js_string *query, int id, int sock,
                      conn *ect, js_string *question, int rd_val) {
    mhash_e spot_data;
    int qtype_o, qtype_q;
    /* js_string *lower; */

    qtype_o = get_rtype(question);
    qtype_q = get_rtype(query);

    if(qtype_o == JS_ERROR || qtype_q == JS_ERROR) {
        return JS_ERROR;
        }


    spot_data = mhash_get(bighash,query);
    /* If found, give back the response */
    if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
        if(qtype_o == RR_A || qtype_q == RR_CNAME) {
#ifdef AUTHONLY
            udpsuccess(spot_data.value,id,sock,0,question,
                       spot_data.point,1,rd_val,ect,force_auth,0);
#else
            udpsuccess(spot_data.value,id,sock,0,question,
                       spot_data.point,1,rd_val,ect,force_auth,rd_val);
#endif /* AUTHONLY */
            }
        else {
#ifdef AUTHONLY
            udpsuccess(spot_data.value,id,sock,0,question,
                       spot_data.point,0,rd_val,ect,force_auth,0);
#else
            udpsuccess(spot_data.value,id,sock,0,question,
                       spot_data.point,0,rd_val,ect,force_auth,rd_val);
#endif /* AUTHONLY */
             }
        return JS_SUCCESS;
        }

    /* Not found */
    return 0;
    }

/* Check to see if we have a collision that, when we anally follow
 * RFC1034 section 4.3.3, makes it so we stop looking for a star record
 * "above".  In other words, if there is a record b.example.com and a
 * record *.example.com, this makes sure a.b.example.com and
 * c.a.b.example.com do not resolve */
int star_collision(js_string *lookfor, mhash *bighash) {
        js_string *be_anal = 0;
        mhash_e spot_data;
        if(js_length(lookfor) < 2) {
            return 0;
            }
        be_anal = js_create(257,1);
        if(js_copy(lookfor,be_anal) == JS_ERROR) {
            js_destroy(be_anal);
            return JS_ERROR;
            }
        /* This is a very hackey way of removing the first character
         * from the string.  This works, but it's very important to
         * restore the string pointer to its previous position before
         * deallocating the string. */
        be_anal->string++;
        be_anal->unit_count--;
        if(change_rtype(be_anal,RR_ANY) == JS_ERROR) {
            be_anal->string--;
            js_destroy(be_anal);
            return JS_ERROR;
            }
        spot_data = mhash_get(bighash,be_anal);
        if(spot_data.value != 0 &&
              spot_data.datatype == MARA_DNS_LIST) {
            /* There is a RR phohibiting the star record.  Return
             * name error */
            be_anal->string--;
            js_destroy(be_anal);
            return 1;
            }
        be_anal->string--;
        js_destroy(be_anal);
        return 0;
}

/* Process the DNS query that comes in from the 'net
   Input: uncompressed form of incoming UDP query, IP address of where
          this query came from, socket number of this socket
   Output: JS_ERROR on error, JS_SUCCESS on success
*/

int proc_query(js_string *raw, conn *ect, int sock) {

    q_header header; /* Header of the question */
    js_string *lookfor = 0; /* What to look for in the big hash */
    js_string *origq = 0; /* Original query asked by the user */
    js_string *lc = 0; /* Lower-case version of query asked by the user */
    rr *nxstore = 0; /* A pointer to the SOA we return when we hit a
                        NXDOMAIN */
    /* int case_folded; */
    int length, result_code = 0, qtype;
#ifndef AUTHONLY
    int has_recursive_authority = 0;
#endif
    mhash_e spot_data;
    int have_authority = 0; /* Do we have authority for this record?
                               (must be 1 to return a NXDOMAIN) */
    rr *point = 0;
    uint32 ip;
    int desires_recursion = 0; /* Do they desire recursion? */
    char *num_string = 0; /* The string to put the number of thread running
                             in */
    unsigned int mem_usage; /* The amount of memory a maradns process has
                               allocated */
    struct sockaddr_in *z; /* Makes certain ugly declarations readable */
    int always_not_there = 0;
    int rd_val = 0;


    /* Sanity checks */
    if(js_has_sanity(raw) == JS_ERROR)
        return JS_SUCCESS;
    if(raw->unit_size != 1)
        return JS_SUCCESS;

    /* Get the header */
    if(read_hdr(raw,&header) == JS_ERROR) { /* Something went wrong,
                                               return error "Format error" */
        udperror(sock,raw,0,0,FORMAT_ERROR,"Couldn't get header",2,0,ect,1);
        return JS_SUCCESS;
        }

    /* See if they desire recursion or not */
    desires_recursion = rd_val = header.rd;

    /* We only answer questions (Thanks to Roy Arends for pointing out this
       security flaw) */
    if(header.qr != 0) {
        return JS_SUCCESS;
        }

    /* We only support a qdcount of 1 */
    if(header.qdcount != 1) {
        if(no_fingerprint != 1)
            udperror(sock,raw,0,0,NOT_IMPLEMENTED,"Qdcount not 1",2,
                            desires_recursion,ect,1);
        return JS_SUCCESS;
        }

    /* Get the question from the stream */
    if(raw->unit_count < 14) {
        if(no_fingerprint != 1)
            udperror(sock,raw,0,0,FORMAT_ERROR,"bad question hdr",2,
                            desires_recursion,ect,1);
        return JS_SUCCESS;
        }

    /* Determine the length of the domain label in the question */
    length = dlabel_length(raw,12);
    if(length < 0 || length > 255) {
        if(no_fingerprint != 1)
            udperror(sock,raw,0,0,FORMAT_ERROR,"bad question length",2,
                            desires_recursion,ect,1);
        return JS_SUCCESS;
        }

    if(raw->unit_count < 16 + length) { /* 16 because 12 for the header,
                                           and 4 for the type and class */
        if(no_fingerprint != 1)
            udperror(sock,raw,0,0,FORMAT_ERROR,"question doesn't fit",2,
                            desires_recursion,ect,1);
        return JS_SUCCESS;
        }

    /* Create the lookfor string, returning error if appropriate */
    if((lookfor = js_create(256,1)) == 0) {
        if(no_fingerprint != 1)
            udperror(sock,raw,0,0,SERVER_FAIL,
                     "can't create lookfor string",2,desires_recursion,ect,1);
        return JS_ERROR;
        }
    if((origq = js_create(256,1)) == 0) {
        udperror(sock,raw,0,0,SERVER_FAIL,"can't create origq string",2,
                        desires_recursion,ect,1);
        js_destroy(lookfor);
        return JS_ERROR;
        }

    /* Get the query we will look for from their raw query */
    if(js_substr(raw,lookfor,12,length + 2) == JS_ERROR) {
        goto serv_fail;
        }

    /* We only support an opcode of 0 (standard query)
       (this check is down here so we can echo the question) */
    if(header.opcode != 0) {
        /* Since TinyDNS also returns NOT_IMPLEMENTED here, no need for
           a fingerprint check. */
        udperror(sock,raw,0,lookfor,NOT_IMPLEMENTED,"non-0 opcode",2,
                        desires_recursion,ect,1);
        js_destroy(origq); js_destroy(lookfor);
        return JS_SUCCESS;
        }

    /* Return "not implemented" if the class is not 1 (Internet class) */
    /* Down here so we can echo the question */
    if(*(raw->string + length + 14) != 0 &&
       *(raw->string + length + 15) != 1) {
        if(no_fingerprint != 1) {
            udperror(sock,raw,0,lookfor,NOT_IMPLEMENTED,"Class not 1",2,
                            desires_recursion,ect,1);
            }
        js_destroy(origq); js_destroy(lookfor);
        return JS_ERROR;
        }

    /* Copy the original query over to the "original query" string */
    if(js_copy(lookfor,origq) == JS_ERROR) {
        goto serv_fail;
        }

    /* Convert the query in to lower-case, since DNS is case-insensitive
     * and any attempt to not make it so is buggy */
    fold_case(lookfor);

    /* Get the type of query the client desires */
    qtype = get_rtype(origq);
    if(qtype == JS_ERROR) {
        goto serv_fail;
        }

    /* We may reject AAAA queries */
    if(qtype == 28 && reject_aaaa != 0) {
            udpnotfound(ra_data,header.id,sock,0,origq,0,
                            desires_recursion,ect,2);
            js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
            return JS_SUCCESS;
    }

    /* We may reject PTR queries */
    if(qtype == RR_PTR && reject_ptr != 0) {
            udpnotfound(ra_data,header.id,sock,0,origq,0,
                            desires_recursion,ect,2);
            js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
            return JS_SUCCESS;
    }

    if(qtype >= 250 && qtype <= 254) { /* IXFR, AXFR, and 2 more */
        goto not_impl;
        }

    /* Set 'ip' to have the ip they are using MaraDNS from */
    z = (struct sockaddr_in *)ect->d;
    ip = htonl((z->sin_addr).s_addr);

#ifndef AUTHONLY
    /* See if they have permission to perform a recursive query */
    has_recursive_authority = check_recursive_acl(ip);
    if(has_recursive_authority == JS_ERROR)
        goto serv_fail;
#endif

    /* We go straight to processing this as a recursive query at a
     * dos_protection_level of 78; this is the default value of
     * dos_protection_level if neither csv1 nor csv2 nor csv2_default_zonefile
     * is set */
    if(dos_protection_level == 78 ) {
            goto recursive_call;
    }
    /* When dos_protection_level is 79, the only authoritative-type thing we
     * do is report the version number of MaraDNS if they ask for it and have
     * the authority to get this information */
    if(dos_protection_level == 79) {
            goto report_version;
    }

    /* We don't process RR_ANY records if dos_protection_level is greater
     * than 13 */
    if(dos_protection_level > 13) {
            goto skip_rrany;
    }

    /* Handle the case of RR_ANY */
    if(qtype == RR_ANY) {
        result_code = udpany(header.id,sock,0,lookfor,rrany_set,bighash,
                        desires_recursion,ect,0,origq);
        if(result_code == JS_SUCCESS) {
            js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
            return JS_SUCCESS;
            }
        else if(result_code == JS_ERROR)
            goto serv_fail;
        /* Otherwise, no RR_ANY information was found.  We will return,
           if appropriate, the expected "no such host" SOA reply or
           NS delegation reply.  Hack: Since there *should not* be
           any elements in the hash proper with "ANY" as the desired
           record type, we go ahead and perform the various normal
           searches. */
        }

skip_rrany:

    /* OK, start the complicated domain look up routine */
    /* Look for upper and lower case versions of the query as
       they asked it */
    if(hunt_single_query(lookfor,header.id,sock,ect,origq,rd_val) != 0) {
        js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
        return JS_SUCCESS;
        }

    /* If dos_protection_level is greater than seven, then we don't look up
     * CNAME records */
    if(dos_protection_level > 7) {
            goto skip_cname;
    }

    /* OK, if not found, maybe there is a CNAME with the same domain label */
    if(change_rtype(lookfor,RR_CNAME) == JS_ERROR) {
        goto serv_fail;
        }
    if(hunt_single_query(lookfor,header.id,sock,ect,origq,rd_val) != 0) {
        js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
        return JS_SUCCESS;
        }

skip_cname:

    /* If dos_protection_level is one or higher, we don't process
     * any of the messages one can get about MaraDNS */

    if(dos_protection_level > 0) {
            goto skip_easter_egg;
    }

    /* Franky's request to have "administrative IPs", finally implemented
     * after almost four years */

    if(check_ipv4_acl(ip,admin_acl) != 1) {
            goto skip_easter_egg;
    }


report_version:

        /* A TXT query to "version.maradns." will
           return the version of MaraDNS being run.  This only
           works if we are not authoritative for "maradns.org", since
           the real "erre-con-erre-cigarro.maradns.org" says
           "MaraDNS version number not available" in the TXT record.
           Note: This is disabled if no_fingerprint is 1 or if
           debug_msg_level is less than one */
        if(origq->unit_count == 19 && *(origq->string) == 7
           && no_fingerprint != 1 && debug_msg_level >= 1) {
            result_code = easter_egg(header.id,sock,ect,origq,
               "Tversion.maradns.",RR_TXT,"MaraDNS version ",
#ifdef VERSION
               VERSION
#else
               "Broken compile, VERSION not defined"
#endif /* VERSION */
            );
            if(result_code == JS_SUCCESS) {
                js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
                return JS_SUCCESS;
                }
            if(result_code == JS_ERROR) {
                goto serv_fail;
                }
            }

    /* At dos_protection_level 79, the only authoritative-type thing we do
     * is let them see the version number of MaraDNS (see above) */
    if(dos_protection_level == 79 && debug_msg_level <= 1) {
            goto recursive_call;
    }

        /* A TXT query to "numthreads.maradns." tells us the number of
           threads that MaraDNS is running; this is only enabled if
           no_fingerprint is 0 and if debug_msg_level is 2 or greater
        */
#ifndef AUTHONLY
       if(origq->unit_count == 22 && *(origq->string) == 10
          && no_fingerprint != 1 && debug_msg_level >= 2) {
           /* Allocate a string to put the number of threads running in */
           if((num_string = js_alloc(32,1)) == 0) {
               js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
               return JS_ERROR;
               }
           snprintf(num_string,10,"%d",how_many_threads());
           result_code = easter_egg(header.id,sock,ect,origq,
            "Tnumthreads.maradns.",RR_TXT,"Number threads running: ",
            num_string);
           js_dealloc(num_string);
           if(result_code == JS_SUCCESS) {
               js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
               return JS_SUCCESS;
               }
           if(result_code == JS_ERROR) {
               goto serv_fail;
               }
           }
#endif /* AUTHONLY */

        /* A TXT query to "memusage.maradns." tells us the number of
           threads that MaraDNS is running; this is only enabled if
           no_fingerprint is 0 and if debug_msg_level is 2 or greater
        */
       if(origq->unit_count == 20 && *(origq->string) == 8
          && no_fingerprint != 1 && debug_msg_level >= 2) {
           /* Allocate a string to put the number of threads running in */
           mem_usage = js_tell_memory_allocated();
           if(mem_usage > 0) {
               if((num_string = js_alloc(32,1)) == 0) {
                   js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
                   return JS_ERROR;
                   }
               snprintf(num_string,14,"%d",mem_usage);
               result_code = easter_egg(header.id,sock,ect,origq,
                "Tmemusage.maradns.",RR_TXT,"Memory usage, in bytes: ",
                num_string);
               js_dealloc(num_string);
               }
           else {
               result_code = easter_egg(header.id,sock,ect,origq,
                "Tmemusage.maradns.",RR_TXT,"Memory usage unknown; ",
                "try compiling with make debug (note that this will greatly"
                " slow down MaraDNS)");
               }
           if(result_code == JS_SUCCESS) {
               js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
               return JS_SUCCESS;
               }
           if(result_code == JS_ERROR) {
               goto serv_fail;
               }
           }

        /* A TXT query to "timestamp.maradns." tells us the time
           on the system MaraDNS is running on; this is only enabled if
           no_fingerprint is 0 and if debug_msg_level is 2 or greater
        */
       if(origq->unit_count == 21 && *(origq->string) == 9
          && no_fingerprint != 1 && debug_msg_level >= 3) {
           qual_timestamp the_time;
           /* Allocate a string to put the number of threads running in */
           the_time = qual_get_time();
           if(the_time > 0) {
               if((num_string = js_alloc(32,1)) == 0) {
                   js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
                   return JS_ERROR;
                   }
               if(sizeof(int) < 8 && the_time > 2147483647U) {
                   strncpy(num_string,"pastY2038",11);
                   }
               else {
                   snprintf(num_string,14,"%d",(int)the_time);
                   }
               result_code = easter_egg(header.id,sock,ect,origq,
                "Ttimestamp.maradns.",RR_TXT,"Timestamp: ",
                num_string);
               js_dealloc(num_string);
               }
           else {
               result_code = easter_egg(header.id,sock,ect,origq,
                "Tmemusage.maradns.",RR_TXT,"Memory usage unknown; ",
                "try compiling with make debug (note that this will greatly"
                " slow down MaraDNS)");
               }
           if(result_code == JS_SUCCESS) {
               js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
               return JS_SUCCESS;
               }
           if(result_code == JS_ERROR) {
               goto serv_fail;
               }
           }

        /* A TXT query to "cache-elements.maradns." tells us the number of
           elements in the DNS cache; this is only enabled if
           no_fingerprint is 0 and if debug_msg_level is 2 or greater
        */
#ifndef AUTHONLY
       if(origq->unit_count == 26 && *(origq->string) == 14
          && no_fingerprint != 1 && debug_msg_level >= 2) {
           /* Allocate a string to put the number of threads running in */
           if((num_string = js_alloc(32,1)) == 0) {
               js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
               return JS_ERROR;
               }
           snprintf(num_string,10,"%d",cache_elements());
           result_code = easter_egg(header.id,sock,ect,origq,
            "Tcache-elements.maradns.",RR_TXT,"Elements in DNS cache: ",
            num_string);
           js_dealloc(num_string);
           if(result_code == JS_SUCCESS) {
               js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
               return JS_SUCCESS;
               }
           if(result_code == JS_ERROR) {
               goto serv_fail;
               }
           }
#endif /* AUTHONLY */

/* A TXT query to [0-9].verbose_level.maradns. (where [0-9] is a number
   between 0 and 9) sets the verbose_level to the number in question.
   This is only allowed if remote_admin is set to one */
       if(origq->unit_count == 27 && *(origq->string) == 1
          && no_fingerprint != 1 && remote_admin == 1) {
           int new_verbose_level;
           char *query_string;
           new_verbose_level = *(origq->string + 1) - '0';
           if(new_verbose_level < 0 || new_verbose_level > 9) {
               goto skip_easter_egg;
               }
           /* Allocate a string to put the verbose_level in */
           if((num_string = js_alloc(4,1)) == 0) {
               js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
               return JS_ERROR;
               }
           *num_string = new_verbose_level + '0';
           *(num_string + 1) = 0;
           if((query_string = js_alloc(37,1)) == 0) {
               js_dealloc(num_string);
               js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
               return JS_ERROR;
               }
           if(strncpy(query_string,"T0.verbose_level.maradns.",29) == 0) {
               js_dealloc(query_string);
               js_dealloc(num_string);
               js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
               return JS_ERROR;
               }
           *(query_string + 1) = '0' + new_verbose_level;
           result_code = easter_egg(header.id,sock,ect,origq,
            query_string,RR_TXT,"Verbose level is now ",num_string);
           js_dealloc(num_string);
           js_dealloc(query_string);
           if(result_code == JS_SUCCESS) {
               log_level = new_verbose_level;
               log_lock();
               printf("Verbose_level remotely set to %d\n",new_verbose_level);
               log_unlock();
#ifndef AUTHONLY
               init_rlog_level(new_verbose_level);
#endif
               js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
               return JS_SUCCESS;
               }
           if(result_code == JS_ERROR) {
               goto serv_fail;
               }
           }

skip_easter_egg:

    /* If dos_protection_level is greater than 11, then we don't
     * look for delegation NS records */
    if(dos_protection_level > 11) {
        goto skip_delegation_ns;
    }

    /* OK, if not found, maybe there is a *nonauthoritative* NS record with
       the same domain label */
    if(change_rtype(lookfor,RR_NS) == JS_ERROR) {
        goto serv_fail;
        }

    spot_data = mhash_get(bighash,lookfor);
    point = spot_data.value;
    /* If the non-authoritative NS was found, return the NS infomation */
    if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR &&
       point->authoritative == 0) {
        /* It is possible, but unlikely, they want recursion */
#ifndef AUTHONLY
        if(recurse_delegation == 1 && desires_recursion == 1 &&
           has_recursive_authority == 1) {
                /* Recursion only works for IPV4 */
                if(ect->type != 4) {
                        js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
                        return JS_ERROR;
                }
                /* Launch the thread that will process the request; we
                 * copy ect->d over */
                z = (struct sockaddr_in *)ect->d;
                launch_thread(header.id,sock,*z,origq);
                js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
                return JS_SUCCESS;
                }
#endif
        /* We return a NS server delegation */
        udpsuccess(spot_data.value,header.id,sock,0,origq,spot_data.point,
                        0,desires_recursion,ect,0,0);
        js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
        return JS_SUCCESS;
        }

    /* See if it is a dotted-decimal IP */
    if(no_fingerprint != 1) {
        result_code = ddip_check(header.id,sock,ect,origq);
        if(result_code == JS_SUCCESS) {
            js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
            return JS_SUCCESS;
            }
        if(result_code == JS_ERROR) {
            goto serv_fail;
            }
        }

    /* Look for a NS record at the same level or above.  E.G., if they
       ask for somthing.below.sub.example.com. and we have knowledge
       that sub.example.com is a NS record, act as appropriate.  Send
       them the NS record -or- go recursive if the NS record is
       non-authoritative (we're not handling the zone), otherwise return
       a "host not there" if the NS record is authoritative
     */

    nxstore = NULL;

    if(
#ifndef AUTHONLY
       has_recursive_authority != 1 ||
#endif
       desires_recursion != 1 || recurse_delegation != 1) {
      do {
        spot_data = mhash_get(bighash,lookfor);
        point = spot_data.value;
        /* We stop going up the tree if we have found an authoritative NS
           record */
        if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR &&
           point->authoritative != 0) {
            have_authority = 1;
            /* Look for a SOA record of the same type to prepare for
               a NXDOMAIN reply */
            if(change_rtype(lookfor,RR_SOA) == JS_ERROR) {
                goto serv_fail;
                }
            spot_data = mhash_get(bighash,lookfor);
            if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
                nxstore = spot_data.value;
                }
            break;
            }
        /* Return the NS record we found "up the tree", if appropriate */
        if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
            /* We return a NS server delegation */
            udpsuccess(spot_data.value,header.id,sock,0,origq,
                       spot_data.point,0,desires_recursion,ect,0,0);
            js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
            return JS_SUCCESS;
            }
        } while(bobbit_label(lookfor) > 0);
    }

skip_delegation_ns:

    /* Skip search for star levels at the beginning of hostnames if
     * dos_protection_level is greater than 17 */
    if(dos_protection_level > 17) {
            goto skip_starwhitis;
    }

    /* Remmy's issue: If we have any recordtype for a given record, we don't
     * look for a star record with the same name.  This emulates BIND's
     * behavior for star records and strictly follows RFC1034 section
     * 4.3.3 */
    if(bind_star_handling >= 1) {
        if(js_copy(origq,lookfor) == JS_ERROR)
            goto serv_fail;
        if(fold_case(lookfor) == JS_ERROR)
            goto serv_fail;
        /* Look for an ANY record with the same name and
           goto skip_starwhitis if found */
        if(change_rtype(lookfor,RR_ANY) == JS_ERROR) {
            goto serv_fail;
        }
        spot_data = mhash_get(bighash,lookfor);
        if(spot_data.value != 0 && spot_data.datatype == MARA_DNS_LIST) {
            always_not_there = 4;
            goto skip_starwhitis;
            }
        }

    /* Maybe it is a star record they are looking for */

    /* We need to restore "lookfor" because we shredded both
       strings looking for a NS sub-delegation */
    if(js_copy(origq,lookfor) == JS_ERROR) {
        goto serv_fail;
        }
    if(fold_case(lookfor) == JS_ERROR) {
        goto serv_fail;
        }
    /* Convert lookfor in to a star label */
    if(make_starlabel(lookfor) == JS_ERROR) {
        goto serv_fail;
        }

    /* Look for the star record in the big hash */
    spot_data = mhash_get(bighash,lookfor);
    if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
        udpstar(spot_data.value,header.id,sock,0,origq,origq,
                        desires_recursion,0,ect);
        js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
        return JS_SUCCESS;
        }

    /* When bind_star_handling is set, we never incorrectly return a
     * NXDOMAIN if there is a star record with the given name */
    if(bind_star_handling >= 1) {
        int rtype_saver;
        rtype_saver = get_rtype(lookfor);
        if(change_rtype(lookfor,RR_ANY) == JS_ERROR) {
            goto serv_fail;
        }
        spot_data = mhash_get(bighash,lookfor);
        if(spot_data.value != 0 && spot_data.datatype == MARA_DNS_LIST) {
            always_not_there = 4;
            }
        if(change_rtype(lookfor,rtype_saver) == JS_ERROR) {
            goto serv_fail;
            }
        }

    /* Anally strict RFC 1034 section 4.3.3 compliance.  If you
     * have b.example.com and *.example.com, a.b.example.com does
     * *not* match the star record */
    if(bind_star_handling == 2) {
        int r;
        r = star_collision(lookfor,bighash);
        if(r == -1) {
            goto serv_fail;
        } else if(r == 1) {
            udpnotfound(nxstore,header.id,sock,0,origq,0,desires_recursion,
                        ect,always_not_there);
            js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
            return JS_SUCCESS;
        }
    }

    /* OK, maybe there is a star record "above".  In other words,
       handle the case when they ask for foo.bar.example.com and we have
       a record for *.example.com */
    while(bobbit_starlabel(lookfor) > 0) {
        int rtype_saver = 1;

        if(bind_star_handling >= 1) {
            rtype_saver = get_rtype(lookfor);
        }
        spot_data = mhash_get(bighash,lookfor);
        point = spot_data.value;
        if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {

            /* We found the record */
            udpstar(spot_data.value,header.id,sock,0,origq,origq,
                            desires_recursion,0,ect);
            js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
            return JS_SUCCESS;
            }
        else {
            /* Make sure we don't incorrectly return a NXDOMAIN */
            if(bind_star_handling >= 1) {
                if(change_rtype(lookfor,RR_ANY) == JS_ERROR) {
                    goto serv_fail;
                }
                spot_data = mhash_get(bighash,lookfor);
                if(spot_data.value != 0 &&
                     spot_data.datatype == MARA_DNS_LIST) {
                    always_not_there = 4;
                } else

        /* If bind_star_handling is really high, look for any collision
         * that breaks RFC1034 section 4.3.3 compliance */
        if(bind_star_handling == 2) {
            int r;
            r = star_collision(lookfor,bighash);
            if(r == -1) {
                goto serv_fail;
            } else if(r == 1) {
                udpnotfound(nxstore,header.id,sock,0,origq,0,desires_recursion,
                        ect,(always_not_there & 3));
                js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
                return JS_SUCCESS;
            }
          }

                if(change_rtype(lookfor,rtype_saver) == JS_ERROR) {
                    goto serv_fail;
                }
              }
            }


        }

    /* Perhaps they have a star record which points to a CNAME (yes,
       some people actually do this) */

    /* All CNAME lookups are disabled when the dos_protection_level
     * is greater then seven */
    if(dos_protection_level > 7) {
            goto skip_starwhitis;
    }

    /* We need to restore "lookfor" because, again, we shredded
       both strings looking for a star label that was not a CNAME */
    if(js_copy(origq,lookfor) == JS_ERROR)
        goto serv_fail;
    if(fold_case(lookfor) == JS_ERROR)
        goto serv_fail;
    /* First, we make it a star label */
    if(make_starlabel(lookfor) == JS_ERROR)
        goto serv_fail;
    /* Then we make it a CNAME rtype */
    if(change_rtype(lookfor,RR_CNAME) == JS_ERROR)
        goto serv_fail;

    /* Look for the star CNAME record in the big hash */
    spot_data = mhash_get(bighash,lookfor);
    if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
        /* We have to make a form of origq that is a cname */
        if(js_copy(origq,lookfor) == JS_ERROR)
            goto serv_fail;
        if(change_rtype(lookfor,RR_CNAME) == JS_ERROR)
            goto serv_fail;
        udpstar(spot_data.value,header.id,sock,0,origq,lookfor,
                        desires_recursion,0,ect);
        js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
        return JS_SUCCESS;
        }

    /* Look for a star record "above" when it is a CNAME */
    while(bobbit_starlabel(lookfor) > 0) {
        spot_data = mhash_get(bighash,lookfor);

        /* Optional strict RFC1034 section 4.3.3 compliance */
        if(bind_star_handling >= 1) {
            int rtype_saver;
            rtype_saver = get_rtype(lookfor);
            if(change_rtype(lookfor,RR_ANY) == JS_ERROR) {
                goto serv_fail;
            }
            spot_data = mhash_get(bighash,lookfor);
            if(spot_data.value != 0 &&
                 spot_data.datatype == MARA_DNS_LIST) {
                goto skip_starwhitis;
            }
            if(change_rtype(lookfor,rtype_saver) == JS_ERROR) {
                goto serv_fail;
            }
        }

        if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
            if(js_copy(origq,lookfor) == JS_ERROR)
                goto serv_fail;
            if(change_rtype(lookfor,RR_CNAME) == JS_ERROR)
                goto serv_fail;
            udpstar(spot_data.value,header.id,sock,0,origq,lookfor,
                            desires_recursion,0,ect);
            js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
            return JS_SUCCESS;
            }
        }

    /* Perhaps they have something which ends in a star record;
       look for it.  starwhitis_end */

skip_starwhitis:

    /* If they have enabled stars at the end of hostnames */
    if(default_zonefile_enabled == 1) {
        int labels;
        /* Again, restore the original query */
        if(js_copy(origq,lookfor) == JS_ERROR)
            goto serv_fail;
        /* Now, start lopping off the dlabels from the *end* of the hostname */
        while((labels = bobbit_starlabel_end(lookfor)) > 0) {
            /* I like to know just how many labels we really have;
             * this will not break any real-world use (It only breaks
             * cases where they ask for "www.example.com" and we have
             * "www.example.com.*"; this will also break cases like
             * "a.b.c.d.e.[...].y.z.[...].a.b.c.d[...and so on...].z. Where
             * the domain name was very long with very short labels.  Again,
             * not in the real world ) */
            int store_rtype;
            if(labels > 120)
                    continue;
            spot_data = mhash_get(bighash,lookfor);
            point = spot_data.value;
            if(point != 0 && spot_data.datatype == MARA_DNSRR) {
                udpstar(point,header.id,sock,0,origq,origq,
                                desires_recursion,labels,ect);
                js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
                return JS_SUCCESS;
                }

            /* If we failed, see if we return "not there" or NXDOMAIN */
            store_rtype = get_rtype(lookfor);
            if(store_rtype == JS_ERROR) {
                goto serv_fail;
                }
            if(change_rtype(lookfor,RR_ANY) == JS_ERROR) {
                goto serv_fail;
                }
            spot_data = mhash_get(bighash,lookfor);
            if(change_rtype(lookfor,store_rtype) == JS_ERROR) {
                goto serv_fail;
                }
            if(spot_data.value != 0 && spot_data.datatype == MARA_DNS_LIST) {
                always_not_there = 4;
                }

            }
        /* OK, if we didn't find anything, give out a
         * NXDOMAIN or "Not there" reply. */

        /* Find the SOA record for the default zonefile */
        if(change_rtype(lookfor,RR_SOA) == JS_ERROR) {
                js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
                return JS_ERROR;
                }
        spot_data = mhash_get(bighash,lookfor);
        point = spot_data.value;
        if(point == 0 || spot_data.datatype != MARA_DNSRR) {
                js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
                return JS_ERROR;
                }
        udpnotfound(point,header.id,sock,0,origq,0,desires_recursion,ect,
                    always_not_there);
        js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
        return JS_SUCCESS;
        }

recursive_call:

    /* If we do not have authority for this record... */
    if(have_authority == 0 && default_zonefile_enabled != 1) {
        /* Ask other DNS servers for RRs which we do not have authoity
           for.  */

        /* Launch a separate thread to recursivly determine the
           host name in question */
#ifndef AUTHONLY
        if(has_recursive_authority == 1 && desires_recursion == 1) {
            /* Recursion only works for IPV4 */
            if(ect->type != 4) {
                js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
                return JS_ERROR;
                }
            /* Launch the thread that will process the request; we
             * copy ect->d over */
            z = ect->d;
            launch_thread(header.id,sock,*z,origq);
            }
        else
#endif
            udperror(sock,raw,0,0,REFUSED,
            "I'm sorry Dave (recurse attempt)",3,desires_recursion,ect,1);
        js_destroy(lookfor);
        js_destroy(origq);
        js_destroy(lc);
        return JS_SUCCESS;
        }

    /* Currently, MaraDNS will not support star records for NS
       subdelegation.  Code in ParseCsv1.c warns the user of this fact. */

    udpnotfound(nxstore,header.id,sock,0,origq,0,desires_recursion,ect,
                always_not_there);
    js_destroy(lookfor); js_destroy(origq); js_destroy(lc);

    return JS_SUCCESS;

    /* Work around C's lack of error handling and garbage collection with
       gotos */
    serv_fail:
        js_destroy(origq);
        if(lc != 0) {
            js_destroy(lc);
            }
        if(no_fingerprint != 1)
            udperror(sock,raw,0,lookfor,SERVER_FAIL,
                     "serv_fail in proc_query",2,desires_recursion,ect,1);
        js_destroy(lookfor);
        return JS_ERROR;

    not_impl:
        js_destroy(origq);
        js_destroy(lc);
        if(no_fingerprint != 1)
            udperror(sock,raw,0,lookfor,NOT_IMPLEMENTED,
                     "not_impl in proc_query",2,desires_recursion,ect,1);
        js_destroy(lookfor);
        return JS_ERROR;

    }

/* Bind to IPV4 UDP port 53. (or DNS_PORT if debugging MaraDNS on a
                              system where I do not have root, and
                              theirfore can not bind to a low port number)

   Input:  pointer to socket to bind on, js_string with the dotted-decimal
           ip address to bind to
   Output: JS_ERROR on error, JS_SUCCESS on success
*/

int udp_ipv4_bind(int *sockets, ipv4pair *addresses) {
    int len_inet; /* Length */
    struct sockaddr_in dns_udp;
    int counter;

    /* Sanity checks */
    if(sockets == 0)
        return JS_ERROR;

    counter = 0;

    /* Create a socket address to use with bind() */
    while(counter < 500 && addresses[counter].ip != 0xffffffff) {
        /* Create a raw UDP socket */
        if((sockets[counter] = socket(AF_INET,SOCK_DGRAM,0)) == -1) {
            return JS_ERROR;
            }

        memset(&dns_udp,0,sizeof(dns_udp));
        dns_udp.sin_family = AF_INET;
        /* DNS_PORT is usually 53, but can be another port.  Defined in
           MaraDNS.h */
        dns_udp.sin_port = htons(dns_port);
        if((dns_udp.sin_addr.s_addr = htonl(addresses[counter].ip))
           == INADDR_NONE)
            return JS_ERROR;

        len_inet = sizeof(dns_udp);

        /* Bind to the socket.  Note that we usually have to be root to
           do this */
        if(bind(sockets[counter],(struct sockaddr *)&dns_udp,len_inet) == -1)
            return JS_ERROR;

        counter++;
        }

    /* We are now bound to UDP port 53. (Or whatever DNS_PORT is) Leave */
    return JS_SUCCESS;
    }

/* We don't allow both recursive and ipv6 support, since the recursive
 * resolver is ipv4-only */

#ifdef IPV6
/* Cygwin doesn't have ipv6 yet */
#ifndef __CYGWIN__
/* Bind to IPV6 UDP port 53. (or DNS_PORT if debugging MaraDNS on a system
                         where I do not have root, and theirfore can not
                         bind to a low port number)
   Input:  pointer to socket to bind on, js_string with the dotted-decimal
           ip address to bind to
   Output: JS_ERROR on error, JS_SUCCESS on success
*/

int udp_ipv6_bind(int *sock, int splace, js_string *ipv6_address) {
    int len_inet; /* Length */
    struct sockaddr_in6 dns_udp;
    int counter;
    char ascii_ipv6[128];

    if(js_js2str(ipv6_address,ascii_ipv6,100) == JS_ERROR) {
            return JS_ERROR;
    }
    if(splace >= 501) {
            return JS_ERROR;
    }

    /* Sanity checks */
    if(socket == 0)
        return JS_ERROR;

    counter = 0;

    if((sock[splace] = socket(AF_INET6,SOCK_DGRAM,0)) == -1) {
            return JS_ERROR;
            }

    memset(&dns_udp,0,sizeof(dns_udp));
    dns_udp.sin6_family = AF_INET6;
    /* DNS_PORT is usually 53, but can be another port.  Defined in
       MaraDNS.h */
    dns_udp.sin6_port = htons(dns_port);
    inet_pton(AF_INET6,ascii_ipv6,&(dns_udp.sin6_addr));

   /* if((dns_udp.sin6_addr = htonl(addresses[counter].ip))
         == INADDR_NONE)
            return JS_ERROR; */

    len_inet = sizeof(dns_udp);

    /* Bind to the socket.  Note that we usually have to be root to
       do this */
    if(bind(sock[splace],(struct sockaddr *)&dns_udp,len_inet) == -1)
            return JS_ERROR;

    /* We are now bound to UDP port 53. (Or whatever DNS_PORT is) Leave */
    return JS_SUCCESS;
    }
#endif /* __CYGWIN__ */
#endif /* IPV6 */

/* Return a list of addresses we are bound to
 * Input: None
 * Output: bind_addresses */
ipv4pair *get_bind_addr_list() {
        return bind_addresses;
}

/* Return a list of addresses to synthesize for people who want different
 * synthetic NS IPs than the IPs MaraDNS is bound to; this will return
 * the bind_address list if the synthip list is not set */
ipv4pair *get_synthip_list() {
        if(csv2_synthip_list[0].ip == 0xffffffff) {
                return bind_addresses;
        }
        return csv2_synthip_list;
}

/* Get information from a previously binded UDP socket
   Input:  list of UDP bound sockets, list of addresses we are bound to,
           pointer to sockaddr structure that will contain
           the IP of the system connecting to us, pointer to js_string
           object that will have the data in question, maximum allowed
           length of data we receive
   Output: JS_ERROR on error, socket we got packet from on success
*/

int getudp(int *sock,ipv4pair *addr,conn *ect,
           js_string *data, int max_len, int have_ipv6_address) {
    int len_inet, counter, len;
    fd_set rx_fd;
    int select_output;
    int max_socket;
    struct timeval timeout;
    struct sockaddr_in *ipv4_client;
#ifdef IPV6
/* Cugwin doesn't support ipv6 yet */
#ifndef __CYGWIN__
    struct sockaddr_in6 *ipv6_client;
#endif /* __CYGWIN__ */
#endif

    /* Sanity checks */
    if(ect == 0 || data == 0)
        return JS_ERROR;
    if(js_has_sanity(data) == JS_ERROR)
        return JS_ERROR;
    if(data->unit_size != 1)
        return JS_ERROR;
    if(max_len < 0 || max_len >= data->max_count)
        return JS_ERROR;

    FD_ZERO(&rx_fd);
    counter = 0;
    max_socket = 0;
    while(counter < 500 && addr[counter].ip != 0xffffffff) {
        FD_SET(sock[counter],&rx_fd);
        if((sock[counter] + 1) > max_socket) {
            max_socket = sock[counter] + 1;
            }
        counter++;
        }
#ifdef IPV6
/* Cygwin doesn't have ipv6 yet */
#ifndef __CYGWIN__
    if(have_ipv6_address == 1) {
        FD_SET(sock[counter],&rx_fd);
        if((sock[counter] + 1) > max_socket) {
            max_socket = sock[counter] + 1;
            }
        }
#endif /* __CYGWIN__ */
#endif
    if(max_socket == 0) /* No sockets */ {
        return JS_ERROR;
        }

    timeout.tv_sec = 1; /* Check for HUP signal every second */
    timeout.tv_usec = 0;

    /* OK, wait for activity on any of those sockets */
    select_output = select(max_socket,&rx_fd,NULL,NULL,&timeout);

    if(select_output <= 0) { /* 0: Timeout; less than 0: error */
        return JS_ERROR;
        }

    /* Figure out which socket gave us something */
    counter = 0;
    while(counter < 500 && addr[counter].ip != 0xffffffff) {
        if(FD_ISSET(sock[counter],&rx_fd)) {
            len_inet = sizeof(struct sockaddr_in);
            ect->addrlen = len_inet;

            ipv4_client = js_alloc(1,sizeof(struct sockaddr_in));
            if(ipv4_client == 0)
                    return JS_ERROR;

#ifdef SELECT_PROBLEM
            fcntl(sock[counter], F_SETFL, O_NONBLOCK);
#endif
            len = recvfrom(sock[counter],data->string,max_len,0,
                           (struct sockaddr *)ipv4_client,
                           (socklen_t *)&(ect->addrlen));
            if(len < 0) {
                js_dealloc(ipv4_client);
                return JS_ERROR;
                }
            ect->type = 4;
            ect->d = ipv4_client;

            data->unit_count = len;

            return sock[counter];
            }
        counter++;
        }
#ifdef IPV6
/* Cygwin doesn't have ipv6 yet */
#ifndef __CYGWIN__
    if(have_ipv6_address == 1) {
        if(FD_ISSET(sock[counter],&rx_fd)) {
            socklen_t stupid_gcc_warning; /* To get rid of gcc warning */
            len_inet = sizeof(struct sockaddr_in6);
            ect->addrlen = len_inet;
            ipv6_client = js_alloc(1,sizeof(struct sockaddr_in6));
            if(ipv6_client == 0)
                    return JS_ERROR;

#ifdef SELECT_PROBLEM
            fcntl(sock[counter], F_SETFL, O_NONBLOCK);
#endif
            stupid_gcc_warning = ect->addrlen;
            len = recvfrom(sock[counter],data->string,max_len,0,
                           (struct sockaddr *)ipv6_client,
                           &stupid_gcc_warning);
            ect->addrlen = stupid_gcc_warning;

            if(len < 0) {
                js_dealloc(ipv6_client);
                return JS_ERROR;
                }

            ect->type = 6;
            ect->d = ipv6_client;

            data->unit_count = len;

            return sock[counter];
            }
        }
#endif /* __CYGWIN__ */
#endif /* IPV6 */

    /* "JS_ERROR" means "nobody talked to us in the last second" */
    ect->type = 0;
    ect->d = 0;
    return JS_ERROR;

    }

/* Function that initialized the ra_data (bogus reply to send if MaraDNS
 * is set up to reject aaaa or ptr queries, and the DNS client sends a
 * aaaa/ptr query */

rr *init_ra_data() {
        js_string *rq; /* Query */
        js_string *rv; /* Value */
        rr *lra_data;
        /* Allocate memory */
        rq = js_create(256,1);
        rv = js_create(256,1);
        if(rq == 0 || rv == 0) {
                harderror("reject_aaaa error 1");
        }
        if((lra_data = js_alloc(1,sizeof(rr))) == 0) {
                harderror("reject_aaaa error 2");
        }

        /* Clear out all of ra_data fields */
        init_rr(lra_data);
        /* The following porbably isn't necessary */
        lra_data->expire = lra_data->ttl = lra_data->authoritative = 0;
        lra_data->rr_type = 0; lra_data->next = lra_data->ip = 0;
        lra_data->query = lra_data->data = 0; lra_data->seen = 0;
        lra_data->zap = 0; lra_data->ptr = 0;

        /* Set up query ("." type SOA) */
        js_addbyte(rq,0);
        js_adduint16(rq,RR_SOA);

        /* Set up value (SOA: It ain't there) */
        /* SOA field 1: SOA origin (make '.') */
        js_addbyte(rv,0);
        /* SOA field 2: SOA Mname (email address of person in charge; make
         * 'm@m.m') */
        js_addbyte(rv,1); js_addbyte(rv,'m'); /* m@ ... */
        js_addbyte(rv,1); js_addbyte(rv,'m'); /* ... m. ... */
        js_addbyte(rv,1); js_addbyte(rv,'m'); /* ... m ... */
        js_addbyte(rv,0); /* Final 'dot' */
        /* SOA field 3: Serial (make 1) */
        js_adduint16(rv,0); js_adduint16(rv,1);
        /* SOA field 4: Refresh (make 60) */
        js_adduint16(rv,0); js_adduint16(rv,60);
        /* SOA field 5: Retry (make 60) */
        js_adduint16(rv,0); js_adduint16(rv,60);
        /* SOA field 6: Expire (make 60) */
        js_adduint16(rv,0); js_adduint16(rv,60);
        /* SOA field 7: TTL (make 60) */
        js_adduint16(rv,0); js_adduint16(rv,60);

        /* OK, strings set up; add to ra_data */
        lra_data->expire = 0;
        lra_data->ttl = 60;
        lra_data->authoritative = 1;
        lra_data->rr_type = RR_SOA;
        lra_data->data = rv;
        lra_data->query = rq;
        return lra_data;
}

/* The core of the DNS server */

int main(int argc, char **argv) {

    js_string *mararc_loc = 0, *errors = 0,
              *bind_address = 0, *ipv6_bind_address = 0,
              *csv2_synthip_address = 0,
              *ipv4_bind_address = 0, *incoming = 0,
              *uncomp = 0, *verbstr = 0;
#ifndef MINGW32
    unsigned char chroot_zt[255];
#ifndef __CYGWIN__
    uid_t uid;
#endif
    gid_t gid;
#endif
    int errorn, value, maxprocs, counter;
    int sock[514];
    int cache_size;
    int min_ttl_n = 300, min_ttl_c = 300;
    int timestamp_type = 5; /* Type of timestamp */
#ifndef AUTHONLY
    int max_glueless; /* Maximum allowed glueless level */
    int max_q_total; /* Maximum total queries in attempt to resolve hostname */
    int timeout; /* Maximum time to wait for a remote server when performing
                    a recursive query */
    int handle_noreply = 1; /* How tohandle a recursive query when you don't
                             * get a remote reply at all */
    int retry_cycles = 2; /* Number of times to try and contact all of the
                           * name servers for a given domain */
    int thread_overhead = THREAD_OVERHEAD; /* The amount of memory we need
                                            * to allow to be allocated for
                                            * threads */
#else
    int thread_overhead = 0; /* No memory needed for threads */
/* Cygwin doesn't have ipv6 support yet */
#ifndef __CYGWIN__
    struct sockaddr_in6 *clin6;
#endif /* __CYGWIN__ */
#endif
    int verbose_query = 0;
    struct sockaddr client;
    struct sockaddr_in *clin = 0; /* So we can log the IP */
#ifndef MINGW32
    struct rlimit rlim;
#endif /* MINGW32 */
    int have_ipv6_address = 0;
    int default_dos_level = 78; /* 78: Recursive-only; 0: default when
                                 * there is one or more zonefiles */

#ifdef MINGW32
    /* Windows-specific initialization of socket */
    WSADATA wsaData;
    WORD wVersionRequested = MAKEWORD(2,2);
    WSAStartup( wVersionRequested, &wsaData);
#endif /* MINGW32 */

#ifndef AUTHONLY
    int recurse_min_bind_port = 15000;
    int recurse_number_ports = 4096;
#endif

    memset(&client,0,sizeof(client)); /* Initialize ya variables */
    clin = (struct sockaddr_in *)&client;
#ifdef AUTHONLY
/* Cygwin doesn't have ipv6 yet */
#ifndef __CYGWIN__
    clin6 = (struct sockaddr_in6 *)&client;
#endif /* __CYGWIN__ */
#endif

    /* Initialize the strings (allocate memory for them, etc.) */
    if((mararc_loc = js_create(256,1)) == 0)
        harderror(L_MLC); /* "Could not create mararc_loc string" */
    if(js_set_encode(mararc_loc,MARA_LOCALE) == JS_ERROR)
        harderror(L_MLL); /* "Could not set locale for mararc_loc string" */

    if((errors = js_create(256,1)) == 0)
        harderror(L_EC); /* "Could not create errors string" */
    if(js_set_encode(errors,MARA_LOCALE) == JS_ERROR)
        harderror(L_EL); /* "Could not set locale for errors string" */

    if((incoming = js_create(768,1)) == 0)
        harderror(L_IC); /* "Could not create incoming string" */
    if(js_set_encode(incoming,MARA_LOCALE) == JS_ERROR)
        harderror(L_IL); /* "Could not set locale for incoming string" */

    if((uncomp = js_create(768,1)) == 0)
        harderror(L_UCC); /* "Could not create uncomp string" */
    if(js_set_encode(uncomp,MARA_LOCALE) == JS_ERROR)
        harderror(L_UCL); /* "Could not set locale for uncomp string" */

    /* First, find the mararc file */
    if(argc == 1) { /* No arguments */
        if(find_mararc(mararc_loc) == JS_ERROR)
            harderror(L_LOC_MARARC); /* "Error locating mararc file" */
        }
    else if(argc==2) { /* maradns -v or maradns --version */
        printf("%s %s\n%s %s\n%s\n",L_THISIS,VERSION,L_COMPILED,
               COMPILED,L_RTFM); /* "This is MaraDNS version blah blah blah */
        exit(0);
        }
    else if(argc==3) { /* maradns -f /wherever/mararc */
        if(js_qstr2js(mararc_loc,argv[2]) == JS_ERROR)
            harderror(L_MARARC_ARG); /* "Could not get mararc from command line" */
        }
    else
        harderror(L_USAGE); /* "Usage: maradns [-f mararc_location]" */

    /* Then parse that file */
    if(read_mararc(mararc_loc,errors,&errorn) == JS_ERROR) {
        harderror(L_MARARC_PARSE); /* "Error parsing contents of mararc file" */
        }
    js_destroy(mararc_loc);
    if(errorn != 0) {
        /* Print this out at log level 0 because it is a fatal error */
        if(errorn != -1)
          /* "Error parsing contents of mararc file on line " */
          printf("%s%d%s",L_MARARC_LINE,errorn,L_N); /* errorn, "\n" */
        printf("%s",L_ERROR_CODE); /* "Error code: " */
        js_show_stdout(errors);
        printf("%s",L_N); /* "\n" */
        exit(2);
        }

    /* There are too many greedy lawyers in the US */
    verbstr = read_string_kvar("hide_disclaimer");
    if(verbstr == 0 || js_length(verbstr) != 3) {
        printf("%s","THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS OR\n");
        printf("%s","IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n");
        printf("%s","OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n");
        printf("%s","IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\n");
        printf("%s","INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n");
        printf("%s","(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n");
        printf("%s","SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n");
        printf("%s","HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n");
        printf("%s","STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n");
        printf("%s","IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n");
        printf("%s","POSSIBILITY OF SUCH DAMAGE.\n");
        printf("\nTo not display this message, add the follwing to your ");
        printf("mararc file:\n\nhide_disclaimer = \"YES\"\n\n");
        }
    js_destroy(verbstr);
    verbstr = 0;
    /* Get in to a state of least privledge ASAP */

    /* Limit the maximum number of processes */
    maxprocs = read_numeric_kvar("maxprocs",64);
    if(maxprocs == 0) {
        maxprocs = 64;
        }

    if(maxprocs > 5000) {
        maxprocs = 5000;
        mlog(L_MAXPROC_MAX); /* "Maxprocs can not be greater than 5000" */
        }
#ifndef MINGW32
#ifndef DARWIN
    rlim.rlim_cur = rlim.rlim_max = maxprocs;

    /* If this OS supports setrlimit and if setrlimit fails, bail (the ENOSYS
       check is there so OSes w/o setrlimit support can still run MaraDNS) */
#ifdef RLIMIT_NPROC
    if(setrlimit(RLIMIT_NPROC,&rlim) != 0 && errno != ENOSYS)
        sys_harderror(L_MAXPROC_SET); /* "Unable to set maximum number of processes" */
#endif /* RLIMIT_NPROC */
#endif /* DARWIN */
#endif /* MINGW32 */

    /* Determine how many elements the cache can have */
    cache_size = read_numeric_kvar("maximum_cache_elements",1024);
    if(cache_size < 32 || cache_size > 268435455) /* 2^28 - 1 */ {
        mlog(L_INVALID_CACHESIZE);
        cache_size = 1024;
        }

    /* Determine the level of error reporting */
    log_level = read_numeric_kvar("verbose_level",1);
#ifndef AUTHONLY
    init_rlog_level(log_level);
#endif

    /* If they want a synthetic IP given when the recursive resolver would
     * otherwise give a SOA "not there" record (or when it's impossible to
     * contact an upstream_server if handle_noreply is appropriately set),
     * prepare most of the synthetic answer we will give them. */
    verbstr = read_string_kvar("notthere_ip");
    if(verbstr != 0) {
        notthere_ip = make_notthere_ip(verbstr);
        js_destroy(verbstr);
    }
    verbstr = 0;

    /* Determine if we will handle star records the way BIND does:
       . If a non-A record for foo.example.com exists
       . And an A record for *.example.com exists
       . And the user asks for the A record for "foo.example.com"
       . Return "not there" instead of the A record attached to "*.example.com"

     If this is 0, we return the A record attached to "*.example.com"
     If this is 1, we return "not there" (since at least one RR for
     foo.example.com exists)
     If this is any other value, exit with a fatal error
     */
     bind_star_handling = read_numeric_kvar("bind_star_handling",1);
     if(bind_star_handling < 0 || bind_star_handling > 2) {
         harderror("bind_star_handling must have a value of 0, 1, or 2");
     }

     /* remote_admin: Whether we allow remote administration.  Currently,
        the only remote administration allowed is to change the
        verbose_level while MaraDNS is running.  I am allowing this
        under limited circumstances (your IP *must* be on the
        admin_acl list for this to work) because this will help
        debug some wonkeyness which happens when MaraDNS is very heavily
        loaded (1200 queries a second in large datacenters).  I personally
        think the wonkeyness is because the underlying kernel is overloaded,
        but this will allow more comprehensive debugging */
     remote_admin = read_numeric_kvar("remote_admin",0);
     if(remote_admin < 0 || remote_admin > 1) {
         harderror("remote_admin must have a value of 0 or 1");
     }

    /* Set the timestamp type */
    timestamp_type = read_numeric_kvar("timestamp_type",5);
    set_timestamp(timestamp_type);

    /* Get the minttl values from the kvar database (if there) */
    min_ttl_n = read_numeric_kvar("min_ttl",300);
    min_ttl_c = read_numeric_kvar("min_ttl_cname",min_ttl_n);
    min_visible_ttl = read_numeric_kvar("min_visible_ttl",30);
    if(min_visible_ttl < 5)
        min_visible_ttl = 5;

#ifndef AUTHONLY
    handle_noreply = read_numeric_kvar("handle_noreply",1);
    if(handle_noreply < 0 || handle_noreply > 2) {
        harderror("handle_noreply must have a value between 0 and 2");
    }
    init_handle_noreply(handle_noreply);

    retry_cycles = read_numeric_kvar("retry_cycles",2);
    if(retry_cycles < 1 || retry_cycles > 31) {
        harderror("retry_cycles must have a value between 1 and 31");
        }
    init_retry_cycles(retry_cycles);

    recurse_min_bind_port = read_numeric_kvar("recurse_min_bind_port",15000);
    if(recurse_min_bind_port < 1024) {
        harderror("recurse_min_bind_port must have a minimum value of 1024");
    }
    recurse_number_ports = read_numeric_kvar("recurse_number_ports",4096);
    if(recurse_number_ports < 256 || recurse_number_ports > 32768) {
  harderror("recurse_number_ports must have a value between 256 and 32768");
        }
    /* I learned this trick during an interview at Google */
    if((recurse_number_ports & (recurse_number_ports - 1)) != 0) {
        harderror("recurse_number_ports must be a power of two");
    }
    if(recurse_min_bind_port + recurse_number_ports >= 65535) {
        harderror(
"recurse_min_bind_port + recurse_number_ports too large for 16-bit portnum");
        }
    set_port_range(recurse_min_bind_port,recurse_number_ports);
#endif /* AUTHONLY */

    /* Set the values */
#ifndef AUTHONLY
    set_min_ttl(min_ttl_n,min_ttl_c);
#endif

    /* Determine if we should make a "best faith" effort to have
       no MaraDNS-specific features */
    no_fingerprint = read_numeric_kvar("no_fingerprint",0);
    /* We may set up a bogus "not there" reply */
    reject_aaaa = read_numeric_kvar("reject_aaaa",0);
    reject_ptr = read_numeric_kvar("reject_ptr",0);
    if(reject_aaaa != 0 || reject_ptr != 0) {
            ra_data = init_ra_data();
    }

    /* Determine if we should return NS and SOA records when given a RR_ANY
       query */
    rrany_set = read_numeric_kvar("default_rrany_set",3);

    /* There are 3 user-customizable parameters which determine the maximum
       number of records we spit out for various chains of records */
    /* Maximum number of A records we show for a given host name in the
       additional section */
    max_ar_chain = read_numeric_kvar("max_ar_chain",1);
    /* Maximum number of records we show for any non-A chain in the answer
       or additional section */
    max_chain = read_numeric_kvar("max_chain",8);
    /* Maximum number of records we show total */
    max_total = read_numeric_kvar("max_total",20);
    /* Whether to supress dangling CNAME warnings */
    no_cname_warnings = read_numeric_kvar("no_cname_warnings",0);

    verbose_query = read_numeric_kvar("verbose_query",0);

    /* Set the dns_port */
    dns_port = read_numeric_kvar("dns_port",53);
    if(dns_port < 1 || dns_port > 65530) {
        harderror("dns_port must be between 1 and 65530");
        exit(1);
    }

    /* Get the values for the synthetic SOA serial and the synthetic SOA
       origin (called MNAME in RFC1035) */
    synth_soa_serial = read_numeric_kvar("synth_soa_serial",1);
    if(synth_soa_serial < 1 || synth_soa_serial > 2) {
        harderror("Fatal: synth_soa_serial must be 1 or 2\n");
        }
#ifdef MINGW32
    /* Mingw32 doesn't have gmtime_r, so this has to puke.  Just ignore
     * that stupid dnsreport.com warning if you're using the win32 port */
    if(synth_soa_serial == 2) {
     harderror("Fatal: synth_soa_serial can not be 2 with the Windows port\n");
    }
#endif /* MINGW32 */

    verbstr = read_string_kvar("synth_soa_origin");
    if(verbstr != 0 && js_length(verbstr) > 0) {
        synth_soa_origin = js_create(256,1);
        if(synth_soa_origin == 0) {
            harderror("Fatal: can not create synth_soa_origin string");
            }
        if(js_qstr2js(synth_soa_origin,"Z") != JS_SUCCESS) {
            harderror("Fatal: could not make synth_soa_origin string Z");
            }
        if(js_append(verbstr,synth_soa_origin) == JS_ERROR) {
            harderror("Fatal: could not append to synth_soa_origin string");
            }
        /* We should see if the origin already has a dot on the end */
        if(js_qappend(".",synth_soa_origin) == JS_ERROR) {
            harderror("Fatal: could not append final dot to soa_synth_origin");
            }
        /* Now we make that raw data */
        if(hname_2rfc1035(synth_soa_origin) <= 0) {
            harderror("Fatal: Malformed synth_soa_origin value.\n"
            "Please make sure that synth_soa_origin is a valid hostname\n"
            "*without* a dot at the end.  For example:\n\n"
            "\tsynth_soa_origin = \"example.com\"\n");
            }
        }


#ifdef AUTHONLY
    /* Determine the list of ipv4 ips that we send long packets to */
    for(counter = 0; counter < 511; counter++)
        long_packet[counter].ip = 0xffffffff;
    verbstr = read_string_kvar("long_packet_ipv4");
    if(verbstr != 0 && js_length(verbstr) > 0) {
        if(make_ip_acl(verbstr,long_packet,500,0) == JS_ERROR)
            harderror("Could not make long packet list");
    }
    if(verbstr != 0) {
        js_destroy(verbstr);
        verbstr = 0;
    }
#endif

#ifndef AUTHONLY
    /* Determine what the ACL is for recursive queries */
    /* Initialize the ACL list */
    for(counter = 0; counter < 511; counter++)
        recurse_acl[counter].ip = 0xffffffff;
    /* Read in the ACL list from the mararc file */
    verbstr = read_string_kvar("recursive_acl");
    if(verbstr != 0 && js_length(verbstr) > 0) {
        recursion_enabled = 1;
        /* If recursive ACL is set, then we set all the variables
           which use recursion */
        if(make_ip_acl(verbstr,recurse_acl,500,0) == JS_ERROR)
            harderror(L_ACL_LIST_RECURSE); /* "Could not make ip ACL list" */

        /* Determine what the maximum glueless level is */
        max_glueless = read_numeric_kvar("max_glueless_level",10);
        if(max_glueless == 0)
            harderror(L_INVALID_MAXGLUE); /* max_glueless_level needs to be a number, and be greater than zero */

        /* Determine the total numer of queries to perform in a recursive
           query */
        max_q_total = read_numeric_kvar("max_queries_total",32);
        if(max_q_total == 0)
            harderror(L_INVALID_MAXQTOTAL); /* max_queries_total needs to be a number, and be greater than zero */

        /* Determine the maximum time to wait for a remote server (in
           seconds) */
        timeout = read_numeric_kvar("timeout_seconds",2);
        if(timeout < 1)
            harderror(L_INVALID_TIMEOUT); /* timeout_seconds needs to be a number, and be greater than zero */

        /* Load the "seed" data in to the DNS cache */
        counter = init_cache(cache_size,maxprocs,max_glueless,max_q_total,
                             timeout,verbose_query);
        thread_overhead += maxprocs * PER_THREAD_MEM;
        if(counter < 0) {
            switch(counter) {
                case -7:
                    /* In MaraDNS 1.2, we now use a built-in list of the
                     * ICANN root servers if the root_servers variable
                     * is not set */
                    harderror(L_SETROOTNS); /* root_servers["."] must be set in the mararc file; e.g. root_servers["."] = "198.41.0.4" */
                case -11:
                    harderror(L_BADROOTNS); /* root_servers["."] in the mararc file is invalid.; Example good value: root_servers["."] = "198.41.0.4" */
                case -14:
                    harderror(L_CHOOSEONE); /* Both root_servers and upstream_servers are set in the mararc file... please choose one  */
                default:
                    printf(L_ERROR_VALUE); /* "Error value (for software devlopers): " */
                    printf("%d\n",counter);
                    harderror(L_INITCACHE_FAIL); /* Init_cache() failed */
                }
            }

    /* Read in the list of spam-friendly DNS servers, which we will
       refuse to get data from */
    if(verbstr != 0) { js_destroy(verbstr); verbstr = 0; }
    verbstr = read_string_kvar("spammers");

    /* If there is a spam-friendly list, read it in */
    if(verbstr != 0 && js_length(verbstr) > 0) {
        if(init_spammers(verbstr) == JS_ERROR)
            harderror(L_INIT_SPAMMERS); /* "Could not make spammers list" */
        }
    /* Otherwise, make sure the spammers list is an empty list */
    else {
        if(init_spammers(0) == JS_ERROR)
            harderror(L_INIT_SPAMMERS); /* "Could not make spammers list" */
        }

    if(verbstr != 0) { js_destroy(verbstr); verbstr = 0; }
    /* BEGIN RNG USING CODE */
    /* Determine which file to read the key from */
    verbstr = read_string_kvar("random_seed_file");
    if(verbstr != 0 && js_length(verbstr) > 0) {
        counter = init_rng(verbstr,0);
        if(counter < 0) {
            switch(counter) {
                case -2:
                    sys_harderror(L_OPENSEED_FAIL); /* Could not open the random_seed_file */
                case -3:
                    harderror(L_NEED16BYTES); /* The random_seed_file needs to be 16 bytes or longer */
                default:
                    harderror(L_INITCRYPTO_FAIL); /* "Init_crypto() failed" */
                }
            }
        }
    else if((recurse_acl[0]).ip != 0xffffffff) {
        /* Default random_seed_file value: /dev/urandom */
        if(verbstr != 0) { js_destroy(verbstr); verbstr = 0; }
        verbstr = js_create(256,1);
        if(js_qstr2js(verbstr,"/dev/urandom") == JS_ERROR)
             harderror(L_KVAR_Q);
        counter = init_rng(verbstr,0);
        if(counter < 0) {
            switch(counter) {
                case -2:
                    sys_harderror(
"MaraDNS was unable to open up the file /dev/urandom\n"
"MaraDNS needs a random seed for security purposes.  Please create a file \n"
"with 16 or more bytes of random data in it, and have your mararc file point\n"
"to this random seed as follows:\n\n"
"\trandom_seed_file = \"filename\"\n\nWhere filename is the name of the file\n"
"with 16 or more bytes of random data.\n");
                case -3:
                    harderror("The file /dev/urandom is not 16 bytes long.\n"
"This is very unusual.\n\n"
"MaraDNS needs a random seed for security purposes.  Please create a file \n"
"with 16 or more bytes of random data in it, and have your mararc file point\n"
"to this random seed as follows:\n\n"
"\trandom_seed_file = \"filename\"\n\nWhere filename is the name of the file\n"
"with 16 or more bytes of random data.\n");
                default:
                    harderror(L_INITCRYPTO_FAIL); /* "Init_crypto() failed" */
                }
            }
        }
    /* END RNG USING CODE */
    } else { /* We can lower the cap of maximum memory usable */
        thread_overhead = 0; }
#else /* AUTHONLY */
    /* Die right away so an admin isn't scratching their head wondering why
     * Mara isn't able to recursively resolve hostname */
    if(verbstr != 0) { js_destroy(verbstr); verbstr = 0; }
    verbstr = read_string_kvar("recursive_acl");
    if(verbstr != 0 && js_length(verbstr) > 0) {
            harderror("No recursion when MaraDNS is compiled authonly");
    }
#endif /* AUTHONLY */

    /* Set up the list of IPs allowed to look at (and possibly change)
     * MaraDNS' internal information */
    for(counter = 0; counter < 511; counter++)
        admin_acl[counter].ip = 0xffffffff;
    if(verbstr != 0) { js_destroy(verbstr); verbstr = 0; }
    verbstr = read_string_kvar("admin_acl");
    if(verbstr != 0 && js_length(verbstr) > 0) {
        if(make_ip_acl(verbstr,admin_acl,500,0) == JS_ERROR)
            harderror("Could not make admin_acl list");
        }
    default_dos_level = 78; /* 78: Recursive-only; 0: default when
                             * there is one or more zonefiles */
    if(admin_acl[0].ip != 0xffffffff) {
        default_dos_level = 79; /* 79: Only check for Tversion.maradns. */
        }

    /* Anything after this does not need recursion enabled for the
       kvar in question to be read */

    /* Determine whether to wait before sending a reply (used only
       for debugging) */
    debug_delay = read_numeric_kvar("debug_response_delay",0);

    /* Set the debug_msg_level to the Debug message level they want */
    debug_msg_level = read_numeric_kvar("debug_msg_level",1);

#ifndef MINGW32
    /* Determine if we are root */
#ifndef __CYGWIN__
    if(geteuid() == 0) {
#endif

        if(verbstr != 0) { js_destroy(verbstr); verbstr = 0; }
        verbstr = read_string_kvar("chroot_dir");
        if(verbstr == 0) {
            harderror(L_CHROOT_KVAR);
            }
        if(js_length(verbstr) <= 0)
            harderror(L_CHROOT_KVAR); /* "Problem getting chroot kvar.\nYou must have chroot_dir set if you start this as root" */
        if(js_js2str(verbstr,(char *)chroot_zt,200) == JS_ERROR)
            harderror(L_CHROOT_NT); /* "Problem making chroot nt string.\nMake sure the chroot directory is 200 chars or less" */
        if(chdir((char *)chroot_zt) != 0)
            sys_harderror(L_CHROOT_CHANGE); /* "Problem changing to chroot dir.\nMake sure chroot_dir points to a valid directory" */
#if ! (defined __CYGWIN__ || defined QNX)
        if(chroot((char *)chroot_zt) != 0)
            sys_harderror(L_CHROOT_DO);  /* "Problem changing the root directory." */
#endif

        mlog(L_CHROOT_SUCCESS); /* "Root directory changed" */
#else
    if(1 == 1) {
#endif /* MINGW32 */

        /* Bind to port 53
           To Do: use capset to give us privledged bind abilities without
                  needing to be root.
        */
        bind_address = read_string_kvar("bind_address");
        csv2_synthip_address = read_string_kvar("csv2_synthip_list");
        ipv4_bind_address = read_string_kvar("ipv4_bind_addresses");
        /* If there is no bind address set, have MaraDNS return an error */
        if(js_length(ipv4_bind_address) < 1 && js_length(bind_address) < 1) {
            harderror("The mararc variable ipv4_bind_addresses must be set.\n"
"This is the IP or list of IPs that your MaraDNS DNS server will have.\n"
"This can be a single IP, such as:\n\n"
"\tipv4_bind_addresses = \"127.0.0.1\"\n"
"\nOr a list of IPs, such as:\n\n"
"\tipv4_bind_addresses = \"127.0.0.1,10.7.14.86\"\n");
            }
        if(js_length(ipv4_bind_address) >= 1 &&
           js_length(bind_address) >=1) {
  harderror("Both bind_address and ipv4_bind_addresses can not be set.");
            }
        for(counter = 0; counter < 512 ; counter++) {
            bind_addresses[counter].ip = 0xffffffff;
            csv2_synthip_list[counter].ip = 0xffffffff;
            sock[counter] = 0;
            }
        if(js_length(bind_address) >=1) {
            if(make_ip_acl(bind_address,bind_addresses,500,0) == JS_ERROR)
                harderror("Can not make ip acl for the bind addresses"
"\nMake sure you have a correctly formatted value for the bind_address in your"
"\nmararc file");
            } else {
            if(make_ip_acl(ipv4_bind_address,bind_addresses,500,0) == JS_ERROR)
                harderror("Can not make ip acl for the ipv4 bind addresses\n"
"Make sure you have a correctly formatted value for the ipv4_bind_address in"
"\nyour mararc file");
            }

        if(js_length(csv2_synthip_address) >= 1) {
            if(make_ip_acl(csv2_synthip_address,csv2_synthip_list,500,0) ==
                            JS_ERROR)
                harderror("Looks like there is a malformed csv2_synthip_list");
            }

        if(udp_ipv4_bind(sock,bind_addresses) == JS_ERROR)
            sys_harderror(L_BINDFAIL); /* "Problem binding to port 53.\nMost likely, another process is already listening on port 53" */
        if(js_length(bind_address) >=1) {
            zjlog(L_BIND2ADDR,bind_address); /* "Binding to address " */
        } else {
            zjlog(L_BIND2ADDR,ipv4_bind_address); /* "Binding to address " */
        }
        mlog(L_BIND_SUCCESS);  /* "Socket opened on UDP port 53" */
        js_destroy(bind_address);
        js_destroy(csv2_synthip_address);
        js_destroy(ipv4_bind_address);

        ipv6_bind_address = read_string_kvar("ipv6_bind_address");
#ifndef IPV6
        /* If there is an ipv6 bind address, have MaraDNS return an error */
        if(js_length(ipv6_bind_address) >= 1) {
                harderror("maradns must be compiled as authonly to have ipv6 support\n"
                "./configure --authonly ; make will compile maradns thusly\n"
                "Note that this will disable recursion and caching");
        }
#else
/* Cygwin doesn't have ipv6 support yet */
#ifndef __CYGWIN__
        if(js_length(ipv6_bind_address) >= 1) {
                for(counter = 0; counter < 502; counter++) {
                        if(sock[counter] == 0)
                            break;
                }
                if(counter >= 501) {
                        harderror("We are bound to too many ipv4 addresses");
                }
                if(udp_ipv6_bind(sock,counter,ipv6_bind_address) == JS_ERROR) {
                        sys_harderror("Binding to a ipv6 socket failed");
                }
                have_ipv6_address = 1;
        }
#else
        if(js_length(ipv6_bind_address) >= 1) {
                printf("Cygwin doesn't have ipv6 support\n");
                exit(1);
        }
#endif /* __CYGWIN__ */
#endif /* IPV6 */

#ifndef MINGW32
        /* Drop the elevated privileges */
        /* First, change the GID */
        gid = read_numeric_kvar("maradns_gid",MARADNS_DEFAULT_GID);
#ifndef __CYGWIN__
        /* Drop all supplemental groups */
        setgroups(1,&gid);
#endif /* __CYGWIN__ */
        /* Set the group ID */
        setgid(gid);

#ifndef __CYGWIN__
        /* Next, change the UID */
        uid = read_numeric_kvar("maradns_uid",MARADNS_DEFAULT_UID);
        if(uid < 10)
            harderror(L_BADUID); /* "maradns_uid is less than 10 or not a number.\nThis uid must have a value of 10 or more" */
        if(setuid(uid) != 0)
            sys_harderror(L_NODROP); /* "Could not drop root uid" */
        /* Workaround for known Linux kernel security problem circa
           early 2000 */
        if(setuid(0) == 0)
            sys_harderror(L_STILL_ROOT);  /* "We seem to still be root" */

        mlog(L_DROP_SUCCESS); /* "Root privileges dropped" */
#endif /* __CYGWIN__ */
#endif /* MINGW32 */

#ifndef __CYGWIN__
        }
    else {
#else
    if(1 == 2) {
#endif /* __CYGWIN__ */
#ifndef ALLOW_NON_ROOT
        harderror("Running MaraDNS 2.0 as a non-root server support disabled");
#else
        /* Bind to port 53 as a non-root user */
        bind_address = read_string_kvar("bind_address");
        ipv4_bind_address = read_string_kvar("ipv4_bind_addresses");

        if(js_length(ipv4_bind_address) < 1 && js_length(bind_address) < 1) {
         harderror("The mararc variable ipv4_bind_addresses must be set.");
        }

        if(js_length(ipv4_bind_address) >= 1 && js_length(bind_address) >=1) {
       harderror("Both bind_address and ipv4_bind_addresses can not be set.");
        }

        for(counter = 0; counter < 512 ; counter++)
                bind_addresses[counter].ip = 0xffffffff;

        if(js_length(bind_address) >=1) {
            if(make_ip_acl(bind_address,bind_addresses,500,0) == JS_ERROR) {
                harderror("Looks like bind_address may have a bad value");
            }
        } else {
          if(make_ip_acl(ipv4_bind_address,bind_addresses,500,0) == JS_ERROR) {
              harderror("Looks like ipv4_bind_addresses may have a bad value");
            }
        }

        if(udp_ipv4_bind(sock,bind_addresses) == JS_ERROR)
                sys_harderror(L_BEROOT); /* "Problem binding to port 53.\nYou should run this as root" */

        mlog(L_BIND_SUCCESS);  /* "Socket opened on UDP port 53" */
#endif /* ALLOW_NON_ROOT */
        }

    csv2_tilde_handling = read_numeric_kvar("csv2_tilde_handling",2);

    /* Make sure that if csv2_tilde_handling is set, it has a value 0-3 */
    if(csv2_tilde_handling < 0 || csv2_tilde_handling > 3) {
        harderror("csv2_tilde_handling "
                  "must have a value between 0 and 3");
        exit(1);
        }

    recurse_delegation = read_numeric_kvar("recurse_delegation",0);

    /* Make sure that if recurse_delegation is set, it has a value 0-1 */
    if(recurse_delegation < 0 || recurse_delegation > 1) {
        harderror("recurse_delegation "
                  "must have a value between 0 and 1");
        exit(1);
        }

#ifndef AUTHONLY
    /* Set the upstream port */
    set_upstream_port(read_numeric_kvar("upstream_port",53));
#endif

    /* Create the big hash */
    bighash = 0;
    bighash = mhash_create(8);
    if(bighash == 0)
        harderror(L_NOBIGHASH); /* "Could not create big hash" */

    /* populate_main uses qual timestamps for the csv2 zone files */
    qual_set_time();
    value = populate_main(bighash,errors,recursion_enabled);
    if(dns_records_served > 0) {
        printf("MaraDNS proudly serves you %d DNS records\n",
               dns_records_served);
        }

    /* Limit the amount of memory we can use */
#ifdef MAX_MEM
    /* Limit the maximum amount of memory we can allocate, in
     * bytes */
    maxprocs = read_numeric_kvar("max_mem",
               1048576 + thread_overhead +
               ((cache_size + dns_records_served) * 1536));
    if(maxprocs < 262144 && maxprocs > 0) { maxprocs = 262144; }
    if(maxprocs > 0) {
      rlim.rlim_cur = rlim.rlim_max = maxprocs;
      if(setrlimit(MAX_MEM,&rlim) != 0) {
        if(errno == ENOSYS) {
            printf(
      "WARNING: Your system does not allow setting memory allocation limits!");
            }
        else {
#ifdef __CYGWIN__
            printf(
      "WARNING: Your system does not allow setting memory allocation limits!");
#else /* __CYGWIN__ */
            harderror("Unable to set memory allocation limits");
#endif
            }
       } else {
            printf("MaraDNS maximum memory allocation set to %d bytes\n",
                   maxprocs);
            }
     }
#else /* MAX_MEM */
     printf(
      "WARNING: Your system does not allow setting memory allocation limits!");
#endif /* MAX_MEM */

    /* If we have one or more elements in the cache, we will need to look
     * through the cache for elements (default_dos_level, in this context,
     * allows us to save time when doing just recursive queries by not
     * bothering with cache lookups) */
    if(value == JS_SUCCESS) {
        default_dos_level = 0;
    }

    if(value == JS_ERROR)
        harderror(L_NOPOPULATE); /* "Error running populate_main program" */
    else if(value == -2) {
        js_show_stdout(errors);
        printf("%s",L_N); /* "\n" */
        harderror(L_POPULATE_FATAL); /* "This error in populate hash is fatal" */
        }

    if(verbstr != 0) { js_destroy(verbstr); verbstr = 0; }
    verbstr = read_string_kvar("csv2_default_zonefile");
    if(verbstr !=0 && js_length(verbstr) > 0) {
            js_string *zone;
            int q;
            if(recursion_enabled == 1) {
                    harderror("Default zonefile not permitted when recursion is enabled");
            }
            if((zone = js_create(12,1)) == 0) {
                    harderror("Unable to create zone js_string");
            }
            if(js_qstr2js(zone,"*") == JS_ERROR) {
                    harderror("Unable to create zone string");
            }
            q = csv2_parse_zone_bighash(zone,verbstr,bighash,1);
            if(q < 0) {
                   printf("Error parsing csv2 default zonefile %d\n",q);
            }
            printf("Csv2 default zonefile parsed\n");
            default_zonefile_enabled = 1;
            default_dos_level = 0;
    }

    /* Set the dos_protection_level to see if we disable some features
     * to protect us from a denial of service attack. */
    dos_protection_level =
        read_numeric_kvar("dos_protection_level",default_dos_level);

    mlog(L_RRS_LOADED);  /* "All RRs have been loaded" */

    /* Right now, all we do after getting a HUP signal is exit with a code
       of 8.  The static DNS database is too tangley for me to figure out
       how to clear all of the memory it uses quickly; there is too much
       chance of having a memory leak with this, so I don't feel comfortable
       doing the right thing after getting a HUP signal until Franky comes
       back to help make sure HUP handling is memory-leak free */
#ifndef MINGW32
    signal(SIGHUP,handle_hup); /* All this does is change the got_hup_signal
                                  global variable */
#ifdef DEBUG
    signal(SIGTERM,display_unfreed);
    signal(SIGINT,display_unfreed);
#endif /* DEBUG */
#endif /* MINGW32 */

    /* Initialize the new decompression code */
    /* Disabled until I can get the bugs worked out */
    decomp_init(log_level);

    /* Flush out any messages that have already appeared */
    fflush(stdout);

    if(log_level >= 3)
        mlog(L_DATAWAIT); /* "Awaiting data on port 53" */
    /* Listen for data on the UDP socket */
    for(;;) {
        int sock_num;
        conn ect; /* The space is not a typo */
        ect.type = 0;
        ect.d = (void *)0;
        ect.addrlen = 0;
        /* Make sure we never got a HUP signal */
        if(got_hup_signal != 0) {
            printf("HUP signal sent to MaraDNS process\n");
            printf("Exiting with return value of 8\n");
            exit(8);
            }
        /* Update the timestamp; this needs to be run once a second */
        qual_set_time();
        if(log_level >= 50) /* This happens once a second */
            mlog(L_DATAWAIT); /* "Awaiting data on port 53" */
        sock_num = getudp(sock,bind_addresses,&ect,incoming,512,
                          have_ipv6_address);
        if(sock_num == JS_ERROR)
            continue;
        if(log_level >= 3)
            mlog(L_GOTDATA);     /* "Message received, processing" */
        if(decompress_data(incoming,uncomp) == JS_ERROR) {
            if(log_level >= 4) {
                if(ect.type == 4) {
                    clin = (struct sockaddr_in *)(ect.d);
                }
#ifdef IPV6
/* Cygwin doesn't have ipv6 support yet */
#ifndef __CYGWIN__
                else {
                    clin6 = (struct sockaddr_in6 *)(ect.d);
                }
#endif /* __CYGWIN__ */
#endif
                log_lock();
                show_timestamp();
                printf("%s ","Query from");
                if(ect.type == 4) {
                    debug_show_ip(ntohl(clin->sin_addr.s_addr));
                } else {
#ifdef IPV6
/* Cygwin doesn't have ipv6 support yet */
#ifndef __CYGWIN__
                    debug_show_socket_ipv6(clin6);
#endif /* __CYGWIN__ */
#else
                    printf(" UNKNOWN ");
#endif
                }
                printf("has decompression error: ");
                show_esc_stdout(incoming);
                printf("\n");
                log_unlock();
                }
            if(ect.d != 0) {
                js_dealloc(ect.d);
                }
            continue;
            }
        if(log_level >= 5) {
            log_lock();
            show_timestamp();
            printf("Decompressed packet: ");
            show_esc_stdout(uncomp);
            printf("\n");
            log_unlock();
            }
        if(log_level >= 3 && uncomp->unit_count > 12) {
            /* Show them the query */
            counter = dlabel_length(uncomp,12);
            value = js_readuint16(uncomp,12+counter);
            if(js_substr(uncomp,incoming,12,counter) != JS_ERROR) {
                clin = (struct sockaddr_in *)(ect.d);
#ifdef IPV6
/* Cygwin doesn't have ipv6 support yet */
#ifndef __CYGWIN__
                clin6 = (struct sockaddr_in6 *)(ect.d);
#endif /* __CYGWIN__ */
#endif
                hname_translate(incoming,value);
                /* Yes, I know, put these in the "to localize" header file */
                log_lock();
                show_timestamp();
                printf("%s: ","Query from");
                if(ect.type == 4) {
                    debug_show_ip(ntohl(clin->sin_addr.s_addr));
                } else {
#ifdef IPV6
/* Cygwin doesn't have ipv6 support yet */
#ifndef __CYGWIN__
                    debug_show_socket_ipv6(clin6);
#endif /* __CYGWIN__ */
#else
                    printf(" UNKNOWN ");
#endif
                }
                printf(" ");
                js_show_stdout(incoming);
                printf("\n");
                log_unlock();
                }
            }
        /* Delay the processing the request, as needed */
#ifndef MINGW32
        if(debug_delay > 0)
            sleep(debug_delay);
#endif
        /* Process the query */
        proc_query(uncomp,&ect,sock_num);
        /* Free the memory used by the ect structure */
        if(ect.d != 0) {
                js_dealloc(ect.d);
            }
        }

    /* We should never end up here */

    exit(7); /* Exit code 7: Broke out of loop somehow */

    }