Apache2.2系でデータベースを使ったユーザ認証(mod_authn_mysql)
かなりはまった上、あんまり情報が見つからなかったので、メモがてら。
ApacheでのBasic認証といえば、.htpasswdファイルで設定するのが定番だけどDBを使って認証することもできる
今回は、MySQLで設定するべく環境を構築した
yumインストールでのインストール方法はよくあるみたいで引っかかるのだがソースからインストールしている人が全然いない
yumでインストールしたいのはやまやまなんだけど、ソースからしろということなので...結局3日くらい悩んだYO
mod_auth_mysqlインストール
環境
CentOS6
MySQL5.6.16 ソースインストール済み
Apache2.2.26 ソースインストール済み
PHP5.5.10 ソースインストール済み
ソースGET
3.0.0が最新版。最新版といっても2005-08-04のもの。今2014年なので10年近く前なんですけど...
枯れている技術なのか使わない方がいいのか分からんが、どれだけ探してもこれしかないのでしょうがなくダウンロード
# wget http://downloads.sourceforge.net/project/modauthmysql/modauthmysql/3.0.0/mod_auth_mysql-3.0.0.tar.gz
いざインストール
解凍したフォルダ内のBUILDファイルを見ると、
apxs -c -L/usr/lib/mysql -I/usr/include/mysql -lmysqlclient -lm -lz mod_auth_mysql.c
みたいなコマンドを打てと書いてある。パスを適切に書き換えて、インストールする...が、エラーが大量に出た
# tar xvfz mod_auth_mysql-3.0.0.tar.gz
# cd mod_auth_mysql-3.0.0
# /usr/local/apache2/bin/apxs -c -L/usr/local/mysql/lib -I/usr/local/mysql/include -lmysqlclient -lm -lz mod_auth_mysql.c
/usr/local/apache2/build/libtool --silent --mode=compile gcc -prefer-pic -DLINUX -D_REENTRANT -D_GNU_SOURCE -g -O2 -pthread -I/usr/local/apache2/include -I/usr/local/apache2/include -I/usr/local/apache2/include -I/usr/local/mysql/include -c -o mod_auth_mysql.lo mod_auth_mysql.c && touch mod_auth_mysql.slo
mod_auth_mysql.c:591: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:595: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:599: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:603: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:607: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:611: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:615: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:619: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:623: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:627: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:631: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:635: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:639: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:643: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:651: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:655: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:659: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:663: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:667: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:671: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c: In function 'format_request':
mod_auth_mysql.c:947: warning: pointer/integer type mismatch in conditional expression
apxs:Error: Command failed with rc=65536
ググってみると、どうやらApache2.2用パッチがあるらしい
Apache2.2用パッチをあてる
これをあててから再度インストール
# wget http://sourceforge.net/p/modauthmysql/patches/_discuss/thread/efda3a06/c5a7/attachment/apache22.diff
# patch -p0 < apache22.diff
# /usr/local/apache2/bin/apxs -c -L/usr/local/mysql/lib -I/usr/local/mysql/include -lmysqlclient -lm -lz mod_auth_mysql.c
# /usr/local/apache2/bin/apxs -i mod_auth_mysql.la
エラーでなくなった。
無事に成功した模様。
httpd.confの設定
httpd.confにLoadModuleを追加
# vi /usr/local/apache2/conf/httpd.conf
動くかと思いきや失敗
# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Syntax error on line 54 of /usr/local/apache2/conf/httpd.conf: Cannot load /usr/local/apache2/modules/mod_auth_mysql.so into server: /usr/local/apache2/modules/mod_auth_mysql.so: symbol mysql_select_db, version libmysqlclient_18 not defined in file libmysqlclient.so.18 with link time reference
[FAILED]
うーん、ライブラリがちゃんとリンクしてないみたい
# ldd /usr/local/apache2/modules/mod_auth_mysql.so
libmysqlclient.so.18 => /usr/lib64/mysql/libmysqlclient.so.18
libmysqlclient.so.18が変なところ見てるなぁ...
誰かが yum install mysql した跡があるので、その時にはいったっぽい?
ソースからインストールしている人があまりいなくて半日ほどぐるぐる悩む...
ようやく http://d.hatena.ne.jp/natsumesouxx/20111126/1322339821 が引っかかった
ldconfigで一時的に探索パスを追加
# ldconfig /usr/local/mysql/lib
再度インストール作業やりなおし
無事正しいパスでインストールされた
# ldd /usr/local/apache2/modules/mod_auth_mysql.so
libmysqlclient.so.18 => /usr/local/mysql/lib/libmysqlclient.so.18
動くかな?
# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
やっと動いたー!お疲れ!
MySQLで認証用のテーブル作る
mysql> CREATE DATABASE auth_db;
mysql> USE auth_db;
mysql> CREATE TABLE users (user CHAR(32) NOT NULL,passwd CHAR(64) NOT NULL,PRIMARY KEY (user));
mysql> GRANT SELECT ON auth_db.users TO authuser@localhost IDENTIFIED BY 'PaSsW0Rd';
mysql> INSERT INTO users VALUES ('user_name', ENCRYPT('user_password'));
Query OK, 1 row affected (0.01 sec)
mysql> \q
この辺は情報も多いので特に悩まず...
暗号化方式何にしようかな?くらい
md5やsha1は古いらしいので、今ならcryptがよいらしい
参考:http://php.net/manual/ja/faq.passwords.php
httpd.confで認証設定
# vi /usr/local/apache2/conf/httpd.conf
下記記述を適当な位置に追加(一番最後とか)
<Directory /usr/local/apache2/htdocs/test_login>
AuthName "MySQL authenticated zone"
AuthType Basic
AuthMYSQLEnable on
AuthMySQLUser authuser
AuthMySQLPassword PaSsW0Rd
AuthMySQLDB auth_db
AuthMySQLUserTable users
AuthMySQLNameField user
AuthMySQLPasswordField passwd
require valid-user
</Directory>
Apache再起動してブラウザからアクセス
httpd://xxx.xxx.xxx.xxx/test_login/
ベーシック認証の表示がされる→認証OK
...となるはずがちゃんと動かない!!!!!
エラーログを見ると下記のようなエラーが出ている
# vi /usr/local/apache2/logs/error_log
[Fri Mar 28 10:56:45 2014] [error] [client xxx.xxx.xxx.xxx] AuthUserFile not specified in the configuration
グーグル検索でひっかかった下記を参考に、再度設定
<Directory /usr/local/apache2/htdocs/test_login>
AuthBasicAuthoritative off
AuthMySQLAuthoritative On
AuthUserFile /dev/null
AuthName "MySQL authenticated zone"
AuthType Basic
AuthMYSQLEnable on
AuthMySQLUser authuser
AuthMySQLPassword PaSsW0Rd
AuthMySQLDB auth_db
AuthMySQLUserTable users
AuthMySQLNameField user
AuthMySQLPasswordField passwd
require valid-user
</Directory>
太字部分が必要らしい
Apache再起動して、再度アクセス
→OK!お疲れ!