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.

76 lines
2.7 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. #
  6. # Please note that I am no longer using Github. If you want to report an issue
  7. # or contact me, visit https://forum.webseodesigners.com/web-design-seo-and-hosting-f16/
  8. #
  9. # I am maintaining the urlencode function at GitLab: https://gitlab.com/santerikannisto/urlencode
  10. #
  11. # Written by Santeri Kannisto <santeri.kannisto@webseodesigners.com>
  12. # Public domain, 2017-2018
  13. #export DEPLOY_CPANEL_USER=myusername
  14. ######## Public functions #####################
  15. #domain keyfile certfile cafile fullchain
  16. cpanel_uapi_deploy() {
  17. _cdomain="$1"
  18. _ckey="$2"
  19. _ccert="$3"
  20. _cca="$4"
  21. _cfullchain="$5"
  22. _debug _cdomain "$_cdomain"
  23. _debug _ckey "$_ckey"
  24. _debug _ccert "$_ccert"
  25. _debug _cca "$_cca"
  26. _debug _cfullchain "$_cfullchain"
  27. if ! _exists uapi; then
  28. _err "The command uapi is not found."
  29. return 1
  30. fi
  31. # read cert and key files and urlencode both
  32. _certstr=$(cat "$_ccert")
  33. _keystr=$(cat "$_ckey")
  34. _cert=$(_cpanel_uapi_urlencode "$_certstr")
  35. _key=$(_cpanel_uapi_urlencode "$_keystr")
  36. _debug _cert "$_cert"
  37. _debug _key "$_key"
  38. if [ "$(id -u)" = 0 ]; then
  39. if [ -z "$DEPLOY_CPANEL_USER" ]; then
  40. _err "It seems that you are root, please define the target user name: export DEPLOY_CPANEL_USER=username"
  41. return 1
  42. fi
  43. _savedomainconf DEPLOY_CPANEL_USER "$DEPLOY_CPANEL_USER"
  44. _response=$(uapi --user="$DEPLOY_CPANEL_USER" SSL install_ssl domain="$_cdomain" cert="$_cert" key="$_key")
  45. else
  46. _response=$(uapi SSL install_ssl domain="$_cdomain" cert="$_cert" key="$_key")
  47. fi
  48. error_response="status: 0"
  49. if test "${_response#*$error_response}" != "$_response"; then
  50. _err "Error in deploying certificate:"
  51. _err "$_response"
  52. return 1
  53. fi
  54. _debug response "$_response"
  55. _info "Certificate successfully deployed"
  56. return 0
  57. }
  58. ######## Private functions below #####################
  59. _cpanel_uapi_urlencode() {
  60. printf "%s" "$1" |
  61. # convert newlines to audible bell so that that sed can handle the input without using non-POSIX extensions
  62. tr "\\r\\n" "\\a" |
  63. # urlencode characters
  64. sed -e 's/%/%25/g' -e 's/ /%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/+/%2B/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' -e 's/\a/%0A/g' --posix
  65. }