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.

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