s4:samba_dnsupdate: cache the already registered records
authorStefan Metzmacher <metze@samba.org>
Mon, 28 Apr 2014 06:29:40 +0000 (08:29 +0200)
committerStefan Metzmacher <metze@samba.org>
Tue, 26 Aug 2014 07:13:06 +0000 (09:13 +0200)
This way we can delete records which are not used anymore.

E.g. if the ip address changed.

Bug: https://bugzilla.samba.org/show_bug.cgi?id=9831

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
selftest/target/Samba3.pm
source4/dsdb/dns/dns_update.c
source4/scripting/bin/samba_dnsupdate

index 8c15d3750f1ab76d304916b9d3f09fc105fea5d7..554410580fadfa0140d8659f360e905fec5df198 100755 (executable)
@@ -1259,7 +1259,7 @@ domadmins:X:$gid_domadmins:
        print DNS_UPDATE_LIST "AAAA $server. $server_ipv6\n";
        close(DNS_UPDATE_LIST);
 
-        if (system("$ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate --all-interfaces --use-file=$dns_host_file -s $conffile --update-list=$prefix/dns_update_list --no-substiutions --no-credentials") != 0) {
+        if (system("$ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate --all-interfaces --use-file=$dns_host_file -s $conffile --update-list=$prefix/dns_update_list --update-cache=$prefix/dns_update_cache --no-substiutions --no-credentials") != 0) {
                 die "Unable to update hostname into $dns_host_file";
         }
 
index 3e10447f0fc13145c24240e3eedee719c361485c..aa617c6aeca3ca0301c4314fb0c677ca911fb68d 100644 (file)
@@ -397,6 +397,7 @@ struct dnsupdate_RODC_state {
        struct irpc_message *msg;
        struct dnsupdate_RODC *r;
        char *tmp_path;
+       char *tmp_path2;
        int fd;
 };
 
@@ -406,6 +407,9 @@ static int dnsupdate_RODC_destructor(struct dnsupdate_RODC_state *st)
                close(st->fd);
        }
        unlink(st->tmp_path);
+       if (st->tmp_path2 != NULL) {
+               unlink(st->tmp_path2);
+       }
        return 0;
 }
 
@@ -483,6 +487,13 @@ static NTSTATUS dnsupdate_dnsupdate_RODC(struct irpc_message *msg,
 
        talloc_set_destructor(st, dnsupdate_RODC_destructor);
 
+       st->tmp_path2 = talloc_asprintf(st, "%s.cache", st->tmp_path);
+       if (!st->tmp_path2) {
+               talloc_free(st);
+               r->out.result = NT_STATUS_NO_MEMORY;
+               return NT_STATUS_OK;
+       }
+
        sid_dn = ldb_dn_new_fmt(st, s->samdb, "<SID=%s>", dom_sid_string(st, r->in.dom_sid));
        if (!sid_dn) {
                talloc_free(st);
@@ -575,6 +586,8 @@ static NTSTATUS dnsupdate_dnsupdate_RODC(struct irpc_message *msg,
                                dns_update_command,
                                "--update-list",
                                st->tmp_path,
+                               "--update-cache",
+                               st->tmp_path2,
                                NULL);
        NT_STATUS_HAVE_NO_MEMORY(req);
 
index 1b21216b53357cc4a4404090945fd262817d694f..9c7c5e232cb62ff329bfe868838f0600bbfb1009 100755 (executable)
@@ -63,6 +63,7 @@ parser.add_option("--all-names", action="store_true")
 parser.add_option("--all-interfaces", action="store_true")
 parser.add_option("--use-file", type="string", help="Use a file, rather than real DNS calls")
 parser.add_option("--update-list", type="string", help="Add DNS names from the given file")
+parser.add_option("--update-cache", type="string", help="Cache database of already registered records")
 parser.add_option("--fail-immediately", action='store_true', help="Exit on first failure")
 parser.add_option("--no-credentials", dest='nocreds', action='store_true', help="don't try and get credentials")
 parser.add_option("--no-substiutions", dest='nosubs', action='store_true', help="don't try and expands variables in file specified by --update-list")
@@ -278,12 +279,14 @@ def get_subst_vars(samdb):
     return vars
 
 
-def call_nsupdate(d):
+def call_nsupdate(d, op="add"):
     """call nsupdate for an entry."""
     global ccachename, nsupdate_cmd, krb5conf
 
+    assert(op in ["add", "delete"])
+
     if opts.verbose:
-        print "Calling nsupdate for %s" % d
+        print "Calling nsupdate for %s (%s)" % (d, op)
 
     if opts.use_file is not None:
         try:
@@ -299,8 +302,13 @@ def call_nsupdate(d):
         wfile = os.fdopen(tmp_fd, 'a')
         rfile.seek(0)
         for line in rfile:
+            if op == "delete":
+                l = parse_dns_line(line, {})
+                if str(l).lower() == str(d).lower():
+                    continue
             wfile.write(line)
-        wfile.write(str(d)+"\n")
+        if op == "add":
+            wfile.write(str(d)+"\n")
         os.rename(tmpfile, opts.use_file)
         fcntl.lockf(rfile, fcntl.LOCK_UN)
         return
@@ -312,18 +320,18 @@ def call_nsupdate(d):
     if getattr(d, 'nameservers', None):
         f.write('server %s\n' % d.nameservers[0])
     if d.type == "A":
-        f.write("update add %s %u A %s\n" % (normalised_name, default_ttl, d.ip))
+        f.write("update %s %s %u A %s\n" % (op, normalised_name, default_ttl, d.ip))
     if d.type == "AAAA":
-        f.write("update add %s %u AAAA %s\n" % (normalised_name, default_ttl, d.ip))
+        f.write("update %s %s %u AAAA %s\n" % (op, normalised_name, default_ttl, d.ip))
     if d.type == "SRV":
-        if d.existing_port is not None:
+        if op == "add" and d.existing_port is not None:
             f.write("update delete %s SRV 0 %s %s %s\n" % (normalised_name, d.existing_weight,
                                                            d.existing_port, d.dest))
-        f.write("update add %s %u SRV 0 100 %s %s\n" % (normalised_name, default_ttl, d.port, d.dest))
+        f.write("update %s %s %u SRV 0 100 %s %s\n" % (op, normalised_name, default_ttl, d.port, d.dest))
     if d.type == "CNAME":
-        f.write("update add %s %u CNAME %s\n" % (normalised_name, default_ttl, d.dest))
+        f.write("update %s %s %u CNAME %s\n" % (op, normalised_name, default_ttl, d.dest))
     if d.type == "NS":
-        f.write("update add %s %u NS %s\n" % (normalised_name, default_ttl, d.dest))
+        f.write("update %s %s %u NS %s\n" % (op, normalised_name, default_ttl, d.dest))
     if opts.verbose:
         f.write("show\n")
     f.write("send\n")
@@ -359,10 +367,12 @@ def call_nsupdate(d):
 
 
 
-def rodc_dns_update(d, t):
+def rodc_dns_update(d, t, op):
     '''a single DNS update via the RODC netlogon call'''
     global sub_vars
 
+    assert(op in ["add", "delete"])
+
     if opts.verbose:
         print "Calling netlogon RODC update for %s" % d
 
@@ -386,7 +396,10 @@ def rodc_dns_update(d, t):
     name.weight   = 0
     if d.port is not None:
         name.port = int(d.port)
-    name.dns_register = True
+    if op == "add":
+        name.dns_register = True
+    else:
+        name.dns_register = False
     dns_names.names = [ name ]
     site_name = sub_vars['SITE'].decode('utf-8')
 
@@ -405,10 +418,12 @@ def rodc_dns_update(d, t):
         sys.exit(1)
 
 
-def call_rodc_update(d):
+def call_rodc_update(d, op="add"):
     '''RODCs need to use the netlogon API for nsupdate'''
     global lp, sub_vars
 
+    assert(op in ["add", "delete"])
+
     # we expect failure for 3268 if we aren't a GC
     if d.port is not None and int(d.port) == 3268:
         return
@@ -428,7 +443,7 @@ def call_rodc_update(d):
         subname = samba.substitute_var(map[t], sub_vars)
         if subname.lower() == d.name.lower():
             # found a match - do the update
-            rodc_dns_update(d, t)
+            rodc_dns_update(d, t, op)
             return
     if opts.verbose:
         print("Unable to map to netlogon DNS update: %s" % d)
@@ -440,6 +455,11 @@ if opts.update_list:
 else:
     dns_update_list = 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')
+
 # 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')
@@ -458,8 +478,31 @@ else:
 # build up a list of update commands to pass to nsupdate
 update_list = []
 dns_list = []
+cache_list = []
+delete_list = []
 
 dup_set = set()
+cache_set = set()
+
+rebuild_cache = False
+try:
+    cfile = open(dns_update_cache, 'r+')
+except IOError:
+    # Perhaps create it
+    cfile = open(dns_update_cache, 'w+')
+    # 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))
 
 # read each line, and check that the DNS name exists
 for line in file:
@@ -497,17 +540,50 @@ for d in dns_list:
 
 # now check if the entries already exist on the DNS server
 for d in dns_list:
+    found = False
+    for c in cache_list:
+        if str(c).lower() == str(d).lower():
+            found = True
+            break
+    if not found:
+        rebuild_cache = True
     if opts.all_names or not check_dns_name(d):
         update_list.append(d)
 
-if len(update_list) == 0:
+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
+    if not opts.all_names and not check_dns_name(c):
+        continue
+    delete_list.append(c)
+
+if len(delete_list) == 0 and len(update_list) == 0 and not rebuild_cache:
     if opts.verbose:
         print "No DNS updates needed"
     sys.exit(0)
 
 # get our krb5 creds
-if not opts.nocreds:
-    get_credentials(lp)
+if len(delete_list) != 0 or len(update_list) != 0:
+    if not opts.nocreds:
+        get_credentials(lp)
+
+# ask nsupdate to delete entries as needed
+for d in delete_list:
+    if am_rodc:
+        if d.name.lower() == domain.lower():
+            continue
+        if not d.type in [ 'A', 'AAAA' ]:
+            call_rodc_update(d, op="delete")
+        else:
+            call_nsupdate(d, op="delete")
+    else:
+        call_nsupdate(d, op="delete")
 
 # ask nsupdate to add entries as needed
 for d in update_list:
@@ -521,6 +597,15 @@ for d in update_list:
     else:
         call_nsupdate(d)
 
+if rebuild_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:
+        wfile.write(str(d)+"\n")
+    os.rename(tmpfile, dns_update_cache)
+fcntl.lockf(cfile, fcntl.LOCK_UN)
+
 # delete the ccache if we created it
 if ccachename is not None:
     os.unlink(ccachename)