原則上在 CentOS 下安裝 OpenLDAP 是件很輕鬆的事,用 YUM 裝一下就好了,我的環境如下

CentOS:6.4 x86_64

OpenLDAP:2.4.32

1. 安裝 openldap 套件

# yum install -y openldap-servers openldap-clients openldap-devel

2. 複製設定檔到對應的目錄

# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
# cp /usr/share/openldap-servers/slapd.conf.obsolete /etc/openldap/slapd.conf

3. 產生 LDAP 管理者 root 的密碼,並記下來等會兒設定至 slapd.conf

# slappasswd
New password: (輸入密碼)Re-enter new password: (輸入密碼)
{SSHA} UtMBTNpmr8QuRHyDBEQEBljDWwet3iv9

4. 修改主要 Config 檔 slapd.conf (以下是我的範例,僅供參考)

# 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

# Allow LDAPv2 client connections.  This is NOT the default.
allow bind_v2

# Do not enable referrals until AFTER you have a working directory
# service AND an understanding of referrals.
#referral       ldap://root.openldap.org

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

loglevel        256
logfile        /var/log/slapd/ldap.log

# Load dynamic backend modules
# - modulepath is architecture dependent value (32/64-bit system)
# - back_sql.la overlay requires openldap-server-sql package
# - dyngroup.la and dynlist.la cannot be used at the same time

# modulepath /usr/lib/openldap
# modulepath /usr/lib64/openldap

# moduleload accesslog.la
# moduleload auditlog.la
# moduleload back_sql.la
# moduleload chain.la
# moduleload collect.la
# moduleload constraint.la
# moduleload dds.la
# moduleload deref.la
# moduleload dyngroup.la
# moduleload dynlist.la
# moduleload memberof.la
# moduleload pbind.la
# moduleload pcache.la
# moduleload ppolicy.la
# moduleload refint.la
# moduleload retcode.la
# moduleload rwm.la
# moduleload seqmod.la
# moduleload smbk5pwd.la
# moduleload sssvlv.la
# moduleload syncprov.la
# moduleload translucent.la
# moduleload unique.la
# moduleload valsort.la

# 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.
TLSCACertificatePath /etc/openldap/certs
TLSCertificateFile "\"OpenLDAP Server\""
TLSCertificateKeyFile /etc/openldap/certs/password

# Sample security restrictions
#       Require integrity protection (prevent hijacking)
#       Require 112-bit (3DES or better) encryption for updates
#       Require 63-bit encryption for simple bind
# security ssf=1 update_ssf=112 simple_bind=64

# Sample access control policy:
#       Root DSE: allow anyone to read it
#       Subschema (sub)entry DSE: allow anyone to read it
#       Other DSEs:
#               Allow self write access
#               Allow authenticated users read access
#               Allow anonymous users to authenticate
#       Directives needed to implement policy:
# access to dn.base="" by * read
# access to dn.base="cn=Subschema" by * read
# access to *
#       by self write
#       by users read
#       by anonymous auth
#
# if no access controls are present, the default policy
# allows anyone and everyone to read anything but restricts
# updates to rootdn.  (e.g., "access to * by * read")
#
# rootdn can always read and write EVERYTHING!

# 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=bojack,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=bojack,dc=com,dc=tw" write
       by * none

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

#######################################################################
# database definitions
#######################################################################

database        bdb
suffix          "dc=ldap,dc=bojack,dc=com,dc=tw"
checkpoint      1024 15
rootdn          "cn=root,dc=ldap,dc=bojack,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}vgn3Ra9Qp0o4BpoYdbvBwLw9eYzNvZJS

# The database directory MUST exist prior to running slapd AND
# should only be accessible by the slapd and slap tools.
# Mode 700 recommended.
directory       /var/lib/ldap

# Indices to maintain for this database
index objectClass                       eq,pres
index ou,cn,mail,surname,givenname      eq,pres,sub
index uidNumber,gidNumber,loginShell    eq,pres
index uid,memberUid                     eq,pres,sub
index nisMapName,nisMapEntry            eq,pres,sub

# Replicas of this database
#replogfile /var/lib/ldap/openldap-master-replog
#replica host=ldap-1.example.com:389 starttls=critical
#     bindmethod=sasl saslmech=GSSAPI
#     authcId=host/[email protected]

5. 設定將 LDAP 的 log 寫入 /var/log/slapd

# vi /etc/rsyslog.conf

加入這一行

local4.*            /var/log/slapd

6. 定義並新增 base.ldif 來定義整個樹狀結構

# cd /etc/openldap/
# mkdir ldif
# cd ldif
# vi base.ldif

我的 base.ldif 檔案內容如下,原則上就是只有一個員工 ou

# Bojack LDAP Base DN
dn: dc=ldap,dc=bojack,dc=com,dc=tw
objectClass: dcObject
objectClass: organization
dc: ldap
o: BOJACK-LDAP

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

dn: ou=employee,dc=ldap,dc=bojack,dc=com,dc=tw
ou: employee
objectClass: organizationalUnit
description: employee

刪除原有的資料後再新增進去

# rm -rf /etc/openldap/slapd.d/*
# slapadd -v -l /etc/openldap/ldif/base.ldif

The first database does not allow slapadd; using the first available one (2)
added: "dc=ldap,dc=bojack,dc=com,dc=tw" (00000001)
added: "cn=root,dc=ldap,dc=bojack,dc=com,dc=tw" (00000002)
added: "ou=employee,dc=ldap,dc=bojack,dc=com,dc=tw" (00000003)
_#################### 100.00% eta   none elapsed            none fast!
Closing DB...

將 LDAP 新的資料庫內容產生檔案到 slapd.d 的目錄內並改變擁有權

# slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d

config file testing succeeded

最後來改變權限

# chown -R ldap:ldap /var/lib/ldap
# chown -R ldap:ldap /etc/openldap/slapd.d

7. 設定開機自動啟動 LDAP

# service slapd start
# chkconfig --list slapd
# chkconfig --level 345 slapd on

8. 利用 ldapsearch 驗證一下資料是否已匯入

# ldapsearch -x -b "dc=ldap,dc=bojack,dc=com,dc=tw"
# extended LDIF
#
# LDAPv3
# base <dc=ldap,dc=bojack,dc=com,dc=tw> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#

# ldap.bojack.com.tw
dn: dc=ldap,dc=bojack,dc=com,dc=tw
objectClass: dcObject
objectClass: organization
dc: ldap
o: BOJACK-LDAP

# root, ldap.bojack.com.tw
dn: cn=root,dc=ldap,dc=bojack,dc=com,dc=tw
objectClass: organizationalRole
cn: root

# employee, ldap.bojack.com.tw
dn: ou=employee,dc=ldap,dc=bojack,dc=com,dc=tw
ou: employee
objectClass: organizationalUnit
description: employee

# search result
search: 2
result: 0 Success

# numResponses: 4
# numEntries: 3

9. 如果要重新匯入請記得清除舊有全部資料

# service slapd stop
# rm -rf /var/lib/ldap/*
# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
# rm -rf /etc/openldap/slapd.d/*
# slapadd -v -l /etc/openldap/ldif/base.ldif
# slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d
# chown -R ldap:ldap /etc/openldap/slapd.d
# chown -R ldap:ldap /var/lib/ldap
# service slapd start

參考資料:

IT水電工-哆啦胖虎 - LDAP基本安裝及整合登入認證機制的架設 on CentOS 6.2_x64

http://bojack.pixnet.net/blog/post/32056233

By tony

自由軟體愛好者~喜歡不斷的思考各種問題,有新的事物都會想去學習嘗試 做實驗並熱衷研究 沒有所謂頂天的技術 只有謙虛及不斷的學習 精進專業,本站主要以分享系統及網路相關知識、資源而建立。 Github http://stnet253.github.io

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料