ldap+ldap-account-manager+svn+apache+php做账号管理

Centos6配置ldap

设备基本信息:

1、系统基本信息

cat /etc/redhat-release

CentOS release 6.6 (Final)

 

uname -a

Linux master 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

 

2、软件版本基本信息,软件都是yum安装的

 

扫描二维码关注公众号,回复: 1948897 查看本文章

一、基础配置

 

1、关闭防火墙

iptables -F

/etc/init.d/iptables save

/etc/init.d/iptables stop

chkconfig --del iptables

 

2、关闭selinux

sed -i 's/SELINUX=enabled/SELINUX=disabled/g' /etc/selinux/config

setenforce 0

查看selinux

getenforce

 

3、配置域名解析hosts
echo "192.168.18.184 hxy.com" >> /etc/hosts

 

4、安装ldap

yum -y install openldap openldap-servers openldap-clients openldap-devel compat-openldap -y

 

5、查看安装完成后的结果:

[root@bogon ~]# tail -1 /etc/passwd

ldap:x:55:55:OpenLDAP server:/var/lib/ldap:/sbin/nologin

 

6、还可以通过rpm -qa|grep openldap看到安装了5个软件包

rpm -qa|grep openlda

openldap-servers-2.4.40-16.el6.x86_64

openldap-2.4.40-16.el6.x86_64

openldap-clients-2.4.40-16.el6.x86_64

compat-openldap-2.3.43-2.el6.x86_64

openldap-devel-2.4.40-16.el6.x86_64

 

 

7、复制配置文件

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

cp /usr/share/openldap-servers/slapd.conf.obsolete /etc/openldap/slapd.conf

 

8、生成密码:(密码你自己设定hxy123)这条命令是将输出结果输入到slapd.conf文件尾部

注:-s后面接的是你的密码

slappasswd -s hxy123|sed -e "s#{SSHA}#rootpw\t{SSHA}#g">>/etc/openldap/slapd.conf

 

tail -1 /etc/openldap/slapd.conf

rootpw   {SSHA}8oL44HqnBOxEZyKGJr1Som2WV7apHHG0

 

9、查看slapd.conf配置文件:

grep -Ev "^$|^#" /etc/openldap/slapd.conf > /etc/openldap/slapd.conf_bak

\cp /etc/openldap/slapd.conf_bak /etc/openldap/slapd.conf

cat /etc/openldap/slapd.conf

 

include         /etc/openldap/schema/corba.schema

include         /etc/openldap/schema/core.schema

include         /etc/openldap/schema/cosine.schema

include         /etc/openldap/schema/duaconf.schema

include         /etc/openldap/schema/dyngroup.schema

include         /etc/openldap/schema/inetorgperson.schema

include         /etc/openldap/schema/java.schema

include         /etc/openldap/schema/misc.schema

include         /etc/openldap/schema/nis.schema

include         /etc/openldap/schema/openldap.schema

include         /etc/openldap/schema/ppolicy.schema

include         /etc/openldap/schema/collective.schema

allow bind_v2

pidfile         /var/run/openldap/slapd.pid

argsfile        /var/run/openldap/slapd.args

TLSCACertificatePath /etc/openldap/certs

TLSCertificateFile "\"OpenLDAP Server\""

TLSCertificateKeyFile /etc/openldap/certs/password

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

database        bdb

suffix          "dc=my-domain,dc=com"

checkpoint      1024 15

rootdn          "cn=Manager,dc=my-domain,dc=com"

directory       /var/lib/ldap

index objectClass                       eq,pres

index ou,cn,mail,surname,givenname      eq,pres,sub

index uidNumber,gidNumber,loginShell    eq,pres

index uid,memberUid                     eq,pres,sub

index nisMapName,nisMapEntry            eq,pres,sub

rootpw   {SSHA}8oL44HqnBOxEZyKGJr1Som2WV7apHHG0

 

10、修改配置文件

sed -i 's@cn=Manager,dc=my-domain,dc=com@cn=Forand,dc=hxy,dc=com@g' /etc/openldap/slapd.conf

sed -i 's@dc=my-domain,dc=com@dc=hxy,dc=com@' /etc/openldap/slapd.conf

echo -e 'loglevel 296\ncachesize 1000'>>/etc/openldap/slapd.conf

 

vim /etc/openldap/slapd.conf

        by dn.exact="cn=Forand,dc=hxy,dc=com" read

suffix          "dc=hxy,dc=com"

rootdn          "cn=Forand,dc=hxy,dc=com"

rootpw   {SSHA}8oL44HqnBOxEZyKGJr1Som2WV7apHHG0

loglevel 296
cachesize 1000

image.png

这是和源文件不同的几个地方

 

11、修改完成后测试一下配置文件是否有问题

slaptest -f /etc/openldap/slapd.conf

我这里报错了!:

[root@hxy openldap]# slaptest -f /etc/openldap/slapd.conf -v

5b0d6f3d bdb_db_open: DB_CONFIG for suffix "dc=hxy,dc=com" has changed.

5b0d6f3d Performing database recovery to activate new settings.

5b0d6f3d bdb_db_open: database "dc=hxy,dc=com": recovery skipped in read-only mode. Run manual recovery if errors are encountered.

5b0d6f3d bdb_db_open: database "dc=hxy,dc=com": db_open(/var/lib/ldap/id2entry.bdb) failed: No such file or directory (2).

5b0d6f3d backend_startup_one (type=bdb, suffix="dc=hxy,dc=com"): bi_db_open failed! (2)

slap_startup failed (test would succeed using the -u switch)

 image.png

 

在网上查了一下,

db_open(/var/lib/ldap/id2entry.bdb) failed: No such file or directory (2

backend_startup_one (type=bdb, suffix="dc=hxy,dc=com"): bi_db_open failed! (2)

(这个文件需要登slapd启动时,才会生成 所以现在没有 不用管)

 这里报错是因为在第三步后没有重新生成配置文件,启动slapd。而是直接修改配置文件去了

 

12、可以试下这样启动

slaptest -f /etc/openldap/slapd.conf  -u

config file testing succeeded

image.png 

测试配置文件没问题

 

13、启动服务

1)问题1

image.png 

这里又报错,这是因为没有给/var/lib/ldap授权,授权后chown -R ldap.ldap /var/lib/ldap/,再重启slapd/etc/init.d/slapd restart,可以看到成功的

 

 

chown -R ldap.ldap /var/lib/ldap/

/etc/init.d/slapd restart

Stopping slapd:                                            [FAILED]

Starting slapd:                                            [  OK  ]

image.png 

 

2)问题2:如果出现:

image.png 

报错原因为权限问题,解决方法为

rm -rf /etc/openldap/slapd.d/*

slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/

chown -R ldap.ldap /etc/openldap/slapd.d

/etc/init.d/slapd restart

 

14、把ldap的日志加入系统日志记录

cp /etc/rsyslog.conf /etc/rsyslog.conf.bak.$(date +%F%T)
echo '#record ldap.log by hxy 2018-07-08' >> /etc/rsyslog.conf
echo 'local4.* /var/log/ldap.log'>> /etc/rsyslog.conf
tail -1 /etc/rsyslog.conf
#local4.* /var/log/ldap.log
service rsyslog restart

 

15重新生成配置文件的配置信息:

先删除最先的配置文件生成的信息:

rm -rf /etc/openldap/slapd.d/*

重新生成:

slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/

config file testing succeeded

查看是否生成的是自己修改的配置文件信息:

cat /etc/openldap/slapd.d/cn\=config/olcDatabase\=\{2\}bdb.ldif

他这里会输出一推信息,我们只关心我们的信息是否存在就行了:

cat /etc/openldap/slapd.d/cn\=config/olcDatabase\=\{2\}bdb.ldif|egrep "olcSuffix|olcRootDN"

olcSuffix: dc=hxy,dc=com

olcRootDN: cn=Forand,dc=hxy,dc=com

 image.png

这样就OK

授权 重启:

chown -R ldap.ldap /etc/openldap/slapd.d/

/etc/init.d/slapd restart

 

16、配置ldap数据库权限

ll /var/lib/ldap/DB_CONFIG 
chown ldap.ldap /var/lib/ldap/DB_CONFIG
chmod 700 /var/lib/ldap/
ls -l /var/lib/ldap/

 

17、验证是否成功:

slaptest -u

config file testing succeeded

image.png 

 

18、创建一个账号,用于导入ldap数据库(密码123456)

useradd testuser

passwd testuser

useradd ldapuser

passwd ldapuser

 

19、至此,这些用户仅仅是系统上存在的用户(存储在/etc/passwd/etc/shadow上),并没有在LDAP数据库里,所以要把这些用户导入到LDAP里面去。但LDAP只能识别特定格式的文件 即后缀为ldif的文件(也是文本文件),所以不能直接使用/etc/passwd/etc/shadow。 需要migrationtools这个工具把这两个文件转变成LDAP能识别的文件。

 

20、安装配置migrationtools

yum install migrationtools -y

 

21、进入migrationtool配置目录

cd /usr/share/migrationtools/

23、首先编辑

sed -i 's/padl/hxy/g' migrate_common.ph

 

vim migrate_common.ph

image.png 

改为

# Default DNS domain

$DEFAULT_MAIL_DOMAIN = "hxy.com";

 

# Default base

$DEFAULT_BASE = "dc=hxy,dc=com";

 

 

24、创建LDIF文件 

拷贝密码和组的文件到tmp下面然后留下你需要的用户和组,不然整个用系统的文件有很多很杂,

cp /etc/passwd /tmp/

cp /etc/group /tmp/

 

cat /tmp/passwd

testuser:x:501:501::/home/testuser:/bin/bash

ldapuser:x:502:502::/home/ldapuser:/bin/bash

 

cat /tmp/group

testuser:x:501:

ldapuser:x:502:

 

25、下面利用pl脚本将/etc/passwd /etc/shadow生成LDAP能读懂的文件格式,保存在/tmp/

cd /usr/share/migrationtools/

./migrate_base.pl > /tmp/base.ldif

./migrate_passwd.pl  /tmp/passwd > /tmp/passwd.ldif

./migrate_group.pl  /tmp/group > /tmp/group.ldif

 

26、下面就要把这三个文件导入到LDAP,这样LDAP的数据库里就有了我们想要的用户

ldapadd -x -D "cn=Forand,dc=hxy,dc=com" -W -f /tmp/base.ldif

ldapadd -x -D "cn=Forand,dc=hxy,dc=com" -W -f /tmp/passwd.ldif

ldapadd -x -D "cn=Forand,dc=hxy,dc=com" -W -f /tmp/group.ldif

过程若无报错,则LDAP服务端配置完毕

这过程中会让你输密码,密码就是你上面配置的密码

image.png

 image.png

image.png


 

注:如果遇到如下错误:

image.png

解决办法是:

rm -rf /etc/openldap/slapd.d/*

chown -R ldap.ldap /etc/openldap

slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d

config file testing succeeded

 

27、测试查找内容

ldapsearch -LLL -W -x -H ldap://hxy.com -D "cn=Forand,dc=hxy,dc=com" -b "dc=hxy,dc=com" "(uid=*)"

image.png 

如果找不到就重启下ldap就行

 

到这里ldap的基本配置算是完成了

我们还可以下载个web管理界面来更号的管理ldap

28、ldap master配置web管理接口

①、安装lamp环境
yum install -y httpd php php-ldap php-gd

②、安装ldap-account-manager管理软件
https://www.ldap-account-manager.org/lamcms/releases?page=3
ldap-account-manager-3.7.tar.gz安装包上传到/var/www/html目录

cd /var/www/html/

tar zxf ldap-account-manager-3.7.tar.gz

mv ldap-account-manager-3.7 ldap

cd ldap/config

cp config.cfg_sample config.cfg

cp lam.conf_sample lam.conf

sed -i 's#cn=Manager#cn=Forand#g' lam.conf

sed -i 's#dc=my-domain#dc=hxy#g' lam.conf

chown -R apache.apache /var/www/html/ldap

 

 

③页面登录一下

http://192.168.18.184/ldap/templates/login.php

image.png

密码就是上面你配置的ldap的密码

选择中文登录

 

image.png

点击建立

 

⑤查看你导入进来的用户

image.png 

⑥新建新的用户

点击新用户

1)、编辑个人信息

image.png 

2)、编辑unix信息

image.png

3)、再设置密码(123456),点击确定,然后点击保存

image.png

image.png 

4)、再在用户里面查看一下

image.png

 

29、检查新建的ldap用户是否能够查询到

ldapsearch -LLL -W -x -H ldap://hxy.com -D "cn=Forand,dc=hxy,dc=com" -b "dc=hxy,dc=com" "(uid=test1)"

image.png

能查询到OK ldap配置完成

 

到此ldap配置完成

 

二、配置svn

1、安装svn

yum install -y subversion

 

2、新建svn目录

mkdir -p /root/svn/svnserver

4、启动svn

svnserve -d -r /root/svn/svnserver/

备注:如果要更改端口启动的时候接上--listen-port”新端口。

 

5、创建版本库

svnadmin create /root/svn/svnserver/patch

 

6、修改svnserve.conf配置文件,在[general]下方加入这四行

 

> /root/svn/svnserver/patch/conf/svnserve.conf

cat >> /root/svn/svnserver/patch/conf/svnserve.conf<<EOF

[general]

anon-access = none

auth-access = write

password-db = passwd

authz-db = authz

realm = patch

[sasl]

use-sasl = true

EOF

 

 

vim /root/svn/svnserver/patch/conf/svnserve.conf

[general]

anon-access = none

auth-access = write

password-db = passwd

authz-db = authz

realm = patch

[sasl]

use-sasl = true

备注:realm 这行也可以不加。

 

7、创建svn用户和权限分配

/root/svn/svnserver/patch/conf目录下,有两个文件是配置用户账号密码和用户权限的。

 

passwd 此文件配置用户账号和密码

vim /root/svn/svnserver/patch/conf/passwd

[users]

svnTest= 123456

test = 123456

 

authz 此文件配置用户权限

vim /root/svn/svnserver/patch/conf/authz

[aliases]

[groups]

ldap_user = test1

admin = test

user = test,svnTest

[patch:/]

@admin = rw

@user = rw

@ldap_user = rw

备注:authz这个文件详细控制权限

 

9linux上检查命令是

svn checkout svn://192.168.18.184:3690/patch

image.png 

image.png

输入完成后这个就是svn

 

或者是在windows上面下个客户端做实验都行

image.png

image.png 

 

 

三、SVN配置完成后配置saslauthd的认证方式

1saslauthd

cat >> /etc/saslauthd.conf <<EOF

ldap_servers: ldap://hxy.com/

ldap_bind_dn: cn=Forand,dc=hxy,dc=com

ldap_bind_pw: hxy123

ldap_search_base: ou=People,dc=hxy,dc=com

ldap_filter: uid=%U

ldap_password_attr: userPassword  

EOF

 

vim /etc/saslauthd.conf

ldap_servers: ldap://hxy.com/ #你的ldap地址

ldap_bind_dn: cn=Forand,dc=hxy,dc=com

ldap_bind_pw: hxy123 #你ldap的密码

ldap_search_base: ou=People,dc=hxy,dc=com

ldap_filter: uid=%U

ldap_password_attr: userPassword  

 

2、修改saslauthd认证方式为ldap认证

sed -i 's/MECH=pam/MECH=ldap/' /etc/sysconfig/saslauthd

 

vim /etc/sysconfig/saslauthd

MECH=pam

改为

MECH=ldap

 

3、修改svn使用saslauthd认证

echo -e 'pwcheck_method: saslauthd\nmech_list: PLAIN LOGIN' >>/etc/sasl2/svn.conf

 

vim /etc/sasl2/svn.conf

pwcheck_method: saslauthd

mech_list: PLAIN LOGIN   

 

4、然后重启saslauthd服务

/etc/init.d/saslauthd restart

 

5、然后试着验证一下ldap用户用

testsaslauthd -utest1 -p123456

image.png

这样就没问题了

 

 

6、svnserver的配置文件上把saslauth的认证开启

 

image.png

 

7、取消这个的注释

vim /root/svn/svnserver/patch/conf/svnserve.conf

use-sasl = true

或者可以直接用sed

sed -i 's/# use-sasl = true/use-sasl = true/' /root/svn/svnserver/patch/conf/svnserve.conf

 

8、重启svn服务

pkill svn

svnserve -d -r /root/svn/svnserver/

image.png

9、然后用ldap账户提交svn

svn checkout svn://192.168.18.184/patch /tmp --username=test1 --password=123456

image.png

image.png 

这样就没问题了

 

到此ldap+svn试验完成


本文参考:

https://www.cnblogs.com/dadong616/p/5092655.html

https://blog.csdn.net/reblue520/article/details/51804162


猜你喜欢

转载自blog.51cto.com/853056088/2138855