selftest: move all winbind test rules to one place
[samba.git] / selftest / target / Samba4.pm
old mode 100644 (file)
new mode 100755 (executable)
index 495cc23..5ce2e84
@@ -40,7 +40,7 @@ sub openldap_start($$$) {
 sub slapd_start($$)
 {
        my $count = 0;
-       my ($self, $env_vars) = @_;
+       my ($self, $env_vars, $STDIN_READER) = @_;
        my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
 
        my $uri = $env_vars->{LDAP_URI};
@@ -51,11 +51,22 @@ sub slapd_start($$)
        }
        # running slapd in the background means it stays in the same process group, so it can be
        # killed by timelimit
-       if ($self->{ldap} eq "fedora-ds") {
-               system("$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd -D $env_vars->{FEDORA_DS_DIR} -d0 -i $env_vars->{FEDORA_DS_PIDFILE}> $env_vars->{LDAPDIR}/logs 2>&1 &");
-       } elsif ($self->{ldap} eq "openldap") {
-               system("$ENV{OPENLDAP_SLAPD} -d0 -F $env_vars->{SLAPD_CONF_D} -h $uri > $env_vars->{LDAPDIR}/logs 2>&1 &");
+       my $pid = fork();
+       if ($pid == 0) {
+               open STDOUT, ">$env_vars->{LDAPDIR}/logs";
+               open STDERR, '>&STDOUT';
+               close($env_vars->{STDIN_PIPE});
+               open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
+
+               if ($self->{ldap} eq "fedora-ds") {
+                       exec("$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd", "-D", $env_vars->{FEDORA_DS_DIR}, "-d0", "-i", $env_vars->{FEDORA_DS_PIDFILE});
+               } elsif ($self->{ldap} eq "openldap") {
+                       exec($ENV{OPENLDAP_SLAPD}, "-dnone", "-F", $env_vars->{SLAPD_CONF_D}, "-h", $uri);
+               }
+               die("Unable to start slapd: $!");
        }
+       $env_vars->{SLAPD_PID} = $pid;
+       sleep(1);
        while (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") != 0) {
                $count++;
                if ($count > 40) {
@@ -70,29 +81,29 @@ sub slapd_start($$)
 sub slapd_stop($$)
 {
        my ($self, $envvars) = @_;
-       if ($self->{ldap} eq "fedora-ds") {
-               system("$envvars->{LDAPDIR}/slapd-$envvars->{LDAP_INSTANCE}/stop-slapd");
-       } elsif ($self->{ldap} eq "openldap") {
-               unless (open(IN, "<$envvars->{OPENLDAP_PIDFILE}")) {
-                       warn("unable to open slapd pid file: $envvars->{OPENLDAP_PIDFILE}");
-                       return 0;
-               }
-               kill 9, <IN>;
-               close(IN);
-       }
+       kill 9, $envvars->{SLAPD_PID};
        return 1;
 }
 
 sub check_or_start($$$)
 {
         my ($self, $env_vars, $process_model) = @_;
+       my $STDIN_READER;
 
        return 0 if $self->check_env($env_vars);
 
        # use a pipe for stdin in the child processes. This allows
        # those processes to monitor the pipe for EOF to ensure they
        # exit when the test script exits
-       pipe(STDIN_READER, $env_vars->{STDIN_PIPE});
+       pipe($STDIN_READER, $env_vars->{STDIN_PIPE});
+
+       # Start slapd before samba, but with the fifo on stdin
+       if (defined($self->{ldap})) {
+               unless($self->slapd_start($env_vars, $STDIN_READER)) {
+                       warn("couldn't start slapd (main run)");
+                       return undef;
+               }
+       }
 
        print "STARTING SAMBA...";
        my $pid = fork();
@@ -106,12 +117,14 @@ sub check_or_start($$$)
                SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
 
                $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
-               $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
+               $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
                $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
 
                $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
                $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
-               $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
+               $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
+               $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
+               $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
 
                $ENV{UID_WRAPPER} = "1";
 
@@ -126,14 +139,14 @@ sub check_or_start($$$)
                }
 
                close($env_vars->{STDIN_PIPE});
-               open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
+               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: $!");
        }
        $env_vars->{SAMBA_PID} = $pid;
        print "DONE\n";
 
-       close(STDIN_READER);
+       close($STDIN_READER);
 
        return $pid;
 }
@@ -141,6 +154,7 @@ sub check_or_start($$$)
 sub wait_for_start($$)
 {
        my ($self, $testenv_vars) = @_;
+       my $ret;
        # give time for nbt server to register its names
        print "delaying for nbt name registration\n";
        sleep 2;
@@ -161,7 +175,29 @@ sub wait_for_start($$)
        system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
        system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
 
+       # 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->{NETBIOS_NAME} eq "rodc")) {
+           # Add hosts file for name lookups
+           $ENV{NSS_WRAPPER_HOSTS} = $testenv_vars->{NSS_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";
+           sleep(1);
+           while (system("$ldbsearch -H ldap://$testenv_vars->{SERVER} -U$testenv_vars->{USERNAME}%$testenv_vars->{PASSWORD} -s base -b \"$rid_set_dn\" rIDAllocationPool > /dev/null") != 0) {
+               $count++;
+               if ($count > 40) {
+                   $ret = 1;
+                   last;
+               }
+               sleep(1);
+           }
+       }
        print $self->getlog_env($testenv_vars);
+
+       return $ret
 }
 
 sub write_ldb_file($$$)
@@ -227,7 +263,9 @@ sub mk_keyblobs($$)
        my $admincertfile = "$tlsdir/admincert.pem";
        my $admincertupnfile = "$tlsdir/admincertupn.pem";
 
-       mkdir($tlsdir, 0777);
+       mkdir($tlsdir, 0700);
+       my $oldumask = umask;
+       umask 0077;
 
        #This is specified here to avoid draining entropy on every run
        open(DHFILE, ">$dhfile");
@@ -418,6 +456,8 @@ Zd7J9s//rNFNa7waklFkDaY56+QWTFtdvxfE+KoHaqt6X8u6pqi7p3M4wDKQox+9Dx8yWFyq
 Wfz/8alZ5aMezCQzXJyIaJsCLeKABosSwHcpAFmxlQ==
 -----END CERTIFICATE-----
 EOF
+
+       umask $oldumask;
 }
 
 sub provision_raw_prepare($$$$$$$$$$)
@@ -471,6 +511,8 @@ sub provision_raw_prepare($$$$$$$$$$)
        chomp $unix_name;
        $ctx->{unix_name} = $unix_name;
        $ctx->{unix_uid} = $>;
+       my @mygid = split(" ", $();
+       $ctx->{unix_gid} = $mygid[0];
        $ctx->{unix_gids_str} = $);
        @{$ctx->{unix_gids}} = split(" ", $ctx->{unix_gids_str});
 
@@ -489,6 +531,7 @@ sub provision_raw_prepare($$$$$$$$$$)
        $ctx->{ntp_signd_socket_dir} = "$prefix_abs/ntp_signd_socket";
        $ctx->{nsswrap_passwd} = "$ctx->{etcdir}/passwd";
        $ctx->{nsswrap_group} = "$ctx->{etcdir}/group";
+       $ctx->{nsswrap_hosts} = "$ENV{SELFTEST_PREFIX}/hosts";
 
        $ctx->{tlsdir} = "$ctx->{privatedir}/tls";
 
@@ -508,6 +551,7 @@ sub provision_raw_prepare($$$$$$$$$$)
        my @provision_options = ();
        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}\"");
        if (defined($ENV{GDB_PROVISION})) {
                push (@provision_options, "gdb --args");
                if (!defined($ENV{PYTHON})) {
@@ -610,6 +654,8 @@ sub provision_raw_step1($$)
        server signing = on
 
         idmap_ldb:use rfc2307=yes
+       winbind enum users = yes
+       winbind enum groups = yes
 ";
 
        print CONFFILE "
@@ -632,8 +678,12 @@ sub provision_raw_step1($$)
        open(PWD, ">$ctx->{nsswrap_passwd}");
        print PWD "
 root:x:0:0:root gecos:$ctx->{prefix_abs}:/bin/false
+$ctx->{unix_name}:x:$ctx->{unix_uid}:100:$ctx->{unix_name} gecos:$ctx->{prefix_abs}:/bin/false
 nobody:x:65534:65533:nobody gecos:$ctx->{prefix_abs}:/bin/false
 pdbtest:x:65533:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
+pdbtest2:x:65532:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
+pdbtest3:x:65531:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
+pdbtest4:x:65530:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
 ";
        close(PWD);
         my $uid_rfc2307test = 65533;
@@ -645,10 +695,17 @@ wheel:x:10:
 users:x:100:
 nobody:x:65533:
 nogroup:x:65534:nobody
+$ctx->{unix_name}:x:$ctx->{unix_gid}:
 ";
        close(GRP);
         my $gid_rfc2307test = 65532;
 
+       my $hostname = lc($ctx->{hostname});
+       open(HOSTS, ">>$ctx->{nsswrap_hosts}");
+       print HOSTS "$ctx->{ipv4} ${hostname}.samba.example.com ${hostname}
+";
+       close(HOSTS);
+
        my $configuration = "--configfile=$ctx->{smb_conf}";
 
 #Ensure the config file is valid before we start
@@ -675,7 +732,7 @@ nogroup:x:65534:nobody
                PASSWORD => $ctx->{password},
                LDAPDIR => $ctx->{ldapdir},
                LDAP_INSTANCE => $ctx->{ldap_instance},
-               WINBINDD_SOCKET_DIR => $ctx->{winbindd_socket_dir},
+               SELFTEST_WINBINDD_SOCKET_DIR => $ctx->{winbindd_socket_dir},
                NCALRPCDIR => $ctx->{ncalrpcdir},
                LOCKDIR => $ctx->{lockdir},
                STATEDIR => $ctx->{statedir},
@@ -686,13 +743,16 @@ nogroup:x:65534:nobody
                SOCKET_WRAPPER_DEFAULT_IFACE => $ctx->{swiface},
                NSS_WRAPPER_PASSWD => $ctx->{nsswrap_passwd},
                NSS_WRAPPER_GROUP => $ctx->{nsswrap_group},
+               NSS_WRAPPER_HOSTS => $ctx->{nsswrap_hosts},
                SAMBA_TEST_FIFO => "$ctx->{prefix}/samba_test.fifo",
                SAMBA_TEST_LOG => "$ctx->{prefix}/samba_test.log",
                SAMBA_TEST_LOG_POS => 0,
-               NSS_WRAPPER_WINBIND_SO_PATH => Samba::nss_wrapper_winbind_so_path($self),
+               NSS_WRAPPER_MODULE_SO_PATH => Samba::nss_wrapper_winbind_so_path($self),
+               NSS_WRAPPER_MODULE_FN_PREFIX => "winbind",
                 LOCAL_PATH => $ctx->{share},
                 UID_RFC2307TEST => $uid_rfc2307test,
-                GID_RFC2307TEST => $gid_rfc2307test
+                GID_RFC2307TEST => $gid_rfc2307test,
+                SERVER_ROLE => $ctx->{server_role}
        };
 
        return $ret;
@@ -750,6 +810,7 @@ sub provision($$$$$$$$$)
        server max protocol = SMB2
        host msdfs = $msdfs
        lanman auth = yes
+       allow nt4 crypto = yes
 
        $extra_smbconf_options
 
@@ -857,7 +918,7 @@ $extra_smbconf_shares
        return $self->provision_raw_step2($ctx, $ret);
 }
 
-sub provision_member($$$)
+sub provision_s4member($$$)
 {
        my ($self, $prefix, $dcvars) = @_;
        print "PROVISIONING MEMBER...";
@@ -961,7 +1022,8 @@ sub provision_rpc_proxy($$$)
        $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
        $cmd .= "$samba_tool delegation for-any-protocol '$ret->{NETBIOSNAME}\$' on";
-       $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} $dcvars->{CONFIGURATION}";
+        $cmd .= " $dcvars->{CONFIGURATION}";
+        print $cmd;
 
        unless (system($cmd) == 0) {
                warn("Delegation failed\n$cmd");
@@ -973,7 +1035,7 @@ sub provision_rpc_proxy($$$)
        $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
        $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
        $cmd .= "$samba_tool delegation add-service '$ret->{NETBIOSNAME}\$' cifs/$dcvars->{SERVER}";
-       $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} $dcvars->{CONFIGURATION}";
+        $cmd .= " $dcvars->{CONFIGURATION}";
 
        unless (system($cmd) == 0) {
                warn("Delegation failed\n$cmd");
@@ -1049,7 +1111,7 @@ sub provision_promoted_dc($$$)
        $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
        $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";
+       $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs --dns-backend=BIND9_DLZ";
 
        unless (system($cmd) == 0) {
                warn("Join failed\n$cmd");
@@ -1261,6 +1323,34 @@ sub provision_fl2003dc($$)
                                   "locDCpass6",
                                   undef, "allow dns updates = nonsecure and secure", "", undef);
 
+       unless (defined $ret) {
+               return undef;
+       }
+
+       $ret->{DC_SERVER} = $ret->{SERVER};
+       $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
+       $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
+       $ret->{DC_USERNAME} = $ret->{USERNAME};
+       $ret->{DC_PASSWORD} = $ret->{PASSWORD};
+
+       my @samba_tool_options;
+       push (@samba_tool_options, Samba::bindir_path($self, "samba-tool"));
+       push (@samba_tool_options, "domain");
+       push (@samba_tool_options, "passwordsettings");
+       push (@samba_tool_options, "set");
+       push (@samba_tool_options, "--configfile=$ret->{SERVERCONFFILE}");
+       push (@samba_tool_options, "--min-pwd-age=0");
+       push (@samba_tool_options, "--history-length=1");
+
+       my $samba_tool_cmd = join(" ", @samba_tool_options);
+
+       unless (system($samba_tool_cmd) == 0) {
+               warn("Unable to set min password age to 0: \n$samba_tool_cmd\n");
+               return undef;
+       }
+
+        return $ret;
+
        unless($self->add_wins_config("$prefix/private")) {
                warn("Unable to add wins configuration");
                return undef;
@@ -1386,6 +1476,8 @@ sub provision_plugin_s4_dc($$)
         server services = -smb +s3fs
         xattr_tdb:file = $prefix_abs/statedir/xattr.tdb
 
+       dbwrap_tdb_mutexes:* = yes
+
        kernel oplocks = no
        kernel change notify = no
 
@@ -1418,6 +1510,7 @@ sub provision_plugin_s4_dc($$)
        queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
        queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
        lpq cache time = 0
+       print notify backchannel = yes
 
 ";
 
@@ -1465,7 +1558,7 @@ sub provision_plugin_s4_dc($$)
                                   "domain controller",
                                   "plugindc",
                                   "PLUGINDOMAIN",
-                                  "plugin.samba.example.com",
+                                  "plugindc.samba.example.com",
                                   "2008",
                                   "locDCpass1",
                                   undef, $extra_smbconf_options,
@@ -1500,7 +1593,7 @@ sub provision_chgdcpass($$)
                                   "chgdcpassword.samba.example.com",
                                   "2008",
                                   "chgDCpass1",
-                                  undef, "server services = -dns", "",
+                                  undef, "", "",
                                   $extra_provision_options);
 
        return undef unless(defined $ret);
@@ -1546,22 +1639,17 @@ sub teardown_env($$)
            $count++;
        }
 
-       if ($count <= 20 && kill(0, $pid) == 0) {
-           return;
-       }
-
-       kill "TERM", $pid;
+       if ($count > 30 || kill(0, $pid)) {
+           kill "TERM", $pid;
 
-       until ($count > 20) {
-           if (Samba::cleanup_child($pid, "samba") == -1) {
-               last;
+           until ($count > 40) {
+               if (Samba::cleanup_child($pid, "samba") == -1) {
+                   last;
+               }
+               sleep(1);
+               $count++;
            }
-           sleep(1);
-           $count++;
-       }
-
-       # If it is still around, kill it
-       if ($count > 20 && kill(0, $pid) == 0) {
+           # If it is still around, kill it
            warn "server process $pid took more than $count seconds to exit, killing\n";
            kill 9, $pid;
        }
@@ -1645,7 +1733,7 @@ sub setup_env($$$)
                if (not defined($self->{vars}->{dc})) {
                        $self->setup_dc("$path/dc");
                }
-               return $self->setup_member("$path/s4member", $self->{vars}->{dc});
+               return $self->setup_s4member("$path/s4member", $self->{vars}->{dc});
        } elsif ($envname eq "rodc") {
                if (not defined($self->{vars}->{dc})) {
                        $self->setup_dc("$path/dc");
@@ -1671,18 +1759,18 @@ sub setup_env($$$)
        }
 }
 
-sub setup_member($$$)
+sub setup_s4member($$$)
 {
        my ($self, $path, $dc_vars) = @_;
 
-       my $env = $self->provision_member($path, $dc_vars);
+       my $env = $self->provision_s4member($path, $dc_vars);
 
        if (defined $env) {
                $self->check_or_start($env, "single");
 
                $self->wait_for_start($env);
 
-               $self->{vars}->{member} = $env;
+               $self->{vars}->{s4member} = $env;
        }
 
        return $env;