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.

134 lines
4.1 KiB

  1. #!/usr/bin/env sh
  2. # Author: Qvinti (Aleksandr Zaitsev) <qvinti.com@gmail.com>
  3. # Report Bugs here: https://github.com/Neilpang/acme.sh/issues/2683 or https://www.ukraine.com.ua/forum/domennie-imena/acmesh-dnsapi-dlya-hosting-Ukra.html
  4. # Will be called by acme.sh to add the txt record to https://www.ukraine.com.ua/ DNS api.
  5. # Hosting Ukraine API documentation: https://api.adm.tools/osnovnie-polozheniya/dostup-k-api/
  6. # Usage: ./acme.sh --issue -d yourdomain.com [-d '*.yourdomain.com'] --dns dns_hostingukraine
  7. # API endpoint:
  8. HostingUkraine_Api="https://adm.tools/api.php"
  9. # Your login:
  10. HostingUkraine_Login=""
  11. # Your api token:
  12. HostingUkraine_Token=""
  13. ######## Public functions #####################
  14. # Used to add txt record
  15. dns_hostingukraine_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. subdomain=$(echo "$fulldomain" | sed -e "s/\.$domain//")
  19. _hostingukraine_init
  20. _debug fulldomain "$fulldomain"
  21. _debug domain "$domain"
  22. _info "Adding txt record. ($fulldomain)"
  23. _hostingukraine_api_request POST "dns_record" "create" "\"domain\":\"$domain\",\"subdomain\":\"$subdomain\",\"type\":\"TXT\",\"data\":\"$txtvalue\""
  24. if _contains "$response" "\"status\":\"error\""; then
  25. _err "Add txt record, Failure! ($fulldomain)"
  26. return 1
  27. fi
  28. _info "Add txt record, OK! ($fulldomain)"
  29. return 0
  30. }
  31. # Used to remove the txt record after validation
  32. dns_hostingukraine_rm() {
  33. fulldomain=$1
  34. txtvalue=$2
  35. _hostingukraine_init
  36. _debug "Getting txt records"
  37. _hostingukraine_api_request POST "dns_record" "info" "\"domain\":\"$domain\""
  38. if _contains "$response" "\"status\":\"error\""; then
  39. _err "Get domain records, Failure! ($domain)"
  40. return 1
  41. fi
  42. ids=$(echo "$response" | _egrep_o "[^{]+${txtvalue}[^}]+" | _egrep_o "id\":[^\,]+" | cut -c5-)
  43. if [ -z "$ids" ]; then
  44. _err "Empty TXT records! ($fulldomain: $txtvalue)"
  45. return 1
  46. fi
  47. for id in $ids; do
  48. stack="${stack:+${stack},}${id}"
  49. done
  50. _hostingukraine_api_request POST "dns_record" "delete" "\"domain\":\"$domain\",\"stack\":[$stack]"
  51. if _contains "$response" "\"status\":\"error\""; then
  52. _err "Remove txt record, Failure! ($fulldomain: $id)"
  53. return 1
  54. fi
  55. _info "Remove txt record, OK! ($fulldomain: $id)"
  56. return 0
  57. }
  58. #################### Private functions below ##################################
  59. # Check root zone
  60. _get_root() {
  61. domain=$1
  62. i=1
  63. _hostingukraine_api_request POST "dns_domain" "info" "\"search\":\"\""
  64. while true; do
  65. host=$(printf "%s" "$domain" | cut -d . -f $i-100)
  66. _debug host "$host"
  67. if [ -z "$host" ]; then
  68. _err "Get root, Failure! ($domain)"
  69. return 1
  70. fi
  71. if _contains "$response" "\"name\":\"$host\""; then
  72. _info "Get root, OK! ($host)"
  73. return 0
  74. fi
  75. i=$(_math "$i" + 1)
  76. done
  77. _err "Get root, Error! ($domain)"
  78. return 1
  79. }
  80. # Check credentials and root zone
  81. _hostingukraine_init() {
  82. HostingUkraine_Login="${HostingUkraine_Login:-$(_readaccountconf_mutable HostingUkraine_Login)}"
  83. HostingUkraine_Token="${HostingUkraine_Token:-$(_readaccountconf_mutable HostingUkraine_Token)}"
  84. if [ -z "$HostingUkraine_Login" ] || [ -z "$HostingUkraine_Token" ]; then
  85. HostingUkraine_Login=""
  86. HostingUkraine_Token=""
  87. _err "You didn't specify a Hosting Ukraine account or token yet."
  88. _err "Please create the account and token and try again. Info: https://api.adm.tools/osnovnie-polozheniya/dostup-k-api/"
  89. return 1
  90. fi
  91. _saveaccountconf_mutable HostingUkraine_Login "$HostingUkraine_Login"
  92. _saveaccountconf_mutable HostingUkraine_Token "$HostingUkraine_Token"
  93. _debug "First detect the root zone"
  94. if ! _get_root "$domain"; then
  95. _err "Invalid domain! ($domain)"
  96. return 1
  97. fi
  98. }
  99. # Send request to API endpoint
  100. _hostingukraine_api_request() {
  101. request_method=$1
  102. class=$2
  103. method=$3
  104. data=$4
  105. response="$(_post "{\"auth_login\":\"$HostingUkraine_Login\",\"auth_token\":\"$HostingUkraine_Token\",\"class\":\"$class\",\"method\":\"$method\",$data}" "$HostingUkraine_Api" "" "$request_method" "application/json")"
  106. if [ "$?" != "0" ]; then
  107. _err "error $response"
  108. return 1
  109. fi
  110. _debug2 response "$response"
  111. return 0
  112. }