107 lines
4.0 KiB
Bash
Executable File
107 lines
4.0 KiB
Bash
Executable File
#!/bin/bash
|
|
# FILE: wgapi:back/lib/ssl/peer/add
|
|
# DESCRIPTION: Create SSL certs for a new host
|
|
# USAGE: add hostname username ipstring
|
|
# ERRORS:
|
|
# 3: Bad usage
|
|
# 4: config file not found
|
|
# 5: openssl or config not found
|
|
# 6: not root
|
|
# 7: openssl failed
|
|
|
|
|
|
|
|
CONFIG_FILE='/etc/wgapi/config'
|
|
if ! [ ${#} -eq 3 ]; then
|
|
printf 'ERROR! Invalid number of arguments to %s: %s\n' "${0}" "${*}" >>"${LOGFILE}"
|
|
exit 3
|
|
fi
|
|
if ! [ -f "${CONFIG_FILE}" ]; then
|
|
printf 'ERROR! %s couldnt find %s\n' "${0}" "${*}" >>"${LOGFILE}"
|
|
exit 4
|
|
fi
|
|
if ! [ -x '/usr/bin/openssl' ]; then
|
|
printf 'ERROR! /usr/bin/openssl not found!\n' >>"${LOGFILE}"
|
|
exit 5
|
|
fi
|
|
if ! [ -f '/etc/ssl/openssl.cnf' ]; then
|
|
printf 'ERROR! /etc/ssl/openssl.cnf not found!\n' >>"${LOGFILE}"
|
|
exit 5
|
|
fi
|
|
source "${CONFIG_FILE}"
|
|
|
|
hostname="${1}"
|
|
username="${2}"
|
|
ipstring="${3}"
|
|
|
|
printf 'Signing SSL certs for %s.%s.%s...\n' "${hostname}" "${username}" "${TLD}" >>"${LOGFILE}"
|
|
|
|
# Make a directory for the new files
|
|
which mkdir >>"${LOGFILE}"
|
|
if ! sudo mkdir "${SSL_CONFIG_DIR:?}/${username:?}/${hostname:?}/" >>"${LOGFILE}" 2>/dev/null; then
|
|
printf 'Failed to create directory %s/%s/%s/\n' "${SSL_CONFIG_DIR}" "${username}" "${hostname}" >>"${LOGFILE}"
|
|
exit 7
|
|
fi
|
|
|
|
# Generate key
|
|
if ! sudo /usr/bin/openssl genrsa -out "${SSL_CONFIG_DIR:?}/${username:?}/${hostname:?}/server.key" >>"${LOGFILE}" 2>&1; then
|
|
printf 'Failed to generate SSL key %s/%s/server.key\n' "${username}" "${hostname}" >>"${LOGFILE}"
|
|
exit 7
|
|
fi
|
|
if ! [ -f "${SSL_CONFIG_DIR:?}/${username:?}/${hostname:?}/server.key" ]; then
|
|
printf 'SSL key %s/%s/server.key was not generated!\n' "${username}" "${hostname}" >>"${LOGFILE}"
|
|
exit 7
|
|
fi
|
|
if ! sudo chmod 400 "${SSL_CONFIG_DIR}/${username}/${hostname}/server.key" >>"${LOGFILE}" 2>&1; then
|
|
printf 'Failed to chmod SSL key %s/%s/server.key\n' "${username}" "${hostname}" >>"${LOGFILE}"
|
|
exit 7
|
|
fi
|
|
|
|
# Generate config
|
|
san="\n[SAN]\nsubjectAltNames=DNS:${hostname:?}.${username:?}.${TLD:?},DNS:*.${hostname:?}.${username:?}.${TLD:?}"
|
|
[ "${ipstring}" != "" ] && san="${san},${ipstring}"
|
|
if ! printf '%s' "${san}" | sudo cat '/etc/ssl/openssl.cnf' /dev/stdin \
|
|
> "${SSL_CONFIG_DIR:?}/${username:?}/${hostname:?}.cnf"; then
|
|
printf 'Failed to generate %s/%s.cnf\n' "${username}" "${hostname}" >>"${LOGFILE}"
|
|
exit 7
|
|
fi
|
|
sudo tail "${SSL_CONFIG_DIR:?}/${username:?}/${hostname:?}.cnf" >>"${LOGFILE}"
|
|
|
|
# Generate CSR
|
|
if ! sudo /usr/bin/openssl req -new -sha256 -reqexts SAN \
|
|
-key "${SSL_CONFIG_DIR}/${username}/${hostname}/server.key" \
|
|
-out "${SSL_CONFIG_DIR}/${username}/${hostname}.csr" \
|
|
-config "${SSL_CONFIG_DIR}/${username}/${hostname}.cnf" \
|
|
-subj "/O=${SSL_ORG}/OU=${username}/CN=${hostname}.${username}.${TLD}" \
|
|
>>"${LOGFILE}" 2>&1; then
|
|
printf 'Failed to generate %s/%s.cnf\n' "${username}" "${hostname}" >>"${LOGFILE}"
|
|
exit 7
|
|
fi
|
|
|
|
# Generate cert
|
|
if ! sudo /usr/bin/openssl x509 -req -sha256 -extensions SAN -CAcreateserial \
|
|
-extfile "${SSL_CONFIG_DIR}/${username}/${hostname}.cnf" \
|
|
-in "${SSL_CONFIG_DIR}/${username}/${hostname}.csr" \
|
|
-CA "${SSL_CA_CERT}" -CAkey "${SSL_CA_KEY}" \
|
|
-passin "pass:${SSL_CA_PASS}" \
|
|
-out "${SSL_CONFIG_DIR}/${username}/${hostname}/server.crt" \
|
|
-days "${SSL_DAYS}" >>"${LOGFILE}" 2>&1; then
|
|
printf 'Failed to generate SSL cert %s/%s/server.crt\n' "${username}" "${hostname}" >>"${LOGFILE}"
|
|
exit 7
|
|
fi
|
|
if ! [ -f "${SSL_CONFIG_DIR:?}/${username:?}/${hostname:?}/server.crt" ]; then
|
|
printf 'SSL key %s/%s/server.crt was not generated!\n' "${username}" "${hostname}" >>"${LOGFILE}"
|
|
exit 7
|
|
fi
|
|
if ! sudo chmod 644 "${SSL_CONFIG_DIR}/${username}/${hostname}/server.crt"; then
|
|
printf 'Failed to chmod SSL cert %s/%s/server.crt\n' "${username}" "${hostname}" >>"${LOGFILE}"
|
|
exit 7
|
|
fi
|
|
|
|
# Remove old files
|
|
if ! sudo rm "${SSL_CONFIG_DIR}/${username}/${hostname}.cnf" "${SSL_CONFIG_DIR}/${username}/${hostname}.csr" 2>/dev/null; then
|
|
printf 'Failed to remove old SSL config files %s/%s/%s.cnf\n' "${SSL_CONFIG_DIR}" "${username}" "${hostname}" >>"${LOGFILE}"
|
|
exit 7
|
|
fi
|
|
|
|
printf 'SSL certs for %s.%s.%s are ready\n' "${hostname}" "${username}" "${TLD}" >>"${LOGFILE}" |