1 # Samba4 AD database checker
3 # Copyright (C) Andrew Tridgell 2011
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 import samba.getopt as options
21 from samba.auth import system_session
22 from samba.samdb import SamDB
23 from samba.netcmd import (
28 from samba.dbchecker import dbcheck
31 class cmd_dbcheck(Command):
32 """Check local AD database for errors."""
33 synopsis = "%prog [<DN>] [options]"
35 takes_optiongroups = {
36 "sambaopts": options.SambaOptions,
37 "versionopts": options.VersionOptions,
38 "credopts": options.CredentialsOptionsDouble,
41 def process_yes(option, opt, value, parser):
47 if ((arg[:2] == "--" and len(arg) > 2) or
48 (arg[:1] == "-" and len(arg) > 1 and arg[1] != "-")):
49 setattr(parser.values, "yes", True)
51 setattr(parser.values, "yes_rules", arg.split())
54 setattr(parser.values, "yes", True)
59 Option("--scope", dest="scope", default="SUB",
60 help="Pass search scope that builds DN list. Options: SUB, ONE, BASE"),
61 Option("--fix", dest="fix", default=False, action='store_true',
62 help='Fix any errors found'),
63 Option("--yes", action='callback', callback=process_yes,
64 help="don't confirm changes, just do them all as a single transaction"),
65 Option("--cross-ncs", dest="cross_ncs", default=False, action='store_true',
66 help="cross naming context boundaries"),
67 Option("-v", "--verbose", dest="verbose", action="store_true", default=False,
68 help="Print more details of checking"),
69 Option("--quiet", dest="quiet", action="store_true", default=False,
70 help="don't print details of checking"),
71 Option("--attrs", dest="attrs", default=None, help="list of attributes to check (space separated)"),
72 Option("--reindex", dest="reindex", default=False, action="store_true", help="force database re-index"),
73 Option("--force-modules", dest="force_modules", default=False, action="store_true", help="force loading of Samba modules and ignore the @MODULES record (for very old databases)"),
74 Option("--reset-well-known-acls", dest="reset_well_known_acls", default=False, action="store_true", help="reset ACLs on objects with well known default ACL values to the default"),
75 Option("-H", "--URL", help="LDB URL for database or target server (defaults to local SAM database)",
76 type=str, metavar="URL", dest="H"),
79 def run(self, DN=None, H=None, verbose=False, fix=False, yes=False,
80 cross_ncs=False, quiet=False,
81 scope="SUB", credopts=None, sambaopts=None, versionopts=None,
82 attrs=None, reindex=False, force_modules=False,
83 reset_well_known_acls=False, yes_rules=[]):
85 lp = sambaopts.get_loadparm()
87 over_ldap = H is not None and H.startswith('ldap')
90 creds = credopts.get_credentials(lp, fallback_machine=True)
95 samdb = SamDB(session_info=system_session(), url=H,
96 credentials=creds, lp=lp, options=["modules=samba_dsdb"])
99 samdb = SamDB(session_info=system_session(), url=H,
100 credentials=creds, lp=lp)
102 raise CommandError("Failed to connect to DB at %s. If this is a really old sam.ldb (before alpha9), then try again with --force-modules" % H)
105 if H is None or not over_ldap:
108 samdb_schema = SamDB(session_info=system_session(), url=None,
109 credentials=creds, lp=lp)
111 scope_map = { "SUB": ldb.SCOPE_SUBTREE, "BASE": ldb.SCOPE_BASE, "ONE":ldb.SCOPE_ONELEVEL }
112 scope = scope.upper()
113 if not scope in scope_map:
114 raise CommandError("Unknown scope %s" % scope)
115 search_scope = scope_map[scope]
117 controls = ['show_deleted:1']
119 controls.append('paged_results:1:1000')
121 controls.append("search_options:1:2")
126 attrs = attrs.split()
128 started_transaction = False
130 samdb.transaction_start()
131 started_transaction = True
133 chk = dbcheck(samdb, samdb_schema=samdb_schema, verbose=verbose,
134 fix=fix, yes=yes, quiet=quiet, in_transaction=started_transaction,
135 reset_well_known_acls=reset_well_known_acls)
137 for option in yes_rules:
138 if hasattr(chk, option):
139 setattr(chk, option, 'ALL')
141 raise CommandError("Invalid fix rule %s" % option)
144 self.outf.write("Re-indexing...\n")
146 if chk.reindex_database():
147 self.outf.write("completed re-index OK\n")
150 self.outf.write("Resetting @MODULES...\n")
152 if chk.reset_modules():
153 self.outf.write("completed @MODULES reset OK\n")
156 error_count = chk.check_database(DN=DN, scope=search_scope,
157 controls=controls, attrs=attrs)
159 if started_transaction:
160 samdb.transaction_cancel()
163 if started_transaction:
164 samdb.transaction_commit()