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.

80 lines
2.7 KiB

  1. #!/usr/bin/env sh
  2. # Script to deploy certificate to DirectAdmin
  3. # https://docs.directadmin.com/directadmin/customizing-workflow/api-all-about.html#creating-a-login-key
  4. # https://docs.directadmin.com/changelog/version-1.24.4.html#cmd-api-catch-all-pop-passwords-frontpage-protected-dirs-ssl-certs
  5. # This deployment required following variables
  6. # export DirectAdmin_ENDPOINT="example.com:2222"
  7. # export DirectAdmin_USERNAME="Your DirectAdmin Username"
  8. # export DirectAdmin_KEY="Your DirectAdmin Login Key or Password"
  9. # export DirectAdmin_MAIN_DOMAIN="Your DirectAdmin Main Domain, NOT Subdomain"
  10. # returns 0 means success, otherwise error.
  11. ######## Public functions #####################
  12. #domain keyfile certfile cafile fullchain
  13. directadmin_deploy() {
  14. _cdomain="$1"
  15. _ckey="$2"
  16. _ccert="$3"
  17. _cca="$4"
  18. _cfullchain="$5"
  19. _debug _cdomain "$_cdomain"
  20. _debug _ckey "$_ckey"
  21. _debug _ccert "$_ccert"
  22. _debug _cca "$_cca"
  23. _debug _cfullchain "$_cfullchain"
  24. if [ -z "$DirectAdmin_ENDPOINT" ]; then
  25. _err "DirectAdmin_ENDPOINT is not defined."
  26. return 1
  27. else
  28. _savedomainconf DirectAdmin_ENDPOINT "$DirectAdmin_ENDPOINT"
  29. fi
  30. if [ -z "$DirectAdmin_USERNAME" ]; then
  31. _err "DirectAdmin_USERNAME is not defined."
  32. return 1
  33. else
  34. _savedomainconf DirectAdmin_USERNAME "$DirectAdmin_USERNAME"
  35. fi
  36. if [ -z "$DirectAdmin_KEY" ]; then
  37. _err "DirectAdmin_KEY is not defined."
  38. return 1
  39. else
  40. _savedomainconf DirectAdmin_KEY "$DirectAdmin_KEY"
  41. fi
  42. if [ -z "$DirectAdmin_MAIN_DOMAIN" ]; then
  43. _err "DirectAdmin_MAIN_DOMAIN is not defined."
  44. return 1
  45. else
  46. _savedomainconf DirectAdmin_MAIN_DOMAIN "$DirectAdmin_MAIN_DOMAIN"
  47. fi
  48. _info "Deploying certificate to DirectAdmin..."
  49. # upload certificate
  50. string_cfullchain=$(sed 's/$/\\n/' "$_cfullchain" | tr -d '\n')
  51. string_key=$(sed 's/$/\\n/' "$_ckey" | tr -d '\n')
  52. _request_body="{\"domain\":\"$DirectAdmin_MAIN_DOMAIN\",\"action\":\"save\",\"type\":\"paste\",\"certificate\":\"$string_key\n$string_cfullchain\n\"}"
  53. _debug _request_body "$_request_body"
  54. _debug DirectAdmin_ENDPOINT "$DirectAdmin_ENDPOINT"
  55. _debug DirectAdmin_USERNAME "$DirectAdmin_USERNAME"
  56. _debug DirectAdmin_KEY "$DirectAdmin_KEY"
  57. _debug DirectAdmin_MAIN_DOMAIN "$DirectAdmin_MAIN_DOMAIN"
  58. _response=$(_post "$_request_body" "https://$DirectAdmin_USERNAME:$DirectAdmin_KEY@$DirectAdmin_ENDPOINT/CMD_API_SSL" "" "POST" "application/json")
  59. if _contains "$_response" "error=1"; then
  60. _err "Error in deploying $_cdomain certificate to DirectAdmin Domain $DirectAdmin_MAIN_DOMAIN."
  61. _err "$_response"
  62. return 1
  63. fi
  64. _info "$_response"
  65. _info "Domain $_cdomain certificate successfully deployed to DirectAdmin Domain $DirectAdmin_MAIN_DOMAIN."
  66. return 0
  67. }