3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2019
6 # Downgrade a database from 4.11 format to 4.7 format. 4.7 Format will
7 # run on any version of Samba AD, and Samba will repack/reconfigure the
8 # database if necessary.
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 from __future__ import print_function
26 # Find right directory when running from source tree
27 sys.path.insert(0, "bin/python")
34 from samba import getopt as options
35 from samba.samdb import SamDB
36 from samba.dbchecker import dbcheck
37 from samba.credentials import Credentials
38 parser = optparse.OptionParser("sambaundoguididx")
39 sambaopts = options.SambaOptions(parser)
40 parser.add_option_group(options.VersionOptions(parser))
41 parser.add_option("-H", "--URL", help="LDB URL for database",
42 type=str, metavar="URL", dest="H")
43 opts, args = parser.parse_args()
49 lp_ctx = sambaopts.get_loadparm()
52 url = lp_ctx.private_path("sam.ldb")
56 samdb = ldb.Ldb(url=url,
57 flags=ldb.FLG_DONT_CREATE_DB,
60 partitions = samdb.search(base="@PARTITION",
62 attrs=["backendStore", "partition"])
64 backend = str(partitions[0].get('backendStore', 'tdb'))
68 options = ["pack_format_override=%d" % ldb.PACKING_FORMAT]
69 # We can't remove GUID indexes from LMDB in case there are very
70 # long DNs, so we just move down the pack format, which also removes
71 # references to ORDERED_INTEGER in @ATTRIBUTES.
73 # Reopen the DB with pack_format_override set
74 samdb = SamDB(url=url,
75 flags=ldb.FLG_DONT_CREATE_DB,
78 samdb.transaction_start()
79 samdb.transaction_commit()
80 print("Your database has been downgraded to LDB pack format version %0x (v1)." % ldb.PACKING_FORMAT)
82 print("NOTE: Any use of a Samba 4.11 tool that modifies the DB will "
83 "auto-upgrade back to pack format version %0x (v2)" %
84 ldb.PACKING_FORMAT_V2)
87 # This is needed to force the @ATTRIBUTES and @INDEXLIST to be correct
88 lp_ctx.set("dsdb:guid index", "false")
90 modmsg = ldb.Message()
91 modmsg.dn = ldb.Dn(samdb, '@INDEXLIST')
92 modmsg.add(ldb.MessageElement(
94 flags=ldb.FLAG_MOD_REPLACE,
96 modmsg.add(ldb.MessageElement(
98 flags=ldb.FLAG_MOD_REPLACE,
101 samdb.transaction_start()
104 privatedir = os.path.dirname(url)
107 for part in partitions[0]['partition']:
108 dbname = str(part).split(":")[1]
109 dbpath = os.path.join(privatedir, dbname)
110 if os.path.isfile(dbpath):
111 dbpath = "ldb://" + dbpath
112 db = ldb.Ldb(url=dbpath,
113 options=["modules:"],
114 flags=ldb.FLG_DONT_CREATE_DB)
115 db.transaction_start()
120 db.transaction_commit()
122 samdb.transaction_commit()
124 print("Re-opening with the full DB stack")
125 samdb = SamDB(url=url,
126 flags=ldb.FLG_DONT_CREATE_DB,
128 print("Re-triggering another re-index")
131 chk.reindex_database()
133 print("Your database has been downgraded to DN-based index values.")
135 print("NOTE: Any use of a Samba 4.8 or later tool including ldbsearch will "
136 "auto-upgrade back to GUID index mode")