s3:libsmb: Plumb cli_smb2_qpathinfo_basic() inside cli_qpathinfo_basic().
[Samba.git] / selftest / target / Samba3.pm
blob20587bf94dd69c4af2440a75f9fcd6537977546e
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 print "smbd does not have ADS support\n" unless $found_ads;
29 return $found_ads;
32 sub new($$) {
33 my ($classname, $bindir, $binary_mapping, $srcdir, $server_maxtime) = @_;
34 my $self = { vars => {},
35 bindir => $bindir,
36 binary_mapping => $binary_mapping,
37 srcdir => $srcdir,
38 server_maxtime => $server_maxtime
40 bless $self;
41 return $self;
44 sub teardown_env($$)
46 my ($self, $envvars) = @_;
47 my $count = 0;
49 # This should cause smbd to terminate gracefully
50 close($envvars->{STDIN_PIPE});
52 my $smbdpid = $envvars->{SMBD_TL_PID};
53 my $nmbdpid = $envvars->{NMBD_TL_PID};
54 my $winbinddpid = $envvars->{WINBINDD_TL_PID};
56 # This should give it time to write out the gcov data
57 until ($count > 20) {
58 my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
59 my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
60 my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
61 if ($smbdchild == -1
62 && $nmbdchild == -1
63 && $winbinddchild == -1) {
64 last;
66 sleep(1);
67 $count++;
70 if ($count <= 20 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
71 return;
74 $self->stop_sig_term($smbdpid);
75 $self->stop_sig_term($nmbdpid);
76 $self->stop_sig_term($winbinddpid);
78 $count = 0;
79 until ($count > 10) {
80 my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
81 my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
82 my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
83 if ($smbdchild == -1
84 && $nmbdchild == -1
85 && $winbinddchild == -1) {
86 last;
88 sleep(1);
89 $count++;
92 if ($count <= 10 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
93 return;
96 warn("timelimit process did not quit on SIGTERM, sending SIGKILL");
97 $self->stop_sig_kill($smbdpid);
98 $self->stop_sig_kill($nmbdpid);
99 $self->stop_sig_kill($winbinddpid);
101 return 0;
104 sub getlog_env_app($$$)
106 my ($self, $envvars, $name) = @_;
108 my $title = "$name LOG of: $envvars->{NETBIOSNAME}\n";
109 my $out = $title;
111 open(LOG, "<".$envvars->{$name."_TEST_LOG"});
113 seek(LOG, $envvars->{$name."_TEST_LOG_POS"}, SEEK_SET);
114 while (<LOG>) {
115 $out .= $_;
117 $envvars->{$name."_TEST_LOG_POS"} = tell(LOG);
118 close(LOG);
120 return "" if $out eq $title;
122 return $out;
125 sub getlog_env($$)
127 my ($self, $envvars) = @_;
128 my $ret = "";
130 $ret .= $self->getlog_env_app($envvars, "SMBD");
131 $ret .= $self->getlog_env_app($envvars, "NMBD");
132 $ret .= $self->getlog_env_app($envvars, "WINBINDD");
134 return $ret;
137 sub check_env($$)
139 my ($self, $envvars) = @_;
141 my $childpid = waitpid(-1, WNOHANG);
143 # TODO ...
144 return 1;
147 sub setup_env($$$)
149 my ($self, $envname, $path) = @_;
151 if (defined($self->{vars}->{$envname})) {
152 return $self->{vars}->{$envname};
155 if ($envname eq "s3dc") {
156 return $self->setup_s3dc("$path/s3dc");
157 } elsif ($envname eq "simpleserver") {
158 return $self->setup_simpleserver("$path/simpleserver");
159 } elsif ($envname eq "maptoguest") {
160 return $self->setup_maptoguest("$path/maptoguest");
161 } elsif ($envname eq "ktest") {
162 return $self->setup_ktest("$path/ktest");
163 } elsif ($envname eq "member") {
164 if (not defined($self->{vars}->{s3dc})) {
165 if (not defined($self->setup_s3dc("$path/s3dc"))) {
166 return undef;
169 return $self->setup_member("$path/member", $self->{vars}->{s3dc});
170 } else {
171 return "UNKNOWN";
175 sub setup_s3dc($$)
177 my ($self, $path) = @_;
179 print "PROVISIONING S3DC...";
181 my $s3dc_options = "
182 domain master = yes
183 domain logons = yes
184 lanman auth = yes
186 rpc_server:epmapper = external
187 rpc_server:spoolss = external
188 rpc_server:lsarpc = external
189 rpc_server:samr = external
190 rpc_server:netlogon = external
191 rpc_server:register_embedded_np = yes
193 rpc_daemon:epmd = fork
194 rpc_daemon:spoolssd = fork
195 rpc_daemon:lsasd = fork
198 my $vars = $self->provision($path,
199 "LOCALS3DC2",
200 "locals3dc2pass",
201 $s3dc_options);
203 $vars or return undef;
205 if (not $self->check_or_start($vars, "yes", "yes", "yes")) {
206 return undef;
209 $vars->{DC_SERVER} = $vars->{SERVER};
210 $vars->{DC_SERVER_IP} = $vars->{SERVER_IP};
211 $vars->{DC_NETBIOSNAME} = $vars->{NETBIOSNAME};
212 $vars->{DC_USERNAME} = $vars->{USERNAME};
213 $vars->{DC_PASSWORD} = $vars->{PASSWORD};
215 $self->{vars}->{s3dc} = $vars;
217 return $vars;
220 sub setup_member($$$)
222 my ($self, $prefix, $s3dcvars) = @_;
224 print "PROVISIONING MEMBER...";
226 my $member_options = "
227 security = domain
228 server signing = on
230 my $ret = $self->provision($prefix,
231 "LOCALMEMBER3",
232 "localmember3pass",
233 $member_options);
235 $ret or return undef;
237 my $net = Samba::bindir_path($self, "net");
238 my $cmd = "";
239 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
240 $cmd .= "$net join $ret->{CONFIGURATION} $s3dcvars->{DOMAIN} member";
241 $cmd .= " -U$s3dcvars->{USERNAME}\%$s3dcvars->{PASSWORD}";
243 if (system($cmd) != 0) {
244 warn("Join failed\n$cmd");
245 return undef;
248 if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
249 return undef;
252 $ret->{DC_SERVER} = $s3dcvars->{SERVER};
253 $ret->{DC_SERVER_IP} = $s3dcvars->{SERVER_IP};
254 $ret->{DC_NETBIOSNAME} = $s3dcvars->{NETBIOSNAME};
255 $ret->{DC_USERNAME} = $s3dcvars->{USERNAME};
256 $ret->{DC_PASSWORD} = $s3dcvars->{PASSWORD};
258 return $ret;
261 sub setup_admember($$$$)
263 my ($self, $prefix, $dcvars) = @_;
265 # If we didn't build with ADS, pretend this env was never available
266 if (not $self->have_ads()) {
267 return "UNKNOWN";
270 print "PROVISIONING S3 AD MEMBER...";
272 my $member_options = "
273 security = ads
274 server signing = on
275 workgroup = $dcvars->{DOMAIN}
276 realm = $dcvars->{REALM}
279 my $ret = $self->provision($prefix,
280 "LOCALADMEMBER",
281 "loCalMemberPass",
282 $member_options);
284 $ret or return undef;
286 close(USERMAP);
287 $ret->{DOMAIN} = $dcvars->{DOMAIN};
288 $ret->{REALM} = $dcvars->{REALM};
290 my $ctx;
291 my $prefix_abs = abs_path($prefix);
292 $ctx = {};
293 $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
294 $ctx->{domain} = $dcvars->{DOMAIN};
295 $ctx->{realm} = $dcvars->{REALM};
296 $ctx->{dnsname} = lc($dcvars->{REALM});
297 $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
298 Samba::mk_krb5_conf($ctx, "");
300 $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
302 my $net = Samba::bindir_path($self, "net");
303 my $cmd = "";
304 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
305 $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
306 $cmd .= "$net join $ret->{CONFIGURATION}";
307 $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
309 if (system($cmd) != 0) {
310 warn("Join failed\n$cmd");
311 return undef;
314 # We need world access to this share, as otherwise the domain
315 # administrator from the AD domain provided by Samba4 can't
316 # access the share for tests.
317 chmod 0777, "$prefix/share";
319 if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
320 return undef;
323 $ret->{DC_SERVER} = $dcvars->{SERVER};
324 $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
325 $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
326 $ret->{DC_USERNAME} = $dcvars->{USERNAME};
327 $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
329 # Special case, this is called from Samba4.pm but needs to use the Samba3 check_env and get_log_env
330 $ret->{target} = $self;
332 return $ret;
335 sub setup_admember_rfc2307($$$$)
337 my ($self, $prefix, $dcvars) = @_;
339 # If we didn't build with ADS, pretend this env was never available
340 if (not $self->have_ads()) {
341 return "UNKNOWN";
344 print "PROVISIONING S3 AD MEMBER WITH idmap_rfc2307 config...";
346 my $member_options = "
347 security = ads
348 server signing = on
349 workgroup = $dcvars->{DOMAIN}
350 realm = $dcvars->{REALM}
351 idmap config $dcvars->{DOMAIN} : backend = rfc2307
352 idmap config $dcvars->{DOMAIN} : range = 2000000-2999999
353 idmap config $dcvars->{DOMAIN} : ldap_server = ad
354 idmap config $dcvars->{DOMAIN} : bind_path_user = ou=idmap,dc=samba,dc=example,dc=com
355 idmap config $dcvars->{DOMAIN} : bind_path_group = ou=idmap,dc=samba,dc=example,dc=com
358 my $ret = $self->provision($prefix,
359 "RFC2307MEMBER",
360 "loCalMemberPass",
361 $member_options);
363 $ret or return undef;
365 close(USERMAP);
366 $ret->{DOMAIN} = $dcvars->{DOMAIN};
367 $ret->{REALM} = $dcvars->{REALM};
369 my $ctx;
370 my $prefix_abs = abs_path($prefix);
371 $ctx = {};
372 $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
373 $ctx->{domain} = $dcvars->{DOMAIN};
374 $ctx->{realm} = $dcvars->{REALM};
375 $ctx->{dnsname} = lc($dcvars->{REALM});
376 $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
377 Samba::mk_krb5_conf($ctx, "");
379 $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
381 my $net = Samba::bindir_path($self, "net");
382 my $cmd = "";
383 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
384 $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
385 $cmd .= "$net join $ret->{CONFIGURATION}";
386 $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
388 if (system($cmd) != 0) {
389 warn("Join failed\n$cmd");
390 return undef;
393 # We need world access to this share, as otherwise the domain
394 # administrator from the AD domain provided by Samba4 can't
395 # access the share for tests.
396 chmod 0777, "$prefix/share";
398 if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
399 return undef;
402 $ret->{DC_SERVER} = $dcvars->{SERVER};
403 $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
404 $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
405 $ret->{DC_USERNAME} = $dcvars->{USERNAME};
406 $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
408 # Special case, this is called from Samba4.pm but needs to use the Samba3 check_env and get_log_env
409 $ret->{target} = $self;
411 return $ret;
414 sub setup_simpleserver($$)
416 my ($self, $path) = @_;
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 = xattr_tdb streams_depot
426 [vfs_aio_fork]
427 path = $prefix_abs/share
428 vfs objects = aio_fork
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});
785 my $dns_host_file = "$ENV{SELFTEST_PREFIX}/dns_host_file";
787 my @dirs = ();
789 my $shrdir="$prefix_abs/share";
790 push(@dirs,$shrdir);
792 my $libdir="$prefix_abs/lib";
793 push(@dirs,$libdir);
795 my $piddir="$prefix_abs/pid";
796 push(@dirs,$piddir);
798 my $privatedir="$prefix_abs/private";
799 push(@dirs,$privatedir);
801 my $lockdir="$prefix_abs/lockdir";
802 push(@dirs,$lockdir);
804 my $eventlogdir="$prefix_abs/lockdir/eventlog";
805 push(@dirs,$eventlogdir);
807 my $logdir="$prefix_abs/logs";
808 push(@dirs,$logdir);
810 my $driver32dir="$shrdir/W32X86";
811 push(@dirs,$driver32dir);
813 my $driver64dir="$shrdir/x64";
814 push(@dirs,$driver64dir);
816 my $driver40dir="$shrdir/WIN40";
817 push(@dirs,$driver40dir);
819 my $ro_shrdir="$shrdir/root-tmp";
820 push(@dirs,$ro_shrdir);
822 my $msdfs_shrdir="$shrdir/msdfsshare";
823 push(@dirs,$msdfs_shrdir);
825 my $msdfs_deeppath="$msdfs_shrdir/deeppath";
826 push(@dirs,$msdfs_deeppath);
828 # this gets autocreated by winbindd
829 my $wbsockdir="$prefix_abs/winbindd";
830 my $wbsockprivdir="$lockdir/winbindd_privileged";
832 my $nmbdsockdir="$prefix_abs/nmbd";
833 unlink($nmbdsockdir);
836 ## create the test directory layout
838 die ("prefix_abs = ''") if $prefix_abs eq "";
839 die ("prefix_abs = '/'") if $prefix_abs eq "/";
841 mkdir($prefix_abs, 0777);
842 print "CREATE TEST ENVIRONMENT IN '$prefix'...";
843 if (not defined($no_delete_prefix) or not $no_delete_prefix) {
844 system("rm -rf $prefix_abs/*");
846 mkdir($_, 0777) foreach(@dirs);
849 ## lockdir and piddir must be 0755
851 chmod 0755, $lockdir;
852 chmod 0755, $piddir;
856 ## create ro and msdfs share layout
859 chmod 0755, $ro_shrdir;
860 my $unreadable_file = "$ro_shrdir/unreadable_file";
861 unless (open(UNREADABLE_FILE, ">$unreadable_file")) {
862 warn("Unable to open $unreadable_file");
863 return undef;
865 close(UNREADABLE_FILE);
866 chmod 0600, $unreadable_file;
868 my $msdfs_target = "$ro_shrdir/msdfs-target";
869 unless (open(MSDFS_TARGET, ">$msdfs_target")) {
870 warn("Unable to open $msdfs_target");
871 return undef;
873 close(MSDFS_TARGET);
874 chmod 0666, $msdfs_target;
875 symlink "msdfs:$server_ip\\ro-tmp", "$msdfs_shrdir/msdfs-src1";
876 symlink "msdfs:$server_ip\\ro-tmp", "$msdfs_shrdir/deeppath/msdfs-src2";
878 my $conffile="$libdir/server.conf";
880 my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/lib/nss_wrapper/nss_wrapper.pl";
881 my $nss_wrapper_passwd = "$privatedir/passwd";
882 my $nss_wrapper_group = "$privatedir/group";
884 my $mod_printer_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/printing/modprinter.pl";
886 my @eventlog_list = ("dns server", "application");
889 ## calculate uids and gids
892 my ($max_uid, $max_gid);
893 my ($uid_nobody, $uid_root, $uid_pdbtest);
894 my ($gid_nobody, $gid_nogroup, $gid_root, $gid_domusers, $gid_domadmins);
896 if ($unix_uid < 0xffff - 2) {
897 $max_uid = 0xffff;
898 } else {
899 $max_uid = $unix_uid;
902 $uid_root = $max_uid - 1;
903 $uid_nobody = $max_uid - 2;
904 $uid_pdbtest = $max_uid - 3;
906 if ($unix_gids[0] < 0xffff - 3) {
907 $max_gid = 0xffff;
908 } else {
909 $max_gid = $unix_gids[0];
912 $gid_nobody = $max_gid - 1;
913 $gid_nogroup = $max_gid - 2;
914 $gid_root = $max_gid - 3;
915 $gid_domusers = $max_gid - 4;
916 $gid_domadmins = $max_gid - 5;
919 ## create conffile
922 unless (open(CONF, ">$conffile")) {
923 warn("Unable to open $conffile");
924 return undef;
926 print CONF "
927 [global]
928 netbios name = $server
929 interfaces = $server_ip/8
930 bind interfaces only = yes
931 panic action = $self->{srcdir}/selftest/gdb_backtrace %d %\$(MAKE_TEST_BINARY)
932 smbd:suicide mode = yes
934 workgroup = $domain
936 private dir = $privatedir
937 pid directory = $piddir
938 lock directory = $lockdir
939 log file = $logdir/log.\%m
940 log level = 1
941 debug pid = yes
942 max log size = 0
944 state directory = $lockdir
945 cache directory = $lockdir
947 passdb backend = tdbsam
949 time server = yes
951 add user script = $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
952 add group script = $nss_wrapper_pl --group_path $nss_wrapper_group --type group --action add --name %g
953 add machine script = $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
954 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
955 delete user script = $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action delete --name %u
956 delete group script = $nss_wrapper_pl --group_path $nss_wrapper_group --type group --action delete --name %g
957 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
959 addprinter command = $mod_printer_pl -a -s $conffile --
960 deleteprinter command = $mod_printer_pl -d -s $conffile --
962 eventlog list = application \"dns server\"
964 kernel oplocks = no
965 kernel change notify = no
967 syslog = no
968 printing = bsd
969 printcap name = /dev/null
971 winbindd:socket dir = $wbsockdir
972 nmbd:socket dir = $nmbdsockdir
973 idmap config * : range = 100000-200000
974 winbind enum users = yes
975 winbind enum groups = yes
977 # min receivefile size = 4000
979 read only = no
980 server signing = auto
982 smbd:sharedelay = 100000
983 smbd:writetimeupdatedelay = 500000
984 map hidden = no
985 map system = no
986 map readonly = no
987 store dos attributes = yes
988 create mask = 755
989 dos filemode = yes
990 vfs objects = acl_xattr fake_acls xattr_tdb streams_depot
992 printing = vlp
993 print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
994 lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
995 lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
996 lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
997 lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
998 queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
999 queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1000 lpq cache time = 0
1002 ncalrpc dir = $prefix_abs/ncalrpc
1003 resolv:host file = $dns_host_file
1005 # The samba3.blackbox.smbclient_s3 test uses this to test that
1006 # sending messages works, and that the %m sub works.
1007 message command = mv %s $shrdir/message.%m
1009 # Begin extra options
1010 $extra_options
1011 # End extra options
1013 #Include user defined custom parameters if set
1016 if (defined($ENV{INCLUDE_CUSTOM_CONF})) {
1017 print CONF "\t$ENV{INCLUDE_CUSTOM_CONF}\n";
1020 print CONF "
1021 [tmp]
1022 path = $shrdir
1023 comment = smb username is [%U]
1024 [tmpenc]
1025 path = $shrdir
1026 comment = encrypt smb username is [%U]
1027 smb encrypt = required
1028 vfs objects = dirsort
1029 [tmpguest]
1030 path = $shrdir
1031 guest ok = yes
1032 [guestonly]
1033 path = $shrdir
1034 guest only = yes
1035 guest ok = yes
1036 [forceuser]
1037 path = $shrdir
1038 force user = $unix_name
1039 guest ok = yes
1040 [forcegroup]
1041 path = $shrdir
1042 force group = nogroup
1043 guest ok = yes
1044 [ro-tmp]
1045 path = $ro_shrdir
1046 guest ok = yes
1047 [write-list-tmp]
1048 path = $shrdir
1049 read only = yes
1050 write list = $unix_name
1051 [valid-users-tmp]
1052 path = $shrdir
1053 valid users = $unix_name
1054 [msdfs-share]
1055 path = $msdfs_shrdir
1056 msdfs root = yes
1057 guest ok = yes
1058 [hideunread]
1059 copy = tmp
1060 hide unreadable = yes
1061 [tmpcase]
1062 copy = tmp
1063 case sensitive = yes
1064 [hideunwrite]
1065 copy = tmp
1066 hide unwriteable files = yes
1067 [durable]
1068 copy = tmp
1069 kernel share modes = no
1070 kernel oplocks = no
1071 posix locking = no
1072 [print1]
1073 copy = tmp
1074 printable = yes
1076 [print2]
1077 copy = print1
1078 [print3]
1079 copy = print1
1080 default devmode = no
1081 [lp]
1082 copy = print1
1084 [nfs4acl_simple]
1085 path = $shrdir
1086 comment = smb username is [%U]
1087 nfs4:mode = simple
1088 vfs objects = nfs4acl_xattr xattr_tdb
1090 [nfs4acl_special]
1091 path = $shrdir
1092 comment = smb username is [%U]
1093 nfs4:mode = special
1094 vfs objects = nfs4acl_xattr xattr_tdb
1096 [xcopy_share]
1097 path = $shrdir
1098 comment = smb username is [%U]
1099 create mask = 777
1100 force create mode = 777
1101 [posix_share]
1102 path = $shrdir
1103 comment = smb username is [%U]
1104 create mask = 0777
1105 force create mode = 0
1106 directory mask = 0777
1107 force directory mode = 0
1108 vfs objects = xattr_tdb
1110 [print\$]
1111 copy = tmp
1113 close(CONF);
1116 ## create a test account
1119 unless (open(PASSWD, ">$nss_wrapper_passwd")) {
1120 warn("Unable to open $nss_wrapper_passwd");
1121 return undef;
1123 print PASSWD "nobody:x:$uid_nobody:$gid_nobody:nobody gecos:$prefix_abs:/bin/false
1124 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
1125 pdbtest:x:$uid_pdbtest:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
1127 if ($unix_uid != 0) {
1128 print PASSWD "root:x:$uid_root:$gid_root:root gecos:$prefix_abs:/bin/false
1131 close(PASSWD);
1133 unless (open(GROUP, ">$nss_wrapper_group")) {
1134 warn("Unable to open $nss_wrapper_group");
1135 return undef;
1137 print GROUP "nobody:x:$gid_nobody:
1138 nogroup:x:$gid_nogroup:nobody
1139 $unix_name-group:x:$unix_gids[0]:
1140 domusers:X:$gid_domusers:
1141 domadmins:X:$gid_domadmins:
1143 if ($unix_gids[0] != 0) {
1144 print GROUP "root:x:$gid_root:
1148 close(GROUP);
1150 foreach my $evlog (@eventlog_list) {
1151 my $evlogtdb = "$eventlogdir/$evlog.tdb";
1152 open(EVENTLOG, ">$evlogtdb") or die("Unable to open $evlogtdb");
1153 close(EVENTLOG);
1156 $ENV{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1157 $ENV{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1159 my $cmd = Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $unix_name > /dev/null";
1160 unless (open(PWD, "|$cmd")) {
1161 warn("Unable to set password for test account\n$cmd");
1162 return undef;
1164 print PWD "$password\n$password\n";
1165 unless (close(PWD)) {
1166 warn("Unable to set password for test account\n$cmd");
1167 return undef;
1169 print "DONE\n";
1171 open(DNS_UPDATE_LIST, ">$prefix/dns_update_list") or die("Unable to open $$prefix/dns_update_list");
1172 print DNS_UPDATE_LIST "A $server. $server_ip";
1173 close(DNS_UPDATE_LIST);
1175 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) {
1176 die "Unable to update hostname into $dns_host_file";
1179 $ret{SERVER_IP} = $server_ip;
1180 $ret{NMBD_TEST_LOG} = "$prefix/nmbd_test.log";
1181 $ret{NMBD_TEST_LOG_POS} = 0;
1182 $ret{WINBINDD_TEST_LOG} = "$prefix/winbindd_test.log";
1183 $ret{WINBINDD_TEST_LOG_POS} = 0;
1184 $ret{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
1185 $ret{SMBD_TEST_LOG_POS} = 0;
1186 $ret{SERVERCONFFILE} = $conffile;
1187 $ret{CONFIGURATION} ="-s $conffile";
1188 $ret{SERVER} = $server;
1189 $ret{USERNAME} = $unix_name;
1190 $ret{USERID} = $unix_uid;
1191 $ret{DOMAIN} = $domain;
1192 $ret{NETBIOSNAME} = $server;
1193 $ret{PASSWORD} = $password;
1194 $ret{PIDDIR} = $piddir;
1195 $ret{WINBINDD_SOCKET_DIR} = $wbsockdir;
1196 $ret{WINBINDD_PRIV_PIPE_DIR} = $wbsockprivdir;
1197 $ret{NMBD_SOCKET_DIR} = $nmbdsockdir;
1198 $ret{SOCKET_WRAPPER_DEFAULT_IFACE} = $swiface;
1199 $ret{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1200 $ret{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1201 $ret{NSS_WRAPPER_WINBIND_SO_PATH} = Samba::nss_wrapper_winbind_so_path($self);
1202 $ret{LOCAL_PATH} = "$shrdir";
1204 return \%ret;
1207 sub wait_for_start($$$$$)
1209 my ($self, $envvars, $nmbd, $winbindd, $smbd) = @_;
1210 my $ret;
1212 if ($nmbd eq "yes") {
1213 # give time for nbt server to register its names
1214 print "delaying for nbt name registration\n";
1215 sleep(10);
1216 # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init
1217 my $nmblookup = Samba::bindir_path($self, "nmblookup3");
1218 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} __SAMBA__");
1219 system("$nmblookup $envvars->{CONFIGURATION} __SAMBA__");
1220 system("$nmblookup $envvars->{CONFIGURATION} -U 127.255.255.255 __SAMBA__");
1221 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} $envvars->{SERVER}");
1222 system("$nmblookup $envvars->{CONFIGURATION} $envvars->{SERVER}");
1225 if ($winbindd eq "yes") {
1226 print "checking for winbindd\n";
1227 my $count = 0;
1228 do {
1229 $ret = system("WINBINDD_SOCKET_DIR=" . $envvars->{WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " -p");
1230 if ($ret != 0) {
1231 sleep(2);
1233 $count++;
1234 } while ($ret != 0 && $count < 10);
1235 if ($count == 10) {
1236 print "WINBINDD not reachable after 20 seconds\n";
1237 teardown_env($self, $envvars);
1238 return 0;
1242 if ($smbd eq "yes") {
1243 # make sure smbd is also up set
1244 print "wait for smbd\n";
1246 my $count = 0;
1247 do {
1248 $ret = system(Samba::bindir_path($self, "smbclient3") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER} -U% -p 139");
1249 if ($ret != 0) {
1250 sleep(2);
1252 $count++
1253 } while ($ret != 0 && $count < 10);
1254 if ($count == 10) {
1255 print "SMBD failed to start up in a reasonable time (20sec)\n";
1256 teardown_env($self, $envvars);
1257 return 0;
1261 # Ensure we have domain users mapped.
1262 $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=513 unixgroup=domusers type=domain");
1263 if ($ret != 0) {
1264 return 1;
1266 $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=512 unixgroup=domadmins type=domain");
1267 if ($ret != 0) {
1268 return 1;
1271 if ($winbindd eq "yes") {
1272 # note: creating builtin groups requires winbindd for the
1273 # unix id allocator
1274 $ret = system("WINBINDD_SOCKET_DIR=" . $envvars->{WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} sam createbuiltingroup Users");
1275 if ($ret != 0) {
1276 print "Failed to create BUILTIN\\Users group\n";
1277 return 0;
1279 my $count = 0;
1280 do {
1281 system(Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} cache flush");
1282 $ret = system("WINBINDD_SOCKET_DIR=" . $envvars->{WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545");
1283 if ($ret != 0) {
1284 sleep(2);
1286 $count++;
1287 } while ($ret != 0 && $count < 10);
1288 if ($count == 10) {
1289 print "WINBINDD not reachable after 20 seconds\n";
1290 teardown_env($self, $envvars);
1291 return 0;
1295 print $self->getlog_env($envvars);
1297 return 1;