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.

210 lines
5.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1 month ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_nic_info='nic.ru
  4. Site: nic.ru
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_nic
  6. Options:
  7. NIC_ClientID Client ID
  8. NIC_ClientSecret Client Secret
  9. NIC_Username Username
  10. NIC_Password Password
  11. Issues: github.com/acmesh-official/acme.sh/issues/2547
  12. '
  13. NIC_Api="https://api.nic.ru"
  14. dns_nic_add() {
  15. fulldomain="${1}"
  16. txtvalue="${2}"
  17. if ! _nic_get_authtoken save; then
  18. _err "get NIC auth token failed"
  19. return 1
  20. fi
  21. _debug "First detect the root zone"
  22. if ! _get_root "$fulldomain"; then
  23. _err "Invalid domain"
  24. return 1
  25. fi
  26. _debug _sub_domain "$_sub_domain"
  27. _debug _domain "$_domain"
  28. _debug _service "$_service"
  29. _info "Adding record"
  30. if ! _nic_rest PUT "services/$_service/zones/$_domain/records" "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><request><rr-list><rr><name>$_sub_domain</name><type>TXT</type><txt><string>$txtvalue</string></txt></rr></rr-list></request>"; then
  31. _err "Add TXT record error"
  32. return 1
  33. fi
  34. if ! _nic_rest POST "services/$_service/zones/$_domain/commit" ""; then
  35. return 1
  36. fi
  37. _info "Added, OK"
  38. }
  39. dns_nic_rm() {
  40. fulldomain="${1}"
  41. txtvalue="${2}"
  42. if ! _nic_get_authtoken; then
  43. _err "get NIC auth token failed"
  44. return 1
  45. fi
  46. if ! _get_root "$fulldomain"; then
  47. _err "Invalid domain"
  48. return 1
  49. fi
  50. _debug _sub_domain "$_sub_domain"
  51. _debug _domain "$_domain"
  52. _debug _service "$_service"
  53. if ! _nic_rest GET "services/$_service/zones/$_domain/records"; then
  54. _err "Get records error"
  55. return 1
  56. fi
  57. _domain_id=$(printf "%s" "$response" | grep "$_sub_domain" | grep -- "$txtvalue" | sed -r "s/.*<rr id=\"(.*)\".*/\1/g")
  58. if ! _nic_rest DELETE "services/$_service/zones/$_domain/records/$_domain_id"; then
  59. _err "Delete record error"
  60. return 1
  61. fi
  62. if ! _nic_rest POST "services/$_service/zones/$_domain/commit" ""; then
  63. return 1
  64. fi
  65. }
  66. #################### Private functions below ##################################
  67. #_nic_get_auth_elements [need2save]
  68. _nic_get_auth_elements() {
  69. _need2save=$1
  70. NIC_ClientID="${NIC_ClientID:-$(_readaccountconf_mutable NIC_ClientID)}"
  71. NIC_ClientSecret="${NIC_ClientSecret:-$(_readaccountconf_mutable NIC_ClientSecret)}"
  72. NIC_Username="${NIC_Username:-$(_readaccountconf_mutable NIC_Username)}"
  73. NIC_Password="${NIC_Password:-$(_readaccountconf_mutable NIC_Password)}"
  74. ## for backward compatibility
  75. if [ -z "$NIC_ClientID" ] || [ -z "$NIC_ClientSecret" ]; then
  76. NIC_Token="${NIC_Token:-$(_readaccountconf_mutable NIC_Token)}"
  77. _debug NIC_Token "$NIC_Token"
  78. if [ -n "$NIC_Token" ]; then
  79. _two_values="$(echo "${NIC_Token}" | _dbase64)"
  80. _debug _two_values "$_two_values"
  81. NIC_ClientID=$(echo "$_two_values" | cut -d':' -f1)
  82. NIC_ClientSecret=$(echo "$_two_values" | cut -d':' -f2-)
  83. _debug restored_NIC_ClientID "$NIC_ClientID"
  84. _debug restored_NIC_ClientSecret "$NIC_ClientSecret"
  85. fi
  86. fi
  87. if [ -z "$NIC_ClientID" ] || [ -z "$NIC_ClientSecret" ] || [ -z "$NIC_Username" ] || [ -z "$NIC_Password" ]; then
  88. NIC_ClientID=""
  89. NIC_ClientSecret=""
  90. NIC_Username=""
  91. NIC_Password=""
  92. _err "You must export variables: NIC_ClientID, NIC_ClientSecret, NIC_Username and NIC_Password"
  93. return 1
  94. fi
  95. if [ "$_need2save" ]; then
  96. _saveaccountconf_mutable NIC_ClientID "$NIC_ClientID"
  97. _saveaccountconf_mutable NIC_ClientSecret "$NIC_ClientSecret"
  98. _saveaccountconf_mutable NIC_Username "$NIC_Username"
  99. _saveaccountconf_mutable NIC_Password "$NIC_Password"
  100. fi
  101. NIC_BasicAuth=$(printf "%s:%s" "${NIC_ClientID}" "${NIC_ClientSecret}" | _base64)
  102. _debug NIC_BasicAuth "$NIC_BasicAuth"
  103. }
  104. #_nic_get_authtoken [need2save]
  105. _nic_get_authtoken() {
  106. _need2save=$1
  107. if ! _nic_get_auth_elements "$_need2save"; then
  108. return 1
  109. fi
  110. _info "Getting NIC auth token"
  111. export _H1="Authorization: Basic ${NIC_BasicAuth}"
  112. export _H2="Content-Type: application/x-www-form-urlencoded"
  113. res=$(_post "grant_type=password&username=${NIC_Username}&password=${NIC_Password}&scope=%28GET%7CPUT%7CPOST%7CDELETE%29%3A%2Fdns-master%2F.%2B" "$NIC_Api/oauth/token" "" "POST")
  114. if _contains "$res" "access_token"; then
  115. _auth_token=$(printf "%s" "$res" | cut -d , -f2 | tr -d "\"" | sed "s/access_token://")
  116. _info "Token received"
  117. _debug _auth_token "$_auth_token"
  118. return 0
  119. fi
  120. return 1
  121. }
  122. _get_root() {
  123. domain="$1"
  124. i=1
  125. p=1
  126. if ! _nic_rest GET "zones"; then
  127. return 1
  128. fi
  129. _all_domains=$(printf "%s" "$response" | grep "idn-name" | sed -r "s/.*idn-name=\"(.*)\" name=.*/\1/g")
  130. _debug2 _all_domains "$_all_domains"
  131. while true; do
  132. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  133. _debug h "$h"
  134. if [ -z "$h" ]; then
  135. return 1
  136. fi
  137. if _contains "$_all_domains" "^$h$"; then
  138. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  139. _domain=$h
  140. _service=$(printf "%s" "$response" | grep -m 1 "idn-name=\"$_domain\"" | sed -r "s/.*service=\"(.*)\".*$/\1/")
  141. return 0
  142. fi
  143. p="$i"
  144. i=$(_math "$i" + 1)
  145. done
  146. return 1
  147. }
  148. _nic_rest() {
  149. m="$1"
  150. ep="$2"
  151. data="$3"
  152. _debug "$ep"
  153. export _H1="Content-Type: application/xml"
  154. export _H2="Authorization: Bearer $_auth_token"
  155. if [ "$m" != "GET" ]; then
  156. _debug data "$data"
  157. response=$(_post "$data" "$NIC_Api/dns-master/$ep" "" "$m")
  158. else
  159. response=$(_get "$NIC_Api/dns-master/$ep")
  160. fi
  161. if _contains "$response" "<errors>"; then
  162. error=$(printf "%s" "$response" | grep "error code" | sed -r "s/.*<error code=.*>(.*)<\/error>/\1/g")
  163. _err "Error: $error"
  164. return 1
  165. fi
  166. if ! _contains "$response" "<status>success</status>"; then
  167. return 1
  168. fi
  169. _debug2 response "$response"
  170. return 0
  171. }