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.

64 lines
1.8 KiB

8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 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@2globalnomads.info>
  6. # Public domain, 2017
  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. if ! _exists php; then
  26. _err "The command php is not found."
  27. return 1
  28. fi
  29. # read cert and key files and urlencode both
  30. _certstr=$(cat "$_ccert")
  31. _keystr=$(cat "$_ckey")
  32. _cert=$(php -r "echo urlencode(\"$_certstr\");")
  33. _key=$(php -r "echo urlencode(\"$_keystr\");")
  34. _debug _cert "$_cert"
  35. _debug _key "$_key"
  36. if [ "$(id -u)" = 0 ]; then
  37. if [ -z "$DEPLOY_CPANEL_USER" ]; then
  38. _err "It seems that you are root, please define the target user name: export DEPLOY_CPANEL_USER=username"
  39. return 1
  40. fi
  41. _savedomainconf DEPLOY_CPANEL_USER "$DEPLOY_CPANEL_USER"
  42. _response=$(uapi --user="$DEPLOY_CPANEL_USER" SSL install_ssl domain="$_cdomain" cert="$_cert" key="$_key")
  43. else
  44. _response=$(uapi SSL install_ssl domain="$_cdomain" cert="$_cert" key="$_key")
  45. fi
  46. error_response="status: 0"
  47. if test "${_response#*$error_response}" != "$_response"; then
  48. _err "Error in deploying certificate:"
  49. _err "$_response"
  50. return 1
  51. fi
  52. _debug response "$_response"
  53. _info "Certificate successfully deployed"
  54. return 0
  55. }