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
):
32 synopsis
= "%prog [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",
41 Option("--hide-disabled",
44 help="Do not list disabled user accounts"),
45 Option("--locked-only",
46 help="Only list locked user accounts",
49 Option("-b", "--base-dn",
50 help="Specify base DN to use",
52 Option("--full-dn", dest
="full_dn",
55 help="Display DN instead of the sAMAccountName.")
58 takes_optiongroups
= {
59 "sambaopts": options
.SambaOptions
,
60 "credopts": options
.CredentialsOptions
,
61 "versionopts": options
.VersionOptions
,
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()
82 search_dn
= samdb
.normalize_dn_in_domain(base_dn
)
85 if hide_expired
is True:
86 current_nttime
= samdb
.get_nttime()
87 filter_expires
= "(|(accountExpires=0)(accountExpires>=%u))" % (
91 if hide_disabled
is True:
92 filter_disabled
= "(!(userAccountControl:%s:=%u))" % (
93 ldb
.OID_COMPARATOR_AND
, dsdb
.UF_ACCOUNTDISABLE
)
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
,
107 res
= samdb
.search(search_dn
,
108 scope
=ldb
.SCOPE_SUBTREE
,
110 attrs
=["samaccountname"])
116 self
.outf
.write("%s\n" % msg
.get("dn"))
119 self
.outf
.write("%s\n" % msg
.get("samaccountname", idx
=0))