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