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
|
/************************************************************************
* Unreal Internet Relay Chat Daemon, src/ircd.c
* Copyright (C) 1990 Jarkko Oikarinen and
* University of Oulu, Computing Center
*
* 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef CLEAN_COMPILE
static char sccsid[] =
"@(#)ircd.c 2.48 3/9/94 (C) 1988 University of Oulu, \
Computing Center and Jarkko Oikarinen";
#endif
#include "config.h"
#include "struct.h"
#include "common.h"
#include "sys.h"
#include "numeric.h"
#include "msg.h"
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#ifndef _WIN32
#include <sys/file.h>
#include <pwd.h>
#include <grp.h>
#include <sys/time.h>
#else
#include <io.h>
#include <direct.h>
#endif
#ifdef HPUX
#define _KERNEL /* HPUX has the world's worst headers... */
#endif
#ifndef _WIN32
#include <sys/resource.h>
#endif
#ifdef HPUX
#undef _KERNEL
#endif
#include <errno.h>
#ifdef HAVE_PSSTRINGS
#include <sys/exec.h>
#endif
#ifdef HAVE_PSTAT
#include <sys/pstat.h>
#endif
#include "h.h"
#ifndef NO_FDLIST
#include "fdlist.h"
#endif
#ifdef STRIPBADWORDS
#include "badwords.h"
#endif
#include "version.h"
#include "proto.h"
#ifdef _WIN32
extern BOOL IsService;
#endif
#ifdef USE_LIBCURL
#include <curl/curl.h>
#endif
ID_Copyright
("(C) 1988 University of Oulu, Computing Center and Jarkko Oikarinen");
ID_Notes("2.48 3/9/94");
#ifdef __FreeBSD__
char *malloc_options = "h" MALLOC_FLAGS_EXTRA;
#endif
time_t TSoffset = 0;
#ifndef _WIN32
extern char unreallogo[];
#endif
int SVSNOOP = 0;
extern MODVAR char *buildid;
time_t timeofday = 0;
int tainted = 0;
LoopStruct loop;
MODVAR MemoryInfo StatsZ;
#ifndef _WIN32
uid_t irc_uid = 0;
gid_t irc_gid = 0;
#endif
int R_do_dns, R_fin_dns, R_fin_dnsc, R_fail_dns, R_do_id, R_fin_id, R_fail_id;
char REPORT_DO_DNS[256], REPORT_FIN_DNS[256], REPORT_FIN_DNSC[256],
REPORT_FAIL_DNS[256], REPORT_DO_ID[256], REPORT_FIN_ID[256],
REPORT_FAIL_ID[256];
extern ircstats IRCstats;
aClient me; /* That's me */
MODVAR char *me_hash;
aClient *client = &me; /* Pointer to beginning of Client list */
extern char backupbuf[8192];
#ifdef _WIN32
extern void CleanUpSegv(int sig);
extern SERVICE_STATUS_HANDLE IRCDStatusHandle;
extern SERVICE_STATUS IRCDStatus;
#endif
#ifndef NO_FDLIST
fdlist default_fdlist;
fdlist busycli_fdlist;
fdlist serv_fdlist;
fdlist oper_fdlist;
fdlist unknown_fdlist;
float currentrate;
float currentrate2; /* outgoing */
float highest_rate = 0;
float highest_rate2 = 0;
int lifesux = 0;
int LRV = LOADRECV;
TS LCF = LOADCFREQ;
int currlife = 0;
int HTMLOCK = 0;
int noisy_htm = 1;
long lastrecvK = 0;
long lastsendK = 0;
TS check_fdlists();
#endif
unsigned char conf_debuglevel = 0;
char trouble_info[1024];
#ifdef USE_LIBCURL
extern void url_init(void);
#endif
time_t highesttimeofday=0, oldtimeofday=0, lasthighwarn=0;
void save_stats(void)
{
FILE *stats = fopen("ircd.stats", "w");
if (!stats)
return;
fprintf(stats, "%i\n", IRCstats.clients);
fprintf(stats, "%i\n", IRCstats.invisible);
fprintf(stats, "%i\n", IRCstats.servers);
fprintf(stats, "%i\n", IRCstats.operators);
fprintf(stats, "%i\n", IRCstats.unknown);
fprintf(stats, "%i\n", IRCstats.me_clients);
fprintf(stats, "%i\n", IRCstats.me_servers);
fprintf(stats, "%i\n", IRCstats.me_max);
fprintf(stats, "%i\n", IRCstats.global_max);
fclose(stats);
}
void server_reboot(char *);
void restart(char *);
static void open_debugfile(), setup_signals();
extern void init_glines(void);
extern void tkl_init(void);
MODVAR TS last_garbage_collect = 0;
#ifndef _WIN32
MODVAR char **myargv;
#else
LPCSTR cmdLine;
#endif
int portnum = -1; /* Server port number, listening this */
char *configfile = CONFIGFILE; /* Server configuration file */
int debuglevel = 10; /* Server debug level */
int bootopt = 0; /* Server boot option flags */
char *debugmode = ""; /* -"- -"- -"- */
char *sbrk0; /* initial sbrk(0) */
static int dorehash = 0, dorestart = 0;
static char *dpath = DPATH;
MODVAR int booted = FALSE;
MODVAR TS nextconnect = 1; /* time for next try_connections call */
MODVAR TS nextping = 1; /* same as above for check_pings() */
MODVAR TS nextdnscheck = 0; /* next time to poll dns to force timeouts */
MODVAR TS nextexpire = 1; /* next expire run on the dns cache */
MODVAR TS lastlucheck = 0;
#ifdef UNREAL_DEBUG
#undef CHROOTDIR
#define CHROOT
#endif
MODVAR TS NOW;
#if defined(PROFIL) && !defined(_WIN32)
extern etext();
VOIDSIG s_monitor(void)
{
static int mon = 0;
#ifdef POSIX_SIGNALS
struct sigaction act;
#endif
(void)moncontrol(mon);
mon = 1 - mon;
#ifdef POSIX_SIGNALS
act.sa_handler = s_rehash;
act.sa_flags = 0;
(void)sigemptyset(&act.sa_mask);
(void)sigaddset(&act.sa_mask, SIGUSR1);
(void)sigaction(SIGUSR1, &act, NULL);
#else
(void)signal(SIGUSR1, s_monitor);
#endif
}
#endif
VOIDSIG s_die()
{
#ifdef _WIN32
int i;
aClient *cptr;
if (!IsService)
{
unload_all_modules();
for (i = LastSlot; i >= 0; i--)
if ((cptr = local[i]) && DBufLength(&cptr->sendQ) > 0)
(void)send_queued(cptr);
exit(-1);
}
else {
SERVICE_STATUS status;
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
SC_HANDLE hService = OpenService(hSCManager, "UnrealIRCd", SERVICE_STOP);
ControlService(hService, SERVICE_CONTROL_STOP, &status);
}
#else
unload_all_modules();
flush_connections(&me);
exit(-1);
#endif
}
#ifndef _WIN32
static VOIDSIG s_rehash()
#else
VOIDSIG s_rehash()
#endif
{
#ifdef POSIX_SIGNALS
struct sigaction act;
#endif
dorehash = 1;
#ifdef POSIX_SIGNALS
act.sa_handler = s_rehash;
act.sa_flags = 0;
(void)sigemptyset(&act.sa_mask);
(void)sigaddset(&act.sa_mask, SIGHUP);
(void)sigaction(SIGHUP, &act, NULL);
#else
# ifndef _WIN32
(void)signal(SIGHUP, s_rehash); /* sysV -argv */
# endif
#endif
}
void restart(char *mesg)
{
server_reboot(mesg);
}
VOIDSIG s_restart()
{
dorestart = 1;
#if 0
static int restarting = 0;
if (restarting == 0) {
/*
* Send (or attempt to) a dying scream to oper if present
*/
restarting = 1;
server_reboot("SIGINT");
}
#endif
}
#ifndef _WIN32
VOIDSIG dummy()
{
#ifndef HAVE_RELIABLE_SIGNALS
(void)signal(SIGALRM, dummy);
(void)signal(SIGPIPE, dummy);
#ifndef HPUX /* Only 9k/800 series require this, but don't know how to.. */
# ifdef SIGWINCH
(void)signal(SIGWINCH, dummy);
# endif
#endif
#else
# ifdef POSIX_SIGNALS
struct sigaction act;
act.sa_handler = dummy;
act.sa_flags = 0;
(void)sigemptyset(&act.sa_mask);
(void)sigaddset(&act.sa_mask, SIGALRM);
(void)sigaddset(&act.sa_mask, SIGPIPE);
# ifdef SIGWINCH
(void)sigaddset(&act.sa_mask, SIGWINCH);
# endif
(void)sigaction(SIGALRM, &act, (struct sigaction *)NULL);
(void)sigaction(SIGPIPE, &act, (struct sigaction *)NULL);
# ifdef SIGWINCH
(void)sigaction(SIGWINCH, &act, (struct sigaction *)NULL);
# endif
# endif
#endif
}
#endif /* _WIN32 */
void server_reboot(char *mesg)
{
int i;
#ifdef _WIN32
aClient *cptr;
#endif
sendto_realops("Aieeeee!!! Restarting server... %s", mesg);
Debug((DEBUG_NOTICE, "Restarting server... %s", mesg));
#ifndef _WIN32
flush_connections(&me);
#else
for (i = LastSlot; i >= 0; i--)
if ((cptr = local[i]) && DBufLength(&cptr->sendQ) > 0)
(void)send_queued(cptr);
#endif
/*
* ** fd 0 must be 'preserved' if either the -d or -i options have
* ** been passed to us before restarting.
*/
#ifdef HAVE_SYSLOG
(void)closelog();
#endif
#ifndef _WIN32
for (i = 3; i < MAXCONNECTIONS; i++)
(void)close(i);
if (!(bootopt & (BOOT_TTY | BOOT_DEBUG)))
(void)close(2);
(void)close(1);
(void)close(0);
(void)execv(MYNAME, myargv);
#else
close_connections();
if (!IsService)
{
CleanUp();
WinExec(cmdLine, SW_SHOWDEFAULT);
}
#endif
#ifndef _WIN32
Debug((DEBUG_FATAL, "Couldn't restart server: %s", strerror(errno)));
#else
Debug((DEBUG_FATAL, "Couldn't restart server: %s",
strerror(GetLastError())));
#endif
unload_all_modules();
#ifdef _WIN32
if (IsService)
{
SERVICE_STATUS status;
PROCESS_INFORMATION pi;
STARTUPINFO si;
char fname[MAX_PATH];
bzero(&status, sizeof(status));
bzero(&si, sizeof(si));
IRCDStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(IRCDStatusHandle, &IRCDStatus);
GetModuleFileName(GetModuleHandle(NULL), fname, MAX_PATH);
CreateProcess(fname, "restartsvc", NULL, NULL, FALSE,
0, NULL, NULL, &si, &pi);
IRCDStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(IRCDStatusHandle, &IRCDStatus);
ExitProcess(0);
}
else
#endif
exit(-1);
}
MODVAR char *areason;
EVENT(loop_event)
{
if (loop.do_garbage_collect == 1) {
garbage_collect(NULL);
}
}
EVENT(garbage_collect)
{
extern int freelinks;
extern Link *freelink;
Link p;
int ii;
if (loop.do_garbage_collect == 1)
sendto_realops("Doing garbage collection ..");
if (freelinks > HOW_MANY_FREELINKS_ALLOWED) {
ii = freelinks;
while (freelink && (freelinks > HOW_MANY_FREELINKS_ALLOWED)) {
freelinks--;
p.next = freelink;
freelink = freelink->next;
MyFree(p.next);
}
if (loop.do_garbage_collect == 1) {
loop.do_garbage_collect = 0;
sendto_realops
("Cleaned up %i garbage blocks", (ii - freelinks));
}
}
if (loop.do_garbage_collect == 1)
loop.do_garbage_collect = 0;
}
EVENT(deprecated_notice)
{
/* Send a warning to opers currently online every week after November 1, 2016 */
if (TStime() > 1477954800)
{
sendto_realops("[WARNING] UnrealIRCd 3.2.x is no longer supported after December 31, 2016. "
"See https://www.unrealircd.org/docs/UnrealIRCd_3.2.x_deprecated");
ircd_log(LOG_ERROR, "[WARNING] UnrealIRCd 3.2.x is no longer supported after December 31, 2016. "
"See https://www.unrealircd.org/docs/UnrealIRCd_3.2.x_deprecated");
}
}
/*
** try_connections
**
** Scan through configuration and try new connections.
** Returns the calendar time when the next call to this
** function should be made latest. (No harm done if this
** is called earlier or later...)
*/
static TS try_connections(TS currenttime)
{
ConfigItem_link *aconf;
ConfigItem_deny_link *deny;
aClient *cptr;
int connecting, confrq;
TS next = 0;
ConfigItem_class *cltmp;
connecting = FALSE;
Debug((DEBUG_NOTICE, "Connection check at : %s",
myctime(currenttime)));
for (aconf = conf_link; aconf; aconf = (ConfigItem_link *) aconf->next) {
/*
* Also when already connecting! (update holdtimes) --SRB
*/
if (!(aconf->options & CONNECT_AUTO) || (aconf->flag.temporary == 1))
continue;
cltmp = aconf->class;
/*
* ** Skip this entry if the use of it is still on hold until
* ** future. Otherwise handle this entry (and set it on hold
* ** until next time). Will reset only hold times, if already
* ** made one successfull connection... [this algorithm is
* ** a bit fuzzy... -- msa >;) ]
*/
if ((aconf->hold > currenttime)) {
if ((next > aconf->hold) || (next == 0))
next = aconf->hold;
continue;
}
confrq = cltmp->connfreq;
aconf->hold = currenttime + confrq;
/*
* ** Found a CONNECT config with port specified, scan clients
* ** and see if this server is already connected?
*/
cptr = find_name(aconf->servername, (aClient *)NULL);
if (!cptr && (cltmp->clients < cltmp->maxclients)) {
/*
* Check connect rules to see if we're allowed to try
*/
for (deny = conf_deny_link; deny;
deny = (ConfigItem_deny_link *) deny->next)
if (!match(deny->mask, aconf->servername)
&& crule_eval(deny->rule))
break;
if (!deny && connect_server(aconf, (aClient *)NULL,
(struct hostent *)NULL) == 0)
sendto_realops
("Connection to %s[%s] activated.",
aconf->servername, aconf->hostname);
}
if ((next > aconf->hold) || (next == 0))
next = aconf->hold;
}
Debug((DEBUG_NOTICE, "Next connection check : %s", myctime(next)));
return (next);
}
/* Now find_kill is only called when a kline-related command is used:
AKILL/RAKILL/KLINE/UNKLINE/REHASH. Very significant CPU usage decrease.
-- Barubary */
extern TS check_pings(TS currenttime)
{
aClient *cptr = NULL;
ConfigItem_ban *bconf = NULL;
char killflag = 0;
int i = 0;
char banbuf[1024];
char scratch[64];
int ping = 0;
for (i = 0; i <= LastSlot; i++) {
/*
* If something we should not touch ..
*/
if (!(cptr = local[i]) || IsMe(cptr) || IsLog(cptr))
continue;
/*
* ** Note: No need to notify opers here. It's
* ** already done when "FLAGS_DEADSOCKET" is set.
*/
if (cptr->flags & FLAGS_DEADSOCKET) {
(void)exit_client(cptr, cptr, &me, cptr->error_str ? cptr->error_str : "Dead socket");
continue;
}
killflag = 0;
/*
* Check if user is banned
*/
if (loop.do_bancheck) {
if (find_tkline_match(cptr, 0) < 0) {
/*
* Client exited
*/
continue;
}
find_shun(cptr);
if (!killflag && IsPerson(cptr)) {
/*
* If it's a user, we check for CONF_BAN_USER
*/
bconf =
Find_ban(cptr, make_user_host(cptr->
user ? cptr->user->username : cptr->
username,
cptr->user ? cptr->user->realhost : cptr->
sockhost), CONF_BAN_USER);
if (bconf)
killflag++;
if (!killflag && !IsAnOper(cptr) &&
(bconf =
Find_ban(NULL, cptr->info, CONF_BAN_REALNAME))) {
killflag++;
}
}
/*
* If no cookie, we search for Z:lines
*/
if (!killflag)
if ((bconf =
Find_ban(cptr, Inet_ia2p(&cptr->ip),
CONF_BAN_IP)))
killflag++;
if (killflag) {
if (IsPerson(cptr))
sendto_realops("Ban active for %s (%s)",
get_client_name(cptr, FALSE),
bconf->reason ? bconf->
reason : "no reason");
if (IsServer(cptr))
sendto_realops
("Ban active for server %s (%s)",
get_client_name(cptr, FALSE),
bconf->reason ? bconf->
reason : "no reason");
if (bconf->reason) {
if (IsPerson(cptr))
snprintf(banbuf, sizeof banbuf - 1,
"User has been banned (%s)", bconf->reason);
else
snprintf(banbuf, sizeof banbuf - 1,
"Banned (%s)", bconf->reason);
(void)exit_client(cptr, cptr, &me,
banbuf);
} else {
if (IsPerson(cptr))
(void)exit_client(cptr, cptr,
&me,
"User has been banned");
else
(void)exit_client(cptr, cptr,
&me, "Banned");
}
continue;
}
}
/* Do spamfilter 'user' banchecks.. */
if (loop.do_bancheck_spamf_user && IsPerson(cptr))
{
if (find_spamfilter_user(cptr, SPAMFLAG_NOWARN) == FLUSH_BUFFER)
continue;
}
if (loop.do_bancheck_spamf_away && IsPerson(cptr) && cptr->user->away)
{
if (dospamfilter(cptr, cptr->user->away, SPAMF_AWAY, NULL, SPAMFLAG_NOWARN, NULL) == FLUSH_BUFFER)
continue;
}
/*
* We go into ping phase
*/
ping =
IsRegistered(cptr) ? (cptr->class ? cptr->
class->pingfreq : CONNECTTIMEOUT) : CONNECTTIMEOUT;
Debug((DEBUG_DEBUG, "c(%s)=%d p %d k %d a %d", cptr->name,
cptr->status, ping, killflag,
currenttime - cptr->lasttime));
/* If ping is less than or equal to the last time we received a command from them */
if (ping <= (currenttime - cptr->lasttime))
{
if (
/* If we have sent a ping */
((cptr->flags & FLAGS_PINGSENT)
/* And they had 2x ping frequency to respond */
&& ((currenttime - cptr->lasttime) >= (2 * ping)))
||
/* Or isn't registered and time spent is larger than ping .. */
(!IsRegistered(cptr) && (currenttime - cptr->since >= ping))
)
{
/* if it's registered and doing dns/auth, timeout */
if (!IsRegistered(cptr) && (DoingDNS(cptr) || DoingAuth(cptr)))
{
if (cptr->authfd >= 0) {
CLOSE_SOCK(cptr->authfd);
--OpenFiles;
cptr->authfd = -1;
cptr->count = 0;
*cptr->buffer = '\0';
}
if (SHOWCONNECTINFO && !cptr->serv) {
if (DoingDNS(cptr))
sendto_one(cptr,
REPORT_FAIL_DNS);
else if (DoingAuth(cptr))
sendto_one(cptr,
REPORT_FAIL_ID);
}
Debug((DEBUG_NOTICE,
"DNS/AUTH timeout %s",
get_client_name(cptr, TRUE)));
unrealdns_delreq_bycptr(cptr);
ClearAuth(cptr);
ClearDNS(cptr);
SetAccess(cptr);
cptr->firsttime = currenttime;
cptr->lasttime = currenttime;
continue;
}
if (IsServer(cptr) || IsConnecting(cptr) ||
IsHandshake(cptr)
#ifdef USE_SSL
|| IsSSLConnectHandshake(cptr)
#endif
) {
sendto_realops
("No response from %s, closing link",
get_client_name(cptr, FALSE));
sendto_serv_butone(&me,
":%s GLOBOPS :No response from %s, closing link",
me.name, get_client_name(cptr,
FALSE));
}
#ifdef USE_SSL
if (IsSSLAcceptHandshake(cptr))
Debug((DEBUG_DEBUG, "ssl accept handshake timeout: %s (%li-%li > %li)", cptr->sockhost,
currenttime, cptr->since, ping));
#endif
(void)ircsprintf(scratch, "Ping timeout: %ld seconds",
(long) (TStime() - cptr->lasttime));
exit_client(cptr, cptr, &me, scratch);
continue;
}
else if (IsRegistered(cptr) &&
((cptr->flags & FLAGS_PINGSENT) == 0)) {
/*
* if we havent PINGed the connection and we havent
* heard from it in a while, PING it to make sure
* it is still alive.
*/
cptr->flags |= FLAGS_PINGSENT;
/*
* not nice but does the job
*/
cptr->lasttime = currenttime - ping;
sendto_one(cptr, "%s :%s",
IsToken(cptr) ? TOK_PING : MSG_PING,
me.name);
}
}
/*
* Check UNKNOWN connections - if they have been in this state
* for > 100s, close them.
*/
if (IsUnknown(cptr)
#ifdef USE_SSL
|| (IsSSLAcceptHandshake(cptr) || IsSSLConnectHandshake(cptr))
#endif
)
if (cptr->firsttime ? ((currenttime - cptr->firsttime) >
100) : 0)
(void)exit_client(cptr, cptr, &me,
"Connection Timed Out");
}
/*
* EXPLANATION
* * on a server with a large volume of clients, at any given point
* * there may be a client which needs to be pinged the next second,
* * or even right away (a second may have passed while running
* * check_pings). Preserving CPU time is more important than
* * pinging clients out at exact times, IMO. Therefore, I am going to make
* * check_pings always return currenttime + 9. This means that it may take
* * a user up to 9 seconds more than pingfreq to timeout. Oh well.
* * Plus, the number is 9 to 'stagger' our check_pings calls out over
* * time, to avoid doing it and the other tasks ircd does at the same time
* * all the time (which are usually done on intervals of 5 seconds or so).
* * - lucas
* *
*/
loop.do_bancheck = loop.do_bancheck_spamf_user = loop.do_bancheck_spamf_away = 0;
Debug((DEBUG_NOTICE, "Next check_ping() call at: %s, %d %d %d",
myctime(currenttime+9), ping, currenttime+9, currenttime));
return currenttime + 9;
}
/*
** bad_command
** This is called when the commandline is not acceptable.
** Give error message and exit without starting anything.
*/
static int bad_command(void)
{
#ifndef _WIN32
(void)printf
("Usage: ircd [-f config] [-h servername] [-p portnumber] [-x loglevel] [-t] [-H]\n");
(void)printf("Server not started\n\n");
#else
if (!IsService) {
MessageBox(NULL,
"Usage: wircd [-h servername] [-p portnumber] [-x loglevel]\n",
"UnrealIRCD/32", MB_OK);
}
#endif
return (-1);
}
char chess[] = {
85, 110, 114, 101, 97, 108, 0
};
#ifndef NO_FDLIST
inline TS check_fdlists(TS now)
{
aClient *cptr;
int pri; /* temp. for priority */
int i, j;
j = 0;
for (i = LastSlot; i >= 0; i--) {
if (!(cptr = local[i]))
continue;
if (IsServer(cptr) || IsListening(cptr)
|| IsOper(cptr) || DoingAuth(cptr)) {
busycli_fdlist.entry[++j] = i;
continue;
}
pri = cptr->priority;
if (cptr->receiveM == cptr->lastrecvM)
pri += 2; /* lower a bit */
else
pri -= 30;
if (pri < 0)
pri = 0;
if (pri > 80)
pri = 80;
cptr->lastrecvM = cptr->receiveM;
cptr->priority = pri;
if ((pri < 10) || (!lifesux && (pri < 25)))
busycli_fdlist.entry[++j] = i;
}
busycli_fdlist.last_entry = j; /* rest of the fdlist is garbage */
return (now + FDLISTCHKFREQ + lifesux * FDLISTCHKFREQ);
}
EVENT(e_check_fdlists)
{
check_fdlists(TStime());
}
#endif
static void version_check_logerror(char *fmt, ...)
{
va_list va;
char buf[1024];
va_start(va, fmt);
vsnprintf(buf, sizeof(buf), fmt, va);
va_end(va);
#ifndef _WIN32
fprintf(stderr, "[!!!] %s\n", buf);
#else
win_log("[!!!] %s", buf);
#endif
}
/** Ugly version checker that ensures zlib/ssl/curl runtime libraries match the
* version we compiled for.
*/
static void do_version_check()
{
const char *compiledfor, *runtime;
int error = 0;
#ifdef USE_SSL
compiledfor = OPENSSL_VERSION_TEXT;
runtime = SSLeay_version(SSLEAY_VERSION);
if (strcasecmp(compiledfor, runtime))
{
version_check_logerror("OpenSSL version mismatch: compiled for '%s', library is '%s'",
compiledfor, runtime);
error=1;
}
#endif
#ifdef ZIP_LINKS
runtime = zlibVersion();
compiledfor = ZLIB_VERSION;
if (*compiledfor != *runtime)
{
version_check_logerror("Zlib version mismatch: compiled for '%s', library is '%s'",
compiledfor, runtime);
error = 1;
}
#endif
#ifdef USE_LIBCURL
/* Perhaps someone should tell them to do this a bit more easy ;)
* problem is runtime output is like: 'libcurl/7.11.1 zlib/1.2.1 c-ares/1.2.0'
* while header output is like: '7.11.1'.
*/
{
char buf[128], *p;
runtime = curl_version();
compiledfor = LIBCURL_VERSION;
if (!strncmp(runtime, "libcurl/", 8))
{
strlcpy(buf, runtime+8, sizeof(buf));
p = strchr(buf, ' ');
if (p)
{
*p = '\0';
if (strcmp(compiledfor, buf))
{
version_check_logerror("Curl version mismatch: compiled for '%s', library is '%s'",
compiledfor, buf);
error = 1;
}
}
}
}
#endif
if (error)
{
#ifndef _WIN32
version_check_logerror("Header<->library mismatches can make UnrealIRCd *CRASH*! "
"Make sure you don't have multiple versions of openssl or zlib installed (eg: "
"one in /usr and one in /usr/local). And, if you recently upgraded them, "
"be sure to recompile Unreal.");
#else
version_check_logerror("Header<->library mismatches can make UnrealIRCd *CRASH*! "
"This should never happen with official Windows builds... unless "
"you overwrote any .dll files with newer/older ones or something.");
win_error();
#endif
tainted = 1;
}
}
extern time_t TSoffset;
extern int unreal_time_synch(int timeout);
extern MODVAR Event *events;
extern struct MODVAR ThrottlingBucket *ThrottlingHash[THROTTLING_HASH_SIZE+1];
/** This functions resets a couple of timers and does other things that
* are absolutely cruicial when the clock is adjusted - particularly
* when the clock goes backwards. -- Syzop
*/
void fix_timers(void)
{
int i, cnt;
aClient *acptr;
Event *e;
struct ThrottlingBucket *n;
struct ThrottlingBucket z = { NULL, NULL, {0}, 0, 0};
/* Client time stuff */
for (i = 0; i <= LastSlot; i++)
{
if (!(acptr = local[i]) || IsMe(acptr))
continue;
/* all (servers AND users) */
if (MyConnect(acptr))
{
if (acptr->since > TStime())
{
Debug((DEBUG_DEBUG, "fix_timers(): %s: acptr->since %ld -> %ld",
acptr->name, acptr->since, TStime()));
acptr->since = TStime();
}
if (acptr->lasttime > TStime())
{
Debug((DEBUG_DEBUG, "fix_timers(): %s: acptr->lasttime %ld -> %ld",
acptr->name, acptr->lasttime, TStime()));
acptr->lasttime = TStime();
}
if (acptr->last > TStime())
{
Debug((DEBUG_DEBUG, "fix_timers(): %s: acptr->last %ld -> %ld",
acptr->name, acptr->last, TStime()));
acptr->last = TStime();
}
}
/* users */
if (MyClient(acptr))
{
if (acptr->nextnick > TStime())
{
Debug((DEBUG_DEBUG, "fix_timers(): %s: acptr->nextnick %ld -> %ld",
acptr->name, acptr->nextnick, TStime()));
acptr->nextnick = TStime();
}
if (acptr->nexttarget > TStime())
{
Debug((DEBUG_DEBUG, "fix_timers(): %s: acptr->nexttarget %ld -> %ld",
acptr->name, acptr->nexttarget, TStime()));
acptr->nexttarget = TStime();
}
}
}
/* Reset all event timers */
for (e = events; e; e = e->next)
{
if (e->last > TStime())
{
Debug((DEBUG_DEBUG, "fix_timers(): %s: e->last %ld -> %ld",
e->name, e->last, TStime()-1));
e->last = TStime()-1;
}
}
/* Just flush all throttle stuff... */
cnt = 0;
for (i = 0; i < THROTTLING_HASH_SIZE; i++)
for (n = ThrottlingHash[i]; n; n = n->next)
{
z.next = (struct ThrottlingBucket *) DelListItem(n, ThrottlingHash[i]);
cnt++;
MyFree(n);
n = &z;
}
Debug((DEBUG_DEBUG, "fix_timers(): removed %d throttling item(s)", cnt));
Debug((DEBUG_DEBUG, "fix_timers(): updating nextping/nextconnect/nextdnscheck/nextexpire (%ld/%ld/%ld/%ld)",
nextping, nextconnect, nextdnscheck, nextexpire));
nextping = nextconnect = nextdnscheck = nextexpire = 1;
}
#ifndef _WIN32
static void generate_cloakkeys()
{
/* Generate 3 cloak keys */
#define GENERATE_CLOAKKEY_MINLEN 16
#define GENERATE_CLOAKKEY_MAXLEN 32 /* Length of cloak keys to generate. */
char keyBuf[GENERATE_CLOAKKEY_MAXLEN + 1];
int keyNum;
int keyLen;
int charIndex;
int value;
short has_upper;
short has_lower;
short has_num;
fprintf(stderr, "Here are 3 random cloak keys:\n");
for (keyNum = 0; keyNum < 3; ++keyNum)
{
has_upper = 0;
has_lower = 0;
has_num = 0;
keyLen = (getrandom8() % (GENERATE_CLOAKKEY_MAXLEN - GENERATE_CLOAKKEY_MINLEN + 1)) + GENERATE_CLOAKKEY_MINLEN;
for (charIndex = 0; charIndex < keyLen; ++charIndex)
{
switch (getrandom8() % 3)
{
case 0: /* Uppercase. */
keyBuf[charIndex] = (char)('A' + (getrandom8() % ('Z' - 'A')));
has_upper = 1;
break;
case 1: /* Lowercase. */
keyBuf[charIndex] = (char)('a' + (getrandom8() % ('z' - 'a')));
has_lower = 1;
break;
case 2: /* Digit. */
keyBuf[charIndex] = (char)('0' + (getrandom8() % ('9' - '0')));
has_num = 1;
break;
}
}
keyBuf[keyLen] = '\0';
if (has_upper && has_lower && has_num)
(void)fprintf(stderr, "%s\n", keyBuf);
else
/* Try again. For this reason, keyNum must be signed. */
keyNum--;
}
}
#endif
/* MY tdiff... because 'double' sucks.
* This should work until 2038, and very likely after that as well
* because 'long' should be 64 bit on all systems by then... -- Syzop
*/
#define mytdiff(a, b) ((long)a - (long)b)
#ifndef _WIN32
int main(int argc, char *argv[])
#else
int InitwIRCD(int argc, char *argv[])
#endif
{
#ifdef _WIN32
WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
#else
uid_t uid, euid;
gid_t gid, egid;
TS delay = 0;
struct passwd *pw;
struct group *gr;
#endif
#ifdef HAVE_PSTAT
union pstun pstats;
#endif
int portarg = 0;
#ifdef FORCE_CORE
struct rlimit corelim;
#endif
#ifndef NO_FDLIST
TS nextfdlistcheck = 0; /*end of priority code */
#endif
memset(&botmotd, '\0', sizeof(aMotdFile));
memset(&rules, '\0', sizeof(aMotdFile));
memset(&opermotd, '\0', sizeof(aMotdFile));
memset(&motd, '\0', sizeof(aMotdFile));
memset(&smotd, '\0', sizeof(aMotdFile));
memset(&svsmotd, '\0', sizeof(aMotdFile));
#ifdef _WIN32
CreateMutex(NULL, FALSE, "UnrealMutex");
SetErrorMode(SEM_FAILCRITICALERRORS);
#endif
#if !defined(_WIN32) && !defined(_AMIGA)
sbrk0 = (char *)sbrk((size_t)0);
uid = getuid();
euid = geteuid();
gid = getgid();
egid = getegid();
#ifndef IRC_USER
if (!euid)
{
fprintf(stderr,
"WARNING: You are running UnrealIRCd as root and it is not\n"
" configured to drop priviliges. This is _very_ dangerous,\n"
" as any compromise of your UnrealIRCd is the same as\n"
" giving a cracker root SSH access to your box.\n"
" You should either start UnrealIRCd under a different\n"
" account than root, or set IRC_USER in include/config.h\n"
" to a nonprivileged username and recompile.\n");
}
#endif /* IRC_USER */
# ifdef PROFIL
(void)monstartup(0, etext);
(void)moncontrol(1);
(void)signal(SIGUSR1, s_monitor);
# endif
#endif
#if defined(IRC_USER) && defined(IRC_GROUP)
if ((int)getuid() == 0) {
pw = getpwnam(IRC_USER);
gr = getgrnam(IRC_GROUP);
if ((pw == NULL) || (gr == NULL)) {
fprintf(stderr, "ERROR: Unable to lookup to specified user (IRC_USER) or group (IRC_GROUP): %s\n", strerror(errno));
exit(-1);
} else {
irc_uid = pw->pw_uid;
irc_gid = gr->gr_gid;
}
}
#endif
#ifdef CHROOTDIR
if (chdir(dpath)) {
perror("chdir");
fprintf(stderr, "ERROR: Unable to change to directory '%s'\n", dpath);
exit(-1);
}
if (geteuid() != 0)
fprintf(stderr, "WARNING: IRCd compiled with CHROOTDIR but effective user id is not root!? "
"Booting is very likely to fail...\n");
init_resolver(1);
{
struct stat sb;
mode_t umaskold;
umaskold = umask(0);
if (mkdir("dev", S_IRUSR|S_IWUSR|S_IXUSR|S_IXGRP|S_IXOTH) != 0 && errno != EEXIST)
{
fprintf(stderr, "ERROR: Cannot mkdir dev: %s\n", strerror(errno));
exit(5);
}
if (stat("/dev/urandom", &sb) != 0)
{
fprintf(stderr, "ERROR: Cannot stat /dev/urandom: %s\n", strerror(errno));
exit(5);
}
if (mknod("dev/urandom", sb.st_mode, sb.st_rdev) != 0 && errno != EEXIST)
{
fprintf(stderr, "ERROR: Cannot mknod dev/urandom: %s\n", strerror(errno));
exit(5);
}
if (stat("/dev/null", &sb) != 0)
{
fprintf(stderr, "ERROR: Cannot stat /dev/null: %s\n", strerror(errno));
exit(5);
}
if (mknod("dev/null", sb.st_mode, sb.st_rdev) != 0 && errno != EEXIST)
{
fprintf(stderr, "ERROR: Cannot mknod dev/null: %s\n", strerror(errno));
exit(5);
}
if (stat("/dev/tty", &sb) != 0)
{
fprintf(stderr, "ERROR: Cannot stat /dev/tty: %s\n", strerror(errno));
exit(5);
}
if (mknod("dev/tty", sb.st_mode, sb.st_rdev) != 0 && errno != EEXIST)
{
fprintf(stderr, "ERROR: Cannot mknod dev/tty: %s\n", strerror(errno));
exit(5);
}
umask(umaskold);
}
if (chroot(DPATH)) {
(void)fprintf(stderr, "ERROR: Cannot (chdir/)chroot to directory '%s'\n", dpath);
exit(5);
}
#endif /*CHROOTDIR*/
#ifndef _WIN32
myargv = argv;
#else
cmdLine = GetCommandLine();
#endif
#ifndef _WIN32
(void)umask(077); /* better safe than sorry --SRB */
#else
WSAStartup(wVersionRequested, &wsaData);
#endif
bzero((char *)&me, sizeof(me));
bzero(&StatsZ, sizeof(StatsZ));
setup_signals();
charsys_reset();
init_ircstats();
#ifdef USE_LIBCURL
url_init();
#endif
tkl_init();
umode_init();
#ifdef EXTCMODE
extcmode_init();
#endif
extban_init();
init_random(); /* needs to be done very early!! */
clear_scache_hash_table();
#ifdef FORCE_CORE
corelim.rlim_cur = corelim.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_CORE, &corelim))
printf("unlimit core size failed; errno = %d\n", errno);
#endif
/*
* ** All command line parameters have the syntax "-fstring"
* ** or "-f string" (e.g. the space is optional). String may
* ** be empty. Flag characters cannot be concatenated (like
* ** "-fxyz"), it would conflict with the form "-fstring".
*/
while (--argc > 0 && (*++argv)[0] == '-') {
char *p = argv[0] + 1;
int flag = *p++;
if (flag == '\0' || *p == '\0') {
if (argc > 1 && argv[1][0] != '-') {
p = *++argv;
argc -= 1;
} else
p = "";
}
switch (flag) {
#ifndef _WIN32
case 'a':
bootopt |= BOOT_AUTODIE;
break;
case 'c':
bootopt |= BOOT_CONSOLE;
break;
case 'q':
bootopt |= BOOT_QUICK;
break;
case 'd':
(void)setuid((uid_t) uid);
#else
case 'd':
#endif
dpath = p;
break;
case 'F':
bootopt |= BOOT_NOFORK;
break;
#ifndef _WIN32
case 'f':
#ifndef CMDLINE_CONFIG
if ((uid == euid) && (gid == egid))
configfile = p;
else
printf("ERROR: Command line config with a setuid/setgid ircd is not allowed");
#else
(void)setuid((uid_t) uid);
configfile = p;
#endif
break;
case 'h':
if (!strchr(p, '.')) {
(void)printf
("ERROR: %s is not valid: Server names must contain at least 1 \".\"\n",
p);
exit(1);
}
strncpyzt(me.name, p, sizeof(me.name));
break;
#endif
#ifndef _WIN32
case 'P':{
short type;
char *result;
srandom(TStime());
if ((type = Auth_FindType(p)) == -1) {
printf("No such auth type %s\n", p);
exit(0);
}
p = *++argv;
argc--;
#ifdef AUTHENABLE_UNIXCRYPT
if ((type == AUTHTYPE_UNIXCRYPT) && (strlen(p) > 8))
{
printf("WARNING: Password truncated to 8 characters due to 'crypt' algorithm. "
"You are suggested to use the 'md5' algorithm instead.");
p[8] = '\0';
}
#endif
if (!(result = Auth_Make(type, p))) {
printf("Authentication failed\n");
exit(0);
}
printf("Encrypted password is: %s\n", result);
exit(0);
}
#endif
case 'p':
if ((portarg = atoi(p)) > 0)
portnum = portarg;
break;
case 's':
(void)printf("sizeof(aClient) == %ld\n",
(long)sizeof(aClient));
(void)printf("sizeof(aChannel) == %ld\n",
(long)sizeof(aChannel));
(void)printf("sizeof(aServer) == %ld\n",
(long)sizeof(aServer));
(void)printf("sizeof(Link) == %ld\n",
(long)sizeof(Link));
(void)printf("sizeof(anUser) == %ld\n",
(long)sizeof(anUser));
(void)printf("sizeof(aTKline) == %ld\n",
(long)sizeof(aTKline));
(void)printf("sizeof(struct ircstatsx) == %ld\n",
(long)sizeof(struct ircstatsx));
(void)printf("aClient remote == %ld\n",
(long)CLIENT_REMOTE_SIZE);
exit(0);
break;
#ifndef _WIN32
case 't':
(void)setuid((uid_t) uid);
bootopt |= BOOT_TTY;
break;
case 'v':
(void)printf("%s build %s\n", version, buildid);
#else
case 'v':
if (!IsService) {
MessageBox(NULL, version,
"UnrealIRCD/Win32 version", MB_OK);
}
#endif
exit(0);
case 'C':
config_verbose = atoi(p);
break;
case 'x':
#ifdef DEBUGMODE
# ifndef _WIN32
(void)setuid((uid_t) uid);
# endif
debuglevel = atoi(p);
debugmode = *p ? p : "0";
bootopt |= BOOT_DEBUG;
break;
#else
# ifndef _WIN32
(void)fprintf(stderr,
"%s: DEBUGMODE must be defined for -x y\n",
myargv[0]);
# else
if (!IsService) {
MessageBox(NULL,
"DEBUGMODE must be defined for -x option",
"UnrealIRCD/32", MB_OK);
}
# endif
exit(0);
#endif
#ifndef _WIN32
case 'k':
generate_cloakkeys();
exit(0);
#endif
default:
return bad_command();
break;
}
}
do_version_check();
#ifndef CHROOTDIR
if (chdir(dpath)) {
# ifndef _WIN32
perror("chdir");
fprintf(stderr, "ERROR: Unable to change to directory '%s'\n", dpath);
# else
if (!IsService) {
MessageBox(NULL, strerror(GetLastError()),
"UnrealIRCD/32: chdir()", MB_OK);
}
# endif
exit(-1);
}
#endif
#ifndef _WIN32
mkdir("tmp", S_IRUSR|S_IWUSR|S_IXUSR); /* Create the tmp dir, if it doesn't exist */
#if defined(USE_LIBCURL) && defined(REMOTEINC_SPECIALCACHE)
mkdir("cache", S_IRUSR|S_IWUSR|S_IXUSR); /* Create the cache dir, if using curl and it doesn't exist */
#endif
#else
mkdir("tmp");
#if defined(USE_LIBCURL) && defined(REMOTEINC_SPECIALCACHE)
mkdir("cache");
#endif
#endif
#ifndef _WIN32
/*
* didn't set debuglevel
*/
/*
* but asked for debugging output to tty
*/
if ((debuglevel < 0) && (bootopt & BOOT_TTY)) {
(void)fprintf(stderr,
"you specified -t without -x. use -x <n>\n");
exit(-1);
}
#endif
/* HACK! This ifndef should be removed when the restart-on-w32-brings-up-dialog bug
* is fixed. This is just an ugly "ignore the invalid parameter" thing ;). -- Syzop
*/
#ifndef _WIN32
if (argc > 0)
return bad_command(); /* This should exit out */
#endif
#ifndef _WIN32
fprintf(stderr, "%s", unreallogo);
fprintf(stderr, " v%s\n", VERSIONONLY);
fprintf(stderr, " using %s\n", tre_version());
#ifdef USE_SSL
fprintf(stderr, " using %s\n", SSLeay_version(SSLEAY_VERSION));
#endif
#ifdef ZIP_LINKS
fprintf(stderr, " using zlib %s\n", zlibVersion());
#endif
#ifdef USE_LIBCURL
fprintf(stderr, " using %s\n", curl_version());
#endif
fprintf(stderr, "\n");
#endif
clear_client_hash_table();
clear_channel_hash_table();
clear_watch_hash_table();
bzero(&loop, sizeof(loop));
init_CommandHash();
initlists();
initwhowas();
initstats();
DeleteTempModules();
booted = FALSE;
/* Hack to stop people from being able to read the config file */
#if !defined(_WIN32) && !defined(_AMIGA) && !defined(OSXTIGER) && DEFAULT_PERMISSIONS != 0
chmod(CPATH, DEFAULT_PERMISSIONS);
#endif
init_dynconf();
#ifdef STATIC_LINKING
{
ModuleInfo ModCoreInfo;
ModCoreInfo.size = sizeof(ModuleInfo);
ModCoreInfo.module_load = 0;
ModCoreInfo.handle = NULL;
l_commands_Test(&ModCoreInfo);
}
#endif
/*
* Add default class
*/
default_class =
(ConfigItem_class *) MyMallocEx(sizeof(ConfigItem_class));
default_class->flag.permanent = 1;
default_class->pingfreq = PINGFREQUENCY;
default_class->maxclients = 100;
default_class->sendq = MAXSENDQLENGTH;
default_class->name = "default";
AddListItem(default_class, conf_class);
if (init_conf(configfile, 0) < 0)
{
exit(-1);
}
booted = TRUE;
load_tunefile();
make_umodestr();
make_cmodestr();
#ifdef EXTCMODE
make_extcmodestr();
#endif
make_extbanstr();
isupport_init();
if (!find_Command_simple("AWAY") /*|| !find_Command_simple("KILL") ||
!find_Command_simple("OPER") || !find_Command_simple("PING")*/)
{
config_error("Someone forgot to load modules with proper commands in them. READ THE DOCUMENTATION");
#ifdef _WIN32
/* Temporary! */
config_error("As of Unreal3.2.1 modules are supported on windows, "
"therefore you MUST load the commands.dll module and a cloaking module. "
"Just add 'loadmodule \"modules/commands.dll\"' and 'loadmodule \"modules/cloak.dll\"' "
"to your unrealircd.conf and be sure to read the release notes!");
win_error();
#endif
exit(-4);
}
#ifdef USE_SSL
#ifndef _WIN32
fprintf(stderr, "* Initializing SSL.\n");
#endif
init_ssl();
#endif
#ifndef _WIN32
fprintf(stderr,
"* Dynamic configuration initialized .. booting IRCd.\n");
fprintf(stderr,
"---------------------------------------------------------------------\n");
#endif
if (time(NULL) > 1459461600)
{
fprintf(stderr, "WARNING: UnrealIRCd 3.2.x is no longer supported after December 31, 2016.\n"
"See https://www.unrealircd.org/docs/UnrealIRCd_3.2.x_deprecated\n");
}
open_debugfile();
#ifndef NO_FDLIST
init_fdlist(&serv_fdlist);
init_fdlist(&busycli_fdlist);
init_fdlist(&default_fdlist);
init_fdlist(&oper_fdlist);
init_fdlist(&unknown_fdlist);
{
int i;
for (i = MAXCONNECTIONS + 1; i > 0; i--)
default_fdlist.entry[i] = i;
}
#endif
if (portnum < 0)
portnum = PORTNUM;
me.port = portnum;
(void)init_sys();
me.flags = FLAGS_LISTEN;
me.fd = -1;
SetMe(&me);
make_server(&me);
#ifdef HAVE_SYSLOG
openlog("ircd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
#endif
/*
* Put in our info
*/
strncpyzt(me.info, conf_me->info, sizeof(me.info));
strncpyzt(me.name, conf_me->name, sizeof(me.name));
/*
* We accept the first listen record
*/
portnum = conf_listen->port;
/*
* This is completely unneeded-Sts
me.ip.S_ADDR =
*conf_listen->ip != '*' ? inet_addr(conf_listen->ip) : INADDR_ANY;
*/
Debug((DEBUG_ERROR, "Port = %d", portnum));
if (inetport(&me, conf_listen->ip, portnum))
exit(1);
set_non_blocking(me.fd, &me);
conf_listen->options |= LISTENER_BOUND;
me.umodes = conf_listen->options;
conf_listen->listener = &me;
run_configuration();
ircd_log(LOG_ERROR, "UnrealIRCd started.");
read_motd(conf_files->botmotd_file, &botmotd);
read_motd(conf_files->rules_file, &rules);
read_motd(conf_files->opermotd_file, &opermotd);
read_motd(conf_files->motd_file, &motd);
read_motd(conf_files->smotd_file, &smotd);
read_motd(conf_files->svsmotd_file, &svsmotd);
strncpy(me.sockhost, conf_listen->ip, sizeof(me.sockhost) - 1);
if (me.name[0] == '\0')
strncpyzt(me.name, me.sockhost, sizeof(me.name));
me.hopcount = 0;
me.authfd = -1;
me.next = NULL;
me.user = NULL;
me.from = &me;
me.class = (ConfigItem_class *) conf_listen;
/*
* This listener will never go away
*/
conf_listen->clients++;
me_hash = find_or_add(me.name);
me.serv->up = me_hash;
me.serv->numeric = conf_me->numeric;
add_server_to_table(&me);
timeofday = time(NULL);
me.lasttime = me.since = me.firsttime = TStime();
(void)add_to_client_hash_table(me.name, &me);
#if !defined(_AMIGA) && !defined(_WIN32) && !defined(NO_FORKING)
if (!(bootopt & BOOT_NOFORK))
if (fork())
exit(0);
#endif
(void)ircsprintf(REPORT_DO_DNS, ":%s %s", me.name, BREPORT_DO_DNS);
(void)ircsprintf(REPORT_FIN_DNS, ":%s %s", me.name, BREPORT_FIN_DNS);
(void)ircsprintf(REPORT_FIN_DNSC, ":%s %s", me.name, BREPORT_FIN_DNSC);
(void)ircsprintf(REPORT_FAIL_DNS, ":%s %s", me.name, BREPORT_FAIL_DNS);
(void)ircsprintf(REPORT_DO_ID, ":%s %s", me.name, BREPORT_DO_ID);
(void)ircsprintf(REPORT_FIN_ID, ":%s %s", me.name, BREPORT_FIN_ID);
(void)ircsprintf(REPORT_FAIL_ID, ":%s %s", me.name, BREPORT_FAIL_ID);
R_do_dns = strlen(REPORT_DO_DNS);
R_fin_dns = strlen(REPORT_FIN_DNS);
R_fin_dnsc = strlen(REPORT_FIN_DNSC);
R_fail_dns = strlen(REPORT_FAIL_DNS);
R_do_id = strlen(REPORT_DO_ID);
R_fin_id = strlen(REPORT_FIN_ID);
R_fail_id = strlen(REPORT_FAIL_ID);
#if !defined(IRC_USER) && !defined(_WIN32)
if ((uid != euid) && !euid) {
(void)fprintf(stderr,
"ERROR: do not run ircd setuid root. Make it setuid a normal user.\n");
exit(-1);
}
#endif
#if defined(IRC_USER) && defined(IRC_GROUP)
if ((int)getuid() == 0) {
/* NOTE: irc_uid/irc_gid have been looked up earlier, before the chrooting code */
if ((irc_uid == 0) || (irc_gid == 0)) {
(void)fprintf(stderr,
"ERROR: SETUID and SETGID have not been set properly"
"\nPlease read your documentation\n(HINT: IRC_USER and IRC_GROUP in include/config.h cannot be root/wheel)\n");
exit(-1);
} else {
/*
* run as a specified user
*/
(void)fprintf(stderr, "WARNING: ircd invoked as root\n");
(void)fprintf(stderr, " changing to uid %d\n", irc_uid);
(void)fprintf(stderr, " changing to gid %d\n", irc_gid);
if (setgid(irc_gid))
{
fprintf(stderr, "ERROR: Unable to change group: %s\n", strerror(errno));
exit(-1);
}
if (setuid(irc_uid))
{
fprintf(stderr, "ERROR: Unable to change userid: %s\n", strerror(errno));
exit(-1);
}
}
}
#endif
if (TIMESYNCH)
{
if (!unreal_time_synch(TIMESYNCH_TIMEOUT))
ircd_log(LOG_ERROR, "TIME SYNCH: Unable to synchronize time: %s. "
"This means UnrealIRCd was unable to synchronize the IRCd clock to a known good time source. "
"As long as the server owner keeps the server clock synchronized through NTP, everything will be fine.",
unreal_time_synch_error());
}
fix_timers(); /* Fix timers AFTER reading tune file AND timesynch */
write_pidfile();
Debug((DEBUG_NOTICE, "Server ready..."));
SetupEvents();
#ifdef THROTTLING
init_throttling_hash();
#endif
#ifdef NEWCHFLOODPROT
init_modef();
#endif
loop.do_bancheck = 0;
loop.ircd_booted = 1;
#if defined(HAVE_SETPROCTITLE)
setproctitle("%s", me.name);
#elif defined(HAVE_PSTAT)
pstats.pst_command = me.name;
pstat(PSTAT_SETCMD, pstats, strlen(me.name), 0, 0);
#elif defined(HAVE_PSSTRINGS)
PS_STRINGS->ps_nargvstr = 1;
PS_STRINGS->ps_argvstr = me.name;
#endif
module_loadall(0);
#ifdef STATIC_LINKING
l_commands_Load(0);
#endif
#ifndef NO_FDLIST
check_fdlists(TStime());
#endif
#ifdef _WIN32
return 1;
}
void SocketLoop(void *dummy)
{
TS delay = 0;
static TS lastglinecheck = 0;
TS last_tune;
#ifndef NO_FDLIST
TS nextfdlistcheck = 0; /*end of priority code */
#endif
while (1)
#else
nextping = timeofday;
/*
* Forever drunk .. forever drunk ..
* * (Sorry Alphaville.)
*/
for (;;)
#endif
{
#define NEGATIVE_SHIFT_WARN -15
#define POSITIVE_SHIFT_WARN 20
timeofday = time(NULL) + TSoffset;
if (oldtimeofday == 0)
oldtimeofday = timeofday; /* pretend everything is ok the first time.. */
if (mytdiff(timeofday, oldtimeofday) < NEGATIVE_SHIFT_WARN) {
/* tdiff = # of seconds of time set backwards (positive number! eg: 60) */
long tdiff = oldtimeofday - timeofday;
ircd_log(LOG_ERROR, "WARNING: Time running backwards! Clock set back ~%ld seconds (%ld -> %ld)",
tdiff, oldtimeofday, timeofday);
ircd_log(LOG_ERROR, "[TimeShift] Resetting a few timers to prevent IRCd freeze!");
sendto_realops("WARNING: Time running backwards! Clock set back ~%ld seconds (%ld -> %ld)",
tdiff, oldtimeofday, timeofday);
sendto_realops("Incorrect time for IRC servers is a serious problem. "
"Time being set backwards (either by TSCTL or by resetting the clock) is "
"even more serious and can cause clients to freeze, channels to be "
"taken over, and other issues.");
sendto_realops("Please be sure your clock is always synchronized before "
"the IRCd is started or use the built-in timesynch feature.");
sendto_realops("[TimeShift] Resetting a few timers to prevent IRCd freeze!");
fix_timers();
nextfdlistcheck = 0;
} else
if (mytdiff(timeofday, oldtimeofday) > POSITIVE_SHIFT_WARN) /* do not set too low or you get false positives */
{
/* tdiff = # of seconds of time set forward (eg: 60) */
long tdiff = timeofday - oldtimeofday;
ircd_log(LOG_ERROR, "WARNING: Time jumped ~%ld seconds ahead! (%ld -> %ld)",
tdiff, oldtimeofday, timeofday);
ircd_log(LOG_ERROR, "[TimeShift] Resetting some timers!");
sendto_realops("WARNING: Time jumped ~%ld seconds ahead! (%ld -> %ld)",
tdiff, oldtimeofday, timeofday);
sendto_realops("Incorrect time for IRC servers is a serious problem. "
"Time being adjusted (either by TSCTL or by resetting the clock) "
"more than a few seconds forward/backward can lead to serious issues.");
sendto_realops("Please be sure your clock is always synchronized before "
"the IRCd is started or use the built-in timesynch feature.");
sendto_realops("[TimeShift] Resetting some timers!");
fix_timers();
nextfdlistcheck = 0;
}
if (highesttimeofday+NEGATIVE_SHIFT_WARN > timeofday)
{
if (lasthighwarn > timeofday)
lasthighwarn = timeofday;
if (timeofday - lasthighwarn > 300)
{
ircd_log(LOG_ERROR, "[TimeShift] The (IRCd) clock was set backwards. "
"Waiting for time to be OK again. This will be in %ld seconds",
highesttimeofday - timeofday);
sendto_realops("[TimeShift] The (IRCd) clock was set backwards. Timers, nick- "
"and channel-timestamps are possibly incorrect. This message will "
"repeat itself until we catch up with the original time, which will be "
"in %ld seconds", highesttimeofday - timeofday);
lasthighwarn = timeofday;
}
} else {
highesttimeofday = timeofday;
}
oldtimeofday = timeofday;
LockEventSystem();
DoEvents();
UnlockEventSystem();
/*
* ** Run through the hashes and check lusers every
* ** second
* ** also check for expiring glines
*/
#ifndef NO_FDLIST
lastrecvK = me.receiveK;
lastsendK = me.sendK;
#endif
if (IRCstats.clients > IRCstats.global_max)
IRCstats.global_max = IRCstats.clients;
if (IRCstats.me_clients > IRCstats.me_max)
IRCstats.me_max = IRCstats.me_clients;
/*
* ** We only want to connect if a connection is due,
* ** not every time through. Note, if there are no
* ** active C lines, this call to Tryconnections is
* ** made once only; it will return 0. - avalon
*/
if (nextconnect && timeofday >= nextconnect)
nextconnect = try_connections(timeofday);
/*
* ** take the smaller of the two 'timed' event times as
* ** the time of next event (stops us being late :) - avalon
* ** WARNING - nextconnect can return 0!
*/
if (nextconnect)
delay = MIN(nextping, nextconnect);
else
delay = nextping;
delay = MIN(nextdnscheck, delay);
delay = MIN(nextexpire, delay);
delay -= timeofday;
/*
* ** Adjust delay to something reasonable [ad hoc values]
* ** (one might think something more clever here... --msa)
* ** We don't really need to check that often and as long
* ** as we don't delay too long, everything should be ok.
* ** waiting too long can cause things to timeout...
* ** i.e. PINGS -> a disconnection :(
* ** - avalon
*/
if (delay < 1)
delay = 1;
else
delay = MIN(delay, TIMESEC);
#ifdef NO_FDLIST
(void)read_message(delay);
timeofday = time(NULL) + TSoffset;
#else
(void)read_message(0, &serv_fdlist); /* servers */
(void)read_message(1, &busycli_fdlist); /* busy clients */
if (lifesux) {
static time_t alllasttime = 0;
(void)read_message(1, &serv_fdlist);
/*
* read servs more often
*/
if (lifesux > 9) { /* life really sucks */
(void)read_message(1, &busycli_fdlist);
(void)read_message(1, &serv_fdlist);
}
flush_fdlist_connections(&serv_fdlist);
timeofday = time(NULL) + TSoffset;
if ((alllasttime + (lifesux + 1)) < timeofday) {
read_message(delay, NULL); /* check everything */
alllasttime = timeofday;
}
} else {
read_message(delay, NULL); /* check everything */
timeofday = time(NULL) + TSoffset;
}
#endif
/*
* Debug((DEBUG_DEBUG, "Got message(s)"));
*/
/*
* ** ...perhaps should not do these loops every time,
* ** but only if there is some chance of something
* ** happening (but, note that conf->hold times may
* ** be changed elsewhere--so precomputed next event
* ** time might be too far away... (similarly with
* ** ping times) --msa
*/
#ifdef NO_FDLIST
if (timeofday >= nextping || loop.do_bancheck)
#else
if ((timeofday >= nextping && !lifesux) || loop.do_bancheck)
#endif
nextping = check_pings(timeofday);
if (dorehash)
{
(void)rehash(&me, &me, 1);
dorehash = 0;
}
if (dorestart)
{
server_reboot("SIGINT");
}
/*
* ** Flush output buffers on all connections timeofday if they
* ** have data in them (or at least try to flush)
* ** -avalon
*/
#ifndef NO_FDLIST
/*
* check which clients are active
*/
if (timeofday > nextfdlistcheck)
nextfdlistcheck = check_fdlists(timeofday);
#endif
flush_connections(&me);
/* ThA UnReAl TrOuBlE RePoRtInG SyStEm!!! */
if (trouble_info[0] != '\0')
{
sendto_realops("*** TROUBLE: %s ***", trouble_info);
ircd_log(LOG_ERROR, "TROUBLE: %s", trouble_info);
trouble_info[0] = '\0';
}
}
}
/*
* open_debugfile
*
* If the -t option is not given on the command line when the server is
* started, all debugging output is sent to the file set by LPATH in config.h
* Here we just open that file and make sure it is opened to fd 2 so that
* any fprintf's to stderr also goto the logfile. If the debuglevel is not
* set from the command line by -x, use /dev/null as the dummy logfile as long
* as DEBUGMODE has been defined, else dont waste the fd.
*/
static void open_debugfile(void)
{
#ifdef DEBUGMODE
int fd;
aClient *cptr;
if (debuglevel >= 0) {
cptr = make_client(NULL, NULL);
cptr->fd = 2;
SetLog(cptr);
cptr->port = debuglevel;
cptr->flags = 0;
cptr->listener = cptr;
/*
* local[2] = cptr; winlocal
*/
(void)strlcpy(cptr->sockhost, me.sockhost,
sizeof cptr->sockhost);
# ifndef _WIN32
(void)printf("isatty = %d ttyname = %#x\n",
isatty(2), (u_int)ttyname(2));
if (!(bootopt & BOOT_TTY)) { /* leave debugging output on fd 2 */
(void)truncate(LOGFILE, 0);
if ((fd = open(LOGFILE, O_WRONLY | O_CREAT, 0600)) < 0)
if ((fd = open("/dev/null", O_WRONLY)) < 0)
exit(-1);
if (fd != 2) {
(void)dup2(fd, 2);
(void)close(fd);
}
strncpyzt(cptr->name, LOGFILE, sizeof(cptr->name));
} else if (isatty(2) && ttyname(2))
strncpyzt(cptr->name, ttyname(2), sizeof(cptr->name));
else
# endif
(void)strcpy(cptr->name, "FD2-Pipe");
Debug((DEBUG_FATAL,
"Debug: File <%s> Level: %d at %s", cptr->name,
cptr->port, myctime(time(NULL))));
} else
/*
* local[2] = NULL; winlocal
*/
#endif
return;
}
static void setup_signals()
{
#ifndef _WIN32
#ifdef POSIX_SIGNALS
struct sigaction act;
act.sa_handler = SIG_IGN;
act.sa_flags = 0;
(void)sigemptyset(&act.sa_mask);
(void)sigaddset(&act.sa_mask, SIGPIPE);
(void)sigaddset(&act.sa_mask, SIGALRM);
# ifdef SIGWINCH
(void)sigaddset(&act.sa_mask, SIGWINCH);
(void)sigaction(SIGWINCH, &act, NULL);
# endif
(void)sigaction(SIGPIPE, &act, NULL);
act.sa_handler = dummy;
(void)sigaction(SIGALRM, &act, NULL);
act.sa_handler = s_rehash;
(void)sigemptyset(&act.sa_mask);
(void)sigaddset(&act.sa_mask, SIGHUP);
(void)sigaction(SIGHUP, &act, NULL);
act.sa_handler = s_restart;
(void)sigaddset(&act.sa_mask, SIGINT);
(void)sigaction(SIGINT, &act, NULL);
act.sa_handler = s_die;
(void)sigaddset(&act.sa_mask, SIGTERM);
(void)sigaction(SIGTERM, &act, NULL);
#else
# ifndef HAVE_RELIABLE_SIGNALS
(void)signal(SIGPIPE, dummy);
# ifdef SIGWINCH
(void)signal(SIGWINCH, dummy);
# endif
# else
# ifdef SIGWINCH
(void)signal(SIGWINCH, SIG_IGN);
# endif
(void)signal(SIGPIPE, SIG_IGN);
# endif
(void)signal(SIGALRM, dummy);
(void)signal(SIGHUP, s_rehash);
(void)signal(SIGTERM, s_die);
(void)signal(SIGINT, s_restart);
#endif
#ifdef RESTARTING_SYSTEMCALLS
/*
* ** At least on Apollo sr10.1 it seems continuing system calls
* ** after signal is the default. The following 'siginterrupt'
* ** should change that default to interrupting calls.
*/
(void)siginterrupt(SIGALRM, 1);
#endif
#else
(void)signal(SIGSEGV, CleanUpSegv);
#endif
}
|