+ return self.core.system.get_address(proto) or default
+
+
+class DDNSProtocolDynDNS2(object):
+ """
+ This is an abstract class that implements the DynDNS updater
+ protocol version 2. As this is a popular way to update dynamic
+ DNS records, this class is supposed make the provider classes
+ shorter and simpler.
+ """
+
+ # Information about the format of the request is to be found
+ # http://dyn.com/support/developers/api/perform-update/
+ # http://dyn.com/support/developers/api/return-codes/
+
+ def _prepare_request_data(self):
+ data = {
+ "hostname" : self.hostname,
+ "myip" : self.get_address("ipv4"),
+ }
+
+ return data
+
+ def update(self):
+ data = self._prepare_request_data()
+
+ # Send update to the server.
+ response = self.send_request(self.url, data=data,
+ username=self.username, password=self.password)
+
+ # Get the full response message.
+ output = response.read()
+
+ # Handle success messages.
+ if output.startswith("good") or output.startswith("nochg"):
+ return
+
+ # Handle error codes.
+ if output == "badauth":
+ raise DDNSAuthenticationError
+ elif output == "aduse":
+ raise DDNSAbuseError
+ elif output == "notfqdn":
+ raise DDNSRequestError(_("No valid FQDN was given."))
+ elif output == "nohost":
+ raise DDNSRequestError(_("Specified host does not exist."))
+ elif output == "911":
+ raise DDNSInternalServerError
+ elif output == "dnserr":
+ raise DDNSInternalServerError(_("DNS error encountered."))
+
+ # If we got here, some other update error happened.
+ raise DDNSUpdateError(_("Server response: %s") % output)
+
+
+class DDNSResponseParserXML(object):
+ """
+ This class provides a parser for XML responses which
+ will be sent by various providers. This class uses the python
+ shipped XML minidom module to walk through the XML tree and return
+ a requested element.
+ """
+
+ def get_xml_tag_value(self, document, content):
+ # Send input to the parser.
+ xmldoc = xml.dom.minidom.parseString(document)
+
+ # Get XML elements by the given content.
+ element = xmldoc.getElementsByTagName(content)
+
+ # If no element has been found, we directly can return None.
+ if not element:
+ return None
+
+ # Only get the first child from an element, even there are more than one.
+ firstchild = element[0].firstChild
+
+ # Get the value of the child.
+ value = firstchild.nodeValue
+
+ # Return the value.
+ return value
+
+
+class DDNSProviderAllInkl(DDNSProvider):
+ handle = "all-inkl.com"
+ name = "All-inkl.com"
+ website = "http://all-inkl.com/"
+ protocols = ("ipv4",)
+
+ # There are only information provided by the vendor how to
+ # perform an update on a FRITZ Box. Grab requried informations
+ # from the net.
+ # http://all-inkl.goetze.it/v01/ddns-mit-einfachen-mitteln/
+
+ url = "http://dyndns.kasserver.com"
+
+ def update(self):
+ # There is no additional data required so we directly can
+ # send our request.
+ response = self.send_request(self.url, username=self.username, password=self.password)
+
+ # Get the full response message.
+ output = response.read()
+
+ # Handle success messages.
+ if output.startswith("good") or output.startswith("nochg"):
+ return
+
+ # If we got here, some other update error happened.
+ raise DDNSUpdateError
+
+
+class DDNSProviderBindNsupdate(DDNSProvider):
+ handle = "nsupdate"
+ name = "BIND nsupdate utility"
+ website = "http://en.wikipedia.org/wiki/Nsupdate"
+
+ DEFAULT_TTL = 60
+
+ def update(self):
+ scriptlet = self.__make_scriptlet()
+
+ # -v enables TCP hence we transfer keys and other data that may
+ # exceed the size of one packet.
+ # -t sets the timeout
+ command = ["nsupdate", "-v", "-t", "60"]
+
+ p = subprocess.Popen(command, shell=True,
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ )
+ stdout, stderr = p.communicate(scriptlet)
+
+ if p.returncode == 0:
+ return
+
+ raise DDNSError("nsupdate terminated with error code: %s\n %s" % (p.returncode, stderr))
+
+ def __make_scriptlet(self):
+ scriptlet = []
+
+ # Set a different server the update is sent to.
+ server = self.get("server", None)
+ if server:
+ scriptlet.append("server %s" % server)
+
+ key = self.get("key", None)
+ if key:
+ secret = self.get("secret")
+
+ scriptlet.append("key %s %s" % (key, secret))
+
+ ttl = self.get("ttl", self.DEFAULT_TTL)
+
+ # Perform an update for each supported protocol.
+ for rrtype, proto in (("AAAA", "ipv6"), ("A", "ipv4")):
+ address = self.get_address(proto)
+ if not address:
+ continue
+
+ scriptlet.append("update delete %s. %s" % (self.hostname, rrtype))
+ scriptlet.append("update add %s. %s %s %s" % \
+ (self.hostname, ttl, rrtype, address))
+
+ # Send the actions to the server.
+ scriptlet.append("send")
+ scriptlet.append("quit")
+
+ logger.debug(_("Scriptlet:"))
+ for line in scriptlet:
+ # Masquerade the line with the secret key.
+ if line.startswith("key"):
+ line = "key **** ****"
+
+ logger.debug(" %s" % line)
+
+ return "\n".join(scriptlet)