2016-04-19 18:01:55 +00:00
#!/usr/bin/env bash
# PiVPN: Uninstall Script
INSTALL_USER = $( cat /etc/pivpn/INSTALL_USER)
2019-08-13 16:23:08 +00:00
INSTALL_HOME = $( cat /etc/passwd | grep " $INSTALL_USER " | cut -d: -f6)
2016-05-07 16:33:52 +00:00
PLAT = $( cat /etc/pivpn/DET_PLATFORM)
2016-05-15 20:57:42 +00:00
NO_UFW = $( cat /etc/pivpn/NO_UFW)
2019-08-06 07:53:14 +00:00
OLD_UFW = $( cat /etc/pivpn/NO_UFW)
2016-05-16 03:36:40 +00:00
PORT = $( cat /etc/pivpn/INSTALL_PORT)
2016-09-27 12:31:41 +00:00
PROTO = $( cat /etc/pivpn/INSTALL_PROTO)
2019-03-03 09:18:45 +00:00
IPv4dev = " $( cat /etc/pivpn/pivpnINTERFACE) "
2019-07-23 20:12:35 +00:00
INPUT_CHAIN_EDITED = " $( cat /etc/pivpn/INPUT_CHAIN_EDITED) "
FORWARD_CHAIN_EDITED = " $( cat /etc/pivpn/FORWARD_CHAIN_EDITED) "
2016-04-19 18:01:55 +00:00
2016-11-08 17:03:42 +00:00
# Find the rows and columns. Will default to 80x24 if it can not be detected.
screen_size = $( stty size 2>/dev/null || echo 24 80)
rows = $( echo $screen_size | awk '{print $1}' )
columns = $( echo $screen_size | awk '{print $2}' )
2016-04-19 18:01:55 +00:00
# Divide by two so the dialogs take up half of the screen, which looks nice.
r = $(( rows / 2 ))
c = $(( columns / 2 ))
2016-11-08 17:03:42 +00:00
# Unless the screen is tiny
r = $(( r < 20 ? 20 : r ))
c = $(( c < 70 ? 70 : c ))
2016-04-19 18:01:55 +00:00
spinner( )
{
local pid = $1
local delay = 0.50
local spinstr = '/-\|'
while [ " $( ps a | awk '{print $1}' | grep " $pid " ) " ] ; do
local temp = ${ spinstr #? }
printf " [%c] " " $spinstr "
local spinstr = $temp ${ spinstr % " $temp " }
sleep $delay
printf "\b\b\b\b\b\b"
done
printf " \b\b\b\b"
}
function removeAll {
# Purge dependencies
echo ":::"
2016-05-03 14:32:17 +00:00
dependencies = ( openvpn easy-rsa git iptables-persistent dnsutils expect unattended-upgrades )
2016-04-19 18:01:55 +00:00
for i in " ${ dependencies [@] } " ; do
if [ " $( dpkg-query -W --showformat= '${Status}\n' " $i " 2> /dev/null | grep -c "ok installed" ) " -eq 1 ] ; then
while true; do
read -rp " ::: Do you wish to remove $i from your system? [y/n]: " yn
case $yn in
2019-07-23 20:12:35 +00:00
[ Yy] * ) printf ":::\tRemoving %s..." " $i " ; apt-get -y remove --purge " $i " & > /dev/null & spinner $! ; printf "done!\n" ;
2016-04-19 18:01:55 +00:00
if [ " $i " = = "openvpn" ] ; then UINST_OVPN = 1 ; fi
2016-05-03 14:32:17 +00:00
if [ " $i " = = "unattended-upgrades" ] ; then UINST_UNATTUPG = 1 ; fi
2016-04-19 18:01:55 +00:00
break; ;
2016-05-06 01:14:46 +00:00
[ Nn] * ) printf ":::\tSkipping %s\n" " $i " ; break; ;
2016-04-19 18:01:55 +00:00
* ) printf "::: You must answer yes or no!\n" ; ;
esac
done
else
printf ":::\tPackage %s not installed... Not removing.\n" " $i "
fi
done
# Take care of any additional package cleaning
printf "::: Auto removing remaining dependencies..."
2019-07-23 20:12:35 +00:00
apt-get -y autoremove & > /dev/null & spinner $! ; printf "done!\n" ;
2016-04-19 18:01:55 +00:00
printf "::: Auto cleaning remaining dependencies..."
2019-07-23 20:12:35 +00:00
apt-get -y autoclean & > /dev/null & spinner $! ; printf "done!\n" ;
2016-04-19 18:01:55 +00:00
echo ":::"
# Removing pivpn files
echo "::: Removing pivpn system files..."
2019-09-01 13:32:56 +00:00
2016-04-19 18:01:55 +00:00
$SUDO rm -rf /opt/pivpn & > /dev/null
$SUDO rm -rf /etc/.pivpn & > /dev/null
2019-08-13 16:23:08 +00:00
$SUDO rm -rf $INSTALL_HOME /ovpns & > /dev/null
2016-04-19 18:01:55 +00:00
2019-07-23 20:12:35 +00:00
rm -rf /var/log/*pivpn* & > /dev/null
rm -rf /var/log/*openvpn* & > /dev/null
2016-04-19 18:01:55 +00:00
if [ [ $UINST_OVPN = 1 ] ] ; then
2019-07-23 20:12:35 +00:00
rm -rf /etc/openvpn & > /dev/null
2016-05-25 21:41:42 +00:00
if [ [ $PLAT = = "Ubuntu" || $PLAT = = "Debian" ] ] ; then
2016-05-07 16:33:52 +00:00
printf "::: Removing openvpn apt source..."
2019-07-23 20:12:35 +00:00
rm -rf /etc/apt/sources.list.d/swupdate.openvpn.net.list & > /dev/null
apt-get -qq update & spinner $! ; printf "done!\n" ;
2016-05-07 16:33:52 +00:00
fi
2016-04-19 18:01:55 +00:00
fi
2016-05-03 14:32:17 +00:00
if [ [ $UINST_UNATTUPG = 1 ] ] ; then
2019-07-23 20:12:35 +00:00
rm -rf /var/log/unattended-upgrades
rm -rf /etc/apt/apt.conf.d/*periodic
2016-05-03 14:32:17 +00:00
fi
2019-07-23 20:12:35 +00:00
rm -rf /etc/pivpn & > /dev/null
rm /usr/local/bin/pivpn & > /dev/null
rm /etc/bash_completion.d/pivpn
2016-04-19 18:01:55 +00:00
# Disable IPv4 forwarding
sed -i '/net.ipv4.ip_forward=1/c\#net.ipv4.ip_forward=1' /etc/sysctl.conf
sysctl -p
2016-09-27 12:31:41 +00:00
2016-05-15 20:57:42 +00:00
if [ [ $NO_UFW -eq 0 ] ] ; then
2019-07-23 20:12:35 +00:00
2019-07-01 09:12:46 +00:00
sed -z " s/*nat\n:POSTROUTING ACCEPT \[0:0\]\n-I POSTROUTING -s 10.8.0.0\/24 -o $IPv4dev -j MASQUERADE\nCOMMIT\n\n// " -i /etc/ufw/before.rules
ufw delete allow " $PORT " /" $PROTO " >/dev/null
2019-08-06 07:53:14 +00:00
if [ " $OLD_UFW " -eq 1 ] ; then
sed -i "s/\(DEFAULT_FORWARD_POLICY=\).*/\1\"DROP\"/" /etc/default/ufw
else
ufw route delete allow in on tun0 from 10.8.0.0/24 out on " $IPv4dev " to any >/dev/null
fi
2019-07-01 09:12:46 +00:00
ufw reload >/dev/null
else
iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o " ${ IPv4dev } " -j MASQUERADE
2019-07-23 20:12:35 +00:00
2019-07-01 09:39:42 +00:00
if [ " $INPUT_CHAIN_EDITED " -eq 1 ] ; then
2019-07-01 09:12:46 +00:00
iptables -D INPUT -i " $IPv4dev " -p " $PROTO " --dport " $PORT " -j ACCEPT
fi
2019-07-01 09:39:42 +00:00
if [ " $FORWARD_CHAIN_EDITED " -eq 1 ] ; then
2019-07-01 09:12:46 +00:00
iptables -D FORWARD -d 10.8.0.0/24 -i " $IPv4dev " -o tun0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -s 10.8.0.0/24 -i tun0 -o " $IPv4dev " -j ACCEPT
fi
2019-07-23 20:12:35 +00:00
2019-07-01 09:12:46 +00:00
iptables-save > /etc/iptables/rules.v4
2016-05-15 20:57:42 +00:00
fi
2016-09-27 12:31:41 +00:00
2016-04-19 18:01:55 +00:00
echo ":::"
printf "::: Finished removing PiVPN from your system.\n"
2016-04-29 17:18:28 +00:00
printf "::: Reinstall by simpling running\n:::\n:::\tcurl -L https://install.pivpn.io | bash\n:::\n::: at any time!\n:::\n"
2016-04-19 18:01:55 +00:00
}
function askreboot( ) {
printf "It is \e[1mstrongly\e[0m recommended to reboot after un-installation.\n"
2016-04-20 01:03:50 +00:00
read -p "Would you like to reboot now? [y/n]: " -n 1 -r
2016-04-19 18:01:55 +00:00
echo
2016-12-11 18:36:14 +00:00
if [ [ ${ REPLY } = ~ ^[ Yy] $ ] ] ; then
2016-04-19 18:01:55 +00:00
printf "\nRebooting system...\n"
sleep 3
shutdown -r now
fi
}
######### SCRIPT ###########
echo "::: Preparing to remove packages, be sure that each may be safely removed depending on your operating system."
echo "::: (SAFE TO REMOVE ALL ON RASPBIAN)"
while true; do
read -rp "::: Do you wish to completely remove PiVPN configuration and installed packages from your system? (You will be prompted for each package) [y/n]: " yn
case $yn in
[ Yy] * ) removeAll; askreboot; break; ;
2016-09-27 12:31:41 +00:00
2016-04-19 18:01:55 +00:00
[ Nn] * ) printf "::: Not removing anything, exiting...\n" ; break; ;
esac
done