Debian Codegraph @

source4/torture/drs/python/repl_rodc.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 # Test replication scenarios involving an RODC
5 #
6 # Copyright (C) Catalyst.Net Ltd. 2017
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=dc1_dns_name [this is unused for the test, but it'll still try to connect]
26 # export SUBUNITRUN=$samba4srcdir/scripting/bin/subunitrun
27 # PYTHONPATH="$PYTHONPATH:$samba4srcdir/torture/drs/python" $SUBUNITRUN repl_rodc -U"$DOMAIN/$DC_USERNAME"%"$DC_PASSWORD"
28 #
29
30 import drs_base
31 import samba.tests
32 import ldb
33
34 from samba import WERRORError
35 from samba.join import DCJoinContext
36 from samba.dcerpc import drsuapi, misc, drsblobs, security
37 from samba.ndr import ndr_unpack, ndr_pack
38 from samba.samdb import BinaryDn
39 from samba.credentials import Credentials
40
41 import random
42 import time
43
44
45 def drs_get_rodc_partial_attribute_set(samdb, samdb1, exceptions=None):
46 '''get a list of attributes for RODC replication'''
47 if exceptions is None:
48 exceptions = []
49
50 partial_attribute_set = drsuapi.DsPartialAttributeSet()
51 partial_attribute_set.version = 1
52
53 attids = []
54
55 # the exact list of attids we send is quite critical. Note that
56 # we do ask for the secret attributes, but set SPECIAL_SECRET_PROCESSING
57 # to zero them out
58 schema_dn = samdb.get_schema_basedn()
59 res = samdb.search(base=schema_dn, scope=ldb.SCOPE_SUBTREE,
60 expression="objectClass=attributeSchema",
61 attrs=["lDAPDisplayName", "systemFlags",
62 "searchFlags"])
63
64 for r in res:
65 ldap_display_name = str(r["lDAPDisplayName"][0])
66 if "systemFlags" in r:
67 system_flags = str(r["systemFlags"][0])
68 if (int(system_flags) & (samba.dsdb.DS_FLAG_ATTR_NOT_REPLICATED |
69 samba.dsdb.DS_FLAG_ATTR_IS_CONSTRUCTED)):
70 continue
71 if "searchFlags" in r:
72 search_flags = str(r["searchFlags"][0])
73 if (int(search_flags) & samba.dsdb.SEARCH_FLAG_RODC_ATTRIBUTE):
74 continue
75 try:
76 attid = samdb1.get_attid_from_lDAPDisplayName(ldap_display_name)
77 if attid not in exceptions:
78 attids.append(int(attid))
79 except:
80 pass
81
82 # the attids do need to be sorted, or windows doesn't return
83 # all the attributes we need
84 attids.sort()
85 partial_attribute_set.attids = attids
86 partial_attribute_set.num_attids = len(attids)
87 return partial_attribute_set
88
89
90 class DrsRodcTestCase(drs_base.DrsBaseTestCase):
91 """Intended as a semi-black box test case for replication involving
92 an RODC."""
93
94 def setUp(self):
95 super(DrsRodcTestCase, self).setUp()
96 self.base_dn = self.ldb_dc1.get_default_basedn()
97
98 self.ou = samba.tests.create_test_ou(self.ldb_dc1, "test_drs_rodc")
99 self.allowed_group = "CN=Allowed RODC Password Replication Group,CN=Users,%s" % self.base_dn
100
101 self.site = self.ldb_dc1.server_site_name()
102 self.rodc_name = "TESTRODCDRS%s" % random.randint(1, 10000000)
103 self.rodc_pass = "password12#"
104 self.computer_dn = "CN=%s,OU=Domain Controllers,%s" % (self.rodc_name, self.base_dn)
105
106 self.rodc_ctx = DCJoinContext(server=self.ldb_dc1.host_dns_name(),
107 creds=self.get_credentials(),
108 lp=self.get_loadparm(), site=self.site,
109 netbios_name=self.rodc_name,
110 targetdir=None, domain=None,
111 machinepass=self.rodc_pass)
112 self._create_rodc(self.rodc_ctx)
113 self.rodc_ctx.create_tmp_samdb()
114 self.tmp_samdb = self.rodc_ctx.tmp_samdb
115
116 rodc_creds = Credentials()
117 rodc_creds.guess(self.rodc_ctx.lp)
118 rodc_creds.set_username(self.rodc_name + '$')
119 rodc_creds.set_password(self.rodc_pass)
120 self.rodc_creds = rodc_creds
121
122 (self.drs, self.drs_handle) = self._ds_bind(self.dnsname_dc1)
123 (self.rodc_drs, self.rodc_drs_handle) = self._ds_bind(self.dnsname_dc1, rodc_creds)
124
125 def tearDown(self):
126 self.rodc_ctx.cleanup_old_join()
127 super(DrsRodcTestCase, self).tearDown()
128
129 def test_admin_repl_secrets(self):
130 """
131 When a secret attribute is set to be replicated to an RODC with the
132 admin credentials, it should always replicate regardless of whether
133 or not it's in the Allowed RODC Password Replication Group.
134 """
135 rand = random.randint(1, 10000000)
136 expected_user_attributes = [drsuapi.DRSUAPI_ATTID_lmPwdHistory,
137 drsuapi.DRSUAPI_ATTID_supplementalCredentials,
138 drsuapi.DRSUAPI_ATTID_ntPwdHistory,
139 drsuapi.DRSUAPI_ATTID_unicodePwd,
140 drsuapi.DRSUAPI_ATTID_dBCSPwd]
141
142 user_name = "test_rodcA_%s" % rand
143 user_dn = "CN=%s,%s" % (user_name, self.ou)
144 self.ldb_dc1.add({
145 "dn": user_dn,
146 "objectclass": "user",
147 "sAMAccountName": user_name
148 })
149
150 # Store some secret on this user
151 self.ldb_dc1.setpassword("(sAMAccountName=%s)" % user_name, 'penguin12#', False, user_name)
152
153 req10 = self._getnc_req10(dest_dsa=str(self.rodc_ctx.ntds_guid),
154 invocation_id=self.ldb_dc1.get_invocation_id(),
155 nc_dn_str=user_dn,
156 exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET,
157 partial_attribute_set=drs_get_rodc_partial_attribute_set(self.ldb_dc1, self.tmp_samdb),
158 max_objects=133,
159 replica_flags=0)
160 (level, ctr) = self.drs.DsGetNCChanges(self.drs_handle, 10, req10)
161
162 # Check that the user has been added to msDSRevealedUsers
163 self._assert_in_revealed_users(user_dn, expected_user_attributes)
164
165 def test_admin_repl_secrets_DummyDN_GUID(self):
166 """
167 When a secret attribute is set to be replicated to an RODC with the
168 admin credentials, it should always replicate regardless of whether
169 or not it's in the Allowed RODC Password Replication Group.
170 """
171 rand = random.randint(1, 10000000)
172 expected_user_attributes = [drsuapi.DRSUAPI_ATTID_lmPwdHistory,
173 drsuapi.DRSUAPI_ATTID_supplementalCredentials,
174 drsuapi.DRSUAPI_ATTID_ntPwdHistory,
175 drsuapi.DRSUAPI_ATTID_unicodePwd,
176 drsuapi.DRSUAPI_ATTID_dBCSPwd]
177
178 user_name = "test_rodcA_%s" % rand
179 user_dn = "CN=%s,%s" % (user_name, self.ou)
180 self.ldb_dc1.add({
181 "dn": user_dn,
182 "objectclass": "user",
183 "sAMAccountName": user_name
184 })
185
186 res = self.ldb_dc1.search(base=user_dn, scope=ldb.SCOPE_BASE,
187 attrs=["objectGUID"])
188
189 user_guid = misc.GUID(res[0]["objectGUID"][0])
190
191 # Store some secret on this user
192 self.ldb_dc1.setpassword("(sAMAccountName=%s)" % user_name, 'penguin12#', False, user_name)
193
194 req10 = self._getnc_req10(dest_dsa=str(self.rodc_ctx.ntds_guid),
195 invocation_id=self.ldb_dc1.get_invocation_id(),
196 nc_dn_str="DummyDN",
197 nc_guid=user_guid,
198 exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET,
199 partial_attribute_set=drs_get_rodc_partial_attribute_set(self.ldb_dc1, self.tmp_samdb),
200 max_objects=133,
201 replica_flags=0)
202 try:
203 (level, ctr) = self.drs.DsGetNCChanges(self.drs_handle, 10, req10)
204 except WERRORError as e1:
205 (enum, estr) = e1.args
206 self.fail(f"DsGetNCChanges failed with {estr}")
207
208 # Check that the user has been added to msDSRevealedUsers
209 self._assert_in_revealed_users(user_dn, expected_user_attributes)
210
211 def test_rodc_repl_secrets(self):
212 """
213 When a secret attribute is set to be replicated to an RODC with
214 the RODC account credentials, it should not replicate if it's in
215 the Allowed RODC Password Replication Group. Once it is added to
216 the group, it should replicate.
217 """
218 rand = random.randint(1, 10000000)
219 expected_user_attributes = [drsuapi.DRSUAPI_ATTID_lmPwdHistory,
220 drsuapi.DRSUAPI_ATTID_supplementalCredentials,
221 drsuapi.DRSUAPI_ATTID_ntPwdHistory,
222 drsuapi.DRSUAPI_ATTID_unicodePwd,
223 drsuapi.DRSUAPI_ATTID_dBCSPwd]
224
225 user_name = "test_rodcB_%s" % rand
226 user_dn = "CN=%s,%s" % (user_name, self.ou)
227 self.ldb_dc1.add({
228 "dn": user_dn,
229 "objectclass": "user",
230 "sAMAccountName": user_name
231 })
232
233 # Store some secret on this user
234 self.ldb_dc1.setpassword("(sAMAccountName=%s)" % user_name, 'penguin12#', False, user_name)
235
236 req10 = self._getnc_req10(dest_dsa=str(self.rodc_ctx.ntds_guid),
237 invocation_id=self.ldb_dc1.get_invocation_id(),
238 nc_dn_str=user_dn,
239 exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET,
240 partial_attribute_set=drs_get_rodc_partial_attribute_set(self.ldb_dc1, self.tmp_samdb),
241 max_objects=133,
242 replica_flags=0)
243
244 try:
245 (level, ctr) = self.rodc_drs.DsGetNCChanges(self.rodc_drs_handle, 10, req10)
246 self.fail("Successfully replicated secrets to an RODC that shouldn't have been replicated.")
247 except WERRORError as e:
248 (enum, estr) = e.args
249 self.assertEqual(enum, 8630) # ERROR_DS_DRA_SECRETS_DENIED
250
251 # send the same request again and we should get the same response
252 try:
253 (level, ctr) = self.rodc_drs.DsGetNCChanges(self.rodc_drs_handle, 10, req10)
254 self.fail("Successfully replicated secrets to an RODC that shouldn't have been replicated.")
255 except WERRORError as e1:
256 (enum, estr) = e1.args
257 self.assertEqual(enum, 8630) # ERROR_DS_DRA_SECRETS_DENIED
258
259 # Retry with Administrator credentials, ignores password replication groups
260 (level, ctr) = self.drs.DsGetNCChanges(self.drs_handle, 10, req10)
261
262 # Check that the user has been added to msDSRevealedUsers
263 self._assert_in_revealed_users(user_dn, expected_user_attributes)
264
265 def test_rodc_repl_secrets_follow_on_req(self):
266 """
267 Checks that an RODC can't subvert an existing (valid) GetNCChanges
268 request to reveal secrets it shouldn't have access to.
269 """
270
271 # send an acceptable request that will match as many GUIDs as possible.
272 # Here we set the SPECIAL_SECRET_PROCESSING flag so that the request gets accepted.
273 # (On the server, this builds up the getnc_state->guids array)
274 req8 = self._exop_req8(dest_dsa=str(self.rodc_ctx.ntds_guid),
275 invocation_id=self.ldb_dc1.get_invocation_id(),
276 nc_dn_str=self.ldb_dc1.domain_dn(),
277 exop=drsuapi.DRSUAPI_EXOP_NONE,
278 max_objects=1,
279 replica_flags=drsuapi.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING)
280 (level, ctr) = self.rodc_drs.DsGetNCChanges(self.rodc_drs_handle, 8, req8)
281
282 # Get the next replication chunk, but set REPL_SECRET this time. This
283 # is following on the previous accepted request, but we've changed
284 # exop to now request secrets. This request should fail
285 try:
286 req8 = self._exop_req8(dest_dsa=str(self.rodc_ctx.ntds_guid),
287 invocation_id=self.ldb_dc1.get_invocation_id(),
288 nc_dn_str=self.ldb_dc1.domain_dn(),
289 exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET)
290 req8.highwatermark = ctr.new_highwatermark
291
292 (level, ctr) = self.rodc_drs.DsGetNCChanges(self.rodc_drs_handle, 8, req8)
293
294 self.fail("Successfully replicated secrets to an RODC that shouldn't have been replicated.")
295 except RuntimeError as e2:
296 (enum, estr) = e2.args
297 pass
298
299 def test_msDSRevealedUsers_admin(self):
300 """
301 When a secret attribute is to be replicated to an RODC, the contents
302 of the attribute should be added to the msDSRevealedUsers attribute
303 of the computer object corresponding to the RODC.
304 """
305
306 rand = random.randint(1, 10000000)
307 expected_user_attributes = [drsuapi.DRSUAPI_ATTID_lmPwdHistory,
308 drsuapi.DRSUAPI_ATTID_supplementalCredentials,
309 drsuapi.DRSUAPI_ATTID_ntPwdHistory,
310 drsuapi.DRSUAPI_ATTID_unicodePwd,
311 drsuapi.DRSUAPI_ATTID_dBCSPwd]
312
313 # Add a user on DC1, add it to allowed password replication
314 # group, and replicate to RODC with EXOP_REPL_SECRETS
315 user_name = "test_rodcC_%s" % rand
316 password = "password12#"
317 user_dn = "CN=%s,%s" % (user_name, self.ou)
318 self.ldb_dc1.add({
319 "dn": user_dn,
320 "objectclass": "user",
321 "sAMAccountName": user_name
322 })
323
324 # Store some secret on this user
325 self.ldb_dc1.setpassword("(sAMAccountName=%s)" % user_name, password, False, user_name)
326
327 self.ldb_dc1.add_remove_group_members("Allowed RODC Password Replication Group",
328 [user_name],
329 add_members_operation=True)
330
331 req10 = self._getnc_req10(dest_dsa=str(self.rodc_ctx.ntds_guid),
332 invocation_id=self.ldb_dc1.get_invocation_id(),
333 nc_dn_str=user_dn,
334 exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET,
335 partial_attribute_set=drs_get_rodc_partial_attribute_set(self.ldb_dc1, self.tmp_samdb),
336 max_objects=133,
337 replica_flags=0)
338 (level, ctr) = self.drs.DsGetNCChanges(self.drs_handle, 10, req10)
339
340 # Check that the user has been added to msDSRevealedUsers
341 (packed_attrs_1, unpacked_attrs_1) = self._assert_in_revealed_users(user_dn, expected_user_attributes)
342
343 # Change the user's password on DC1
344 self.ldb_dc1.setpassword("(sAMAccountName=%s)" % user_name, password + "1", False, user_name)
345
346 (packed_attrs_2, unpacked_attrs_2) = self._assert_in_revealed_users(user_dn, expected_user_attributes)
347 self._assert_attrlist_equals(unpacked_attrs_1, unpacked_attrs_2)
348
349 # Replicate to RODC again with EXOP_REPL_SECRETS
350 req10 = self._getnc_req10(dest_dsa=str(self.rodc_ctx.ntds_guid),
351 invocation_id=self.ldb_dc1.get_invocation_id(),
352 nc_dn_str=user_dn,
353 exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET,
354 partial_attribute_set=drs_get_rodc_partial_attribute_set(self.ldb_dc1, self.tmp_samdb),
355 max_objects=133,
356 replica_flags=0)
357 (level, ctr) = self.drs.DsGetNCChanges(self.drs_handle, 10, req10)
358
359 # This is important for Windows, because the entry won't have been
360 # updated in time if we don't have it. Even with this sleep, it only
361 # passes some of the time...
362 time.sleep(5)
363
364 # Check that the entry in msDSRevealedUsers has been updated
365 (packed_attrs_3, unpacked_attrs_3) = self._assert_in_revealed_users(user_dn, expected_user_attributes)
366 self._assert_attrlist_changed(unpacked_attrs_2, unpacked_attrs_3, expected_user_attributes)
367
368 # We should be able to delete the user
369 self.ldb_dc1.deleteuser(user_name)
370
371 res = self.ldb_dc1.search(scope=ldb.SCOPE_BASE, base=self.computer_dn,
372 attrs=["msDS-RevealedUsers"])
373 self.assertFalse("msDS-RevealedUsers" in res[0])
374
375 def test_msDSRevealedUsers(self):
376 """
377 When a secret attribute is to be replicated to an RODC, the contents
378 of the attribute should be added to the msDSRevealedUsers attribute
379 of the computer object corresponding to the RODC.
380 """
381
382 rand = random.randint(1, 10000000)
383 expected_user_attributes = [drsuapi.DRSUAPI_ATTID_lmPwdHistory,
384 drsuapi.DRSUAPI_ATTID_supplementalCredentials,
385 drsuapi.DRSUAPI_ATTID_ntPwdHistory,
386 drsuapi.DRSUAPI_ATTID_unicodePwd,
387 drsuapi.DRSUAPI_ATTID_dBCSPwd]
388
389 # Add a user on DC1, add it to allowed password replication
390 # group, and replicate to RODC with EXOP_REPL_SECRETS
391 user_name = "test_rodcD_%s" % rand
392 password = "password12#"
393 user_dn = "CN=%s,%s" % (user_name, self.ou)
394 self.ldb_dc1.add({
395 "dn": user_dn,
396 "objectclass": "user",
397 "sAMAccountName": user_name
398 })
399
400 # Store some secret on this user
401 self.ldb_dc1.setpassword("(sAMAccountName=%s)" % user_name, password, False, user_name)
402
403 self.ldb_dc1.add_remove_group_members("Allowed RODC Password Replication Group",
404 [user_name],
405 add_members_operation=True)
406
407 req10 = self._getnc_req10(dest_dsa=str(self.rodc_ctx.ntds_guid),
408 invocation_id=self.ldb_dc1.get_invocation_id(),
409 nc_dn_str=user_dn,
410 exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET,
411 partial_attribute_set=drs_get_rodc_partial_attribute_set(self.ldb_dc1, self.tmp_samdb),
412 max_objects=133,
413 replica_flags=0)
414 (level, ctr) = self.drs.DsGetNCChanges(self.drs_handle, 10, req10)
415
416 # Check that the user has been added to msDSRevealedUsers
417 (packed_attrs_1, unpacked_attrs_1) = self._assert_in_revealed_users(user_dn, expected_user_attributes)
418
419 # Change the user's password on DC1
420 self.ldb_dc1.setpassword("(sAMAccountName=%s)" % user_name, password + "1", False, user_name)
421
422 (packed_attrs_2, unpacked_attrs_2) = self._assert_in_revealed_users(user_dn, expected_user_attributes)
423 self._assert_attrlist_equals(unpacked_attrs_1, unpacked_attrs_2)
424
425 # Replicate to RODC again with EXOP_REPL_SECRETS
426 req10 = self._getnc_req10(dest_dsa=str(self.rodc_ctx.ntds_guid),
427 invocation_id=self.ldb_dc1.get_invocation_id(),
428 nc_dn_str=user_dn,
429 exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET,
430 partial_attribute_set=drs_get_rodc_partial_attribute_set(self.ldb_dc1, self.tmp_samdb),
431 max_objects=133,
432 replica_flags=0)
433 (level, ctr) = self.rodc_drs.DsGetNCChanges(self.rodc_drs_handle, 10, req10)
434
435 # This is important for Windows, because the entry won't have been
436 # updated in time if we don't have it. Even with this sleep, it only
437 # passes some of the time...
438 time.sleep(5)
439
440 # Check that the entry in msDSRevealedUsers has been updated
441 (packed_attrs_3, unpacked_attrs_3) = self._assert_in_revealed_users(user_dn, expected_user_attributes)
442 self._assert_attrlist_changed(unpacked_attrs_2, unpacked_attrs_3, expected_user_attributes)
443
444 # We should be able to delete the user
445 self.ldb_dc1.deleteuser(user_name)
446
447 res = self.ldb_dc1.search(scope=ldb.SCOPE_BASE, base=self.computer_dn,
448 attrs=["msDS-RevealedUsers"])
449 self.assertFalse("msDS-RevealedUsers" in res[0])
450
451 def test_msDSRevealedUsers_pas(self):
452 """
453 If we provide a Partial Attribute Set when replicating to an RODC,
454 we should ignore it and replicate all of the secret attributes anyway
455 msDSRevealedUsers attribute.
456 """
457 rand = random.randint(1, 10000000)
458 expected_user_attributes = [drsuapi.DRSUAPI_ATTID_lmPwdHistory,
459 drsuapi.DRSUAPI_ATTID_supplementalCredentials,
460 drsuapi.DRSUAPI_ATTID_ntPwdHistory,
461 drsuapi.DRSUAPI_ATTID_unicodePwd,
462 drsuapi.DRSUAPI_ATTID_dBCSPwd]
463 pas_exceptions = [drsuapi.DRSUAPI_ATTID_lmPwdHistory,
464 drsuapi.DRSUAPI_ATTID_supplementalCredentials,
465 drsuapi.DRSUAPI_ATTID_ntPwdHistory,
466 drsuapi.DRSUAPI_ATTID_dBCSPwd]
467
468 # Add a user on DC1, add it to allowed password replication
469 # group, and replicate to RODC with EXOP_REPL_SECRETS
470 user_name = "test_rodcE_%s" % rand
471 password = "password12#"
472 user_dn = "CN=%s,%s" % (user_name, self.ou)
473 self.ldb_dc1.add({
474 "dn": user_dn,
475 "objectclass": "user",
476 "sAMAccountName": user_name
477 })
478
479 # Store some secret on this user
480 self.ldb_dc1.setpassword("(sAMAccountName=%s)" % user_name, password, False, user_name)
481
482 self.ldb_dc1.add_remove_group_members("Allowed RODC Password Replication Group",
483 [user_name],
484 add_members_operation=True)
485
486 pas = drs_get_rodc_partial_attribute_set(self.ldb_dc1, self.tmp_samdb, exceptions=pas_exceptions)
487 req10 = self._getnc_req10(dest_dsa=str(self.rodc_ctx.ntds_guid),
488 invocation_id=self.ldb_dc1.get_invocation_id(),
489 nc_dn_str=user_dn,
490 exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET,
491 partial_attribute_set=pas,
492 max_objects=133,
493 replica_flags=0)
494 (level, ctr) = self.drs.DsGetNCChanges(self.drs_handle, 10, req10)
495
496 # Make sure that we still replicate the secrets
497 for attribute in ctr.first_object.object.attribute_ctr.attributes:
498 if attribute.attid in pas_exceptions:
499 pas_exceptions.remove(attribute.attid)
500 for attribute in pas_exceptions:
501 self.fail("%d was not replicated even though the partial attribute set should be ignored."
502 % attribute)
503
504 # Check that the user has been added to msDSRevealedUsers
505 (packed_attrs_1, unpacked_attrs_1) = self._assert_in_revealed_users(user_dn, expected_user_attributes)
506
507 def test_msDSRevealedUsers_using_other_RODC(self):
508 """
509 Ensure that the machine account is tied to the destination DSA.
510 """
511 # Create a new identical RODC with just the first letter missing
512 other_rodc_name = self.rodc_name[1:]
513 other_rodc_ctx = DCJoinContext(server=self.ldb_dc1.host_dns_name(),
514 creds=self.get_credentials(),
515 lp=self.get_loadparm(), site=self.site,
516 netbios_name=other_rodc_name,
517 targetdir=None, domain=None,
518 machinepass=self.rodc_pass)
519 self._create_rodc(other_rodc_ctx)
520
521 other_rodc_creds = Credentials()
522 other_rodc_creds.guess(other_rodc_ctx.lp)
523 other_rodc_creds.set_username(other_rodc_name + '$')
524 other_rodc_creds.set_password(self.rodc_pass)
525
526 (other_rodc_drs, other_rodc_drs_handle) = self._ds_bind(self.dnsname_dc1, other_rodc_creds)
527
528 rand = random.randint(1, 10000000)
529
530 user_name = "test_rodcF_%s" % rand
531 user_dn = "CN=%s,%s" % (user_name, self.ou)
532 self.ldb_dc1.add({
533 "dn": user_dn,
534 "objectclass": "user",
535 "sAMAccountName": user_name
536 })
537
538 # Store some secret on this user
539 self.ldb_dc1.setpassword("(sAMAccountName=%s)" % user_name, 'penguin12#', False, user_name)
540 self.ldb_dc1.add_remove_group_members("Allowed RODC Password Replication Group",
541 [user_name],
542 add_members_operation=True)
543
544 req10 = self._getnc_req10(dest_dsa=str(other_rodc_ctx.ntds_guid),
545 invocation_id=self.ldb_dc1.get_invocation_id(),
546 nc_dn_str=user_dn,
547 exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET,
548 partial_attribute_set=drs_get_rodc_partial_attribute_set(self.ldb_dc1, self.tmp_samdb),
549 max_objects=133,
550 replica_flags=0)
551
552 try:
553 (level, ctr) = self.rodc_drs.DsGetNCChanges(self.rodc_drs_handle, 10, req10)
554 self.fail("Successfully replicated secrets to an RODC that shouldn't have been replicated.")
555 except WERRORError as e3:
556 (enum, estr) = e3.args
557 self.assertEqual(enum, 8630) # ERROR_DS_DRA_SECRETS_DENIED
558
559 req10 = self._getnc_req10(dest_dsa=str(self.rodc_ctx.ntds_guid),
560 invocation_id=self.ldb_dc1.get_invocation_id(),
561 nc_dn_str=user_dn,
562 exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET,
563 partial_attribute_set=drs_get_rodc_partial_attribute_set(self.ldb_dc1, self.tmp_samdb),
564 max_objects=133,
565 replica_flags=0)
566
567 try:
568 (level, ctr) = other_rodc_drs.DsGetNCChanges(other_rodc_drs_handle, 10, req10)
569 self.fail("Successfully replicated secrets to an RODC that shouldn't have been replicated.")
570 except WERRORError as e4:
571 (enum, estr) = e4.args
572 self.assertEqual(enum, 8630) # ERROR_DS_DRA_SECRETS_DENIED
573
574 def test_msDSRevealedUsers_local_deny_allow(self):
575 """
576 Ensure that the deny trumps allow, and we can modify these
577 attributes directly instead of the global groups.
578
579 This may fail on Windows due to tokenGroup calculation caching.
580 """
581 rand = random.randint(1, 10000000)
582 expected_user_attributes = [drsuapi.DRSUAPI_ATTID_lmPwdHistory,
583 drsuapi.DRSUAPI_ATTID_supplementalCredentials,
584 drsuapi.DRSUAPI_ATTID_ntPwdHistory,
585 drsuapi.DRSUAPI_ATTID_unicodePwd,
586 drsuapi.DRSUAPI_ATTID_dBCSPwd]
587
588 # Add a user on DC1, add it to allowed password replication
589 # group, and replicate to RODC with EXOP_REPL_SECRETS
590 user_name = "test_rodcF_%s" % rand
591 password = "password12#"
592 user_dn = "CN=%s,%s" % (user_name, self.ou)
593 self.ldb_dc1.add({
594 "dn": user_dn,
595 "objectclass": "user",
596 "sAMAccountName": user_name
597 })
598
599 # Store some secret on this user
600 self.ldb_dc1.setpassword("(sAMAccountName=%s)" % user_name, password, False, user_name)
601
602 req10 = self._getnc_req10(dest_dsa=str(self.rodc_ctx.ntds_guid),
603 invocation_id=self.ldb_dc1.get_invocation_id(),
604 nc_dn_str=user_dn,
605 exop=drsuapi.DRSUAPI_EXOP_REPL_SECRET,
606 partial_attribute_set=drs_get_rodc_partial_attribute_set(self.ldb_dc1, self.tmp_samdb),
607 max_objects=133,
608 replica_flags=0)
609
610 m = ldb.Message()
611 m.dn = ldb.Dn(self.ldb_dc1, self.computer_dn)
612
613 m["msDS-RevealOnDemandGroup"] = \
614 ldb.MessageElement(user_dn, ldb.FLAG_MOD_ADD,
615 "msDS-RevealOnDemandGroup")
616 self.ldb_dc1.modify(m)
617
618 # In local allow, should be success
619 try:
620 (level, ctr) = self.rodc_drs.DsGetNCChanges(self.rodc_drs_handle, 10, req10)
621 except:
622 self.fail("Should have succeeded when in local allow group")
623
624 self._assert_in_revealed_users(user_dn, expected_user_attributes)
625
626 (self.rodc_drs, self.rodc_drs_handle) = self._ds_bind(self.dnsname_dc1, self.rodc_creds)
627
628 m = ldb.Message()
629 m.dn = ldb.Dn(self.ldb_dc1, self.computer_dn)
630
631 m["msDS-NeverRevealGroup"] = \
632 ldb.MessageElement(user_dn, ldb.FLAG_MOD_ADD,
633 "msDS-NeverRevealGroup")
634 self.ldb_dc1.modify(m)
635
636 # In local allow and deny, should be failure
637 try:
638 (level, ctr) = self.rodc_drs.DsGetNCChanges(self.rodc_drs_handle, 10, req10)
639 self.fail("Successfully replicated secrets to an RODC that shouldn't have been replicated.")
640 except WERRORError as e5:
641 (enum, estr) = e5.args
642 self.assertEqual(enum, 8630) # ERROR_DS_DRA_SECRETS_DENIED
643
644 m = ldb.Message()
645 m.dn = ldb.Dn(self.ldb_dc1, self.computer_dn)
646
647 m["msDS-RevealOnDemandGroup"] = \
648 ldb.MessageElement(user_dn, ldb.FLAG_MOD_DELETE,
649 "msDS-RevealOnDemandGroup")
650 self.ldb_dc1.modify(m)
651
652 # In local deny, should be failure
653 (self.rodc_drs, self.rodc_drs_handle) = self._ds_bind(self.dnsname_dc1, self.rodc_creds)
654 try:
655 (level, ctr) = self.rodc_drs.DsGetNCChanges(self.rodc_drs_handle, 10, req10)
656 self.fail("Successfully replicated secrets to an RODC that shouldn't have been replicated.")
657 except WERRORError as e6:
658 (enum, estr) = e6.args
659 self.assertEqual(enum, 8630) # ERROR_DS_DRA_SECRETS_DENIED
660
661 def _assert_in_revealed_users(self, user_dn, attrlist):
662 res = self.ldb_dc1.search(scope=ldb.SCOPE_BASE, base=self.computer_dn,
663 attrs=["msDS-RevealedUsers"])
664 revealed_users = res[0]["msDS-RevealedUsers"]
665 actual_attrids = []
666 packed_attrs = []
667 unpacked_attrs = []
668 for attribute in revealed_users:
669 attribute = attribute.decode('utf8')
670 dsdb_dn = BinaryDn(self.ldb_dc1, attribute)
671 metadata = ndr_unpack(drsblobs.replPropertyMetaData1, dsdb_dn.binary)
672 if user_dn in attribute:
673 unpacked_attrs.append(metadata)
674 packed_attrs.append(dsdb_dn.binary)
675 actual_attrids.append(metadata.attid)
676
677 self.assertEqual(sorted(actual_attrids), sorted(attrlist))
678
679 return (packed_attrs, unpacked_attrs)
680
681 def _assert_attrlist_equals(self, list_1, list_2):
682 return self._assert_attrlist_changed(list_1, list_2, [], num_changes=0, expected_new_usn=False)
683
684 def _assert_attrlist_changed(self, list_1, list_2, changed_attributes, num_changes=1, expected_new_usn=True):
685 for i in range(len(list_2)):
686 self.assertEqual(list_1[i].attid, list_2[i].attid)
687 self.assertEqual(list_1[i].originating_invocation_id, list_2[i].originating_invocation_id)
688 self.assertEqual(list_1[i].version + num_changes, list_2[i].version)
689
690 if expected_new_usn:
691 self.assertTrue(list_1[i].originating_usn < list_2[i].originating_usn)
692 self.assertTrue(list_1[i].local_usn < list_2[i].local_usn)
693 else:
694 self.assertEqual(list_1[i].originating_usn, list_2[i].originating_usn)
695 self.assertEqual(list_1[i].local_usn, list_2[i].local_usn)
696
697 if list_1[i].attid in changed_attributes:
698 # We do the changes too quickly, so unless we put sleeps
699 # in between calls, these remain the same. Checking the USNs
700 # is enough.
701 pass
702 #self.assertTrue(list_1[i].originating_change_time < list_2[i].originating_change_time)
703 else:
704 self.assertEqual(list_1[i].originating_change_time, list_2[i].originating_change_time)
705
706 def _create_rodc(self, ctx):
707 ctx.nc_list = [ctx.base_dn, ctx.config_dn, ctx.schema_dn]
708 ctx.full_nc_list = [ctx.base_dn, ctx.config_dn, ctx.schema_dn]
709 ctx.krbtgt_dn = "CN=krbtgt_%s,CN=Users,%s" % (ctx.myname, ctx.base_dn)
710
711 ctx.never_reveal_sid = ["<SID=%s-%s>" % (ctx.domsid, security.DOMAIN_RID_RODC_DENY),
712 "<SID=%s>" % security.SID_BUILTIN_ADMINISTRATORS,
713 "<SID=%s>" % security.SID_BUILTIN_SERVER_OPERATORS,
714 "<SID=%s>" % security.SID_BUILTIN_BACKUP_OPERATORS,
715 "<SID=%s>" % security.SID_BUILTIN_ACCOUNT_OPERATORS]
716 ctx.reveal_sid = "<SID=%s-%s>" % (ctx.domsid, security.DOMAIN_RID_RODC_ALLOW)
717
718 mysid = ctx.get_mysid()
719 admin_dn = "<SID=%s>" % mysid
720 ctx.managedby = admin_dn
721
722 ctx.userAccountControl = (samba.dsdb.UF_WORKSTATION_TRUST_ACCOUNT |
723 samba.dsdb.UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION |
724 samba.dsdb.UF_PARTIAL_SECRETS_ACCOUNT)
725
726 ctx.connection_dn = "CN=RODC Connection (FRS),%s" % ctx.ntds_dn
727 ctx.secure_channel_type = misc.SEC_CHAN_RODC
728 ctx.RODC = True
729 ctx.replica_flags = (drsuapi.DRSUAPI_DRS_INIT_SYNC |
730 drsuapi.DRSUAPI_DRS_PER_SYNC |
731 drsuapi.DRSUAPI_DRS_GET_ANC |
732 drsuapi.DRSUAPI_DRS_NEVER_SYNCED |
733 drsuapi.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING)
734
735 ctx.join_add_objects()