samba-tool: Added "user list" command
authorGiampaolo Lauria <lauria2@yahoo.com>
Tue, 8 Nov 2011 16:51:42 +0000 (11:51 -0500)
committerAndrew Tridgell <tridge@samba.org>
Thu, 10 Nov 2011 03:24:21 +0000 (14:24 +1100)
source4/scripting/python/samba/netcmd/user.py

index 7072521aed25b00d25ac5d1101e3563a93b7eba1..2ebf0ed3155be8d7b00afd81fc152ba806fe49c8 100644 (file)
@@ -26,6 +26,7 @@ from getpass import getpass
 from samba.auth import system_session
 from samba.samdb import SamDB
 from samba import gensec, generate_random_password
+from samba import dsdb
 from samba.net import Net
 
 from samba.netcmd import (
@@ -186,6 +187,38 @@ Example2 shows how to delete a user in the domain against the local server.   su
         self.outf.write("Deleted user %s\n" % username)
 
 
+class cmd_user_list(Command):
+    """List all users"""
+
+    synopsis = "%prog [options]"
+
+    takes_options = [
+        Option("-H", "--URL", help="LDB URL for database or target server", type=str,
+               metavar="URL", dest="H"),
+        ]
+
+    def run(self, sambaopts=None, credopts=None, versionopts=None, H=None):
+        lp = sambaopts.get_loadparm()
+        creds = credopts.get_credentials(lp, fallback_machine=True)
+
+        samdb = SamDB(url=H, session_info=system_session(),
+            credentials=creds, lp=lp)
+
+        domain_dn = samdb.domain_dn()
+        res = samdb.search(domain_dn, scope=ldb.SCOPE_SUBTREE,
+                    expression=("(&(objectClass=user)(userAccountControl:%s:=%u))"
+                    % (ldb.OID_COMPARATOR_AND, dsdb.UF_NORMAL_ACCOUNT)),
+                    attrs=["name"])
+        if (len(res) == 0):
+            return
+
+        try:
+            for msg in res:
+                self.outf.write("%s\n" % msg["name"][0])
+        except Exception, msg:
+            raise CommandError("Failed to get user list: %s" % msg)
+
+
 class cmd_user_enable(Command):
     """Enables a user
 
@@ -440,6 +473,7 @@ class cmd_user(SuperCommand):
     subcommands["create"] = cmd_user_create()
     subcommands["delete"] = cmd_user_delete()
     subcommands["enable"] = cmd_user_enable()
+    subcommands["list"] = cmd_user_list()
     subcommands["setexpiry"] = cmd_user_setexpiry()
     subcommands["password"] = cmd_user_password()
     subcommands["setpassword"] = cmd_user_setpassword()