2016年5月11日 星期三

CentOS 6.4下安裝 OpenLDAP服務


Step1. 安裝openldap相關套件
[root@ldap ~]# yum install -y openldap-devel openldap-servers openldap openldap-clients

套件安裝完之後
其設定檔會在 /etc/openldap
指令類的會存放在 /usr/sbin/
存放 bdb 記錄資料在 /var/lib/ldap


Step2. 產生ldap管理者密碼
[root@ldap ~]# slappasswd
New password:
Re-enter new password:
{SSHA}VfWmRISGjLGb9SD5BbBxxNC7SiHzm6Q+ // (此行SSHA等一下會在 slapd.conf 內用到)

Step3. 複製並建立openldap設定檔與DB設定檔
[root@ldap ~]# cp /usr/share/openldap-servers/slapd.conf.obsolete /etc/openldap/slapd.conf
[root@ldap ~]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG


Step4. 設定主要設定檔slapd.conf
[root@ldap ~]# vi /etc/openldap/slapd.conf

#
# See slapd.conf(5) for details on configuration options.
# This file should NOT be world readable.
#

include         /etc/openldap/schema/corba.schema
include         /etc/openldap/schema/core.schema
include         /etc/openldap/schema/cosine.schema
include         /etc/openldap/schema/duaconf.schema
include         /etc/openldap/schema/dyngroup.schema
include         /etc/openldap/schema/inetorgperson.schema
include         /etc/openldap/schema/java.schema
include         /etc/openldap/schema/misc.schema
include         /etc/openldap/schema/nis.schema
include         /etc/openldap/schema/openldap.schema
include         /etc/openldap/schema/ppolicy.schema
include         /etc/openldap/schema/collective.schema

...
......
pidfile         /var/run/openldap/slapd.pid
argsfile        /var/run/openldap/slapd.args

#以下指定 log 紀錄
loglevel        256
logfile        /var/log/slapd/ldap.log

...
......
# The next three lines allow use of TLS for encrypting connections using a
# dummy test certificate which you can generate by running
# /usr/libexec/openldap/generate-server-cert.sh. Your client software may balk
# at self-signed certificates, however.

#若使用 SSL 憑證,則這個地方需修改
TLSCACertificatePath /etc/openldap/certs
TLSCertificateFile "\"OpenLDAP Server\""
TLSCertificateKeyFile /etc/openldap/certs/password

...
......
# enable on-the-fly configuration (cn=config)
database config
access to *
        by dn.exact="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" manage
        by * none

# enable server status monitoring (cn=monitor)
database monitor
access to *
        by dn.exact="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read
        by dn.exact="cn=root,dc=ldap,dc=tomy168,dc=com,dc=tw" read
        by * none
#增加底下這兩段
access to attrs=userPassword
       by self write
       by anonymous auth
       by dn.base="cn=root,dc=ldap,dc=tomy168,dc=com,dc=tw" write
       by * none
#attrs=userPassword 限制 userPassword 只用於認證,只能用來做認證用,只有 user 自己才能修改密碼
#self write 允許使用者變更自己的密碼
#anonymous auth匿名用戶需要認證
#* none任何人都無法存取

access to *
       by self write
       by users read
       by dn.base="cn=root,dc=ldap,dc=tomy168,dc=com,dc=tw" write
       by * none

...
......
#######################################################################
# database definitions
#######################################################################

database        bdb
suffix          "dc=ldap,dc=tomy168,dc=com,dc=tw"
checkpoint      1024 15
rootdn          "cn=root,dc=ldap,dc=tomy168,dc=com,dc=tw"
# Cleartext passwords, especially for the rootdn, should
# be avoided.  See slappasswd(8) and slapd.conf(5) for details.
# Use of strong authentication encouraged.
# rootpw                secret
# rootpw                {crypt}ijFYNcSNctBYg
rootpw          {SSHA}VfWmRISGjLGb9SD5BbBxxNC7SiHzm6Q+


設定目錄權限
[root@ldap ~]# chown ldap:ldap -R /var/lib/ldap/


Step5. 修改 rsyslog 增加 LDAP 記錄
[root@ldap ~]# vi /etc/rsyslog.conf
// 增加下面兩行

# LDAP Server Log
local4.*                                                /var/log/slapd/ldap.log


Step6. 建立 LDAP 根路徑檔
目錄 /etc/openldap/data 是用來放 ldif 的檔案位置
[root@ldap ~]# mkdir /etc/openldap/data
[root@ldap ~]# chown ldap:ldap -R /etc/openldap/data
[root@ldap ~]# vi /etc/openldap/data/root.ldif
# NTHU LDAP Base DN
dn: dc=ldap,dc=tomy168,dc=com,dc=tw
objectClass: dcObject
objectClass: organization
dc: ldap
o: NTHU-LDAP

# Magager ldap.tomy168.com.tw Root DN
dn: cn=root,dc=tomy168,dc=com,dc=tw
objectClass: organizationalRole
cn: root

dn: ou=staff,dc=tomy168,dc=com,dc=tw
ou: staff
objectClass: organizationalUnit
description: teacher

dn: ou=student,dc=tomy168,dc=com,dc=tw
ou: student
objectClass: organizationalUnit
description: student

刪除舊的資料並將剛定義的root.ldif加入到LDAP的資料庫內
[root@ldap ~]# rm -rf /etc/openldap/slapd.d/*
[root@ldap ~]# slapadd -v -l /etc/openldap/data/root.ldif
The first database does not allow slapadd; using the first available one (2)
added: "dc=ldap,dc=tomy168,dc=com,dc=tw" (00000001)
added: "cn=root,dc=ldap,dc=tomy168,dc=com,dc=tw" (00000002)
added: "ou=staff,dc=ldap,dc=tomy168,dc=com,dc=tw" (00000003)
added: "ou=prof,dc=ldap,dc=tomy168,dc=com,dc=tw" (00000004)
added: "ou=pt-prof,dc=ldap,dc=tomy168,dc=com,dc=tw" (00000005)
added: "ou=student,dc=ldap,dc=tomy168,dc=com,dc=tw" (00000006)
added: "ou=alumni,dc=ldap,dc=tomy168,dc=com,dc=tw" (00000007)
_#################### 100.00% eta   none elapsed            none fast!
Closing DB...

接著測試 slapd.conf
[root@ldap ~]# slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d
config file testing succeeded


Step7. 啟動LDAP
[root@ldap ~]# chown -R ldap:ldap /etc/openldap/slapd.d
[root@ldap ~]# service slapd restart
[root@ldap ~]# chkconfig slapd on


最後來測試一下LDAP能否正確查詢名稱
[root@ldap ~]# sldapsearch -x -b "dc=ldap,dc=nthu,dc=org,dc=tw"

若要重新匯入 / 重新設計 root.ldif (例如測試LDAP成功了,想改用自己單位的資料時)請記得清除舊有全部資料,你可以參考底下的步驟進行:
[root@ldap ~]# service slapd stop
[root@ldap ~]# rm -rf /var/lib/ldap/*
[root@ldap ~]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
[root@ldap ~]# rm -rf /etc/openldap/slapd.d/*
[root@ldap ~]# slapadd -v -l /etc/openldap/data/root.ldif
[root@ldap ~]# slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d
[root@ldap ~]# chown -R ldap:ldap /etc/openldap/slapd.d
[root@ldap ~]# chown -R ldap:ldap /var/lib/ldap 
[root@ldap ~]# service slapd start 

提示:做 LDAP 變更的時候,slapd 是不能在執行中的,你必須先將這個服務停止,如第一行的 service slapd stop ,這樣修改才會不導致錯誤。




本文內容參考自黃昏的甘蔗

0 意見: