4 #https://doc.powerdns.com/md/httpapi/api_spec/
6 #PDNS_Url="http://ns.example.com:8081"
7 #PDNS_ServerId="localhost"
8 #PDNS_Token="0123456789ABCDEF"
13 ######## Public functions #####################
14 #Usage: add _acme-challenge.www.domain.com "123456789ABCDEF0000000000000000000000000000000000000"
21 if [ -z "$PDNS_Url" ]; then
23 _err
"You don't specify PowerDNS address."
24 _err
"Please set PDNS_Url and try again."
28 if [ -z "$PDNS_ServerId" ]; then
30 _err
"You don't specify PowerDNS server id."
31 _err
"Please set you PDNS_ServerId and try again."
35 if [ -z "$PDNS_Token" ]; then
37 _err
"You don't specify PowerDNS token."
38 _err
"Please create you PDNS_Token and try again."
42 if [ -z "$PDNS_Ttl" ]; then
43 PDNS_Ttl
="$DEFAULT_PDNS_TTL"
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"
51 if [ "$PDNS_Ttl" != "$DEFAULT_PDNS_TTL" ]; then
52 _saveaccountconf PDNS_Ttl
"$PDNS_Ttl"
55 _debug
"Detect root zone"
56 if ! _get_root
"$fulldomain"; then
60 _debug _domain
"$_domain"
62 if ! set_record
"$_domain" "$fulldomain" "$txtvalue"; then
74 if [ -z "$PDNS_Ttl" ]; then
75 PDNS_Ttl
="$DEFAULT_PDNS_TTL"
78 _debug
"Detect root zone"
79 if ! _get_root
"$fulldomain"; then
84 _debug _domain
"$_domain"
86 if ! rm_record
"$_domain" "$fulldomain" "$txtvalue"; then
100 _build_record_string
"$new_challenge"
101 _list_existingchallenges
102 for oldchallenge
in $_existing_challenges; do
103 _build_record_string
"$oldchallenge"
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]}]}"; then
107 _err
"Set txt record error."
111 if ! notify_slaves
"$root"; then
119 _info
"Remove record"
124 #Enumerate existing acme challenges
125 _list_existingchallenges
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\"}]}"; then
130 _err
"Delete txt record error."
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"
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]}]}"; then
144 _err
"Set txt record error."
148 if ! notify_slaves
"$root"; then
152 _info
"Record not found, nothing to remove"
161 if ! _pdns_rest
"PUT" "/api/v1/servers/$PDNS_ServerId/zones/$root/notify"; then
162 _err
"Notify slaves error."
169 #################### Private functions below ##################################
170 #_acme-challenge.www.domain.com
177 if _pdns_rest
"GET" "/api/v1/servers/$PDNS_ServerId/zones"; then
178 _zones_response
="$response"
182 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
184 if _contains
"$_zones_response" "\"name\": \"$h.\""; then
197 _debug
"$domain not found"
207 export _H1
="X-API-Key: $PDNS_Token"
209 if [ ! "$method" = "GET" ]; then
211 response
="$(_post "$data" "$PDNS_Url$ep" "" "$method")"
213 response
="$(_get "$PDNS_Url$ep")"
216 if [ "$?" != "0" ]; then
220 _debug2 response
"$response"
225 _build_record_string
() {
226 _record_string
="${_record_string:+${_record_string}, }{\"content\": \"\\\"${1}\\\"\", \"disabled\": false}"
229 _list_existingchallenges
() {
230 _pdns_rest
"GET" "/api/v1/servers/$PDNS_ServerId/zones/$root"
231 _existing_challenges
=$
(echo "$response" | _normalizeJson | _egrep_o
"\"name\":\"${fulldomain}[^]]*}" | _egrep_o
'content\":\"\\"[^\\]*' |
sed -n 's/^content":"\\"//p')