|
|
|
@ -0,0 +1,101 @@ |
|
|
|
#!/usr/bin/env bash |
|
|
|
# Need to add your email address and API key to clouddns below or set as env variables |
|
|
|
email=${CLOUDDNS_EMAIL:-''} |
|
|
|
password=${CLOUDDNS_PASSWORD:-''} |
|
|
|
client=${CLOUDDNS_CLIENT:-''} |
|
|
|
|
|
|
|
# This script adds a token to clouddns DNS for the ACME challenge |
|
|
|
# usage dns_add_clouddns "domain name" "token" |
|
|
|
# return codes are; |
|
|
|
# 0 - success |
|
|
|
# 1 - error in input |
|
|
|
# 2 - error within internal processing |
|
|
|
# 3 - error in result ( domain not found in clouddns etc) |
|
|
|
|
|
|
|
fulldomain="${1}" |
|
|
|
token="${2}" |
|
|
|
API='https://admin.vshosting.cloud/clouddns' |
|
|
|
LOGIN_API='https://admin.vshosting.cloud/api/public/auth/login' |
|
|
|
|
|
|
|
# Check initial parameters |
|
|
|
if [[ -z "$fulldomain" ]]; then |
|
|
|
echo "DNS script requires full domain name as first parameter" |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
if [[ -z "$token" ]]; then |
|
|
|
echo "DNS script requires challenge token as second parameter" |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
if [[ -z "$email" ]]; then |
|
|
|
echo "CLOUDDNS_EMAIL (email) parameter not set" |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
if [[ -z "$password" ]]; then |
|
|
|
echo "CLOUDDNS_PASSWORD (password) parameter not set" |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
if [[ -z "$client" ]]; then |
|
|
|
echo "CLOUDDNS_CLIENT (id) parameter not set" |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
|
|
|
|
# Login to clouddns to get accessToken |
|
|
|
resp=$(curl --silent -X POST -H 'Content-Type: application/json' "$LOGIN_API" \ |
|
|
|
--data "{\"email\": \"$email\", \"password\": \"$password\"}") |
|
|
|
re='"accessToken":"([^,]*)",' # Match access token |
|
|
|
if [[ "${resp// }" =~ $re ]]; then |
|
|
|
access_token="${BASH_REMATCH[1]}" |
|
|
|
fi |
|
|
|
if [[ -z "$access_token" ]]; then |
|
|
|
echo 'Could not get access token; check your credentials' |
|
|
|
exit 3 |
|
|
|
fi |
|
|
|
curl_params=( -H "Authorization: Bearer $access_token" -H 'Content-Type: application/json' ) |
|
|
|
|
|
|
|
# Get main domain and challenge record |
|
|
|
domain_root=$(echo "$fulldomain" | awk -F\. '{print $(NF-1) FS $NF}') |
|
|
|
txt_record="_acme-challenge.$domain_root." |
|
|
|
|
|
|
|
# Get domain id |
|
|
|
curl_domainid_body="{\"search\": [{\"name\": \"clientId\", \"operator\": \"eq\", \"value\": \"$client\"}, {\"name\": \"domainName\", \"operator\": \"eq\", \"value\": \"$domain_root.\"}]}" |
|
|
|
resp=$(curl --silent "${curl_params[@]}" -X POST -d "$curl_domainid_body" "$API/domain/search") |
|
|
|
re='domainType":"[^"]*","id":"([^,]*)",' # Find result section |
|
|
|
if [[ "${resp//[$'\t\r\n ']}" =~ $re ]]; then |
|
|
|
domain_id="${BASH_REMATCH[1]}" |
|
|
|
fi |
|
|
|
|
|
|
|
if [[ -z "$domain_id" ]]; then |
|
|
|
echo 'Domain name not found on your CloudDNS account' |
|
|
|
exit 3 |
|
|
|
fi |
|
|
|
|
|
|
|
# Get challenge record ID |
|
|
|
resp=$(curl --silent "${curl_params[@]}" -X GET "$API/domain/$domain_id" ) |
|
|
|
re="\"lastDomainRecordList\".*\"id\":\"([^,]*)\"[^}]*\"name\":\"$txt_record\"," # Match domain id |
|
|
|
if [[ "${resp//[$'\t\r\n ']}" =~ $re ]]; then |
|
|
|
record_id="${BASH_REMATCH[1]}" |
|
|
|
fi |
|
|
|
|
|
|
|
if [[ -z "$record_id" ]]; then |
|
|
|
echo 'Challenge record does not exist' |
|
|
|
exit 3 |
|
|
|
fi |
|
|
|
|
|
|
|
# Remove challenge record |
|
|
|
resp=$(curl --silent "${curl_params[@]}" -X DELETE "$API/record/$record_id") |
|
|
|
|
|
|
|
# If removing record failed (error:) then print error message |
|
|
|
if [[ "${resp// }" == *'"error"'* ]]; then |
|
|
|
re='"message":"([^"]+)"' |
|
|
|
if [[ "$resp" =~ $re ]]; then |
|
|
|
echo "Error: DNS challenge not removed: ${BASH_REMATCH[1]}" |
|
|
|
exit 3 |
|
|
|
else |
|
|
|
echo "Error: DNS challenge not removed: unknown error - ${resp}" |
|
|
|
exit 3 |
|
|
|
fi |
|
|
|
fi |
|
|
|
|
|
|
|
# Publish challenge record deletion |
|
|
|
resp=$(curl --silent "${curl_params[@]}" -X PUT "$API/domain/$domain_id/publish" \ |
|
|
|
--data "{\"soaTtl\":300}") |