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