You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
6.7 KiB

  1. #!/usr/bin/env sh
  2. # hosting.de API
  3. # Values to export:
  4. # export HOSTINGDE_ENDPOINT='https://secure.hosting.de'
  5. # export HOSTINGDE_APIKEY='xxxxx'
  6. ######## Public functions #####################
  7. dns_hostingde_add() {
  8. fulldomain="${1}"
  9. txtvalue="${2}"
  10. _debug "Calling: _hostingde_addRecord() '${fulldomain}' '${txtvalue}'"
  11. _hostingde_apiKey && _hostingde_getZoneConfig && _hostingde_addRecord
  12. return $?
  13. }
  14. dns_hostingde_rm() {
  15. fulldomain="${1}"
  16. txtvalue="${2}"
  17. _debug "Calling: _hostingde_removeRecord() '${fulldomain}' '${txtvalue}'"
  18. _hostingde_apiKey && _hostingde_getZoneConfig && _hostingde_removeRecord
  19. return $?
  20. }
  21. #################### own Private functions below ##################################
  22. _hostingde_apiKey() {
  23. HOSTINGDE_APIKEY="${HOSTINGDE_APIKEY:-$(_readaccountconf_mutable HOSTINGDE_APIKEY)}"
  24. if [ -z "$HOSTINGDE_APIKEY" ] || [ -z "$HOSTINGDE_ENDPOINT" ]; then
  25. HOSTINGDE_APIKEY=""
  26. HOSTINGDE_ENDPOINT=""
  27. _err "You haven't specified hosting.de API key or endpoint yet."
  28. _err "Please create your key and try again."
  29. return 1
  30. fi
  31. _saveaccountconf_mutable HOSTINGDE_APIKEY "$HOSTINGDE_APIKEY"
  32. _saveaccountconf_mutable HOSTINGDE_ENDPOINT "$HOSTINGDE_ENDPOINT"
  33. }
  34. _hostingde_parse() {
  35. find="${1}"
  36. if [ "${2}" ]; then
  37. notfind="${2}"
  38. fi
  39. if [ "${notfind}" ]; then
  40. _egrep_o \""${find}\":.*" | grep -v "${notfind}" | cut -d ':' -f 2 | cut -d ',' -f 1 | tr -d ' '
  41. else
  42. _egrep_o \""${find}\":.*" | cut -d ':' -f 2 | cut -d ',' -f 1 | tr -d ' '
  43. fi
  44. }
  45. _hostingde_getZoneConfig() {
  46. _info "Getting ZoneConfig"
  47. curZone="${fulldomain#*.}"
  48. returnCode=1
  49. while _contains "${curZone}" "\\."; do
  50. curData="{\"filter\":{\"field\":\"zoneName\",\"value\":\"${curZone}\"},\"limit\":1,\"authToken\":\"${HOSTINGDE_APIKEY}\"}"
  51. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneConfigsFind")"
  52. _debug "Calling zoneConfigsFind: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneConfigsFind'"
  53. _debug "Result of zoneConfigsFind: '$curResult'"
  54. if _contains "${curResult}" '"status": "error"'; then
  55. if _contains "${curResult}" '"code": 10109'; then
  56. _err "The API-Key is invalid or could not be found"
  57. else
  58. _err "UNKNOWN API ERROR"
  59. fi
  60. returnCode=1
  61. break
  62. fi
  63. if _contains "${curResult}" '"totalEntries": 1'; then
  64. _info "Retrieved zone data."
  65. _debug "Zone data: '${curResult}'"
  66. zoneConfigId=$(echo "${curResult}" | _hostingde_parse "id")
  67. zoneConfigName=$(echo "${curResult}" | _hostingde_parse "name")
  68. zoneConfigType=$(echo "${curResult}" | _hostingde_parse "type" "FindZoneConfigsResult")
  69. zoneConfigExpire=$(echo "${curResult}" | _hostingde_parse "expire")
  70. zoneConfigNegativeTtl=$(echo "${curResult}" | _hostingde_parse "negativeTtl")
  71. zoneConfigRefresh=$(echo "${curResult}" | _hostingde_parse "refresh")
  72. zoneConfigRetry=$(echo "${curResult}" | _hostingde_parse "retry")
  73. zoneConfigTtl=$(echo "${curResult}" | _hostingde_parse "ttl")
  74. zoneConfigDnsServerGroupId=$(echo "${curResult}" | _hostingde_parse "dnsServerGroupId")
  75. zoneConfigEmailAddress=$(echo "${curResult}" | _hostingde_parse "emailAddress")
  76. zoneConfigDnsSecMode=$(echo "${curResult}" | _hostingde_parse "dnsSecMode")
  77. if [ "${zoneConfigType}" != "\"NATIVE\"" ]; then
  78. _err "Zone is not native"
  79. returnCode=1
  80. break
  81. fi
  82. _debug "zoneConfigId '${zoneConfigId}'"
  83. returnCode=0
  84. break
  85. fi
  86. curZone="${curZone#*.}"
  87. done
  88. if [ $returnCode -ne 0 ]; then
  89. _info "ZoneEnd reached, Zone ${curZone} not found in hosting.de API"
  90. fi
  91. return $returnCode
  92. }
  93. _hostingde_getZoneStatus() {
  94. _debug "Checking Zone status"
  95. curData="{\"filter\":{\"field\":\"zoneConfigId\",\"value\":${zoneConfigId}},\"limit\":1,\"authToken\":\"${HOSTINGDE_APIKEY}\"}"
  96. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind")"
  97. _debug "Calling zonesFind '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind'"
  98. _debug "Result of zonesFind '$curResult'"
  99. zoneStatus=$(echo "${curResult}" | _hostingde_parse "status" "success")
  100. _debug "zoneStatus '${zoneStatus}'"
  101. return 0
  102. }
  103. _hostingde_addRecord() {
  104. _info "Adding record to zone"
  105. _hostingde_getZoneStatus
  106. _debug "Result of zoneStatus: '${zoneStatus}'"
  107. while [ "${zoneStatus}" != "\"active\"" ]; do
  108. _sleep 5
  109. _hostingde_getZoneStatus
  110. _debug "Result of zoneStatus: '${zoneStatus}'"
  111. done
  112. 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}]}"
  113. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  114. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  115. _debug "Result of zoneUpdate: '$curResult'"
  116. if _contains "${curResult}" '"status": "error"'; then
  117. if _contains "${curResult}" '"code": 10109'; then
  118. _err "The API-Key is invalid or could not be found"
  119. else
  120. _err "UNKNOWN API ERROR"
  121. fi
  122. return 1
  123. fi
  124. return 0
  125. }
  126. _hostingde_removeRecord() {
  127. _info "Removing record from zone"
  128. _hostingde_getZoneStatus
  129. _debug "Result of zoneStatus: '$zoneStatus'"
  130. while [ "$zoneStatus" != "\"active\"" ]; do
  131. _sleep 5
  132. _hostingde_getZoneStatus
  133. _debug "Result of zoneStatus: '$zoneStatus'"
  134. done
  135. 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}\\\"\"}]}"
  136. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  137. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  138. _debug "Result of zoneUpdate: '$curResult'"
  139. if _contains "${curResult}" '"status": "error"'; then
  140. if _contains "${curResult}" '"code": 10109'; then
  141. _err "The API-Key is invalid or could not be found"
  142. else
  143. _err "UNKNOWN API ERROR"
  144. fi
  145. return 1
  146. fi
  147. return 0
  148. }