ldap


ldap目录服务介绍

  目录是一类为了浏览和搜索数据而设计的特殊的数据库。例如:为人所熟知的微软公司的活动目录(active directory)就是目录数据库的一种。目录服务是按照树状形式存储信息的,目录包含基于熟悉的描述性信息,并且支持高级的过滤功能

  一般来说,目录不支持大多数事务性数据库所支持的高吞吐量和复杂的更新操作。目录进行更新操作,可以说是要么全部,要么都不的院子操作。目录服务适合的业务应用在于提供大量的查询和搜索操作,而不是大量的写入操作

  LDAP是轻量目录访问协议(Lightweight Directory AccessProtocol)的所写

  LDAP标准实际上实在X.500标准基础上产生的一个简化版本

 

LDAP中的常用名词缩写及含义

dc  Domain Component   #域名的部分,其格式是将万丈的域名分成几部分,如域名为example.com编程dc=example,dc=com
uid User ID   #用户id,如Daniel
ou  Organization Unit  #组织单位,类似于Linux文件系统中的子目录,它是一个容器对象,组织单位可以包含其他各种对象(包括其他组织单元)
cn   Common Name  #公共名称,姓名
sn   Surname    #姓
dn   Distinguished   #唯一辨别名,类似于linux的绝对路径,每个对象都有一个唯一的名称,如“uid=tom,ou=market,dc=example,dc=com”,在一个目录树种DN总是唯一的
rdn  Relative dn   #相对辨别名,类似于文件系统中的相对路径,它是与目录树结构无关的部分
c     Country   #国家
o     Organization  #组织名

 

LDAP目录服务的特点

#
LDAP是一个跨平台的、标准的协议,近几年来得到了业界广泛的认可
LDAP的结构用属性结构来表示,而不是用表格,因此不用SQL语句维护了
LDAP提供了静态数据的快速查询方式,但在写数据方面并不擅长
LDAP服务可以使用基于“推”或“拉”的复制信息技术,用简单的或基于安全证书的安全认证,复制部分或全部数据,既保证数据的安全性,又提高了数据的访问效率
LDAP是一个安全的协议,LDAPv3支持SALSL、SSL、TLS,使用认证来确保事务的安全,另外,LDAP提供了不通层次的访问控制,可以限制不通用户的访问权限
LDAP支持一类数据存储,LDAP存储的数据可以是文本资料、二进制图片等
#。。。。

 

安装

#环境centos6.9
IP:172.16.50.121 域名:etiantian.org
#依赖 yum groupinstall -y "Compatibility libraries" yum -y install openldap openldap-* yum -y install nscd nss-pam-ldapd nss-* pcre pcre-* rpm -qa openldap openldap-2.4.40-16.el6.x86_64

  

  

配置ldap#配置ldap master

cd  /etc/openldap
cp /usr/share/openldap-servers/slapd.conf.obsolete slapd.conf  #复制配置文件到/etc/openldap

#生成管理员密码
[root@bogon openldap]# slappasswd -s admin123
{SSHA}1hcNpGAkBvNFPmE0IKZBqJgsb7Lfz98O
#admin123就是密码了,管理员是admin
slappasswd -s admin123 | sed -e "s#{SSHA}#rootpw\t{SSHA}#g" >> slapd.conf
[root@bogon openldap]# tail -1 slapd.conf 
rootpw	{SSHA}sC4BN/0B8OEPJeTcVt442LT1oPPiNZde #这个是生成密码并且写进配置文件

#修改配置文件
cp slapd.conf slapd.conf.bak #备份
egrep -v "^#|^$" slapd.conf > a.conf
mv a.conf slapd.conf

vi slapd.conf

database        bdb  #指定使用的数据库,bdb,Berkeley DB(BDB)
suffix          "dc=etiantian,dc=org"  #指定要搜索的后缀,etiantian,org,www.etiantian.org的话就在前面再加一个dc=www
#checkpoint     1024 15
rootdn          "cn=admin,dc=etiantian,dc=org"  #指定管理员dn路径,使用这个dn可以登录OPENLDAP服务器
#保存退出

[root@bogon openldap]# cat >>slapd.conf <<EOF
> #add start by Daniel 2018/01/08
> loglevel 296      #设置日志级别,记录日志信息方便调试。296级别是有256(日志连接/操作/结果)、32(搜索过滤器处理)、8(连接管理累加的结果)
> cachesize 1000     #设置ldap可以缓存的记录数
> checkpoint 2048 10  #把内存中的数据写会数据文件的操作,上面设置表示每达到2048KB或者10分钟执行一次checkpoint,即写入数据文件的操作
> #add end by Daniel 2018/01/08
> EOF

#下面这一段都删掉或者注释掉
database config
access to *
        by dn.exact="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" manage
        by * none
database monitor
access to *
        by dn.exact="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read
        by dn.exact="cn=Manager,dc=my-domain,dc=com" read
        by * none

#加上这段
access to * 
        by self write
        by anonymous auth
        by * read

#配置解析
echo "172.16.50.121 etiantian.org" >> /etc/hosts

#配置syslog,记录ldap服务日志
cp /etc/rsyslog.conf /etc/rsyslog.conf.bak.$(date +%F%T) #备份
echo "#record ldap.log by Daniel 2018/01/08" >> /etc/rsyslog.conf
echo 'local4.*   /var/log/ldap.log' >> /etc/rsyslog.conf
service rsyslog restart     #重启服务

  

 

实战配置LDAP数据库并授权

#查看数据库路径
[root@bogon openldap]# grep  directory slapd.conf
# Do not enable referrals until AFTER you have a working directory
# The database directory MUST exist prior to running slapd AND 
directory	/var/lib/ldap  #<<这就是路径

cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/ #拷贝模板到数据库路径下

chown ldap:ldap /var/lib/ldap/
chmod 700 /var/lib/ldap/            #授权

[root@bogon openldap]# slaptest -u
config file testing succeeded    #表示配置已成功,你就成功了

  

启动LDAP服务

[root@bogon openldap]# /etc/init.d/slapd restart
停止 slapd:                                               [失败]
正在启动 slapd:                                           [确定]

#启动成功
chkconfig slapd on #开机自启动 #然后查看ldap的数据库 ldapsearch -LLL -W -x -H ldap://etiantian.org -D "cn=admin,dc=etiantian,dc=org" -b "dc=etiantian,dc=org" "(uid=*)" #输入密码后会报错,这是因为我们用的2.3的配置文件,然后我们要生成2.4的 rm -rf /etc/openldap/slapd.d/* slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/ #然后查看slapd.d下 chown -R ldap.ldap /etc/openldap/slapd.d/ #给权限 #然后启动ldap ldapsearch -LLL -W -x -H ldap://etiantian.org -D "cn=admin,dc=etiantian,dc=org" -b "dc=etiantian,dc=org" "(uid=*)" #执行出现No such object (32),正确

  

 导入和备份ldap数据

#通过已有文件导入

vi base.ldif
dn: dc=etiantian,dc=org
objectClass: organization
objectClass: dcObject
dc: etiantian
o: etiantian

dn: ou=People, dc=etiantian, dc=org
objectClass: organizationalUnit
ou: People

dn: ou=group,dc=etiantiant,dc=org
objectClass: organizationalUnit
ou: group

dn: cn=tech,ou=group,dc=etiantian,dc=org
objectClass: posixGroup
description:: 5oqA5pyv6YOo
gidNumber: 10001
cn: tech

vi jack.ldif
dn: uid=jack,ou=People,dc=etiantian,dc=org
objectClass: posixaccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
homeDirectory: /home/jack
loginShell: /bin/bash
uid: jack
cn: jack
userPassword:: 55G/ReqPKeOZ8SpgszwIQhaBXySNU4mw
uidNumber: 10005
gidNumber: 10001
sn: jack

ldapadd -x -H ldap://etiantian.org -D "cn=admin,dc=etiantian,dc=org" -W -f xxx.ldif   #-H 地址 ,-D dn,admin的dn
 
#查询
ldapsearch -LLL -w admin123 -x -H ldap://etiantian.org -D "cn=admin,dc=etiantian,dc=org" -b "dc=etiantian,dc=org" #-w ,不交互,跟mysql -uroot -p passwd是一样的,其他参数可以--help自己去看
dn: dc=etiantian,dc=org
objectClass: organization
objectClass: dcObject
dc: etiantian
o: etiantian

dn: ou=People,dc=etiantian,dc=org
objectClass: organizationalUnit
ou: People

dn: uid=jack,ou=People,dc=etiantian,dc=org
objectClass: posixAccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
homeDirectory: /home/jack
loginShell: /bin/bash
uid: jack
cn: jack
userPassword:: 55G/ReqPKeOZ8SpgszwIQhaBXySNU4mw
uidNumber: 10005
gidNumber: 10001
sn: jack
#有了刚才添加的东西
 
#备份
ldapsearch -LLL -w admin123 -x -H ldap://etiantian.org -D "cn=admin,dc=etiantian,dc=org" -b "dc=etiantian,dc=org" >>/bak.ldap.ldif #很简单,直接导入就好了

  

安装及配置ldap管理ldap服务器

#为ldap master配置web管理接口
    ldap的客户端管理接口有很多,有b/s结构的,有web的,也有c/s结构的。我们以b/s结构的ldap-account-manager-3.7.tar.gz软件为例讲解

#依赖安装
yum -y install php httpd php-ldap php-gd
[root@bogon ldap]# rpm -qa httpd php php-ldap php-gd
httpd-2.2.15-60.el6.centos.6.x86_64
php-gd-5.3.3-49.el6.x86_64
php-ldap-5.3.3-49.el6.x86_64
php-5.3.3-49.el6.x86_64

#安装
cd /root/
wget https://jaist.dl.sourceforge.net/project/lam/LAM/3.7/ldap-account-manager-3.7.tar.gz
tar zxf ldap-account-manager-3.7.tar.gz
cp -R /root/ldap-account-manager-3.7 /var/www/html/ldap
cd /var/www/html/ldap/config
cp lam.conf_sample lam.conf_sample.bak   #备份
cp config.cfg_sample config.cfg  #备份
mv lam.conf_sample  lam.conf  #改名

sed -i 's#cn=Manager#cn=admin#g' lam.conf
sed -i 's#dc=my-domain#dc=etiantian#g' lam.conf
sed -i 's#dc=com#dc=org#g' lam.conf  #修改
diff config/lam.conf_sample config/lam.conf  #比较

chown -R apache.apache /var/www/html/ldap/ #授权
http://172.16.50.121/ldap  #浏览器访问

  

 

 

通过ldap web管理ldap服务器

 

#输入密码,默认lam

#我们前面生成的密码,admin123

#进去后点建立

#然后就有刚才建立的用户了

#添加组

 

 

 

 

 

 #添加用户

 

 

#保存,添加成功

#服务器查询一下

[root@etiantian ~]# ldapsearch -LLL -w admin123 -x -H ldap://etiantian.org -D "cn=admin,dc=etiantian,dc=org" -b "dc=etiantian,dc=org" "(uid=ddaniel)"
dn: uid=ddaniel,ou=People,dc=etiantian,dc=org
objectClass: posixAccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
homeDirectory: /home/ddaniel
loginShell: /bin/bash
uid: ddaniel
cn: Daniel Daniel
uidNumber: 10006
gidNumber: 10000
userPassword:: e1NTSEF9YTJQN0RSZ2hyVDdFcVJpeENqVlMyYlVpMEs4aDFYbjk=
sn: Daniel
givenName: Daniel

  

生成初始导入的数据及备份LDAP数据

#先导出数据
ldapsearch  -LLL -w admin123 -x -H ldap://172.16.50.121 -D "cn=admin,dc=etiantian,dc=org" -b "dc=etiantian,dc=org" > ldap.ldif
#然后可以分类,分成目录树,和用户和组信息,导入的时候也是,跟上面的jack一样

  

启用SVN服务器的SASL验证机制

#SVN原来有个本地验证,不懂得看我前面写的SVN,现在我们不用SVN本地验证,用ldap统一验证
#这个东西叫sasl验证机制
    SASL全称Simple Authentication and Security Laye,是一种用来扩充C/S模式验证能力的二级制
    SASL是一个胶合库,通过这个库把应用层与形式多样的认证系统整合在一起。这有点类似于PAM,但是后者是认证方式,决定什么人可以访问服务,二SASL是认证过程,侧重于信任建立过程,这个过程可以调用PAM来建立信任关系。在这里Memcached就是上面提到的应用层,具体的认证交给SASL库,SASL会根据相应的认证机制来完成验证功能

#安装sasl
yum install -y *sasl*
[root@etiantian /]# saslauthd -v
saslauthd 2.1.23
authentication mechanisms: getpwent kerberos5 pam rimap shadow ldap

#修改
[root@etiantian /]# grep -i mech /etc/sysconfig/saslauthd  #查看mech这行
# Mechanism to use when checking passwords.  Run "saslauthd -v" to get a list
# of which mechanism your installation was compiled with the ablity to use.
MECH=pam
# Options sent to the saslauthd. If the MECH is other than "pam" uncomment the next line.
[root@etiantian /]# sed -i 's#MECH=pam#MECH=shadow#g' /etc/sysconfig/saslauthd  #修改
[root@etiantian /]# grep -i mech /etc/sysconfig/saslauthd  #再次查看
# Mechanism to use when checking passwords.  Run "saslauthd -v" to get a list
# of which mechanism your installation was compiled with the ablity to use.
MECH=shadow
# Options sent to the saslauthd. If the MECH is other than "pam" uncomment the next line.

#启动
[root@etiantian /]# /etc/init.d/saslauthd restart
停止 saslauthd:                                           [失败]
正在启动 saslauthd:                                       [确定]
#创建一个新用户并设置密码
useradd Daniel
passwd Daniel
#验证
testsaslauthd -uDaniel -p123456 #返回OK,表示成功
#使用ldap验证
sed -i 's#MECH=shadow#MECH=ldap#g' /etc/sysconfig/saslauthd 
/etc/init.d/saslauthd restart  #重启一下
#使用ldap验证都是失败,因为还没有连接ldap服务器地址
#解决
vim vi /etc/saslauthd.conf

ldap_servers: ldap://etiantian.org/
ldap_bind_dn: cn=admin,dc=etiantian,dc=org
ldap_bind_pw: admin123
ldap_search_base: ou=People,dc=etiantian,dc=org
ldap_filter: uid=%U
ldap_password_attr: userPassword  #保存
/etc/init.d/saslauthd restart #重启服务
testsaslauthd -uDaniel -p123456 #使用ldap用户测试,成功!

#安装svn,照着我前面做
#安装后
vi /etc/sasl2/svn.conf
wcheck_method: saslauthd
mech_list: PLAIN LOGIN

#配置svn的支持sasl
sed -i 's@# use-sasl = true@use-sasl = true@g'  >svnserve.conf
vi svnserve.conf
#password-db = /application/svnpasswd/passwd
#authz-db = /application/svnpasswd/authz  #这两注释掉
#或者
vi /application/svnpasswd/authz  #修改这个文件,以后ldap增加用户,直接管理这个文件就行了
ldap_user = Daniel,faker
[sadoc:/]
daniel = rw
@ldap_user = rw

pkill svnserve
svnserve -d -r /application/svndata/

svn checkout svn://etiantian.org/sadoc /svn --username=Daniel --password=123456   #使用ldap用户认证

  

 


 

转载于:https://www.cnblogs.com/wazy/p/8243926.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值