Debian Codegraph @

source4/torture/drs/python/getnc_schema.py

raw
1 import drs_base
2 import ldb
3 import time
4 import random
5 import os
6
7 break_me = os.getenv("PLEASE_BREAK_MY_WINDOWS") == "1"
8 assert break_me, ("This test breaks Windows active directory after "
9 "a few runs. Set PLEASE_BREAK_MY_WINDOWS=1 to run.")
10
11 # This test runs against Windows. To run, set up two Windows AD DCs, join one
12 # to the other, and make sure the passwords are the same. SMB_CONF_PATH must
13 # also be set to any smb.conf file. Set DC1 to the PDC's hostname, and DC2 to
14 # the join'd DC's hostname. Example:
15 # PLEASE_BREAK_MY_WINDOWS=1
16 # DC1=pdc DC2=joindc
17 # SMB_CONF_PATH=st/ad_dc/etc/smb.conf
18 # PYTHONPATH=$PYTHONPATH:./source4/torture/drs/python
19 # python3 ./source4/scripting/bin/subunitrun getnc_schema
20 # -UAdministrator%Password
21
22 class SchemaReplicationTests(drs_base.DrsBaseTestCase):
23
24 def setUp(self):
25 super(SchemaReplicationTests, self).setUp()
26 self.creds = self.get_credentials()
27 self.cmdline_auth = "-U{}%{}".format(self.creds.get_username(),
28 self.creds.get_password())
29
30 self.from_ldb = self.ldb_dc1
31 self.dest_ldb = self.ldb_dc2
32 self._disable_inbound_repl(self.url_dc1)
33 self._disable_all_repl(self.url_dc1)
34 self.free_offset = 0
35
36 def tearDown(self):
37 self._enable_inbound_repl(self.url_dc1)
38 self._enable_all_repl(self.url_dc1)
39
40 def do_repl(self, partition_dn):
41 self._enable_inbound_repl(self.url_dc1)
42 self._enable_all_repl(self.url_dc1)
43
44 samba_tool_cmd = ["drs", "replicate", self.url_dc2, self.url_dc1]
45 samba_tool_cmd += [partition_dn]
46 username = self.creds.get_username()
47 password = self.creds.get_password()
48 samba_tool_cmd += ["-U{0}%{1}".format(username, password)]
49
50 (result, out, err) = self.runsubcmd(*samba_tool_cmd)
51
52 try:
53 self.assertCmdSuccess(result, out, err)
54 except AssertionError:
55 print("Failed repl, retrying in 10s")
56 time.sleep(10)
57 (result, out, err) = self.runsubcmd(*samba_tool_cmd)
58
59 self._disable_inbound_repl(self.url_dc1)
60 self._disable_all_repl(self.url_dc1)
61
62 self.assertCmdSuccess(result, out, err)
63
64 # Get a unique prefix for some search expression like "([att]=[pref]{i}*)"
65 def get_unique(self, expr_templ):
66 found = True
67 while found:
68 i = random.randint(0, 65535)
69 res = self.from_ldb.search(base=self.schema_dn,
70 scope=ldb.SCOPE_SUBTREE,
71 expression=expr_templ.format(i=i))
72 found = len(res) > 0
73
74 return str(i)
75
76 def unique_gov_id_prefix(self):
77 prefix = "1.3.6.1.4.1.7165.4.6.2.8."
78 return prefix + self.get_unique("(governsId=" + prefix + "{i}.*)")
79
80 def unique_cn_prefix(self, prefix="testobj"):
81 return prefix + self.get_unique("(cn=" + prefix + "{i}x*)") + "x"
82
83 # Make test schema classes linked to each other in a line, then modify
84 # them in reverse order so when we repl, a link crosses the chunk
85 # boundary. Chunk size is 133 by default so we do 150.
86 def test_poss_superiors_across_chunk(self):
87 num_schema_objects_to_add = 150
88 class_name = self.unique_cn_prefix()
89
90 ldif_template = """
91 dn: CN={class_name}{i},{schema_dn}
92 objectClass: top
93 objectClass: classSchema
94 adminDescription: {class_name}{i}
95 adminDisplayName: {class_name}{i}
96 cn: {class_name}{i}
97 governsId: {gov_id}.{i}
98 instanceType: 4
99 objectClassCategory: 1
100 systemFlags: 16
101 systemOnly: FALSE
102 """
103
104 ldif_kwargs = {'class_name': class_name,
105 'schema_dn': self.schema_dn}
106 gov_id = self.unique_gov_id_prefix()
107 ldif = ldif_template.format(i=0, gov_id=gov_id, **ldif_kwargs)
108 self.from_ldb.add_ldif(ldif)
109
110 ldif_template += "systemPossSuperiors: {possSup}\n"
111
112 ids = list(range(num_schema_objects_to_add))
113 got_no_such_attrib = False
114 for i in ids[1:]:
115 last_class_name = class_name + str(i-1)
116 ldif = ldif_template.format(i=i, gov_id=gov_id,
117 possSup=last_class_name,
118 **ldif_kwargs)
119
120 try:
121 self.from_ldb.add_ldif(ldif)
122 if got_no_such_attrib:
123 self.from_ldb.set_schema_update_now()
124 except ldb.LdbError as e:
125 if e.args[0] != ldb.ERR_NO_SUCH_ATTRIBUTE:
126 self.fail(e)
127 if got_no_such_attrib:
128 self.fail(("got NO_SUCH_ATTRIB even after "
129 "setting schemaUpdateNow", str(e)))
130 print("got NO_SUCH_ATTRIB, trying schemaUpdateNow")
131 got_no_such_attrib = True
132 self.from_ldb.set_schema_update_now()
133 self.from_ldb.add_ldif(ldif)
134 self.from_ldb.set_schema_update_now()
135
136 ldif_template = """
137 dn: CN={class_name}{i},{schema_dn}
138 changetype: modify
139 replace: adminDescription
140 adminDescription: new_description
141 """
142
143 for i in reversed(ids):
144 ldif = ldif_template.format(i=i, **ldif_kwargs)
145 self.from_ldb.modify_ldif(ldif)
146
147 self.do_repl(self.schema_dn)
148
149 dn_templ = "CN={class_name}{i},{schema_dn}"
150 for i in ids:
151 dn = dn_templ.format(i=i, **ldif_kwargs)
152 res = self.dest_ldb.search(base=dn, scope=ldb.SCOPE_BASE)
153 self.assertEqual(len(res), 1)
154
155 # Test for method of adding linked attributes in schema partition
156 # required by other tests.
157 def test_create_linked_attribute_in_schema(self):
158 # Make an object outside of the schema partition that we can link to
159 user_name = self.unique_cn_prefix("user")
160 user_dn = "CN={},CN=Users,{}".format(user_name, self.domain_dn)
161
162 ldif_template = """
163 dn: {user_dn}
164 objectClass: person
165 objectClass: user"""
166 ldif = ldif_template.format(user_dn=user_dn)
167 self.from_ldb.add_ldif(ldif)
168
169 # Make test class name unique so test can run multiple times
170 class_name = self.unique_cn_prefix("class")
171
172 kwargs = {'class_name': class_name,
173 'schema_dn': self.schema_dn,
174 'user_dn': user_dn}
175
176 # Add an auxiliary schemaClass (cat 3) class and give it managedBy
177 # so we can create schema objects with linked attributes.
178 ldif_template = """
179 dn: CN={class_name},{schema_dn}
180 objectClass: classSchema
181 governsId: {gov_id}.0
182 instanceType: 4
183 systemFlags: 16
184 systemOnly: FALSE
185 objectClassCategory: 3
186 mayContain: managedBy
187 """
188
189 gov_id = self.unique_gov_id_prefix()
190 ldif = ldif_template.format(gov_id=gov_id, **kwargs)
191 self.from_ldb.add_ldif(ldif)
192
193 # Now make an instance that points back to the user with managedBy,
194 # thus creating an object in the schema with a linked attribute
195 ldif_template = """
196 dn: CN=link{class_name},{schema_dn}
197 objectClass: classSchema
198 objectClass: {class_name}
199 instanceType: 4
200 governsId: {gov_id}.0
201 systemFlags: 16
202 managedBy: {user_dn}
203 """
204
205 gov_id = self.unique_gov_id_prefix()
206 ldif = ldif_template.format(gov_id=gov_id, **kwargs)
207 self.from_ldb.add_ldif(ldif)
208
209 # Check link exists on test schema object
210 dn_templ = "CN=link{class_name},{schema_dn}"
211 dn = dn_templ.format(**kwargs)
212 res = self.from_ldb.search(base=dn, scope=ldb.SCOPE_BASE)
213 self.assertEqual(len(res), 1)
214 self.assertIsNotNone(res[0].get("managedBy"))
215 self.assertEqual(str(res[0].get("managedBy")[0]), user_dn)
216
217 # Check backlink on user object
218 res = self.from_ldb.search(base=user_dn, scope=ldb.SCOPE_BASE)
219 self.assertEqual(len(res), 1)
220 managed_objs = res[0].get("managedObjects")
221 self.assertEqual(len(managed_objs), 1)
222 managed_objs = [str(o) for o in managed_objs]
223 self.assertEqual(managed_objs, [dn_templ.format(**kwargs)])
224
225 def test_schema_linked_attributes(self):
226 num_test_objects = 9
227
228 # Make an object outside of the schema partition that we can link to
229 user_name = self.unique_cn_prefix("user")
230 user_dn = "CN={},CN=Users,{}".format(user_name, self.domain_dn)
231
232 ldif_template = """
233 dn: {user_dn}
234 objectClass: person
235 objectClass: user"""
236 ldif = ldif_template.format(user_dn=user_dn)
237 self.from_ldb.add_ldif(ldif)
238
239 self.do_repl(self.domain_dn)
240
241 # Make test object name prefixes unique so test can run multiple times
242 # in a single testenv (can't delete schema objects)
243 class_name = self.unique_cn_prefix("class")
244 link_class_name = self.unique_cn_prefix("linkClass")
245
246 kwargs = {'class_name': class_name,
247 'schema_dn': self.schema_dn,
248 'link_class_name': link_class_name,
249 'user_dn': user_dn}
250
251 # Add an auxiliary schemaClass (cat 3) class and give it managedBy
252 # so we can create schema objects with linked attributes.
253 ldif_template = """
254 dn: CN={class_name},{schema_dn}
255 objectClass: classSchema
256 governsId: {gov_id}.0
257 instanceType: 4
258 systemFlags: 16
259 systemOnly: FALSE
260 objectClassCategory: 3
261 mayContain: managedBy
262 """
263
264 gov_id = self.unique_gov_id_prefix()
265 ldif = ldif_template.format(gov_id=gov_id, **kwargs)
266 self.from_ldb.add_ldif(ldif)
267
268 # Now make instances that point back to the user with managedBy,
269 # thus creating objects in the schema with linked attributes
270 ldif_template = """
271 dn: CN={link_class_name}{i},{schema_dn}
272 objectClass: classSchema
273 objectClass: {class_name}
274 instanceType: 4
275 governsId: {gov_id}.0
276 systemFlags: 16
277 managedBy: {user_dn}
278 """
279
280 id_range = list(range(num_test_objects))
281 for i in id_range:
282 gov_id = self.unique_gov_id_prefix()
283 ldif = ldif_template.format(i=i, gov_id=gov_id, **kwargs)
284 self.from_ldb.add_ldif(ldif)
285
286 self.do_repl(self.schema_dn)
287
288 # Check link exists in each test schema objects at destination DC
289 dn_templ = "CN={link_class_name}{i},{schema_dn}"
290 for i in id_range:
291 dn = dn_templ.format(i=i, **kwargs)
292 res = self.dest_ldb.search(base=dn, scope=ldb.SCOPE_BASE)
293 self.assertEqual(len(res), 1)
294 self.assertIsNotNone(res[0].get("managedBy"))
295 self.assertEqual(str(res[0].get("managedBy")[0]), user_dn)
296
297 # Check backlinks list on user object contains DNs of test objects.
298 res = self.dest_ldb.search(base=user_dn, scope=ldb.SCOPE_BASE)
299 self.assertEqual(len(res), 1)
300 managed_objs = res[0].get("managedObjects")
301 self.assertIsNotNone(managed_objs)
302 managed_objs_set = {str(el) for el in managed_objs}
303 expected = {dn_templ.format(i=i, **kwargs) for i in id_range}
304 self.assertEqual(managed_objs_set, expected)