Merge branch 'lightningwirelabs'
[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 DDNSProviderLightningWireLabs(DDNSProvider):
125         INFO = {
126                 "handle"    : "dns.lightningwirelabs.com",
127                 "name"      : "Lightning Wire Labs",
128                 "website"   : "http://dns.lightningwirelabs.com/",
129                 "protocols" : ["ipv6", "ipv4",]
130         }
131
132         # Information about the format of the HTTPS request is to be found
133         # https://dns.lightningwirelabs.com/knowledge-base/api/ddns
134         url = "https://dns.lightningwirelabs.com/update"
135
136         @property
137         def token(self):
138                 """
139                         Fast access to the token.
140                 """
141                 return self.get("token")
142
143         def __call__(self):
144                 data =  {
145                         "hostname" : self.hostname,
146                 }
147
148                 # Check if we update an IPv6 address.
149                 address6 = self.get_address("ipv6")
150                 if address6:
151                         data["address6"] = address6
152
153                 # Check if we update an IPv4 address.
154                 address4 = self.get_address("ipv4")
155                 if address4:
156                         data["address4"] = address4
157
158                 # Raise an error if none address is given.
159                 if not data.has_key("address6") and not data.has_key("address4"):
160                         raise DDNSConfigurationError
161
162                 # Check if a token has been set.
163                 if self.token:
164                         data["token"] = self.token
165
166                 # Check for username and password.
167                 elif self.username and self.password:
168                         data.update({
169                                 "username" : self.username,
170                                 "password" : self.password,
171                         })
172
173                 # Raise an error if no auth details are given.
174                 else:
175                         raise DDNSConfigurationError
176
177                 # Send update to the server.
178                 response = self.send_request(url, data=data)
179
180                 # Handle success messages.
181                 if response.code == 200:
182                         return
183
184                 # Handle error codes.
185                 if response.code == "403":
186                         raise DDNSAuthenticationError
187                 elif response.code == "400":
188                         raise DDNSRequestError
189
190                 # If we got here, some other update error happened.
191                 raise DDNSUpdateError
192
193
194 class DDNSProviderNOIP(DDNSProvider):
195         INFO = {
196                 "handle"    : "no-ip.com",
197                 "name"      : "No-IP",
198                 "website"   : "http://www.no-ip.com/",
199                 "protocols" : ["ipv4",]
200         }
201
202         # Information about the format of the HTTP request is to be found
203         # here: http://www.no-ip.com/integrate/request and
204         # here: http://www.no-ip.com/integrate/response
205
206         url = "http://%(username)s:%(password)s@dynupdate.no-ip.com/nic/update"
207
208         def __call__(self):
209                 url = self.url % {
210                         "username" : self.username,
211                         "password" : self.password,
212                 }
213
214                 data = {
215                         "hostname" : self.hostname,
216                         "address"  : self.get_address("ipv4"),
217                 }
218
219                 # Send update to the server.
220                 response = self.send_request(url, data=data)
221
222                 # Get the full response message.
223                 output = response.read()
224
225                 # Handle success messages.
226                 if output.startswith("good") or output.startswith("nochg"):
227                         return
228
229                 # Handle error codes.
230                 if output == "badauth":
231                         raise DDNSAuthenticationError
232                 elif output == "aduse":
233                         raise DDNSAbuseError
234                 elif output == "911":
235                         raise DDNSInternalServerError
236
237                 # If we got here, some other update error happened.
238                 raise DDNSUpdateError
239
240
241 class DDNSProviderSelfhost(DDNSProvider):
242         INFO = {
243                 "handle"    : "selfhost.de",
244                 "name"      : "Selfhost.de",
245                 "website"   : "http://www.selfhost.de/",
246                 "protocols" : ["ipv4",],
247         }
248
249         url = "https://carol.selfhost.de/update"
250
251         def __call__(self):
252                 data = {
253                         "username" : self.username,
254                         "password" : self.password,
255                         "textmodi" : "1",
256                 }
257
258                 response = self.send_request(self.url, data=data)
259
260                 match = re.search("status=20(0|4)", response.read())
261                 if not match:
262                         raise DDNSUpdateError