samba-tool dbcheck: Add --reset-well-known-acls
[nivanova/samba-autobuild/.git] / python / samba / drs_utils.py
1 # DRS utility code
2 #
3 # Copyright Andrew Tridgell 2010
4 # Copyright Andrew Bartlett 2010
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 int(lp.get("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 class drs_Replicate(object):
148     '''DRS replication calls'''
149
150     def __init__(self, binding_string, lp, creds, samdb):
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)
154         self.samdb = samdb
155         self.replication_state = self.net.replicate_init(self.samdb, lp, self.drs)
156
157     def drs_get_rodc_partial_attribute_set(self):
158         '''get a list of attributes for RODC replication'''
159         partial_attribute_set = drsuapi.DsPartialAttributeSet()
160         partial_attribute_set.version = 1
161
162         attids = []
163
164         # the exact list of attids we send is quite critical. Note that
165         # we do ask for the secret attributes, but set SPECIAL_SECRET_PROCESSING
166         # to zero them out
167         schema_dn = self.samdb.get_schema_basedn()
168         res = self.samdb.search(base=schema_dn, scope=ldb.SCOPE_SUBTREE,
169                                       expression="objectClass=attributeSchema",
170                                       attrs=["lDAPDisplayName", "systemFlags",
171                                              "searchFlags"])
172
173         for r in res:
174             ldap_display_name = r["lDAPDisplayName"][0]
175             if "systemFlags" in r:
176                 system_flags      = r["systemFlags"][0]
177                 if (int(system_flags) & (samba.dsdb.DS_FLAG_ATTR_NOT_REPLICATED |
178                                          samba.dsdb.DS_FLAG_ATTR_IS_CONSTRUCTED)):
179                     continue
180             if "searchFlags" in r:
181                 search_flags = r["searchFlags"][0]
182                 if (int(search_flags) & samba.dsdb.SEARCH_FLAG_RODC_ATTRIBUTE):
183                     continue
184             attid = self.samdb.get_attid_from_lDAPDisplayName(ldap_display_name)
185             attids.append(int(attid))
186
187         # the attids do need to be sorted, or windows doesn't return
188         # all the attributes we need
189         attids.sort()
190         partial_attribute_set.attids         = attids
191         partial_attribute_set.num_attids = len(attids)
192         return partial_attribute_set
193
194     def replicate(self, dn, source_dsa_invocation_id, destination_dsa_guid,
195                   schema=False, exop=drsuapi.DRSUAPI_EXOP_NONE, rodc=False,
196                   replica_flags=None):
197         '''replicate a single DN'''
198
199         # setup for a GetNCChanges call
200         req8 = drsuapi.DsGetNCChangesRequest8()
201
202         req8.destination_dsa_guid           = destination_dsa_guid
203         req8.source_dsa_invocation_id       = source_dsa_invocation_id
204         req8.naming_context                 = drsuapi.DsReplicaObjectIdentifier()
205         req8.naming_context.dn              = dn
206         req8.highwatermark                  = drsuapi.DsReplicaHighWaterMark()
207         req8.highwatermark.tmp_highest_usn  = 0
208         req8.highwatermark.reserved_usn     = 0
209         req8.highwatermark.highest_usn      = 0
210         req8.uptodateness_vector            = None
211         if replica_flags is not None:
212             req8.replica_flags = replica_flags
213         elif exop == drsuapi.DRSUAPI_EXOP_REPL_SECRET:
214             req8.replica_flags              = 0
215         else:
216             req8.replica_flags              = (drsuapi.DRSUAPI_DRS_INIT_SYNC |
217                                                drsuapi.DRSUAPI_DRS_PER_SYNC |
218                                                drsuapi.DRSUAPI_DRS_GET_ANC |
219                                                drsuapi.DRSUAPI_DRS_NEVER_SYNCED)
220             if rodc:
221                 req8.replica_flags |= drsuapi.DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING
222             else:
223                 req8.replica_flags |= drsuapi.DRSUAPI_DRS_WRIT_REP
224         req8.max_object_count = 402
225         req8.max_ndr_size = 402116
226         req8.extended_op = exop
227         req8.fsmo_info = 0
228         req8.partial_attribute_set = None
229         req8.partial_attribute_set_ex = None
230         req8.mapping_ctr.num_mappings = 0
231         req8.mapping_ctr.mappings = None
232
233         if not schema and rodc:
234             req8.partial_attribute_set = self.drs_get_rodc_partial_attribute_set()
235
236         if self.supported_extensions & drsuapi.DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8:
237             req_level = 8
238             req = req8
239         else:
240             req_level = 5
241             req5 = drsuapi.DsGetNCChangesRequest5()
242             for a in dir(req5):
243                 if a[0] != '_':
244                     setattr(req5, a, getattr(req8, a))
245             req = req5
246
247         while True:
248             (level, ctr) = self.drs.DsGetNCChanges(self.drs_handle, req_level, req)
249             if ctr.first_object is None and ctr.object_count != 0:
250                 raise RuntimeError("DsGetNCChanges: NULL first_object with object_count=%u" % (ctr.object_count))
251             self.net.replicate_chunk(self.replication_state, level, ctr,
252                 schema=schema, req_level=req_level, req=req)
253             if ctr.more_data == 0:
254                 break
255             req.highwatermark = ctr.new_highwatermark