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.

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