忍者ブログ
やったことの記録 主にlinuxとかperlとか
プロフィール
HN:
隠居SE
性別:
非公開
カテゴリー
P R
ブログ内検索
忍者カウンター
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

個人でサーバー内DBを使ってゴニョゴニョする程度だったら、
Sqlite3が一番軽いしメンテも楽だし...ってことでずっとSqlite3使ってたのですが、
ネットワーク越しにDB閲覧しようと思ったら、
Sqlite3だと極端に重くなってしまいました。

原因がSqlite3にあるのかWebDAVにあるのかは不明ですが、
ちゃんとしたDBエンジンを使うタイミングかと思い、
現役時代ぶりにPostgresをインストールしてみました。

インストールや設定は難なく終了し、
データの取り込みも行い、
さて、同じサーバー上のcgiからアクセスするかと言う段になって、
何故か繋がらない(- -;ウーム
同じcgiでバッチならちゃんと動くのに。

と言うことでログを漁ると、

httpd/error_log
[Fri Sep 21 12:33:03 2018] [error] DBI connect('dbname=foo','',...) failed: could not connect to server: Permission denied\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? at /usr/share/perl5/foo/DBI.pm line 125\nCompilation failed in require at /home/foo/html/view.cgi line 9.\nBEGIN failed--compilation aborted at /home/foo/html/view.cgi line 9.\n
と言うわけで、アクセス権がない模様。

考えられる原因は3つ。
ひとつはpostgresの設定。
しかし、postgresのログには何も出ていないので、postgresに到達できていないと考えるべき。

次に考えられるのはiptables。しかし、
sysconfig/iptables
-A INPUT -i lo -j ACCEPT
と言う具合に、iptablesの設定はローカルアクセスを制限していないので、これも違う。

と言うことは、3つ目の可能性であるSELinuxのポリシー制限。
と言うわけでログを確認すると、
audit.log
type=AVC msg=audit(1537500783.022:18112): avc:  denied  { connectto } for  pid=3214 comm="httpd" path="/var/run/postgresql/.s.PGSQL.5432" scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:system_r:initrc_t:s0 tclass=unix_stream_socket
と言うことで、どうやらpostgresのsocketにアクセス権がないらしい。

事前に、ApacheからDBにアクセスするための設定として、
[root@foo foo]# setsebool -P httpd_can_network_connect_db 1
と言うコマンドを入れておいたのですが、不十分だったようです。

と言うわけで、以下のように設定を生成して追加。
[root@foo foo]# cat /var/log/audit/audit.log | grep postgres | audit2allow -M postgres
[root@foo foo]# semodule -i postgres.pp
以下のような設定が生成されたようです。
module postgres 1.0;

require {
        type postgresql_db_t;
        type httpd_t;
        type initrc_t;
        class unix_stream_socket connectto;
        class dir read;
        class file getattr;
}

#============= httpd_t ==============
allow httpd_t initrc_t:unix_stream_socket connectto;
allow httpd_t postgresql_db_t:dir read;
allow httpd_t postgresql_db_t:file getattr;

これで一応、アクセスができるようになりました。 めでたしめでたし。
PR
Powerd by NINJAブログ / Designed by SUSH
Copyright © 隠居SEの備忘録 All Rights Reserved.
忍者ブログ/[PR]