1
0
mirror of https://github.com/billz/raspap-webgui.git synced 2023-10-10 13:37:24 +02:00

Aligns uninstall w/ #537

This commit is contained in:
billz 2020-04-08 09:53:41 +01:00
parent e5c93970ac
commit 223ae3e11a

View File

@ -1,66 +1,95 @@
#!/bin/bash #!/bin/bash
raspap_dir="/etc/raspap" #
raspap_user="www-data" # RaspAP uninstall functions
version=`sed 's/\..*//' /etc/debian_version` # 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.
# Determine Raspbian version and set default home location for lighttpd # 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"
readonly raspap_dnsmasq="/etc/dnsmasq.d/090_raspap.conf"
readonly raspap_sysctl="/etc/sysctl.d/90_raspap.conf"
readonly rulesv4="/etc/iptables/rules.v4"
webroot_dir="/var/www/html" webroot_dir="/var/www/html"
if [ $version -eq 10 ]; then
version_msg="Raspbian 10.0 (Buster)"
php_package="php7.1-cgi"
elif [ $version -eq 9 ]; then
version_msg="Raspbian 9.0 (Stretch)"
php_package="php7.0-cgi"
else
version_msg="Raspbian 8.0 (Jessie) or earlier"
webroot_dir="/var/www"
php_package="php5-cgi"
fi
phpcgiconf="" # Determines host Linux distrubtion details
if [ "$php_package" = "php7.1-cgi" ]; then function _get_linux_distro() {
phpcgiconf="/etc/php/7.1/cgi/php.ini" if type lsb_release >/dev/null 2>&1; then # linuxbase.org
elif [ "$php_package" = "php7.0-cgi" ]; then OS=$(lsb_release -si)
phpcgiconf="/etc/php/7.0/cgi/php.ini" RELEASE=$(lsb_release -sr)
elif [ "$php_package" = "php5-cgi" ]; then CODENAME=$(lsb_release -sc)
phpcgiconf="/etc/php5/cgi/php.ini" DESC=$(lsb_release -sd)
fi 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
"18.04"|"19.10") # Ubuntu Server
php_package="php7.4-cgi"
phpcgiconf="/etc/php/7.4/cgi/php.ini" ;;
"10")
php_package="php7.3-cgi"
phpcgiconf="/etc/php/7.3/cgi/php.ini" ;;
"9")
php_package="php7.0-cgi"
phpcgiconf="/etc/php/7.0/cgi/php.ini" ;;
esac
}
# Outputs a RaspAP Install log line # Outputs a RaspAP Install log line
function install_log() { function _install_log() {
echo -e "\033[1;32mRaspAP Install: $*\033[m" echo -e "\033[1;32mRaspAP Uninstall: $*\033[m"
} }
# Outputs a RaspAP Install Error log line and exits with status code 1 # Outputs a RaspAP Install Error log line and exits with status code 1
function install_error() { function _install_error() {
echo -e "\033[1;37;41mRaspAP Install Error: $*\033[m" echo -e "\033[1;37;41mRaspAP Uninstall Error: $*\033[m"
exit 1 exit 1
} }
# Checks to make sure uninstallation info is correct # Checks to make sure uninstallation info is correct
function config_uninstallation() { function _config_uninstallation() {
install_log "Configure installation" _install_log "Configure uninstall of RaspAP"
echo "Detected ${version_msg}" _get_linux_distro
echo "Install directory: ${raspap_dir}" echo "Detected ${DESC}"
echo "Lighttpd directory: ${webroot_dir}" echo "RaspAP install directory: ${raspap_dir}"
echo -n "Uninstall RaspAP with these values? [y/N]: " echo -n "Lighttpd install directory: ${webroot_dir}? [Y/n]: "
read answer read answer
if [[ $answer != "y" ]]; then 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]: "
read answer
if [[ "$answer" != "${answer#[Nn]}" ]]; then
echo "Installation aborted." echo "Installation aborted."
exit 0 exit 0
fi fi
} }
# Checks for/restore backup files # Checks for/restore backup files
function check_for_backups() { function _check_for_backups() {
if [ -d "$raspap_dir/backups" ]; then if [ -d "$raspap_dir/backups" ]; then
if [ -f "$raspap_dir/backups/interfaces" ]; then
echo -n "Restore the last interfaces file? [y/N]: "
read answer
if [[ $answer -eq 'y' ]]; then
sudo cp "$raspap_dir/backups/interfaces" /etc/network/interfaces
fi
fi
if [ -f "$raspap_dir/backups/hostapd.conf" ]; then if [ -f "$raspap_dir/backups/hostapd.conf" ]; then
echo -n "Restore the last hostapd configuration file? [y/N]: " echo -n "Restore the last hostapd configuration file? [y/N]: "
read answer read answer
@ -89,63 +118,101 @@ function check_for_backups() {
sudo cp "$raspap_dir/backups/php.ini" "$phpcgiconf" sudo cp "$raspap_dir/backups/php.ini" "$phpcgiconf"
fi fi
fi fi
if [ -f "$raspap_dir/backups/rc.local" ]; then
echo -n "Restore the last rc.local file? [y/N]: "
read answer
if [[ $answer -eq 'y' ]]; then
sudo cp "$raspap_dir/backups/rc.local" /etc/rc.local
else
echo -n "Remove RaspAP Lines from /etc/rc.local? [Y/n]: "
if [[ $answer -ne 'n' ]]; then
sed -i '/#RASPAP/d' /etc/rc.local
fi
fi
fi
fi fi
} }
# Removes RaspAP directories # Removes RaspAP directories
function remove_raspap_directories() { function _remove_raspap_directories() {
install_log "Removing RaspAP Directories" _install_log "Removing RaspAP Directories"
if [ ! -d "$raspap_dir" ]; then if [ ! -d "$raspap_dir" ]; then
install_error "RaspAP Configuration directory not found. Exiting!" _install_error "RaspAP Configuration directory not found. Exiting."
fi fi
if [ ! -d "$webroot_dir" ]; then if [ ! -d "$webroot_dir" ]; then
install_error "RaspAP Installation directory not found. Exiting!" _install_error "RaspAP Installation directory not found. Exiting."
fi fi
sudo rm -rf "$webroot_dir"/* || _install_error "Unable to remove $webroot_dir"
sudo rm -rf "$raspap_dir" || _install_error "Unable to remove $raspap_dir"
}
sudo rm -rf "$webroot_dir"/* # Removes raspapd.service
sudo rm -rf "$raspap_dir" function _remove_raspap_service() {
_install_log "Removing raspapd.service"
sudo rm /lib/systemd/system/raspapd.service || _install_error "Unable to remove raspap.service file"
sudo systemctl daemon-reload
sudo systemctl disable raspapd.service || _install_error "Failed to disable raspap.service"
echo "Done."
}
# 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"
sudo rm $raspap_sysctl || _install_error "Unable to remove $raspap_sysctl"
sudo /etc/init.d/procps restart || _install_error "Unable to execute procps"
echo "Checking iptables rules"
rules=(
"-A POSTROUTING -j MASQUERADE"
"-A POSTROUTING -s 192.168.50.0/24 ! -d 192.168.50.0/24 -j MASQUERADE"
)
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."
} }
# Removes installed packages # Removes installed packages
function remove_installed_packages() { function _remove_installed_packages() {
install_log "Removing installed packages" _install_log "Removing installed packages"
echo -n "Remove the following installed packages? lighttpd $php_package git hostapd dnsmasq vnstat [y/N]: " _set_php_package
if [ ${OS,,} = "debian" ] || [ ${OS,,} = "ubuntu" ]; then
dhcpcd_package="dhcpcd5"
else
dhcpcd_package="dnsmasq"
fi
echo -n "Remove the following installed packages? lighttpd hostapd iptables-persistent $php_package $dhcpcd_package vnstat qrencode [y/N]: "
read answer read answer
if [ "$answer" != 'n' ] && [ "$answer" != 'N' ]; then if [ "$answer" != 'n' ] && [ "$answer" != 'N' ]; then
echo "Removing packages." echo "Removing packages."
sudo apt-get remove lighttpd $php_package git hostapd dnsmasq vnstat sudo apt-get remove lighttpd hostapd iptables-persistent $php_package $dhcpcd_package vnstat qrencode || _install_error "Unable to remove installed packages"
sudo apt-get autoremove sudo apt-get autoremove || _install_error "Unable to run apt autoremove"
else else
echo "Leaving packages installed." echo "Leaving packages installed."
fi fi
} }
# Removes www-data from sudoers # Removes www-data from sudoers
function clean_sudoers() { function _remove_sudoers() {
# should this check for only our commands? _install_log "Removing sudoers permissions"
sudo sed -i '/www-data/d' /etc/sudoers echo "Removing ${raspap_sudoers}"
sudo rm "$raspap_sudoers" || _install_error "Unable to remove $raspap_sudoers"
echo "Done."
} }
function remove_raspap() { function _uninstall_complete() {
config_uninstallation _install_log "Uninstall completed"
check_for_backups echo "It is recommended that you reboot your system as a final step."
remove_raspap_directories
remove_installed_packages
clean_sudoers
} }
remove_raspap function _remove_raspap() {
_config_uninstallation
_check_for_backups
_remove_raspap_service
_restore_networking
_remove_raspap_directories
_remove_installed_packages
_remove_sudoers
_uninstall_complete
}
_remove_raspap