CVE-2016-2113: selftest: use "tls verify peer = no_check"
[samba.git] / selftest / selftest.pl
index 1483174d7a97ebb2c836332da54a2134ff3e69c5..ff5f27d08555c5177721ab504e63daa615b1efd6 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 # Bootstrap Samba and run a number of tests against it.
-# Copyright (C) 2005-2009 Jelmer Vernooij <jelmer@samba.org>
+# Copyright (C) 2005-2010 Jelmer Vernooij <jelmer@samba.org>
 # Copyright (C) 2007-2009 Stefan Metzmacher <metze@samba.org>
 
 # This program is free software; you can redistribute it and/or modify
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-=pod
-
-=head1 NAME
-
-selftest - Samba test runner
-
-=head1 SYNOPSIS
-
-selftest --help
-
-selftest [--srcdir=DIR] [--builddir=DIR] [--exeext=EXT][--target=samba4|samba3|win|kvm] [--socket-wrapper] [--quick] [--exclude=FILE] [--include=FILE] [--one] [--prefix=prefix] [--testlist=FILE] [TESTS]
-
-=head1 DESCRIPTION
-
-A simple test runner. TESTS is a regular expression with tests to run.
-
-=head1 OPTIONS
-
-=over 4
-
-=item I<--help>
-
-Show list of available options.
-
-=item I<--srcdir=DIR>
-
-Source directory.
-
-=item I<--builddir=DIR>
-
-Build directory.
-
-=item I<--exeext=EXT>
-
-Executable extention
-
-=item I<--prefix=DIR>
-
-Change directory to run tests in. Default is 'st'.
-
-=item I<--target samba4|samba3|win|kvm>
-
-Specify test target against which to run. Default is 'samba4'.
-
-=item I<--quick>
-
-Run only a limited number of tests. Intended to run in about 30 seconds on 
-moderately recent systems.
-               
-=item I<--socket-wrapper>
-
-Use socket wrapper library for communication with server. Only works 
-when the server is running locally.
-
-Will prevent TCP and UDP ports being opened on the local host but 
-(transparently) redirects these calls to use unix domain sockets.
-
-=item I<--exclude>
-
-Specify a file containing a list of tests that should be skipped. Possible 
-candidates are tests that segfault the server, flip or don't end. 
-
-=item I<--include>
-
-Specify a file containing a list of tests that should be run. Same format 
-as the --exclude flag.
-
-Not includes specified means all tests will be run.
-
-=item I<--one>
-
-Abort as soon as one test fails.
-
-=item I<--testlist>
-
-Load a list of tests from the specified location.
-
-=back
-
-=head1 ENVIRONMENT
-
-=over 4
-
-=item I<SMBD_VALGRIND>
-
-=item I<TORTURE_MAXTIME>
-
-=item I<VALGRIND>
-
-=item I<TLS_ENABLED>
-
-=item I<srcdir>
-
-=back
-
-=head1 LICENSE
-
-selftest is licensed under the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>.
-
-=head1 AUTHOR
-
-Jelmer Vernooij
-
-=cut
-
 use strict;
 
 use FindBin qw($RealBin $Script);
@@ -130,40 +25,42 @@ use Getopt::Long;
 use POSIX;
 use Cwd qw(abs_path);
 use lib "$RealBin";
-use Subunit qw(parse_results);
-use Subunit::Filter;
+use Subunit;
 use SocketWrapper;
+use target::Samba;
 
 eval {
 require Time::HiRes;
+Time::HiRes->import("time");
 };
-unless ($@) {
-       use Time::HiRes qw(time);
+if ($@) {
+       print "You don't have Time::Hires installed !\n";
 }
 
 my $opt_help = 0;
-my $opt_target = "samba4";
+my $opt_target = "samba";
 my $opt_quick = 0;
 my $opt_socket_wrapper = 0;
 my $opt_socket_wrapper_pcap = undef;
 my $opt_socket_wrapper_keep_pcap = undef;
+my $opt_random_order = 0;
 my $opt_one = 0;
 my @opt_exclude = ();
 my @opt_include = ();
-my $opt_verbose = 0;
-my $opt_image = undef;
 my $opt_testenv = 0;
+my $opt_list = 0;
 my $ldap = undef;
-my $opt_analyse_cmd = undef;
 my $opt_resetup_env = undef;
-my $opt_bindir = undef;
-my $opt_no_lazy_setup = undef;
 my $opt_load_list = undef;
+my $opt_libnss_wrapper_so_path = "";
+my $opt_libresolv_wrapper_so_path = "";
+my $opt_libsocket_wrapper_so_path = "";
+my $opt_libuid_wrapper_so_path = "";
+my $opt_use_dns_faking = 0;
 my @testlists = ();
 
 my $srcdir = ".";
-my $builddir = ".";
-my $exeext = "";
+my $bindir = "./bin";
 my $prefix = "./st";
 
 my @includes = ();
@@ -240,30 +137,22 @@ sub run_testsuite($$$$$)
        Subunit::start_testsuite($name);
        Subunit::progress_push();
        Subunit::report_time(time());
+       system($cmd);
+       Subunit::report_time(time());
+       Subunit::progress_pop();
 
-       open(RESULTS, "$cmd 2>&1|");
-
-       my $msg_ops = new Subunit::Filter("$name\.");
-
-       parse_results($msg_ops, *RESULTS);
-
-       my $ret = 0;
-
-       unless (close(RESULTS)) {
-               if ($!) {
-                       Subunit::progress_pop();
-                       Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
-                       return 0;
-               } else {
-                       $ret = $?;
-               }
-       } 
-
-       if ($ret & 127) {
+       if ($? == -1) {
                Subunit::progress_pop();
-               Subunit::end_testsuite($name, "error", sprintf("Testsuite died with signal %d, %s coredump", ($ret & 127), ($ret & 128) ? "with": "without"));
-               return 0;
+               Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
+               exit(1);
+       } elsif ($? & 127) {
+               Subunit::end_testsuite($name, "error",
+                       sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127),  ($? & 128) ? 'with' : 'without'));
+               exit(1);
        }
+
+       my $exitcode = $? >> 8;
+
        my $envlog = getlog_env($envname);
        if ($envlog ne "") {
                print "envlog: $envlog\n";
@@ -272,10 +161,6 @@ sub run_testsuite($$$$$)
        print "command: $cmd\n";
        printf "expanded command: %s\n", expand_environment_strings($cmd);
 
-       my $exitcode = $ret >> 8;
-
-       Subunit::report_time(time());
-       Subunit::progress_pop();
        if ($exitcode == 0) {
                Subunit::end_testsuite($name, "success");
        } else {
@@ -305,33 +190,40 @@ Usage: $Script [OPTIONS] TESTNAME-REGEX
 
 Generic options:
  --help                     this help page
- --target=samba[34]|win|kvm Samba version to target
- --testlist=FILE           file to read available tests from
+ --target=samba[3]|win      Samba version to target
+ --testlist=FILE            file to read available tests from
+ --exclude=FILE             Exclude tests listed in the file
+ --include=FILE             Include tests listed in the file
 
 Paths:
  --prefix=DIR               prefix to run tests in [st]
  --srcdir=DIR               source directory [.]
- --builddir=DIR             output directory [.]
- --exeext=EXT               executable extention []
+ --bindir=DIR               binaries directory [./bin]
+
+Preload cwrap:
+ --nss_wrapper_so_path=FILE the nss_wrapper library to preload
+ --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
+ --socket_wrapper_so_path=FILE the socket_wrapper library to preload
+ --uid_wrapper_so_path=FILE the uid_wrapper library to preload
+
+DNS:
+  --use-dns-faking          Fake DNS entries rather than talking to our
+                            DNS implementation.
 
 Target Specific:
- --socket-wrapper-pcap     save traffic to pcap directories
+ --socket-wrapper-pcap      save traffic to pcap directories
  --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that 
                             failed
  --socket-wrapper           enable socket wrapper
- --bindir=PATH              path to target binaries
 
 Samba4 Specific:
  --ldap=openldap|fedora-ds  back samba onto specified ldap server
 
-Kvm Specific:
- --image=PATH               path to KVM image
-
 Behaviour:
  --quick                    run quick overall test
  --one                      abort when the first test fails
- --verbose                  be verbose
- --analyse-cmd CMD          command to run after each test
+ --testenv                  run a shell in the requested test environment
+ --list                     list available tests
 ";
        exit(0);
 }
@@ -348,24 +240,27 @@ my $result = GetOptions (
                'exclude=s' => \@opt_exclude,
                'include=s' => \@opt_include,
                'srcdir=s' => \$srcdir,
-               'builddir=s' => \$builddir,
-               'exeext=s' => \$exeext,
-               'verbose' => \$opt_verbose,
+               'bindir=s' => \$bindir,
                'testenv' => \$opt_testenv,
+               'list' => \$opt_list,
                'ldap:s' => \$ldap,
-               'analyse-cmd=s' => \$opt_analyse_cmd,
-               'no-lazy-setup' => \$opt_no_lazy_setup,
                'resetup-environment' => \$opt_resetup_env,
-               'bindir:s' => \$opt_bindir,
-               'image=s' => \$opt_image,
                'testlist=s' => \@testlists,
+               'random-order' => \$opt_random_order,
                'load-list=s' => \$opt_load_list,
+               'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
+               'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
+               'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
+               'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
+               'use-dns-faking' => \$opt_use_dns_faking
            );
 
 exit(1) if (not $result);
 
 ShowHelp() if ($opt_help);
 
+die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
+
 # we want unbuffered output
 $| = 1;
 
@@ -374,13 +269,12 @@ my @tests = @ARGV;
 # quick hack to disable rpc validation when using valgrind - its way too slow
 unless (defined($ENV{VALGRIND})) {
        $ENV{VALIDATE} = "validate";
-       $ENV{MALLOC_CHECK_} = 2;
+       $ENV{MALLOC_CHECK_} = 3;
 }
 
 # make all our python scripts unbuffered
 $ENV{PYTHONUNBUFFERED} = 1;
 
-my $bindir = ($opt_bindir or "$builddir/bin");
 my $bindir_abs = abs_path($bindir);
 
 # Backwards compatibility:
@@ -404,12 +298,20 @@ $prefix =~ s+/$++;
 
 die("using an empty prefix isn't allowed") unless $prefix ne "";
 
-#Ensure we have the test prefix around
-mkdir($prefix, 0777) unless -d $prefix;
+# Ensure we have the test prefix around.
+#
+# We need restrictive
+# permissions on this as some subdirectories in this tree will have
+# wider permissions (ie 0777) and this would allow other users on the
+# host to subvert the test process.
+mkdir($prefix, 0700) unless -d $prefix;
+chmod 0700, $prefix;
 
 my $prefix_abs = abs_path($prefix);
+my $tmpdir_abs = abs_path("$prefix/tmp");
+mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
+
 my $srcdir_abs = abs_path($srcdir);
-my $builddir_abs = abs_path($builddir);
 
 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
@@ -419,14 +321,11 @@ $ENV{KRB5CCNAME} = "$prefix/krb5ticket";
 $ENV{PREFIX_ABS} = $prefix_abs;
 $ENV{SRCDIR} = $srcdir;
 $ENV{SRCDIR_ABS} = $srcdir_abs;
-$ENV{BUILDDIR} = $builddir;
-$ENV{BUILDDIR_ABS} = $builddir_abs;
-$ENV{EXEEXT} = $exeext;
+$ENV{BINDIR} = $bindir_abs;
 
 my $tls_enabled = not $opt_quick;
 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
-$ENV{LDB_MODULES_PATH} = "$bindir_abs/modules/ldb";
-$ENV{LD_SAMBA_MODULE_PATH} = "$bindir_abs/modules";
+
 sub prefix_pathvar($$)
 {
        my ($name, $newpath) = @_;
@@ -449,67 +348,97 @@ if ($opt_socket_wrapper_pcap) {
        $opt_socket_wrapper = 1;
 }
 
+my $ld_preload = $ENV{LD_PRELOAD};
+
+if ($opt_libnss_wrapper_so_path) {
+       if ($ld_preload) {
+               $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
+       } else {
+               $ld_preload = "$opt_libnss_wrapper_so_path";
+       }
+}
+
+if ($opt_libresolv_wrapper_so_path) {
+       if ($ld_preload) {
+               $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
+       } else {
+               $ld_preload = "$opt_libresolv_wrapper_so_path";
+       }
+}
+
+if ($opt_libsocket_wrapper_so_path) {
+       if ($ld_preload) {
+               $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
+       } else {
+               $ld_preload = "$opt_libsocket_wrapper_so_path";
+       }
+}
+
+if ($opt_libuid_wrapper_so_path) {
+       if ($ld_preload) {
+               $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
+       } else {
+               $ld_preload = "$opt_libuid_wrapper_so_path";
+       }
+}
+
+$ENV{LD_PRELOAD} = $ld_preload;
+print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
+
+# Enable uid_wrapper globally
+$ENV{UID_WRAPPER} = 1;
+
+# Disable RTLD_DEEPBIND hack for Samba bind dlz module
+#
+# This is needed in order to allow the ldb_*ldap module
+# to work with a preloaded socket wrapper.
+$ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
+
 my $socket_wrapper_dir;
 if ($opt_socket_wrapper) {
        $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
        print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
-} else {
-        unless ($< == 0) { 
-                print "WARNING: Not using socket wrapper, but also not running as root. Will not be able to listen on proper ports\n";
+} elsif (not $opt_list) {
+        unless ($< == 0) {
+                warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
         }
 }
 
-my $target;
-my $testenv_default = "none";
-
-if ($opt_target eq "samba4") {
-       $testenv_default = "all";
-       require target::Samba4;
-       $target = new Samba4($bindir, $ldap, "$srcdir/setup", $exeext);
-} elsif ($opt_target eq "samba3") {
-       if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
-               die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'.  Exiting....");
-       }
-       $testenv_default = "member";
-       require target::Samba3;
-       $target = new Samba3($bindir);
-} elsif ($opt_target eq "win") {
-       die("Windows tests will not run with socket wrapper enabled.") 
-               if ($opt_socket_wrapper);
-       $testenv_default = "dc";
-       require target::Windows;
-       $target = new Windows();
-} elsif ($opt_target eq "kvm") {
-       die("Kvm tests will not run with socket wrapper enabled.") 
-               if ($opt_socket_wrapper);
-       require target::Kvm;
-       die("No image specified") unless ($opt_image);
-       $target = new Kvm($opt_image, undef);
+if ($opt_use_dns_faking) {
+       print "DNS: Faking nameserver\n";
+       $ENV{SAMBA_DNS_FAKING} = 1;
 }
 
-#
-# Start a Virtual Distributed Ethernet Switch
-# Returns the pid of the switch.
-#
-sub start_vde_switch($)
-{
-       my ($path) = @_;
+my $target;
+my $testenv_default = "none";
 
-       system("vde_switch --pidfile $path/vde.pid --sock $path/vde.sock --daemon");
+# After this many seconds, the server will self-terminate.  All tests
+# must terminate in this time, and testenv will only stay alive this
+# long
 
-       open(PID, "$path/vde.pid");
-       <PID> =~ /([0-9]+)/;
-       my $pid = $1;
-       close(PID);
+my $server_maxtime;
+if ($opt_testenv) {
+    # 1 year should be enough :-)
+    $server_maxtime = 365 * 24 * 60 * 60;
+} else {
+    # make test should run under 4 hours
+    $server_maxtime = 4 * 60 * 60;
+}
 
-       return $pid;
+if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
+    $server_maxtime = $ENV{SMBD_MAXTIME};
 }
 
-# Stop a Virtual Distributed Ethernet Switch
-sub stop_vde_switch($)
-{
-       my ($pid) = @_;
-       kill 9, $pid;
+unless ($opt_list) {
+       if ($opt_target eq "samba") {
+               $testenv_default = "ad_dc_ntvfs";
+               require target::Samba;
+               $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
+       } elsif ($opt_target eq "samba3") {
+               $testenv_default = "nt4_member";
+               require target::Samba3;
+               $target = new Samba3($bindir, $srcdir_abs, $server_maxtime);
+       }
 }
 
 sub read_test_regexes($)
@@ -546,40 +475,94 @@ my $interfaces = join(',', ("127.0.0.11/8",
                            "127.0.0.15/8",
                            "127.0.0.16/8"));
 
-my $conffile = "$prefix_abs/client/client.conf";
+my $clientdir = "$prefix_abs/client";
+
+my $conffile = "$clientdir/client.conf";
 $ENV{SMB_CONF_PATH} = $conffile;
 
-sub write_clientconf($$)
+sub write_clientconf($$$)
 {
-       my ($conffile, $vars) = @_;
+       my ($conffile, $clientdir, $vars) = @_;
 
-       mkdir("$prefix/client", 0777) unless -d "$prefix/client";
+       mkdir("$clientdir", 0777) unless -d "$clientdir";
 
-       if ( -d "$prefix/client/private" ) {
-               unlink <$prefix/client/private/*>;
+       if ( -d "$clientdir/private" ) {
+               unlink <$clientdir/private/*>;
        } else {
-               mkdir("$prefix/client/private", 0777);
+               mkdir("$clientdir/private", 0777);
        }
 
-       if ( -d "$prefix/client/lockdir" ) {
-               unlink <$prefix/client/lockdir/*>;
+       if ( -d "$clientdir/lockdir" ) {
+               unlink <$clientdir/lockdir/*>;
        } else {
-               mkdir("$prefix/client/lockdir", 0777);
+               mkdir("$clientdir/lockdir", 0777);
        }
 
-       if ( -d "$prefix_abs/client/ncalrpcdir" ) {
-               unlink <$prefix/client/ncalrpcdir/*>;
+       if ( -d "$clientdir/statedir" ) {
+               unlink <$clientdir/statedir/*>;
        } else {
-               mkdir("$prefix/client/ncalrpcdir", 0777);
+               mkdir("$clientdir/statedir", 0777);
        }
 
-       open(CF, ">$conffile");
-       print CF "[global]\n";
-       if (defined($ENV{VALGRIND})) {
-               print CF "\ticonv:native = true\n";
+       if ( -d "$clientdir/cachedir" ) {
+               unlink <$clientdir/cachedir/*>;
        } else {
-               print CF "\ticonv:native = false\n";
+               mkdir("$clientdir/cachedir", 0777);
+       }
+
+       # this is ugly, but the ncalrpcdir needs exactly 0755
+       # otherwise tests fail.
+       my $mask = umask;
+       umask 0022;
+       if ( -d "$clientdir/ncalrpcdir/np" ) {
+               unlink <$clientdir/ncalrpcdir/np/*>;
+               rmdir "$clientdir/ncalrpcdir/np";
+       }
+       if ( -d "$clientdir/ncalrpcdir" ) {
+               unlink <$clientdir/ncalrpcdir/*>;
+               rmdir "$clientdir/ncalrpcdir";
        }
+       mkdir("$clientdir/ncalrpcdir", 0755);
+       umask $mask;
+
+       my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
+       my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
+       my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
+       my $ca_users_dir = "$cadir/Users";
+
+       if ( -d "$clientdir/pkinit" ) {
+               unlink <$clientdir/pkinit/*>;
+       } else {
+               mkdir("$clientdir/pkinit", 0700);
+       }
+
+       # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
+       # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
+       # We make a copy here and make the certificated easily
+       # accessable in the client environment.
+       my $mask = umask;
+       umask 0077;
+       opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
+       for my $d (readdir USERS) {
+               my $user_dir = "${ca_users_dir}/${d}";
+               next if ${d} =~ /^\./;
+               next if (! -d "${user_dir}");
+               opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
+               for my $l (readdir USER) {
+                       my $user_link = "${user_dir}/${l}";
+                       next if ${l} =~ /^\./;
+                       next if (! -l "${user_link}");
+
+                       my $dest = "${clientdir}/pkinit/${l}";
+                       Samba::copy_file_content(${user_link}, ${dest});
+               }
+               closedir USER;
+       }
+       closedir USERS;
+       umask $mask;
+
+       open(CF, ">$conffile");
+       print CF "[global]\n";
        print CF "\tnetbios name = client\n";
        if (defined($vars->{DOMAIN})) {
                print CF "\tworkgroup = $vars->{DOMAIN}\n";
@@ -591,33 +574,33 @@ sub write_clientconf($$)
                print CF "\tinterfaces = $interfaces\n";
        }
        print CF "
-       private dir = $prefix_abs/client/private
-       lock dir = $prefix_abs/client/lockdir
-       ncalrpc dir = $prefix_abs/client/ncalrpcdir
-       name resolve order = bcast file
-       panic action = $RealBin/gdb_backtrace \%PID\% \%PROG\%
+       private dir = $clientdir/private
+       lock dir = $clientdir/lockdir
+       state directory = $clientdir/statedir
+       cache directory = $clientdir/cachedir
+       ncalrpc dir = $clientdir/ncalrpcdir
+       panic action = $RealBin/gdb_backtrace \%d
        max xmit = 32K
        notify:inotify = false
        ldb:nosync = true
        system:anonymous = true
        client lanman auth = Yes
        log level = 1
-       torture:basedir = $prefix_abs/client
+       torture:basedir = $clientdir
 #We don't want to pass our self-tests if the PAC code is wrong
        gensec:require_pac = true
-       modules dir = $ENV{LD_SAMBA_MODULE_PATH}
-       setup directory = ./setup
-       resolv:host file = $prefix_abs/dns_host_file
+#We don't want to run 'speed' tests for very long
+        torture:timelimit = 1
+        winbind separator = /
+       tls cafile = ${cacert}
+       tls crlfile = ${cacrl_pem}
+       tls verify peer = no_check
 ";
        close(CF);
 }
 
 my @todo = ();
 
-my $testsdir = "$srcdir/selftest";
-
-my %required_envs = ();
-
 sub should_run_test($)
 {
        my $name = shift;
@@ -640,23 +623,27 @@ sub read_testlist($)
        open(IN, $filename) or die("Unable to open $filename: $!");
 
        while (<IN>) {
-               if (/-- TEST(-LOADLIST)? --\n/) {
+               if (/-- TEST(-LOADLIST|) --\n/) {
                        my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
                        my $name = <IN>;
                        $name =~ s/\n//g;
                        my $env = <IN>;
                        $env =~ s/\n//g;
+                       my $loadlist;
+                       if ($supports_loadlist) {
+                               $loadlist = <IN>;
+                               $loadlist =~ s/\n//g;
+                       }
                        my $cmdline = <IN>;
                        $cmdline =~ s/\n//g;
                        if (should_run_test($name) == 1) {
-                               $required_envs{$env} = 1;
-                               push (@ret, [$name, $env, $cmdline, $supports_loadlist]);
+                               push (@ret, [$name, $env, $cmdline, $loadlist]);
                        }
                } else {
                        print;
                }
        }
-       close(IN) or die("Error creating recipe");
+       close(IN) or die("Error creating recipe from $filename");
        return @ret;
 }
 
@@ -665,22 +652,18 @@ if ($#testlists == -1) {
 }
 
 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
+$ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
+$ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
 if ($opt_socket_wrapper) {
        $ENV{SELFTEST_INTERFACES} = $interfaces;
 } else {
        $ENV{SELFTEST_INTERFACES} = "";
 }
-if ($opt_verbose) {
-       $ENV{SELFTEST_VERBOSE} = "1";
-} else {
-       $ENV{SELFTEST_VERBOSE} = "";
-}
 if ($opt_quick) {
        $ENV{SELFTEST_QUICK} = "1";
 } else {
        $ENV{SELFTEST_QUICK} = "";
 }
-$ENV{SELFTEST_TARGET} = $opt_target;
 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
 
 my @available = ();
@@ -705,18 +688,13 @@ if ($opt_load_list) {
        close(LOAD_LIST);
 }
 
-Subunit::progress($#available+1);
-Subunit::report_time(time());
-
 my $individual_tests = undef;
 $individual_tests = {};
 
 foreach my $testsuite (@available) {
        my $name = $$testsuite[0];
        my $skipreason = skip($name);
-       if (defined($skipreason)) {
-               Subunit::skip_testsuite($name, $skipreason);
-       } elsif (defined($restricted)) {
+       if (defined($restricted)) {
                # Find the testsuite for this test
                my $match = undef;
                foreach my $r (@$restricted) {
@@ -724,15 +702,27 @@ foreach my $testsuite (@available) {
                                $individual_tests->{$name} = [];
                                $match = $r;
                                $restricted_used->{$r} = 1;
-                       } elsif (substr($r, $name, length($name)+1) eq "$name.") {
-                               push(@{$individual_tests->{$name}}, $1);
+                       } elsif (substr($r, 0, length($name)+1) eq "$name.") {
+                               push(@{$individual_tests->{$name}}, $r);
                                $match = $r;
                                $restricted_used->{$r} = 1;
                        }
                }
-               push(@todo, $testsuite) if ($match);
+               if ($match) {
+                       if (defined($skipreason)) {
+                               if (not $opt_list) {
+                                       Subunit::skip_testsuite($name, $skipreason);
+                               }
+                       } else {
+                               push(@todo, $testsuite);
+                       }
+               }
+       } elsif (defined($skipreason)) {
+               if (not $opt_list) {
+                       Subunit::skip_testsuite($name, $skipreason);
+               }
        } else {
-               push(@todo, $testsuite); 
+               push(@todo, $testsuite);
        }
 }
 
@@ -748,6 +738,12 @@ if (defined($restricted)) {
 }
 
 my $suitestotal = $#todo + 1;
+
+unless ($opt_list) {
+       Subunit::progress($suitestotal);
+       Subunit::report_time(time());
+}
+
 my $i = 0;
 $| = 1;
 
@@ -769,33 +765,54 @@ my @exported_envvars = (
        "DOMAIN",
        "REALM",
 
+       # stuff related to a trusted domain
+       "TRUST_SERVER",
+       "TRUST_SERVER_IP",
+       "TRUST_SERVER_IPV6",
+       "TRUST_NETBIOSNAME",
+       "TRUST_USERNAME",
+       "TRUST_PASSWORD",
+       "TRUST_DOMAIN",
+       "TRUST_REALM",
+
        # domain controller stuff
        "DC_SERVER",
        "DC_SERVER_IP",
+       "DC_SERVER_IPV6",
        "DC_NETBIOSNAME",
        "DC_NETBIOSALIAS",
 
        # domain member
        "MEMBER_SERVER",
        "MEMBER_SERVER_IP",
+       "MEMBER_SERVER_IPV6",
        "MEMBER_NETBIOSNAME",
        "MEMBER_NETBIOSALIAS",
 
        # rpc proxy controller stuff
        "RPC_PROXY_SERVER",
        "RPC_PROXY_SERVER_IP",
+       "RPC_PROXY_SERVER_IPV6",
        "RPC_PROXY_NETBIOSNAME",
        "RPC_PROXY_NETBIOSALIAS",
 
        # domain controller stuff for Vampired DC
        "VAMPIRE_DC_SERVER",
        "VAMPIRE_DC_SERVER_IP",
+       "VAMPIRE_DC_SERVER_IPV6",
        "VAMPIRE_DC_NETBIOSNAME",
        "VAMPIRE_DC_NETBIOSALIAS",
 
+       "PROMOTED_DC_SERVER",
+       "PROMOTED_DC_SERVER_IP",
+       "PROMOTED_DC_SERVER_IPV6",
+       "PROMOTED_DC_NETBIOSNAME",
+       "PROMOTED_DC_NETBIOSALIAS",
+
        # server stuff
        "SERVER",
        "SERVER_IP",
+       "SERVER_IPV6",
        "NETBIOSNAME",
        "NETBIOSALIAS",
 
@@ -806,22 +823,50 @@ my @exported_envvars = (
        "DC_USERNAME",
        "DC_PASSWORD",
 
+       # UID/GID for rfc2307 mapping tests
+       "UID_RFC2307TEST",
+       "GID_RFC2307TEST",
+
        # misc stuff
        "KRB5_CONFIG",
-       "WINBINDD_SOCKET_DIR",
+       "SELFTEST_WINBINDD_SOCKET_DIR",
        "WINBINDD_PRIV_PIPE_DIR",
-       "LOCAL_PATH"
+       "NMBD_SOCKET_DIR",
+       "LOCAL_PATH",
+
+       # nss_wrapper
+       "NSS_WRAPPER_PASSWD",
+       "NSS_WRAPPER_GROUP",
+       "NSS_WRAPPER_HOSTS",
+       "NSS_WRAPPER_MODULE_SO_PATH",
+       "NSS_WRAPPER_MODULE_FN_PREFIX",
+
+       # resolv_wrapper
+       "RESOLV_WRAPPER_CONF",
+       "RESOLV_WRAPPER_HOSTS",
 );
 
-$SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub { 
+sub sighandler($)
+{
        my $signame = shift;
+
+       $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
+       $SIG{PIPE} = 'IGNORE';
+
+       open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
+
+       print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
        teardown_env($_) foreach(keys %running_envs);
-       die("Received signal $signame");
+       system("pstree -p $$");
+       print "$0: PID[$$]: Exiting...\n";
+       exit(1);
 };
 
-sub setup_env($)
+$SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
+
+sub setup_env($$)
 {
-       my ($name) = @_;
+       my ($name, $prefix) = @_;
 
        my $testenv_vars = undef;
 
@@ -834,16 +879,22 @@ sub setup_env($)
 
        $option = "client" if $option eq "";
 
-       if ($envname eq "none") {
-               $testenv_vars = {};
-       } elsif (defined(get_running_env($envname))) {
+       if (defined(get_running_env($envname))) {
                $testenv_vars = get_running_env($envname);
-               if (not $target->check_env($testenv_vars)) {
-                       print $target->getlog_env($testenv_vars);
+               if (not $testenv_vars->{target}->check_env($testenv_vars)) {
+                       print $testenv_vars->{target}->getlog_env($testenv_vars);
                        $testenv_vars = undef;
                }
        } else {
                $testenv_vars = $target->setup_env($envname, $prefix);
+               if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
+                   return $testenv_vars;
+               } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
+                       $testenv_vars->{target} = $target;
+               }
+               if (not defined($testenv_vars)) {
+                       warn("$opt_target can't start up known environment '$envname'");
+               }
        }
 
        return undef unless defined($testenv_vars);
@@ -855,7 +906,7 @@ sub setup_env($)
                $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
        } elsif ($option eq "client") {
                SocketWrapper::set_default_iface(11);
-               write_clientconf($conffile, $testenv_vars);
+               write_clientconf($conffile, $clientdir, $testenv_vars);
                $ENV{SMB_CONF_PATH} = $conffile;
        } else {
                die("Unknown option[$option] for envname[$envname]");
@@ -889,43 +940,53 @@ sub getlog_env($)
 {
        my ($envname) = @_;
        return "" if ($envname eq "none");
-       return $target->getlog_env(get_running_env($envname));
+       my $env = get_running_env($envname);
+       return $env->{target}->getlog_env($env);
 }
 
 sub check_env($)
 {
        my ($envname) = @_;
-       return 1 if ($envname eq "none");
-       return $target->check_env(get_running_env($envname));
+       my $env = get_running_env($envname);
+       return $env->{target}->check_env($env);
 }
 
 sub teardown_env($)
 {
        my ($envname) = @_;
        return if ($envname eq "none");
-       $target->teardown_env(get_running_env($envname));
+       print STDERR "teardown_env($envname)\n";
+       my $env = get_running_env($envname);
+       $env->{target}->teardown_env($env);
        delete $running_envs{$envname};
 }
 
 # This 'global' file needs to be empty when we start
 unlink("$prefix_abs/dns_host_file");
+unlink("$prefix_abs/hosts");
 
-if ($opt_no_lazy_setup) {
-       setup_env($_) foreach (keys %required_envs);
+if ($opt_random_order) {
+       require List::Util;
+       my @newtodo = List::Util::shuffle(@todo);
+       @todo = @newtodo;
 }
 
 if ($opt_testenv) {
        my $testenv_name = $ENV{SELFTEST_TESTENV};
        $testenv_name = $testenv_default unless defined($testenv_name);
 
-       my $testenv_vars = setup_env($testenv_name);
+       my $testenv_vars = setup_env($testenv_name, $prefix);
+
+       if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
+               die("Unable to setup environment $testenv_name");
+       }
 
        $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
+       $ENV{ENVNAME} = $testenv_name;
 
        my $envvarstr = exported_envvars_str($testenv_vars);
 
-       my $term = ($ENV{TERMINAL} or "xterm");
-       system("$term -e 'echo -e \"
+       my @term_args = ("echo -e \"
 Welcome to the Samba4 Test environment '$testenv_name'
 
 This matches the client environment used in make test
@@ -936,39 +997,81 @@ TORTURE_OPTIONS=\$TORTURE_OPTIONS
 SMB_CONF_PATH=\$SMB_CONF_PATH
 
 $envvarstr
-\" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash'");
+\" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
+       my @term = ();
+       if ($ENV{TERMINAL}) {
+           @term = ($ENV{TERMINAL});
+       } else {
+           @term = ("xterm", "-e");
+           unshift(@term_args, ("bash", "-c"));
+       }
+
+       system(@term, @term_args);
+
        teardown_env($testenv_name);
+} elsif ($opt_list) {
+       foreach (@todo) {
+               my $name = $$_[0];
+               my $envname = $$_[1];
+               my $cmd = $$_[2];
+               my $listcmd = $$_[3];
+
+               unless (defined($listcmd)) {
+                       warn("Unable to list tests in $name");
+                       # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
+                       # a single "test".
+                       print "$name\n";
+                       next;
+               }
+
+               system($listcmd);
+
+               if ($? == -1) {
+                       die("Unable to run $listcmd: $!");
+               } elsif ($? & 127) {
+                       die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127),  ($? & 128) ? 'with' : 'without'));
+               }
+
+               my $exitcode = $? >> 8;
+               if ($exitcode != 0) {
+                       die("$cmd exited with exit code $exitcode");
+               }
+       }
 } else {
        foreach (@todo) {
                $i++;
                my $cmd = $$_[2];
-               $cmd =~ s/([\(\)])/\\$1/g;
                my $name = $$_[0];
                my $envname = $$_[1];
 
-               my $envvars = setup_env($envname);
+               my $envvars = setup_env($envname, $prefix);
                if (not defined($envvars)) {
-                       Subunit::skip_testsuite($name, 
-                               "unable to set up environment $envname");
+                       Subunit::start_testsuite($name);
+                       Subunit::end_testsuite($name, "error",
+                               "unable to set up environment $envname - exiting");
+                       next;
+               } elsif ($envvars eq "UNKNOWN") {
+                       Subunit::start_testsuite($name);
+                       Subunit::end_testsuite($name, "skip",
+                               "environment $envname is unknown in this test backend - skipping");
                        next;
                }
 
                # Generate a file with the individual tests to run, if the 
                # test runner for this test suite supports it.
-               if ($$_[3] and $individual_tests and $individual_tests->{$name}) {
-                       my ($fh, $listid_file) = tempfile(UNLINK => 0);
-                       foreach (@{$individual_tests->{$name}}) {
-                               print $fh "$_\n";
+               if ($individual_tests and $individual_tests->{$name}) {
+                       if ($$_[3]) {
+                               my ($fh, $listid_file) = tempfile(UNLINK => 0);
+                               foreach my $test (@{$individual_tests->{$name}}) {
+                                       print $fh substr($test, length($name)+1) . "\n";
+                               }
+                               $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
+                       } else {
+                               warn("Unable to run individual tests in $name, it does not support --loadlist.");
                        }
-                       $cmd .= " --load-list=$listid_file";
                }
 
-               run_testsuite($envname, $name, $cmd, $i, $suitestotal,
-                                     );
-
-               if (defined($opt_analyse_cmd)) {
-                       system("$opt_analyse_cmd \"$name\"");
-               }
+               run_testsuite($envname, $name, $cmd, $i, $suitestotal);
 
                teardown_env($envname) if ($opt_resetup_env);
        }