From 7ddf47951bd472841c5f365e5fff7d28b1ce4972 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 11 Sep 2017 21:39:44 +1200 Subject: [PATCH] scripting: Add script (backportable) to undo a GUID index This script allows the DB to be read, and re-indexed, by an earlier Samba version, most likely 4.7 with some backported patches. Signed-off-by: Andrew Bartlett Reviewed-by: Garming Sam Autobuild-User(master): Andrew Bartlett Autobuild-Date(master): Sat Sep 23 09:16:31 CEST 2017 on sn-devel-144 --- source4/scripting/bin/sambaundoguididx | 84 ++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100755 source4/scripting/bin/sambaundoguididx diff --git a/source4/scripting/bin/sambaundoguididx b/source4/scripting/bin/sambaundoguididx new file mode 100755 index 00000000000..24a95e20d7f --- /dev/null +++ b/source4/scripting/bin/sambaundoguididx @@ -0,0 +1,84 @@ +#!/usr/bin/env python +import optparse +import sys + +# Find right directory when running from source tree +sys.path.insert(0, "bin/python") + + +import samba +import ldb +import urllib +import os +from samba import getopt as options +from samba.samdb import SamDB +from samba.dbchecker import dbcheck +from samba.credentials import Credentials +parser = optparse.OptionParser("sambaundoguididx") +sambaopts = options.SambaOptions(parser) +parser.add_option_group(options.VersionOptions(parser)) +parser.add_option("-H", "--URL", help="LDB URL for database", + type=str, metavar="URL", dest="H") +opts, args = parser.parse_args() + +if len(args) != 0: + parser.print_usage() + sys.exit(1) + +lp_ctx = sambaopts.get_loadparm() +lp_ctx.set("dsdb:guid index", "false") + +if opts.H is None: + url = lp_ctx.samdb_url() +else: + url = opts.H + +samdb = ldb.Ldb(url=url, options=["modules:"]) + +partitions = samdb.search(base="@PARTITION", + scope=ldb.SCOPE_BASE, + attrs=["partition"]) + +modmsg = ldb.Message() +modmsg.dn = ldb.Dn(samdb, '@INDEXLIST') +modmsg.add(ldb.MessageElement( + elements=[], + flags=ldb.FLAG_MOD_REPLACE, + name='@IDXGUID')) +modmsg.add(ldb.MessageElement( + elements=[], + flags=ldb.FLAG_MOD_REPLACE, + name='@IDX_DN_GUID')) + +samdb.transaction_start() +samdb.modify(modmsg) + +privatedir = os.path.dirname(url) + +dbs = [] +for part in partitions[0]['partition']: + file_quoted = part.split(":")[1] + tdbname = urllib.unquote(file_quoted) + tdbpath = os.path.join(privatedir, tdbname) + + db = ldb.Ldb(url=tdbpath, options=["modules:"]) + db.transaction_start() + db.modify(modmsg) + dbs.append(db) + +for db in dbs: + db.transaction_commit() + +samdb.transaction_commit() + +print "Re-opening with the full DB stack" +samdb = SamDB(url=url, + lp=lp_ctx) +print "Re-triggering another re-index" +chk = dbcheck(samdb) + +chk.reindex_database() + +print "Your database has been downgraded to DN-based index values." + +print "NOTE: Any use of a Samba 4.8 tool including ldbsearch will auto-upgrade back to GUID index mode"