Debian Codegraph @

lib/ldb-samba/tests/match_rules_remote.py

raw
1 #!/usr/bin/env python3
2
3 import optparse
4 import sys
5 import os
6 import samba
7 import samba.getopt as options
8
9 from samba.tests.subunitrun import SubunitOptions, TestProgram
10
11 from samba.samdb import SamDB
12 from samba.auth import system_session
13 from samba import sd_utils
14 from samba.ndr import ndr_unpack
15 from ldb import Message, MessageElement, Dn, LdbError
16 from ldb import FLAG_MOD_ADD, FLAG_MOD_REPLACE, FLAG_MOD_DELETE
17 from ldb import SCOPE_BASE, SCOPE_SUBTREE, SCOPE_ONELEVEL
18
19 from match_rules import MatchRulesTestsBase
20
21
22 class MatchRulesTestsUser(MatchRulesTestsBase):
23 def setUp(self):
24 self.sambaopts = sambaopts
25 self.credopts = credopts
26 self.host = host
27 super().setUp()
28 self.sd_utils = sd_utils.SDUtils(self.ldb)
29
30 self.user_pass = "samba123@"
31 self.match_test_user = "matchtestuser"
32 self.ldb.newuser(self.match_test_user,
33 self.user_pass,
34 userou=self.ou_rdn)
35 user_creds = self.insta_creds(template=self.creds,
36 username=self.match_test_user,
37 userpass=self.user_pass)
38 self.user_ldb = SamDB(host, credentials=user_creds, lp=self.lp)
39 token_res = self.user_ldb.search(scope=SCOPE_BASE,
40 base="",
41 attrs=["tokenGroups"])
42 self.user_sid = ndr_unpack(samba.dcerpc.security.dom_sid,
43 token_res[0]["tokenGroups"][0])
44
45 self.member_attr_guid = "bf9679c0-0de6-11d0-a285-00aa003049e2"
46
47 def test_with_denied_link(self):
48
49 # add an ACE that denies the user Read Property (RP) access to
50 # the member attr (which is similar to making the attribute
51 # confidential)
52 ace = "(OD;;RP;{0};;{1})".format(self.member_attr_guid,
53 self.user_sid)
54 g2_dn = Dn(self.ldb, "CN=g2,%s" % self.ou_groups)
55
56 # add the ACE that denies access to the attr under test
57 self.sd_utils.dacl_add_ace(g2_dn, ace)
58
59 # Search without transitive match must return 0 results
60 res1 = self.ldb.search("cn=g4,%s" % self.ou_groups,
61 scope=SCOPE_BASE,
62 expression="member=cn=u1,%s" % self.ou_users)
63 self.assertEqual(len(res1), 0)
64
65 # Search with transitive match must return 1 results
66 res1 = self.ldb.search("cn=g4,%s" % self.ou_groups,
67 scope=SCOPE_BASE,
68 expression="member:1.2.840.113556.1.4.1941:=cn=u1,%s" % self.ou_users)
69 self.assertEqual(len(res1), 1)
70 self.assertEqual(str(res1[0].dn).lower(), ("CN=g4,%s" % self.ou_groups).lower())
71
72 # Search as a user match must return 0 results as the intermediate link can't be seen
73 res1 = self.user_ldb.search("cn=g4,%s" % self.ou_groups,
74 scope=SCOPE_BASE,
75 expression="member:1.2.840.113556.1.4.1941:=cn=u1,%s" % self.ou_users)
76 self.assertEqual(len(res1), 0)
77
78
79
80 parser = optparse.OptionParser("match_rules_remote.py [options] <host>")
81 sambaopts = options.SambaOptions(parser)
82 parser.add_option_group(sambaopts)
83 parser.add_option_group(options.VersionOptions(parser))
84
85 # use command line creds if available
86 credopts = options.CredentialsOptions(parser)
87 parser.add_option_group(credopts)
88 opts, args = parser.parse_args()
89 subunitopts = SubunitOptions(parser)
90 parser.add_option_group(subunitopts)
91
92 if len(args) < 1:
93 parser.print_usage()
94 sys.exit(1)
95
96 host = args[0]
97
98 if "://" not in host:
99 if os.path.isfile(host):
100 host = "tdb://%s" % host
101 else:
102 host = "ldap://%s" % host
103
104 TestProgram(module=__name__, opts=subunitopts)