この問題を解決する方法

この問題を解決する方法

このためのユニットファイルを作成したいのですが、stunnel失敗の理由はわかりません。

私のstunnel.confは次のようになります。

#Provide the full path to your certificate-key pair file

cert = /etc/pki/tls/certs/stunnel.pem

#lock the process into a chroot jail

chroot = /var/run/stunnel

# and create the PID file in this jail

pid = /stunnel.pid

#change the UID and GID of the process for security reasons
setuid = nobody
setgid = nobody

#enable client mode
client = yes


socket = l:TCP_NODELAY=1
#socket = r:TCP:NODELAY=1

[mysqls]
accept = 127.0.0.1:3306
connect = 10.0.0.3:3307

実行するとstunnel /etc/stunnel/stunnel.conf動作します。

これは私のStunnelシステムデバイスファイルです。

[Unit]
;Description=SSL tunnel for network daemons
;Documentation=man:stunnel https://www.stunnel.org/docs.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target
Alias=stunnel.target

[Service]
Type=forking
User=nobody
Group=nobody
RuntimeDirectory=stunnel
ExecStartPre=-/usr/bin/mkdir /var/run/stunnel
ExecStartPre=-/user/bin/chown nobody:nobody /var/run/stunnel
ExecStart=/bin/stunnel /etc/stunnel/stunnel.conf
ExecStop=/bin/killall -9 stunnel

起動しようとするとsystemctl start mystunnel.service失敗します。

Job for mystunnel.service failed because the control process exited with error code. See "systemctl status mystunnel.service" and "journalctl -xe" for details.

実装するjournalctl -xe

Feb 20 19:26:07 otrs1 polkitd[610]: Registered Authentication Agent for unix-process:14179:2643087 (system bus name :1.62 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, loc
Feb 20 19:26:07 otrs1 systemd[1]: Starting mystunnel.service...
-- Subject: Unit stunnel-otrs.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mystunnel.service has begun starting up.
Feb 20 19:26:07 otrs1 mkdir[14185]: /usr/bin/mkdir: cannot create directory ‘/var/run/stunnel’: File exists
Feb 20 19:26:07 otrs1 systemd[14186]: Failed at step EXEC spawning /user/bin/chown: No such file or directory
-- Subject: Process /user/bin/chown could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- The process /user/bin/chown could not be executed and failed.
--
-- The error number returned by this process is 2.
Feb 20 19:26:08 otrs1 stunnel[14188]: Clients allowed=500
Feb 20 19:26:08 otrs1 stunnel[14188]: stunnel 4.56 on x86_64-redhat-linux-gnu platform
Feb 20 19:26:08 otrs1 stunnel[14188]: Compiled/running with OpenSSL 1.0.1e-fips 11 Feb 2013
Feb 20 19:26:08 otrs1 stunnel[14188]: Threading:PTHREAD Sockets:POLL,IPv6 SSL:ENGINE,OCSP,FIPS Auth:LIBWRAP
Feb 20 19:26:08 otrs1 stunnel[14188]: Reading configuration from file /etc/stunnel/stunnel.conf
Feb 20 19:26:08 otrs1 stunnel[14188]: FIPS mode is enabled
Feb 20 19:26:08 otrs1 stunnel[14188]: Compression not enabled
Feb 20 19:26:08 otrs1 stunnel[14188]: PRNG seeded successfully
Feb 20 19:26:08 otrs1 stunnel[14188]: Initializing service [mysqls]
Feb 20 19:26:08 otrs1 stunnel[14188]: Certificate: /etc/pki/tls/certs/stunnel.pem
Feb 20 19:26:08 otrs1 stunnel[14188]: Error reading certificate file: /etc/pki/tls/certs/stunnel.pem
Feb 20 19:26:08 otrs1 stunnel[14188]: error queue: 140DC002: error:140DC002:SSL routines:SSL_CTX_use_certificate_chain_file:system lib
Feb 20 19:26:08 otrs1 stunnel[14188]: error queue: 20074002: error:20074002:BIO routines:FILE_CTRL:system lib
Feb 20 19:26:08 otrs1 stunnel[14188]: SSL_CTX_use_certificate_chain_file: 200100D: error:0200100D:system library:fopen:Permission denied
Feb 20 19:26:08 otrs1 stunnel[14188]: Service [mysqls]: Failed to initialize SSL context
Feb 20 19:26:08 otrs1 stunnel[14188]: str_stats: 12 block(s), 1050 data byte(s), 696 control byte(s)
Feb 20 19:26:08 otrs1 polkitd[610]: Unregistered Authentication Agent for unix-process:14179:2643087 (system bus name :1.62, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus)
Feb 20 19:26:08 otrs1 systemd[1]: stunnel-otrs.service: control process exited, code=exited status=1
Feb 20 19:26:08 otrs1 systemd[1]: Failed to start stunnel-otrs.service.
-- Subject: Unit stunnel-otrs.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit stunnel-otrs.service has failed.
--
-- The result is failed.
Feb 20 19:26:08 otrs1 systemd[1]: Unit mystunnel.service entered failed state.
Feb 20 19:26:08 otrs1 systemd[1]: mystunnel.service failed.

ディレクトリを作成できないというエラーが発生する理由は理解できませんが(明らかに存在するため)、ディレクトリは作成されません!そしてなぜ証明書を読むことができないのですか?手動で実行するのはなぜですか? SELinuxが無効になっています。

編集する:

df -h

Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root   14G  1.9G   13G  14% /
devtmpfs             234M     0  234M   0% /dev
tmpfs                245M   54M  191M  22% /dev/shm
tmpfs                245M  4.4M  240M   2% /run
tmpfs                245M     0  245M   0% /sys/fs/cgroup
/dev/sda1           1014M  138M  877M  14% /boot
tmpfs                 49M     0   49M   0% /run/user/0

編集2:

ErikFの提案を適用した後、ディレクトリ存在の問題は消えましたが、証明書の読み取りはまだ失敗します。

   Feb 20 20:42:59 otrs1 polkitd[610]: Registered Authentication Agent for unix-process:16232:3104221 (system bus name :1.73 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, loc
    Feb 20 20:42:59 otrs1 systemd[1]: Starting stunnel-otrs.service...
    -- Subject: Unit stunnel-otrs.service has begun start-up
    -- Defined-By: systemd
    -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
    --
    -- Unit stunnel-otrs.service has begun starting up.
    Feb 20 20:42:59 otrs1 stunnel[16239]: Clients allowed=500
    Feb 20 20:42:59 otrs1 stunnel[16239]: stunnel 4.56 on x86_64-redhat-linux-gnu platform
    Feb 20 20:42:59 otrs1 stunnel[16239]: Compiled/running with OpenSSL 1.0.1e-fips 11 Feb 2013
    Feb 20 20:42:59 otrs1 stunnel[16239]: Threading:PTHREAD Sockets:POLL,IPv6 SSL:ENGINE,OCSP,FIPS Auth:LIBWRAP
    Feb 20 20:42:59 otrs1 stunnel[16239]: Reading configuration from file /etc/stunnel/stunnel.conf
    Feb 20 20:42:59 otrs1 stunnel[16239]: FIPS mode is enabled
    Feb 20 20:42:59 otrs1 stunnel[16239]: Compression not enabled
    Feb 20 20:42:59 otrs1 stunnel[16239]: PRNG seeded successfully
    Feb 20 20:42:59 otrs1 stunnel[16239]: Initializing service [mysqls]
    Feb 20 20:42:59 otrs1 stunnel[16239]: Certificate: /etc/pki/tls/certs/stunnel.pem
    Feb 20 20:42:59 otrs1 stunnel[16239]: Error reading certificate file: /etc/pki/tls/certs/stunnel.pem
    Feb 20 20:42:59 otrs1 stunnel[16239]: error queue: 140DC002: error:140DC002:SSL routines:SSL_CTX_use_certificate_chain_file:system lib
    Feb 20 20:42:59 otrs1 stunnel[16239]: error queue: 20074002: error:20074002:BIO routines:FILE_CTRL:system lib
    Feb 20 20:42:59 otrs1 stunnel[16239]: SSL_CTX_use_certificate_chain_file: 200100D: error:0200100D:system library:fopen:Permission denied
    Feb 20 20:42:59 otrs1 stunnel[16239]: Service [mysqls]: Failed to initialize SSL context
    Feb 20 20:42:59 otrs1 stunnel[16239]: str_stats: 12 block(s), 1050 data byte(s), 696 control byte(s)
    Feb 20 20:42:59 otrs1 polkitd[610]: Unregistered Authentication Agent for unix-process:16232:3104221 (system bus name :1.73, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus)
    Feb 20 20:42:59 otrs1 systemd[1]: stunnel-otrs.service: control process exited, code=exited status=1
    Feb 20 20:42:59 otrs1 systemd[1]: Failed to start stunnel-otrs.service.
    -- Subject: Unit stunnel-otrs.service has failed
    -- Defined-By: systemd
    -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
    --
    -- Unit stunnel-otrs.service has failed.
    --
    -- The result is failed.
    Feb 20 20:42:59 otrs1 systemd[1]: Unit stunnel-otrs.service entered failed state.
    Feb 20 20:42:59 otrs1 systemd[1]: stunnel-otrs.service failed.

Any ideas or hints please? 

Appreciate or help! 

ベストアンサー1

この問題を解決する方法

サービスマネージャの下で実行されているサービスは自分自身をデーモン化しようとしないでください。不安定で危険なPIDファイルメカニズムを使用しないでください(ほとんどの一般的な場合)。これらの作業はすべて、サービス管理システムによって正しく実行されます。

stunnelしたがって、このようにコマンドラインで実行すると、プロセスを終了/停止するまでシェルが待機する必要があります。

許可されていないユーザーアカウントのスポンサーとして実行されるすべてのもの、削減されたファイルシステムアクセス設定、および実際に(オープンソケットファイル記述子を継承して使用できるプログラムの場合)、リスニングソケットを開くことはサービス管理の領域です。

これは、システム化された人々が素直にデーモンを実行する「新しいスタイル」方式と呼ぶことです。実際、これは、daemontoolsユーザーが過去20年間にデーモンを実行したと言った方法であり、IBMが過去25年間にデーモンを実行したと言った方法です。

権限のないユーザーアカウントは、nobodyファイルの所有者としてリストしてはいけません。関連サービスには所有権の許可は必要ありません。どのファイルまたはディレクトリ別の言葉したがって、権限のないユーザーアカウントは、この目的のために特別に作成されたアカウントである必要があります。

useradd --shell /usr/bin/true mysql-stunnel-d

systemdで環境を設定することはchroot()実際には簡単ではありません。なぜなら/bin/stunnel、証明書ファイルと設定ファイルに加えて、docoに従って読み取られるすべてのファイルには、/dev/zeroNSS設定システムやタイムゾーンなどのシステムエントリがたくさん含まれているからです。データベース構成が必要なのはBindReadOnlyPathsより簡単な方法ですProtect…

構成ファイル

ソケットはソケット単位で記述されます。プロトコルstunnelを理解していませんが、LISTEN_FDSUCSPI-TCPと互換性があります。したがって、ソケットデバイスは許容ソケットを記述する必要があります。

; /etc/systemd/service/mysql-stunnel.socket
[単位]
説明=MySQL用SSLラッパー
ドキュメント=

[ソケット]
ListenStream=127.0.0.1:mysql
ListenStream=[::1]:mysql
承諾=はい
遅延なし=はい

[インストールする]
WantedBy =マルチユーザー。ターゲット

これはソケットsystemctlここで.thisを使用して有効化/無効化/開始/停止提供する、サービスユニットによって記述され、要求時に自動的に開始されます。ソケットを通して。承認ソケットなので、テンプレートサービス単位です。サービス管理を通じて行われたすべての権限の削除と設定について説明します。

; /etc/systemd/service/[Eメール保護]
[単位]
説明=MySQL用SSLラッパー
ドキュメント=

[提供する]
タイプ=シンプル
user=mysql-stunnel-d
ホームページ保護=はい
保護システム=厳格
PrivateTmp=はい
標準入力=ソケット
標準出力=ソケット
標準エラー = ジャーナル
ExecStart=/bin/stunnel /etc/stunnel/mysql-stunnel.conf

これにより、構成stunnelファイルは処理されません。どのサービス管理コンテンツそのためにしなさい:

# /etc/stunnel/mysql-stunnel.conf
証明書 = /etc/pki/tls/certs/stunnel.pem
顧客=はい
前景=はい
接続=10.0.0.3:3307

ボーナスコンテンツ

はい、それはdaemontools方式です。これら2つのユニットファイルをconvert-systemd-units次のように渡します。Noshツールセット私たちはdaemontools方式でこれを行い、適切なチェーンセットを使用してUCSPIや他のツールをロードする方法を示すrunプログラム(他のいくつかのプログラムと一緒に)を受けました。service

%システム制御変換 systemd-units ./mysql-stunnel.socket
システム単位変換: 警告: ./[Eメール保護]:必須設定:[サービス] StandardError =ログ
Convert-systemd-units: 警告: ./mysql-stunnel.socket: 未使用の設定: [unit] docs =
システム単位変換: 警告: ./[Eメール保護]:未使用設定:[service]標準エラー=ログ
システム単位変換: 警告: ./[Eメール保護]:未使用の設定:[unit]document =
%システム制御 cat./mysql-stunnel
開始: #!/bin/nosh                
start:#./mysql-stunnel.socket で生成されたスタートアップファイル
開始:事実
停止: #!/bin/nosh
stop:#./mysql-stunnel.socket で生成されたファイルを停止します。
停止:事実
実行: #!/bin/nosh
run:#./mysql-stunnel.socket で生成されたファイルを実行します。
実行:#MySQL用SSLラッパー
実行:tcp-socket-listen 127.0.0.1 mysql
実行: tcp-socket-listen "::1" mysql
実行:制御グループ../mysql-stunnel.serviceに移動します。
実行:envuidgid --additional --mysql-stunnel-d
実行:userenv-fromenv
実行:共有のキャンセル - マウント
実行:set-mount-object --recursiveスレーブ/
実行: make-private-fs --temp --homes
実行: make-read-only-fs --os --etc
実行: set-mount-object --recursive share/
実行: setuidgid --supplemental --mysql-stunnel-d
実行:tcp-socket-accept - 遅延なし
実行: ./service
サービス: #!/bin/nosh
service:# ./ によって生成されたサービスファイル[Eメール保護]
サービス:#MySQL用SSLラッパー
サービス:/bin/stunnel /etc/stunnel/mysql-stunnel.conf
再起動: #!/bin/sh
restart:#Restart./生成されたファイル[Eメール保護]
restart:exec false#スクリプトパラメータを無視します。
%                                                                                                                                                                                           

追加読書

おすすめ記事