# openVPN+LDAP AD认证,组权限管理

时间:2024-11-27 11:36:44

# openVPN+LDAP AD认证,组权限管理

原创内容http://www.cnblogs.com/elvi/p/7661178.html

# openVPN+LDAP AD认证,组权限管理

#环境 centos6
#关闭selinux、防火墙
#vim /etc/sysconfig/network #修改hostname
#vim /etc/hosts #添加hostname名称

#更换阿里源
yum -y install wget vim
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
#安装epel包
#yum install epel-release #EPEL源
yum -y install http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum makecache #生成缓存

ntpdate ntp.shu.edu.cn ##同步时间

# 安装openssl和lzo,lzo用于压缩通讯数据加快传输速度
#yum -y install openssl openssl-devel
#yum -y install lzo
#yum -y install openvpn easy-rsa # 安装openvpn和easy-rsa
yum -y install openssl openssl-devel lzo openvpn easy-rsa

# 修改vars文件
cd /usr/share/easy-rsa/2.0/
cp vars vars.bak
vim vars
    # 修改注册信息,比如公司地址、公司名称、部门名称等。
    export KEY_COUNTRY="CN"
    export KEY_PROVINCE="ShangHai"
    export KEY_CITY="ShangHai"
    export KEY_ORG="elven"
    export KEY_EMAIL="me@elven.me"
    export KEY_OU="elven"
# 初始化环境变量
source vars
# 清除keys目录下所有与证书相关的文件
./clean-all
# 生成根证书ca.crt和根密钥ca.key(一路按回车即可,存放目录/usr/share/easy-rsa/2.0/keys)
./build-ca
# 服务端证书和密钥(一路按回车,提示y/n时,输入y再按回车,一共两次)
./build-key-server server
# 为客户端生成证书和密钥(一路按回车,提示y/n时,输入y再按回车,一共两次)
./build-key client
# 创建迪菲·赫尔曼密钥,会生成dh2048.pem文件(生成过程慢,不要去中断它)
./build-dh
# 生成ta.key文件(防DDos攻击、UDP淹没等恶意攻击)
openvpn --genkey --secret keys/ta.key

###创建服务器端配置文件
# 在openvpn的配置目录下新建一个keys目录
mkdir /etc/openvpn/keys
# 将需要用到的openvpn证书和密钥复制一份到刚创建好的keys目录中
cp /usr/share/easy-rsa/2.0/keys/{ca.crt,server.{crt,key},dh2048.pem,ta.key} /etc/openvpn/keys
# 复制一份服务器端配置文件模板server.conf到/etc/openvpn/
cp /usr/share/doc/openvpn-*/sample/sample-config-files/server.conf /etc/openvpn/
# 查看server.conf里的配置参数
grep '^[^#;]' /etc/openvpn/server.conf
# 编辑server.conf
cp /etc/openvpn/server.conf /etc/openvpn/server.conf.bak
vim /etc/openvpn/server.conf
    #--------
    port
    proto tcp
    dev tun
    ca keys/ca.crt
    cert keys/server.crt
    key keys/server.key  # This file should be kept secret
    dh keys/dh2048.pem
    server 192.168.98.0 255.255.255.0 #虚拟网段
    ifconfig-pool-persist ipp.txt
    push "route 192.168.98.0 255.255.255.0"
    push "route 192.168.0.0 255.255.255.0"
    push "route 172.16.11.0 255.255.255.0"
    push "dhcp-option DNS 172.16.11.12"
    push "dhcp-option DNS 172.16.11.14"
    ;push "redirect-gateway def1 bypass-dhcp"  #使客户端所有网络通信通过vpn
    client-to-client
    duplicate-cn
    keepalive
    tls-auth keys/ta.key  # This file is secret
    comp-lzo
    persist-key
    persist-tun
    status openvpn-status.log
    log-append openvpn.log
    verb
    #--------

###配置内核和防火墙,启动服务
# 开启路由转发功能
sysctl -
sed -i '/net.ipv4.ip_forward/s/0/1/' /etc/sysctl.conf
sysctl -p
# 配置防火墙,别忘记保存
iptables -I INPUT -p tcp --dport  -m comment --comment "openvpn" -j ACCEPT
iptables -t nat -A POSTROUTING -s  -j MASQUERADE
service iptables save

# 启动openvpn并设置为开机启动
service openvpn start
chkconfig openvpn on
netstat -anpt |grep vpn #查看网络端口
#service iptables restart
#service openvpn restart

#客户端配置文件
#cp /usr/share/doc/openvpn-*/sample/sample-config-files/client.conf client.ovpn
vim /etc/openvpn/client.ovpn
    # client.ovpn
    client
    dev tun
    proto tcp
    #remote
    remote
    resolv-retry infinite
    nobind
    persist-key
    persist-tun
    ca ca.crt
    #cert client.crt
    #key client.key
    remote-cert-tls server
    tls-auth ta.key
    comp-lzo
    verb
    auth-user-pass #密码认证

########################
# 开启客户端
openvpn /etc/openvpn/client.ovpn &
pkill openvpn #关闭进程

#client keys
mkdir /etc/openvpn/kk
# 客户端需要的证书复制到刚创建好的目录
cp /usr/share/easy-rsa/2.0/keys/{ca.crt,client.{crt,key},ta.key} /etc/openvpn/kk

#Windows客户端
#将OpenVPN服务器上的client.ovpn、ca.crt、client.crt、client1.key、ta.key上传到Windows客户端安装目录下的config文件夹

#############Ldap  openvpn-auth-ldap认证(推荐)
cd /etc/openvpn
yum install openvpn-auth-ldap -y
vi /etc/openvpn/auth/ldap.conf
    #---
    ### OpenVPN LDAP 认证
    <LDAP>
    URL ldap://172.16.11.14:389
    #认证用户密码
    BindDN "CN=ldap,OU=Dev,OU=Tech,OU=XLH_users,DC=test,DC=cn"
    password "
    #设置网络超时时间
    Timeout
    TLSEnable no
    FollowReferrals no
    </LDAP>
    <Authorization>
    BaseDN "DC=test,DC=cn"
    SearchFilter "(&(sAMAccountName=%u)(memberof=CN=vpn,OU=role,OU=XLH_users,DC=test,DC=cn))"
    #SearchFilter "(sAMAccountName=%u)"
    RequireGroup false
    #使用AD组权限
    <Group>
    BaseDN "OU=role,OU=XLH_users,DC=test,DC=cn"
    SearchFilter "(|(cn=vpn))"
    #MemberAttribute "member"
    MemberAttribute uniqueMember
    </Group>
    </Authorization>
    #---
vim /etc/openvpn/server.conf
# openvpn-auth-ldap
    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
service openvpn restart
##############################################
#@ checkpsw.sh  密码文件认证
vim /etc/openvpn/checkpsw.sh
##
chmod +x /etc/openvpn/checkpsw.sh
vim /etc/openvpn/psw-file
aa1 qq1
 vim /etc/openvpn/psw-file
vim /etc/openvpn/server.conf
    # ---- checkpsw.sh
    script-security  system
    auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env
    client-cert-not-required
    username-as-common-name
service openvpn restart
##############################################
#合并证书到配置文件中,仅供参考
vim client.ovpn
#ca ca.crt 注释证书文件
#在最后面添加以下内容:
</key>
key-direction
<tls-auth>
ta.key文件内容
</tls-auth>
################ Python脚本 Ldap 认证
cd /etc/openvpn
echo elven >>/etc/openvpn/authfile.conf #openvpn Ldap授权用户文件
echo test2 >/etc/openvpn/user.conf  #Ldap认证用户
echo w1w1 >>/etc/openvpn/user.conf
#yum install python-ldap
vim /etc/openvpn/check_credit.py
 #openvpn-Ldap.py
chmod +x /etc/openvpn/check_credit.py
python check_credit.py user.conf 

vim /etc/openvpn/server.conf
# openvpn-Ldap.py
auth-user-pass-verify /etc/openvpn/check_credit.py via-file
client-cert-not-required
username-as-common-name
script-security
##############################################