PEP8: fix E128: continuation line under-indented for visual indent
[nivanova/samba-autobuild/.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 class drsException(Exception):
31     """Base element for drs errors"""
32
33     def __init__(self, value):
34         self.value = value
35
36     def __str__(self):
37         return "drsException: " + self.value
38
39
40 def drsuapi_connect(server, lp, creds):
41     """Make a DRSUAPI connection to the server.
42
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
49     """
50
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)
55     try:
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))
60
61     return (drsuapiBind, drsuapiHandle, bindSupportedExtensions)
62
63
64 def sendDsReplicaSync(drsuapiBind, drsuapi_handle, source_dsa_guid,
65                       naming_context, req_option):
66     """Send DS replica sync request.
67
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
75     """
76
77     nc = drsuapi.DsReplicaObjectIdentifier()
78     nc.dn = naming_context
79
80     req1 = drsuapi.DsReplicaSyncRequest1()
81     req1.naming_context = nc;
82     req1.options = req_option
83     req1.source_dsa_guid = misc.GUID(source_dsa_guid)
84
85     try:
86         drsuapiBind.DsReplicaSync(drsuapi_handle, 1, req1)
87     except Exception as estr:
88         raise drsException("DsReplicaSync failed %s" % estr)
89
90
91 def sendRemoveDsServer(drsuapiBind, drsuapi_handle, server_dsa_dn, domain):
92     """Send RemoveDSServer request.
93
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
97         demote
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
101     """
102
103     try:
104         req1 = drsuapi.DsRemoveDSServerRequest1()
105         req1.server_dn = str(server_dsa_dn)
106         req1.domain_dn = str(domain)
107         req1.commit = 1
108
109         drsuapiBind.DsRemoveDSServer(drsuapi_handle, 1, req1)
110     except Exception as estr:
111         raise drsException("DsRemoveDSServer failed %s" % estr)
112
113
114 def drs_DsBind(drs):
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)
148
149     return (handle, info.info.supported_extensions)
150
151
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
156
157     attids = []
158
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
161     # to zero them out
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",
166                               "searchFlags"])
167
168     for r in res:
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)):
174                 continue
175         if "searchFlags" in r:
176             search_flags = r["searchFlags"][0]
177             if (int(search_flags) & samba.dsdb.SEARCH_FLAG_RODC_ATTRIBUTE):
178                 continue
179         attid = samdb.get_attid_from_lDAPDisplayName(ldap_display_name)
180         attids.append(int(attid))
181
182     # the attids do need to be sorted, or windows doesn't return
183     # all the attributes we need
184     attids.sort()
185     partial_attribute_set.attids         = attids
186     partial_attribute_set.num_attids = len(attids)
187     return partial_attribute_set
188
189
190 class drs_Replicate(object):
191     '''DRS replication calls'''
192
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)
197         self.samdb = samdb
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)
203         self.more_flags = 0
204
205     def _should_retry_with_get_tgt(self, error_code, req):
206
207         # If the error indicates we fail to resolve a target object for a
208         # linked attribute, then we should retry the request with GET_TGT
209         # (if we support it and haven't already tried that)
210
211         # TODO fix up the below line when we next update werror_err_table.txt
212         # and pull in the new error-code
213         # return (error_code == werror.WERR_DS_DRA_RECYCLED_TARGET and
214         return (error_code == 0x21bf and
215                 (req.more_flags & drsuapi.DRSUAPI_DRS_GET_TGT) == 0 and
216                 self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10)
217
218     def process_chunk(self, level, ctr, schema, req_level, req, first_chunk):
219         '''Processes a single chunk of received replication data'''
220         # pass the replication into the py_net.c python bindings for processing
221         self.net.replicate_chunk(self.replication_state, level, ctr,
222                                  schema=schema, req_level=req_level, req=req)
223
224     def replicate(self, dn, source_dsa_invocation_id, destination_dsa_guid,
225                   schema=False, exop=drsuapi.DRSUAPI_EXOP_NONE, rodc=False,
226                   replica_flags=None, full_sync=True, sync_forced=False, more_flags=0):
227         '''replicate a single DN'''
228
229         # setup for a GetNCChanges call
230         if self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10:
231             req = drsuapi.DsGetNCChangesRequest10()
232             req.more_flags = (more_flags | self.more_flags)
233             req_level = 10
234         else:
235             req_level = 8
236             req = drsuapi.DsGetNCChangesRequest8()
237
238         req.destination_dsa_guid = destination_dsa_guid
239         req.source_dsa_invocation_id = source_dsa_invocation_id
240         req.naming_context = drsuapi.DsReplicaObjectIdentifier()
241         req.naming_context.dn = dn
242
243         # Default to a full replication if we don't find an upToDatenessVector
244         udv = None
245         hwm = drsuapi.DsReplicaHighWaterMark()
246         hwm.tmp_highest_usn = 0
247         hwm.reserved_usn = 0
248         hwm.highest_usn = 0
249
250         if not full_sync:
251             res = self.samdb.search(base=dn, scope=ldb.SCOPE_BASE,
252                                     attrs=["repsFrom"])
253             if "repsFrom" in res[0]:
254                 for reps_from_packed in res[0]["repsFrom"]:
255                     reps_from_obj = ndr_unpack(drsblobs.repsFromToBlob, reps_from_packed)
256                     if reps_from_obj.ctr.source_dsa_invocation_id == source_dsa_invocation_id:
257                         hwm = reps_from_obj.ctr.highwatermark
258
259             udv = drsuapi.DsReplicaCursorCtrEx()
260             udv.version = 1
261             udv.reserved1 = 0
262             udv.reserved2 = 0
263
264             cursors_v1 = []
265             cursors_v2 = dsdb._dsdb_load_udv_v2(self.samdb,
266                                                 self.samdb.get_default_basedn())
267             for cursor_v2 in cursors_v2:
268                 cursor_v1 = drsuapi.DsReplicaCursor()
269                 cursor_v1.source_dsa_invocation_id = cursor_v2.source_dsa_invocation_id
270                 cursor_v1.highest_usn = cursor_v2.highest_usn
271                 cursors_v1.append(cursor_v1)
272
273             udv.cursors = cursors_v1
274             udv.count = len(cursors_v1)
275
276         req.highwatermark = hwm
277         req.uptodateness_vector = udv
278
279         if replica_flags is not None:
280             req.replica_flags = replica_flags
281         elif exop == drsuapi.DRSUAPI_EXOP_REPL_SECRET:
282             req.replica_flags = 0
283         else:
284             req.replica_flags = (drsuapi.DRSUAPI_DRS_INIT_SYNC |
285                                  drsuapi.DRSUAPI_DRS_PER_SYNC |
286                                  drsuapi.DRSUAPI_DRS_GET_ANC |
287                                  drsuapi.DRSUAPI_DRS_NEVER_SYNCED |
288                                  drsuapi.DRSUAPI_DRS_GET_ALL_GROUP_MEMBERSHIP)
289             if rodc:
290                 req.replica_flags |= (
291                      drsuapi.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING)
292             else:
293                 req.replica_flags |= drsuapi.DRSUAPI_DRS_WRIT_REP
294
295         if sync_forced:
296             req.replica_flags |= drsuapi.DRSUAPI_DRS_SYNC_FORCED
297
298         req.max_object_count = 402
299         req.max_ndr_size = 402116
300         req.extended_op = exop
301         req.fsmo_info = 0
302         req.partial_attribute_set = None
303         req.partial_attribute_set_ex = None
304         req.mapping_ctr.num_mappings = 0
305         req.mapping_ctr.mappings = None
306
307         if not schema and rodc:
308             req.partial_attribute_set = drs_get_rodc_partial_attribute_set(self.samdb)
309
310         if not self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8:
311             req_level = 5
312             req5 = drsuapi.DsGetNCChangesRequest5()
313             for a in dir(req5):
314                 if a[0] != '_':
315                     setattr(req5, a, getattr(req, a))
316             req = req5
317
318         num_objects = 0
319         num_links = 0
320         first_chunk = True
321
322         while True:
323             (level, ctr) = self.drs.DsGetNCChanges(self.drs_handle, req_level, req)
324             if ctr.first_object is None and ctr.object_count != 0:
325                 raise RuntimeError("DsGetNCChanges: NULL first_object with object_count=%u" % (ctr.object_count))
326
327             try:
328                 self.process_chunk(level, ctr, schema, req_level, req, first_chunk)
329             except WERRORError as e:
330                 # Check if retrying with the GET_TGT flag set might resolve this error
331                 if self._should_retry_with_get_tgt(e.args[0], req):
332
333                     print("Missing target object - retrying with DRS_GET_TGT")
334                     req.more_flags |= drsuapi.DRSUAPI_DRS_GET_TGT
335
336                     # try sending the request again (this has the side-effect
337                     # of causing the DC to restart the replication from scratch)
338                     first_chunk = True
339                     continue
340                 else:
341                     raise e
342
343             first_chunk = False
344             num_objects += ctr.object_count
345
346             # Cope with servers that do not return level 6, so do not return any links
347             try:
348                 num_links += ctr.linked_attributes_count
349             except AttributeError:
350                 pass
351
352             if ctr.more_data == 0:
353                 break
354             req.highwatermark = ctr.new_highwatermark
355
356         return (num_objects, num_links)
357
358
359 # Handles the special case of creating a new clone of a DB, while also renaming
360 # the entire DB's objects on the way through
361 class drs_ReplicateRenamer(drs_Replicate):
362     '''Uses DRS replication to rename the entire DB'''
363
364     def __init__(self, binding_string, lp, creds, samdb, invocation_id,
365                  old_base_dn, new_base_dn):
366         super(drs_ReplicateRenamer, self).__init__(binding_string, lp, creds,
367                                                    samdb, invocation_id)
368         self.old_base_dn = old_base_dn
369         self.new_base_dn = new_base_dn
370
371         # because we're renaming the DNs, we know we're going to have trouble
372         # resolving link targets. Normally we'd get to the end of replication
373         # only to find we need to retry the whole replication with the GET_TGT
374         # flag set. Always setting the GET_TGT flag avoids this extra work.
375         self.more_flags = drsuapi.DRSUAPI_DRS_GET_TGT
376
377     def rename_dn(self, dn_str):
378         '''Uses string substitution to replace the base DN'''
379         return re.sub('%s$' % self.old_base_dn, self.new_base_dn, dn_str)
380
381     def update_name_attr(self, base_obj):
382         '''Updates the 'name' attribute for the base DN object'''
383         for attr in base_obj.attribute_ctr.attributes:
384             if attr.attid == DRSUAPI_ATTID_name:
385                 base_dn = ldb.Dn(self.samdb, base_obj.identifier.dn)
386                 new_name = base_dn.get_rdn_value()
387                 attr.value_ctr.values[0].blob = new_name.encode('utf-16-le')
388
389     def rename_top_level_object(self, first_obj):
390         '''Renames the first/top-level object in a partition'''
391         old_dn = first_obj.identifier.dn
392         first_obj.identifier.dn = self.rename_dn(first_obj.identifier.dn)
393         print("Renaming partition %s --> %s" % (old_dn,
394                                                 first_obj.identifier.dn))
395
396         # we also need to fix up the 'name' attribute for the base DN,
397         # otherwise the RDNs won't match
398         if first_obj.identifier.dn == self.new_base_dn:
399             self.update_name_attr(first_obj)
400
401     def process_chunk(self, level, ctr, schema, req_level, req, first_chunk):
402         '''Processes a single chunk of received replication data'''
403
404         # we need to rename the NC in every chunk - this gets used in searches
405         # when applying the chunk
406         if ctr.naming_context:
407             ctr.naming_context.dn = self.rename_dn(ctr.naming_context.dn)
408
409         # rename the first object in each partition. This will cause every
410         # subsequent object in the partiton to be renamed as a side-effect
411         if first_chunk and ctr.object_count != 0:
412             self.rename_top_level_object(ctr.first_object.object)
413
414         # then do the normal repl processing to apply this chunk to our DB
415         super(drs_ReplicateRenamer, self).process_chunk(level, ctr, schema,
416                                                         req_level, req,
417                                                         first_chunk)