selftest: Use the actual name of the nmblookup binary.
[kamenim/samba-autobuild/.git] / selftest / target / Samba3.pm
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.
5
6 package Samba3;
7
8 use strict;
9 use Cwd qw(abs_path);
10 use FindBin qw($RealBin);
11 use POSIX;
12 use target::Samba;
13
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: $!");
19
20         while (<IN>) {
21                 if (/WITH_ADS/) {
22                        $found_ads = 1;
23                 }
24         }
25         close IN;
26
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;
30 }
31
32 # return smb.conf parameters applicable to @path, based on the underlying
33 # filesystem type
34 sub get_fs_specific_conf($$)
35 {
36         my ($self, $path) = @_;
37         my $mods = "";
38         my $stat_out = `stat --file-system $path` or return "";
39
40         if ($stat_out =~ m/Type:\s+btrfs/) {
41                 $mods .= "btrfs ";
42         }
43
44         if ($mods) {
45                 return "vfs objects = $mods";
46         }
47
48         return undef;
49 }
50
51 sub new($$) {
52         my ($classname, $bindir, $binary_mapping, $srcdir, $server_maxtime) = @_;
53         my $self = { vars => {},
54                      bindir => $bindir,
55                      binary_mapping => $binary_mapping,
56                      srcdir => $srcdir,
57                      server_maxtime => $server_maxtime
58         };
59         bless $self;
60         return $self;
61 }
62
63 sub teardown_env($$)
64 {
65         my ($self, $envvars) = @_;
66         my $count = 0;
67
68         # This should cause smbd to terminate gracefully
69         close($envvars->{STDIN_PIPE});
70
71         my $smbdpid = $envvars->{SMBD_TL_PID};
72         my $nmbdpid = $envvars->{NMBD_TL_PID};
73         my $winbinddpid = $envvars->{WINBINDD_TL_PID};
74
75         # This should give it time to write out the gcov data
76         until ($count > 20) {
77             my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
78             my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
79             my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
80             if ($smbdchild == -1
81                 && $nmbdchild == -1
82                 && $winbinddchild == -1) {
83                 last;
84             }
85             sleep(1);
86             $count++;
87         }
88
89         if ($count <= 20 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
90             return;
91         }
92
93         $self->stop_sig_term($smbdpid);
94         $self->stop_sig_term($nmbdpid);
95         $self->stop_sig_term($winbinddpid);
96
97         $count = 0;
98         until ($count > 10) {
99             my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
100             my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
101             my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
102             if ($smbdchild == -1
103                 && $nmbdchild == -1
104                 && $winbinddchild == -1) {
105                 last;
106             }
107             sleep(1);
108             $count++;
109         }
110
111         if ($count <= 10 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
112             return;
113         }
114
115         warn("timelimit process did not quit on SIGTERM, sending SIGKILL");
116         $self->stop_sig_kill($smbdpid);
117         $self->stop_sig_kill($nmbdpid);
118         $self->stop_sig_kill($winbinddpid);
119
120         return 0;
121 }
122
123 sub getlog_env_app($$$)
124 {
125         my ($self, $envvars, $name) = @_;
126
127         my $title = "$name LOG of: $envvars->{NETBIOSNAME}\n";
128         my $out = $title;
129
130         open(LOG, "<".$envvars->{$name."_TEST_LOG"});
131
132         seek(LOG, $envvars->{$name."_TEST_LOG_POS"}, SEEK_SET);
133         while (<LOG>) {
134                 $out .= $_;
135         }
136         $envvars->{$name."_TEST_LOG_POS"} = tell(LOG);
137         close(LOG);
138
139         return "" if $out eq $title;
140  
141         return $out;
142 }
143
144 sub getlog_env($$)
145 {
146         my ($self, $envvars) = @_;
147         my $ret = "";
148
149         $ret .= $self->getlog_env_app($envvars, "SMBD");
150         $ret .= $self->getlog_env_app($envvars, "NMBD");
151         $ret .= $self->getlog_env_app($envvars, "WINBINDD");
152
153         return $ret;
154 }
155
156 sub check_env($$)
157 {
158         my ($self, $envvars) = @_;
159
160         my $childpid = waitpid(-1, WNOHANG);
161
162         # TODO ...
163         return 1;
164 }
165
166 sub setup_env($$$)
167 {
168         my ($self, $envname, $path) = @_;
169
170         $ENV{ENVNAME} = $envname;
171
172         if (defined($self->{vars}->{$envname})) {
173                 return $self->{vars}->{$envname};
174         }
175
176         if ($envname eq "s3dc") {
177                 return $self->setup_s3dc("$path/s3dc");
178         } elsif ($envname eq "simpleserver") {
179                 return $self->setup_simpleserver("$path/simpleserver");
180         } elsif ($envname eq "maptoguest") {
181                 return $self->setup_maptoguest("$path/maptoguest");
182         } elsif ($envname eq "ktest") {
183                 return $self->setup_ktest("$path/ktest");
184         } elsif ($envname eq "member") {
185                 if (not defined($self->{vars}->{s3dc})) {
186                         if (not defined($self->setup_s3dc("$path/s3dc"))) {
187                                 return undef;
188                         }
189                 }
190                 return $self->setup_member("$path/member", $self->{vars}->{s3dc});
191         } else {
192                 return "UNKNOWN";
193         }
194 }
195
196 sub setup_s3dc($$)
197 {
198         my ($self, $path) = @_;
199
200         print "PROVISIONING S3DC...";
201
202         my $s3dc_options = "
203         domain master = yes
204         domain logons = yes
205         lanman auth = yes
206
207         rpc_server:epmapper = external
208         rpc_server:spoolss = external
209         rpc_server:lsarpc = external
210         rpc_server:samr = external
211         rpc_server:netlogon = external
212         rpc_server:register_embedded_np = yes
213
214         rpc_daemon:epmd = fork
215         rpc_daemon:spoolssd = fork
216         rpc_daemon:lsasd = fork
217 ";
218
219         my $vars = $self->provision($path,
220                                     "LOCALS3DC2",
221                                     "locals3dc2pass",
222                                     $s3dc_options);
223
224         $vars or return undef;
225
226         if (not $self->check_or_start($vars, "yes", "yes", "yes")) {
227                return undef;
228         }
229
230         $vars->{DC_SERVER} = $vars->{SERVER};
231         $vars->{DC_SERVER_IP} = $vars->{SERVER_IP};
232         $vars->{DC_NETBIOSNAME} = $vars->{NETBIOSNAME};
233         $vars->{DC_USERNAME} = $vars->{USERNAME};
234         $vars->{DC_PASSWORD} = $vars->{PASSWORD};
235
236         $self->{vars}->{s3dc} = $vars;
237
238         return $vars;
239 }
240
241 sub setup_member($$$)
242 {
243         my ($self, $prefix, $s3dcvars) = @_;
244
245         print "PROVISIONING MEMBER...";
246
247         my $member_options = "
248         security = domain
249         server signing = on
250         dbwrap_tdb_mutexes:* = yes
251 ";
252         my $ret = $self->provision($prefix,
253                                    "LOCALMEMBER3",
254                                    "localmember3pass",
255                                    $member_options);
256
257         $ret or return undef;
258
259         my $net = Samba::bindir_path($self, "net");
260         my $cmd = "";
261         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
262         $cmd .= "$net join $ret->{CONFIGURATION} $s3dcvars->{DOMAIN} member";
263         $cmd .= " -U$s3dcvars->{USERNAME}\%$s3dcvars->{PASSWORD}";
264
265         if (system($cmd) != 0) {
266             warn("Join failed\n$cmd");
267             return undef;
268         }
269
270         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
271                return undef;
272         }
273
274         $ret->{DC_SERVER} = $s3dcvars->{SERVER};
275         $ret->{DC_SERVER_IP} = $s3dcvars->{SERVER_IP};
276         $ret->{DC_NETBIOSNAME} = $s3dcvars->{NETBIOSNAME};
277         $ret->{DC_USERNAME} = $s3dcvars->{USERNAME};
278         $ret->{DC_PASSWORD} = $s3dcvars->{PASSWORD};
279
280         return $ret;
281 }
282
283 sub setup_admember($$$$)
284 {
285         my ($self, $prefix, $dcvars) = @_;
286
287         # If we didn't build with ADS, pretend this env was never available
288         if (not $self->have_ads()) {
289                 return "UNKNOWN";
290         }
291
292         print "PROVISIONING S3 AD MEMBER...";
293
294         my $member_options = "
295         security = ads
296         server signing = on
297         workgroup = $dcvars->{DOMAIN}
298         realm = $dcvars->{REALM}
299 ";
300
301         my $ret = $self->provision($prefix,
302                                    "LOCALADMEMBER",
303                                    "loCalMemberPass",
304                                    $member_options);
305
306         $ret or return undef;
307
308         close(USERMAP);
309         $ret->{DOMAIN} = $dcvars->{DOMAIN};
310         $ret->{REALM} = $dcvars->{REALM};
311
312         my $ctx;
313         my $prefix_abs = abs_path($prefix);
314         $ctx = {};
315         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
316         $ctx->{domain} = $dcvars->{DOMAIN};
317         $ctx->{realm} = $dcvars->{REALM};
318         $ctx->{dnsname} = lc($dcvars->{REALM});
319         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
320         Samba::mk_krb5_conf($ctx, "");
321
322         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
323
324         my $net = Samba::bindir_path($self, "net");
325         my $cmd = "";
326         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
327         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
328         $cmd .= "$net join $ret->{CONFIGURATION}";
329         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
330
331         if (system($cmd) != 0) {
332             warn("Join failed\n$cmd");
333             return undef;
334         }
335
336         # We need world access to this share, as otherwise the domain
337         # administrator from the AD domain provided by Samba4 can't
338         # access the share for tests.
339         chmod 0777, "$prefix/share";
340
341         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
342                 return undef;
343         }
344
345         $ret->{DC_SERVER} = $dcvars->{SERVER};
346         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
347         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
348         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
349         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
350
351         # Special case, this is called from Samba4.pm but needs to use the Samba3 check_env and get_log_env
352         $ret->{target} = $self;
353
354         return $ret;
355 }
356
357 sub setup_admember_rfc2307($$$$)
358 {
359         my ($self, $prefix, $dcvars) = @_;
360
361         # If we didn't build with ADS, pretend this env was never available
362         if (not $self->have_ads()) {
363                 return "UNKNOWN";
364         }
365
366         print "PROVISIONING S3 AD MEMBER WITH idmap_rfc2307 config...";
367
368         my $member_options = "
369         security = ads
370         server signing = on
371         workgroup = $dcvars->{DOMAIN}
372         realm = $dcvars->{REALM}
373         idmap config $dcvars->{DOMAIN} : backend = rfc2307
374         idmap config $dcvars->{DOMAIN} : range = 2000000-2999999
375         idmap config $dcvars->{DOMAIN} : ldap_server = ad
376         idmap config $dcvars->{DOMAIN} : bind_path_user = ou=idmap,dc=samba,dc=example,dc=com
377         idmap config $dcvars->{DOMAIN} : bind_path_group = ou=idmap,dc=samba,dc=example,dc=com
378 ";
379
380         my $ret = $self->provision($prefix,
381                                    "RFC2307MEMBER",
382                                    "loCalMemberPass",
383                                    $member_options);
384
385         $ret or return undef;
386
387         close(USERMAP);
388         $ret->{DOMAIN} = $dcvars->{DOMAIN};
389         $ret->{REALM} = $dcvars->{REALM};
390
391         my $ctx;
392         my $prefix_abs = abs_path($prefix);
393         $ctx = {};
394         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
395         $ctx->{domain} = $dcvars->{DOMAIN};
396         $ctx->{realm} = $dcvars->{REALM};
397         $ctx->{dnsname} = lc($dcvars->{REALM});
398         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
399         Samba::mk_krb5_conf($ctx, "");
400
401         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
402
403         my $net = Samba::bindir_path($self, "net");
404         my $cmd = "";
405         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
406         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
407         $cmd .= "$net join $ret->{CONFIGURATION}";
408         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
409
410         if (system($cmd) != 0) {
411             warn("Join failed\n$cmd");
412             return undef;
413         }
414
415         # We need world access to this share, as otherwise the domain
416         # administrator from the AD domain provided by Samba4 can't
417         # access the share for tests.
418         chmod 0777, "$prefix/share";
419
420         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
421                 return undef;
422         }
423
424         $ret->{DC_SERVER} = $dcvars->{SERVER};
425         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
426         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
427         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
428         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
429
430         # Special case, this is called from Samba4.pm but needs to use the Samba3 check_env and get_log_env
431         $ret->{target} = $self;
432
433         return $ret;
434 }
435
436 sub setup_simpleserver($$)
437 {
438         my ($self, $path) = @_;
439
440         print "PROVISIONING server with security=share...";
441
442         my $prefix_abs = abs_path($path);
443
444         my $simpleserver_options = "
445         lanman auth = yes
446         vfs objects = xattr_tdb streams_depot
447
448 [vfs_aio_fork]
449         path = $prefix_abs/share
450         vfs objects = aio_fork
451         read only = no
452         vfs_aio_fork:erratic_testing_mode=yes
453 ";
454
455         my $vars = $self->provision($path,
456                                     "LOCALSHARE4",
457                                     "local4pass",
458                                     $simpleserver_options);
459
460         $vars or return undef;
461
462         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
463                return undef;
464         }
465
466         $self->{vars}->{simpleserver} = $vars;
467
468         return $vars;
469 }
470
471 sub setup_ktest($$$)
472 {
473         my ($self, $prefix) = @_;
474
475         # If we didn't build with ADS, pretend this env was never available
476         if (not $self->have_ads()) {
477                 return "UNKNOWN";
478         }
479
480         print "PROVISIONING server with security=ads...";
481
482         my $ktest_options = "
483         workgroup = KTEST
484         realm = ktest.samba.example.com
485         security = ads
486         username map = $prefix/lib/username.map
487         server signing = required
488 ";
489
490         my $ret = $self->provision($prefix,
491                                    "LOCALKTEST6",
492                                    "localktest6pass",
493                                    $ktest_options);
494
495         $ret or return undef;
496
497         my $ctx;
498         my $prefix_abs = abs_path($prefix);
499         $ctx = {};
500         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
501         $ctx->{domain} = "KTEST";
502         $ctx->{realm} = "KTEST.SAMBA.EXAMPLE.COM";
503         $ctx->{dnsname} = lc($ctx->{realm});
504         $ctx->{kdc_ipv4} = "0.0.0.0";
505         Samba::mk_krb5_conf($ctx, "");
506
507         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
508
509         open(USERMAP, ">$prefix/lib/username.map") or die("Unable to open $prefix/lib/username.map");
510         print USERMAP "
511 $ret->{USERNAME} = KTEST\\Administrator
512 ";
513         close(USERMAP);
514
515 #This is the secrets.tdb created by 'net ads join' from Samba3 to a
516 #Samba4 DC with the same parameters as are being used here.  The
517 #domain SID is S-1-5-21-1071277805-689288055-3486227160
518
519         system("cp $self->{srcdir}/source3/selftest/ktest-secrets.tdb $prefix/private/secrets.tdb");
520         chmod 0600, "$prefix/private/secrets.tdb";
521
522 #Make sure there's no old ntdb file.
523         system("rm -f $prefix/private/secrets.ntdb");
524
525 #This uses a pre-calculated krb5 credentials cache, obtained by running Samba4 with:
526 # "--option=kdc:service ticket lifetime=239232" "--option=kdc:user ticket lifetime=239232" "--option=kdc:renewal lifetime=239232"
527 #
528 #and having in krb5.conf:
529 # ticket_lifetime = 799718400
530 # renew_lifetime = 799718400
531 #
532 # The commands for the -2 keytab where were:
533 # kinit administrator@KTEST.SAMBA.EXAMPLE.COM
534 # kvno host/localktest6@KTEST.SAMBA.EXAMPLE.COM
535 # kvno cifs/localktest6@KTEST.SAMBA.EXAMPLE.COM
536 # kvno host/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
537 # kvno cifs/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
538 #
539 # and then for the -3 keytab, I did
540 #
541 # net changetrustpw; kdestroy and the same again.
542 #
543 # This creates a credential cache with a very long lifetime (2036 at
544 # at 2011-04), and shows that running 'net changetrustpw' does not
545 # break existing logins (for the secrets.tdb method at least).
546 #
547
548         $ret->{KRB5_CCACHE}="FILE:$prefix/krb5_ccache";
549
550         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-2 $prefix/krb5_ccache-2");
551         chmod 0600, "$prefix/krb5_ccache-2";
552
553         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-3 $prefix/krb5_ccache-3");
554         chmod 0600, "$prefix/krb5_ccache-3";
555
556         # We need world access to this share, as otherwise the domain
557         # administrator from the AD domain provided by ktest can't
558         # access the share for tests.
559         chmod 0777, "$prefix/share";
560
561         if (not $self->check_or_start($ret, "yes", "no", "yes")) {
562                return undef;
563         }
564         return $ret;
565 }
566
567 sub setup_maptoguest($$)
568 {
569         my ($self, $path) = @_;
570
571         print "PROVISIONING maptoguest...";
572
573         my $options = "
574 map to guest = bad user
575 ";
576
577         my $vars = $self->provision($path,
578                                     "maptoguest",
579                                     "maptoguestpass",
580                                     $options);
581
582         $vars or return undef;
583
584         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
585                return undef;
586         }
587
588         $self->{vars}->{s3maptoguest} = $vars;
589
590         return $vars;
591 }
592
593 sub stop_sig_term($$) {
594         my ($self, $pid) = @_;
595         kill("USR1", $pid) or kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
596 }
597
598 sub stop_sig_kill($$) {
599         my ($self, $pid) = @_;
600         kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
601 }
602
603 sub write_pid($$$)
604 {
605         my ($env_vars, $app, $pid) = @_;
606
607         open(PID, ">$env_vars->{PIDDIR}/timelimit.$app.pid");
608         print PID $pid;
609         close(PID);
610 }
611
612 sub read_pid($$)
613 {
614         my ($env_vars, $app) = @_;
615
616         open(PID, "<$env_vars->{PIDDIR}/timelimit.$app.pid");
617         my $pid = <PID>;
618         close(PID);
619         return $pid;
620 }
621
622 sub check_or_start($$$$$) {
623         my ($self, $env_vars, $nmbd, $winbindd, $smbd) = @_;
624
625         # use a pipe for stdin in the child processes. This allows
626         # those processes to monitor the pipe for EOF to ensure they
627         # exit when the test script exits
628         pipe(STDIN_READER, $env_vars->{STDIN_PIPE});
629
630         unlink($env_vars->{NMBD_TEST_LOG});
631         print "STARTING NMBD...";
632         my $pid = fork();
633         if ($pid == 0) {
634                 open STDOUT, ">$env_vars->{NMBD_TEST_LOG}";
635                 open STDERR, '>&STDOUT';
636
637                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
638
639                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
640                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
641                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
642
643                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
644                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
645                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
646                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
647                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
648
649                 $ENV{ENVNAME} = "$ENV{ENVNAME}.nmbd";
650
651                 if ($nmbd ne "yes") {
652                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
653                                 my $signame = shift;
654                                 print("Skip nmbd received signal $signame");
655                                 exit 0;
656                         };
657                         sleep($self->{server_maxtime});
658                         exit 0;
659                 }
660
661                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "nmbd");
662                 my @optargs = ("-d0");
663                 if (defined($ENV{NMBD_OPTIONS})) {
664                         @optargs = split(/ /, $ENV{NMBD_OPTIONS});
665                 }
666                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
667                 if(defined($ENV{NMBD_VALGRIND})) { 
668                         @preargs = split(/ /, $ENV{NMBD_VALGRIND});
669                 }
670                 my @args = ("-F", "--no-process-group",
671                             "-s", $env_vars->{SERVERCONFFILE},
672                             "-l", $env_vars->{LOGDIR});
673                 if (not defined($ENV{NMBD_DONT_LOG_STDOUT})) {
674                         push(@args, "--log-stdout");
675                 }
676
677                 close($env_vars->{STDIN_PIPE});
678                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
679
680                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
681                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
682         }
683         $env_vars->{NMBD_TL_PID} = $pid;
684         write_pid($env_vars, "nmbd", $pid);
685         print "DONE\n";
686
687         unlink($env_vars->{WINBINDD_TEST_LOG});
688         print "STARTING WINBINDD...";
689         $pid = fork();
690         if ($pid == 0) {
691                 open STDOUT, ">$env_vars->{WINBINDD_TEST_LOG}";
692                 open STDERR, '>&STDOUT';
693
694                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
695
696                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
697                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
698                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
699
700                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
701                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
702                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
703                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
704                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
705
706                 $ENV{ENVNAME} = "$ENV{ENVNAME}.winbindd";
707
708                 if ($winbindd ne "yes") {
709                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
710                                 my $signame = shift;
711                                 print("Skip winbindd received signal $signame");
712                                 exit 0;
713                         };
714                         sleep($self->{server_maxtime});
715                         exit 0;
716                 }
717
718                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "winbindd");
719                 my @optargs = ("-d0");
720                 if (defined($ENV{WINBINDD_OPTIONS})) {
721                         @optargs = split(/ /, $ENV{WINBINDD_OPTIONS});
722                 }
723                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
724                 if(defined($ENV{WINBINDD_VALGRIND})) {
725                         @preargs = split(/ /, $ENV{WINBINDD_VALGRIND});
726                 }
727                 my @args = ("-F", "--no-process-group",
728                             "-s", $env_vars->{SERVERCONFFILE},
729                             "-l", $env_vars->{LOGDIR});
730                 if (not defined($ENV{WINBINDD_DONT_LOG_STDOUT})) {
731                         push(@args, "--stdout");
732                 }
733
734                 close($env_vars->{STDIN_PIPE});
735                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
736
737                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
738                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
739         }
740         $env_vars->{WINBINDD_TL_PID} = $pid;
741         write_pid($env_vars, "winbindd", $pid);
742         print "DONE\n";
743
744         unlink($env_vars->{SMBD_TEST_LOG});
745         print "STARTING SMBD...";
746         $pid = fork();
747         if ($pid == 0) {
748                 open STDOUT, ">$env_vars->{SMBD_TEST_LOG}";
749                 open STDERR, '>&STDOUT';
750
751                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
752
753                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
754                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
755                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
756
757                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
758                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
759                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
760                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
761                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
762
763                 $ENV{ENVNAME} = "$ENV{ENVNAME}.smbd";
764
765                 if ($smbd ne "yes") {
766                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
767                                 my $signame = shift;
768                                 print("Skip smbd received signal $signame");
769                                 exit 0;
770                         };
771                         sleep($self->{server_maxtime});
772                         exit 0;
773                 }
774
775                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "smbd");
776                 my @optargs = ("-d0");
777                 if (defined($ENV{SMBD_OPTIONS})) {
778                         @optargs = split(/ /, $ENV{SMBD_OPTIONS});
779                 }
780                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
781                 if(defined($ENV{SMBD_VALGRIND})) {
782                         @preargs = split(/ /,$ENV{SMBD_VALGRIND});
783                 }
784                 my @args = ("-F", "--no-process-group",
785                             "-s", $env_vars->{SERVERCONFFILE},
786                             "-l", $env_vars->{LOGDIR});
787                 if (not defined($ENV{SMBD_DONT_LOG_STDOUT})) {
788                         push(@args, "--log-stdout");
789                 }
790
791                 close($env_vars->{STDIN_PIPE});
792                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
793
794                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
795                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
796         }
797         $env_vars->{SMBD_TL_PID} = $pid;
798         write_pid($env_vars, "smbd", $pid);
799         print "DONE\n";
800
801         close(STDIN_READER);
802
803         return $self->wait_for_start($env_vars, $nmbd, $winbindd, $smbd);
804 }
805
806 sub provision($$$$$$)
807 {
808         my ($self, $prefix, $server, $password, $extra_options, $no_delete_prefix) = @_;
809
810         ##
811         ## setup the various environment variables we need
812         ##
813
814         my $swiface = Samba::get_interface($server);
815         my %ret = ();
816         my $server_ip = "127.0.0.$swiface";
817         my $domain = "SAMBA-TEST";
818
819         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `PATH=/usr/ucb:$ENV{PATH} whoami`);
820         chomp $unix_name;
821         my $unix_uid = $>;
822         my $unix_gids_str = $);
823         my @unix_gids = split(" ", $unix_gids_str);
824
825         my $prefix_abs = abs_path($prefix);
826         my $bindir_abs = abs_path($self->{bindir});
827
828         my $dns_host_file = "$ENV{SELFTEST_PREFIX}/dns_host_file";
829
830         my @dirs = ();
831
832         my $shrdir="$prefix_abs/share";
833         push(@dirs,$shrdir);
834
835         my $libdir="$prefix_abs/lib";
836         push(@dirs,$libdir);
837
838         my $piddir="$prefix_abs/pid";
839         push(@dirs,$piddir);
840
841         my $privatedir="$prefix_abs/private";
842         push(@dirs,$privatedir);
843
844         my $lockdir="$prefix_abs/lockdir";
845         push(@dirs,$lockdir);
846
847         my $eventlogdir="$prefix_abs/lockdir/eventlog";
848         push(@dirs,$eventlogdir);
849
850         my $logdir="$prefix_abs/logs";
851         push(@dirs,$logdir);
852
853         my $driver32dir="$shrdir/W32X86";
854         push(@dirs,$driver32dir);
855
856         my $driver64dir="$shrdir/x64";
857         push(@dirs,$driver64dir);
858
859         my $driver40dir="$shrdir/WIN40";
860         push(@dirs,$driver40dir);
861
862         my $ro_shrdir="$shrdir/root-tmp";
863         push(@dirs,$ro_shrdir);
864
865         my $msdfs_shrdir="$shrdir/msdfsshare";
866         push(@dirs,$msdfs_shrdir);
867
868         my $msdfs_deeppath="$msdfs_shrdir/deeppath";
869         push(@dirs,$msdfs_deeppath);
870
871         # this gets autocreated by winbindd
872         my $wbsockdir="$prefix_abs/winbindd";
873         my $wbsockprivdir="$lockdir/winbindd_privileged";
874
875         my $nmbdsockdir="$prefix_abs/nmbd";
876         unlink($nmbdsockdir);
877
878         ## 
879         ## create the test directory layout
880         ##
881         die ("prefix_abs = ''") if $prefix_abs eq "";
882         die ("prefix_abs = '/'") if $prefix_abs eq "/";
883
884         mkdir($prefix_abs, 0777);
885         print "CREATE TEST ENVIRONMENT IN '$prefix'...";
886         if (not defined($no_delete_prefix) or not $no_delete_prefix) {
887             system("rm -rf $prefix_abs/*");
888         }
889         mkdir($_, 0777) foreach(@dirs);
890
891         my $fs_specific_conf = $self->get_fs_specific_conf($shrdir);
892
893         ##
894         ## lockdir and piddir must be 0755
895         ##
896         chmod 0755, $lockdir;
897         chmod 0755, $piddir;
898
899
900         ##
901         ## create ro and msdfs share layout
902         ##
903
904         chmod 0755, $ro_shrdir;
905         my $unreadable_file = "$ro_shrdir/unreadable_file";
906         unless (open(UNREADABLE_FILE, ">$unreadable_file")) {
907                 warn("Unable to open $unreadable_file");
908                 return undef;
909         }
910         close(UNREADABLE_FILE);
911         chmod 0600, $unreadable_file;
912
913         my $msdfs_target = "$ro_shrdir/msdfs-target";
914         unless (open(MSDFS_TARGET, ">$msdfs_target")) {
915                 warn("Unable to open $msdfs_target");
916                 return undef;
917         }
918         close(MSDFS_TARGET);
919         chmod 0666, $msdfs_target;
920         symlink "msdfs:$server_ip\\ro-tmp", "$msdfs_shrdir/msdfs-src1";
921         symlink "msdfs:$server_ip\\ro-tmp", "$msdfs_shrdir/deeppath/msdfs-src2";
922
923         my $conffile="$libdir/server.conf";
924
925         my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/lib/nss_wrapper/nss_wrapper.pl";
926         my $nss_wrapper_passwd = "$privatedir/passwd";
927         my $nss_wrapper_group = "$privatedir/group";
928         my $nss_wrapper_hosts = "$ENV{SELFTEST_PREFIX}/hosts";
929
930         my $mod_printer_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/printing/modprinter.pl";
931
932         my @eventlog_list = ("dns server", "application");
933
934         ##
935         ## calculate uids and gids
936         ##
937
938         my ($max_uid, $max_gid);
939         my ($uid_nobody, $uid_root, $uid_pdbtest, $uid_pdbtest2);
940         my ($gid_nobody, $gid_nogroup, $gid_root, $gid_domusers, $gid_domadmins);
941
942         if ($unix_uid < 0xffff - 4) {
943                 $max_uid = 0xffff;
944         } else {
945                 $max_uid = $unix_uid;
946         }
947
948         $uid_root = $max_uid - 1;
949         $uid_nobody = $max_uid - 2;
950         $uid_pdbtest = $max_uid - 3;
951         $uid_pdbtest2 = $max_uid - 4;
952
953         if ($unix_gids[0] < 0xffff - 5) {
954                 $max_gid = 0xffff;
955         } else {
956                 $max_gid = $unix_gids[0];
957         }
958
959         $gid_nobody = $max_gid - 1;
960         $gid_nogroup = $max_gid - 2;
961         $gid_root = $max_gid - 3;
962         $gid_domusers = $max_gid - 4;
963         $gid_domadmins = $max_gid - 5;
964
965         ##
966         ## create conffile
967         ##
968
969         unless (open(CONF, ">$conffile")) {
970                 warn("Unable to open $conffile");
971                 return undef;
972         }
973         print CONF "
974 [global]
975         netbios name = $server
976         interfaces = $server_ip/8
977         bind interfaces only = yes
978         panic action = cd $self->{srcdir} && $self->{srcdir}/selftest/gdb_backtrace %d %\$(MAKE_TEST_BINARY)
979         smbd:suicide mode = yes
980
981         workgroup = $domain
982
983         private dir = $privatedir
984         pid directory = $piddir
985         lock directory = $lockdir
986         log file = $logdir/log.\%m
987         log level = 1
988         debug pid = yes
989         max log size = 0
990
991         state directory = $lockdir
992         cache directory = $lockdir
993
994         passdb backend = tdbsam
995
996         time server = yes
997
998         add user script =               $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
999         add group script =              $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action add --name %g
1000         add machine script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
1001         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
1002         delete user script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action delete --name %u
1003         delete group script =           $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action delete --name %g
1004         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
1005
1006         addprinter command =            $mod_printer_pl -a -s $conffile --
1007         deleteprinter command =         $mod_printer_pl -d -s $conffile --
1008
1009         eventlog list = application \"dns server\"
1010
1011         kernel oplocks = no
1012         kernel change notify = no
1013
1014         syslog = no
1015         printing = bsd
1016         printcap name = /dev/null
1017
1018         winbindd socket directory = $wbsockdir
1019         nmbd:socket dir = $nmbdsockdir
1020         idmap config * : range = 100000-200000
1021         winbind enum users = yes
1022         winbind enum groups = yes
1023         winbind separator = /
1024
1025 #       min receivefile size = 4000
1026
1027         read only = no
1028         server signing = auto
1029
1030         smbd:sharedelay = 100000
1031         smbd:writetimeupdatedelay = 500000
1032         map hidden = no
1033         map system = no
1034         map readonly = no
1035         store dos attributes = yes
1036         create mask = 755
1037         dos filemode = yes
1038         vfs objects = acl_xattr fake_acls xattr_tdb streams_depot
1039
1040         printing = vlp
1041         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1042         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1043         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1044         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1045         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1046         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1047         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1048         lpq cache time = 0
1049         print notify backchannel = yes
1050
1051         ncalrpc dir = $prefix_abs/ncalrpc
1052         resolv:host file = $dns_host_file
1053
1054         # The samba3.blackbox.smbclient_s3 test uses this to test that
1055         # sending messages works, and that the %m sub works.
1056         message command = mv %s $shrdir/message.%m
1057
1058         # Begin extra options
1059         $extra_options
1060         # End extra options
1061
1062         #Include user defined custom parameters if set
1063 ";
1064
1065         if (defined($ENV{INCLUDE_CUSTOM_CONF})) {
1066                 print CONF "\t$ENV{INCLUDE_CUSTOM_CONF}\n";
1067         }
1068
1069         print CONF "
1070 [tmp]
1071         path = $shrdir
1072         comment = smb username is [%U]
1073 [tmpsort]
1074         path = $shrdir
1075         comment = Load dirsort module
1076         vfs objects = dirsort acl_xattr fake_acls xattr_tdb streams_depot
1077 [tmpenc]
1078         path = $shrdir
1079         comment = encrypt smb username is [%U]
1080         smb encrypt = required
1081         vfs objects = dirsort
1082 [tmpguest]
1083         path = $shrdir
1084         guest ok = yes
1085 [guestonly]
1086         path = $shrdir
1087         guest only = yes
1088         guest ok = yes
1089 [forceuser]
1090         path = $shrdir
1091         force user = $unix_name
1092         guest ok = yes
1093 [forcegroup]
1094         path = $shrdir
1095         force group = nogroup
1096         guest ok = yes
1097 [ro-tmp]
1098         path = $ro_shrdir
1099         guest ok = yes
1100 [write-list-tmp]
1101         path = $shrdir
1102         read only = yes
1103         write list = $unix_name
1104 [valid-users-tmp]
1105         path = $shrdir
1106         valid users = $unix_name
1107 [msdfs-share]
1108         path = $msdfs_shrdir
1109         msdfs root = yes
1110         guest ok = yes
1111 [hideunread]
1112         copy = tmp
1113         hide unreadable = yes
1114 [tmpcase]
1115         copy = tmp
1116         case sensitive = yes
1117 [hideunwrite]
1118         copy = tmp
1119         hide unwriteable files = yes
1120 [durable]
1121         copy = tmp
1122         kernel share modes = no
1123         kernel oplocks = no
1124         posix locking = no
1125 [fs_specific]
1126         copy = tmp
1127         $fs_specific_conf
1128 [print1]
1129         copy = tmp
1130         printable = yes
1131
1132 [print2]
1133         copy = print1
1134 [print3]
1135         copy = print1
1136         default devmode = no
1137 [lp]
1138         copy = print1
1139
1140 [nfs4acl_simple]
1141         path = $shrdir
1142         comment = smb username is [%U]
1143         nfs4:mode = simple
1144         vfs objects = nfs4acl_xattr xattr_tdb
1145
1146 [nfs4acl_special]
1147         path = $shrdir
1148         comment = smb username is [%U]
1149         nfs4:mode = special
1150         vfs objects = nfs4acl_xattr xattr_tdb
1151
1152 [xcopy_share]
1153         path = $shrdir
1154         comment = smb username is [%U]
1155         create mask = 777
1156         force create mode = 777
1157 [posix_share]
1158         path = $shrdir
1159         comment = smb username is [%U]
1160         create mask = 0777
1161         force create mode = 0
1162         directory mask = 0777
1163         force directory mode = 0
1164         vfs objects = xattr_tdb
1165 [aio]
1166         copy = tmp
1167         aio read size = 1
1168         aio write size = 1
1169
1170 [print\$]
1171         copy = tmp
1172         ";
1173         close(CONF);
1174
1175         ##
1176         ## create a test account
1177         ##
1178
1179         unless (open(PASSWD, ">$nss_wrapper_passwd")) {
1180            warn("Unable to open $nss_wrapper_passwd");
1181            return undef;
1182         } 
1183         print PASSWD "nobody:x:$uid_nobody:$gid_nobody:nobody gecos:$prefix_abs:/bin/false
1184 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
1185 pdbtest:x:$uid_pdbtest:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
1186 pdbtest2:x:$uid_pdbtest2:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
1187 ";
1188         if ($unix_uid != 0) {
1189                 print PASSWD "root:x:$uid_root:$gid_root:root gecos:$prefix_abs:/bin/false
1190 ";
1191         }
1192         close(PASSWD);
1193
1194         unless (open(GROUP, ">$nss_wrapper_group")) {
1195              warn("Unable to open $nss_wrapper_group");
1196              return undef;
1197         }
1198         print GROUP "nobody:x:$gid_nobody:
1199 nogroup:x:$gid_nogroup:nobody
1200 $unix_name-group:x:$unix_gids[0]:
1201 domusers:X:$gid_domusers:
1202 domadmins:X:$gid_domadmins:
1203 ";
1204         if ($unix_gids[0] != 0) {
1205                 print GROUP "root:x:$gid_root:
1206 ";
1207         }
1208
1209         close(GROUP);
1210
1211         ## hosts
1212         my $hostname = lc($server);
1213         unless (open(HOSTS, ">>$nss_wrapper_hosts")) {
1214                 warn("Unable to open $nss_wrapper_hosts");
1215                 return undef;
1216         }
1217         print HOSTS "${server_ip} ${hostname}.samba.example.com ${hostname}
1218 ";
1219         close(HOSTS);
1220
1221
1222         foreach my $evlog (@eventlog_list) {
1223                 my $evlogtdb = "$eventlogdir/$evlog.tdb";
1224                 open(EVENTLOG, ">$evlogtdb") or die("Unable to open $evlogtdb");
1225                 close(EVENTLOG);
1226         }
1227
1228         $ENV{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1229         $ENV{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1230         $ENV{NSS_WRAPPER_HOSTS} = $nss_wrapper_hosts;
1231
1232         my $cmd = "UID_WRAPPER_ROOT=1 " . Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $unix_name > /dev/null";
1233         unless (open(PWD, "|$cmd")) {
1234              warn("Unable to set password for test account\n$cmd");
1235              return undef;
1236         }
1237         print PWD "$password\n$password\n";
1238         unless (close(PWD)) {
1239              warn("Unable to set password for test account\n$cmd");
1240              return undef; 
1241         }
1242         print "DONE\n";
1243
1244         open(DNS_UPDATE_LIST, ">$prefix/dns_update_list") or die("Unable to open $$prefix/dns_update_list");
1245         print DNS_UPDATE_LIST "A $server. $server_ip";
1246         close(DNS_UPDATE_LIST);
1247
1248         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) {
1249                 die "Unable to update hostname into $dns_host_file";
1250         }
1251
1252         $ret{SERVER_IP} = $server_ip;
1253         $ret{NMBD_TEST_LOG} = "$prefix/nmbd_test.log";
1254         $ret{NMBD_TEST_LOG_POS} = 0;
1255         $ret{WINBINDD_TEST_LOG} = "$prefix/winbindd_test.log";
1256         $ret{WINBINDD_TEST_LOG_POS} = 0;
1257         $ret{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
1258         $ret{SMBD_TEST_LOG_POS} = 0;
1259         $ret{SERVERCONFFILE} = $conffile;
1260         $ret{CONFIGURATION} ="-s $conffile";
1261         $ret{SERVER} = $server;
1262         $ret{USERNAME} = $unix_name;
1263         $ret{USERID} = $unix_uid;
1264         $ret{DOMAIN} = $domain;
1265         $ret{NETBIOSNAME} = $server;
1266         $ret{PASSWORD} = $password;
1267         $ret{PIDDIR} = $piddir;
1268         $ret{SELFTEST_WINBINDD_SOCKET_DIR} = $wbsockdir;
1269         $ret{WINBINDD_PRIV_PIPE_DIR} = $wbsockprivdir;
1270         $ret{NMBD_SOCKET_DIR} = $nmbdsockdir;
1271         $ret{SOCKET_WRAPPER_DEFAULT_IFACE} = $swiface;
1272         $ret{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1273         $ret{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1274         $ret{NSS_WRAPPER_HOSTS} = $nss_wrapper_hosts;
1275         $ret{NSS_WRAPPER_MODULE_SO_PATH} = Samba::nss_wrapper_winbind_so_path($self);
1276         $ret{NSS_WRAPPER_MODULE_FN_PREFIX} = "winbind";
1277         $ret{LOCAL_PATH} = "$shrdir";
1278         $ret{LOGDIR} = $logdir;
1279
1280         return \%ret;
1281 }
1282
1283 sub wait_for_start($$$$$)
1284 {
1285         my ($self, $envvars, $nmbd, $winbindd, $smbd) = @_;
1286         my $ret;
1287
1288         if ($nmbd eq "yes") {
1289             # give time for nbt server to register its names
1290             print "delaying for nbt name registration\n";
1291             sleep(10);
1292             # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init 
1293             my $nmblookup = Samba::bindir_path($self, "nmblookup");
1294             system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} __SAMBA__");
1295             system("$nmblookup $envvars->{CONFIGURATION} __SAMBA__");
1296             system("$nmblookup $envvars->{CONFIGURATION} -U 127.255.255.255 __SAMBA__");
1297             system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} $envvars->{SERVER}");
1298             system("$nmblookup $envvars->{CONFIGURATION} $envvars->{SERVER}");
1299         }
1300
1301         if ($winbindd eq "yes") {
1302             print "checking for winbindd\n";
1303             my $count = 0;
1304             do {
1305                 $ret = system("SELFTEST_WINBINDD_SOCKET_DIR=" . $envvars->{SELFTEST_WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " --ping-dc");
1306                 if ($ret != 0) {
1307                     sleep(2);
1308                 }
1309                 $count++;
1310             } while ($ret != 0 && $count < 10);
1311             if ($count == 10) {
1312                 print "WINBINDD not reachable after 20 seconds\n";
1313                 teardown_env($self, $envvars);
1314                 return 0;
1315             }
1316         }
1317
1318         if ($smbd eq "yes") {
1319             # make sure smbd is also up set
1320             print "wait for smbd\n";
1321
1322             my $count = 0;
1323             do {
1324                 $ret = system(Samba::bindir_path($self, "smbclient3") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER} -U% -p 139");
1325                 if ($ret != 0) {
1326                     sleep(2);
1327                 }
1328                 $count++
1329             } while ($ret != 0 && $count < 10);
1330             if ($count == 10) {
1331                 print "SMBD failed to start up in a reasonable time (20sec)\n";
1332                 teardown_env($self, $envvars);
1333                 return 0;
1334             }
1335         }
1336
1337         # Ensure we have domain users mapped.
1338         $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=513 unixgroup=domusers type=domain");
1339         if ($ret != 0) {
1340             return 1;
1341         }
1342         $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=512 unixgroup=domadmins type=domain");
1343         if ($ret != 0) {
1344             return 1;
1345         }
1346
1347         if ($winbindd eq "yes") {
1348             # note: creating builtin groups requires winbindd for the
1349             # unix id allocator
1350             $ret = system("SELFTEST_WINBINDD_SOCKET_DIR=" . $envvars->{SELFTEST_WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} sam createbuiltingroup Users");
1351             if ($ret != 0) {
1352                 print "Failed to create BUILTIN\\Users group\n";
1353                 return 0;
1354             }
1355             my $count = 0;
1356             do {
1357                 system(Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} cache flush");
1358                 $ret = system("SELFTEST_WINBINDD_SOCKET_DIR=" . $envvars->{SELFTEST_WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545");
1359                 if ($ret != 0) {
1360                     sleep(2);
1361                 }
1362                 $count++;
1363             } while ($ret != 0 && $count < 10);
1364             if ($count == 10) {
1365                 print "WINBINDD not reachable after 20 seconds\n";
1366                 teardown_env($self, $envvars);
1367                 return 0;
1368             }
1369         }
1370
1371         print $self->getlog_env($envvars);
1372
1373         return 1;
1374 }
1375
1376 1;