]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_anx.sh
Merge pull request #4658 from Justman10000/master
[mirror_acme.sh.git] / dnsapi / dns_anx.sh
1 #!/usr/bin/env sh
2
3 # Anexia CloudDNS acme.sh hook
4 # Author: MA
5
6 #ANX_Token="xxxx"
7
8 ANX_API='https://engine.anexia-it.com/api/clouddns/v1'
9
10 ######## Public functions #####################
11
12 dns_anx_add() {
13 fulldomain=$1
14 txtvalue=$2
15
16 _info "Using ANX CDNS API"
17
18 ANX_Token="${ANX_Token:-$(_readaccountconf_mutable ANX_Token)}"
19 _debug fulldomain "$fulldomain"
20 _debug txtvalue "$txtvalue"
21
22 if [ "$ANX_Token" ]; then
23 _saveaccountconf_mutable ANX_Token "$ANX_Token"
24 else
25 _err "You didn't specify a ANEXIA Engine API token."
26 return 1
27 fi
28
29 _debug "First detect the root zone"
30 if ! _get_root "$fulldomain"; then
31 _err "invalid domain"
32 return 1
33 fi
34
35 # Always add records, wildcard need two records with the same name
36 _anx_rest POST "zone.json/${_domain}/records" "{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"rdata\":\"$txtvalue\"}"
37 if _contains "$response" "$txtvalue"; then
38 return 0
39 else
40 return 1
41 fi
42 }
43
44 dns_anx_rm() {
45 fulldomain=$1
46 txtvalue=$2
47
48 _info "Using ANX CDNS API"
49
50 ANX_Token="${ANX_Token:-$(_readaccountconf_mutable ANX_Token)}"
51
52 _debug fulldomain "$fulldomain"
53 _debug txtvalue "$txtvalue"
54
55 _debug "First detect the root zone"
56 if ! _get_root "$fulldomain"; then
57 _err "invalid domain"
58 return 1
59 fi
60
61 _get_record_id
62
63 if _is_uuid "$_record_id"; then
64 if ! _anx_rest DELETE "zone.json/${_domain}/records/$_record_id"; then
65 _err "Delete record"
66 return 1
67 fi
68 else
69 _info "No record found."
70 fi
71 echo "$response" | tr -d " " | grep \"status\":\"OK\" >/dev/null
72 }
73
74 #################### Private functions below ##################################
75
76 _is_uuid() {
77 pattern='^\{?[A-Z0-9a-z]{8}-[A-Z0-9a-z]{4}-[A-Z0-9a-z]{4}-[A-Z0-9a-z]{4}-[A-Z0-9a-z]{12}\}?$'
78 if echo "$1" | _egrep_o "$pattern" >/dev/null; then
79 return 0
80 fi
81 return 1
82 }
83
84 _get_record_id() {
85 _debug subdomain "$_sub_domain"
86 _debug domain "$_domain"
87
88 if _anx_rest GET "zone.json/${_domain}/records?name=$_sub_domain&type=TXT"; then
89 _debug response "$response"
90 if _contains "$response" "\"name\":\"$_sub_domain\"" >/dev/null; then
91 _record_id=$(printf "%s\n" "$response" | _egrep_o "\[.\"identifier\":\"[^\"]*\"" | head -n 1 | cut -d : -f 2 | tr -d \")
92 else
93 _record_id=''
94 fi
95 else
96 _err "Search existing record"
97 fi
98 }
99
100 _anx_rest() {
101 m=$1
102 ep="$2"
103 data="$3"
104 _debug "$ep"
105
106 export _H1="Content-Type: application/json"
107 export _H2="Authorization: Token $ANX_Token"
108
109 if [ "$m" != "GET" ]; then
110 _debug data "$data"
111 response="$(_post "$data" "${ANX_API}/$ep" "" "$m")"
112 else
113 response="$(_get "${ANX_API}/$ep")"
114 fi
115
116 # shellcheck disable=SC2181
117 if [ "$?" != "0" ]; then
118 _err "error $ep"
119 return 1
120 fi
121 _debug response "$response"
122 return 0
123 }
124
125 _get_root() {
126 domain=$1
127 i=1
128 p=1
129
130 _anx_rest GET "zone.json"
131
132 while true; do
133 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
134 _debug h "$h"
135 if [ -z "$h" ]; then
136 #not valid
137 return 1
138 fi
139
140 if _contains "$response" "\"name\":\"$h\""; then
141 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
142 _domain=$h
143 return 0
144 fi
145
146 p=$i
147 i=$(_math "$i" + 1)
148 done
149 return 1
150 }