raspap-webgui-mirror/installers/uninstall.sh

267 lines
9.5 KiB
Bash
Raw Normal View History

2017-10-03 05:09:55 +00:00
#!/bin/bash
2020-04-08 08:53:41 +00:00
#
# RaspAP uninstall functions
# Author: @billz <billzimmerman@gmail.com>
# License: GNU General Public License v3.0
#
# You are not obligated to bundle the LICENSE file with your RaspAP projects as long
# as you leave these references intact in the header comments of your source files.
# Exit on error
set -o errexit
# Exit on error inside functions
set -o errtrace
# Turn on traces, disabled by default
# set -o xtrace
# Set defaults
readonly raspap_dir="/etc/raspap"
readonly raspap_user="www-data"
readonly raspap_sudoers="/etc/sudoers.d/090_raspap"
2020-12-15 07:40:21 +00:00
readonly raspap_default="/etc/dnsmasq.d/090_raspap.conf"
readonly raspap_wlan0="/etc/dnsmasq.d/090_wlan0.conf"
2020-04-08 08:53:41 +00:00
readonly raspap_sysctl="/etc/sysctl.d/90_raspap.conf"
readonly raspap_adblock="/etc/dnsmasq.d/090_adblock.conf"
2020-05-27 08:04:16 +00:00
readonly raspap_network="/etc/systemd/network/"
2020-04-08 08:53:41 +00:00
readonly rulesv4="/etc/iptables/rules.v4"
webroot_dir="/var/www/html"
# Determines host Linux distribution details
2020-04-08 08:53:41 +00:00
function _get_linux_distro() {
if type lsb_release >/dev/null 2>&1; then # linuxbase.org
OS=$(lsb_release -si)
RELEASE=$(lsb_release -sr)
CODENAME=$(lsb_release -sc)
DESC=$(lsb_release -sd)
LONG_BIT=$(getconf LONG_BIT)
2020-04-08 08:53:41 +00:00
elif [ -f /etc/os-release ]; then # freedesktop.org
. /etc/os-release
OS=$ID
RELEASE=$VERSION_ID
CODENAME=$VERSION_CODENAME
DESC=$PRETTY_NAME
else
_install_error "Unsupported Linux distribution"
fi
}
# Sets php package option based on Linux version, abort if unsupported distro
function _set_php_package() {
case $RELEASE in
23.05|12*) # Debian 12 & Armbian 23.05
php_package="php8.2-cgi"
phpcgiconf="/etc/php/8.2/cgi/php.ini" ;;
23.04) # Ubuntu Server 23.04
php_package="php8.1-cgi"
phpcgiconf="/etc/php/8.1/cgi/php.ini" ;;
22.04|20.04|18.04|19.10|11*) # Previous Ubuntu Server, Debian & Armbian distros
2020-04-08 08:53:41 +00:00
php_package="php7.4-cgi"
phpcgiconf="/etc/php/7.4/cgi/php.ini" ;;
10*|11*)
2020-04-08 08:53:41 +00:00
php_package="php7.3-cgi"
phpcgiconf="/etc/php/7.3/cgi/php.ini" ;;
2020-05-27 08:04:16 +00:00
9*)
2020-04-08 08:53:41 +00:00
php_package="php7.0-cgi"
phpcgiconf="/etc/php/7.0/cgi/php.ini" ;;
8)
_install_error "${DESC} and php5 are unsupported."
exit 1 ;;
*)
_install_error "${DESC} is unsupported."
exit 1 ;;
2020-04-08 08:53:41 +00:00
esac
}
2017-10-03 05:09:55 +00:00
# Outputs a RaspAP Install log line
2020-04-08 08:53:41 +00:00
function _install_log() {
echo -e "\033[1;32mRaspAP Uninstall: $*\033[m"
2017-10-03 05:09:55 +00:00
}
# Outputs a RaspAP Install Error log line and exits with status code 1
2020-04-08 08:53:41 +00:00
function _install_error() {
echo -e "\033[1;37;41mRaspAP Uninstall Error: $*\033[m"
2017-10-03 05:09:55 +00:00
exit 1
}
# Checks to make sure uninstallation info is correct
2020-04-08 08:53:41 +00:00
function _config_uninstallation() {
_install_log "Configure uninstall of RaspAP"
_get_linux_distro
echo "Detected OS: ${DESC} ${LONG_BIT}-bit"
2020-04-08 08:53:41 +00:00
echo "RaspAP install directory: ${raspap_dir}"
echo -n "Lighttpd install directory: ${webroot_dir}? [Y/n]: "
read answer
if [ "$answer" != "${answer#[Nn]}" ]; then
read -e -p "Enter alternate lighttpd directory: " -i "/var/www/html" webroot_dir
fi
echo "Uninstall from lighttpd directory: ${webroot_dir}"
echo -n "Uninstall RaspAP with these values? [Y/n]: "
2017-10-03 05:09:55 +00:00
read answer
2020-04-08 08:53:41 +00:00
if [[ "$answer" != "${answer#[Nn]}" ]]; then
2017-10-03 05:09:55 +00:00
echo "Installation aborted."
exit 0
fi
}
# Checks for/restore backup files
2020-04-08 08:53:41 +00:00
function _check_for_backups() {
2017-10-03 05:09:55 +00:00
if [ -d "$raspap_dir/backups" ]; then
if [ -f "$raspap_dir/backups/hostapd.conf" ]; then
echo -n "Restore the last hostapd configuration file? [y/N]: "
read answer
if [[ $answer -eq 'y' ]]; then
sudo cp "$raspap_dir/backups/hostapd.conf" /etc/hostapd/hostapd.conf
fi
fi
if [ -f "$raspap_dir/backups/dnsmasq.conf" ]; then
echo -n "Restore the last dnsmasq configuration file? [y/N]: "
read answer
if [[ $answer -eq 'y' ]]; then
sudo cp "$raspap_dir/backups/dnsmasq.conf" /etc/dnsmasq.conf
fi
fi
if [ -f "$raspap_dir/backups/dhcpcd.conf" ]; then
echo -n "Restore the last dhcpcd.conf file? [y/N]: "
read answer
2017-10-03 05:09:55 +00:00
if [[ $answer -eq 'y' ]]; then
sudo cp "$raspap_dir/backups/dhcpcd.conf" /etc/dhcpcd.conf
fi
fi
if [ -f "$raspap_dir/backups/php.ini" ] && [ -f "$phpcgiconf" ]; then
echo -n "Restore the last php.ini file? [y/N]: "
read answer
if [[ $answer -eq 'y' ]]; then
sudo cp "$raspap_dir/backups/php.ini" "$phpcgiconf"
fi
fi
2017-10-03 05:09:55 +00:00
fi
}
# Removes RaspAP directories
2020-04-08 08:53:41 +00:00
function _remove_raspap_directories() {
_install_log "Removing RaspAP Directories"
2017-10-03 05:09:55 +00:00
if [ ! -d "$raspap_dir" ]; then
2020-04-08 08:53:41 +00:00
_install_error "RaspAP Configuration directory not found. Exiting."
2017-10-03 05:09:55 +00:00
fi
if [ ! -d "$webroot_dir" ]; then
2020-04-08 08:53:41 +00:00
_install_error "RaspAP Installation directory not found. Exiting."
2017-10-03 05:09:55 +00:00
fi
2020-04-08 08:53:41 +00:00
sudo rm -rf "$webroot_dir"/* || _install_error "Unable to remove $webroot_dir"
sudo rm -rf "$raspap_dir" || _install_error "Unable to remove $raspap_dir"
}
2017-10-03 05:09:55 +00:00
2020-04-08 08:53:41 +00:00
# Removes raspapd.service
function _remove_raspap_service() {
_install_log "Removing raspapd.service"
2020-12-06 09:11:43 +00:00
if [ -f /lib/systemd/system/raspapd.service ]; then
sudo rm /lib/systemd/system/raspapd.service || _install_error "Unable to remove raspap.service file"
fi
2020-04-08 08:53:41 +00:00
sudo systemctl daemon-reload
echo "Done."
}
2017-10-03 05:09:55 +00:00
2020-04-08 08:53:41 +00:00
# Restores networking config to pre-install defaults
function _restore_networking() {
_install_log "Restoring networking config to pre-install defaults"
echo "Disabling IP forwarding in $raspap_sysctl"
2020-05-27 08:04:16 +00:00
sudo rm "$raspap_sysctl" || _install_error "Unable to remove $raspap_sysctl"
2020-04-08 08:53:41 +00:00
sudo /etc/init.d/procps restart || _install_error "Unable to execute procps"
echo "Checking iptables rules"
rules=(
"-A POSTROUTING -j MASQUERADE"
2022-04-10 18:09:23 +00:00
"-A POSTROUTING -s 192.168.50.0/24 ! -d 192.168.50.0/24 -j MASQUERADE"
2020-04-08 08:53:41 +00:00
)
for rule in "${rules[@]}"; do
if grep -- "$rule" $rulesv4 > /dev/null; then
rule=$(sed -e 's/^\(-A POSTROUTING\)/-t nat -D POSTROUTING/' <<< $rule)
echo "Removing rule: ${rule}"
sudo iptables $rule || _install_error "Unable to execute iptables"
removed=true
fi
done
# Persist rules if removed
if [ "$removed" = true ]; then
echo "Removing persistent iptables rules"
sudo iptables-save | sudo tee $rulesv4 > /dev/null || _install_error "Unable to execute iptables-save"
fi
echo "Done."
2020-05-27 08:04:16 +00:00
# Remove dnsmasq and bridge configs
2020-12-15 07:40:21 +00:00
echo "Removing 090_raspap.conf from dnsmasq"
if [ -f $raspap_default ]; then
sudo rm "$raspap_default" || _install_error "Unable to remove $raspap_default"
fi
2020-12-06 09:08:11 +00:00
echo "Removing 090_wlan0.conf from dnsmasq"
2020-12-15 07:40:21 +00:00
if [ -f $raspap_wlan0 ]; then
sudo rm "$raspap_wlan0" || _install_error "Unable to remove $raspap_wlan0"
2020-12-06 09:08:11 +00:00
fi
2020-05-27 08:04:16 +00:00
echo "Removing raspap bridge configurations"
sudo rm "$raspap_network"/raspap* || _install_error "Unable to remove bridge config"
if [ -f $raspap_adblock ]; then
echo "Removing raspap adblock configuration"
sudo rm "$raspap_adblock" || _install_error "Unable to remove adblock config"
fi
2017-10-03 05:09:55 +00:00
}
# Removes installed packages
2020-04-08 08:53:41 +00:00
function _remove_installed_packages() {
_install_log "Removing installed packages"
_set_php_package
# Set default
dhcpcd_package="dnsmasq"
2020-04-08 08:53:41 +00:00
if [ ${OS,,} = "debian" ] || [ ${OS,,} = "ubuntu" ]; then
dhcpcd_package="dhcpcd5"
iw_package="iw"
fi
if [ ${OS,,} = "raspbian" ] && [[ ${RELEASE} =~ ^(12) ]]; then
dhcpcd_package="dhcpcd dhcpcd-base"
2020-04-08 08:53:41 +00:00
fi
echo -n "Remove the following installed packages? lighttpd hostapd iptables-persistent $php_package $dhcpcd_package $iw_package vnstat qrencode jq [y/N]: "
read answer
if [ "$answer" == 'y' ] || [ "$answer" == 'Y' ]; then
echo "Removing packages."
sudo apt-get remove lighttpd hostapd iptables-persistent $php_package $dhcpcd_package $iw_package vnstat qrencode jq || _install_error "Unable to remove installed packages"
2020-04-08 08:53:41 +00:00
sudo apt-get autoremove || _install_error "Unable to run apt autoremove"
else
echo "Leaving packages installed."
fi
}
2017-10-03 05:09:55 +00:00
# Removes www-data from sudoers
2020-04-08 08:53:41 +00:00
function _remove_sudoers() {
_install_log "Removing sudoers permissions"
echo "Removing ${raspap_sudoers}"
sudo rm "$raspap_sudoers" || _install_error "Unable to remove $raspap_sudoers"
echo "Done."
}
function _remove_lighttpd_config() {
echo "Unlinking 50-raspap-router.conf from /etc/lighttpd/conf-enabled/"
sudo unlink "/etc/lighttpd/conf-enabled/50-raspap-router.conf" || _install_error "Unable to unlink lighttpd config"
echo "Removing 50-raspap-router.conf from /etc/lighttpd/conf-available/"
sudo rm "/etc/lighttpd/conf-available/50-raspap-router.conf" || _install_error "Unable to remove lighttpd config"
sudo systemctl restart lighttpd.service || _install_status 1 "Unable to restart lighttpd"
echo "Done."
}
2020-04-08 08:53:41 +00:00
function _uninstall_complete() {
_install_log "Uninstall completed"
echo "Check your network configuration before rebooting to ensure access."
2017-10-03 05:09:55 +00:00
}
2020-04-08 08:53:41 +00:00
function _remove_raspap() {
_config_uninstallation
_check_for_backups
_remove_raspap_service
_restore_networking
_remove_raspap_directories
_remove_lighttpd_config
2020-04-08 08:53:41 +00:00
_remove_installed_packages
_remove_sudoers
_uninstall_complete
2017-10-03 05:09:55 +00:00
}