OpenLDAP
OpenLDAP 是 LDAP 協議的一個開源實現。LDAP 服務器本質上是一個為只讀訪問而優化的非關係型數據庫。它主要用做地址簿查詢(如 email 客戶端)或對各種服務訪問做後台認證以及用戶數據權限管控。(例如,訪問 Samba 時,它可以起到域控制器的作用;或者 Linux 系統認證 時代替 /etc/passwd
的作用。)
ldap
開頭的命令(如:ldapsearch
)是客戶端工具,以 slap
開頭的命令(如:slapcat
)是服務端工具。本頁面內容僅基於一個基本的 OpenLDAP 安裝做簡要配置說明。
安裝[編輯 | 編輯原始碼]
OpenLDAP 軟件包同時包含了服務器和客戶端。請安裝軟件包 openldap包。
配置[編輯 | 編輯原始碼]
服務端[編輯 | 編輯原始碼]
- 如果你在使用過時的
slapd.conf
配置文件,可以使用以下命令將其轉換為新的cn=config
數據庫:
$ slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/
- 如果你現在的設備上存在 OpenLDAP 數據庫,且想要將其移除,可以清空
/var/lib/openldap/openldap-data/
下的所有內容。因此,請備份好DB_CONFIG
。
作為服務端,Slapd 將其所有配置保存在其數據庫內。因此,我們需要先將配置編寫為 LDIF 文件,然後將其導入。
首先創建 /var/lib/openldap/openldap-data/
目錄,LDAP 數據庫將存放在該位置下(OpenLDAP 將其稱為「database 1」):
# install -m 0700 -o ldap -g ldap -d /var/lib/openldap/openldap-data/
然後為 LDAP 配置數據庫(「database 0」)創建新路徑:
# install -m 0670 -o root -g ldap -d /etc/openldap/slapd.d
創建 /etc/openldap/config.ldif
,其中包括如下最小可用配置:
/etc/openldap/config.ldif
# The root config entry dn: cn=config objectClass: olcGlobal cn: config olcArgsFile: /run/openldap/slapd.args olcPidFile: /run/openldap/slapd.pid # Schemas dn: cn=schema,cn=config objectClass: olcSchemaConfig cn: schema # TODO: Include further schemas as necessary include: file:///etc/openldap/schema/core.ldif # The config database dn: olcDatabase=config,cn=config objectClass: olcDatabaseConfig olcDatabase: config olcRootDN: cn=Manager,$BASEDN # The database for our entries dn: olcDatabase=mdb,cn=config objectClass: olcDatabaseConfig objectClass: olcMdbConfig olcDatabase: mdb olcSuffix: $BASEDN olcRootDN: cn=Manager,$BASEDN olcRootPW: $PASSWD olcDbDirectory: /var/lib/openldap/openldap-data # TODO: Create further indexes olcDbIndex: objectClass eq
配置中需要根據需要修改部分內容:
- 所有
$BASEDN
都需要被替換為有效 DN。假設你手上的域名為example.com
,那你可以使用dc=example,dc=com
。 $PASSWD
需替換為加鹽並哈希化的密碼,可通過運行slappasswd
來生成。
另外,你也可以添加更多 schema,並創建額外索引來提高數據庫性能。具體實現取決於用例,但也有幾點建議可供參考。對於 LDAP 認證,你需要添加下面三個 schema 以使用 posixAccount
對象類來儲存用戶信息。
# TODO: Create further indexes olcDbIndex: objectClass eq olcDbIndex: uid pres,eq olcDbIndex: mail pres,sub,eq olcDbIndex: cn,sn pres,sub,eq olcDbIndex: dc eq # Additional schemas # RFC1274: Cosine and Internet X.500 schema include: file:///etc/openldap/schema/cosine.ldif # RFC2307: An Approach for Using LDAP as a Network Information Service # Check RFC2307bis for nested groups and an auxiliary posixGroup objectClass (way easier) include: file:///etc/openldap/schema/nis.ldif # RFC2798: Internet Organizational Person include: file:///etc/openldap/schema/inetorgperson.ldif
Allow logins to the ldap
user account with chsh
, typically selecting the shell /bin/bash
. 然後以 ldap
用戶身份導入設置:
[ldap]$ slapadd -n 0 -F /etc/openldap/slapd.d/ -l /etc/openldap/config.ldif
你也可以直接以 root
用戶運行該命令。但如果你這麼做,請確保 ldap
可以查看 /etc/openldap/slapd.d/
的內容:
# slapadd -n 0 -F /etc/openldap/slapd.d/ -l /etc/openldap/config.ldif # chown -R ldap:ldap /etc/openldap/*
如果一切順利,你將能看到 /etc/openldap/slapd.d
下的目錄,名稱類似於 cn=config
。
默認情況下,OpenLDAP 會不加密監聽所有網絡接口。如果要使其僅監聽本地 IP 接口,可以編輯 slapd.service
讀取的環境變量文件:
/etc/conf.d/slapd
SLAPD_URLS="ldap://127.0.0.1/ ldap://[::1]" SLAPD_OPTIONS=
最後,通過啟動 slapd.service
來啟動 slapd 守護進程。
- 如果你想讓目錄接受來自網絡的請求,建議使用 TLS。詳細內容請參考#基於 TLS 的 OpenLDAP。
- 如果你計劃使用 LDAP 服務器進行認證,建議查看 LDAP authentication#LDAP server setup 中的訪問控制配置部分。
- 不應使用 Berkeley DB(BDB)。對於一般的 slapd 數據庫,建議使用 slapd(8) 的 mdb 後端作為主後端。它使用了 OpenLDAP 自己的輕量內存映射數據庫(LMDB)實現來存儲數據,被設計用於取代 Berkeley DB 後端。官方倉庫中的 OpenLDAP 包默認使用 mdb。
客戶端[編輯 | 編輯原始碼]
客戶的配置文件位於 /etc/openldap/ldap.conf
。
這個配置很簡單,只需要將 BASE
設置為服務器的前綴,將 URI
設置為服務器的地址:
/etc/openldap/ldap.conf
BASE dc=example,dc=com URI ldap://localhost
要使用 SSL 的話:
URI
的協議 (ldap 或 ldaps) 要和 slapd 配置一致- 要使用自簽名的證書,在
ldap.conf
中添加TLS_REQCERT allow
一行 - 要使用由認證機構簽名的證書,在
ldap.conf
中添加TLS_CACERTDIR /usr/share/ca-certificates/trust-source
一行.
創建初始項[編輯 | 編輯原始碼]
base.ldif
,而不是遵循下面的步驟。配置好客戶端後,創建根項和管理員(Manager)角色項:
$ ldapadd -x -D 'cn=Manager,dc=example,dc=com' -W dn: dc=example,dc=com objectClass: dcObject objectClass: organization dc: example o: Example description: Example directory dn: cn=Manager,dc=example,dc=com objectClass: organizationalRole cn: Manager description: Directory Manager ^D
第一行後的內容是在 stdin 輸入的,或者用 -f
選項從文件或重定向讀入.
安裝後測試[編輯 | 編輯原始碼]
運行下面命令:
$ ldapsearch -x '(objectclass=*)' -b 'dc=example,dc=com'
或認證為 rootdn (將 -x
替換為 -D user -W
), 以上面的配置為例:
$ ldapsearch -D "cn=Manager,dc=example,dc=com" -W '(objectclass=*)' -b 'dc=example,dc=com'
應該能看到數據庫中的信息.
基於 TLS 的 OpenLDAP[編輯 | 編輯原始碼]
如果通過網絡訪問 OpenLDAP 服務器,尤其是當你的服務器上保存有敏感數據時,明文傳輸這些數據存在被他人嗅探的風險。下面章節將指導你如何設置 LDAP 服務器與客戶端之間的 SSL 連接以加密傳輸數據。
要使用 TLS,你必須獲得一個證書。測試時可以使用自簽名證書。關於證書的詳細信息請參閱 OpenSSL。
創建一個自簽名證書[編輯 | 編輯原始碼]
輸入下列命令創建一個自簽署證書:
$ openssl req -new -x509 -nodes -out slapdcert.pem -keyout slapdkey.pem -days 365
You will be prompted for information about your LDAP server. Much of the information can be left blank. The most important information is the common name. This must be set to the DNS name of your LDAP server. If your LDAP server's IP address resolves to example.org but its server certificate shows a CN of bad.example.org, LDAP clients will reject the certificate and will be unable to negotiate TLS connections (apparently the results are wholly unpredictable).
Now that the certificate files have been created copy them to /etc/openldap/ssl/
(create this directory if it does not exist) and secure them. slapdcert.pem
must be world readable because it contains the public key. slapdkey.pem
on the other hand should only be readable for the ldap user for security reasons:
# mv slapdcert.pem slapdkey.pem /etc/openldap/ssl/ # chmod -R 755 /etc/openldap/ssl/ # chmod 400 /etc/openldap/ssl/slapdkey.pem # chmod 444 /etc/openldap/ssl/slapdcert.pem # chown ldap /etc/openldap/ssl/slapdkey.pem
為 slapd 配置 SSL[編輯 | 編輯原始碼]
Edit the configuration to tell LDAP where the certificate files reside by executing the following command:
ldd /usr/bin/slapd
.ldapmodify -D 'cn=Manager,dc=example,dc=com' -W
dn: cn=config add: olcTLSCertificateFile olcTLSCertificateFile: /etc/openldap/ssl/slapdcert.pem - add: olcTLSCertificateKeyFile olcTLSCertificateKeyFile: /etc/openldap/ssl/slapdkey.pem
If you are using a signed SSL Certificate from a certification authority such as Let’s Encrypt, you will also need to specify the path to the root certificates database and your intermediary certificate. You will also need to change ownership of the .pem files and intermediary directories to make them readable to the user ldap
:
ldapmodify -D 'cn=Manager,dc=example,dc=com' -W
dn: cn=config add: olcTLSCACertificateFile olcTLSCACertificateFile: /etc/letsencrypt/live/ldap.my-domain.com/chain.pem - add: olcTLSCertificateFile olcTLSCertificateFile: /etc/letsencrypt/live/ldap.my-domain.com/cert.pem - add: olcTLSCertificateKeyFile olcTLSCertificateKeyFile: /etc/letsencrypt/live/ldap.my-domain.com/privkey.pem - add: olcTLSCACertificatePath olcTLSCACertificatePath: /usr/share/ca-certificates/trust-source
SSLv2/v3
Disable SSLv2/v3 and use strong ciphers.
ldapmodify -D 'cn=Manager,dc=example,dc=com' -W
dn: cn=config add: olcTLSProtocolMin olcTLSProtocolMin: 3.3 - add: olcTLSCipherSuite olcTLSCipherSuite: DEFAULT:!kRSA:!kDHE -
TLSProtocolMin specifies the minimum version in wire format, so "3.3" actually means TLSv1.2.
The TLSCipherSuite specifies a list of OpenSSL ciphers from which slapd will choose when negotiating TLS connections, in decreasing order of preference. In addition to those specific ciphers, you can use any of the wildcards supported by OpenSSL. Note: DEFAULT is a wildcard. See ciphers(1ssl) for description of ciphers, wildcards and options supported.
openssl ciphers -v ALL:COMPLEMENTOFALL
. Always test which ciphers will actually be enabled by TLSCipherSuite by providing it to OpenSSL command, like this: openssl ciphers -v 'DEFAULT'
.啟動基於 SSL 的 slapd[編輯 | 編輯原始碼]
You will have to edit the environment file read by slapd.service
to change the protocol slapd listens on:
/etc/conf.d/slapd
SLAPD_URLS="ldaps:///" SLAPD_OPTIONS=
Localhost connections do not need to use SSL. So, if you want to access the server locally you should change the SLAPD_URLS
line to:
SLAPD_URLS="ldap://127.0.0.1 ldaps:///"
Then restart slapd.service
. If it was enabled before, reenable it now.
TLS_REQCERT allow
to /etc/openldap/ldap.conf
on the client, or it will not be able connect to the server.下一步[編輯 | 編輯原始碼]
You now have a basic LDAP installation. The next step is to design your directory. The design is heavily dependent on what you are using it for. If you are new to LDAP, consider starting with a directory design recommended by the specific client services that will use the directory (PAM, Postfix, etc).
A directory for system authentication is the LDAP authentication article.
A nice web frontend is phpLDAPadmin.
備份 LDAP[編輯 | 編輯原始碼]
It is imperative that we have a backup of our LDAP database and configuration in case we ever need to restore for any number of reasons.
導出配置[編輯 | 編輯原始碼]
[ldap]$ slapcat -vF /etc/openldap/slapd.d -n 0 -l "$(hostname)-ldap-mdb-config-$(date '+%F').ldif"
導出數據庫[編輯 | 編輯原始碼]
[ldap]$ slapcat -v -n 1 -l "$(hostname)-ldap-database-$(date '+%F').ldif"
恢復 LDAP[編輯 | 編輯原始碼]
導入配置[編輯 | 編輯原始碼]
[ldap]$ slapadd -v -n 0 -F /etc/openldap/slapd.d -l <filename from config export>
導入數據庫[編輯 | 編輯原始碼]
[ldap]$ slapadd -v -n 1 -F /etc/openldap/slapd.d -l <filename from database export>
排錯[編輯 | 編輯原始碼]
檢查 slapd 配置[編輯 | 編輯原始碼]
可以使用該命令檢查設置:
$ slaptest -F /etc/openldap/slapd.d/ -v
檢查客戶端認證[編輯 | 編輯原始碼]
If you cannot connect to your server for non-secure authentication:
$ ldapsearch -x -H ldap://ldaservername:389 -D cn=Manager,dc=example,dc=exampledomain
and for TLS secured authentication with:
$ ldapsearch -x -H ldaps://ldaservername:636 -D cn=Manager,dc=example,dc=exampledomain
LDAP 服務突然停止[編輯 | 編輯原始碼]
如果你發現 slapd 似乎啟動後突然停止了,可以嘗試:
# chown -R ldap:ldap /var/lib/openldap
來允許 slapd 以「ldap」用戶身份對數據目錄進行寫入。
LDAP 服務未啟動[編輯 | 編輯原始碼]
嘗試從命令行啟動服務器並啟用調試輸出:
# slapd -u ldap -g ldap -h ldaps://ldaservername:636 -d Config,Stats
參閱[編輯 | 編輯原始碼]
- OpenLDAP 官方管理員文檔
- phpLDAPadmin - 類似 phpMyAdmin 的網頁管理工具。
- LDAP authentication
- 來自 Arch 用戶軟件倉庫 (AUR) 的 apachedirectorystudioAUR 是一個基於 Eclipse 的 LDAP 查看器,適用於 OpenLDAP。