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