2f7be2b4bf28ab312e74c1657d216b3dc0b3fdf5
[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 import os
24 import sys
25 from optparse import OptionParser
26
27 sys.path.insert(0, "bin/python")
28
29 import samba
30 import samba.getopt as options
31 from ldb import SCOPE_BASE, SCOPE_SUBTREE
32 from samba.dcerpc import drsuapi, misc, drsblobs
33 from samba.drs_utils import drs_DsBind
34 from samba.samdb import SamDB
35 from samba.auth import system_session
36 from samba.ndr import ndr_pack, ndr_unpack
37
38
39 def _samdb_fetch_pfm(samdb):
40     """Fetch prefixMap stored in SamDB using LDB connection"""
41     res = samdb.search(base=samdb.get_schema_basedn(), expression="", scope=SCOPE_BASE, attrs=["*"])
42     assert len(res) == 1
43     pfm = ndr_unpack(drsblobs.prefixMapBlob,
44                      str(res[0]['prefixMap']))
45     return pfm.ctr
46
47 def _drs_fetch_pfm(server, samdb, creds, lp):
48     """Fetch prefixMap using DRS interface"""
49     binding_str = "ncacn_ip_tcp:%s[print,seal]" % server
50
51     drs = drsuapi.drsuapi(binding_str, lp, creds)
52     (drs_handle, supported_extensions) = drs_DsBind(drs)
53     print "DRS Handle: %s" % drs_handle
54
55     req8 = drsuapi.DsGetNCChangesRequest8()
56
57     dest_dsa = misc.GUID("9c637462-5b8c-4467-aef2-bdb1f57bc4ef")
58     replica_flags = 0
59
60     req8.destination_dsa_guid = dest_dsa
61     req8.source_dsa_invocation_id = misc.GUID(samdb.get_invocation_id())
62     req8.naming_context = drsuapi.DsReplicaObjectIdentifier()
63     req8.naming_context.dn = unicode(samdb.get_schema_basedn())
64     req8.highwatermark = drsuapi.DsReplicaHighWaterMark()
65     req8.highwatermark.tmp_highest_usn = 0
66     req8.highwatermark.reserved_usn = 0
67     req8.highwatermark.highest_usn = 0
68     req8.uptodateness_vector = None
69     req8.replica_flags = replica_flags
70     req8.max_object_count = 0
71     req8.max_ndr_size = 402116
72     req8.extended_op = 0
73     req8.fsmo_info = 0
74     req8.partial_attribute_set = None
75     req8.partial_attribute_set_ex = None
76     req8.mapping_ctr.num_mappings = 0
77     req8.mapping_ctr.mappings = None
78
79     (level, ctr) = drs.DsGetNCChanges(drs_handle, 8, req8)
80     pfm = ctr.mapping_ctr
81     # check for schemaInfo element
82     pfm_it = pfm.mappings[-1]
83     assert pfm_it.id_prefix == 0
84     assert pfm_it.oid.length == 21
85     assert pfm_it.oid.binary_oid[0] == 255
86     # remove schemaInfo element
87     pfm.num_mappings -= 1
88     return pfm
89
90 def _pfm_verify(drs_pfm, ldb_pfm):
91     errors = []
92     if drs_pfm.num_mappings != ldb_pfm.num_mappings:
93         errors.append("Different count of prefixes: drs = %d, ldb = %d"
94                       % (drs_pfm.num_mappings, ldb_pfm.num_mappings))
95     count = min(drs_pfm.num_mappings, ldb_pfm.num_mappings)
96     for i in range(0, count):
97         it_err = []
98         drs_it = drs_pfm.mappings[i]
99         ldb_it = ldb_pfm.mappings[i]
100         if drs_it.id_prefix != ldb_it.id_prefix:
101             it_err.append("id_prefix")
102         if drs_it.oid.length != ldb_it.oid.length:
103             it_err.append("oid.length")
104         if drs_it.oid.binary_oid != ldb_it.oid.binary_oid:
105             it_err.append("oid.binary_oid")
106         if len(it_err):
107             errors.append("[%2d] differences in (%s)" % (i, it_err))
108     return errors
109
110 ########### main code ###########
111 if __name__ == "__main__":
112     # command line parsing
113     parser = OptionParser("pfm_verify.py [options] server")
114     sambaopts = options.SambaOptions(parser)
115     parser.add_option_group(sambaopts)
116     credopts = options.CredentialsOptionsDouble(parser)
117     parser.add_option_group(credopts)
118
119     (opts, args) = parser.parse_args()
120
121     lp = sambaopts.get_loadparm()
122     creds = credopts.get_credentials(lp)
123
124     if len(args) != 1:
125         import os
126         if not "DC_SERVER" in os.environ.keys():
127              parser.error("You must supply a server")
128         args.append(os.environ["DC_SERVER"])
129
130     if creds.is_anonymous():
131         parser.error("You must supply credentials")
132         pass
133
134     server = args[0]
135
136     samdb = SamDB(url="ldap://%s" % server,
137                   session_info=system_session(),
138                   credentials=creds, lp=lp)
139
140     drs_pfm = _drs_fetch_pfm(server, samdb, creds, lp)
141     ldb_pfm = _samdb_fetch_pfm(samdb)
142     errors = _pfm_verify(drs_pfm, ldb_pfm)
143     if len(errors):
144         print "prefixMap verification errors:"
145         print "%s" % errors
146         sys.exit(1)