From c43dce3a61e17791cc31f45498bae2d52edcf969 Mon Sep 17 00:00:00 2001 From: Martin Babinsky Date: Mon, 12 Oct 2015 16:24:50 +0200 Subject: [PATCH] always ask the resolver for the reverse zone when manipulating PTR records Instead of searching for all zones to identify the correct reverse zone, we will first ask the resolver to return the name of zone that should contain the desired record and then see if IPA manages this zone. This patch also removes a duplicate function in bindinstance.py that is not used anywhere. https://fedorahosted.org/freeipa/ticket/5200 Reviewed-By: Petr Spacek --- ipalib/plugins/dns.py | 51 +++++++++++-------------------- ipaserver/install/bindinstance.py | 2 -- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/ipalib/plugins/dns.py b/ipalib/plugins/dns.py index aefdedca6..ef282c946 100644 --- a/ipalib/plugins/dns.py +++ b/ipalib/plugins/dns.py @@ -530,35 +530,26 @@ def add_forward_record(zone, name, str_address): except errors.EmptyModlist: pass # the entry already exists and matches -def get_reverse_zone(ipaddr, prefixlen=None): +def get_reverse_zone(ipaddr): + """ + resolve the reverse zone for IP address and see if it is managed by IPA + server + :param ipaddr: host IP address + :return: tuple containing name of the reverse zone and the name of the + record + """ ip = netaddr.IPAddress(str(ipaddr)) revdns = DNSName(unicode(ip.reverse_dns)) + revzone = DNSName(dns.resolver.zone_for_name(revdns)) - if prefixlen is None: - revzone = None - - result = api.Command['dnszone_find']()['result'] - for zone in result: - zonename = zone['idnsname'][0] - if (revdns.is_subdomain(zonename.make_absolute()) and - (revzone is None or zonename.is_subdomain(revzone))): - revzone = zonename - else: - if ip.version == 4: - pos = 4 - prefixlen / 8 - elif ip.version == 6: - pos = 32 - prefixlen / 4 - items = ip.reverse_dns.split('.') - revzone = DNSName(items[pos:]) - - try: - api.Command['dnszone_show'](revzone) - except errors.NotFound: - revzone = None - - if revzone is None: + try: + api.Command['dnszone_show'](revzone) + except errors.NotFound: raise errors.NotFound( - reason=_('DNS reverse zone for IP address %(addr)s not found') % dict(addr=ipaddr) + reason=_( + 'DNS reverse zone %(revzone)s for IP address ' + '%(addr)s is not managed by this server') % dict( + addr=ipaddr, revzone=revzone) ) revname = revdns.relativize(revzone) @@ -592,11 +583,8 @@ def add_records_for_host_validation(option_name, host, domain, ip_addresses, che if check_reverse: try: - prefixlen = None - if not ip.defaultnet: - prefixlen = ip.prefixlen # we prefer lookup of the IP through the reverse zone - revzone, revname = get_reverse_zone(ip, prefixlen) + revzone, revname = get_reverse_zone(ip) reverse = api.Command['dnsrecord_find'](revzone, idnsname=revname) if reverse['count'] > 0: raise errors.DuplicateEntry( @@ -621,10 +609,7 @@ def add_records_for_host(host, domain, ip_addresses, add_forward=True, add_rever if add_reverse: try: - prefixlen = None - if not ip.defaultnet: - prefixlen = ip.prefixlen - revzone, revname = get_reverse_zone(ip, prefixlen) + revzone, revname = get_reverse_zone(ip) addkw = {'ptrrecord': host.derelativize(domain).ToASCII()} api.Command['dnsrecord_add'](revzone, revname, **addkw) except errors.EmptyModlist: diff --git a/ipaserver/install/bindinstance.py b/ipaserver/install/bindinstance.py index 1ba744221..0bc0557cc 100644 --- a/ipaserver/install/bindinstance.py +++ b/ipaserver/install/bindinstance.py @@ -276,8 +276,6 @@ def find_reverse_zone(ip_address, api=api): return None -def get_reverse_zone(ip_address): - return find_reverse_zone(ip_address) or get_reverse_zone_default(ip_address) def read_reverse_zone(default, ip_address): while True: