Implemented offline parameter.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 5 Mar 2014 22:59:47 +0000 (23:59 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 5 Mar 2014 22:59:47 +0000 (23:59 +0100)
cgi-bin/dyndns.py

index 8291ac2..95fcfda 100755 (executable)
@@ -66,15 +66,18 @@ class HostnameMissing(HostnameError):
 class PasswordWrong(HostnameError):
        pass
 
-class IpError(DynDnsError):
+class MyipError(DynDnsError):
        returncode = 'badip' # not documented at dyn.com
 
-class IpMissing(IpError):
+class MyipMissing(MyipError):
        pass
 
-class IpInvalid(IpError):
+class MyipInvalid(MyipError):
        pass
 
+class OfflineInvalid(DynDnsError):
+       returncode = 'badparam' # not documented at dyn.com
+
 try:
        # check username
        if username is None:
@@ -103,24 +106,39 @@ try:
        if hostname.endswith(ZONE):
                hostname = hostname[:-len(ZONE)]
 
+       # check offline
+       if offline is None or offline.lower() == 'no':
+               offline = False
+       elif offline.lower() == 'yes':
+               offline = True
+       else:
+               raise OfflineInvalid()
+
        # check IP address
-       if myip is None:
-               raise IpMissing()
-       try:
-               ip = ipaddr.IPAddress(myip) # throws an exception if the IP address is not valid
-       except ValueError:
-               raise IpInvalid()
-       if isinstance(ip, ipaddr.IPv4Address):
-               iptype = 'A'
-       elif isinstance(ip, ipaddr.IPv6Address):
-               iptype = 'AAAA'
+       if not offline:
+               if myip is None:
+                       raise MyipMissing()
+               try:
+                       ip = ipaddr.IPAddress(myip) # throws an exception if the IP address is not valid
+               except ValueError:
+                       raise MyipInvalid()
+               if isinstance(ip, ipaddr.IPv4Address):
+                       iptype = 'A'
+               elif isinstance(ip, ipaddr.IPv6Address):
+                       iptype = 'AAAA'
+               else:
+                       raise MyipInvalid() # should never happen
+
+       # update bind
+       if offline:
+               call(['sudo', '/usr/local/bin/nsupdate_dyndns_del', hostname, 'A'])
+               call(['sudo', '/usr/local/bin/nsupdate_dyndns_del', hostname, 'AAAA'])
        else:
-               raise IpInvalid() # should never happen
+               call(['sudo', '/usr/local/bin/nsupdate_dyndns', hostname, myip, iptype])
 
-       # access granted
+       # return success
        print "Content-Type: text/html"
        print
-       call(['sudo', '/usr/local/bin/nsupdate_dyndns', hostname, myip, iptype])
        print "good"
        # Note: we should return 'nochg' in case the IP has not changed, however we don't know yet.