服务器维护,服务器代维,安全设置,漏洞扫描,入侵检测服务

运维之家

 找回密码
 注册
搜索
查看: 5790|回复: 0

freeradius+openvpn+mysql搭建

[复制链接]
dirtysea 发表于 2012-8-17 18:08:33 | 显示全部楼层 |阅读模式

http://blog.chinaunix.net/space.php?uid=8551991&do=blog&id=94037

 

一、前言
具体的调用流程是:
win的openvpn客户端 auth-user-pass认证模式(通过ca.crt  ta.key  server.crt),到openvpn服务器,调用插件radiusplugin (
/usr/local/openvpn/radiusplugin.so /usr/local/openvpn/radiusplugin.cnf),radiusplugin调用radiusd(name=127.0.0.1、sharedsecret= testing123radius的服务端口),通过radiusd的clients.conf配置 实现服务器的本身127.0.0.1密匙模式调用radiusd,再通过radiusd.conf的配置 sql方式的认证,再到radiusd的sql.conf配置(通过rlm_sql_mysql调用)到mysql数据库
 
具体的安装是上述调用的逆向过程
 
 
二、安装freeradius以及rlm_sql_mysql.so
1、  编译安装
(1)、编译安装freeradius
wget  ftp://ftp.freeradius.org:/pub/radius/freeradius-1.1.7.tar.gz
tar zxvf freeradius-1.1.7.tar.gz
cd freeradius-1.1.7
./configure 
cp libltdl/ltdl.h src/include/
make
make install
注释:如果没有cp libltdl/ltdl.h src/include/  在make的时候会有2个错误,应该是路径的问题
 
(2)、安装rlm_sql_mysql相关
由于我的mysql是编译好的二进制包安装的,所以系统没有rlm_sql_mysql.so这个东西
所以要重新编译出rlm_sql_mysql.so这个东西来。
ln -s /usr/local/mysql/bin/mysql_config /sbin/mysql_config
cd  /usr/local/src/freeradius-1.1.7/src/modules/rlm_sql/drivers/rlm_sql_mysql
./configure  --with-mysql-dir=/usr/local/mysql  --with-mysql-lib-dir=/usr/local/mysql/lib \
--with-mysql-include-dir=/usr/local/mysq/include
make
make install
 
这里的编译参数是根据每个人的实际情况设定的,这一步的编译要在freeradius编译安装后再做。
 
vi /etc/ld.so.conf
/usr/local/lib
ldconfig
 
 
 

2、构建radius数据库(建立相关用户和密码)
/usr/local/mysql/bin/mysql uroot pXXXX

mysql>create database radius;

mysql>GRANT ALL PRIVILEGES ON radius.* TO radius@localhost IDENTIFIED BY  'AAAA';

mysql>\q
cd /usr/local/src/freeradius-1.1.7
/usr/local/mysql/bin/mysql -uroot -pXXXX radius < ./doc/examples/mysql.sql
 
3、配置radiusd.conf以及sql.conf
(1)、radiusd.conf配置
vi /usr/local/etc/raddb/radiusd.conf
 
proxy_requests = no
 
authorize {
preprocess
chap
mschap
suffix
eap
sql
#      pap
#      files
}
注释:使用sql,authorize中的file必须被注释掉。
preacct {
        preprocess
        acct_unique
        suffix
#       files
}

accounting {
      detail
      unix
      radutmp
      sql
}
     

(2)、sql.conf配置
vi /usr/local/etc/raddb/sql.conf
driver
 = "rlm_sql_mysql"
server = "localhost"
login = "radius"
password = "AAAA"

radius_db = "radius"

配置NAS信息:
vi /usr/local/etc/raddb/clients.conf

#
本地帐号,用于测试
client 127.0.0.1 {
        secret          = testing123
        shortname       = localhost
        nastype         = other
}
 
注释:
secret  这里是针对client 127.0.0.1通讯密匙

#mysql -u root -p
mysql> use radius;
建立组信息:
mysql> insert into radgroupreply (groupname,attribute,op,value) values ('user','Auth-Type',':=','Local');
mysql> insert into radgroupreply (groupname,attribute,op,value) values ('user','Service-Type',':=','Framed-User');
mysql> insert into radgroupreply (groupname,attribute,op,value) values ('user','Framed-IP-Address',':=','255.255.255.255');
mysql> insert into radgroupreply (groupname,attribute,op,value) values ('user','Framed-IP-Netmask',':=','255.255.255.0');
建立用户信息:
mysql> insert into radcheck (username,attribute,op,value) values ('test','User-Password',':=','test');
mysql> insert into radcheck (username,attribute,op,value) values ('sense','User-Password',':=','123456');
将用户加入组中:
mysql> insert into usergroup (username,groupname) values ('test','user');

 
注释:
对于 radcheck 这个表attribute的项有好几种设置的,我们设置的是认证的密码模式明码User-Password,也可以设置成加密Crypt-Password,如果是加密模式需要libgcrypt支持。
同时对于表radgroupreply中项attribute,的含义不是太明白:Auth-Type  Service-Type  Framed-IP-Address Framed-IP-NetmaskAcct-Interim-Interval
 
 
 
测试:
radiusd –x &
这个时候控制台会被占用
 
另开一个控制台进行测试
ln -s /usr/local/bin/radtest /sbin/radtest
/sbin/radtest test test localhost 0 testing123
Sending Access-Request of id 204 to 127.0.0.1 port 1812
        User-Name = "test"
        User-Password = "test"
        NAS-IP-Address = 255.255.255.255
        NAS-Port = 0
rad_recv: Access-Accept packet from host 127.0.0.1:1812, id=204, length=38
        Service-Type = Framed-User
        Framed-IP-Address = 255.255.255.255
        Framed-IP-Netmask = 255.255.255.0
/sbin/radtest sense 123456 localhost 0 testing123
Sending Access-Request of id 212 to 127.0.0.1 port 1812
        User-Name = "sense"
        User-Password = "123456"
        NAS-IP-Address = 255.255.255.255
        NAS-Port = 0
rad_recv: Access-Accept packet from host 127.0.0.1:1812, id=212, length=20
由于test加入了组所以信息量多一点
 
以上测试说明freeradius-mysql工作正常!
说明(freeradius\rlm_sql_mysql.so\mysql\)正常!

三、     OpenVPN
 
1、其中我的openssl我是系统自带的,所以不要再次安装
rpm -aq|grep openssl
openssl-devel-0.9.8b-8.3.el5
openssl-0.9.8b-8.3.el5
 
2、安装openvpn2.09
cd /usr/local/src
wget http://openvpn.net/release/openvpn-2.0.9.tar.gz
tar zxvf openvpn-2.0.9.tar.gz
cd openvpn-2.0.9
首先是看一下openvpn.spec里面对于liblzo1和openssl的版本要求,对照自己的版本看符合要求吗
 
检查pam-devel包是否安装,否则从系统盘安装改软件包,一般系统安装时都已经安装了
这一部分的pam不一定要安装,因为openvpn不是直接调用mysql的,如果是openvpn直接调用mysql需要安装pam。(openvpn直接通过mysql,跟通过radius调用mysql主要是在计费统计上有很大区别,认证什么的差不多)
rpm -qa | grep pam
pam_passwdqc-1.0.2-1.2.2
pam-0.99.6.2-3.14.el5
pam_pkcs11-0.5.3-23
pam_krb5-2.2.11-1
pam-devel-0.99.6.2-3.14.el5
pam_smb-1.1.7-7.2.1
pam_ccreds-3-5
 
1)、lzo
wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.02.tar.gz
tar zxvf lzo-2.02.tar.gz
cd lzo-2.02
./configure
make
make install
这一部分是支持openvpn的压缩功能
 
(2)、安装openvpn
cd openvpn-2.0.9 
./configure --prefix=/usr/local/openvpn --with-lzo-headers=/usr/local/include/lzo \
--with-lzo-lib=/usr/local/lib --with-ssl-headers=/usr/include/openssl \
--with-ssl-lib=/usr/lib
make
make install
注释:以上路径请根据自己系统的配置调整
 
(3)、生成密匙
cd /usr/local/src/openvpn-2.0.9
cp -rf ./easy-rsa/  /usr/local/openvpn/
cd /usr/local/openvpn/easy-rsa/2.0
source ./vars
./clean-all
./build-ca
./build-key-server server
./build-dh
./build-key cl1
cd keys
/usr/local/openvpn/sbin/openvpn --genkey --secret ta.key
cd /usr/local/openvpn
mkdir ssl
cp -a ./easy-rsa/keys/2.0/ca.crt ./ssl/
cp -a ./easy-rsa/keys/2.0/dh1024.pem ./ssl/
cp -a ./easy-rsa/keys/2.0/ta.key ./ssl/
cp -a ./easy-rsa/keys/2.0/server.crt ./ssl/
cp -a ./easy-rsa/keys/2.0/server.key ./ssl/
 
3、安装 RadiusPlugin
1)、安装
cd /usr/local/src/
wget  wget http://www.nongnu.org/radiusplugin/radiusplugin_v2.0b_beta2.tar.gz 
tar zxvf radiusplugin_v2.0b_beta2.tar.gz
cd radiusplugin_v2.0b_beta2
make
 
cp /usr/local/src/radiusplugin_v2.0b_beta2/radiusplugin.so /usr/local/openvpn/
cp /usr/local/src/radiusplugin_v2.0b_beta2/radiusplugin.cnf /usr/local/openvpn/
vi /usr/local/openvpn/radiusplugin.cnf
这里2个文件的位置,是自己定义的,根据这个定义的位置,后面相关配置要设定这里路径的
 
2)、配置radiusplusin插件的配置文件radiusplugin.cnf
 
# The NAS identifier which is sent to the RADIUS server
NAS-Identifier=OpenVpn
# The service type which is sent to the RADIUS server
Service-Type=5
# The framed protocol which is sent to the RADIUS server
Framed-Protocol=1
# The NAS port type which is sent to the RADIUS server
NAS-Port-Type=5
# The NAS IP address which is sent to the RADIUS server
NAS-IP-Address=127.0.0.1
# Path to the OpenVPN configfile. The plugin searches there for
# client-config-dir PATH   (searches for the path)
# status FILE                      (searches for the file, version must be 1)
# client-cert-not-required (if the option is used or not)
# username-as-common-name  (if the option is used or not)
OpenVPNConfig=/usr/local/openvpn/server.conf
 
 # Support for topology option in OpenVPN 2.1
# If you don't specify anything, option "net30" (default in OpenVPN) is used. 
# You can only use one of the options at the same time.
# If you use topology option "subnet", fill in the right netmask, e.g. from OpenVPN option "--server NETWORK NETMASK"  
#subnet=255.255.255.0
# If you use topology option "p2p", fill in the right network, e.g. from OpenVPN option "--server NETWORK NETMASK"
#p2p=10.10.0.1
####################### Ich benutze die Default Option
 
# Allows the plugin to overwrite the client config in client config file directory,
# default is true
overwriteccfiles=true
 
# Path to a script for vendor specific attributes.
# Leave it out if you don't use an own script.
# vsascript=/root/workspace/radiusplugin_v2.0.5_beta/vsascript.pl
 
# Path to the pipe for communication with the vsascript.
# Leave it out if you don't use an own script.
# vsanamedpipe=/tmp/vsapipe
 
# A radius server definition, there could be more than one.
# The priority of the server depends on the order in this file. The first one has the highest priority.
server
{
        # The UDP port for radius accounting.
        acctport=1813
        # The UDP port for radius authentication.
        authport=1812
        # The name or ip address of the radius server.
        name=127.0.0.1
        # How many times should the plugin send the if there is no response?
        retry=1
        # How long should the plugin wait for a response?
        wait=1
        # The shared secret.
        sharedsecret= testing123
}
 
 
注释:这里的sharedsecret= testing123radiusd的设置相关,注意/usr/local/etc/raddb/clients.conf中的client 127.0.0.1 的设置,(这个clients.conf是调用radius的客户端配置)
 client 127.0.0.1 {
    secret = testing123
       shortname = localhost
    nastype = other
}
同时重要的是要在正式应用的时候 同时修改这2个地方的testing123,这个就是radius的密匙
 
4、配置openvpn的服务器设置文件server.conf
cp /usr/local/src/openvpn-2.0.9/sample-config-files/server.conf /usr/local/openvpn/server.conf
vi /usr/local/openvpn/server.conf
 
注释:注意这里的server.conf要与/usr/local/openvpn/radiusplugin.cnf中OpenVPNConfig的设置一致
这里的openvpn服务器端可以有多个配置文件,每一个配置等于开启了一个单独的vpn服务,但是要单独设置每个服务器端配置文件./build-key-server ser***  同时不同的客户端./build-key cl1*** 对应于相应 ser****的unit name,同时客户端要拷贝不同的ser***.crt
 
port 1194   
proto udp
# Which device
dev tun
;fast-io
user nobody
group nogroup
persist-tun
persist-key
server 10.14.0.0 255.255.0.0
management 127.0.0.1 7505
float
 
username-as-common-name
;client-config-dir ccd 
client-cert-not-required
client-to-client
#push "redirect-gateway def1"
push "dhcp-option DNS 172.21.41.15"
ping-timer-rem
keepalive 10 120
# Use compression
comp-lzo
# Strong encryption
tls-server
tls-auth ssl/ta.key 0
dh ssl/dh1024.pem
ca ssl/ca.crt
cert ssl/server.crt
key ssl/server.key
max-clients 200
plugin /usr/local/openvpn/radiusplugin.so /usr/local/openvpn/radiusplugin.cnf
verb 3
mute 10
status /var/log/openvpn/status.log 1
log /var/log/openvpn/openvpn.log
 
 
mkdir /usr/local/openvpn/ccd
mkdir /var/log/openvpn
 
这里我定义10.14段主要是不想跟别人定义的冲突:
#让客户端发起的所有IP请求都通过OPENVPN服务器 
#这一句还是不用的好,没必要全部从vpn走,一般通过dns把内网的通过这里就可以了
#除非对方本地上网是被限制的,那么可以考虑开放这个配置,所有的通过vpn上网
push "redirect-gateway def1"
#定义客户端的dns服务器地址,设置本地的服务器内网地址就可以了 
push "dhcp-option DNS 172.21.41.15"
注释:(这一部分暂时用不到,就是端到端的服务配置)
关于服务器端内网可以访问客户端内网的设置
# 使服务器子网内机器可以访问客户端子网内机器
# 仅用于路由模式
#
假设:客户端子网网段192.168.1.0
#
首先,在服务器配置文件中添加下面这两行
#     client-config-dir ccd
#   
route 192.168.1.0   255.255.255.0
#
然后在服务器端ccd目录下创建一个文件,文件名是客户端的公共名,这里的公共名是客户证书的公共名
# 文件内容是:
  iroute 192.168.1.0   255.255.255.0

;client-config-dir ccd
;route 192.168.1.0   255.255.255.0

ccd目录通常在配置文件目录下面建立,公共名(common name)在生成证书,回答问题时填上的,跟证书的文件名一至.
route 192.168.1.0   255.255.255.0
表示在服务器端增加访问客户端的路由(192.168.1.0/24指的是客户端网段)

我现在的配置还暂时用不到这个设置项,以上只是参考,感觉有些特殊的地方还是用的到的!
client-to-client 如果让Client之间可以相互看见,去掉本行的注释掉,否则Client之间无法相互访问
duplicate-cn  是否允许一个User同时登录多次,去掉本行注释后可以使用同一个用户名登录多次
plugin /usr/local/openvpn/radiusplugin.so /usr/local/openvpn/radiusplugin.cnf 说明使用的插件,
client-cert-not-required #不请求客户的CA证书

username-as-common-name #使用客户提供的UserName作为Common Name

四、开启radiusopenvpn服务
1、建立openvpnshell执行脚本
cp /usr/local/src/openvpn-2.0.9/sample-scripts/openvpn.init  /etc/init.d/
ln -s /usr/local/openvpn/sbin/openvpn  /usr/sbin/openvpn
vi /etc/init.d/openvpn.init
work=/usr/local/openvpn
2、开启 openvpn服务器进程
/etc/init.d/openvpn.init start
这个时候要看一下  ifconfig 有没有 tun0设备,如果有就基本ok了!
如果有错误,请看/var/log/messages  /var/log/openvpn/openvpn.log的信息
如果关闭openvpn使用 /etc/init.d/openvpn.init stop
在测试的时候,由于配置出错,会碰到/etc/init.d/openvpn.init stop关闭不了,使用killall openvpn来关闭
 
3、开启radius服务
这个做到这里容易忘记,我就忘了,还查了一阵子错误
radiusd &
 
五、windows下client.opvn客户端配置文件:
# Which device
dev tun
persist-key
persist-tun
# Our remote peer
nobind
remote *.*.*.* 1194
#出现用户、密码的认证
auth-user-pass
ns-cert-type server
tls-auth ta.key 1
#对于客户端建立了vpn连接后,通过vpn远程走的route设定(这里172.16.0.0  255.248.0.0是所有的内网包括从内部路由走出去的其他公司的内网,可以添加多个网段。)
route 172.16.0.0 255.240.0.0
# Use compression
comp-lzo
# Strong encryption
verb 3
mute 10
 
六、服务器端的路由和防火墙的调整
把以下的iptables命令放在靠前的规则中
#我这里定义$IPT 是 /sbin/iptables
#对于udp 1813和udp 1812 端口,因为radius是127.0.0.1本地的调用,所以不要另外再开许可
#打开openvpn连接端口udp 1194

$IPT -A INPUT -p udp -m udp --dport 1194 -j ACCEPT

#许可tun设备的,如果你开的是tap设备进行相应的更改
$IPT -A INPUT -i tun0  -j ACCEPT
$IPT -A OUTPUT -o tun0  -j ACCEPT
$IPT -A FORWARD -i tun0 -j ACCEPT
$IPT -t nat -A POSTROUTING -s 10.14.0.0/24 -o $LOCAL_IFACE -j SNAT --to-source $LOCAL_IP
七、自安装客户端的生成步骤
1、下载客户端**程序

http://www.openvpn.se/files/nsis/nsis205.exe

http://www.openvpn.se/files/install_packages_source/openvpn_install_source-2.0.9-gui-1.0.3.zip

 

2、安装nsis205.exe

 

1)、生成正确的配置源

windows下解压缩openvpn_install_source-2.0.9-gui-1.0.3.zip

**openvpn_install_source-2.0.9-gui-1.0.3/openvpn/目录下,建立config

将正确的客户端的配置文件和密匙放到这个目录下(一个配置文件、3个跟密匙有关的文件):

client.ovpn ta.key ca.crtserver.crt

 

注释:

clint.ovpn就是client.conf

 

(2)、编辑生成正确的配置文件

编辑openvpn_install_source-2.0.9-gui-1.0.3目录下openvpn-gui.nsi文件

查找  ;File "${HOME}\config\Office.ovpn"

替换成

  File "${HOME}\config\client.ovpn"

  File "${HOME}\config\ca.crt"

  File "${HOME}\config\server.crt"

  File "${HOME}\config\ta.key"

 

3)、生成自动安装程序

运行NSIS Menu

compiler -- star MakNsiSW

菜单files--load script

 

导入openvpn_install_source-2.0.9-gui-1.0.3/目录下的openvpn-gui.nsi

 

导入这个openvpn-gui.nsi就会在目录openvpn_install_source-2.0.9-gui-1.0.3下自动生成openvpn_install_source-2.0.9-gui-1.0.3-install.exe
在客户端运行这个程序就能得到正确配置的客户端!
 
参考:
http://www.roessner-net.com/VPN_RADIUS_MYSQL.howto.txt
http://www.linuxfly.org/read.php?86
http://www.xiaohui.com/dev/server/20070514-install-openvpn.htm
http://www.xxlinux.com/linux/article/development/database/20060707/2522_2.html
http://blog.chinaunix.net/u/2389/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|手机版|Archiver|运维之家

GMT+8, 2024-3-29 02:57 , Processed in 0.108542 second(s), 18 queries .

Powered by Dirtysea

© 2008-2020 Dirtysea.com.

快速回复 返回顶部 返回列表