3 # user sensitive command
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 dsdb
, ldb
24 from samba
.auth
import system_session
25 from samba
.netcmd
import Command
, CommandError
, Option
26 from samba
.samdb
import SamDB
29 class cmd_user_sensitive(Command
):
30 """Set/unset or show UF_NOT_DELEGATED for an account."""
32 synopsis
= "%prog <accountname> [(show|on|off)] [options]"
34 takes_optiongroups
= {
35 "sambaopts": options
.SambaOptions
,
36 "credopts": options
.CredentialsOptions
,
37 "versionopts": options
.VersionOptions
,
41 Option("-H", "--URL", help="LDB URL for database or target server", type=str,
42 metavar
="URL", dest
="H"),
45 takes_args
= ["accountname", "cmd"]
47 def run(self
, accountname
, cmd
, H
=None, credopts
=None, sambaopts
=None,
50 if cmd
not in ("show", "on", "off"):
51 raise CommandError("invalid argument: '%s' (choose from 'show', 'on', 'off')" % cmd
)
53 lp
= sambaopts
.get_loadparm()
54 creds
= credopts
.get_credentials(lp
, fallback_machine
=True)
55 sam
= SamDB(url
=H
, session_info
=system_session(),
56 credentials
=creds
, lp
=lp
)
58 search_filter
= "sAMAccountName=%s" % ldb
.binary_encode(accountname
)
59 flag
= dsdb
.UF_NOT_DELEGATED
62 res
= sam
.search(scope
=ldb
.SCOPE_SUBTREE
, expression
=search_filter
,
63 attrs
=["userAccountControl"])
65 raise Exception("Unable to find account where '%s'" % search_filter
)
67 uac
= int(res
[0].get("userAccountControl")[0])
69 self
.outf
.write("Account-DN: %s\n" % str(res
[0].dn
))
70 self
.outf
.write("UF_NOT_DELEGATED: %s\n" % bool(uac
& flag
))
80 sam
.toggle_userAccountFlags(search_filter
, flag
, flags_str
="Not-Delegated",
82 except Exception as err
:
83 raise CommandError(err
)