wagon/back/lib/ssl_peer_add

96 lines
3.4 KiB
Plaintext
Raw Normal View History

2022-08-29 01:04:37 -06:00
#!/bin/bash
# FILE: wgapi:back/lib/ssl/peer/add
# DESCRIPTION: Create SSL certs for a new host
# USAGE: add hostname username ipstring
2022-09-06 20:57:41 -06:00
# ERRORS:
# 3: Bad usage
# 4: config file not found
# 5: openssl or config not found
# 6: not root
# 7: openssl failed
2022-08-29 01:04:37 -06:00
2022-09-06 20:57:41 -06:00
CONFIG_FILE='/etc/wgapi/config'
2022-09-10 10:54:30 -06:00
[ ${#} -eq 0 ] || (
printf 'ERROR! Invalid number of arguments to %s: %s\n' "${0}" "${*}" >>"${LOGFILE}"
exit 3
)
[ -f "${CONFIG_FILE}" ] || (
printf 'ERROR! %s couldnt find %s\n' "${0}" "${*}" >>"${LOGFILE}"
exit 4
)
[ -x '/usr/bin/openssl' ] || (
printf 'ERROR! /usr/bin/openssl not found!\n' >>"${LOGFILE}"
exit 5
)
[ -f '/etc/ssl/openssl.cnf' ] || (
printf 'ERROR! /etc/ssl/openssl.cnf not found!\n' >>"${LOGFILE}"
exit 5
)
2022-09-06 20:57:41 -06:00
source "${CONFIG_FILE}"
2022-09-09 20:02:39 -06:00
hostname="${1}"
username="${2}"
ipstring="${3}"
printf 'Signing SSL certs for %s.%s.%s...\n' "${hostname}" "${username}" "${TLD}" >>"${LOGFILE}"
2022-09-08 21:15:30 -06:00
# Generate key
2022-09-10 10:54:30 -06:00
sudo /usr/bin/openssl genrsa -out "${SSL_CONFIG_DIR:?}/${username:?}/${hostname:?}/server.key" >/dev/null 2>&1 || (
2022-09-09 20:02:39 -06:00
printf 'Failed to generate SSL key %s/%s/server.key\n' "${username}" "${hostname}" >>"${LOGFILE}"
exit 7
)
[ -f "${SSL_CONFIG_DIR:?}/${username:?}/${hostname:?}/server.key" ] || (
printf 'SSL key %s/%s/server.key was not generated!\n' "${username}" "${hostname}" >>"${LOGFILE}"
exit 7
)
2022-09-10 10:54:30 -06:00
sudo chmod 400 "${SSL_CONFIG_DIR}/${username}/${hostname}/server.key" || (
2022-09-09 20:02:39 -06:00
printf 'Failed to chmod SSL key %s/%s/server.key\n' "${username}" "${hostname}" >>"${LOGFILE}"
exit 7
)
# Generate config
2022-09-09 20:02:39 -06:00
san="\n[SAN]\nsubjectAltNames=DNS:${hostname:?}.${username:?}.${TLD:?},DNS:*.${hostname:?}.${username:?}.${TLD:?}"
[ "${ipstring}" != "" ] && san="${san},${ipstring}"
2022-09-08 21:15:30 -06:00
cat '/etc/ssl/openssl.cnf' <(printf '%s' "${san}") \
2022-09-09 20:02:39 -06:00
> "${SSL_CONFIG_DIR:?}/${username:?}/${hostname:?}.cnf" || (
printf 'Failed to generate %s/%s.cnf\n' "${username}" "${hostname}" >>"${LOGFILE}"
exit 7
)
# Generate CSR
2022-09-10 10:54:30 -06:00
sudo /usr/bin/openssl req -new -sha256 -reqexts SAN \
-key "${SSL_CONFIG_DIR}/${username}/${hostname}/server.key" \
2022-09-08 21:15:30 -06:00
-out "${SSL_CONFIG_DIR}/${username}/${hostname}.csr" \
-config "${SSL_CONFIG_DIR}/${username}/${hostname}.cnf" \
-subj "/O=${SSL_ORG}/OU=${username}/CN=${hostname}.${username}.${TLD}" \
2022-09-09 20:02:39 -06:00
>/dev/null 2>&1 || (
printf 'Failed to generate %s/%s.cnf\n' "${username}" "${hostname}" >>"${LOGFILE}"
exit 7
)
# Generate cert
2022-09-10 10:54:30 -06:00
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}" \
2022-09-08 21:15:30 -06:00
-out "${SSL_CONFIG_DIR}/${username}/${hostname}/server.crt" \
2022-09-09 20:02:39 -06:00
-days "${SSL_DAYS}" >/dev/null 2>&1 || (
printf 'Failed to generate SSL cert %s/%s/server.crt\n' "${username}" "${hostname}" >>"${LOGFILE}"
exit 7
)
[ -f "${SSL_CONFIG_DIR:?}/${username:?}/${hostname:?}/server.crt" ] || (
printf 'SSL key %s/%s/server.crt was not generated!\n' "${username}" "${hostname}" >>"${LOGFILE}"
exit 7
)
2022-09-10 10:54:30 -06:00
sudo chmod 644 "${SSL_CONFIG_DIR}/${username}/${hostname}/server.crt" || (
2022-09-09 20:02:39 -06:00
printf 'Failed to chmod SSL cert %s/%s/server.crt\n' "${username}" "${hostname}" >>"${LOGFILE}"
exit 7
)
# Remove old files
2022-09-10 10:54:30 -06:00
sudo rm "${SSL_CONFIG_DIR}/${username}/${hostname}.cnf" "${SSL_CONFIG_DIR}/${username}/${hostname}.csr" 2>/dev/null
2022-09-09 20:02:39 -06:00
printf 'SSL certs for %s.%s.%s are ready\n' "${hostname}" "${username}" "${TLD}" >>"${LOGFILE}"