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.

167 lines
4.9 KiB

4 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_da_info='DirectAdmin Server API
  4. Site: DirectAdmin.com/api.php
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_da
  6. Options:
  7. DA_Api API Server URL. E.g. "https://remoteUser:remotePassword@da.domain.tld:8443"
  8. DA_Api_Insecure Insecure TLS. 0: check for cert validity, 1: always accept
  9. Issues: github.com/TigerP/acme.sh/issues
  10. '
  11. ######## Public functions #####################
  12. # Usage: dns_myapi_add _acme-challenge.www.example.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. # Used to add txt record
  14. dns_da_add() {
  15. fulldomain="${1}"
  16. txtvalue="${2}"
  17. _debug "Calling: dns_da_add() '${fulldomain}' '${txtvalue}'"
  18. _DA_credentials && _DA_getDomainInfo && _DA_addTxt
  19. }
  20. # Usage: dns_da_rm _acme-challenge.www.example.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  21. # Used to remove the txt record after validation
  22. dns_da_rm() {
  23. fulldomain="${1}"
  24. txtvalue="${2}"
  25. _debug "Calling: dns_da_rm() '${fulldomain}' '${txtvalue}'"
  26. _DA_credentials && _DA_getDomainInfo && _DA_rmTxt
  27. }
  28. #################### Private functions below ##################################
  29. # Usage: _DA_credentials
  30. # It will check if the needed settings are available
  31. _DA_credentials() {
  32. DA_Api="${DA_Api:-$(_readaccountconf_mutable DA_Api)}"
  33. DA_Api_Insecure="${DA_Api_Insecure:-$(_readaccountconf_mutable DA_Api_Insecure)}"
  34. if [ -z "${DA_Api}" ] || [ -z "${DA_Api_Insecure}" ]; then
  35. DA_Api=""
  36. DA_Api_Insecure=""
  37. _err "You haven't specified the DirectAdmin Login data, URL and whether you want check the DirectAdmin SSL cert. Please try again."
  38. return 1
  39. else
  40. _saveaccountconf_mutable DA_Api "${DA_Api}"
  41. _saveaccountconf_mutable DA_Api_Insecure "${DA_Api_Insecure}"
  42. # Set whether curl should use secure or insecure mode
  43. export HTTPS_INSECURE="${DA_Api_Insecure}"
  44. fi
  45. }
  46. # Usage: _get_root _acme-challenge.www.example.com
  47. # Split the full domain to a domain and subdomain
  48. #returns
  49. # _sub_domain=_acme-challenge.www
  50. # _domain=example.com
  51. _get_root() {
  52. domain=$1
  53. i=2
  54. p=1
  55. # Get a list of all the domains
  56. # response will contain "list[]=example.com&list[]=example.org"
  57. _da_api CMD_API_SHOW_DOMAINS "" "${domain}"
  58. while true; do
  59. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  60. _debug h "$h"
  61. if [ -z "$h" ]; then
  62. # not valid
  63. _debug "The given domain $h is not valid"
  64. return 1
  65. fi
  66. if _contains "$response" "$h" >/dev/null; then
  67. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  68. _domain=$h
  69. return 0
  70. fi
  71. p=$i
  72. i=$(_math "$i" + 1)
  73. done
  74. _debug "Stop on 100"
  75. return 1
  76. }
  77. # Usage: _da_api CMD_API_* data example.com
  78. # Use the DirectAdmin API and check the result
  79. # returns
  80. # response="error=0&text=Result text&details="
  81. _da_api() {
  82. cmd=$1
  83. data=$2
  84. domain=$3
  85. _debug "$domain; $data"
  86. response="$(_post "$data" "$DA_Api/$cmd" "" "POST")"
  87. if [ "$?" != "0" ]; then
  88. _err "error $cmd"
  89. return 1
  90. fi
  91. _debug response "$response"
  92. case "${cmd}" in
  93. CMD_API_DNS_CONTROL)
  94. # Parse the result in general
  95. # error=0&text=Records Deleted&details=
  96. # error=1&text=Cannot View Dns Record&details=No domain provided
  97. err_field="$(_getfield "$response" 1 '&')"
  98. txt_field="$(_getfield "$response" 2 '&')"
  99. details_field="$(_getfield "$response" 3 '&')"
  100. error="$(_getfield "$err_field" 2 '=')"
  101. text="$(_getfield "$txt_field" 2 '=')"
  102. details="$(_getfield "$details_field" 2 '=')"
  103. _debug "error: ${error}, text: ${text}, details: ${details}"
  104. if [ "$error" != "0" ]; then
  105. _err "error $response"
  106. return 1
  107. fi
  108. ;;
  109. CMD_API_SHOW_DOMAINS) ;;
  110. esac
  111. return 0
  112. }
  113. # Usage: _DA_getDomainInfo
  114. # Get the root zone if possible
  115. _DA_getDomainInfo() {
  116. _debug "First detect the root zone"
  117. if ! _get_root "$fulldomain"; then
  118. _err "invalid domain"
  119. return 1
  120. else
  121. _debug "The root domain: $_domain"
  122. _debug "The sub domain: $_sub_domain"
  123. fi
  124. return 0
  125. }
  126. # Usage: _DA_addTxt
  127. # Use the API to add a record
  128. _DA_addTxt() {
  129. curData="domain=${_domain}&action=add&type=TXT&name=${_sub_domain}&value=\"${txtvalue}\""
  130. _debug "Calling _DA_addTxt: '${curData}' '${DA_Api}/CMD_API_DNS_CONTROL'"
  131. _da_api CMD_API_DNS_CONTROL "${curData}" "${_domain}"
  132. _debug "Result of _DA_addTxt: '$response'"
  133. if _contains "${response}" 'error=0'; then
  134. _debug "Add TXT succeeded"
  135. return 0
  136. fi
  137. _debug "Add TXT failed"
  138. return 1
  139. }
  140. # Usage: _DA_rmTxt
  141. # Use the API to remove a record
  142. _DA_rmTxt() {
  143. curData="domain=${_domain}&action=select&txtrecs0=name=${_sub_domain}&value=\"${txtvalue}\""
  144. _debug "Calling _DA_rmTxt: '${curData}' '${DA_Api}/CMD_API_DNS_CONTROL'"
  145. if _da_api CMD_API_DNS_CONTROL "${curData}" "${_domain}"; then
  146. _debug "Result of _DA_rmTxt: '$response'"
  147. else
  148. _err "Result of _DA_rmTxt: '$response'"
  149. fi
  150. if _contains "${response}" 'error=0'; then
  151. _debug "RM TXT succeeded"
  152. return 0
  153. fi
  154. _debug "RM TXT failed"
  155. return 1
  156. }