selftest: Refactor duplicated code to set ENV vars
[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 # NOTE: Refer to the README for more details about the various testenvs,
7 # and tips about adding new testenvs.
8
9 package Samba3;
10
11 use strict;
12 use Cwd qw(abs_path);
13 use FindBin qw($RealBin);
14 use POSIX;
15 use target::Samba;
16 use File::Path 'remove_tree';
17
18 sub have_ads($) {
19         my ($self) = @_;
20         my $found_ads = 0;
21         my $smbd_build_options = Samba::bindir_path($self, "smbd") . " -b|";
22         open(IN, $smbd_build_options) or die("Unable to run $smbd_build_options: $!");
23
24         while (<IN>) {
25                 if (/WITH_ADS/) {
26                        $found_ads = 1;
27                 }
28         }
29         close IN;
30
31         # If we were not built with ADS support, pretend we were never even available
32         print "smbd does not have ADS support\n" unless $found_ads;
33         return $found_ads;
34 }
35
36 # return smb.conf parameters applicable to @path, based on the underlying
37 # filesystem type
38 sub get_fs_specific_conf($$)
39 {
40         my ($self, $path) = @_;
41         my $mods = "";
42         my $stat_out = `stat --file-system $path` or return "";
43
44         if ($stat_out =~ m/Type:\s+btrfs/) {
45                 $mods .= "streams_xattr btrfs";
46         }
47
48         if ($mods) {
49                 return "vfs objects = $mods";
50         }
51
52         return undef;
53 }
54
55 sub new($$) {
56         my ($classname, $bindir, $srcdir, $server_maxtime) = @_;
57         my $self = { vars => {},
58                      bindir => $bindir,
59                      srcdir => $srcdir,
60                      server_maxtime => $server_maxtime
61         };
62         bless $self;
63         return $self;
64 }
65
66 sub teardown_env($$)
67 {
68         my ($self, $envvars) = @_;
69         my $count = 0;
70
71         # This should cause smbd to terminate gracefully
72         close($envvars->{STDIN_PIPE});
73
74         my $smbdpid = $envvars->{SMBD_TL_PID};
75         my $nmbdpid = $envvars->{NMBD_TL_PID};
76         my $winbinddpid = $envvars->{WINBINDD_TL_PID};
77
78         # This should give it time to write out the gcov data
79         until ($count > 20) {
80             my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
81             my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
82             my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
83             if ($smbdchild == -1
84                 && $nmbdchild == -1
85                 && $winbinddchild == -1) {
86                 last;
87             }
88             sleep(1);
89             $count++;
90         }
91
92         if ($count <= 20 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
93             return;
94         }
95
96         $self->stop_sig_term($smbdpid);
97         $self->stop_sig_term($nmbdpid);
98         $self->stop_sig_term($winbinddpid);
99
100         $count = 0;
101         until ($count > 10) {
102             my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
103             my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
104             my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
105             if ($smbdchild == -1
106                 && $nmbdchild == -1
107                 && $winbinddchild == -1) {
108                 last;
109             }
110             sleep(1);
111             $count++;
112         }
113
114         if ($count <= 10 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
115             return;
116         }
117
118         warn("timelimit process did not quit on SIGTERM, sending SIGKILL");
119         $self->stop_sig_kill($smbdpid);
120         $self->stop_sig_kill($nmbdpid);
121         $self->stop_sig_kill($winbinddpid);
122
123         return 0;
124 }
125
126 sub getlog_env_app($$$)
127 {
128         my ($self, $envvars, $name) = @_;
129
130         my $title = "$name LOG of: $envvars->{NETBIOSNAME}\n";
131         my $out = $title;
132
133         open(LOG, "<".$envvars->{$name."_TEST_LOG"});
134
135         seek(LOG, $envvars->{$name."_TEST_LOG_POS"}, SEEK_SET);
136         while (<LOG>) {
137                 $out .= $_;
138         }
139         $envvars->{$name."_TEST_LOG_POS"} = tell(LOG);
140         close(LOG);
141
142         return "" if $out eq $title;
143  
144         return $out;
145 }
146
147 sub getlog_env($$)
148 {
149         my ($self, $envvars) = @_;
150         my $ret = "";
151
152         $ret .= $self->getlog_env_app($envvars, "SMBD");
153         $ret .= $self->getlog_env_app($envvars, "NMBD");
154         $ret .= $self->getlog_env_app($envvars, "WINBINDD");
155
156         return $ret;
157 }
158
159 sub check_env($$)
160 {
161         my ($self, $envvars) = @_;
162
163         my $childpid = waitpid(-1, WNOHANG);
164
165         # TODO ...
166         return 1;
167 }
168
169 # Declare the environments Samba3 makes available.
170 # To be set up, they will be called as
171 #   samba3->setup_$envname($self, $path, $dep_1_vars, $dep_2_vars, ...)
172 %Samba3::ENV_DEPS = (
173         # name              => [dep_1, dep_2, ...],
174         nt4_dc              => [],
175         nt4_dc_schannel     => [],
176
177         simpleserver        => [],
178         fileserver          => [],
179         maptoguest          => [],
180         ktest               => [],
181
182         nt4_member          => ["nt4_dc"],
183
184         ad_member           => ["ad_dc"],
185         ad_member_rfc2307   => ["ad_dc_ntvfs"],
186         ad_member_idmap_rid => ["ad_dc"],
187         ad_member_idmap_ad  => ["fl2008r2dc"],
188 );
189
190 %Samba3::ENV_DEPS_POST = ();
191
192 sub setup_nt4_dc
193 {
194         my ($self, $path) = @_;
195
196         print "PROVISIONING NT4 DC...";
197
198         my $nt4_dc_options = "
199         domain master = yes
200         domain logons = yes
201         lanman auth = yes
202         ntlm auth = yes
203         raw NTLMv2 auth = yes
204         server schannel = auto
205
206         rpc_server:epmapper = external
207         rpc_server:spoolss = external
208         rpc_server:lsarpc = external
209         rpc_server:samr = external
210         rpc_server:netlogon = external
211         rpc_server:register_embedded_np = yes
212         rpc_server:FssagentRpc = external
213
214         rpc_daemon:epmd = fork
215         rpc_daemon:spoolssd = fork
216         rpc_daemon:lsasd = fork
217         rpc_daemon:fssd = fork
218         fss: sequence timeout = 1
219         check parent directory delete on close = yes
220 ";
221
222         my $vars = $self->provision($path, "SAMBA-TEST",
223                                     "LOCALNT4DC2",
224                                     "localntdc2pass",
225                                     $nt4_dc_options);
226
227         $vars or return undef;
228
229         if (not $self->check_or_start($vars, "yes", "yes", "yes")) {
230                return undef;
231         }
232
233         $vars->{DOMSID} = $vars->{SAMSID};
234         $vars->{DC_SERVER} = $vars->{SERVER};
235         $vars->{DC_SERVER_IP} = $vars->{SERVER_IP};
236         $vars->{DC_SERVER_IPV6} = $vars->{SERVER_IPV6};
237         $vars->{DC_NETBIOSNAME} = $vars->{NETBIOSNAME};
238         $vars->{DC_USERNAME} = $vars->{USERNAME};
239         $vars->{DC_PASSWORD} = $vars->{PASSWORD};
240
241         return $vars;
242 }
243
244 sub setup_nt4_dc_schannel
245 {
246         my ($self, $path) = @_;
247
248         print "PROVISIONING NT4 DC WITH SERVER SCHANNEL ...";
249
250         my $pdc_options = "
251         domain master = yes
252         domain logons = yes
253         lanman auth = yes
254
255         rpc_server:epmapper = external
256         rpc_server:spoolss = external
257         rpc_server:lsarpc = external
258         rpc_server:samr = external
259         rpc_server:netlogon = external
260         rpc_server:register_embedded_np = yes
261
262         rpc_daemon:epmd = fork
263         rpc_daemon:spoolssd = fork
264         rpc_daemon:lsasd = fork
265
266         server schannel = yes
267         # used to reproduce bug #12772
268         server max protocol = SMB2_02
269 ";
270
271         my $vars = $self->provision($path, "NT4SCHANNEL",
272                                     "LOCALNT4DC9",
273                                     "localntdc9pass",
274                                     $pdc_options);
275
276         $vars or return undef;
277
278         if (not $self->check_or_start($vars, "yes", "yes", "yes")) {
279                return undef;
280         }
281
282         $vars->{DOMSID} = $vars->{SAMSID};
283         $vars->{DC_SERVER} = $vars->{SERVER};
284         $vars->{DC_SERVER_IP} = $vars->{SERVER_IP};
285         $vars->{DC_SERVER_IPV6} = $vars->{SERVER_IPV6};
286         $vars->{DC_NETBIOSNAME} = $vars->{NETBIOSNAME};
287         $vars->{DC_USERNAME} = $vars->{USERNAME};
288         $vars->{DC_PASSWORD} = $vars->{PASSWORD};
289
290         return $vars;
291 }
292
293 sub setup_nt4_member
294 {
295         my ($self, $prefix, $nt4_dc_vars) = @_;
296         my $count = 0;
297         my $rc;
298
299         print "PROVISIONING MEMBER...";
300
301         my $require_mutexes = "dbwrap_tdb_require_mutexes:* = yes";
302         $require_mutexes = "" if ($ENV{SELFTEST_DONT_REQUIRE_TDB_MUTEX_SUPPORT} eq "1");
303
304         my $member_options = "
305         security = domain
306         dbwrap_tdb_mutexes:* = yes
307         ${require_mutexes}
308 ";
309         my $ret = $self->provision($prefix, $nt4_dc_vars->{DOMAIN},
310                                    "LOCALNT4MEMBER3",
311                                    "localnt4member3pass",
312                                    $member_options);
313
314         $ret or return undef;
315
316         my $nmblookup = Samba::bindir_path($self, "nmblookup");
317         do {
318                 print "Waiting for the LOGON SERVER registration ...\n";
319                 $rc = system("$nmblookup $ret->{CONFIGURATION} $ret->{DOMAIN}\#1c");
320                 if ($rc != 0) {
321                         sleep(1);
322                 }
323                 $count++;
324         } while ($rc != 0 && $count < 10);
325         if ($count == 10) {
326                 print "NMBD not reachable after 10 retries\n";
327                 teardown_env($self, $ret);
328                 return 0;
329         }
330
331         my $net = Samba::bindir_path($self, "net");
332         # Add hosts file for name lookups
333         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
334         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
335         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
336         $cmd .= "$net rpc join $ret->{CONFIGURATION} $nt4_dc_vars->{DOMAIN} member";
337         $cmd .= " -U$nt4_dc_vars->{USERNAME}\%$nt4_dc_vars->{PASSWORD}";
338
339         if (system($cmd) != 0) {
340             warn("Join failed\n$cmd");
341             return undef;
342         }
343
344         # Add hosts file for name lookups
345         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
346         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
347         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
348         $cmd .= "$net $ret->{CONFIGURATION} primarytrust dumpinfo | grep -q 'REDACTED SECRET VALUES'";
349
350         if (system($cmd) != 0) {
351             warn("check 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->{DOMSID} = $nt4_dc_vars->{DOMSID};
360         $ret->{DC_SERVER} = $nt4_dc_vars->{SERVER};
361         $ret->{DC_SERVER_IP} = $nt4_dc_vars->{SERVER_IP};
362         $ret->{DC_SERVER_IPV6} = $nt4_dc_vars->{SERVER_IPV6};
363         $ret->{DC_NETBIOSNAME} = $nt4_dc_vars->{NETBIOSNAME};
364         $ret->{DC_USERNAME} = $nt4_dc_vars->{USERNAME};
365         $ret->{DC_PASSWORD} = $nt4_dc_vars->{PASSWORD};
366
367         return $ret;
368 }
369
370 sub setup_ad_member
371 {
372         my ($self, $prefix, $dcvars) = @_;
373
374         my $prefix_abs = abs_path($prefix);
375         my @dirs = ();
376
377         # If we didn't build with ADS, pretend this env was never available
378         if (not $self->have_ads()) {
379                 return "UNKNOWN";
380         }
381
382         print "PROVISIONING S3 AD MEMBER...";
383
384         mkdir($prefix_abs, 0777);
385
386         my $share_dir="$prefix_abs/share";
387         push(@dirs, $share_dir);
388
389         my $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}";
390         push(@dirs, $substitution_path);
391
392         $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}/U_alice";
393         push(@dirs, $substitution_path);
394
395         $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}/U_alice/G_domain users";
396         push(@dirs, $substitution_path);
397
398         # Using '/' as the winbind separator is a bad idea ...
399         $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}/u_$dcvars->{DOMAIN}";
400         push(@dirs, $substitution_path);
401
402         $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}/u_$dcvars->{DOMAIN}/alice";
403         push(@dirs, $substitution_path);
404
405         $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}/u_$dcvars->{DOMAIN}/alice/g_$dcvars->{DOMAIN}";
406         push(@dirs, $substitution_path);
407
408         $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}/u_$dcvars->{DOMAIN}/alice/g_$dcvars->{DOMAIN}/domain users";
409         push(@dirs, $substitution_path);
410
411         my $member_options = "
412         security = ads
413         workgroup = $dcvars->{DOMAIN}
414         realm = $dcvars->{REALM}
415         netbios aliases = foo bar
416         template homedir = /home/%D/%G/%U
417         auth event notification = true
418         password server = $dcvars->{SERVER}
419
420 [sub_dug]
421         path = $share_dir/D_%D/U_%U/G_%G
422         writeable = yes
423
424 [sub_dug2]
425         path = $share_dir/D_%D/u_%u/g_%g
426         writeable = yes
427
428 ";
429
430         my $ret = $self->provision($prefix, $dcvars->{DOMAIN},
431                                    "LOCALADMEMBER",
432                                    "loCalMemberPass",
433                                    $member_options,
434                                    $dcvars->{SERVER_IP},
435                                    $dcvars->{SERVER_IPV6});
436
437         $ret or return undef;
438
439         mkdir($_, 0777) foreach(@dirs);
440
441         close(USERMAP);
442         $ret->{DOMAIN} = $dcvars->{DOMAIN};
443         $ret->{REALM} = $dcvars->{REALM};
444         $ret->{DOMSID} = $dcvars->{DOMSID};
445
446         my $ctx;
447         $ctx = {};
448         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
449         $ctx->{domain} = $dcvars->{DOMAIN};
450         $ctx->{realm} = $dcvars->{REALM};
451         $ctx->{dnsname} = lc($dcvars->{REALM});
452         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
453         $ctx->{kdc_ipv6} = $dcvars->{SERVER_IPV6};
454         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
455         Samba::mk_krb5_conf($ctx, "");
456
457         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
458         $ret->{RESOLV_CONF} = $dcvars->{RESOLV_CONF};
459
460         my $net = Samba::bindir_path($self, "net");
461         # Add hosts file for name lookups
462         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
463         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
464         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
465                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
466         } else {
467                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
468         }
469         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
470         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
471         $cmd .= "$net join $ret->{CONFIGURATION}";
472         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
473
474         if (system($cmd) != 0) {
475             warn("Join failed\n$cmd");
476             return undef;
477         }
478
479         # We need world access to this share, as otherwise the domain
480         # administrator from the AD domain provided by Samba4 can't
481         # access the share for tests.
482         chmod 0777, "$prefix/share";
483
484         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
485                 return undef;
486         }
487
488         $ret->{DC_SERVER} = $dcvars->{SERVER};
489         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
490         $ret->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
491         $ret->{DC_SERVERCONFFILE} = $dcvars->{SERVERCONFFILE};
492         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
493         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
494         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
495
496         return $ret;
497 }
498
499 sub setup_ad_member_rfc2307
500 {
501         my ($self, $prefix, $dcvars) = @_;
502
503         # If we didn't build with ADS, pretend this env was never available
504         if (not $self->have_ads()) {
505                 return "UNKNOWN";
506         }
507
508         print "PROVISIONING S3 AD MEMBER WITH idmap_rfc2307 config...";
509
510         my $member_options = "
511         security = ads
512         workgroup = $dcvars->{DOMAIN}
513         realm = $dcvars->{REALM}
514         idmap cache time = 0
515         idmap negative cache time = 0
516         idmap config * : backend = autorid
517         idmap config * : range = 1000000-1999999
518         idmap config * : rangesize = 100000
519         idmap config $dcvars->{DOMAIN} : backend = rfc2307
520         idmap config $dcvars->{DOMAIN} : range = 2000000-2999999
521         idmap config $dcvars->{DOMAIN} : ldap_server = ad
522         idmap config $dcvars->{DOMAIN} : bind_path_user = ou=idmap,dc=samba,dc=example,dc=com
523         idmap config $dcvars->{DOMAIN} : bind_path_group = ou=idmap,dc=samba,dc=example,dc=com
524
525         password server = $dcvars->{SERVER}
526 ";
527
528         my $ret = $self->provision($prefix, $dcvars->{DOMAIN},
529                                    "RFC2307MEMBER",
530                                    "loCalMemberPass",
531                                    $member_options,
532                                    $dcvars->{SERVER_IP},
533                                    $dcvars->{SERVER_IPV6});
534
535         $ret or return undef;
536
537         close(USERMAP);
538         $ret->{DOMAIN} = $dcvars->{DOMAIN};
539         $ret->{REALM} = $dcvars->{REALM};
540         $ret->{DOMSID} = $dcvars->{DOMSID};
541
542         my $ctx;
543         my $prefix_abs = abs_path($prefix);
544         $ctx = {};
545         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
546         $ctx->{domain} = $dcvars->{DOMAIN};
547         $ctx->{realm} = $dcvars->{REALM};
548         $ctx->{dnsname} = lc($dcvars->{REALM});
549         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
550         $ctx->{kdc_ipv6} = $dcvars->{SERVER_IPV6};
551         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
552         Samba::mk_krb5_conf($ctx, "");
553
554         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
555         $ret->{RESOLV_CONF} = $dcvars->{RESOLV_CONF};
556
557         my $net = Samba::bindir_path($self, "net");
558         # Add hosts file for name lookups
559         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
560         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
561         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
562                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
563         } else {
564                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
565         }
566         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
567         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
568         $cmd .= "$net join $ret->{CONFIGURATION}";
569         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
570
571         if (system($cmd) != 0) {
572             warn("Join failed\n$cmd");
573             return undef;
574         }
575
576         # We need world access to this share, as otherwise the domain
577         # administrator from the AD domain provided by Samba4 can't
578         # access the share for tests.
579         chmod 0777, "$prefix/share";
580
581         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
582                 return undef;
583         }
584
585         $ret->{DC_SERVER} = $dcvars->{SERVER};
586         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
587         $ret->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
588         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
589         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
590         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
591
592         return $ret;
593 }
594
595 sub setup_ad_member_idmap_rid
596 {
597         my ($self, $prefix, $dcvars) = @_;
598
599         # If we didn't build with ADS, pretend this env was never available
600         if (not $self->have_ads()) {
601                 return "UNKNOWN";
602         }
603
604         print "PROVISIONING S3 AD MEMBER WITH idmap_rid config...";
605
606         my $member_options = "
607         security = ads
608         workgroup = $dcvars->{DOMAIN}
609         realm = $dcvars->{REALM}
610         idmap config * : backend = tdb
611         idmap config * : range = 1000000-1999999
612         idmap config $dcvars->{DOMAIN} : backend = rid
613         idmap config $dcvars->{DOMAIN} : range = 2000000-2999999
614         # Prevent overridding the provisioned lib/krb5.conf which sets certain
615         # values required for tests to succeed
616         create krb5 conf = no
617 ";
618
619         my $ret = $self->provision($prefix, $dcvars->{DOMAIN},
620                                    "IDMAPRIDMEMBER",
621                                    "loCalMemberPass",
622                                    $member_options,
623                                    $dcvars->{SERVER_IP},
624                                    $dcvars->{SERVER_IPV6});
625
626         $ret or return undef;
627
628         close(USERMAP);
629         $ret->{DOMAIN} = $dcvars->{DOMAIN};
630         $ret->{REALM} = $dcvars->{REALM};
631         $ret->{DOMSID} = $dcvars->{DOMSID};
632
633         my $ctx;
634         my $prefix_abs = abs_path($prefix);
635         $ctx = {};
636         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
637         $ctx->{domain} = $dcvars->{DOMAIN};
638         $ctx->{realm} = $dcvars->{REALM};
639         $ctx->{dnsname} = lc($dcvars->{REALM});
640         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
641         $ctx->{kdc_ipv6} = $dcvars->{SERVER_IPV6};
642         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
643         Samba::mk_krb5_conf($ctx, "");
644
645         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
646         $ret->{RESOLV_CONF} = $dcvars->{RESOLV_CONF};
647
648         my $net = Samba::bindir_path($self, "net");
649         # Add hosts file for name lookups
650         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
651         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
652         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
653                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
654         } else {
655                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
656         }
657         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
658         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
659         $cmd .= "$net join $ret->{CONFIGURATION}";
660         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
661
662         if (system($cmd) != 0) {
663             warn("Join failed\n$cmd");
664             return undef;
665         }
666
667         # We need world access to this share, as otherwise the domain
668         # administrator from the AD domain provided by Samba4 can't
669         # access the share for tests.
670         chmod 0777, "$prefix/share";
671
672         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
673                 return undef;
674         }
675
676         $ret->{DC_SERVER} = $dcvars->{SERVER};
677         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
678         $ret->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
679         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
680         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
681         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
682
683         return $ret;
684 }
685
686 sub setup_ad_member_idmap_ad
687 {
688         my ($self, $prefix, $dcvars) = @_;
689
690         # If we didn't build with ADS, pretend this env was never available
691         if (not $self->have_ads()) {
692                 return "UNKNOWN";
693         }
694
695         print "PROVISIONING S3 AD MEMBER WITH idmap_ad config...";
696
697         my $member_options = "
698         security = ads
699         workgroup = $dcvars->{DOMAIN}
700         realm = $dcvars->{REALM}
701         password server = $dcvars->{SERVER}
702         idmap config * : backend = tdb
703         idmap config * : range = 1000000-1999999
704         idmap config $dcvars->{DOMAIN} : backend = ad
705         idmap config $dcvars->{DOMAIN} : range = 2000000-2999999
706         idmap config $dcvars->{TRUST_DOMAIN} : backend = ad
707         idmap config $dcvars->{TRUST_DOMAIN} : range = 2000000-2999999
708 ";
709
710         my $ret = $self->provision($prefix, $dcvars->{DOMAIN},
711                                    "IDMAPADMEMBER",
712                                    "loCalMemberPass",
713                                    $member_options,
714                                    $dcvars->{SERVER_IP},
715                                    $dcvars->{SERVER_IPV6});
716
717         $ret or return undef;
718
719         close(USERMAP);
720         $ret->{DOMAIN} = $dcvars->{DOMAIN};
721         $ret->{REALM} = $dcvars->{REALM};
722         $ret->{DOMSID} = $dcvars->{DOMSID};
723
724         my $ctx;
725         my $prefix_abs = abs_path($prefix);
726         $ctx = {};
727         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
728         $ctx->{domain} = $dcvars->{DOMAIN};
729         $ctx->{realm} = $dcvars->{REALM};
730         $ctx->{dnsname} = lc($dcvars->{REALM});
731         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
732         $ctx->{kdc_ipv6} = $dcvars->{SERVER_IPV6};
733         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
734         Samba::mk_krb5_conf($ctx, "");
735
736         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
737         $ret->{RESOLV_CONF} = $dcvars->{RESOLV_CONF};
738
739         my $net = Samba::bindir_path($self, "net");
740         # Add hosts file for name lookups
741         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
742         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
743         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
744                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
745         } else {
746                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
747         }
748         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
749         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
750         $cmd .= "$net join $ret->{CONFIGURATION}";
751         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
752
753         if (system($cmd) != 0) {
754             warn("Join failed\n$cmd");
755             return undef;
756         }
757
758         # We need world access to this share, as otherwise the domain
759         # administrator from the AD domain provided by Samba4 can't
760         # access the share for tests.
761         chmod 0777, "$prefix/share";
762
763         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
764                 return undef;
765         }
766
767         $ret->{DC_SERVER} = $dcvars->{SERVER};
768         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
769         $ret->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
770         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
771         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
772         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
773
774         $ret->{TRUST_SERVER} = $dcvars->{TRUST_SERVER};
775         $ret->{TRUST_USERNAME} = $dcvars->{TRUST_USERNAME};
776         $ret->{TRUST_PASSWORD} = $dcvars->{TRUST_PASSWORD};
777         $ret->{TRUST_DOMAIN} = $dcvars->{TRUST_DOMAIN};
778         $ret->{TRUST_REALM} = $dcvars->{TRUST_REALM};
779         $ret->{TRUST_DOMSID} = $dcvars->{TRUST_DOMSID};
780
781         return $ret;
782 }
783
784 sub setup_simpleserver
785 {
786         my ($self, $path) = @_;
787
788         print "PROVISIONING simple server...";
789
790         my $prefix_abs = abs_path($path);
791
792         my $simpleserver_options = "
793         lanman auth = yes
794         ntlm auth = yes
795         vfs objects = xattr_tdb streams_depot
796         change notify = no
797         smb encrypt = off
798
799 [vfs_aio_pthread]
800         path = $prefix_abs/share
801         read only = no
802         vfs objects = aio_pthread
803         aio_pthread:aio open = yes
804         smbd:async dosmode = no
805
806 [vfs_aio_pthread_async_dosmode_default1]
807         path = $prefix_abs/share
808         read only = no
809         vfs objects = aio_pthread
810         store dos attributes = yes
811         aio_pthread:aio open = yes
812         smbd:async dosmode = yes
813
814 [vfs_aio_pthread_async_dosmode_default2]
815         path = $prefix_abs/share
816         read only = no
817         vfs objects = aio_pthread xattr_tdb
818         store dos attributes = yes
819         aio_pthread:aio open = yes
820         smbd:async dosmode = yes
821
822 [vfs_aio_pthread_async_dosmode_force_sync1]
823         path = $prefix_abs/share
824         read only = no
825         vfs objects = aio_pthread
826         store dos attributes = yes
827         aio_pthread:aio open = yes
828         smbd:async dosmode = yes
829         # This simulates non linux systems
830         smbd:force sync user path safe threadpool = yes
831         smbd:force sync user chdir safe threadpool = yes
832         smbd:force sync root path safe threadpool = yes
833         smbd:force sync root chdir safe threadpool = yes
834
835 [vfs_aio_pthread_async_dosmode_force_sync2]
836         path = $prefix_abs/share
837         read only = no
838         vfs objects = aio_pthread xattr_tdb
839         store dos attributes = yes
840         aio_pthread:aio open = yes
841         smbd:async dosmode = yes
842         # This simulates non linux systems
843         smbd:force sync user path safe threadpool = yes
844         smbd:force sync user chdir safe threadpool = yes
845         smbd:force sync root path safe threadpool = yes
846         smbd:force sync root chdir safe threadpool = yes
847
848 [vfs_aio_fork]
849         path = $prefix_abs/share
850         vfs objects = aio_fork
851         read only = no
852         vfs_aio_fork:erratic_testing_mode=yes
853
854 [dosmode]
855         path = $prefix_abs/share
856         vfs objects =
857         store dos attributes = yes
858         hide files = /hidefile/
859         hide dot files = yes
860
861 [enc_desired]
862         path = $prefix_abs/share
863         vfs objects =
864         smb encrypt = desired
865
866 [hidenewfiles]
867         path = $prefix_abs/share
868         hide new files timeout = 5
869 ";
870
871         my $vars = $self->provision($path, "WORKGROUP",
872                                     "LOCALSHARE4",
873                                     "local4pass",
874                                     $simpleserver_options);
875
876         $vars or return undef;
877
878         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
879                return undef;
880         }
881
882         return $vars;
883 }
884
885 sub setup_fileserver
886 {
887         my ($self, $path) = @_;
888         my $prefix_abs = abs_path($path);
889         my $srcdir_abs = abs_path($self->{srcdir});
890
891         print "PROVISIONING file server ...\n";
892
893         my @dirs = ();
894
895         mkdir($prefix_abs, 0777);
896
897         my $usershare_dir="$prefix_abs/lib/usershare";
898
899         mkdir("$prefix_abs/lib", 0755);
900         remove_tree($usershare_dir);
901         mkdir($usershare_dir, 01770);
902
903         my $share_dir="$prefix_abs/share";
904
905         # Create share directory structure
906         my $lower_case_share_dir="$share_dir/lower-case";
907         push(@dirs, $lower_case_share_dir);
908
909         my $lower_case_share_dir_30000="$share_dir/lower-case-30000";
910         push(@dirs, $lower_case_share_dir_30000);
911
912         my $dfree_share_dir="$share_dir/dfree";
913         push(@dirs, $dfree_share_dir);
914         push(@dirs, "$dfree_share_dir/subdir1");
915         push(@dirs, "$dfree_share_dir/subdir2");
916         push(@dirs, "$dfree_share_dir/subdir3");
917
918         my $quotadir_dir="$share_dir/quota";
919         push(@dirs, $quotadir_dir);
920
921         my $valid_users_sharedir="$share_dir/valid_users";
922         push(@dirs,$valid_users_sharedir);
923
924         my $offline_sharedir="$share_dir/offline";
925         push(@dirs,$offline_sharedir);
926
927         my $force_user_valid_users_dir = "$share_dir/force_user_valid_users";
928         push(@dirs, $force_user_valid_users_dir);
929
930         my $smbget_sharedir="$share_dir/smbget";
931         push(@dirs,$smbget_sharedir);
932
933         my $tarmode_sharedir="$share_dir/tarmode";
934         push(@dirs,$tarmode_sharedir);
935
936         my $usershare_sharedir="$share_dir/usershares";
937         push(@dirs,$usershare_sharedir);
938
939         my $fileserver_options = "
940         kernel change notify = yes
941
942         usershare path = $usershare_dir
943         usershare max shares = 10
944         usershare allow guests = yes
945         usershare prefix allow list = $usershare_sharedir
946
947         get quota command = $prefix_abs/getset_quota.py
948         set quota command = $prefix_abs/getset_quota.py
949 [lowercase]
950         path = $lower_case_share_dir
951         comment = smb username is [%U]
952         case sensitive = True
953         default case = lower
954         preserve case = no
955         short preserve case = no
956 [lowercase-30000]
957         path = $lower_case_share_dir_30000
958         comment = smb username is [%U]
959         case sensitive = True
960         default case = lower
961         preserve case = no
962         short preserve case = no
963 [dfree]
964         path = $dfree_share_dir
965         comment = smb username is [%U]
966         dfree command = $srcdir_abs/testprogs/blackbox/dfree.sh
967 [valid-users-access]
968         path = $valid_users_sharedir
969         valid users = +userdup
970 [offline]
971         path = $offline_sharedir
972         vfs objects = offline
973
974 # BUG: https://bugzilla.samba.org/show_bug.cgi?id=9878
975 # RH BUG: https://bugzilla.redhat.com/show_bug.cgi?id=1077651
976 [force_user_valid_users]
977         path = $force_user_valid_users_dir
978         comment = force user with valid users combination test share
979         valid users = +force_user
980         force user = force_user
981         force group = everyone
982         write list = force_user
983
984 [smbget]
985         path = $smbget_sharedir
986         comment = smb username is [%U]
987         guest ok = yes
988 [ign_sysacls]
989         path = $share_dir
990         comment = ignore system acls
991         acl_xattr:ignore system acls = yes
992 [inherit_owner]
993         path = $share_dir
994         comment = inherit owner
995         inherit owner = yes
996 [inherit_owner_u]
997         path = $share_dir
998         comment = inherit only unix owner
999         inherit owner = unix only
1000         acl_xattr:ignore system acls = yes
1001 # BUG: https://bugzilla.samba.org/show_bug.cgi?id=13690
1002 [force_group_test]
1003         path = $share_dir
1004         comment = force group test
1005 #       force group = everyone
1006 [homes]
1007         comment = Home directories
1008         browseable = No
1009         read only = No
1010 ";
1011
1012         my $vars = $self->provision($path, "WORKGROUP",
1013                                     "FILESERVER",
1014                                     "fileserver",
1015                                     $fileserver_options,
1016                                     undef,
1017                                     undef,
1018                                     1);
1019
1020         $vars or return undef;
1021
1022         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
1023                return undef;
1024         }
1025
1026
1027         mkdir($_, 0777) foreach(@dirs);
1028
1029         ## Create case sensitive lower case share dir
1030         foreach my $file ('a'..'z') {
1031                 my $full_path = $lower_case_share_dir . '/' . $file;
1032                 open my $fh, '>', $full_path;
1033                 # Add some content to file
1034                 print $fh $full_path;
1035                 close $fh;
1036         }
1037
1038         for (my $file = 1; $file < 51; ++$file) {
1039                 my $full_path = $lower_case_share_dir . '/' . $file;
1040                 open my $fh, '>', $full_path;
1041                 # Add some content to file
1042                 print $fh $full_path;
1043                 close $fh;
1044         }
1045
1046         # Create content for 30000 share
1047         foreach my $file ('a'..'z') {
1048                 my $full_path = $lower_case_share_dir_30000 . '/' . $file;
1049                 open my $fh, '>', $full_path;
1050                 # Add some content to file
1051                 print $fh $full_path;
1052                 close $fh;
1053         }
1054
1055         for (my $file = 1; $file < 30001; ++$file) {
1056                 my $full_path = $lower_case_share_dir_30000 . '/' . $file;
1057                 open my $fh, '>', $full_path;
1058                 # Add some content to file
1059                 print $fh $full_path;
1060                 close $fh;
1061         }
1062
1063         ##
1064         ## create a listable file in valid_users_share
1065         ##
1066         my $valid_users_target = "$valid_users_sharedir/foo";
1067         unless (open(VALID_USERS_TARGET, ">$valid_users_target")) {
1068                 warn("Unable to open $valid_users_target");
1069                 return undef;
1070         }
1071         close(VALID_USERS_TARGET);
1072         chmod 0644, $valid_users_target;
1073
1074         return $vars;
1075 }
1076
1077 sub setup_ktest
1078 {
1079         my ($self, $prefix) = @_;
1080
1081         # If we didn't build with ADS, pretend this env was never available
1082         if (not $self->have_ads()) {
1083                 return "UNKNOWN";
1084         }
1085
1086         print "PROVISIONING server with security=ads...";
1087
1088         my $ktest_options = "
1089         workgroup = KTEST
1090         realm = ktest.samba.example.com
1091         security = ads
1092         username map = $prefix/lib/username.map
1093         server signing = required
1094         server min protocol = SMB3_00
1095         client max protocol = SMB3
1096
1097         # This disables NTLM auth against the local SAM, which
1098         # we use can then test this setting by.
1099         ntlm auth = disabled
1100 ";
1101
1102         my $ret = $self->provision($prefix, "KTEST",
1103                                    "LOCALKTEST6",
1104                                    "localktest6pass",
1105                                    $ktest_options);
1106
1107         $ret or return undef;
1108
1109         my $ctx;
1110         my $prefix_abs = abs_path($prefix);
1111         $ctx = {};
1112         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
1113         $ctx->{domain} = "KTEST";
1114         $ctx->{realm} = "KTEST.SAMBA.EXAMPLE.COM";
1115         $ctx->{dnsname} = lc($ctx->{realm});
1116         $ctx->{kdc_ipv4} = "0.0.0.0";
1117         $ctx->{kdc_ipv6} = "::";
1118         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
1119         Samba::mk_krb5_conf($ctx, "");
1120
1121         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
1122
1123         open(USERMAP, ">$prefix/lib/username.map") or die("Unable to open $prefix/lib/username.map");
1124         print USERMAP "
1125 $ret->{USERNAME} = KTEST\\Administrator
1126 ";
1127         close(USERMAP);
1128
1129 #This is the secrets.tdb created by 'net ads join' from Samba3 to a
1130 #Samba4 DC with the same parameters as are being used here.  The
1131 #domain SID is S-1-5-21-1071277805-689288055-3486227160
1132         $ret->{SAMSID} = "S-1-5-21-1911091480-1468226576-2729736297";
1133         $ret->{DOMSID} = "S-1-5-21-1071277805-689288055-3486227160";
1134
1135         system("cp $self->{srcdir}/source3/selftest/ktest-secrets.tdb $prefix/private/secrets.tdb");
1136         chmod 0600, "$prefix/private/secrets.tdb";
1137
1138 #Make sure there's no old ntdb file.
1139         system("rm -f $prefix/private/secrets.ntdb");
1140
1141 #This uses a pre-calculated krb5 credentials cache, obtained by running Samba4 with:
1142 # "--option=kdc:service ticket lifetime=239232" "--option=kdc:user ticket lifetime=239232" "--option=kdc:renewal lifetime=239232"
1143 #
1144 #and having in krb5.conf:
1145 # ticket_lifetime = 799718400
1146 # renew_lifetime = 799718400
1147 #
1148 # The commands for the -2 keytab where were:
1149 # kinit administrator@KTEST.SAMBA.EXAMPLE.COM
1150 # kvno host/localktest6@KTEST.SAMBA.EXAMPLE.COM
1151 # kvno cifs/localktest6@KTEST.SAMBA.EXAMPLE.COM
1152 # kvno host/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
1153 # kvno cifs/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
1154 #
1155 # and then for the -3 keytab, I did
1156 #
1157 # net changetrustpw; kdestroy and the same again.
1158 #
1159 # This creates a credential cache with a very long lifetime (2036 at
1160 # at 2011-04), and shows that running 'net changetrustpw' does not
1161 # break existing logins (for the secrets.tdb method at least).
1162 #
1163
1164         $ret->{KRB5_CCACHE}="FILE:$prefix/krb5_ccache";
1165
1166         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-2 $prefix/krb5_ccache-2");
1167         chmod 0600, "$prefix/krb5_ccache-2";
1168
1169         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-3 $prefix/krb5_ccache-3");
1170         chmod 0600, "$prefix/krb5_ccache-3";
1171
1172         # We need world access to this share, as otherwise the domain
1173         # administrator from the AD domain provided by ktest can't
1174         # access the share for tests.
1175         chmod 0777, "$prefix/share";
1176
1177         if (not $self->check_or_start($ret, "yes", "no", "yes")) {
1178                return undef;
1179         }
1180         return $ret;
1181 }
1182
1183 sub setup_maptoguest
1184 {
1185         my ($self, $path) = @_;
1186
1187         print "PROVISIONING maptoguest...";
1188
1189         my $options = "
1190 map to guest = bad user
1191 ntlm auth = yes
1192 ";
1193
1194         my $vars = $self->provision($path, "WORKGROUP",
1195                                     "maptoguest",
1196                                     "maptoguestpass",
1197                                     $options);
1198
1199         $vars or return undef;
1200
1201         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
1202                return undef;
1203         }
1204
1205         return $vars;
1206 }
1207
1208 sub stop_sig_term($$) {
1209         my ($self, $pid) = @_;
1210         kill("USR1", $pid) or kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
1211 }
1212
1213 sub stop_sig_kill($$) {
1214         my ($self, $pid) = @_;
1215         kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
1216 }
1217
1218 sub write_pid($$$)
1219 {
1220         my ($env_vars, $app, $pid) = @_;
1221
1222         open(PID, ">$env_vars->{PIDDIR}/timelimit.$app.pid");
1223         print PID $pid;
1224         close(PID);
1225 }
1226
1227 sub read_pid($$)
1228 {
1229         my ($env_vars, $app) = @_;
1230
1231         open(PID, "<$env_vars->{PIDDIR}/timelimit.$app.pid");
1232         my $pid = <PID>;
1233         close(PID);
1234         return $pid;
1235 }
1236
1237 sub check_or_start($$$$$) {
1238         my ($self, $env_vars, $nmbd, $winbindd, $smbd) = @_;
1239
1240         # use a pipe for stdin in the child processes. This allows
1241         # those processes to monitor the pipe for EOF to ensure they
1242         # exit when the test script exits
1243         pipe(STDIN_READER, $env_vars->{STDIN_PIPE});
1244
1245         unlink($env_vars->{NMBD_TEST_LOG});
1246         print "STARTING NMBD...";
1247         my $pid = fork();
1248         if ($pid == 0) {
1249                 open STDOUT, ">$env_vars->{NMBD_TEST_LOG}";
1250                 open STDERR, '>&STDOUT';
1251
1252                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
1253
1254                 my $skip_resolv_wrapper = 1;
1255                 Samba::set_env_for_process("nmbd", $env_vars, $skip_resolv_wrapper);
1256
1257                 if ($nmbd ne "yes") {
1258                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
1259                                 my $signame = shift;
1260                                 print("Skip nmbd received signal $signame");
1261                                 exit 0;
1262                         };
1263                         sleep($self->{server_maxtime});
1264                         exit 0;
1265                 }
1266
1267                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "nmbd");
1268                 my @optargs = ("-d0");
1269                 if (defined($ENV{NMBD_OPTIONS})) {
1270                         @optargs = split(/ /, $ENV{NMBD_OPTIONS});
1271                 }
1272                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
1273                 if(defined($ENV{NMBD_VALGRIND})) { 
1274                         @preargs = split(/ /, $ENV{NMBD_VALGRIND});
1275                 }
1276                 my @args = ("-F", "--no-process-group",
1277                             "-s", $env_vars->{SERVERCONFFILE},
1278                             "-l", $env_vars->{LOGDIR});
1279                 if (not defined($ENV{NMBD_DONT_LOG_STDOUT})) {
1280                         push(@args, "--log-stdout");
1281                 }
1282
1283                 close($env_vars->{STDIN_PIPE});
1284                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1285
1286                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1287                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1288         }
1289         $env_vars->{NMBD_TL_PID} = $pid;
1290         write_pid($env_vars, "nmbd", $pid);
1291         print "DONE\n";
1292
1293         unlink($env_vars->{WINBINDD_TEST_LOG});
1294         print "STARTING WINBINDD...";
1295         $pid = fork();
1296         if ($pid == 0) {
1297                 open STDOUT, ">$env_vars->{WINBINDD_TEST_LOG}";
1298                 open STDERR, '>&STDOUT';
1299
1300                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
1301
1302                 Samba::set_env_for_process("winbindd", $env_vars);
1303
1304                 if ($winbindd ne "yes") {
1305                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
1306                                 my $signame = shift;
1307                                 print("Skip winbindd received signal $signame");
1308                                 exit 0;
1309                         };
1310                         sleep($self->{server_maxtime});
1311                         exit 0;
1312                 }
1313
1314                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "winbindd");
1315                 my @optargs = ("-d0");
1316                 if (defined($ENV{WINBINDD_OPTIONS})) {
1317                         @optargs = split(/ /, $ENV{WINBINDD_OPTIONS});
1318                 }
1319                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
1320                 if(defined($ENV{WINBINDD_VALGRIND})) {
1321                         @preargs = split(/ /, $ENV{WINBINDD_VALGRIND});
1322                 }
1323                 my @args = ("-F", "--no-process-group",
1324                             "-s", $env_vars->{SERVERCONFFILE},
1325                             "-l", $env_vars->{LOGDIR});
1326                 if (not defined($ENV{WINBINDD_DONT_LOG_STDOUT})) {
1327                         push(@args, "--stdout");
1328                 }
1329
1330                 close($env_vars->{STDIN_PIPE});
1331                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1332
1333                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1334                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1335         }
1336         $env_vars->{WINBINDD_TL_PID} = $pid;
1337         write_pid($env_vars, "winbindd", $pid);
1338         print "DONE\n";
1339
1340         unlink($env_vars->{SMBD_TEST_LOG});
1341         print "STARTING SMBD...";
1342         $pid = fork();
1343         if ($pid == 0) {
1344                 open STDOUT, ">$env_vars->{SMBD_TEST_LOG}";
1345                 open STDERR, '>&STDOUT';
1346
1347                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
1348
1349                 Samba::set_env_for_process("smbd", $env_vars);
1350
1351                 if ($smbd ne "yes") {
1352                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
1353                                 my $signame = shift;
1354                                 print("Skip smbd received signal $signame");
1355                                 exit 0;
1356                         };
1357                         sleep($self->{server_maxtime});
1358                         exit 0;
1359                 }
1360
1361                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "smbd");
1362                 my @optargs = ("-d0");
1363                 if (defined($ENV{SMBD_OPTIONS})) {
1364                         @optargs = split(/ /, $ENV{SMBD_OPTIONS});
1365                 }
1366                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
1367                 if(defined($ENV{SMBD_VALGRIND})) {
1368                         @preargs = split(/ /,$ENV{SMBD_VALGRIND});
1369                 }
1370                 my @args = ("-F", "--no-process-group",
1371                             "-s", $env_vars->{SERVERCONFFILE},
1372                             "-l", $env_vars->{LOGDIR});
1373                 if (not defined($ENV{SMBD_DONT_LOG_STDOUT})) {
1374                         push(@args, "--log-stdout");
1375                 }
1376
1377                 close($env_vars->{STDIN_PIPE});
1378                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1379
1380                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1381                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1382         }
1383         $env_vars->{SMBD_TL_PID} = $pid;
1384         write_pid($env_vars, "smbd", $pid);
1385         print "DONE\n";
1386
1387         close(STDIN_READER);
1388
1389         return $self->wait_for_start($env_vars, $nmbd, $winbindd, $smbd);
1390 }
1391
1392 sub createuser($$$$$)
1393 {
1394         my ($self, $username, $password, $conffile, $env) = @_;
1395         my $cmd = "UID_WRAPPER_ROOT=1 " . Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $username > /dev/null";
1396
1397         keys %$env;
1398         while(my($var, $val) = each %$env) {
1399                 $cmd = "$var=\"$val\" $cmd";
1400         }
1401
1402         unless (open(PWD, "|$cmd")) {
1403             warn("Unable to set password for $username account\n$cmd");
1404             return undef;
1405         }
1406         print PWD "$password\n$password\n";
1407         unless (close(PWD)) {
1408             warn("Unable to set password for $username account\n$cmd");
1409             return undef;
1410         }
1411 }
1412
1413 sub provision($$$$$$$$$)
1414 {
1415         my ($self, $prefix, $domain, $server, $password, $extra_options, $dc_server_ip, $dc_server_ipv6, $no_delete_prefix) = @_;
1416
1417         ##
1418         ## setup the various environment variables we need
1419         ##
1420
1421         my $samsid = Samba::random_domain_sid();
1422         my $swiface = Samba::get_interface($server);
1423         my %ret = ();
1424         my %createuser_env = ();
1425         my $server_ip = Samba::get_ipv4_addr($server);
1426         my $server_ipv6 = Samba::get_ipv6_addr($server);
1427
1428         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `PATH=/usr/ucb:$ENV{PATH} whoami`);
1429         chomp $unix_name;
1430         my $unix_uid = $>;
1431         my $unix_gids_str = $);
1432         my @unix_gids = split(" ", $unix_gids_str);
1433
1434         my $prefix_abs = abs_path($prefix);
1435         my $bindir_abs = abs_path($self->{bindir});
1436
1437         my @dirs = ();
1438
1439         my $shrdir="$prefix_abs/share";
1440         push(@dirs,$shrdir);
1441
1442         my $libdir="$prefix_abs/lib";
1443         push(@dirs,$libdir);
1444
1445         my $piddir="$prefix_abs/pid";
1446         push(@dirs,$piddir);
1447
1448         my $privatedir="$prefix_abs/private";
1449         push(@dirs,$privatedir);
1450
1451         my $binddnsdir = "$prefix_abs/bind-dns";
1452         push(@dirs, $binddnsdir);
1453
1454         my $lockdir="$prefix_abs/lockdir";
1455         push(@dirs,$lockdir);
1456
1457         my $eventlogdir="$prefix_abs/lockdir/eventlog";
1458         push(@dirs,$eventlogdir);
1459
1460         my $logdir="$prefix_abs/logs";
1461         push(@dirs,$logdir);
1462
1463         my $driver32dir="$shrdir/W32X86";
1464         push(@dirs,$driver32dir);
1465
1466         my $driver64dir="$shrdir/x64";
1467         push(@dirs,$driver64dir);
1468
1469         my $driver40dir="$shrdir/WIN40";
1470         push(@dirs,$driver40dir);
1471
1472         my $ro_shrdir="$shrdir/root-tmp";
1473         push(@dirs,$ro_shrdir);
1474
1475         my $msdfs_shrdir="$shrdir/msdfsshare";
1476         push(@dirs,$msdfs_shrdir);
1477
1478         my $msdfs_deeppath="$msdfs_shrdir/deeppath";
1479         push(@dirs,$msdfs_deeppath);
1480
1481         my $badnames_shrdir="$shrdir/badnames";
1482         push(@dirs,$badnames_shrdir);
1483
1484         my $lease1_shrdir="$shrdir/SMB2_10";
1485         push(@dirs,$lease1_shrdir);
1486
1487         my $lease2_shrdir="$shrdir/SMB3_00";
1488         push(@dirs,$lease2_shrdir);
1489
1490         my $manglenames_shrdir="$shrdir/manglenames";
1491         push(@dirs,$manglenames_shrdir);
1492
1493         my $widelinks_shrdir="$shrdir/widelinks";
1494         push(@dirs,$widelinks_shrdir);
1495
1496         my $widelinks_linkdir="$shrdir/widelinks_foo";
1497         push(@dirs,$widelinks_linkdir);
1498
1499         my $shadow_tstdir="$shrdir/shadow";
1500         push(@dirs,$shadow_tstdir);
1501         my $shadow_mntdir="$shadow_tstdir/mount";
1502         push(@dirs,$shadow_mntdir);
1503         my $shadow_basedir="$shadow_mntdir/base";
1504         push(@dirs,$shadow_basedir);
1505         my $shadow_shrdir="$shadow_basedir/share";
1506         push(@dirs,$shadow_shrdir);
1507
1508         my $nosymlinks_shrdir="$shrdir/nosymlinks";
1509         push(@dirs,$nosymlinks_shrdir);
1510
1511         my $local_symlinks_shrdir="$shrdir/local_symlinks";
1512         push(@dirs,$local_symlinks_shrdir);
1513
1514         # this gets autocreated by winbindd
1515         my $wbsockdir="$prefix_abs/winbindd";
1516
1517         my $nmbdsockdir="$prefix_abs/nmbd";
1518         unlink($nmbdsockdir);
1519
1520         ## 
1521         ## create the test directory layout
1522         ##
1523         die ("prefix_abs = ''") if $prefix_abs eq "";
1524         die ("prefix_abs = '/'") if $prefix_abs eq "/";
1525
1526         mkdir($prefix_abs, 0777);
1527         print "CREATE TEST ENVIRONMENT IN '$prefix'...";
1528         if (not defined($no_delete_prefix) or not $no_delete_prefix) {
1529             system("rm -rf $prefix_abs/*");
1530         }
1531         mkdir($_, 0777) foreach(@dirs);
1532
1533         my $fs_specific_conf = $self->get_fs_specific_conf($shrdir);
1534
1535         ##
1536         ## lockdir and piddir must be 0755
1537         ##
1538         chmod 0755, $lockdir;
1539         chmod 0755, $piddir;
1540
1541
1542         ##
1543         ## create ro and msdfs share layout
1544         ##
1545
1546         chmod 0755, $ro_shrdir;
1547         my $unreadable_file = "$ro_shrdir/unreadable_file";
1548         unless (open(UNREADABLE_FILE, ">$unreadable_file")) {
1549                 warn("Unable to open $unreadable_file");
1550                 return undef;
1551         }
1552         close(UNREADABLE_FILE);
1553         chmod 0600, $unreadable_file;
1554
1555         my $msdfs_target = "$ro_shrdir/msdfs-target";
1556         unless (open(MSDFS_TARGET, ">$msdfs_target")) {
1557                 warn("Unable to open $msdfs_target");
1558                 return undef;
1559         }
1560         close(MSDFS_TARGET);
1561         chmod 0666, $msdfs_target;
1562         symlink "msdfs:$server_ip\\ro-tmp,$server_ipv6\\ro-tmp",
1563                 "$msdfs_shrdir/msdfs-src1";
1564         symlink "msdfs:$server_ipv6\\ro-tmp", "$msdfs_shrdir/deeppath/msdfs-src2";
1565
1566         ##
1567         ## create bad names in $badnames_shrdir
1568         ##
1569         ## (An invalid name, would be mangled to 8.3).
1570         my $badname_target = "$badnames_shrdir/\340|\231\216\377\177";
1571         unless (open(BADNAME_TARGET, ">$badname_target")) {
1572                 warn("Unable to open $badname_target");
1573                 return undef;
1574         }
1575         close(BADNAME_TARGET);
1576         chmod 0666, $badname_target;
1577
1578         ## (A bad name, would not be mangled to 8.3).
1579         my $badname_target = "$badnames_shrdir/\240\276\346\327\377\177";
1580         unless (open(BADNAME_TARGET, ">$badname_target")) {
1581                 warn("Unable to open $badname_target");
1582                 return undef;
1583         }
1584         close(BADNAME_TARGET);
1585         chmod 0666, $badname_target;
1586
1587         ## (A bad good name).
1588         my $badname_target = "$badnames_shrdir/blank.txt";
1589         unless (open(BADNAME_TARGET, ">$badname_target")) {
1590                 warn("Unable to open $badname_target");
1591                 return undef;
1592         }
1593         close(BADNAME_TARGET);
1594         chmod 0666, $badname_target;
1595
1596         ##
1597         ## create mangleable directory names in $manglenames_shrdir
1598         ##
1599         my $manglename_target = "$manglenames_shrdir/foo:bar";
1600         mkdir($manglename_target, 0777);
1601
1602         ##
1603         ## create symlinks for widelinks tests.
1604         ##
1605         my $widelinks_target = "$widelinks_linkdir/target";
1606         unless (open(WIDELINKS_TARGET, ">$widelinks_target")) {
1607                 warn("Unable to open $widelinks_target");
1608                 return undef;
1609         }
1610         close(WIDELINKS_TARGET);
1611         chmod 0666, $widelinks_target;
1612         ##
1613         ## This link should get ACCESS_DENIED
1614         ##
1615         symlink "$widelinks_target", "$widelinks_shrdir/source";
1616         ##
1617         ## This link should be allowed
1618         ##
1619         symlink "$widelinks_shrdir", "$widelinks_shrdir/dot";
1620
1621         my $conffile="$libdir/server.conf";
1622         my $dfqconffile="$libdir/dfq.conf";
1623         my $errorinjectconf="$libdir/error_inject.conf";
1624         my $delayinjectconf="$libdir/delay_inject.conf";
1625
1626         my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/third_party/nss_wrapper/nss_wrapper.pl";
1627         my $nss_wrapper_passwd = "$privatedir/passwd";
1628         my $nss_wrapper_group = "$privatedir/group";
1629         my $nss_wrapper_hosts = "$ENV{SELFTEST_PREFIX}/hosts";
1630         my $resolv_conf = "$privatedir/resolv.conf";
1631         my $dns_host_file = "$ENV{SELFTEST_PREFIX}/dns_host_file";
1632
1633         my $mod_printer_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/printing/modprinter.pl";
1634
1635         my $fake_snap_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/fake_snap.pl";
1636
1637         my @eventlog_list = ("dns server", "application");
1638
1639         ##
1640         ## calculate uids and gids
1641         ##
1642
1643         my ($max_uid, $max_gid);
1644         my ($uid_nobody, $uid_root, $uid_pdbtest, $uid_pdbtest2, $uid_userdup);
1645         my ($uid_pdbtest_wkn);
1646         my ($uid_smbget);
1647         my ($uid_force_user);
1648         my ($gid_nobody, $gid_nogroup, $gid_root, $gid_domusers, $gid_domadmins);
1649         my ($gid_userdup, $gid_everyone);
1650         my ($gid_force_user);
1651         my ($uid_user1);
1652         my ($uid_user2);
1653         my ($uid_gooduser);
1654         my ($uid_eviluser);
1655         my ($uid_slashuser);
1656
1657         if ($unix_uid < 0xffff - 13) {
1658                 $max_uid = 0xffff;
1659         } else {
1660                 $max_uid = $unix_uid;
1661         }
1662
1663         $uid_root = $max_uid - 1;
1664         $uid_nobody = $max_uid - 2;
1665         $uid_pdbtest = $max_uid - 3;
1666         $uid_pdbtest2 = $max_uid - 4;
1667         $uid_userdup = $max_uid - 5;
1668         $uid_pdbtest_wkn = $max_uid - 6;
1669         $uid_force_user = $max_uid - 7;
1670         $uid_smbget = $max_uid - 8;
1671         $uid_user1 = $max_uid - 9;
1672         $uid_user2 = $max_uid - 10;
1673         $uid_gooduser = $max_uid - 11;
1674         $uid_eviluser = $max_uid - 12;
1675         $uid_slashuser = $max_uid - 13;
1676
1677         if ($unix_gids[0] < 0xffff - 8) {
1678                 $max_gid = 0xffff;
1679         } else {
1680                 $max_gid = $unix_gids[0];
1681         }
1682
1683         $gid_nobody = $max_gid - 1;
1684         $gid_nogroup = $max_gid - 2;
1685         $gid_root = $max_gid - 3;
1686         $gid_domusers = $max_gid - 4;
1687         $gid_domadmins = $max_gid - 5;
1688         $gid_userdup = $max_gid - 6;
1689         $gid_everyone = $max_gid - 7;
1690         $gid_force_user = $max_gid - 8;
1691
1692         ##
1693         ## create conffile
1694         ##
1695
1696         unless (open(CONF, ">$conffile")) {
1697                 warn("Unable to open $conffile");
1698                 return undef;
1699         }
1700
1701         my $interfaces = Samba::get_interfaces_config($server);
1702
1703         print CONF "
1704 [global]
1705         netbios name = $server
1706         interfaces = $interfaces
1707         bind interfaces only = yes
1708         panic action = cd $self->{srcdir} && $self->{srcdir}/selftest/gdb_backtrace %d %\$(MAKE_TEST_BINARY)
1709         smbd:suicide mode = yes
1710
1711         workgroup = $domain
1712
1713         private dir = $privatedir
1714         binddns dir = $binddnsdir
1715         pid directory = $piddir
1716         lock directory = $lockdir
1717         log file = $logdir/log.\%m
1718         log level = 1
1719         debug pid = yes
1720         max log size = 0
1721
1722         state directory = $lockdir
1723         cache directory = $lockdir
1724
1725         passdb backend = tdbsam
1726
1727         time server = yes
1728
1729         add user script =               $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
1730         add group script =              $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action add --name %g
1731         add machine script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
1732         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
1733         delete user script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action delete --name %u
1734         delete group script =           $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action delete --name %g
1735         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
1736
1737         addprinter command =            $mod_printer_pl -a -s $conffile --
1738         deleteprinter command =         $mod_printer_pl -d -s $conffile --
1739
1740         eventlog list = application \"dns server\"
1741
1742         kernel oplocks = no
1743         kernel change notify = no
1744
1745         logging = file
1746         printing = bsd
1747         printcap name = /dev/null
1748
1749         winbindd socket directory = $wbsockdir
1750         nmbd:socket dir = $nmbdsockdir
1751         idmap config * : range = 100000-200000
1752         winbind enum users = yes
1753         winbind enum groups = yes
1754         winbind separator = /
1755         include system krb5 conf = no
1756
1757 #       min receivefile size = 4000
1758
1759         read only = no
1760
1761         smbd:sharedelay = 100000
1762         smbd:writetimeupdatedelay = 500000
1763         map hidden = no
1764         map system = no
1765         map readonly = no
1766         store dos attributes = yes
1767         create mask = 755
1768         dos filemode = yes
1769         strict rename = yes
1770         strict sync = yes
1771         vfs objects = acl_xattr fake_acls xattr_tdb streams_depot time_audit full_audit
1772
1773         full_audit:syslog = no
1774         full_audit:success = none
1775         full_audit:failure = none
1776
1777         printing = vlp
1778         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1779         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1780         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1781         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1782         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1783         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1784         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1785         lpq cache time = 0
1786         print notify backchannel = yes
1787
1788         ncalrpc dir = $prefix_abs/ncalrpc
1789
1790         # The samba3.blackbox.smbclient_s3 test uses this to test that
1791         # sending messages works, and that the %m sub works.
1792         message command = mv %s $shrdir/message.%m
1793
1794         # fsrvp server requires registry shares
1795         registry shares = yes
1796
1797         # Used by RPC SRVSVC tests
1798         add share command = $bindir_abs/smbaddshare
1799         change share command = $bindir_abs/smbchangeshare
1800         delete share command = $bindir_abs/smbdeleteshare
1801
1802         # fruit:copyfile is a global option
1803         fruit:copyfile = yes
1804
1805         #this does not mean that we use non-secure test env,
1806         #it just means we ALLOW one to be configured.
1807         allow insecure wide links = yes
1808
1809         # Begin extra options
1810         $extra_options
1811         # End extra options
1812
1813         #Include user defined custom parameters if set
1814 ";
1815
1816         if (defined($ENV{INCLUDE_CUSTOM_CONF})) {
1817                 print CONF "\t$ENV{INCLUDE_CUSTOM_CONF}\n";
1818         }
1819
1820         print CONF "
1821 [tmp]
1822         path = $shrdir
1823         comment = smb username is [%U]
1824 [tmpsort]
1825         path = $shrdir
1826         comment = Load dirsort module
1827         vfs objects = dirsort acl_xattr fake_acls xattr_tdb streams_depot
1828 [tmpenc]
1829         path = $shrdir
1830         comment = encrypt smb username is [%U]
1831         smb encrypt = required
1832         vfs objects = dirsort
1833 [tmpguest]
1834         path = $shrdir
1835         guest ok = yes
1836 [guestonly]
1837         path = $shrdir
1838         guest only = yes
1839         guest ok = yes
1840 [forceuser]
1841         path = $shrdir
1842         force user = $unix_name
1843         guest ok = yes
1844 [forceuser_unixonly]
1845         comment = force a user with unix user SID and group SID
1846         path = $shrdir
1847         force user = pdbtest
1848         guest ok = yes
1849 [forceuser_wkngroup]
1850         comment = force a user with well-known group SID
1851         path = $shrdir
1852         force user = pdbtest_wkn
1853         guest ok = yes
1854 [forcegroup]
1855         path = $shrdir
1856         force group = nogroup
1857         guest ok = yes
1858 [ro-tmp]
1859         path = $ro_shrdir
1860         guest ok = yes
1861 [write-list-tmp]
1862         path = $shrdir
1863         read only = yes
1864         write list = $unix_name
1865 [valid-users-tmp]
1866         path = $shrdir
1867         valid users = $unix_name
1868         access based share enum = yes
1869 [msdfs-share]
1870         path = $msdfs_shrdir
1871         msdfs root = yes
1872         msdfs shuffle referrals = yes
1873         guest ok = yes
1874 [hideunread]
1875         copy = tmp
1876         hide unreadable = yes
1877 [tmpcase]
1878         copy = tmp
1879         case sensitive = yes
1880 [hideunwrite]
1881         copy = tmp
1882         hide unwriteable files = yes
1883 [durable]
1884         copy = tmp
1885         kernel share modes = no
1886         kernel oplocks = no
1887         posix locking = no
1888 [fs_specific]
1889         copy = tmp
1890         $fs_specific_conf
1891 [print1]
1892         copy = tmp
1893         printable = yes
1894
1895 [print2]
1896         copy = print1
1897 [print3]
1898         copy = print1
1899         default devmode = no
1900 [lp]
1901         copy = print1
1902
1903 [nfs4acl_simple_40]
1904         path = $shrdir
1905         comment = smb username is [%U]
1906         nfs4:mode = simple
1907         nfs4acl_xattr:version = 40
1908         vfs objects = nfs4acl_xattr xattr_tdb
1909
1910 [nfs4acl_special_40]
1911         path = $shrdir
1912         comment = smb username is [%U]
1913         nfs4:mode = special
1914         nfs4acl_xattr:version = 40
1915         vfs objects = nfs4acl_xattr xattr_tdb
1916
1917 [nfs4acl_simple_41]
1918         path = $shrdir
1919         comment = smb username is [%U]
1920         nfs4:mode = simple
1921         vfs objects = nfs4acl_xattr xattr_tdb
1922
1923 [nfs4acl_xdr_40]
1924         path = $shrdir
1925         comment = smb username is [%U]
1926         vfs objects = nfs4acl_xattr xattr_tdb
1927         nfs4:mode = simple
1928         nfs4acl_xattr:encoding = xdr
1929         nfs4acl_xattr:version = 40
1930
1931 [nfs4acl_xdr_41]
1932         path = $shrdir
1933         comment = smb username is [%U]
1934         vfs objects = nfs4acl_xattr xattr_tdb
1935         nfs4:mode = simple
1936         nfs4acl_xattr:encoding = xdr
1937         nfs4acl_xattr:version = 41
1938
1939 [nfs4acl_nfs_40]
1940         path = $shrdir
1941         comment = smb username is [%U]
1942         vfs objects = nfs4acl_xattr xattr_tdb
1943         nfs4:mode = simple
1944         nfs4acl_xattr:encoding = nfs
1945         nfs4acl_xattr:version = 40
1946         nfs4acl_xattr:xattr_name = security.nfs4acl_xdr
1947
1948 [nfs4acl_nfs_41]
1949         path = $shrdir
1950         comment = smb username is [%U]
1951         vfs objects = nfs4acl_xattr xattr_tdb
1952         nfs4:mode = simple
1953         nfs4acl_xattr:encoding = nfs
1954         nfs4acl_xattr:version = 41
1955         nfs4acl_xattr:xattr_name = security.nfs4acl_xdr
1956
1957 [xcopy_share]
1958         path = $shrdir
1959         comment = smb username is [%U]
1960         create mask = 777
1961         force create mode = 777
1962 [posix_share]
1963         path = $shrdir
1964         comment = smb username is [%U]
1965         create mask = 0777
1966         force create mode = 0
1967         directory mask = 0777
1968         force directory mode = 0
1969         vfs objects = xattr_tdb streams_depot
1970 [aio]
1971         copy = tmp
1972         aio read size = 1
1973         aio write size = 1
1974
1975 [print\$]
1976         copy = tmp
1977
1978 [vfs_fruit]
1979         path = $shrdir
1980         vfs objects = catia fruit streams_xattr acl_xattr xattr_tdb
1981         fruit:resource = file
1982         fruit:metadata = netatalk
1983         fruit:locking = netatalk
1984         fruit:encoding = native
1985         fruit:veto_appledouble = no
1986
1987 [vfs_fruit_xattr]
1988         path = $shrdir
1989         # This is used by vfs.fruit tests that require real fs xattr
1990         vfs objects = catia fruit streams_xattr acl_xattr
1991         fruit:resource = file
1992         fruit:metadata = netatalk
1993         fruit:locking = netatalk
1994         fruit:encoding = native
1995         fruit:veto_appledouble = no
1996
1997 [vfs_fruit_metadata_stream]
1998         path = $shrdir
1999         vfs objects = fruit streams_xattr acl_xattr xattr_tdb
2000         fruit:resource = file
2001         fruit:metadata = stream
2002         fruit:veto_appledouble = no
2003
2004 [vfs_fruit_stream_depot]
2005         path = $shrdir
2006         vfs objects = fruit streams_depot acl_xattr xattr_tdb
2007         fruit:resource = stream
2008         fruit:metadata = stream
2009         fruit:veto_appledouble = no
2010
2011 [vfs_wo_fruit]
2012         path = $shrdir
2013         vfs objects = streams_xattr acl_xattr xattr_tdb
2014
2015 [vfs_wo_fruit_stream_depot]
2016         path = $shrdir
2017         vfs objects = streams_depot acl_xattr xattr_tdb
2018
2019 [vfs_fruit_timemachine]
2020         path = $shrdir
2021         vfs objects = fruit streams_xattr acl_xattr xattr_tdb
2022         fruit:resource = file
2023         fruit:metadata = stream
2024         fruit:time machine = yes
2025         fruit:time machine max size = 32K
2026
2027 [vfs_fruit_wipe_intentionally_left_blank_rfork]
2028         path = $shrdir
2029         vfs objects = fruit streams_xattr acl_xattr xattr_tdb
2030         fruit:resource = file
2031         fruit:metadata = stream
2032         fruit:wipe_intentionally_left_blank_rfork = true
2033         fruit:delete_empty_adfiles = false
2034         fruit:veto_appledouble = no
2035
2036 [vfs_fruit_delete_empty_adfiles]
2037         path = $shrdir
2038         vfs objects = fruit streams_xattr acl_xattr xattr_tdb
2039         fruit:resource = file
2040         fruit:metadata = stream
2041         fruit:wipe_intentionally_left_blank_rfork = true
2042         fruit:delete_empty_adfiles = true
2043         fruit:veto_appledouble = no
2044
2045 [badname-tmp]
2046         path = $badnames_shrdir
2047         guest ok = yes
2048
2049 [manglenames_share]
2050         path = $manglenames_shrdir
2051         guest ok = yes
2052
2053 [dynamic_share]
2054         path = $shrdir/%R
2055         guest ok = yes
2056
2057 [widelinks_share]
2058         path = $widelinks_shrdir
2059         wide links = no
2060         guest ok = yes
2061
2062 [fsrvp_share]
2063         path = $shrdir
2064         comment = fake shapshots using rsync
2065         vfs objects = shell_snap shadow_copy2
2066         shell_snap:check path command = $fake_snap_pl --check
2067         shell_snap:create command = $fake_snap_pl --create
2068         shell_snap:delete command = $fake_snap_pl --delete
2069         # a relative path here fails, the snapshot dir is no longer found
2070         shadow:snapdir = $shrdir/.snapshots
2071
2072 [shadow1]
2073         path = $shadow_shrdir
2074         comment = previous versions snapshots under mount point
2075         vfs objects = shadow_copy2
2076         shadow:mountpoint = $shadow_mntdir
2077
2078 [shadow2]
2079         path = $shadow_shrdir
2080         comment = previous versions snapshots outside mount point
2081         vfs objects = shadow_copy2
2082         shadow:mountpoint = $shadow_mntdir
2083         shadow:snapdir = $shadow_tstdir/.snapshots
2084
2085 [shadow3]
2086         path = $shadow_shrdir
2087         comment = previous versions with subvolume snapshots, snapshots under base dir
2088         vfs objects = shadow_copy2
2089         shadow:mountpoint = $shadow_mntdir
2090         shadow:basedir = $shadow_basedir
2091         shadow:snapdir = $shadow_basedir/.snapshots
2092
2093 [shadow4]
2094         path = $shadow_shrdir
2095         comment = previous versions with subvolume snapshots, snapshots outside mount point
2096         vfs objects = shadow_copy2
2097         shadow:mountpoint = $shadow_mntdir
2098         shadow:basedir = $shadow_basedir
2099         shadow:snapdir = $shadow_tstdir/.snapshots
2100
2101 [shadow5]
2102         path = $shadow_shrdir
2103         comment = previous versions at volume root snapshots under mount point
2104         vfs objects = shadow_copy2
2105         shadow:mountpoint = $shadow_shrdir
2106
2107 [shadow6]
2108         path = $shadow_shrdir
2109         comment = previous versions at volume root snapshots outside mount point
2110         vfs objects = shadow_copy2
2111         shadow:mountpoint = $shadow_shrdir
2112         shadow:snapdir = $shadow_tstdir/.snapshots
2113
2114 [shadow7]
2115         path = $shadow_shrdir
2116         comment = previous versions snapshots everywhere
2117         vfs objects = shadow_copy2
2118         shadow:mountpoint = $shadow_mntdir
2119         shadow:snapdirseverywhere = yes
2120
2121 [shadow8]
2122         path = $shadow_shrdir
2123         comment = previous versions using snapsharepath
2124         vfs objects = shadow_copy2
2125         shadow:mountpoint = $shadow_mntdir
2126         shadow:snapdir = $shadow_tstdir/.snapshots
2127         shadow:snapsharepath = share
2128
2129 [shadow_fmt0]
2130         comment = Testing shadow:format with default option
2131         vfs object = shadow_copy2
2132         path = $shadow_shrdir
2133         read only = no
2134         guest ok = yes
2135         shadow:mountpoint = $shadow_mntdir
2136         shadow:basedir = $shadow_basedir
2137         shadow:snapdir = $shadow_basedir/.snapshots
2138         shadow:format = \@GMT-%Y.%m.%d-%H.%M.%S
2139
2140 [shadow_fmt1]
2141         comment = Testing shadow:format with only date component
2142         vfs object = shadow_copy2
2143         path = $shadow_shrdir
2144         read only = no
2145         guest ok = yes
2146         shadow:mountpoint = $shadow_mntdir
2147         shadow:basedir = $shadow_basedir
2148         shadow:snapdir = $shadow_basedir/.snapshots
2149         shadow:format = \@GMT-%Y-%m-%d
2150
2151 [shadow_fmt2]
2152         comment = Testing shadow:format with some hardcoded prefix
2153         vfs object = shadow_copy2
2154         path = $shadow_shrdir
2155         read only = no
2156         guest ok = yes
2157         shadow:mountpoint = $shadow_mntdir
2158         shadow:basedir = $shadow_basedir
2159         shadow:snapdir = $shadow_basedir/.snapshots
2160         shadow:format = snap\@GMT-%Y.%m.%d-%H.%M.%S
2161
2162 [shadow_fmt3]
2163         comment = Testing shadow:format with modified format
2164         vfs object = shadow_copy2
2165         path = $shadow_shrdir
2166         read only = no
2167         guest ok = yes
2168         shadow:mountpoint = $shadow_mntdir
2169         shadow:basedir = $shadow_basedir
2170         shadow:snapdir = $shadow_basedir/.snapshots
2171         shadow:format = \@GMT-%Y.%m.%d-%H_%M_%S-snap
2172
2173 [shadow_fmt4]
2174         comment = Testing shadow:snapprefix regex
2175         vfs object = shadow_copy2
2176         path = $shadow_shrdir
2177         read only = no
2178         guest ok = yes
2179         shadow:mountpoint = $shadow_mntdir
2180         shadow:basedir = $shadow_basedir
2181         shadow:snapdir = $shadow_basedir/.snapshots
2182         shadow:snapprefix = \^s[a-z]*p\$
2183         shadow:format = _GMT-%Y.%m.%d-%H.%M.%S
2184
2185 [shadow_fmt5]
2186         comment = Testing shadow:snapprefix with delim regex
2187         vfs object = shadow_copy2
2188         path = $shadow_shrdir
2189         read only = no
2190         guest ok = yes
2191         shadow:mountpoint = $shadow_mntdir
2192         shadow:basedir = $shadow_basedir
2193         shadow:snapdir = $shadow_basedir/.snapshots
2194         shadow:delimiter = \@GMT
2195         shadow:snapprefix = [a-z]*
2196         shadow:format = \@GMT-%Y.%m.%d-%H.%M.%S
2197
2198 [shadow_wl]
2199         path = $shadow_shrdir
2200         comment = previous versions with wide links allowed
2201         vfs objects = shadow_copy2
2202         shadow:mountpoint = $shadow_mntdir
2203         wide links = yes
2204
2205 [shadow_write]
2206         path = $shadow_tstdir
2207         comment = previous versions snapshots under mount point
2208         vfs objects = shadow_copy2 streams_xattr error_inject
2209         aio write size = 0
2210         error_inject:pwrite = EBADF
2211         shadow:mountpoint = $shadow_tstdir
2212
2213 [dfq]
2214         path = $shrdir/dfree
2215         vfs objects = acl_xattr fake_acls xattr_tdb fake_dfq
2216         admin users = $unix_name
2217         include = $dfqconffile
2218 [dfq_cache]
2219         path = $shrdir/dfree
2220         vfs objects = acl_xattr fake_acls xattr_tdb fake_dfq
2221         admin users = $unix_name
2222         include = $dfqconffile
2223         dfree cache time = 60
2224 [dfq_owner]
2225         path = $shrdir/dfree
2226         vfs objects = acl_xattr fake_acls xattr_tdb fake_dfq
2227         inherit owner = yes
2228         include = $dfqconffile
2229 [quotadir]
2230         path = $shrdir/quota
2231         admin users = $unix_name
2232
2233 [acl_xattr_ign_sysacl_posix]
2234         copy = tmp
2235         acl_xattr:ignore system acls = yes
2236         acl_xattr:default acl style = posix
2237 [acl_xattr_ign_sysacl_windows]
2238         copy = tmp
2239         acl_xattr:ignore system acls = yes
2240         acl_xattr:default acl style = windows
2241
2242 [mangle_illegal]
2243         copy = tmp
2244         mangled names = illegal
2245
2246 [nosymlinks]
2247         copy = tmp
2248         path = $nosymlinks_shrdir
2249         follow symlinks = no
2250
2251 [local_symlinks]
2252         copy = tmp
2253         path = $local_symlinks_shrdir
2254         follow symlinks = yes
2255
2256 [kernel_oplocks]
2257         copy = tmp
2258         kernel oplocks = yes
2259         vfs objects = streams_xattr xattr_tdb
2260
2261 [streams_xattr]
2262         copy = tmp
2263         vfs objects = streams_xattr xattr_tdb
2264
2265 [compound_find]
2266         copy = tmp
2267         smbd:find async delay usec = 10000
2268 [error_inject]
2269         copy = tmp
2270         vfs objects = error_inject
2271         include = $errorinjectconf
2272
2273 [delay_inject]
2274         copy = tmp
2275         vfs objects = delay_inject
2276         kernel share modes = no
2277         kernel oplocks = no
2278         posix locking = no
2279         include = $delayinjectconf
2280
2281 [aio_delay_inject]
2282         copy = tmp
2283         vfs objects = delay_inject
2284         delay_inject:pread_send = 2000
2285         delay_inject:pwrite_send = 2000
2286
2287 [delete_readonly]
2288         path = $prefix_abs/share
2289         delete readonly = yes
2290         ";
2291         close(CONF);
2292
2293         my $net = Samba::bindir_path($self, "net");
2294         my $cmd = "";
2295         $cmd .= "SMB_CONF_PATH=\"$conffile\" ";
2296         $cmd .= "$net setlocalsid $samsid";
2297
2298         if (system($cmd) != 0) {
2299             warn("Join failed\n$cmd");
2300             return undef;
2301         }
2302
2303         unless (open(ERRORCONF, ">$errorinjectconf")) {
2304                 warn("Unable to open $errorinjectconf");
2305                 return undef;
2306         }
2307         close(ERRORCONF);
2308
2309         unless (open(DELAYCONF, ">$delayinjectconf")) {
2310                 warn("Unable to open $delayinjectconf");
2311                 return undef;
2312         }
2313         close(DELAYCONF);
2314
2315         unless (open(DFQCONF, ">$dfqconffile")) {
2316                 warn("Unable to open $dfqconffile");
2317                 return undef;
2318         }
2319         close(DFQCONF);
2320
2321         ##
2322         ## create a test account
2323         ##
2324
2325         unless (open(PASSWD, ">$nss_wrapper_passwd")) {
2326            warn("Unable to open $nss_wrapper_passwd");
2327            return undef;
2328         } 
2329         print PASSWD "nobody:x:$uid_nobody:$gid_nobody:nobody gecos:$prefix_abs:/bin/false
2330 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
2331 pdbtest:x:$uid_pdbtest:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
2332 pdbtest2:x:$uid_pdbtest2:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
2333 userdup:x:$uid_userdup:$gid_userdup:userdup gecos:$prefix_abs:/bin/false
2334 pdbtest_wkn:x:$uid_pdbtest_wkn:$gid_everyone:pdbtest_wkn gecos:$prefix_abs:/bin/false
2335 force_user:x:$uid_force_user:$gid_force_user:force user gecos:$prefix_abs:/bin/false
2336 smbget_user:x:$uid_smbget:$gid_domusers:smbget_user gecos:$prefix_abs:/bin/false
2337 user1:x:$uid_user1:$gid_nogroup:user1 gecos:$prefix_abs:/bin/false
2338 user2:x:$uid_user2:$gid_nogroup:user2 gecos:$prefix_abs:/bin/false
2339 gooduser:x:$uid_gooduser:$gid_domusers:gooduser gecos:$prefix_abs:/bin/false
2340 eviluser:x:$uid_eviluser:$gid_domusers:eviluser gecos::/bin/false
2341 slashuser:x:$uid_slashuser:$gid_domusers:slashuser gecos:/:/bin/false
2342 ";
2343         if ($unix_uid != 0) {
2344                 print PASSWD "root:x:$uid_root:$gid_root:root gecos:$prefix_abs:/bin/false
2345 ";
2346         }
2347         close(PASSWD);
2348
2349         unless (open(GROUP, ">$nss_wrapper_group")) {
2350              warn("Unable to open $nss_wrapper_group");
2351              return undef;
2352         }
2353         print GROUP "nobody:x:$gid_nobody:
2354 nogroup:x:$gid_nogroup:nobody
2355 $unix_name-group:x:$unix_gids[0]:
2356 domusers:X:$gid_domusers:
2357 domadmins:X:$gid_domadmins:
2358 userdup:x:$gid_userdup:$unix_name
2359 everyone:x:$gid_everyone:
2360 force_user:x:$gid_force_user:
2361 ";
2362         if ($unix_gids[0] != 0) {
2363                 print GROUP "root:x:$gid_root:
2364 ";
2365         }
2366
2367         close(GROUP);
2368
2369         ## hosts
2370         my $hostname = lc($server);
2371         unless (open(HOSTS, ">>$nss_wrapper_hosts")) {
2372                 warn("Unable to open $nss_wrapper_hosts");
2373                 return undef;
2374         }
2375         print HOSTS "${server_ip} ${hostname}.samba.example.com ${hostname}\n";
2376         print HOSTS "${server_ipv6} ${hostname}.samba.example.com ${hostname}\n";
2377         close(HOSTS);
2378
2379         ## hosts
2380         unless (open(RESOLV_CONF, ">$resolv_conf")) {
2381                 warn("Unable to open $resolv_conf");
2382                 return undef;
2383         }
2384         if (defined($dc_server_ip) or defined($dc_server_ipv6)) {
2385                 if (defined($dc_server_ip)) {
2386                         print RESOLV_CONF "nameserver $dc_server_ip\n";
2387                 }
2388                 if (defined($dc_server_ipv6)) {
2389                         print RESOLV_CONF "nameserver $dc_server_ipv6\n";
2390                 }
2391         } else {
2392                 print RESOLV_CONF "nameserver ${server_ip}\n";
2393                 print RESOLV_CONF "nameserver ${server_ipv6}\n";
2394         }
2395         close(RESOLV_CONF);
2396
2397         foreach my $evlog (@eventlog_list) {
2398                 my $evlogtdb = "$eventlogdir/$evlog.tdb";
2399                 open(EVENTLOG, ">$evlogtdb") or die("Unable to open $evlogtdb");
2400                 close(EVENTLOG);
2401         }
2402
2403         $createuser_env{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
2404         $createuser_env{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
2405         $createuser_env{NSS_WRAPPER_HOSTS} = $nss_wrapper_hosts;
2406         $createuser_env{NSS_WRAPPER_HOSTNAME} = "${hostname}.samba.example.com";
2407         if ($ENV{SAMBA_DNS_FAKING}) {
2408                 $createuser_env{RESOLV_WRAPPER_HOSTS} = $dns_host_file;
2409         } else {
2410                 $createuser_env{RESOLV_WRAPPER_CONF} = $resolv_conf;
2411         }
2412
2413         createuser($self, $unix_name, $password, $conffile, \%createuser_env) || die("Unable to create user");
2414         createuser($self, "force_user", $password, $conffile, \%createuser_env) || die("Unable to create force_user");
2415         createuser($self, "smbget_user", $password, $conffile, \%createuser_env) || die("Unable to create smbget_user");
2416         createuser($self, "user1", $password, $conffile, \%createuser_env) || die("Unable to create user1");
2417         createuser($self, "user2", $password, $conffile, \%createuser_env) || die("Unable to create user2");
2418         createuser($self, "gooduser", $password, $conffile, \%createuser_env) || die("Unable to create gooduser");
2419         createuser($self, "eviluser", $password, $conffile, \%createuser_env) || die("Unable to create eviluser");
2420         createuser($self, "slashuser", $password, $conffile, \%createuser_env) || die("Unable to create slashuser");
2421
2422         open(DNS_UPDATE_LIST, ">$prefix/dns_update_list") or die("Unable to open $$prefix/dns_update_list");
2423         print DNS_UPDATE_LIST "A $server. $server_ip\n";
2424         print DNS_UPDATE_LIST "AAAA $server. $server_ipv6\n";
2425         close(DNS_UPDATE_LIST);
2426
2427         print "DONE\n";
2428
2429         $ret{SERVER_IP} = $server_ip;
2430         $ret{SERVER_IPV6} = $server_ipv6;
2431         $ret{NMBD_TEST_LOG} = "$prefix/nmbd_test.log";
2432         $ret{NMBD_TEST_LOG_POS} = 0;
2433         $ret{WINBINDD_TEST_LOG} = "$prefix/winbindd_test.log";
2434         $ret{WINBINDD_TEST_LOG_POS} = 0;
2435         $ret{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
2436         $ret{SMBD_TEST_LOG_POS} = 0;
2437         $ret{SERVERCONFFILE} = $conffile;
2438         $ret{CONFIGURATION} ="-s $conffile";
2439         $ret{LOCK_DIR} = $lockdir;
2440         $ret{SERVER} = $server;
2441         $ret{USERNAME} = $unix_name;
2442         $ret{USERID} = $unix_uid;
2443         $ret{DOMAIN} = $domain;
2444         $ret{SAMSID} = $samsid;
2445         $ret{NETBIOSNAME} = $server;
2446         $ret{PASSWORD} = $password;
2447         $ret{PIDDIR} = $piddir;
2448         $ret{SELFTEST_WINBINDD_SOCKET_DIR} = $wbsockdir;
2449         $ret{NMBD_SOCKET_DIR} = $nmbdsockdir;
2450         $ret{SOCKET_WRAPPER_DEFAULT_IFACE} = $swiface;
2451         $ret{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
2452         $ret{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
2453         $ret{NSS_WRAPPER_HOSTS} = $nss_wrapper_hosts;
2454         $ret{NSS_WRAPPER_HOSTNAME} = "${hostname}.samba.example.com";
2455         $ret{NSS_WRAPPER_MODULE_SO_PATH} = Samba::nss_wrapper_winbind_so_path($self);
2456         $ret{NSS_WRAPPER_MODULE_FN_PREFIX} = "winbind";
2457         if ($ENV{SAMBA_DNS_FAKING}) {
2458                 $ret{RESOLV_WRAPPER_HOSTS} = $dns_host_file;
2459         } else {
2460                 $ret{RESOLV_WRAPPER_CONF} = $resolv_conf;
2461         }
2462         $ret{LOCAL_PATH} = "$shrdir";
2463         $ret{LOGDIR} = $logdir;
2464
2465         #
2466         # Avoid hitting system krb5.conf -
2467         # An env that needs Kerberos will reset this to the real
2468         # value.
2469         #
2470         $ret{KRB5_CONFIG} = abs_path($prefix) . "/no_krb5.conf";
2471
2472         # Define KRB5CCNAME for each environment we set up
2473         $ret{KRB5_CCACHE} = abs_path($prefix) . "/krb5ccache";
2474         $ENV{KRB5CCNAME} = $ret{KRB5_CCACHE};
2475
2476         return \%ret;
2477 }
2478
2479 sub wait_for_start($$$$$)
2480 {
2481         my ($self, $envvars, $nmbd, $winbindd, $smbd) = @_;
2482         my $cmd;
2483         my $netcmd;
2484         my $ret;
2485
2486         if ($nmbd eq "yes") {
2487                 my $count = 0;
2488
2489                 # give time for nbt server to register its names
2490                 print "checking for nmbd\n";
2491
2492                 # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init
2493                 my $nmblookup = Samba::bindir_path($self, "nmblookup");
2494
2495                 do {
2496                         $ret = system("$nmblookup $envvars->{CONFIGURATION} $envvars->{SERVER}");
2497                         if ($ret != 0) {
2498                                 sleep(1);
2499                         } else {
2500                                 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} __SAMBA__");
2501                                 system("$nmblookup $envvars->{CONFIGURATION} __SAMBA__");
2502                                 system("$nmblookup $envvars->{CONFIGURATION} -U 127.255.255.255 __SAMBA__");
2503                                 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} $envvars->{SERVER}");
2504                         }
2505                         $count++;
2506                 } while ($ret != 0 && $count < 10);
2507                 if ($count == 10) {
2508                         print "NMBD not reachable after 10 retries\n";
2509                         teardown_env($self, $envvars);
2510                         return 0;
2511                 }
2512         }
2513
2514         if ($winbindd eq "yes") {
2515             print "checking for winbindd\n";
2516             my $count = 0;
2517             $cmd = "SELFTEST_WINBINDD_SOCKET_DIR='$envvars->{SELFTEST_WINBINDD_SOCKET_DIR}' ";
2518             $cmd .= "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2519             $cmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2520             $cmd .= Samba::bindir_path($self, "wbinfo") . " --ping-dc";
2521
2522             do {
2523                 if ($ret != 0) {
2524                     $ret = system($cmd);
2525                     sleep(1);
2526                 }
2527                 $count++;
2528             } while ($ret != 0 && $count < 20);
2529             if ($count == 20) {
2530                 print "WINBINDD not reachable after 20 seconds\n";
2531                 teardown_env($self, $envvars);
2532                 return 0;
2533             }
2534         }
2535
2536         if ($smbd eq "yes") {
2537             # make sure smbd is also up set
2538             print "wait for smbd\n";
2539
2540             my $count = 0;
2541             do {
2542                 $ret = system(Samba::bindir_path($self, "smbclient") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER} -U% -p 139");
2543                 if ($ret != 0) {
2544                     sleep(1);
2545                 }
2546                 $count++
2547             } while ($ret != 0 && $count < 20);
2548             if ($count == 20) {
2549                 print "SMBD failed to start up in a reasonable time (20sec)\n";
2550                 teardown_env($self, $envvars);
2551                 return 0;
2552             }
2553         }
2554
2555         # Ensure we have domain users mapped.
2556         $netcmd = "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2557         $netcmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2558         $netcmd .= Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} ";
2559
2560         $cmd = $netcmd . "groupmap delete ntgroup=domusers";
2561         $ret = system($cmd);
2562
2563         $cmd = $netcmd . "groupmap add rid=513 unixgroup=domusers type=domain";
2564         $ret = system($cmd);
2565         if ($ret != 0) {
2566                 print("\"$cmd\" failed\n");
2567                 return 1;
2568         }
2569
2570         $cmd = $netcmd . "groupmap delete ntgroup=domadmins";
2571         $ret = system($cmd);
2572
2573         $cmd = $netcmd . "groupmap add rid=512 unixgroup=domadmins type=domain";
2574         $ret = system($cmd);
2575         if ($ret != 0) {
2576                 print("\"$cmd\" failed\n");
2577                 return 1;
2578         }
2579
2580         $cmd = $netcmd . "groupmap delete ntgroup=everyone";
2581         $ret = system($cmd);
2582
2583         $cmd = $netcmd . "groupmap add sid=S-1-1-0 unixgroup=everyone type=builtin";
2584         $ret = system($cmd);
2585         if ($ret != 0) {
2586                 print("\"$cmd\" failed\n");
2587                 return 1;
2588         }
2589
2590         # note: creating builtin groups requires winbindd for the
2591         # unix id allocator
2592         my $create_builtin_users = "no";
2593         if ($winbindd eq "yes") {
2594                 $cmd = "SELFTEST_WINBINDD_SOCKET_DIR='$envvars->{SELFTEST_WINBINDD_SOCKET_DIR}' ";
2595                 $cmd .= "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2596                 $cmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2597                 $cmd .= Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545";
2598                 my $wbinfo_out = qx($cmd 2>&1);
2599                 if ($? != 0) {
2600                         # wbinfo doesn't give us a better error code then
2601                         # WBC_ERR_DOMAIN_NOT_FOUND, but at least that's
2602                         # different then WBC_ERR_WINBIND_NOT_AVAILABLE
2603                         if ($wbinfo_out !~ /WBC_ERR_DOMAIN_NOT_FOUND/) {
2604                                 print("Failed to run \"wbinfo --sid-to-gid=S-1-5-32-545\": $wbinfo_out");
2605                                 teardown_env($self, $envvars);
2606                                 return 0;
2607                         }
2608                         $create_builtin_users = "yes";
2609                 }
2610         }
2611         if ($create_builtin_users eq "yes") {
2612             $cmd = "SELFTEST_WINBINDD_SOCKET_DIR='$envvars->{SELFTEST_WINBINDD_SOCKET_DIR}' ";
2613             $cmd .= "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2614             $cmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2615             $cmd .= Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} ";
2616             $cmd .= "sam createbuiltingroup Users";
2617             $ret = system($cmd);
2618             if ($ret != 0) {
2619                 print "Failed to create BUILTIN\\Users group\n";
2620                 teardown_env($self, $envvars);
2621                 return 0;
2622             }
2623
2624             $cmd = Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} ";
2625             $cmd .= "cache del IDMAP/SID2XID/S-1-5-32-545";
2626             system($cmd);
2627
2628             $cmd = "SELFTEST_WINBINDD_SOCKET_DIR='$envvars->{SELFTEST_WINBINDD_SOCKET_DIR}' ";
2629             $cmd .= "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2630             $cmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2631             $cmd .= Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545";
2632             $ret = system($cmd);
2633             if ($ret != 0) {
2634                 print "Missing \"BUILTIN\\Users\", did net sam createbuiltingroup Users fail?\n";
2635                 teardown_env($self, $envvars);
2636                 return 0;
2637             }
2638         }
2639
2640         print $self->getlog_env($envvars);
2641
2642         return 1;
2643 }
2644
2645 1;