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.

282 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="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. # correct file ownership according to the directory, the keystore is placed in
  123. _unifi_keystore_dir=$(dirname "${_unifi_keystore}")
  124. _unifi_keystore_dir_owner=$(ls -ld "${_unifi_keystore_dir}" | awk '{print $3}')
  125. _unifi_keystore_owner=$(ls -l "${_unifi_keystore}" | awk '{print $3}')
  126. if ! [ "${_unifi_keystore_owner}" = "${_unifi_keystore_dir_owner}" ] ; then
  127. _debug "Changing keystore owner to ${_unifi_keystore_dir_owner}"
  128. chown $_unifi_keystore_dir_owner "${_unifi_keystore}" >/dev/null 2>&1 # fail quietly if we're not running as root
  129. fi
  130. # Update unifi service for certificate cipher compatibility
  131. if ${ACME_OPENSSL_BIN:-openssl} pkcs12 \
  132. -in "$_import_pkcs12" \
  133. -password pass:aircontrolenterprise \
  134. -nokeys | ${ACME_OPENSSL_BIN:-openssl} x509 -text \
  135. -noout | grep -i "signature" | grep -iq ecdsa >/dev/null 2>&1; then
  136. if [ -f "$(dirname ${DEPLOY_UNIFI_KEYSTORE})/system.properties" ] ; then
  137. _unifi_system_properties="$(dirname ${DEPLOY_UNIFI_KEYSTORE})/system.properties"
  138. else
  139. _unifi_system_properties="/usr/lib/unifi/data/system.properties"
  140. fi
  141. if [ -f "${_unifi_system_properties}" ] ; then
  142. cp -f "${_unifi_system_properties}" "${_unifi_system_properties}"_original
  143. _info "Updating system configuration for cipher compatibility."
  144. _info "Saved original system config to ${_unifi_system_properties}_original"
  145. sed -i '/unifi\.https\.ciphers/d' "${_unifi_system_properties}"
  146. echo "unifi.https.ciphers=ECDHE-ECDSA-AES256-GCM-SHA384,ECDHE-RSA-AES128-GCM-SHA256" >>"${_unifi_system_properties}"
  147. sed -i '/unifi\.https\.sslEnabledProtocols/d' "${_unifi_system_properties}"
  148. echo "unifi.https.sslEnabledProtocols=TLSv1.3,TLSv1.2" >>"${_unifi_system_properties}"
  149. _info "System configuration updated."
  150. fi
  151. fi
  152. rm "$_import_pkcs12"
  153. # Restarting unifi-core will bring up unifi, doing it out of order results in
  154. # a certificate error, and breaks wifiman.
  155. # Restart if we aren't doing unifi-core, otherwise stop for later restart.
  156. if systemctl -q is-active unifi; then
  157. if [ ! -f "${DEPLOY_UNIFI_CORE_CONFIG:-/data/unifi-core/config}/unifi-core.key" ]; then
  158. _reload_cmd="${_reload_cmd:+$_reload_cmd && }systemctl restart unifi"
  159. else
  160. _reload_cmd="${_reload_cmd:+$_reload_cmd && }systemctl stop unifi"
  161. fi
  162. fi
  163. _services_updated="${_services_updated} unifi"
  164. _info "Install Unifi Controller certificate success!"
  165. elif [ "$DEPLOY_UNIFI_KEYSTORE" ]; then
  166. _err "The specified DEPLOY_UNIFI_KEYSTORE='$DEPLOY_UNIFI_KEYSTORE' is not valid, please check."
  167. return 1
  168. fi
  169. # Cloud Key environment (non-UnifiOS -- nginx serves admin pages) --
  170. # auto-detect by file /etc/ssl/private/cloudkey.key:
  171. _cloudkey_certdir="${DEPLOY_UNIFI_CLOUDKEY_CERTDIR:-/etc/ssl/private}"
  172. if [ -f "${_cloudkey_certdir}/cloudkey.key" ]; then
  173. _info "Installing certificate for Cloud Key Gen1 (nginx admin pages)"
  174. _debug _cloudkey_certdir "$_cloudkey_certdir"
  175. if [ ! -w "$_cloudkey_certdir" ]; then
  176. _err "The directory $_cloudkey_certdir is not writable; please check permissions."
  177. return 1
  178. fi
  179. # Cloud Key expects to load the keystore from /etc/ssl/private/unifi.keystore.jks.
  180. # Normally /usr/lib/unifi/data/keystore is a symlink there (so the keystore was
  181. # updated above), but if not, we don't know how to handle this installation:
  182. if ! cmp -s "$_unifi_keystore" "${_cloudkey_certdir}/unifi.keystore.jks"; then
  183. _err "Unsupported Cloud Key configuration: keystore not found at '${_cloudkey_certdir}/unifi.keystore.jks'"
  184. return 1
  185. fi
  186. cat "$_cfullchain" >"${_cloudkey_certdir}/cloudkey.crt"
  187. cat "$_ckey" >"${_cloudkey_certdir}/cloudkey.key"
  188. (cd "$_cloudkey_certdir" && tar -cf cert.tar cloudkey.crt cloudkey.key unifi.keystore.jks)
  189. if systemctl -q is-active nginx; then
  190. _reload_cmd="${_reload_cmd:+$_reload_cmd && }service nginx restart"
  191. fi
  192. _info "Install Cloud Key Gen1 certificate success!"
  193. _services_updated="${_services_updated} nginx"
  194. elif [ "$DEPLOY_UNIFI_CLOUDKEY_CERTDIR" ]; then
  195. _err "The specified DEPLOY_UNIFI_CLOUDKEY_CERTDIR='$DEPLOY_UNIFI_CLOUDKEY_CERTDIR' is not valid, please check."
  196. return 1
  197. fi
  198. # UnifiOS environment -- auto-detect by /data/unifi-core/config/unifi-core.key:
  199. _unifi_core_config="${DEPLOY_UNIFI_CORE_CONFIG:-/data/unifi-core/config}"
  200. if [ -f "${_unifi_core_config}/unifi-core.key" ]; then
  201. _info "Installing certificate for UnifiOS"
  202. _debug _unifi_core_config "$_unifi_core_config"
  203. if [ ! -w "$_unifi_core_config" ]; then
  204. _err "The directory $_unifi_core_config is not writable; please check permissions."
  205. return 1
  206. fi
  207. # Save the existing certs in case something goes wrong.
  208. cp -f "${_unifi_core_config}"/unifi-core.crt "${_unifi_core_config}"/unifi-core_original.crt
  209. cp -f "${_unifi_core_config}"/unifi-core.key "${_unifi_core_config}"/unifi-core_original.key
  210. _info "Previous certificate and key saved to ${_unifi_core_config}/unifi-core_original.crt/key."
  211. cat "$_cfullchain" >"${_unifi_core_config}/unifi-core.crt"
  212. cat "$_ckey" >"${_unifi_core_config}/unifi-core.key"
  213. if systemctl -q is-active unifi-core; then
  214. _reload_cmd="${_reload_cmd:+$_reload_cmd && }systemctl restart unifi-core"
  215. fi
  216. _info "Install UnifiOS certificate success!"
  217. _services_updated="${_services_updated} unifi-core"
  218. elif [ "$DEPLOY_UNIFI_CORE_CONFIG" ]; then
  219. _err "The specified DEPLOY_UNIFI_CORE_CONFIG='$DEPLOY_UNIFI_CORE_CONFIG' is not valid, please check."
  220. return 1
  221. fi
  222. if [ -z "$_services_updated" ]; then
  223. # None of the Unifi environments were auto-detected, so no deployment has occurred
  224. # (and none of DEPLOY_UNIFI_{KEYSTORE,CLOUDKEY_CERTDIR,CORE_CONFIG} were set).
  225. _err "Unable to detect Unifi environment in standard location."
  226. _err "(This deploy hook must be run on the Unifi device, not a remote machine.)"
  227. _err "For non-standard Unifi installations, set DEPLOY_UNIFI_KEYSTORE,"
  228. _err "DEPLOY_UNIFI_CLOUDKEY_CERTDIR, and/or DEPLOY_UNIFI_CORE_CONFIG as appropriate."
  229. return 1
  230. fi
  231. _reload_cmd="${DEPLOY_UNIFI_RELOAD:-$_reload_cmd}"
  232. if [ -z "$_reload_cmd" ]; then
  233. _err "Certificates were installed for services:${_services_updated},"
  234. _err "but none appear to be active. Please set DEPLOY_UNIFI_RELOAD"
  235. _err "to a command that will restart the necessary services."
  236. return 1
  237. fi
  238. _info "Reload services (this may take some time): $_reload_cmd"
  239. if eval "$_reload_cmd"; then
  240. _info "Reload success!"
  241. else
  242. _err "Reload error"
  243. return 1
  244. fi
  245. # Successful, so save all (non-default) config:
  246. _savedeployconf DEPLOY_UNIFI_KEYSTORE "$DEPLOY_UNIFI_KEYSTORE"
  247. _savedeployconf DEPLOY_UNIFI_KEYPASS "$DEPLOY_UNIFI_KEYPASS"
  248. _savedeployconf DEPLOY_UNIFI_CLOUDKEY_CERTDIR "$DEPLOY_UNIFI_CLOUDKEY_CERTDIR"
  249. _savedeployconf DEPLOY_UNIFI_CORE_CONFIG "$DEPLOY_UNIFI_CORE_CONFIG"
  250. _savedeployconf DEPLOY_UNIFI_RELOAD "$DEPLOY_UNIFI_RELOAD"
  251. return 0
  252. }