Centos 7:安裝openldap servers + clients

現今網路常用的服務很多,例如apache、mail、samba等,在這些常用的服務裡,會有使用者帳號的問題,每當要使用mail時就要輸入mail的帳號密碼,存取samba要有samba的帳號密碼,服務越多要記的帳號密碼就更多,如果需要定期修改密碼就更累了。

LDAP〈Lightweight Directory Access Protocol 輕型目錄存取協定〉是一種目綠服務,可用來記錄各種的人員資訊,就像是通訊錄一樣。進階的做法,也可以拿來做帳號整合,若是AP有支援,那麼要使用同一組帳號密碼就不再是難事了。

LDAP,可以把他想成資料庫的一種,對於有資料庫設計經驗的人應該不成問題,但若之前都沒有碰過,那就把LDAP想成組織圖一樣,只是這個組織圖是構築在你的腦海裡,所以最好準備紙筆,把架構畫出來,才好進行設定。組織圖大約如下:
                    testuse.example.com
            /          \
                  login         company
                 /   \                /     \
                        user group   unit     customer
                         /     |     \
                          mis account hr
若要表示一筆人員記錄,可使用:cn=user name,ou=gourp,dc=your,dc=domain。

若是上面的組織圖來說,在mis部門下有一位coodie,那麼對於這位coodie的表示法為:cn=coodie,ou=mis,ou=unit,ou=company,dc=testuse,dc=idv,dc=tw。

上面這一長串,我們稱之為一個 dn 值,在LDAP的表示法,都是由小到大,也就是人名先表示、再表示部門、單位(這和西方的門牌表示法是一樣的)。

LDAP也可以使用中文,例如王小華是在mis部門下,依名字設定dn,值為:
cn=王小華,ou=mis,ou=unit,ou=company,dc=testuse,dc=example,dc=com。

安裝所需軟體
[root@openldap ~]# yum -y install openldap compat-openldap openldap-clients openldap-servers openldap-servers-sql openldap-devel

啟動服務
[root@openldap ~]# systemctl start slapd
[root@openldap ~]# systemctl enable slapd

slapd即standard alone ldap daemon,該process預設的port為389。

檢視port
[root@openldap ~]# netstat -tunlp | grep 389

tcp         0      0 0.0.0.0:389             0.0.0.0:*               LISTEN      2198/slapd
tcp6       0      0 :::389                      :::*                        LISTEN      2198/slapd

設定LDAP root password
[root@openldap ~]# slappasswd -h {SSHA} -s nchc1234

{SSHA}I/7GK6qqa/vzuybNtQr2WlPmyPoZty3H    # 這段hash值要記下來,稍後會用的到

OpenLDAP servers的配置檔位於/etc/openldap/slapd.d/。在開始使用LDAP服務之前,我們必須要更新這3個變數的內容:olcSuffix、olcRootDN、olcRootPW。

olcSuffix:suffix是用來定義LDAP的根尾碼,例如:dc=testuse,dc=example,dc=com。

olcRootDN:rootdn指的是LDAP的root,設定之後就可以對整個LDAP系統做增修減等動作。

一般cn值為Manager。例如:cn=Manager,dc=testuse,dc=example,dc=com
olcRootPW:root的密碼,通常會加密。

Above entries are to be updated in /etc/openldap/slapd.d/cn=config/olcDatabase={2}hdb.ldif file. Manually edit of LDAP configuration is not recommended as you will lose changes whenever you run ldapmodify command.

設定openldap server
[root@openldap slapd.d]# pwd
/etc/openldap/slapd.d

# 新增db.ldif
[root@openldap slapd.d]# vim db.ldif

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=example,dc=com

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=ldapadm,dc=example,dc=com    # cn以及dc需自訂,這裡的範例是ldapadm,example.com

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootPW
olcRootPW: {SSHA}I/7GK6qqa/vzuybNtQr2WlPmyPoZty3H    # 貼上剛剛記下來的hash值

ldif(LDAP Data Interchange Format)是LDAP中數據交換的一種文件格式。文件內容採用的是key-value形式,注意value後面不能有空格。

dn即distingush name。

olc即Online Configuration,表示寫入LDAP後不需要重啟即可生效。

changetype: modify表示修改一個entry,changetype的值可以是add,delete, modify等。

olcRootDN,設定root的distingush name。

replace: olcRootPW表示用olcRootPW的屬性對這個entry做取代。

olcRootPW: {SSHA}krOGXDmiCdSXuXocOf10F96LJO5ijdXo指定了屬性值,這邊就是root的密碼。

將db.ldif的設定值導入LDAP server
[root@openldap slapd.d]# ldapmodify -Y EXTERNAL -H ldapi:/// -f db.ldif

Make a changes to /etc/openldap/slapd.d/cn=config/olcDatabase={1}monitor.ldif (Do not edit manually) file to restrict the monitor access only to ldap root (ldapadm) user not to others.

新增monitor.ldif
[root@openldap slapd.d]# vim monitor.ldif

dn: olcDatabase={1}monitor,cn=config
changetype: modify
replace: olcAccess
olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external, cn=auth" read by dn.base="cn=ldapadm,dc=example,dc=com" read by * none

olcAccess即access,該key用於指定目錄的ACL,即誰有甚麼權限可以存取甚麼。

將monitor.ldif的設定值導入LDAP server
[root@openldap slapd.d]# ldapmodify -Y EXTERNAL -H ldapi:/// -f monitor.ldif

建立LDAP server憑證
[root@openldap slapd.d]# openssl req -new -x509 -nodes -out /etc/openldap/certs/exampleldapcert.pem -keyout /etc/openldap/certs/exampleldapkey.pem -days 365

Generating a 2048 bit RSA private key
...+++
.....................................+++
writing new private key to '/etc/openldap/certs/exampleldapkey.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]: TW
State or Province Name (full name) []: Taiwan
Locality Name (eg, city) [Default City]: Hsinchu
Organization Name (eg, company) [Default Company Ltd]:coodie
Organizational Unit Name (eg, section) []:coodie
Common Name (eg, your name or your server's hostname) []:openldap
Email Address []:XXX@nchc.narl.org.tw

設定pem檔的使用者以及群組
[root@openldap slapd.d]# chown -R ldap:ldap /etc/openldap/certs/*.pem

檢視LDAP憑證
[root@openldap slapd.d]# ll /etc/openldap/certs/*.pem

-rw-r--r-- 1 ldap ldap 1298 Sep 18 13:30 /etc/openldap/certs/exampleldapcert.pem
-rw-r--r-- 1 ldap ldap 1708 Sep 18 13:30 /etc/openldap/certs/exampleldapkey.pem

建立certs.ldif〈Create certs.ldif file to configure LDAP to use secure communication using a self-signed certificate.〉
[root@openldap slapd.d]# vim certs.ldif

dn: cn=config
changetype: modify
replace: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/openldap/certs/exampleldapkey.pem

dn: cn=config
changetype: modify
replace: olcTLSCertificateFile
olcTLSCertificateFile: /etc/openldap/certs/exampleldapcert.pem

將certs.ldif的設定值導入LDAP server
[root@openldap slapd.d]# ldapmodify -Y EXTERNAL -H ldapi:/// -f certs.ldif

測試設定檔
[root@openldap slapd.d]# slaptest -u

config file testing succeeded    # 確認驗證成功

設定DB_CONFIG
(Copy the sample database configuration file to /var/lib/ldap and update the file permissions.)
[root@openldap slapd.d]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
[root@openldap slapd.d]# chown ldap:ldap /var/lib/ldap/*

DB_CONIFG中主要是關於Berkeley DB的相關配置,此設定檔包含index快取量,可調整效能表現。

導入cosine、nis and inetorgperson LDAP schemas
[root@openldap slapd.d]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
[root@openldap slapd.d]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif
[root@openldap slapd.d]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif

schema包含為了支持特殊場景相關的屬性,可選擇性導入。

建立base.ldif for domain
[root@openldap slapd.d]# vim base.ldif

dn: dc=example,dc=com
dc: example
objectClass: top
objectClass: domain

dn: cn=ldapadm ,dc=example,dc=com
objectClass: organizationalRole
cn: ldapadm
description: LDAP Manager

dn: ou=People,dc=example,dc=com
objectClass: organizationalUnit
ou: People

dn: ou=Group,dc=example,dc=com
objectClass: organizationalUnit
ou: Group

DC即Domain Component,LDAP目錄類似文件系統目錄dc=example,dc=com相當於/com/example。

CN即Common Name,CN有可能代表一個用戶名稱,例如cn=Manager,dc=example,dc=com表示在/com/example區域下的root用戶Manager。

OU即Organizational Unit,例如ou=People,dc=example,dc=com表示在/com/example區域下的一個組織單元People。

建立目錄樹狀結構
[root@openldap slapd.d]# ldapadd -x -W -D "cn=ldapadm,dc=example,dc=com" -f base.ldif

Enter LDAP Password: 
adding new entry "dc=example,dc=com"

adding new entry "cn=ldapadm ,dc=example,dc=com"

adding new entry "ou=People,dc=example,dc=com"


adding new entry "ou=Group,dc=example,dc=com"

x:使用簡單的驗證(亦即不用進行SASL認證)。

D:指定dn(Distinguished Name)識別名稱連結LDAP directory。

W:使用互動式輸入管理者密碼,有別於-w(小寫),需緊接著指令之後鍵入密碼。

f:指定 LDIF 檔名。

建立一個新使用者的ldif(創多個使用者時記得要更改uidNumber)
[root@openldap slapd.d]# vim ldapuser1.ldif

dn: uid=ldapuser1,ou=People,dc=example,dc=com
objectClass: top
objectClass: account
objectClass: posixAccount
objectClass: shadowAccount
cn: ldapadm
uid: ldapuser1
uidNumber: 9999
gidNumber: 100    # 100為user
homeDirectory: /home/ldapuser1
loginShell: /bin/bash
gecos: ldapuser1
userPassword: {crypt}x
shadowLastChange: 17058
shadowMin: 0
shadowMax: 99999
shadowWarning: 7

dn: uid=ldapuser1,ou=People,dc=example,dc=com,每一筆資料都要有唯一的dn(distingush name) ,以供查詢時使用。

dn: 中的uid,ou名稱都可以自己定義,例如dn:

useridentificationname=user,unit=People,dc=example,dc=com。

利用上述的檔案+ldapadd指令,新建一個LDAP使用者
[root@openldap slapd.d]# ldapadd -x -W -D "cn=ldapadm,dc=example,dc=com" -f ldapuser1.ldif

Enter LDAP Password: 

adding new entry "uid=ldapuser1,ou=People,dc=example,dc=com"

設定ldapuser1的密碼
[root@openldap slapd.d]# ldappasswd -s test123 -W -D "cn=ldapadm,dc=example,dc=com" -x "uid=ldapuser1,ou=People,dc=example,dc=com"

-s specify the password for the username。

-x username for which the password is changed。

-D Distinguished name to authenticate to the LDAP server。

驗證ldapuser1內容
[root@openldap slapd.d]# ldapsearch -x cn=ldapadm -b dc=example,dc=com

# extended LDIF
#
# LDAPv3
# base <dc=example,dc=com> with scope subtree
# filter: cn=ldapuser1
# requesting: ALL
#

# ldapuser1, People, example.com
dn: uid=ldapuser1,ou=People,dc=example,dc=com
objectClass: top
objectClass: account
objectClass: posixAccount
objectClass: shadowAccount
cn: ldapuser1
uid: ldapuser1
uidNumber: 9999
gidNumber: 100
homeDirectory: /home/ldapuser1
loginShell: /bin/bash
gecos: ldapuser1
shadowLastChange: 17058
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
userPassword:: e1NTSEF9cEExWHB6azlRQ2lsS3ppV0JqYjdHSXZEbFBxYW4wcmo=

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

x:使用簡單的驗證(亦即不用進行 SASL 認證)。

b:指定 BDN(Base Distinguished Name),也就是篩選條件。

利用ldapdelete指令移除LDAP使用者(選項)
[root@openldap slapd.d]# ldapdelete -W -D "cn=ldapadm,dc=example,dc=com" "uid=ldapuser1,ou=People,dc=example,dc=com"

建立防火牆規則
[root@openldap ~]# firewall-cmd --permanent --add-service=ldap
[root@openldap ~]# firewall-cmd --reload

設定rsyslog,將LDAP log記錄在/var/log/ldap.log
[root@openldap ~]# echo 'local4.* /var/log/ldap.log' >> /etc/rsyslog.conf

# 重啟rsyslog服務
[root@openldap ~]# systemctl restart rsyslog

安裝LDAP client
[root@openldap ~]# yum install -y openldap-clients nss-pam-ldapd

用authconfig指令設定LDAP認證
[root@openldap ~]# authconfig --enableldap --enableldapauth --ldapserver=192.168.10.236 --ldapbasedn="dc=example,dc=com" --enablemkhomedir --update

# 重啟nslcd
[root@openldap ~]# systemctl restart  nslcd

用getent查詢LDAP帳號
[root@openldap ~]# getent passwd ldapuser1


ldapuser1:x:9999:100:ldapuser1:/home/ldapuser1:/bin/bash

測試用LDAP帳號登入
testuse login: ldapuser1
Password:
Creating directory '/home/ldapuser1'
Last login: Sat Sep 30 11:28:31 on tty1
[ldapuser1@testuse ~]$


*************************************************************************
備註:手動建立的ldif檔若是放在/etc/openldap/slapd.d/cn=config/下,會出現下列錯誤:
[root@openldap cn=config]# slaptest -u
5d2d3b90 UNKNOWN attributeDescription "CHANGETYPE" inserted.
5d2d3b90 UNKNOWN attributeDescription "REPLACE" inserted.
5d2d3b90 is_entry_objectclass("olcDatabase={2}hdb,cn=config,cn=config", "2.16.840.1.113730.3.2.6") no objectClass attribute
5d2d3b90 is_entry_objectclass("olcDatabase={1}monitor,cn=config,cn=config", "2.16.840.1.113730.3.2.6") no objectClass attribute
config file testing succeeded
-------------------------------------------------------------------------------------------------
[root@openldap cn=config]# systemctl restart slapd

[root@openldap cn=config]# systemctl status slapd -l
......
Jul 16 10:53:47 openldap slapd[20506]: PROXIED attributeDescription "DC" inserted.
Jul 16 10:53:47 openldap slapd[20506]: UNKNOWN attributeDescription "DC" inserted.
Jul 16 10:53:47 openldap slapd[20506]: config error processing DC=example,DC=com,cn=config:

......
-------------------------------------------------------------------------------------------------
[root@openldap cn=config]# ldapadd -x -W -D "cn=ldapadm,dc=example,dc=com" -f ldapuser1.ldif
Enter LDAP Password:

ldap_bind: Invalid credentials (49)  ← 輸入正確的密碼卻得到認證錯誤的訊息

*************************************************************************

留言

這個網誌中的熱門文章

Apache的一些安全性設定

Ubuntu 16.04:安裝ldap client