7 import samba
.getopt
as options
9 from samba
.tests
.subunitrun
import SubunitOptions
, TestProgram
11 from samba
.samdb
import SamDB
12 from samba
.auth
import system_session
13 from ldb
import Message
, MessageElement
, Dn
, LdbError
14 from ldb
import FLAG_MOD_ADD
, FLAG_MOD_REPLACE
, FLAG_MOD_DELETE
15 from ldb
import SCOPE_BASE
, SCOPE_SUBTREE
, SCOPE_ONELEVEL
17 # TODO I'm ignoring case in these tests for now.
18 # This should be fixed to work inline with Windows.
19 # The literal strings are in the case Windows uses.
20 # Windows appear to preserve casing of the RDN and uppercase the other keys.
23 class MatchRulesTests(samba
.tests
.TestCase
):
25 super(MatchRulesTests
, self
).setUp()
27 self
.ldb
= SamDB(host
, credentials
=creds
, session_info
=system_session(lp
), lp
=lp
)
28 self
.base_dn
= self
.ldb
.domain_dn()
29 self
.ou
= "OU=matchrulestest,%s" % self
.base_dn
30 self
.ou_users
= "OU=users,%s" % self
.ou
31 self
.ou_groups
= "OU=groups,%s" % self
.ou
32 self
.ou_computers
= "OU=computers,%s" % self
.ou
34 # Add a organizational unit to create objects
37 "objectclass": "organizationalUnit"})
39 # Add the following OU hierarchy and set otherWellKnownObjects,
40 # which has BinaryDN syntax:
48 "dn": "OU=o1,%s" % self
.ou
,
49 "objectclass": "organizationalUnit"})
51 "dn": "OU=o2,OU=o1,%s" % self
.ou
,
52 "objectclass": "organizationalUnit"})
54 "dn": "OU=o3,OU=o2,OU=o1,%s" % self
.ou
,
55 "objectclass": "organizationalUnit"})
57 "dn": "OU=o4,OU=o3,OU=o2,OU=o1,%s" % self
.ou
,
58 "objectclass": "organizationalUnit"})
61 m
.dn
= Dn(self
.ldb
, self
.ou
)
62 m
["otherWellKnownObjects"] = MessageElement("B:32:00000000000000000000000000000001:OU=o1,%s" % self
.ou
,
63 FLAG_MOD_ADD
, "otherWellKnownObjects")
67 m
.dn
= Dn(self
.ldb
, "OU=o1,%s" % self
.ou
)
68 m
["otherWellKnownObjects"] = MessageElement("B:32:00000000000000000000000000000002:OU=o2,OU=o1,%s" % self
.ou
,
69 FLAG_MOD_ADD
, "otherWellKnownObjects")
73 m
.dn
= Dn(self
.ldb
, "OU=o2,OU=o1,%s" % self
.ou
)
74 m
["otherWellKnownObjects"] = MessageElement("B:32:00000000000000000000000000000003:OU=o3,OU=o2,OU=o1,%s" % self
.ou
,
75 FLAG_MOD_ADD
, "otherWellKnownObjects")
79 m
.dn
= Dn(self
.ldb
, "OU=o3,OU=o2,OU=o1,%s" % self
.ou
)
80 m
["otherWellKnownObjects"] = MessageElement("B:32:00000000000000000000000000000004:OU=o4,OU=o3,OU=o2,OU=o1,%s" % self
.ou
,
81 FLAG_MOD_ADD
, "otherWellKnownObjects")
84 # Create OU for users and groups
87 "objectclass": "organizationalUnit"})
90 "objectclass": "organizationalUnit"})
92 "dn": self
.ou_computers
,
93 "objectclass": "organizationalUnit"})
97 "dn": "cn=g1,%s" % self
.ou_groups
,
98 "objectclass": "group"})
100 "dn": "cn=g2,%s" % self
.ou_groups
,
101 "objectclass": "group"})
103 "dn": "cn=g4,%s" % self
.ou_groups
,
104 "objectclass": "group"})
106 "dn": "cn=g3,%s" % self
.ou_groups
,
107 "objectclass": "group"})
111 "dn": "cn=u1,%s" % self
.ou_users
,
112 "objectclass": "user"})
114 "dn": "cn=u2,%s" % self
.ou_users
,
115 "objectclass": "user"})
117 "dn": "cn=u3,%s" % self
.ou_users
,
118 "objectclass": "user"})
120 "dn": "cn=u4,%s" % self
.ou_users
,
121 "objectclass": "user"})
123 # Add computers to test Object(DN-Binary) syntax
125 "dn": "cn=c1,%s" % self
.ou_computers
,
126 "objectclass": "computer",
127 "dNSHostName": "c1.%s" % self
.lp
.get("realm").lower(),
128 "servicePrincipalName": ["HOST/c1"],
129 "sAMAccountName": "c1$",
130 "userAccountControl": "83890178"})
133 "dn": "cn=c2,%s" % self
.ou_computers
,
134 "objectclass": "computer",
135 "dNSHostName": "c2.%s" % self
.lp
.get("realm").lower(),
136 "servicePrincipalName": ["HOST/c2"],
137 "sAMAccountName": "c2$",
138 "userAccountControl": "83890178"})
141 "dn": "cn=c3,%s" % self
.ou_computers
,
142 "objectclass": "computer",
143 "dNSHostName": "c3.%s" % self
.lp
.get("realm").lower(),
144 "servicePrincipalName": ["HOST/c3"],
145 "sAMAccountName": "c3$",
146 "userAccountControl": "83890178"})
148 # Create the following hierarchy:
160 m
.dn
= Dn(self
.ldb
, "CN=g1,%s" % self
.ou_groups
)
161 m
["member"] = MessageElement("CN=u1,%s" % self
.ou_users
,
162 FLAG_MOD_ADD
, "member")
167 m
.dn
= Dn(self
.ldb
, "CN=g2,%s" % self
.ou_groups
)
168 m
["member"] = MessageElement("cn=u2,%s" % self
.ou_users
,
169 FLAG_MOD_ADD
, "member")
174 m
.dn
= Dn(self
.ldb
, "cn=g3,%s" % self
.ou_groups
)
175 m
["member"] = MessageElement("CN=u3,%s" % self
.ou_users
,
176 FLAG_MOD_ADD
, "member")
181 m
.dn
= Dn(self
.ldb
, "cn=g4,%s" % self
.ou_groups
)
182 m
["member"] = MessageElement("cn=u4,%s" % self
.ou_users
,
183 FLAG_MOD_ADD
, "member")
188 m
.dn
= Dn(self
.ldb
, "CN=g4,%s" % self
.ou_groups
)
189 m
["member"] = MessageElement("cn=g3,%s" % self
.ou_groups
,
190 FLAG_MOD_ADD
, "member")
195 m
.dn
= Dn(self
.ldb
, "cn=g3,%s" % self
.ou_groups
)
196 m
["member"] = MessageElement("CN=g2,%s" % self
.ou_groups
,
197 FLAG_MOD_ADD
, "member")
202 m
.dn
= Dn(self
.ldb
, "cn=g2,%s" % self
.ou_groups
)
203 m
["member"] = MessageElement("cn=g1,%s" % self
.ou_groups
,
204 FLAG_MOD_ADD
, "member")
207 # The msDS-RevealedUsers is owned by system and cannot be modified
208 # directly. Set the schemaUpgradeInProgress flag as workaround
209 # and create this hierarchy:
216 # While appropriate for this test, this is NOT a good practice
217 # in general. This is only done here because the alternative
218 # is to make a schema modification.
220 # IF/WHEN Samba protects this attribute better, this
221 # particular part of the test can be removed, as the same code
222 # is covered by the addressBookRoots2 case well enough.
225 m
.dn
= Dn(self
.ldb
, "")
226 m
["e1"] = MessageElement("1", FLAG_MOD_REPLACE
, "schemaUpgradeInProgress")
230 m
.dn
= Dn(self
.ldb
, "cn=c2,%s" % self
.ou_computers
)
231 m
["e1"] = MessageElement("B:8:01010101:cn=c3,%s" % self
.ou_computers
,
232 FLAG_MOD_ADD
, "msDS-RevealedUsers")
236 m
.dn
= Dn(self
.ldb
, "cn=c1,%s" % self
.ou_computers
)
237 m
["e1"] = MessageElement("B:8:01010101:cn=c2,%s" % self
.ou_computers
,
238 FLAG_MOD_ADD
, "msDS-RevealedUsers")
242 m
.dn
= Dn(self
.ldb
, "")
243 m
["e1"] = MessageElement("0", FLAG_MOD_REPLACE
, "schemaUpgradeInProgress")
246 # Add a couple of ms-Exch-Configuration-Container to test forward-link
247 # attributes without backward link (addressBookRoots2)
252 "dn": "cn=e1,%s" % self
.ou
,
253 "objectclass": "msExchConfigurationContainer"})
255 "dn": "cn=e2,%s" % self
.ou
,
256 "objectclass": "msExchConfigurationContainer"})
259 m
.dn
= Dn(self
.ldb
, "cn=e2,%s" % self
.ou
)
260 m
["e1"] = MessageElement("cn=c1,%s" % self
.ou_computers
,
261 FLAG_MOD_ADD
, "addressBookRoots2")
265 m
.dn
= Dn(self
.ldb
, "cn=e1,%s" % self
.ou
)
266 m
["e1"] = MessageElement("cn=e2,%s" % self
.ou
,
267 FLAG_MOD_ADD
, "addressBookRoots2")
271 super(MatchRulesTests
, self
).tearDown()
272 self
.ldb
.delete(self
.ou
, controls
=['tree_delete:0'])
274 def test_u1_member_of_g4(self
):
275 # Search without transitive match must return 0 results
276 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
278 expression
="member=cn=u1,%s" % self
.ou_users
)
279 self
.assertEqual(len(res1
), 0)
281 res1
= self
.ldb
.search("cn=u1,%s" % self
.ou_users
,
283 expression
="memberOf=cn=g4,%s" % self
.ou_groups
)
284 self
.assertEqual(len(res1
), 0)
286 # Search with transitive match must return 1 results
287 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
289 expression
="member:1.2.840.113556.1.4.1941:=cn=u1,%s" % self
.ou_users
)
290 self
.assertEqual(len(res1
), 1)
291 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g4,%s" % self
.ou_groups
).lower())
293 res1
= self
.ldb
.search("cn=u1,%s" % self
.ou_users
,
295 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s" % self
.ou_groups
)
296 self
.assertEqual(len(res1
), 1)
297 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=u1,%s" % self
.ou_users
).lower())
299 def test_g1_member_of_g4(self
):
300 # Search without transitive match must return 0 results
301 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
303 expression
="member=cn=g1,%s" % self
.ou_groups
)
304 self
.assertEqual(len(res1
), 0)
306 res1
= self
.ldb
.search("cn=g1,%s" % self
.ou_groups
,
308 expression
="memberOf=cn=g4,%s" % self
.ou_groups
)
309 self
.assertEqual(len(res1
), 0)
311 # Search with transitive match must return 1 results
312 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
314 expression
="member:1.2.840.113556.1.4.1941:=cn=g1,%s" % self
.ou_groups
)
315 self
.assertEqual(len(res1
), 1)
316 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g4,%s" % self
.ou_groups
).lower())
318 res1
= self
.ldb
.search("cn=g1,%s" % self
.ou_groups
,
320 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s" % self
.ou_groups
)
321 self
.assertEqual(len(res1
), 1)
322 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g1,%s" % self
.ou_groups
).lower())
324 def test_u1_groups(self
):
325 res1
= self
.ldb
.search(self
.ou_groups
,
327 expression
="member=cn=u1,%s" % self
.ou_users
)
328 self
.assertEqual(len(res1
), 1)
329 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g1,%s" % self
.ou_groups
).lower())
331 res1
= self
.ldb
.search(self
.ou_users
,
333 expression
="member=cn=u1,%s" % self
.ou_users
)
334 self
.assertEqual(len(res1
), 0)
336 res1
= self
.ldb
.search(self
.ou_groups
,
338 expression
="member:1.2.840.113556.1.4.1941:=cn=u1,%s" % self
.ou_users
)
339 self
.assertEqual(len(res1
), 4)
340 dn_list
= [str(res
.dn
).lower() for res
in res1
]
341 self
.assertTrue(("CN=g1,%s" % self
.ou_groups
).lower() in dn_list
)
342 self
.assertTrue(("CN=g2,%s" % self
.ou_groups
).lower() in dn_list
)
343 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
344 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
346 res1
= self
.ldb
.search(self
.ou_users
,
348 expression
="member:1.2.840.113556.1.4.1941:=cn=u1,%s" % self
.ou_users
)
349 self
.assertEqual(len(res1
), 0)
351 def test_u2_groups(self
):
352 res1
= self
.ldb
.search(self
.ou_groups
,
354 expression
="member=cn=u2,%s" % self
.ou_users
)
355 self
.assertEqual(len(res1
), 1)
356 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g2,%s" % self
.ou_groups
).lower())
358 res1
= self
.ldb
.search(self
.ou_users
,
360 expression
="member=cn=u2,%s" % self
.ou_users
)
361 self
.assertEqual(len(res1
), 0)
363 res1
= self
.ldb
.search(self
.ou_groups
,
365 expression
="member:1.2.840.113556.1.4.1941:=cn=u2,%s" % self
.ou_users
)
366 self
.assertEqual(len(res1
), 3)
367 dn_list
= [str(res
.dn
).lower() for res
in res1
]
368 self
.assertTrue(("CN=g2,%s" % self
.ou_groups
).lower() in dn_list
)
369 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
370 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
372 res1
= self
.ldb
.search(self
.ou_users
,
374 expression
="member:1.2.840.113556.1.4.1941:=cn=u2,%s" % self
.ou_users
)
375 self
.assertEqual(len(res1
), 0)
377 def test_u3_groups(self
):
378 res1
= self
.ldb
.search(self
.ou_groups
,
380 expression
="member=cn=u3,%s" % self
.ou_users
)
381 self
.assertEqual(len(res1
), 1)
382 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g3,%s" % self
.ou_groups
).lower())
384 res1
= self
.ldb
.search(self
.ou_users
,
386 expression
="member=cn=u3,%s" % self
.ou_users
)
387 self
.assertEqual(len(res1
), 0)
389 res1
= self
.ldb
.search(self
.ou_groups
,
391 expression
="member:1.2.840.113556.1.4.1941:=cn=u3,%s" % self
.ou_users
)
392 self
.assertEqual(len(res1
), 2)
393 dn_list
= [str(res
.dn
).lower() for res
in res1
]
394 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
395 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
397 res1
= self
.ldb
.search(self
.ou_users
,
399 expression
="member:1.2.840.113556.1.4.1941:=cn=u3,%s" % self
.ou_users
)
400 self
.assertEqual(len(res1
), 0)
402 def test_u4_groups(self
):
403 res1
= self
.ldb
.search(self
.ou_groups
,
405 expression
="member=cn=u4,%s" % self
.ou_users
)
406 self
.assertEqual(len(res1
), 1)
407 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g4,%s" % self
.ou_groups
).lower())
409 res1
= self
.ldb
.search(self
.ou_users
,
411 expression
="member=cn=u4,%s" % self
.ou_users
)
412 self
.assertEqual(len(res1
), 0)
414 res1
= self
.ldb
.search(self
.ou_groups
,
416 expression
="member:1.2.840.113556.1.4.1941:=cn=u4,%s" % self
.ou_users
)
417 self
.assertEqual(len(res1
), 1)
418 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g4,%s" % self
.ou_groups
).lower())
420 res1
= self
.ldb
.search(self
.ou_users
,
422 expression
="member:1.2.840.113556.1.4.1941:=cn=u4,%s" % self
.ou_users
)
423 self
.assertEqual(len(res1
), 0)
425 def test_extended_dn_u1(self
):
426 res1
= self
.ldb
.search("cn=u1,%s" % self
.ou_users
,
428 expression
="objectClass=*",
429 attrs
=['objectSid', 'objectGUID'])
430 self
.assertEqual(len(res1
), 1)
431 self
.assertEqual(str(res1
[0].dn
).lower(), ("cn=u1,%s" % self
.ou_users
).lower())
433 sid
= self
.ldb
.schema_format_value("objectSid", res1
[0]["objectSid"][0]).decode('utf8')
434 guid
= self
.ldb
.schema_format_value("objectGUID", res1
[0]['objectGUID'][0]).decode('utf8')
436 res1
= self
.ldb
.search(self
.ou_groups
,
438 expression
="member=<SID=%s>" % sid
)
439 self
.assertEqual(len(res1
), 1)
440 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g1,%s" % self
.ou_groups
).lower())
442 res1
= self
.ldb
.search(self
.ou_groups
,
444 expression
="member=<GUID=%s>" % guid
)
445 self
.assertEqual(len(res1
), 1)
446 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g1,%s" % self
.ou_groups
).lower())
448 res1
= self
.ldb
.search(self
.ou_groups
,
450 expression
="member:1.2.840.113556.1.4.1941:=<SID=%s>" % sid
)
451 self
.assertEqual(len(res1
), 4)
452 dn_list
= [str(res
.dn
).lower() for res
in res1
]
453 self
.assertTrue(("CN=g1,%s" % self
.ou_groups
).lower() in dn_list
)
454 self
.assertTrue(("CN=g2,%s" % self
.ou_groups
).lower() in dn_list
)
455 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
456 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
458 res1
= self
.ldb
.search(self
.ou_groups
,
459 scope
=SCOPE_ONELEVEL
,
460 expression
="member:1.2.840.113556.1.4.1941:=<SID=%s>" % sid
)
461 self
.assertEqual(len(res1
), 4)
462 dn_list
= [str(res
.dn
).lower() for res
in res1
]
463 self
.assertTrue(("CN=g1,%s" % self
.ou_groups
).lower() in dn_list
)
464 self
.assertTrue(("CN=g2,%s" % self
.ou_groups
).lower() in dn_list
)
465 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
466 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
468 res1
= self
.ldb
.search(self
.ou_groups
,
470 expression
="member:1.2.840.113556.1.4.1941:=<GUID=%s>" % guid
)
471 self
.assertEqual(len(res1
), 4)
472 dn_list
= [str(res
.dn
).lower() for res
in res1
]
473 self
.assertTrue(("CN=g1,%s" % self
.ou_groups
).lower() in dn_list
)
474 self
.assertTrue(("CN=g2,%s" % self
.ou_groups
).lower() in dn_list
)
475 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
476 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
478 res1
= self
.ldb
.search(self
.ou_groups
,
479 scope
=SCOPE_ONELEVEL
,
480 expression
="member:1.2.840.113556.1.4.1941:=<GUID=%s>" % guid
)
481 self
.assertEqual(len(res1
), 4)
482 dn_list
= [str(res
.dn
).lower() for res
in res1
]
483 self
.assertTrue(("CN=g1,%s" % self
.ou_groups
).lower() in dn_list
)
484 self
.assertTrue(("CN=g2,%s" % self
.ou_groups
).lower() in dn_list
)
485 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
486 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
488 def test_extended_dn_u2(self
):
489 res1
= self
.ldb
.search("cn=u2,%s" % self
.ou_users
,
491 expression
="objectClass=*",
492 attrs
=['objectSid', 'objectGUID'])
493 self
.assertEqual(len(res1
), 1)
494 self
.assertEqual(str(res1
[0].dn
).lower(), ("cn=u2,%s" % self
.ou_users
).lower())
496 sid
= self
.ldb
.schema_format_value("objectSid", res1
[0]["objectSid"][0]).decode('utf8')
497 guid
= self
.ldb
.schema_format_value("objectGUID", res1
[0]['objectGUID'][0]).decode('utf8')
499 res1
= self
.ldb
.search(self
.ou_groups
,
501 expression
="member=<SID=%s>" % sid
)
502 self
.assertEqual(len(res1
), 1)
503 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g2,%s" % self
.ou_groups
).lower())
505 res1
= self
.ldb
.search(self
.ou_groups
,
507 expression
="member=<GUID=%s>" % guid
)
508 self
.assertEqual(len(res1
), 1)
509 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g2,%s" % self
.ou_groups
).lower())
511 res1
= self
.ldb
.search(self
.ou_groups
,
513 expression
="member:1.2.840.113556.1.4.1941:=<SID=%s>" % sid
)
514 self
.assertEqual(len(res1
), 3)
515 dn_list
= [str(res
.dn
).lower() for res
in res1
]
516 self
.assertTrue(("CN=g2,%s" % self
.ou_groups
).lower() in dn_list
)
517 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
518 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
520 res1
= self
.ldb
.search(self
.ou_groups
,
521 scope
=SCOPE_ONELEVEL
,
522 expression
="member:1.2.840.113556.1.4.1941:=<SID=%s>" % sid
)
523 self
.assertEqual(len(res1
), 3)
524 dn_list
= [str(res
.dn
).lower() for res
in res1
]
525 self
.assertTrue(("CN=g2,%s" % self
.ou_groups
).lower() in dn_list
)
526 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
527 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
529 res1
= self
.ldb
.search(self
.ou_groups
,
531 expression
="member:1.2.840.113556.1.4.1941:=<GUID=%s>" % guid
)
532 self
.assertEqual(len(res1
), 3)
533 dn_list
= [str(res
.dn
).lower() for res
in res1
]
534 self
.assertTrue(("CN=g2,%s" % self
.ou_groups
).lower() in dn_list
)
535 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
536 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
538 res1
= self
.ldb
.search(self
.ou_groups
,
539 scope
=SCOPE_ONELEVEL
,
540 expression
="member:1.2.840.113556.1.4.1941:=<GUID=%s>" % guid
)
541 self
.assertEqual(len(res1
), 3)
542 dn_list
= [str(res
.dn
).lower() for res
in res1
]
543 self
.assertTrue(("CN=g2,%s" % self
.ou_groups
).lower() in dn_list
)
544 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
545 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
547 def test_extended_dn_u3(self
):
548 res1
= self
.ldb
.search("cn=u3,%s" % self
.ou_users
,
550 expression
="objectClass=*",
551 attrs
=['objectSid', 'objectGUID'])
552 self
.assertEqual(len(res1
), 1)
553 self
.assertEqual(str(res1
[0].dn
).lower(), ("cn=u3,%s" % self
.ou_users
).lower())
555 sid
= self
.ldb
.schema_format_value("objectSid", res1
[0]["objectSid"][0]).decode('utf8')
556 guid
= self
.ldb
.schema_format_value("objectGUID", res1
[0]['objectGUID'][0]).decode('utf8')
558 res1
= self
.ldb
.search(self
.ou_groups
,
560 expression
="member=<SID=%s>" % sid
)
561 self
.assertEqual(len(res1
), 1)
562 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g3,%s" % self
.ou_groups
).lower())
564 res1
= self
.ldb
.search(self
.ou_groups
,
566 expression
="member=<GUID=%s>" % guid
)
567 self
.assertEqual(len(res1
), 1)
568 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g3,%s" % self
.ou_groups
).lower())
570 res1
= self
.ldb
.search(self
.ou_groups
,
572 expression
="member:1.2.840.113556.1.4.1941:=<SID=%s>" % sid
)
573 self
.assertEqual(len(res1
), 2)
574 dn_list
= [str(res
.dn
).lower() for res
in res1
]
575 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
576 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
578 res1
= self
.ldb
.search(self
.ou_groups
,
579 scope
=SCOPE_ONELEVEL
,
580 expression
="member:1.2.840.113556.1.4.1941:=<SID=%s>" % sid
)
581 self
.assertEqual(len(res1
), 2)
582 dn_list
= [str(res
.dn
).lower() for res
in res1
]
583 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
584 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
586 res1
= self
.ldb
.search(self
.ou_groups
,
588 expression
="member:1.2.840.113556.1.4.1941:=<GUID=%s>" % guid
)
589 self
.assertEqual(len(res1
), 2)
590 dn_list
= [str(res
.dn
).lower() for res
in res1
]
591 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
592 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
594 res1
= self
.ldb
.search(self
.ou_groups
,
595 scope
=SCOPE_ONELEVEL
,
596 expression
="member:1.2.840.113556.1.4.1941:=<GUID=%s>" % guid
)
597 self
.assertEqual(len(res1
), 2)
598 dn_list
= [str(res
.dn
).lower() for res
in res1
]
599 self
.assertTrue(("CN=g3,%s" % self
.ou_groups
).lower() in dn_list
)
600 self
.assertTrue(("CN=g4,%s" % self
.ou_groups
).lower() in dn_list
)
602 def test_extended_dn_u4(self
):
603 res1
= self
.ldb
.search("cn=u4,%s" % self
.ou_users
,
605 expression
="objectClass=*",
606 attrs
=['objectSid', 'objectGUID'])
607 self
.assertEqual(len(res1
), 1)
608 self
.assertEqual(str(res1
[0].dn
).lower(), ("cn=u4,%s" % self
.ou_users
).lower())
610 sid
= self
.ldb
.schema_format_value("objectSid", res1
[0]["objectSid"][0]).decode('utf8')
611 guid
= self
.ldb
.schema_format_value("objectGUID", res1
[0]['objectGUID'][0]).decode('utf8')
613 res1
= self
.ldb
.search(self
.ou_groups
,
615 expression
="member=<SID=%s>" % sid
)
616 self
.assertEqual(len(res1
), 1)
617 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g4,%s" % self
.ou_groups
).lower())
619 res1
= self
.ldb
.search(self
.ou_groups
,
621 expression
="member=<GUID=%s>" % guid
)
622 self
.assertEqual(len(res1
), 1)
623 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g4,%s" % self
.ou_groups
).lower())
625 res1
= self
.ldb
.search(self
.ou_groups
,
626 scope
=SCOPE_ONELEVEL
,
627 expression
="member=<GUID=%s>" % guid
)
628 self
.assertEqual(len(res1
), 1)
629 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g4,%s" % self
.ou_groups
).lower())
631 res1
= self
.ldb
.search(self
.ou_groups
,
633 expression
="member:1.2.840.113556.1.4.1941:=<SID=%s>" % sid
)
634 self
.assertEqual(len(res1
), 1)
635 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g4,%s" % self
.ou_groups
).lower())
637 res1
= self
.ldb
.search(self
.ou_groups
,
638 scope
=SCOPE_ONELEVEL
,
639 expression
="member:1.2.840.113556.1.4.1941:=<SID=%s>" % sid
)
640 self
.assertEqual(len(res1
), 1)
641 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g4,%s" % self
.ou_groups
).lower())
643 res1
= self
.ldb
.search(self
.ou_groups
,
645 expression
="member:1.2.840.113556.1.4.1941:=<GUID=%s>" % guid
)
646 self
.assertEqual(len(res1
), 1)
647 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g4,%s" % self
.ou_groups
).lower())
649 res1
= self
.ldb
.search(self
.ou_groups
,
650 scope
=SCOPE_ONELEVEL
,
651 expression
="member:1.2.840.113556.1.4.1941:=<GUID=%s>" % guid
)
652 self
.assertEqual(len(res1
), 1)
653 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=g4,%s" % self
.ou_groups
).lower())
655 def test_object_dn_binary(self
):
656 res1
= self
.ldb
.search(self
.ou_computers
,
658 expression
="msDS-RevealedUsers=B:8:01010101:cn=c3,%s" % self
.ou_computers
)
659 self
.assertEqual(len(res1
), 1)
660 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=c2,%s" % self
.ou_computers
).lower())
662 res1
= self
.ldb
.search(self
.ou_computers
,
663 scope
=SCOPE_ONELEVEL
,
664 expression
="msDS-RevealedUsers=B:8:01010101:cn=c3,%s" % self
.ou_computers
)
665 self
.assertEqual(len(res1
), 1)
666 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=c2,%s" % self
.ou_computers
).lower())
668 res1
= self
.ldb
.search(self
.ou_computers
,
670 expression
="msDS-RevealedUsers:1.2.840.113556.1.4.1941:=B:8:01010101:cn=c3,%s" % self
.ou_computers
)
671 self
.assertEqual(len(res1
), 2)
672 dn_list
= [str(res
.dn
).lower() for res
in res1
]
673 self
.assertTrue(("CN=c1,%s" % self
.ou_computers
).lower() in dn_list
)
674 self
.assertTrue(("CN=c2,%s" % self
.ou_computers
).lower() in dn_list
)
676 res1
= self
.ldb
.search(self
.ou_computers
,
677 scope
=SCOPE_ONELEVEL
,
678 expression
="msDS-RevealedUsers:1.2.840.113556.1.4.1941:=B:8:01010101:cn=c3,%s" % self
.ou_computers
)
679 self
.assertEqual(len(res1
), 2)
680 dn_list
= [str(res
.dn
).lower() for res
in res1
]
681 self
.assertTrue(("CN=c1,%s" % self
.ou_computers
).lower() in dn_list
)
682 self
.assertTrue(("CN=c2,%s" % self
.ou_computers
).lower() in dn_list
)
684 def test_one_way_links(self
):
685 res1
= self
.ldb
.search(self
.ou
,
687 expression
="addressBookRoots2=cn=c1,%s" % self
.ou_computers
)
688 self
.assertEqual(len(res1
), 1)
689 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=e2,%s" % self
.ou
).lower())
691 res1
= self
.ldb
.search(self
.ou
,
692 scope
=SCOPE_ONELEVEL
,
693 expression
="addressBookRoots2=cn=c1,%s" % self
.ou_computers
)
694 self
.assertEqual(len(res1
), 1)
695 self
.assertEqual(str(res1
[0].dn
).lower(), ("CN=e2,%s" % self
.ou
).lower())
697 res1
= self
.ldb
.search(self
.ou
,
699 expression
="addressBookRoots2:1.2.840.113556.1.4.1941:=cn=c1,%s" % self
.ou_computers
)
700 self
.assertEqual(len(res1
), 2)
701 dn_list
= [str(res
.dn
).lower() for res
in res1
]
702 self
.assertTrue(("CN=e1,%s" % self
.ou
).lower() in dn_list
)
703 self
.assertTrue(("CN=e2,%s" % self
.ou
).lower() in dn_list
)
705 res1
= self
.ldb
.search(self
.ou
,
706 scope
=SCOPE_ONELEVEL
,
707 expression
="addressBookRoots2:1.2.840.113556.1.4.1941:=cn=c1,%s" % self
.ou_computers
)
708 self
.assertEqual(len(res1
), 2)
709 dn_list
= [str(res
.dn
).lower() for res
in res1
]
710 self
.assertTrue(("CN=e1,%s" % self
.ou
).lower() in dn_list
)
711 self
.assertTrue(("CN=e2,%s" % self
.ou
).lower() in dn_list
)
713 def test_not_linked_attrs(self
):
714 res1
= self
.ldb
.search(self
.base_dn
,
716 expression
="wellKnownObjects=B:32:aa312825768811d1aded00c04fd8d5cd:CN=computers,%s" % self
.base_dn
)
717 self
.assertEqual(len(res1
), 1)
718 self
.assertEqual(str(res1
[0].dn
).lower(), self
.base_dn
.lower())
720 def test_invalid_basedn(self
):
721 res1
= self
.ldb
.search(self
.base_dn
,
723 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=c1,ou=computers,ou=matchrulestest,%sXX" % self
.base_dn
)
724 self
.assertEqual(len(res1
), 0)
726 res1
= self
.ldb
.search(self
.base_dn
,
728 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=XX,ou=computers,ou=matchrulestest,%s" % self
.base_dn
)
729 self
.assertEqual(len(res1
), 0)
731 def test_subtree(self
):
732 res1
= self
.ldb
.search(self
.ou
,
734 expression
="otherWellKnownObjects=B:32:00000000000000000000000000000004:OU=o4,OU=o3,OU=o2,OU=o1,%s" % self
.ou
)
735 self
.assertEqual(len(res1
), 1)
736 self
.assertEqual(str(res1
[0].dn
).lower(), ("OU=o3,OU=o2,OU=o1,%s" % self
.ou
).lower())
738 res1
= self
.ldb
.search(self
.ou
,
739 scope
=SCOPE_ONELEVEL
,
740 expression
="otherWellKnownObjects=B:32:00000000000000000000000000000004:OU=o4,OU=o3,OU=o2,OU=o1,%s" % self
.ou
)
741 self
.assertEqual(len(res1
), 0)
743 res1
= self
.ldb
.search(self
.ou
,
745 expression
="otherWellKnownObjects:1.2.840.113556.1.4.1941:=B:32:00000000000000000000000000000004:OU=o4,OU=o3,OU=o2,OU=o1,%s" % self
.ou
)
746 self
.assertEqual(len(res1
), 0)
748 res1
= self
.ldb
.search(self
.ou
,
749 scope
=SCOPE_ONELEVEL
,
750 expression
="otherWellKnownObjects:1.2.840.113556.1.4.1941:=B:32:00000000000000000000000000000004:OU=o4,OU=o3,OU=o2,OU=o1,%s" % self
.ou
)
751 self
.assertEqual(len(res1
), 0)
753 def test_unknown_oid(self
):
754 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
756 expression
="member:2.4.681.226012.2.8.3882:=cn=u1,%s" % self
.ou_users
)
757 self
.assertEqual(len(res1
), 0)
759 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
761 expression
="member:8.16.8720.1008448.8.32.15528:=cn=u1,%s" % self
.ou_users
)
762 self
.assertEqual(len(res1
), 0)
764 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
766 expression
="member:1.2.3.4:=cn=u1,%s" % self
.ou_users
)
767 self
.assertEqual(len(res1
), 0)
769 def test_nul_text(self
):
770 self
.assertRaises((ValueError,TypeError),
771 lambda: self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
773 expression
="\00member:1.2.840.113556.1.4.1941:=cn=u1,%s" % self
.ou_users
))
774 self
.assertRaises((ValueError,TypeError),
775 lambda: self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
777 expression
="member:1.2.840\00.113556.1.4.1941:=cn=u1,%s" % self
.ou_users
))
778 self
.assertRaises((ValueError,TypeError),
779 lambda: self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
781 expression
="member:1.2.840.113556.1.4.1941:=cn=u1\00,%s" % self
.ou_users
))
782 self
.assertRaises(LdbError
,
783 lambda: self
.ldb
.search("cn=\00g4,%s" % self
.ou_groups
,
785 expression
="member:1.2.840.113556.1.4.1941:=cn=u1,%s" % self
.ou_users
))
786 self
.assertRaises(LdbError
,
787 lambda: self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
789 expression
="member:1.2.840.113556.1.4.1941:"))
790 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
792 expression
="member:1.2.840.113556.1.4.1941:=")
793 self
.assertEqual(len(res1
), 0)
794 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
796 expression
="member=")
797 self
.assertEqual(len(res1
), 0)
798 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
800 expression
="member:1.2.840.113556.1.4.1941:=nonexistent")
801 self
.assertEqual(len(res1
), 0)
802 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
804 expression
="member=nonexistent")
805 self
.assertEqual(len(res1
), 0)
806 self
.assertRaises(LdbError
,
807 lambda: self
.ldb
.search("cn=\00g4,%s" % self
.ou_groups
,
809 expression
="member:1.2.840.113556.1.4.1941:cn=u1,%s" % self
.ou_users
))
810 self
.assertRaises(LdbError
,
811 lambda: self
.ldb
.search("cn=\00g4,%s" % self
.ou_groups
,
813 expression
="member:1.2.840.113556.1.4.1941:=cn=u1"))
814 self
.assertRaises(LdbError
,
815 lambda: self
.ldb
.search("cn=\00g4,%s" % self
.ou_groups
,
817 expression
="member:1.2.840.113556.1.4.1941:=cn="))
818 self
.assertRaises(LdbError
,
819 lambda: self
.ldb
.search("cn=\00g4,%s" % self
.ou_groups
,
821 expression
="member::=cn=u1,%s" % self
.ou_users
))
823 def test_misc_matches(self
):
824 res1
= self
.ldb
.search(self
.ou_groups
,
826 expression
="member=cn=g1,%s" % self
.ou_groups
)
827 self
.assertEqual(len(res1
), 0)
829 res1
= self
.ldb
.search("cn=g1,%s" % self
.ou_groups
,
831 expression
="member=cn=g1,%s" % self
.ou_groups
)
832 self
.assertEqual(len(res1
), 0)
834 res1
= self
.ldb
.search(self
.ou_groups
,
836 expression
="member=cn=g1,%s" % self
.ou_groups
)
837 self
.assertEqual(len(res1
), 1)
838 self
.assertEqual(str(res1
[0].dn
), "CN=g2,%s" % self
.ou_groups
)
840 res1
= self
.ldb
.search(self
.ou_groups
,
841 scope
=SCOPE_ONELEVEL
,
842 expression
="member=cn=g1,%s" % self
.ou_groups
)
843 self
.assertEqual(len(res1
), 1)
844 self
.assertEqual(str(res1
[0].dn
), "CN=g2,%s" % self
.ou_groups
)
846 res1
= self
.ldb
.search(self
.ou_groups
,
848 expression
="member:1.2.840.113556.1.4.1941:=cn=g1,%s" % self
.ou_groups
)
849 self
.assertEqual(len(res1
), 0)
851 res1
= self
.ldb
.search("cn=g1,%s" % self
.ou_groups
,
853 expression
="member:1.2.840.113556.1.4.1941:=cn=g1,%s" % self
.ou_groups
)
854 self
.assertEqual(len(res1
), 0)
856 res1
= self
.ldb
.search(self
.ou_groups
,
858 expression
="member:1.2.840.113556.1.4.1941:=cn=g1,%s" % self
.ou_groups
)
859 self
.assertEqual(len(res1
), 3)
860 dn_list
= [str(res
.dn
) for res
in res1
]
861 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
862 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
863 self
.assertTrue("CN=g4,%s" % self
.ou_groups
in dn_list
)
865 res1
= self
.ldb
.search(self
.ou_groups
,
866 scope
=SCOPE_ONELEVEL
,
867 expression
="member:1.2.840.113556.1.4.1941:=cn=g1,%s" % self
.ou_groups
)
868 self
.assertEqual(len(res1
), 3)
869 dn_list
= [str(res
.dn
) for res
in res1
]
870 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
871 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
872 self
.assertTrue("CN=g4,%s" % self
.ou_groups
in dn_list
)
874 res1
= self
.ldb
.search(self
.ou_groups
,
876 expression
="member:1.2.840.113556.1.4.1941:=cn=g4,%s" % self
.ou_groups
)
877 self
.assertEqual(len(res1
), 0)
879 res1
= self
.ldb
.search(self
.ou_groups
,
880 scope
=SCOPE_ONELEVEL
,
881 expression
="member:1.2.840.113556.1.4.1941:=cn=g4,%s" % self
.ou_groups
)
882 self
.assertEqual(len(res1
), 0)
884 res1
= self
.ldb
.search(self
.ou_groups
,
886 expression
="memberOf=cn=g4,%s" % self
.ou_groups
)
887 self
.assertEqual(len(res1
), 0)
889 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
891 expression
="memberOf=cn=g4,%s" % self
.ou_groups
)
892 self
.assertEqual(len(res1
), 0)
894 res1
= self
.ldb
.search(self
.ou_groups
,
896 expression
="memberOf=cn=g4,%s" % self
.ou_groups
)
897 self
.assertEqual(len(res1
), 1)
898 self
.assertEqual(str(res1
[0].dn
), ("CN=g3,%s" % self
.ou_groups
))
900 res1
= self
.ldb
.search(self
.ou_groups
,
901 scope
=SCOPE_ONELEVEL
,
902 expression
="memberOf=cn=g4,%s" % self
.ou_groups
)
903 self
.assertEqual(len(res1
), 1)
904 self
.assertEqual(str(res1
[0].dn
), ("CN=g3,%s" % self
.ou_groups
))
906 res1
= self
.ldb
.search(self
.ou_groups
,
908 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s" % self
.ou_groups
)
909 self
.assertEqual(len(res1
), 0)
911 res1
= self
.ldb
.search("cn=g4,%s" % self
.ou_groups
,
913 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s" % self
.ou_groups
)
914 self
.assertEqual(len(res1
), 0)
916 res1
= self
.ldb
.search(self
.ou_groups
,
918 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s" % self
.ou_groups
)
919 self
.assertEqual(len(res1
), 3)
920 dn_list
= [str(res
.dn
) for res
in res1
]
921 self
.assertTrue("CN=g1,%s" % self
.ou_groups
in dn_list
)
922 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
923 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
925 res1
= self
.ldb
.search(self
.ou_groups
,
926 scope
=SCOPE_ONELEVEL
,
927 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s" % self
.ou_groups
)
928 self
.assertEqual(len(res1
), 3)
929 dn_list
= [str(res
.dn
) for res
in res1
]
930 self
.assertTrue("CN=g1,%s" % self
.ou_groups
in dn_list
)
931 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
932 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
934 res1
= self
.ldb
.search(self
.ou_groups
,
936 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=g1,%s" % self
.ou_groups
)
937 self
.assertEqual(len(res1
), 0)
939 res1
= self
.ldb
.search(self
.ou_groups
,
941 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=g1,%s" % self
.ou_groups
)
942 self
.assertEqual(len(res1
), 0)
945 class MatchRuleConditionTests(samba
.tests
.TestCase
):
947 super(MatchRuleConditionTests
, self
).setUp()
949 self
.ldb
= SamDB(host
, credentials
=creds
, session_info
=system_session(lp
), lp
=lp
)
950 self
.base_dn
= self
.ldb
.domain_dn()
951 self
.ou
= "OU=matchruleconditiontests,%s" % self
.base_dn
952 self
.ou_users
= "OU=users,%s" % self
.ou
953 self
.ou_groups
= "OU=groups,%s" % self
.ou
954 self
.ou_computers
= "OU=computers,%s" % self
.ou
956 # Add a organizational unit to create objects
959 "objectclass": "organizationalUnit"})
961 # Create users, groups, and computers
964 "objectclass": "organizationalUnit"})
966 "dn": self
.ou_groups
,
967 "objectclass": "organizationalUnit"})
969 "dn": self
.ou_computers
,
970 "objectclass": "organizationalUnit"})
973 "dn": "cn=g1,%s" % self
.ou_groups
,
974 "objectclass": "group"})
976 "dn": "cn=g2,%s" % self
.ou_groups
,
977 "objectclass": "group"})
979 "dn": "cn=g3,%s" % self
.ou_groups
,
980 "objectclass": "group"})
982 "dn": "cn=g4,%s" % self
.ou_groups
,
983 "objectclass": "group"})
986 "dn": "cn=u1,%s" % self
.ou_users
,
987 "objectclass": "group"})
989 "dn": "cn=u2,%s" % self
.ou_users
,
990 "objectclass": "group"})
992 "dn": "cn=u3,%s" % self
.ou_users
,
993 "objectclass": "group"})
995 "dn": "cn=u4,%s" % self
.ou_users
,
996 "objectclass": "group"})
999 "dn": "cn=c1,%s" % self
.ou_computers
,
1000 "objectclass": "user"})
1003 "dn": "cn=c2,%s" % self
.ou_computers
,
1004 "objectclass": "user"})
1007 "dn": "cn=c3,%s" % self
.ou_computers
,
1008 "objectclass": "user"})
1011 "dn": "cn=c4,%s" % self
.ou_computers
,
1012 "objectclass": "user"})
1014 # Assign groups according to the following structure:
1015 # g1-->g2---->g3 --g4
1017 # u1- >u2-- | u3<- | u4
1020 # *c1 is a member of u1, u2, u3, and u4
1022 # u2 is a member of g1 and g2
1024 m
.dn
= Dn(self
.ldb
, "CN=g1,%s" % self
.ou_groups
)
1025 m
["member"] = MessageElement("CN=u2,%s" % self
.ou_users
,
1026 FLAG_MOD_ADD
, "member")
1030 m
.dn
= Dn(self
.ldb
, "CN=g2,%s" % self
.ou_groups
)
1031 m
["member"] = MessageElement("CN=u2,%s" % self
.ou_users
,
1032 FLAG_MOD_ADD
, "member")
1035 # g2 is a member of g1
1037 m
.dn
= Dn(self
.ldb
, "CN=g1,%s" % self
.ou_groups
)
1038 m
["member"] = MessageElement("CN=g2,%s" % self
.ou_groups
,
1039 FLAG_MOD_ADD
, "member")
1042 # g3 is a member of g2
1044 m
.dn
= Dn(self
.ldb
, "CN=g2,%s" % self
.ou_groups
)
1045 m
["member"] = MessageElement("CN=g3,%s" % self
.ou_groups
,
1046 FLAG_MOD_ADD
, "member")
1049 # u3 is a member of g3 and g4
1051 m
.dn
= Dn(self
.ldb
, "CN=g3,%s" % self
.ou_groups
)
1052 m
["member"] = MessageElement("CN=u3,%s" % self
.ou_users
,
1053 FLAG_MOD_ADD
, "member")
1057 m
.dn
= Dn(self
.ldb
, "CN=g4,%s" % self
.ou_groups
)
1058 m
["member"] = MessageElement("CN=u3,%s" % self
.ou_users
,
1059 FLAG_MOD_ADD
, "member")
1062 # u4 is a member of g4
1064 m
.dn
= Dn(self
.ldb
, "CN=g4,%s" % self
.ou_groups
)
1065 m
["member"] = MessageElement("CN=u4,%s" % self
.ou_users
,
1066 FLAG_MOD_ADD
, "member")
1069 # c1 is a member of u1, u2, u3, and u4
1071 m
.dn
= Dn(self
.ldb
, "CN=u1,%s" % self
.ou_users
)
1072 m
["member"] = MessageElement("CN=c1,%s" % self
.ou_computers
,
1073 FLAG_MOD_ADD
, "member")
1077 m
.dn
= Dn(self
.ldb
, "CN=u2,%s" % self
.ou_users
)
1078 m
["member"] = MessageElement("CN=c1,%s" % self
.ou_computers
,
1079 FLAG_MOD_ADD
, "member")
1083 m
.dn
= Dn(self
.ldb
, "CN=u3,%s" % self
.ou_users
)
1084 m
["member"] = MessageElement("CN=c1,%s" % self
.ou_computers
,
1085 FLAG_MOD_ADD
, "member")
1089 m
.dn
= Dn(self
.ldb
, "CN=u4,%s" % self
.ou_users
)
1090 m
["member"] = MessageElement("CN=c1,%s" % self
.ou_computers
,
1091 FLAG_MOD_ADD
, "member")
1094 # c2 is a member of u1
1096 m
.dn
= Dn(self
.ldb
, "CN=u1,%s" % self
.ou_users
)
1097 m
["member"] = MessageElement("CN=c2,%s" % self
.ou_computers
,
1098 FLAG_MOD_ADD
, "member")
1101 # c3 is a member of u2 and g3
1103 m
.dn
= Dn(self
.ldb
, "CN=u2,%s" % self
.ou_users
)
1104 m
["member"] = MessageElement("CN=c3,%s" % self
.ou_computers
,
1105 FLAG_MOD_ADD
, "member")
1109 m
.dn
= Dn(self
.ldb
, "CN=g3,%s" % self
.ou_groups
)
1110 m
["member"] = MessageElement("CN=c3,%s" % self
.ou_computers
,
1111 FLAG_MOD_ADD
, "member")
1114 # c4 is a member of u4 and g4
1116 m
.dn
= Dn(self
.ldb
, "CN=u4,%s" % self
.ou_users
)
1117 m
["member"] = MessageElement("CN=c4,%s" % self
.ou_computers
,
1118 FLAG_MOD_ADD
, "member")
1122 m
.dn
= Dn(self
.ldb
, "CN=g4,%s" % self
.ou_groups
)
1123 m
["member"] = MessageElement("CN=c4,%s" % self
.ou_computers
,
1124 FLAG_MOD_ADD
, "member")
1128 super(MatchRuleConditionTests
, self
).tearDown()
1129 self
.ldb
.delete(self
.ou
, controls
=['tree_delete:0'])
1131 def test_g1_members(self
):
1132 res1
= self
.ldb
.search(self
.ou
,
1133 scope
=SCOPE_SUBTREE
,
1134 expression
="memberOf=cn=g1,%s" % self
.ou_groups
)
1135 self
.assertEquals(len(res1
), 2)
1136 dn_list
= [str(res
.dn
) for res
in res1
]
1137 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1138 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1140 res1
= self
.ldb
.search(self
.ou
,
1141 scope
=SCOPE_SUBTREE
,
1142 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=g1,%s" % self
.ou_groups
)
1143 self
.assertEquals(len(res1
), 6)
1144 dn_list
= [str(res
.dn
) for res
in res1
]
1145 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1146 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1147 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1148 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1149 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1150 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1152 res1
= self
.ldb
.search(self
.ou
,
1153 scope
=SCOPE_SUBTREE
,
1154 expression
="member=cn=g1,%s" % self
.ou_groups
)
1155 self
.assertEquals(len(res1
), 0)
1157 res1
= self
.ldb
.search(self
.ou
,
1158 scope
=SCOPE_SUBTREE
,
1159 expression
="member:1.2.840.113556.1.4.1941:=cn=g1,%s" % self
.ou_groups
)
1160 self
.assertEquals(len(res1
), 0)
1162 def test_g2_members(self
):
1163 res1
= self
.ldb
.search(self
.ou
,
1164 scope
=SCOPE_SUBTREE
,
1165 expression
="memberOf=cn=g2,%s" % self
.ou_groups
)
1166 self
.assertEquals(len(res1
), 2)
1167 dn_list
= [str(res
.dn
) for res
in res1
]
1168 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1169 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1171 res1
= self
.ldb
.search(self
.ou
,
1172 scope
=SCOPE_SUBTREE
,
1173 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=g2,%s" % self
.ou_groups
)
1174 self
.assertEquals(len(res1
), 5)
1175 dn_list
= [str(res
.dn
) for res
in res1
]
1176 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1177 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1178 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1179 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1180 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1182 res1
= self
.ldb
.search(self
.ou
,
1183 scope
=SCOPE_SUBTREE
,
1184 expression
="member=cn=g2,%s" % self
.ou_groups
)
1185 self
.assertEquals(len(res1
), 1)
1186 self
.assertEquals(str(res1
[0].dn
), "CN=g1,%s" % self
.ou_groups
)
1188 res1
= self
.ldb
.search(self
.ou
,
1189 scope
=SCOPE_SUBTREE
,
1190 expression
="member:1.2.840.113556.1.4.1941:=cn=g2,%s" % self
.ou_groups
)
1191 self
.assertEquals(len(res1
), 1)
1192 self
.assertEquals(str(res1
[0].dn
), "CN=g1,%s" % self
.ou_groups
)
1194 def test_g3_members(self
):
1195 res1
= self
.ldb
.search(self
.ou
,
1196 scope
=SCOPE_SUBTREE
,
1197 expression
="memberOf=cn=g3,%s" % self
.ou_groups
)
1198 self
.assertEquals(len(res1
), 2)
1199 dn_list
= [str(res
.dn
) for res
in res1
]
1200 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1201 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1203 res1
= self
.ldb
.search(self
.ou
,
1204 scope
=SCOPE_SUBTREE
,
1205 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=g3,%s" % self
.ou_groups
)
1206 self
.assertEquals(len(res1
), 3)
1207 dn_list
= [str(res
.dn
) for res
in res1
]
1208 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1209 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1210 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1212 res1
= self
.ldb
.search(self
.ou
,
1213 scope
=SCOPE_SUBTREE
,
1214 expression
="member=cn=g3,%s" % self
.ou_groups
)
1215 self
.assertEquals(len(res1
), 1)
1216 self
.assertEquals(str(res1
[0].dn
), "CN=g2,%s" % self
.ou_groups
)
1218 res1
= self
.ldb
.search(self
.ou
,
1219 scope
=SCOPE_SUBTREE
,
1220 expression
="member:1.2.840.113556.1.4.1941:=cn=g3,%s" % self
.ou_groups
)
1221 self
.assertEquals(len(res1
), 2)
1222 dn_list
= [str(res
.dn
) for res
in res1
]
1223 self
.assertTrue("CN=g1,%s" % self
.ou_groups
in dn_list
)
1224 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1226 def test_g4_members(self
):
1227 res1
= self
.ldb
.search(self
.ou
,
1228 scope
=SCOPE_SUBTREE
,
1229 expression
="memberOf=cn=g4,%s" % self
.ou_groups
)
1230 self
.assertEquals(len(res1
), 3)
1231 dn_list
= [str(res
.dn
) for res
in res1
]
1232 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1233 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1234 self
.assertTrue("CN=c4,%s" % self
.ou_computers
in dn_list
)
1236 res1
= self
.ldb
.search(self
.ou
,
1237 scope
=SCOPE_SUBTREE
,
1238 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s" % self
.ou_groups
)
1239 self
.assertEquals(len(res1
), 4)
1240 dn_list
= [str(res
.dn
) for res
in res1
]
1241 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1242 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1243 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1244 self
.assertTrue("CN=c4,%s" % self
.ou_computers
in dn_list
)
1246 res1
= self
.ldb
.search(self
.ou
,
1247 scope
=SCOPE_SUBTREE
,
1248 expression
="member=cn=g4,%s" % self
.ou_groups
)
1249 self
.assertEquals(len(res1
), 0)
1251 res1
= self
.ldb
.search(self
.ou
,
1252 scope
=SCOPE_SUBTREE
,
1253 expression
="member:1.2.840.113556.1.4.1941:=cn=g4,%s" % self
.ou_groups
)
1254 self
.assertEquals(len(res1
), 0)
1256 def test_u1_members(self
):
1257 res1
= self
.ldb
.search(self
.ou
,
1258 scope
=SCOPE_SUBTREE
,
1259 expression
="memberOf=cn=u1,%s" % self
.ou_users
)
1260 self
.assertEqual(len(res1
), 2)
1261 dn_list
= [str(res
.dn
) for res
in res1
]
1262 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1263 self
.assertTrue("CN=c2,%s" % self
.ou_computers
in dn_list
)
1265 res1
= self
.ldb
.search(self
.ou
,
1266 scope
=SCOPE_SUBTREE
,
1267 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=u1,%s" % self
.ou_users
)
1268 self
.assertEqual(len(res1
), 2)
1269 dn_list
= [str(res
.dn
) for res
in res1
]
1270 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1271 self
.assertTrue("CN=c2,%s" % self
.ou_computers
in dn_list
)
1273 res1
= self
.ldb
.search(self
.ou
,
1274 scope
=SCOPE_SUBTREE
,
1275 expression
="member=cn=u1,%s" % self
.ou_users
)
1276 self
.assertEqual(len(res1
), 0)
1278 res1
= self
.ldb
.search(self
.ou
,
1279 scope
=SCOPE_SUBTREE
,
1280 expression
="member:1.2.840.113556.1.4.1941:=cn=u1,%s" % self
.ou_users
)
1281 self
.assertEqual(len(res1
), 0)
1283 def test_u2_members(self
):
1284 res1
= self
.ldb
.search(self
.ou
,
1285 scope
=SCOPE_SUBTREE
,
1286 expression
="memberOf=cn=u2,%s" % self
.ou_users
)
1287 self
.assertEqual(len(res1
), 2)
1288 dn_list
= [str(res
.dn
) for res
in res1
]
1289 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1290 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1292 res1
= self
.ldb
.search(self
.ou
,
1293 scope
=SCOPE_SUBTREE
,
1294 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=u2,%s" % self
.ou_users
)
1295 self
.assertEqual(len(res1
), 2)
1296 dn_list
= [str(res
.dn
) for res
in res1
]
1297 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1298 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1300 res1
= self
.ldb
.search(self
.ou
,
1301 scope
=SCOPE_SUBTREE
,
1302 expression
="member=cn=u2,%s" % self
.ou_users
)
1303 self
.assertEqual(len(res1
), 2)
1304 dn_list
= [str(res
.dn
) for res
in res1
]
1305 self
.assertTrue("CN=g1,%s" % self
.ou_groups
in dn_list
)
1306 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1308 res1
= self
.ldb
.search(self
.ou
,
1309 scope
=SCOPE_SUBTREE
,
1310 expression
="member:1.2.840.113556.1.4.1941:=cn=u2,%s" % self
.ou_users
)
1311 self
.assertEqual(len(res1
), 2)
1312 dn_list
= [str(res
.dn
) for res
in res1
]
1313 self
.assertTrue("CN=g1,%s" % self
.ou_groups
in dn_list
)
1314 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1316 def test_u3_members(self
):
1317 res1
= self
.ldb
.search(self
.ou
,
1318 scope
=SCOPE_SUBTREE
,
1319 expression
="member=cn=u3,%s" % self
.ou_users
)
1320 self
.assertEqual(len(res1
), 2)
1321 dn_list
= [str(res
.dn
) for res
in res1
]
1322 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1323 self
.assertTrue("CN=g4,%s" % self
.ou_groups
in dn_list
)
1325 res1
= self
.ldb
.search(self
.ou
,
1326 scope
=SCOPE_SUBTREE
,
1327 expression
="member:1.2.840.113556.1.4.1941:=cn=u3,%s" % self
.ou_users
)
1328 self
.assertEqual(len(res1
), 4)
1329 dn_list
= [str(res
.dn
) for res
in res1
]
1330 self
.assertTrue("CN=g1,%s" % self
.ou_groups
in dn_list
)
1331 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1332 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1333 self
.assertTrue("CN=g4,%s" % self
.ou_groups
in dn_list
)
1335 res1
= self
.ldb
.search(self
.ou
,
1336 scope
=SCOPE_SUBTREE
,
1337 expression
="memberOf=cn=u3,%s" % self
.ou_users
)
1338 self
.assertEqual(len(res1
), 1)
1339 self
.assertEqual(str(res1
[0].dn
), "CN=c1,%s" % self
.ou_computers
)
1341 res1
= self
.ldb
.search(self
.ou
,
1342 scope
=SCOPE_SUBTREE
,
1343 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=u3,%s" % self
.ou_users
)
1344 self
.assertEqual(len(res1
), 1)
1345 self
.assertEqual(str(res1
[0].dn
), "CN=c1,%s" % self
.ou_computers
)
1347 def test_u4_members(self
):
1348 res1
= self
.ldb
.search(self
.ou
,
1349 scope
=SCOPE_SUBTREE
,
1350 expression
="member=cn=u4,%s" % self
.ou_users
)
1351 self
.assertEqual(len(res1
), 1)
1352 self
.assertEqual(str(res1
[0].dn
), "CN=g4,%s" % self
.ou_groups
)
1354 res1
= self
.ldb
.search(self
.ou
,
1355 scope
=SCOPE_SUBTREE
,
1356 expression
="member:1.2.840.113556.1.4.1941:=cn=u4,%s" % self
.ou_users
)
1357 self
.assertEqual(len(res1
), 1)
1358 self
.assertEqual(str(res1
[0].dn
), "CN=g4,%s" % self
.ou_groups
)
1360 res1
= self
.ldb
.search(self
.ou
,
1361 scope
=SCOPE_SUBTREE
,
1362 expression
="memberOf=cn=u4,%s" % self
.ou_users
)
1363 self
.assertEqual(len(res1
), 2)
1364 dn_list
= [str(res
.dn
) for res
in res1
]
1365 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1366 self
.assertTrue("CN=c4,%s" % self
.ou_computers
in dn_list
)
1368 res1
= self
.ldb
.search(self
.ou
,
1369 scope
=SCOPE_SUBTREE
,
1370 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=u4,%s" % self
.ou_users
)
1371 self
.assertEqual(len(res1
), 2)
1372 dn_list
= [str(res
.dn
) for res
in res1
]
1373 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1374 self
.assertTrue("CN=c4,%s" % self
.ou_computers
in dn_list
)
1376 def test_c1_members(self
):
1377 res1
= self
.ldb
.search(self
.ou
,
1378 scope
=SCOPE_SUBTREE
,
1379 expression
="member=cn=c1,%s" % self
.ou_computers
)
1380 self
.assertEqual(len(res1
), 4)
1381 dn_list
= [str(res
.dn
) for res
in res1
]
1382 self
.assertTrue("CN=u1,%s" % self
.ou_users
in dn_list
)
1383 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1384 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1385 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1387 res1
= self
.ldb
.search(self
.ou
,
1388 scope
=SCOPE_SUBTREE
,
1389 expression
="member:1.2.840.113556.1.4.1941:=cn=c1,%s" % self
.ou_computers
)
1390 self
.assertEqual(len(res1
), 8)
1391 dn_list
= [str(res
.dn
) for res
in res1
]
1392 self
.assertTrue("CN=u1,%s" % self
.ou_users
in dn_list
)
1393 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1394 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1395 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1396 self
.assertTrue("CN=g1,%s" % self
.ou_groups
in dn_list
)
1397 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1398 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1399 self
.assertTrue("CN=g4,%s" % self
.ou_groups
in dn_list
)
1401 res1
= self
.ldb
.search(self
.ou
,
1402 scope
=SCOPE_SUBTREE
,
1403 expression
="memberOf=cn=c1,%s" % self
.ou_computers
)
1404 self
.assertEqual(len(res1
), 0)
1406 res1
= self
.ldb
.search(self
.ou
,
1407 scope
=SCOPE_SUBTREE
,
1408 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=c1,%s" % self
.ou_computers
)
1409 self
.assertEqual(len(res1
), 0)
1411 def test_c2_members(self
):
1412 res1
= self
.ldb
.search(self
.ou
,
1413 scope
=SCOPE_SUBTREE
,
1414 expression
="member=cn=c2,%s" % self
.ou_computers
)
1415 self
.assertEqual(len(res1
), 1)
1416 self
.assertEqual(str(res1
[0].dn
), "CN=u1,%s" % self
.ou_users
)
1418 res1
= self
.ldb
.search(self
.ou
,
1419 scope
=SCOPE_SUBTREE
,
1420 expression
="member:1.2.840.113556.1.4.1941:=cn=c2,%s" % self
.ou_computers
)
1421 self
.assertEqual(len(res1
), 1)
1422 self
.assertEqual(str(res1
[0].dn
), "CN=u1,%s" % self
.ou_users
)
1424 res1
= self
.ldb
.search(self
.ou
,
1425 scope
=SCOPE_SUBTREE
,
1426 expression
="memberOf=cn=c2,%s" % self
.ou_computers
)
1427 self
.assertEqual(len(res1
), 0)
1429 res1
= self
.ldb
.search(self
.ou
,
1430 scope
=SCOPE_SUBTREE
,
1431 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=c2,%s" % self
.ou_computers
)
1432 self
.assertEqual(len(res1
), 0)
1434 def test_c3_members(self
):
1435 res1
= self
.ldb
.search(self
.ou
,
1436 scope
=SCOPE_SUBTREE
,
1437 expression
="member=cn=c3,%s" % self
.ou_computers
)
1438 self
.assertEqual(len(res1
), 2)
1439 dn_list
= [str(res
.dn
) for res
in res1
]
1440 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1441 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1443 res1
= self
.ldb
.search(self
.ou
,
1444 scope
=SCOPE_SUBTREE
,
1445 expression
="member:1.2.840.113556.1.4.1941:=cn=c3,%s" % self
.ou_computers
)
1446 self
.assertEqual(len(res1
), 4)
1447 dn_list
= [str(res
.dn
) for res
in res1
]
1448 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1449 self
.assertTrue("CN=g1,%s" % self
.ou_groups
in dn_list
)
1450 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1451 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1453 res1
= self
.ldb
.search(self
.ou
,
1454 scope
=SCOPE_SUBTREE
,
1455 expression
="memberOf=cn=c3,%s" % self
.ou_computers
)
1456 self
.assertEqual(len(res1
), 0)
1458 res1
= self
.ldb
.search(self
.ou
,
1459 scope
=SCOPE_SUBTREE
,
1460 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=c3,%s" % self
.ou_computers
)
1461 self
.assertEqual(len(res1
), 0)
1463 def test_c4_members(self
):
1464 res1
= self
.ldb
.search(self
.ou
,
1465 scope
=SCOPE_SUBTREE
,
1466 expression
="member=cn=c4,%s" % self
.ou_computers
)
1467 self
.assertEqual(len(res1
), 2)
1468 dn_list
= [str(res
.dn
) for res
in res1
]
1469 self
.assertTrue("CN=g4,%s" % self
.ou_groups
in dn_list
)
1470 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1472 res1
= self
.ldb
.search(self
.ou
,
1473 scope
=SCOPE_SUBTREE
,
1474 expression
="member:1.2.840.113556.1.4.1941:=cn=c4,%s" % self
.ou_computers
)
1475 self
.assertEqual(len(res1
), 2)
1476 dn_list
= [str(res
.dn
) for res
in res1
]
1477 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1478 self
.assertTrue("CN=g4,%s" % self
.ou_groups
in dn_list
)
1480 res1
= self
.ldb
.search(self
.ou
,
1481 scope
=SCOPE_SUBTREE
,
1482 expression
="memberOf=cn=c4,%s" % self
.ou_computers
)
1483 self
.assertEqual(len(res1
), 0)
1485 res1
= self
.ldb
.search(self
.ou
,
1486 scope
=SCOPE_SUBTREE
,
1487 expression
="memberOf:1.2.840.113556.1.4.1941:=cn=c4,%s" % self
.ou_computers
)
1488 self
.assertEqual(len(res1
), 0)
1490 def test_or_member_queries(self
):
1491 res1
= self
.ldb
.search(self
.ou
,
1492 scope
=SCOPE_SUBTREE
,
1493 expression
=("(|(member:1.2.840.113556.1.4.1941:=cn=c1,%s)"
1494 "(member:1.2.840.113556.1.4.1941:=cn=c2,%s))") % (
1495 self
.ou_computers
, self
.ou_computers
))
1496 self
.assertEqual(len(res1
), 8)
1497 dn_list
= [str(res
.dn
) for res
in res1
]
1498 self
.assertTrue("CN=u1,%s" % self
.ou_users
in dn_list
)
1499 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1500 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1501 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1502 self
.assertTrue("CN=g1,%s" % self
.ou_groups
in dn_list
)
1503 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1504 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1505 self
.assertTrue("CN=g4,%s" % self
.ou_groups
in dn_list
)
1507 res1
= self
.ldb
.search(self
.ou
,
1508 scope
=SCOPE_SUBTREE
,
1509 expression
=("(|(member:1.2.840.113556.1.4.1941:=cn=c2,%s)"
1510 "(member:1.2.840.113556.1.4.1941:=cn=c3,%s))") % (
1511 self
.ou_computers
, self
.ou_computers
))
1512 self
.assertEqual(len(res1
), 5)
1513 dn_list
= [str(res
.dn
) for res
in res1
]
1514 self
.assertTrue("CN=u1,%s" % self
.ou_users
in dn_list
)
1515 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1516 self
.assertTrue("CN=g1,%s" % self
.ou_groups
in dn_list
)
1517 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1518 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1520 res1
= self
.ldb
.search(self
.ou
,
1521 scope
=SCOPE_SUBTREE
,
1522 expression
=("(|(member:1.2.840.113556.1.4.1941:=cn=c2,%s)"
1523 "(member:1.2.840.113556.1.4.1941:=cn=c4,%s))") % (
1524 self
.ou_computers
, self
.ou_computers
))
1525 self
.assertEqual(len(res1
), 3)
1526 dn_list
= [str(res
.dn
) for res
in res1
]
1527 self
.assertTrue("CN=u1,%s" % self
.ou_users
in dn_list
)
1528 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1529 self
.assertTrue("CN=g4,%s" % self
.ou_groups
in dn_list
)
1531 res1
= self
.ldb
.search(self
.ou
,
1532 scope
=SCOPE_SUBTREE
,
1533 expression
=("(|(member:1.2.840.113556.1.4.1941:=cn=c3,%s)"
1534 "(member:1.2.840.113556.1.4.1941:=cn=c4,%s))") % (
1535 self
.ou_computers
, self
.ou_computers
))
1536 self
.assertEqual(len(res1
), 6)
1537 dn_list
= [str(res
.dn
) for res
in res1
]
1538 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1539 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1540 self
.assertTrue("CN=g1,%s" % self
.ou_groups
in dn_list
)
1541 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1542 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1543 self
.assertTrue("CN=g4,%s" % self
.ou_groups
in dn_list
)
1545 res1
= self
.ldb
.search(self
.ou
,
1546 scope
=SCOPE_SUBTREE
,
1547 expression
=("(|(member:1.2.840.113556.1.4.1941:=cn=u1,%s)"
1548 "(member:1.2.840.113556.1.4.1941:=cn=c4,%s))") % (
1549 self
.ou_users
, self
.ou_computers
))
1550 self
.assertEqual(len(res1
), 2)
1551 dn_list
= [str(res
.dn
) for res
in res1
]
1552 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1553 self
.assertTrue("CN=g4,%s" % self
.ou_groups
in dn_list
)
1555 def test_and_member_queries(self
):
1556 res1
= self
.ldb
.search(self
.ou
,
1557 scope
=SCOPE_SUBTREE
,
1558 expression
=("(&(member:1.2.840.113556.1.4.1941:=cn=c1,%s)"
1559 "(member:1.2.840.113556.1.4.1941:=cn=c2,%s))") % (
1560 self
.ou_computers
, self
.ou_computers
))
1561 self
.assertEqual(len(res1
), 1)
1562 self
.assertEqual(str(res1
[0].dn
), "CN=u1,%s" % self
.ou_users
)
1564 res1
= self
.ldb
.search(self
.ou
,
1565 scope
=SCOPE_SUBTREE
,
1566 expression
=("(&(member:1.2.840.113556.1.4.1941:=cn=c2,%s)"
1567 "(member:1.2.840.113556.1.4.1941:=cn=c3,%s))") % (
1568 self
.ou_computers
, self
.ou_computers
))
1569 self
.assertEqual(len(res1
), 0)
1571 res1
= self
.ldb
.search(self
.ou
,
1572 scope
=SCOPE_SUBTREE
,
1573 expression
=("(&(member:1.2.840.113556.1.4.1941:=cn=c3,%s)"
1574 "(member:1.2.840.113556.1.4.1941:=cn=u3,%s))") % (
1575 self
.ou_computers
, self
.ou_users
))
1576 self
.assertEqual(len(res1
), 3)
1577 dn_list
= [str(res
.dn
) for res
in res1
]
1578 self
.assertTrue("CN=g1,%s" % self
.ou_groups
in dn_list
)
1579 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1580 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1582 res1
= self
.ldb
.search(self
.ou
,
1583 scope
=SCOPE_SUBTREE
,
1584 expression
=("(&(member:1.2.840.113556.1.4.1941:=cn=c1,%s)"
1585 "(member:1.2.840.113556.1.4.1941:=cn=u4,%s))") % (
1586 self
.ou_computers
, self
.ou_computers
))
1587 self
.assertEqual(len(res1
), 0)
1589 def test_or_memberOf_queries(self
):
1590 res1
= self
.ldb
.search(self
.ou
,
1591 scope
=SCOPE_SUBTREE
,
1592 expression
=("(|(memberOf:1.2.840.113556.1.4.1941:=cn=g1,%s)"
1593 "(memberOf:1.2.840.113556.1.4.1941:=cn=g2,%s))") % (
1594 self
.ou_groups
, self
.ou_groups
))
1595 self
.assertEqual(len(res1
), 6)
1596 dn_list
= [str(res
.dn
) for res
in res1
]
1597 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1598 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1599 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1600 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1601 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1602 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1604 res1
= self
.ldb
.search(self
.ou
,
1605 scope
=SCOPE_SUBTREE
,
1606 expression
=("(|(memberOf:1.2.840.113556.1.4.1941:=cn=g1,%s)"
1607 "(memberOf:1.2.840.113556.1.4.1941:=cn=g3,%s))") % (
1608 self
.ou_groups
, self
.ou_groups
))
1609 self
.assertEqual(len(res1
), 6)
1610 dn_list
= [str(res
.dn
) for res
in res1
]
1611 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1612 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1613 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1614 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1615 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1616 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1618 res1
= self
.ldb
.search(self
.ou
,
1619 scope
=SCOPE_SUBTREE
,
1620 expression
=("(|(memberOf:1.2.840.113556.1.4.1941:=cn=g1,%s)"
1621 "(memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s))") % (
1622 self
.ou_groups
, self
.ou_groups
))
1623 self
.assertEqual(len(res1
), 8)
1624 dn_list
= [str(res
.dn
) for res
in res1
]
1625 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1626 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1627 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1628 self
.assertTrue("CN=g2,%s" % self
.ou_groups
in dn_list
)
1629 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1630 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1631 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1632 self
.assertTrue("CN=c4,%s" % self
.ou_computers
in dn_list
)
1634 res1
= self
.ldb
.search(self
.ou
,
1635 scope
=SCOPE_SUBTREE
,
1636 expression
=("(|(memberOf:1.2.840.113556.1.4.1941:=cn=g2,%s)"
1637 "(memberOf:1.2.840.113556.1.4.1941:=cn=g3,%s))") %
1638 (self
.ou_groups
, self
.ou_groups
))
1639 self
.assertEqual(len(res1
), 5)
1640 dn_list
= [str(res
.dn
) for res
in res1
]
1641 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1642 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1643 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1644 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1645 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1647 res1
= self
.ldb
.search(self
.ou
,
1648 scope
=SCOPE_SUBTREE
,
1649 expression
=("(|(memberOf:1.2.840.113556.1.4.1941:=cn=g2,%s)"
1650 "(memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s))") % (
1651 self
.ou_groups
, self
.ou_groups
))
1652 self
.assertEqual(len(res1
), 7)
1653 dn_list
= [str(res
.dn
) for res
in res1
]
1654 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1655 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1656 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1657 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1658 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1659 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1660 self
.assertTrue("CN=c4,%s" % self
.ou_computers
in dn_list
)
1662 res1
= self
.ldb
.search(self
.ou
,
1663 scope
=SCOPE_SUBTREE
,
1664 expression
=("(|(memberOf:1.2.840.113556.1.4.1941:=cn=g3,%s)"
1665 "(memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s))") % (
1666 self
.ou_groups
, self
.ou_groups
))
1667 self
.assertEqual(len(res1
), 5)
1668 dn_list
= [str(res
.dn
) for res
in res1
]
1669 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1670 self
.assertTrue("CN=u4,%s" % self
.ou_users
in dn_list
)
1671 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1672 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1673 self
.assertTrue("CN=c4,%s" % self
.ou_computers
in dn_list
)
1675 def test_and_memberOf_queries(self
):
1676 res1
= self
.ldb
.search(self
.ou
,
1677 scope
=SCOPE_SUBTREE
,
1678 expression
=("(&(memberOf:1.2.840.113556.1.4.1941:=cn=g1,%s)"
1679 "(memberOf:1.2.840.113556.1.4.1941:=cn=g2,%s))") % (
1680 self
.ou_groups
, self
.ou_groups
))
1681 self
.assertEqual(len(res1
), 5)
1682 dn_list
= [str(res
.dn
) for res
in res1
]
1683 self
.assertTrue("CN=u2,%s" % self
.ou_users
in dn_list
)
1684 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1685 self
.assertTrue("CN=g3,%s" % self
.ou_groups
in dn_list
)
1686 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1687 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1689 res1
= self
.ldb
.search(self
.ou
,
1690 scope
=SCOPE_SUBTREE
,
1691 expression
=("(&(memberOf:1.2.840.113556.1.4.1941:=cn=g1,%s)"
1692 "(memberOf:1.2.840.113556.1.4.1941:=cn=g3,%s))") % (
1693 self
.ou_groups
, self
.ou_groups
))
1694 self
.assertEqual(len(res1
), 3)
1695 dn_list
= [str(res
.dn
) for res
in res1
]
1696 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1697 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1698 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1700 res1
= self
.ldb
.search(self
.ou
,
1701 scope
=SCOPE_SUBTREE
,
1702 expression
=("(&(memberOf:1.2.840.113556.1.4.1941:=cn=g1,%s)"
1703 "(memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s))") % (
1704 self
.ou_groups
, self
.ou_groups
))
1705 self
.assertEqual(len(res1
), 2)
1706 dn_list
= [str(res
.dn
) for res
in res1
]
1707 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1708 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1710 res1
= self
.ldb
.search(self
.ou
,
1711 scope
=SCOPE_SUBTREE
,
1712 expression
=("(&(memberOf:1.2.840.113556.1.4.1941:=cn=g2,%s)"
1713 "(memberOf:1.2.840.113556.1.4.1941:=cn=g3,%s))") % (
1714 self
.ou_groups
, self
.ou_groups
))
1715 self
.assertEqual(len(res1
), 3)
1716 dn_list
= [str(res
.dn
) for res
in res1
]
1717 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1718 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1719 self
.assertTrue("CN=c3,%s" % self
.ou_computers
in dn_list
)
1721 res1
= self
.ldb
.search(self
.ou
,
1722 scope
=SCOPE_SUBTREE
,
1723 expression
=("(&(memberOf:1.2.840.113556.1.4.1941:=cn=g2,%s)"
1724 "(memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s))") % (
1725 self
.ou_groups
, self
.ou_groups
))
1726 self
.assertEqual(len(res1
), 2)
1727 dn_list
= [str(res
.dn
) for res
in res1
]
1728 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1729 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1731 res1
= self
.ldb
.search(self
.ou
,
1732 scope
=SCOPE_SUBTREE
,
1733 expression
=("(&(memberOf:1.2.840.113556.1.4.1941:=cn=g3,%s)"
1734 "(memberOf:1.2.840.113556.1.4.1941:=cn=g4,%s))") % (
1735 self
.ou_groups
, self
.ou_groups
))
1736 self
.assertEqual(len(res1
), 2)
1737 dn_list
= [str(res
.dn
) for res
in res1
]
1738 self
.assertTrue("CN=u3,%s" % self
.ou_users
in dn_list
)
1739 self
.assertTrue("CN=c1,%s" % self
.ou_computers
in dn_list
)
1741 res1
= self
.ldb
.search(self
.ou
,
1742 scope
=SCOPE_SUBTREE
,
1743 expression
=("(&(memberOf:1.2.840.113556.1.4.1941:=cn=g1,%s)"
1744 "(memberOf:1.2.840.113556.1.4.1941:=cn=c1,%s))") % (
1745 self
.ou_groups
, self
.ou_computers
))
1746 self
.assertEqual(len(res1
), 0)
1749 parser
= optparse
.OptionParser("match_rules.py [options] <host>")
1750 sambaopts
= options
.SambaOptions(parser
)
1751 parser
.add_option_group(sambaopts
)
1752 parser
.add_option_group(options
.VersionOptions(parser
))
1754 # use command line creds if available
1755 credopts
= options
.CredentialsOptions(parser
)
1756 parser
.add_option_group(credopts
)
1757 opts
, args
= parser
.parse_args()
1758 subunitopts
= SubunitOptions(parser
)
1759 parser
.add_option_group(subunitopts
)
1762 parser
.print_usage()
1767 lp
= sambaopts
.get_loadparm()
1768 creds
= credopts
.get_credentials(lp
)
1770 if "://" not in host
:
1771 if os
.path
.isfile(host
):
1772 host
= "tdb://%s" % host
1774 host
= "ldap://%s" % host
1776 TestProgram(module
=__name__
, opts
=subunitopts
)