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