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.

213 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
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. _res_arr=$(printf "%s" "$res" | tr -d "\"{}" | tr "," " ");
  110. for r in $_res_arr; do
  111. if [ $(echo "$r" | grep "access_token") ]; then
  112. _auth_token=$(echo "$r" | sed "s/access_token://")
  113. fi
  114. done
  115. _info "Token received"
  116. _debug _auth_token "$_auth_token"
  117. return 0
  118. fi
  119. return 1
  120. }
  121. _get_root() {
  122. domain="$1"
  123. i=1
  124. p=1
  125. if ! _nic_rest GET "zones"; then
  126. return 1
  127. fi
  128. _all_domains=$(printf "%s" "$response" | grep "idn-name" | sed -r "s/.*idn-name=\"(.*)\" name=.*/\1/g")
  129. _debug2 _all_domains "$_all_domains"
  130. while true; do
  131. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  132. _debug h "$h"
  133. if [ -z "$h" ]; then
  134. return 1
  135. fi
  136. if _contains "$_all_domains" "^$h$"; then
  137. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  138. _domain=$h
  139. _service=$(printf "%s" "$response" | grep -m 1 "idn-name=\"$_domain\"" | sed -r "s/.*service=\"(.*)\".*$/\1/")
  140. return 0
  141. fi
  142. p="$i"
  143. i=$(_math "$i" + 1)
  144. done
  145. return 1
  146. }
  147. _nic_rest() {
  148. m="$1"
  149. ep="$2"
  150. data="$3"
  151. _debug "$ep"
  152. export _H1="Content-Type: application/xml"
  153. export _H2="Authorization: Bearer $_auth_token"
  154. if [ "$m" != "GET" ]; then
  155. _debug data "$data"
  156. response=$(_post "$data" "$NIC_Api/dns-master/$ep" "" "$m")
  157. else
  158. response=$(_get "$NIC_Api/dns-master/$ep")
  159. fi
  160. if _contains "$response" "<errors>"; then
  161. error=$(printf "%s" "$response" | grep "error code" | sed -r "s/.*<error code=.*>(.*)<\/error>/\1/g")
  162. _err "Error: $error"
  163. return 1
  164. fi
  165. if ! _contains "$response" "<status>success</status>"; then
  166. return 1
  167. fi
  168. _debug2 response "$response"
  169. return 0
  170. }