| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Test conflict scenarios on the RODC |
| 5 |
# |
| 6 |
# Copyright (C) Kamen Mazdrashki <[email protected]> 2011 |
| 7 |
# Copyright (C) Catalyst.NET Ltd 2018 |
| 8 |
# |
| 9 |
# This program is free software; you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU General Public License as published by |
| 11 |
# the Free Software Foundation; either version 3 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# This program is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU General Public License |
| 20 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
# |
| 22 |
|
| 23 |
# |
| 24 |
# Usage: |
| 25 |
# export DC1=dc1_dns_name |
| 26 |
# export DC2=dc2_dns_name (RODC) |
| 27 |
# export SUBUNITRUN=$samba4srcdir/scripting/bin/subunitrun |
| 28 |
# PYTHONPATH="$PYTHONPATH:$samba4srcdir/torture/drs/python" $SUBUNITRUN replica_sync_rodc -U"$DOMAIN/$DC_USERNAME"%"$DC_PASSWORD" |
| 29 |
# |
| 30 |
|
| 31 |
import drs_base |
| 32 |
import samba.tests |
| 33 |
import time |
| 34 |
import ldb |
| 35 |
from samba.common import get_string |
| 36 |
|
| 37 |
from ldb import ( |
| 38 |
SCOPE_BASE, LdbError, ERR_NO_SUCH_OBJECT) |
| 39 |
|
| 40 |
|
| 41 |
class DrsReplicaSyncTestCase(drs_base.DrsBaseTestCase): |
| 42 |
"""Intended as a black box test case for DsReplicaSync |
| 43 |
implementation. It should test the behavior of this |
| 44 |
case in cases when inbound replication is disabled""" |
| 45 |
|
| 46 |
def setUp(self): |
| 47 |
super(DrsReplicaSyncTestCase, self).setUp() |
| 48 |
self._disable_all_repl(self.dnsname_dc1) |
| 49 |
self.ou1 = None |
| 50 |
self.ou2 = None |
| 51 |
|
| 52 |
def tearDown(self): |
| 53 |
# re-enable replication |
| 54 |
self._enable_all_repl(self.dnsname_dc1) |
| 55 |
|
| 56 |
super(DrsReplicaSyncTestCase, self).tearDown() |
| 57 |
|
| 58 |
def _create_ou(self, samdb, name): |
| 59 |
ldif = """ |
| 60 |
dn: %s,%s |
| 61 |
objectClass: organizationalUnit |
| 62 |
""" % (name, self.domain_dn) |
| 63 |
samdb.add_ldif(ldif) |
| 64 |
res = samdb.search(base="%s,%s" % (name, self.domain_dn), |
| 65 |
scope=SCOPE_BASE, attrs=["objectGUID"]) |
| 66 |
return get_string(self._GUID_string(res[0]["objectGUID"][0])) |
| 67 |
|
| 68 |
def _check_deleted(self, sam_ldb, guid): |
| 69 |
# search the user by guid as it may be deleted |
| 70 |
res = sam_ldb.search(base='<GUID=%s>' % guid, |
| 71 |
controls=["show_deleted:1"], |
| 72 |
attrs=["isDeleted", "objectCategory", "ou"]) |
| 73 |
self.assertEqual(len(res), 1) |
| 74 |
ou_cur = res[0] |
| 75 |
# Deleted Object base DN |
| 76 |
dodn = self._deleted_objects_dn(sam_ldb) |
| 77 |
# now check properties of the user |
| 78 |
name_cur = ou_cur["ou"][0] |
| 79 |
self.assertEqual(ou_cur["isDeleted"][0], "TRUE") |
| 80 |
self.assertTrue(not("objectCategory" in ou_cur)) |
| 81 |
self.assertTrue(dodn in str(ou_cur["dn"]), |
| 82 |
"OU %s is deleted but it is not located under %s!" % (name_cur, dodn)) |
| 83 |
|
| 84 |
def test_ReplConflictsRODC(self): |
| 85 |
"""Tests that objects created in conflict become conflict DNs""" |
| 86 |
# Replicate all objects to RODC beforehand |
| 87 |
self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, forced=True) |
| 88 |
|
| 89 |
# Create conflicting objects on DC1 and DC2, with DC1 object created first |
| 90 |
name = "OU=Test RODC Conflict" |
| 91 |
self.ou1 = self._create_ou(self.ldb_dc1, name) |
| 92 |
|
| 93 |
# Replicate single object |
| 94 |
self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, |
| 95 |
nc_dn="%s,%s" % (name, self.domain_dn), |
| 96 |
local=True, single=True, forced=True) |
| 97 |
|
| 98 |
# Delete the object, so another can be added |
| 99 |
self.ldb_dc1.delete('<GUID=%s>' % self.ou1) |
| 100 |
|
| 101 |
# Create a conflicting DN as it would appear to the RODC |
| 102 |
self.ou2 = self._create_ou(self.ldb_dc1, name) |
| 103 |
|
| 104 |
try: |
| 105 |
self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, |
| 106 |
nc_dn="%s,%s" % (name, self.domain_dn), |
| 107 |
local=True, single=True, forced=True) |
| 108 |
except: |
| 109 |
# Cleanup the object |
| 110 |
self.ldb_dc1.delete('<GUID=%s>' % self.ou2) |
| 111 |
return |
| 112 |
|
| 113 |
# Replicate cannot succeed, HWM would be updated incorrectly. |
| 114 |
self.fail("DRS replicate should have failed.") |
| 115 |
|
| 116 |
def test_ReplConflictsRODCRename(self): |
| 117 |
"""Tests that objects created in conflict become conflict DNs""" |
| 118 |
# Replicate all objects to RODC beforehand |
| 119 |
self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, forced=True) |
| 120 |
|
| 121 |
# Create conflicting objects on DC1 and DC2, with DC1 object created first |
| 122 |
name = "OU=Test RODC Rename Conflict" |
| 123 |
self.ou1 = self._create_ou(self.ldb_dc1, name) |
| 124 |
|
| 125 |
# Replicate single object |
| 126 |
self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, |
| 127 |
nc_dn="%s,%s" % (name, self.domain_dn), |
| 128 |
local=True, single=True, forced=True) |
| 129 |
|
| 130 |
# Create a non-conflicting DN to rename as conflicting |
| 131 |
free_name = "OU=Test RODC Rename No Conflict" |
| 132 |
self.ou2 = self._create_ou(self.ldb_dc1, free_name) |
| 133 |
|
| 134 |
self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, |
| 135 |
nc_dn="%s,%s" % (free_name, self.domain_dn), |
| 136 |
local=True, single=True, forced=True) |
| 137 |
|
| 138 |
# Delete the object, so we can rename freely |
| 139 |
# DO NOT REPLICATE TO THE RODC |
| 140 |
self.ldb_dc1.delete('<GUID=%s>' % self.ou1) |
| 141 |
|
| 142 |
# Collide the name from the RODC perspective |
| 143 |
self.ldb_dc1.rename("<GUID=%s>" % self.ou2, "%s,%s" % (name, self.domain_dn)) |
| 144 |
|
| 145 |
try: |
| 146 |
self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, |
| 147 |
nc_dn="%s,%s" % (name, self.domain_dn), |
| 148 |
local=True, single=True, forced=True) |
| 149 |
except: |
| 150 |
# Cleanup the object |
| 151 |
self.ldb_dc1.delete('<GUID=%s>' % self.ou2) |
| 152 |
return |
| 153 |
|
| 154 |
# Replicate cannot succeed, HWM would be updated incorrectly. |
| 155 |
self.fail("DRS replicate should have failed.") |