From fa877fa73d2422b5284763bcb298f79bf7b6a502 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 12 Jul 2015 10:29:43 +0200 Subject: [PATCH] Add update and clean script --- README.md | 10 ++++++++ clean.sh | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ update.sh | 41 ++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100755 clean.sh create mode 100755 update.sh diff --git a/README.md b/README.md index 900aff4..429c5a7 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,16 @@ Only tested on Debian Jessie. Feel free to open issues. * User get the configurations files via the web application * User run OpenVPN (for example `systemctl start openvpn@client`) +## Update + + git pull origin master + ./update.sh www_base_dir + +## Desinstall +It will remove all installed components (OpenVPN keys and configurations, the web application, iptables rules...). + + ./clean.sh www_base_dir + ## Use of * [Bootstrap](https://github.com/twbs/bootstrap) diff --git a/clean.sh b/clean.sh new file mode 100755 index 0000000..e510c19 --- /dev/null +++ b/clean.sh @@ -0,0 +1,74 @@ +#!/bin/bash + +print_help () { + echo -e "./clean.sh www_basedir" + echo -e "\tbase_dir: The place where the web application is in" +} + +# Ensure to be root +if [ "$EUID" -ne 0 ]; then + echo "Please run as root" + exit +fi + +# Ensure there are enought arguments +if [ "$#" -ne 1 ]; then + print_help + exit +fi + +www="$1/openvpn-admin" + +if [ ! -d "$www" ]; then + print_help + exit +fi + +# Get root pass (to delete the database and the user) +mysql_root_pass="" +status_code=1 + +while [ $status_code -ne 0 ]; do + echo -n "Server MySQL root password: " + read mysql_root_pass + echo "SHOW DATABASES" | mysql -u root --password="$mysql_root_pass" &> /dev/null + status_code=$? +done + +mysql_user=$(sed -n "s/^.*user = '\(.*\)'.*$/\1/p" "$www/include/config.php") + +if [ "$mysql_user" = "" ]; then + echo "Can't find the MySQL user. Please ensure your include/config.php is well structured or report an issue" + exit +fi + +echo "Are you sure to completely delete OpenVPN configurations, the web application (with the MySQL user/database) and the iptables rules? (yes/*)" +read agree + +if [ "$agree" != "yes" ]; then + exit +fi + +# MySQL delete +echo "DROP USER $mysql_user@localhost" | mysql -u root --password="$mysql_root_pass" +echo "DROP DATABASE \`openvpn-admin\`" | mysql -u root --password="$mysql_root_pass" + +# Files delete (openvpn confs/keys + web application) +rm -r /etc/openvpn/easy-rsa/ +rm -r /etc/openvpn/{scripts,server.conf,ca.crt,ta.key,server.crt,server.key,dh*.pem} +rm -r "$www" + +# Remove rooting rules +echo 0 > "/proc/sys/net/ipv4/ip_forward" +sed -i '/net.ipv4.ip_forward = 1/d' '/etc/sysctl.conf' + +iptables -D FORWARD -i tun0 -j ACCEPT +iptables -D FORWARD -o tun0 -j ACCEPT +iptables -D OUTPUT -o tun0 -j ACCEPT + +iptables -D FORWARD -i tun0 -o eth0 -j ACCEPT +iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE +iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE +iptables -t nat -D POSTROUTING -s 10.8.0.2/24 -o eth0 -j MASQUERADE + +echo "The application has been completely removed" diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..d7d07fe --- /dev/null +++ b/update.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +print_help () { + echo -e "./update.sh www_basedir" + echo -e "\tbase_dir: The place where the web application is in" +} + +# Ensure to be root +if [ "$EUID" -ne 0 ]; then + echo "Please run as root" + exit +fi + +# Ensure there are enought arguments +if [ "$#" -ne 1 ]; then + print_help + exit +fi + +www="$1/openvpn-admin" + +if [ ! -d "$www" ]; then + print_help + exit +fi + +base_path=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +user=$(ls -l "$www/include/config.php" | awk '{ print $3 }') +group=$(ls -l "$www/include/config.php" | awk '{ print $4 }') + + +rm -r "${www:?}/"{index.php,bower.json,.bowerrc,js,include/html,include/connect.php,include/functions.php,include/grids.php,css} + +cp -r "$base_path/"{index.php,bower.json,.bowerrc,js,css} "$www" +cp -r "$base_path/include/"{html,connect.php,functions.php,grids.php} "$www/include" + +cd "$www" + +bower --allow-root install +chown -R "$user:$group" "$www"