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.

181 lines
8.6 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_hostingde_info='Hosting.de
  4. Site: Hosting.de
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_hostingde
  6. Options:
  7. HOSTINGDE_ENDPOINT Endpoint. E.g. "https://secure.hosting.de"
  8. HOSTINGDE_APIKEY API Key
  9. Issues: github.com/acmesh-official/acme.sh/issues/2058
  10. '
  11. ######## Public functions #####################
  12. dns_hostingde_add() {
  13. fulldomain="${1}"
  14. txtvalue="${2}"
  15. _debug "Calling: _hostingde_addRecord() '${fulldomain}' '${txtvalue}'"
  16. _hostingde_apiKey && _hostingde_getZoneConfig && _hostingde_addRecord
  17. return $?
  18. }
  19. dns_hostingde_rm() {
  20. fulldomain="${1}"
  21. txtvalue="${2}"
  22. _debug "Calling: _hostingde_removeRecord() '${fulldomain}' '${txtvalue}'"
  23. _hostingde_apiKey && _hostingde_getZoneConfig && _hostingde_removeRecord
  24. return $?
  25. }
  26. #################### own Private functions below ##################################
  27. _hostingde_apiKey() {
  28. HOSTINGDE_APIKEY="${HOSTINGDE_APIKEY:-$(_readaccountconf_mutable HOSTINGDE_APIKEY)}"
  29. HOSTINGDE_ENDPOINT="${HOSTINGDE_ENDPOINT:-$(_readaccountconf_mutable HOSTINGDE_ENDPOINT)}"
  30. if [ -z "$HOSTINGDE_APIKEY" ] || [ -z "$HOSTINGDE_ENDPOINT" ]; then
  31. HOSTINGDE_APIKEY=""
  32. HOSTINGDE_ENDPOINT=""
  33. _err "You haven't specified hosting.de API key or endpoint yet."
  34. _err "Please create your key and try again."
  35. return 1
  36. fi
  37. _saveaccountconf_mutable HOSTINGDE_APIKEY "$HOSTINGDE_APIKEY"
  38. _saveaccountconf_mutable HOSTINGDE_ENDPOINT "$HOSTINGDE_ENDPOINT"
  39. }
  40. _hostingde_parse() {
  41. find="${1}"
  42. if [ "${2}" ]; then
  43. notfind="${2}"
  44. fi
  45. if [ "${notfind}" ]; then
  46. _egrep_o \""${find}\":.*" | grep -v "${notfind}" | cut -d ':' -f 2 | cut -d ',' -f 1 | tr -d ' '
  47. else
  48. _egrep_o \""${find}\":.*" | cut -d ':' -f 2 | cut -d ',' -f 1 | tr -d ' '
  49. fi
  50. }
  51. _hostingde_getZoneConfig() {
  52. _info "Getting ZoneConfig"
  53. curZone="${fulldomain#*.}"
  54. returnCode=1
  55. while _contains "${curZone}" "\\."; do
  56. curData="{\"filter\":{\"field\":\"zoneName\",\"value\":\"${curZone}\"},\"limit\":1,\"authToken\":\"${HOSTINGDE_APIKEY}\"}"
  57. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneConfigsFind")"
  58. _debug "Calling zoneConfigsFind: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneConfigsFind'"
  59. _debug "Result of zoneConfigsFind: '$curResult'"
  60. if _contains "${curResult}" '"status": "error"'; then
  61. if _contains "${curResult}" '"code": 10109'; then
  62. _err "The API-Key is invalid or could not be found"
  63. else
  64. _err "UNKNOWN API ERROR"
  65. fi
  66. returnCode=1
  67. break
  68. fi
  69. if _contains "${curResult}" '"totalEntries": 1'; then
  70. _info "Retrieved zone data."
  71. _debug "Zone data: '${curResult}'"
  72. zoneConfigId=$(echo "${curResult}" | _hostingde_parse "id")
  73. zoneConfigName=$(echo "${curResult}" | _hostingde_parse "name")
  74. zoneConfigType=$(echo "${curResult}" | _hostingde_parse "type" "FindZoneConfigsResult")
  75. zoneConfigExpire=$(echo "${curResult}" | _hostingde_parse "expire")
  76. zoneConfigNegativeTtl=$(echo "${curResult}" | _hostingde_parse "negativeTtl")
  77. zoneConfigRefresh=$(echo "${curResult}" | _hostingde_parse "refresh")
  78. zoneConfigRetry=$(echo "${curResult}" | _hostingde_parse "retry")
  79. zoneConfigTtl=$(echo "${curResult}" | _hostingde_parse "ttl")
  80. zoneConfigDnsServerGroupId=$(echo "${curResult}" | _hostingde_parse "dnsServerGroupId")
  81. zoneConfigEmailAddress=$(echo "${curResult}" | _hostingde_parse "emailAddress")
  82. zoneConfigDnsSecMode=$(echo "${curResult}" | _hostingde_parse "dnsSecMode")
  83. zoneConfigTemplateValues=$(echo "${curResult}" | _hostingde_parse "templateValues")
  84. if [ "$zoneConfigTemplateValues" != "null" ]; then
  85. _debug "Zone is tied to a template."
  86. zoneConfigTemplateValuesTemplateId=$(echo "${curResult}" | _hostingde_parse "templateId")
  87. zoneConfigTemplateValuesTemplateName=$(echo "${curResult}" | _hostingde_parse "templateName")
  88. zoneConfigTemplateValuesTemplateReplacementsIPv4=$(echo "${curResult}" | _hostingde_parse "ipv4Replacement")
  89. zoneConfigTemplateValuesTemplateReplacementsIPv6=$(echo "${curResult}" | _hostingde_parse "ipv6Replacement")
  90. zoneConfigTemplateValuesTemplateReplacementsMailIPv4=$(echo "${curResult}" | _hostingde_parse "mailIpv4Replacement")
  91. zoneConfigTemplateValuesTemplateReplacementsMailIPv6=$(echo "${curResult}" | _hostingde_parse "mailIpv6Replacement")
  92. zoneConfigTemplateValuesTemplateTieToTemplate=$(echo "${curResult}" | _hostingde_parse "tieToTemplate")
  93. zoneConfigTemplateValues="{\"templateId\":${zoneConfigTemplateValuesTemplateId},\"templateName\":${zoneConfigTemplateValuesTemplateName},\"templateReplacements\":{\"ipv4Replacement\":${zoneConfigTemplateValuesTemplateReplacementsIPv4},\"ipv6Replacement\":${zoneConfigTemplateValuesTemplateReplacementsIPv6},\"mailIpv4Replacement\":${zoneConfigTemplateValuesTemplateReplacementsMailIPv4},\"mailIpv6Replacement\":${zoneConfigTemplateValuesTemplateReplacementsMailIPv6}},\"tieToTemplate\":${zoneConfigTemplateValuesTemplateTieToTemplate}}"
  94. _debug "Template values: '{$zoneConfigTemplateValues}'"
  95. fi
  96. if [ "${zoneConfigType}" != "\"NATIVE\"" ]; then
  97. _err "Zone is not native"
  98. returnCode=1
  99. break
  100. fi
  101. _debug "zoneConfigId '${zoneConfigId}'"
  102. returnCode=0
  103. break
  104. fi
  105. curZone="${curZone#*.}"
  106. done
  107. if [ $returnCode -ne 0 ]; then
  108. _info "ZoneEnd reached, Zone ${curZone} not found in hosting.de API"
  109. fi
  110. return $returnCode
  111. }
  112. _hostingde_getZoneStatus() {
  113. _debug "Checking Zone status"
  114. curData="{\"filter\":{\"field\":\"zoneConfigId\",\"value\":${zoneConfigId}},\"limit\":1,\"authToken\":\"${HOSTINGDE_APIKEY}\"}"
  115. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind")"
  116. _debug "Calling zonesFind '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zonesFind'"
  117. _debug "Result of zonesFind '$curResult'"
  118. zoneStatus=$(echo "${curResult}" | _hostingde_parse "status" "success")
  119. _debug "zoneStatus '${zoneStatus}'"
  120. return 0
  121. }
  122. _hostingde_addRecord() {
  123. _info "Adding record to zone"
  124. _hostingde_getZoneStatus
  125. _debug "Result of zoneStatus: '${zoneStatus}'"
  126. while [ "${zoneStatus}" != "\"active\"" ]; do
  127. _sleep 5
  128. _hostingde_getZoneStatus
  129. _debug "Result of zoneStatus: '${zoneStatus}'"
  130. done
  131. 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}]}"
  132. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  133. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  134. _debug "Result of zoneUpdate: '$curResult'"
  135. if _contains "${curResult}" '"status": "error"'; then
  136. if _contains "${curResult}" '"code": 10109'; then
  137. _err "The API-Key is invalid or could not be found"
  138. else
  139. _err "UNKNOWN API ERROR"
  140. fi
  141. return 1
  142. fi
  143. return 0
  144. }
  145. _hostingde_removeRecord() {
  146. _info "Removing record from zone"
  147. _hostingde_getZoneStatus
  148. _debug "Result of zoneStatus: '$zoneStatus'"
  149. while [ "$zoneStatus" != "\"active\"" ]; do
  150. _sleep 5
  151. _hostingde_getZoneStatus
  152. _debug "Result of zoneStatus: '$zoneStatus'"
  153. done
  154. 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}\\\"\"}]}"
  155. curResult="$(_post "${curData}" "${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate")"
  156. _debug "Calling zoneUpdate: '${curData}' '${HOSTINGDE_ENDPOINT}/api/dns/v1/json/zoneUpdate'"
  157. _debug "Result of zoneUpdate: '$curResult'"
  158. if _contains "${curResult}" '"status": "error"'; then
  159. if _contains "${curResult}" '"code": 10109'; then
  160. _err "The API-Key is invalid or could not be found"
  161. else
  162. _err "UNKNOWN API ERROR"
  163. fi
  164. return 1
  165. fi
  166. return 0
  167. }