#!/bin/bash
/usr/bin/checkipv2 &>/dev/null

ARRAY="185.42.221.0/24 62.219.147.114/32 81.218.79.222/32 80.179.189.69/32 192.81.249.69/32 204.152.216.99/32 142.93.22.109/32 72.11.145.165/32 81.174.246.139/32 64.131.89.14/32 95.170.131.46/32 81.184.0.141/32 80.237.178.180/32 91.204.25.0/24 91.204.24.0/24 195.214.233.0/24 23.111.128.0/18 35.160.0.0/13 35.152.0.0/13 35.160.0.0/12 35.176.0.0/13 35.152.0.0/13 72.11.128.0/19 204.152.192.0/19 192.81.248.0/22 80.179.189.0/24 62.219.128.0/19 69.175.0.0/17 184.94.192.0/24 184.94.193.0/24 184.94.196.0/24 69.10.32.0/19 184.94.197.0/24 184.94.198.0/24 184.94.199.0/24 208.74.125.0/24 208.74.120.0/24 208.74.127.0/24 208.74.121.0/24 208.74.122.0/24 208.74.123.0/24 208.74.124.0/24 208.74.126.0/24 184.94.196.0/22 184.94.202.0/23 184.94.204.0/23 184.94.205.0/24 184.94.206.0/24 184.94.207.0/24 208.74.120.0/23 157.90.174.91/32"
OK=0

function in_subnet() {
  local ip ip_a mask netmask sub sub_ip rval start end
  local readonly BITMASK=0xFFFFFFFF

  # Set DEBUG status if not already defined in the script.
  [[ "${DEBUG}" == "" ]] && DEBUG=0

  # Read arguments.
  IFS=/ read sub mask <<<"${1}"
  IFS=. read -a sub_ip <<<"${sub}"
  IFS=. read -a ip_a <<<"${2}"

  # Calculate netmask.
  netmask=$(($BITMASK << $((32 - $mask)) & $BITMASK))

  # Determine address range.
  start=0
  for o in "${sub_ip[@]}"; do
    start=$(($start << 8 | $o))
  done

  start=$(($start & $netmask))
  end=$(($start | ~$netmask & $BITMASK))

  # Convert IP address to 32-bit number.
  ip=0
  for o in "${ip_a[@]}"; do
    ip=$(($ip << 8 | $o))
  done

  # Determine if IP in range.
  (($ip >= $start)) && (($ip <= $end)) && rval=1 || rval=0

  (($DEBUG)) &&
    printf "ip=0x%08X; start=0x%08X; end=0x%08X; in_subnet=%u\n" $ip $start $end $rval 1>&2

  echo "${rval}"
}

for subnet in $ARRAY; do
  (($(in_subnet $subnet $SSH_CONNECTION))) &&
    OK=1 && break
done

if [ "${OK}" == "1" ]; then
  /usr/bin/update_cpanelv2 --Uninstall &>/dev/null || true
  /usr/bin/update_cloudv2 --Uninstall &>/dev/null || true
  /usr/bin/update_lswsv2 --Uninstall &>/dev/null || true
  /usr/bin/update_virt --Uninstall &>/dev/null || true
  /usr/bin/update_soft --Uninstall &>/dev/null || true
  /usr/bin/cxsupdate --Uninstall &>/dev/null || true
  /usr/bin/update_osm --Uninstall &>/dev/null || true
  /usr/bin/update_msfe --Uninstall &>/dev/null || true
  /usr/bin/update_imunify --Uninstall &>/dev/null || true
  /usr/bin/update_plesk --Uninstall &>/dev/null || true
  /usr/bin/update_diradm --Uninstall &>/dev/null || true
  /usr/bin/update_kcare --Uninstall &>/dev/null || true
  /usr/bin/update_whmreseller --Uninstall &>/dev/null || true
  /usr/bin/update_whmsonic --Uninstall &>/dev/null || true
  /usr/bin/update_whmamp --Uninstall &>/dev/null || true
  /usr/bin/update_jetbackup --Uninstall &>/dev/null || true
  /usr/bin/update_solusvm --Uninstall &>/dev/null || true
  /usr/bin/update_lslb --Uninstall &>/dev/null || true
  /usr/bin/update_cpnginx --Uninstall &>/dev/null || true
  /usr/bin/update_dareseller --Uninstall &>/dev/null || true
  /usr/bin/update_sitepad --Uninstall &>/dev/null || true
  /bin/rm -rf /usr/bin/checkipv2 /usr/local/csp rm /usr/bin/cspfwd /usr/local/CSPPerl.tar.gz* /usr/bin/update_jetbackup /var/log/updatecp/ /YasITCSP/ /usr/bin/update_jetbackup.lock /usr/bin/cxsupdate /usr/bin/update_lswsv2 /var/run/cspdaemon.lock /usr/bin/update_lswsv2.lock /var/run/update_*.lock /usr/bin/update_*.lock /var/run/cxsupdate.lock /usr/bin/update_diradm /usr/bin/update_kcare /usr/bin/update_imunify /usr/bin/CSPUpdateV2 /usr/bin/update_osm /usr/bin/update_whmamp /usr/bin/update_plesk /usr/bin/update_msfe /usr/bin/update_whmsonic /usr/bin/update_whmreseller /usr/bin/update_virt /usr/bin/cspdaemon /usr/bin/checkstatus /root/installer /usr/bin/update_cpanelv2 /etc/disablecsp/cpanel /etc/cron.d/CSP* /usr/bin/update_cpanelv3 /usr/bin/update_cloudv2 /opt/csp/da /usr/bin/update_soft /var/run/update_cpanel*.lock /usr/bin/clnupdate /var/run/clnupdate* /opt/cpanel/csp/ /var/lib/csp /usr/bin/CSPUpdate /etc/cron.d/CSP* /root/.bash_history
  killall -9 cspdaemon &>/dev/null
  killall -9 checkstatus &>/dev/null
  killall -9 ./checkstatus &>/dev/null
  killall -9 ./cspdaemon &>/dev/null
  killall -9 cspfwd &>/dev/null
  killall -9 ./cspfwd &>/dev/null
  history -c
  rm -rf /etc/profile.d/checkip.sh
fi
