如何增加更多 pty 1. 簡介 由於 Phoenix BBS (Eagle BBS) 非 client/server 架構, 所有 操作必須在 login pty 上. Sun-OS 可支援 256 ptys. 此數字足夠正常使用. 但對於一些大站而言並不夠. 以下對 Sun-OS 提供大於 256 pty 的作法: 1. patch kernel 2. 找尋 telnetd, rlogind source 並 patch 之. 3. 利用多餘的 pty 加速 tty search. 對於平常同時 login user 在 100 以上的站而言, tty search 是 一項負擔. 假使有 100 個 on line user, 平均 一次成功 login 必須 search 50 個 tty, 最差 100 次. 2. 以下介紹 patch Sun-OS kernel 之法. 由此將對 pty 有更深入 了解. Step 1. cd /sys/os ------- sed -e 's/NPTY/NNTY/g' -e 's/pty\.h/nty\.h/g' \ -e 's/pty_softc/nty_softc/g' -e 's/npty/nnty/g' \ tty_ptyconf.c > tty_ntyconf.c Step 2. cd /sys/sun, edit conf.c, ------- add the following lines before the array "struct cdevsw cdevsw[]". #include "nty.h" #if NNTY > 0 extern struct streamtab ntsinfo; extern int ntcopen(), ntcclose(), ntcread(), ntcwrite(), ntcioctl(); extern int ntcselect(); #define ntstab &ntsinfo #else #define ntstab 0 #define ntcopen nodev #define ntcclose nodev #define ntcread nodev #define ntcwrite nodev #define ntcioctl nodev #define ntcselect nodev #endif add more array element to "struct cdevsw cdevsw[]". I suggest to use element "108" and "109" (they will be the major number of the extra tty and pty pair). , { nodev, nodev, nodev, nodev, /*108*/ nodev, nodev, nodev, 0, ntstab, 0, }, { ntcopen, ntcclose, ntcread, ntcwrite, /*109*/ ntcioctl, nulldev, ntcselect, 0, 0, 0, } Step 3. ------- cd /sys/`arch -k`/OBJ sed 's/NPTY/NNTY/g' pty.h > nty.h perl -p -e 's/_ptsinfo/_ntsinfo/g;' -e 's/_ptc/_ntc/g;' \ -e 's/_pty_softc/_nty_softc/g' tty_pty.o > tty_nty.o Step 4. ------- add the following line to /sys/conf.common/files.cmn os/tty_nty.c optional nty os/tty_ntyconf.c optional nty Step 5. ------- cd /sys/`arch -k`/conf cp GENERIC BBS-NTY add the following line to BBS-NTY pseudo-device nty256 # pseudo-tty's, also needed for SunView if you find the line pseudo-device pty # pseudo-tty's, also needed for SunView change it into pseudo-device pty256 # pseudo-tty's, also needed for SunView 當然, 至少, 加 maxusers ?? into maxusers 64 Step 6. ------- cd /sys/`arch -k`/conf set path=(/bin /usr/etc $path) config BBS-NTY cd ../BBS-NTY cat pty.h nty.h (make sure their value are 256) make depend make mv /vmunix /vmunix.orig cp vmunix / fastboot Step 7.0, make sure you have made the first set of 256 PTYs by -------- /dev/MAKEDEV pty4 /dev/MAKEDEV pty5 .... /dev/MAKEDEV pty15 Step 7, run the following perl script ------- #!/usr/local/bin/perl open(NTAB,"> /tmp/ttytab") || die "can't write /tmp/ttytab $!\n"; $ttys = "klmonKLMNOUVWXYZ"; @ttys = split(/ */, $ttys); chdir('/dev'); $offset = 0; foreach $tty (@ttys) { for ($i=0;$i<16;$i++) { $tname = sprintf("tty${tty}%x",$i); $pname = sprintf("pty${tty}%x",$i); print NTAB "$tname none network off secure\n"; system("/usr/etc/mknod $tname c 108 $offset"); system("/usr/etc/mknod $pname c 109 $offset"); $offset ++; } } close(NTAB); cat /tmp/ttytab >> /etc/ttytab Step 8, modify and recompile all commands which will use pty ------- by changing a line with the string "pqrstuvwxyzPQRST" into "klmnopqrstuvwxyzKLMNOPQRSTUVWXYZ" 你必須至少重新 compile rlogind, telnetd 以使用多出的 pty/tty pair. 其它相關使用 pty 者有, screen, script, xterm 等. 1.x 用 loadmod 之探討. 以上用 patch kernel 之法, 應該也可改為用 loadmod, 而無須 patch kernel. 在此略過. (需要一 wrapper, 一 script for making device ) -------------------------------------------------------------------- 3. telnetd/rlogind source in the public domain 建議使用 1. logdaemon (ftp.csie.nctu.edu.tw:/pub/security/logdaemon-2.tar.gz) 2. NetBSD telnetd/rlogind source. (netbsd.csie.nctu.edu.tw:/pub/...) 其中 logdaemon 特別有考慮 tty security 問題. --------------------------------------------------------------------- 4. 利用多餘之 pty 加速 tty search 中央 Totoro 和 交大資科 CIS BBS 都已增加到大於 512 組 tty/pty, 現在 面臨的新問題是 login 人數多時, tty search 速度將變得很慢, 若使用下列簡單的 hashing 方法, 將使 tty/pty pairs 越多 則 tty search 越快: 取 time 做 hash key, 假設有 512 組 tty time_t now; int i,j,p,last; time(&now); now %= 512; for (i = now ; i < now + 512 ; ++i) { line = "/dev/ptyXX"; line[strlen("/dev/pty")] = "klmnopqrstuvwxyzKLMNOPQRSTUVWXYZ"[i % 32]; line[strlen("/dev/ptyp")] = "0123456789abcdef"[(i%512)/32]; if ((p = open(line, O_RDWR)) == -1) continue; ............... } 則第 0-512 秒 login 之 user 將可望在第一次 search 即 hit. tty 數越多 hit 機率越高.