3 # Copyright Andrew Tridgell 2010
4 # Copyright Andrew Bartlett 2010
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
21 from samba
.net
import Net
25 class drsException(Exception):
26 """Base element for drs errors"""
28 def __init__(self
, value
):
32 return "drsException: " + self
.value
35 def drsuapi_connect(server
, lp
, creds
):
36 """Make a DRSUAPI connection to the server.
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
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
)
51 drsuapiBind
= drsuapi
.drsuapi(binding_string
, lp
, creds
)
52 (drsuapiHandle
, bindSupportedExtensions
) = drs_DsBind(drsuapiBind
)
54 raise drsException("DRS connection to %s failed: %s" % (server
, e
))
56 return (drsuapiBind
, drsuapiHandle
, bindSupportedExtensions
)
59 def sendDsReplicaSync(drsuapiBind
, drsuapi_handle
, source_dsa_guid
,
60 naming_context
, req_option
):
61 """Send DS replica sync request.
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
72 nc
= drsuapi
.DsReplicaObjectIdentifier()
73 nc
.dn
= naming_context
75 req1
= drsuapi
.DsReplicaSyncRequest1()
76 req1
.naming_context
= nc
;
77 req1
.options
= req_option
78 req1
.source_dsa_guid
= misc
.GUID(source_dsa_guid
)
81 drsuapiBind
.DsReplicaSync(drsuapi_handle
, 1, req1
)
82 except Exception, estr
:
83 raise drsException("DsReplicaSync failed %s" % estr
)
86 def sendRemoveDsServer(drsuapiBind
, drsuapi_handle
, server_dsa_dn
, domain
):
87 """Send RemoveDSServer request.
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
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
99 req1
= drsuapi
.DsRemoveDSServerRequest1()
100 req1
.server_dn
= str(server_dsa_dn
)
101 req1
.domain_dn
= str(domain
)
104 drsuapiBind
.DsRemoveDSServer(drsuapi_handle
, 1, req1
)
105 except Exception, estr
:
106 raise drsException("DsRemoveDSServer failed %s" % estr
)
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
)
144 return (handle
, info
.info
.supported_extensions
)
147 class drs_Replicate(object):
148 '''DRS replication calls'''
150 def __init__(self
, binding_string
, lp
, creds
, samdb
, invocation_id
):
151 self
.drs
= drsuapi
.drsuapi(binding_string
, lp
, creds
)
152 (self
.drs_handle
, self
.supported_extensions
) = drs_DsBind(self
.drs
)
153 self
.net
= Net(creds
=creds
, lp
=lp
)
155 if not isinstance(invocation_id
, misc
.GUID
):
156 raise RuntimeError("Must supply GUID for invocation_id")
157 if invocation_id
== misc
.GUID("00000000-0000-0000-0000-000000000000"):
158 raise RuntimeError("Must not set GUID 00000000-0000-0000-0000-000000000000 as invocation_id")
159 self
.replication_state
= self
.net
.replicate_init(self
.samdb
, lp
, self
.drs
, invocation_id
)
161 def drs_get_rodc_partial_attribute_set(self
):
162 '''get a list of attributes for RODC replication'''
163 partial_attribute_set
= drsuapi
.DsPartialAttributeSet()
164 partial_attribute_set
.version
= 1
168 # the exact list of attids we send is quite critical. Note that
169 # we do ask for the secret attributes, but set SPECIAL_SECRET_PROCESSING
171 schema_dn
= self
.samdb
.get_schema_basedn()
172 res
= self
.samdb
.search(base
=schema_dn
, scope
=ldb
.SCOPE_SUBTREE
,
173 expression
="objectClass=attributeSchema",
174 attrs
=["lDAPDisplayName", "systemFlags",
178 ldap_display_name
= r
["lDAPDisplayName"][0]
179 if "systemFlags" in r
:
180 system_flags
= r
["systemFlags"][0]
181 if (int(system_flags
) & (samba
.dsdb
.DS_FLAG_ATTR_NOT_REPLICATED |
182 samba
.dsdb
.DS_FLAG_ATTR_IS_CONSTRUCTED
)):
184 if "searchFlags" in r
:
185 search_flags
= r
["searchFlags"][0]
186 if (int(search_flags
) & samba
.dsdb
.SEARCH_FLAG_RODC_ATTRIBUTE
):
188 attid
= self
.samdb
.get_attid_from_lDAPDisplayName(ldap_display_name
)
189 attids
.append(int(attid
))
191 # the attids do need to be sorted, or windows doesn't return
192 # all the attributes we need
194 partial_attribute_set
.attids
= attids
195 partial_attribute_set
.num_attids
= len(attids
)
196 return partial_attribute_set
198 def replicate(self
, dn
, source_dsa_invocation_id
, destination_dsa_guid
,
199 schema
=False, exop
=drsuapi
.DRSUAPI_EXOP_NONE
, rodc
=False,
201 '''replicate a single DN'''
203 # setup for a GetNCChanges call
204 req8
= drsuapi
.DsGetNCChangesRequest8()
206 req8
.destination_dsa_guid
= destination_dsa_guid
207 req8
.source_dsa_invocation_id
= source_dsa_invocation_id
208 req8
.naming_context
= drsuapi
.DsReplicaObjectIdentifier()
209 req8
.naming_context
.dn
= dn
210 req8
.highwatermark
= drsuapi
.DsReplicaHighWaterMark()
211 req8
.highwatermark
.tmp_highest_usn
= 0
212 req8
.highwatermark
.reserved_usn
= 0
213 req8
.highwatermark
.highest_usn
= 0
214 req8
.uptodateness_vector
= None
215 if replica_flags
is not None:
216 req8
.replica_flags
= replica_flags
217 elif exop
== drsuapi
.DRSUAPI_EXOP_REPL_SECRET
:
218 req8
.replica_flags
= 0
220 req8
.replica_flags
= (drsuapi
.DRSUAPI_DRS_INIT_SYNC |
221 drsuapi
.DRSUAPI_DRS_PER_SYNC |
222 drsuapi
.DRSUAPI_DRS_GET_ANC |
223 drsuapi
.DRSUAPI_DRS_NEVER_SYNCED
)
225 req8
.replica_flags |
= (
226 drsuapi
.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING
)
228 req8
.replica_flags |
= drsuapi
.DRSUAPI_DRS_WRIT_REP
229 req8
.max_object_count
= 402
230 req8
.max_ndr_size
= 402116
231 req8
.extended_op
= exop
233 req8
.partial_attribute_set
= None
234 req8
.partial_attribute_set_ex
= None
235 req8
.mapping_ctr
.num_mappings
= 0
236 req8
.mapping_ctr
.mappings
= None
238 if not schema
and rodc
:
239 req8
.partial_attribute_set
= self
.drs_get_rodc_partial_attribute_set()
241 if self
.supported_extensions
& drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8
:
246 req5
= drsuapi
.DsGetNCChangesRequest5()
249 setattr(req5
, a
, getattr(req8
, a
))
253 (level
, ctr
) = self
.drs
.DsGetNCChanges(self
.drs_handle
, req_level
, req
)
254 if ctr
.first_object
is None and ctr
.object_count
!= 0:
255 raise RuntimeError("DsGetNCChanges: NULL first_object with object_count=%u" % (ctr
.object_count
))
256 self
.net
.replicate_chunk(self
.replication_state
, level
, ctr
,
257 schema
=schema
, req_level
=req_level
, req
=req
)
258 if ctr
.more_data
== 0:
260 req
.highwatermark
= ctr
.new_highwatermark