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.

83 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. local string="${1}"
  11. local strlen=${#string}
  12. local encoded=""
  13. local pos c o
  14. for (( pos=0 ; pos<strlen ; pos++ )); do
  15. c=${string:$pos:1}
  16. case "$c" in
  17. [-_.~a-zA-Z0-9] ) o="${c}" ;;
  18. *) printf -v o '%%%02x' "'$c"
  19. esac
  20. encoded+="${o}"
  21. done
  22. echo "${encoded}"
  23. }
  24. ######## Public functions #####################
  25. #domain keyfile certfile cafile fullchain
  26. cpanel_uapi_deploy() {
  27. _cdomain="$1"
  28. _ckey="$2"
  29. _ccert="$3"
  30. _cca="$4"
  31. _cfullchain="$5"
  32. _debug _cdomain "$_cdomain"
  33. _debug _ckey "$_ckey"
  34. _debug _ccert "$_ccert"
  35. _debug _cca "$_cca"
  36. _debug _cfullchain "$_cfullchain"
  37. if ! _exists uapi; then
  38. _err "The command uapi is not found."
  39. return 1
  40. fi
  41. if ! _exists php; then
  42. _err "The command php is not found."
  43. return 1
  44. fi
  45. # read cert and key files and urlencode both
  46. _certstr=$(cat "$_ccert")
  47. _keystr=$(cat "$_ckey")
  48. _cert=$(__urlencode "$_certstr")
  49. _key=$(__urlencode "$_keystr")
  50. _debug _cert "$_cert"
  51. _debug _key "$_key"
  52. if [ "$(id -u)" = 0 ]; then
  53. if [ -z "$DEPLOY_CPANEL_USER" ]; then
  54. _err "It seems that you are root, please define the target user name: export DEPLOY_CPANEL_USER=username"
  55. return 1
  56. fi
  57. _savedomainconf DEPLOY_CPANEL_USER "$DEPLOY_CPANEL_USER"
  58. _response=$(uapi --user="$DEPLOY_CPANEL_USER" SSL install_ssl domain="$_cdomain" cert="$_cert" key="$_key")
  59. else
  60. _response=$(uapi SSL install_ssl domain="$_cdomain" cert="$_cert" key="$_key")
  61. fi
  62. error_response="status: 0"
  63. if test "${_response#*$error_response}" != "$_response"; then
  64. _err "Error in deploying certificate:"
  65. _err "$_response"
  66. return 1
  67. fi
  68. _debug response "$_response"
  69. _info "Certificate successfully deployed"
  70. return 0
  71. }