]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_pdns.sh
Merge pull request #4531 from NCDGHA/bugfix/issue_4530_fix_http_status_503
[mirror_acme.sh.git] / dnsapi / dns_pdns.sh
1 #!/usr/bin/env sh
2
3 #PowerDNS Embedded API
4 #https://doc.powerdns.com/md/httpapi/api_spec/
5 #
6 #PDNS_Url="http://ns.example.com:8081"
7 #PDNS_ServerId="localhost"
8 #PDNS_Token="0123456789ABCDEF"
9 #PDNS_Ttl=60
10
11 DEFAULT_PDNS_TTL=60
12
13 ######## Public functions #####################
14 #Usage: add _acme-challenge.www.domain.com "123456789ABCDEF0000000000000000000000000000000000000"
15 #fulldomain
16 #txtvalue
17 dns_pdns_add() {
18 fulldomain=$1
19 txtvalue=$2
20
21 if [ -z "$PDNS_Url" ]; then
22 PDNS_Url=""
23 _err "You don't specify PowerDNS address."
24 _err "Please set PDNS_Url and try again."
25 return 1
26 fi
27
28 if [ -z "$PDNS_ServerId" ]; then
29 PDNS_ServerId=""
30 _err "You don't specify PowerDNS server id."
31 _err "Please set you PDNS_ServerId and try again."
32 return 1
33 fi
34
35 if [ -z "$PDNS_Token" ]; then
36 PDNS_Token=""
37 _err "You don't specify PowerDNS token."
38 _err "Please create you PDNS_Token and try again."
39 return 1
40 fi
41
42 if [ -z "$PDNS_Ttl" ]; then
43 PDNS_Ttl="$DEFAULT_PDNS_TTL"
44 fi
45
46 #save the api addr and key to the account conf file.
47 _saveaccountconf PDNS_Url "$PDNS_Url"
48 _saveaccountconf PDNS_ServerId "$PDNS_ServerId"
49 _saveaccountconf PDNS_Token "$PDNS_Token"
50
51 if [ "$PDNS_Ttl" != "$DEFAULT_PDNS_TTL" ]; then
52 _saveaccountconf PDNS_Ttl "$PDNS_Ttl"
53 fi
54
55 _debug "Detect root zone"
56 if ! _get_root "$fulldomain"; then
57 _err "invalid domain"
58 return 1
59 fi
60 _debug _domain "$_domain"
61
62 if ! set_record "$_domain" "$fulldomain" "$txtvalue"; then
63 return 1
64 fi
65
66 return 0
67 }
68
69 #fulldomain
70 dns_pdns_rm() {
71 fulldomain=$1
72 txtvalue=$2
73
74 if [ -z "$PDNS_Ttl" ]; then
75 PDNS_Ttl="$DEFAULT_PDNS_TTL"
76 fi
77
78 _debug "Detect root zone"
79 if ! _get_root "$fulldomain"; then
80 _err "invalid domain"
81 return 1
82 fi
83
84 _debug _domain "$_domain"
85
86 if ! rm_record "$_domain" "$fulldomain" "$txtvalue"; then
87 return 1
88 fi
89
90 return 0
91 }
92
93 set_record() {
94 _info "Adding record"
95 root=$1
96 full=$2
97 new_challenge=$3
98
99 _record_string=""
100 _build_record_string "$new_challenge"
101 _list_existingchallenges
102 for oldchallenge in $_existing_challenges; do
103 _build_record_string "$oldchallenge"
104 done
105
106 if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root" "{\"rrsets\": [{\"changetype\": \"REPLACE\", \"name\": \"$full.\", \"type\": \"TXT\", \"ttl\": $PDNS_Ttl, \"records\": [$_record_string]}]}" "application/json"; then
107 _err "Set txt record error."
108 return 1
109 fi
110
111 if ! notify_slaves "$root"; then
112 return 1
113 fi
114
115 return 0
116 }
117
118 rm_record() {
119 _info "Remove record"
120 root=$1
121 full=$2
122 txtvalue=$3
123
124 #Enumerate existing acme challenges
125 _list_existingchallenges
126
127 if _contains "$_existing_challenges" "$txtvalue"; then
128 #Delete all challenges (PowerDNS API does not allow to delete content)
129 if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root" "{\"rrsets\": [{\"changetype\": \"DELETE\", \"name\": \"$full.\", \"type\": \"TXT\"}]}" "application/json"; then
130 _err "Delete txt record error."
131 return 1
132 fi
133 _record_string=""
134 #If the only existing challenge was the challenge to delete: nothing to do
135 if ! [ "$_existing_challenges" = "$txtvalue" ]; then
136 for oldchallenge in $_existing_challenges; do
137 #Build up the challenges to re-add, ommitting the one what should be deleted
138 if ! [ "$oldchallenge" = "$txtvalue" ]; then
139 _build_record_string "$oldchallenge"
140 fi
141 done
142 #Recreate the existing challenges
143 if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root" "{\"rrsets\": [{\"changetype\": \"REPLACE\", \"name\": \"$full.\", \"type\": \"TXT\", \"ttl\": $PDNS_Ttl, \"records\": [$_record_string]}]}" "application/json"; then
144 _err "Set txt record error."
145 return 1
146 fi
147 fi
148 if ! notify_slaves "$root"; then
149 return 1
150 fi
151 else
152 _info "Record not found, nothing to remove"
153 fi
154
155 return 0
156 }
157
158 notify_slaves() {
159 root=$1
160
161 if ! _pdns_rest "PUT" "/api/v1/servers/$PDNS_ServerId/zones/$root/notify"; then
162 _err "Notify slaves error."
163 return 1
164 fi
165
166 return 0
167 }
168
169 #################### Private functions below ##################################
170 #_acme-challenge.www.domain.com
171 #returns
172 # _domain=domain.com
173 _get_root() {
174 domain=$1
175 i=1
176
177 if _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones"; then
178 _zones_response=$(echo "$response" | _normalizeJson)
179 fi
180
181 while true; do
182 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
183
184 if _contains "$_zones_response" "\"name\":\"$h.\""; then
185 _domain="$h."
186 if [ -z "$h" ]; then
187 _domain="=2E"
188 fi
189 return 0
190 fi
191
192 if [ -z "$h" ]; then
193 return 1
194 fi
195 i=$(_math $i + 1)
196 done
197 _debug "$domain not found"
198
199 return 1
200 }
201
202 _pdns_rest() {
203 method=$1
204 ep=$2
205 data=$3
206 ct=$4
207
208 export _H1="X-API-Key: $PDNS_Token"
209
210 if [ ! "$method" = "GET" ]; then
211 _debug data "$data"
212 response="$(_post "$data" "$PDNS_Url$ep" "" "$method" "$ct")"
213 else
214 response="$(_get "$PDNS_Url$ep")"
215 fi
216
217 if [ "$?" != "0" ]; then
218 _err "error $ep"
219 return 1
220 fi
221 _debug2 response "$response"
222
223 return 0
224 }
225
226 _build_record_string() {
227 _record_string="${_record_string:+${_record_string}, }{\"content\": \"\\\"${1}\\\"\", \"disabled\": false}"
228 }
229
230 _list_existingchallenges() {
231 _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones/$root"
232 _existing_challenges=$(echo "$response" | _normalizeJson | _egrep_o "\"name\":\"${fulldomain}[^]]*}" | _egrep_o 'content\":\"\\"[^\\]*' | sed -n 's/^content":"\\"//p')
233 }