5 # Copyright Andrew Tridgell 2010
6 # Copyright Andrew Bartlett 2010
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 from samba
.dcerpc
import drsuapi
, misc
23 from samba
.net
import Net
28 """Base element for drs errors"""
30 def __init__(self
, value
):
34 return "drsException: " + self
.value
37 def drsuapi_connect(server
, lp
, creds
):
39 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 int(lp
.get("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
)
60 def sendDsReplicaSync(drsuapiBind
, drsuapi_handle
, source_dsa_guid
, naming_context
, req_option
):
62 :param drsuapiBind: a drsuapi Bind object
63 :param drsuapi_handle: a drsuapi hanle on the drsuapi connection
64 :param source_dsa_guid: the guid of the source dsa for the replication
65 :param naming_context: the DN of the naming context to replicate
66 :param req_options: replication options for the DsReplicaSync call
67 :raise drsException: if any error occur while sending and receiving the reply
71 nc
= drsuapi
.DsReplicaObjectIdentifier()
72 nc
.dn
= naming_context
74 req1
= drsuapi
.DsReplicaSyncRequest1()
75 req1
.naming_context
= nc
;
76 req1
.options
= req_option
77 req1
.source_dsa_guid
= misc
.GUID(source_dsa_guid
)
80 drsuapiBind
.DsReplicaSync(drsuapi_handle
, 1, req1
)
81 except Exception, estr
:
82 raise drsException("DsReplicaSync failed %s" % estr
)
84 def sendRemoveDsServer(drsuapiBind
, drsuapi_handle
, server_dsa_dn
, domain
):
86 :param drsuapiBind: a drsuapi Bind object
87 :param drsuapi_handle: a drsuapi hanle on the drsuapi connection
88 :param server_dsa_dn: a DN object of the server's dsa that we want to demote
89 :param domain: a DN object of the server's domain
90 :raise drsException: if any error occur while sending and receiving the reply
91 for the DsRemoveDSServer
95 req1
= drsuapi
.DsRemoveDSServerRequest1()
96 req1
.server_dn
= str(server_dsa_dn
)
97 req1
.domain_dn
= str(domain
)
100 drsuapiBind
.DsRemoveDSServer(drsuapi_handle
, 1, req1
)
101 except Exception, estr
:
102 raise drsException("DsRemoveDSServer failed %s" % estr
)
105 '''make a DsBind call, returning the binding handle'''
106 bind_info
= drsuapi
.DsBindInfoCtr()
107 bind_info
.length
= 28
108 bind_info
.info
= drsuapi
.DsBindInfo28()
109 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_BASE
110 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION
111 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI
112 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2
113 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS
114 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1
115 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION
116 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE
117 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2
118 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION
119 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2
120 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD
121 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND
122 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO
123 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION
124 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01
125 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP
126 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY
127 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3
128 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2
129 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6
130 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS
131 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8
132 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5
133 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6
134 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3
135 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7
136 bind_info
.info
.supported_extensions |
= drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT
137 (info
, handle
) = drs
.DsBind(misc
.GUID(drsuapi
.DRSUAPI_DS_BIND_GUID
), bind_info
)
139 return (handle
, info
.info
.supported_extensions
)
143 '''DRS replication calls'''
145 def __init__(self
, binding_string
, lp
, creds
, samdb
):
146 self
.drs
= drsuapi
.drsuapi(binding_string
, lp
, creds
)
147 (self
.drs_handle
, self
.supported_extensions
) = drs_DsBind(self
.drs
)
148 self
.net
= Net(creds
=creds
, lp
=lp
)
150 self
.replication_state
= self
.net
.replicate_init(self
.samdb
, lp
, self
.drs
)
152 def drs_get_rodc_partial_attribute_set(self
):
153 '''get a list of attributes for RODC replication'''
154 partial_attribute_set
= drsuapi
.DsPartialAttributeSet()
155 partial_attribute_set
.version
= 1
159 # the exact list of attids we send is quite critical. Note that
160 # we do ask for the secret attributes, but set SPECIAL_SECRET_PROCESSING
162 schema_dn
= self
.samdb
.get_schema_basedn()
163 res
= self
.samdb
.search(base
=schema_dn
, scope
=ldb
.SCOPE_SUBTREE
,
164 expression
="objectClass=attributeSchema",
165 attrs
=["lDAPDisplayName", "systemFlags",
169 ldap_display_name
= r
["lDAPDisplayName"][0]
170 if "systemFlags" in r
:
171 system_flags
= r
["systemFlags"][0]
172 if (int(system_flags
) & (samba
.dsdb
.DS_FLAG_ATTR_NOT_REPLICATED |
173 samba
.dsdb
.DS_FLAG_ATTR_IS_CONSTRUCTED
)):
175 if "searchFlags" in r
:
176 search_flags
= r
["searchFlags"][0]
177 if (int(search_flags
) & samba
.dsdb
.SEARCH_FLAG_RODC_ATTRIBUTE
):
179 attid
= self
.samdb
.get_attid_from_lDAPDisplayName(ldap_display_name
)
180 attids
.append(int(attid
))
182 # the attids do need to be sorted, or windows doesn't return
183 # all the attributes we need
185 partial_attribute_set
.attids
= attids
186 partial_attribute_set
.num_attids
= len(attids
)
187 return partial_attribute_set
189 def replicate(self
, dn
, source_dsa_invocation_id
, destination_dsa_guid
,
190 schema
=False, exop
=drsuapi
.DRSUAPI_EXOP_NONE
, rodc
=False,
192 '''replicate a single DN'''
194 # setup for a GetNCChanges call
195 req8
= drsuapi
.DsGetNCChangesRequest8()
197 req8
.destination_dsa_guid
= destination_dsa_guid
198 req8
.source_dsa_invocation_id
= source_dsa_invocation_id
199 req8
.naming_context
= drsuapi
.DsReplicaObjectIdentifier()
200 req8
.naming_context
.dn
= dn
201 req8
.highwatermark
= drsuapi
.DsReplicaHighWaterMark()
202 req8
.highwatermark
.tmp_highest_usn
= 0
203 req8
.highwatermark
.reserved_usn
= 0
204 req8
.highwatermark
.highest_usn
= 0
205 req8
.uptodateness_vector
= None
206 if replica_flags
is not None:
207 req8
.replica_flags
= replica_flags
208 elif exop
== drsuapi
.DRSUAPI_EXOP_REPL_SECRET
:
209 req8
.replica_flags
= 0
211 req8
.replica_flags
= (drsuapi
.DRSUAPI_DRS_INIT_SYNC |
212 drsuapi
.DRSUAPI_DRS_PER_SYNC |
213 drsuapi
.DRSUAPI_DRS_GET_ANC |
214 drsuapi
.DRSUAPI_DRS_NEVER_SYNCED
)
216 req8
.replica_flags |
= drsuapi
.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING
218 req8
.replica_flags |
= drsuapi
.DRSUAPI_DRS_WRIT_REP
219 req8
.max_object_count
= 402
220 req8
.max_ndr_size
= 402116
221 req8
.extended_op
= exop
223 req8
.partial_attribute_set
= None
224 req8
.partial_attribute_set_ex
= None
225 req8
.mapping_ctr
.num_mappings
= 0
226 req8
.mapping_ctr
.mappings
= None
228 if not schema
and rodc
:
229 req8
.partial_attribute_set
= self
.drs_get_rodc_partial_attribute_set()
231 if self
.supported_extensions
& drsuapi
.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8
:
236 req5
= drsuapi
.DsGetNCChangesRequest5()
239 setattr(req5
, a
, getattr(req8
, a
))
244 (level
, ctr
) = self
.drs
.DsGetNCChanges(self
.drs_handle
, req_level
, req
)
245 if ctr
.first_object
== None and ctr
.object_count
!= 0:
246 raise RuntimeError("DsGetNCChanges: NULL first_object with object_count=%u" % (ctr
.object_count
))
247 self
.net
.replicate_chunk(self
.replication_state
, level
, ctr
,
248 schema
=schema
, req_level
=req_level
, req
=req
)
249 if ctr
.more_data
== 0:
251 req
.highwatermark
.tmp_highest_usn
= ctr
.new_highwatermark
.tmp_highest_usn