]> git.ipfire.org Git - oddments/ddns.git/blob - src/ddns/providers.py
f95aa2acb52159da023b5e6edb40491c9eddf4fa
[oddments/ddns.git] / src / ddns / providers.py
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # ddns - A dynamic DNS client for IPFire #
5 # Copyright (C) 2012 IPFire development team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 # Import all possible exception types.
23 from .errors import *
24
25 class DDNSProvider(object):
26 INFO = {
27 # A short string that uniquely identifies
28 # this provider.
29 "handle" : None,
30
31 # The full name of the provider.
32 "name" : None,
33
34 # A weburl to the homepage of the provider.
35 # (Where to register a new account?)
36 "website" : None,
37
38 # A list of supported protocols.
39 "protocols" : ["ipv6", "ipv4"],
40 }
41
42 DEFAULT_SETTINGS = {}
43
44 def __init__(self, core, **settings):
45 self.core = core
46
47 # Copy a set of default settings and
48 # update them by those from the configuration file.
49 self.settings = self.DEFAULT_SETTINGS.copy()
50 self.settings.update(settings)
51
52 def __repr__(self):
53 return "<DDNS Provider %s (%s)>" % (self.name, self.handle)
54
55 def __cmp__(self, other):
56 return cmp(self.hostname, other.hostname)
57
58 @property
59 def name(self):
60 """
61 Returns the name of the provider.
62 """
63 return self.INFO.get("name")
64
65 @property
66 def website(self):
67 """
68 Returns the website URL of the provider
69 or None if that is not available.
70 """
71 return self.INFO.get("website", None)
72
73 @property
74 def handle(self):
75 """
76 Returns the handle of this provider.
77 """
78 return self.INFO.get("handle")
79
80 def get(self, key, default=None):
81 """
82 Get a setting from the settings dictionary.
83 """
84 return self.settings.get(key, default)
85
86 @property
87 def hostname(self):
88 """
89 Fast access to the hostname.
90 """
91 return self.get("hostname")
92
93 @property
94 def username(self):
95 """
96 Fast access to the username.
97 """
98 return self.get("username")
99
100 @property
101 def password(self):
102 """
103 Fast access to the password.
104 """
105 return self.get("password")
106
107 def __call__(self):
108 self.update()
109
110 def update(self):
111 raise NotImplementedError
112
113 def send_request(self, *args, **kwargs):
114 """
115 Proxy connection to the send request
116 method.
117 """
118 return self.core.system.send_request(*args, **kwargs)
119
120 def get_address(self, proto):
121 """
122 Proxy method to get the current IP address.
123 """
124 return self.core.system.get_address(proto)
125
126
127 class DDNSProviderDHS(DDNSProvider):
128 INFO = {
129 "handle" : "dhs.org",
130 "name" : "DHS International",
131 "website" : "http://dhs.org/",
132 "protocols" : ["ipv4",]
133 }
134
135 # No information about the used update api provided on webpage,
136 # grabed from source code of ez-ipudate.
137 url = "http://members.dhs.org/nic/hosts"
138
139 def update(self):
140 url = self.url % {
141 "username" : self.username,
142 "password" : self.password,
143 }
144
145 data = {
146 "domain" : self.hostname,
147 "ip" : self.get_address("ipv4"),
148 "hostcmd" : "edit",
149 "hostcmdstage" : "2",
150 "type" : "4",
151 }
152
153 # Send update to the server.
154 response = self.send_request(url, username=self.username, password=self.password,
155 data=data)
156
157 # Handle success messages.
158 if response.code == 200:
159 return
160
161 # Handle error codes.
162 elif response.code == "401":
163 raise DDNSAuthenticationError
164
165 # If we got here, some other update error happened.
166 raise DDNSUpdateError
167
168
169 class DDNSProviderDNSpark(DDNSProvider):
170 INFO = {
171 "handle" : "dnspark.com",
172 "name" : "DNS Park",
173 "website" : "http://dnspark.com/",
174 "protocols" : ["ipv4",]
175 }
176
177 # Informations to the used api can be found here:
178 # https://dnspark.zendesk.com/entries/31229348-Dynamic-DNS-API-Documentation
179 url = "https://control.dnspark.com/api/dynamic/update.php"
180
181 def update(self):
182 url = self.url % {
183 "username" : self.username,
184 "password" : self.password,
185 }
186
187 data = {
188 "domain" : self.hostname,
189 "ip" : self.get_address("ipv4"),
190 }
191
192 # Send update to the server.
193 response = self.send_request(url, username=self.username, password=self.password,
194 data=data)
195
196 # Get the full response message.
197 output = response.read()
198
199 # Handle success messages.
200 if output.startswith("ok") or output.startswith("nochange"):
201 return
202
203 # Handle error codes.
204 if output == "unauth":
205 raise DDNSAuthenticationError
206 elif output == "abuse":
207 raise DDNSAbuseError
208 elif output == "blocked":
209 raise DDNSBlockedError
210 elif output == "nofqdn":
211 raise DDNSRequestError(_("No valid FQDN was given."))
212 elif output == "nohost":
213 raise DDNSRequestError(_("Invalid hostname specified."))
214 elif output == "notdyn":
215 raise DDNSRequestError(_("Hostname not marked as a dynamic host."))
216 elif output == "invalid":
217 raise DDNSRequestError(_("Invalid IP address has been sent."))
218
219 # If we got here, some other update error happened.
220 raise DDNSUpdateError
221
222
223 class DDNSProviderLightningWireLabs(DDNSProvider):
224 INFO = {
225 "handle" : "dns.lightningwirelabs.com",
226 "name" : "Lightning Wire Labs",
227 "website" : "http://dns.lightningwirelabs.com/",
228 "protocols" : ["ipv6", "ipv4",]
229 }
230
231 # Information about the format of the HTTPS request is to be found
232 # https://dns.lightningwirelabs.com/knowledge-base/api/ddns
233 url = "https://dns.lightningwirelabs.com/update"
234
235 @property
236 def token(self):
237 """
238 Fast access to the token.
239 """
240 return self.get("token")
241
242 def update(self):
243 data = {
244 "hostname" : self.hostname,
245 }
246
247 # Check if we update an IPv6 address.
248 address6 = self.get_address("ipv6")
249 if address6:
250 data["address6"] = address6
251
252 # Check if we update an IPv4 address.
253 address4 = self.get_address("ipv4")
254 if address4:
255 data["address4"] = address4
256
257 # Raise an error if none address is given.
258 if not data.has_key("address6") and not data.has_key("address4"):
259 raise DDNSConfigurationError
260
261 # Check if a token has been set.
262 if self.token:
263 data["token"] = self.token
264
265 # Check for username and password.
266 elif self.username and self.password:
267 data.update({
268 "username" : self.username,
269 "password" : self.password,
270 })
271
272 # Raise an error if no auth details are given.
273 else:
274 raise DDNSConfigurationError
275
276 # Send update to the server.
277 response = self.send_request(self.url, data=data)
278
279 # Handle success messages.
280 if response.code == 200:
281 return
282
283 # Handle error codes.
284 if response.code == "403":
285 raise DDNSAuthenticationError
286 elif response.code == "400":
287 raise DDNSRequestError
288
289 # If we got here, some other update error happened.
290 raise DDNSUpdateError
291
292
293 class DDNSProviderNOIP(DDNSProvider):
294 INFO = {
295 "handle" : "no-ip.com",
296 "name" : "No-IP",
297 "website" : "http://www.no-ip.com/",
298 "protocols" : ["ipv4",]
299 }
300
301 # Information about the format of the HTTP request is to be found
302 # here: http://www.no-ip.com/integrate/request and
303 # here: http://www.no-ip.com/integrate/response
304
305 url = "http://%(username)s:%(password)s@dynupdate.no-ip.com/nic/update"
306
307 def update(self):
308 url = self.url % {
309 "username" : self.username,
310 "password" : self.password,
311 }
312
313 data = {
314 "hostname" : self.hostname,
315 "address" : self.get_address("ipv4"),
316 }
317
318 # Send update to the server.
319 response = self.send_request(url, data=data)
320
321 # Get the full response message.
322 output = response.read()
323
324 # Handle success messages.
325 if output.startswith("good") or output.startswith("nochg"):
326 return
327
328 # Handle error codes.
329 if output == "badauth":
330 raise DDNSAuthenticationError
331 elif output == "aduse":
332 raise DDNSAbuseError
333 elif output == "911":
334 raise DDNSInternalServerError
335
336 # If we got here, some other update error happened.
337 raise DDNSUpdateError
338
339
340 class DDNSProviderSelfhost(DDNSProvider):
341 INFO = {
342 "handle" : "selfhost.de",
343 "name" : "Selfhost.de",
344 "website" : "http://www.selfhost.de/",
345 "protocols" : ["ipv4",],
346 }
347
348 url = "https://carol.selfhost.de/update"
349
350 def update(self):
351 data = {
352 "username" : self.username,
353 "password" : self.password,
354 "textmodi" : "1",
355 }
356
357 response = self.send_request(self.url, data=data)
358
359 match = re.search("status=20(0|4)", response.read())
360 if not match:
361 raise DDNSUpdateError