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