#! /bin/sh
# Author: Ričardas Stoma
# Company: Kolmisoft
# Year: 2014
# About: Recompile M2 module without stopping calls
. /usr/src/m2/framework/bash_functions.sh
report "Script disabled" 3
exit
###########################################
######## GLOBAL VARIABLES ########
###########################################
# path to freeradius config
RADIUS_CONF=/usr/local/etc/raddb/radiusd.conf
# path to M2 RADIUS source files
M2_RADIUS_SOURCE=/usr/src/m2_core/freeradius/rlm_m2
# path to M2 Registrations source files
M2_REGISTRATIONS_SOURCE=/usr/src/m2_core/freeswitch/mod_m2_registrations
# path to M2 FS source files
M2_FS_SOURCE=/usr/src/m2_core/freeswitch/mod_xml_m2_radius
# path to freeswitch m2 radius config
FS_M2_CONF=/usr/local/freeswitch/conf/autoload_configs/xml_m2_radius.conf.xml
# path to freeswitch modules configuration
FS_MODULES_CONF=/usr/local/freeswitch/conf/autoload_configs/modules.conf.xml
# authentication port for temporary radius
RADIUS_AUTH_PORT=
# accounting port for temporary radius
RADIUS_ACCT_PORT=
# new authentication port for temporary radius
NEW_RADIUS_AUTH_PORT=
# new accounting port for temporary radius
NEW_RADIUS_ACCT_PORT=
# current version of m2 core defined in source files (will be set later)
CURRENT_CORE=
# installed m2 core version (will be set later)
INSTALLED_CORE=
# show debug messages
DEBUG=0
# fix radius ports
FIX=0
# clean fs config
CLEAN=0
CLEAN_EXTERNAL=0
# where to display debug output? default - do not display
DEBUG_OUTPUT=/dev/null
# patch to source files
SOURCE_FILES_PATH=/usr/src/m2_core
# database
DB_PASSWORD=`cat /etc/m2/system.conf | grep dbsecret | awk '{print $3}'`
DB_USERNAME=`cat /etc/m2/system.conf | grep dbuser | awk '{print $3}'`
DB_NAME=`cat /etc/m2/system.conf | grep dbname | awk '{print $3}'`
DB_HOST=`cat /etc/m2/system.conf | grep dbhost | awk '{print $3}'`
if [[ -e "/var/www/.ssh/id_rsa" ]]; then
SSH_KEY="/var/www/.ssh/id_rsa"
else
SSH_KEY="/root/.ssh/id_rsa"
fi
###########################################
######## FUNCTIONS ########
###########################################
# Recompile Freeswitch related script
recompile_fs_scripts() {
# get all active FS servers
mysql -h $DB_HOST $DB_NAME -u $DB_USERNAME -p$DB_PASSWORD -e "SELECT server_ip, ssh_username, ssh_port FROM servers WHERE server_type = 'freeswitch' AND active = 1" > /tmp/m2_servers.txt
while read -r line
do
local SERVER_IP=`echo "$line" | awk '{print $1}'`
local SSH_USERNAME=`echo "$line" | awk '{print $2}'`
local SSH_PORT=`echo "$line" | awk '{print $3}'`
if [ "$SERVER_IP" != "server_ip" ]; then
local LOCAL_SERVER=1
local CHECK_SERVER=`ifconfig | grep -F "$SERVER_IP" | wc -l`
if [ "$CHECK_SERVER" == "0" ]; then
LOCAL_SERVER=0
fi
if [ "$LOCAL_SERVER" == "1" ]; then
svn update /usr/src/m2 &> $DEBUG_OUTPUT
/usr/src/m2/scripts/scripts_install.sh LATEST
/usr/src/m2/core/m2_core_radius_configure.sh NO_RESTART
else
report "Recompiling scripts on FS server $SERVER_IP" 3
ssh -i $SSH_KEY -n $SSH_USERNAME@$SERVER_IP -p $SSH_PORT "svn update /usr/src/m2 &> /dev/null && /usr/src/m2/scripts/scripts_install.sh LATEST && /usr/local/m2/m2_freeswitch_devices" &> $DEBUG_OUTPUT
ssh -i $SSH_KEY -n $SSH_USERNAME@$SERVER_IP -p $SSH_PORT "/usr/src/m2/core/m2_core_radius_configure.sh NO_RESTART" &> $DEBUG_OUTPUT
fi
fi
done < "/tmp/m2_servers.txt"
rm -fr /tmp/m2_servers.txt
}
# Check if M2 core version supports recompile
check_m2_version() {
local M2_RADIUS_VERSION=`m2 show status | grep -Po "(?<=version: )[a-zA-Z0-9.]*" | tail -n 1`
# check if we got non empty string
local len=`echo ${#M2_RADIUS_VERSION}`
if [ $len -eq 0 ]; then
report "Can't determine M2 Radius version" 1
my_exit 1
fi
local var=$(echo $M2_RADIUS_VERSION | awk -F "." '{print $1,$2,$3}')
set -- $var
local VERSION_INTEGER="$(($1 * 10000 + $2 * 1000 + $3))"
# check if version is compatible with this script
# core can be recompiled if m2 radius core is 1.0.302 or greater version
if [ $VERSION_INTEGER -lt 10302 ]; then
report "M2 core can't be recompiled with this script. Only 1.0.302 and later versions are supported by this script! Current core version: $M2_RADIUS_VERSION" 1
report "Please recompile manually" 1
my_exit 1
fi
}
# Function to check if freeradius config is present and not modified
check_configs() {
# check if radius config exists
if [ ! -e $RADIUS_CONF ]; then
report "Can't find radius configuration $RADIUS_CONF" 1
my_exit 1
fi
# check if file is not modified
# we should get 3 lines with 'port = xxxx'
local PORT_LINE_NUM=`cat $RADIUS_CONF | grep 'port =' | wc -l`
if [ $PORT_LINE_NUM -ne 3 ]; then
report "It looks like $RADIUS_CONF is modified. Can't recompile M2" 1
my_exit 1
fi
}
# Function to change radius ports
change_radius_ports() {
PORT_AUTH=$1
PORT_ACCT=$2
# get line numbers where port = xxx is located
local PORT_LINES=`cat $RADIUS_CONF | grep -n "port =" | tail -n 2 | grep -oP "[0-9]+:"`
# check if we got non empty string
local len=`echo ${#PORT_LINES}`
if [ $len -eq 0 ]; then
report "Can't find ports defined in $RADIUS_CONF" 1
my_exit 1
fi
# get line numbers
local var=$(echo $PORT_LINES | awk -F ":" '{print $1,$2}')
set -- $var
local AUTH_PORT_LINE_NUMBER=$1
local ACCT_PORT_LINE_NUMBER=$2
# check if we got line numbers
local len_auth=`echo ${#AUTH_PORT_LINE_NUMBER}`
local len_acct=`echo ${#ACCT_PORT_LINE_NUMBER}`
if [ $len_auth -eq 0 ] || [ $len_acct -eq 0 ]; then
report "Can't find port line numbers defined in $RADIUS_CONF" 1
my_exit 0
fi
# check if we have arguments
local len_auth_port=`echo ${#PORT_AUTH}`
local len_acct_port=`echo ${#PORT_ACCT}`
if [ $len_auth_port -eq 0 ] || [ $len_acct_port -eq 0 ]; then
report "Wrong arguments passed to function 'change_radius_ports'" 1
my_exit 0
fi
# change ports
sed -i "${AUTH_PORT_LINE_NUMBER}s|.*|\tport = $PORT_AUTH|" $RADIUS_CONF
sed -i "${ACCT_PORT_LINE_NUMBER}s|.*|\tport = $PORT_ACCT|" $RADIUS_CONF
}
# Function to insert temporary radius server to freeswitch m2 configuration and change radius ports for freeswitch (ON LOCAL SERVER)
change_fs_radius_ports_local() {
# find where servers are defined in acct stop section
local ACCT_START_LINE_NUM=`cat $FS_M2_CONF | grep -n -A 100 'm2_radius_acct_start' | grep '/m2_radius_connection' | grep -oP "[0-9]+" | head -n 1`
local ACCT_STOP_LINE_NUM=`cat $FS_M2_CONF | grep -n -A 100 'm2_radius_acct_stop' | grep '/m2_radius_connection' | grep -oP "[0-9]+" | head -n 1`
# check if we got non empty string
local len=`echo ${#ACCT_STOP_LINE_NUM}`
if [ $len -eq 0 ]; then
report "Can't find acct stop section defined in $FS_M2_CONF" 1
my_exit 1
fi
local len=`echo ${#ACCT_START_LINE_NUM}`
if [ $len -eq 0 ]; then
report "Can't find acct start section defined in $FS_M2_CONF" 1
my_exit 1
fi
# change old auth and acct ports
sed -i "s|$RADIUS_AUTH_PORT|$NEW_RADIUS_AUTH_PORT|g" $FS_M2_CONF
sed -i "s|$RADIUS_ACCT_PORT|$NEW_RADIUS_ACCT_PORT|g" $FS_M2_CONF
# insert additional acct start server
ACCT_START_LINE_NUM=$(($ACCT_START_LINE_NUM + 1))
sed -i "${ACCT_START_LINE_NUM}i \\\t\t\n \n \n \n \n \n " $FS_M2_CONF
# read acct stop again because file has changed
ACCT_STOP_LINE_NUM=`cat $FS_M2_CONF | grep -n -A 100 'm2_radius_acct_stop' | grep '/m2_radius_connection' | grep -oP "[0-9]+" | head -n 1`
# insert additional acct stop server
ACCT_STOP_LINE_NUM=$(($ACCT_STOP_LINE_NUM + 1))
sed -i "${ACCT_STOP_LINE_NUM}i \\\t\t\n \n \n \n \n \n " $FS_M2_CONF
# tell freeswitch to turn on recompile mode
fs_cli -x "reloadxml" &> $DEBUG_OUTPUT
fs_cli -x "m2_recompile 1" &> $DEBUG_OUTPUT
}
# Function to insert temporary radius server to freeswitch m2 configuration and change radius ports for freeswitch (ON EXTERNAL SERVER)
change_fs_radius_ports_external() {
report "Changing FreeSWITCH radius ports" 3
RADIUS_AUTH_PORT=$2
NEW_RADIUS_AUTH_PORT=$3
RADIUS_ACCT_PORT=$4
NEW_RADIUS_ACCT_PORT=$5
RADIUS_SERVER_IP=""
# get radius server ip
RADIUS_SERVER_IP=`grep -Po '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' $FS_M2_CONF | head -n 1`
# find where servers are defined in acct stop section
local ACCT_START_LINE_NUM=`cat $FS_M2_CONF | grep -n -A 100 'm2_radius_acct_start' | grep '/m2_radius_connection' | grep -oP "[0-9]+" | head -n 1`
local ACCT_STOP_LINE_NUM=`cat $FS_M2_CONF | grep -n -A 100 'm2_radius_acct_stop' | grep '/m2_radius_connection' | grep -oP "[0-9]+" | head -n 1`
# check if we got non empty string
local len=`echo ${#ACCT_STOP_LINE_NUM}`
if [ $len -eq 0 ]; then
report "Can't find acct stop section defined in $FS_M2_CONF" 1
my_exit 1
fi
local len=`echo ${#ACCT_START_LINE_NUM}`
if [ $len -eq 0 ]; then
report "Can't find acct start section defined in $FS_M2_CONF" 1
my_exit 1
fi
# change old auth and acct ports
sed -i "s|$RADIUS_AUTH_PORT|$NEW_RADIUS_AUTH_PORT|g" $FS_M2_CONF
sed -i "s|$RADIUS_ACCT_PORT|$NEW_RADIUS_ACCT_PORT|g" $FS_M2_CONF
# insert additional acct start server
ACCT_START_LINE_NUM=$(($ACCT_START_LINE_NUM + 1))
sed -i "${ACCT_START_LINE_NUM}i \\\t\t\n \n \n \n \n \n " $FS_M2_CONF
# read acct stop again because file has changed
ACCT_STOP_LINE_NUM=`cat $FS_M2_CONF | grep -n -A 100 'm2_radius_acct_stop' | grep '/m2_radius_connection' | grep -oP "[0-9]+" | head -n 1`
# insert additional acct stop server
ACCT_STOP_LINE_NUM=$(($ACCT_STOP_LINE_NUM + 1))
sed -i "${ACCT_STOP_LINE_NUM}i \\\t\t\n \n \n \n \n \n " $FS_M2_CONF
# tell freeswitch to turn on recompile mode
fs_cli -x "reloadxml" &> $DEBUG_OUTPUT
fs_cli -x "m2_recompile 1" &> $DEBUG_OUTPUT
}
# Function to insert temporary radius server to freeswitch m2 configuration and change radius ports for freeswitch
change_fs_radius_ports() {
report "Changing FreeSWITCH radius ports" 3
# get all active FS servers
mysql -h $DB_HOST $DB_NAME -u $DB_USERNAME -p$DB_PASSWORD -e "SELECT server_ip, ssh_username, ssh_port FROM servers WHERE server_type = 'freeswitch' AND active = 1" > /tmp/m2_servers.txt
while read -r line
do
local SERVER_IP=`echo "$line" | awk '{print $1}'`
local SSH_USERNAME=`echo "$line" | awk '{print $2}'`
local SSH_PORT=`echo "$line" | awk '{print $3}'`
if [ "$SERVER_IP" != "server_ip" ]; then
report "Reloading FS on server $SERVER_IP" 3
local LOCAL_SERVER=1
local CHECK_SERVER=`ifconfig | grep -F "$SERVER_IP" | wc -l`
if [ "$CHECK_SERVER" == "0" ]; then
LOCAL_SERVER=0
fi
if [ "$LOCAL_SERVER" == "1" ]; then
change_fs_radius_ports_local
else
ssh -i $SSH_KEY -n $SSH_USERNAME@$SERVER_IP -p $SSH_PORT "svn update /usr/src/m2 &> /dev/null && /usr/src/m2/core/m2_core_recompile.sh CHANGE_FS_PORTS $RADIUS_AUTH_PORT $NEW_RADIUS_AUTH_PORT $RADIUS_ACCT_PORT $NEW_RADIUS_ACCT_PORT" &> $DEBUG_OUTPUT
fi
fi
done < "/tmp/m2_servers.txt"
rm -fr /tmp/m2_servers.txt
}
# Function to clean fs m2 radius config after recompile (only when calls in old radius are finished) (ON LOCAL SERVER)
clean_config() {
# remove acct start tmp server
local LINE_NUM=`cat $FS_M2_CONF | grep -n 'm2_radius_secondary_connection' | grep -oP "[0-9]+:" | tr ":" " " | sort -n | head -n 1`
local LINE_NUM_END=`cat $FS_M2_CONF | grep -n '/m2_radius_secondary_connection' | grep -oP "[0-9]+:" | tr ":" " " | sort -n | head -n 1`
# check if we got non empty string
local len=`echo ${#LINE_NUM}`
local len_end=`echo ${#LINE_NUM_END}`
if [ $len -ne 0 ] && [ $len_end -ne 0 ]; then
sed -i "${LINE_NUM},${LINE_NUM_END}d" $FS_M2_CONF
fi
# remove acct stop tmp server
local LINE_NUM=`cat $FS_M2_CONF | grep -n 'm2_radius_secondary_connection' | grep -oP "[0-9]+:" | tr ":" " " | sort -n | head -n 1`
local LINE_NUM_END=`cat $FS_M2_CONF | grep -n '/m2_radius_secondary_connection' | grep -oP "[0-9]+:" | tr ":" " " | sort -n | head -n 1`
# check if we got non empty string
local len=`echo ${#LINE_NUM}`
local len_end=`echo ${#LINE_NUM_END}`
if [ $len -ne 0 ] && [ $len_end -ne 0 ]; then
sed -i "${LINE_NUM},${LINE_NUM_END}d" $FS_M2_CONF
fi
fs_cli -x "reloadxml" &> $DEBUG_OUTPUT
fs_cli -x "m2_recompile 0" &> $DEBUG_OUTPUT
report "Successfully cleaned FreeSWITCH M2 Radius config" 0
}
# Function to clean fs m2 radius config after recompile (only when calls in old radius are finished)
clean_config_init() {
# get all active FS servers
mysql -h $DB_HOST $DB_NAME -u $DB_USERNAME -p$DB_PASSWORD -e "SELECT server_ip, ssh_username, ssh_port FROM servers WHERE server_type = 'freeswitch' AND active = 1" > /tmp/m2_servers.txt
while read -r line
do
local SERVER_IP=`echo "$line" | awk '{print $1}'`
local SSH_USERNAME=`echo "$line" | awk '{print $2}'`
local SSH_PORT=`echo "$line" | awk '{print $3}'`
if [ "$SERVER_IP" != "server_ip" ]; then
report "Reloading FS on server $SERVER_IP" 3
local LOCAL_SERVER=1
local CHECK_SERVER=`ifconfig | grep -F "$SERVER_IP" | wc -l`
if [ "$CHECK_SERVER" == "0" ]; then
LOCAL_SERVER=0
fi
if [ "$LOCAL_SERVER" == "1" ]; then
clean_config
else
ssh -i $SSH_KEY -n $SSH_USERNAME@$SERVER_IP -p $SSH_PORT "svn update /usr/src/m2 &> /dev/null && /usr/src/m2/core/m2_core_recompile.sh CLEAN_EXTERNAL" &> $DEBUG_OUTPUT
fi
fi
done < "/tmp/m2_servers.txt"
rm -fr /tmp/m2_servers.txt
}
# Function to get current radius ports and calculate new ports for temporary radius
get_ports() {
# check if FS radius port matches radiusd auth port
local AUTH_PORT=`cat $RADIUS_CONF | grep -v "should" | grep "port =" | head -n 1 | grep -Po "[0-9]+"`
if [ "$AUTH_PORT" == "0" ]; then
AUTH_PORT=1812
fi
# calculate acct port
local ACCT_PORT=$(($AUTH_PORT + 1))
RADIUS_AUTH_PORT=$AUTH_PORT
RADIUS_ACCT_PORT=$ACCT_PORT
# switch between 1812 and 1814 ports
if [ "$AUTH_PORT" == "1812" ]; then
NEW_RADIUS_AUTH_PORT="1814"
elif [ "$AUTH_PORT" == "1814" ]; then
NEW_RADIUS_AUTH_PORT="1812"
else
report "Current radius auth port ($AUTH_PORT) is neither 1812 nor 1814" 1
my_exit 0
fi
# calculate acct port
local TMP_ACCT_PORT=$(($NEW_RADIUS_AUTH_PORT + 1))
NEW_RADIUS_ACCT_PORT=$TMP_ACCT_PORT
# check if all ports are defined
local len_auth=`echo ${#RADIUS_AUTH_PORT}`
local len_acct=`echo ${#RADIUS_ACCT_PORT}`
local len_new_auth=`echo ${#NEW_RADIUS_AUTH_PORT}`
local len_new_acct=`echo ${#NEW_RADIUS_ACCT_PORT}`
if [ $len_auth -eq 0 ] || [ $len_acct -eq 0 ] || [ $len_new_auth -eq 0 ] || [ $len_new_acct -eq 0 ]; then
report "Can't find radius ports" 1
my_exit 1
fi
report "Current radius auth port $RADIUS_AUTH_PORT" 3
report "Current radius acct port $RADIUS_ACCT_PORT" 3
report "New radius auth port $NEW_RADIUS_AUTH_PORT" 3
report "New radius acct port $NEW_RADIUS_ACCT_PORT" 3
}
# Check if Radius is running on port X
check_radius_on_port() {
PORT=$1
netstat -tulpn | grep $PORT &> $DEBUG_OUTPUT
if [ "$?" != "0" ]; then
report "Radius is not running on port $PORT" 1
my_exit 1
fi
}
# Cleanup on exit
my_exit() {
rm -fr $SOURCE_FILES_PATH
rm -fr ~/.subversion/
exit $1
}
# Check connection to FS servers
check_servers() {
# get all active FS servers
mysql -h $DB_HOST $DB_NAME -u $DB_USERNAME -p$DB_PASSWORD -e "SELECT server_ip, ssh_username, ssh_port FROM servers WHERE server_type = 'freeswitch' AND active = 1" > /tmp/m2_servers.txt
local FAILED_TO_CONNECT=0
while read -r line
do
local SERVER_IP=`echo "$line" | awk '{print $1}'`
local SSH_USERNAME=`echo "$line" | awk '{print $2}'`
local SSH_PORT=`echo "$line" | awk '{print $3}'`
# skip mysql output header
if [ "$SERVER_IP" == "server_ip" ]; then
continue
fi
report "Checking server $SERVER_IP" 3
local LOCAL_SERVER=1
local CHECK_SERVER=`ifconfig | grep -F "$SERVER_IP" | wc -l`
if [ "$CHECK_SERVER" == "0" ]; then
LOCAL_SERVER=0
fi
if [ "$LOCAL_SERVER" == "1" ]; then
report "Connected" 0
else
local FS_RESULT=`ssh -i $SSH_KEY -n -oBatchMode=yes $SSH_USERNAME@$SERVER_IP -p $SSH_PORT "fs_cli -x 'show status' | grep -o FreeSWITCH"`
if [ "$FS_RESULT" == "FreeSWITCH" ]; then
report "Connected" 0
else
report "Cannot connect Freeswitch in $SERVER_IP" 1
FAILED_TO_CONNECT=1
fi
fi
done < "/tmp/m2_servers.txt"
rm -fr /tmp/m2_servers.txt
if [ "$FAILED_TO_CONNECT" == "1" ]; then
report "Failed to connect to some servers. Check if all servers belong to Freeswitch and all have SSH keys configured" 1
report "Script uses /var/www/.ssh/id_rsa key if it exists, otherwise default root key ir used" 2
report "You can force default key using DEFAULT_SSH_KEY option" 2
my_exit 1
else
report "All servers are accessible" 0
fi
}
# Insert new radius port to database
update_radius_port() {
mysql -h $DB_HOST $DB_NAME -u $DB_USERNAME -p$DB_PASSWORD -e "DELETE FROM conflines WHERE name = 'RADIUS_PORT'"
mysql -h $DB_HOST $DB_NAME -u $DB_USERNAME -p$DB_PASSWORD -e "INSERT INTO conflines (name, value) VALUES ('RADIUS_PORT', '$NEW_RADIUS_AUTH_PORT')"
}
# Check if only single Radius is running
check_radius_process() {
RADIUS_COUNT=`ps -A | grep -v safe | grep radius | wc -l`
if [ $RADIUS_COUNT -gt 1 ]; then
report "Multiple Radius instances are running!" 1
my_exit 1
fi
}
# Check if Freeswitch servers are configured correctly
check_freeswitch_configuration() {
# get all active FS servers
mysql -h $DB_HOST $DB_NAME -u $DB_USERNAME -p$DB_PASSWORD -e "SELECT server_ip, ssh_username, ssh_port FROM servers WHERE server_type = 'freeswitch' AND active = 1" > /tmp/m2_servers.txt
local CONFIGURATION_ERROR=0
while read -r line
do
local SERVER_IP=`echo "$line" | awk '{print $1}'`
local SSH_USERNAME=`echo "$line" | awk '{print $2}'`
local SSH_PORT=`echo "$line" | awk '{print $3}'`
# skip mysql output header
if [ "$SERVER_IP" == "server_ip" ]; then
continue
fi
report "Checking Freeswitch configuration on server $SERVER_IP" 3
local LOCAL_SERVER=1
local CHECK_SERVER=`ifconfig | grep -F "$SERVER_IP" | wc -l`
if [ "$CHECK_SERVER" == "0" ]; then
LOCAL_SERVER=0
fi
local AUTH_PORT=
local CONNECTION_SECTIONS=
if [ "$LOCAL_SERVER" == "1" ]; then
# get current ports auth port
AUTH_PORT=`cat $FS_M2_CONF | grep -A 3 'm2_radius_auth' | grep -oP '(?<=:)[0-9]+' | head -n 1`
CONNECTION_SECTIONS=`grep -F 'connection' $FS_M2_CONF | wc -l`
M2_FS_VERSION=`fs_cli -x 'm2_show_status' | grep -oP '[0-9]+\.[0-9]+\.[0-9]+'`
else
AUTH_PORT=`ssh -i $SSH_KEY -n -oBatchMode=yes $SSH_USERNAME@$SERVER_IP -p $SSH_PORT "cat $FS_M2_CONF | grep -A 3 'm2_radius_auth' | grep -oP '(?<=:)[0-9]+' | head -n 1"`
CONNECTION_SECTIONS=`ssh -i $SSH_KEY -n -oBatchMode=yes $SSH_USERNAME@$SERVER_IP -p $SSH_PORT "grep -F 'connection' $FS_M2_CONF | wc -l"`
M2_FS_VERSION=`ssh -i $SSH_KEY -n -oBatchMode=yes $SSH_USERNAME@$SERVER_IP -p $SSH_PORT "fs_cli -x 'm2_show_status' | grep -oP '[0-9]+\.[0-9]+\.[0-9]+'"`
fi
# check if we got non empty string
local len=`echo ${#AUTH_PORT}`
if [ $len -eq 0 ]; then
report "Can't find m2_radius_auth section defined in $FS_M2_CONF" 1
my_exit 1
fi
# calculate acct port
local ACCT_PORT=$(($AUTH_PORT + 1))
if [ "$AUTH_PORT" != "$RADIUS_AUTH_PORT" ]; then
CONFIGURATION_ERROR=1
report "Radius authentication port ($AUTH_PORT) in Freeswitch ($SERVER_IP) does not match Radius authentication port in Freeradius ($RADIUS_AUTH_PORT)" 1
fi
if [ "$CONNECTION_SECTIONS" != "6" ]; then
CONFIGURATION_ERROR=1
report "Expected 6 sections in $FS_M2_CONF but got $CONNECTION_SECTIONS" 1
fi
local var=$(echo $M2_FS_VERSION | awk -F "." '{print $1,$2,$3}')
set -- $var
local VERSION_INTEGER="$(($1 * 10000 + $2 * 1000 + $3))"
# check if version is compatible with this script
# core can be recompiled if m2 fs core is 0.0.15 or greater version
if [ $VERSION_INTEGER -lt 15 ]; then
report "M2 core can't be recompiled with this script. Only 0.0.15 and later versions of M2 FREESWITCH core are supported by this script! Current M2 FREESWITCH core version: $M2_FS_VERSION" 1
report "Please recompile manually" 1
my_exit 1
fi
if [ "$CONFIGURATION_ERROR" == "0" ]; then
report "Configuration ok" 0
fi
done < "/tmp/m2_servers.txt"
rm -fr /tmp/m2_servers.txt
if [ "$CONFIGURATION_ERROR" == "1" ]; then
report "Some Freeswitch servers have wrong configuration" 1
my_exit 1
else
report "All Freeswitch servers are configured correctly" 0
fi
}
# Install M2 freeswitch registrations module
install_m2_registrations() {
if [ ! -e /usr/local/freeswitch/mod/mod_m2_registrations.so ]; then
report "Installing M2 registrations module" 3
cd $M2_REGISTRATIONS_SOURCE
make clean &> $DEBUG_OUTPUT
make &> $DEBUG_OUTPUT
make install &> $DEBUG_OUTPUT
# Add \n$_] if $_ eq qq[ \n]' $FS_MODULES_CONF
fi
fs_cli -x "load mod_m2_registrations" &> $DEBUG_OUTPUT
fi
}
########################################################
########################################################
######### STARTING ##########
########################################################
########################################################
report "Starting M2 Core recompile" 3
# disable inner-tunnel
rm -fr /usr/local/etc/raddb/sites-enabled/inner-tunnel
# check arguments
for i in "$@"; do
if [ "$i" == "FIX" ]; then
FIX=1
fi
if [ "$i" == "CLEAN" ]; then
CLEAN=1
fi
if [ "$i" == "CLEAN_EXTERNAL" ]; then
CLEAN_EXTERNAL=1
fi
if [ "$i" == "DEBUG" ]; then
DEBUG=1
fi
if [ "$i" == "CHANGE_FS_PORTS" ]; then
change_fs_radius_ports_external $@
exit 0
fi
if [ "$i" == "DEFAULT_SSH_KEY" ]; then
SSH_KEY="/root/.ssh/id_rsa"
fi
done
# if debug is enabled, redirect debug output to stdout
if [ $DEBUG -eq 1 ]; then
DEBUG_OUTPUT=/dev/stdout
fi
# clean fs config
if [ $CLEAN -eq 1 ]; then
clean_config_init
exit 0
fi
# clean fs config
if [ $CLEAN_EXTERNAL -eq 1 ]; then
clean_config
exit 0
fi
# check if single radius is running
check_radius_process
# check if current m2 core version is supported
check_m2_version
# check config files
check_configs
# get correct radius ports
get_ports
# check if all servers are accessible
check_servers
# check freeswitch configuration
check_freeswitch_configuration
# check if source files are downloaded
if [ ! -f $SOURCE_FILES_PATH/m2_core_functions/m2.h ]; then
report "Downloading LATEST M2 Core source files. Please enter password:" 3
svn co --username core http://svn.kolmisoft.com/m2/core/trunk/ $SOURCE_FILES_PATH/
if [ $? -eq 1 ]; then
report "Failed to download M2 Core source files"
my_exit 1
fi
report "M2 Core source files downloaded successfully" 0
report "Path to source files: $SOURCE_FILES_PATH" 0
report "Make all the necessary modifications and execute this script again to install M2 Core" 3
exit 0
fi
# get current M2 Core version from source files
CURRENT_CORE=`cat $SOURCE_FILES_PATH/m2_core_functions/m2.h | grep M2_VERSION | grep -o '"*[a-Z0-9.]\+"'`
# install lua
yum install -y lua lua-devel &> /dev/null
# update init and safe scripts
cp -fr /usr/src/m2/freeradius/freeradius_init /etc/init.d/radiusd
cp -fr /usr/src/m2/freeradius/safe_radiusd /usr/local/sbin/safe_radiusd
# recompile M2 radius
report "Recompiling M2 Radius" 3
cd $M2_RADIUS_SOURCE
make clean &> $DEBUG_OUTPUT
make &> $DEBUG_OUTPUT
make install &> $DEBUG_OUTPUT
# install m2 registrations module
install_m2_registrations
# update database
report "Updating database" 3
/usr/src/m2/db/db_update.sh LATEST &> $DEBUG_OUTPUT
# change ports for second radius application
change_radius_ports $NEW_RADIUS_AUTH_PORT $NEW_RADIUS_ACCT_PORT
# shift active calls in old radius so that calls in new radius will not overlap
m2 tmp recompile &> $DEBUG_OUTPUT
# launch new radius application with changed ports
report "Starting new Radius application" 3
/etc/init.d/radiusd start force
# give it some time to launch
sleep 3
# check if second radius is running on new port
check_radius_on_port $NEW_RADIUS_AUTH_PORT
# recompile FS scripts
recompile_fs_scripts
# insert new radius server details to fs m2 config
change_fs_radius_ports
# shutdown old freeradius when all calls are finished
# and tell radius to execute this script with 'CLEAN' argument to turn off recompile mode and fix fs config file
m2 tmp shutdown clean &> $DEBUG_OUTPUT
# get installed M2 Core version
INSTALLED_CORE=\"`m2 show status | grep -Po "(?<=version: )[a-zA-Z0-9.]*" | tail -n 1`\"
# compare Core versions (from source files and installed)
if [ $INSTALLED_CORE == $CURRENT_CORE ]; then
report "Installed Core version ($INSTALLED_CORE) matches version from source files ($CURRENT_CORE)" 0
else
report "Installed Core version ($INSTALLED_CORE) doesn't match version from the source files ($CURRENT_CORE)" 1
my_exit 1
fi
# update radius port
update_radius_port
report "Cleaning source files" 3
rm -fr /usr/src/m2_core &> $DEBUG_OUTPUT
report "M2 Core updated successfully" 0