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