From: Rob van der Linde Date: Thu, 27 Jul 2023 05:19:34 +0000 (+1200) Subject: netcmd: user: move user move command X-Git-Tag: tevent-0.16.0~1260 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41492dadcc1f727f2ab530a86e8312ccfd880111;p=thirdparty%2Fsamba.git netcmd: user: move user move command Signed-off-by: Rob van der Linde Reviewed-by: Douglas Bagnall Reviewed-by: Andrew Bartlett --- diff --git a/python/samba/netcmd/user/__init__.py b/python/samba/netcmd/user/__init__.py index ead3185296d..a9a76e488ce 100644 --- a/python/samba/netcmd/user/__init__.py +++ b/python/samba/netcmd/user/__init__.py @@ -49,6 +49,7 @@ from .enable import cmd_user_enable from .getgroups import cmd_user_getgroups from .getpassword import cmd_user_getpassword, cmd_user_syncpasswords from .list import cmd_user_list +from .move import cmd_user_move from .password import cmd_user_password from .setexpiry import cmd_user_setexpiry from .setpassword import cmd_user_setpassword @@ -56,86 +57,6 @@ from .setprimarygroup import cmd_user_setprimarygroup from .show import cmd_user_show -class cmd_user_move(Command): - """Move a user to an organizational unit/container. - - This command moves a user account into the specified organizational unit - or container. - The username specified on the command is the sAMAccountName. - The name of the organizational unit or container can be specified as a - full DN or without the domainDN component. - - The command may be run from the root userid or another authorized userid. - - The -H or --URL= option can be used to execute the command against a remote - server. - - Example1: - samba-tool user move User1 'OU=OrgUnit,DC=samdom,DC=example,DC=com' \\ - -H ldap://samba.samdom.example.com -U administrator - - Example1 shows how to move a user User1 into the 'OrgUnit' organizational - unit on a remote LDAP server. - - The -H parameter is used to specify the remote target server. - - Example2: - samba-tool user move User1 CN=Users - - Example2 shows how to move a user User1 back into the CN=Users container - on the local server. - """ - - synopsis = "%prog [options]" - - takes_options = [ - Option("-H", "--URL", help="LDB URL for database or target server", - type=str, metavar="URL", dest="H"), - ] - - takes_args = ["username", "new_parent_dn"] - takes_optiongroups = { - "sambaopts": options.SambaOptions, - "credopts": options.CredentialsOptions, - "versionopts": options.VersionOptions, - } - - def run(self, username, new_parent_dn, credopts=None, sambaopts=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) - domain_dn = ldb.Dn(samdb, samdb.domain_dn()) - - filter = ("(&(sAMAccountType=%d)(sAMAccountName=%s))" % - (dsdb.ATYPE_NORMAL_ACCOUNT, ldb.binary_encode(username))) - try: - res = samdb.search(base=domain_dn, - expression=filter, - scope=ldb.SCOPE_SUBTREE) - user_dn = res[0].dn - except IndexError: - raise CommandError('Unable to find user "%s"' % (username)) - - try: - full_new_parent_dn = samdb.normalize_dn_in_domain(new_parent_dn) - except Exception as e: - raise CommandError('Invalid new_parent_dn "%s": %s' % - (new_parent_dn, e)) - - full_new_user_dn = ldb.Dn(samdb, str(user_dn)) - full_new_user_dn.remove_base_components(len(user_dn) - 1) - full_new_user_dn.add_base(full_new_parent_dn) - - try: - samdb.rename(user_dn, full_new_user_dn) - except Exception as e: - raise CommandError('Failed to move user "%s"' % username, e) - self.outf.write('Moved user "%s" into "%s"\n' % - (username, full_new_parent_dn)) - - class cmd_user_rename(Command): """Rename a user and related attributes. diff --git a/python/samba/netcmd/user/move.py b/python/samba/netcmd/user/move.py new file mode 100644 index 00000000000..bcb0f9e309f --- /dev/null +++ b/python/samba/netcmd/user/move.py @@ -0,0 +1,106 @@ +# user management +# +# user move command +# +# Copyright Jelmer Vernooij 2010 +# Copyright Theresa Halloran 2011 +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +import samba.getopt as options +from samba import dsdb, ldb +from samba.auth import system_session +from samba.netcmd import Command, CommandError, Option +from samba.samdb import SamDB + + +class cmd_user_move(Command): + """Move a user to an organizational unit/container. + + This command moves a user account into the specified organizational unit + or container. + The username specified on the command is the sAMAccountName. + The name of the organizational unit or container can be specified as a + full DN or without the domainDN component. + + The command may be run from the root userid or another authorized userid. + + The -H or --URL= option can be used to execute the command against a remote + server. + + Example1: + samba-tool user move User1 'OU=OrgUnit,DC=samdom,DC=example,DC=com' \\ + -H ldap://samba.samdom.example.com -U administrator + + Example1 shows how to move a user User1 into the 'OrgUnit' organizational + unit on a remote LDAP server. + + The -H parameter is used to specify the remote target server. + + Example2: + samba-tool user move User1 CN=Users + + Example2 shows how to move a user User1 back into the CN=Users container + on the local server. + """ + + synopsis = "%prog [options]" + + takes_options = [ + Option("-H", "--URL", help="LDB URL for database or target server", + type=str, metavar="URL", dest="H"), + ] + + takes_args = ["username", "new_parent_dn"] + takes_optiongroups = { + "sambaopts": options.SambaOptions, + "credopts": options.CredentialsOptions, + "versionopts": options.VersionOptions, + } + + def run(self, username, new_parent_dn, credopts=None, sambaopts=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) + domain_dn = ldb.Dn(samdb, samdb.domain_dn()) + + filter = ("(&(sAMAccountType=%d)(sAMAccountName=%s))" % + (dsdb.ATYPE_NORMAL_ACCOUNT, ldb.binary_encode(username))) + try: + res = samdb.search(base=domain_dn, + expression=filter, + scope=ldb.SCOPE_SUBTREE) + user_dn = res[0].dn + except IndexError: + raise CommandError('Unable to find user "%s"' % (username)) + + try: + full_new_parent_dn = samdb.normalize_dn_in_domain(new_parent_dn) + except Exception as e: + raise CommandError('Invalid new_parent_dn "%s": %s' % + (new_parent_dn, e)) + + full_new_user_dn = ldb.Dn(samdb, str(user_dn)) + full_new_user_dn.remove_base_components(len(user_dn) - 1) + full_new_user_dn.add_base(full_new_parent_dn) + + try: + samdb.rename(user_dn, full_new_user_dn) + except Exception as e: + raise CommandError('Failed to move user "%s"' % username, e) + self.outf.write('Moved user "%s" into "%s"\n' % + (username, full_new_parent_dn))