2018-12-11 02:23:17 +03:00
#!/usr/bin/env python3
2012-03-12 22:23:50 +04:00
# vim: expandtab
2010-02-25 07:12:53 +03:00
#
# update our DNS names using TSIG-GSS
#
# Copyright (C) Andrew Tridgell 2010
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
2010-03-09 15:34:10 +03:00
import fcntl
2010-02-25 07:12:53 +03:00
import sys
import tempfile
2010-11-15 11:09:14 +03:00
import subprocess
2010-02-25 07:12:53 +03:00
2010-03-03 06:28:42 +03:00
# ensure we get messages out immediately, so they get in the samba logs,
# and don't get swallowed by a timeout
2011-02-21 08:46:58 +03:00
os.environ['PYTHONUNBUFFERED'] = '1'
2010-03-03 06:28:42 +03:00
2023-09-01 04:25:29 +03:00
# forcing GMT avoids a problem in some timezones with kerberos. Both MIT and
2010-09-29 07:43:58 +04:00
# heimdal can get mutual authentication errors due to the 24 second difference
# between UTC and GMT when using some zone files (eg. the PDT zone from
# the US)
2011-02-21 08:46:58 +03:00
os.environ["TZ"] = "GMT"
2010-09-29 07:43:58 +04:00
2010-02-25 07:12:53 +03:00
# Find right directory when running from source tree
sys.path.insert(0, "bin/python")
import samba
import optparse
2010-03-29 18:08:11 +04:00
from samba import getopt as options
from ldb import SCOPE_BASE
2014-04-28 19:33:50 +04:00
from samba import dsdb
2010-02-25 07:12:53 +03:00
from samba.auth import system_session
2010-02-26 05:30:44 +03:00
from samba.samdb import SamDB
2010-09-19 07:57:26 +04:00
from samba.dcerpc import netlogon, winbind
2015-02-26 02:29:23 +03:00
from samba.netcmd.dns import cmd_dns
from samba import gensec
2018-03-27 05:24:44 +03:00
from samba.kcc import kcc_utils
2020-09-11 23:29:46 +03:00
from samba.common import get_string
2018-03-12 04:45:48 +03:00
import ldb
2010-03-05 03:45:10 +03:00
2020-10-24 12:17:44 +03:00
from samba.dnsresolver import DNSResolver
2010-11-30 07:23:39 +03:00
import dns.resolver
import dns.exception
2010-02-26 05:30:44 +03:00
default_ttl = 900
2010-09-19 07:57:26 +04:00
am_rodc = False
2010-11-15 11:09:14 +03:00
error_count = 0
2010-02-26 05:30:44 +03:00
2019-06-27 07:57:21 +03:00
parser = optparse.OptionParser("samba_dnsupdate [options]")
2010-02-26 05:30:44 +03:00
sambaopts = options.SambaOptions(parser)
parser.add_option_group(sambaopts)
parser.add_option_group(options.VersionOptions(parser))
parser.add_option("--verbose", action="store_true")
2015-02-26 02:29:23 +03:00
parser.add_option("--use-samba-tool", action="store_true", help="Use samba-tool to make updates over RPC, rather than over DNS")
parser.add_option("--use-nsupdate", action="store_true", help="Use nsupdate command to make updates over DNS (default, if kinit successful)")
2010-09-20 00:02:05 +04:00
parser.add_option("--all-names", action="store_true")
2010-03-09 15:34:10 +03:00
parser.add_option("--all-interfaces", action="store_true")
2015-08-07 05:57:20 +03:00
parser.add_option("--current-ip", action="append", help="IP address to update DNS to match (helpful if behind NAT, valid multiple times, defaults to values from interfaces=)")
parser.add_option("--rpc-server-ip", type="string", help="IP address of server to use with samba-tool (defaults to first --current-ip)")
2010-03-09 15:34:10 +03:00
parser.add_option("--use-file", type="string", help="Use a file, rather than real DNS calls")
2010-09-28 08:07:17 +04:00
parser.add_option("--update-list", type="string", help="Add DNS names from the given file")
2014-04-28 10:29:40 +04:00
parser.add_option("--update-cache", type="string", help="Cache database of already registered records")
2010-11-15 11:09:14 +03:00
parser.add_option("--fail-immediately", action='store_true', help="Exit on first failure")
2011-11-22 07:58:29 +04:00
parser.add_option("--no-credentials", dest='nocreds', action='store_true', help="don't try and get credentials")
2016-05-26 05:12:40 +03:00
parser.add_option("--no-substitutions", dest='nosubs', action='store_true', help="don't try and expands variables in file specified by --update-list")
2010-02-26 05:30:44 +03:00
creds = None
ccachename = None
opts, args = parser.parse_args()
if len(args) != 0:
parser.print_usage()
sys.exit(1)
lp = sambaopts.get_loadparm()
domain = lp.get("realm")
host = lp.get("netbios name")
2019-03-27 05:21:14 +03:00
all_interfaces = opts.all_interfaces
2010-03-09 15:34:10 +03:00
2019-03-27 05:21:14 +03:00
IPs = opts.current_ip or samba.interface_ips(lp, bool(all_interfaces)) or []
2015-08-07 05:57:20 +03:00
2010-02-26 05:30:44 +03:00
nsupdate_cmd = lp.get('nsupdate command')
2018-09-05 17:54:01 +03:00
dns_zone_scavenging = lp.get("dns zone scavenging")
2019-06-27 07:57:21 +03:00
if len(IPs) == 0:
print("No IP interfaces - skipping DNS updates\n")
parser.print_usage()
2010-02-26 05:30:44 +03:00
sys.exit(0)
2019-03-27 05:21:14 +03:00
rpc_server_ip = opts.rpc_server_ip or IPs[0]
2015-08-07 05:57:20 +03:00
2019-03-27 05:21:14 +03:00
IP6s = [ip for ip in IPs if ':' in ip]
IP4s = [ip for ip in IPs if ':' not in ip]
2011-06-02 11:09:17 +04:00
2018-10-30 03:06:20 +03:00
smb_conf = sambaopts.get_loadparm_path()
2011-06-02 11:09:17 +04:00
2010-09-19 07:57:26 +04:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("IPs: %s" % IPs)
2010-02-26 05:30:44 +03:00
2017-04-11 05:14:15 +03:00
def get_possible_rw_dns_server(creds, domain):
"""Get a list of possible read-write DNS servers, starting with
the SOA. The SOA is the correct answer, but old Samba domains
(4.6 and prior) do not maintain this value, so add NS servers
as well"""
ans_soa = check_one_dns_name(domain, 'SOA')
# Actually there is only one
2019-03-27 05:21:14 +03:00
hosts_soa = [str(a.mname).rstrip('.') for a in ans_soa]
2017-04-11 05:14:15 +03:00
# This is not strictly legit, but old Samba domains may have an
# unmaintained SOA record, so go for any NS that we can get a
# ticket to.
ans_ns = check_one_dns_name(domain, 'NS')
# Actually there is only one
2019-03-27 05:21:14 +03:00
hosts_ns = [str(a.target).rstrip('.') for a in ans_ns]
2017-04-11 05:14:15 +03:00
2019-03-27 05:21:14 +03:00
return hosts_soa + hosts_ns
2017-04-11 05:14:15 +03:00
def get_krb5_rw_dns_server(creds, domain):
"""Get a list of read-write DNS servers that we can obtain a ticket
2023-09-01 04:25:29 +03:00
to, starting with the SOA. The SOA is the correct answer, but
2017-04-11 05:14:15 +03:00
old Samba domains (4.6 and prior) do not maintain this value,
so continue with the NS servers as well until we get one that
the KDC will issue a ticket to.
"""
rw_dns_servers = get_possible_rw_dns_server(creds, domain)
# Actually there is only one
2019-03-27 05:21:14 +03:00
for i, target_hostname in enumerate(rw_dns_servers):
2017-04-11 05:14:15 +03:00
settings = {}
settings["lp_ctx"] = lp
settings["target_hostname"] = target_hostname
gensec_client = gensec.Security.start_client(settings)
gensec_client.set_credentials(creds)
gensec_client.set_target_service("DNS")
gensec_client.set_target_hostname(target_hostname)
gensec_client.want_feature(gensec.FEATURE_SEAL)
gensec_client.start_mech_by_sasl_name("GSSAPI")
2018-10-30 16:29:13 +03:00
server_to_client = b""
2017-04-11 05:14:15 +03:00
try:
(client_finished, client_to_server) = gensec_client.update(server_to_client)
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("Successfully obtained Kerberos ticket to DNS/%s as %s" \
% (target_hostname, creds.get_username()))
2017-04-11 05:14:15 +03:00
return target_hostname
except RuntimeError:
# Only raise an exception if they all failed
2019-03-27 05:21:14 +03:00
if i == len(rw_dns_servers) - 1:
raise
2012-03-12 22:23:50 +04:00
2010-02-26 05:30:44 +03:00
def get_credentials(lp):
2012-03-12 22:23:50 +04:00
"""# get credentials if we haven't got them already."""
2010-09-16 08:13:48 +04:00
from samba import credentials
2015-08-10 03:15:04 +03:00
global ccachename
2010-09-16 08:13:48 +04:00
creds = credentials.Credentials()
2010-02-26 05:30:44 +03:00
creds.guess(lp)
2010-04-08 23:01:17 +04:00
creds.set_machine_account(lp)
2010-09-16 08:13:48 +04:00
creds.set_krb_forwardable(credentials.NO_KRB_FORWARDABLE)
2010-02-26 05:30:44 +03:00
(tmp_fd, ccachename) = tempfile.mkstemp()
2013-04-11 05:33:34 +04:00
try:
2015-08-10 03:15:04 +03:00
if opts.use_file is not None:
return
2018-10-30 04:11:46 +03:00
creds.get_named_ccache(lp, ccachename)
2017-04-10 08:13:46 +03:00
# Now confirm we can get a ticket to the DNS server
2017-04-11 05:14:15 +03:00
get_krb5_rw_dns_server(creds, sub_vars['DNSDOMAIN'] + '.')
return creds
2015-02-26 02:29:23 +03:00
2013-04-11 05:33:34 +04:00
except RuntimeError as e:
os.unlink(ccachename)
raise e
2010-02-26 05:30:44 +03:00
class dnsobj(object):
2012-03-12 22:23:50 +04:00
"""an object to hold a parsed DNS line"""
2010-03-09 15:34:10 +03:00
def __init__(self, string_form):
list = string_form.split()
2012-03-12 22:29:34 +04:00
if len(list) < 3:
raise Exception("Invalid DNS entry %r" % string_form)
2010-02-26 05:30:44 +03:00
self.dest = None
self.port = None
self.ip = None
2010-02-26 05:58:32 +03:00
self.existing_port = None
self.existing_weight = None
2015-02-26 02:29:23 +03:00
self.existing_cname_target = None
2015-08-11 03:37:01 +03:00
self.rpc = False
self.zone = None
if list[0] == "RPC":
self.rpc = True
self.zone = list[1]
list = list[2:]
2010-03-09 15:34:10 +03:00
self.type = list[0]
2014-04-28 19:26:51 +04:00
self.name = list[1]
2014-10-07 17:35:34 +04:00
self.nameservers = []
2010-03-09 15:34:10 +03:00
if self.type == 'SRV':
2012-03-12 22:29:34 +04:00
if len(list) < 4:
raise Exception("Invalid DNS entry %r" % string_form)
2014-04-28 19:26:51 +04:00
self.dest = list[2]
2010-03-09 15:34:10 +03:00
self.port = list[3]
2011-06-02 11:09:17 +04:00
elif self.type in ['A', 'AAAA']:
2010-03-09 15:34:10 +03:00
self.ip = list[2] # usually $IP, which gets replaced
elif self.type == 'CNAME':
2014-04-28 19:26:51 +04:00
self.dest = list[2]
2011-09-21 02:59:30 +04:00
elif self.type == 'NS':
2014-04-28 19:26:51 +04:00
self.dest = list[2]
2010-03-09 15:34:10 +03:00
else:
2014-04-28 19:26:51 +04:00
raise Exception("Received unexpected DNS reply of type %s: %s" % (self.type, string_form))
2010-03-09 15:34:10 +03:00
2010-02-26 05:30:44 +03:00
def __str__(self):
2014-04-28 10:27:26 +04:00
if self.type == "A":
2012-09-27 20:30:47 +04:00
return "%s %s %s" % (self.type, self.name, self.ip)
2014-04-28 10:27:26 +04:00
if self.type == "AAAA":
2012-09-27 20:30:47 +04:00
return "%s %s %s" % (self.type, self.name, self.ip)
2014-04-28 10:27:26 +04:00
if self.type == "SRV":
2012-09-27 20:30:47 +04:00
return "%s %s %s %s" % (self.type, self.name, self.dest, self.port)
2014-04-28 10:27:26 +04:00
if self.type == "CNAME":
2012-09-27 20:30:47 +04:00
return "%s %s %s" % (self.type, self.name, self.dest)
2014-04-28 10:27:26 +04:00
if self.type == "NS":
2012-09-27 20:30:47 +04:00
return "%s %s %s" % (self.type, self.name, self.dest)
2010-02-26 05:30:44 +03:00
def parse_dns_line(line, sub_vars):
2012-03-12 22:23:50 +04:00
"""parse a DNS line from."""
2011-12-07 02:57:07 +04:00
if line.startswith("SRV _ldap._tcp.pdc._msdcs.") and not samdb.am_pdc():
2014-04-28 19:33:50 +04:00
# We keep this as compat to the dns_update_list of 4.0/4.1
2011-12-07 02:57:07 +04:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("Skipping PDC entry (%s) as we are not a PDC" % line)
2011-12-07 02:57:07 +04:00
return None
2010-02-26 05:30:44 +03:00
subline = samba.substitute_var(line, sub_vars)
2014-04-28 19:33:50 +04:00
if subline == '' or subline[0] == "#":
return None
2012-03-12 22:29:34 +04:00
return dnsobj(subline)
2010-02-26 05:30:44 +03:00
2012-03-12 22:23:50 +04:00
2010-02-26 05:30:44 +03:00
def hostname_match(h1, h2):
2012-03-12 22:23:50 +04:00
"""see if two hostnames match."""
2010-02-26 05:30:44 +03:00
h1 = str(h1)
h2 = str(h2)
return h1.lower().rstrip('.') == h2.lower().rstrip('.')
2017-04-10 08:10:27 +03:00
def get_resolver(d=None):
2019-03-27 05:21:14 +03:00
resolv_conf = os.getenv('RESOLV_CONF', default='/etc/resolv.conf')
2020-10-24 12:17:44 +03:00
resolver = DNSResolver(filename=resolv_conf, configure=True)
2015-02-26 02:29:23 +03:00
if d is not None and d.nameservers != []:
resolver.nameservers = d.nameservers
2017-04-10 08:10:27 +03:00
return resolver
def check_one_dns_name(name, name_type, d=None):
resolver = get_resolver(d)
2019-03-27 05:21:14 +03:00
if d and not d.nameservers:
2015-02-26 02:29:23 +03:00
d.nameservers = resolver.nameservers
2019-03-27 05:21:14 +03:00
# dns.resolver.Answer
2020-10-24 12:17:44 +03:00
return resolver.resolve(name, name_type)
2010-02-26 05:30:44 +03:00
def check_dns_name(d):
2012-03-12 22:23:50 +04:00
"""check that a DNS entry exists."""
2010-02-26 05:58:32 +03:00
normalised_name = d.name.rstrip('.') + '.'
2010-02-26 05:30:44 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("Looking for DNS entry %s as %s" % (d, normalised_name))
2012-03-12 22:23:50 +04:00
2010-03-09 15:34:10 +03:00
if opts.use_file is not None:
try:
dns_file = open(opts.use_file, "r")
except IOError:
return False
2012-03-12 22:23:50 +04:00
2023-02-21 01:45:11 +03:00
with dns_file:
for line in dns_file:
line = line.strip()
if line == '' or line[0] == "#":
continue
if line.lower() == str(d).lower():
return True
2010-03-09 15:34:10 +03:00
return False
2010-02-26 05:30:44 +03:00
try:
2015-02-26 02:29:23 +03:00
ans = check_one_dns_name(normalised_name, d.type, d)
2015-08-13 02:34:36 +03:00
except dns.exception.Timeout:
raise Exception("Timeout while waiting to contact a working DNS server while looking for %s as %s" % (d, normalised_name))
except dns.resolver.NoNameservers:
raise Exception("Unable to contact a working DNS server while looking for %s as %s" % (d, normalised_name))
except dns.resolver.NXDOMAIN:
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("The DNS entry %s, queried as %s does not exist" % (d, normalised_name))
2015-08-13 02:34:36 +03:00
return False
except dns.resolver.NoAnswer:
2010-11-30 07:23:39 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("The DNS entry %s, queried as %s does not hold this record type" % (d, normalised_name))
2010-02-26 05:30:44 +03:00
return False
2015-08-13 02:34:36 +03:00
except dns.exception.DNSException:
raise Exception("Failure while trying to resolve %s as %s" % (d, normalised_name))
2011-06-02 11:09:17 +04:00
if d.type in ['A', 'AAAA']:
2010-02-26 05:30:44 +03:00
# we need to be sure that our IP is there
for rdata in ans:
if str(rdata) == str(d.ip):
return True
2012-03-12 22:23:50 +04:00
elif d.type == 'CNAME':
2010-02-26 05:30:44 +03:00
for i in range(len(ans)):
if hostname_match(ans[i].target, d.dest):
return True
2015-02-26 02:29:23 +03:00
else:
d.existing_cname_target = str(ans[i].target)
2012-03-12 22:23:50 +04:00
elif d.type == 'NS':
2011-09-21 02:59:30 +04:00
for i in range(len(ans)):
if hostname_match(ans[i].target, d.dest):
return True
2012-03-12 22:23:50 +04:00
elif d.type == 'SRV':
2010-02-26 05:58:32 +03:00
for rdata in ans:
2010-02-26 05:30:44 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("Checking %s against %s" % (rdata, d))
2010-02-26 05:58:32 +03:00
if hostname_match(rdata.target, d.dest):
if str(rdata.port) == str(d.port):
return True
else:
d.existing_port = str(rdata.port)
d.existing_weight = str(rdata.weight)
2010-11-30 07:23:39 +03:00
2010-02-26 05:30:44 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("Lookup of %s succeeded, but we failed to find a matching DNS entry for %s" % (normalised_name, d))
2010-03-09 15:34:10 +03:00
2010-02-26 05:30:44 +03:00
return False
2011-12-07 02:57:07 +04:00
def get_subst_vars(samdb):
2012-03-12 22:23:50 +04:00
"""get the list of substitution vars."""
2010-09-19 07:57:26 +04:00
global lp, am_rodc
2010-02-26 05:30:44 +03:00
vars = {}
2011-08-22 11:41:08 +04:00
vars['DNSDOMAIN'] = samdb.domain_dns_name()
vars['DNSFOREST'] = samdb.forest_dns_name()
vars['HOSTNAME'] = samdb.host_dns_name()
2010-02-26 05:30:44 +03:00
vars['NTDSGUID'] = samdb.get_ntds_GUID()
vars['SITE'] = samdb.server_site_name()
2011-08-19 11:32:26 +04:00
res = samdb.search(base=samdb.get_default_basedn(), scope=SCOPE_BASE, attrs=["objectGUID"])
2010-02-26 05:30:44 +03:00
guid = samdb.schema_format_value("objectGUID", res[0]['objectGUID'][0])
2018-10-02 16:45:53 +03:00
vars['DOMAINGUID'] = get_string(guid)
2014-04-28 19:33:50 +04:00
vars['IF_DC'] = ""
vars['IF_RWDC'] = "# "
vars['IF_RODC'] = "# "
vars['IF_PDC'] = "# "
vars['IF_GC'] = "# "
vars['IF_RWGC'] = "# "
vars['IF_ROGC'] = "# "
vars['IF_DNS_DOMAIN'] = "# "
vars['IF_RWDNS_DOMAIN'] = "# "
vars['IF_RODNS_DOMAIN'] = "# "
vars['IF_DNS_FOREST'] = "# "
vars['IF_RWDNS_FOREST'] = "# "
vars['IF_R0DNS_FOREST'] = "# "
2010-09-19 07:57:26 +04:00
am_rodc = samdb.am_rodc()
2014-04-28 19:33:50 +04:00
if am_rodc:
vars['IF_RODC'] = ""
else:
vars['IF_RWDC'] = ""
if samdb.am_pdc():
vars['IF_PDC'] = ""
# check if we "are DNS server"
res = samdb.search(base=samdb.get_config_basedn(),
expression='(objectguid=%s)' % vars['NTDSGUID'],
attrs=["options", "msDS-hasMasterNCs"])
if len(res) == 1:
if "options" in res[0]:
options = int(res[0]["options"][0])
if (options & dsdb.DS_NTDSDSA_OPT_IS_GC) != 0:
vars['IF_GC'] = ""
if am_rodc:
vars['IF_ROGC'] = ""
else:
vars['IF_RWGC'] = ""
basedn = str(samdb.get_default_basedn())
2014-09-26 02:42:16 +04:00
forestdn = str(samdb.get_root_basedn())
2014-04-28 19:33:50 +04:00
if "msDS-hasMasterNCs" in res[0]:
for e in res[0]["msDS-hasMasterNCs"]:
if str(e) == "DC=DomainDnsZones,%s" % basedn:
vars['IF_DNS_DOMAIN'] = ""
if am_rodc:
vars['IF_RODNS_DOMAIN'] = ""
else:
vars['IF_RWDNS_DOMAIN'] = ""
2014-09-26 02:42:16 +04:00
if str(e) == "DC=ForestDnsZones,%s" % forestdn:
2014-04-28 19:33:50 +04:00
vars['IF_DNS_FOREST'] = ""
if am_rodc:
vars['IF_RODNS_FOREST'] = ""
else:
vars['IF_RWDNS_FOREST'] = ""
2010-09-19 07:57:26 +04:00
2010-02-26 05:30:44 +03:00
return vars
2014-04-28 10:29:40 +04:00
def call_nsupdate(d, op="add"):
2012-03-12 22:23:50 +04:00
"""call nsupdate for an entry."""
2012-12-20 18:57:43 +04:00
global ccachename, nsupdate_cmd, krb5conf
2010-02-26 05:30:44 +03:00
2014-04-28 10:29:40 +04:00
assert(op in ["add", "delete"])
2010-03-09 15:34:10 +03:00
if opts.use_file is not None:
2019-02-07 11:40:19 +03:00
if opts.verbose:
print("Use File instead of nsupdate for %s (%s)" % (d, op))
2012-09-26 04:02:43 +04:00
try:
rfile = open(opts.use_file, 'r+')
except IOError:
# Perhaps create it
2023-02-21 01:45:11 +03:00
open(opts.use_file, 'w+').close()
2012-09-26 04:02:43 +04:00
# Open it for reading again, in case someone else got to it first
rfile = open(opts.use_file, 'r+')
2012-09-26 03:48:48 +04:00
fcntl.lockf(rfile, fcntl.LOCK_EX)
(file_dir, file_name) = os.path.split(opts.use_file)
(tmp_fd, tmpfile) = tempfile.mkstemp(dir=file_dir, prefix=file_name, suffix="XXXXXX")
wfile = os.fdopen(tmp_fd, 'a')
for line in rfile:
2014-04-28 10:29:40 +04:00
if op == "delete":
l = parse_dns_line(line, {})
if str(l).lower() == str(d).lower():
continue
2012-09-26 03:48:48 +04:00
wfile.write(line)
2014-04-28 10:29:40 +04:00
if op == "add":
wfile.write(str(d)+"\n")
2023-02-21 01:45:11 +03:00
rfile.close()
wfile.close()
2012-09-26 03:48:48 +04:00
os.rename(tmpfile, opts.use_file)
2010-03-09 15:34:10 +03:00
return
2019-02-07 11:40:19 +03:00
if opts.verbose:
print("Calling nsupdate for %s (%s)" % (d, op))
2010-09-28 08:07:17 +04:00
normalised_name = d.name.rstrip('.') + '.'
2010-02-26 05:30:44 +03:00
(tmp_fd, tmpfile) = tempfile.mkstemp()
f = os.fdopen(tmp_fd, 'w')
2017-02-17 08:24:27 +03:00
2019-01-03 18:44:45 +03:00
resolver = get_resolver(d)
2017-02-17 08:24:27 +03:00
2019-01-03 18:44:45 +03:00
# Local the zone for this name
zone = dns.resolver.zone_for_name(normalised_name,
resolver=resolver)
# Now find the SOA, or if we can't get a ticket to the SOA,
# any server with an NS record we can get a ticket for.
#
# Thanks to the Kerberos Credentials cache this is not
# expensive inside the loop
server = get_krb5_rw_dns_server(creds, zone)
f.write('server %s\n' % server)
2017-02-17 08:24:27 +03:00
2010-02-26 05:30:44 +03:00
if d.type == "A":
2014-04-28 10:29:40 +04:00
f.write("update %s %s %u A %s\n" % (op, normalised_name, default_ttl, d.ip))
2011-06-02 11:09:17 +04:00
if d.type == "AAAA":
2014-04-28 10:29:40 +04:00
f.write("update %s %s %u AAAA %s\n" % (op, normalised_name, default_ttl, d.ip))
2010-02-26 05:30:44 +03:00
if d.type == "SRV":
2014-04-28 10:29:40 +04:00
if op == "add" and d.existing_port is not None:
2010-09-28 08:07:17 +04:00
f.write("update delete %s SRV 0 %s %s %s\n" % (normalised_name, d.existing_weight,
2010-02-26 05:58:32 +03:00
d.existing_port, d.dest))
2014-04-28 10:29:40 +04:00
f.write("update %s %s %u SRV 0 100 %s %s\n" % (op, normalised_name, default_ttl, d.port, d.dest))
2010-02-26 05:30:44 +03:00
if d.type == "CNAME":
2014-04-28 10:29:40 +04:00
f.write("update %s %s %u CNAME %s\n" % (op, normalised_name, default_ttl, d.dest))
2011-09-21 02:59:30 +04:00
if d.type == "NS":
2014-04-28 10:29:40 +04:00
f.write("update %s %s %u NS %s\n" % (op, normalised_name, default_ttl, d.dest))
2010-02-26 05:30:44 +03:00
if opts.verbose:
f.write("show\n")
f.write("send\n")
f.close()
2015-08-25 18:26:05 +03:00
# Set a bigger MTU size to work around a bug in nsupdate's doio_send()
os.environ["SOCKET_WRAPPER_MTU"] = "2000"
2011-04-07 10:02:44 +04:00
global error_count
2011-11-22 07:58:29 +04:00
if ccachename:
os.environ["KRB5CCNAME"] = ccachename
2010-11-15 11:09:14 +03:00
try:
2010-12-08 08:27:38 +03:00
cmd = nsupdate_cmd[:]
cmd.append(tmpfile)
2014-09-11 12:59:20 +04:00
env = os.environ
2012-12-20 18:57:43 +04:00
if krb5conf:
env["KRB5_CONFIG"] = krb5conf
2011-11-22 07:58:29 +04:00
if ccachename:
2012-12-20 18:57:43 +04:00
env["KRB5CCNAME"] = ccachename
2011-11-22 07:58:29 +04:00
ret = subprocess.call(cmd, shell=False, env=env)
2011-04-07 01:34:47 +04:00
if ret != 0:
if opts.fail_immediately:
2011-09-21 02:59:30 +04:00
if opts.verbose:
print("Failed update with %s" % tmpfile)
2011-04-07 01:34:47 +04:00
sys.exit(1)
error_count = error_count + 1
if opts.verbose:
print("Failed nsupdate: %d" % ret)
2018-02-14 00:33:06 +03:00
except Exception as estr:
2010-11-15 11:09:14 +03:00
if opts.fail_immediately:
sys.exit(1)
error_count = error_count + 1
2010-11-17 04:33:02 +03:00
if opts.verbose:
print("Failed nsupdate: %s : %s" % (str(d), estr))
2010-02-26 05:30:44 +03:00
os.unlink(tmpfile)
2015-08-25 18:26:05 +03:00
# Let socket_wrapper set the default MTU size
os.environ["SOCKET_WRAPPER_MTU"] = "0"
2010-02-26 05:30:44 +03:00
2010-09-19 07:57:26 +04:00
2015-08-11 03:37:01 +03:00
def call_samba_tool(d, op="add", zone=None):
2015-02-26 02:29:23 +03:00
"""call samba-tool dns to update an entry."""
assert(op in ["add", "delete"])
if (sub_vars['DNSFOREST'] != sub_vars['DNSDOMAIN']) and \
sub_vars['DNSFOREST'].endswith('.' + sub_vars['DNSDOMAIN']):
2018-09-27 20:15:49 +03:00
print("Refusing to use samba-tool when forest %s is under domain %s" \
% (sub_vars['DNSFOREST'], sub_vars['DNSDOMAIN']))
2015-02-26 02:29:23 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("Calling samba-tool dns for %s (%s)" % (d, op))
2015-02-26 02:29:23 +03:00
normalised_name = d.name.rstrip('.') + '.'
2015-08-11 03:37:01 +03:00
if zone is None:
if normalised_name == (sub_vars['DNSDOMAIN'] + '.'):
short_name = '@'
zone = sub_vars['DNSDOMAIN']
elif normalised_name == (sub_vars['DNSFOREST'] + '.'):
short_name = '@'
zone = sub_vars['DNSFOREST']
elif normalised_name == ('_msdcs.' + sub_vars['DNSFOREST'] + '.'):
short_name = '@'
2015-02-26 02:29:23 +03:00
zone = '_msdcs.' + sub_vars['DNSFOREST']
else:
2015-08-11 03:37:01 +03:00
if not normalised_name.endswith('.' + sub_vars['DNSDOMAIN'] + '.'):
2018-09-27 20:15:49 +03:00
print("Not Calling samba-tool dns for %s (%s), %s not in %s" % (d, op, normalised_name, sub_vars['DNSDOMAIN'] + '.'))
2015-08-11 03:37:01 +03:00
return False
elif normalised_name.endswith('._msdcs.' + sub_vars['DNSFOREST'] + '.'):
zone = '_msdcs.' + sub_vars['DNSFOREST']
else:
zone = sub_vars['DNSDOMAIN']
len_zone = len(zone)+2
short_name = normalised_name[:-len_zone]
else:
2015-02-26 02:29:23 +03:00
len_zone = len(zone)+2
short_name = normalised_name[:-len_zone]
if d.type == "A":
2015-08-07 05:57:20 +03:00
args = [rpc_server_ip, zone, short_name, "A", d.ip]
2015-02-26 02:29:23 +03:00
if d.type == "AAAA":
2015-08-07 05:57:20 +03:00
args = [rpc_server_ip, zone, short_name, "AAAA", d.ip]
2015-02-26 02:29:23 +03:00
if d.type == "SRV":
if op == "add" and d.existing_port is not None:
2019-08-29 22:32:30 +03:00
print("Not handling modify of existing SRV %s using samba-tool" % d)
2015-02-26 02:29:23 +03:00
return False
2018-10-11 12:54:32 +03:00
args = [rpc_server_ip, zone, short_name, "SRV",
"%s %s %s %s" % (d.dest, d.port, "0", "100")]
2015-02-26 02:29:23 +03:00
if d.type == "CNAME":
if d.existing_cname_target is None:
2015-08-07 05:57:20 +03:00
args = [rpc_server_ip, zone, short_name, "CNAME", d.dest]
2015-02-26 02:29:23 +03:00
else:
op = "update"
2015-08-07 05:57:20 +03:00
args = [rpc_server_ip, zone, short_name, "CNAME",
2015-02-26 02:29:23 +03:00
d.existing_cname_target.rstrip('.'), d.dest]
if d.type == "NS":
2015-08-07 05:57:20 +03:00
args = [rpc_server_ip, zone, short_name, "NS", d.dest]
2015-02-26 02:29:23 +03:00
2018-10-30 03:06:20 +03:00
if smb_conf and args:
args += ["--configfile=" + smb_conf]
2015-02-26 02:29:23 +03:00
global error_count
try:
cmd = cmd_dns()
if opts.verbose:
2021-09-07 16:04:16 +03:00
print(f'Calling samba-tool dns {op} --use-kerberos off -P {args}')
2022-09-07 06:34:23 +03:00
command, args = cmd._resolve("dns", op, "--use-kerberos", "off", "-P", *args)
ret = command._run(*args)
2016-07-27 00:40:09 +03:00
if ret == -1:
if opts.fail_immediately:
sys.exit(1)
error_count = error_count + 1
if opts.verbose:
2017-04-11 05:23:49 +03:00
print("Failed 'samba-tool dns' based update of %s" % (str(d)))
2018-02-14 00:33:06 +03:00
except Exception as estr:
2015-02-26 02:29:23 +03:00
if opts.fail_immediately:
sys.exit(1)
error_count = error_count + 1
if opts.verbose:
print("Failed 'samba-tool dns' based update: %s : %s" % (str(d), estr))
2016-07-26 02:34:05 +03:00
raise
2015-02-26 02:29:23 +03:00
2019-02-07 11:42:36 +03:00
irpc_wb = None
def cached_irpc_wb(lp):
global irpc_wb
if irpc_wb is not None:
return irpc_wb
irpc_wb = winbind.winbind("irpc:winbind_server", lp)
return irpc_wb
2014-04-28 10:29:40 +04:00
def rodc_dns_update(d, t, op):
2010-09-19 07:57:26 +04:00
'''a single DNS update via the RODC netlogon call'''
global sub_vars
2014-04-28 10:29:40 +04:00
assert(op in ["add", "delete"])
2010-09-19 07:57:26 +04:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("Calling netlogon RODC update for %s" % d)
2010-09-19 07:57:26 +04:00
2010-09-20 00:02:40 +04:00
typemap = {
netlogon.NlDnsLdapAtSite : netlogon.NlDnsInfoTypeNone,
netlogon.NlDnsGcAtSite : netlogon.NlDnsDomainNameAlias,
netlogon.NlDnsDsaCname : netlogon.NlDnsDomainNameAlias,
netlogon.NlDnsKdcAtSite : netlogon.NlDnsInfoTypeNone,
netlogon.NlDnsDcAtSite : netlogon.NlDnsInfoTypeNone,
netlogon.NlDnsRfc1510KdcAtSite : netlogon.NlDnsInfoTypeNone,
netlogon.NlDnsGenericGcAtSite : netlogon.NlDnsDomainNameAlias
}
2019-02-07 11:42:36 +03:00
w = cached_irpc_wb(lp)
2010-09-19 07:57:26 +04:00
dns_names = netlogon.NL_DNS_NAME_INFO_ARRAY()
dns_names.count = 1
name = netlogon.NL_DNS_NAME_INFO()
name.type = t
2010-09-20 00:02:40 +04:00
name.dns_domain_info_type = typemap[t]
2010-09-19 07:57:26 +04:00
name.priority = 0
name.weight = 0
if d.port is not None:
name.port = int(d.port)
2014-04-28 10:29:40 +04:00
if op == "add":
name.dns_register = True
else:
name.dns_register = False
2010-09-19 07:57:26 +04:00
dns_names.names = [ name ]
2020-07-04 05:27:06 +03:00
site_name = sub_vars['SITE']
2010-09-19 07:57:26 +04:00
2010-11-18 06:53:20 +03:00
global error_count
2010-09-19 07:57:26 +04:00
try:
ret_names = w.DsrUpdateReadOnlyServerDnsRecords(site_name, default_ttl, dns_names)
if ret_names.names[0].status != 0:
print("Failed to set DNS entry: %s (status %u)" % (d, ret_names.names[0].status))
2010-11-18 06:53:20 +03:00
error_count = error_count + 1
2018-02-14 00:33:06 +03:00
except RuntimeError as reason:
2010-09-20 00:02:40 +04:00
print("Error setting DNS entry of type %u: %s: %s" % (t, d, reason))
2010-11-18 06:53:20 +03:00
error_count = error_count + 1
2019-02-07 11:42:36 +03:00
if opts.verbose:
print("Called netlogon RODC update for %s" % d)
2010-11-18 06:53:20 +03:00
if error_count != 0 and opts.fail_immediately:
sys.exit(1)
2010-09-19 07:57:26 +04:00
2014-04-28 10:29:40 +04:00
def call_rodc_update(d, op="add"):
2010-09-19 07:57:26 +04:00
'''RODCs need to use the netlogon API for nsupdate'''
global lp, sub_vars
2014-04-28 10:29:40 +04:00
assert(op in ["add", "delete"])
2010-09-19 07:57:26 +04:00
# we expect failure for 3268 if we aren't a GC
if d.port is not None and int(d.port) == 3268:
return
# map the DNS request to a netlogon update type
map = {
2010-09-20 00:02:40 +04:00
netlogon.NlDnsLdapAtSite : '_ldap._tcp.${SITE}._sites.${DNSDOMAIN}',
2010-09-19 07:57:26 +04:00
netlogon.NlDnsGcAtSite : '_ldap._tcp.${SITE}._sites.gc._msdcs.${DNSDOMAIN}',
netlogon.NlDnsDsaCname : '${NTDSGUID}._msdcs.${DNSFOREST}',
netlogon.NlDnsKdcAtSite : '_kerberos._tcp.${SITE}._sites.dc._msdcs.${DNSDOMAIN}',
netlogon.NlDnsDcAtSite : '_ldap._tcp.${SITE}._sites.dc._msdcs.${DNSDOMAIN}',
netlogon.NlDnsRfc1510KdcAtSite : '_kerberos._tcp.${SITE}._sites.${DNSDOMAIN}',
netlogon.NlDnsGenericGcAtSite : '_gc._tcp.${SITE}._sites.${DNSFOREST}'
}
for t in map:
subname = samba.substitute_var(map[t], sub_vars)
if subname.lower() == d.name.lower():
# found a match - do the update
2014-04-28 10:29:40 +04:00
rodc_dns_update(d, t, op)
2010-09-19 07:57:26 +04:00
return
if opts.verbose:
print("Unable to map to netlogon DNS update: %s" % d)
2010-02-26 05:30:44 +03:00
# get the list of DNS entries we should have
2019-03-27 05:21:14 +03:00
dns_update_list = opts.update_list or lp.private_path('dns_update_list')
2010-02-26 05:30:44 +03:00
2019-03-27 05:21:14 +03:00
dns_update_cache = opts.update_cache or lp.private_path('dns_update_cache')
2014-04-28 10:29:40 +04:00
2015-07-30 18:38:34 +03:00
krb5conf = None
# only change the krb5.conf if we are not in selftest
if 'SOCKET_WRAPPER_DIR' not in os.environ:
# use our private krb5.conf to avoid problems with the wrong domain
# bind9 nsupdate wants the default domain set
krb5conf = lp.private_path('krb5.conf')
os.environ['KRB5_CONFIG'] = krb5conf
2010-09-27 04:40:05 +04:00
2019-06-27 07:57:21 +03:00
try:
file = open(dns_update_list, "r")
except OSError as e:
2023-02-22 02:07:30 +03:00
if opts.update_list:
2019-06-27 07:57:21 +03:00
print("The specified update list does not exist")
else:
print("The server update list was not found, "
"and --update-list was not provided.")
print(e)
print()
parser.print_usage()
sys.exit(1)
2010-02-26 05:30:44 +03:00
2012-09-26 04:02:43 +04:00
if opts.nosubs:
sub_vars = {}
else:
samdb = SamDB(url=lp.samdb_url(), session_info=system_session(), lp=lp)
2011-12-07 02:57:07 +04:00
2012-09-26 04:02:43 +04:00
# get the substitution dictionary
sub_vars = get_subst_vars(samdb)
2010-02-26 05:30:44 +03:00
# build up a list of update commands to pass to nsupdate
update_list = []
dns_list = []
2014-04-28 10:29:40 +04:00
cache_list = []
delete_list = []
2010-02-26 05:30:44 +03:00
2011-08-24 09:47:27 +04:00
dup_set = set()
2014-04-28 10:29:40 +04:00
cache_set = set()
rebuild_cache = False
try:
cfile = open(dns_update_cache, 'r+')
except IOError:
# Perhaps create it
2023-02-21 01:45:11 +03:00
open(dns_update_cache, 'w+').close()
2014-04-28 10:29:40 +04:00
# Open it for reading again, in case someone else got to it first
cfile = open(dns_update_cache, 'r+')
fcntl.lockf(cfile, fcntl.LOCK_EX)
for line in cfile:
line = line.strip()
if line == '' or line[0] == "#":
continue
c = parse_dns_line(line, {})
if c is None:
continue
if str(c) not in cache_set:
cache_list.append(c)
cache_set.add(str(c))
2011-08-24 09:47:27 +04:00
2023-02-21 01:45:11 +03:00
cfile.close()
2018-03-12 04:45:48 +03:00
site_specific_rec = []
2010-02-26 05:30:44 +03:00
# read each line, and check that the DNS name exists
2010-04-27 12:24:52 +04:00
for line in file:
line = line.strip()
2018-03-12 04:45:48 +03:00
if '${SITE}' in line:
site_specific_rec.append(line)
2010-04-27 12:24:52 +04:00
if line == '' or line[0] == "#":
2010-02-26 05:30:44 +03:00
continue
d = parse_dns_line(line, sub_vars)
2011-12-07 02:57:07 +04:00
if d is None:
continue
2011-06-02 11:09:17 +04:00
if d.type == 'A' and len(IP4s) == 0:
continue
if d.type == 'AAAA' and len(IP6s) == 0:
continue
2011-08-24 09:47:27 +04:00
if str(d) not in dup_set:
dns_list.append(d)
dup_set.add(str(d))
2010-02-26 05:30:44 +03:00
2023-02-21 01:45:11 +03:00
file.close()
2018-03-12 04:45:48 +03:00
# Perform automatic site coverage by default
auto_coverage = True
if not am_rodc and auto_coverage:
site_names = kcc_utils.uncovered_sites_to_cover(samdb,
samdb.server_site_name())
# Duplicate all site specific records for the uncovered site
for site in site_names:
to_add = [samba.substitute_var(line, {'SITE': site})
for line in site_specific_rec]
for site_line in to_add:
d = parse_dns_line(site_line,
sub_vars=sub_vars)
if d is not None and str(d) not in dup_set:
dns_list.append(d)
dup_set.add(str(d))
2010-02-26 05:30:44 +03:00
# now expand the entries, if any are A record with ip set to $IP
# then replace with multiple entries, one for each interface IP
for d in dns_list:
2011-06-02 11:09:17 +04:00
if d.ip != "$IP":
continue
if d.type == 'A':
d.ip = IP4s[0]
for i in range(len(IP4s)-1):
d2 = dnsobj(str(d))
d2.ip = IP4s[i+1]
dns_list.append(d2)
if d.type == 'AAAA':
d.ip = IP6s[0]
for i in range(len(IP6s)-1):
2010-11-15 02:54:50 +03:00
d2 = dnsobj(str(d))
2011-06-02 11:09:17 +04:00
d2.ip = IP6s[i+1]
2010-02-26 05:30:44 +03:00
dns_list.append(d2)
# now check if the entries already exist on the DNS server
for d in dns_list:
2014-04-28 10:29:40 +04:00
found = False
for c in cache_list:
if str(c).lower() == str(d).lower():
found = True
break
if not found:
rebuild_cache = True
2017-02-27 01:39:51 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("need cache add: %s" % d)
2018-09-05 17:54:01 +03:00
if dns_zone_scavenging:
update_list.append(d)
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("scavenging requires update: %s" % d)
2018-09-05 17:54:01 +03:00
elif opts.all_names:
update_list.append(d)
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("force update: %s" % d)
2018-09-05 17:54:01 +03:00
elif not check_dns_name(d):
update_list.append(d)
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("need update: %s" % d)
2010-02-26 05:30:44 +03:00
2014-04-28 10:29:40 +04:00
for c in cache_list:
found = False
for d in dns_list:
if str(c).lower() == str(d).lower():
found = True
break
if found:
continue
rebuild_cache = True
2017-02-27 01:39:51 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("need cache remove: %s" % c)
2014-04-28 10:29:40 +04:00
if not opts.all_names and not check_dns_name(c):
continue
delete_list.append(c)
2015-08-10 03:05:19 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("need delete: %s" % c)
2014-04-28 10:29:40 +04:00
if len(delete_list) == 0 and len(update_list) == 0 and not rebuild_cache:
2010-02-26 05:30:44 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("No DNS updates needed")
2010-02-26 05:30:44 +03:00
sys.exit(0)
2015-08-10 03:15:04 +03:00
else:
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("%d DNS updates and %d DNS deletes needed" % (len(update_list), len(delete_list)))
2010-02-26 05:30:44 +03:00
2015-02-26 02:29:23 +03:00
use_samba_tool = opts.use_samba_tool
use_nsupdate = opts.use_nsupdate
2010-02-26 05:30:44 +03:00
# get our krb5 creds
2019-11-05 13:25:14 +03:00
if (delete_list or update_list) and not opts.nocreds:
2015-08-10 03:15:04 +03:00
try:
creds = get_credentials(lp)
except RuntimeError as e:
ccachename = None
2015-02-26 02:29:23 +03:00
2015-08-10 03:15:04 +03:00
if sub_vars['IF_RWDNS_DOMAIN'] == "# ":
raise
2015-02-26 02:29:23 +03:00
2015-08-10 03:15:04 +03:00
if use_nsupdate:
raise
2015-02-26 02:29:23 +03:00
2018-09-27 20:15:49 +03:00
print("Failed to get Kerberos credentials, falling back to samba-tool: %s" % e)
2015-08-10 03:15:04 +03:00
use_samba_tool = True
2015-02-26 02:29:23 +03:00
2014-04-28 10:29:40 +04:00
# ask nsupdate to delete entries as needed
for d in delete_list:
2015-08-11 03:37:01 +03:00
if d.rpc or (not use_nsupdate and use_samba_tool):
if opts.verbose:
2019-11-05 13:25:52 +03:00
print("delete (samba-tool): %s" % d)
2015-08-11 03:37:01 +03:00
call_samba_tool(d, op="delete", zone=d.zone)
2015-02-26 02:29:23 +03:00
elif am_rodc:
2014-04-28 10:29:40 +04:00
if d.name.lower() == domain.lower():
2015-08-10 03:05:19 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("skip delete (rodc): %s" % d)
2014-04-28 10:29:40 +04:00
continue
2023-09-01 04:26:04 +03:00
if d.type not in [ 'A', 'AAAA' ]:
2015-08-10 03:05:19 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("delete (rodc): %s" % d)
2014-04-28 10:29:40 +04:00
call_rodc_update(d, op="delete")
else:
2015-08-10 03:05:19 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("delete (nsupdate): %s" % d)
2014-04-28 10:29:40 +04:00
call_nsupdate(d, op="delete")
else:
2015-08-10 03:05:19 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("delete (nsupdate): %s" % d)
2014-04-28 10:29:40 +04:00
call_nsupdate(d, op="delete")
2010-02-26 05:30:44 +03:00
# ask nsupdate to add entries as needed
for d in update_list:
2015-08-11 03:37:01 +03:00
if d.rpc or (not use_nsupdate and use_samba_tool):
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("update (samba-tool): %s" % d)
2015-08-11 03:37:01 +03:00
call_samba_tool(d, zone=d.zone)
2015-02-26 02:29:23 +03:00
elif am_rodc:
2010-09-30 04:33:49 +04:00
if d.name.lower() == domain.lower():
2015-08-10 03:05:19 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("skip (rodc): %s" % d)
2010-09-30 04:33:49 +04:00
continue
2023-09-01 04:26:04 +03:00
if d.type not in [ 'A', 'AAAA' ]:
2015-08-10 03:05:19 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("update (rodc): %s" % d)
2010-09-30 04:33:49 +04:00
call_rodc_update(d)
else:
2015-08-10 03:05:19 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("update (nsupdate): %s" % d)
2010-09-30 04:33:49 +04:00
call_nsupdate(d)
2010-09-19 07:57:26 +04:00
else:
2015-08-10 03:05:19 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("update(nsupdate): %s" % d)
2010-09-19 07:57:26 +04:00
call_nsupdate(d)
2010-02-26 05:30:44 +03:00
2014-04-28 10:29:40 +04:00
if rebuild_cache:
2018-09-27 20:15:49 +03:00
print("Rebuilding cache at %s" % dns_update_cache)
2014-04-28 10:29:40 +04:00
(file_dir, file_name) = os.path.split(dns_update_cache)
(tmp_fd, tmpfile) = tempfile.mkstemp(dir=file_dir, prefix=file_name, suffix="XXXXXX")
wfile = os.fdopen(tmp_fd, 'a')
for d in dns_list:
2015-08-13 02:34:36 +03:00
if opts.verbose:
2018-09-27 20:15:49 +03:00
print("Adding %s to %s" % (str(d), file_name))
2014-04-28 10:29:40 +04:00
wfile.write(str(d)+"\n")
2023-02-21 01:45:11 +03:00
wfile.close()
2014-04-28 10:29:40 +04:00
os.rename(tmpfile, dns_update_cache)
2010-02-26 05:30:44 +03:00
# delete the ccache if we created it
if ccachename is not None:
os.unlink(ccachename)
2010-11-15 11:09:14 +03:00
2010-11-17 04:33:02 +03:00
if error_count != 0:
print("Failed update of %u entries" % error_count)
2010-11-15 11:09:14 +03:00
sys.exit(error_count)