summaryrefslogtreecommitdiffstats
path: root/mbbsd/mbbsd.c
blob: 044927dc53e028956c9104fe9793a0dcc34bf5f1 (plain) (blame)
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
/* $Id$ */
#include "bbs.h"

#define SOCKET_QLEN 4
#define TH_LOW 100
#define TH_HIGH 120

static void do_aloha(char *hello);
static void getremotename(struct sockaddr_in * from, char *rhost, char *rname);

#if 0
static jmp_buf  byebye;
#endif

static char     remoteusername[40] = "?";

static unsigned char enter_uflag;
static int      use_shell_login_mode = 0;

static struct sockaddr_in xsin;

#ifdef USE_RFORK
#define fork() rfork(RFFDG | RFPROC | RFNOWAIT)
#endif

/* set signal handler, which won't be reset once signal comes */
static void
signal_restart(int signum, void (*handler) (int))
{
    struct sigaction act;
    act.sa_handler = handler;
    memset(&(act.sa_mask), 0, sizeof(sigset_t));
    act.sa_flags = 0;
    sigaction(signum, &act, NULL);
}

static void
start_daemon()
{
    int             n, fd;

    /*
     * 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_t          dummy = time(NULL);
    struct tm      *dummy_time = localtime(&dummy);
    char            buf[32];

    strftime(buf, sizeof(buf), "%d/%b/%Y:%H:%M:%S", dummy_time);

#ifndef NO_FORK
    if ((n = fork())) {
    exit(0);
    }
#endif

    /* rocker.011018: it's a good idea to close all unexcept fd!! */
    n = getdtablesize();
    while (n)
    close(--n);

    /* in2: open /dev/null to fd:2 */
    if( ((fd = open("/dev/null", O_WRONLY)) >= 0) && fd != 2 ){
    dup2(fd, 2);
    close(fd);
    }

    /*
     * rocker.011018: we don't need to remember original tty, so request a
     * new session id
     */
    setsid();

    /*
     * rocker.011018: after new session, we should insure the process is
     * clean daemon
     */
#ifndef NO_FORK
    if ((n = fork())) {
    exit(0);
    }
#endif

    if(getenv("SSH_CLIENT"))
      unsetenv("SSH_CLIENT");
}

static void
reapchild(int sig)
{
    int             state, pid;

    while ((pid = waitpid(-1, &state, WNOHANG | WUNTRACED)) > 0);
}

void
log_user(char *msg)
{
    char            filename[200], buf[200];

    snprintf(filename, sizeof(filename), BBSHOME "/home/%c/%s/USERLOG",
         cuser.userid[0], cuser.userid);
    snprintf(buf, sizeof(buf), "%s\n", msg);
    log_file(filename, msg, 1);
}


void
log_usies(char *mode, char *mesg)
{
    char            genbuf[200];

    if (!mesg)
    snprintf(genbuf, sizeof(genbuf),
         cuser.userid[0] ? "%s %s %-12s Stay:%d (%s)\n" :
         "%s %s %s Stay:%d (%s)\n",
         Cdate(&now), mode, cuser.userid,
         (int)(now - login_start_time) / 60, cuser.username);
    else
    snprintf(genbuf, sizeof(genbuf),
         cuser.userid[0] ? "%s %s %-12s %s\n" : "%s %s %s%s\n",
         Cdate(&now), mode, cuser.userid, mesg);
    log_file(FN_USIES, genbuf, 1);

    /* 追蹤使用者 */
    if (HAS_PERM(PERM_LOGUSER))
    log_user(genbuf);
}

static void
setflags(int mask, int value)
{
    if (value)
    cuser.uflag |= mask;
    else
    cuser.uflag &= ~mask;
}

void
u_exit(char *mode)
{
    //userec_t xuser;
    int             diff = (time(0) - login_start_time) / 60;

    /* close fd 0 & 1 to terminate network */
    close(0);
    close(1);

    reload_money();
    auto_backup();
    setflags(PAGER_FLAG, currutmp->pager != 1);
    setflags(CLOAK_FLAG, currutmp->invisible);
    save_brdbuf();

#ifdef ASSESS
    cuser.goodpost = currutmp->goodpost;
    cuser.badpost = currutmp->badpost;
    cuser.goodsale = currutmp->goodsale;
    cuser.badsale = currutmp->badsale;
#endif

    cuser.invisible = currutmp->invisible;
    cuser.pager = currutmp->pager;
    memcpy(cuser.mind, currutmp->mind, 4);
    setutmpbid(0);
    if (!(HAS_PERM(PERM_SYSOP) && HAS_PERM(PERM_DENYPOST)) &&
    !currutmp->invisible)
    do_aloha("<<下站通知>> -- 我走囉!");

    purge_utmp(currutmp);
    if ((cuser.uflag != enter_uflag) || (currmode & MODE_DIRTY) || diff) {
    if (!diff && cuser.numlogins)
        cuser.numlogins = --cuser.numlogins;
    /* Leeym 上站停留時間限制式 */
    }
    passwd_update(usernum, &cuser);
    log_usies(mode, NULL);
}

void
system_abort()
{
    if (currmode)
    u_exit("ABORT");

    clear();
    refresh();
    fprintf(stdout, "謝謝光臨, 記得常來喔 !\n");
    exit(0);
}

void
abort_bbs(int sig)
{
    if (currmode)
    u_exit("AXXED");
    exit(0);
}

static void
abort_bbs_debug(int sig)
{
#ifdef DEBUGSLEEP
    static int      reentrant = 0;

    if (!reentrant) {
    int             i;
    /* close all file descriptors (including the network connection) */
    for (i = 0; i < 256; ++i)
        close(i);
    reentrant = 1;
    if (currmode)
        u_exit("AXXED");
    setproctitle("debug me!(%d)(%s,%d)", sig, cuser.userid, currstat);
    sleep(3600);        /* wait 60 mins for debug */
    }
#endif
    exit(0);
}

/* 登錄 BBS 程式 */
static void
mysrand()
{
    srand(time(NULL) + getpid());   /* 時間跟 pid 當 rand 的 seed */
}

int
dosearchuser(char *userid)
{
    if ((usernum = getuser(userid)))
    memcpy(&cuser, &xuser, sizeof(cuser));
    else
    memset(&cuser, 0, sizeof(cuser));
    return usernum;
}

void
talk_request(int sig)
{
    bell();
    bell();
    if (currutmp->msgcount) {
    char            timebuf[100];
    time_t          now = time(0);

    move(0, 0);
    clrtoeol();
    prints("\033[33;41m★%s\033[34;47m [%s] %s \033[0m",
         SHM->uinfo[currutmp->destuip].userid, my_ctime(&now,timebuf,sizeof(timebuf)),
         (currutmp->sig == 2) ? "重要消息廣播!(請Ctrl-U,l查看熱訊記錄)"
         : "呼叫、呼叫,聽到請回答");
    refresh();
    } else {
    unsigned char   mode0 = currutmp->mode;
    char            c0 = currutmp->chatid[0];
    screenline_t   *screen0 = calloc(t_lines, sizeof(screenline_t));

    currutmp->mode = 0;
    currutmp->chatid[0] = 1;
    memcpy(screen0, big_picture, t_lines * sizeof(screenline_t));
    talkreply();
    currutmp->mode = mode0;
    currutmp->chatid[0] = c0;
    memcpy(big_picture, screen0, t_lines * sizeof(screenline_t));
    free(screen0);
    redoscr();
    }
}

void
show_call_in(int save, int which)
{
    char            buf[200];
    snprintf(buf, sizeof(buf), "\033[1;33;46m★%s\033[37;45m %s \033[m",
         currutmp->msgs[which].userid, currutmp->msgs[which].last_call_in);
    move(b_lines, 0);
    clrtoeol();
    refresh();
    outmsg(buf);

    if (save) {
    char            genbuf[200];
    time_t          now;
    if (!fp_writelog) {
        sethomefile(genbuf, cuser.userid, fn_writelog);
        fp_writelog = fopen(genbuf, "a");
    }
    if (fp_writelog) {
        time(&now);
        fprintf(fp_writelog, "%s [%s]\n", buf, Cdatelite(&now));
    }
    }
}

static int
add_history_water(water_t * w, msgque_t * msg)
{
    memcpy(&w->msg[w->top], msg, sizeof(msgque_t));
    w->top++;
    w->top %= WATERMODE(WATER_OFO) ? 5 : MAX_REVIEW;

    if (w->count < MAX_REVIEW)
    w->count++;

    return w->count;
}

static int
add_history(msgque_t * msg)
{
    int             i = 0, j, waterinit = 0;
    water_t        *tmp;
    if (WATERMODE(WATER_ORIG) || WATERMODE(WATER_NEW))
    add_history_water(&water[0], msg);
    if (WATERMODE(WATER_NEW) || WATERMODE(WATER_OFO)) {
    for (i = 0; i < 5 && swater[i]; i++)
        if (swater[i]->pid == msg->pid)
        break;
    if (i == 5) {
        waterinit = 1;
        i = 4;
        memset(swater[4], 0, sizeof(water_t));
    } else if (!swater[i]) {
        water_usies = i + 1;
        swater[i] = &water[i + 1];
        waterinit = 1;
    }
    tmp = swater[i];

    if (waterinit) {
        memcpy(swater[i]->userid, msg->userid, sizeof(swater[i]->userid));
        swater[i]->pid = msg->pid;
    }
    if (!swater[i]->uin)
        swater[i]->uin = currutmp;

    for (j = i; j > 0; j--)
        swater[j] = swater[j - 1];
    swater[0] = tmp;
    add_history_water(swater[0], msg);
    }
    if (WATERMODE(WATER_ORIG) || WATERMODE(WATER_NEW)) {
    if (watermode > 0 &&
        (water_which == swater[0] || water_which == &water[0])) {
        if (watermode < water_which->count)
        watermode++;
        t_display_new();
    }
    }
    return i;
}

void
write_request(int sig)
{
    int             i;

    if (WATERMODE(WATER_OFO)) {
    /*
     * sig = SIGUSR2 waterball come in 0       flush to water[]  (by
     * my_write2())
     */
    if (sig != 0) {
        if (wmofo == 0) /* 正在回水球 */
        wmofo = 1;
        bell();
        show_call_in(1, currutmp->msgcount - 1);
        refresh();
    }
    if (sig == 0 ||     /* 回水球的時候又有水球進來, 回完後一次寫回去  */
        wmofo == -1) {  /* 不在回水球模式                              */
        do {
        add_history(&currutmp->msgs[0]);
        if (currutmp->msgcount--)
            for (i = 0; i < currutmp->msgcount; i++)
            currutmp->msgs[i] = currutmp->msgs[i + 1];
        }
        while (currutmp->msgcount);
        currutmp->msgcount = 0;
    }
    } else {
    if (currutmp->mode != 0 &&
        currutmp->pager != 0 &&
        cuser.userlevel != 0 &&
        currutmp->msgcount != 0 &&
        currutmp->mode != TALK &&
        currutmp->mode != EDITING &&
        currutmp->mode != CHATING &&
        currutmp->mode != PAGE &&
        currutmp->mode != IDLE &&
        currutmp->mode != MAILALL && currutmp->mode != MONITOR) {
        char            c0 = currutmp->chatid[0];
        int             currstat0 = currstat;
        unsigned char   mode0 = currutmp->mode;

        currutmp->mode = 0;
        currutmp->chatid[0] = 2;
        currstat = HIT;

        do {
        bell();
        show_call_in(1, 0);
        igetch();
        currutmp->msgcount--;
        if (currutmp->msgcount >= MAX_MSGS) {
            /* this causes chaos... jochang */
            raise(SIGFPE);
        }
        add_history(&currutmp->msgs[0]);
        for (i = 0; i < currutmp->msgcount; i++)
            currutmp->msgs[i] = currutmp->msgs[i + 1];
        }
        while (currutmp->msgcount);
        currutmp->chatid[0] = c0;
        currutmp->mode = mode0;
        currstat = currstat0;
    } else {
        bell();
        show_call_in(1, 0);
        add_history(&currutmp->msgs[0]);

        refresh();
        currutmp->msgcount = 0;
    }
    }
}

static void
multi_user_check()
{
    register userinfo_t *ui;
    register pid_t  pid;
    char            genbuf[3];

    if (HAS_PERM(PERM_SYSOP))
    return;         /* don't check sysops */

    if (cuser.userlevel) {
    if (!(ui = (userinfo_t *) search_ulist(usernum)))
        return;     /* user isn't logged in */

    pid = ui->pid;
    if (!pid /* || (kill(pid, 0) == -1) */ )
        return;     /* stale entry in utmp file */

    getdata(b_lines - 1, 0, "您想刪除其他重複的 login (Y/N)嗎?[Y] ",
        genbuf, 3, LCECHO);

    if (genbuf[0] != 'n') {
        if (pid > 0)
        kill(pid, SIGHUP);
        log_usies("KICK ", cuser.username);
    } else {
        if (search_ulistn(usernum, 3) != NULL)
        system_abort(); /* Goodbye(); */
    }
    } else {
    /* allow multiple guest user */
    if (search_ulistn(usernum, 100) != NULL) {
        outs("\n抱歉,目前已有太多 guest 在站上, 請用new註冊。\n");
        pressanykey();
        oflush();
        exit(1);
    }
    }
}

/* bad login */
static char     str_badlogin[] = "logins.bad";

static void
logattempt(char *uid, char type)
{
    char            fname[40];
    int             fd, len;
    char            genbuf[200];

    snprintf(genbuf, sizeof(genbuf), "%c%-12s[%s] %s@%s\n", type, uid,
        Cdate(&login_start_time), remoteusername, fromhost);
    len = strlen(genbuf);
    if ((fd = open(str_badlogin, O_WRONLY | O_CREAT | O_APPEND, 0644)) > 0) {
    write(fd, genbuf, len);
    close(fd);
    }
    if (type == '-') {
    snprintf(genbuf, sizeof(genbuf),
         "[%s] %s\n", Cdate(&login_start_time), fromhost);
    len = strlen(genbuf);
    sethomefile(fname, uid, str_badlogin);
    if ((fd = open(fname, O_WRONLY | O_CREAT | O_APPEND, 0644)) > 0) {
        write(fd, genbuf, len);
        close(fd);
    }
    }
}

inline static void mkuserdir(char *userid)
{
    char genbuf[200];
    sethomepath(genbuf, userid);
    // assume it is a dir, so just check if it is exist
    if (access(genbuf, F_OK) != 0)
    mkdir(genbuf, 0755);
}

static void
login_query()
{
#ifdef GB_CONVERT
    /* uid 加一位, for gb login */
    char            uid[IDLEN + 2], passbuf[PASSLEN];
    int             attempts, len;
#else
    char            uid[IDLEN + 1], passbuf[PASSLEN];
    int             attempts;
#endif
    resolve_garbage();
    now = time(0);

#ifdef DEBUG
    move(1, 0);
    prints("debugging mode\ncurrent pid: %d\n", getpid());
#else
    show_file("etc/Welcome", 1, -1, NO_RELOAD);
#endif
    output("1", 1);

    /* hint */

    attempts = 0;
    while (1) {
    if (attempts++ >= LOGINATTEMPTS) {
        more("etc/goodbye", NA);
        pressanykey();
        exit(1);
    }
#ifdef DEBUG
    move(19, 0);
    prints("current pid: %d ", getpid());
#endif
    getdata(20, 0, "請輸入代號,或以[guest]參觀,以[new]註冊:",
        uid, sizeof(uid), DOECHO);

#ifdef GB_CONVERT
    /* switch to gb mode if uid end with '.' */
    len = strlen(uid);
    if (uid[0] && uid[len - 1] == '.') {
        set_converting_type(1);
        uid[len - 1] = 0;
    }
    else if (len >= IDLEN + 1)
        uid[IDLEN] = 0;
#endif

    if (strcasecmp(uid, str_new) == 0) {
#ifdef LOGINASNEW
        new_register();
        mkuserdir(cuser.userid);
        break;
#else
        outs("本系統目前無法以 new 註冊, 請用 guest 進入\n");
        continue;
#endif
    } else if (uid[0] == '\0' || !dosearchuser(uid)) {
        outs(err_uid);
    } else if (strcmp(uid, STR_GUEST)) {
        getdata(21, 0, MSG_PASSWD,
            passbuf, sizeof(passbuf), NOECHO);
        passbuf[8] = '\0';

        if (!checkpasswd(cuser.passwd, passbuf)
         /* || (HAS_PERM(PERM_SYSOP) && !use_shell_login_mode) */ ) {
        logattempt(cuser.userid, '-');
        outs(ERR_PASSWD);
        } else {
        logattempt(cuser.userid, ' ');
        if (strcasecmp("SYSOP", cuser.userid) == 0){
            cuser.userlevel = PERM_BASIC | PERM_CHAT | PERM_PAGE |
            PERM_POST | PERM_LOGINOK | PERM_MAILLIMIT |
            PERM_CLOAK | PERM_SEECLOAK | PERM_XEMPT |
            PERM_DENYPOST | PERM_BM | PERM_ACCOUNTS |
            PERM_CHATROOM | PERM_BOARD | PERM_SYSOP | PERM_BBSADM;
            mkuserdir(cuser.userid);
        }
        break;
        }
    } else {        /* guest */
        cuser.userlevel = 0;
        cuser.uflag = COLOR_FLAG | PAGER_FLAG | BRDSORT_FLAG | MOVIE_FLAG;
        mkuserdir(cuser.userid);
        break;
    }
    }
    multi_user_check();
}

void
add_distinct(char *fname, char *line)
{
    FILE           *fp;
    int             n = 0;

    if ((fp = fopen(fname, "a+"))) {
    char            buffer[80];
    char            tmpname[100];
    FILE           *fptmp;

    strlcpy(tmpname, fname, sizeof(tmpname));
    strcat(tmpname, "_tmp");
    if (!(fptmp = fopen(tmpname, "w"))) {
        fclose(fp);
        return;
    }
    rewind(fp);
    while (fgets(buffer, 80, fp)) {
        char           *p = buffer + strlen(buffer) - 1;

        if (p[-1] == '\n' || p[-1] == '\r')
        p[-1] = 0;
        if (!strcmp(buffer, line))
        break;
        sscanf(buffer + strlen(buffer) + 2, "%d", &n);
        fprintf(fptmp, "%s%c#%d\n", buffer, 0, n);
    }

    if (feof(fp))
        fprintf(fptmp, "%s%c#1\n", line, 0);
    else {
        sscanf(buffer + strlen(buffer) + 2, "%d", &n);
        fprintf(fptmp, "%s%c#%d\n", buffer, 0, n + 1);
        while (fgets(buffer, 80, fp)) {
        sscanf(buffer + strlen(buffer) + 2, "%d", &n);
        fprintf(fptmp, "%s%c#%d\n", buffer, 0, n);
        }
    }
    fclose(fp);
    fclose(fptmp);
    unlink(fname);
    rename(tmpname, fname);
    }
}

void
del_distinct(char *fname, char *line)
{
    FILE           *fp;
    int             n = 0;

    if ((fp = fopen(fname, "r"))) {
    char            buffer[80];
    char            tmpname[100];
    FILE           *fptmp;

    strlcpy(tmpname, fname, sizeof(tmpname));
    strcat(tmpname, "_tmp");
    if (!(fptmp = fopen(tmpname, "w"))) {
        fclose(fp);
        return;
    }
    rewind(fp);
    while (fgets(buffer, 80, fp)) {
        char           *p = buffer + strlen(buffer) - 1;

        if (p[-1] == '\n' || p[-1] == '\r')
        p[-1] = 0;
        if (!strcmp(buffer, line))
        break;
        sscanf(buffer + strlen(buffer) + 2, "%d", &n);
        fprintf(fptmp, "%s%c#%d\n", buffer, 0, n);
    }

    if (!feof(fp))
        while (fgets(buffer, 80, fp)) {
        sscanf(buffer + strlen(buffer) + 2, "%d", &n);
        fprintf(fptmp, "%s%c#%d\n", buffer, 0, n);
        }
    fclose(fp);
    fclose(fptmp);
    unlink(fname);
    rename(tmpname, fname);
    }
}

#ifdef WHERE
static int
where(char *from)
{
    register int    i = 0, count = 0, j;

    for (j = 0; j < SHM->top; j++) {
    char           *token = strtok(SHM->domain[j], "&");

    i = 0;
    count = 0;
    while (token) {
        if (strstr(from, token))
        count++;
        token = strtok(NULL, "&");
        i++;
    }
    if (i == count)
        break;
    }
    if (i != count)
    return 0;
    return j;
}
#endif

static void
check_BM(void)
{
    /* XXX: -_- */
    int             i;

    cuser.userlevel &= ~PERM_BM;
    for( i = 0 ; i < numboards ; ++i )
    if( is_BM_cache(i + 1) ) /* XXXbid */
        return;
    //for (i = 0, bhdr = bcache; i < numboards && !is_BM(bhdr->BM); i++, bhdr++);
}

static void
setup_utmp(int mode)
{
    userinfo_t      uinfo;
    memset(&uinfo, 0, sizeof(uinfo));
    uinfo.pid = currpid = getpid();
    uinfo.uid = usernum;
    uinfo.mode = currstat = mode;
    uinfo.mailalert = load_mailalert(cuser.userid);

#ifdef ASSESS
    uinfo.goodpost = cuser.goodpost;
    uinfo.badpost = cuser.badpost;
    uinfo.goodsale = cuser.goodsale;
    uinfo.badsale = cuser.badsale;
#endif

    uinfo.userlevel = cuser.userlevel;
    uinfo.sex = cuser.sex % 8;
    uinfo.lastact = time(NULL);
    strlcpy(uinfo.userid, cuser.userid, sizeof(uinfo.userid));
    //strlcpy(uinfo.realname, cuser.realname, sizeof(uinfo.realname));
    strlcpy(uinfo.username, cuser.username, sizeof(uinfo.username));
    strlcpy(uinfo.from, fromhost, sizeof(uinfo.from));
    uinfo.five_win = cuser.five_win;
    uinfo.five_lose = cuser.five_lose;
    uinfo.five_tie = cuser.five_tie;
    uinfo.chc_win = cuser.chc_win;
    uinfo.chc_lose = cuser.chc_lose;
    uinfo.chc_tie = cuser.chc_tie;
    uinfo.invisible = cuser.invisible % 2;
    uinfo.pager = cuser.pager % 5;
    memcpy(uinfo.mind, cuser.mind, 4);
#ifdef WHERE
    uinfo.from_alias = where(fromhost);
#endif
#ifndef FAST_LOGIN
    setuserfile(buf, "remoteuser");

    strlcpy(remotebuf, fromhost, sizeof(fromhost));
    strcat(remotebuf, ctime(&now));
    remotebuf[strlen(remotebuf) - 1] = 0;
    add_distinct(buf, remotebuf);
#endif
    if (enter_uflag & CLOAK_FLAG)
    uinfo.invisible = YEA;
    getnewutmpent(&uinfo);
    SHM->UTMPneedsort = 1;
    if (!(cuser.numlogins % 20) && cuser.userlevel & PERM_BM)
    check_BM();     /* Ptt 自動取下離職板主權力 */
#ifndef _BBS_UTIL_C_
    friend_load(0);
    nice(3);
#endif
}

inline static void welcome_msg(void) {
    prints("\033[m      歡迎您第 \033[1;33m%d\033[0;37m 度拜訪本站,"
        "上次您是從 \033[1;33m%s\033[0;37m 連往本站,\n"
        "     我記得那天是 \033[1;33m%s\033[0;37m。\n",
        ++cuser.numlogins, cuser.lasthost, Cdate(&cuser.lastlogin));
    pressanykey();
}

inline static void check_bad_login(void) {
    char            genbuf[200];
    setuserfile(genbuf, str_badlogin);
    if (more(genbuf, NA) != -1) {
    move(b_lines - 3, 0);
    prints("通常並沒有辦法知道該ip是誰所有, "
        "以及其意圖(是不小心按錯或有意測您密碼)\n"
        "若您有帳號被盜用疑慮, 請經常更改您的密碼或使用加密連線");
    if (getans("您要刪除以上錯誤嘗試的記錄嗎(Y/N)?[Y]") != 'n')
        unlink(genbuf);
    }
}

inline static void birthday_make_a_wish(struct tm *ptime, struct tm *tmp){
    if (currutmp->birth && tmp->tm_mday != ptime->tm_mday) {
    more("etc/birth.post", YEA);
    brc_initial("WhoAmI");
    set_board();
    do_post();
    }
}

inline static void record_lasthost(char *fromhost, int len){
    strncpy(cuser.lasthost, fromhost, len);
    cuser.lasthost[len - 1] = '\0';
}

inline static void check_mailbox_and_read(void){
    if (chkmailbox())
    m_read();
}

static void init_guest_info(void)
{
    int i;
    char           *nick[13] = {
    "椰子", "貝殼", "內衣", "寶特瓶", "翻車魚",
    "樹葉", "浮萍", "鞋子", "潛水艇", "魔王",
    "鐵罐", "考卷", "大美女"
    };
    char           *name[13] = {
    "大王椰子", "鸚鵡螺", "比基尼", "可口可樂", "仰泳的魚",
    "憶", "高岡屋", "AIR Jordon", "紅色十月號", "批踢踢",
    "SASAYA椰奶", "鴨蛋", "布魯克鱈魚香絲"
    };
    char           *addr[13] = {
    "天堂樂園", "大海", "綠島小夜曲", "美國", "綠色珊瑚礁",
    "遠方", "原本海", "NIKE", "蘇聯", "男八618室",
    "愛之味", "天上", "藍色珊瑚礁"
    };
    i = login_start_time % 13;
    snprintf(cuser.username, sizeof(cuser.username),
        "海邊漂來的%s", nick[(int)i]);
    strlcpy(currutmp->username, cuser.username,
        sizeof(currutmp->username));
    strlcpy(cuser.realname, name[(int)i], sizeof(cuser.realname));
    //strlcpy(currutmp->realname, cuser.realname, sizeof(currutmp->realname));
    strlcpy(cuser.address, addr[(int)i], sizeof(cuser.address));
    cuser.sex = i % 8;
    currutmp->pager = 2;
}

#ifdef FOREIGN_REG
inline static void foreign_warning(void){
    if ((cuser.uflag2 & FOREIGN) && !(cuser.uflag2 & LIVERIGHT)){
    if (login_start_time - cuser.firstlogin > (FOREIGN_REG_DAY - 5) * 24 * 3600){
        mail_muser(cuser, "[出入境管理局]", "etc/foreign_expired_warn");
    }
    else if (login_start_time - cuser.firstlogin > FOREIGN_REG_DAY * 24 * 3600){
        cuser.userlevel &= ~(PERM_LOGINOK | PERM_POST);
        vmsg("警告:請至出入境管理局申請永久居留");
    }
    }
}
#endif

static void
user_login()
{
    char            i;
    struct tm      *ptime, *tmp;
    time_t          now;
    int             a, ifbirth;

    /* get local time */
    time(&now);
    ptime = localtime(&now);
    
    /* 初始化: random number 增加user跟時間的差異 */
    mysrand();

    /* show welcome_login */
    ifbirth = (ptime->tm_mday == cuser.day &&
           ptime->tm_mon + 1 == cuser.month);
    if (ifbirth)
    more("etc/Welcome_birth", NA);
    else {
#ifndef MULTI_WELCOME_LOGIN
    more("etc/Welcome_login", NA);
#else
    if( SHM->GV2.e.nWelcomes ){
        char            buf[80];
        snprintf(buf, sizeof(buf), "etc/Welcome_login.%d",
             (int)login_start_time % SHM->GV2.e.nWelcomes);
        more(buf, NA);
    }
#endif
    }

    log_usies("ENTER", fromhost);
    setproctitle("%s: %s", margs, cuser.userid);
    resolve_fcache();
    resolve_boards();
    memset(&water[0], 0, sizeof(water_t) * 6);
    strlcpy(water[0].userid, " 全部 ", sizeof(water[0].userid));

    if(getenv("SSH_CLIENT") != NULL){
    char frombuf[50];
    sscanf(getenv("SSH_CLIENT"), "%s", frombuf);
    xsin.sin_family = AF_INET;
    xsin.sin_port = htons(23);
    inet_pton(AF_INET, frombuf, &xsin.sin_addr);
    getremotename(&xsin, fromhost, remoteusername);   /* FC931 */
    }

    /* 初始化 uinfo、flag、mode */
    setup_utmp(LOGIN);
    currmode = MODE_STARTED;
    enter_uflag = cuser.uflag;
    currutmp->birth = ifbirth;

    tmp = localtime(&cuser.lastlogin);
    if ((a = SHM->UTMPnumber) > SHM->max_user) {
    SHM->max_user = a;
    SHM->max_time = now;
    }
    init_brdbuf();
    brc_initial(DEFAULT_BOARD);
    set_board();

    if (!(HAS_PERM(PERM_SYSOP) && HAS_PERM(PERM_DENYPOST)) &&
    !currutmp->invisible)
    do_aloha("<<上站通知>> -- 我來啦!");

    if (cuser.userlevel) {  /* not guest */
    move(t_lines - 4, 0);
    welcome_msg();

    birthday_make_a_wish(ptime, tmp);
    check_bad_login();
    check_mailbox_and_read();
    check_register();
    record_lasthost(fromhost, 16);
    restore_backup();
    } else if (!strcmp(cuser.userid, STR_GUEST)) {
    init_guest_info();
    pressanykey();
    } else {
    pressanykey();
    check_mailbox_and_read();
    }

    if (!PERM_HIDE(currutmp))
    cuser.lastlogin = login_start_time;

#ifdef FOREIGN_REG
    foreign_warning();
#endif
    passwd_update(usernum, &cuser);

    for (i = 0; i < NUMVIEWFILE; i++)
    if ((cuser.loginview >> i) & 1)
        more(loginview_file[(int)i][0], YEA);
}

static void
do_aloha(char *hello)
{
    FILE           *fp;
    char            userid[80];
    char            genbuf[200];

    setuserfile(genbuf, "aloha");
    if ((fp = fopen(genbuf, "r"))) {
    snprintf(genbuf, sizeof(genbuf), hello);
    while (fgets(userid, 80, fp)) {
        userinfo_t     *uentp;
        if ((uentp = (userinfo_t *) search_ulist_userid(userid)) && 
            isvisible(uentp, currutmp)) {
        my_write(uentp->pid, genbuf, uentp->userid, 2, NULL);
        }
    }
    fclose(fp);
    }
}

static void
do_term_init()
{
    term_init();
    initscr();
    if(use_shell_login_mode)
    raise(SIGWINCH);
}

inline static void
start_client()
{
#ifdef CPULIMIT
    struct rlimit   rml;
    rml.rlim_cur = CPULIMIT * 60;
    rml.rlim_max = CPULIMIT * 60;
    setrlimit(RLIMIT_CPU, &rml);
#endif

    /* system init */
    nice(2);            /* Ptt: lower priority */
    login_start_time = time(0);
    currmode = 0;

    signal(SIGHUP, abort_bbs);
    signal(SIGTERM, abort_bbs);
    signal(SIGPIPE, abort_bbs);

    signal(SIGINT, abort_bbs_debug);
    signal(SIGQUIT, abort_bbs_debug);
    signal(SIGILL, abort_bbs_debug);
    signal(SIGABRT, abort_bbs_debug);
    signal(SIGFPE, abort_bbs_debug);
    signal(SIGBUS, abort_bbs_debug);
    signal(SIGSEGV, abort_bbs_debug);

    signal_restart(SIGUSR1, talk_request);
    signal_restart(SIGUSR2, write_request);

    dup2(0, 1);

    /* initialize passwd semaphores */
    if (passwd_init())
    exit(1);

    do_term_init();
    signal(SIGALRM, abort_bbs);
    alarm(600);

    login_query();      /* Ptt 加上login time out */
    m_init();           /* init the user mail path */
    user_login();

    if (now - SHM->close_vote_time > 86400)
    //改為一天一次
    {
    b_closepolls();
    SHM->close_vote_time = now;
    }
    if (!(cuser.uflag & COLOR_FLAG))
    showansi = 0;
    signal(SIGALRM, SIG_IGN);

    domenu(MMENU, "主功\能表", (currutmp->mailalert ? 'M' : 'C'), cmdlist);
}

/* 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
    };
    char           *cmd;
    int             n, len;
    struct timeval  to;
    char            buf[64];
    fd_set          ReadSet, r;
    
    FD_ZERO(&ReadSet);
    FD_SET(0, &ReadSet);
    for (n = 0, cmd = svr; n < 4; n++) {
    len = (n == 1 ? 6 : 3);
    write(0, cmd, len);
    cmd += len;
    to.tv_sec = 3;
    to.tv_usec = 0;
    r = ReadSet;
    if (select(1, &r, NULL, NULL, &to) > 0)
        recv(0, buf, sizeof(buf), 0);
    }
}

/* 取得 remote user name 以判定身份                */
/*
 * rfc931() speaks a common subset of the RFC 931, AUTH, TAP, IDENT and RFC
 * 1413 protocols. It queries an RFC 931 etc. compatible daemon on a remote
 * host to look up the owner of a connection. The information should not be
 * used for authentication purposes. This routine intercepts alarm signals.
 * 
 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
 */

#define STRN_CPY(d,s,l) { strncpy((d),(s),(l)); (d)[(l)-1] = 0; }
#define RFC931_TIMEOUT   10
#define RFC931_PORT     113 /* Semi-well-known port */
#define ANY_PORT        0   /* Any old port will do */

#if 0
/* timeout - handle timeouts */
static void
timeout(int sig)
{
    longjmp(byebye, sig);
}
#endif

static void
getremotename(struct sockaddr_in * from, char *rhost, char *rname)
{

    /* get remote host name */

#ifdef FAST_LOGIN
    strcpy(rhost, (char *)inet_ntoa(from->sin_addr));
#else
    struct sockaddr_in our_sin;
    struct sockaddr_in rmt_sin;
    unsigned        rmt_port, rmt_pt;
    unsigned        our_port, our_pt;
    FILE           *fp;
    char            buffer[512], user[80], *cp;
    int             s;
    static struct hostent *hp;


    hp = NULL;
    if (setjmp(byebye) == 0) {
    signal(SIGALRM, timeout);
    alarm(3);
    hp = gethostbyaddr((char *)&from->sin_addr, sizeof(struct in_addr),
               from->sin_family);
    alarm(0);
    }
    strcpy(rhost, hp ? hp->h_name : (char *)inet_ntoa(from->sin_addr));

    /*
     * Use one unbuffered stdio stream for writing to and for reading from
     * the RFC931 etc. server. This is done because of a bug in the SunOS
     * 4.1.x stdio library. The bug may live in other stdio implementations,
     * too. When we use a single, buffered, bidirectional stdio stream ("r+"
     * or "w+" mode) we read our own output. Such behaviour would make sense
     * with resources that support random-access operations, but not with
     * sockets.
     */

    s = sizeof(our_sin);
    if (getsockname(0, (struct sockaddr *) & our_sin, &s) < 0)
    return;

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    return;

    if (!(fp = fdopen(s, "r+"))) {
    close(s);
    return;
    }
    /* Set up a timer so we won't get stuck while waiting for the server. */
    if (setjmp(byebye) == 0) {
    signal(SIGALRM, timeout);
    alarm(RFC931_TIMEOUT);

    /*
     * Bind the local and remote ends of the query socket to the same IP
     * addresses as the connection under investigation. We go through all
     * this trouble because the local or remote system might have more
     * than one network address. The RFC931 etc. client sends only port
     * numbers; the server takes the IP addresses from the query socket.
     */
    our_pt = ntohs(our_sin.sin_port);
    our_sin.sin_port = htons(ANY_PORT);

    rmt_sin = *from;
    rmt_pt = ntohs(rmt_sin.sin_port);
    rmt_sin.sin_port = htons(RFC931_PORT);

    setbuf(fp, (char *)0);
    s = fileno(fp);

    if (bind(s, (struct sockaddr *) & our_sin, sizeof(our_sin)) >= 0 &&
      connect(s, (struct sockaddr *) & rmt_sin, sizeof(rmt_sin)) >= 0) {
        /*
         * Send query to server. Neglect the risk that a 13-byte write
         * would have to be fragmented by the local system and cause
         * trouble with buggy System V stdio libraries.
         */
        fprintf(fp, "%u,%u\r\n", rmt_pt, our_pt);
        fflush(fp);
        /*
         * Read response from server. Use fgets()/sscanf() so we can work
         * around System V stdio libraries that incorrectly assume EOF
         * when a read from a socket returns less than requested.
         */
        if (fgets(buffer, sizeof(buffer), fp) && !ferror(fp)
        && !feof(fp)
        && sscanf(buffer, "%u , %u : USERID :%*[^:]:%79s", &rmt_port,
              &our_port, user) == 3 && rmt_pt == rmt_port
        && our_pt == our_port) {

        /*
         * Strip trailing carriage return. It is part of the
         * protocol, not part of the data.
         */
        if ((cp = (char *)strchr(user, '\r')))
            *cp = 0;
        strlcpy(rname, user, sizeof(user));
        }
    }
    alarm(0);
    }
    fclose(fp);
#endif
}

static int
bind_port(int port)
{
    int             sock, on;

    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    on = 1;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
    setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof(on));

    on = 0;
    setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&on, sizeof(on));

    xsin.sin_port = htons(port);
    if (bind(sock, (struct sockaddr *) & xsin, sizeof xsin) < 0) {
    syslog(LOG_INFO, "bbsd bind_port can't bind to %d", port);
    exit(1);
    }
    if (listen(sock, SOCKET_QLEN) < 0) {
    syslog(LOG_INFO, "bbsd bind_port can't listen to %d", port);
    exit(1);
    }
    return sock;
}


/*******************************************************/


static int      shell_login(int argc, char *argv[], char *envp[]);
static int      daemon_login(int argc, char *argv[], char *envp[]);
static int      check_ban_and_load(int fd);

int
main(int argc, char *argv[], char *envp[])
{
    start_time = time(NULL);

    /* avoid SIGPIPE */
    signal(SIGPIPE, SIG_IGN);

    /* avoid erroneous signal from other mbbsd */
    signal(SIGUSR1, SIG_IGN);
    signal(SIGUSR2, SIG_IGN);

    attach_SHM();
    if( (argc == 3 && shell_login(argc, argv, envp)) ||
    (argc != 3 && daemon_login(argc, argv, envp)) )
    start_client();

    return 0;
}

static int
shell_login(int argc, char *argv[], char *envp[])
{

    /* Give up root privileges: no way back from here */
    setgid(BBSGID);
    setuid(BBSUID);
    chdir(BBSHOME);


    use_shell_login_mode = 1;
    initsetproctitle(argc, argv, envp);

    /*
     * copy fromindent: Standard input:1138: Error:Unexpected end of file the
     * original "bbs"
     */
    if (argc > 1) {
    strcpy(fromhost, argv[1]);
    if (argc > 3)
        strlcpy(remoteusername, argv[3], sizeof(remoteusername));
    }
    close(2);
    /* don't close fd 1, at least init_tty need it */

    init_tty();
    if (check_ban_and_load(0)) {
    return 0;
    }
    return 1;
}

static int
daemon_login(int argc, char *argv[], char *envp[])
{
    int             msock, csock;   /* socket for Master and Child */
    FILE           *fp;
    int             listen_port = 23;
    int             len_of_sock_addr;
    char            buf[256];

    /* setup standalone */

    start_daemon();

    signal_restart(SIGCHLD, reapchild);

    /* choose port */
    if (argc == 1)
    listen_port = 3006;
    else if (argc >= 2)
    listen_port = atoi(argv[1]);

    snprintf(margs, sizeof(margs), "%s %d ", argv[0], listen_port);

    /* port binding */
    xsin.sin_family = AF_INET;
    msock = bind_port(listen_port);
    if (msock < 0) {
    syslog(LOG_INFO, "mbbsd bind_port failed.\n");
    exit(1);
    }
    initsetproctitle(argc, argv, envp);
    setproctitle("%s: listening ", margs);

    /* Give up root privileges: no way back from here */
    setgid(BBSGID);
    setuid(BBSUID);
    chdir(BBSHOME);

#ifndef NO_FORK
#ifdef PRE_FORK
    if( listen_port == 23 ){ // only pre-fork in port 23
    int     i;
    for( i = 0 ; i < PRE_FORK ; ++i )
        if( fork() <= 0 )
        break;
    }
#endif
#endif

    snprintf(buf, sizeof(buf), "run/mbbsd.%d.%d.pid", listen_port, getpid());
    if ((fp = fopen(buf, "w"))) {
    fprintf(fp, "%d\n", getpid());
    fclose(fp);
    }

    /* main loop */
    for (;;) {
    len_of_sock_addr = sizeof(xsin);
    csock = accept(msock, (struct sockaddr *) & xsin, (socklen_t *) & len_of_sock_addr);

    if (csock < 0) {
        if (errno != EINTR)
        sleep(1);
        continue;
    }
    if (check_ban_and_load(csock)) {
        close(csock);
        continue;
    }
#ifdef NO_FORK
    break;
#else
    if (fork() == 0)
        break;
    else
        close(csock);
#endif

    }
    /* here is only child running */

    setproctitle("%s: ...login wait... ", margs);
    close(msock);
    dup2(csock, 0);
    close(csock);

    getremotename(&xsin, fromhost, remoteusername);
    telnet_init();
    return 1;
}

/*
 * check if we're banning login and if the load is too high. if login is
 * permitted, return 0; else return -1; approriate message is output to fd.
 */
static int
check_ban_and_load(int fd)
{
    FILE           *fp;
    static time_t   chkload_time = 0;
    static int      overload = 0;   /* overload or banned, update every 1
                     * sec  */
    static int      banned = 0;

#ifdef INSCREEN
    write(fd, INSCREEN, sizeof(INSCREEN));
#else
#define BANNER \
"【" BBSNAME "】◎ 台大流行網 ◎(" MYHOSTNAME ") 調幅(" MYIP ") \r\n"
    write(fd, BANNER, sizeof(BANNER));
#endif

    if ((time(0) - chkload_time) > 1) {
    overload = 0;
    banned = 0;

    if(cpuload(NULL) > MAX_CPULOAD)
        overload = 1;
    else if (SHM->UTMPnumber >= MAX_ACTIVE
#ifdef DYMAX_ACTIVE
        || (SHM->GV2.e.dymaxactive > 2000 &&
            SHM->UTMPnumber >= SHM->GV2.e.dymaxactive)
#endif
        ) {
        ++SHM->GV2.e.toomanyusers;
        overload = 2;
    } else if(!access(BBSHOME "/" BAN_FILE, R_OK))
        banned = 1;

    chkload_time = time(0);
    }

    if(overload == 1)
    write(fd, "系統過載, 請稍後再來\r\n", 22);
    else if(overload == 2)
    write(fd, "由於人數過多,請您稍後再來。", 28);
    else if (banned && (fp = fopen(BBSHOME "/" BAN_FILE, "r"))) {
    char     buf[256];
    while (fgets(buf, sizeof(buf), fp))
        write(fd, buf, strlen(buf));
    fclose(fp);
    }

    if (banned || overload)
    return -1;

    return 0;
}