openvpn/build_key-pass.sh

304 lines
7.1 KiB
Bash
Executable File

#!/usr/bin/env bash
script_dir="$(dirname $(realpath $0))"
conf_file=${script_dir}/conf/install_openvpn.conf
log_file="$(mktemp)"
_date="$(date +%Y-%m-%d-%H%M)"
#---------------------------------------
#-----------------------------
# Base Function(s)
#-----------------------------
#---------------------------------------
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
fatal(){
echo ""
echo -e "\t[ \033[31m\033[1mFatal\033[m ]: $*"
echo ""
echo -e "\t\033[37m\033[1mInstalllation will be interrupted\033[m\033[m"
echo ""
exit 1
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mError\033[m ]: $*"
echo ""
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
}
echo_done() {
echo -e "\033[80G[ \033[32mdone\033[m ]"
}
echo_ok() {
echo -e "\033[80G[ \033[32mok\033[m ]"
}
echo_warning() {
echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]"
}
echo_failed(){
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[80G[ \033[37mskipped\033[m ]"
}
clear
echo ""
echo -e "\033[32m--\033[m"
echo ""
NEW_KEY_NAME=""
if [ -z "$NEW_KEY_NAME" ]; then
echo "Insert key name."
echo ""
echo ""
echononl "key name: "
read NEW_KEY_NAME
while [ "X$NEW_KEY_NAME" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mKey name is required!\033[m\n"
echononl "key name: "
read NEW_KEY_NAME
done
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Enter a password for the new key"
echo ""
_KEY_PW_1="X"
_KEY_PW_2="Y"
while [ "$_KEY_PW_1" != "$_KEY_PW_2" ]
do
echononl "Password: "
read -s _KEY_PW_1
echo
if [ "X$_KEY_PW_1" = "X" ]; then
echo -e "\n\t\033[33m\033[1mA password is required!\033[m\n"
continue
fi
echononl "Repeat the password: "
read -s _KEY_PW_2
echo
if [ "$_KEY_PW_1" != "$_KEY_PW_2" ];then
echo -e "\n\t\033[33m\033[1mpassword entries are NOT identical!\033[m\n"
else
KEY_PW=$_KEY_PW_1
fi
done
info "Going to create key \033[37m\033[1m${NEW_KEY_NAME}.key\033[m.."
echo -n "To continue type uppercase 'YES': "
read OK
echo ""
if [[ "$OK" != "YES" ]] ; then
fatal "Abort by user request - Answer as not 'YES'"
fi
#---------------------------------------
#-----------------------------
# Read Configurations from $conf_file
#-----------------------------
#---------------------------------------
echononl " Load configuration file.."
if [[ ! -f "$conf_file" ]]; then
echo_failed
fatal "Configuration file \033[37m\033[1m$(basename ${conf_file})\033[m not found!"
else
source "${conf_file}"
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
fi
#---------------------------------------
#-----------------------------
# Create Certificate
#-----------------------------
#---------------------------------------
echo ""
# - source file vars
# -
echononl " Load configuration '${EASY_RSA_DIR}/vars'.."
source ${EASY_RSA_DIR}/vars > "$log_file" 2>&1
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
# ---
# - Create Keys and Certs
# ---
echo ""
echo -e "\033[32m--\033[m"
echo "Create Keys and Certs .."
echo -e "\033[32m--\033[m"
echo ""
# - Build Key
# -
echo -e " \033[32mNow create the key \033[37m${NEW_KEY_NAME}.key\033[32m. This is an interactive procedure.\033[m"
echo ""
echo -e " Enter \033[37m\033[1m${KEY_PW}\033[m for Password"
echo -e " Enter \033[37m\033[1m${KEY_CN}-${NEW_KEY_NAME}\033[m as commonName"
echo ""
echo " For all other entries simply type <return> or confirm with 'y'"
echo ""
echononl "Type <return> to continue: "
read ok
echo ""
${EASY_RSA_DIR}/build-key-pass ${NEW_KEY_NAME}
if [[ $? -eq 0 ]] ; then
info "Building key '${NEW_KEY_NAME}.key' was successfully."
else
error "Building key '${NEW_KEY_NAME}.key' failed!"
fi
echo ""
echononl "Type <return> to continue: "
read ok
echo ""
echononl " Add new key credentials to file ${OPENVPN_BASE_DIR}/keys-created.txt"
cat << EOF >> ${OPENVPN_BASE_DIR}/keys-created.txt
key...............: ${NEW_KEY_NAME}.key
common name.......: ${KEY_CN}-${NEW_KEY_NAME}
password..........: ${KEY_PW}
EOF
if [[ $? -eq 0 ]] ; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Do you want me to copy the created key material into the home directory "
echo "of a specified user?"
echo ""
echo -e " Specify a username or type \033[33mNone\033[m to ommit this step."
echo ""
if [[ -n "$DEFAULT_USER_TO_COPY_CREDENTIALS" ]] ; then
echo -e " Defaults to user '${DEFAULT_USER_TO_COPY_CREDENTIALS}'"
echo ""
fi
user_name=""
if [[ -n "$DEFAULT_USER_TO_COPY_CREDENTIALS" ]] ; then
echononl "Username: [${DEFAULT_USER_TO_COPY_CREDENTIALS}]: "
read user_name
if [[ "X$user_name" = "X" ]]; then
user_name="${DEFAULT_USER_TO_COPY_CREDENTIALS}"
_copy_to_user_home_dir=true
elif [[ "$(echo "${user_name,,}")" = "none" ]]; then
_copy_to_user_home_dir=false
else
_copy_to_user_home_dir=true
fi
else
while [[ "X$user_name" = "X" ]]; do
echononl "Username: "
read user_name
if [[ "X$user_name" = "X" ]]; then
echo
echo -e " Give a Username or type \033[33mNone\033[m"
echo
continue
elif [[ "$(echo "${user_name,,}")" = "none" ]]; then
_copy_to_user_home_dir=false
else
_copy_to_user_home_dir=true
fi
done
fi
if $_copy_to_user_home_dir ; then
_home_dir=$(eval echo "~$user_name")
_failed=false
echo ""
echononl " Copy key material into dir '${_home_dir}/${KEY_CN}-${NEW_KEY_NAME}'.."
mkdir -p "${_home_dir}/VPN/${KEY_CN}-${NEW_KEY_NAME}" > $log_file 2>&1
if [[ $? -ne 0 ]] ; then
_failed=true
fi
cp -a ${OPENVPN_BASE_DIR}/keys/${NEW_KEY_NAME}.key "${_home_dir}/VPN/${KEY_CN}-${NEW_KEY_NAME}" >> $log_file 2>&1
if [[ $? -ne 0 ]] ; then
_failed=true
fi
cp -a -a ${OPENVPN_BASE_DIR}/keys/${NEW_KEY_NAME}.crt "${_home_dir}/VPN/${KEY_CN}-${NEW_KEY_NAME}" >> $log_file 2>&1
if [[ $? -ne 0 ]] ; then
_failed=true
fi
cp -a -a ${OPENVPN_BASE_DIR}/keys/ca.crt "${_home_dir}/VPN/${KEY_CN}-${NEW_KEY_NAME}" >> $log_file 2>&1
if [[ $? -ne 0 ]] ; then
_failed=true
fi
cp -a -a ${OPENVPN_BASE_DIR}/keys/ta.key "${_home_dir}/VPN/${KEY_CN}-${NEW_KEY_NAME}" >> $log_file 2>&1
if [[ $? -ne 0 ]] ; then
_failed=true
fi
cat << EOF > "${_home_dir}/VPN/${KEY_CN}-${NEW_KEY_NAME}/passwd.txt"
key...............: ${NEW_KEY_NAME}.key
common name.......: ${KEY_CN}-${NEW_KEY_NAME}
password..........: ${KEY_PW}
EOF
if [[ $? -ne 0 ]] ; then
_failed=true
fi
chown -R ${user_name}:$user_name "${_home_dir}/VPN" >> $log_file 2>&1
if [[ $? -ne 0 ]] ; then
_failed=true
fi
if $_failed ; then
echo_failed
error "$(cat $log_file)"
else
echo_ok
fi
fi
rm -f $log_file
echo ""
exit 0