tdb: Add another overflow check to tdb_expand_adjust
[Samba.git] / selftest / target / Samba3.pm
blob91a8133e71f57ed920d70ccd7dd0cae89ff9e2c8
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) = @_;
417 print "PROVISIONING server with security=share...";
419 my $prefix_abs = abs_path($path);
421 my $simpleserver_options = "
422 lanman auth = yes
423 vfs objects = xattr_tdb streams_depot
425 [vfs_aio_fork]
426 path = $prefix_abs/share
427 vfs objects = aio_fork
428 read only = no
429 vfs_aio_fork:erratic_testing_mode=yes
432 my $vars = $self->provision($path,
433 "LOCALSHARE4",
434 "local4pass",
435 $simpleserver_options);
437 $vars or return undef;
439 if (not $self->check_or_start($vars, "yes", "no", "yes")) {
440 return undef;
443 $self->{vars}->{simpleserver} = $vars;
445 return $vars;
448 sub setup_ktest($$$)
450 my ($self, $prefix) = @_;
452 # If we didn't build with ADS, pretend this env was never available
453 if (not $self->have_ads()) {
454 return "UNKNOWN";
457 print "PROVISIONING server with security=ads...";
459 my $ktest_options = "
460 workgroup = KTEST
461 realm = ktest.samba.example.com
462 security = ads
463 username map = $prefix/lib/username.map
464 server signing = required
467 my $ret = $self->provision($prefix,
468 "LOCALKTEST6",
469 "localktest6pass",
470 $ktest_options);
472 $ret or return undef;
474 my $ctx;
475 my $prefix_abs = abs_path($prefix);
476 $ctx = {};
477 $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
478 $ctx->{domain} = "KTEST";
479 $ctx->{realm} = "KTEST.SAMBA.EXAMPLE.COM";
480 $ctx->{dnsname} = lc($ctx->{realm});
481 $ctx->{kdc_ipv4} = "0.0.0.0";
482 Samba::mk_krb5_conf($ctx, "");
484 $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
486 open(USERMAP, ">$prefix/lib/username.map") or die("Unable to open $prefix/lib/username.map");
487 print USERMAP "
488 $ret->{USERNAME} = KTEST\\Administrator
490 close(USERMAP);
492 #This is the secrets.tdb created by 'net ads join' from Samba3 to a
493 #Samba4 DC with the same parameters as are being used here. The
494 #domain SID is S-1-5-21-1071277805-689288055-3486227160
496 system("cp $self->{srcdir}/source3/selftest/ktest-secrets.tdb $prefix/private/secrets.tdb");
497 chmod 0600, "$prefix/private/secrets.tdb";
499 #Make sure there's no old ntdb file.
500 system("rm -f $prefix/private/secrets.ntdb");
502 #This uses a pre-calculated krb5 credentials cache, obtained by running Samba4 with:
503 # "--option=kdc:service ticket lifetime=239232" "--option=kdc:user ticket lifetime=239232" "--option=kdc:renewal lifetime=239232"
505 #and having in krb5.conf:
506 # ticket_lifetime = 799718400
507 # renew_lifetime = 799718400
509 # The commands for the -2 keytab where were:
510 # kinit administrator@KTEST.SAMBA.EXAMPLE.COM
511 # kvno host/localktest6@KTEST.SAMBA.EXAMPLE.COM
512 # kvno cifs/localktest6@KTEST.SAMBA.EXAMPLE.COM
513 # kvno host/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
514 # kvno cifs/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
516 # and then for the -3 keytab, I did
518 # net changetrustpw; kdestroy and the same again.
520 # This creates a credential cache with a very long lifetime (2036 at
521 # at 2011-04), and shows that running 'net changetrustpw' does not
522 # break existing logins (for the secrets.tdb method at least).
525 $ret->{KRB5_CCACHE}="FILE:$prefix/krb5_ccache";
527 system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-2 $prefix/krb5_ccache-2");
528 chmod 0600, "$prefix/krb5_ccache-2";
530 system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-3 $prefix/krb5_ccache-3");
531 chmod 0600, "$prefix/krb5_ccache-3";
533 # We need world access to this share, as otherwise the domain
534 # administrator from the AD domain provided by ktest can't
535 # access the share for tests.
536 chmod 0777, "$prefix/share";
538 if (not $self->check_or_start($ret, "yes", "no", "yes")) {
539 return undef;
541 return $ret;
544 sub setup_maptoguest($$)
546 my ($self, $path) = @_;
548 print "PROVISIONING maptoguest...";
550 my $options = "
551 map to guest = bad user
554 my $vars = $self->provision($path,
555 "maptoguest",
556 "maptoguestpass",
557 $options);
559 $vars or return undef;
561 if (not $self->check_or_start($vars, "yes", "no", "yes")) {
562 return undef;
565 $self->{vars}->{s3maptoguest} = $vars;
567 return $vars;
570 sub stop_sig_term($$) {
571 my ($self, $pid) = @_;
572 kill("USR1", $pid) or kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
575 sub stop_sig_kill($$) {
576 my ($self, $pid) = @_;
577 kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
580 sub write_pid($$$)
582 my ($env_vars, $app, $pid) = @_;
584 open(PID, ">$env_vars->{PIDDIR}/timelimit.$app.pid");
585 print PID $pid;
586 close(PID);
589 sub read_pid($$)
591 my ($env_vars, $app) = @_;
593 open(PID, "<$env_vars->{PIDDIR}/timelimit.$app.pid");
594 my $pid = <PID>;
595 close(PID);
596 return $pid;
599 sub check_or_start($$$$$) {
600 my ($self, $env_vars, $nmbd, $winbindd, $smbd) = @_;
602 # use a pipe for stdin in the child processes. This allows
603 # those processes to monitor the pipe for EOF to ensure they
604 # exit when the test script exits
605 pipe(STDIN_READER, $env_vars->{STDIN_PIPE});
607 unlink($env_vars->{NMBD_TEST_LOG});
608 print "STARTING NMBD...";
609 my $pid = fork();
610 if ($pid == 0) {
611 open STDOUT, ">$env_vars->{NMBD_TEST_LOG}";
612 open STDERR, '>&STDOUT';
614 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
616 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
617 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
618 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
620 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
621 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
622 $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
624 $ENV{UID_WRAPPER} = "1";
626 if ($nmbd ne "yes") {
627 $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
628 my $signame = shift;
629 print("Skip nmbd received signal $signame");
630 exit 0;
632 sleep($self->{server_maxtime});
633 exit 0;
636 my @optargs = ("-d0");
637 if (defined($ENV{NMBD_OPTIONS})) {
638 @optargs = split(/ /, $ENV{NMBD_OPTIONS});
641 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "nmbd");
643 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
644 if(defined($ENV{NMBD_VALGRIND})) {
645 @preargs = split(/ /, $ENV{NMBD_VALGRIND});
648 close($env_vars->{STDIN_PIPE});
649 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
651 exec(@preargs, Samba::bindir_path($self, "nmbd"), "-F", "--no-process-group", "--log-stdout", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start nmbd: $!");
653 $env_vars->{NMBD_TL_PID} = $pid;
654 write_pid($env_vars, "nmbd", $pid);
655 print "DONE\n";
657 unlink($env_vars->{WINBINDD_TEST_LOG});
658 print "STARTING WINBINDD...";
659 $pid = fork();
660 if ($pid == 0) {
661 open STDOUT, ">$env_vars->{WINBINDD_TEST_LOG}";
662 open STDERR, '>&STDOUT';
664 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
666 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
667 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
668 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
670 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
671 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
672 $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
674 $ENV{UID_WRAPPER} = "1";
676 if ($winbindd ne "yes") {
677 $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
678 my $signame = shift;
679 print("Skip winbindd received signal $signame");
680 exit 0;
682 sleep($self->{server_maxtime});
683 exit 0;
686 my @optargs = ("-d0");
687 if (defined($ENV{WINBINDD_OPTIONS})) {
688 @optargs = split(/ /, $ENV{WINBINDD_OPTIONS});
691 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "winbindd");
693 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
694 if(defined($ENV{WINBINDD_VALGRIND})) {
695 @preargs = split(/ /, $ENV{WINBINDD_VALGRIND});
698 print "Starting winbindd with config $env_vars->{SERVERCONFFILE}\n";
700 close($env_vars->{STDIN_PIPE});
701 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
703 exec(@preargs, Samba::bindir_path($self, "winbindd"), "-F", "--no-process-group", "--stdout", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start winbindd: $!");
705 $env_vars->{WINBINDD_TL_PID} = $pid;
706 write_pid($env_vars, "winbindd", $pid);
707 print "DONE\n";
709 unlink($env_vars->{SMBD_TEST_LOG});
710 print "STARTING SMBD...";
711 $pid = fork();
712 if ($pid == 0) {
713 open STDOUT, ">$env_vars->{SMBD_TEST_LOG}";
714 open STDERR, '>&STDOUT';
716 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
718 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
719 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
720 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
722 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
723 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
724 $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
726 $ENV{UID_WRAPPER} = "1";
728 if ($smbd ne "yes") {
729 $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
730 my $signame = shift;
731 print("Skip smbd received signal $signame");
732 exit 0;
734 sleep($self->{server_maxtime});
735 exit 0;
738 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "smbd");
739 my @optargs = ("-d0");
740 if (defined($ENV{SMBD_OPTIONS})) {
741 @optargs = split(/ /, $ENV{SMBD_OPTIONS});
743 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
744 if(defined($ENV{SMBD_VALGRIND})) {
745 @preargs = split(/ /,$ENV{SMBD_VALGRIND});
748 close($env_vars->{STDIN_PIPE});
749 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
751 exec(@preargs, Samba::bindir_path($self, "smbd"), "-F", "--no-process-group", "--log-stdout", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start smbd: $!");
753 $env_vars->{SMBD_TL_PID} = $pid;
754 write_pid($env_vars, "smbd", $pid);
755 print "DONE\n";
757 close(STDIN_READER);
759 return $self->wait_for_start($env_vars, $nmbd, $winbindd, $smbd);
762 sub provision($$$$$$)
764 my ($self, $prefix, $server, $password, $extra_options, $no_delete_prefix) = @_;
767 ## setup the various environment variables we need
770 my $swiface = Samba::get_interface($server);
771 my %ret = ();
772 my $server_ip = "127.0.0.$swiface";
773 my $domain = "SAMBA-TEST";
775 my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `PATH=/usr/ucb:$ENV{PATH} whoami`);
776 chomp $unix_name;
777 my $unix_uid = $>;
778 my $unix_gids_str = $);
779 my @unix_gids = split(" ", $unix_gids_str);
781 my $prefix_abs = abs_path($prefix);
782 my $bindir_abs = abs_path($self->{bindir});
784 my $dns_host_file = "$ENV{SELFTEST_PREFIX}/dns_host_file";
786 my @dirs = ();
788 my $shrdir="$prefix_abs/share";
789 push(@dirs,$shrdir);
791 my $libdir="$prefix_abs/lib";
792 push(@dirs,$libdir);
794 my $piddir="$prefix_abs/pid";
795 push(@dirs,$piddir);
797 my $privatedir="$prefix_abs/private";
798 push(@dirs,$privatedir);
800 my $lockdir="$prefix_abs/lockdir";
801 push(@dirs,$lockdir);
803 my $eventlogdir="$prefix_abs/lockdir/eventlog";
804 push(@dirs,$eventlogdir);
806 my $logdir="$prefix_abs/logs";
807 push(@dirs,$logdir);
809 my $driver32dir="$shrdir/W32X86";
810 push(@dirs,$driver32dir);
812 my $driver64dir="$shrdir/x64";
813 push(@dirs,$driver64dir);
815 my $driver40dir="$shrdir/WIN40";
816 push(@dirs,$driver40dir);
818 my $ro_shrdir="$shrdir/root-tmp";
819 push(@dirs,$ro_shrdir);
821 my $msdfs_shrdir="$shrdir/msdfsshare";
822 push(@dirs,$msdfs_shrdir);
824 my $msdfs_deeppath="$msdfs_shrdir/deeppath";
825 push(@dirs,$msdfs_deeppath);
827 # this gets autocreated by winbindd
828 my $wbsockdir="$prefix_abs/winbindd";
829 my $wbsockprivdir="$lockdir/winbindd_privileged";
831 my $nmbdsockdir="$prefix_abs/nmbd";
832 unlink($nmbdsockdir);
835 ## create the test directory layout
837 die ("prefix_abs = ''") if $prefix_abs eq "";
838 die ("prefix_abs = '/'") if $prefix_abs eq "/";
840 mkdir($prefix_abs, 0777);
841 print "CREATE TEST ENVIRONMENT IN '$prefix'...";
842 if (not defined($no_delete_prefix) or not $no_delete_prefix) {
843 system("rm -rf $prefix_abs/*");
845 mkdir($_, 0777) foreach(@dirs);
848 ## lockdir and piddir must be 0755
850 chmod 0755, $lockdir;
851 chmod 0755, $piddir;
855 ## create ro and msdfs share layout
858 chmod 0755, $ro_shrdir;
859 my $unreadable_file = "$ro_shrdir/unreadable_file";
860 unless (open(UNREADABLE_FILE, ">$unreadable_file")) {
861 warn("Unable to open $unreadable_file");
862 return undef;
864 close(UNREADABLE_FILE);
865 chmod 0600, $unreadable_file;
867 my $msdfs_target = "$ro_shrdir/msdfs-target";
868 unless (open(MSDFS_TARGET, ">$msdfs_target")) {
869 warn("Unable to open $msdfs_target");
870 return undef;
872 close(MSDFS_TARGET);
873 chmod 0666, $msdfs_target;
874 symlink "msdfs:$server_ip\\ro-tmp", "$msdfs_shrdir/msdfs-src1";
875 symlink "msdfs:$server_ip\\ro-tmp", "$msdfs_shrdir/deeppath/msdfs-src2";
877 my $conffile="$libdir/server.conf";
879 my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/lib/nss_wrapper/nss_wrapper.pl";
880 my $nss_wrapper_passwd = "$privatedir/passwd";
881 my $nss_wrapper_group = "$privatedir/group";
883 my $mod_printer_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/printing/modprinter.pl";
885 my @eventlog_list = ("dns server", "application");
888 ## calculate uids and gids
891 my ($max_uid, $max_gid);
892 my ($uid_nobody, $uid_root, $uid_pdbtest);
893 my ($gid_nobody, $gid_nogroup, $gid_root, $gid_domusers, $gid_domadmins);
895 if ($unix_uid < 0xffff - 2) {
896 $max_uid = 0xffff;
897 } else {
898 $max_uid = $unix_uid;
901 $uid_root = $max_uid - 1;
902 $uid_nobody = $max_uid - 2;
903 $uid_pdbtest = $max_uid - 3;
905 if ($unix_gids[0] < 0xffff - 3) {
906 $max_gid = 0xffff;
907 } else {
908 $max_gid = $unix_gids[0];
911 $gid_nobody = $max_gid - 1;
912 $gid_nogroup = $max_gid - 2;
913 $gid_root = $max_gid - 3;
914 $gid_domusers = $max_gid - 4;
915 $gid_domadmins = $max_gid - 5;
918 ## create conffile
921 unless (open(CONF, ">$conffile")) {
922 warn("Unable to open $conffile");
923 return undef;
925 print CONF "
926 [global]
927 netbios name = $server
928 interfaces = $server_ip/8
929 bind interfaces only = yes
930 panic action = $self->{srcdir}/selftest/gdb_backtrace %d %\$(MAKE_TEST_BINARY)
931 smbd:suicide mode = yes
933 workgroup = $domain
935 private dir = $privatedir
936 pid directory = $piddir
937 lock directory = $lockdir
938 log file = $logdir/log.\%m
939 log level = 1
940 debug pid = yes
941 max log size = 0
943 state directory = $lockdir
944 cache directory = $lockdir
946 passdb backend = tdbsam
948 time server = yes
950 add user script = $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
951 add group script = $nss_wrapper_pl --group_path $nss_wrapper_group --type group --action add --name %g
952 add machine script = $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
953 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
954 delete user script = $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action delete --name %u
955 delete group script = $nss_wrapper_pl --group_path $nss_wrapper_group --type group --action delete --name %g
956 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
958 addprinter command = $mod_printer_pl -a -s $conffile --
959 deleteprinter command = $mod_printer_pl -d -s $conffile --
961 eventlog list = application \"dns server\"
963 kernel oplocks = no
964 kernel change notify = no
966 syslog = no
967 printing = bsd
968 printcap name = /dev/null
970 winbindd:socket dir = $wbsockdir
971 nmbd:socket dir = $nmbdsockdir
972 idmap config * : range = 100000-200000
973 winbind enum users = yes
974 winbind enum groups = yes
976 # min receivefile size = 4000
978 read only = no
979 server signing = auto
981 smbd:sharedelay = 100000
982 smbd:writetimeupdatedelay = 500000
983 map hidden = no
984 map system = no
985 map readonly = no
986 store dos attributes = yes
987 create mask = 755
988 dos filemode = yes
989 vfs objects = acl_xattr fake_acls xattr_tdb streams_depot
991 printing = vlp
992 print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
993 lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
994 lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
995 lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
996 lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
997 queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
998 queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
999 lpq cache time = 0
1001 ncalrpc dir = $prefix_abs/ncalrpc
1002 resolv:host file = $dns_host_file
1004 # The samba3.blackbox.smbclient_s3 test uses this to test that
1005 # sending messages works, and that the %m sub works.
1006 message command = mv %s $shrdir/message.%m
1008 # Begin extra options
1009 $extra_options
1010 # End extra options
1012 #Include user defined custom parameters if set
1015 if (defined($ENV{INCLUDE_CUSTOM_CONF})) {
1016 print CONF "\t$ENV{INCLUDE_CUSTOM_CONF}\n";
1019 print CONF "
1020 [tmp]
1021 path = $shrdir
1022 comment = smb username is [%U]
1023 [tmpenc]
1024 path = $shrdir
1025 comment = encrypt smb username is [%U]
1026 smb encrypt = required
1027 vfs objects = dirsort
1028 [tmpguest]
1029 path = $shrdir
1030 guest ok = yes
1031 [guestonly]
1032 path = $shrdir
1033 guest only = yes
1034 guest ok = yes
1035 [forceuser]
1036 path = $shrdir
1037 force user = $unix_name
1038 guest ok = yes
1039 [forcegroup]
1040 path = $shrdir
1041 force group = nogroup
1042 guest ok = yes
1043 [ro-tmp]
1044 path = $ro_shrdir
1045 guest ok = yes
1046 [write-list-tmp]
1047 path = $shrdir
1048 read only = yes
1049 write list = $unix_name
1050 [valid-users-tmp]
1051 path = $shrdir
1052 valid users = $unix_name
1053 [msdfs-share]
1054 path = $msdfs_shrdir
1055 msdfs root = yes
1056 guest ok = yes
1057 [hideunread]
1058 copy = tmp
1059 hide unreadable = yes
1060 [tmpcase]
1061 copy = tmp
1062 case sensitive = yes
1063 [hideunwrite]
1064 copy = tmp
1065 hide unwriteable files = yes
1066 [durable]
1067 copy = tmp
1068 kernel share modes = no
1069 kernel oplocks = no
1070 posix locking = no
1071 [print1]
1072 copy = tmp
1073 printable = yes
1075 [print2]
1076 copy = print1
1077 [print3]
1078 copy = print1
1079 default devmode = no
1080 [lp]
1081 copy = print1
1083 [nfs4acl_simple]
1084 path = $shrdir
1085 comment = smb username is [%U]
1086 nfs4:mode = simple
1087 vfs objects = nfs4acl_xattr xattr_tdb
1089 [nfs4acl_special]
1090 path = $shrdir
1091 comment = smb username is [%U]
1092 nfs4:mode = special
1093 vfs objects = nfs4acl_xattr xattr_tdb
1095 [xcopy_share]
1096 path = $shrdir
1097 comment = smb username is [%U]
1098 create mask = 777
1099 force create mode = 777
1100 [posix_share]
1101 path = $shrdir
1102 comment = smb username is [%U]
1103 create mask = 0777
1104 force create mode = 0
1105 directory mask = 0777
1106 force directory mode = 0
1107 vfs objects = xattr_tdb
1109 [print\$]
1110 copy = tmp
1112 close(CONF);
1115 ## create a test account
1118 unless (open(PASSWD, ">$nss_wrapper_passwd")) {
1119 warn("Unable to open $nss_wrapper_passwd");
1120 return undef;
1122 print PASSWD "nobody:x:$uid_nobody:$gid_nobody:nobody gecos:$prefix_abs:/bin/false
1123 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
1124 pdbtest:x:$uid_pdbtest:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
1126 if ($unix_uid != 0) {
1127 print PASSWD "root:x:$uid_root:$gid_root:root gecos:$prefix_abs:/bin/false";
1129 close(PASSWD);
1131 unless (open(GROUP, ">$nss_wrapper_group")) {
1132 warn("Unable to open $nss_wrapper_group");
1133 return undef;
1135 print GROUP "nobody:x:$gid_nobody:
1136 nogroup:x:$gid_nogroup:nobody
1137 $unix_name-group:x:$unix_gids[0]:
1138 domusers:X:$gid_domusers:
1139 domadmins:X:$gid_domadmins:
1141 if ($unix_gids[0] != 0) {
1142 print GROUP "root:x:$gid_root:";
1145 close(GROUP);
1147 foreach my $evlog (@eventlog_list) {
1148 my $evlogtdb = "$eventlogdir/$evlog.tdb";
1149 open(EVENTLOG, ">$evlogtdb") or die("Unable to open $evlogtdb");
1150 close(EVENTLOG);
1153 $ENV{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1154 $ENV{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1156 my $cmd = Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $unix_name > /dev/null";
1157 unless (open(PWD, "|$cmd")) {
1158 warn("Unable to set password for test account\n$cmd");
1159 return undef;
1161 print PWD "$password\n$password\n";
1162 unless (close(PWD)) {
1163 warn("Unable to set password for test account\n$cmd");
1164 return undef;
1166 print "DONE\n";
1168 open(DNS_UPDATE_LIST, ">$prefix/dns_update_list") or die("Unable to open $$prefix/dns_update_list");
1169 print DNS_UPDATE_LIST "A $server. $server_ip";
1170 close(DNS_UPDATE_LIST);
1172 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) {
1173 die "Unable to update hostname into $dns_host_file";
1176 $ret{SERVER_IP} = $server_ip;
1177 $ret{NMBD_TEST_LOG} = "$prefix/nmbd_test.log";
1178 $ret{NMBD_TEST_LOG_POS} = 0;
1179 $ret{WINBINDD_TEST_LOG} = "$prefix/winbindd_test.log";
1180 $ret{WINBINDD_TEST_LOG_POS} = 0;
1181 $ret{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
1182 $ret{SMBD_TEST_LOG_POS} = 0;
1183 $ret{SERVERCONFFILE} = $conffile;
1184 $ret{CONFIGURATION} ="-s $conffile";
1185 $ret{SERVER} = $server;
1186 $ret{USERNAME} = $unix_name;
1187 $ret{USERID} = $unix_uid;
1188 $ret{DOMAIN} = $domain;
1189 $ret{NETBIOSNAME} = $server;
1190 $ret{PASSWORD} = $password;
1191 $ret{PIDDIR} = $piddir;
1192 $ret{WINBINDD_SOCKET_DIR} = $wbsockdir;
1193 $ret{WINBINDD_PRIV_PIPE_DIR} = $wbsockprivdir;
1194 $ret{NMBD_SOCKET_DIR} = $nmbdsockdir;
1195 $ret{SOCKET_WRAPPER_DEFAULT_IFACE} = $swiface;
1196 $ret{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1197 $ret{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1198 $ret{NSS_WRAPPER_WINBIND_SO_PATH} = Samba::nss_wrapper_winbind_so_path($self);
1199 $ret{LOCAL_PATH} = "$shrdir";
1201 return \%ret;
1204 sub wait_for_start($$$$$)
1206 my ($self, $envvars, $nmbd, $winbindd, $smbd) = @_;
1207 my $ret;
1209 if ($nmbd eq "yes") {
1210 # give time for nbt server to register its names
1211 print "delaying for nbt name registration\n";
1212 sleep(10);
1213 # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init
1214 my $nmblookup = Samba::bindir_path($self, "nmblookup3");
1215 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} __SAMBA__");
1216 system("$nmblookup $envvars->{CONFIGURATION} __SAMBA__");
1217 system("$nmblookup $envvars->{CONFIGURATION} -U 127.255.255.255 __SAMBA__");
1218 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} $envvars->{SERVER}");
1219 system("$nmblookup $envvars->{CONFIGURATION} $envvars->{SERVER}");
1222 if ($winbindd eq "yes") {
1223 print "checking for winbindd\n";
1224 my $count = 0;
1225 do {
1226 $ret = system("WINBINDD_SOCKET_DIR=" . $envvars->{WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " -p");
1227 if ($ret != 0) {
1228 sleep(2);
1230 $count++;
1231 } while ($ret != 0 && $count < 10);
1232 if ($count == 10) {
1233 print "WINBINDD not reachable after 20 seconds\n";
1234 teardown_env($self, $envvars);
1235 return 0;
1239 if ($smbd eq "yes") {
1240 # make sure smbd is also up set
1241 print "wait for smbd\n";
1243 my $count = 0;
1244 do {
1245 $ret = system(Samba::bindir_path($self, "smbclient3") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER} -U% -p 139");
1246 if ($ret != 0) {
1247 sleep(2);
1249 $count++
1250 } while ($ret != 0 && $count < 10);
1251 if ($count == 10) {
1252 print "SMBD failed to start up in a reasonable time (20sec)\n";
1253 teardown_env($self, $envvars);
1254 return 0;
1258 # Ensure we have domain users mapped.
1259 $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=513 unixgroup=domusers type=domain");
1260 if ($ret != 0) {
1261 return 1;
1263 $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=512 unixgroup=domadmins type=domain");
1264 if ($ret != 0) {
1265 return 1;
1268 if ($winbindd eq "yes") {
1269 # note: creating builtin groups requires winbindd for the
1270 # unix id allocator
1271 $ret = system("WINBINDD_SOCKET_DIR=" . $envvars->{WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} sam createbuiltingroup Users");
1272 if ($ret != 0) {
1273 print "Failed to create BUILTIN\\Users group\n";
1274 return 0;
1276 my $count = 0;
1277 do {
1278 system(Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} cache flush");
1279 $ret = system("WINBINDD_SOCKET_DIR=" . $envvars->{WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545");
1280 if ($ret != 0) {
1281 sleep(2);
1283 $count++;
1284 } while ($ret != 0 && $count < 10);
1285 if ($count == 10) {
1286 print "WINBINDD not reachable after 20 seconds\n";
1287 teardown_env($self, $envvars);
1288 return 0;
1292 print $self->getlog_env($envvars);
1294 return 1;