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.

155 lines
4.8 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 (uncomment the line below and change xxxx to your login):
  10. #HostingUkraine_Login="xxxx"
  11. # Your api token (uncomment the line below and change xxxx to your token):
  12. #HostingUkraine_Token="xxxx"
  13. ######## Public functions #####################
  14. # Used to add txt record
  15. #fulldomain=_acme-challenge.yourDomain.com
  16. #txtvalue=txt_data_value
  17. dns_hostingukraine_add() {
  18. fulldomain=$1
  19. txtvalue=$2
  20. _hostingukraine_init
  21. _debug "First detect the root zone"
  22. if ! _get_root "$fulldomain"; then
  23. _err "Invalid root domain for: ($fulldomain)"
  24. return 1
  25. fi
  26. _info "Adding txt record. ($fulldomain)"
  27. _hostingukraine_api_request POST "dns_record" "create" "\"domain\":\"$_domain\",\"subdomain\":\"$_sub_domain\",\"type\":\"TXT\",\"data\":\"$txtvalue\""
  28. if _contains "$response" "\"status\":\"error\""; then
  29. _err "Add txt record, Failure! ($fulldomain)"
  30. return 1
  31. fi
  32. _info "Add txt record, OK! ($fulldomain)"
  33. return 0
  34. }
  35. # Used to remove the txt record after validation
  36. #fulldomain=_acme-challenge.yourDomain.com
  37. #txtvalue=txt_data_value
  38. dns_hostingukraine_rm() {
  39. fulldomain=$1
  40. txtvalue=$2
  41. _hostingukraine_init
  42. _debug "First detect the root zone"
  43. if ! _get_root "$fulldomain"; then
  44. _err "Invalid root domain for: ($fulldomain)"
  45. return 1
  46. fi
  47. _debug "Getting txt records"
  48. _hostingukraine_api_request POST "dns_record" "info" "\"domain\":\"$_domain\""
  49. if _contains "$response" "\"status\":\"error\""; then
  50. _err "Get domain records, Failure! ($_domain)"
  51. return 1
  52. fi
  53. ids=$(echo "$response" | _egrep_o "[^{]+${txtvalue}[^}]+" | _egrep_o "id\":[^\,]+" | cut -c5-)
  54. _debug ids "$ids"
  55. if [ -z "$ids" ]; then
  56. _err "Empty TXT records! ($fulldomain: $txtvalue)"
  57. return 1
  58. fi
  59. for id in $ids; do
  60. stack="${stack:+${stack},}${id}"
  61. done
  62. _hostingukraine_api_request POST "dns_record" "delete" "\"domain\":\"$_domain\",\"stack\":[$stack]"
  63. if _contains "$response" "\"status\":\"error\""; then
  64. _err "Remove txt record, Failure! ($fulldomain: $id)"
  65. return 1
  66. fi
  67. _info "Remove txt record, OK! ($fulldomain: $id)"
  68. return 0
  69. }
  70. #################### Private functions below ##################################
  71. # Check root zone
  72. #domain=_acme-challenge.yourDomain.com
  73. #returns
  74. # _sub_domain=_acme-challenge
  75. # _domain=yourDomain.com
  76. _get_root() {
  77. domain=$1
  78. i=1
  79. p=1
  80. _hostingukraine_api_request POST "dns_domain" "info" "\"search\":\"\""
  81. while true; do
  82. host=$(printf "%s" "$domain" | cut -d . -f $i-100)
  83. _debug host "$host"
  84. if [ -z "$host" ]; then
  85. _err "Get root, Failure! ($domain)"
  86. return 1
  87. fi
  88. if _contains "$response" "\"name\":\"$host\""; then
  89. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  90. _domain=$host
  91. _debug fulldomain "$domain"
  92. _debug _sub_domain "$_sub_domain"
  93. _debug _domain "$_domain"
  94. _info "Get root, OK! ($host)"
  95. return 0
  96. fi
  97. p=$i
  98. i=$(_math "$i" + 1)
  99. done
  100. _err "Get root, Error! ($domain)"
  101. return 1
  102. }
  103. # Check credentials and root zone
  104. _hostingukraine_init() {
  105. HostingUkraine_Login="${HostingUkraine_Login:-$(_readaccountconf_mutable HostingUkraine_Login)}"
  106. HostingUkraine_Token="${HostingUkraine_Token:-$(_readaccountconf_mutable HostingUkraine_Token)}"
  107. if [ -z "$HostingUkraine_Login" ] || [ -z "$HostingUkraine_Token" ]; then
  108. HostingUkraine_Login=""
  109. HostingUkraine_Token=""
  110. _err "You didn't specify a Hosting Ukraine account or token yet."
  111. _err "Please create the account and token and try again. Info: https://api.adm.tools/osnovnie-polozheniya/dostup-k-api/"
  112. return 1
  113. fi
  114. _saveaccountconf_mutable HostingUkraine_Login "$HostingUkraine_Login"
  115. _saveaccountconf_mutable HostingUkraine_Token "$HostingUkraine_Token"
  116. }
  117. # Send request to API endpoint
  118. #request_method=POST
  119. #class=dns_domain|dns_record
  120. #method=info|create|delete
  121. #data=part_of_json_data
  122. _hostingukraine_api_request() {
  123. request_method=$1
  124. class=$2
  125. method=$3
  126. data=$4
  127. response="$(_post "{\"auth_login\":\"$HostingUkraine_Login\",\"auth_token\":\"$HostingUkraine_Token\",\"class\":\"$class\",\"method\":\"$method\",$data}" "$HostingUkraine_Api" "" "$request_method" "application/json")"
  128. if [ "$?" != "0" ]; then
  129. _err "error $response"
  130. return 1
  131. fi
  132. _debug2 response "$response"
  133. return 0
  134. }