samba_gpoupdate: Rename the command to samba-gpupdate
[nivanova/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 import logging
50 from samba.kcc.debug import logger, DEBUG, DEBUG_FN
51 from samba.kcc import KCC
52
53 # If DEFAULT_RNG_SEED is None, /dev/urandom or system time is used.
54 DEFAULT_RNG_SEED = None
55
56
57 def test_all_reps_from(kcc, dburl, lp, creds, unix_now, rng_seed=None,
58                        ldif_file=None):
59     """Run the KCC from all DSAs in read-only mode
60
61     The behaviour depends on the global opts variable which contains
62     command line variables. Usually you will want to run it with
63     opt.dot_file_dir set (via --dot-file-dir) to see the graphs that
64     would be created from each DC.
65
66     :param lp: a loadparm object.
67     :param creds: a Credentials object.
68     :param unix_now: the unix epoch time as an integer
69     :param rng_seed: a seed for the random number generator
70     :return None:
71     """
72     # This implies readonly and attempt_live_connections
73     dsas = kcc.list_dsas()
74     samdb = kcc.samdb
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 is not None:
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         if ldif_file is not None:
95             try:
96                 # The dburl in this case is a temporary database.
97                 # Its non-existence is ensured at the script startup.
98                 # If it exists, it is from a previous iteration of
99                 # this loop -- unless we're in an unfortunate race.
100                 # Because this database is temporary, it lacks some
101                 # detail and needs to be re-created anew to set the
102                 # local dsa.
103                 os.unlink(dburl)
104             except OSError:
105                 pass
106
107             kcc.import_ldif(dburl, lp, ldif_file, dsa_dn)
108         else:
109             kcc.samdb = samdb
110         kcc.run(dburl, lp, creds, forced_local_dsa=dsa_dn,
111                 forget_local_links=opts.forget_local_links,
112                 forget_intersite_links=opts.forget_intersite_links,
113                 attempt_live_connections=opts.attempt_live_connections)
114
115         current, needed = kcc.my_dsa.get_rep_tables()
116
117         for dsa in kcc.my_site.dsa_table.values():
118             if dsa is kcc.my_dsa:
119                 continue
120             kcc.translate_ntdsconn(dsa)
121             c, n = dsa.get_rep_tables()
122             current.update(c)
123             needed.update(n)
124
125         for name, rep_table, rep_parts in (
126                 ('needed', needed, needed_parts),
127                 ('current', current, current_parts)):
128             for part, nc_rep in rep_table.items():
129                 edges = rep_parts.setdefault(part, [])
130                 for reps_from in nc_rep.rep_repsFrom:
131                     source = guid_to_dnstr[str(reps_from.source_dsa_obj_guid)]
132                     dest = guid_to_dnstr[str(nc_rep.rep_dsa_guid)]
133                     edges.append((source, dest))
134
135         for site in kcc.site_table.values():
136             for dsa in site.dsa_table.values():
137                 if dsa.is_ro():
138                     vertex_colours.append('#cc0000')
139                 else:
140                     vertex_colours.append('#0000cc')
141                 dot_vertices.append(dsa.dsa_dnstr)
142                 if dsa.connect_table:
143                     DEBUG_FN("DSA %s %s connections:\n%s" %
144                              (dsa.dsa_dnstr, len(dsa.connect_table),
145                               [x.from_dnstr for x in
146                                dsa.connect_table.values()]))
147                 for con in dsa.connect_table.values():
148                     if con.is_rodc_topology():
149                         colours.append('red')
150                     else:
151                         colours.append('blue')
152                     dot_edges.append((con.from_dnstr, dsa.dsa_dnstr))
153
154     verify_and_dot('all-dsa-connections', dot_edges, vertices=dot_vertices,
155                    label="all dsa NTDSConnections", properties=(),
156                    debug=DEBUG, verify=opts.verify,
157                    dot_file_dir=opts.dot_file_dir,
158                    directed=True, edge_colors=colours,
159                    vertex_colors=vertex_colours)
160
161     for name, rep_parts in (('needed', needed_parts),
162                             ('current', current_parts)):
163         for part, edges in rep_parts.items():
164             verify_and_dot('all-repsFrom_%s__%s' % (name, part), edges,
165                            directed=True, label=part,
166                            properties=(), debug=DEBUG, verify=opts.verify,
167                            dot_file_dir=opts.dot_file_dir)
168
169 ##################################################
170 # samba_kcc entry point
171 ##################################################
172
173
174 parser = optparse.OptionParser("samba_kcc [options]")
175 sambaopts = options.SambaOptions(parser)
176 credopts = options.CredentialsOptions(parser)
177
178 parser.add_option_group(sambaopts)
179 parser.add_option_group(credopts)
180 parser.add_option_group(options.VersionOptions(parser))
181
182 parser.add_option("--readonly", default=False,
183                   help="compute topology but do not update database",
184                   action="store_true")
185
186 parser.add_option("--debug",
187                   help="debug output",
188                   action="store_true")
189
190 parser.add_option("--verify",
191                   help="verify that assorted invariants are kept",
192                   action="store_true")
193
194 parser.add_option("--list-verify-tests",
195                   help=("list what verification actions are available "
196                         "and do nothing else"),
197                   action="store_true")
198
199 parser.add_option("--dot-file-dir", default=None,
200                   help="Write Graphviz .dot files to this directory")
201
202 parser.add_option("--seed",
203                   help="random number seed",
204                   type=int, default=DEFAULT_RNG_SEED)
205
206 parser.add_option("--importldif",
207                   help="import topology ldif file",
208                   type=str, metavar="<file>")
209
210 parser.add_option("--exportldif",
211                   help="export topology ldif file",
212                   type=str, metavar="<file>")
213
214 parser.add_option("-H", "--URL",
215                   help="LDB URL for database or target server",
216                   type=str, metavar="<URL>", dest="dburl")
217
218 parser.add_option("--tmpdb",
219                   help="schemaless database file to create for ldif import",
220                   type=str, metavar="<file>")
221
222 parser.add_option("--now",
223                   help=("assume current time is this ('YYYYmmddHHMMSS[tz]',"
224                         " default: system time)"),
225                   type=str, metavar="<date>")
226
227 parser.add_option("--forced-local-dsa",
228                   help="run calculations assuming the DSA is this DN",
229                   type=str, metavar="<DSA>")
230
231 parser.add_option("--attempt-live-connections", default=False,
232                   help="Attempt to connect to other DSAs to test links",
233                   action="store_true")
234
235 parser.add_option("--list-valid-dsas", default=False,
236                   help=("Print a list of DSA dnstrs that could be"
237                         " used in --forced-local-dsa"),
238                   action="store_true")
239
240 parser.add_option("--test-all-reps-from", default=False,
241                   help="Create and verify a graph of reps-from for every DSA",
242                   action="store_true")
243
244 parser.add_option("--forget-local-links", default=False,
245                   help="pretend not to know the existing local topology",
246                   action="store_true")
247
248 parser.add_option("--forget-intersite-links", default=False,
249                   help="pretend not to know the existing intersite topology",
250                   action="store_true")
251
252
253 opts, args = parser.parse_args()
254
255
256 if opts.list_verify_tests:
257     list_verify_tests()
258     sys.exit(0)
259
260 if opts.test_all_reps_from:
261     opts.readonly = True
262
263 if opts.debug:
264     logger.setLevel(logging.DEBUG)
265 elif opts.readonly:
266     logger.setLevel(logging.INFO)
267 else:
268     logger.setLevel(logging.WARNING)
269
270 random.seed(opts.seed)
271
272 if opts.now:
273     for timeformat in ("%Y%m%d%H%M%S%Z", "%Y%m%d%H%M%S"):
274         try:
275             now_tuple = time.strptime(opts.now, timeformat)
276             break
277         except ValueError:
278             pass
279     else:
280         # else happens if break doesn't --> no match
281         print >> sys.stderr, "could not parse time '%s'" % opts.now
282         sys.exit(1)
283     unix_now = int(time.mktime(now_tuple))
284 else:
285     unix_now = int(time.time())
286
287 lp = sambaopts.get_loadparm()
288 creds = credopts.get_credentials(lp, fallback_machine=True)
289
290 if opts.dburl is None:
291     if opts.importldif:
292         opts.dburl = opts.tmpdb
293     else:
294         opts.dburl = lp.samdb_url()
295 elif opts.importldif:
296     logger.error("Don't use -H/--URL with --importldif, use --tmpdb instead")
297     sys.exit(1)
298
299 # Instantiate Knowledge Consistency Checker and perform run
300 kcc = KCC(unix_now, readonly=opts.readonly, verify=opts.verify,
301           debug=opts.debug, dot_file_dir=opts.dot_file_dir)
302
303 if opts.exportldif:
304     rc = kcc.export_ldif(opts.dburl, lp, creds, opts.exportldif)
305     sys.exit(rc)
306
307 if opts.importldif:
308     if opts.tmpdb is None or opts.tmpdb.startswith('ldap'):
309         logger.error("Specify a target temp database file with --tmpdb option")
310         sys.exit(1)
311     if os.path.exists(opts.tmpdb):
312         logger.error("The temp database file (%s) specified with --tmpdb "
313                      "already exists. We refuse to clobber it." % opts.tmpdb)
314         sys.exit(1)
315
316     rc = kcc.import_ldif(opts.tmpdb, lp, opts.importldif,
317                          forced_local_dsa=opts.forced_local_dsa)
318     if rc != 0:
319         sys.exit(rc)
320
321
322 kcc.load_samdb(opts.dburl, lp, creds, force=False)
323
324 if opts.test_all_reps_from:
325     test_all_reps_from(kcc, opts.dburl, lp, creds, unix_now,
326                        rng_seed=opts.seed,
327                        ldif_file=opts.importldif)
328     sys.exit()
329
330 if opts.list_valid_dsas:
331     print '\n'.join(kcc.list_dsas())
332     sys.exit()
333
334 try:
335     rc = kcc.run(opts.dburl, lp, creds, opts.forced_local_dsa,
336                  opts.forget_local_links, opts.forget_intersite_links,
337                  attempt_live_connections=opts.attempt_live_connections)
338     sys.exit(rc)
339
340 except GraphError as e:
341     print e
342     sys.exit(1)