LDAPサーバを作る
利用サイトもあるので一応押さえておく所の一つかな
過去記事 LDAP/サーバ構築
このサイトでは認証として ActiveDrectoryを模した samba、伝統的なNISを取り上げてる.
ここでは LDAP の openLDAP を扱う 別のLDAPエンジン FreeIPA LDAP/389DirectoryServer
RockyLinuxで作ってみた. nisもいいのだがsambaと連携するとパスワードが2系統存在する羽目になるので、それが見えにくいLDAPを作る. samba-adがパスワード系の一本化には最適と思っている
冗長構成 LDAP/failover、パスワードポリシーLDAP/ppolicy、ツールphpLDAPadmin
*RAID1な構成で運用すればよくね?って思うが、求めるところもあって作ってみた
インストール †
[root@ldap-server ~]# cat /etc/redhat-release
Rocky Linux release 8.6 (Green Obsidian)
[root@ldap-server ~]# dnf --enablerepo=powertools install openldap-servers openldap-clients
(「openldap-servers」パッケージは「powertools 」レポジトリに存在)
[root@ldap-server ~]# dnf info openldap-servers
Last metadata expiration check: 0:04:31 ago on Fri 11 Nov 2022 06:20:53 AM JST.
Installed Packages
Name : openldap-servers
Version : 2.4.46
Release : 18.el8
Architecture : x86_64
Size : 4.8 M
Source : openldap-2.4.46-18.el8.src.rpm
Repository : @System
From repo : powertools
Summary : LDAP server
URL : http://www.openldap.org/
License : OpenLDAP
Description : OpenLDAP is an open-source suite of LDAP (Lightweight Directory Access
: Protocol) applications and development tools. LDAP is a set of
: protocols for accessing directory services (usually phone book style
: information, but other information is possible) over the Internet,
: similar to the way DNS (Domain Name System) information is propagated
: over the Internet. This package contains the slapd server and related files.
[root@ldap-server ~]#
Rocky8でインストールされるのは「OpenLDAP-2.4.46」であった.
openldap-serverパッケージはあんまりないみたい. 他のディストリビューターは 「389 Directory Server」がメインっぽい
加えてユーザ[ldap]が追加されるみたい
[root@ldap-server ~]# id ldap
uid=55(ldap) gid=55(ldap) groups=55(ldap)
[root@ldap-server ~]#
パッケージのインストールはここまで。
次にldapが使用するデータベースBackendsを選ぶ. 大抵はBerkeley DB Backendsなんだろうけど、PostgreSQLらをBackendsにすることも可能みたい.
ここでは普通にBerkeley DB Backendsを使います. そのBerkeley DB Backendsの仕様書をコピーします
[root@ldap-server ~]# cp -a /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
(中身)
[root@ldap-server ~]# grep -v -e '^\s*#' -e '^\s*$' /var/lib/ldap/DB_CONFIG
set_cachesize 0 268435456 1
set_lg_regionmax 262144
set_lg_bsize 2097152
[root@ldap-server ~]#
稼働ユーザが ldap なのでファイルの所有権を変更します
[root@ldap-server ~]# chown ldap:ldap /var/lib/ldap/DB_CONFIG
[root@ldap-server ~]# ls -l /var/lib/ldap/DB_CONFIG
-rw-r--r--. 1 ldap ldap 845 Oct 12 2021 /var/lib/ldap/DB_CONFIG
[root@ldap-server ~]#
起動 †
っで起動します。他に事前の準備は?とCentOS6時代ならそう思うのだが、どうやらldapを動かしたまま
初期設定を調整するみたい. 事前に/etc/openldap/slapd.d/配下を弄るのもあるのだろうけど、結構複雑.
[root@ldap-server ~]# systemctl enable slapd.service --now
[root@ldap-server ~]# journalctl -u slapd
-- Logs begin at Fri 2022-11-11 06:18:46 JST, end at Fri 2022-11-11 06:28:19 JST. --
Nov 11 06:28:19 ldap-server systemd[1]: Starting OpenLDAP Server Daemon...
Nov 11 06:28:19 ldap-server runuser[20827]: pam_unix(runuser:session): session opened for user ldap by (uid=0)
Nov 11 06:28:19 ldap-server runuser[20827]: pam_unix(runuser:session): session closed for user ldap
Nov 11 06:28:19 ldap-server slapd[20837]: @(#) $OpenLDAP: slapd 2.4.46 (Oct 11 2021 21:12:19) $
mockbuild@ord1-prod-x86build002.svc.aws.rockylinux.org:/builddir/build/BUILD/openldap-2.4.46/openldap-2.4.46/servers/slapd
Nov 11 06:28:19 ldap-server slapd[20839]: slapd starting
Nov 11 06:28:19 ldap-server systemd[1]: Started OpenLDAP Server Daemon.
[root@ldap-server ~]#
*「too many open files」とかログにでるなら、slapd.serviceに「LimitNOFILE=4096」と追記する
そしてサービスとして提供しているのでfirewallに穴を空ける.
[root@ldap-server ~]# firewall-cmd --add-service=ldap --add-service=ldaps --zone=public --permanent
[root@ldap-server ~]# firewall-cmd --reload
[root@ldap-server ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens192
sources:
services: cockpit dhcpv6-client ldap ldaps ssh
ports:
protocols:
forward: no
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@ldap-server ~]#
「--add-service」で使われる「/usr/lib/firewalld/services/ldap.xml」と「/usr/lib/firewalld/services/ldaps.xml」は tcp/389、tcp/636 をそれぞれ空けている
ちなみにSELinuxは有効のままにしてます
[root@ldap-server ~]# getenforce
Enforcing
[root@ldap-server ~]#
ldapサーバ管理者のパスワードを定義 †
「サーバ管理者」と「ディレクトリ管理者」の2つを用意します
「サーバ管理者」ldapにスキーマの追加をするときとかに使う
「ディレクトリ管理者」ldap内に展開したスキーマ上でエントリー(データ)の登録・更新・削除に使用する
っでまずは「サーバ管理者」のパスワードを定義します
[root@ldap-server ~]# slappasswd
New password: <-- パスワードを入力
Re-enter new password: <-- 再入力
{SSHA}abecefghijklmnopqrstu123123123123123 <--- 暗号化されたパスワード
[root@ldap-server ~]#
「暗号化されたパスワード」文字列をコピーして、管理者のパスワードを定義するファイル「rootpw.ldif」を用意します
[root@ldap-server ~]# mkdir ldap <-- ldap作業用フォルダを設けた
[root@ldap-server ~]# cd ldap/
[root@ldap-server ldap]# vi rootpw.ldif
dn: olcDatabase={0}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}abecefghijklmnopqrstu123123123123123
[root@ldap-server ldap]#
この定義ファイル「rootpw.ldif」をldapに反映させる
[root@ldap-server ldap]# ldapadd -Y EXTERNAL -H ldapi:// -f rootpw.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "olcDatabase={0}config,cn=config"
[root@ldap-server ldap]#
一応ここで確認
[root@ldap-server ldap]# ldapsearch -x -LLL -W -D cn=config -b cn=config dn
Enter LDAP Password: <-- 「サーバ管理者」のパスワードを入力
dn: cn=config
dn: cn=schema,cn=config
dn: cn={0}core,cn=schema,cn=config
dn: olcDatabase={-1}frontend,cn=config
dn: olcDatabase={0}config,cn=config
dn: olcDatabase={1}monitor,cn=config
dn: olcDatabase={2}hdb,cn=config
[root@ldap-server ldap]#
スキーマ登録 †
「openldap-servers」パッケージに含まれている利用可能なスキーマは下記の通り
[root@ldap-server ~]# ls -CF /etc/openldap/schema/
collective.ldif core.schema dyngroup.ldif java.schema openldap.ldif ppolicy.schema
collective.schema cosine.ldif dyngroup.schema misc.ldif openldap.schema
corba.ldif cosine.schema inetorgperson.ldif misc.schema pmi.ldif
corba.schema duaconf.ldif inetorgperson.schema nis.ldif pmi.schema
core.ldif duaconf.schema java.ldif nis.schema ppolicy.ldif
[root@ldap-server ~]#
何を入れればいいのか不明なのだが、アカウント管理ならどうも「cosine.ldif」「nis.ldif」「inetorgperson.ldif」を適用すればいいみたい
内容的には下記のご様子.
- cosine.ldif
attributetype: personalTitle, mobileTelephoneNumber, buildingName,,
objectclass: newPilotPerson,,.
- nis.ldif
attributetype: gecos, homeDirectory, loginShell,,
objectclass: posixAccount, posixGroup,,.
- inetorgperson.ldif
attributetype: departmentNumber, employeeNumber, employeeType,,
objectclass: inetOrgPerson
スキーマ登録
[root@ldap-server ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
[root@ldap-server ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif
[root@ldap-server ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif
っで確認
[root@ldap-server ldap]# ldapsearch -x -LLL -W -D cn=config -b cn=config dn
Enter LDAP Password:
dn: cn=config
dn: cn=schema,cn=config
dn: cn={0}core,cn=schema,cn=config
dn: cn={1}cosine,cn=schema,cn=config <--- 増えた
dn: cn={2}nis,cn=schema,cn=config <--- 増えた
dn: cn={3}inetorgperson,cn=schema,cn=config <--- 増えた
dn: olcDatabase={-1}frontend,cn=config
dn: olcDatabase={0}config,cn=config
dn: olcDatabase={1}monitor,cn=config
dn: olcDatabase={2}mdb,cn=config
[root@ldap-server ldap]#
自分のドメインの設計(その1) †
ここではドメインは
dc=sybyl,dc=local
ディレクトリ管理者は
cn=Manager,dc=sybyl,dc=local
とします。
そのためのドメイン設定ファイル(ldapdomain.ldif)を用意します
その前に「サーバ管理者」の暗号化パスワード文字列を算出したように再度slappasswdを使って今度は「ディレクトリ管理者」の暗号化パスワードを求めます
[root@ldap-server ~]# slappasswd
New password: <-- 「ディレクトリ管理者」向けのパスワードを入力
Re-enter new password:
{SSHA}qawsedrftgyhujikolp <--- 暗号化されたパスワード
[root@ldap-server ~]#
この「暗号化されたパスワード」をコピーしてドメイン設定ファイルを作成します
[root@ldap-server ~]# vi ldap/ldapdomain.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=Manager,dc=sybyl,dc=local" read by * none
dn: olcDatabase={2}mdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=sybyl,dc=local
dn: olcDatabase={2}mdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=Manager,dc=sybyl,dc=local
dn: olcDatabase={2}mdb,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}qawsedrftgyhujikolp <-- ここに「ディレクトリ管理者」の「暗号化されたパスワード」を入れる
dn: olcDatabase={2}mdb,cn=config
changetype: modify
add: olcAccess
olcAccess: {0}to attrs=userPassword,shadowLastChange by
dn="cn=Manager,dc=sybyl,dc=local" write by anonymous auth by self write by * none
olcAccess: {1}to dn.base="" by * read
olcAccess: {2}to * by dn="cn=Manager,dc=sybyl,dc=local" write by * read
[root@ldap-server ~]#
これをLDAPに反映させます. 上記をコピペで貼り付けると失敗する可能性があります. 空行に何かあるようで、空行を削除して、再度空行を設ければ行けるかも
[root@ldap-server ~]# ldapmodify -H ldapi:/// -f ldap/ldapdomain.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "olcDatabase={1}monitor,cn=config"
modifying entry "olcDatabase={2}mdb,cn=config"
modifying entry "olcDatabase={2}mdb,cn=config"
modifying entry "olcDatabase={2}mdb,cn=config"
modifying entry "olcDatabase={2}mdb,cn=config"
[root@ldap-server ~]#
で完了。
自分のドメインの設計(その2) †
トップドメインを用意します。
下記ファイル(domain.ldif)には、
- トップドメインの定義(dn: dc=sybyl,dc=local)
- 管理者の定義(dn: cn=Manager,dc=sybyl,dc=local)
- ユーザの定義枠(dn: ou=People,dc=sybyl,dc=local)
- グループの定義枠(dn: ou=Group,dc=sybyl,dc=local)
が含まれてます 投入時は下記の日本語部分は削除してください
[root@ldap-server ~]# vi ldap/domain.ldif
#TOPドメインの定義
dn: dc=sybyl,dc=local
objectClass: top
objectClass: dcObject
objectclass: organization
dc: sybyl
o: sybyl local
description: sybyl Ldap
#管理者の定義
dn: cn=Manager,dc=sybyl,dc=local
objectClass: organizationalRole
cn: Manager
description: Directory Manager
#people(/etc/passwdなもの)の定義
dn: ou=People,dc=sybyl,dc=local
objectClass: organizationalUnit
ou: People
#group(/etc/groupなもの)の定義
dn: ou=Group,dc=sybyl,dc=local
objectClass: organizationalUnit
ou: Group
[root@ldap-server ~]#
これは新規追加となるので「ldapadd」を使います。こちらも上記をコピペで貼り付けると失敗する可能性があります. 同じように空行を一旦削除して、再度空行を入れてみてください
「Enter LDAP Password:」とパスワードを問われるが、これはディレクトリ管理者のパスワード.
[root@ldap-server ~]# ldapadd -x -D cn=Manager,dc=sybyl,dc=local -W -f ldap/domain.ldif
Enter LDAP Password:
adding new entry "dc=sybyl,dc=local"
adding new entry "cn=Manager,dc=sybyl,dc=local"
adding new entry "ou=People,dc=sybyl,dc=local"
adding new entry "ou=Group,dc=sybyl,dc=local"
[root@ldap-server ~]#
これで枠が完成した。
「ldap_bind: Invalid credentials (49)」とか表示されたら恐らくパスワードがあっていない.
パスワードの修正ですが、ldifを作って更新なのですが、「ldapvi」なるプログラムを使うとviで修正が可能となる
[root@ldap-server ~]# yum --enablerepo=epel install ldapvi
[root@ldap-server ~]# ldapvi -Y EXTERNAL -h ldapi:/// -b olcDatabase={2}mdb,cn=config
# -*- coding: utf-8 -*-
# http://www.lichteblau.com/ldapvi/manual#syntax
# SASL output:
# SASL/EXTERNAL authentication started
# SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
# SASL SSF: 0
0 olcDatabase={2}mdb,cn=config
objectClass: olcDatabaseConfig
objectClass: olcMdbConfig
olcDatabase: {2}mdb
olcDbDirectory: /var/lib/ldap
olcDbIndex: objectClass eq,pres
olcDbIndex: ou,cn,mail,surname,givenname eq,pres,sub
olcAccess: {0}to attrs=userPassword,shadowLastChange by dn="cn=Manager,dc=sybyl,dc=local" write by anonymous auth by self write by * none
olcAccess: {1}to dn.base="" by * read
olcAccess: {2}to * by dn="cn=Manager,dc=sybyl,dc=local" write by * read
olcRootPW: {SSHA}qawsedrftgyhujikolp <---- ここの値を修正する
olcSuffix: dc=sybyl,dc=local
olcRootDN: cn=Manager,dc=sybyl,dc=local
[root@ldap-server ~]#
ちょいとした修正には便利なツールです
メモ †
「ldap anonymous bind」 CentOS7のOpenLDAPは「ldap anonymous bind」が既定で有効みたい
qnapとかは既定で無効化されている. この有効/無効は単に olcAccess で定義されている模様.
現状は
[root@ldap-server ~]# ldapsearch -Y EXTERNAL -H ldapi:/// -b olcDatabase={2}mdb,cn=config -o ldif-wrap=no | grep olcAccess
:
olcAccess: {0}to attrs=userPassword,shadowLastChange by dn="cn=Manager,dc=sybyl,dc=local" write by anonymous auth by self write by * none
olcAccess: {1}to dn.base="" by * read
olcAccess: {2}to * by dn="cn=Manager,dc=sybyl,dc=local" write by * read
[root@ldap-server ~]#
と「olcAccess: {2}」にて全てを「read」となっている
参照先 https://kazuhira-r.hatenablog.com/entry/2018/12/07/000729
LDAPSを有効にしてみる †
クラスター内で認証、研究室内で認証なら通信の暗号は要らないと思っていたのだが、rsh系のオワコン扱いと同じようにLDAPもLDAPSで運用しないとSELinux君が暴れるそうな....なので施してみた.
本筋は秘密鍵でサーバ申請書「CSR(Certificate Signing Request)」を作って、
それをCA認証局に持っていってサーバ証明書「CRT(SSL Cerver Certificate)」を作ってもらうのですが、
サーバ申請書もCA認証局も同じなので、得られるのは自己署名な証明書サーバ証明書「CRT(SSL Cerver Certificate)」となります.
[root@ldap-server ~]# cd /etc/openldap/certs/
[root@ldap-server certs]# ls -l
total 0
[root@ldap-server certs]#
(ldap-serverの秘密鍵[key]を作る)
[root@ldap-server certs]# openssl genrsa 2048 > server.key
(秘密鍵を使ってサーバ申請書[CSR]を作る)
[root@ldap-server certs]# openssl req -new -key server.key -out server.csr
:
Country Name (2 letter code) [XX]:JP <-- JPのみ入力してあとはリターンキーのみで逃げる
:
[root@ldap-server certs]#
(秘密鍵とサーバ申請書[CSR]からサーバ証明書[CRT]を得る)
[root@ldap-server certs]# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650
[root@ldap-server certs]# ls -l
total 12
-rw-r--r--. 1 root root 1115 Apr 23 08:37 server.crt
-rw-r--r--. 1 root root 952 Apr 23 08:37 server.csr
-rw-r--r--. 1 root root 1675 Apr 23 08:31 server.key
[root@ldap-server certs]#
これらのファイルのありかをopenldapに伝える
[root@ldap-server ~]# vi ldap/ldaps.ldif
dn: cn=config
changetype: modify
replace: olcTLSCertificateFile
olcTLSCertificateFile: /etc/openldap/certs/server.crt
dn: cn=config
changetype: modify
replace: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/openldap/certs/server.key
[root@ldap-server ~]#
[root@ldap-server ~]# ldapadd -Y EXTERNAL -H ldapi:// -W -f ldap/ldaps.ldif
Enter LDAP Password:
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "cn=config"
modifying entry "cn=config"
[root@ldap-server ~]#
「cn=config」部分を確認してみると
[root@ldap-server ~]# ldapsearch -Y EXTERNAL -H ldapi:/// -b cn=config cn=config -o ldif-wrap=no
:
olcTLSCertificateFile: /etc/openldap/certs/server.crt
olcTLSCertificateKeyFile: /etc/openldap/certs/server.key
:
[root@ldap-server ~]#
っと確かに存在している. これで再起動してldapsを確認してみる
[root@ldap-server ~]# systemctl restart slapd
[root@ldap-server ~]# lsof -i
:
slapd 8319 ldap 7u IPv4 78179 0t0 TCP *:ldap (LISTEN)
slapd 8319 ldap 8u IPv6 78180 0t0 TCP *:ldap (LISTEN)
slapd 8319 ldap 9u IPv4 78183 0t0 TCP *:ldaps (LISTEN)
slapd 8319 ldap 10u IPv6 78184 0t0 TCP *:ldaps (LISTEN)
:
[root@ldap-server ~]#
確かにldapsで通信可能みたい
ldaps経由でldapsearchコマンドで検索すると「使った」CA認証局はプライベートなので事前にTLS_REQCERTは無効にしておきます
[root@ldap-server ~]# ldapsearch -x -H ldaps://ldap-server -b dc=sybyl,dc=local
ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1) <-- ldapsで繋げるとエラーになる
[root@ldap-server ~]# echo "TLS_REQCERT never" >> /etc/openldap/ldap.conf
[root@ldap-server ~]# ldapsearch -x -H ldaps://ldap-server -b dc=sybyl,dc=local
:
(データがぞろぞろ表示される)
:
[root@ldap-server ~]#
完了 †
これで openLDAP サーバが作れました。器/枠のみです。次にこの枠にデータを流し込みます.
|