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.

305 lines
13 KiB

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