PEP8: fix E703: statement ends with a semicolon
[nivanova/samba-autobuild/.git] / source4 / scripting / devel / pfm_verify.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # script to verify cached prefixMap on remote
5 # server against the prefixMap stored in Schema NC
6 #
7 # Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22
23 from __future__ import print_function
24 import os
25 import sys
26 from optparse import OptionParser
27
28 sys.path.insert(0, "bin/python")
29
30 import samba
31 import samba.getopt as options
32 from ldb import SCOPE_BASE, SCOPE_SUBTREE
33 from samba.dcerpc import drsuapi, misc, drsblobs
34 from samba.drs_utils import drs_DsBind
35 from samba.samdb import SamDB
36 from samba.auth import system_session
37 from samba.ndr import ndr_pack, ndr_unpack
38
39
40 def _samdb_fetch_pfm(samdb):
41     """Fetch prefixMap stored in SamDB using LDB connection"""
42     res = samdb.search(base=samdb.get_schema_basedn(), expression="", scope=SCOPE_BASE, attrs=["*"])
43     assert len(res) == 1
44     pfm = ndr_unpack(drsblobs.prefixMapBlob,
45                      str(res[0]['prefixMap']))
46
47     pfm_schi = _samdb_fetch_schi(samdb)
48
49     return (pfm.ctr, pfm_schi)
50
51
52 def _samdb_fetch_schi(samdb):
53     """Fetch schemaInfo stored in SamDB using LDB connection"""
54     res = samdb.search(base=samdb.get_schema_basedn(), expression="", scope=SCOPE_BASE, attrs=["*"])
55     assert len(res) == 1
56     if 'schemaInfo' in res[0]:
57         pfm_schi = ndr_unpack(drsblobs.schemaInfoBlob,
58                               str(res[0]['schemaInfo']))
59     else:
60         pfm_schi = drsblobs.schemaInfoBlob()
61         pfm_schi.marker = 0xFF
62     return pfm_schi
63
64
65 def _drs_fetch_pfm(server, samdb, creds, lp):
66     """Fetch prefixMap using DRS interface"""
67     binding_str = "ncacn_ip_tcp:%s[print,seal]" % server
68
69     drs = drsuapi.drsuapi(binding_str, lp, creds)
70     (drs_handle, supported_extensions) = drs_DsBind(drs)
71     print("DRS Handle: %s" % drs_handle)
72
73     req8 = drsuapi.DsGetNCChangesRequest8()
74
75     dest_dsa = misc.GUID("9c637462-5b8c-4467-aef2-bdb1f57bc4ef")
76     replica_flags = 0
77
78     req8.destination_dsa_guid = dest_dsa
79     req8.source_dsa_invocation_id = misc.GUID(samdb.get_invocation_id())
80     req8.naming_context = drsuapi.DsReplicaObjectIdentifier()
81     req8.naming_context.dn = unicode(samdb.get_schema_basedn())
82     req8.highwatermark = drsuapi.DsReplicaHighWaterMark()
83     req8.highwatermark.tmp_highest_usn = 0
84     req8.highwatermark.reserved_usn = 0
85     req8.highwatermark.highest_usn = 0
86     req8.uptodateness_vector = None
87     req8.replica_flags = replica_flags
88     req8.max_object_count = 0
89     req8.max_ndr_size = 402116
90     req8.extended_op = 0
91     req8.fsmo_info = 0
92     req8.partial_attribute_set = None
93     req8.partial_attribute_set_ex = None
94     req8.mapping_ctr.num_mappings = 0
95     req8.mapping_ctr.mappings = None
96
97     (level, ctr) = drs.DsGetNCChanges(drs_handle, 8, req8)
98     pfm = ctr.mapping_ctr
99     # check for schemaInfo element
100     pfm_it = pfm.mappings[-1]
101     assert pfm_it.id_prefix == 0
102     assert pfm_it.oid.length == 21
103     s = ''
104     for x in pfm_it.oid.binary_oid:
105         s += chr(x)
106     pfm_schi = ndr_unpack(drsblobs.schemaInfoBlob, s)
107     assert pfm_schi.marker == 0xFF
108     # remove schemaInfo element
109     pfm.num_mappings -= 1
110     return (pfm, pfm_schi)
111
112
113 def _pfm_verify(drs_pfm, ldb_pfm):
114     errors = []
115     if drs_pfm.num_mappings != ldb_pfm.num_mappings:
116         errors.append("Different count of prefixes: drs = %d, ldb = %d"
117                       % (drs_pfm.num_mappings, ldb_pfm.num_mappings))
118     count = min(drs_pfm.num_mappings, ldb_pfm.num_mappings)
119     for i in range(0, count):
120         it_err = []
121         drs_it = drs_pfm.mappings[i]
122         ldb_it = ldb_pfm.mappings[i]
123         if drs_it.id_prefix != ldb_it.id_prefix:
124             it_err.append("id_prefix")
125         if drs_it.oid.length != ldb_it.oid.length:
126             it_err.append("oid.length")
127         if drs_it.oid.binary_oid != ldb_it.oid.binary_oid:
128             it_err.append("oid.binary_oid")
129         if len(it_err):
130             errors.append("[%2d] differences in (%s)" % (i, it_err))
131     return errors
132
133
134 def _pfm_schi_verify(drs_schi, ldb_schi):
135     errors = []
136     print(drs_schi.revision)
137     print(drs_schi.invocation_id)
138     if drs_schi.marker != ldb_schi.marker:
139         errors.append("Different marker in schemaInfo: drs = %d, ldb = %d"
140                       % (drs_schi.marker, ldb_schi.marker))
141     if drs_schi.revision != ldb_schi.revision:
142         errors.append("Different revision in schemaInfo: drs = %d, ldb = %d"
143                       % (drs_schi.revision, ldb_schi.revision))
144     if drs_schi.invocation_id != ldb_schi.invocation_id:
145         errors.append("Different invocation_id in schemaInfo: drs = %s, ldb = %s"
146                       % (drs_schi.invocation_id, ldb_schi.invocation_id))
147     return errors
148
149
150 ########### main code ###########
151 if __name__ == "__main__":
152     # command line parsing
153     parser = OptionParser("pfm_verify.py [options] server")
154     sambaopts = options.SambaOptions(parser)
155     parser.add_option_group(sambaopts)
156     credopts = options.CredentialsOptionsDouble(parser)
157     parser.add_option_group(credopts)
158
159     (opts, args) = parser.parse_args()
160
161     lp = sambaopts.get_loadparm()
162     creds = credopts.get_credentials(lp)
163
164     if len(args) != 1:
165         import os
166         if not "DC_SERVER" in os.environ.keys():
167             parser.error("You must supply a server")
168         args.append(os.environ["DC_SERVER"])
169
170     if creds.is_anonymous():
171         parser.error("You must supply credentials")
172         pass
173
174     server = args[0]
175
176     samdb = SamDB(url="ldap://%s" % server,
177                   session_info=system_session(lp),
178                   credentials=creds, lp=lp)
179
180     exit_code = 0
181     (drs_pfm, drs_schi) = _drs_fetch_pfm(server, samdb, creds, lp)
182     (ldb_pfm, ldb_schi) = _samdb_fetch_pfm(samdb)
183     # verify prefixMaps
184     errors = _pfm_verify(drs_pfm, ldb_pfm)
185     if len(errors):
186         print("prefixMap verification errors:")
187         print("%s" % errors)
188         exit_code = 1
189     # verify schemaInfos
190     errors = _pfm_schi_verify(drs_schi, ldb_schi)
191     if len(errors):
192         print("schemaInfo verification errors:")
193         print("%s" % errors)
194         exit_code = 2
195
196     if exit_code != 0:
197         sys.exit(exit_code)