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
|
/*-------------------------------------------------------*/
/* bbsd.c ( NTHU CS MapleBBS Ver 3.00 ) */
/*-------------------------------------------------------*/
/* author : opus.bbs@bbs.cs.nthu.edu.tw */
/* target : BBS daemon/main/login/top-menu routines */
/* create : 95/03/29 */
/* update : 96/10/10 */
/*-------------------------------------------------------*/
#define _MAIN_C_
#include "bbs.h"
#include "dns.h"
#include <sys/wait.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/telnet.h>
#include <sys/resource.h>
#define QLEN 3
#define PID_FILE "run/bbs.pid"
#define LOG_FILE "run/bbs.log"
#undef SERVER_USAGE
static int myports[MAX_BBSDPORT] = BBSD_PORT;
static pid_t currpid;
extern BCACHE *bshm;
extern UCACHE *ushm;
/* static int mport; */ /* Thor.990325: 不需要了:P */
static u_long tn_addr;
#ifdef CHAT_SECURE
char passbuf[PSWDLEN + 1];
#endif
#ifdef MODE_STAT
extern UMODELOG modelog;
extern time_t mode_lastchange;
#endif
/* ----------------------------------------------------- */
/* 離開 BBS 程式 */
/* ----------------------------------------------------- */
void
alog(mode, msg) /* Admin 行為記錄 */
char *mode, *msg;
{
char buf[512];
sprintf(buf, "%s %s %-13s%s\n", Now(), mode, cuser.userid, msg);
f_cat(FN_RUN_ADMIN, buf);
}
void
blog(mode, msg) /* BBS 一般記錄 */
char *mode, *msg;
{
char buf[512];
sprintf(buf, "%s %s %-13s%s\n", Now(), mode, cuser.userid, msg);
f_cat(FN_RUN_USIES, buf);
}
void
cclog(mode, msg) /* 權限購買記錄 */
char *mode, *msg;
{
char buf[512];
sprintf(buf, "%s %s %-13s%s\n", Now(), mode, cuser.userid, msg);
f_cat("run/buyperm.log", buf);
}
#ifdef MODE_STAT
void
log_modes()
{
time(&modelog.logtime);
rec_add(FN_RUN_MODE_CUR, &modelog, sizeof(UMODELOG));
}
#endif
void
u_exit(mode)
char *mode;
{
int fd, diff;
char fpath[80];
ACCT tuser;
if (currbno >= 0 && bshm->mantime[currbno] > 0)
bshm->mantime[currbno]--; /* 退出最後看的那個板 */
utmp_free(cutmp); /* 釋放 UTMP shm */
diff = (time(&cuser.lastlogin) - ap_start) / 60;
sprintf(fpath, "Stay: %d (%d)", diff, currpid);
blog(mode, fpath);
if (cuser.userlevel)
{
ve_backup(); /* 編輯器自動備份 */
brh_save(); /* 儲存閱讀記錄檔 */
}
#ifndef LOG_BMW /* 離站刪除水球 */
usr_fpath(fpath, cuser.userid, fn_amw);
unlink(fpath);
usr_fpath(fpath, cuser.userid, fn_bmw);
unlink(fpath);
#endif
#ifdef MODE_STAT
log_modes();
#endif
/* 寫回 .ACCT */
if (!HAS_STATUS(STATUS_DATALOCK)) /* itoc.010811: 沒有被站長鎖定,才可以回存 .ACCT */
{
usr_fpath(fpath, cuser.userid, fn_acct);
fd = open(fpath, O_RDWR);
if (fd >= 0)
{
if (read(fd, &tuser, sizeof(ACCT)) == sizeof(ACCT))
{
if (diff >= 1)
{
cuser.numlogins++; /* Thor.980727.註解: 在站上未超過一分鐘不予計算次數 */
addmoney(diff); /* itoc.010805: 上站一分鐘加一元 */
}
if (HAS_STATUS(STATUS_COINLOCK)) /* itoc.010831: 若是 multi-login 的第二隻以後,不儲存錢幣 */
{
cuser.money = tuser.money;
cuser.gold = tuser.gold;
}
/* itoc.010811.註解: 如果使用者在線上沒有認證的話,
那麼 cuser 及 tuser 的 userlevel/tvalid 是同步的;
但若使用者在線上回認證信/填認證碼/被站長審核註冊單..等認證通過的話,
那麼 tuser 的 userlevel/tvalid 才是比較新的 */
cuser.userlevel = tuser.userlevel;
cuser.tvalid = tuser.tvalid;
lseek(fd, (off_t) 0, SEEK_SET);
write(fd, &cuser, sizeof(ACCT));
}
close(fd);
}
}
}
void
abort_bbs()
{
if (bbstate)
u_exit("AXXED");
exit(0);
}
static void
login_abort(msg)
char *msg;
{
outs(msg);
refresh();
exit(0);
}
/* Thor.980903: lkchu patch: 不使用上站申請帳號時, 則下列 function均不用 */
#ifdef LOGINASNEW
/* ----------------------------------------------------- */
/* 檢查 user 註冊情況 */
/* ----------------------------------------------------- */
static int
belong(flist, key)
char *flist;
char *key;
{
int fd, rc;
rc = 0;
if ((fd = open(flist, O_RDONLY)) >= 0)
{
mgets(-1);
while (flist = mgets(fd))
{
str_lower(flist, flist);
if (str_str(key, flist))
{
rc = 1;
break;
}
}
close(fd);
}
return rc;
}
static int
is_badid(userid)
char *userid;
{
int ch;
char *str;
if (strlen(userid) < 2)
return 1;
if (!is_alpha(*userid))
return 1;
if (!str_cmp(userid, STR_NEW))
return 1;
str = userid;
while (ch = *(++str))
{
if (!is_alnum(ch))
return 1;
}
return (belong(FN_ETC_BADID, userid));
}
static int
uniq_userno(fd)
int fd;
{
char buf[4096];
int userno, size;
SCHEMA *sp; /* record length 16 可整除 4096 */
userno = 1;
while ((size = read(fd, buf, sizeof(buf))) > 0)
{
sp = (SCHEMA *) buf;
do
{
if (sp->userid[0] == '\0')
{
lseek(fd, -size, SEEK_CUR);
return userno;
}
userno++;
size -= sizeof(SCHEMA);
sp++;
} while (size);
}
return userno;
}
static void
acct_apply()
{
SCHEMA slot;
char buf[80];
char *userid;
int try, fd;
film_out(FILM_APPLY, 0);
memset(&cuser, 0, sizeof(ACCT));
userid = cuser.userid;
try = 0;
for (;;)
{
if (!vget(18, 0, msg_uid, userid, IDLEN + 1, DOECHO))
login_abort("\n再見 ...");
if (is_badid(userid))
{
vmsg("無法接受這個代號,請使用英文字母,並且不要包含空格");
}
else
{
usr_fpath(buf, userid, NULL);
if (dashd(buf))
vmsg("此代號已經有人使用");
else
break;
}
if (++try >= 10)
login_abort("\n您嘗試錯誤的輸入太多,請下次再來吧");
}
for (;;)
{
vget(19, 0, "請設定密碼:", buf, PSWDLEN + 1, NOECHO);
if ((strlen(buf) < 4) || !strcmp(buf, userid))
{
vmsg("密碼太簡單,易遭入侵,至少要 4 個字,請重新輸入");
continue;
}
vget(20, 0, "請檢查密碼:", buf + PSWDLEN + 2, PSWDLEN + 1, NOECHO);
if (!strcmp(buf, buf + PSWDLEN + 2))
break;
vmsg("密碼輸入錯誤, 請重新輸入密碼");
}
str_ncpy(cuser.passwd, genpasswd(buf), sizeof(cuser.passwd));
do
{
vget(20, 0, "暱 稱:", cuser.username, UNLEN + 1, DOECHO);
} while (strlen(cuser.username) < 2);
// /* itoc.010317: 提示 user 以後將不能改姓名 */
// vmsg("注意:請輸入真實姓名,本站不提供修改姓名的功\能");
do
{
vget(21, 0, "真實姓名:", cuser.realname, RNLEN + 1, DOECHO);
} while (strlen(cuser.realname) < 4);
#if 0
char year; /* 生日(民國年) */
char month; /* 生日(月) */
char day; /* 生日(日) */
#endif
int year;
do
{
vget(22, 0, "出生年(民國):", buf, 4, DOECHO);
year = atoi(buf);
} while (year < 1 || year > 255);
cuser.year = year;
do
{
vget (22, 18, "出生月:", buf, 3, DOECHO);
cuser.month = atoi(buf);
} while (cuser.month < 1 || cuser.month > 12);
do
{
vget (22, 30, "出生日:", buf, 3, DOECHO);
cuser.day = atoi(buf);
} while (cuser.day < 1 || cuser.day > 31);
do
{
vget (23, 0, "性別 (0)中性 (1)男性 (2)女性:", buf, 2, DOECHO);
cuser.sex = atoi(buf);
} while (cuser.sex < 0 || cuser.sex > 2);
cuser.userlevel = PERM_DEFAULT;
cuser.ufo = UFO_DEFAULT_NEW;
cuser.numlogins = 1;
cuser.tvalid = ap_start; /* itoc.030724: 拿上站時間當第一次認證碼的 seed */
sprintf(cuser.email, "%s.bbs@%s", cuser.userid, str_host); /* itoc.010902: 預設 email */
/* Ragnarok.050528: 可能二人同時申請同一個 ID,在此必須再檢查一次 */
usr_fpath(buf, userid, NULL);
if (dashd(buf))
{
vmsg("此代號剛被註冊走,請重新申請");
abort_bbs();
}
/* dispatch unique userno */
cuser.firstlogin = cuser.lastlogin = cuser.tcheck = slot.uptime = ap_start;
memcpy(slot.userid, userid, IDLEN);
fd = open(FN_SCHEMA, O_RDWR | O_CREAT, 0600);
{
/* flock(fd, LOCK_EX); */
/* Thor.981205: 用 fcntl 取代flock, POSIX標準用法 */
f_exlock(fd);
cuser.userno = try = uniq_userno(fd);
write(fd, &slot, sizeof(slot));
/* flock(fd, LOCK_UN); */
/* Thor.981205: 用 fcntl 取代flock, POSIX標準用法 */
f_unlock(fd);
}
close(fd);
/* create directory */
/* usr_fpath(buf, userid, NULL); */ /* 剛做過 */
mkdir(buf, 0700);
strcat(buf, "/@");
mkdir(buf, 0700);
usr_fpath(buf, userid, "gem"); /* itoc.010727: 個人精華區 */
/* mak_dirs(buf); */
mak_links(buf); /* itoc.010924: 減少個人精華區目錄 */
#ifdef MY_FAVORITE
usr_fpath(buf, userid, "MF");
mkdir(buf, 0700);
#endif
usr_fpath(buf, userid, fn_acct);
fd = open(buf, O_WRONLY | O_CREAT, 0600);
write(fd, &cuser, sizeof(ACCT));
close(fd);
/* Thor.990416: 注意: 怎麼會有 .ACCT長度是0的, 而且只有 @目錄, 持續觀察中 */
sprintf(buf, "%d", try);
blog("APPLY", buf);
}
#endif /* LOGINASNEW */
/* ----------------------------------------------------- */
/* bad login */
/* ----------------------------------------------------- */
#define FN_BADLOGIN "logins.bad"
static void
logattempt(type, content)
int type; /* '-' login failure ' ' success */
char *content;
{
char buf[128], fpath[64];
sprintf(buf, "%s %c %s\n", Btime(&ap_start), type, content);
usr_fpath(fpath, cuser.userid, FN_LOG);
f_cat(fpath, buf);
if (type != ' ')
{
usr_fpath(fpath, cuser.userid, FN_BADLOGIN);
sprintf(buf, "[%s] %s\n", Btime(&ap_start), fromhost);
f_cat(fpath, buf);
}
}
/* ----------------------------------------------------- */
/* 登錄 BBS 程式 */
/* ----------------------------------------------------- */
extern void talk_rqst();
extern void bmw_rqst();
#ifdef HAVE_WHERE
//static
int /* 1:在list中 0:不在list中 */
belong_list(filelist, key, desc)
char *filelist, *key, *desc;
{
FILE *fp;
char buf[80], *str;
int rc;
rc = 0;
if (fp = fopen(filelist, "r"))
{
while (fgets(buf, sizeof(buf), fp))
{
if (buf[0] == '#')
continue;
if (str = (char *) strchr(buf, ' '))
{
*str = '\0';
if (strstr(key, buf))
{
/* 跳過空白分隔 */
for (str++; *str && isspace(*str); str++)
;
strcpy(desc, str);
if (str = (char *) strchr(desc, '\n')) /* 最後的 '\n' 不要 */
*str = '\0';
rc = 1;
break;
}
}
}
fclose(fp);
}
return rc;
}
#endif
static void
utmp_setup(mode)
int mode;
{
UTMP utmp;
uschar *addr;
memset(&utmp, 0, sizeof(utmp));
utmp.pid = currpid;
utmp.userno = cuser.userno;
utmp.mode = bbsmode = mode;
/* utmp.in_addr = tn_addr; */ /* itoc.010112: 改變umtp.in_addr以使ulist_cmp_host正常 */
addr = (uschar *) &tn_addr;
utmp.in_addr = (addr[0] << 24) + (addr[1] << 16) + (addr[2] << 8) + addr[3];
utmp.userlevel = cuser.userlevel; /* itoc.010309: 把 userlevel 也放入 cache */
utmp.ufo = cuser.ufo;
utmp.status = 0;
strcpy(utmp.userid, cuser.userid);
strcpy(utmp.realid, cuser.userid);
#ifdef DETAIL_IDLETIME
utmp.idle_time = ap_start;
#endif
#ifdef GUEST_NICK
if (!cuser.userlevel) /* guest */
{
char nick[9][5] = {"遊子", "水滴", "訪客", "補帖", "豬頭", "影子", "病毒", "童年", "石像"};
sprintf(cuser.username, "太陽下的%s", nick[ap_start % 9]);
}
#endif /* GUEST_NICK */
strcpy(utmp.username, cuser.username);
#ifdef HAVE_WHERE
# ifdef GUEST_WHERE
if (!cuser.userlevel) /* guest */
{
/* itoc.010910: GUEST_NICK 和 GUEST_WHERE 的亂數模數避免一樣 */
char from[16][9] = {"風亭九思", "青埔朝陽", "率意通衢", "南台遠眺", "康莊迎曦", "碧草如茵", "緣慧潤生", "西庭笑語",
"玉樹向榮", "綠掩重樓", "松林立翠", "竹湖晨風", "竹園映亭", "曲道夾蔭", "荷塘月色", "思園春曉"};
strcpy(utmp.from, from[ap_start % 16]);
}
else
# endif /* GUEST_WHERE */
{
/* 像 hinet 這種 ip 很多, DN 很少的,就寫入 etc/fqdn *
* 像 140.112. 這種就寫在 etc/host *
* 即使 DNS 爛掉,在 etc/host 裡面的還是可以照樣判斷成功 *
* 如果把 140.112. 寫入 etc/host 中,就不用把 ntu.edu.tw *
* 重覆寫入 etc/fqdn 裡了 */
char name[256];
/* 先比對 ID */
str_lower(name, cuser.userid);
if (!belong_list(FN_ETC_IDHOME, name, utmp.from))
{
/* 再比對 FQDN */
str_lower(name, fromhost); /* itoc.011011: 大小寫均可,etc/fqdn 裡面都要寫小寫 */
if (!belong_list(FN_ETC_FQDN, name, utmp.from))
{
/* 再比對 ip */
sprintf(name, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
if (!belong_list(FN_ETC_HOST, name, utmp.from))
str_ncpy(utmp.from, fromhost, sizeof(utmp.from)); /* 如果都沒找到對應故鄉,就是用 fromhost */
}
}
}
#else
str_ncpy(utmp.from, fromhost, sizeof(utmp.from));
#endif /* HAVE_WHERE */
/* Thor: 告訴User已經滿了放不下... */
if (!utmp_new(&utmp))
login_abort("\n您剛剛選的位子已經被人捷足先登了,請下次再來吧");
/* itoc.001223: utmp_new 完再 pal_cache,如果 login_abort 就不做了 */
pal_cache();
}
/* ----------------------------------------------------- */
/* user login */
/* ----------------------------------------------------- */
static int /* 回傳 multi */
login_user(content)
char *content;
{
int attempts; /* 嘗試幾次錯誤 */
int multi;
char fpath[64], uid[IDLEN + 1];
#ifndef CHAT_SECURE
char passbuf[PSWDLEN + 1];
#endif
move(b_lines, 0);
time_t now = time(NULL);
if (now >= 1301587200 && now <= 1301673600) /* 愚人節計畫 */
{
outs("\033[1;44m ※ 本站不開放 \033[1;32m" STR_GUEST "\033[1;37m 參觀"
" 申請新帳號:\033[1;31m" STR_NEW "\033[1;44m \033[m");
}
else
{
outs(" ※ 本站不開放 \033[1;32m" STR_GUEST "\033[m 參觀"
" 申請新帳號:\033[1;31m" STR_NEW "\033[m");
}
attempts = 0;
multi = 0;
for (;;)
{
if (++attempts > LOGINATTEMPTS)
{
film_out(FILM_TRYOUT, 0);
login_abort("\n再見 ...");
}
time_t now = time(NULL);
if (now >= 1301587200 && now <= 1301673600) /* 愚人節 */
{
vget(b_lines - 2, 0, " [Login ID] ", uid, IDLEN + 1, DOECHO);
}
else
{
vget(b_lines - 2, 0, " [您的帳號] ", uid, IDLEN + 1, DOECHO);
}
if (!str_cmp(uid, STR_NEW))
{
#ifdef LOGINASNEW
# ifdef HAVE_GUARANTOR /* itoc.000319: 保證人制度 */
vget(b_lines - 2, 0, " [您的保人] ", uid, IDLEN + 1, DOECHO);
if (!*uid || (acct_load(&cuser, uid) < 0))
{
vmsg("抱歉,沒有介紹人不得加入本站");
}
else if (!HAS_PERM(PERM_GUARANTOR))
{
vmsg("抱歉,您不夠資格擔任別人的介紹人");
}
else if (!vget(b_lines - 2, 40, "[保人密碼] ", passbuf, PSWDLEN + 1, NOECHO))
{
continue;
}
else
{
if (chkpasswd(cuser.passwd, passbuf))
{
logattempt('-', content);
vmsg(ERR_PASSWD);
}
else
{
FILE *fp;
char parentid[IDLEN + 1], buf[80];
time_t now;
/* itoc.010820: 記錄保人於保證人及被保人 */
strcpy(parentid, cuser.userid);
acct_apply();
time(&now);
/* itoc.010820.註解: 把對方 log 在行首,在 reaper 時可以方便砍 tree */
sprintf(buf, "%s 於 %s 介紹此人(%s)加入本站\n", parentid, Btime(&now), cuser.userid);
usr_fpath(fpath, cuser.userid, "guarantor");
if (fp = fopen(fpath, "a"))
{
fputs(buf, fp);
fclose(fp);
}
sprintf(buf, "%s 於 %s 被此人(%s)介紹加入本站\n", cuser.userid, Btime(&now), parentid);
usr_fpath(fpath, parentid, "guarantor");
if (fp = fopen(fpath, "a"))
{
fputs(buf, fp);
fclose(fp);
}
break;
}
}
# else
acct_apply(); /* Thor.980917: 註解: setup cuser ok */
break;
# endif
#else
outs("\n本系統目前暫停線上註冊, 請用 " STR_GUEST " 進入");
continue;
#endif
}
else if (!*uid)
{
/* 若沒輸入 ID,那麼 continue */
}
else if (str_cmp(uid, STR_GUEST)) /* 一般使用者 */
{
if (now >= 1301587200 && now <= 1301673600) /* 愚人節 */
{
if (!vget(b_lines - 2, 40, "[驗證金鑰] ", passbuf, PSWDLEN + 1, NOECHO))
continue; /* 不打密碼則取消登入 */
}
else
{
if (!vget(b_lines - 2, 40, "[您的密碼] ", passbuf, PSWDLEN + 1, NOECHO))
continue; /* 不打密碼則取消登入 */
}
/* itoc.040110: 在輸入完 ID 及密碼,才載入 .ACCT */
if (acct_load(&cuser, uid) < 0)
{
vmsg(err_uid);
continue;
}
if (chkpasswd(cuser.passwd, passbuf))
{
logattempt('-', content);
vmsg(ERR_PASSWD);
}
else
{
if (!str_cmp(cuser.userid, str_sysop))
{
#ifdef SYSOP_SU
/* 簡單的 SU 功能 */
if (vans("變更使用者身分(Y/N)?[N] ") == 'y')
{
for (;;)
{
if (vget(b_lines - 2, 0, " [變更帳號] ", uid, IDLEN + 1, DOECHO) &&
acct_load(&cuser, uid) >= 0)
break;
vmsg(err_uid);
}
}
else
#endif
{
/* SYSOP gets all permission bits */
/* itoc.010902: DENY perm 排外 */
// cuser.userlevel = ~0 ^ (PERM_DENYMAIL | PERM_DENYTALK | PERM_DENYCHAT | PERM_DENYPOST | PERM_DENYLOGIN | PERM_PURGE);
}
}
if (cuser.ufo & UFO_ACL)
{
usr_fpath(fpath, cuser.userid, FN_ACL);
str_lower(fromhost, fromhost); /* lkchu.981201: 換小寫 */
if (!acl_has(fpath, "", fromhost))
{ /* Thor.980728: 注意 acl 檔中要全部小寫 */
logattempt('-', content);
login_abort("\n您的上站地點不太對勁,請核對 [上站地點設定檔]");
}
}
logattempt(' ', content);
/* check for multi-session */
if (!HAS_PERM(PERM_ALLADMIN))
{
UTMP *ui;
pid_t pid;
if (HAS_PERM(PERM_DENYLOGIN | PERM_PURGE))
login_abort("\n這個帳號暫停服務,詳情請向站長洽詢。");
if (!(ui = (UTMP *) utmp_find(cuser.userno)))
break; /* user isn't logged in */
pid = ui->pid;
if (pid && vans("您想踢掉其他重複的 login (Y/N)嗎?[Y] ") != 'n' && pid == ui->pid)
{
if ((kill(pid, SIGTERM) == -1) && (errno == ESRCH))
utmp_free(ui);
else
sleep(3); /* 被踢的人這時候正在自我了斷 */
blog("MULTI", cuser.userid);
}
if ((multi = utmp_count(cuser.userno, 0)) >= MULTI_MAX || /* 線上已有 MULTI_MAX 隻自己,禁止登入 */
(!multi && acct_load(&cuser, uid) < 0)) /* yiting.050101: 若剛已踢掉所有 multi-login,那麼重新讀取以套用變更 */
login_abort("\n再見 ...");
}
break;
}
}
else
{ /* guest */
vmsg("抱歉,本站不開放 " STR_GUEST " 參觀");
continue;
}
}
return multi;
}
static void
login_level()
{
int fd;
usint level;
ACCT tuser;
char fpath[64];
/* itoc.010804.註解: 有 PERM_VALID 者自動發給 PERM_POST PERM_PAGE PERM_CHAT */
level = cuser.userlevel | (PERM_ALLVALID ^ PERM_VALID);
if (!(level & PERM_ALLADMIN))
{
#ifdef JUSTIFY_PERIODICAL
if ((level & PERM_VALID) && (cuser.tvalid + VALID_PERIOD < ap_start))
{
level ^= PERM_VALID;
/* itoc.011116: 主動發信通知使用者,一直送信不知道會不會太耗空間 !? */
mail_self(FN_ETC_REREG, str_sysop, "您的認證已經過期,請重新認證", 0);
}
#endif
#ifdef NEWUSER_LIMIT
/* 即使已經通過認證,還是要見習三天 */
if (ap_start - cuser.firstlogin < 3 * 86400)
level &= ~PERM_POST;
#endif
/* itoc.000520: 未經身分認證, 禁止 post/chat/talk/write */
if (!(level & PERM_VALID))
level &= ~(PERM_POST | PERM_CHAT | PERM_PAGE);
if (level & PERM_DENYPOST)
level &= ~PERM_POST;
if (level & PERM_DENYTALK)
level &= ~PERM_PAGE;
if (level & PERM_DENYCHAT)
level &= ~PERM_CHAT;
if ((cuser.numemails >> 4) > (cuser.numlogins + cuser.numposts))
level |= PERM_DENYMAIL;
}
cuser.userlevel = level;
usr_fpath(fpath, cuser.userid, fn_acct);
if ((fd = open(fpath, O_RDWR)) >= 0)
{
if (read(fd, &tuser, sizeof(ACCT)) == sizeof(ACCT))
{
/* itoc.010805.註解: 這次的寫回 .ACCT 是為了讓別人 Query 線上使用者時
出現的上站時間/來源正確,以及回存正確的 userlvel */
tuser.userlevel = level;
tuser.lastlogin = ap_start;
strcpy(tuser.lasthost, cuser.lasthost);
lseek(fd, (off_t) 0, SEEK_SET);
write(fd, &tuser, sizeof(ACCT));
}
close(fd);
}
}
static void
login_status(multi)
int multi;
{
usint status;
char fpath[64];
struct tm *ptime;
status = 0;
/* itoc.010831: multi-login 的第二隻加上不可變動錢幣的旗標 */
if (multi)
status |= STATUS_COINLOCK;
/* itoc.011022: 加入生日旗標 */
ptime = localtime(&ap_start);
if (cuser.day == ptime->tm_mday && cuser.month == ptime->tm_mon + 1)
status |= STATUS_BIRTHDAY;
/* 朋友名單同步、清理過期信件 */
if (ap_start > cuser.tcheck + CHECK_PERIOD)
{
outz(MSG_CHKDATA);
refresh();
cuser.tcheck = ap_start;
usr_fpath(fpath, cuser.userid, fn_pal);
pal_sync(fpath);
#ifdef HAVE_ALOHA
usr_fpath(fpath, cuser.userid, FN_FRIENZ);
frienz_sync(fpath);
#endif
#ifdef OVERDUE_MAILDEL
status |= m_quota(); /* Thor.註解: 資料整理稽核有包含 BIFF check */
#endif
}
#ifdef OVERDUE_MAILDEL
else
#endif
status |= m_query(cuser.userid);
/* itoc.010924: 檢查個人精華區是否過多 */
#ifndef LINUX /* 在 Linux 下這檢查怪怪的 */
{
struct stat st;
usr_fpath(fpath, cuser.userid, "gem");
if (!stat(fpath, &st) && (st.st_size >= 512 * 7))
status |= STATUS_MGEMOVER;
}
#endif
cutmp->status |= status;
}
static void
login_other()
{
usint status;
char fpath[64];
/* 刪除錯誤登入記錄 */
usr_fpath(fpath, cuser.userid, FN_BADLOGIN);
if (more(fpath, (char *) -1) >= 0 && vans("以上為輸入密碼錯誤時的上站地點記錄,要刪除嗎(Y/N)?[Y] ") != 'n')
unlink(fpath);
if (!HAS_PERM(PERM_VALID))
film_out(FILM_NOTIFY, -1); /* 尚未認證通知 */
#ifdef JUSTIFY_PERIODICAL
else if (!HAS_PERM(PERM_ALLADMIN) && (cuser.tvalid + VALID_PERIOD - INVALID_NOTICE_PERIOD < ap_start))
film_out(FILM_REREG, -1); /* 有效時間逾期 10 天前提出警告 */
#endif
#ifdef NEWUSER_LIMIT
if (ap_start - cuser.firstlogin < 3 * 86400)
film_out(FILM_NEWUSER, -1); /* 即使已經通過認證,還是要見習三天 */
#endif
status = cutmp->status;
#ifdef OVERDUE_MAILDEL
if (status & STATUS_MQUOTA)
film_out(FILM_MQUOTA, -1); /* 過期信件即將清除警告 */
#endif
if (status & STATUS_MAILOVER)
film_out(FILM_MAILOVER, -1); /* 信件過多或寄信過多 */
//if (status & STATUS_MGEMOVER)
//film_out(FILM_MGEMOVER, -1); /* itoc.010924: 個人精華區過多警告 */
if (status & STATUS_BIRTHDAY)
film_out(FILM_BIRTHDAY, -1); /* itoc.010415: 生日當天上站有 special 歡迎畫面 */
ve_recover(); /* 上次斷線,編輯器回存 */
}
static void
tn_login()
{
int multi;
char buf[128];
bbsmode = M_LOGIN; /* itoc.020828: 以免過久未輸入時 igetch 會出現 movie */
/* --------------------------------------------------- */
/* 登錄系統 */
/* --------------------------------------------------- */
/* Thor.990415: 記錄ip, 怕正查不到 */
sprintf(buf, "%s ip:%08x (%d)", fromhost, tn_addr, currpid);
multi = login_user(buf);
blog("ENTER", buf);
/* --------------------------------------------------- */
/* 初始化 utmp、flag、mode、信箱 */
/* --------------------------------------------------- */
bbstate = STAT_STARTED; /* 進入系統以後才可以回水球 */
utmp_setup(M_LOGIN); /* Thor.980917: 註解: cutmp, cutmp-> setup ok */
total_user = ushm->count; /* itoc.011027: 未進使用者名單前,啟始化 total_user */
mbox_main();
#ifdef MODE_STAT
memset(&modelog, 0, sizeof(UMODELOG));
mode_lastchange = ap_start;
#endif
if (cuser.userlevel) /* not guest */
{
/* ------------------------------------------------- */
/* 核對 user level 並將 .ACCT 寫回 */
/* ------------------------------------------------- */
/* itoc.030929: 在 .ACCT 寫回以前,不可以有任何 vmsg(NULL) 或 more(xxxx, NULL)
等的東西,這樣如果 user 在 vmsg(NULL) 時回認證信,才不會被寫回的 cuser 蓋過 */
cuser.lastlogin = ap_start;
str_ncpy(cuser.lasthost, fromhost, sizeof(cuser.lasthost));
login_level();
/* ------------------------------------------------- */
/* 設定 status */
/* ------------------------------------------------- */
login_status(multi);
/* ------------------------------------------------- */
/* 秀些資訊 */
/* ------------------------------------------------- */
login_other();
}
srand(ap_start * cuser.userno * currpid);
}
static void
tn_motd()
{
usint ufo;
ufo = cuser.ufo;
if (!(ufo & UFO_MOTD))
{
more("gem/@/@-day", NULL); /* 今日熱門話題 */
pad_view();
}
time_t now = time(NULL);
if (now >= 1301587200 && now <= 1301673600)
more("gem/@/@FOOLDAYPOST", NULL); /* 愚人節 */
#ifdef HAVE_NOALOHA
if (!(ufo & UFO_NOALOHA))
#endif
{
#ifdef LOGIN_NOTIFY
loginNotify();
#endif
#ifdef HAVE_ALOHA
aloha();
#endif
}
#ifdef HAVE_FORCE_BOARD
brd_force(); /* itoc.000319: 強制閱讀公告板 */
#endif
}
/* ----------------------------------------------------- */
/* trap signals */
/* ----------------------------------------------------- */
static void
tn_signals()
{
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = (void *) abort_bbs;
sigaction(SIGBUS, &act, NULL);
sigaction(SIGSEGV, &act, NULL);
sigaction(SIGTERM, &act, NULL);
sigaction(SIGXCPU, &act, NULL);
#ifdef SIGSYS
/* Thor.981221: easy for porting */
sigaction(SIGSYS, &act, NULL);/* bad argument to system call */
#endif
act.sa_handler = (void *) talk_rqst;
sigaction(SIGUSR1, &act, NULL);
act.sa_handler = (void *) bmw_rqst;
sigaction(SIGUSR2, &act, NULL);
/* 在此借用 sigset_t act.sa_mask */
sigaddset(&act.sa_mask, SIGPIPE);
sigprocmask(SIG_BLOCK, &act.sa_mask, NULL);
}
static int
show_file(fpath, ln, lines)
char *fpath;
int ln, lines;
{
FILE *fp;
char buf[ANSILINELEN];
int i;
if ((fp = fopen(fpath, "r")))
{
i = lines;
while (fgets(buf, ANSILINELEN, fp) && i--)
outs(buf);
fclose(fp);
return 1;
}
sprintf(buf, "%s 遺失,請告知站長", fpath);
zmsg(buf);
return 0;
}
static inline void
tn_main()
{
clear();
#ifdef HAVE_LOGIN_DENIED
if (acl_has(BBS_ACLFILE, "", fromhost))
login_abort("\n貴機器於不被敝站接受");
#endif
/*
outs("系統維護中\n\n造成您的困擾深感抱歉<(_ _)>");
if (vkey() != 'm' || vkey() != 'p' || vkey() != '6' || vkey() != '0' || vkey() != '7')
login_abort("\n關站維修,再見");
*/
move(0, 0);
time(&ap_start);
time_t now = time(NULL);
if (now >= 1301587200 && now <= 1301673600)
{
prints("\033[1;36;44msony.tfcis.org\033[0;44m \033[1;32m→→→\033[0;44m \033[1;33m南一資訊\033[0;44m \033[1;32m←←←\033[0;44 \033[1;36m210.70.137.212\033[m\n");
prints("\033[1;44m 歡迎光臨【\033[33;46m 索尼小站 \033[37;44m】目前線上人數 [\033[33m%d\033[37m] 人\033[0;44m \033[m", ushm->count);
}
else
{
prints("%s ⊙ " SCHOOLNAME " ⊙ " MYIPADDR "\n"
"歡迎光臨【\033[1;33;46m %s \033[m】目前線上人數 [%d] 人",
str_host, str_site, ushm->count);
}
//film_out((ap_start % 3) + FILM_OPENING0, 3); /* 亂數顯示開頭畫面 */
char fpath[64];
time(&ap_start);
sprintf(fpath, "gem/@/@opening.%d", time(0) % 3);
prints("\n\n"); /* Chensc.070322: 修正進站畫面垂直位置 */
show_file(fpath, 3, 20);
currpid = getpid();
tn_signals(); /* Thor.980806: 放於 tn_login前, 以便 call in不會被踢 */
tn_login();
board_main();
gem_main();
#ifdef MY_FAVORITE
mf_main();
#endif
talk_main();
tn_motd();
menu();
abort_bbs(); /* to make sure it will terminate */
}
/* ----------------------------------------------------- */
/* FSA (finite state automata) for telnet protocol */
/* ----------------------------------------------------- */
static void
telnet_init()
{
static char svr[] =
{
IAC, DO, TELOPT_TTYPE,
IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE,
IAC, WILL, TELOPT_ECHO,
IAC, WILL, TELOPT_SGA
};
int n, len;
char *cmd;
int rset;
struct timeval to;
char buf[64];
/* --------------------------------------------------- */
/* init telnet protocol */
/* --------------------------------------------------- */
cmd = svr;
for (n = 0; n < 4; n++)
{
len = (n == 1 ? 6 : 3);
send(0, cmd, len, 0);
cmd += len;
rset = 1;
/* Thor.981221: for future reservation bug */
to.tv_sec = 1;
to.tv_usec = 1;
if (select(1, (fd_set *) & rset, NULL, NULL, &to) > 0)
recv(0, buf, sizeof(buf), 0);
}
}
/* ----------------------------------------------------- */
/* 支援超過 24 列的畫面 */
/* ----------------------------------------------------- */
static void
term_init()
{
#if 0 /* fuse.030518: 註解 */
server問:你會改變行列數嗎?(TN_NAWS, Negotiate About Window Size)
client答:Yes, I do. (TNCH_DO)
那麼在連線時,當TERM變化行列數時就會發出:
TNCH_IAC + TNCH_SB + TN_NAWS + 行數列數 + TNCH_IAC + TNCH_SE;
#endif
/* ask client to report it's term size */
static char svr[] = /* server */
{
IAC, DO, TELOPT_NAWS
};
int rset;
char buf[64], *rcv;
struct timeval to;
/* 問對方 (telnet client) 有沒有支援不同的螢幕寬高 */
send(0, svr, 3, 0);
rset = 1;
to.tv_sec = 1;
to.tv_usec = 1;
if (select(1, (fd_set *) & rset, NULL, NULL, &to) > 0)
recv(0, buf, sizeof(buf), 0);
rcv = NULL;
if ((uschar) buf[0] == IAC && buf[2] == TELOPT_NAWS)
{
/* gslin: Unix 的 telnet 對有無加 port 參數的行為不太一樣 */
if ((uschar) buf[1] == SB)
{
rcv = buf + 3;
}
else if ((uschar) buf[1] == WILL)
{
if ((uschar) buf[3] != IAC)
{
rset = 1;
to.tv_sec = 1;
to.tv_usec = 1;
if (select(1, (fd_set *) & rset, NULL, NULL, &to) > 0)
recv(0, buf + 3, sizeof(buf) - 3, 0);
}
if ((uschar) buf[3] == IAC && (uschar) buf[4] == SB && buf[5] == TELOPT_NAWS)
rcv = buf + 6;
}
}
if (rcv)
{
b_lines = ntohs(* (short *) (rcv + 2)) - 1;
b_cols = ntohs(* (short *) rcv) - 1;
/* b_lines 至少要 23,最多不能超過 T_LINES - 1 */
if (b_lines >= T_LINES)
b_lines = T_LINES - 1;
else if (b_lines < 23)
b_lines = 23;
/* b_cols 至少要 79,最多不能超過 T_COLS - 1 */
if (b_cols >= T_COLS)
b_cols = T_COLS - 1;
else if (b_cols < 79)
b_cols = 79;
}
else
{
b_lines = 23;
b_cols = 79;
}
d_cols = b_cols - 79;
}
/* ----------------------------------------------------- */
/* stand-alone daemon */
/* ----------------------------------------------------- */
static void
start_daemon(port)
int port; /* Thor.981206: 取 0 代表 *沒有參數* , -1 代表 -i (inetd) */
{
int n;
struct linger ld;
struct sockaddr_in sin;
#ifdef HAVE_RLIMIT
struct rlimit limit;
#endif
char buf[80], data[80];
time_t val;
/*
* More idiot speed-hacking --- the first time conversion makes the C
* library open the files containing the locale definition and time zone.
* If this hasn't happened in the parent process, it happens in the
* children, once per connection --- and it does add up.
*/
time(&val);
strftime(buf, 80, "%d/%b/%Y %H:%M:%S", localtime(&val));
#ifdef HAVE_RLIMIT
/* --------------------------------------------------- */
/* adjust resource : 16 mega is enough */
/* --------------------------------------------------- */
limit.rlim_cur = limit.rlim_max = 16 * 1024 * 1024;
/* setrlimit(RLIMIT_FSIZE, &limit); */
setrlimit(RLIMIT_DATA, &limit);
#ifdef SOLARIS
#define RLIMIT_RSS RLIMIT_AS /* Thor.981206: port for solaris 2.6 */
#endif
setrlimit(RLIMIT_RSS, &limit);
limit.rlim_cur = limit.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &limit);
limit.rlim_cur = limit.rlim_max = 60 * 20;
setrlimit(RLIMIT_CPU, &limit);
#endif
/* --------------------------------------------------- */
/* speed-hacking DNS resolve */
/* --------------------------------------------------- */
dns_init();
/* --------------------------------------------------- */
/* change directory to bbshome */
/* --------------------------------------------------- */
chdir(BBSHOME);
umask(077);
/* --------------------------------------------------- */
/* detach daemon process */
/* --------------------------------------------------- */
/* The integer file descriptors associated with the streams
stdin, stdout, and stderr are 0,1, and 2, respectively. */
close(1);
close(2);
if (port == -1) /* Thor.981206: inetd -i */
{
/* Give up root privileges: no way back from here */
setgid(BBSGID);
setuid(BBSUID);
#if 1
n = sizeof(sin);
if (getsockname(0, (struct sockaddr *) &sin, &n) >= 0)
port = ntohs(sin.sin_port);
#endif
/* mport = port; */ /* Thor.990325: 不需要了:P */
sprintf(data, "%d\t%s\t%d\tinetd -i\n", getpid(), buf, port);
f_cat(PID_FILE, data);
return;
}
close(0);
if (fork())
exit(0);
setsid();
if (fork())
exit(0);
/* --------------------------------------------------- */
/* fork daemon process */
/* --------------------------------------------------- */
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
if (port == 0) /* Thor.981206: port 0 代表沒有參數 */
{
n = MAX_BBSDPORT - 1;
while (n)
{
if (fork() == 0)
break;
sleep(1);
n--;
}
port = myports[n];
}
n = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
val = 1;
setsockopt(n, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val));
ld.l_onoff = ld.l_linger = 0;
setsockopt(n, SOL_SOCKET, SO_LINGER, (char *) &ld, sizeof(ld));
/* mport = port; */ /* Thor.990325: 不需要了:P */
sin.sin_port = htons(port);
if ((bind(n, (struct sockaddr *) &sin, sizeof(sin)) < 0) || (listen(n, QLEN) < 0))
exit(1);
/* --------------------------------------------------- */
/* Give up root privileges: no way back from here */
/* --------------------------------------------------- */
setgid(BBSGID);
setuid(BBSUID);
/* standalone */
sprintf(data, "%d\t%s\t%d\n", getpid(), buf, port);
f_cat(PID_FILE, data);
}
/* ----------------------------------------------------- */
/* reaper - clean up zombie children */
/* ----------------------------------------------------- */
static inline void
reaper()
{
while (waitpid(-1, NULL, WNOHANG | WUNTRACED) > 0);
}
#ifdef SERVER_USAGE
static void
servo_usage()
{
struct rusage ru;
FILE *fp;
fp = fopen("run/bbs.usage", "a");
if (!getrusage(RUSAGE_CHILDREN, &ru))
{
fprintf(fp, "\n[Server Usage] %d: %d\n\n"
"user time: %.6f\n"
"system time: %.6f\n"
"maximum resident set size: %lu P\n"
"integral resident set size: %lu\n"
"page faults not requiring physical I/O: %d\n"
"page faults requiring physical I/O: %d\n"
"swaps: %d\n"
"block input operations: %d\n"
"block output operations: %d\n"
"messages sent: %d\n"
"messages received: %d\n"
"signals received: %d\n"
"voluntary context switches: %d\n"
"involuntary context switches: %d\n\n",
getpid(), ap_start,
(double) ru.ru_utime.tv_sec + (double) ru.ru_utime.tv_usec / 1000000.0,
(double) ru.ru_stime.tv_sec + (double) ru.ru_stime.tv_usec / 1000000.0,
ru.ru_maxrss,
ru.ru_idrss,
ru.ru_minflt,
ru.ru_majflt,
ru.ru_nswap,
ru.ru_inblock,
ru.ru_oublock,
ru.ru_msgsnd,
ru.ru_msgrcv,
ru.ru_nsignals,
ru.ru_nvcsw,
ru.ru_nivcsw);
}
fclose(fp);
}
#endif
static void
main_term()
{
#ifdef SERVER_USAGE
servo_usage();
#endif
exit(0);
}
static inline void
main_signals()
{
struct sigaction act;
/* act.sa_mask = 0; */ /* Thor.981105: 標準用法 */
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = reaper;
sigaction(SIGCHLD, &act, NULL);
act.sa_handler = main_term;
sigaction(SIGTERM, &act, NULL);
#ifdef SERVER_USAGE
act.sa_handler = servo_usage;
sigaction(SIGPROF, &act, NULL);
#endif
/* sigblock(sigmask(SIGPIPE)); */
}
int
main(argc, argv)
int argc;
char *argv[];
{
int csock; /* socket for Master and Child */
int value;
int *totaluser;
struct sockaddr_in sin;
/* --------------------------------------------------- */
/* setup standalone daemon */
/* --------------------------------------------------- */
/* Thor.990325: usage, bbsd, or bbsd -i, or bbsd 1234 */
/* Thor.981206: 取 0 代表 *沒有參數*, -1 代表 -i */
start_daemon(argc > 1 ? strcmp("-i", argv[1]) ? atoi(argv[1]) : -1 : 0);
main_signals();
/* --------------------------------------------------- */
/* attach shared memory & semaphore */
/* --------------------------------------------------- */
#ifdef HAVE_SEM
sem_init();
#endif
ushm_init();
bshm_init();
fshm_init();
/* --------------------------------------------------- */
/* main loop */
/* --------------------------------------------------- */
totaluser = &ushm->count;
/* avgload = &ushm->avgload; */
for (;;)
{
value = 1;
if (select(1, (fd_set *) & value, NULL, NULL, NULL) < 0)
continue;
value = sizeof(sin);
csock = accept(0, (struct sockaddr *) &sin, &value);
if (csock < 0)
{
reaper();
continue;
}
ap_start++;
argc = *totaluser;
if (argc >= MAXACTIVE - 5 /* || *avgload > THRESHOLD */ )
{
/* 借用 currtitle */
sprintf(currtitle, "目前線上人數 [%d] 人,系統飽和,請稍後再來\n", argc);
send(csock, currtitle, strlen(currtitle), 0);
close(csock);
continue;
}
if (fork())
{
close(csock);
continue;
}
dup2(csock, 0);
close(csock);
/* ------------------------------------------------- */
/* ident remote host / user name via RFC931 */
/* ------------------------------------------------- */
tn_addr = sin.sin_addr.s_addr;
dns_name((char *) &sin.sin_addr, fromhost);
telnet_init();
term_init();
tn_main();
}
}
|