.gitlab-ci-main.yml: Add safe.directory '*'
[Samba.git] / python / samba / netcmd / user / delete.py
blob377068709374226e98af46fc3ae7da0b860fc7ce
1 # user management
3 # delete user
5 # Copyright Jelmer Vernooij 2010 <jelmer@samba.org>
6 # Copyright Theresa Halloran 2011 <theresahalloran@gmail.com>
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.
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.
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/>.
22 import samba.getopt as options
23 from samba import ldb
24 from samba.auth import system_session
25 from samba.netcmd import Command, CommandError, Option
26 from samba.samdb import SamDB
27 from samba.dsdb import ATYPE_NORMAL_ACCOUNT
30 class cmd_user_delete(Command):
31 """Delete a user.
33 This command deletes a user account from the Active Directory domain. The username specified on the command is the sAMAccountName.
35 Once the account is deleted, all permissions and memberships associated with that account are deleted. If a new user account is added with the same name as a previously deleted account name, the new user does not have the previous permissions. The new account user will be assigned a new security identifier (SID) and permissions and memberships will have to be added.
37 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.
39 Example1:
40 samba-tool user delete User1 -H ldap://samba.samdom.example.com --username=administrator --password=passw1rd
42 Example1 shows how to delete a user in the domain against a remote LDAP server. The -H parameter is used to specify the remote target server. The --username= and --password= options are used to pass the username and password of a user that exists on the remote server and is authorized to issue the command on that server.
44 Example2:
45 sudo samba-tool user delete User2
47 Example2 shows how to delete a user in the domain against the local server. sudo is used so a user may run the command as root.
49 """
50 synopsis = "%prog <username> [options]"
52 takes_options = [
53 Option("-H", "--URL", help="LDB URL for database or target server", type=str,
54 metavar="URL", dest="H"),
57 takes_args = ["username"]
58 takes_optiongroups = {
59 "sambaopts": options.SambaOptions,
60 "credopts": options.CredentialsOptions,
61 "versionopts": options.VersionOptions,
64 def run(self, username, credopts=None, sambaopts=None, versionopts=None,
65 H=None):
66 lp = sambaopts.get_loadparm()
67 creds = credopts.get_credentials(lp, fallback_machine=True)
69 samdb = SamDB(url=H, session_info=system_session(),
70 credentials=creds, lp=lp)
72 filter = (f"(&(sAMAccountName={ldb.binary_encode(username)})"
73 f"(sAMAccountType={ATYPE_NORMAL_ACCOUNT}))")
75 try:
76 res = samdb.search(base=samdb.domain_dn(),
77 scope=ldb.SCOPE_SUBTREE,
78 expression=filter,
79 attrs=["dn"])
80 user_dn = res[0].dn
81 except IndexError:
82 raise CommandError('Unable to find user "%s"' % (username))
84 try:
85 samdb.delete(user_dn)
86 except Exception as e:
87 raise CommandError('Failed to remove user "%s"' % username, e)
88 self.outf.write("Deleted user %s\n" % username)