Skip to content

Commit

Permalink
scripting: Add script (backportable) to undo a GUID index
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Garming Sam <[email protected]>

Autobuild-User(master): Andrew Bartlett <[email protected]>
Autobuild-Date(master): Sat Sep 23 09:16:31 CEST 2017 on sn-devel-144
  • Loading branch information
abartlet committed Sep 23, 2017
1 parent 5d404ea commit 7ddf479
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions source4/scripting/bin/sambaundoguididx
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 7ddf479

Please sign in to comment.