apache の認証を NTLM 認証にする。

apache の認証を NTLM 認証にして、ActiveDirectoryと連携させます。
まずは、winbind の設定が正しく行えている上まで持って行ってください。
http://d.hatena.ne.jp/rti7743/20110426/1303840756

ntlm_auth の確認

まず、ログインしたいユーザで入れるか確認する。

ntlm_auth --username テストユーザ

必要なソフトウェアのインストール

yum install subversion httpd httpd-devel gcc

mod_auth_ntlm_winbind のソースコードを持ってくる。

svn co svn://svnanon.samba.org/lorikeet/trunk/mod_auth_ntlm_winbind mod_auth_ntlm_winbind

コンパイルしてインストール

cd mod_auth_ntlm_winbind
autoconf
./configure
make
make install

/var/lib/samba/winbindd_privileged のグループ権限をapache にする。

chown root.apache /var/lib/samba/winbindd_privileged

忘れるとエラーになるので注意。

認証テスト用のディレクトリとファイルを作る。

mkdir /var/www/html/ntlm_auth
echo "hello" > /var/www/html/ntlm_auth/index.html

apacheの設定をする

vi /etc/httpd/conf/httpd.conf

一番下に追加

-----------------------------------------------------------------------------
KeepAlive On
LoadModule auth_ntlm_winbind_module modules/mod_auth_ntlm_winbind.so

Alias /ntlm_auth/ "/var/www/html/ntlm_auth/"
<Directory "/var/www/html/ntlm_auth/">
  NTLMAuth on
  AuthType NTLM
  AuthName "NTLM Authentication"
  NTLMAuthHelper "/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp"
  NTLMBasicAuthoritative on
  require valid-user
</Directory>
-----------------------------------------------------------------------------

apache を起動する。

/etc/init.d/httpd restart

ブラウザからアクセスする

http://192.168.56.123/ntlm_auth/

hello というテキストが表示されれば成功。

うまくいかない場合

エラーログを見ます。

less /var/log/httpd/error_log
うまくいかない場合 Permission denied: couldn't spawn child ntlm helper process: /usr/bin/ntlm_auth
[Sun May 08 04:55:09 2011] [error] [client 192.168.56.1] (13)Permission denied: couldn't spawn child ntlm helper process: /usr/bin/ntlm_auth

SELinux を切り忘れていませんか?

getenforce

Enforcing と表示された場合は、 setenforce で無効にしてください。

setenforce 0
うまくいかない場合 winbind client not authorized to use winbindd_pam_auth_crap. Ensure permissions on /var/lib/samba/
winbindd_privileged are set correctly.
[2011/05/08 05:10:42.045204,  0] utils/ntlm_auth.c:598(winbind_pw_check)
  Login for user [KAEDE]\[rti3]@[KAEDE] failed due to [winbind client not authorized to use winbindd_pam_auth_crap. Ensure permissions on /var/lib/samba/winbindd_privileged are set correctly.]
[Sun May 08 05:10:42 2011] [error] [client 192.168.56.1] (20014)Internal error: ntlm_auth reports Broken Helper: BH NT_STATUS_ACCESS_DENIED
[2011/05/08 05:10:42.045642,  0] utils/ntlm_auth.c:888(manage_squid_ntlmssp_request)
  NTLMSSP BH: NT_STATUS_ACCESS_DENIED


/var/lib/samba/winbindd_privileged の権限を確認します。

ls -al /var/lib/samba/winbindd_privileged 


drwxr-x--- 2 root apache 4096  5月  7 04:58 .             << これ root.apache かどうか。
drwxr-xr-x 8 root root   4096  5月  8 04:46 ..
srwxrwxrwx 1 root root      0  5月  7 04:58 pipe

グループ制限

このままでは、ADのユーザ全員が入れてしまうので、所属グループによる制限をかけます。
今回は、 web-allow というグループを作成し、そのグループに所属している人だけが閲覧できるようにしてみます。



認証をかけるために グループの SID を求めます。

wbinfo -n "web-allow"
-------------応答-------------------------------------------------
S-1-5-21-2558067586-3132001088-1936578106-1116 SID_DOM_GROUP (2)
------------------------------------------------------------------

これにより、web-allowグループは、 S-1-5-21-2558067586-3132001088-1936578106-1116 ということがわかりました。


次に、先ほど作ったapacheの設定を変更し制限を付けてみます。
ntlm_auth に引数 --require-membership-of=S-1-5-21-2558067586-3132001088-1936578106-1116 を与えます。
これでグループweb-allow (S-1-5-21-2558067586-3132001088-1936578106-1116) に所属していることという制約を作れます。

vi /etc/httpd/conf/httpd.conf
-----------------------------------------------------------------------------
KeepAlive On
LoadModule auth_ntlm_winbind_module modules/mod_auth_ntlm_winbind.so

Alias /ntlm_auth/ "/var/www/html/ntlm_auth/"
<Directory "/var/www/html/ntlm_auth/">
  NTLMAuth on
  AuthType NTLM
  AuthName "NTLM Authentication"
#  NTLMAuthHelper "/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp"
  NTLMAuthHelper "/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp --require-membership-of=S-1-5-21-2558067586-3132001088-1936578106-1116"
  NTLMBasicAuthoritative on
  require valid-user
</Directory>
-----------------------------------------------------------------------------

apache を再起動する。

/etc/init.d/httpd restart

ブラウザからアクセスする

http://192.168.56.123/ntlm_auth/

hello というテキストが表示されれば成功です。
グループweb-allow に所属していないユーザとかでエラーになることなども確認します。


これを利用して、 社員の人のみ入れるサーバとか、EAP用の鍵を配布するhttpsサーバとか、特定のグループのひとだけがコミットできる git , SVN とか、いろいろ応用して遊べると思います。let's try!