Commit db95beb3 authored by Francisco Biete's avatar Francisco Biete Committed by Nirbhay Choubey

MDEV-9903 - 10.2 : Check and run rsync daemon only in the needed IP

See https://github.com/MariaDB/server/pull/235
I submit this code under the BSD-new license.
parent d5e6d837
......@@ -60,7 +60,8 @@ check_pid_and_port()
{
local pid_file=$1
local rsync_pid=$2
local rsync_port=$3
local rsync_addr=$3
local rsync_port=$4
if ! which lsof > /dev/null; then
wsrep_log_error "lsof tool not found in PATH! Make sure you have it installed."
......@@ -69,18 +70,44 @@ check_pid_and_port()
local port_info=$(lsof -i :$rsync_port -Pn 2>/dev/null | \
grep "(LISTEN)")
local is_listening_all=$(echo $port_info | \
grep "*:$rsync_port" 2>/dev/null)
local is_listening_addr=$(echo $port_info | \
grep "$rsync_addr:$rsync_port" 2>/dev/null)
local is_rsync=$(echo $port_info | \
grep -w '^rsync[[:space:]]\+'"$rsync_pid" 2>/dev/null)
if [ -n "$port_info" -a -z "$is_rsync" ]; then
wsrep_log_error "rsync daemon port '$rsync_port' has been taken"
exit 16 # EBUSY
if [ ! -z "$is_listening_all" -o ! -z "$is_listening_addr" ]; then
if [ -z "$is_rsync" ]; then
wsrep_log_error "rsync daemon port '$rsync_port' has been taken"
exit 16 # EBUSY
fi
fi
check_pid $pid_file && \
[ -n "$port_info" ] && [ -n "$is_rsync" ] && \
[ $(cat $pid_file) -eq $rsync_pid ]
}
is_local_ip()
{
local address="$1"
local get_addr_bin=`which ifconfig`
if [ -z "$get_addr_bin" ]
then
get_addr_bin=`which ip`
get_addr_bin="$get_addr_bin address show"
# Add an slash at the end, so we don't get false positive : 172.18.0.4 matches 172.18.0.41
# ip output format is "X.X.X.X/mask"
address="${address}/"
else
# Add an space at the end, so we don't get false positive : 172.18.0.4 matches 172.18.0.41
# ifconfig output format is "X.X.X.X "
address="$address "
fi
$get_addr_bin | grep "$address" > /dev/null
}
MAGIC_FILE="$WSREP_SST_OPT_DATA/rsync_sst_complete"
rm -rf "$MAGIC_FILE"
......@@ -271,6 +298,7 @@ then
ADDR=$WSREP_SST_OPT_ADDR
RSYNC_PORT=$(echo $ADDR | awk -F ':' '{ print $2 }')
RSYNC_ADDR=$(echo $ADDR | awk -F ':' '{ print $1 }')
if [ -z "$RSYNC_PORT" ]
then
RSYNC_PORT=4444
......@@ -303,11 +331,19 @@ EOF
# rm -rf "$DATA"/ib_logfile* # we don't want old logs around
# listen at all interfaces (for firewalled setups)
rsync --daemon --no-detach --port $RSYNC_PORT --config "$RSYNC_CONF" &
# If the IP is local listen only in it
if is_local_ip $RSYNC_ADDR
then
rsync --daemon --no-detach --address $RSYNC_ADDR --port $RSYNC_PORT --config "$RSYNC_CONF" &
else
# Not local, possibly a NAT, listen in all interface
rsync --daemon --no-detach --port $RSYNC_PORT --config "$RSYNC_CONF" &
# Overwrite address with all
RSYNC_ADDR="*"
fi
RSYNC_REAL_PID=$!
until check_pid_and_port $RSYNC_PID $RSYNC_REAL_PID $RSYNC_PORT
until check_pid_and_port $RSYNC_PID $RSYNC_REAL_PID $RSYNC_ADDR $RSYNC_PORT
do
sleep 0.2
done
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment