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.

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