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.

239 lines
7.6 KiB

  1. #!/usr/bin/env sh
  2. # DirectAdmin 1.58.2 API
  3. # This script can be used to deploy certificates to DirectAdmin
  4. #
  5. # User must provide login data and URL (incl. port) to DirectAdmin.
  6. # You can create login key, by using the Login Keys function
  7. # ( https://da.example.com:8443/CMD_LOGIN_KEYS ), which only has access to
  8. # - CMD_API_SSL
  9. #
  10. # Report bugs to https://github.com/Eddict/acme.sh/issues
  11. #
  12. # Values to export:
  13. # export DA_Api="https://remoteUser:remotePassword@da.example.com:8443"
  14. # export DA_Api_Insecure=1
  15. #
  16. # Set DA_Api_Insecure to 1 for insecure and 0 for secure -> difference is
  17. # whether ssl cert is checked for validity (0) or whether it is just accepted (1)
  18. #
  19. # Thanks to https://github.com/TigerP, creator of dnsapi/dns_da.sh
  20. # That script helped a lot to create this one
  21. ######## Public functions #####################
  22. directadmin_deploy() {
  23. _cdomain="$1"
  24. _ckey="$2"
  25. _ccert="$3"
  26. _cca="$4"
  27. _cfullchain="$5"
  28. _debug _cdomain "$_cdomain"
  29. _debug _ckey "$_ckey"
  30. _debug _ccert "$_ccert"
  31. _debug _cca "$_cca"
  32. _debug _cfullchain "$_cfullchain"
  33. _DA_credentials && _DA_setSSL
  34. return 0
  35. }
  36. #################### Private functions below ##################################
  37. # Usage: _DA_credentials
  38. # It will check if the needed settings are available
  39. _DA_credentials() {
  40. DA_Api="${DA_Api:-$(_readaccountconf_mutable DA_Api)}"
  41. DA_Api_Insecure="${DA_Api_Insecure:-$(_readaccountconf_mutable DA_Api_Insecure)}"
  42. if [ -z "${DA_Api}" ] || [ -z "${DA_Api_Insecure}" ]; then
  43. DA_Api=""
  44. DA_Api_Insecure=""
  45. _err "You haven't specified the DirectAdmin Login data, URL and whether you want check the DirectAdmin SSL cert. Please try again."
  46. return 1
  47. else
  48. _saveaccountconf_mutable DA_Api "${DA_Api}"
  49. _saveaccountconf_mutable DA_Api_Insecure "${DA_Api_Insecure}"
  50. # Set whether curl should use secure or insecure mode
  51. export HTTPS_INSECURE="${DA_Api_Insecure}"
  52. fi
  53. }
  54. # Usage: _da_get_api CMD_API_* data example.com
  55. # Use the DirectAdmin API and check the result
  56. # returns
  57. # response="error=0&text=Result text&details="
  58. _da_get_api() {
  59. cmd=$1
  60. data=$2
  61. domain=$3
  62. _debug "$domain; $data"
  63. if ! response=$(_get "$DA_Api/$cmd?$data"); then
  64. _err "error $cmd"
  65. return 1
  66. fi
  67. _secure_debug2 response "$response"
  68. return 0
  69. }
  70. # Usage: _DA_setSSL
  71. # Use the API to set the certificates
  72. _DA_setSSL() {
  73. curData="domain=${_cdomain}&json=yes"
  74. _debug "Calling _da_get_api: '${curData}' '${DA_Api}/CMD_API_SSL'"
  75. _da_get_api CMD_API_SSL "${curData}" "${domain}"
  76. _secure_debug2 "response" "$response"
  77. cert_response=$response
  78. name="ssl_on"
  79. if ! _contains "$cert_response" "$name"; then
  80. _err "'${name}' was not found in response."
  81. return 1
  82. fi
  83. ssl_on="$(echo "$cert_response" | jq -r .$name)"
  84. _debug2 "$name" "$ssl_on"
  85. if [ "$ssl_on" = "yes" ]; then
  86. _debug "Domain '${_cdomain}' has SSL enabled: $(__green "$ssl_on")"
  87. else
  88. _err "Domain '${_cdomain}' does not has SSL enabled: $ssl_on"
  89. if [ -z "$FORCE" ]; then
  90. _info "Add '$(__red '--force')' to force to deploy."
  91. return 1
  92. fi
  93. fi
  94. name="server"
  95. if ! _contains "$cert_response" "$name"; then
  96. _err "'${name}' was not found in response."
  97. return 1
  98. fi
  99. server="$(echo "$cert_response" | jq -r .$name)"
  100. _debug "$name" "$server"
  101. if [ "$server" = "no" ]; then
  102. _debug "Domain '${_cdomain}' is using a custom/pasted certificate."
  103. else
  104. _err "Domain '${_cdomain}' is using the server certificate."
  105. if [ -z "$FORCE" ]; then
  106. _info "Add '$(__red '--force')' to force to deploy."
  107. return 1
  108. fi
  109. fi
  110. curData="domain=${_cdomain}&view=cacert&json=yes"
  111. _debug "Calling _DA_da_get_api_getSSL: '${curData}' '${DA_Api}/CMD_API_SSL'"
  112. _da_get_api CMD_API_SSL "${curData}" "${_cdomain}"
  113. _secure_debug2 "response" "$response"
  114. cacert_response=$response
  115. name="enabled"
  116. if ! _contains "$cacert_response" "$name"; then
  117. _err "'${name}' was not found in response."
  118. return 1
  119. fi
  120. enabled="$(echo "$cacert_response" | jq -r .$name)"
  121. _debug "$name" "$enabled"
  122. cca=$(cat -v "$_cca")
  123. cca_flat="$(echo "$cca" | tr -d '\r' | tr -d '\n')"
  124. ckey=$(cat -v "$_ckey")
  125. ckey_flat="$(echo "$ckey" | tr -d '\r' | tr -d '\n')"
  126. ccert=$(cat -v "$_ccert")
  127. ccert_flat="$(echo "$ccert" | tr -d '\r' | tr -d '\n')"
  128. name="cacert"
  129. sameCaCert=1
  130. if [ "$enabled" = "yes" ]; then
  131. _debug "Domain '${_cdomain}' is using a CA certificate."
  132. cacert="$(echo "$cacert_response" | jq -r .$name)"
  133. cacert_flat="$(echo "$cacert" | tr -d '\r' | tr -d '\n')"
  134. _debug2 "$name" "$cacert"
  135. if [ "$cacert_flat" != "$cca_flat" ]; then
  136. sameCaCert=0
  137. _info "Domain '${_cdomain}' is using $(__red 'a different') CA certificate."
  138. else
  139. _info "Domain '${_cdomain}' is using the same CA certificate."
  140. fi
  141. else
  142. _err "Domain '${_cdomain}' is currently not using a CA certificate."
  143. if [ -z "$FORCE" ]; then
  144. _info "Add '$(__red '--force')' to force to deploy."
  145. return 1
  146. fi
  147. fi
  148. name="key"
  149. sameKey=1
  150. if _contains "$cert_response" "$name"; then
  151. key="$(echo "$cert_response" | jq -r .$name)"
  152. key_flat="$(echo "$key" | tr -d '\r' | tr -d '\n')"
  153. _secure_debug2 "$name" "$key"
  154. if [ "$key_flat" != "$ckey_flat" ]; then
  155. sameKey=0
  156. _info "Domain '${_cdomain}' is using $(__red 'a different') private key."
  157. else
  158. _info "Domain '${_cdomain}' is using the same private key."
  159. fi
  160. fi
  161. name="certificate"
  162. sameCert=1
  163. if _contains "$cert_response" "$name"; then
  164. cert="$(echo "$cert_response" | jq -r .$name)"
  165. cert_flat="$(echo "$cert" | tr -d '\r' | tr -d '\n')"
  166. _debug2 "$name" "$cert"
  167. if [ "$cert_flat" != "$ccert_flat" ]; then
  168. sameCert=0
  169. _info "Domain '${_cdomain}' is using $(__red 'a different') certificate."
  170. else
  171. _info "Domain '${_cdomain}' is using the same certificate."
  172. fi
  173. fi
  174. if [ -n "$FORCE" ] || [ $sameCaCert -eq 0 ] || [ $sameKey -eq 0 ] || [ $sameCert -eq 0 ]; then
  175. if [ -n "$FORCE" ] || [ $sameCaCert -eq 0 ]; then
  176. export _H1="Content-Type: application/x-www-form-urlencoded"
  177. encoded_cacert_value="$(printf "%s" "${cca}" | _url_encode)"
  178. _debug2 encoded_cacert_value "$encoded_cacert_value"
  179. curData="domain=${_cdomain}&action=save&type=cacert&active=yes&cacert=${encoded_cacert_value}"
  180. response="$(_post "$curData" "${DA_Api}/CMD_API_SSL")"
  181. if _contains "${response}" 'error=0'; then
  182. _info "$(__green "Setting the cacert succeeded for domain '${_cdomain}'.")"
  183. else
  184. _err "Setting the cacert failed for domain '${_cdomain}'. Check response:"
  185. _err "$response"
  186. return 1
  187. fi
  188. fi
  189. if [ -n "$FORCE" ] || [ $sameKey -eq 0 ] || [ $sameCert -eq 0 ]; then
  190. export _H1="Content-Type: application/x-www-form-urlencoded"
  191. encoded_keycert_value="$(printf "%s" "${ckey}$'\n'${ccert}" | _url_encode)"
  192. _debug2 encoded_cert_value "$encoded_keycert_value"
  193. curData="domain=${_cdomain}&action=save&type=paste&request=no&certificate=${encoded_keycert_value}"
  194. response="$(_post "$curData" "${DA_Api}/CMD_API_SSL")"
  195. if _contains "${response}" 'error=0'; then
  196. _info "$(__green "Setting the key and cert succeeded for domain '${_cdomain}'.")"
  197. else
  198. _err "Setting the key and cert failed for domain '${_cdomain}'. Check response:"
  199. _err "$response"
  200. return 1
  201. fi
  202. fi
  203. else
  204. if [ $sameCaCert -eq 1 ] && [ $sameKey -eq 1 ] && [ $sameCert -eq 1 ]; then
  205. _info "Nothing to do. Domain '${_cdomain}' $(__green 'has already the same certifcates active.')"
  206. if [ -z "$FORCE" ]; then
  207. _info "Add '$(__red '--force')' to force to deploy."
  208. fi
  209. fi
  210. fi
  211. return 0
  212. }