5ff0631daeba8fcae6bf9a681b74fc1ab851ac7d
[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 raise NotImplementedError
109
110 def send_request(self, *args, **kwargs):
111 """
112 Proxy connection to the send request
113 method.
114 """
115 return self.core.system.send_request(*args, **kwargs)
116
117 def get_address(self, proto):
118 """
119 Proxy method to get the current IP address.
120 """
121 return self.core.system.get_address(proto)
122
123
124 class DDNSProviderDHS(DDNSProvider):
125 INFO = {
126 "handle" : "dhs.org",
127 "name" : "DHS International",
128 "website" : "http://dhs.org/",
129 "protocols" : ["ipv4",]
130 }
131
132 # No information about the used update api provided on webpage,
133 # grabed from source code of ez-ipudate.
134 url = "http://members.dhs.org/nic/hosts"
135
136 def __call__(self):
137 url = self.url % {
138 "username" : self.username,
139 "password" : self.password,
140 }
141
142 data = {
143 "domain" : self.hostname,
144 "ip" : self.get_address("ipv4"),
145 "hostcmd" : "edit",
146 "hostcmdstage" : "2",
147 "type" : "4",
148 }
149
150 # Send update to the server.
151 response = self.send_request(url, username=self.username, password=self.password,
152 data=data)
153
154 # Handle success messages.
155 if response.code == 200:
156 return
157
158 # Handle error codes.
159 elif response.code == "401":
160 raise DDNSAuthenticationError
161
162 # If we got here, some other update error happened.
163 raise DDNSUpdateError
164
165
166 class DDNSProviderDNSpark(DDNSProvider):
167 INFO = {
168 "handle" : "dnspark.com",
169 "name" : "DNS Park",
170 "website" : "http://dnspark.com/",
171 "protocols" : ["ipv4",]
172 }
173
174 # Informations to the used api can be found here:
175 # https://dnspark.zendesk.com/entries/31229348-Dynamic-DNS-API-Documentation
176 url = "https://control.dnspark.com/api/dynamic/update.php"
177
178 def __call__(self):
179 url = self.url % {
180 "username" : self.username,
181 "password" : self.password,
182 }
183
184 data = {
185 "domain" : self.hostname,
186 "ip" : self.get_address("ipv4"),
187 }
188
189 # Send update to the server.
190 response = self.send_request(url, username=self.username, password=self.password,
191 data=data)
192
193 # Get the full response message.
194 output = response.read()
195
196 # Handle success messages.
197 if output.startswith("ok") or output.startswith("nochange"):
198 return
199
200 # Handle error codes.
201 if output == "unauth":
202 raise DDNSAuthenticationError
203 elif output == "abuse":
204 raise DDNSAbuseError
205 elif output == "blocked":
206 raise DDNSBlockedError
207 elif output == "nofqdn":
208 raise DDNSRequestError(_("No valid FQDN was given."))
209 elif output == "nohost":
210 raise DDNSRequestError(_("Invalid hostname specified."))
211 elif output == "notdyn":
212 raise DDNSRequestError(_("Hostname not marked as a dynamic host."))
213 elif output == "invalid":
214 raise DDNSRequestError(_("Invalid IP address has been sent."))
215
216 # If we got here, some other update error happened.
217 raise DDNSUpdateError
218
219
220 class DDNSProviderLightningWireLabs(DDNSProvider):
221 INFO = {
222 "handle" : "dns.lightningwirelabs.com",
223 "name" : "Lightning Wire Labs",
224 "website" : "http://dns.lightningwirelabs.com/",
225 "protocols" : ["ipv6", "ipv4",]
226 }
227
228 # Information about the format of the HTTPS request is to be found
229 # https://dns.lightningwirelabs.com/knowledge-base/api/ddns
230 url = "https://dns.lightningwirelabs.com/update"
231
232 @property
233 def token(self):
234 """
235 Fast access to the token.
236 """
237 return self.get("token")
238
239 def __call__(self):
240 data = {
241 "hostname" : self.hostname,
242 }
243
244 # Check if we update an IPv6 address.
245 address6 = self.get_address("ipv6")
246 if address6:
247 data["address6"] = address6
248
249 # Check if we update an IPv4 address.
250 address4 = self.get_address("ipv4")
251 if address4:
252 data["address4"] = address4
253
254 # Raise an error if none address is given.
255 if not data.has_key("address6") and not data.has_key("address4"):
256 raise DDNSConfigurationError
257
258 # Check if a token has been set.
259 if self.token:
260 data["token"] = self.token
261
262 # Check for username and password.
263 elif self.username and self.password:
264 data.update({
265 "username" : self.username,
266 "password" : self.password,
267 })
268
269 # Raise an error if no auth details are given.
270 else:
271 raise DDNSConfigurationError
272
273 # Send update to the server.
274 response = self.send_request(url, data=data)
275
276 # Handle success messages.
277 if response.code == 200:
278 return
279
280 # Handle error codes.
281 if response.code == "403":
282 raise DDNSAuthenticationError
283 elif response.code == "400":
284 raise DDNSRequestError
285
286 # If we got here, some other update error happened.
287 raise DDNSUpdateError
288
289
290 class DDNSProviderNOIP(DDNSProvider):
291 INFO = {
292 "handle" : "no-ip.com",
293 "name" : "No-IP",
294 "website" : "http://www.no-ip.com/",
295 "protocols" : ["ipv4",]
296 }
297
298 # Information about the format of the HTTP request is to be found
299 # here: http://www.no-ip.com/integrate/request and
300 # here: http://www.no-ip.com/integrate/response
301
302 url = "http://%(username)s:%(password)s@dynupdate.no-ip.com/nic/update"
303
304 def __call__(self):
305 url = self.url % {
306 "username" : self.username,
307 "password" : self.password,
308 }
309
310 data = {
311 "hostname" : self.hostname,
312 "address" : self.get_address("ipv4"),
313 }
314
315 # Send update to the server.
316 response = self.send_request(url, data=data)
317
318 # Get the full response message.
319 output = response.read()
320
321 # Handle success messages.
322 if output.startswith("good") or output.startswith("nochg"):
323 return
324
325 # Handle error codes.
326 if output == "badauth":
327 raise DDNSAuthenticationError
328 elif output == "aduse":
329 raise DDNSAbuseError
330 elif output == "911":
331 raise DDNSInternalServerError
332
333 # If we got here, some other update error happened.
334 raise DDNSUpdateError
335
336
337 class DDNSProviderSelfhost(DDNSProvider):
338 INFO = {
339 "handle" : "selfhost.de",
340 "name" : "Selfhost.de",
341 "website" : "http://www.selfhost.de/",
342 "protocols" : ["ipv4",],
343 }
344
345 url = "https://carol.selfhost.de/update"
346
347 def __call__(self):
348 data = {
349 "username" : self.username,
350 "password" : self.password,
351 "textmodi" : "1",
352 }
353
354 response = self.send_request(self.url, data=data)
355
356 match = re.search("status=20(0|4)", response.read())
357 if not match:
358 raise DDNSUpdateError