217 lines
9.5 KiB
Bash
217 lines
9.5 KiB
Bash
#!/bin/bash
|
|
# Vérification root
|
|
if [ `whoami` != 'root' ];
|
|
then
|
|
exit
|
|
fi
|
|
|
|
# Initialisation des variables
|
|
SOURCE=`pwd`
|
|
echo ' __ __ _____ ____ ____ _____ ______'
|
|
echo ' \ \ / / /\ | __ \ / __ \| _ \ /\ / ____| ____|'
|
|
echo ' \ V / / \ | |__) | | | | |_) | / \ | (___ | |__'
|
|
echo ' > < / /\ \ | _ /| | | | _ < / /\ \ \___ \| __|'
|
|
echo ' / . \ / ____ \| | \ \| |__| | |_) / ____ \ ____) | |____'
|
|
echo ' /_/ \_\/_/ \_\_| \_\\____/|____/_/ \_\_____/|______|'
|
|
echo ' _____ __ _'
|
|
echo '/ ___| / _| |'
|
|
echo '\ `--. ___ | |_| |___ ____ _ _ __ ___'
|
|
echo ' `--. \/ _ \| _| __\ \ /\ / / _` | '__/ _ \'
|
|
echo '/\__/ / (_) | | | |_ \ V V / (_| | | | __/'
|
|
echo '\____/ \___/|_| \__| \_/\_/ \__,_|_| \___|'
|
|
echo ""
|
|
echo "Programme de déploiement de service OpenVPN (V2)"
|
|
echo ""
|
|
read -p "Ce script s'adresse aux utilisateurs expérimentés. Voulez-vous continuer ? (o/N) : " CONFIRM
|
|
CONFIRM=${CONFIRM^^}
|
|
if [ $CONFIRM != 'O' ];
|
|
then
|
|
exit
|
|
fi
|
|
echo "#######################"
|
|
echo "# Lancement du script #"
|
|
echo "#######################"
|
|
|
|
# Installation des mises à jours
|
|
echo "########################################"
|
|
echo "# Début de la procédure de mise à jour #"
|
|
echo "########################################"
|
|
apt-get update
|
|
apt-get upgrade -y
|
|
echo "-- Mise à jour terminé !"
|
|
|
|
# Services à déployer
|
|
echo "#######################"
|
|
echo "# Services à déployer #"
|
|
echo "#######################"
|
|
read -p "Voulez-vous déployer le service VPN ? (o/N) : " VPN
|
|
VPN=${VPN^^}
|
|
|
|
# Récupération interface
|
|
read -p "Quel est l'interface réseau à utilisé ? : " INTERFACE
|
|
INTERFACE=${INTERFACE,,}
|
|
|
|
if [ $VPN == 'O' ];
|
|
then
|
|
|
|
# Installation VPN
|
|
echo "######################################"
|
|
echo "# Lancement de l'installation du VPN #"
|
|
echo "######################################"
|
|
apt-get install openssl -y
|
|
echo "-- Installation de OpenSSL terminé !"
|
|
apt-get install openvpn -y
|
|
echo "-- Installation de OpenVPN terminé !"
|
|
apt-get install easy-rsa -y
|
|
echo "-- Installation de Easy-RSA terminé !"
|
|
apt-get install ipcalc -y
|
|
echo "-- Installation de IPCALC terminé !"
|
|
apt-get install zip -y
|
|
echo "-- Installation de ZIP terminé !"
|
|
|
|
# Questions VPN et définition des variables
|
|
echo "########################"
|
|
echo "# Configuration du VPN #"
|
|
echo "########################"
|
|
read -p "Entrer le code pays du certificat : " KEY_COUNTRY
|
|
KEY_COUNTRY=${KEY_COUNTRY^^}
|
|
read -p "Entrer le département du certificat : " KEY_PROVINCE
|
|
read -p "Entrer la ville du certificat : " KEY_CITY
|
|
read -p "Entrer le nom de l'organisation du certificat : " KEY_ORG
|
|
read -p "Entrer l'unité d'organisation du certificat : " KEY_OU
|
|
read -p "Entrer l'adresse email de contact du certificat : " KEY_EMAIL
|
|
read -p "Entrer le nom de l'instance VPN : " VPN_NAME
|
|
VPN_NAME=${VPN_NAME,,}
|
|
read -p "Entrer le port de l'instance VPN : " VPN_PORT
|
|
read -p "Entrer l'adresse réseau de votre réseau VPN : " VPN_NETWORK
|
|
read -p "Entrer le masque de votre réseau VPN : " VPN_NETMASK
|
|
read -p "Entrer le FQDN de votre serveur : " VPN_ADRESSE
|
|
VPN_ADRESSE=${VPN_ADRESSE,,}
|
|
read -p "Entrer le DNS que votre client utilisera : " VPN_DNS
|
|
read -p "Entrer le TLD de votre réseau VPN : " VPN_TLD
|
|
VPN_TLD=${VPN_TLD,,}
|
|
read -p "Entre le nom du certificat client dde l'instance VPN : " VPN_USER
|
|
VPN_USER=${VPN_USER,,}
|
|
read -p "Voulez-vous sécuriser le certificat client par un mot de passe ? (o/N) : " VPN_USER_PASS
|
|
VPN_USER_PASS=${VPN_USER_PASS^^}
|
|
VPN_NETWORK=`ipcalc $VPN_NETWORK $VPN_NETMASK | grep Network | awk -F" " '{print $2}' | awk -F"/" '{print $1}'`
|
|
INTERFACES_CONFIG=`cat /etc/network/interfaces | grep "pre-up iptables-restore < /etc/openvpn/iptables.rules"`
|
|
|
|
# Configuration VPN
|
|
echo "###################################"
|
|
echo "# Génération du certifiat serveur #"
|
|
echo "###################################"
|
|
mkdir /etc/openvpn/easy-rsa
|
|
cp /usr/share/easy-rsa/* /etc/openvpn/easy-rsa
|
|
echo "-- easy-rsa copié !"
|
|
echo "" > /etc/openvpn/easy-rsa/vars
|
|
echo -e "export EASY_RSA=\"\`pwd\`\"\nexport OPENSSL=\"openssl\"\nexport PKCS11TOOL=\"pkcs11-tool\"\nexport GREP=\"grep\"\nexport KEY_CONFIG=\`\$EASY_RSA/whichopensslcnf \$EASY_RSA\`\nexport KEY_DIR=\"\$EASY_RSA/keys\"\necho NOTE: If you run ./clean-all, I will be doing a rm -rf on \$KEY_DIR\nexport PKCS11_MODULE_PATH=\"dummy\"\nexport PKCS11_PIN=\"dummy\"\nexport KEY_SIZE=2048\nexport CA_EXPIRE=3650\nexport KEY_COUNTRY=\"$KEY_COUNTRY\"\nexport KEY_PROVINCE=\"$KEY_PROVINCE\"\nexport KEY_CITY=\"$KEY_CITY\"\nexport KEY_ORG=\"$KEY_ORG\"\nexport KEY_EMAIL=\"$KEY_EMAIL\"\nexport KEY_OU=\"$KEY_OU\"\nexport KEY_NAME=\"$VPN_NAME\"" >> /etc/openvpn/easy-rsa/vars
|
|
cp /etc/openvpn/easy-rsa/openssl-1.0.0.cnf /etc/openvpn/easy-rsa/openssl.cnf
|
|
echo "-- easy-rsa modifié !"
|
|
cd /etc/openvpn/easy-rsa
|
|
source vars
|
|
./clean-all
|
|
./build-dh
|
|
./pkitool --initca
|
|
./pkitool --server $VPN_NAME
|
|
openvpn --genkey --secret keys/ta.key
|
|
echo "-- Certificat généré !"
|
|
cp /etc/openvpn/easy-rsa/keys/ca.crt /etc/openvpn/easy-rsa/keys/ta.key /etc/openvpn/easy-rsa/keys/$VPN_NAME.crt /etc/openvpn/easy-rsa/keys/$VPN_NAME.key /etc/openvpn/easy-rsa/keys/dh2048.pem /etc/openvpn/
|
|
echo "-- Copie du certificat !"
|
|
echo "####################################"
|
|
echo "# Copie de la configuration du VPN #"
|
|
echo "####################################"
|
|
mkdir /etc/openvpn/jail
|
|
mkdir /etc/openvpn/jail/tmp
|
|
mkdir /etc/openvpn/clientconf
|
|
touch /etc/openvpn/$VPN_NAME.conf
|
|
echo -e "mode server\nproto tcp\nport $VPN_PORT\ndev tun\ntopology subnet\nca ca.crt\ncert $VPN_NAME.crt\nkey $VPN_NAME.key\ndh dh2048.pem\ntls-auth ta.key 1\nkey-direction 0\ncipher AES-256-CBC\nserver $VPN_NETWORK $VPN_NETMASK\npush \"redirect-gateway def1\"\npush \"dhcp-option DNS $VPN_DNS\"\npush \"dhcp-option DOMAIN $VPN_TLD\"\nkeepalive 10 120\nclient-to-client\nuser nobody\ngroup nogroup\nchroot /etc/openvpn/jail\npersist-key\npersist-tun\ncomp-lzo\nduplicate-cn\nverb 3\nmute 20\nstatus openvpn-status.log\nlog-append /var/log/openvpn.log" >> /etc/openvpn/$VPN_NAME.conf
|
|
echo "-- Fichiers créés !"
|
|
echo "################################"
|
|
echo "# Activation du routage et NAT #"
|
|
echo "################################"
|
|
sed -i -e "s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g" /etc/sysctl.conf
|
|
echo "-- Routage activé !"
|
|
iptables -I FORWARD -i tun0 -j ACCEPT
|
|
iptables -I FORWARD -o tun0 -j ACCEPT
|
|
iptables -I INPUT -i tun0 -j ACCEPT
|
|
iptables -I OUTPUT -o tun0 -j ACCEPT
|
|
iptables -t nat -A POSTROUTING -o $INTERFACE -j MASQUERADE
|
|
sh -c "iptables-save > /etc/openvpn/iptables.rules"
|
|
if [ -z $INTERFACES_CONFIG ];
|
|
then
|
|
echo "pre-up iptables-restore < /etc/openvpn/iptables.rules" >> /etc/network/interfaces
|
|
fi
|
|
echo "-- Règles NAT activé !"
|
|
echo "##################################"
|
|
echo "# Génération du certifiat client #"
|
|
echo "##################################"
|
|
if [ $VPN_USER_PASS == 'O' ];
|
|
then
|
|
echo "Entrer le mot de passe du certificat client : "
|
|
./build-key-pass $VPN_USER
|
|
else
|
|
./build-key $VPN_USER
|
|
fi
|
|
echo "-- Certificat créé !"
|
|
mkdir /etc/openvpn/clientconf/$VPN_USER
|
|
cp /etc/openvpn/ca.crt /etc/openvpn/ta.key /etc/openvpn/easy-rsa/keys/$VPN_USER.crt /etc/openvpn/easy-rsa/keys/$VPN_USER.key /etc/openvpn/clientconf/$VPN_USER/
|
|
echo "-- Certificat copié !"
|
|
touch /etc/openvpn/clientconf/$VPN_USER/client.conf
|
|
echo -e "client\ndev tun\nproto tcp-client\nremote $VPN_ADRESSE $VPN_PORT\nresolv-retry infinite\ncipher AES-256-CBC\nca ca.crt\ncert $VPN_USER.crt\nkey $VPN_USER.key\ntls-auth ta.key 1\nkey-direction 1\nnobind\npersist-key\npersist-tun\ncomp-lzo\nverb 3\nauth-nocache" >> /etc/openvpn/clientconf/$VPN_USER/client.conf
|
|
cp /etc/openvpn/clientconf/$VPN_USER/client.conf /etc/openvpn/clientconf/$VPN_USER/client.ovpn
|
|
echo "-- Configuration généré !"
|
|
zip /etc/openvpn/clientconf/$VPN_USER.zip /etc/openvpn/clientconf/$VPN_USER/*.*
|
|
echo "-- Archive créé !"
|
|
fi
|
|
|
|
# Redémarrage des services
|
|
echo "############################"
|
|
echo "# Redémarrage des services #"
|
|
echo "############################"
|
|
if [ $VPN == 'O' ];
|
|
then
|
|
service openvpn restart
|
|
echo "-- OpenVPN redémarré !"
|
|
fi
|
|
cd $SOURCE
|
|
|
|
# Ajout de la vérification XAROBASE
|
|
FILE=`cat /etc/XAROBASE`
|
|
|
|
if [ -z $FILE ] || [ $FILE != 'INSTALLED' ];
|
|
then
|
|
# Ajout du motd
|
|
touch /etc/XAROBASE
|
|
echo "INSTALLED" > /etc/XAROBASE
|
|
echo "" > /etc/motd
|
|
echo ' __ __ _____ ____ ____ _____ ______' >> /etc/motd
|
|
echo ' \ \ / / /\ | __ \ / __ \| _ \ /\ / ____| ____|' >> /etc/motd
|
|
echo ' \ V / / \ | |__) | | | | |_) | / \ | (___ | |__' >> /etc/motd
|
|
echo ' > < / /\ \ | _ /| | | | _ < / /\ \ \___ \| __|' >> /etc/motd
|
|
echo ' / . \ / ____ \| | \ \| |__| | |_) / ____ \ ____) | |____' >> /etc/motd
|
|
echo ' /_/ \_\/_/ \_\_| \_\\____/|____/_/ \_\_____/|______|' >> /etc/motd
|
|
echo ' _____ __ _' >> /etc/motd
|
|
echo '/ ___| / _| |' >> /etc/motd
|
|
echo '\ `--. ___ | |_| |___ ____ _ _ __ ___' >> /etc/motd
|
|
echo ' `--. \/ _ \| _| __\ \ /\ / / _` | '__/ _ \' >> /etc/motd
|
|
echo '/\__/ / (_) | | | |_ \ V V / (_| | | | __/' >> /etc/motd
|
|
echo '\____/ \___/|_| \__| \_/\_/ \__,_|_| \___|' >> /etc/motd
|
|
echo -e "\n\t\t\t\t\t\t${HOSTNAME^^}" >> /etc/motd
|
|
echo '' >> /etc/motd
|
|
fi
|
|
if [ $VPN == 'O' ];
|
|
then
|
|
echo "-- Service VPN" >> /etc/motd
|
|
fi
|
|
echo "#########################"
|
|
echo "# Fin de l'installation #"
|
|
echo "#########################"
|
|
echo "Vous disposé maintenant des services suivant : "
|
|
if [ $VPN == 'O' ];
|
|
then
|
|
echo "-- Service VPN"
|
|
echo "Vous pouvez vous connecter au serveur avec un client grâce à l'archive présente dans /etc/openvpn/clientconf/$VPN_USER.zip"
|
|
echo "Un redémarrage est nécessaire !"
|
|
fi
|