From fd21237b27bd1aea0d1f93c8b9e92509da16192d Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 27 Oct 2015 20:35:37 -0500 Subject: [PATCH 1/4] Update gravity.sh sudo errors as root When gravity.sh is run as the root user, it errors out on the sudo command. These changes make the script check to see if it is invoked by the root and sets the sudo variable to handle root and non-root runs. --- gravity.sh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/gravity.sh b/gravity.sh index f1b7aea0..82e87ae5 100755 --- a/gravity.sh +++ b/gravity.sh @@ -5,6 +5,11 @@ # This script should only be run after you have a static IP address set on the Pi piholeIP=$(hostname -I) +#Checks if the script is being run as root and sets sudo accordingly +SUDO='' +if (( $EUID !=0 )); then SUDO='sudo' +fi + # Ad-list sources--one per line in single quotes sources=('https://adaway.org/hosts.txt' 'http://adblock.gjtech.net/?format=unix-hosts' @@ -45,7 +50,7 @@ if [[ -d $piholeDir ]];then : else echo "** Creating pihole directory..." - sudo mkdir $piholeDir + $SUDO mkdir $piholeDir fi # Add additional swap to prevent the "Error fork: unable to allocate memory" message: https://github.com/jacobsalmela/pi-hole/issues/37 @@ -53,10 +58,10 @@ function createSwapFile() ######################### { echo "** Creating more swap space to accomodate large solar masses..." - sudo dphys-swapfile swapoff - sudo curl -s -o /etc/dphys-swapfile https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/dphys-swapfile - sudo dphys-swapfile setup - sudo dphys-swapfile swapon + $SUDO dphys-swapfile swapoff + $SUDO curl -s -o /etc/dphys-swapfile https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/dphys-swapfile + $SUDO dphys-swapfile setup + $SUDO dphys-swapfile swapon } @@ -138,7 +143,7 @@ function gravity_advanced() echo "** Formatting domains into a HOSTS file..." cat $origin/$eventHorizon | awk '{sub(/\r$/,""); print "'"$piholeIP"'" $0}' > $origin/$accretionDisc # Copy the file over as /etc/pihole/gravity.list so dnsmasq can use it - sudo cp $origin/$accretionDisc $adList + $SUDO cp $origin/$accretionDisc $adList kill -HUP $(pidof dnsmasq) } From d756f430d8e0a686c8f792acf30972521d1f4fee Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 5 Nov 2015 12:55:13 -0600 Subject: [PATCH 2/4] Checks if the script is being run as root These changes check to see if it installer is invoked by the root and sets the sudo variable to handle root and non-root runs. --- automated install/basic-install.sh | 89 ++++++++++++++++-------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index 7c48fdba..d9f3cbb4 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -24,70 +24,75 @@ echo " Press Enter when ready " echo " " read +#Checks if the script is being run as root and sets sudo accordingly +SUDO='' +if (( $EUID !=0 )); then SUDO='sudo' +fi + 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") + $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") else : fi echo "Updating the Pi..." -sudo apt-get update -sudo apt-get -y upgrade +$SUDO apt-get update +$SUDO apt-get -y upgrade echo "Installing tools..." -sudo apt-get -y install dnsutils -sudo apt-get -y install bc -sudo apt-get -y install toilet +$SUDO apt-get -y install dnsutils +$SUDO apt-get -y install bc +$SUDO apt-get -y install toilet echo "Installing DNS..." -sudo apt-get -y install dnsmasq -sudo update-rc.d dnsmasq enable +$SUDO apt-get -y install dnsmasq +$SUDO update-rc.d dnsmasq enable echo "Installing a Web server" -sudo apt-get -y install lighttpd php5-common php5-cgi php5 -sudo mkdir /var/www/html -sudo chown www-data:www-data /var/www/html -sudo chmod 775 /var/www/html -sudo usermod -a -G www-data pi +$SUDO apt-get -y install lighttpd php5-common php5-cgi php5 +$SUDO mkdir /var/www/html +$SUDO chown www-data:www-data /var/www/html +$SUDO chmod 775 /var/www/html +$SUDO usermod -a -G www-data pi echo "Stopping services to modify them..." -sudo service dnsmasq stop -sudo service lighttpd stop +$SUDO service dnsmasq stop +$SUDO service lighttpd stop echo "Backing up original config files and downloading Pi-hole ones..." -sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig -sudo mv /etc/lighttpd/lighttpd.conf /etc/lighttpd/lighttpd.conf.orig -sudo mv /var/www/html/index.lighttpd.html /var/www/html/index.lighttpd.orig -sudo curl -o /etc/dnsmasq.conf "https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/dnsmasq.conf" -sudo curl -o /etc/lighttpd/lighttpd.conf "https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/lighttpd.conf" -sudo lighty-enable-mod fastcgi fastcgi-php -sudo mkdir /var/www/html/pihole -sudo curl -o /var/www/html/pihole/index.html "https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/index.html" +$SUDO mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig +$SUDO mv /etc/lighttpd/lighttpd.conf /etc/lighttpd/lighttpd.conf.orig +$SUDO mv /var/www/html/index.lighttpd.html /var/www/html/index.lighttpd.orig +$SUDO curl -o /etc/dnsmasq.conf "https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/dnsmasq.conf" +$SUDO curl -o /etc/lighttpd/lighttpd.conf "https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/lighttpd.conf" +$SUDO lighty-enable-mod fastcgi fastcgi-php +$SUDO mkdir /var/www/html/pihole +$SUDO curl -o /var/www/html/pihole/index.html "https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/index.html" echo "Installing the Web interface..." -sudo wget https://github.com/jacobsalmela/AdminLTE/archive/master.zip -O /var/www/master.zip -sudo unzip /var/www/master.zip -d /var/www/html/ -sudo mv /var/www/html/AdminLTE-master /var/www/html/admin -sudo rm /var/www/master.zip 2>/dev/null -sudo touch /var/log/pihole.log -sudo chmod 644 /var/log/pihole.log -sudo chown dnsmasq:root /var/log/pihole.log +$SUDO wget https://github.com/jacobsalmela/AdminLTE/archive/master.zip -O /var/www/master.zip +$SUDO unzip /var/www/master.zip -d /var/www/html/ +$SUDO mv /var/www/html/AdminLTE-master /var/www/html/admin +$SUDO rm /var/www/master.zip 2>/dev/null +$SUDO touch /var/log/pihole.log +$SUDO chmod 644 /var/log/pihole.log +$SUDO chown dnsmasq:root /var/log/pihole.log echo "Locating the Pi-hole..." -sudo curl -o /usr/local/bin/gravity.sh "https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/gravity.sh" -sudo curl -o /usr/local/bin/chronometer.sh "https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/chronometer.sh" -sudo chmod 755 /usr/local/bin/gravity.sh -sudo chmod 755 /usr/local/bin/chronometer.sh +$SUDO curl -o /usr/local/bin/gravity.sh "https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/gravity.sh" +$SUDO curl -o /usr/local/bin/chronometer.sh "https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/chronometer.sh" +$SUDO chmod 755 /usr/local/bin/gravity.sh +$SUDO chmod 755 /usr/local/bin/chronometer.sh echo "Entering the event horizon..." -sudo /usr/local/bin/gravity.sh +$SUDO /usr/local/bin/gravity.sh echo "Restarting..." -sudo reboot +$SUDO reboot From b5a0ffe8c1773c15e8a0e4f4d26e45cdf45eb5bd Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 5 Nov 2015 22:47:56 -0600 Subject: [PATCH 3/4] Checks for root/non-root/sudo Checks if script is running as root, non-root, or sudo. For root, it sets SUDO variable to nothing and the script runs. If it's not root, then it checks to see if sudo is installed. If it is installed, then it sets SUDO variable to 'sudo' and the script runs. If it is not root and sudo is not installed, then the user does not have sufficient privileges to run the script and it exits. --- gravity.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gravity.sh b/gravity.sh index 82e87ae5..6b4a4b74 100755 --- a/gravity.sh +++ b/gravity.sh @@ -6,8 +6,13 @@ piholeIP=$(hostname -I) #Checks if the script is being run as root and sets sudo accordingly -SUDO='' -if (( $EUID !=0 )); then SUDO='sudo' +echo "Checking if running as root..." +if (( $EUID==0 )); then SUDO='' +echo "WE ARE ROOT!" +elif [ $(dpkg-query -s -f='${Status}' sudo 2>/dev/null | grep -c "ok installed") -eq 1 ]; then SUDO='sudo' +echo "sudo IS installed... setting SUDO to sudo!" +else echo "Sudo NOT found AND not ROOT! Must run script as root!" +exit 1 fi # Ad-list sources--one per line in single quotes From c168bf4f2ef8861d30b1c0baf580ff24fc13680c Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 5 Nov 2015 22:53:12 -0600 Subject: [PATCH 4/4] Checks for root/non-root/sudo Checks if script is running as root, non-root, or sudo. For root, it sets SUDO variable to nothing and the script runs. If it's not root, then it checks to see if sudo is installed. If it is installed, then it sets SUDO variable to 'sudo' and the script runs. If it is not root and sudo is not installed, then the user does not have sufficient privileges to run the installer and it exits. --- automated install/basic-install.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index d9f3cbb4..06f98d10 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -25,8 +25,13 @@ echo " " read #Checks if the script is being run as root and sets sudo accordingly -SUDO='' -if (( $EUID !=0 )); then SUDO='sudo' +echo "Checking if running as root..." +if (( $EUID==0 )); then SUDO='' +echo "WE ARE ROOT!" +elif [ $(dpkg-query -s -f='${Status}' sudo 2>/dev/null | grep -c "ok installed") -eq 1 ]; then SUDO='sudo' +echo "sudo IS installed... setting SUDO to sudo!" +else echo "Sudo NOT found AND not ROOT! Must run script as root!" +exit 1 fi if [[ -f /etc/dnsmasq.d/adList.conf ]];then