From 1a6a626a45107c7b3451ac0d33dd1b6d214dab97 Mon Sep 17 00:00:00 2001 From: Giampaolo Lauria Date: Tue, 2 Aug 2011 18:05:54 -0400 Subject: [PATCH] samba-tool: Changed fsmo implementation Changed fsmo code to fit the object model Signed-off-by: Amitay Isaacs Signed-off-by: Andrew Bartlett --- source4/scripting/python/samba/netcmd/fsmo.py | 198 +++++++++++++++++--------- 1 file changed, 130 insertions(+), 68 deletions(-) diff --git a/source4/scripting/python/samba/netcmd/fsmo.py b/source4/scripting/python/samba/netcmd/fsmo.py index c2b0101a46f..58b920d0a37 100644 --- a/source4/scripting/python/samba/netcmd/fsmo.py +++ b/source4/scripting/python/samba/netcmd/fsmo.py @@ -28,14 +28,17 @@ from samba.auth import system_session from samba.netcmd import ( Command, CommandError, + SuperCommand, Option, ) from samba.samdb import SamDB -class cmd_fsmo(Command): - """Flexible Single Master Operations (FSMO) roles management *""" - synopsis = "(show | transfer | seize )" + +class cmd_fsmo_seize(Command): + """Seize the role""" + + synopsis = "%prog fsmo seize [options]" takes_options = [ Option("-H", "--URL", help="LDB URL for database or target server", type=str, @@ -51,45 +54,7 @@ infrastructure=InfrastructureMasterRole\n all=all of the above"""), ] - takes_args = ["subcommand"] - - def transfer_role(self, role, samdb): - m = ldb.Message() - m.dn = ldb.Dn(samdb, "") - if role == "rid": - m["becomeRidMaster"]= ldb.MessageElement( - "1", ldb.FLAG_MOD_REPLACE, - "becomeRidMaster") - elif role == "pdc": - domain_dn = samdb.domain_dn() - res = samdb.search(domain_dn, - scope=ldb.SCOPE_BASE, attrs=["objectSid"]) - assert len(res) == 1 - sid = res[0]["objectSid"][0] - m["becomePdc"]= ldb.MessageElement( - sid, ldb.FLAG_MOD_REPLACE, - "becomePdc") - elif role == "naming": - m["becomeDomainMaster"]= ldb.MessageElement( - "1", ldb.FLAG_MOD_REPLACE, - "becomeDomainMaster") - samdb.modify(m) - elif role == "infrastructure": - m["becomeInfrastructureMaster"]= ldb.MessageElement( - "1", ldb.FLAG_MOD_REPLACE, - "becomeInfrastructureMaster") - elif role == "schema": - m["becomeSchemaMaster"]= ldb.MessageElement( - "1", ldb.FLAG_MOD_REPLACE, - "becomeSchemaMaster") - else: - raise CommandError("Invalid FSMO role.") - try: - samdb.modify(m) - except LdbError, (num, msg): - raise CommandError("Failed to initiate transfer of '%s' role: %s" % (role, msg)) - print("FSMO transfer of '%s' role successful" % role) - + takes_args = [] def seize_role(self, role, samdb, force): res = samdb.search("", @@ -132,8 +97,39 @@ all=all of the above"""), raise CommandError("Failed to initiate role seize of '%s' role: %s" % (role, msg)) print("FSMO transfer of '%s' role successful" % role) + def run(self, force=None, H=None, role=None, + credopts=None, sambaopts=None, versionopts=None): + + lp = sambaopts.get_loadparm() + creds = credopts.get_credentials(lp, fallback_machine=True) + + samdb = SamDB(url=H, session_info=system_session(), + credentials=creds, lp=lp) + + if role == "all": + self.seize_role("rid", samdb, force) + self.seize_role("pdc", samdb, force) + self.seize_role("naming", samdb, force) + self.seize_role("infrastructure", samdb, force) + self.seize_role("schema", samdb, force) + else: + self.seize_role(role, samdb, force) - def run(self, subcommand, force=None, H=None, role=None, + + +class cmd_fsmo_show(Command): + """Show the roles""" + + synopsis = "%prog fsmo show [options]" + + takes_options = [ + Option("-H", "--URL", help="LDB URL for database or target server", type=str, + metavar="URL", dest="H"), + ] + + takes_args = [] + + def run(self, H=None, credopts=None, sambaopts=None, versionopts=None): lp = sambaopts.get_loadparm() creds = credopts.get_credentials(lp, fallback_machine=True) @@ -172,29 +168,95 @@ all=all of the above"""), assert len(res) == 1 self.ridMaster = res[0]["fSMORoleOwner"][0] - if subcommand == "show": - self.message("InfrastructureMasterRole owner: " + self.infrastructureMaster) - self.message("RidAllocationMasterRole owner: " + self.ridMaster) - self.message("PdcEmulationMasterRole owner: " + self.pdcEmulator) - self.message("DomainNamingMasterRole owner: " + self.namingMaster) - self.message("SchemaMasterRole owner: " + self.schemaMaster) - elif subcommand == "transfer": - if role == "all": - self.transfer_role("rid", samdb) - self.transfer_role("pdc", samdb) - self.transfer_role("naming", samdb) - self.transfer_role("infrastructure", samdb) - self.transfer_role("schema", samdb) - else: - self.transfer_role(role, samdb) - elif subcommand == "seize": - if role == "all": - self.seize_role("rid", samdb, force) - self.seize_role("pdc", samdb, force) - self.seize_role("naming", samdb, force) - self.seize_role("infrastructure", samdb, force) - self.seize_role("schema", samdb, force) - else: - self.seize_role(role, samdb, force) + self.message("InfrastructureMasterRole owner: " + self.infrastructureMaster) + self.message("RidAllocationMasterRole owner: " + self.ridMaster) + self.message("PdcEmulationMasterRole owner: " + self.pdcEmulator) + self.message("DomainNamingMasterRole owner: " + self.namingMaster) + self.message("SchemaMasterRole owner: " + self.schemaMaster) + + + +class cmd_fsmo_transfer(Command): + """Transfer the role""" + + synopsis = "%prog fsmo transfer [options]" + + takes_options = [ + Option("-H", "--URL", help="LDB URL for database or target server", type=str, + metavar="URL", dest="H"), + Option("--role", type="choice", choices=["rid", "pdc", "infrastructure","schema","naming","all"], + help="""The FSMO role to seize or transfer.\n +rid=RidAllocationMasterRole\n +schema=SchemaMasterRole\n +pdc=PdcEmulationMasterRole\n +naming=DomainNamingMasterRole\n +infrastructure=InfrastructureMasterRole\n +all=all of the above"""), + ] + + takes_args = [] + + def transfer_role(self, role, samdb): + m = ldb.Message() + m.dn = ldb.Dn(samdb, "") + if role == "rid": + m["becomeRidMaster"]= ldb.MessageElement( + "1", ldb.FLAG_MOD_REPLACE, + "becomeRidMaster") + elif role == "pdc": + domain_dn = samdb.domain_dn() + res = samdb.search(domain_dn, + scope=ldb.SCOPE_BASE, attrs=["objectSid"]) + assert len(res) == 1 + sid = res[0]["objectSid"][0] + m["becomePdc"]= ldb.MessageElement( + sid, ldb.FLAG_MOD_REPLACE, + "becomePdc") + elif role == "naming": + m["becomeDomainMaster"]= ldb.MessageElement( + "1", ldb.FLAG_MOD_REPLACE, + "becomeDomainMaster") + samdb.modify(m) + elif role == "infrastructure": + m["becomeInfrastructureMaster"]= ldb.MessageElement( + "1", ldb.FLAG_MOD_REPLACE, + "becomeInfrastructureMaster") + elif role == "schema": + m["becomeSchemaMaster"]= ldb.MessageElement( + "1", ldb.FLAG_MOD_REPLACE, + "becomeSchemaMaster") else: - raise CommandError("Wrong argument '%s'!" % subcommand) + raise CommandError("Invalid FSMO role.") + try: + samdb.modify(m) + except LdbError, (num, msg): + raise CommandError("Failed to initiate transfer of '%s' role: %s" % (role, msg)) + print("FSMO transfer of '%s' role successful" % role) + + def run(self, force=None, H=None, role=None, + credopts=None, sambaopts=None, versionopts=None): + + lp = sambaopts.get_loadparm() + creds = credopts.get_credentials(lp, fallback_machine=True) + + samdb = SamDB(url=H, session_info=system_session(), + credentials=creds, lp=lp) + + if role == "all": + self.transfer_role("rid", samdb) + self.transfer_role("pdc", samdb) + self.transfer_role("naming", samdb) + self.transfer_role("infrastructure", samdb) + self.transfer_role("schema", samdb) + else: + self.transfer_role(role, samdb) + + + +class cmd_fsmo(SuperCommand): + """Flexible Single Master Operations (FSMO) roles management *""" + + subcommands = {} + subcommands["seize"] = cmd_fsmo_seize() + subcommands["show"] = cmd_fsmo_show() + subcommands["transfer"] = cmd_fsmo_transfer() -- 2.11.4.GIT