IT pass HikiWiki - [ITPASS2011]tcpserverのインストールと設定 Diff

  • Added parts are displayed like this.
  • Deleted parts are displayed like this.

{{toc}}

[((<ITPASSサーバ構築ドキュメント>)) へ戻る]

= tcpserver のインストールと設定

== inetd からの qmail 起動を停止

テストのために記述していた /etc/inetd.conf の smtpで始まる行をコメントアウトする. そして, inetdを再起動する.

  # /etc/init.d/openbsd-inetd restart

smtp ポートを listen しているプログラムがないことを netstat -l で確認する.

  # netstat -l | grep smtp

listen しているプログラムがある場合は以下のように表示される.
  tcp        0      0 *:smtp                  *:*                     LISTEN

== ソースの取得と展開, パッチ当てとmake

参考: ((<URL:http://cyberam.dip.jp/linux_server/mail/qmail_main.html>))

  # wget http://tools.qmail.jp/ucspi-tcp/ucspi-tcp-0.88.tar.gz
  # wget http://qmail.org/moni.csi.hu/pub/glibc-2.3.1/ucspi-tcp-0.88.errno.patch
  # tar xfz ucspi-tcp-0.88.tar.gz
  # cd ucspi-tcp-0.88
  # patch  -p1 < ../ucspi-tcp-0.88.errno.patch

インストール先を /usr/local/ucspi-tcp に変更する.
すなわち, conf-home の一行目を /usr/local/ucspi-tcp に書き換える.

  # make
  # make setup check

インストールした qmail の実行ファイル群へパスを通す.

ここで ((<[ITPASS2011]パスの設定>)) の((* <一般ユーザ用コマンドのパス> *)) に ((*/usr/local/ucspi-tcp/bin*)) を追加する.
(システム用コマンドも一般ユーザ用コマンドと同じ場所にインストールされているため, システム用コマンド用の設定を別途行う必要は無い).

== 起動テスト

使い方は

  # tcpserver
  
とすると出力される. 詳しくは((<URL:http://cr.yp.to/ucspi-tcp/tcpserver.html>))に書いてある.

qmaildのUID, nofilesのGIDを確認する.

  $ id qmaild

uid=116(qmaild) gid=200(nofiles) groups=200(nofiles)

以下を実行し, tcpserverを起動する.

   # tcpserver -u xxxx -g yyyy 0 smtp /var/qmail/bin/qmail-smtpd &
  
ここで, xxxx は qmaild の UID, yyyy は nofiles の GID である.

メール受信テストを ((<[ITPASS2011]qmailのインストールと設定#smtp(仮)>)) を参考にやってみる. すなわち, 他のホストから自分のメールアドレスに宛ててメールを送り, 受信できる事を確認する.

== 起動スクリプト作成

起動スクリプトを /etc/init.d/tcpserver に作成する.
中身は ((<tcpserver 起動スクリプト|URL:../epa/tcpserver/tcpserver1>))
を参照に作成する.

  # cd /etc/init.d/
  # touch tcpserver
  # chown root:root tcpserver
  # chmod 755 tcpserver
  # vi tcpserver

さらに, vi などで /etc/init.d/tcpserver の NAME=tcpserver の上に以下が追記する.

  ### BEGIN INIT INFO
  # Provides: tcpserver
  # Required-Start: $remote_fs
  # Required-Stop: $remote_fs
  # Should-Start: $network $syslog
  # Should-Stop: $network $syslog
  # Default-Start: 2 3 4 5
  # Default-Stop: 0 1 6
  # Short-Description: Start and stop tcpserver
  # Description: tcpserver is tcpserver
  ### END INIT INFO

確認後, update-rc.d スクリプトでランレベルごとの設定を行う.

  # update-rc.d tcpserver defaults

== ホスト制限設定

smtp 中継を許可するホストを指定する.
((<URL:http://man.qmail.jp/faq/faq5.html#5.4.>))を参考に
ホストリストファイル tcp.smtp を作成し, そのファイルを元に CDB データ
ベースファイルを作成する.

/usr/local/ucspi-tcp/etc の下に tcp.smtp を作成し, を以下を書く.
   133.30.XXX.XXX:allow,RELAYCLIENT=""
   127.:allow,RELAYCLIENT=""

1 行目の 133.30.XXX.XXX は後で設定確認するための作業用である. 作業者がアカウントを持つ他のマシンの IP アドレスを記入する.
次に以下のコマンドを実行して, tcp.smtp.cdb を作る.

  # tcprules tcp.smtp.cdb tcp.smtp.tmp < tcp.smtp

== ホスト制限確認

/etc/init.d/tcpserver を修正された起動スクリプト
((<tcpserver 起動スクリプト修正版|URL:../epa/tcpserver/tcpserver2>))
に置き換える.

tcpserver プロセスを再起動する.

  # /etc/init.d/tcpserver restart

このときに, 以下のようなレスポンスが返ってくる.

  Restarting tcpserver
  Stopping smtp server: tcpserver
  start-stop-daemon: warning: failed to kill 17014: No such process
  1 pids were not killed
  No process in pidfile `/var/run/tcpserver.pid' found running; none  killed.
  Starting smtp server: tcpserver

先ほど /usr/local/ucspi-tcp/etc/tcp.smtp に書いた IP アドレス(ここでは, 133.30.109.22) を持つ計算機 (ika) から telnet でアクセスし, メールサーバ以外のホストへのメール送信を試みる. (下記のコマンドについては ((<URL:http://man.qmail.jp/jinstall/test.receive.html>)) 参照). プロン
プト入力時には, 左端に '>' を記してある. 実際には表示されない.

  % telnet tako-itpass.scitec.kobe-u.ac.jp 25
  Trying 133.30.109.21...
  Connected to tako-itapss.scitec.kobe-u.ac.jp.
  Escape character is '^]'.
  220 tako-itpass.scitec.kobe-u.ac.jp ESMTP
  > helo dude
  250 domain
  > mail <hoge1_at_stu.kobe-u.ac.jp>
  250 ok
  > rcpt <hoge2_at_stu.kobe-u.ac.jp>   # <- 実在のアドレスへ
  (アドレスを, <>で囲むのを忘れずに)
  250 ok
  > data
  354 go ahead
  > Subject: testing
  >
  > This is a test.
  > .
  250 ok 812345679 qp 12345
  > quit
  221 domain
  Connection closed by foreign host.
  %

このメールが届くことを確認する.

次に, /usr/local/ucspi-tcp/etc/tcp.smtp から133.30.110.XX:allow,RELAYCLIENT="" を削除して tcprules コマンドを実行し, tcpserver を restart した後に同様の作業を行う.

この際には rcpt <hogehoge_at_stu.kobe-u.ac.jp> を入力した段階で

  553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)

というエラーメッセージが返り, メールの送信を拒絶することを確認する.

== ホスト制御データベース更新スクリプト

tcprules などのコマンドを覚えておくのは面倒なので,
/usr/local/ucspi-tcp/etc/ 以下に更新スクリプトを置いてある.
ika から USB 経由で tako コピーした.
  # chmod u+x tcp_smtp_update.sh
tcprules を覚えているというのであれば, 以下は必要ない.

/usr/local/ucspi-tcp/etc/tcp.smtp を更新後は

  $ /usr/local/ucspi-tcp/etc/tcp_smtp_update.sh update

でデータベースファイルが更新され, tcpserver プロセスが再起動する.

ここで, 以下のようなレスポンスが返ってくる場合がある.

  /bin/bash^M: bad interpreter: そのようなファイルやディレクトリはありません

これは、Windowsマシンでシェルスクリプトを書いて Linux サーバにそのままアップロードして利用しようとすると発生してしまう.
原因は、Windows の改行コードと Linux の改行コードが異なることにあるので, 改行コードを変更する.

上のスクリプトを実行する.

  # /usr/local/ucspi-tcp/etc/tcp_smtp_update.sh update

以下のように表示されれば成功である.

  Updating /usr/local/ucspi-tcp/etc/tcp.smtp.cdb using /usr/local/ucspi-tcp/etc/tcp.smtp
  /usr/local/ucspi-tcp/bin/tcprules /usr/local/ucspi-tcp/etc/tcp.smtp.cdb /usr/local/ucspi-tcp/etc/tcp.smtp.tmp
  /etc/init.d/tcpserver restart
  Restarting tcpserver
  Stopping smtp server: tcpserver
  Starting smtp server: tcpserver
  
  Update is successful.

更新したデータベースの内容に応じて正しくメールが送れていることを確認できたら OK.

== 参考資料

* ((<[ITPASS2010]tcpserverのインストールと設定>))
* ((<[Memo2010][ITPASS] tako 構築作業ログ 8 (MAIL1)>))
* ((<europa's free space|URL:http://europa.cocolog-nifty.com/space/2011/07/qpopperv410mkdi.html>))

[((<ITPASSサーバ構築ドキュメント>)) へ戻る]