3 # Copyright Andrew Tridgell 2010
4 # Copyright Andrew Bartlett 2017
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from samba.dcerpc import drsuapi, misc, drsblobs
21 from samba.net import Net
22 from samba.ndr import ndr_unpack
23 from samba import dsdb
24 from samba import werror
25 from samba import WERRORError
27 from samba.dcerpc.drsuapi import DRSUAPI_ATTID_name
30 class drsException(Exception):
31 """Base element for drs errors"""
33 def __init__(self, value):
37 return "drsException: " + self.value
40 def drsuapi_connect(server, lp, creds):
41 """Make a DRSUAPI connection to the server.
43 :param server: the name of the server to connect to
44 :param lp: a samba line parameter object
45 :param creds: credential used for the connection
46 :return: A tuple with the drsuapi bind object, the drsuapi handle
47 and the supported extensions.
48 :raise drsException: if the connection fails
51 binding_options = "seal"
52 if lp.log_level() >= 9:
53 binding_options += ",print"
54 binding_string = "ncacn_ip_tcp:%s[%s]" % (server, binding_options)
56 drsuapiBind = drsuapi.drsuapi(binding_string, lp, creds)
57 (drsuapiHandle, bindSupportedExtensions) = drs_DsBind(drsuapiBind)
58 except Exception as e:
59 raise drsException("DRS connection to %s failed: %s" % (server, e))
61 return (drsuapiBind, drsuapiHandle, bindSupportedExtensions)
64 def sendDsReplicaSync(drsuapiBind, drsuapi_handle, source_dsa_guid,
65 naming_context, req_option):
66 """Send DS replica sync request.
68 :param drsuapiBind: a drsuapi Bind object
69 :param drsuapi_handle: a drsuapi handle on the drsuapi connection
70 :param source_dsa_guid: the guid of the source dsa for the replication
71 :param naming_context: the DN of the naming context to replicate
72 :param req_options: replication options for the DsReplicaSync call
73 :raise drsException: if any error occur while sending and receiving the
74 reply for the dsReplicaSync
77 nc = drsuapi.DsReplicaObjectIdentifier()
78 nc.dn = naming_context
80 req1 = drsuapi.DsReplicaSyncRequest1()
81 req1.naming_context = nc;
82 req1.options = req_option
83 req1.source_dsa_guid = misc.GUID(source_dsa_guid)
86 drsuapiBind.DsReplicaSync(drsuapi_handle, 1, req1)
87 except Exception as estr:
88 raise drsException("DsReplicaSync failed %s" % estr)
91 def sendRemoveDsServer(drsuapiBind, drsuapi_handle, server_dsa_dn, domain):
92 """Send RemoveDSServer request.
94 :param drsuapiBind: a drsuapi Bind object
95 :param drsuapi_handle: a drsuapi handle on the drsuapi connection
96 :param server_dsa_dn: a DN object of the server's dsa that we want to
98 :param domain: a DN object of the server's domain
99 :raise drsException: if any error occur while sending and receiving the
100 reply for the DsRemoveDSServer
104 req1 = drsuapi.DsRemoveDSServerRequest1()
105 req1.server_dn = str(server_dsa_dn)
106 req1.domain_dn = str(domain)
109 drsuapiBind.DsRemoveDSServer(drsuapi_handle, 1, req1)
110 except Exception as estr:
111 raise drsException("DsRemoveDSServer failed %s" % estr)
115 '''make a DsBind call, returning the binding handle'''
116 bind_info = drsuapi.DsBindInfoCtr()
117 bind_info.length = 28
118 bind_info.info = drsuapi.DsBindInfo28()
119 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_BASE
120 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION
121 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI
122 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2
123 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS
124 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1
125 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION
126 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE
127 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2
128 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION
129 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2
130 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD
131 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND
132 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO
133 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION
134 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01
135 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP
136 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY
137 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3
138 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2
139 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6
140 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS
141 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8
142 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5
143 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6
144 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3
145 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7
146 bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT
147 (info, handle) = drs.DsBind(misc.GUID(drsuapi.DRSUAPI_DS_BIND_GUID), bind_info)
149 return (handle, info.info.supported_extensions)
152 def drs_get_rodc_partial_attribute_set(samdb):
153 '''get a list of attributes for RODC replication'''
154 partial_attribute_set = drsuapi.DsPartialAttributeSet()
155 partial_attribute_set.version = 1
159 # the exact list of attids we send is quite critical. Note that
160 # we do ask for the secret attributes, but set SPECIAL_SECRET_PROCESSING
162 schema_dn = samdb.get_schema_basedn()
163 res = samdb.search(base=schema_dn, scope=ldb.SCOPE_SUBTREE,
164 expression="objectClass=attributeSchema",
165 attrs=["lDAPDisplayName", "systemFlags",
169 ldap_display_name = r["lDAPDisplayName"][0]
170 if "systemFlags" in r:
171 system_flags = r["systemFlags"][0]
172 if (int(system_flags) & (samba.dsdb.DS_FLAG_ATTR_NOT_REPLICATED |
173 samba.dsdb.DS_FLAG_ATTR_IS_CONSTRUCTED)):
175 if "searchFlags" in r:
176 search_flags = r["searchFlags"][0]
177 if (int(search_flags) & samba.dsdb.SEARCH_FLAG_RODC_ATTRIBUTE):
179 attid = samdb.get_attid_from_lDAPDisplayName(ldap_display_name)
180 attids.append(int(attid))
182 # the attids do need to be sorted, or windows doesn't return
183 # all the attributes we need
185 partial_attribute_set.attids = attids
186 partial_attribute_set.num_attids = len(attids)
187 return partial_attribute_set
190 class drs_Replicate(object):
191 '''DRS replication calls'''
193 def __init__(self, binding_string, lp, creds, samdb, invocation_id):
194 self.drs = drsuapi.drsuapi(binding_string, lp, creds)
195 (self.drs_handle, self.supported_extensions) = drs_DsBind(self.drs)
196 self.net = Net(creds=creds, lp=lp)
198 if not isinstance(invocation_id, misc.GUID):
199 raise RuntimeError("Must supply GUID for invocation_id")
200 if invocation_id == misc.GUID("00000000-0000-0000-0000-000000000000"):
201 raise RuntimeError("Must not set GUID 00000000-0000-0000-0000-000000000000 as invocation_id")
202 self.replication_state = self.net.replicate_init(self.samdb, lp, self.drs, invocation_id)
204 def _should_retry_with_get_tgt(self, error_code, req):
206 # If the error indicates we fail to resolve a target object for a
207 # linked attribute, then we should retry the request with GET_TGT
208 # (if we support it and haven't already tried that)
210 # TODO fix up the below line when we next update werror_err_table.txt
211 # and pull in the new error-code
212 # return (error_code == werror.WERR_DS_DRA_RECYCLED_TARGET and
213 return (error_code == 0x21bf and
214 (req.more_flags & drsuapi.DRSUAPI_DRS_GET_TGT) == 0 and
215 self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10)
217 def process_chunk(self, level, ctr, schema, req_level, req, first_chunk):
218 '''Processes a single chunk of received replication data'''
219 # pass the replication into the py_net.c python bindings for processing
220 self.net.replicate_chunk(self.replication_state, level, ctr,
221 schema=schema, req_level=req_level, req=req)
223 def replicate(self, dn, source_dsa_invocation_id, destination_dsa_guid,
224 schema=False, exop=drsuapi.DRSUAPI_EXOP_NONE, rodc=False,
225 replica_flags=None, full_sync=True, sync_forced=False, more_flags=0):
226 '''replicate a single DN'''
228 # setup for a GetNCChanges call
229 if self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10:
230 req = drsuapi.DsGetNCChangesRequest10()
231 req.more_flags = more_flags
235 req = drsuapi.DsGetNCChangesRequest8()
237 req.destination_dsa_guid = destination_dsa_guid
238 req.source_dsa_invocation_id = source_dsa_invocation_id
239 req.naming_context = drsuapi.DsReplicaObjectIdentifier()
240 req.naming_context.dn = dn
242 # Default to a full replication if we don't find an upToDatenessVector
244 hwm = drsuapi.DsReplicaHighWaterMark()
245 hwm.tmp_highest_usn = 0
250 res = self.samdb.search(base=dn, scope=ldb.SCOPE_BASE,
252 if "repsFrom" in res[0]:
253 for reps_from_packed in res[0]["repsFrom"]:
254 reps_from_obj = ndr_unpack(drsblobs.repsFromToBlob, reps_from_packed)
255 if reps_from_obj.ctr.source_dsa_invocation_id == source_dsa_invocation_id:
256 hwm = reps_from_obj.ctr.highwatermark
258 udv = drsuapi.DsReplicaCursorCtrEx()
264 cursors_v2 = dsdb._dsdb_load_udv_v2(self.samdb,
265 self.samdb.get_default_basedn())
266 for cursor_v2 in cursors_v2:
267 cursor_v1 = drsuapi.DsReplicaCursor()
268 cursor_v1.source_dsa_invocation_id = cursor_v2.source_dsa_invocation_id
269 cursor_v1.highest_usn = cursor_v2.highest_usn
270 cursors_v1.append(cursor_v1)
272 udv.cursors = cursors_v1
273 udv.count = len(cursors_v1)
275 req.highwatermark = hwm
276 req.uptodateness_vector = udv
278 if replica_flags is not None:
279 req.replica_flags = replica_flags
280 elif exop == drsuapi.DRSUAPI_EXOP_REPL_SECRET:
281 req.replica_flags = 0
283 req.replica_flags = (drsuapi.DRSUAPI_DRS_INIT_SYNC |
284 drsuapi.DRSUAPI_DRS_PER_SYNC |
285 drsuapi.DRSUAPI_DRS_GET_ANC |
286 drsuapi.DRSUAPI_DRS_NEVER_SYNCED |
287 drsuapi.DRSUAPI_DRS_GET_ALL_GROUP_MEMBERSHIP)
289 req.replica_flags |= (
290 drsuapi.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING)
292 req.replica_flags |= drsuapi.DRSUAPI_DRS_WRIT_REP
295 req.replica_flags |= drsuapi.DRSUAPI_DRS_SYNC_FORCED
297 req.max_object_count = 402
298 req.max_ndr_size = 402116
299 req.extended_op = exop
301 req.partial_attribute_set = None
302 req.partial_attribute_set_ex = None
303 req.mapping_ctr.num_mappings = 0
304 req.mapping_ctr.mappings = None
306 if not schema and rodc:
307 req.partial_attribute_set = drs_get_rodc_partial_attribute_set(self.samdb)
309 if not self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8:
311 req5 = drsuapi.DsGetNCChangesRequest5()
314 setattr(req5, a, getattr(req, a))
322 (level, ctr) = self.drs.DsGetNCChanges(self.drs_handle, req_level, req)
323 if ctr.first_object is None and ctr.object_count != 0:
324 raise RuntimeError("DsGetNCChanges: NULL first_object with object_count=%u" % (ctr.object_count))
327 self.process_chunk(level, ctr, schema, req_level, req, first_chunk)
328 except WERRORError as e:
329 # Check if retrying with the GET_TGT flag set might resolve this error
330 if self._should_retry_with_get_tgt(e.args[0], req):
332 print("Missing target object - retrying with DRS_GET_TGT")
333 req.more_flags |= drsuapi.DRSUAPI_DRS_GET_TGT
335 # try sending the request again (this has the side-effect
336 # of causing the DC to restart the replication from scratch)
343 num_objects += ctr.object_count
345 # Cope with servers that do not return level 6, so do not return any links
347 num_links += ctr.linked_attributes_count
348 except AttributeError:
351 if ctr.more_data == 0:
353 req.highwatermark = ctr.new_highwatermark
355 return (num_objects, num_links)
358 # Handles the special case of creating a new clone of a DB, while also renaming
359 # the entire DB's objects on the way through
360 class drs_ReplicateRenamer(drs_Replicate):
361 '''Uses DRS replication to rename the entire DB'''
363 def __init__(self, binding_string, lp, creds, samdb, invocation_id,
364 old_base_dn, new_base_dn):
365 super(drs_ReplicateRenamer, self).__init__(binding_string, lp, creds,
366 samdb, invocation_id)
367 self.old_base_dn = old_base_dn
368 self.new_base_dn = new_base_dn
370 def rename_dn(self, dn_str):
371 '''Uses string substitution to replace the base DN'''
372 return re.sub('%s$' % self.old_base_dn, self.new_base_dn, dn_str)
374 def update_name_attr(self, base_obj):
375 '''Updates the 'name' attribute for the base DN object'''
376 for attr in base_obj.attribute_ctr.attributes:
377 if attr.attid == DRSUAPI_ATTID_name:
378 base_dn = ldb.Dn(self.samdb, base_obj.identifier.dn)
379 new_name = base_dn.get_rdn_value()
380 attr.value_ctr.values[0].blob = new_name.encode('utf-16-le')
382 def rename_top_level_object(self, first_obj):
383 '''Renames the first/top-level object in a partition'''
384 old_dn = first_obj.identifier.dn
385 first_obj.identifier.dn = self.rename_dn(first_obj.identifier.dn)
386 print("Renaming partition %s --> %s" % (old_dn,
387 first_obj.identifier.dn))
389 # we also need to fix up the 'name' attribute for the base DN,
390 # otherwise the RDNs won't match
391 if first_obj.identifier.dn == self.new_base_dn:
392 self.update_name_attr(first_obj)
394 def process_chunk(self, level, ctr, schema, req_level, req, first_chunk):
395 '''Processes a single chunk of received replication data'''
397 # we need to rename the NC in every chunk - this gets used in searches
398 # when applying the chunk
399 if ctr.naming_context:
400 ctr.naming_context.dn = self.rename_dn(ctr.naming_context.dn)
402 # rename the first object in each partition. This will cause every
403 # subsequent object in the partiton to be renamed as a side-effect
404 if first_chunk and ctr.object_count != 0:
405 self.rename_top_level_object(ctr.first_object.object)
407 # then do the normal repl processing to apply this chunk to our DB
408 super(drs_ReplicateRenamer, self).process_chunk(level, ctr, schema,