openssl・opensshインストール(2001.11.23)

 opensshによる通信はtelnetを暗号化したものと考えるとわかりやすい。それだけの機能ではないが
多く利用されている形態がtelnetの代替手段であると思われる。#全機能はこちら
 opensshを使うためにはopensslとzlibをインストールしなければいけない。
※必須ではないがtcp_wrappers7.6もインストールしておくとアクセス制御に便利なので
今回はtcp_wrppersもインストールすることにする。

最近のopensshにはsftpコマンドが含まれるようになったので
ファイル転送も暗号化をすることができるようになる。インストールも設定もそう難しくはない。
 最近のtelnetはセキュリティホールがありセキュリティパッチが
あたってないものもあるのでこの際opensshに移行してしまうことをお勧めする。
今回はtelnetの代わりにopensshを利用する目的でインストールする。

【準備】
●opensshのダウンロード
ftp://ftp.kddlabs.co.jp/OpenBSD/OpenSSH/portable/
から最新版をダウンロード
※-engine-という名前のソースも同じく最新版であるが
 これは外部暗号化デバイスをサポートしているということらしいが今回は
 そういった使い方はしないのでダウンロードする必要はない。

※セキュリティホールもあるので必ず最新を使いましょう。


●opensslのダウンロード
http://www.openssl.org/source/
から最新版をダウンロード
※セキュリティホールがあるのでOpenSSL0.9.6以上使いましょう。

●zlibのダウンロード
http://www.gzip.org/zlib/
から最新版をダウンロードする

●tcp_wrappersのダウンロード
ftp://ftp.porcupine.org/pub/security/
から最新版をダウンロードする。
※IPv6版もあるのでIPv6対応を考えるならこちらを選択するとよい。

●prngd
ftp://ftp.aet.tu-cottbus.de/pub/postfix_tls/related/prngd/
から最新版をダウンロードする。
※なくてもopensshはインストールはできるが
  /dev/randomがないのでWarningが出る。

WARNING: you are using the builtin random number collection service.
Please read WARNING.RNG and request that your OS vendor includes
/dev/random in future versions of their OS.

とういようなものである。

※Solaris8でも112438のパッチを当てていれば
特にprngd入れなくてもいいですけどね。
このパッチで/dev/randomが使えるようになります。
入れてもおくと後でほかに使い道があるかもしれないので勉強と思って入れてみるのもいいかも・・・
http://www.cosy.sbg.ac.at/~andi/SUNrand/
であればsolaris全システムに対応した/dev/randomがパッケージで提供されています。

【インストール】
●openssl
Configureオプションは展開したソースの中のREADME参照
今回はデフォルトでMakefileを作成します。

# ./config 2>&1 |tee config.log
# make 2>&1 | tee make.log
# make install 2>&1 | tee make_install.log
以上でopensslが/usr/local/ssl/binにインストールされます。

●zlib
# ./configure 2>&1 | tee configure.log
# make 2>&1 |tee make.log
# make install 2>&1 | tee make_install.log
以上で/usr/local/libにlibz.aのライブラリ
および/usr/local/includeにzlib.hとzconf.hのヘッダーがインストールされます。

●tcp_wrappers
# gzip -d < tcp_wrappers_7.6-ipv6.1.tar.gz | tar xf -
# cd tcp_wrappers_7.6-ipv6.1
# make sunos5 CC=gcc REAL_DAEMON_DIR=/usr/local/sbin \
  LIBS="-lsocket -lnsl" FACILITY=LOG_LOCAL5 \
  IPV6=-DHAVE_IPV6 2>&1 | tee make.log

# cp tcpd tcpdchk tcpdmatch safe_finger try-from /usr/local/sbin/
  ※もし/usr/local/sbinがなかったら#mkdir -p /usr/local/sbinで作ります
# cp libwrap.a /usr/local/lib/
# cp tcpd.h /usr/local/include/

●prngd
# gzip -d < prngd-0.9.23.tar.gz | tar xf -
# cd prngd-0.9.23
# make CC=gcc CFLAGS="-O -DSOLARIS" SYSLIBS=-lsocket -lnsl 2>&1|tee make.log
# cp prngd /usr/local/sbin/
# cp contrib/Solaris-7/prngd.conf.solaris-7 /etc/prngd.conf
  ※Solaris8でもSolaris7ディレクトリのものを使う!
# cat /var/adm/messages /var/log/syslog > /etc/prngd-seed
# /usr/local/sbin/prngd /var/run/egd-pool
# ls -l /var/run/egd-pool

srwxrwxrwx 1 root other 0 Jan 14 18:09 /var/run/egd-pool

ここまで終わったらテストをしてみたいがegc.plというperlスクリプトを
使用することになるので、このパッケージをダウンロードする。
http://www.lothar.com/tech/crypto/
からegd-x.x.tar.gzを取得するし適当な場所に解凍する。
perlが必然的に必要になるのでインストールしておくこと!

# gzip -d < egd-0.8.tar.gz | tar xf -
# cd egd-0.8/eg
# perl egc.pl /var/run/egd-pool get

16823 bits of entropy in pool

# perl egc.pl /var/run/egd-pool read 255

got 255 bytes of entropy: 6c15143062051629963b9bb563c96b8685a47e581d196ccca28da5f
40d8c6e84e4e55b620bb11cc713ff3f43bac1c363ffdc26f40b5daedb38d7c1823202105818252f32
0d58e6ca9c2aa33f7cd5974adf40cf77cc3410e9672b13587bf6f906cc1c84ffc701834fee756aa57
14c8a1ecbe28069ca39e8f150afa6fd427795808ee36ef48f4f0075d66aaa8a9d4e0ca171e453035c
65007a6218cdf525da7dc46dd50e5ceaa61bee721b53fdd3ba3ce32f87bee37aa559bc5d30271e87f
174c03648b75204e67f86445e2740dcab4fc57d716d48de50ef65103db10201da870a28941c956a8c
489a0f28529bc01d0cbc17af0bb93d33fcd1ad5e8c588cf456


●openssh
もしもprivsepを有効にするのであれば先にprivsep用のユーザを追加しておきます。
# groupadd sshd
# useradd -g sshd -c 'sshd privsep' -d /var/empty -s /bin/false sshd
# mkdir /var/empty;chown root:sys /var/empty;chmod 755 /var/empty
privsepしないなら別に関係ないので飛ばしてください。

# ./configure --with-tcp-wrappers \
         --with-zlib=/usr/local/lib \
         --with-ssl-dir=/usr/local/ssl \
         --with-privsep-user=sshd \
         --with-privsep-path=/var/empty \
         --with-4in6 \
         --with-pid-dir=/var/run \
         --disable-utmp \
         --disable-wtmp \
         --with-ipaddr-display 2>&1 | tee configure.log

※solaris8でインストールする場合はconfigureに「--disable-utmp --disable-wtmp」もつけておくといいでしょう。
# make 2>&1 | tee make.log
# make install 2>&1 | tee make_install.log

最後になぜかちゃんとsshdユーザがあるのにないって言ってくるのですが
ちゃんとidコマンドでユーザの表示もされているので特に問題ないです。
しかも、後の方でprivsepの動作も確認できています。

id sshd || \
echo "WARNING: Privilege separation user \"sshd\" does not exist"
uid=10005(sshd) gid=20003(sshd) groups=20003(sshd)

↑この部分のcheck-userのルーチンのやり方ってこれでええのかなあ?
Makefileの中のcheck-userってところで走ってるみたいです。
idでユーザが引けてる分には問題ないでしょう。

【設定】
●opensshの設定
※設定ファイルはデフォルトで/usr/local/etc/sshd_config

# vi /usr/local/etc/sshd_config

Port 22
#Protocol 2,1   
#Ver2プロトコルをデフォルトにするならコメントをはずす
HostKey /usr/local/etc/ssh_host_key
HostKey /usr/local/etc/ssh_host_rsa_key
HostKey /usr/local/etc/ssh_host_dsa_key
KeyRegenerationInterval 3600
ServerKeyBits 768
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 600
PermitRootLogin no #rootでログインできるようにするにはyesにする
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
RhostsAuthentication no
IgnoreRhosts yes
RhostsRSAAuthentication no
HostbasedAuthentication no
PasswordAuthentication yes
PermitEmptyPasswords no
UsePrivilegeSeparation yes #privsep使わないならコメントアウト
X11Forwarding no
X11DisplayOffset 10
PrintMotd yes   
#OSのバージョンなどログイン時に出さないようにするにはno
KeepAlive yes
Subsystem sftp /usr/local/libexec/sftp-server


●tcp_wrappersの設定
# vi /etc/hosts.allow

sshd:192.168.128. : allow


# vi /etc/hosts.deny

all:all:deny


●ユーザのsshキーの作成
bash-2.05$ ssh-keygen -t rsa1 -f ~nitta/.ssh/identity -N "xxxxx"←パスフレーズ

Generating public/private rsa1 key pair.
Your identification has been saved in /export/home/nitta/.ssh/identity.
Your public key has been saved in /export/home/nitta/.ssh/identity.pub.
The key fingerprint is:
94:99:c3:95:4e:eb:88:26:76:cb:ea:42:f7:02:29:ab nitta@sakura

キー作成が終了するとホームディレクトリに.sshというディレクトリができる。
bash-2.05$ ls ~nitta/.ssh/

identity identity.pub prng_seed

identity:秘密鍵
identity.pub:公開鍵


先ほど作成した時のパスワードを使ってログインするためには
公開鍵と同じ内容のものをauthorized_keysというファイルを作成する。

bash-2.05$ cat identity.pub > authorized_keys
bash-2.05$ chmod 600 authorized_keys identity identity.pub
そして秘密鍵(identity)をフロッピーにでも入れて持ってきて
ttssh.exeを起動して図1のように秘密鍵(identity)を指定します。
そしてログインのパスワードはキーを作成したときのパスフレーズ


図1

★ttssh
TeraTermProの拡張機能でTeraTermからsshを使えるようにするための
アプリケーションである。最近は図のように日本語が対応しているので
ここからTeraTermProとttsshをまずインストールをしていただきたい。


【起動】
●opensshの起動確認
# /usr/local/sbin/sshd -f /usr/local/etc/sshd_config
これでエラーがあっても特に見た目にはわからないので。
# ps -ef |grep sshd
デーモンが起動していることを確認してください。

もし、起動時にオプションをつけて起動したい場合のオプション一覧は
# MANPATH=/usr/local/man:/usr/man;export MANPATH;man sshd
を実行してみてください。

# ls -l /var/run/
でsshd.pidファイルができていることを確認。
※pidファイルの場所を変更してコンパイルしている場合は読み替えてください。

tcp_wrappers設定確認
# /usr/local/sbin/tcpdmatch sshd 192.168.128.3
grantedになれば許可されている。
※inetdにないというエラーが出ますがopensshにtcpwrapersを組み込みで
コンパイルしているので特に関係なし。

# /usr/local/sbin/tcpdmatch sshd 131.248.64.1
これがdeniedになれば許可されていない。


【起動ファイル】
起動ファイルを作成しておきます。
# vi /etc/init.d/sshd

#!/bin/sh

case "$1" in
start)
# Start daemons.
if [ ! -f /var/run/sshd.pid ]; then
echo "Starting SSHD: \n"
##↓prngd使わないときはコメントアウト
/usr/local/sbin/prngd /var/run/egd-pool
/usr/local/sbin/sshd -f /usr/local/etc/sshd_config > /dev/null 2>&1
else
echo "process sshd allready running!\n"
exit 1
fi
;;
stop)
# Stop daemons.
if [ -f /var/run/sshd.pid ]; then
echo "Shutting down SSHD: \n"
kill -15 `cat /var/run/sshd.pid`
##↓prngd使わないときはコメントアウト
/usr/bin/rm /var/run/egd-pool
else
echo "process sshd not running\n" > /dev/null 2>&1
exit 1
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit 0


# chown root:sys /etc/init.d/sshd
# ln -s /etc/init.d/sshd /etc/rc2.d/S99sshd
# chmod 744 /etc/init.d/sshd

一度起動と停止を試してね。

※privsepについて

それからPrivsepしてるときの様子は以下のような感じ。

nitta 12359 12356 0 23:39:42 ? 0:00 /usr/local/sbin/sshd -f /usr/local/etc/sshd_config
root 12356 12337 0 23:39:38 ? 0:00 /usr/local/sbin/sshd -f /usr/local/etc/sshd_config
root 12337 1 2 23:39:26 ? 0:01 /usr/local/sbin/sshd -f /usr/local/etc/sshd_config

一番目の行がprivsepされてる様子がわかります。
12337のプロセスで接続をリスンして12356がprivのモニタープロセス。
そして12359がログインしてきたユーザの所有プロセスになっているのがわかると思います。
インストールして実際にやってみるとすぐにわかると思います。




以上でtelnetの代替としては終わりとするが、本当にopensshを使い込むと
X11のフォワーディングなどいろいろとセキュアに使い込むことができる。
あとはopensshの公式サイトやmanファイルなどを参考に設定していけばいいでしょう。
sshd_configに関する全項目はman sshdを見ればかなり詳細に記されている。
またデフォルトでは存在しないファイルで、もう少しいろんな制御やログイン時にスクリプトを実行させたり
といったことも可能になる。


【make installでエラーになる時】
# make install 2>&1|tee make_instal.log

if test ! -z ""; then \
/usr/local/bin/perl ./fixprogs ssh_prng_cmds ; \
fi
(cd openbsd-compat && make)
make[1]: Entering directory `/usr/local/src/ssh/openssh-3.8p1/openbsd-compat'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/usr/local/src/ssh/openssh-3.8p1/openbsd-compat'
(cd scard && make DESTDIR= install)
make[1]: Entering directory `/usr/local/src/ssh/openssh-3.8p1/scard'
../mkinstalldirs /usr/local/share
/usr/local/bin/install -c -m 0644 ./Ssh.bin /usr/local/share/Ssh.bin
make[1]: Leaving directory `/usr/local/src/ssh/openssh-3.8p1/scard'
./mkinstalldirs /usr/local/bin
./mkinstalldirs /usr/local/sbin
./mkinstalldirs /usr/local/man
./mkinstalldirs /usr/local/share
./mkinstalldirs /usr/local/man/man1
./mkinstalldirs /usr/local/man/man5
./mkinstalldirs /usr/local/man/man8
./mkinstalldirs /usr/local/libexec
(umask 022 ; ./mkinstalldirs /usr/local/var/empty)
/usr/local/bin/install -c -m 0755 -s ssh /usr/local/bin/ssh
BFD: /usr/local/bin/st2Nayf2: warning: allocated section `.interp' not in segment
/usr/local/bin/install -c -m 0755 -s scp /usr/local/bin/scp
BFD: /usr/local/bin/stagaOf2: warning: allocated section `.interp' not in segment
/usr/local/bin/install -c -m 0755 -s ssh-add /usr/local/bin/ssh-add
BFD: /usr/local/bin/stqya4f2: warning: allocated section `.interp' not in segment
/usr/local/bin/install -c -m 0755 -s ssh-agent /usr/local/bin/ssh-agent
BFD: /usr/local/bin/stX5aig2: warning: allocated section `.interp' not in segment
/usr/local/bin/install -c -m 0755 -s ssh-keygen /usr/local/bin/ssh-keygen
BFD: /usr/local/bin/stFDayg2: warning: allocated section `.interp' not in segment
/usr/local/bin/install -c -m 0755 -s ssh-keyscan /usr/local/bin/ssh-keyscan
BFD: /usr/local/bin/stJ.aOg2: warning: allocated section `.interp' not in segment
/usr/local/bin/install -c -m 0755 -s sshd /usr/local/sbin/sshd
BFD: /usr/local/sbin/stX2a4g2: warning: allocated section `.interp' not in segment
if test ! -z "" ; then \
/usr/local/bin/install -c -m 0755 -s ssh-rand-helper /usr/local/libexec/ssh-rand-helper ; \
fi
/usr/local/bin/install -c -m 4711 -s ssh-keysign /usr/local/libexec/ssh-keysign
BFD: /usr/local/libexec/stLFaqh2: warning: allocated section `.interp' not in segment
/usr/local/bin/install -c -m 0755 -s sftp /usr/local/bin/sftp
BFD: /usr/local/bin/stKXaGh2: warning: allocated section `.interp' not in segment
/usr/local/bin/install -c -m 0755 -s sftp-server /usr/local/libexec/sftp-server
BFD: /usr/local/libexec/st67aWh2: warning: allocated section `.interp' not in segment
/usr/local/bin/install -c -m 644 ssh.1.out /usr/local/man/man1/ssh.1
/usr/local/bin/install -c -m 644 scp.1.out /usr/local/man/man1/scp.1
/usr/local/bin/install -c -m 644 ssh-add.1.out /usr/local/man/man1/ssh-add.1
/usr/local/bin/install -c -m 644 ssh-agent.1.out /usr/local/man/man1/ssh-agent.1
/usr/local/bin/install -c -m 644 ssh-keygen.1.out /usr/local/man/man1/ssh-keygen.1
/usr/local/bin/install -c -m 644 ssh-keyscan.1.out /usr/local/man/man1/ssh-keyscan.1
/usr/local/bin/install -c -m 644 sshd_config.5.out /usr/local/man/man5/sshd_config.5
/usr/local/bin/install -c -m 644 ssh_config.5.out /usr/local/man/man5/ssh_config.5
/usr/local/bin/install -c -m 644 sshd.8.out /usr/local/man/man8/sshd.8
if [ ! -z "" ]; then \
/usr/local/bin/install -c -m 644 ssh-rand-helper.8.out /usr/local/man/man8/ssh-rand-helper.8 ; \
fi
/usr/local/bin/install -c -m 644 sftp.1.out /usr/local/man/man1/sftp.1
/usr/local/bin/install -c -m 644 sftp-server.8.out /usr/local/man/man8/sftp-server.8
/usr/local/bin/install -c -m 644 ssh-keysign.8.out /usr/local/man/man8/ssh-keysign.8
rm -f /usr/local/bin/slogin
ln -s ./ssh /usr/local/bin/slogin
rm -f /usr/local/man/man1/slogin.1
ln -s ./ssh.1 /usr/local/man/man1/slogin.1
if [ ! -d /usr/local/etc ]; then \
./mkinstalldirs /usr/local/etc; \
fi
Generating public/private rsa1 key pair.
Your identification has been saved in /usr/local/etc/ssh_host_key.
Your public key has been saved in /usr/local/etc/ssh_host_key.pub.
The key fingerprint is:
c7:49:e8:64:c9:bc:85:fd:e4:f8:9a:99:43:0e:5b:05 root@solaris26
Generating public/private dsa key pair.
Your identification has been saved in /usr/local/etc/ssh_host_dsa_key.
Your public key has been saved in /usr/local/etc/ssh_host_dsa_key.pub.
The key fingerprint is:
69:b2:57:75:31:75:36:71:9f:18:ac:4d:aa:3a:6e:d5 root@solaris26
Generating public/private rsa key pair.
Your identification has been saved in /usr/local/etc/ssh_host_rsa_key.
Your public key has been saved in /usr/local/etc/ssh_host_rsa_key.pub.
The key fingerprint is:
95:76:e0:b2:1b:da:54:77:c1:45:62:d8:9f:34:5a:00 root@solaris26
/usr/local/sbin/sshd -t -f /usr/local/etc/sshd_config
make: *** [check-config] Killed

一度だけこんなエラーが出ました。
これが出た時はsolaris2.6でコンパイルした時です。
bash2.05# PATH=/usr/ccs/bin:$PATH export PATH
として/usr/ccs/binのldを使うことでmake installは成功しました。