netcmd/drs: add cmd_drs_uptodateness with json support
[samba.git] / python / samba / netcmd / drs.py
1 # implement samba_tool drs commands
2 #
3 # Copyright Andrew Tridgell 2010
4 # Copyright Andrew Bartlett 2017
5 #
6 # based on C implementation by Kamen Mazdrashki <kamen.mazdrashki@postpath.com>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 from __future__ import print_function
22
23 import samba.getopt as options
24 import ldb
25 import logging
26 from . import common
27 import json
28
29 from samba.auth import system_session
30 from samba.netcmd import (
31     Command,
32     CommandError,
33     Option,
34     SuperCommand,
35 )
36 from samba.samdb import SamDB
37 from samba import drs_utils, nttime2string, dsdb
38 from samba.dcerpc import drsuapi, misc
39 from samba.join import join_clone
40 from samba.ndr import ndr_unpack
41 from samba.dcerpc import drsblobs
42 from samba import colour
43
44 from samba.uptodateness import (
45     get_partition_maps,
46     get_utdv_edges,
47     get_utdv_distances,
48     get_utdv_summary,
49     get_kcc_and_dsas,
50 )
51
52
53 def drsuapi_connect(ctx):
54     '''make a DRSUAPI connection to the server'''
55     try:
56         (ctx.drsuapi, ctx.drsuapi_handle, ctx.bind_supported_extensions) = drs_utils.drsuapi_connect(ctx.server, ctx.lp, ctx.creds)
57     except Exception as e:
58         raise CommandError("DRS connection to %s failed" % ctx.server, e)
59
60
61 def samdb_connect(ctx):
62     '''make a ldap connection to the server'''
63     try:
64         ctx.samdb = SamDB(url="ldap://%s" % ctx.server,
65                           session_info=system_session(),
66                           credentials=ctx.creds, lp=ctx.lp)
67     except Exception as e:
68         raise CommandError("LDAP connection to %s failed" % ctx.server, e)
69
70
71 def drs_errmsg(werr):
72     '''return "was successful" or an error string'''
73     (ecode, estring) = werr
74     if ecode == 0:
75         return "was successful"
76     return "failed, result %u (%s)" % (ecode, estring)
77
78
79 def attr_default(msg, attrname, default):
80     '''get an attribute from a ldap msg with a default'''
81     if attrname in msg:
82         return msg[attrname][0]
83     return default
84
85
86 def drs_parse_ntds_dn(ntds_dn):
87     '''parse a NTDS DN returning a site and server'''
88     a = ntds_dn.split(',')
89     if a[0] != "CN=NTDS Settings" or a[2] != "CN=Servers" or a[4] != 'CN=Sites':
90         raise RuntimeError("bad NTDS DN %s" % ntds_dn)
91     server = a[1].split('=')[1]
92     site   = a[3].split('=')[1]
93     return (site, server)
94
95
96 DEFAULT_SHOWREPL_FORMAT = 'classic'
97
98
99 class cmd_drs_showrepl(Command):
100     """Show replication status."""
101
102     synopsis = "%prog [<DC>] [options]"
103
104     takes_optiongroups = {
105         "sambaopts": options.SambaOptions,
106         "versionopts": options.VersionOptions,
107         "credopts": options.CredentialsOptions,
108     }
109
110     takes_options = [
111         Option("--json", help="replication details in JSON format",
112                dest='format', action='store_const', const='json'),
113         Option("--summary", help=("summarize overall DRS health as seen "
114                                   "from this server"),
115                dest='format', action='store_const', const='summary'),
116         Option("--pull-summary", help=("Have we successfully replicated "
117                                        "from all relevent servers?"),
118                dest='format', action='store_const', const='pull_summary'),
119         Option("--notify-summary", action='store_const',
120                const='notify_summary', dest='format',
121                help=("Have we successfully notified all relevent servers of "
122                      "local changes, and did they say they successfully "
123                      "replicated?")),
124         Option("--classic", help="print local replication details",
125                dest='format', action='store_const', const='classic',
126                default=DEFAULT_SHOWREPL_FORMAT),
127         Option("-v", "--verbose", help="Be verbose", action="store_true"),
128         Option("--color", help="Use colour output (yes|no|auto)",
129                default='no'),
130     ]
131
132     takes_args = ["DC?"]
133
134     def parse_neighbour(self, n):
135         """Convert an ldb neighbour object into a python dictionary"""
136         dsa_objectguid = str(n.source_dsa_obj_guid)
137         d = {
138             'NC dn': n.naming_context_dn,
139             "DSA objectGUID": dsa_objectguid,
140             "last attempt time": nttime2string(n.last_attempt),
141             "last attempt message": drs_errmsg(n.result_last_attempt),
142             "consecutive failures": n.consecutive_sync_failures,
143             "last success": nttime2string(n.last_success),
144             "NTDS DN": str(n.source_dsa_obj_dn),
145             'is deleted': False
146         }
147
148         try:
149             self.samdb.search(base="<GUID=%s>" % dsa_objectguid,
150                               scope=ldb.SCOPE_BASE,
151                               attrs=[])
152         except ldb.LdbError as e:
153             (errno, _) = e.args
154             if errno == ldb.ERR_NO_SUCH_OBJECT:
155                 d['is deleted'] = True
156             else:
157                 raise
158         try:
159             (site, server) = drs_parse_ntds_dn(n.source_dsa_obj_dn)
160             d["DSA"] = "%s\%s" % (site, server)
161         except RuntimeError:
162             pass
163         return d
164
165     def print_neighbour(self, d):
166         '''print one set of neighbour information'''
167         self.message("%s" % d['NC dn'])
168         if 'DSA' in d:
169             self.message("\t%s via RPC" % d['DSA'])
170         else:
171             self.message("\tNTDS DN: %s" % d['NTDS DN'])
172         self.message("\t\tDSA object GUID: %s" % d['DSA objectGUID'])
173         self.message("\t\tLast attempt @ %s %s" % (d['last attempt time'],
174                                                    d['last attempt message']))
175         self.message("\t\t%u consecutive failure(s)." %
176                      d['consecutive failures'])
177         self.message("\t\tLast success @ %s" % d['last success'])
178         self.message("")
179
180     def get_neighbours(self, info_type):
181         req1 = drsuapi.DsReplicaGetInfoRequest1()
182         req1.info_type = info_type
183         try:
184             (info_type, info) = self.drsuapi.DsReplicaGetInfo(
185                 self.drsuapi_handle, 1, req1)
186         except Exception as e:
187             raise CommandError("DsReplicaGetInfo of type %u failed" % info_type, e)
188
189         reps = [self.parse_neighbour(n) for n in info.array]
190         return reps
191
192     def run(self, DC=None, sambaopts=None,
193             credopts=None, versionopts=None,
194             format=DEFAULT_SHOWREPL_FORMAT,
195             verbose=False, color='no'):
196         self.apply_colour_choice(color)
197         self.lp = sambaopts.get_loadparm()
198         if DC is None:
199             DC = common.netcmd_dnsname(self.lp)
200         self.server = DC
201         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
202         self.verbose = verbose
203
204         output_function = {
205             'summary': self.summary_output,
206             'notify_summary': self.notify_summary_output,
207             'pull_summary': self.pull_summary_output,
208             'json': self.json_output,
209             'classic': self.classic_output,
210         }.get(format)
211         if output_function is None:
212             raise CommandError("unknown showrepl format %s" % format)
213
214         return output_function()
215
216     def json_output(self):
217         data = self.get_local_repl_data()
218         del data['site']
219         del data['server']
220         json.dump(data, self.outf, indent=2)
221
222     def summary_output_handler(self, typeof_output):
223         """Print a short message if every seems fine, but print details of any
224         links that seem broken."""
225         failing_repsto = []
226         failing_repsfrom = []
227
228         local_data = self.get_local_repl_data()
229
230         if typeof_output != "pull_summary":
231             for rep in local_data['repsTo']:
232                 if rep['is deleted']:
233                     continue
234                 if rep["consecutive failures"] != 0 or rep["last success"] == 0:
235                     failing_repsto.append(rep)
236
237         if typeof_output != "notify_summary":
238             for rep in local_data['repsFrom']:
239                 if rep['is deleted']:
240                     continue
241                 if rep["consecutive failures"] != 0 or rep["last success"] == 0:
242                     failing_repsfrom.append(rep)
243
244         if failing_repsto or failing_repsfrom:
245             self.message(colour.c_RED("There are failing connections"))
246             if failing_repsto:
247                 self.message(colour.c_RED("Failing outbound connections:"))
248                 for rep in failing_repsto:
249                     self.print_neighbour(rep)
250             if failing_repsfrom:
251                 self.message(colour.c_RED("Failing inbound connection:"))
252                 for rep in failing_repsfrom:
253                     self.print_neighbour(rep)
254
255             return 1
256
257         self.message(colour.c_GREEN("[ALL GOOD]"))
258
259     def summary_output(self):
260         return self.summary_output_handler("summary")
261
262     def notify_summary_output(self):
263         return self.summary_output_handler("notify_summary")
264
265     def pull_summary_output(self):
266         return self.summary_output_handler("pull_summary")
267
268     def get_local_repl_data(self):
269         drsuapi_connect(self)
270         samdb_connect(self)
271
272         # show domain information
273         ntds_dn = self.samdb.get_dsServiceName()
274
275         (site, server) = drs_parse_ntds_dn(ntds_dn)
276         try:
277             ntds = self.samdb.search(base=ntds_dn, scope=ldb.SCOPE_BASE, attrs=['options', 'objectGUID', 'invocationId'])
278         except Exception as e:
279             raise CommandError("Failed to search NTDS DN %s" % ntds_dn)
280
281         dsa_details = {
282             "options": int(attr_default(ntds[0], "options", 0)),
283             "objectGUID": self.samdb.schema_format_value(
284                 "objectGUID", ntds[0]["objectGUID"][0]),
285             "invocationId": self.samdb.schema_format_value(
286                 "objectGUID", ntds[0]["invocationId"][0])
287         }
288
289         conn = self.samdb.search(base=ntds_dn, expression="(objectClass=nTDSConnection)")
290         repsfrom = self.get_neighbours(drsuapi.DRSUAPI_DS_REPLICA_INFO_NEIGHBORS)
291         repsto = self.get_neighbours(drsuapi.DRSUAPI_DS_REPLICA_INFO_REPSTO)
292
293         conn_details = []
294         for c in conn:
295             c_rdn, sep, c_server_dn = c['fromServer'][0].partition(',')
296             d = {
297                 'name': str(c['name']),
298                 'remote DN': c['fromServer'][0],
299                 'options': int(attr_default(c, 'options', 0)),
300                 'enabled': (attr_default(c, 'enabledConnection',
301                                          'TRUE').upper() == 'TRUE')
302             }
303
304             conn_details.append(d)
305             try:
306                 c_server_res = self.samdb.search(base=c_server_dn,
307                                                  scope=ldb.SCOPE_BASE,
308                                                  attrs=["dnsHostName"])
309                 d['dns name'] = c_server_res[0]["dnsHostName"][0]
310             except ldb.LdbError as e:
311                 (errno, _) = e.args
312                 if errno == ldb.ERR_NO_SUCH_OBJECT:
313                     d['is deleted'] = True
314             except (KeyError, IndexError):
315                 pass
316
317             d['replicates NC'] = []
318             for r in c.get('mS-DS-ReplicatesNCReason', []):
319                 a = str(r).split(':')
320                 d['replicates NC'].append((a[3], int(a[2])))
321
322         return {
323             'dsa': dsa_details,
324             'repsFrom': repsfrom,
325             'repsTo': repsto,
326             'NTDSConnections': conn_details,
327             'site': site,
328             'server': server
329         }
330
331     def classic_output(self):
332         data = self.get_local_repl_data()
333         dsa_details = data['dsa']
334         repsfrom = data['repsFrom']
335         repsto = data['repsTo']
336         conn_details = data['NTDSConnections']
337         site = data['site']
338         server = data['server']
339
340         self.message("%s\\%s" % (site, server))
341         self.message("DSA Options: 0x%08x" % dsa_details["options"])
342         self.message("DSA object GUID: %s" % dsa_details["objectGUID"])
343         self.message("DSA invocationId: %s\n" % dsa_details["invocationId"])
344
345         self.message("==== INBOUND NEIGHBORS ====\n")
346         for n in repsfrom:
347             self.print_neighbour(n)
348
349         self.message("==== OUTBOUND NEIGHBORS ====\n")
350         for n in repsto:
351             self.print_neighbour(n)
352
353         reasons = ['NTDSCONN_KCC_GC_TOPOLOGY',
354                    'NTDSCONN_KCC_RING_TOPOLOGY',
355                    'NTDSCONN_KCC_MINIMIZE_HOPS_TOPOLOGY',
356                    'NTDSCONN_KCC_STALE_SERVERS_TOPOLOGY',
357                    'NTDSCONN_KCC_OSCILLATING_CONNECTION_TOPOLOGY',
358                    'NTDSCONN_KCC_INTERSITE_GC_TOPOLOGY',
359                    'NTDSCONN_KCC_INTERSITE_TOPOLOGY',
360                    'NTDSCONN_KCC_SERVER_FAILOVER_TOPOLOGY',
361                    'NTDSCONN_KCC_SITE_FAILOVER_TOPOLOGY',
362                    'NTDSCONN_KCC_REDUNDANT_SERVER_TOPOLOGY']
363
364         self.message("==== KCC CONNECTION OBJECTS ====\n")
365         for d in conn_details:
366             self.message("Connection --")
367             if d.get('is deleted'):
368                 self.message("\tWARNING: Connection to DELETED server!")
369
370             self.message("\tConnection name: %s" % d['name'])
371             self.message("\tEnabled        : %s" % str(d['enabled']).upper())
372             self.message("\tServer DNS name : %s" % d.get('dns name'))
373             self.message("\tServer DN name  : %s" % d['remote DN'])
374             self.message("\t\tTransportType: RPC")
375             self.message("\t\toptions: 0x%08X" % d['options'])
376
377             if d['replicates NC']:
378                 for nc, reason in d['replicates NC']:
379                     self.message("\t\tReplicatesNC: %s" % nc)
380                     self.message("\t\tReason: 0x%08x" % reason)
381                     for s in reasons:
382                         if getattr(dsdb, s, 0) & reason:
383                             self.message("\t\t\t%s" % s)
384             else:
385                 self.message("Warning: No NC replicated for Connection!")
386
387
388 class cmd_drs_kcc(Command):
389     """Trigger knowledge consistency center run."""
390
391     synopsis = "%prog [<DC>] [options]"
392
393     takes_optiongroups = {
394         "sambaopts": options.SambaOptions,
395         "versionopts": options.VersionOptions,
396         "credopts": options.CredentialsOptions,
397     }
398
399     takes_args = ["DC?"]
400
401     def run(self, DC=None, sambaopts=None,
402             credopts=None, versionopts=None):
403
404         self.lp = sambaopts.get_loadparm()
405         if DC is None:
406             DC = common.netcmd_dnsname(self.lp)
407         self.server = DC
408
409         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
410
411         drsuapi_connect(self)
412
413         req1 = drsuapi.DsExecuteKCC1()
414         try:
415             self.drsuapi.DsExecuteKCC(self.drsuapi_handle, 1, req1)
416         except Exception as e:
417             raise CommandError("DsExecuteKCC failed", e)
418         self.message("Consistency check on %s successful." % DC)
419
420
421 class cmd_drs_replicate(Command):
422     """Replicate a naming context between two DCs."""
423
424     synopsis = "%prog <destinationDC> <sourceDC> <NC> [options]"
425
426     takes_optiongroups = {
427         "sambaopts": options.SambaOptions,
428         "versionopts": options.VersionOptions,
429         "credopts": options.CredentialsOptions,
430     }
431
432     takes_args = ["DEST_DC", "SOURCE_DC", "NC"]
433
434     takes_options = [
435         Option("--add-ref", help="use ADD_REF to add to repsTo on source", action="store_true"),
436         Option("--sync-forced", help="use SYNC_FORCED to force inbound replication", action="store_true"),
437         Option("--sync-all", help="use SYNC_ALL to replicate from all DCs", action="store_true"),
438         Option("--full-sync", help="resync all objects", action="store_true"),
439         Option("--local", help="pull changes directly into the local database (destination DC is ignored)", action="store_true"),
440         Option("--local-online", help="pull changes into the local database (destination DC is ignored) as a normal online replication", action="store_true"),
441         Option("--async-op", help="use ASYNC_OP for the replication", action="store_true"),
442         Option("--single-object", help="Replicate only the object specified, instead of the whole Naming Context (only with --local)", action="store_true"),
443     ]
444
445     def drs_local_replicate(self, SOURCE_DC, NC, full_sync=False,
446                             single_object=False,
447                             sync_forced=False):
448         '''replicate from a source DC to the local SAM'''
449
450         self.server = SOURCE_DC
451         drsuapi_connect(self)
452
453         self.local_samdb = SamDB(session_info=system_session(), url=None,
454                                  credentials=self.creds, lp=self.lp)
455
456         self.samdb = SamDB(url="ldap://%s" % self.server,
457                            session_info=system_session(),
458                            credentials=self.creds, lp=self.lp)
459
460         # work out the source and destination GUIDs
461         res = self.local_samdb.search(base="", scope=ldb.SCOPE_BASE,
462                                       attrs=["dsServiceName"])
463         self.ntds_dn = res[0]["dsServiceName"][0]
464
465         res = self.local_samdb.search(base=self.ntds_dn, scope=ldb.SCOPE_BASE,
466                                       attrs=["objectGUID"])
467         self.ntds_guid = misc.GUID(
468             self.samdb.schema_format_value("objectGUID",
469                                            res[0]["objectGUID"][0]))
470
471         source_dsa_invocation_id = misc.GUID(self.samdb.get_invocation_id())
472         dest_dsa_invocation_id = misc.GUID(self.local_samdb.get_invocation_id())
473         destination_dsa_guid = self.ntds_guid
474
475         exop = drsuapi.DRSUAPI_EXOP_NONE
476
477         if single_object:
478             exop = drsuapi.DRSUAPI_EXOP_REPL_OBJ
479             full_sync = True
480
481         self.samdb.transaction_start()
482         repl = drs_utils.drs_Replicate("ncacn_ip_tcp:%s[seal]" % self.server,
483                                        self.lp,
484                                        self.creds, self.local_samdb,
485                                        dest_dsa_invocation_id)
486
487         # Work out if we are an RODC, so that a forced local replicate
488         # with the admin pw does not sync passwords
489         rodc = self.local_samdb.am_rodc()
490         try:
491             (num_objects, num_links) = repl.replicate(NC,
492                                                       source_dsa_invocation_id,
493                                                       destination_dsa_guid,
494                                                       rodc=rodc,
495                                                       full_sync=full_sync,
496                                                       exop=exop,
497                                                       sync_forced=sync_forced)
498         except Exception as e:
499             raise CommandError("Error replicating DN %s" % NC, e)
500         self.samdb.transaction_commit()
501
502         if full_sync:
503             self.message("Full Replication of all %d objects and %d links "
504                          "from %s to %s was successful." %
505                          (num_objects, num_links, SOURCE_DC,
506                           self.local_samdb.url))
507         else:
508             self.message("Incremental replication of %d objects and %d links "
509                          "from %s to %s was successful." %
510                          (num_objects, num_links, SOURCE_DC,
511                           self.local_samdb.url))
512
513     def run(self, DEST_DC, SOURCE_DC, NC,
514             add_ref=False, sync_forced=False, sync_all=False, full_sync=False,
515             local=False, local_online=False, async_op=False, single_object=False,
516             sambaopts=None, credopts=None, versionopts=None):
517
518         self.server = DEST_DC
519         self.lp = sambaopts.get_loadparm()
520
521         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
522
523         if local:
524             self.drs_local_replicate(SOURCE_DC, NC, full_sync=full_sync,
525                                      single_object=single_object,
526                                      sync_forced=sync_forced)
527             return
528
529         if local_online:
530             server_bind = drsuapi.drsuapi("irpc:dreplsrv", lp_ctx=self.lp)
531             server_bind_handle = misc.policy_handle()
532         else:
533             drsuapi_connect(self)
534             server_bind = self.drsuapi
535             server_bind_handle = self.drsuapi_handle
536
537         if not async_op:
538             # Give the sync replication 5 minutes time
539             server_bind.request_timeout = 5 * 60
540
541         samdb_connect(self)
542
543         # we need to find the NTDS GUID of the source DC
544         msg = self.samdb.search(base=self.samdb.get_config_basedn(),
545                                 expression="(&(objectCategory=server)(|(name=%s)(dNSHostName=%s)))" % (
546             ldb.binary_encode(SOURCE_DC),
547             ldb.binary_encode(SOURCE_DC)),
548                                 attrs=[])
549         if len(msg) == 0:
550             raise CommandError("Failed to find source DC %s" % SOURCE_DC)
551         server_dn = msg[0]['dn']
552
553         msg = self.samdb.search(base=server_dn, scope=ldb.SCOPE_ONELEVEL,
554                                 expression="(|(objectCategory=nTDSDSA)(objectCategory=nTDSDSARO))",
555                                 attrs=['objectGUID', 'options'])
556         if len(msg) == 0:
557             raise CommandError("Failed to find source NTDS DN %s" % SOURCE_DC)
558         source_dsa_guid = msg[0]['objectGUID'][0]
559         dsa_options = int(attr_default(msg, 'options', 0))
560
561         req_options = 0
562         if not (dsa_options & dsdb.DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL):
563             req_options |= drsuapi.DRSUAPI_DRS_WRIT_REP
564         if add_ref:
565             req_options |= drsuapi.DRSUAPI_DRS_ADD_REF
566         if sync_forced:
567             req_options |= drsuapi.DRSUAPI_DRS_SYNC_FORCED
568         if sync_all:
569             req_options |= drsuapi.DRSUAPI_DRS_SYNC_ALL
570         if full_sync:
571             req_options |= drsuapi.DRSUAPI_DRS_FULL_SYNC_NOW
572         if async_op:
573             req_options |= drsuapi.DRSUAPI_DRS_ASYNC_OP
574
575         try:
576             drs_utils.sendDsReplicaSync(server_bind, server_bind_handle, source_dsa_guid, NC, req_options)
577         except drs_utils.drsException as estr:
578             raise CommandError("DsReplicaSync failed", estr)
579         if async_op:
580             self.message("Replicate from %s to %s was started." % (SOURCE_DC, DEST_DC))
581         else:
582             self.message("Replicate from %s to %s was successful." % (SOURCE_DC, DEST_DC))
583
584
585 class cmd_drs_bind(Command):
586     """Show DRS capabilities of a server."""
587
588     synopsis = "%prog [<DC>] [options]"
589
590     takes_optiongroups = {
591         "sambaopts": options.SambaOptions,
592         "versionopts": options.VersionOptions,
593         "credopts": options.CredentialsOptions,
594     }
595
596     takes_args = ["DC?"]
597
598     def run(self, DC=None, sambaopts=None,
599             credopts=None, versionopts=None):
600
601         self.lp = sambaopts.get_loadparm()
602         if DC is None:
603             DC = common.netcmd_dnsname(self.lp)
604         self.server = DC
605         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
606
607         drsuapi_connect(self)
608
609         bind_info = drsuapi.DsBindInfoCtr()
610         bind_info.length = 28
611         bind_info.info = drsuapi.DsBindInfo28()
612         (info, handle) = self.drsuapi.DsBind(misc.GUID(drsuapi.DRSUAPI_DS_BIND_GUID), bind_info)
613
614         optmap = [
615             ("DRSUAPI_SUPPORTED_EXTENSION_BASE", "DRS_EXT_BASE"),
616             ("DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION", "DRS_EXT_ASYNCREPL"),
617             ("DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI", "DRS_EXT_REMOVEAPI"),
618             ("DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2", "DRS_EXT_MOVEREQ_V2"),
619             ("DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS", "DRS_EXT_GETCHG_DEFLATE"),
620             ("DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1", "DRS_EXT_DCINFO_V1"),
621             ("DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION", "DRS_EXT_RESTORE_USN_OPTIMIZATION"),
622             ("DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY", "DRS_EXT_ADDENTRY"),
623             ("DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE", "DRS_EXT_KCC_EXECUTE"),
624             ("DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2", "DRS_EXT_ADDENTRY_V2"),
625             ("DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION", "DRS_EXT_LINKED_VALUE_REPLICATION"),
626             ("DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2", "DRS_EXT_DCINFO_V2"),
627             ("DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD", "DRS_EXT_INSTANCE_TYPE_NOT_REQ_ON_MOD"),
628             ("DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND", "DRS_EXT_CRYPTO_BIND"),
629             ("DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO", "DRS_EXT_GET_REPL_INFO"),
630             ("DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION", "DRS_EXT_STRONG_ENCRYPTION"),
631             ("DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01", "DRS_EXT_DCINFO_VFFFFFFFF"),
632             ("DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP", "DRS_EXT_TRANSITIVE_MEMBERSHIP"),
633             ("DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY", "DRS_EXT_ADD_SID_HISTORY"),
634             ("DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3", "DRS_EXT_POST_BETA3"),
635             ("DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5", "DRS_EXT_GETCHGREQ_V5"),
636             ("DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2", "DRS_EXT_GETMEMBERSHIPS2"),
637             ("DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6", "DRS_EXT_GETCHGREQ_V6"),
638             ("DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS", "DRS_EXT_NONDOMAIN_NCS"),
639             ("DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8", "DRS_EXT_GETCHGREQ_V8"),
640             ("DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5", "DRS_EXT_GETCHGREPLY_V5"),
641             ("DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6", "DRS_EXT_GETCHGREPLY_V6"),
642             ("DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3", "DRS_EXT_WHISTLER_BETA3"),
643             ("DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7", "DRS_EXT_WHISTLER_BETA3"),
644             ("DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT", "DRS_EXT_WHISTLER_BETA3"),
645             ("DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS", "DRS_EXT_W2K3_DEFLATE"),
646             ("DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10", "DRS_EXT_GETCHGREQ_V10"),
647             ("DRSUAPI_SUPPORTED_EXTENSION_RESERVED_PART2", "DRS_EXT_RESERVED_FOR_WIN2K_OR_DOTNET_PART2"),
648             ("DRSUAPI_SUPPORTED_EXTENSION_RESERVED_PART3", "DRS_EXT_RESERVED_FOR_WIN2K_OR_DOTNET_PART3")
649         ]
650
651         optmap_ext = [
652             ("DRSUAPI_SUPPORTED_EXTENSION_ADAM", "DRS_EXT_ADAM"),
653             ("DRSUAPI_SUPPORTED_EXTENSION_LH_BETA2", "DRS_EXT_LH_BETA2"),
654             ("DRSUAPI_SUPPORTED_EXTENSION_RECYCLE_BIN", "DRS_EXT_RECYCLE_BIN")]
655
656         self.message("Bind to %s succeeded." % DC)
657         self.message("Extensions supported:")
658         for (opt, str) in optmap:
659             optval = getattr(drsuapi, opt, 0)
660             if info.info.supported_extensions & optval:
661                 yesno = "Yes"
662             else:
663                 yesno = "No "
664             self.message("  %-60s: %s (%s)" % (opt, yesno, str))
665
666         if isinstance(info.info, drsuapi.DsBindInfo48):
667             self.message("\nExtended Extensions supported:")
668             for (opt, str) in optmap_ext:
669                 optval = getattr(drsuapi, opt, 0)
670                 if info.info.supported_extensions_ext & optval:
671                     yesno = "Yes"
672                 else:
673                     yesno = "No "
674                 self.message("  %-60s: %s (%s)" % (opt, yesno, str))
675
676         self.message("\nSite GUID: %s" % info.info.site_guid)
677         self.message("Repl epoch: %u" % info.info.repl_epoch)
678         if isinstance(info.info, drsuapi.DsBindInfo48):
679             self.message("Forest GUID: %s" % info.info.config_dn_guid)
680
681
682 class cmd_drs_options(Command):
683     """Query or change 'options' for NTDS Settings object of a Domain Controller."""
684
685     synopsis = "%prog [<DC>] [options]"
686
687     takes_optiongroups = {
688         "sambaopts": options.SambaOptions,
689         "versionopts": options.VersionOptions,
690         "credopts": options.CredentialsOptions,
691     }
692
693     takes_args = ["DC?"]
694
695     takes_options = [
696         Option("--dsa-option", help="DSA option to enable/disable", type="str",
697                metavar="{+|-}IS_GC | {+|-}DISABLE_INBOUND_REPL | {+|-}DISABLE_OUTBOUND_REPL | {+|-}DISABLE_NTDSCONN_XLATE"),
698     ]
699
700     option_map = {"IS_GC": 0x00000001,
701                   "DISABLE_INBOUND_REPL": 0x00000002,
702                   "DISABLE_OUTBOUND_REPL": 0x00000004,
703                   "DISABLE_NTDSCONN_XLATE": 0x00000008}
704
705     def run(self, DC=None, dsa_option=None,
706             sambaopts=None, credopts=None, versionopts=None):
707
708         self.lp = sambaopts.get_loadparm()
709         if DC is None:
710             DC = common.netcmd_dnsname(self.lp)
711         self.server = DC
712         self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
713
714         samdb_connect(self)
715
716         ntds_dn = self.samdb.get_dsServiceName()
717         res = self.samdb.search(base=ntds_dn, scope=ldb.SCOPE_BASE, attrs=["options"])
718         dsa_opts = int(res[0]["options"][0])
719
720         # print out current DSA options
721         cur_opts = [x for x in self.option_map if self.option_map[x] & dsa_opts]
722         self.message("Current DSA options: " + ", ".join(cur_opts))
723
724         # modify options
725         if dsa_option:
726             if dsa_option[:1] not in ("+", "-"):
727                 raise CommandError("Unknown option %s" % dsa_option)
728             flag = dsa_option[1:]
729             if flag not in self.option_map.keys():
730                 raise CommandError("Unknown option %s" % dsa_option)
731             if dsa_option[:1] == "+":
732                 dsa_opts |= self.option_map[flag]
733             else:
734                 dsa_opts &= ~self.option_map[flag]
735             # save new options
736             m = ldb.Message()
737             m.dn = ldb.Dn(self.samdb, ntds_dn)
738             m["options"] = ldb.MessageElement(str(dsa_opts), ldb.FLAG_MOD_REPLACE, "options")
739             self.samdb.modify(m)
740             # print out new DSA options
741             cur_opts = [x for x in self.option_map if self.option_map[x] & dsa_opts]
742             self.message("New DSA options: " + ", ".join(cur_opts))
743
744
745 class cmd_drs_clone_dc_database(Command):
746     """Replicate an initial clone of domain, but DO NOT JOIN it."""
747
748     synopsis = "%prog <dnsdomain> [options]"
749
750     takes_optiongroups = {
751         "sambaopts": options.SambaOptions,
752         "versionopts": options.VersionOptions,
753         "credopts": options.CredentialsOptions,
754     }
755
756     takes_options = [
757         Option("--server", help="DC to join", type=str),
758         Option("--targetdir", help="where to store provision (required)", type=str),
759         Option("-q", "--quiet", help="Be quiet", action="store_true"),
760         Option("--include-secrets", help="Also replicate secret values", action="store_true"),
761         Option("-v", "--verbose", help="Be verbose", action="store_true")
762     ]
763
764     takes_args = ["domain"]
765
766     def run(self, domain, sambaopts=None, credopts=None,
767             versionopts=None, server=None, targetdir=None,
768             quiet=False, verbose=False, include_secrets=False):
769         lp = sambaopts.get_loadparm()
770         creds = credopts.get_credentials(lp)
771
772         logger = self.get_logger(verbose=verbose, quiet=quiet)
773
774         if targetdir is None:
775             raise CommandError("--targetdir option must be specified")
776
777         join_clone(logger=logger, server=server, creds=creds, lp=lp,
778                    domain=domain, dns_backend='SAMBA_INTERNAL',
779                    targetdir=targetdir, include_secrets=include_secrets)
780
781
782 class cmd_drs_uptodateness(Command):
783     """Show uptodateness status"""
784
785     synopsis = "%prog [options]"
786
787     takes_optiongroups = {
788         "sambaopts": options.SambaOptions,
789         "versionopts": options.VersionOptions,
790         "credopts": options.CredentialsOptions,
791     }
792
793     takes_options = [
794         Option("-H", "--URL", metavar="URL", dest="H",
795                help="LDB URL for database or target server"),
796         Option("-p", "--partition",
797                help="restrict to this partition"),
798         Option("--json", action='store_true',
799                help="Print data in json format"),
800         Option("--maximum", action='store_true',
801                help="Print maximum out-of-date-ness only"),
802         Option("--median", action='store_true',
803                help="Print median out-of-date-ness only"),
804         Option("--full", action='store_true',
805                help="Print full out-of-date-ness data"),
806     ]
807
808     def format_as_json(self, partitions_summaries):
809         return json.dumps(partitions_summaries, indent=2)
810
811     def format_as_text(self, partitions_summaries):
812         lines = []
813         for part_name, summary in partitions_summaries.items():
814             items = ['%s: %s' % (k, v) for k, v in summary.items()]
815             line = '%-15s %s' % (part_name, '  '.join(items))
816             lines.append(line)
817         return '\n'.join(lines)
818
819     def run(self, H=None, partition=None,
820             json=False, maximum=False, median=False, full=False,
821             sambaopts=None, credopts=None, versionopts=None,
822             quiet=False, verbose=False):
823
824         lp = sambaopts.get_loadparm()
825         creds = credopts.get_credentials(lp, fallback_machine=True)
826         local_kcc, dsas = get_kcc_and_dsas(H, lp, creds)
827         samdb = local_kcc.samdb
828         short_partitions, _ = get_partition_maps(samdb)
829         if partition:
830             if partition in short_partitions:
831                 part_dn = short_partitions[partition]
832                 # narrow down to specified partition only
833                 short_partitions = {partition: part_dn}
834             else:
835                 raise CommandError("unknown partition %s" % partition)
836
837         filters = []
838         if maximum:
839             filters.append('maximum')
840         if median:
841             filters.append('median')
842
843         partitions_distances = {}
844         partitions_summaries = {}
845         for part_name, part_dn in short_partitions.items():
846             utdv_edges = get_utdv_edges(local_kcc, dsas, part_dn, lp, creds)
847             distances = get_utdv_distances(utdv_edges, dsas)
848             summary = get_utdv_summary(distances, filters=filters)
849             partitions_distances[part_name] = distances
850             partitions_summaries[part_name] = summary
851
852         if full:
853             # always print json format
854             output = self.format_as_json(partitions_distances)
855         else:
856             if json:
857                 output = self.format_as_json(partitions_summaries)
858             else:
859                 output = self.format_as_text(partitions_summaries)
860
861         print(output, file=self.outf)
862
863
864 class cmd_drs(SuperCommand):
865     """Directory Replication Services (DRS) management."""
866
867     subcommands = {}
868     subcommands["bind"] = cmd_drs_bind()
869     subcommands["kcc"] = cmd_drs_kcc()
870     subcommands["replicate"] = cmd_drs_replicate()
871     subcommands["showrepl"] = cmd_drs_showrepl()
872     subcommands["options"] = cmd_drs_options()
873     subcommands["clone-dc-database"] = cmd_drs_clone_dc_database()
874     subcommands["uptodateness"] = cmd_drs_uptodateness()