CentOS 6.7 架設 OpenLDAP

考量到學校多數採用 OpenLDAP ,故以 CentOS 6.7 安裝 OpenLDAP 實際操作過並做一個紀錄。
OpenLDAP 的官方網站:http://www.openldap.org/

-------------------------------------------

安裝LDAP 的步驟:

    執行安裝命令
    產生 ldap 管理者的密碼
    複製樣本檔
    設定主要設定檔 slapd.conf
    修改 rsyslog 增加 LDAP 記錄
    建立 LDAP 根路徑檔
    啟動 slapd
    設定開機自動執行 slapd



1、執行安裝命令

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

Installed:
openldap-clients.x86_64 0:2.4.23-32.el6_4.1                openldap-devel.x86_64 0:2.4.23-32.el6_4.1
  openldap-servers.x86_64 0:2.4.23-32.el6_4.1
Dependency Installed:
cyrus-sasl-devel.x86_64 0:2.1.23-13.el6_3.1

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

2、產生 ldap 管理者的密碼

slappasswd
New password: ooxxoo
Re-enter new password: ooxxoo
{SSHA}A0GFrw/1dpGrusm0QqqqWWmHMMwuqfd
// (此行SSHA等一下會在 slapd.conf 內用到)

3、複製樣本檔

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

4、設定主要設定檔 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

#在底下這行下指定 log 紀錄
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.

#若有使用 SSL 憑證,則這個地方需修改
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=nthu,dc=org,dc=tw" read
        by * none
#增加底下這兩段
access to attrs=userPassword
       by self write
       by anonymous auth
       by dn.base="cn=root,dc=ldap,dc=nthu,dc=org,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=nthu,dc=org,dc=tw" write
       by * none

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

database        bdb
#suffix         "dc=my-domain,dc=com"
suffix          "dc=ldap,dc=nthu,dc=org,dc=tw"
checkpoint      1024 15
#rootdn         "cn=Manager,dc=my-domain,dc=com"
rootdn          "cn=root,dc=ldap,dc=nthu,dc=org,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}A0GFrw/1dpGrusm0QqqqWWmHMMwuqfd

# 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/ldap-master.example.com@EXAMPLE.COM


設定目錄權限

sudo chown ldap:ldap -R /var/lib/ldap/


5、修改 rsyslog 增加 LDAP 記錄

vi /etc/rsyslog.conf

// 增加下面兩行

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

重新啟動 rsyslog 服務 (重新載入 /etc/rsyslog.conf 設定)

/etc/init.d/rsyslog restart


6、建立 LDAP 根路徑檔

編輯 root.ldif
目錄 /etc/openldap/data 是用來放 ldif 的檔案位置

mkdir /etc/openldap/data
chown ldap:ldap -R /etc/openldap/data


編寫 ldap 根路徑的定義 (root.ldif 名稱非絕對)

vi /etc/openldap/data/root.ldif

內容為

# NTHU LDAP Base DN
dn: dc=ldap,dc=nthu,dc=org,dc=tw
objectClass: dcObject
objectClass: organization
dc: ldap
o: NTHU-LDAP

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

dn: ou=staff,dc=ldap,dc=nthu,dc=org,dc=tw
ou: staff
objectClass: organizationalUnit
description: staff

dn: ou=prof,dc=ldap,dc=nthu,dc=org,dc=tw
ou: prof
objectClass: organizationalUnit
description: prof

dn: ou=pt-prof,dc=ldap,dc=nthu,dc=org,dc=tw
ou: pt-prof
objectClass: organizationalUnit
description: pt-prof

dn: ou=student,dc=ldap,dc=nthu,dc=org,dc=tw
ou: student
objectClass: organizationalUnit
description: student

dn: ou=alumni,dc=ldap,dc=nthu,dc=org,dc=tw
ou: alumni
objectClass: organizationalUnit
description: alumni

接著刪除舊的資料並將剛定義的root.ldif加入到LDAP的資料庫內

rm -rf /etc/openldap/slapd.d/*
slapadd -v -l /etc/openldap/data/root.ldif

他會出現類似底下這樣的訊息,代表加入了 root.ldif

The first database does not allow slapadd; using the first available one (2)
added: "dc=ldap,dc=nthu,dc=org,dc=tw" (00000001)
added: "cn=root,dc=ldap,dc=nthu,dc=org,dc=tw" (00000002)
added: "ou=staff,dc=ldap,dc=nthu,dc=org,dc=tw" (00000003)
added: "ou=prof,dc=ldap,dc=nthu,dc=org,dc=tw" (00000004)
added: "ou=pt-prof,dc=ldap,dc=nthu,dc=org,dc=tw" (00000005)
added: "ou=student,dc=ldap,dc=nthu,dc=org,dc=tw" (00000006)
added: "ou=alumni,dc=ldap,dc=nthu,dc=org,dc=tw" (00000007)
_#################### 100.00% eta   none elapsed            none fast!
Closing DB...

接著測試 slapd.conf

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

config file testing succeeded


7、啟動 slapd

chown -R ldap:ldap /etc/openldap/slapd.d
service slapd restart


8、設定開機自動執行 slapd

chkconfig slapd on


最後來測試一下LDAP能否正確查詢名稱

ldapsearch -x -b "dc=ldap,dc=nthu,dc=org,dc=tw"

# extended LDIF
#
# LDAPv3
# base <dc=ldap,dc=nthu,dc=org,dc=tw> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#

# ldap.nthu.org.tw
dn: dc=ldap,dc=nthu,dc=org,dc=tw
objectClass: dcObject
objectClass: organization
dc: ldap
o: nthu-LDAP

# root, ldap.nthu.org.tw
dn: cn=root,dc=ldap,dc=nthu,dc=org,dc=tw
objectClass: organizationalRole
cn: root

# staff, ldap.nthu.org.tw
dn: ou=staff,dc=ldap,dc=nthu,dc=org,dc=tw
ou: staff
objectClass: organizationalUnit
description: staff

# prof, ldap.nthu.org.tw
dn: ou=prof,dc=ldap,dc=nthu,dc=org,dc=tw
ou: prof
objectClass: organizationalUnit
description: prof

# pt-prof, ldap.nthu.org.tw
dn: ou=pt-prof,dc=ldap,dc=nthu,dc=org,dc=tw
ou: pt-prof
objectClass: organizationalUnit
description: pt-prof

# student, ldap.nthu.org.tw
dn: ou=student,dc=ldap,dc=nthu,dc=org,dc=tw
ou: student
objectClass: organizationalUnit
description: student

# alumni, ldap.nthu.org.tw
dn: ou=alumni,dc=ldap,dc=nthu,dc=org,dc=tw
ou: alumni
objectClass: organizationalUnit
description: alumni

# search result
search: 2
result: 0 Success

# numResponses: 8
# numEntries: 7


**重點 **

如果上述的步驟亂了,或是要重新匯入 / 重新設計 root.ldif (例如測試LDAP成功了,想改用自己單位的資料時)請記得清除舊有全部資料,你可以參考底下的步驟進行

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/data/root.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


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


再來你就可以使用 users.ldif 建立人員名冊,將使用者資料寫在 user.ldif 然後利用 ldapmodify 這個指令將其加入

ldapmodify -D "cn=Manager,dc=nthu,dc=org,dc=com" -w LDAP的管理密碼 -x -a -f /etc/openldap/data/users.ldif

下一步將來介紹安裝 LAM(LDAP Account manager)來管理 ldap 裡的資料。

~End

後記:

// 修改 slapd.d 目錄擁有者,不然啟動時會出現
// ldif_read_file: Permission denied for "/etc/openldap/slapd.d/cn=config.ldif"
// slaptest: bad configuration file!
chown ldap:ldap -R /etc/openldap/slapd.d/

// 欲並變更slapd.conf的內容
rm -rf /etc/openldap/slapd.d/*
slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d
// 因為還有子目錄 cn=config 刪除後重建 owner 會變成 root:root,所以要 chown
chown -R ldap:ldap /etc/openldap/slapd.d
service slapd restart

留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

PHP 與 JavaScript 之間傳值利用 json