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

233 lines
8.8 KiB
Bash
Raw Normal View History

2016-06-16 15:16:19 +02:00
raspap_dir="/etc/raspap"
raspap_user="www-data"
version=`sed 's/\..*//' /etc/debian_version`
# Determine version and set default home location for lighttpd
if [ $version -ge 8 ]; then
2017-05-19 18:33:01 +02:00
version_msg="Raspian version 8.0 or later"
webroot_dir="/var/www/html"
2017-01-27 10:31:26 +01:00
else
2017-05-19 18:33:01 +02:00
version_msg="Raspian version earlier than 8.0"
webroot_dir="/var/www"
fi
2016-06-16 15:16:19 +02:00
2017-05-19 18:33:01 +02:00
# Outputs a RaspAP Install log line
2016-06-16 15:16:19 +02:00
function install_log() {
2017-05-19 18:33:01 +02:00
echo -e "\033[1;32mRaspAP Install: $*\033[m"
2016-06-16 15:16:19 +02:00
}
2017-05-19 18:33:01 +02:00
# Outputs a RaspAP Install Error log line and exits with status code 1
2016-06-16 15:16:19 +02:00
function install_error() {
2017-05-19 18:33:01 +02:00
echo -e "\033[1;37;41mRaspAP Install Error: $*\033[m"
2016-06-16 15:16:19 +02:00
exit 1
}
2017-05-19 18:33:01 +02:00
# Outputs a welcome message
function display_welcome() {
raspberry='\033[0;35m'
green='\033[1;32m'
echo -e "${raspberry}\n"
echo -e " 888888ba .d888888 888888ba"
echo -e " 88 8b d8 88 88 8b"
echo -e "a88aaaa8P' .d8888b. .d8888b. 88d888b. 88aaaaa88a a88aaaa8P"
echo -e " 88 8b. 88 88 Y8ooooo. 88 88 88 88 88"
echo -e " 88 88 88. .88 88 88. .88 88 88 88"
echo -e " dP dP 88888P8 88888P 88Y888P 88 88 dP"
echo -e " 88"
echo -e " dP"
echo -e "${green}"
echo -e "The Quick Installer will guide you through a few easy steps\n\n"
}
2016-06-16 15:16:19 +02:00
### NOTE: all the below functions are overloadable for system-specific installs
### NOTE: some of the below functions MUST be overloaded due to system-specific installs
function config_installation() {
install_log "Configure installation"
2017-05-19 18:33:01 +02:00
echo "Detected ${version_msg}"
echo "Install directory: ${raspap_dir}"
2017-05-19 18:33:01 +02:00
echo "Lighttpd directory: ${webroot_dir}"
2016-06-16 15:16:19 +02:00
echo -n "Complete installation with these values? [y/N]: "
read answer
if [[ $answer != "y" ]]; then
echo "Installation aborted."
exit 0
fi
}
# Runs a system software update to make sure we're using all fresh packages
function update_system_packages() {
# OVERLOAD THIS
install_error "No function definition for update_system_packages"
}
# Installs additional dependencies using system package manager
function install_dependencies() {
# OVERLOAD THIS
install_error "No function definition for install_dependencies"
}
# Enables PHP for lighttpd and restarts service for settings to take effect
function enable_php_lighttpd() {
install_log "Enabling PHP for lighttpd"
sudo lighty-enable-mod fastcgi-php
if [ $? -eq 2 ]; then echo already enabled; else install_error "Cannot enable fastcgi-php for lighttpd"; fi
sudo /etc/init.d/lighttpd restart || install_error "Unable to restart lighttpd"
2016-06-16 15:16:19 +02:00
}
# Verifies existence and permissions of RaspAP directory
function create_raspap_directories() {
install_log "Creating RaspAP directories"
if [ -d "$raspap_dir" ]; then
2017-10-01 21:36:51 +02:00
sudo mv $raspap_dir "$raspap_dir.`date +%F-%R`" || install_error "Unable to move old '$raspap_dir' out of the way"
2016-06-16 15:16:19 +02:00
fi
sudo mkdir -p "$raspap_dir" || install_error "Unable to create directory '$raspap_dir'"
# Create a directory for existing file backups.
sudo mkdir -p "$raspap_dir/backups"
2016-06-16 15:16:19 +02:00
sudo chown -R $raspap_user:$raspap_user "$raspap_dir" || install_error "Unable to change file ownership for '$raspap_dir'"
}
# Fetches latest files from github to webroot
function download_latest_files() {
2016-10-23 17:39:33 +02:00
if [ -d "$webroot_dir" ]; then
2017-10-01 21:36:51 +02:00
sudo mv $webroot_dir "$webroot_dir.`date +%F-%R`" || install_error "Unable to remove old webroot directory"
2016-06-16 15:16:19 +02:00
fi
install_log "Cloning latest files from github"
2016-10-23 17:39:33 +02:00
git clone https://github.com/billz/raspap-webgui /tmp/raspap-webgui || install_error "Unable to download files from github"
sudo mv /tmp/raspap-webgui $webroot_dir || install_error "Unable to move raspap-webgui to web root"
2016-06-16 15:16:19 +02:00
}
# Sets files ownership in web root directory
function change_file_ownership() {
if [ ! -d "$webroot_dir" ]; then
install_error "Web root directory doesn't exist"
fi
install_log "Changing file ownership in web root directory"
sudo chown -R $raspap_user:$raspap_user "$webroot_dir" || install_error "Unable to change file ownership for '$webroot_dir'"
}
# Check for existing /etc/network/interfaces and /etc/hostapd/hostapd.conf files
function check_for_old_configs() {
if [ -f /etc/network/interfaces ]; then
sudo cp /etc/network/interfaces "$raspap_dir/backups/interfaces.`date +%F-%R`"
sudo ln -sf "$raspap_dir/backups/interfaces.`date +%F-%R`" "$raspap_dir/backups/interfaces"
fi
if [ -f /etc/hostapd/hostapd.conf ]; then
sudo cp /etc/hostapd/hostapd.conf "$raspap_dir/backups/hostapd.conf.`date +%F-%R`"
sudo ln -sf "$raspap_dir/backups/hostapd.conf.`date +%F-%R`" "$raspap_dir/backups/hostapd.conf"
fi
if [ -f /etc/dnsmasq.conf ]; then
sudo cp /etc/dnsmasq.conf "$raspap_dir/backups/dnsmasq.conf.`date +%F-%R`"
sudo ln -sf "$raspap_dir/backups/dnsmasq.conf.`date +%F-%R`" "$raspap_dir/backups/dnsmasq.conf"
fi
if [ -f /etc/dhcpcd.conf ]; then
sudo cp /etc/dhcpcd.conf "$raspap_dir/backups/dhcpcd.conf.`date +%F-%R`"
sudo ln -sf "$raspap_dir/backups/dhcpcd.conf.`date +%F-%R`" "$raspap_dir/backups/dhcpcd.conf"
fi
}
2016-06-16 15:16:19 +02:00
# Move configuration file to the correct location
function move_config_file() {
if [ ! -d "$raspap_dir" ]; then
install_error "'$raspap_dir' directory doesn't exist"
fi
install_log "Moving configuration file to '$raspap_dir'"
sudo mv "$webroot_dir"/raspap.php "$raspap_dir" || install_error "Unable to move files to '$raspap_dir'"
sudo chown -R $raspap_user:$raspap_user "$raspap_dir" || install_error "Unable to change file ownership for '$raspap_dir'"
}
2016-10-23 17:39:33 +02:00
# Set up default configuration
function default_configuration() {
install_log "Setting up hostapd"
if [ -f /etc/default/hostapd ]; then
sudo mv /etc/default/hostapd /tmp/default_hostapd.old || install_error "Unable to remove old /etc/default/hostapd file"
fi
sudo mv $webroot_dir/config/default_hostapd /etc/default/hostapd || install_error "Unable to move hostapd defaults file"
sudo mv $webroot_dir/config/hostapd.conf /etc/hostapd/hostapd.conf || install_error "Unable to move hostapd configuration file"
sudo mv $webroot_dir/config/dnsmasq.conf /etc/dnsmasq.conf || install_error "Unable to move dnsmasq configuration file"
sudo mv $webroot_dir/config/dhcpcd.conf /etc/dhcpcd.conf || install_error "Unable to move dhcpcd configuration file"
sudo mv $webroot_dir/config/rc.local /etc/rc.local || install_error "Unable to move rc.local file"
2016-10-23 17:39:33 +02:00
}
# Add a single entry to the sudoers file
function sudo_add() {
sudo bash -c "echo \"www-data ALL=(ALL) NOPASSWD:$1\" | (EDITOR=\"tee -a\" visudo)" \
|| install_error "Unable to patch /etc/sudoers"
}
2016-06-16 15:16:19 +02:00
# Adds www-data user to the sudoers file with restrictions on what the user can execute
function patch_system_files() {
# Set commands array
cmds=(
'/sbin/ifdown wlan0'
'/sbin/ifup wlan0'
'/bin/cat /etc/wpa_supplicant/wpa_supplicant.conf'
'/bin/cp /tmp/wifidata /etc/wpa_supplicant/wpa_supplicant.conf'
'/sbin/wpa_cli scan_results'
'/sbin/wpa_cli scan'
'/sbin/wpa_cli reconfigure'
'/bin/cp /tmp/hostapddata /etc/hostapd/hostapd.conf'
'/etc/init.d/hostapd start'
'/etc/init.d/hostapd stop'
'/etc/init.d/dnsmasq start'
'/etc/init.d/dnsmasq stop'
'/bin/cp /tmp/dhcpddata /etc/dnsmasq.conf'
'/sbin/shutdown -h now'
'/sbin/reboot'
'/sbin/ip link set wlan0 down'
'/sbin/ip link set wlan0 up'
'/sbin/ip -s a f label wlan0'
)
# Check if sudoers needs patchin
if [ $(sudo grep -c www-data /etc/sudoers) -ne 15 ]; then
# Sudoers file has incorrect number of commands. Wiping them out.
install_log "Cleaning sudoers file"
sudo sed -i '/www-data/d' /etc/sudoers
install_log "Patching system sudoers file"
# patch /etc/sudoers file
for cmd in "${cmds[@]}"; do
sudo_add $cmd
done
else
install_log "Sudoers file already patched"
fi
2016-06-16 15:16:19 +02:00
}
function install_complete() {
install_log "Installation completed!"
2016-06-16 15:16:19 +02:00
echo -n "The system needs to be rebooted as a final step. Reboot now? [y/N]: "
read answer
if [[ $answer != "y" ]]; then
echo "Installation aborted."
exit 0
fi
2016-10-23 18:37:29 +02:00
sudo shutdown -r now || install_error "Unable to execute shutdown"
2016-06-16 15:16:19 +02:00
}
function install_raspap() {
2017-05-19 18:33:01 +02:00
display_welcome
2016-06-16 15:16:19 +02:00
config_installation
update_system_packages
install_dependencies
enable_php_lighttpd
create_raspap_directories
check_for_old_configs
2016-06-16 15:16:19 +02:00
download_latest_files
change_file_ownership
move_config_file
2016-10-23 17:39:33 +02:00
default_configuration
2016-06-16 15:16:19 +02:00
patch_system_files
install_complete
2016-08-14 18:40:59 +02:00
}