freeradius+xl2tp+mysql整合


freeradius+xl2tp+mysql整合

搭了5個小時,可以說是入門到精通了。
首先請確認你已經搭建好L2TP,並可以正常使用。  如何在Ubuntu下配置L2TP VPN


 

 

L2TP使用radiusclient連接freeradius進行認證,freeradius有兩種認證方式,一種基於文本,一種基於mysql數據庫。這里使用mysql數據庫進行數據查詢連接。

freeradius在mysql中查詢到用戶數據,認證成功后即可確認連接。


 一、首先安裝軟件

apt-get install mysql freeradius freeradius-mysql freeradius-utils

下載freeradius-client-1.1.6並安裝

wget -c ftp://ftp.freeradius.org/pub/freeradius/freeradius-client-1.1.6.tar.gz 
tar -zxf freeradius-client-1.1.6.tar.gz 
cd freeradius-client-1.1.6 
./configure 
make && make install

freeradius-client安裝之后的配置文件目錄為/usr/local/etc/radiusclient/
freeradius安裝之后的配置文件目錄為/etc/freeradius

   二、l2tp啟用radius認證功能

vim /etc/ppp/options.xl2tpd
新增最后兩行啟用radius認證功能,反之注釋掉即可不啟用
plugin /usr/lib/pppd/2.4.5/radius.so
radius-config-file /usr/local/etc/radiusclient/radiusclient.conf

 基本文本數據的本地測試(選做)

測試是否安裝成功,如果不需要與mysql集成,那么就已安裝完成。

vim /etc/freeradius/users
查找 steve Cleartext-Password := "testing"76-84行), 取消該段內容的注釋。

#大寫X,意思是以debug模式運行。freeradiux -X 啟動測試服務器,這時會占住一個終端,ctrl+c關閉。

freeradius -X 

#新開一個窗口執行,看到"Access-Accept packet"就成功了。

radtest steve testing localhost 18120 testing123
#steve是用戶名,testing是用戶password,localhost是freeradius服務器地址,18120是localhost地址freeradius的默認端口號,testing123是radiusclient和freeradius的通信密鑰

看到Access-Accept packet這一步很重要,只有看到了才能正常使用。注意查看freeradius -X提示的地址和端口號。

有可能遇到以下問題:

1) 服務器測試窗口出現:Listen to the unknown client xxx.xxx.xx.xxx(本機外網IP)....

 這時需要對/etc/freeradius/client.conf文件進行相關配置,修改客戶端信息,將freeRadius服務器IP寫入

vim /etc/freeradius/client.conf
client crayon-vpn {
  ipaddr =  xxx.xxx.xx.xxx
  secret = testing123

}

並在 /etc/hosts文件中配置 IP和主機名:(據說127.0.0.1 最后那個server也要加,不知有沒有用)

2) freeradius -X時出現以下錯誤

Failed binding to authentication address *port 1812:Address already in use

用命令lsof -i:1812查看占用進程,一般就是freeradius本身,kill -9 PID強制終止該進程,或者/etc/init.d/freeradius stop,然后重新運行命令freeradius -X便可以運行服務程序了

 

freeradius

啟動、重啟、停止freeradius服務的命令:

/etc/init.d/freeradius stop
/etc/init.d/freeradius start
/etc/init.d/freeradius restart

freeradius是一個Server端的軟件,用於做認證服務,包含基礎Client端,但這里使用radiusclient(以下);

freeradius可以和mysql整合,加快用戶數據讀取,以及后續的用戶數據更新等操作。

 三、freeradius和mysql集成

3.1、創建radius數據庫

 mysqladmin -uroot -p123456 CREATE radius; #123456是你mysql的root密碼。

3.2、導入到radius數據庫 

注意:admin.sql包含了帳號和密碼,切記要修改。

cd /etc/freeradius/sql/mysql
mysql -uroot -p123456 < admin.SQL
mysql -uroot -p123456 radius < ippool.SQL
mysql -uroot -p123456 radius < schema.SQL
mysql -uroot -p123456 radius < wimax.SQL
mysql -uroot -p123456 radius < cui.SQL
mysql -uroot -p123456 radius < nas.SQL

3.3、設定數據庫帳號和密碼 

默認的帳號是radius,密碼是radpass

vim /usr/local/etc/freeradius/sql.conf

3.4、啟用nas表查詢 

省略了clients.conf,可以從數據庫的nas表讀取client。

sed -i 's/\#readclients/readclients/g' /usr/local/etc/freeradius/sql.conf

3.5、打開sql數據庫支持

3.5.1 修改主配置文件radiusd.conf

vim /etc/freeradius/radiusd.conf #查找"sql.conf”(683行),去掉#號

3.5.2 修改sites-enabled目錄配置文件 

vim /etc/freeradius/sites-enabled/default
找到authorize {}模塊,注釋掉files(152行),去掉sql前的#號(159行)。 
找到preacct {}模塊,注釋掉files(354行)。 
找到accounting {}模塊,注釋掉radutmp(378行),去掉sql前面的#號(388行)。 
找到session {}模塊,注釋掉radutmp(432行),去掉sql前面的#號(436行)。 
找到post-auth {}模塊,去掉sql前的#號(457行),去掉sql前的#號(545行)。

vim /usr/local/etc/freeradius/sites-enabled/inner-tunnel
找到authorize {}模塊,注釋掉files(124行),去掉sql前的#號(131行)。 
找到session {}模塊,注釋掉radutmp(251行),去掉sql前面的#號(255行)。 
找到post-auth {}模塊,去掉sql前的#號(277行),去掉sql前的#號(301行)。

3.5.3 打開在線人數查詢支持 

查找simul_count_query將279-282行注釋去掉

vim /usr/local/etc/freeradius/sql/mysql/dialup.conf

四、radiusclient

  radiusclient是一個Client端的軟件,連接VPN時就是連接到radiusclient然后再連接freeradius服務器

1、執行以下命令,不然在連接時會報錯,親測在/var/log/syslog日志中排查確認確實如此

sed -i 's/radius_deadtime/\#radius_deadtime/g' /usr/local/etc/radiusclient/radiusclient.conf
sed -i 's/bindaddr/\#bindaddr/g' /usr/local/etc/radiusclient/radiusclient.conf

2、增加一些字典文件,不然Windows客戶端連接出錯

cat >>/usr/local/etc/radiusclient/dictionary<<EOF
INCLUDE /usr/local/etc/radiusclient/dictionary.sip
INCLUDE /usr/local/etc/radiusclient/dictionary.ascend
INCLUDE /usr/local/etc/radiusclient/dictionary.merit
INCLUDE /usr/local/etc/radiusclient/dictionary.compat
INCLUDE /usr/local/etc/radiusclient/dictionary.microsoft
EOF

3、文件dictionary.microsoft好像不存在,可以自己新建一個,源碼如下

vim /usr/local/etc/radiusclient/dictionary.microsoft
# -*- text -*-
#
#    Microsoft's VSA's, from RFC 2548
#
#    $Id: dictionary.microsoft,v 1.8 2005/08/08 22:23:37 aland Exp $
#

VENDOR        Microsoft            311

ATTRIBUTE    MS-CHAP-Response            1    string    Microsoft
ATTRIBUTE    MS-CHAP-Error                2    string    Microsoft
ATTRIBUTE    MS-CHAP-CPW-1                3    string    Microsoft
ATTRIBUTE    MS-CHAP-CPW-2                4    string    Microsoft
ATTRIBUTE    MS-CHAP-LM-Enc-PW            5    string    Microsoft
ATTRIBUTE    MS-CHAP-NT-Enc-PW            6    string    Microsoft
ATTRIBUTE    MS-MPPE-Encryption-Policy        7    string    Microsoft
# This is referred to as both singular and plural in the RFC.
# Plural seems to make more sense.
ATTRIBUTE    MS-MPPE-Encryption-Type            8    string    Microsoft
ATTRIBUTE    MS-MPPE-Encryption-Types        8    string    Microsoft
ATTRIBUTE    MS-RAS-Vendor                9    integer    Microsoft
ATTRIBUTE    MS-CHAP-Domain                10    string    Microsoft
ATTRIBUTE    MS-CHAP-Challenge            11    string    Microsoft
ATTRIBUTE    MS-CHAP-MPPE-Keys            12    string    Microsoft
ATTRIBUTE    MS-BAP-Usage                13    integer    Microsoft
ATTRIBUTE    MS-Link-Utilization-Threshold        14    integer    Microsoft
ATTRIBUTE    MS-Link-Drop-Time-Limit            15    integer    Microsoft
ATTRIBUTE    MS-MPPE-Send-Key            16    string    Microsoft
ATTRIBUTE    MS-MPPE-Recv-Key            17    string    Microsoft
ATTRIBUTE    MS-RAS-Version                18    string    Microsoft
ATTRIBUTE    MS-Old-ARAP-Password            19    string    Microsoft
ATTRIBUTE    MS-New-ARAP-Password            20    string    Microsoft
ATTRIBUTE    MS-ARAP-PW-Change-Reason        21    integer    Microsoft

ATTRIBUTE    MS-Filter                22    string    Microsoft
ATTRIBUTE    MS-Acct-Auth-Type            23    integer    Microsoft
ATTRIBUTE    MS-Acct-EAP-Type            24    integer    Microsoft

ATTRIBUTE    MS-CHAP2-Response            25    string    Microsoft
ATTRIBUTE    MS-CHAP2-Success            26    string    Microsoft
ATTRIBUTE    MS-CHAP2-CPW                27    string    Microsoft

ATTRIBUTE    MS-Primary-DNS-Server            28    ipaddr    Microsoft
ATTRIBUTE    MS-Secondary-DNS-Server            29    ipaddr    Microsoft
ATTRIBUTE    MS-Primary-NBNS-Server            30    ipaddr    Microsoft
ATTRIBUTE    MS-Secondary-NBNS-Server        31    ipaddr    Microsoft

#ATTRIBUTE    MS-ARAP-Challenge    33    octets

#
#    Integer Translations
#

#    MS-BAP-Usage Values

VALUE    MS-BAP-Usage            Not-Allowed        0
VALUE    MS-BAP-Usage            Allowed            1
VALUE    MS-BAP-Usage            Required        2

#    MS-ARAP-Password-Change-Reason Values

VALUE    MS-ARAP-PW-Change-Reason    Just-Change-Password    1
VALUE    MS-ARAP-PW-Change-Reason    Expired-Password    2
VALUE    MS-ARAP-PW-Change-Reason    Admin-Requires-Password-Change 3
VALUE    MS-ARAP-PW-Change-Reason    Password-Too-Short    4

#    MS-Acct-Auth-Type Values

VALUE    MS-Acct-Auth-Type        PAP            1
VALUE    MS-Acct-Auth-Type        CHAP            2
VALUE    MS-Acct-Auth-Type        MS-CHAP-1        3
VALUE    MS-Acct-Auth-Type        MS-CHAP-2        4
VALUE    MS-Acct-Auth-Type        EAP            5

#    MS-Acct-EAP-Type Values

VALUE    MS-Acct-EAP-Type        MD5            4
VALUE    MS-Acct-EAP-Type        OTP            5
VALUE    MS-Acct-EAP-Type        Generic-Token-Card    6
VALUE    MS-Acct-EAP-Type        TLS            13
dictionary.microsoft

這個網站上有https://github.com/ates/radius/blob/master/priv/dictionary.microsoft

 4、注釋掉dictionary文件里ipv6相關的行

sed -i '/ipv6/s/^/#/' /usr/local/etc/radiusclient/dictionary

5、修改servers文件

vim /usr/local/etc/radiusclient/servers
在最后新增一行
SERVER_IP         testing123
其中SERVER_IP 是你的服務器IP
testing123是radiusclient和freeradius通信密鑰

 6、在radiusclient.conf文件中配置freeradius相關信息

vim /usr/local/etc/radiusclient/radiusclient.conf
指定freeRadius服務器的IP,將SERVER_IP改為你的服務器IP地址:
authserver      SERVER_IP
acctserver      SERVER_IP
指定連接到服務器的配置文件目錄:
servers         /usr/local/etc/radiusclient/servers

 五、用戶權限管理

# 連接mysql使用radius數據庫 

mysql -uroot -p123456
mysql> USE radius;

# 添加用戶demo,密碼為demo並且加入"user"用戶組,注意是在radcheck、radusergroup表。 

mysql> INSERT INTO radcheck (username,attribute,op,VALUE) VALUES ('demo','Password',':=','demo');
mysql> INSERT INTO radusergroup (username,groupname) VALUES ('demo','user');

# 限制同時登陸次數,注意是在radgroupcheck表 

mysql> INSERT INTO radgroupcheck (groupname,attribute,op,VALUE) VALUES ('normal','Simultaneous-Use',':=','1');

# 測試是否生效 

freeradiusd -X
# 注意查看這里出現的IP地址有沒有你的公網IP,以及記住其所使用的端口1812
# radtest USERNAME PASSWORD SERVER_IP PORT radtest和freeradius的通信密鑰(默認是testing123)
radtest demo demo 公網IP 1812 testing123

如果出現ACCESS字樣就是說兩者通信成功了

重啟下freeradius服務后就可以連接了

/etc/init.d/freeradius restart

六、查看日志以及排錯

排錯是很關鍵的一步,因為很可能不能一步到位完美登錄。

日志:

  /var/log/syslog  這個日志記錄登錄時的請求,以及出錯的原因,很多時候都是配置文件的問題

  /var/log/auth.log  這個日志應該有所幫助

  /var/log/freeradius/radius.log  這個文件是freeradius的日志

 

參考博文:

  1、VPN流量控制技術:PPTP/L2TP + FreeRADIUS + MySQL 安裝與配置  作者:騎着老虎闖天涯

  2、Linux平台下搭建freeRADIUS集成MySQL並整合***  作者:Dhfijk


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM