sambaundoguididx: use the right escaped oder unescaped sam ldb files
[herb/samba-autobuild/.git] / source4 / scripting / bin / sambaundoguididx
1 #!/usr/bin/env python3
2 import optparse
3 import sys
4
5 # Find right directory when running from source tree
6 sys.path.insert(0, "bin/python")
7
8
9 import samba
10 import ldb
11 import urllib
12 import os
13 from samba import getopt as options
14 from samba.samdb import SamDB
15 from samba.dbchecker import dbcheck
16 from samba.credentials import Credentials
17 parser = optparse.OptionParser("sambaundoguididx")
18 sambaopts = options.SambaOptions(parser)
19 parser.add_option_group(options.VersionOptions(parser))
20 parser.add_option("-H", "--URL", help="LDB URL for database",
21                   type=str, metavar="URL", dest="H")
22 opts, args = parser.parse_args()
23
24 if len(args) != 0:
25     parser.print_usage()
26     sys.exit(1)
27
28 lp_ctx = sambaopts.get_loadparm()
29 lp_ctx.set("dsdb:guid index", "false")
30
31 if opts.H is None:
32     url = lp_ctx.samdb_url()
33 else:
34     url = opts.H
35
36 samdb = ldb.Ldb(url=url, options=["modules:"])
37
38 partitions = samdb.search(base="@PARTITION",
39                           scope=ldb.SCOPE_BASE,
40                           attrs=["partition"])
41
42 modmsg = ldb.Message()
43 modmsg.dn = ldb.Dn(samdb, '@INDEXLIST')
44 modmsg.add(ldb.MessageElement(
45     elements=[],
46     flags=ldb.FLAG_MOD_REPLACE,
47     name='@IDXGUID'))
48 modmsg.add(ldb.MessageElement(
49     elements=[],
50     flags=ldb.FLAG_MOD_REPLACE,
51     name='@IDX_DN_GUID'))
52
53 samdb.transaction_start()
54 samdb.modify(modmsg)
55
56 privatedir = os.path.dirname(url)
57
58 dbs = []
59 for part in partitions[0]['partition']:
60     tdbname = part.split(":")[1]
61     tdbpath = os.path.join(privatedir, tdbname)
62
63     db = ldb.Ldb(url=tdbpath, options=["modules:"])
64     db.transaction_start()
65     db.modify(modmsg)
66     dbs.append(db)
67
68 for db in dbs:
69     db.transaction_commit()
70
71 samdb.transaction_commit()
72
73 print("Re-opening with the full DB stack")
74 samdb = SamDB(url=url,
75                           lp=lp_ctx)
76 print "Re-triggering another re-index"
77 chk = dbcheck(samdb)
78
79 chk.reindex_database()
80
81 print "Your database has been downgraded to DN-based index values."
82
83 print "NOTE: Any use of a Samba 4.8 tool including ldbsearch will auto-upgrade back to GUID index mode"