]> git.ipfire.org Git - ddns.git/blobdiff - src/ddns/providers.py
Add nsupdate.info as new provider.
[ddns.git] / src / ddns / providers.py
index ed893a4d2e29ed67bb88237ed0101c2846b22d42..0c735fc6a41c18fe49f487a0e394c87f31c277b1 100644 (file)
@@ -20,6 +20,7 @@
 ###############################################################################
 
 import logging
+import subprocess
 import urllib2
 import xml.dom.minidom
 
@@ -253,6 +254,72 @@ class DDNSProviderAllInkl(DDNSProvider):
                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)
+
+
 class DDNSProviderDHS(DDNSProvider):
        handle    = "dhs.org"
        name      = "DHS International"
@@ -608,6 +675,48 @@ class DDNSProviderNOIP(DDNSProtocolDynDNS2, DDNSProvider):
                return data
 
 
+class DDNSProviderNsupdateINFO(DDNSProtocolDynDNS2, DDNSProvider):
+       handle    = "nsupdate.info"
+       name      = "nsupdate.info"
+       website   = "http://www.nsupdate.info/"
+       protocols = ("ipv6", "ipv4",)
+
+       # Information about the format of the HTTP request can be found
+       # after login on the provider user intrface and here:
+       # http://nsupdateinfo.readthedocs.org/en/latest/user.html
+
+       # Nsupdate.info uses the hostname as user part for the HTTP basic auth,
+       # and for the password a so called secret.
+       @property
+       def username(self):
+               return self.get("hostname")
+
+       @property
+       def password(self):
+               return self.get("secret")
+
+       @property
+       def proto(self):
+               return self.get("proto")
+
+       @property
+       def url(self):
+               # The update URL is different by the used protocol.
+               if self.proto == "ipv4":
+                       return "https://ipv4.nsupdate.info/nic/update"
+               elif self.proto == "ipv6":
+                       return "https://ipv6.nsupdate.info/nic/update"
+               else:
+                       raise DDNSUpdateError(_("Invalid protocol has been given"))
+
+       def _prepare_request_data(self):
+               data = {
+                       "myip" : self.get_address(self.proto),
+               }
+
+               return data
+
+
 class DDNSProviderOVH(DDNSProtocolDynDNS2, DDNSProvider):
        handle    = "ovh.com"
        name      = "OVH"