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.

276 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. fi
  144. rm "$_import_pkcs12"
  145. # Restarting unifi-core will bring up unifi, doing it out of order results in
  146. # a certificate error, and breaks wifiman.
  147. # Restart if we aren't doing Unifi OS (e.g. unifi-core service), otherwise stop for later restart.
  148. _unifi_reload="${DEPLOY_UNIFI_RELOAD:-systemctl restart unifi}"
  149. if [ ! -f "${DEPLOY_UNIFI_CORE_CONFIG:-/data/unifi-core/config}/unifi-core.key" ]; then
  150. _reload_cmd="${_reload_cmd:+$_reload_cmd && }$_unifi_reload"
  151. else
  152. _info "Stopping Unifi Controller for later restart."
  153. _unifi_stop=$(echo "${_unifi_reload}" | sed -e 's/restart/stop/')
  154. $_unifi_stop
  155. _reload_cmd="${_reload_cmd:+$_reload_cmd && }$_unifi_reload"
  156. _info "Unifi Controller stopped."
  157. fi
  158. _services_updated="${_services_updated} unifi"
  159. _info "Install Unifi Controller certificate success!"
  160. elif [ "$DEPLOY_UNIFI_KEYSTORE" ]; then
  161. _err "The specified DEPLOY_UNIFI_KEYSTORE='$DEPLOY_UNIFI_KEYSTORE' is not valid, please check."
  162. return 1
  163. fi
  164. # Cloud Key environment (non-UnifiOS -- nginx serves admin pages) --
  165. # auto-detect by file /etc/ssl/private/cloudkey.key:
  166. _cloudkey_certdir="${DEPLOY_UNIFI_CLOUDKEY_CERTDIR:-/etc/ssl/private}"
  167. if [ -f "${_cloudkey_certdir}/cloudkey.key" ]; then
  168. _info "Installing certificate for Cloud Key Gen1 (nginx admin pages)"
  169. _debug _cloudkey_certdir "$_cloudkey_certdir"
  170. if [ ! -w "$_cloudkey_certdir" ]; then
  171. _err "The directory $_cloudkey_certdir is not writable; please check permissions."
  172. return 1
  173. fi
  174. # Cloud Key expects to load the keystore from /etc/ssl/private/unifi.keystore.jks.
  175. # Normally /usr/lib/unifi/data/keystore is a symlink there (so the keystore was
  176. # updated above), but if not, we don't know how to handle this installation:
  177. if ! cmp -s "$_unifi_keystore" "${_cloudkey_certdir}/unifi.keystore.jks"; then
  178. _err "Unsupported Cloud Key configuration: keystore not found at '${_cloudkey_certdir}/unifi.keystore.jks'"
  179. return 1
  180. fi
  181. cat "$_cfullchain" >"${_cloudkey_certdir}/cloudkey.crt"
  182. cat "$_ckey" >"${_cloudkey_certdir}/cloudkey.key"
  183. (cd "$_cloudkey_certdir" && tar -cf cert.tar cloudkey.crt cloudkey.key unifi.keystore.jks)
  184. if systemctl -q is-active nginx; then
  185. _reload_cmd="${_reload_cmd:+$_reload_cmd && }service nginx restart"
  186. fi
  187. _info "Install Cloud Key Gen1 certificate success!"
  188. _services_updated="${_services_updated} nginx"
  189. elif [ "$DEPLOY_UNIFI_CLOUDKEY_CERTDIR" ]; then
  190. _err "The specified DEPLOY_UNIFI_CLOUDKEY_CERTDIR='$DEPLOY_UNIFI_CLOUDKEY_CERTDIR' is not valid, please check."
  191. return 1
  192. fi
  193. # UnifiOS environment -- auto-detect by /data/unifi-core/config/unifi-core.key:
  194. _unifi_core_config="${DEPLOY_UNIFI_CORE_CONFIG:-/data/unifi-core/config}"
  195. if [ -f "${_unifi_core_config}/unifi-core.key" ]; then
  196. _info "Installing certificate for UnifiOS"
  197. _debug _unifi_core_config "$_unifi_core_config"
  198. if [ ! -w "$_unifi_core_config" ]; then
  199. _err "The directory $_unifi_core_config is not writable; please check permissions."
  200. return 1
  201. fi
  202. # Save the existing certs in case something goes wrong.
  203. cp -f "${_unifi_core_config}"/unifi-core.crt "${_unifi_core_config}"/unifi-core_original.crt
  204. cp -f "${_unifi_core_config}"/unifi-core.key "${_unifi_core_config}"/unifi-core_original.key
  205. _info "Previous certificate and key saved to ${_unifi_core_config}/unifi-core_original.crt.key."
  206. cat "$_cfullchain" >"${_unifi_core_config}/unifi-core.crt"
  207. cat "$_ckey" >"${_unifi_core_config}/unifi-core.key"
  208. _unifi_os_reload="${DEPLOY_UNIFI_OS_RELOAD:-systemctl restart unifi-core}"
  209. _reload_cmd="${_reload_cmd:+$_reload_cmd && }$_unifi_os_reload"
  210. _info "Install UnifiOS certificate success!"
  211. _services_updated="${_services_updated} unifi-core"
  212. elif [ "$DEPLOY_UNIFI_CORE_CONFIG" ]; then
  213. _err "The specified DEPLOY_UNIFI_CORE_CONFIG='$DEPLOY_UNIFI_CORE_CONFIG' is not valid, please check."
  214. return 1
  215. fi
  216. if [ -z "$_services_updated" ]; then
  217. # None of the Unifi environments were auto-detected, so no deployment has occurred
  218. # (and none of DEPLOY_UNIFI_{KEYSTORE,CLOUDKEY_CERTDIR,CORE_CONFIG} were set).
  219. _err "Unable to detect Unifi environment in standard location."
  220. _err "(This deploy hook must be run on the Unifi device, not a remote machine.)"
  221. _err "For non-standard Unifi installations, set DEPLOY_UNIFI_KEYSTORE,"
  222. _err "DEPLOY_UNIFI_CLOUDKEY_CERTDIR, and/or DEPLOY_UNIFI_CORE_CONFIG as appropriate."
  223. return 1
  224. fi
  225. _reload_cmd="${DEPLOY_UNIFI_RELOAD:-$_reload_cmd}"
  226. if [ -z "$_reload_cmd" ]; then
  227. _err "Certificates were installed for services:${_services_updated},"
  228. _err "but none appear to be active. Please set DEPLOY_UNIFI_RELOAD"
  229. _err "to a command that will restart the necessary services."
  230. return 1
  231. fi
  232. _info "Reload services (this may take some time): $_reload_cmd"
  233. if eval "$_reload_cmd"; then
  234. _info "Reload success!"
  235. else
  236. _err "Reload error"
  237. return 1
  238. fi
  239. # Successful, so save all (non-default) config:
  240. _savedeployconf DEPLOY_UNIFI_KEYSTORE "$DEPLOY_UNIFI_KEYSTORE"
  241. _savedeployconf DEPLOY_UNIFI_KEYPASS "$DEPLOY_UNIFI_KEYPASS"
  242. _savedeployconf DEPLOY_UNIFI_CLOUDKEY_CERTDIR "$DEPLOY_UNIFI_CLOUDKEY_CERTDIR"
  243. _savedeployconf DEPLOY_UNIFI_CORE_CONFIG "$DEPLOY_UNIFI_CORE_CONFIG"
  244. _savedeployconf DEPLOY_UNIFI_RELOAD "$DEPLOY_UNIFI_RELOAD"
  245. return 0
  246. }