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