Add testcase for idmap_rfc2307 module
[Samba.git] / selftest / target / Samba3.pm
blob8d9c4b931062546a2d45b6bc25eeaad7dbfda985
1 #!/usr/bin/perl
2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2005-2007 Jelmer Vernooij <jelmer@samba.org>
4 # Published under the GNU GPL, v3 or later.
6 package Samba3;
8 use strict;
9 use Cwd qw(abs_path);
10 use FindBin qw($RealBin);
11 use POSIX;
12 use target::Samba;
14 sub have_ads($) {
15 my ($self) = @_;
16 my $found_ads = 0;
17 my $smbd_build_options = Samba::bindir_path($self, "smbd") . " -b|";
18 open(IN, $smbd_build_options) or die("Unable to run $smbd_build_options: $!");
20 while (<IN>) {
21 if (/WITH_ADS/) {
22 $found_ads = 1;
25 close IN;
27 # If we were not built with ADS support, pretend we were never even available
28 return $found_ads;
31 sub new($$) {
32 my ($classname, $bindir, $binary_mapping, $srcdir, $server_maxtime) = @_;
33 my $self = { vars => {},
34 bindir => $bindir,
35 binary_mapping => $binary_mapping,
36 srcdir => $srcdir,
37 server_maxtime => $server_maxtime
39 bless $self;
40 return $self;
43 sub teardown_env($$)
45 my ($self, $envvars) = @_;
46 my $count = 0;
48 # This should cause smbd to terminate gracefully
49 close($envvars->{STDIN_PIPE});
51 my $smbdpid = $envvars->{SMBD_TL_PID};
52 my $nmbdpid = $envvars->{NMBD_TL_PID};
53 my $winbinddpid = $envvars->{WINBINDD_TL_PID};
55 # This should give it time to write out the gcov data
56 until ($count > 20) {
57 my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
58 my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
59 my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
60 if ($smbdchild == -1
61 && $nmbdchild == -1
62 && $winbinddchild == -1) {
63 last;
65 sleep(1);
66 $count++;
69 if ($count <= 20 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
70 return;
73 $self->stop_sig_term($smbdpid);
74 $self->stop_sig_term($nmbdpid);
75 $self->stop_sig_term($winbinddpid);
77 $count = 0;
78 until ($count > 10) {
79 my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
80 my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
81 my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
82 if ($smbdchild == -1
83 && $nmbdchild == -1
84 && $winbinddchild == -1) {
85 last;
87 sleep(1);
88 $count++;
91 if ($count <= 10 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
92 return;
95 warn("timelimit process did not quit on SIGTERM, sending SIGKILL");
96 $self->stop_sig_kill($smbdpid);
97 $self->stop_sig_kill($nmbdpid);
98 $self->stop_sig_kill($winbinddpid);
100 return 0;
103 sub getlog_env_app($$$)
105 my ($self, $envvars, $name) = @_;
107 my $title = "$name LOG of: $envvars->{NETBIOSNAME}\n";
108 my $out = $title;
110 open(LOG, "<".$envvars->{$name."_TEST_LOG"});
112 seek(LOG, $envvars->{$name."_TEST_LOG_POS"}, SEEK_SET);
113 while (<LOG>) {
114 $out .= $_;
116 $envvars->{$name."_TEST_LOG_POS"} = tell(LOG);
117 close(LOG);
119 return "" if $out eq $title;
121 return $out;
124 sub getlog_env($$)
126 my ($self, $envvars) = @_;
127 my $ret = "";
129 $ret .= $self->getlog_env_app($envvars, "SMBD");
130 $ret .= $self->getlog_env_app($envvars, "NMBD");
131 $ret .= $self->getlog_env_app($envvars, "WINBINDD");
133 return $ret;
136 sub check_env($$)
138 my ($self, $envvars) = @_;
140 my $childpid = waitpid(-1, WNOHANG);
142 # TODO ...
143 return 1;
146 sub setup_env($$$)
148 my ($self, $envname, $path) = @_;
150 if (defined($self->{vars}->{$envname})) {
151 return $self->{vars}->{$envname};
154 if ($envname eq "s3dc") {
155 return $self->setup_s3dc("$path/s3dc");
156 } elsif ($envname eq "simpleserver") {
157 return $self->setup_simpleserver("$path/simpleserver");
158 } elsif ($envname eq "maptoguest") {
159 return $self->setup_maptoguest("$path/maptoguest");
160 } elsif ($envname eq "ktest") {
161 return $self->setup_ktest("$path/ktest");
162 } elsif ($envname eq "member") {
163 if (not defined($self->{vars}->{s3dc})) {
164 if (not defined($self->setup_s3dc("$path/s3dc"))) {
165 return undef;
168 return $self->setup_member("$path/member", $self->{vars}->{s3dc});
169 } else {
170 return "UNKNOWN";
174 sub setup_s3dc($$)
176 my ($self, $path) = @_;
178 print "PROVISIONING S3DC...";
180 my $s3dc_options = "
181 domain master = yes
182 domain logons = yes
183 lanman auth = yes
185 rpc_server:epmapper = external
186 rpc_server:spoolss = external
187 rpc_server:lsarpc = external
188 rpc_server:samr = external
189 rpc_server:netlogon = external
190 rpc_server:register_embedded_np = yes
192 rpc_daemon:epmd = fork
193 rpc_daemon:spoolssd = fork
194 rpc_daemon:lsasd = fork
197 my $vars = $self->provision($path,
198 "LOCALS3DC2",
199 "locals3dc2pass",
200 $s3dc_options);
202 $vars or return undef;
204 if (not $self->check_or_start($vars, "yes", "yes", "yes")) {
205 return undef;
208 $vars->{DC_SERVER} = $vars->{SERVER};
209 $vars->{DC_SERVER_IP} = $vars->{SERVER_IP};
210 $vars->{DC_NETBIOSNAME} = $vars->{NETBIOSNAME};
211 $vars->{DC_USERNAME} = $vars->{USERNAME};
212 $vars->{DC_PASSWORD} = $vars->{PASSWORD};
214 $self->{vars}->{s3dc} = $vars;
216 return $vars;
219 sub setup_member($$$)
221 my ($self, $prefix, $s3dcvars) = @_;
223 print "PROVISIONING MEMBER...";
225 my $member_options = "
226 security = domain
227 server signing = on
229 my $ret = $self->provision($prefix,
230 "LOCALMEMBER3",
231 "localmember3pass",
232 $member_options);
234 $ret or return undef;
236 my $net = Samba::bindir_path($self, "net");
237 my $cmd = "";
238 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
239 $cmd .= "$net join $ret->{CONFIGURATION} $s3dcvars->{DOMAIN} member";
240 $cmd .= " -U$s3dcvars->{USERNAME}\%$s3dcvars->{PASSWORD}";
242 if (system($cmd) != 0) {
243 warn("Join failed\n$cmd");
244 return undef;
247 if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
248 return undef;
251 $ret->{DC_SERVER} = $s3dcvars->{SERVER};
252 $ret->{DC_SERVER_IP} = $s3dcvars->{SERVER_IP};
253 $ret->{DC_NETBIOSNAME} = $s3dcvars->{NETBIOSNAME};
254 $ret->{DC_USERNAME} = $s3dcvars->{USERNAME};
255 $ret->{DC_PASSWORD} = $s3dcvars->{PASSWORD};
257 return $ret;
260 sub setup_admember($$$$)
262 my ($self, $prefix, $dcvars) = @_;
264 # If we didn't build with ADS, pretend this env was never available
265 if (not $self->have_ads()) {
266 return "UNKNOWN";
269 print "PROVISIONING S3 AD MEMBER...";
271 my $member_options = "
272 security = ads
273 server signing = on
274 workgroup = $dcvars->{DOMAIN}
275 realm = $dcvars->{REALM}
278 my $ret = $self->provision($prefix,
279 "LOCALADMEMBER",
280 "loCalMemberPass",
281 $member_options);
283 $ret or return undef;
285 close(USERMAP);
286 $ret->{DOMAIN} = $dcvars->{DOMAIN};
287 $ret->{REALM} = $dcvars->{REALM};
289 my $ctx;
290 my $prefix_abs = abs_path($prefix);
291 $ctx = {};
292 $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
293 $ctx->{domain} = $dcvars->{DOMAIN};
294 $ctx->{realm} = $dcvars->{REALM};
295 $ctx->{dnsname} = lc($dcvars->{REALM});
296 $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
297 Samba::mk_krb5_conf($ctx, "");
299 $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
301 my $net = Samba::bindir_path($self, "net");
302 my $cmd = "";
303 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
304 $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
305 $cmd .= "$net join $ret->{CONFIGURATION}";
306 $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
308 if (system($cmd) != 0) {
309 warn("Join failed\n$cmd");
310 return undef;
313 # We need world access to this share, as otherwise the domain
314 # administrator from the AD domain provided by Samba4 can't
315 # access the share for tests.
316 chmod 0777, "$prefix/share";
318 if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
319 return undef;
322 $ret->{DC_SERVER} = $dcvars->{SERVER};
323 $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
324 $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
325 $ret->{DC_USERNAME} = $dcvars->{USERNAME};
326 $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
328 # Special case, this is called from Samba4.pm but needs to use the Samba3 check_env and get_log_env
329 $ret->{target} = $self;
331 return $ret;
334 sub setup_admember_rfc2307($$$$)
336 my ($self, $prefix, $dcvars) = @_;
338 # If we didn't build with ADS, pretend this env was never available
339 if (not $self->have_ads()) {
340 return "UNKNOWN";
343 print "PROVISIONING S3 AD MEMBER WITH idmap_rfc2307 config...";
345 my $member_options = "
346 security = ads
347 server signing = on
348 workgroup = $dcvars->{DOMAIN}
349 realm = $dcvars->{REALM}
350 idmap config $dcvars->{DOMAIN} : backend = rfc2307
351 idmap config $dcvars->{DOMAIN} : range = 2000000-2999999
352 idmap config $dcvars->{DOMAIN} : ldap_server = ad
353 idmap config $dcvars->{DOMAIN} : bind_path_user = ou=idmap,dc=samba,dc=example,dc=com
354 idmap config $dcvars->{DOMAIN} : bind_path_group = ou=idmap,dc=samba,dc=example,dc=com
357 my $ret = $self->provision($prefix,
358 "RFC2307MEMBER",
359 "loCalMemberPass",
360 $member_options);
362 $ret or return undef;
364 close(USERMAP);
365 $ret->{DOMAIN} = $dcvars->{DOMAIN};
366 $ret->{REALM} = $dcvars->{REALM};
368 my $ctx;
369 my $prefix_abs = abs_path($prefix);
370 $ctx = {};
371 $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
372 $ctx->{domain} = $dcvars->{DOMAIN};
373 $ctx->{realm} = $dcvars->{REALM};
374 $ctx->{dnsname} = lc($dcvars->{REALM});
375 $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
376 Samba::mk_krb5_conf($ctx, "");
378 $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
380 my $net = Samba::bindir_path($self, "net");
381 my $cmd = "";
382 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
383 $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
384 $cmd .= "$net join $ret->{CONFIGURATION}";
385 $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
387 if (system($cmd) != 0) {
388 warn("Join failed\n$cmd");
389 return undef;
392 # We need world access to this share, as otherwise the domain
393 # administrator from the AD domain provided by Samba4 can't
394 # access the share for tests.
395 chmod 0777, "$prefix/share";
397 if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
398 return undef;
401 $ret->{DC_SERVER} = $dcvars->{SERVER};
402 $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
403 $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
404 $ret->{DC_USERNAME} = $dcvars->{USERNAME};
405 $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
407 # Special case, this is called from Samba4.pm but needs to use the Samba3 check_env and get_log_env
408 $ret->{target} = $self;
410 return $ret;
413 sub setup_simpleserver($$)
415 my ($self, $path) = @_;
416 my $vfs_modulesdir_abs = $ENV{VFSLIBDIR};
418 print "PROVISIONING server with security=share...";
420 my $prefix_abs = abs_path($path);
422 my $simpleserver_options = "
423 lanman auth = yes
424 vfs objects = $vfs_modulesdir_abs/xattr_tdb.so $vfs_modulesdir_abs/streams_depot.so
426 [vfs_aio_fork]
427 path = $prefix_abs/share
428 vfs objects = $vfs_modulesdir_abs/aio_fork.so
429 read only = no
430 vfs_aio_fork:erratic_testing_mode=yes
433 my $vars = $self->provision($path,
434 "LOCALSHARE4",
435 "local4pass",
436 $simpleserver_options);
438 $vars or return undef;
440 if (not $self->check_or_start($vars, "yes", "no", "yes")) {
441 return undef;
444 $self->{vars}->{simpleserver} = $vars;
446 return $vars;
449 sub setup_ktest($$$)
451 my ($self, $prefix) = @_;
453 # If we didn't build with ADS, pretend this env was never available
454 if (not $self->have_ads()) {
455 return "UNKNOWN";
458 print "PROVISIONING server with security=ads...";
460 my $ktest_options = "
461 workgroup = KTEST
462 realm = ktest.samba.example.com
463 security = ads
464 username map = $prefix/lib/username.map
465 server signing = required
468 my $ret = $self->provision($prefix,
469 "LOCALKTEST6",
470 "localktest6pass",
471 $ktest_options);
473 $ret or return undef;
475 my $ctx;
476 my $prefix_abs = abs_path($prefix);
477 $ctx = {};
478 $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
479 $ctx->{domain} = "KTEST";
480 $ctx->{realm} = "KTEST.SAMBA.EXAMPLE.COM";
481 $ctx->{dnsname} = lc($ctx->{realm});
482 $ctx->{kdc_ipv4} = "0.0.0.0";
483 Samba::mk_krb5_conf($ctx, "");
485 $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
487 open(USERMAP, ">$prefix/lib/username.map") or die("Unable to open $prefix/lib/username.map");
488 print USERMAP "
489 $ret->{USERNAME} = KTEST\\Administrator
491 close(USERMAP);
493 #This is the secrets.tdb created by 'net ads join' from Samba3 to a
494 #Samba4 DC with the same parameters as are being used here. The
495 #domain SID is S-1-5-21-1071277805-689288055-3486227160
497 system("cp $self->{srcdir}/source3/selftest/ktest-secrets.tdb $prefix/private/secrets.tdb");
498 chmod 0600, "$prefix/private/secrets.tdb";
500 #Make sure there's no old ntdb file.
501 system("rm -f $prefix/private/secrets.ntdb");
503 #This uses a pre-calculated krb5 credentials cache, obtained by running Samba4 with:
504 # "--option=kdc:service ticket lifetime=239232" "--option=kdc:user ticket lifetime=239232" "--option=kdc:renewal lifetime=239232"
506 #and having in krb5.conf:
507 # ticket_lifetime = 799718400
508 # renew_lifetime = 799718400
510 # The commands for the -2 keytab where were:
511 # kinit administrator@KTEST.SAMBA.EXAMPLE.COM
512 # kvno host/localktest6@KTEST.SAMBA.EXAMPLE.COM
513 # kvno cifs/localktest6@KTEST.SAMBA.EXAMPLE.COM
514 # kvno host/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
515 # kvno cifs/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
517 # and then for the -3 keytab, I did
519 # net changetrustpw; kdestroy and the same again.
521 # This creates a credential cache with a very long lifetime (2036 at
522 # at 2011-04), and shows that running 'net changetrustpw' does not
523 # break existing logins (for the secrets.tdb method at least).
526 $ret->{KRB5_CCACHE}="FILE:$prefix/krb5_ccache";
528 system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-2 $prefix/krb5_ccache-2");
529 chmod 0600, "$prefix/krb5_ccache-2";
531 system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-3 $prefix/krb5_ccache-3");
532 chmod 0600, "$prefix/krb5_ccache-3";
534 # We need world access to this share, as otherwise the domain
535 # administrator from the AD domain provided by ktest can't
536 # access the share for tests.
537 chmod 0777, "$prefix/share";
539 if (not $self->check_or_start($ret, "yes", "no", "yes")) {
540 return undef;
542 return $ret;
545 sub setup_maptoguest($$)
547 my ($self, $path) = @_;
549 print "PROVISIONING maptoguest...";
551 my $options = "
552 map to guest = bad user
555 my $vars = $self->provision($path,
556 "maptoguest",
557 "maptoguestpass",
558 $options);
560 $vars or return undef;
562 if (not $self->check_or_start($vars, "yes", "no", "yes")) {
563 return undef;
566 $self->{vars}->{s3maptoguest} = $vars;
568 return $vars;
571 sub stop_sig_term($$) {
572 my ($self, $pid) = @_;
573 kill("USR1", $pid) or kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
576 sub stop_sig_kill($$) {
577 my ($self, $pid) = @_;
578 kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
581 sub write_pid($$$)
583 my ($env_vars, $app, $pid) = @_;
585 open(PID, ">$env_vars->{PIDDIR}/timelimit.$app.pid");
586 print PID $pid;
587 close(PID);
590 sub read_pid($$)
592 my ($env_vars, $app) = @_;
594 open(PID, "<$env_vars->{PIDDIR}/timelimit.$app.pid");
595 my $pid = <PID>;
596 close(PID);
597 return $pid;
600 sub check_or_start($$$$$) {
601 my ($self, $env_vars, $nmbd, $winbindd, $smbd) = @_;
603 # use a pipe for stdin in the child processes. This allows
604 # those processes to monitor the pipe for EOF to ensure they
605 # exit when the test script exits
606 pipe(STDIN_READER, $env_vars->{STDIN_PIPE});
608 unlink($env_vars->{NMBD_TEST_LOG});
609 print "STARTING NMBD...";
610 my $pid = fork();
611 if ($pid == 0) {
612 open STDOUT, ">$env_vars->{NMBD_TEST_LOG}";
613 open STDERR, '>&STDOUT';
615 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
617 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
618 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
619 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
621 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
622 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
623 $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
625 $ENV{UID_WRAPPER} = "1";
627 if ($nmbd ne "yes") {
628 $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
629 my $signame = shift;
630 print("Skip nmbd received signal $signame");
631 exit 0;
633 sleep($self->{server_maxtime});
634 exit 0;
637 my @optargs = ("-d0");
638 if (defined($ENV{NMBD_OPTIONS})) {
639 @optargs = split(/ /, $ENV{NMBD_OPTIONS});
642 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "nmbd");
644 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
645 if(defined($ENV{NMBD_VALGRIND})) {
646 @preargs = split(/ /, $ENV{NMBD_VALGRIND});
649 close($env_vars->{STDIN_PIPE});
650 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
652 exec(@preargs, Samba::bindir_path($self, "nmbd"), "-F", "--no-process-group", "--log-stdout", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start nmbd: $!");
654 $env_vars->{NMBD_TL_PID} = $pid;
655 write_pid($env_vars, "nmbd", $pid);
656 print "DONE\n";
658 unlink($env_vars->{WINBINDD_TEST_LOG});
659 print "STARTING WINBINDD...";
660 $pid = fork();
661 if ($pid == 0) {
662 open STDOUT, ">$env_vars->{WINBINDD_TEST_LOG}";
663 open STDERR, '>&STDOUT';
665 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
667 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
668 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
669 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
671 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
672 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
673 $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
675 $ENV{UID_WRAPPER} = "1";
677 if ($winbindd ne "yes") {
678 $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
679 my $signame = shift;
680 print("Skip winbindd received signal $signame");
681 exit 0;
683 sleep($self->{server_maxtime});
684 exit 0;
687 my @optargs = ("-d0");
688 if (defined($ENV{WINBINDD_OPTIONS})) {
689 @optargs = split(/ /, $ENV{WINBINDD_OPTIONS});
692 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "winbindd");
694 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
695 if(defined($ENV{WINBINDD_VALGRIND})) {
696 @preargs = split(/ /, $ENV{WINBINDD_VALGRIND});
699 print "Starting winbindd with config $env_vars->{SERVERCONFFILE}\n";
701 close($env_vars->{STDIN_PIPE});
702 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
704 exec(@preargs, Samba::bindir_path($self, "winbindd"), "-F", "--no-process-group", "--stdout", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start winbindd: $!");
706 $env_vars->{WINBINDD_TL_PID} = $pid;
707 write_pid($env_vars, "winbindd", $pid);
708 print "DONE\n";
710 unlink($env_vars->{SMBD_TEST_LOG});
711 print "STARTING SMBD...";
712 $pid = fork();
713 if ($pid == 0) {
714 open STDOUT, ">$env_vars->{SMBD_TEST_LOG}";
715 open STDERR, '>&STDOUT';
717 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
719 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
720 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
721 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
723 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
724 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
725 $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
727 $ENV{UID_WRAPPER} = "1";
729 if ($smbd ne "yes") {
730 $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
731 my $signame = shift;
732 print("Skip smbd received signal $signame");
733 exit 0;
735 sleep($self->{server_maxtime});
736 exit 0;
739 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "smbd");
740 my @optargs = ("-d0");
741 if (defined($ENV{SMBD_OPTIONS})) {
742 @optargs = split(/ /, $ENV{SMBD_OPTIONS});
744 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
745 if(defined($ENV{SMBD_VALGRIND})) {
746 @preargs = split(/ /,$ENV{SMBD_VALGRIND});
749 close($env_vars->{STDIN_PIPE});
750 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
752 exec(@preargs, Samba::bindir_path($self, "smbd"), "-F", "--no-process-group", "--log-stdout", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start smbd: $!");
754 $env_vars->{SMBD_TL_PID} = $pid;
755 write_pid($env_vars, "smbd", $pid);
756 print "DONE\n";
758 close(STDIN_READER);
760 return $self->wait_for_start($env_vars, $nmbd, $winbindd, $smbd);
763 sub provision($$$$$$)
765 my ($self, $prefix, $server, $password, $extra_options, $no_delete_prefix) = @_;
768 ## setup the various environment variables we need
771 my $swiface = Samba::get_interface($server);
772 my %ret = ();
773 my $server_ip = "127.0.0.$swiface";
774 my $domain = "SAMBA-TEST";
776 my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `PATH=/usr/ucb:$ENV{PATH} whoami`);
777 chomp $unix_name;
778 my $unix_uid = $>;
779 my $unix_gids_str = $);
780 my @unix_gids = split(" ", $unix_gids_str);
782 my $prefix_abs = abs_path($prefix);
783 my $bindir_abs = abs_path($self->{bindir});
784 my $vfs_modulesdir_abs = ($ENV{VFSLIBDIR} or $bindir_abs);
786 my $dns_host_file = "$ENV{SELFTEST_PREFIX}/dns_host_file";
788 my @dirs = ();
790 my $shrdir="$prefix_abs/share";
791 push(@dirs,$shrdir);
793 my $libdir="$prefix_abs/lib";
794 push(@dirs,$libdir);
796 my $piddir="$prefix_abs/pid";
797 push(@dirs,$piddir);
799 my $privatedir="$prefix_abs/private";
800 push(@dirs,$privatedir);
802 my $lockdir="$prefix_abs/lockdir";
803 push(@dirs,$lockdir);
805 my $eventlogdir="$prefix_abs/lockdir/eventlog";
806 push(@dirs,$eventlogdir);
808 my $logdir="$prefix_abs/logs";
809 push(@dirs,$logdir);
811 my $driver32dir="$shrdir/W32X86";
812 push(@dirs,$driver32dir);
814 my $driver64dir="$shrdir/x64";
815 push(@dirs,$driver64dir);
817 my $driver40dir="$shrdir/WIN40";
818 push(@dirs,$driver40dir);
820 my $ro_shrdir="$shrdir/root-tmp";
821 push(@dirs,$ro_shrdir);
823 my $msdfs_shrdir="$shrdir/msdfsshare";
824 push(@dirs,$msdfs_shrdir);
826 my $msdfs_deeppath="$msdfs_shrdir/deeppath";
827 push(@dirs,$msdfs_deeppath);
829 # this gets autocreated by winbindd
830 my $wbsockdir="$prefix_abs/winbindd";
831 my $wbsockprivdir="$lockdir/winbindd_privileged";
833 my $nmbdsockdir="$prefix_abs/nmbd";
834 unlink($nmbdsockdir);
837 ## create the test directory layout
839 die ("prefix_abs = ''") if $prefix_abs eq "";
840 die ("prefix_abs = '/'") if $prefix_abs eq "/";
842 mkdir($prefix_abs, 0777);
843 print "CREATE TEST ENVIRONMENT IN '$prefix'...";
844 if (not defined($no_delete_prefix) or not $no_delete_prefix) {
845 system("rm -rf $prefix_abs/*");
847 mkdir($_, 0777) foreach(@dirs);
850 ## lockdir and piddir must be 0755
852 chmod 0755, $lockdir;
853 chmod 0755, $piddir;
857 ## create ro and msdfs share layout
860 chmod 0755, $ro_shrdir;
861 my $unreadable_file = "$ro_shrdir/unreadable_file";
862 unless (open(UNREADABLE_FILE, ">$unreadable_file")) {
863 warn("Unable to open $unreadable_file");
864 return undef;
866 close(UNREADABLE_FILE);
867 chmod 0600, $unreadable_file;
869 my $msdfs_target = "$ro_shrdir/msdfs-target";
870 unless (open(MSDFS_TARGET, ">$msdfs_target")) {
871 warn("Unable to open $msdfs_target");
872 return undef;
874 close(MSDFS_TARGET);
875 chmod 0666, $msdfs_target;
876 symlink "msdfs:$server_ip\\ro-tmp", "$msdfs_shrdir/msdfs-src1";
877 symlink "msdfs:$server_ip\\ro-tmp", "$msdfs_shrdir/deeppath/msdfs-src2";
879 my $conffile="$libdir/server.conf";
881 my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/lib/nss_wrapper/nss_wrapper.pl";
882 my $nss_wrapper_passwd = "$privatedir/passwd";
883 my $nss_wrapper_group = "$privatedir/group";
885 my $mod_printer_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/printing/modprinter.pl";
887 my @eventlog_list = ("dns server", "application");
890 ## calculate uids and gids
893 my ($max_uid, $max_gid);
894 my ($uid_nobody, $uid_root, $uid_pdbtest);
895 my ($gid_nobody, $gid_nogroup, $gid_root, $gid_domusers, $gid_domadmins);
897 if ($unix_uid < 0xffff - 2) {
898 $max_uid = 0xffff;
899 } else {
900 $max_uid = $unix_uid;
903 $uid_root = $max_uid - 1;
904 $uid_nobody = $max_uid - 2;
905 $uid_pdbtest = $max_uid - 3;
907 if ($unix_gids[0] < 0xffff - 3) {
908 $max_gid = 0xffff;
909 } else {
910 $max_gid = $unix_gids[0];
913 $gid_nobody = $max_gid - 1;
914 $gid_nogroup = $max_gid - 2;
915 $gid_root = $max_gid - 3;
916 $gid_domusers = $max_gid - 4;
917 $gid_domadmins = $max_gid - 5;
920 ## create conffile
923 unless (open(CONF, ">$conffile")) {
924 warn("Unable to open $conffile");
925 return undef;
927 print CONF "
928 [global]
929 netbios name = $server
930 interfaces = $server_ip/8
931 bind interfaces only = yes
932 panic action = $self->{srcdir}/selftest/gdb_backtrace %d %\$(MAKE_TEST_BINARY)
933 smbd:suicide mode = yes
935 workgroup = $domain
937 private dir = $privatedir
938 pid directory = $piddir
939 lock directory = $lockdir
940 log file = $logdir/log.\%m
941 log level = 1
942 debug pid = yes
943 max log size = 0
945 state directory = $lockdir
946 cache directory = $lockdir
948 passdb backend = tdbsam
950 time server = yes
952 add user script = $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
953 add group script = $nss_wrapper_pl --group_path $nss_wrapper_group --type group --action add --name %g
954 add machine script = $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
955 add user to group script = $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type member --action add --member %u --name %g --group_path $nss_wrapper_group
956 delete user script = $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action delete --name %u
957 delete group script = $nss_wrapper_pl --group_path $nss_wrapper_group --type group --action delete --name %g
958 delete user from group script = $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type member --action delete --member %u --name %g --group_path $nss_wrapper_group
960 addprinter command = $mod_printer_pl -a -s $conffile --
961 deleteprinter command = $mod_printer_pl -d -s $conffile --
963 eventlog list = application \"dns server\"
965 kernel oplocks = no
966 kernel change notify = no
968 syslog = no
969 printing = bsd
970 printcap name = /dev/null
972 winbindd:socket dir = $wbsockdir
973 nmbd:socket dir = $nmbdsockdir
974 idmap config * : range = 100000-200000
975 winbind enum users = yes
976 winbind enum groups = yes
978 # min receivefile size = 4000
980 read only = no
981 server signing = auto
983 smbd:sharedelay = 100000
984 smbd:writetimeupdatedelay = 500000
985 map hidden = no
986 map system = no
987 map readonly = no
988 store dos attributes = yes
989 create mask = 0777
990 directory mask = 0777
991 dos filemode = yes
992 vfs objects = $vfs_modulesdir_abs/acl_xattr.so $vfs_modulesdir_abs/fake_acls.so $vfs_modulesdir_abs/xattr_tdb.so $vfs_modulesdir_abs/streams_depot.so
994 printing = vlp
995 print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
996 lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
997 lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
998 lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
999 lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1000 queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1001 queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1002 lpq cache time = 0
1004 ncalrpc dir = $prefix_abs/ncalrpc
1005 resolv:host file = $dns_host_file
1007 # The samba3.blackbox.smbclient_s3 test uses this to test that
1008 # sending messages works, and that the %m sub works.
1009 message command = mv %s $shrdir/message.%m
1011 # Begin extra options
1012 $extra_options
1013 # End extra options
1015 #Include user defined custom parameters if set
1018 if (defined($ENV{INCLUDE_CUSTOM_CONF})) {
1019 print CONF "\t$ENV{INCLUDE_CUSTOM_CONF}\n";
1022 print CONF "
1023 [tmp]
1024 path = $shrdir
1025 comment = smb username is [%U]
1026 [tmpenc]
1027 path = $shrdir
1028 comment = encrypt smb username is [%U]
1029 smb encrypt = required
1030 [tmpguest]
1031 path = $shrdir
1032 guest ok = yes
1033 [guestonly]
1034 path = $shrdir
1035 guest only = yes
1036 guest ok = yes
1037 [forceuser]
1038 path = $shrdir
1039 force user = $unix_name
1040 guest ok = yes
1041 [forcegroup]
1042 path = $shrdir
1043 force group = nogroup
1044 guest ok = yes
1045 [ro-tmp]
1046 path = $ro_shrdir
1047 guest ok = yes
1048 [write-list-tmp]
1049 path = $shrdir
1050 read only = yes
1051 write list = $unix_name
1052 [valid-users-tmp]
1053 path = $shrdir
1054 valid users = $unix_name
1055 [msdfs-share]
1056 path = $msdfs_shrdir
1057 msdfs root = yes
1058 guest ok = yes
1059 [hideunread]
1060 copy = tmp
1061 hide unreadable = yes
1062 [tmpcase]
1063 copy = tmp
1064 case sensitive = yes
1065 [hideunwrite]
1066 copy = tmp
1067 hide unwriteable files = yes
1068 [durable]
1069 copy = tmp
1070 kernel share modes = no
1071 kernel oplocks = no
1072 posix locking = no
1073 [print1]
1074 copy = tmp
1075 printable = yes
1077 [print2]
1078 copy = print1
1079 [print3]
1080 copy = print1
1081 default devmode = no
1082 [lp]
1083 copy = print1
1084 [xcopy_share]
1085 path = $shrdir
1086 comment = smb username is [%U]
1087 create mask = 777
1088 force create mode = 777
1089 [posix_share]
1090 path = $shrdir
1091 comment = smb username is [%U]
1092 create mask = 0777
1093 force create mode = 0
1094 directory mask = 0777
1095 force directory mode = 0
1096 vfs objects = $vfs_modulesdir_abs/xattr_tdb.so
1098 [print\$]
1099 copy = tmp
1101 close(CONF);
1104 ## create a test account
1107 unless (open(PASSWD, ">$nss_wrapper_passwd")) {
1108 warn("Unable to open $nss_wrapper_passwd");
1109 return undef;
1111 print PASSWD "nobody:x:$uid_nobody:$gid_nobody:nobody gecos:$prefix_abs:/bin/false
1112 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
1113 pdbtest:x:$uid_pdbtest:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
1115 if ($unix_uid != 0) {
1116 print PASSWD "root:x:$uid_root:$gid_root:root gecos:$prefix_abs:/bin/false";
1118 close(PASSWD);
1120 unless (open(GROUP, ">$nss_wrapper_group")) {
1121 warn("Unable to open $nss_wrapper_group");
1122 return undef;
1124 print GROUP "nobody:x:$gid_nobody:
1125 nogroup:x:$gid_nogroup:nobody
1126 $unix_name-group:x:$unix_gids[0]:
1127 domusers:X:$gid_domusers:
1128 domadmins:X:$gid_domadmins:
1130 if ($unix_gids[0] != 0) {
1131 print GROUP "root:x:$gid_root:";
1134 close(GROUP);
1136 foreach my $evlog (@eventlog_list) {
1137 my $evlogtdb = "$eventlogdir/$evlog.tdb";
1138 open(EVENTLOG, ">$evlogtdb") or die("Unable to open $evlogtdb");
1139 close(EVENTLOG);
1142 $ENV{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1143 $ENV{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1145 my $cmd = Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $unix_name > /dev/null";
1146 unless (open(PWD, "|$cmd")) {
1147 warn("Unable to set password for test account\n$cmd");
1148 return undef;
1150 print PWD "$password\n$password\n";
1151 unless (close(PWD)) {
1152 warn("Unable to set password for test account\n$cmd");
1153 return undef;
1155 print "DONE\n";
1157 open(DNS_UPDATE_LIST, ">$prefix/dns_update_list") or die("Unable to open $$prefix/dns_update_list");
1158 print DNS_UPDATE_LIST "A $server. $server_ip";
1159 close(DNS_UPDATE_LIST);
1161 if (system("$ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate --all-interfaces --use-file=$dns_host_file -s $conffile --update-list=$prefix/dns_update_list --no-substiutions --no-credentials") != 0) {
1162 die "Unable to update hostname into $dns_host_file";
1165 $ret{SERVER_IP} = $server_ip;
1166 $ret{NMBD_TEST_LOG} = "$prefix/nmbd_test.log";
1167 $ret{NMBD_TEST_LOG_POS} = 0;
1168 $ret{WINBINDD_TEST_LOG} = "$prefix/winbindd_test.log";
1169 $ret{WINBINDD_TEST_LOG_POS} = 0;
1170 $ret{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
1171 $ret{SMBD_TEST_LOG_POS} = 0;
1172 $ret{SERVERCONFFILE} = $conffile;
1173 $ret{CONFIGURATION} ="-s $conffile";
1174 $ret{SERVER} = $server;
1175 $ret{USERNAME} = $unix_name;
1176 $ret{USERID} = $unix_uid;
1177 $ret{DOMAIN} = $domain;
1178 $ret{NETBIOSNAME} = $server;
1179 $ret{PASSWORD} = $password;
1180 $ret{PIDDIR} = $piddir;
1181 $ret{WINBINDD_SOCKET_DIR} = $wbsockdir;
1182 $ret{WINBINDD_PRIV_PIPE_DIR} = $wbsockprivdir;
1183 $ret{NMBD_SOCKET_DIR} = $nmbdsockdir;
1184 $ret{SOCKET_WRAPPER_DEFAULT_IFACE} = $swiface;
1185 $ret{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1186 $ret{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1187 $ret{NSS_WRAPPER_WINBIND_SO_PATH} = Samba::nss_wrapper_winbind_so_path($self);
1188 $ret{LOCAL_PATH} = "$shrdir";
1190 return \%ret;
1193 sub wait_for_start($$$$$)
1195 my ($self, $envvars, $nmbd, $winbindd, $smbd) = @_;
1196 my $ret;
1198 if ($nmbd eq "yes") {
1199 # give time for nbt server to register its names
1200 print "delaying for nbt name registration\n";
1201 sleep(10);
1202 # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init
1203 my $nmblookup = Samba::bindir_path($self, "nmblookup3");
1204 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} __SAMBA__");
1205 system("$nmblookup $envvars->{CONFIGURATION} __SAMBA__");
1206 system("$nmblookup $envvars->{CONFIGURATION} -U 127.255.255.255 __SAMBA__");
1207 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} $envvars->{SERVER}");
1208 system("$nmblookup $envvars->{CONFIGURATION} $envvars->{SERVER}");
1211 if ($winbindd eq "yes") {
1212 print "checking for winbindd\n";
1213 my $count = 0;
1214 do {
1215 $ret = system("WINBINDD_SOCKET_DIR=" . $envvars->{WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " -p");
1216 if ($ret != 0) {
1217 sleep(2);
1219 $count++;
1220 } while ($ret != 0 && $count < 10);
1221 if ($count == 10) {
1222 print "WINBINDD not reachable after 20 seconds\n";
1223 teardown_env($self, $envvars);
1224 return 0;
1228 if ($smbd eq "yes") {
1229 # make sure smbd is also up set
1230 print "wait for smbd\n";
1232 my $count = 0;
1233 do {
1234 $ret = system(Samba::bindir_path($self, "smbclient3") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER} -U% -p 139");
1235 if ($ret != 0) {
1236 sleep(2);
1238 $count++
1239 } while ($ret != 0 && $count < 10);
1240 if ($count == 10) {
1241 print "SMBD failed to start up in a reasonable time (20sec)\n";
1242 teardown_env($self, $envvars);
1243 return 0;
1247 # Ensure we have domain users mapped.
1248 $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=513 unixgroup=domusers type=domain");
1249 if ($ret != 0) {
1250 return 1;
1252 $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=512 unixgroup=domadmins type=domain");
1253 if ($ret != 0) {
1254 return 1;
1257 if ($winbindd eq "yes") {
1258 # note: creating builtin groups requires winbindd for the
1259 # unix id allocator
1260 $ret = system("WINBINDD_SOCKET_DIR=" . $envvars->{WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} sam createbuiltingroup Users");
1261 if ($ret != 0) {
1262 print "Failed to create BUILTIN\\Users group\n";
1263 return 0;
1265 my $count = 0;
1266 do {
1267 system(Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} cache flush");
1268 $ret = system("WINBINDD_SOCKET_DIR=" . $envvars->{WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545");
1269 if ($ret != 0) {
1270 sleep(2);
1272 $count++;
1273 } while ($ret != 0 && $count < 10);
1274 if ($count == 10) {
1275 print "WINBINDD not reachable after 20 seconds\n";
1276 teardown_env($self, $envvars);
1277 return 0;
1281 print $self->getlog_env($envvars);
1283 return 1;