]>
Commit | Line | Data |
---|---|---|
ccc2142b SP |
1 | #!/usr/bin/env sh |
2 | ||
3 | # | |
93d29a97 | 4 | # Hexonet_Login="username!roleId" |
ccc2142b | 5 | # |
93d29a97 | 6 | # Hexonet_Password="rolePassword" |
ccc2142b SP |
7 | |
8 | Hexonet_Api="https://coreapi.1api.net/api/call.cgi" | |
9 | ||
10 | ######## Public functions ##################### | |
11 | ||
12 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
13 | dns_hexonet_add() { | |
14 | fulldomain=$1 | |
15 | txtvalue=$2 | |
16 | ||
93d29a97 | 17 | Hexonet_Login="${Hexonet_Login:-$(_readaccountconf_mutable Hexonet_Login)}" |
ccc2142b | 18 | Hexonet_Password="${Hexonet_Password:-$(_readaccountconf_mutable Hexonet_Password)}" |
93d29a97 | 19 | if [ -z "$Hexonet_Login" ] || [ -z "$Hexonet_Password" ]; then |
20 | Hexonet_Login="" | |
ccc2142b | 21 | Hexonet_Password="" |
93d29a97 | 22 | _err "You must export variables: Hexonet_Login and Hexonet_Password" |
ccc2142b SP |
23 | return 1 |
24 | fi | |
25 | ||
93d29a97 | 26 | if ! _contains "$Hexonet_Login" "!"; then |
27 | _err "It seems that the Hexonet_Login=$Hexonet_Login is not a restrivteed user." | |
ccc2142b SP |
28 | _err "Please check and retry." |
29 | return 1 | |
30 | fi | |
31 | ||
32 | #save the username and password to the account conf file. | |
93d29a97 | 33 | _saveaccountconf_mutable Hexonet_Login "$Hexonet_Login" |
ccc2142b SP |
34 | _saveaccountconf_mutable Hexonet_Password "$Hexonet_Password" |
35 | ||
36 | _debug "First detect the root zone" | |
37 | if ! _get_root "$fulldomain"; then | |
38 | _err "invalid domain" | |
39 | return 1 | |
40 | fi | |
41 | _debug _sub_domain "$_sub_domain" | |
42 | _debug _domain "$_domain" | |
43 | ||
44 | _debug "Getting txt records" | |
e9edecf3 | 45 | _hexonet_rest "command=QueryDNSZoneRRList&dnszone=${h}.&RRTYPE=TXT" |
ccc2142b SP |
46 | |
47 | if ! _contains "$response" "CODE=200"; then | |
48 | _err "Error" | |
49 | return 1 | |
50 | fi | |
51 | ||
52 | _info "Adding record" | |
53 | if _hexonet_rest "command=UpdateDNSZone&dnszone=${_domain}.&addrr0=${_sub_domain}%20IN%20TXT%20${txtvalue}"; then | |
54 | if _contains "$response" "CODE=200"; then | |
55 | _info "Added, OK" | |
56 | return 0 | |
57 | else | |
58 | _err "Add txt record error." | |
59 | return 1 | |
60 | fi | |
61 | fi | |
62 | _err "Add txt record error." | |
63 | return 1 | |
64 | ||
65 | } | |
66 | ||
67 | #fulldomain txtvalue | |
68 | dns_hexonet_rm() { | |
69 | fulldomain=$1 | |
70 | txtvalue=$2 | |
71 | ||
93d29a97 | 72 | Hexonet_Login="${Hexonet_Login:-$(_readaccountconf_mutable Hexonet_Login)}" |
ccc2142b | 73 | Hexonet_Password="${Hexonet_Password:-$(_readaccountconf_mutable Hexonet_Password)}" |
93d29a97 | 74 | if [ -z "$Hexonet_Login" ] || [ -z "$Hexonet_Password" ]; then |
75 | Hexonet_Login="" | |
ccc2142b | 76 | Hexonet_Password="" |
93d29a97 | 77 | _err "You must export variables: Hexonet_Login and Hexonet_Password" |
ccc2142b SP |
78 | return 1 |
79 | fi | |
80 | ||
81 | _debug "First detect the root zone" | |
82 | if ! _get_root "$fulldomain"; then | |
83 | _err "invalid domain" | |
84 | return 1 | |
85 | fi | |
86 | ||
87 | _debug _sub_domain "$_sub_domain" | |
88 | _debug _domain "$_domain" | |
89 | ||
90 | _debug "Getting txt records" | |
a9d46297 | 91 | _hexonet_rest "command=QueryDNSZoneRRList&dnszone=${h}.&RRTYPE=TXT&RR=${_sub_domain}%20IN%20TXT%20\"${txtvalue}\"" |
ccc2142b SP |
92 | |
93 | if ! _contains "$response" "CODE=200"; then | |
94 | _err "Error" | |
95 | return 1 | |
96 | fi | |
97 | ||
98 | count=$(printf "%s\n" "$response" | _egrep_o "PROPERTY[TOTAL][0]=" | cut -d = -f 2) | |
99 | _debug count "$count" | |
100 | if [ "$count" = "0" ]; then | |
101 | _info "Don't need to remove." | |
102 | else | |
a9d46297 | 103 | if ! _hexonet_rest "command=UpdateDNSZone&dnszone=${_domain}.&delrr0=${_sub_domain}%20IN%20TXT%20\"${txtvalue}\""; then |
ccc2142b SP |
104 | _err "Delete record error." |
105 | return 1 | |
106 | fi | |
107 | _contains "$response" "CODE=200" | |
108 | fi | |
109 | ||
110 | } | |
111 | ||
112 | #################### Private functions below ################################## | |
113 | #_acme-challenge.www.domain.com | |
114 | #returns | |
115 | # _sub_domain=_acme-challenge.www | |
116 | # _domain=domain.com | |
117 | _get_root() { | |
118 | domain=$1 | |
119 | i=1 | |
120 | p=1 | |
121 | while true; do | |
122 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
123 | _debug h "$h" | |
124 | if [ -z "$h" ]; then | |
125 | #not valid | |
126 | return 1 | |
127 | fi | |
128 | ||
e9edecf3 | 129 | if ! _hexonet_rest "command=QueryDNSZoneRRList&dnszone=${h}."; then |
ccc2142b SP |
130 | return 1 |
131 | fi | |
132 | ||
133 | if _contains "$response" "CODE=200"; then | |
134 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
135 | _domain=$h | |
136 | return 0 | |
137 | fi | |
138 | p=$i | |
139 | i=$(_math "$i" + 1) | |
140 | done | |
141 | return 1 | |
142 | } | |
143 | ||
144 | _hexonet_rest() { | |
145 | query_params="$1" | |
146 | _debug "$query_params" | |
147 | ||
93d29a97 | 148 | response="$(_get "${Hexonet_Api}?s_login=${Hexonet_Login}&s_pw=${Hexonet_Password}&${query_params}")" |
ccc2142b SP |
149 | |
150 | if [ "$?" != "0" ]; then | |
151 | _err "error $query_params" | |
152 | return 1 | |
153 | fi | |
154 | _debug2 response "$response" | |
155 | return 0 | |
156 | } |