You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
12 KiB

  1. #!/usr/bin/env sh
  2. # Here is a script to deploy cert on a Unifi Controller or Cloud Key device.
  3. # It supports:
  4. # - self-hosted Unifi Controller
  5. # - Unifi Cloud Key (Gen1/2/2+)
  6. # - Unifi Cloud Key running UnifiOS (v2.0.0+, Gen2/2+ only)
  7. # See below regarding keytool. Not tested.
  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. #
  17. # See below regarding keytool. Not tested.
  18. # - Unifi Dream Machine
  19. # This has not been tested on other "all-in-one" devices such as
  20. # UDM Pro or Unifi Express.
  21. #
  22. # OS Version v2.0.0+
  23. # Network Application version 7.0.0+
  24. # OS version ~3.1 removed java and keytool from the UnifiOS.
  25. # Using PKCS12 format keystore appears to work fine.
  26. #
  27. # Please report bugs to https://github.com/acmesh-official/acme.sh/issues/3359
  28. #returns 0 means success, otherwise error.
  29. # The deploy-hook automatically detects standard Unifi installations
  30. # for each of the supported environments. Most users should not need
  31. # to set any of these variables, but if you are running a self-hosted
  32. # Controller with custom locations, set these as necessary before running
  33. # the deploy hook. (Defaults shown below.)
  34. #
  35. # Settings for Unifi Controller:
  36. # Location of Java keystore or unifi.keystore.jks file:
  37. #DEPLOY_UNIFI_KEYSTORE="/usr/lib/unifi/data/keystore"
  38. # Keystore password (built into Unifi Controller, not a user-set password):
  39. #DEPLOY_UNIFI_KEYPASS="aircontrolenterprise"
  40. # Command to restart Unifi Controller:
  41. #DEPLOY_UNIFI_RELOAD="service unifi restart"
  42. #
  43. # Settings for Unifi Cloud Key Gen1 (nginx admin pages):
  44. # Directory where cloudkey.crt and cloudkey.key live:
  45. #DEPLOY_UNIFI_CLOUDKEY_CERTDIR="/etc/ssl/private"
  46. # Command to restart maintenance pages and Controller
  47. # (same setting as above, default is updated when running on Cloud Key Gen1):
  48. #DEPLOY_UNIFI_RELOAD="service nginx restart && service unifi restart"
  49. #
  50. # Settings for UnifiOS (Cloud Key Gen2):
  51. # Directory where unifi-core.crt and unifi-core.key live:
  52. #DEPLOY_UNIFI_CORE_CONFIG="/data/unifi-core/config/"
  53. # Command to restart unifi-core:
  54. #DEPLOY_UNIFI_RELOAD="systemctl restart unifi-core"
  55. #
  56. # At least one of DEPLOY_UNIFI_KEYSTORE, DEPLOY_UNIFI_CLOUDKEY_CERTDIR,
  57. # or DEPLOY_UNIFI_CORE_CONFIG must exist to receive the deployed certs.
  58. ######## Public functions #####################
  59. #domain keyfile certfile cafile fullchain
  60. unifi_deploy() {
  61. _cdomain="$1"
  62. _ckey="$2"
  63. _ccert="$3"
  64. _cca="$4"
  65. _cfullchain="$5"
  66. _debug _cdomain "$_cdomain"
  67. _debug _ckey "$_ckey"
  68. _debug _ccert "$_ccert"
  69. _debug _cca "$_cca"
  70. _debug _cfullchain "$_cfullchain"
  71. _getdeployconf DEPLOY_UNIFI_KEYSTORE
  72. _getdeployconf DEPLOY_UNIFI_KEYPASS
  73. _getdeployconf DEPLOY_UNIFI_CLOUDKEY_CERTDIR
  74. _getdeployconf DEPLOY_UNIFI_CORE_CONFIG
  75. _getdeployconf DEPLOY_UNIFI_RELOAD
  76. _debug2 DEPLOY_UNIFI_KEYSTORE "$DEPLOY_UNIFI_KEYSTORE"
  77. _debug2 DEPLOY_UNIFI_KEYPASS "$DEPLOY_UNIFI_KEYPASS"
  78. _debug2 DEPLOY_UNIFI_CLOUDKEY_CERTDIR "$DEPLOY_UNIFI_CLOUDKEY_CERTDIR"
  79. _debug2 DEPLOY_UNIFI_CORE_CONFIG "$DEPLOY_UNIFI_CORE_CONFIG"
  80. _debug2 DEPLOY_UNIFI_RELOAD "$DEPLOY_UNIFI_RELOAD"
  81. # Space-separated list of environments detected and installed:
  82. _services_updated=""
  83. # Default reload commands accumulated as we auto-detect environments:
  84. _reload_cmd=""
  85. # Unifi Controller environment (self hosted or any Cloud Key) --
  86. # auto-detect by file /usr/lib/unifi/data/keystore
  87. # auto-detect by file /usr/lib/unifi/data/keystore
  88. _unifi_keystore="${DEPLOY_UNIFI_KEYSTORE:-/usr/lib/unifi/data/keystore}"
  89. if [ -f "$_unifi_keystore" ]; then
  90. _debug _unifi_keystore "$_unifi_keystore"
  91. if ! _exists keytool; then
  92. _do_keytool=0
  93. _info "Installing certificate for Unifi Controller (PKCS12 keystore)."
  94. else
  95. _do_keytool=1
  96. _info "Installing certificate for Unifi Controller (Java keystore)"
  97. fi
  98. if [ ! -w "$_unifi_keystore" ]; then
  99. _err "The file $_unifi_keystore is not writable, please change the permission."
  100. return 1
  101. fi
  102. _unifi_keypass="${DEPLOY_UNIFI_KEYPASS:-aircontrolenterprise}"
  103. _debug "Generate import pkcs12"
  104. _import_pkcs12="$(_mktemp)"
  105. _debug "_toPkcs $_import_pkcs12 $_ckey $_ccert $_cca $_unifi_keypass unifi root"
  106. _toPkcs "$_import_pkcs12" "$_ckey" "$_ccert" "$_cca" "$_unifi_keypass" unifi root
  107. # shellcheck disable=SC2181
  108. if [ "$?" != "0" ]; then
  109. _err "Error generating pkcs12. Please re-run with --debug and report a bug."
  110. return 1
  111. fi
  112. # Save the existing keystore in case something goes wrong.
  113. mv -f "${_unifi_keystore}" "${_unifi_keystore}"_original
  114. _info "Previous keystore saved to ${_unifi_keystore}_original."
  115. if [ "$_do_keytool" -eq 1 ]; then
  116. _debug "Import into keystore: $_unifi_keystore"
  117. if keytool -importkeystore \
  118. -deststorepass "$_unifi_keypass" -destkeypass "$_unifi_keypass" -destkeystore "$_unifi_keystore" \
  119. -srckeystore "$_import_pkcs12" -srcstoretype PKCS12 -srcstorepass "$_unifi_keypass" \
  120. -alias unifi -noprompt; then
  121. _debug "Import keystore success!"
  122. else
  123. _err "Error importing into Unifi Java keystore."
  124. _err "Please re-run with --debug and report a bug."
  125. _info "Restoring original keystore."
  126. mv -f "${_unifi_keystore}"_original "${_unifi_keystore}"
  127. rm "$_import_pkcs12"
  128. return 1
  129. fi
  130. else
  131. _debug "Copying new keystore to $_unifi_keystore"
  132. cp -f "$_import_pkcs12" "$_unifi_keystore"
  133. fi
  134. # Update unifi service for certificate cipher compatibility
  135. if ${ACME_OPENSSL_BIN:-openssl} pkcs12 \
  136. -in "$_import_pkcs12" \
  137. -password pass:aircontrolenterprise \
  138. -nokeys | ${ACME_OPENSSL_BIN:-openssl} x509 -text \
  139. -noout | grep -i "signature" | grep -iq ecdsa >/dev/null 2>&1; then
  140. cp -f /usr/lib/unifi/data/system.properties /usr/lib/unifi/data/system.properties_original
  141. _info "Updating system configuration for cipher compatibility."
  142. _info "Saved original system config to /usr/lib/unifi/data/system.properties_original"
  143. sed -i '/unifi\.https\.ciphers/d' /usr/lib/unifi/data/system.properties
  144. echo "unifi.https.ciphers=ECDHE-ECDSA-AES256-GCM-SHA384,ECDHE-RSA-AES128-GCM-SHA256" >>/usr/lib/unifi/data/system.properties
  145. sed -i '/unifi\.https\.sslEnabledProtocols/d' /usr/lib/unifi/data/system.properties
  146. echo "unifi.https.sslEnabledProtocols=TLSv1.3,TLSv1.2" >>/usr/lib/unifi/data/system.properties
  147. _info "System configuration updated."
  148. fi
  149. rm "$_import_pkcs12"
  150. # Restarting unifi-core will bring up unifi, doing it out of order results in
  151. # a certificate error, and breaks wifiman.
  152. # Restart if we aren't doing unifi-core, otherwise stop for later restart.
  153. if systemctl -q is-active unifi; then
  154. if [ ! -f "${DEPLOY_UNIFI_CORE_CONFIG:-/data/unifi-core/config}/unifi-core.key" ]; then
  155. _reload_cmd="${_reload_cmd:+$_reload_cmd && }systemctl restart unifi"
  156. else
  157. _reload_cmd="${_reload_cmd:+$_reload_cmd && }systemctl stop unifi"
  158. fi
  159. if [ ! -f "${DEPLOY_UNIFI_CORE_CONFIG:-/data/unifi-core/config}/unifi-core.key" ]; then
  160. _reload_cmd="${_reload_cmd:+$_reload_cmd && }systemctl restart unifi"
  161. else
  162. _reload_cmd="${_reload_cmd:+$_reload_cmd && }systemctl stop unifi"
  163. fi
  164. fi
  165. _services_updated="${_services_updated} unifi"
  166. _info "Install Unifi Controller certificate success!"
  167. elif [ "$DEPLOY_UNIFI_KEYSTORE" ]; then
  168. _err "The specified DEPLOY_UNIFI_KEYSTORE='$DEPLOY_UNIFI_KEYSTORE' is not valid, please check."
  169. return 1
  170. fi
  171. # Cloud Key environment (non-UnifiOS -- nginx serves admin pages) --
  172. # auto-detect by file /etc/ssl/private/cloudkey.key:
  173. _cloudkey_certdir="${DEPLOY_UNIFI_CLOUDKEY_CERTDIR:-/etc/ssl/private}"
  174. if [ -f "${_cloudkey_certdir}/cloudkey.key" ]; then
  175. _info "Installing certificate for Cloud Key Gen1 (nginx admin pages)"
  176. _debug _cloudkey_certdir "$_cloudkey_certdir"
  177. if [ ! -w "$_cloudkey_certdir" ]; then
  178. _err "The directory $_cloudkey_certdir is not writable; please check permissions."
  179. return 1
  180. fi
  181. # Cloud Key expects to load the keystore from /etc/ssl/private/unifi.keystore.jks.
  182. # Normally /usr/lib/unifi/data/keystore is a symlink there (so the keystore was
  183. # updated above), but if not, we don't know how to handle this installation:
  184. if ! cmp -s "$_unifi_keystore" "${_cloudkey_certdir}/unifi.keystore.jks"; then
  185. _err "Unsupported Cloud Key configuration: keystore not found at '${_cloudkey_certdir}/unifi.keystore.jks'"
  186. return 1
  187. fi
  188. cat "$_cfullchain" >"${_cloudkey_certdir}/cloudkey.crt"
  189. cat "$_ckey" >"${_cloudkey_certdir}/cloudkey.key"
  190. (cd "$_cloudkey_certdir" && tar -cf cert.tar cloudkey.crt cloudkey.key unifi.keystore.jks)
  191. if systemctl -q is-active nginx; then
  192. _reload_cmd="${_reload_cmd:+$_reload_cmd && }service nginx restart"
  193. fi
  194. _info "Install Cloud Key Gen1 certificate success!"
  195. _services_updated="${_services_updated} nginx"
  196. elif [ "$DEPLOY_UNIFI_CLOUDKEY_CERTDIR" ]; then
  197. _err "The specified DEPLOY_UNIFI_CLOUDKEY_CERTDIR='$DEPLOY_UNIFI_CLOUDKEY_CERTDIR' is not valid, please check."
  198. return 1
  199. fi
  200. # UnifiOS environment -- auto-detect by /data/unifi-core/config/unifi-core.key:
  201. _unifi_core_config="${DEPLOY_UNIFI_CORE_CONFIG:-/data/unifi-core/config}"
  202. if [ -f "${_unifi_core_config}/unifi-core.key" ]; then
  203. _info "Installing certificate for UnifiOS"
  204. _debug _unifi_core_config "$_unifi_core_config"
  205. if [ ! -w "$_unifi_core_config" ]; then
  206. _err "The directory $_unifi_core_config is not writable; please check permissions."
  207. return 1
  208. fi
  209. # Save the existing certs in case something goes wrong.
  210. cp -f "${_unifi_core_config}"/unifi-core.crt "${_unifi_core_config}"/unifi-core_original.crt
  211. cp -f "${_unifi_core_config}"/unifi-core.key "${_unifi_core_config}"/unifi-core_original.key
  212. _info "Previous certificate and key saved to ${_unifi_core_config}/unifi-core_original.crt/key."
  213. # Save the existing certs in case something goes wrong.
  214. cp -f "${_unifi_core_config}"/unifi-core.crt "${_unifi_core_config}"/unifi-core_original.crt
  215. cp -f "${_unifi_core_config}"/unifi-core.key "${_unifi_core_config}"/unifi-core_original.key
  216. _info "Previous certificate and key saved to ${_unifi_core_config}/unifi-core_original.crt/key."
  217. cat "$_cfullchain" >"${_unifi_core_config}/unifi-core.crt"
  218. cat "$_ckey" >"${_unifi_core_config}/unifi-core.key"
  219. if systemctl -q is-active unifi-core; then
  220. _reload_cmd="${_reload_cmd:+$_reload_cmd && }systemctl restart unifi-core"
  221. fi
  222. _info "Install UnifiOS certificate success!"
  223. _services_updated="${_services_updated} unifi-core"
  224. elif [ "$DEPLOY_UNIFI_CORE_CONFIG" ]; then
  225. _err "The specified DEPLOY_UNIFI_CORE_CONFIG='$DEPLOY_UNIFI_CORE_CONFIG' is not valid, please check."
  226. return 1
  227. fi
  228. if [ -z "$_services_updated" ]; then
  229. # None of the Unifi environments were auto-detected, so no deployment has occurred
  230. # (and none of DEPLOY_UNIFI_{KEYSTORE,CLOUDKEY_CERTDIR,CORE_CONFIG} were set).
  231. _err "Unable to detect Unifi environment in standard location."
  232. _err "(This deploy hook must be run on the Unifi device, not a remote machine.)"
  233. _err "For non-standard Unifi installations, set DEPLOY_UNIFI_KEYSTORE,"
  234. _err "DEPLOY_UNIFI_CLOUDKEY_CERTDIR, and/or DEPLOY_UNIFI_CORE_CONFIG as appropriate."
  235. return 1
  236. fi
  237. _reload_cmd="${DEPLOY_UNIFI_RELOAD:-$_reload_cmd}"
  238. if [ -z "$_reload_cmd" ]; then
  239. _err "Certificates were installed for services:${_services_updated},"
  240. _err "but none appear to be active. Please set DEPLOY_UNIFI_RELOAD"
  241. _err "to a command that will restart the necessary services."
  242. return 1
  243. fi
  244. _info "Reload services (this may take some time): $_reload_cmd"
  245. if eval "$_reload_cmd"; then
  246. _info "Reload success!"
  247. else
  248. _err "Reload error"
  249. return 1
  250. fi
  251. # Successful, so save all (non-default) config:
  252. _savedeployconf DEPLOY_UNIFI_KEYSTORE "$DEPLOY_UNIFI_KEYSTORE"
  253. _savedeployconf DEPLOY_UNIFI_KEYPASS "$DEPLOY_UNIFI_KEYPASS"
  254. _savedeployconf DEPLOY_UNIFI_CLOUDKEY_CERTDIR "$DEPLOY_UNIFI_CLOUDKEY_CERTDIR"
  255. _savedeployconf DEPLOY_UNIFI_CORE_CONFIG "$DEPLOY_UNIFI_CORE_CONFIG"
  256. _savedeployconf DEPLOY_UNIFI_RELOAD "$DEPLOY_UNIFI_RELOAD"
  257. return 0
  258. }