From 9e78375d2c822de021c8083eabfa2aee7455d515 Mon Sep 17 00:00:00 2001 From: Douglas Bagnall Date: Thu, 5 Mar 2015 11:40:55 +1300 Subject: [PATCH] samba_kcc: add an option to set assumed current time 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 Reviewed-by: Garming Sam Reviewed-by: Andrew Bartlett --- python/samba/kcc_utils.py | 8 ++++---- source4/scripting/bin/samba_kcc | 31 ++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/python/samba/kcc_utils.py b/python/samba/kcc_utils.py index 3e0845ee03b..599f8d91a75 100644 --- a/python/samba/kcc_utils.py +++ b/python/samba/kcc_utils.py @@ -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) diff --git a/source4/scripting/bin/samba_kcc b/source4/scripting/bin/samba_kcc index 2adb9f62872..ab1cafea208 100755 --- a/source4/scripting/bin/samba_kcc +++ b/source4/scripting/bin/samba_kcc @@ -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="") +parser.add_option("--now", + help="assume current time is this ('YYYYmmddHHMMSS[tz]', default: system time)", + type=str, metavar="") + 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()