selftest: Set NSS_WRAPPER_HOSTS when creating the trusts
[nivanova/samba-autobuild/.git] / selftest / target / Samba4.pm
index 9d0b7496e7f4f942b8813d71530b5c14a9e39d9e..4f3ad0ed3bf17960a7b2a279f6958e58545122f0 100755 (executable)
@@ -110,7 +110,7 @@ sub check_or_start($$$)
                }
        }
 
-       print "STARTING SAMBA...";
+       print "STARTING SAMBA...\n";
        my $pid = fork();
        if ($pid == 0) {
                # we want out from samba to go to the log file, but also
@@ -122,6 +122,10 @@ sub check_or_start($$$)
                SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
 
                $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
+               $ENV{KRB5CCNAME} = "$env_vars->{KRB5_CCACHE}.samba";
+               if (defined($ENV{MITKRB5})) {
+                       $ENV{KRB5_KDC_PROFILE} = $env_vars->{MITKDC_CONFIG};
+               }
                $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
                $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
 
@@ -154,7 +158,7 @@ sub check_or_start($$$)
                close($env_vars->{STDIN_PIPE});
                open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
 
-               exec(@preargs, Samba::bindir_path($self, "samba"), "-M", $process_model, "-i", "--maximum-runtime=$self->{server_maxtime}", $env_vars->{CONFIGURATION}, @optargs) or die("Unable to start samba: $!");
+               exec(@preargs, Samba::bindir_path($self, "samba"), "-M", $process_model, "-i", "--no-process-group", "--maximum-runtime=$self->{server_maxtime}", $env_vars->{CONFIGURATION}, @optargs) or die("Unable to start samba: $!");
        }
        $env_vars->{SAMBA_PID} = $pid;
        print "DONE ($pid)\n";
@@ -172,6 +176,7 @@ sub check_or_start($$$)
 sub wait_for_start($$)
 {
        my ($self, $testenv_vars) = @_;
+       my $count = 0;
        my $ret = 0;
 
        if (not $self->check_env($testenv_vars)) {
@@ -179,54 +184,95 @@ sub wait_for_start($$)
            return -1;
        }
 
-       # give time for nbt server to register its names
-       print "delaying for nbt name registration\n";
-       sleep 2;
-
        # This will return quickly when things are up, but be slow if we
        # need to wait for (eg) SSL init
        my $nmblookup =  Samba::bindir_path($self, "nmblookup4");
-       system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
-       system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
-       system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
-       system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
-       system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
-       system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
-       system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
-       system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
-       system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
-       system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
-       system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
-       system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
+
+       do {
+               $ret = system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
+               if ($ret != 0) {
+                       sleep(1);
+               } else {
+                       system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
+                       system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
+                       system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
+                       system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
+                       system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
+                       system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
+                       system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
+                       system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
+                       system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
+                       system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
+                       system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
+               }
+               $count++;
+       } while ($ret != 0 && $count < 20);
+       if ($count == 20) {
+               warn("nbt not reachable after 20 retries\n");
+               teardown_env($self, $testenv_vars);
+               return 0;
+       }
 
        # Ensure we have the first RID Set before we start tests.  This makes the tests more reliable.
-       if ($testenv_vars->{SERVER_ROLE} eq "domain controller" and not ($testenv_vars->{NETBIOSNAME} eq "RODC")) {
-           # Add hosts file for name lookups
-           $ENV{NSS_WRAPPER_HOSTS} = $testenv_vars->{NSS_WRAPPER_HOSTS};
+       if ($testenv_vars->{SERVER_ROLE} eq "domain controller") {
+               print "waiting for working LDAP and a RID Set to be allocated\n";
+               my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
+               my $count = 0;
+               my $base_dn = "DC=".join(",DC=", split(/\./, $testenv_vars->{REALM}));
+
+               my $search_dn = $base_dn;
+               if ($testenv_vars->{NETBIOSNAME} ne "RODC") {
+                       # TODO currently no check for actual rIDAllocationPool
+                       $search_dn = "cn=RID Set,cn=$testenv_vars->{NETBIOSNAME},ou=domain controllers,$base_dn";
+               }
+               my $max_wait = 60;
+
+               # Add hosts file for name lookups
+               my $cmd = "NSS_WRAPPER_HOSTS='$testenv_vars->{NSS_WRAPPER_HOSTS}' ";
                if (defined($testenv_vars->{RESOLV_WRAPPER_CONF})) {
-                       $ENV{RESOLV_WRAPPER_CONF} = $testenv_vars->{RESOLV_WRAPPER_CONF};
+                       $cmd .= "RESOLV_WRAPPER_CONF='$testenv_vars->{RESOLV_WRAPPER_CONF}' ";
                } else {
-                       $ENV{RESOLV_WRAPPER_HOSTS} = $testenv_vars->{RESOLV_WRAPPER_HOSTS};
+                       $cmd .= "RESOLV_WRAPPER_HOSTS='$testenv_vars->{RESOLV_WRAPPER_HOSTS}' ";
                }
 
-           print "waiting for working LDAP and a RID Set to be allocated\n";
-           my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
-           my $count = 0;
-           my $base_dn = "DC=".join(",DC=", split(/\./, $testenv_vars->{REALM}));
-           my $rid_set_dn = "cn=RID Set,cn=$testenv_vars->{NETBIOSNAME},ou=domain controllers,$base_dn";
-           my $max_wait = 60;
-           my $cmd = "$ldbsearch $testenv_vars->{CONFIGURATION} -H ldap://$testenv_vars->{SERVER} -U$testenv_vars->{USERNAME}%$testenv_vars->{PASSWORD} -s base -b \"$rid_set_dn\" rIDAllocationPool";
-           sleep(1);
-           while (system("$cmd >/dev/null") != 0) {
-               $count++;
-               if ($count > $max_wait) {
-                   warn("Timed out ($max_wait sec) waiting for working LDAP and a RID Set to be allocated by $testenv_vars->{NETBIOSNAME} PID $testenv_vars->{SAMBA_PID}");
-                   $ret = -1;
-                   last;
+               $cmd .= "$ldbsearch ";
+               $cmd .= "$testenv_vars->{CONFIGURATION} ";
+               $cmd .= "-H ldap://$testenv_vars->{SERVER} ";
+               $cmd .= "-U$testenv_vars->{USERNAME}%$testenv_vars->{PASSWORD} ";
+               $cmd .= "-s base ";
+               $cmd .= "-b '$search_dn' ";
+               while (system("$cmd >/dev/null") != 0) {
+                       $count++;
+                       if ($count > $max_wait) {
+                               warn("Timed out ($max_wait sec) waiting for working LDAP and a RID Set to be allocated by $testenv_vars->{NETBIOSNAME} PID $testenv_vars->{SAMBA_PID}");
+                               $ret = -1;
+                               last;
+                       }
+                       sleep(1);
                }
-               sleep(1);
-           }
        }
+
+       my $wbinfo =  Samba::bindir_path($self, "wbinfo");
+
+       $count = 0;
+       do {
+               my $cmd = "NSS_WRAPPER_PASSWD=$testenv_vars->{NSS_WRAPPER_PASSWD} ";
+               $cmd .= "NSS_WRAPPER_GROUP=$testenv_vars->{NSS_WRAPPER_GROUP} ";
+               $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=$testenv_vars->{SELFTEST_WINBINDD_SOCKET_DIR} ";
+               $cmd .= "$wbinfo -P";
+               $ret = system($cmd);
+
+               if ($ret != 0) {
+                       sleep(1);
+               }
+               $count++;
+       } while ($ret != 0 && $count < 20);
+       if ($count == 20) {
+               warn("winbind not reachable after 20 retries\n");
+               teardown_env($self, $testenv_vars);
+               return 0;
+       }
+
        print $self->getlog_env($testenv_vars);
 
        return $ret
@@ -297,14 +343,15 @@ sub setup_namespaces($$:$$)
 
        my $samba_tool =  Samba::bindir_path($self, "samba-tool");
 
-       my $cmd_env = "";
+       my $cmd_env = "NSS_WRAPPER_HOSTS='$localenv->{NSS_WRAPPER_HOSTS}' ";
        $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$localenv->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        if (defined($localenv->{RESOLV_WRAPPER_CONF})) {
                $cmd_env .= "RESOLV_WRAPPER_CONF=\"$localenv->{RESOLV_WRAPPER_CONF}\" ";
        } else {
                $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$localenv->{RESOLV_WRAPPER_HOSTS}\" ";
        }
-       $cmd_env .= " KRB5_CONFIG=\"$localenv->{KRB5_CONFIG}\"";
+       $cmd_env .= " KRB5_CONFIG=\"$localenv->{KRB5_CONFIG}\" ";
+       $cmd_env .= "KRB5CCNAME=\"$localenv->{KRB5_CCACHE}\" ";
 
        my $cmd_config = " $localenv->{CONFIGURATION}";
 
@@ -331,17 +378,19 @@ sub setup_trust($$$$$)
        $localenv->{TRUST_PASSWORD} = $remoteenv->{PASSWORD};
        $localenv->{TRUST_DOMAIN} = $remoteenv->{DOMAIN};
        $localenv->{TRUST_REALM} = $remoteenv->{REALM};
+       $localenv->{TRUST_DOMSID} = $remoteenv->{DOMSID};
 
        my $samba_tool =  Samba::bindir_path($self, "samba-tool");
        # setup the trust
-       my $cmd_env = "";
+       my $cmd_env = "NSS_WRAPPER_HOSTS='$localenv->{NSS_WRAPPER_HOSTS}' ";
        $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$localenv->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        if (defined($localenv->{RESOLV_WRAPPER_CONF})) {
                $cmd_env .= "RESOLV_WRAPPER_CONF=\"$localenv->{RESOLV_WRAPPER_CONF}\" ";
        } else {
                $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$localenv->{RESOLV_WRAPPER_HOSTS}\" ";
        }
-       $cmd_env .= " KRB5_CONFIG=\"$localenv->{KRB5_CONFIG}\"";
+       $cmd_env .= " KRB5_CONFIG=\"$localenv->{KRB5_CONFIG}\" ";
+       $cmd_env .= "KRB5CCNAME=\"$localenv->{KRB5_CCACHE}\" ";
 
        my $cmd_config = " $localenv->{CONFIGURATION}";
        my $cmd_creds = $cmd_config;
@@ -356,13 +405,27 @@ sub setup_trust($$$$$)
                return undef;
        }
 
+       my $groupname = "g_$localenv->{TRUST_DOMAIN}";
+       my $groupadd = $cmd_env;
+       $groupadd .= " $samba_tool group add '$groupname' --group-scope=Domain $cmd_config";
+       unless (system($groupadd) == 0) {
+               warn("Failed to create group \n$groupadd");
+               return undef;
+       }
+       my $groupmem = $cmd_env;
+       $groupmem .= " $samba_tool group addmembers '$groupname' '$localenv->{TRUST_DOMSID}-513' $cmd_config";
+       unless (system($groupmem) == 0) {
+               warn("Failed to add group member \n$groupmem");
+               return undef;
+       }
+
        return $localenv
 }
 
-sub provision_raw_prepare($$$$$$$$$$$)
+sub provision_raw_prepare($$$$$$$$$$$$)
 {
        my ($self, $prefix, $server_role, $hostname,
-           $domain, $realm, $functional_level,
+           $domain, $realm, $samsid, $functional_level,
            $password, $kdc_ipv4, $kdc_ipv6) = @_;
        my $ctx;
        my $netbiosname = uc($hostname);
@@ -393,6 +456,7 @@ sub provision_raw_prepare($$$$$$$$$$$)
        $ctx->{password} = $password;
        $ctx->{kdc_ipv4} = $kdc_ipv4;
        $ctx->{kdc_ipv6} = $kdc_ipv6;
+       $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
        if ($functional_level eq "2000") {
                $ctx->{supported_enctypes} = "arcfour-hmac-md5 des-cbc-md5 des-cbc-crc"
        }
@@ -405,6 +469,7 @@ sub provision_raw_prepare($$$$$$$$$$$)
        $ctx->{domain} = $domain;
        $ctx->{realm} = uc($realm);
        $ctx->{dnsname} = lc($realm);
+       $ctx->{samsid} = $samsid;
 
        $ctx->{functional_level} = $functional_level;
 
@@ -421,14 +486,16 @@ sub provision_raw_prepare($$$$$$$$$$$)
        $ctx->{piddir} = "$prefix_abs/pid";
        $ctx->{smb_conf} = "$ctx->{etcdir}/smb.conf";
        $ctx->{krb5_conf} = "$ctx->{etcdir}/krb5.conf";
+       $ctx->{krb5_ccache} = "$prefix_abs/krb5_ccache";
+       $ctx->{mitkdc_conf} = "$ctx->{etcdir}/mitkdc.conf";
        $ctx->{privatedir} = "$prefix_abs/private";
+       $ctx->{binddnsdir} = "$prefix_abs/bind-dns";
        $ctx->{ncalrpcdir} = "$prefix_abs/ncalrpc";
        $ctx->{lockdir} = "$prefix_abs/lockdir";
        $ctx->{logdir} = "$prefix_abs/logs";
        $ctx->{statedir} = "$prefix_abs/statedir";
        $ctx->{cachedir} = "$prefix_abs/cachedir";
        $ctx->{winbindd_socket_dir} = "$prefix_abs/winbindd_socket";
-       $ctx->{winbindd_privileged_socket_dir} = "$prefix_abs/winbindd_privileged_socket";
        $ctx->{ntp_signd_socket_dir} = "$prefix_abs/ntp_signd_socket";
        $ctx->{nsswrap_passwd} = "$ctx->{etcdir}/passwd";
        $ctx->{nsswrap_group} = "$ctx->{etcdir}/group";
@@ -450,6 +517,7 @@ sub provision_raw_prepare($$$$$$$$$$$)
        $ctx->{interfaces} = "$ctx->{ipv4}/8 $ctx->{ipv6}/64";
 
        push(@{$ctx->{directories}}, $ctx->{privatedir});
+       push(@{$ctx->{directories}}, $ctx->{binddnsdir});
        push(@{$ctx->{directories}}, $ctx->{etcdir});
        push(@{$ctx->{directories}}, $ctx->{piddir});
        push(@{$ctx->{directories}}, $ctx->{lockdir});
@@ -460,7 +528,8 @@ sub provision_raw_prepare($$$$$$$$$$$)
        $ctx->{smb_conf_extra_options} = "";
 
        my @provision_options = ();
-       push (@provision_options, "KRB5_CONFIG=\"$ctx->{krb5_config}\"");
+       push (@provision_options, "KRB5_CONFIG=\"$ctx->{krb5_conf}\"");
+       push (@provision_options, "KRB5_CCACHE=\"$ctx->{krb5_ccache}\"");
        push (@provision_options, "NSS_WRAPPER_PASSWD=\"$ctx->{nsswrap_passwd}\"");
        push (@provision_options, "NSS_WRAPPER_GROUP=\"$ctx->{nsswrap_group}\"");
        push (@provision_options, "NSS_WRAPPER_HOSTS=\"$ctx->{nsswrap_hosts}\"");
@@ -496,6 +565,9 @@ sub provision_raw_prepare($$$$$$$$$$$)
        push (@provision_options, "--quiet");
        push (@provision_options, "--domain=$ctx->{domain}");
        push (@provision_options, "--realm=$ctx->{realm}");
+       if (defined($ctx->{samsid})) {
+               push (@provision_options, "--domain-sid=$ctx->{samsid}");
+       }
        push (@provision_options, "--adminpass=$ctx->{password}");
        push (@provision_options, "--krbtgtpass=krbtgt$ctx->{password}");
        push (@provision_options, "--machinepass=machine$ctx->{password}");
@@ -539,13 +611,13 @@ sub provision_raw_step1($$)
        workgroup = $ctx->{domain}
        realm = $ctx->{realm}
        private dir = $ctx->{privatedir}
+       binddns dir = $ctx->{binddnsdir}
        pid directory = $ctx->{piddir}
        ncalrpc dir = $ctx->{ncalrpcdir}
        lock dir = $ctx->{lockdir}
        state directory = $ctx->{statedir}
        cache directory = $ctx->{cachedir}
        winbindd socket directory = $ctx->{winbindd_socket_dir}
-       winbindd privileged socket directory = $ctx->{winbindd_privileged_socket_dir}
        ntp signd socket directory = $ctx->{ntp_signd_socket_dir}
        winbind separator = /
        interfaces = $ctx->{interfaces}
@@ -565,17 +637,24 @@ sub provision_raw_step1($$)
        log file = $ctx->{logdir}/log.\%m
        log level = $ctx->{server_loglevel}
        lanman auth = Yes
+       ntlm auth = Yes
        rndc command = true
        dns update command = $ctx->{samba_dnsupdate}
        spn update command = $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_spnupdate -s $ctx->{smb_conf}
+       gpo update command = $ENV{SRCDIR_ABS}/source4/scripting/bin/samba-gpupdate -s $ctx->{smb_conf} -H $ctx->{privatedir}/sam.ldb --target=Computer
        dreplsrv:periodic_startup_interval = 0
        dsdb:schema update allowed = yes
 
+        prefork children = 4
+
         vfs objects = dfs_samba4 acl_xattr fake_acls xattr_tdb streams_depot
 
         idmap_ldb:use rfc2307=yes
        winbind enum users = yes
        winbind enum groups = yes
+
+        rpc server port:netlogon = 1026
+
 ";
 
        print CONFFILE "
@@ -595,6 +674,7 @@ sub provision_raw_step1($$)
        }
 
        Samba::mk_krb5_conf($ctx);
+       Samba::mk_mitkdc_conf($ctx, abs_path(Samba::bindir_path($self, "shared")));
 
        open(PWD, ">$ctx->{nsswrap_passwd}");
        if ($ctx->{unix_uid} != 0) {
@@ -657,6 +737,8 @@ nogroup:x:65534:nobody
 
        my $ret = {
                KRB5_CONFIG => $ctx->{krb5_conf},
+               KRB5_CCACHE => $ctx->{krb5_ccache},
+               MITKDC_CONFIG => $ctx->{mitkdc_conf},
                PIDDIR => $ctx->{piddir},
                SERVER => $ctx->{hostname},
                SERVER_IP => $ctx->{ipv4},
@@ -665,6 +747,7 @@ nogroup:x:65534:nobody
                DOMAIN => $ctx->{domain},
                USERNAME => $ctx->{username},
                REALM => $ctx->{realm},
+               SAMSID => $ctx->{samsid},
                PASSWORD => $ctx->{password},
                LDAPDIR => $ctx->{ldapdir},
                LDAP_INSTANCE => $ctx->{ldap_instance},
@@ -674,6 +757,7 @@ nogroup:x:65534:nobody
                STATEDIR => $ctx->{statedir},
                CACHEDIR => $ctx->{cachedir},
                PRIVATEDIR => $ctx->{privatedir},
+               BINDDNSDIR => $ctx->{binddnsdir},
                SERVERCONFFILE => $ctx->{smb_conf},
                CONFIGURATION => $configuration,
                SOCKET_WRAPPER_DEFAULT_IFACE => $ctx->{swiface},
@@ -699,6 +783,10 @@ nogroup:x:65534:nobody
                $ret->{RESOLV_WRAPPER_HOSTS} = $ctx->{dns_host_file};
        }
 
+       if ($ctx->{server_role} eq "domain controller") {
+               $ret->{DOMSID} = $ret->{SAMSID};
+       }
+
        return $ret;
 }
 
@@ -718,8 +806,9 @@ sub provision_raw_step2($$$)
        my $testallowed_account = "testallowed";
        my $samba_tool_cmd = "";
        $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
-           . " user add --configfile=$ctx->{smb_conf} $testallowed_account $ctx->{password}";
+           . " user create --configfile=$ctx->{smb_conf} $testallowed_account $ctx->{password}";
        unless (system($samba_tool_cmd) == 0) {
                warn("Unable to add testallowed user: \n$samba_tool_cmd\n");
                return undef;
@@ -727,6 +816,7 @@ sub provision_raw_step2($$$)
 
        my $ldbmodify = "";
        $ldbmodify .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $ldbmodify .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $ldbmodify .= Samba::bindir_path($self, "ldbmodify");
        my $base_dn = "DC=".join(",DC=", split(/\./, $ctx->{realm}));
 
@@ -758,8 +848,9 @@ servicePrincipalName: host/testallowed
 
        $samba_tool_cmd = "";
        $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
-           . " user add --configfile=$ctx->{smb_conf} testdenied $ctx->{password}";
+           . " user create --configfile=$ctx->{smb_conf} testdenied $ctx->{password}";
        unless (system($samba_tool_cmd) == 0) {
                warn("Unable to add testdenied user: \n$samba_tool_cmd\n");
                return undef;
@@ -777,6 +868,7 @@ userPrincipalName: testdenied_upn\@$ctx->{realm}.upn
 
        $samba_tool_cmd = "";
        $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
            . " group addmembers --configfile=$ctx->{smb_conf} 'Allowed RODC Password Replication Group' '$testallowed_account'";
        unless (system($samba_tool_cmd) == 0) {
@@ -784,6 +876,39 @@ userPrincipalName: testdenied_upn\@$ctx->{realm}.upn
                return undef;
        }
 
+       # Create to users alice and bob!
+       my $user_account_array = ["alice", "bob", "jane"];
+
+       foreach my $user_account (@{$user_account_array}) {
+               my $samba_tool_cmd = "";
+
+               $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+               $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
+               $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
+                   . " user create --configfile=$ctx->{smb_conf} $user_account Secret007";
+               unless (system($samba_tool_cmd) == 0) {
+                       warn("Unable to create user: $user_account\n$samba_tool_cmd\n");
+                       return undef;
+               }
+       }
+
+       my $ldbmodify = "";
+       $ldbmodify .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $ldbmodify .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
+       $ldbmodify .= Samba::bindir_path($self, "ldbmodify");
+
+       my $base_dn = "DC=".join(",DC=", split(/\./, $ctx->{realm}));
+       my $user_dn = "cn=jane,cn=users,$base_dn";
+
+       open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
+       print LDIF "dn: $user_dn
+changetype: modify
+replace: userPrincipalName
+userPrincipalName: jane.doe\@$ctx->{realm}
+-
+";
+       close(LDIF);
+
        return $ret;
 }
 
@@ -794,9 +919,13 @@ sub provision($$$$$$$$$$)
            $password, $kdc_ipv4, $kdc_ipv6, $extra_smbconf_options, $extra_smbconf_shares,
            $extra_provision_options) = @_;
 
+       my $samsid = Samba::random_domain_sid();
+
        my $ctx = $self->provision_raw_prepare($prefix, $server_role,
                                               $hostname,
-                                              $domain, $realm, $functional_level,
+                                              $domain, $realm,
+                                              $samsid,
+                                              $functional_level,
                                               $password, $kdc_ipv4, $kdc_ipv6);
 
        if (defined($extra_provision_options)) {
@@ -823,7 +952,6 @@ sub provision($$$$$$$$$$)
        server max protocol = SMB2
        host msdfs = $msdfs
        lanman auth = yes
-       allow nt4 crypto = yes
 
        # fruit:copyfile is a global option
        fruit:copyfile = yes
@@ -900,7 +1028,7 @@ sub provision($$$$$$$$$$)
        path = $ctx->{share}
        vfs objects = catia fruit streams_xattr acl_xattr
        ea support = yes
-       fruit:ressource = file
+       fruit:resource = file
        fruit:metadata = netatalk
        fruit:locking = netatalk
        fruit:encoding = native
@@ -944,16 +1072,17 @@ $extra_smbconf_shares
        return $self->provision_raw_step2($ctx, $ret);
 }
 
-sub provision_s4member($$$)
+sub provision_s4member($$$$$)
 {
-       my ($self, $prefix, $dcvars) = @_;
-       print "PROVISIONING MEMBER...";
+       my ($self, $prefix, $dcvars, $hostname, $more_conf) = @_;
+       print "PROVISIONING MEMBER...\n";
        my $extra_smb_conf = "
         passdb backend = samba_dsdb
 winbindd:use external pipes = true
 
 # the source4 smb server doesn't allow signing by default
 server signing = enabled
+raw NTLMv2 auth = yes
 
 rpc_server:default = external
 rpc_server:svcctl = embedded
@@ -965,11 +1094,14 @@ rpc_server:spoolss = embedded
 rpc_daemon:spoolssd = embedded
 rpc_server:tcpip = no
 ";
+       if ($more_conf) {
+               $extra_smb_conf = $extra_smb_conf . $more_conf . "\n";
+       }
        my $ret = $self->provision($prefix,
                                   "member server",
-                                  "s4member",
-                                  "SAMBADOMAIN",
-                                  "samba.example.com",
+                                  $hostname,
+                                  $dcvars->{DOMAIN},
+                                  $dcvars->{REALM},
                                   "2008",
                                   "locMEMpass3",
                                   $dcvars->{SERVER_IP},
@@ -980,7 +1112,7 @@ rpc_server:tcpip = no
        }
 
        my $samba_tool =  Samba::bindir_path($self, "samba-tool");
-       my $cmd = "";
+       my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
        $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        if (defined($ret->{RESOLV_WRAPPER_CONF})) {
                $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
@@ -988,6 +1120,7 @@ rpc_server:tcpip = no
                $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
        }
        $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
        $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
        $cmd .= " --machinepass=machine$ret->{PASSWORD}";
@@ -1004,6 +1137,7 @@ rpc_server:tcpip = no
        $ret->{MEMBER_USERNAME} = $ret->{USERNAME};
        $ret->{MEMBER_PASSWORD} = $ret->{PASSWORD};
 
+       $ret->{DOMSID} = $dcvars->{DOMSID};
        $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
        $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
        $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
@@ -1017,7 +1151,7 @@ rpc_server:tcpip = no
 sub provision_rpc_proxy($$$)
 {
        my ($self, $prefix, $dcvars) = @_;
-       print "PROVISIONING RPC PROXY...";
+       print "PROVISIONING RPC PROXY...\n";
 
        my $extra_smbconf_options = "
         passdb backend = samba_dsdb
@@ -1043,14 +1177,13 @@ sub provision_rpc_proxy($$$)
        my $ret = $self->provision($prefix,
                                   "member server",
                                   "localrpcproxy",
-                                  "SAMBADOMAIN",
-                                  "samba.example.com",
+                                  $dcvars->{DOMAIN},
+                                  $dcvars->{REALM},
                                   "2008",
                                   "locRPCproxypass4",
                                   $dcvars->{SERVER_IP},
                                   $dcvars->{SERVER_IPV6},
                                   $extra_smbconf_options, "", undef);
-
        unless ($ret) {
                return undef;
        }
@@ -1058,7 +1191,7 @@ sub provision_rpc_proxy($$$)
        my $samba_tool =  Samba::bindir_path($self, "samba-tool");
 
        # The joind runs in the context of the rpc_proxy/member for now
-       my $cmd = "";
+       my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
        $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        if (defined($ret->{RESOLV_WRAPPER_CONF})) {
                $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
@@ -1066,6 +1199,7 @@ sub provision_rpc_proxy($$$)
                $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
        }
        $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
        $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
        $cmd .= " --machinepass=machine$ret->{PASSWORD}";
@@ -1079,6 +1213,7 @@ sub provision_rpc_proxy($$$)
        $cmd = "";
        $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
+       $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $cmd .= "$samba_tool delegation for-any-protocol '$ret->{NETBIOSNAME}\$' on";
         $cmd .= " $dcvars->{CONFIGURATION}";
         print $cmd;
@@ -1092,6 +1227,7 @@ sub provision_rpc_proxy($$$)
        $cmd = "";
        $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
+       $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $cmd .= "$samba_tool delegation add-service '$ret->{NETBIOSNAME}\$' cifs/$dcvars->{SERVER}";
         $cmd .= " $dcvars->{CONFIGURATION}";
 
@@ -1107,6 +1243,7 @@ sub provision_rpc_proxy($$$)
        $ret->{RPC_PROXY_USERNAME} = $ret->{USERNAME};
        $ret->{RPC_PROXY_PASSWORD} = $ret->{PASSWORD};
 
+       $ret->{DOMSID} = $dcvars->{DOMSID};
        $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
        $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
        $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
@@ -1120,13 +1257,14 @@ sub provision_rpc_proxy($$$)
 sub provision_promoted_dc($$$)
 {
        my ($self, $prefix, $dcvars) = @_;
-       print "PROVISIONING PROMOTED DC...";
+       print "PROVISIONING PROMOTED DC...\n";
 
        # We do this so that we don't run the provision.  That's the job of 'samba-tool domain dcpromo'.
        my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
                                               "promotedvdc",
-                                              "SAMBADOMAIN",
-                                              "samba.example.com",
+                                              $dcvars->{DOMAIN},
+                                              $dcvars->{REALM},
+                                              $dcvars->{SAMSID},
                                               "2008",
                                               $dcvars->{PASSWORD},
                                               $dcvars->{SERVER_IP},
@@ -1138,6 +1276,8 @@ sub provision_promoted_dc($$$)
        max xmit = 32K
        server max protocol = SMB2
 
+        ntlm auth = ntlmv2-only
+
 [sysvol]
        path = $ctx->{statedir}/sysvol
        read only = yes
@@ -1154,7 +1294,7 @@ sub provision_promoted_dc($$$)
        }
 
        my $samba_tool =  Samba::bindir_path($self, "samba-tool");
-       my $cmd = "";
+       my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
        $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        if (defined($ret->{RESOLV_WRAPPER_CONF})) {
                $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
@@ -1162,6 +1302,7 @@ sub provision_promoted_dc($$$)
                $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
        }
        $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} MEMBER --realm=$dcvars->{REALM}";
        $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
        $cmd .= " --machinepass=machine$ret->{PASSWORD}";
@@ -1172,9 +1313,10 @@ sub provision_promoted_dc($$$)
        }
 
        my $samba_tool =  Samba::bindir_path($self, "samba-tool");
-       my $cmd = "";
+       my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
        $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $cmd .= "$samba_tool domain dcpromo $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
        $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
        $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs --dns-backend=BIND9_DLZ";
@@ -1201,15 +1343,25 @@ sub provision_promoted_dc($$$)
 
 sub provision_vampire_dc($$$)
 {
-       my ($self, $prefix, $dcvars) = @_;
-       print "PROVISIONING VAMPIRE DC...";
+       my ($self, $prefix, $dcvars, $fl) = @_;
+       print "PROVISIONING VAMPIRE DC @ FL $fl...\n";
+       my $name = "localvampiredc";
+       my $extra_conf = "";
+
+       if ($fl == "2000") {
+               $name = "vampire2000dc";
+       } else {
+               $extra_conf = "drs: immediate link sync = yes
+                       drs: max link sync = 250";
+       }
 
        # We do this so that we don't run the provision.  That's the job of 'net vampire'.
        my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
-                                              "localvampiredc",
-                                              "SAMBADOMAIN",
-                                              "samba.example.com",
-                                              "2008",
+                                              $name,
+                                              $dcvars->{DOMAIN},
+                                              $dcvars->{REALM},
+                                              $dcvars->{DOMSID},
+                                              $fl,
                                               $dcvars->{PASSWORD},
                                               $dcvars->{SERVER_IP},
                                               $dcvars->{SERVER_IPV6});
@@ -1220,6 +1372,9 @@ sub provision_vampire_dc($$$)
        max xmit = 32K
        server max protocol = SMB2
 
+        ntlm auth = mschapv2-and-ntlmv2-only
+       $extra_conf
+
 [sysvol]
        path = $ctx->{statedir}/sysvol
        read only = yes
@@ -1236,7 +1391,7 @@ sub provision_vampire_dc($$$)
        }
 
        my $samba_tool =  Samba::bindir_path($self, "samba-tool");
-       my $cmd = "";
+       my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
        $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        if (defined($ret->{RESOLV_WRAPPER_CONF})) {
                $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
@@ -1244,20 +1399,28 @@ sub provision_vampire_dc($$$)
                $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
        }
        $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
        $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} --domain-critical-only";
        $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
+       $cmd .= " --backend-store=mdb";
 
        unless (system($cmd) == 0) {
                warn("Join failed\n$cmd");
                return undef;
        }
 
-       $ret->{VAMPIRE_DC_SERVER} = $ret->{SERVER};
-       $ret->{VAMPIRE_DC_SERVER_IP} = $ret->{SERVER_IP};
-       $ret->{VAMPIRE_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
-       $ret->{VAMPIRE_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
-
+        if ($fl == "2000") {
+               $ret->{VAMPIRE_2000_DC_SERVER} = $ret->{SERVER};
+               $ret->{VAMPIRE_2000_DC_SERVER_IP} = $ret->{SERVER_IP};
+               $ret->{VAMPIRE_2000_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
+               $ret->{VAMPIRE_2000_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
+        } else {
+               $ret->{VAMPIRE_DC_SERVER} = $ret->{SERVER};
+               $ret->{VAMPIRE_DC_SERVER_IP} = $ret->{SERVER_IP};
+               $ret->{VAMPIRE_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
+               $ret->{VAMPIRE_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
+        }
        $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
        $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
        $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
@@ -1272,13 +1435,15 @@ sub provision_vampire_dc($$$)
 sub provision_subdom_dc($$$)
 {
        my ($self, $prefix, $dcvars) = @_;
-       print "PROVISIONING SUBDOMAIN DC...";
+       print "PROVISIONING SUBDOMAIN DC...\n";
 
        # We do this so that we don't run the provision.  That's the job of 'net vampire'.
+       my $samsid = undef; # TODO pass the domain sid all the way down
        my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
                                               "localsubdc",
                                               "SAMBASUBDOM",
                                               "sub.samba.example.com",
+                                              $samsid,
                                               "2008",
                                               $dcvars->{PASSWORD},
                                               undef);
@@ -1305,9 +1470,10 @@ sub provision_subdom_dc($$$)
        }
 
        Samba::mk_krb5_conf($ctx);
+       Samba::mk_mitkdc_conf($ctx, abs_path(Samba::bindir_path($self, "shared")));
 
        my $samba_tool =  Samba::bindir_path($self, "samba-tool");
-       my $cmd = "";
+       my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
        $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        if (defined($ret->{RESOLV_WRAPPER_CONF})) {
                $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
@@ -1315,6 +1481,7 @@ sub provision_subdom_dc($$$)
                $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
        }
        $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $ctx->{dnsname} subdomain ";
        $cmd .= "--parent-domain=$dcvars->{REALM} -U$dcvars->{DC_USERNAME}\@$dcvars->{REALM}\%$dcvars->{DC_PASSWORD}";
        $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
@@ -1348,10 +1515,19 @@ sub provision_ad_dc_ntvfs($$)
        # ensure upgrades which used that name still work with the now
        # alias.
 
-       print "PROVISIONING AD DC (NTVFS)...";
+       print "PROVISIONING AD DC (NTVFS)...\n";
         my $extra_conf_options = "netbios aliases = localDC1-a
         server services = +winbind -winbindd
        ldap server require strong auth = allow_sasl_over_tls
+       allow nt4 crypto = yes
+       raw NTLMv2 auth = yes
+       lsa over netlogon = yes
+        rpc server port = 1027
+        auth event notification = true
+       dsdb event notification = true
+       dsdb password event notification = true
+       dsdb group change notification = true
+       server schannel = auto
        ";
        my $ret = $self->provision($prefix,
                                   "domain controller",
@@ -1365,8 +1541,10 @@ sub provision_ad_dc_ntvfs($$)
                                   $extra_conf_options,
                                   "",
                                   undef);
+       unless ($ret) {
+               return undef;
+       }
 
-       return undef unless(defined $ret);
        unless($self->add_wins_config("$prefix/private")) {
                warn("Unable to add wins configuration");
                return undef;
@@ -1387,11 +1565,17 @@ sub provision_fl2000dc($$)
 {
        my ($self, $prefix) = @_;
 
-       print "PROVISIONING DC WITH FOREST LEVEL 2000...";
+       print "PROVISIONING DC WITH FOREST LEVEL 2000...\n";
        my $extra_conf_options = "
        spnego:simulate_w2k=yes
        ntlmssp_server:force_old_spnego=yes
 ";
+       my $extra_provision_options = undef;
+       # This environment uses plain text secrets
+       # i.e. secret attributes are not encrypted on disk.
+       # This allows testing of the --plaintext-secrets option for
+       # provision
+       push (@{$extra_provision_options}, "--plaintext-secrets");
        my $ret = $self->provision($prefix,
                                   "domain controller",
                                   "dc5",
@@ -1403,7 +1587,10 @@ sub provision_fl2000dc($$)
                                   undef,
                                   $extra_conf_options,
                                   "",
-                                  undef);
+                                  $extra_provision_options);
+       unless ($ret) {
+               return undef;
+       }
 
        unless($self->add_wins_config("$prefix/private")) {
                warn("Unable to add wins configuration");
@@ -1426,8 +1613,9 @@ sub provision_fl2003dc($$$)
        my $swiface1 = Samba::get_interface("fakednsforwarder1");
        my $swiface2 = Samba::get_interface("fakednsforwarder2");
 
-       print "PROVISIONING DC WITH FOREST LEVEL 2003...";
+       print "PROVISIONING DC WITH FOREST LEVEL 2003...\n";
        my $extra_conf_options = "allow dns updates = nonsecure and secure
+       dcesrv:header signing = no
        dns forwarder = 127.0.0.$swiface1 127.0.0.$swiface2";
        my $ret = $self->provision($prefix,
                                   "domain controller",
@@ -1441,7 +1629,6 @@ sub provision_fl2003dc($$$)
                                   $extra_conf_options,
                                   "",
                                   undef);
-
        unless (defined $ret) {
                return undef;
        }
@@ -1483,7 +1670,7 @@ sub provision_fl2008r2dc($$$)
 {
        my ($self, $prefix, $dcvars) = @_;
 
-       print "PROVISIONING DC WITH FOREST LEVEL 2008r2...";
+       print "PROVISIONING DC WITH FOREST LEVEL 2008r2...\n";
         my $extra_conf_options = "ldap server require strong auth = no";
        my $ret = $self->provision($prefix,
                                   "domain controller",
@@ -1497,6 +1684,9 @@ sub provision_fl2008r2dc($$$)
                                   $extra_conf_options,
                                   "",
                                   undef);
+       unless (defined $ret) {
+               return undef;
+       }
 
        unless ($self->add_wins_config("$prefix/private")) {
                warn("Unable to add wins configuration");
@@ -1517,13 +1707,14 @@ sub provision_fl2008r2dc($$$)
 sub provision_rodc($$$)
 {
        my ($self, $prefix, $dcvars) = @_;
-       print "PROVISIONING RODC...";
+       print "PROVISIONING RODC...\n";
 
        # We do this so that we don't run the provision.  That's the job of 'net join RODC'.
        my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
                                               "rodc",
-                                              "SAMBADOMAIN",
-                                              "samba.example.com",
+                                              $dcvars->{DOMAIN},
+                                              $dcvars->{REALM},
+                                              $dcvars->{DOMSID},
                                               "2008",
                                               $dcvars->{PASSWORD},
                                               $dcvars->{SERVER_IP},
@@ -1540,6 +1731,7 @@ sub provision_rodc($$$)
        $ctx->{smb_conf_extra_options} = "
        max xmit = 32K
        server max protocol = SMB2
+       password server = $dcvars->{DC_SERVER}
 
 [sysvol]
        path = $ctx->{statedir}/sysvol
@@ -1564,7 +1756,7 @@ sub provision_rodc($$$)
        }
 
        my $samba_tool =  Samba::bindir_path($self, "samba-tool");
-       my $cmd = "";
+       my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
        $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        if (defined($ret->{RESOLV_WRAPPER_CONF})) {
                $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
@@ -1572,6 +1764,7 @@ sub provision_rodc($$$)
                $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
        }
        $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} RODC";
        $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
        $cmd .= " --server=$dcvars->{DC_SERVER} --use-ntvfs";
@@ -1585,6 +1778,7 @@ sub provision_rodc($$$)
         # user password verified on the RODC
        my $testallowed_account = "testallowed account";
        $cmd = "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
+       $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
        $cmd .= "$samba_tool rodc preload '$testallowed_account' $ret->{CONFIGURATION}";
        $cmd .= " --server=$dcvars->{DC_SERVER}";
 
@@ -1599,6 +1793,7 @@ sub provision_rodc($$$)
        $ctx->{kdc_ipv4} = $ret->{SERVER_IP};
        $ctx->{kdc_ipv6} = $ret->{SERVER_IPV6};
        Samba::mk_krb5_conf($ctx);
+       Samba::mk_mitkdc_conf($ctx, abs_path(Samba::bindir_path($self, "shared")));
 
        $ret->{RODC_DC_SERVER} = $ret->{SERVER};
        $ret->{RODC_DC_SERVER_IP} = $ret->{SERVER_IP};
@@ -1615,9 +1810,30 @@ sub provision_rodc($$$)
        return $ret;
 }
 
-sub provision_ad_dc($$)
+sub read_config_h($)
 {
-       my ($self, $prefix) = @_;
+       my ($name) = @_;
+       my %ret = {};
+       open(LF, "<$name") or die("unable to read $name: $!");
+       while (<LF>) {
+               chomp;
+               next if not (/^#define /);
+               if (/^#define (.*?)[ \t]+(.*?)$/) {
+                       $ret{$1} = $2;
+                       next;
+               }
+               if (/^#define (.*?)[ \t]+$/) {
+                       $ret{$1} = 1;;
+                       next;
+               }
+       }
+       close(LF);
+       return \%ret;
+}
+
+sub provision_ad_dc($$$$$$)
+{
+       my ($self, $prefix, $hostname, $domain, $realm, $smbconf_args) = @_;
 
        my $prefix_abs = abs_path($prefix);
 
@@ -1628,6 +1844,15 @@ sub provision_ad_dc($$)
        my $require_mutexes = "dbwrap_tdb_require_mutexes:* = yes";
        $require_mutexes = "" if ($ENV{SELFTEST_DONT_REQUIRE_TDB_MUTEX_SUPPORT} eq "1");
 
+       my $config_h = {};
+
+       if (defined($ENV{CONFIG_H})) {
+               $config_h = read_config_h($ENV{CONFIG_H});
+       }
+
+       my $password_hash_gpg_key_ids = "password hash gpg key ids = 4952E40301FAB41A";
+       $password_hash_gpg_key_ids = "" unless defined($config_h->{HAVE_GPGME});
+
        my $extra_smbconf_options = "
         server services = -smb +s3fs
         xattr_tdb:file = $prefix_abs/statedir/xattr.tdb
@@ -1635,8 +1860,11 @@ sub provision_ad_dc($$)
        dbwrap_tdb_mutexes:* = yes
        ${require_mutexes}
 
+       ${password_hash_gpg_key_ids}
+
        kernel oplocks = no
        kernel change notify = no
+       smb2 leases = no
 
        logging = file
        printing = bsd
@@ -1649,6 +1877,7 @@ sub provision_ad_dc($$)
        smbd:writetimeupdatedelay = 500000
        create mask = 755
        dos filemode = yes
+       check parent directory delete on close = yes
 
         dcerpc endpoint servers = -winreg -srvsvc
 
@@ -1667,6 +1896,13 @@ sub provision_ad_dc($$)
        queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
        lpq cache time = 0
        print notify backchannel = yes
+
+       server schannel = auto
+        auth event notification = true
+       dsdb event notification = true
+       dsdb password event notification = true
+       dsdb group change notification = true
+        $smbconf_args
 ";
 
        my $extra_smbconf_shares = "
@@ -1708,21 +1944,25 @@ sub provision_ad_dc($$)
        copy = print1
 ";
 
-       print "PROVISIONING AD DC...";
+       my $extra_provision_options = undef;
+       push (@{$extra_provision_options}, "--backend-store=mdb");
+       print "PROVISIONING AD DC...\n";
        my $ret = $self->provision($prefix,
                                   "domain controller",
-                                  "addc",
-                                  "ADDOMAIN",
-                                  "addom.samba.example.com",
+                                  $hostname,
+                                  $domain,
+                                  $realm,
                                   "2008",
                                   "locDCpass1",
                                   undef,
                                   undef,
                                   $extra_smbconf_options,
                                   $extra_smbconf_shares,
-                                  undef);
+                                  $extra_provision_options);
+       unless (defined $ret) {
+               return undef;
+       }
 
-       return undef unless(defined $ret);
        unless($self->add_wins_config("$prefix/private")) {
                warn("Unable to add wins configuration");
                return undef;
@@ -1742,8 +1982,11 @@ sub provision_chgdcpass($$)
 {
        my ($self, $prefix) = @_;
 
-       print "PROVISIONING CHGDCPASS...";
+       print "PROVISIONING CHGDCPASS...\n";
        my $extra_provision_options = undef;
+       # This environment disallows the use of this password
+       # (and also removes the default AD complexity checks)
+       my $unacceptable_password = "widk3Dsle32jxdBdskldsk55klASKQ";
        push (@{$extra_provision_options}, "--dns-backend=BIND9_DLZ");
        my $ret = $self->provision($prefix,
                                   "domain controller",
@@ -1754,11 +1997,13 @@ sub provision_chgdcpass($$)
                                   "chgDCpass1",
                                   undef,
                                   undef,
-                                  "",
+                                  "check password script = sed -e '/$unacceptable_password/{;q1}; /$unacceptable_password/!{q0}'\n",
                                   "",
                                   $extra_provision_options);
+       unless (defined $ret) {
+               return undef;
+       }
 
-       return undef unless(defined $ret);
        unless($self->add_wins_config("$prefix/private")) {
                warn("Unable to add wins configuration");
                return undef;
@@ -1778,11 +2023,12 @@ sub provision_chgdcpass($$)
        $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
        $ret->{DC_USERNAME} = $ret->{USERNAME};
        $ret->{DC_PASSWORD} = $ret->{PASSWORD};
+       $ret->{UNACCEPTABLE_PASSWORD} = $unacceptable_password;
 
        return $ret;
 }
 
-sub teardown_env($$)
+sub teardown_env_terminate($$)
 {
        my ($self, $envvars) = @_;
        my $pid;
@@ -1795,28 +2041,50 @@ sub teardown_env($$)
        my $childpid;
 
        # This should give it time to write out the gcov data
+       until ($count > 15) {
+           if (Samba::cleanup_child($pid, "samba") != 0) {
+               return;
+           }
+           sleep(1);
+           $count++;
+       }
+
+       # After 15 Seconds, work out why this thing is still alive
+       warn "server process $pid took more than $count seconds to exit, showing backtrace:\n";
+       system("$self->{srcdir}/selftest/gdb_backtrace $pid");
+
        until ($count > 30) {
-           if (Samba::cleanup_child($pid, "samba") == -1) {
-               last;
+           if (Samba::cleanup_child($pid, "samba") != 0) {
+               return;
            }
            sleep(1);
            $count++;
        }
 
-       if ($count > 30 || kill(0, $pid)) {
+       if (kill(0, $pid)) {
+           warn "server process $pid took more than $count seconds to exit, sending SIGTERM\n";
            kill "TERM", $pid;
+       }
 
-           until ($count > 40) {
-               if (Samba::cleanup_child($pid, "samba") == -1) {
-                   last;
-               }
-               sleep(1);
-               $count++;
+       until ($count > 40) {
+           if (Samba::cleanup_child($pid, "samba") != 0) {
+               return;
            }
-           # If it is still around, kill it
-           warn "server process $pid took more than $count seconds to exit, killing\n";
+           sleep(1);
+           $count++;
+       }
+       # If it is still around, kill it
+       if (kill(0, $pid)) {
+           warn "server process $pid took more than $count seconds to exit, killing\n with SIGKILL\n";
            kill 9, $pid;
        }
+       return;
+}
+
+sub teardown_env($$)
+{
+       my ($self, $envvars) = @_;
+       teardown_env_terminate($self, $envvars);
 
        $self->slapd_stop($envvars) if ($self->{ldap});
 
@@ -1862,106 +2130,76 @@ sub check_env($$)
        } else {
            return 1;
        }
-
 }
 
-sub setup_env($$$)
+# Declare the environments Samba4 makes available.
+# To be set up, they will be called as
+#   samba4->setup_$envname($self, $path, $dep_1_vars, $dep_2_vars, ...)
+%Samba4::ENV_DEPS = (
+       # name               => [dep_1, dep_2, ...],
+       ad_dc_ntvfs          => [],
+       ad_dc                => [],
+       ad_dc_no_nss         => [],
+       ad_dc_no_ntlm        => [],
+       ad_dc_ntvfs          => [],
+       backupfromdc         => [],
+
+       fl2008r2dc           => ["ad_dc"],
+       fl2003dc             => ["ad_dc"],
+       fl2000dc             => [],
+
+       vampire_2000_dc      => ["fl2000dc"],
+       vampire_dc           => ["ad_dc_ntvfs"],
+       promoted_dc          => ["ad_dc_ntvfs"],
+       subdom_dc            => ["ad_dc_ntvfs"],
+
+       rodc                 => ["ad_dc_ntvfs"],
+       rpc_proxy            => ["ad_dc_ntvfs"],
+       chgdcpass            => [],
+
+       s4member_dflt_domain => ["ad_dc_ntvfs"],
+       s4member             => ["ad_dc_ntvfs"],
+
+       restoredc            => ["backupfromdc"],
+       renamedc             => ["backupfromdc"],
+       offlinebackupdc      => ["backupfromdc"],
+       labdc                => ["backupfromdc"],
+
+       none                 => [],
+);
+
+sub setup_s4member
 {
-       my ($self, $envname, $path) = @_;
-       my $target3 = $self->{target3};
-
-       $ENV{ENVNAME} = $envname;
+       my ($self, $path, $dc_vars) = @_;
 
-       if (defined($self->{vars}->{$envname})) {
-               return $self->{vars}->{$envname};
-       }
+       my $env = $self->provision_s4member($path, $dc_vars, "s4member");
 
-       if ($envname eq "ad_dc_ntvfs") {
-               return $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
-       } elsif ($envname eq "fl2000dc") {
-               return $self->setup_fl2000dc("$path/fl2000dc");
-       } elsif ($envname eq "fl2003dc") {
-               if (not defined($self->{vars}->{ad_dc})) {
-                       $self->setup_ad_dc("$path/ad_dc");
-               }
-               return $self->setup_fl2003dc("$path/fl2003dc", $self->{vars}->{ad_dc});
-       } elsif ($envname eq "fl2008r2dc") {
-               if (not defined($self->{vars}->{ad_dc})) {
-                       $self->setup_ad_dc("$path/ad_dc");
-               }
-               return $self->setup_fl2008r2dc("$path/fl2008r2dc", $self->{vars}->{ad_dc});
-       } elsif ($envname eq "rpc_proxy") {
-               if (not defined($self->{vars}->{ad_dc_ntvfs})) {
-                       $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
-               }
-               return $self->setup_rpc_proxy("$path/rpc_proxy", $self->{vars}->{ad_dc_ntvfs});
-       } elsif ($envname eq "vampire_dc") {
-               if (not defined($self->{vars}->{ad_dc_ntvfs})) {
-                       $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
-               }
-               return $self->setup_vampire_dc("$path/vampire_dc", $self->{vars}->{ad_dc_ntvfs});
-       } elsif ($envname eq "promoted_dc") {
-               if (not defined($self->{vars}->{ad_dc_ntvfs})) {
-                       $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
-               }
-               return $self->setup_promoted_dc("$path/promoted_dc", $self->{vars}->{ad_dc_ntvfs});
-       } elsif ($envname eq "subdom_dc") {
-               if (not defined($self->{vars}->{ad_dc_ntvfs})) {
-                       $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
-               }
-               return $self->setup_subdom_dc("$path/subdom_dc", $self->{vars}->{ad_dc_ntvfs});
-       } elsif ($envname eq "s4member") {
-               if (not defined($self->{vars}->{ad_dc_ntvfs})) {
-                       $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
-               }
-               return $self->setup_s4member("$path/s4member", $self->{vars}->{ad_dc_ntvfs});
-       } elsif ($envname eq "rodc") {
-               if (not defined($self->{vars}->{ad_dc_ntvfs})) {
-                       $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
-               }
-               return $self->setup_rodc("$path/rodc", $self->{vars}->{ad_dc_ntvfs});
-       } elsif ($envname eq "chgdcpass") {
-               return $self->setup_chgdcpass("$path/chgdcpass", $self->{vars}->{chgdcpass});
-       } elsif ($envname eq "ad_member") {
-               if (not defined($self->{vars}->{ad_dc_ntvfs})) {
-                       $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
-               }
-               return $target3->setup_admember("$path/ad_member", $self->{vars}->{ad_dc_ntvfs}, 29);
-       } elsif ($envname eq "ad_dc") {
-               return $self->setup_ad_dc("$path/ad_dc");
-       } elsif ($envname eq "ad_dc_no_nss") {
-               return $self->setup_ad_dc("$path/ad_dc_no_nss", "no_nss");
-       } elsif ($envname eq "ad_member_rfc2307") {
-               if (not defined($self->{vars}->{ad_dc_ntvfs})) {
-                       $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
+       if (defined $env) {
+               if (not defined($self->check_or_start($env, "standard"))) {
+                       return undef;
                }
-               return $target3->setup_admember_rfc2307("$path/ad_member_rfc2307",
-                                                       $self->{vars}->{ad_dc_ntvfs}, 34);
-       } elsif ($envname eq "none") {
-               return $self->setup_none("$path/none");
-       } else {
-               return "UNKNOWN";
        }
+
+       return $env;
 }
 
-sub setup_s4member($$$)
+sub setup_s4member_dflt_domain
 {
        my ($self, $path, $dc_vars) = @_;
 
-       my $env = $self->provision_s4member($path, $dc_vars);
+       my $env = $self->provision_s4member($path, $dc_vars, "s4member_dflt",
+                                           "winbind use default domain = yes");
 
        if (defined $env) {
                if (not defined($self->check_or_start($env, "standard"))) {
                        return undef;
                }
-
-               $self->{vars}->{s4member} = $env;
        }
 
        return $env;
 }
 
-sub setup_rpc_proxy($$$)
+sub setup_rpc_proxy
 {
        my ($self, $path, $dc_vars) = @_;
 
@@ -1971,13 +2209,11 @@ sub setup_rpc_proxy($$$)
                if (not defined($self->check_or_start($env, "standard"))) {
                        return undef;
                }
-
-               $self->{vars}->{rpc_proxy} = $env;
        }
        return $env;
 }
 
-sub setup_ad_dc_ntvfs($$)
+sub setup_ad_dc_ntvfs
 {
        my ($self, $path) = @_;
 
@@ -1987,13 +2223,11 @@ sub setup_ad_dc_ntvfs($$)
                    warn("Failed to start ad_dc_ntvfs");
                        return undef;
                }
-
-               $self->{vars}->{ad_dc_ntvfs} = $env;
        }
        return $env;
 }
 
-sub setup_chgdcpass($$)
+sub setup_chgdcpass
 {
        my ($self, $path) = @_;
 
@@ -2002,13 +2236,11 @@ sub setup_chgdcpass($$)
                if (not defined($self->check_or_start($env, "standard"))) {
                        return undef;
                }
-
-               $self->{vars}->{chgdcpass} = $env;
        }
        return $env;
 }
 
-sub setup_fl2000dc($$)
+sub setup_fl2000dc
 {
        my ($self, $path) = @_;
 
@@ -2017,14 +2249,12 @@ sub setup_fl2000dc($$)
                if (not defined($self->check_or_start($env, "standard"))) {
                        return undef;
                }
-
-               $self->{vars}->{fl2000dc} = $env;
        }
 
        return $env;
 }
 
-sub setup_fl2003dc($$$)
+sub setup_fl2003dc
 {
        my ($self, $path, $dc_vars) = @_;
 
@@ -2036,13 +2266,11 @@ sub setup_fl2003dc($$$)
                }
 
                $env = $self->setup_trust($env, $dc_vars, "external", "--no-aes-keys");
-
-               $self->{vars}->{fl2003dc} = $env;
        }
        return $env;
 }
 
-sub setup_fl2008r2dc($$$)
+sub setup_fl2008r2dc
 {
        my ($self, $path, $dc_vars) = @_;
 
@@ -2059,30 +2287,40 @@ sub setup_fl2008r2dc($$$)
                $self->setup_namespaces($env, $upn_array, $spn_array);
 
                $env = $self->setup_trust($env, $dc_vars, "forest", "");
-
-               $self->{vars}->{fl2008r2dc} = $env;
        }
 
        return $env;
 }
 
-sub setup_vampire_dc($$$)
+sub setup_vampire_dc
 {
-       my ($self, $path, $dc_vars) = @_;
+       return setup_generic_vampire_dc(@_, "2008");
+}
 
-       my $env = $self->provision_vampire_dc($path, $dc_vars);
+sub setup_vampire_2000_dc
+{
+       return setup_generic_vampire_dc(@_, "2000");
+}
+
+sub setup_generic_vampire_dc
+{
+       my ($self, $path, $dc_vars, $fl) = @_;
+
+       my $env = $self->provision_vampire_dc($path, $dc_vars, $fl);
 
        if (defined $env) {
                if (not defined($self->check_or_start($env, "single"))) {
                        return undef;
                }
 
-               $self->{vars}->{vampire_dc} = $env;
-
                # force replicated DC to update repsTo/repsFrom
                # for vampired partitions
                my $samba_tool =  Samba::bindir_path($self, "samba-tool");
-               my $cmd = "";
+
+               # as 'vampired' dc may add data in its local replica
+               # we need to synchronize data between DCs
+               my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
+               my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
                $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
                if (defined($env->{RESOLV_WRAPPER_CONF})) {
                        $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
@@ -2090,18 +2328,26 @@ sub setup_vampire_dc($$$)
                        $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
                }
                $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
-               $cmd .= " $samba_tool drs kcc -k no $env->{DC_SERVER}";
-               $cmd .= " $env->{CONFIGURATION}";
+               $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
+               $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
+               $cmd .= " $dc_vars->{CONFIGURATION}";
                $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
-               unless (system($cmd) == 0) {
-                       warn("Failed to exec kcc\n$cmd");
+               # replicate Configuration NC
+               my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
+               unless(system($cmd_repl) == 0) {
+                       warn("Failed to replicate\n$cmd_repl");
+                       return undef;
+               }
+               # replicate Default NC
+               $cmd_repl = "$cmd \"$base_dn\"";
+               unless(system($cmd_repl) == 0) {
+                       warn("Failed to replicate\n$cmd_repl");
                        return undef;
                }
 
-               # as 'vampired' dc may add data in its local replica
-               # we need to synchronize data between DCs
+               # Pull in a full set of changes from the main DC
                my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
-               $cmd = "";
+               $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
                $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
                if (defined($env->{RESOLV_WRAPPER_CONF})) {
                        $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
@@ -2109,7 +2355,8 @@ sub setup_vampire_dc($$$)
                        $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
                }
                $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
-               $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
+               $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
+               $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
                $cmd .= " $dc_vars->{CONFIGURATION}";
                $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
                # replicate Configuration NC
@@ -2129,7 +2376,7 @@ sub setup_vampire_dc($$$)
        return $env;
 }
 
-sub setup_promoted_dc($$$)
+sub setup_promoted_dc
 {
        my ($self, $path, $dc_vars) = @_;
 
@@ -2140,39 +2387,16 @@ sub setup_promoted_dc($$$)
                        return undef;
                }
 
-               $self->{vars}->{promoted_dc} = $env;
-
                # force source and replicated DC to update repsTo/repsFrom
                # for vampired partitions
                my $samba_tool =  Samba::bindir_path($self, "samba-tool");
-               my $cmd = "";
-               $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
-               $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
-               $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
-               $cmd .= " $env->{CONFIGURATION}";
-               $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
-               unless (system($cmd) == 0) {
-                       warn("Failed to exec kcc\n$cmd");
-                       return undef;
-               }
-
-               my $samba_tool =  Samba::bindir_path($self, "samba-tool");
-               my $cmd = "";
-               $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
-               $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
-               $cmd .= " $samba_tool drs kcc $env->{SERVER}";
-               $cmd .= " $env->{CONFIGURATION}";
-               $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
-               unless (system($cmd) == 0) {
-                       warn("Failed to exec kcc\n$cmd");
-                       return undef;
-               }
-
+               my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
                # as 'vampired' dc may add data in its local replica
                # we need to synchronize data between DCs
                my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
                $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
                $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
+               $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
                $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
                $cmd .= " $dc_vars->{CONFIGURATION}";
                $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
@@ -2193,7 +2417,7 @@ sub setup_promoted_dc($$$)
        return $env;
 }
 
-sub setup_subdom_dc($$$)
+sub setup_subdom_dc
 {
        my ($self, $path, $dc_vars) = @_;
 
@@ -2204,28 +2428,17 @@ sub setup_subdom_dc($$$)
                        return undef;
                }
 
-               $self->{vars}->{subdom_dc} = $env;
-
                # force replicated DC to update repsTo/repsFrom
                # for primary domain partitions
                my $samba_tool =  Samba::bindir_path($self, "samba-tool");
-               my $cmd = "";
-               $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
-               $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
-               $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
-               $cmd .= " $env->{CONFIGURATION}";
-               $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
-               unless (system($cmd) == 0) {
-                       warn("Failed to exec kcc\n$cmd");
-                       return undef;
-               }
-
+               my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
                # as 'subdomain' dc may add data in its local replica
                # we need to synchronize data between DCs
                my $base_dn = "DC=".join(",DC=", split(/\./, $env->{REALM}));
                my $config_dn = "CN=Configuration,DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
                $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
                $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
+               $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
                $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SUBDOM_DC_SERVER}";
                $cmd .= " $dc_vars->{CONFIGURATION}";
                $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
@@ -2246,7 +2459,7 @@ sub setup_subdom_dc($$$)
        return $env;
 }
 
-sub setup_rodc($$$)
+sub setup_rodc
 {
        my ($self, $path, $dc_vars) = @_;
 
@@ -2256,39 +2469,18 @@ sub setup_rodc($$$)
                return undef;
        }
 
-       if (not defined($self->check_or_start($env, "single"))) {
+       if (not defined($self->check_or_start($env, "standard"))) {
            return undef;
        }
 
-       # force source and replicated DC to update repsTo/repsFrom
-       # for vampired partitions
        my $samba_tool =  Samba::bindir_path($self, "samba-tool");
        my $cmd = "";
-       $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
-       $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
-       $cmd .= " $samba_tool drs kcc -k no $env->{DC_SERVER}";
-       $cmd .= " $env->{CONFIGURATION}";
-       $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
-       unless (system($cmd) == 0) {
-           warn("Failed to exec kcc\n$cmd");
-           return undef;
-       }
-
-       my $samba_tool =  Samba::bindir_path($self, "samba-tool");
-       my $cmd = "";
-       $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
-       $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
-       $cmd .= " $samba_tool drs kcc -k no $env->{SERVER}";
-       $cmd .= " $env->{CONFIGURATION}";
-       $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
-       unless (system($cmd) == 0) {
-           warn("Failed to exec kcc\n$cmd");
-           return undef;
-       }
 
        my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
-       $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
+       $cmd .= "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
+       $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
        $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
+       $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
        $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
        $cmd .= " $dc_vars->{CONFIGURATION}";
        $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
@@ -2305,30 +2497,54 @@ sub setup_rodc($$$)
            return undef;
        }
 
-       $self->{vars}->{rodc} = $env;
-
        return $env;
 }
 
-sub setup_ad_dc($$)
+sub setup_ad_dc
 {
-       my ($self, $path, $no_nss) = @_;
+       my ($self, $path) = @_;
 
        # If we didn't build with ADS, pretend this env was never available
        if (not $self->{target3}->have_ads()) {
               return "UNKNOWN";
        }
 
-       my $env = $self->provision_ad_dc($path);
+       my $env = $self->provision_ad_dc($path, "addc", "ADDOMAIN",
+                                        "addom.samba.example.com", "");
        unless ($env) {
                return undef;
        }
 
-       if (defined($no_nss) and $no_nss) {
-               $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
-               $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
+       if (not defined($self->check_or_start($env, "single"))) {
+           return undef;
+       }
+
+       my $upn_array = ["$env->{REALM}.upn"];
+       my $spn_array = ["$env->{REALM}.spn"];
+
+       $self->setup_namespaces($env, $upn_array, $spn_array);
+
+       return $env;
+}
+
+sub setup_ad_dc_no_nss
+{
+       my ($self, $path) = @_;
+
+       # If we didn't build with ADS, pretend this env was never available
+       if (not $self->{target3}->have_ads()) {
+              return "UNKNOWN";
+       }
+
+       my $env = $self->provision_ad_dc($path, "addc_no_nss", "ADNONSSDOMAIN",
+                                        "adnonssdom.samba.example.com", "");
+       unless ($env) {
+               return undef;
        }
 
+       $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
+       $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
+
        if (not defined($self->check_or_start($env, "single"))) {
            return undef;
        }
@@ -2338,11 +2554,416 @@ sub setup_ad_dc($$)
 
        $self->setup_namespaces($env, $upn_array, $spn_array);
 
-       $self->{vars}->{ad_dc} = $env;
        return $env;
 }
 
-sub setup_none($$)
+sub setup_ad_dc_no_ntlm
+{
+       my ($self, $path) = @_;
+
+       # If we didn't build with ADS, pretend this env was never available
+       if (not $self->{target3}->have_ads()) {
+              return "UNKNOWN";
+       }
+
+       my $env = $self->provision_ad_dc($path, "addc_no_ntlm", "ADNONTLMDOMAIN",
+                                        "adnontlmdom.samba.example.com",
+                                        "ntlm auth = disabled");
+       unless ($env) {
+               return undef;
+       }
+
+       if (not defined($self->check_or_start($env, "prefork"))) {
+           return undef;
+       }
+
+       my $upn_array = ["$env->{REALM}.upn"];
+       my $spn_array = ["$env->{REALM}.spn"];
+
+       $self->setup_namespaces($env, $upn_array, $spn_array);
+
+       return $env;
+}
+
+# Sets up a DC that's solely used to do a domain backup from. We then use the
+# backupfrom-DC to create the restore-DC - this proves that the backup/restore
+# process will create a Samba DC that will actually start up.
+# We don't use the backup-DC for anything else because its domain will conflict
+# with the restore DC.
+sub setup_backupfromdc
+{
+       my ($self, $path) = @_;
+
+       # If we didn't build with ADS, pretend this env was never available
+       if (not $self->{target3}->have_ads()) {
+              return "UNKNOWN";
+       }
+
+       my $env = $self->provision_ad_dc($path, "backupfromdc", "BACKUPDOMAIN",
+                                        "backupdom.samba.example.com", "");
+       unless ($env) {
+               return undef;
+       }
+
+       if (not defined($self->check_or_start($env, "standard"))) {
+           return undef;
+       }
+
+       my $upn_array = ["$env->{REALM}.upn"];
+       my $spn_array = ["$env->{REALM}.spn"];
+
+       $self->setup_namespaces($env, $upn_array, $spn_array);
+
+       return $env;
+}
+
+# returns the server/user-auth params needed to run an online backup cmd
+sub get_backup_server_args
+{
+       # dcvars contains the env info for the backup DC testenv
+       my ($self, $dcvars) = @_;
+       my $server = $dcvars->{DC_SERVER_IP};
+       my $server_args = "--server=$server ";
+       $server_args .= "-U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
+
+       return $server_args;
+}
+
+# Creates a backup of a running testenv DC
+sub create_backup
+{
+       # note: dcvars contains the env info for the backup DC testenv
+       my ($self, $env, $dcvars, $backupdir, $backup_cmd) = @_;
+
+       # get all the env variables we pass in with the samba-tool command
+       my $cmd_env = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
+       $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
+       if (defined($env->{RESOLV_WRAPPER_CONF})) {
+               $cmd_env .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
+       } else {
+               $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
+       }
+       # Note: use the backupfrom-DC's krb5.conf to do the backup
+       $cmd_env .= " KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
+       $cmd_env .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
+
+       # use samba-tool to create a backup from the 'backupfromdc' DC
+       my $cmd = "";
+       my $samba_tool = Samba::bindir_path($self, "samba-tool");
+
+       $cmd .= "$cmd_env $samba_tool domain backup $backup_cmd";
+       $cmd .= " --targetdir=$backupdir";
+
+       print "Executing: $cmd\n";
+       unless(system($cmd) == 0) {
+               warn("Failed to create backup using: \n$cmd");
+               return undef;
+       }
+
+       # get the name of the backup file created
+       opendir(DIR, $backupdir);
+       my @files = grep(/\.tar/, readdir(DIR));
+       closedir(DIR);
+
+       if(scalar @files != 1) {
+               warn("Backup file not found in directory $backupdir\n");
+               return undef;
+       }
+       my $backup_file = "$backupdir/$files[0]";
+       print "Using backup file $backup_file...\n";
+
+       return $backup_file;
+}
+
+# Restores a backup-file to populate a testenv for a new DC
+sub restore_backup_file
+{
+       my ($self, $backup_file, $restore_opts, $restoredir, $smbconf) = @_;
+
+       # pass the restore command the testenv's smb.conf that we've already
+       # generated. But move it to a temp-dir first, so that the restore doesn't
+       # overwrite it
+       my $tmpdir = File::Temp->newdir();
+       my $tmpconf = "$tmpdir/smb.conf";
+       my $cmd = "cp $smbconf $tmpconf";
+       unless(system($cmd) == 0) {
+               warn("Failed to backup smb.conf using: \n$cmd");
+               return -1;
+       }
+
+       my $samba_tool = Samba::bindir_path($self, "samba-tool");
+       $cmd = "$samba_tool domain backup restore --backup-file=$backup_file";
+       $cmd .= " --targetdir=$restoredir $restore_opts --configfile=$tmpconf";
+
+       print "Executing: $cmd\n";
+       unless(system($cmd) == 0) {
+               warn("Failed to restore backup using: \n$cmd");
+               return -1;
+       }
+
+       print "Restore complete\n";
+       return 0
+}
+
+# sets up the initial directory and returns the new testenv's env info
+# (without actually doing a 'domain join')
+sub prepare_dc_testenv
+{
+       my ($self, $prefix, $dcname, $domain, $realm, $password) = @_;
+
+       my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
+                                              $dcname,
+                                              $domain,
+                                              $realm,
+                                              undef,
+                                              "2008",
+                                              $password,
+                                              undef,
+                                              undef);
+
+       # the restore uses a slightly different state-dir location to other testenvs
+       $ctx->{statedir} = "$ctx->{prefix_abs}/state";
+       push(@{$ctx->{directories}}, "$ctx->{statedir}");
+
+       # add support for sysvol/netlogon/tmp shares
+       $ctx->{share} = "$ctx->{prefix_abs}/share";
+       push(@{$ctx->{directories}}, "$ctx->{share}");
+
+       $ctx->{smb_conf_extra_options} = "
+       max xmit = 32K
+       server max protocol = SMB2
+
+[sysvol]
+       path = $ctx->{statedir}/sysvol
+       read only = no
+
+[netlogon]
+       path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
+       read only = no
+
+[tmp]
+       path = $ctx->{share}
+       read only = no
+       posix:sharedelay = 10000
+       posix:oplocktimeout = 3
+       posix:writetimeupdatedelay = 50000
+
+";
+
+       my $env = $self->provision_raw_step1($ctx);
+
+       $env->{DC_SERVER} = $env->{SERVER};
+       $env->{DC_SERVER_IP} = $env->{SERVER_IP};
+       $env->{DC_SERVER_IPV6} = $env->{SERVER_IPV6};
+       $env->{DC_NETBIOSNAME} = $env->{NETBIOSNAME};
+       $env->{DC_USERNAME} = $env->{USERNAME};
+       $env->{DC_PASSWORD} = $env->{PASSWORD};
+
+    return ($env, $ctx);
+}
+
+
+# Set up a DC testenv solely by using the samba-tool domain backup/restore
+# commands. This proves that we can backup an online DC ('backupfromdc') and
+# use the backup file to create a valid, working samba DC.
+sub setup_restoredc
+{
+       # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
+       my ($self, $prefix, $dcvars) = @_;
+       print "Preparing RESTORE DC...\n";
+
+       my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "restoredc",
+                                                   $dcvars->{DOMAIN},
+                                                   $dcvars->{REALM},
+                                                   $dcvars->{PASSWORD});
+
+       # create a backup of the 'backupfromdc'
+       my $backupdir = File::Temp->newdir();
+       my $server_args = $self->get_backup_server_args($dcvars);
+       my $backup_args = "online $server_args";
+       my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
+                                              $backup_args);
+       unless($backup_file) {
+               return undef;
+       }
+
+       # restore the backup file to populate the restore-DC testenv
+       my $restore_dir = abs_path($prefix);
+       my $ret = $self->restore_backup_file($backup_file,
+                                            "--newservername=$env->{SERVER}",
+                                            $restore_dir, $env->{SERVERCONFFILE});
+       unless ($ret == 0) {
+               return undef;
+       }
+
+       # start samba for the restored DC
+       if (not defined($self->check_or_start($env, "standard"))) {
+           return undef;
+       }
+
+       my $upn_array = ["$env->{REALM}.upn"];
+       my $spn_array = ["$env->{REALM}.spn"];
+
+       $self->setup_namespaces($env, $upn_array, $spn_array);
+
+       return $env;
+}
+
+# Set up a DC testenv solely by using the 'samba-tool domain backup rename' and
+# restore commands. This proves that we can backup and rename an online DC
+# ('backupfromdc') and use the backup file to create a valid, working samba DC.
+sub setup_renamedc
+{
+       # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
+       my ($self, $prefix, $dcvars) = @_;
+       print "Preparing RENAME DC...\n";
+
+       my $realm = "renamedom.samba.example.com";
+       my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "renamedc",
+                                                   "RENAMEDOMAIN", $realm,
+                                                   $dcvars->{PASSWORD});
+
+       # create a backup of the 'backupfromdc' which renames the domain
+       my $backupdir = File::Temp->newdir();
+       my $server_args = $self->get_backup_server_args($dcvars);
+       my $backup_args = "rename $env->{DOMAIN} $env->{REALM} $server_args";
+       my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
+                                              $backup_args);
+       unless($backup_file) {
+               return undef;
+       }
+
+       # restore the backup file to populate the rename-DC testenv
+       my $restore_dir = abs_path($prefix);
+       my $restore_opts =  "--newservername=$env->{SERVER} --host-ip=$env->{SERVER_IP}";
+       my $ret = $self->restore_backup_file($backup_file, $restore_opts,
+                                            $restore_dir, $env->{SERVERCONFFILE});
+       unless ($ret == 0) {
+               return undef;
+       }
+
+       # start samba for the restored DC
+       if (not defined($self->check_or_start($env, "standard"))) {
+           return undef;
+       }
+
+       my $upn_array = ["$env->{REALM}.upn"];
+       my $spn_array = ["$env->{REALM}.spn"];
+
+       $self->setup_namespaces($env, $upn_array, $spn_array);
+
+       return $env;
+}
+
+# Set up a DC testenv solely by using the 'samba-tool domain backup offline' and
+# restore commands. This proves that we do an offline backup of a local DC
+# ('backupfromdc') and use the backup file to create a valid, working samba DC.
+sub setup_offlinebackupdc
+{
+       # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
+       my ($self, $prefix, $dcvars) = @_;
+       print "Preparing OFFLINE BACKUP DC...\n";
+
+       my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "offlinebackupdc",
+                                                   $dcvars->{DOMAIN},
+                                                   $dcvars->{REALM},
+                                                   $dcvars->{PASSWORD});
+
+       # create an offline backup of the 'backupfromdc' target
+       my $backupdir = File::Temp->newdir();
+       my $cmd = "offline -s $dcvars->{SERVERCONFFILE}";
+       my $backup_file = $self->create_backup($env, $dcvars,
+                                              $backupdir, $cmd);
+
+       unless($backup_file) {
+               return undef;
+       }
+
+       # restore the backup file to populate the rename-DC testenv
+       my $restore_dir = abs_path($prefix);
+       my $restore_opts =  "--newservername=$env->{SERVER} --host-ip=$env->{SERVER_IP}";
+       my $ret = $self->restore_backup_file($backup_file, $restore_opts,
+                                            $restore_dir, $env->{SERVERCONFFILE});
+       unless ($ret == 0) {
+               return undef;
+       }
+
+       # re-create the testenv's krb5.conf (the restore may have overwritten it)
+       Samba::mk_krb5_conf($ctx);
+
+       # start samba for the restored DC
+       if (not defined($self->check_or_start($env, "standard"))) {
+           return undef;
+       }
+
+       my $upn_array = ["$env->{REALM}.upn"];
+       my $spn_array = ["$env->{REALM}.spn"];
+
+       $self->setup_namespaces($env, $upn_array, $spn_array);
+
+       return $env;
+}
+
+# Set up a DC testenv solely by using the samba-tool 'domain backup rename' and
+# restore commands, using the --no-secrets option. This proves that we can
+# create a realistic lab environment from an online DC ('backupfromdc').
+sub setup_labdc
+{
+       # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
+       my ($self, $prefix, $dcvars) = @_;
+       print "Preparing LAB-DOMAIN DC...\n";
+
+       my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "labdc",
+                                                   "LABDOMAIN",
+                                                   "labdom.samba.example.com",
+                                                   $dcvars->{PASSWORD});
+
+       # create a backup of the 'backupfromdc' which renames the domain and uses
+       # the --no-secrets option to scrub any sensitive info
+       my $backupdir = File::Temp->newdir();
+       my $server_args = $self->get_backup_server_args($dcvars);
+       my $backup_args = "rename $env->{DOMAIN} $env->{REALM} $server_args";
+       $backup_args .= " --no-secrets";
+       my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
+                                              $backup_args);
+       unless($backup_file) {
+               return undef;
+       }
+
+       # restore the backup file to populate the lab-DC testenv
+       my $restore_dir = abs_path($prefix);
+       my $restore_opts =  "--newservername=$env->{SERVER} --host-ip=$env->{SERVER_IP}";
+       my $ret = $self->restore_backup_file($backup_file, $restore_opts,
+                                            $restore_dir, $env->{SERVERCONFFILE});
+       unless ($ret == 0) {
+               return undef;
+       }
+
+       # because we don't include any secrets in the backup, we need to reset the
+       # admin user's password back to what the testenv expects
+       my $samba_tool = Samba::bindir_path($self, "samba-tool");
+       my $cmd = "$samba_tool user setpassword $env->{USERNAME} ";
+       $cmd .= "--newpassword=$env->{PASSWORD} -H $restore_dir/private/sam.ldb";
+
+       unless(system($cmd) == 0) {
+               warn("Failed to reset admin's password: \n$cmd");
+               return -1;
+       }
+
+       # start samba for the restored DC
+       if (not defined($self->check_or_start($env, "standard"))) {
+           return undef;
+       }
+
+       my $upn_array = ["$env->{REALM}.upn"];
+       my $spn_array = ["$env->{REALM}.spn"];
+
+       $self->setup_namespaces($env, $upn_array, $spn_array);
+
+       return $env;
+}
+
+sub setup_none
 {
        my ($self, $path) = @_;