2018-08-01 16:37:08 +02:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# hosting.de API
|
|
|
|
|
|
|
|
# Values to export:
|
|
|
|
# export HOSTINGDE_ENDPOINT='https://secure.hosting.de'
|
|
|
|
# export HOSTINGDE_APIKEY='xxxxx'
|
|
|
|
|
|
|
|
######## Public functions #####################
|
|
|
|
|
|
|
|
dns_hostingde_add() {
|
|
|
|
fulldomain="${1}"
|
|
|
|
txtvalue="${2}"
|
|
|
|
_debug "Calling: _hostingde_addRecord() '${fulldomain}' '${txtvalue}'"
|
|
|
|
_hostingde_apiKey && _hostingde_getZoneConfig && _hostingde_addRecord
|
2019-02-04 11:27:04 +01:00
|
|
|
return $?
|
2018-08-01 16:37:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dns_hostingde_rm() {
|
|
|
|
fulldomain="${1}"
|
|
|
|
txtvalue="${2}"
|
|
|
|
_debug "Calling: _hostingde_removeRecord() '${fulldomain}' '${txtvalue}'"
|
|
|
|
_hostingde_apiKey && _hostingde_getZoneConfig && _hostingde_removeRecord
|
2019-02-04 11:27:04 +01:00
|
|
|
return $?
|
2018-08-01 16:37:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#################### own Private functions below ##################################
|
|
|
|
|
|
|
|
_hostingde_apiKey() {
|
|
|
|
HOSTINGDE_APIKEY="${HOSTINGDE_APIKEY:-$(_readaccountconf_mutable HOSTINGDE_APIKEY)}"
|
|
|
|
if [ -z "$HOSTINGDE_APIKEY" ] || [ -z "$HOSTINGDE_ENDPOINT" ]; then
|
|
|
|
HOSTINGDE_APIKEY=""
|
|
|
|
HOSTINGDE_ENDPOINT=""
|
|
|
|
_err "You haven't specified hosting.de API key or endpoint yet."
|
|
|
|
_err "Please create your key and try again."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_saveaccountconf_mutable HOSTINGDE_APIKEY "$HOSTINGDE_APIKEY"
|
|
|
|
_saveaccountconf_mutable HOSTINGDE_ENDPOINT "$HOSTINGDE_ENDPOINT"
|
|
|
|
}
|
|
|
|
|
2019-02-04 11:27:04 +01:00
|
|
|
_hostingde_parse() {
|
|
|
|
find="${1}"
|
|
|
|
if [ "${2}" ]; then
|
|
|
|
notfind="${2}"
|
|
|
|
fi
|
|
|
|
if [ "${notfind}" ]; then
|
2019-02-11 11:47:48 +01:00
|
|
|
_egrep_o \""${find}\":.*" | grep -v "${notfind}" | cut -d ':' -f 2 | cut -d ',' -f 1 | tr -d ' '
|
2019-02-04 11:27:04 +01:00
|
|
|
else
|
2019-02-11 11:47:48 +01:00
|
|
|
_egrep_o \""${find}\":.*" | cut -d ':' -f 2 | cut -d ',' -f 1 | tr -d ' '
|
2019-02-04 11:27:04 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-08-01 16:37:08 +02:00
|
|
|
_hostingde_getZoneConfig() {
|
|
|
|
_info "Getting ZoneConfig"
|
|
|
|
curZone="${fulldomain#*.}"
|
|
|
|
returnCode=1
|
|
|
|
while _contains "${curZone}" "\\."; do
|
|
|
|
curData="{\"filter\":{\"field\":\"zoneName\",\"value\":\"${curZone}\"},\"limit\":1,\"authToken\":\"${HOSTINGDE_APIKEY}\"}"
|
|
|
|
curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneConfigsFind")"
|
|
|
|
_debug "Calling zoneConfigsFind: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneConfigsFind'"
|
|
|
|
_debug "Result of zoneConfigsFind: '$curResult'"
|
|
|
|
if _contains "${curResult}" '"status": "error"'; then
|
|
|
|
if _contains "${curResult}" '"code": 10109'; then
|
|
|
|
_err "The API-Key is invalid or could not be found"
|
|
|
|
else
|
|
|
|
_err "UNKNOWN API ERROR"
|
|
|
|
fi
|
|
|
|
returnCode=1
|
2018-08-01 17:00:22 +02:00
|
|
|
break
|
2018-08-01 16:37:08 +02:00
|
|
|
fi
|
|
|
|
if _contains "${curResult}" '"totalEntries": 1'; then
|
|
|
|
_info "Retrieved zone data."
|
|
|
|
_debug "Zone data: '${curResult}'"
|
2019-02-04 11:27:04 +01:00
|
|
|
zoneConfigId=$(echo "${curResult}" | _hostingde_parse "id")
|
|
|
|
zoneConfigName=$(echo "${curResult}" | _hostingde_parse "name")
|
|
|
|
zoneConfigType=$(echo "${curResult}" | _hostingde_parse "type" "FindZoneConfigsResult")
|
|
|
|
zoneConfigExpire=$(echo "${curResult}" | _hostingde_parse "expire")
|
|
|
|
zoneConfigNegativeTtl=$(echo "${curResult}" | _hostingde_parse "negativeTtl")
|
|
|
|
zoneConfigRefresh=$(echo "${curResult}" | _hostingde_parse "refresh")
|
|
|
|
zoneConfigRetry=$(echo "${curResult}" | _hostingde_parse "retry")
|
|
|
|
zoneConfigTtl=$(echo "${curResult}" | _hostingde_parse "ttl")
|
|
|
|
zoneConfigDnsServerGroupId=$(echo "${curResult}" | _hostingde_parse "dnsServerGroupId")
|
|
|
|
zoneConfigEmailAddress=$(echo "${curResult}" | _hostingde_parse "emailAddress")
|
|
|
|
zoneConfigDnsSecMode=$(echo "${curResult}" | _hostingde_parse "dnsSecMode")
|
2019-02-04 15:40:45 +01:00
|
|
|
if [ "${zoneConfigType}" != "\"NATIVE\"" ]; then
|
2019-01-21 15:02:09 +01:00
|
|
|
_err "Zone is not native"
|
|
|
|
returnCode=1
|
|
|
|
break
|
|
|
|
fi
|
2018-08-01 16:37:08 +02:00
|
|
|
_debug "zoneConfigId '${zoneConfigId}'"
|
|
|
|
returnCode=0
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
curZone="${curZone#*.}"
|
|
|
|
done
|
|
|
|
if [ $returnCode -ne 0 ]; then
|
2018-08-01 17:00:22 +02:00
|
|
|
_info "ZoneEnd reached, Zone ${curZone} not found in hosting.de API"
|
2018-08-01 16:37:08 +02:00
|
|
|
fi
|
|
|
|
return $returnCode
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:44:11 +01:00
|
|
|
_hostingde_getZoneStatus() {
|
|
|
|
_debug "Checking Zone status"
|
2019-02-04 11:27:04 +01:00
|
|
|
curData="{\"filter\":{\"field\":\"zoneConfigId\",\"value\":${zoneConfigId}},\"limit\":1,\"authToken\":\"${HOSTINGDE_APIKEY}\"}"
|
2019-01-02 16:44:11 +01:00
|
|
|
curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind")"
|
|
|
|
_debug "Calling zonesFind '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind'"
|
|
|
|
_debug "Result of zonesFind '$curResult'"
|
2019-02-04 11:27:04 +01:00
|
|
|
zoneStatus=$(echo "${curResult}" | _hostingde_parse "status" "success")
|
2019-01-02 16:44:11 +01:00
|
|
|
_debug "zoneStatus '${zoneStatus}'"
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-08-01 16:37:08 +02:00
|
|
|
_hostingde_addRecord() {
|
|
|
|
_info "Adding record to zone"
|
2019-01-02 16:44:11 +01:00
|
|
|
_hostingde_getZoneStatus
|
|
|
|
_debug "Result of zoneStatus: '${zoneStatus}'"
|
2019-02-04 11:27:04 +01:00
|
|
|
while [ "${zoneStatus}" != "\"active\"" ]; do
|
2019-01-03 10:32:59 +01:00
|
|
|
_sleep 5
|
2019-01-02 16:44:11 +01:00
|
|
|
_hostingde_getZoneStatus
|
|
|
|
_debug "Result of zoneStatus: '${zoneStatus}'"
|
|
|
|
done
|
2019-02-04 11:27:04 +01:00
|
|
|
curData="{\"authToken\":\"${HOSTINGDE_APIKEY}\",\"zoneConfig\":{\"id\":${zoneConfigId},\"name\":${zoneConfigName},\"type\":${zoneConfigType},\"dnsServerGroupId\":${zoneConfigDnsServerGroupId},\"dnsSecMode\":${zoneConfigDnsSecMode},\"emailAddress\":${zoneConfigEmailAddress},\"soaValues\":{\"expire\":${zoneConfigExpire},\"negativeTtl\":${zoneConfigNegativeTtl},\"refresh\":${zoneConfigRefresh},\"retry\":${zoneConfigRetry},\"ttl\":${zoneConfigTtl}}},\"recordsToAdd\":[{\"name\":\"${fulldomain}\",\"type\":\"TXT\",\"content\":\"\\\"${txtvalue}\\\"\",\"ttl\":3600}]}"
|
2018-08-01 16:37:08 +02:00
|
|
|
curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
|
|
|
|
_debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
|
|
|
|
_debug "Result of zoneUpdate: '$curResult'"
|
|
|
|
if _contains "${curResult}" '"status": "error"'; then
|
|
|
|
if _contains "${curResult}" '"code": 10109'; then
|
|
|
|
_err "The API-Key is invalid or could not be found"
|
|
|
|
else
|
|
|
|
_err "UNKNOWN API ERROR"
|
|
|
|
fi
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
_hostingde_removeRecord() {
|
|
|
|
_info "Removing record from zone"
|
2019-01-02 16:44:11 +01:00
|
|
|
_hostingde_getZoneStatus
|
|
|
|
_debug "Result of zoneStatus: '$zoneStatus'"
|
2019-02-04 11:27:04 +01:00
|
|
|
while [ "$zoneStatus" != "\"active\"" ]; do
|
2019-01-03 10:32:59 +01:00
|
|
|
_sleep 5
|
2019-01-02 16:44:11 +01:00
|
|
|
_hostingde_getZoneStatus
|
|
|
|
_debug "Result of zoneStatus: '$zoneStatus'"
|
|
|
|
done
|
2019-02-04 11:27:04 +01:00
|
|
|
curData="{\"authToken\":\"${HOSTINGDE_APIKEY}\",\"zoneConfig\":{\"id\":${zoneConfigId},\"name\":${zoneConfigName},\"type\":${zoneConfigType},\"dnsServerGroupId\":${zoneConfigDnsServerGroupId},\"dnsSecMode\":${zoneConfigDnsSecMode},\"emailAddress\":${zoneConfigEmailAddress},\"soaValues\":{\"expire\":${zoneConfigExpire},\"negativeTtl\":${zoneConfigNegativeTtl},\"refresh\":${zoneConfigRefresh},\"retry\":${zoneConfigRetry},\"ttl\":${zoneConfigTtl}}},\"recordsToDelete\":[{\"name\":\"${fulldomain}\",\"type\":\"TXT\",\"content\":\"\\\"${txtvalue}\\\"\"}]}"
|
2018-08-01 16:37:08 +02:00
|
|
|
curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
|
|
|
|
_debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
|
|
|
|
_debug "Result of zoneUpdate: '$curResult'"
|
|
|
|
if _contains "${curResult}" '"status": "error"'; then
|
|
|
|
if _contains "${curResult}" '"code": 10109'; then
|
|
|
|
_err "The API-Key is invalid or could not be found"
|
|
|
|
else
|
|
|
|
_err "UNKNOWN API ERROR"
|
|
|
|
fi
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|