we now track blockip and only add when needed and only delete entries we created.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 27 Jul 2016 21:43:40 +0000 (23:43 +0200)
committergregor herrmann <gregoa@debian.org>
Wed, 27 Jul 2016 22:21:38 +0000 (00:21 +0200)
bin/tdyndns_update

index 272a610..0667c87 100755 (executable)
@@ -29,7 +29,7 @@ def forward_lookup(fqdn, ip_family):
        zonename = os.path.basename(filename)
        zone = dns.zone.from_file(filename, zonename, relativize=False)
        for name, ttl, rdata in zone.iterate_rdatas(ip_family):
-               if name[:-1] == fqdn:  # [:-1] removes trailing dot
+               if str(name)[:-1] == fqdn:  # [:-1] removes trailing dot
                        return ipaddr.IPAddress(rdata.address)
 
 
@@ -86,13 +86,6 @@ def blockip_whitelist_delete(ip):
                check_call(command)
 
 
-def blockip_whitelist_delete_if_unused(ip):
-       if ipfamily_by_ip(args.ip) == 'A':
-               sync_dynamic_zones()
-               if len(list(reverse_lookup(ip))) == 0:
-                       blockip_whitelist_delete(ip)
-
-
 def main(args):
        try:
                if args.delete:
@@ -100,16 +93,22 @@ def main(args):
                                nsupdate_delete(args.fqdn, 'A')
                                nsupdate_delete(args.fqdn, 'AAAA')
                        else:
+                               ipfamily = ipfamily_by_ip(args.ip)
+                               sync_dynamic_zones()
+                               old_ip = forward_lookup(args.fqdn, ipfamily)
                                nsupdate_delete(args.fqdn, ipfamily_by_ip(args.ip))
-                               blockip_whitelist_delete_if_unused(args.ip)
+                               if old_ip is not None:
+                                       blockip_whitelist_delete(old_ip)
                else:
                        ipfamily = ipfamily_by_ip(args.ip)
+                       sync_dynamic_zones()
                        old_ip = forward_lookup(args.fqdn, ipfamily)
                        nsupdate_delete(args.fqdn, ipfamily)
-                       if old_ip is not None:
-                               blockip_whitelist_delete_if_unused(old_ip)
                        nsupdate_add(args.fqdn, args.ttl, args.ip)
-                       blockip_whitelist_add(args.ip)
+                       if old_ip != args.ip:
+                               if old_ip is not None:
+                                       blockip_whitelist_delete(old_ip)
+                               blockip_whitelist_add(args.ip)
        except CalledProcessError as e:
                sys.exit(e.returncode)