>From ffd2b7fb458c87edb93d98dfb7d20897f9fa7fd3 Mon Sep 17 00:00:00 2001 From: Roel van Meer Date: Tue, 22 Dec 2015 14:55:18 +1300 Subject: [PATCH 1/2] samba-tool sites: Add "list" command This adds one command to samba-tool: samba-tool sites list The command lists the configured sites in the domain. --- python/samba/netcmd/sites.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/python/samba/netcmd/sites.py b/python/samba/netcmd/sites.py index f0c792d..13936dc 100644 --- a/python/samba/netcmd/sites.py +++ b/python/samba/netcmd/sites.py @@ -16,6 +16,8 @@ # along with this program. If not, see . # +import os +import ldb from samba import sites, subnets from samba.samdb import SamDB import samba.getopt as options @@ -102,6 +104,37 @@ class cmd_sites_delete(Command): self.outf.write("Site %s removed!\n" % sitename) +class cmd_sites_list(Command): + """List all sites.""" + + synopsis = "%prog [options]" + + takes_optiongroups = { + "sambaopts": options.SambaOptions, + "versionopts": options.VersionOptions, + "credopts": options.CredentialsOptions, + } + + def run(self, sambaopts=None, credopts=None, versionopts=None): + lp = sambaopts.get_loadparm() + creds = credopts.get_credentials(lp, fallback_machine=True) + url = lp.private_path("sam.ldb") + + if not os.path.exists(url): + raise CommandError("secret database not found at %s " % url) + samdb = SamDB(url=url, session_info=system_session(), + credentials=creds, lp=lp) + + res = samdb.search(samdb.get_config_basedn(), scope=ldb.SCOPE_SUBTREE, + expression="(objectClass=Site)", + attrs=["cn"]) + if (len(res) == 0): + return + + for msg in res: + self.outf.write("%s\n" % msg.get("cn", idx=0)) + + class cmd_sites_subnet_create(Command): """Create a new subnet.""" synopsis = "%prog [options]" @@ -220,9 +253,11 @@ class cmd_sites_subnet(SuperCommand): "set-site": cmd_sites_subnet_set_site(), } + class cmd_sites(SuperCommand): """Sites management.""" subcommands = {} subcommands["create"] = cmd_sites_create() subcommands["remove"] = cmd_sites_delete() + subcommands["list"] = cmd_sites_list() subcommands["subnet"] = cmd_sites_subnet() -- 2.1.4 >From 3413a1bf54cd9be0bc87c32f788e27c047bea875 Mon Sep 17 00:00:00 2001 From: Roel van Meer Date: Tue, 22 Dec 2015 15:03:23 +1300 Subject: [PATCH 2/2] samba-tool sites: Add "moveserver" command This adds a command that can be used to move a server from one site to another: samba-tool sites moveserver --- python/samba/netcmd/sites.py | 41 ++++++++++++++++++++++++++++++++++ python/samba/sites.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/python/samba/netcmd/sites.py b/python/samba/netcmd/sites.py index 13936dc..480234c 100644 --- a/python/samba/netcmd/sites.py +++ b/python/samba/netcmd/sites.py @@ -135,6 +135,46 @@ class cmd_sites_list(Command): self.outf.write("%s\n" % msg.get("cn", idx=0)) +class cmd_sites_moveserver(Command): + """Move a server from one site to another.""" + + synopsis = "%prog [options]" + + takes_args = ["server", "cursitename", "newsitename"] + + takes_options = [ + Option("-H", "--URL", help="LDB URL for database or target server", + type=str, metavar="URL", dest="H"), + ] + + takes_optiongroups = { + "sambaopts": options.SambaOptions, + "versionopts": options.VersionOptions, + "credopts": options.CredentialsOptions, + } + + def run(self, server, cursitename, newsitename, sambaopts=None, + credopts=None, versionopts=None, H=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) + + samdb.transaction_start() + try: + ok = sites.move_server(samdb, samdb.get_config_basedn(), server, + cursitename, newsitename) + samdb.transaction_commit() + except Exception, e: + samdb.transaction_cancel() + raise CommandError( + "Error while moving server %s, error: %s" % (server, str(e))) + + self.outf.write("Server %s moved successfully\n" % server) + + + class cmd_sites_subnet_create(Command): """Create a new subnet.""" synopsis = "%prog [options]" @@ -260,4 +300,5 @@ class cmd_sites(SuperCommand): subcommands["create"] = cmd_sites_create() subcommands["remove"] = cmd_sites_delete() subcommands["list"] = cmd_sites_list() + subcommands["moveserver"] = cmd_sites_moveserver() subcommands["subnet"] = cmd_sites_subnet() diff --git a/python/samba/sites.py b/python/samba/sites.py index 7111cfa..0ab69fe 100644 --- a/python/samba/sites.py +++ b/python/samba/sites.py @@ -43,6 +43,10 @@ class SiteServerNotEmptyException(SiteException): """Raised when the site still has servers attached.""" +class ServerNotFoundException(SiteException): + """Raised when a server is not found and it's expected to exist.""" + + def create_site(samdb, configDn, siteName): """ Create a site @@ -119,3 +123,51 @@ def delete_site(samdb, configDn, siteName): samdb.delete(dnsite, ["tree_delete:0"]) return True + + +def move_server(samdb, configDn, server, cursiteName, newsiteName): + """ + Move a server from one site to another + + :param samdb: A samdb connection + :param configDn: The DN of the configuration partition + :param server: The name of the server to be moved + :param cursiteName: Name of the site the server is currently in + :param newsiteName: Name of the site the server should be moved to + :return: True upon success + :raise SiteNotFoundException: if either of the sites does not exist + :raise ServerNotFoundException: if the server is not found in the + specified site + """ + + dnsites = ldb.Dn(samdb, "CN=Sites,%s" % (str(configDn))) + dncursite = ldb.Dn(samdb, "CN=%s,CN=Sites,%s" % + (cursiteName, str(configDn))) + dnnewsite = ldb.Dn(samdb, "CN=%s,CN=Sites,%s" % + (newsiteName, str(configDn))) + dnservers = ldb.Dn(samdb, "CN=Servers,CN=%s,CN=Sites,%s" % + (cursiteName, str(configDn))) + dncurserver = ldb.Dn(samdb, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s" % + (server.upper(), cursiteName, str(configDn))) + dnnewserver = ldb.Dn(samdb, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s" % + (server.upper(), newsiteName, str(configDn))) + + ret = samdb.search(base=dnsites, scope=ldb.SCOPE_ONELEVEL, + expression='(dn=%s)' % str(dncursite)) + if len(ret) != 1: + raise SiteNotFoundException('Site %s does not exist' % cursiteName) + + ret = samdb.search(base=dnsites, scope=ldb.SCOPE_ONELEVEL, + expression='(dn=%s)' % str(dnnewsite)) + if len(ret) != 1: + raise SiteNotFoundException('Site %s does not exist' % newsiteName) + + ret = samdb.search(base=dnservers, scope=ldb.SCOPE_ONELEVEL, + expression='(dn=%s)' % str(dncurserver)) + if len(ret) != 1: + raise ServerNotFoundException( + 'Server %s not found in site %s' % (server, cursiteName)) + + samdb.rename(dncurserver, dnnewserver) + + return True -- 2.1.4