7fab4802522fefb2a865a85b52d24f2558a403a3
[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
28
29 class drsException(Exception):
30     """Base element for drs errors"""
31
32     def __init__(self, value):
33         self.value = value
34
35     def __str__(self):
36         return "drsException: " + self.value
37
38
39 def drsuapi_connect(server, lp, creds):
40     """Make a DRSUAPI connection to the server.
41
42     :param server: the name of the server to connect to
43     :param lp: a samba line parameter object
44     :param creds: credential used for the connection
45     :return: A tuple with the drsuapi bind object, the drsuapi handle
46                 and the supported extensions.
47     :raise drsException: if the connection fails
48     """
49
50     binding_options = "seal"
51     if lp.log_level() >= 9:
52         binding_options += ",print"
53     binding_string = "ncacn_ip_tcp:%s[%s]" % (server, binding_options)
54     try:
55         drsuapiBind = drsuapi.drsuapi(binding_string, lp, creds)
56         (drsuapiHandle, bindSupportedExtensions) = drs_DsBind(drsuapiBind)
57     except Exception as e:
58         raise drsException("DRS connection to %s failed: %s" % (server, e))
59
60     return (drsuapiBind, drsuapiHandle, bindSupportedExtensions)
61
62
63 def sendDsReplicaSync(drsuapiBind, drsuapi_handle, source_dsa_guid,
64         naming_context, req_option):
65     """Send DS replica sync request.
66
67     :param drsuapiBind: a drsuapi Bind object
68     :param drsuapi_handle: a drsuapi handle on the drsuapi connection
69     :param source_dsa_guid: the guid of the source dsa for the replication
70     :param naming_context: the DN of the naming context to replicate
71     :param req_options: replication options for the DsReplicaSync call
72     :raise drsException: if any error occur while sending and receiving the
73         reply for the dsReplicaSync
74     """
75
76     nc = drsuapi.DsReplicaObjectIdentifier()
77     nc.dn = naming_context
78
79     req1 = drsuapi.DsReplicaSyncRequest1()
80     req1.naming_context = nc;
81     req1.options = req_option
82     req1.source_dsa_guid = misc.GUID(source_dsa_guid)
83
84     try:
85         drsuapiBind.DsReplicaSync(drsuapi_handle, 1, req1)
86     except Exception as estr:
87         raise drsException("DsReplicaSync failed %s" % estr)
88
89
90 def sendRemoveDsServer(drsuapiBind, drsuapi_handle, server_dsa_dn, domain):
91     """Send RemoveDSServer request.
92
93     :param drsuapiBind: a drsuapi Bind object
94     :param drsuapi_handle: a drsuapi handle on the drsuapi connection
95     :param server_dsa_dn: a DN object of the server's dsa that we want to
96         demote
97     :param domain: a DN object of the server's domain
98     :raise drsException: if any error occur while sending and receiving the
99         reply for the DsRemoveDSServer
100     """
101
102     try:
103         req1 = drsuapi.DsRemoveDSServerRequest1()
104         req1.server_dn = str(server_dsa_dn)
105         req1.domain_dn = str(domain)
106         req1.commit = 1
107
108         drsuapiBind.DsRemoveDSServer(drsuapi_handle, 1, req1)
109     except Exception as estr:
110         raise drsException("DsRemoveDSServer failed %s" % estr)
111
112
113 def drs_DsBind(drs):
114     '''make a DsBind call, returning the binding handle'''
115     bind_info = drsuapi.DsBindInfoCtr()
116     bind_info.length = 28
117     bind_info.info = drsuapi.DsBindInfo28()
118     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_BASE
119     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION
120     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI
121     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2
122     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS
123     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1
124     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION
125     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE
126     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2
127     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION
128     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2
129     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD
130     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND
131     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO
132     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION
133     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01
134     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP
135     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY
136     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3
137     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2
138     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6
139     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS
140     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8
141     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5
142     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6
143     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3
144     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7
145     bind_info.info.supported_extensions |= drsuapi.DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT
146     (info, handle) = drs.DsBind(misc.GUID(drsuapi.DRSUAPI_DS_BIND_GUID), bind_info)
147
148     return (handle, info.info.supported_extensions)
149
150
151 def drs_get_rodc_partial_attribute_set(samdb):
152     '''get a list of attributes for RODC replication'''
153     partial_attribute_set = drsuapi.DsPartialAttributeSet()
154     partial_attribute_set.version = 1
155
156     attids = []
157
158     # the exact list of attids we send is quite critical. Note that
159     # we do ask for the secret attributes, but set SPECIAL_SECRET_PROCESSING
160     # to zero them out
161     schema_dn = samdb.get_schema_basedn()
162     res = samdb.search(base=schema_dn, scope=ldb.SCOPE_SUBTREE,
163                        expression="objectClass=attributeSchema",
164                        attrs=["lDAPDisplayName", "systemFlags",
165                               "searchFlags"])
166
167     for r in res:
168         ldap_display_name = r["lDAPDisplayName"][0]
169         if "systemFlags" in r:
170             system_flags      = r["systemFlags"][0]
171             if (int(system_flags) & (samba.dsdb.DS_FLAG_ATTR_NOT_REPLICATED |
172                                      samba.dsdb.DS_FLAG_ATTR_IS_CONSTRUCTED)):
173                 continue
174         if "searchFlags" in r:
175             search_flags = r["searchFlags"][0]
176             if (int(search_flags) & samba.dsdb.SEARCH_FLAG_RODC_ATTRIBUTE):
177                 continue
178         attid = samdb.get_attid_from_lDAPDisplayName(ldap_display_name)
179         attids.append(int(attid))
180
181     # the attids do need to be sorted, or windows doesn't return
182     # all the attributes we need
183     attids.sort()
184     partial_attribute_set.attids         = attids
185     partial_attribute_set.num_attids = len(attids)
186     return partial_attribute_set
187
188
189 class drs_Replicate(object):
190     '''DRS replication calls'''
191
192     def __init__(self, binding_string, lp, creds, samdb, invocation_id):
193         self.drs = drsuapi.drsuapi(binding_string, lp, creds)
194         (self.drs_handle, self.supported_extensions) = drs_DsBind(self.drs)
195         self.net = Net(creds=creds, lp=lp)
196         self.samdb = samdb
197         if not isinstance(invocation_id, misc.GUID):
198             raise RuntimeError("Must supply GUID for invocation_id")
199         if invocation_id == misc.GUID("00000000-0000-0000-0000-000000000000"):
200             raise RuntimeError("Must not set GUID 00000000-0000-0000-0000-000000000000 as invocation_id")
201         self.replication_state = self.net.replicate_init(self.samdb, lp, self.drs, invocation_id)
202
203     def _should_retry_with_get_tgt(self, error_code, req):
204
205         # If the error indicates we fail to resolve a target object for a
206         # linked attribute, then we should retry the request with GET_TGT
207         # (if we support it and haven't already tried that)
208
209         # TODO fix up the below line when we next update werror_err_table.txt
210         # and pull in the new error-code
211         # return (error_code == werror.WERR_DS_DRA_RECYCLED_TARGET and
212         return (error_code == 0x21bf and
213                 (req.more_flags & drsuapi.DRSUAPI_DRS_GET_TGT) == 0 and
214                 self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10)
215
216     def replicate(self, dn, source_dsa_invocation_id, destination_dsa_guid,
217                   schema=False, exop=drsuapi.DRSUAPI_EXOP_NONE, rodc=False,
218                   replica_flags=None, full_sync=True, sync_forced=False, more_flags=0):
219         '''replicate a single DN'''
220
221         # setup for a GetNCChanges call
222         if self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10:
223             req = drsuapi.DsGetNCChangesRequest10()
224             req.more_flags = more_flags
225             req_level = 10
226         else:
227             req_level = 8
228             req = drsuapi.DsGetNCChangesRequest8()
229
230         req.destination_dsa_guid = destination_dsa_guid
231         req.source_dsa_invocation_id = source_dsa_invocation_id
232         req.naming_context = drsuapi.DsReplicaObjectIdentifier()
233         req.naming_context.dn = dn
234
235         # Default to a full replication if we don't find an upToDatenessVector
236         udv = None
237         hwm = drsuapi.DsReplicaHighWaterMark()
238         hwm.tmp_highest_usn = 0
239         hwm.reserved_usn = 0
240         hwm.highest_usn = 0
241
242         if not full_sync:
243             res = self.samdb.search(base=dn, scope=ldb.SCOPE_BASE,
244                                     attrs=["repsFrom"])
245             if "repsFrom" in res[0]:
246                 for reps_from_packed in res[0]["repsFrom"]:
247                     reps_from_obj = ndr_unpack(drsblobs.repsFromToBlob, reps_from_packed)
248                     if reps_from_obj.ctr.source_dsa_invocation_id == source_dsa_invocation_id:
249                         hwm = reps_from_obj.ctr.highwatermark
250
251             udv = drsuapi.DsReplicaCursorCtrEx()
252             udv.version = 1
253             udv.reserved1 = 0
254             udv.reserved2 = 0
255
256             cursors_v1 = []
257             cursors_v2 = dsdb._dsdb_load_udv_v2(self.samdb,
258                                                 self.samdb.get_default_basedn())
259             for cursor_v2 in cursors_v2:
260                 cursor_v1 = drsuapi.DsReplicaCursor()
261                 cursor_v1.source_dsa_invocation_id = cursor_v2.source_dsa_invocation_id
262                 cursor_v1.highest_usn = cursor_v2.highest_usn
263                 cursors_v1.append(cursor_v1)
264
265             udv.cursors = cursors_v1
266             udv.count = len(cursors_v1)
267
268         req.highwatermark = hwm
269         req.uptodateness_vector = udv
270
271         if replica_flags is not None:
272             req.replica_flags = replica_flags
273         elif exop == drsuapi.DRSUAPI_EXOP_REPL_SECRET:
274             req.replica_flags = 0
275         else:
276             req.replica_flags = (drsuapi.DRSUAPI_DRS_INIT_SYNC |
277                                  drsuapi.DRSUAPI_DRS_PER_SYNC |
278                                  drsuapi.DRSUAPI_DRS_GET_ANC |
279                                  drsuapi.DRSUAPI_DRS_NEVER_SYNCED |
280                                  drsuapi.DRSUAPI_DRS_GET_ALL_GROUP_MEMBERSHIP)
281             if rodc:
282                 req.replica_flags |= (
283                      drsuapi.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING)
284             else:
285                 req.replica_flags |= drsuapi.DRSUAPI_DRS_WRIT_REP
286
287         if sync_forced:
288             req.replica_flags |= drsuapi.DRSUAPI_DRS_SYNC_FORCED
289
290         req.max_object_count = 402
291         req.max_ndr_size = 402116
292         req.extended_op = exop
293         req.fsmo_info = 0
294         req.partial_attribute_set = None
295         req.partial_attribute_set_ex = None
296         req.mapping_ctr.num_mappings = 0
297         req.mapping_ctr.mappings = None
298
299         if not schema and rodc:
300             req.partial_attribute_set = drs_get_rodc_partial_attribute_set(self.samdb)
301
302         if not self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8:
303             req_level = 5
304             req5 = drsuapi.DsGetNCChangesRequest5()
305             for a in dir(req5):
306                 if a[0] != '_':
307                     setattr(req5, a, getattr(req, a))
308             req = req5
309
310         num_objects = 0
311         num_links = 0
312         while True:
313             (level, ctr) = self.drs.DsGetNCChanges(self.drs_handle, req_level, req)
314             if ctr.first_object is None and ctr.object_count != 0:
315                 raise RuntimeError("DsGetNCChanges: NULL first_object with object_count=%u" % (ctr.object_count))
316
317             try:
318                 self.net.replicate_chunk(self.replication_state, level, ctr,
319                     schema=schema, req_level=req_level, req=req)
320             except WERRORError as e:
321                 # Check if retrying with the GET_TGT flag set might resolve this error
322                 if self._should_retry_with_get_tgt(e[0], req):
323
324                     print("Missing target object - retrying with DRS_GET_TGT")
325                     req.more_flags |= drsuapi.DRSUAPI_DRS_GET_TGT
326
327                     # try sending the request again
328                     continue
329                 else:
330                     raise e
331
332             num_objects += ctr.object_count
333
334             # Cope with servers that do not return level 6, so do not return any links
335             try:
336                 num_links += ctr.linked_attributes_count
337             except AttributeError:
338                 pass
339
340             if ctr.more_data == 0:
341                 break
342             req.highwatermark = ctr.new_highwatermark
343
344         return (num_objects, num_links)