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