2013-09-28 17:17:12 +02:00
|
|
|
"""
|
2013-09-28 13:47:49 +02:00
|
|
|
Misc. DNS related code: query, dynamic update, etc.
|
2013-09-29 17:21:05 +02:00
|
|
|
|
|
|
|
Usually, higher level code wants to call the add/update/delete functions.
|
2013-09-28 13:47:49 +02:00
|
|
|
"""
|
|
|
|
|
2013-10-18 15:30:17 -07:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2013-09-28 17:02:43 +02:00
|
|
|
import dns.inet
|
2013-09-28 15:29:16 +02:00
|
|
|
import dns.name
|
|
|
|
import dns.resolver
|
|
|
|
import dns.query
|
|
|
|
import dns.update
|
|
|
|
import dns.tsig
|
|
|
|
import dns.tsigkeyring
|
|
|
|
|
2013-10-03 19:26:39 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
2013-09-28 15:29:16 +02:00
|
|
|
|
2013-09-28 17:02:43 +02:00
|
|
|
class SameIpError(ValueError):
|
|
|
|
"""
|
|
|
|
raised if an IP address is already present in DNS and and update was
|
|
|
|
requested, but is not needed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2013-10-03 20:39:55 +02:00
|
|
|
def check_ip(ipaddr, keys=('ipv4', 'ipv6')):
|
2013-09-29 17:21:05 +02:00
|
|
|
"""
|
2013-10-03 20:39:55 +02:00
|
|
|
Check if a string is a valid ip address and also
|
|
|
|
determine the kind of the address (address family).
|
|
|
|
Return first key for v4, second key for v6.
|
|
|
|
|
|
|
|
:param ipaddr: ip address, v4 or v6, str
|
|
|
|
:param keys: 2-tuple (v4key, v6key)
|
|
|
|
:return: v4key or v6key
|
|
|
|
:raises: ValueError if the ip is invalid
|
2013-09-29 17:21:05 +02:00
|
|
|
"""
|
|
|
|
af = dns.inet.af_for_address(ipaddr)
|
2013-10-03 20:39:55 +02:00
|
|
|
return keys[af == dns.inet.AF_INET6]
|
2013-09-29 17:21:05 +02:00
|
|
|
|
|
|
|
|
2013-10-08 15:39:45 +02:00
|
|
|
def add(fqdn, ipaddr, ttl=60, origin=None):
|
2013-09-29 17:21:05 +02:00
|
|
|
"""
|
|
|
|
intelligent dns adder - first does a lookup on the master server to find
|
|
|
|
the current ip and only sends an 'add' if there is no such entry.
|
|
|
|
otherwise send an 'upd' if the if we have a different ip.
|
|
|
|
|
|
|
|
:param fqdn: fully qualified domain name (str)
|
|
|
|
:param ipaddr: new ip address
|
|
|
|
:param ttl: time to live, default 60s (int)
|
2013-10-08 15:39:45 +02:00
|
|
|
:param origin: origin zone (optional, str)
|
2013-09-29 17:21:05 +02:00
|
|
|
:raises: SameIpError if new and old IP is the same
|
2013-10-03 20:47:25 +02:00
|
|
|
:raises: ValueError if ipaddr is no valid ip address string
|
2013-09-29 17:21:05 +02:00
|
|
|
"""
|
2013-10-03 20:39:55 +02:00
|
|
|
rdtype = check_ip(ipaddr, keys=('A', 'AAAA'))
|
2013-09-29 17:21:05 +02:00
|
|
|
try:
|
2013-10-08 15:39:45 +02:00
|
|
|
current_ipaddr = query_ns(fqdn, rdtype, origin=origin)
|
2013-09-29 17:21:05 +02:00
|
|
|
# check if ip really changed
|
|
|
|
ok = ipaddr != current_ipaddr
|
|
|
|
action = 'upd'
|
|
|
|
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
|
|
|
|
# no dns entry yet, ok
|
|
|
|
ok = True
|
|
|
|
action = 'add'
|
|
|
|
if ok:
|
|
|
|
# only send an add/update if the ip really changed as the update
|
|
|
|
# causes write I/O on the nameserver and also traffic to the
|
|
|
|
# dns slaves (they get a notify if we update the zone).
|
2013-10-08 15:39:45 +02:00
|
|
|
update_ns(fqdn, rdtype, ipaddr, action=action, ttl=ttl, origin=origin)
|
2013-09-29 17:21:05 +02:00
|
|
|
else:
|
|
|
|
raise SameIpError
|
|
|
|
|
|
|
|
|
2013-10-08 15:39:45 +02:00
|
|
|
def delete(fqdn, rdtype=None, origin=None):
|
2013-09-29 17:21:05 +02:00
|
|
|
"""
|
|
|
|
dns deleter
|
|
|
|
|
|
|
|
:param fqdn: fully qualified domain name (str)
|
|
|
|
:param rdtype: 'A', 'AAAA' or None (deletes 'A' and 'AAAA')
|
2013-10-08 15:39:45 +02:00
|
|
|
:param origin: origin zone (optional, str)
|
2013-09-29 17:21:05 +02:00
|
|
|
"""
|
|
|
|
if rdtype is not None:
|
|
|
|
assert rdtype in ['A', 'AAAA', ]
|
|
|
|
rdtypes = [rdtype, ]
|
|
|
|
else:
|
|
|
|
rdtypes = ['A', 'AAAA']
|
|
|
|
for rdtype in rdtypes:
|
2013-10-08 15:39:45 +02:00
|
|
|
update_ns(fqdn, rdtype, action='del', origin=origin)
|
2013-09-29 17:21:05 +02:00
|
|
|
|
|
|
|
|
2013-10-08 15:39:45 +02:00
|
|
|
def update(fqdn, ipaddr, ttl=60, origin=None):
|
2013-09-29 02:00:15 +02:00
|
|
|
"""
|
|
|
|
intelligent dns updater - first does a lookup on the master server to find
|
|
|
|
the current ip and only sends a dynamic update if we have a different ip.
|
|
|
|
|
|
|
|
:param fqdn: fully qualified domain name (str)
|
|
|
|
:param ipaddr: new ip address
|
|
|
|
:param ttl: time to live, default 60s (int)
|
2013-10-08 15:39:45 +02:00
|
|
|
:param origin: origin zone (optional, str)
|
2013-09-29 02:00:15 +02:00
|
|
|
:raises: SameIpError if new and old IP is the same
|
2013-10-03 20:47:25 +02:00
|
|
|
:raises: ValueError if ipaddr is no valid ip address string
|
2013-09-29 02:00:15 +02:00
|
|
|
"""
|
2013-10-03 20:39:55 +02:00
|
|
|
rdtype = check_ip(ipaddr, keys=('A', 'AAAA'))
|
2013-09-28 17:02:43 +02:00
|
|
|
try:
|
2013-10-08 15:39:45 +02:00
|
|
|
current_ipaddr = query_ns(fqdn, rdtype, origin=origin)
|
2013-09-28 17:02:43 +02:00
|
|
|
# check if ip really changed
|
|
|
|
ok = ipaddr != current_ipaddr
|
2013-09-29 00:34:26 +02:00
|
|
|
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
|
2013-09-28 17:02:43 +02:00
|
|
|
# no dns entry yet, ok
|
|
|
|
ok = True
|
|
|
|
if ok:
|
|
|
|
# only send an update if the ip really changed as the update
|
|
|
|
# causes write I/O on the nameserver and also traffic to the
|
|
|
|
# dns slaves (they get a notify if we update the zone).
|
2013-10-08 15:39:45 +02:00
|
|
|
update_ns(fqdn, rdtype, ipaddr, action='upd', ttl=ttl, origin=origin)
|
2013-09-28 17:02:43 +02:00
|
|
|
else:
|
|
|
|
raise SameIpError
|
|
|
|
|
|
|
|
|
2013-09-29 23:43:41 +02:00
|
|
|
def query_ns(qname, rdtype, origin=None):
|
2013-09-28 13:47:49 +02:00
|
|
|
"""
|
|
|
|
query a dns name from our master server
|
|
|
|
|
|
|
|
:param qname: the query name
|
|
|
|
:type qname: dns.name.Name object or str
|
|
|
|
:param rdtype: the query type
|
|
|
|
:type rdtype: int or str
|
2013-10-03 17:59:55 +02:00
|
|
|
:param origin: origin zone
|
|
|
|
:type origin: str or None
|
2013-09-28 13:47:49 +02:00
|
|
|
:return: IP (as str)
|
|
|
|
"""
|
2013-09-29 23:43:41 +02:00
|
|
|
origin, name = parse_name(qname, origin)
|
|
|
|
origin_str = str(origin)
|
|
|
|
nameserver = get_ns_info(origin_str)[0]
|
2013-09-28 13:47:49 +02:00
|
|
|
resolver = dns.resolver.Resolver(configure=False)
|
|
|
|
# we do not configure it from resolv.conf, but patch in the values we
|
|
|
|
# want into the documented attributes:
|
2013-09-29 23:43:41 +02:00
|
|
|
resolver.nameservers = [nameserver, ]
|
2013-09-30 01:58:16 +02:00
|
|
|
resolver.search = [dns.name.from_text(settings.BASEDOMAIN), ]
|
2013-09-28 13:47:49 +02:00
|
|
|
answer = resolver.query(qname, rdtype)
|
|
|
|
return str(list(answer)[0])
|
2013-09-28 15:29:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def parse_name(fqdn, origin=None):
|
|
|
|
"""
|
|
|
|
Parse a fully qualified domain name into a relative name
|
|
|
|
and a origin zone. Please note that the origin return value will
|
|
|
|
have a trailing dot.
|
|
|
|
|
|
|
|
:param fqdn: fully qualified domain name (str)
|
|
|
|
:param origin: origin zone (optional, str)
|
|
|
|
:return: origin, relative name (both dns.name.Name)
|
|
|
|
"""
|
|
|
|
fqdn = dns.name.from_text(fqdn)
|
|
|
|
if origin is None:
|
|
|
|
origin = dns.resolver.zone_for_name(fqdn)
|
|
|
|
rel_name = fqdn.relativize(origin)
|
|
|
|
else:
|
|
|
|
origin = dns.name.from_text(origin)
|
|
|
|
rel_name = fqdn - origin
|
|
|
|
return origin, rel_name
|
|
|
|
|
|
|
|
|
2013-09-29 23:43:41 +02:00
|
|
|
def get_ns_info(origin):
|
|
|
|
"""
|
|
|
|
Get the master nameserver for the <origin> zone, the key needed
|
|
|
|
to update the zone and the key algorithm used.
|
|
|
|
|
|
|
|
:param origin: zone we are dealing with, must be with trailing dot
|
2013-10-08 15:20:08 +02:00
|
|
|
:return: master nameserver, update key, update algo
|
2013-09-29 23:43:41 +02:00
|
|
|
"""
|
2013-10-18 15:30:17 -07:00
|
|
|
from .models import Domain
|
|
|
|
d = Domain.objects.get(domain=origin.rstrip('.'))
|
|
|
|
algorithm = getattr(dns.tsig, d.nameserver_update_algorithm)
|
|
|
|
return d.nameserver_ip, d.nameserver_update_key, algorithm
|
2013-09-29 23:43:41 +02:00
|
|
|
|
|
|
|
|
2013-09-28 15:29:16 +02:00
|
|
|
def update_ns(fqdn, rdtype='A', ipaddr=None, origin=None, action='upd', ttl=60):
|
|
|
|
"""
|
|
|
|
update our master server
|
|
|
|
|
2013-09-28 15:36:27 +02:00
|
|
|
:param fqdn: the fully qualified domain name to update (str)
|
|
|
|
:param rdtype: the record type (default: 'A') (str)
|
|
|
|
:param ipaddr: ip address (v4 or v6), if needed (str)
|
|
|
|
:param origin: the origin zone to update (default; autodetect) (str)
|
2013-09-28 15:29:16 +02:00
|
|
|
:param action: 'add', 'del' or 'upd'
|
2013-09-28 15:36:27 +02:00
|
|
|
:param ttl: time to live for the added/updated resource, default 60s (int)
|
|
|
|
:return: dns response
|
2013-09-28 15:29:16 +02:00
|
|
|
"""
|
|
|
|
assert action in ['add', 'del', 'upd', ]
|
|
|
|
origin, name = parse_name(fqdn, origin)
|
2013-09-29 23:43:41 +02:00
|
|
|
origin_str = str(origin)
|
|
|
|
nameserver, key, algo = get_ns_info(origin_str)
|
2013-09-28 15:29:16 +02:00
|
|
|
upd = dns.update.Update(origin,
|
2013-09-29 23:43:41 +02:00
|
|
|
keyring=dns.tsigkeyring.from_text({origin_str: key}),
|
|
|
|
keyalgorithm=algo)
|
2013-09-28 15:29:16 +02:00
|
|
|
if action == 'add':
|
|
|
|
assert ipaddr is not None
|
|
|
|
upd.add(name, ttl, rdtype, ipaddr)
|
|
|
|
elif action == 'del':
|
|
|
|
upd.delete(name, rdtype)
|
|
|
|
elif action == 'upd':
|
|
|
|
assert ipaddr is not None
|
|
|
|
upd.replace(name, ttl, rdtype, ipaddr)
|
2013-10-18 15:30:17 -07:00
|
|
|
logger.debug("performing %s for name %s and origin %s with rdtype %s and ipaddr %s" % (
|
|
|
|
action, name, origin, rdtype, ipaddr))
|
2013-09-29 23:43:41 +02:00
|
|
|
response = dns.query.tcp(upd, nameserver)
|
2013-09-28 15:29:16 +02:00
|
|
|
return response
|