Digest認証
@IT:Apacheでユーザー認証を行うには(Digest認証編)を見ながら遊んでみる。
認証エリアの作成
# mkdir /var/www/leader
Apacheの設定ファイル編集
# vi /etc/apache2/sites-available/default(↓を追記) <Directory "/var/www/leader/"> AuthType Digest AuthName "Secret Zone" AuthDigestDomain /leader/ AuthDigestFile /etc/apache2/sites-available/.htdigest Require user valid-user </Directory>
認証ユーザの作成
# htdigest -c /etc/apache2/sites-available/.htdigest "Secret Zone" jobs Adding password for jobs in realm Secret Zone. New password: (apple) Re-type new password: (apple)
Apacheを再起動
うぉ!なぜ?
# /etc/init.d/apache2 restart * Restarting web server apache2 * We failed to correctly shutdown apache, so we're now killing all running apache processes. This is almost certainly suboptimal, so please make sure your system is working as you'd expect now! Syntax error on line 25 of /etc/apache2/sites-enabled/000-default: Invalid command 'AuthDigestDomain', perhaps misspelled or defined by a module not included in the server configuration [fail]
調査
Apacheのプロセスを確認。メッセージのとおり、確かにkillされている。
# ps -ef | grep apache | grep -v grep (何も表示されず)
メッセージにあるファイルを確認。
# ls -l /etc/apache2/sites-enabled/000-default lrwxrwxrwx 1 root root 36 2009-03-20 20:49 /etc/apache2/sites-enabled/000-default -> /etc/apache2/sites-available/default
シンボリックリンクされたファイルを確認。
# ls -l /etc/apache2/sites-available/default -rw-r--r-- 1 root root 1521 2009-04-03 00:39 /etc/apache2/sites-available/default
シンボリックリンクされたファイルの中(line 25)を確認。さっきの編集内容に問題がありそう。
# echo "set number" >> ~/.exrc ←viエディタ上に行番号表示 # vi /etc/apache2/sites-available/default (省略) 22 <Directory /var/www/leader> 23 AuthType Digest 24 AuthName "Secret Zone" 25 AuthDigestDomain /leader/ 26 AuthUserFile /etc/apache2/sites-available/.htdigest 27 Require user valid-user 29 </Directory> (省略)
類似事例とその対処法を検索したところ、Apacheのバージョンによって実現方法が異なることが判明。
本当はドキュメント読んで裏取るのが正解だけど、以下を参考にApacheモジュールの設定を確認。
# ls -l /etc/apache2/mods-enabled | grep auth lrwxrwxrwx 1 root root 33 2009-03-20 20:49 auth_basic.load -> ../mods-available/auth_basic.load lrwxrwxrwx 1 root root 33 2009-03-20 20:49 authn_file.load -> ../mods-available/authn_file.load lrwxrwxrwx 1 root root 36 2009-03-20 20:49 authz_default.load -> ../mods-available/authz_default.load lrwxrwxrwx 1 root root 38 2009-03-20 20:49 authz_groupfile.load -> ../mods-available/authz_groupfile.load lrwxrwxrwx 1 root root 33 2009-03-20 20:49 authz_host.load -> ../mods-available/authz_host.load lrwxrwxrwx 1 root root 33 2009-03-20 20:49 authz_user.load -> ../mods-available/authz_user.load
とりあえず、必須と思われるauth_digest_moduleを追加
# a2enmod Your choices are: actions alias asis auth_basic auth_digest authn_alias authn_anon authn_dbd authn_dbm authn_default authn_file authnz_ldap authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cern_meta cgi cgid charset_lite dav dav_fs dav_lock dbd deflate dir disk_cache dump_io env expires ext_filter file_cache filter headers ident imagemap include info ldap log_forensic mem_cache mime mime_magic negotiation php5 proxy proxy_ajp proxy_balancer proxy_connect proxy_ftp proxy_http rewrite setenvif speling ssl status substitute suexec unique_id userdir usertrack version vhost_alias Module name? auth_digest Module auth_digest installed; run /etc/init.d/apache2 force-reload to enable. # ls -l /etc/apache2/mods-enabled | grep auth_digest lrwxrwxrwx 1 root root 34 2009-04-03 11:37 auth_digest.load -> ../mods-available/auth_digest.load
Apacheの設定ファイル再編集
# vi /etc/apache2/sites-available/default(↓のとおり修正) <Directory "/var/www/leader/"> AuthType Digest AuthName "Secret Zone" AuthDigestProvider file AuthDigestDomain /leader/ AuthUserFile /etc/apache2/sites-available/.htdigest Require user valid-user </Directory>
Apacheを再々(?)起動
# /etc/init.d/apache2 restart * Restarting web server apache2 [ OK ]
サーバ側のコマンドラインから、http://localhost/leader/にアクセス
きたー。レスポンスヘッダが、"WWW-Authenticate: Digest"ってなってる。
# telnet localhost 80 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GET /leader HTTP/1.0 Host:localhost:80 HTTP/1.1 401 Authorization Required Date: Fri, 03 Apr 2009 02:53:23 GMT Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.5 with Suhosin-Patch WWW-Authenticate: Digest realm="Secret Zone", nonce="9iT2pp1mBAA=02e75cb9b54a2551f02e18ae1e97f2edd264b1c4", algorithm=MD5, domain="/leader/", qop="auth" Content-Length: 515 Connection: close Content-Type: text/html; charset=iso-8859-1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Authorization Required</title> </head><body> <h1>Authorization Required</h1> <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.</p> <hr> <address>Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.5 with Suhosin-Patch Server at localhost Port 80</address> </body></html> Connection closed by foreign host.