2 ###############################################################################
4 # ddns - A dynamic DNS client for IPFire #
5 # Copyright (C) 2012 IPFire development team #
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. #
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. #
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/>. #
20 ###############################################################################
30 from .__version
__ import CLIENT_VERSION
34 # Initialize the logger.
36 logger
= logging
.getLogger("ddns.system")
39 class DDNSSystem(object):
41 The DDNSSystem class adds a layer of abstraction
42 between the ddns software and the system.
45 # The default useragent.
46 USER_AGENT
= "IPFireDDNSUpdater/%s" % CLIENT_VERSION
48 def __init__(self
, core
):
49 # Connection to the core of the program.
55 # Find out on which distribution we are running.
56 self
.distro
= self
._get
_distro
_identifier
()
57 logger
.debug(_("Running on distribution: %s") % self
.distro
)
61 proxy
= self
.core
.settings
.get("proxy")
63 # Strip http:// at the beginning.
64 if proxy
and proxy
.startswith("http://"):
69 def get_local_ip_address(self
, proto
):
70 ip_address
= self
._get
_local
_ip
_address
(proto
)
72 # Check if the IP address is usable and only return it then
73 if self
._is
_usable
_ip
_address
(proto
, ip_address
):
76 def _get_local_ip_address(self
, proto
):
77 # Legacy code for IPFire 2.
78 if self
.distro
== "ipfire-2" and proto
== "ipv4":
80 with
open("/var/ipfire/red/local-ipaddress") as f
:
91 raise NotImplementedError
93 def _guess_external_ip_address(self
, url
, timeout
=10):
95 Sends a request to an external web server
96 to determine the current default IP address.
99 response
= self
.send_request(url
, timeout
=timeout
)
101 # If the server could not be reached, we will return nothing.
102 except DDNSNetworkError
:
105 if not response
.code
== 200:
108 match
= re
.search(r
"^Your IP address is: (.*)$", response
.read())
112 return match
.group(1)
114 def guess_external_ip_address(self
, family
, **kwargs
):
116 url
= "https://checkip6.dns.lightningwirelabs.com"
117 elif family
== "ipv4":
118 url
= "https://checkip4.dns.lightningwirelabs.com"
120 raise ValueError("unknown address family")
122 return self
._guess
_external
_ip
_address
(url
, **kwargs
)
124 def send_request(self
, url
, method
="GET", data
=None, username
=None, password
=None, timeout
=30):
125 assert method
in ("GET", "POST")
127 # Add all arguments in the data dict to the URL and escape them properly.
128 if method
== "GET" and data
:
129 query_args
= self
._format
_query
_args
(data
)
133 url
= "%s&%s" % (url
, query_args
)
135 url
= "%s?%s" % (url
, query_args
)
137 logger
.debug("Sending request (%s): %s" % (method
, url
))
139 logger
.debug(" data: %s" % data
)
141 req
= urllib
.request
.Request(url
, data
=data
)
143 if username
and password
:
144 basic_auth_header
= self
._make
_basic
_auth
_header
(username
, password
)
145 req
.add_header("Authorization", "Basic %s" % basic_auth_header
)
147 # Set the user agent.
148 req
.add_header("User-Agent", self
.USER_AGENT
)
150 # All requests should not be cached anywhere.
151 req
.add_header("Pragma", "no-cache")
153 # Set the upstream proxy if needed.
155 logger
.debug("Using proxy: %s" % self
.proxy
)
157 # Configure the proxy for this request.
158 req
.set_proxy(self
.proxy
, "http")
160 assert req
.get_method() == method
162 logger
.debug(_("Request header:"))
163 for k
, v
in req
.headers
.items():
164 logger
.debug(" %s: %s" % (k
, v
))
167 resp
= urllib
.request
.urlopen(req
, timeout
=timeout
)
169 # Log response header.
170 logger
.debug(_("Response header (Status Code %s):") % resp
.code
)
171 for k
, v
in resp
.info().items():
172 logger
.debug(" %s: %s" % (k
, v
))
174 # Return the entire response object.
177 except urllib
.error
.HTTPError
as e
:
178 # Log response header.
179 logger
.debug(_("Response header (Status Code %s):") % e
.code
)
180 for k
, v
in e
.hdrs
.items():
181 logger
.debug(" %s: %s" % (k
, v
))
185 raise DDNSRequestError(e
.reason
)
187 # 401 - Authorization Required
189 elif e
.code
in (401, 403):
190 raise DDNSAuthenticationError(e
.reason
)
193 # Either the provider has changed the API, or
194 # there is an error on the server
196 raise DDNSNotFound(e
.reason
)
198 # 429 - Too Many Requests
200 raise DDNSTooManyRequests(e
.reason
)
202 # 500 - Internal Server Error
204 raise DDNSInternalServerError(e
.reason
)
206 # 503 - Service Unavailable
208 raise DDNSServiceUnavailableError(e
.reason
)
210 # Raise all other unhandled exceptions.
213 except urllib
.error
.URLError
as e
:
216 if isinstance(e
.reason
, ssl
.SSLError
):
219 if e
.reason
== "CERTIFICATE_VERIFY_FAILED":
220 raise DDNSCertificateError
222 # Raise all other SSL errors
223 raise DDNSSSLError(e
.reason
)
225 # Name or service not known
226 if e
.reason
.errno
== -2:
227 raise DDNSResolveError
229 # Network Unreachable (e.g. no IPv6 access)
230 if e
.reason
.errno
== 101:
231 raise DDNSNetworkUnreachableError
234 elif e
.reason
.errno
== 111:
235 raise DDNSConnectionRefusedError
238 elif e
.reason
.errno
== 113:
239 raise DDNSNoRouteToHostError(req
.host
)
241 # Raise all other unhandled exceptions.
244 except socket
.timeout
as e
:
245 logger
.debug(_("Connection timeout"))
247 raise DDNSConnectionTimeoutError
249 def _format_query_args(self
, data
):
252 for k
, v
in data
.items():
253 arg
= "%s=%s" % (k
, urllib
.parse
.quote(v
))
256 return "&".join(args
)
258 def _make_basic_auth_header(self
, username
, password
):
259 authstring
= "%s:%s" % (username
, password
)
261 # Encode authorization data in base64.
262 authstring
= base64
.encodebytes(authstring
)
264 # Remove any newline characters.
265 authstring
= authstring
.replace("\n", "")
269 def get_address(self
, proto
):
271 Returns the current IP address for
272 the given IP protocol.
275 return self
.__addresses
[proto
]
277 # IP is currently unknown and needs to be retrieved.
279 self
.__addresses
[proto
] = address
= \
280 self
._get
_address
(proto
)
284 def _get_address(self
, proto
):
285 assert proto
in ("ipv6", "ipv4")
287 # IPFire 2 does not support IPv6.
288 if self
.distro
== "ipfire-2" and proto
== "ipv6":
291 # Check if the external IP address should be guessed from
293 guess_ip
= self
.core
.settings
.get("guess_external_ip", "true")
294 guess_ip
= guess_ip
in ("true", "yes", "1")
296 # Get the local IP address.
297 local_ip_address
= None
301 local_ip_address
= self
.get_local_ip_address(proto
)
302 except NotImplementedError:
303 logger
.warning(_("Falling back to check the IP address with help of a public server"))
305 # If no local IP address could be determined, we will fall back to the guess
306 # it with help of an external server...
307 if not local_ip_address
:
308 local_ip_address
= self
.guess_external_ip_address(proto
)
310 return local_ip_address
312 def _is_usable_ip_address(self
, proto
, address
):
314 Returns True is the local IP address is usable
315 for dynamic DNS (i.e. is not a RFC1918 address or similar).
318 # This is not the most perfect solution to match
319 # these addresses, but instead of pulling in an entire
320 # library to handle the IP addresses better, we match
321 # with regular expressions instead.
323 # RFC1918 address space
324 r
"^10\.\d+\.\d+\.\d+$",
325 r
"^192\.168\.\d+\.\d+$",
326 r
"^172\.(1[6-9]|2[0-9]|31)\.\d+\.\d+$",
328 # Dual Stack Lite address space
329 r
"^100\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\.\d+\.\d+$",
332 for match
in matches
:
333 m
= re
.match(match
, address
)
337 # Found a match. IP address is not usable.
340 # In all other cases, return OK.
343 def resolve(self
, hostname
, proto
=None):
348 elif proto
== "ipv6":
349 family
= socket
.AF_INET6
350 elif proto
== "ipv4":
351 family
= socket
.AF_INET
353 raise ValueError("Protocol not supported: %s" % proto
)
355 # Resolve the host address.
357 response
= socket
.getaddrinfo(hostname
, None, family
)
358 except socket
.gaierror
as e
:
359 # Name or service not known
363 # Temporary failure in name resolution
365 raise DDNSResolveError(hostname
)
367 # No record for requested family available (e.g. no AAAA)
374 for family
, socktype
, proto
, canonname
, sockaddr
in response
:
376 if family
== socket
.AF_INET6
:
377 address
, port
, flow_info
, scope_id
= sockaddr
379 # Only use the global scope.
380 if not scope_id
== 0:
384 elif family
== socket
.AF_INET
:
385 address
, port
= sockaddr
387 # Ignore everything else...
391 # Add to repsonse list if not already in there.
392 if address
not in addresses
:
393 addresses
.append(address
)
397 def _get_distro_identifier(self
):
399 Returns a unique identifier for the distribution
402 os_release
= self
.__parse
_os
_release
()
406 system_release
= self
.__parse
_system
_release
()
408 return system_release
410 # If nothing else could be found, we return
414 def __parse_os_release(self
):
416 Tries to parse /etc/os-release and
417 returns a unique distribution identifier
421 f
= open("/etc/os-release", "r")
431 for line
in f
.readlines():
432 m
= re
.match(r
"^([A-Z\_]+)=(.*)$", line
)
436 os_release
[m
.group(1)] = m
.group(2)
439 return "%(ID)s-%(VERSION_ID)s" % os_release
443 def __parse_system_release(self
):
445 Tries to parse /etc/system-release and
446 returns a unique distribution identifier
450 f
= open("/etc/system-release", "r")
462 # Check for IPFire systems
463 m
= re
.match(r
"^IPFire (\d).(\d+)", line
)
465 return "ipfire-%s" % m
.group(1)