librpc/ndr: Fix fuzz CI on latest tumbleweed
[Samba.git] / python / samba / netcmd / user / list.py
blob3d16f0ef9d7a89ea737dc25b5564f4a366e9b4dd
1 # user management
3 # list users
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, Option
26 from samba.samdb import SamDB
29 class cmd_user_list(Command):
30 """List all users."""
32 synopsis = "%prog [options]"
34 takes_options = [
35 Option("-H", "--URL", help="LDB URL for database or target server", type=str,
36 metavar="URL", dest="H"),
37 Option("--hide-expired",
38 help="Do not list expired user accounts",
39 default=False,
40 action='store_true'),
41 Option("--hide-disabled",
42 default=False,
43 action='store_true',
44 help="Do not list disabled user accounts"),
45 Option("--locked-only",
46 help="Only list locked user accounts",
47 default=False,
48 action='store_true'),
49 Option("-b", "--base-dn",
50 help="Specify base DN to use",
51 type=str),
52 Option("--full-dn", dest="full_dn",
53 default=False,
54 action='store_true',
55 help="Display DN instead of the sAMAccountName.")
58 takes_optiongroups = {
59 "sambaopts": options.SambaOptions,
60 "credopts": options.CredentialsOptions,
61 "versionopts": options.VersionOptions,
64 def run(self,
65 sambaopts=None,
66 credopts=None,
67 versionopts=None,
68 H=None,
69 hide_expired=False,
70 hide_disabled=False,
71 locked_only=False,
72 base_dn=None,
73 full_dn=False):
74 lp = sambaopts.get_loadparm()
75 creds = credopts.get_credentials(lp, fallback_machine=True)
77 samdb = SamDB(url=H, session_info=system_session(),
78 credentials=creds, lp=lp)
80 search_dn = samdb.domain_dn()
81 if base_dn:
82 search_dn = samdb.normalize_dn_in_domain(base_dn)
84 filter_expires = ""
85 if hide_expired is True:
86 current_nttime = samdb.get_nttime()
87 filter_expires = "(|(accountExpires=0)(accountExpires>=%u))" % (
88 current_nttime)
90 filter_disabled = ""
91 if hide_disabled is True:
92 filter_disabled = "(!(userAccountControl:%s:=%u))" % (
93 ldb.OID_COMPARATOR_AND, dsdb.UF_ACCOUNTDISABLE)
95 filter_locked = ""
96 if locked_only is True:
97 # use lockoutTime=* to filter out accounts without a set lockoutTime
98 filter_locked = "(&(lockoutTime=*)(!(lockoutTime=0)))"
100 filter = "(&(objectClass=user)(userAccountControl:%s:=%u)%s%s%s)" % (
101 ldb.OID_COMPARATOR_AND,
102 dsdb.UF_NORMAL_ACCOUNT,
103 filter_disabled,
104 filter_locked,
105 filter_expires)
107 res = samdb.search(search_dn,
108 scope=ldb.SCOPE_SUBTREE,
109 expression=filter,
110 attrs=["samaccountname"])
111 if (len(res) == 0):
112 return
114 for msg in res:
115 if full_dn:
116 self.outf.write("%s\n" % msg.get("dn"))
117 continue
119 self.outf.write("%s\n" % msg.get("samaccountname", idx=0))