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.

190 lines
8.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_parse_no_strip_whitespace() {
  47. find="${1}"
  48. if [ "${2}" ]; then
  49. notfind="${2}"
  50. fi
  51. if [ "${notfind}" ]; then
  52. _egrep_o \""${find}\":.*" | grep -v "${notfind}" | cut -d ':' -f 2 | cut -d ',' -f 1
  53. else
  54. _egrep_o \""${find}\":.*" | cut -d ':' -f 2 | cut -d ',' -f 1
  55. fi
  56. }
  57. _hostingde_getZoneConfig() {
  58. _info "Getting ZoneConfig"
  59. curZone="${fulldomain#*.}"
  60. returnCode=1
  61. while _contains "${curZone}" "\\."; do
  62. curData="{\"filter\":{\"field\":\"zoneName\",\"value\":\"${curZone}\"},\"limit\":1,\"authToken\":\"${HOSTINGDE_APIKEY}\"}"
  63. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneConfigsFind")"
  64. _debug "Calling zoneConfigsFind: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneConfigsFind'"
  65. _debug "Result of zoneConfigsFind: '$curResult'"
  66. if _contains "${curResult}" '"status": "error"'; then
  67. if _contains "${curResult}" '"code": 10109'; then
  68. _err "The API-Key is invalid or could not be found"
  69. else
  70. _err "UNKNOWN API ERROR"
  71. fi
  72. returnCode=1
  73. break
  74. fi
  75. if _contains "${curResult}" '"totalEntries": 1'; then
  76. _info "Retrieved zone data."
  77. _debug "Zone data: '${curResult}'"
  78. zoneConfigId=$(echo "${curResult}" | _hostingde_parse "id")
  79. zoneConfigName=$(echo "${curResult}" | _hostingde_parse "name")
  80. zoneConfigType=$(echo "${curResult}" | _hostingde_parse "type" "FindZoneConfigsResult")
  81. zoneConfigExpire=$(echo "${curResult}" | _hostingde_parse "expire")
  82. zoneConfigNegativeTtl=$(echo "${curResult}" | _hostingde_parse "negativeTtl")
  83. zoneConfigRefresh=$(echo "${curResult}" | _hostingde_parse "refresh")
  84. zoneConfigRetry=$(echo "${curResult}" | _hostingde_parse "retry")
  85. zoneConfigTtl=$(echo "${curResult}" | _hostingde_parse "ttl")
  86. zoneConfigDnsServerGroupId=$(echo "${curResult}" | _hostingde_parse "dnsServerGroupId")
  87. zoneConfigEmailAddress=$(echo "${curResult}" | _hostingde_parse "emailAddress")
  88. zoneConfigDnsSecMode=$(echo "${curResult}" | _hostingde_parse "dnsSecMode")
  89. zoneConfigTemplateValues=$(echo "${curResult}" | _hostingde_parse_no_strip_whitespace "templateValues")
  90. if [ "$zoneConfigTemplateValues" != "null" ]; then
  91. _debug "Zone is tied to a template."
  92. zoneConfigTemplateValuesTemplateId=$(echo "${curResult}" | _hostingde_parse "templateId")
  93. zoneConfigTemplateValuesTemplateName=$(echo "${curResult}" | _hostingde_parse_no_strip_whitespace "templateName")
  94. zoneConfigTemplateValuesTemplateReplacementsIPv4=$(echo "${curResult}" | _hostingde_parse "ipv4Replacement")
  95. zoneConfigTemplateValuesTemplateReplacementsIPv6=$(echo "${curResult}" | _hostingde_parse "ipv6Replacement")
  96. zoneConfigTemplateValuesTemplateReplacementsMailIPv4=$(echo "${curResult}" | _hostingde_parse "mailIpv4Replacement")
  97. zoneConfigTemplateValuesTemplateReplacementsMailIPv6=$(echo "${curResult}" | _hostingde_parse "mailIpv6Replacement")
  98. zoneConfigTemplateValuesTemplateTieToTemplate=$(echo "${curResult}" | _hostingde_parse "tieToTemplate")
  99. zoneConfigTemplateValues="{\"templateId\":${zoneConfigTemplateValuesTemplateId},\"templateName\":${zoneConfigTemplateValuesTemplateName},\"templateReplacements\":{\"ipv4Replacement\":${zoneConfigTemplateValuesTemplateReplacementsIPv4},\"ipv6Replacement\":${zoneConfigTemplateValuesTemplateReplacementsIPv6},\"mailIpv4Replacement\":${zoneConfigTemplateValuesTemplateReplacementsMailIPv4},\"mailIpv6Replacement\":${zoneConfigTemplateValuesTemplateReplacementsMailIPv6}},\"tieToTemplate\":${zoneConfigTemplateValuesTemplateTieToTemplate}}"
  100. _debug "Template values: '{$zoneConfigTemplateValues}'"
  101. fi
  102. if [ "${zoneConfigType}" != "\"NATIVE\"" ]; then
  103. _err "Zone is not native"
  104. returnCode=1
  105. break
  106. fi
  107. _debug "zoneConfigId '${zoneConfigId}'"
  108. returnCode=0
  109. break
  110. fi
  111. curZone="${curZone#*.}"
  112. done
  113. if [ $returnCode -ne 0 ]; then
  114. _info "ZoneEnd reached, Zone ${curZone} not found in hosting.de API"
  115. fi
  116. return $returnCode
  117. }
  118. _hostingde_getZoneStatus() {
  119. _debug "Checking Zone status"
  120. curData="{\"filter\":{\"field\":\"zoneConfigId\",\"value\":${zoneConfigId}},\"limit\":1,\"authToken\":\"${HOSTINGDE_APIKEY}\"}"
  121. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind")"
  122. _debug "Calling zonesFind '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind'"
  123. _debug "Result of zonesFind '$curResult'"
  124. zoneStatus=$(echo "${curResult}" | _hostingde_parse "status" "success")
  125. _debug "zoneStatus '${zoneStatus}'"
  126. return 0
  127. }
  128. _hostingde_addRecord() {
  129. _info "Adding record to zone"
  130. _hostingde_getZoneStatus
  131. _debug "Result of zoneStatus: '${zoneStatus}'"
  132. while [ "${zoneStatus}" != "\"active\"" ]; do
  133. _sleep 5
  134. _hostingde_getZoneStatus
  135. _debug "Result of zoneStatus: '${zoneStatus}'"
  136. done
  137. 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}]}"
  138. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  139. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  140. _debug "Result of zoneUpdate: '$curResult'"
  141. if _contains "${curResult}" '"status": "error"'; then
  142. if _contains "${curResult}" '"code": 10109'; then
  143. _err "The API-Key is invalid or could not be found"
  144. else
  145. _err "UNKNOWN API ERROR"
  146. fi
  147. return 1
  148. fi
  149. return 0
  150. }
  151. _hostingde_removeRecord() {
  152. _info "Removing record from zone"
  153. _hostingde_getZoneStatus
  154. _debug "Result of zoneStatus: '$zoneStatus'"
  155. while [ "$zoneStatus" != "\"active\"" ]; do
  156. _sleep 5
  157. _hostingde_getZoneStatus
  158. _debug "Result of zoneStatus: '$zoneStatus'"
  159. done
  160. 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}\\\"\"}]}"
  161. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  162. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  163. _debug "Result of zoneUpdate: '$curResult'"
  164. if _contains "${curResult}" '"status": "error"'; then
  165. if _contains "${curResult}" '"code": 10109'; then
  166. _err "The API-Key is invalid or could not be found"
  167. else
  168. _err "UNKNOWN API ERROR"
  169. fi
  170. return 1
  171. fi
  172. return 0
  173. }