-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-unifi-cert.sh
executable file
·88 lines (83 loc) · 4.02 KB
/
gen-unifi-cert.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
# Modified script from here: https://github.com/FarsetLabs/letsencrypt-helper-scripts/blob/master/letsencrypt-unifi.sh
# Modified by: Brielle Bruns <[email protected]>
# Download URL: https://source.sosdg.org/brielle/lets-encrypt-scripts
# Version: 1.5
# Last Changed: 02/04/2018
#!/usr/bin/env bash
# Modified script from here: https://github.com/FarsetLabs/letsencrypt-helper-scripts/blob/master/letsencrypt-unifi.sh
# Modified by: Brielle Bruns <[email protected]>
# Download URL: https://source.sosdg.org/brielle/lets-encrypt-scripts
# Version: 1.5
# Last Changed: 02/04/2018
# 02/02/2016: Fixed some errors with key export/import, removed lame docker requirements
# 02/27/2016: More verbose progress report
# 03/08/2016: Add renew option, reformat code, command line options
# 03/24/2016: More sanity checking, embedding cert
# 10/23/2017: Apparently don't need the ace.jar parts, so disable them
# 02/04/2018: LE disabled tls-sni-01, so switch to just tls-sni, as certbot 0.22 and later automatically fall back to http/80 for auth
if [[ -z ${MAINDOMAIN} ]]; then
echo "Error: At least one -d argument is required"
exit 1
fi
if `md5sum -c /opt/unifi/${MAINDOMAIN}/${MAINDOMAIN}.cer.md5 &>/dev/null`; then
echo "Cert has not changed, not updating controller."
exit 0
else
TEMPFILE=$(mktemp)
CATEMPFILE=$(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 > "${CATEMPFILE}" <<'_EOF'
-----BEGIN CERTIFICATE-----
MIIDSjCCAjKgAwIBAgIQRK+wgNajJ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ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ
-----END CERTIFICATE-----
_EOF
echo "Cert has changed, updating controller..."
md5sum /opt/unifi/${MAINDOMAIN}/${MAINDOMAIN}.cer > /opt/unifi/${MAINDOMAIN}/${MAINDOMAIN}.pem.md5
echo "Using openssl to prepare certificate..."
cat /opt/unifi/${MAINDOMAIN}/fullchain.cer >> "${CATEMPFILE}"
openssl pkcs12 -export -passout pass:aircontrolenterprise \
-in /opt/unifi/${MAINDOMAIN}/${MAINDOMAIN}.cer \
-inkey /opt/unifi/${MAINDOMAIN}/${MAINDOMAIN}.key \
-out "${TEMPFILE}" -name unifi \
-CAfile "${CATEMPFILE}" -caname root
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.tmp \
-deststoretype PKCS12 \
-srckeystore "${TEMPFILE}" -srcstoretype PKCS12 \
-srcstorepass aircontrolenterprise \
-alias unifi
rm -f "${TEMPFILE}" "${CATEMPFILE}"
rm -f /usr/lib/unifi/data/keystore
mv /usr/lib/unifi/data/keystore.tmp /usr/lib/unifi/data/keystore
rm /usr/lib/unifi/data/keystore.tmp
echo "Starting Unifi controller..."
service unifi start
echo "Done!"
fi