LDAP是一款轻量级目录访问协议(Lightweight Directory Access Protocol,简称LDAP),属于开源集中账号管理架构的实现,且支持众多系统版本,被广大互联网公司所采用。关于openLDAP的更多信息请查看openLDAP官网

安装

安装环境

CentOS 7.5 64bit、 openLDAP 2.4.44

安装

同步时间、关闭SELINUX、关闭防火墙

ntpdate -u ntp.api.bz
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
setenforce 0
systemctl disable firewalld.service
systemctl stop firewalld.service

安装openLDAP

yum install openldap compat-openldap openldap-clients openldap-servers \
    openldap-servers-sql openldap-devel migrationtools

基础配置

从OpenLDAP2.4.23版本开始所有配置数据都保存在/etc/openldap/slapd.d/中,建议不再使用slapd.conf作为配置文件。

设置管理员密码

slappasswd -s <your_passwd>

提醒:将生成的加密密码进行复制,后面的配置中会使用。

修改文件olcDatabase={2}hdb.ldif

文件路径:/etc/openldap/slapd.d/cn\=config/olcDatabase\=\{2\}hdb.ldif 主要修改以下几个参数:
数据存储路径:olcDbDirectory: /var/lib/ldap
域名:olcSuffix: dc=domain,dc=com
超级账号:olcRootDN: cn=Manager,dc=domain,dc=com
上一步生成的密码:olcRootPW: {SSHA}fNJOnWST5pVtJ99hGDVVm11pLqgQxlOa

dc按照自己实际情况填写!

dn: olcDatabase={2}hdb
objectClass: olcDatabaseConfig
objectClass: olcHdbConfig
olcDatabase: {2}hdb
olcDbDirectory: /var/lib/ldap
olcSuffix: dc=domain,dc=com
olcRootDN: cn=Manager,dc=domain,dc=com
olcDbIndex: objectClass eq,pres
olcDbIndex: ou,cn,mail,surname,givenname eq,pres,sub
structuralObjectClass: olcHdbConfig
entryUUID: dd1cf184-f92b-1038-9db2-e1f25a7ff2d3
creatorsName: cn=config
createTimestamp: 20190422092215Z
entryCSN: 20190422092215.936824Z#000000#000#000000
modifiersName: cn=config
modifyTimestamp: 20190422092215Z
olcRootPW: {SSHA}fNJOnWST5pVtJ99hGDVVm11pLqgQxlOa

修改文件olcDatabase={1}monitor.ldif

文件路径:/etc/openldap/slapd.d/cn\=config/olcDatabase\=\{1\}monitor.ldif 修改参数olcAccess,注意修改dc为前面自己写的值。
olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=extern al,cn=auth" read by dn.base="cn=Manager,dc=domain,dc=com" read by * none

dn: olcDatabase={1}monitor
objectClass: olcDatabaseConfig
olcDatabase: {1}monitor
olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=extern
 al,cn=auth" read by dn.base="cn=Manager,dc=domain,dc=com" read by * none
structuralObjectClass: olcDatabaseConfig
entryUUID: dd1cee1e-f92b-1038-9db1-e1f25a7ff2d3
creatorsName: cn=config
createTimestamp: 20190422092215Z
entryCSN: 20190422092215.936737Z#000000#000#000000
modifiersName: cn=config
modifyTimestamp: 20190422092215Z

配置数据库

/var/lib/ldap是默认路径,填写时以自己实际路径为准。

cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG

将配置的数据存放路径权限给ldap

chown -R ldap:ldap /var/lib/ldap/
chmod 700 -R /var/lib/ldap

验证配置

执行命令

slaptest -u

最后一行为config file testing successded即可。

启动slapd并设置为开机自启

systemctl enable slapd
systemctl start slapd

查看slapd状态

systemctl status slapd

导入Schema

ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/collective.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/corba.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/duaconf.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/dyngroup.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/java.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/misc.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/openldap.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/pmi.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/ppolicy.ldif

修改文件migrate_common.ph

文件路径:/usr/share/migrationtools/migrate_common.ph 修改第71、74和90行。

注意填写自己域名

# Default DNS domain
$DEFAULT_MAIL_DOMAIN = "domain.com";
# Default base 
$DEFAULT_BASE = "dc=domain,dc=com";
$EXTENDED_SCHEMA = 1;

生成base.ldif

在目录/usr/share/migrationtools/下执行命令

./migrate_base.pl > base.ldif

添加base.ldif到ldap

在目录/usr/share/migrationtools/下执行命令

ldapadd -x -D "cn=Manager,dc=domain,dc=com" -W -f ./base.ldif

注意填写自己的dc
这里会要求输入密码,输入的密码为第一步中的明文密码。

检查是否添加成功

必须检查确认Manager数据添加成功,才能通过phpldapAdmin登录。
执行命令下面的命令,输入密码同样为明文密码。

ldapsearch -x -D "cn=Manager,dc=domain,dc=com" -b \
    "ou=Aliases,dc=domain,dc=com" -W

Web管理PhpLdapAdmin

安装httpd和PhpLdapAdmin

rpm -ivh http://mirrors.ukfast.co.uk/sites/dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
yum install httpd phpldapadmin

修改文件phpldapadmin.conf

文件路径:/etc/httpd/conf.d/phpldapadmin.conf 注意:注释掉第一个IfModule,否则会禁止访问。

<Directory /usr/share/phpldapadmin/htdocs>
 # <IfModule mod_authz_core.c>
    # Apache 2.4
    # Require local
 # </IfModule>
  <IfModule !mod_authz_core.c>
    # Apache 2.2
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
    Allow from ::1
  </IfModule>
</Directory>

修改文件config.php

文件路径:/etc/phpldapadmin/config.php 修改属性$servers->setValue的uid为dn(文件第397行)

$servers->setValue('login','attr','dn');
// $servers->setValue('login','attr','uid');

修改文件http.conf

文件路径:/etc/httpd/conf/httpd.conf
将原来的Directory属性注释,修改为如下内容

#<Directory />
#    AllowOverride none
#    Require all denied
#</Directory>
<Directory />
     Options Indexes FollowSymLinks
     AllowOverride none
</Directory>

启动httpd并设置为开机自启

systemctl enable httpd
systemctl start httpd

查看httpd运行状态

systemctl status httpd

访问phpLDAPadmin

在浏览器中输入http://<IP> /phpldapadmin 输入账号填写cn=Manager,dc=domain,dc=com

自助修改密码服务

添加repo

在路径/etc/yum.repos.d下添加文件LTB-Project.repo 文件内容如下

[ltb-project-noarch]
name=LTB project packages (noarch)
baseurl=https://ltb-project.org/rpm/$releasever/noarch
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-LTB-project

安装SSP

yum install self-service-password

修改文件config.inc.php

文件路径:/usr/share/self-service-password/conf/

# LDAP
$ldap_url = "ldap://<IP>:389";
$ldap_starttls = false;
$ldap_binddn = "cn=manager,dc=domain,dc=com";
$ldap_bindpw = "your_passwd";
$ldap_base = "dc=domain,dc=com";
$ldap_login_attribute = "uid";
$ldap_fullname_attribute = "cn";
$ldap_filter = "(&(objectClass=person)($ldap_login_attribute={login}))";
## Mail
# LDAP mail attribute
$mail_attribute = "mail";
# Get mail address directly from LDAP (only first mail entry)
# and hide mail input field
# default = false
$mail_address_use_ldap = false;
# Who the email should come from
$mail_from = "example@example.com";
$mail_from_name = "Self Service Password";
$mail_signature = "";
# Notify users anytime their password is changed
$notify_on_change = true;
# PHPMailer configuration (see https://github.com/PHPMailer/PHPMailer)
$mail_sendmailpath = '/usr/sbin/sendmail';
$mail_protocol = 'smtp';
$mail_smtp_debug = 0;
$mail_debug_format = 'html';
$mail_smtp_host = 'smtphm.qiye.163.com';
$mail_smtp_auth = true;
$mail_smtp_user = 'example@example.com';
$mail_smtp_pass = 'your_passwd';
$mail_smtp_port = 25;
$mail_smtp_timeout = 30;
$mail_smtp_keepalive = false;
$mail_smtp_secure = 'tls';
$mail_smtp_autotls = true;
$mail_contenttype = 'text/plain';
$mail_wordwrap = 0;
$mail_charset = 'utf-8';
$mail_priority = 3;
$mail_newline = PHP_EOL;
$hash = "SSHA";
$keyphrase = "domain";
$who_change_password = "manager";

3.修改文件self-service-password.conf

文件路径:/etc/httpd/conf.d/self-service-password.conf

<VirtualHost *>
    DocumentRoot /usr/share/self-service-password
    DirectoryIndex index.php
    AddDefaultCharset UTF-8
    Alias /ssp /usr/share/self-service-password
<Directory “/usr/share/self-service-password”>
    AllowOverride None
    Require all granted
</Directory>
    LogLevel warn
    ErrorLog /var/log/httpd/ssp_error_log
    CustomLog /var/log/httpd/ssp_access_log combined
</VirtualHost>

4.登录SSP

在访问之前重启httpd服务

systemctl restart httpd.service

在浏览器输入网址http://<IP>/ssp/index.php

日志功能

创建loglevel.ldif

/etc/openldap下新建文件loglevel.ldif

dn: cn=config
changetype: modify
replace: olcLogLevel
olcLogLevel: stats

导入日志文件

ldapmodify -Y EXTERNAL -H ldapi:/// -f /etc/openldap/loglevel.ldif

修改rsyslog.conf

文件路径:/etc/rsyslog.conf 在文件末尾加入以下内容

local4.* /var/log/slapd/slapd.log

创建日志文件目录并授权

mkdir /var/log/slapd
chmod 755 /var/log/slapd
chown -R ldap:ldap /var/log/slapd/

重启服务

systemctl restart rsyslog.service
systemctl restart slapd.service

FAQ

提示警告信息

Automatically removed objectClass from template Samba: Group Mapping: sambaGroupMapping removed from template as it is not defined in the schema 修改文件/etc/phpldapadmin/config.php第166行

$config->custom->appearance['hide_template_warning'] = true;

创建通用用户Generic: User Account时添加email属性

修改文件posixAccount.xml 文件路径:/usr/share/phpldapadmin/templates/creation 在文件中添加以下属性

<attribute id="mail">
        <display>Email</display>
        <order>10</order>
        <page>1</page>
</attribute>

附录

关键字

英文全称

含义

dc

Domain Component

域名的部分,其格式是将完整的域名分成几部分,如域名为example.com变成dc=example,dc=com

uid

User Id

用户ID,唯一值

ou

Organization Unit

组织单位,类似于Linux文件系统中的子目录,它是一个容器对象,组织单位可以包含其他各种对象(包括其他组织单元)

cn

Common Name

公共名称

sn

Surname


dn

Distinguished Name

惟一辨别名,类似于Linux文件系统中的绝对路径,每个对象都有一个惟一的名称,在一个目录树中DN总是惟一的

rdn

Relative dn

相对辨别名,类似于文件系统中的相对路径,它是与目录树结构无关的部分

c

Country

国家

o

Organization

组织名

参考文献

1、烂泥行天下 2、openLDAP初识