2016-10-09 15:56:04 +02:00
|
|
|
#!/usr/bin/env sh
|
2023-11-18 17:57:12 +01:00
|
|
|
# shellcheck disable=SC2034
|
|
|
|
dns_pdns_info='PowerDNS Server API
|
|
|
|
Site: PowerDNS.com
|
|
|
|
Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_pdns
|
|
|
|
Options:
|
|
|
|
PDNS_Url API URL. E.g. "http://ns.example.com:8081"
|
|
|
|
PDNS_ServerId Server ID. E.g. "localhost"
|
|
|
|
PDNS_Token API Token
|
|
|
|
PDNS_Ttl=60 Domain TTL. Default: "60".
|
|
|
|
'
|
2016-10-09 15:56:04 +02:00
|
|
|
|
2016-10-09 16:15:15 +02:00
|
|
|
DEFAULT_PDNS_TTL=60
|
|
|
|
|
2016-10-09 15:56:04 +02:00
|
|
|
######## Public functions #####################
|
|
|
|
#Usage: add _acme-challenge.www.domain.com "123456789ABCDEF0000000000000000000000000000000000000"
|
2016-11-17 12:52:00 +01:00
|
|
|
#fulldomain
|
|
|
|
#txtvalue
|
2016-10-09 15:56:04 +02:00
|
|
|
dns_pdns_add() {
|
|
|
|
fulldomain=$1
|
|
|
|
txtvalue=$2
|
|
|
|
|
2016-11-09 12:30:39 +01:00
|
|
|
if [ -z "$PDNS_Url" ]; then
|
2016-11-12 04:13:40 +01:00
|
|
|
PDNS_Url=""
|
2016-10-09 15:56:04 +02:00
|
|
|
_err "You don't specify PowerDNS address."
|
|
|
|
_err "Please set PDNS_Url and try again."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2016-11-09 12:30:39 +01:00
|
|
|
if [ -z "$PDNS_ServerId" ]; then
|
2016-11-12 04:13:40 +01:00
|
|
|
PDNS_ServerId=""
|
2016-10-09 15:56:04 +02:00
|
|
|
_err "You don't specify PowerDNS server id."
|
|
|
|
_err "Please set you PDNS_ServerId and try again."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2016-11-09 12:30:39 +01:00
|
|
|
if [ -z "$PDNS_Token" ]; then
|
2016-11-12 04:13:40 +01:00
|
|
|
PDNS_Token=""
|
2016-10-09 15:56:04 +02:00
|
|
|
_err "You don't specify PowerDNS token."
|
|
|
|
_err "Please create you PDNS_Token and try again."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2016-11-09 12:30:39 +01:00
|
|
|
if [ -z "$PDNS_Ttl" ]; then
|
2016-11-11 16:30:14 +01:00
|
|
|
PDNS_Ttl="$DEFAULT_PDNS_TTL"
|
2016-10-09 15:56:04 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
#save the api addr and key to the account conf file.
|
|
|
|
_saveaccountconf PDNS_Url "$PDNS_Url"
|
|
|
|
_saveaccountconf PDNS_ServerId "$PDNS_ServerId"
|
|
|
|
_saveaccountconf PDNS_Token "$PDNS_Token"
|
2016-11-09 12:30:39 +01:00
|
|
|
|
|
|
|
if [ "$PDNS_Ttl" != "$DEFAULT_PDNS_TTL" ]; then
|
2016-10-09 16:15:15 +02:00
|
|
|
_saveaccountconf PDNS_Ttl "$PDNS_Ttl"
|
|
|
|
fi
|
2016-10-09 15:56:04 +02:00
|
|
|
|
2016-11-17 12:52:00 +01:00
|
|
|
_debug "Detect root zone"
|
2016-11-11 16:30:14 +01:00
|
|
|
if ! _get_root "$fulldomain"; then
|
2016-10-09 15:56:04 +02:00
|
|
|
_err "invalid domain"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
_debug _domain "$_domain"
|
|
|
|
|
2016-11-09 12:30:39 +01:00
|
|
|
if ! set_record "$_domain" "$fulldomain" "$txtvalue"; then
|
2016-10-09 15:56:04 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:08:02 +02:00
|
|
|
#fulldomain
|
|
|
|
dns_pdns_rm() {
|
|
|
|
fulldomain=$1
|
2018-03-24 18:46:04 +01:00
|
|
|
txtvalue=$2
|
|
|
|
|
|
|
|
if [ -z "$PDNS_Ttl" ]; then
|
|
|
|
PDNS_Ttl="$DEFAULT_PDNS_TTL"
|
|
|
|
fi
|
2016-10-25 17:08:02 +02:00
|
|
|
|
2016-11-17 12:52:00 +01:00
|
|
|
_debug "Detect root zone"
|
|
|
|
if ! _get_root "$fulldomain"; then
|
|
|
|
_err "invalid domain"
|
|
|
|
return 1
|
|
|
|
fi
|
2018-03-24 18:46:04 +01:00
|
|
|
|
2016-11-17 12:52:00 +01:00
|
|
|
_debug _domain "$_domain"
|
|
|
|
|
2018-03-24 18:46:04 +01:00
|
|
|
if ! rm_record "$_domain" "$fulldomain" "$txtvalue"; then
|
2016-11-17 12:52:00 +01:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
2016-10-25 17:08:02 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 15:56:04 +02:00
|
|
|
set_record() {
|
|
|
|
_info "Adding record"
|
|
|
|
root=$1
|
|
|
|
full=$2
|
2018-03-21 16:43:42 +01:00
|
|
|
new_challenge=$3
|
2016-10-09 15:56:04 +02:00
|
|
|
|
2018-03-21 16:43:42 +01:00
|
|
|
_record_string=""
|
2018-03-22 11:13:46 +01:00
|
|
|
_build_record_string "$new_challenge"
|
2018-03-24 18:46:04 +01:00
|
|
|
_list_existingchallenges
|
2018-03-22 11:13:46 +01:00
|
|
|
for oldchallenge in $_existing_challenges; do
|
|
|
|
_build_record_string "$oldchallenge"
|
2018-03-21 16:43:42 +01:00
|
|
|
done
|
|
|
|
|
2021-06-25 10:12:23 +02:00
|
|
|
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
|
2016-10-09 15:56:04 +02:00
|
|
|
_err "Set txt record error."
|
|
|
|
return 1
|
|
|
|
fi
|
2016-11-17 12:52:00 +01:00
|
|
|
|
2018-03-24 18:46:04 +01:00
|
|
|
if ! notify_slaves "$root"; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2016-11-17 12:52:00 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
rm_record() {
|
|
|
|
_info "Remove record"
|
|
|
|
root=$1
|
|
|
|
full=$2
|
2018-03-24 18:46:04 +01:00
|
|
|
txtvalue=$3
|
2016-11-17 12:52:00 +01:00
|
|
|
|
2018-03-24 18:46:04 +01:00
|
|
|
#Enumerate existing acme challenges
|
|
|
|
_list_existingchallenges
|
2016-11-17 12:52:00 +01:00
|
|
|
|
2018-03-24 18:46:04 +01:00
|
|
|
if _contains "$_existing_challenges" "$txtvalue"; then
|
|
|
|
#Delete all challenges (PowerDNS API does not allow to delete content)
|
2021-06-25 10:12:23 +02:00
|
|
|
if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root" "{\"rrsets\": [{\"changetype\": \"DELETE\", \"name\": \"$full.\", \"type\": \"TXT\"}]}" "application/json"; then
|
2018-03-24 18:46:04 +01:00
|
|
|
_err "Delete txt record error."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
_record_string=""
|
|
|
|
#If the only existing challenge was the challenge to delete: nothing to do
|
|
|
|
if ! [ "$_existing_challenges" = "$txtvalue" ]; then
|
|
|
|
for oldchallenge in $_existing_challenges; do
|
|
|
|
#Build up the challenges to re-add, ommitting the one what should be deleted
|
|
|
|
if ! [ "$oldchallenge" = "$txtvalue" ]; then
|
|
|
|
_build_record_string "$oldchallenge"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
#Recreate the existing challenges
|
2021-06-25 10:12:23 +02:00
|
|
|
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
|
2018-03-24 18:46:04 +01:00
|
|
|
_err "Set txt record error."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if ! notify_slaves "$root"; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
_info "Record not found, nothing to remove"
|
2016-11-17 12:52:00 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
notify_slaves() {
|
|
|
|
root=$1
|
|
|
|
|
2018-03-15 02:38:17 +01:00
|
|
|
if ! _pdns_rest "PUT" "/api/v1/servers/$PDNS_ServerId/zones/$root/notify"; then
|
2016-11-17 12:52:00 +01:00
|
|
|
_err "Notify slaves error."
|
2016-10-09 15:56:04 +02:00
|
|
|
return 1
|
|
|
|
fi
|
2016-11-17 12:52:00 +01:00
|
|
|
|
2016-10-09 15:56:04 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-12-14 21:32:24 +01:00
|
|
|
#################### Private functions below ##################################
|
2016-10-09 15:56:04 +02:00
|
|
|
#_acme-challenge.www.domain.com
|
|
|
|
#returns
|
|
|
|
# _domain=domain.com
|
|
|
|
_get_root() {
|
|
|
|
domain=$1
|
|
|
|
i=1
|
|
|
|
|
2016-11-09 12:30:39 +01:00
|
|
|
if _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones"; then
|
2021-02-11 11:20:18 +01:00
|
|
|
_zones_response=$(echo "$response" | _normalizeJson)
|
2016-10-09 15:56:04 +02:00
|
|
|
fi
|
|
|
|
|
2016-11-11 16:30:14 +01:00
|
|
|
while true; do
|
2024-10-13 17:41:22 +02:00
|
|
|
h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
|
2016-10-09 15:56:04 +02:00
|
|
|
|
2021-02-11 11:20:18 +01:00
|
|
|
if _contains "$_zones_response" "\"name\":\"$h.\""; then
|
2018-03-15 02:38:17 +01:00
|
|
|
_domain="$h."
|
|
|
|
if [ -z "$h" ]; then
|
|
|
|
_domain="=2E"
|
|
|
|
fi
|
2016-10-09 15:56:04 +02:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2018-03-15 02:38:17 +01:00
|
|
|
if [ -z "$h" ]; then
|
|
|
|
return 1
|
|
|
|
fi
|
2016-11-11 16:30:14 +01:00
|
|
|
i=$(_math $i + 1)
|
2016-10-09 15:56:04 +02:00
|
|
|
done
|
|
|
|
_debug "$domain not found"
|
2016-11-17 12:52:00 +01:00
|
|
|
|
2016-10-09 15:56:04 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
_pdns_rest() {
|
|
|
|
method=$1
|
|
|
|
ep=$2
|
|
|
|
data=$3
|
2021-06-25 10:12:23 +02:00
|
|
|
ct=$4
|
2016-10-09 15:56:04 +02:00
|
|
|
|
2017-01-09 17:04:09 +01:00
|
|
|
export _H1="X-API-Key: $PDNS_Token"
|
2016-10-09 15:56:04 +02:00
|
|
|
|
2016-11-09 12:30:39 +01:00
|
|
|
if [ ! "$method" = "GET" ]; then
|
2016-10-09 15:56:04 +02:00
|
|
|
_debug data "$data"
|
2021-06-25 10:12:23 +02:00
|
|
|
response="$(_post "$data" "$PDNS_Url$ep" "" "$method" "$ct")"
|
2016-10-09 15:56:04 +02:00
|
|
|
else
|
|
|
|
response="$(_get "$PDNS_Url$ep")"
|
|
|
|
fi
|
|
|
|
|
2016-11-09 12:30:39 +01:00
|
|
|
if [ "$?" != "0" ]; then
|
2016-10-09 15:56:04 +02:00
|
|
|
_err "error $ep"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
_debug2 response "$response"
|
|
|
|
|
|
|
|
return 0
|
2016-11-09 12:30:39 +01:00
|
|
|
}
|
2018-03-21 16:43:42 +01:00
|
|
|
|
|
|
|
_build_record_string() {
|
2018-03-24 18:46:04 +01:00
|
|
|
_record_string="${_record_string:+${_record_string}, }{\"content\": \"\\\"${1}\\\"\", \"disabled\": false}"
|
|
|
|
}
|
|
|
|
|
|
|
|
_list_existingchallenges() {
|
|
|
|
_pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones/$root"
|
|
|
|
_existing_challenges=$(echo "$response" | _normalizeJson | _egrep_o "\"name\":\"${fulldomain}[^]]*}" | _egrep_o 'content\":\"\\"[^\\]*' | sed -n 's/^content":"\\"//p')
|
2018-03-21 16:43:42 +01:00
|
|
|
}
|