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.

86 lines
3.0 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_SCHEME="https" # Optional, https or http, defaults to https
  7. # export DirectAdmin_ENDPOINT="example.com:2222"
  8. # export DirectAdmin_USERNAME="Your DirectAdmin Username"
  9. # export DirectAdmin_KEY="Your DirectAdmin Login Key or Password"
  10. # export DirectAdmin_MAIN_DOMAIN="Your DirectAdmin Main Domain, NOT Subdomain"
  11. # returns 0 means success, otherwise error.
  12. ######## Public functions #####################
  13. #domain keyfile certfile cafile fullchain
  14. directadmin_deploy() {
  15. _cdomain="$1"
  16. _ckey="$2"
  17. _ccert="$3"
  18. _cca="$4"
  19. _cfullchain="$5"
  20. _debug _cdomain "$_cdomain"
  21. _debug _ckey "$_ckey"
  22. _debug _ccert "$_ccert"
  23. _debug _cca "$_cca"
  24. _debug _cfullchain "$_cfullchain"
  25. if [ -z "$DirectAdmin_ENDPOINT" ]; then
  26. _err "DirectAdmin_ENDPOINT is not defined."
  27. return 1
  28. else
  29. _savedomainconf DirectAdmin_ENDPOINT "$DirectAdmin_ENDPOINT"
  30. fi
  31. if [ -z "$DirectAdmin_USERNAME" ]; then
  32. _err "DirectAdmin_USERNAME is not defined."
  33. return 1
  34. else
  35. _savedomainconf DirectAdmin_USERNAME "$DirectAdmin_USERNAME"
  36. fi
  37. if [ -z "$DirectAdmin_KEY" ]; then
  38. _err "DirectAdmin_KEY is not defined."
  39. return 1
  40. else
  41. _savedomainconf DirectAdmin_KEY "$DirectAdmin_KEY"
  42. fi
  43. if [ -z "$DirectAdmin_MAIN_DOMAIN" ]; then
  44. _err "DirectAdmin_MAIN_DOMAIN is not defined."
  45. return 1
  46. else
  47. _savedomainconf DirectAdmin_MAIN_DOMAIN "$DirectAdmin_MAIN_DOMAIN"
  48. fi
  49. # Optional SCHEME
  50. _getdeployconf DirectAdmin_SCHEME
  51. # set default values for DirectAdmin_SCHEME
  52. [ -n "${DirectAdmin_SCHEME}" ] || DirectAdmin_SCHEME="https"
  53. _info "Deploying certificate to DirectAdmin..."
  54. # upload certificate
  55. string_cfullchain=$(sed 's/$/\\n/' "$_cfullchain" | tr -d '\n')
  56. string_key=$(sed 's/$/\\n/' "$_ckey" | tr -d '\n')
  57. _request_body="{\"domain\":\"$DirectAdmin_MAIN_DOMAIN\",\"action\":\"save\",\"type\":\"paste\",\"certificate\":\"$string_key\n$string_cfullchain\n\"}"
  58. _debug _request_body "$_request_body"
  59. _debug DirectAdmin_ENDPOINT "$DirectAdmin_ENDPOINT"
  60. _debug DirectAdmin_USERNAME "$DirectAdmin_USERNAME"
  61. _debug DirectAdmin_KEY "$DirectAdmin_KEY"
  62. _debug DirectAdmin_MAIN_DOMAIN "$DirectAdmin_MAIN_DOMAIN"
  63. _response=$(_post "$_request_body" "$DirectAdmin_SCHEME://$DirectAdmin_USERNAME:$DirectAdmin_KEY@$DirectAdmin_ENDPOINT/CMD_API_SSL" "" "POST" "application/json")
  64. if _contains "$_response" "error=1"; then
  65. _err "Error in deploying $_cdomain certificate to DirectAdmin Domain $DirectAdmin_MAIN_DOMAIN."
  66. _err "$_response"
  67. return 1
  68. fi
  69. _info "$_response"
  70. _info "Domain $_cdomain certificate successfully deployed to DirectAdmin Domain $DirectAdmin_MAIN_DOMAIN."
  71. return 0
  72. }