はきだめ

何もありません。

【令和最新版】IRCサーバーを作る手順

【令和最新版】IRCサーバーを作る手順

概要

令和の時代にもなってIRCサーバーを必要とする方向けに、IRCサーバーを作る手順を残しておきます。 SSLに対応させないまま作る手順は他にもググれば出てきますが、時代が時代なのでSSLでの暗号化通信に対応させます。

なお、この手順は実際にIRCサーバーを作った時のhistoryから必要そうなコマンドを抜き出して書いています。 不必要なコマンドとかあるかもしれませんし、何か間違っている・不足しているコマンドもあるかもしれません。 ただ、今からIRCサーバーを作ろうとするような人は、サーバーにも強いはずなので、適宜対応してください。

使用するもの

  • AlmaLinux 8.3

    • CentOSが事実上死亡したので、AlmaLinuxを使用します。
    • RHEL8系なら何でも良いと思います。
  • ngIRCd

    • 何だか導入も手軽らしいので、ngIRCdを使用します。

手順

手順A (dnfでEPELからインストール)

  • dnfでEPELからインストールする手順です。
  • この手順でインストールされるngIRCdは、OpenSSLでなく、GnuTLSを使用しようとします。
# EPEL・ngIRCdインストール
dnf install epel-release
dnf install ngircd

# GnuTLSインストール
dnf install gnutls-devel
dnf install gnutls-utils

# 鍵作成
# ※EPELからインストールされてくるngIRCdは、OpenSSLではなく、GnuTLSを使用します。
#  また、GnuTLSのパスフレーズに対応していません。パスフレーズは設定しないでください。
cd /etc/ssl/
mkdir ngircd
cd ngircd
certtool --generate-privkey --bits 2048 --outfile server-key.pem
certtool --generate-self-signed --load-privkey server-key.pem --outfile server-cert.pem

# 設定バックアップ・変更
# ※設定内容は公式のドキュメントとか参照して適宜変更してください。
cd /etc
cp -p ngircd.conf ngircd.conf.orig
vi ngircd.conf

diff ngircd.conf.orig ngircd.conf
28c28,29
<       Name = irc.example.net
---
>       #Name = irc.example.net
>       Name = localhost.localdomain
42c43
<       Info = Server Info Text
---
>       ;Info = Server Info Text
48c49
<       Listen = 127.0.0.1
---
>       ;Listen = 127.0.0.1
75a77
>       Ports = 平文接続ポート
110a113
>       MaxConnectionsIP = 10
113a117
>       MaxJoins = 0
151a156
>       AllowRemoteOper = yes
162a168
>       CloakHost = %x.cloaked.host 
167a174
>       CloakHostModeX = %x.cloaked.user
171a179
>       CloakHostSalt = 適当な文字列
174a183
>       CloakUserToNick = no
178a188,189
>       ConnectIPv6 = no
>       ConnectIPv4 = no
186a198
>       DNS = no
211a224
>       OperChanPAutoOp = yes
270a284
>       CertFile = /etc/ssl/ngircd/server-cert.pem
281c295,296
<       CipherList = @SYSTEM
---
>       ;CipherList = @SYSTEM
>       CipherList = SECURE128:-VERS-SSL3.0
287a303
>       KeyFile = /etc/ssl/ngircd/server-key.pem
293a310
>       Ports = SSL接続ポート

# 起動・自動起動有効化・状態確認
systemctl start ngircd
systemctl enable ngircd
systemctl status ngircd

手順B (ソースからコンパイル・インストール)

  • 死ぬほど面倒です。やめておいた方が良いです。
# システム全体の暗号化ポリシー変更
# ※これやらないとTLS1.0とかTLS1.1とかで接続出来ません。
update-crypto-policies --set LEGACY

# 念のため今のうちにシステム再起動
reboot

# Gitからクローン
git clone https://github.com/ngircd/ngircd.git

# コンパイルで必要になるものをインストール
dnf install openssl-devel
dnf install pam-devel

# コンパイル・インストール
# ※configure実行時、機能を適宜追加・削除なりしてください。
#  機能は公式のドキュメントとか「./configure --help」で見れます。
cd ngircd/
./autogen.sh
./configure --enable-ipv6 --with-openssl --with-pam --with-iconv
make
make install

# グループ・ユーザー作成
groupadd -r ngircd
adduser -g ngircd -r -d / -s /sbin/nologin ngircd

# OpenSSL鍵作成・所有者・パーミッション変更
cd /etc/ssl/
mkdir ngircd
cd ngircd

openssl req -newkey rsa:2048 -x509 -keyout server-key.pem -out server-cert.pem -days 3650
openssl dhparam -2 -out dhparams.pem 4096

cd /etc/ssl/
chown -R ngircd: ngircd
cd ngircd
chmod 600 *

# 設定バックアップ・変更
# ※設定内容は公式のドキュメントとか参照して適宜変更してください。
#  CipherListは「/etc/crypto-policies/back-ends/opensslcnf.config」の
#  CipherStringの中身をコピペしてきた方が確実です。
cd /usr/local/etc
chown ngircd: ngircd.conf
cp -p ngircd.conf ngircd.conf.orig

vi ngircd.conf
diff ngircd.conf.orig ngircd.conf
28c28
<       Name = irc.example.net
---
>       Name = localhost.localdomain
42c42
<       Info = Server Info Text
---
>       ;Info = Server Info Text
75a76
>       Ports = 平文接続ポート
80a82
>       ServerGID = ngircd
87a90
>       ServerUID = ngircd
106a110
>       MaxConnections = 0
110a115
>       MaxConnectionsIP = 5
113a119
>       MaxJoins = 0
128a135
>       MaxListSize = 250
151a159
>       AllowRemoteOper = yes
162a171
>       CloakHost = %x.cloaked.host
167a177
>       CloakHostModeX = %x.cloaked.user
171a182
>       CloakHostSalt = 適当な文字列
174a186
>       CloakUserToNick = no
178a191,192
>       ConnectIPv6 = no
>       ConnectIPv4 = no
186a201
>       DNS = no
191a207
>       Ident = no
220a237
>       PAM = no 
264c281
< ;[SSL]
---
> [SSL]
270a288
>       CertFile = /etc/ssl/ngircd/server-cert.pem
279a298
>       CipherList = @SECLEVEL=1:kEECDH:kRSA:kEDH:kPSK:kDHEPSK:kECDHEPSK:!DES:!RC2:!IDEA:-SEED:!eNULL:!aNULL:!MD5:-SHA384:-CAMELLIA:-ARIA:-AESCCM8
282a302
>       DHFile = /etc/ssl/ngircd/dhparams.pem
285a306
>       KeyFile = /etc/ssl/ngircd/server-key.pem
288a310
>       KeyFilePassword = 設定したパスフレーズ
291a314
>       Ports = SSL接続ポート

# systemdのユニットファイル設置
cp -p /root/ngircd/contrib/ngircd.service /etc/systemd/system/

# ユニットファイル内、UserとGroupを作成したユーザー・グループに修正。
# また、ExecStartを実際にプログラムのパスに修正して保存する。
vi /etc/systemd/system/ngircd.service
# ngIRCd systemd service unit.
# See systemd(1), systemd.unit(5), systemd.service(5), systemd.exec(5).

[Unit]
Description=Next Generation IRC Daemon
Documentation=man:ngircd(8) man:ngircd.conf(5) https://ngircd.barton.de
After=network.target
Wants=anope.service atheme.service irc-services.service
Wants=bopm.service
Before=anope.service atheme.service irc-services.service
Before=bopm.service

[Service]
Type=forking
User=ngircd
Group=ngircd
# Settings & limits:
CapabilityBoundingSet=CAP_SYS_CHROOT CAP_NET_BIND_SERVICE
MemoryDenyWriteExecute=yes
NoNewPrivileges=yes
PrivateDevices=yes
PrivateTmp=yes
ProtectControlGroups=yes
ProtectHome=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
ProtectSystem=full
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
RestrictRealtime=yes
RuntimeDirectory=ircd
RuntimeDirectoryMode=750
# Try to load "default files" from any Debian package variant to keep this
# unit generic.
EnvironmentFile=-/etc/default/ngircd
EnvironmentFile=-/etc/default/ngircd-full
EnvironmentFile=-/etc/default/ngircd-full-dbg
# Start ngIRCd. Note: systemd doesn't allow to use $DAEMON here!
ExecStart=/usr/local/sbin/ngircd $PARAMS
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target

# systemdのユニットファイル変更反映
systemctl daemon-reload

# 起動・自動起動有効化・状態確認
systemctl start ngircd
systemctl enable ngircd
systemctl status ngircd

# gitでクローンしてきたフォルダの後片付け
cd ~
rm -rf ngircd/

手順Aで起動しないとき

  • まずは /var/log/messages を確認してみてください。
Apr  2 00:21:30 localhost ngircd[2279]: [2279:5    0] ngIRCd 26-IPv6+IRCPLUS+PAM+SSL+SYSLOG+ZLIB-x86_64/redhat/linux-gnu starting ...
Apr  2 00:21:30 localhost ngircd[2279]: [2279:6    0] Using configuration file "/etc/ngircd.conf" ...
Apr  2 00:21:30 localhost ngircd[2279]: [2279:3    0] Can't read MOTD file "/etc/ngircd.motd": No such file or directory
Apr  2 00:21:30 localhost ngircd[2279]: [2279:4    0] No administrative information configured but required by RFC!
Apr  2 00:21:30 localhost ngircd[2279]: [2279:4    0] Ignoring SSL "KeyFilePassword": Not supported by GnuTLS.
Apr  2 00:21:30 localhost ngircd[2279]: [2279:3    0] Failed to set certificate key file (cert /etc/ssl/ngircd/server-cert.pem, key /etc/ssl/ngircd/server-key.pem): Decryption has failed.
Apr  2 00:21:30 localhost ngircd[2279]: [2279:3    0] Error during SSL initialization!
Apr  2 00:21:30 localhost ngircd[2279]: [2279:1    0] Fatal: Initialization failed, exiting!
Apr  2 00:21:30 localhost systemd[1]: ngircd.service: Main process exited, code=exited, status=1/FAILURE
Apr  2 00:21:30 localhost systemd[1]: ngircd.service: Failed with result 'exit-code'.
Apr  2 00:21:30 localhost systemd[1]: ngircd.service: Service RestartSec=100ms expired, scheduling restart.
Apr  2 00:21:30 localhost systemd[1]: ngircd.service: Scheduled restart job, restart counter is at 5.
Apr  2 00:21:30 localhost systemd[1]: ngircd.service: Start request repeated too quickly.
Apr  2 00:21:30 localhost systemd[1]: ngircd.service: Failed with result 'exit-code'.
Apr  2 01:06:19 localhost ngircd[7836]: [7836:5    0] ngIRCd 26-IPv6+IRCPLUS+PAM+SSL+SYSLOG+ZLIB-x86_64/redhat/linux-gnu starting ...
Apr  2 01:06:19 localhost ngircd[7836]: [7836:6    0] Using configuration file "/etc/ngircd.conf" ...
Apr  2 01:06:19 localhost ngircd[7836]: [7836:3    0] Can't read MOTD file "/etc/ngircd.motd": No such file or directory
Apr  2 01:06:19 localhost ngircd[7836]: [7836:4    0] No administrative information configured but required by RFC!
Apr  2 01:06:19 localhost ngircd[7836]: [7836:4    0] DHFile not set, generating 2048 bit DH parameters. This may take a while ...
Apr  2 01:06:20 localhost ngircd[7836]: [7836:3    1] Failed to set certificate key file (cert /etc/ssl/ngircd/server-cert.pem, key /etc/ssl/ngircd/server-key.pem): Base64 decoding error.
Apr  2 01:06:20 localhost ngircd[7836]: [7836:3    1] Error during SSL initialization!
Apr  2 01:06:20 localhost ngircd[7836]: [7836:1    1] Fatal: Initialization failed, exiting!
Apr  2 01:06:20 localhost systemd[1]: ngircd.service: Main process exited, code=exited, status=1/FAILURE
Apr  2 01:06:20 localhost systemd[1]: ngircd.service: Failed with result 'exit-code'.
Apr  2 01:06:20 localhost systemd[1]: ngircd.service: Service RestartSec=100ms expired, scheduling restart.
Apr  2 01:06:20 localhost systemd[1]: ngircd.service: Scheduled restart job, restart counter is at 5.
Apr  2 01:06:20 localhost systemd[1]: ngircd.service: Start request repeated too quickly.
Apr  2 01:06:20 localhost systemd[1]: ngircd.service: Failed with result 'exit-code'.
  • 上記のような場合、以下を確認してください。
    • ngircd.conf内のCipherListが「SECURE128:-VERS-SSL3.0」となっていることを確認。
    • ngircd.conf内のKeyFilePasswordを設定していないことを確認。
    • 鍵をGnuTLSで作成していることを確認。

手順Bで起動しないとき

  • /var/log/messages を眺めてがんばってください