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.

139 lines
4.9 KiB

  1. #!/usr/bin/env sh
  2. # Deploy script to install keys to the openHAB keystore
  3. # This script attempts to restart the openHAB service upon completion.
  4. # In order for this to work, the user running acme.sh needs to be able
  5. # to execute the DEPLOY_OPENHABIAN_RESTART command
  6. # (default: sudo service openhab restart) without needing a password prompt.
  7. # To ensure this deployment runs properly ensure permissions are configured
  8. # correctly, or change the command variable as needed.
  9. # Configuration options:
  10. # DEPLOY_OPENHABIAN_KEYPASS : The default should be appropriate here for most cases,
  11. # but change this to change the password used for the keystore.
  12. # DEPLOY_OPENHABIAN_KEYSTORE : The full path of the openHAB keystore file. This will
  13. # default to a path based on the $OPENHAB_USERDATA directory.
  14. # This should generate based on existing openHAB env vars.
  15. # DEPLOY_OPENHABIAN_RESTART : The command used to restart openHAB
  16. openhabian_deploy() {
  17. # Name parameters, load configs
  18. _cdomain="$1"
  19. _ckey="$2"
  20. _ccert="$3"
  21. _cca="$4"
  22. _cfullchain="$5"
  23. _debug _cdomain "$_cdomain"
  24. _debug _ckey "$_ckey"
  25. _debug _ccert "$_ccert"
  26. _debug _cca "$_cca"
  27. _debug _cfullchain "$_cfullchain"
  28. _getdeployconf DEPLOY_OPENHABIAN_KEYSTORE
  29. _getdeployconf DEPLOY_OPENHABIAN_KEYPASS
  30. _getdeployconf DEPLOY_OPENHABIAN_RESTART
  31. _debug2 DEPLOY_OPENHABIAN_KEYSTORE "$DEPLOY_OPENHABIAN_KEYSTORE"
  32. _debug2 DEPLOY_OPENHABIAN_KEYPASS "$DEPLOY_OPENHABIAN_KEYPASS"
  33. _debug2 DEPLOY_OPENHABIAN_RESTART "$DEPLOY_OPENHABIAN_RESTART"
  34. # Define configurable options
  35. _openhab_keystore="${DEPLOY_OPENHABIAN_KEYSTORE:-${OPENHAB_USERDATA}/etc/keystore}"
  36. _openhab_keypass="${DEPLOY_OPENHABIAN_KEYPASS:-openhab}"
  37. _default_restart="sudo service openhab restart"
  38. _openhab_restart="${DEPLOY_OPENHABIAN_RESTART:-$_default_restart}"
  39. _debug _openhab_keystore "$_openhab_keystore"
  40. _debug _openhab_keypass "$_openhab_keypass"
  41. _debug _openhab_restart "$_openhab_restart"
  42. # Verify Dependencies
  43. if ! _exists keytool; then
  44. _err "keytool not found, please install keytool"
  45. return 1
  46. fi
  47. if [ ! -w "$_openhab_keystore" ]; then
  48. _err "The file $_openhab_keystore is not writable, please change the permission."
  49. return 1
  50. fi
  51. # Take a backup of the old keystore
  52. _debug "Storing a backup of the existing keystore at ${_openhab_keystore}.bak"
  53. cp "${_openhab_keystore}" "${_openhab_keystore}.bak"
  54. # Generate PKCS12 keystore
  55. _new_pkcs12="$(_mktemp)"
  56. # _toPkcs doesn't support -nodes param
  57. if ${ACME_OPENSSL_BIN:-openssl} pkcs12 \
  58. -export \
  59. -inkey "$_ckey" \
  60. -in "$_ccert" \
  61. -certfile "$_cca" \
  62. -name mykey \
  63. -out "$_new_pkcs12" \
  64. -nodes -passout "pass:$_openhab_keypass"; then
  65. _debug "Successfully created pkcs keystore"
  66. else
  67. _err "Error generating pkcs12."
  68. _err "Please re-run with --debug and report a bug."
  69. rm "$_new_pkcs12"
  70. return 1
  71. fi
  72. # Remove old cert from existing store
  73. if keytool -delete \
  74. -alias mykey \
  75. -deststorepass "$_openhab_keypass" \
  76. -keystore "$_openhab_keystore"; then
  77. _info "Successfully deleted old key"
  78. else
  79. _err "Error deleting old key"
  80. _err "Please re-run with --debug and report a bug."
  81. rm "$_new_pkcs12"
  82. return 1
  83. fi
  84. # Add new certificate to store
  85. if keytool -importkeystore \
  86. -srckeystore "$_new_pkcs12" \
  87. -srcstoretype PKCS12 \
  88. -srcstorepass "$_openhab_keypass" \
  89. -alias mykey \
  90. -destkeystore "$_openhab_keystore" \
  91. -deststoretype jks \
  92. -deststorepass "$_openhab_keypass" \
  93. -destalias mykey; then
  94. _info "Successfully imported new key"
  95. else
  96. _err "Failure when importing key"
  97. _err "Please re-run with --debug and report a bug."
  98. rm "$_new_pkcs12"
  99. return 1
  100. fi
  101. # Reload openHAB service
  102. if eval "$_openhab_restart"; then
  103. _info "Restarted openhab"
  104. else
  105. _err "Failed to restart openHAB, please restart openHAB manually."
  106. _err "The new key has been installed, but openHAB may not use it until restarted"
  107. _err "To prevent this error, override the restart command with DEPLOY_OPENHABIAN_RESTART \
  108. and ensure it can be called by the acme.sh user"
  109. return 1
  110. fi
  111. _savedeployconf DEPLOY_OPENHABIAN_KEYSTORE "$DEPLOY_OPENHABIAN_KEYSTORE"
  112. _savedeployconf DEPLOY_OPENHABIAN_KEYPASS "$DEPLOY_OPENHABIAN_KEYPASS"
  113. _savedeployconf DEPLOY_OPENHABIAN_RESTART "$DEPLOY_OPENHABIAN_RESTART"
  114. rm "$_new_pkcs12"
  115. }
  116. # Credits:
  117. # This solution was heavily informed by a few existing scripts:
  118. # - https://gist.github.com/jpmens/8029383
  119. # - https://github.com/matsahm/openhab_change_ssl/blob/bd46986581631319606ae4c594d4ed774a67cd39/openhab_change_ssl
  120. # Thank you!