3 # Copyright Matthias Dieter Wallnoefer 2009
4 # Copyright Andrew Kroeger 2009
5 # Copyright Jelmer Vernooij 2007-2012
6 # Copyright Giampaolo Lauria 2011
7 # Copyright Matthieu Patou <mat@matws.net> 2011
8 # Copyright Andrew Bartlett 2008-2015
9 # Copyright Stefan Metzmacher 2012
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 import samba.getopt as options
36 from samba import ntstatus
37 from samba import NTSTATUSError
38 from getpass import getpass
39 from samba.net import Net, LIBNET_JOIN_AUTOMATIC
41 from samba.join import join_RODC, join_DC, join_subdomain
42 from samba.auth import system_session
43 from samba.samdb import SamDB
44 from samba.ndr import ndr_unpack, ndr_pack, ndr_print
45 from samba.dcerpc import drsuapi
46 from samba.dcerpc import drsblobs
47 from samba.dcerpc import lsa
48 from samba.dcerpc import netlogon
49 from samba.dcerpc import security
50 from samba.dcerpc import nbt
51 from samba.dcerpc import misc
52 from samba.dcerpc.samr import DOMAIN_PASSWORD_COMPLEX, DOMAIN_PASSWORD_STORE_CLEARTEXT
53 from samba.netcmd import (
59 from samba.netcmd.common import netcmd_get_domain_infos_via_cldap
60 from samba.samba3 import Samba3
61 from samba.samba3 import param as s3param
62 from samba.upgrade import upgrade_from_samba3
63 from samba.drs_utils import (
64 sendDsReplicaSync, drsuapi_connect, drsException,
66 from samba import remove_dc, arcfour_encrypt, string_to_byte_array
68 from samba.dsdb import (
69 DS_DOMAIN_FUNCTION_2000,
70 DS_DOMAIN_FUNCTION_2003,
71 DS_DOMAIN_FUNCTION_2003_MIXED,
72 DS_DOMAIN_FUNCTION_2008,
73 DS_DOMAIN_FUNCTION_2008_R2,
74 DS_DOMAIN_FUNCTION_2012,
75 DS_DOMAIN_FUNCTION_2012_R2,
76 DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL,
77 DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL,
78 UF_WORKSTATION_TRUST_ACCOUNT,
79 UF_SERVER_TRUST_ACCOUNT,
80 UF_TRUSTED_FOR_DELEGATION,
81 UF_PARTIAL_SECRETS_ACCOUNT
84 from samba.provision import (
89 from samba.provision.common import (
95 def get_testparm_var(testparm, smbconf, varname):
96 errfile = open(os.devnull, 'w')
97 p = subprocess.Popen([testparm, '-s', '-l',
98 '--parameter-name=%s' % varname, smbconf],
99 stdout=subprocess.PIPE, stderr=errfile)
100 (out,err) = p.communicate()
102 lines = out.split('\n')
104 return lines[0].strip()
108 import samba.dckeytab
110 cmd_domain_export_keytab = None
112 class cmd_domain_export_keytab(Command):
113 """Dump Kerberos keys of the domain into a keytab."""
115 synopsis = "%prog <keytab> [options]"
117 takes_optiongroups = {
118 "sambaopts": options.SambaOptions,
119 "credopts": options.CredentialsOptions,
120 "versionopts": options.VersionOptions,
124 Option("--principal", help="extract only this principal", type=str),
127 takes_args = ["keytab"]
129 def run(self, keytab, credopts=None, sambaopts=None, versionopts=None, principal=None):
130 lp = sambaopts.get_loadparm()
132 net.export_keytab(keytab=keytab, principal=principal)
135 class cmd_domain_info(Command):
136 """Print basic info about a domain and the DC passed as parameter."""
138 synopsis = "%prog <ip_address> [options]"
143 takes_optiongroups = {
144 "sambaopts": options.SambaOptions,
145 "credopts": options.CredentialsOptions,
146 "versionopts": options.VersionOptions,
149 takes_args = ["address"]
151 def run(self, address, credopts=None, sambaopts=None, versionopts=None):
152 lp = sambaopts.get_loadparm()
154 res = netcmd_get_domain_infos_via_cldap(lp, None, address)
156 raise CommandError("Invalid IP address '" + address + "'!")
157 self.outf.write("Forest : %s\n" % res.forest)
158 self.outf.write("Domain : %s\n" % res.dns_domain)
159 self.outf.write("Netbios domain : %s\n" % res.domain_name)
160 self.outf.write("DC name : %s\n" % res.pdc_dns_name)
161 self.outf.write("DC netbios name : %s\n" % res.pdc_name)
162 self.outf.write("Server site : %s\n" % res.server_site)
163 self.outf.write("Client site : %s\n" % res.client_site)
166 class cmd_domain_provision(Command):
167 """Provision a domain."""
169 synopsis = "%prog [options]"
171 takes_optiongroups = {
172 "sambaopts": options.SambaOptions,
173 "versionopts": options.VersionOptions,
177 Option("--interactive", help="Ask for names", action="store_true"),
178 Option("--domain", type="string", metavar="DOMAIN",
179 help="NetBIOS domain name to use"),
180 Option("--domain-guid", type="string", metavar="GUID",
181 help="set domainguid (otherwise random)"),
182 Option("--domain-sid", type="string", metavar="SID",
183 help="set domainsid (otherwise random)"),
184 Option("--ntds-guid", type="string", metavar="GUID",
185 help="set NTDS object GUID (otherwise random)"),
186 Option("--invocationid", type="string", metavar="GUID",
187 help="set invocationid (otherwise random)"),
188 Option("--host-name", type="string", metavar="HOSTNAME",
189 help="set hostname"),
190 Option("--host-ip", type="string", metavar="IPADDRESS",
191 help="set IPv4 ipaddress"),
192 Option("--host-ip6", type="string", metavar="IP6ADDRESS",
193 help="set IPv6 ipaddress"),
194 Option("--site", type="string", metavar="SITENAME",
195 help="set site name"),
196 Option("--adminpass", type="string", metavar="PASSWORD",
197 help="choose admin password (otherwise random)"),
198 Option("--krbtgtpass", type="string", metavar="PASSWORD",
199 help="choose krbtgt password (otherwise random)"),
200 Option("--machinepass", type="string", metavar="PASSWORD",
201 help="choose machine password (otherwise random)"),
202 Option("--dns-backend", type="choice", metavar="NAMESERVER-BACKEND",
203 choices=["SAMBA_INTERNAL", "BIND9_FLATFILE", "BIND9_DLZ", "NONE"],
204 help="The DNS server backend. SAMBA_INTERNAL is the builtin name server (default), "
205 "BIND9_FLATFILE uses bind9 text database to store zone information, "
206 "BIND9_DLZ uses samba4 AD to store zone information, "
207 "NONE skips the DNS setup entirely (not recommended)",
208 default="SAMBA_INTERNAL"),
209 Option("--dnspass", type="string", metavar="PASSWORD",
210 help="choose dns password (otherwise random)"),
211 Option("--ldapadminpass", type="string", metavar="PASSWORD",
212 help="choose password to set between Samba and its LDAP backend (otherwise random)"),
213 Option("--root", type="string", metavar="USERNAME",
214 help="choose 'root' unix username"),
215 Option("--nobody", type="string", metavar="USERNAME",
216 help="choose 'nobody' user"),
217 Option("--users", type="string", metavar="GROUPNAME",
218 help="choose 'users' group"),
219 Option("--quiet", help="Be quiet", action="store_true"),
220 Option("--blank", action="store_true",
221 help="do not add users or groups, just the structure"),
222 Option("--ldap-backend-type", type="choice", metavar="LDAP-BACKEND-TYPE",
223 help="Test initialisation support for unsupported LDAP backend type (fedora-ds or openldap) DO NOT USE",
224 choices=["fedora-ds", "openldap"]),
225 Option("--server-role", type="choice", metavar="ROLE",
226 choices=["domain controller", "dc", "member server", "member", "standalone"],
227 help="The server role (domain controller | dc | member server | member | standalone). Default is dc.",
228 default="domain controller"),
229 Option("--function-level", type="choice", metavar="FOR-FUN-LEVEL",
230 choices=["2000", "2003", "2008", "2008_R2"],
231 help="The domain and forest function level (2000 | 2003 | 2008 | 2008_R2 - always native). Default is (Windows) 2008_R2 Native.",
233 Option("--next-rid", type="int", metavar="NEXTRID", default=1000,
234 help="The initial nextRid value (only needed for upgrades). Default is 1000."),
235 Option("--partitions-only",
236 help="Configure Samba's partitions, but do not modify them (ie, join a BDC)", action="store_true"),
237 Option("--targetdir", type="string", metavar="DIR",
238 help="Set target directory"),
239 Option("--ol-mmr-urls", type="string", metavar="LDAPSERVER",
240 help="List of LDAP-URLS [ ldap://<FQHN>:<PORT>/ (where <PORT> has to be different than 389!) ] separated with comma (\",\") for use with OpenLDAP-MMR (Multi-Master-Replication), e.g.: \"ldap://s4dc1:9000,ldap://s4dc2:9000\""),
241 Option("--use-rfc2307", action="store_true", help="Use AD to store posix attributes (default = no)"),
245 Option("--ldap-dryrun-mode", help="Configure LDAP backend, but do not run any binaries and exit early. Used only for the test environment. DO NOT USE",
246 action="store_true"),
247 Option("--slapd-path", type="string", metavar="SLAPD-PATH",
248 help="Path to slapd for LDAP backend [e.g.:'/usr/local/libexec/slapd']. Required for Setup with LDAP-Backend. OpenLDAP Version >= 2.4.17 should be used."),
249 Option("--ldap-backend-extra-port", type="int", metavar="LDAP-BACKEND-EXTRA-PORT", help="Additional TCP port for LDAP backend server (to use for replication)"),
250 Option("--ldap-backend-forced-uri", type="string", metavar="LDAP-BACKEND-FORCED-URI",
251 help="Force the LDAP backend connection to be to a particular URI. Use this ONLY for 'existing' backends, or when debugging the interaction with the LDAP backend and you need to intercept the LDA"),
252 Option("--ldap-backend-nosync", help="Configure LDAP backend not to call fsync() (for performance in test environments)", action="store_true"),
256 Option("--use-ntvfs", action="store_true", help="Use NTVFS for the fileserver (default = no)"),
257 Option("--use-xattrs", type="choice", choices=["yes","no","auto"],
258 metavar="[yes|no|auto]",
259 help="Define if we should use the native fs capabilities or a tdb file for "
260 "storing attributes likes ntacl when --use-ntvfs is set. "
261 "auto tries to make an inteligent guess based on the user rights and system capabilities",
265 if os.getenv('TEST_LDAP', "no") == "yes":
266 takes_options.extend(openldap_options)
268 if samba.is_ntvfs_fileserver_built():
269 takes_options.extend(ntvfs_options)
273 def run(self, sambaopts=None, versionopts=None,
296 ldap_backend_type=None,
300 partitions_only=None,
307 ldap_backend_nosync=None,
308 ldap_backend_extra_port=None,
309 ldap_backend_forced_uri=None,
310 ldap_dryrun_mode=None):
312 self.logger = self.get_logger("provision")
314 self.logger.setLevel(logging.WARNING)
316 self.logger.setLevel(logging.INFO)
318 lp = sambaopts.get_loadparm()
319 smbconf = lp.configfile
321 if dns_forwarder is not None:
322 suggested_forwarder = dns_forwarder
324 suggested_forwarder = self._get_nameserver_ip()
325 if suggested_forwarder is None:
326 suggested_forwarder = "none"
328 if len(self.raw_argv) == 1:
332 from getpass import getpass
335 def ask(prompt, default=None):
336 if default is not None:
337 print "%s [%s]: " % (prompt, default),
339 print "%s: " % (prompt,),
340 return sys.stdin.readline().rstrip("\n") or default
343 default = socket.getfqdn().split(".", 1)[1].upper()
346 realm = ask("Realm", default)
347 if realm in (None, ""):
348 raise CommandError("No realm set!")
351 default = realm.split(".")[0]
354 domain = ask("Domain", default)
356 raise CommandError("No domain set!")
358 server_role = ask("Server Role (dc, member, standalone)", "dc")
360 dns_backend = ask("DNS backend (SAMBA_INTERNAL, BIND9_FLATFILE, BIND9_DLZ, NONE)", "SAMBA_INTERNAL")
361 if dns_backend in (None, ''):
362 raise CommandError("No DNS backend set!")
364 if dns_backend == "SAMBA_INTERNAL":
365 dns_forwarder = ask("DNS forwarder IP address (write 'none' to disable forwarding)", suggested_forwarder)
366 if dns_forwarder.lower() in (None, 'none'):
367 suggested_forwarder = None
371 adminpassplain = getpass("Administrator password: ")
372 if not adminpassplain:
373 self.errf.write("Invalid administrator password.\n")
375 adminpassverify = getpass("Retype password: ")
376 if not adminpassplain == adminpassverify:
377 self.errf.write("Sorry, passwords do not match.\n")
379 adminpass = adminpassplain
383 realm = sambaopts._lp.get('realm')
385 raise CommandError("No realm set!")
387 raise CommandError("No domain set!")
390 self.logger.info("Administrator password will be set randomly!")
392 if function_level == "2000":
393 dom_for_fun_level = DS_DOMAIN_FUNCTION_2000
394 elif function_level == "2003":
395 dom_for_fun_level = DS_DOMAIN_FUNCTION_2003
396 elif function_level == "2008":
397 dom_for_fun_level = DS_DOMAIN_FUNCTION_2008
398 elif function_level == "2008_R2":
399 dom_for_fun_level = DS_DOMAIN_FUNCTION_2008_R2
401 if dns_backend == "SAMBA_INTERNAL" and dns_forwarder is None:
402 dns_forwarder = suggested_forwarder
404 samdb_fill = FILL_FULL
406 samdb_fill = FILL_NT4SYNC
407 elif partitions_only:
408 samdb_fill = FILL_DRS
410 if targetdir is not None:
411 if not os.path.isdir(targetdir):
416 if use_xattrs == "yes":
418 elif use_xattrs == "auto" and use_ntvfs == False:
420 elif use_ntvfs == False:
421 raise CommandError("--use-xattrs=no requires --use-ntvfs (not supported for production use). "
422 "Please re-run with --use-xattrs omitted.")
423 elif use_xattrs == "auto" and not lp.get("posix:eadb"):
425 file = tempfile.NamedTemporaryFile(dir=os.path.abspath(targetdir))
427 file = tempfile.NamedTemporaryFile(dir=os.path.abspath(os.path.dirname(lp.get("private dir"))))
430 samba.ntacls.setntacl(lp, file.name,
431 "O:S-1-5-32G:S-1-5-32", "S-1-5-32", "native")
434 self.logger.info("You are not root or your system does not support xattr, using tdb backend for attributes. ")
439 self.logger.info("not using extended attributes to store ACLs and other metadata. If you intend to use this provision in production, rerun the script as root on a system supporting xattrs.")
440 if ldap_backend_type == "existing":
441 if ldap_backend_forced_uri is not None:
442 self.logger.warn("You have specified to use an existing LDAP server as the backend, please make sure an LDAP server is running at %s" % ldap_backend_forced_uri)
444 self.logger.info("You have specified to use an existing LDAP server as the backend, please make sure an LDAP server is running at the default location")
446 if ldap_backend_forced_uri is not None:
447 self.logger.warn("You have specified to use an fixed URI %s for connecting to your LDAP server backend. This is NOT RECOMMENDED, as our default communiation over ldapi:// is more secure and much less")
449 if domain_sid is not None:
450 domain_sid = security.dom_sid(domain_sid)
452 session = system_session()
454 result = provision(self.logger,
455 session, smbconf=smbconf, targetdir=targetdir,
456 samdb_fill=samdb_fill, realm=realm, domain=domain,
457 domainguid=domain_guid, domainsid=domain_sid,
459 hostip=host_ip, hostip6=host_ip6,
460 sitename=site, ntdsguid=ntds_guid,
461 invocationid=invocationid, adminpass=adminpass,
462 krbtgtpass=krbtgtpass, machinepass=machinepass,
463 dns_backend=dns_backend, dns_forwarder=dns_forwarder,
464 dnspass=dnspass, root=root, nobody=nobody,
466 serverrole=server_role, dom_for_fun_level=dom_for_fun_level,
467 backend_type=ldap_backend_type,
468 ldapadminpass=ldapadminpass, ol_mmr_urls=ol_mmr_urls, slapd_path=slapd_path,
469 useeadb=eadb, next_rid=next_rid, lp=lp, use_ntvfs=use_ntvfs,
470 use_rfc2307=use_rfc2307, skip_sysvolacl=False,
471 ldap_backend_extra_port=ldap_backend_extra_port,
472 ldap_backend_forced_uri=ldap_backend_forced_uri,
473 nosync=ldap_backend_nosync, ldap_dryrun_mode=ldap_dryrun_mode)
475 except ProvisioningError, e:
476 raise CommandError("Provision failed", e)
478 result.report_logger(self.logger)
480 def _get_nameserver_ip(self):
481 """Grab the nameserver IP address from /etc/resolv.conf."""
483 RESOLV_CONF="/etc/resolv.conf"
485 if not path.isfile(RESOLV_CONF):
486 self.logger.warning("Failed to locate %s" % RESOLV_CONF)
491 handle = open(RESOLV_CONF, 'r')
493 if not line.startswith('nameserver'):
495 # we want the last non-space continuous string of the line
496 return line.strip().split()[-1]
498 if handle is not None:
501 self.logger.warning("No nameserver found in %s" % RESOLV_CONF)
504 class cmd_domain_dcpromo(Command):
505 """Promote an existing domain member or NT4 PDC to an AD DC."""
507 synopsis = "%prog <dnsdomain> [DC|RODC] [options]"
509 takes_optiongroups = {
510 "sambaopts": options.SambaOptions,
511 "versionopts": options.VersionOptions,
512 "credopts": options.CredentialsOptions,
516 Option("--server", help="DC to join", type=str),
517 Option("--site", help="site to join", type=str),
518 Option("--targetdir", help="where to store provision", type=str),
519 Option("--domain-critical-only",
520 help="only replicate critical domain objects",
521 action="store_true"),
522 Option("--machinepass", type=str, metavar="PASSWORD",
523 help="choose machine password (otherwise random)"),
524 Option("--dns-backend", type="choice", metavar="NAMESERVER-BACKEND",
525 choices=["SAMBA_INTERNAL", "BIND9_DLZ", "NONE"],
526 help="The DNS server backend. SAMBA_INTERNAL is the builtin name server (default), "
527 "BIND9_DLZ uses samba4 AD to store zone information, "
528 "NONE skips the DNS setup entirely (this DC will not be a DNS server)",
529 default="SAMBA_INTERNAL"),
530 Option("--quiet", help="Be quiet", action="store_true"),
531 Option("--verbose", help="Be verbose", action="store_true")
535 Option("--use-ntvfs", action="store_true", help="Use NTVFS for the fileserver (default = no)"),
538 if samba.is_ntvfs_fileserver_built():
539 takes_options.extend(ntvfs_options)
542 takes_args = ["domain", "role?"]
544 def run(self, domain, role=None, sambaopts=None, credopts=None,
545 versionopts=None, server=None, site=None, targetdir=None,
546 domain_critical_only=False, parent_domain=None, machinepass=None,
547 use_ntvfs=False, dns_backend=None,
548 quiet=False, verbose=False):
549 lp = sambaopts.get_loadparm()
550 creds = credopts.get_credentials(lp)
551 net = Net(creds, lp, server=credopts.ipaddress)
554 site = "Default-First-Site-Name"
556 logger = self.get_logger()
558 logger.setLevel(logging.DEBUG)
560 logger.setLevel(logging.WARNING)
562 logger.setLevel(logging.INFO)
564 netbios_name = lp.get("netbios name")
570 join_DC(logger=logger, server=server, creds=creds, lp=lp, domain=domain,
571 site=site, netbios_name=netbios_name, targetdir=targetdir,
572 domain_critical_only=domain_critical_only,
573 machinepass=machinepass, use_ntvfs=use_ntvfs,
574 dns_backend=dns_backend,
575 promote_existing=True)
577 join_RODC(logger=logger, server=server, creds=creds, lp=lp, domain=domain,
578 site=site, netbios_name=netbios_name, targetdir=targetdir,
579 domain_critical_only=domain_critical_only,
580 machinepass=machinepass, use_ntvfs=use_ntvfs, dns_backend=dns_backend,
581 promote_existing=True)
583 raise CommandError("Invalid role '%s' (possible values: DC, RODC)" % role)
586 class cmd_domain_join(Command):
587 """Join domain as either member or backup domain controller."""
589 synopsis = "%prog <dnsdomain> [DC|RODC|MEMBER|SUBDOMAIN] [options]"
591 takes_optiongroups = {
592 "sambaopts": options.SambaOptions,
593 "versionopts": options.VersionOptions,
594 "credopts": options.CredentialsOptions,
598 Option("--server", help="DC to join", type=str),
599 Option("--site", help="site to join", type=str),
600 Option("--targetdir", help="where to store provision", type=str),
601 Option("--parent-domain", help="parent domain to create subdomain under", type=str),
602 Option("--domain-critical-only",
603 help="only replicate critical domain objects",
604 action="store_true"),
605 Option("--machinepass", type=str, metavar="PASSWORD",
606 help="choose machine password (otherwise random)"),
607 Option("--adminpass", type="string", metavar="PASSWORD",
608 help="choose adminstrator password when joining as a subdomain (otherwise random)"),
609 Option("--dns-backend", type="choice", metavar="NAMESERVER-BACKEND",
610 choices=["SAMBA_INTERNAL", "BIND9_DLZ", "NONE"],
611 help="The DNS server backend. SAMBA_INTERNAL is the builtin name server (default), "
612 "BIND9_DLZ uses samba4 AD to store zone information, "
613 "NONE skips the DNS setup entirely (this DC will not be a DNS server)",
614 default="SAMBA_INTERNAL"),
615 Option("--quiet", help="Be quiet", action="store_true"),
616 Option("--verbose", help="Be verbose", action="store_true")
620 Option("--use-ntvfs", help="Use NTVFS for the fileserver (default = no)",
623 if samba.is_ntvfs_fileserver_built():
624 takes_options.extend(ntvfs_options)
626 takes_args = ["domain", "role?"]
628 def run(self, domain, role=None, sambaopts=None, credopts=None,
629 versionopts=None, server=None, site=None, targetdir=None,
630 domain_critical_only=False, parent_domain=None, machinepass=None,
631 use_ntvfs=False, dns_backend=None, adminpass=None,
632 quiet=False, verbose=False):
633 lp = sambaopts.get_loadparm()
634 creds = credopts.get_credentials(lp)
635 net = Net(creds, lp, server=credopts.ipaddress)
638 site = "Default-First-Site-Name"
640 logger = self.get_logger()
642 logger.setLevel(logging.DEBUG)
644 logger.setLevel(logging.WARNING)
646 logger.setLevel(logging.INFO)
648 netbios_name = lp.get("netbios name")
653 if role is None or role == "MEMBER":
654 (join_password, sid, domain_name) = net.join_member(
655 domain, netbios_name, LIBNET_JOIN_AUTOMATIC,
656 machinepass=machinepass)
658 self.errf.write("Joined domain %s (%s)\n" % (domain_name, sid))
660 join_DC(logger=logger, server=server, creds=creds, lp=lp, domain=domain,
661 site=site, netbios_name=netbios_name, targetdir=targetdir,
662 domain_critical_only=domain_critical_only,
663 machinepass=machinepass, use_ntvfs=use_ntvfs, dns_backend=dns_backend)
665 join_RODC(logger=logger, server=server, creds=creds, lp=lp, domain=domain,
666 site=site, netbios_name=netbios_name, targetdir=targetdir,
667 domain_critical_only=domain_critical_only,
668 machinepass=machinepass, use_ntvfs=use_ntvfs,
669 dns_backend=dns_backend)
670 elif role == "SUBDOMAIN":
672 logger.info("Administrator password will be set randomly!")
674 netbios_domain = lp.get("workgroup")
675 if parent_domain is None:
676 parent_domain = ".".join(domain.split(".")[1:])
677 join_subdomain(logger=logger, server=server, creds=creds, lp=lp, dnsdomain=domain,
678 parent_domain=parent_domain, site=site,
679 netbios_name=netbios_name, netbios_domain=netbios_domain,
680 targetdir=targetdir, machinepass=machinepass,
681 use_ntvfs=use_ntvfs, dns_backend=dns_backend,
684 raise CommandError("Invalid role '%s' (possible values: MEMBER, DC, RODC, SUBDOMAIN)" % role)
687 class cmd_domain_demote(Command):
688 """Demote ourselves from the role of Domain Controller."""
690 synopsis = "%prog [options]"
693 Option("--server", help="writable DC to write demotion changes on", type=str),
694 Option("-H", "--URL", help="LDB URL for database or target server", type=str,
695 metavar="URL", dest="H"),
696 Option("--remove-other-dead-server", help="Dead DC (name or NTDS GUID) "
697 "to remove ALL references to (rather than this DC)", type=str),
698 Option("--quiet", help="Be quiet", action="store_true"),
699 Option("--verbose", help="Be verbose", action="store_true"),
702 takes_optiongroups = {
703 "sambaopts": options.SambaOptions,
704 "credopts": options.CredentialsOptions,
705 "versionopts": options.VersionOptions,
708 def run(self, sambaopts=None, credopts=None,
709 versionopts=None, server=None,
710 remove_other_dead_server=None, H=None,
711 verbose=False, quiet=False):
712 lp = sambaopts.get_loadparm()
713 creds = credopts.get_credentials(lp)
714 net = Net(creds, lp, server=credopts.ipaddress)
716 logger = self.get_logger()
718 logger.setLevel(logging.DEBUG)
720 logger.setLevel(logging.WARNING)
722 logger.setLevel(logging.INFO)
724 if remove_other_dead_server is not None:
725 if server is not None:
726 samdb = SamDB(url="ldap://%s" % server,
727 session_info=system_session(),
728 credentials=creds, lp=lp)
730 samdb = SamDB(url=H, session_info=system_session(), credentials=creds, lp=lp)
732 remove_dc.remove_dc(samdb, logger, remove_other_dead_server)
733 except remove_dc.DemoteException as err:
734 raise CommandError("Demote failed: %s" % err)
737 netbios_name = lp.get("netbios name")
738 samdb = SamDB(url=H, session_info=system_session(), credentials=creds, lp=lp)
740 res = samdb.search(expression='(&(objectClass=computer)(serverReferenceBL=*))', attrs=["dnsHostName", "name"])
742 raise CommandError("Unable to search for servers")
745 raise CommandError("You are the latest server in the domain")
749 if str(e["name"]).lower() != netbios_name.lower():
750 server = e["dnsHostName"]
753 ntds_guid = samdb.get_ntds_GUID()
754 msg = samdb.search(base=str(samdb.get_config_basedn()),
755 scope=ldb.SCOPE_SUBTREE, expression="(objectGUID=%s)" % ntds_guid,
757 if len(msg) == 0 or "options" not in msg[0]:
758 raise CommandError("Failed to find options on %s" % ntds_guid)
761 dsa_options = int(str(msg[0]['options']))
763 res = samdb.search(expression="(fSMORoleOwner=%s)" % str(ntds_dn),
764 controls=["search_options:1:2"])
767 raise CommandError("Current DC is still the owner of %d role(s), use the role command to transfer roles to another DC" % len(res))
769 self.errf.write("Using %s as partner server for the demotion\n" %
771 (drsuapiBind, drsuapi_handle, supportedExtensions) = drsuapi_connect(server, lp, creds)
773 self.errf.write("Deactivating inbound replication\n")
775 if not (dsa_options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) and not samdb.am_rodc():
779 dsa_options |= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
780 nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
784 self.errf.write("Asking partner server %s to synchronize from us\n"
786 for part in (samdb.get_schema_basedn(),
787 samdb.get_config_basedn(),
788 samdb.get_root_basedn()):
789 nc = drsuapi.DsReplicaObjectIdentifier()
792 req1 = drsuapi.DsReplicaSyncRequest1()
793 req1.naming_context = nc;
794 req1.options = drsuapi.DRSUAPI_DRS_WRIT_REP
795 req1.source_dsa_guid = misc.GUID(ntds_guid)
798 drsuapiBind.DsReplicaSync(drsuapi_handle, 1, req1)
799 except RuntimeError as (werr, string):
800 if werr == 8452: #WERR_DS_DRA_NO_REPLICA
804 "Error while replicating out last local changes from '%s' for demotion, "
805 "re-enabling inbound replication\n" % part)
806 dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
807 nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
809 raise CommandError("Error while sending a DsReplicaSync for partition '%s'" % str(part), string)
811 remote_samdb = SamDB(url="ldap://%s" % server,
812 session_info=system_session(),
813 credentials=creds, lp=lp)
815 self.errf.write("Changing userControl and container\n")
816 res = remote_samdb.search(base=str(remote_samdb.domain_dn()),
817 expression="(&(objectClass=user)(sAMAccountName=%s$))" %
818 netbios_name.upper(),
819 attrs=["userAccountControl"])
821 uac = int(str(res[0]["userAccountControl"]))
825 "Error while demoting, re-enabling inbound replication\n")
826 dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
827 nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
829 raise CommandError("Error while changing account control", e)
833 "Error while demoting, re-enabling inbound replication")
834 dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
835 nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
837 raise CommandError("Unable to find object with samaccountName = %s$"
838 " in the remote dc" % netbios_name.upper())
842 uac &= ~(UF_SERVER_TRUST_ACCOUNT|UF_TRUSTED_FOR_DELEGATION|UF_PARTIAL_SECRETS_ACCOUNT)
843 uac |= UF_WORKSTATION_TRUST_ACCOUNT
848 msg["userAccountControl"] = ldb.MessageElement("%d" % uac,
849 ldb.FLAG_MOD_REPLACE,
850 "userAccountControl")
852 remote_samdb.modify(msg)
855 "Error while demoting, re-enabling inbound replication")
856 dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
857 nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
860 raise CommandError("Error while changing account control", e)
862 parent = msg.dn.parent()
863 dc_name = res[0].dn.get_rdn_value()
864 rdn = "CN=%s" % dc_name
866 # Let's move to the Computer container
870 computer_dn = ldb.Dn(remote_samdb, "CN=Computers,%s" % str(remote_samdb.domain_dn()))
871 res = remote_samdb.search(base=computer_dn, expression=rdn, scope=ldb.SCOPE_ONELEVEL)
874 res = remote_samdb.search(base=computer_dn, expression="%s-%d" % (rdn, i),
875 scope=ldb.SCOPE_ONELEVEL)
876 while(len(res) != 0 and i < 100):
878 res = remote_samdb.search(base=computer_dn, expression="%s-%d" % (rdn, i),
879 scope=ldb.SCOPE_ONELEVEL)
883 "Error while demoting, re-enabling inbound replication\n")
884 dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
885 nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
891 msg["userAccountControl"] = ldb.MessageElement("%d" % uac,
892 ldb.FLAG_MOD_REPLACE,
893 "userAccountControl")
895 remote_samdb.modify(msg)
897 raise CommandError("Unable to find a slot for renaming %s,"
898 " all names from %s-1 to %s-%d seemed used" %
899 (str(dc_dn), rdn, rdn, i - 9))
901 newrdn = "%s-%d" % (rdn, i)
904 newdn = ldb.Dn(remote_samdb, "%s,%s" % (newrdn, str(computer_dn)))
905 remote_samdb.rename(dc_dn, newdn)
908 "Error while demoting, re-enabling inbound replication\n")
909 dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
910 nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
916 msg["userAccountControl"] = ldb.MessageElement("%d" % uac,
917 ldb.FLAG_MOD_REPLACE,
918 "userAccountControl")
920 remote_samdb.modify(msg)
921 raise CommandError("Error while renaming %s to %s" % (str(dc_dn), str(newdn)), e)
924 server_dsa_dn = samdb.get_serverName()
925 domain = remote_samdb.get_root_basedn()
928 req1 = drsuapi.DsRemoveDSServerRequest1()
929 req1.server_dn = str(server_dsa_dn)
930 req1.domain_dn = str(domain)
933 drsuapiBind.DsRemoveDSServer(drsuapi_handle, 1, req1)
934 except RuntimeError as (werr, string):
935 if not (dsa_options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) and not samdb.am_rodc():
937 "Error while demoting, re-enabling inbound replication\n")
938 dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
939 nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
945 msg["userAccountControl"] = ldb.MessageElement("%d" % uac,
946 ldb.FLAG_MOD_REPLACE,
947 "userAccountControl")
948 remote_samdb.modify(msg)
949 remote_samdb.rename(newdn, dc_dn)
950 if werr == 8452: #WERR_DS_DRA_NO_REPLICA
951 raise CommandError("The DC %s is not present on (already removed from) the remote server: " % server_dsa_dn, e)
953 raise CommandError("Error while sending a removeDsServer of %s: " % server_dsa_dn, e)
955 remove_dc.remove_sysvol_references(remote_samdb, logger, dc_name)
957 # These are objects under the computer account that should be deleted
958 for s in ("CN=Enterprise,CN=NTFRS Subscriptions",
959 "CN=%s, CN=NTFRS Subscriptions" % lp.get("realm"),
960 "CN=Domain system Volumes (SYSVOL Share), CN=NTFRS Subscriptions",
961 "CN=NTFRS Subscriptions"):
963 remote_samdb.delete(ldb.Dn(remote_samdb,
964 "%s,%s" % (s, str(newdn))))
965 except ldb.LdbError, l:
968 self.errf.write("Demote successful\n")
971 class cmd_domain_level(Command):
972 """Raise domain and forest function levels."""
974 synopsis = "%prog (show|raise <options>) [options]"
976 takes_optiongroups = {
977 "sambaopts": options.SambaOptions,
978 "credopts": options.CredentialsOptions,
979 "versionopts": options.VersionOptions,
983 Option("-H", "--URL", help="LDB URL for database or target server", type=str,
984 metavar="URL", dest="H"),
985 Option("--quiet", help="Be quiet", action="store_true"),
986 Option("--forest-level", type="choice", choices=["2003", "2008", "2008_R2", "2012", "2012_R2"],
987 help="The forest function level (2003 | 2008 | 2008_R2 | 2012 | 2012_R2)"),
988 Option("--domain-level", type="choice", choices=["2003", "2008", "2008_R2", "2012", "2012_R2"],
989 help="The domain function level (2003 | 2008 | 2008_R2 | 2012 | 2012_R2)")
992 takes_args = ["subcommand"]
994 def run(self, subcommand, H=None, forest_level=None, domain_level=None,
995 quiet=False, credopts=None, sambaopts=None, versionopts=None):
996 lp = sambaopts.get_loadparm()
997 creds = credopts.get_credentials(lp, fallback_machine=True)
999 samdb = SamDB(url=H, session_info=system_session(),
1000 credentials=creds, lp=lp)
1002 domain_dn = samdb.domain_dn()
1004 res_forest = samdb.search("CN=Partitions,%s" % samdb.get_config_basedn(),
1005 scope=ldb.SCOPE_BASE, attrs=["msDS-Behavior-Version"])
1006 assert len(res_forest) == 1
1008 res_domain = samdb.search(domain_dn, scope=ldb.SCOPE_BASE,
1009 attrs=["msDS-Behavior-Version", "nTMixedDomain"])
1010 assert len(res_domain) == 1
1012 res_dc_s = samdb.search("CN=Sites,%s" % samdb.get_config_basedn(),
1013 scope=ldb.SCOPE_SUBTREE, expression="(objectClass=nTDSDSA)",
1014 attrs=["msDS-Behavior-Version"])
1015 assert len(res_dc_s) >= 1
1017 # default values, since "msDS-Behavior-Version" does not exist on Windows 2000 AD
1018 level_forest = DS_DOMAIN_FUNCTION_2000
1019 level_domain = DS_DOMAIN_FUNCTION_2000
1021 if "msDS-Behavior-Version" in res_forest[0]:
1022 level_forest = int(res_forest[0]["msDS-Behavior-Version"][0])
1023 if "msDS-Behavior-Version" in res_domain[0]:
1024 level_domain = int(res_domain[0]["msDS-Behavior-Version"][0])
1025 level_domain_mixed = int(res_domain[0]["nTMixedDomain"][0])
1028 for msg in res_dc_s:
1029 if "msDS-Behavior-Version" in msg:
1030 if min_level_dc is None or int(msg["msDS-Behavior-Version"][0]) < min_level_dc:
1031 min_level_dc = int(msg["msDS-Behavior-Version"][0])
1033 min_level_dc = DS_DOMAIN_FUNCTION_2000
1034 # well, this is the least
1037 if level_forest < DS_DOMAIN_FUNCTION_2000 or level_domain < DS_DOMAIN_FUNCTION_2000:
1038 raise CommandError("Domain and/or forest function level(s) is/are invalid. Correct them or reprovision!")
1039 if min_level_dc < DS_DOMAIN_FUNCTION_2000:
1040 raise CommandError("Lowest function level of a DC is invalid. Correct this or reprovision!")
1041 if level_forest > level_domain:
1042 raise CommandError("Forest function level is higher than the domain level(s). Correct this or reprovision!")
1043 if level_domain > min_level_dc:
1044 raise CommandError("Domain function level is higher than the lowest function level of a DC. Correct this or reprovision!")
1046 if subcommand == "show":
1047 self.message("Domain and forest function level for domain '%s'" % domain_dn)
1048 if level_forest == DS_DOMAIN_FUNCTION_2000 and level_domain_mixed != 0:
1049 self.message("\nATTENTION: You run SAMBA 4 on a forest function level lower than Windows 2000 (Native). This isn't supported! Please raise!")
1050 if level_domain == DS_DOMAIN_FUNCTION_2000 and level_domain_mixed != 0:
1051 self.message("\nATTENTION: You run SAMBA 4 on a domain function level lower than Windows 2000 (Native). This isn't supported! Please raise!")
1052 if min_level_dc == DS_DOMAIN_FUNCTION_2000 and level_domain_mixed != 0:
1053 self.message("\nATTENTION: You run SAMBA 4 on a lowest function level of a DC lower than Windows 2003. This isn't supported! Please step-up or upgrade the concerning DC(s)!")
1057 if level_forest == DS_DOMAIN_FUNCTION_2000:
1059 elif level_forest == DS_DOMAIN_FUNCTION_2003_MIXED:
1060 outstr = "2003 with mixed domains/interim (NT4 DC support)"
1061 elif level_forest == DS_DOMAIN_FUNCTION_2003:
1063 elif level_forest == DS_DOMAIN_FUNCTION_2008:
1065 elif level_forest == DS_DOMAIN_FUNCTION_2008_R2:
1067 elif level_forest == DS_DOMAIN_FUNCTION_2012:
1069 elif level_forest == DS_DOMAIN_FUNCTION_2012_R2:
1072 outstr = "higher than 2012 R2"
1073 self.message("Forest function level: (Windows) " + outstr)
1075 if level_domain == DS_DOMAIN_FUNCTION_2000 and level_domain_mixed != 0:
1076 outstr = "2000 mixed (NT4 DC support)"
1077 elif level_domain == DS_DOMAIN_FUNCTION_2000 and level_domain_mixed == 0:
1079 elif level_domain == DS_DOMAIN_FUNCTION_2003_MIXED:
1080 outstr = "2003 with mixed domains/interim (NT4 DC support)"
1081 elif level_domain == DS_DOMAIN_FUNCTION_2003:
1083 elif level_domain == DS_DOMAIN_FUNCTION_2008:
1085 elif level_domain == DS_DOMAIN_FUNCTION_2008_R2:
1087 elif level_domain == DS_DOMAIN_FUNCTION_2012:
1089 elif level_domain == DS_DOMAIN_FUNCTION_2012_R2:
1092 outstr = "higher than 2012 R2"
1093 self.message("Domain function level: (Windows) " + outstr)
1095 if min_level_dc == DS_DOMAIN_FUNCTION_2000:
1097 elif min_level_dc == DS_DOMAIN_FUNCTION_2003:
1099 elif min_level_dc == DS_DOMAIN_FUNCTION_2008:
1101 elif min_level_dc == DS_DOMAIN_FUNCTION_2008_R2:
1103 elif min_level_dc == DS_DOMAIN_FUNCTION_2012:
1105 elif min_level_dc == DS_DOMAIN_FUNCTION_2012_R2:
1108 outstr = "higher than 2012 R2"
1109 self.message("Lowest function level of a DC: (Windows) " + outstr)
1111 elif subcommand == "raise":
1114 if domain_level is not None:
1115 if domain_level == "2003":
1116 new_level_domain = DS_DOMAIN_FUNCTION_2003
1117 elif domain_level == "2008":
1118 new_level_domain = DS_DOMAIN_FUNCTION_2008
1119 elif domain_level == "2008_R2":
1120 new_level_domain = DS_DOMAIN_FUNCTION_2008_R2
1121 elif domain_level == "2012":
1122 new_level_domain = DS_DOMAIN_FUNCTION_2012
1123 elif domain_level == "2012_R2":
1124 new_level_domain = DS_DOMAIN_FUNCTION_2012_R2
1126 if new_level_domain <= level_domain and level_domain_mixed == 0:
1127 raise CommandError("Domain function level can't be smaller than or equal to the actual one!")
1128 if new_level_domain > min_level_dc:
1129 raise CommandError("Domain function level can't be higher than the lowest function level of a DC!")
1131 # Deactivate mixed/interim domain support
1132 if level_domain_mixed != 0:
1133 # Directly on the base DN
1135 m.dn = ldb.Dn(samdb, domain_dn)
1136 m["nTMixedDomain"] = ldb.MessageElement("0",
1137 ldb.FLAG_MOD_REPLACE, "nTMixedDomain")
1141 m.dn = ldb.Dn(samdb, "CN=" + lp.get("workgroup") + ",CN=Partitions,%s" % samdb.get_config_basedn())
1142 m["nTMixedDomain"] = ldb.MessageElement("0",
1143 ldb.FLAG_MOD_REPLACE, "nTMixedDomain")
1146 except ldb.LdbError, (enum, emsg):
1147 if enum != ldb.ERR_UNWILLING_TO_PERFORM:
1150 # Directly on the base DN
1152 m.dn = ldb.Dn(samdb, domain_dn)
1153 m["msDS-Behavior-Version"]= ldb.MessageElement(
1154 str(new_level_domain), ldb.FLAG_MOD_REPLACE,
1155 "msDS-Behavior-Version")
1159 m.dn = ldb.Dn(samdb, "CN=" + lp.get("workgroup")
1160 + ",CN=Partitions,%s" % samdb.get_config_basedn())
1161 m["msDS-Behavior-Version"]= ldb.MessageElement(
1162 str(new_level_domain), ldb.FLAG_MOD_REPLACE,
1163 "msDS-Behavior-Version")
1166 except ldb.LdbError, (enum, emsg):
1167 if enum != ldb.ERR_UNWILLING_TO_PERFORM:
1170 level_domain = new_level_domain
1171 msgs.append("Domain function level changed!")
1173 if forest_level is not None:
1174 if forest_level == "2003":
1175 new_level_forest = DS_DOMAIN_FUNCTION_2003
1176 elif forest_level == "2008":
1177 new_level_forest = DS_DOMAIN_FUNCTION_2008
1178 elif forest_level == "2008_R2":
1179 new_level_forest = DS_DOMAIN_FUNCTION_2008_R2
1180 elif forest_level == "2012":
1181 new_level_forest = DS_DOMAIN_FUNCTION_2012
1182 elif forest_level == "2012_R2":
1183 new_level_forest = DS_DOMAIN_FUNCTION_2012_R2
1185 if new_level_forest <= level_forest:
1186 raise CommandError("Forest function level can't be smaller than or equal to the actual one!")
1187 if new_level_forest > level_domain:
1188 raise CommandError("Forest function level can't be higher than the domain function level(s). Please raise it/them first!")
1191 m.dn = ldb.Dn(samdb, "CN=Partitions,%s" % samdb.get_config_basedn())
1192 m["msDS-Behavior-Version"]= ldb.MessageElement(
1193 str(new_level_forest), ldb.FLAG_MOD_REPLACE,
1194 "msDS-Behavior-Version")
1196 msgs.append("Forest function level changed!")
1197 msgs.append("All changes applied successfully!")
1198 self.message("\n".join(msgs))
1200 raise CommandError("invalid argument: '%s' (choose from 'show', 'raise')" % subcommand)
1203 class cmd_domain_passwordsettings(Command):
1204 """Set password settings.
1206 Password complexity, password lockout policy, history length,
1207 minimum password length, the minimum and maximum password age) on
1208 a Samba AD DC server.
1210 Use against a Windows DC is possible, but group policy will override it.
1213 synopsis = "%prog (show|set <options>) [options]"
1215 takes_optiongroups = {
1216 "sambaopts": options.SambaOptions,
1217 "versionopts": options.VersionOptions,
1218 "credopts": options.CredentialsOptions,
1222 Option("-H", "--URL", help="LDB URL for database or target server", type=str,
1223 metavar="URL", dest="H"),
1224 Option("--quiet", help="Be quiet", action="store_true"),
1225 Option("--complexity", type="choice", choices=["on","off","default"],
1226 help="The password complexity (on | off | default). Default is 'on'"),
1227 Option("--store-plaintext", type="choice", choices=["on","off","default"],
1228 help="Store plaintext passwords where account have 'store passwords with reversible encryption' set (on | off | default). Default is 'off'"),
1229 Option("--history-length",
1230 help="The password history length (<integer> | default). Default is 24.", type=str),
1231 Option("--min-pwd-length",
1232 help="The minimum password length (<integer> | default). Default is 7.", type=str),
1233 Option("--min-pwd-age",
1234 help="The minimum password age (<integer in days> | default). Default is 1.", type=str),
1235 Option("--max-pwd-age",
1236 help="The maximum password age (<integer in days> | default). Default is 43.", type=str),
1237 Option("--account-lockout-duration",
1238 help="The the length of time an account is locked out after exeeding the limit on bad password attempts (<integer in mins> | default). Default is 30 mins.", type=str),
1239 Option("--account-lockout-threshold",
1240 help="The number of bad password attempts allowed before locking out the account (<integer> | default). Default is 0 (never lock out).", type=str),
1241 Option("--reset-account-lockout-after",
1242 help="After this time is elapsed, the recorded number of attempts restarts from zero (<integer> | default). Default is 30.", type=str),
1245 takes_args = ["subcommand"]
1247 def run(self, subcommand, H=None, min_pwd_age=None, max_pwd_age=None,
1248 quiet=False, complexity=None, store_plaintext=None, history_length=None,
1249 min_pwd_length=None, account_lockout_duration=None, account_lockout_threshold=None,
1250 reset_account_lockout_after=None, credopts=None, sambaopts=None,
1252 lp = sambaopts.get_loadparm()
1253 creds = credopts.get_credentials(lp)
1255 samdb = SamDB(url=H, session_info=system_session(),
1256 credentials=creds, lp=lp)
1258 domain_dn = samdb.domain_dn()
1259 res = samdb.search(domain_dn, scope=ldb.SCOPE_BASE,
1260 attrs=["pwdProperties", "pwdHistoryLength", "minPwdLength",
1261 "minPwdAge", "maxPwdAge", "lockoutDuration", "lockoutThreshold",
1262 "lockOutObservationWindow"])
1263 assert(len(res) == 1)
1265 pwd_props = int(res[0]["pwdProperties"][0])
1266 pwd_hist_len = int(res[0]["pwdHistoryLength"][0])
1267 cur_min_pwd_len = int(res[0]["minPwdLength"][0])
1269 cur_min_pwd_age = int(abs(int(res[0]["minPwdAge"][0])) / (1e7 * 60 * 60 * 24))
1270 if int(res[0]["maxPwdAge"][0]) == -0x8000000000000000:
1273 cur_max_pwd_age = int(abs(int(res[0]["maxPwdAge"][0])) / (1e7 * 60 * 60 * 24))
1274 cur_account_lockout_threshold = int(res[0]["lockoutThreshold"][0])
1276 if int(res[0]["lockoutDuration"][0]) == -0x8000000000000000:
1277 cur_account_lockout_duration = 0
1279 cur_account_lockout_duration = abs(int(res[0]["lockoutDuration"][0])) / (1e7 * 60)
1280 cur_reset_account_lockout_after = abs(int(res[0]["lockOutObservationWindow"][0])) / (1e7 * 60)
1281 except Exception, e:
1282 raise CommandError("Could not retrieve password properties!", e)
1284 if subcommand == "show":
1285 self.message("Password informations for domain '%s'" % domain_dn)
1287 if pwd_props & DOMAIN_PASSWORD_COMPLEX != 0:
1288 self.message("Password complexity: on")
1290 self.message("Password complexity: off")
1291 if pwd_props & DOMAIN_PASSWORD_STORE_CLEARTEXT != 0:
1292 self.message("Store plaintext passwords: on")
1294 self.message("Store plaintext passwords: off")
1295 self.message("Password history length: %d" % pwd_hist_len)
1296 self.message("Minimum password length: %d" % cur_min_pwd_len)
1297 self.message("Minimum password age (days): %d" % cur_min_pwd_age)
1298 self.message("Maximum password age (days): %d" % cur_max_pwd_age)
1299 self.message("Account lockout duration (mins): %d" % cur_account_lockout_duration)
1300 self.message("Account lockout threshold (attempts): %d" % cur_account_lockout_threshold)
1301 self.message("Reset account lockout after (mins): %d" % cur_reset_account_lockout_after)
1302 elif subcommand == "set":
1305 m.dn = ldb.Dn(samdb, domain_dn)
1307 if complexity is not None:
1308 if complexity == "on" or complexity == "default":
1309 pwd_props = pwd_props | DOMAIN_PASSWORD_COMPLEX
1310 msgs.append("Password complexity activated!")
1311 elif complexity == "off":
1312 pwd_props = pwd_props & (~DOMAIN_PASSWORD_COMPLEX)
1313 msgs.append("Password complexity deactivated!")
1315 if store_plaintext is not None:
1316 if store_plaintext == "on" or store_plaintext == "default":
1317 pwd_props = pwd_props | DOMAIN_PASSWORD_STORE_CLEARTEXT
1318 msgs.append("Plaintext password storage for changed passwords activated!")
1319 elif store_plaintext == "off":
1320 pwd_props = pwd_props & (~DOMAIN_PASSWORD_STORE_CLEARTEXT)
1321 msgs.append("Plaintext password storage for changed passwords deactivated!")
1323 if complexity is not None or store_plaintext is not None:
1324 m["pwdProperties"] = ldb.MessageElement(str(pwd_props),
1325 ldb.FLAG_MOD_REPLACE, "pwdProperties")
1327 if history_length is not None:
1328 if history_length == "default":
1331 pwd_hist_len = int(history_length)
1333 if pwd_hist_len < 0 or pwd_hist_len > 24:
1334 raise CommandError("Password history length must be in the range of 0 to 24!")
1336 m["pwdHistoryLength"] = ldb.MessageElement(str(pwd_hist_len),
1337 ldb.FLAG_MOD_REPLACE, "pwdHistoryLength")
1338 msgs.append("Password history length changed!")
1340 if min_pwd_length is not None:
1341 if min_pwd_length == "default":
1344 min_pwd_len = int(min_pwd_length)
1346 if min_pwd_len < 0 or min_pwd_len > 14:
1347 raise CommandError("Minimum password length must be in the range of 0 to 14!")
1349 m["minPwdLength"] = ldb.MessageElement(str(min_pwd_len),
1350 ldb.FLAG_MOD_REPLACE, "minPwdLength")
1351 msgs.append("Minimum password length changed!")
1353 if min_pwd_age is not None:
1354 if min_pwd_age == "default":
1357 min_pwd_age = int(min_pwd_age)
1359 if min_pwd_age < 0 or min_pwd_age > 998:
1360 raise CommandError("Minimum password age must be in the range of 0 to 998!")
1363 min_pwd_age_ticks = -int(min_pwd_age * (24 * 60 * 60 * 1e7))
1365 m["minPwdAge"] = ldb.MessageElement(str(min_pwd_age_ticks),
1366 ldb.FLAG_MOD_REPLACE, "minPwdAge")
1367 msgs.append("Minimum password age changed!")
1369 if max_pwd_age is not None:
1370 if max_pwd_age == "default":
1373 max_pwd_age = int(max_pwd_age)
1375 if max_pwd_age < 0 or max_pwd_age > 999:
1376 raise CommandError("Maximum password age must be in the range of 0 to 999!")
1379 if max_pwd_age == 0:
1380 max_pwd_age_ticks = -0x8000000000000000
1382 max_pwd_age_ticks = -int(max_pwd_age * (24 * 60 * 60 * 1e7))
1384 m["maxPwdAge"] = ldb.MessageElement(str(max_pwd_age_ticks),
1385 ldb.FLAG_MOD_REPLACE, "maxPwdAge")
1386 msgs.append("Maximum password age changed!")
1388 if account_lockout_duration is not None:
1389 if account_lockout_duration == "default":
1390 account_lockout_duration = 30
1392 account_lockout_duration = int(account_lockout_duration)
1394 if account_lockout_duration < 0 or account_lockout_duration > 99999:
1395 raise CommandError("Maximum password age must be in the range of 0 to 99999!")
1398 if account_lockout_duration == 0:
1399 account_lockout_duration_ticks = -0x8000000000000000
1401 account_lockout_duration_ticks = -int(account_lockout_duration * (60 * 1e7))
1403 m["lockoutDuration"] = ldb.MessageElement(str(account_lockout_duration_ticks),
1404 ldb.FLAG_MOD_REPLACE, "lockoutDuration")
1405 msgs.append("Account lockout duration changed!")
1407 if account_lockout_threshold is not None:
1408 if account_lockout_threshold == "default":
1409 account_lockout_threshold = 0
1411 account_lockout_threshold = int(account_lockout_threshold)
1413 m["lockoutThreshold"] = ldb.MessageElement(str(account_lockout_threshold),
1414 ldb.FLAG_MOD_REPLACE, "lockoutThreshold")
1415 msgs.append("Account lockout threshold changed!")
1417 if reset_account_lockout_after is not None:
1418 if reset_account_lockout_after == "default":
1419 reset_account_lockout_after = 30
1421 reset_account_lockout_after = int(reset_account_lockout_after)
1423 if reset_account_lockout_after < 0 or reset_account_lockout_after > 99999:
1424 raise CommandError("Maximum password age must be in the range of 0 to 99999!")
1427 if reset_account_lockout_after == 0:
1428 reset_account_lockout_after_ticks = -0x8000000000000000
1430 reset_account_lockout_after_ticks = -int(reset_account_lockout_after * (60 * 1e7))
1432 m["lockOutObservationWindow"] = ldb.MessageElement(str(reset_account_lockout_after_ticks),
1433 ldb.FLAG_MOD_REPLACE, "lockOutObservationWindow")
1434 msgs.append("Duration to reset account lockout after changed!")
1436 if max_pwd_age > 0 and min_pwd_age >= max_pwd_age:
1437 raise CommandError("Maximum password age (%d) must be greater than minimum password age (%d)!" % (max_pwd_age, min_pwd_age))
1440 raise CommandError("You must specify at least one option to set. Try --help")
1442 msgs.append("All changes applied successfully!")
1443 self.message("\n".join(msgs))
1445 raise CommandError("Wrong argument '%s'!" % subcommand)
1448 class cmd_domain_classicupgrade(Command):
1449 """Upgrade from Samba classic (NT4-like) database to Samba AD DC database.
1451 Specify either a directory with all Samba classic DC databases and state files (with --dbdir) or
1452 the testparm utility from your classic installation (with --testparm).
1455 synopsis = "%prog [options] <classic_smb_conf>"
1457 takes_optiongroups = {
1458 "sambaopts": options.SambaOptions,
1459 "versionopts": options.VersionOptions
1463 Option("--dbdir", type="string", metavar="DIR",
1464 help="Path to samba classic DC database directory"),
1465 Option("--testparm", type="string", metavar="PATH",
1466 help="Path to samba classic DC testparm utility from the previous installation. This allows the default paths of the previous installation to be followed"),
1467 Option("--targetdir", type="string", metavar="DIR",
1468 help="Path prefix where the new Samba 4.0 AD domain should be initialised"),
1469 Option("--quiet", help="Be quiet", action="store_true"),
1470 Option("--verbose", help="Be verbose", action="store_true"),
1471 Option("--dns-backend", type="choice", metavar="NAMESERVER-BACKEND",
1472 choices=["SAMBA_INTERNAL", "BIND9_FLATFILE", "BIND9_DLZ", "NONE"],
1473 help="The DNS server backend. SAMBA_INTERNAL is the builtin name server (default), "
1474 "BIND9_FLATFILE uses bind9 text database to store zone information, "
1475 "BIND9_DLZ uses samba4 AD to store zone information, "
1476 "NONE skips the DNS setup entirely (this DC will not be a DNS server)",
1477 default="SAMBA_INTERNAL")
1481 Option("--use-ntvfs", help="Use NTVFS for the fileserver (default = no)",
1482 action="store_true"),
1483 Option("--use-xattrs", type="choice", choices=["yes","no","auto"],
1484 metavar="[yes|no|auto]",
1485 help="Define if we should use the native fs capabilities or a tdb file for "
1486 "storing attributes likes ntacl when --use-ntvfs is set. "
1487 "auto tries to make an inteligent guess based on the user rights and system capabilities",
1490 if samba.is_ntvfs_fileserver_built():
1491 takes_options.extend(ntvfs_options)
1493 takes_args = ["smbconf"]
1495 def run(self, smbconf=None, targetdir=None, dbdir=None, testparm=None,
1496 quiet=False, verbose=False, use_xattrs="auto", sambaopts=None, versionopts=None,
1497 dns_backend=None, use_ntvfs=False):
1499 if not os.path.exists(smbconf):
1500 raise CommandError("File %s does not exist" % smbconf)
1502 if testparm and not os.path.exists(testparm):
1503 raise CommandError("Testparm utility %s does not exist" % testparm)
1505 if dbdir and not os.path.exists(dbdir):
1506 raise CommandError("Directory %s does not exist" % dbdir)
1508 if not dbdir and not testparm:
1509 raise CommandError("Please specify either dbdir or testparm")
1511 logger = self.get_logger()
1513 logger.setLevel(logging.DEBUG)
1515 logger.setLevel(logging.WARNING)
1517 logger.setLevel(logging.INFO)
1519 if dbdir and testparm:
1520 logger.warning("both dbdir and testparm specified, ignoring dbdir.")
1523 lp = sambaopts.get_loadparm()
1525 s3conf = s3param.get_context()
1528 s3conf.set("realm", sambaopts.realm)
1530 if targetdir is not None:
1531 if not os.path.isdir(targetdir):
1535 if use_xattrs == "yes":
1537 elif use_xattrs == "auto" and use_ntvfs == False:
1539 elif use_ntvfs == False:
1540 raise CommandError("--use-xattrs=no requires --use-ntvfs (not supported for production use). "
1541 "Please re-run with --use-xattrs omitted.")
1542 elif use_xattrs == "auto" and not s3conf.get("posix:eadb"):
1544 tmpfile = tempfile.NamedTemporaryFile(dir=os.path.abspath(targetdir))
1546 tmpfile = tempfile.NamedTemporaryFile(dir=os.path.abspath(os.path.dirname(lp.get("private dir"))))
1549 samba.ntacls.setntacl(lp, tmpfile.name,
1550 "O:S-1-5-32G:S-1-5-32", "S-1-5-32", "native")
1553 # FIXME: Don't catch all exceptions here
1554 logger.info("You are not root or your system does not support xattr, using tdb backend for attributes. "
1555 "If you intend to use this provision in production, rerun the script as root on a system supporting xattrs.")
1559 # Set correct default values from dbdir or testparm
1562 paths["state directory"] = dbdir
1563 paths["private dir"] = dbdir
1564 paths["lock directory"] = dbdir
1565 paths["smb passwd file"] = dbdir + "/smbpasswd"
1567 paths["state directory"] = get_testparm_var(testparm, smbconf, "state directory")
1568 paths["private dir"] = get_testparm_var(testparm, smbconf, "private dir")
1569 paths["smb passwd file"] = get_testparm_var(testparm, smbconf, "smb passwd file")
1570 paths["lock directory"] = get_testparm_var(testparm, smbconf, "lock directory")
1571 # "testparm" from Samba 3 < 3.4.x is not aware of the parameter
1572 # "state directory", instead make use of "lock directory"
1573 if len(paths["state directory"]) == 0:
1574 paths["state directory"] = paths["lock directory"]
1577 s3conf.set(p, paths[p])
1579 # load smb.conf parameters
1580 logger.info("Reading smb.conf")
1581 s3conf.load(smbconf)
1582 samba3 = Samba3(smbconf, s3conf)
1584 logger.info("Provisioning")
1585 upgrade_from_samba3(samba3, logger, targetdir, session_info=system_session(),
1586 useeadb=eadb, dns_backend=dns_backend, use_ntvfs=use_ntvfs)
1589 class cmd_domain_samba3upgrade(cmd_domain_classicupgrade):
1590 __doc__ = cmd_domain_classicupgrade.__doc__
1592 # This command is present for backwards compatibility only,
1593 # and should not be shown.
1597 class LocalDCCredentialsOptions(options.CredentialsOptions):
1598 def __init__(self, parser):
1599 options.CredentialsOptions.__init__(self, parser, special_name="local-dc")
1601 class DomainTrustCommand(Command):
1602 """List domain trusts."""
1605 Command.__init__(self)
1606 self.local_lp = None
1608 self.local_server = None
1609 self.local_binding_string = None
1610 self.local_creds = None
1612 self.remote_server = None
1613 self.remote_binding_string = None
1614 self.remote_creds = None
1616 WERR_OK = 0x00000000
1617 WERR_INVALID_FUNCTION = 0x00000001
1618 WERR_NERR_ACFNOTLOADED = 0x000008B3
1619 WERR_RPC_S_PROCNUM_OUT_OF_RANGE = 0x000006D1
1621 def _uint32(self, v):
1622 return ctypes.c_uint32(v).value
1624 def check_runtime_error(self, runtime, val):
1628 err32 = self._uint32(runtime[0])
1634 class LocalRuntimeError(CommandError):
1635 def __init__(exception_self, self, runtime, message):
1636 err32 = self._uint32(runtime[0])
1638 msg = "LOCAL_DC[%s]: %s - ERROR(0x%08X) - %s" % (
1639 self.local_server, message, err32, errstr)
1640 CommandError.__init__(exception_self, msg)
1642 class RemoteRuntimeError(CommandError):
1643 def __init__(exception_self, self, runtime, message):
1644 err32 = self._uint32(runtime[0])
1646 msg = "REMOTE_DC[%s]: %s - ERROR(0x%08X) - %s" % (
1647 self.remote_server, message, err32, errstr)
1648 CommandError.__init__(exception_self, msg)
1650 class LocalLdbError(CommandError):
1651 def __init__(exception_self, self, ldb_error, message):
1652 errval = ldb_error[0]
1653 errstr = ldb_error[1]
1654 msg = "LOCAL_DC[%s]: %s - ERROR(%d) - %s" % (
1655 self.local_server, message, errval, errstr)
1656 CommandError.__init__(exception_self, msg)
1658 def setup_local_server(self, sambaopts, localdcopts):
1659 if self.local_server is not None:
1660 return self.local_server
1662 lp = sambaopts.get_loadparm()
1664 local_server = localdcopts.ipaddress
1665 if local_server is None:
1666 server_role = lp.server_role()
1667 if server_role != "ROLE_ACTIVE_DIRECTORY_DC":
1668 raise CommandError("Invalid server_role %s" % (server_role))
1669 local_server = lp.get('netbios name')
1670 local_transport = "ncalrpc"
1671 local_binding_options = ""
1672 local_binding_options += ",auth_type=ncalrpc_as_system"
1673 local_ldap_url = None
1676 local_transport = "ncacn_np"
1677 local_binding_options = ""
1678 local_ldap_url = "ldap://%s" % local_server
1679 local_creds = localdcopts.get_credentials(lp)
1683 self.local_server = local_server
1684 self.local_binding_string = "%s:%s[%s]" % (local_transport, local_server, local_binding_options)
1685 self.local_ldap_url = local_ldap_url
1686 self.local_creds = local_creds
1687 return self.local_server
1689 def new_local_lsa_connection(self):
1690 return lsa.lsarpc(self.local_binding_string, self.local_lp, self.local_creds)
1692 def new_local_netlogon_connection(self):
1693 return netlogon.netlogon(self.local_binding_string, self.local_lp, self.local_creds)
1695 def new_local_ldap_connection(self):
1696 return SamDB(url=self.local_ldap_url,
1697 session_info=system_session(),
1698 credentials=self.local_creds,
1701 def setup_remote_server(self, credopts, domain,
1703 require_writable=True):
1706 assert require_writable
1708 if self.remote_server is not None:
1709 return self.remote_server
1711 self.remote_server = "__unknown__remote_server__.%s" % domain
1712 assert self.local_server is not None
1714 remote_creds = credopts.get_credentials(self.local_lp)
1715 remote_server = credopts.ipaddress
1716 remote_binding_options = ""
1718 # TODO: we should also support NT4 domains
1719 # we could use local_netlogon.netr_DsRGetDCNameEx2() with the remote domain name
1720 # and delegate NBT or CLDAP to the local netlogon server
1722 remote_net = Net(remote_creds, self.local_lp, server=remote_server)
1723 remote_flags = nbt.NBT_SERVER_LDAP | nbt.NBT_SERVER_DS
1724 if require_writable:
1725 remote_flags |= nbt.NBT_SERVER_WRITABLE
1727 remote_flags |= nbt.NBT_SERVER_PDC
1728 remote_info = remote_net.finddc(flags=remote_flags, domain=domain, address=remote_server)
1730 raise CommandError("Failed to find a writeable DC for domain '%s'" % domain)
1732 nbt.NBT_SERVER_PDC: "PDC",
1733 nbt.NBT_SERVER_GC: "GC",
1734 nbt.NBT_SERVER_LDAP: "LDAP",
1735 nbt.NBT_SERVER_DS: "DS",
1736 nbt.NBT_SERVER_KDC: "KDC",
1737 nbt.NBT_SERVER_TIMESERV: "TIMESERV",
1738 nbt.NBT_SERVER_CLOSEST: "CLOSEST",
1739 nbt.NBT_SERVER_WRITABLE: "WRITABLE",
1740 nbt.NBT_SERVER_GOOD_TIMESERV: "GOOD_TIMESERV",
1741 nbt.NBT_SERVER_NDNC: "NDNC",
1742 nbt.NBT_SERVER_SELECT_SECRET_DOMAIN_6: "SELECT_SECRET_DOMAIN_6",
1743 nbt.NBT_SERVER_FULL_SECRET_DOMAIN_6: "FULL_SECRET_DOMAIN_6",
1744 nbt.NBT_SERVER_ADS_WEB_SERVICE: "ADS_WEB_SERVICE",
1745 nbt.NBT_SERVER_DS_8: "DS_8",
1746 nbt.NBT_SERVER_HAS_DNS_NAME: "HAS_DNS_NAME",
1747 nbt.NBT_SERVER_IS_DEFAULT_NC: "IS_DEFAULT_NC",
1748 nbt.NBT_SERVER_FOREST_ROOT: "FOREST_ROOT",
1750 server_type_string = self.generic_bitmap_to_string(flag_map,
1751 remote_info.server_type, names_only=True)
1752 self.outf.write("RemoteDC Netbios[%s] DNS[%s] ServerType[%s]\n" % (
1753 remote_info.pdc_name,
1754 remote_info.pdc_dns_name,
1755 server_type_string))
1757 self.remote_server = remote_info.pdc_dns_name
1758 self.remote_binding_string="ncacn_np:%s[%s]" % (self.remote_server, remote_binding_options)
1759 self.remote_creds = remote_creds
1760 return self.remote_server
1762 def new_remote_lsa_connection(self):
1763 return lsa.lsarpc(self.remote_binding_string, self.local_lp, self.remote_creds)
1765 def new_remote_netlogon_connection(self):
1766 return netlogon.netlogon(self.remote_binding_string, self.local_lp, self.remote_creds)
1768 def get_lsa_info(self, conn, policy_access):
1769 objectAttr = lsa.ObjectAttribute()
1770 objectAttr.sec_qos = lsa.QosInfo()
1772 policy = conn.OpenPolicy2(''.decode('utf-8'),
1773 objectAttr, policy_access)
1775 info = conn.QueryInfoPolicy2(policy, lsa.LSA_POLICY_INFO_DNS)
1777 return (policy, info)
1779 def get_netlogon_dc_info(self, conn, server):
1780 info = conn.netr_DsRGetDCNameEx2(server,
1781 None, 0, None, None, None,
1782 netlogon.DS_RETURN_DNS_NAME)
1785 def netr_DomainTrust_to_name(self, t):
1786 if t.trust_type == lsa.LSA_TRUST_TYPE_DOWNLEVEL:
1787 return t.netbios_name
1791 def netr_DomainTrust_to_type(self, a, t):
1793 primary_parent = None
1795 if _t.trust_flags & netlogon.NETR_TRUST_FLAG_PRIMARY:
1797 if not _t.trust_flags & netlogon.NETR_TRUST_FLAG_TREEROOT:
1798 primary_parent = a[_t.parent_index]
1801 if t.trust_flags & netlogon.NETR_TRUST_FLAG_IN_FOREST:
1802 if t is primary_parent:
1805 if t.trust_flags & netlogon.NETR_TRUST_FLAG_TREEROOT:
1808 parent = a[t.parent_index]
1809 if parent is primary:
1814 if t.trust_attributes & lsa.LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE:
1819 def netr_DomainTrust_to_transitive(self, t):
1820 if t.trust_flags & netlogon.NETR_TRUST_FLAG_IN_FOREST:
1823 if t.trust_attributes & lsa.LSA_TRUST_ATTRIBUTE_NON_TRANSITIVE:
1826 if t.trust_attributes & lsa.LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE:
1831 def netr_DomainTrust_to_direction(self, t):
1832 if t.trust_flags & netlogon.NETR_TRUST_FLAG_INBOUND and \
1833 t.trust_flags & netlogon.NETR_TRUST_FLAG_OUTBOUND:
1836 if t.trust_flags & netlogon.NETR_TRUST_FLAG_INBOUND:
1839 if t.trust_flags & netlogon.NETR_TRUST_FLAG_OUTBOUND:
1844 def generic_enum_to_string(self, e_dict, v, names_only=False):
1848 v32 = self._uint32(v)
1849 w = "__unknown__%08X__" % v32
1851 r = "0x%x (%s)" % (v, w)
1854 def generic_bitmap_to_string(self, b_dict, v, names_only=False):
1859 for b in sorted(b_dict.keys()):
1866 c32 = self._uint32(c)
1867 s += ["__unknown_%08X__" % c32]
1872 r = "0x%x (%s)" % (v, w)
1875 def trustType_string(self, v):
1877 lsa.LSA_TRUST_TYPE_DOWNLEVEL : "DOWNLEVEL",
1878 lsa.LSA_TRUST_TYPE_UPLEVEL : "UPLEVEL",
1879 lsa.LSA_TRUST_TYPE_MIT : "MIT",
1880 lsa.LSA_TRUST_TYPE_DCE : "DCE",
1882 return self.generic_enum_to_string(types, v)
1884 def trustDirection_string(self, v):
1886 lsa.LSA_TRUST_DIRECTION_INBOUND |
1887 lsa.LSA_TRUST_DIRECTION_OUTBOUND : "BOTH",
1888 lsa.LSA_TRUST_DIRECTION_INBOUND : "INBOUND",
1889 lsa.LSA_TRUST_DIRECTION_OUTBOUND : "OUTBOUND",
1891 return self.generic_enum_to_string(directions, v)
1893 def trustAttributes_string(self, v):
1895 lsa.LSA_TRUST_ATTRIBUTE_NON_TRANSITIVE : "NON_TRANSITIVE",
1896 lsa.LSA_TRUST_ATTRIBUTE_UPLEVEL_ONLY : "UPLEVEL_ONLY",
1897 lsa.LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN : "QUARANTINED_DOMAIN",
1898 lsa.LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE : "FOREST_TRANSITIVE",
1899 lsa.LSA_TRUST_ATTRIBUTE_CROSS_ORGANIZATION : "CROSS_ORGANIZATION",
1900 lsa.LSA_TRUST_ATTRIBUTE_WITHIN_FOREST : "WITHIN_FOREST",
1901 lsa.LSA_TRUST_ATTRIBUTE_TREAT_AS_EXTERNAL : "TREAT_AS_EXTERNAL",
1902 lsa.LSA_TRUST_ATTRIBUTE_USES_RC4_ENCRYPTION : "USES_RC4_ENCRYPTION",
1904 return self.generic_bitmap_to_string(attributes, v)
1906 def kerb_EncTypes_string(self, v):
1908 security.KERB_ENCTYPE_DES_CBC_CRC : "DES_CBC_CRC",
1909 security.KERB_ENCTYPE_DES_CBC_MD5 : "DES_CBC_MD5",
1910 security.KERB_ENCTYPE_RC4_HMAC_MD5 : "RC4_HMAC_MD5",
1911 security.KERB_ENCTYPE_AES128_CTS_HMAC_SHA1_96 : "AES128_CTS_HMAC_SHA1_96",
1912 security.KERB_ENCTYPE_AES256_CTS_HMAC_SHA1_96 : "AES256_CTS_HMAC_SHA1_96",
1913 security.KERB_ENCTYPE_FAST_SUPPORTED : "FAST_SUPPORTED",
1914 security.KERB_ENCTYPE_COMPOUND_IDENTITY_SUPPORTED : "COMPOUND_IDENTITY_SUPPORTED",
1915 security.KERB_ENCTYPE_CLAIMS_SUPPORTED : "CLAIMS_SUPPORTED",
1916 security.KERB_ENCTYPE_RESOURCE_SID_COMPRESSION_DISABLED : "RESOURCE_SID_COMPRESSION_DISABLED",
1918 return self.generic_bitmap_to_string(enctypes, v)
1920 def entry_tln_status(self, e_flags, ):
1922 return "Status[Enabled]"
1925 lsa.LSA_TLN_DISABLED_NEW : "Disabled-New",
1926 lsa.LSA_TLN_DISABLED_ADMIN : "Disabled",
1927 lsa.LSA_TLN_DISABLED_CONFLICT : "Disabled-Conflicting",
1929 return "Status[%s]" % self.generic_bitmap_to_string(flags, e_flags, names_only=True)
1931 def entry_dom_status(self, e_flags):
1933 return "Status[Enabled]"
1936 lsa.LSA_SID_DISABLED_ADMIN : "Disabled-SID",
1937 lsa.LSA_SID_DISABLED_CONFLICT : "Disabled-SID-Conflicting",
1938 lsa.LSA_NB_DISABLED_ADMIN : "Disabled-NB",
1939 lsa.LSA_NB_DISABLED_CONFLICT : "Disabled-NB-Conflicting",
1941 return "Status[%s]" % self.generic_bitmap_to_string(flags, e_flags, names_only=True)
1943 def write_forest_trust_info(self, fti, tln=None, collisions=None):
1945 tln_string = " TDO[%s]" % tln
1949 self.outf.write("Namespaces[%d]%s:\n" % (
1950 len(fti.entries), tln_string))
1952 for i in xrange(0, len(fti.entries)):
1956 collision_string = ""
1958 if collisions is not None:
1959 for c in collisions.entries:
1963 collision_string = " Collision[%s]" % (c.name.string)
1965 d = e.forest_trust_data
1966 if e.type == lsa.LSA_FOREST_TRUST_TOP_LEVEL_NAME:
1967 self.outf.write("TLN: %-32s DNS[*.%s]%s\n" % (
1968 self.entry_tln_status(flags),
1969 d.string, collision_string))
1970 elif e.type == lsa.LSA_FOREST_TRUST_TOP_LEVEL_NAME_EX:
1971 self.outf.write("TLN_EX: %-29s DNS[*.%s]\n" % (
1973 elif e.type == lsa.LSA_FOREST_TRUST_DOMAIN_INFO:
1974 self.outf.write("DOM: %-32s DNS[%s] Netbios[%s] SID[%s]%s\n" % (
1975 self.entry_dom_status(flags),
1976 d.dns_domain_name.string,
1977 d.netbios_domain_name.string,
1978 d.domain_sid, collision_string))
1981 class cmd_domain_trust_list(DomainTrustCommand):
1982 """List domain trusts."""
1984 synopsis = "%prog [options]"
1986 takes_optiongroups = {
1987 "sambaopts": options.SambaOptions,
1988 "versionopts": options.VersionOptions,
1989 "localdcopts": LocalDCCredentialsOptions,
1995 def run(self, sambaopts=None, versionopts=None, localdcopts=None):
1997 local_server = self.setup_local_server(sambaopts, localdcopts)
1999 local_netlogon = self.new_local_netlogon_connection()
2000 except RuntimeError as error:
2001 raise self.LocalRuntimeError(self, error, "failed to connect netlogon server")
2004 local_netlogon_trusts = local_netlogon.netr_DsrEnumerateDomainTrusts(local_server,
2005 netlogon.NETR_TRUST_FLAG_IN_FOREST |
2006 netlogon.NETR_TRUST_FLAG_OUTBOUND |
2007 netlogon.NETR_TRUST_FLAG_INBOUND)
2008 except RuntimeError as error:
2009 if self.check_runtime_error(error, self.WERR_RPC_S_PROCNUM_OUT_OF_RANGE):
2010 # TODO: we could implement a fallback to lsa.EnumTrustDom()
2011 raise CommandError("LOCAL_DC[%s]: netr_DsrEnumerateDomainTrusts not supported." % (
2013 raise self.LocalRuntimeError(self, error, "netr_DsrEnumerateDomainTrusts failed")
2015 a = local_netlogon_trusts.array
2017 if t.trust_flags & netlogon.NETR_TRUST_FLAG_PRIMARY:
2019 self.outf.write("%-14s %-15s %-19s %s\n" % (
2020 "Type[%s]" % self.netr_DomainTrust_to_type(a, t),
2021 "Transitive[%s]" % self.netr_DomainTrust_to_transitive(t),
2022 "Direction[%s]" % self.netr_DomainTrust_to_direction(t),
2023 "Name[%s]" % self.netr_DomainTrust_to_name(t)))
2026 class cmd_domain_trust_show(DomainTrustCommand):
2027 """Show trusted domain details."""
2029 synopsis = "%prog NAME [options]"
2031 takes_optiongroups = {
2032 "sambaopts": options.SambaOptions,
2033 "versionopts": options.VersionOptions,
2034 "localdcopts": LocalDCCredentialsOptions,
2040 takes_args = ["domain"]
2042 def run(self, domain, sambaopts=None, versionopts=None, localdcopts=None):
2044 local_server = self.setup_local_server(sambaopts, localdcopts)
2046 local_lsa = self.new_local_lsa_connection()
2047 except RuntimeError as error:
2048 raise self.LocalRuntimeError(self, error, "failed to connect lsa server")
2051 local_policy_access = lsa.LSA_POLICY_VIEW_LOCAL_INFORMATION
2052 (local_policy, local_lsa_info) = self.get_lsa_info(local_lsa, local_policy_access)
2053 except RuntimeError as error:
2054 raise self.LocalRuntimeError(self, error, "failed to query LSA_POLICY_INFO_DNS")
2056 self.outf.write("LocalDomain Netbios[%s] DNS[%s] SID[%s]\n" % (
2057 local_lsa_info.name.string,
2058 local_lsa_info.dns_domain.string,
2059 local_lsa_info.sid))
2061 lsaString = lsa.String()
2062 lsaString.string = domain
2064 local_tdo_full = local_lsa.QueryTrustedDomainInfoByName(local_policy,
2065 lsaString, lsa.LSA_TRUSTED_DOMAIN_INFO_FULL_INFO)
2066 local_tdo_info = local_tdo_full.info_ex
2067 local_tdo_posix = local_tdo_full.posix_offset
2068 except NTSTATUSError as error:
2069 if self.check_runtime_error(error, ntstatus.NT_STATUS_OBJECT_NAME_NOT_FOUND):
2070 raise CommandError("trusted domain object does not exist for domain [%s]" % domain)
2072 raise self.LocalRuntimeError(self, error, "QueryTrustedDomainInfoByName(FULL_INFO) failed")
2075 local_tdo_enctypes = local_lsa.QueryTrustedDomainInfoByName(local_policy,
2076 lsaString, lsa.LSA_TRUSTED_DOMAIN_SUPPORTED_ENCRYPTION_TYPES)
2077 except NTSTATUSError as error:
2078 if self.check_runtime_error(error, ntstatus.NT_STATUS_INVALID_PARAMETER):
2080 if self.check_runtime_error(error, ntstatus.NT_STATUS_INVALID_INFO_CLASS):
2083 if error is not None:
2084 raise self.LocalRuntimeError(self, error,
2085 "QueryTrustedDomainInfoByName(SUPPORTED_ENCRYPTION_TYPES) failed")
2087 local_tdo_enctypes = lsa.TrustDomainInfoSupportedEncTypes()
2088 local_tdo_enctypes.enc_types = 0
2091 local_tdo_forest = None
2092 if local_tdo_info.trust_attributes & lsa.LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE:
2093 local_tdo_forest = local_lsa.lsaRQueryForestTrustInformation(local_policy,
2094 lsaString, lsa.LSA_FOREST_TRUST_DOMAIN_INFO)
2095 except RuntimeError as error:
2096 if self.check_runtime_error(error, self.NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE):
2098 if self.check_runtime_error(error, ntstatus.NT_STATUS_NOT_FOUND):
2100 if error is not None:
2101 raise self.LocalRuntimeError(self, error, "lsaRQueryForestTrustInformation failed")
2103 local_tdo_forest = lsa.ForestTrustInformation()
2104 local_tdo_forest.count = 0
2105 local_tdo_forest.entries = []
2107 self.outf.write("TrusteDomain:\n\n");
2108 self.outf.write("NetbiosName: %s\n" % local_tdo_info.netbios_name.string)
2109 if local_tdo_info.netbios_name.string != local_tdo_info.domain_name.string:
2110 self.outf.write("DnsName: %s\n" % local_tdo_info.domain_name.string)
2111 self.outf.write("SID: %s\n" % local_tdo_info.sid)
2112 self.outf.write("Type: %s\n" % self.trustType_string(local_tdo_info.trust_type))
2113 self.outf.write("Direction: %s\n" % self.trustDirection_string(local_tdo_info.trust_direction))
2114 self.outf.write("Attributes: %s\n" % self.trustAttributes_string(local_tdo_info.trust_attributes))
2115 posix_offset_u32 = ctypes.c_uint32(local_tdo_posix.posix_offset).value
2116 posix_offset_i32 = ctypes.c_int32(local_tdo_posix.posix_offset).value
2117 self.outf.write("PosixOffset: 0x%08X (%d)\n" % (posix_offset_u32, posix_offset_i32))
2118 self.outf.write("kerb_EncTypes: %s\n" % self.kerb_EncTypes_string(local_tdo_enctypes.enc_types))
2120 if local_tdo_info.trust_attributes & lsa.LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE:
2121 self.write_forest_trust_info(local_tdo_forest,
2122 tln=local_tdo_info.domain_name.string)
2126 class cmd_domain_trust_create(DomainTrustCommand):
2127 """Create a domain or forest trust."""
2129 synopsis = "%prog DOMAIN [options]"
2131 takes_optiongroups = {
2132 "sambaopts": options.SambaOptions,
2133 "versionopts": options.VersionOptions,
2134 "credopts": options.CredentialsOptions,
2135 "localdcopts": LocalDCCredentialsOptions,
2139 Option("--type", type="choice", metavar="TYPE",
2140 choices=["external", "forest"],
2141 help="The type of the trust: 'external' or 'forest'.",
2143 default="external"),
2144 Option("--direction", type="choice", metavar="DIRECTION",
2145 choices=["incoming", "outgoing", "both"],
2146 help="The trust direction: 'incoming', 'outgoing' or 'both'.",
2147 dest='trust_direction',
2149 Option("--create-location", type="choice", metavar="LOCATION",
2150 choices=["local", "both"],
2151 help="Where to create the trusted domain object: 'local' or 'both'.",
2152 dest='create_location',
2154 Option("--cross-organisation", action="store_true",
2155 help="The related domains does not belong to the same organisation.",
2156 dest='cross_organisation',
2158 Option("--quarantined", type="choice", metavar="yes|no",
2159 choices=["yes", "no", None],
2160 help="Special SID filtering rules are applied to the trust. "
2161 "With --type=external the default is yes. "
2162 "With --type=forest the default is no.",
2163 dest='quarantined_arg',
2165 Option("--not-transitive", action="store_true",
2166 help="The forest trust is not transitive.",
2167 dest='not_transitive',
2169 Option("--treat-as-external", action="store_true",
2170 help="The treat the forest trust as external.",
2171 dest='treat_as_external',
2173 Option("--no-aes-keys", action="store_false",
2174 help="The trust uses aes kerberos keys.",
2175 dest='use_aes_keys',
2177 Option("--skip-validation", action="store_false",
2178 help="Skip validation of the trust.",
2183 takes_args = ["domain"]
2185 def run(self, domain, sambaopts=None, localdcopts=None, credopts=None, versionopts=None,
2186 trust_type=None, trust_direction=None, create_location=None,
2187 cross_organisation=False, quarantined_arg=None,
2188 not_transitive=False, treat_as_external=False,
2189 use_aes_keys=False, validate=True):
2191 lsaString = lsa.String()
2194 if quarantined_arg is None:
2195 if trust_type == 'external':
2197 elif quarantined_arg == 'yes':
2200 if trust_type != 'forest':
2202 raise CommandError("--not-transitive requires --type=forest")
2203 if treat_as_external:
2204 raise CommandError("--treat-as-external requires --type=forest")
2208 enc_types = lsa.TrustDomainInfoSupportedEncTypes()
2209 enc_types.enc_types = security.KERB_ENCTYPE_AES128_CTS_HMAC_SHA1_96
2210 enc_types.enc_types |= security.KERB_ENCTYPE_AES256_CTS_HMAC_SHA1_96
2212 local_policy_access = lsa.LSA_POLICY_VIEW_LOCAL_INFORMATION
2213 local_policy_access |= lsa.LSA_POLICY_TRUST_ADMIN
2214 local_policy_access |= lsa.LSA_POLICY_CREATE_SECRET
2216 local_trust_info = lsa.TrustDomainInfoInfoEx()
2217 local_trust_info.trust_type = lsa.LSA_TRUST_TYPE_UPLEVEL
2218 local_trust_info.trust_direction = 0
2219 if trust_direction == "both":
2220 local_trust_info.trust_direction |= lsa.LSA_TRUST_DIRECTION_INBOUND
2221 local_trust_info.trust_direction |= lsa.LSA_TRUST_DIRECTION_OUTBOUND
2222 elif trust_direction == "incoming":
2223 local_trust_info.trust_direction |= lsa.LSA_TRUST_DIRECTION_INBOUND
2224 elif trust_direction == "outgoing":
2225 local_trust_info.trust_direction |= lsa.LSA_TRUST_DIRECTION_OUTBOUND
2226 local_trust_info.trust_attributes = 0
2227 if cross_organisation:
2228 local_trust_info.trust_attributes |= lsa.LSA_TRUST_ATTRIBUTE_CROSS_ORGANIZATION
2230 local_trust_info.trust_attributes |= lsa.LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN
2231 if trust_type == "forest":
2232 local_trust_info.trust_attributes |= lsa.LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE
2234 local_trust_info.trust_attributes |= lsa.LSA_TRUST_ATTRIBUTE_NON_TRANSITIVE
2235 if treat_as_external:
2236 local_trust_info.trust_attributes |= lsa.LSA_TRUST_ATTRIBUTE_TREAT_AS_EXTERNAL
2238 def get_password(name):
2241 if password is not None and password is not '':
2243 password = getpass("New %s Password: " % name)
2244 passwordverify = getpass("Retype %s Password: " % name)
2245 if not password == passwordverify:
2247 self.outf.write("Sorry, passwords do not match.\n")
2249 incoming_secret = None
2250 outgoing_secret = None
2251 remote_policy_access = lsa.LSA_POLICY_VIEW_LOCAL_INFORMATION
2252 if create_location == "local":
2253 if local_trust_info.trust_direction & lsa.LSA_TRUST_DIRECTION_INBOUND:
2254 incoming_password = get_password("Incoming Trust")
2255 incoming_secret = string_to_byte_array(incoming_password.encode('utf-16-le'))
2256 if local_trust_info.trust_direction & lsa.LSA_TRUST_DIRECTION_OUTBOUND:
2257 outgoing_password = get_password("Outgoing Trust")
2258 outgoing_secret = string_to_byte_array(outgoing_password.encode('utf-16-le'))
2260 remote_trust_info = None
2262 # We use 240 random bytes.
2263 # Windows uses 28 or 240 random bytes. I guess it's
2264 # based on the trust type external vs. forest.
2266 # The initial trust password can be up to 512 bytes
2267 # while the versioned passwords used for periodic updates
2268 # can only be up to 498 bytes, as netr_ServerPasswordSet2()
2269 # needs to pass the NL_PASSWORD_VERSION structure within the
2270 # 512 bytes and a 2 bytes confounder is required.
2272 def random_trust_secret(length, use_aes_keys=True):
2273 secret = [0] * length
2275 pw1 = samba.generate_random_password(length/2, length/2)
2276 if not use_aes_keys:
2277 # With arcfour-hmac-md5 we have to use valid utf16
2278 # in order to generate the correct pre-auth key
2279 # based on a utf8 password.
2281 # We can remove this once our client libraries
2282 # support using the correct NTHASH.
2283 return string_to_byte_array(pw1.encode('utf-16-le'))
2285 # We mix characters from generate_random_password
2286 # with random numbers from random.randint()
2287 for i in range(len(secret)):
2289 secret[i] = ord(pw1[i])
2291 secret[i] = random.randint(0, 255)
2295 if local_trust_info.trust_direction & lsa.LSA_TRUST_DIRECTION_INBOUND:
2296 incoming_secret = random_trust_secret(240, use_aes_keys=use_aes_keys)
2297 if local_trust_info.trust_direction & lsa.LSA_TRUST_DIRECTION_OUTBOUND:
2298 outgoing_secret = random_trust_secret(240, use_aes_keys=use_aes_keys)
2300 remote_policy_access |= lsa.LSA_POLICY_TRUST_ADMIN
2301 remote_policy_access |= lsa.LSA_POLICY_CREATE_SECRET
2303 remote_trust_info = lsa.TrustDomainInfoInfoEx()
2304 remote_trust_info.trust_type = lsa.LSA_TRUST_TYPE_UPLEVEL
2305 remote_trust_info.trust_direction = 0
2306 if trust_direction == "both":
2307 remote_trust_info.trust_direction |= lsa.LSA_TRUST_DIRECTION_INBOUND
2308 remote_trust_info.trust_direction |= lsa.LSA_TRUST_DIRECTION_OUTBOUND
2309 elif trust_direction == "incoming":
2310 remote_trust_info.trust_direction |= lsa.LSA_TRUST_DIRECTION_OUTBOUND
2311 elif trust_direction == "outgoing":
2312 remote_trust_info.trust_direction |= lsa.LSA_TRUST_DIRECTION_INBOUND
2313 remote_trust_info.trust_attributes = 0
2314 if cross_organisation:
2315 remote_trust_info.trust_attributes |= lsa.LSA_TRUST_ATTRIBUTE_CROSS_ORGANIZATION
2317 remote_trust_info.trust_attributes |= lsa.LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN
2318 if trust_type == "forest":
2319 remote_trust_info.trust_attributes |= lsa.LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE
2321 remote_trust_info.trust_attributes |= lsa.LSA_TRUST_ATTRIBUTE_NON_TRANSITIVE
2322 if treat_as_external:
2323 remote_trust_info.trust_attributes |= lsa.LSA_TRUST_ATTRIBUTE_TREAT_AS_EXTERNAL
2325 local_server = self.setup_local_server(sambaopts, localdcopts)
2327 local_lsa = self.new_local_lsa_connection()
2328 except RuntimeError as error:
2329 raise self.LocalRuntimeError(self, error, "failed to connect lsa server")
2332 (local_policy, local_lsa_info) = self.get_lsa_info(local_lsa, local_policy_access)
2333 except RuntimeError as error:
2334 raise self.LocalRuntimeError(self, error, "failed to query LSA_POLICY_INFO_DNS")
2336 self.outf.write("LocalDomain Netbios[%s] DNS[%s] SID[%s]\n" % (
2337 local_lsa_info.name.string,
2338 local_lsa_info.dns_domain.string,
2339 local_lsa_info.sid))
2342 remote_server = self.setup_remote_server(credopts, domain)
2343 except RuntimeError as error:
2344 raise self.RemoteRuntimeError(self, error, "failed to locate remote server")
2347 remote_lsa = self.new_remote_lsa_connection()
2348 except RuntimeError as error:
2349 raise self.RemoteRuntimeError(self, error, "failed to connect lsa server")
2352 (remote_policy, remote_lsa_info) = self.get_lsa_info(remote_lsa, remote_policy_access)
2353 except RuntimeError as error:
2354 raise self.RemoteRuntimeError(self, error, "failed to query LSA_POLICY_INFO_DNS")
2356 self.outf.write("RemoteDomain Netbios[%s] DNS[%s] SID[%s]\n" % (
2357 remote_lsa_info.name.string,
2358 remote_lsa_info.dns_domain.string,
2359 remote_lsa_info.sid))
2361 local_trust_info.domain_name.string = remote_lsa_info.dns_domain.string
2362 local_trust_info.netbios_name.string = remote_lsa_info.name.string
2363 local_trust_info.sid = remote_lsa_info.sid
2365 if remote_trust_info:
2366 remote_trust_info.domain_name.string = local_lsa_info.dns_domain.string
2367 remote_trust_info.netbios_name.string = local_lsa_info.name.string
2368 remote_trust_info.sid = local_lsa_info.sid
2371 lsaString.string = local_trust_info.domain_name.string
2372 local_old_netbios = local_lsa.QueryTrustedDomainInfoByName(local_policy,
2373 lsaString, lsa.LSA_TRUSTED_DOMAIN_INFO_FULL_INFO)
2374 raise CommandError("TrustedDomain %s already exist'" % lsaString.string)
2375 except NTSTATUSError as error:
2376 if not self.check_runtime_error(error, ntstatus.NT_STATUS_OBJECT_NAME_NOT_FOUND):
2377 raise self.LocalRuntimeError(self, error,
2378 "QueryTrustedDomainInfoByName(%s, FULL_INFO) failed" % (
2382 lsaString.string = local_trust_info.netbios_name.string
2383 local_old_dns = local_lsa.QueryTrustedDomainInfoByName(local_policy,
2384 lsaString, lsa.LSA_TRUSTED_DOMAIN_INFO_FULL_INFO)
2385 raise CommandError("TrustedDomain %s already exist'" % lsaString.string)
2386 except NTSTATUSError as error:
2387 if not self.check_runtime_error(error, ntstatus.NT_STATUS_OBJECT_NAME_NOT_FOUND):
2388 raise self.LocalRuntimeError(self, error,
2389 "QueryTrustedDomainInfoByName(%s, FULL_INFO) failed" % (
2392 if remote_trust_info:
2394 lsaString.string = remote_trust_info.domain_name.string
2395 remote_old_netbios = remote_lsa.QueryTrustedDomainInfoByName(remote_policy,
2396 lsaString, lsa.LSA_TRUSTED_DOMAIN_INFO_FULL_INFO)
2397 raise CommandError("TrustedDomain %s already exist'" % lsaString.string)
2398 except NTSTATUSError as error:
2399 if not self.check_runtime_error(error, ntstatus.NT_STATUS_OBJECT_NAME_NOT_FOUND):
2400 raise self.RemoteRuntimeError(self, error,
2401 "QueryTrustedDomainInfoByName(%s, FULL_INFO) failed" % (
2405 lsaString.string = remote_trust_info.netbios_name.string
2406 remote_old_dns = remote_lsa.QueryTrustedDomainInfoByName(remote_policy,
2407 lsaString, lsa.LSA_TRUSTED_DOMAIN_INFO_FULL_INFO)
2408 raise CommandError("TrustedDomain %s already exist'" % lsaString.string)
2409 except NTSTATUSError as error:
2410 if not self.check_runtime_error(error, ntstatus.NT_STATUS_OBJECT_NAME_NOT_FOUND):
2411 raise self.RemoteRuntimeError(self, error,
2412 "QueryTrustedDomainInfoByName(%s, FULL_INFO) failed" % (
2416 local_netlogon = self.new_local_netlogon_connection()
2417 except RuntimeError as error:
2418 raise self.LocalRuntimeError(self, error, "failed to connect netlogon server")
2421 local_netlogon_info = self.get_netlogon_dc_info(local_netlogon, local_server)
2422 except RuntimeError as error:
2423 raise self.LocalRuntimeError(self, error, "failed to get netlogon dc info")
2425 if remote_trust_info:
2427 remote_netlogon = self.new_remote_netlogon_connection()
2428 except RuntimeError as error:
2429 raise self.RemoteRuntimeError(self, error, "failed to connect netlogon server")
2432 remote_netlogon_info = self.get_netlogon_dc_info(remote_netlogon, remote_server)
2433 except RuntimeError as error:
2434 raise self.RemoteRuntimeError(self, error, "failed to get netlogon dc info")
2436 def generate_AuthInOutBlob(secret, update_time):
2438 blob = drsblobs.trustAuthInOutBlob()
2443 clear = drsblobs.AuthInfoClear()
2444 clear.size = len(secret)
2445 clear.password = secret
2447 info = drsblobs.AuthenticationInformation()
2448 info.LastUpdateTime = samba.unix2nttime(update_time)
2449 info.AuthType = lsa.TRUST_AUTH_TYPE_CLEAR
2450 info.AuthInfo = clear
2452 array = drsblobs.AuthenticationInformationArray()
2454 array.array = [info]
2456 blob = drsblobs.trustAuthInOutBlob()
2458 blob.current = array
2462 def generate_AuthInfoInternal(session_key, incoming=None, outgoing=None):
2463 confounder = [0] * 512
2464 for i in range(len(confounder)):
2465 confounder[i] = random.randint(0, 255)
2467 trustpass = drsblobs.trustDomainPasswords()
2469 trustpass.confounder = confounder
2470 trustpass.outgoing = outgoing
2471 trustpass.incoming = incoming
2473 trustpass_blob = ndr_pack(trustpass)
2475 encrypted_trustpass = arcfour_encrypt(session_key, trustpass_blob)
2477 auth_blob = lsa.DATA_BUF2()
2478 auth_blob.size = len(encrypted_trustpass)
2479 auth_blob.data = string_to_byte_array(encrypted_trustpass)
2481 auth_info = lsa.TrustDomainInfoAuthInfoInternal()
2482 auth_info.auth_blob = auth_blob
2486 update_time = samba.current_unix_time()
2487 incoming_blob = generate_AuthInOutBlob(incoming_secret, update_time)
2488 outgoing_blob = generate_AuthInOutBlob(outgoing_secret, update_time)
2490 local_tdo_handle = None
2491 remote_tdo_handle = None
2493 local_auth_info = generate_AuthInfoInternal(local_lsa.session_key,
2494 incoming=incoming_blob,
2495 outgoing=outgoing_blob)
2496 if remote_trust_info:
2497 remote_auth_info = generate_AuthInfoInternal(remote_lsa.session_key,
2498 incoming=outgoing_blob,
2499 outgoing=incoming_blob)
2502 if remote_trust_info:
2503 self.outf.write("Creating remote TDO.\n")
2504 current_request = { "location": "remote", "name": "CreateTrustedDomainEx2"}
2505 remote_tdo_handle = remote_lsa.CreateTrustedDomainEx2(remote_policy,
2508 lsa.LSA_TRUSTED_DOMAIN_ALL_ACCESS)
2509 self.outf.write("Remote TDO created.\n")
2511 self.outf.write("Setting supported encryption types on remote TDO.\n")
2512 current_request = { "location": "remote", "name": "SetInformationTrustedDomain"}
2513 remote_lsa.SetInformationTrustedDomain(remote_tdo_handle,
2514 lsa.LSA_TRUSTED_DOMAIN_SUPPORTED_ENCRYPTION_TYPES,
2517 self.outf.write("Creating local TDO.\n")
2518 current_request = { "location": "local", "name": "CreateTrustedDomainEx2"}
2519 local_tdo_handle = local_lsa.CreateTrustedDomainEx2(local_policy,
2522 lsa.LSA_TRUSTED_DOMAIN_ALL_ACCESS)
2523 self.outf.write("Local TDO created\n")
2525 self.outf.write("Setting supported encryption types on local TDO.\n")
2526 current_request = { "location": "local", "name": "SetInformationTrustedDomain"}
2527 local_lsa.SetInformationTrustedDomain(local_tdo_handle,
2528 lsa.LSA_TRUSTED_DOMAIN_SUPPORTED_ENCRYPTION_TYPES,
2530 except RuntimeError as error:
2531 self.outf.write("Error: %s failed %sly - cleaning up\n" % (
2532 current_request['name'], current_request['location']))
2533 if remote_tdo_handle:
2534 self.outf.write("Deleting remote TDO.\n")
2535 remote_lsa.DeleteObject(remote_tdo_handle)
2536 remote_tdo_handle = None
2537 if local_tdo_handle:
2538 self.outf.write("Deleting local TDO.\n")
2539 local_lsa.DeleteObject(local_tdo_handle)
2540 local_tdo_handle = None
2541 if current_request['location'] is "remote":
2542 raise self.RemoteRuntimeError(self, error, "%s" % (
2543 current_request['name']))
2544 raise self.LocalRuntimeError(self, error, "%s" % (
2545 current_request['name']))
2548 if local_trust_info.trust_attributes & lsa.LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE:
2549 self.outf.write("Setup local forest trust information...\n")
2551 # get all information about the remote trust
2552 # this triggers netr_GetForestTrustInformation to the remote domain
2553 # and lsaRSetForestTrustInformation() locally, but new top level
2554 # names are disabled by default.
2555 local_forest_info = local_netlogon.netr_DsRGetForestTrustInformation(local_netlogon_info.dc_unc,
2556 remote_lsa_info.dns_domain.string,
2557 netlogon.DS_GFTI_UPDATE_TDO)
2558 except RuntimeError as error:
2559 raise self.LocalRuntimeError(self, error, "netr_DsRGetForestTrustInformation() failed")
2562 # here we try to enable all top level names
2563 local_forest_collision = local_lsa.lsaRSetForestTrustInformation(local_policy,
2564 remote_lsa_info.dns_domain,
2565 lsa.LSA_FOREST_TRUST_DOMAIN_INFO,
2568 except RuntimeError as error:
2569 raise self.LocalRuntimeError(self, error, "lsaRSetForestTrustInformation() failed")
2571 self.write_forest_trust_info(local_forest_info,
2572 tln=remote_lsa_info.dns_domain.string,
2573 collisions=local_forest_collision)
2575 if remote_trust_info:
2576 self.outf.write("Setup remote forest trust information...\n")
2578 # get all information about the local trust (from the perspective of the remote domain)
2579 # this triggers netr_GetForestTrustInformation to our domain.
2580 # and lsaRSetForestTrustInformation() remotely, but new top level
2581 # names are disabled by default.
2582 remote_forest_info = remote_netlogon.netr_DsRGetForestTrustInformation(remote_netlogon_info.dc_unc,
2583 local_lsa_info.dns_domain.string,
2584 netlogon.DS_GFTI_UPDATE_TDO)
2585 except RuntimeError as error:
2586 raise self.RemoteRuntimeError(self, error, "netr_DsRGetForestTrustInformation() failed")
2589 # here we try to enable all top level names
2590 remote_forest_collision = remote_lsa.lsaRSetForestTrustInformation(remote_policy,
2591 local_lsa_info.dns_domain,
2592 lsa.LSA_FOREST_TRUST_DOMAIN_INFO,
2595 except RuntimeError as error:
2596 raise self.RemoteRuntimeError(self, error, "lsaRSetForestTrustInformation() failed")
2598 self.write_forest_trust_info(remote_forest_info,
2599 tln=local_lsa_info.dns_domain.string,
2600 collisions=remote_forest_collision)
2602 if local_trust_info.trust_direction & lsa.LSA_TRUST_DIRECTION_OUTBOUND:
2603 self.outf.write("Validating outgoing trust...\n")
2605 local_trust_verify = local_netlogon.netr_LogonControl2Ex(local_netlogon_info.dc_unc,
2606 netlogon.NETLOGON_CONTROL_TC_VERIFY,
2608 remote_lsa_info.dns_domain.string)
2609 except RuntimeError as error:
2610 raise self.LocalRuntimeError(self, error, "NETLOGON_CONTROL_TC_VERIFY failed")
2612 local_trust_status = self._uint32(local_trust_verify.pdc_connection_status[0])
2613 local_conn_status = self._uint32(local_trust_verify.tc_connection_status[0])
2615 if local_trust_verify.flags & netlogon.NETLOGON_VERIFY_STATUS_RETURNED:
2616 local_validation = "LocalValidation: DC[%s] CONNECTION[%s] TRUST[%s] VERIFY_STATUS_RETURNED" % (
2617 local_trust_verify.trusted_dc_name,
2618 local_trust_verify.tc_connection_status[1],
2619 local_trust_verify.pdc_connection_status[1])
2621 local_validation = "LocalValidation: DC[%s] CONNECTION[%s] TRUST[%s]" % (
2622 local_trust_verify.trusted_dc_name,
2623 local_trust_verify.tc_connection_status[1],
2624 local_trust_verify.pdc_connection_status[1])
2626 if local_trust_status != self.WERR_OK or local_conn_status != self.WERR_OK:
2627 raise CommandError(local_validation)
2629 self.outf.write("OK: %s\n" % local_validation)
2631 if remote_trust_info:
2632 if remote_trust_info.trust_direction & lsa.LSA_TRUST_DIRECTION_OUTBOUND:
2633 self.outf.write("Validating incoming trust...\n")
2635 remote_trust_verify = remote_netlogon.netr_LogonControl2Ex(remote_netlogon_info.dc_unc,
2636 netlogon.NETLOGON_CONTROL_TC_VERIFY,
2638 local_lsa_info.dns_domain.string)
2639 except RuntimeError as error:
2640 raise self.RemoteRuntimeError(self, error, "NETLOGON_CONTROL_TC_VERIFY failed")
2642 remote_trust_status = self._uint32(remote_trust_verify.pdc_connection_status[0])
2643 remote_conn_status = self._uint32(remote_trust_verify.tc_connection_status[0])
2645 if remote_trust_verify.flags & netlogon.NETLOGON_VERIFY_STATUS_RETURNED:
2646 remote_validation = "RemoteValidation: DC[%s] CONNECTION[%s] TRUST[%s] VERIFY_STATUS_RETURNED" % (
2647 remote_trust_verify.trusted_dc_name,
2648 remote_trust_verify.tc_connection_status[1],
2649 remote_trust_verify.pdc_connection_status[1])
2651 remote_validation = "RemoteValidation: DC[%s] CONNECTION[%s] TRUST[%s]" % (
2652 remote_trust_verify.trusted_dc_name,
2653 remote_trust_verify.tc_connection_status[1],
2654 remote_trust_verify.pdc_connection_status[1])
2656 if remote_trust_status != self.WERR_OK or remote_conn_status != self.WERR_OK:
2657 raise CommandError(remote_validation)
2659 self.outf.write("OK: %s\n" % remote_validation)
2661 if remote_tdo_handle is not None:
2663 remote_lsa.Close(remote_tdo_handle)
2664 except RuntimeError as error:
2666 remote_tdo_handle = None
2667 if local_tdo_handle is not None:
2669 local_lsa.Close(local_tdo_handle)
2670 except RuntimeError as error:
2672 local_tdo_handle = None
2674 self.outf.write("Success.\n")
2677 class cmd_domain_trust_delete(DomainTrustCommand):
2678 """Delete a domain trust."""
2680 synopsis = "%prog DOMAIN [options]"
2682 takes_optiongroups = {
2683 "sambaopts": options.SambaOptions,
2684 "versionopts": options.VersionOptions,
2685 "credopts": options.CredentialsOptions,
2686 "localdcopts": LocalDCCredentialsOptions,
2690 Option("--delete-location", type="choice", metavar="LOCATION",
2691 choices=["local", "both"],
2692 help="Where to delete the trusted domain object: 'local' or 'both'.",
2693 dest='delete_location',
2697 takes_args = ["domain"]
2699 def run(self, domain, sambaopts=None, localdcopts=None, credopts=None, versionopts=None,
2700 delete_location=None):
2702 local_policy_access = lsa.LSA_POLICY_VIEW_LOCAL_INFORMATION
2703 local_policy_access |= lsa.LSA_POLICY_TRUST_ADMIN
2704 local_policy_access |= lsa.LSA_POLICY_CREATE_SECRET
2706 if delete_location == "local":
2707 remote_policy_access = None
2709 remote_policy_access = lsa.LSA_POLICY_VIEW_LOCAL_INFORMATION
2710 remote_policy_access |= lsa.LSA_POLICY_TRUST_ADMIN
2711 remote_policy_access |= lsa.LSA_POLICY_CREATE_SECRET
2713 local_server = self.setup_local_server(sambaopts, localdcopts)
2715 local_lsa = self.new_local_lsa_connection()
2716 except RuntimeError as error:
2717 raise self.LocalRuntimeError(self, error, "failed to connect lsa server")
2720 (local_policy, local_lsa_info) = self.get_lsa_info(local_lsa, local_policy_access)
2721 except RuntimeError as error:
2722 raise self.LocalRuntimeError(self, error, "failed to query LSA_POLICY_INFO_DNS")
2724 self.outf.write("LocalDomain Netbios[%s] DNS[%s] SID[%s]\n" % (
2725 local_lsa_info.name.string,
2726 local_lsa_info.dns_domain.string,
2727 local_lsa_info.sid))
2729 local_tdo_info = None
2730 local_tdo_handle = None
2731 remote_tdo_info = None
2732 remote_tdo_handle = None
2734 lsaString = lsa.String()
2736 lsaString.string = domain
2737 local_tdo_info = local_lsa.QueryTrustedDomainInfoByName(local_policy,
2738 lsaString, lsa.LSA_TRUSTED_DOMAIN_INFO_INFO_EX)
2739 except NTSTATUSError as error:
2740 if self.check_runtime_error(error, ntstatus.NT_STATUS_OBJECT_NAME_NOT_FOUND):
2741 raise CommandError("Failed to find trust for domain '%s'" % domain)
2742 raise self.RemoteRuntimeError(self, error, "failed to locate remote server")
2745 if remote_policy_access is not None:
2747 remote_server = self.setup_remote_server(credopts, domain)
2748 except RuntimeError as error:
2749 raise self.RemoteRuntimeError(self, error, "failed to locate remote server")
2752 remote_lsa = self.new_remote_lsa_connection()
2753 except RuntimeError as error:
2754 raise self.RemoteRuntimeError(self, error, "failed to connect lsa server")
2757 (remote_policy, remote_lsa_info) = self.get_lsa_info(remote_lsa, remote_policy_access)
2758 except RuntimeError as error:
2759 raise self.RemoteRuntimeError(self, error, "failed to query LSA_POLICY_INFO_DNS")
2761 self.outf.write("RemoteDomain Netbios[%s] DNS[%s] SID[%s]\n" % (
2762 remote_lsa_info.name.string,
2763 remote_lsa_info.dns_domain.string,
2764 remote_lsa_info.sid))
2766 if remote_lsa_info.sid != local_tdo_info.sid or \
2767 remote_lsa_info.name.string != local_tdo_info.netbios_name.string or \
2768 remote_lsa_info.dns_domain.string != local_tdo_info.domain_name.string:
2769 raise CommandError("LocalTDO inconsistend: Netbios[%s] DNS[%s] SID[%s]" % (
2770 local_tdo_info.netbios_name.string,
2771 local_tdo_info.domain_name.string,
2772 local_tdo_info.sid))
2775 lsaString.string = local_lsa_info.dns_domain.string
2776 remote_tdo_info = remote_lsa.QueryTrustedDomainInfoByName(remote_policy,
2777 lsaString, lsa.LSA_TRUSTED_DOMAIN_INFO_INFO_EX)
2778 except NTSTATUSError as error:
2779 if not self.check_runtime_error(error, ntstatus.NT_STATUS_OBJECT_NAME_NOT_FOUND):
2780 raise self.RemoteRuntimeError(self, error, "QueryTrustedDomainInfoByName(%s)" % (
2784 if remote_tdo_info is not None:
2785 if local_lsa_info.sid != remote_tdo_info.sid or \
2786 local_lsa_info.name.string != remote_tdo_info.netbios_name.string or \
2787 local_lsa_info.dns_domain.string != remote_tdo_info.domain_name.string:
2788 raise CommandError("RemoteTDO inconsistend: Netbios[%s] DNS[%s] SID[%s]" % (
2789 remote_tdo_info.netbios_name.string,
2790 remote_tdo_info.domain_name.string,
2791 remote_tdo_info.sid))
2793 if local_tdo_info is not None:
2795 lsaString.string = local_tdo_info.domain_name.string
2796 local_tdo_handle = local_lsa.OpenTrustedDomainByName(local_policy,
2798 security.SEC_STD_DELETE)
2799 except RuntimeError as error:
2800 raise self.LocalRuntimeError(self, error, "OpenTrustedDomainByName(%s)" % (
2803 local_lsa.DeleteObject(local_tdo_handle)
2804 local_tdo_handle = None
2806 if remote_tdo_info is not None:
2808 lsaString.string = remote_tdo_info.domain_name.string
2809 remote_tdo_handle = remote_lsa.OpenTrustedDomainByName(remote_policy,
2811 security.SEC_STD_DELETE)
2812 except RuntimeError as error:
2813 raise self.RemoteRuntimeError(self, error, "OpenTrustedDomainByName(%s)" % (
2816 if remote_tdo_handle is not None:
2818 remote_lsa.DeleteObject(remote_tdo_handle)
2819 remote_tdo_handle = None
2820 self.outf.write("RemoteTDO deleted.\n")
2821 except RuntimeError as error:
2822 self.outf.write("%s\n" % self.RemoteRuntimeError(self, error, "DeleteObject() failed"))
2824 if local_tdo_handle is not None:
2826 local_lsa.DeleteObject(local_tdo_handle)
2827 local_tdo_handle = None
2828 self.outf.write("LocalTDO deleted.\n")
2829 except RuntimeError as error:
2830 self.outf.write("%s\n" % self.LocalRuntimeError(self, error, "DeleteObject() failed"))
2834 class cmd_domain_trust_validate(DomainTrustCommand):
2835 """Validate a domain trust."""
2837 synopsis = "%prog DOMAIN [options]"
2839 takes_optiongroups = {
2840 "sambaopts": options.SambaOptions,
2841 "versionopts": options.VersionOptions,
2842 "credopts": options.CredentialsOptions,
2843 "localdcopts": LocalDCCredentialsOptions,
2847 Option("--validate-location", type="choice", metavar="LOCATION",
2848 choices=["local", "both"],
2849 help="Where to validate the trusted domain object: 'local' or 'both'.",
2850 dest='validate_location',
2854 takes_args = ["domain"]
2856 def run(self, domain, sambaopts=None, versionopts=None, credopts=None, localdcopts=None,
2857 validate_location=None):
2859 local_policy_access = lsa.LSA_POLICY_VIEW_LOCAL_INFORMATION
2861 local_server = self.setup_local_server(sambaopts, localdcopts)
2863 local_lsa = self.new_local_lsa_connection()
2864 except RuntimeError as error:
2865 raise self.LocalRuntimeError(self, error, "failed to connect lsa server")
2868 (local_policy, local_lsa_info) = self.get_lsa_info(local_lsa, local_policy_access)
2869 except RuntimeError as error:
2870 raise self.LocalRuntimeError(self, error, "failed to query LSA_POLICY_INFO_DNS")
2872 self.outf.write("LocalDomain Netbios[%s] DNS[%s] SID[%s]\n" % (
2873 local_lsa_info.name.string,
2874 local_lsa_info.dns_domain.string,
2875 local_lsa_info.sid))
2878 lsaString = lsa.String()
2879 lsaString.string = domain
2880 local_tdo_info = local_lsa.QueryTrustedDomainInfoByName(local_policy,
2881 lsaString, lsa.LSA_TRUSTED_DOMAIN_INFO_INFO_EX)
2882 except NTSTATUSError as error:
2883 if self.check_runtime_error(error, ntstatus.NT_STATUS_OBJECT_NAME_NOT_FOUND):
2884 raise CommandError("trusted domain object does not exist for domain [%s]" % domain)
2886 raise self.LocalRuntimeError(self, error, "QueryTrustedDomainInfoByName(INFO_EX) failed")
2888 self.outf.write("LocalTDO Netbios[%s] DNS[%s] SID[%s]\n" % (
2889 local_tdo_info.netbios_name.string,
2890 local_tdo_info.domain_name.string,
2891 local_tdo_info.sid))
2894 local_netlogon = self.new_local_netlogon_connection()
2895 except RuntimeError as error:
2896 raise self.LocalRuntimeError(self, error, "failed to connect netlogon server")
2899 local_trust_verify = local_netlogon.netr_LogonControl2Ex(local_server,
2900 netlogon.NETLOGON_CONTROL_TC_VERIFY,
2902 local_tdo_info.domain_name.string)
2903 except RuntimeError as error:
2904 raise self.LocalRuntimeError(self, error, "NETLOGON_CONTROL_TC_VERIFY failed")
2906 local_trust_status = self._uint32(local_trust_verify.pdc_connection_status[0])
2907 local_conn_status = self._uint32(local_trust_verify.tc_connection_status[0])
2909 if local_trust_verify.flags & netlogon.NETLOGON_VERIFY_STATUS_RETURNED:
2910 local_validation = "LocalValidation: DC[%s] CONNECTION[%s] TRUST[%s] VERIFY_STATUS_RETURNED" % (
2911 local_trust_verify.trusted_dc_name,
2912 local_trust_verify.tc_connection_status[1],
2913 local_trust_verify.pdc_connection_status[1])
2915 local_validation = "LocalValidation: DC[%s] CONNECTION[%s] TRUST[%s]" % (
2916 local_trust_verify.trusted_dc_name,
2917 local_trust_verify.tc_connection_status[1],
2918 local_trust_verify.pdc_connection_status[1])
2920 if local_trust_status != self.WERR_OK or local_conn_status != self.WERR_OK:
2921 raise CommandError(local_validation)
2923 self.outf.write("OK: %s\n" % local_validation)
2926 server = local_trust_verify.trusted_dc_name.replace('\\', '')
2927 domain_and_server = "%s\\%s" % (local_tdo_info.domain_name.string, server)
2928 local_trust_rediscover = local_netlogon.netr_LogonControl2Ex(local_server,
2929 netlogon.NETLOGON_CONTROL_REDISCOVER,
2932 except RuntimeError as error:
2933 raise self.LocalRuntimeError(self, error, "NETLOGON_CONTROL_REDISCOVER failed")
2935 local_conn_status = self._uint32(local_trust_rediscover.tc_connection_status[0])
2936 local_rediscover = "LocalRediscover: DC[%s] CONNECTION[%s]" % (
2937 local_trust_rediscover.trusted_dc_name,
2938 local_trust_rediscover.tc_connection_status[1])
2940 if local_conn_status != self.WERR_OK:
2941 raise CommandError(local_rediscover)
2943 self.outf.write("OK: %s\n" % local_rediscover)
2945 if validate_location != "local":
2947 remote_server = self.setup_remote_server(credopts, domain, require_pdc=False)
2948 except RuntimeError as error:
2949 raise self.RemoteRuntimeError(self, error, "failed to locate remote server")
2952 remote_netlogon = self.new_remote_netlogon_connection()
2953 except RuntimeError as error:
2954 raise self.RemoteRuntimeError(self, error, "failed to connect netlogon server")
2957 remote_trust_verify = remote_netlogon.netr_LogonControl2Ex(remote_server,
2958 netlogon.NETLOGON_CONTROL_TC_VERIFY,
2960 local_lsa_info.dns_domain.string)
2961 except RuntimeError as error:
2962 raise self.RemoteRuntimeError(self, error, "NETLOGON_CONTROL_TC_VERIFY failed")
2964 remote_trust_status = self._uint32(remote_trust_verify.pdc_connection_status[0])
2965 remote_conn_status = self._uint32(remote_trust_verify.tc_connection_status[0])
2967 if remote_trust_verify.flags & netlogon.NETLOGON_VERIFY_STATUS_RETURNED:
2968 remote_validation = "RemoteValidation: DC[%s] CONNECTION[%s] TRUST[%s] VERIFY_STATUS_RETURNED" % (
2969 remote_trust_verify.trusted_dc_name,
2970 remote_trust_verify.tc_connection_status[1],
2971 remote_trust_verify.pdc_connection_status[1])
2973 remote_validation = "RemoteValidation: DC[%s] CONNECTION[%s] TRUST[%s]" % (
2974 remote_trust_verify.trusted_dc_name,
2975 remote_trust_verify.tc_connection_status[1],
2976 remote_trust_verify.pdc_connection_status[1])
2978 if remote_trust_status != self.WERR_OK or remote_conn_status != self.WERR_OK:
2979 raise CommandError(remote_validation)
2981 self.outf.write("OK: %s\n" % remote_validation)
2984 server = remote_trust_verify.trusted_dc_name.replace('\\', '')
2985 domain_and_server = "%s\\%s" % (local_lsa_info.dns_domain.string, server)
2986 remote_trust_rediscover = remote_netlogon.netr_LogonControl2Ex(remote_server,
2987 netlogon.NETLOGON_CONTROL_REDISCOVER,
2990 except RuntimeError as error:
2991 raise self.RemoteRuntimeError(self, error, "NETLOGON_CONTROL_REDISCOVER failed")
2993 remote_conn_status = self._uint32(remote_trust_rediscover.tc_connection_status[0])
2995 remote_rediscover = "RemoteRediscover: DC[%s] CONNECTION[%s]" % (
2996 remote_trust_rediscover.trusted_dc_name,
2997 remote_trust_rediscover.tc_connection_status[1])
2999 if remote_conn_status != self.WERR_OK:
3000 raise CommandError(remote_rediscover)
3002 self.outf.write("OK: %s\n" % remote_rediscover)
3006 class cmd_domain_trust_namespaces(DomainTrustCommand):
3007 """Manage forest trust namespaces."""
3009 synopsis = "%prog [DOMAIN] [options]"
3011 takes_optiongroups = {
3012 "sambaopts": options.SambaOptions,
3013 "versionopts": options.VersionOptions,
3014 "localdcopts": LocalDCCredentialsOptions,
3018 Option("--refresh", type="choice", metavar="check|store",
3019 choices=["check", "store", None],
3020 help="List and maybe store refreshed forest trust information: 'check' or 'store'.",
3023 Option("--enable-all", action="store_true",
3024 help="Try to update disabled entries, not allowed with --refresh=check.",
3027 Option("--enable-tln", action="append", metavar='DNSDOMAIN',
3028 help="Enable a top level name entry. Can be specified multiple times.",
3031 Option("--disable-tln", action="append", metavar='DNSDOMAIN',
3032 help="Disable a top level name entry. Can be specified multiple times.",
3035 Option("--add-tln-ex", action="append", metavar='DNSDOMAIN',
3036 help="Add a top level exclusion entry. Can be specified multiple times.",
3039 Option("--delete-tln-ex", action="append", metavar='DNSDOMAIN',
3040 help="Delete a top level exclusion entry. Can be specified multiple times.",
3041 dest='delete_tln_ex',
3043 Option("--enable-nb", action="append", metavar='NETBIOSDOMAIN',
3044 help="Enable a netbios name in a domain entry. Can be specified multiple times.",
3047 Option("--disable-nb", action="append", metavar='NETBIOSDOMAIN',
3048 help="Disable a netbios name in a domain entry. Can be specified multiple times.",
3051 Option("--enable-sid", action="append", metavar='DOMAINSID',
3052 help="Enable a SID in a domain entry. Can be specified multiple times.",
3053 dest='enable_sid_str',
3055 Option("--disable-sid", action="append", metavar='DOMAINSID',
3056 help="Disable a SID in a domain entry. Can be specified multiple times.",
3057 dest='disable_sid_str',
3059 Option("--add-upn-suffix", action="append", metavar='DNSDOMAIN',
3060 help="Add a new uPNSuffixes attribute for the local forest. Can be specified multiple times.",
3063 Option("--delete-upn-suffix", action="append", metavar='DNSDOMAIN',
3064 help="Delete an existing uPNSuffixes attribute of the local forest. Can be specified multiple times.",
3067 Option("--add-spn-suffix", action="append", metavar='DNSDOMAIN',
3068 help="Add a new msDS-SPNSuffixes attribute for the local forest. Can be specified multiple times.",
3071 Option("--delete-spn-suffix", action="append", metavar='DNSDOMAIN',
3072 help="Delete an existing msDS-SPNSuffixes attribute of the local forest. Can be specified multiple times.",
3077 takes_args = ["domain?"]
3079 def run(self, domain=None, sambaopts=None, localdcopts=None, versionopts=None,
3080 refresh=None, enable_all=False,
3081 enable_tln=[], disable_tln=[], add_tln_ex=[], delete_tln_ex=[],
3082 enable_sid_str=[], disable_sid_str=[], enable_nb=[], disable_nb=[],
3083 add_upn=[], delete_upn=[], add_spn=[], delete_spn=[]):
3085 require_update = False
3088 if refresh == "store":
3089 raise CommandError("--refresh=%s not allowed without DOMAIN" % refresh)
3092 raise CommandError("--enable-all not allowed without DOMAIN")
3094 if len(enable_tln) > 0:
3095 raise CommandError("--enable-tln not allowed without DOMAIN")
3096 if len(disable_tln) > 0:
3097 raise CommandError("--disable-tln not allowed without DOMAIN")
3099 if len(add_tln_ex) > 0:
3100 raise CommandError("--add-tln-ex not allowed without DOMAIN")
3101 if len(delete_tln_ex) > 0:
3102 raise CommandError("--delete-tln-ex not allowed without DOMAIN")
3104 if len(enable_nb) > 0:
3105 raise CommandError("--enable-nb not allowed without DOMAIN")
3106 if len(disable_nb) > 0:
3107 raise CommandError("--disable-nb not allowed without DOMAIN")
3109 if len(enable_sid_str) > 0:
3110 raise CommandError("--enable-sid not allowed without DOMAIN")
3111 if len(disable_sid_str) > 0:
3112 raise CommandError("--disable-sid not allowed without DOMAIN")
3114 if len(add_upn) > 0:
3116 if not n.startswith("*."):
3118 raise CommandError("value[%s] specified for --add-upn-suffix should not include with '*.'" % n)
3119 require_update = True
3120 if len(delete_upn) > 0:
3121 for n in delete_upn:
3122 if not n.startswith("*."):
3124 raise CommandError("value[%s] specified for --delete-upn-suffix should not include with '*.'" % n)
3125 require_update = True
3127 for d in delete_upn:
3128 if a.lower() != d.lower():
3130 raise CommandError("value[%s] specified for --add-upn-suffix and --delete-upn-suffix" % a)
3132 if len(add_spn) > 0:
3134 if not n.startswith("*."):
3136 raise CommandError("value[%s] specified for --add-spn-suffix should not include with '*.'" % n)
3137 require_update = True
3138 if len(delete_spn) > 0:
3139 for n in delete_spn:
3140 if not n.startswith("*."):
3142 raise CommandError("value[%s] specified for --delete-spn-suffix should not include with '*.'" % n)
3143 require_update = True
3145 for d in delete_spn:
3146 if a.lower() != d.lower():
3148 raise CommandError("value[%s] specified for --add-spn-suffix and --delete-spn-suffix" % a)
3150 if len(add_upn) > 0:
3151 raise CommandError("--add-upn-suffix not allowed together with DOMAIN")
3152 if len(delete_upn) > 0:
3153 raise CommandError("--delete-upn-suffix not allowed together with DOMAIN")
3154 if len(add_spn) > 0:
3155 raise CommandError("--add-spn-suffix not allowed together with DOMAIN")
3156 if len(delete_spn) > 0:
3157 raise CommandError("--delete-spn-suffix not allowed together with DOMAIN")
3159 if refresh is not None:
3160 if refresh == "store":
3161 require_update = True
3163 if enable_all and refresh != "store":
3164 raise CommandError("--enable-all not allowed together with --refresh=%s" % refresh)
3166 if len(enable_tln) > 0:
3167 raise CommandError("--enable-tln not allowed together with --refresh")
3168 if len(disable_tln) > 0:
3169 raise CommandError("--disable-tln not allowed together with --refresh")
3171 if len(add_tln_ex) > 0:
3172 raise CommandError("--add-tln-ex not allowed together with --refresh")
3173 if len(delete_tln_ex) > 0:
3174 raise CommandError("--delete-tln-ex not allowed together with --refresh")
3176 if len(enable_nb) > 0:
3177 raise CommandError("--enable-nb not allowed together with --refresh")
3178 if len(disable_nb) > 0:
3179 raise CommandError("--disable-nb not allowed together with --refresh")
3181 if len(enable_sid_str) > 0:
3182 raise CommandError("--enable-sid not allowed together with --refresh")
3183 if len(disable_sid_str) > 0:
3184 raise CommandError("--disable-sid not allowed together with --refresh")
3187 require_update = True
3189 if len(enable_tln) > 0:
3190 raise CommandError("--enable-tln not allowed together with --enable-all")
3192 if len(enable_nb) > 0:
3193 raise CommandError("--enable-nb not allowed together with --enable-all")
3195 if len(enable_sid) > 0:
3196 raise CommandError("--enable-sid not allowed together with --enable-all")
3198 if len(enable_tln) > 0:
3199 require_update = True
3200 if len(disable_tln) > 0:
3201 require_update = True
3202 for e in enable_tln:
3203 for d in disable_tln:
3204 if e.lower() != d.lower():
3206 raise CommandError("value[%s] specified for --enable-tln and --disable-tln" % e)
3208 if len(add_tln_ex) > 0:
3209 for n in add_tln_ex:
3210 if not n.startswith("*."):
3212 raise CommandError("value[%s] specified for --add-tln-ex should not include with '*.'" % n)
3213 require_update = True
3214 if len(delete_tln_ex) > 0:
3215 for n in delete_tln_ex:
3216 if not n.startswith("*."):
3218 raise CommandError("value[%s] specified for --delete-tln-ex should not include with '*.'" % n)
3219 require_update = True
3220 for a in add_tln_ex:
3221 for d in delete_tln_ex:
3222 if a.lower() != d.lower():
3224 raise CommandError("value[%s] specified for --add-tln-ex and --delete-tln-ex" % a)
3226 if len(enable_nb) > 0:
3227 require_update = True
3228 if len(disable_nb) > 0:
3229 require_update = True
3231 for d in disable_nb:
3232 if e.upper() != d.upper():
3234 raise CommandError("value[%s] specified for --enable-nb and --disable-nb" % e)
3237 for s in enable_sid_str:
3239 sid = security.dom_sid(s)
3240 except TypeError as error:
3241 raise CommandError("value[%s] specified for --enable-sid is not a valid SID" % s)
3242 enable_sid.append(sid)
3244 for s in disable_sid_str:
3246 sid = security.dom_sid(s)
3247 except TypeError as error:
3248 raise CommandError("value[%s] specified for --disable-sid is not a valid SID" % s)
3249 disable_sid.append(sid)
3250 if len(enable_sid) > 0:
3251 require_update = True
3252 if len(disable_sid) > 0:
3253 require_update = True
3254 for e in enable_sid:
3255 for d in disable_sid:
3258 raise CommandError("value[%s] specified for --enable-sid and --disable-sid" % e)
3260 local_policy_access = lsa.LSA_POLICY_VIEW_LOCAL_INFORMATION
3262 local_policy_access |= lsa.LSA_POLICY_TRUST_ADMIN
3264 local_server = self.setup_local_server(sambaopts, localdcopts)
3266 local_lsa = self.new_local_lsa_connection()
3267 except RuntimeError as error:
3268 raise self.LocalRuntimeError(self, error, "failed to connect lsa server")
3271 (local_policy, local_lsa_info) = self.get_lsa_info(local_lsa, local_policy_access)
3272 except RuntimeError as error:
3273 raise self.LocalRuntimeError(self, error, "failed to query LSA_POLICY_INFO_DNS")
3275 self.outf.write("LocalDomain Netbios[%s] DNS[%s] SID[%s]\n" % (
3276 local_lsa_info.name.string,
3277 local_lsa_info.dns_domain.string,
3278 local_lsa_info.sid))
3282 local_netlogon = self.new_local_netlogon_connection()
3283 except RuntimeError as error:
3284 raise self.LocalRuntimeError(self, error, "failed to connect netlogon server")
3287 local_netlogon_info = self.get_netlogon_dc_info(local_netlogon, local_server)
3288 except RuntimeError as error:
3289 raise self.LocalRuntimeError(self, error, "failed to get netlogon dc info")
3291 if local_netlogon_info.domain_name != local_netlogon_info.forest_name:
3292 raise CommandError("The local domain [%s] is not the forest root [%s]" % (
3293 local_netlogon_info.domain_name,
3294 local_netlogon_info.forest_name))
3297 # get all information about our own forest
3298 own_forest_info = local_netlogon.netr_DsRGetForestTrustInformation(local_netlogon_info.dc_unc,
3300 except RuntimeError as error:
3301 if self.check_runtime_error(error, self.WERR_RPC_S_PROCNUM_OUT_OF_RANGE):
3302 raise CommandError("LOCAL_DC[%s]: netr_DsRGetForestTrustInformation() not supported." % (
3305 if self.check_runtime_error(error, self.WERR_INVALID_FUNCTION):
3306 raise CommandError("LOCAL_DC[%s]: netr_DsRGetForestTrustInformation() not supported." % (
3309 if self.check_runtime_error(error, self.WERR_NERR_ACFNOTLOADED):
3310 raise CommandError("LOCAL_DC[%s]: netr_DsRGetForestTrustInformation() not supported." % (
3313 raise self.LocalRuntimeError(self, error, "netr_DsRGetForestTrustInformation() failed")
3315 self.outf.write("Own forest trust information...\n")
3316 self.write_forest_trust_info(own_forest_info,
3317 tln=local_lsa_info.dns_domain.string)
3320 local_samdb = self.new_local_ldap_connection()
3321 except RuntimeError as error:
3322 raise self.LocalRuntimeError(self, error, "failed to connect to SamDB")
3324 local_partitions_dn = "CN=Partitions,%s" % str(local_samdb.get_config_basedn())
3325 attrs = ['uPNSuffixes', 'msDS-SPNSuffixes']
3327 msgs = local_samdb.search(base=local_partitions_dn,
3328 scope=ldb.SCOPE_BASE,
3329 expression="(objectClass=crossRefContainer)",
3331 stored_msg = msgs[0]
3332 except ldb.LdbError as error:
3333 raise self.LocalLdbError(self, error, "failed to search partition dn")
3335 stored_upn_vals = []
3336 if 'uPNSuffixes' in stored_msg:
3337 stored_upn_vals.extend(stored_msg['uPNSuffixes'])
3339 stored_spn_vals = []
3340 if 'msDS-SPNSuffixes' in stored_msg:
3341 stored_spn_vals.extend(stored_msg['msDS-SPNSuffixes'])
3343 self.outf.write("Stored uPNSuffixes attributes[%d]:\n" % len(stored_upn_vals))
3344 for v in stored_upn_vals:
3345 self.outf.write("TLN: %-32s DNS[*.%s]\n" % ("", v))
3346 self.outf.write("Stored msDS-SPNSuffixes attributes[%d]:\n" % len(stored_spn_vals))
3347 for v in stored_spn_vals:
3348 self.outf.write("TLN: %-32s DNS[*.%s]\n" % ("", v))
3350 if not require_update:
3354 update_upn_vals = []
3355 update_upn_vals.extend(stored_upn_vals)
3358 update_spn_vals = []
3359 update_spn_vals.extend(stored_spn_vals)
3363 for i in xrange(0, len(update_upn_vals)):
3364 v = update_upn_vals[i]
3365 if v.lower() != upn.lower():
3370 raise CommandError("Entry already present for value[%s] specified for --add-upn-suffix" % upn)
3371 update_upn_vals.append(upn)
3374 for upn in delete_upn:
3376 for i in xrange(0, len(update_upn_vals)):
3377 v = update_upn_vals[i]
3378 if v.lower() != upn.lower():
3383 raise CommandError("Entry not found for value[%s] specified for --delete-upn-suffix" % upn)
3385 update_upn_vals.pop(idx)
3390 for i in xrange(0, len(update_spn_vals)):
3391 v = update_spn_vals[i]
3392 if v.lower() != spn.lower():
3397 raise CommandError("Entry already present for value[%s] specified for --add-spn-suffix" % spn)
3398 update_spn_vals.append(spn)
3401 for spn in delete_spn:
3403 for i in xrange(0, len(update_spn_vals)):
3404 v = update_spn_vals[i]
3405 if v.lower() != spn.lower():
3410 raise CommandError("Entry not found for value[%s] specified for --delete-spn-suffix" % spn)
3412 update_spn_vals.pop(idx)
3415 self.outf.write("Update uPNSuffixes attributes[%d]:\n" % len(update_upn_vals))
3416 for v in update_upn_vals:
3417 self.outf.write("TLN: %-32s DNS[*.%s]\n" % ("", v))
3418 self.outf.write("Update msDS-SPNSuffixes attributes[%d]:\n" % len(update_spn_vals))
3419 for v in update_spn_vals:
3420 self.outf.write("TLN: %-32s DNS[*.%s]\n" % ("", v))
3422 update_msg = ldb.Message()
3423 update_msg.dn = stored_msg.dn
3426 update_msg['uPNSuffixes'] = ldb.MessageElement(update_upn_vals,
3427 ldb.FLAG_MOD_REPLACE,
3430 update_msg['msDS-SPNSuffixes'] = ldb.MessageElement(update_spn_vals,
3431 ldb.FLAG_MOD_REPLACE,
3434 local_samdb.modify(update_msg)
3435 except ldb.LdbError as error:
3436 raise self.LocalLdbError(self, error, "failed to update partition dn")
3439 stored_forest_info = local_netlogon.netr_DsRGetForestTrustInformation(local_netlogon_info.dc_unc,
3441 except RuntimeError as error:
3442 raise self.LocalRuntimeError(self, error, "netr_DsRGetForestTrustInformation() failed")
3444 self.outf.write("Stored forest trust information...\n")
3445 self.write_forest_trust_info(stored_forest_info,
3446 tln=local_lsa_info.dns_domain.string)
3450 lsaString = lsa.String()
3451 lsaString.string = domain
3452 local_tdo_info = local_lsa.QueryTrustedDomainInfoByName(local_policy,
3453 lsaString, lsa.LSA_TRUSTED_DOMAIN_INFO_INFO_EX)
3454 except NTSTATUSError as error:
3455 if self.check_runtime_error(error, ntstatus.NT_STATUS_OBJECT_NAME_NOT_FOUND):
3456 raise CommandError("trusted domain object does not exist for domain [%s]" % domain)
3458 raise self.LocalRuntimeError(self, error, "QueryTrustedDomainInfoByName(INFO_EX) failed")
3460 self.outf.write("LocalTDO Netbios[%s] DNS[%s] SID[%s]\n" % (
3461 local_tdo_info.netbios_name.string,
3462 local_tdo_info.domain_name.string,
3463 local_tdo_info.sid))
3465 if not local_tdo_info.trust_attributes & lsa.LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE:
3466 raise CommandError("trusted domain object for domain [%s] is not marked as FOREST_TRANSITIVE." % domain)
3468 if refresh is not None:
3470 local_netlogon = self.new_local_netlogon_connection()
3471 except RuntimeError as error:
3472 raise self.LocalRuntimeError(self, error, "failed to connect netlogon server")
3475 local_netlogon_info = self.get_netlogon_dc_info(local_netlogon, local_server)
3476 except RuntimeError as error:
3477 raise self.LocalRuntimeError(self, error, "failed to get netlogon dc info")
3479 lsa_update_check = 1
3480 if refresh == "store":
3481 netlogon_update_tdo = netlogon.DS_GFTI_UPDATE_TDO
3483 lsa_update_check = 0
3485 netlogon_update_tdo = 0
3488 # get all information about the remote trust
3489 # this triggers netr_GetForestTrustInformation to the remote domain
3490 # and lsaRSetForestTrustInformation() locally, but new top level
3491 # names are disabled by default.
3492 fresh_forest_info = local_netlogon.netr_DsRGetForestTrustInformation(local_netlogon_info.dc_unc,
3493 local_tdo_info.domain_name.string,
3494 netlogon_update_tdo)
3495 except RuntimeError as error:
3496 raise self.LocalRuntimeError(self, error, "netr_DsRGetForestTrustInformation() failed")
3499 fresh_forest_collision = local_lsa.lsaRSetForestTrustInformation(local_policy,
3500 local_tdo_info.domain_name,
3501 lsa.LSA_FOREST_TRUST_DOMAIN_INFO,
3504 except RuntimeError as error:
3505 raise self.LocalRuntimeError(self, error, "lsaRSetForestTrustInformation() failed")
3507 self.outf.write("Fresh forest trust information...\n")
3508 self.write_forest_trust_info(fresh_forest_info,
3509 tln=local_tdo_info.domain_name.string,
3510 collisions=fresh_forest_collision)
3512 if refresh == "store":
3514 lsaString = lsa.String()
3515 lsaString.string = local_tdo_info.domain_name.string
3516 stored_forest_info = local_lsa.lsaRQueryForestTrustInformation(local_policy,
3518 lsa.LSA_FOREST_TRUST_DOMAIN_INFO)
3519 except RuntimeError as error:
3520 raise self.LocalRuntimeError(self, error, "lsaRQueryForestTrustInformation() failed")
3522 self.outf.write("Stored forest trust information...\n")
3523 self.write_forest_trust_info(stored_forest_info,
3524 tln=local_tdo_info.domain_name.string)
3529 # The none --refresh path
3533 lsaString = lsa.String()
3534 lsaString.string = local_tdo_info.domain_name.string
3535 local_forest_info = local_lsa.lsaRQueryForestTrustInformation(local_policy,
3537 lsa.LSA_FOREST_TRUST_DOMAIN_INFO)
3538 except RuntimeError as error:
3539 raise self.LocalRuntimeError(self, error, "lsaRQueryForestTrustInformation() failed")
3541 self.outf.write("Local forest trust information...\n")
3542 self.write_forest_trust_info(local_forest_info,
3543 tln=local_tdo_info.domain_name.string)
3545 if not require_update:
3549 entries.extend(local_forest_info.entries)
3550 update_forest_info = lsa.ForestTrustInformation()
3551 update_forest_info.count = len(entries)
3552 update_forest_info.entries = entries
3555 for i in xrange(0, len(update_forest_info.entries)):
3556 r = update_forest_info.entries[i]
3557 if r.type != lsa.LSA_FOREST_TRUST_TOP_LEVEL_NAME:
3559 if update_forest_info.entries[i].flags == 0:
3561 update_forest_info.entries[i].time = 0
3562 update_forest_info.entries[i].flags &= ~lsa.LSA_TLN_DISABLED_MASK
3563 for i in xrange(0, len(update_forest_info.entries)):
3564 r = update_forest_info.entries[i]
3565 if r.type != lsa.LSA_FOREST_TRUST_DOMAIN_INFO:
3567 if update_forest_info.entries[i].flags == 0:
3569 update_forest_info.entries[i].time = 0
3570 update_forest_info.entries[i].flags &= ~lsa.LSA_NB_DISABLED_MASK
3571 update_forest_info.entries[i].flags &= ~lsa.LSA_SID_DISABLED_MASK
3573 for tln in enable_tln:
3575 for i in xrange(0, len(update_forest_info.entries)):
3576 r = update_forest_info.entries[i]
3577 if r.type != lsa.LSA_FOREST_TRUST_TOP_LEVEL_NAME:
3579 if r.forest_trust_data.string.lower() != tln.lower():
3584 raise CommandError("Entry not found for value[%s] specified for --enable-tln" % tln)
3585 if not update_forest_info.entries[idx].flags & lsa.LSA_TLN_DISABLED_MASK:
3586 raise CommandError("Entry found for value[%s] specified for --enable-tln is already enabled" % tln)
3587 update_forest_info.entries[idx].time = 0
3588 update_forest_info.entries[idx].flags &= ~lsa.LSA_TLN_DISABLED_MASK
3590 for tln in disable_tln:
3592 for i in xrange(0, len(update_forest_info.entries)):
3593 r = update_forest_info.entries[i]
3594 if r.type != lsa.LSA_FOREST_TRUST_TOP_LEVEL_NAME:
3596 if r.forest_trust_data.string.lower() != tln.lower():
3601 raise CommandError("Entry not found for value[%s] specified for --disable-tln" % tln)
3602 if update_forest_info.entries[idx].flags & lsa.LSA_TLN_DISABLED_ADMIN:
3603 raise CommandError("Entry found for value[%s] specified for --disable-tln is already disabled" % tln)
3604 update_forest_info.entries[idx].time = 0
3605 update_forest_info.entries[idx].flags &= ~lsa.LSA_TLN_DISABLED_MASK
3606 update_forest_info.entries[idx].flags |= lsa.LSA_TLN_DISABLED_ADMIN
3608 for tln_ex in add_tln_ex:
3610 for i in xrange(0, len(update_forest_info.entries)):
3611 r = update_forest_info.entries[i]
3612 if r.type != lsa.LSA_FOREST_TRUST_TOP_LEVEL_NAME_EX:
3614 if r.forest_trust_data.string.lower() != tln_ex.lower():
3619 raise CommandError("Entry already present for value[%s] specified for --add-tln-ex" % tln_ex)
3621 tln_dot = ".%s" % tln_ex.lower()
3623 for i in xrange(0, len(update_forest_info.entries)):
3624 r = update_forest_info.entries[i]
3625 if r.type != lsa.LSA_FOREST_TRUST_TOP_LEVEL_NAME:
3627 r_dot = ".%s" % r.forest_trust_data.string.lower()
3628 if tln_dot == r_dot:
3629 raise CommandError("TLN entry present for value[%s] specified for --add-tln-ex" % tln_ex)
3630 if not tln_dot.endswith(r_dot):
3636 raise CommandError("No TLN parent present for value[%s] specified for --add-tln-ex" % tln_ex)
3638 r = lsa.ForestTrustRecord()
3639 r.type = lsa.LSA_FOREST_TRUST_TOP_LEVEL_NAME_EX
3642 r.forest_trust_data.string = tln_ex
3645 entries.extend(update_forest_info.entries)
3646 entries.insert(idx + 1, r)
3647 update_forest_info.count = len(entries)
3648 update_forest_info.entries = entries
3650 for tln_ex in delete_tln_ex:
3652 for i in xrange(0, len(update_forest_info.entries)):
3653 r = update_forest_info.entries[i]
3654 if r.type != lsa.LSA_FOREST_TRUST_TOP_LEVEL_NAME_EX:
3656 if r.forest_trust_data.string.lower() != tln_ex.lower():
3661 raise CommandError("Entry not found for value[%s] specified for --delete-tln-ex" % tln_ex)
3664 entries.extend(update_forest_info.entries)
3666 update_forest_info.count = len(entries)
3667 update_forest_info.entries = entries
3669 for nb in enable_nb:
3671 for i in xrange(0, len(update_forest_info.entries)):
3672 r = update_forest_info.entries[i]
3673 if r.type != lsa.LSA_FOREST_TRUST_DOMAIN_INFO:
3675 if r.forest_trust_data.netbios_domain_name.string.upper() != nb.upper():
3680 raise CommandError("Entry not found for value[%s] specified for --enable-nb" % nb)
3681 if not update_forest_info.entries[idx].flags & lsa.LSA_NB_DISABLED_MASK:
3682 raise CommandError("Entry found for value[%s] specified for --enable-nb is already enabled" % nb)
3683 update_forest_info.entries[idx].time = 0
3684 update_forest_info.entries[idx].flags &= ~lsa.LSA_NB_DISABLED_MASK
3686 for nb in disable_nb:
3688 for i in xrange(0, len(update_forest_info.entries)):
3689 r = update_forest_info.entries[i]
3690 if r.type != lsa.LSA_FOREST_TRUST_DOMAIN_INFO:
3692 if r.forest_trust_data.netbios_domain_name.string.upper() != nb.upper():
3697 raise CommandError("Entry not found for value[%s] specified for --delete-nb" % nb)
3698 if update_forest_info.entries[idx].flags & lsa.LSA_NB_DISABLED_ADMIN:
3699 raise CommandError("Entry found for value[%s] specified for --disable-nb is already disabled" % nb)
3700 update_forest_info.entries[idx].time = 0
3701 update_forest_info.entries[idx].flags &= ~lsa.LSA_NB_DISABLED_MASK
3702 update_forest_info.entries[idx].flags |= lsa.LSA_NB_DISABLED_ADMIN
3704 for sid in enable_sid:
3706 for i in xrange(0, len(update_forest_info.entries)):
3707 r = update_forest_info.entries[i]
3708 if r.type != lsa.LSA_FOREST_TRUST_DOMAIN_INFO:
3710 if r.forest_trust_data.domain_sid != sid:
3715 raise CommandError("Entry not found for value[%s] specified for --enable-sid" % sid)
3716 if not update_forest_info.entries[idx].flags & lsa.LSA_SID_DISABLED_MASK:
3717 raise CommandError("Entry found for value[%s] specified for --enable-sid is already enabled" % nb)
3718 update_forest_info.entries[idx].time = 0
3719 update_forest_info.entries[idx].flags &= ~lsa.LSA_SID_DISABLED_MASK
3721 for sid in disable_sid:
3723 for i in xrange(0, len(update_forest_info.entries)):
3724 r = update_forest_info.entries[i]
3725 if r.type != lsa.LSA_FOREST_TRUST_DOMAIN_INFO:
3727 if r.forest_trust_data.domain_sid != sid:
3732 raise CommandError("Entry not found for value[%s] specified for --delete-sid" % sid)
3733 if update_forest_info.entries[idx].flags & lsa.LSA_SID_DISABLED_ADMIN:
3734 raise CommandError("Entry found for value[%s] specified for --disable-sid is already disabled" % nb)
3735 update_forest_info.entries[idx].time = 0
3736 update_forest_info.entries[idx].flags &= ~lsa.LSA_SID_DISABLED_MASK
3737 update_forest_info.entries[idx].flags |= lsa.LSA_SID_DISABLED_ADMIN
3740 update_forest_collision = local_lsa.lsaRSetForestTrustInformation(local_policy,
3741 local_tdo_info.domain_name,
3742 lsa.LSA_FOREST_TRUST_DOMAIN_INFO,
3743 update_forest_info, 0)
3744 except RuntimeError as error:
3745 raise self.LocalRuntimeError(self, error, "lsaRSetForestTrustInformation() failed")
3747 self.outf.write("Updated forest trust information...\n")
3748 self.write_forest_trust_info(update_forest_info,
3749 tln=local_tdo_info.domain_name.string,
3750 collisions=update_forest_collision)
3753 lsaString = lsa.String()
3754 lsaString.string = local_tdo_info.domain_name.string
3755 stored_forest_info = local_lsa.lsaRQueryForestTrustInformation(local_policy,
3757 lsa.LSA_FOREST_TRUST_DOMAIN_INFO)
3758 except RuntimeError as error:
3759 raise self.LocalRuntimeError(self, error, "lsaRQueryForestTrustInformation() failed")
3761 self.outf.write("Stored forest trust information...\n")
3762 self.write_forest_trust_info(stored_forest_info,
3763 tln=local_tdo_info.domain_name.string)
3766 class cmd_domain_tombstones_expunge(Command):
3767 """Expunge tombstones from the database.
3769 This command expunges tombstones from the database."""
3770 synopsis = "%prog NC [NC [...]] [options]"
3773 Option("-H", "--URL", help="LDB URL for database or target server", type=str,
3774 metavar="URL", dest="H"),
3775 Option("--current-time",
3776 help="The current time to evaluate the tombstone lifetime from, expressed as YYYY-MM-DD",
3778 Option("--tombstone-lifetime", help="Number of days a tombstone should be preserved for", type=int),
3781 takes_args = ["nc*"]
3783 takes_optiongroups = {
3784 "sambaopts": options.SambaOptions,
3785 "credopts": options.CredentialsOptions,
3786 "versionopts": options.VersionOptions,
3789 def run(self, *ncs, **kwargs):
3790 sambaopts = kwargs.get("sambaopts")
3791 credopts = kwargs.get("credopts")
3792 versionpts = kwargs.get("versionopts")
3794 current_time_string = kwargs.get("current_time")
3795 tombstone_lifetime = kwargs.get("tombstone_lifetime")
3796 lp = sambaopts.get_loadparm()
3797 creds = credopts.get_credentials(lp)
3798 samdb = SamDB(url=H, session_info=system_session(),
3799 credentials=creds, lp=lp)
3801 if current_time_string is not None:
3802 current_time_obj = time.strptime(current_time_string, "%Y-%m-%d")
3803 current_time = long(time.mktime(current_time_obj))
3806 current_time = long(time.time())
3809 res = samdb.search(expression="", base="", scope=ldb.SCOPE_BASE,
3810 attrs=["namingContexts"])
3813 for nc in res[0]["namingContexts"]:
3818 started_transaction = False
3820 samdb.transaction_start()
3821 started_transaction = True
3823 removed_links) = samdb.garbage_collect_tombstones(ncs,
3824 current_time=current_time,
3825 tombstone_lifetime=tombstone_lifetime)
3827 except Exception, err:
3828 if started_transaction:
3829 samdb.transaction_cancel()
3830 raise CommandError("Failed to expunge / garbage collect tombstones", err)
3832 samdb.transaction_commit()
3834 self.outf.write("Removed %d objects and %d links successfully\n"
3835 % (removed_objects, removed_links))
3839 class cmd_domain_trust(SuperCommand):
3840 """Domain and forest trust management."""
3843 subcommands["list"] = cmd_domain_trust_list()
3844 subcommands["show"] = cmd_domain_trust_show()
3845 subcommands["create"] = cmd_domain_trust_create()
3846 subcommands["delete"] = cmd_domain_trust_delete()
3847 subcommands["validate"] = cmd_domain_trust_validate()
3848 subcommands["namespaces"] = cmd_domain_trust_namespaces()
3850 class cmd_domain_tombstones(SuperCommand):
3851 """Domain tombstone and recycled object management."""
3854 subcommands["expunge"] = cmd_domain_tombstones_expunge()
3856 class cmd_domain(SuperCommand):
3857 """Domain management."""
3860 subcommands["demote"] = cmd_domain_demote()
3861 if cmd_domain_export_keytab is not None:
3862 subcommands["exportkeytab"] = cmd_domain_export_keytab()
3863 subcommands["info"] = cmd_domain_info()
3864 subcommands["provision"] = cmd_domain_provision()
3865 subcommands["join"] = cmd_domain_join()
3866 subcommands["dcpromo"] = cmd_domain_dcpromo()
3867 subcommands["level"] = cmd_domain_level()
3868 subcommands["passwordsettings"] = cmd_domain_passwordsettings()
3869 subcommands["classicupgrade"] = cmd_domain_classicupgrade()
3870 subcommands["samba3upgrade"] = cmd_domain_samba3upgrade()
3871 subcommands["trust"] = cmd_domain_trust()
3872 subcommands["tombstones"] = cmd_domain_tombstones()