在本章实验之前需要提前搭建好,用户基于证书认证的openvpn并且能够正常链接,我是按照我之前发的第一篇关于openvpn的帖子做的。

1)提前准备 :用户基于证书访问openvpn

2)安装 ldap ,ldap web 并创建用户,能够在通过 ldap 找到创建的用户

3)配置 openvpn 基于openldap  验证

 

环境介绍

cat /etc/redhat-release

uname -r

Image2_thumb1

 

需要有以下组件,缺一不可

yum grouplist   #查看安装了那些包组

   Base

   Compatibility libraries

   Debugging Tools

   Development tools

   Dial-up Networking Support

   Hardware monitoring utilities

   Performance Tools

 

时间同步并添加定时任务(这一步在之前搭建openvpn基于证书认证时已经创建了)

 

修改 /etc/hosts 文件,指定本机地址和将要设定的域名。修改后通过 ping 命令进行测试

echo “192.168.6.28 yuci.com” >> /etc/hosts

ping yuci.com

Image_thumb

 

安装各种依赖包

yum -y install openldap openldap-*

yum -y install nscd nss-pam-ldapd pcre pcre-*

yum -y update nss-softokn-freebl

yum -y install nss-*

 

复制并备份 ldap 的配置文件 slapd.conf

cd /etc/openldap/

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

slappasswd -s 123456 | sed -e "s#{SSHA}#rootpw\t{SSHA}#g" >> slapd.conf

tail -1 slapd.conf

cp slapd.conf slapd.conf.bak

 

编辑 slapd.conf 文件,注释 114-117 行,注意不要有空格

vim slapd.conf

database        bdb

suffix             "dc=yuci,dc=com"

rootdn           "cn=damin,dc=yuci,dc=com"

Image3_thumb

文件最后添加以下选项

loglevel          296     #日志级别

cachesize       1000   # ldap 缓存的记录数

checkpoint     2048 10     #把内存中的数据写回数据文件,每2048kb或者10分钟执行一次

Image4_thumb

删除配置文件中的默认权限,并添加以下新的权限

access to *

by self write

by anonymous auth

by * read

Image5_thumb

Image6_thumb

 

编辑日志log,做好备份

cp /etc/rsyslog.conf /etc/rsyslog.conf.bak

echo '#record ldap.log by yuci 2017-01-06' >> /etc/rsyslog.conf

echo 'local4.*             /var/log/ldap.log' >> /etc/rsyslog.conf

tail -1 /etc/rsyslog.conf

/etc/init.d/rsyslog restart

 

LDAP数据库格式为bdb,存储路径为/var/lib.ldap

grep directory slapd.conf

Image7_thumb

 

从模板中拷贝数据库的配置文件,并且授权修改权限

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

ll /var/lib/ldap/DB_CONFIG

chown ldap:ldap /var/lib/ldap/DB_CONFIG

chmod 700 /var/lib/ldap/

 

测试文件配置成功

slaptest -u

Image8_thumb

启动 openldap 服务

/etc/init.d/slapd start

Image9_thumb

 

Image_thumb1

 

查看端口

lsof -i :389

Image10_thumb

检查进程

ps -ef | grep ldap | grep -v grep

Image11_thumb

添加开机自启动,并查看

chkconfig slapd on

chkconfig --list slapd

Image12_thumb

查看启动日志,如果日志为空,说明没有配好

cat /var/log/ldap.log

Image13_thumb

 

查看 ldap 数据库中的用户

通过以下命令查询 ldap 数据库,报错如下

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

Image14_thumb

解决报错

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

lsof -i :389

再次尝试连接数据库,还是出现以下报错,没有项目,可以进行下一步了

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

Image15_thumb

 

为 ldap master 配置 web 管理接口

安装依赖的软件服务,并检查

yum -y install httpd php php-ldap php-gd -y

rpm -qa httpd php php-ldap php-gd

 

下载地址:3.7版本

https://www.ldap-account-manager.org/lamcms/

 

切换目录,将刚才下载的文件上传到此目录。解压,重命名这个软件

cd /var/www/html/

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

mv ldap-account-manager-3.7/ ldap

Image16_thumb

 

备份,重命名,并修改文件内容

cd ldap/config

cp config.cfg_sample config.cfg_sample.bak

cp lam.conf_sample lam.conf_sample.bak

mv 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=yuci#g' lam.conf

sed -i 's#dc=com#dc=com#g' lam.conf     #因为我的域名是 yuci.com 所以并不需要这项

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

 

重启 httpd 服务

/etc/init.d/httpd start

 

通过浏览器访问 192.168.6.29/ldap , 可以正常访问,准备配置

Image17_thumb6

Image18_thumb1

Image19_thumb1

Image20_thumb1

Image21_thumb1

Image22_thumb

Image23_thumb1

添加一个用户,一个组

Image24_thumb1

Image25_thumb1

Image26_thumb1

服务器端查看数据,找到刚才创建的 yuciuser01 用户

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

Image27_thumb

 

开始配置 openvpn基于openldap 的验证。

要保证用户可以通过证书链接 openvpn,openldap 中有用户

 

更改yum源并yum安装 openvpn-auth-ldap。需要通过这个插件来进行openvpn与openldap之间的链接。

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

sed -i 's@#b@b@g' /etc/yum.repos.d/epel.repo

sed -i 's@mirrorlist@#mirrorlist@g' /etc/yum.repos.d/epel.repo

yum -y install openvpn-auth-ldap

 

备份并修改 openvpn-auth-ldap 的配置文件

cp /etc/openvpn/auth/ldap.conf /etc/openvpn/auth/ldap.conf.bak

vim /etc/openvpn/auth/ldap.conf
[root@openvpn ~]# diff /etc/openvpn/auth/ldap.conf.bak /etc/openvpn/auth/ldap.conf 
  
3c3 

  
< URL ldap://ldap1.example.org 

  
--- 

  
> URL 
ldap://192.168.6.28:389
 

  
6c6 

  
< # BindDN uid=Manager,ou=People,dc=example,dc=com 

  
--- 

  
> BindDN cn=admin,dc=yuci,dc=com 

  
9c9 

  
< # Password SecretPassword 

  
--- 

  
> Password 123456 

  
15c15 

  
< TLSEnable yes 

  
--- 

  
> TLSEnable no 

  
18c18 

  
< FollowReferrals yes 

  
--- 

  
> FollowReferrals no 

  
38c38 

  
< BaseDN "ou=People,dc=example,dc=com" 

  
--- 

  
> BaseDN "dc=yuci,dc=com" 

  
41c41 

  
< SearchFilter "(&(uid=%u)(accountStatus=active))" 

  
--- 

  
> SearchFilter "(uid=%u)"

cp /usr/share/doc/openvpn-auth-ldap-2.0.3/auth-ldap.conf /usr/share/doc/openvpn-auth-ldap-2.0.3/auth-ldap.conf.bak

vim /usr/share/doc/openvpn-auth-ldap-2.0.3/auth-ldap.conf
[root@openvpn ~]# diff /usr/share/doc/openvpn-auth-ldap-2.0.3/auth-ldap.conf.bak /usr/share/doc/openvpn-auth-ldap-2.0.3/auth-ldap.conf 
  
3c3 

  
< URL ldap://ldap1.example.org 

  
--- 

  
> URL 
ldap://192.168.6.28:389
 

  
6c6 

  
< # BindDN uid=Manager,ou=People,dc=example,dc=com 

  
--- 

  
> BindDN cn=admin,dc=yuci,dc=com 

  
9c9 

  
< # Password SecretPassword 

  
--- 

  
> Password 123456 

  
15c15 

  
< TLSEnable yes 

  
--- 

  
> TLSEnable no 

  
18c18 

  
< FollowReferrals yes 

  
--- 

  
> FollowReferrals no 

  
21c21 

  
< TLSCACertFile /usr/local/etc/ssl/ca.pem 

  
--- 

  
> # TLSCACertFile /usr/local/etc/ssl/ca.pem 

  
24c24 

  
< TLSCACertDir /etc/ssl/certs 

  
--- 

  
> # TLSCACertDir /etc/ssl/certs 

  
28,29c28,29 

  
< TLSCertFile /usr/local/etc/ssl/client-cert.pem 

  
< TLSKeyFile /usr/local/etc/ssl/client-key.pem 

  
--- 

  
> # TLSCertFile /usr/local/etc/ssl/client-cert.pem 

  
> # TLSKeyFile /usr/local/etc/ssl/client-key.pem 

  
38c38 

  
< BaseDN "ou=People,dc=example,dc=com" 

  
--- 

  
> BaseDN "dc=yuci,dc=com" 

  
41c41 

  
< SearchFilter "(&(uid=%u)(accountStatus=active))" 

  
--- 

  
> SearchFilter "(uid=%u)" 

  
50,52c50,52 

  
< BaseDN "ou=Groups,dc=example,dc=com" 

  
< SearchFilter "(|(cn=developers)(cn=artists))" 

  
< MemberAttribute uniqueMember 

  
--- 

  
> BaseDN "ou=Group,dc=yuci,dc=com" 

  
> SearchFilter "(|(cn=*)(cn=artists))" 

  
> #MemberAttribute uniqueMember

 

修改 openvpn 的配置文件 server.conf,添加如下内容

plugin /usr/lib64/openvpn/plugin/lib/openvpn-auth-ldap.so "/etc/openvpn/auth/ldap.conf cn=%u"

client-cert-not-required

username-as-common-name

 

修改后 server.conf 文件内容如下

port 1194 
  proto tcp 

  dev tun 

  ca /etc/openvpn/keys/ca.crt 

  cert /etc/openvpn/keys/server.crt 

  key /etc/openvpn/keys/server.key  # This file should be kept secret 

  dh /etc/openvpn/keys/dh1024.pem 

  log /var/log/openvpn.log 

  server 10.8.0.0 255.255.255.0 

  ifconfig-pool-persist ipp.txt 

  keepalive 10 120 

  comp-lzo 

  persist-key 

  persist-tun 

  status openvpn-status.log 

  verb 3
plugin /usr/lib64/openvpn/plugin/lib/openvpn-auth-ldap.so "/etc/openvpn/auth/ldap.conf cn=%u" 
  client-cert-not-required 

  username-as-common-name

 

还需要更改客户端的配置文件 user1.ovpn 文件,添加如下内容

auth-user-pass

client      
dev tun       
proto tcp       
remote 192.168.6.45 1194       
resolv-retry infinite       
nobind       
persist-key       
persist-tun       
ca ca.crt       
;cert user1.crt       
;key user1.key       
ns-cert-type server       
auth-user-pass 

  
comp-lzo       
verb 3

 

到此位置配置完成,通过 user1用户可以验证链接 openvpn ,在 openldap web 上创建的用户都可以通过验证并链接。