2016年7月9日 星期六

【Linux】在 CentOS 6 上安裝 LDAP Server 2.4

最近因任務需求必須將 LDAP Server 從 FreeBSD 移至 CentOS 上面,做個簡單的小筆記
原則上在 CentOS 下安裝 OpenLDAP 是件很輕鬆的事,用 YUM 裝一下就好了,我的環境如下
CentOS:6    x86_64
OpenLDAP:2.4.40
####################
yum install -y openldap-servers openldap-clients openldap-devel
/bin/cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG   -f 
#/bin/cp /usr/share/openldap-servers/slapd.conf.obsolete /etc/openldap/slapd.conf   -f 
複製前一版slapd.conf.obsolete
/bin/cp /163.17.209.1/etc/slapd.conf.obsolete  /etc/openldap/slapd.conf  -f
slappasswd
###################
/bin/cp /163.17.209.1/etc/openldap/slapd.conf  /etc/openldap/.  -f 
vim  /etc/openldap/slapd.conf
change root password
####################################
/bin/cp /163.17.209.1/etc/rsyslog.conf  /etc/.    -f
mkdir  /etc/openldap/ldif
/bin/cp /163.17.209.1/etc/openldap/ldif/base.ldif  /etc/openldap/ldif/.    -f 
vim  /etc/openldap/ldif/base.ldif
###########################################
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 /var/lib/ldap
chown -R ldap:ldap /etc/openldap/slapd.d
service slapd start
chkconfig --list slapd
chkconfig --level 345 slapd on
##############################################
ldapsearch -x -b "dc=wcjs,dc=tc,dc=edu,dc=tw"
############################################
rm -rf /var/lib/ldap/*
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
############匯入資料####/home/lia/slapcat/y/20150301ldap.ldif####################
############匯入資料####/home/lia/slapcat/y/20150301ldap.ldif####################
############匯入資料####/home/lia/slapcat/y/20150301ldap.ldif####################
############匯入資料####/home/lia/slapcat/y/20150301ldap.ldif####################
slapadd -v -l /163.17.209.1/etc/ldap.ldif.1 
chown -R ldap:ldap /var/lib/ldap
service slapd restart
##################################################
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

資料備份當使用者帳號非常多時,備份就是一個重要動作了(就算帳號不多也要做啦 ^^)
1. 匯出資料
slapcat -n 2 > ldap.ldif <== 將LDAP的資料庫資料匯出到ldap.ldif檔
註:在CenetOS6,openldap-servers-2.4以上版本,第一個db是不能匯出的,必需下 -n 2的參數
2. 匯入(重建)資料
 2.1. 刪除/var/lib/ldap/*
 2.2. cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
 2.3. slapadd -v -l ldap.ldif 
 2.4. ll /var/lib/ldap
 2.5. chkown -R ldap:ldap /var/lib/ldap
 2.6. service slapd restart
註:2.3為匯入的重點步驟,其他是重建檔案及重新啟動
范老師上課資料
rm -rf /var/lib/ldap/*
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
slapadd -v -l /163.17.209.1/etc/ldap.ldif.1 
chown -R ldap:ldap /var/lib/ldap
service slapd restart

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
 #######################################################################
 # 以下為 ACL Rules 存取安全設定 (增加 Access Control 部份)
 # 合法的使用者能讀取 LDAP 資料庫,但不能讀/寫其它人的密碼

 #######################################################################
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

 #######################################################################
 # 為了讓 nss_ldap 機制能夠運作,必須開啟 anonymous 讀取的權限,但是限制它不能從任何 IP 讀取
 #######################################################################
access to *
       by self write
       by users read
##      by anonymous peername.IP=127.0.0.1 read
##      by anonymous peername.IP=192.168.1.0%255.255.255.0 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/ldap-master.example.com@EXAMPLE.COM
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
參考資料