3 # Copyright William Brown <william@blackhats.net.au> 2018
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 (
30 class cmd_forest_show(Command):
31 """Display forest settings.
33 These settings control the behaviour of all domain controllers in this
34 forest. This displays those settings from the replicated configuration
38 synopsis = "%prog [options]"
40 takes_optiongroups = {
41 "sambaopts": options.SambaOptions,
42 "versionopts": options.VersionOptions,
43 "credopts": options.CredentialsOptions,
47 Option("-H", "--URL", help="LDB URL for database or target server",
48 type=str, metavar="URL", dest="H"),
51 def run(self, H=None, credopts=None, sambaopts=None, versionopts=None):
52 lp = sambaopts.get_loadparm()
53 creds = credopts.get_credentials(lp)
55 samdb = SamDB(url=H, session_info=system_session(),
56 credentials=creds, lp=lp)
58 domain_dn = samdb.domain_dn()
59 object_dn = "%s,%s" % (self.objectdn, domain_dn)
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)
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!
70 self.outf.write("Settings for %s\n" % object_dn)
71 for attr in self.attributes:
73 self.outf.write("%s: %s\n" % (attr, res_object[attr][0]))
75 self.outf.write("%s: <NO VALUE>\n" % attr)
77 class cmd_forest_set(Command):
78 """Modify forest settings.
80 This will alter the setting specified to value.
86 synopsis = "%prog value [options]"
88 takes_optiongroups = {
89 "sambaopts": options.SambaOptions,
90 "versionopts": options.VersionOptions,
91 "credopts": options.CredentialsOptions,
95 Option("-H", "--URL", help="LDB URL for database or target server",
96 type=str, metavar="URL", dest="H"),
99 takes_args = ["value"]
101 def run(self, value, H=None, credopts=None, sambaopts=None, versionopts=None):
102 lp = sambaopts.get_loadparm()
103 creds = credopts.get_credentials(lp)
105 samdb = SamDB(url=H, session_info=system_session(),
106 credentials=creds, lp=lp)
108 domain_dn = samdb.domain_dn()
109 object_dn = "%s,%s" % (self.objectdn, domain_dn)
111 # Create the modification
113 m.dn = ldb.Dn(samdb, object_dn)
114 m[self.attribute] = ldb.MessageElement(
115 value, ldb.FLAG_MOD_REPLACE, self.attribute)
118 self.outf.write("set %s: %s\n" % (self.attribute, value))
121 # Then you override it for each setting name:
123 class cmd_forest_show_directory_service(cmd_forest_show):
124 """Display Directory Service settings for the forest.
126 These settings control how the Directory Service behaves on all domain
127 controllers in the forest.
129 objectdn = "CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration"
130 attributes = ['dsheuristics']
132 class cmd_forest_set_directory_service_dsheuristics(cmd_forest_set):
133 """Set the value of dsheuristics on the Directory Service.
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
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.
143 Example: dsheuristics 0000002
145 This would allow anonymous LDAP searches to the domain (you may still need
146 to alter access controls to allow this).
148 objectdn = "CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration"
149 attribute = 'dsheuristics'
151 class cmd_forest_directory_service(SuperCommand):
152 """Forest configuration partition management."""
155 subcommands["show"] = cmd_forest_show_directory_service()
156 subcommands["dsheuristics"] = cmd_forest_set_directory_service_dsheuristics()
158 class cmd_forest(SuperCommand):
159 """Forest management."""
162 subcommands["directory_service"] = cmd_forest_directory_service()