40b8d29853a63316c2cf907cbf16b4fa5a733a5b
[kai/samba-autobuild/.git] / selftest / target / Samba3.pm
1 #!/usr/bin/perl
2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2005-2007 Jelmer Vernooij <jelmer@samba.org>
4 # Published under the GNU GPL, v3 or later.
5
6 package Samba3;
7
8 use strict;
9 use Cwd qw(abs_path);
10 use FindBin qw($RealBin);
11 use POSIX;
12 use target::Samba;
13
14 sub have_ads($) {
15         my ($self) = @_;
16         my $found_ads = 0;
17         my $smbd_build_options = Samba::bindir_path($self, "smbd") . " -b|";
18         open(IN, $smbd_build_options) or die("Unable to run $smbd_build_options: $!");
19
20         while (<IN>) {
21                 if (/WITH_ADS/) {
22                        $found_ads = 1;
23                 }
24         }
25         close IN;
26
27         # If we were not built with ADS support, pretend we were never even available
28         print "smbd does not have ADS support\n" unless $found_ads;
29         return $found_ads;
30 }
31
32 # return smb.conf parameters applicable to @path, based on the underlying
33 # filesystem type
34 sub get_fs_specific_conf($$)
35 {
36         my ($self, $path) = @_;
37         my $mods = "";
38         my $stat_out = `stat --file-system $path` or return "";
39
40         if ($stat_out =~ m/Type:\s+btrfs/) {
41                 $mods .= "btrfs ";
42         }
43
44         if ($mods) {
45                 return "vfs objects = $mods";
46         }
47
48         return undef;
49 }
50
51 sub new($$) {
52         my ($classname, $bindir, $srcdir, $server_maxtime) = @_;
53         my $self = { vars => {},
54                      bindir => $bindir,
55                      srcdir => $srcdir,
56                      server_maxtime => $server_maxtime
57         };
58         bless $self;
59         return $self;
60 }
61
62 sub teardown_env($$)
63 {
64         my ($self, $envvars) = @_;
65         my $count = 0;
66
67         # This should cause smbd to terminate gracefully
68         close($envvars->{STDIN_PIPE});
69
70         my $smbdpid = $envvars->{SMBD_TL_PID};
71         my $nmbdpid = $envvars->{NMBD_TL_PID};
72         my $winbinddpid = $envvars->{WINBINDD_TL_PID};
73
74         # This should give it time to write out the gcov data
75         until ($count > 20) {
76             my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
77             my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
78             my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
79             if ($smbdchild == -1
80                 && $nmbdchild == -1
81                 && $winbinddchild == -1) {
82                 last;
83             }
84             sleep(1);
85             $count++;
86         }
87
88         if ($count <= 20 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
89             return;
90         }
91
92         $self->stop_sig_term($smbdpid);
93         $self->stop_sig_term($nmbdpid);
94         $self->stop_sig_term($winbinddpid);
95
96         $count = 0;
97         until ($count > 10) {
98             my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
99             my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
100             my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
101             if ($smbdchild == -1
102                 && $nmbdchild == -1
103                 && $winbinddchild == -1) {
104                 last;
105             }
106             sleep(1);
107             $count++;
108         }
109
110         if ($count <= 10 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
111             return;
112         }
113
114         warn("timelimit process did not quit on SIGTERM, sending SIGKILL");
115         $self->stop_sig_kill($smbdpid);
116         $self->stop_sig_kill($nmbdpid);
117         $self->stop_sig_kill($winbinddpid);
118
119         return 0;
120 }
121
122 sub getlog_env_app($$$)
123 {
124         my ($self, $envvars, $name) = @_;
125
126         my $title = "$name LOG of: $envvars->{NETBIOSNAME}\n";
127         my $out = $title;
128
129         open(LOG, "<".$envvars->{$name."_TEST_LOG"});
130
131         seek(LOG, $envvars->{$name."_TEST_LOG_POS"}, SEEK_SET);
132         while (<LOG>) {
133                 $out .= $_;
134         }
135         $envvars->{$name."_TEST_LOG_POS"} = tell(LOG);
136         close(LOG);
137
138         return "" if $out eq $title;
139  
140         return $out;
141 }
142
143 sub getlog_env($$)
144 {
145         my ($self, $envvars) = @_;
146         my $ret = "";
147
148         $ret .= $self->getlog_env_app($envvars, "SMBD");
149         $ret .= $self->getlog_env_app($envvars, "NMBD");
150         $ret .= $self->getlog_env_app($envvars, "WINBINDD");
151
152         return $ret;
153 }
154
155 sub check_env($$)
156 {
157         my ($self, $envvars) = @_;
158
159         my $childpid = waitpid(-1, WNOHANG);
160
161         # TODO ...
162         return 1;
163 }
164
165 sub setup_env($$$)
166 {
167         my ($self, $envname, $path) = @_;
168
169         $ENV{ENVNAME} = $envname;
170
171         if (defined($self->{vars}->{$envname})) {
172                 return $self->{vars}->{$envname};
173         }
174
175         #
176         # Avoid hitting system krb5.conf -
177         # An env that needs Kerberos will reset this to the real
178         # value.
179         #
180         $ENV{KRB5_CONFIG} = "$path/no_krb5.conf";
181
182         if ($envname eq "nt4_dc") {
183                 return $self->setup_nt4_dc("$path/nt4_dc");
184         } elsif ($envname eq "nt4_dc_schannel") {
185                 return $self->setup_nt4_dc_schannel("$path/nt4_dc_schannel");
186         } elsif ($envname eq "simpleserver") {
187                 return $self->setup_simpleserver("$path/simpleserver");
188         } elsif ($envname eq "fileserver") {
189                 return $self->setup_fileserver("$path/fileserver");
190         } elsif ($envname eq "maptoguest") {
191                 return $self->setup_maptoguest("$path/maptoguest");
192         } elsif ($envname eq "ktest") {
193                 return $self->setup_ktest("$path/ktest");
194         } elsif ($envname eq "nt4_member") {
195                 if (not defined($self->{vars}->{nt4_dc})) {
196                         if (not defined($self->setup_nt4_dc("$path/nt4_dc"))) {
197                                 return undef;
198                         }
199                 }
200                 return $self->setup_nt4_member("$path/nt4_member", $self->{vars}->{nt4_dc});
201         } else {
202                 return "UNKNOWN";
203         }
204 }
205
206 sub setup_nt4_dc($$)
207 {
208         my ($self, $path) = @_;
209
210         print "PROVISIONING NT4 DC...";
211
212         my $nt4_dc_options = "
213         domain master = yes
214         domain logons = yes
215         lanman auth = yes
216         raw NTLMv2 auth = yes
217
218         rpc_server:epmapper = external
219         rpc_server:spoolss = external
220         rpc_server:lsarpc = external
221         rpc_server:samr = external
222         rpc_server:netlogon = external
223         rpc_server:register_embedded_np = yes
224         rpc_server:FssagentRpc = external
225
226         rpc_daemon:epmd = fork
227         rpc_daemon:spoolssd = fork
228         rpc_daemon:lsasd = fork
229         rpc_daemon:fssd = fork
230         fss: sequence timeout = 1
231 ";
232
233         my $vars = $self->provision($path,
234                                     "LOCALNT4DC2",
235                                     "localntdc2pass",
236                                     $nt4_dc_options);
237
238         $vars or return undef;
239
240         if (not $self->check_or_start($vars, "yes", "yes", "yes")) {
241                return undef;
242         }
243
244         $vars->{DC_SERVER} = $vars->{SERVER};
245         $vars->{DC_SERVER_IP} = $vars->{SERVER_IP};
246         $vars->{DC_SERVER_IPV6} = $vars->{SERVER_IPV6};
247         $vars->{DC_NETBIOSNAME} = $vars->{NETBIOSNAME};
248         $vars->{DC_USERNAME} = $vars->{USERNAME};
249         $vars->{DC_PASSWORD} = $vars->{PASSWORD};
250
251         $self->{vars}->{nt4_dc} = $vars;
252
253         return $vars;
254 }
255
256 sub setup_nt4_dc_schannel($$)
257 {
258         my ($self, $path) = @_;
259
260         print "PROVISIONING NT4 DC WITH SERVER SCHANNEL ...";
261
262         my $pdc_options = "
263         domain master = yes
264         domain logons = yes
265         lanman auth = yes
266
267         rpc_server:epmapper = external
268         rpc_server:spoolss = external
269         rpc_server:lsarpc = external
270         rpc_server:samr = external
271         rpc_server:netlogon = external
272         rpc_server:register_embedded_np = yes
273
274         rpc_daemon:epmd = fork
275         rpc_daemon:spoolssd = fork
276         rpc_daemon:lsasd = fork
277
278         server schannel = yes
279 ";
280
281         my $vars = $self->provision($path,
282                                     "LOCALNT4DC9",
283                                     "localntdc9pass",
284                                     $pdc_options);
285
286         $vars or return undef;
287
288         if (not $self->check_or_start($vars, "yes", "yes", "yes")) {
289                return undef;
290         }
291
292         $vars->{DC_SERVER} = $vars->{SERVER};
293         $vars->{DC_SERVER_IP} = $vars->{SERVER_IP};
294         $vars->{DC_SERVER_IPV6} = $vars->{SERVER_IPV6};
295         $vars->{DC_NETBIOSNAME} = $vars->{NETBIOSNAME};
296         $vars->{DC_USERNAME} = $vars->{USERNAME};
297         $vars->{DC_PASSWORD} = $vars->{PASSWORD};
298
299         $self->{vars}->{nt4_dc_schannel} = $vars;
300
301         return $vars;
302 }
303
304 sub setup_nt4_member($$$)
305 {
306         my ($self, $prefix, $nt4_dc_vars) = @_;
307         my $count = 0;
308         my $rc;
309
310         print "PROVISIONING MEMBER...";
311
312         my $require_mutexes = "dbwrap_tdb_require_mutexes:* = yes";
313         $require_mutexes = "" if ($ENV{SELFTEST_DONT_REQUIRE_TDB_MUTEX_SUPPORT} eq "1");
314
315         my $member_options = "
316         security = domain
317         server signing = on
318         dbwrap_tdb_mutexes:* = yes
319         ${require_mutexes}
320 ";
321         my $ret = $self->provision($prefix,
322                                    "LOCALNT4MEMBER3",
323                                    "localnt4member3pass",
324                                    $member_options);
325
326         $ret or return undef;
327
328         my $nmblookup = Samba::bindir_path($self, "nmblookup");
329         do {
330                 print "Waiting for the LOGON SERVER registration ...\n";
331                 $rc = system("$nmblookup $ret->{CONFIGURATION} $ret->{DOMAIN}\#1c");
332                 if ($rc != 0) {
333                         sleep(1);
334                 }
335                 $count++;
336         } while ($rc != 0 && $count < 10);
337         if ($count == 10) {
338                 print "NMBD not reachable after 10 retries\n";
339                 teardown_env($self, $ret);
340                 return 0;
341         }
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 .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
347         $cmd .= "$net join $ret->{CONFIGURATION} $nt4_dc_vars->{DOMAIN} member";
348         $cmd .= " -U$nt4_dc_vars->{USERNAME}\%$nt4_dc_vars->{PASSWORD}";
349
350         if (system($cmd) != 0) {
351             warn("Join failed\n$cmd");
352             return undef;
353         }
354
355         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
356                return undef;
357         }
358
359         $ret->{DC_SERVER} = $nt4_dc_vars->{SERVER};
360         $ret->{DC_SERVER_IP} = $nt4_dc_vars->{SERVER_IP};
361         $ret->{DC_SERVER_IPV6} = $nt4_dc_vars->{SERVER_IPV6};
362         $ret->{DC_NETBIOSNAME} = $nt4_dc_vars->{NETBIOSNAME};
363         $ret->{DC_USERNAME} = $nt4_dc_vars->{USERNAME};
364         $ret->{DC_PASSWORD} = $nt4_dc_vars->{PASSWORD};
365
366         return $ret;
367 }
368
369 sub setup_admember($$$$)
370 {
371         my ($self, $prefix, $dcvars) = @_;
372
373         # If we didn't build with ADS, pretend this env was never available
374         if (not $self->have_ads()) {
375                 return "UNKNOWN";
376         }
377
378         print "PROVISIONING S3 AD MEMBER...";
379
380         my $member_options = "
381         security = ads
382         server signing = on
383         workgroup = $dcvars->{DOMAIN}
384         realm = $dcvars->{REALM}
385 ";
386
387         my $ret = $self->provision($prefix,
388                                    "LOCALADMEMBER",
389                                    "loCalMemberPass",
390                                    $member_options,
391                                    $dcvars->{SERVER_IP},
392                                    $dcvars->{SERVER_IPV6});
393
394         $ret or return undef;
395
396         close(USERMAP);
397         $ret->{DOMAIN} = $dcvars->{DOMAIN};
398         $ret->{REALM} = $dcvars->{REALM};
399
400         my $ctx;
401         my $prefix_abs = abs_path($prefix);
402         $ctx = {};
403         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
404         $ctx->{domain} = $dcvars->{DOMAIN};
405         $ctx->{realm} = $dcvars->{REALM};
406         $ctx->{dnsname} = lc($dcvars->{REALM});
407         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
408         $ctx->{kdc_ipv6} = $dcvars->{SERVER_IPV6};
409         Samba::mk_krb5_conf($ctx, "");
410
411         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
412
413         my $net = Samba::bindir_path($self, "net");
414         my $cmd = "";
415         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
416         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
417                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
418         } else {
419                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
420         }
421         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
422         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
423         $cmd .= "$net join $ret->{CONFIGURATION}";
424         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
425
426         if (system($cmd) != 0) {
427             warn("Join failed\n$cmd");
428             return undef;
429         }
430
431         # We need world access to this share, as otherwise the domain
432         # administrator from the AD domain provided by Samba4 can't
433         # access the share for tests.
434         chmod 0777, "$prefix/share";
435
436         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
437                 return undef;
438         }
439
440         $ret->{DC_SERVER} = $dcvars->{SERVER};
441         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
442         $ret->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
443         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
444         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
445         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
446
447         # Special case, this is called from Samba4.pm but needs to use the Samba3 check_env and get_log_env
448         $ret->{target} = $self;
449
450         return $ret;
451 }
452
453 sub setup_admember_rfc2307($$$$)
454 {
455         my ($self, $prefix, $dcvars) = @_;
456
457         # If we didn't build with ADS, pretend this env was never available
458         if (not $self->have_ads()) {
459                 return "UNKNOWN";
460         }
461
462         print "PROVISIONING S3 AD MEMBER WITH idmap_rfc2307 config...";
463
464         my $member_options = "
465         security = ads
466         server signing = on
467         workgroup = $dcvars->{DOMAIN}
468         realm = $dcvars->{REALM}
469         idmap config $dcvars->{DOMAIN} : backend = rfc2307
470         idmap config $dcvars->{DOMAIN} : range = 2000000-2999999
471         idmap config $dcvars->{DOMAIN} : ldap_server = ad
472         idmap config $dcvars->{DOMAIN} : bind_path_user = ou=idmap,dc=samba,dc=example,dc=com
473         idmap config $dcvars->{DOMAIN} : bind_path_group = ou=idmap,dc=samba,dc=example,dc=com
474 ";
475
476         my $ret = $self->provision($prefix,
477                                    "RFC2307MEMBER",
478                                    "loCalMemberPass",
479                                    $member_options,
480                                    $dcvars->{SERVER_IP},
481                                    $dcvars->{SERVER_IPV6});
482
483         $ret or return undef;
484
485         close(USERMAP);
486         $ret->{DOMAIN} = $dcvars->{DOMAIN};
487         $ret->{REALM} = $dcvars->{REALM};
488
489         my $ctx;
490         my $prefix_abs = abs_path($prefix);
491         $ctx = {};
492         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
493         $ctx->{domain} = $dcvars->{DOMAIN};
494         $ctx->{realm} = $dcvars->{REALM};
495         $ctx->{dnsname} = lc($dcvars->{REALM});
496         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
497         $ctx->{kdc_ipv6} = $dcvars->{SERVER_IPV6};
498         Samba::mk_krb5_conf($ctx, "");
499
500         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
501
502         my $net = Samba::bindir_path($self, "net");
503         my $cmd = "";
504         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
505         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
506                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
507         } else {
508                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
509         }
510         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
511         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
512         $cmd .= "$net join $ret->{CONFIGURATION}";
513         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
514
515         if (system($cmd) != 0) {
516             warn("Join failed\n$cmd");
517             return undef;
518         }
519
520         # We need world access to this share, as otherwise the domain
521         # administrator from the AD domain provided by Samba4 can't
522         # access the share for tests.
523         chmod 0777, "$prefix/share";
524
525         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
526                 return undef;
527         }
528
529         $ret->{DC_SERVER} = $dcvars->{SERVER};
530         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
531         $ret->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
532         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
533         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
534         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
535
536         # Special case, this is called from Samba4.pm but needs to use the Samba3 check_env and get_log_env
537         $ret->{target} = $self;
538
539         return $ret;
540 }
541
542 sub setup_simpleserver($$)
543 {
544         my ($self, $path) = @_;
545
546         print "PROVISIONING simple server...";
547
548         my $prefix_abs = abs_path($path);
549
550         my $simpleserver_options = "
551         lanman auth = yes
552         vfs objects = xattr_tdb streams_depot time_audit full_audit
553         change notify = no
554
555 [vfs_aio_fork]
556         path = $prefix_abs/share
557         vfs objects = aio_fork
558         read only = no
559         vfs_aio_fork:erratic_testing_mode=yes
560 ";
561
562         my $vars = $self->provision($path,
563                                     "LOCALSHARE4",
564                                     "local4pass",
565                                     $simpleserver_options);
566
567         $vars or return undef;
568
569         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
570                return undef;
571         }
572
573         $self->{vars}->{simpleserver} = $vars;
574
575         return $vars;
576 }
577
578 sub setup_fileserver($$)
579 {
580         my ($self, $path) = @_;
581         my $prefix_abs = abs_path($path);
582         my $srcdir_abs = abs_path($self->{srcdir});
583
584         print "PROVISIONING file server ...\n";
585
586         my @dirs = ();
587
588         mkdir($prefix_abs, 0777);
589
590         my $share_dir="$prefix_abs/share";
591
592         # Create share directory structure
593         my $lower_case_share_dir="$share_dir/lower-case";
594         push(@dirs, $lower_case_share_dir);
595
596         my $lower_case_share_dir_30000="$share_dir/lower-case-30000";
597         push(@dirs, $lower_case_share_dir_30000);
598
599         my $dfree_share_dir="$share_dir/dfree";
600         push(@dirs, $dfree_share_dir);
601         push(@dirs, "$dfree_share_dir/subdir1");
602         push(@dirs, "$dfree_share_dir/subdir2");
603
604         my $valid_users_sharedir="$share_dir/valid_users";
605         push(@dirs,$valid_users_sharedir);
606
607         my $offline_sharedir="$share_dir/offline";
608         push(@dirs,$offline_sharedir);
609
610         my $force_user_valid_users_dir = "$share_dir/force_user_valid_users";
611         push(@dirs, $force_user_valid_users_dir);
612
613         my $smbget_sharedir="$share_dir/smbget";
614         push(@dirs,$smbget_sharedir);
615
616         my $fileserver_options = "
617 [lowercase]
618         path = $lower_case_share_dir
619         comment = smb username is [%U]
620         case sensitive = True
621         default case = lower
622         preserve case = no
623         short preserve case = no
624 [lowercase-30000]
625         path = $lower_case_share_dir_30000
626         comment = smb username is [%U]
627         case sensitive = True
628         default case = lower
629         preserve case = no
630         short preserve case = no
631 [dfree]
632         path = $dfree_share_dir
633         comment = smb username is [%U]
634         dfree command = $srcdir_abs/testprogs/blackbox/dfree.sh
635 [valid-users-access]
636         path = $valid_users_sharedir
637         valid users = +userdup
638 [offline]
639         path = $offline_sharedir
640         vfs objects = offline
641
642 # BUG: https://bugzilla.samba.org/show_bug.cgi?id=9878
643 # RH BUG: https://bugzilla.redhat.com/show_bug.cgi?id=1077651
644 [force_user_valid_users]
645         path = $force_user_valid_users_dir
646         comment = force user with valid users combination test share
647         valid users = +force_user
648         force user = force_user
649         force group = everyone
650         write list = force_user
651
652 [smbget]
653         path = $smbget_sharedir
654         comment = smb username is [%U]
655         guest ok = yes
656 [ign_sysacls]
657         path = $share_dir
658         comment = ignore system acls
659         acl_xattr:ignore system acls = yes
660 ";
661
662         my $vars = $self->provision($path,
663                                     "FILESERVER",
664                                     "fileserver",
665                                     $fileserver_options,
666                                     undef,
667                                     undef,
668                                     1);
669
670         $vars or return undef;
671
672         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
673                return undef;
674         }
675
676         $self->{vars}->{fileserver} = $vars;
677
678         mkdir($_, 0777) foreach(@dirs);
679
680         ## Create case sensitive lower case share dir
681         foreach my $file ('a'..'z') {
682                 my $full_path = $lower_case_share_dir . '/' . $file;
683                 open my $fh, '>', $full_path;
684                 # Add some content to file
685                 print $fh $full_path;
686                 close $fh;
687         }
688
689         for (my $file = 1; $file < 51; ++$file) {
690                 my $full_path = $lower_case_share_dir . '/' . $file;
691                 open my $fh, '>', $full_path;
692                 # Add some content to file
693                 print $fh $full_path;
694                 close $fh;
695         }
696
697         # Create content for 30000 share
698         foreach my $file ('a'..'z') {
699                 my $full_path = $lower_case_share_dir_30000 . '/' . $file;
700                 open my $fh, '>', $full_path;
701                 # Add some content to file
702                 print $fh $full_path;
703                 close $fh;
704         }
705
706         for (my $file = 1; $file < 30001; ++$file) {
707                 my $full_path = $lower_case_share_dir_30000 . '/' . $file;
708                 open my $fh, '>', $full_path;
709                 # Add some content to file
710                 print $fh $full_path;
711                 close $fh;
712         }
713
714         ##
715         ## create a listable file in valid_users_share
716         ##
717         my $valid_users_target = "$valid_users_sharedir/foo";
718         unless (open(VALID_USERS_TARGET, ">$valid_users_target")) {
719                 warn("Unable to open $valid_users_target");
720                 return undef;
721         }
722         close(VALID_USERS_TARGET);
723         chmod 0644, $valid_users_target;
724
725         return $vars;
726 }
727
728 sub setup_ktest($$$)
729 {
730         my ($self, $prefix) = @_;
731
732         # If we didn't build with ADS, pretend this env was never available
733         if (not $self->have_ads()) {
734                 return "UNKNOWN";
735         }
736
737         print "PROVISIONING server with security=ads...";
738
739         my $ktest_options = "
740         workgroup = KTEST
741         realm = ktest.samba.example.com
742         security = ads
743         username map = $prefix/lib/username.map
744         server signing = required
745 ";
746
747         my $ret = $self->provision($prefix,
748                                    "LOCALKTEST6",
749                                    "localktest6pass",
750                                    $ktest_options);
751
752         $ret or return undef;
753
754         my $ctx;
755         my $prefix_abs = abs_path($prefix);
756         $ctx = {};
757         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
758         $ctx->{domain} = "KTEST";
759         $ctx->{realm} = "KTEST.SAMBA.EXAMPLE.COM";
760         $ctx->{dnsname} = lc($ctx->{realm});
761         $ctx->{kdc_ipv4} = "0.0.0.0";
762         $ctx->{kdc_ipv6} = "::";
763         Samba::mk_krb5_conf($ctx, "");
764
765         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
766
767         open(USERMAP, ">$prefix/lib/username.map") or die("Unable to open $prefix/lib/username.map");
768         print USERMAP "
769 $ret->{USERNAME} = KTEST\\Administrator
770 ";
771         close(USERMAP);
772
773 #This is the secrets.tdb created by 'net ads join' from Samba3 to a
774 #Samba4 DC with the same parameters as are being used here.  The
775 #domain SID is S-1-5-21-1071277805-689288055-3486227160
776
777         system("cp $self->{srcdir}/source3/selftest/ktest-secrets.tdb $prefix/private/secrets.tdb");
778         chmod 0600, "$prefix/private/secrets.tdb";
779
780 #Make sure there's no old ntdb file.
781         system("rm -f $prefix/private/secrets.ntdb");
782
783 #This uses a pre-calculated krb5 credentials cache, obtained by running Samba4 with:
784 # "--option=kdc:service ticket lifetime=239232" "--option=kdc:user ticket lifetime=239232" "--option=kdc:renewal lifetime=239232"
785 #
786 #and having in krb5.conf:
787 # ticket_lifetime = 799718400
788 # renew_lifetime = 799718400
789 #
790 # The commands for the -2 keytab where were:
791 # kinit administrator@KTEST.SAMBA.EXAMPLE.COM
792 # kvno host/localktest6@KTEST.SAMBA.EXAMPLE.COM
793 # kvno cifs/localktest6@KTEST.SAMBA.EXAMPLE.COM
794 # kvno host/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
795 # kvno cifs/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
796 #
797 # and then for the -3 keytab, I did
798 #
799 # net changetrustpw; kdestroy and the same again.
800 #
801 # This creates a credential cache with a very long lifetime (2036 at
802 # at 2011-04), and shows that running 'net changetrustpw' does not
803 # break existing logins (for the secrets.tdb method at least).
804 #
805
806         $ret->{KRB5_CCACHE}="FILE:$prefix/krb5_ccache";
807
808         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-2 $prefix/krb5_ccache-2");
809         chmod 0600, "$prefix/krb5_ccache-2";
810
811         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-3 $prefix/krb5_ccache-3");
812         chmod 0600, "$prefix/krb5_ccache-3";
813
814         # We need world access to this share, as otherwise the domain
815         # administrator from the AD domain provided by ktest can't
816         # access the share for tests.
817         chmod 0777, "$prefix/share";
818
819         if (not $self->check_or_start($ret, "yes", "no", "yes")) {
820                return undef;
821         }
822         return $ret;
823 }
824
825 sub setup_maptoguest($$)
826 {
827         my ($self, $path) = @_;
828
829         print "PROVISIONING maptoguest...";
830
831         my $options = "
832 map to guest = bad user
833 ";
834
835         my $vars = $self->provision($path,
836                                     "maptoguest",
837                                     "maptoguestpass",
838                                     $options);
839
840         $vars or return undef;
841
842         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
843                return undef;
844         }
845
846         $self->{vars}->{s3maptoguest} = $vars;
847
848         return $vars;
849 }
850
851 sub stop_sig_term($$) {
852         my ($self, $pid) = @_;
853         kill("USR1", $pid) or kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
854 }
855
856 sub stop_sig_kill($$) {
857         my ($self, $pid) = @_;
858         kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
859 }
860
861 sub write_pid($$$)
862 {
863         my ($env_vars, $app, $pid) = @_;
864
865         open(PID, ">$env_vars->{PIDDIR}/timelimit.$app.pid");
866         print PID $pid;
867         close(PID);
868 }
869
870 sub read_pid($$)
871 {
872         my ($env_vars, $app) = @_;
873
874         open(PID, "<$env_vars->{PIDDIR}/timelimit.$app.pid");
875         my $pid = <PID>;
876         close(PID);
877         return $pid;
878 }
879
880 sub check_or_start($$$$$) {
881         my ($self, $env_vars, $nmbd, $winbindd, $smbd) = @_;
882
883         # use a pipe for stdin in the child processes. This allows
884         # those processes to monitor the pipe for EOF to ensure they
885         # exit when the test script exits
886         pipe(STDIN_READER, $env_vars->{STDIN_PIPE});
887
888         unlink($env_vars->{NMBD_TEST_LOG});
889         print "STARTING NMBD...";
890         my $pid = fork();
891         if ($pid == 0) {
892                 open STDOUT, ">$env_vars->{NMBD_TEST_LOG}";
893                 open STDERR, '>&STDOUT';
894
895                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
896
897                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
898                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
899                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
900
901                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
902                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
903                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
904                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
905                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
906                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
907                 $ENV{UID_WRAPPER_ROOT} = "1";
908
909                 $ENV{ENVNAME} = "$ENV{ENVNAME}.nmbd";
910
911                 if ($nmbd ne "yes") {
912                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
913                                 my $signame = shift;
914                                 print("Skip nmbd received signal $signame");
915                                 exit 0;
916                         };
917                         sleep($self->{server_maxtime});
918                         exit 0;
919                 }
920
921                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "nmbd");
922                 my @optargs = ("-d0");
923                 if (defined($ENV{NMBD_OPTIONS})) {
924                         @optargs = split(/ /, $ENV{NMBD_OPTIONS});
925                 }
926                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
927                 if(defined($ENV{NMBD_VALGRIND})) { 
928                         @preargs = split(/ /, $ENV{NMBD_VALGRIND});
929                 }
930                 my @args = ("-F", "--no-process-group",
931                             "-s", $env_vars->{SERVERCONFFILE},
932                             "-l", $env_vars->{LOGDIR});
933                 if (not defined($ENV{NMBD_DONT_LOG_STDOUT})) {
934                         push(@args, "--log-stdout");
935                 }
936
937                 close($env_vars->{STDIN_PIPE});
938                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
939
940                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
941                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
942         }
943         $env_vars->{NMBD_TL_PID} = $pid;
944         write_pid($env_vars, "nmbd", $pid);
945         print "DONE\n";
946
947         unlink($env_vars->{WINBINDD_TEST_LOG});
948         print "STARTING WINBINDD...";
949         $pid = fork();
950         if ($pid == 0) {
951                 open STDOUT, ">$env_vars->{WINBINDD_TEST_LOG}";
952                 open STDERR, '>&STDOUT';
953
954                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
955
956                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
957                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
958                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
959
960                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
961                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
962                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
963                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
964                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
965                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
966                 if (defined($env_vars->{RESOLV_WRAPPER_CONF})) {
967                         $ENV{RESOLV_WRAPPER_CONF} = $env_vars->{RESOLV_WRAPPER_CONF};
968                 } else {
969                         $ENV{RESOLV_WRAPPER_HOSTS} = $env_vars->{RESOLV_WRAPPER_HOSTS};
970                 }
971                 $ENV{UID_WRAPPER_ROOT} = "1";
972
973                 $ENV{ENVNAME} = "$ENV{ENVNAME}.winbindd";
974
975                 if ($winbindd ne "yes") {
976                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
977                                 my $signame = shift;
978                                 print("Skip winbindd received signal $signame");
979                                 exit 0;
980                         };
981                         sleep($self->{server_maxtime});
982                         exit 0;
983                 }
984
985                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "winbindd");
986                 my @optargs = ("-d0");
987                 if (defined($ENV{WINBINDD_OPTIONS})) {
988                         @optargs = split(/ /, $ENV{WINBINDD_OPTIONS});
989                 }
990                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
991                 if(defined($ENV{WINBINDD_VALGRIND})) {
992                         @preargs = split(/ /, $ENV{WINBINDD_VALGRIND});
993                 }
994                 my @args = ("-F", "--no-process-group",
995                             "-s", $env_vars->{SERVERCONFFILE},
996                             "-l", $env_vars->{LOGDIR});
997                 if (not defined($ENV{WINBINDD_DONT_LOG_STDOUT})) {
998                         push(@args, "--stdout");
999                 }
1000
1001                 close($env_vars->{STDIN_PIPE});
1002                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1003
1004                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1005                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1006         }
1007         $env_vars->{WINBINDD_TL_PID} = $pid;
1008         write_pid($env_vars, "winbindd", $pid);
1009         print "DONE\n";
1010
1011         unlink($env_vars->{SMBD_TEST_LOG});
1012         print "STARTING SMBD...";
1013         $pid = fork();
1014         if ($pid == 0) {
1015                 open STDOUT, ">$env_vars->{SMBD_TEST_LOG}";
1016                 open STDERR, '>&STDOUT';
1017
1018                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
1019
1020                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
1021                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
1022                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
1023
1024                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
1025                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
1026                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
1027                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
1028                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
1029                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
1030                 if (defined($env_vars->{RESOLV_WRAPPER_CONF})) {
1031                         $ENV{RESOLV_WRAPPER_CONF} = $env_vars->{RESOLV_WRAPPER_CONF};
1032                 } else {
1033                         $ENV{RESOLV_WRAPPER_HOSTS} = $env_vars->{RESOLV_WRAPPER_HOSTS};
1034                 }
1035                 $ENV{UID_WRAPPER_ROOT} = "1";
1036
1037                 $ENV{ENVNAME} = "$ENV{ENVNAME}.smbd";
1038
1039                 if ($smbd ne "yes") {
1040                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
1041                                 my $signame = shift;
1042                                 print("Skip smbd received signal $signame");
1043                                 exit 0;
1044                         };
1045                         sleep($self->{server_maxtime});
1046                         exit 0;
1047                 }
1048
1049                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "smbd");
1050                 my @optargs = ("-d0");
1051                 if (defined($ENV{SMBD_OPTIONS})) {
1052                         @optargs = split(/ /, $ENV{SMBD_OPTIONS});
1053                 }
1054                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
1055                 if(defined($ENV{SMBD_VALGRIND})) {
1056                         @preargs = split(/ /,$ENV{SMBD_VALGRIND});
1057                 }
1058                 my @args = ("-F", "--no-process-group",
1059                             "-s", $env_vars->{SERVERCONFFILE},
1060                             "-l", $env_vars->{LOGDIR});
1061                 if (not defined($ENV{SMBD_DONT_LOG_STDOUT})) {
1062                         push(@args, "--log-stdout");
1063                 }
1064
1065                 close($env_vars->{STDIN_PIPE});
1066                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1067
1068                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1069                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1070         }
1071         $env_vars->{SMBD_TL_PID} = $pid;
1072         write_pid($env_vars, "smbd", $pid);
1073         print "DONE\n";
1074
1075         close(STDIN_READER);
1076
1077         return $self->wait_for_start($env_vars, $nmbd, $winbindd, $smbd);
1078 }
1079
1080 sub createuser($$$$)
1081 {
1082         my ($self, $username, $password, $conffile) = @_;
1083         my $cmd = "UID_WRAPPER_ROOT=1 " . Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $username > /dev/null";
1084         unless (open(PWD, "|$cmd")) {
1085             warn("Unable to set password for $username account\n$cmd");
1086             return undef;
1087         }
1088         print PWD "$password\n$password\n";
1089         unless (close(PWD)) {
1090             warn("Unable to set password for $username account\n$cmd");
1091             return undef;
1092         }
1093         print "DONE\n";
1094 }
1095
1096 sub provision($$$$$$$$)
1097 {
1098         my ($self, $prefix, $server, $password, $extra_options, $dc_server_ip, $dc_server_ipv6, $no_delete_prefix) = @_;
1099
1100         ##
1101         ## setup the various environment variables we need
1102         ##
1103
1104         my $swiface = Samba::get_interface($server);
1105         my %ret = ();
1106         my $server_ip = "127.0.0.$swiface";
1107         my $server_ipv6 = sprintf("fd00:0000:0000:0000:0000:0000:5357:5f%02x", $swiface);
1108         my $domain = "SAMBA-TEST";
1109
1110         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `PATH=/usr/ucb:$ENV{PATH} whoami`);
1111         chomp $unix_name;
1112         my $unix_uid = $>;
1113         my $unix_gids_str = $);
1114         my @unix_gids = split(" ", $unix_gids_str);
1115
1116         my $prefix_abs = abs_path($prefix);
1117         my $bindir_abs = abs_path($self->{bindir});
1118
1119         my @dirs = ();
1120
1121         my $shrdir="$prefix_abs/share";
1122         push(@dirs,$shrdir);
1123
1124         my $libdir="$prefix_abs/lib";
1125         push(@dirs,$libdir);
1126
1127         my $piddir="$prefix_abs/pid";
1128         push(@dirs,$piddir);
1129
1130         my $privatedir="$prefix_abs/private";
1131         push(@dirs,$privatedir);
1132
1133         my $lockdir="$prefix_abs/lockdir";
1134         push(@dirs,$lockdir);
1135
1136         my $eventlogdir="$prefix_abs/lockdir/eventlog";
1137         push(@dirs,$eventlogdir);
1138
1139         my $logdir="$prefix_abs/logs";
1140         push(@dirs,$logdir);
1141
1142         my $driver32dir="$shrdir/W32X86";
1143         push(@dirs,$driver32dir);
1144
1145         my $driver64dir="$shrdir/x64";
1146         push(@dirs,$driver64dir);
1147
1148         my $driver40dir="$shrdir/WIN40";
1149         push(@dirs,$driver40dir);
1150
1151         my $ro_shrdir="$shrdir/root-tmp";
1152         push(@dirs,$ro_shrdir);
1153
1154         my $msdfs_shrdir="$shrdir/msdfsshare";
1155         push(@dirs,$msdfs_shrdir);
1156
1157         my $msdfs_deeppath="$msdfs_shrdir/deeppath";
1158         push(@dirs,$msdfs_deeppath);
1159
1160         my $badnames_shrdir="$shrdir/badnames";
1161         push(@dirs,$badnames_shrdir);
1162
1163         my $lease1_shrdir="$shrdir/SMB2_10";
1164         push(@dirs,$lease1_shrdir);
1165
1166         my $lease2_shrdir="$shrdir/SMB3_00";
1167         push(@dirs,$lease2_shrdir);
1168
1169         my $manglenames_shrdir="$shrdir/manglenames";
1170         push(@dirs,$manglenames_shrdir);
1171
1172         my $widelinks_shrdir="$shrdir/widelinks";
1173         push(@dirs,$widelinks_shrdir);
1174
1175         my $widelinks_linkdir="$shrdir/widelinks_foo";
1176         push(@dirs,$widelinks_linkdir);
1177
1178         my $shadow_tstdir="$shrdir/shadow";
1179         push(@dirs,$shadow_tstdir);
1180         my $shadow_mntdir="$shadow_tstdir/mount";
1181         push(@dirs,$shadow_mntdir);
1182         my $shadow_basedir="$shadow_mntdir/base";
1183         push(@dirs,$shadow_basedir);
1184         my $shadow_shrdir="$shadow_basedir/share";
1185         push(@dirs,$shadow_shrdir);
1186
1187         # this gets autocreated by winbindd
1188         my $wbsockdir="$prefix_abs/winbindd";
1189         my $wbsockprivdir="$lockdir/winbindd_privileged";
1190
1191         my $nmbdsockdir="$prefix_abs/nmbd";
1192         unlink($nmbdsockdir);
1193
1194         ## 
1195         ## create the test directory layout
1196         ##
1197         die ("prefix_abs = ''") if $prefix_abs eq "";
1198         die ("prefix_abs = '/'") if $prefix_abs eq "/";
1199
1200         mkdir($prefix_abs, 0777);
1201         print "CREATE TEST ENVIRONMENT IN '$prefix'...";
1202         if (not defined($no_delete_prefix) or not $no_delete_prefix) {
1203             system("rm -rf $prefix_abs/*");
1204         }
1205         mkdir($_, 0777) foreach(@dirs);
1206
1207         my $fs_specific_conf = $self->get_fs_specific_conf($shrdir);
1208
1209         ##
1210         ## lockdir and piddir must be 0755
1211         ##
1212         chmod 0755, $lockdir;
1213         chmod 0755, $piddir;
1214
1215
1216         ##
1217         ## create ro and msdfs share layout
1218         ##
1219
1220         chmod 0755, $ro_shrdir;
1221         my $unreadable_file = "$ro_shrdir/unreadable_file";
1222         unless (open(UNREADABLE_FILE, ">$unreadable_file")) {
1223                 warn("Unable to open $unreadable_file");
1224                 return undef;
1225         }
1226         close(UNREADABLE_FILE);
1227         chmod 0600, $unreadable_file;
1228
1229         my $msdfs_target = "$ro_shrdir/msdfs-target";
1230         unless (open(MSDFS_TARGET, ">$msdfs_target")) {
1231                 warn("Unable to open $msdfs_target");
1232                 return undef;
1233         }
1234         close(MSDFS_TARGET);
1235         chmod 0666, $msdfs_target;
1236         symlink "msdfs:$server_ip\\ro-tmp,$server_ipv6\\ro-tmp",
1237                 "$msdfs_shrdir/msdfs-src1";
1238         symlink "msdfs:$server_ipv6\\ro-tmp", "$msdfs_shrdir/deeppath/msdfs-src2";
1239
1240         ##
1241         ## create bad names in $badnames_shrdir
1242         ##
1243         ## (An invalid name, would be mangled to 8.3).
1244         my $badname_target = "$badnames_shrdir/\340|\231\216\377\177";
1245         unless (open(BADNAME_TARGET, ">$badname_target")) {
1246                 warn("Unable to open $badname_target");
1247                 return undef;
1248         }
1249         close(BADNAME_TARGET);
1250         chmod 0666, $badname_target;
1251
1252         ## (A bad name, would not be mangled to 8.3).
1253         my $badname_target = "$badnames_shrdir/\240\276\346\327\377\177";
1254         unless (open(BADNAME_TARGET, ">$badname_target")) {
1255                 warn("Unable to open $badname_target");
1256                 return undef;
1257         }
1258         close(BADNAME_TARGET);
1259         chmod 0666, $badname_target;
1260
1261         ## (A bad good name).
1262         my $badname_target = "$badnames_shrdir/blank.txt";
1263         unless (open(BADNAME_TARGET, ">$badname_target")) {
1264                 warn("Unable to open $badname_target");
1265                 return undef;
1266         }
1267         close(BADNAME_TARGET);
1268         chmod 0666, $badname_target;
1269
1270         ##
1271         ## create mangleable directory names in $manglenames_shrdir
1272         ##
1273         my $manglename_target = "$manglenames_shrdir/foo:bar";
1274         mkdir($manglename_target, 0777);
1275
1276         ##
1277         ## create symlinks for widelinks tests.
1278         ##
1279         my $widelinks_target = "$widelinks_linkdir/target";
1280         unless (open(WIDELINKS_TARGET, ">$widelinks_target")) {
1281                 warn("Unable to open $widelinks_target");
1282                 return undef;
1283         }
1284         close(WIDELINKS_TARGET);
1285         chmod 0666, $widelinks_target;
1286         ##
1287         ## This link should get ACCESS_DENIED
1288         ##
1289         symlink "$widelinks_target", "$widelinks_shrdir/source";
1290         ##
1291         ## This link should be allowed
1292         ##
1293         symlink "$widelinks_shrdir", "$widelinks_shrdir/dot";
1294
1295         my $conffile="$libdir/server.conf";
1296         my $dfqconffile="$libdir/dfq.conf";
1297
1298         my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/lib/nss_wrapper/nss_wrapper.pl";
1299         my $nss_wrapper_passwd = "$privatedir/passwd";
1300         my $nss_wrapper_group = "$privatedir/group";
1301         my $nss_wrapper_hosts = "$ENV{SELFTEST_PREFIX}/hosts";
1302         my $resolv_conf = "$privatedir/resolv.conf";
1303         my $dns_host_file = "$ENV{SELFTEST_PREFIX}/dns_host_file";
1304
1305         my $mod_printer_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/printing/modprinter.pl";
1306
1307         my $fake_snap_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/fake_snap.pl";
1308
1309         my @eventlog_list = ("dns server", "application");
1310
1311         ##
1312         ## calculate uids and gids
1313         ##
1314
1315         my ($max_uid, $max_gid);
1316         my ($uid_nobody, $uid_root, $uid_pdbtest, $uid_pdbtest2, $uid_userdup);
1317         my ($uid_pdbtest_wkn);
1318         my ($uid_smbget);
1319         my ($uid_force_user);
1320         my ($gid_nobody, $gid_nogroup, $gid_root, $gid_domusers, $gid_domadmins);
1321         my ($gid_userdup, $gid_everyone);
1322         my ($gid_force_user);
1323
1324         if ($unix_uid < 0xffff - 7) {
1325                 $max_uid = 0xffff;
1326         } else {
1327                 $max_uid = $unix_uid;
1328         }
1329
1330         $uid_root = $max_uid - 1;
1331         $uid_nobody = $max_uid - 2;
1332         $uid_pdbtest = $max_uid - 3;
1333         $uid_pdbtest2 = $max_uid - 4;
1334         $uid_userdup = $max_uid - 5;
1335         $uid_pdbtest_wkn = $max_uid - 6;
1336         $uid_force_user = $max_uid - 7;
1337         $uid_smbget = $max_uid - 8;
1338
1339         if ($unix_gids[0] < 0xffff - 8) {
1340                 $max_gid = 0xffff;
1341         } else {
1342                 $max_gid = $unix_gids[0];
1343         }
1344
1345         $gid_nobody = $max_gid - 1;
1346         $gid_nogroup = $max_gid - 2;
1347         $gid_root = $max_gid - 3;
1348         $gid_domusers = $max_gid - 4;
1349         $gid_domadmins = $max_gid - 5;
1350         $gid_userdup = $max_gid - 6;
1351         $gid_everyone = $max_gid - 7;
1352         $gid_force_user = $max_gid - 8;
1353
1354         ##
1355         ## create conffile
1356         ##
1357
1358         unless (open(CONF, ">$conffile")) {
1359                 warn("Unable to open $conffile");
1360                 return undef;
1361         }
1362         print CONF "
1363 [global]
1364         netbios name = $server
1365         interfaces = $server_ip/8 $server_ipv6/64
1366         bind interfaces only = yes
1367         panic action = cd $self->{srcdir} && $self->{srcdir}/selftest/gdb_backtrace %d %\$(MAKE_TEST_BINARY)
1368         smbd:suicide mode = yes
1369
1370         workgroup = $domain
1371
1372         private dir = $privatedir
1373         pid directory = $piddir
1374         lock directory = $lockdir
1375         log file = $logdir/log.\%m
1376         log level = 1
1377         debug pid = yes
1378         max log size = 0
1379
1380         state directory = $lockdir
1381         cache directory = $lockdir
1382
1383         passdb backend = tdbsam
1384
1385         time server = yes
1386
1387         add user script =               $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
1388         add group script =              $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action add --name %g
1389         add machine script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
1390         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
1391         delete user script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action delete --name %u
1392         delete group script =           $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action delete --name %g
1393         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
1394
1395         addprinter command =            $mod_printer_pl -a -s $conffile --
1396         deleteprinter command =         $mod_printer_pl -d -s $conffile --
1397
1398         eventlog list = application \"dns server\"
1399
1400         kernel oplocks = no
1401         kernel change notify = no
1402         smb2 leases = yes
1403
1404         logging = file
1405         printing = bsd
1406         printcap name = /dev/null
1407
1408         winbindd socket directory = $wbsockdir
1409         nmbd:socket dir = $nmbdsockdir
1410         idmap config * : range = 100000-200000
1411         winbind enum users = yes
1412         winbind enum groups = yes
1413         winbind separator = /
1414
1415 #       min receivefile size = 4000
1416
1417         read only = no
1418         server signing = auto
1419
1420         smbd:sharedelay = 100000
1421         smbd:writetimeupdatedelay = 500000
1422         map hidden = no
1423         map system = no
1424         map readonly = no
1425         store dos attributes = yes
1426         create mask = 755
1427         dos filemode = yes
1428         strict rename = yes
1429         strict sync = yes
1430         vfs objects = acl_xattr fake_acls xattr_tdb streams_depot
1431
1432         printing = vlp
1433         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1434         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1435         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1436         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1437         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1438         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1439         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1440         lpq cache time = 0
1441         print notify backchannel = yes
1442
1443         ncalrpc dir = $prefix_abs/ncalrpc
1444
1445         # The samba3.blackbox.smbclient_s3 test uses this to test that
1446         # sending messages works, and that the %m sub works.
1447         message command = mv %s $shrdir/message.%m
1448
1449         # fsrvp server requires registry shares
1450         registry shares = yes
1451
1452         # Used by RPC SRVSVC tests
1453         add share command = $bindir_abs/smbaddshare
1454         change share command = $bindir_abs/smbchangeshare
1455         delete share command = $bindir_abs/smbdeleteshare
1456
1457         # fruit:copyfile is a global option
1458         fruit:copyfile = yes
1459
1460         #this does not mean that we use non-secure test env,
1461         #it just means we ALLOW one to be configured.
1462         allow insecure wide links = yes
1463
1464         # Begin extra options
1465         $extra_options
1466         # End extra options
1467
1468         #Include user defined custom parameters if set
1469 ";
1470
1471         if (defined($ENV{INCLUDE_CUSTOM_CONF})) {
1472                 print CONF "\t$ENV{INCLUDE_CUSTOM_CONF}\n";
1473         }
1474
1475         print CONF "
1476 [tmp]
1477         path = $shrdir
1478         comment = smb username is [%U]
1479 [tmpsort]
1480         path = $shrdir
1481         comment = Load dirsort module
1482         vfs objects = dirsort acl_xattr fake_acls xattr_tdb streams_depot
1483 [tmpenc]
1484         path = $shrdir
1485         comment = encrypt smb username is [%U]
1486         smb encrypt = required
1487         vfs objects = dirsort
1488 [tmpguest]
1489         path = $shrdir
1490         guest ok = yes
1491 [guestonly]
1492         path = $shrdir
1493         guest only = yes
1494         guest ok = yes
1495 [forceuser]
1496         path = $shrdir
1497         force user = $unix_name
1498         guest ok = yes
1499 [forceuser_unixonly]
1500         comment = force a user with unix user SID and group SID
1501         path = $shrdir
1502         force user = pdbtest
1503         guest ok = yes
1504 [forceuser_wkngroup]
1505         comment = force a user with well-known group SID
1506         path = $shrdir
1507         force user = pdbtest_wkn
1508         guest ok = yes
1509 [forcegroup]
1510         path = $shrdir
1511         force group = nogroup
1512         guest ok = yes
1513 [ro-tmp]
1514         path = $ro_shrdir
1515         guest ok = yes
1516 [write-list-tmp]
1517         path = $shrdir
1518         read only = yes
1519         write list = $unix_name
1520 [valid-users-tmp]
1521         path = $shrdir
1522         valid users = $unix_name
1523         access based share enum = yes
1524 [msdfs-share]
1525         path = $msdfs_shrdir
1526         msdfs root = yes
1527         msdfs shuffle referrals = yes
1528         guest ok = yes
1529 [hideunread]
1530         copy = tmp
1531         hide unreadable = yes
1532 [tmpcase]
1533         copy = tmp
1534         case sensitive = yes
1535 [hideunwrite]
1536         copy = tmp
1537         hide unwriteable files = yes
1538 [durable]
1539         copy = tmp
1540         kernel share modes = no
1541         kernel oplocks = no
1542         posix locking = no
1543 [fs_specific]
1544         copy = tmp
1545         $fs_specific_conf
1546 [print1]
1547         copy = tmp
1548         printable = yes
1549
1550 [print2]
1551         copy = print1
1552 [print3]
1553         copy = print1
1554         default devmode = no
1555 [lp]
1556         copy = print1
1557
1558 [nfs4acl_simple]
1559         path = $shrdir
1560         comment = smb username is [%U]
1561         nfs4:mode = simple
1562         vfs objects = nfs4acl_xattr xattr_tdb
1563
1564 [nfs4acl_special]
1565         path = $shrdir
1566         comment = smb username is [%U]
1567         nfs4:mode = special
1568         vfs objects = nfs4acl_xattr xattr_tdb
1569
1570 [xcopy_share]
1571         path = $shrdir
1572         comment = smb username is [%U]
1573         create mask = 777
1574         force create mode = 777
1575 [posix_share]
1576         path = $shrdir
1577         comment = smb username is [%U]
1578         create mask = 0777
1579         force create mode = 0
1580         directory mask = 0777
1581         force directory mode = 0
1582         vfs objects = xattr_tdb
1583 [aio]
1584         copy = tmp
1585         aio read size = 1
1586         aio write size = 1
1587
1588 [print\$]
1589         copy = tmp
1590
1591 [vfs_fruit]
1592         path = $shrdir
1593         vfs objects = catia fruit streams_xattr acl_xattr
1594         ea support = yes
1595         fruit:ressource = file
1596         fruit:metadata = netatalk
1597         fruit:locking = netatalk
1598         fruit:encoding = native
1599
1600 [badname-tmp]
1601         path = $badnames_shrdir
1602         guest ok = yes
1603
1604 [manglenames_share]
1605         path = $manglenames_shrdir
1606         guest ok = yes
1607
1608 [dynamic_share]
1609         path = $shrdir/%R
1610         guest ok = yes
1611
1612 [widelinks_share]
1613         path = $widelinks_shrdir
1614         wide links = no
1615         guest ok = yes
1616
1617 [fsrvp_share]
1618         path = $shrdir
1619         comment = fake shapshots using rsync
1620         vfs objects = shell_snap shadow_copy2
1621         shell_snap:check path command = $fake_snap_pl --check
1622         shell_snap:create command = $fake_snap_pl --create
1623         shell_snap:delete command = $fake_snap_pl --delete
1624         # a relative path here fails, the snapshot dir is no longer found
1625         shadow:snapdir = $shrdir/.snapshots
1626
1627 [shadow1]
1628         path = $shadow_shrdir
1629         comment = previous versions snapshots under mount point
1630         vfs objects = shadow_copy2
1631         shadow:mountpoint = $shadow_mntdir
1632
1633 [shadow2]
1634         path = $shadow_shrdir
1635         comment = previous versions snapshots outside mount point
1636         vfs objects = shadow_copy2
1637         shadow:mountpoint = $shadow_mntdir
1638         shadow:snapdir = $shadow_tstdir/.snapshots
1639
1640 [shadow3]
1641         path = $shadow_shrdir
1642         comment = previous versions with subvolume snapshots, snapshots under base dir
1643         vfs objects = shadow_copy2
1644         shadow:mountpoint = $shadow_mntdir
1645         shadow:basedir = $shadow_basedir
1646         shadow:snapdir = $shadow_basedir/.snapshots
1647
1648 [shadow4]
1649         path = $shadow_shrdir
1650         comment = previous versions with subvolume snapshots, snapshots outside mount point
1651         vfs objects = shadow_copy2
1652         shadow:mountpoint = $shadow_mntdir
1653         shadow:basedir = $shadow_basedir
1654         shadow:snapdir = $shadow_tstdir/.snapshots
1655
1656 [shadow5]
1657         path = $shadow_shrdir
1658         comment = previous versions at volume root snapshots under mount point
1659         vfs objects = shadow_copy2
1660         shadow:mountpoint = $shadow_shrdir
1661
1662 [shadow6]
1663         path = $shadow_shrdir
1664         comment = previous versions at volume root snapshots outside mount point
1665         vfs objects = shadow_copy2
1666         shadow:mountpoint = $shadow_shrdir
1667         shadow:snapdir = $shadow_tstdir/.snapshots
1668
1669 [shadow7]
1670         path = $shadow_shrdir
1671         comment = previous versions snapshots everywhere
1672         vfs objects = shadow_copy2
1673         shadow:mountpoint = $shadow_mntdir
1674         shadow:snapdirseverywhere = yes
1675
1676 [shadow8]
1677         path = $shadow_shrdir
1678         comment = previous versions using snapsharepath
1679         vfs objects = shadow_copy2
1680         shadow:mountpoint = $shadow_mntdir
1681         shadow:snapdir = $shadow_tstdir/.snapshots
1682         shadow:snapsharepath = share
1683
1684 [shadow_wl]
1685         path = $shadow_shrdir
1686         comment = previous versions with wide links allowed
1687         vfs objects = shadow_copy2
1688         shadow:mountpoint = $shadow_mntdir
1689         wide links = yes
1690 [dfq]
1691         path = $shrdir/dfree
1692         vfs objects = fake_dfq
1693         admin users = $unix_name
1694         include = $dfqconffile
1695         ";
1696         close(CONF);
1697
1698         unless (open(DFQCONF, ">$dfqconffile")) {
1699                 warn("Unable to open $dfqconffile");
1700                 return undef;
1701         }
1702         close(DFQCONF);
1703
1704         ##
1705         ## create a test account
1706         ##
1707
1708         unless (open(PASSWD, ">$nss_wrapper_passwd")) {
1709            warn("Unable to open $nss_wrapper_passwd");
1710            return undef;
1711         } 
1712         print PASSWD "nobody:x:$uid_nobody:$gid_nobody:nobody gecos:$prefix_abs:/bin/false
1713 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
1714 pdbtest:x:$uid_pdbtest:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
1715 pdbtest2:x:$uid_pdbtest2:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
1716 userdup:x:$uid_userdup:$gid_userdup:userdup gecos:$prefix_abs:/bin/false
1717 pdbtest_wkn:x:$uid_pdbtest_wkn:$gid_everyone:pdbtest_wkn gecos:$prefix_abs:/bin/false
1718 force_user:x:$uid_force_user:$gid_force_user:force user gecos:$prefix_abs:/bin/false
1719 smbget_user:x:$uid_smbget:$gid_domusers:smbget_user gecos:$prefix_abs:/bin/false
1720 ";
1721         if ($unix_uid != 0) {
1722                 print PASSWD "root:x:$uid_root:$gid_root:root gecos:$prefix_abs:/bin/false
1723 ";
1724         }
1725         close(PASSWD);
1726
1727         unless (open(GROUP, ">$nss_wrapper_group")) {
1728              warn("Unable to open $nss_wrapper_group");
1729              return undef;
1730         }
1731         print GROUP "nobody:x:$gid_nobody:
1732 nogroup:x:$gid_nogroup:nobody
1733 $unix_name-group:x:$unix_gids[0]:
1734 domusers:X:$gid_domusers:
1735 domadmins:X:$gid_domadmins:
1736 userdup:x:$gid_userdup:$unix_name
1737 everyone:x:$gid_everyone:
1738 force_user:x:$gid_force_user:
1739 ";
1740         if ($unix_gids[0] != 0) {
1741                 print GROUP "root:x:$gid_root:
1742 ";
1743         }
1744
1745         close(GROUP);
1746
1747         ## hosts
1748         my $hostname = lc($server);
1749         unless (open(HOSTS, ">>$nss_wrapper_hosts")) {
1750                 warn("Unable to open $nss_wrapper_hosts");
1751                 return undef;
1752         }
1753         print HOSTS "${server_ip} ${hostname}.samba.example.com ${hostname}\n";
1754         print HOSTS "${server_ipv6} ${hostname}.samba.example.com ${hostname}\n";
1755         close(HOSTS);
1756
1757         ## hosts
1758         unless (open(RESOLV_CONF, ">$resolv_conf")) {
1759                 warn("Unable to open $resolv_conf");
1760                 return undef;
1761         }
1762         if (defined($dc_server_ip) or defined($dc_server_ipv6)) {
1763                 if (defined($dc_server_ip)) {
1764                         print RESOLV_CONF "nameserver $dc_server_ip\n";
1765                 }
1766                 if (defined($dc_server_ipv6)) {
1767                         print RESOLV_CONF "nameserver $dc_server_ipv6\n";
1768                 }
1769         } else {
1770                 print RESOLV_CONF "nameserver ${server_ip}\n";
1771                 print RESOLV_CONF "nameserver ${server_ipv6}\n";
1772         }
1773         close(RESOLV_CONF);
1774
1775         foreach my $evlog (@eventlog_list) {
1776                 my $evlogtdb = "$eventlogdir/$evlog.tdb";
1777                 open(EVENTLOG, ">$evlogtdb") or die("Unable to open $evlogtdb");
1778                 close(EVENTLOG);
1779         }
1780
1781         $ENV{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1782         $ENV{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1783         $ENV{NSS_WRAPPER_HOSTS} = $nss_wrapper_hosts;
1784         $ENV{NSS_WRAPPER_HOSTNAME} = "${hostname}.samba.example.com";
1785         if ($ENV{SAMBA_DNS_FAKING}) {
1786                 $ENV{RESOLV_WRAPPER_CONF} = $resolv_conf;
1787         } else {
1788                 $ENV{RESOLV_WRAPPER_HOSTS} = $dns_host_file;
1789         }
1790
1791         createuser($self, $unix_name, $password, $conffile) || die("Unable to create user");
1792         createuser($self, "force_user", $password, $conffile) || die("Unable to create force_user");
1793         createuser($self, "smbget_user", $password, $conffile) || die("Unable to create smbget_user");
1794
1795         open(DNS_UPDATE_LIST, ">$prefix/dns_update_list") or die("Unable to open $$prefix/dns_update_list");
1796         print DNS_UPDATE_LIST "A $server. $server_ip\n";
1797         print DNS_UPDATE_LIST "AAAA $server. $server_ipv6\n";
1798         close(DNS_UPDATE_LIST);
1799
1800         $ret{SERVER_IP} = $server_ip;
1801         $ret{SERVER_IPV6} = $server_ipv6;
1802         $ret{NMBD_TEST_LOG} = "$prefix/nmbd_test.log";
1803         $ret{NMBD_TEST_LOG_POS} = 0;
1804         $ret{WINBINDD_TEST_LOG} = "$prefix/winbindd_test.log";
1805         $ret{WINBINDD_TEST_LOG_POS} = 0;
1806         $ret{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
1807         $ret{SMBD_TEST_LOG_POS} = 0;
1808         $ret{SERVERCONFFILE} = $conffile;
1809         $ret{CONFIGURATION} ="-s $conffile";
1810         $ret{SERVER} = $server;
1811         $ret{USERNAME} = $unix_name;
1812         $ret{USERID} = $unix_uid;
1813         $ret{DOMAIN} = $domain;
1814         $ret{NETBIOSNAME} = $server;
1815         $ret{PASSWORD} = $password;
1816         $ret{PIDDIR} = $piddir;
1817         $ret{SELFTEST_WINBINDD_SOCKET_DIR} = $wbsockdir;
1818         $ret{WINBINDD_PRIV_PIPE_DIR} = $wbsockprivdir;
1819         $ret{NMBD_SOCKET_DIR} = $nmbdsockdir;
1820         $ret{SOCKET_WRAPPER_DEFAULT_IFACE} = $swiface;
1821         $ret{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1822         $ret{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1823         $ret{NSS_WRAPPER_HOSTS} = $nss_wrapper_hosts;
1824         $ret{NSS_WRAPPER_HOSTNAME} = "${hostname}.samba.example.com";
1825         $ret{NSS_WRAPPER_MODULE_SO_PATH} = Samba::nss_wrapper_winbind_so_path($self);
1826         $ret{NSS_WRAPPER_MODULE_FN_PREFIX} = "winbind";
1827         if ($ENV{SAMBA_DNS_FAKING}) {
1828                 $ret{RESOLV_WRAPPER_HOSTS} = $dns_host_file;
1829         } else {
1830                 $ret{RESOLV_WRAPPER_CONF} = $resolv_conf;
1831         }
1832         $ret{LOCAL_PATH} = "$shrdir";
1833         $ret{LOGDIR} = $logdir;
1834
1835         #
1836         # Avoid hitting system krb5.conf -
1837         # An env that needs Kerberos will reset this to the real
1838         # value.
1839         #
1840         $ret{KRB5_CONFIG} = abs_path($prefix) . "/no_krb5.conf";
1841
1842         return \%ret;
1843 }
1844
1845 sub wait_for_start($$$$$)
1846 {
1847         my ($self, $envvars, $nmbd, $winbindd, $smbd) = @_;
1848         my $ret;
1849
1850         if ($nmbd eq "yes") {
1851                 my $count = 0;
1852
1853                 # give time for nbt server to register its names
1854                 print "checking for nmbd\n";
1855
1856                 # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init
1857                 my $nmblookup = Samba::bindir_path($self, "nmblookup");
1858
1859                 do {
1860                         $ret = system("$nmblookup $envvars->{CONFIGURATION} $envvars->{SERVER}");
1861                         if ($ret != 0) {
1862                                 sleep(1);
1863                         } else {
1864                                 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} __SAMBA__");
1865                                 system("$nmblookup $envvars->{CONFIGURATION} __SAMBA__");
1866                                 system("$nmblookup $envvars->{CONFIGURATION} -U 127.255.255.255 __SAMBA__");
1867                                 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} $envvars->{SERVER}");
1868                         }
1869                         $count++;
1870                 } while ($ret != 0 && $count < 10);
1871                 if ($count == 10) {
1872                         print "NMBD not reachable after 10 retries\n";
1873                         teardown_env($self, $envvars);
1874                         return 0;
1875                 }
1876         }
1877
1878         if ($winbindd eq "yes") {
1879             print "checking for winbindd\n";
1880             my $count = 0;
1881             do {
1882                 $ret = system("SELFTEST_WINBINDD_SOCKET_DIR=" . $envvars->{SELFTEST_WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " --ping-dc");
1883                 if ($ret != 0) {
1884                     sleep(2);
1885                 }
1886                 $count++;
1887             } while ($ret != 0 && $count < 10);
1888             if ($count == 10) {
1889                 print "WINBINDD not reachable after 20 seconds\n";
1890                 teardown_env($self, $envvars);
1891                 return 0;
1892             }
1893         }
1894
1895         if ($smbd eq "yes") {
1896             # make sure smbd is also up set
1897             print "wait for smbd\n";
1898
1899             my $count = 0;
1900             do {
1901                 $ret = system(Samba::bindir_path($self, "smbclient") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER} -U% -p 139");
1902                 if ($ret != 0) {
1903                     sleep(2);
1904                 }
1905                 $count++
1906             } while ($ret != 0 && $count < 10);
1907             if ($count == 10) {
1908                 print "SMBD failed to start up in a reasonable time (20sec)\n";
1909                 teardown_env($self, $envvars);
1910                 return 0;
1911             }
1912         }
1913
1914         # Ensure we have domain users mapped.
1915         $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=513 unixgroup=domusers type=domain");
1916         if ($ret != 0) {
1917             return 1;
1918         }
1919         $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=512 unixgroup=domadmins type=domain");
1920         if ($ret != 0) {
1921             return 1;
1922         }
1923         $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add sid=S-1-1-0 unixgroup=everyone type=builtin");
1924         if ($ret != 0) {
1925             return 1;
1926         }
1927
1928         if ($winbindd eq "yes") {
1929             # note: creating builtin groups requires winbindd for the
1930             # unix id allocator
1931             $ret = system("SELFTEST_WINBINDD_SOCKET_DIR=" . $envvars->{SELFTEST_WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} sam createbuiltingroup Users");
1932             if ($ret != 0) {
1933                 print "Failed to create BUILTIN\\Users group\n";
1934                 return 0;
1935             }
1936             my $count = 0;
1937             do {
1938                 system(Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} cache flush");
1939                 $ret = system("SELFTEST_WINBINDD_SOCKET_DIR=" . $envvars->{SELFTEST_WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545");
1940                 if ($ret != 0) {
1941                     sleep(2);
1942                 }
1943                 $count++;
1944             } while ($ret != 0 && $count < 10);
1945             if ($count == 10) {
1946                 print "WINBINDD not reachable after 20 seconds\n";
1947                 teardown_env($self, $envvars);
1948                 return 0;
1949             }
1950         }
1951
1952         print $self->getlog_env($envvars);
1953
1954         return 1;
1955 }
1956
1957 1;