OpenLDAP

来自Gea-Suan Lin's Wiki
跳到导航 跳到搜索

OpenLDAP是一套LDAP服务器软件。

安装

由于OpenLDAP 2.4在2007年十月发行,至今只有一直发行minor release,所以通常都可以透过系统的套件装到合理的版本:

sudo apt -y install slapd

另外会安装ldap-utils,主要是因为CLI下常用的工具不在OpenLDAP内提供,而是透过第三方的方式操作(像是ldapmodifyldapdelete这些指令):

sudo apt -y install ldap-utils

基本概念

这边会提到一些OpenLDAP与一般LDAP比较不一样的地方,以及一些值得提出来的基本概念。

管理员账号名称

在OpenLDAP的文件的范例中,都是以cn=Manager当作是管理权限账号。但透过网络搜寻可以看到很多人是使用cn=admin,甚至是cn=root的账号。在这份文件里面我们采用了OpenLDAP文件范例的惯例来设计。

管理员账号本体

在OpenLDAP的设定档中,你可以用rootdnrootpw设定管理员账号与密码,但这个方式将密码写死在设定档内,对于有政策需要定期更换密码时会比较困难。

在这种情境下我们会考虑只设定rootdn而不设定rootpw,另外建立一个物件,挂上organizationalRole,并赋予simpleSecurityObject这个类别,将密码放在数据库内管理:

dn: cn=Manager,dc=example,dc=com
objectClass: organizationalRole
objectClass: simpleSecurityObject
cn: Manager
userPassword: {CRYPT}$1$xxxxxxxx$yyyyyyyyyyyyyyyyyyyyyy

设定档

/etc/default/slapd

Ubuntu 16.04内的版本预设会使用目录结构设定档(预设读取/etc/ldap/slapd.d/整个目录),但这跟网络上一般使用单档设定不同(通常会是/etc/ldap/slapd.conf),所以我们要告知slapd使用档案设定档。

这要修改/etc/default/slapd的内容,将SLAPD_CONF参数从空的值改为/etc/ldap/slapd.conf

# Default location of the slapd.conf file or slapd.d cn=config directory. If
# empty, use the compiled-in default (/etc/ldap/slapd.d with a fallback to
# /etc/ldap/slapd.conf).
SLAPD_CONF=/etc/ldap/slapd.conf

/etc/ldap/slapd.conf

我们要将slapd.conf的范本复制到/etc/ldap/slapd.conf之后修改(范本在/usr/share/slapd/slapd.conf这边),里面有一些变数需要设定:

  • @BACKEND@设为mdb
  • @SUFFIX@设为dc=example,dc=com
  • @ADMIN@设为cn=Manager,dc=example,dc=com

其中的dc=example,dc=com为范例,可以依照实际组织设定。

mdb

另外因为使用mdb,需要将bdb(指BerkeleyDB)相关的dbconfig的参数都删除掉。

密码设定

网络上大多数的文章(过旧的文章)都会推荐使用{SSHA},但这只是salted-SHA1,计算速度很快。目前OpenLDAP内建的算法都没有足够的抵抗能力,需要透过外部支援降低被攻击的可能性。

考虑到支援度,APR1-MD5是还算堪用的算法,可以让攻击的计算速度慢一千倍(相比于SSHA)[1],所以透过以下的设定要求透过系统底层的crypt()使用APR1-MD5:

password-hash   "{CRYPT}"
password-crypt-salt-format "$1$%.8s"

如果支援度没有问题,可以考虑bcrypt的方式($2$),对于暴力计算的抵抗力会更好。

rootdn与rootpw

设定rootdn的dn会有完整的管理权限,不需要另外使用ACL授权。而rootdn的密码刚开始需要用rootpw设定,可以用下面的指令产生rootpw

slappasswd -h '{CRYPT}' -c '$1$%.8s'

然后把设定写入/etc/ldap/slapd.conf内:

rootdn          "cn=Manager,dc=example,dc=com"
rootpw          "{CRYPT}$1$xxxxxxxx$yyyyyyyyyyyyyyyyyyyyyy"

资料初始化设定

预设数据库内应该是空的(可以透过ldapsearch -x的指令确认),所以会需要塞一些资讯进去。资料初始化会透过ldapmodify指令进行,这会需要安装前面提到的ldap-utils

Domain

先加入domain本身,档案写到init-domain.ldif,然后透过ldapmodify -x -D 'cn=Manager,dc=example,dc=com' -W -f init-domain.ldif加入即可:(需要修改dndc的值)

dn: dc=example,dc=com
changetype: add
objectclass: top
objectclass: domain
dc: example

People

再来是产生ou=People,dc=example,dc=com

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

Groups

再来是产生ou=Groups,dc=example,dc=com

dn: ou=Groups,dc=example,dc=com
changetype: add
objectClass: organizationalUnit
ou: Groups

Manager

这是管理员的身份,可以不在数据库内设定,而是透过slapd.conf内的ACL与rootpw设定。

dn: cn=Manager,dc=example,dc=com
changetype: add
objectClass: organizationalRole
objectClass: simpleSecurityObject
cn: Manager
userPassword: {CRYPT}$1$xxxxxxxx$yyyyyyyyyyyyyyyyyyyy

增加完成后可以拿掉/etc/ldap/slapd.conf内的rootpw设定。

Replication

这边先放设定:

# Replication
readonly on
syncrepl rid=100
        binddn="cn=syncuser,dc=example,dc=com"
        bindmethod=simple
        credentials="password"
        interval=00:00:01:00
        provider=ldaps://ldap-master.example.com:636
        retry="60 +"
        searchbase="dc=example,dc=com"
        tls_cacert=/etc/ssl/certs/ca-certificates.crt
        type=refreshAndPersist

这边ldaps的部分表示走SSL,可以用Let's Encrypt的凭证会比较容易处理(不需要另外开CA授权)。

常用指令

相关连结

参考资料

外部链接