前面已经成功部署了OpenVPN的服务端。客户端和服务端采用的是基于TLS的双向认证,需要给每个客户端生成客户端私钥和证书。本篇将在OpenVPN服务端集成的OpenLDAP认证,这样能够使客户端用户在连接VPN时直接使用统一的OpenLDAP账号。部署的环境和版本信息如下:

安装openvpn-auth-ldap

使用yum安装:

1yum install epel-releas
2yum install openvpn-auth-ldap

主要安装了/usr/lib64/openvpn/plugin/lib/openvpn-auth-ldap.so文件。

编辑/etc/openvpn/auth/ldap.conf文件:

 1<LDAP>
 2        URL             ldap://192.168.61.159:389
 3        BindDN          cn=Manager,dc=frognew,dc=com
 4        Password        thepassword
 5        Timeout         15
 6        TLSEnable       no
 7        FollowReferrals no
 8</LDAP>
 9
10<Authorization>
11        BaseDN          "ou=People,dc=frognew,dc=com"
12
13        SearchFilter    "uid=%u"
14
15        RequireGroup    true
16
17        <Group>
18                BaseDN          "ou=Group,dc=frognew,dc=com"
19                SearchFilter    "cn=vpn"
20                MemberAttribute memberUid
21        </Group>
22</Authorization>

编辑openvpn的配置文件/etc/openvpn/server.conf:

1plugin /usr/lib64/openvpn/plugin/lib/openvpn-auth-ldap.so "{{ openvpn_auth_ldap_conf_dir }}/ldap.conf cn=%u" 
2client-cert-not-required 
  • 使用了上面安装的openvpn-auth-ldap认证插件
  • client-cert-not-required不再需要客户端证书,将改为使用OpenLDAP中的用户认证

注意上面的ldap.conf中RequireGroup true以及Group的配置实际我们期望是必须是LDAP中的名称为vpn的组下的用户才可以登录VPN。但根据这个ISSUE https://github.com/threerings/openvpn-auth-ldap/issues/7,当前2.0.3的openvpn-auth-ldap不支持。因此如果只想限制LDAP中某些用户可以使用VPN的话,只能设置RequireGroup false,然后可以在SearchFilter中做一些文章,比如(&(uid=%u)(ou=vpn))即只有用户的ou字段为vpn的才可以。

完成配置后重新启动openvpn。

客户端配置和测试

客户端的配置调整如下:

 1client
 2dev tun
 3proto tcp
 4remote xxx.xxx.xxx 11194
 5resolv-retry infinite
 6nobind
 7persist-key
 8persist-tun
 9
10ca ca.crt
11;cert client.crt
12;key client.key
13remote-cert-tls server
14tls-auth ta.key 1
15cipher AES-256-CBC
16
17ns-cert-type server
18auth-user-pass
19
20comp-lzo
21verb 3
  • 上面的配置注释掉了cert client.crt;key client.key不再需要客户端证书client.crt和秘钥client.key
  • ns-cert-type serverauth-user-pass是新加入的配置开启了用户名密码认证

修改完配置后使用VPN客户端连接即可。

参考