ラベル winbind の投稿を表示しています。 すべての投稿を表示
ラベル winbind の投稿を表示しています。 すべての投稿を表示

2012年2月1日水曜日

ApacheからNTML認証でADユーザを利用する


Apacheにmod_auth_ntlm_winbindを組み込み、NTML認証を使ってActiveDirectoryのユーザを利用する方法です。 Basic認証より安全かも。AD一元管理されたユーザ/パスワードを利用できるので運用がラク。
・ActiveDirectoryサーバOS: Windows Server 2008 R2
 → FQDNは win2k8r2-ad01.kuitan.net

・LinuxサーバOS: CentOS6.2 (64-bit)
 →Apacheバージョン: 2.2.15
 →Sambaバージョン: 3.5.10
 ※その他、samba-winbind、httpd-develが必要です。事前準備ヨロ。


■mod_auth_ntlm_winbindの取得
[root@centos62 ダウンロード]# svn co svn://svnanon.samba.org/lorikeet/trunk/mod_auth_ntlm_winbind mod_auth_ntlm_winbind
A    mod_auth_ntlm_winbind/contrib
A    mod_auth_ntlm_winbind/contrib/mod_auth_ntlm_winbind-20060510-connect_http10.patch
A    mod_auth_ntlm_winbind/Makefile.in
A    mod_auth_ntlm_winbind/debian
A    mod_auth_ntlm_winbind/debian/control
A    mod_auth_ntlm_winbind/debian/auth_ntlm_winbind.load
A    mod_auth_ntlm_winbind/debian/compat
A    mod_auth_ntlm_winbind/debian/changelog
A    mod_auth_ntlm_winbind/debian/copyright
A    mod_auth_ntlm_winbind/debian/rules
A    mod_auth_ntlm_winbind/500mod_auth_ntlm_winbind.info
A    mod_auth_ntlm_winbind/AUTHORS
A    mod_auth_ntlm_winbind/VERSION
A    mod_auth_ntlm_winbind/mod_auth_ntlm_winbind.c
A    mod_auth_ntlm_winbind/configure.in
A    mod_auth_ntlm_winbind/README
 U   mod_auth_ntlm_winbind
リビジョン 801 をチェックアウトしました。
[root@centos62 ダウンロード]#


■mod_auth_ntlm_winbindのインストール(1分以内で完了)
[root@centos62 ダウンロード]# cd mod_auth_ntlm_winbind/
[root@centos62 mod_auth_ntlm_winbind]# autoconf
[root@centos62 mod_auth_ntlm_winbind]# ./configure --with-apxs=/usr/sbin/apxs --with-apache=/usr/sbin/httpd
checking for gcc... gcc
checking for C compiler default output file name... a.out
(後略)

[root@centos62 mod_auth_ntlm_winbind]# make
/usr/sbin/apxs -DAPACHE2 -Wc,-Wall -c mod_auth_ntlm_winbind.c
/usr/lib64/apr-1/build/libtool --silent --mode=compile gcc -prefer-pic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wformat-security -fno-strict-aliasing  -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -pthread -I/usr/include/httpd  -I/usr/include/apr-1   -I/usr/include/apr-1  -Wall -DAPACHE2  -c -o mod_auth_ntlm_winbind.lo mod_auth_ntlm_winbind.c && touch mod_auth_ntlm_winbind.slo
/usr/lib64/apr-1/build/libtool --silent --mode=link gcc -o mod_auth_ntlm_winbind.la  -rpath /usr/lib64/httpd/modules -module -avoid-version    mod_auth_ntlm_winbind.lo
[root@centos62 mod_auth_ntlm_winbind]#

[root@centos62 mod_auth_ntlm_winbind]# make install
/usr/sbin/apxs -DAPACHE2 -S LIBEXECDIR=//usr/lib64/httpd/modules -n auth_ntlm_winbind -i .libs/mod_auth_ntlm_winbind.so
/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' .libs/mod_auth_ntlm_winbind.so //usr/lib64/httpd/modules
/usr/lib64/apr-1/build/libtool --mode=install cp .libs/mod_auth_ntlm_winbind.so //usr/lib64/httpd/modules/
libtool: install: cp .libs/mod_auth_ntlm_winbind.so //usr/lib64/httpd/modules/mod_auth_ntlm_winbind.so
Warning!  dlname not found in //usr/lib64/httpd/modules/mod_auth_ntlm_winbind.so.
Assuming installing a .so rather than a libtool archive.
chmod 755 //usr/lib64/httpd/modules/mod_auth_ntlm_winbind.so
[root@centos62 mod_auth_ntlm_winbind]#
■表示確認用コンテンツ準備
[root@centos62 ~]# mkdir /var/www/html/ntlm
[root@centos62 ~]# vi /var/www/html/ntlm/index.html
[root@centos62 ~]# cat /var/www/html/ntlm/index.html
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#99ffdd">
NTLM認証に成功しました。<br />
</body>
</html>
[root@centos62 ~]#


■httpd.confの設定
[root@centos62 ~]# cd /etc/httpd/conf/
[root@centos62 conf]# cp -p httpd.conf httpd.conf.20100528
[root@centos62 ~]# vi /etc/httpd/conf/httpd.conf
------------------------------------------------
76行目あたり(Off→On)
KeepAlive On

202行目あたり(追記)
LoadModule auth_ntlm_winbind_module modules/mod_auth_ntlm_winbind.so

最下行に追記
# for NTLM settings
Alias /ntlm/ "/var/www/html/ntlm/"
<Directory "/var/www/html/ntlm/">
  NTLMAuth on
  AuthType NTLM
  AuthName "NTLM Authentication"
  NTLMAuthHelper "/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp"
  NTLMBasicAuthoritative on
  require valid-user
</Directory>
------------------------------------------------


■winbindd_privilegedディレクトリにApache(mod_auth_ntlm_winbind)のプロセスのユーザへのアクセス権を設定
[root@centos62 ~]# chgrp apache /var/lib/samba/winbindd_privileged


■Apacheを起動
[root@centos62 ~]# /etc/init.d/httpd start
httpd を起動中:                                            [  OK  ]


■ブラウザからアクセス
http://192.168.xxx.143/ntlm/
NTLM認証に成功しました。の画面が表示されれば成功!!

※ちなみに、smb.conf等の設定が間違っていたりするとあっさり401がお目見えします。

Authorization Required

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.


Apache/2.2.15 (CentOS) Server at 192.168.xxx.143 Port 80

2012年1月29日日曜日

LinuxサーバをActiveDirectoryに連携させる


ActiveDirectory上で一元管理されたユーザをLinuxサーバ上で使うことが目的です。
・ActiveDirectoryサーバOS: Windows Server 2008 R2
 → FQDNは win2k8r2-ad01.kuitan.net
・LinuxサーバOS: CentOS6.2 (64-bit)


■/etc/hostsに自HOSTおよびADサーバを追加

[root@centos62 ~]# vi /etc/hosts
[root@centos62 ~]# cat /etc/hosts
# 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
127.0.0.1   centos62 localhost localhost.localdomain
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.xxx.125   win2k8r2-ad01.kuitan.net win2k8r2-ad01
[root@centos62 ~]#

■resolv.confの設定

[root@centos62 ~]# vi /etc/resolv.conf
[root@centos62 ~]# cat /etc/resolv.conf
# Generated by NetworkManager


# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
#
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com

search kuitan.net
nameserver 192.168.xxx.125
[root@centos62 ~]#

⇒networkサービスを再起動すると設定がクリアされてしまうのでifcfg-eth0に記述
[root@centos62 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
[root@centos62 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
NM_CONTROLLED="yes"
ONBOOT=yes
HWADDR=00:0C:29:xx:xx:xx
TYPE=Ethernet
BOOTPROTO=none
IPADDR=192.168.xxx.143
PREFIX=24
GATEWAY=192.168.xxx.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
UUID=5xxx6bd0-0ba0-7ffb-4xx1-d6xxxxxf3203
DOMAIN=kuitan.net
DNS1=192.168.xxx.125
[root@centos62 ~]#

[root@centos62 ~]# /etc/init.d/network restart
インターフェース eth0 を終了中:  デバイスの状態: 3 (切断済み)
                                                           [  OK  ]
ループバックインターフェースを終了中                       [  OK  ]
ループバックインターフェイスを呼び込み中                   [  OK  ]
インターフェース eth0 を活性化中:  アクティブ接続の状態: アクティベート済み
アクティブ接続のパス: /org/freedesktop/NetworkManager/ActiveConnection/3
                                                           [  OK  ]
[root@centos62 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search kuitan.net
nameserver 192.168.xxx.125
[root@centos62 ~]#

■Windbindに必要なSambaのパッケージ確認(samba-clientとsamba-commonが必要)

[root@centos62 ~]# rpm -qa | grep samba
samba-winbind-clients-3.5.10-114.el6.x86_64
samba-3.5.10-114.el6.x86_64
samba4-libs-4.0.0-23.alpha11.el6.x86_64
samba-common-3.5.10-114.el6.x86_64
samba-client-3.5.10-114.el6.x86_64
[root@centos62 ~]#

■Winbindがない場合はあらかじめインストールしておく(CentOS6.2インストールメディアより)

[root@centos62 ~]# rpm -ivh /media/CentOS_6.2_Final/Packages/samba-winbind-3.5.10-114.el6.x86_64.rpm
警告: /media/CentOS_6.2_Final/Packages/samba-winbind-3.5.10-114.el6.x86_64.rpm: ヘッダ V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
準備中...                ################################# [100%]
   1:samba-winbind          ################################# [100%]
[root@centos62 ~]# rpm -qa | grep winbind
samba-winbind-clients-3.5.10-114.el6.x86_64
samba-winbind-3.5.10-114.el6.x86_64
[root@centos62 ~]#
※ntlm_authコマンドも利用できるようになる。

■smb.confの設定([global]セクションに下記を追加)

[root@centos62 ~]# cd /etc/samba
[root@centos62 samba]# ll
合計 20
-rw-r--r--. 1 root root   20 12月  8 09:15 2011 lmhosts
-rw-r--r--. 1 root root 9778 12月  8 09:15 2011 smb.conf
-rw-r--r--. 1 root root   97 12月  8 09:15 2011 smbusers
[root@centos62 samba]# cp -p smb.conf smb.conf.20111208
[root@centos62 samba]# vi smb.conf

245行目あたりから
# ---- Settings for Winbind ----
  security = ads
  workgroup = KUITAN
  realm = KUITAN.NET
  password server = win2k8r2-ad01.kuitan.net
  encrypt passwords = true
  idmap uid = 10000-25000
  idmap gid = 10000-20000
  winbind use default domain = yes
  winbind cache time = 90
#  winbind nested groups = yes
#  template shell = /bin/bash
#  template homedir = /home/%U

■nsswitch.confの設定

[root@rails01 ~]# cp -p /etc/nsswitch.conf /etc/nsswitch.conf.20040923
[root@rails01 ~]# vi /etc/nsswitch.conf
33行目あたりを修正
passwd:     files winbind
shadow:     files winbind
group:      files winbind

■krb5.confの設定

[root@centos62 ~]# cp -p /etc/krb5.conf /etc/krb5.conf.20100218
[root@centos62 ~]# vi /etc/krb5.conf
[root@centos62 ~]# cat /etc/krb5.conf
[logging]
 default = FILE:/var/log/krb5libs.log
 kdc = FILE:/var/log/krb5kdc.log
 admin_server = FILE:/var/log/kadmind.log

[libdefaults]
 default_realm = KUITAN.NET
 dns_lookup_realm = false
 dns_lookup_kdc = false
 ticket_lifetime = 24h
 renew_lifetime = 7d
 forwardable = true

[realms]
 KUITAN.NET = {
  kdc = win2k8r2-ad01.kuitan.net
  admin_server = win2k8r2-ad01.kuitan.net
 }

[domain_realm]
 .kuitan.net = KUITAN.NET
 kuitan.net = KUITAN.NET
[root@centos62 ~]#

⇒下記のようにviの文字列置換を使うとラク。
:%s/EXAMPLE.COM/KUITAN.NET/g
:%s/kerberos.example.com/win2k8r2-ad01.kuitan.net/g
:%s/example.com/kuitan.net/g

■smbとwinbindを起動

[root@centos62 ~]# /etc/init.d/smb start
SMB サービスを起動中:                                      [  OK  ]
[root@centos62 ~]# /etc/init.d/winbind start
Winbind サービスを起動中:                                  [  OK  ]
[root@centos62 ~]#

■Windowsドメインに参加

[root@centos62 ~]# net ads join -U administrator
Enter administrator's password:
Using short domain name -- KUITAN
Joined 'CENTOS62' to realm 'kuitan.net'
[2012/01/29 01:52:23.073504,  0] libads/kerberos.c:333(ads_kinit_password)
  kerberos_kinit_password CENTOS62$@KUITAN.NET failed: Clock skew too great
No DNS domain configured for centos62. Unable to perform DNS Update.
DNS update failed!
[root@centos62 ~]#
⇒時刻のずれが大きいため接続できない。

⇒ADサーバと時刻同期
[root@centos62 ~]# net time set -I win2k8r2-ad01.kuitan.net
2012年  1月  29日 日曜日 01:42:17 JST
[root@centos62 ~]#

⇒時刻同期後再実行
[root@centos62 ~]# net ads join -U administrator
Enter administrator's password:
Using short domain name -- KUITAN
Joined 'CENTOS62' to realm 'kuitan.net'
No DNS domain configured for centos62. Unable to perform DNS Update.
DNS update failed!
[root@centos62 ~]#

⇒接続確認
[root@centos62 ~]# net ads info
LDAP server: 192.168.xxx.125
LDAP server name: win2k8r2-ad01.kuitan.net
Realm: KUITAN.NET
Bind Path: dc=KUITAN,dc=NET
LDAP port: 389
Server time: 日, 29  1月 2012 01:45:45 JST
KDC server: 192.168.xxx.125
Server time offset: 1
[root@centos62 ~]# net ads testjoin
Join is OK

⇒AD上のユーザとグループの表示
[root@centos62 ~]# wbinfo -u
administrator
guest
krbtgt
aduser01
aduser02
[root@centos62 ~]# wbinfo -g
domain computers
domain controllers
schema admins
enterprise admins
cert publishers
domain admins
domain users
domain guests
group policy creator owners
ras and ias servers
allowed rodc password replication group
denied rodc password replication group
read-only domain controllers
enterprise read-only domain controllers
dnsadmins
dnsupdateproxy
[root@centos62 ~]#

[root@centos62 ~]# id aduser01
uid=10000(aduser01) gid=10000(domain users) 所属グループ=10000(domain users)

[root@centos62 ~]# ntlm_auth --username=KUITAN\\aduser02
password:
NT_STATUS_OK: Success (0x0)
[root@centos62 ~]#

この情報が何かのお役にたてれば幸いです。スポソサ~へのご協力いただけると幸いです。m(_ _)m