dns_hostingde.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. zoneConfigTemplateValues=$(echo "${curResult}" | _hostingde_parse "templateValues")
  79. if [ "$zoneConfigTemplateValues" != "null" ]; then
  80. _debug "Zone is tied to a template."
  81. zoneConfigTemplateValuesTemplateId=$(echo "${curResult}" | _hostingde_parse "templateId")
  82. zoneConfigTemplateValuesTemplateName=$(echo "${curResult}" | _hostingde_parse "templateName")
  83. zoneConfigTemplateValuesTemplateReplacementsIPv4=$(echo "${curResult}" | _hostingde_parse "ipv4Replacement")
  84. zoneConfigTemplateValuesTemplateReplacementsIPv6=$(echo "${curResult}" | _hostingde_parse "ipv6Replacement")
  85. zoneConfigTemplateValuesTemplateReplacementsMailIPv4=$(echo "${curResult}" | _hostingde_parse "mailIpv4Replacement")
  86. zoneConfigTemplateValuesTemplateReplacementsMailIPv6=$(echo "${curResult}" | _hostingde_parse "mailIpv6Replacement")
  87. zoneConfigTemplateValuesTemplateTieToTemplate=$(echo "${curResult}" | _hostingde_parse "tieToTemplate")
  88. zoneConfigTemplateValues="{\"templateId\":${zoneConfigTemplateValuesTemplateId},\"templateName\":${zoneConfigTemplateValuesTemplateName},\"templateReplacements\":{\"ipv4Replacement\":${zoneConfigTemplateValuesTemplateReplacementsIPv4},\"ipv6Replacement\":${zoneConfigTemplateValuesTemplateReplacementsIPv6},\"mailIpv4Replacement\":${zoneConfigTemplateValuesTemplateReplacementsMailIPv4},\"mailIpv6Replacement\":${zoneConfigTemplateValuesTemplateReplacementsMailIPv6}},\"tieToTemplate\":${zoneConfigTemplateValuesTemplateTieToTemplate}}"
  89. _debug "Template values: '{$zoneConfigTemplateValues}'"
  90. fi
  91. if [ "${zoneConfigType}" != "\"NATIVE\"" ]; then
  92. _err "Zone is not native"
  93. returnCode=1
  94. break
  95. fi
  96. _debug "zoneConfigId '${zoneConfigId}'"
  97. returnCode=0
  98. break
  99. fi
  100. curZone="${curZone#*.}"
  101. done
  102. if [ $returnCode -ne 0 ]; then
  103. _info "ZoneEnd reached, Zone ${curZone} not found in hosting.de API"
  104. fi
  105. return $returnCode
  106. }
  107. _hostingde_getZoneStatus() {
  108. _debug "Checking Zone status"
  109. curData="{\"filter\":{\"field\":\"zoneConfigId\",\"value\":${zoneConfigId}},\"limit\":1,\"authToken\":\"${HOSTINGDE_APIKEY}\"}"
  110. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind")"
  111. _debug "Calling zonesFind '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind'"
  112. _debug "Result of zonesFind '$curResult'"
  113. zoneStatus=$(echo "${curResult}" | _hostingde_parse "status" "success")
  114. _debug "zoneStatus '${zoneStatus}'"
  115. return 0
  116. }
  117. _hostingde_addRecord() {
  118. _info "Adding record to zone"
  119. _hostingde_getZoneStatus
  120. _debug "Result of zoneStatus: '${zoneStatus}'"
  121. while [ "${zoneStatus}" != "\"active\"" ]; do
  122. _sleep 5
  123. _hostingde_getZoneStatus
  124. _debug "Result of zoneStatus: '${zoneStatus}'"
  125. done
  126. 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}},\"templateValues\":${zoneConfigTemplateValues}},\"recordsToAdd\":[{\"name\":\"${fulldomain}\",\"type\":\"TXT\",\"content\":\"\\\"${txtvalue}\\\"\",\"ttl\":3600}]}"
  127. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  128. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  129. _debug "Result of zoneUpdate: '$curResult'"
  130. if _contains "${curResult}" '"status": "error"'; then
  131. if _contains "${curResult}" '"code": 10109'; then
  132. _err "The API-Key is invalid or could not be found"
  133. else
  134. _err "UNKNOWN API ERROR"
  135. fi
  136. return 1
  137. fi
  138. return 0
  139. }
  140. _hostingde_removeRecord() {
  141. _info "Removing record from zone"
  142. _hostingde_getZoneStatus
  143. _debug "Result of zoneStatus: '$zoneStatus'"
  144. while [ "$zoneStatus" != "\"active\"" ]; do
  145. _sleep 5
  146. _hostingde_getZoneStatus
  147. _debug "Result of zoneStatus: '$zoneStatus'"
  148. done
  149. 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}},\"templateValues\":${zoneConfigTemplateValues}},\"recordsToDelete\":[{\"name\":\"${fulldomain}\",\"type\":\"TXT\",\"content\":\"\\\"${txtvalue}\\\"\"}]}"
  150. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  151. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  152. _debug "Result of zoneUpdate: '$curResult'"
  153. if _contains "${curResult}" '"status": "error"'; then
  154. if _contains "${curResult}" '"code": 10109'; then
  155. _err "The API-Key is invalid or could not be found"
  156. else
  157. _err "UNKNOWN API ERROR"
  158. fi
  159. return 1
  160. fi
  161. return 0
  162. }