s4-samba-tool: Add samba-tool ntacl sysvolcheck command
[samba.git] / source4 / scripting / python / samba / netcmd / dbcheck.py
1 # Samba4 AD database checker
2 #
3 # Copyright (C) Andrew Tridgell 2011
4 #
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.
9 #
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.
14 #
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/>.
17 #
18
19 import ldb, sys
20 import samba.getopt as options
21 from samba.auth import system_session
22 from samba.samdb import SamDB
23 from samba.netcmd import (
24     Command,
25     CommandError,
26     Option
27     )
28 from samba.dbchecker import dbcheck
29
30
31 class cmd_dbcheck(Command):
32     """check local AD database for errors"""
33     synopsis = "%prog [<DN>] [options]"
34
35     takes_optiongroups = {
36         "sambaopts": options.SambaOptions,
37         "versionopts": options.VersionOptions,
38         "credopts": options.CredentialsOptionsDouble,
39     }
40
41     takes_args = ["DN?"]
42
43     takes_options = [
44         Option("--scope", dest="scope", default="SUB",
45             help="Pass search scope that builds DN list. Options: SUB, ONE, BASE"),
46         Option("--fix", dest="fix", default=False, action='store_true',
47                help='Fix any errors found'),
48         Option("--yes", dest="yes", default=False, action='store_true',
49                help="don't confirm changes, just do them all as a single transaction"),
50         Option("--cross-ncs", dest="cross_ncs", default=False, action='store_true',
51                help="cross naming context boundaries"),
52         Option("-v", "--verbose", dest="verbose", action="store_true", default=False,
53             help="Print more details of checking"),
54         Option("--quiet", dest="quiet", action="store_true", default=False,
55             help="don't print details of checking"),
56         Option("--attrs", dest="attrs", default=None, help="list of attributes to check (space separated)"),
57         Option("--reindex", dest="reindex", default=False, action="store_true", help="force database re-index"),
58         Option("-H", "--URL", help="LDB URL for database or target server (defaults to local SAM database)",
59                type=str, metavar="URL", dest="H"),
60         ]
61
62     def run(self, DN=None, H=None, verbose=False, fix=False, yes=False,
63             cross_ncs=False, quiet=False,
64             scope="SUB", credopts=None, sambaopts=None, versionopts=None,
65             attrs=None, reindex=False):
66
67         lp = sambaopts.get_loadparm()
68
69         over_ldap = H is not None and H.startswith('ldap')
70
71         if over_ldap:
72             creds = credopts.get_credentials(lp, fallback_machine=True)
73         else:
74             creds = None
75
76         samdb = SamDB(session_info=system_session(), url=H,
77                       credentials=creds, lp=lp)
78
79         if H is None or not over_ldap:
80             samdb_schema = samdb
81         else:
82             samdb_schema = SamDB(session_info=system_session(), url=None,
83                                  credentials=creds, lp=lp)
84
85         scope_map = { "SUB": ldb.SCOPE_SUBTREE, "BASE": ldb.SCOPE_BASE, "ONE":ldb.SCOPE_ONELEVEL }
86         scope = scope.upper()
87         if not scope in scope_map:
88             raise CommandError("Unknown scope %s" % scope)
89         search_scope = scope_map[scope]
90
91         controls = ['show_deleted:1']
92         if over_ldap:
93             controls.append('paged_results:1:1000')
94         if cross_ncs:
95             controls.append("search_options:1:2")
96
97         if not attrs:
98             attrs = ['*']
99         else:
100             attrs = attrs.split()
101
102         started_transaction = False
103         if yes and fix:
104             samdb.transaction_start()
105             started_transaction = True
106         try:
107             chk = dbcheck(samdb, samdb_schema=samdb_schema, verbose=verbose,
108                     fix=fix, yes=yes, quiet=quiet, in_transaction=started_transaction)
109
110             if reindex:
111                 self.outf.write("Re-indexing...\n")
112                 error_count = 0
113                 if chk.reindex_database():
114                     self.outf.write("completed re-index OK\n")
115             else:
116                 error_count = chk.check_database(DN=DN, scope=search_scope,
117                         controls=controls, attrs=attrs)
118         except:
119             if started_transaction:
120                 samdb.transaction_cancel()
121             raise
122
123         if started_transaction:
124             samdb.transaction_commit()
125
126         if error_count != 0:
127             sys.exit(1)