#!/bin/bash
# server-setup.sh
# OpenDRS Online Discrepancy Reporting System
# Copyright (C) 2022 Rod Wright
# SPDX-License-Identifier: GPL-2.0
inst_name="OpenDRS"
docroot_path="/var/www"
apache_user="www-data"
apache_group="www-data"
current_user=`whoami`
if [ "$current_user" != "root" ]
then
echo "You must have root privileges to run this setup."
echo "Try 'sudo ./server-setup.sh'"
exit 1
fi
echo ""
echo ""
echo ""
echo ""
echo "This script will configure a Raspberry Pi to be an OpenDRS server."
echo ""
echo ""
echo "You should have already run the terminal-setup.sh script in this directory."
echo ""
if [ ! -e /etc/drs.conf ]
then
echo "It looks like the terminal-setup.sh script has not been run yet."
echo "This is required before running the server-setup.sh script."
echo "Aborting."
exit 1
fi
echo "It looks like you have. Continuing."
echo ""
echo ""
echo "Updating Raspberry Pi OS..."
sleep 10
echo ""
echo ""
echo ""
echo ""
apt update && apt dist-upgrade -y
echo ""
echo ""
echo ""
echo ""
echo "...done."
sleep 10
echo ""
echo ""
echo ""
echo ""
echo "IMPORTANT!: Be sure to make note of usernames and passwords you provide below."
echo "You will need them later."
echo ""
echo "Installing required server packages. You will be prompted about a couple of things:"
echo ""
echo "- Select apache2 as the web server to configure automatically."
echo ""
echo "- Choose Yes when prompted to install phpmyadmin database and allow it to"
echo " generate a random password(leave the field blank)."
echo ""
echo -n "Press enter to continue."
read continueok
echo ""
echo ""
echo ""
echo ""
apt install -y apache2 php mariadb-server phpmyadmin uuid-runtime
apt install -y php-mysqli
cp etc/apache2/mods-available/alias.conf /etc/apache2/mods-available
service apache2 restart
echo "...done."
echo ""
echo ""
echo "Configuring the MariaDB installation. There will be some more prompts:"
echo ""
echo "- You'll be prompted for the current password for the root user. You've"
echo " just installed MariaDB and you haven't set one yet, so just press enter."
echo ""
echo "- You'll be prompted to switch to unix_socket authentication. Answer Y."
echo ""
echo "- You'll be prompted to change the root password. Answer Y and enter a password."
echo " Note that this is just the password for the MariaDB root user, not the terminal."
echo " DO NOT FORGET THIS PASSWORD. You'll need it later during OpenDRS install."
echo ""
echo "- Answer Y to remove anonymous users, disallow root login remotely, remove"
echo " test database and access, and reload privilege tables."
echo ""
echo -n "Press enter to proceed."
read continueok
echo ""
echo ""
echo ""
echo ""
mysql_secure_installation
echo ""
echo ""
# This is a fresh install
operation="installed"
# prompt for mysql information
bad_mysql_adm=1
while [ $bad_mysql_adm -eq 1 ]
do
admintestdb="drsadm`date +%Y%m%d`"
echo "Starting OpenDRS installation."
echo ""
echo "We need to know the username and password of a MySQL administrator"
echo "to be able to create the database and user for OpenDRS. The username"
echo "will be root and the password will be the one you were urged to remember"
echo "a minute ago."
echo ""
echo -n "MySQL admin username: "
read mysql_adm_user
echo ""
echo -n "MySQL admin password: "
read -s mysql_adm_pass
export MYSQL_PWD="$mysql_adm_pass"
mysql -u"$mysql_adm_user" -e "create database $admintestdb;drop database $admintestdb"
if [ $? -eq 0 ]
then
bad_mysql_adm=0
else
echo "ERROR: Either the username/password you supplied were incorrect or the user"
echo -n "is not a MySQL administrator. Try again? [y/N]: "
read admtry
if [ "$admtry" != "Y" -o "$admtry" != "y" ]
then
exit
fi
fi
done
echo ""
echo "What would you like to call this installation of OpenDRS? If you accept"
echo "the default, it will be called $inst_name. If you will be running multiple"
echo "instances of OpenDRS on this server, you should choose a distinctive name."
echo -n "Installation name [$inst_name]: "
read inst_name_in
if [ "$inst_name_in" != "" ]
then
inst_name=$inst_name_in
fi
echo ""
echo ""
echo ""
echo ""
# Create initial database
db_create_fail=1
while [ $db_create_fail -eq 1 ]
do
name_valid=0
while [ $name_valid -eq 0 ]
do
echo "What would you like to call the database for this installation of OpenDRS?"
echo "If you accept the default or make it blank, it will be $inst_name."
echo "Spaces or special characters are not allowed in the name."
echo -n "database name [$inst_name]: "
read new_db_name
if [ "$new_db_name" = "" ]
then
new_db_name=$inst_name
fi
echo $new_db_name|egrep -q "\W"
if [ $? -eq 0 ]
then
echo "The name you entered seems to contain whitespace or special"
echo "characters. Try again or just accept the default."
else
name_valid=1
fi
done
echo ""
export MYSQL_PWD="$mysql_adm_pass"
mysql -u"$mysql_adm_user" -e "create database $new_db_name"
if [ $? -eq 0 ]
then
db_create_fail=0
else
echo "An error was encountered when trying to create the database."
echo "This probably means the database already exists."
echo -n "Try again with a different database name? [Y/N]: "
read dbcreatetry
if [ "$dbcreatetry" != "Y" -o "$dbcreatetry" != "y" ]
then
exit
fi
fi
done
# Create user and give rights to database
drs_user="$new_db_name-user"
drs_pass=`uuidgen`
export MYSQL_PWD="$mysql_adm_pass"
mysql -u"$mysql_adm_user" -e "create user if not exists '$drs_user'@'localhost' identified by '$drs_pass'"
mysql -u"$mysql_adm_user" -e "grant all on $new_db_name.* to '$drs_user'@'localhost'"
mysql -u"$mysql_adm_user" -e "flush privileges"
# Populate initial database
mysql -u"$mysql_adm_user" $new_db_name < ../opendrs-initial.sql
# Set the installation name
export MYSQL_PWD="$drs_pass"
mysql -u"$drs_user" $new_db_name -e "update config set value=\"$inst_name\",defaultvalue=\"$inst_name\" where name=\"app_name\""
# Create install path
echo ""
echo ""
echo ""
echo ""
echo "Creating installation directory . . ."
echo ""
echo ""
echo ""
echo ""
echo "Installing to $docroot_path/$inst_name"
sleep 10
install_path=$docroot_path/$inst_name
mkdir $install_path
# Create db.php if it doesn't exist
if [ ! -f "$install_path/db.php" ]
then
cp ../db.php.template $install_path/db.php
sed -i "s/DBNAME/$new_db_name/g" $install_path/db.php
sed -i "s/DBUSER/$drs_user/g" $install_path/db.php
sed -i "s/DBPASS/$drs_pass/g" $install_path/db.php
fi
# copy distribution to install location
echo "Installing distribution . . ."
cp -Rv ../distfiles/* $install_path
ln -s $install_path/writeups.php $install_path/index.php
if [ ! -L "$install_path/jquery-ui" ]
then
ln -s $install_path/jquery-ui* $install_path/jquery-ui
fi
echo ""
echo ""
echo ""
echo ""
# set ownership
echo "In order to set the ownership correctly, we need to know the user and"
echo "group that the webserver expects its files to be owned by. This is"
echo "usually not the root account. Refer to the ownership of other files in"
echo "your webserver's document tree to see what this should be."
echo "Enter the user and group separated by a colon (username:groupname)."
echo "Enter the user:group of the OpenDRS installation"
echo -n "directory [$apache_user:$apache_group] :"
read httpd_user_group_in
if [ "$httpd_user_group_in" != "" ]
then
httpd_user_group="$httpd_user_group_in"
else
httpd_user_group="$apache_user:$apache_group"
fi
echo ""
echo "Setting file ownership . . ."
chown -R $httpd_user_group $install_path
echo ""
# tell user to launch web browser to continue
echo ""
echo ""
echo ""
echo "OpenDRS has been installed."
echo "# OpenDRS default Apache configuration" > /etc/apache2/conf-available/$inst_name.conf
echo "" >> /etc/apache2/conf-available/$inst_name.conf
echo "Alias /$inst_name $install_path" >> /etc/apache2/conf-available/$inst_name.conf
echo "" >> /etc/apache2/conf-available/$inst_name.conf
echo "