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