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.

78 lines
2.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. # Here is the script to deploy the cert to your cpanel using the cpanel API.
  3. # Uses command line uapi. --user option is needed only if run as root.
  4. # Returns 0 when success.
  5. # Written by Santeri Kannisto <santeri.kannisto@webseodesigners.com>
  6. # Public domain, 2017
  7. #export DEPLOY_CPANEL_USER=myusername
  8. ######## Private functions #####################
  9. __urlencode() {
  10. __length="${#1}"
  11. for ((_offset = 0; _offset < __length; _offset++)); do
  12. _print_offset="${1:_offset:1}"
  13. case "${_print_offset}" in
  14. [a-zA-Z0-9.~_-]) printf "${_print_offset}" ;;
  15. ' ') printf + ;;
  16. *) printf '%%%X' "'${_print_offset}" ;;
  17. esac
  18. done
  19. }
  20. ######## Public functions #####################
  21. #domain keyfile certfile cafile fullchain
  22. cpanel_uapi_deploy() {
  23. _cdomain="$1"
  24. _ckey="$2"
  25. _ccert="$3"
  26. _cca="$4"
  27. _cfullchain="$5"
  28. _debug _cdomain "$_cdomain"
  29. _debug _ckey "$_ckey"
  30. _debug _ccert "$_ccert"
  31. _debug _cca "$_cca"
  32. _debug _cfullchain "$_cfullchain"
  33. if ! _exists uapi; then
  34. _err "The command uapi is not found."
  35. return 1
  36. fi
  37. if ! _exists php; then
  38. _err "The command php is not found."
  39. return 1
  40. fi
  41. # read cert and key files and urlencode both
  42. _certstr=$(cat "$_ccert")
  43. _keystr=$(cat "$_ckey")
  44. _cert=$(__urlencode "$_certstr")
  45. _key=$(__urlencode "$_keystr")
  46. _debug _cert "$_cert"
  47. _debug _key "$_key"
  48. if [ "$(id -u)" = 0 ]; then
  49. if [ -z "$DEPLOY_CPANEL_USER" ]; then
  50. _err "It seems that you are root, please define the target user name: export DEPLOY_CPANEL_USER=username"
  51. return 1
  52. fi
  53. _savedomainconf DEPLOY_CPANEL_USER "$DEPLOY_CPANEL_USER"
  54. _response=$(uapi --user="$DEPLOY_CPANEL_USER" SSL install_ssl domain="$_cdomain" cert="$_cert" key="$_key")
  55. else
  56. _response=$(uapi SSL install_ssl domain="$_cdomain" cert="$_cert" key="$_key")
  57. fi
  58. error_response="status: 0"
  59. if test "${_response#*$error_response}" != "$_response"; then
  60. _err "Error in deploying certificate:"
  61. _err "$_response"
  62. return 1
  63. fi
  64. _debug response "$_response"
  65. _info "Certificate successfully deployed"
  66. return 0
  67. }