ldap 常见问题和解决办法

适用人群: 掌握ldap基础同学

开始罗列常见的问题!

一,ldap安装部署问题

   目前最新的版本是2.4,之前的老版本 是2.3.

1.1  2.3 和2.4版本的安装问题 

           2.3 和2.4最大的区别在于配置文件:

            配置文件:旧方式,通过/etc/openldap/slapd.conf配置。 (2.3和2.4)

           非配置文件:配置存储在LDAP server中,可动态更新。在/etc/openldap/sladp.d目录中配置。(2.4)

           所以,在安装2.4版本的ldap时,在/etc/openldap/slapd.d下会生成下面的目录和文件

           drwxr-x--- 3 ldap ldap 4096 Nov 10 10:06 cn=config

           -rw------- 1 ldap ldap 1226 Nov 10 10:06 cn=config.ldif

1.2 配置不同带来的问题

        对于2.4版本,初次安装,大家都习惯将slapd.conf中的根域名修改为自己想要的。例如rootdn "cn=Manager,dc=test,dc=com"

        修改完后slaptest -f /etc/openldap/slapd.conf 先确定配置文件没有问题,然后启动ldap 服务,service ldap start 发现没有任何报错, 但是当你导入ldif文件的时候,会发现怎么也不成功,反而报错提示你,你的根是

       dc=my-domain,dc=com(默认的配置)

       **解决办法:

        (1)停掉ldap service ldap stop

        (2) 删除/etc/openldap/slapd.d/下所有文件,rm -rf /data/openldap/slapd.d/*

        (3)执行slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/,这个命令会根据你当前的配置生成一份新的到非配置文件中 

         (4) 然后重新导入ldif文件就成功了

二,ldif 文件导入问题

      首先,要说明下ldif 对内容的格式要求很严格

2.1 正确书写格式: 

(1空行)

dn:(空格) dc=mail,dc=kaspersky,dc=com(结尾无空格)

objectclass: (空格)dcObject(结尾无空格)

objectclass: (空格)organization(结尾无空格)

o: (空格)kaspersky(结尾无空格)

dc:(空格) test(结尾无空格)

 书写的时候,一定要注意空格问题,否则就回失败。

2.2 导入前初始化

 这了给大家提供一个模板,主要是初始化根 和管理员账号,因为ldap是树型的,所以下面的必须先生成

编写base.ldif

[root@localhost ldap]# catbase.ldif

dn: dc=test,dc=com

dc: pincer

objectClass: top

objectClass: dcObject

objectClass: organization

o: pincer

dn: cn=Manager,dc=test,dc=com

objectClass: organizationalRole

cn: Manager

ldapadd -x -D 'cn=Manager,dc=pincer,dc=com' -W -f base.ldif 

推荐一个工具,可以将本地passwd文件和group都生成ldif文件

2.3版本中,安装ldap后默认会有

[root@localhost openldap]# ll /usr/share/openldap/migration/|egrep 'migrate_passwd.pl|migrate_group.pl' 
-rwxr-xr-x 1 root root 2718 Feb 20 2010 migrate_group.pl

-rwxr-xr-x 1 root root 5639 Feb 20 2010 migrate_passwd.pl

2.4版本中,是没有此工具的,可以通过配置yum 安装  

 yum -y install migrationtools*

migrate_passwd.pl 可以将/etc/passwd 中的数据转化为ldif文件

migrate_group.pl  可以将/etc/group 中的数据转化为ldif文件

使用过程中,主要要先创建OU

三,ldap主从和主主同步

3.1 主主同步

     这里有一点要注意,主主同步,只能写一个master,另一个master不支持同时写入。

3.2 主从同步

    目前主从同步方式常见有2中,

      2.3版本同步方式是主动方式,由master将数据推到slave上

      2.4版本支持鏡像模式,是由slave端,主动去拉取master 的数据

     具体的区别,推荐大家看http://bbs.51cto.com/thread-1078277-1.html 去了解。

3.3 主从同步一般使用ldap 用户进行,下面贴出ldap master和slave的配置内容,这块比较简单

master配置文件:

overlay syncprov

syncprov-sessionlog 100

updatedn cn=Manager,dc=test,dc=com

slave配置

syncrepl rid=123

        provider=ldap://192.168.98.129:389

        type=refreshAndPersist

        #retry="5 5 300 5"

        interval=00:00:01:00

        searchbase="dc=test,dc=com"

        filter="(objectClass=*)"

        logbase="cn=accesslog"

        logfilter="(&(objectClass=auditWriteObject)(reqResult=0))"

        #scope=sub

        #attrs="cn,sn,ou,telephoneNumber,title,l"

        schemachecking=off

        bindmethod=simple

        binddn="uid=qq,ou=People,dc=test,dc=com"

        credentials=redhat

updateref ldap://192.168.223.131:389

常见问题一般是同步用户导致的,

在slave上可以通过上面uid=qq,ou=People,dc=test,dc=com 这个用户进行master查询,这个用户是需要在mater上进行添加的

 ldapsearch -x -b 'dc=test,dc=com' -D 'uid=qq,ou=People,dc=test,dc=com' -W -h 192.168.98.129  

后面会给出更详细万能的排错方式。


四,tls加密认证

    4.1 tls CA和证书

    这里推荐一个简单办法进行配置,首先选择一个机器作为CA认证中心

    首先清理掉默认的CA目录,然后重新生成

[root@localhost misc]# cd /etc/pki/ 
[root@localhost pki]# ls 
CA nssdb rpm-gpg server.crt server.csr server.key tls 
[root@localhost pki]# rm -rf CA

重新生成

[root@localhost ssl]# cd /etc/pki/tls/misc/

[root@localhost misc]# ./CA -newca 
CA certificate filename (or enter to create) 

Making CA certificate ... 
Generating a 1024 bit RSA private key 
.........++++++ 
................++++++ 
writing new private key to '../../CA/private/./cakey.pem' 
Enter PEM pass phrase: 
Verifying - Enter PEM pass phrase: 
----- 
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) [GB]: 
State or Province Name (full name) [Berkshire]: 
Locality Name (eg, city) [Newbury]: 
Organization Name (eg, company) [My Company Ltd]: 
Organizational Unit Name (eg, section) []: 
Common Name (eg, your name or your server's hostname) []:pincer  
Email Address []:root@pincer.com 

Please enter the following 'extra' attributes 
to be sent with your certificate request 
A challenge password []: 
An optional company name []:abc 
Using configuration from /etc/pki/tls/openssl.cnf 
Enter pass phrase for ../../CA/private/./cakey.pem: 
Check that the request matches the signature 
Signature ok 
Certificate Details: 
Serial Number: 0 (0x0) 
Validity 
Not Before: Feb 15 07:11:29 2015 GMT 
Not After : Feb 14 07:11:29 2018 GMT 
Subject: 
countryName = GB 
stateOrProvinceName = Berkshire 
organizationName = My Company Ltd 
commonName = pincer 
emailAddress = root@pincer.com 
X509v3 extensions: 
X509v3 Basic Constraints: 
CA:FALSE 
Netscape Comment: 
OpenSSL Generated Certificate 
X509v3 Subject Key Identifier: 
BA:B6:03:66:2F:8E:9A:AB:79:B2:82:24:CC:EA:15:8C:7D:69:77:2B 
X509v3 Authority Key Identifier: 
keyid:BA:B6:03:66:2F:8E:9A:AB:79:B2:82:24:CC:EA:15:8C:7D:69:77:2B 

Certificate is to be certified until Feb 14 07:11:29 2018 GMT (1095 days) 

Write out database with 1 new entries 
Data Base Updated 

下面为每个ldap server生成一个签名证书

[root@localhost misc]# openssl req -newkey rsa:1024 -nodes -keyout slapd-key.pem -out slapd-req.pem 
Generating a 1024 bit RSA private key 
...++++++ 
...++++++ 
writing new private key to 'slapd-key.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) [GB]: 
State or Province Name (full name) [Berkshire]: 
Locality Name (eg, city) [Newbury]: 
Organization Name (eg, company) [My Company Ltd]: 
Organizational Unit Name (eg, section) []: 
Common Name (eg, your name or your server's hostname) []:pincer 
Email Address []:root@pincer.com 

Please enter the following 'extra' attributes 
to be sent with your certificate request 
A challenge password []: 
An optional company name []:abc 
[root@localhost misc]# 
[root@localhost misc]# 
[root@localhost misc]# ls 
CA CA.pl c_hash c_info c_issuer c_name slapd-key.pem slapd-req.pem 


[root@localhost misc]# openssl ca -out slapd-cert.pem -infiles slapd-req.pem 
Using configuration from /etc/pki/tls/openssl.cnf 
Enter pass phrase for ../../CA/private/cakey.pem: 
Check that the request matches the signature 
Signature ok 
Certificate Details: 
Serial Number: 1 (0x1) 
Validity 
Not Before: Feb 15 07:13:21 2015 GMT 
Not After : Feb 15 07:13:21 2016 GMT 
Subject: 
countryName = GB 
stateOrProvinceName = Berkshire 
organizationName = My Company Ltd 
commonName = pincer  ###这里要注意,这里的写主机名,则连接的时候,ldapsearch -h也要写主机名,所以,为了线上使用,需要填写ldap server的ip地址
emailAddress = root@pincer.com 
X509v3 extensions: 
X509v3 Basic Constraints: 
CA:FALSE 
Netscape Comment: 
OpenSSL Generated Certificate 
X509v3 Subject Key Identifier: 
DC:50:25:5D:64:DB:DC:50:5C:F5:30:03:76:6A:9F:38:EE:A3:38:C5 
X509v3 Authority Key Identifier: 
keyid:BA:B6:03:66:2F:8E:9A:AB:79:B2:82:24:CC:EA:15:8C:7D:69:77:2B 

Certificate is to be certified until Feb 15 07:13:21 2016 GMT (365 days) 
Sign the certificate? [y/n]:y 


1 out of 1 certificate requests certified, commit? [y/n]y 
Write out database with 1 new entries 
Data Base Updated 

从上面注释可以看出,填写commonName 要注意,如果希望客户端通过ip访问,这里就要写当前服务器的ip地址。这里非常关键。


3,下面将证书复制到ldap 目录中使用

修改ldap配置文件

[root@localhost pki]# vim /etc/openldap/slapd.conf

TLSCACertificateFile /etc/openldap/ssl/cacert.pem

TLSCertificateFile /etc/openldap/ssl/slapd-cert.pem

TLSCertificateKeyFile /etc/openldap/ssl/slapd-key.pem

将证书复制到/etc/openldap/ssl/ 中,修改slapd-key.pem 文件权限为600

[root@localhost pki]# cp CA/cacert.pem tls/misc/slapd-* /etc/openldap/ssl/

[root@localhost pki]# chmod 600 /etc/openldap/ssl/slapd-key.pem

重启ldap 会监听一个636 端口,即为加密认证

4,修改客户端配置认证

注意: 如果有其他客户端,则将/etc/openldap/ssl下的目录全部复制到ldap client上,然后修改/etc/openlda/ldap.conf 就好了

[root@localhost pki]# vim /etc/openldap/ldap.conf

URI ldaps://pincer/           (1)这里的pincer要跟生成证书的comm写的一样, 我们这里写的是pincer,所以这里也要写pincer

BASE dc=my-domain,dc=com

#TLS_CACERTDIR /etc/openldap/cacerts

TLS_CACERTDIR /etc/openldap/ssl

TLS_CACERT /etc/openldap/ssl/cacert.pem

TLS_REQCERT hard

上面的必须要加

配置/etc/ldap.conf

  (2)#下面的顺序一定要这样

ssl start_tls

ssl on

上面的(1)和(2) 不对,  ldapsearch 不受影响,但是getent 和系统验证登录就会失败(卡主)



日志和debug排错

ldap 的日志比较生硬,看不出很多问题,但是有日志总比没有强

要注意的是:日志级别是累加的:296 = 256 日志连接/操作/结果 + 32 搜索过滤器处理 + 8 连接管理: 文件slapd.config中添加: loglevel 296 ,默认记录在/var/log/message中,

将日志记录到slapd.log中,把下面的内容添加到 /etc/syslog.conf 中: local4.debug /var/log/slapd.log 重启syslog服务 service syslog restart 在/var/log/slapd.log中即可查看ldap的日志信息

,下面列下ldap常用的日志级别

-1记录所有的信息

0不记录debug

1跟踪功能调用的信息

2包处理的debug信息

4丰富的debug信息

8连接管理信息

16包的收发信息

32搜索过滤的处理过程

64配置文件的处理过程

128访问控制列表处理过程

256连接、操作及其结果的统计数据

512向客户端返回的结果的统计信息

1024与shell后端的通信信息

2048显示记录条目的分析信息

4096数据库缓存处理信息

8192数据库索引

16384从服务器数据同步资源消耗处理信息

说下我最常用的日志级别吧, 我一般使用的是269,64 这2个,主要用于ldap启动排错 和ldap主从同步排错。

为了更方便,我们可以在启动ldap的时候,使用slapd 来开启服务,同时加上日志级别,就能方面的debug出问题,

slapd -d 269

如果看不出什么很详细的信息,也可以使用strace 进行进程更新,查看问题出在什么地方