Debian Codegraph @

source4/torture/drs/python/repl_schema.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 # Tests various schema replication scenarios
5 #
6 # Copyright (C) Kamen Mazdrashki <[email protected]> 2010
7 #
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.
12 #
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.
17 #
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/>.
20 #
21
22 #
23 # Usage:
24 # export DC1=dc1_dns_name
25 # export DC2=dc2_dns_name
26 # export SUBUNITRUN=$samba4srcdir/scripting/bin/subunitrun
27 # PYTHONPATH="$PYTHONPATH:$samba4srcdir/torture/drs/python" $SUBUNITRUN repl_schema -U"$DOMAIN/$DC_USERNAME"%"$DC_PASSWORD"
28 #
29
30 import time
31 import random
32 import ldb
33 import drs_base
34
35 from ldb import (
36 ERR_NO_SUCH_OBJECT,
37 LdbError,
38 SCOPE_BASE,
39 Message,
40 FLAG_MOD_ADD,
41 FLAG_MOD_REPLACE
42 )
43 from samba.dcerpc import drsuapi, misc
44 from samba.drs_utils import drs_DsBind
45 from samba import dsdb
46
47
48 class DrsReplSchemaTestCase(drs_base.DrsBaseTestCase):
49
50 # prefix for all objects created
51 obj_prefix = None
52 # current Class or Attribute object id
53 obj_id = 0
54
55 def _exop_req8(self, dest_dsa, invocation_id, nc_dn_str, exop,
56 replica_flags=0, max_objects=0):
57 req8 = drsuapi.DsGetNCChangesRequest8()
58
59 req8.destination_dsa_guid = misc.GUID(dest_dsa) if dest_dsa else misc.GUID()
60 req8.source_dsa_invocation_id = misc.GUID(invocation_id)
61 req8.naming_context = drsuapi.DsReplicaObjectIdentifier()
62 req8.naming_context.dn = str(nc_dn_str)
63 req8.highwatermark = drsuapi.DsReplicaHighWaterMark()
64 req8.highwatermark.tmp_highest_usn = 0
65 req8.highwatermark.reserved_usn = 0
66 req8.highwatermark.highest_usn = 0
67 req8.uptodateness_vector = None
68 req8.replica_flags = replica_flags
69 req8.max_object_count = max_objects
70 req8.max_ndr_size = 402116
71 req8.extended_op = exop
72 req8.fsmo_info = 0
73 req8.partial_attribute_set = None
74 req8.partial_attribute_set_ex = None
75 req8.mapping_ctr.num_mappings = 0
76 req8.mapping_ctr.mappings = None
77
78 return req8
79
80 def setUp(self):
81 super(DrsReplSchemaTestCase, self).setUp()
82
83 # disable automatic replication temporary
84 self._disable_all_repl(self.dnsname_dc1)
85 self._disable_all_repl(self.dnsname_dc2)
86
87 # make sure DCs are synchronized before the test
88 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, forced=True)
89 self._net_drs_replicate(DC=self.dnsname_dc1, fromDC=self.dnsname_dc2, forced=True)
90 # initialize objects prefix if not done yet
91 if self.obj_prefix is None:
92 t = time.strftime("%s", time.gmtime())
93 DrsReplSchemaTestCase.obj_prefix = "DrsReplSchema-%s" % t
94
95 def tearDown(self):
96 self._enable_all_repl(self.dnsname_dc1)
97 self._enable_all_repl(self.dnsname_dc2)
98 super(DrsReplSchemaTestCase, self).tearDown()
99
100 def _make_obj_names(self, base_name):
101 '''Try to create a unique name for an object
102 that is to be added to schema'''
103 self.obj_id += 1
104 obj_name = "%s-%d-%s" % (self.obj_prefix, self.obj_id, base_name)
105 obj_ldn = obj_name.replace("-", "")
106 obj_dn = ldb.Dn(self.ldb_dc1, "CN=X")
107 obj_dn.add_base(ldb.Dn(self.ldb_dc1, self.schema_dn))
108 obj_dn.set_component(0, "CN", obj_name)
109 return (obj_dn, obj_name, obj_ldn)
110
111 def _schema_new_class(self, ldb_ctx, base_name, base_int, oc_cat=1, attrs=None):
112 (class_dn, class_name, class_ldn) = self._make_obj_names(base_name)
113 rec = {"dn": class_dn,
114 "objectClass": ["top", "classSchema"],
115 "cn": class_name,
116 "lDAPDisplayName": class_ldn,
117 "governsId": "1.3.6.1.4.1.7165.4.6.2.5."
118 + str((100000 * base_int) + random.randint(1, 100000)) + ".1.5.13",
119 "instanceType": "4",
120 "objectClassCategory": "%d" % oc_cat,
121 "subClassOf": "top",
122 "systemOnly": "FALSE"}
123 # allow overriding/adding attributes
124 if attrs is not None:
125 rec.update(attrs)
126 # add it to the Schema
127 try:
128 ldb_ctx.add(rec)
129 except LdbError as e:
130 (enum, estr) = e.args
131 self.fail("Adding record failed with %d/%s" % (enum, estr))
132
133 self._ldap_schemaUpdateNow(ldb_ctx)
134 return (rec["lDAPDisplayName"], rec["dn"])
135
136 def _schema_new_attr(self, ldb_ctx, base_name, base_int, attrs=None):
137 (attr_dn, attr_name, attr_ldn) = self._make_obj_names(base_name)
138 rec = {"dn": attr_dn,
139 "objectClass": ["top", "attributeSchema"],
140 "cn": attr_name,
141 "lDAPDisplayName": attr_ldn,
142 "attributeId": "1.3.6.1.4.1.7165.4.6.1.5."
143 + str((100000 * base_int) + random.randint(1, 100000)) + ".1.5.13",
144 "attributeSyntax": "2.5.5.12",
145 "omSyntax": "64",
146 "instanceType": "4",
147 "isSingleValued": "TRUE",
148 "systemOnly": "FALSE"}
149 # allow overriding/adding attributes
150 if attrs is not None:
151 rec.update(attrs)
152 # add it to the Schema
153 ldb_ctx.add(rec)
154 self._ldap_schemaUpdateNow(ldb_ctx)
155 return (rec["lDAPDisplayName"], rec["dn"])
156
157 def _check_object(self, obj_dn):
158 '''Check if object obj_dn exists on both DCs'''
159 res_dc1 = self.ldb_dc1.search(base=obj_dn,
160 scope=SCOPE_BASE,
161 attrs=["*"])
162 self.assertEqual(len(res_dc1), 1,
163 "%s doesn't exists on %s" % (obj_dn, self.dnsname_dc1))
164 try:
165 res_dc2 = self.ldb_dc2.search(base=obj_dn,
166 scope=SCOPE_BASE,
167 attrs=["*"])
168 except LdbError as e1:
169 (enum, estr) = e1.args
170 if enum == ERR_NO_SUCH_OBJECT:
171 self.fail("%s doesn't exists on %s" % (obj_dn, self.dnsname_dc2))
172 raise
173 self.assertEqual(len(res_dc2), 1,
174 "%s doesn't exists on %s" % (obj_dn, self.dnsname_dc2))
175
176 def test_class(self):
177 """Simple test for classSchema replication"""
178 # add new classSchema object
179 (c_ldn, c_dn) = self._schema_new_class(self.ldb_dc1, "cls-S", 0)
180 # force replication from DC1 to DC2
181 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, nc_dn=self.schema_dn, forced=True)
182 # check object is replicated
183 self._check_object(c_dn)
184
185 def test_classInheritance(self):
186 """Test inheritance through subClassOf
187 I think 5 levels of inheritance is pretty decent for now."""
188 # add 5 levels deep hierarchy
189 c_dn_list = []
190 c_ldn_last = None
191 for i in range(1, 6):
192 base_name = "cls-I-%02d" % i
193 (c_ldn, c_dn) = self._schema_new_class(self.ldb_dc1, base_name, i)
194 c_dn_list.append(c_dn)
195 if c_ldn_last:
196 # inherit from last class added
197 m = Message.from_dict(self.ldb_dc1,
198 {"dn": c_dn,
199 "subClassOf": c_ldn_last},
200 FLAG_MOD_REPLACE)
201 self.ldb_dc1.modify(m)
202 # store last class ldapDisplayName
203 c_ldn_last = c_ldn
204 # force replication from DC1 to DC2
205 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, nc_dn=self.schema_dn, forced=True)
206 # check objects are replicated
207 for c_dn in c_dn_list:
208 self._check_object(c_dn)
209
210 def test_classWithCustomAttribute(self):
211 """Create new Attribute and a Class,
212 that has value for newly created attribute.
213 This should check code path that searches for
214 AttributeID_id in Schema cache"""
215 # add new attributeSchema object
216 (a_ldn, a_dn) = self._schema_new_attr(self.ldb_dc1, "attr-A", 7)
217 # add a base classSchema class so we can use our new
218 # attribute in class definition in a sibling class
219 (c_ldn, c_dn) = self._schema_new_class(self.ldb_dc1, "cls-A", 8,
220 1,
221 {"systemMayContain": a_ldn,
222 "subClassOf": "classSchema"})
223 # add new classSchema object with value for a_ldb attribute
224 (c_ldn, c_dn) = self._schema_new_class(self.ldb_dc1, "cls-B", 9,
225 1,
226 {"objectClass": ["top", "classSchema", c_ldn],
227 a_ldn: "test_classWithCustomAttribute"})
228 # force replication from DC1 to DC2
229 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, nc_dn=self.schema_dn, forced=True)
230 # check objects are replicated
231 self._check_object(c_dn)
232 self._check_object(a_dn)
233
234 def test_classWithCustomLinkAttribute(self):
235 """Create new Attribute and a Class,
236 that has value for newly created attribute.
237 This should check code path that searches for
238 AttributeID_id in Schema cache"""
239 # add new attributeSchema object
240 (a_ldn, a_dn) = self._schema_new_attr(self.ldb_dc1, "attr-Link-X", 10,
241 attrs={'linkID': "1.2.840.113556.1.2.50",
242 "attributeSyntax": "2.5.5.1",
243 "omSyntax": "127"})
244 # add a base classSchema class so we can use our new
245 # attribute in class definition in a sibling class
246 (c_ldn, c_dn) = self._schema_new_class(self.ldb_dc1, "cls-Link-Y", 11,
247 1,
248 {"systemMayContain": a_ldn,
249 "subClassOf": "classSchema"})
250 # add new classSchema object with value for a_ldb attribute
251 (c_ldn, c_dn) = self._schema_new_class(self.ldb_dc1, "cls-Link-Z", 12,
252 1,
253 {"objectClass": ["top", "classSchema", c_ldn],
254 a_ldn: self.schema_dn})
255 # force replication from DC1 to DC2
256 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, nc_dn=self.schema_dn, forced=True)
257 # check objects are replicated
258 self._check_object(c_dn)
259 self._check_object(a_dn)
260
261 res = self.ldb_dc1.search(base="",
262 scope=SCOPE_BASE,
263 attrs=["domainFunctionality"])
264
265 if int(res[0]["domainFunctionality"][0]) > dsdb.DS_DOMAIN_FUNCTION_2000:
266 res = self.ldb_dc1.search(base=a_dn,
267 scope=SCOPE_BASE,
268 attrs=["msDS-IntId"])
269 self.assertEqual(1, len(res))
270 self.assertTrue("msDS-IntId" in res[0])
271 int_id = int(res[0]["msDS-IntId"][0])
272 if int_id < 0:
273 int_id += (1 << 32)
274
275 dc_guid_1 = self.ldb_dc1.get_invocation_id()
276
277 drs, drs_handle = self._ds_bind(self.dnsname_dc1, ip=self.url_dc1)
278
279 req8 = self._exop_req8(dest_dsa=None,
280 invocation_id=dc_guid_1,
281 nc_dn_str=c_dn,
282 exop=drsuapi.DRSUAPI_EXOP_REPL_OBJ,
283 replica_flags=drsuapi.DRSUAPI_DRS_SYNC_FORCED)
284
285 (level, ctr) = drs.DsGetNCChanges(drs_handle, 8, req8)
286
287 for link in ctr.linked_attributes:
288 self.assertTrue(link.attid != int_id,
289 'Got %d for both' % link.attid)
290
291 def test_attribute(self):
292 """Simple test for attributeSchema replication"""
293 # add new attributeSchema object
294 (a_ldn, a_dn) = self._schema_new_attr(self.ldb_dc1, "attr-S", 13)
295 # force replication from DC1 to DC2
296 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, nc_dn=self.schema_dn, forced=True)
297 # check object is replicated
298 self._check_object(a_dn)
299
300 def test_attribute_on_ou(self):
301 """Simple test having an OU with a custom attribute replicated correctly
302
303 This ensures that the server
304 """
305
306 # add new attributeSchema object
307 (a_ldn, a_dn) = self._schema_new_attr(self.ldb_dc1, "attr-OU-S", 14)
308 (c_ldn, c_dn) = self._schema_new_class(self.ldb_dc1, "cls-OU-A", 15,
309 3,
310 {"mayContain": a_ldn})
311 ou_dn = ldb.Dn(self.ldb_dc1, "ou=X")
312 ou_dn.add_base(self.ldb_dc1.get_default_basedn())
313 ou_dn.set_component(0, "OU", a_dn.get_component_value(0))
314 rec = {"dn": ou_dn,
315 "objectClass": ["top", "organizationalUnit", c_ldn],
316 "ou": ou_dn.get_component_value(0),
317 a_ldn: "test OU"}
318 self.ldb_dc1.add(rec)
319
320 # force replication from DC1 to DC2
321 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, nc_dn=self.domain_dn, forced=True)
322 # check objects are replicated
323 self._check_object(c_dn)
324 self._check_object(a_dn)
325 self._check_object(ou_dn)
326 self.ldb_dc1.delete(ou_dn)
327
328 def test_all(self):
329 """Basic plan is to create bunch of classSchema
330 and attributeSchema objects, replicate Schema NC
331 and then check all objects are replicated correctly"""
332
333 # add new classSchema object
334 (c_ldn, c_dn) = self._schema_new_class(self.ldb_dc1, "cls-A", 16)
335 # add new attributeSchema object
336 (a_ldn, a_dn) = self._schema_new_attr(self.ldb_dc1, "attr-A", 17)
337
338 # add attribute to the class we have
339 m = Message.from_dict(self.ldb_dc1,
340 {"dn": c_dn,
341 "mayContain": a_ldn},
342 FLAG_MOD_ADD)
343 self.ldb_dc1.modify(m)
344
345 # force replication from DC1 to DC2
346 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, nc_dn=self.schema_dn, forced=True)
347
348 # check objects are replicated
349 self._check_object(c_dn)
350 self._check_object(a_dn)
351
352 def test_classWithCustomBinaryDNLinkAttribute(self):
353 # Add a new attribute to the schema, which has binary DN syntax (2.5.5.7)
354 (bin_ldn, bin_dn) = self._schema_new_attr(self.ldb_dc1, "attr-Link-Bin", 18,
355 attrs={"linkID": "1.2.840.113556.1.2.50",
356 "attributeSyntax": "2.5.5.7",
357 "omSyntax": "127"})
358
359 (bin_ldn_b, bin_dn_b) = self._schema_new_attr(self.ldb_dc1, "attr-Link-Bin-Back", 19,
360 attrs={"linkID": bin_ldn,
361 "attributeSyntax": "2.5.5.1",
362 "omSyntax": "127"})
363
364 # Add a new class to the schema which can have the binary DN attribute
365 (c_ldn, c_dn) = self._schema_new_class(self.ldb_dc1, "cls-Link-Bin", 20,
366 3,
367 {"mayContain": bin_ldn})
368 (c_ldn_b, c_dn_b) = self._schema_new_class(self.ldb_dc1, "cls-Link-Bin-Back", 21,
369 3,
370 {"mayContain": bin_ldn_b})
371
372 link_end_dn = ldb.Dn(self.ldb_dc1, "ou=X")
373 link_end_dn.add_base(self.ldb_dc1.get_default_basedn())
374 link_end_dn.set_component(0, "OU", bin_dn_b.get_component_value(0))
375
376 ou_dn = ldb.Dn(self.ldb_dc1, "ou=X")
377 ou_dn.add_base(self.ldb_dc1.get_default_basedn())
378 ou_dn.set_component(0, "OU", bin_dn.get_component_value(0))
379
380 # Add an instance of the class to be pointed at
381 rec = {"dn": link_end_dn,
382 "objectClass": ["top", "organizationalUnit", c_ldn_b],
383 "ou": link_end_dn.get_component_value(0)}
384 self.ldb_dc1.add(rec)
385
386 # .. and one that does, and points to the first one
387 rec = {"dn": ou_dn,
388 "objectClass": ["top", "organizationalUnit", c_ldn],
389 "ou": ou_dn.get_component_value(0)}
390 self.ldb_dc1.add(rec)
391
392 m = Message.from_dict(self.ldb_dc1,
393 {"dn": ou_dn,
394 bin_ldn: "B:8:1234ABCD:%s" % str(link_end_dn)},
395 FLAG_MOD_ADD)
396 self.ldb_dc1.modify(m)
397
398 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
399 nc_dn=self.schema_dn, forced=True)
400 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
401 nc_dn=self.domain_dn, forced=True)
402
403 self._check_object(c_dn)
404 self._check_object(bin_dn)
405
406 # Make sure we can delete the backlink
407 self.ldb_dc1.delete(link_end_dn)
408
409 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
410 nc_dn=self.schema_dn, forced=True)
411 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
412 nc_dn=self.domain_dn, forced=True)
413
414 def test_rename(self):
415 """Basic plan is to create a classSchema
416 and attributeSchema objects, replicate Schema NC
417 and then check all objects are replicated correctly"""
418
419 # add new classSchema object
420 (c_ldn, c_dn) = self._schema_new_class(self.ldb_dc1, "cls-B", 20)
421
422 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
423 nc_dn=self.schema_dn, forced=True)
424
425 # check objects are replicated
426 self._check_object(c_dn)
427
428 # rename the Class CN
429 c_dn_new = ldb.Dn(self.ldb_dc1, str(c_dn))
430 c_dn_new.set_component(0,
431 "CN",
432 c_dn.get_component_value(0) + "-NEW")
433 try:
434 self.ldb_dc1.rename(c_dn, c_dn_new)
435 except LdbError as e2:
436 (num, _) = e2.args
437 self.fail("failed to change CN for %s: %s" % (c_dn, _))
438
439 # force replication from DC1 to DC2
440 self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
441 nc_dn=self.schema_dn, forced=True)
442
443 # check objects are replicated
444 self._check_object(c_dn_new)