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.

110 lines
3.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_addRecord() {
  67. _info "Adding record to zone"
  68. curData="{\"authToken\":\"${HOSTINGDE_APIKEY}\",\"zoneConfig\":{\"id\":\"${zoneConfigId}\"},\"recordsToAdd\":[{\"name\":\"${fulldomain}\",\"type\":\"TXT\",\"content\":\"\\\"${txtvalue}\\\"\",\"ttl\":3600}]}"
  69. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  70. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  71. _debug "Result of zoneUpdate: '$curResult'"
  72. if _contains "${curResult}" '"status": "error"'; then
  73. if _contains "${curResult}" '"code": 10109'; then
  74. _err "The API-Key is invalid or could not be found"
  75. else
  76. _err "UNKNOWN API ERROR"
  77. fi
  78. return 1
  79. fi
  80. return 0
  81. }
  82. _hostingde_removeRecord() {
  83. _info "Removing record from zone"
  84. curData="{\"authToken\":\"${HOSTINGDE_APIKEY}\",\"zoneConfig\":{\"id\":\"${zoneConfigId}\"},\"recordsToDelete\":[{\"name\":\"${fulldomain}\",\"type\":\"TXT\",\"content\":\"\\\"${txtvalue}\\\"\"}]}"
  85. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  86. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  87. _debug "Result of zoneUpdate: '$curResult'"
  88. if _contains "${curResult}" '"status": "error"'; then
  89. if _contains "${curResult}" '"code": 10109'; then
  90. _err "The API-Key is invalid or could not be found"
  91. else
  92. _err "UNKNOWN API ERROR"
  93. fi
  94. return 1
  95. fi
  96. return 0
  97. }