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.

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