]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_nic.sh
minor
[mirror_acme.sh.git] / dnsapi / dns_nic.sh
1 #!/usr/bin/env sh
2
3 #
4 #NIC_Token="sdfsdfsdfljlbjkljlkjsdfoiwjedfglgkdlfgkfgldfkg"
5 #
6 #NIC_Username="000000/NIC-D"
7
8 #NIC_Password="xxxxxxx"
9
10 NIC_Api="https://api.nic.ru"
11
12 dns_nic_add() {
13 fulldomain="${1}"
14 txtvalue="${2}"
15
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
20 NIC_Token=""
21 NIC_Username=""
22 NIC_Password=""
23 _err "You must export variables: NIC_Token, NIC_Username and NIC_Password"
24 return 1
25 fi
26
27 _saveaccountconf_mutable NIC_Customer "$NIC_Token"
28 _saveaccountconf_mutable NIC_Username "$NIC_Username"
29 _saveaccountconf_mutable NIC_Password "$NIC_Password"
30
31 if ! _nic_get_authtoken "$NIC_Username" "$NIC_Password" "$NIC_Token"; then
32 _err "get NIC auth token failed"
33 return 1
34 fi
35
36 _debug "First detect the root zone"
37 if ! _get_root "$fulldomain"; then
38 _err "Invalid domain"
39 return 1
40 fi
41
42 _debug _sub_domain "$_sub_domain"
43 _debug _domain "$_domain"
44 _debug _service "$_service"
45
46 _info "Adding record"
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"
49 return 1
50 fi
51
52 if ! _nic_rest POST "services/$_service/zones/$_domain/commit" ""; then
53 return 1
54 fi
55 _info "Added, OK"
56 }
57
58 dns_nic_rm() {
59 fulldomain="${1}"
60 txtvalue="${2}"
61
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
66 NIC_Token=""
67 NIC_Username=""
68 NIC_Password=""
69 _err "You must export variables: NIC_Token, NIC_Username and NIC_Password"
70 return 1
71 fi
72
73 if ! _nic_get_authtoken "$NIC_Username" "$NIC_Password" "$NIC_Token"; then
74 _err "get NIC auth token failed"
75 return 1
76 fi
77
78 if ! _get_root "$fulldomain"; then
79 _err "Invalid domain"
80 return 1
81 fi
82
83 _debug _sub_domain "$_sub_domain"
84 _debug _domain "$_domain"
85 _debug _service "$_service"
86
87 if ! _nic_rest GET "services/$_service/zones/$_domain/records"; then
88 _err "Get records error"
89 return 1
90 fi
91
92 _domain_id=$(printf "%s" "$response" | grep "$_sub_domain" | grep -- "$txtvalue" | sed -r "s/.*<rr id=\"(.*)\".*/\1/g")
93
94 if ! _nic_rest DELETE "services/$_service/zones/$_domain/records/$_domain_id"; then
95 _err "Delete record error"
96 return 1
97 fi
98
99 if ! _nic_rest POST "services/$_service/zones/$_domain/commit" ""; then
100 return 1
101 fi
102 }
103
104 #################### Private functions below ##################################
105
106 _nic_get_authtoken() {
107 username="$1"
108 password="$2"
109 token="$3"
110
111 _info "Getting NIC auth token"
112
113 export _H1="Authorization: Basic $token"
114 export _H2="Content-Type: application/x-www-form-urlencoded"
115
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"
121 return 0
122 fi
123 return 1
124 }
125
126 _get_root() {
127 domain="$1"
128 i=1
129 p=1
130
131 if ! _nic_rest GET "zones"; then
132 return 1
133 fi
134
135 _all_domains=$(printf "%s" "$response" | grep "idn-name" | sed -r "s/.*idn-name=\"(.*)\" name=.*/\1/g")
136 _debug2 _all_domains "$_all_domains"
137
138 while true; do
139 h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
140 _debug h "$h"
141
142 if [ -z "$h" ]; then
143 return 1
144 fi
145
146 if _contains "$_all_domains" "^$h$"; then
147 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
148 _domain=$h
149 _service=$(printf "%s" "$response" | grep "$_domain" | sed -r "s/.*service=\"(.*)\".*$/\1/")
150 return 0
151 fi
152 p="$i"
153 i=$(_math "$i" + 1)
154 done
155 return 1
156 }
157
158 _nic_rest() {
159 m="$1"
160 ep="$2"
161 data="$3"
162 _debug "$ep"
163
164 export _H1="Content-Type: application/xml"
165 export _H2="Authorization: Bearer $_auth_token"
166
167 if [ "$m" != "GET" ]; then
168 _debug data "$data"
169 response=$(_post "$data" "$NIC_Api/dns-master/$ep" "" "$m")
170 else
171 response=$(_get "$NIC_Api/dns-master/$ep")
172 fi
173
174 if _contains "$response" "<errors>"; then
175 error=$(printf "%s" "$response" | grep "error code" | sed -r "s/.*<error code=.*>(.*)<\/error>/\1/g")
176 _err "Error: $error"
177 return 1
178 fi
179
180 if ! _contains "$response" "<status>success</status>"; then
181 return 1
182 fi
183 _debug2 response "$response"
184 return 0
185 }