]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
samba_kcc: add an option to set assumed current time
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Wed, 4 Mar 2015 22:40:55 +0000 (11:40 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 28 May 2015 05:25:08 +0000 (07:25 +0200)
The KCC algorithm contains a timeouts in a couple of places, and we
need to be able to set the time for testing these.

This also means samba_kcc uses the same time in all places.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/kcc_utils.py
source4/scripting/bin/samba_kcc

index 3e0845ee03b05b97dd26ef244aa490c87935d150..599f8d91a75514234188a3a7fd1857a7b43c0fd4 100644 (file)
@@ -1423,13 +1423,14 @@ class Site(object):
     """An individual site object discovered thru the configuration
     naming context.  Contains all DSAs that exist within the site
     """
-    def __init__(self, site_dnstr):
+    def __init__(self, site_dnstr, unix_now):
         self.site_dnstr = site_dnstr
         self.site_guid = None
         self.site_options = 0
         self.site_topo_generator = None
         self.site_topo_failover = 0  # appears to be in minutes
         self.dsa_table = {}
+        self.unix_now = unix_now
 
     def load_site(self, samdb):
         """Loads the NTDS Site Settions options attribute for the site
@@ -1555,9 +1556,8 @@ class Site(object):
         D_sort = []
         d_dsa = None
 
-        unixnow = int(time.time())     # seconds since 1970
-        ntnow = unix2nttime(unixnow) # double word number of 100 nanosecond
-                                       # intervals since 1600s
+        ntnow = unix2nttime(self.unix_now) # double word number of 100 nanosecond
+                                           # intervals since 1600s
 
         for dsa in self.dsa_table.values():
             D_sort.append(dsa)
index 2adb9f628727f58126ff05134411166e6ae239ad..ab1cafea2083daee92aae0cd3eb4e50aa423f368 100755 (executable)
@@ -38,6 +38,7 @@ import optparse
 import logging
 import itertools
 import heapq
+import time
 
 from samba import (
     getopt as options,
@@ -154,7 +155,7 @@ class KCC(object):
         self.my_site_dnstr = "CN=%s,CN=Sites,%s" % (
                               self.samdb.server_site_name(),
                               self.samdb.get_config_basedn())
-        site = Site(self.my_site_dnstr)
+        site = Site(self.my_site_dnstr, unix_now)
         site.load_site(self.samdb)
 
         self.site_table[str(site.site_guid)] = site
@@ -178,7 +179,7 @@ class KCC(object):
         for msg in res:
             sitestr = str(msg.dn)
 
-            site = Site(sitestr)
+            site = Site(sitestr, unix_now)
             site.load_site(self.samdb)
 
             # already loaded
@@ -301,13 +302,12 @@ class KCC(object):
             if failed_link.failure_count > 0:
                 unix_first_time_failure = nttime2unix(failed_link.time_first_failure)
                 # TODO guard against future
-                current_time = int(time.time())
-                if unix_first_time_failure > current_time:
+                if unix_first_time_failure > unix_now:
                     logger.error("The last success time attribute for \
                                  repsFrom is in the future!")
 
                 # Perform calculation in seconds
-                if (current_time - unix_first_time_failure) > 60 * 60 * 2:
+                if (unix_now - unix_first_time_failure) > 60 * 60 * 2:
                     return True
 
         # TODO connections
@@ -2929,6 +2929,10 @@ parser.add_option("--tmpdb",
                   help="schemaless database file to create for ldif import",
                   type=str, metavar="<file>")
 
+parser.add_option("--now",
+                  help="assume current time is this ('YYYYmmddHHMMSS[tz]', default: system time)",
+                  type=str, metavar="<date>")
+
 logger = logging.getLogger("samba_kcc")
 logger.addHandler(logging.StreamHandler(sys.stdout))
 
@@ -2956,6 +2960,23 @@ else:
 if opts.dburl is None:
     opts.dburl = lp.samdb_url()
 
+if opts.now:
+    for timeformat in ("%Y%m%d%H%M%S%Z", "%Y%m%d%H%M%S"):
+        try:
+            now_tuple = time.strptime(opts.now, timeformat)
+            break
+        except ValueError:
+            pass
+    else:
+        # else happens if break doesn't --> no match
+        print >> sys.stderr, "could not parse time '%s'" % opts.now
+        sys.exit(1)
+
+    unix_now = int(time.mktime(now_tuple))
+else:
+    unix_now = int(time.time())
+
+
 # Instantiate Knowledge Consistency Checker and perform run
 kcc = KCC()