/
mbbsd.c
2218 lines (1893 loc) · 53.4 KB
/
mbbsd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "bbs.h"
#include "daemons.h"
#include <sys/wait.h>
#include <netinet/tcp.h>
#ifdef __linux__
# ifdef CRITICAL_MEMORY
# include <malloc.h>
# endif
# ifdef DEBUG
# include <mcheck.h>
# endif
#endif
#define SOCKET_QLEN 4
void do_aloha(const char *hello);
static void getremotename(const struct in_addr from, char *rhost);
//////////////////////////////////////////////////////////////////
// Site Optimization
// override these macro if you need more optimization,
// based on OS/lib/package...
#ifndef OPTIMIZE_SOCKET
#define OPTIMIZE_SOCKET(sock) do {} while (0)
#endif
#ifndef XAUTH_HOST
#define XAUTH_HOST(x) x
#endif
#ifndef XAUTH_GETREMOTENAME
#define XAUTH_GETREMOTENAME(x) x
#endif
static int listen_port;
static int is_secure_connection;
#define MAX_BINDPORT 20
enum TermMode {
TermMode_TELNET,
TermMode_TTY,
};
struct ProgramOption {
bool daemon_mode;
bool tunnel_mode;
enum TermMode term_mode;
int term_width, term_height;
int nport;
int port[MAX_BINDPORT];
int flag_listenfd;
char* flag_tunnel_path;
bool flag_fork;
bool flag_checkload;
char flag_user[IDLEN+1];
};
static void
free_program_option(struct ProgramOption *opt)
{
if (!opt)
return;
free(opt->flag_tunnel_path);
free(opt);
}
#ifdef DETECT_CLIENT
Fnv32_t client_code=FNV1_32_INIT;
void UpdateClientCode(unsigned char c)
{
FNV1A_CHAR(c, client_code);
}
void LogClientCode()
{
int fd = OpenCreate("log/client_code",O_WRONLY | O_APPEND);
if(fd>=0) {
write(fd, &client_code, sizeof(client_code));
close(fd);
}
}
#endif
#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(struct ProgramOption *option)
{
#ifndef VALGRIND
int n, fd;
#endif
/*
* 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;
char buf[32];
localtime_r(&dummy, &dummy_time);
strftime(buf, sizeof(buf), "%d/%b/%Y:%H:%M:%S", &dummy_time);
if (option->flag_fork) {
if (fork()) {
exit(0);
}
}
/* rocker.011018: it's a good idea to close all unexcept fd!! */
#ifndef VALGRIND
n = getdtablesize();
while (n)
close(--n);
if( ((fd = OpenCreate("log/stderr", O_WRONLY | O_APPEND)) >= 0) && fd != 2 ){
dup2(fd, 2);
close(fd);
}
#endif
if(getenv("SSH_CLIENT"))
unsetenv("SSH_CLIENT");
if(getenv("SSH_CONNECTION"))
unsetenv("SSH_CONNECTION");
/*
* 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
*/
if (option->flag_fork) {
if (fork()) {
exit(0);
}
}
}
static void
reapchild(int sig GCC_UNUSED)
{
int state, pid;
while ((pid = waitpid(-1, &state, WNOHANG | WUNTRACED)) > 0);
}
void
log_usies(const char *mode, const char *mesg)
{
now = time(NULL);
if (!mesg)
log_filef(FN_USIES, LOG_CREAT,
"%s %s %-12s Stay:%d\n",
Cdate(&now), mode, cuser.userid ,
(int)(now - login_start_time) / 60);
else
log_filef(FN_USIES, LOG_CREAT,
"%s %s %-12s %s\n",
Cdate(&now), mode, cuser.userid, mesg);
/* 追蹤使用者 */
if (HasUserPerm(PERM_LOGUSER))
log_user("logout");
}
void
u_exit(const char *mode)
{
currmode = 0;
/* close fd 0 & 1 to terminate network */
close(0);
close(1);
// verify if utmp is valid. only flush data if utmp is correct.
assert(strncmp(currutmp->userid,cuser.userid, IDLEN)==0);
if(strncmp(currutmp->userid, cuser.userid, IDLEN)!=0)
return;
auto_backup();
save_brdbuf();
brc_finalize();
// XXX TOTO for guests, skip the save process?
pwcuExitSave();
setutmpbid(0);
purge_utmp(currutmp);
log_usies(mode, NULL);
}
void
abort_bbs(int sig GCC_UNUSED)
{
/* ignore normal signals */
Signal(SIGALRM, SIG_IGN);
Signal(SIGUSR1, SIG_IGN);
Signal(SIGUSR2, SIG_IGN);
Signal(SIGHUP, SIG_IGN);
Signal(SIGTERM, SIG_IGN);
Signal(SIGPIPE, SIG_IGN);
if (currmode) {
STATINC(STAT_MBBSD_ABORTED);
u_exit("ABORTED");
}
exit(0);
}
#ifdef GCC_NORETURN
static void abort_bbs_debug(int sig) GCC_NORETURN;
#endif
/* NOTE: It's better to use signal-safe functions. Avoid to call
* functions with global/static variable -- data may be corrupted */
static void
abort_bbs_debug(int sig)
{
int i;
sigset_t sigset;
switch(sig) {
case SIGINT: STATINC(STAT_SIGINT); break;
case SIGQUIT: STATINC(STAT_SIGQUIT); break;
case SIGILL: STATINC(STAT_SIGILL); break;
case SIGABRT: STATINC(STAT_SIGABRT); break;
case SIGFPE: STATINC(STAT_SIGFPE); break;
case SIGBUS: STATINC(STAT_SIGBUS); break;
case SIGSEGV: STATINC(STAT_SIGSEGV); break;
case SIGXCPU: STATINC(STAT_SIGXCPU); break;
}
/* ignore normal signals */
Signal(SIGALRM, SIG_IGN);
Signal(SIGUSR1, SIG_IGN);
Signal(SIGUSR2, SIG_IGN);
Signal(SIGHUP, SIG_IGN);
Signal(SIGTERM, SIG_IGN);
Signal(SIGPIPE, SIG_IGN);
Signal(SIGSEGV, SIG_DFL);
/* unblock */
sigemptyset(&sigset);
sigaddset(&sigset, SIGINT);
sigaddset(&sigset, SIGQUIT);
sigaddset(&sigset, SIGILL);
sigaddset(&sigset, SIGABRT);
sigaddset(&sigset, SIGFPE);
sigaddset(&sigset, SIGBUS);
sigaddset(&sigset, SIGSEGV);
sigaddset(&sigset, SIGXCPU);
sigprocmask(SIG_UNBLOCK, &sigset, NULL);
fprintf(stderr, "%d %d %d %.12s\n", (int)time4(NULL), getpid(), sig, cuser.userid);
#define CRASH_MSG ANSI_RESET \
"\r\n程式異常, 立刻斷線. \r\n" \
"請洽 " BN_BUGREPORT " 板詳述問題發生經過。\r\n"
#define XCPU_MSG ANSI_RESET \
"\r\n程式耗用過多計算資源, 立刻斷線。\r\n" \
"可能是 (a)執行太多耗用資源的動作 或 (b)程式掉入無窮迴圈. "\
"請洽 " BN_BUGREPORT " 板詳述問題發生經過。\r\n"
if(sig==SIGXCPU)
write(1, XCPU_MSG, sizeof(XCPU_MSG));
else
write(1, CRASH_MSG, sizeof(CRASH_MSG));
sleep(5);
/* close all file descriptors (including the network connection) */
for (i = 0; i < 256; ++i)
close(i);
/* log */
/* assume vsnprintf() in log_file() is signal-safe, is it? */
log_filef("log/crash.log", LOG_CREAT,
"%d %d %d %.12s\n", (int)time4(NULL), getpid(), sig, cuser.userid);
/* try logout... not a good idea, maybe crash again. now disabled */
/*
if (currmode) {
currmode = 0;
u_exit("AXXED");
}
*/
#ifdef DEBUGSLEEP
#ifndef VALGRIND
setproctitle("debug me!(%d)(%s,%d)", sig, cuser.userid, currstat);
#endif
/* do this manually to prevent broken stuff */
/* will broken currutmp cause problems here? hope not... */
if(currutmp && strncmp(cuser.userid, currutmp->userid, IDLEN) == EQUSTR)
currutmp->mode = DEBUGSLEEPING;
sleep(DEBUGSLEEP_SECONDS);
#endif
exit(0);
}
#ifdef CPULIMIT_PER_DAY
static void
signal_xcpu_handler(int sig)
{
static time_t last_time_exceeded = 0;
struct rlimit rml;
int margin_for_handler = 5;
bool give_more_time = true;
if (last_time_exceeded == 0)
last_time_exceeded = login_start_time;
assert(last_time_exceeded);
// 不用 (time(0) - login_start_time) 來平均, 避免用好幾天之後突然狂吃 cpu 的狀況.
if (time(0) - last_time_exceeded < DAY_SECONDS)
give_more_time = false;
last_time_exceeded = time(0);
getrlimit(RLIMIT_CPU, &rml);
// reach hard limit
if (rml.rlim_cur + CPULIMIT_PER_DAY + margin_for_handler > rml.rlim_max)
give_more_time = false;
if (!give_more_time) {
abort_bbs_debug(sig);
assert(0); // shout not reach here
}
rml.rlim_cur += CPULIMIT_PER_DAY;
setrlimit(RLIMIT_CPU, &rml);
}
#endif
/* 登錄 BBS 程式 */
static void
mysrand(void)
{
unsigned int seed;
must_getrandom(&seed, sizeof(seed));
seed ^= getpid();
srandom(seed);
}
void
talk_request(int sig GCC_UNUSED)
{
STATINC(STAT_TALKREQUEST);
bell();
bell();
if (currutmp->msgcount) {
syncnow();
move(0, 0);
clrtoeol();
prints(ANSI_COLOR(33;41) "★%s" ANSI_COLOR(34;47) " [%s] %s " ANSI_RESET,
SHM->uinfo[currutmp->destuip].userid, Cdatelite(&now),
(currutmp->sig == 2) ? "重要消息廣播!(請Ctrl-U,l查看熱訊記錄)"
: "呼叫、呼叫,聽到請回答");
refresh();
} else {
unsigned char mode0 = currutmp->mode;
char c0 = currutmp->chatid[0];
screen_backup_t old_screen;
currutmp->mode = 0;
currutmp->chatid[0] = 1;
scr_dump(&old_screen);
talkreply();
currutmp->mode = mode0;
currutmp->chatid[0] = c0;
scr_restore(&old_screen);
}
}
void
show_call_in(int save, int which)
{
char buf[200];
int mode = currutmp->msgs[which].msgmode;
#ifdef PLAY_ANGEL
if (mode == MSGMODE_TOANGEL) {
snprintf(buf, sizeof(buf), ANSI_COLOR(1;37;46) "★%s" ANSI_COLOR(37;45)
" %s " ANSI_RESET,
currutmp->msgs[which].userid,
currutmp->msgs[which].last_call_in);
// I must be an Angel. Let's try to update angel beats info.
// TODO maybe it's better to move this to "sender".
angel_notify_activity(currutmp->msgs[which].userid);
} else
#endif
snprintf(buf, sizeof(buf), ANSI_COLOR(1;33;46) "★%s" ANSI_COLOR(37;45)
" %s " ANSI_RESET, currutmp->msgs[which].userid,
currutmp->msgs[which].last_call_in);
outmsg(buf);
if (save && mode != MSGMODE_ALOHA) {
char genbuf[200];
if (!fp_writelog) {
sethomefile(genbuf, cuser.userid, fn_writelog);
fp_writelog = fopen(genbuf, "a");
}
if (fp_writelog) {
fprintf(fp_writelog, "%s [%s]\n", buf, Cdatelite(&now));
}
}
}
static int
add_history_water(water_t * w, const msgque_t * msg)
{
memcpy(&w->msg[w->top], msg, sizeof(msgque_t));
w->top++;
w->top %= PAGER_UI_IS(PAGER_UI_OFO) ? 5 : MAX_REVIEW;
if (w->count < MAX_REVIEW)
w->count++;
return w->count;
}
static int
add_history(const msgque_t * msg)
{
int i = 0, j, waterinit = 0;
water_t *tmp;
check_water_init();
if (PAGER_UI_IS(PAGER_UI_ORIG) || PAGER_UI_IS(PAGER_UI_NEW))
add_history_water(&water[0], msg);
if (PAGER_UI_IS(PAGER_UI_NEW) || PAGER_UI_IS(PAGER_UI_OFO)) {
for (i = 0; i < WB_OFO_USER_NUM; i++) {
if (swater[i] == NULL)
break;
if (swater[i]->pid == msg->pid
#ifdef PLAY_ANGEL
&& swater[i]->msg[0].msgmode == msg->msgmode
/* When throwing waterball to angel directly */
#endif
)
break;
}
if (i == WB_OFO_USER_NUM) {
waterinit = 1;
i = WB_OFO_USER_NUM - 1;
memset(swater[i], 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 (PAGER_UI_IS(PAGER_UI_ORIG) || PAGER_UI_IS(PAGER_UI_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, msgcount;
STATINC(STAT_WRITEREQUEST);
#ifdef NOKILLWATERBALL
if( reentrant_write_request ) /* kill again by shmctl */
return;
reentrant_write_request = 1;
#endif
syncnow();
check_water_init();
if (PAGER_UI_IS(PAGER_UI_OFO)) {
/* 如果目前正在回水球模式的話, 就不能進行 add_history() ,
因為會改寫 water[], 而使回水球目的爛掉, 所以分成幾種情況考慮.
sig != 0表真的有水球進來, 故顯示.
sig == 0表示沒有水球進來, 不過之前尚有水球還沒寫到 water[].
*/
static int alreadyshow = 0;
if( sig ){ /* 真的有水球進來 */
/* 若原來正在 REPLYING , 則改成 RECVINREPLYING,
這樣在回水球結束後, 會再呼叫一次 write_request(0) */
if( wmofo == REPLYING )
wmofo = RECVINREPLYING;
/* 顯示 */
for( ; alreadyshow < currutmp->msgcount && alreadyshow < MAX_MSGS
; ++alreadyshow ){
bell();
show_call_in(1, alreadyshow);
refresh();
}
}
/* 看看是不是要把 currutmp->msg 拿回 water[] (by add_history())
須要是不在回水球中 (NOTREPLYING) */
if( wmofo == NOTREPLYING &&
(msgcount = currutmp->msgcount) > 0 ){
for( i = 0 ; i < msgcount ; ++i )
add_history(&currutmp->msgs[i]);
if( (currutmp->msgcount -= msgcount) < 0 )
currutmp->msgcount = 0;
alreadyshow = 0;
}
} else {
if (currutmp->mode != 0 &&
currutmp->pager != PAGER_OFF &&
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;
#ifdef NOKILLWATERBALL
currutmp->wbtime = 0;
#endif
if( (msgcount = currutmp->msgcount) > 0 ){
for( i = 0 ; i < msgcount ; ++i ){
bell();
show_call_in(1, 0);
add_history(&currutmp->msgs[0]);
if( (--currutmp->msgcount) < 0 )
i = msgcount; /* force to exit for() */
else if( currutmp->msgcount > 0 )
memmove(&currutmp->msgs[0],
&currutmp->msgs[1],
sizeof(msgque_t) * currutmp->msgcount);
vkey();
}
}
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;
}
}
#ifdef NOKILLWATERBALL
reentrant_write_request = 0;
currutmp->wbtime = 0; /* race */
#endif
}
static userinfo_t*
getotherlogin(int num)
{
userinfo_t *ui;
do {
if (!(ui = (userinfo_t *) search_ulistn(usernum, num)))
return NULL; /* user isn't logged in */
/* skip sleeping process, this is slow if lots */
if(ui->mode == DEBUGSLEEPING)
num++;
else if(ui->pid <= 0)
num++;
else if(kill(ui->pid, 0) < 0)
num++;
else
break;
} while (1);
return ui;
}
static void
multi_user_check(void)
{
register userinfo_t *ui;
char genbuf[3];
if (HasUserPerm(PERM_SYSOP))
return; /* don't check sysops */
// race condition here, sleep may help..?
if (cuser.userlevel) {
usleep(random()%1000000); // 0~1s
ui = getotherlogin(1);
if(ui == NULL)
return;
move(b_lines-3, 0); clrtobot();
outs("\n" ANSI_COLOR(1) "注意: 您有其它連線已登入此帳號。" ANSI_RESET);
getdata(b_lines - 1, 0, "您想刪除其他重複登入的連線嗎?[Y/n] ",
genbuf, 3, LCECHO);
usleep(random()%5000000); // 0~5s
if (genbuf[0] != 'n') {
do {
// scan again, old ui may be invalid
ui = getotherlogin(1);
if(ui==NULL)
return;
if (ui->pid > 0) {
if(kill(ui->pid, SIGHUP)<0) {
perror("kill SIGHUP fail");
break;
}
log_usies("KICK ", cuser.nickname);
} else {
fprintf(stderr, "id=%s ui->pid=0\n", cuser.userid);
}
usleep(random()%2000000+1000000); // 1~3s
} while(getotherlogin(3) != NULL);
} else {
/* deny login if still have 3 */
if (getotherlogin(3) != NULL) {
sleep(1);
abort_bbs(0); /* Goodbye(); */
}
}
} else {
/* allow multiple guest user */
if (search_ulistn(usernum, MAX_GUEST) != NULL) {
sleep(1);
vmsg("抱歉,目前已有太多 guest 在站上, 請用new註冊。");
exit(1);
}
}
}
void
mkuserdir(const char *userid)
{
char genbuf[PATHLEN];
sethomepath(genbuf, userid);
// assume it is a dir, so just check if it is exist
if (access(genbuf, F_OK) != 0)
Mkdir(genbuf);
}
static int
load_current_user(const char *uid)
{
// userid should be already normalized.
int is_admin_only = 0;
#ifdef ADMIN_PORT
// ----------------------------------------------------- PORT TESTING
// XXX currently this does not work if we're using tunnel.
is_admin_only = (listen_port == ADMIN_PORT);
#endif
// ----------------------------------------------------- NEW ACCOUNT
#ifdef STR_REGNEW
if (!is_admin_only && strcasecmp(uid, STR_REGNEW) == 0) {
# ifndef LOGINASNEW
assert(false);
exit(0);
# endif // !LOGINASNEW
new_register();
clear();
mkuserdir(cuser.userid);
reginit_fav();
} else
#endif
// ----------------------------------------------------- RECOVER ACCOUNT
#ifdef STR_RECOVER
if (!is_admin_only && strcasecmp(uid, STR_RECOVER) == 0) {
// Allow more time due to sending emails for verification code.
alarm(1800);
recover_account();
// Should have exited.
assert(false);
exit(1);
} else
#endif
// --------------------------------------------------- GUEST ACCOUNT
#ifdef STR_GUEST
if (!is_admin_only && strcasecmp(uid, STR_GUEST) == 0) {
if (initcuser(STR_GUEST)< 1) exit (0) ;
pwcuInitGuestPerm();
// can we prevent mkuserdir() here?
mkuserdir(cuser.userid);
} else
#endif
// ---------------------------------------------------- USER ACCOUNT
{
if (!cuser.userid[0] && initcuser(uid) < 1)
exit(0);
if (is_admin_only) {
if (!HasUserPerm(PERM_SYSOP | PERM_BBSADM | PERM_BOARD |
PERM_ACCOUNTS | PERM_CHATROOM |
PERM_VIEWSYSOP | PERM_PRG)) {
puts("\r\n權限不足,請換其它 port 連線。\r\n");
exit(0);
}
}
#ifdef LOCAL_LOGIN_MOD
LOCAL_LOGIN_MOD();
#endif
if (strcasecmp(str_sysop, cuser.userid) == 0){
#ifdef NO_SYSOP_ACCOUNT
exit(0);
#else /* 自動加上各個主要權限 */
// TODO only allow in local connection?
pwcuInitAdminPerm();
#endif
}
/* 早該有 home 了, 不知道為何有的帳號會沒有, 被砍掉了? */
mkuserdir(cuser.userid);
logattempt(cuser.userid, ' ', login_start_time, fromhost);
ensure_user_agreement_version();
}
// check multi user
multi_user_check();
return 1;
}
static void
login_query(char *ruid)
{
#ifdef CONVERT
/* uid 加一位, for gb login */
char uid[IDLEN + 2];
int len;
#else
char uid[IDLEN + 1];
#endif
char passbuf[PASSLEN];
int attempts;
resolve_garbage();
#ifdef DEBUG
move(1, 0);
prints("debugging mode\ncurrent pid: %d\n", getpid());
#else
show_file("etc/Welcome", 1, -1, SHOWFILE_ALLOW_ALL);
#endif
attempts = 0;
while (1) {
if (attempts++ >= LOGINATTEMPTS) {
more("etc/goodbye", NA);
pressanykey();
sleep(3);
exit(1);
}
pwcuInitZero();
#ifdef DEBUG
move(19, 0);
prints("current pid: %d ", getpid());
#endif
if (getdata(20, 0, "請輸入代號,或以 guest 參觀,或以 new 註冊: ",
uid, sizeof(uid), DOECHO) < 1)
{
// got nothing
outs("請重新輸入。\n");
continue;
}
telnet_turnoff_client_detect();
#ifdef CONVERT
/* switch to gb mode if uid end with '.' */
len = strlen(uid);
if (uid[0] && uid[len - 1] == ',') {
set_converting_type(CONV_UTF8);
uid[len - 1] = 0;
redrawwin();
}
else if (uid[0] && uid[len - 1] == '.') {
outs("Sorry, GB encoding is not supported anymore. Please use UTF-8 (id,)");
continue;
}
else if (len >= IDLEN + 1)
uid[IDLEN] = 0;
#endif
if (0) {
assert(false);
#ifdef STR_GUEST
} else if (strcasecmp(uid, STR_GUEST) == 0) { /* guest */
strlcpy(ruid, STR_GUEST, IDLEN+1);
break;
#endif
#ifdef STR_REGNEW
} else if (strcasecmp(uid, STR_REGNEW) == 0) {
# ifndef LOGINASNEW
outs("本系統目前無法以 " STR_REGNEW " 註冊"
# ifdef STR_GUEST
", 請用 " STR_GUEST " 進入"
# endif // STR_GUEST
"\n");
continue;
# endif // !LOGINASNEW
strlcpy(ruid, STR_REGNEW, IDLEN+1);
break;
#endif // STR_REGNEW
#ifdef STR_RECOVER
} else if (strcasecmp(uid, STR_RECOVER) == 0) {
strlcpy(ruid, STR_RECOVER, IDLEN+1);
break;
#endif // STR_RECOVER
} else if (is_validuserid(uid)) {
/* normal user */
int valid_user = 1;
/* load the user record */
if (initcuser(uid) < 1 || !cuser.userid[0])
valid_user = 0;
/* check if the user is forced to login via secure connection. */
if (valid_user &&
(passwd_require_secure_connection(&cuser) && !is_secure_connection)) {
outs("抱歉,此帳號已設定為只能使用安全連線(如ssh)登入。\n");
doupdate();
sleep(5);
continue;
}
/* ask user for password, even the user does not exists. */
getdata(21, 0, MSG_PASSWD,
passbuf, sizeof(passbuf), NOECHO);
passbuf[8] = '\0';
move (22, 0); clrtoeol();
outs("正在檢查密碼...");
move(22, 0); refresh();
/* prepare for later */
clrtoeol();
if (!valid_user || !checkpasswd(cuser.passwd, passbuf)) {
if(is_validuserid(cuser.userid))
logattempt(cuser.userid , '-', login_start_time, fromhost);
sleep(1);
outs(ERR_PASSWD);
} else {
strlcpy(ruid, cuser.userid, IDLEN+1);
outs("密碼正確! 開始登入系統...");
move(22, 0); refresh();
clrtoeol();
break;
}
} else {
outs(err_uid);
}
}
// auth ok.
#ifdef DETECT_CLIENT
LogClientCode();
#endif
}
static void
check_BM(void)
{
int i, total;
assert(HasUserPerm(PERM_BM));
for( i = 0, total = num_boards() ; i < total ; ++i )
if( is_BM_cache(i + 1) ) /* XXXbid */
return;
// disable BM permission
pwcuBitDisableLevel(PERM_BM);
clear();
outs("\n由於您已有一段時間未再擔任任何看板板主,\n"
"\n板主權(包含進入BM板及加大信箱等)已被收回。\n");
pressanykey();
}
static void
setup_utmp(int mode)
{
/* NOTE, 在 getnewutmpent 之前不應該有任何 slow/blocking function */
userinfo_t uinfo = {0};
uinfo.pid = currpid = getpid();
uinfo.uid = usernum;
uinfo.mode = currstat = mode;
uinfo.userlevel = cuser.userlevel;
uinfo.lastact = time(NULL);
// only enable this after you've really changed talk.c to NOT use from_alias.
uinfo.from_ip = inet_addr(fromhost);
strlcpy(uinfo.userid, cuser.userid, sizeof(uinfo.userid));
strlcpy(uinfo.nickname, cuser.nickname, sizeof(uinfo.nickname));
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.chess_elo_rating = cuser.chess_elo_rating;
uinfo.go_win = cuser.go_win;
uinfo.go_lose = cuser.go_lose;
uinfo.go_tie = cuser.go_tie;
uinfo.dark_win = cuser.dark_win;
uinfo.dark_lose = cuser.dark_lose;
uinfo.dark_tie = cuser.dark_tie;
uinfo.invisible = (cuser.invisible % 2) && (!HasUserPerm(PERM_VIOLATELAW));
uinfo.pager = cuser.pager % PAGER_MODES;
uinfo.withme = cuser.withme & ~WITHME_ALLFLAG;
if(cuser.withme & (cuser.withme<<1) & (WITHME_ALLFLAG<<1))
uinfo.withme = 0;
getnewutmpent(&uinfo);
//////////////////////////////////////////////////////////////////
// 以下可以進行比較花時間的運算。
//////////////////////////////////////////////////////////////////
currmode = MODE_STARTED;
SHM->UTMPneedsort = 1;
strip_nonebig5((unsigned char *)currutmp->nickname, sizeof(currutmp->nickname));