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.

134 lines
4.9 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. }
  13. dns_hostingde_rm() {
  14. fulldomain="${1}"
  15. txtvalue="${2}"
  16. _debug "Calling: _hostingde_removeRecord() '${fulldomain}' '${txtvalue}'"
  17. _hostingde_apiKey && _hostingde_getZoneConfig && _hostingde_removeRecord
  18. }
  19. #################### own Private functions below ##################################
  20. _hostingde_apiKey() {
  21. HOSTINGDE_APIKEY="${HOSTINGDE_APIKEY:-$(_readaccountconf_mutable HOSTINGDE_APIKEY)}"
  22. if [ -z "$HOSTINGDE_APIKEY" ] || [ -z "$HOSTINGDE_ENDPOINT" ]; then
  23. HOSTINGDE_APIKEY=""
  24. HOSTINGDE_ENDPOINT=""
  25. _err "You haven't specified hosting.de API key or endpoint yet."
  26. _err "Please create your key and try again."
  27. return 1
  28. fi
  29. _saveaccountconf_mutable HOSTINGDE_APIKEY "$HOSTINGDE_APIKEY"
  30. _saveaccountconf_mutable HOSTINGDE_ENDPOINT "$HOSTINGDE_ENDPOINT"
  31. }
  32. _hostingde_getZoneConfig() {
  33. _info "Getting ZoneConfig"
  34. curZone="${fulldomain#*.}"
  35. returnCode=1
  36. while _contains "${curZone}" "\\."; do
  37. curData="{\"filter\":{\"field\":\"zoneName\",\"value\":\"${curZone}\"},\"limit\":1,\"authToken\":\"${HOSTINGDE_APIKEY}\"}"
  38. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneConfigsFind")"
  39. _debug "Calling zoneConfigsFind: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneConfigsFind'"
  40. _debug "Result of zoneConfigsFind: '$curResult'"
  41. if _contains "${curResult}" '"status": "error"'; then
  42. if _contains "${curResult}" '"code": 10109'; then
  43. _err "The API-Key is invalid or could not be found"
  44. else
  45. _err "UNKNOWN API ERROR"
  46. fi
  47. returnCode=1
  48. break
  49. fi
  50. if _contains "${curResult}" '"totalEntries": 1'; then
  51. _info "Retrieved zone data."
  52. _debug "Zone data: '${curResult}'"
  53. # read ZoneConfigId for later update
  54. zoneConfigId=$(echo "${curResult}" | _egrep_o '"id":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  55. _debug "zoneConfigId '${zoneConfigId}'"
  56. returnCode=0
  57. break
  58. fi
  59. curZone="${curZone#*.}"
  60. done
  61. if [ $returnCode -ne 0 ]; then
  62. _info "ZoneEnd reached, Zone ${curZone} not found in hosting.de API"
  63. fi
  64. return $returnCode
  65. }
  66. _hostingde_getZoneStatus() {
  67. _debug "Checking Zone status"
  68. curData="{\"filter\":{\"field\":\"zoneConfigId\",\"value\":\"${zoneConfigId}\"},\"limit\":1,\"authToken\":\"${HOSTINGDE_APIKEY}\"}"
  69. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind")"
  70. _debug "Calling zonesFind '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind'"
  71. _debug "Result of zonesFind '$curResult'"
  72. zoneStatus=$(echo "${curResult}" | grep -v success | _egrep_o '"status":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  73. _debug "zoneStatus '${zoneStatus}'"
  74. return 0
  75. }
  76. _hostingde_addRecord() {
  77. _info "Adding record to zone"
  78. _hostingde_getZoneStatus
  79. _debug "Result of zoneStatus: '${zoneStatus}'"
  80. while [ "${zoneStatus}" != "active" ]; do
  81. sleep 5
  82. _hostingde_getZoneStatus
  83. _debug "Result of zoneStatus: '${zoneStatus}'"
  84. done
  85. curData="{\"authToken\":\"${HOSTINGDE_APIKEY}\",\"zoneConfig\":{\"id\":\"${zoneConfigId}\"},\"recordsToAdd\":[{\"name\":\"${fulldomain}\",\"type\":\"TXT\",\"content\":\"\\\"${txtvalue}\\\"\",\"ttl\":3600}]}"
  86. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  87. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  88. _debug "Result of zoneUpdate: '$curResult'"
  89. if _contains "${curResult}" '"status": "error"'; then
  90. if _contains "${curResult}" '"code": 10109'; then
  91. _err "The API-Key is invalid or could not be found"
  92. else
  93. _err "UNKNOWN API ERROR"
  94. fi
  95. return 1
  96. fi
  97. return 0
  98. }
  99. _hostingde_removeRecord() {
  100. _info "Removing record from zone"
  101. _hostingde_getZoneStatus
  102. _debug "Result of zoneStatus: '$zoneStatus'"
  103. while [ "$zoneStatus" != "active" ]; do
  104. sleep 5
  105. _hostingde_getZoneStatus
  106. _debug "Result of zoneStatus: '$zoneStatus'"
  107. done
  108. curData="{\"authToken\":\"${HOSTINGDE_APIKEY}\",\"zoneConfig\":{\"id\":\"${zoneConfigId}\"},\"recordsToDelete\":[{\"name\":\"${fulldomain}\",\"type\":\"TXT\",\"content\":\"\\\"${txtvalue}\\\"\"}]}"
  109. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  110. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  111. _debug "Result of zoneUpdate: '$curResult'"
  112. if _contains "${curResult}" '"status": "error"'; then
  113. if _contains "${curResult}" '"code": 10109'; then
  114. _err "The API-Key is invalid or could not be found"
  115. else
  116. _err "UNKNOWN API ERROR"
  117. fi
  118. return 1
  119. fi
  120. return 0
  121. }