3 # Copyright Andrew Tridgell 2010
4 # Copyright Andrew Bartlett 2017
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.
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.
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/>.
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
27 class drsException(Exception):
28 """Base element for drs errors"""
30 def __init__(self
, value
):
34 return "drsException: " + self
.value
37 def drsuapi_connect(server
, lp
, creds
):
38 """Make a DRSUAPI connection to the server.
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
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
)
53 drsuapiBind
= drsuapi
.drsuapi(binding_string
, lp
, creds
)
54 (drsuapiHandle
, bindSupportedExtensions
) = drs_DsBind(drsuapiBind
)
56 raise drsException("DRS connection to %s failed: %s" % (server
, e
))
58 return (drsuapiBind
, drsuapiHandle
, bindSupportedExtensions
)
61 def sendDsReplicaSync(drsuapiBind
, drsuapi_handle
, source_dsa_guid
,
62 naming_context
, req_option
):
63 """Send DS replica sync request.
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
74 nc
= drsuapi
.DsReplicaObjectIdentifier()
75 nc
.dn
= naming_context
77 req1
= drsuapi
.DsReplicaSyncRequest1()
78 req1
.naming_context
= nc
;
79 req1
.options
= req_option
80 req1
.source_dsa_guid
= misc
.GUID(source_dsa_guid
)
83 drsuapiBind
.DsReplicaSync(drsuapi_handle
, 1, req1
)
84 except Exception, estr
:
85 raise drsException("DsReplicaSync failed %s" % estr
)
88 def sendRemoveDsServer(drsuapiBind
, drsuapi_handle
, server_dsa_dn
, domain
):
89 """Send RemoveDSServer request.
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
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
101 req1
= drsuapi
.DsRemoveDSServerRequest1()
102 req1
.server_dn
= str(server_dsa_dn
)
103 req1
.domain_dn
= str(domain
)
106 drsuapiBind
.DsRemoveDSServer(drsuapi_handle
, 1, req1
)
107 except Exception, estr
:
108 raise drsException("DsRemoveDSServer failed %s" % estr
)
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
)
146 return (handle
, info
.info
.supported_extensions
)
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
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
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",
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
)):
172 if "searchFlags" in r
:
173 search_flags
= r
["searchFlags"][0]
174 if (int(search_flags
) & samba
.dsdb
.SEARCH_FLAG_RODC_ATTRIBUTE
):
176 attid
= samdb
.get_attid_from_lDAPDisplayName(ldap_display_name
)
177 attids
.append(int(attid
))
179 # the attids do need to be sorted, or windows doesn't return
180 # all the attributes we need
182 partial_attribute_set
.attids
= attids
183 partial_attribute_set
.num_attids
= len(attids
)
184 return partial_attribute_set
187 class drs_Replicate(object):
188 '''DRS replication calls'''
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
)
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
)
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):
204 '''replicate a single DN'''
206 # setup for a GetNCChanges call
207 req8
= drsuapi
.DsGetNCChangesRequest8()
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
216 res
= self
.samdb
.search(base
=dn
, scope
=ldb
.SCOPE_BASE
,
218 if "repsFrom" in res
[0]:
219 for reps_from_packed
in res
[0]["repsFrom"]:
220 reps_from_obj
= ndr_unpack(drsblobs
.repsFromToBlob
, reps_from_packed
)
221 if reps_from_obj
.ctr
.source_dsa_invocation_id
== source_dsa_invocation_id
:
222 hwm
= reps_from_obj
.ctr
.highwatermark
224 udv
= drsuapi
.DsReplicaCursorCtrEx()
230 cursors_v2
= dsdb
._dsdb
_load
_udv
_v
2(self
.samdb
,
231 self
.samdb
.get_default_basedn())
232 for cursor_v2
in cursors_v2
:
233 cursor_v1
= drsuapi
.DsReplicaCursor()
234 cursor_v1
.source_dsa_invocation_id
= cursor_v2
.source_dsa_invocation_id
235 cursor_v1
.highest_usn
= cursor_v2
.highest_usn
236 cursors_v1
.append(cursor_v1
)
238 udv
.cursors
= cursors_v1
239 udv
.count
= len(cursors_v1
)
241 # If we can't find an upToDateVector, or where told not to, replicate fully
242 hwm
= drsuapi
.DsReplicaHighWaterMark()
243 hwm
.tmp_highest_usn
= 0
247 req8
.highwatermark
= hwm
248 req8
.uptodateness_vector
= udv
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
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
)
261 req8
.replica_flags |
= (
262 drsuapi
.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING
)
264 req8
.replica_flags |
= drsuapi
.DRSUAPI_DRS_WRIT_REP
265 req8
.max_object_count
= 402
266 req8
.max_ndr_size
= 402116
267 req8
.extended_op
= exop
269 req8
.partial_attribute_set
= None
270 req8
.partial_attribute_set_ex
= None
271 req8
.mapping_ctr
.num_mappings
= 0
272 req8
.mapping_ctr
.mappings
= None
274 if not schema
and rodc
:
275 req8
.partial_attribute_set
= drs_get_rodc_partial_attribute_set(self
.samdb
)
277 if self
.supported_extensions
& drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8
:
282 req5
= drsuapi
.DsGetNCChangesRequest5()
285 setattr(req5
, a
, getattr(req8
, a
))
291 (level
, ctr
) = self
.drs
.DsGetNCChanges(self
.drs_handle
, req_level
, req
)
292 if ctr
.first_object
is None and ctr
.object_count
!= 0:
293 raise RuntimeError("DsGetNCChanges: NULL first_object with object_count=%u" % (ctr
.object_count
))
294 self
.net
.replicate_chunk(self
.replication_state
, level
, ctr
,
295 schema
=schema
, req_level
=req_level
, req
=req
)
297 num_objects
+= ctr
.object_count
299 # Cope with servers that do not return level 6, so do not return any links
301 num_links
+= ctr
.linked_attributes_count
302 except AttributeError:
305 if ctr
.more_data
== 0:
307 req
.highwatermark
= ctr
.new_highwatermark
309 return (num_objects
, num_links
)