selftest/Samba3: export ENVNAME
[sharpe/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 ";
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{UID_WRAPPER} = "1";
647
648                 $ENV{ENVNAME} = "$ENV{ENVNAME}.nmbd";
649
650                 if ($nmbd ne "yes") {
651                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
652                                 my $signame = shift;
653                                 print("Skip nmbd received signal $signame");
654                                 exit 0;
655                         };
656                         sleep($self->{server_maxtime});
657                         exit 0;
658                 }
659
660                 my @optargs = ("-d0");
661                 if (defined($ENV{NMBD_OPTIONS})) {
662                         @optargs = split(/ /, $ENV{NMBD_OPTIONS});
663                 }
664
665                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "nmbd");
666
667                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
668                 if(defined($ENV{NMBD_VALGRIND})) { 
669                         @preargs = split(/ /, $ENV{NMBD_VALGRIND});
670                 }
671
672                 close($env_vars->{STDIN_PIPE});
673                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
674
675                 exec(@preargs, Samba::bindir_path($self, "nmbd"), "-F", "--no-process-group", "--log-stdout", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start nmbd: $!");
676         }
677         $env_vars->{NMBD_TL_PID} = $pid;
678         write_pid($env_vars, "nmbd", $pid);
679         print "DONE\n";
680
681         unlink($env_vars->{WINBINDD_TEST_LOG});
682         print "STARTING WINBINDD...";
683         $pid = fork();
684         if ($pid == 0) {
685                 open STDOUT, ">$env_vars->{WINBINDD_TEST_LOG}";
686                 open STDERR, '>&STDOUT';
687
688                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
689
690                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
691                 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
692                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
693
694                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
695                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
696                 $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
697
698                 $ENV{UID_WRAPPER} = "1";
699
700                 $ENV{ENVNAME} = "$ENV{ENVNAME}.winbindd";
701
702                 if ($winbindd ne "yes") {
703                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
704                                 my $signame = shift;
705                                 print("Skip winbindd received signal $signame");
706                                 exit 0;
707                         };
708                         sleep($self->{server_maxtime});
709                         exit 0;
710                 }
711
712                 my @optargs = ("-d0");
713                 if (defined($ENV{WINBINDD_OPTIONS})) {
714                         @optargs = split(/ /, $ENV{WINBINDD_OPTIONS});
715                 }
716
717                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "winbindd");
718
719                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
720                 if(defined($ENV{WINBINDD_VALGRIND})) {
721                         @preargs = split(/ /, $ENV{WINBINDD_VALGRIND});
722                 }
723
724                 print "Starting winbindd with config $env_vars->{SERVERCONFFILE}\n";
725
726                 close($env_vars->{STDIN_PIPE});
727                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
728
729                 exec(@preargs, Samba::bindir_path($self, "winbindd"), "-F", "--no-process-group", "--stdout", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start winbindd: $!");
730         }
731         $env_vars->{WINBINDD_TL_PID} = $pid;
732         write_pid($env_vars, "winbindd", $pid);
733         print "DONE\n";
734
735         unlink($env_vars->{SMBD_TEST_LOG});
736         print "STARTING SMBD...";
737         $pid = fork();
738         if ($pid == 0) {
739                 open STDOUT, ">$env_vars->{SMBD_TEST_LOG}";
740                 open STDERR, '>&STDOUT';
741
742                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
743
744                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
745                 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
746                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
747
748                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
749                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
750                 $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
751
752                 $ENV{UID_WRAPPER} = "1";
753
754                 $ENV{ENVNAME} = "$ENV{ENVNAME}.smbd";
755
756                 if ($smbd ne "yes") {
757                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
758                                 my $signame = shift;
759                                 print("Skip smbd received signal $signame");
760                                 exit 0;
761                         };
762                         sleep($self->{server_maxtime});
763                         exit 0;
764                 }
765
766                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "smbd");
767                 my @optargs = ("-d0");
768                 if (defined($ENV{SMBD_OPTIONS})) {
769                         @optargs = split(/ /, $ENV{SMBD_OPTIONS});
770                 }
771                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
772                 if(defined($ENV{SMBD_VALGRIND})) {
773                         @preargs = split(/ /,$ENV{SMBD_VALGRIND});
774                 }
775
776                 close($env_vars->{STDIN_PIPE});
777                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
778
779                 exec(@preargs, Samba::bindir_path($self, "smbd"), "-F", "--no-process-group", "--log-stdout", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start smbd: $!");
780         }
781         $env_vars->{SMBD_TL_PID} = $pid;
782         write_pid($env_vars, "smbd", $pid);
783         print "DONE\n";
784
785         close(STDIN_READER);
786
787         return $self->wait_for_start($env_vars, $nmbd, $winbindd, $smbd);
788 }
789
790 sub provision($$$$$$)
791 {
792         my ($self, $prefix, $server, $password, $extra_options, $no_delete_prefix) = @_;
793
794         ##
795         ## setup the various environment variables we need
796         ##
797
798         my $swiface = Samba::get_interface($server);
799         my %ret = ();
800         my $server_ip = "127.0.0.$swiface";
801         my $domain = "SAMBA-TEST";
802
803         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `PATH=/usr/ucb:$ENV{PATH} whoami`);
804         chomp $unix_name;
805         my $unix_uid = $>;
806         my $unix_gids_str = $);
807         my @unix_gids = split(" ", $unix_gids_str);
808
809         my $prefix_abs = abs_path($prefix);
810         my $bindir_abs = abs_path($self->{bindir});
811
812         my $dns_host_file = "$ENV{SELFTEST_PREFIX}/dns_host_file";
813
814         my @dirs = ();
815
816         my $shrdir="$prefix_abs/share";
817         push(@dirs,$shrdir);
818
819         my $libdir="$prefix_abs/lib";
820         push(@dirs,$libdir);
821
822         my $piddir="$prefix_abs/pid";
823         push(@dirs,$piddir);
824
825         my $privatedir="$prefix_abs/private";
826         push(@dirs,$privatedir);
827
828         my $lockdir="$prefix_abs/lockdir";
829         push(@dirs,$lockdir);
830
831         my $eventlogdir="$prefix_abs/lockdir/eventlog";
832         push(@dirs,$eventlogdir);
833
834         my $logdir="$prefix_abs/logs";
835         push(@dirs,$logdir);
836
837         my $driver32dir="$shrdir/W32X86";
838         push(@dirs,$driver32dir);
839
840         my $driver64dir="$shrdir/x64";
841         push(@dirs,$driver64dir);
842
843         my $driver40dir="$shrdir/WIN40";
844         push(@dirs,$driver40dir);
845
846         my $ro_shrdir="$shrdir/root-tmp";
847         push(@dirs,$ro_shrdir);
848
849         my $msdfs_shrdir="$shrdir/msdfsshare";
850         push(@dirs,$msdfs_shrdir);
851
852         my $msdfs_deeppath="$msdfs_shrdir/deeppath";
853         push(@dirs,$msdfs_deeppath);
854
855         # this gets autocreated by winbindd
856         my $wbsockdir="$prefix_abs/winbindd";
857         my $wbsockprivdir="$lockdir/winbindd_privileged";
858
859         my $nmbdsockdir="$prefix_abs/nmbd";
860         unlink($nmbdsockdir);
861
862         my $fs_specific_conf = $self->get_fs_specific_conf($shrdir);
863
864         ## 
865         ## create the test directory layout
866         ##
867         die ("prefix_abs = ''") if $prefix_abs eq "";
868         die ("prefix_abs = '/'") if $prefix_abs eq "/";
869
870         mkdir($prefix_abs, 0777);
871         print "CREATE TEST ENVIRONMENT IN '$prefix'...";
872         if (not defined($no_delete_prefix) or not $no_delete_prefix) {
873             system("rm -rf $prefix_abs/*");
874         }
875         mkdir($_, 0777) foreach(@dirs);
876
877         ##
878         ## lockdir and piddir must be 0755
879         ##
880         chmod 0755, $lockdir;
881         chmod 0755, $piddir;
882
883
884         ##
885         ## create ro and msdfs share layout
886         ##
887
888         chmod 0755, $ro_shrdir;
889         my $unreadable_file = "$ro_shrdir/unreadable_file";
890         unless (open(UNREADABLE_FILE, ">$unreadable_file")) {
891                 warn("Unable to open $unreadable_file");
892                 return undef;
893         }
894         close(UNREADABLE_FILE);
895         chmod 0600, $unreadable_file;
896
897         my $msdfs_target = "$ro_shrdir/msdfs-target";
898         unless (open(MSDFS_TARGET, ">$msdfs_target")) {
899                 warn("Unable to open $msdfs_target");
900                 return undef;
901         }
902         close(MSDFS_TARGET);
903         chmod 0666, $msdfs_target;
904         symlink "msdfs:$server_ip\\ro-tmp", "$msdfs_shrdir/msdfs-src1";
905         symlink "msdfs:$server_ip\\ro-tmp", "$msdfs_shrdir/deeppath/msdfs-src2";
906
907         my $conffile="$libdir/server.conf";
908
909         my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/lib/nss_wrapper/nss_wrapper.pl";
910         my $nss_wrapper_passwd = "$privatedir/passwd";
911         my $nss_wrapper_group = "$privatedir/group";
912
913         my $mod_printer_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/printing/modprinter.pl";
914
915         my @eventlog_list = ("dns server", "application");
916
917         ##
918         ## calculate uids and gids
919         ##
920
921         my ($max_uid, $max_gid);
922         my ($uid_nobody, $uid_root, $uid_pdbtest);
923         my ($gid_nobody, $gid_nogroup, $gid_root, $gid_domusers, $gid_domadmins);
924
925         if ($unix_uid < 0xffff - 2) {
926                 $max_uid = 0xffff;
927         } else {
928                 $max_uid = $unix_uid;
929         }
930
931         $uid_root = $max_uid - 1;
932         $uid_nobody = $max_uid - 2;
933         $uid_pdbtest = $max_uid - 3;
934
935         if ($unix_gids[0] < 0xffff - 3) {
936                 $max_gid = 0xffff;
937         } else {
938                 $max_gid = $unix_gids[0];
939         }
940
941         $gid_nobody = $max_gid - 1;
942         $gid_nogroup = $max_gid - 2;
943         $gid_root = $max_gid - 3;
944         $gid_domusers = $max_gid - 4;
945         $gid_domadmins = $max_gid - 5;
946
947         ##
948         ## create conffile
949         ##
950
951         unless (open(CONF, ">$conffile")) {
952                 warn("Unable to open $conffile");
953                 return undef;
954         }
955         print CONF "
956 [global]
957         netbios name = $server
958         interfaces = $server_ip/8
959         bind interfaces only = yes
960         panic action = cd $self->{srcdir} && $self->{srcdir}/selftest/gdb_backtrace %d %\$(MAKE_TEST_BINARY)
961         smbd:suicide mode = yes
962
963         workgroup = $domain
964
965         private dir = $privatedir
966         pid directory = $piddir
967         lock directory = $lockdir
968         log file = $logdir/log.\%m
969         log level = 1
970         debug pid = yes
971         max log size = 0
972
973         state directory = $lockdir
974         cache directory = $lockdir
975
976         passdb backend = tdbsam
977
978         time server = yes
979
980         add user script =               $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
981         add group script =              $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action add --name %g
982         add machine script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
983         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
984         delete user script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action delete --name %u
985         delete group script =           $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action delete --name %g
986         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
987
988         addprinter command =            $mod_printer_pl -a -s $conffile --
989         deleteprinter command =         $mod_printer_pl -d -s $conffile --
990
991         eventlog list = application \"dns server\"
992
993         kernel oplocks = no
994         kernel change notify = no
995
996         syslog = no
997         printing = bsd
998         printcap name = /dev/null
999
1000         winbindd socket directory = $wbsockdir
1001         nmbd:socket dir = $nmbdsockdir
1002         idmap config * : range = 100000-200000
1003         winbind enum users = yes
1004         winbind enum groups = yes
1005
1006 #       min receivefile size = 4000
1007
1008         read only = no
1009         server signing = auto
1010
1011         smbd:sharedelay = 100000
1012         smbd:writetimeupdatedelay = 500000
1013         map hidden = no
1014         map system = no
1015         map readonly = no
1016         store dos attributes = yes
1017         create mask = 755
1018         dos filemode = yes
1019         vfs objects = acl_xattr fake_acls xattr_tdb streams_depot
1020
1021         printing = vlp
1022         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1023         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1024         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1025         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1026         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1027         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1028         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1029         lpq cache time = 0
1030         print notify backchannel = yes
1031
1032         ncalrpc dir = $prefix_abs/ncalrpc
1033         resolv:host file = $dns_host_file
1034
1035         # The samba3.blackbox.smbclient_s3 test uses this to test that
1036         # sending messages works, and that the %m sub works.
1037         message command = mv %s $shrdir/message.%m
1038
1039         # Begin extra options
1040         $extra_options
1041         # End extra options
1042
1043         #Include user defined custom parameters if set
1044 ";
1045
1046         if (defined($ENV{INCLUDE_CUSTOM_CONF})) {
1047                 print CONF "\t$ENV{INCLUDE_CUSTOM_CONF}\n";
1048         }
1049
1050         print CONF "
1051 [tmp]
1052         path = $shrdir
1053         comment = smb username is [%U]
1054 [tmpenc]
1055         path = $shrdir
1056         comment = encrypt smb username is [%U]
1057         smb encrypt = required
1058         vfs objects = dirsort
1059 [tmpguest]
1060         path = $shrdir
1061         guest ok = yes
1062 [guestonly]
1063         path = $shrdir
1064         guest only = yes
1065         guest ok = yes
1066 [forceuser]
1067         path = $shrdir
1068         force user = $unix_name
1069         guest ok = yes
1070 [forcegroup]
1071         path = $shrdir
1072         force group = nogroup
1073         guest ok = yes
1074 [ro-tmp]
1075         path = $ro_shrdir
1076         guest ok = yes
1077 [write-list-tmp]
1078         path = $shrdir
1079         read only = yes
1080         write list = $unix_name
1081 [valid-users-tmp]
1082         path = $shrdir
1083         valid users = $unix_name
1084 [msdfs-share]
1085         path = $msdfs_shrdir
1086         msdfs root = yes
1087         guest ok = yes
1088 [hideunread]
1089         copy = tmp
1090         hide unreadable = yes
1091 [tmpcase]
1092         copy = tmp
1093         case sensitive = yes
1094 [hideunwrite]
1095         copy = tmp
1096         hide unwriteable files = yes
1097 [durable]
1098         copy = tmp
1099         kernel share modes = no
1100         kernel oplocks = no
1101         posix locking = no
1102 [fs_specific]
1103         copy = tmp
1104         $fs_specific_conf
1105 [print1]
1106         copy = tmp
1107         printable = yes
1108
1109 [print2]
1110         copy = print1
1111 [print3]
1112         copy = print1
1113         default devmode = no
1114 [lp]
1115         copy = print1
1116
1117 [nfs4acl_simple]
1118         path = $shrdir
1119         comment = smb username is [%U]
1120         nfs4:mode = simple
1121         vfs objects = nfs4acl_xattr xattr_tdb
1122
1123 [nfs4acl_special]
1124         path = $shrdir
1125         comment = smb username is [%U]
1126         nfs4:mode = special
1127         vfs objects = nfs4acl_xattr xattr_tdb
1128
1129 [xcopy_share]
1130         path = $shrdir
1131         comment = smb username is [%U]
1132         create mask = 777
1133         force create mode = 777
1134 [posix_share]
1135         path = $shrdir
1136         comment = smb username is [%U]
1137         create mask = 0777
1138         force create mode = 0
1139         directory mask = 0777
1140         force directory mode = 0
1141         vfs objects = xattr_tdb
1142 [aio]
1143         copy = tmp
1144         aio read size = 1
1145         aio write size = 1
1146
1147 [print\$]
1148         copy = tmp
1149         ";
1150         close(CONF);
1151
1152         ##
1153         ## create a test account
1154         ##
1155
1156         unless (open(PASSWD, ">$nss_wrapper_passwd")) {
1157            warn("Unable to open $nss_wrapper_passwd");
1158            return undef;
1159         } 
1160         print PASSWD "nobody:x:$uid_nobody:$gid_nobody:nobody gecos:$prefix_abs:/bin/false
1161 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
1162 pdbtest:x:$uid_pdbtest:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
1163 ";
1164         if ($unix_uid != 0) {
1165                 print PASSWD "root:x:$uid_root:$gid_root:root gecos:$prefix_abs:/bin/false
1166 ";
1167         }
1168         close(PASSWD);
1169
1170         unless (open(GROUP, ">$nss_wrapper_group")) {
1171              warn("Unable to open $nss_wrapper_group");
1172              return undef;
1173         }
1174         print GROUP "nobody:x:$gid_nobody:
1175 nogroup:x:$gid_nogroup:nobody
1176 $unix_name-group:x:$unix_gids[0]:
1177 domusers:X:$gid_domusers:
1178 domadmins:X:$gid_domadmins:
1179 ";
1180         if ($unix_gids[0] != 0) {
1181                 print GROUP "root:x:$gid_root:
1182 ";
1183         }
1184
1185         close(GROUP);
1186
1187         foreach my $evlog (@eventlog_list) {
1188                 my $evlogtdb = "$eventlogdir/$evlog.tdb";
1189                 open(EVENTLOG, ">$evlogtdb") or die("Unable to open $evlogtdb");
1190                 close(EVENTLOG);
1191         }
1192
1193         $ENV{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1194         $ENV{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1195
1196         my $cmd = Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $unix_name > /dev/null";
1197         unless (open(PWD, "|$cmd")) {
1198              warn("Unable to set password for test account\n$cmd");
1199              return undef;
1200         }
1201         print PWD "$password\n$password\n";
1202         unless (close(PWD)) {
1203              warn("Unable to set password for test account\n$cmd");
1204              return undef; 
1205         }
1206         print "DONE\n";
1207
1208         open(DNS_UPDATE_LIST, ">$prefix/dns_update_list") or die("Unable to open $$prefix/dns_update_list");
1209         print DNS_UPDATE_LIST "A $server. $server_ip";
1210         close(DNS_UPDATE_LIST);
1211
1212         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) {
1213                 die "Unable to update hostname into $dns_host_file";
1214         }
1215
1216         $ret{SERVER_IP} = $server_ip;
1217         $ret{NMBD_TEST_LOG} = "$prefix/nmbd_test.log";
1218         $ret{NMBD_TEST_LOG_POS} = 0;
1219         $ret{WINBINDD_TEST_LOG} = "$prefix/winbindd_test.log";
1220         $ret{WINBINDD_TEST_LOG_POS} = 0;
1221         $ret{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
1222         $ret{SMBD_TEST_LOG_POS} = 0;
1223         $ret{SERVERCONFFILE} = $conffile;
1224         $ret{CONFIGURATION} ="-s $conffile";
1225         $ret{SERVER} = $server;
1226         $ret{USERNAME} = $unix_name;
1227         $ret{USERID} = $unix_uid;
1228         $ret{DOMAIN} = $domain;
1229         $ret{NETBIOSNAME} = $server;
1230         $ret{PASSWORD} = $password;
1231         $ret{PIDDIR} = $piddir;
1232         $ret{WINBINDD_SOCKET_DIR} = $wbsockdir;
1233         $ret{WINBINDD_PRIV_PIPE_DIR} = $wbsockprivdir;
1234         $ret{NMBD_SOCKET_DIR} = $nmbdsockdir;
1235         $ret{SOCKET_WRAPPER_DEFAULT_IFACE} = $swiface;
1236         $ret{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1237         $ret{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1238         $ret{NSS_WRAPPER_WINBIND_SO_PATH} = Samba::nss_wrapper_winbind_so_path($self);
1239         $ret{LOCAL_PATH} = "$shrdir";
1240
1241         return \%ret;
1242 }
1243
1244 sub wait_for_start($$$$$)
1245 {
1246         my ($self, $envvars, $nmbd, $winbindd, $smbd) = @_;
1247         my $ret;
1248
1249         if ($nmbd eq "yes") {
1250             # give time for nbt server to register its names
1251             print "delaying for nbt name registration\n";
1252             sleep(10);
1253             # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init 
1254             my $nmblookup = Samba::bindir_path($self, "nmblookup3");
1255             system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} __SAMBA__");
1256             system("$nmblookup $envvars->{CONFIGURATION} __SAMBA__");
1257             system("$nmblookup $envvars->{CONFIGURATION} -U 127.255.255.255 __SAMBA__");
1258             system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} $envvars->{SERVER}");
1259             system("$nmblookup $envvars->{CONFIGURATION} $envvars->{SERVER}");
1260         }
1261
1262         if ($winbindd eq "yes") {
1263             print "checking for winbindd\n";
1264             my $count = 0;
1265             do {
1266                 $ret = system("WINBINDD_SOCKET_DIR=" . $envvars->{WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " -p");
1267                 if ($ret != 0) {
1268                     sleep(2);
1269                 }
1270                 $count++;
1271             } while ($ret != 0 && $count < 10);
1272             if ($count == 10) {
1273                 print "WINBINDD not reachable after 20 seconds\n";
1274                 teardown_env($self, $envvars);
1275                 return 0;
1276             }
1277         }
1278
1279         if ($smbd eq "yes") {
1280             # make sure smbd is also up set
1281             print "wait for smbd\n";
1282
1283             my $count = 0;
1284             do {
1285                 $ret = system(Samba::bindir_path($self, "smbclient3") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER} -U% -p 139");
1286                 if ($ret != 0) {
1287                     sleep(2);
1288                 }
1289                 $count++
1290             } while ($ret != 0 && $count < 10);
1291             if ($count == 10) {
1292                 print "SMBD failed to start up in a reasonable time (20sec)\n";
1293                 teardown_env($self, $envvars);
1294                 return 0;
1295             }
1296         }
1297
1298         # Ensure we have domain users mapped.
1299         $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=513 unixgroup=domusers type=domain");
1300         if ($ret != 0) {
1301             return 1;
1302         }
1303         $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=512 unixgroup=domadmins type=domain");
1304         if ($ret != 0) {
1305             return 1;
1306         }
1307
1308         if ($winbindd eq "yes") {
1309             # note: creating builtin groups requires winbindd for the
1310             # unix id allocator
1311             $ret = system("WINBINDD_SOCKET_DIR=" . $envvars->{WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} sam createbuiltingroup Users");
1312             if ($ret != 0) {
1313                 print "Failed to create BUILTIN\\Users group\n";
1314                 return 0;
1315             }
1316             my $count = 0;
1317             do {
1318                 system(Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} cache flush");
1319                 $ret = system("WINBINDD_SOCKET_DIR=" . $envvars->{WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545");
1320                 if ($ret != 0) {
1321                     sleep(2);
1322                 }
1323                 $count++;
1324             } while ($ret != 0 && $count < 10);
1325             if ($count == 10) {
1326                 print "WINBINDD not reachable after 20 seconds\n";
1327                 teardown_env($self, $envvars);
1328                 return 0;
1329             }
1330         }
1331
1332         print $self->getlog_env($envvars);
1333
1334         return 1;
1335 }
1336
1337 1;