netcmd: user: readpasswords: move getpassword command to readpasswords
[Samba.git] / python / samba / netcmd / user / list.py
blob10605ca68f473ccc3194c3de72f1d4076f31f19e
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("-b", "--base-dn",
46 help="Specify base DN to use",
47 type=str),
48 Option("--full-dn", dest="full_dn",
49 default=False,
50 action='store_true',
51 help="Display DN instead of the sAMAccountName.")
54 takes_optiongroups = {
55 "sambaopts": options.SambaOptions,
56 "credopts": options.CredentialsOptions,
57 "versionopts": options.VersionOptions,
60 def run(self,
61 sambaopts=None,
62 credopts=None,
63 versionopts=None,
64 H=None,
65 hide_expired=False,
66 hide_disabled=False,
67 base_dn=None,
68 full_dn=False):
69 lp = sambaopts.get_loadparm()
70 creds = credopts.get_credentials(lp, fallback_machine=True)
72 samdb = SamDB(url=H, session_info=system_session(),
73 credentials=creds, lp=lp)
75 search_dn = samdb.domain_dn()
76 if base_dn:
77 search_dn = samdb.normalize_dn_in_domain(base_dn)
79 filter_expires = ""
80 if hide_expired is True:
81 current_nttime = samdb.get_nttime()
82 filter_expires = "(|(accountExpires=0)(accountExpires>=%u))" % (
83 current_nttime)
85 filter_disabled = ""
86 if hide_disabled is True:
87 filter_disabled = "(!(userAccountControl:%s:=%u))" % (
88 ldb.OID_COMPARATOR_AND, dsdb.UF_ACCOUNTDISABLE)
90 filter = "(&(objectClass=user)(userAccountControl:%s:=%u)%s%s)" % (
91 ldb.OID_COMPARATOR_AND,
92 dsdb.UF_NORMAL_ACCOUNT,
93 filter_disabled,
94 filter_expires)
96 res = samdb.search(search_dn,
97 scope=ldb.SCOPE_SUBTREE,
98 expression=filter,
99 attrs=["samaccountname"])
100 if (len(res) == 0):
101 return
103 for msg in res:
104 if full_dn:
105 self.outf.write("%s\n" % msg.get("dn"))
106 continue
108 self.outf.write("%s\n" % msg.get("samaccountname", idx=0))