KCC: docstring for test_all_reps_from()
[bbaumbach/samba-autobuild/.git] / source4 / scripting / bin / samba_kcc
1 #!/usr/bin/env python
2 #
3 # Compute our KCC topology
4 #
5 # Copyright (C) Dave Craft 2011
6 # Copyright (C) Andrew Bartlett 2015
7 #
8 # Andrew Bartlett's alleged work performed by his underlings Douglas
9 # Bagnall and Garming Sam.
10 #
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.
15 #
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.
20 #
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/>.
23
24 import os
25 import sys
26 import random
27
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'
31
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
35 # the US)
36 os.environ["TZ"] = "GMT"
37
38 # Find right directory when running from source tree
39 sys.path.insert(0, "bin/python")
40
41 import optparse
42 import time
43
44 from samba import getopt as options
45
46 from samba.kcc.graph_utils import verify_and_dot, list_verify_tests
47 from samba.kcc.graph_utils import GraphError
48
49
50 import logging
51 from samba.kcc.debug import logger, DEBUG, DEBUG_FN
52 from samba.kcc import KCC
53
54
55 def test_all_reps_from(lp, creds, unix_now, rng_seed=None):
56     """Run the KCC from all DSAs in read-only mode
57
58     The behaviour depends on the global opts variable which contains
59     command line variables. Usually you will want to run it with
60     opt.dot_file_dir set (via --dot-file-dir) to see the graphs that
61     would be created from each DC.
62
63     :param lp: a loadparm object.
64     :param creds: a Credentials object.
65     :param unix_now: the unix epoch time as an integer
66     :param rng_seed: a seed for the random number generator
67     :return None:
68     """
69     # This implies readonly and attempt_live_connections
70     kcc = KCC(unix_now, readonly=True,
71               verify=opts.verify, debug=opts.debug,
72               dot_file_dir=opts.dot_file_dir)
73     kcc.load_samdb(opts.dburl, lp, creds)
74     dsas = kcc.list_dsas()
75     needed_parts = {}
76     current_parts = {}
77
78     guid_to_dnstr = {}
79     for site in kcc.site_table.values():
80         guid_to_dnstr.update((str(dsa.dsa_guid), dnstr)
81                              for dnstr, dsa in site.dsa_table.items())
82
83     dot_edges = []
84     dot_vertices = []
85     colours = []
86     vertex_colours = []
87
88     for dsa_dn in dsas:
89         if rng_seed:
90             random.seed(rng_seed)
91         kcc = KCC(unix_now, readonly=True,
92                   verify=opts.verify, debug=opts.debug,
93                   dot_file_dir=opts.dot_file_dir)
94         kcc.run(opts.dburl, lp, creds, forced_local_dsa=dsa_dn,
95                 forget_local_links=opts.forget_local_links,
96                 forget_intersite_links=opts.forget_intersite_links,
97                 attempt_live_connections=opts.attempt_live_connections)
98
99         current, needed = kcc.my_dsa.get_rep_tables()
100
101         for dsa in kcc.my_site.dsa_table.values():
102             if dsa is kcc.my_dsa:
103                 continue
104             kcc.translate_ntdsconn(dsa)
105             c, n = dsa.get_rep_tables()
106             current.update(c)
107             needed.update(n)
108
109         for name, rep_table, rep_parts in (
110                 ('needed', needed, needed_parts),
111                 ('current', current, current_parts)):
112             for part, nc_rep in rep_table.items():
113                 edges = rep_parts.setdefault(part, [])
114                 for reps_from in nc_rep.rep_repsFrom:
115                     source = guid_to_dnstr[str(reps_from.source_dsa_obj_guid)]
116                     dest = guid_to_dnstr[str(nc_rep.rep_dsa_guid)]
117                     edges.append((source, dest))
118
119         for site in kcc.site_table.values():
120             for dsa in site.dsa_table.values():
121                 if dsa.is_ro():
122                     vertex_colours.append('#cc0000')
123                 else:
124                     vertex_colours.append('#0000cc')
125                 dot_vertices.append(dsa.dsa_dnstr)
126                 if dsa.connect_table:
127                     DEBUG_FN("DSA %s %s connections:\n%s" %
128                              (dsa.dsa_dnstr, len(dsa.connect_table),
129                               [x.from_dnstr for x in
130                                dsa.connect_table.values()]))
131                 for con in dsa.connect_table.values():
132                     if con.is_rodc_topology():
133                         colours.append('red')
134                     else:
135                         colours.append('blue')
136                     dot_edges.append((con.from_dnstr, dsa.dsa_dnstr))
137
138     verify_and_dot('all-dsa-connections', dot_edges, vertices=dot_vertices,
139                    label="all dsa NTDSConnections", properties=(),
140                    debug=DEBUG, verify=opts.verify,
141                    dot_file_dir=opts.dot_file_dir,
142                    directed=True, edge_colors=colours,
143                    vertex_colors=vertex_colours)
144
145     for name, rep_parts in (('needed', needed_parts),
146                             ('current', current_parts)):
147         for part, edges in rep_parts.items():
148             verify_and_dot('all-repsFrom_%s__%s' % (name, part), edges,
149                            directed=True, label=part,
150                            properties=(), debug=DEBUG, verify=opts.verify,
151                            dot_file_dir=opts.dot_file_dir)
152
153 ##################################################
154 # samba_kcc entry point
155 ##################################################
156
157
158 parser = optparse.OptionParser("samba_kcc [options]")
159 sambaopts = options.SambaOptions(parser)
160 credopts = options.CredentialsOptions(parser)
161
162 parser.add_option_group(sambaopts)
163 parser.add_option_group(credopts)
164 parser.add_option_group(options.VersionOptions(parser))
165
166 parser.add_option("--readonly", default=False,
167                   help="compute topology but do not update database",
168                   action="store_true")
169
170 parser.add_option("--debug",
171                   help="debug output",
172                   action="store_true")
173
174 parser.add_option("--verify",
175                   help="verify that assorted invariants are kept",
176                   action="store_true")
177
178 parser.add_option("--list-verify-tests",
179                   help=("list what verification actions are available "
180                         "and do nothing else"),
181                   action="store_true")
182
183 parser.add_option("--dot-file-dir", default=None,
184                   help="Write Graphviz .dot files to this directory")
185
186 parser.add_option("--seed",
187                   help="random number seed",
188                   type=int)
189
190 parser.add_option("--importldif",
191                   help="import topology ldif file",
192                   type=str, metavar="<file>")
193
194 parser.add_option("--exportldif",
195                   help="export topology ldif file",
196                   type=str, metavar="<file>")
197
198 parser.add_option("-H", "--URL",
199                   help="LDB URL for database or target server",
200                   type=str, metavar="<URL>", dest="dburl")
201
202 parser.add_option("--tmpdb",
203                   help="schemaless database file to create for ldif import",
204                   type=str, metavar="<file>")
205
206 parser.add_option("--now",
207                   help=("assume current time is this ('YYYYmmddHHMMSS[tz]',"
208                         " default: system time)"),
209                   type=str, metavar="<date>")
210
211 parser.add_option("--forced-local-dsa",
212                   help="run calculations assuming the DSA is this DN",
213                   type=str, metavar="<DSA>")
214
215 parser.add_option("--attempt-live-connections", default=False,
216                   help="Attempt to connect to other DSAs to test links",
217                   action="store_true")
218
219 parser.add_option("--list-valid-dsas", default=False,
220                   help=("Print a list of DSA dnstrs that could be"
221                         " used in --forced-local-dsa"),
222                   action="store_true")
223
224 parser.add_option("--test-all-reps-from", default=False,
225                   help="Create and verify a graph of reps-from for every DSA",
226                   action="store_true")
227
228 parser.add_option("--forget-local-links", default=False,
229                   help="pretend not to know the existing local topology",
230                   action="store_true")
231
232 parser.add_option("--forget-intersite-links", default=False,
233                   help="pretend not to know the existing intersite topology",
234                   action="store_true")
235
236
237 opts, args = parser.parse_args()
238
239
240 if opts.list_verify_tests:
241     list_verify_tests()
242     sys.exit(0)
243
244 if opts.debug:
245     logger.setLevel(logging.DEBUG)
246 elif opts.readonly:
247     logger.setLevel(logging.INFO)
248 else:
249     logger.setLevel(logging.WARNING)
250
251 # initialize seed from optional input parameter
252 if opts.seed:
253     random.seed(opts.seed)
254 else:
255     random.seed(0xACE5CA11)
256
257 if opts.now:
258     for timeformat in ("%Y%m%d%H%M%S%Z", "%Y%m%d%H%M%S"):
259         try:
260             now_tuple = time.strptime(opts.now, timeformat)
261             break
262         except ValueError:
263             pass
264     else:
265         # else happens if break doesn't --> no match
266         print >> sys.stderr, "could not parse time '%s'" % opts.now
267         sys.exit(1)
268     unix_now = int(time.mktime(now_tuple))
269 else:
270     unix_now = int(time.time())
271
272 lp = sambaopts.get_loadparm()
273 creds = credopts.get_credentials(lp, fallback_machine=True)
274
275 if opts.dburl is None:
276     opts.dburl = lp.samdb_url()
277
278 if opts.test_all_reps_from:
279     opts.readonly = True
280     rng_seed = opts.seed or 0xACE5CA11
281     test_all_reps_from(lp, creds, unix_now, rng_seed=rng_seed)
282     sys.exit()
283
284 # Instantiate Knowledge Consistency Checker and perform run
285 kcc = KCC(unix_now, readonly=opts.readonly, verify=opts.verify,
286           debug=opts.debug, dot_file_dir=opts.dot_file_dir)
287
288
289 if opts.exportldif:
290     rc = kcc.export_ldif(opts.dburl, lp, creds, opts.exportldif)
291     sys.exit(rc)
292
293 if opts.importldif:
294     if opts.tmpdb is None or opts.tmpdb.startswith('ldap'):
295         logger.error("Specify a target temp database file with --tmpdb option")
296         sys.exit(1)
297
298     rc = kcc.import_ldif(opts.tmpdb, lp, creds, opts.importldif,
299                          forced_local_dsa=opts.forced_local_dsa)
300     if rc != 0:
301         sys.exit(rc)
302
303 if opts.list_valid_dsas:
304     kcc.load_samdb(opts.dburl, lp, creds)
305     print '\n'.join(kcc.list_dsas())
306     sys.exit()
307
308 try:
309     rc = kcc.run(opts.dburl, lp, creds, opts.forced_local_dsa,
310                  opts.forget_local_links, opts.forget_intersite_links,
311                  attempt_live_connections=opts.attempt_live_connections)
312     sys.exit(rc)
313
314 except GraphError, e:
315     print e
316     sys.exit(1)