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.5 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
  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. NIC_ClientID=$(echo "$_two_values" | cut -d':' -f1)
  76. NIC_ClientSecret=$(echo "$_two_values" | cut -d':' -f2-)
  77. _debug restored_NIC_ClientID "$NIC_ClientID"
  78. _debug restored_NIC_ClientSecret "$NIC_ClientSecret"
  79. fi
  80. fi
  81. if [ -z "$NIC_ClientID" ] || [ -z "$NIC_ClientSecret" ] || [ -z "$NIC_Username" ] || [ -z "$NIC_Password" ]; then
  82. NIC_ClientID=""
  83. NIC_ClientSecret=""
  84. NIC_Username=""
  85. NIC_Password=""
  86. _err "You must export variables: NIC_ClientID, NIC_ClientSecret, NIC_Username and NIC_Password"
  87. return 1
  88. fi
  89. if [ "$_need2save" ]; then
  90. _saveaccountconf_mutable NIC_ClientID "$NIC_ClientID"
  91. _saveaccountconf_mutable NIC_ClientSecret "$NIC_ClientSecret"
  92. _saveaccountconf_mutable NIC_Username "$NIC_Username"
  93. _saveaccountconf_mutable NIC_Password "$NIC_Password"
  94. fi
  95. NIC_BasicAuth=$(printf "%s:%s" "${NIC_ClientID}" "${NIC_ClientSecret}" | _base64)
  96. _debug NIC_BasicAuth "$NIC_BasicAuth"
  97. }
  98. #_nic_get_authtoken [need2save]
  99. _nic_get_authtoken() {
  100. _need2save=$1
  101. if ! _nic_get_auth_elements "$_need2save"; then
  102. return 1
  103. fi
  104. _info "Getting NIC auth token"
  105. export _H1="Authorization: Basic ${NIC_BasicAuth}"
  106. export _H2="Content-Type: application/x-www-form-urlencoded"
  107. 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")
  108. if _contains "$res" "access_token"; then
  109. _auth_token=$(printf "%s" "$res" | cut -d , -f2 | tr -d "\"" | sed "s/access_token://")
  110. _info "Token received"
  111. _debug _auth_token "$_auth_token"
  112. return 0
  113. fi
  114. return 1
  115. }
  116. _get_root() {
  117. domain="$1"
  118. i=1
  119. p=1
  120. if ! _nic_rest GET "zones"; then
  121. return 1
  122. fi
  123. _all_domains=$(printf "%s" "$response" | grep "idn-name" | sed -r "s/.*idn-name=\"(.*)\" name=.*/\1/g")
  124. _debug2 _all_domains "$_all_domains"
  125. while true; do
  126. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  127. _debug h "$h"
  128. if [ -z "$h" ]; then
  129. return 1
  130. fi
  131. if _contains "$_all_domains" "^$h$"; then
  132. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  133. _domain=$h
  134. _service=$(printf "%s" "$response" | grep "idn-name=\"$_domain\"" | sed -r "s/.*service=\"(.*)\".*$/\1/")
  135. return 0
  136. fi
  137. p="$i"
  138. i=$(_math "$i" + 1)
  139. done
  140. return 1
  141. }
  142. _nic_rest() {
  143. m="$1"
  144. ep="$2"
  145. data="$3"
  146. _debug "$ep"
  147. export _H1="Content-Type: application/xml"
  148. export _H2="Authorization: Bearer $_auth_token"
  149. if [ "$m" != "GET" ]; then
  150. _debug data "$data"
  151. response=$(_post "$data" "$NIC_Api/dns-master/$ep" "" "$m")
  152. else
  153. response=$(_get "$NIC_Api/dns-master/$ep")
  154. fi
  155. if _contains "$response" "<errors>"; then
  156. error=$(printf "%s" "$response" | grep "error code" | sed -r "s/.*<error code=.*>(.*)<\/error>/\1/g")
  157. _err "Error: $error"
  158. return 1
  159. fi
  160. if ! _contains "$response" "<status>success</status>"; then
  161. return 1
  162. fi
  163. _debug2 response "$response"
  164. return 0
  165. }