/
netcup.py
executable file
·185 lines (163 loc) · 7.71 KB
/
netcup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
Copyright (c) 2023 Ingo Lafrenz <opnsense@der-ingo.de>
Copyright (c) 2023 Ad Schellevis <ad@opnsense.org>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------------------------------
Netcup DNS provider, see https://ccp.netcup.net/run/webservice/servers/endpoint.php
"""
import syslog
import requests
from . import BaseAccount
class Netcup(BaseAccount):
_services = ['netcup']
def __init__(self, account: dict):
super().__init__(account)
self.settings['APIPassword'] = None
self.settings['APIKey'] = None
# min TTL set to 300
self.settings['ttl'] = max(int(self.settings['ttl']) if self.settings.get('ttl', '').isdigit() else 0, 300)
@staticmethod
def known_services():
return Netcup._services
@staticmethod
def match(account):
return account.get('service') in Netcup._services
def execute(self):
if super().execute():
if self.settings['hostnames'].find(',') > -1:
self.settings['hostnames'] = self.settings['hostnames'].split(',')[0]
syslog.syslog(
syslog.LOG_WARNING,
"Multiple hostnames detected, ignoring all except first. "+
"Consider using CNAMEs or create separate DynDNS instances for each hostname."
)
if self.settings['hostnames'].find('.') == -1:
syslog.syslog(syslog.LOG_ERR, "Incomplete FQDN offerred %s" % self.settings['hostnames'])
return False
self.hostname, self.domain = self.settings['hostnames'].split('.', 1)
if self.settings['password'].count(':') > 1:
if self.settings['password'].count('\\:') == 1:
self.settings['APIPassword'], self.settings['APIKey'] = self.settings['password'].split('\\:')
elif self.settings['password'].count(':') == 1:
self.settings['APIPassword'], self.settings['APIKey'] = self.settings['password'].split(':')
if self.settings['APIPassword'] is None or self.settings['APIKey'] is None:
syslog.syslog(
syslog.LOG_ERR,
"Unable to parse APIPassword:APIKey, when colons are used, make sure to escape the separator (\:)."
)
return False
self.netcupAPISessionID = self._login()
if not self.netcupAPISessionID:
return False
dnsZoneInfo = self._sendRequest(self._createRequestPayload('infoDnsZone'))
if not dnsZoneInfo:
return False
if str(self.settings['ttl']) != dnsZoneInfo['ttl']:
dnsZoneInfo['ttl'] = str(self.settings['ttl'])
self._sendRequest(self._createRequestPayload('updateDnsZone', {'dnszone': dnsZoneInfo}))
dnsRecordsInfo = self._sendRequest(self._createRequestPayload('infoDnsRecords'))
if not dnsRecordsInfo:
return False
recordType = 'AAAA' if ':' in self.current_address else 'A'
self._updateIpAddress(recordType, dnsRecordsInfo)
self._logout()
self.update_state(address=self.current_address)
return True
def _login(self):
requestPayload = {
'action': 'login',
'param': {
'customernumber': self.settings['username'],
'apikey': self.settings['APIKey'],
'apipassword': self.settings['APIPassword']
}
}
return self._sendRequest(requestPayload).get('apisessionid', None)
def _updateDnsRecords(self, hostRecord):
return self._sendRequest(
self._createRequestPayload(
'updateDnsRecords',
{'dnsrecordset': {'dnsrecords': [hostRecord]}}
)
)
def _logout(self):
requestPayload = {
'action': 'logout',
'param': {
'customernumber': self._account['username'],
'apikey': self.settings['APIKey'],
'apisessionid': self.netcupAPISessionID
}
}
return self._sendRequest(requestPayload)
def _updateIpAddress(self, recordType, dnsRecordsInfo):
matchingRecords = [
r for r in dnsRecordsInfo['dnsrecords'] if r['type'] == recordType and r['hostname'] == self.hostname
]
if len(matchingRecords) > 1:
raise Exception(f'Too many {recordType} records for hostname {self.hostname} in DNS zone {self.domain}.')
if matchingRecords:
hostRecord = matchingRecords[0]
else:
hostRecord = {
'hostname': self.hostname,
'type': recordType,
'destination': None
}
currentNetcupIPAddress = hostRecord['destination']
if self.current_address != currentNetcupIPAddress:
syslog.syslog(
syslog.LOG_NOTICE,
f'IP address change detected. Old IP: {currentNetcupIPAddress}, new IP: {self.current_address}'
)
hostRecord['destination'] = self.current_address
if self._updateDnsRecords(hostRecord):
syslog.syslog(
syslog.LOG_NOTICE,
f'Successfully updated {recordType} record for {self.hostname}.{self.domain} to {self.current_address}'
)
else:
syslog.syslog(syslog.LOG_NOTICE, 'IP address has not changed. Nothing to do.')
def _createRequestPayload(self, action, extraParameters={}):
requestPayload = {
'action': action,
'param': {
'domainname': self.domain,
'customernumber': self._account['username'],
'apikey': self.settings['APIKey'],
'apisessionid': self.netcupAPISessionID,
}
}
requestPayload['param'].update(extraParameters)
return requestPayload
def _sendRequest(self, payload):
req = requests.post(url='https://ccp.netcup.net/run/webservice/servers/endpoint.php?JSON', json=payload)
try:
resp = req.json()
except requests.exceptions.JSONDecodeError:
resp = {}
if resp.get('status', '') == 'success':
return resp['responsedata']
else:
syslog.syslog(
syslog.LOG_ERR,
f"{payload['action']} failed with status {resp['status']}. response: {resp}"
)
return {}