s4-join: Import DNS zones in AD DC join
[metze/samba/wip.git] / source4 / scripting / python / samba / netcmd / domain.py
index f7e060f730edbd6562706fa917c403050e41b4d8..4e73a29140e7cf54840ec2740491e1f7163f439e 100644 (file)
@@ -1,11 +1,10 @@
-#!/usr/bin/env python
-#
 # domain management
 #
 # Copyright Matthias Dieter Wallnoefer 2009
 # Copyright Andrew Kroeger 2009
 # Copyright Jelmer Vernooij 2009
 # Copyright Giampaolo Lauria 2011
+# Copyright Matthieu Patou <mat@matws.net> 2011
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 
 import samba.getopt as options
 import ldb
-import sys, os
+import string
+import os
 import tempfile
 import logging
-from samba import Ldb
 from samba.net import Net, LIBNET_JOIN_AUTOMATIC
-from samba.dcerpc.misc import SEC_CHAN_WKSTA
+import samba.ntacls
 from samba.join import join_RODC, join_DC, join_subdomain
 from samba.auth import system_session
 from samba.samdb import SamDB
+from samba.dcerpc import drsuapi
 from samba.dcerpc.samr import DOMAIN_PASSWORD_COMPLEX, DOMAIN_PASSWORD_STORE_CLEARTEXT
 from samba.netcmd import (
     Command,
@@ -41,10 +41,14 @@ from samba.netcmd import (
     SuperCommand,
     Option
     )
+from samba.netcmd.common import netcmd_get_domain_infos_via_cldap
 from samba.samba3 import Samba3
 from samba.samba3 import param as s3param
 from samba.upgrade import upgrade_from_samba3
-from samba.provision import ProvisioningError
+from samba.drs_utils import (
+                            sendDsReplicaSync, drsuapi_connect, drsException,
+                            sendRemoveDsServer)
+
 
 from samba.dsdb import (
     DS_DOMAIN_FUNCTION_2000,
@@ -52,6 +56,11 @@ from samba.dsdb import (
     DS_DOMAIN_FUNCTION_2003_MIXED,
     DS_DOMAIN_FUNCTION_2008,
     DS_DOMAIN_FUNCTION_2008_R2,
+    DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL,
+    DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL,
+    UF_WORKSTATION_TRUST_ACCOUNT,
+    UF_SERVER_TRUST_ACCOUNT,
+    UF_TRUSTED_FOR_DELEGATION
     )
 
 def get_testparm_var(testparm, smbconf, varname):
@@ -59,27 +68,74 @@ def get_testparm_var(testparm, smbconf, varname):
     output = os.popen(cmd, 'r').readline()
     return output.strip()
 
-class cmd_domain_export_keytab(Command):
-    """Dumps kerberos keys of the domain into a keytab"""
+try:
+   import samba.dckeytab
+   class cmd_domain_export_keytab(Command):
+       """Dumps kerberos keys of the domain into a keytab"""
+
+       synopsis = "%prog <keytab> [options]"
+
+       takes_optiongroups = {
+           "sambaopts": options.SambaOptions,
+           "credopts": options.CredentialsOptions,
+           "versionopts": options.VersionOptions,
+           }
+
+       takes_options = [
+           Option("--principal", help="extract only this principal", type=str),
+           ]
+
+       takes_args = ["keytab"]
+
+       def run(self, keytab, credopts=None, sambaopts=None, versionopts=None, principal=None):
+           lp = sambaopts.get_loadparm()
+           net = Net(None, lp)
+           net.export_keytab(keytab=keytab, principal=principal)
+except:
+   cmd_domain_export_keytab = None
 
-    synopsis = "%prog domain exportkeytab <keytab> [options]"
+
+class cmd_domain_info(Command):
+    """Print basic info about a domain and the DC passed as parameter"""
+
+    synopsis = "%prog <ip_address> [options]"
 
     takes_options = [
         ]
 
-    takes_args = ["keytab"]
+    takes_optiongroups = {
+        "sambaopts": options.SambaOptions,
+        "credopts": options.CredentialsOptions,
+        "versionopts": options.VersionOptions,
+        }
 
-    def run(self, keytab, credopts=None, sambaopts=None, versionopts=None):
-        lp = sambaopts.get_loadparm()
-        net = Net(None, lp, server=credopts.ipaddress)
-        net.export_keytab(keytab=keytab)
+    takes_args = ["address"]
 
+    def run(self, address, credopts=None, sambaopts=None, versionopts=None):
+        lp = sambaopts.get_loadparm()
+        try:
+            res = netcmd_get_domain_infos_via_cldap(lp, None, address)
+            print "Forest           : %s" % res.forest
+            print "Domain           : %s" % res.dns_domain
+            print "Netbios domain   : %s" % res.domain_name
+            print "DC name          : %s" % res.pdc_dns_name
+            print "DC netbios name  : %s" % res.pdc_name
+            print "Server site      : %s" % res.server_site
+            print "Client site      : %s" % res.client_site
+        except RuntimeError:
+            raise CommandError("Invalid IP address '" + address + "'!")
 
 
 class cmd_domain_join(Command):
-    """Joins domain as either member or backup domain controller *"""
+    """Joins domain as either member or backup domain controller"""
 
-    synopsis = "%prog domain join <dnsdomain> [DC|RODC|MEMBER|SUBDOMAIN] [options]"
+    synopsis = "%prog <dnsdomain> [DC|RODC|MEMBER|SUBDOMAIN] [options]"
+
+    takes_optiongroups = {
+        "sambaopts": options.SambaOptions,
+        "versionopts": options.VersionOptions,
+        "credopts": options.CredentialsOptions,
+    }
 
     takes_options = [
         Option("--server", help="DC to join", type=str),
@@ -89,13 +145,24 @@ class cmd_domain_join(Command):
         Option("--domain-critical-only",
                help="only replicate critical domain objects",
                action="store_true"),
-        ]
+        Option("--machinepass", type=str, metavar="PASSWORD",
+               help="choose machine password (otherwise random)"),
+        Option("--use-ntvfs", help="Use NTVFS for the fileserver (default = no)",
+               action="store_true"),
+        Option("--dns-backend", type="choice", metavar="NAMESERVER-BACKEND",
+               choices=["SAMBA_INTERNAL", "BIND9_DLZ", "NONE"],
+               help="The DNS server backend. SAMBA_INTERNAL is the builtin name server, " \
+                   "BIND9_DLZ uses samba4 AD to store zone information (default), " \
+                   "NONE skips the DNS setup entirely (this DC will not be a DNS server)",
+               default="BIND9_DLZ")
+       ]
 
     takes_args = ["domain", "role?"]
 
     def run(self, domain, role=None, sambaopts=None, credopts=None,
             versionopts=None, server=None, site=None, targetdir=None,
-            domain_critical_only=False, parent_domain=None):
+            domain_critical_only=False, parent_domain=None, machinepass=None,
+            use_ntvfs=False, dns_backend=None):
         lp = sambaopts.get_loadparm()
         creds = credopts.get_credentials(lp)
         net = Net(creds, lp, server=credopts.ipaddress)
@@ -111,36 +178,278 @@ class cmd_domain_join(Command):
         if role is None or role == "MEMBER":
             (join_password, sid, domain_name) = net.join_member(domain,
                                                                 netbios_name,
-                                                                LIBNET_JOIN_AUTOMATIC)
+                                                                LIBNET_JOIN_AUTOMATIC,
+                                                                machinepass=machinepass)
 
             self.outf.write("Joined domain %s (%s)\n" % (domain_name, sid))
             return
         elif role == "DC":
             join_DC(server=server, creds=creds, lp=lp, domain=domain,
                     site=site, netbios_name=netbios_name, targetdir=targetdir,
-                    domain_critical_only=domain_critical_only)
+                    domain_critical_only=domain_critical_only,
+                    machinepass=machinepass, use_ntvfs=use_ntvfs, dns_backend=dns_backend)
             return
         elif role == "RODC":
             join_RODC(server=server, creds=creds, lp=lp, domain=domain,
                       site=site, netbios_name=netbios_name, targetdir=targetdir,
-                      domain_critical_only=domain_critical_only)
+                      domain_critical_only=domain_critical_only,
+                      machinepass=machinepass, use_ntvfs=use_ntvfs, dns_backend=dns_backend)
             return
         elif role == "SUBDOMAIN":
             netbios_domain = lp.get("workgroup")
             if parent_domain is None:
                 parent_domain = ".".join(domain.split(".")[1:])
             join_subdomain(server=server, creds=creds, lp=lp, dnsdomain=domain, parent_domain=parent_domain,
-                           site=site, netbios_name=netbios_name, netbios_domain=netbios_domain, targetdir=targetdir)
+                           site=site, netbios_name=netbios_name, netbios_domain=netbios_domain, targetdir=targetdir,
+                           machinepass=machinepass, use_ntvfs=use_ntvfs, dns_backend=dns_backend)
             return
         else:
-            raise CommandError("Invalid role %s (possible values: MEMBER, DC, RODC)" % role)
+            raise CommandError("Invalid role '%s' (possible values: MEMBER, DC, RODC, SUBDOMAIN)" % role)
+
+
+
+class cmd_domain_demote(Command):
+    """Demote ourselves from the role of Domain Controller"""
+
+    synopsis = "%prog [options]"
+
+    takes_options = [
+        Option("--server", help="DC to force replication before demote", type=str),
+        Option("--targetdir", help="where provision is stored", type=str),
+        ]
+
+    takes_optiongroups = {
+        "sambaopts": options.SambaOptions,
+        "credopts": options.CredentialsOptions,
+        "versionopts": options.VersionOptions,
+        }
+
+    def run(self, sambaopts=None, credopts=None,
+            versionopts=None, server=None, targetdir=None):
+        lp = sambaopts.get_loadparm()
+        creds = credopts.get_credentials(lp)
+        net = Net(creds, lp, server=credopts.ipaddress)
+
+        netbios_name = lp.get("netbios name")
+        samdb = SamDB(session_info=system_session(), credentials=creds, lp=lp)
+        if not server:
+            res = samdb.search(expression='(&(objectClass=computer)(serverReferenceBL=*))', attrs=["dnsHostName", "name"])
+            if (len(res) == 0):
+                raise CommandError("Unable to search for servers")
+
+            if (len(res) == 1):
+                raise CommandError("You are the latest server in the domain")
+
+            server = None
+            for e in res:
+                if str(e["name"]).lower() != netbios_name.lower():
+                    server = e["dnsHostName"]
+                    break
+
+        ntds_guid = samdb.get_ntds_GUID()
+        msg = samdb.search(base=str(samdb.get_config_basedn()), scope=ldb.SCOPE_SUBTREE,
+                                expression="(objectGUID=%s)" % ntds_guid,
+                                attrs=['options'])
+        if len(msg) == 0 or "options" not in msg[0]:
+            raise CommandError("Failed to find options on %s" % ntds_guid)
+
+        ntds_dn = msg[0].dn
+        dsa_options = int(str(msg[0]['options']))
+
+        res = samdb.search(expression="(fSMORoleOwner=%s)" % str(ntds_dn),
+                            controls=["search_options:1:2"])
+
+        if len(res) != 0:
+            raise CommandError("Current DC is still the owner of %d role(s), use the role command to transfer roles to another DC" % len(res))
+
+        print "Using %s as partner server for the demotion" % server
+        (drsuapiBind, drsuapi_handle, supportedExtensions) = drsuapi_connect(server, lp, creds)
+
+        print "Desactivating inbound replication"
+
+        nmsg = ldb.Message()
+        nmsg.dn = msg[0].dn
+
+        dsa_options |= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
+        nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
+        samdb.modify(nmsg)
+
+        if not (dsa_options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) and not samdb.am_rodc():
+
+            print "Asking partner server %s to synchronize from us" % server
+            for part in (samdb.get_schema_basedn(),
+                            samdb.get_config_basedn(),
+                            samdb.get_root_basedn()):
+                try:
+                    sendDsReplicaSync(drsuapiBind, drsuapi_handle, ntds_guid, str(part), drsuapi.DRSUAPI_DRS_WRIT_REP)
+                except drsException, e:
+                    print "Error while demoting, re-enabling inbound replication"
+                    dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
+                    nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
+                    samdb.modify(nmsg)
+                    raise CommandError("Error while sending a DsReplicaSync for partion %s" % str(part), e)
+        try:
+            remote_samdb = SamDB(url="ldap://%s" % server,
+                                session_info=system_session(),
+                                credentials=creds, lp=lp)
+
+            print "Changing userControl and container"
+            res = remote_samdb.search(base=str(remote_samdb.get_root_basedn()),
+                                expression="(&(objectClass=user)(sAMAccountName=%s$))" %
+                                            netbios_name.upper(),
+                                attrs=["userAccountControl"])
+            dc_dn = res[0].dn
+            uac = int(str(res[0]["userAccountControl"]))
+
+        except Exception, e:
+                print "Error while demoting, re-enabling inbound replication"
+                dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
+                nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
+                samdb.modify(nmsg)
+                raise CommandError("Error while changing account control", e)
+
+        if (len(res) != 1):
+            print "Error while demoting, re-enabling inbound replication"
+            dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
+            nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
+            samdb.modify(nmsg)
+            raise CommandError("Unable to find object with samaccountName = %s$"
+                               " in the remote dc" % netbios_name.upper())
+
+        olduac = uac
+
+        uac ^= (UF_SERVER_TRUST_ACCOUNT|UF_TRUSTED_FOR_DELEGATION)
+        uac |= UF_WORKSTATION_TRUST_ACCOUNT
+
+        msg = ldb.Message()
+        msg.dn = dc_dn
+
+        msg["userAccountControl"] = ldb.MessageElement("%d" % uac,
+                                                        ldb.FLAG_MOD_REPLACE,
+                                                        "userAccountControl")
+        try:
+            remote_samdb.modify(msg)
+        except Exception, e:
+            print "Error while demoting, re-enabling inbound replication"
+            dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
+            nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
+            samdb.modify(nmsg)
+
+            raise CommandError("Error while changing account control", e)
+
+        parent = msg.dn.parent()
+        rdn = str(res[0].dn)
+        rdn = string.replace(rdn, ",%s" % str(parent), "")
+        # Let's move to the Computer container
+        i = 0
+        newrdn = rdn
+
+        computer_dn = ldb.Dn(remote_samdb, "CN=Computers,%s" % str(remote_samdb.get_root_basedn()))
+        res = remote_samdb.search(base=computer_dn, expression=rdn, scope=ldb.SCOPE_ONELEVEL)
+
+        if (len(res) != 0):
+            res = remote_samdb.search(base=computer_dn, expression="%s-%d" % (rdn, i),
+                                        scope=ldb.SCOPE_ONELEVEL)
+            while(len(res) != 0 and i < 100):
+                i = i + 1
+                res = remote_samdb.search(base=computer_dn, expression="%s-%d" % (rdn, i),
+                                            scope=ldb.SCOPE_ONELEVEL)
+
+            if i == 100:
+                print "Error while demoting, re-enabling inbound replication"
+                dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
+                nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
+                samdb.modify(nmsg)
+
+                msg = ldb.Message()
+                msg.dn = dc_dn
+
+                msg["userAccountControl"] = ldb.MessageElement("%d" % uac,
+                                                        ldb.FLAG_MOD_REPLACE,
+                                                        "userAccountControl")
+
+                remote_samdb.modify(msg)
+
+                raise CommandError("Unable to find a slot for renaming %s,"
+                                    " all names from %s-1 to %s-%d seemed used" %
+                                    (str(dc_dn), rdn, rdn, i - 9))
+
+            newrdn = "%s-%d" % (rdn, i)
+
+        try:
+            newdn = ldb.Dn(remote_samdb, "%s,%s" % (newrdn, str(computer_dn)))
+            remote_samdb.rename(dc_dn, newdn)
+        except Exception, e:
+            print "Error while demoting, re-enabling inbound replication"
+            dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
+            nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
+            samdb.modify(nmsg)
 
+            msg = ldb.Message()
+            msg.dn = dc_dn
+
+            msg["userAccountControl"] = ldb.MessageElement("%d" % uac,
+                                                    ldb.FLAG_MOD_REPLACE,
+                                                    "userAccountControl")
+
+            remote_samdb.modify(msg)
+            raise CommandError("Error while renaming %s to %s" % (str(dc_dn), str(newdn)), e)
+
+
+        server_dsa_dn = samdb.get_serverName()
+        domain = remote_samdb.get_root_basedn()
+
+        try:
+            sendRemoveDsServer(drsuapiBind, drsuapi_handle, server_dsa_dn, domain)
+        except drsException, e:
+            print "Error while demoting, re-enabling inbound replication"
+            dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
+            nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
+            samdb.modify(nmsg)
+
+            msg = ldb.Message()
+            msg.dn = newdn
+
+            msg["userAccountControl"] = ldb.MessageElement("%d" % uac,
+                                                    ldb.FLAG_MOD_REPLACE,
+                                                    "userAccountControl")
+            print str(dc_dn)
+            remote_samdb.modify(msg)
+            remote_samdb.rename(newdn, dc_dn)
+            raise CommandError("Error while sending a removeDsServer", e)
+
+        for s in ("CN=Entreprise,CN=Microsoft System Volumes,CN=System,CN=Configuration",
+                  "CN=%s,CN=Microsoft System Volumes,CN=System,CN=Configuration" % lp.get("realm"),
+                  "CN=Domain System Volumes (SYSVOL share),CN=File Replication Service,CN=System"):
+            try:
+                remote_samdb.delete(ldb.Dn(remote_samdb,
+                                    "%s,%s,%s" % (str(rdn), s, str(remote_samdb.get_root_basedn()))))
+            except ldb.LdbError, l:
+                pass
+
+        for s in ("CN=Entreprise,CN=NTFRS Subscriptions",
+                  "CN=%s, CN=NTFRS Subscriptions" % lp.get("realm"),
+                  "CN=Domain system Volumes (SYSVOL Share), CN=NTFRS Subscriptions",
+                  "CN=NTFRS Subscriptions"):
+            try:
+                remote_samdb.delete(ldb.Dn(remote_samdb,
+                                    "%s,%s" % (s, str(newdn))))
+            except ldb.LdbError, l:
+                pass
+
+        self.outf.write("Demote successfull\n")
 
 
 class cmd_domain_level(Command):
     """Raises domain and forest function levels"""
 
-    synopsis = "%prog domain level (show|raise <options>) [options]"
+    synopsis = "%prog (show|raise <options>) [options]"
+
+    takes_optiongroups = {
+        "sambaopts": options.SambaOptions,
+        "credopts": options.CredentialsOptions,
+        "versionopts": options.VersionOptions,
+        }
 
     takes_options = [
         Option("-H", "--URL", help="LDB URL for database or target server", type=str,
@@ -164,7 +473,7 @@ class cmd_domain_level(Command):
 
         domain_dn = samdb.domain_dn()
 
-        res_forest = samdb.search("CN=Partitions," + samdb.get_config_basedn(),
+        res_forest = samdb.search("CN=Partitions,%s" % samdb.get_config_basedn(),
           scope=ldb.SCOPE_BASE, attrs=["msDS-Behavior-Version"])
         assert len(res_forest) == 1
 
@@ -172,7 +481,7 @@ class cmd_domain_level(Command):
           attrs=["msDS-Behavior-Version", "nTMixedDomain"])
         assert len(res_domain) == 1
 
-        res_dc_s = samdb.search("CN=Sites," + samdb.get_config_basedn(),
+        res_dc_s = samdb.search("CN=Sites,%s" % samdb.get_config_basedn(),
           scope=ldb.SCOPE_SUBTREE, expression="(objectClass=nTDSDSA)",
           attrs=["msDS-Behavior-Version"])
         assert len(res_dc_s) >= 1
@@ -264,7 +573,7 @@ class cmd_domain_level(Command):
                     new_level_domain = DS_DOMAIN_FUNCTION_2008_R2
 
                 if new_level_domain <= level_domain and level_domain_mixed == 0:
-                    raise CommandError("Domain function level can't be smaller equal to the actual one!")
+                    raise CommandError("Domain function level can't be smaller than or equal to the actual one!")
 
                 if new_level_domain > min_level_dc:
                     raise CommandError("Domain function level can't be higher than the lowest function level of a DC!")
@@ -279,7 +588,7 @@ class cmd_domain_level(Command):
                     samdb.modify(m)
                     # Under partitions
                     m = ldb.Message()
-                    m.dn = ldb.Dn(samdb, "CN=" + lp.get("workgroup") + ",CN=Partitions," + ldb.get_config_basedn())
+                    m.dn = ldb.Dn(samdb, "CN=" + lp.get("workgroup") + ",CN=Partitions,%s" % samdb.get_config_basedn())
                     m["nTMixedDomain"] = ldb.MessageElement("0",
                       ldb.FLAG_MOD_REPLACE, "nTMixedDomain")
                     try:
@@ -298,7 +607,7 @@ class cmd_domain_level(Command):
                 # Under partitions
                 m = ldb.Message()
                 m.dn = ldb.Dn(samdb, "CN=" + lp.get("workgroup")
-                  + ",CN=Partitions," + ldb.get_config_basedn())
+                  + ",CN=Partitions,%s" % samdb.get_config_basedn())
                 m["msDS-Behavior-Version"]= ldb.MessageElement(
                   str(new_level_domain), ldb.FLAG_MOD_REPLACE,
                           "msDS-Behavior-Version")
@@ -319,11 +628,11 @@ class cmd_domain_level(Command):
                 elif forest_level == "2008_R2":
                     new_level_forest = DS_DOMAIN_FUNCTION_2008_R2
                 if new_level_forest <= level_forest:
-                    raise CommandError("Forest function level can't be smaller equal to the actual one!")
+                    raise CommandError("Forest function level can't be smaller than or equal to the actual one!")
                 if new_level_forest > level_domain:
                     raise CommandError("Forest function level can't be higher than the domain function level(s). Please raise it/them first!")
                 m = ldb.Message()
-                m.dn = ldb.Dn(samdb, "CN=Partitions," + ldb.get_config_basedn())
+                m.dn = ldb.Dn(samdb, "CN=Partitions,%s" % samdb.get_config_basedn())
                 m["msDS-Behavior-Version"]= ldb.MessageElement(
                   str(new_level_forest), ldb.FLAG_MOD_REPLACE,
                           "msDS-Behavior-Version")
@@ -332,35 +641,7 @@ class cmd_domain_level(Command):
             msgs.append("All changes applied successfully!")
             self.message("\n".join(msgs))
         else:
-            raise CommandError("Wrong argument '%s'!" % subcommand)
-
-
-
-class cmd_domain_machinepassword(Command):
-    """Gets a machine password out of our SAM"""
-
-    synopsis = "%prog domain machinepassword <accountname> [options]"
-
-    takes_args = ["secret"]
-
-    def run(self, secret, sambaopts=None, credopts=None, versionopts=None):
-        lp = sambaopts.get_loadparm()
-        creds = credopts.get_credentials(lp, fallback_machine=True)
-        name = lp.get("secrets database")
-        path = lp.get("private dir")
-        url = os.path.join(path, name)
-        if not os.path.exists(url):
-            raise CommandError("secret database not found at %s " % url)
-        secretsdb = Ldb(url=url, session_info=system_session(),
-            credentials=creds, lp=lp)
-        result = secretsdb.search(attrs=["secret"],
-            expression="(&(objectclass=primaryDomain)(samaccountname=%s))" % ldb.binary_encode(secret))
-
-        if len(result) != 1:
-            raise CommandError("search returned %d records, expected 1" % len(result))
-
-        self.outf.write("%s\n" % result[0]["secret"])
-
+            raise CommandError("invalid argument: '%s' (choose from 'show', 'raise')" % subcommand)
 
 
 class cmd_domain_passwordsettings(Command):
@@ -370,7 +651,13 @@ class cmd_domain_passwordsettings(Command):
     and maximum password age) on a Samba4 server.
     """
 
-    synopsis = "%prog domain passwordsettings (show|set <options>) [options]"
+    synopsis = "%prog (show|set <options>) [options]"
+
+    takes_optiongroups = {
+        "sambaopts": options.SambaOptions,
+        "versionopts": options.VersionOptions,
+        "credopts": options.CredentialsOptions,
+        }
 
     takes_options = [
         Option("-H", "--URL", help="LDB URL for database or target server", type=str,
@@ -413,7 +700,10 @@ class cmd_domain_passwordsettings(Command):
             cur_min_pwd_len = int(res[0]["minPwdLength"][0])
             # ticks -> days
             cur_min_pwd_age = int(abs(int(res[0]["minPwdAge"][0])) / (1e7 * 60 * 60 * 24))
-            cur_max_pwd_age = int(abs(int(res[0]["maxPwdAge"][0])) / (1e7 * 60 * 60 * 24))
+            if int(res[0]["maxPwdAge"][0]) == -0x8000000000000000:
+                cur_max_pwd_age = 0
+            else:
+                cur_max_pwd_age = int(abs(int(res[0]["maxPwdAge"][0])) / (1e7 * 60 * 60 * 24))
         except Exception, e:
             raise CommandError("Could not retrieve password properties!", e)
 
@@ -509,7 +799,10 @@ class cmd_domain_passwordsettings(Command):
                     raise CommandError("Maximum password age must be in the range of 0 to 999!")
 
                 # days -> ticks
-                max_pwd_age_ticks = -int(max_pwd_age * (24 * 60 * 60 * 1e7))
+                if max_pwd_age == 0:
+                    max_pwd_age_ticks = -0x8000000000000000
+                else:
+                    max_pwd_age_ticks = -int(max_pwd_age * (24 * 60 * 60 * 1e7))
 
                 m["maxPwdAge"] = ldb.MessageElement(str(max_pwd_age_ticks),
                   ldb.FLAG_MOD_REPLACE, "maxPwdAge")
@@ -527,13 +820,14 @@ class cmd_domain_passwordsettings(Command):
             raise CommandError("Wrong argument '%s'!" % subcommand)
 
 
-class cmd_domain_samba3upgrade(Command):
-    """Upgrade from Samba3 database to Samba4 AD database"""
+class cmd_domain_classicupgrade(Command):
+    """Upgrade from Samba classic (NT4-like) database to Samba AD DC database.
 
-    synopsis = "%prog domain samba3upgrade [options] <samba3_smb_conf>"
+    Specify either a directory with all Samba classic DC databases and state files (with --dbdir) or
+    the testparm utility from your classic installation (with --testparm).
+    """
 
-    long_description = """Specify either samba3 database directory (with --libdir) or
-samba3 testparm utility (with --testparm)."""
+    synopsis = "%prog [options] <classic_smb_conf>"
 
     takes_optiongroups = {
         "sambaopts": options.SambaOptions,
@@ -541,45 +835,55 @@ samba3 testparm utility (with --testparm)."""
     }
 
     takes_options = [
-        Option("--libdir", type="string", metavar="DIR",
-                  help="Path to samba3 database directory"),
+        Option("--dbdir", type="string", metavar="DIR",
+                  help="Path to samba classic DC database directory"),
         Option("--testparm", type="string", metavar="PATH",
-                  help="Path to samba3 testparm utility"),
+                  help="Path to samba classic DC testparm utility from the previous installation.  This allows the default paths of the previous installation to be followed"),
         Option("--targetdir", type="string", metavar="DIR",
                   help="Path prefix where the new Samba 4.0 AD domain should be initialised"),
-        Option("--quiet", help="Be quiet"),
+        Option("--quiet", help="Be quiet", action="store_true"),
+        Option("--verbose", help="Be verbose", action="store_true"),
         Option("--use-xattrs", type="choice", choices=["yes","no","auto"], metavar="[yes|no|auto]",
                    help="Define if we should use the native fs capabilities or a tdb file for storing attributes likes ntacl, auto tries to make an inteligent guess based on the user rights and system capabilities", default="auto"),
+        Option("--dns-backend", type="choice", metavar="NAMESERVER-BACKEND",
+               choices=["SAMBA_INTERNAL", "BIND9_FLATFILE", "BIND9_DLZ", "NONE"],
+               help="The DNS server backend. SAMBA_INTERNAL is the builtin name server, " \
+                   "BIND9_FLATFILE uses bind9 text database to store zone information, " \
+                   "BIND9_DLZ uses samba4 AD to store zone information (default), " \
+                   "NONE skips the DNS setup entirely (this DC will not be a DNS server)",
+               default="BIND9_DLZ")
     ]
 
     takes_args = ["smbconf"]
 
-    def run(self, smbconf=None, targetdir=None, libdir=None, testparm=None, 
-            quiet=None, use_xattrs=None, sambaopts=None, versionopts=None):
+    def run(self, smbconf=None, targetdir=None, dbdir=None, testparm=None, 
+            quiet=False, verbose=False, use_xattrs=None, sambaopts=None, versionopts=None,
+            dns_backend=None):
 
         if not os.path.exists(smbconf):
             raise CommandError("File %s does not exist" % smbconf)
-        
+
         if testparm and not os.path.exists(testparm):
             raise CommandError("Testparm utility %s does not exist" % testparm)
 
-        if libdir and not os.path.exists(libdir):
-            raise CommandError("Directory %s does not exist" % libdir)
+        if dbdir and not os.path.exists(dbdir):
+            raise CommandError("Directory %s does not exist" % dbdir)
 
-        if not libdir and not testparm:
-            raise CommandError("Please specify either libdir or testparm")
+        if not dbdir and not testparm:
+            raise CommandError("Please specify either dbdir or testparm")
 
-        if libdir and testparm:
-            self.outf.write("warning: both libdir and testparm specified, ignoring libdir.\n")
-            libdir = None
-
-        logger = logging.getLogger("upgrade")
-        logger.addHandler(logging.StreamHandler(sys.stdout))
-        if quiet:
+        logger = self.get_logger()
+        if verbose:
+            logger.setLevel(logging.DEBUG)
+        elif quiet:
             logger.setLevel(logging.WARNING)
         else:
             logger.setLevel(logging.INFO)
 
+        if dbdir and testparm:
+            logger.warning("both dbdir and testparm specified, ignoring dbdir.")
+            dbdir = None
+
         lp = sambaopts.get_loadparm()
 
         s3conf = s3param.get_context()
@@ -587,30 +891,41 @@ samba3 testparm utility (with --testparm)."""
         if sambaopts.realm:
             s3conf.set("realm", sambaopts.realm)
 
+        if targetdir is not None:
+            if not os.path.isdir(targetdir):
+                os.mkdir(targetdir)
+
         eadb = True
         if use_xattrs == "yes":
             eadb = False
         elif use_xattrs == "auto" and not s3conf.get("posix:eadb"):
-            tmpfile = tempfile.NamedTemporaryFile()
+            if targetdir:
+                tmpfile = tempfile.NamedTemporaryFile(dir=os.path.abspath(targetdir))
+            else:
+                tmpfile = tempfile.NamedTemporaryFile(dir=os.path.abspath(os.path.dirname(lp.get("private dir"))))
             try:
-                samba.ntacls.setntacl(lp, tmpfile.name,
-                            "O:S-1-5-32G:S-1-5-32", "S-1-5-32", "native")
-                eadb = False
-            except:
-                # FIXME: Don't catch all exceptions here
-                logger.info("You are not root or your system do not support xattr, using tdb backend for attributes. "
-                            "If you intend to use this provision in production, rerun the script as root on a system supporting xattrs.")
-            tmpfile.close()
-
-        # Set correct default values from libdir or testparm
+                try:
+                    samba.ntacls.setntacl(lp, tmpfile.name,
+                                "O:S-1-5-32G:S-1-5-32", "S-1-5-32", "native")
+                    eadb = False
+                except Exception:
+                    # FIXME: Don't catch all exceptions here
+                    logger.info("You are not root or your system do not support xattr, using tdb backend for attributes. "
+                                "If you intend to use this provision in production, rerun the script as root on a system supporting xattrs.")
+            finally:
+                tmpfile.close()
+
+        # Set correct default values from dbdir or testparm
         paths = {}
-        if libdir:
-            paths["state directory"] = libdir
-            paths["private dir"] = libdir
-            paths["lock directory"] = libdir
+        if dbdir:
+            paths["state directory"] = dbdir
+            paths["private dir"] = dbdir
+            paths["lock directory"] = dbdir
+            paths["smb passwd file"] = dbdir + "/smbpasswd"
         else:
             paths["state directory"] = get_testparm_var(testparm, smbconf, "state directory")
             paths["private dir"] = get_testparm_var(testparm, smbconf, "private dir")
+            paths["smb passwd file"] = get_testparm_var(testparm, smbconf, "smb passwd file")
             paths["lock directory"] = get_testparm_var(testparm, smbconf, "lock directory")
             # "testparm" from Samba 3 < 3.4.x is not aware of the parameter
             # "state directory", instead make use of "lock directory"
@@ -627,16 +942,18 @@ samba3 testparm utility (with --testparm)."""
     
         logger.info("Provisioning")
         upgrade_from_samba3(samba3, logger, targetdir, session_info=system_session(), 
-                            useeadb=eadb)
-
+                            useeadb=eadb, dns_backend=dns_backend)
 
 class cmd_domain(SuperCommand):
     """Domain management"""
 
     subcommands = {}
-    subcommands["exportkeytab"] = cmd_domain_export_keytab()
+    subcommands["demote"] = cmd_domain_demote()
+    if type(cmd_domain_export_keytab).__name__ != 'NoneType':
+        subcommands["exportkeytab"] = cmd_domain_export_keytab()
+    subcommands["info"] = cmd_domain_info()
     subcommands["join"] = cmd_domain_join()
     subcommands["level"] = cmd_domain_level()
-    subcommands["machinepassword"] = cmd_domain_machinepassword()
     subcommands["passwordsettings"] = cmd_domain_passwordsettings()
-    subcommands["samba3upgrade"] = cmd_domain_samba3upgrade()
+    subcommands["classicupgrade"] = cmd_domain_classicupgrade()
+    subcommands["samba3upgrade"] = cmd_domain_classicupgrade()