1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-11 05:18:09 +03:00

python/samba/netcmd/group.py: add group show

The samba-tool user command can show the ldif of a user. This is
useful for groups also, especially to determine the objectSID and
objectGUID. Add support for group show to samba-tool.

Signed-off-by: William Brown <william@blackhats.net.au>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
This commit is contained in:
William Brown 2018-04-26 13:59:06 +10:00 committed by Andrew Bartlett
parent b8e51a8174
commit 289ae87c3b
3 changed files with 96 additions and 0 deletions

View File

@ -610,6 +610,11 @@
<para>Remove members from the specified AD group.</para> <para>Remove members from the specified AD group.</para>
</refsect3> </refsect3>
<refsect3>
<title>group show <replaceable>groupname</replaceable> [options]</title>
<para>Show group object and it's attributes.</para>
</refsect3>
<refsect2> <refsect2>
<title>ldapcmp <replaceable>URL1</replaceable> <replaceable>URL2</replaceable> <replaceable>domain|configuration|schema|dnsdomain|dnsforest</replaceable> [options] </title> <title>ldapcmp <replaceable>URL1</replaceable> <replaceable>URL2</replaceable> <replaceable>domain|configuration|schema|dnsdomain|dnsforest</replaceable> [options] </title>
<para>Compare two LDAP databases.</para> <para>Compare two LDAP databases.</para>

View File

@ -26,6 +26,7 @@ from getpass import getpass
from samba.auth import system_session from samba.auth import system_session
from samba.samdb import SamDB from samba.samdb import SamDB
from samba.dsdb import ( from samba.dsdb import (
ATYPE_SECURITY_GLOBAL_GROUP,
GTYPE_SECURITY_BUILTIN_LOCAL_GROUP, GTYPE_SECURITY_BUILTIN_LOCAL_GROUP,
GTYPE_SECURITY_DOMAIN_LOCAL_GROUP, GTYPE_SECURITY_DOMAIN_LOCAL_GROUP,
GTYPE_SECURITY_GLOBAL_GROUP, GTYPE_SECURITY_GLOBAL_GROUP,
@ -500,6 +501,85 @@ class cmd_group_move(Command):
self.outf.write('Moved group "%s" into "%s"\n' % self.outf.write('Moved group "%s" into "%s"\n' %
(groupname, full_new_parent_dn)) (groupname, full_new_parent_dn))
class cmd_group_show(Command):
"""Display a group AD object.
This command displays a group object and it's attributes in the Active
Directory domain.
The group name specified on the command is the sAMAccountName of the group.
The command may be run from the root userid or another authorized userid.
The -H or --URL= option can be used to execute the command against a remote
server.
Example1:
samba-tool group show Group1 -H ldap://samba.samdom.example.com \
-U administrator --password=passw1rd
Example1 shows how to display a group's attributes in the domain against a remote
LDAP server.
The -H parameter is used to specify the remote target server.
Example2:
samba-tool group show Group2
Example2 shows how to display a group's attributes in the domain against a local
LDAP server.
Example3:
samba-tool group show Group3 --attributes=member,objectGUID
Example3 shows how to display a users objectGUID and member attributes.
"""
synopsis = "%prog <group name> [options]"
takes_options = [
Option("-H", "--URL", help="LDB URL for database or target server",
type=str, metavar="URL", dest="H"),
Option("--attributes",
help=("Comma separated list of attributes, "
"which will be printed."),
type=str, dest="group_attrs"),
]
takes_args = ["groupname"]
takes_optiongroups = {
"sambaopts": options.SambaOptions,
"credopts": options.CredentialsOptions,
"versionopts": options.VersionOptions,
}
def run(self, groupname, credopts=None, sambaopts=None, versionopts=None,
H=None, group_attrs=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)
attrs = None
if group_attrs:
attrs = group_attrs.split(",")
filter = ("(&(sAMAccountType=%d)(sAMAccountName=%s))" %
( ATYPE_SECURITY_GLOBAL_GROUP,
ldb.binary_encode(groupname)))
domaindn = samdb.domain_dn()
try:
res = samdb.search(base=domaindn, expression=filter,
scope=ldb.SCOPE_SUBTREE, attrs=attrs)
user_dn = res[0].dn
except IndexError:
raise CommandError('Unable to find group "%s"' % (groupname))
for msg in res:
user_ldif = samdb.write_ldif(msg, ldb.CHANGETYPE_NONE)
self.outf.write(user_ldif)
class cmd_group(SuperCommand): class cmd_group(SuperCommand):
"""Group management.""" """Group management."""
@ -511,3 +591,4 @@ class cmd_group(SuperCommand):
subcommands["list"] = cmd_group_list() subcommands["list"] = cmd_group_list()
subcommands["listmembers"] = cmd_group_list_members() subcommands["listmembers"] = cmd_group_list_members()
subcommands["move"] = cmd_group_move() subcommands["move"] = cmd_group_move()
subcommands["show"] = cmd_group_show()

View File

@ -170,6 +170,16 @@ class GroupCmdTestCase(SambaToolCmdTest):
self.assertCmdSuccess(result, out, err, self.assertCmdSuccess(result, out, err,
"Failed to delete ou '%s'" % full_ou_dn) "Failed to delete ou '%s'" % full_ou_dn)
def test_show(self):
"""Assert that we can show a group correctly."""
(result, out, err) = self.runsubcmd("group", "show", "Domain Users",
"-H", "ldap://%s" % os.environ["DC_SERVER"],
"-U%s%%%s" % (os.environ["DC_USERNAME"],
os.environ["DC_PASSWORD"]))
self.assertCmdSuccess(result, out, err)
self.assertEquals(err,"","Shouldn't be any error messages")
self.assertIn("dn: CN=Domain Users,CN=Users,DC=samba,DC=example,DC=com", out)
def _randomGroup(self, base={}): def _randomGroup(self, base={}):
"""create a group with random attribute values, you can specify base attributes""" """create a group with random attribute values, you can specify base attributes"""
group = { group = {