2016-10-04 17:46:14 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# Create OVPN Client
|
|
|
|
# Default Variable Declarations
|
|
|
|
DEFAULT="Default.txt"
|
|
|
|
FILEEXT=".ovpn"
|
|
|
|
CRT=".crt"
|
2016-12-06 15:56:51 +00:00
|
|
|
KEY=".key"
|
2016-10-04 17:46:14 +00:00
|
|
|
CA="ca.crt"
|
|
|
|
TA="ta.key"
|
2016-12-06 15:56:51 +00:00
|
|
|
INDEX="/etc/openvpn/easy-rsa/pki/index.txt"
|
2016-04-19 18:01:55 +00:00
|
|
|
INSTALL_USER=$(cat /etc/pivpn/INSTALL_USER)
|
|
|
|
|
2016-05-01 03:37:27 +00:00
|
|
|
# Functions def
|
2016-04-30 17:28:01 +00:00
|
|
|
|
2016-05-01 03:37:27 +00:00
|
|
|
function keynoPASS() {
|
2016-04-30 17:28:01 +00:00
|
|
|
|
2016-05-01 03:37:27 +00:00
|
|
|
#Build the client key
|
|
|
|
expect << EOF
|
2016-10-09 11:34:17 +00:00
|
|
|
set timeout -1
|
2016-12-06 15:56:51 +00:00
|
|
|
spawn ./easyrsa build-client-full "$NAME" nopass
|
2016-05-01 03:37:27 +00:00
|
|
|
expect eof
|
|
|
|
EOF
|
|
|
|
|
2016-12-06 15:56:51 +00:00
|
|
|
cd pki || exit
|
2016-05-01 03:37:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function keyPASS() {
|
2016-04-30 17:28:01 +00:00
|
|
|
|
2016-05-01 03:37:27 +00:00
|
|
|
stty -echo
|
|
|
|
while true
|
|
|
|
do
|
2016-10-04 18:54:09 +00:00
|
|
|
printf "Enter the password for the client: "
|
2016-10-04 17:46:14 +00:00
|
|
|
read -r PASSWD
|
2016-05-01 03:37:27 +00:00
|
|
|
printf "\n"
|
|
|
|
printf "Enter the password again to verify: "
|
2016-10-04 17:46:14 +00:00
|
|
|
read -r PASSWD2
|
2016-05-01 03:37:27 +00:00
|
|
|
printf "\n"
|
2016-11-12 02:55:47 +00:00
|
|
|
[ "${PASSWD}" = "${PASSWD2}" ] && break
|
2016-05-01 03:37:27 +00:00
|
|
|
printf "Passwords do not match! Please try again.\n"
|
|
|
|
done
|
|
|
|
stty echo
|
2016-11-12 02:55:47 +00:00
|
|
|
if [[ -z "${PASSWD}" ]]; then
|
2016-10-04 17:46:14 +00:00
|
|
|
echo "You left the password blank"
|
|
|
|
echo "If you don't want a password, please run:"
|
|
|
|
echo "pivpn add nopass"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ ${#PASSWD} -lt 4 ] || [ ${#PASSWD} -gt 1024 ]
|
|
|
|
then
|
|
|
|
echo "Password must be between from 4 to 1024 characters"
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-05-01 03:37:27 +00:00
|
|
|
|
2016-11-11 22:45:48 +00:00
|
|
|
#Escape chars in PASSWD
|
2016-11-12 02:55:47 +00:00
|
|
|
PASSWD=$(echo -n ${PASSWD} | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/\$/\\\$/g' -e 's/!/\\!/g' -e 's/\./\\\./g' -e "s/'/\\\'/g" -e 's/"/\\"/g' -e 's/\*/\\\*/g' -e 's/\@/\\\@/g' -e 's/\#/\\\#/g' -e 's/£/\\£/g' -e 's/%/\\%/g' -e 's/\^/\\\^/g' -e 's/\&/\\\&/g' -e 's/(/\\(/g' -e 's/)/\\)/g' -e 's/-/\\-/g' -e 's/_/\\_/g' -e 's/\+/\\\+/g' -e 's/=/\\=/g' -e 's/\[/\\\[/g' -e 's/\]/\\\]/g' -e 's/;/\\;/g' -e 's/:/\\:/g' -e 's/|/\\|/g' -e 's/</\\</g' -e 's/>/\\>/g' -e 's/,/\\,/g' -e 's/?/\\?/g' -e 's/~/\\~/g' -e 's/{/\\{/g' -e 's/}/\\}/g')
|
2016-11-11 22:45:48 +00:00
|
|
|
|
2016-05-01 03:37:27 +00:00
|
|
|
#Build the client key and then encrypt the key
|
|
|
|
|
|
|
|
expect << EOF
|
2016-10-09 11:34:17 +00:00
|
|
|
set timeout -1
|
2016-12-06 15:56:51 +00:00
|
|
|
spawn ./easyrsa build-client-full "$NAME"
|
2016-11-12 02:55:47 +00:00
|
|
|
expect "Enter PEM pass phrase" { send "${PASSWD}\r" }
|
|
|
|
expect "Verifying - Enter PEM pass phrase" { send "${PASSWD}\r" }
|
2016-05-01 03:37:27 +00:00
|
|
|
expect eof
|
2016-04-30 17:28:01 +00:00
|
|
|
EOF
|
|
|
|
|
2016-12-06 15:56:51 +00:00
|
|
|
cd pki || exit
|
2016-04-30 17:28:01 +00:00
|
|
|
|
2016-05-01 03:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf "Enter a Name for the Client: "
|
2016-10-04 17:46:14 +00:00
|
|
|
read -r NAME
|
|
|
|
|
2016-12-06 15:56:51 +00:00
|
|
|
if [[ "${NAME}" =~ [^a-zA-Z0-9] ]]; then
|
|
|
|
echo "Name can only contain alphanumeric characters."
|
2016-10-04 17:46:14 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2016-05-01 03:37:27 +00:00
|
|
|
|
2016-12-06 15:56:51 +00:00
|
|
|
if [[ -z "${NAME}" ]]; then
|
|
|
|
echo "You cannot leave the name blank."
|
2016-05-06 01:04:57 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2016-10-04 18:54:09 +00:00
|
|
|
# Check if name is already in use
|
|
|
|
while read -r line || [ -n "$line" ]; do
|
2016-12-06 15:56:51 +00:00
|
|
|
if [ "$(echo "$line" | sed -e 's:.*/CN=::')" == "${NAME}" ]; then
|
|
|
|
echo "Name is already in use."
|
2016-10-04 18:54:09 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2016-12-06 15:56:51 +00:00
|
|
|
done <${INDEX}
|
2016-10-04 18:54:09 +00:00
|
|
|
|
|
|
|
# Check if name is reserved
|
2016-12-06 15:56:51 +00:00
|
|
|
if [ "${NAME}" == "ta" ] || [ "${NAME}" == "server" ] || [ "${NAME}" == "ca" ]; then
|
|
|
|
echo "Sorry, this is in use by the server and cannot be used by clients."
|
2016-10-04 18:54:09 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2016-10-04 17:46:14 +00:00
|
|
|
cd /etc/openvpn/easy-rsa || exit
|
2016-05-01 03:37:27 +00:00
|
|
|
|
|
|
|
if [[ "$@" =~ "nopass" ]]; then
|
|
|
|
keynoPASS
|
|
|
|
else
|
|
|
|
keyPASS
|
|
|
|
fi
|
2016-10-04 17:46:14 +00:00
|
|
|
|
|
|
|
#1st Verify that clients Public Key Exists
|
2016-12-06 15:56:51 +00:00
|
|
|
if [ ! -f "issued/${NAME}${CRT}" ]; then
|
2016-10-04 17:46:14 +00:00
|
|
|
echo "[ERROR]: Client Public Key Certificate not found: $NAME$CRT"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
echo "Client's cert found: $NAME$CRT"
|
|
|
|
|
|
|
|
#Then, verify that there is a private key for that client
|
2016-12-06 15:56:51 +00:00
|
|
|
if [ ! -f "private/${NAME}${KEY}" ]; then
|
|
|
|
echo "[ERROR]: Client Private Key not found: $NAME$KEY"
|
2016-10-04 17:46:14 +00:00
|
|
|
exit
|
|
|
|
fi
|
2016-04-19 18:01:55 +00:00
|
|
|
echo "Client's Private Key found: $NAME$KEY"
|
2016-10-04 17:46:14 +00:00
|
|
|
|
|
|
|
#Confirm the CA public key exists
|
2016-12-06 15:56:51 +00:00
|
|
|
if [ ! -f "${CA}" ]; then
|
2016-10-04 17:46:14 +00:00
|
|
|
echo "[ERROR]: CA Public Key not found: $CA"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
echo "CA public Key found: $CA"
|
|
|
|
|
|
|
|
#Confirm the tls-auth ta key file exists
|
2016-12-06 15:56:51 +00:00
|
|
|
if [ ! -f "${TA}" ]; then
|
2016-10-04 17:46:14 +00:00
|
|
|
echo "[ERROR]: tls-auth Key not found: $TA"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
echo "tls-auth Private Key found: $TA"
|
|
|
|
|
|
|
|
#Ready to make a new .ovpn file
|
|
|
|
{
|
|
|
|
# Start by populating with the default file
|
2016-12-06 15:56:51 +00:00
|
|
|
cat "${DEFAULT}"
|
2016-10-04 17:46:14 +00:00
|
|
|
|
|
|
|
#Now, append the CA Public Cert
|
|
|
|
echo "<ca>"
|
2016-12-06 15:56:51 +00:00
|
|
|
cat "${CA}"
|
2016-10-04 17:46:14 +00:00
|
|
|
echo "</ca>"
|
|
|
|
|
|
|
|
#Next append the client Public Cert
|
|
|
|
echo "<cert>"
|
2016-12-06 15:56:51 +00:00
|
|
|
sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' < "issued/${NAME}${CRT}"
|
2016-10-04 17:46:14 +00:00
|
|
|
echo "</cert>"
|
|
|
|
|
|
|
|
#Then, append the client Private Key
|
|
|
|
echo "<key>"
|
2016-12-06 15:56:51 +00:00
|
|
|
cat "private/${NAME}${KEY}"
|
2016-10-04 17:46:14 +00:00
|
|
|
echo "</key>"
|
|
|
|
|
|
|
|
#Finally, append the TA Private Key
|
|
|
|
echo "<tls-auth>"
|
2016-12-06 15:56:51 +00:00
|
|
|
cat "${TA}"
|
2016-10-04 17:46:14 +00:00
|
|
|
echo "</tls-auth>"
|
2016-12-06 15:56:51 +00:00
|
|
|
} > "${NAME}${FILEEXT}"
|
2016-04-19 18:01:55 +00:00
|
|
|
|
|
|
|
# Copy the .ovpn profile to the home directory for convenient remote access
|
2016-12-06 15:56:51 +00:00
|
|
|
cp "/etc/openvpn/easy-rsa/pki/$NAME$FILEEXT" "/home/$INSTALL_USER/ovpns/$NAME$FILEEXT"
|
2016-10-04 17:46:14 +00:00
|
|
|
chown "$INSTALL_USER" "/home/$INSTALL_USER/ovpns/$NAME$FILEEXT"
|
2016-04-30 17:28:01 +00:00
|
|
|
printf "\n\n"
|
|
|
|
printf "========================================================\n"
|
2016-10-04 17:46:14 +00:00
|
|
|
printf "\e[1mDone! %s successfully created!\e[0m \n" "$NAME$FILEEXT"
|
|
|
|
printf "%s was copied to:\n" "$NAME$FILEEXT"
|
|
|
|
printf " /home/%s/ovpns\n" "$INSTALL_USER"
|
2016-04-30 17:28:01 +00:00
|
|
|
printf "for easy transfer.\n"
|
|
|
|
printf "========================================================\n\n"
|