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

136 lines
4.9 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
2016-03-24 19:22:39 -06:00
# Version: 1.2
2016-03-24 18:55:56 -06:00
# Last Changed: 03/24/2016
2016-02-27 14:19:29 -07:00
# 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-03-24 18:55:56 -06:00
# 03/24/2016: More sanity checking, embedding cert
2016-02-27 14:19:29 -07:00
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
2016-03-24 18:55:56 -06:00
while getopts "ird:e:" opt; do
2016-03-08 12:04:03 -07:00
case $opt in
2016-03-24 18:55:56 -06:00
i) onlyinsert="yes";;
2016-03-08 12:04:03 -07:00
r) renew="yes";;
d) domains+=("$OPTARG");;
e) email=("$OPTARG");;
esac
done
2016-02-27 14:19:29 -07:00
2016-03-24 18:55:56 -06:00
# Location of LetsEncrypt binary we use
2016-06-20 10:00:15 -06:00
LEBINARY="/usr/src/letsencrypt/certbot-auto"
2016-02-27 14:19:29 -07:00
2016-03-24 18:55:56 -06:00
# Command line options depending on New or Renew.
2016-03-08 12:04:03 -07:00
NEWCERT="--renew-by-default certonly"
RENEWCERT="-n renew"
2016-03-24 18:55:56 -06:00
if [[ ! -x ${LEBINARY} ]]; then
echo "Error: LetsEncrypt binary not found in ${LEBINARY} !"
echo "You'll need to do one of the following:"
echo "1) Change LEBINARY variable in this script"
echo "2) Install LE manually or via your package manager and do #1"
echo "3) Use the included get-letsencrypt.sh script to install it"
exit 1
fi
2016-03-08 12:04:03 -07:00
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:21:26 -07:00
MAINDOMAIN=${domains[0]}
2016-03-08 12:04:03 -07:00
2016-03-24 18:59:12 -06:00
if [[ -z ${MAINDOMAIN} ]]; then
2016-03-24 18:55:56 -06:00
echo "Error: At least one -d argument is required"
exit 1
fi
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
2016-03-24 18:55:56 -06:00
if [[ ${onlyinsert} != "yes" ]]; then
echo "Firing up standalone authenticator on TCP port 443 and requesting cert..."
${LEBINARY} \
--server https://acme-v01.api.letsencrypt.org/directory \
--agree-tos \
--standalone --standalone-supported-challenges tls-sni-01 \
${LEOPTIONS}
fi
2016-03-08 12:04:03 -07:00
2016-03-08 12:19:24 -07:00
if `md5sum -c /etc/letsencrypt/live/${MAINDOMAIN}/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)
2016-03-24 18:55:56 -06:00
CERTTEMPFILE=$(mktemp)
# 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
cat > ${CERTTEMPFILE} <<'_EOF'
-----BEGIN CERTIFICATE-----
MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/
MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow
PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD
Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O
rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq
OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b
xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw
7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD
aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV
HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG
SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69
ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr
AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz
R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5
JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo
Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ
-----END CERTIFICATE-----
_EOF
2016-03-08 12:04:03 -07:00
echo "Cert has changed, updating controller..."
2016-03-08 12:16:52 -07:00
md5sum /etc/letsencrypt/live/${MAINDOMAIN}/cert.pem > /etc/letsencrypt/live/${MAINDOMAIN}/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:16:52 -07:00
-in /etc/letsencrypt/live/${MAINDOMAIN}/cert.pem \
-inkey /etc/letsencrypt/live/${MAINDOMAIN}/privkey.pem \
2016-03-08 12:04:03 -07:00
-out ${TEMPFILE} -name unifi \
2016-03-08 12:16:52 -07:00
-CAfile /etc/letsencrypt/live/${MAINDOMAIN}/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 \
2016-03-08 12:32:11 -07:00
-deststorepass aircontrolenterprise \
-destkeypass aircontrolenterprise \
2016-03-08 12:04:03 -07:00
-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:16:52 -07:00
/etc/letsencrypt/live/${MAINDOMAIN}/cert.pem \
/etc/letsencrypt/live/${MAINDOMAIN}/chain.pem \
2016-03-24 18:55:56 -06:00
${CERTTEMPFILE}
rm -f ${CERTTEMPFILE}
2016-03-08 12:04:03 -07:00
echo "Starting Unifi controller..."
service unifi start
echo "Done!"
fi