2015-07-09 14:26:07 +02:00
#!/bin/bash
print_help ( ) {
echo -e "./install.sh www_basedir user group"
echo -e "\tbase_dir: The place where the web application will be put in"
echo -e "\tuser: User of the web application"
echo -e "\tgroup: Group of the web application"
}
# Ensure to be root
2016-08-26 14:38:13 +02:00
if [ " $EUID " -ne 0 ] ; then
2015-07-09 14:26:07 +02:00
echo "Please run as root"
exit
fi
# Ensure there are enought arguments
if [ " $# " -ne 3 ] ; then
print_help
exit
fi
2015-08-07 19:20:31 +02:00
# Ensure there are the prerequisites
for i in openvpn mysql php bower node unzip wget sed; do
which $i > /dev/null
if [ " $? " -ne 0 ] ; then
echo " Miss $i "
exit
fi
done
2015-07-09 14:26:07 +02:00
www = $1
user = $2
group = $3
openvpn_admin = " $www /openvpn-admin "
# Check the validity of the arguments
if [ ! -d " $www " ] || ! grep -q " $user " "/etc/passwd" || ! grep -q " $group " "/etc/group" ; then
print_help
exit
fi
base_path = $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd )
2016-09-04 13:50:17 +02:00
2015-07-09 14:26:07 +02:00
printf "\n################## Server informations ##################\n"
2016-09-04 13:50:17 +02:00
read -p "Server Hostname/IP: " ip_server
2015-07-09 14:26:07 +02:00
2016-09-04 13:50:17 +02:00
read -p "Port [443]: " server_port
2016-08-26 14:38:13 +02:00
2016-09-04 13:50:17 +02:00
if [ [ ! -z $server_port ] ] ; then
2016-08-26 14:38:13 +02:00
server_port = "443"
else
server_port = $server_port
fi
2015-07-09 14:26:07 +02:00
# Get root pass (to create the database and the user)
mysql_root_pass = ""
status_code = 1
while [ $status_code -ne 0 ] ; do
2016-08-26 14:38:13 +02:00
read -p "Server MySQL root password: " -s mysql_root_pass; echo
2016-09-04 13:50:17 +02:00
echo "SHOW DATABASES" | mysql -u root --password= " $mysql_root_pass " & > /dev/null
status_code = $?
2015-07-09 14:26:07 +02:00
done
sql_result = $( echo "SHOW DATABASES" | mysql -u root --password= " $mysql_root_pass " | grep -e " ^openvpn-admin $" )
# Check if the database doesn't already exist
if [ " $sql_result " != "" ] ; then
echo "The database openvpn-admin already exists."
exit
fi
2016-08-26 14:38:13 +02:00
2015-07-09 14:26:07 +02:00
# Check if the user doesn't already exist
2016-08-26 14:38:13 +02:00
read -p "Server MySQL openvpn-admin user (will be created): " mysql_user
2015-07-09 14:26:07 +02:00
echo " SHOW GRANTS FOR $mysql_user @localhost " | mysql -u root --password= " $mysql_root_pass " & > /dev/null
if [ $? -eq 0 ] ; then
echo "The MySQL user already exists."
exit
fi
2016-08-26 14:38:13 +02:00
read -p "Server MySQL openvpn-admin user password: " -s mysql_pass; echo
2015-07-09 14:26:07 +02:00
# TODO MySQL port & host ?
printf "\n################## Certificates informations ##################\n"
2016-09-04 13:50:17 +02:00
read -p "Key size (1024, 2048 or 4096) [2048]: " key_size
2015-07-09 14:26:07 +02:00
2016-09-04 13:50:17 +02:00
read -p "Root certificate expiration (in days) [3650]: " ca_expire
2015-07-09 14:26:07 +02:00
2016-09-04 13:50:17 +02:00
read -p "Certificate expiration (in days) [3650]: " cert_expire
2015-07-09 14:26:07 +02:00
2016-09-04 13:50:17 +02:00
read -p "Country Name (2 letter code) [US]: " cert_country
2015-07-09 14:26:07 +02:00
2016-09-04 13:50:17 +02:00
read -p "State or Province Name (full name) [California]: " cert_province
2015-07-09 14:26:07 +02:00
2016-09-04 13:50:17 +02:00
read -p "Locality Name (eg, city) [San Francisco]: " cert_city
2015-07-09 14:26:07 +02:00
2016-09-04 13:50:17 +02:00
read -p "Organization Name (eg, company) [Copyleft Certificate Co]: " cert_org
2015-07-09 14:26:07 +02:00
2016-09-04 13:50:17 +02:00
read -p "Organizational Unit Name (eg, section) [My Organizational Unit]: " cert_ou
2015-07-09 14:26:07 +02:00
2016-09-04 13:50:17 +02:00
read -p "Email Address [me@example.net]: " cert_email
2015-07-31 16:50:29 +02:00
2016-09-04 13:50:17 +02:00
read -p "Common Name (eg, your name or your server's hostname) [ChangeMe]: " key_cn
2015-07-31 16:50:29 +02:00
2015-07-09 14:26:07 +02:00
printf "\n################## Creating the certificates ##################\n"
2016-09-04 13:50:17 +02:00
EASYRSA_RELEASES = ( $(
curl -s https://api.github.com/repos/OpenVPN/easy-rsa/releases | \
grep 'tag_name' | \
grep -E '3(\.[0-9]+)+' | \
awk '{ print $2 }' | \
sed 's/[,|"|v]//g'
) )
EASYRSA_LATEST = ${ EASYRSA_RELEASES [0] }
2015-07-09 14:26:07 +02:00
# Get the rsa keys
2016-09-04 13:50:17 +02:00
wget -q --show-progress https://github.com/OpenVPN/easy-rsa/releases/download/${ EASYRSA_LATEST } /EasyRSA-${ EASYRSA_LATEST } .tgz
tar -xaf EasyRSA-${ EASYRSA_LATEST } .tgz
mv EasyRSA-${ EASYRSA_LATEST } /etc/openvpn/easy-rsa
rm -r EasyRSA-${ EASYRSA_LATEST } .tgz
2015-07-09 14:26:07 +02:00
cd /etc/openvpn/easy-rsa
2016-09-04 13:50:17 +02:00
if [ [ ! -z $key_size ] ] ; then
export EASYRSA_KEY_SIZE = $key_size
fi
if [ [ ! -z $ca_expire ] ] ; then
export EASYRSA_CA_EXPIRE = $ca_expire
fi
if [ [ ! -z $cert_expire ] ] ; then
export EASYRSA_CERT_EXPIRE = $cert_expire
fi
if [ [ ! -z $cert_country ] ] ; then
export EASYRSA_REQ_COUNTRY = $cert_country
fi
if [ [ ! -z $cert_province ] ] ; then
export EASYRSA_REQ_PROVINCE = $cert_province
fi
if [ [ ! -z $cert_city ] ] ; then
export EASYRSA_REQ_CITY = $cert_city
fi
if [ [ ! -z $cert_org ] ] ; then
export EASYRSA_REQ_ORG = $cert_org
fi
if [ [ ! -z $cert_ou ] ] ; then
export EASYRSA_REQ_OU = $cert_ou
fi
if [ [ ! -z $cert_email ] ] ; then
export EASYRSA_REQ_EMAIL = $cert_email
fi
if [ [ ! -z $key_cn ] ] ; then
export EASYRSA_REQ_CN = $key_cn
fi
2015-07-09 14:26:07 +02:00
2016-09-04 13:50:17 +02:00
# Init PKI dirs and build CA certs
./easyrsa init-pki
./easyrsa build-ca nopass
# Generate Diffie-Hellman parameters
./easyrsa gen-dh
# Genrate server keypair
./easyrsa build-server-full server nopass
2015-07-09 14:26:07 +02:00
2016-09-04 13:50:17 +02:00
# Generate shared-secret for TLS Authentication
openvpn --genkey --secret pki/ta.key
2015-07-09 14:26:07 +02:00
printf "\n################## Setup OpenVPN ##################\n"
# Copy certificates and the server configuration in the openvpn directory
2016-09-04 13:50:17 +02:00
cp /etc/openvpn/easy-rsa/pki/{ ca.crt,ta.key,issued/server.crt,private/server.key,dh.pem} "/etc/openvpn/"
2015-07-09 14:26:07 +02:00
cp " $base_path /installation/server.conf " "/etc/openvpn/"
2015-07-31 18:34:32 +02:00
mkdir "/etc/openvpn/ccd"
2016-09-04 13:50:17 +02:00
sed -i "s/dh dh1024\.pem/dh dh.pem/" "/etc/openvpn/server.conf"
2016-08-26 14:38:13 +02:00
sed -i " s/port 443/port $server_port / " "/etc/openvpn/server.conf"
2015-07-09 14:26:07 +02:00
printf "\n################## Setup firewall ##################\n"
# Make ip forwading and make it persistent
echo 1 > "/proc/sys/net/ipv4/ip_forward"
echo "net.ipv4.ip_forward = 1" >> "/etc/sysctl.conf"
# Iptable rules
iptables -I FORWARD -i tun0 -j ACCEPT
iptables -I FORWARD -o tun0 -j ACCEPT
iptables -I OUTPUT -o tun0 -j ACCEPT
iptables -A FORWARD -i tun0 -o eth0 -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 10.8.0.2/24 -o eth0 -j MASQUERADE
2016-09-04 13:50:17 +02:00
2015-07-09 14:26:07 +02:00
printf "\n################## Setup MySQL database ##################\n"
echo "CREATE DATABASE \`openvpn-admin\`" | mysql -u root --password= " $mysql_root_pass "
echo " CREATE USER $mysql_user @localhost IDENTIFIED BY ' $mysql_pass ' " | mysql -u root --password= " $mysql_root_pass "
echo " GRANT ALL PRIVILEGES ON \`openvpn-admin\`.* TO $mysql_user @localhost " | mysql -u root --password= " $mysql_root_pass "
echo "FLUSH PRIVILEGES" | mysql -u root --password= " $mysql_root_pass "
printf "\n################## Setup web application ##################\n"
# Copy bash scripts (which will insert row in MySQL)
cp -r " $base_path /installation/scripts " "/etc/openvpn/"
2015-07-29 18:35:08 +02:00
chmod +x "/etc/openvpn/scripts/" *
2015-07-09 14:26:07 +02:00
2015-07-31 19:02:08 +02:00
# Configure MySQL in openvpn scripts
2015-08-07 20:04:55 +02:00
sed -i " s/USER=''/USER=' $mysql_user '/ " "/etc/openvpn/scripts/config.sh"
sed -i " s/PASS=''/PASS=' $mysql_pass '/ " "/etc/openvpn/scripts/config.sh"
2015-07-31 19:02:08 +02:00
2015-07-09 14:26:07 +02:00
# Create the directory of the web application
mkdir " $openvpn_admin "
cp -r " $base_path / " { index.php,sql,bower.json,.bowerrc,js,include,css,installation/client-conf} " $openvpn_admin "
# New workspace
cd " $openvpn_admin "
# Replace config.php variables
sed -i " s/\$user = '';/\$user = ' $mysql_user ';/ " "./include/config.php"
sed -i " s/\$pass = '';/\$pass = ' $mysql_pass ';/ " "./include/config.php"
# Replace in the client configurations with the ip of the server
2016-08-26 14:38:13 +02:00
sed -i " s/remote xxx\.xxx\.xxx\.xxx 443/remote $ip_server $server_port / " "./client-conf/gnu-linux/client.conf"
sed -i " s/remote xxx\.xxx\.xxx\.xxx 443/remote $ip_server $server_port / " "./client-conf/windows/client.ovpn"
2015-07-09 14:26:07 +02:00
# Copy ta.key inside the client-conf directory
2015-07-29 18:35:08 +02:00
cp "/etc/openvpn/" { ca.crt,ta.key} "./client-conf/gnu-linux/"
cp "/etc/openvpn/" { ca.crt,ta.key} "./client-conf/windows/"
2015-07-09 14:26:07 +02:00
# Install third parties
bower --allow-root install
chown -R " $user : $group " " $openvpn_admin "
printf "\n################## Finish ##################\n"
echo "Congratulation, you have successfuly setup openvpn-admin. Please, finish the installation by configuring your web server (Apache, NGinx...) and install the web application by visiting http://your-installation/index.php?installation"
echo "Then, you will be able to run OpenVPN with systemctl start openvpn@server"
echo "Please, report any issues here https://github.com/Chocobozzz/OpenVPN-Admin"