lets-encrypt-scripts/gen-unifi-cert.sh

88 lines
3.0 KiB
Bash
Raw Normal View History

2016-02-19 21:13:25 -07:00
#!/usr/bin/env bash
# Modified script from here: https://github.com/FarsetLabs/letsencrypt-helper-scripts/blob/master/letsencrypt-unifi.sh
# Modified by: Brielle Bruns <bruns@2mbit.com>
2016-02-27 14:19:29 -07:00
# Download URL: https://source.sosdg.org/brielle/lets-encrypt-scripts
# Last Changed: 2/27/2016
# 02/02/2016: Fixed some errors with key export/import, removed lame docker requirements
# 02/27/2016: More verbose progress report
2016-03-08 12:04:03 -07:00
# 03/08/2016: Add renew option, reformat code, command line options
2016-02-27 14:19:29 -07:00
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
2016-03-08 12:04:03 -07:00
while getopts "rd:e:" opt; do
case $opt in
r) renew="yes";;
d) domains+=("$OPTARG");;
e) email=("$OPTARG");;
esac
done
2016-02-27 14:19:29 -07:00
# Identrust cross-signed CA cert needed by the java keystore for import.
# Can get original here: https://www.identrust.com/certificates/trustid/root-download-x3.html
2016-02-19 21:13:25 -07:00
EXTRACERT="/root/DSTROOTCAX3.txt"
2016-02-27 14:19:29 -07:00
2016-03-08 12:04:03 -07:00
NEWCERT="--renew-by-default certonly"
RENEWCERT="-n renew"
if [[ ! -z ${email} ]]; then
email="--email ${email}"
else
email=""
fi
shift $((OPTIND -1))
for val in "${domains[@]}"; do
DOMAINS="${DOMAINS} -d ${val} "
done
2016-03-08 12:07:03 -07:00
if [[ $renew == "yes" ]]; then
2016-03-08 12:04:03 -07:00
LEOPTIONS=${RENEWCERT}
else
LEOPTIONS="${email} ${DOMAINS} ${NEWCERT}"
fi
2016-02-27 14:19:29 -07:00
echo "Firing up standalone authenticator on TCP port 443 and requesting cert..."
2016-02-19 21:13:25 -07:00
/usr/src/letsencrypt/letsencrypt-auto \
--server https://acme-v01.api.letsencrypt.org/directory \
2016-03-08 12:04:03 -07:00
--agree-tos \
2016-02-19 21:13:25 -07:00
--standalone --standalone-supported-challenges tls-sni-01 \
2016-03-08 12:04:03 -07:00
${LEOPTIONS}
2016-03-08 12:11:20 -07:00
if `md5sum -c /etc/letsencrypt/live/${DOMAIN}/cert.pem.md5 &>/dev/null`; then
2016-03-08 12:04:03 -07:00
echo "Cert has not changed, not updating controller."
exit 0
else
TEMPFILE=$(mktemp)
echo "Cert has changed, updating controller..."
2016-03-08 12:12:27 -07:00
md5sum /etc/letsencrypt/live/${domains[0]}/cert.pem > /etc/letsencrypt/live/${domains[0]}/cert.pem.md5
2016-03-08 12:04:03 -07:00
echo "Using openssl to prepare certificate..."
openssl pkcs12 -export -passout pass:aircontrolenterprise \
2016-03-08 12:11:20 -07:00
-in /etc/letsencrypt/live/${domains[0]}/cert.pem \
-inkey /etc/letsencrypt/live/${domains[0]}/privkey.pem \
2016-03-08 12:04:03 -07:00
-out ${TEMPFILE} -name unifi \
2016-03-08 12:11:20 -07:00
-CAfile /etc/letsencrypt/live/${domains[0]}/chain.pem -caname root
2016-03-08 12:04:03 -07:00
echo "Stopping Unifi controller..."
service unifi stop
echo "Removing existing certificate from Unifi protected keystore..."
keytool -delete -alias unifi -keystore /usr/lib/unifi/data/keystore \
-deststorepass aircontrolenterprise
echo "Inserting certificate into Unifi keystore..."
keytool -trustcacerts -importkeystore \
-deststorepass aircontrolenterprise \
-destkeypass aircontrolenterprise \
-destkeystore /usr/lib/unifi/data/keystore \
-srckeystore ${TEMPFILE} -srcstoretype PKCS12 \
-srcstorepass aircontrolenterprise \
-alias unifi
rm -f ${TEMPFILE}
echo "Importing cert into Unifi database..."
java -jar /usr/lib/unifi/lib/ace.jar import_cert \
2016-03-08 12:11:20 -07:00
/etc/letsencrypt/live/${domains[0]}/cert.pem \
/etc/letsencrypt/live/${domains[0]}/chain.pem \
2016-03-08 12:04:03 -07:00
${EXTRACERT}
echo "Starting Unifi controller..."
service unifi start
echo "Done!"
fi