3 # Compute our KCC topology
5 # Copyright (C) Dave Craft 2011
6 # Copyright (C) Andrew Bartlett 2015
8 # Andrew Bartlett's alleged work performed by his underlings Douglas
9 # Bagnall and Garming Sam.
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 # ensure we get messages out immediately, so they get in the samba logs,
29 # and don't get swallowed by a timeout
30 os.environ['PYTHONUNBUFFERED'] = '1'
32 # forcing GMT avoids a problem in some timezones with kerberos. Both MIT
33 # heimdal can get mutual authentication errors due to the 24 second difference
34 # between UTC and GMT when using some zone files (eg. the PDT zone from
36 os.environ["TZ"] = "GMT"
38 # Find right directory when running from source tree
39 sys.path.insert(0, "bin/python")
44 from samba import getopt as options
46 from samba.kcc.graph_utils import verify_and_dot, list_verify_tests
47 from samba.kcc.graph_utils import GraphError
51 from samba.kcc.debug import logger, DEBUG, DEBUG_FN
52 from samba.kcc import KCC
55 def test_all_reps_from(lp, creds, unix_now, rng_seed=None):
56 # This implies readonly and attempt_live_connections
57 kcc = KCC(unix_now, readonly=True,
58 verify=opts.verify, debug=opts.debug,
59 dot_file_dir=opts.dot_file_dir)
60 kcc.load_samdb(opts.dburl, lp, creds)
61 dsas = kcc.list_dsas()
66 for site in kcc.site_table.values():
67 guid_to_dnstr.update((str(dsa.dsa_guid), dnstr)
68 for dnstr, dsa in site.dsa_table.items())
78 kcc = KCC(unix_now, readonly=True,
79 verify=opts.verify, debug=opts.debug,
80 dot_file_dir=opts.dot_file_dir)
81 kcc.run(opts.dburl, lp, creds, forced_local_dsa=dsa_dn,
82 forget_local_links=opts.forget_local_links,
83 forget_intersite_links=opts.forget_intersite_links,
84 attempt_live_connections=opts.attempt_live_connections)
86 current, needed = kcc.my_dsa.get_rep_tables()
88 for dsa in kcc.my_site.dsa_table.values():
91 kcc.translate_ntdsconn(dsa)
92 c, n = dsa.get_rep_tables()
96 for name, rep_table, rep_parts in (
97 ('needed', needed, needed_parts),
98 ('current', current, current_parts)):
99 for part, nc_rep in rep_table.items():
100 edges = rep_parts.setdefault(part, [])
101 for reps_from in nc_rep.rep_repsFrom:
102 source = guid_to_dnstr[str(reps_from.source_dsa_obj_guid)]
103 dest = guid_to_dnstr[str(nc_rep.rep_dsa_guid)]
104 edges.append((source, dest))
106 for site in kcc.site_table.values():
107 for dsa in site.dsa_table.values():
109 vertex_colours.append('#cc0000')
111 vertex_colours.append('#0000cc')
112 dot_vertices.append(dsa.dsa_dnstr)
113 if dsa.connect_table:
114 DEBUG_FN("DSA %s %s connections:\n%s" %
115 (dsa.dsa_dnstr, len(dsa.connect_table),
116 [x.from_dnstr for x in
117 dsa.connect_table.values()]))
118 for con in dsa.connect_table.values():
119 if con.is_rodc_topology():
120 colours.append('red')
122 colours.append('blue')
123 dot_edges.append((con.from_dnstr, dsa.dsa_dnstr))
125 verify_and_dot('all-dsa-connections', dot_edges, vertices=dot_vertices,
126 label="all dsa NTDSConnections", properties=(),
127 debug=DEBUG, verify=opts.verify,
128 dot_file_dir=opts.dot_file_dir,
129 directed=True, edge_colors=colours,
130 vertex_colors=vertex_colours)
132 for name, rep_parts in (('needed', needed_parts),
133 ('current', current_parts)):
134 for part, edges in rep_parts.items():
135 verify_and_dot('all-repsFrom_%s__%s' % (name, part), edges,
136 directed=True, label=part,
137 properties=(), debug=DEBUG, verify=opts.verify,
138 dot_file_dir=opts.dot_file_dir)
140 ##################################################
141 # samba_kcc entry point
142 ##################################################
145 parser = optparse.OptionParser("samba_kcc [options]")
146 sambaopts = options.SambaOptions(parser)
147 credopts = options.CredentialsOptions(parser)
149 parser.add_option_group(sambaopts)
150 parser.add_option_group(credopts)
151 parser.add_option_group(options.VersionOptions(parser))
153 parser.add_option("--readonly", default=False,
154 help="compute topology but do not update database",
157 parser.add_option("--debug",
161 parser.add_option("--verify",
162 help="verify that assorted invariants are kept",
165 parser.add_option("--list-verify-tests",
166 help=("list what verification actions are available "
167 "and do nothing else"),
170 parser.add_option("--dot-file-dir", default=None,
171 help="Write Graphviz .dot files to this directory")
173 parser.add_option("--seed",
174 help="random number seed",
177 parser.add_option("--importldif",
178 help="import topology ldif file",
179 type=str, metavar="<file>")
181 parser.add_option("--exportldif",
182 help="export topology ldif file",
183 type=str, metavar="<file>")
185 parser.add_option("-H", "--URL",
186 help="LDB URL for database or target server",
187 type=str, metavar="<URL>", dest="dburl")
189 parser.add_option("--tmpdb",
190 help="schemaless database file to create for ldif import",
191 type=str, metavar="<file>")
193 parser.add_option("--now",
194 help=("assume current time is this ('YYYYmmddHHMMSS[tz]',"
195 " default: system time)"),
196 type=str, metavar="<date>")
198 parser.add_option("--forced-local-dsa",
199 help="run calculations assuming the DSA is this DN",
200 type=str, metavar="<DSA>")
202 parser.add_option("--attempt-live-connections", default=False,
203 help="Attempt to connect to other DSAs to test links",
206 parser.add_option("--list-valid-dsas", default=False,
207 help=("Print a list of DSA dnstrs that could be"
208 " used in --forced-local-dsa"),
211 parser.add_option("--test-all-reps-from", default=False,
212 help="Create and verify a graph of reps-from for every DSA",
215 parser.add_option("--forget-local-links", default=False,
216 help="pretend not to know the existing local topology",
219 parser.add_option("--forget-intersite-links", default=False,
220 help="pretend not to know the existing intersite topology",
224 opts, args = parser.parse_args()
227 if opts.list_verify_tests:
232 logger.setLevel(logging.DEBUG)
234 logger.setLevel(logging.INFO)
236 logger.setLevel(logging.WARNING)
238 # initialize seed from optional input parameter
240 random.seed(opts.seed)
242 random.seed(0xACE5CA11)
245 for timeformat in ("%Y%m%d%H%M%S%Z", "%Y%m%d%H%M%S"):
247 now_tuple = time.strptime(opts.now, timeformat)
252 # else happens if break doesn't --> no match
253 print >> sys.stderr, "could not parse time '%s'" % opts.now
255 unix_now = int(time.mktime(now_tuple))
257 unix_now = int(time.time())
259 lp = sambaopts.get_loadparm()
260 creds = credopts.get_credentials(lp, fallback_machine=True)
262 if opts.dburl is None:
263 opts.dburl = lp.samdb_url()
265 if opts.test_all_reps_from:
267 rng_seed = opts.seed or 0xACE5CA11
268 test_all_reps_from(lp, creds, unix_now, rng_seed=rng_seed)
271 # Instantiate Knowledge Consistency Checker and perform run
272 kcc = KCC(unix_now, readonly=opts.readonly, verify=opts.verify,
273 debug=opts.debug, dot_file_dir=opts.dot_file_dir)
277 rc = kcc.export_ldif(opts.dburl, lp, creds, opts.exportldif)
281 if opts.tmpdb is None or opts.tmpdb.startswith('ldap'):
282 logger.error("Specify a target temp database file with --tmpdb option")
285 rc = kcc.import_ldif(opts.tmpdb, lp, creds, opts.importldif)
289 if opts.list_valid_dsas:
290 kcc.load_samdb(opts.dburl, lp, creds)
291 print '\n'.join(kcc.list_dsas())
295 rc = kcc.run(opts.dburl, lp, creds, opts.forced_local_dsa,
296 opts.forget_local_links, opts.forget_intersite_links,
297 attempt_live_connections=opts.attempt_live_connections)
300 except GraphError, e: