PEP8: fix E123: closing bracket does not match indentation of opening bracket's line
[garming/samba-autobuild/.git] / python / samba / netcmd / forest.py
1 # domain management
2 #
3 # Copyright William Brown <william@blackhats.net.au> 2018
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
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     SuperCommand,
27     Option
28 )
29
30 class cmd_forest_show(Command):
31     """Display forest settings.
32
33     These settings control the behaviour of all domain controllers in this
34     forest. This displays those settings from the replicated configuration
35     partition.
36     """
37
38     synopsis = "%prog [options]"
39
40     takes_optiongroups = {
41         "sambaopts": options.SambaOptions,
42         "versionopts": options.VersionOptions,
43         "credopts": options.CredentialsOptions,
44     }
45
46     takes_options = [
47         Option("-H", "--URL", help="LDB URL for database or target server",
48                 type=str, metavar="URL", dest="H"),
49     ]
50
51     def run(self, H=None, credopts=None, sambaopts=None, versionopts=None):
52         lp = sambaopts.get_loadparm()
53         creds = credopts.get_credentials(lp)
54
55         samdb = SamDB(url=H, session_info=system_session(),
56             credentials=creds, lp=lp)
57
58         domain_dn = samdb.domain_dn()
59         object_dn = "%s,%s" % (self.objectdn, domain_dn)
60
61         # Show all the settings we know how to set in the forest object!
62         res = samdb.search(base=object_dn, scope=ldb.SCOPE_BASE,
63                            attrs=self.attributes)
64
65         # Now we just display these attributes. The value is that
66         # we make them a bit prettier and human accessible.
67         # There should only be one response!
68         res_object = res[0]
69
70         self.outf.write("Settings for %s\n" % object_dn)
71         for attr in self.attributes:
72             try:
73                 self.outf.write("%s: %s\n" % (attr, res_object[attr][0]))
74             except KeyError:
75                 self.outf.write("%s: <NO VALUE>\n" % attr)
76
77 class cmd_forest_set(Command):
78     """Modify forest settings.
79
80     This will alter the setting specified to value.
81     """
82
83     attribute = None
84     objectdn = None
85
86     synopsis = "%prog value [options]"
87
88     takes_optiongroups = {
89         "sambaopts": options.SambaOptions,
90         "versionopts": options.VersionOptions,
91         "credopts": options.CredentialsOptions,
92     }
93
94     takes_options = [
95         Option("-H", "--URL", help="LDB URL for database or target server",
96                 type=str, metavar="URL", dest="H"),
97     ]
98
99     takes_args = ["value"]
100
101     def run(self, value, H=None, credopts=None, sambaopts=None, versionopts=None):
102         lp = sambaopts.get_loadparm()
103         creds = credopts.get_credentials(lp)
104
105         samdb = SamDB(url=H, session_info=system_session(),
106             credentials=creds, lp=lp)
107
108         domain_dn = samdb.domain_dn()
109         object_dn = "%s,%s" % (self.objectdn, domain_dn)
110
111         # Create the modification
112         m = ldb.Message()
113         m.dn = ldb.Dn(samdb, object_dn)
114         m[self.attribute] = ldb.MessageElement(
115             value, ldb.FLAG_MOD_REPLACE, self.attribute)
116
117         samdb.modify(m)
118         self.outf.write("set %s: %s\n" % (self.attribute, value))
119
120
121 # Then you override it for each setting name:
122
123 class cmd_forest_show_directory_service(cmd_forest_show):
124     """Display Directory Service settings for the forest.
125
126     These settings control how the Directory Service behaves on all domain
127     controllers in the forest.
128     """
129     objectdn = "CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration"
130     attributes = ['dsheuristics']
131
132 class cmd_forest_set_directory_service_dsheuristics(cmd_forest_set):
133     """Set the value of dsheuristics on the Directory Service.
134
135     This value alters the behaviour of the Directory Service on all domain
136     controllers in the forest. Documentation related to this parameter can be
137     found here: https://msdn.microsoft.com/en-us/library/cc223560.aspx
138
139     In summary each "character" of the number-string, controls a setting.
140     A common setting is to set the value "2" in the 7th character. This controls
141     anonymous search behaviour.
142
143     Example: dsheuristics 0000002
144
145     This would allow anonymous LDAP searches to the domain (you may still need
146     to alter access controls to allow this).
147     """
148     objectdn = "CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration"
149     attribute = 'dsheuristics'
150
151 class cmd_forest_directory_service(SuperCommand):
152     """Forest configuration partition management."""
153
154     subcommands = {}
155     subcommands["show"] = cmd_forest_show_directory_service()
156     subcommands["dsheuristics"] = cmd_forest_set_directory_service_dsheuristics()
157
158 class cmd_forest(SuperCommand):
159     """Forest management."""
160
161     subcommands = {}
162     subcommands["directory_service"] = cmd_forest_directory_service()