A
download s_conf.c
Language: C
License: GPL
Copyright: (C) 2002 by the past and present ircd coders, and others.
LOC: 1602
Project Info
NeoStats
Server: SourceForge
Type: cvs
...stats\neostats\NeoIRCd\src\
   .cvsignore
   .depend
   adns.c
   balloc.c
   channel.c
   channel_mode.c
   class.c
   client.c
   cloak.c
   crypt.c
   descrip.mms
   dynlink.c
   event.c
   fdlist.c
   fileio.c
   getopt.c
   hash.c
   hook.c
   hostmask.c
   irc_string.c
   ircd.c
   ircd_parser.y
   ircd_signal.c
   ircdauth.c
   kdparse.c
   linebuf.c
   list.c
   listener.c
   m_error.c
   Makefile.in
   match.c
   md5.c
   memory.c
   messages.tab
   modules.c
   motd.c
   numeric.c
   packet.c
   parse.c
   restart.c
   resv.c
   rsa.c
   s_auth.c
   s_bsd.c
   s_bsd_devpoll.c
   s_bsd_kqueue.c
   s_bsd_poll.c
   s_bsd_select.c
   s_bsd_sigio.c
   s_conf.c
   s_debug.c
   s_gline.c
   s_log.c
   s_misc.c
   s_serv.c
   s_stats.c
   s_user.c
   scache.c
   send.c
   snprintf.c
   sprintf_irc.c
   ssl.c
   tools.c
   version.c.SH
   whowas.c

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
/*
 *  NeoIRCd: NeoStats Group. Based on Hybird7
 *  s_conf.c: Configuration file functions.
 *
 *  Copyright (C) 2002 by the past and present ircd coders, and others.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *  USA
 *
 *  $Id: s_conf.c,v 1.13 2003/03/06 14:01:51 fishwaldo Exp $
 */

#include "stdinc.h"
#include "ircd_defs.h"
#include "tools.h"
#include "s_conf.h"
#include "s_serv.h"
#include "resv.h"
#include "s_stats.h"
#include "channel.h"
#include "class.h"
#include "client.h"
#include "common.h"
#include "event.h"
#include "hash.h"
#include "irc_string.h"
#include "ircd.h"
#include "list.h"
#include "listener.h"
#include "hostmask.h"
#include "modules.h"
#include "numeric.h"
#include "fdlist.h"
#include "s_bsd.h"
#include "s_log.h"
#include "send.h"
#include "s_gline.h"
#include "s_debug.h"
#include "fileio.h"
#include "memory.h"

struct config_server_hide ConfigServerHide;

extern int yyparse(); /* defined in y.tab.c */
extern int lineno;
extern char linebuf[];
extern char conffilebuf[IRCD_BUFSIZE];
int scount = 0;       /* used by yyparse(), etc */

#ifndef INADDR_NONE
#define INADDR_NONE ((unsigned int) 0xffffffff)
#endif


/* internally defined functions */

static void lookup_confhost(struct ConfItem* aconf);
static int  SplitUserHost(struct ConfItem *aconf);

static void     set_default_conf(void);
static void     validate_conf(void);
static void     read_conf(FBFILE*);
static void     clear_out_old_conf(void);
static void     flush_deleted_I_P(void);
static void     expire_tklines(dlink_list *);
static int 	is_attached(struct Client *client_p, struct ConfItem *aconf);

FBFILE* conf_fbfile_in;
extern char yytext[];

/* address of class 0 conf */
static struct   Class* class0;

static  int     verify_access(struct Client *client_p, const char *username);
static  int     attach_iline(struct Client *, struct ConfItem *);

static void clear_special_conf(struct ConfItem **);

/* usually, with hash tables, you use a prime number...
 * but in this case I am dealing with ip addresses, not ascii strings.
 */

#define IP_HASH_SIZE 0x1000

struct ip_entry
{
#ifndef IPV6
  u_int32_t ip;
#else
  struct irc_inaddr ip;
#endif
  int        count;
  time_t last_attempt;
  struct ip_entry *next;
};

static struct ip_entry *ip_hash_table[IP_HASH_SIZE];
static BlockHeap *ip_entry_heap = NULL;
static int ip_entries_count = 0;
static int hash_ip(struct irc_inaddr *);
static void garbage_collect_ip_entries(void);
static struct ip_entry *find_or_add_ip(struct irc_inaddr*);

/* general conf items link list root */
struct ConfItem* ConfigItemList = NULL;

/* conf xline link list root */
struct ConfItem        *x_conf = ((struct ConfItem *)NULL);

/*
 * conf_dns_callback
 * inputs	- pointer to struct ConfItem
 *		- pointer to adns reply
 * output	- none
 * side effects	- called when resolver query finishes
 * if the query resulted in a successful search, hp will contain
 * a non-null pointer, otherwise hp will be null.
 * if successful save hp in the conf item it was called with
 */
static void 
conf_dns_callback(void* vptr, adns_answer *reply)
{
  struct ConfItem *aconf = (struct ConfItem *) vptr;

  if (reply && reply->status == adns_s_ok)
  {
#ifdef IPV6
    copy_s_addr(IN_ADDR(aconf->ipnum),
                reply->rrs.addr->addr.inet6.sin6_addr.s6_addr);
#else
    copy_s_addr(IN_ADDR(aconf->ipnum),
                reply->rrs.addr->addr.inet.sin_addr.s_addr);
#endif
    MyFree(reply);
  }

  MyFree(aconf->dns_query);
  aconf->dns_query = NULL;
}

/*
 * conf_dns_lookup - do a nameserver lookup of the conf host
 * if the conf entry is currently doing a ns lookup do nothing, otherwise
 * allocate a dns_query and start ns lookup.
 */
static void
conf_dns_lookup(struct ConfItem* aconf)
{
  if (aconf->dns_query == NULL)
    {
      aconf->dns_query = MyMalloc(sizeof(struct DNSQuery));
      aconf->dns_query->ptr = aconf;
      aconf->dns_query->callback = conf_dns_callback;
      adns_gethost(aconf->host, aconf->aftype, aconf->dns_query);
    }
}

/*
 * make_conf
 *
 * inputs	- none
 * output	- pointer to new conf entry
 * side effects	- none
 */
struct ConfItem* 
make_conf()
{
  struct ConfItem* aconf;

  aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
  aconf->status       = CONF_ILLEGAL;
  aconf->aftype       = AF_INET;
  return(aconf);
}

/*
 * free_conf
 *
 * inputs	- pointer to conf to free
 * output	- none
 * side effects	- crucial password fields are zeroed, conf is freed
 */
void 
free_conf(struct ConfItem* aconf)
{
  assert(aconf != NULL);
  if(aconf == NULL)
    return;
  assert(!(aconf->status & CONF_CLIENT) ||
         (aconf->host && strcmp(aconf->host, "NOMATCH")) ||
         (aconf->clients == -1));
  delete_adns_queries(aconf->dns_query);
  MyFree(aconf->host);
  if (aconf->passwd)
    memset(aconf->passwd, 0, strlen(aconf->passwd));
  MyFree(aconf->passwd);
  if (aconf->spasswd)
    memset(aconf->spasswd, 0, strlen(aconf->spasswd));
  MyFree(aconf->spasswd);
  MyFree(aconf->name);
  MyFree(aconf->className);
  MyFree(aconf->user);
  MyFree(aconf->fakename);
#ifdef HAVE_LIBCRYPTO
  if (aconf->rsa_public_key)        { RSA_free(aconf->rsa_public_key); }
  if (aconf->rsa_public_key_file)   { MyFree(aconf->rsa_public_key_file); }
#endif
  MyFree((char*) aconf);
}

/*
 * remove all conf entries from the client except those which match
 * the status field mask.
 */
void
det_confs_butmask(struct Client *client_p, int mask)
{
  dlink_node *dlink;
  dlink_node *link_next;
  struct ConfItem *aconf;

  DLINK_FOREACH_SAFE(dlink, link_next, client_p->localClient->confs.head)
  {
    aconf = dlink->data;

    if ((aconf->status & mask) == 0)
    {
      detach_conf(client_p, aconf);
    }
  }
}

static struct LinkReport {
  int conf_type;
  int rpl_stats;
  int conf_char;
} report_array[] = {
  { CONF_SERVER,           RPL_STATSCLINE, 'C'},
  { CONF_LEAF,             RPL_STATSLLINE, 'L'},
  { CONF_OPERATOR,         RPL_STATSOLINE, 'O'},
  { CONF_HUB,              RPL_STATSHLINE, 'H'},
  { 0, 0, '\0' }
};

/*
 * report_configured_links
 *
 * inputs	- pointer to client to report to
 *		- type of line to report
 * output	- NONE
 * side effects	-
 */
void 
report_configured_links(struct Client* source_p, int mask)
{
  struct ConfItem*   tmp;
  struct LinkReport* p;
  char*              host;
  char*              pass;
  char*              user;
  char*              name;
  char*		     classname;
  int                port;

  for (tmp = ConfigItemList; tmp; tmp = tmp->next)
  {
    if (tmp->status & mask)
      {
        for (p = &report_array[0]; p->conf_type; p++)
          if (p->conf_type == tmp->status)
            break;
	    
        if(p->conf_type == 0)
	  return;

        get_printable_conf(tmp, &name, &host, &pass, &user, &port,&classname);

        if(mask & CONF_SERVER)
          {
            char c;
	    char buf[20];
	    char *s = buf;
	    
	    buf[0] = '\0';
            c = p->conf_char;
	    
	    if (tmp->flags & CONF_FLAGS_ALLOW_AUTO_CONN)
	      *s++ = 'A';
	    if (tmp->flags & CONF_FLAGS_CRYPTLINK)
	      *s++ = 'C';
	    if (tmp->flags & CONF_FLAGS_LAZY_LINK)
	      *s++ = 'L';
	    if (tmp->flags & CONF_FLAGS_COMPRESSED)
	      *s++ = 'Z';
	    if (tmp->flags & CONF_FLAGS_ULINED)
	      *s++ = 'U';
	    if (tmp->fakename)
	      *s++ = 'M';
	    
	    if (!buf[0])
              *s++ = '*';
	      
	    *s++ = '\0';

              sendto_one(source_p, form_str(p->rpl_stats), me.name,
                         source_p->name, c,
                         host,
			 buf,
                         name,
                         port,
                         classname);

          }
        else if(mask & (CONF_OPERATOR))
          {
            /* Don't allow non opers to see oper privs */
            if(IsOper(source_p))
              sendto_one(source_p, form_str(p->rpl_stats), me.name,
                         source_p->name,
                         p->conf_char,
                         user, host, name,
                         oper_privs_as_string((struct Client *)NULL,port),
                         classname,
                         oper_flags_as_string((int)tmp->hold));
            else
              sendto_one(source_p, form_str(p->rpl_stats), me.name,
                         source_p->name, p->conf_char,
                         user, host, name,
                         "0",
                         classname,
                         "");
          }
        else
          sendto_one(source_p, form_str(p->rpl_stats), me.name,
                     source_p->name, p->conf_char,
                     host, name, port,
                     classname);
      }
  }
}

/*
 * report_specials - report special conf entries
 *
 * inputs       - struct Client pointer to client to report to
 *              - int flags type of special struct ConfItem to report
 *              - int numeric for struct ConfItem to report
 * output       - none
 * side effects -
 */
void 
report_specials(struct Client* source_p, int flags, int numeric)
{
  struct ConfItem* this_conf;
  struct ConfItem* aconf;
  char*            name;
  char*            host;
  char*            pass;
  char*            user;
  char*       classname;
  int              port;

  if (flags & CONF_XLINE)
    this_conf = x_conf;
  else return;

  for (aconf = this_conf; aconf; aconf = aconf->next)
    if (aconf->status & flags)
      {
        get_printable_conf(aconf, &name, &host, &pass,
                           &user, &port, &classname);

        sendto_one(source_p, form_str(numeric),
                   me.name,
                   source_p->name,
                   name,
                   pass);
      }
}

/*
 * check_client
 *
 * inputs	- pointer to client
 * output	- 0 = Success
 * 		  NOT_AUTHORIZED (-1) = Access denied (no I line match)
 * 		  SOCKET_ERROR   (-2) = Bad socket.
 * 		  I_LINE_FULL    (-3) = I-line is full
 *		  TOO_MANY       (-4) = Too many connections from hostname
 * 		  BANNED_CLIENT  (-5) = K-lined
 * side effects - Ordinary client access check.
 *		  Look for conf lines which have the same
 * 		  status as the flags passed.
 */
int 
check_client(struct Client *client_p, struct Client *source_p, char *username)
{
  int i;
 
  ClearAccess(source_p);

  /* I'm already in big trouble if source_p->localClient is NULL -db */
  if ((i = verify_access(source_p, username)))
  {
    ilog(L_INFO, "Access denied: %s[%s]", 
	 source_p->name, source_p->localClient->sockhost);
  }

  switch( i )
    {
    case SOCKET_ERROR:
      exit_client(client_p, source_p, &me, "Socket Error");
      break;

    case TOO_MANY:
      sendto_realops_flags(FLAGS_FULL|FLAGS_REMOTE, L_ALL, 
                           "Too many on IP for %s (%s).",
			   get_client_name(source_p, SHOW_IP),
			   source_p->localClient->sockhost);
			   
      ilog(L_INFO,"Too many connections on IP from %s.",
	   get_client_name(source_p, SHOW_IP));
      
      ServerStats->is_ref++;
      (void)exit_client(client_p, source_p, &me, 
			"No more connections allowed on that IP" );
      break;

    case I_LINE_FULL:
      sendto_realops_flags(FLAGS_FULL|FLAGS_REMOTE, L_ALL,
                           "I-line is full for %s (%s).",
			   get_client_name(source_p, SHOW_IP),
			   source_p->localClient->sockhost);
			   
      ilog(L_INFO,"Too many connections from %s.",
	   get_client_name(source_p, SHOW_IP));
      
      ServerStats->is_ref++;
      (void)exit_client(client_p, source_p, &me, 
		"No more connections allowed in your connection class" );
      break;

    case NOT_AUTHORIZED:
    {
      static char ipaddr[HOSTIPLEN];
      ServerStats->is_ref++;
      /* jdc - lists server name & port connections are on */
      /*       a purely cosmetical change */
      inetntop(source_p->localClient->aftype, &IN_ADDR(source_p->localClient->ip), ipaddr, HOSTIPLEN);
      sendto_realops_flags(FLAGS_UNAUTH|FLAGS_REMOTE, L_ALL,
			   "Unauthorised client connection from %s [%s] on [%s/%u].",
			   get_client_name(source_p, SHOW_IP),
			   ipaddr,
			   source_p->localClient->listener->name,
			   source_p->localClient->listener->port);
			   
      ilog(L_INFO,
	  "Unauthorized client connection from %s on [%s/%u].",
	  get_client_name(source_p, SHOW_IP),
	  source_p->localClient->listener->name,
	  source_p->localClient->listener->port);
	  
      exit_client(client_p, source_p, &me,
		  "You are not authorised to use this server");
      break;
    }
    case BANNED_CLIENT:
      exit_client(client_p,client_p, &me, "*** Banned ");
      ServerStats->is_ref++;
      break;

    case 0:
    default:
      break;
    }
  return(i);
}

/*
 * verify_access
 *
 * inputs	- pointer to client to verify
 *		- pointer to proposed username
 * output	- 0 if success -'ve if not
 * side effect	- find the first (best) I line to attach.
 */
static int 
verify_access(struct Client* client_p, const char* username)
{
  struct ConfItem* aconf;
  struct ConfItem* gkill_conf;
  char non_ident[USERLEN + 1];

  if (IsGotId(client_p))
    {
      aconf = find_address_conf(client_p->host,client_p->username,
				&client_p->localClient->ip,
				client_p->localClient->aftype);
    }
  else
    {
      strlcpy(non_ident, "~", sizeof(non_ident));
      strlcat(non_ident, username, USERLEN + 1);
      aconf = find_address_conf(client_p->host,non_ident,
				&client_p->localClient->ip,
				client_p->localClient->aftype);
    }

  if (aconf != NULL)
    {
      if (aconf->status & CONF_CLIENT)
	{
	  if (aconf->flags & CONF_FLAGS_REDIR)
	    {
	      sendto_one(client_p, form_str(RPL_REDIR), me.name, client_p->name,
			 aconf->name ? aconf->name : "", aconf->port);
	      return(NOT_AUTHORIZED);
	    }
	  if (ConfigFileEntry.glines)
	    {
	      if (!IsConfExemptKline(aconf) && !IsConfExemptGline(aconf))
		{
		  if (IsGotId(client_p))
		    gkill_conf = find_gkill(client_p, client_p->username);
		  else
		    gkill_conf = find_gkill(client_p, non_ident);

		  if (gkill_conf)
		    {
		      sendto_one(client_p, ":%s NOTICE %s :*** G-lined", me.name,
				 client_p->name);
		      sendto_one(client_p, ":%s NOTICE %s :*** Banned %s",
				 me.name, client_p->name, 
				 gkill_conf->passwd);
		      return(BANNED_CLIENT);
		    }
		}
	    }
	  if (IsConfDoIdentd(aconf))
	    SetNeedId(client_p);
	  if (IsConfRestricted(aconf))
	    SetRestricted(client_p);

	  return(attach_iline(client_p, aconf));
	}
      else if (aconf->status & CONF_KILL)
	{
	  if (ConfigFileEntry.kline_with_reason)
	    {
	      sendto_one(client_p, ":%s NOTICE %s :*** Banned %s",
			 me.name,client_p->name,aconf->passwd);
	    }
	  return(BANNED_CLIENT);
	}
    }
 return(NOT_AUTHORIZED);
}

/*
 * attach_iline
 *
 * inputs	- client pointer
 *		- conf pointer
 * output	-
 * side effects	- do actual attach
 */
static int
attach_iline(struct Client *client_p, struct ConfItem *aconf)
{
  struct ip_entry *ip_found;
  ip_found = find_or_add_ip(&client_p->localClient->ip);

  SetIpHash(client_p);
  ip_found->count++;

  /* only check it if its non zero */
  if (aconf->c_class /* This should never non NULL *grin* */ &&
      ConfConFreq(aconf) && ip_found->count > ConfConFreq(aconf))
    {
      if(!IsConfExemptLimits(aconf))
        return(TOO_MANY); /* Already at maximum allowed ip#'s */
      else
        {
          sendto_one(client_p,
       ":%s NOTICE %s :*** :I: line is full, but you have an >I: line!",
                     me.name,client_p->name);
        }
    }

  return(attach_conf(client_p, aconf));
}

/*
 * init_ip_hash_table()
 *
 * input                - NONE
 * output               - NONE
 * side effects         - allocate memory for ip_entry(s)
 *			- clear the ip hash table
 */

void 
init_ip_hash_table()
{
  ip_entry_heap = BlockHeapCreate(sizeof(struct ip_entry), 2*MAXCONNECTIONS);
  memset((void *)ip_hash_table, 0, sizeof(ip_hash_table));
}

/* 
 * find_or_add_ip()
 *
 * inputs       - client_p
 *              - name
 *
 * output       - pointer to a struct ip_entry
 * side effects -
 *
 * If the ip # was not found, a new struct ip_entry is created, and the ip
 * count set to 0.
 */

static struct ip_entry *
find_or_add_ip(struct irc_inaddr *ip_in)
{
  struct ip_entry *ptr, *newptr;
  int hash_index=hash_ip(ip_in);

  for(ptr = ip_hash_table[hash_index]; ptr; ptr = ptr->next)
  {
    if(memcmp(&ptr->ip, ip_in, sizeof(struct irc_inaddr)) == 0)
    {
      /* Found entry already in hash, return it. */
      return(ptr);
    }
  }

  if (ip_entries_count >= (2*MAXCONNECTIONS))
    garbage_collect_ip_entries();

  newptr = BlockHeapAlloc(ip_entry_heap);
  ip_entries_count++;
  memcpy(&newptr->ip, ip_in, sizeof(struct irc_inaddr));
  newptr->count = 0;
  newptr->last_attempt = 0;

  if ((ptr = ip_hash_table[hash_index]) != NULL)
    newptr->next = ptr;
  else
    newptr->next = NULL;

  ip_hash_table[hash_index] = newptr;
  return(newptr);
}

/* 
 * remove_one_ip
 *
 * inputs        - unsigned long IP address value
 * output        - NONE
 * side effects  - The ip address given, is looked up in ip hash table
 *                 and number of ip#'s for that ip decremented.
 *                 If ip # count reaches 0 and has expired,
 *		   the struct ip_entry is returned to the ip_entry_heap
 */

void 
remove_one_ip(struct irc_inaddr *ip_in)
{
  struct ip_entry *ptr;
  struct ip_entry *last_ptr = NULL;
  int hash_index = hash_ip(ip_in);

  for(ptr = ip_hash_table[hash_index]; ptr; ptr = ptr->next)
  {
#ifndef IPV6
    if (ptr->ip != PIN_ADDR(ip_in))
      continue;
#else
    if (memcmp(&IN_ADDR(ptr->ip), &PIN_ADDR(ip_in),
	       sizeof(struct irc_inaddr)))
      continue;
#endif
    if (ptr->count > 0)
      ptr->count--;
    if (ptr->count == 0 &&
	(CurrentTime-ptr->last_attempt) >= ConfigFileEntry.throttle_time)
    {
      if (last_ptr != NULL)
	last_ptr->next = ptr->next;
      else
	ip_hash_table[hash_index] = ptr->next;

      BlockHeapFree(ip_entry_heap, ptr);
      ip_entries_count--;
      return;
    }
    last_ptr = ptr;
  }
}

/*
 * hash_ip()
 * 
 * input        - pointer to an irc_inaddr
 * output       - integer value used as index into hash table
 * side effects - hopefully, none
 */

#ifndef IPV6
static int  
hash_ip(struct irc_inaddr *addr)
{
  int hash;
  u_int32_t ip;

  ip = ntohl(PIN_ADDR(addr));
  hash = ((ip >> 12) + ip) & (IP_HASH_SIZE-1);
  return(hash);
}
#else /* IPV6 */
static int
hash_ip(struct irc_inaddr *addr)
{
  int hash;
  u_int32_t *ip = (u_int32_t *)&PIN_ADDR(addr);

  if(IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ip))
  {
     hash = ((ip[3] >> 12) + ip[3]) & (IP_HASH_SIZE-1);
     return(hash);
  } 
    
  hash = ip[0] ^ ip[3];
  hash ^= hash >> 16;  
  hash ^= hash >> 8;   
  hash = hash & (IP_HASH_SIZE - 1);
  return(hash);
}
#endif /* IPV6 */


/*
 * count_ip_hash
 *
 * inputs        - pointer to counter of number of ips hashed 
 *               - pointer to memory used for ip hash
 * output        - returned via pointers input
 * side effects  - NONE
 *
 * number of hashed ip #'s is counted up, plus the amount of memory
 * used in the hash.
 */

void 
count_ip_hash(int *number_ips_stored,u_long *mem_ips_stored)
{
  struct ip_entry *ptr;
  int i;

  *number_ips_stored = 0;
  *mem_ips_stored = 0;

  for (i = 0; i < IP_HASH_SIZE ;i++)
  {
    for (ptr = ip_hash_table[i]; ptr; ptr = ptr->next)
    {
      *number_ips_stored += 1;
      *mem_ips_stored += sizeof(struct ip_entry);
    }
  }
}

/*
 * garbage_collect_ip_entries()
 *
 * input	- NONE
 * output	- NONE
 * side effects	- free up all ip entries with no connections
 */
static void
garbage_collect_ip_entries(void)
{
  struct ip_entry *ptr;
  struct ip_entry *last_ptr;
  struct ip_entry *next_ptr;
  int i;

  for(i = 0; i < IP_HASH_SIZE; i++)
  {
    last_ptr = NULL;
    for(ptr = ip_hash_table[i]; ptr; ptr = next_ptr)
    {
      next_ptr = ptr->next;

      if (ptr->count == 0)
      {
	if (last_ptr != NULL)
	  last_ptr->next = ptr->next;
	else
	  ip_hash_table[i] = ptr->next;
	BlockHeapFree(ip_entry_heap, ptr);
	ip_entries_count--;
      }
      else
	last_ptr = ptr;
    }
  }
}

/*
 * iphash_stats()
 *
 * input	- 
 * output	-
 * side effects	-
 */
void 
iphash_stats(struct Client *client_p, struct Client *source_p,
	     int parc, char *parv[],FBFILE* out)
{
  struct ip_entry *ptr;
  int i;
  int collision_count;
  char result_buf[256];

  if(out == NULL)
    sendto_one(source_p,":%s NOTICE %s :*** hash stats for iphash",
               me.name,client_p->name);
  else
  {
    (void)sprintf(result_buf,"*** hash stats for iphash\n");
    (void)fbputs(result_buf,out);
  }

  for(i = 0; i < IP_HASH_SIZE ;i++)
  {
    collision_count = 0;
    for (ptr = ip_hash_table[i]; ptr; ptr = ptr->next)
      collision_count++;

    if(collision_count != 0)
    {
      if(out == NULL)
      {
	sendto_one(source_p,":%s NOTICE %s :Entry %d (0x%X) Collisions %d",
		   me.name,client_p->name,i,i,collision_count);
      }
      else
      {
	(void)sprintf(result_buf,"Entry %d (0x%X) Collisions %d\n",
		      i,i,collision_count);
	(void)fbputs(result_buf,out);
      }
    }
  }
}

/*
 * detach_conf
 *
 * inputs	- pointer to client to detach
 * 		- pointer to conf item to detach
 * output	- 0 for success, -1 for failure
 * side effects	- Disassociate configuration from the client.
 *		  Also removes a class from the list if marked for deleting.
 */
int 
detach_conf(struct Client* client_p,struct ConfItem* aconf)
{
  dlink_node *ptr;

  if(aconf == NULL)
    return(-1);

  DLINK_FOREACH(ptr, client_p->localClient->confs.head)
  {
    if (ptr->data == aconf)
    {
      if ((aconf) && (ClassPtr(aconf)))
      {
	if (aconf->status & CONF_CLIENT_MASK)
	{
	  if (ConfLinks(aconf) > 0)
	    --ConfLinks(aconf);
	}
	if (ConfMaxLinks(aconf) == -1 && ConfLinks(aconf) == 0)
	{
	  free_class(ClassPtr(aconf));
	  ClassPtr(aconf) = NULL;
	}
      }
      if (aconf && !--aconf->clients && IsIllegal(aconf))
	free_conf(aconf);
      dlinkDelete(ptr, &client_p->localClient->confs);
      free_dlink_node(ptr);
      return(0);
    }
  }
  return(-1);
}

/*
 * is_attached
 *
 * inputs	- pointer to client to check
 * 		- pointer to conf item to check
 * output	- 1 if attached, 0 if not
 * side effects	- 
 */
static int 
is_attached(struct Client *client_p, struct ConfItem *aconf)
{
  dlink_node *ptr=NULL;

  DLINK_FOREACH(ptr, client_p->localClient->confs.head)
  {
    if (ptr->data == aconf)
      break;
  }
  return((ptr != NULL) ? 1 : 0);
}

/*
 * attach_conf
 * 
 * inputs	- client pointer
 * 		- conf pointer
 * output	-
 * side effects - Associate a specific configuration entry to a *local*
 *                client (this is the one which used in accepting the
 *                connection). Note, that this automatically changes the
 *                attachment if there was an old one...
 */
int 
attach_conf(struct Client *client_p,struct ConfItem *aconf)
{
  dlink_node *lp;

  if (is_attached(client_p, aconf))
  {
    return(1);
  }
  if (IsIllegal(aconf))
  {
    return(NOT_AUTHORIZED);
  }

  if ((aconf->status & CONF_OPERATOR) == 0)
  {
    if ((aconf->status & CONF_CLIENT) &&
	ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
    {
      if (!IsConfExemptLimits(aconf))
      {
	return(I_LINE_FULL); 
      }
      else
      {
	send(client_p->localClient->fd,
	     "NOTICE FLINE :I: line is full, but you have an >I: line!\n",
	     56, 0);
	SetExemptLimits(client_p);
      }
    }
  }

  if(aconf->status & FLAGS2_RESTRICTED)
    SetRestricted(client_p);

  lp = make_dlink_node();

  dlinkAdd(aconf, lp, &client_p->localClient->confs);

  aconf->clients++;
  if (aconf->status & CONF_CLIENT_MASK)
    ConfLinks(aconf)++;
  return(0);
}

/*
 * attach_confs - Attach all possible CONF lines to a client
 * if the name passed matches that for the conf file (for non-C/N lines) 
 * or is an exact match (C/N lines only).  The difference in behaviour 
 * is to stop C:*::* and N:*::*.
 * returns count of conf entries attached if successful, 0 if none are found
 *
 * NOTE: this will allow C:::* and N:::* because the match mask is the
 * conf line and not the name
 */
int 
attach_confs(struct Client* client_p, const char* name, int statmask)
{
  struct ConfItem* tmp;
  int conf_counter = 0;
  
  for (tmp = ConfigItemList; tmp; tmp = tmp->next)
    {
      if ((tmp->status & statmask) && !IsIllegal(tmp) &&
          tmp->name && match(tmp->name, name))
        {
          if (-1 < attach_conf(client_p, tmp))
            ++conf_counter;
        }
      else if ((tmp->status & statmask) && !IsIllegal(tmp) &&
               tmp->name && !irccmp(tmp->name, name))
        {
          if (-1 < attach_conf(client_p, tmp))
            ++conf_counter;
        }
    }
  return(conf_counter);
}

/*
 * attach_connect_block
 *
 * inputs	- pointer to server to attach
 * 		- name of server
 *		- hostname of server
 * output	- true (1) if both are found, otherwise return false (0)
 * side effects - find connect block and attach them to connecting client
 */
int 
attach_connect_block(struct Client *client_p,
		     const char* name,
		     const char* host)
{
  struct ConfItem* ptr;

  assert(client_p != NULL);
  assert(host != NULL);
  if(client_p == NULL || host == NULL)
    return(0);

  for (ptr = ConfigItemList; ptr; ptr = ptr->next)
    {
     if (IsIllegal(ptr))
       continue;
     if (ptr->status != CONF_SERVER)
       continue;
     if ((match(name, ptr->name) == 0) || (match(ptr->host, host) == 0))
       continue;
     attach_conf(client_p, ptr);
     return(-1);
    }
  return(0);
}

/*
 * find_conf_exact
 *
 * inputs	- pointer to name to find
 *		- pointer to username to find
 *		- pointer to host to find
 *		- int mask of type of conf to find
 * output	- NULL or pointer to conf found
 * side effects	- find a conf entry which matches the hostname
 *		  and has the same name.
 */
struct ConfItem* 
find_conf_exact(const char* name, const char* user, 
		const char* host, int statmask)
{
  struct ConfItem *tmp;

  for (tmp = ConfigItemList; tmp; tmp = tmp->next)
    {
      if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
          irccmp(tmp->name, name))
        continue;
      /*
      ** Accept if the *real* hostname (usually sockethost)
      ** socket host) matches *either* host or name field
      ** of the configuration.
      */
      if (!match(tmp->host, host) || !match(tmp->user,user)
          || irccmp(tmp->name, name) )
        continue;
      if (tmp->status & CONF_OPERATOR)
        {
          if (tmp->clients < ConfMaxLinks(tmp))
            return(tmp);
          else
            continue;
        }
      else
        return(tmp);
    }
  return(NULL);
}

/*
 * find_conf_name
 *
 * inputs	- pointer to conf link list to search
 *		- pointer to name to find
 *		- int mask of type of conf to find
 * output	- NULL or pointer to conf found
 * side effects	- find a conf entry which matches the name
 *		  and has the given mask.
 */
struct ConfItem* 
find_conf_name(dlink_list *list, const char* name, int statmask)
{
  dlink_node *ptr;
  struct ConfItem* aconf;
  
  DLINK_FOREACH(ptr, list->head)
  {
    aconf = ptr->data;
    if ((aconf->status & statmask) && aconf->name && 
	(!irccmp(aconf->name, name) || match(aconf->name, name)))
      return(aconf);
  }
  return(NULL);
}


/*
 * find_conf_by_name
 *
 * inputs	- pointer to name to match on
 *		- int mask of type of conf to find
 * output	- NULL or pointer to conf found
 * side effects	- find a conf entry which matches the name
 *		  and has the given mask.
 *
 */
struct ConfItem* 
find_conf_by_name(const char* name, int status)
{
  struct ConfItem* conf;
  assert(name != NULL);
  if(name == NULL)
    return NULL;
  for (conf = ConfigItemList; conf; conf = conf->next)
    {
      if (conf->status == status && conf->name &&
          match(name, conf->name))
        return conf;
    }
  return NULL;
}

/*
 * find_conf_by_host
 *
 * inputs	- pointer to hostname to match on
 *		- int mask of type of conf to find
 * output	- NULL or pointer to conf found
 * side effects	- find a conf entry which matches the host
 *		  and has the given mask.
 *
 */
struct ConfItem* 
find_conf_by_host(const char* host, int status)
{
  struct ConfItem* conf;
  assert(host != NULL);
  if(host == NULL)
    return(NULL);
  for (conf = ConfigItemList; conf; conf = conf->next)
    {
      if (conf->status == status && conf->host &&
          match(host, conf->host))
        return(conf);
    }
  return(NULL);
}

/*
 * find_x_conf
 *
 * inputs       - pointer to char string to find
 * output       - NULL or pointer to found struct ConfItem
 * side effects - looks for a match on name field
 */
struct ConfItem *
find_x_conf(char *to_find)
{
  struct ConfItem *aconf;

  for (aconf = x_conf; aconf; aconf = aconf->next)
    {
      if (BadPtr(aconf->name))
          continue;

      if(match_esc(aconf->name,to_find))
        return(aconf);

    }
  return(NULL);
}

/*
 * clear_special_conf
 * 
 * inputs       - pointer to pointer of root of special conf link list
 * output       - none
 * side effects - clears given special conf lines
 */
static void 
clear_special_conf(struct ConfItem **this_conf)
{
  struct ConfItem *aconf;
  struct ConfItem *next_aconf;

  for (aconf = *this_conf; aconf; aconf = next_aconf)
    {
      next_aconf = aconf->next;
      free_conf(aconf);
    }
  *this_conf = (struct ConfItem *)NULL;
  return;
}

/*
 * rehash
 *
 * Actual REHASH service routine. Called with sig == 0 if it has been called
 * as a result of an operator issuing this command, else assume it has been
 * called as a result of the server receiving a HUP signal.
 */
int
rehash(int sig)
{
  if (sig != 0)
    {
      sendto_realops_flags(FLAGS_ALL, L_ALL,
			   "Got signal SIGHUP, reloading ircd conf. file");
    }

  restart_resolver();
  /* don't close listeners until we know we can go ahead with the rehash */
  read_conf_files(NO);

  if (ServerInfo.description != NULL)
    {
      strlcpy(me.info, ServerInfo.description, sizeof(me.info));
    }

  flush_deleted_I_P();
  check_klines();
  reopen_log(logFileName);
  return(0);
}

/*
 * set_default_conf()
 *
 * inputs	- NONE
 * output	- NONE
 * side effects	- Set default values here.
 *		  This is called **PRIOR** to parsing the
 *		  configuration file.  If you want to do some validation
 *		  of values later, put them in validate_conf().
 */

#define YES     1
#define NO      0
#define UNSET  -1

static void 
set_default_conf(void)
{
  class0 = find_class("default");       /* which one is the default class ? */

#ifdef HAVE_LIBCRYPTO
  ServerInfo.rsa_private_key = NULL;
  ServerInfo.rsa_private_key_file = NULL;
#endif

  /* ServerInfo.name is not rehashable */
  /* ServerInfo.name = ServerInfo.name; */
  ServerInfo.description = NULL;
  DupString(ServerInfo.network_name, NETWORK_NAME_DEFAULT);
  DupString(ServerInfo.network_desc, NETWORK_DESC_DEFAULT);

  memset(&ServerInfo.ip, 0, sizeof(ServerInfo.ip));
  ServerInfo.specific_ipv4_vhost = 0;
  memset(&ServerInfo.ip6, 0, sizeof(ServerInfo.ip6));
  ServerInfo.specific_ipv6_vhost = 0;

  ServerInfo.max_clients = MAX_CLIENTS;  /* XXX - these don't seem to */
  ServerInfo.max_buffer = MAX_BUFFER;    /*       actually do anything! */
  /* Don't reset hub, as that will break lazylinks */
  /* ServerInfo.hub = NO; */
  ServerInfo.dns_host.sin_addr.s_addr = 0;
  ServerInfo.dns_host.sin_port = 0;
  AdminInfo.name = NULL;
  AdminInfo.email = NULL;
  AdminInfo.description = NULL;

  set_log_level(L_NOTICE);
  
  ConfigFileEntry.failed_oper_notice = YES;
  ConfigFileEntry.anti_nick_flood = NO;
  ConfigFileEntry.max_nick_time = 20;
  ConfigFileEntry.max_nick_changes = 5;
  ConfigFileEntry.max_accept = 20;
  ConfigFileEntry.anti_spam_exit_message_time = 0;
  ConfigFileEntry.ts_warn_delta = TS_WARN_DELTA_DEFAULT;
  ConfigFileEntry.ts_max_delta = TS_MAX_DELTA_DEFAULT;
  ConfigFileEntry.kline_with_reason = YES;
  ConfigFileEntry.client_exit = YES;
  ConfigFileEntry.kline_with_connection_closed = NO;
  ConfigFileEntry.warn_no_nline = YES;
  ConfigFileEntry.non_redundant_klines = YES;
  ConfigFileEntry.stats_o_oper_only = NO;
  ConfigFileEntry.stats_k_oper_only = 1; /* masked */
  ConfigFileEntry.stats_i_oper_only = 1; /* masked */
  ConfigFileEntry.stats_P_oper_only = NO;
  ConfigFileEntry.pace_wait = 10;
  ConfigFileEntry.caller_id_wait = 60;
  ConfigFileEntry.pace_wait_simple = 1;
  ConfigFileEntry.short_motd = NO;
  ConfigFileEntry.no_oper_flood = NO;
  ConfigFileEntry.true_no_oper_flood = NO;
  ConfigFileEntry.fname_userlog[0] = '\0';
  ConfigFileEntry.fname_foperlog[0] = '\0';
  ConfigFileEntry.fname_operlog[0] = '\0';
  ConfigFileEntry.glines = NO;
  ConfigFileEntry.use_egd = NO;
  /* don't reset msglocale setting -- we'd overwrite then env string */
  ConfigFileEntry.gline_time = 12 * 3600;
  ConfigFileEntry.idletime = 0;
  ConfigFileEntry.dots_in_ident = 0;
  ConfigFileEntry.maximum_links = MAXIMUM_LINKS_DEFAULT;
  ConfigFileEntry.max_targets = MAX_TARGETS_DEFAULT;
  DupString(ConfigFileEntry.servlink_path, SLPATH);
  strlcpy(ConfigFileEntry.operautojoin, "", CHANNELLEN);
  ConfigFileEntry.egdpool_path = NULL;
  
#ifdef HAVE_LIBCRYPTO
  /* jdc -- This is our default value for a cipher.  According to the
   *        CRYPTLINK document (doc/cryptlink.txt), BF/128 must be supported
   *        under all circumstances if cryptlinks are enabled.  So,
   *        this will be our default.
   *
   *        NOTE: I apologise for the hard-coded value of "1" (BF/128).
   *              This should be moved into a find_cipher() routine.
   */
  ConfigFileEntry.default_cipher_preference = &CipherTable[1];
#endif
#ifdef HAVE_LIBZ
  ConfigFileEntry.compression_level = 0;
#endif

  ConfigFileEntry.oper_umodes = FLAGS_LOCOPS | FLAGS_SERVNOTICE |
    FLAGS_OPERWALL | FLAGS_WALLOP;
  ConfigFileEntry.oper_only_umodes = FLAGS_DEBUG;
  ConfigFileEntry.throttle_time = 10;


  ConfigChannel.knock_delay = 300;
  ConfigChannel.knock_delay_channel = 60;
  ConfigChannel.max_chans_per_user = 15;
  ConfigChannel.max_bans = 25;

  /* 60 * 30 = 1800 = 30 minutes */
  ConfigChannel.persist_time = 1800;

  ConfigChannel.default_split_user_count = 0;
  ConfigChannel.default_split_server_count = 0;
  ConfigChannel.no_join_on_split = NO;
  ConfigChannel.no_create_on_split = NO;
  ConfigChannel.oper_pass_resv = YES;

  ConfigServerHide.flatten_links = 0;
  ConfigServerHide.hide_servers = 0;
  ConfigServerHide.disable_remote = 0;
  ConfigServerHide.links_delay = 300;
  ConfigServerHide.hidden = 0;
  ConfigServerHide.disable_hidden = 0;

  ConfigFileEntry.min_nonwildcard = 4;
  ConfigFileEntry.default_floodcount = 8;
  ConfigFileEntry.client_flood = CLIENT_FLOOD_DEFAULT;

#ifdef IPV6  
  ConfigFileEntry.fallback_to_ip6_int = 1;
#endif

#ifdef EFNET
  ConfigFileEntry.use_help = 0;
#else
  ConfigFileEntry.use_help = 1;
#endif
}
#undef YES
#undef NO

/*
 * read_conf() 
 *
 *
 * inputs       - file descriptor pointing to config file to use
 * output       - None
 * side effects	- Read configuration file.
 */
static void 
read_conf(FBFILE* file)
{
  scount = lineno = 0;

  set_default_conf(); /* Set default values prior to conf parsing */
  yyparse();          /* Load the values from the conf */
  validate_conf();    /* Check to make sure some values are still okay. */
                      /* Some global values are also loaded here. */
  check_class();      /* Make sure classes are valid */
}

static void 
validate_conf(void)
{
  if(ConfigFileEntry.ts_warn_delta < TS_WARN_DELTA_MIN)
    ConfigFileEntry.ts_warn_delta = TS_WARN_DELTA_DEFAULT;

  if(ConfigFileEntry.ts_max_delta < TS_MAX_DELTA_MIN)
    ConfigFileEntry.ts_max_delta = TS_MAX_DELTA_DEFAULT;

  if(ConfigFileEntry.servlink_path == NULL)
    DupString(ConfigFileEntry.servlink_path, SLPATH);

  if(ServerInfo.network_name == NULL)
    DupString(ServerInfo.network_name,NETWORK_NAME_DEFAULT);

  if(ServerInfo.network_desc == NULL)
    DupString(ServerInfo.network_desc,NETWORK_DESC_DEFAULT);

  if ((ConfigFileEntry.client_flood < CLIENT_FLOOD_MIN) ||
      (ConfigFileEntry.client_flood > CLIENT_FLOOD_MAX))
     ConfigFileEntry.client_flood = CLIENT_FLOOD_MAX;

  GlobalSetOptions.idletime = (ConfigFileEntry.idletime * 60);
}

/*
 * conf_add_conf
 * Inputs	- ConfItem
 * Output	- none
 * Side effects	- add given conf to link list
 */
void 
conf_add_conf(struct ConfItem *aconf)
{
  (void)collapse(aconf->host);
  (void)collapse(aconf->user);
  Debug((DEBUG_NOTICE,
	 "Read Init: (%d) (%s) (%s) (%s) (%d) (%d)",
	 aconf->status, 
	 aconf->host ? aconf->host : "<NULL>",
	 aconf->passwd ? aconf->passwd : "<NULL>",
	 aconf->user ? aconf->user : "<NULL>",
	 aconf->port,
	 aconf->c_class ? ConfClassType(aconf): 0 ));

  aconf->next = ConfigItemList;
  ConfigItemList = aconf;
}

/*
 * SplitUserHost
 *
 * inputs	- struct ConfItem pointer
 * output	- return 1/0 true false or -1 for error
 * side effects - splits user@host found in a name field of conf given
 *		  stuff the user into ->user and the host into ->host
 */
static int 
SplitUserHost(struct ConfItem *aconf)
{
  char *p;
  char *new_user;
  char *new_host;

  if ( (p = strchr(aconf->host, '@')) )
    {
      *p = '\0';
      DupString(new_user, aconf->host);
      MyFree(aconf->user);
      aconf->user = new_user;
      p++;
      DupString(new_host,p);
      MyFree(aconf->host);
      aconf->host = new_host;
    }
  else
    {
      DupString(aconf->user, "*");
    }
  return(1);
}

/*
 * lookup_confhost - start DNS lookups of all hostnames in the conf
 * line and convert an IP addresses in a.b.c.d number for to IP#s.
 *
 */
static void 
lookup_confhost(struct ConfItem* aconf)
{
  if (BadPtr(aconf->host) || BadPtr(aconf->name))
    {
      ilog(L_ERROR, "Host/server name error: (%s) (%s)",
          aconf->host, aconf->name);
      return;
    }

  if (strchr(aconf->host, '*') || strchr(aconf->host, '?'))
    return;
  /*
  ** Do name lookup now on hostnames given and store the
  ** ip numbers in conf structure.
  */
  if (inetpton(DEF_FAM, aconf->host, &IN_ADDR(aconf->ipnum)) <= 0)
    {
      conf_dns_lookup(aconf);
    }
}

/*
 * conf_connect_allowed
 *
 * inputs	- pointer to inaddr
 *		- int type ipv4 or ipv6
 * output	- BANNED or accepted
 * side effects	- none
 */
int 
conf_connect_allowed(struct irc_inaddr *addr, int aftype)
{
  struct ip_entry *ip_found;
  struct ConfItem *aconf = find_dline_conf(addr,aftype);
 
  /* DLINE exempt also gets you out of static limits/pacing... */
  if (aconf && (aconf->status & CONF_EXEMPTDLINE))
    return(0);
 
  if (aconf != NULL)
    return(BANNED_CLIENT);

  ip_found = find_or_add_ip(addr);
  if ((CurrentTime - ip_found->last_attempt) <
      ConfigFileEntry.throttle_time)
  {
    ip_found->last_attempt = CurrentTime;
    return(TOO_FAST);
  }
  ip_found->last_attempt = CurrentTime;
  return(0);
}

/*
 * find_kill
 *
 * inputs	- pointer to client structure
 * output	- pointer to struct ConfItem if found
 * side effects	- See if this user is klined already,
 *		  and if so, return struct ConfItem pointer
 */
struct ConfItem *
find_kill(struct Client* client_p)
{
  struct ConfItem *aconf;
  assert(client_p != NULL);
  if(client_p == NULL)
    return NULL;

  aconf = find_kline_conf(client_p->host, client_p->username,
			  &client_p->localClient->ip,
			  client_p->localClient->aftype);
  if (aconf == NULL)
    return aconf;
  if(aconf->status & CONF_KILL)
    return aconf;
  return(NULL);
}

/* add_temp_kline
 *
 * inputs        - pointer to struct ConfItem
 * output        - none
 * Side effects  - links in given struct ConfItem into 
 *                 temporary kline link list
 */
void
add_temp_kline(struct ConfItem *aconf)
{
  dlink_node *kill_node;
  kill_node = make_dlink_node();
  dlinkAdd(aconf, kill_node, &temporary_klines);
  aconf->flags |= CONF_FLAGS_TEMPORARY;
  add_conf_by_address(aconf->host, CONF_KILL, aconf->user, aconf);
}

/*
 * cleanup_tklines
 *
 * inputs       - NONE
 * output       - NONE
 * side effects - call function to expire tklines
 *                This is an event started off in ircd.c
 */
void
cleanup_tklines(void *notused)
{
  expire_tklines(&temporary_klines);
}

/*
 * expire_tklines
 *
 * inputs       - tkline list pointer
 * output       - NONE
 * side effects - expire tklines
 */
static void
expire_tklines(dlink_list *tklist)
{
  dlink_node *kill_node;
  dlink_node *next_node;
  struct ConfItem *kill_ptr;
  DLINK_FOREACH_SAFE(kill_node, next_node, tklist->head)
    {
      kill_ptr = kill_node->data;

      if (kill_ptr->hold <= CurrentTime)
	{
          /* Alert opers that a TKline expired - Hwy */
          sendto_realops_flags(FLAGS_ALL, L_ALL,
			       "Temporary K-line for [%s@%s] expired",
			       (kill_ptr->user) ? kill_ptr->user : "*",
			       (kill_ptr->host) ? kill_ptr->host : "*");

	  delete_one_address_conf(kill_ptr->host, kill_ptr);
	  dlinkDelete(kill_node, tklist);
	  free_dlink_node(kill_node);
	}
    }
}

/*
 * oper_privs_as_string
 *
 * inputs        - pointer to client_p or NULL
 * output        - pointer to static string showing oper privs
 * side effects  -
 * return as string, the oper privs as derived from port
 * also, set the oper privs if given client_p non NULL
 */

char *
oper_privs_as_string(struct Client *client_p,int port)
{
  static char privs_out[16];
  char *privs_ptr;

  privs_ptr = privs_out;
  *privs_ptr = '\0';

  if (port & CONF_OPER_GLINE)
    {
      if(client_p)
        SetOperGline(client_p);
      *privs_ptr++ = 'G';
    }
  else
    *privs_ptr++ = 'g';

  if(port & CONF_OPER_K)
    {
      if(client_p)
        SetOperK(client_p);
      *privs_ptr++ = 'K';
    }
  else
    *privs_ptr++ = 'k';

  if(port & CONF_OPER_N)
    {
      if(client_p)
        SetOperN(client_p);
      *privs_ptr++ = 'N';
    }
  else
    *privs_ptr++ = 'n';

  if(port & CONF_OPER_GLOBAL_KILL)
    {
      if(client_p)
        SetOperGlobalKill(client_p);
      *privs_ptr++ = 'O';
    }
  else
    *privs_ptr++ = 'o';

  if(port & CONF_OPER_REMOTE)
    {
      if(client_p)
        SetOperRemote(client_p);
      *privs_ptr++ = 'R';
    }
  else
    *privs_ptr++ = 'r';
  
  if(port & CONF_OPER_UNKLINE)
    {
      if(client_p)
        SetOperUnkline(client_p);
      *privs_ptr++ = 'U';
    }
  else
    *privs_ptr++ = 'u';

  if(port & CONF_OPER_REHASH)
    {
      if(client_p)
        SetOperRehash(client_p);
      *privs_ptr++ = 'H';
    }
  else
    *privs_ptr++ = 'h';

  if(port & CONF_OPER_DIE)
    {
      if(client_p)
        SetOperDie(client_p);
      *privs_ptr++ = 'D';
    }
  else
    *privs_ptr++ = 'd';

  if (port & CONF_OPER_ADMIN)
    {
      if (client_p)
	SetOperAdmin(client_p);
      *privs_ptr++ = 'A';
    }
  else
    *privs_ptr++ = 'a';
  
  *privs_ptr = '\0';

  return(privs_out);
}


/* oper_flags_as_string
 *
 * inputs        - oper flags as bit mask
 * output        - oper flags as as string
 * side effects -
 *
 */
char *
oper_flags_as_string(int flags)
{
  /* This MUST be extended if we add any more modes... -Hwy */
  static char flags_out[18];
  char *flags_ptr;

  flags_ptr = flags_out;
  *flags_ptr = '\0';

  if(flags & FLAGS_INVISIBLE)
    *flags_ptr++ = 'i';
  if(flags & FLAGS_WALLOP)
    *flags_ptr++ = 'w';
  if(flags & FLAGS_SERVNOTICE)
    *flags_ptr++ = 's';
  if(flags & FLAGS_CCONN)
    *flags_ptr++ = 'c';
  if(flags & FLAGS_REJ)
    *flags_ptr++ = 'r';
  if(flags & FLAGS_SKILL)
    *flags_ptr++ = 'k';
  if(flags & FLAGS_FULL)
    *flags_ptr++ = 'f';
  if(flags & FLAGS_SPY)
    *flags_ptr++ = 'y';
  if(flags & FLAGS_DEBUG)
    *flags_ptr++ = 'd';
  if(flags & FLAGS_NCHANGE)
    *flags_ptr++ = 'n';
  if(flags & FLAGS_ADMIN)
    *flags_ptr++ = 'a';
  if(flags & FLAGS_EXTERNAL)
    *flags_ptr++ = 'x';
  if(flags & FLAGS_UNAUTH)
    *flags_ptr++ = 'u';
  if(flags & FLAGS_BOTS)
    *flags_ptr++ = 'b';
  if(flags & FLAGS_LOCOPS)
    *flags_ptr++ = 'l';
  if(flags & FLAGS_CALLERID)
    *flags_ptr++ = 'g';
  *flags_ptr = '\0';

  return(flags_out);
}


/* const char* get_oper_name(struct Client *client_p)
 * Input: A client to find the active oper{} name for.
 * Output: The nick!user@host{oper} of the oper.
 *         "oper" is server name for remote opers
 * Side effects: None.
 */
char*
get_oper_name(struct Client *client_p)
{
  dlink_node *cnode;

  /* +5 for !,@,{,} and null */
  static char buffer[NICKLEN+USERLEN+HOSTLEN+HOSTLEN+5];

  if (MyConnect(client_p))
  {
    DLINK_FOREACH(cnode, client_p->localClient->confs.head)
    {
      if (((struct ConfItem*)cnode->data)->status & CONF_OPERATOR)
      {
	ircsprintf(buffer, "%s!%s@%s{%s}", client_p->name,
		   client_p->username, client_p->host,
		   ((struct ConfItem*)cnode->data)->name);
	return(buffer);
      }
    }
    /* Probably should assert here for now. If there is an oper out there 
     * with no oper{} conf attached, it would be good for us to know...
     */
    assert(0); /* Oper without oper conf! */
  }
  ircsprintf(buffer, "%s!%s@%s{%s}", client_p->name,
	     client_p->username, client_p->host, client_p->servptr->name);
  return(buffer);
}

/*
 * get_printable_conf
 *
 * inputs        - struct ConfItem
 *
 * output         - name 
 *                - host
 *                - pass
 *                - user
 *                - port
 *
 * side effects        -
 * Examine the struct struct ConfItem, setting the values
 * of name, host, pass, user to values either
 * in aconf, or "<NULL>" port is set to aconf->port in all cases.
 */

void 
get_printable_conf(struct ConfItem *aconf, char **name, char **host,
                           char **pass, char **user,int *port,char **classname)
{
  static  char        null[] = "<NULL>";
  static  char        zero[] = "default";

  *name = BadPtr(aconf->name) ? null : aconf->name;
  *host = BadPtr(aconf->host) ? null : aconf->host;
  *pass = BadPtr(aconf->passwd) ? null : aconf->passwd;
  *user = BadPtr(aconf->user) ? null : aconf->user;
  *classname = BadPtr(aconf->className) ? zero : aconf->className;
  *port = (int)aconf->port;
}

/*
 * read_conf_files
 *
 * inputs       - cold start YES or NO
 * output       - none
 * side effects - read all conf files needed, ircd.conf kline.conf etc.
 */
void 
read_conf_files(int cold)
{
  FBFILE *file;
  const char *filename, *kfilename, *dfilename; /* kline or conf filename */

  conf_fbfile_in = NULL;

  filename = get_conf_name(CONF_TYPE);

  /* We need to know the initial filename for the yyerror() to report
     FIXME: The full path is in conffilenamebuf first time since we
             dont know anything else

     - Gozem 2002-07-21 
  */
  strlcpy(conffilebuf, filename, sizeof(conffilebuf));

  if ((conf_fbfile_in = fbopen(filename,"r")) == NULL)
    {
      if(cold)
        {
          ilog(L_CRIT, "Failed in reading configuration file %s", filename);
          exit(-1);
        }
      else
        {
          sendto_realops_flags(FLAGS_ALL, L_ALL,
			       "Can't open file '%s' - aborting rehash!",
			       filename );
          return;
        }
    }

  if (cold)
  {
    /* set to 'undefined' */
  }
  else
  {
    clear_out_old_conf();
  }

  read_conf(conf_fbfile_in);
  fbclose(conf_fbfile_in);

  kfilename = get_conf_name(KLINE_TYPE);
  if (irccmp(filename, kfilename))
    {
      if((file = fbopen(kfilename,"r")) == NULL)
        {
	  if (cold)
	    ilog(L_ERROR, "Failed reading kline file %s", filename);
	  else
	    sendto_realops_flags(FLAGS_ALL, L_ALL,
				 "Can't open %s file klines could be missing!",
				 kfilename);
	}
      else
	{
	  parse_k_file(file);
	  fbclose(file);
	}
    }

  dfilename = get_conf_name(DLINE_TYPE);
  if (irccmp(filename, dfilename) && irccmp(kfilename, dfilename))
    {
      if ((file = fbopen(dfilename,"r")) == NULL)
	{
	  if(cold)
	    ilog(L_ERROR, "Failed reading dline file %s", dfilename);
	  else
	    sendto_realops_flags(FLAGS_ALL, L_ALL,
				 "Can't open %s file dlines could be missing!",
				 dfilename);
	}
      else
	{
	  parse_d_file(file);
	  fbclose(file);
	}
    }
}

/*
 * clear_out_old_conf
 *
 * inputs       - none
 * output       - none
 * side effects - Clear out the old configuration
 */
static 
void clear_out_old_conf(void)
{
  struct ConfItem **tmp = &ConfigItemList;
  struct ConfItem *tmp2;
  struct Class    *cltmp;

  /*
   * We only need to free anything allocated by yyparse() here.
   * Reseting structs, etc, is taken care of by set_default_conf().
   */
  while ((tmp2 = *tmp))
  {
    if (tmp2->clients)
    {
      /*
       ** Configuration entry is still in use by some
       ** local clients, cannot delete it--mark it so
       ** that it will be deleted when the last client
       ** exits...
       */
      if (!(tmp2->status & CONF_CLIENT))
      {
        *tmp = tmp2->next;
        tmp2->next = NULL;
      }
      else
        tmp = &tmp2->next;
      tmp2->status |= CONF_ILLEGAL;
    }
    else
    {
      *tmp = tmp2->next;
      free_conf(tmp2);
    }
  }

  /*
   * don't delete the class table, rather mark all entries
   * for deletion. The table is cleaned up by check_class. - avalon
   */
  assert(ClassList != NULL);
  
  for (cltmp = ClassList->next; cltmp; cltmp = cltmp->next)
    MaxLinks(cltmp) = -1;

  clear_out_address_conf();
  clear_special_conf(&x_conf);

  /* clean out module paths */
#ifndef STATIC_MODULES
  mod_clear_paths();
#endif

  /* clean out ServerInfo */
  MyFree(ServerInfo.description);
  ServerInfo.description = NULL;
  MyFree(ServerInfo.network_name);
  ServerInfo.network_name = NULL;
  MyFree(ServerInfo.network_desc);
  ServerInfo.network_desc = NULL;
#ifdef HAVE_LIBCRYPTO
  if (ServerInfo.rsa_private_key != NULL)
  {
    RSA_free(ServerInfo.rsa_private_key);
    ServerInfo.rsa_private_key = NULL;
  }
  if (ServerInfo.rsa_private_key_file != NULL)
  {
    MyFree(ServerInfo.rsa_private_key_file);
    ServerInfo.rsa_private_key_file = NULL;
  }
#endif

  /* clean out old resvs from the conf */
  clear_conf_resv();

  /* clean out AdminInfo */
  MyFree(AdminInfo.name);
  AdminInfo.name = NULL;
  MyFree(AdminInfo.email);
  AdminInfo.email = NULL;
  MyFree(AdminInfo.description);
  AdminInfo.description = NULL;

  /* operator{} and class{} blocks are freed above */
  /* clean out listeners */
  close_listeners();

  /* auth{}, quarantine{}, shared{}, connect{}, kill{}, deny{}, exempt{}
   * and gecos{} blocks are freed above too
   */

  /* clean out general */
  MyFree(ConfigFileEntry.servlink_path);
  ConfigFileEntry.servlink_path = NULL;
#ifdef HAVE_LIBCRYPTO
  ConfigFileEntry.default_cipher_preference = NULL;
#endif /* HAVE_LIBCRYPTO */

  /* OK, that should be everything... */
}

/*
 * flush_deleted_I_P
 *
 * inputs       - none
 * output       - none
 * side effects - This function removes I/P conf items
 */

static void
flush_deleted_I_P(void)
{
  struct ConfItem **tmp = &ConfigItemList;
  struct ConfItem *tmp2;

  /*
   * flush out deleted I and P lines although still in use.
   */
  for (tmp = &ConfigItemList; (tmp2 = *tmp); )
    {
      if (!(tmp2->status & CONF_ILLEGAL))
        tmp = &tmp2->next;
      else
        {
          *tmp = tmp2->next;
          tmp2->next = NULL;
          if (!tmp2->clients)
            free_conf(tmp2);
        }
    }
}

/*
 * WriteKlineOrDline
 *
 * inputs       - kline or dline type flag
 *              - client pointer to report to
 *              - user name of target
 *              - host name of target
 *              - reason for target
 *              - time_t cur_time
 * output       - NONE
 * side effects - This function takes care of
 *                finding right kline or dline conf file, writing
 *                the right lines to this file, 
 *                notifying the oper that their kline/dline is in place
 *                notifying the opers on the server about the k/d line
 *                forwarding the kline onto the next U lined server
 *                
 */
void 
WriteKlineOrDline( KlineType type,
		   struct Client *source_p,
		   char *user,
		   char *host,
		   const char *reason,
		   const char *oper_reason,
		   const char *current_date,
		   time_t cur_time)
{
  char buffer[1024];
  FBFILE *out;
  const char *filename;         /* filename to use for kline */

  filename = get_conf_name(type);

  if (type == DLINE_TYPE)
    {
      sendto_realops_flags(FLAGS_ALL, L_ALL,
			   "%s added D-Line for [%s] [%s]",
			   get_oper_name(source_p), host, reason);
      sendto_one(source_p, ":%s NOTICE %s :Added D-Line [%s] to %s",
		 me.name, source_p->name, host, filename);

    }
  else
    {
      sendto_realops_flags(FLAGS_ALL, L_ALL,
			   "%s added K-Line for [%s@%s] [%s]",
			   get_oper_name(source_p), user, host, reason);
      sendto_one(source_p, ":%s NOTICE %s :Added K-Line [%s@%s]",
		 me.name, source_p->name, user, host);
    }

  if ((out = fbopen(filename, "a")) == NULL)
    {
      sendto_realops_flags(FLAGS_ALL, L_ALL,
			   "*** Problem opening %s ", filename);
      return;
    }

  if (oper_reason == NULL)
    oper_reason = "";

  if(type==KLINE_TYPE)
    ircsprintf(buffer, "\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%ld\n",
               user,
	       host,
               reason,
	       oper_reason,
	       current_date,
	       get_oper_name(source_p),
               (long) cur_time);
  else
    ircsprintf(buffer, "\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%ld\n",
               host,
               reason,
	       oper_reason,
	       current_date,
	       get_oper_name(source_p),
               (long) cur_time);


  if (fbputs(buffer,out) == -1)
    {
      sendto_realops_flags(FLAGS_ALL, L_ALL,
			   "*** Problem writing to %s",filename);
      fbclose(out);
      return;
    }
      
  fbclose(out);

  if(type==KLINE_TYPE)
    ilog(L_TRACE, "%s added K-Line for [%s@%s] [%s]",
        source_p->name, user, host, reason);
  else
    ilog(L_TRACE, "%s added D-Line for [%s] [%s]",
           get_oper_name(source_p), host, reason);
}

/* get_conf_name
 *
 * inputs       - type of conf file to return name of file for
 * output       - pointer to filename for type of conf
 * side effects - none
 */
const char *
get_conf_name(KlineType type)
{
  if(type == CONF_TYPE)
    {
      return(ConfigFileEntry.configfile);
    }
  else if(type == KLINE_TYPE)
    {
      return(ConfigFileEntry.klinefile);
    }

  return(ConfigFileEntry.dlinefile);
}

/*
 * conf_add_class_to_conf
 * inputs       - pointer to config item
 * output       - NONE
 * side effects - Add a class pointer to a conf 
 */

void 
conf_add_class_to_conf(struct ConfItem *aconf)
{
  if(aconf->className == NULL)
    {
      DupString(aconf->className,"default");
      ClassPtr(aconf) = class0;
      return;
    }

  ClassPtr(aconf) = find_class(aconf->className);

  if(ClassPtr(aconf) == class0)
    {
      sendto_realops_flags(FLAGS_ALL, L_ALL,
	   "Warning *** Defaulting to default class for missing class \"%s\"",
			   aconf->className);
      MyFree(aconf->className);
      DupString(aconf->className,"default");
      return;
    }

  if (ConfMaxLinks(aconf) < 0)
    {
      ClassPtr(aconf) = find_class(0);
      MyFree(aconf->className);
      DupString(aconf->className,"default");
      return;
    }
}

#define MAXCONFLINKS 150

/*
 * conf_add_server
 *
 * inputs       - pointer to config item
 *		- pointer to link count already on this conf
 * output       - NONE
 * side effects - Add a connect block
 */
int 
conf_add_server(struct ConfItem *aconf, int lcount)
{
  conf_add_class_to_conf(aconf);

  if (lcount > MAXCONFLINKS || !aconf->host || !aconf->name)
    {
      sendto_realops_flags(FLAGS_ALL, L_ALL,"Bad connect block");
      ilog(L_WARN, "Bad connect block");
      return(-1);
    }

  if (BadPtr(aconf->passwd) && !(aconf->flags & CONF_FLAGS_CRYPTLINK))
    {
      sendto_realops_flags(FLAGS_ALL, L_ALL,"Bad connect block, name %s",
			   aconf->name);
      ilog(L_WARN, "Bad connect block, host %s",aconf->name);
      return(-1);
    }
          
  if (SplitUserHost(aconf) < 0)
    {
      sendto_realops_flags(FLAGS_ALL, L_ALL,"Bad connect block, name %s",
			   aconf->name);
      ilog(L_WARN, "Bad connect block, name %s",aconf->name);
      return(-1);
    }
  lookup_confhost(aconf);
  return(0);
}

/*
 * conf_add_d_conf
 * inputs       - pointer to config item
 * output       - NONE
 * side effects - Add a d/D line
 */
void
conf_add_d_conf(struct ConfItem *aconf)
{
  if (aconf->host == NULL)
    return;

  aconf->user = NULL;

  /* XXX - Should 'd' ever be in the old conf? For new conf we don't
   *       need this anyway, so I will disable it for now... -A1kmm
   */

  if (parse_netmask(aconf->host, NULL, NULL) == HM_HOST)
    {
      ilog(L_WARN,"Invalid Dline %s ignored",aconf->host);
      free_conf(aconf);
    }
  else
    {
      add_conf_by_address(aconf->host, CONF_DLINE, NULL, aconf);
    }
}

/*
 * conf_add_x_conf
 * inputs       - pointer to config item
 * output       - NONE
 * side effects - Add a X line
 */

void 
conf_add_x_conf(struct ConfItem *aconf)
{
  aconf->next = x_conf;
  x_conf = aconf;
}


/*
 * conf_add_fields
 * inputs       - pointer to config item
 *              - pointer to host_field
 *		- pointer to pass_field
 *              - pointer to user_field
 *              - pointer to port_field
 *		- pointer to class_field
 * output       - NONE
 * side effects - update host/pass/user/port fields of given aconf
 */

void 
conf_add_fields(struct ConfItem *aconf,
		char *host_field,
		char *pass_field,
		char *user_field,
		char *port_field,
		char *class_field)
{
  if (host_field != NULL)
    DupString(aconf->host, host_field);
  if (pass_field != NULL)
    DupString(aconf->passwd, pass_field);
  if (user_field != NULL)
    DupString(aconf->user, user_field);
  if (port_field != NULL)
    aconf->port = atoi(port_field);
  if (class_field != NULL)
    DupString(aconf->className, class_field);
}

/*
 * yyerror
 *
 * inputs	- message from parser
 * output	- none
 * side effects	- message to opers and log file entry is made
 */
void 
yyerror(char *msg)
{
  char newlinebuf[BUFSIZE];

  strip_tabs(newlinebuf, (const unsigned char *)linebuf, strlen(linebuf));

  sendto_realops_flags(FLAGS_ALL, L_ALL,"\"%s\", line %d: %s: %s",
		       conffilebuf, lineno + 1, msg, newlinebuf);

  ilog(L_WARN, "\"%s\", line %d: %s: %s",
		       conffilebuf, lineno + 1, msg, newlinebuf);
}

int 
conf_fbgets(char *lbuf,int max_size, FBFILE *fb)
{
  char* buff;

  if ((buff = fbgets(lbuf,max_size,fb)) == NULL)
    return(0);

  return(strlen(lbuf));
}

int 
conf_yy_fatal_error(char *msg)
{
  return(0);
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us