VERSION: Bump version number up to 4.0.4.
[samba.git] / source4 / scripting / python / samba / join.py
index 41d97cb47715cd9de470c1a111fefacec8556a8b..c55c22cad539169e0466d742bd71d283a2358979 100644 (file)
@@ -44,7 +44,7 @@ class DCJoinException(Exception):
 
 
 class dc_join(object):
-    '''perform a DC join'''
+    """Perform a DC join."""
 
     def __init__(ctx, server=None, creds=None, lp=None, site=None,
                  netbios_name=None, targetdir=None, domain=None,
@@ -56,10 +56,6 @@ class dc_join(object):
         ctx.netbios_name = netbios_name
         ctx.targetdir = targetdir
         ctx.use_ntvfs = use_ntvfs
-        if dns_backend is None:
-            ctx.dns_backend = "NONE"
-        else:
-            ctx.dns_backend = dns_backend
 
         ctx.promote_existing = promote_existing
         ctx.promote_from_dn = None
@@ -118,6 +114,22 @@ class dc_join(object):
 
         ctx.dnsdomain = ctx.samdb.domain_dns_name()
         ctx.dnsforest = ctx.samdb.forest_dns_name()
+        ctx.domaindns_zone = 'DC=DomainDnsZones,%s' % ctx.base_dn
+        ctx.forestdns_zone = 'DC=ForestDnsZones,%s' % ctx.base_dn
+
+        res_domaindns = ctx.samdb.search(scope=ldb.SCOPE_ONELEVEL,
+                                         attrs=[],
+                                         base=ctx.samdb.get_partitions_dn(),
+                                         expression="(&(objectClass=crossRef)(ncName=%s))" % ctx.domaindns_zone)
+        if dns_backend is None:
+            ctx.dns_backend = "NONE"
+        else:
+            if len(res_domaindns) == 0:
+                ctx.dns_backend = "NONE"
+                print "NO DNS zone information found in source domain, not replicating DNS"
+            else:
+                ctx.dns_backend = dns_backend
+
         ctx.dnshostname = "%s.%s" % (ctx.myname, ctx.dnsdomain)
 
         ctx.realm = ctx.dnsdomain
@@ -155,7 +167,7 @@ class dc_join(object):
             pass
 
     def cleanup_old_join(ctx):
-        '''remove any DNs from a previous join'''
+        """Remove any DNs from a previous join."""
         try:
             # find the krbtgt link
             print("checking sAMAccountName")
@@ -208,7 +220,7 @@ class dc_join(object):
             pass
 
     def promote_possible(ctx):
-        '''confirm that the account is just a bare NT4 BDC or a member server, so can be safely promoted'''
+        """confirm that the account is just a bare NT4 BDC or a member server, so can be safely promoted"""
         if ctx.subdomain:
             # This shouldn't happen
             raise Exception("Can not promote into a subdomain")
@@ -222,12 +234,12 @@ class dc_join(object):
             raise Exception("Account '%s' appears to be an active DC, use 'samba-tool domain join' if you must re-create this account" % ctx.samname)
         if (int(res[0]["userAccountControl"][0]) & (samba.dsdb.UF_WORKSTATION_TRUST_ACCOUNT|samba.dsdb.UF_SERVER_TRUST_ACCOUNT) == 0):
             raise Exception("Account %s is not a domain member or a bare NT4 BDC, use 'samba-tool domain join' instead'" % ctx.samname)
-        
+
         ctx.promote_from_dn = res[0].dn
 
 
     def find_dc(ctx, domain):
-        '''find a writeable DC for the given domain'''
+        """find a writeable DC for the given domain"""
         try:
             ctx.cldap_ret = ctx.net.finddc(domain=domain, flags=nbt.NBT_SERVER_LDAP | nbt.NBT_SERVER_DS | nbt.NBT_SERVER_WRITABLE)
         except Exception:
@@ -415,7 +427,7 @@ class dc_join(object):
 
     def join_add_ntdsdsa(ctx):
         '''add the ntdsdsa object'''
-        # FIXME: the partition (NC) assignment has to be made dynamic
+
         print "Adding %s" % ctx.ntds_dn
         rec = {
             "dn" : ctx.ntds_dn,
@@ -473,7 +485,7 @@ class dc_join(object):
                 rec["msDS-NeverRevealGroup"] = ctx.never_reveal_sid
             elif ctx.promote_existing:
                 rec["msDS-NeverRevealGroup"] = []
-                
+
             if ctx.reveal_sid:
                 rec["msDS-RevealOnDemandGroup"] = ctx.reveal_sid
             elif ctx.promote_existing:
@@ -568,7 +580,7 @@ class dc_join(object):
             ctx.samdb.modify(m)
 
     def join_add_objects2(ctx):
-        '''add the various objects needed for the join, for subdomains post replication'''
+        """add the various objects needed for the join, for subdomains post replication"""
 
         print "Adding %s" % ctx.partition_dn
         # NOTE: windows sends a ntSecurityDescriptor here, we
@@ -627,7 +639,7 @@ class dc_join(object):
                            replica_flags=drsuapi.DRSUAPI_DRS_WRIT_REP)
 
     def join_provision(ctx):
-        '''provision the local SAM'''
+        """Provision the local SAM."""
 
         print "Calling bare provision"
 
@@ -651,7 +663,7 @@ class dc_join(object):
         ctx.names       = presult.names
 
     def join_provision_own_domain(ctx):
-        '''provision the local SAM'''
+        """Provision the local SAM."""
 
         # we now operate exclusively on the local database, which
         # we need to reopen in order to get the newly created schema
@@ -686,7 +698,7 @@ class dc_join(object):
         print("Provision OK for domain %s" % ctx.names.dnsdomain)
 
     def join_replicate(ctx):
-        '''replicate the SAM'''
+        """Replicate the SAM."""
 
         print "Starting replication"
         ctx.local_samdb.transaction_start()
@@ -734,16 +746,22 @@ class dc_join(object):
                 repl.replicate(ctx.base_dn, source_dsa_invocation_id,
                                destination_dsa_guid, rodc=ctx.RODC,
                                replica_flags=ctx.domain_replica_flags)
+            print "Done with always replicated NC (base, config, schema)"
 
-            if 'DC=DomainDnsZones,%s' % ctx.base_dn in ctx.nc_list:
-                repl.replicate('DC=DomainDnsZones,%s' % ctx.base_dn, source_dsa_invocation_id,
-                               destination_dsa_guid, rodc=ctx.RODC,
-                               replica_flags=ctx.replica_flags)
+            for nc in (ctx.domaindns_zone, ctx.forestdns_zone):
+                if nc in ctx.nc_list:
+                    print "Replicating %s" % (str(nc))
+                    repl.replicate(nc, source_dsa_invocation_id,
+                                    destination_dsa_guid, rodc=ctx.RODC,
+                                    replica_flags=ctx.replica_flags)
 
             if 'DC=ForestDnsZones,%s' % ctx.root_dn in ctx.nc_list:
                 repl.replicate('DC=ForestDnsZones,%s' % ctx.root_dn, source_dsa_invocation_id,
                                destination_dsa_guid, rodc=ctx.RODC,
                                replica_flags=ctx.replica_flags)
+            # FIXME At this point we should add an entry in the forestdns and domaindns NC
+            # (those under CN=Partions,DC=...)
+            # in order to indicate that we hold a replica for this NC
 
             if ctx.RODC:
                 repl.replicate(ctx.acct_dn, source_dsa_invocation_id,
@@ -779,12 +797,17 @@ class dc_join(object):
             ctx.drsuapi.DsReplicaUpdateRefs(ctx.drsuapi_handle, 1, r)
 
     def join_finalise(ctx):
-        '''finalise the join, mark us synchronised and setup secrets db'''
+        """Finalise the join, mark us synchronised and setup secrets db."""
 
         logger = logging.getLogger("provision")
         logger.addHandler(logging.StreamHandler(sys.stdout))
 
-        print "Sending DsReplicateUpdateRefs for all the partitions"
+        # FIXME we shouldn't do this in all cases
+        # If for some reasons we joined in another site than the one of
+        # DC we just replicated from then we don't need to send the updatereplicateref
+        # as replication between sites is time based and on the initiative of the
+        # requesting DC
+        print "Sending DsReplicateUpdateRefs for all the replicated partitions"
         for nc in ctx.full_nc_list:
             ctx.send_DsReplicaUpdateRefs(nc)
 
@@ -847,7 +870,7 @@ class dc_join(object):
                             targetdir=ctx.targetdir)
 
     def join_setup_trusts(ctx):
-        '''provision the local SAM'''
+        """provision the local SAM."""
 
         def arcfour_encrypt(key, data):
             from Crypto.Cipher import ARC4
@@ -960,13 +983,15 @@ class dc_join(object):
 
 
     def do_join(ctx):
+        # full_nc_list is the list of naming context (NC) for which we will
+        # send a updateRef command to the partner DC
         ctx.nc_list = [ ctx.config_dn, ctx.schema_dn ]
         ctx.full_nc_list = [ctx.base_dn, ctx.config_dn, ctx.schema_dn ]
 
         if not ctx.subdomain:
             ctx.nc_list += [ctx.base_dn]
             if ctx.dns_backend != "NONE":
-                ctx.nc_list += ['DC=DomainDnsZones,%s' % ctx.base_dn]
+                ctx.nc_list += [ctx.domaindns_zone]
 
         if ctx.dns_backend != "NONE":
             ctx.full_nc_list += ['DC=DomainDnsZones,%s' % ctx.base_dn]
@@ -977,7 +1002,7 @@ class dc_join(object):
             ctx.promote_possible()
         else:
             ctx.cleanup_old_join()
-            
+
         try:
             ctx.join_add_objects()
             ctx.join_provision()
@@ -997,7 +1022,7 @@ def join_RODC(server=None, creds=None, lp=None, site=None, netbios_name=None,
               targetdir=None, domain=None, domain_critical_only=False,
               machinepass=None, use_ntvfs=False, dns_backend=None,
               promote_existing=False):
-    """join as a RODC"""
+    """Join as a RODC."""
 
     ctx = dc_join(server, creds, lp, site, netbios_name, targetdir, domain,
                   machinepass, use_ntvfs, dns_backend, promote_existing)
@@ -1011,11 +1036,12 @@ def join_RODC(server=None, creds=None, lp=None, site=None, netbios_name=None,
     ctx.krbtgt_dn = "CN=krbtgt_%s,CN=Users,%s" % (ctx.myname, ctx.base_dn)
 
     # setup some defaults for accounts that should be replicated to this RODC
-    ctx.never_reveal_sid = [ "<SID=%s-%s>" % (ctx.domsid, security.DOMAIN_RID_RODC_DENY),
-                             "<SID=%s>" % security.SID_BUILTIN_ADMINISTRATORS,
-                             "<SID=%s>" % security.SID_BUILTIN_SERVER_OPERATORS,
-                             "<SID=%s>" % security.SID_BUILTIN_BACKUP_OPERATORS,
-                             "<SID=%s>" % security.SID_BUILTIN_ACCOUNT_OPERATORS ]
+    ctx.never_reveal_sid = [
+        "<SID=%s-%s>" % (ctx.domsid, security.DOMAIN_RID_RODC_DENY),
+        "<SID=%s>" % security.SID_BUILTIN_ADMINISTRATORS,
+        "<SID=%s>" % security.SID_BUILTIN_SERVER_OPERATORS,
+        "<SID=%s>" % security.SID_BUILTIN_BACKUP_OPERATORS,
+        "<SID=%s>" % security.SID_BUILTIN_ACCOUNT_OPERATORS]
     ctx.reveal_sid = "<SID=%s-%s>" % (ctx.domsid, security.DOMAIN_RID_RODC_ALLOW)
 
     mysid = ctx.get_mysid()
@@ -1044,7 +1070,6 @@ def join_RODC(server=None, creds=None, lp=None, site=None, netbios_name=None,
 
     ctx.do_join()
 
-
     print "Joined domain %s (SID %s) as an RODC" % (ctx.domain_name, ctx.domsid)
 
 
@@ -1052,7 +1077,7 @@ def join_DC(server=None, creds=None, lp=None, site=None, netbios_name=None,
             targetdir=None, domain=None, domain_critical_only=False,
             machinepass=None, use_ntvfs=False, dns_backend=None,
             promote_existing=False):
-    """join as a DC"""
+    """Join as a DC."""
     ctx = dc_join(server, creds, lp, site, netbios_name, targetdir, domain,
                   machinepass, use_ntvfs, dns_backend, promote_existing)
 
@@ -1079,10 +1104,11 @@ def join_DC(server=None, creds=None, lp=None, site=None, netbios_name=None,
     ctx.do_join()
     print "Joined domain %s (SID %s) as a DC" % (ctx.domain_name, ctx.domsid)
 
-def join_subdomain(server=None, creds=None, lp=None, site=None, netbios_name=None,
-                   targetdir=None, parent_domain=None, dnsdomain=None, netbios_domain=None,
-                   machinepass=None, use_ntvfs=False, dns_backend=None):
-    """join as a DC"""
+def join_subdomain(server=None, creds=None, lp=None, site=None,
+        netbios_name=None, targetdir=None, parent_domain=None, dnsdomain=None,
+        netbios_domain=None, machinepass=None, use_ntvfs=False,
+        dns_backend=None):
+    """Join as a DC."""
     ctx = dc_join(server, creds, lp, site, netbios_name, targetdir, parent_domain,
                   machinepass, use_ntvfs, dns_backend)
     ctx.subdomain = True