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.

147 lines
6.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. zoneConfigId=$(echo "${curResult}" | _egrep_o '"id":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  54. zoneConfigName=$(echo "${curResult}" | _egrep_o '"name":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  55. zoneConfigType=$(echo "${curResult}" | grep -v "FindZoneConfigsResult" | _egrep_o '"type":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  56. zoneConfigExpire=$(echo "${curResult}" | _egrep_o '"expire":.*' | cut -d ':' -f 2 | cut -d '"' -f 2 | cut -d ',' -f 1)
  57. zoneConfigNegativeTtl=$(echo "${curResult}" | _egrep_o '"negativeTtl":.*' | cut -d ':' -f 2 | cut -d '"' -f 2 | cut -d ',' -f 1)
  58. zoneConfigRefresh=$(echo "${curResult}" | _egrep_o '"refresh":.*' | cut -d ':' -f 2 | cut -d '"' -f 2 | cut -d ',' -f 1)
  59. zoneConfigRetry=$(echo "${curResult}" | _egrep_o '"retry":.*' | cut -d ':' -f 2 | cut -d '"' -f 2 | cut -d ',' -f 1)
  60. zoneConfigTtl=$(echo "${curResult}" | _egrep_o '"ttl":.*' | cut -d ':' -f 2 | cut -d '"' -f 2 | cut -d ',' -f 1)
  61. zoneConfigDnsServerGroupId=$(echo "${curResult}" | _egrep_o '"dnsServerGroupId":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  62. zoneConfigEmailAddress=$(echo "${curResult}" | _egrep_o '"emailAddress":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  63. zoneConfigDnsSecMode=$(echo "${curResult}" | _egrep_o '"dnsSecMode":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  64. if [ "${zoneConfigType}" != "NATIVE" ]; then
  65. _err "Zone is not native"
  66. returnCode=1
  67. break
  68. fi
  69. _debug "zoneConfigId '${zoneConfigId}'"
  70. returnCode=0
  71. break
  72. fi
  73. curZone="${curZone#*.}"
  74. done
  75. if [ $returnCode -ne 0 ]; then
  76. _info "ZoneEnd reached, Zone ${curZone} not found in hosting.de API"
  77. fi
  78. return $returnCode
  79. }
  80. _hostingde_getZoneStatus() {
  81. _debug "Checking Zone status"
  82. curData="{\"filter\":{\"field\":\"zoneConfigId\",\"value\":\"${zoneConfigId}\"},\"limit\":1,\"authToken\":\"${HOSTINGDE_APIKEY}\"}"
  83. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind")"
  84. _debug "Calling zonesFind '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind'"
  85. _debug "Result of zonesFind '$curResult'"
  86. zoneStatus=$(echo "${curResult}" | grep -v success | _egrep_o '"status":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  87. _debug "zoneStatus '${zoneStatus}'"
  88. return 0
  89. }
  90. _hostingde_addRecord() {
  91. _info "Adding record to zone"
  92. _hostingde_getZoneStatus
  93. _debug "Result of zoneStatus: '${zoneStatus}'"
  94. while [ "${zoneStatus}" != "active" ]; do
  95. _sleep 5
  96. _hostingde_getZoneStatus
  97. _debug "Result of zoneStatus: '${zoneStatus}'"
  98. done
  99. 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}]}"
  100. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  101. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  102. _debug "Result of zoneUpdate: '$curResult'"
  103. if _contains "${curResult}" '"status": "error"'; then
  104. if _contains "${curResult}" '"code": 10109'; then
  105. _err "The API-Key is invalid or could not be found"
  106. else
  107. _err "UNKNOWN API ERROR"
  108. fi
  109. return 1
  110. fi
  111. return 0
  112. }
  113. _hostingde_removeRecord() {
  114. _info "Removing record from zone"
  115. _hostingde_getZoneStatus
  116. _debug "Result of zoneStatus: '$zoneStatus'"
  117. while [ "$zoneStatus" != "active" ]; do
  118. _sleep 5
  119. _hostingde_getZoneStatus
  120. _debug "Result of zoneStatus: '$zoneStatus'"
  121. done
  122. 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}\\\"\"}]}"
  123. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  124. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  125. _debug "Result of zoneUpdate: '$curResult'"
  126. if _contains "${curResult}" '"status": "error"'; then
  127. if _contains "${curResult}" '"code": 10109'; then
  128. _err "The API-Key is invalid or could not be found"
  129. else
  130. _err "UNKNOWN API ERROR"
  131. fi
  132. return 1
  133. fi
  134. return 0
  135. }