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.

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