#!/bin/bash
SYS_VERSION=`rpm -q --queryformat '%{VERSION}' centos-release`
#
# freeradius daemon installation and configuration script
#

# cleaning up previous installation
yum -y erase freeradius radiusclient-ng radiusclient-ng-utils
rm -Rf /etc/{radiusclient,radiusclient-ng,raddb}			

# installing freeradius
if [ $SYS_VERSION -eq 6 ]; then
 FREERADIUS_VERSION=2.1.12-4
 rpm -ivh freeradius-$FREERADIUS_VERSION.el6_3.src.rpm
 yum -y install gdbm-devel libtool libtool-ltdl-devel net-snmp-devel net-snmp-utils readline-devel python-devel mysql-devel postgresql-devel unixODBC-devel openldap-devel
 PERL_MM_USE_DEFAULT=1 perl -MCPAN -e "install ExtUtils::Embed";
 /bin/cp freeradius.spec /root/rpmbuild/SPECS/
 rpmbuild -bb /root/rpmbuild/SPECS/freeradius.spec
 (cd /root/rpmbuild/RPMS/`uname -i`; rpm -ivh freeradius-$FREERADIUS_VERSION*.rpm freeradius-perl-$FREERADIUS_VERSION*.rpm)

# installing radiusclient
 sed -i /etc/yum.conf -e 's/^\(exclude=.*\)/\1 freeradius-client-utils\*/g'
 yum -y install radiusclient-ng radiusclient-ng-utils

# configuring radiusclient to make it work with pppd
 mv /etc/radiusclient-ng /etc/radiusclient
 /bin/cp ./client/* /etc/radiusclient
 ln -s /usr/share/radiusclient-ng/dictionary /etc/radiusclient/dictionary
 echo "INCLUDE /etc/radiusclient/dictionary.microsoft" >> /etc/radiusclient/dictionary

# configuring freeradius
 /bin/cp ./module-perl /etc/raddb/modules/perl
 /bin/cp ./nvpn_rlm.pl /etc/raddb/
 /bin/cp ./site-default /etc/raddb/sites-available/default
cat > /etc/raddb/clients.conf <<EOF
client localhost {
        ipaddr = 127.0.0.1
        secret          = yoursecrethere
        require_message_authenticator = no
        nastype     = other
}
EOF

 test -d /etc/monit.d && /bin/cp ./monit-freeradius /etc/monit.d

 service radiusd restart
 chkconfig radiusd on
elif [ $SYS_VERSION -eq 7 ]; then
  FREERADIUS_VERSION=3.0.13-6
  rpm -ivh freeradius-$FREERADIUS_VERSION.el7.src.rpm
  yum -y install gdbm-devel libtool libtool-ltdl-devel net-snmp-devel net-snmp-utils readline-devel python-devel mysql-devel postgresql-devel unixODBC-devel openldap-devel libtalloc-devel tncfhh-devel sqlite-devel
  /usr/local/bin/cpanm -n ExtUtils::Embed
  rpmbuild -bb /root/rpmbuild/SPECS/freeradius.spec
  (cd /root/rpmbuild/RPMS/`uname -i`; rpm -ivh freeradius-$FREERADIUS_VERSION*.rpm freeradius-perl-$FREERADIUS_VERSION*.rpm)
# installing radiusclient
  sed -i /etc/yum.conf -e 's/^\(exclude=.*\)/\1 freeradius-client-utils\*/g'
  yum -y install radiusclient-ng radiusclient-ng-utils
# configuring radiusclient to make it work with pppd
  mv /etc/radiusclient-ng /etc/radiusclient
  /bin/cp ./client/* /etc/radiusclient
  ln -s /usr/share/radiusclient-ng/dictionary /etc/radiusclient/dictionary
  echo "INCLUDE /etc/radiusclient/dictionary.microsoft" >> /etc/radiusclient/dictionary
# configuring freeradius
  /bin/cp ./module-perl /etc/raddb/mods-enabled/perl
  sed -i 's/module/filename/g' /etc/raddb/mods-enabled/perl
  sed -i 's/\${confdir}/\/etc\/raddb/g' /etc/raddb/mods-enabled/perl
  /bin/cp ./nvpn_rlm.pl /etc/raddb/
  /bin/cp ./site-default-c7 /etc/raddb/sites-available/default
  /bin/cp -f ./ippool /etc/raddb/mods-enabled/
  /bin/rm -f /etc/raddb/sites-enabled/inner-tunnel
  sed -i '174s/^/DEFAULT    Pool-Name := main_pool/' /etc/raddb/mods-config/files/authorize
#  sed -i '175s/^/DEFAULT    NAS-Port = 1, Pool-Name := first_pool/' /etc/raddb/mods-config/files/authorize
#  sed -i '176s/^/DEFAULT    NAS-Port = 2, Pool-Name := second_pool/' /etc/raddb/mods-config/files/authorize
cat > /etc/raddb/clients.conf <<EOF
client localhost {
        ipaddr = 127.0.0.1
        secret          = yoursecrethere
        require_message_authenticator = no
        nastype     = other
}
EOF

 test -d /etc/monit.d && /bin/cp ./monit-freeradius-c7 /etc/monit.d/monit-freeradius
 systemctl daemon-reload
 systemctl enable radiusd
 systemctl start radiusd
fi
