#!/bin/bash
if [ -z "$1" ]
then
echo "============================================================="
echo "VPN -- Generate crt key pair"
echo "============================================================="
echo "Usage: bash gen.sh username"
exit
fi
echo "============================================================="
echo "VPN -- Generate crt key pair for user: $1"
echo "============================================================="
ADMIN_EMAIL="admin@mycompany.ru"
USER=$1
RSA="/home/ca/easy-rsa-master/easyrsa3/"
PKI="$RSA"pki/
PKI_KEY="$PKI"private/
PKI_CRT="$PKI"issued/
USR_CRT="/home/ca/cert_generation/user_crt/"
USR_DISTR="/home/ca/cert_generation/user_distr/"
if [ ! -f "$PKI_KEY$USER.key" ]
then
echo "File $PKI_KEY$USER.key does not exists, creating..."
cd "$RSA"
./easyrsa build-client-full $USER nopass
fi
if [ -e "$USR_CRT$USER/" ]
then
echo "Already exists, removing user folder $USR_CRT$USER..."
rm -r -f "$USR_CRT$USER/"
fi
mkdir $USR_CRT/$USER/
cp "$PKI_KEY$USER.key" "$USR_CRT$USER/$USER.key"
cp "$PKI_CRT$USER.crt" "$USR_CRT$USER/$USER.crt"
cp "$PKI"ca.crt "$USR_CRT$1"
cp "$USR_DISTR"ta.key "$USR_CRT$USER"
cp "$USR_DISTR"openssl.cnf "$USR_CRT$USER"
cp "$USR_DISTR"readme_vpn_win.txt "$USR_CRT$USER"
cp "$USR_DISTR"server.conf "$USR_CRT$USER"/server.conf.1
cp "$USR_DISTR"mycompany_vpn.ovpn "$USR_CRT$USER"/mycompany_vpn_$USER.ovpn.1
cp "$USR_DISTR"readme_vpn_win.txt "$USR_CRT$USER"/readme_vpn_win.txt.1
sed "s/change_me/$USER/g" "$USR_CRT$1"/server.conf.1 > "$USR_CRT$1"/server.conf
rm "$USR_CRT$USER"/server.conf.1
sed "s/change_me/$USER/g" "$USR_CRT$1"/mycompany_vpn_$USER.ovpn.1 > "$USR_CRT$1"/mycompany_vpn_$USER.ovpn
rm "$USR_CRT$USER"/mycompany_vpn_$USER.ovpn.1
sed "s/change_me/$USER/g" "$USR_CRT$1"/readme_vpn_win.txt.1 > "$USR_CRT$1"/readme_vpn_win.txt
rm "$USR_CRT$USER"/readme_vpn_win.txt.1
tar -cvzf "$USR_CRT$USER/$USER.tar.gz" "$USR_CRT$USER/"
echo "VPN: crt, key and configuration files for user $USER" | mutt $ADMIN_EMAIL -a $USR_CRT/$USER/$USER.tar.gz -s "VPN: crt, key and configuration files for user $USER"
echo "---------> DONE!"
echo "Keys fo user $USER sent to $ADMIN_EMAIL"