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.

66 lines
2.3 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
  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-2018
  7. #export DEPLOY_CPANEL_USER=myusername
  8. ######## Public functions #####################
  9. #domain keyfile certfile cafile fullchain
  10. cpanel_uapi_deploy() {
  11. _cdomain="$1"
  12. _ckey="$2"
  13. _ccert="$3"
  14. _cca="$4"
  15. _cfullchain="$5"
  16. _debug _cdomain "$_cdomain"
  17. _debug _ckey "$_ckey"
  18. _debug _ccert "$_ccert"
  19. _debug _cca "$_cca"
  20. _debug _cfullchain "$_cfullchain"
  21. if ! _exists uapi; then
  22. _err "The command uapi is not found."
  23. return 1
  24. fi
  25. # read cert and key files and urlencode both
  26. _certstr=$(cat "$_ccert")
  27. _keystr=$(cat "$_ckey")
  28. _cert=$(_cpanel_uapi_urlencode "$_certstr")
  29. _key=$(_cpanel_uapi_urlencode "$_keystr")
  30. _debug _cert "$_cert"
  31. _debug _key "$_key"
  32. if [ "$(id -u)" = 0 ]; then
  33. if [ -z "$DEPLOY_CPANEL_USER" ]; then
  34. _err "It seems that you are root, please define the target user name: export DEPLOY_CPANEL_USER=username"
  35. return 1
  36. fi
  37. _savedomainconf DEPLOY_CPANEL_USER "$DEPLOY_CPANEL_USER"
  38. _response=$(uapi --user="$DEPLOY_CPANEL_USER" SSL install_ssl domain="$_cdomain" cert="$_cert" key="$_key")
  39. else
  40. _response=$(uapi SSL install_ssl domain="$_cdomain" cert="$_cert" key="$_key")
  41. fi
  42. error_response="status: 0"
  43. if test "${_response#*$error_response}" != "$_response"; then
  44. _err "Error in deploying certificate:"
  45. _err "$_response"
  46. return 1
  47. fi
  48. _debug response "$_response"
  49. _info "Certificate successfully deployed"
  50. return 0
  51. }
  52. ######## Private functions below #####################
  53. _cpanel_uapi_urlencode() {
  54. printf "%s" "$1" | sed --posix -e 's/%/%25/g' -e ':a;N;$!ba;s/\n/%0a/g' -e 's/+/%2b/g' -e 's/[[:space:]]/%20/g' -e 's/\!/%21/g' -e 's/"/%22/g' -e 's/#/%23/g' -e 's/\$/%24/g' -e 's/&/%26/g' -e 's/'\''/%27/g' -e 's/(/%28/g' -e 's/)/%29/g' -e 's/\*/%2a/g' -e 's/,/%2c/g' -e 's/\./%2e/g' -e 's/\//%2f/g' -e 's/:/%3a/g' -e 's/;/%3b/g' -e 's/</%3c/g' -e 's/=/%3d/g' -e 's/>/%3e/g' -e 's/?/%3f/g' -e 's/@/%40/g' -e 's/\[/%5b/g' -e 's/\\/%5c/g' -e 's/\]/%5d/g' -e 's/\^/%5e/g' -e 's/_/%5f/g' -e 's/`/%60/g' -e 's/{/%7b/g' -e 's/|/%7c/g' -e 's/}/%7d/g' -e 's/~/%7e/g'
  55. }