架設基於FreeRADIUS帶有認證計費功能的Open***

1、文檔預習
mysql

    在查看下面博文以前,請你們先預覽一下我前幾篇關於Open***的部署,一遍與你們更好的瞭解:linux

    搭建基於證書認證登陸的Open***服務器ios

    搭建基於用戶密碼認證的Open***sql

    使用MySQL驗證Open***用戶登陸訪問數據庫

2、FreeRadius簡介安全

    RADIUS認證服務器(Remote Authentication Dial In User Service,遠程用戶撥號認證系統)是目前應用最普遍的AAA協議(AAA=authentication、Authorization、Accounting,即認證、受權、計費)。隨着網絡安全需求提升,中小企業的局域網集中用戶認證,特別是使用VPDN專網的也逐漸須要創建本身的認證服務器以管理撥號用戶。但這些用戶不須要使用昂貴的專業系統,採用PC服務器和Linux系統的FreeRADIUS+MySQL就能可靠地實現。bash

    FreeRADIUS包含一個radius服務器和radius-client,能夠對支持radius協議的網絡設備進行鑑權記帳,常見的開源路由器操做系統:如Openwrt,DD-wrt等,都支持radius協議,對PPPOE,熱點,***等服務器進行帳戶管理認證,記帳。服務器

    Open***及FreeRADIUS 已經開發出了現成的Open***+FreeRADIUS 協同工做的插件。open***官方插件是PAM-Radius,FreeRADIUS官方插件是radiusplugin,這二者不一樣的之處是,PAM-Radius僅有認證功能,不能向FreeRADIUS服務器發送計費報文,這裏我使用的是掛載FreeRadius 官方的radiusplugin模塊,由於此模塊能夠向FreeRADIUS服務器發送計費報文,從而實現計費功能。網絡

wKiom1agp_OhrJFfAAGklOUyQEY991.png

3、安裝FreeRadiussession

一、方案介紹

    因爲歷史的遺留問題,將Open***、FreeRADIUS及MySQL部署在了同一臺機器上,可是若是是在新的環境中部署,在硬件容許的狀況下應該將它們部署在不一樣的機器上。daloRADIUS是一個用PHP語言寫的Web應用程序,將其部署到一臺有PHP環境的Apache的服務器上就行。

    Open***、MySQL以及的安裝我這裏再也不介紹,你們能夠看我前面的博文,安裝也很是簡單。

一、環境準備

#關閉防火牆
service iptables stop
#關閉SELinux
setenforce 0
#開啓路由轉發
sysctl -w net.ipv4.ip_forward=1
#添加策略
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

二、安裝FreeRadius以及數據庫組件

    FreeRadius如今最新的版本是3.0.1,若是要使用最新的版本那須要編譯安裝,我這裏爲了簡便,使用安裝光盤編譯好的rpm包,版本是2.2.6。

yum install freeradius freeradius-mysql -y

   咱們能夠經過"rpm -ql freeradius"看到安裝了好的文件。

三、安裝RadiusPlugin

   RadiusPlugin插件的下載地址:http://www.nongnu.org/radiusplugin/。

#下載插件
wget http://www.nongnu.org/radiusplugin/radiusplugin_v2.1a_beta1.tar.gz
#解壓縮
tar xf radiusplugin_v2.1a_beta1.tar.gz 
cd radiusplugin_v2.1a_beta1
#解決依賴問題
yum install libgcrypt-devel -y
#安裝很是簡單,直接在目錄下make
make
#把編譯好的模塊拷貝到
cp radiusplugin.so /usr/lib64/open***/plugin/lib/
#拷貝相應的配置文件
cp radiusplugin.cnf /etc/open***/

4、配置FreeRADIUS與MySQL的鏈接

   對於FreeRADIUS服務器來講,根據須要,要對3個配置文件進行修改,其中一個是FreeRADIUS服務器的主配置文件radiusd.conf,一個是認證配置文件default,還有一個是與SQL數據庫相關的配置文件sql.conf。

wKioL1agpX_zQBfEAADATgC0dHQ730.png

一、配置數據庫

   首先配置一下數據庫,數據庫的sql腳本是freeradius-mysql軟件包帶的,因此須要安裝它。

mysql> create database radius;
mysql> grant all privileges on radius.* to radius@"localhost" identified by "radpass";
mysql> grant all privileges on radius.* to radius@"127.0.0.1" identified by "radpass";
mysql> use radius;
mysql> source /etc/raddb/sql/mysql/schema.sql
mysql> source /etc/raddb/sql/mysql/nas.sql
mysql> show tables;
+------------------+
| Tables_in_radius |
+------------------+
| nas              |
| radacct          |
| radcheck         |
| radgroupcheck    |
| radgroupreply    |
| radpostauth      |
| radreply         |
| radusergroup     |
+------------------+
# 插入測試數據,用於後面認證
mysql> insert into radcheck (username,attribute,op,value) values ('test','Cleartext-Password',':=','test');
8 rows in set (0.00 sec)

    主數據庫各字段含義:

  • radcheck 用戶檢查信息表

  • radreply 用戶回覆信息表

  • radgroupcheck 用戶組檢查信息表

  • radgroupreply 用戶組檢查信息表

  • radusergroup 用戶和組關係表

  • radacct 計費狀況表

  • radpostauth 認證後處理信息,能夠包括認證請求成功和拒絕的記錄。

  • nas 網絡設備表

    擴展功能表,能夠按需導入ippool.sql、wimax.sql wimax、cui.sql cui詳細的表定義。

二、配置主配置文件radiusd.conf

    這個配置文件比較簡單,在module模塊裏面把sql.conf和counter.conf前面的註釋去掉便可,用來啓用數據庫鏈接查詢等功能:

        #  As of 2.0.5, most of the module configurations are in a
        #  sub-directory.  Files matching the regex /[a-zA-Z0-9_.]+/
        #  are loaded.  The modules are initialized ONLY if they are
        #  referenced in a processing section, such as authorize,
        #  authenticate, accounting, pre/post-proxy, etc.
        #
        $INCLUDE ${confdir}/modules/
        #  Extensible Authentication Protocol
        #
        #  For all EAP related authentications.
        #  Now in another file, because it is very large.
        #
        $INCLUDE eap.conf
        #  Include another file that has the SQL-related configuration.
        #  This is another file only because it tends to be big.
        #
        $INCLUDE sql.conf             #打開這裏的註釋
        
        #   
        #  This module is an SQL enabled version of the counter module.
        #   
        #  Rather than maintaining seperate (GDBM) databases of
        #  accounting info for each counter, this module uses the data
        #  stored in the raddacct table by the sql modules. This
        #  module NEVER does any database INSERTs or UPDATEs.  It is
        #  totally dependent on the SQL module to process Accounting
        #  packets.
        #   
        $INCLUDE sql/mysql/counter.conf      #打開這裏的註釋

        #

三、配置radius認證文件

    修改/etc/raddb/site_enabled下的defoult文件,默認是使用的文件認證,如今取消文件認證,改成sql認證,差很少少有file的註釋掉,sql的啓用,下面是個人模板。

[root@ios*** ~]# grep -Ev "^#|[:spcae:]*#|^$" /etc/raddb/sites-enabled/default 
authorize {
        preprocess
        chap
        mschap
        digest
        suffix
        eap {
                ok = return
        }
        sql                #去掉註釋,開啓sql
        expiration
        logintime
        pap
}
authenticate {
        Auth-Type PAP {
                pap
        }
        Auth-Type CHAP {
                chap
        }
        Auth-Type MS-CHAP {
                mschap
        }
        digest
        unix
        eap
}
preacct {
        preprocess
        acct_unique
        suffix
#       files
}
accounting {
        detail
        unix
        radutmp
        sql                 #去掉註釋
        exec
        attr_filter.accounting_response
}
session {
        radutmp
        sql
}
post-auth {
        sql
        exec
        Post-Auth-Type REJECT {
                attr_filter.access_reject
        }
}
pre-proxy {
}
post-proxy {
        eap
}

四、配置與mysql數據庫鏈接的配置文件sql.conf

    其實按照上面的參數設定的話,這個文件不須要修改的,文件內容以下:

[root@ios*** ~]# grep -Ev "^#|[:spcae:]*#|^$" /etc/raddb/sql.conf
sql {
        database = "mysql"
        driver = "rlm_sql_${database}"
        server = "localhost"
        login = "radius"
        password = "radpass"
        radius_db = "radius"
        acct_table1 = "radacct"
        acct_table2 = "radacct"
        postauth_table = "radpostauth"
        authcheck_table = "radcheck"
        authreply_table = "radreply"
        groupcheck_table = "radgroupcheck"
        groupreply_table = "radgroupreply"
        usergroup_table = "radusergroup"
        deletestalesessions = yes
        sqltrace = no
        sqltracefile = ${logdir}/sqltrace.sql
        num_sql_socks = ${thread[pool].max_servers}
        connect_failure_retry_delay = 60
        lifetime = 0
        max_queries = 0
        nas_table = "nas"
        $INCLUDE sql/${database}/dialup.conf
}

五、測試FreeRADIUS和MySQL的通訊認證

    配置到這裏咱們簡單測試一下FreeRADIUS的認證是否能夠了。須要命令radtest,若是沒有這個命令,咱們能夠經過安裝軟件包來添加。

yum install freeradius-utils -y
# 啓動freeradius,可使用radiusd -X開啓Debug模式
service radiusd start

    用radtest測試test用戶,密碼是test。

[root@ios*** ~]# radtest test test localhost 0 testing123
Sending Access-Request of id 239 to 127.0.0.1 port 1812
        User-Name = "test"
        User-Password = "test"
        NAS-IP-Address = 127.0.0.1
        NAS-Port = 0
        Message-Authenticator = 0x00000000000000000000000000000000
rad_recv: Access-Accept packet from host 127.0.0.1 port 1812, id=239, length=20

    服務器若是返回有「Access-Accept」的字樣,表明認證成功了,若是有「Access-Reject」字樣則認證失敗,這時能夠停用radiusd服務,用命令radiusd -X開啓服務從所有的debug信息中找到問題並解決,若是NAS-IP-Address顯示的不是127.0.0.1那說明你沒有配置主機名解析,請在hosts裏面配置一下。

5、Open***經過插件鏈接FreeRADIUS進行認證

wKioL1agp0TT6HvYAADyBLMhmVg020.png

一、配置與客戶端通訊配置文件clients.conf

    這個文件是FreeRADIUS與客戶端(RadiusPlugin)通訊使用的配置文件,默認文件只需添加shorname = localhost項,通訊密鑰能夠默認。

[root@ios*** ~]# grep -Ev "^#|[:spcae:]*#|^$" /etc/raddb/clients.conf 
client localhost {
        ipaddr = 127.0.0.1
        secret          = testing123    #此參數與後面的radiusplugin.cnf內參數需一致
        require_message_authenticator = no
        shortname       = localhost
}

二、配置radiusplugin.cnf

    此文件是用來關聯Open***和FreeRADIUS的,前面FreeRADIUS和MySQL的測試已經成功了,下面配置好此文件以後就能夠測試***的認證了,此文件須要修改的地方很少,我這裏的配置以下:

[root@ios*** ~]# grep -Ev "^#|^$" /etc/open***/radiusplugin.cnf 
NAS-Identifier=Open***
Service-Type=5
Framed-Protocol=1
NAS-Port-Type=5
NAS-IP-Address=127.0.0.1
Open***Config=/etc/open***/server.conf
subnet=255.255.255.0
overwriteccfiles=true
nonfatalaccounting=false
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      #和FreeRadius的客戶端配置文件保持一致
}

三、Open***服務端配置文件

    經過plugin字段讓Open***調用RadiusPlugin插件。

[root@ios*** ~]# grep -Ev "^#|^$|^;" /etc/open***/server.conf 
management 127.0.0.1 5800     #開啓管理接口,不須要能夠註釋掉
port 1194
proto tcp
dev tun
ca /usr/share/doc/open***-2.3.9/sample/sample-keys/ca.crt  #注意證書的路徑,不一樣版本不同
cert /usr/share/doc/open***-2.3.9/sample/sample-keys/server.crt
key /usr/share/doc/open***-2.3.9/sample/sample-keys/server.key  
dh /usr/share/doc/open***-2.3.9/sample/sample-keys/dh2048.pem
topology subnet          #開啓節省IP,具體爲何查看個人Open***安裝文檔
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "route 10.0.0.0 255.0.0.0"
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 114.114.114.114"
client-to-client
duplicate-cn
keepalive 10 120
comp-lzo
persist-key
persist-tun
status /var/log/open***/open***-status.log
log-append /var/log/open***/open***.log
verb 3
script-security 3 system
plugin /usr/lib64/open***/plugin/lib/radiusplugin.so /etc/open***/radiusplugin.cnf
client-cert-not-required
username-as-common-name

四、Open***客戶端配置文件

    我這裏是Windows的客戶端,配置文件以下:

client
dev tun
proto tcp
remote 211.152.x.x 1194         #Open***服務器的IP地址
nobind
persist-key
persist-tun
ca ca.crt
;cert client.crt
;key client.key
comp-lzo
verb 3
auth-user-pass             #客戶端使用帳號密碼登陸
reneg-sec 360000

五、***鏈接測試

    重啓Open*** 服務端後客戶端就能夠用「用戶名/密碼」的形式登陸***了,若是要添加新的用戶只須要在radius數據庫的radcheck表插入新的記錄便可,非常方便快捷。

    在/var/log/radius和/var/log/open***下面有不少日誌是記錄鏈接狀態的,若是鏈接有問題能夠查看日誌查找緣由。

    下面是個人Open***的日誌,若是你的FreeRADIUS開啓Debug模式的話也能夠看到登陸成功的信息。

wKiom1ae_tyhu_AnAAGvyV-ZatI720.png

    到此爲止,Open***用戶已經成功經過了FreeRADIUS驗證,並且也成功登陸了Open***服務器,所以經過FreeRADIUS驗證Open***用戶登陸Open***服務器也便所有完成了。

6、瞭解Open***的使用狀況

   要了解Open***的使用狀況,可使用FreeRADIUS服務器提供的命令,例如radwatch、radwho、radlast等等,這些命令是freeradius和freeradius-utils軟件包帶來的。

wKioL1afAx3CvCEiAAA6RDKQMoE813.png

7、使用daloRADIUS Web程序管理FreeRADIUS服務

   由於篇幅的緣由,關於Web管理的部署配置請點擊查看下面的文章。

     

        請點擊鏈接查看WEB管理詳細部署



                    wKioL1jd79uBgnwTAAB3a83FKoY233.jpg

相關文章
相關標籤/搜索