@ -44,18 +44,21 @@ NEWLINE='\
#################### API Templates ##################################
pleskxml_tplt_get_domains="<packet><customer><get-domain-list><filter/></get-domain-list></customer></packet>"
# Get a list of domains that PLESK can manage, so we can check root domain + host for acme.sh
# Also used to test credentials and URI.
# No args.
# Get a list of domains that PLESK can manage, so we can check root domain + host for acme.sh
# Also used to test credentials and URI.
# No args.
pleskxml_tplt_get_dns_records="<packet><dns><get_rec><filter><site-id>%s</site-id></filter></get_rec></dns></packet>"
# Get all DNS records for a Plesk domain ID.
# ARG = Plesk domain id to query
# Get all DNS records for a Plesk domain ID.
# ARG = Plesk domain id to query
pleskxml_tplt_add_txt_record="<packet><dns><add_rec><site-id>%s</site-id><type>TXT</type><host>%s</host><value>%s</value></add_rec></dns></packet>"
# Add a TXT record to a domain.
# ARGS = (1) Plesk internal domain ID, (2) "hostname" for the new record, eg '_acme_challenge', (3) TXT record value
# Add a TXT record to a domain.
# ARGS = (1) Plesk internal domain ID, (2) "hostname" for the new record, eg '_acme_challenge', (3) TXT record value
pleskxml_tplt_rmv_dns_record="<packet><dns><del_rec><filter><id>%s</id></filter></del_rec></dns></packet>"
# Add a TXT record to a domain.
# ARG = the Plesk internal ID for the dns record to be deleted
# Add a TXT record to a domain.
# ARG = the Plesk internal ID for the dns record to be deleted
#################### Public functions ##################################
@ -82,7 +85,7 @@ dns_pleskxml_add() {
# printf using template in a variable - not a style issue
# shellcheck disable=SC2059
request="$( printf "$pleskxml_tplt_add_txt_record" "$root_domain_id" "$sub_domain_name" "$txtvalue" )"
request="$(printf "$pleskxml_tplt_add_txt_record" "$root_domain_id" "$sub_domain_name" "$txtvalue")"
if ! _call_api "$request"; then
return 1
fi
@ -90,7 +93,7 @@ dns_pleskxml_add() {
# OK, we should have added a TXT record. Let's check and return success if so.
# All that should be left in the result, is one section, containing <result><status>ok</status><id>NEW_DNS_RECORD_ID</id></result>
results="$( _api_response_split "$pleskxml_prettyprint_result" 'result' '<status>' )"
results="$(_api_response_split "$pleskxml_prettyprint_result" 'result' '<status>')"
if ! _value "$results" | grep '<status>ok</status>' | grep -qE '<id>[0-9]+</id>'; then
# Error - doesn't contain expected string. Something's wrong.
@ -101,7 +104,7 @@ dns_pleskxml_add() {
return 1
fi
recid="$( _value "$results" | grep -E '<id>[0-9]+</id>' | sed -E 's/^.*<id>([0-9]+)<\/id>.*$/\1/' )"
recid="$(_value "$results" | grep -E '<id>[0-9]+</id>' | sed -E 's/^.*<id>([0-9]+)<\/id>.*$/\1/')"
_info "Success. TXT record appears to be correctly added (Plesk record ID=$recid). Exiting dns_pleskxml_add()."
@ -129,16 +132,16 @@ dns_pleskxml_rm() {
# printf using template in a variable - not a style issue
# shellcheck disable=SC2059
request="$( printf "$pleskxml_tplt_get_dns_records" "$root_domain_id" )"
request="$(printf "$pleskxml_tplt_get_dns_records" "$root_domain_id")"
if ! _call_api "$request"; then
return 1
fi
# Reduce output to one line per DNS record, filtered for TXT records with a record ID only (which they should all have)
reclist="$( _api_response_split "$pleskxml_prettyprint_result" 'result' '<status>ok</status>' | \
grep "<site-id>${root_domain_id}</site-id>" | \
grep -E '<id>[0-9]+</id>' | \
grep '<type>TXT</type>' \
reclist="$(_api_response_split "$pleskxml_prettyprint_result" 'result' '<status>ok</status>' \
| grep "<site-id>${root_domain_id}</site-id>" \
| grep -E '<id>[0-9]+</id>' \
| grep '<type>TXT</type>' \
)"
if [ -z "$reclist" ]; then
@ -148,14 +151,13 @@ dns_pleskxml_rm() {
_debug "Got list of DNS TXT records for root domain '$root_domain_name'"':\n'"$reclist"
recid="$( _value "$reclist" | \
grep "<host>$1.</host>" | \
grep "<value>$txtvalue</value>" | \
sed -E 's/(^.*<id>|<\/id>.*$)//g' \
recid="$(_value "$reclist" \
| grep "<host>$1.</host>" \
| grep "<value>$txtvalue</value>" \
| sed -E 's/(^.*<id>|<\/id>.*$)//g' \
)"
_debug "List of DNS TXT records for host:"'\n'"$( _value "$reclist" | grep "<host>$1.</host>" )"
_debug "List of DNS TXT records for host:"'\n'"$(_value "$reclist" | grep "<host>$1.</host>")"
if ! _value "$recid" | grep -Eq '^[0-9]+$'; then
_err "DNS records for root domain '${root_domain_name}' (Plesk ID ${root_domain_id}) + host '${sub_domain_name}' do not contain the TXT record '${txtvalue}'"
@ -168,7 +170,7 @@ dns_pleskxml_rm() {
# printf using template in a variable - not a style issue
# shellcheck disable=SC2059
request="$( printf "$pleskxml_tplt_rmv_dns_record" "$recid" )"
request="$(printf "$pleskxml_tplt_rmv_dns_record" "$recid")"
if ! _call_api "$request"; then
return 1
fi
@ -176,7 +178,7 @@ dns_pleskxml_rm() {
# OK, we should have removed a TXT record. Let's check and return success if so.
# All that should be left in the result, is one section, containing <result><status>ok</status><id>PLESK_DELETED_DNS_RECORD_ID</id></result>
results="$( _api_response_split "$pleskxml_prettyprint_result" 'result' '<status>' )"
results="$(_api_response_split "$pleskxml_prettyprint_result" 'result' '<status>')"
if ! _value "$results" | grep '<status>ok</status>' | grep -qE '<id>[0-9]+</id>'; then
# Error - doesn't contain expected string. Something's wrong.
@ -214,11 +216,11 @@ _valuecut() {
# $2 - tag to resplit on (usually "result" or "domain")
# $3 - regex to recognise useful return lines
_api_response_split() {
printf '%s' "$1" | \
sed -E 's/(^[[:space:]]+|[[:space:]]+$)//g' | \
tr -d '\n\r' | \
sed -E "s/<\/?$2>/${NEWLINE}/g" | \
grep -E "$3"
printf '%s' "$1" \
| sed -E 's/(^[[:space:]]+|[[:space:]]+$)//g' \
| tr -d '\n\r' \
| sed -E "s/<\/?$2>/${NEWLINE}/g" \
| grep -E "$3"
}
@ -242,16 +244,15 @@ _call_api() {
# Detect any <status> that isn't "ok". None of the used calls should fail if the API is working correctly.
# Also detect if there simply aren't any status lines (null result?) and report that, as well.
statuslines="$( echo "$pleskxml_prettyprint_result" | grep -E '^[[:space:]]*<status>[^<]*</status>[[:space:]]*$' )"
statuslines="$(echo "$pleskxml_prettyprint_result" | grep -E '^[[:space:]]*<status>[^<]*</status>[[:space:]]*$')"
if _value "$statuslines" | grep -qv '<status>ok</status>'; then
# We have some status lines that aren't "ok". Get the details
errtext="$( \
_value "$pleskxml_prettyprint_result" | \
grep -iE "(<status>|<errcode>|<errtext>)" | \
sed -E 's/(^[[:space:]]+|<\/[a-z]+$)//g' | \
sed -E 's/^<([a-z]+)>/\1: /' \
errtext="$( _value "$pleskxml_prettyprint_result" \
| grep -iE "(<status>|<errcode>|<errtext>)" \
| sed -E 's/(^[[:space:]]+|<\/[a-z]+$)//g' \
| sed -E 's/^<([a-z]+)>/\1: /' \
)"
elif ! _value "$statuslines" | grep -q '<status>ok</status>'; then
@ -326,14 +327,17 @@ _credential_check() {
# For a FQDN, identify the root domain managed by Plesk, its domain ID in Plesk, and the host if any.
# IMPORTANT NOTE: a result with host = empty string is OK for this API, see
# https://docs.plesk.com/en-US/obsidian/api-rpc/about-xml-api/reference/managing-dns/managing-dns-records/adding-dns-record.34798
# See notes at top of this file
_pleskxml_get_root_domain() {
_debug "Identifying DNS root domain for '$1' that is managed by the Plesk account."
# test if the domain is valid for splitting.
if _value "$root_domain_name" | grep -qvE '^[^.]+\.[^.]+\.[^.]'; then
### COMMENTED OUT ALSO FOR SAME REASON
### _err "Invalid domain. The ACME domain must contain at least three parts (aa.bb.tld) to identify a host, domain, and tld for the TXT record."
_err "Invalid domain. The ACME domain must contain at least two parts (aa.bb) to identify a domain and tld for the TXT record."
return 1
fi
@ -350,7 +354,8 @@ _pleskxml_get_root_domain() {
# for non-Western character sets.
# Output will be one line per known domain, containing 1 or 2 <name> tages and an <id> tag
# We don't actually need to check for type, name, *and* id, but it guarantees only usable lines are returned.
output="$( _api_response_split "$pleskxml_prettyprint_result" 'domain' '<type>domain</type>' | sed -E 's/<(\/?)ascii-name>/<\1name>/g' | grep '<name>' | grep '<id>' )"
output="$(_api_response_split "$pleskxml_prettyprint_result" 'domain' '<type>domain</type>' | sed -E 's/<(\/?)ascii-name>/<\1name>/g' | grep '<name>' | grep '<id>')"
_debug 'Domains managed by Plesk server are (ignore the hacked output):\n' "$output"
@ -364,14 +369,13 @@ _pleskxml_get_root_domain() {
_debug "Checking if '$root_domain_name' is managed by the Plesk server..."
root_domain_id="$( _value "$output" | grep "<name>$root_domain_name</name>" | _head_n 1 | sed -E 's/^.*<id>([0-9]+)<\/id>.*$/\1/' )"
root_domain_id="$(_value "$output" | grep "<name>$root_domain_name</name>" | _head_n 1 | sed -E 's/^.*<id>([0-9]+)<\/id>.*$/\1/')"
if [ -n "$root_domain_id" ]; then
# Found a match
# Note that a result with host = empty string is OK for this API, see
# https://docs.plesk.com/en-US/obsidian/api-rpc/about-xml-api/reference/managing-dns/managing-dns-records/adding-dns-record.34798
# See notes at top of this file
sub_domain_name="$( _value "$1" | sed -E "s/\.?${root_domain_name}"'$//' )"
# SEE IMPORTANT NOTE ABOVE - THIS FUNCTION CAN RETURN HOST='', AND THAT'S OK FOR PLESK XML API WHICH ALLOWS IT.
# SO WE HANDLE IT AND DON'T PREVENT IT
sub_domain_name="$(_value "$1" | sed -E "s/\.?${root_domain_name}"'$//')"
_info "Matched host '$1' to: DOMAIN '${root_domain_name}' (Plesk ID '${root_domain_id}'), HOST '${sub_domain_name}'. Returning."
return 0
fi
@ -383,7 +387,7 @@ _pleskxml_get_root_domain() {
else
_debug "No match,and next parent would be a TLD..."
fi
root_domain_name="$( _valuecut 2 1000 "$root_domain_name" )"
root_domain_name="$(_valuecut 2 1000 "$root_domain_name")"
doneloop=1
done