s4:kdc: Implement KDC plugin hardware authentication policy
[samba.git] / source4 / scripting / bin / samba_dnsupdate
index d18d8bd1f1903fc994fb9ce729f38e940b042449..6d9d5bc3deebcdd002d38d40425e5deac9050621 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # vim: expandtab
 #
 # update our DNS names using TSIG-GSS
@@ -29,7 +29,7 @@ import subprocess
 # and don't get swallowed by a timeout
 os.environ['PYTHONUNBUFFERED'] = '1'
 
-# forcing GMT avoids a problem in some timezones with kerberos. Both MIT
+# forcing GMT avoids a problem in some timezones with kerberos. Both MIT and
 # 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)
@@ -48,8 +48,11 @@ from samba.samdb import SamDB
 from samba.dcerpc import netlogon, winbind
 from samba.netcmd.dns import cmd_dns
 from samba import gensec
+from samba.kcc import kcc_utils
+from samba.common import get_string
+import ldb
 
-samba.ensure_third_party_module("dns", "dnspython")
+from samba.dnsresolver import DNSResolver
 import dns.resolver
 import dns.exception
 
@@ -57,7 +60,7 @@ default_ttl = 900
 am_rodc = False
 error_count = 0
 
-parser = optparse.OptionParser("samba_dnsupdate")
+parser = optparse.OptionParser("samba_dnsupdate [options]")
 sambaopts = options.SambaOptions(parser)
 parser.add_option_group(sambaopts)
 parser.add_option_group(options.VersionOptions(parser))
@@ -88,39 +91,79 @@ lp = sambaopts.get_loadparm()
 
 domain = lp.get("realm")
 host = lp.get("netbios name")
-if opts.all_interfaces:
-    all_interfaces = True
-else:
-    all_interfaces = False
+all_interfaces = opts.all_interfaces
 
-if opts.current_ip:
-    IPs = opts.current_ip
-else:
-    IPs = samba.interface_ips(lp, all_interfaces)
+IPs = opts.current_ip or samba.interface_ips(lp, bool(all_interfaces)) or []
 
 nsupdate_cmd = lp.get('nsupdate command')
+dns_zone_scavenging = lp.get("dns zone scavenging")
 
 if len(IPs) == 0:
-    print "No IP interfaces - skipping DNS updates"
+    print("No IP interfaces - skipping DNS updates\n")
+    parser.print_usage()
     sys.exit(0)
 
-if opts.rpc_server_ip:
-    rpc_server_ip = opts.rpc_server_ip
-else:
-    rpc_server_ip = IPs[0]
+rpc_server_ip = opts.rpc_server_ip or IPs[0]
 
-IP6s = []
-IP4s = []
-for i in IPs:
-    if i.find(':') != -1:
-        IP6s.append(i)
-    else:
-        IP4s.append(i)
+IP6s = [ip for ip in IPs if ':' in ip]
+IP4s = [ip for ip in IPs if ':' not in ip]
 
+smb_conf = sambaopts.get_loadparm_path()
 
 if opts.verbose:
-    print "IPs: %s" % IPs
-
+    print("IPs: %s" % IPs)
+
+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
+    hosts_soa = [str(a.mname).rstrip('.') for a in ans_soa]
+
+    # 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
+    hosts_ns = [str(a.target).rstrip('.') for a in ans_ns]
+
+    return hosts_soa + hosts_ns
+
+def get_krb5_rw_dns_server(creds, domain):
+    """Get a list of read-write DNS servers that we can obtain a ticket
+       to, starting with the SOA.  The SOA is the correct answer, but
+       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
+    for i, target_hostname in enumerate(rw_dns_servers):
+        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")
+        server_to_client = b""
+        try:
+            (client_finished, client_to_server) = gensec_client.update(server_to_client)
+            if opts.verbose:
+                print("Successfully obtained Kerberos ticket to DNS/%s as %s" \
+                    % (target_hostname, creds.get_username()))
+            return target_hostname
+        except RuntimeError:
+            # Only raise an exception if they all failed
+            if i == len(rw_dns_servers) - 1:
+                raise
 
 def get_credentials(lp):
     """# get credentials if we haven't got them already."""
@@ -132,37 +175,14 @@ def get_credentials(lp):
     creds.set_krb_forwardable(credentials.NO_KRB_FORWARDABLE)
     (tmp_fd, ccachename) = tempfile.mkstemp()
     try:
-        creds.get_named_ccache(lp, ccachename)
-
         if opts.use_file is not None:
             return
 
-        # Now confirm we can get a ticket to a DNS server
-        ans = check_one_dns_name(sub_vars['DNSDOMAIN'] + '.', 'NS')
-        for i in range(len(ans)):
-            target_hostname = str(ans[i].target).rstrip('.')
-            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")
-            server_to_client = ""
-            try:
-                (client_finished, client_to_server) = gensec_client.update(server_to_client)
-                if opts.verbose:
-                    print "Successfully obtained Kerberos ticket to DNS/%s as %s" \
-                            % (target_hostname, creds.get_username())
-                return
-            except RuntimeError:
-                # Only raise an exception if they all failed
-                if i != len(ans) - 1:
-                    pass
-                raise
+        creds.get_named_ccache(lp, ccachename)
+
+        # Now confirm we can get a ticket to the DNS server
+        get_krb5_rw_dns_server(creds, sub_vars['DNSDOMAIN'] + '.')
+        return creds
 
     except RuntimeError as e:
         os.unlink(ccachename)
@@ -223,7 +243,7 @@ def parse_dns_line(line, sub_vars):
     if line.startswith("SRV _ldap._tcp.pdc._msdcs.") and not samdb.am_pdc():
         # We keep this as compat to the dns_update_list of 4.0/4.1
         if opts.verbose:
-            print "Skipping PDC entry (%s) as we are not a PDC" % line
+            print("Skipping PDC entry (%s) as we are not a PDC" % line)
         return None
     subline = samba.substitute_var(line, sub_vars)
     if subline == '' or subline[0] == "#":
@@ -237,25 +257,27 @@ def hostname_match(h1, h2):
     h2 = str(h2)
     return h1.lower().rstrip('.') == h2.lower().rstrip('.')
 
-def check_one_dns_name(name, name_type, d=None):
-    resolv_conf = os.getenv('RESOLV_CONF')
-    if not resolv_conf:
-        resolv_conf = '/etc/resolv.conf'
-    resolver = dns.resolver.Resolver(filename=resolv_conf, configure=True)
+def get_resolver(d=None):
+    resolv_conf = os.getenv('RESOLV_CONF', default='/etc/resolv.conf')
+    resolver = DNSResolver(filename=resolv_conf, configure=True)
 
     if d is not None and d.nameservers != []:
         resolver.nameservers = d.nameservers
-    elif d is not None:
-        d.nameservers = resolver.nameservers
 
-    ans = resolver.query(name, name_type)
-    return ans
+    return resolver
+
+def check_one_dns_name(name, name_type, d=None):
+    resolver = get_resolver(d)
+    if d and not d.nameservers:
+        d.nameservers = resolver.nameservers
+    # dns.resolver.Answer
+    return resolver.resolve(name, name_type)
 
 def check_dns_name(d):
     """check that a DNS entry exists."""
     normalised_name = d.name.rstrip('.') + '.'
     if opts.verbose:
-        print "Looking for DNS entry %s as %s" % (d, normalised_name)
+        print("Looking for DNS entry %s as %s" % (d, normalised_name))
 
     if opts.use_file is not None:
         try:
@@ -263,12 +285,13 @@ def check_dns_name(d):
         except IOError:
             return False
 
-        for line in dns_file:
-            line = line.strip()
-            if line == '' or line[0] == "#":
-                continue
-            if line.lower() == str(d).lower():
-                return True
+        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
         return False
 
     try:
@@ -279,11 +302,11 @@ def check_dns_name(d):
         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:
-            print "The DNS entry %s, queried as %s does not exist" % (d, normalised_name)
+            print("The DNS entry %s, queried as %s does not exist" % (d, normalised_name))
         return False
     except dns.resolver.NoAnswer:
         if opts.verbose:
-            print "The DNS entry %s, queried as %s does not hold this record type" % (d, normalised_name)
+            print("The DNS entry %s, queried as %s does not hold this record type" % (d, normalised_name))
         return False
     except dns.exception.DNSException:
         raise Exception("Failure while trying to resolve %s as %s" % (d, normalised_name))
@@ -305,7 +328,7 @@ def check_dns_name(d):
     elif d.type == 'SRV':
         for rdata in ans:
             if opts.verbose:
-                print "Checking %s against %s" % (rdata, d)
+                print("Checking %s against %s" % (rdata, d))
             if hostname_match(rdata.target, d.dest):
                 if str(rdata.port) == str(d.port):
                     return True
@@ -314,7 +337,7 @@ def check_dns_name(d):
                     d.existing_weight = str(rdata.weight)
 
     if opts.verbose:
-        print "Lookup of %s succeeded, but we failed to find a matching DNS entry for %s" % (normalised_name, d)
+        print("Lookup of %s succeeded, but we failed to find a matching DNS entry for %s" % (normalised_name, d))
 
     return False
 
@@ -331,7 +354,7 @@ def get_subst_vars(samdb):
     vars['SITE']      = samdb.server_site_name()
     res = samdb.search(base=samdb.get_default_basedn(), scope=SCOPE_BASE, attrs=["objectGUID"])
     guid = samdb.schema_format_value("objectGUID", res[0]['objectGUID'][0])
-    vars['DOMAINGUID'] = guid
+    vars['DOMAINGUID'] = get_string(guid)
 
     vars['IF_DC'] = ""
     vars['IF_RWDC'] = "# "
@@ -398,22 +421,21 @@ def call_nsupdate(d, op="add"):
 
     assert(op in ["add", "delete"])
 
-    if opts.verbose:
-        print "Calling nsupdate for %s (%s)" % (d, op)
-
     if opts.use_file is not None:
+        if opts.verbose:
+            print("Use File instead of nsupdate for %s (%s)" % (d, op))
+
         try:
             rfile = open(opts.use_file, 'r+')
         except IOError:
             # Perhaps create it
-            rfile = open(opts.use_file, 'w+')
+            open(opts.use_file, 'w+').close()
             # Open it for reading again, in case someone else got to it first
             rfile = open(opts.use_file, 'r+')
         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')
-        rfile.seek(0)
         for line in rfile:
             if op == "delete":
                 l = parse_dns_line(line, {})
@@ -422,16 +444,33 @@ def call_nsupdate(d, op="add"):
             wfile.write(line)
         if op == "add":
             wfile.write(str(d)+"\n")
+        rfile.close()
+        wfile.close()
         os.rename(tmpfile, opts.use_file)
-        fcntl.lockf(rfile, fcntl.LOCK_UN)
         return
 
+    if opts.verbose:
+        print("Calling nsupdate for %s (%s)" % (d, op))
+
     normalised_name = d.name.rstrip('.') + '.'
 
     (tmp_fd, tmpfile) = tempfile.mkstemp()
     f = os.fdopen(tmp_fd, 'w')
-    if d.nameservers != []:
-        f.write('server %s\n' % d.nameservers[0])
+
+    resolver = get_resolver(d)
+
+    # 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)
+
     if d.type == "A":
         f.write("update %s %s %u A %s\n" % (op, normalised_name, default_ttl, d.ip))
     if d.type == "AAAA":
@@ -473,7 +512,7 @@ def call_nsupdate(d, op="add"):
             error_count = error_count + 1
             if opts.verbose:
                 print("Failed nsupdate: %d" % ret)
-    except Exception, estr:
+    except Exception as estr:
         if opts.fail_immediately:
             sys.exit(1)
         error_count = error_count + 1
@@ -492,11 +531,11 @@ def call_samba_tool(d, op="add", zone=None):
 
     if (sub_vars['DNSFOREST'] != sub_vars['DNSDOMAIN']) and \
        sub_vars['DNSFOREST'].endswith('.' + sub_vars['DNSDOMAIN']):
-        print "Refusing to use samba-tool when forest %s is under domain %s" \
-            % (sub_vars['DNSFOREST'], sub_vars['DNSDOMAIN'])
+        print("Refusing to use samba-tool when forest %s is under domain %s" \
+            % (sub_vars['DNSFOREST'], sub_vars['DNSDOMAIN']))
 
     if opts.verbose:
-        print "Calling samba-tool dns for %s (%s)" % (d, op)
+        print("Calling samba-tool dns for %s (%s)" % (d, op))
 
     normalised_name = d.name.rstrip('.') + '.'
     if zone is None:
@@ -511,7 +550,7 @@ def call_samba_tool(d, op="add", zone=None):
             zone = '_msdcs.' + sub_vars['DNSFOREST']
         else:
             if not normalised_name.endswith('.' + sub_vars['DNSDOMAIN'] + '.'):
-                print "Not Calling samba-tool dns for %s (%s), %s not in %s" % (d, op, normalised_name, sub_vars['DNSDOMAIN'] + '.')
+                print("Not Calling samba-tool dns for %s (%s), %s not in %s" % (d, op, normalised_name, sub_vars['DNSDOMAIN'] + '.'))
                 return False
             elif normalised_name.endswith('._msdcs.' + sub_vars['DNSFOREST'] + '.'):
                 zone = '_msdcs.' + sub_vars['DNSFOREST']
@@ -529,15 +568,10 @@ def call_samba_tool(d, op="add", zone=None):
         args = [rpc_server_ip, zone, short_name, "AAAA", d.ip]
     if d.type == "SRV":
         if op == "add" and d.existing_port is not None:
-            print "Not handling modify of exising SRV %s using samba-tool" % d
+            print("Not handling modify of existing SRV %s using samba-tool" % d)
             return False
-            op = "update"
-            args = [rpc_server_ip, zone, short_name, "SRV",
-                    "%s %s %s %s" % (d.existing_weight,
-                                     d.existing_port, "0", "100"),
-                    "%s %s %s %s" % (d.dest, d.port, "0", "100")]
-        else:
-            args = [rpc_server_ip, zone, short_name, "SRV", "%s %s %s %s" % (d.dest, d.port, "0", "100")]
+        args = [rpc_server_ip, zone, short_name, "SRV",
+                "%s %s %s %s" % (d.dest, d.port, "0", "100")]
     if d.type == "CNAME":
         if d.existing_cname_target is None:
             args = [rpc_server_ip, zone, short_name, "CNAME", d.dest]
@@ -549,25 +583,37 @@ def call_samba_tool(d, op="add", zone=None):
     if d.type == "NS":
         args = [rpc_server_ip, zone, short_name, "NS", d.dest]
 
+    if smb_conf and args:
+        args += ["--configfile=" + smb_conf]
+
     global error_count
     try:
         cmd = cmd_dns()
         if opts.verbose:
-            print "Calling samba-tool dns %s -k no -P %s" % (op, args)
-        ret = cmd._run("dns", op, "-k", "no", "-P", *args)
+            print(f'Calling samba-tool dns {op} --use-kerberos off -P {args}')
+        command, args = cmd._resolve("dns", op, "--use-kerberos", "off", "-P", *args)
+        ret = command._run(*args)
         if ret == -1:
             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))
-    except Exception, estr:
-        raise
+                print("Failed 'samba-tool dns' based update of %s" % (str(d)))
+    except Exception as estr:
         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))
+        raise
+
+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
 
 def rodc_dns_update(d, t, op):
     '''a single DNS update via the RODC netlogon call'''
@@ -576,7 +622,7 @@ def rodc_dns_update(d, t, op):
     assert(op in ["add", "delete"])
 
     if opts.verbose:
-        print "Calling netlogon RODC update for %s" % d
+        print("Calling netlogon RODC update for %s" % d)
 
     typemap = {
         netlogon.NlDnsLdapAtSite       : netlogon.NlDnsInfoTypeNone,
@@ -588,7 +634,7 @@ def rodc_dns_update(d, t, op):
         netlogon.NlDnsGenericGcAtSite  : netlogon.NlDnsDomainNameAlias
         }
 
-    w = winbind.winbind("irpc:winbind_server", lp)
+    w = cached_irpc_wb(lp)
     dns_names = netlogon.NL_DNS_NAME_INFO_ARRAY()
     dns_names.count = 1
     name = netlogon.NL_DNS_NAME_INFO()
@@ -603,7 +649,7 @@ def rodc_dns_update(d, t, op):
     else:
         name.dns_register = False
     dns_names.names = [ name ]
-    site_name = sub_vars['SITE'].decode('utf-8')
+    site_name = sub_vars['SITE']
 
     global error_count
 
@@ -612,10 +658,13 @@ def rodc_dns_update(d, t, op):
         if ret_names.names[0].status != 0:
             print("Failed to set DNS entry: %s (status %u)" % (d, ret_names.names[0].status))
             error_count = error_count + 1
-    except RuntimeError, reason:
+    except RuntimeError as reason:
         print("Error setting DNS entry of type %u: %s: %s" % (t, d, reason))
         error_count = error_count + 1
 
+    if opts.verbose:
+        print("Called netlogon RODC update for %s" % d)
+
     if error_count != 0 and opts.fail_immediately:
         sys.exit(1)
 
@@ -652,22 +701,30 @@ def call_rodc_update(d, op="add"):
 
 
 # get the list of DNS entries we should have
-if opts.update_list:
-    dns_update_list = opts.update_list
-else:
-    dns_update_list = lp.private_path('dns_update_list')
+dns_update_list = opts.update_list or lp.private_path('dns_update_list')
 
-if opts.update_cache:
-    dns_update_cache = opts.update_cache
-else:
-    dns_update_cache = lp.private_path('dns_update_cache')
+dns_update_cache = opts.update_cache or lp.private_path('dns_update_cache')
 
-# 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
+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
 
-file = open(dns_update_list, "r")
+try:
+    file = open(dns_update_list, "r")
+except OSError as e:
+    if opts.update_list:
+        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)
 
 if opts.nosubs:
     sub_vars = {}
@@ -691,7 +748,7 @@ try:
     cfile = open(dns_update_cache, 'r+')
 except IOError:
     # Perhaps create it
-    cfile = open(dns_update_cache, 'w+')
+    open(dns_update_cache, 'w+').close()
     # 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)
@@ -706,9 +763,17 @@ for line in cfile:
         cache_list.append(c)
         cache_set.add(str(c))
 
+cfile.close()
+
+site_specific_rec = []
+
 # read each line, and check that the DNS name exists
 for line in file:
     line = line.strip()
+
+    if '${SITE}' in line:
+        site_specific_rec.append(line)
+
     if line == '' or line[0] == "#":
         continue
     d = parse_dns_line(line, sub_vars)
@@ -722,6 +787,27 @@ for line in file:
         dns_list.append(d)
         dup_set.add(str(d))
 
+file.close()
+
+# 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))
+
 # 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:
@@ -749,15 +835,20 @@ for d in dns_list:
             break
     if not found:
         rebuild_cache = True
-    if opts.all_names:
+        if opts.verbose:
+            print("need cache add: %s" % d)
+    if dns_zone_scavenging:
         update_list.append(d)
         if opts.verbose:
-            print "force update: %s" % d
+            print("scavenging requires update: %s" % d)
+    elif opts.all_names:
+        update_list.append(d)
+        if opts.verbose:
+            print("force update: %s" % d)
     elif not check_dns_name(d):
         update_list.append(d)
         if opts.verbose:
-            print "need update: %s" % d
-
+            print("need update: %s" % d)
 
 for c in cache_list:
     found = False
@@ -768,24 +859,26 @@ for c in cache_list:
     if found:
         continue
     rebuild_cache = True
+    if opts.verbose:
+        print("need cache remove: %s" % c)
     if not opts.all_names and not check_dns_name(c):
         continue
     delete_list.append(c)
     if opts.verbose:
-        print "need delete: %s" % c
+        print("need delete: %s" % c)
 
 if len(delete_list) == 0 and len(update_list) == 0 and not rebuild_cache:
     if opts.verbose:
-        print "No DNS updates needed"
+        print("No DNS updates needed")
     sys.exit(0)
 else:
     if opts.verbose:
-        print "%d DNS updates and %d DNS deletes needed" % (len(update_list), len(delete_list))
+        print("%d DNS updates and %d DNS deletes needed" % (len(update_list), len(delete_list)))
 
 use_samba_tool = opts.use_samba_tool
 use_nsupdate = opts.use_nsupdate
 # get our krb5 creds
-if len(delete_list) != 0 or len(update_list) != 0 and not opts.nocreds:
+if (delete_list or update_list) and not opts.nocreds:
     try:
         creds = get_credentials(lp)
     except RuntimeError as e:
@@ -797,7 +890,7 @@ if len(delete_list) != 0 or len(update_list) != 0 and not opts.nocreds:
         if use_nsupdate:
             raise
 
-        print "Failed to get Kerberos credentials, falling back to samba-tool: %s" % e
+        print("Failed to get Kerberos credentials, falling back to samba-tool: %s" % e)
         use_samba_tool = True
 
 
@@ -805,63 +898,63 @@ if len(delete_list) != 0 or len(update_list) != 0 and not opts.nocreds:
 for d in delete_list:
     if d.rpc or (not use_nsupdate and use_samba_tool):
         if opts.verbose:
-            print "update (samba-tool): %s" % d
+            print("delete (samba-tool): %s" % d)
         call_samba_tool(d, op="delete", zone=d.zone)
 
     elif am_rodc:
         if d.name.lower() == domain.lower():
             if opts.verbose:
-                print "skip delete (rodc): %s" % d
+                print("skip delete (rodc): %s" % d)
             continue
-        if not d.type in [ 'A', 'AAAA' ]:
+        if d.type not in [ 'A', 'AAAA' ]:
             if opts.verbose:
-                print "delete (rodc): %s" % d
+                print("delete (rodc): %s" % d)
             call_rodc_update(d, op="delete")
         else:
             if opts.verbose:
-                print "delete (nsupdate): %s" % d
+                print("delete (nsupdate): %s" % d)
             call_nsupdate(d, op="delete")
     else:
         if opts.verbose:
-            print "delete (nsupdate): %s" % d
+            print("delete (nsupdate): %s" % d)
         call_nsupdate(d, op="delete")
 
 # ask nsupdate to add entries as needed
 for d in update_list:
     if d.rpc or (not use_nsupdate and use_samba_tool):
         if opts.verbose:
-            print "update (samba-tool): %s" % d
+            print("update (samba-tool): %s" % d)
         call_samba_tool(d, zone=d.zone)
 
     elif am_rodc:
         if d.name.lower() == domain.lower():
             if opts.verbose:
-                print "skip (rodc): %s" % d
+                print("skip (rodc): %s" % d)
             continue
-        if not d.type in [ 'A', 'AAAA' ]:
+        if d.type not in [ 'A', 'AAAA' ]:
             if opts.verbose:
-                print "update (rodc): %s" % d
+                print("update (rodc): %s" % d)
             call_rodc_update(d)
         else:
             if opts.verbose:
-                print "update (nsupdate): %s" % d
+                print("update (nsupdate): %s" % d)
             call_nsupdate(d)
     else:
         if opts.verbose:
-            print "update(nsupdate): %s" % d
+            print("update(nsupdate): %s" % d)
         call_nsupdate(d)
 
 if rebuild_cache:
-    print "Rebuilding cache at %s" % dns_update_cache
+    print("Rebuilding cache at %s" % dns_update_cache)
     (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:
         if opts.verbose:
-            print "Adding %s to %s" % (str(d), file_name)
+            print("Adding %s to %s" % (str(d), file_name))
         wfile.write(str(d)+"\n")
+    wfile.close()
     os.rename(tmpfile, dns_update_cache)
-fcntl.lockf(cfile, fcntl.LOCK_UN)
 
 # delete the ccache if we created it
 if ccachename is not None: