def make_update(action, query): hostname = query.hostname.encode('ascii') D = dns.name.from_text(domain) H = dns.name.from_text(query.hostname) if H.is_subdomain(D): R = H.relativize(D) else: return "400 NOTAUTH %s\n" % H.to_text() keyring, algo = read_session_key("/etc/bind/keys/webapp.key") update = dns.update.Update(D, keyring=keyring, keyalgorithm=algo) if action == 'update': update.present(R, 'a') update.replace(R, 300, 'a', query.ip.encode('ascii')) elif action == 'delete': update.present(R, 'a') update.delete(R, 'a') elif action == 'add': update.absent(R, 'a') update.add(R, 300, 'a', query.ip.encode('ascii')) response = dns.query.tcp(update, '127.0.0.1') if response.rcode() == 0: return "NOERROR %s\n" % H.to_text() else: return "%s %s\n" % (dns.rcode.to_text(response.rcode()), H.to_text())
def test_to_wire3(self): update = dns.update.Update('example') update.id = 1 update.present('foo') update.present('foo', 'a') update.present('bar', 'a', '10.0.0.5') update.absent('blaz2') update.absent('blaz2', 'a') update.replace('foo', 300, 'a', '10.0.0.1', '10.0.0.2') update.add('bar', dns.rdataset.from_text(1, 1, 300, '10.0.0.3')) update.delete('bar', 'a', '10.0.0.4') update.delete('blaz', 'a') update.delete('blaz2') self.failUnless(update.to_wire() == goodwire)
def test_to_wire3(self): # type: () -> None update = dns.update.Update('example') update.id = 1 update.present('foo') update.present('foo', 'a') update.present('bar', 'a', '10.0.0.5') update.absent('blaz2') update.absent('blaz2', 'a') update.replace('foo', 300, 'a', '10.0.0.1', '10.0.0.2') update.add('bar', dns.rdataset.from_text(1, 1, 300, '10.0.0.3')) update.delete('bar', 'a', '10.0.0.4') update.delete('blaz', 'a') update.delete('blaz2') self.assertEqual(update.to_wire(), goodwire)
def test_to_wire2(self): # type: () -> None update = dns.update.Update('example') update.id = 1 update.present('foo') update.present('foo', 'a') update.present('bar', 'a', '10.0.0.5') update.absent('blaz2') update.absent('blaz2', 'a') update.replace('foo', 300, 'a', '10.0.0.1', '10.0.0.2') update.add('bar', 300, dns.rdata.from_text(1, 1, '10.0.0.3')) update.delete('bar', 'a', '10.0.0.4') update.delete('blaz', 'a') update.delete('blaz2') self.failUnless(update.to_wire() == goodwire)
def test_to_wire3(self): # type: () -> None update = dns.update.Update("example") update.id = 1 update.present("foo") update.present("foo", "a") update.present("bar", "a", "10.0.0.5") update.absent("blaz2") update.absent("blaz2", "a") update.replace("foo", 300, "a", "10.0.0.1", "10.0.0.2") update.add("bar", dns.rdataset.from_text(1, 1, 300, "10.0.0.3")) update.delete("bar", "a", "10.0.0.4") update.delete("blaz", "a") update.delete("blaz2") self.assertEqual(update.to_wire(), goodwire)
def test_to_wire1(self): update = dns.update.Update('example') update.id = 1 update.present('foo') update.present('foo', 'a') update.present('bar', 'a', '10.0.0.5') update.absent('blaz2') update.absent('blaz2', 'a') update.replace('foo', 300, 'a', '10.0.0.1', '10.0.0.2') update.add('bar', 300, 'a', '10.0.0.3') update.delete('bar', 'a', '10.0.0.4') update.delete('blaz', 'a') update.delete('blaz2') self.failUnless(update.to_wire() == goodwire)
def test_to_wire1(self): # type: () -> None update = dns.update.Update('example') update.id = 1 update.present('foo') update.present('foo', 'a') update.present('bar', 'a', '10.0.0.5') update.absent('blaz2') update.absent('blaz2', 'a') update.replace('foo', 300, 'a', '10.0.0.1', '10.0.0.2') update.add('bar', 300, 'a', '10.0.0.3') update.delete('bar', 'a', '10.0.0.4') update.delete('blaz', 'a') update.delete('blaz2') self.failUnless(update.to_wire() == goodwire)
def test_to_wire1(self): # type: () -> None update = dns.update.Update('example') update.id = 1 update.present('foo') update.present('foo', 'a') update.present('bar', 'a', '10.0.0.5') update.absent('blaz2') update.absent('blaz2', 'a') update.replace('foo', 300, 'a', '10.0.0.1', '10.0.0.2') update.add('bar', 300, 'a', '10.0.0.3') update.delete('bar', 'a', '10.0.0.4') update.delete('blaz', 'a') update.delete('blaz2') self.assertTrue(update.to_wire() == goodwire)
def _prepare_dns_updates(add_rrsets, delete_rrsets, my_zones): """Prepare a set of DNS updates for the specified rrset additions and deletions. One update will be created for each zone mentioned in the rrsets. Constrints will be added to the DNS update message: * when deleting the record, ensure that it existed * when adding a record, ensure that it did not exist * when modifying (deleting and readding) a record, ensure that it existed Returns a dict mapping zone names to dnspython Update objects. """ updates = {} for rrset in delete_rrsets: zone = _get_zone(rrset.name, my_zones) # Create a new update for this zone if necessary if zone not in updates: updates[zone] = dns.update.Update(zone, keyring=_create_keyring(zone)) update = updates[zone] # Require the record exist before deleting it. update.present(rrset.name, *rrset.items) # Delete the record. update.delete(rrset.name, rrset) for rrset in add_rrsets: zone = _get_zone(rrset.name, my_zones) # Create a new update for this zone if necessary if zone not in updates: updates[zone] = dns.update.Update(zone, keyring=_create_keyring(zone)) update = updates[zone] # For additions only, require that the record not exist before adding # it. We're processing each modification as a delete/add pair, so # it will exist before the update (and we ensure this above). if rrset.name not in [delete.name for delete in delete_rrsets]: update.absent(rrset.name, rrset.rdtype) update.add(rrset.name, rrset) return updates