From a5d1e88fd51740f84e9b2dba06174215c4ba9813 Mon Sep 17 00:00:00 2001 From: Timothy Hofland Date: Tue, 10 Mar 2026 22:54:12 +0100 Subject: [PATCH] fix: harden setup script with robust root check and portable shell syntax --- scripts/setup.sh | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/scripts/setup.sh b/scripts/setup.sh index 36841b2..b62f325 100644 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -12,9 +12,10 @@ if [ -z "$REPO_URL" ]; then exit 1 fi -# Check for root -if [ "$EUID" -ne 0 ]; then - echo "Please run as root (use sudo)" +# 0. ROOT CHECK (Robust) +if [ "$(id -u)" -ne 0 ]; then + echo "ERROR: This script must be run as root." + echo "Please use: sudo bash $0 $@" exit 1 fi @@ -33,7 +34,8 @@ WIFI_SSID=${WIFI_SSID:-MPVJ-AP} while true; do WIFI_COUNTRY=$(whiptail --inputbox "Enter 2-letter Country Code (e.g. US, GB, DE)" 8 45 "US" --title "WiFi Country Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1 WIFI_COUNTRY=$(echo "$WIFI_COUNTRY" | tr '[:lower:]' '[:upper:]') - if [[ ! "$WIFI_COUNTRY" =~ ^[A-Z]{2}$ ]]; then + # Portable regex check using grep + if ! echo "$WIFI_COUNTRY" | grep -qE "^[A-Z]{2}$"; then whiptail --msgbox "Error: Country Code must be exactly 2 letters (e.g., US)." 8 45 < /dev/tty else break @@ -96,18 +98,15 @@ fi # 2.6 Graphics Driver Configuration (KMS/DRM) echo "Configuring graphics drivers for headless operation..." if [ -f /boot/config.txt ]; then - # Increase GPU memory (needed for mapping) if grep -q "gpu_mem=" /boot/config.txt; then sed -i "s/gpu_mem=.*/gpu_mem=256/g" /boot/config.txt else echo "gpu_mem=256" >> /boot/config.txt fi - # Enable KMS driver if not present if ! grep -q "dtoverlay=vc4-kms-v3d" /boot/config.txt; then echo "dtoverlay=vc4-kms-v3d" >> /boot/config.txt fi fi -# Disable boot to desktop (force console auto-login) if command -v raspi-config > /dev/null; then raspi-config nonint do_boot_behaviour B2 > /dev/null 2>&1 fi @@ -133,7 +132,7 @@ EOF sed -i 's/#DAEMON_CONF=""/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/g' /etc/default/hostapd # 3.2 dnsmasq -mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak 2>/dev/null || true +[ -f /etc/dnsmasq.conf ] && mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak cat < /etc/dnsmasq.conf interface=wlan0 dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h @@ -223,7 +222,6 @@ if [ ! -d "openFrameworks" ]; then cd openFrameworks/scripts/linux/debian ./install_dependencies.sh -y > /dev/null 2>&1 cd ../../../ - # Compile OF (this takes a while) cd scripts/linux/debian ./install_codecs.sh > /dev/null 2>&1 cd ../../../ @@ -233,14 +231,13 @@ fi cd /home/pi/openFrameworks/addons if [ ! -d "ofxPiMapper" ]; then sudo -u pi git clone https://github.com/kr15h/ofxPiMapper.git - sudo -u pi git clone https://github.com/vanderlin/ofxOfelia.git # dependency often needed + sudo -u pi git clone https://github.com/vanderlin/ofxOfelia.git fi -# Build Example +# Build Example with Highlight Patch cd /home/pi/openFrameworks/addons/ofxPiMapper if [ -f src/Osc/OscControl.cpp ]; then echo "Applying highlight surface OSC listener modification..." - # Add highlight listener that selects the surface for 1 second (mock highlight) sed -i '/if (m.getAddress() == "\/ofxPiMapper\/surface\/select"){/i \ if (m.getAddress() == "/ofxPiMapper/surface/highlight"){ \ int surfaceIndex = m.getArgAsInt32(0); \ @@ -249,7 +246,6 @@ if [ -f src/Osc/OscControl.cpp ]; then }' src/Osc/OscControl.cpp fi -# Build Example cd /home/pi/openFrameworks/addons/ofxPiMapper/example-basic sudo -u pi make -j1 > /dev/null 2>&1 cp bin/example-basic /usr/local/bin/ofxPiMapper