wagon/back/lib/ssl_peer_add

51 lines
1.8 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'
[ ${#} -eq 0 ] || exit 3
(( ${EUID} == 0 )) || exit 6
[ -f "${CONFIG_FILE}" ] || exit 4
[ -x '/usr/bin/openssl' ] || exit 5
[ -f '/etc/ssl/openssl.cnf' ] || exit 5
source "${CONFIG_FILE}"
# Generate key
2022-09-06 20:57:41 -06:00
/usr/bin/openssl genrsa -out "${SSL_CONFIG_DIR}/${username}/${hostname}/server.key" >/dev/null 2>&1 || exit 7
chmod 400 "${SSL_CONFIG_DIR}/${username}/${hostname}/server.key"
# Generate config
cat '/etc/ssl/openssl.cnf' \
<(printf "\n[SAN]\nsubjectAltNames=DNS:${hostname}.${username}.${TLD},DNS:*.${hostname}.${username}.${TLD},${3}") \
> "${SSL_CONFIG_DIR}/${username}/${hostname}.cnf"
# Generate CSR
2022-09-06 20:57:41 -06:00
/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}" \
2022-09-06 20:57:41 -06:00
>/dev/null 2>&1 || exit 7
# Generate cert
2022-09-06 20:57:41 -06:00
/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"
2022-09-06 20:57:41 -06:00
-days "${SSL_DAYS}" >/dev/null 2>&1 || exit 7
chmod 644 "${SSL_CONFIG_DIR}/${username}/${hostname}/server.crt"
# Remove old files
rm "${SSL_CONFIG_DIR}/${username}/${hostname}.cnf" "${SSL_CONFIG_DIR}/${username}/${hostname}.csr" 2>/dev/null