From 3eb6739263ce2e362799c0308d4181fe633300c8 Mon Sep 17 00:00:00 2001 From: ryt51V Date: Wed, 2 Mar 2016 19:28:33 +0000 Subject: [PATCH] Installer and uninstaller now rerun themselves as root using sudo at the start of the script, rather than run individual commands with sudo. --- automated install/basic-install.sh | 188 +++++++++++++++-------------- automated install/uninstall.sh | 69 ++++++----- 2 files changed, 135 insertions(+), 122 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index b3e6202f..7481108d 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -16,6 +16,28 @@ # # curl -L install.pi-hole.net | bash +######## ROOT ######### +# Check if root, and if not then rerun with sudo. +echo ":::" +if [[ $EUID -eq 0 ]];then + echo "::: You are root." + # Older versions of Pi-hole set $SUDO="sudo" and prefixed commands with it, + # rather than rerunning as sudo. Just in case it turns up by accident, + # explicitly set the $SUDO variable to an empty string. + SUDO="" +else + echo "::: sudo will be used." + # Check if it is actually installed + # If it isn't, exit because the install cannot complete + if [[ $(dpkg-query -s sudo) ]];then + echo "::: Running sudo $@" + sudo "$@" + exit $? + else + echo "::: Please install sudo or run this script as root." + exit 1 + fi +fi ######## VARIABLES ######### @@ -47,22 +69,6 @@ availableInterfaces=$(ip -o link | awk '{print $2}' | grep -v "lo" | cut -d':' - dhcpcdFile=/etc/dhcpcd.conf ######## FIRST CHECK ######## -# Must be root to install -echo ":::" -if [[ $EUID -eq 0 ]];then - echo "::: You are root." -else - echo "::: sudo will be used for the install." - # Check if it is actually installed - # If it isn't, exit because the install cannot complete - if [[ $(dpkg-query -s sudo) ]];then - export SUDO="sudo" - else - echo "::: Please install sudo or run this as root." - exit 1 - fi -fi - if [ -d "/etc/pihole" ]; then # Likely an existing install @@ -78,7 +84,7 @@ spinner() { spin='-\|/' i=0 - while $SUDO kill -0 $pid 2>/dev/null + while kill -0 $pid 2>/dev/null do i=$(( (i+1) %4 )) printf "\b${spin:$i:1}" @@ -91,13 +97,13 @@ backupLegacyPihole() { # This function detects and backups the pi-hole v1 files. It will not do anything to the current version files. if [[ -f /etc/dnsmasq.d/adList.conf ]];then echo "::: Original Pi-hole detected. Initiating sub space transport" - $SUDO mkdir -p /etc/pihole/original/ - $SUDO mv /etc/dnsmasq.d/adList.conf /etc/pihole/original/adList.conf.$(date "+%Y-%m-%d") - $SUDO mv /etc/dnsmasq.conf /etc/pihole/original/dnsmasq.conf.$(date "+%Y-%m-%d") - $SUDO mv /etc/resolv.conf /etc/pihole/original/resolv.conf.$(date "+%Y-%m-%d") - $SUDO mv /etc/lighttpd/lighttpd.conf /etc/pihole/original/lighttpd.conf.$(date "+%Y-%m-%d") - $SUDO mv /var/www/pihole/index.html /etc/pihole/original/index.html.$(date "+%Y-%m-%d") - $SUDO mv /usr/local/bin/gravity.sh /etc/pihole/original/gravity.sh.$(date "+%Y-%m-%d") + mkdir -p /etc/pihole/original/ + mv /etc/dnsmasq.d/adList.conf /etc/pihole/original/adList.conf.$(date "+%Y-%m-%d") + mv /etc/dnsmasq.conf /etc/pihole/original/dnsmasq.conf.$(date "+%Y-%m-%d") + mv /etc/resolv.conf /etc/pihole/original/resolv.conf.$(date "+%Y-%m-%d") + mv /etc/lighttpd/lighttpd.conf /etc/pihole/original/lighttpd.conf.$(date "+%Y-%m-%d") + mv /var/www/pihole/index.html /etc/pihole/original/index.html.$(date "+%Y-%m-%d") + mv /usr/local/bin/gravity.sh /etc/pihole/original/gravity.sh.$(date "+%Y-%m-%d") else : fi @@ -224,7 +230,7 @@ useIPv6dialog() { piholeIPv6=$(ip -6 route get 2001:4860:4860::8888 | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }') whiptail --msgbox --backtitle "IPv6..." --title "IPv6 Supported" "$piholeIPv6 will be used to block ads." $r $c - $SUDO touch /etc/pihole/.useIPv6 + touch /etc/pihole/.useIPv6 } getStaticIPv4Settings() { @@ -287,7 +293,7 @@ setDHCPCD() { echo "::: interface $piholeInterface static ip_address=$IPv4addr static routers=$IPv4gw - static domain_name_servers=$IPv4gw" | $SUDO tee -a $dhcpcdFile >/dev/null + static domain_name_servers=$IPv4gw" | tee -a $dhcpcdFile >/dev/null } setStaticIPv4() { @@ -297,7 +303,7 @@ setStaticIPv4() { : else setDHCPCD - $SUDO ip addr replace dev $piholeInterface $IPv4addr + ip addr replace dev $piholeInterface $IPv4addr echo ":::" echo "::: Setting IP to $IPv4addr. You may need to restart after the install is complete." echo ":::" @@ -437,66 +443,66 @@ versionCheckDNSmasq(){ if grep -q $dnsSearch $dnsFile1; then echo " it is from a previous pi-hole install." echo -n "::: Backing up dnsmasq.conf to dnsmasq.conf.orig..." - $SUDO mv -f $dnsFile1 $dnsFile2 + mv -f $dnsFile1 $dnsFile2 echo " done." echo -n "::: Restoring default dnsmasq.conf..." - $SUDO cp $defaultFile $dnsFile1 + cp $defaultFile $dnsFile1 echo " done." else echo " it is not a pi-hole file, leaving alone!" fi else echo -n "::: No dnsmasq.conf found.. restoring default dnsmasq.conf..." - $SUDO cp $defaultFile $dnsFile1 + cp $defaultFile $dnsFile1 echo " done." fi echo -n "::: Copying 01-pihole.conf to /etc/dnsmasq.d/01-pihole.conf..." - $SUDO cp $newFileToInstall $newFileFinalLocation + cp $newFileToInstall $newFileFinalLocation echo " done." - $SUDO sed -i "s/@INT@/$piholeInterface/" $newFileFinalLocation + sed -i "s/@INT@/$piholeInterface/" $newFileFinalLocation if [[ "$piholeDNS1" != "" ]]; then - $SUDO sed -i "s/@DNS1@/$piholeDNS1/" $newFileFinalLocation + sed -i "s/@DNS1@/$piholeDNS1/" $newFileFinalLocation else - $SUDO sed -i '/^server=@DNS1@/d' $newFileFinalLocation + sed -i '/^server=@DNS1@/d' $newFileFinalLocation fi if [[ "$piholeDNS2" != "" ]]; then - $SUDO sed -i "s/@DNS2@/$piholeDNS2/" $newFileFinalLocation + sed -i "s/@DNS2@/$piholeDNS2/" $newFileFinalLocation else - $SUDO sed -i '/^server=@DNS2@/d' $newFileFinalLocation + sed -i '/^server=@DNS2@/d' $newFileFinalLocation fi } installScripts() { # Install the scripts from /etc/.pihole to their various locations - $SUDO echo ":::" - $SUDO echo -n "::: Installing scripts..." - $SUDO cp /etc/.pihole/gravity.sh /usr/local/bin/gravity.sh - $SUDO cp /etc/.pihole/advanced/Scripts/chronometer.sh /usr/local/bin/chronometer.sh - $SUDO cp /etc/.pihole/advanced/Scripts/whitelist.sh /usr/local/bin/whitelist.sh - $SUDO cp /etc/.pihole/advanced/Scripts/blacklist.sh /usr/local/bin/blacklist.sh - $SUDO cp /etc/.pihole/advanced/Scripts/piholeLogFlush.sh /usr/local/bin/piholeLogFlush.sh - $SUDO cp /etc/.pihole/advanced/Scripts/updateDashboard.sh /usr/local/bin/updateDashboard.sh - $SUDO chmod 755 /usr/local/bin/{gravity,chronometer,whitelist,blacklist,piholeLogFlush,updateDashboard}.sh - $SUDO echo " done." + echo ":::" + echo -n "::: Installing scripts..." + cp /etc/.pihole/gravity.sh /usr/local/bin/gravity.sh + cp /etc/.pihole/advanced/Scripts/chronometer.sh /usr/local/bin/chronometer.sh + cp /etc/.pihole/advanced/Scripts/whitelist.sh /usr/local/bin/whitelist.sh + cp /etc/.pihole/advanced/Scripts/blacklist.sh /usr/local/bin/blacklist.sh + cp /etc/.pihole/advanced/Scripts/piholeLogFlush.sh /usr/local/bin/piholeLogFlush.sh + cp /etc/.pihole/advanced/Scripts/updateDashboard.sh /usr/local/bin/updateDashboard.sh + chmod 755 /usr/local/bin/{gravity,chronometer,whitelist,blacklist,piholeLogFlush,updateDashboard}.sh + echo " done." } installConfigs() { # Install the configs from /etc/.pihole to their various locations - $SUDO echo ":::" - $SUDO echo "::: Installing configs..." + echo ":::" + echo "::: Installing configs..." versionCheckDNSmasq - $SUDO mv /etc/lighttpd/lighttpd.conf /etc/lighttpd/lighttpd.conf.orig - $SUDO cp /etc/.pihole/advanced/lighttpd.conf /etc/lighttpd/lighttpd.conf + mv /etc/lighttpd/lighttpd.conf /etc/lighttpd/lighttpd.conf.orig + cp /etc/.pihole/advanced/lighttpd.conf /etc/lighttpd/lighttpd.conf } stopServices() { # Stop dnsmasq and lighttpd - $SUDO echo ":::" - $SUDO echo -n "::: Stopping services..." - #$SUDO service dnsmasq stop & spinner $! || true - $SUDO service lighttpd stop & spinner $! || true - $SUDO echo " done." + echo ":::" + echo -n "::: Stopping services..." + #service dnsmasq stop & spinner $! || true + service lighttpd stop & spinner $! || true + echo " done." } checkForDependencies() { @@ -516,12 +522,12 @@ checkForDependencies() { #update package lists echo ":::" echo -n "::: apt-get update has not been run today. Running now..." - $SUDO apt-get -qq update & spinner $! + apt-get -qq update & spinner $! echo " done!" fi echo ":::" echo -n "::: Checking apt-get for upgraded packages...." - updatesToInstall=$($SUDO apt-get -s -o Debug::NoLocking=true upgrade | grep -c ^Inst) + updatesToInstall=$(apt-get -s -o Debug::NoLocking=true upgrade | grep -c ^Inst) echo " done!" echo ":::" if [[ $updatesToInstall -eq "0" ]]; then @@ -541,7 +547,7 @@ checkForDependencies() { echo -n "::: Checking for $i..." if [ $(dpkg-query -W -f='${Status}' $i 2>/dev/null | grep -c "ok installed") -eq 0 ]; then echo -n " Not found! Installing...." - $SUDO apt-get -y -qq install $i > /dev/null & spinner $! + apt-get -y -qq install $i > /dev/null & spinner $! echo " done!" else echo " already installed!" @@ -582,8 +588,8 @@ is_repo() { make_repo() { # Remove the non-repod interface and clone the interface echo -n "::: Cloning $2 into $1..." - $SUDO rm -rf $1 - $SUDO git clone -q "$2" "$1" > /dev/null & spinner $! + rm -rf $1 + git clone -q "$2" "$1" > /dev/null & spinner $! echo " done!" } @@ -591,7 +597,7 @@ update_repo() { # Pull the latest commits echo -n "::: Updating repo in $1..." cd "$1" - $SUDO git pull -q > /dev/null & spinner $! + git pull -q > /dev/null & spinner $! echo " done!" } @@ -599,46 +605,46 @@ update_repo() { CreateLogFile() { # Create logfiles if necessary echo ":::" - $SUDO echo -n "::: Creating log file and changing owner to dnsmasq..." + echo -n "::: Creating log file and changing owner to dnsmasq..." if [ ! -f /var/log/pihole.log ]; then - $SUDO touch /var/log/pihole.log - $SUDO chmod 644 /var/log/pihole.log - $SUDO chown dnsmasq:root /var/log/pihole.log - $SUDO echo " done!" + touch /var/log/pihole.log + chmod 644 /var/log/pihole.log + chown dnsmasq:root /var/log/pihole.log + echo " done!" else - $SUDO echo " already exists!" + echo " already exists!" fi } installPiholeWeb() { # Install the web interface - $SUDO echo ":::" - $SUDO echo -n "::: Installing pihole custom index page..." + echo ":::" + echo -n "::: Installing pihole custom index page..." if [ -d "/var/www/html/pihole" ]; then - $SUDO echo " Existing page detected, not overwriting" + echo " Existing page detected, not overwriting" else - $SUDO mkdir /var/www/html/pihole - $SUDO mv /var/www/html/index.lighttpd.html /var/www/html/index.lighttpd.orig - $SUDO cp /etc/.pihole/advanced/index.html /var/www/html/pihole/index.html - $SUDO echo " done!" + mkdir /var/www/html/pihole + mv /var/www/html/index.lighttpd.html /var/www/html/index.lighttpd.orig + cp /etc/.pihole/advanced/index.html /var/www/html/pihole/index.html + echo " done!" fi } installCron() { # Install the cron job - $SUDO echo ":::" - $SUDO echo -n "::: Installing latest Cron script..." - $SUDO cp /etc/.pihole/advanced/pihole.cron /etc/cron.d/pihole - $SUDO echo " done!" + echo ":::" + echo -n "::: Installing latest Cron script..." + cp /etc/.pihole/advanced/pihole.cron /etc/cron.d/pihole + echo " done!" } runGravity() { # Rub gravity.sh to build blacklists - $SUDO echo ":::" - $SUDO echo "::: Preparing to run gravity.sh to refresh hosts..." + echo ":::" + echo "::: Preparing to run gravity.sh to refresh hosts..." if ls /etc/pihole/list* 1> /dev/null 2>&1; then echo "::: Cleaning up previous install (preserving whitelist/blacklist)" - $SUDO rm /etc/pihole/list.* + rm /etc/pihole/list.* fi #Don't run as SUDO, this was causing issues echo "::: Running gravity.sh" @@ -654,7 +660,7 @@ setUser(){ echo "::: User 'pihole' already exists" else echo "::: User 'pihole' doesn't exist. Creating..." - $SUDO useradd -r -s /usr/sbin/nologin pihole + useradd -r -s /usr/sbin/nologin pihole fi } @@ -663,11 +669,11 @@ installPihole() { checkForDependencies # done stopServices setUser - $SUDO mkdir -p /etc/pihole/ - $SUDO chown www-data:www-data /var/www/html - $SUDO chmod 775 /var/www/html - $SUDO usermod -a -G www-data pihole - $SUDO lighty-enable-mod fastcgi fastcgi-php > /dev/null + mkdir -p /etc/pihole/ + chown www-data:www-data /var/www/html + chmod 775 /var/www/html + usermod -a -G www-data pihole + lighty-enable-mod fastcgi fastcgi-php > /dev/null getGitFiles installScripts @@ -692,7 +698,7 @@ The install log is in /etc/pihole." $r $c ######## SCRIPT ############ # Start the installer -$SUDO mkdir -p /etc/pihole/ +mkdir -p /etc/pihole/ welcomeDialogs # Verify there is enough disk space for the install @@ -712,14 +718,14 @@ setDNS installPihole | tee $tmpLog # Move the log file into /etc/pihole for storage -$SUDO mv $tmpLog $instalLogLoc +mv $tmpLog $instalLogLoc displayFinalMessage echo -n "::: Restarting services..." # Start services -$SUDO service dnsmasq restart -$SUDO service lighttpd start +service dnsmasq restart +service lighttpd start echo " done." echo ":::" diff --git a/automated install/uninstall.sh b/automated install/uninstall.sh index ee4e80a0..949266e0 100644 --- a/automated install/uninstall.sh +++ b/automated install/uninstall.sh @@ -10,40 +10,47 @@ # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. -# Must be root to uninstall +# Check if root, and if not then rerun with sudo. +echo ":::" if [[ $EUID -eq 0 ]];then - echo "You are root." + echo "::: You are root." + # Older versions of Pi-hole set $SUDO="sudo" and prefixed commands with it, + # rather than rerunning as sudo. Just in case it turns up by accident, + # explicitly set the $SUDO variable to an empty string. + SUDO="" else - echo "sudo will be used for the install." - # Check if it is actually installed - # If it isn't, exit because the unnstall cannot complete - if [[ $(dpkg-query -s sudo) ]];then - export SUDO="sudo" - else - echo "Please install sudo or run this as root." - exit 1 - fi + echo "::: sudo will be used." + # Check if it is actually installed + # If it isn't, exit because the install cannot complete + if [[ $(dpkg-query -s sudo) ]];then + echo "::: Running sudo $@" + sudo "$@" + exit $? + else + echo "::: Please install sudo or run this script as root." + exit 1 + fi fi ######### SCRIPT ########### -$SUDO apt-get -y remove --purge dnsutils bc toilet -$SUDO apt-get -y remove --purge dnsmasq -$SUDO apt-get -y remove --purge lighttpd php5-common php5-cgi php5 +apt-get -y remove --purge dnsutils bc toilet +apt-get -y remove --purge dnsmasq +apt-get -y remove --purge lighttpd php5-common php5-cgi php5 # Only web directories/files that are created by pihole should be removed. echo "Removing the Pi-hole Web server files..." -$SUDO rm -rf /var/www/html/admin -$SUDO rm -rf /var/www/html/pihole -$SUDO rm /var/www/html/index.lighttpd.orig +rm -rf /var/www/html/admin +rm -rf /var/www/html/pihole +rm /var/www/html/index.lighttpd.orig # If the web directory is empty after removing these files, then the parent html folder can be removed. if [[ ! "$(ls -A /var/www/html)" ]]; then - $SUDO rm -rf /var/www/html + rm -rf /var/www/html fi echo "Removing dnsmasq config files..." -$SUDO rm /etc/dnsmasq.conf /etc/dnsmasq.conf.orig +rm /etc/dnsmasq.conf /etc/dnsmasq.conf.orig # Attempt to preserve backwards compatibility with older versions # to guarantee no additional changes were made to /etc/crontab after @@ -51,23 +58,23 @@ $SUDO rm /etc/dnsmasq.conf /etc/dnsmasq.conf.orig # preserved. if [[ -f /etc/crontab.orig ]]; then echo "Initial Pi-hole cron detected. Restoring the default system cron..." - $SUDO mv /etc/crontab /etc/crontab.pihole - $SUDO mv /etc/crontab.orig /etc/crontab - $SUDO service cron restart + mv /etc/crontab /etc/crontab.pihole + mv /etc/crontab.orig /etc/crontab + service cron restart fi # Attempt to preserve backwards compatibility with older versions if [[ -f /etc/cron.d/pihole ]];then echo "Removing cron.d/pihole..." - $SUDO rm /etc/cron.d/pihole + rm /etc/cron.d/pihole fi echo "Removing config files and scripts..." -$SUDO rm /etc/dnsmasq.conf -$SUDO rm -rf /etc/lighttpd/ -$SUDO rm /var/log/pihole.log -$SUDO rm /usr/local/bin/gravity.sh -$SUDO rm /usr/local/bin/chronometer.sh -$SUDO rm /usr/local/bin/whitelist.sh -$SUDO rm /usr/local/bin/piholeLogFlush.sh -$SUDO rm -rf /etc/pihole/ +rm /etc/dnsmasq.conf +rm -rf /etc/lighttpd/ +rm /var/log/pihole.log +rm /usr/local/bin/gravity.sh +rm /usr/local/bin/chronometer.sh +rm /usr/local/bin/whitelist.sh +rm /usr/local/bin/piholeLogFlush.sh +rm -rf /etc/pihole/