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 ###############################################################################
28 from __version__
import CLIENT_VERSION
32 # Initialize the logger.
34 logger
= logging
.getLogger("ddns.system")
37 class DDNSSystem(object):
39 The DDNSSystem class adds a layer of abstraction
40 between the ddns software and the system.
43 # The default useragent.
44 USER_AGENT
= "IPFireDDNSUpdater/%s" % CLIENT_VERSION
46 def __init__(self
, core
):
47 # Connection to the core of the program.
53 # Find out on which distribution we are running.
54 self
.distro
= self
._get
_distro
_identifier
()
55 logger
.debug(_("Running on distribution: %s") % self
.distro
)
59 proxy
= self
.core
.settings
.get("proxy")
61 # Strip http:// at the beginning.
62 if proxy
and proxy
.startswith("http://"):
67 def get_local_ipv6_address(self
):
70 def get_local_ipv4_address(self
):
71 if self
.distro
== "ipfire-2":
73 with
open("/var/ipfire/red/local-ipaddress") as f
:
85 def _guess_external_ip_address(self
, url
, timeout
=10):
87 Sends a request to an external web server
88 to determine the current default IP address.
91 response
= self
.send_request(url
, timeout
=timeout
)
93 # If the server could not be reached, we will return nothing.
94 except DDNSNetworkError
:
97 if not response
.code
== 200:
100 match
= re
.search(r
"^Your IP address is: (.*)$", response
.read())
104 return match
.group(1)
106 def guess_external_ipv6_address(self
):
108 Sends a request to the internet to determine
109 the public IPv6 address.
111 return self
._guess
_external
_ip
_address
("http://checkip6.dns.lightningwirelabs.com")
113 def guess_external_ipv4_address(self
):
115 Sends a request to the internet to determine
116 the public IPv4 address.
118 return self
._guess
_external
_ip
_address
("http://checkip4.dns.lightningwirelabs.com")
120 def send_request(self
, url
, method
="GET", data
=None, username
=None, password
=None, timeout
=30):
121 assert method
in ("GET", "POST")
123 # Add all arguments in the data dict to the URL and escape them properly.
124 if method
== "GET" and data
:
125 query_args
= self
._format
_query
_args
(data
)
129 url
= "%s&%s" % (url
, query_args
)
131 url
= "%s?%s" % (url
, query_args
)
133 logger
.debug("Sending request (%s): %s" % (method
, url
))
135 logger
.debug(" data: %s" % data
)
137 req
= urllib2
.Request(url
, data
=data
)
139 if username
and password
:
140 basic_auth_header
= self
._make
_basic
_auth
_header
(username
, password
)
141 req
.add_header("Authorization", "Basic %s" % basic_auth_header
)
143 # Set the user agent.
144 req
.add_header("User-Agent", self
.USER_AGENT
)
146 # All requests should not be cached anywhere.
147 req
.add_header("Pragma", "no-cache")
149 # Set the upstream proxy if needed.
151 logger
.debug("Using proxy: %s" % self
.proxy
)
153 # Configure the proxy for this request.
154 req
.set_proxy(self
.proxy
, "http")
156 assert req
.get_method() == method
158 logger
.debug(_("Request header:"))
159 for k
, v
in req
.headers
.items():
160 logger
.debug(" %s: %s" % (k
, v
))
163 resp
= urllib2
.urlopen(req
, timeout
=timeout
)
165 # Log response header.
166 logger
.debug(_("Response header (Status Code %s):") % resp
.code
)
167 for k
, v
in resp
.info().items():
168 logger
.debug(" %s: %s" % (k
, v
))
170 # Return the entire response object.
173 except urllib2
.HTTPError
, e
:
176 raise DDNSRequestError(e
.reason
)
178 # 401 - Authorization Required
180 elif e
.code
in (401, 403):
181 raise DDNSAuthenticationError(e
.reason
)
183 # 500 - Internal Server Error
185 raise DDNSInternalServerError(e
.reason
)
187 # 503 - Service Unavailable
189 raise DDNSServiceUnavailableError(e
.reason
)
191 # Raise all other unhandled exceptions.
194 except urllib2
.URLError
, e
:
196 # Network Unreachable (e.g. no IPv6 access)
197 if e
.reason
.errno
== 101:
198 raise DDNSNetworkUnreachableError
199 elif e
.reason
.errno
== 111:
200 raise DDNSConnectionRefusedError
202 # Raise all other unhandled exceptions.
205 except socket
.timeout
, e
:
206 logger
.debug(_("Connection timeout"))
208 raise DDNSConnectionTimeoutError
210 def _format_query_args(self
, data
):
213 for k
, v
in data
.items():
214 arg
= "%s=%s" % (k
, urllib
.quote(v
))
217 return "&".join(args
)
219 def _make_basic_auth_header(self
, username
, password
):
220 authstring
= "%s:%s" % (username
, password
)
222 # Encode authorization data in base64.
223 authstring
= base64
.encodestring(authstring
)
225 # Remove any newline characters.
226 authstring
= authstring
.replace("\n", "")
230 def get_address(self
, proto
):
232 Returns the current IP address for
233 the given IP protocol.
236 return self
.__addresses
[proto
]
238 # IP is currently unknown and needs to be retrieved.
240 self
.__addresses
[proto
] = address
= \
241 self
._get
_address
(proto
)
245 def _get_address(self
, proto
):
246 assert proto
in ("ipv6", "ipv4")
248 # IPFire 2 does not support IPv6.
249 if self
.distro
== "ipfire-2" and proto
== "ipv6":
252 # Check if the external IP address should be guessed from
254 guess_ip
= self
.core
.settings
.get("guess_external_ip", "true")
256 # If the external IP address should be used, we just do
258 if guess_ip
in ("true", "yes", "1"):
260 return self
.guess_external_ipv6_address()
262 elif proto
== "ipv4":
263 return self
.guess_external_ipv4_address()
265 # Get the local IP addresses.
268 return self
.get_local_ipv6_address()
269 elif proto
== "ipv4":
270 return self
.get_local_ipv4_address()
272 def resolve(self
, hostname
, proto
=None):
277 elif proto
== "ipv6":
278 family
= socket
.AF_INET6
279 elif proto
== "ipv4":
280 family
= socket
.AF_INET
282 raise ValueError("Protocol not supported: %s" % proto
)
284 # Resolve the host address.
286 response
= socket
.getaddrinfo(hostname
, None, family
)
287 except socket
.gaierror
, e
:
288 # Name or service not known
292 # No record for requested family available (e.g. no AAAA)
299 for family
, socktype
, proto
, canonname
, sockaddr
in response
:
301 if family
== socket
.AF_INET6
:
302 address
, port
, flow_info
, scope_id
= sockaddr
304 # Only use the global scope.
305 if not scope_id
== 0:
309 elif family
== socket
.AF_INET
:
310 address
, port
= sockaddr
312 # Ignore everything else...
316 # Add to repsonse list if not already in there.
317 if not address
in addresses
:
318 addresses
.append(address
)
322 def _get_distro_identifier(self
):
324 Returns a unique identifier for the distribution
327 os_release
= self
.__parse
_os
_release
()
331 system_release
= self
.__parse
_system
_release
()
333 return system_release
335 # If nothing else could be found, we return
339 def __parse_os_release(self
):
341 Tries to parse /etc/os-release and
342 returns a unique distribution identifier
346 f
= open("/etc/os-release", "r")
356 for line
in f
.readlines():
357 m
= re
.match(r
"^([A-Z\_]+)=(.*)$", line
)
361 os_release
[m
.group(1)] = m
.group(2)
364 return "%(ID)s-%(VERSION_ID)s" % os_release
368 def __parse_system_release(self
):
370 Tries to parse /etc/system-release and
371 returns a unique distribution identifier
375 f
= open("/etc/system-release", "r")
387 # Check for IPFire systems
388 m
= re
.match(r
"^IPFire (\d).(\d+)", line
)
390 return "ipfire-%s" % m
.group(1)