5
5
# - self-hosted Unifi Controller
6
6
# - Unifi Cloud Key (Gen1/2/2+)
7
7
# - Unifi Cloud Key running UnifiOS (v2.0.0+, Gen2/2+ only)
8
+ # - Unifi Dream Machine
9
+ # This has not been tested on other "all-in-one" devices such as
10
+ # UDM Pro or Unifi Express.
11
+ #
12
+ # OS Version v2.0.0+
13
+ # Network Application version 7.0.0+
14
+ # OS version ~3.1 removed java and keytool from the UnifiOS.
15
+ # Using PKCS12 format keystore appears to work fine.
16
+ #
8
17
# Please report bugs to https://github.com/acmesh-official/acme.sh/issues/3359
9
18
10
19
# returns 0 means success, otherwise error.
@@ -74,14 +83,16 @@ unifi_deploy() {
74
83
_reload_cmd=" "
75
84
76
85
# Unifi Controller environment (self hosted or any Cloud Key) --
77
- # auto-detect by file /usr/lib/unifi/data/keystore:
86
+ # auto-detect by file /usr/lib/unifi/data/keystore
78
87
_unifi_keystore=" ${DEPLOY_UNIFI_KEYSTORE:-/ usr/ lib/ unifi/ data/ keystore} "
79
88
if [ -f " $_unifi_keystore " ]; then
80
- _info " Installing certificate for Unifi Controller (Java keystore)"
81
89
_debug _unifi_keystore " $_unifi_keystore "
82
90
if ! _exists keytool; then
83
- _err " keytool not found"
84
- return 1
91
+ _do_keytool=0
92
+ _info " Installing certificate for Unifi Controller (PKCS12 keystore)."
93
+ else
94
+ _do_keytool=1
95
+ _info " Installing certificate for Unifi Controller (Java keystore)"
85
96
fi
86
97
if [ ! -w " $_unifi_keystore " ]; then
87
98
_err " The file $_unifi_keystore is not writable, please change the permission."
@@ -92,29 +103,65 @@ unifi_deploy() {
92
103
93
104
_debug " Generate import pkcs12"
94
105
_import_pkcs12=" $( _mktemp) "
106
+ _debug " _toPkcs $_import_pkcs12 $_ckey $_ccert $_cca $_unifi_keypass unifi root"
95
107
_toPkcs " $_import_pkcs12 " " $_ckey " " $_ccert " " $_cca " " $_unifi_keypass " unifi root
96
108
# shellcheck disable=SC2181
97
109
if [ " $? " != " 0" ]; then
98
110
_err " Error generating pkcs12. Please re-run with --debug and report a bug."
99
111
return 1
100
112
fi
101
113
102
- _debug " Import into keystore: $_unifi_keystore "
103
- if keytool -importkeystore \
104
- -deststorepass " $_unifi_keypass " -destkeypass " $_unifi_keypass " -destkeystore " $_unifi_keystore " \
105
- -srckeystore " $_import_pkcs12 " -srcstoretype PKCS12 -srcstorepass " $_unifi_keypass " \
106
- -alias unifi -noprompt; then
107
- _debug " Import keystore success!"
108
- rm " $_import_pkcs12 "
114
+ # Save the existing keystore in case something goes wrong.
115
+ mv -f " ${_unifi_keystore} " " ${_unifi_keystore} " _original
116
+ _info " Previous keystore saved to ${_unifi_keystore} _original."
117
+
118
+ if [ " $_do_keytool " -eq 1 ]; then
119
+ _debug " Import into keystore: $_unifi_keystore "
120
+ if keytool -importkeystore \
121
+ -deststorepass " $_unifi_keypass " -destkeypass " $_unifi_keypass " -destkeystore " $_unifi_keystore " \
122
+ -srckeystore " $_import_pkcs12 " -srcstoretype PKCS12 -srcstorepass " $_unifi_keypass " \
123
+ -alias unifi -noprompt; then
124
+ _debug " Import keystore success!"
125
+ else
126
+ _err " Error importing into Unifi Java keystore."
127
+ _err " Please re-run with --debug and report a bug."
128
+ _info " Restoring original keystore."
129
+ mv -f " ${_unifi_keystore} " _original " ${_unifi_keystore} "
130
+ rm " $_import_pkcs12 "
131
+ return 1
132
+ fi
109
133
else
110
- _err " Error importing into Unifi Java keystore."
111
- _err " Please re-run with --debug and report a bug."
112
- rm " $_import_pkcs12 "
113
- return 1
134
+ _debug " Copying new keystore to $_unifi_keystore "
135
+ cp -f " $_import_pkcs12 " " $_unifi_keystore "
136
+ fi
137
+
138
+ # Update unifi service for certificate cipher compatibility
139
+ if ${ACME_OPENSSL_BIN:- openssl} pkcs12 \
140
+ -in " $_import_pkcs12 " \
141
+ -password pass:aircontrolenterprise \
142
+ -nokeys | ${ACME_OPENSSL_BIN:- openssl} x509 -text \
143
+ -noout | grep -i " signature" | grep -iq ecdsa > /dev/null 2>&1 ; then
144
+ cp -f /usr/lib/unifi/data/system.properties /usr/lib/unifi/data/system.properties_original
145
+ _info " Updating system configuration for cipher compatibility."
146
+ _info " Saved original system config to /usr/lib/unifi/data/system.properties_original"
147
+ sed -i ' /unifi\.https\.ciphers/d' /usr/lib/unifi/data/system.properties
148
+ echo " unifi.https.ciphers=ECDHE-ECDSA-AES256-GCM-SHA384,ECDHE-RSA-AES128-GCM-SHA256" >> /usr/lib/unifi/data/system.properties
149
+ sed -i ' /unifi\.https\.sslEnabledProtocols/d' /usr/lib/unifi/data/system.properties
150
+ echo " unifi.https.sslEnabledProtocols=TLSv1.3,TLSv1.2" >> /usr/lib/unifi/data/system.properties
151
+ _info " System configuration updated."
114
152
fi
115
153
154
+ rm " $_import_pkcs12 "
155
+
156
+ # Restarting unifi-core will bring up unifi, doing it out of order results in
157
+ # a certificate error, and breaks wifiman.
158
+ # Restart if we aren't doing unifi-core, otherwise stop for later restart.
116
159
if systemctl -q is-active unifi; then
117
- _reload_cmd=" ${_reload_cmd: +$_reload_cmd && } service unifi restart"
160
+ if [ ! -f " ${DEPLOY_UNIFI_CORE_CONFIG:-/ data/ unifi-core/ config} /unifi-core.key" ]; then
161
+ _reload_cmd=" ${_reload_cmd: +$_reload_cmd && } systemctl restart unifi"
162
+ else
163
+ _reload_cmd=" ${_reload_cmd: +$_reload_cmd && } systemctl stop unifi"
164
+ fi
118
165
fi
119
166
_services_updated=" ${_services_updated} unifi"
120
167
_info " Install Unifi Controller certificate success!"
@@ -165,6 +212,11 @@ unifi_deploy() {
165
212
return 1
166
213
fi
167
214
215
+ # Save the existing certs in case something goes wrong.
216
+ cp -f " ${_unifi_core_config} " /unifi-core.crt " ${_unifi_core_config} " /unifi-core_original.crt
217
+ cp -f " ${_unifi_core_config} " /unifi-core.key " ${_unifi_core_config} " /unifi-core_original.key
218
+ _info " Previous certificate and key saved to ${_unifi_core_config} /unifi-core_original.crt/key."
219
+
168
220
cat " $_cfullchain " > " ${_unifi_core_config} /unifi-core.crt"
169
221
cat " $_ckey " > " ${_unifi_core_config} /unifi-core.key"
170
222
0 commit comments