PEP8: fix E302: expected 2 blank lines, found 1
[amitay/samba.git] / python / samba / drs_utils.py
1 # DRS utility code
2 #
3 # Copyright Andrew Tridgell 2010
4 # Copyright Andrew Bartlett 2017
5 #
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.
10 #
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.
15 #
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/>.
18 #
19
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
26 import samba, ldb
27 from samba.dcerpc.drsuapi import DRSUAPI_ATTID_name
28 import re
29
30
31 class drsException(Exception):
32     """Base element for drs errors"""
33
34     def __init__(self, value):
35         self.value = value
36
37     def __str__(self):
38         return "drsException: " + self.value
39
40
41 def drsuapi_connect(server, lp, creds):
42     """Make a DRSUAPI connection to the server.
43
44     :param server: the name of the server to connect to
45     :param lp: a samba line parameter object
46     :param creds: credential used for the connection
47     :return: A tuple with the drsuapi bind object, the drsuapi handle
48                 and the supported extensions.
49     :raise drsException: if the connection fails
50     """
51
52     binding_options = "seal"
53     if lp.log_level() >= 9:
54         binding_options += ",print"
55     binding_string = "ncacn_ip_tcp:%s[%s]" % (server, binding_options)
56     try:
57         drsuapiBind = drsuapi.drsuapi(binding_string, lp, creds)
58         (drsuapiHandle, bindSupportedExtensions) = drs_DsBind(drsuapiBind)
59     except Exception as e:
60         raise drsException("DRS connection to %s failed: %s" % (server, e))
61
62     return (drsuapiBind, drsuapiHandle, bindSupportedExtensions)
63
64
65 def sendDsReplicaSync(drsuapiBind, drsuapi_handle, source_dsa_guid,
66                       naming_context, req_option):
67     """Send DS replica sync request.
68
69     :param drsuapiBind: a drsuapi Bind object
70     :param drsuapi_handle: a drsuapi handle on the drsuapi connection
71     :param source_dsa_guid: the guid of the source dsa for the replication
72     :param naming_context: the DN of the naming context to replicate
73     :param req_options: replication options for the DsReplicaSync call
74     :raise drsException: if any error occur while sending and receiving the
75         reply for the dsReplicaSync
76     """
77
78     nc = drsuapi.DsReplicaObjectIdentifier()
79     nc.dn = naming_context
80
81     req1 = drsuapi.DsReplicaSyncRequest1()
82     req1.naming_context = nc;
83     req1.options = req_option
84     req1.source_dsa_guid = misc.GUID(source_dsa_guid)
85
86     try:
87         drsuapiBind.DsReplicaSync(drsuapi_handle, 1, req1)
88     except Exception as estr:
89         raise drsException("DsReplicaSync failed %s" % estr)
90
91
92 def sendRemoveDsServer(drsuapiBind, drsuapi_handle, server_dsa_dn, domain):
93     """Send RemoveDSServer request.
94
95     :param drsuapiBind: a drsuapi Bind object
96     :param drsuapi_handle: a drsuapi handle on the drsuapi connection
97     :param server_dsa_dn: a DN object of the server's dsa that we want to
98         demote
99     :param domain: a DN object of the server's domain
100     :raise drsException: if any error occur while sending and receiving the
101         reply for the DsRemoveDSServer
102     """
103
104     try:
105         req1 = drsuapi.DsRemoveDSServerRequest1()
106         req1.server_dn = str(server_dsa_dn)
107         req1.domain_dn = str(domain)
108         req1.commit = 1
109
110         drsuapiBind.DsRemoveDSServer(drsuapi_handle, 1, req1)
111     except Exception as estr:
112         raise drsException("DsRemoveDSServer failed %s" % estr)
113
114
115 def drs_DsBind(drs):
116     '''make a DsBind call, returning the binding handle'''
117     bind_info = drsuapi.DsBindInfoCtr()
118     bind_info.length = 28
119     bind_info.info = drsuapi.DsBindInfo28()
120     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_BASE
121     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION
122     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI
123     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2
124     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS
125     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1
126     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION
127     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE
128     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2
129     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION
130     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2
131     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD
132     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND
133     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO
134     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION
135     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01
136     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP
137     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY
138     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3
139     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2
140     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6
141     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS
142     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8
143     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5
144     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6
145     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3
146     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7
147     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT
148     (info, handle) = drs.DsBind(misc.GUID(drsuapi.DRSUAPI_DS_BIND_GUID), bind_info)
149
150     return (handle, info.info.supported_extensions)
151
152
153 def drs_get_rodc_partial_attribute_set(samdb):
154     '''get a list of attributes for RODC replication'''
155     partial_attribute_set = drsuapi.DsPartialAttributeSet()
156     partial_attribute_set.version = 1
157
158     attids = []
159
160     # the exact list of attids we send is quite critical. Note that
161     # we do ask for the secret attributes, but set SPECIAL_SECRET_PROCESSING
162     # to zero them out
163     schema_dn = samdb.get_schema_basedn()
164     res = samdb.search(base=schema_dn, scope=ldb.SCOPE_SUBTREE,
165                        expression="objectClass=attributeSchema",
166                        attrs=["lDAPDisplayName", "systemFlags",
167                               "searchFlags"])
168
169     for r in res:
170         ldap_display_name = r["lDAPDisplayName"][0]
171         if "systemFlags" in r:
172             system_flags      = r["systemFlags"][0]
173             if (int(system_flags) & (samba.dsdb.DS_FLAG_ATTR_NOT_REPLICATED |
174                                      samba.dsdb.DS_FLAG_ATTR_IS_CONSTRUCTED)):
175                 continue
176         if "searchFlags" in r:
177             search_flags = r["searchFlags"][0]
178             if (int(search_flags) & samba.dsdb.SEARCH_FLAG_RODC_ATTRIBUTE):
179                 continue
180         attid = samdb.get_attid_from_lDAPDisplayName(ldap_display_name)
181         attids.append(int(attid))
182
183     # the attids do need to be sorted, or windows doesn't return
184     # all the attributes we need
185     attids.sort()
186     partial_attribute_set.attids         = attids
187     partial_attribute_set.num_attids = len(attids)
188     return partial_attribute_set
189
190
191 class drs_Replicate(object):
192     '''DRS replication calls'''
193
194     def __init__(self, binding_string, lp, creds, samdb, invocation_id):
195         self.drs = drsuapi.drsuapi(binding_string, lp, creds)
196         (self.drs_handle, self.supported_extensions) = drs_DsBind(self.drs)
197         self.net = Net(creds=creds, lp=lp)
198         self.samdb = samdb
199         if not isinstance(invocation_id, misc.GUID):
200             raise RuntimeError("Must supply GUID for invocation_id")
201         if invocation_id == misc.GUID("00000000-0000-0000-0000-000000000000"):
202             raise RuntimeError("Must not set GUID 00000000-0000-0000-0000-000000000000 as invocation_id")
203         self.replication_state = self.net.replicate_init(self.samdb, lp, self.drs, invocation_id)
204         self.more_flags = 0
205
206     def _should_retry_with_get_tgt(self, error_code, req):
207
208         # If the error indicates we fail to resolve a target object for a
209         # linked attribute, then we should retry the request with GET_TGT
210         # (if we support it and haven't already tried that)
211
212         # TODO fix up the below line when we next update werror_err_table.txt
213         # and pull in the new error-code
214         # return (error_code == werror.WERR_DS_DRA_RECYCLED_TARGET and
215         return (error_code == 0x21bf and
216                 (req.more_flags & drsuapi.DRSUAPI_DRS_GET_TGT) == 0 and
217                 self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10)
218
219     def process_chunk(self, level, ctr, schema, req_level, req, first_chunk):
220         '''Processes a single chunk of received replication data'''
221         # pass the replication into the py_net.c python bindings for processing
222         self.net.replicate_chunk(self.replication_state, level, ctr,
223                                  schema=schema, req_level=req_level, req=req)
224
225     def replicate(self, dn, source_dsa_invocation_id, destination_dsa_guid,
226                   schema=False, exop=drsuapi.DRSUAPI_EXOP_NONE, rodc=False,
227                   replica_flags=None, full_sync=True, sync_forced=False, more_flags=0):
228         '''replicate a single DN'''
229
230         # setup for a GetNCChanges call
231         if self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10:
232             req = drsuapi.DsGetNCChangesRequest10()
233             req.more_flags = (more_flags | self.more_flags)
234             req_level = 10
235         else:
236             req_level = 8
237             req = drsuapi.DsGetNCChangesRequest8()
238
239         req.destination_dsa_guid = destination_dsa_guid
240         req.source_dsa_invocation_id = source_dsa_invocation_id
241         req.naming_context = drsuapi.DsReplicaObjectIdentifier()
242         req.naming_context.dn = dn
243
244         # Default to a full replication if we don't find an upToDatenessVector
245         udv = None
246         hwm = drsuapi.DsReplicaHighWaterMark()
247         hwm.tmp_highest_usn = 0
248         hwm.reserved_usn = 0
249         hwm.highest_usn = 0
250
251         if not full_sync:
252             res = self.samdb.search(base=dn, scope=ldb.SCOPE_BASE,
253                                     attrs=["repsFrom"])
254             if "repsFrom" in res[0]:
255                 for reps_from_packed in res[0]["repsFrom"]:
256                     reps_from_obj = ndr_unpack(drsblobs.repsFromToBlob, reps_from_packed)
257                     if reps_from_obj.ctr.source_dsa_invocation_id == source_dsa_invocation_id:
258                         hwm = reps_from_obj.ctr.highwatermark
259
260             udv = drsuapi.DsReplicaCursorCtrEx()
261             udv.version = 1
262             udv.reserved1 = 0
263             udv.reserved2 = 0
264
265             cursors_v1 = []
266             cursors_v2 = dsdb._dsdb_load_udv_v2(self.samdb,
267                                                 self.samdb.get_default_basedn())
268             for cursor_v2 in cursors_v2:
269                 cursor_v1 = drsuapi.DsReplicaCursor()
270                 cursor_v1.source_dsa_invocation_id = cursor_v2.source_dsa_invocation_id
271                 cursor_v1.highest_usn = cursor_v2.highest_usn
272                 cursors_v1.append(cursor_v1)
273
274             udv.cursors = cursors_v1
275             udv.count = len(cursors_v1)
276
277         req.highwatermark = hwm
278         req.uptodateness_vector = udv
279
280         if replica_flags is not None:
281             req.replica_flags = replica_flags
282         elif exop == drsuapi.DRSUAPI_EXOP_REPL_SECRET:
283             req.replica_flags = 0
284         else:
285             req.replica_flags = (drsuapi.DRSUAPI_DRS_INIT_SYNC |
286                                  drsuapi.DRSUAPI_DRS_PER_SYNC |
287                                  drsuapi.DRSUAPI_DRS_GET_ANC |
288                                  drsuapi.DRSUAPI_DRS_NEVER_SYNCED |
289                                  drsuapi.DRSUAPI_DRS_GET_ALL_GROUP_MEMBERSHIP)
290             if rodc:
291                 req.replica_flags |= (
292                      drsuapi.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING)
293             else:
294                 req.replica_flags |= drsuapi.DRSUAPI_DRS_WRIT_REP
295
296         if sync_forced:
297             req.replica_flags |= drsuapi.DRSUAPI_DRS_SYNC_FORCED
298
299         req.max_object_count = 402
300         req.max_ndr_size = 402116
301         req.extended_op = exop
302         req.fsmo_info = 0
303         req.partial_attribute_set = None
304         req.partial_attribute_set_ex = None
305         req.mapping_ctr.num_mappings = 0
306         req.mapping_ctr.mappings = None
307
308         if not schema and rodc:
309             req.partial_attribute_set = drs_get_rodc_partial_attribute_set(self.samdb)
310
311         if not self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8:
312             req_level = 5
313             req5 = drsuapi.DsGetNCChangesRequest5()
314             for a in dir(req5):
315                 if a[0] != '_':
316                     setattr(req5, a, getattr(req, a))
317             req = req5
318
319         num_objects = 0
320         num_links = 0
321         first_chunk = True
322
323         while True:
324             (level, ctr) = self.drs.DsGetNCChanges(self.drs_handle, req_level, req)
325             if ctr.first_object is None and ctr.object_count != 0:
326                 raise RuntimeError("DsGetNCChanges: NULL first_object with object_count=%u" % (ctr.object_count))
327
328             try:
329                 self.process_chunk(level, ctr, schema, req_level, req, first_chunk)
330             except WERRORError as e:
331                 # Check if retrying with the GET_TGT flag set might resolve this error
332                 if self._should_retry_with_get_tgt(e.args[0], req):
333
334                     print("Missing target object - retrying with DRS_GET_TGT")
335                     req.more_flags |= drsuapi.DRSUAPI_DRS_GET_TGT
336
337                     # try sending the request again (this has the side-effect
338                     # of causing the DC to restart the replication from scratch)
339                     first_chunk = True
340                     continue
341                 else:
342                     raise e
343
344             first_chunk = False
345             num_objects += ctr.object_count
346
347             # Cope with servers that do not return level 6, so do not return any links
348             try:
349                 num_links += ctr.linked_attributes_count
350             except AttributeError:
351                 pass
352
353             if ctr.more_data == 0:
354                 break
355             req.highwatermark = ctr.new_highwatermark
356
357         return (num_objects, num_links)
358
359
360 # Handles the special case of creating a new clone of a DB, while also renaming
361 # the entire DB's objects on the way through
362 class drs_ReplicateRenamer(drs_Replicate):
363     '''Uses DRS replication to rename the entire DB'''
364
365     def __init__(self, binding_string, lp, creds, samdb, invocation_id,
366                  old_base_dn, new_base_dn):
367         super(drs_ReplicateRenamer, self).__init__(binding_string, lp, creds,
368                                                    samdb, invocation_id)
369         self.old_base_dn = old_base_dn
370         self.new_base_dn = new_base_dn
371
372         # because we're renaming the DNs, we know we're going to have trouble
373         # resolving link targets. Normally we'd get to the end of replication
374         # only to find we need to retry the whole replication with the GET_TGT
375         # flag set. Always setting the GET_TGT flag avoids this extra work.
376         self.more_flags = drsuapi.DRSUAPI_DRS_GET_TGT
377
378     def rename_dn(self, dn_str):
379         '''Uses string substitution to replace the base DN'''
380         return re.sub('%s$' % self.old_base_dn, self.new_base_dn, dn_str)
381
382     def update_name_attr(self, base_obj):
383         '''Updates the 'name' attribute for the base DN object'''
384         for attr in base_obj.attribute_ctr.attributes:
385             if attr.attid == DRSUAPI_ATTID_name:
386                 base_dn = ldb.Dn(self.samdb, base_obj.identifier.dn)
387                 new_name = base_dn.get_rdn_value()
388                 attr.value_ctr.values[0].blob = new_name.encode('utf-16-le')
389
390     def rename_top_level_object(self, first_obj):
391         '''Renames the first/top-level object in a partition'''
392         old_dn = first_obj.identifier.dn
393         first_obj.identifier.dn = self.rename_dn(first_obj.identifier.dn)
394         print("Renaming partition %s --> %s" % (old_dn,
395                                                 first_obj.identifier.dn))
396
397         # we also need to fix up the 'name' attribute for the base DN,
398         # otherwise the RDNs won't match
399         if first_obj.identifier.dn == self.new_base_dn:
400             self.update_name_attr(first_obj)
401
402     def process_chunk(self, level, ctr, schema, req_level, req, first_chunk):
403         '''Processes a single chunk of received replication data'''
404
405         # we need to rename the NC in every chunk - this gets used in searches
406         # when applying the chunk
407         if ctr.naming_context:
408             ctr.naming_context.dn = self.rename_dn(ctr.naming_context.dn)
409
410         # rename the first object in each partition. This will cause every
411         # subsequent object in the partiton to be renamed as a side-effect
412         if first_chunk and ctr.object_count != 0:
413             self.rename_top_level_object(ctr.first_object.object)
414
415         # then do the normal repl processing to apply this chunk to our DB
416         super(drs_ReplicateRenamer, self).process_chunk(level, ctr, schema,
417                                                         req_level, req,
418                                                         first_chunk)