Total Pageviews

Friday 31 July 2015

freeradius+openvpn+pam_radius整合认证(不太好用)


还是要用radius认证,pam真是好东西
1、freeradius的安装配置,略过了
2、下载pam_radius 模块
wget ftp://ftp.freeradius.org/pub/radius/pam_radius-1.3.16.tar
3、make之后,复制pam_radius_auth.so到/lib/security/.
创建/etc/raddb
把pam_radius_auth.conf 文件重新命名成server,到/etc/raddb/ 修改这个文件,同时要修改freeradius认证端的clients.conf的内容
否则,用saslauthd测试的时候就会出现以下提示
Apr 15 01:10:24 shbflow saslauthd[3461]: pam_radius_auth: Could not open configuration file /etc/raddb/server: No such file or directory
根据日志很好排错
4、/etc/pam.d/openvpn  文件内容
auth            sufficient      pam_radius_auth.so      debug
account         required        pam_radius_auth.so
注意以上几点,freeraidus和openvpn 配合的很好咯
简单的来说,过程是这样的
openvpn通过openvpn-auth-pam.so 模块把验证请求交给PAM
PAM通过pam_radius_auth.so 模块把验证请求交给本地或者远端的freeradius
freeradius 从数据库里面比较用户名密码,返回.

from http://studyboy.cn/?p=241
----------------------------------------------------------------------------
OpenVPN配合RADIUS验证
2007-12-01 00:34
Linux 下有个PAM的模块部分,PAM本身的意思就是“插入式验证模块”。以我理解,意思就是一套灵活的验证机制,PAM提供了很多的验证库,通过PAM可以为 应用程序自由定制各种验证方式和验证流程,Radius验证方式当然也能整合进去。PAM的Radius模块使得应用程序支持Radius协议的验证成为 了可能,可以连接指定的Radius服务器而连接访问到后台的数据库。那么这样的话,就可以通过数据库将多个系统进行后台整合(比如Radius到数据库 里去验证的用户同时也是其他系统或者应用程序中的用户)。因此,按我的认为,这方面的内容是比较有参考价值的。

1.确认安装PAM功能模组
[root@VPNSRV02 ~]#yum install pam*
这里基本上不会有什么问题,因为PAM部分本来就是系统自带的。这里只是确认安装而已。


2.安装Freeradius
[root@VPNSRV02 ~]#yum install freeradius*
Dependencies Resolved
=============================================================================
Package                 Arch       Version          Repository        Size
=============================================================================
Installing: (主要程序包)
freeradius              i386       1.1.3-1.1.el5    base              1.2 M
freeradius-mysql        i386       1.1.3-1.1.el5    base               15 k
freeradius-postgresql   i386       1.1.3-1.1.el5    base               15 k
freeradius-unixODBC     i386       1.1.3-1.1.el5    base               14 k
Installing for dependencies: (相关依赖包)
libtool-ltdl            i386       1.5.22-6.1       base               37 k
lm_sensors              i386       2.10.0-3.1       base              494 k
mysql                   i386       5.0.22-2.1       base              3.0 M
net-snmp                i386       1:5.3.1-14.el5   base              699 k
net-snmp-libs           i386       1:5.3.1-14.el5   base              1.1 M
net-snmp-utils          i386       1:5.3.1-14.el5   base              178 k
perl-DBI                i386       1.52-1.fc6       base              605 k
postgresql              i386       8.1.4-1.1        base              2.8 M
postgresql-libs         i386       8.1.4-1.1        base              194 k
unixODBC                i386       2.2.11-7.1       base              832 k

Transaction Summary
=============================================================================
Install     14 Package(s)        
Update       0 Package(s)        
Remove       0 Package(s)        


3.安装pam_radius
下载pam_radius.tar.gz这个源码包,目前还没有十分现成的rpm包可以用,所以可以下载这个来编译安装从中获得2个比较重要的文件

pam_radius_auth.conf
使用PAM插入验证模块进行Radius验证的配置文件,在编译安装pam_radius这个源码包之后,会自动生成这个配置文件的。

pam_radius_auth.so
支持PAM插入验证模块进行Radius验证的库文件,在编译安装pam_radius这个源码包之后,会自动生成这个库,应当会自己安装到/lib/security/pam_radius_auth.so这个路径,如果没有的话请手动复制或者移动到该路径。


4.配置PAM的RADIUS模块配置文件
查看pam_radius_auth.conf模板文件
[root@VPNSRV02 pam_radius]# cat pam_radius_auth.conf
------------------------------------------------------------------------------------
# pam_radius_auth configuration file. Copy to: /etc/raddb/server
(注意看到第一行的使用提示:就是将此pam_radius_auth.conf文件配置完毕后,需要复制到/etc/raddb/目录下,并且改文件名为server)
# For proper security, this file SHOULD have permissions 0600,
# that is readable by root, and NO ONE else. If anyone other than
# root can read this file, then they can spoof responses from the server!
#
# There are 3 fields per line in this file. There may be multiple
# lines. Blank lines or lines beginning with '#' are treated as
# comments, and are ignored. The fields are:
#
# server[:port] secret [timeout]
#
# the port name or number is optional. The default port name is
# "radius", and is looked up from /etc/services The timeout field is
# optional. The default timeout is 3 seconds.
#
# If multiple RADIUS server lines exist, they are tried in order. The
# first server to return success or failure causes the module to return
# success or failure. Only if a server fails to response is it skipped,
# and the next server in turn is used.
#
# The timeout field controls how many seconds the module waits before
# deciding that the server has failed to respond.
#
# server[:port] shared_secret      timeout (s)
# 127.0.0.1     secret               3
# other-server other-secret        3
(这里就是设定连接的Radius验证服务器的部分,第一项server就是写上Radius服务器的地址如果不设定端口的话表示使用Radius的默认 端口,如果端口经过定制的话,还必须冒号加上指定端口号。第二项shared_sercret是填上与Radius服务器部分共享的关键字部分,需要和 Radius服务器端使用的一致。第三项是验证过程超时时间,单位为秒,当与Radius服务器进行交互的时候,如果等待消息超过这个指定的时间的话,就 认为连接超时失败了)
#
# having localhost in your radius configuration is a Good Thing.
#
# See the INSTALL file for pam.conf hints.
-------------------------------------------------------------------------------------

首先将模版文件复制到/etc/raddb/目录下,并且改名为server,使其生效。
[root@VPNSRV02 ~]# cp pam_radius_auth.conf /etc/raddb/server

编辑pam_radius验证配置文件
[root@VPNSRV02 raddb]# vi server
-------------------------------------------------------------------------------------
# server[:port]       shared_secret      timeout (s)
# 127.0.0.1           secret                 3
# other-server        other-secret       3
223.223.223.20        kanecruise          5
(设定Radius服务器的地址为223.223.223.20,Radius端口使用默认,交互关键字为kanecruise,超时时间为5秒)
123.123.123.10:991    kanecruise        5
(设定第二Radius服务器的地址为123.123.123.10,Radius端口定制为991,交互关键字为kanecruise,超时时间为5秒)
-------------------------------------------------------------------------------------



5.配置PAM下OpenVPN的验证配置文件
由于是OpenVPN使用插入式验证模块,所以它的PAM验证配置文件应当放在/etc/pam.d/这个路径下的,由于在server.conf服务器 端主配置文件中指定了OpenVPN的PAM验证配置文件为openvpn。因此这个文件必须为/etc/pam.d/openvpn,和配置文件中指定 的一致。注意,默认情况下可能没有这个文件,请确认并手动添加和配置。
[root@VPNSRV02 ~]# cd /etc/pam.d
编辑OpenVPN的验证配置
[root@VPNSRV02 pam.d]# vi openvpn
-----------------------------------------------------------------
auth    required        /lib/security/pam_radius_auth.so
(auth使用账户身份验证,required表示条件为“通过必要,失败充分”的意思,即一旦此处验证没有通过的话,那么该用户非法条件已经充足,不需 要其他多余的验证步骤了;但是如果此处验证通过的话,只是该用户通过验证的必要条件而已,并不足以说明该用户通过完全验证。最后一段表示该验证使用的库, 必需要注明该库在系统里的绝对路径)

account required        /lib/security/pam_radius_auth.so
(account使用账户权限验证,验证条件为required,使用/lib/security/pam_radius_auth.so这个库)
-----------------------------------------------------------------



6.处理OpenVPN的Server端和Client端的主配置文件
OpenVPN服务端和客户端的主配置文件
[root@VPNSRV02 openvpn]# vi server.conf
--------------------------------------------------------------------------------------------------
port 9988  
proto udp
dev tun
ca /etc/openvpn/vpnkeys/ca.crt
cert /etc/openvpn/vpnkeys/vpnsrv01.crt
key /etc/openvpn/vpnkeys/vpnsrv01.key
dh /etc/openvpn/vpnkeys/dh1024.pem
server 10.99.0..0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "route 111.111.111.0 255.255.255.0"
push "route 222.222.0.0 255.255.0.0"
push "route 123.123.123.234 255.255.255.255"
client-to-client
;duplicate-cn
keepalive 10 120
tls-auth /etc/openvpn/vpnkeys/ta.key 0
comp-lzo
;max-clients 100
user nobody
group nobody
persist-key
persist-tun
status /var/log/openvpn-status.log
log         /var/log/openvpn.log
log-append /var/log/openvpn.log
verb 3
mute 20

##~~~~~~~~~Radius Special~~~~~~~~~~~~~~##(以下是OpenVPN服务端主配置文件支持Radius验证需要添加的配置项)
plugin /usr/share/openvpn/plugin/lib/openvpn-auth-pam.so openvpn
(设定插入式模块以及对应配置文件。这里指定的模块openvpn-auth-pam是针对OpenVPN的PAM模块,需要指定这个模块所在的绝对路 径。另外在后面需要跟着对应的PAM下OpenVPN这个应用程序的验证配置文件,这里没有写上绝对路径而只写了openvpn这个文件名,其实默认的路 径是在/etc/pam.d/下的。说一下这个来源,在安装了OpenVPN的包之后就会带有这个模块了)

client-cert-not-required
(设定Client端不再需要客户端证书。由于现在对Client使用Radius的PAM方式验证,因此当接入的时候就不再需要Client证书,只需要通过用户名和口令验证)

username-as-common-name
(设定Client使用的通用名就是用户名。Client与Client之间通过不同的Common Name通用名来彼此区别,而且一个Client的接入必须要一个Common Name,这个通用名一般都是记录在Client的证书里面的。但是当使用Radius验证的方式后,Client就不再需要证书,但是它连接的时候同样 需要Common Name,而现在用户名就代替了通用名了)
------------------------------------------------------------------------------------------------------



在Windows系统下的OpenVPN Client主配置文件设定
[root@VPNCLNT02 openvpn]# vi client.ovpen
--------------------------------------------------------------------------------------------------
client
dev tun
proto udp
remote 123.123.123.233 9988
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
(客户端的主配置文件最主要的更改部分在于:只需要指定CA根证书就可以。而cert和key这两项由于Client端接入不再需要证书密钥,所以不需要设置那2项)
ns-cert-type server
tls-auth ta.key 1
comp-lzo
verb 3
mute 20
----------------------------------------------------------------------------



在Linux系统下的OpenVPN Client主配置文件设定
[root@VPNCLNT03 ~]# vi /etc/openvpn/client.conf
----------------------------------------------------------------------------
client
dev tun
proto udp
remote 123.123.123.233 9988
resolv-retry infinite
nobind
user nobody
group nobody
persist-key
persist-tun
ca ca.crt
(客户端的主配置文件最主要的更改部分在于:只需要指定CA根证书就可以。而cert和key这两项由于Client端接入不再需要证书密钥,所以不需要设置那2项)
ns-cert-type server
tls-auth ta.key 1
comp-lzo
verb 3
mute 20

有两个库文件,位置一定要搞清
OpenVPN支持PAM验证模块,是放在它自己的plugin/lib/目录下的
[root@VPNSRV02 ~]# locate openvpn-auth-pam.so
/usr/share/openvpn/plugin/lib/openvpn-auth-pam.so

PAM支持RADIUS的验证模块,是放在系统的/lib/security/目录下的
[root@VPNSRV02 ~]# locate pam_radius_auth.so
/lib/security/pam_radius_auth.so

另外一个就是要确定一个PAM支持库文件libpam.so,它是放在系统/lib/目录下的,一般在安装PAM后就会默认有的,请确认一下。
[root@VPNSRV02 ~]# locate libpam.so
/lib/libpam.so


7.Server端启动OpenVPN服务
[root@VPNSRV02 ~]# service openvpn restart
Shutting down openvpn:                                     [ OK ]
Starting openvpn:                                              [ OK ]


8.在Radius服务器端的工作
需要注意一些问题:
如果Radius服务器端设定在连接协商过程的关键字的话,那么在OpenVPn的Server端也要指定吻合的关键字。使用的端口也要保持一致。
另外,如果有些Radius服务器设定了IP地址验证功能的话,记得要将OpenVPN服务器的IP地址要在Radius服务器端添加为信任地址。


9.Client端启动OpenVPN去连接Server
当然我的Client是Windows的操作系统,下面就是Dos下在连接过程中反馈的信息:
Wed Sep 05 09:53:14 2007 OpenVPN 2.0.9 Win32-MinGW [SSL] [LZO] built on Oct 1 2
006
Enter Auth Username:kanecruise
(由于使用了Radius验证,那么就会通过Radius验证用户的用户名和密码,这里我输入的是用户名)
Enter Auth Password:123456
(这里输入密码。当确定输入后,就会通过Radius向Radius服务器进行访问验证,Radius服务器到数据库中去匹配)
Wed Sep 05 09:53:26 2007 IMPORTANT: OpenVPN's default port number is now 9988, b
ased on an official port number assignment by IANA. OpenVPN 2.0-beta16 and earl
ier used 5000 as the default port.
Wed Sep 05 09:53:26 2007 LZO compression initialized
Wed Sep 05 09:53:26 2007 Control Channel MTU parms [ L:1542 D:138 EF:38 EB:0 ET:
0 EL:0 ]
Wed Sep 05 09:53:26 2007 Data Channel MTU parms [ L:1542 D:1450 EF:42 EB:135 ET:
0 EL:0 AF:3/1 ]
Wed Sep 05 09:53:26 2007 Local Options hash (VER=V4): '41690919'
Wed Sep 05 09:53:26 2007 Expected Remote Options hash (VER=V4): '530fdded'
Wed Sep 05 09:53:26 2007 UDPv4 link local: [undef]
Wed Sep 05 09:53:26 2007 UDPv4 link remote: 123.123.123.233:9988
Wed Sep 05 09:53:26 2007 TLS: Initial packet from 123.123.123.233:9988, sid=9e5c
c104 9b13f722
Wed Sep 05 09:53:26 2007 VERIFY OK: depth=1, /C=CN/ST=Shanghai/L=Shanghai/O=Center/O
U=TL/CN=VPN01/emailAddress=kanecruiseisgod@hotmail.com
Wed Sep 05 09:53:26 2007 VERIFY OK: nsCertType=SERVER
Wed Sep 05 09:53:26 2007 VERIFY OK: depth=0, /C=CN/ST=Shanghai/O=Center/OU=Center/CN=VPN
SRV01/emailAddress=kanecruiseisgod@hotmail.com
Wed Sep 05 09:53:27 2007 Data Channel Encrypt: Cipher 'BF-CBC' initialized with
128 bit key
Wed Sep 05 09:53:27 2007 Data Channel Encrypt: Using 160 bit message hash 'SHA1'
for HMAC authentication
Wed Sep 05 09:53:27 2007 Data Channel Decrypt: Cipher 'BF-CBC' initialized with
128 bit key
Wed Sep 05 09:53:27 2007 Data Channel Decrypt: Using 160 bit message hash 'SHA1'
for HMAC authentication
Wed Sep 05 09:53:27 2007 Control Channel: TLSv1, cipher TLSv1/SSLv3 DHE-RSA-AES2
56-SHA, 1024 bit RSA
Wed Sep 05 09:53:27 2007 [VPNSRV01] Peer Connection Initiated with 221.238.193.2
33:9988
Wed Sep 05 09:53:28 2007 SENT CONTROL [VPNSRV01]: 'PUSH_REQUEST' (status=1)
Wed Sep 05 09:53:28 2007 PUSH: Received control message: 'PUSH_REPLY,route 111.111.111.0
255.255.255.0,route 222.222.0.0 255.255.255.224,route 123.123.123.234
255.255.255.255,route 10.99.0.0 255.255.255.0,ping 10,ping-restart 120,ifconfig 10.99.0.18 10.99.0.17'
Wed Sep 05 09:53:28 2007 OPTIONS IMPORT: timers and/or timeouts modified
Wed Sep 05 09:53:28 2007 OPTIONS IMPORT: --ifconfig/up options modified
Wed Sep 05 09:53:28 2007 OPTIONS IMPORT: route options modified
Wed Sep 05 09:53:28 2007 TAP-WIN32 device [VPN] opened: 
\\.\Global\{6FCB56CF-B12
5-4D24-91A3-822CE03972DD}.tap
Wed Sep 05 09:53:28 2007 TAP-Win32 Driver Version 8.4
Wed Sep 05 09:53:28 2007 TAP-Win32 MTU=1500
Wed Sep 05 09:53:28 2007 Notified TAP-Win32 driver to set a DHCP IP/netmask of 1
0.99.0.18/255.255.255.252 on interface {6FCB56CF-B125-4D24-91A3-822CE03972DD} [DH
CP-serv: 10.99.0.17, lease-time: 31536000]
Wed Sep 05 09:53:28 2007 Successful ARP Flush on interface [2] {6FCB56CF-B125-4D
24-91A3-822CE03972DD}
Wed Sep 05 09:53:28 2007 TEST ROUTES: 0/0 succeeded len=4 ret=0 a=0 u/d=down
Wed Sep 05 09:53:28 2007 Route: Waiting for TUN/TAP interface to come up...
Wed Sep 05 09:53:29 2007 TEST ROUTES: 0/0 succeeded len=4 ret=0 a=0 u/d=down
Wed Sep 05 09:53:29 2007 Route: Waiting for TUN/TAP interface to come up...
Wed Sep 05 09:53:30 2007 TEST ROUTES: 4/4 succeeded len=4 ret=1 a=0 u/d=up
Wed Sep 05 09:53:30 2007 route ADD 111.111.111.0 MASK 255.255.255.224 10.99.0.17
Wed Sep 05 09:53:30 2007 Route addition via IPAPI succeeded
Wed Sep 05 09:53:30 2007 route ADD 222.222.0.0 MASK 255.255.255.224 10.99.0.17
Wed Sep 05 09:53:30 2007 Route addition via IPAPI succeeded
Wed Sep 05 09:53:30 2007 route ADD 123.123.123.234 MASK 255.255.255.255 10.99.0.1
7
Wed Sep 05 09:53:30 2007 Route addition via IPAPI succeeded
Wed Sep 05 09:53:30 2007 route ADD 10.99.0.0 MASK 255.255.255.0 10.99.0.17
Wed Sep 05 09:53:30 2007 Route addition via IPAPI succeeded
Wed Sep 05 09:53:30 2007 Initialization Sequence Completed
----------------------------------------------------------------------------------
连接成功



10.在Client端查看网络接口信息
-----------------------------------------------------------------------------------
Microsoft Windows [Version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.

C:\Documents and Settings\Administrator>ipconfig/all
Windows IP Configuration
   Host Name . . . . . . . . . . . . : kserver
   Primary Dns Suffix . . . . . . . :
   Node Type . . . . . . . . . . . . : Unknown
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter VPN:
(这个就是OpenVPN激活的VPN网络接口,并且从Server那里收到了地址分配)
   Connection-specific DNS Suffix . :
   Description . . . . . . . . . . . : TAP-Win32 Adapter V8
   Physical Address. . . . . . . . . : 00-FF-6F-CB-56-CF
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   IP Address. . . . . . . . . . . . : 10.99.0.18
   Subnet Mask . . . . . . . . . . . : 255.255.255.252
   Default Gateway . . . . . . . . . :
   DHCP Server . . . . . . . . . . . : 10.99.0.17
   Lease Obtained. . . . . . . . . . : 2007年9月5日 10:57:20
   Lease Expires . . . . . . . . . . : 2008年9月4日 10:57:20

Ethernet adapter NET1:
   Connection-specific DNS Suffix . :
   Description . . . . . . . . . . . : Broadcom 440x 10/100 Integrated Controlle
r
   Physical Address. . . . . . . . . : 00-18-8B-82-52-1A
   DHCP Enabled. . . . . . . . . . . : No
   IP Address. . . . . . . . . . . . : 192.168.1.102
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :
   DNS Servers . . . . . . . . . . . : 202.96.209.5
                                       202.96.209.133

Ethernet adapter NET0:
   Connection-specific DNS Suffix . :
   Description . . . . . . . . . . . : ADMtek AN983 10/100 PCI Adapter
   Physical Address. . . . . . . . . : 00-E0-4C-B3-F3-43
   DHCP Enabled. . . . . . . . . . . : No
   IP Address. . . . . . . . . . . . : 192.168.0.197
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.0.1
   DNS Servers . . . . . . . . . . . : 202.96.209.5
                                       202.96.209.133

C:\Documents and Settings\Administrator>
-----------------------------------------------------------------------------------



11.在Client端查看路由表信息,查看Push的路由
----------------------------------------------------------------------------------
Microsoft Windows [Version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.

C:\Documents and Settings\Administrator>route PRINT
IPv4 Route Table
===========================================================================
Interface List
0x1 ........................... MS TCP Loopback interface
0x2 ...00 ff 6f cb 56 cf ...... TAP-Win32 Adapter V8
0x10004 ...00 18 8b 82 52 1a ...... Broadcom 440x 10/100 Integrated Controller
0x10005 ...00 e0 4c b3 f3 43 ...... ADMtek AN983 10/100 PCI Adapter
===========================================================================
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface Metric
          0.0.0.0          0.0.0.0      192.168.0.1    192.168.0.197     20
         10.99.0.0    255.255.255.0      10.99.0.17       10.99.0.18      1
        10.99.0.16 255.255.255.252      10.99.0.18       10.99.0.18     30
        10.99.0.18 255.255.255.255       127.0.0.1        127.0.0.1     30
   10.255.255.255 255.255.255.255       10.99.0.18       10.99.0.18     30
        127.0.0.0        255.0.0.0        127.0.0.1        127.0.0.1      1
      192.168.0.0    255.255.255.0    192.168.0.197    192.168.0.197     20
    192.168.0.197 255.255.255.255        127.0.0.1        127.0.0.1     20
    192.168.0.255 255.255.255.255    192.168.0.197    192.168.0.197     20
      192.168.1.0    255.255.255.0    192.168.1.102    192.168.1.102     20
    192.168.1.102 255.255.255.255        127.0.0.1        127.0.0.1     20
    192.168.1.255 255.255.255.255    192.168.1.102    192.168.1.102     20
    111.111.111.0    255.255.255.0       10.99.0.17       10.99.0.18      1
      222.222.0.0      255.255.0.0       10.99.0.17       10.99.0.18      1
123.123.123.234 255.255.255.255       10.99.0.17       10.99.0.18      1
        224.0.0.0        240.0.0.0       10.99.0.18       10.99.0.18     30
        224.0.0.0        240.0.0.0    192.168.0.197    192.168.0.197     20
        224.0.0.0        240.0.0.0    192.168.1.102    192.168.1.102     20
255.255.255.255 255.255.255.255       10.99.0.18       10.99.0.18      1
255.255.255.255 255.255.255.255    192.168.0.197    192.168.0.197      1
255.255.255.255 255.255.255.255    192.168.1.102    192.168.1.102      1
Default Gateway:     192.168.0.1

from http://hi.baidu.com/anthem620/blog/item/22914127ff6a3800908f9d25.html