4 #NIC_Token="sdfsdfsdfljlbjkljlkjsdfoiwjedfglgkdlfgkfgldfkg"
6 #NIC_Username="000000/NIC-D"
8 #NIC_Password="xxxxxxx"
10 NIC_Api
="https://api.nic.ru"
16 NIC_Token
="${NIC_Token:-$(_readaccountconf_mutable NIC_Token)}"
17 NIC_Username
="${NIC_Username:-$(_readaccountconf_mutable NIC_Username)}"
18 NIC_Password
="${NIC_Password:-$(_readaccountconf_mutable NIC_Password)}"
19 if [ -z "$NIC_Token" ] ||
[ -z "$NIC_Username" ] ||
[ -z "$NIC_Password" ]; then
23 _err
"You must export variables: NIC_Token, NIC_Username and NIC_Password"
27 _saveaccountconf_mutable NIC_Customer
"$NIC_Token"
28 _saveaccountconf_mutable NIC_Username
"$NIC_Username"
29 _saveaccountconf_mutable NIC_Password
"$NIC_Password"
31 if ! _nic_get_authtoken
"$NIC_Username" "$NIC_Password" "$NIC_Token"; then
32 _err
"get NIC auth token failed"
36 _debug
"First detect the root zone"
37 if ! _get_root
"$fulldomain"; then
42 _debug _sub_domain
"$_sub_domain"
43 _debug _domain
"$_domain"
44 _debug _service
"$_service"
47 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
48 _err
"Add TXT record error"
52 if ! _nic_rest POST
"services/$_service/zones/$_domain/commit" ""; then
62 NIC_Token
="${NIC_Token:-$(_readaccountconf_mutable NIC_Token)}"
63 NIC_Username
="${NIC_Username:-$(_readaccountconf_mutable NIC_Username)}"
64 NIC_Password
="${NIC_Password:-$(_readaccountconf_mutable NIC_Password)}"
65 if [ -z "$NIC_Token" ] ||
[ -z "$NIC_Username" ] ||
[ -z "$NIC_Password" ]; then
69 _err
"You must export variables: NIC_Token, NIC_Username and NIC_Password"
73 if ! _nic_get_authtoken
"$NIC_Username" "$NIC_Password" "$NIC_Token"; then
74 _err
"get NIC auth token failed"
78 if ! _get_root
"$fulldomain"; then
83 _debug _sub_domain
"$_sub_domain"
84 _debug _domain
"$_domain"
85 _debug _service
"$_service"
87 if ! _nic_rest GET
"services/$_service/zones/$_domain/records"; then
88 _err
"Get records error"
92 _domain_id
=$
(printf "%s" "$response" |
grep "$_sub_domain" |
grep "$txtvalue" |
sed -r "s/.*<rr id=\"(.*)\".*/\1/g")
94 if ! _nic_rest DELETE
"services/$_service/zones/$_domain/records/$_domain_id"; then
95 _err
"Delete record error"
99 if ! _nic_rest POST
"services/$_service/zones/$_domain/commit" ""; then
104 #################### Private functions below ##################################
106 _nic_get_authtoken
() {
111 _info
"Getting NIC auth token"
113 export _H1
="Authorization: Basic $token"
114 export _H2
="Content-Type: application/x-www-form-urlencoded"
116 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")
117 if _contains
"$res" "access_token"; then
118 _auth_token
=$
(printf "%s" "$res" | cut
-d , -f2 |
tr -d "\"" |
sed "s/access_token://")
119 _info
"Token received"
120 _debug _auth_token
"$_auth_token"
131 if ! _nic_rest GET
"zones"; then
135 _all_domains
=$
(printf "%s" "$response" |
grep "idn-name" |
sed -r "s/.*idn-name=\"(.*)\" name=.*/\1/g")
136 _debug2 _all_domains
"$_all_domains"
139 h
=$
(printf "%s" "$domain" | cut
-d .
-f "$i"-100)
146 if _contains
"$_all_domains" "^$h$"; then
147 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
149 _service
=$
(printf "%s" "$response" |
grep "$_domain" |
sed -r "s/.*service=\"(.*)\".*$/\1/")
164 export _H1
="Content-Type: application/xml"
165 export _H2
="Authorization: Bearer $_auth_token"
167 if [ "$m" != "GET" ]; then
169 response
=$
(_post
"$data" "$NIC_Api/dns-master/$ep" "" "$m")
171 response
=$
(_get
"$NIC_Api/dns-master/$ep")
174 if _contains
"$response" "<errors>"; then
175 error
=$
(printf "%s" "$response" |
grep "error code" |
sed -r "s/.*<error code=.*>(.*)<\/error>/\1/g")
180 if ! _contains
"$response" "<status>success</status>"; then
183 _debug2 response
"$response"