summaryrefslogtreecommitdiffstats
path: root/pttbbs/daemon/logind/logind.c
blob: a93f0f2bacc3a379459f4ec3c8ef11b14d8668a0 (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
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
// The High Performance Login Daemon (works with tunnel mode)
// $Id$
//
// Create:       Hung-Te Lin <piaip@csie.ntu.edu.tw>
// Contributors: wens, kcwu
// Initial Date: 2009/06/01
//
// Copyright (C) 2009, Hung-Te Lin <piaip@csie.ntu.edu.tw>
// All rights reserved

// TODO:
// 1. [done] cache guest's usernum and check if too many guests online
// 2. [drop] change close connection to 'wait until user hit then close'
// 3. [done] regular check text screen files instead of HUP?
// 4. [done] re-start mbbsd if pipe broken?
// 5. [drop] clean mbbsd pid log files?
// 6. [done] handle non-block i/o
// 7. [done] asynchronous tunnel handshake
// 8. simplify async ack queue to ordered sequence
// 9. force telnet_init_cmd to complete
// 10.prioritized logattempt (and let connection to wait logattempt to end)

#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <signal.h>
#include <event.h>

// XXX should we need to keep this definition?
#define _BBS_UTIL_C_

#include "bbs.h"
#include "banip.h"
#include "daemons.h"

#ifndef LOGIND_REGULAR_CHECK_DURATION
#define LOGIND_REGULAR_CHECK_DURATION   (15)
#endif 

#ifndef LOGIND_MAX_FDS
#define LOGIND_MAX_FDS      (100000)
#endif

#define MY_EVENT_PRIORITY_NUMBERS   (4)
#define EVTPRIORITY_NORM    (MY_EVENT_PRIORITY_NUMBERS/2)
#define EVTPRIORITY_ACK     (EVTPRIORITY_NORM-1)

// some systems has hard limit of this to 128.
#ifndef LOGIND_SOCKET_QLEN
#define LOGIND_SOCKET_QLEN  (100)
#endif

#ifndef AUTHFAIL_SLEEP_SEC
#define AUTHFAIL_SLEEP_SEC  (15)
#endif

#ifndef OVERLOAD_SLEEP_SEC
#define OVERLOAD_SLEEP_SEC  (60)
#endif

#ifndef ACK_TIMEOUT_SEC
#define ACK_TIMEOUT_SEC     (5*60)
#endif

#ifndef BAN_SLEEP_SEC
#define BAN_SLEEP_SEC       (60)
#endif

#ifndef IDLE_TIMEOUT_SEC
#define IDLE_TIMEOUT_SEC    (20*60)
#endif

#ifndef MAX_TEXT_SCREEN_LINES
#define MAX_TEXT_SCREEN_LINES   (24)
#endif

#ifndef OPTIMIZE_SOCKET
#define OPTIMIZE_SOCKET(sock) do {} while(0)
#endif

// to prevent flood trying services...
#ifndef LOGIND_MAX_RETRY_SERVICE
#define LOGIND_MAX_RETRY_SERVICE   (15)
#endif

// local definiions
#define MY_SVC_NAME  "logind"
#define LOG_PREFIX  "[logind] "

///////////////////////////////////////////////////////////////////////
// global variables
int g_tunnel;           // tunnel for service daemon
int g_logattempt_pipe;  // pipe to log attempts

// server status
int g_overload = 0;
int g_banned   = 0;
int g_opened_fd= 0;
int g_nonblock = 1;
int g_async_ack= 1;
int g_async_logattempt = 1;

// debug and reporting
int g_verbose  = 0;
int g_report_timeout = 0;
char g_logfile_path[PATHLEN];

// retry service
char g_retry_cmd[PATHLEN];
int  g_retry_times;

// cache data
int g_reload_data = 1;  // request to reload data
time4_t g_welcome_mtime;
int g_guest_usernum  = 0;  // numeric uid of guest account
int g_guest_too_many = 0;  // 1 if exceed MAX_GUEST

enum {
    VERBOSE_ERROR,
    VERBOSE_INFO,
    VERBOSE_DEBUG
};

///////////////////////////////////////////////////////////////////////
// login context, constants and states

enum {
    LOGIN_STATE_INIT  = 1,
    LOGIN_STATE_USERID,
    LOGIN_STATE_PASSWD,
    LOGIN_STATE_AUTH,
    LOGIN_STATE_WAITACK,

    LOGIN_HANDLE_WAIT = 1,
    LOGIN_HANDLE_BEEP,
    LOGIN_HANDLE_OUTC,
    LOGIN_HANDLE_REDRAW_USERID,
    LOGIN_HANDLE_BS,
    LOGIN_HANDLE_PROMPT_PASSWD,
    LOGIN_HANDLE_START_AUTH,

    AUTH_RESULT_STOP   = -3,
    AUTH_RESULT_FREEID_TOOMANY = -2,
    AUTH_RESULT_FREEID = -1,
    AUTH_RESULT_FAIL   = 0,
    AUTH_RESULT_RETRY  = AUTH_RESULT_FAIL,
    AUTH_RESULT_OK     = 1,
};

#ifdef  CONVERT
#define IDBOXLEN    (IDLEN+2)   // one extra char for encoding
#else
#define IDBOXLEN    (IDLEN+1)
#endif

typedef struct {
    int  state;
    int  retry;
    int  encoding;
    int  t_lines;
    int  t_cols;
    int  icurr;         // cursor (only available in userid input mode)
    Fnv32_t client_code;
    char userid [IDBOXLEN];
    char pad0;   // for safety
    char passwd [PASSLEN+1];
    char pad1;   // for safety
    char hostip [IPV4LEN+1];
    char pad2;   // for safety
    char port   [IDLEN+1];
    char pad3;   // for safety
} login_ctx;

typedef struct {
    unsigned int cb;
    time4_t      enter;
    struct bufferevent *bufev;
    struct event ev;
    TelnetCtx    telnet;
    VtkbdCtx     vtkbd;
    login_ctx    ctx;
} login_conn_ctx;

typedef struct {
    size_t  cb;
    time4_t logtime;
    char    userid[IDLEN+1];
    char    hostip[IPV4LEN+1];
} logattempt_ctx;

typedef struct {
    struct event ev;
    int    port;
} bind_event;

void 
login_ctx_init(login_ctx *ctx)
{
    assert(ctx);
    memset(ctx, 0, sizeof(login_ctx));
    ctx->state = LOGIN_STATE_INIT;
    ctx->client_code = FNV1_32_INIT;
}

int 
login_ctx_retry(login_ctx *ctx)
{
    assert(ctx);
    ctx->state = LOGIN_STATE_USERID;
    ctx->encoding = 0;
    memset(ctx->userid, 0, sizeof(ctx->userid));
    memset(ctx->passwd, 0, sizeof(ctx->passwd));
    ctx->icurr    = 0;
    // do not touch hostip, client code, t_*
    ctx->retry ++;
    return ctx->retry;
}

int 
login_ctx_handle(login_ctx *ctx, int c)
{
    int l;

    assert(ctx);
    switch(ctx->state)
    {
        case LOGIN_STATE_INIT:
        case LOGIN_STATE_USERID:
            l = strlen(ctx->userid);

            switch(c)
            {
                case KEY_ENTER:
                    ctx->state = LOGIN_STATE_PASSWD;
                    return LOGIN_HANDLE_PROMPT_PASSWD;

                case KEY_BS:
                    if (!l || !ctx->icurr)
                        return LOGIN_HANDLE_BEEP;
                    if (ctx->userid[ctx->icurr])
                    {
                        ctx->icurr--;
                        memmove(ctx->userid + ctx->icurr,
                                ctx->userid + ctx->icurr+1,
                                l - ctx->icurr);
                        return LOGIN_HANDLE_REDRAW_USERID;
                    }
                    // simple BS
                    ctx->icurr--;
                    ctx->userid[l-1] = 0;
                    return LOGIN_HANDLE_BS;

                case Ctrl('D'):
                case KEY_DEL:
                    if (!l || !ctx->userid[ctx->icurr])
                        return LOGIN_HANDLE_BEEP;
                    memmove(ctx->userid + ctx->icurr,
                            ctx->userid + ctx->icurr+1,
                            l - ctx->icurr);
                    return LOGIN_HANDLE_REDRAW_USERID;

                case Ctrl('B'):
                case KEY_LEFT:
                    if (ctx->icurr)
                        ctx->icurr--;
                    return LOGIN_HANDLE_REDRAW_USERID;

                case Ctrl('F'):
                case KEY_RIGHT:
                    if (ctx->userid[ctx->icurr])
                        ctx->icurr ++;
                    return LOGIN_HANDLE_REDRAW_USERID;

                case Ctrl('A'):
                case KEY_HOME:
                    ctx->icurr = 0;
                    return LOGIN_HANDLE_REDRAW_USERID;

                case Ctrl('E'):
                case KEY_END:
                    ctx->icurr = l;
                    return LOGIN_HANDLE_REDRAW_USERID;

                case Ctrl('K'):
                    if (!l || !ctx->userid[ctx->icurr])
                        return LOGIN_HANDLE_BEEP;
                    memset( ctx->userid + ctx->icurr, 0,
                            l - ctx->icurr +1);
                    return LOGIN_HANDLE_REDRAW_USERID;
            }

            // default: insert characters
            if (!isascii(c) || !isprint(c) || 
                c == ' ' ||
                l+1 >= sizeof(ctx->userid))
                return LOGIN_HANDLE_BEEP;

            memmove(ctx->userid + ctx->icurr + 1,
                    ctx->userid + ctx->icurr,
                    l - ctx->icurr +1);
            ctx->userid[ctx->icurr++] = c;

            if (ctx->icurr != l+1)
                return LOGIN_HANDLE_REDRAW_USERID;

            return LOGIN_HANDLE_OUTC;

        case LOGIN_STATE_PASSWD:
            l = strlen(ctx->passwd);

            if (c == KEY_ENTER)
            {
                // no matter what, apply the passwd
                ctx->state = LOGIN_STATE_AUTH;
                return LOGIN_HANDLE_START_AUTH;
            }
            if (c == KEY_BS)
            {
                if (!l)
                    return LOGIN_HANDLE_BEEP;
                ctx->passwd[l-1] = 0;
                return LOGIN_HANDLE_WAIT;
            }

            // XXX check VGET_PASSWD = VGET_NOECHO|VGET_ASCIIONLY
            if ( (!isascii(c) || !isprint(c)) || 
                l+1 >= sizeof(ctx->passwd))
                return LOGIN_HANDLE_BEEP;

            ctx->passwd[l] = c;

            return LOGIN_HANDLE_WAIT;

        default:
            break;
    }
    return LOGIN_HANDLE_BEEP;
}

///////////////////////////////////////////////////////////////////////
// Mini Queue

#ifndef ACK_QUEUE_DEFAULT_CAPACITY
#define ACK_QUEUE_DEFAULT_CAPACITY  (128)
#endif

struct login_ack_queue
{
    login_conn_ctx **queue;
    size_t           size;
    size_t           reuse;
    size_t           capacity;
};

static struct login_ack_queue g_ack_queue;

static void
ackq_gc()
{
    // reset queue to zero if already empty.
    if (g_ack_queue.reuse == g_ack_queue.size)
        g_ack_queue.reuse =  g_ack_queue.size = 0;
}

static void
ackq_add(login_conn_ctx *ctx)
{
    assert(ctx->cb == sizeof(login_conn_ctx));
    if (g_ack_queue.reuse)
    {
        // there's some space in the queue, let's use it.
        size_t i;
        for (i = 0; i < g_ack_queue.size; i++)
        {
            if (g_ack_queue.queue[i])
                continue;

            g_ack_queue.queue[i] = ctx;
            g_ack_queue.reuse--;
            ackq_gc();
            return;
        }
        assert(!"corrupted ack queue");
        // may cause leak here, since queue is corrupted.
        return;
    }

    if (++g_ack_queue.size > g_ack_queue.capacity)
    {
        time4_t tnow = time(NULL);
        g_ack_queue.capacity *= 2;
        if (g_ack_queue.capacity < ACK_QUEUE_DEFAULT_CAPACITY)
            g_ack_queue.capacity = ACK_QUEUE_DEFAULT_CAPACITY;

        fprintf(stderr, LOG_PREFIX "%s: resize ack queue to: %u (%u in use)\r\n", 
                Cdate(&tnow),
                (unsigned int)g_ack_queue.capacity, (unsigned int)g_ack_queue.size);

        g_ack_queue.queue = (login_conn_ctx**) realloc (g_ack_queue.queue, 
                sizeof(login_conn_ctx*) * g_ack_queue.capacity);
        assert(g_ack_queue.queue);
    }
    g_ack_queue.queue[g_ack_queue.size-1] = ctx;
    ackq_gc();
}

static int
ackq_del(login_conn_ctx *conn)
{
    size_t i;

    // XXX in current implementation, the conn pointer may be
    // destroyed before getting acks, so don't check its validness.
    // assert(conn && conn->cb == sizeof(login_conn_ctx));
    for (i = 0; i < g_ack_queue.size; i++)
    {
        if (g_ack_queue.queue[i] != conn)
            continue;

        // found the target
        g_ack_queue.queue[i] = NULL;

        if (i+1 == g_ack_queue.size)
            g_ack_queue.size--;
        else
            g_ack_queue.reuse++;

        ackq_gc();
        return 1;
    }

    return 0;
}

///////////////////////////////////////////////////////////////////////
// I/O

static ssize_t 
_buff_write(login_conn_ctx *conn, const void *buf, size_t nbytes)
{
    return bufferevent_write(conn->bufev, buf, nbytes);
}

///////////////////////////////////////////////////////////////////////
// Mini Terminal

static void 
_mt_bell(login_conn_ctx *conn)
{
    static const char b = Ctrl('G');
    _buff_write(conn, &b, 1);
}

static void 
_mt_bs(login_conn_ctx *conn)
{
    static const char cmd[] = "\b \b";
    _buff_write(conn, cmd, sizeof(cmd)-1);
}

static void 
_mt_home(login_conn_ctx *conn)
{
    static const char cmd[] = ESC_STR "[H";
    _buff_write(conn, cmd, sizeof(cmd)-1);
}

static void 
_mt_clrtoeol(login_conn_ctx *conn)
{
    static const char cmd[] = ESC_STR "[K";
    _buff_write(conn, cmd, sizeof(cmd)-1);
}

static void 
_mt_clear(login_conn_ctx *conn)
{
    static const char cmd[] = ESC_STR "[2J";
    _mt_home(conn);
    _buff_write(conn, cmd, sizeof(cmd)-1);
}

static void 
_mt_move_yx(login_conn_ctx *conn, const char *mcmd)
{
    static const char cmd1[] = ESC_STR "[",
                      cmd2[] = "H";
    _buff_write(conn, cmd1, sizeof(cmd1)-1);
    _buff_write(conn, mcmd, strlen(mcmd));
    _buff_write(conn, cmd2, sizeof(cmd2)-1);
}

///////////////////////////////////////////////////////////////////////
// Telnet Protocol

static void 
_telnet_resize_term_cb(void *resize_arg, int w, int h)
{
    login_ctx *ctx = (login_ctx*) resize_arg;
    assert(ctx);
    ctx->t_lines = h;
    ctx->t_cols  = w;
}

static void 
_telnet_update_cc_cb(void *cc_arg, unsigned char c)
{
    login_ctx *ctx = (login_ctx*) cc_arg;
    assert(ctx);
    // fprintf(stderr, "update cc: %08lX", (unsigned long)ctx->client_code);
    FNV1A_CHAR(c, ctx->client_code);
    // fprintf(stderr, "-> %08lX\r\n", (unsigned long)ctx->client_code);
}

static void 
_telnet_write_data_cb(void *write_arg, int fd, const void *buf, size_t nbytes)
{
    login_conn_ctx *conn = (login_conn_ctx *)write_arg;
    _buff_write(conn, buf, nbytes);
}

#ifdef  LOGIND_OPENFD_IN_AYT
static void 
_telnet_send_ayt_cb(void *ayt_arg, int fd)
{
    login_conn_ctx *conn = (login_conn_ctx *)ayt_arg;
    char buf[64];

    assert(conn);
    if (!g_async_ack)
    {
        snprintf(buf, sizeof(buf), "  (#%d)fd:%u  \r\n", 
                g_retry_times, g_opened_fd);
    }
    else
    {
        snprintf(buf, sizeof(buf), "  (#%d)fd:%u,ack:%u(-%u)  \r\n", 
                g_retry_times, g_opened_fd, 
                (unsigned int)g_ack_queue.size, 
                (unsigned int)g_ack_queue.reuse );
    }
    _buff_write(conn, buf, strlen(buf));
}
#endif

const static struct TelnetCallback 
telnet_callback = {
    _telnet_write_data_cb,
    _telnet_resize_term_cb,

#ifdef DETECT_CLIENT
    _telnet_update_cc_cb,
#else
    NULL,
#endif

#ifdef LOGIND_OPENFD_IN_AYT
    _telnet_send_ayt_cb,
#else
    NULL,
#endif
};

///////////////////////////////////////////////////////////////////////
// Socket Option

static void
_enable_nonblock(int sock)
{
    // XXX note: NONBLOCK is not always inherited (eg, not on Linux).
    fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK);
}

static void
_disable_nonblock(int sock)
{
    // XXX note: NONBLOCK is not always inherited (eg, not on Linux).
    fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) & (~O_NONBLOCK) );
}

static int 
_set_connection_opt(int sock)
{
    const int szrecv = 1024, szsend=4096;
    const struct linger lin = {0};

    // keep alive: server will check target connection. (around 2hr)
    const int on = 1;
    setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void*)&on, sizeof(on));
   
    // fast close
    setsockopt(sock, SOL_SOCKET, SO_LINGER, &lin, sizeof(lin));
    // adjust transmission window
    setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void*)&szrecv, sizeof(szrecv));
    setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void*)&szsend, sizeof(szsend));
    OPTIMIZE_SOCKET(sock);

    return 0;
}

static int
_set_tunnel_opt(int sock)
{
    // XXX RCVBUF/SNDBUF seems not really required...
#if 0
    const int szrecv = 131072, szsend = 131072;

    // adjust transmission window
    if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void*)&szrecv, sizeof(szrecv)) < 0 ||
        setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void*)&szsend, sizeof(szsend)) < 0)
    {
        fprintf(stderr, LOG_PREFIX "WARNING: "
                "set_tunnel_opt: failed to increase buffer to (%u,%u)\r\n", szsend, szrecv);
    }
#endif
    return 0;
}

static int
_set_bind_opt(int sock)
{
    const int on = 1;

    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on));
    _set_connection_opt(sock);

    return 0;
}

///////////////////////////////////////////////////////////////////////
// Draw Screen

#ifndef  INSCREEN
# define INSCREEN ANSI_RESET "\r\n【" BBSNAME "】◎(" MYHOSTNAME ", " MYIP ") \r\n"
#endif

#ifdef   STR_GUEST
# define MSG_GUEST ",或以 " STR_GUEST " 參觀"
#else
# define MSG_GUEST ""
#endif

#ifdef   STR_REGNEW
# define MSG_REGNEW ",或以 new 註冊"
#else
# define MSG_REGNEW
#endif

#define BOTTOM_YX           "24;1"
#define LOGIN_PROMPT_MSG    ANSI_RESET "請輸入代號" MSG_GUEST MSG_REGNEW ": " ANSI_REVERSE
#define LOGIN_PROMPT_YX     "21;1"
#define LOGIN_PROMPT_END    ANSI_RESET
#define PASSWD_PROMPT_MSG   ANSI_RESET MSG_PASSWD
#define PASSWD_PROMPT_YX    "22;1"
#define PASSWD_CHECK_MSG    ANSI_RESET "正在檢查帳號與密碼..."
#define PASSWD_CHECK_YX     PASSWD_PROMPT_YX
#define AUTH_SUCCESS_MSG    ANSI_RESET "密碼正確! 開始登入系統...\r\n"
#define AUTH_SUCCESS_YX     PASSWD_PROMPT_YX
#define FREEAUTH_SUCCESS_MSG    ANSI_RESET "開始登入系統...\r\n"
#define FREEAUTH_SUCCESS_YX AUTH_SUCCESS_YX
#define AUTH_FAIL_MSG       ANSI_RESET ERR_PASSWD
#define AUTH_FAIL_YX        PASSWD_PROMPT_YX
#define USERID_EMPTY_MSG    ANSI_RESET "請重新輸入。"
#define USERID_EMPTY_YX     PASSWD_PROMPT_YX
#define SERVICE_FAIL_MSG    ANSI_COLOR(0;1;31) "抱歉,部份系統正在維護中,請稍候再試。 " ANSI_RESET
#define SERVICE_FAIL_YX     BOTTOM_YX
#define OVERLOAD_CPU_MSG    ANSI_RESET " 系統過載, 請稍後再來... "
#define OVERLOAD_CPU_YX     BOTTOM_YX
#define OVERLOAD_USER_MSG   ANSI_RESET " 由於人數過多,請您稍後再來... "
#define OVERLOAD_USER_YX    BOTTOM_YX

#define REJECT_FREE_UID_MSG ANSI_RESET " 抱歉,此帳號或服務已達上限。 "
#define REJECT_FREE_UID_YX  BOTTOM_YX

#ifdef  STR_GUEST
#define TOO_MANY_GUEST_MSG  ANSI_RESET " 抱歉,目前已有太多 " STR_GUEST " 在站上。 "
#define TOO_MANY_GUEST_YX   BOTTOM_YX
#endif

#define FN_WELCOME          BBSHOME "/etc/Welcome"
#define FN_GOODBYE          BBSHOME "/etc/goodbye"
#define FN_BAN              BBSHOME "/" BAN_FILE

static char *welcome_screen, *goodbye_screen, *ban_screen;

static void
load_text_screen_file(const char *filename, char **pptr)
{
    FILE *fp = NULL;
    off_t sz, wsz=0, psz;
    char *p, *s = NULL;
    int max_lines = MAX_TEXT_SCREEN_LINES;

    sz = dashs(filename);
    if (sz > 1)
    {
        wsz = sz*2 +1; // *2 for cr+lf, extra one byte for safe strchr().
        fp = fopen(filename, "rt");
    }

    // check valid file
    assert(pptr);
    s = *pptr;
    if (!fp)
    {
        if (s) free(s);
        *pptr = NULL;
        return;
    }

    // check memory buffer
    s = realloc(*pptr, wsz);  
    if (!s)
    {
        fclose(fp);
        return;
    }
    *pptr = s;

    // prepare buffer
    memset(s, 0, wsz);
    p = s;
    psz = wsz;

    while ( max_lines-- > 0 &&
            fgets(p, psz, fp))
    {
        size_t l = strlen(p);
        psz -= l;
        p   += l;
        if (l > 0 && *(p-1) == '\n')
        {
            // convert \n to \r\n
            *(p-1)= '\r';
            *p ++ = '\n';
        }
        // Remove \n from last line (breaks full screen)
        if (max_lines == 0) {
            *(p-2) = '\0';
            p     -= 2;
            psz   += 2;
        }

    }
    fclose(fp);
}

static void regular_check();

static void
reload_data()
{
    regular_check();

    if (!g_reload_data)
        return;

    fprintf(stderr, LOG_PREFIX "start reloading data.\r\n");
    g_reload_data = 0;
    g_welcome_mtime = dasht(FN_WELCOME);
    load_text_screen_file(FN_WELCOME, &welcome_screen);
    load_text_screen_file(FN_GOODBYE, &goodbye_screen);
    load_text_screen_file(FN_BAN,     &ban_screen);
}

static void
draw_text_screen(login_conn_ctx *conn, const char *scr)
{
    const char *ps, *pe;
    char buf[64];
    time4_t now;

    _mt_clear(conn);
    if (!scr || !*scr)
        return;

    // draw the screen from text file
    // XXX Because the text file may contain a very small subset of escape sequence
    // *t[Cdate] and *u[SHM->UTMPnumber], we implement a tiny version of 
    // expand_esc_star here.

    ps = pe = scr;
    while(pe && *pe)
    {
        // find a safe range between (ps, pe) to print
        pe = strchr(pe, ESC_CHR);
        if (!pe)
        {
            // no more escapes, print all.
            _buff_write(conn, ps, strlen(ps));
            break;
        }

        // let's look ahead
        pe++;

        // if not esc_star, search for next.
        if (*pe != '*')
            continue;

        // flush previous data
        _buff_write(conn, ps, pe - ps - 1);

        buf[0] = 0; pe++;

        // expand the star
        switch(*pe)
        {
            case 't':   // current time
                // strcpy(buf, "[date]");
                now = time(0);
                strlcpy(buf, Cdate(&now), sizeof(buf));
                break;

            case 'u':   // current online users
                // strcpy(buf, "[SHM->UTMPnumber]");
                snprintf(buf, sizeof(buf), "%d", SHM->UTMPnumber);
                break;
        }

        if(buf[0])
            _buff_write(conn, buf, strlen(buf));
        pe ++;
        ps = pe;
    }
}

static void
draw_goodbye(login_conn_ctx *conn)
{
    draw_text_screen(conn, goodbye_screen);
}

static void 
draw_userid_prompt(login_conn_ctx *conn, const char *uid, int icurr)
{
    char box[IDBOXLEN];

    _mt_move_yx(conn, LOGIN_PROMPT_YX);  _mt_clrtoeol(conn);
    _buff_write(conn, LOGIN_PROMPT_MSG, sizeof(LOGIN_PROMPT_MSG)-1);
    // draw input box
    memset(box, ' ', sizeof(box));
    if (uid) memcpy(box, uid, strlen(uid));
    _buff_write (conn, box,   sizeof(box));
    memset(box, '\b',sizeof(box));
    _buff_write (conn, box,   sizeof(box)-icurr);
}

static void
draw_userid_prompt_end(login_conn_ctx *conn)
{
    if (g_verbose > VERBOSE_DEBUG) 
        fprintf(stderr, LOG_PREFIX "reset connection attribute.\r\n");
    _buff_write(conn, LOGIN_PROMPT_END, sizeof(LOGIN_PROMPT_END)-1);
}

static void
draw_passwd_prompt(login_conn_ctx *conn)
{
    _mt_move_yx(conn, PASSWD_PROMPT_YX); _mt_clrtoeol(conn);
    _buff_write(conn, PASSWD_PROMPT_MSG, sizeof(PASSWD_PROMPT_MSG)-1);
}

static void
draw_empty_userid_warn(login_conn_ctx *conn)
{
    _mt_move_yx(conn, USERID_EMPTY_YX); _mt_clrtoeol(conn);
    _buff_write(conn, USERID_EMPTY_MSG, sizeof(USERID_EMPTY_MSG)-1);
}

static void 
draw_check_passwd(login_conn_ctx *conn)
{
    _mt_move_yx(conn, PASSWD_CHECK_YX); _mt_clrtoeol(conn);
    _buff_write(conn, PASSWD_CHECK_MSG, sizeof(PASSWD_CHECK_MSG)-1);
}

static void
draw_auth_success(login_conn_ctx *conn, int free)
{
    if (free)
    {
        _mt_move_yx(conn, FREEAUTH_SUCCESS_YX); _mt_clrtoeol(conn);
        _buff_write(conn, FREEAUTH_SUCCESS_MSG, sizeof(FREEAUTH_SUCCESS_MSG)-1);
    } else {
        _mt_move_yx(conn, AUTH_SUCCESS_YX); _mt_clrtoeol(conn);
        _buff_write(conn, AUTH_SUCCESS_MSG, sizeof(AUTH_SUCCESS_MSG)-1);
    }
}

static void
draw_auth_fail(login_conn_ctx *conn)
{
    _mt_move_yx(conn, AUTH_FAIL_YX); _mt_clrtoeol(conn);
    _buff_write(conn, AUTH_FAIL_MSG, sizeof(AUTH_FAIL_MSG)-1);
}

static void
draw_service_failure(login_conn_ctx *conn)
{
    _mt_move_yx(conn, PASSWD_CHECK_YX); _mt_clrtoeol(conn);
    _mt_move_yx(conn, SERVICE_FAIL_YX); _mt_clrtoeol(conn);
    _buff_write(conn, SERVICE_FAIL_MSG, sizeof(SERVICE_FAIL_MSG)-1);
}

static void
draw_overload(login_conn_ctx *conn, int type)
{
    // XXX currently overload is displayed immediately after
    // banner/INSCREEN, so an enter is enough.
    _buff_write(conn, "\r\n", 2);
    // _mt_move_yx(conn, PASSWD_CHECK_YX); _mt_clrtoeol(conn);
    if (type == 1)
    {
        // _mt_move_yx(conn, OVERLOAD_CPU_YX); _mt_clrtoeol(conn);
        _buff_write(conn, OVERLOAD_CPU_MSG, sizeof(OVERLOAD_CPU_MSG)-1);
    } 
    else if (type == 2)
    {
        // _mt_move_yx(conn, OVERLOAD_USER_YX); _mt_clrtoeol(conn);
        _buff_write(conn, OVERLOAD_USER_MSG, sizeof(OVERLOAD_USER_MSG)-1);
    } 
    else {
        assert(!"unknown overload type");
        // _mt_move_yx(conn, OVERLOAD_CPU_YX); _mt_clrtoeol(conn);
        _buff_write(conn, OVERLOAD_CPU_MSG, sizeof(OVERLOAD_CPU_MSG)-1);
    }
}

static void
draw_reject_free_userid(login_conn_ctx *conn, const char *freeid)
{
    _mt_move_yx(conn, PASSWD_CHECK_YX); _mt_clrtoeol(conn);
#ifdef STR_GUEST
    if (strcasecmp(freeid, STR_GUEST) == 0)
    {
        _mt_move_yx(conn, TOO_MANY_GUEST_YX); _mt_clrtoeol(conn);
        _buff_write(conn, TOO_MANY_GUEST_MSG, sizeof(TOO_MANY_GUEST_MSG)-1);
        return;
    }
#endif
    _mt_move_yx(conn, REJECT_FREE_UID_YX); _mt_clrtoeol(conn);
    _buff_write(conn, REJECT_FREE_UID_MSG, sizeof(REJECT_FREE_UID_MSG)-1);

}

///////////////////////////////////////////////////////////////////////
// BBS Logic

static void
regular_check()
{
    // cache results
    static time_t last_check_time = 0;
    time_t now = time(0);

    if ( now - last_check_time < LOGIND_REGULAR_CHECK_DURATION)
        return;

    last_check_time = now;
    g_overload = 0;
    g_banned   = 0;

#ifndef LOGIND_DONT_CHECK_FREE_USERID
    g_guest_too_many = 0;
    g_guest_usernum  = 0;
#endif

    if (cpuload(NULL) > MAX_CPULOAD)
    {
        g_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;
        g_overload = 2;
    }

    if (dashf(FN_BAN))
    {
        g_banned = 1;
        load_text_screen_file(FN_BAN, &ban_screen);
    }

    // check welcome screen
    if (g_verbose > VERBOSE_INFO) 
        fprintf(stderr, LOG_PREFIX "check welcome screen.\r\n");
    if (dasht(FN_WELCOME) != g_welcome_mtime)
    {
        g_reload_data = 1;
        if (g_verbose > VERBOSE_INFO)
            fprintf(stderr, LOG_PREFIX 
                    "modified. must update welcome screen ...\r\n");
    }
}

static int 
check_banip(char *host)
{
    uint32_t thisip = ipstr2int(host);
    return uintbsearch(thisip, &banip[1], banip[0]) ? 1 : 0;
}

static const char *
auth_is_free_userid(const char *userid)
{
#if defined(STR_GUEST) && !defined(NO_GUEST_ACCOUNT_REG)
    if (strcasecmp(userid, STR_GUEST) == 0)
        return STR_GUEST;
#endif

#ifdef STR_REGNEW 
    if (strcasecmp(userid, STR_REGNEW) == 0)
        return STR_REGNEW;
#endif

    return NULL;
}
static int
auth_check_free_userid_allowance(const char *userid)
{
#ifdef LOGIND_DONT_CHECK_FREE_USERID
    // XXX experimental to disable free id checking
    return 1;
#endif

#ifdef STR_REGNEW
    // accept all 'new' command.
    if (strcasecmp(userid, STR_REGNEW) == 0)
        return 1;
#endif

#ifdef STR_GUEST
    if (strcasecmp(userid, STR_GUEST) == 0)
    {
#  ifndef MAX_GUEST
        g_guest_too_many = 0;
#  else
        // if already too many guest, fast reject until next regular check.
        // XXX TODO also cache if guest is not too many?
        if (g_guest_too_many)
            return 0;

        // now, load guest account information.
        if (!g_guest_usernum)
        {
            if (g_verbose > VERBOSE_INFO) 
                fprintf(stderr, LOG_PREFIX " reload guest information\r\n");

            // reload guest information
            g_guest_usernum = searchuser(STR_GUEST, NULL);

            if (g_guest_usernum < 1 || g_guest_usernum > MAX_USERS)
                g_guest_usernum = 0;

            // if guest is not created, it's administrator's problem...
            assert(g_guest_usernum);
        }

        // update the 'too many' status.
        g_guest_too_many = 
            (!g_guest_usernum || (search_ulistn(g_guest_usernum, MAX_GUEST) != NULL));

        if (g_verbose > VERBOSE_INFO) fprintf(stderr, LOG_PREFIX 
                " guests are %s\r\n", g_guest_too_many ? "TOO MANY" : "ok.");

#  endif // MAX_GUEST
        return g_guest_too_many ? 0 : 1;
    }
#endif // STR_GUEST

    // shall never reach here.
    assert(!"unknown free userid");
    return 0;
}


// NOTE ctx->passwd will be destroyed (must > PASSLEN+1)
// NOTE ctx->userid may be changed (must > IDLEN+1)
static int
auth_user_challenge(login_ctx *ctx)
{
    char *uid = ctx->userid,
         *passbuf = ctx->passwd;
    const char *free_uid = auth_is_free_userid(uid);
    userec_t user = {0};

    if (free_uid)
    {
        strlcpy(ctx->userid, free_uid, sizeof(ctx->userid));
        return AUTH_RESULT_FREEID;
    }

    if (passwd_load_user(uid, &user) < 1 ||
        !user.userid[0] ||
        !checkpasswd(user.passwd, passbuf) )
    {
        if (user.userid[0])
            strcpy(uid, user.userid);
        return AUTH_RESULT_FAIL;
    }

    // normalize user id
    strcpy(uid, user.userid);
    return AUTH_RESULT_OK;
}

static void
retry_service()
{
    // empty g_tunnel means the service is not started or waiting retry.
    if (!g_tunnel)
        return ;

    g_tunnel = 0;

    // now, see if we can retry for it.
    if (!*g_retry_cmd)
        return;

    if (g_retry_times >= LOGIND_MAX_RETRY_SERVICE)
    {
        fprintf(stderr, LOG_PREFIX 
                "retry too many times (>%d), stop and wait manually maintainance.\r\n",
                LOGIND_MAX_RETRY_SERVICE);
        return;
    }

    g_retry_times++;
    fprintf(stderr, LOG_PREFIX "#%d retry to start service: %s\r\n", 
            g_retry_times, g_retry_cmd);
    system(g_retry_cmd);
}

static int
login_conn_end_ack(login_conn_ctx *conn, void *ack, int fd);

static int 
start_service(int fd, login_conn_ctx *conn)
{
    login_data ld = {0};
    int ack = 0;
    login_ctx *ctx;

    if (!g_tunnel)
        return 0;

    assert(conn);
    ctx = &conn->ctx;

    ld.cb  = sizeof(ld);
    ld.ack = (void*)conn;

    strlcpy(ld.userid, ctx->userid, sizeof(ld.userid));
    strlcpy(ld.hostip, ctx->hostip, sizeof(ld.hostip));
    strlcpy(ld.port,   ctx->port,   sizeof(ld.port));
    ld.encoding = ctx->encoding;
    ld.client_code = ctx->client_code;
    ld.t_lines  = 24;   // default size
    ld.t_cols   = 80;
    if (ctx->t_lines > ld.t_lines)
        ld.t_lines = ctx->t_lines;
    if (ctx->t_cols > ld.t_cols)
        ld.t_cols = ctx->t_cols;

    if (g_verbose > VERBOSE_INFO) 
        fprintf(stderr, LOG_PREFIX "start new service: %s@%s:%s #%d\r\n",
                ld.userid, ld.hostip, ld.port, fd);

    // XXX simulate the cache re-construction in mbbsd/login_query.
    resolve_garbage();

    // since mbbsd may be running in blocking mode, let's re-configure fd.
    _disable_nonblock(fd);

    // deliver the fd to hosting service
    if (send_remote_fd(g_tunnel, fd) < 0)
    {
        if (g_verbose > VERBOSE_ERROR) fprintf(stderr, LOG_PREFIX
                "failed in send_remote_fd\r\n");
        return ack;
    }
   
    // deliver the login data to hosting servier
    if (towrite(g_tunnel, &ld, sizeof(ld)) < sizeof(ld))
    {
        if (g_verbose > VERBOSE_ERROR) fprintf(stderr, LOG_PREFIX
                "failed in towrite(login_data)\r\n");
        return ack;
    }

    // to prevent buffer full, we set priority here to force all ackes processed
    // (otherwise tunnel daemon may try to send act and 
    event_priority_set(&conn->ev, EVTPRIORITY_ACK);


    // wait (or async) service to response
    if (!login_conn_end_ack(conn, ld.ack, fd))
    {
        if (g_verbose > VERBOSE_ERROR) fprintf(stderr, LOG_PREFIX
                "failed in logind_conn_end_ack\r\n");
        return ack;
    }
    return 1;
}

static void
logattempt2(const char *userid, char c, time4_t logtime, const char *hostip)
{
    logattempt_ctx ctx = {0};

    while (g_async_logattempt)
    {
        assert(g_logattempt_pipe);
        ctx.cb = sizeof(ctx);
        ctx.logtime = logtime;
        strlcpy(ctx.userid, userid, sizeof(ctx.userid));
        strlcpy(ctx.hostip, hostip, sizeof(ctx.hostip));

        if (towrite(g_logattempt_pipe, &ctx, sizeof(ctx)) == sizeof(ctx))
            return;

        // failed ... back to internal.
        fprintf(stderr, LOG_PREFIX 
                "error: cannot use logattempt daemon, change to internal.\r\n");
        close(g_logattempt_pipe);
        g_async_logattempt= 0;
        g_logattempt_pipe = 0;
        break;
    }
    logattempt(userid, c, logtime, hostip);
}

static int 
auth_start(int fd, login_conn_ctx *conn)
{
    login_ctx *ctx = &conn->ctx;
    int isfree = 0, was_valid_uid = 0;
    draw_check_passwd(conn);

    if (is_validuserid(ctx->userid))
    {
        // ctx content may be changed.
        was_valid_uid = 1;
        switch (auth_user_challenge(ctx))
        {
            case AUTH_RESULT_FAIL:
                // logattempt(ctx->userid , '-', time(0), ctx->hostip);
                logattempt2(ctx->userid , '-', time(0), ctx->hostip);
                break;

            case AUTH_RESULT_FREEID:
                isfree = 1;
                // share FREEID case, no break here!
            case AUTH_RESULT_OK:
                if (!isfree)
                {
                    // do nothing. logattempt for auth-ok users is
                    // now done in mbbsd.
                }
                else if (!auth_check_free_userid_allowance(ctx->userid))
                {
                    // XXX since the only case of free
                    draw_reject_free_userid(conn, ctx->userid);
                    return AUTH_RESULT_STOP;
                }

                draw_auth_success(conn, isfree);

                if (!start_service(fd, conn))
                {
                    // too bad, we can't start service.
                    retry_service();
                    draw_service_failure(conn);
                    return AUTH_RESULT_STOP;
                }
                return AUTH_RESULT_OK;

            default:
                assert(!"unknown auth state.");
                break;
        }

    }

    // auth fail.
    _mt_bell(conn);

    // if fail, restart
    if (login_ctx_retry(ctx) >= LOGINATTEMPTS)
    {
        // end retry.
        draw_goodbye(conn);
        if (g_verbose > VERBOSE_INFO) 
            fprintf(stderr, LOG_PREFIX "auth fail (goodbye):  %s@%s  #%d...",
                    conn->ctx.userid, conn->ctx.hostip, fd);
        return AUTH_RESULT_STOP;

    }

    // prompt for retry
    if (was_valid_uid)
        draw_auth_fail(conn);
    else
        draw_empty_userid_warn(conn);

    ctx->state = LOGIN_STATE_USERID;
    draw_userid_prompt(conn, NULL, 0);
    return AUTH_RESULT_RETRY;
}

///////////////////////////////////////////////////////////////////////
// Event callbacks

static struct event ev_sighup, ev_tunnel, ev_ack;

static void 
sighup_cb(int signal, short event, void *arg)
{
    fprintf(stderr, LOG_PREFIX 
            "caught sighup (request to reload) with %u opening fd...\r\n",
            g_opened_fd);
    g_reload_data = 1;
}

static void
stop_g_tunnel()
{
    if (!g_tunnel)
        return;

    close(g_tunnel);
    if (g_async_ack) event_del(&ev_ack);
    g_tunnel = 0;
}

static void
stop_tunnel(int tunnel_fd)
{
    if (!tunnel_fd)
        return;
    if (tunnel_fd == g_tunnel)
        stop_g_tunnel();
    else
        close(tunnel_fd);
}

static void 
endconn_cb(int fd, short event, void *arg)
{
    login_conn_ctx *conn = (login_conn_ctx*) arg;
    if (g_verbose > VERBOSE_INFO) fprintf(stderr, LOG_PREFIX
            "login_conn_remove: removed connection (%s@%s) #%d...",
            conn->ctx.userid, conn->ctx.hostip, fd);

    // remove from ack queue
    if (conn->ctx.state == LOGIN_STATE_WAITACK)
    {
        // it should be already inside.
        ackq_del(conn);
    }

    event_del(&conn->ev);
    bufferevent_free(conn->bufev);
    close(fd);
    g_opened_fd--;
    free(conn);
    if (g_verbose > VERBOSE_INFO) fprintf(stderr, " done.\r\n");
}

static void
endconn_cb_buffer(struct bufferevent * evb, short event, void *arg)
{
    login_conn_ctx *conn = (login_conn_ctx*) arg;

    // "event" for bufferevent and normal event are different
    endconn_cb(EVENT_FD(&conn->ev), 0, arg);
}

static void 
login_conn_remove(login_conn_ctx *conn, int fd, int sleep_sec)
{
    assert(conn->cb == sizeof(login_conn_ctx));
    if (!sleep_sec)
    {
        endconn_cb(fd, EV_TIMEOUT, (void*) conn);
    } else {
        struct timeval tv = { sleep_sec, 0};
        event_del(&conn->ev);
        event_set(&conn->ev, fd, 0, endconn_cb, conn);
        event_add(&conn->ev, &tv);
        if (g_verbose > VERBOSE_INFO) fprintf(stderr, LOG_PREFIX
                "login_conn_remove: stop conn #%d in %d seconds later.\r\n", 
                fd, sleep_sec);
    }
}

static void *
get_tunnel_ack(int tunnel)
{
    void *arg = NULL;

    if (toread(tunnel, &arg, sizeof(arg)) < sizeof(arg) ||
        !arg)
    {
        // sorry... broken, let's shutdown the tunnel.
        if (g_verbose > VERBOSE_ERROR)
            fprintf(stderr, LOG_PREFIX
                    "get_tunnel_ack: tunnel (%d) is broken with arg %p.\r\n", 
                    tunnel, arg);

        stop_tunnel(tunnel);
        return NULL;
    }

    return arg;

}

static void
ack_cb(int tunnel, short event, void *arg)
{
    login_conn_ctx *conn = NULL;

    assert(tunnel);
    if (!(event & EV_READ))
    {
        // not read event (closed? timeout?)
        if (g_verbose > VERBOSE_ERROR) fprintf(stderr, LOG_PREFIX 
                "warning: invalid ack event at tunnel %d.\r\n", tunnel);
        stop_tunnel(tunnel);
        return;
    }

    assert(sizeof(arg) == sizeof(conn));
    conn = (login_conn_ctx*) get_tunnel_ack(tunnel);
    if (!conn)
    {
        if (g_verbose > VERBOSE_ERROR) fprintf(stderr, LOG_PREFIX 
                "warning: invalid ack at tunnel %d.\r\n", tunnel);
        return;
    }

    // some connections may be removed (for example, socket close) before being acked.
    // XXX FIXME if someone created a new connection before ack comes and re-used
    // the memory location of previous destroyed one, we'd have problem here.
    if (!ackq_del(conn))
    {
        if  (g_verbose > VERBOSE_ERROR) fprintf(stderr, LOG_PREFIX 
                "drop abandoned ack connection: %p.\r\n", conn);
        return;
    }

    // check connection
    if (conn->cb != sizeof(login_conn_ctx))
    {
        fprintf(stderr, LOG_PREFIX 
                "warning: received invalid ack from tunnel. abort/reset tunnel?\r\n");
        // assert(conn && conn->cb == sizeof(login_conn_ctx));
        return;
    }

    // reset the state to prevent processing ackq again
    conn->ctx.state = LOGIN_STATE_AUTH;
    // this event is still in queue.
    login_conn_remove(conn, conn->telnet.fd, 0);
}


static int
login_conn_end_ack(login_conn_ctx *conn, void *ack, int fd)
{
    // fprintf(stderr, LOG_PREFIX "login_conn_end_ack: enter.\r\n");

    if (g_async_ack)
    {
        // simply wait for ack_cb to complete
        // fprintf(stderr, LOG_PREFIX "login_conn_end_ack: async mode.\r\n");

        // mark as queued for waiting ack
        conn->ctx.state = LOGIN_STATE_WAITACK;
        ackq_add(conn);

        // set a safe timeout
        login_conn_remove(conn, fd, ACK_TIMEOUT_SEC);

    } else {
        // wait service to complete
        void *rack = NULL;

        // fprintf(stderr, LOG_PREFIX "login_conn_end_ack: sync mode.\r\n");
        if (!g_tunnel)
            return 0;

        rack = get_tunnel_ack(g_tunnel);
        if (!rack)
            return 0;

        if (rack != ack)
        {
            // critical error!
            fprintf(stderr, LOG_PREFIX 
                    "login_conn_end_ack: failed in ack value (%p != %p).\r\n",
                    rack, ack);

            stop_g_tunnel();
            return 0;
        }

        // safe to close.
        login_conn_remove(conn, fd, 0);
    }
    return 1;
}

static void 
client_cb(int fd, short event, void *arg)
{
    login_conn_ctx *conn = (login_conn_ctx*) arg;
    int len, r;
    unsigned char buf[64], ch;
    char *s = (char*)buf;

    // for time-out, simply close connection.
    if (event & EV_TIMEOUT)
    {
        // report timeout conection information --
        if (g_verbose || g_report_timeout)
        {
            time4_t tnow = time(NULL);
            strlcpy((char*)buf, Cdate(&tnow), sizeof(buf));

            fprintf(stderr, LOG_PREFIX
                    "timeout: %-16s [%s -> %s : %-4ds] %08X %s%s\r\n",
                     conn->ctx.hostip,
                     Cdate(&conn->enter),
                     buf,
                     tnow - conn->enter,
                    (unsigned int)conn->ctx.client_code,
                    (conn->ctx.state == LOGIN_STATE_INIT) ? "(*dummy*) " : "",
                     conn->ctx.userid
                   );
        }
        // end report ---------------------------

        endconn_cb(fd, EV_TIMEOUT, (void*) conn);
        return;
    }

    // XXX will this happen?
    if (!(event & EV_READ))
    {
        assert(event & EV_READ);
        return;
    }

    if ( (len = read(fd, buf, sizeof(buf))) <= 0)
    {
        // case to continue:
        if ((len < 0) && (errno == EINTR || errno == EAGAIN))
            return;

        // len == 0: EOF
        // len <  0: any other error.

        // close connection
        login_conn_remove(conn, fd, 0);
        return;
    }

    len = telnet_process(&conn->telnet, buf, len);

    while (len-- > 0)
    {
        int c = vtkbd_process((unsigned char)*s++, &conn->vtkbd);

        if (c == KEY_INCOMPLETE)
            continue;

        if (c == KEY_UNKNOWN)
        {
            // XXX for stupid clients always doing anti-idle, 
            // user will get beeps and have no idea what happened...
            // _mt_bell(conn);
            continue;
        }

        // deal with context
        switch ( login_ctx_handle(&conn->ctx, c) )
        {
            case LOGIN_HANDLE_WAIT:
                break;

            case LOGIN_HANDLE_BEEP:
                _mt_bell(conn);
                break;

            case LOGIN_HANDLE_BS:
                _mt_bs(conn);
                break;

            case LOGIN_HANDLE_REDRAW_USERID:
                if (g_verbose > VERBOSE_DEBUG) fprintf(stderr, LOG_PREFIX
                        "redraw userid: id=[%s], icurr=%d\r\n",
                        conn->ctx.userid, conn->ctx.icurr);
                draw_userid_prompt(conn, conn->ctx.userid, conn->ctx.icurr);
                break;

            case LOGIN_HANDLE_OUTC:
                ch = c;
                _buff_write(conn, &ch, 1);
                break;

            case LOGIN_HANDLE_PROMPT_PASSWD:
                // because prompt would reverse attribute, reset here.
                draw_userid_prompt_end(conn);

#ifdef DETECT_CLIENT
                // stop detection
                conn->telnet.cc_arg = NULL;
#endif
                if (conn->ctx.userid[0])
                {
                    char *uid = conn->ctx.userid;
                    char *uid_lastc = uid + strlen(uid)-1;

                    draw_passwd_prompt(conn);
#ifdef CONVERT
                    // convert encoding if required
                    switch(*uid_lastc)
                    {
                        case '.':   // GB mode
                            conn->ctx.encoding = CONV_GB;
                            *uid_lastc = 0;
                            break;
                        case ',':   // UTF-8 mode
                            conn->ctx.encoding = CONV_UTF8;
                            *uid_lastc = 0;
                            break;
                    }
                    // force to eliminate the extra field.
                    // (backward behavior compatible)
                    uid[IDLEN] = 0;
#endif
                    // accounts except free_auth [guest / new]
                    // require passwd.
                    if (!auth_is_free_userid(uid))
                        break;
                }

                // force changing state
                conn->ctx.state = LOGIN_STATE_AUTH;
                // XXX share start auth, no break here.
            case LOGIN_HANDLE_START_AUTH:
                if ((r = auth_start(fd, conn)) != AUTH_RESULT_RETRY)
                {
                    // for AUTH_RESULT_OK, the connection is handled in
                    // login_conn_end_ack.
                    if (r != AUTH_RESULT_OK)
                        login_conn_remove(conn, fd, AUTHFAIL_SLEEP_SEC);
                    return;
                }
                break;
        }
    }
}

static void 
listen_cb(int lfd, short event, void *arg)
{
    int fd;
    struct sockaddr_in xsin = {0};
    struct timeval idle_tv = { IDLE_TIMEOUT_SEC, 0};
    socklen_t szxsin = sizeof(xsin);
    login_conn_ctx *conn;
    bind_event *pbindev = (bind_event*) arg;

    while ( (fd = accept(lfd, (struct sockaddr *)&xsin, &szxsin)) >= 0 ) {

        // XXX note: NONBLOCK is not always inherited (eg, not on Linux).
        // So we have to set blocking mode for client again here.
        if (g_nonblock) _enable_nonblock(fd);

#ifndef LOGIND_NO_INSCREEN
        // fast draw banner (don't use buffered i/o - this banner is not really important.)
# ifdef INSCREEN
        write(fd, INSCREEN, sizeof(INSCREEN));
# endif // INSCREEN
#endif // !LOGIND_NO_INSCREEN

        if ((conn = malloc(sizeof(login_conn_ctx))) == NULL) {
            close(fd);
            return;
        }
        memset(conn, 0, sizeof(login_conn_ctx));
        conn->cb = sizeof(login_conn_ctx);
        conn->enter = (time4_t) time(NULL);

        if ((conn->bufev = bufferevent_new(fd, NULL, NULL, endconn_cb_buffer, conn)) == NULL) {
            free(conn);
            close(fd);
            return;
        }

        g_opened_fd ++;
        reload_data();
        login_ctx_init(&conn->ctx);

        // initialize telnet protocol
        telnet_ctx_init(&conn->telnet, &telnet_callback, fd);
        telnet_ctx_set_write_arg (&conn->telnet, (void*) conn); // use conn for buffered events
        telnet_ctx_set_resize_arg(&conn->telnet, (void*) &conn->ctx);
#ifdef DETECT_CLIENT
        telnet_ctx_set_cc_arg(&conn->telnet, (void*) &conn->ctx);
#endif
#ifdef LOGIND_OPENFD_IN_AYT
        telnet_ctx_set_ayt_arg(&conn->telnet, (void*) conn); // use conn for buffered events
#endif
        // better send after all parameters were set
        telnet_ctx_send_init_cmds(&conn->telnet);

        // get remote ip & local port info
        inet_ntop(AF_INET, &xsin.sin_addr, conn->ctx.hostip, sizeof(conn->ctx.hostip));
        snprintf(conn->ctx.port, sizeof(conn->ctx.port), "%u", pbindev->port); // ntohs(xsin.sin_port));

        if (g_verbose > VERBOSE_INFO) fprintf(stderr, LOG_PREFIX
                "new connection: fd=#%d %s:%s (opened fd: %d)\r\n", 
                fd, conn->ctx.hostip, conn->ctx.port, g_opened_fd);

        // set events
        event_set(&conn->ev, fd, EV_READ|EV_PERSIST, client_cb, conn);
        event_add(&conn->ev, &idle_tv);

        // check ban here?  XXX can we directly use xsin.sin_addr instead of ASCII form?
        if (g_banned || check_banip(conn->ctx.hostip) )
        {
            // draw ban screen, if available. (for banip, this is empty).
            draw_text_screen (conn, ban_screen);
            login_conn_remove(conn, fd, BAN_SLEEP_SEC);
            return;
        }

        // XXX check system load here.
        if (g_overload)
        {
            draw_overload    (conn, g_overload);
            login_conn_remove(conn, fd, OVERLOAD_SLEEP_SEC);
            return;

        } else {
            draw_text_screen  (conn, welcome_screen);
            draw_userid_prompt(conn, NULL, 0);
        }

        // in blocking mode, we cannot wait accept to return error.
        if (!g_nonblock)
            break;
    }
}

static void 
tunnel_cb(int fd, short event, void *arg)
{
    int cfd;
    if ((cfd = accept(fd, NULL, NULL)) < 0 )
        return;

    // got new tunnel
    fprintf(stderr, LOG_PREFIX "new tunnel established.\r\n");
    _set_connection_opt(cfd);
    _set_tunnel_opt(cfd);

    stop_g_tunnel();
    g_tunnel = cfd;

    if (g_async_ack)
    {
        event_set(&ev_ack, g_tunnel, EV_READ | EV_PERSIST, ack_cb, NULL);
        event_add(&ev_ack, NULL);
    }
}

///////////////////////////////////////////////////////////////////////
// Main 

static int
logattempt_daemon()
{
    int pipe_fds[2];
    int pid;
    logattempt_ctx ctx = {0};

    fprintf(stderr, LOG_PREFIX "forking logattempt daemon...\r\n");
    if (pipe(pipe_fds) < 0)
    {
        perror("pipe");
        return -1;
    }

    pid = fork();
    if (pid < 0)
    {
        perror("fork");
        return -1;
    }

    if (pid != 0)
    {
        g_logattempt_pipe = pipe_fds[1];
        close(pipe_fds[0]);
        return 0;
    }

    // child here.
    g_logattempt_pipe = pipe_fds[0];
    close(pipe_fds[1]);
    setproctitle(MY_SVC_NAME " [logattempts]");

    // TODO change to batched processing
    while (toread(g_logattempt_pipe, &ctx, sizeof(ctx)) == sizeof(ctx))
    {
        if (ctx.cb != sizeof(ctx))
        {
            fprintf(stderr, LOG_PREFIX "broken pipe. abort.\r\n");
            break;
        }

        logattempt(ctx.userid, '-', ctx.logtime, ctx.hostip);
    }

    exit(0);
}
static int 
bind_port(int port)
{
    char buf[STRLEN];
    int sfd;
    bind_event *pev = NULL;

    snprintf(buf, sizeof(buf), "*:%d", port);

    fprintf(stderr, LOG_PREFIX "binding to port: %d...", port);
    if ( (sfd = tobindex(buf, LOGIND_SOCKET_QLEN, _set_bind_opt, 1)) < 0 )
    {
        fprintf(stderr, LOG_PREFIX "cannot bind to port: %d. abort.\r\n", port);
        return -1;
    }

    // only set non-blocking to out ports (not for tunnel!)
    if (g_nonblock)
        _enable_nonblock(sfd);

    pev = malloc  (sizeof(bind_event));
    memset(pev, 0, sizeof(bind_event));
    assert(pev);

    pev->port = port;
    event_set(&pev->ev, sfd, EV_READ | EV_PERSIST, listen_cb, pev);
    event_add(&pev->ev, NULL);
    fprintf(stderr,"ok. \r\n");
    return 0;
}

static int 
parse_bindports_conf(FILE *fp, 
        char *tunnel_path, int sz_tunnel_path,
        char *tclient_cmd, int sz_tclient_cmd
        )
{
    char buf [STRLEN*3], vprogram[STRLEN], vport[STRLEN], vtunnel[STRLEN];
    int bound_ports = 0;
    int iport = 0;

    // format: [ vprogram port ] or [ vprogram tunnel path ]
    while (fgets(buf, sizeof(buf), fp))
    {
        if (sscanf(buf, "%s%s", vprogram, vport) != 2)
            continue;
        if (strcmp(vprogram, MY_SVC_NAME) != 0)
            continue;

        if (strcmp(vport, "client") == 0)
        {
            // syntax: client command-line$
            if (*tclient_cmd)
            {
                fprintf(stderr, LOG_PREFIX
                        "warning: ignored configuration file due to specified client command: %s\r\n",
                        tclient_cmd);
                continue;
            }
            if (sscanf(buf, "%*s%*s%[^\n]", vtunnel) != 1 || !*vtunnel)
            {
                fprintf(stderr, LOG_PREFIX "incorrect tunnel client configuration. abort.\r\n");
                exit(1);
            }
            if (g_verbose) fprintf(stderr, "client: %s\r\n", vtunnel);
            strlcpy(tclient_cmd, vtunnel, sz_tclient_cmd);
            continue;
        }
        else if (strcmp(vport, "client_retry") == 0)
        {
            // syntax: client_retry command-line$
            if (*g_retry_cmd)
            {
                fprintf(stderr, LOG_PREFIX
                        "warning: ignored configuration file due to specified retry command: %s\r\n",
                        g_retry_cmd);
                continue;
            }
            if (sscanf(buf, "%*s%*s%[^\n]", vtunnel) != 1 || !*vtunnel)
            {
                fprintf(stderr, LOG_PREFIX "incorrect retry client configuration. abort.\r\n");
                exit(1);
            }
            if (g_verbose) fprintf(stderr, "client_retry: %s\r\n", vtunnel);
            strlcpy(g_retry_cmd, vtunnel, sizeof(g_retry_cmd));
            continue;
        }
        else if (strcmp(vport, "logfile") == 0)
        {
            // syntax: logfile file$
            if (*g_logfile_path)
            {
                fprintf(stderr, LOG_PREFIX
                        "warning: ignored configuration file due to specified log: %s\r\n",
                        g_logfile_path);
                continue;
            }
            if (sscanf(buf, "%*s%*s%s", vtunnel) != 1 || !*vtunnel)
            {
                fprintf(stderr, LOG_PREFIX "incorrect tunnel configuration. abort.\r\n");
                exit(1);
            }
            if (g_verbose) fprintf(stderr, "log_file: %s\r\n", vtunnel);
            strlcpy(g_logfile_path, vtunnel, sizeof(g_logfile_path));
            continue;
        }
        else if (strcmp(vport, "tunnel") == 0)
        {
            if (*tunnel_path)
            {
                fprintf(stderr, LOG_PREFIX
                        "warning: ignored configuration file due to specified tunnel: %s\r\n",
                        tunnel_path);
                continue;
            }
            if (sscanf(buf, "%*s%*s%s", vtunnel) != 1 || !*vtunnel)
            {
                fprintf(stderr, LOG_PREFIX "incorrect tunnel configuration. abort.\r\n");
                exit(1);
            }
            if (g_verbose) fprintf(stderr, "tunnel: %s\r\n", vtunnel);
            strlcpy(tunnel_path, vtunnel, sz_tunnel_path);
            continue;
        }

        iport = atoi(vport);
        if (!iport)
        {
            fprintf(stderr, LOG_PREFIX "warning: unknown settings: %s\r\n", buf);
            continue;
        }

        if (bind_port(iport) < 0)
        {
            fprintf(stderr, LOG_PREFIX "cannot bind to port: %d. abort.\r\n", iport);
            exit(3);
        }
        bound_ports++;
    }
    return bound_ports;
}

int 
main(int argc, char *argv[], char *envp[])
{
    int     ch, port = 0, bound_ports = 0, tfd, as_daemon = 1;
    FILE   *fp;
    char tunnel_path[PATHLEN] = "", tclient_cmd[PATHLEN] = "";
    const char *config_file = FN_CONF_BINDPORTS;
    struct event_base *evb;
    struct rlimit r = {.rlim_cur = LOGIND_MAX_FDS, .rlim_max = LOGIND_MAX_FDS};

    Signal(SIGPIPE, SIG_IGN);
    initsetproctitle(argc, argv, envp);

    while ( (ch = getopt(argc, argv, "f:p:t:l:r:hvTDdBbAaMm")) != -1 )
    {
        switch( ch ){
        case 'f':
            config_file = optarg;
            break;

        case 'l':
            strlcpy(g_logfile_path, optarg, sizeof(g_logfile_path));
            break;

        case 'p':
            if (optarg) port = atoi(optarg);
            break;

        case 't':
            strlcpy(tunnel_path, optarg, sizeof(tunnel_path));
            break;

        case 'r':
            strlcpy(g_retry_cmd, optarg, sizeof(g_retry_cmd));
            break;

        case 'd':
            as_daemon = 1;
            break;

        case 'D':
            as_daemon = 0;
            break;

        case 'a':
            g_async_ack = 1;
            break;

        case 'A':
            g_async_ack = 0;
            break;

        case 'b':
            g_nonblock = 1;
            break;

        case 'B':
            g_nonblock = 0;
            break;

        case 'm':
            g_async_logattempt = 1;
            break;
            
        case 'M':
            g_async_logattempt = 0;
            break;

        case 'v':
            g_verbose++;
            break;

        case 'T':
            g_report_timeout = 1;
            break;

        case 'h':
        default:
            fprintf(stderr,
                    "usage: %s [-vTmMaAbBdD] [-l log_file] [-f conf] [-p port] [-t tunnel] [-c client_command]\r\n", argv[0]);
            fprintf(stderr, 
                    "\t-v:    provide verbose messages\r\n"
                    "\t-T:    provide timeout connection info\r\n"
                    "\t-d/-D: do/not enter daemon mode (default: %s)\r\n"
                    "\t-a/-A: do/not use asynchronous service ack (deafult: %s)\r\n"
                    "\t-b/-B: do/not use non-blocking socket mode (default: %s)\r\n"
                    "\t-m/-M: do/not use asynchronous logattempts (default: %s)\r\n"
                    "\t-f: read configuration from file (default: %s)\r\n", 
                    as_daemon   ? "true" : "false",
                    g_async_ack ? "true" : "false",
                    g_nonblock  ? "true" : "false",
                    g_async_logattempt  ? "true" : "false",
                    BBSHOME "/" FN_CONF_BINDPORTS);
            fprintf(stderr, 
                    "\t-l: log meesages into log_file\r\n"
                    "\t-p: bind (listen) to specific port\r\n"
                    "\t-t: create tunnel in given path\r\n"
                    "\t-c: spawn a (tunnel) client after initialization\r\n"
                    "\t-r: the command to retry spawning clients\r\n"
                   );
            return 1;
        }
    }

    if (setrlimit(RLIMIT_NOFILE, &r) < 0)
    {
        fprintf(stderr, LOG_PREFIX "warning: cannot increase max fd to %u...\r\n", LOGIND_MAX_FDS);
    }

    chdir(BBSHOME);
    attach_SHM();

    if (g_async_logattempt && logattempt_daemon() < 0)
    {
        fprintf(stderr, LOG_PREFIX "error: cannot fork to handle logattempts.\r\n");
        return 5;
    }

    reload_data();
    evb = event_init();
    event_base_priority_init(evb, MY_EVENT_PRIORITY_NUMBERS);

    // bind ports
    if (port && bind_port(port) < 0)
    {
        fprintf(stderr, LOG_PREFIX "cannot bind to port: %d. abort.\r\n", port);
        return 3;
    }
    if (port)
        bound_ports++;

    // bind from port list file
    if( NULL != (fp = fopen(config_file, "rt")) )
    {
        bound_ports += parse_bindports_conf(fp, 
                tunnel_path, sizeof(tunnel_path),
                tclient_cmd, sizeof(tclient_cmd));
        fclose(fp);
    }

    if (!bound_ports)
    {
        fprintf(stderr, LOG_PREFIX "error: no ports to bind. abort.\r\n");
        return 4;
    }
    if (!*tunnel_path)
    {
        fprintf(stderr, LOG_PREFIX "error: must assign one tunnel path. abort.\r\n");
        return 4;
    }

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

    // create tunnel
    fprintf(stderr, LOG_PREFIX "creating tunnel: %s...", tunnel_path);
    if ( (tfd = tobindex(tunnel_path, 1, _set_bind_opt, 1)) < 0)
    {
        fprintf(stderr, LOG_PREFIX "cannot create tunnel. abort.\r\n");
        return 2;
    }
    fprintf(stderr, "ok.\r\n");
    event_set(&ev_tunnel, tfd, EV_READ | EV_PERSIST, tunnel_cb, &ev_tunnel);
    event_add(&ev_tunnel, NULL);

    // daemonize!
    if (as_daemon)
    {
        char *logfile_path = NULL;
        if (g_logfile_path[0]) logfile_path = g_logfile_path;
        fprintf(stderr, LOG_PREFIX "start daemonize\r\n");
        daemonize(BBSHOME "/run/logind.pid", logfile_path);

        // because many of the libraries used in this daemon (for example,
        // passwd / logging / ...) all assume cwd=BBSHOME,
        // let's workaround them.
        chdir(BBSHOME);
    }

    // Some event notification mechanisms don't work across forks (e.g. kqueue)
    event_reinit(evb);

    // SIGHUP handler is reset in daemonize()
    signal_set(&ev_sighup, SIGHUP, sighup_cb, &ev_sighup);
    signal_add(&ev_sighup, NULL);

    // spawn tunnel client if specified.
    if (*tclient_cmd)
    {
        int r;
        fprintf(stderr, LOG_PREFIX "invoking client...\r\n");
        // XXX this should NOT be a blocking call.
        r = system(tclient_cmd);
        if (g_verbose)
            fprintf(stderr, LOG_PREFIX "client return value = %d\r\n", r);
    }

    // warning: after daemonize, the directory was changed to root (/)...
    {
        time4_t tnow = time(0);
        fprintf(stderr, LOG_PREFIX "%s: start event dispatch.\r\n",
                Cdate(&tnow));
    }
    event_dispatch();

    return 0;
}

// vim:et