| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Unix SMB/CIFS implementation. |
| 5 |
# Copyright (C) Kamen Mazdrashki <[email protected]> 2011 |
| 6 |
# Copyright (C) Andrew Bartlett <[email protected]> 2016 |
| 7 |
# Copyright (C) Catalyst IT Ltd. 2016 |
| 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 |
import sys |
| 24 |
import time |
| 25 |
import os |
| 26 |
import ldb |
| 27 |
|
| 28 |
sys.path.insert(0, "bin/python") |
| 29 |
import samba.tests |
| 30 |
from samba.tests.samba_tool.base import SambaToolCmdTest |
| 31 |
from samba import dsdb |
| 32 |
from samba.dcerpc import drsuapi, misc, drsblobs, security |
| 33 |
from samba.ndr import ndr_unpack, ndr_pack |
| 34 |
from samba.drs_utils import drs_DsBind |
| 35 |
from samba import gensec |
| 36 |
from ldb import ( |
| 37 |
SCOPE_BASE, |
| 38 |
Message, |
| 39 |
FLAG_MOD_REPLACE, |
| 40 |
) |
| 41 |
from samba.common import cmp |
| 42 |
from samba.common import get_string |
| 43 |
|
| 44 |
|
| 45 |
class DrsBaseTestCase(SambaToolCmdTest): |
| 46 |
"""Base class implementation for all DRS python tests. |
| 47 |
It is intended to provide common initialization and |
| 48 |
and functionality used by all DRS tests in drs/python |
| 49 |
test package. For instance, DC1 and DC2 are always used |
| 50 |
to pass URLs for DCs to test against""" |
| 51 |
|
| 52 |
def setUp(self): |
| 53 |
super(DrsBaseTestCase, self).setUp() |
| 54 |
creds = self.get_credentials() |
| 55 |
creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL) |
| 56 |
|
| 57 |
# connect to DCs |
| 58 |
self.url_dc1 = samba.tests.env_get_var_value("DC1") |
| 59 |
(self.ldb_dc1, self.info_dc1) = samba.tests.connect_samdb_ex(self.url_dc1, |
| 60 |
ldap_only=True) |
| 61 |
self.url_dc2 = samba.tests.env_get_var_value("DC2") |
| 62 |
(self.ldb_dc2, self.info_dc2) = samba.tests.connect_samdb_ex(self.url_dc2, |
| 63 |
ldap_only=True) |
| 64 |
self.test_ldb_dc = self.ldb_dc1 |
| 65 |
|
| 66 |
# cache some of RootDSE props |
| 67 |
self.schema_dn = str(self.info_dc1["schemaNamingContext"][0]) |
| 68 |
self.domain_dn = str(self.info_dc1["defaultNamingContext"][0]) |
| 69 |
self.config_dn = str(self.info_dc1["configurationNamingContext"][0]) |
| 70 |
self.forest_level = int(self.info_dc1["forestFunctionality"][0]) |
| 71 |
|
| 72 |
# we will need DCs DNS names for 'samba-tool drs' command |
| 73 |
self.dnsname_dc1 = str(self.info_dc1["dnsHostName"][0]) |
| 74 |
self.dnsname_dc2 = str(self.info_dc2["dnsHostName"][0]) |
| 75 |
|
| 76 |
# for debugging the test code |
| 77 |
self._debug = False |
| 78 |
|
| 79 |
def tearDown(self): |
| 80 |
super(DrsBaseTestCase, self).tearDown() |
| 81 |
|
| 82 |
def set_test_ldb_dc(self, ldb_dc): |
| 83 |
"""Sets which DC's LDB we perform operations on during the test""" |
| 84 |
self.test_ldb_dc = ldb_dc |
| 85 |
|
| 86 |
def _GUID_string(self, guid): |
| 87 |
return get_string(self.test_ldb_dc.schema_format_value("objectGUID", guid)) |
| 88 |
|
| 89 |
def _ldap_schemaUpdateNow(self, sam_db): |
| 90 |
rec = {"dn": "", |
| 91 |
"schemaUpdateNow": "1"} |
| 92 |
m = Message.from_dict(sam_db, rec, FLAG_MOD_REPLACE) |
| 93 |
sam_db.modify(m) |
| 94 |
|
| 95 |
def _deleted_objects_dn(self, sam_ldb): |
| 96 |
wkdn = "<WKGUID=18E2EA80684F11D2B9AA00C04F79F805,%s>" % self.domain_dn |
| 97 |
res = sam_ldb.search(base=wkdn, |
| 98 |
scope=SCOPE_BASE, |
| 99 |
controls=["show_deleted:1"]) |
| 100 |
self.assertEqual(len(res), 1) |
| 101 |
return str(res[0]["dn"]) |
| 102 |
|
| 103 |
def _lost_and_found_dn(self, sam_ldb, nc): |
| 104 |
wkdn = "<WKGUID=%s,%s>" % (dsdb.DS_GUID_LOSTANDFOUND_CONTAINER, nc) |
| 105 |
res = sam_ldb.search(base=wkdn, |
| 106 |
scope=SCOPE_BASE) |
| 107 |
self.assertEqual(len(res), 1) |
| 108 |
return str(res[0]["dn"]) |
| 109 |
|
| 110 |
def _make_obj_name(self, prefix): |
| 111 |
return prefix + time.strftime("%s", time.gmtime()) |
| 112 |
|
| 113 |
def _samba_tool_cmd_list(self, drs_command): |
| 114 |
# make command line credentials string |
| 115 |
|
| 116 |
# If test runs on windows then it can provide its own auth string |
| 117 |
if hasattr(self, 'cmdline_auth'): |
| 118 |
cmdline_auth = self.cmdline_auth |
| 119 |
else: |
| 120 |
ccache_name = self.get_creds_ccache_name() |
| 121 |
|
| 122 |
# Tunnel the command line credentials down to the |
| 123 |
# subcommand to avoid a new kinit |
| 124 |
cmdline_auth = "--use-krb5-ccache=%s" % ccache_name |
| 125 |
|
| 126 |
# bin/samba-tool drs <drs_command> <cmdline_auth> |
| 127 |
return ["drs", drs_command, cmdline_auth] |
| 128 |
|
| 129 |
def _net_drs_replicate(self, DC, fromDC, nc_dn=None, forced=True, |
| 130 |
local=False, full_sync=False, single=False): |
| 131 |
if nc_dn is None: |
| 132 |
nc_dn = self.domain_dn |
| 133 |
# make base command line |
| 134 |
samba_tool_cmdline = self._samba_tool_cmd_list("replicate") |
| 135 |
# bin/samba-tool drs replicate <Dest_DC_NAME> <Src_DC_NAME> <Naming Context> |
| 136 |
samba_tool_cmdline += [DC, fromDC, nc_dn] |
| 137 |
|
| 138 |
if forced: |
| 139 |
samba_tool_cmdline += ["--sync-forced"] |
| 140 |
if local: |
| 141 |
samba_tool_cmdline += ["--local"] |
| 142 |
if full_sync: |
| 143 |
samba_tool_cmdline += ["--full-sync"] |
| 144 |
if single: |
| 145 |
samba_tool_cmdline += ["--single-object"] |
| 146 |
|
| 147 |
(result, out, err) = self.runsubcmd(*samba_tool_cmdline) |
| 148 |
self.assertCmdSuccess(result, out, err) |
| 149 |
self.assertEqual(err, "", "Shouldn't be any error messages") |
| 150 |
|
| 151 |
def _enable_inbound_repl(self, DC): |
| 152 |
# make base command line |
| 153 |
samba_tool_cmd = self._samba_tool_cmd_list("options") |
| 154 |
# disable replication |
| 155 |
samba_tool_cmd += [DC, "--dsa-option=-DISABLE_INBOUND_REPL"] |
| 156 |
(result, out, err) = self.runsubcmd(*samba_tool_cmd) |
| 157 |
self.assertCmdSuccess(result, out, err) |
| 158 |
self.assertEqual(err, "", "Shouldn't be any error messages") |
| 159 |
|
| 160 |
def _disable_inbound_repl(self, DC): |
| 161 |
# make base command line |
| 162 |
samba_tool_cmd = self._samba_tool_cmd_list("options") |
| 163 |
# disable replication |
| 164 |
samba_tool_cmd += [DC, "--dsa-option=+DISABLE_INBOUND_REPL"] |
| 165 |
(result, out, err) = self.runsubcmd(*samba_tool_cmd) |
| 166 |
self.assertCmdSuccess(result, out, err) |
| 167 |
self.assertEqual(err, "", "Shouldn't be any error messages") |
| 168 |
|
| 169 |
def _enable_all_repl(self, DC): |
| 170 |
self._enable_inbound_repl(DC) |
| 171 |
# make base command line |
| 172 |
samba_tool_cmd = self._samba_tool_cmd_list("options") |
| 173 |
# enable replication |
| 174 |
samba_tool_cmd += [DC, "--dsa-option=-DISABLE_OUTBOUND_REPL"] |
| 175 |
(result, out, err) = self.runsubcmd(*samba_tool_cmd) |
| 176 |
self.assertCmdSuccess(result, out, err) |
| 177 |
self.assertEqual(err, "", "Shouldn't be any error messages") |
| 178 |
|
| 179 |
def _disable_all_repl(self, DC): |
| 180 |
self._disable_inbound_repl(DC) |
| 181 |
# make base command line |
| 182 |
samba_tool_cmd = self._samba_tool_cmd_list("options") |
| 183 |
# disable replication |
| 184 |
samba_tool_cmd += [DC, "--dsa-option=+DISABLE_OUTBOUND_REPL"] |
| 185 |
(result, out, err) = self.runsubcmd(*samba_tool_cmd) |
| 186 |
self.assertCmdSuccess(result, out, err) |
| 187 |
self.assertEqual(err, "", "Shouldn't be any error messages") |
| 188 |
|
| 189 |
def _get_highest_hwm_utdv(self, ldb_conn): |
| 190 |
res = ldb_conn.search("", scope=ldb.SCOPE_BASE, attrs=["highestCommittedUSN"]) |
| 191 |
hwm = drsuapi.DsReplicaHighWaterMark() |
| 192 |
hwm.tmp_highest_usn = int(res[0]["highestCommittedUSN"][0]) |
| 193 |
hwm.reserved_usn = 0 |
| 194 |
hwm.highest_usn = hwm.tmp_highest_usn |
| 195 |
|
| 196 |
utdv = drsuapi.DsReplicaCursorCtrEx() |
| 197 |
cursors = [] |
| 198 |
c1 = drsuapi.DsReplicaCursor() |
| 199 |
c1.source_dsa_invocation_id = misc.GUID(ldb_conn.get_invocation_id()) |
| 200 |
c1.highest_usn = hwm.highest_usn |
| 201 |
cursors.append(c1) |
| 202 |
utdv.count = len(cursors) |
| 203 |
utdv.cursors = cursors |
| 204 |
return (hwm, utdv) |
| 205 |
|
| 206 |
def _get_identifier(self, ldb_conn, dn): |
| 207 |
res = ldb_conn.search(dn, scope=ldb.SCOPE_BASE, |
| 208 |
attrs=["objectGUID", "objectSid"]) |
| 209 |
id = drsuapi.DsReplicaObjectIdentifier() |
| 210 |
id.guid = ndr_unpack(misc.GUID, res[0]['objectGUID'][0]) |
| 211 |
if "objectSid" in res[0]: |
| 212 |
id.sid = ndr_unpack(security.dom_sid, res[0]['objectSid'][0]) |
| 213 |
id.dn = str(res[0].dn) |
| 214 |
return id |
| 215 |
|
| 216 |
def _get_ctr6_links(self, ctr6): |
| 217 |
""" |
| 218 |
Unpacks the linked attributes from a DsGetNCChanges response |
| 219 |
and returns them as a list. |
| 220 |
""" |
| 221 |
ctr6_links = [] |
| 222 |
for lidx in range(0, ctr6.linked_attributes_count): |
| 223 |
l = ctr6.linked_attributes[lidx] |
| 224 |
try: |
| 225 |
target = ndr_unpack(drsuapi.DsReplicaObjectIdentifier3, |
| 226 |
l.value.blob) |
| 227 |
except: |
| 228 |
target = ndr_unpack(drsuapi.DsReplicaObjectIdentifier3Binary, |
| 229 |
l.value.blob) |
| 230 |
al = AbstractLink(l.attid, l.flags, |
| 231 |
l.identifier.guid, |
| 232 |
target.guid, target.dn) |
| 233 |
ctr6_links.append(al) |
| 234 |
|
| 235 |
return ctr6_links |
| 236 |
|
| 237 |
def _get_ctr6_object_guids(self, ctr6): |
| 238 |
"""Returns all the object GUIDs in a GetNCChanges response""" |
| 239 |
guid_list = [] |
| 240 |
|
| 241 |
obj = ctr6.first_object |
| 242 |
for i in range(0, ctr6.object_count): |
| 243 |
guid_list.append(str(obj.object.identifier.guid)) |
| 244 |
obj = obj.next_object |
| 245 |
|
| 246 |
return guid_list |
| 247 |
|
| 248 |
def _ctr6_debug(self, ctr6): |
| 249 |
""" |
| 250 |
Displays basic info contained in a DsGetNCChanges response. |
| 251 |
Having this debug code allows us to see the difference in behaviour |
| 252 |
between Samba and Windows easier. Turn on the self._debug flag to see it. |
| 253 |
""" |
| 254 |
|
| 255 |
if self._debug: |
| 256 |
print("------------ recvd CTR6 -------------") |
| 257 |
|
| 258 |
next_object = ctr6.first_object |
| 259 |
for i in range(0, ctr6.object_count): |
| 260 |
print("Obj %d: %s %s" % (i, next_object.object.identifier.dn[:25], |
| 261 |
next_object.object.identifier.guid)) |
| 262 |
next_object = next_object.next_object |
| 263 |
|
| 264 |
print("Linked Attributes: %d" % ctr6.linked_attributes_count) |
| 265 |
for lidx in range(0, ctr6.linked_attributes_count): |
| 266 |
l = ctr6.linked_attributes[lidx] |
| 267 |
try: |
| 268 |
target = ndr_unpack(drsuapi.DsReplicaObjectIdentifier3, |
| 269 |
l.value.blob) |
| 270 |
except: |
| 271 |
target = ndr_unpack(drsuapi.DsReplicaObjectIdentifier3Binary, |
| 272 |
l.value.blob) |
| 273 |
|
| 274 |
print("Link Tgt %s... <-- Src %s" |
| 275 |
% (target.dn[:25], l.identifier.guid)) |
| 276 |
state = "Del" |
| 277 |
if l.flags & drsuapi.DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE: |
| 278 |
state = "Act" |
| 279 |
print(" v%u %s changed %u" % (l.meta_data.version, state, |
| 280 |
l.meta_data.originating_change_time)) |
| 281 |
|
| 282 |
print("HWM: %d" % (ctr6.new_highwatermark.highest_usn)) |
| 283 |
print("Tmp HWM: %d" % (ctr6.new_highwatermark.tmp_highest_usn)) |
| 284 |
print("More data: %d" % (ctr6.more_data)) |
| 285 |
|
| 286 |
def _get_replication(self, replica_flags, |
| 287 |
drs_error=drsuapi.DRSUAPI_EXOP_ERR_NONE, drs=None, drs_handle=None, |
| 288 |
highwatermark=None, uptodateness_vector=None, |
| 289 |
more_flags=0, max_objects=133, exop=0, |
| 290 |
dest_dsa=drsuapi.DRSUAPI_DS_BIND_GUID_W2K3, |
| 291 |
source_dsa=None, invocation_id=None, nc_dn_str=None): |
| 292 |
""" |
| 293 |
Builds a DsGetNCChanges request based on the information provided |
| 294 |
and returns the response received from the DC. |
| 295 |
""" |
| 296 |
if source_dsa is None: |
| 297 |
source_dsa = self.test_ldb_dc.get_ntds_GUID() |
| 298 |
if invocation_id is None: |
| 299 |
invocation_id = self.test_ldb_dc.get_invocation_id() |
| 300 |
if nc_dn_str is None: |
| 301 |
nc_dn_str = self.test_ldb_dc.domain_dn() |
| 302 |
|
| 303 |
if highwatermark is None: |
| 304 |
if self.default_hwm is None: |
| 305 |
(highwatermark, _) = self._get_highest_hwm_utdv(self.test_ldb_dc) |
| 306 |
else: |
| 307 |
highwatermark = self.default_hwm |
| 308 |
|
| 309 |
if drs is None: |
| 310 |
drs = self.drs |
| 311 |
if drs_handle is None: |
| 312 |
drs_handle = self.drs_handle |
| 313 |
|
| 314 |
# Add a modify_highwatermark method to cause damage to the |
| 315 |
# highwatermark. For example, it could set reserved_usn to |
| 316 |
# zero as Entra ID does. |
| 317 |
modify_hwm = getattr(self, "modify_highwatermark", None) |
| 318 |
if modify_hwm is not None: |
| 319 |
modify_hwm(highwatermark) |
| 320 |
|
| 321 |
req10 = self._getnc_req10(dest_dsa=dest_dsa, |
| 322 |
invocation_id=invocation_id, |
| 323 |
nc_dn_str=nc_dn_str, |
| 324 |
exop=exop, |
| 325 |
max_objects=max_objects, |
| 326 |
replica_flags=replica_flags, |
| 327 |
more_flags=more_flags) |
| 328 |
req10.highwatermark = highwatermark |
| 329 |
if uptodateness_vector is not None: |
| 330 |
uptodateness_vector_v1 = drsuapi.DsReplicaCursorCtrEx() |
| 331 |
cursors = [] |
| 332 |
for i in range(0, uptodateness_vector.count): |
| 333 |
c = uptodateness_vector.cursors[i] |
| 334 |
c1 = drsuapi.DsReplicaCursor() |
| 335 |
c1.source_dsa_invocation_id = c.source_dsa_invocation_id |
| 336 |
c1.highest_usn = c.highest_usn |
| 337 |
cursors.append(c1) |
| 338 |
uptodateness_vector_v1.count = len(cursors) |
| 339 |
uptodateness_vector_v1.cursors = cursors |
| 340 |
req10.uptodateness_vector = uptodateness_vector_v1 |
| 341 |
(level, ctr) = drs.DsGetNCChanges(drs_handle, 10, req10) |
| 342 |
self._ctr6_debug(ctr) |
| 343 |
|
| 344 |
self.assertEqual(level, 6, "expected level 6 response!") |
| 345 |
self.assertEqual(ctr.source_dsa_guid, misc.GUID(source_dsa)) |
| 346 |
self.assertEqual(ctr.source_dsa_invocation_id, misc.GUID(invocation_id)) |
| 347 |
self.assertEqual(ctr.extended_ret, drs_error) |
| 348 |
|
| 349 |
return ctr |
| 350 |
|
| 351 |
def _check_replication(self, expected_dns, replica_flags, expected_links=None, |
| 352 |
drs_error=drsuapi.DRSUAPI_EXOP_ERR_NONE, drs=None, drs_handle=None, |
| 353 |
highwatermark=None, uptodateness_vector=None, |
| 354 |
more_flags=0, more_data=False, |
| 355 |
dn_ordered=True, links_ordered=True, |
| 356 |
max_objects=133, exop=0, |
| 357 |
dest_dsa=drsuapi.DRSUAPI_DS_BIND_GUID_W2K3, |
| 358 |
source_dsa=None, invocation_id=None, nc_dn_str=None, |
| 359 |
nc_object_count=0, nc_linked_attributes_count=0): |
| 360 |
""" |
| 361 |
Makes sure that replication returns the specific error given. |
| 362 |
""" |
| 363 |
if expected_links is None: |
| 364 |
expected_links = [] |
| 365 |
|
| 366 |
# send a DsGetNCChanges to the DC |
| 367 |
ctr6 = self._get_replication(replica_flags, |
| 368 |
drs_error, drs, drs_handle, |
| 369 |
highwatermark, uptodateness_vector, |
| 370 |
more_flags, max_objects, exop, dest_dsa, |
| 371 |
source_dsa, invocation_id, nc_dn_str) |
| 372 |
|
| 373 |
# check the response is what we expect |
| 374 |
self._check_ctr6(ctr6, expected_dns, expected_links, |
| 375 |
nc_object_count=nc_object_count, more_data=more_data, |
| 376 |
dn_ordered=dn_ordered, links_ordered=links_ordered, |
| 377 |
nc_linked_attributes_count=nc_linked_attributes_count) |
| 378 |
return (ctr6.new_highwatermark, ctr6.uptodateness_vector) |
| 379 |
|
| 380 |
def _get_ctr6_dn_list(self, ctr6): |
| 381 |
""" |
| 382 |
Returns the DNs contained in a DsGetNCChanges response. |
| 383 |
""" |
| 384 |
dn_list = [] |
| 385 |
next_object = ctr6.first_object |
| 386 |
for i in range(0, ctr6.object_count): |
| 387 |
dn_list.append(next_object.object.identifier.dn) |
| 388 |
next_object = next_object.next_object |
| 389 |
self.assertEqual(next_object, None) |
| 390 |
|
| 391 |
return dn_list |
| 392 |
|
| 393 |
def _check_ctr6(self, ctr6, expected_dns=None, expected_links=None, |
| 394 |
dn_ordered=True, links_ordered=True, |
| 395 |
more_data=False, nc_object_count=0, |
| 396 |
nc_linked_attributes_count=0, drs_error=0): |
| 397 |
""" |
| 398 |
Check that a ctr6 matches the specified parameters. |
| 399 |
""" |
| 400 |
if expected_dns is None: |
| 401 |
expected_dns = [] |
| 402 |
|
| 403 |
if expected_links is None: |
| 404 |
expected_links = [] |
| 405 |
|
| 406 |
ctr6_raw_dns = self._get_ctr6_dn_list(ctr6) |
| 407 |
|
| 408 |
# filter out changes to the RID Set objects, as these can happen |
| 409 |
# intermittently and mess up the test assertions |
| 410 |
ctr6_dns = [] |
| 411 |
for dn in ctr6_raw_dns: |
| 412 |
if "CN=RID Set," in dn or "CN=RID Manager$," in dn: |
| 413 |
print("Removing {0} from GetNCChanges reply".format(dn)) |
| 414 |
else: |
| 415 |
ctr6_dns.append(dn) |
| 416 |
|
| 417 |
self.assertEqual(len(ctr6_dns), len(expected_dns), |
| 418 |
"Received unexpected objects (%s)" % ctr6_dns) |
| 419 |
self.assertEqual(ctr6.object_count, len(ctr6_raw_dns)) |
| 420 |
self.assertEqual(ctr6.linked_attributes_count, len(expected_links)) |
| 421 |
self.assertEqual(ctr6.more_data, more_data) |
| 422 |
self.assertEqual(ctr6.nc_object_count, nc_object_count) |
| 423 |
self.assertEqual(ctr6.nc_linked_attributes_count, nc_linked_attributes_count) |
| 424 |
self.assertEqual(ctr6.drs_error[0], drs_error) |
| 425 |
|
| 426 |
i = 0 |
| 427 |
for dn in expected_dns: |
| 428 |
# Expect them back in the exact same order as specified. |
| 429 |
if dn_ordered: |
| 430 |
self.assertNotEqual(ctr6_dns[i], None) |
| 431 |
self.assertEqual(ctr6_dns[i], dn) |
| 432 |
i = i + 1 |
| 433 |
# Don't care what order |
| 434 |
else: |
| 435 |
self.assertTrue(dn in ctr6_dns, "Couldn't find DN '%s' anywhere in ctr6 response." % dn) |
| 436 |
|
| 437 |
# Extract the links from the response |
| 438 |
ctr6_links = self._get_ctr6_links(ctr6) |
| 439 |
expected_links.sort() |
| 440 |
|
| 441 |
lidx = 0 |
| 442 |
for el in expected_links: |
| 443 |
if links_ordered: |
| 444 |
self.assertEqual(el, ctr6_links[lidx]) |
| 445 |
lidx += 1 |
| 446 |
else: |
| 447 |
self.assertTrue(el in ctr6_links, "Couldn't find link '%s' anywhere in ctr6 response." % el) |
| 448 |
|
| 449 |
def _exop_req8(self, dest_dsa, invocation_id, nc_dn_str, exop, |
| 450 |
replica_flags=0, max_objects=0, partial_attribute_set=None, |
| 451 |
partial_attribute_set_ex=None, mapping_ctr=None, nc_guid=None): |
| 452 |
req8 = drsuapi.DsGetNCChangesRequest8() |
| 453 |
|
| 454 |
req8.destination_dsa_guid = misc.GUID(dest_dsa) if dest_dsa else misc.GUID() |
| 455 |
req8.source_dsa_invocation_id = misc.GUID(invocation_id) |
| 456 |
req8.naming_context = drsuapi.DsReplicaObjectIdentifier() |
| 457 |
req8.naming_context.dn = str(nc_dn_str) |
| 458 |
if nc_guid is not None: |
| 459 |
req8.naming_context.guid = nc_guid |
| 460 |
req8.highwatermark = drsuapi.DsReplicaHighWaterMark() |
| 461 |
req8.highwatermark.tmp_highest_usn = 0 |
| 462 |
req8.highwatermark.reserved_usn = 0 |
| 463 |
req8.highwatermark.highest_usn = 0 |
| 464 |
req8.uptodateness_vector = None |
| 465 |
req8.replica_flags = replica_flags |
| 466 |
req8.max_object_count = max_objects |
| 467 |
req8.max_ndr_size = 402116 |
| 468 |
req8.extended_op = exop |
| 469 |
req8.fsmo_info = 0 |
| 470 |
req8.partial_attribute_set = partial_attribute_set |
| 471 |
req8.partial_attribute_set_ex = partial_attribute_set_ex |
| 472 |
if mapping_ctr: |
| 473 |
req8.mapping_ctr = mapping_ctr |
| 474 |
else: |
| 475 |
req8.mapping_ctr.num_mappings = 0 |
| 476 |
req8.mapping_ctr.mappings = None |
| 477 |
|
| 478 |
return req8 |
| 479 |
|
| 480 |
def _getnc_req10(self, dest_dsa, invocation_id, nc_dn_str, exop, |
| 481 |
replica_flags=0, max_objects=0, partial_attribute_set=None, |
| 482 |
partial_attribute_set_ex=None, mapping_ctr=None, |
| 483 |
more_flags=0, nc_guid=None): |
| 484 |
req10 = drsuapi.DsGetNCChangesRequest10() |
| 485 |
|
| 486 |
req10.destination_dsa_guid = misc.GUID(dest_dsa) if dest_dsa else misc.GUID() |
| 487 |
req10.source_dsa_invocation_id = misc.GUID(invocation_id) |
| 488 |
req10.naming_context = drsuapi.DsReplicaObjectIdentifier() |
| 489 |
req10.naming_context.dn = str(nc_dn_str) |
| 490 |
if nc_guid is not None: |
| 491 |
req10.naming_context.guid = nc_guid |
| 492 |
req10.highwatermark = drsuapi.DsReplicaHighWaterMark() |
| 493 |
req10.highwatermark.tmp_highest_usn = 0 |
| 494 |
req10.highwatermark.reserved_usn = 0 |
| 495 |
req10.highwatermark.highest_usn = 0 |
| 496 |
req10.uptodateness_vector = None |
| 497 |
req10.replica_flags = replica_flags |
| 498 |
req10.max_object_count = max_objects |
| 499 |
req10.max_ndr_size = 402116 |
| 500 |
req10.extended_op = exop |
| 501 |
req10.fsmo_info = 0 |
| 502 |
req10.partial_attribute_set = partial_attribute_set |
| 503 |
req10.partial_attribute_set_ex = partial_attribute_set_ex |
| 504 |
if mapping_ctr: |
| 505 |
req10.mapping_ctr = mapping_ctr |
| 506 |
else: |
| 507 |
req10.mapping_ctr.num_mappings = 0 |
| 508 |
req10.mapping_ctr.mappings = None |
| 509 |
req10.more_flags = more_flags |
| 510 |
|
| 511 |
return req10 |
| 512 |
|
| 513 |
def _ds_bind(self, server_name, creds=None, ip=None): |
| 514 |
if ip is None: |
| 515 |
binding_str = f"ncacn_ip_tcp:{server_name}[seal]" |
| 516 |
else: |
| 517 |
binding_str = f"ncacn_ip_tcp:{ip}[seal,target_hostname={server_name}]" |
| 518 |
|
| 519 |
if creds is None: |
| 520 |
creds = self.get_credentials() |
| 521 |
drs = drsuapi.drsuapi(binding_str, self.get_loadparm(), creds) |
| 522 |
(drs_handle, supported_extensions) = drs_DsBind(drs) |
| 523 |
return (drs, drs_handle) |
| 524 |
|
| 525 |
def get_partial_attribute_set(self, attids=None): |
| 526 |
if attids is None: |
| 527 |
attids = [drsuapi.DRSUAPI_ATTID_objectClass] |
| 528 |
partial_attribute_set = drsuapi.DsPartialAttributeSet() |
| 529 |
partial_attribute_set.attids = attids |
| 530 |
partial_attribute_set.num_attids = len(attids) |
| 531 |
return partial_attribute_set |
| 532 |
|
| 533 |
|
| 534 |
class AbstractLink: |
| 535 |
def __init__(self, attid, flags, identifier, targetGUID, |
| 536 |
targetDN=""): |
| 537 |
self.attid = attid |
| 538 |
self.flags = flags |
| 539 |
self.identifier = str(identifier) |
| 540 |
self.selfGUID_blob = ndr_pack(identifier) |
| 541 |
self.targetGUID = str(targetGUID) |
| 542 |
self.targetGUID_blob = ndr_pack(targetGUID) |
| 543 |
self.targetDN = targetDN |
| 544 |
|
| 545 |
def __repr__(self): |
| 546 |
return "AbstractLink(0x%08x, 0x%08x, %s, %s)" % ( |
| 547 |
self.attid, self.flags, self.identifier, self.targetGUID) |
| 548 |
|
| 549 |
def __internal_cmp__(self, other, verbose=False): |
| 550 |
"""See CompareLinks() in MS-DRSR section 4.1.10.5.17""" |
| 551 |
if not isinstance(other, AbstractLink): |
| 552 |
if verbose: |
| 553 |
print("AbstractLink.__internal_cmp__(%r, %r) => wrong type" % (self, other)) |
| 554 |
return NotImplemented |
| 555 |
|
| 556 |
c = cmp(self.selfGUID_blob, other.selfGUID_blob) |
| 557 |
if c != 0: |
| 558 |
if verbose: |
| 559 |
print("AbstractLink.__internal_cmp__(%r, %r) => %d different identifier" % (self, other, c)) |
| 560 |
return c |
| 561 |
|
| 562 |
c = other.attid - self.attid |
| 563 |
if c != 0: |
| 564 |
if verbose: |
| 565 |
print("AbstractLink.__internal_cmp__(%r, %r) => %d different attid" % (self, other, c)) |
| 566 |
return c |
| 567 |
|
| 568 |
self_active = self.flags & drsuapi.DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE |
| 569 |
other_active = other.flags & drsuapi.DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE |
| 570 |
|
| 571 |
c = self_active - other_active |
| 572 |
if c != 0: |
| 573 |
if verbose: |
| 574 |
print("AbstractLink.__internal_cmp__(%r, %r) => %d different FLAG_ACTIVE" % (self, other, c)) |
| 575 |
return c |
| 576 |
|
| 577 |
c = cmp(self.targetGUID_blob, other.targetGUID_blob) |
| 578 |
if c != 0: |
| 579 |
if verbose: |
| 580 |
print("AbstractLink.__internal_cmp__(%r, %r) => %d different target" % (self, other, c)) |
| 581 |
return c |
| 582 |
|
| 583 |
c = self.flags - other.flags |
| 584 |
if c != 0: |
| 585 |
if verbose: |
| 586 |
print("AbstractLink.__internal_cmp__(%r, %r) => %d different flags" % (self, other, c)) |
| 587 |
return c |
| 588 |
|
| 589 |
return 0 |
| 590 |
|
| 591 |
def __lt__(self, other): |
| 592 |
c = self.__internal_cmp__(other) |
| 593 |
if c == NotImplemented: |
| 594 |
return NotImplemented |
| 595 |
if c < 0: |
| 596 |
return True |
| 597 |
return False |
| 598 |
|
| 599 |
def __le__(self, other): |
| 600 |
c = self.__internal_cmp__(other) |
| 601 |
if c == NotImplemented: |
| 602 |
return NotImplemented |
| 603 |
if c <= 0: |
| 604 |
return True |
| 605 |
return False |
| 606 |
|
| 607 |
def __eq__(self, other): |
| 608 |
c = self.__internal_cmp__(other, verbose=True) |
| 609 |
if c == NotImplemented: |
| 610 |
return NotImplemented |
| 611 |
if c == 0: |
| 612 |
return True |
| 613 |
return False |
| 614 |
|
| 615 |
def __ne__(self, other): |
| 616 |
c = self.__internal_cmp__(other) |
| 617 |
if c == NotImplemented: |
| 618 |
return NotImplemented |
| 619 |
if c != 0: |
| 620 |
return True |
| 621 |
return False |
| 622 |
|
| 623 |
def __gt__(self, other): |
| 624 |
c = self.__internal_cmp__(other) |
| 625 |
if c == NotImplemented: |
| 626 |
return NotImplemented |
| 627 |
if c > 0: |
| 628 |
return True |
| 629 |
return False |
| 630 |
|
| 631 |
def __ge__(self, other): |
| 632 |
c = self.__internal_cmp__(other) |
| 633 |
if c == NotImplemented: |
| 634 |
return NotImplemented |
| 635 |
if c >= 0: |
| 636 |
return True |
| 637 |
return False |
| 638 |
|
| 639 |
def __hash__(self): |
| 640 |
return hash((self.attid, self.flags, self.identifier, self.targetGUID)) |