From 5612019c0e44a2a3950ea74426bce44a8bb51b63 Mon Sep 17 00:00:00 2001 From: stilez Date: Mon, 13 Aug 2018 14:20:36 +0000 Subject: [PATCH] replace 4-space tabs -> 2-space tabs Format engine seems to insist on it :) --- dnsapi/dns_pleskxml.sh | 1120 ++++++++++++++++++++-------------------- 1 file changed, 560 insertions(+), 560 deletions(-) diff --git a/dnsapi/dns_pleskxml.sh b/dnsapi/dns_pleskxml.sh index a845ffec..f34f6dec 100644 --- a/dnsapi/dns_pleskxml.sh +++ b/dnsapi/dns_pleskxml.sh @@ -21,90 +21,90 @@ # API NOTES: # 1) The API uses a user/password combination. It should therefore require cURL over HTTPS -# with a MAXIMALLY SECURE TLS CIPHER AND GOOD CERT + REVOCATION CHECKS ON THE API URI, -# in (almost) all cases. +# with a MAXIMALLY SECURE TLS CIPHER AND GOOD CERT + REVOCATION CHECKS ON THE API URI, +# in (almost) all cases. -# Acceptable/valid ciphers and certificate checks can be specified via optional cURL variables (see below). -# Note that edge cases may exist where SSL is not yet set up -# (e.g. testing Plesk on ones own network), so although highly recommended, this can be OVERRIDDEN. +# Acceptable/valid ciphers and certificate checks can be specified via optional cURL variables (see below). +# Note that edge cases may exist where SSL is not yet set up +# (e.g. testing Plesk on ones own network), so although highly recommended, this can be OVERRIDDEN. # 2) --anyauth is used with cURL, to ensure the highest available level of encryption. # 3) The API references domains by a domain ID, when manipulating records. So the code must -# initially convert domain names (string) to Plesk domain IDs (numeric). +# initially convert domain names (string) to Plesk domain IDs (numeric). # REQUIRED VARIABLES: # You need to provide the Plesk URI and login (username and password) as follows: -# export pleskxml_uri="https://www.plesk_uri.org:8443/enterprise/control/agent.php" -# (or something similar) -# export pleskxml_user="johndoe" -# export pleskxml_pass="XXXXX" +# export pleskxml_uri="https://www.plesk_uri.org:8443/enterprise/control/agent.php" +# (or something similar) +# export pleskxml_user="johndoe" +# export pleskxml_pass="XXXXX" # OPTIONAL VARIABLES: # To use an insecure Plesk URI, set the following: -# export pleskxml_allow_insecure_uri=yes +# export pleskxml_allow_insecure_uri=yes # Extra cURL args (for certificate handling, timeout etc): -# export pleskxml_optional_curl_args=LIST_OF_ARGS -# (eg =-v or ="-H 'HEADER STRINGS'") +# export pleskxml_optional_curl_args=LIST_OF_ARGS +# (eg =-v or ="-H 'HEADER STRINGS'") # Debug level (0/absent=none, 1=all, 2=major msgs only, 3=minimum msgs/most severe only) # If debug level is nonzero, all DBG messages equal to or more severe than this, are displayed. # By design if DBG level is 9 for a message, it is ALWAYS shown, this is used for _info and _err -# export pleskxml_debug_min_level=2 +# export pleskxml_debug_min_level=2 ############ Before anything else, define dedug functions to be sure they are detected even if while testing ##################### _DBG_EARLY_CHECK_MODE() { - if printf '%s' "${pleskxml_debug_min_level:-0}" | grep -qE '^[0-3]$' ; then - _pleskxml_DBG_LEVEL="${pleskxml_debug_min_level:-0}" - _pleskxml_DBG_COUNT=0 - else - _err "Invalid debug level, exiting. \$pleskxml_debug_min_level = '${pleskxml_debug_min_level}' " - return 1 - fi + if printf '%s' "${pleskxml_debug_min_level:-0}" | grep -qE '^[0-3]$' ; then + _pleskxml_DBG_LEVEL="${pleskxml_debug_min_level:-0}" + _pleskxml_DBG_COUNT=0 + else + _err "Invalid debug level, exiting. \$pleskxml_debug_min_level = '${pleskxml_debug_min_level}' " + return 1 + fi - _info "plesk XML running in debug mode. Debug level = '${_pleskxml_DBG_LEVEL}' " - # This won't display if DBG level was set to zero. + _info "plesk XML running in debug mode. Debug level = '${_pleskxml_DBG_LEVEL}' " + # This won't display if DBG level was set to zero. } # arg1 = severity level (1=least serious, 3=most serious) # By design if DBG level is 9 for a MESSAGE, the message is ALWAYS shown, this is used for _info and _err # arg2 = message _DBG() { - if [ "$1" -eq 9 ] || ( [ "$_pleskxml_DBG_LEVEL" -gt 0 ] && [ "$1" -ge "$_pleskxml_DBG_LEVEL" ] ); then - case $1 in - 1) _pleskxml_severity='INFO' - ;; - 2) _pleskxml_severity='WARN' - ;; - 3) _pleskxml_severity='ERR' - ;; - 9) _pleskxml_severity='_ACME.SH' - ;; - esac - _pleskxml_DBG_COUNT=$(( _pleskxml_DBG_COUNT + 1 )) - printf '%04d DEBUG [%s]:\n%s\n\n' "$_pleskxml_DBG_COUNT" "$_pleskxml_severity" "$2" - fi + if [ "$1" -eq 9 ] || ( [ "$_pleskxml_DBG_LEVEL" -gt 0 ] && [ "$1" -ge "$_pleskxml_DBG_LEVEL" ] ); then + case $1 in + 1) _pleskxml_severity='INFO' + ;; + 2) _pleskxml_severity='WARN' + ;; + 3) _pleskxml_severity='ERR' + ;; + 9) _pleskxml_severity='_ACME.SH' + ;; + esac + _pleskxml_DBG_COUNT=$(( _pleskxml_DBG_COUNT + 1 )) + printf '%04d DEBUG [%s]:\n%s\n\n' "$_pleskxml_DBG_COUNT" "$_pleskxml_severity" "$2" + fi } # arg1 = severity level (1=least serious, 3=most serious) # arg2 = message (vardump will be appended) _DBG_VARDUMP() { - _DBG "$1" "$( printf '%s: 1st lines of current defined variables are now:\n%s\n\n' "${2:-NO_FURTHER_MSG}" "$( set | grep '_pleskxml' | sort )" )" + _DBG "$1" "$( printf '%s: 1st lines of current defined variables are now:\n%s\n\n' "${2:-NO_FURTHER_MSG}" "$( set | grep '_pleskxml' | sort )" )" } _DBG_ERR_TRAP() { - echo "Error on line $1" + echo "Error on line $1" } @@ -135,26 +135,26 @@ _pleskxml_newline=' # (otherwise printf repeats the string causing the API call to fail) _pleskxml_tplt_get_domain_id="%s" - # Convert domain name to a Plesk internal domain ID - # Args: - # the domain name to query + # Convert domain name to a Plesk internal domain ID + # Args: + # the domain name to query _pleskxml_tplt_add_txt_record="%sTXT%s%s" - # Adds a TXT record to a domain - # Args: - # the Plesk internal domain ID for the domain - # the "host" entry within the domain, to add this to (eg '_acme_challenge') - # the TXT record value + # Adds a TXT record to a domain + # Args: + # the Plesk internal domain ID for the domain + # the "host" entry within the domain, to add this to (eg '_acme_challenge') + # the TXT record value _pleskxml_tplt_rmv_dns_record="%s" - # Adds a TXT record to a domain - # Args: - # the Plesk internal ID for the dns record to delete + # Adds a TXT record to a domain + # Args: + # the Plesk internal ID for the dns record to delete _pleskxml_tplt_get_dns_records="%s" - # Gets all DNS records for a Plesk domain ID - # Args: - # the domain id to query + # Gets all DNS records for a Plesk domain ID + # Args: + # the domain id to query ############ Define public functions ##################### @@ -163,78 +163,78 @@ _pleskxml_tplt_get_dns_records="%s/dev/null )" - _pleskxml_retcode="$?" - _DBG 1 "_pleskxml_prettyprint_result =${_pleskxml_newline}'$_pleskxml_prettyprint_result' " - _DBG 2 "retcode = $_pleskxml_retcode" + _DBG 2 "$( printf 'cURL command: %s %s %s' "$_pleskxml_curlpath" "$_pleskxml_curlargs" "$_pleskxml_uri" )" + _pleskxml_prettyprint_result="$( eval "$_pleskxml_curlpath" "$_pleskxml_curlargs" "$_pleskxml_uri" 2>/dev/null )" + _pleskxml_retcode="$?" + _DBG 1 "_pleskxml_prettyprint_result =${_pleskxml_newline}'$_pleskxml_prettyprint_result' " + _DBG 2 "retcode = $_pleskxml_retcode" # BUGFIX TO CHECK - WILL RETCODE FROM cURL BE AVAILABLE HERE? - # Abort if cURL failed + # Abort if cURL failed - if [ $_pleskxml_retcode -ne 0 ]; then - _pleskxml_errors="Exiting due to cURL error when querying Plesk XML API. The cURL return code was: $_pleskxml_retcode." - _DBG 2 "$_pleskxml_errors" - return 1 - fi + if [ $_pleskxml_retcode -ne 0 ]; then + _pleskxml_errors="Exiting due to cURL error when querying Plesk XML API. The cURL return code was: $_pleskxml_retcode." + _DBG 2 "$_pleskxml_errors" + return 1 + fi - # OK. Next, check XML reply was OK. Start by pushing it into one line, with leading/trailing space trimmed. + # OK. Next, check XML reply was OK. Start by pushing it into one line, with leading/trailing space trimmed. -# _pleskxml_result="$( printf '%s' "$_pleskxml_prettyprint_result" | \ -# awk '{$1=$1};1' | \ -# tr -d '\n' \ -# )" +# _pleskxml_result="$( printf '%s' "$_pleskxml_prettyprint_result" | \ +# awk '{$1=$1};1' | \ +# tr -d '\n' \ +# )" - _pleskxml_result="$( printf '%s' "$_pleskxml_prettyprint_result" | \ - sed -E 's/(^[[:space:]]+|[[:space:]]+$)//g' | \ - tr -d '\n' \ - )" - - _DBG_VARDUMP 2 'cURL succeeded, valid cURL response obtained' + _pleskxml_result="$( printf '%s' "$_pleskxml_prettyprint_result" | \ + sed -E 's/(^[[:space:]]+|[[:space:]]+$)//g' | \ + tr -d '\n' \ + )" + + _DBG_VARDUMP 2 'cURL succeeded, valid cURL response obtained' - # Now we need to check item by item if it's OK. - # As we go, we will strip out "known OK" stuff to leave the core reply. + # Now we need to check item by item if it's OK. + # As we go, we will strip out "known OK" stuff to leave the core reply. - # XML header and packet version? + # XML header and packet version? - _DBG 2 'Checking and tags exist...' - - if printf '%s' "$_pleskxml_result" | grep -qiEv '^<\?xml version=[^>]+>]+>.*$'; then - # Error - should have .... Abort - _pleskxml_errors="Error when querying Plesk XML API. The API did not return a valid XML response. The response was:${_pleskxml_newline}${_pleskxml_prettyprint_result}${_pleskxml_newline}The collapsed version was:${_pleskxml_newline}'${_pleskxml_result}'${_pleskxml_newline}" - _DBG 2 "$_pleskxml_errors" - return 1 - else - # So far so good. Strip the and ... tags and continue - _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ - sed -E 's/^<\?xml version[^>]+>]+>(.*)<\/packet>$/\1/' \ - )" - fi - - _DBG 2 "Checking tags don't exist..." - - # section found anywhere in response? - # This usually means some kind of basic API error such as login failure, bad XML request, etc - - - if printf '%s' "$_pleskxml_result" | grep -qiE '.*'; then - # Error - shouldn't contain .... Abort - _pleskxml_errors='Error when querying Plesk XML API. The result contained a tag.\nThis usually indicates an invalid login, badly formatted API request or other error. The response was:\n'"$_pleskxml_prettyprint_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - fi - - - _DBG 2 'Checking 1 or >=1 tag (or tags) found, each containing 'status:ok'...' - - - # Check results section. Most commands only have one results section. - # But some (i.e., get all DNS records for a domain) have many results sections, - # and we will need to check each ... section separately. - # So this gets a bit messy, especially as we don't have non-greedy regex - # and we will have to work around that as well. - - # For this, we will split the string up again with exactly 1 section per line. - # We check there is at least one result section. Then we add newlines before and after - # any ... and ignore any lines that don't contain ''. - - if printf '%s' "$_pleskxml_result" | grep -qiEv '.*'; then - # Error - doesn't contain .... Abort - _pleskxml_errors='Error when querying Plesk XML API. The result did not contain a section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - fi - - _DBG 2 'Found at least 1 section. Splitting each result section to a separate line' + _DBG 2 'Checking and tags exist...' + if printf '%s' "$_pleskxml_result" | grep -qiEv '^<\?xml version=[^>]+>]+>.*$'; then + # Error - should have .... Abort + _pleskxml_errors="Error when querying Plesk XML API. The API did not return a valid XML response. The response was:${_pleskxml_newline}${_pleskxml_prettyprint_result}${_pleskxml_newline}The collapsed version was:${_pleskxml_newline}'${_pleskxml_result}'${_pleskxml_newline}" + _DBG 2 "$_pleskxml_errors" + return 1 + else + # So far so good. Strip the and ... tags and continue _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ - sed "s//\\${_pleskxml_newline}/g" | \ - sed "s/<\/result>/<\/result>\\${_pleskxml_newline}/g" | \ - grep '' \ - )" + sed -E 's/^<\?xml version[^>]+>]+>(.*)<\/packet>$/\1/' \ + )" + fi - # Detect and abort if there are >1 sections and we're ponly expecting 1 section. + _DBG 2 "Checking tags don't exist..." - _pleskxml_linecount=$( printf '%s\n' "$_pleskxml_result" | wc -l ) + # section found anywhere in response? + # This usually means some kind of basic API error such as login failure, bad XML request, etc - _DBG 2 "Result is: '$_pleskxml_result' (${_pleskxml_linecount} line(s))" - _DBG_VARDUMP 2 'Testing section linecount is OK (1 or >=1 as required)' + if printf '%s' "$_pleskxml_result" | grep -qiE '.*'; then + # Error - shouldn't contain .... Abort + _pleskxml_errors='Error when querying Plesk XML API. The result contained a tag.\nThis usually indicates an invalid login, badly formatted API request or other error. The response was:\n'"$_pleskxml_prettyprint_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + fi - if [ $_pleskxml_multiple_results_allowed -eq 0 ] && [ "$_pleskxml_linecount" -gt 1 ]; then - # Error - contains multiple sections. Abort - _pleskxml_errors='Error when querying Plesk XML API. The result contained more than one section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - fi - _DBG 2 "Found ${_pleskxml_linecount} section(s), checking each has status:ok..." + _DBG 2 'Checking 1 or >=1 tag (or tags) found, each containing 'status:ok'...' - # Loop through each section, checking every line has exactly one result section, - # containing exactly one status section, which contains ok - while IFS= read -r _pleskxml_line; do + # Check results section. Most commands only have one results section. + # But some (i.e., get all DNS records for a domain) have many results sections, + # and we will need to check each ... section separately. + # So this gets a bit messy, especially as we don't have non-greedy regex + # and we will have to work around that as well. - # _pleskxml_line *should* contain a single result section. - # Check this is correct. + # For this, we will split the string up again with exactly 1 section per line. + # We check there is at least one result section. Then we add newlines before and after + # any ... and ignore any lines that don't contain ''. + + if printf '%s' "$_pleskxml_result" | grep -qiEv '.*'; then + # Error - doesn't contain .... Abort + _pleskxml_errors='Error when querying Plesk XML API. The result did not contain a section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + fi + + _DBG 2 'Found at least 1 section. Splitting each result section to a separate line' + + _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ + sed "s//\\${_pleskxml_newline}/g" | \ + sed "s/<\/result>/<\/result>\\${_pleskxml_newline}/g" | \ + grep '' \ + )" + + # Detect and abort if there are >1 sections and we're ponly expecting 1 section. + + _pleskxml_linecount=$( printf '%s\n' "$_pleskxml_result" | wc -l ) + + _DBG 2 "Result is: '$_pleskxml_result' (${_pleskxml_linecount} line(s))" + + _DBG_VARDUMP 2 'Testing section linecount is OK (1 or >=1 as required)' + + if [ $_pleskxml_multiple_results_allowed -eq 0 ] && [ "$_pleskxml_linecount" -gt 1 ]; then + # Error - contains multiple sections. Abort + _pleskxml_errors='Error when querying Plesk XML API. The result contained more than one section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + fi + + _DBG 2 "Found ${_pleskxml_linecount} section(s), checking each has status:ok..." + + # Loop through each section, checking every line has exactly one result section, + # containing exactly one status section, which contains ok + + while IFS= read -r _pleskxml_line; do + + # _pleskxml_line *should* contain a single result section. + # Check this is correct. # _DBG "Checking a section... content is ${_pleskxml_line}" - if printf '%s' "$_pleskxml_line" | grep -qiEv '^.*$'; then - # Error - doesn't contain .... Abort - _pleskxml_errors='Error when querying Plesk XML API. A section was not found where expected.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\n' - return 1 - fi + if printf '%s' "$_pleskxml_line" | grep -qiEv '^.*$'; then + # Error - doesn't contain .... Abort + _pleskxml_errors='Error when querying Plesk XML API. A section was not found where expected.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\n' + return 1 + fi - # Now strip the tag and check there is precisely one section and its ciontents are "ok" + # Now strip the tag and check there is precisely one section and its ciontents are "ok" - _pleskxml_line="$( printf '%s' "$_pleskxml_line" | sed -E 's/^(.*)<\/result>$/\1/' )" + _pleskxml_line="$( printf '%s' "$_pleskxml_line" | sed -E 's/^(.*)<\/result>$/\1/' )" - if printf '%s' "$_pleskxml_line" | grep -qiEv '.*'; then - # Error - doesn't contain .... Abort - _pleskxml_errors='Error when querying Plesk XML API. A section did not contain a section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - elif printf '%s' "$_pleskxml_line" | grep -qiE '.*.*'; then - # Error - contains ....... Abort - _pleskxml_errors='Error when querying Plesk XML API. A section contained more than one section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - elif printf '%s' "$_pleskxml_line" | grep -qiEv 'ok'; then - # Error - doesn't contain ok. Abort - _pleskxml_errors='Error when querying Plesk XML API. A tag did not contain "ok". The response was:\n'"$_pleskxml_prettyprint_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - fi + if printf '%s' "$_pleskxml_line" | grep -qiEv '.*'; then + # Error - doesn't contain .... Abort + _pleskxml_errors='Error when querying Plesk XML API. A section did not contain a section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + elif printf '%s' "$_pleskxml_line" | grep -qiE '.*.*'; then + # Error - contains ....... Abort + _pleskxml_errors='Error when querying Plesk XML API. A section contained more than one section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + elif printf '%s' "$_pleskxml_line" | grep -qiEv 'ok'; then + # Error - doesn't contain ok. Abort + _pleskxml_errors='Error when querying Plesk XML API. A tag did not contain "ok". The response was:\n'"$_pleskxml_prettyprint_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + fi # _DBG "Line is OK. Looping to next line or exiting..." - done << EOL + done << EOL $_pleskxml_result EOL - # So far so good. Remove all ok sections as they're checked now. + # So far so good. Remove all ok sections as they're checked now. - _DBG 2 "All results lines had status:ok. Exiting loop, and removing all ok tags now they've been checked" + _DBG 2 "All results lines had status:ok. Exiting loop, and removing all ok tags now they've been checked" - _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ - sed -E 's/ok<\/status>//g' \ - )" + _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ + sed -E 's/ok<\/status>//g' \ + )" - # Result is OK. Remove any redundant self-closing tags, and or tags, and exit + # Result is OK. Remove any redundant self-closing tags, and or tags, and exit - _DBG 2 'Now removing any self-closing tags, or ... tags' + _DBG 2 'Now removing any self-closing tags, or ... tags' - _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ - sed -E 's/(<[a-zA-Z0-9._-]+[[:space:]]*\/>|<\/?data\/?>)//g' \ - )" + _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ + sed -E 's/(<[a-zA-Z0-9._-]+[[:space:]]*\/>|<\/?data\/?>)//g' \ + )" - _DBG 2 "About to exit API function. Result = ${_pleskxml_newline}'${_pleskxml_result}' " + _DBG 2 "About to exit API function. Result = ${_pleskxml_newline}'${_pleskxml_result}' " - _DBG_VARDUMP 2 'Successfully exiting Plesk XML API function' + _DBG_VARDUMP 2 'Successfully exiting Plesk XML API function' - return 0 + return 0 } @@ -616,88 +616,88 @@ EOL _pleskxml_get_domain_ID() { - _DBG 2 "Entered Plesk get_domain_ID($*), to get the domain's Plesk ID." + _DBG 2 "Entered Plesk get_domain_ID($*), to get the domain's Plesk ID." - # Call cURL to convert a domain name to a plesk domain ID. + # Call cURL to convert a domain name to a plesk domain ID. - _DBG 2 'About to make API request (domain name -> domain ID)' + _DBG 2 'About to make API request (domain name -> domain ID)' - _pleskxml_api_request "$_pleskxml_tplt_get_domain_id" "$1" - _pleskxml_retcode=$? - # $1 is the domain name we wish to convert to a Plesk domain ID + _pleskxml_api_request "$_pleskxml_tplt_get_domain_id" "$1" + _pleskxml_retcode=$? + # $1 is the domain name we wish to convert to a Plesk domain ID - _DBG 2 'Returned from API request, now back in get_domain_ID()' + _DBG 2 'Returned from API request, now back in get_domain_ID()' - if [ $_pleskxml_retcode -ne 0 ] || [ "$_pleskxml_errors" != '' ] || [ "$_pleskxml_result" = '' ]; then - # Really, just testing return code should be enough, based on above code, but let's go "all-in" and test all variables returned - _DBG 2 "$_pleskxml_errors" - return 1 - fi + if [ $_pleskxml_retcode -ne 0 ] || [ "$_pleskxml_errors" != '' ] || [ "$_pleskxml_result" = '' ]; then + # Really, just testing return code should be enough, based on above code, but let's go "all-in" and test all variables returned + _DBG 2 "$_pleskxml_errors" + return 1 + fi - # OK, we should have a domain ID. Let's check and return it if so. + # OK, we should have a domain ID. Let's check and return it if so. - # Result should comprise precisely one section + # Result should comprise precisely one section - _DBG 2 'Testing API return data for one and removing if so' - - if printf '%s' "$_pleskxml_result" | grep -qiEv '^.*$'; then - # Error - doesn't comprise DOMAINNAME. Something's wrong. Abort - _pleskxml_errors='Error when querying Plesk XML API. The API did not comprise a section containing all other data.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - elif printf '%s' "$_pleskxml_result" | grep -qiE '.*'; then - # Error - contains ....... Abort - _pleskxml_errors='Error when querying Plesk XML API. The API contained more than one section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - else - # So far so good. Remove the ... section and continue - _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ - sed -E 's/(^|<\/result>$)//g' \ - )" - fi - - # Result should contain precisely one section, containing the domain name inquired. - - _DBG 2 'Testing API return data for one and removing if so' - - if printf '%s' "$_pleskxml_result" | grep -qiv "$1"; then - # Error - doesn't contain DOMAINNAME. Something's wrong. Abort - _pleskxml_errors='Error when querying Plesk XML API. The API did not contain the expected section containing the domain name.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - elif printf '%s' "$_pleskxml_result" | grep -qiE '.*'; then - # Error - contains ....... Abort - _pleskxml_errors='Error when querying Plesk XML API. The API contained more than one section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - else - # So far so good. Remove the ... section and continue - _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ - sed "s/$1<\/filter-id>//" \ - )" - fi - - # All that should be left is one section, containing DOMAIN_ID - - _DBG 2 "Remaining part of result is now: '$_pleskxml_result' " - - if printf '%s' "$_pleskxml_result" | grep -qiEv '^[0-9]+$'; then - # Error - doesn't contain just NUMBERS. Something's wrong. Abort - _pleskxml_errors='Error when querying Plesk XML API. The API did not contain the expected [NUMERIC_ID] section, or contained other unexpected values as well.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - fi - - # SUCCESS! Remove the surrounding tag and return the value! + _DBG 2 'Testing API return data for one and removing if so' + if printf '%s' "$_pleskxml_result" | grep -qiEv '^.*$'; then + # Error - doesn't comprise DOMAINNAME. Something's wrong. Abort + _pleskxml_errors='Error when querying Plesk XML API. The API did not comprise a section containing all other data.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + elif printf '%s' "$_pleskxml_result" | grep -qiE '.*'; then + # Error - contains ....... Abort + _pleskxml_errors='Error when querying Plesk XML API. The API contained more than one section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + else + # So far so good. Remove the ... section and continue _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ - sed -E 's/^([0-9]+)<\/id>$/\1/' \ - )" + sed -E 's/(^|<\/result>$)//g' \ + )" + fi - _DBG_VARDUMP 2 'SUCCESSFULLY exiting Plesk get_domain_ID' + # Result should contain precisely one section, containing the domain name inquired. - return 0 + _DBG 2 'Testing API return data for one and removing if so' + + if printf '%s' "$_pleskxml_result" | grep -qiv "$1"; then + # Error - doesn't contain DOMAINNAME. Something's wrong. Abort + _pleskxml_errors='Error when querying Plesk XML API. The API did not contain the expected section containing the domain name.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + elif printf '%s' "$_pleskxml_result" | grep -qiE '.*'; then + # Error - contains ....... Abort + _pleskxml_errors='Error when querying Plesk XML API. The API contained more than one section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + else + # So far so good. Remove the ... section and continue + _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ + sed "s/$1<\/filter-id>//" \ + )" + fi + + # All that should be left is one section, containing DOMAIN_ID + + _DBG 2 "Remaining part of result is now: '$_pleskxml_result' " + + if printf '%s' "$_pleskxml_result" | grep -qiEv '^[0-9]+$'; then + # Error - doesn't contain just NUMBERS. Something's wrong. Abort + _pleskxml_errors='Error when querying Plesk XML API. The API did not contain the expected [NUMERIC_ID] section, or contained other unexpected values as well.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + fi + + # SUCCESS! Remove the surrounding tag and return the value! + + _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ + sed -E 's/^([0-9]+)<\/id>$/\1/' \ + )" + + _DBG_VARDUMP 2 'SUCCESSFULLY exiting Plesk get_domain_ID' + + return 0 } @@ -708,112 +708,112 @@ _pleskxml_get_domain_ID() { _pleskxml_get_dns_records() { - _DBG 2 "Entered Plesk _pleskxml_get_dns_records($*)" + _DBG 2 "Entered Plesk _pleskxml_get_dns_records($*)" - # First, we need to get all DNS records, and check the list is valid + # First, we need to get all DNS records, and check the list is valid - _DBG 2 'About to make API request (get DNS records)' + _DBG 2 'About to make API request (get DNS records)' - _pleskxml_api_request "$_pleskxml_tplt_get_dns_records" "$1" - _pleskxml_retcode=$? - # $1 is the Plesk internal domain ID for the domain + _pleskxml_api_request "$_pleskxml_tplt_get_dns_records" "$1" + _pleskxml_retcode=$? + # $1 is the Plesk internal domain ID for the domain - _DBG 2 'Returned from API request, now back in get_txt_records()' + _DBG 2 'Returned from API request, now back in get_txt_records()' - if [ $_pleskxml_retcode -ne 0 ] || [ "$_pleskxml_errors" != '' ] || [ "$_pleskxml_result" = '' ]; then - # Really, just testing return code should be enough, based on above code, but let's go "all-in" and test all variables returned - _DBG 2 "$_pleskxml_errors" - return 1 - fi + if [ $_pleskxml_retcode -ne 0 ] || [ "$_pleskxml_errors" != '' ] || [ "$_pleskxml_result" = '' ]; then + # Really, just testing return code should be enough, based on above code, but let's go "all-in" and test all variables returned + _DBG 2 "$_pleskxml_errors" + return 1 + fi - # OK, we should have a section containing a list of DNS records. - # Now keep only the TXT records + # OK, we should have a section containing a list of DNS records. + # Now keep only the TXT records - _DBG 2 "Full DNS records were:${_pleskxml_newline}${_pleskxml_newline}'${_pleskxml_result}' " + _DBG 2 "Full DNS records were:${_pleskxml_newline}${_pleskxml_newline}'${_pleskxml_result}' " - if [ -n "${2:-}" ]; then - _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ - grep "$2" \ - )" - _DBG 2 "Filtered relevant DNS records. Records to be returned are:${_pleskxml_newline}${_pleskxml_newline}'${_pleskxml_result}' " - else - _DBG 2 'Not filtering DNS records. All records will be returned.' - fi + if [ -n "${2:-}" ]; then + _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ + grep "$2" \ + )" + _DBG 2 "Filtered relevant DNS records. Records to be returned are:${_pleskxml_newline}${_pleskxml_newline}'${_pleskxml_result}' " + else + _DBG 2 'Not filtering DNS records. All records will be returned.' + fi - _DBG 2 "SUCCESSFULLY exiting _pleskxml_get_dns_records" - return 0 + _DBG 2 "SUCCESSFULLY exiting _pleskxml_get_dns_records" + return 0 } _pleskxml_add_txt_record() { - _DBG 2 "Entered Plesk _pleskxml_add_txt_record($*)" + _DBG 2 "Entered Plesk _pleskxml_add_txt_record($*)" - _DBG 2 'About to make API request (add TXT record)' + _DBG 2 'About to make API request (add TXT record)' - _pleskxml_api_request "$_pleskxml_tplt_add_txt_record" "$1" "$2" "$3" - _pleskxml_retcode=$? + _pleskxml_api_request "$_pleskxml_tplt_add_txt_record" "$1" "$2" "$3" + _pleskxml_retcode=$? - # $1 is the Plesk internal domain ID for the domain - # $2 is the "host" entry within the domain, to add this to (eg '_acme_challenge') - # $3 is the TXT record value + # $1 is the Plesk internal domain ID for the domain + # $2 is the "host" entry within the domain, to add this to (eg '_acme_challenge') + # $3 is the TXT record value - _DBG 2 'Returned from API request, now back in add_txt_record()' + _DBG 2 'Returned from API request, now back in add_txt_record()' - if [ $_pleskxml_retcode -ne 0 ] || [ "$_pleskxml_errors" != '' ] || [ "$_pleskxml_result" = '' ]; then - # Really, just testing return code should be enough, based on above code, but let's go "all-in" and test all variables returned - _DBG 2 "$_pleskxml_errors" - return 1 - fi + if [ $_pleskxml_retcode -ne 0 ] || [ "$_pleskxml_errors" != '' ] || [ "$_pleskxml_result" = '' ]; then + # Really, just testing return code should be enough, based on above code, but let's go "all-in" and test all variables returned + _DBG 2 "$_pleskxml_errors" + return 1 + fi - # 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 PLESK_NEW_DNS_RECORD_ID + # 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 PLESK_NEW_DNS_RECORD_ID - if printf '%s' "$_pleskxml_result" | grep -qivE '^[0-9]+$'; then - # Error - doesn't contain just NUMBERS. Something's wrong. Abort - _pleskxml_errors='Error when calling Plesk XML API. The API did not contain the expected [PLESK_NEW_DNS_RECORD_ID] section, or contained other unexpected values as well.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - fi + if printf '%s' "$_pleskxml_result" | grep -qivE '^[0-9]+$'; then + # Error - doesn't contain just NUMBERS. Something's wrong. Abort + _pleskxml_errors='Error when calling Plesk XML API. The API did not contain the expected [PLESK_NEW_DNS_RECORD_ID] section, or contained other unexpected values as well.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + fi - # SUCCESS! Remove the surrounding tags and return the value! - # (although we don't actually use it! + # SUCCESS! Remove the surrounding tags and return the value! + # (although we don't actually use it! - _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ - sed -E "s/^([0-9]+)<\/id><\/result>$/\1/" \ - )" + _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ + sed -E "s/^([0-9]+)<\/id><\/result>$/\1/" \ + )" - _DBG_VARDUMP 2 'SUCCESSFULLY exiting Plesk _pleskxml_add_txt_record' + _DBG_VARDUMP 2 'SUCCESSFULLY exiting Plesk _pleskxml_add_txt_record' - return 0 + return 0 } _pleskxml_rmv_dns_record() { - _DBG 2 "Entered Plesk _pleskxml_rmv_dns_record($*)" + _DBG 2 "Entered Plesk _pleskxml_rmv_dns_record($*)" - _DBG 2 'About to make API request (rmv TXT record)' + _DBG 2 'About to make API request (rmv TXT record)' - _pleskxml_api_request "$_pleskxml_tplt_rmv_dns_record" "$1" - _pleskxml_retcode=$? + _pleskxml_api_request "$_pleskxml_tplt_rmv_dns_record" "$1" + _pleskxml_retcode=$? - # $1 is the Plesk internal domain ID for the TXT record + # $1 is the Plesk internal domain ID for the TXT record - _DBG 2 'Returned from API request, now back in rmv_dns_record()' + _DBG 2 'Returned from API request, now back in rmv_dns_record()' - if [ $_pleskxml_retcode -ne 0 ] || [ "$_pleskxml_errors" != '' ] || [ "$_pleskxml_result" = '' ]; then - # Really, just testing return code should be enough, based on above code, but let's go "all-in" and test all variables returned - _DBG 2 "$_pleskxml_errors" - return 1 - fi + if [ $_pleskxml_retcode -ne 0 ] || [ "$_pleskxml_errors" != '' ] || [ "$_pleskxml_result" = '' ]; then + # Really, just testing return code should be enough, based on above code, but let's go "all-in" and test all variables returned + _DBG 2 "$_pleskxml_errors" + return 1 + fi - # OK, we should have removed a TXT record. If it failed, there wouldn't have been a "status:ok" above + # OK, we should have removed a TXT record. If it failed, there wouldn't have been a "status:ok" above - _DBG_VARDUMP 2 'SUCCESSFULLY exiting Plesk _pleskxml_rmv_dns_record' + _DBG_VARDUMP 2 'SUCCESSFULLY exiting Plesk _pleskxml_rmv_dns_record' - return 0 + return 0 } @@ -823,76 +823,76 @@ _pleskxml_rmv_dns_record() { _pleskxml_rmv_txt_record() { - _DBG 2 "Entered Plesk _pleskxml_rmv_dns_TXT_record($*). Getting DNS TXT records for the domain ID" + _DBG 2 "Entered Plesk _pleskxml_rmv_dns_TXT_record($*). Getting DNS TXT records for the domain ID" - _pleskxml_get_dns_records "$1" 'TXT' - _pleskxml_retcode=$? - # $1 is the Plesk internal domain ID for the domain + _pleskxml_get_dns_records "$1" 'TXT' + _pleskxml_retcode=$? + # $1 is the Plesk internal domain ID for the domain - _DBG 2 'Returned from API request, now back in rmv_txt_record()' + _DBG 2 'Returned from API request, now back in rmv_txt_record()' - if [ $_pleskxml_retcode -ne 0 ] || [ "$_pleskxml_errors" != '' ] || [ "$_pleskxml_result" = '' ]; then - # Really, just testing return code should be enough, based on above code, but let's go "all-in" and test all variables returned - _DBG 2 "$_pleskxml_errors" - return 1 - fi + if [ $_pleskxml_retcode -ne 0 ] || [ "$_pleskxml_errors" != '' ] || [ "$_pleskxml_result" = '' ]; then + # Really, just testing return code should be enough, based on above code, but let's go "all-in" and test all variables returned + _DBG 2 "$_pleskxml_errors" + return 1 + fi - # OK, we should have a section containing a list of DNS TXT records. - # Now we need to find our desired record in it (if it exists). - # and might as well collapse any successful matches to a signle line for line-count purposes at the same time + # OK, we should have a section containing a list of DNS TXT records. + # Now we need to find our desired record in it (if it exists). + # and might as well collapse any successful matches to a signle line for line-count purposes at the same time - _DBG 2 "Filters to apply (as literal strings):${_pleskxml_newline}'${2:-}.'${_pleskxml_newline}'${3:-}' " + _DBG 2 "Filters to apply (as literal strings):${_pleskxml_newline}'${2:-}.'${_pleskxml_newline}'${3:-}' " + + _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ + grep -F "${2:-}." | \ + grep -F "${3:-}" | \ + sed -E 's/(^[[:space:]]+|[[:space:]]+$)//g' | \ + tr -d '\n' \ + )" + # Run 2 separate GREP filters, because the host and value order isn't mandatory in the API return data + # ands this avoids regex and escaping which is easier + # NOTE: the returned "host" field is actually the FQDN, not just the host ID, hence the grep match on that field. + + _DBG 2 "Filtered result:${_pleskxml_newline}'$_pleskxml_result' " + + if printf '%s' "$_pleskxml_result" | grep -qiE ".*"; then + # Error - contains ....... Abort + _pleskxml_errors='Error when querying Plesk XML API. The API contained more than one section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' + _DBG 2 "$_pleskxml_errors" + return 1 + fi + + if printf '%s\n' "$_pleskxml_result" | grep -qiv ""; then + # No matching TXT records, so we're done. + _info "Couldn't find a TXT record matching the requested host/value. Not an error, but a concern..." + _DBG 2 "Exiting Plesk _pleskxml_rmv_txt_record (without raising an error), as nothing more to do: the record requested for deletion doesn't exist" + _pleskxml_result='' + return 0 + fi + + # If we get here, there was a single TXT record match, so we delete it. _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ - grep -F "${2:-}." | \ - grep -F "${3:-}" | \ - sed -E 's/(^[[:space:]]+|[[:space:]]+$)//g' | \ - tr -d '\n' \ - )" - # Run 2 separate GREP filters, because the host and value order isn't mandatory in the API return data - # ands this avoids regex and escaping which is easier - # NOTE: the returned "host" field is actually the FQDN, not just the host ID, hence the grep match on that field. + sed -E 's/^.*([0-9]+)<\/id>.*$/\1/' \ + )" - _DBG 2 "Filtered result:${_pleskxml_newline}'$_pleskxml_result' " + _DBG 2 "A unique matching DNS TXT record was found, with Plesk record ID = '$_pleskxml_result'. Calling API to delete this record." - if printf '%s' "$_pleskxml_result" | grep -qiE ".*"; then - # Error - contains ....... Abort - _pleskxml_errors='Error when querying Plesk XML API. The API contained more than one section.\nThis is unexpected: something has gone wrong. Please raise this as a bug/issue in the module. The response was:\n'"$_pleskxml_prettyprint_result"'\nand the exact test string was:\n'"$_pleskxml_result"'\n' - _DBG 2 "$_pleskxml_errors" - return 1 - fi + _pleskxml_rmv_dns_record "$_pleskxml_result" + _pleskxml_retcode=$? - if printf '%s\n' "$_pleskxml_result" | grep -qiv ""; then - # No matching TXT records, so we're done. - _info "Couldn't find a TXT record matching the requested host/value. Not an error, but a concern..." - _DBG 2 "Exiting Plesk _pleskxml_rmv_txt_record (without raising an error), as nothing more to do: the record requested for deletion doesn't exist" - _pleskxml_result='' - return 0 - fi + _DBG 2 'Returned from API request, now back in rmv_txt_record()' - # If we get here, there was a single TXT record match, so we delete it. + if [ $_pleskxml_retcode -ne 0 ] || [ "$_pleskxml_errors" != '' ] || [ "$_pleskxml_result" = '' ]; then + # Really, just testing return code should be enough, based on above code, but let's go "all-in" and test all variables returned + _DBG 2 "$_pleskxml_errors" + return 1 + fi - _pleskxml_result="$( printf '%s' "$_pleskxml_result" | \ - sed -E 's/^.*([0-9]+)<\/id>.*$/\1/' \ - )" + _DBG_VARDUMP 2 'SUCCESSFULLY exiting Plesk _pleskxml_rmv_txt_record' - _DBG 2 "A unique matching DNS TXT record was found, with Plesk record ID = '$_pleskxml_result'. Calling API to delete this record." - - _pleskxml_rmv_dns_record "$_pleskxml_result" - _pleskxml_retcode=$? - - _DBG 2 'Returned from API request, now back in rmv_txt_record()' - - if [ $_pleskxml_retcode -ne 0 ] || [ "$_pleskxml_errors" != '' ] || [ "$_pleskxml_result" = '' ]; then - # Really, just testing return code should be enough, based on above code, but let's go "all-in" and test all variables returned - _DBG 2 "$_pleskxml_errors" - return 1 - fi - - _DBG_VARDUMP 2 'SUCCESSFULLY exiting Plesk _pleskxml_rmv_txt_record' - - return 0 + return 0 } @@ -902,14 +902,14 @@ exit # defined by user - pleskxml_uri="https://plesk.XXXXX.net:8443/enterprise/control/agent.php" - pleskxml_user="XXXXX" - pleskxml_pass="XXXXX" - pleskxml_debug_min_level=3 + pleskxml_uri="https://plesk.XXXXX.net:8443/enterprise/control/agent.php" + pleskxml_user="XXXXX" + pleskxml_pass="XXXXX" + pleskxml_debug_min_level=3 # defined from args by module - _pleskxml_FQDN="_acme_challenge.XXXXX.com" - _pleskxml_TXT_string='~test~string~' + _pleskxml_FQDN="_acme_challenge.XXXXX.com" + _pleskxml_TXT_string='~test~string~' printf '\n\n\n\n======================================================================== START OF RUN\n\n'