1#!/usr/bin/env python3
2#
3# Helper for determining USN ranges created of modified by provision and
4# upgradeprovision.
5# Copyright (C) Matthieu Patou <mat@matws.net> 2009-2011
6#
7#
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.
12#
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.
17#
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/>.
20#
21import sys
22import optparse
23sys.path.insert(0, "bin/python")
24
25from samba.credentials import DONT_USE_KERBEROS
26from samba.auth import system_session
27from samba import Ldb
28import ldb
29
30import samba.getopt as options
31from samba import param
32from samba.upgradehelpers import get_paths, print_provision_ranges, findprovisionrange
33from samba.ndr import ndr_unpack
34from samba.dcerpc import misc
35
36parser = optparse.OptionParser("provision [options]")
37sambaopts = options.SambaOptions(parser)
38parser.add_option_group(sambaopts)
39parser.add_option_group(options.VersionOptions(parser))
40parser.add_option("--storedir", type="string", help="Directory where to store result files")
41credopts = options.CredentialsOptions(parser)
42parser.add_option_group(credopts)
43opts = parser.parse_args()[0]
44lp = sambaopts.get_loadparm()
45smbconf = lp.configfile
46
47creds = credopts.get_credentials(lp)
48creds.set_kerberos_state(DONT_USE_KERBEROS)
49session = system_session()
50paths = get_paths(param, smbconf=smbconf)
51basedn="DC=" + lp.get("realm").replace(".",",DC=")
52samdb = Ldb(paths.samdb, session_info=session, credentials=creds,lp=lp)
53
54res = samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["dsServiceName"])
55
56invocation = None
57if res and len(res) == 1 and res[0]["dsServiceName"] != None:
58    dn = ldb.Dn(samdb, str(res[0]["dsServiceName"]))
59    res = samdb.search(base=str(dn), scope=ldb.SCOPE_BASE, attrs=["invocationId"],
60                        controls=["search_options:1:2"])
61
62    if res and len(res) == 1 and res[0]["invocationId"]:
63        invocation = str(ndr_unpack(misc.GUID, res[0]["invocationId"][0]))
64    else:
65        print("Unable to find invocation ID")
66        sys.exit(1)
67else:
68    print("Unable to find attribute dsServiceName in rootDSE")
69    sys.exit(1)
70
71minobj = 5
72(hash_id, nb_obj) = findprovisionrange(samdb, basedn)
73print("Here is a list of changes that modified more than %d objects in 1 minute." % minobj)
74print("Usually changes made by provision and upgradeprovision are those who affect a couple"
75      " of hundred of objects or more")
76print("Total number of objects: %d\n" % nb_obj)
77
78print_provision_ranges(hash_id, minobj, opts.storedir, str(paths.samdb), invocation)
79