概要
LDAP を SSH, sFTP, www (Basic 認証)のログイン認証に使う。
インストールしたパッケージ
# yum list | grep ldap
nss_ldap.i386 226-10 installed
openldap.i386 2.2.13-4 installed
openldap-clients.i386 2.2.13-4 installed
openldap-devel.i386 2.2.13-4 installed
openldap-servers.i386 2.2.13-4 installed
php-ldap.i386 4.3.9-3.9 installed
python-ldap.i386 2.0.1-2 installed
compat-openldap.i386 2.1.30-4 update
mod_authz_ldap.i386 0.26-2 base
openldap-servers-sql.i386 2.2.13-4 update
# yum list | grep pam
pam.i386 0.77-66.13 installed
pam-devel.i386 0.77-66.13 installed
pam_ccreds.i386 1-3 installed
pam_krb5.i386 2.1.8-1 installed
pam_passwdqc.i386 0.7.5-2 installed
pam_smb.i386 1.1.7-5 installed
設定ファイル
# pwd
/etc/openldap
# ls -R
.:
cacerts ldap.conf schema slapd.conf
./cacerts:
./schema:
README core.schema dyngroup.schema java.schema nis.schema redhat
corba.schema cosine.schema inetorgperson.schema misc.schema openldap.schema
./schema/redhat:
autofs.schema
slapd.conf
/etc/openldap/sldapd.conf
以下のように編集する(rootpw はちゃんと変える事)。
suffix "dc=example,dc=com"
rootdn "cn=Manager,dc=example,dc=com"
rootpw secret
access to attrs=userPassword
by self write
by users auth
by anonymous auth
access to *
by self write
by users read
by anonymous read
この例では、ユーザは自分の情報を読み書きできるけれど、他人のパスワードを見る、または情報を書き換える事はできないように設定している。
以下はあるユーザに管理権限を与える例。
access to attrs=userPassword
by self write
by dn="uid=username,ou=People,dc=example,dc=com" write
by users read
by anonymous auth
access to *
by self write
by dn="uid=username,ou=People,dc=example,dc=com" write
by users read
by anonymous read
rootpw の暗号化
slappasswd を使う。
# slappasswd
New password:
Re-enter new password:
{SSHA}IURIkjnl/xCkPBTG5xCmLKtXfeFLFCSe
生成された文字列を slapd.conf に設定。
rootpw {SSHA}IURIkjnl/xCkPBTG5xCmLKtXfeFLFCSe
起動
# service ldap start
マシン起動時に LDAP も起動するには次のようにする。
# chkconfig ldap on
正常に動作しているかは、次のようにして確認できる。
# ldapsearch -v -x -b '' -s base +
ldap_initialize( <DEFAULT> )
filter: (objectclass=*)
requesting: +
# extended LDIF
#
# LDAPv3
# base <> with scope base
# filter: (objectclass=*)
# requesting: +
#
#
dn:
structuralObjectClass: OpenLDAProotDSE
namingContexts: dc=example,dc=com
supportedControl: 2.16.840.1.113730.3.4.18
supportedControl: 2.16.840.1.113730.3.4.2
supportedControl: 1.3.6.1.4.1.4203.1.10.1
supportedControl: 1.2.840.113556.1.4.1413
supportedControl: 1.2.840.113556.1.4.1339
supportedControl: 1.2.840.113556.1.4.319
supportedControl: 1.2.826.0.1.334810.2.3
supportedExtension: 1.3.6.1.4.1.1466.20037
supportedExtension: 1.3.6.1.4.1.4203.1.11.1
supportedExtension: 1.3.6.1.4.1.4203.1.11.3
supportedFeatures: 1.3.6.1.4.1.4203.1.5.1
supportedFeatures: 1.3.6.1.4.1.4203.1.5.2
supportedFeatures: 1.3.6.1.4.1.4203.1.5.3
supportedFeatures: 1.3.6.1.4.1.4203.1.5.4
supportedFeatures: 1.3.6.1.4.1.4203.1.5.5
supportedLDAPVersion: 2
supportedLDAPVersion: 3
supportedSASLMechanisms: CRAM-MD5
supportedSASLMechanisms: DIGEST-MD5
subschemaSubentry: cn=Subschema
# search result
search: 2
result: 0 Success
# numResponses: 2
# numEntries: 1
#
データの入力
LDIF ファイルを使った入力
LDIF ファイルの例:
dn: dc=example,dc=com
objectClass: dcObject
objectClass: organization
o: example
dc: example
dn: ou=Users,dc=example,dc=com
objectClass: top
objectClass: organizationalUnit
ou: users
dn: cn=FirstnameLastname,ou=Users,dc=example,dc=com
sn: Lastname
telephoneNumber: 03-1111-2222
userPassword: 1234567
mail: user@example.com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
uid: username
cn: FirstnameLastname
コマンド実行例:
# ldapadd -x -h localhost -D cn=manager,dc=example,dc=com -f ldif.txt -W
Enter LDAP Password:
adding new entry "dc=example,dc=com"
adding new entry "ou=Users,dc=example,dc=com"
adding new entry "cn=FirstnameLastname,ou=Users,dc=example,dc=com"
検索例 (uid=username):
# ldapsearch -LLL -x -h localhost -b dc=example,dc=com uid=username
dn: cn=FirstnameLastname,ou=Users,dc=example,dc=com
sn: Lastname
telephoneNumber: 00-1111-2222
userPassword:: MTIzNDU2Nw==
mail: user@example.com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
uid: username
cn: FirstnameLastname
LDAP をシステムのログイン認証に使う
PAM の設定
'authconfig' を実行して、use LDAP と use LDAP auth にチェックし、next へ進んで OK を押す。すると /etc/pam.d/system-auth に以下のように追加される。
auth sufficient /lib/security/$ISA/pam_ldap.so use_first_pass
account [default=bad success=ok user_unknown=ignore] /lib/security/$ISA/pam_ldap.so
password sufficient /lib/security/$ISA/pam_ldap.so use_authtok
session optional /lib/security/$ISA/pam_ldap.so
その後、system-auth に以下の行を追加。
session required /lib/security/$ISA/pam_mkhomedir.so skel=/etc/skel umask=0022
system-auth の例:
# cat /etc/pam.d/system-auth
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth required /lib/security/$ISA/pam_env.so
auth sufficient /lib/security/$ISA/pam_unix.so likeauth nullok
auth sufficient /lib/security/$ISA/pam_ldap.so use_first_pass
auth required /lib/security/$ISA/pam_deny.so
account required /lib/security/$ISA/pam_unix.so broken_shadow
account sufficient /lib/security/$ISA/pam_succeed_if.so uid < 100 quiet
account [default=bad success=ok user_unknown=ignore] /lib/security/$ISA/pam_ldap.so
account required /lib/security/$ISA/pam_permit.so
password requisite /lib/security/$ISA/pam_cracklib.so retry=3
password sufficient /lib/security/$ISA/pam_unix.so nullok use_authtok md5 shadow
password sufficient /lib/security/$ISA/pam_ldap.so use_authtok
password required /lib/security/$ISA/pam_deny.so
session required /lib/security/$ISA/pam_limits.so
session required /lib/security/$ISA/pam_unix.so
session optional /lib/security/$ISA/pam_ldap.so
session required /lib/security/$ISA/pam_mkhomedir.so skel=/etc/skel umask=0022
NSS の設定
/etc/nsswitch.conf の passwd, shadow, group に ldap を追加。
passwd: files ldap
shadow: files ldap
group: files ldap
既存ユーザの移行
移行ツールの設定
/usr/share/openldap/migration/migrate_common.ph の $DEFAULT_MAIL_DOMAIN と $DEFAULT_BASE を修正する。
# Default DNS domain
$DEFAULT_MAIL_DOMAIN = "example.com";
# Default base
$DEFAULT_BASE = "dc=example,dc=com";
移行ツールを実行する
# /usr/share/openldap/migration/migrate_passwd.pl /etc/passwd ./ldifpasswd.txt
# /usr/share/openldap/migration/migrate_group.pl /etc/group ./ldifgroup.txt
./ldifpasswd.txt と ldifgroup.txt が作成される。これらの上位エントリ 'ou=People,dc=example,dc=com' 用の ldif ファイルを作成する。
例:
dn: ou=People,dc=example,dc=com
ou: People
objectClass: top
objectClass: organizationalUnit
dn: ou=Group,dc=example,dc=com
ou: Group
objectClass: top
objectClass: organizationalUnit
このエントリを最初に登録する。
# ldapadd -x -h localhost -D cn=manager,dc=example,dc=com -f ldif.txt -W
Enter LDAP Password:
adding new entry "ou=People,dc=example,dc=com"
移行ツールで作った ldifpasswd.txt から uid が 100 以下のものと 65000 以上のものを削除する(これらはシステムが使用するユーザなので)。その後、以下のように実行する。
# ldapadd -x -h localhost -D cn=manager,dc=example,dc=com -f ldifpasswd.txt -W
Enter LDAP Password:
adding new entry "uid=username,ou=People,dc=example,dc=com"
# ldapadd -x -h localhost -D cn=manager,dc=example,dc=com -f ldifgroup.txt -W
Enter LDAP Password:
adding new entry "ou=People,dc=example,dc=com"
確認手順
- /etc/passwd にユーザがいない事を確認
- /home/username を作成し、owner をそのユーザに変更
- そのユーザでログインする(ログインできるはず)
- LDAP を停止して、同じユーザでログインする(ログインできないはず)
- そのユーザのホームディレクトリを削除
- LDAP を起動して、そのユーザでログインする(ログインできて、ホームディレクトリが作成される)
実行例:
# grep username /etc/passwd
# mkdir /home/username
# chown username /home/username
# ls -l /home
合計 16
drwxr-xr-x 2 username root 4096 3月 5 15:24 username
$ ssh username@example.com
username@example.com's password:
Last login: Mon Aug 22 22:42:17 2005 from 192.168.1.2
-bash-3.00$
# service ldap stop
# ls -l /home
合計 16
drwxr-xr-x 2 500 root 4096 3月 5 15:28 username
$ ssh username@example.com
username@example.com's password:
Permission denied, please try again.
username@example.com's password:
# rm -r /home/username
rm: descend into directory `/home/username'? y
rm: remove 通常ファイル `/home/username/.bash_history'? y
rm: remove ディレクトリ `/home/username'? y
# service ldap start
$ ssh username@example.com
username@example.com's password:
Creating directory '/home/username'.
Last login: Sun Mar 5 15:25:28 2006 from 192.168.1.2
Labels: document, openldap