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